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