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