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