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