GNU Linux-libre 5.15.137-gnu
[releases.git] / fs / cifs / misc.c
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002,2008
5  *   Author(s): Steve French (sfrench@us.ibm.com)
6  *
7  */
8
9 #include <linux/slab.h>
10 #include <linux/ctype.h>
11 #include <linux/mempool.h>
12 #include <linux/vmalloc.h>
13 #include "cifspdu.h"
14 #include "cifsglob.h"
15 #include "cifsproto.h"
16 #include "cifs_debug.h"
17 #include "smberr.h"
18 #include "nterr.h"
19 #include "cifs_unicode.h"
20 #include "smb2pdu.h"
21 #include "cifsfs.h"
22 #ifdef CONFIG_CIFS_DFS_UPCALL
23 #include "dns_resolve.h"
24 #endif
25 #include "fs_context.h"
26
27 extern mempool_t *cifs_sm_req_poolp;
28 extern mempool_t *cifs_req_poolp;
29
30 /* The xid serves as a useful identifier for each incoming vfs request,
31    in a similar way to the mid which is useful to track each sent smb,
32    and CurrentXid can also provide a running counter (although it
33    will eventually wrap past zero) of the total vfs operations handled
34    since the cifs fs was mounted */
35
36 unsigned int
37 _get_xid(void)
38 {
39         unsigned int xid;
40
41         spin_lock(&GlobalMid_Lock);
42         GlobalTotalActiveXid++;
43
44         /* keep high water mark for number of simultaneous ops in filesystem */
45         if (GlobalTotalActiveXid > GlobalMaxActiveXid)
46                 GlobalMaxActiveXid = GlobalTotalActiveXid;
47         if (GlobalTotalActiveXid > 65000)
48                 cifs_dbg(FYI, "warning: more than 65000 requests active\n");
49         xid = GlobalCurrentXid++;
50         spin_unlock(&GlobalMid_Lock);
51         return xid;
52 }
53
54 void
55 _free_xid(unsigned int xid)
56 {
57         spin_lock(&GlobalMid_Lock);
58         /* if (GlobalTotalActiveXid == 0)
59                 BUG(); */
60         GlobalTotalActiveXid--;
61         spin_unlock(&GlobalMid_Lock);
62 }
63
64 struct cifs_ses *
65 sesInfoAlloc(void)
66 {
67         struct cifs_ses *ret_buf;
68
69         ret_buf = kzalloc(sizeof(struct cifs_ses), GFP_KERNEL);
70         if (ret_buf) {
71                 atomic_inc(&sesInfoAllocCount);
72                 ret_buf->status = CifsNew;
73                 ++ret_buf->ses_count;
74                 INIT_LIST_HEAD(&ret_buf->smb_ses_list);
75                 INIT_LIST_HEAD(&ret_buf->tcon_list);
76                 mutex_init(&ret_buf->session_mutex);
77                 spin_lock_init(&ret_buf->iface_lock);
78                 spin_lock_init(&ret_buf->chan_lock);
79         }
80         return ret_buf;
81 }
82
83 void
84 sesInfoFree(struct cifs_ses *buf_to_free)
85 {
86         if (buf_to_free == NULL) {
87                 cifs_dbg(FYI, "Null buffer passed to sesInfoFree\n");
88                 return;
89         }
90
91         atomic_dec(&sesInfoAllocCount);
92         kfree(buf_to_free->serverOS);
93         kfree(buf_to_free->serverDomain);
94         kfree(buf_to_free->serverNOS);
95         kfree_sensitive(buf_to_free->password);
96         kfree(buf_to_free->user_name);
97         kfree(buf_to_free->domainName);
98         kfree_sensitive(buf_to_free->auth_key.response);
99         kfree(buf_to_free->iface_list);
100         kfree_sensitive(buf_to_free);
101 }
102
103 struct cifs_tcon *
104 tconInfoAlloc(void)
105 {
106         struct cifs_tcon *ret_buf;
107
108         ret_buf = kzalloc(sizeof(*ret_buf), GFP_KERNEL);
109         if (!ret_buf)
110                 return NULL;
111         ret_buf->crfid.fid = kzalloc(sizeof(*ret_buf->crfid.fid), GFP_KERNEL);
112         if (!ret_buf->crfid.fid) {
113                 kfree(ret_buf);
114                 return NULL;
115         }
116
117         atomic_inc(&tconInfoAllocCount);
118         ret_buf->tidStatus = CifsNew;
119         ++ret_buf->tc_count;
120         INIT_LIST_HEAD(&ret_buf->openFileList);
121         INIT_LIST_HEAD(&ret_buf->tcon_list);
122         spin_lock_init(&ret_buf->open_file_lock);
123         mutex_init(&ret_buf->crfid.fid_mutex);
124         spin_lock_init(&ret_buf->stat_lock);
125         atomic_set(&ret_buf->num_local_opens, 0);
126         atomic_set(&ret_buf->num_remote_opens, 0);
127
128         return ret_buf;
129 }
130
131 void
132 tconInfoFree(struct cifs_tcon *buf_to_free)
133 {
134         if (buf_to_free == NULL) {
135                 cifs_dbg(FYI, "Null buffer passed to tconInfoFree\n");
136                 return;
137         }
138         atomic_dec(&tconInfoAllocCount);
139         kfree(buf_to_free->nativeFileSystem);
140         kfree_sensitive(buf_to_free->password);
141         kfree(buf_to_free->crfid.fid);
142         kfree(buf_to_free);
143 }
144
145 struct smb_hdr *
146 cifs_buf_get(void)
147 {
148         struct smb_hdr *ret_buf = NULL;
149         /*
150          * SMB2 header is bigger than CIFS one - no problems to clean some
151          * more bytes for CIFS.
152          */
153         size_t buf_size = sizeof(struct smb2_sync_hdr);
154
155         /*
156          * We could use negotiated size instead of max_msgsize -
157          * but it may be more efficient to always alloc same size
158          * albeit slightly larger than necessary and maxbuffersize
159          * defaults to this and can not be bigger.
160          */
161         ret_buf = mempool_alloc(cifs_req_poolp, GFP_NOFS);
162
163         /* clear the first few header bytes */
164         /* for most paths, more is cleared in header_assemble */
165         memset(ret_buf, 0, buf_size + 3);
166         atomic_inc(&bufAllocCount);
167 #ifdef CONFIG_CIFS_STATS2
168         atomic_inc(&totBufAllocCount);
169 #endif /* CONFIG_CIFS_STATS2 */
170
171         return ret_buf;
172 }
173
174 void
175 cifs_buf_release(void *buf_to_free)
176 {
177         if (buf_to_free == NULL) {
178                 /* cifs_dbg(FYI, "Null buffer passed to cifs_buf_release\n");*/
179                 return;
180         }
181         mempool_free(buf_to_free, cifs_req_poolp);
182
183         atomic_dec(&bufAllocCount);
184         return;
185 }
186
187 struct smb_hdr *
188 cifs_small_buf_get(void)
189 {
190         struct smb_hdr *ret_buf = NULL;
191
192 /* We could use negotiated size instead of max_msgsize -
193    but it may be more efficient to always alloc same size
194    albeit slightly larger than necessary and maxbuffersize
195    defaults to this and can not be bigger */
196         ret_buf = mempool_alloc(cifs_sm_req_poolp, GFP_NOFS);
197         /* No need to clear memory here, cleared in header assemble */
198         /*      memset(ret_buf, 0, sizeof(struct smb_hdr) + 27);*/
199         atomic_inc(&smBufAllocCount);
200 #ifdef CONFIG_CIFS_STATS2
201         atomic_inc(&totSmBufAllocCount);
202 #endif /* CONFIG_CIFS_STATS2 */
203
204         return ret_buf;
205 }
206
207 void
208 cifs_small_buf_release(void *buf_to_free)
209 {
210
211         if (buf_to_free == NULL) {
212                 cifs_dbg(FYI, "Null buffer passed to cifs_small_buf_release\n");
213                 return;
214         }
215         mempool_free(buf_to_free, cifs_sm_req_poolp);
216
217         atomic_dec(&smBufAllocCount);
218         return;
219 }
220
221 void
222 free_rsp_buf(int resp_buftype, void *rsp)
223 {
224         if (resp_buftype == CIFS_SMALL_BUFFER)
225                 cifs_small_buf_release(rsp);
226         else if (resp_buftype == CIFS_LARGE_BUFFER)
227                 cifs_buf_release(rsp);
228 }
229
230 /* NB: MID can not be set if treeCon not passed in, in that
231    case it is responsbility of caller to set the mid */
232 void
233 header_assemble(struct smb_hdr *buffer, char smb_command /* command */ ,
234                 const struct cifs_tcon *treeCon, int word_count
235                 /* length of fixed section (word count) in two byte units  */)
236 {
237         char *temp = (char *) buffer;
238
239         memset(temp, 0, 256); /* bigger than MAX_CIFS_HDR_SIZE */
240
241         buffer->smb_buf_length = cpu_to_be32(
242             (2 * word_count) + sizeof(struct smb_hdr) -
243             4 /*  RFC 1001 length field does not count */  +
244             2 /* for bcc field itself */) ;
245
246         buffer->Protocol[0] = 0xFF;
247         buffer->Protocol[1] = 'S';
248         buffer->Protocol[2] = 'M';
249         buffer->Protocol[3] = 'B';
250         buffer->Command = smb_command;
251         buffer->Flags = 0x00;   /* case sensitive */
252         buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES;
253         buffer->Pid = cpu_to_le16((__u16)current->tgid);
254         buffer->PidHigh = cpu_to_le16((__u16)(current->tgid >> 16));
255         if (treeCon) {
256                 buffer->Tid = treeCon->tid;
257                 if (treeCon->ses) {
258                         if (treeCon->ses->capabilities & CAP_UNICODE)
259                                 buffer->Flags2 |= SMBFLG2_UNICODE;
260                         if (treeCon->ses->capabilities & CAP_STATUS32)
261                                 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
262
263                         /* Uid is not converted */
264                         buffer->Uid = treeCon->ses->Suid;
265                         if (treeCon->ses->server)
266                                 buffer->Mid = get_next_mid(treeCon->ses->server);
267                 }
268                 if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
269                         buffer->Flags2 |= SMBFLG2_DFS;
270                 if (treeCon->nocase)
271                         buffer->Flags  |= SMBFLG_CASELESS;
272                 if ((treeCon->ses) && (treeCon->ses->server))
273                         if (treeCon->ses->server->sign)
274                                 buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
275         }
276
277 /*  endian conversion of flags is now done just before sending */
278         buffer->WordCount = (char) word_count;
279         return;
280 }
281
282 static int
283 check_smb_hdr(struct smb_hdr *smb)
284 {
285         /* does it have the right SMB "signature" ? */
286         if (*(__le32 *) smb->Protocol != cpu_to_le32(0x424d53ff)) {
287                 cifs_dbg(VFS, "Bad protocol string signature header 0x%x\n",
288                          *(unsigned int *)smb->Protocol);
289                 return 1;
290         }
291
292         /* if it's a response then accept */
293         if (smb->Flags & SMBFLG_RESPONSE)
294                 return 0;
295
296         /* only one valid case where server sends us request */
297         if (smb->Command == SMB_COM_LOCKING_ANDX)
298                 return 0;
299
300         cifs_dbg(VFS, "Server sent request, not response. mid=%u\n",
301                  get_mid(smb));
302         return 1;
303 }
304
305 int
306 checkSMB(char *buf, unsigned int total_read, struct TCP_Server_Info *server)
307 {
308         struct smb_hdr *smb = (struct smb_hdr *)buf;
309         __u32 rfclen = be32_to_cpu(smb->smb_buf_length);
310         __u32 clc_len;  /* calculated length */
311         cifs_dbg(FYI, "checkSMB Length: 0x%x, smb_buf_length: 0x%x\n",
312                  total_read, rfclen);
313
314         /* is this frame too small to even get to a BCC? */
315         if (total_read < 2 + sizeof(struct smb_hdr)) {
316                 if ((total_read >= sizeof(struct smb_hdr) - 1)
317                             && (smb->Status.CifsError != 0)) {
318                         /* it's an error return */
319                         smb->WordCount = 0;
320                         /* some error cases do not return wct and bcc */
321                         return 0;
322                 } else if ((total_read == sizeof(struct smb_hdr) + 1) &&
323                                 (smb->WordCount == 0)) {
324                         char *tmp = (char *)smb;
325                         /* Need to work around a bug in two servers here */
326                         /* First, check if the part of bcc they sent was zero */
327                         if (tmp[sizeof(struct smb_hdr)] == 0) {
328                                 /* some servers return only half of bcc
329                                  * on simple responses (wct, bcc both zero)
330                                  * in particular have seen this on
331                                  * ulogoffX and FindClose. This leaves
332                                  * one byte of bcc potentially unitialized
333                                  */
334                                 /* zero rest of bcc */
335                                 tmp[sizeof(struct smb_hdr)+1] = 0;
336                                 return 0;
337                         }
338                         cifs_dbg(VFS, "rcvd invalid byte count (bcc)\n");
339                 } else {
340                         cifs_dbg(VFS, "Length less than smb header size\n");
341                 }
342                 return -EIO;
343         }
344
345         /* otherwise, there is enough to get to the BCC */
346         if (check_smb_hdr(smb))
347                 return -EIO;
348         clc_len = smbCalcSize(smb, server);
349
350         if (4 + rfclen != total_read) {
351                 cifs_dbg(VFS, "Length read does not match RFC1001 length %d\n",
352                          rfclen);
353                 return -EIO;
354         }
355
356         if (4 + rfclen != clc_len) {
357                 __u16 mid = get_mid(smb);
358                 /* check if bcc wrapped around for large read responses */
359                 if ((rfclen > 64 * 1024) && (rfclen > clc_len)) {
360                         /* check if lengths match mod 64K */
361                         if (((4 + rfclen) & 0xFFFF) == (clc_len & 0xFFFF))
362                                 return 0; /* bcc wrapped */
363                 }
364                 cifs_dbg(FYI, "Calculated size %u vs length %u mismatch for mid=%u\n",
365                          clc_len, 4 + rfclen, mid);
366
367                 if (4 + rfclen < clc_len) {
368                         cifs_dbg(VFS, "RFC1001 size %u smaller than SMB for mid=%u\n",
369                                  rfclen, mid);
370                         return -EIO;
371                 } else if (rfclen > clc_len + 512) {
372                         /*
373                          * Some servers (Windows XP in particular) send more
374                          * data than the lengths in the SMB packet would
375                          * indicate on certain calls (byte range locks and
376                          * trans2 find first calls in particular). While the
377                          * client can handle such a frame by ignoring the
378                          * trailing data, we choose limit the amount of extra
379                          * data to 512 bytes.
380                          */
381                         cifs_dbg(VFS, "RFC1001 size %u more than 512 bytes larger than SMB for mid=%u\n",
382                                  rfclen, mid);
383                         return -EIO;
384                 }
385         }
386         return 0;
387 }
388
389 bool
390 is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv)
391 {
392         struct smb_hdr *buf = (struct smb_hdr *)buffer;
393         struct smb_com_lock_req *pSMB = (struct smb_com_lock_req *)buf;
394         struct list_head *tmp, *tmp1, *tmp2;
395         struct cifs_ses *ses;
396         struct cifs_tcon *tcon;
397         struct cifsInodeInfo *pCifsInode;
398         struct cifsFileInfo *netfile;
399
400         cifs_dbg(FYI, "Checking for oplock break or dnotify response\n");
401         if ((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) &&
402            (pSMB->hdr.Flags & SMBFLG_RESPONSE)) {
403                 struct smb_com_transaction_change_notify_rsp *pSMBr =
404                         (struct smb_com_transaction_change_notify_rsp *)buf;
405                 struct file_notify_information *pnotify;
406                 __u32 data_offset = 0;
407                 size_t len = srv->total_read - sizeof(pSMBr->hdr.smb_buf_length);
408
409                 if (get_bcc(buf) > sizeof(struct file_notify_information)) {
410                         data_offset = le32_to_cpu(pSMBr->DataOffset);
411
412                         if (data_offset >
413                             len - sizeof(struct file_notify_information)) {
414                                 cifs_dbg(FYI, "Invalid data_offset %u\n",
415                                          data_offset);
416                                 return true;
417                         }
418                         pnotify = (struct file_notify_information *)
419                                 ((char *)&pSMBr->hdr.Protocol + data_offset);
420                         cifs_dbg(FYI, "dnotify on %s Action: 0x%x\n",
421                                  pnotify->FileName, pnotify->Action);
422                         /*   cifs_dump_mem("Rcvd notify Data: ",buf,
423                                 sizeof(struct smb_hdr)+60); */
424                         return true;
425                 }
426                 if (pSMBr->hdr.Status.CifsError) {
427                         cifs_dbg(FYI, "notify err 0x%x\n",
428                                  pSMBr->hdr.Status.CifsError);
429                         return true;
430                 }
431                 return false;
432         }
433         if (pSMB->hdr.Command != SMB_COM_LOCKING_ANDX)
434                 return false;
435         if (pSMB->hdr.Flags & SMBFLG_RESPONSE) {
436                 /* no sense logging error on invalid handle on oplock
437                    break - harmless race between close request and oplock
438                    break response is expected from time to time writing out
439                    large dirty files cached on the client */
440                 if ((NT_STATUS_INVALID_HANDLE) ==
441                    le32_to_cpu(pSMB->hdr.Status.CifsError)) {
442                         cifs_dbg(FYI, "Invalid handle on oplock break\n");
443                         return true;
444                 } else if (ERRbadfid ==
445                    le16_to_cpu(pSMB->hdr.Status.DosError.Error)) {
446                         return true;
447                 } else {
448                         return false; /* on valid oplock brk we get "request" */
449                 }
450         }
451         if (pSMB->hdr.WordCount != 8)
452                 return false;
453
454         cifs_dbg(FYI, "oplock type 0x%x level 0x%x\n",
455                  pSMB->LockType, pSMB->OplockLevel);
456         if (!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE))
457                 return false;
458
459         /* look up tcon based on tid & uid */
460         spin_lock(&cifs_tcp_ses_lock);
461         list_for_each(tmp, &srv->smb_ses_list) {
462                 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
463                 list_for_each(tmp1, &ses->tcon_list) {
464                         tcon = list_entry(tmp1, struct cifs_tcon, tcon_list);
465                         if (tcon->tid != buf->Tid)
466                                 continue;
467
468                         cifs_stats_inc(&tcon->stats.cifs_stats.num_oplock_brks);
469                         spin_lock(&tcon->open_file_lock);
470                         list_for_each(tmp2, &tcon->openFileList) {
471                                 netfile = list_entry(tmp2, struct cifsFileInfo,
472                                                      tlist);
473                                 if (pSMB->Fid != netfile->fid.netfid)
474                                         continue;
475
476                                 cifs_dbg(FYI, "file id match, oplock break\n");
477                                 pCifsInode = CIFS_I(d_inode(netfile->dentry));
478
479                                 set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK,
480                                         &pCifsInode->flags);
481
482                                 netfile->oplock_epoch = 0;
483                                 netfile->oplock_level = pSMB->OplockLevel;
484                                 netfile->oplock_break_cancelled = false;
485                                 cifs_queue_oplock_break(netfile);
486
487                                 spin_unlock(&tcon->open_file_lock);
488                                 spin_unlock(&cifs_tcp_ses_lock);
489                                 return true;
490                         }
491                         spin_unlock(&tcon->open_file_lock);
492                         spin_unlock(&cifs_tcp_ses_lock);
493                         cifs_dbg(FYI, "No matching file for oplock break\n");
494                         return true;
495                 }
496         }
497         spin_unlock(&cifs_tcp_ses_lock);
498         cifs_dbg(FYI, "Can not process oplock break for non-existent connection\n");
499         return true;
500 }
501
502 void
503 dump_smb(void *buf, int smb_buf_length)
504 {
505         if (traceSMB == 0)
506                 return;
507
508         print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_NONE, 8, 2, buf,
509                        smb_buf_length, true);
510 }
511
512 void
513 cifs_autodisable_serverino(struct cifs_sb_info *cifs_sb)
514 {
515         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
516                 struct cifs_tcon *tcon = NULL;
517
518                 if (cifs_sb->master_tlink)
519                         tcon = cifs_sb_master_tcon(cifs_sb);
520
521                 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
522                 cifs_sb->mnt_cifs_serverino_autodisabled = true;
523                 cifs_dbg(VFS, "Autodisabling the use of server inode numbers on %s\n",
524                          tcon ? tcon->treeName : "new server");
525                 cifs_dbg(VFS, "The server doesn't seem to support them properly or the files might be on different servers (DFS)\n");
526                 cifs_dbg(VFS, "Hardlinks will not be recognized on this mount. Consider mounting with the \"noserverino\" option to silence this message.\n");
527
528         }
529 }
530
531 void cifs_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock)
532 {
533         oplock &= 0xF;
534
535         if (oplock == OPLOCK_EXCLUSIVE) {
536                 cinode->oplock = CIFS_CACHE_WRITE_FLG | CIFS_CACHE_READ_FLG;
537                 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
538                          &cinode->vfs_inode);
539         } else if (oplock == OPLOCK_READ) {
540                 cinode->oplock = CIFS_CACHE_READ_FLG;
541                 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
542                          &cinode->vfs_inode);
543         } else
544                 cinode->oplock = 0;
545 }
546
547 /*
548  * We wait for oplock breaks to be processed before we attempt to perform
549  * writes.
550  */
551 int cifs_get_writer(struct cifsInodeInfo *cinode)
552 {
553         int rc;
554
555 start:
556         rc = wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK,
557                          TASK_KILLABLE);
558         if (rc)
559                 return rc;
560
561         spin_lock(&cinode->writers_lock);
562         if (!cinode->writers)
563                 set_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
564         cinode->writers++;
565         /* Check to see if we have started servicing an oplock break */
566         if (test_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags)) {
567                 cinode->writers--;
568                 if (cinode->writers == 0) {
569                         clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
570                         wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
571                 }
572                 spin_unlock(&cinode->writers_lock);
573                 goto start;
574         }
575         spin_unlock(&cinode->writers_lock);
576         return 0;
577 }
578
579 void cifs_put_writer(struct cifsInodeInfo *cinode)
580 {
581         spin_lock(&cinode->writers_lock);
582         cinode->writers--;
583         if (cinode->writers == 0) {
584                 clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
585                 wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
586         }
587         spin_unlock(&cinode->writers_lock);
588 }
589
590 /**
591  * cifs_queue_oplock_break - queue the oplock break handler for cfile
592  * @cfile: The file to break the oplock on
593  *
594  * This function is called from the demultiplex thread when it
595  * receives an oplock break for @cfile.
596  *
597  * Assumes the tcon->open_file_lock is held.
598  * Assumes cfile->file_info_lock is NOT held.
599  */
600 void cifs_queue_oplock_break(struct cifsFileInfo *cfile)
601 {
602         /*
603          * Bump the handle refcount now while we hold the
604          * open_file_lock to enforce the validity of it for the oplock
605          * break handler. The matching put is done at the end of the
606          * handler.
607          */
608         cifsFileInfo_get(cfile);
609
610         queue_work(cifsoplockd_wq, &cfile->oplock_break);
611 }
612
613 void cifs_done_oplock_break(struct cifsInodeInfo *cinode)
614 {
615         clear_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags);
616         wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK);
617 }
618
619 bool
620 backup_cred(struct cifs_sb_info *cifs_sb)
621 {
622         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID) {
623                 if (uid_eq(cifs_sb->ctx->backupuid, current_fsuid()))
624                         return true;
625         }
626         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID) {
627                 if (in_group_p(cifs_sb->ctx->backupgid))
628                         return true;
629         }
630
631         return false;
632 }
633
634 void
635 cifs_del_pending_open(struct cifs_pending_open *open)
636 {
637         spin_lock(&tlink_tcon(open->tlink)->open_file_lock);
638         list_del(&open->olist);
639         spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
640 }
641
642 void
643 cifs_add_pending_open_locked(struct cifs_fid *fid, struct tcon_link *tlink,
644                              struct cifs_pending_open *open)
645 {
646         memcpy(open->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
647         open->oplock = CIFS_OPLOCK_NO_CHANGE;
648         open->tlink = tlink;
649         fid->pending_open = open;
650         list_add_tail(&open->olist, &tlink_tcon(tlink)->pending_opens);
651 }
652
653 void
654 cifs_add_pending_open(struct cifs_fid *fid, struct tcon_link *tlink,
655                       struct cifs_pending_open *open)
656 {
657         spin_lock(&tlink_tcon(tlink)->open_file_lock);
658         cifs_add_pending_open_locked(fid, tlink, open);
659         spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
660 }
661
662 /*
663  * Critical section which runs after acquiring deferred_lock.
664  * As there is no reference count on cifs_deferred_close, pdclose
665  * should not be used outside deferred_lock.
666  */
667 bool
668 cifs_is_deferred_close(struct cifsFileInfo *cfile, struct cifs_deferred_close **pdclose)
669 {
670         struct cifs_deferred_close *dclose;
671
672         list_for_each_entry(dclose, &CIFS_I(d_inode(cfile->dentry))->deferred_closes, dlist) {
673                 if ((dclose->netfid == cfile->fid.netfid) &&
674                         (dclose->persistent_fid == cfile->fid.persistent_fid) &&
675                         (dclose->volatile_fid == cfile->fid.volatile_fid)) {
676                         *pdclose = dclose;
677                         return true;
678                 }
679         }
680         return false;
681 }
682
683 /*
684  * Critical section which runs after acquiring deferred_lock.
685  */
686 void
687 cifs_add_deferred_close(struct cifsFileInfo *cfile, struct cifs_deferred_close *dclose)
688 {
689         bool is_deferred = false;
690         struct cifs_deferred_close *pdclose;
691
692         is_deferred = cifs_is_deferred_close(cfile, &pdclose);
693         if (is_deferred) {
694                 kfree(dclose);
695                 return;
696         }
697
698         dclose->tlink = cfile->tlink;
699         dclose->netfid = cfile->fid.netfid;
700         dclose->persistent_fid = cfile->fid.persistent_fid;
701         dclose->volatile_fid = cfile->fid.volatile_fid;
702         list_add_tail(&dclose->dlist, &CIFS_I(d_inode(cfile->dentry))->deferred_closes);
703 }
704
705 /*
706  * Critical section which runs after acquiring deferred_lock.
707  */
708 void
709 cifs_del_deferred_close(struct cifsFileInfo *cfile)
710 {
711         bool is_deferred = false;
712         struct cifs_deferred_close *dclose;
713
714         is_deferred = cifs_is_deferred_close(cfile, &dclose);
715         if (!is_deferred)
716                 return;
717         list_del(&dclose->dlist);
718         kfree(dclose);
719 }
720
721 void
722 cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode)
723 {
724         struct cifsFileInfo *cfile = NULL;
725         struct file_list *tmp_list, *tmp_next_list;
726         struct list_head file_head;
727
728         if (cifs_inode == NULL)
729                 return;
730
731         INIT_LIST_HEAD(&file_head);
732         spin_lock(&cifs_inode->open_file_lock);
733         list_for_each_entry(cfile, &cifs_inode->openFileList, flist) {
734                 if (delayed_work_pending(&cfile->deferred)) {
735                         if (cancel_delayed_work(&cfile->deferred)) {
736                                 spin_lock(&cifs_inode->deferred_lock);
737                                 cifs_del_deferred_close(cfile);
738                                 spin_unlock(&cifs_inode->deferred_lock);
739
740                                 tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
741                                 if (tmp_list == NULL)
742                                         break;
743                                 tmp_list->cfile = cfile;
744                                 list_add_tail(&tmp_list->list, &file_head);
745                         }
746                 }
747         }
748         spin_unlock(&cifs_inode->open_file_lock);
749
750         list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) {
751                 _cifsFileInfo_put(tmp_list->cfile, false, false);
752                 list_del(&tmp_list->list);
753                 kfree(tmp_list);
754         }
755 }
756
757 void
758 cifs_close_all_deferred_files(struct cifs_tcon *tcon)
759 {
760         struct cifsFileInfo *cfile;
761         struct list_head *tmp;
762         struct file_list *tmp_list, *tmp_next_list;
763         struct list_head file_head;
764
765         INIT_LIST_HEAD(&file_head);
766         spin_lock(&tcon->open_file_lock);
767         list_for_each(tmp, &tcon->openFileList) {
768                 cfile = list_entry(tmp, struct cifsFileInfo, tlist);
769                 if (delayed_work_pending(&cfile->deferred)) {
770                         if (cancel_delayed_work(&cfile->deferred)) {
771                                 spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
772                                 cifs_del_deferred_close(cfile);
773                                 spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
774
775                                 tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
776                                 if (tmp_list == NULL)
777                                         break;
778                                 tmp_list->cfile = cfile;
779                                 list_add_tail(&tmp_list->list, &file_head);
780                         }
781                 }
782         }
783         spin_unlock(&tcon->open_file_lock);
784
785         list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) {
786                 _cifsFileInfo_put(tmp_list->cfile, true, false);
787                 list_del(&tmp_list->list);
788                 kfree(tmp_list);
789         }
790 }
791 void
792 cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon, const char *path)
793 {
794         struct cifsFileInfo *cfile;
795         struct list_head *tmp;
796         struct file_list *tmp_list, *tmp_next_list;
797         struct list_head file_head;
798         void *page;
799         const char *full_path;
800
801         INIT_LIST_HEAD(&file_head);
802         page = alloc_dentry_path();
803         spin_lock(&tcon->open_file_lock);
804         list_for_each(tmp, &tcon->openFileList) {
805                 cfile = list_entry(tmp, struct cifsFileInfo, tlist);
806                 full_path = build_path_from_dentry(cfile->dentry, page);
807                 if (strstr(full_path, path)) {
808                         if (delayed_work_pending(&cfile->deferred)) {
809                                 if (cancel_delayed_work(&cfile->deferred)) {
810                                         spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
811                                         cifs_del_deferred_close(cfile);
812                                         spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
813
814                                         tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
815                                         if (tmp_list == NULL)
816                                                 break;
817                                         tmp_list->cfile = cfile;
818                                         list_add_tail(&tmp_list->list, &file_head);
819                                 }
820                         }
821                 }
822         }
823         spin_unlock(&tcon->open_file_lock);
824
825         list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) {
826                 _cifsFileInfo_put(tmp_list->cfile, true, false);
827                 list_del(&tmp_list->list);
828                 kfree(tmp_list);
829         }
830         free_dentry_path(page);
831 }
832
833 /* parses DFS refferal V3 structure
834  * caller is responsible for freeing target_nodes
835  * returns:
836  * - on success - 0
837  * - on failure - errno
838  */
839 int
840 parse_dfs_referrals(struct get_dfs_referral_rsp *rsp, u32 rsp_size,
841                     unsigned int *num_of_nodes,
842                     struct dfs_info3_param **target_nodes,
843                     const struct nls_table *nls_codepage, int remap,
844                     const char *searchName, bool is_unicode)
845 {
846         int i, rc = 0;
847         char *data_end;
848         struct dfs_referral_level_3 *ref;
849
850         *num_of_nodes = le16_to_cpu(rsp->NumberOfReferrals);
851
852         if (*num_of_nodes < 1) {
853                 cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
854                          *num_of_nodes);
855                 rc = -EINVAL;
856                 goto parse_DFS_referrals_exit;
857         }
858
859         ref = (struct dfs_referral_level_3 *) &(rsp->referrals);
860         if (ref->VersionNumber != cpu_to_le16(3)) {
861                 cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
862                          le16_to_cpu(ref->VersionNumber));
863                 rc = -EINVAL;
864                 goto parse_DFS_referrals_exit;
865         }
866
867         /* get the upper boundary of the resp buffer */
868         data_end = (char *)rsp + rsp_size;
869
870         cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
871                  *num_of_nodes, le32_to_cpu(rsp->DFSFlags));
872
873         *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
874                                 GFP_KERNEL);
875         if (*target_nodes == NULL) {
876                 rc = -ENOMEM;
877                 goto parse_DFS_referrals_exit;
878         }
879
880         /* collect necessary data from referrals */
881         for (i = 0; i < *num_of_nodes; i++) {
882                 char *temp;
883                 int max_len;
884                 struct dfs_info3_param *node = (*target_nodes)+i;
885
886                 node->flags = le32_to_cpu(rsp->DFSFlags);
887                 if (is_unicode) {
888                         __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
889                                                 GFP_KERNEL);
890                         if (tmp == NULL) {
891                                 rc = -ENOMEM;
892                                 goto parse_DFS_referrals_exit;
893                         }
894                         cifsConvertToUTF16((__le16 *) tmp, searchName,
895                                            PATH_MAX, nls_codepage, remap);
896                         node->path_consumed = cifs_utf16_bytes(tmp,
897                                         le16_to_cpu(rsp->PathConsumed),
898                                         nls_codepage);
899                         kfree(tmp);
900                 } else
901                         node->path_consumed = le16_to_cpu(rsp->PathConsumed);
902
903                 node->server_type = le16_to_cpu(ref->ServerType);
904                 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
905
906                 /* copy DfsPath */
907                 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
908                 max_len = data_end - temp;
909                 node->path_name = cifs_strndup_from_utf16(temp, max_len,
910                                                 is_unicode, nls_codepage);
911                 if (!node->path_name) {
912                         rc = -ENOMEM;
913                         goto parse_DFS_referrals_exit;
914                 }
915
916                 /* copy link target UNC */
917                 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
918                 max_len = data_end - temp;
919                 node->node_name = cifs_strndup_from_utf16(temp, max_len,
920                                                 is_unicode, nls_codepage);
921                 if (!node->node_name) {
922                         rc = -ENOMEM;
923                         goto parse_DFS_referrals_exit;
924                 }
925
926                 node->ttl = le32_to_cpu(ref->TimeToLive);
927
928                 ref++;
929         }
930
931 parse_DFS_referrals_exit:
932         if (rc) {
933                 free_dfs_info_array(*target_nodes, *num_of_nodes);
934                 *target_nodes = NULL;
935                 *num_of_nodes = 0;
936         }
937         return rc;
938 }
939
940 struct cifs_aio_ctx *
941 cifs_aio_ctx_alloc(void)
942 {
943         struct cifs_aio_ctx *ctx;
944
945         /*
946          * Must use kzalloc to initialize ctx->bv to NULL and ctx->direct_io
947          * to false so that we know when we have to unreference pages within
948          * cifs_aio_ctx_release()
949          */
950         ctx = kzalloc(sizeof(struct cifs_aio_ctx), GFP_KERNEL);
951         if (!ctx)
952                 return NULL;
953
954         INIT_LIST_HEAD(&ctx->list);
955         mutex_init(&ctx->aio_mutex);
956         init_completion(&ctx->done);
957         kref_init(&ctx->refcount);
958         return ctx;
959 }
960
961 void
962 cifs_aio_ctx_release(struct kref *refcount)
963 {
964         struct cifs_aio_ctx *ctx = container_of(refcount,
965                                         struct cifs_aio_ctx, refcount);
966
967         cifsFileInfo_put(ctx->cfile);
968
969         /*
970          * ctx->bv is only set if setup_aio_ctx_iter() was call successfuly
971          * which means that iov_iter_get_pages() was a success and thus that
972          * we have taken reference on pages.
973          */
974         if (ctx->bv) {
975                 unsigned i;
976
977                 for (i = 0; i < ctx->npages; i++) {
978                         if (ctx->should_dirty)
979                                 set_page_dirty(ctx->bv[i].bv_page);
980                         put_page(ctx->bv[i].bv_page);
981                 }
982                 kvfree(ctx->bv);
983         }
984
985         kfree(ctx);
986 }
987
988 #define CIFS_AIO_KMALLOC_LIMIT (1024 * 1024)
989
990 int
991 setup_aio_ctx_iter(struct cifs_aio_ctx *ctx, struct iov_iter *iter, int rw)
992 {
993         ssize_t rc;
994         unsigned int cur_npages;
995         unsigned int npages = 0;
996         unsigned int i;
997         size_t len;
998         size_t count = iov_iter_count(iter);
999         unsigned int saved_len;
1000         size_t start;
1001         unsigned int max_pages = iov_iter_npages(iter, INT_MAX);
1002         struct page **pages = NULL;
1003         struct bio_vec *bv = NULL;
1004
1005         if (iov_iter_is_kvec(iter)) {
1006                 memcpy(&ctx->iter, iter, sizeof(*iter));
1007                 ctx->len = count;
1008                 iov_iter_advance(iter, count);
1009                 return 0;
1010         }
1011
1012         if (array_size(max_pages, sizeof(*bv)) <= CIFS_AIO_KMALLOC_LIMIT)
1013                 bv = kmalloc_array(max_pages, sizeof(*bv), GFP_KERNEL);
1014
1015         if (!bv) {
1016                 bv = vmalloc(array_size(max_pages, sizeof(*bv)));
1017                 if (!bv)
1018                         return -ENOMEM;
1019         }
1020
1021         if (array_size(max_pages, sizeof(*pages)) <= CIFS_AIO_KMALLOC_LIMIT)
1022                 pages = kmalloc_array(max_pages, sizeof(*pages), GFP_KERNEL);
1023
1024         if (!pages) {
1025                 pages = vmalloc(array_size(max_pages, sizeof(*pages)));
1026                 if (!pages) {
1027                         kvfree(bv);
1028                         return -ENOMEM;
1029                 }
1030         }
1031
1032         saved_len = count;
1033
1034         while (count && npages < max_pages) {
1035                 rc = iov_iter_get_pages(iter, pages, count, max_pages, &start);
1036                 if (rc < 0) {
1037                         cifs_dbg(VFS, "Couldn't get user pages (rc=%zd)\n", rc);
1038                         break;
1039                 }
1040
1041                 if (rc > count) {
1042                         cifs_dbg(VFS, "get pages rc=%zd more than %zu\n", rc,
1043                                  count);
1044                         break;
1045                 }
1046
1047                 iov_iter_advance(iter, rc);
1048                 count -= rc;
1049                 rc += start;
1050                 cur_npages = DIV_ROUND_UP(rc, PAGE_SIZE);
1051
1052                 if (npages + cur_npages > max_pages) {
1053                         cifs_dbg(VFS, "out of vec array capacity (%u vs %u)\n",
1054                                  npages + cur_npages, max_pages);
1055                         break;
1056                 }
1057
1058                 for (i = 0; i < cur_npages; i++) {
1059                         len = rc > PAGE_SIZE ? PAGE_SIZE : rc;
1060                         bv[npages + i].bv_page = pages[i];
1061                         bv[npages + i].bv_offset = start;
1062                         bv[npages + i].bv_len = len - start;
1063                         rc -= len;
1064                         start = 0;
1065                 }
1066
1067                 npages += cur_npages;
1068         }
1069
1070         kvfree(pages);
1071         ctx->bv = bv;
1072         ctx->len = saved_len - count;
1073         ctx->npages = npages;
1074         iov_iter_bvec(&ctx->iter, rw, ctx->bv, npages, ctx->len);
1075         return 0;
1076 }
1077
1078 /**
1079  * cifs_alloc_hash - allocate hash and hash context together
1080  * @name: The name of the crypto hash algo
1081  * @shash: Where to put the pointer to the hash algo
1082  * @sdesc: Where to put the pointer to the hash descriptor
1083  *
1084  * The caller has to make sure @sdesc is initialized to either NULL or
1085  * a valid context. Both can be freed via cifs_free_hash().
1086  */
1087 int
1088 cifs_alloc_hash(const char *name,
1089                 struct crypto_shash **shash, struct sdesc **sdesc)
1090 {
1091         int rc = 0;
1092         size_t size;
1093
1094         if (*sdesc != NULL)
1095                 return 0;
1096
1097         *shash = crypto_alloc_shash(name, 0, 0);
1098         if (IS_ERR(*shash)) {
1099                 cifs_dbg(VFS, "Could not allocate crypto %s\n", name);
1100                 rc = PTR_ERR(*shash);
1101                 *shash = NULL;
1102                 *sdesc = NULL;
1103                 return rc;
1104         }
1105
1106         size = sizeof(struct shash_desc) + crypto_shash_descsize(*shash);
1107         *sdesc = kmalloc(size, GFP_KERNEL);
1108         if (*sdesc == NULL) {
1109                 cifs_dbg(VFS, "no memory left to allocate crypto %s\n", name);
1110                 crypto_free_shash(*shash);
1111                 *shash = NULL;
1112                 return -ENOMEM;
1113         }
1114
1115         (*sdesc)->shash.tfm = *shash;
1116         return 0;
1117 }
1118
1119 /**
1120  * cifs_free_hash - free hash and hash context together
1121  * @shash: Where to find the pointer to the hash algo
1122  * @sdesc: Where to find the pointer to the hash descriptor
1123  *
1124  * Freeing a NULL hash or context is safe.
1125  */
1126 void
1127 cifs_free_hash(struct crypto_shash **shash, struct sdesc **sdesc)
1128 {
1129         kfree(*sdesc);
1130         *sdesc = NULL;
1131         if (*shash)
1132                 crypto_free_shash(*shash);
1133         *shash = NULL;
1134 }
1135
1136 /**
1137  * rqst_page_get_length - obtain the length and offset for a page in smb_rqst
1138  * @rqst: The request descriptor
1139  * @page: The index of the page to query
1140  * @len: Where to store the length for this page:
1141  * @offset: Where to store the offset for this page
1142  */
1143 void rqst_page_get_length(const struct smb_rqst *rqst, unsigned int page,
1144                           unsigned int *len, unsigned int *offset)
1145 {
1146         *len = rqst->rq_pagesz;
1147         *offset = (page == 0) ? rqst->rq_offset : 0;
1148
1149         if (rqst->rq_npages == 1 || page == rqst->rq_npages-1)
1150                 *len = rqst->rq_tailsz;
1151         else if (page == 0)
1152                 *len = rqst->rq_pagesz - rqst->rq_offset;
1153 }
1154
1155 void extract_unc_hostname(const char *unc, const char **h, size_t *len)
1156 {
1157         const char *end;
1158
1159         /* skip initial slashes */
1160         while (*unc && (*unc == '\\' || *unc == '/'))
1161                 unc++;
1162
1163         end = unc;
1164
1165         while (*end && !(*end == '\\' || *end == '/'))
1166                 end++;
1167
1168         *h = unc;
1169         *len = end - unc;
1170 }
1171
1172 /**
1173  * copy_path_name - copy src path to dst, possibly truncating
1174  * @dst: The destination buffer
1175  * @src: The source name
1176  *
1177  * returns number of bytes written (including trailing nul)
1178  */
1179 int copy_path_name(char *dst, const char *src)
1180 {
1181         int name_len;
1182
1183         /*
1184          * PATH_MAX includes nul, so if strlen(src) >= PATH_MAX it
1185          * will truncate and strlen(dst) will be PATH_MAX-1
1186          */
1187         name_len = strscpy(dst, src, PATH_MAX);
1188         if (WARN_ON_ONCE(name_len < 0))
1189                 name_len = PATH_MAX-1;
1190
1191         /* we count the trailing nul */
1192         name_len++;
1193         return name_len;
1194 }
1195
1196 struct super_cb_data {
1197         void *data;
1198         struct super_block *sb;
1199 };
1200
1201 static void tcp_super_cb(struct super_block *sb, void *arg)
1202 {
1203         struct super_cb_data *sd = arg;
1204         struct TCP_Server_Info *server = sd->data;
1205         struct cifs_sb_info *cifs_sb;
1206         struct cifs_tcon *tcon;
1207
1208         if (sd->sb)
1209                 return;
1210
1211         cifs_sb = CIFS_SB(sb);
1212         tcon = cifs_sb_master_tcon(cifs_sb);
1213         if (tcon->ses->server == server)
1214                 sd->sb = sb;
1215 }
1216
1217 static struct super_block *__cifs_get_super(void (*f)(struct super_block *, void *),
1218                                             void *data)
1219 {
1220         struct super_cb_data sd = {
1221                 .data = data,
1222                 .sb = NULL,
1223         };
1224         struct file_system_type **fs_type = (struct file_system_type *[]) {
1225                 &cifs_fs_type, &smb3_fs_type, NULL,
1226         };
1227
1228         for (; *fs_type; fs_type++) {
1229                 iterate_supers_type(*fs_type, f, &sd);
1230                 if (sd.sb) {
1231                         /*
1232                          * Grab an active reference in order to prevent automounts (DFS links)
1233                          * of expiring and then freeing up our cifs superblock pointer while
1234                          * we're doing failover.
1235                          */
1236                         cifs_sb_active(sd.sb);
1237                         return sd.sb;
1238                 }
1239         }
1240         return ERR_PTR(-EINVAL);
1241 }
1242
1243 static void __cifs_put_super(struct super_block *sb)
1244 {
1245         if (!IS_ERR_OR_NULL(sb))
1246                 cifs_sb_deactive(sb);
1247 }
1248
1249 struct super_block *cifs_get_tcp_super(struct TCP_Server_Info *server)
1250 {
1251         return __cifs_get_super(tcp_super_cb, server);
1252 }
1253
1254 void cifs_put_tcp_super(struct super_block *sb)
1255 {
1256         __cifs_put_super(sb);
1257 }
1258
1259 #ifdef CONFIG_CIFS_DFS_UPCALL
1260 int match_target_ip(struct TCP_Server_Info *server,
1261                     const char *share, size_t share_len,
1262                     bool *result)
1263 {
1264         int rc;
1265         char *target, *tip = NULL;
1266         struct sockaddr tipaddr;
1267
1268         *result = false;
1269
1270         target = kzalloc(share_len + 3, GFP_KERNEL);
1271         if (!target) {
1272                 rc = -ENOMEM;
1273                 goto out;
1274         }
1275
1276         scnprintf(target, share_len + 3, "\\\\%.*s", (int)share_len, share);
1277
1278         cifs_dbg(FYI, "%s: target name: %s\n", __func__, target + 2);
1279
1280         rc = dns_resolve_server_name_to_ip(target, &tip, NULL);
1281         if (rc < 0)
1282                 goto out;
1283
1284         cifs_dbg(FYI, "%s: target ip: %s\n", __func__, tip);
1285
1286         if (!cifs_convert_address(&tipaddr, tip, strlen(tip))) {
1287                 cifs_dbg(VFS, "%s: failed to convert target ip address\n",
1288                          __func__);
1289                 rc = -EINVAL;
1290                 goto out;
1291         }
1292
1293         *result = cifs_match_ipaddr((struct sockaddr *)&server->dstaddr,
1294                                     &tipaddr);
1295         cifs_dbg(FYI, "%s: ip addresses match: %u\n", __func__, *result);
1296         rc = 0;
1297
1298 out:
1299         kfree(target);
1300         kfree(tip);
1301
1302         return rc;
1303 }
1304
1305 int cifs_update_super_prepath(struct cifs_sb_info *cifs_sb, char *prefix)
1306 {
1307         kfree(cifs_sb->prepath);
1308
1309         if (prefix && *prefix) {
1310                 cifs_sb->prepath = cifs_sanitize_prepath(prefix, GFP_ATOMIC);
1311                 if (!cifs_sb->prepath)
1312                         return -ENOMEM;
1313
1314                 convert_delimiter(cifs_sb->prepath, CIFS_DIR_SEP(cifs_sb));
1315         } else
1316                 cifs_sb->prepath = NULL;
1317
1318         cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
1319         return 0;
1320 }
1321 #endif