cifs: do not search for channel if server is terminating
[linux.git] / fs / smb / client / smb2pdu.c
CommitLineData
929be906 1// SPDX-License-Identifier: LGPL-2.1
ec2e4523 2/*
ec2e4523 3 *
2b80d049 4 * Copyright (C) International Business Machines Corp., 2009, 2013
ec2e4523
PS
5 * Etersoft, 2012
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
8 *
9 * Contains the routines for constructing the SMB2 PDUs themselves
10 *
ec2e4523
PS
11 */
12
13 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
14 /* Note that there are handle based routines which must be */
15 /* treated slightly differently for reconnection purposes since we never */
16 /* want to reuse a stale file handle and only the caller knows the file info */
17
18#include <linux/fs.h>
19#include <linux/kernel.h>
20#include <linux/vfs.h>
09a4707e 21#include <linux/task_io_accounting_ops.h>
ec2e4523 22#include <linux/uaccess.h>
c6e970a0 23#include <linux/uuid.h>
33319141 24#include <linux/pagemap.h>
ec2e4523 25#include <linux/xattr.h>
ec2e4523
PS
26#include "cifsglob.h"
27#include "cifsacl.h"
28#include "cifsproto.h"
29#include "smb2proto.h"
30#include "cifs_unicode.h"
31#include "cifs_debug.h"
32#include "ntlmssp.h"
33#include "smb2status.h"
09a4707e 34#include "smb2glob.h"
d324f08d 35#include "cifspdu.h"
ceb1b0b9 36#include "cifs_spnego.h"
db223a59 37#include "smbdirect.h"
eccb4422 38#include "trace.h"
a3a53b76
PA
39#ifdef CONFIG_CIFS_DFS_UPCALL
40#include "dfs_cache.h"
41#endif
05b98fd2 42#include "cached_dir.h"
ec2e4523
PS
43
44/*
45 * The following table defines the expected "StructureSize" of SMB2 requests
46 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
47 *
48 * Note that commands are defined in smb2pdu.h in le16 but the array below is
49 * indexed by command in host byte order.
50 */
51static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
52 /* SMB2_NEGOTIATE */ 36,
53 /* SMB2_SESSION_SETUP */ 25,
54 /* SMB2_LOGOFF */ 4,
55 /* SMB2_TREE_CONNECT */ 9,
56 /* SMB2_TREE_DISCONNECT */ 4,
57 /* SMB2_CREATE */ 57,
58 /* SMB2_CLOSE */ 24,
59 /* SMB2_FLUSH */ 24,
60 /* SMB2_READ */ 49,
61 /* SMB2_WRITE */ 49,
62 /* SMB2_LOCK */ 48,
63 /* SMB2_IOCTL */ 57,
64 /* SMB2_CANCEL */ 4,
65 /* SMB2_ECHO */ 4,
66 /* SMB2_QUERY_DIRECTORY */ 33,
67 /* SMB2_CHANGE_NOTIFY */ 32,
68 /* SMB2_QUERY_INFO */ 41,
69 /* SMB2_SET_INFO */ 33,
70 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
71};
72
730928c8 73int smb3_encryption_required(const struct cifs_tcon *tcon)
7fb8986e 74{
edb16135 75 if (!tcon || !tcon->ses)
ae6f8dd4 76 return 0;
7fb8986e
PS
77 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
78 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
79 return 1;
ae6f8dd4
PS
80 if (tcon->seal &&
81 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
82 return 1;
7fb8986e
PS
83 return 0;
84}
ec2e4523
PS
85
86static void
0d35e382 87smb2_hdr_assemble(struct smb2_hdr *shdr, __le16 smb2_cmd,
352d96f3
AA
88 const struct cifs_tcon *tcon,
89 struct TCP_Server_Info *server)
ec2e4523 90{
09ee7a3b 91 struct smb3_hdr_req *smb3_hdr;
2c75426c 92
31473fc4
PS
93 shdr->ProtocolId = SMB2_PROTO_NUMBER;
94 shdr->StructureSize = cpu_to_le16(64);
95 shdr->Command = smb2_cmd;
05d0f8f5 96
352d96f3 97 if (server) {
05d0f8f5
SF
98 /* After reconnect SMB3 must set ChannelSequence on subsequent reqs */
99 if (server->dialect >= SMB30_PROT_ID) {
100 smb3_hdr = (struct smb3_hdr_req *)shdr;
101 /*
102 * if primary channel is not set yet, use default
103 * channel for chan sequence num
104 */
105 if (SERVER_IS_CHAN(server))
106 smb3_hdr->ChannelSequence =
107 cpu_to_le16(server->primary_server->channel_sequence_num);
108 else
109 smb3_hdr->ChannelSequence =
110 cpu_to_le16(server->channel_sequence_num);
111 }
7d414f39 112 spin_lock(&server->req_lock);
69dc4b18 113 /* Request up to 10 credits but don't go over the limit. */
141891f4 114 if (server->credits >= server->max_credits)
31473fc4 115 shdr->CreditRequest = cpu_to_le16(0);
7d414f39 116 else
31473fc4 117 shdr->CreditRequest = cpu_to_le16(
141891f4 118 min_t(int, server->max_credits -
69dc4b18 119 server->credits, 10));
7d414f39
RL
120 spin_unlock(&server->req_lock);
121 } else {
31473fc4 122 shdr->CreditRequest = cpu_to_le16(2);
7d414f39 123 }
0d35e382 124 shdr->Id.SyncId.ProcessId = cpu_to_le32((__u16)current->tgid);
ec2e4523
PS
125
126 if (!tcon)
127 goto out;
128
2b80d049
SF
129 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
130 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
352d96f3 131 if (server && (server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
31473fc4 132 shdr->CreditCharge = cpu_to_le16(1);
2b80d049
SF
133 /* else CreditCharge MBZ */
134
0d35e382 135 shdr->Id.SyncId.TreeId = cpu_to_le32(tcon->tid);
ec2e4523
PS
136 /* Uid is not converted */
137 if (tcon->ses)
0d35e382 138 shdr->SessionId = cpu_to_le64(tcon->ses->Suid);
f87ab88b
SF
139
140 /*
141 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
142 * to pass the path on the Open SMB prefixed by \\server\share.
143 * Not sure when we would need to do the augmented path (if ever) and
144 * setting this flag breaks the SMB2 open operation since it is
145 * illegal to send an empty path name (without \\server\share prefix)
146 * when the DFS flag is set in the SMB open header. We could
147 * consider setting the flag on all operations other than open
148 * but it is safer to net set it for now.
149 */
150/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
31473fc4 151 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
f87ab88b 152
352d96f3 153 if (server && server->sign && !smb3_encryption_required(tcon))
31473fc4 154 shdr->Flags |= SMB2_FLAGS_SIGNED;
ec2e4523 155out:
ec2e4523
PS
156 return;
157}
158
f591062b
SP
159/* helper function for code reuse */
160static int
161cifs_chan_skip_or_disable(struct cifs_ses *ses,
162 struct TCP_Server_Info *server,
163 bool from_reconnect)
164{
165 struct TCP_Server_Info *pserver;
166 unsigned int chan_index;
167
168 if (SERVER_IS_CHAN(server)) {
169 cifs_dbg(VFS,
170 "server %s does not support multichannel anymore. Skip secondary channel\n",
171 ses->server->hostname);
172
173 spin_lock(&ses->chan_lock);
174 chan_index = cifs_ses_get_chan_index(ses, server);
175 if (chan_index == CIFS_INVAL_CHAN_INDEX) {
176 spin_unlock(&ses->chan_lock);
177 goto skip_terminate;
178 }
179
180 ses->chans[chan_index].server = NULL;
88675b22 181 server->terminate = true;
f591062b
SP
182 spin_unlock(&ses->chan_lock);
183
184 /*
185 * the above reference of server by channel
186 * needs to be dropped without holding chan_lock
187 * as cifs_put_tcp_session takes a higher lock
188 * i.e. cifs_tcp_ses_lock
189 */
190 cifs_put_tcp_session(server, from_reconnect);
191
f591062b
SP
192 cifs_signal_cifsd_for_reconnect(server, false);
193
194 /* mark primary server as needing reconnect */
195 pserver = server->primary_server;
196 cifs_signal_cifsd_for_reconnect(pserver, false);
197skip_terminate:
f591062b
SP
198 return -EHOSTDOWN;
199 }
200
201 cifs_server_dbg(VFS,
202 "server does not support multichannel anymore. Disable all other channels\n");
203 cifs_disable_secondary_channels(ses);
204
205
206 return 0;
207}
208
ec2e4523 209static int
352d96f3 210smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
04909192 211 struct TCP_Server_Info *server, bool from_reconnect)
ec2e4523 212{
350f4a56 213 int rc = 0;
c24bb1a8 214 struct nls_table *nls_codepage = NULL;
aa24d1e9 215 struct cifs_ses *ses;
705fc522 216 int xid;
aa24d1e9
PS
217
218 /*
219 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
220 * check for tcp and smb session status done differently
221 * for those three - in the calling routine.
222 */
223 if (tcon == NULL)
7ffbe655 224 return 0;
aa24d1e9 225
c88f7dcd
PA
226 /*
227 * Need to also skip SMB2_IOCTL because it is used for checking nested dfs links in
228 * cifs_tree_connect().
229 */
230 if (smb2_command == SMB2_TREE_CONNECT || smb2_command == SMB2_IOCTL)
7ffbe655 231 return 0;
aa24d1e9 232
d7d7a66a 233 spin_lock(&tcon->tc_lock);
fdf59eb5 234 if (tcon->status == TID_EXITING) {
aa24d1e9 235 /*
491eafce 236 * only tree disconnect allowed when disconnecting ...
aa24d1e9 237 */
491eafce 238 if (smb2_command != SMB2_TREE_DISCONNECT) {
d7d7a66a 239 spin_unlock(&tcon->tc_lock);
f96637be
JP
240 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
241 smb2_command);
aa24d1e9
PS
242 return -ENODEV;
243 }
244 }
d7d7a66a 245 spin_unlock(&tcon->tc_lock);
5bff9f74
PA
246
247 ses = tcon->ses;
248 if (!ses)
249 return -EIO;
250 spin_lock(&ses->ses_lock);
251 if (ses->ses_status == SES_EXITING) {
252 spin_unlock(&ses->ses_lock);
253 return -EIO;
254 }
255 spin_unlock(&ses->ses_lock);
256 if (!ses->server || !server)
aa24d1e9
PS
257 return -EIO;
258
1bcd548d
PA
259 spin_lock(&server->srv_lock);
260 if (server->tcpStatus == CifsNeedReconnect) {
261 /*
262 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
263 * here since they are implicitly done when session drops.
264 */
265 switch (smb2_command) {
266 /*
267 * BB Should we keep oplock break and add flush to exceptions?
268 */
269 case SMB2_TREE_DISCONNECT:
270 case SMB2_CANCEL:
271 case SMB2_CLOSE:
272 case SMB2_OPLOCK_BREAK:
273 spin_unlock(&server->srv_lock);
274 return -EAGAIN;
275 }
276 }
ee1d2179
SP
277
278 /* if server is marked for termination, cifsd will cleanup */
279 if (server->terminate) {
280 spin_unlock(&server->srv_lock);
281 return -EHOSTDOWN;
282 }
1bcd548d
PA
283 spin_unlock(&server->srv_lock);
284
bc962159 285again:
1bcd548d 286 rc = cifs_wait_for_server_reconnect(server, tcon->retry);
3c0070f5
PA
287 if (rc)
288 return rc;
a3a53b76 289
d1a931ce
SP
290 spin_lock(&ses->chan_lock);
291 if (!cifs_chan_needs_reconnect(ses, server) && !tcon->need_reconnect) {
292 spin_unlock(&ses->chan_lock);
7ffbe655 293 return 0;
d1a931ce 294 }
88b024f5 295 spin_unlock(&ses->chan_lock);
d1a931ce
SP
296 cifs_dbg(FYI, "sess reconnect mask: 0x%lx, tcon reconnect: %d",
297 tcon->ses->chans_need_reconnect,
298 tcon->need_reconnect);
aa24d1e9 299
bc962159 300 mutex_lock(&ses->session_mutex);
ee1d2179
SP
301 /*
302 * if this is called by delayed work, and the channel has been disabled
303 * in parallel, the delayed work can continue to execute in parallel
304 * there's a chance that this channel may not exist anymore
305 */
306 spin_lock(&server->srv_lock);
307 if (server->tcpStatus == CifsExiting) {
308 spin_unlock(&server->srv_lock);
309 mutex_unlock(&ses->session_mutex);
310 rc = -EHOSTDOWN;
311 goto out;
312 }
313
76e75270
SC
314 /*
315 * Recheck after acquire mutex. If another thread is negotiating
316 * and the server never sends an answer the socket will be closed
317 * and tcpStatus set to reconnect.
318 */
319 if (server->tcpStatus == CifsNeedReconnect) {
d7d7a66a 320 spin_unlock(&server->srv_lock);
bc962159
SP
321 mutex_unlock(&ses->session_mutex);
322
323 if (tcon->retry)
324 goto again;
325
76e75270 326 rc = -EHOSTDOWN;
76e75270
SC
327 goto out;
328 }
d7d7a66a 329 spin_unlock(&server->srv_lock);
76e75270 330
a43f95fd 331 nls_codepage = ses->local_nls;
c24bb1a8 332
d1a931ce
SP
333 /*
334 * need to prevent multiple threads trying to simultaneously
335 * reconnect the same SMB session
336 */
bc962159 337 spin_lock(&ses->ses_lock);
d1a931ce 338 spin_lock(&ses->chan_lock);
bc962159
SP
339 if (!cifs_chan_needs_reconnect(ses, server) &&
340 ses->ses_status == SES_GOOD) {
d1a931ce 341 spin_unlock(&ses->chan_lock);
bc962159 342 spin_unlock(&ses->ses_lock);
f486ef8e 343 /* this means that we only need to tree connect */
d1a931ce
SP
344 if (tcon->need_reconnect)
345 goto skip_sess_setup;
346
bc962159 347 mutex_unlock(&ses->session_mutex);
d1a931ce
SP
348 goto out;
349 }
350 spin_unlock(&ses->chan_lock);
bc962159 351 spin_unlock(&ses->ses_lock);
d1a931ce 352
f486ef8e 353 rc = cifs_negotiate_protocol(0, ses, server);
d1a931ce 354 if (!rc) {
ee1d2179
SP
355 /*
356 * if server stopped supporting multichannel
357 * and the first channel reconnected, disable all the others.
358 */
359 if (ses->chan_count > 1 &&
360 !(server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
f591062b
SP
361 rc = cifs_chan_skip_or_disable(ses, server,
362 from_reconnect);
363 if (rc) {
ee1d2179 364 mutex_unlock(&ses->session_mutex);
ee1d2179 365 goto out;
ee1d2179
SP
366 }
367 }
368
f486ef8e 369 rc = cifs_setup_session(0, ses, server, nls_codepage);
b0dd940e 370 if ((rc == -EACCES) && !tcon->retry) {
f486ef8e 371 mutex_unlock(&ses->session_mutex);
73f9bfbe 372 rc = -EHOSTDOWN;
b0dd940e 373 goto failed;
8ea21823
SP
374 } else if (rc) {
375 mutex_unlock(&ses->session_mutex);
376 goto out;
b0dd940e 377 }
3663c904 378 } else {
73f9bfbe 379 mutex_unlock(&ses->session_mutex);
aa24d1e9
PS
380 goto out;
381 }
382
d1a931ce 383skip_sess_setup:
3663c904
SP
384 if (!tcon->need_reconnect) {
385 mutex_unlock(&ses->session_mutex);
386 goto out;
387 }
aa24d1e9 388 cifs_mark_open_files_invalid(tcon);
96a988ff
PS
389 if (tcon->use_persistent)
390 tcon->need_reopen_files = true;
52ace1ef 391
565674d6 392 rc = cifs_tree_connect(0, tcon, nls_codepage);
52ace1ef 393
f96637be 394 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
c318e6c2
SF
395 if (rc) {
396 /* If sess reconnected but tcon didn't, something strange ... */
705fc522 397 mutex_unlock(&ses->session_mutex);
bc962159 398 cifs_dbg(VFS, "reconnect tcon failed rc = %d\n", rc);
aa24d1e9 399 goto out;
c318e6c2 400 }
96a988ff 401
ee36a3b3
SP
402 spin_lock(&ses->ses_lock);
403 if (ses->flags & CIFS_SES_FLAG_SCALE_CHANNELS) {
404 spin_unlock(&ses->ses_lock);
405 mutex_unlock(&ses->session_mutex);
406 goto skip_add_channels;
407 }
408 ses->flags |= CIFS_SES_FLAG_SCALE_CHANNELS;
409 spin_unlock(&ses->ses_lock);
410
705fc522
SP
411 if (!rc &&
412 (server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
413 mutex_unlock(&ses->session_mutex);
414
415 /*
416 * query server network interfaces, in case they change
417 */
418 xid = get_xid();
419 rc = SMB3_request_interfaces(xid, tcon, false);
420 free_xid(xid);
421
e77e15fa 422 if (rc == -EOPNOTSUPP && ses->chan_count > 1) {
f591062b
SP
423 /*
424 * some servers like Azure SMB server do not advertise
425 * that multichannel has been disabled with server
426 * capabilities, rather return STATUS_NOT_IMPLEMENTED.
427 * treat this as server not supporting multichannel
428 */
429
430 rc = cifs_chan_skip_or_disable(ses, server,
431 from_reconnect);
432 goto skip_add_channels;
433 } else if (rc)
705fc522
SP
434 cifs_dbg(FYI, "%s: failed to query server interfaces: %d\n",
435 __func__, rc);
436
437 if (ses->chan_max > ses->chan_count &&
f591062b 438 ses->iface_count &&
705fc522 439 !SERVER_IS_CHAN(server)) {
ee36a3b3 440 if (ses->chan_count == 1) {
705fc522 441 cifs_server_dbg(VFS, "supports multichannel now\n");
ee36a3b3
SP
442 queue_delayed_work(cifsiod_wq, &tcon->query_interfaces,
443 (SMB_INTERFACE_POLL_INTERVAL * HZ));
444 }
705fc522
SP
445
446 cifs_try_adding_channels(ses);
447 }
448 } else {
449 mutex_unlock(&ses->session_mutex);
450 }
ee36a3b3 451
f591062b 452skip_add_channels:
ee36a3b3
SP
453 spin_lock(&ses->ses_lock);
454 ses->flags &= ~CIFS_SES_FLAG_SCALE_CHANNELS;
455 spin_unlock(&ses->ses_lock);
705fc522 456
96a988ff 457 if (smb2_command != SMB2_INTERNAL_CMD)
f30bbc38 458 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
96a988ff 459
aa24d1e9 460 atomic_inc(&tconInfoReconnectCount);
aa24d1e9
PS
461out:
462 /*
463 * Check if handle based operation so we know whether we can continue
464 * or not without returning to caller to reset file handle.
465 */
466 /*
467 * BB Is flush done by server on drop of tcp session? Should we special
468 * case it and skip above?
469 */
470 switch (smb2_command) {
471 case SMB2_FLUSH:
472 case SMB2_READ:
473 case SMB2_WRITE:
474 case SMB2_LOCK:
aa24d1e9
PS
475 case SMB2_QUERY_DIRECTORY:
476 case SMB2_CHANGE_NOTIFY:
477 case SMB2_QUERY_INFO:
478 case SMB2_SET_INFO:
4772c795 479 rc = -EAGAIN;
aa24d1e9 480 }
b0dd940e 481failed:
ec2e4523
PS
482 return rc;
483}
484
cb200bd6 485static void
352d96f3
AA
486fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon,
487 struct TCP_Server_Info *server,
488 void *buf,
cb200bd6
PS
489 unsigned int *total_len)
490{
0f46608a 491 struct smb2_pdu *spdu = buf;
cb200bd6
PS
492 /* lookup word count ie StructureSize from table */
493 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
494
495 /*
496 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
497 * largest operations (Create)
498 */
499 memset(buf, 0, 256);
500
0d35e382 501 smb2_hdr_assemble(&spdu->hdr, smb2_command, tcon, server);
cb200bd6
PS
502 spdu->StructureSize2 = cpu_to_le16(parmsize);
503
0d35e382 504 *total_len = parmsize + sizeof(struct smb2_hdr);
cb200bd6
PS
505}
506
ec2e4523
PS
507/*
508 * Allocate and return pointer to an SMB request hdr, and set basic
509 * SMB information in the SMB header. If the return code is zero, this
305428ac 510 * function must have filled in request_buf pointer.
ec2e4523 511 */
84a1f5b1 512static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
352d96f3
AA
513 struct TCP_Server_Info *server,
514 void **request_buf, unsigned int *total_len)
ec2e4523 515{
ec2e4523 516 /* BB eventually switch this to SMB2 specific small buf size */
33eae65c
PA
517 switch (smb2_command) {
518 case SMB2_SET_INFO:
519 case SMB2_QUERY_INFO:
f46ecbd9 520 *request_buf = cifs_buf_get();
33eae65c
PA
521 break;
522 default:
f46ecbd9 523 *request_buf = cifs_small_buf_get();
33eae65c
PA
524 break;
525 }
ec2e4523
PS
526 if (*request_buf == NULL) {
527 /* BB should we add a retry in here if not a writepage? */
528 return -ENOMEM;
529 }
530
352d96f3 531 fill_small_buf(smb2_command, tcon, server,
0d35e382 532 (struct smb2_hdr *)(*request_buf),
305428ac 533 total_len);
ec2e4523
PS
534
535 if (tcon != NULL) {
ec2e4523
PS
536 uint16_t com_code = le16_to_cpu(smb2_command);
537 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
ec2e4523
PS
538 cifs_stats_inc(&tcon->num_smbs_sent);
539 }
540
84a1f5b1
PAS
541 return 0;
542}
543
544static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
352d96f3 545 struct TCP_Server_Info *server,
84a1f5b1
PAS
546 void **request_buf, unsigned int *total_len)
547{
548 int rc;
549
04909192 550 rc = smb2_reconnect(smb2_command, tcon, server, false);
84a1f5b1
PAS
551 if (rc)
552 return rc;
553
352d96f3 554 return __smb2_plain_req_init(smb2_command, tcon, server, request_buf,
84a1f5b1
PAS
555 total_len);
556}
557
558static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
352d96f3 559 struct TCP_Server_Info *server,
84a1f5b1
PAS
560 void **request_buf, unsigned int *total_len)
561{
562 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
563 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
352d96f3
AA
564 return __smb2_plain_req_init(SMB2_IOCTL, tcon, server,
565 request_buf, total_len);
84a1f5b1 566 }
352d96f3
AA
567 return smb2_plain_req_init(SMB2_IOCTL, tcon, server,
568 request_buf, total_len);
ec2e4523
PS
569}
570
d7bef4c4 571/* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
ebb3a9d4
SF
572
573static void
574build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
575{
576 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
577 pneg_ctxt->DataLength = cpu_to_le16(38);
578 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
fc0b3844
RS
579 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
580 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
ebb3a9d4
SF
581 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
582}
583
26ea888f
SF
584static void
585build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
586{
587 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
588 pneg_ctxt->DataLength =
589 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
590 - sizeof(struct smb2_neg_context));
591 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
592 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
593 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
594 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
595}
596
53d31a3f
SF
597static unsigned int
598build_signing_ctxt(struct smb2_signing_capabilities *pneg_ctxt)
599{
600 unsigned int ctxt_len = sizeof(struct smb2_signing_capabilities);
601 unsigned short num_algs = 1; /* number of signing algorithms sent */
602
603 pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;
604 /*
605 * Context Data length must be rounded to multiple of 8 for some servers
606 */
d7173623
EM
607 pneg_ctxt->DataLength = cpu_to_le16(ALIGN(sizeof(struct smb2_signing_capabilities) -
608 sizeof(struct smb2_neg_context) +
609 (num_algs * sizeof(u16)), 8));
53d31a3f
SF
610 pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(num_algs);
611 pneg_ctxt->SigningAlgorithms[0] = cpu_to_le16(SIGNING_ALG_AES_CMAC);
612
d7173623
EM
613 ctxt_len += sizeof(__le16) * num_algs;
614 ctxt_len = ALIGN(ctxt_len, 8);
53d31a3f
SF
615 return ctxt_len;
616 /* TBD add SIGNING_ALG_AES_GMAC and/or SIGNING_ALG_HMAC_SHA256 */
617}
618
ebb3a9d4
SF
619static void
620build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
621{
622 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
fbfd0b46
SF
623 if (require_gcm_256) {
624 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + 1 cipher */
625 pneg_ctxt->CipherCount = cpu_to_le16(1);
626 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES256_GCM;
29e27923
SF
627 } else if (enable_gcm_256) {
628 pneg_ctxt->DataLength = cpu_to_le16(8); /* Cipher Count + 3 ciphers */
629 pneg_ctxt->CipherCount = cpu_to_le16(3);
630 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
631 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES256_GCM;
632 pneg_ctxt->Ciphers[2] = SMB2_ENCRYPTION_AES128_CCM;
fbfd0b46
SF
633 } else {
634 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + 2 ciphers */
635 pneg_ctxt->CipherCount = cpu_to_le16(2);
636 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
637 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
638 }
ebb3a9d4
SF
639}
640
96d3cca1
SF
641static unsigned int
642build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
643{
644 struct nls_table *cp = load_nls_default();
645
646 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
647
648 /* copy up to max of first 100 bytes of server name to NetName field */
df58fae7 649 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
96d3cca1 650 /* context size is DataLength + minimal smb2_neg_context */
d7173623 651 return ALIGN(le16_to_cpu(pneg_ctxt->DataLength) + sizeof(struct smb2_neg_context), 8);
96d3cca1
SF
652}
653
fcef0db6
SF
654static void
655build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
656{
657 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
658 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
0d481325
SF
659 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
660 pneg_ctxt->Name[0] = 0x93;
661 pneg_ctxt->Name[1] = 0xAD;
662 pneg_ctxt->Name[2] = 0x25;
663 pneg_ctxt->Name[3] = 0x50;
664 pneg_ctxt->Name[4] = 0x9C;
665 pneg_ctxt->Name[5] = 0xB4;
666 pneg_ctxt->Name[6] = 0x11;
667 pneg_ctxt->Name[7] = 0xE7;
668 pneg_ctxt->Name[8] = 0xB4;
669 pneg_ctxt->Name[9] = 0x23;
670 pneg_ctxt->Name[10] = 0x83;
671 pneg_ctxt->Name[11] = 0xDE;
672 pneg_ctxt->Name[12] = 0x96;
673 pneg_ctxt->Name[13] = 0x8B;
674 pneg_ctxt->Name[14] = 0xCD;
675 pneg_ctxt->Name[15] = 0x7C;
fcef0db6
SF
676}
677
ebb3a9d4 678static void
13cacea7 679assemble_neg_contexts(struct smb2_negotiate_req *req,
9fe5ff1c 680 struct TCP_Server_Info *server, unsigned int *total_len)
ebb3a9d4 681{
53d31a3f 682 unsigned int ctxt_len, neg_context_count;
775e44d6
PA
683 struct TCP_Server_Info *pserver;
684 char *pneg_ctxt;
685 char *hostname;
ebb3a9d4 686
d5c7076b
SF
687 if (*total_len > 200) {
688 /* In case length corrupted don't want to overrun smb buffer */
afe6f653 689 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
d5c7076b
SF
690 return;
691 }
692
693 /*
694 * round up total_len of fixed part of SMB3 negotiate request to 8
695 * byte boundary before adding negotiate contexts
696 */
d7173623 697 *total_len = ALIGN(*total_len, 8);
d5c7076b
SF
698
699 pneg_ctxt = (*total_len) + (char *)req;
700 req->NegotiateContextOffset = cpu_to_le32(*total_len);
701
ebb3a9d4 702 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
d7173623 703 ctxt_len = ALIGN(sizeof(struct smb2_preauth_neg_context), 8);
fcef0db6
SF
704 *total_len += ctxt_len;
705 pneg_ctxt += ctxt_len;
13cacea7 706
ebb3a9d4 707 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
d7173623 708 ctxt_len = ALIGN(sizeof(struct smb2_encryption_neg_context), 8);
fcef0db6
SF
709 *total_len += ctxt_len;
710 pneg_ctxt += ctxt_len;
711
9de74996
SP
712 /*
713 * secondary channels don't have the hostname field populated
714 * use the hostname field in the primary channel instead
715 */
b3773b19 716 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
775e44d6
PA
717 cifs_server_lock(pserver);
718 hostname = pserver->hostname;
9de74996 719 if (hostname && (hostname[0] != 0)) {
73130a7b 720 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
9de74996 721 hostname);
73130a7b
SF
722 *total_len += ctxt_len;
723 pneg_ctxt += ctxt_len;
73130a7b 724 neg_context_count = 3;
32f31918
SF
725 } else
726 neg_context_count = 2;
775e44d6 727 cifs_server_unlock(pserver);
32f31918
SF
728
729 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
730 *total_len += sizeof(struct smb2_posix_neg_context);
731 pneg_ctxt += sizeof(struct smb2_posix_neg_context);
732 neg_context_count++;
53d31a3f 733
9fe5ff1c
SF
734 if (server->compress_algorithm) {
735 build_compression_ctxt((struct smb2_compression_capabilities_context *)
26ea888f 736 pneg_ctxt);
d7173623 737 ctxt_len = ALIGN(sizeof(struct smb2_compression_capabilities_context), 8);
9fe5ff1c
SF
738 *total_len += ctxt_len;
739 pneg_ctxt += ctxt_len;
53d31a3f
SF
740 neg_context_count++;
741 }
96d3cca1 742
53d31a3f
SF
743 if (enable_negotiate_signing) {
744 ctxt_len = build_signing_ctxt((struct smb2_signing_capabilities *)
745 pneg_ctxt);
746 *total_len += ctxt_len;
747 pneg_ctxt += ctxt_len;
748 neg_context_count++;
749 }
750
751 /* check for and add transport_capabilities and signing capabilities */
752 req->NegotiateContextCount = cpu_to_le16(neg_context_count);
96d3cca1 753
ebb3a9d4 754}
5100d8a3 755
5105a7ff 756/* If invalid preauth context warn but use what we requested, SHA-512 */
5100d8a3
SF
757static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
758{
759 unsigned int len = le16_to_cpu(ctxt->DataLength);
760
5105a7ff
DD
761 /*
762 * Caller checked that DataLength remains within SMB boundary. We still
763 * need to confirm that one HashAlgorithms member is accounted for.
764 */
5100d8a3 765 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
a0a3036b 766 pr_warn_once("server sent bad preauth context\n");
5100d8a3 767 return;
7955f105
SF
768 } else if (len < MIN_PREAUTH_CTXT_DATA_LEN + le16_to_cpu(ctxt->SaltLength)) {
769 pr_warn_once("server sent invalid SaltLength\n");
770 return;
5100d8a3
SF
771 }
772 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
a0a3036b 773 pr_warn_once("Invalid SMB3 hash algorithm count\n");
5100d8a3 774 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
a0a3036b 775 pr_warn_once("unknown SMB3 hash algorithm\n");
5100d8a3
SF
776}
777
26ea888f
SF
778static void decode_compress_ctx(struct TCP_Server_Info *server,
779 struct smb2_compression_capabilities_context *ctxt)
780{
781 unsigned int len = le16_to_cpu(ctxt->DataLength);
782
5105a7ff
DD
783 /*
784 * Caller checked that DataLength remains within SMB boundary. We still
785 * need to confirm that one CompressionAlgorithms member is accounted
786 * for.
787 */
26ea888f 788 if (len < 10) {
a0a3036b 789 pr_warn_once("server sent bad compression cntxt\n");
26ea888f
SF
790 return;
791 }
792 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
a0a3036b 793 pr_warn_once("Invalid SMB3 compress algorithm count\n");
26ea888f
SF
794 return;
795 }
796 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
a0a3036b 797 pr_warn_once("unknown compression algorithm\n");
26ea888f
SF
798 return;
799 }
800 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
801}
802
5100d8a3
SF
803static int decode_encrypt_ctx(struct TCP_Server_Info *server,
804 struct smb2_encryption_neg_context *ctxt)
805{
806 unsigned int len = le16_to_cpu(ctxt->DataLength);
807
808 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
5105a7ff
DD
809 /*
810 * Caller checked that DataLength remains within SMB boundary. We still
811 * need to confirm that one Cipher flexible array member is accounted
812 * for.
813 */
5100d8a3 814 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
a0a3036b 815 pr_warn_once("server sent bad crypto ctxt len\n");
5100d8a3
SF
816 return -EINVAL;
817 }
818
819 if (le16_to_cpu(ctxt->CipherCount) != 1) {
a0a3036b 820 pr_warn_once("Invalid SMB3.11 cipher count\n");
5100d8a3
SF
821 return -EINVAL;
822 }
823 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
511ac89e
SF
824 if (require_gcm_256) {
825 if (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM) {
826 cifs_dbg(VFS, "Server does not support requested encryption type (AES256 GCM)\n");
827 return -EOPNOTSUPP;
828 }
829 } else if (ctxt->Ciphers[0] == 0) {
acf96fef
SF
830 /*
831 * e.g. if server only supported AES256_CCM (very unlikely)
832 * or server supported no encryption types or had all disabled.
833 * Since GLOBAL_CAP_ENCRYPTION will be not set, in the case
834 * in which mount requested encryption ("seal") checks later
835 * on during tree connection will return proper rc, but if
836 * seal not requested by client, since server is allowed to
837 * return 0 to indicate no supported cipher, we can't fail here
838 */
839 server->cipher_type = 0;
840 server->capabilities &= ~SMB2_GLOBAL_CAP_ENCRYPTION;
841 pr_warn_once("Server does not support requested encryption types\n");
842 return 0;
511ac89e
SF
843 } else if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
844 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM) &&
845 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM)) {
846 /* server returned a cipher we didn't ask for */
a0a3036b 847 pr_warn_once("Invalid SMB3.11 cipher returned\n");
5100d8a3
SF
848 return -EINVAL;
849 }
850 server->cipher_type = ctxt->Ciphers[0];
23657ad7 851 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
5100d8a3
SF
852 return 0;
853}
854
53d31a3f
SF
855static void decode_signing_ctx(struct TCP_Server_Info *server,
856 struct smb2_signing_capabilities *pctxt)
857{
858 unsigned int len = le16_to_cpu(pctxt->DataLength);
859
5105a7ff
DD
860 /*
861 * Caller checked that DataLength remains within SMB boundary. We still
862 * need to confirm that one SigningAlgorithms flexible array member is
863 * accounted for.
864 */
53d31a3f
SF
865 if ((len < 4) || (len > 16)) {
866 pr_warn_once("server sent bad signing negcontext\n");
867 return;
868 }
869 if (le16_to_cpu(pctxt->SigningAlgorithmCount) != 1) {
870 pr_warn_once("Invalid signing algorithm count\n");
871 return;
872 }
873 if (le16_to_cpu(pctxt->SigningAlgorithms[0]) > 2) {
874 pr_warn_once("unknown signing algorithm\n");
875 return;
876 }
877
878 server->signing_negotiated = true;
879 server->signing_algorithm = le16_to_cpu(pctxt->SigningAlgorithms[0]);
880 cifs_dbg(FYI, "signing algorithm %d chosen\n",
881 server->signing_algorithm);
882}
883
884
5100d8a3 885static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
977b6170
RS
886 struct TCP_Server_Info *server,
887 unsigned int len_of_smb)
5100d8a3
SF
888{
889 struct smb2_neg_context *pctx;
890 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
891 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
5100d8a3
SF
892 unsigned int len_of_ctxts, i;
893 int rc = 0;
894
895 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
896 if (len_of_smb <= offset) {
afe6f653 897 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
5100d8a3
SF
898 return -EINVAL;
899 }
900
901 len_of_ctxts = len_of_smb - offset;
902
903 for (i = 0; i < ctxt_cnt; i++) {
904 int clen;
905 /* check that offset is not beyond end of SMB */
5100d8a3
SF
906 if (len_of_ctxts < sizeof(struct smb2_neg_context))
907 break;
908
1fc6ad2f 909 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
5105a7ff
DD
910 clen = sizeof(struct smb2_neg_context)
911 + le16_to_cpu(pctx->DataLength);
912 /*
913 * 2.2.4 SMB2 NEGOTIATE Response
914 * Subsequent negotiate contexts MUST appear at the first 8-byte
915 * aligned offset following the previous negotiate context.
916 */
917 if (i + 1 != ctxt_cnt)
918 clen = ALIGN(clen, 8);
5100d8a3
SF
919 if (clen > len_of_ctxts)
920 break;
921
922 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
923 decode_preauth_context(
924 (struct smb2_preauth_neg_context *)pctx);
925 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
926 rc = decode_encrypt_ctx(server,
927 (struct smb2_encryption_neg_context *)pctx);
26ea888f
SF
928 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
929 decode_compress_ctx(server,
930 (struct smb2_compression_capabilities_context *)pctx);
fcef0db6
SF
931 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
932 server->posix_ext_supported = true;
53d31a3f
SF
933 else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES)
934 decode_signing_ctx(server,
935 (struct smb2_signing_capabilities *)pctx);
5100d8a3 936 else
afe6f653 937 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
5100d8a3 938 le16_to_cpu(pctx->ContextType));
5100d8a3
SF
939 if (rc)
940 break;
5105a7ff
DD
941
942 offset += clen;
5100d8a3
SF
943 len_of_ctxts -= clen;
944 }
945 return rc;
946}
947
ce558b0e
SF
948static struct create_posix *
949create_posix_buf(umode_t mode)
950{
951 struct create_posix *buf;
952
953 buf = kzalloc(sizeof(struct create_posix),
954 GFP_KERNEL);
955 if (!buf)
956 return NULL;
957
958 buf->ccontext.DataOffset =
959 cpu_to_le16(offsetof(struct create_posix, Mode));
960 buf->ccontext.DataLength = cpu_to_le32(4);
961 buf->ccontext.NameOffset =
962 cpu_to_le16(offsetof(struct create_posix, Name));
963 buf->ccontext.NameLength = cpu_to_le16(16);
964
965 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
966 buf->Name[0] = 0x93;
967 buf->Name[1] = 0xAD;
968 buf->Name[2] = 0x25;
969 buf->Name[3] = 0x50;
970 buf->Name[4] = 0x9C;
971 buf->Name[5] = 0xB4;
972 buf->Name[6] = 0x11;
973 buf->Name[7] = 0xE7;
974 buf->Name[8] = 0xB4;
975 buf->Name[9] = 0x23;
976 buf->Name[10] = 0x83;
977 buf->Name[11] = 0xDE;
978 buf->Name[12] = 0x96;
979 buf->Name[13] = 0x8B;
980 buf->Name[14] = 0xCD;
981 buf->Name[15] = 0x7C;
982 buf->Mode = cpu_to_le32(mode);
a0a3036b 983 cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
ce558b0e
SF
984 return buf;
985}
986
987static int
988add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
989{
ce558b0e
SF
990 unsigned int num = *num_iovec;
991
992 iov[num].iov_base = create_posix_buf(mode);
d0959b08 993 if (mode == ACL_NO_MODE)
c8ebf077 994 cifs_dbg(FYI, "%s: no mode\n", __func__);
ce558b0e
SF
995 if (iov[num].iov_base == NULL)
996 return -ENOMEM;
997 iov[num].iov_len = sizeof(struct create_posix);
ce558b0e
SF
998 *num_iovec = num + 1;
999 return 0;
1000}
1001
ebb3a9d4 1002
ec2e4523
PS
1003/*
1004 *
1005 * SMB2 Worker functions follow:
1006 *
1007 * The general structure of the worker functions is:
1008 * 1) Call smb2_init (assembles SMB2 header)
1009 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
1010 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
1011 * 4) Decode SMB2 command specific fields in the fixed length area
1012 * 5) Decode variable length data area (if any for this SMB2 command type)
1013 * 6) Call free smb buffer
1014 * 7) return
1015 *
1016 */
1017
1018int
f486ef8e
SP
1019SMB2_negotiate(const unsigned int xid,
1020 struct cifs_ses *ses,
1021 struct TCP_Server_Info *server)
ec2e4523 1022{
40eff45b 1023 struct smb_rqst rqst;
ec2e4523
PS
1024 struct smb2_negotiate_req *req;
1025 struct smb2_negotiate_rsp *rsp;
1026 struct kvec iov[1];
da502f7d 1027 struct kvec rsp_iov;
f5823f5e 1028 int rc;
ec2e4523 1029 int resp_buftype;
ec2e4523
PS
1030 int blob_offset, blob_length;
1031 char *security_blob;
1032 int flags = CIFS_NEG_OP;
13cacea7 1033 unsigned int total_len;
ec2e4523 1034
f96637be 1035 cifs_dbg(FYI, "Negotiate protocol\n");
ec2e4523 1036
3534b850
JL
1037 if (!server) {
1038 WARN(1, "%s: server is NULL!\n", __func__);
1039 return -EIO;
ec2e4523
PS
1040 }
1041
352d96f3
AA
1042 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server,
1043 (void **) &req, &total_len);
ec2e4523
PS
1044 if (rc)
1045 return rc;
1046
0d35e382 1047 req->hdr.SessionId = 0;
0fdfef9a 1048
8bd68c6e
AA
1049 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
1050 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
ec2e4523 1051
f6a6bf7c 1052 if (strcmp(server->vals->version_string,
9764c02f
SF
1053 SMB3ANY_VERSION_STRING) == 0) {
1054 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1055 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
6dffa4c2
SF
1056 req->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
1057 req->DialectCount = cpu_to_le16(3);
1058 total_len += 6;
afe6f653 1059 } else if (strcmp(server->vals->version_string,
9764c02f
SF
1060 SMBDEFAULT_VERSION_STRING) == 0) {
1061 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1062 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1063 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
d5c7076b
SF
1064 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1065 req->DialectCount = cpu_to_le16(4);
1066 total_len += 8;
9764c02f
SF
1067 } else {
1068 /* otherwise send specific dialect */
f6a6bf7c 1069 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
9764c02f 1070 req->DialectCount = cpu_to_le16(1);
13cacea7 1071 total_len += 2;
9764c02f 1072 }
ec2e4523
PS
1073
1074 /* only one of SMB2 signing flags may be set in SMB2 request */
38d77c50 1075 if (ses->sign)
9cd2e62c 1076 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
38d77c50 1077 else if (global_secflags & CIFSSEC_MAY_SIGN)
9cd2e62c 1078 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
38d77c50
JL
1079 else
1080 req->SecurityMode = 0;
ec2e4523 1081
afe6f653 1082 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
679971e7
SF
1083 if (ses->chan_max > 1)
1084 req->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
ec2e4523 1085
3c5f9be1 1086 /* ClientGUID must be zero for SMB2.02 dialect */
afe6f653 1087 if (server->vals->protocol_id == SMB20_PROT_ID)
3c5f9be1 1088 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
ebb3a9d4 1089 else {
3c5f9be1
SF
1090 memcpy(req->ClientGUID, server->client_guid,
1091 SMB2_CLIENT_GUID_SIZE);
afe6f653 1092 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
6dffa4c2
SF
1093 (strcmp(server->vals->version_string,
1094 SMB3ANY_VERSION_STRING) == 0) ||
afe6f653 1095 (strcmp(server->vals->version_string,
d5c7076b 1096 SMBDEFAULT_VERSION_STRING) == 0))
9fe5ff1c 1097 assemble_neg_contexts(req, server, &total_len);
ebb3a9d4 1098 }
ec2e4523 1099 iov[0].iov_base = (char *)req;
13cacea7 1100 iov[0].iov_len = total_len;
ec2e4523 1101
40eff45b
RS
1102 memset(&rqst, 0, sizeof(struct smb_rqst));
1103 rqst.rq_iov = iov;
1104 rqst.rq_nvec = 1;
1105
352d96f3
AA
1106 rc = cifs_send_recv(xid, ses, server,
1107 &rqst, &resp_buftype, flags, &rsp_iov);
da502f7d
PS
1108 cifs_small_buf_release(req);
1109 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
ec2e4523
PS
1110 /*
1111 * No tcon so can't do
1112 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1113 */
7e682f76 1114 if (rc == -EOPNOTSUPP) {
a0a3036b 1115 cifs_server_dbg(VFS, "Dialect not supported by server. Consider specifying vers=1.0 or vers=2.0 on mount for accessing older servers\n");
7e682f76
SF
1116 goto neg_exit;
1117 } else if (rc != 0)
ec2e4523
PS
1118 goto neg_exit;
1119
27893dfc 1120 rc = -EIO;
afe6f653 1121 if (strcmp(server->vals->version_string,
9764c02f
SF
1122 SMB3ANY_VERSION_STRING) == 0) {
1123 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
afe6f653 1124 cifs_server_dbg(VFS,
9764c02f 1125 "SMB2 dialect returned but not requested\n");
27893dfc 1126 goto neg_exit;
9764c02f 1127 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
afe6f653 1128 cifs_server_dbg(VFS,
9764c02f 1129 "SMB2.1 dialect returned but not requested\n");
27893dfc 1130 goto neg_exit;
6dffa4c2
SF
1131 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1132 /* ops set to 3.0 by default for default so update */
1133 server->ops = &smb311_operations;
1134 server->vals = &smb311_values;
9764c02f 1135 }
afe6f653 1136 } else if (strcmp(server->vals->version_string,
9764c02f
SF
1137 SMBDEFAULT_VERSION_STRING) == 0) {
1138 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
afe6f653 1139 cifs_server_dbg(VFS,
9764c02f 1140 "SMB2 dialect returned but not requested\n");
27893dfc 1141 goto neg_exit;
9764c02f
SF
1142 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
1143 /* ops set to 3.0 by default for default so update */
afe6f653
RS
1144 server->ops = &smb21_operations;
1145 server->vals = &smb21_values;
b57a55e2 1146 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
afe6f653
RS
1147 server->ops = &smb311_operations;
1148 server->vals = &smb311_values;
b57a55e2 1149 }
590d08d3 1150 } else if (le16_to_cpu(rsp->DialectRevision) !=
afe6f653 1151 server->vals->protocol_id) {
9764c02f 1152 /* if requested single dialect ensure returned dialect matched */
a0a3036b
JP
1153 cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
1154 le16_to_cpu(rsp->DialectRevision));
27893dfc 1155 goto neg_exit;
9764c02f
SF
1156 }
1157
f96637be 1158 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
ec2e4523 1159
e4aa25e7 1160 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
f96637be 1161 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
e4aa25e7 1162 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
f96637be 1163 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
e4aa25e7 1164 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
f96637be 1165 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
20b6d8b4
SF
1166 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
1167 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
5f7fbf73
SF
1168 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
1169 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
ec2e4523 1170 else {
a0a3036b
JP
1171 cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n",
1172 le16_to_cpu(rsp->DialectRevision));
ec2e4523
PS
1173 goto neg_exit;
1174 }
27893dfc
EM
1175
1176 rc = 0;
ec2e4523
PS
1177 server->dialect = le16_to_cpu(rsp->DialectRevision);
1178
8bd68c6e
AA
1179 /*
1180 * Keep a copy of the hash after negprot. This hash will be
1181 * the starting hash value for all sessions made from this
1182 * server.
1183 */
1184 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
1185 SMB2_PREAUTH_HASH_SIZE);
0fdfef9a 1186
e598d1d8
JL
1187 /* SMB2 only has an extended negflavor */
1188 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
2365c4ea
PS
1189 /* set it to the maximum buffer size value we can send with 1 credit */
1190 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
1191 SMB2_MAX_BUFFER_SIZE);
ec2e4523
PS
1192 server->max_read = le32_to_cpu(rsp->MaxReadSize);
1193 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
ec2e4523 1194 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
07108d0e
SF
1195 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
1196 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
1197 server->sec_mode);
ec2e4523 1198 server->capabilities = le32_to_cpu(rsp->Capabilities);
29e20f9c
PS
1199 /* Internal types */
1200 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
ec2e4523 1201
6d2fcfe6
AA
1202 /*
1203 * SMB3.0 supports only 1 cipher and doesn't have a encryption neg context
1204 * Set the cipher type manually.
1205 */
1206 if (server->dialect == SMB30_PROT_ID && (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1207 server->cipher_type = SMB2_ENCRYPTION_AES128_CCM;
1208
ec2e4523 1209 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
0d35e382 1210 (struct smb2_hdr *)rsp);
5d875cc9
SF
1211 /*
1212 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
1213 * for us will be
1214 * ses->sectype = RawNTLMSSP;
1215 * but for time being this is our only auth choice so doesn't matter.
1216 * We just found a server which sets blob length to zero expecting raw.
1217 */
67dbea2c 1218 if (blob_length == 0) {
5d875cc9 1219 cifs_dbg(FYI, "missing security blob on negprot\n");
67dbea2c
PS
1220 server->sec_ntlmssp = true;
1221 }
3c1bf7e4 1222
38d77c50 1223 rc = cifs_enable_signing(server, ses->sign);
9ddec561
JL
1224 if (rc)
1225 goto neg_exit;
ceb1b0b9 1226 if (blob_length) {
ebdd207e 1227 rc = decode_negTokenInit(security_blob, blob_length, server);
ceb1b0b9
SF
1228 if (rc == 1)
1229 rc = 0;
1230 else if (rc == 0)
1231 rc = -EIO;
ec2e4523 1232 }
5100d8a3 1233
5100d8a3
SF
1234 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1235 if (rsp->NegotiateContextCount)
977b6170
RS
1236 rc = smb311_decode_neg_context(rsp, server,
1237 rsp_iov.iov_len);
5100d8a3 1238 else
afe6f653 1239 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
5100d8a3 1240 }
ec2e4523
PS
1241neg_exit:
1242 free_rsp_buf(resp_buftype, rsp);
1243 return rc;
1244}
5478f9ba 1245
ff1c038a
SF
1246int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1247{
2796d303
LL
1248 int rc;
1249 struct validate_negotiate_info_req *pneg_inbuf;
fe83bebc 1250 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
ff1c038a 1251 u32 rsplen;
9764c02f 1252 u32 inbuflen; /* max of 4 dialects */
afe6f653 1253 struct TCP_Server_Info *server = tcon->ses->server;
ff1c038a
SF
1254
1255 cifs_dbg(FYI, "validate negotiate\n");
1256
8bd68c6e 1257 /* In SMB3.11 preauth integrity supersedes validate negotiate */
afe6f653 1258 if (server->dialect == SMB311_PROT_ID)
8bd68c6e
AA
1259 return 0;
1260
ff1c038a
SF
1261 /*
1262 * validation ioctl must be signed, so no point sending this if we
0603c96f
SF
1263 * can not sign it (ie are not known user). Even if signing is not
1264 * required (enabled but not negotiated), in those cases we selectively
ff1c038a 1265 * sign just this, the first and only signed request on a connection.
0603c96f 1266 * Having validation of negotiate info helps reduce attack vectors.
ff1c038a 1267 */
0603c96f 1268 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
ff1c038a
SF
1269 return 0; /* validation requires signing */
1270
0603c96f
SF
1271 if (tcon->ses->user_name == NULL) {
1272 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1273 return 0; /* validation requires signing */
1274 }
1275
1276 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
3175eb9b 1277 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
0603c96f 1278
2796d303
LL
1279 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1280 if (!pneg_inbuf)
1281 return -ENOMEM;
1282
1283 pneg_inbuf->Capabilities =
afe6f653 1284 cpu_to_le32(server->vals->req_capabilities);
679971e7
SF
1285 if (tcon->ses->chan_max > 1)
1286 pneg_inbuf->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
1287
afe6f653 1288 memcpy(pneg_inbuf->Guid, server->client_guid,
39552ea8 1289 SMB2_CLIENT_GUID_SIZE);
ff1c038a
SF
1290
1291 if (tcon->ses->sign)
2796d303 1292 pneg_inbuf->SecurityMode =
ff1c038a
SF
1293 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1294 else if (global_secflags & CIFSSEC_MAY_SIGN)
2796d303 1295 pneg_inbuf->SecurityMode =
ff1c038a
SF
1296 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1297 else
2796d303 1298 pneg_inbuf->SecurityMode = 0;
ff1c038a 1299
9764c02f 1300
afe6f653 1301 if (strcmp(server->vals->version_string,
9764c02f 1302 SMB3ANY_VERSION_STRING) == 0) {
2796d303
LL
1303 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1304 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
6dffa4c2
SF
1305 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
1306 pneg_inbuf->DialectCount = cpu_to_le16(3);
1307 /* SMB 2.1 not included so subtract one dialect from len */
2796d303 1308 inbuflen = sizeof(*pneg_inbuf) -
6dffa4c2 1309 (sizeof(pneg_inbuf->Dialects[0]));
afe6f653 1310 } else if (strcmp(server->vals->version_string,
9764c02f 1311 SMBDEFAULT_VERSION_STRING) == 0) {
2796d303
LL
1312 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1313 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1314 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
d5c7076b
SF
1315 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1316 pneg_inbuf->DialectCount = cpu_to_le16(4);
6dffa4c2 1317 /* structure is big enough for 4 dialects */
2796d303 1318 inbuflen = sizeof(*pneg_inbuf);
9764c02f
SF
1319 } else {
1320 /* otherwise specific dialect was requested */
2796d303 1321 pneg_inbuf->Dialects[0] =
afe6f653 1322 cpu_to_le16(server->vals->protocol_id);
2796d303 1323 pneg_inbuf->DialectCount = cpu_to_le16(1);
e98ecc6e 1324 /* structure is big enough for 4 dialects, sending only 1 */
2796d303 1325 inbuflen = sizeof(*pneg_inbuf) -
e98ecc6e 1326 sizeof(pneg_inbuf->Dialects[0]) * 3;
9764c02f 1327 }
ff1c038a
SF
1328
1329 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
400d0ad6 1330 FSCTL_VALIDATE_NEGOTIATE_INFO,
153322f7
SF
1331 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1332 (char **)&pneg_rsp, &rsplen);
969ae8e8
NJ
1333 if (rc == -EOPNOTSUPP) {
1334 /*
1335 * Old Windows versions or Netapp SMB server can return
1336 * not supported error. Client should accept it.
1337 */
3175eb9b 1338 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
21078203
CIK
1339 rc = 0;
1340 goto out_free_inbuf;
969ae8e8 1341 } else if (rc != 0) {
a0a3036b
JP
1342 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n",
1343 rc);
2796d303
LL
1344 rc = -EIO;
1345 goto out_free_inbuf;
ff1c038a
SF
1346 }
1347
2796d303
LL
1348 rc = -EIO;
1349 if (rsplen != sizeof(*pneg_rsp)) {
a0a3036b
JP
1350 cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n",
1351 rsplen);
7db0a6ef
SF
1352
1353 /* relax check since Mac returns max bufsize allowed on ioctl */
2796d303
LL
1354 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1355 goto out_free_rsp;
ff1c038a
SF
1356 }
1357
1358 /* check validate negotiate info response matches what we got earlier */
afe6f653 1359 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
ff1c038a
SF
1360 goto vneg_out;
1361
afe6f653 1362 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
ff1c038a
SF
1363 goto vneg_out;
1364
1365 /* do not validate server guid because not saved at negprot time yet */
1366
1367 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
afe6f653 1368 SMB2_LARGE_FILES) != server->capabilities)
ff1c038a
SF
1369 goto vneg_out;
1370
1371 /* validate negotiate successful */
2796d303 1372 rc = 0;
ff1c038a 1373 cifs_dbg(FYI, "validate negotiate info successful\n");
2796d303 1374 goto out_free_rsp;
ff1c038a
SF
1375
1376vneg_out:
3175eb9b 1377 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
2796d303 1378out_free_rsp:
fe83bebc 1379 kfree(pneg_rsp);
2796d303
LL
1380out_free_inbuf:
1381 kfree(pneg_inbuf);
1382 return rc;
ff1c038a
SF
1383}
1384
ef65aaed
SP
1385enum securityEnum
1386smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1387{
1388 switch (requested) {
1389 case Kerberos:
1390 case RawNTLMSSP:
1391 return requested;
1392 case NTLMv2:
1393 return RawNTLMSSP;
1394 case Unspecified:
1395 if (server->sec_ntlmssp &&
1396 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1397 return RawNTLMSSP;
1398 if ((server->sec_kerberos || server->sec_mskerberos) &&
1399 (global_secflags & CIFSSEC_MAY_KRB5))
1400 return Kerberos;
df561f66 1401 fallthrough;
ef65aaed
SP
1402 default:
1403 return Unspecified;
1404 }
1405}
1406
3baf1a7b
SP
1407struct SMB2_sess_data {
1408 unsigned int xid;
1409 struct cifs_ses *ses;
f486ef8e 1410 struct TCP_Server_Info *server;
3baf1a7b
SP
1411 struct nls_table *nls_cp;
1412 void (*func)(struct SMB2_sess_data *);
1413 int result;
1414 u64 previous_session;
1415
1416 /* we will send the SMB in three pieces:
1417 * a fixed length beginning part, an optional
1418 * SPNEGO blob (which can be zero length), and a
1419 * last part which will include the strings
1420 * and rest of bcc area. This allows us to avoid
1421 * a large buffer 17K allocation
1422 */
1423 int buf0_type;
1424 struct kvec iov[2];
1425};
1426
1427static int
1428SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1429{
1430 int rc;
1431 struct cifs_ses *ses = sess_data->ses;
f486ef8e 1432 struct TCP_Server_Info *server = sess_data->server;
3baf1a7b 1433 struct smb2_sess_setup_req *req;
88ea5cb7 1434 unsigned int total_len;
f486ef8e 1435 bool is_binding = false;
3baf1a7b 1436
352d96f3
AA
1437 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server,
1438 (void **) &req,
1439 &total_len);
3baf1a7b
SP
1440 if (rc)
1441 return rc;
1442
bc962159
SP
1443 spin_lock(&ses->ses_lock);
1444 is_binding = (ses->ses_status == SES_GOOD);
1445 spin_unlock(&ses->ses_lock);
f486ef8e
SP
1446
1447 if (is_binding) {
1448 req->hdr.SessionId = cpu_to_le64(ses->Suid);
0d35e382 1449 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
d70e9fa5
AA
1450 req->PreviousSessionId = 0;
1451 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
f486ef8e 1452 cifs_dbg(FYI, "Binding to sess id: %llx\n", ses->Suid);
d70e9fa5
AA
1453 } else {
1454 /* First session, not a reauthenticate */
0d35e382 1455 req->hdr.SessionId = 0;
d70e9fa5
AA
1456 /*
1457 * if reconnect, we need to send previous sess id
1458 * otherwise it is 0
1459 */
d8d9de53 1460 req->PreviousSessionId = cpu_to_le64(sess_data->previous_session);
d70e9fa5 1461 req->Flags = 0; /* MBZ */
f486ef8e
SP
1462 cifs_dbg(FYI, "Fresh session. Previous: %llx\n",
1463 sess_data->previous_session);
d70e9fa5 1464 }
d409014e
SF
1465
1466 /* enough to enable echos and oplocks and one max size write */
5e90aa21
SP
1467 if (server->credits >= server->max_credits)
1468 req->hdr.CreditRequest = cpu_to_le16(0);
1469 else
1470 req->hdr.CreditRequest = cpu_to_le16(
1471 min_t(int, server->max_credits -
1472 server->credits, 130));
3baf1a7b
SP
1473
1474 /* only one of SMB2 signing flags may be set in SMB2 request */
1475 if (server->sign)
1476 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1477 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1478 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1479 else
1480 req->SecurityMode = 0;
1481
8d33096a
SF
1482#ifdef CONFIG_CIFS_DFS_UPCALL
1483 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1484#else
3baf1a7b 1485 req->Capabilities = 0;
8d33096a
SF
1486#endif /* DFS_UPCALL */
1487
3baf1a7b
SP
1488 req->Channel = 0; /* MBZ */
1489
1490 sess_data->iov[0].iov_base = (char *)req;
88ea5cb7
RS
1491 /* 1 for pad */
1492 sess_data->iov[0].iov_len = total_len - 1;
3baf1a7b
SP
1493 /*
1494 * This variable will be used to clear the buffer
1495 * allocated above in case of any error in the calling function.
1496 */
1497 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1498
1499 return 0;
1500}
1501
1502static void
1503SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1504{
01f2ee7e 1505 struct kvec *iov = sess_data->iov;
a4e430c8 1506
01f2ee7e
PA
1507 /* iov[1] is already freed by caller */
1508 if (sess_data->buf0_type != CIFS_NO_BUFFER && iov[0].iov_base)
1509 memzero_explicit(iov[0].iov_base, iov[0].iov_len);
a4e430c8 1510
01f2ee7e 1511 free_rsp_buf(sess_data->buf0_type, iov[0].iov_base);
3baf1a7b
SP
1512 sess_data->buf0_type = CIFS_NO_BUFFER;
1513}
1514
1515static int
1516SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1517{
1518 int rc;
40eff45b 1519 struct smb_rqst rqst;
3baf1a7b 1520 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
da502f7d 1521 struct kvec rsp_iov = { NULL, 0 };
3baf1a7b
SP
1522
1523 /* Testing shows that buffer offset must be at location of Buffer[0] */
1524 req->SecurityBufferOffset =
eb3e28c1 1525 cpu_to_le16(sizeof(struct smb2_sess_setup_req));
3baf1a7b
SP
1526 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1527
40eff45b
RS
1528 memset(&rqst, 0, sizeof(struct smb_rqst));
1529 rqst.rq_iov = sess_data->iov;
1530 rqst.rq_nvec = 2;
3baf1a7b 1531
40eff45b
RS
1532 /* BB add code to build os and lm fields */
1533 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
f486ef8e 1534 sess_data->server,
40eff45b 1535 &rqst,
88ea5cb7 1536 &sess_data->buf0_type,
0f56db83 1537 CIFS_LOG_ERROR | CIFS_SESS_OP, &rsp_iov);
da502f7d
PS
1538 cifs_small_buf_release(sess_data->iov[0].iov_base);
1539 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
3baf1a7b
SP
1540
1541 return rc;
1542}
1543
1544static int
1545SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1546{
1547 int rc = 0;
1548 struct cifs_ses *ses = sess_data->ses;
f486ef8e 1549 struct TCP_Server_Info *server = sess_data->server;
3baf1a7b 1550
cc391b69 1551 cifs_server_lock(server);
f6a6bf7c 1552 if (server->ops->generate_signingkey) {
f486ef8e 1553 rc = server->ops->generate_signingkey(ses, server);
3baf1a7b
SP
1554 if (rc) {
1555 cifs_dbg(FYI,
1556 "SMB3 session key generation failed\n");
cc391b69 1557 cifs_server_unlock(server);
cabfb368 1558 return rc;
3baf1a7b
SP
1559 }
1560 }
f6a6bf7c
AA
1561 if (!server->session_estab) {
1562 server->sequence_number = 0x2;
1563 server->session_estab = true;
3baf1a7b 1564 }
cc391b69 1565 cifs_server_unlock(server);
3baf1a7b
SP
1566
1567 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
3baf1a7b
SP
1568 return rc;
1569}
1570
1571#ifdef CONFIG_CIFS_UPCALL
1572static void
1573SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1574{
1575 int rc;
1576 struct cifs_ses *ses = sess_data->ses;
f486ef8e 1577 struct TCP_Server_Info *server = sess_data->server;
3baf1a7b
SP
1578 struct cifs_spnego_msg *msg;
1579 struct key *spnego_key = NULL;
1580 struct smb2_sess_setup_rsp *rsp = NULL;
f486ef8e 1581 bool is_binding = false;
3baf1a7b
SP
1582
1583 rc = SMB2_sess_alloc_buffer(sess_data);
1584 if (rc)
1585 goto out;
1586
f486ef8e 1587 spnego_key = cifs_get_spnego_key(ses, server);
3baf1a7b
SP
1588 if (IS_ERR(spnego_key)) {
1589 rc = PTR_ERR(spnego_key);
0a018944
SF
1590 if (rc == -ENOKEY)
1591 cifs_dbg(VFS, "Verify user has a krb5 ticket and keyutils is installed\n");
3baf1a7b
SP
1592 spnego_key = NULL;
1593 goto out;
1594 }
1595
1596 msg = spnego_key->payload.data[0];
1597 /*
1598 * check version field to make sure that cifs.upcall is
1599 * sending us a response in an expected form
1600 */
1601 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
a0a3036b
JP
1602 cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n",
1603 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
3baf1a7b
SP
1604 rc = -EKEYREJECTED;
1605 goto out_put_spnego_key;
1606 }
1607
bc962159
SP
1608 spin_lock(&ses->ses_lock);
1609 is_binding = (ses->ses_status == SES_GOOD);
1610 spin_unlock(&ses->ses_lock);
f486ef8e 1611
d70e9fa5 1612 /* keep session key if binding */
f486ef8e 1613 if (!is_binding) {
2fe58d97 1614 kfree_sensitive(ses->auth_key.response);
d70e9fa5
AA
1615 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1616 GFP_KERNEL);
1617 if (!ses->auth_key.response) {
a0a3036b 1618 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
d70e9fa5
AA
1619 msg->sesskey_len);
1620 rc = -ENOMEM;
1621 goto out_put_spnego_key;
1622 }
1623 ses->auth_key.len = msg->sesskey_len;
3baf1a7b 1624 }
3baf1a7b
SP
1625
1626 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1627 sess_data->iov[1].iov_len = msg->secblob_len;
1628
1629 rc = SMB2_sess_sendreceive(sess_data);
1630 if (rc)
1631 goto out_put_spnego_key;
1632
1633 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
d70e9fa5 1634 /* keep session id and flags if binding */
f486ef8e 1635 if (!is_binding) {
0d35e382 1636 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
d70e9fa5
AA
1637 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1638 }
3baf1a7b
SP
1639
1640 rc = SMB2_sess_establish_session(sess_data);
1641out_put_spnego_key:
1642 key_invalidate(spnego_key);
1643 key_put(spnego_key);
39e8db3c 1644 if (rc) {
a4e430c8 1645 kfree_sensitive(ses->auth_key.response);
39e8db3c
PA
1646 ses->auth_key.response = NULL;
1647 ses->auth_key.len = 0;
1648 }
3baf1a7b
SP
1649out:
1650 sess_data->result = rc;
1651 sess_data->func = NULL;
1652 SMB2_sess_free_buffer(sess_data);
1653}
1654#else
1655static void
1656SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1657{
1658 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1659 sess_data->result = -EOPNOTSUPP;
1660 sess_data->func = NULL;
1661}
1662#endif
1663
166cea4d
SP
1664static void
1665SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1666
1667static void
1668SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
5478f9ba 1669{
166cea4d
SP
1670 int rc;
1671 struct cifs_ses *ses = sess_data->ses;
f486ef8e 1672 struct TCP_Server_Info *server = sess_data->server;
5478f9ba 1673 struct smb2_sess_setup_rsp *rsp = NULL;
49bd49f9 1674 unsigned char *ntlmssp_blob = NULL;
5478f9ba 1675 bool use_spnego = false; /* else use raw ntlmssp */
166cea4d 1676 u16 blob_length = 0;
f486ef8e 1677 bool is_binding = false;
d4e63bd6 1678
5478f9ba
PS
1679 /*
1680 * If memory allocation is successful, caller of this function
1681 * frees it.
1682 */
1683 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
166cea4d
SP
1684 if (!ses->ntlmssp) {
1685 rc = -ENOMEM;
1686 goto out_err;
1687 }
5c234aa5 1688 ses->ntlmssp->sesskey_per_smbsess = true;
5478f9ba 1689
166cea4d 1690 rc = SMB2_sess_alloc_buffer(sess_data);
5478f9ba 1691 if (rc)
166cea4d 1692 goto out_err;
5478f9ba 1693
52d00533 1694 rc = build_ntlmssp_smb3_negotiate_blob(&ntlmssp_blob,
f486ef8e 1695 &blob_length, ses, server,
49bd49f9
SP
1696 sess_data->nls_cp);
1697 if (rc)
30b2d7f8 1698 goto out;
c2afb814 1699
166cea4d
SP
1700 if (use_spnego) {
1701 /* BB eventually need to add this */
1702 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1703 rc = -EOPNOTSUPP;
1704 goto out;
166cea4d
SP
1705 }
1706 sess_data->iov[1].iov_base = ntlmssp_blob;
1707 sess_data->iov[1].iov_len = blob_length;
c2afb814 1708
166cea4d
SP
1709 rc = SMB2_sess_sendreceive(sess_data);
1710 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
5478f9ba 1711
166cea4d
SP
1712 /* If true, rc here is expected and not an error */
1713 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
0d35e382 1714 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
166cea4d 1715 rc = 0;
38d77c50 1716
166cea4d
SP
1717 if (rc)
1718 goto out;
5478f9ba 1719
1fc6ad2f 1720 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
166cea4d
SP
1721 le16_to_cpu(rsp->SecurityBufferOffset)) {
1722 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1723 le16_to_cpu(rsp->SecurityBufferOffset));
5478f9ba 1724 rc = -EIO;
166cea4d 1725 goto out;
5478f9ba 1726 }
166cea4d
SP
1727 rc = decode_ntlmssp_challenge(rsp->Buffer,
1728 le16_to_cpu(rsp->SecurityBufferLength), ses);
1729 if (rc)
1730 goto out;
5478f9ba 1731
166cea4d 1732 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
5478f9ba 1733
bc962159
SP
1734 spin_lock(&ses->ses_lock);
1735 is_binding = (ses->ses_status == SES_GOOD);
1736 spin_unlock(&ses->ses_lock);
f486ef8e 1737
d70e9fa5 1738 /* keep existing ses id and flags if binding */
f486ef8e 1739 if (!is_binding) {
0d35e382 1740 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
d70e9fa5
AA
1741 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1742 }
166cea4d
SP
1743
1744out:
01f2ee7e 1745 kfree_sensitive(ntlmssp_blob);
166cea4d
SP
1746 SMB2_sess_free_buffer(sess_data);
1747 if (!rc) {
1748 sess_data->result = 0;
1749 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1750 return;
1751 }
1752out_err:
a4e430c8 1753 kfree_sensitive(ses->ntlmssp);
166cea4d
SP
1754 ses->ntlmssp = NULL;
1755 sess_data->result = rc;
1756 sess_data->func = NULL;
1757}
5478f9ba 1758
166cea4d
SP
1759static void
1760SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1761{
1762 int rc;
1763 struct cifs_ses *ses = sess_data->ses;
f486ef8e 1764 struct TCP_Server_Info *server = sess_data->server;
166cea4d
SP
1765 struct smb2_sess_setup_req *req;
1766 struct smb2_sess_setup_rsp *rsp = NULL;
1767 unsigned char *ntlmssp_blob = NULL;
1768 bool use_spnego = false; /* else use raw ntlmssp */
1769 u16 blob_length = 0;
f486ef8e 1770 bool is_binding = false;
5478f9ba 1771
166cea4d
SP
1772 rc = SMB2_sess_alloc_buffer(sess_data);
1773 if (rc)
1774 goto out;
5478f9ba 1775
166cea4d 1776 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
0d35e382 1777 req->hdr.SessionId = cpu_to_le64(ses->Suid);
166cea4d 1778
f486ef8e
SP
1779 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length,
1780 ses, server,
1781 sess_data->nls_cp);
166cea4d
SP
1782 if (rc) {
1783 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1784 goto out;
5478f9ba
PS
1785 }
1786
166cea4d
SP
1787 if (use_spnego) {
1788 /* BB eventually need to add this */
1789 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1790 rc = -EOPNOTSUPP;
1791 goto out;
1792 }
1793 sess_data->iov[1].iov_base = ntlmssp_blob;
1794 sess_data->iov[1].iov_len = blob_length;
5478f9ba 1795
166cea4d
SP
1796 rc = SMB2_sess_sendreceive(sess_data);
1797 if (rc)
1798 goto out;
1799
1800 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1801
bc962159
SP
1802 spin_lock(&ses->ses_lock);
1803 is_binding = (ses->ses_status == SES_GOOD);
1804 spin_unlock(&ses->ses_lock);
f486ef8e 1805
d70e9fa5 1806 /* keep existing ses id and flags if binding */
f486ef8e 1807 if (!is_binding) {
0d35e382 1808 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
d70e9fa5
AA
1809 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1810 }
5478f9ba 1811
166cea4d 1812 rc = SMB2_sess_establish_session(sess_data);
f560cda9
RS
1813#ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1814 if (ses->server->dialect < SMB30_PROT_ID) {
1815 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
1816 /*
1817 * The session id is opaque in terms of endianness, so we can't
1818 * print it as a long long. we dump it as we got it on the wire
1819 */
1820 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
1821 &ses->Suid);
1822 cifs_dbg(VFS, "Session Key %*ph\n",
1823 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
1824 cifs_dbg(VFS, "Signing Key %*ph\n",
1825 SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
1826 }
1827#endif
166cea4d 1828out:
01f2ee7e 1829 kfree_sensitive(ntlmssp_blob);
166cea4d 1830 SMB2_sess_free_buffer(sess_data);
a4e430c8 1831 kfree_sensitive(ses->ntlmssp);
166cea4d
SP
1832 ses->ntlmssp = NULL;
1833 sess_data->result = rc;
1834 sess_data->func = NULL;
1835}
d4e63bd6 1836
166cea4d 1837static int
f486ef8e 1838SMB2_select_sec(struct SMB2_sess_data *sess_data)
166cea4d 1839{
ef65aaed 1840 int type;
f486ef8e
SP
1841 struct cifs_ses *ses = sess_data->ses;
1842 struct TCP_Server_Info *server = sess_data->server;
ef65aaed 1843
f486ef8e 1844 type = smb2_select_sectype(server, ses->sectype);
ef65aaed
SP
1845 cifs_dbg(FYI, "sess setup type %d\n", type);
1846 if (type == Unspecified) {
a0a3036b 1847 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
ef65aaed
SP
1848 return -EINVAL;
1849 }
d4e63bd6 1850
ef65aaed 1851 switch (type) {
166cea4d
SP
1852 case Kerberos:
1853 sess_data->func = SMB2_auth_kerberos;
1854 break;
1855 case RawNTLMSSP:
1856 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1857 break;
1858 default:
ef65aaed 1859 cifs_dbg(VFS, "secType %d not supported!\n", type);
166cea4d 1860 return -EOPNOTSUPP;
d4e63bd6
SP
1861 }
1862
166cea4d
SP
1863 return 0;
1864}
1865
1866int
1867SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
f486ef8e 1868 struct TCP_Server_Info *server,
166cea4d
SP
1869 const struct nls_table *nls_cp)
1870{
1871 int rc = 0;
166cea4d
SP
1872 struct SMB2_sess_data *sess_data;
1873
1874 cifs_dbg(FYI, "Session Setup\n");
1875
1876 if (!server) {
1877 WARN(1, "%s: server is NULL!\n", __func__);
1878 return -EIO;
d4e63bd6 1879 }
d4e63bd6 1880
166cea4d
SP
1881 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1882 if (!sess_data)
1883 return -ENOMEM;
1884
166cea4d
SP
1885 sess_data->xid = xid;
1886 sess_data->ses = ses;
f486ef8e 1887 sess_data->server = server;
166cea4d
SP
1888 sess_data->buf0_type = CIFS_NO_BUFFER;
1889 sess_data->nls_cp = (struct nls_table *) nls_cp;
b2adf22f 1890 sess_data->previous_session = ses->Suid;
166cea4d 1891
f486ef8e
SP
1892 rc = SMB2_select_sec(sess_data);
1893 if (rc)
1894 goto out;
1895
8bd68c6e
AA
1896 /*
1897 * Initialize the session hash with the server one.
1898 */
f6a6bf7c 1899 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
8bd68c6e 1900 SMB2_PREAUTH_HASH_SIZE);
8bd68c6e 1901
166cea4d
SP
1902 while (sess_data->func)
1903 sess_data->func(sess_data);
1904
c721c389 1905 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
afe6f653 1906 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
3baf1a7b 1907 rc = sess_data->result;
166cea4d 1908out:
a4e430c8 1909 kfree_sensitive(sess_data);
5478f9ba
PS
1910 return rc;
1911}
1912
1913int
1914SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1915{
40eff45b 1916 struct smb_rqst rqst;
5478f9ba
PS
1917 struct smb2_logoff_req *req; /* response is also trivial struct */
1918 int rc = 0;
1919 struct TCP_Server_Info *server;
7fb8986e 1920 int flags = 0;
45305eda
RS
1921 unsigned int total_len;
1922 struct kvec iov[1];
1923 struct kvec rsp_iov;
1924 int resp_buf_type;
5478f9ba 1925
f96637be 1926 cifs_dbg(FYI, "disconnect session %p\n", ses);
5478f9ba
PS
1927
1928 if (ses && (ses->server))
1929 server = ses->server;
1930 else
1931 return -EIO;
1932
eb4c7df6 1933 /* no need to send SMB logoff if uid already closed due to reconnect */
d1a931ce
SP
1934 spin_lock(&ses->chan_lock);
1935 if (CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
1936 spin_unlock(&ses->chan_lock);
eb4c7df6 1937 goto smb2_session_already_dead;
d1a931ce
SP
1938 }
1939 spin_unlock(&ses->chan_lock);
eb4c7df6 1940
352d96f3
AA
1941 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server,
1942 (void **) &req, &total_len);
5478f9ba
PS
1943 if (rc)
1944 return rc;
1945
1946 /* since no tcon, smb2_init can not do this, so do here */
0d35e382 1947 req->hdr.SessionId = cpu_to_le64(ses->Suid);
7fb8986e
PS
1948
1949 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1950 flags |= CIFS_TRANSFORM_REQ;
1951 else if (server->sign)
0d35e382 1952 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
45305eda 1953
392e1c5d 1954 flags |= CIFS_NO_RSP_BUF;
45305eda
RS
1955
1956 iov[0].iov_base = (char *)req;
1957 iov[0].iov_len = total_len;
5478f9ba 1958
40eff45b
RS
1959 memset(&rqst, 0, sizeof(struct smb_rqst));
1960 rqst.rq_iov = iov;
1961 rqst.rq_nvec = 1;
1962
352d96f3
AA
1963 rc = cifs_send_recv(xid, ses, ses->server,
1964 &rqst, &resp_buf_type, flags, &rsp_iov);
da502f7d 1965 cifs_small_buf_release(req);
5478f9ba
PS
1966 /*
1967 * No tcon so can't do
1968 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1969 */
eb4c7df6
SP
1970
1971smb2_session_already_dead:
5478f9ba
PS
1972 return rc;
1973}
faaf946a
PS
1974
1975static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1976{
d60622eb 1977 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
faaf946a
PS
1978}
1979
1980#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1981
de9f68df
SF
1982/* These are similar values to what Windows uses */
1983static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1984{
1985 tcon->max_chunks = 256;
1986 tcon->max_bytes_chunk = 1048576;
1987 tcon->max_bytes_copy = 16777216;
1988}
1989
faaf946a
PS
1990int
1991SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1992 struct cifs_tcon *tcon, const struct nls_table *cp)
1993{
40eff45b 1994 struct smb_rqst rqst;
faaf946a
PS
1995 struct smb2_tree_connect_req *req;
1996 struct smb2_tree_connect_rsp *rsp = NULL;
1997 struct kvec iov[2];
db3b5474 1998 struct kvec rsp_iov = { NULL, 0 };
faaf946a
PS
1999 int rc = 0;
2000 int resp_buftype;
2001 int unc_path_len;
faaf946a 2002 __le16 *unc_path = NULL;
7fb8986e 2003 int flags = 0;
661bb943 2004 unsigned int total_len;
268b8b57 2005 struct TCP_Server_Info *server = cifs_pick_channel(ses);
faaf946a 2006
f96637be 2007 cifs_dbg(FYI, "TCON\n");
faaf946a 2008
afe6f653 2009 if (!server || !tree)
faaf946a
PS
2010 return -EIO;
2011
faaf946a
PS
2012 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
2013 if (unc_path == NULL)
2014 return -ENOMEM;
2015
5574920c
NJ
2016 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp);
2017 if (unc_path_len <= 0) {
faaf946a
PS
2018 kfree(unc_path);
2019 return -EINVAL;
2020 }
5574920c 2021 unc_path_len *= 2;
faaf946a 2022
806a28ef 2023 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
b327a717 2024 tcon->tid = 0;
fae8044c 2025 atomic_set(&tcon->num_remote_opens, 0);
352d96f3
AA
2026 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server,
2027 (void **) &req, &total_len);
faaf946a
PS
2028 if (rc) {
2029 kfree(unc_path);
2030 return rc;
2031 }
2032
5a77e75f 2033 if (smb3_encryption_required(tcon))
ae6f8dd4 2034 flags |= CIFS_TRANSFORM_REQ;
faaf946a
PS
2035
2036 iov[0].iov_base = (char *)req;
661bb943
RS
2037 /* 1 for pad */
2038 iov[0].iov_len = total_len - 1;
faaf946a
PS
2039
2040 /* Testing shows that buffer offset must be at location of Buffer[0] */
eb3e28c1 2041 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req));
5574920c 2042 req->PathLength = cpu_to_le16(unc_path_len);
faaf946a
PS
2043 iov[1].iov_base = unc_path;
2044 iov[1].iov_len = unc_path_len;
2045
e71ab2aa
RS
2046 /*
2047 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
2048 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
8c11a607 2049 * (Samba servers don't always set the flag so also check if null user)
e71ab2aa 2050 */
afe6f653 2051 if ((server->dialect == SMB311_PROT_ID) &&
e71ab2aa 2052 !smb3_encryption_required(tcon) &&
8c11a607
SF
2053 !(ses->session_flags &
2054 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
2055 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
0d35e382 2056 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
6188f28b 2057
40eff45b
RS
2058 memset(&rqst, 0, sizeof(struct smb_rqst));
2059 rqst.rq_iov = iov;
2060 rqst.rq_nvec = 2;
2061
4fe75c4e 2062 /* Need 64 for max size write so ask for more in case not there yet */
5e90aa21
SP
2063 if (server->credits >= server->max_credits)
2064 req->hdr.CreditRequest = cpu_to_le16(0);
2065 else
2066 req->hdr.CreditRequest = cpu_to_le16(
2067 min_t(int, server->max_credits -
2068 server->credits, 64));
4fe75c4e 2069
352d96f3
AA
2070 rc = cifs_send_recv(xid, ses, server,
2071 &rqst, &resp_buftype, flags, &rsp_iov);
da502f7d
PS
2072 cifs_small_buf_release(req);
2073 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
f8af49dd 2074 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
bac35395 2075 if ((rc != 0) || (rsp == NULL)) {
3559134e
SF
2076 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
2077 tcon->need_reconnect = true;
faaf946a
PS
2078 goto tcon_error_exit;
2079 }
2080
cd123007
CJ
2081 switch (rsp->ShareType) {
2082 case SMB2_SHARE_TYPE_DISK:
f96637be 2083 cifs_dbg(FYI, "connection to disk share\n");
cd123007
CJ
2084 break;
2085 case SMB2_SHARE_TYPE_PIPE:
b327a717 2086 tcon->pipe = true;
f96637be 2087 cifs_dbg(FYI, "connection to pipe share\n");
cd123007
CJ
2088 break;
2089 case SMB2_SHARE_TYPE_PRINT:
b327a717 2090 tcon->print = true;
f96637be 2091 cifs_dbg(FYI, "connection to printer\n");
cd123007
CJ
2092 break;
2093 default:
afe6f653 2094 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
faaf946a
PS
2095 rc = -EOPNOTSUPP;
2096 goto tcon_error_exit;
2097 }
2098
2099 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
769ee6a4 2100 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
faaf946a 2101 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
0d35e382 2102 tcon->tid = le32_to_cpu(rsp->hdr.Id.SyncId.TreeId);
68e14569 2103 strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name));
faaf946a
PS
2104
2105 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
2106 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
3175eb9b 2107 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
ae6f8dd4
PS
2108
2109 if (tcon->seal &&
afe6f653 2110 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
3175eb9b 2111 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
ae6f8dd4 2112
de9f68df 2113 init_copy_chunk_defaults(tcon);
afe6f653
RS
2114 if (server->ops->validate_negotiate)
2115 rc = server->ops->validate_negotiate(xid, tcon);
cbd4cbab
SF
2116 if (rc == 0) /* See MS-SMB2 2.2.10 and 3.2.5.5 */
2117 if (tcon->share_flags & SMB2_SHAREFLAG_ISOLATED_TRANSPORT)
2118 server->nosharesock = true;
faaf946a 2119tcon_exit:
f8af49dd 2120
faaf946a
PS
2121 free_rsp_buf(resp_buftype, rsp);
2122 kfree(unc_path);
2123 return rc;
2124
2125tcon_error_exit:
0d35e382 2126 if (rsp && rsp->hdr.Status == STATUS_BAD_NETWORK_NAME)
3175eb9b 2127 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
faaf946a
PS
2128 goto tcon_exit;
2129}
2130
2131int
2132SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
2133{
40eff45b 2134 struct smb_rqst rqst;
faaf946a
PS
2135 struct smb2_tree_disconnect_req *req; /* response is trivial */
2136 int rc = 0;
faaf946a 2137 struct cifs_ses *ses = tcon->ses;
268b8b57 2138 struct TCP_Server_Info *server = cifs_pick_channel(ses);
7fb8986e 2139 int flags = 0;
4eecf4cf
RS
2140 unsigned int total_len;
2141 struct kvec iov[1];
2142 struct kvec rsp_iov;
2143 int resp_buf_type;
faaf946a 2144
f96637be 2145 cifs_dbg(FYI, "Tree Disconnect\n");
faaf946a 2146
68a6afa7 2147 if (!ses || !(ses->server))
faaf946a
PS
2148 return -EIO;
2149
68e14569 2150 trace_smb3_tdis_enter(xid, tcon->tid, ses->Suid, tcon->tree_name);
d1a931ce
SP
2151 spin_lock(&ses->chan_lock);
2152 if ((tcon->need_reconnect) ||
2153 (CIFS_ALL_CHANS_NEED_RECONNECT(tcon->ses))) {
2154 spin_unlock(&ses->chan_lock);
faaf946a 2155 return 0;
d1a931ce
SP
2156 }
2157 spin_unlock(&ses->chan_lock);
faaf946a 2158
dcb45fd7 2159 invalidate_all_cached_dirs(tcon);
72e73c78 2160
268b8b57 2161 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, server,
352d96f3
AA
2162 (void **) &req,
2163 &total_len);
faaf946a
PS
2164 if (rc)
2165 return rc;
2166
5a77e75f 2167 if (smb3_encryption_required(tcon))
7fb8986e
PS
2168 flags |= CIFS_TRANSFORM_REQ;
2169
392e1c5d 2170 flags |= CIFS_NO_RSP_BUF;
4eecf4cf
RS
2171
2172 iov[0].iov_base = (char *)req;
2173 iov[0].iov_len = total_len;
2174
40eff45b
RS
2175 memset(&rqst, 0, sizeof(struct smb_rqst));
2176 rqst.rq_iov = iov;
2177 rqst.rq_nvec = 1;
2178
268b8b57 2179 rc = cifs_send_recv(xid, ses, server,
352d96f3 2180 &rqst, &resp_buf_type, flags, &rsp_iov);
da502f7d 2181 cifs_small_buf_release(req);
68e14569 2182 if (rc) {
faaf946a 2183 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
68e14569
SF
2184 trace_smb3_tdis_err(xid, tcon->tid, ses->Suid, rc);
2185 }
2186 trace_smb3_tdis_done(xid, tcon->tid, ses->Suid);
faaf946a
PS
2187
2188 return rc;
2189}
2503a0db 2190
b8c32dbb 2191
63eb3def
PS
2192static struct create_durable *
2193create_durable_buf(void)
2194{
2195 struct create_durable *buf;
2196
2197 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2198 if (!buf)
2199 return NULL;
2200
2201 buf->ccontext.DataOffset = cpu_to_le16(offsetof
9cbc0b73 2202 (struct create_durable, Data));
63eb3def
PS
2203 buf->ccontext.DataLength = cpu_to_le32(16);
2204 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2205 (struct create_durable, Name));
2206 buf->ccontext.NameLength = cpu_to_le16(4);
12197a7f 2207 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
63eb3def
PS
2208 buf->Name[0] = 'D';
2209 buf->Name[1] = 'H';
2210 buf->Name[2] = 'n';
2211 buf->Name[3] = 'Q';
2212 return buf;
2213}
2214
9cbc0b73
PS
2215static struct create_durable *
2216create_reconnect_durable_buf(struct cifs_fid *fid)
2217{
2218 struct create_durable *buf;
2219
2220 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2221 if (!buf)
2222 return NULL;
2223
2224 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2225 (struct create_durable, Data));
2226 buf->ccontext.DataLength = cpu_to_le32(16);
2227 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2228 (struct create_durable, Name));
2229 buf->ccontext.NameLength = cpu_to_le16(4);
2230 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
2231 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
12197a7f 2232 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
9cbc0b73
PS
2233 buf->Name[0] = 'D';
2234 buf->Name[1] = 'H';
2235 buf->Name[2] = 'n';
2236 buf->Name[3] = 'C';
2237 return buf;
2238}
2239
89a5bfa3
SF
2240static void
2241parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
2242{
1149c846 2243 struct create_disk_id_rsp *pdisk_id = (struct create_disk_id_rsp *)cc;
89a5bfa3
SF
2244
2245 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
2246 pdisk_id->DiskFileId, pdisk_id->VolumeId);
2247 buf->IndexNumber = pdisk_id->DiskFileId;
2248}
2249
ab3459d8 2250static void
69dda305
AA
2251parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
2252 struct create_posix_rsp *posix)
ab3459d8 2253{
69dda305
AA
2254 int sid_len;
2255 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
2256 u8 *end = beg + le32_to_cpu(cc->DataLength);
2257 u8 *sid;
ab3459d8 2258
69dda305
AA
2259 memset(posix, 0, sizeof(*posix));
2260
2261 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
2262 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
2263 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
2264
2265 sid = beg + 12;
2266 sid_len = posix_info_sid_size(sid, end);
2267 if (sid_len < 0) {
2268 cifs_dbg(VFS, "bad owner sid in posix create response\n");
2269 return;
2270 }
2271 memcpy(&posix->owner, sid, sid_len);
2272
2273 sid = sid + sid_len;
2274 sid_len = posix_info_sid_size(sid, end);
2275 if (sid_len < 0) {
2276 cifs_dbg(VFS, "bad group sid in posix create response\n");
2277 return;
2278 }
2279 memcpy(&posix->group, sid, sid_len);
2e8af978 2280
69dda305
AA
2281 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
2282 posix->nlink, posix->mode, posix->reparse_tag);
ab3459d8
SF
2283}
2284
af1689a9
PA
2285int smb2_parse_contexts(struct TCP_Server_Info *server,
2286 struct kvec *rsp_iov,
2287 unsigned int *epoch,
2288 char *lease_key, __u8 *oplock,
2289 struct smb2_file_all_info *buf,
2290 struct create_posix_rsp *posix)
b8c32dbb 2291{
af1689a9 2292 struct smb2_create_rsp *rsp = rsp_iov->iov_base;
b5c7cde3 2293 struct create_context *cc;
af1689a9
PA
2294 size_t rem, off, len;
2295 size_t doff, dlen;
2296 size_t noff, nlen;
fd554396 2297 char *name;
3ece60e3
CIK
2298 static const char smb3_create_tag_posix[] = {
2299 0x93, 0xAD, 0x25, 0x50, 0x9C,
2300 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
2301 0xDE, 0x96, 0x8B, 0xCD, 0x7C
2302 };
b8c32dbb 2303
89a5bfa3 2304 *oplock = 0;
af1689a9
PA
2305
2306 off = le32_to_cpu(rsp->CreateContextsOffset);
2307 rem = le32_to_cpu(rsp->CreateContextsLength);
2308 if (check_add_overflow(off, rem, &len) || len > rsp_iov->iov_len)
2309 return -EINVAL;
2310 cc = (struct create_context *)((u8 *)rsp + off);
89a5bfa3
SF
2311
2312 /* Initialize inode number to 0 in case no valid data in qfid context */
2313 if (buf)
2314 buf->IndexNumber = 0;
2315
af1689a9
PA
2316 while (rem >= sizeof(*cc)) {
2317 doff = le16_to_cpu(cc->DataOffset);
2318 dlen = le32_to_cpu(cc->DataLength);
2319 if (check_add_overflow(doff, dlen, &len) || len > rem)
2320 return -EINVAL;
2321
2322 noff = le16_to_cpu(cc->NameOffset);
2323 nlen = le16_to_cpu(cc->NameLength);
76025cc2 2324 if (noff + nlen > doff)
af1689a9
PA
2325 return -EINVAL;
2326
2327 name = (char *)cc + noff;
2328 switch (nlen) {
2329 case 4:
2330 if (!strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4)) {
2331 *oplock = server->ops->parse_lease_buf(cc, epoch,
2332 lease_key);
2333 } else if (buf &&
2334 !strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4)) {
2335 parse_query_id_ctxt(cc, buf);
2336 }
2337 break;
2338 case 16:
2339 if (posix && !memcmp(name, smb3_create_tag_posix, 16))
69dda305 2340 parse_posix_ctxt(cc, buf, posix);
af1689a9
PA
2341 break;
2342 default:
2343 cifs_dbg(FYI, "%s: unhandled context (nlen=%zu dlen=%zu)\n",
2344 __func__, nlen, dlen);
2345 if (IS_ENABLED(CONFIG_CIFS_DEBUG2))
2346 cifs_dump_mem("context data: ", cc, dlen);
2347 break;
ab3459d8 2348 }
af1689a9
PA
2349
2350 off = le32_to_cpu(cc->Next);
2351 if (!off)
deb7deff 2352 break;
af1689a9
PA
2353 if (check_sub_overflow(rem, off, &rem))
2354 return -EINVAL;
2355 cc = (struct create_context *)((u8 *)cc + off);
deb7deff 2356 }
b8c32dbb 2357
89a5bfa3
SF
2358 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2359 *oplock = rsp->OplockLevel;
2360
af1689a9 2361 return 0;
b8c32dbb
PS
2362}
2363
d22cbfec 2364static int
919e57c3
VL
2365add_lease_context(struct TCP_Server_Info *server,
2366 struct smb2_create_req *req,
2367 struct kvec *iov,
729c0c9d 2368 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
d22cbfec 2369{
d22cbfec
PS
2370 unsigned int num = *num_iovec;
2371
729c0c9d 2372 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
d22cbfec
PS
2373 if (iov[num].iov_base == NULL)
2374 return -ENOMEM;
a41a28bd 2375 iov[num].iov_len = server->vals->create_lease_size;
d22cbfec 2376 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
d22cbfec
PS
2377 *num_iovec = num + 1;
2378 return 0;
2379}
2380
b56eae4d 2381static struct create_durable_v2 *
ca567eb2 2382create_durable_v2_buf(struct cifs_open_parms *oparms)
b56eae4d 2383{
ca567eb2 2384 struct cifs_fid *pfid = oparms->fid;
b56eae4d
SF
2385 struct create_durable_v2 *buf;
2386
2387 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2388 if (!buf)
2389 return NULL;
2390
2391 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2392 (struct create_durable_v2, dcontext));
2393 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2394 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2395 (struct create_durable_v2, Name));
2396 buf->ccontext.NameLength = cpu_to_le16(4);
2397
ca567eb2
SF
2398 /*
2399 * NB: Handle timeout defaults to 0, which allows server to choose
2400 * (most servers default to 120 seconds) and most clients default to 0.
2401 * This can be overridden at mount ("handletimeout=") if the user wants
2402 * a different persistent (or resilient) handle timeout for all opens
2c75426c 2403 * on a particular SMB3 mount.
ca567eb2
SF
2404 */
2405 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
b56eae4d 2406 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
fa70b87c 2407 generate_random_uuid(buf->dcontext.CreateGuid);
b56eae4d
SF
2408 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2409
2410 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2411 buf->Name[0] = 'D';
2412 buf->Name[1] = 'H';
2413 buf->Name[2] = '2';
2414 buf->Name[3] = 'Q';
2415 return buf;
2416}
2417
2418static struct create_durable_handle_reconnect_v2 *
2419create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2420{
2421 struct create_durable_handle_reconnect_v2 *buf;
2422
2423 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2424 GFP_KERNEL);
2425 if (!buf)
2426 return NULL;
2427
2428 buf->ccontext.DataOffset =
2429 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2430 dcontext));
2431 buf->ccontext.DataLength =
2432 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2433 buf->ccontext.NameOffset =
2434 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2435 Name));
2436 buf->ccontext.NameLength = cpu_to_le16(4);
2437
2438 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2439 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2440 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2441 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2442
2443 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2444 buf->Name[0] = 'D';
2445 buf->Name[1] = 'H';
2446 buf->Name[2] = '2';
2447 buf->Name[3] = 'C';
2448 return buf;
2449}
2450
63eb3def 2451static int
b56eae4d
SF
2452add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2453 struct cifs_open_parms *oparms)
2454{
b56eae4d
SF
2455 unsigned int num = *num_iovec;
2456
ca567eb2 2457 iov[num].iov_base = create_durable_v2_buf(oparms);
b56eae4d
SF
2458 if (iov[num].iov_base == NULL)
2459 return -ENOMEM;
2460 iov[num].iov_len = sizeof(struct create_durable_v2);
b56eae4d
SF
2461 *num_iovec = num + 1;
2462 return 0;
2463}
2464
2465static int
2466add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
9cbc0b73 2467 struct cifs_open_parms *oparms)
63eb3def 2468{
63eb3def
PS
2469 unsigned int num = *num_iovec;
2470
b56eae4d
SF
2471 /* indicate that we don't need to relock the file */
2472 oparms->reconnect = false;
2473
2474 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2475 if (iov[num].iov_base == NULL)
2476 return -ENOMEM;
2477 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
b56eae4d
SF
2478 *num_iovec = num + 1;
2479 return 0;
2480}
2481
2482static int
2483add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2484 struct cifs_open_parms *oparms, bool use_persistent)
2485{
b56eae4d
SF
2486 unsigned int num = *num_iovec;
2487
2488 if (use_persistent) {
2489 if (oparms->reconnect)
2490 return add_durable_reconnect_v2_context(iov, num_iovec,
2491 oparms);
2492 else
2493 return add_durable_v2_context(iov, num_iovec, oparms);
2494 }
2495
9cbc0b73
PS
2496 if (oparms->reconnect) {
2497 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2498 /* indicate that we don't need to relock the file */
2499 oparms->reconnect = false;
2500 } else
2501 iov[num].iov_base = create_durable_buf();
63eb3def
PS
2502 if (iov[num].iov_base == NULL)
2503 return -ENOMEM;
2504 iov[num].iov_len = sizeof(struct create_durable);
63eb3def
PS
2505 *num_iovec = num + 1;
2506 return 0;
2507}
2508
cdeaf9d0
SF
2509/* See MS-SMB2 2.2.13.2.7 */
2510static struct crt_twarp_ctxt *
2511create_twarp_buf(__u64 timewarp)
2512{
2513 struct crt_twarp_ctxt *buf;
2514
2515 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2516 if (!buf)
2517 return NULL;
2518
2519 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2520 (struct crt_twarp_ctxt, Timestamp));
2521 buf->ccontext.DataLength = cpu_to_le32(8);
2522 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2523 (struct crt_twarp_ctxt, Name));
2524 buf->ccontext.NameLength = cpu_to_le16(4);
2525 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2526 buf->Name[0] = 'T';
2527 buf->Name[1] = 'W';
2528 buf->Name[2] = 'r';
2529 buf->Name[3] = 'p';
2530 buf->Timestamp = cpu_to_le64(timewarp);
2531 return buf;
2532}
2533
2534/* See MS-SMB2 2.2.13.2.7 */
2535static int
2536add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2537{
cdeaf9d0
SF
2538 unsigned int num = *num_iovec;
2539
2540 iov[num].iov_base = create_twarp_buf(timewarp);
2541 if (iov[num].iov_base == NULL)
2542 return -ENOMEM;
2543 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
cdeaf9d0
SF
2544 *num_iovec = num + 1;
2545 return 0;
2546}
2547
2c75426c 2548/* See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */
975221ec
SF
2549static void setup_owner_group_sids(char *buf)
2550{
2551 struct owner_group_sids *sids = (struct owner_group_sids *)buf;
2552
2553 /* Populate the user ownership fields S-1-5-88-1 */
2554 sids->owner.Revision = 1;
2555 sids->owner.NumAuth = 3;
2556 sids->owner.Authority[5] = 5;
2557 sids->owner.SubAuthorities[0] = cpu_to_le32(88);
2558 sids->owner.SubAuthorities[1] = cpu_to_le32(1);
2559 sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val);
2560
2561 /* Populate the group ownership fields S-1-5-88-2 */
2562 sids->group.Revision = 1;
2563 sids->group.NumAuth = 3;
2564 sids->group.Authority[5] = 5;
2565 sids->group.SubAuthorities[0] = cpu_to_le32(88);
2566 sids->group.SubAuthorities[1] = cpu_to_le32(2);
2567 sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val);
a7a519a4
SF
2568
2569 cifs_dbg(FYI, "owner S-1-5-88-1-%d, group S-1-5-88-2-%d\n", current_fsuid().val, current_fsgid().val);
975221ec
SF
2570}
2571
fdef665b
SF
2572/* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2573static struct crt_sd_ctxt *
975221ec 2574create_sd_buf(umode_t mode, bool set_owner, unsigned int *len)
fdef665b
SF
2575{
2576 struct crt_sd_ctxt *buf;
ea64370b
RS
2577 __u8 *ptr, *aclptr;
2578 unsigned int acelen, acl_size, ace_count;
975221ec
SF
2579 unsigned int owner_offset = 0;
2580 unsigned int group_offset = 0;
f09bd695 2581 struct smb3_acl acl = {};
975221ec 2582
d7173623 2583 *len = round_up(sizeof(struct crt_sd_ctxt) + (sizeof(struct cifs_ace) * 4), 8);
975221ec
SF
2584
2585 if (set_owner) {
975221ec
SF
2586 /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */
2587 *len += sizeof(struct owner_group_sids);
2588 }
fdef665b 2589
fdef665b
SF
2590 buf = kzalloc(*len, GFP_KERNEL);
2591 if (buf == NULL)
2592 return buf;
2593
ea64370b 2594 ptr = (__u8 *)&buf[1];
975221ec 2595 if (set_owner) {
ea64370b
RS
2596 /* offset fields are from beginning of security descriptor not of create context */
2597 owner_offset = ptr - (__u8 *)&buf->sd;
975221ec 2598 buf->sd.OffsetOwner = cpu_to_le32(owner_offset);
ea64370b 2599 group_offset = owner_offset + offsetof(struct owner_group_sids, group);
975221ec 2600 buf->sd.OffsetGroup = cpu_to_le32(group_offset);
ea64370b
RS
2601
2602 setup_owner_group_sids(ptr);
2603 ptr += sizeof(struct owner_group_sids);
975221ec
SF
2604 } else {
2605 buf->sd.OffsetOwner = 0;
2606 buf->sd.OffsetGroup = 0;
2607 }
2608
ea64370b 2609 buf->ccontext.DataOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, sd));
975221ec 2610 buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name));
fdef665b
SF
2611 buf->ccontext.NameLength = cpu_to_le16(4);
2612 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2613 buf->Name[0] = 'S';
2614 buf->Name[1] = 'e';
2615 buf->Name[2] = 'c';
2616 buf->Name[3] = 'D';
2617 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
ea64370b 2618
fdef665b
SF
2619 /*
2620 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2621 * and "DP" ie the DACL is present
2622 */
2623 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2624
2625 /* offset owner, group and Sbz1 and SACL are all zero */
ea64370b
RS
2626 buf->sd.OffsetDacl = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2627 /* Ship the ACL for now. we will copy it into buf later. */
2628 aclptr = ptr;
b06d893e 2629 ptr += sizeof(struct smb3_acl);
fdef665b
SF
2630
2631 /* create one ACE to hold the mode embedded in reserved special SID */
ea64370b
RS
2632 acelen = setup_special_mode_ACE((struct cifs_ace *)ptr, (__u64)mode);
2633 ptr += acelen;
2634 acl_size = acelen + sizeof(struct smb3_acl);
2635 ace_count = 1;
975221ec
SF
2636
2637 if (set_owner) {
2638 /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */
ea64370b
RS
2639 acelen = setup_special_user_owner_ACE((struct cifs_ace *)ptr);
2640 ptr += acelen;
2641 acl_size += acelen;
2642 ace_count += 1;
2643 }
975221ec 2644
643fbcee 2645 /* and one more ACE to allow access for authenticated users */
ea64370b
RS
2646 acelen = setup_authusers_ACE((struct cifs_ace *)ptr);
2647 ptr += acelen;
2648 acl_size += acelen;
2649 ace_count += 1;
2650
2651 acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2652 acl.AclSize = cpu_to_le16(acl_size);
2653 acl.AceCount = cpu_to_le16(ace_count);
f09bd695 2654 /* acl.Sbz1 and Sbz2 MBZ so are not set here, but initialized above */
b06d893e 2655 memcpy(aclptr, &acl, sizeof(struct smb3_acl));
ea64370b
RS
2656
2657 buf->ccontext.DataLength = cpu_to_le32(ptr - (__u8 *)&buf->sd);
d7173623 2658 *len = round_up((unsigned int)(ptr - (__u8 *)buf), 8);
975221ec 2659
fdef665b
SF
2660 return buf;
2661}
2662
2663static int
975221ec 2664add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner)
fdef665b 2665{
fdef665b
SF
2666 unsigned int num = *num_iovec;
2667 unsigned int len = 0;
2668
975221ec 2669 iov[num].iov_base = create_sd_buf(mode, set_owner, &len);
fdef665b
SF
2670 if (iov[num].iov_base == NULL)
2671 return -ENOMEM;
2672 iov[num].iov_len = len;
fdef665b
SF
2673 *num_iovec = num + 1;
2674 return 0;
2675}
2676
ff2a09e9
SF
2677static struct crt_query_id_ctxt *
2678create_query_id_buf(void)
2679{
2680 struct crt_query_id_ctxt *buf;
2681
2682 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2683 if (!buf)
2684 return NULL;
2685
2686 buf->ccontext.DataOffset = cpu_to_le16(0);
2687 buf->ccontext.DataLength = cpu_to_le32(0);
2688 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2689 (struct crt_query_id_ctxt, Name));
2690 buf->ccontext.NameLength = cpu_to_le16(4);
2691 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2692 buf->Name[0] = 'Q';
2693 buf->Name[1] = 'F';
2694 buf->Name[2] = 'i';
2695 buf->Name[3] = 'd';
2696 return buf;
2697}
2698
2699/* See MS-SMB2 2.2.13.2.9 */
2700static int
2701add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2702{
ff2a09e9
SF
2703 unsigned int num = *num_iovec;
2704
2705 iov[num].iov_base = create_query_id_buf();
2706 if (iov[num].iov_base == NULL)
2707 return -ENOMEM;
2708 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
ff2a09e9
SF
2709 *num_iovec = num + 1;
2710 return 0;
2711}
2712
f0712928
AA
2713static int
2714alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2715 const char *treename, const __le16 *path)
2716{
2717 int treename_len, path_len;
2718 struct nls_table *cp;
2719 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2720
2721 /*
2722 * skip leading "\\"
2723 */
2724 treename_len = strlen(treename);
2725 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2726 return -EINVAL;
2727
2728 treename += 2;
2729 treename_len -= 2;
2730
2731 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2732
a1d2eb51
PA
2733 /* make room for one path separator only if @path isn't empty */
2734 *out_len = treename_len + (path[0] ? 1 : 0) + path_len;
f0712928
AA
2735
2736 /*
a1d2eb51
PA
2737 * final path needs to be 8-byte aligned as specified in
2738 * MS-SMB2 2.2.13 SMB2 CREATE Request.
f0712928 2739 */
d7173623 2740 *out_size = round_up(*out_len * sizeof(__le16), 8);
a1d2eb51 2741 *out_path = kzalloc(*out_size + sizeof(__le16) /* null */, GFP_KERNEL);
f0712928
AA
2742 if (!*out_path)
2743 return -ENOMEM;
2744
2745 cp = load_nls_default();
2746 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
7eacba3b
EK
2747
2748 /* Do not append the separator if the path is empty */
2749 if (path[0] != cpu_to_le16(0x0000)) {
de548452
DDAG
2750 UniStrcat((wchar_t *)*out_path, (wchar_t *)sep);
2751 UniStrcat((wchar_t *)*out_path, (wchar_t *)path);
7eacba3b
EK
2752 }
2753
f0712928
AA
2754 unload_nls(cp);
2755
2756 return 0;
2757}
2758
bea851b8
SF
2759int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2760 umode_t mode, struct cifs_tcon *tcon,
2761 const char *full_path,
2762 struct cifs_sb_info *cifs_sb)
2763{
2764 struct smb_rqst rqst;
2765 struct smb2_create_req *req;
256b4c3f 2766 struct smb2_create_rsp *rsp = NULL;
bea851b8
SF
2767 struct cifs_ses *ses = tcon->ses;
2768 struct kvec iov[3]; /* make sure at least one for each open context */
2769 struct kvec rsp_iov = {NULL, 0};
2770 int resp_buftype;
2771 int uni_path_len;
2772 __le16 *copy_path = NULL;
2773 int copy_size;
2774 int rc = 0;
2775 unsigned int n_iov = 2;
2776 __u32 file_attributes = 0;
2777 char *pc_buf = NULL;
2778 int flags = 0;
2779 unsigned int total_len;
256b4c3f 2780 __le16 *utf16_path = NULL;
4f1fffa2
SP
2781 struct TCP_Server_Info *server;
2782 int retries = 0, cur_sleep = 1;
2783
2784replay_again:
2785 /* reinitialize for possible replay */
2786 flags = 0;
2787 n_iov = 2;
2788 server = cifs_pick_channel(ses);
bea851b8
SF
2789
2790 cifs_dbg(FYI, "mkdir\n");
2791
256b4c3f
AA
2792 /* resource #1: path allocation */
2793 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2794 if (!utf16_path)
2795 return -ENOMEM;
2796
352d96f3 2797 if (!ses || !server) {
256b4c3f
AA
2798 rc = -EIO;
2799 goto err_free_path;
2800 }
bea851b8 2801
256b4c3f 2802 /* resource #2: request */
352d96f3
AA
2803 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2804 (void **) &req, &total_len);
bea851b8 2805 if (rc)
256b4c3f
AA
2806 goto err_free_path;
2807
bea851b8
SF
2808
2809 if (smb3_encryption_required(tcon))
2810 flags |= CIFS_TRANSFORM_REQ;
2811
bea851b8
SF
2812 req->ImpersonationLevel = IL_IMPERSONATION;
2813 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2814 /* File attributes ignored on open (used in create though) */
2815 req->FileAttributes = cpu_to_le32(file_attributes);
2816 req->ShareAccess = FILE_SHARE_ALL_LE;
2817 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2818 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2819
2820 iov[0].iov_base = (char *)req;
2821 /* -1 since last byte is buf[0] which is sent below (path) */
2822 iov[0].iov_len = total_len - 1;
2823
2824 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2825
2826 /* [MS-SMB2] 2.2.13 NameOffset:
2827 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2828 * the SMB2 header, the file name includes a prefix that will
2829 * be processed during DFS name normalization as specified in
2830 * section 3.3.5.9. Otherwise, the file name is relative to
2831 * the share that is identified by the TreeId in the SMB2
2832 * header.
2833 */
2834 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2835 int name_len;
2836
0d35e382 2837 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
bea851b8
SF
2838 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2839 &name_len,
68e14569 2840 tcon->tree_name, utf16_path);
256b4c3f
AA
2841 if (rc)
2842 goto err_free_req;
2843
bea851b8
SF
2844 req->NameLength = cpu_to_le16(name_len * 2);
2845 uni_path_len = copy_size;
256b4c3f
AA
2846 /* free before overwriting resource */
2847 kfree(utf16_path);
2848 utf16_path = copy_path;
bea851b8 2849 } else {
256b4c3f 2850 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
bea851b8
SF
2851 /* MUST set path len (NameLength) to 0 opening root of share */
2852 req->NameLength = cpu_to_le16(uni_path_len - 2);
2853 if (uni_path_len % 8 != 0) {
2854 copy_size = roundup(uni_path_len, 8);
2855 copy_path = kzalloc(copy_size, GFP_KERNEL);
2856 if (!copy_path) {
256b4c3f
AA
2857 rc = -ENOMEM;
2858 goto err_free_req;
bea851b8 2859 }
256b4c3f 2860 memcpy((char *)copy_path, (const char *)utf16_path,
bea851b8
SF
2861 uni_path_len);
2862 uni_path_len = copy_size;
256b4c3f
AA
2863 /* free before overwriting resource */
2864 kfree(utf16_path);
2865 utf16_path = copy_path;
bea851b8
SF
2866 }
2867 }
2868
2869 iov[1].iov_len = uni_path_len;
256b4c3f 2870 iov[1].iov_base = utf16_path;
bea851b8
SF
2871 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2872
2873 if (tcon->posix_extensions) {
256b4c3f 2874 /* resource #3: posix buf */
bea851b8 2875 rc = add_posix_context(iov, &n_iov, mode);
256b4c3f
AA
2876 if (rc)
2877 goto err_free_req;
5ec629e0
VL
2878 req->CreateContextsOffset = cpu_to_le32(
2879 sizeof(struct smb2_create_req) +
2880 iov[1].iov_len);
bea851b8
SF
2881 pc_buf = iov[n_iov-1].iov_base;
2882 }
2883
2884
2885 memset(&rqst, 0, sizeof(struct smb_rqst));
2886 rqst.rq_iov = iov;
2887 rqst.rq_nvec = n_iov;
2888
d2f15428 2889 /* no need to inc num_remote_opens because we close it just below */
fddc6ccc 2890 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, full_path, CREATE_NOT_FILE,
efe2e9f3 2891 FILE_WRITE_ATTRIBUTES);
4f1fffa2
SP
2892
2893 if (retries)
2894 smb2_set_replay(server, &rqst);
2895
256b4c3f 2896 /* resource #4: response buffer */
352d96f3
AA
2897 rc = cifs_send_recv(xid, ses, server,
2898 &rqst, &resp_buftype, flags, &rsp_iov);
256b4c3f 2899 if (rc) {
bea851b8
SF
2900 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2901 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
256b4c3f
AA
2902 CREATE_NOT_FILE,
2903 FILE_WRITE_ATTRIBUTES, rc);
2904 goto err_free_rsp_buf;
2905 }
2906
ca780da5
SF
2907 /*
2908 * Although unlikely to be possible for rsp to be null and rc not set,
2909 * adding check below is slightly safer long term (and quiets Coverity
2910 * warning)
2911 */
256b4c3f 2912 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
ca780da5
SF
2913 if (rsp == NULL) {
2914 rc = -EIO;
2915 kfree(pc_buf);
2916 goto err_free_req;
2917 }
2918
351a59da
PA
2919 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
2920 CREATE_NOT_FILE, FILE_WRITE_ATTRIBUTES);
bea851b8 2921
351a59da 2922 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
bea851b8
SF
2923
2924 /* Eventually save off posix specific response info and timestaps */
2925
256b4c3f 2926err_free_rsp_buf:
bea851b8 2927 free_rsp_buf(resp_buftype, rsp);
256b4c3f
AA
2928 kfree(pc_buf);
2929err_free_req:
2930 cifs_small_buf_release(req);
2931err_free_path:
2932 kfree(utf16_path);
4f1fffa2
SP
2933
2934 if (is_replayable_error(rc) &&
2935 smb2_should_replay(tcon, &retries, &cur_sleep))
2936 goto replay_again;
2937
bea851b8 2938 return rc;
bea851b8 2939}
bea851b8 2940
2503a0db 2941int
352d96f3
AA
2942SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2943 struct smb_rqst *rqst, __u8 *oplock,
1eb9fb52 2944 struct cifs_open_parms *oparms, __le16 *path)
2503a0db
PS
2945{
2946 struct smb2_create_req *req;
da502f7d 2947 unsigned int n_iov = 2;
ca81983f 2948 __u32 file_attributes = 0;
1eb9fb52
RS
2949 int copy_size;
2950 int uni_path_len;
4f33bc35 2951 unsigned int total_len;
1eb9fb52
RS
2952 struct kvec *iov = rqst->rq_iov;
2953 __le16 *copy_path;
2954 int rc;
2503a0db 2955
352d96f3
AA
2956 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2957 (void **) &req, &total_len);
2503a0db
PS
2958 if (rc)
2959 return rc;
2960
1eb9fb52
RS
2961 iov[0].iov_base = (char *)req;
2962 /* -1 since last byte is buf[0] which is sent below (path) */
2963 iov[0].iov_len = total_len - 1;
7fb8986e 2964
064f6047 2965 if (oparms->create_options & CREATE_OPTION_READONLY)
ca81983f 2966 file_attributes |= ATTR_READONLY;
db8b631d
SF
2967 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2968 file_attributes |= ATTR_SYSTEM;
ca81983f 2969
2503a0db 2970 req->ImpersonationLevel = IL_IMPERSONATION;
064f6047 2971 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2503a0db
PS
2972 /* File attributes ignored on open (used in create though) */
2973 req->FileAttributes = cpu_to_le32(file_attributes);
2974 req->ShareAccess = FILE_SHARE_ALL_LE;
c3ca78e2 2975
064f6047
PS
2976 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2977 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
4f33bc35 2978 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
f0712928
AA
2979
2980 /* [MS-SMB2] 2.2.13 NameOffset:
2981 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2982 * the SMB2 header, the file name includes a prefix that will
2983 * be processed during DFS name normalization as specified in
2984 * section 3.3.5.9. Otherwise, the file name is relative to
2985 * the share that is identified by the TreeId in the SMB2
2986 * header.
2987 */
2988 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2989 int name_len;
2990
0d35e382 2991 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
f0712928
AA
2992 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2993 &name_len,
68e14569 2994 tcon->tree_name, path);
1eb9fb52 2995 if (rc)
f0712928
AA
2996 return rc;
2997 req->NameLength = cpu_to_le16(name_len * 2);
59aa3718
PS
2998 uni_path_len = copy_size;
2999 path = copy_path;
f0712928
AA
3000 } else {
3001 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
3002 /* MUST set path len (NameLength) to 0 opening root of share */
3003 req->NameLength = cpu_to_le16(uni_path_len - 2);
d7173623 3004 copy_size = round_up(uni_path_len, 8);
1eb9fb52
RS
3005 copy_path = kzalloc(copy_size, GFP_KERNEL);
3006 if (!copy_path)
3007 return -ENOMEM;
3008 memcpy((char *)copy_path, (const char *)path,
3009 uni_path_len);
3010 uni_path_len = copy_size;
3011 path = copy_path;
2503a0db
PS
3012 }
3013
59aa3718
PS
3014 iov[1].iov_len = uni_path_len;
3015 iov[1].iov_base = path;
59aa3718 3016
3e7a02d4 3017 if ((!server->oplocks) || (tcon->no_lease))
b8c32dbb
PS
3018 *oplock = SMB2_OPLOCK_LEVEL_NONE;
3019
a41a28bd 3020 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
b8c32dbb
PS
3021 *oplock == SMB2_OPLOCK_LEVEL_NONE)
3022 req->RequestedOplockLevel = *oplock;
f8015683
SF
3023 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
3024 (oparms->create_options & CREATE_NOT_FILE))
3025 req->RequestedOplockLevel = *oplock; /* no srv lease support */
b8c32dbb 3026 else {
919e57c3 3027 rc = add_lease_context(server, req, iov, &n_iov,
729c0c9d 3028 oparms->fid->lease_key, oplock);
1eb9fb52 3029 if (rc)
d22cbfec 3030 return rc;
b8c32dbb
PS
3031 }
3032
63eb3def 3033 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
da502f7d 3034 rc = add_durable_context(iov, &n_iov, oparms,
b56eae4d 3035 tcon->use_persistent);
1eb9fb52 3036 if (rc)
63eb3def 3037 return rc;
63eb3def
PS
3038 }
3039
ce558b0e 3040 if (tcon->posix_extensions) {
ce558b0e 3041 rc = add_posix_context(iov, &n_iov, oparms->mode);
1eb9fb52 3042 if (rc)
ce558b0e 3043 return rc;
ce558b0e 3044 }
ce558b0e 3045
cdeaf9d0
SF
3046 if (tcon->snapshot_time) {
3047 cifs_dbg(FYI, "adding snapshot context\n");
cdeaf9d0
SF
3048 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
3049 if (rc)
3050 return rc;
3051 }
3052
975221ec
SF
3053 if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) {
3054 bool set_mode;
3055 bool set_owner;
3056
3057 if ((oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
3058 (oparms->mode != ACL_NO_MODE))
3059 set_mode = true;
3060 else {
3061 set_mode = false;
3062 oparms->mode = ACL_NO_MODE;
c3ca78e2
SF
3063 }
3064
975221ec
SF
3065 if (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
3066 set_owner = true;
3067 else
3068 set_owner = false;
3069
3070 if (set_owner | set_mode) {
975221ec
SF
3071 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
3072 rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner);
3073 if (rc)
3074 return rc;
3075 }
c3ca78e2
SF
3076 }
3077
ff2a09e9 3078 add_query_id_context(iov, &n_iov);
cdeaf9d0 3079
5ec629e0
VL
3080 if (n_iov > 2) {
3081 /*
3082 * We have create contexts behind iov[1] (the file
3083 * name), point at them from the main create request
3084 */
3085 req->CreateContextsOffset = cpu_to_le32(
3086 sizeof(struct smb2_create_req) +
3087 iov[1].iov_len);
d2ec43b5 3088 req->CreateContextsLength = 0;
2a8d1387
VL
3089
3090 for (unsigned int i = 2; i < (n_iov-1); i++) {
3091 struct kvec *v = &iov[i];
3092 size_t len = v->iov_len;
3093 struct create_context *cctx =
3094 (struct create_context *)v->iov_base;
3095
3096 cctx->Next = cpu_to_le32(len);
d2ec43b5 3097 le32_add_cpu(&req->CreateContextsLength, len);
2a8d1387 3098 }
d2ec43b5
VL
3099 le32_add_cpu(&req->CreateContextsLength,
3100 iov[n_iov-1].iov_len);
5ec629e0
VL
3101 }
3102
1eb9fb52
RS
3103 rqst->rq_nvec = n_iov;
3104 return 0;
3105}
3106
3107/* rq_iov[0] is the request and is released by cifs_small_buf_release().
3108 * All other vectors are freed by kfree().
3109 */
3110void
3111SMB2_open_free(struct smb_rqst *rqst)
3112{
3113 int i;
3114
32a1fb36
RS
3115 if (rqst && rqst->rq_iov) {
3116 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
3117 for (i = 1; i < rqst->rq_nvec; i++)
3118 if (rqst->rq_iov[i].iov_base != smb2_padding)
3119 kfree(rqst->rq_iov[i].iov_base);
3120 }
1eb9fb52
RS
3121}
3122
3123int
3124SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
3125 __u8 *oplock, struct smb2_file_all_info *buf,
69dda305 3126 struct create_posix_rsp *posix,
1eb9fb52
RS
3127 struct kvec *err_iov, int *buftype)
3128{
3129 struct smb_rqst rqst;
3130 struct smb2_create_rsp *rsp = NULL;
1eb9fb52
RS
3131 struct cifs_tcon *tcon = oparms->tcon;
3132 struct cifs_ses *ses = tcon->ses;
4f1fffa2 3133 struct TCP_Server_Info *server;
4d8dfafc 3134 struct kvec iov[SMB2_CREATE_IOV_SIZE];
1eb9fb52 3135 struct kvec rsp_iov = {NULL, 0};
ef2298a0 3136 int resp_buftype = CIFS_NO_BUFFER;
1eb9fb52
RS
3137 int rc = 0;
3138 int flags = 0;
4f1fffa2
SP
3139 int retries = 0, cur_sleep = 1;
3140
3141replay_again:
3142 /* reinitialize for possible replay */
3143 flags = 0;
3144 server = cifs_pick_channel(ses);
1eb9fb52
RS
3145
3146 cifs_dbg(FYI, "create/open\n");
352d96f3 3147 if (!ses || !server)
1eb9fb52
RS
3148 return -EIO;
3149
3150 if (smb3_encryption_required(tcon))
3151 flags |= CIFS_TRANSFORM_REQ;
3152
40eff45b 3153 memset(&rqst, 0, sizeof(struct smb_rqst));
1eb9fb52 3154 memset(&iov, 0, sizeof(iov));
40eff45b 3155 rqst.rq_iov = iov;
4d8dfafc 3156 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
1eb9fb52 3157
352d96f3
AA
3158 rc = SMB2_open_init(tcon, server,
3159 &rqst, oplock, oparms, path);
1eb9fb52
RS
3160 if (rc)
3161 goto creat_exit;
40eff45b 3162
fddc6ccc 3163 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid, oparms->path,
efe2e9f3
SF
3164 oparms->create_options, oparms->desired_access);
3165
4f1fffa2
SP
3166 if (retries)
3167 smb2_set_replay(server, &rqst);
3168
352d96f3
AA
3169 rc = cifs_send_recv(xid, ses, server,
3170 &rqst, &resp_buftype, flags,
4f33bc35 3171 &rsp_iov);
da502f7d 3172 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2503a0db
PS
3173
3174 if (rc != 0) {
3175 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
91cb74f5
RS
3176 if (err_iov && rsp) {
3177 *err_iov = rsp_iov;
9d874c36 3178 *buftype = resp_buftype;
91cb74f5
RS
3179 resp_buftype = CIFS_NO_BUFFER;
3180 rsp = NULL;
3181 }
28d59363
SF
3182 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
3183 oparms->create_options, oparms->desired_access, rc);
7dcc82c2 3184 if (rc == -EREMCHG) {
a0a3036b 3185 pr_warn_once("server share %s deleted\n",
68e14569 3186 tcon->tree_name);
7dcc82c2
SF
3187 tcon->need_reconnect = true;
3188 }
2503a0db 3189 goto creat_exit;
6b789518
SF
3190 } else if (rsp == NULL) /* unlikely to happen, but safer to check */
3191 goto creat_exit;
3192 else
351a59da
PA
3193 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
3194 oparms->create_options, oparms->desired_access);
2503a0db 3195
fae8044c 3196 atomic_inc(&tcon->num_remote_opens);
351a59da
PA
3197 oparms->fid->persistent_fid = rsp->PersistentFileId;
3198 oparms->fid->volatile_fid = rsp->VolatileFileId;
86f740f2 3199 oparms->fid->access = oparms->desired_access;
dfe33f9a 3200#ifdef CONFIG_CIFS_DEBUG2
0d35e382 3201 oparms->fid->mid = le64_to_cpu(rsp->hdr.MessageId);
dfe33f9a 3202#endif /* CIFS_DEBUG2 */
f0df737e
PS
3203
3204 if (buf) {
fbcff33d
KC
3205 buf->CreationTime = rsp->CreationTime;
3206 buf->LastAccessTime = rsp->LastAccessTime;
3207 buf->LastWriteTime = rsp->LastWriteTime;
3208 buf->ChangeTime = rsp->ChangeTime;
f0df737e
PS
3209 buf->AllocationSize = rsp->AllocationSize;
3210 buf->EndOfFile = rsp->EndofFile;
3211 buf->Attributes = rsp->FileAttributes;
3212 buf->NumberOfLinks = cpu_to_le32(1);
3213 buf->DeletePending = 0;
3214 }
2e44b288 3215
89a5bfa3 3216
af1689a9
PA
3217 rc = smb2_parse_contexts(server, &rsp_iov, &oparms->fid->epoch,
3218 oparms->fid->lease_key, oplock, buf, posix);
2503a0db 3219creat_exit:
1eb9fb52 3220 SMB2_open_free(&rqst);
2503a0db 3221 free_rsp_buf(resp_buftype, rsp);
4f1fffa2
SP
3222
3223 if (is_replayable_error(rc) &&
3224 smb2_should_replay(tcon, &retries, &cur_sleep))
3225 goto replay_again;
3226
2503a0db
PS
3227 return rc;
3228}
3229
4a72dafa 3230int
352d96f3
AA
3231SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3232 struct smb_rqst *rqst,
ccdc77a3 3233 u64 persistent_fid, u64 volatile_fid, u32 opcode,
400d0ad6 3234 char *in_data, u32 indatalen,
153322f7 3235 __u32 max_response_size)
4a72dafa
SF
3236{
3237 struct smb2_ioctl_req *req;
ccdc77a3 3238 struct kvec *iov = rqst->rq_iov;
97754680 3239 unsigned int total_len;
ccdc77a3 3240 int rc;
2c87d6a9 3241 char *in_data_buf;
4a72dafa 3242
352d96f3
AA
3243 rc = smb2_ioctl_req_init(opcode, tcon, server,
3244 (void **) &req, &total_len);
4a72dafa
SF
3245 if (rc)
3246 return rc;
3247
2c87d6a9
LL
3248 if (indatalen) {
3249 /*
3250 * indatalen is usually small at a couple of bytes max, so
3251 * just allocate through generic pool
3252 */
d81f0974 3253 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
2c87d6a9
LL
3254 if (!in_data_buf) {
3255 cifs_small_buf_release(req);
3256 return -ENOMEM;
3257 }
2c87d6a9
LL
3258 }
3259
4a72dafa
SF
3260 req->CtlCode = cpu_to_le32(opcode);
3261 req->PersistentFileId = persistent_fid;
3262 req->VolatileFileId = volatile_fid;
3263
ccdc77a3
RS
3264 iov[0].iov_base = (char *)req;
3265 /*
3266 * If no input data, the size of ioctl struct in
3267 * protocol spec still includes a 1 byte data buffer,
3268 * but if input data passed to ioctl, we do not
3269 * want to double count this, so we do not send
3270 * the dummy one byte of data in iovec[0] if sending
3271 * input data (in iovec[1]).
3272 */
4a72dafa
SF
3273 if (indatalen) {
3274 req->InputCount = cpu_to_le32(indatalen);
3275 /* do not set InputOffset if no input data */
3276 req->InputOffset =
97754680 3277 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
ccdc77a3
RS
3278 rqst->rq_nvec = 2;
3279 iov[0].iov_len = total_len - 1;
2c87d6a9 3280 iov[1].iov_base = in_data_buf;
4a72dafa 3281 iov[1].iov_len = indatalen;
ccdc77a3
RS
3282 } else {
3283 rqst->rq_nvec = 1;
3284 iov[0].iov_len = total_len;
3285 }
4a72dafa
SF
3286
3287 req->OutputOffset = 0;
3288 req->OutputCount = 0; /* MBZ */
3289
3290 /*
153322f7
SF
3291 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
3292 * We Could increase default MaxOutputResponse, but that could require
3293 * more credits. Windows typically sets this smaller, but for some
4a72dafa
SF
3294 * ioctls it may be useful to allow server to send more. No point
3295 * limiting what the server can send as long as fits in one credit
153322f7
SF
3296 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
3297 * to increase this limit up in the future.
3298 * Note that for snapshot queries that servers like Azure expect that
3299 * the first query be minimal size (and just used to get the number/size
3300 * of previous versions) so response size must be specified as EXACTLY
3301 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
3302 * of eight bytes. Currently that is the only case where we set max
3303 * response size smaller.
4a72dafa 3304 */
153322f7 3305 req->MaxOutputResponse = cpu_to_le32(max_response_size);
0d35e382 3306 req->hdr.CreditCharge =
ebf57440
NJ
3307 cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size),
3308 SMB2_MAX_BUFFER_SIZE));
400d0ad6
EM
3309 /* always an FSCTL (for now) */
3310 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
4a72dafa 3311
4587eee0
SF
3312 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
3313 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
0d35e382 3314 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
4a72dafa 3315
ccdc77a3
RS
3316 return 0;
3317}
3318
3319void
3320SMB2_ioctl_free(struct smb_rqst *rqst)
3321{
6457c20e 3322 int i;
2c75426c 3323
2c87d6a9 3324 if (rqst && rqst->rq_iov) {
ccdc77a3 3325 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
6457c20e
MZ
3326 for (i = 1; i < rqst->rq_nvec; i++)
3327 if (rqst->rq_iov[i].iov_base != smb2_padding)
3328 kfree(rqst->rq_iov[i].iov_base);
2c87d6a9 3329 }
ccdc77a3
RS
3330}
3331
153322f7 3332
ccdc77a3
RS
3333/*
3334 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
3335 */
3336int
3337SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
400d0ad6
EM
3338 u64 volatile_fid, u32 opcode, char *in_data, u32 indatalen,
3339 u32 max_out_data_len, char **out_data,
3340 u32 *plen /* returned data len */)
ccdc77a3
RS
3341{
3342 struct smb_rqst rqst;
3343 struct smb2_ioctl_rsp *rsp = NULL;
3344 struct cifs_ses *ses;
352d96f3 3345 struct TCP_Server_Info *server;
72c419d9 3346 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
ccdc77a3
RS
3347 struct kvec rsp_iov = {NULL, 0};
3348 int resp_buftype = CIFS_NO_BUFFER;
3349 int rc = 0;
3350 int flags = 0;
4f1fffa2 3351 int retries = 0, cur_sleep = 1;
ccdc77a3 3352
352d96f3 3353 if (!tcon)
ccdc77a3
RS
3354 return -EIO;
3355
352d96f3 3356 ses = tcon->ses;
ac6ad7a8
CIK
3357 if (!ses)
3358 return -EIO;
352d96f3 3359
4f1fffa2
SP
3360replay_again:
3361 /* reinitialize for possible replay */
3362 flags = 0;
352d96f3 3363 server = cifs_pick_channel(ses);
4f1fffa2 3364
ac6ad7a8 3365 if (!server)
ccdc77a3
RS
3366 return -EIO;
3367
4f1fffa2
SP
3368 cifs_dbg(FYI, "SMB2 IOCTL\n");
3369
3370 if (out_data != NULL)
3371 *out_data = NULL;
3372
3373 /* zero out returned data len, in case of error */
3374 if (plen)
3375 *plen = 0;
3376
ccdc77a3
RS
3377 if (smb3_encryption_required(tcon))
3378 flags |= CIFS_TRANSFORM_REQ;
3379
40eff45b 3380 memset(&rqst, 0, sizeof(struct smb_rqst));
ccdc77a3 3381 memset(&iov, 0, sizeof(iov));
40eff45b 3382 rqst.rq_iov = iov;
72c419d9 3383 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
ccdc77a3 3384
352d96f3
AA
3385 rc = SMB2_ioctl_init(tcon, server,
3386 &rqst, persistent_fid, volatile_fid, opcode,
400d0ad6 3387 in_data, indatalen, max_out_data_len);
ccdc77a3
RS
3388 if (rc)
3389 goto ioctl_exit;
40eff45b 3390
4f1fffa2
SP
3391 if (retries)
3392 smb2_set_replay(server, &rqst);
3393
352d96f3
AA
3394 rc = cifs_send_recv(xid, ses, server,
3395 &rqst, &resp_buftype, flags,
97754680 3396 &rsp_iov);
da502f7d 3397 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
4a72dafa 3398
eccb4422
SF
3399 if (rc != 0)
3400 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
3401 ses->Suid, 0, opcode, rc);
3402
2f3ebaba 3403 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
8e353106 3404 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
4a72dafa 3405 goto ioctl_exit;
9bf0c9cd
SF
3406 } else if (rc == -EINVAL) {
3407 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3408 (opcode != FSCTL_SRV_COPYCHUNK)) {
8e353106 3409 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
9bf0c9cd
SF
3410 goto ioctl_exit;
3411 }
2f3ebaba
RS
3412 } else if (rc == -E2BIG) {
3413 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3414 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3415 goto ioctl_exit;
3416 }
4a72dafa
SF
3417 }
3418
3419 /* check if caller wants to look at return data or just return rc */
3420 if ((plen == NULL) || (out_data == NULL))
3421 goto ioctl_exit;
3422
4d9beec2
SF
3423 /*
3424 * Although unlikely to be possible for rsp to be null and rc not set,
3425 * adding check below is slightly safer long term (and quiets Coverity
3426 * warning)
3427 */
3428 if (rsp == NULL) {
3429 rc = -EIO;
3430 goto ioctl_exit;
3431 }
3432
4a72dafa
SF
3433 *plen = le32_to_cpu(rsp->OutputCount);
3434
3435 /* We check for obvious errors in the output buffer length and offset */
3436 if (*plen == 0)
3437 goto ioctl_exit; /* server returned no data */
2d204ee9 3438 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
3175eb9b 3439 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
4a72dafa
SF
3440 *plen = 0;
3441 rc = -EIO;
3442 goto ioctl_exit;
3443 }
3444
2d204ee9 3445 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
3175eb9b 3446 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
4a72dafa
SF
3447 le32_to_cpu(rsp->OutputOffset));
3448 *plen = 0;
3449 rc = -EIO;
3450 goto ioctl_exit;
3451 }
3452
d034feeb
Y
3453 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3454 *plen, GFP_KERNEL);
4a72dafa
SF
3455 if (*out_data == NULL) {
3456 rc = -ENOMEM;
3457 goto ioctl_exit;
3458 }
3459
4a72dafa 3460ioctl_exit:
ccdc77a3 3461 SMB2_ioctl_free(&rqst);
4a72dafa 3462 free_rsp_buf(resp_buftype, rsp);
4f1fffa2
SP
3463
3464 if (is_replayable_error(rc) &&
3465 smb2_should_replay(tcon, &retries, &cur_sleep))
3466 goto replay_again;
3467
4a72dafa
SF
3468 return rc;
3469}
3470
64a5cfa6
SF
3471/*
3472 * Individual callers to ioctl worker function follow
3473 */
3474
3475int
3476SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3477 u64 persistent_fid, u64 volatile_fid)
3478{
3479 int rc;
64a5cfa6
SF
3480 struct compress_ioctl fsctl_input;
3481 char *ret_data = NULL;
3482
3483 fsctl_input.CompressionState =
bc09d141 3484 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
64a5cfa6
SF
3485
3486 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
400d0ad6 3487 FSCTL_SET_COMPRESSION,
64a5cfa6 3488 (char *)&fsctl_input /* data input */,
153322f7
SF
3489 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3490 &ret_data /* out data */, NULL);
64a5cfa6
SF
3491
3492 cifs_dbg(FYI, "set compression rc %d\n", rc);
64a5cfa6
SF
3493
3494 return rc;
3495}
3496
8eb4ecfa 3497int
352d96f3
AA
3498SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3499 struct smb_rqst *rqst,
43f8a6a7 3500 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
8eb4ecfa
RS
3501{
3502 struct smb2_close_req *req;
3503 struct kvec *iov = rqst->rq_iov;
3504 unsigned int total_len;
3505 int rc;
3506
352d96f3
AA
3507 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server,
3508 (void **) &req, &total_len);
8eb4ecfa
RS
3509 if (rc)
3510 return rc;
3511
351a59da
PA
3512 req->PersistentFileId = persistent_fid;
3513 req->VolatileFileId = volatile_fid;
43f8a6a7
SF
3514 if (query_attrs)
3515 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3516 else
3517 req->Flags = 0;
8eb4ecfa
RS
3518 iov[0].iov_base = (char *)req;
3519 iov[0].iov_len = total_len;
3520
3521 return 0;
3522}
3523
3524void
3525SMB2_close_free(struct smb_rqst *rqst)
3526{
32a1fb36
RS
3527 if (rqst && rqst->rq_iov)
3528 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
8eb4ecfa
RS
3529}
3530
2503a0db 3531int
43f8a6a7
SF
3532__SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3533 u64 persistent_fid, u64 volatile_fid,
3534 struct smb2_file_network_open_info *pbuf)
2503a0db 3535{
40eff45b 3536 struct smb_rqst rqst;
8eb4ecfa 3537 struct smb2_close_rsp *rsp = NULL;
2503a0db 3538 struct cifs_ses *ses = tcon->ses;
4f1fffa2 3539 struct TCP_Server_Info *server;
2503a0db 3540 struct kvec iov[1];
da502f7d 3541 struct kvec rsp_iov;
ef2298a0 3542 int resp_buftype = CIFS_NO_BUFFER;
2503a0db 3543 int rc = 0;
9e8fae25 3544 int flags = 0;
43f8a6a7 3545 bool query_attrs = false;
4f1fffa2
SP
3546 int retries = 0, cur_sleep = 1;
3547
3548replay_again:
3549 /* reinitialize for possible replay */
3550 flags = 0;
3551 query_attrs = false;
3552 server = cifs_pick_channel(ses);
2503a0db 3553
f96637be 3554 cifs_dbg(FYI, "Close\n");
2503a0db 3555
352d96f3 3556 if (!ses || !server)
2503a0db
PS
3557 return -EIO;
3558
5a77e75f 3559 if (smb3_encryption_required(tcon))
7fb8986e
PS
3560 flags |= CIFS_TRANSFORM_REQ;
3561
40eff45b 3562 memset(&rqst, 0, sizeof(struct smb_rqst));
8eb4ecfa 3563 memset(&iov, 0, sizeof(iov));
40eff45b
RS
3564 rqst.rq_iov = iov;
3565 rqst.rq_nvec = 1;
3566
43f8a6a7
SF
3567 /* check if need to ask server to return timestamps in close response */
3568 if (pbuf)
3569 query_attrs = true;
3570
f90f9797 3571 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
352d96f3
AA
3572 rc = SMB2_close_init(tcon, server,
3573 &rqst, persistent_fid, volatile_fid,
43f8a6a7 3574 query_attrs);
8eb4ecfa
RS
3575 if (rc)
3576 goto close_exit;
3577
4f1fffa2
SP
3578 if (retries)
3579 smb2_set_replay(server, &rqst);
3580
352d96f3
AA
3581 rc = cifs_send_recv(xid, ses, server,
3582 &rqst, &resp_buftype, flags, &rsp_iov);
da502f7d 3583 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
2503a0db
PS
3584
3585 if (rc != 0) {
d4a029d2 3586 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
eccb4422
SF
3587 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3588 rc);
2503a0db 3589 goto close_exit;
43f8a6a7 3590 } else {
f90f9797
SF
3591 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3592 ses->Suid);
43f8a6a7 3593 if (pbuf)
0015eb6e
DA
3594 memcpy(&pbuf->network_open_info,
3595 &rsp->network_open_info,
3596 sizeof(pbuf->network_open_info));
43f8a6a7 3597 }
2503a0db 3598
fae8044c 3599 atomic_dec(&tcon->num_remote_opens);
2503a0db 3600close_exit:
8eb4ecfa 3601 SMB2_close_free(&rqst);
2503a0db 3602 free_rsp_buf(resp_buftype, rsp);
9150c3ad
PS
3603
3604 /* retry close in a worker thread if this one is interrupted */
2659d3bf 3605 if (is_interrupt_error(rc)) {
9e8fae25
SF
3606 int tmp_rc;
3607
9150c3ad
PS
3608 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3609 volatile_fid);
3610 if (tmp_rc)
3611 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3612 persistent_fid, tmp_rc);
3613 }
4f1fffa2
SP
3614
3615 if (is_replayable_error(rc) &&
3616 smb2_should_replay(tcon, &retries, &cur_sleep))
3617 goto replay_again;
3618
9150c3ad 3619 return rc;
97ca1762
RS
3620}
3621
43f8a6a7
SF
3622int
3623SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3624 u64 persistent_fid, u64 volatile_fid)
3625{
3626 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3627}
3628
730928c8
RS
3629int
3630smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3631 struct kvec *iov, unsigned int min_buf_size)
be4cb9e3 3632{
c1596ff5 3633 unsigned int smb_len = iov->iov_len;
1fc6ad2f
RS
3634 char *end_of_smb = smb_len + (char *)iov->iov_base;
3635 char *begin_of_buf = offset + (char *)iov->iov_base;
be4cb9e3
PS
3636 char *end_of_buf = begin_of_buf + buffer_length;
3637
3638
3639 if (buffer_length < min_buf_size) {
f96637be
JP
3640 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3641 buffer_length, min_buf_size);
be4cb9e3
PS
3642 return -EINVAL;
3643 }
3644
3645 /* check if beyond RFC1001 maximum length */
3646 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
f96637be
JP
3647 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3648 buffer_length, smb_len);
be4cb9e3
PS
3649 return -EINVAL;
3650 }
3651
3652 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
a0a3036b 3653 cifs_dbg(VFS, "Invalid server response, bad offset to data\n");
be4cb9e3
PS
3654 return -EINVAL;
3655 }
3656
3657 return 0;
3658}
3659
3660/*
3661 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3662 * Caller must free buffer.
3663 */
c5a5f38f
RS
3664int
3665smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3666 struct kvec *iov, unsigned int minbufsize,
3667 char *data)
be4cb9e3 3668{
1fc6ad2f 3669 char *begin_of_buf = offset + (char *)iov->iov_base;
be4cb9e3
PS
3670 int rc;
3671
3672 if (!data)
3673 return -EINVAL;
3674
730928c8 3675 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
be4cb9e3
PS
3676 if (rc)
3677 return rc;
3678
9ee2afe5 3679 memcpy(data, begin_of_buf, minbufsize);
be4cb9e3
PS
3680
3681 return 0;
3682}
3683
296ecbae 3684int
352d96f3
AA
3685SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3686 struct smb_rqst *rqst,
296ecbae
RS
3687 u64 persistent_fid, u64 volatile_fid,
3688 u8 info_class, u8 info_type, u32 additional_info,
f5b05d62 3689 size_t output_len, size_t input_len, void *input)
be4cb9e3
PS
3690{
3691 struct smb2_query_info_req *req;
296ecbae 3692 struct kvec *iov = rqst->rq_iov;
b2fb7fec 3693 unsigned int total_len;
33eae65c 3694 size_t len;
296ecbae 3695 int rc;
be4cb9e3 3696
33eae65c
PA
3697 if (unlikely(check_add_overflow(input_len, sizeof(*req), &len) ||
3698 len > CIFSMaxBufSize))
3699 return -EINVAL;
3700
352d96f3
AA
3701 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
3702 (void **) &req, &total_len);
be4cb9e3
PS
3703 if (rc)
3704 return rc;
3705
42c493c1 3706 req->InfoType = info_type;
f0df737e 3707 req->FileInfoClass = info_class;
be4cb9e3
PS
3708 req->PersistentFileId = persistent_fid;
3709 req->VolatileFileId = volatile_fid;
42c493c1 3710 req->AdditionalInformation = cpu_to_le32(additional_info);
48923d2a 3711
f0df737e 3712 req->OutputBufferLength = cpu_to_le32(output_len);
f5b05d62
RS
3713 if (input_len) {
3714 req->InputBufferLength = cpu_to_le32(input_len);
3715 /* total_len for smb query request never close to le16 max */
3716 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3717 memcpy(req->Buffer, input, input_len);
3718 }
be4cb9e3
PS
3719
3720 iov[0].iov_base = (char *)req;
b2fb7fec 3721 /* 1 for Buffer */
33eae65c 3722 iov[0].iov_len = len;
296ecbae
RS
3723 return 0;
3724}
3725
3726void
3727SMB2_query_info_free(struct smb_rqst *rqst)
3728{
32a1fb36 3729 if (rqst && rqst->rq_iov)
33eae65c 3730 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
296ecbae
RS
3731}
3732
3733static int
3734query_info(const unsigned int xid, struct cifs_tcon *tcon,
3735 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3736 u32 additional_info, size_t output_len, size_t min_len, void **data,
3737 u32 *dlen)
3738{
3739 struct smb_rqst rqst;
3740 struct smb2_query_info_rsp *rsp = NULL;
3741 struct kvec iov[1];
3742 struct kvec rsp_iov;
3743 int rc = 0;
ef2298a0 3744 int resp_buftype = CIFS_NO_BUFFER;
296ecbae 3745 struct cifs_ses *ses = tcon->ses;
ac6ad7a8 3746 struct TCP_Server_Info *server;
296ecbae 3747 int flags = 0;
73aaf920 3748 bool allocated = false;
4f1fffa2 3749 int retries = 0, cur_sleep = 1;
296ecbae
RS
3750
3751 cifs_dbg(FYI, "Query Info\n");
3752
ac6ad7a8
CIK
3753 if (!ses)
3754 return -EIO;
4f1fffa2
SP
3755
3756replay_again:
3757 /* reinitialize for possible replay */
3758 flags = 0;
3759 allocated = false;
352d96f3 3760 server = cifs_pick_channel(ses);
4f1fffa2 3761
ac6ad7a8 3762 if (!server)
296ecbae
RS
3763 return -EIO;
3764
3765 if (smb3_encryption_required(tcon))
3766 flags |= CIFS_TRANSFORM_REQ;
be4cb9e3 3767
40eff45b 3768 memset(&rqst, 0, sizeof(struct smb_rqst));
296ecbae 3769 memset(&iov, 0, sizeof(iov));
40eff45b
RS
3770 rqst.rq_iov = iov;
3771 rqst.rq_nvec = 1;
3772
352d96f3
AA
3773 rc = SMB2_query_info_init(tcon, server,
3774 &rqst, persistent_fid, volatile_fid,
296ecbae 3775 info_class, info_type, additional_info,
f5b05d62 3776 output_len, 0, NULL);
296ecbae
RS
3777 if (rc)
3778 goto qinf_exit;
3779
d42043a6
SF
3780 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3781 ses->Suid, info_class, (__u32)info_type);
3782
4f1fffa2
SP
3783 if (retries)
3784 smb2_set_replay(server, &rqst);
3785
352d96f3
AA
3786 rc = cifs_send_recv(xid, ses, server,
3787 &rqst, &resp_buftype, flags, &rsp_iov);
da502f7d 3788 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
e5d04887 3789
be4cb9e3
PS
3790 if (rc) {
3791 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
eccb4422
SF
3792 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3793 ses->Suid, info_class, (__u32)info_type, rc);
be4cb9e3
PS
3794 goto qinf_exit;
3795 }
3796
d42043a6
SF
3797 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3798 ses->Suid, info_class, (__u32)info_type);
3799
42c493c1
SP
3800 if (dlen) {
3801 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3802 if (!*data) {
3803 *data = kmalloc(*dlen, GFP_KERNEL);
3804 if (!*data) {
3175eb9b 3805 cifs_tcon_dbg(VFS,
42c493c1
SP
3806 "Error %d allocating memory for acl\n",
3807 rc);
3808 *dlen = 0;
73aaf920 3809 rc = -ENOMEM;
42c493c1
SP
3810 goto qinf_exit;
3811 }
73aaf920 3812 allocated = true;
42c493c1
SP
3813 }
3814 }
3815
c5a5f38f
RS
3816 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3817 le32_to_cpu(rsp->OutputBufferLength),
9ee2afe5 3818 &rsp_iov, dlen ? *dlen : min_len, *data);
73aaf920
CIK
3819 if (rc && allocated) {
3820 kfree(*data);
3821 *data = NULL;
3822 *dlen = 0;
3823 }
be4cb9e3
PS
3824
3825qinf_exit:
296ecbae 3826 SMB2_query_info_free(&rqst);
be4cb9e3 3827 free_rsp_buf(resp_buftype, rsp);
4f1fffa2
SP
3828
3829 if (is_replayable_error(rc) &&
3830 smb2_should_replay(tcon, &retries, &cur_sleep))
3831 goto replay_again;
3832
be4cb9e3
PS
3833 return rc;
3834}
9094fad1 3835
42c493c1
SP
3836int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3837 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3838{
3839 return query_info(xid, tcon, persistent_fid, volatile_fid,
3840 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3841 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3842 sizeof(struct smb2_file_all_info), (void **)&data,
3843 NULL);
3844}
3845
e0ae8a9a
SF
3846#if 0
3847/* currently unused, as now we are doing compounding instead (see smb311_posix_query_path_info) */
b1bc1874
SF
3848int
3849SMB311_posix_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3850 u64 persistent_fid, u64 volatile_fid, struct smb311_posix_qinfo *data, u32 *plen)
3851{
3852 size_t output_len = sizeof(struct smb311_posix_qinfo *) +
3853 (sizeof(struct cifs_sid) * 2) + (PATH_MAX * 2);
3854 *plen = 0;
3855
3856 return query_info(xid, tcon, persistent_fid, volatile_fid,
3857 SMB_FIND_FILE_POSIX_INFO, SMB2_O_INFO_FILE, 0,
3858 output_len, sizeof(struct smb311_posix_qinfo), (void **)&data, plen);
e0ae8a9a 3859 /* Note caller must free "data" (passed in above). It may be allocated in query_info call */
b1bc1874 3860}
e0ae8a9a 3861#endif
b1bc1874 3862
f0df737e 3863int
42c493c1 3864SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3970acf7 3865 u64 persistent_fid, u64 volatile_fid,
9541b813 3866 void **data, u32 *plen, u32 extra_info)
f0df737e 3867{
9541b813
BP
3868 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
3869 extra_info;
42c493c1
SP
3870 *plen = 0;
3871
f0df737e 3872 return query_info(xid, tcon, persistent_fid, volatile_fid,
42c493c1 3873 0, SMB2_O_INFO_SECURITY, additional_info,
ee25c6dd 3874 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
f0df737e
PS
3875}
3876
3877int
3878SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3879 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3880{
3881 return query_info(xid, tcon, persistent_fid, volatile_fid,
42c493c1
SP
3882 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3883 sizeof(struct smb2_file_internal_info),
f0df737e 3884 sizeof(struct smb2_file_internal_info),
42c493c1 3885 (void **)&uniqueid, NULL);
f0df737e
PS
3886}
3887
c3498185
SF
3888/*
3889 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3890 * See MS-SMB2 2.2.35 and 2.2.36
3891 */
3892
388962e8 3893static int
c3498185 3894SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
352d96f3
AA
3895 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3896 u64 persistent_fid, u64 volatile_fid,
3897 u32 completion_filter, bool watch_tree)
c3498185
SF
3898{
3899 struct smb2_change_notify_req *req;
3900 struct kvec *iov = rqst->rq_iov;
3901 unsigned int total_len;
3902 int rc;
3903
352d96f3
AA
3904 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server,
3905 (void **) &req, &total_len);
c3498185
SF
3906 if (rc)
3907 return rc;
3908
351a59da
PA
3909 req->PersistentFileId = persistent_fid;
3910 req->VolatileFileId = volatile_fid;
d26c2ddd 3911 /* See note 354 of MS-SMB2, 64K max */
52870d50
SF
3912 req->OutputBufferLength =
3913 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
c3498185
SF
3914 req->CompletionFilter = cpu_to_le32(completion_filter);
3915 if (watch_tree)
3916 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3917 else
3918 req->Flags = 0;
3919
3920 iov[0].iov_base = (char *)req;
3921 iov[0].iov_len = total_len;
3922
3923 return 0;
3924}
3925
3926int
3927SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3928 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
e3e94634
SF
3929 u32 completion_filter, u32 max_out_data_len, char **out_data,
3930 u32 *plen /* returned data len */)
c3498185
SF
3931{
3932 struct cifs_ses *ses = tcon->ses;
4f1fffa2 3933 struct TCP_Server_Info *server;
c3498185 3934 struct smb_rqst rqst;
e3e94634 3935 struct smb2_change_notify_rsp *smb_rsp;
c3498185
SF
3936 struct kvec iov[1];
3937 struct kvec rsp_iov = {NULL, 0};
3938 int resp_buftype = CIFS_NO_BUFFER;
3939 int flags = 0;
3940 int rc = 0;
4f1fffa2
SP
3941 int retries = 0, cur_sleep = 1;
3942
3943replay_again:
3944 /* reinitialize for possible replay */
3945 flags = 0;
3946 server = cifs_pick_channel(ses);
c3498185
SF
3947
3948 cifs_dbg(FYI, "change notify\n");
352d96f3 3949 if (!ses || !server)
c3498185
SF
3950 return -EIO;
3951
3952 if (smb3_encryption_required(tcon))
3953 flags |= CIFS_TRANSFORM_REQ;
3954
3955 memset(&rqst, 0, sizeof(struct smb_rqst));
3956 memset(&iov, 0, sizeof(iov));
e3e94634
SF
3957 if (plen)
3958 *plen = 0;
3959
c3498185
SF
3960 rqst.rq_iov = iov;
3961 rqst.rq_nvec = 1;
3962
352d96f3
AA
3963 rc = SMB2_notify_init(xid, &rqst, tcon, server,
3964 persistent_fid, volatile_fid,
c3498185
SF
3965 completion_filter, watch_tree);
3966 if (rc)
3967 goto cnotify_exit;
3968
3969 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3970 (u8)watch_tree, completion_filter);
4f1fffa2
SP
3971
3972 if (retries)
3973 smb2_set_replay(server, &rqst);
3974
352d96f3
AA
3975 rc = cifs_send_recv(xid, ses, server,
3976 &rqst, &resp_buftype, flags, &rsp_iov);
c3498185
SF
3977
3978 if (rc != 0) {
3979 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3980 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3981 (u8)watch_tree, completion_filter, rc);
e3e94634 3982 } else {
c3498185 3983 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
e3e94634
SF
3984 ses->Suid, (u8)watch_tree, completion_filter);
3985 /* validate that notify information is plausible */
3986 if ((rsp_iov.iov_base == NULL) ||
eb3e28c1 3987 (rsp_iov.iov_len < sizeof(struct smb2_change_notify_rsp) + 1))
e3e94634
SF
3988 goto cnotify_exit;
3989
3990 smb_rsp = (struct smb2_change_notify_rsp *)rsp_iov.iov_base;
3991
3992 smb2_validate_iov(le16_to_cpu(smb_rsp->OutputBufferOffset),
3993 le32_to_cpu(smb_rsp->OutputBufferLength), &rsp_iov,
3994 sizeof(struct file_notify_information));
3995
3996 *out_data = kmemdup((char *)smb_rsp + le16_to_cpu(smb_rsp->OutputBufferOffset),
3997 le32_to_cpu(smb_rsp->OutputBufferLength), GFP_KERNEL);
3998 if (*out_data == NULL) {
3999 rc = -ENOMEM;
4000 goto cnotify_exit;
b535cc79 4001 } else if (plen)
e3e94634
SF
4002 *plen = le32_to_cpu(smb_rsp->OutputBufferLength);
4003 }
c3498185
SF
4004
4005 cnotify_exit:
4006 if (rqst.rq_iov)
4007 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
4008 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4f1fffa2
SP
4009
4010 if (is_replayable_error(rc) &&
4011 smb2_should_replay(tcon, &retries, &cur_sleep))
4012 goto replay_again;
4013
c3498185
SF
4014 return rc;
4015}
4016
4017
4018
9094fad1
PS
4019/*
4020 * This is a no-op for now. We're not really interested in the reply, but
4021 * rather in the fact that the server sent one and that server->lstrp
4022 * gets updated.
4023 *
4024 * FIXME: maybe we should consider checking that the reply matches request?
4025 */
4026static void
4027smb2_echo_callback(struct mid_q_entry *mid)
4028{
4029 struct TCP_Server_Info *server = mid->callback_data;
31473fc4 4030 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
34f4deb7 4031 struct cifs_credits credits = { .value = 0, .instance = 0 };
9094fad1 4032
0fd1d37b 4033 if (mid->mid_state == MID_RESPONSE_RECEIVED
34f4deb7 4034 || mid->mid_state == MID_RESPONSE_MALFORMED) {
0d35e382 4035 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
34f4deb7
PS
4036 credits.instance = server->reconnect_instance;
4037 }
9094fad1 4038
70f08f91 4039 release_mid(mid);
34f4deb7 4040 add_credits(server, &credits, CIFS_ECHO_OP);
9094fad1
PS
4041}
4042
53e0e11e
PS
4043void smb2_reconnect_server(struct work_struct *work)
4044{
4045 struct TCP_Server_Info *server = container_of(work,
4046 struct TCP_Server_Info, reconnect.work);
3663c904
SP
4047 struct TCP_Server_Info *pserver;
4048 struct cifs_ses *ses, *ses2;
53e0e11e 4049 struct cifs_tcon *tcon, *tcon2;
3663c904 4050 struct list_head tmp_list, tmp_ses_list;
8ca5d264 4051 bool ses_exist = false;
8a409cda 4052 bool tcon_selected = false;
18ea4311 4053 int rc;
3663c904 4054 bool resched = false;
18ea4311 4055
04909192
SP
4056 /* first check if ref count has reached 0, if not inc ref count */
4057 spin_lock(&cifs_tcp_ses_lock);
4058 if (!server->srv_count) {
4059 spin_unlock(&cifs_tcp_ses_lock);
4060 return;
4061 }
4062 server->srv_count++;
4063 spin_unlock(&cifs_tcp_ses_lock);
4064
3663c904 4065 /* If server is a channel, select the primary channel */
b3773b19 4066 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
53e0e11e
PS
4067
4068 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3663c904 4069 mutex_lock(&pserver->reconnect_mutex);
53e0e11e 4070
ee1d2179
SP
4071 /* if the server is marked for termination, drop the ref count here */
4072 if (server->terminate) {
4073 cifs_put_tcp_session(server, true);
4074 mutex_unlock(&pserver->reconnect_mutex);
4075 return;
4076 }
4077
53e0e11e 4078 INIT_LIST_HEAD(&tmp_list);
3663c904
SP
4079 INIT_LIST_HEAD(&tmp_ses_list);
4080 cifs_dbg(FYI, "Reconnecting tcons and channels\n");
53e0e11e
PS
4081
4082 spin_lock(&cifs_tcp_ses_lock);
3663c904 4083 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
99f28070
WW
4084 spin_lock(&ses->ses_lock);
4085 if (ses->ses_status == SES_EXITING) {
4086 spin_unlock(&ses->ses_lock);
4087 continue;
4088 }
4089 spin_unlock(&ses->ses_lock);
3663c904 4090
8a409cda 4091 tcon_selected = false;
3663c904 4092
53e0e11e 4093 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
96a988ff 4094 if (tcon->need_reconnect || tcon->need_reopen_files) {
53e0e11e
PS
4095 tcon->tc_count++;
4096 list_add_tail(&tcon->rlist, &tmp_list);
8ca5d264 4097 tcon_selected = true;
53e0e11e
PS
4098 }
4099 }
0ff2b018
RS
4100 /*
4101 * IPC has the same lifetime as its session and uses its
4102 * refcount.
4103 */
b327a717
AA
4104 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
4105 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
8ca5d264 4106 tcon_selected = true;
8e355415 4107 cifs_smb_ses_inc_refcount(ses);
3663c904
SP
4108 }
4109 /*
4110 * handle the case where channel needs to reconnect
4111 * binding session, but tcon is healthy (some other channel
4112 * is active)
4113 */
88b024f5 4114 spin_lock(&ses->chan_lock);
3663c904
SP
4115 if (!tcon_selected && cifs_chan_needs_reconnect(ses, server)) {
4116 list_add_tail(&ses->rlist, &tmp_ses_list);
8a409cda 4117 ses_exist = true;
8e355415 4118 cifs_smb_ses_inc_refcount(ses);
b327a717 4119 }
88b024f5 4120 spin_unlock(&ses->chan_lock);
53e0e11e 4121 }
53e0e11e
PS
4122 spin_unlock(&cifs_tcp_ses_lock);
4123
4124 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
04909192 4125 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server, true);
18ea4311 4126 if (!rc)
96a988ff 4127 cifs_reopen_persistent_handles(tcon);
18ea4311
GP
4128 else
4129 resched = true;
53e0e11e 4130 list_del_init(&tcon->rlist);
0ff2b018
RS
4131 if (tcon->ipc)
4132 cifs_put_smb_ses(tcon->ses);
4133 else
4134 cifs_put_tcon(tcon);
53e0e11e
PS
4135 }
4136
3663c904
SP
4137 if (!ses_exist)
4138 goto done;
4139
4140 /* allocate a dummy tcon struct used for reconnect */
2da338ff 4141 tcon = tcon_info_alloc(false);
3663c904
SP
4142 if (!tcon) {
4143 resched = true;
a96c9448
XT
4144 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
4145 list_del_init(&ses->rlist);
4146 cifs_put_smb_ses(ses);
4147 }
3663c904
SP
4148 goto done;
4149 }
4150
fdf59eb5 4151 tcon->status = TID_GOOD;
3663c904
SP
4152 tcon->retry = false;
4153 tcon->need_reconnect = false;
4154
4155 /* now reconnect sessions for necessary channels */
4156 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
4157 tcon->ses = ses;
04909192 4158 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server, true);
3663c904
SP
4159 if (rc)
4160 resched = true;
4161 list_del_init(&ses->rlist);
4162 cifs_put_smb_ses(ses);
4163 }
df57109b 4164 tconInfoFree(tcon);
3663c904
SP
4165
4166done:
4167 cifs_dbg(FYI, "Reconnecting tcons and channels finished\n");
82334252 4168 if (resched)
18ea4311 4169 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3663c904 4170 mutex_unlock(&pserver->reconnect_mutex);
53e0e11e
PS
4171
4172 /* now we can safely release srv struct */
04909192 4173 cifs_put_tcp_session(server, true);
53e0e11e
PS
4174}
4175
9094fad1
PS
4176int
4177SMB2_echo(struct TCP_Server_Info *server)
4178{
4179 struct smb2_echo_req *req;
4180 int rc = 0;
c713c877 4181 struct kvec iov[1];
738f9de5 4182 struct smb_rqst rqst = { .rq_iov = iov,
c713c877 4183 .rq_nvec = 1 };
7f7ae759 4184 unsigned int total_len;
9094fad1 4185
bda487ac 4186 cifs_dbg(FYI, "In echo request for conn_id %lld\n", server->conn_id);
9094fad1 4187
d7d7a66a 4188 spin_lock(&server->srv_lock);
1a6a41d4
SP
4189 if (server->ops->need_neg &&
4190 server->ops->need_neg(server)) {
d7d7a66a 4191 spin_unlock(&server->srv_lock);
53e0e11e 4192 /* No need to send echo on newly established connections */
82334252 4193 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
53e0e11e 4194 return rc;
4fcd1813 4195 }
d7d7a66a 4196 spin_unlock(&server->srv_lock);
4fcd1813 4197
352d96f3
AA
4198 rc = smb2_plain_req_init(SMB2_ECHO, NULL, server,
4199 (void **)&req, &total_len);
9094fad1
PS
4200 if (rc)
4201 return rc;
4202
0d35e382 4203 req->hdr.CreditRequest = cpu_to_le16(1);
9094fad1 4204
c713c877
RS
4205 iov[0].iov_len = total_len;
4206 iov[0].iov_base = (char *)req;
9094fad1 4207
9b7c18a2 4208 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3349c3a7 4209 server, CIFS_ECHO_OP, NULL);
9094fad1 4210 if (rc)
f96637be 4211 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
9094fad1
PS
4212
4213 cifs_small_buf_release(req);
4214 return rc;
4215}
7a5cfb19 4216
86e14e12
RS
4217void
4218SMB2_flush_free(struct smb_rqst *rqst)
4219{
4220 if (rqst && rqst->rq_iov)
4221 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4222}
4223
7a5cfb19 4224int
86e14e12 4225SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
352d96f3
AA
4226 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
4227 u64 persistent_fid, u64 volatile_fid)
7a5cfb19
PS
4228{
4229 struct smb2_flush_req *req;
86e14e12 4230 struct kvec *iov = rqst->rq_iov;
1f444e4c 4231 unsigned int total_len;
86e14e12 4232 int rc;
7a5cfb19 4233
352d96f3
AA
4234 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server,
4235 (void **) &req, &total_len);
7a5cfb19
PS
4236 if (rc)
4237 return rc;
4238
351a59da
PA
4239 req->PersistentFileId = persistent_fid;
4240 req->VolatileFileId = volatile_fid;
7a5cfb19
PS
4241
4242 iov[0].iov_base = (char *)req;
1f444e4c 4243 iov[0].iov_len = total_len;
7a5cfb19 4244
86e14e12
RS
4245 return 0;
4246}
4247
4248int
4249SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4250 u64 volatile_fid)
4251{
4252 struct cifs_ses *ses = tcon->ses;
4253 struct smb_rqst rqst;
4254 struct kvec iov[1];
4255 struct kvec rsp_iov = {NULL, 0};
4f1fffa2 4256 struct TCP_Server_Info *server;
86e14e12
RS
4257 int resp_buftype = CIFS_NO_BUFFER;
4258 int flags = 0;
4259 int rc = 0;
4f1fffa2
SP
4260 int retries = 0, cur_sleep = 1;
4261
4262replay_again:
4263 /* reinitialize for possible replay */
4264 flags = 0;
4265 server = cifs_pick_channel(ses);
86e14e12
RS
4266
4267 cifs_dbg(FYI, "flush\n");
4268 if (!ses || !(ses->server))
4269 return -EIO;
4270
4271 if (smb3_encryption_required(tcon))
4272 flags |= CIFS_TRANSFORM_REQ;
4273
40eff45b 4274 memset(&rqst, 0, sizeof(struct smb_rqst));
86e14e12 4275 memset(&iov, 0, sizeof(iov));
40eff45b
RS
4276 rqst.rq_iov = iov;
4277 rqst.rq_nvec = 1;
4278
352d96f3
AA
4279 rc = SMB2_flush_init(xid, &rqst, tcon, server,
4280 persistent_fid, volatile_fid);
86e14e12
RS
4281 if (rc)
4282 goto flush_exit;
4283
f90f9797 4284 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
4f1fffa2
SP
4285
4286 if (retries)
4287 smb2_set_replay(server, &rqst);
4288
352d96f3
AA
4289 rc = cifs_send_recv(xid, ses, server,
4290 &rqst, &resp_buftype, flags, &rsp_iov);
7a5cfb19 4291
eccb4422 4292 if (rc != 0) {
7a5cfb19 4293 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
eccb4422
SF
4294 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
4295 rc);
f90f9797
SF
4296 } else
4297 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
4298 ses->Suid);
7a5cfb19 4299
86e14e12
RS
4300 flush_exit:
4301 SMB2_flush_free(&rqst);
da502f7d 4302 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4f1fffa2
SP
4303
4304 if (is_replayable_error(rc) &&
4305 smb2_should_replay(tcon, &retries, &cur_sleep))
4306 goto replay_again;
4307
7a5cfb19
PS
4308 return rc;
4309}
09a4707e 4310
a6559cc1
SM
4311#ifdef CONFIG_CIFS_SMB_DIRECT
4312static inline bool smb3_use_rdma_offload(struct cifs_io_parms *io_parms)
4313{
4314 struct TCP_Server_Info *server = io_parms->server;
4315 struct cifs_tcon *tcon = io_parms->tcon;
4316
4317 /* we can only offload if we're connected */
4318 if (!server || !tcon)
4319 return false;
4320
4321 /* we can only offload on an rdma connection */
4322 if (!server->rdma || !server->smbd_conn)
4323 return false;
4324
4325 /* we don't support signed offload yet */
4326 if (server->sign)
4327 return false;
4328
3891f6c7
SM
4329 /* we don't support encrypted offload yet */
4330 if (smb3_encryption_required(tcon))
4331 return false;
4332
a6559cc1
SM
4333 /* offload also has its overhead, so only do it if desired */
4334 if (io_parms->length < server->smbd_conn->rdma_readwrite_threshold)
4335 return false;
4336
4337 return true;
4338}
4339#endif /* CONFIG_CIFS_SMB_DIRECT */
4340
09a4707e
PS
4341/*
4342 * To form a chain of read requests, any read requests after the first should
4343 * have the end_of_chain boolean set to true.
4344 */
4345static int
738f9de5 4346smb2_new_read_req(void **buf, unsigned int *total_len,
2dabfd5b
LL
4347 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
4348 unsigned int remaining_bytes, int request_type)
09a4707e
PS
4349{
4350 int rc = -EACCES;
d8d9de53 4351 struct smb2_read_req *req = NULL;
0d35e382 4352 struct smb2_hdr *shdr;
352d96f3 4353 struct TCP_Server_Info *server = io_parms->server;
09a4707e 4354
352d96f3
AA
4355 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server,
4356 (void **) &req, total_len);
09a4707e
PS
4357 if (rc)
4358 return rc;
2dabfd5b 4359
2dabfd5b 4360 if (server == NULL)
09a4707e
PS
4361 return -ECONNABORTED;
4362
0d35e382
RS
4363 shdr = &req->hdr;
4364 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
09a4707e 4365
351a59da
PA
4366 req->PersistentFileId = io_parms->persistent_fid;
4367 req->VolatileFileId = io_parms->volatile_fid;
09a4707e
PS
4368 req->ReadChannelInfoOffset = 0; /* reserved */
4369 req->ReadChannelInfoLength = 0; /* reserved */
4370 req->Channel = 0; /* reserved */
4371 req->MinimumCount = 0;
4372 req->Length = cpu_to_le32(io_parms->length);
4373 req->Offset = cpu_to_le64(io_parms->offset);
d323c246
SF
4374
4375 trace_smb3_read_enter(0 /* xid */,
4376 io_parms->persistent_fid,
4377 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4378 io_parms->offset, io_parms->length);
bd3dcc6a
LL
4379#ifdef CONFIG_CIFS_SMB_DIRECT
4380 /*
4381 * If we want to do a RDMA write, fill in and append
4382 * smbd_buffer_descriptor_v1 to the end of read request
4383 */
a6559cc1 4384 if (smb3_use_rdma_offload(io_parms)) {
bd3dcc6a 4385 struct smbd_buffer_descriptor_v1 *v1;
352d96f3 4386 bool need_invalidate = server->dialect == SMB30_PROT_ID;
bd3dcc6a 4387
d08089f6
DH
4388 rdata->mr = smbd_register_mr(server->smbd_conn, &rdata->iter,
4389 true, need_invalidate);
bd3dcc6a 4390 if (!rdata->mr)
b7972092 4391 return -EAGAIN;
bd3dcc6a
LL
4392
4393 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4394 if (need_invalidate)
4395 req->Channel = SMB2_CHANNEL_RDMA_V1;
4396 req->ReadChannelInfoOffset =
d8d9de53 4397 cpu_to_le16(offsetof(struct smb2_read_req, Buffer));
bd3dcc6a 4398 req->ReadChannelInfoLength =
2026b06e 4399 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
bd3dcc6a 4400 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
2026b06e
SF
4401 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
4402 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
4403 v1->length = cpu_to_le32(rdata->mr->mr->length);
bd3dcc6a
LL
4404
4405 *total_len += sizeof(*v1) - 1;
4406 }
4407#endif
09a4707e
PS
4408 if (request_type & CHAINED_REQUEST) {
4409 if (!(request_type & END_OF_CHAIN)) {
b8f57ee8 4410 /* next 8-byte aligned request */
d7173623 4411 *total_len = ALIGN(*total_len, 8);
b8f57ee8 4412 shdr->NextCommand = cpu_to_le32(*total_len);
09a4707e 4413 } else /* END_OF_CHAIN */
31473fc4 4414 shdr->NextCommand = 0;
09a4707e 4415 if (request_type & RELATED_REQUEST) {
31473fc4 4416 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
09a4707e
PS
4417 /*
4418 * Related requests use info from previous read request
4419 * in chain.
4420 */
0d35e382
RS
4421 shdr->SessionId = cpu_to_le64(0xFFFFFFFFFFFFFFFF);
4422 shdr->Id.SyncId.TreeId = cpu_to_le32(0xFFFFFFFF);
351a59da
PA
4423 req->PersistentFileId = (u64)-1;
4424 req->VolatileFileId = (u64)-1;
09a4707e
PS
4425 }
4426 }
4427 if (remaining_bytes > io_parms->length)
4428 req->RemainingBytes = cpu_to_le32(remaining_bytes);
4429 else
4430 req->RemainingBytes = 0;
4431
738f9de5 4432 *buf = req;
09a4707e
PS
4433 return rc;
4434}
4435
4436static void
4437smb2_readv_callback(struct mid_q_entry *mid)
4438{
4439 struct cifs_readdata *rdata = mid->callback_data;
4440 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
352d96f3 4441 struct TCP_Server_Info *server = rdata->server;
0d35e382
RS
4442 struct smb2_hdr *shdr =
4443 (struct smb2_hdr *)rdata->iov[0].iov_base;
34f4deb7 4444 struct cifs_credits credits = { .value = 0, .instance = 0 };
023fc150
DH
4445 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1], .rq_nvec = 1 };
4446
4447 if (rdata->got_bytes) {
4448 rqst.rq_iter = rdata->iter;
4449 rqst.rq_iter_size = iov_iter_count(&rdata->iter);
9ee04875 4450 }
09a4707e 4451
352d96f3
AA
4452 WARN_ONCE(rdata->server != mid->server,
4453 "rdata server %p != mid server %p",
4454 rdata->server, mid->server);
4455
f96637be
JP
4456 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
4457 __func__, mid->mid, mid->mid_state, rdata->result,
4458 rdata->bytes);
09a4707e
PS
4459
4460 switch (mid->mid_state) {
4461 case MID_RESPONSE_RECEIVED:
34f4deb7
PS
4462 credits.value = le16_to_cpu(shdr->CreditRequest);
4463 credits.instance = server->reconnect_instance;
09a4707e 4464 /* result already set, check signature */
4326ed2f 4465 if (server->sign && !mid->decrypted) {
3c1bf7e4
PS
4466 int rc;
4467
d08089f6
DH
4468 iov_iter_revert(&rqst.rq_iter, rdata->got_bytes);
4469 iov_iter_truncate(&rqst.rq_iter, rdata->got_bytes);
0b688cfc 4470 rc = smb2_verify_signature(&rqst, server);
3c1bf7e4 4471 if (rc)
3175eb9b 4472 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
f96637be 4473 rc);
3c1bf7e4 4474 }
09a4707e 4475 /* FIXME: should this be counted toward the initiating task? */
34a54d61
PS
4476 task_io_account_read(rdata->got_bytes);
4477 cifs_stats_bytes_read(tcon, rdata->got_bytes);
09a4707e
PS
4478 break;
4479 case MID_REQUEST_SUBMITTED:
4480 case MID_RETRY_NEEDED:
4481 rdata->result = -EAGAIN;
d913ed17
PS
4482 if (server->sign && rdata->got_bytes)
4483 /* reset bytes number since we can not check a sign */
4484 rdata->got_bytes = 0;
4485 /* FIXME: should this be counted toward the initiating task? */
4486 task_io_account_read(rdata->got_bytes);
4487 cifs_stats_bytes_read(tcon, rdata->got_bytes);
09a4707e 4488 break;
0fd1d37b 4489 case MID_RESPONSE_MALFORMED:
34f4deb7
PS
4490 credits.value = le16_to_cpu(shdr->CreditRequest);
4491 credits.instance = server->reconnect_instance;
30b5ae21 4492 fallthrough;
09a4707e 4493 default:
6b15eb18 4494 rdata->result = -EIO;
09a4707e 4495 }
bd3dcc6a
LL
4496#ifdef CONFIG_CIFS_SMB_DIRECT
4497 /*
4498 * If this rdata has a memmory registered, the MR can be freed
4499 * MR needs to be freed as soon as I/O finishes to prevent deadlock
4500 * because they have limited number and are used for future I/Os
4501 */
4502 if (rdata->mr) {
4503 smbd_deregister_mr(rdata->mr);
4504 rdata->mr = NULL;
4505 }
4506#endif
082aaa87 4507 if (rdata->result && rdata->result != -ENODATA) {
09a4707e 4508 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
7d42e72f
PS
4509 trace_smb3_read_err(0 /* xid */,
4510 rdata->cfile->fid.persistent_fid,
4511 tcon->tid, tcon->ses->Suid, rdata->offset,
4512 rdata->bytes, rdata->result);
4513 } else
4514 trace_smb3_read_done(0 /* xid */,
4515 rdata->cfile->fid.persistent_fid,
4516 tcon->tid, tcon->ses->Suid,
4517 rdata->offset, rdata->got_bytes);
09a4707e
PS
4518
4519 queue_work(cifsiod_wq, &rdata->work);
70f08f91 4520 release_mid(mid);
34f4deb7 4521 add_credits(server, &credits, 0);
09a4707e
PS
4522}
4523
738f9de5 4524/* smb2_async_readv - send an async read, and set up mid to handle result */
09a4707e
PS
4525int
4526smb2_async_readv(struct cifs_readdata *rdata)
4527{
bed9da02 4528 int rc, flags = 0;
31473fc4 4529 char *buf;
0d35e382 4530 struct smb2_hdr *shdr;
09a4707e 4531 struct cifs_io_parms io_parms;
738f9de5 4532 struct smb_rqst rqst = { .rq_iov = rdata->iov,
c713c877 4533 .rq_nvec = 1 };
bed9da02 4534 struct TCP_Server_Info *server;
352d96f3 4535 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
738f9de5 4536 unsigned int total_len;
5e90aa21 4537 int credit_request;
09a4707e 4538
f96637be
JP
4539 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
4540 __func__, rdata->offset, rdata->bytes);
09a4707e 4541
352d96f3
AA
4542 if (!rdata->server)
4543 rdata->server = cifs_pick_channel(tcon->ses);
4544
09a4707e 4545 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
352d96f3 4546 io_parms.server = server = rdata->server;
09a4707e
PS
4547 io_parms.offset = rdata->offset;
4548 io_parms.length = rdata->bytes;
4549 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
4550 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
4551 io_parms.pid = rdata->pid;
bed9da02 4552
2dabfd5b
LL
4553 rc = smb2_new_read_req(
4554 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
f0b93cb9 4555 if (rc)
09a4707e
PS
4556 return rc;
4557
5a77e75f 4558 if (smb3_encryption_required(io_parms.tcon))
7fb8986e
PS
4559 flags |= CIFS_TRANSFORM_REQ;
4560
c713c877
RS
4561 rdata->iov[0].iov_base = buf;
4562 rdata->iov[0].iov_len = total_len;
b8f57ee8 4563
0d35e382 4564 shdr = (struct smb2_hdr *)buf;
09a4707e 4565
335b7b62 4566 if (rdata->credits.value > 0) {
31473fc4 4567 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
bed9da02 4568 SMB2_MAX_BUFFER_SIZE));
5e90aa21
SP
4569 credit_request = le16_to_cpu(shdr->CreditCharge) + 8;
4570 if (server->credits >= server->max_credits)
4571 shdr->CreditRequest = cpu_to_le16(0);
4572 else
4573 shdr->CreditRequest = cpu_to_le16(
4574 min_t(int, server->max_credits -
4575 server->credits, credit_request));
9a1c67e8
PS
4576
4577 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4578 if (rc)
335b7b62 4579 goto async_readv_out;
9a1c67e8 4580
7fb8986e 4581 flags |= CIFS_HAS_CREDITS;
bed9da02
PS
4582 }
4583
09a4707e 4584 kref_get(&rdata->refcount);
352d96f3 4585 rc = cifs_call_async(server, &rqst,
09a4707e 4586 cifs_readv_receive, smb2_readv_callback,
3349c3a7
PS
4587 smb3_handle_read_data, rdata, flags,
4588 &rdata->credits);
e5d04887 4589 if (rc) {
09a4707e 4590 kref_put(&rdata->refcount, cifs_readdata_release);
e5d04887 4591 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
7d42e72f
PS
4592 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
4593 io_parms.tcon->tid,
4594 io_parms.tcon->ses->Suid,
4595 io_parms.offset, io_parms.length, rc);
4596 }
09a4707e 4597
335b7b62 4598async_readv_out:
09a4707e
PS
4599 cifs_small_buf_release(buf);
4600 return rc;
4601}
33319141 4602
d8e05039
PS
4603int
4604SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
4605 unsigned int *nbytes, char **buf, int *buf_type)
4606{
40eff45b 4607 struct smb_rqst rqst;
1efd4fc7 4608 int resp_buftype, rc;
d8d9de53 4609 struct smb2_read_req *req = NULL;
d8e05039 4610 struct smb2_read_rsp *rsp = NULL;
f5688a6d 4611 struct kvec iov[1];
da502f7d 4612 struct kvec rsp_iov;
738f9de5 4613 unsigned int total_len;
7fb8986e
PS
4614 int flags = CIFS_LOG_ERROR;
4615 struct cifs_ses *ses = io_parms->tcon->ses;
d8e05039 4616
352d96f3
AA
4617 if (!io_parms->server)
4618 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4619
d8e05039 4620 *nbytes = 0;
2dabfd5b 4621 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
d8e05039
PS
4622 if (rc)
4623 return rc;
4624
5a77e75f 4625 if (smb3_encryption_required(io_parms->tcon))
7fb8986e
PS
4626 flags |= CIFS_TRANSFORM_REQ;
4627
f5688a6d
RS
4628 iov[0].iov_base = (char *)req;
4629 iov[0].iov_len = total_len;
b8f57ee8 4630
40eff45b
RS
4631 memset(&rqst, 0, sizeof(struct smb_rqst));
4632 rqst.rq_iov = iov;
4633 rqst.rq_nvec = 1;
4634
352d96f3
AA
4635 rc = cifs_send_recv(xid, ses, io_parms->server,
4636 &rqst, &resp_buftype, flags, &rsp_iov);
da502f7d 4637 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
d8e05039 4638
a821df3f
RS
4639 if (rc) {
4640 if (rc != -ENODATA) {
4641 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
4642 cifs_dbg(VFS, "Send error in read = %d\n", rc);
d8d9de53 4643 trace_smb3_read_err(xid,
351a59da 4644 req->PersistentFileId,
7d42e72f
PS
4645 io_parms->tcon->tid, ses->Suid,
4646 io_parms->offset, io_parms->length,
4647 rc);
b0a42f2a 4648 } else
351a59da
PA
4649 trace_smb3_read_done(xid, req->PersistentFileId, io_parms->tcon->tid,
4650 ses->Suid, io_parms->offset, 0);
da502f7d 4651 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
05fd5c2c 4652 cifs_small_buf_release(req);
a821df3f 4653 return rc == -ENODATA ? 0 : rc;
eccb4422 4654 } else
d8d9de53 4655 trace_smb3_read_done(xid,
351a59da 4656 req->PersistentFileId,
eccb4422
SF
4657 io_parms->tcon->tid, ses->Suid,
4658 io_parms->offset, io_parms->length);
d8e05039 4659
088aaf17
Z
4660 cifs_small_buf_release(req);
4661
a821df3f
RS
4662 *nbytes = le32_to_cpu(rsp->DataLength);
4663 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4664 (*nbytes > io_parms->length)) {
4665 cifs_dbg(FYI, "bad length %d for count %d\n",
4666 *nbytes, io_parms->length);
4667 rc = -EIO;
4668 *nbytes = 0;
d8e05039
PS
4669 }
4670
4671 if (*buf) {
977b6170 4672 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
da502f7d 4673 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
d8e05039 4674 } else if (resp_buftype != CIFS_NO_BUFFER) {
da502f7d 4675 *buf = rsp_iov.iov_base;
d8e05039
PS
4676 if (resp_buftype == CIFS_SMALL_BUFFER)
4677 *buf_type = CIFS_SMALL_BUFFER;
4678 else if (resp_buftype == CIFS_LARGE_BUFFER)
4679 *buf_type = CIFS_LARGE_BUFFER;
4680 }
4681 return rc;
4682}
4683
33319141
PS
4684/*
4685 * Check the mid_state and signature on received buffer (if any), and queue the
4686 * workqueue completion task.
4687 */
4688static void
4689smb2_writev_callback(struct mid_q_entry *mid)
4690{
4691 struct cifs_writedata *wdata = mid->callback_data;
4692 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
352d96f3 4693 struct TCP_Server_Info *server = wdata->server;
33319141
PS
4694 unsigned int written;
4695 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
34f4deb7 4696 struct cifs_credits credits = { .value = 0, .instance = 0 };
33319141 4697
352d96f3
AA
4698 WARN_ONCE(wdata->server != mid->server,
4699 "wdata server %p != mid server %p",
4700 wdata->server, mid->server);
4701
33319141
PS
4702 switch (mid->mid_state) {
4703 case MID_RESPONSE_RECEIVED:
0d35e382 4704 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
34f4deb7
PS
4705 credits.instance = server->reconnect_instance;
4706 wdata->result = smb2_check_receive(mid, server, 0);
33319141
PS
4707 if (wdata->result != 0)
4708 break;
4709
4710 written = le32_to_cpu(rsp->DataLength);
4711 /*
4712 * Mask off high 16 bits when bytes written as returned
4713 * by the server is greater than bytes requested by the
4714 * client. OS/2 servers are known to set incorrect
4715 * CountHigh values.
4716 */
4717 if (written > wdata->bytes)
4718 written &= 0xFFFF;
4719
4720 if (written < wdata->bytes)
4721 wdata->result = -ENOSPC;
4722 else
4723 wdata->bytes = written;
4724 break;
4725 case MID_REQUEST_SUBMITTED:
4726 case MID_RETRY_NEEDED:
4727 wdata->result = -EAGAIN;
4728 break;
0fd1d37b 4729 case MID_RESPONSE_MALFORMED:
0d35e382 4730 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
34f4deb7 4731 credits.instance = server->reconnect_instance;
30b5ae21 4732 fallthrough;
33319141
PS
4733 default:
4734 wdata->result = -EIO;
4735 break;
4736 }
db223a59
LL
4737#ifdef CONFIG_CIFS_SMB_DIRECT
4738 /*
4739 * If this wdata has a memory registered, the MR can be freed
4740 * The number of MRs available is limited, it's important to recover
4741 * used MR as soon as I/O is finished. Hold MR longer in the later
4742 * I/O process can possibly result in I/O deadlock due to lack of MR
4743 * to send request on I/O retry
4744 */
4745 if (wdata->mr) {
4746 smbd_deregister_mr(wdata->mr);
4747 wdata->mr = NULL;
4748 }
4749#endif
7d42e72f 4750 if (wdata->result) {
33319141 4751 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
7d42e72f
PS
4752 trace_smb3_write_err(0 /* no xid */,
4753 wdata->cfile->fid.persistent_fid,
4754 tcon->tid, tcon->ses->Suid, wdata->offset,
4755 wdata->bytes, wdata->result);
d6fd4190 4756 if (wdata->result == -ENOSPC)
a0a3036b 4757 pr_warn_once("Out of space writing to %s\n",
68e14569 4758 tcon->tree_name);
7d42e72f
PS
4759 } else
4760 trace_smb3_write_done(0 /* no xid */,
4761 wdata->cfile->fid.persistent_fid,
4762 tcon->tid, tcon->ses->Suid,
4763 wdata->offset, wdata->bytes);
33319141
PS
4764
4765 queue_work(cifsiod_wq, &wdata->work);
70f08f91 4766 release_mid(mid);
34f4deb7 4767 add_credits(server, &credits, 0);
33319141
PS
4768}
4769
4770/* smb2_async_writev - send an async write, and set up mid to handle result */
4771int
4a5c80d7
SF
4772smb2_async_writev(struct cifs_writedata *wdata,
4773 void (*release)(struct kref *kref))
33319141 4774{
cb7e9eab 4775 int rc = -EACCES, flags = 0;
33319141 4776 struct smb2_write_req *req = NULL;
0d35e382 4777 struct smb2_hdr *shdr;
33319141 4778 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
352d96f3 4779 struct TCP_Server_Info *server = wdata->server;
c713c877 4780 struct kvec iov[1];
738f9de5 4781 struct smb_rqst rqst = { };
f5688a6d 4782 unsigned int total_len;
d643a8a4
SM
4783 struct cifs_io_parms _io_parms;
4784 struct cifs_io_parms *io_parms = NULL;
5e90aa21 4785 int credit_request;
33319141 4786
4cdad802 4787 if (!wdata->server || wdata->replay)
352d96f3
AA
4788 server = wdata->server = cifs_pick_channel(tcon->ses);
4789
d643a8a4
SM
4790 /*
4791 * in future we may get cifs_io_parms passed in from the caller,
4792 * but for now we construct it here...
4793 */
4794 _io_parms = (struct cifs_io_parms) {
4795 .tcon = tcon,
4796 .server = server,
4797 .offset = wdata->offset,
4798 .length = wdata->bytes,
4799 .persistent_fid = wdata->cfile->fid.persistent_fid,
4800 .volatile_fid = wdata->cfile->fid.volatile_fid,
4801 .pid = wdata->pid,
4802 };
4803 io_parms = &_io_parms;
4804
352d96f3
AA
4805 rc = smb2_plain_req_init(SMB2_WRITE, tcon, server,
4806 (void **) &req, &total_len);
f0b93cb9
PS
4807 if (rc)
4808 return rc;
33319141 4809
5a77e75f 4810 if (smb3_encryption_required(tcon))
7fb8986e
PS
4811 flags |= CIFS_TRANSFORM_REQ;
4812
0d35e382 4813 shdr = (struct smb2_hdr *)req;
d643a8a4 4814 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
33319141 4815
d643a8a4
SM
4816 req->PersistentFileId = io_parms->persistent_fid;
4817 req->VolatileFileId = io_parms->volatile_fid;
33319141
PS
4818 req->WriteChannelInfoOffset = 0;
4819 req->WriteChannelInfoLength = 0;
d08089f6 4820 req->Channel = SMB2_CHANNEL_NONE;
d643a8a4 4821 req->Offset = cpu_to_le64(io_parms->offset);
33319141 4822 req->DataOffset = cpu_to_le16(
f5688a6d 4823 offsetof(struct smb2_write_req, Buffer));
33319141 4824 req->RemainingBytes = 0;
d323c246 4825
d643a8a4
SM
4826 trace_smb3_write_enter(0 /* xid */,
4827 io_parms->persistent_fid,
4828 io_parms->tcon->tid,
4829 io_parms->tcon->ses->Suid,
4830 io_parms->offset,
4831 io_parms->length);
4832
db223a59
LL
4833#ifdef CONFIG_CIFS_SMB_DIRECT
4834 /*
4835 * If we want to do a server RDMA read, fill in and append
4836 * smbd_buffer_descriptor_v1 to the end of write request
4837 */
a6559cc1 4838 if (smb3_use_rdma_offload(io_parms)) {
db223a59 4839 struct smbd_buffer_descriptor_v1 *v1;
d08089f6 4840 size_t data_size = iov_iter_count(&wdata->iter);
db223a59
LL
4841 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4842
d08089f6
DH
4843 wdata->mr = smbd_register_mr(server->smbd_conn, &wdata->iter,
4844 false, need_invalidate);
db223a59 4845 if (!wdata->mr) {
b7972092 4846 rc = -EAGAIN;
db223a59
LL
4847 goto async_writev_out;
4848 }
4849 req->Length = 0;
4850 req->DataOffset = 0;
d08089f6 4851 req->RemainingBytes = cpu_to_le32(data_size);
db223a59
LL
4852 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4853 if (need_invalidate)
4854 req->Channel = SMB2_CHANNEL_RDMA_V1;
4855 req->WriteChannelInfoOffset =
2026b06e 4856 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
db223a59 4857 req->WriteChannelInfoLength =
2026b06e 4858 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
db223a59 4859 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
2026b06e
SF
4860 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4861 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4862 v1->length = cpu_to_le32(wdata->mr->mr->length);
db223a59
LL
4863 }
4864#endif
c713c877
RS
4865 iov[0].iov_len = total_len - 1;
4866 iov[0].iov_base = (char *)req;
33319141 4867
738f9de5 4868 rqst.rq_iov = iov;
c713c877 4869 rqst.rq_nvec = 1;
d08089f6
DH
4870 rqst.rq_iter = wdata->iter;
4871 rqst.rq_iter_size = iov_iter_count(&rqst.rq_iter);
4cdad802
SP
4872 if (wdata->replay)
4873 smb2_set_replay(server, &rqst);
db223a59 4874#ifdef CONFIG_CIFS_SMB_DIRECT
d08089f6 4875 if (wdata->mr)
c713c877 4876 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
db223a59 4877#endif
d08089f6
DH
4878 cifs_dbg(FYI, "async write at %llu %u bytes iter=%zx\n",
4879 io_parms->offset, io_parms->length, iov_iter_count(&rqst.rq_iter));
33319141 4880
db223a59
LL
4881#ifdef CONFIG_CIFS_SMB_DIRECT
4882 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4883 if (!wdata->mr)
d643a8a4 4884 req->Length = cpu_to_le32(io_parms->length);
db223a59 4885#else
d643a8a4 4886 req->Length = cpu_to_le32(io_parms->length);
db223a59 4887#endif
33319141 4888
335b7b62 4889 if (wdata->credits.value > 0) {
31473fc4 4890 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
cb7e9eab 4891 SMB2_MAX_BUFFER_SIZE));
5e90aa21
SP
4892 credit_request = le16_to_cpu(shdr->CreditCharge) + 8;
4893 if (server->credits >= server->max_credits)
4894 shdr->CreditRequest = cpu_to_le16(0);
4895 else
4896 shdr->CreditRequest = cpu_to_le16(
4897 min_t(int, server->max_credits -
4898 server->credits, credit_request));
9a1c67e8 4899
d643a8a4 4900 rc = adjust_credits(server, &wdata->credits, io_parms->length);
9a1c67e8 4901 if (rc)
335b7b62 4902 goto async_writev_out;
9a1c67e8 4903
7fb8986e 4904 flags |= CIFS_HAS_CREDITS;
cb7e9eab
PS
4905 }
4906
33319141 4907 kref_get(&wdata->refcount);
9b7c18a2 4908 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
3349c3a7 4909 wdata, flags, &wdata->credits);
33319141 4910
e5d04887 4911 if (rc) {
d8d9de53 4912 trace_smb3_write_err(0 /* no xid */,
d643a8a4
SM
4913 io_parms->persistent_fid,
4914 io_parms->tcon->tid,
4915 io_parms->tcon->ses->Suid,
4916 io_parms->offset,
4917 io_parms->length,
4918 rc);
4a5c80d7 4919 kref_put(&wdata->refcount, release);
e5d04887 4920 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
7d42e72f 4921 }
33319141 4922
33319141
PS
4923async_writev_out:
4924 cifs_small_buf_release(req);
33319141
PS
4925 return rc;
4926}
009d3443
PS
4927
4928/*
4929 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4930 * The length field from io_parms must be at least 1 and indicates a number of
4931 * elements with data to write that begins with position 1 in iov array. All
4932 * data length is specified by count.
4933 */
4934int
4935SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4936 unsigned int *nbytes, struct kvec *iov, int n_vec)
4937{
40eff45b 4938 struct smb_rqst rqst;
009d3443
PS
4939 int rc = 0;
4940 struct smb2_write_req *req = NULL;
4941 struct smb2_write_rsp *rsp = NULL;
4942 int resp_buftype;
da502f7d 4943 struct kvec rsp_iov;
7fb8986e 4944 int flags = 0;
f5688a6d 4945 unsigned int total_len;
352d96f3 4946 struct TCP_Server_Info *server;
4f1fffa2 4947 int retries = 0, cur_sleep = 1;
da502f7d 4948
4f1fffa2
SP
4949replay_again:
4950 /* reinitialize for possible replay */
4951 flags = 0;
009d3443 4952 *nbytes = 0;
352d96f3
AA
4953 if (!io_parms->server)
4954 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4955 server = io_parms->server;
4956 if (server == NULL)
4957 return -ECONNABORTED;
4958
4f1fffa2
SP
4959 if (n_vec < 1)
4960 return rc;
4961
352d96f3
AA
4962 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server,
4963 (void **) &req, &total_len);
009d3443
PS
4964 if (rc)
4965 return rc;
4966
5a77e75f 4967 if (smb3_encryption_required(io_parms->tcon))
7fb8986e
PS
4968 flags |= CIFS_TRANSFORM_REQ;
4969
0d35e382 4970 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
009d3443 4971
351a59da
PA
4972 req->PersistentFileId = io_parms->persistent_fid;
4973 req->VolatileFileId = io_parms->volatile_fid;
009d3443
PS
4974 req->WriteChannelInfoOffset = 0;
4975 req->WriteChannelInfoLength = 0;
4976 req->Channel = 0;
4977 req->Length = cpu_to_le32(io_parms->length);
4978 req->Offset = cpu_to_le64(io_parms->offset);
009d3443 4979 req->DataOffset = cpu_to_le16(
f5688a6d 4980 offsetof(struct smb2_write_req, Buffer));
009d3443
PS
4981 req->RemainingBytes = 0;
4982
d323c246
SF
4983 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4984 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4985 io_parms->offset, io_parms->length);
4986
009d3443 4987 iov[0].iov_base = (char *)req;
f5688a6d
RS
4988 /* 1 for Buffer */
4989 iov[0].iov_len = total_len - 1;
009d3443 4990
40eff45b
RS
4991 memset(&rqst, 0, sizeof(struct smb_rqst));
4992 rqst.rq_iov = iov;
4993 rqst.rq_nvec = n_vec + 1;
4994
4f1fffa2
SP
4995 if (retries)
4996 smb2_set_replay(server, &rqst);
4997
352d96f3
AA
4998 rc = cifs_send_recv(xid, io_parms->tcon->ses, server,
4999 &rqst,
f5688a6d 5000 &resp_buftype, flags, &rsp_iov);
da502f7d 5001 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
009d3443
PS
5002
5003 if (rc) {
d8d9de53 5004 trace_smb3_write_err(xid,
351a59da 5005 req->PersistentFileId,
eccb4422
SF
5006 io_parms->tcon->tid,
5007 io_parms->tcon->ses->Suid,
5008 io_parms->offset, io_parms->length, rc);
009d3443 5009 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
f96637be 5010 cifs_dbg(VFS, "Send error in write = %d\n", rc);
eccb4422 5011 } else {
009d3443 5012 *nbytes = le32_to_cpu(rsp->DataLength);
d8d9de53 5013 trace_smb3_write_done(xid,
351a59da 5014 req->PersistentFileId,
d8d9de53
RS
5015 io_parms->tcon->tid,
5016 io_parms->tcon->ses->Suid,
5017 io_parms->offset, *nbytes);
eccb4422 5018 }
e5d04887 5019
6a3eb336 5020 cifs_small_buf_release(req);
e5d04887 5021 free_rsp_buf(resp_buftype, rsp);
4f1fffa2
SP
5022
5023 if (is_replayable_error(rc) &&
5024 smb2_should_replay(io_parms->tcon, &retries, &cur_sleep))
5025 goto replay_again;
5026
009d3443
PS
5027 return rc;
5028}
35143eb5 5029
69dda305 5030int posix_info_sid_size(const void *beg, const void *end)
349e13ad
AA
5031{
5032 size_t subauth;
5033 int total;
5034
5035 if (beg + 1 > end)
5036 return -1;
5037
5038 subauth = *(u8 *)(beg+1);
5039 if (subauth < 1 || subauth > 15)
5040 return -1;
5041
5042 total = 1 + 1 + 6 + 4*subauth;
5043 if (beg + total > end)
5044 return -1;
5045
5046 return total;
5047}
5048
5049int posix_info_parse(const void *beg, const void *end,
5050 struct smb2_posix_info_parsed *out)
5051
5052{
5053 int total_len = 0;
ca38fabc 5054 int owner_len, group_len;
349e13ad
AA
5055 int name_len;
5056 const void *owner_sid;
5057 const void *group_sid;
5058 const void *name;
5059
5060 /* if no end bound given, assume payload to be correct */
5061 if (!end) {
5062 const struct smb2_posix_info *p = beg;
5063
5064 end = beg + le32_to_cpu(p->NextEntryOffset);
5065 /* last element will have a 0 offset, pick a sensible bound */
5066 if (end == beg)
5067 end += 0xFFFF;
5068 }
5069
5070 /* check base buf */
5071 if (beg + sizeof(struct smb2_posix_info) > end)
5072 return -1;
5073 total_len = sizeof(struct smb2_posix_info);
5074
5075 /* check owner sid */
5076 owner_sid = beg + total_len;
ca38fabc
RS
5077 owner_len = posix_info_sid_size(owner_sid, end);
5078 if (owner_len < 0)
349e13ad 5079 return -1;
ca38fabc 5080 total_len += owner_len;
349e13ad
AA
5081
5082 /* check group sid */
5083 group_sid = beg + total_len;
ca38fabc
RS
5084 group_len = posix_info_sid_size(group_sid, end);
5085 if (group_len < 0)
349e13ad 5086 return -1;
ca38fabc 5087 total_len += group_len;
349e13ad
AA
5088
5089 /* check name len */
5090 if (beg + total_len + 4 > end)
5091 return -1;
5092 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
5093 if (name_len < 1 || name_len > 0xFFFF)
5094 return -1;
5095 total_len += 4;
5096
5097 /* check name */
5098 name = beg + total_len;
5099 if (name + name_len > end)
5100 return -1;
5101 total_len += name_len;
5102
5103 if (out) {
5104 out->base = beg;
5105 out->size = total_len;
5106 out->name_len = name_len;
5107 out->name = name;
ca38fabc
RS
5108 memcpy(&out->owner, owner_sid, owner_len);
5109 memcpy(&out->group, group_sid, group_len);
349e13ad
AA
5110 }
5111 return total_len;
5112}
5113
5114static int posix_info_extra_size(const void *beg, const void *end)
5115{
5116 int len = posix_info_parse(beg, end, NULL);
5117
5118 if (len < 0)
5119 return -1;
5120 return len - sizeof(struct smb2_posix_info);
5121}
5122
d324f08d 5123static unsigned int
3d519bd1
AA
5124num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
5125 size_t size)
d324f08d
PS
5126{
5127 int len;
5128 unsigned int entrycount = 0;
5129 unsigned int next_offset = 0;
56446f21
DC
5130 char *entryptr;
5131 FILE_DIRECTORY_INFO *dir_info;
d324f08d
PS
5132
5133 if (bufstart == NULL)
5134 return 0;
5135
56446f21 5136 entryptr = bufstart;
d324f08d
PS
5137
5138 while (1) {
56446f21
DC
5139 if (entryptr + next_offset < entryptr ||
5140 entryptr + next_offset > end_of_buf ||
5141 entryptr + next_offset + size > end_of_buf) {
f96637be 5142 cifs_dbg(VFS, "malformed search entry would overflow\n");
d324f08d
PS
5143 break;
5144 }
5145
56446f21
DC
5146 entryptr = entryptr + next_offset;
5147 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
5148
3d519bd1
AA
5149 if (infotype == SMB_FIND_FILE_POSIX_INFO)
5150 len = posix_info_extra_size(entryptr, end_of_buf);
5151 else
5152 len = le32_to_cpu(dir_info->FileNameLength);
5153
5154 if (len < 0 ||
5155 entryptr + len < entryptr ||
56446f21
DC
5156 entryptr + len > end_of_buf ||
5157 entryptr + len + size > end_of_buf) {
f96637be
JP
5158 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
5159 end_of_buf);
d324f08d
PS
5160 break;
5161 }
5162
56446f21 5163 *lastentry = entryptr;
d324f08d
PS
5164 entrycount++;
5165
56446f21 5166 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
d324f08d
PS
5167 if (!next_offset)
5168 break;
5169 }
5170
5171 return entrycount;
5172}
5173
5174/*
5175 * Readdir/FindFirst
5176 */
0a17799c 5177int SMB2_query_directory_init(const unsigned int xid,
352d96f3
AA
5178 struct cifs_tcon *tcon,
5179 struct TCP_Server_Info *server,
5180 struct smb_rqst *rqst,
0a17799c
RS
5181 u64 persistent_fid, u64 volatile_fid,
5182 int index, int info_level)
d324f08d
PS
5183{
5184 struct smb2_query_directory_req *req;
d324f08d 5185 unsigned char *bufptr;
d324f08d 5186 __le16 asteriks = cpu_to_le16('*');
0a17799c
RS
5187 unsigned int output_size = CIFSMaxBufSize -
5188 MAX_SMB2_CREATE_RESPONSE_SIZE -
5189 MAX_SMB2_CLOSE_RESPONSE_SIZE;
7c00c3a6 5190 unsigned int total_len;
0a17799c
RS
5191 struct kvec *iov = rqst->rq_iov;
5192 int len, rc;
d324f08d 5193
352d96f3
AA
5194 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server,
5195 (void **) &req, &total_len);
d324f08d
PS
5196 if (rc)
5197 return rc;
5198
0a17799c 5199 switch (info_level) {
d324f08d
PS
5200 case SMB_FIND_FILE_DIRECTORY_INFO:
5201 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
d324f08d
PS
5202 break;
5203 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
5204 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
d324f08d 5205 break;
3d519bd1
AA
5206 case SMB_FIND_FILE_POSIX_INFO:
5207 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
5208 break;
d324f08d 5209 default:
3175eb9b 5210 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
0a17799c
RS
5211 info_level);
5212 return -EINVAL;
d324f08d
PS
5213 }
5214
5215 req->FileIndex = cpu_to_le32(index);
5216 req->PersistentFileId = persistent_fid;
5217 req->VolatileFileId = volatile_fid;
5218
5219 len = 0x2;
5220 bufptr = req->Buffer;
5221 memcpy(bufptr, &asteriks, len);
5222
5223 req->FileNameOffset =
eb3e28c1 5224 cpu_to_le16(sizeof(struct smb2_query_directory_req));
d324f08d
PS
5225 req->FileNameLength = cpu_to_le16(len);
5226 /*
5227 * BB could be 30 bytes or so longer if we used SMB2 specific
5228 * buffer lengths, but this is safe and close enough.
5229 */
5230 output_size = min_t(unsigned int, output_size, server->maxBuf);
5231 output_size = min_t(unsigned int, output_size, 2 << 15);
5232 req->OutputBufferLength = cpu_to_le32(output_size);
5233
5234 iov[0].iov_base = (char *)req;
7c00c3a6
RS
5235 /* 1 for Buffer */
5236 iov[0].iov_len = total_len - 1;
d324f08d
PS
5237
5238 iov[1].iov_base = (char *)(req->Buffer);
5239 iov[1].iov_len = len;
5240
0a17799c
RS
5241 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
5242 tcon->ses->Suid, index, output_size);
5243
5244 return 0;
5245}
5246
5247void SMB2_query_directory_free(struct smb_rqst *rqst)
5248{
5249 if (rqst && rqst->rq_iov) {
5250 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
5251 }
5252}
5253
af08f9e7
RS
5254int
5255smb2_parse_query_directory(struct cifs_tcon *tcon,
5256 struct kvec *rsp_iov,
5257 int resp_buftype,
5258 struct cifs_search_info *srch_inf)
5259{
5260 struct smb2_query_directory_rsp *rsp;
5261 size_t info_buf_size;
5262 char *end_of_smb;
5263 int rc;
5264
5265 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
5266
5267 switch (srch_inf->info_level) {
5268 case SMB_FIND_FILE_DIRECTORY_INFO:
35235e19 5269 info_buf_size = sizeof(FILE_DIRECTORY_INFO);
af08f9e7
RS
5270 break;
5271 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
35235e19 5272 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO);
af08f9e7 5273 break;
3d519bd1
AA
5274 case SMB_FIND_FILE_POSIX_INFO:
5275 /* note that posix payload are variable size */
5276 info_buf_size = sizeof(struct smb2_posix_info);
5277 break;
af08f9e7
RS
5278 default:
5279 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
5280 srch_inf->info_level);
5281 return -EINVAL;
5282 }
5283
5284 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5285 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
5286 info_buf_size);
3d519bd1
AA
5287 if (rc) {
5288 cifs_tcon_dbg(VFS, "bad info payload");
af08f9e7 5289 return rc;
3d519bd1 5290 }
af08f9e7
RS
5291
5292 srch_inf->unicode = true;
5293
5294 if (srch_inf->ntwrk_buf_start) {
5295 if (srch_inf->smallBuf)
5296 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
5297 else
5298 cifs_buf_release(srch_inf->ntwrk_buf_start);
5299 }
5300 srch_inf->ntwrk_buf_start = (char *)rsp;
5301 srch_inf->srch_entries_start = srch_inf->last_entry =
5302 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
5303 end_of_smb = rsp_iov->iov_len + (char *)rsp;
3d519bd1
AA
5304
5305 srch_inf->entries_in_buffer = num_entries(
5306 srch_inf->info_level,
5307 srch_inf->srch_entries_start,
5308 end_of_smb,
5309 &srch_inf->last_entry,
5310 info_buf_size);
5311
af08f9e7
RS
5312 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
5313 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
5314 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
5315 srch_inf->srch_entries_start, srch_inf->last_entry);
5316 if (resp_buftype == CIFS_LARGE_BUFFER)
5317 srch_inf->smallBuf = false;
5318 else if (resp_buftype == CIFS_SMALL_BUFFER)
5319 srch_inf->smallBuf = true;
5320 else
a0a3036b 5321 cifs_tcon_dbg(VFS, "Invalid search buffer type\n");
af08f9e7
RS
5322
5323 return 0;
5324}
5325
0a17799c
RS
5326int
5327SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
5328 u64 persistent_fid, u64 volatile_fid, int index,
5329 struct cifs_search_info *srch_inf)
5330{
5331 struct smb_rqst rqst;
5332 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
5333 struct smb2_query_directory_rsp *rsp = NULL;
5334 int resp_buftype = CIFS_NO_BUFFER;
5335 struct kvec rsp_iov;
5336 int rc = 0;
0a17799c 5337 struct cifs_ses *ses = tcon->ses;
4f1fffa2 5338 struct TCP_Server_Info *server;
0a17799c 5339 int flags = 0;
4f1fffa2
SP
5340 int retries = 0, cur_sleep = 1;
5341
5342replay_again:
5343 /* reinitialize for possible replay */
5344 flags = 0;
5345 server = cifs_pick_channel(ses);
0a17799c 5346
c4985c3d 5347 if (!ses || !(ses->server))
0a17799c
RS
5348 return -EIO;
5349
5350 if (smb3_encryption_required(tcon))
5351 flags |= CIFS_TRANSFORM_REQ;
5352
40eff45b 5353 memset(&rqst, 0, sizeof(struct smb_rqst));
0a17799c 5354 memset(&iov, 0, sizeof(iov));
40eff45b 5355 rqst.rq_iov = iov;
0a17799c 5356 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
40eff45b 5357
352d96f3
AA
5358 rc = SMB2_query_directory_init(xid, tcon, server,
5359 &rqst, persistent_fid,
0a17799c
RS
5360 volatile_fid, index,
5361 srch_inf->info_level);
5362 if (rc)
5363 goto qdir_exit;
d323c246 5364
4f1fffa2
SP
5365 if (retries)
5366 smb2_set_replay(server, &rqst);
5367
352d96f3
AA
5368 rc = cifs_send_recv(xid, ses, server,
5369 &rqst, &resp_buftype, flags, &rsp_iov);
da502f7d 5370 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
e5d04887 5371
d324f08d 5372 if (rc) {
31473fc4 5373 if (rc == -ENODATA &&
0d35e382 5374 rsp->hdr.Status == STATUS_NO_MORE_FILES) {
adb3b4e9
SF
5375 trace_smb3_query_dir_done(xid, persistent_fid,
5376 tcon->tid, tcon->ses->Suid, index, 0);
52755808
PS
5377 srch_inf->endOfSearch = true;
5378 rc = 0;
adb3b4e9
SF
5379 } else {
5380 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5381 tcon->ses->Suid, index, 0, rc);
8e6e72ae 5382 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
adb3b4e9 5383 }
d324f08d
PS
5384 goto qdir_exit;
5385 }
d324f08d 5386
af08f9e7
RS
5387 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
5388 srch_inf);
adb3b4e9
SF
5389 if (rc) {
5390 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5391 tcon->ses->Suid, index, 0, rc);
d324f08d 5392 goto qdir_exit;
adb3b4e9 5393 }
0a17799c
RS
5394 resp_buftype = CIFS_NO_BUFFER;
5395
adb3b4e9
SF
5396 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
5397 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
d324f08d
PS
5398
5399qdir_exit:
0a17799c 5400 SMB2_query_directory_free(&rqst);
d324f08d 5401 free_rsp_buf(resp_buftype, rsp);
4f1fffa2
SP
5402
5403 if (is_replayable_error(rc) &&
5404 smb2_should_replay(tcon, &retries, &cur_sleep))
5405 goto replay_again;
5406
d324f08d
PS
5407 return rc;
5408}
5409
ba8ca116 5410int
352d96f3
AA
5411SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
5412 struct smb_rqst *rqst,
5413 u64 persistent_fid, u64 volatile_fid, u32 pid,
5414 u8 info_class, u8 info_type, u32 additional_info,
5415 void **data, unsigned int *size)
35143eb5
PS
5416{
5417 struct smb2_set_info_req *req;
ba8ca116
RS
5418 struct kvec *iov = rqst->rq_iov;
5419 unsigned int i, total_len;
5420 int rc;
35143eb5 5421
352d96f3
AA
5422 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server,
5423 (void **) &req, &total_len);
ba8ca116 5424 if (rc)
35143eb5 5425 return rc;
7fb8986e 5426
0d35e382 5427 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
dac95340 5428 req->InfoType = info_type;
35143eb5
PS
5429 req->FileInfoClass = info_class;
5430 req->PersistentFileId = persistent_fid;
5431 req->VolatileFileId = volatile_fid;
dac95340 5432 req->AdditionalInformation = cpu_to_le32(additional_info);
35143eb5 5433
eb3e28c1 5434 req->BufferOffset = cpu_to_le16(sizeof(struct smb2_set_info_req));
35143eb5
PS
5435 req->BufferLength = cpu_to_le32(*size);
5436
35143eb5 5437 memcpy(req->Buffer, *data, *size);
2fc803ef 5438 total_len += *size;
35143eb5
PS
5439
5440 iov[0].iov_base = (char *)req;
2fc803ef
RS
5441 /* 1 for Buffer */
5442 iov[0].iov_len = total_len - 1;
35143eb5 5443
ba8ca116 5444 for (i = 1; i < rqst->rq_nvec; i++) {
35143eb5
PS
5445 le32_add_cpu(&req->BufferLength, size[i]);
5446 iov[i].iov_base = (char *)data[i];
5447 iov[i].iov_len = size[i];
5448 }
5449
ba8ca116
RS
5450 return 0;
5451}
5452
5453void
5454SMB2_set_info_free(struct smb_rqst *rqst)
5455{
32a1fb36
RS
5456 if (rqst && rqst->rq_iov)
5457 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
ba8ca116
RS
5458}
5459
5460static int
5461send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
5462 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
5463 u8 info_type, u32 additional_info, unsigned int num,
5464 void **data, unsigned int *size)
5465{
5466 struct smb_rqst rqst;
5467 struct smb2_set_info_rsp *rsp = NULL;
5468 struct kvec *iov;
5469 struct kvec rsp_iov;
5470 int rc = 0;
5471 int resp_buftype;
5472 struct cifs_ses *ses = tcon->ses;
4f1fffa2 5473 struct TCP_Server_Info *server;
ba8ca116 5474 int flags = 0;
4f1fffa2
SP
5475 int retries = 0, cur_sleep = 1;
5476
5477replay_again:
5478 /* reinitialize for possible replay */
5479 flags = 0;
5480 server = cifs_pick_channel(ses);
ba8ca116 5481
352d96f3 5482 if (!ses || !server)
ba8ca116
RS
5483 return -EIO;
5484
5485 if (!num)
5486 return -EINVAL;
5487
5488 if (smb3_encryption_required(tcon))
5489 flags |= CIFS_TRANSFORM_REQ;
5490
5491 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
5492 if (!iov)
5493 return -ENOMEM;
5494
40eff45b
RS
5495 memset(&rqst, 0, sizeof(struct smb_rqst));
5496 rqst.rq_iov = iov;
5497 rqst.rq_nvec = num;
5498
352d96f3
AA
5499 rc = SMB2_set_info_init(tcon, server,
5500 &rqst, persistent_fid, volatile_fid, pid,
ba8ca116
RS
5501 info_class, info_type, additional_info,
5502 data, size);
5503 if (rc) {
5504 kfree(iov);
5505 return rc;
5506 }
5507
4f1fffa2
SP
5508 if (retries)
5509 smb2_set_replay(server, &rqst);
ba8ca116 5510
352d96f3
AA
5511 rc = cifs_send_recv(xid, ses, server,
5512 &rqst, &resp_buftype, flags,
2fc803ef 5513 &rsp_iov);
ba8ca116 5514 SMB2_set_info_free(&rqst);
da502f7d 5515 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
35143eb5 5516
eccb4422 5517 if (rc != 0) {
35143eb5 5518 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
eccb4422
SF
5519 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
5520 ses->Suid, info_class, (__u32)info_type, rc);
5521 }
7d3fb24b 5522
35143eb5
PS
5523 free_rsp_buf(resp_buftype, rsp);
5524 kfree(iov);
4f1fffa2
SP
5525
5526 if (is_replayable_error(rc) &&
5527 smb2_should_replay(tcon, &retries, &cur_sleep))
5528 goto replay_again;
5529
35143eb5
PS
5530 return rc;
5531}
5532
c839ff24
PS
5533int
5534SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
6ebfede8 5535 u64 volatile_fid, u32 pid, loff_t new_eof)
c839ff24
PS
5536{
5537 struct smb2_file_eof_info info;
5538 void *data;
5539 unsigned int size;
5540
6ebfede8 5541 info.EndOfFile = cpu_to_le64(new_eof);
c839ff24
PS
5542
5543 data = &info;
5544 size = sizeof(struct smb2_file_eof_info);
5545
6ebfede8 5546 trace_smb3_set_eof(xid, persistent_fid, tcon->tid, tcon->ses->Suid, new_eof);
7c05eae8 5547
3764cbd1 5548 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
dac95340
SP
5549 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
5550 0, 1, &data, &size);
c839ff24 5551}
1feeaac7 5552
dac95340
SP
5553int
5554SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
5555 u64 persistent_fid, u64 volatile_fid,
5556 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
5557{
5558 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5559 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
5560 1, (void **)&pnntsd, &pacllen);
1feeaac7 5561}
983c88a4 5562
5517554e
RS
5563int
5564SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
5565 u64 persistent_fid, u64 volatile_fid,
5566 struct smb2_file_full_ea_info *buf, int len)
5567{
5568 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5569 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
5570 0, 1, (void **)&buf, &len);
5571}
5572
983c88a4
PS
5573int
5574SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
5575 const u64 persistent_fid, const u64 volatile_fid,
5576 __u8 oplock_level)
5577{
40eff45b 5578 struct smb_rqst rqst;
983c88a4 5579 int rc;
0d5a288d 5580 struct smb2_oplock_break *req = NULL;
21ad9487 5581 struct cifs_ses *ses = tcon->ses;
4f1fffa2 5582 struct TCP_Server_Info *server;
7fb8986e 5583 int flags = CIFS_OBREAK_OP;
21ad9487
RS
5584 unsigned int total_len;
5585 struct kvec iov[1];
5586 struct kvec rsp_iov;
5587 int resp_buf_type;
4f1fffa2
SP
5588 int retries = 0, cur_sleep = 1;
5589
5590replay_again:
5591 /* reinitialize for possible replay */
5592 flags = CIFS_OBREAK_OP;
5593 server = cifs_pick_channel(ses);
983c88a4 5594
f96637be 5595 cifs_dbg(FYI, "SMB2_oplock_break\n");
352d96f3
AA
5596 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5597 (void **) &req, &total_len);
983c88a4
PS
5598 if (rc)
5599 return rc;
5600
5a77e75f 5601 if (smb3_encryption_required(tcon))
7fb8986e
PS
5602 flags |= CIFS_TRANSFORM_REQ;
5603
983c88a4
PS
5604 req->VolatileFid = volatile_fid;
5605 req->PersistentFid = persistent_fid;
5606 req->OplockLevel = oplock_level;
0d35e382 5607 req->hdr.CreditRequest = cpu_to_le16(1);
983c88a4 5608
392e1c5d 5609 flags |= CIFS_NO_RSP_BUF;
21ad9487
RS
5610
5611 iov[0].iov_base = (char *)req;
5612 iov[0].iov_len = total_len;
5613
40eff45b
RS
5614 memset(&rqst, 0, sizeof(struct smb_rqst));
5615 rqst.rq_iov = iov;
5616 rqst.rq_nvec = 1;
5617
4f1fffa2
SP
5618 if (retries)
5619 smb2_set_replay(server, &rqst);
5620
352d96f3
AA
5621 rc = cifs_send_recv(xid, ses, server,
5622 &rqst, &resp_buf_type, flags, &rsp_iov);
da502f7d 5623 cifs_small_buf_release(req);
983c88a4
PS
5624 if (rc) {
5625 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
f96637be 5626 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
983c88a4
PS
5627 }
5628
4f1fffa2
SP
5629 if (is_replayable_error(rc) &&
5630 smb2_should_replay(tcon, &retries, &cur_sleep))
5631 goto replay_again;
5632
983c88a4
PS
5633 return rc;
5634}
6fc05c25 5635
730928c8
RS
5636void
5637smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
5638 struct kstatfs *kst)
6fc05c25
PS
5639{
5640 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
5641 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
5642 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
42bec214
SP
5643 kst->f_bfree = kst->f_bavail =
5644 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
6fc05c25
PS
5645 return;
5646}
5647
2d304217
SF
5648static void
5649copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
5650 struct kstatfs *kst)
5651{
5652 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
5653 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
5654 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
5655 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
5656 kst->f_bavail = kst->f_bfree;
5657 else
5658 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
5659 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5660 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
5661 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5662 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
5663
5664 return;
5665}
2d304217 5666
6fc05c25 5667static int
352d96f3
AA
5668build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon,
5669 struct TCP_Server_Info *server,
5670 int level, int outbuf_len, u64 persistent_fid,
5671 u64 volatile_fid)
6fc05c25
PS
5672{
5673 int rc;
5674 struct smb2_query_info_req *req;
b2fb7fec 5675 unsigned int total_len;
6fc05c25 5676
f96637be 5677 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
6fc05c25 5678
352d96f3 5679 if ((tcon->ses == NULL) || server == NULL)
6fc05c25
PS
5680 return -EIO;
5681
352d96f3
AA
5682 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
5683 (void **) &req, &total_len);
6fc05c25
PS
5684 if (rc)
5685 return rc;
5686
5687 req->InfoType = SMB2_O_INFO_FILESYSTEM;
5688 req->FileInfoClass = level;
5689 req->PersistentFileId = persistent_fid;
5690 req->VolatileFileId = volatile_fid;
b2fb7fec 5691 /* 1 for pad */
6fc05c25 5692 req->InputBufferOffset =
eb3e28c1 5693 cpu_to_le16(sizeof(struct smb2_query_info_req));
6fc05c25 5694 req->OutputBufferLength = cpu_to_le32(
eb3e28c1 5695 outbuf_len + sizeof(struct smb2_query_info_rsp));
6fc05c25
PS
5696
5697 iov->iov_base = (char *)req;
b2fb7fec 5698 iov->iov_len = total_len;
6fc05c25
PS
5699 return 0;
5700}
5701
33eae65c
PA
5702static inline void free_qfs_info_req(struct kvec *iov)
5703{
5704 cifs_buf_release(iov->iov_base);
5705}
5706
2d304217
SF
5707int
5708SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
5709 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5710{
5711 struct smb_rqst rqst;
5712 struct smb2_query_info_rsp *rsp = NULL;
5713 struct kvec iov;
5714 struct kvec rsp_iov;
5715 int rc = 0;
5716 int resp_buftype;
5717 struct cifs_ses *ses = tcon->ses;
4f1fffa2 5718 struct TCP_Server_Info *server;
2d304217
SF
5719 FILE_SYSTEM_POSIX_INFO *info = NULL;
5720 int flags = 0;
4f1fffa2
SP
5721 int retries = 0, cur_sleep = 1;
5722
5723replay_again:
5724 /* reinitialize for possible replay */
5725 flags = 0;
5726 server = cifs_pick_channel(ses);
2d304217 5727
352d96f3
AA
5728 rc = build_qfs_info_req(&iov, tcon, server,
5729 FS_POSIX_INFORMATION,
2d304217
SF
5730 sizeof(FILE_SYSTEM_POSIX_INFO),
5731 persistent_fid, volatile_fid);
5732 if (rc)
5733 return rc;
5734
5735 if (smb3_encryption_required(tcon))
5736 flags |= CIFS_TRANSFORM_REQ;
5737
5738 memset(&rqst, 0, sizeof(struct smb_rqst));
5739 rqst.rq_iov = &iov;
5740 rqst.rq_nvec = 1;
5741
4f1fffa2
SP
5742 if (retries)
5743 smb2_set_replay(server, &rqst);
5744
352d96f3
AA
5745 rc = cifs_send_recv(xid, ses, server,
5746 &rqst, &resp_buftype, flags, &rsp_iov);
33eae65c 5747 free_qfs_info_req(&iov);
2d304217
SF
5748 if (rc) {
5749 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5750 goto posix_qfsinf_exit;
5751 }
5752 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5753
5754 info = (FILE_SYSTEM_POSIX_INFO *)(
5755 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
730928c8
RS
5756 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5757 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5758 sizeof(FILE_SYSTEM_POSIX_INFO));
2d304217
SF
5759 if (!rc)
5760 copy_posix_fs_info_to_kstatfs(info, fsdata);
5761
5762posix_qfsinf_exit:
5763 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4f1fffa2
SP
5764
5765 if (is_replayable_error(rc) &&
5766 smb2_should_replay(tcon, &retries, &cur_sleep))
5767 goto replay_again;
5768
2d304217
SF
5769 return rc;
5770}
2d304217 5771
6fc05c25
PS
5772int
5773SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
5774 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5775{
40eff45b 5776 struct smb_rqst rqst;
6fc05c25
PS
5777 struct smb2_query_info_rsp *rsp = NULL;
5778 struct kvec iov;
da502f7d 5779 struct kvec rsp_iov;
6fc05c25
PS
5780 int rc = 0;
5781 int resp_buftype;
5782 struct cifs_ses *ses = tcon->ses;
4f1fffa2 5783 struct TCP_Server_Info *server;
6fc05c25 5784 struct smb2_fs_full_size_info *info = NULL;
7fb8986e 5785 int flags = 0;
4f1fffa2
SP
5786 int retries = 0, cur_sleep = 1;
5787
5788replay_again:
5789 /* reinitialize for possible replay */
5790 flags = 0;
5791 server = cifs_pick_channel(ses);
6fc05c25 5792
352d96f3
AA
5793 rc = build_qfs_info_req(&iov, tcon, server,
5794 FS_FULL_SIZE_INFORMATION,
6fc05c25
PS
5795 sizeof(struct smb2_fs_full_size_info),
5796 persistent_fid, volatile_fid);
5797 if (rc)
5798 return rc;
5799
5a77e75f 5800 if (smb3_encryption_required(tcon))
7fb8986e
PS
5801 flags |= CIFS_TRANSFORM_REQ;
5802
40eff45b
RS
5803 memset(&rqst, 0, sizeof(struct smb_rqst));
5804 rqst.rq_iov = &iov;
5805 rqst.rq_nvec = 1;
5806
4f1fffa2
SP
5807 if (retries)
5808 smb2_set_replay(server, &rqst);
5809
352d96f3
AA
5810 rc = cifs_send_recv(xid, ses, server,
5811 &rqst, &resp_buftype, flags, &rsp_iov);
33eae65c 5812 free_qfs_info_req(&iov);
6fc05c25
PS
5813 if (rc) {
5814 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
34f62640 5815 goto qfsinf_exit;
6fc05c25 5816 }
da502f7d 5817 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
6fc05c25 5818
1fc6ad2f 5819 info = (struct smb2_fs_full_size_info *)(
49f466bd 5820 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
730928c8
RS
5821 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5822 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5823 sizeof(struct smb2_fs_full_size_info));
6fc05c25 5824 if (!rc)
730928c8 5825 smb2_copy_fs_info_to_kstatfs(info, fsdata);
6fc05c25 5826
34f62640 5827qfsinf_exit:
da502f7d 5828 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4f1fffa2
SP
5829
5830 if (is_replayable_error(rc) &&
5831 smb2_should_replay(tcon, &retries, &cur_sleep))
5832 goto replay_again;
5833
34f62640
SF
5834 return rc;
5835}
5836
5837int
5838SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
2167114c 5839 u64 persistent_fid, u64 volatile_fid, int level)
34f62640 5840{
40eff45b 5841 struct smb_rqst rqst;
34f62640
SF
5842 struct smb2_query_info_rsp *rsp = NULL;
5843 struct kvec iov;
da502f7d 5844 struct kvec rsp_iov;
34f62640 5845 int rc = 0;
2167114c 5846 int resp_buftype, max_len, min_len;
34f62640 5847 struct cifs_ses *ses = tcon->ses;
4f1fffa2 5848 struct TCP_Server_Info *server;
34f62640 5849 unsigned int rsp_len, offset;
7fb8986e 5850 int flags = 0;
4f1fffa2
SP
5851 int retries = 0, cur_sleep = 1;
5852
5853replay_again:
5854 /* reinitialize for possible replay */
5855 flags = 0;
5856 server = cifs_pick_channel(ses);
34f62640 5857
2167114c
SF
5858 if (level == FS_DEVICE_INFORMATION) {
5859 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5860 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5861 } else if (level == FS_ATTRIBUTE_INFORMATION) {
5862 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
5863 min_len = MIN_FS_ATTR_INFO_SIZE;
af6a12ea
SF
5864 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
5865 max_len = sizeof(struct smb3_fs_ss_info);
5866 min_len = sizeof(struct smb3_fs_ss_info);
21ba3845
SF
5867 } else if (level == FS_VOLUME_INFORMATION) {
5868 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
5869 min_len = sizeof(struct smb3_fs_vol_info);
2167114c 5870 } else {
af6a12ea 5871 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
2167114c
SF
5872 return -EINVAL;
5873 }
5874
352d96f3
AA
5875 rc = build_qfs_info_req(&iov, tcon, server,
5876 level, max_len,
34f62640
SF
5877 persistent_fid, volatile_fid);
5878 if (rc)
5879 return rc;
5880
5a77e75f 5881 if (smb3_encryption_required(tcon))
7fb8986e
PS
5882 flags |= CIFS_TRANSFORM_REQ;
5883
40eff45b
RS
5884 memset(&rqst, 0, sizeof(struct smb_rqst));
5885 rqst.rq_iov = &iov;
5886 rqst.rq_nvec = 1;
5887
4f1fffa2
SP
5888 if (retries)
5889 smb2_set_replay(server, &rqst);
5890
352d96f3
AA
5891 rc = cifs_send_recv(xid, ses, server,
5892 &rqst, &resp_buftype, flags, &rsp_iov);
33eae65c 5893 free_qfs_info_req(&iov);
34f62640
SF
5894 if (rc) {
5895 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5896 goto qfsattr_exit;
5897 }
da502f7d 5898 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
34f62640
SF
5899
5900 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
5901 offset = le16_to_cpu(rsp->OutputBufferOffset);
730928c8 5902 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
2167114c
SF
5903 if (rc)
5904 goto qfsattr_exit;
5905
5906 if (level == FS_ATTRIBUTE_INFORMATION)
1fc6ad2f 5907 memcpy(&tcon->fsAttrInfo, offset
49f466bd 5908 + (char *)rsp, min_t(unsigned int,
2167114c
SF
5909 rsp_len, max_len));
5910 else if (level == FS_DEVICE_INFORMATION)
1fc6ad2f 5911 memcpy(&tcon->fsDevInfo, offset
49f466bd 5912 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
af6a12ea
SF
5913 else if (level == FS_SECTOR_SIZE_INFORMATION) {
5914 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
1fc6ad2f 5915 (offset + (char *)rsp);
af6a12ea
SF
5916 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
5917 tcon->perf_sector_size =
5918 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
21ba3845
SF
5919 } else if (level == FS_VOLUME_INFORMATION) {
5920 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
5921 (offset + (char *)rsp);
5922 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
5923 tcon->vol_create_time = vol_info->VolumeCreationTime;
af6a12ea 5924 }
34f62640
SF
5925
5926qfsattr_exit:
da502f7d 5927 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4f1fffa2
SP
5928
5929 if (is_replayable_error(rc) &&
5930 smb2_should_replay(tcon, &retries, &cur_sleep))
5931 goto replay_again;
5932
6fc05c25
PS
5933 return rc;
5934}
f7ba7fe6
PS
5935
5936int
5937smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
5938 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5939 const __u32 num_lock, struct smb2_lock_element *buf)
5940{
40eff45b 5941 struct smb_rqst rqst;
f7ba7fe6
PS
5942 int rc = 0;
5943 struct smb2_lock_req *req = NULL;
5944 struct kvec iov[2];
da502f7d 5945 struct kvec rsp_iov;
f7ba7fe6
PS
5946 int resp_buf_type;
5947 unsigned int count;
392e1c5d 5948 int flags = CIFS_NO_RSP_BUF;
ced93679 5949 unsigned int total_len;
4f1fffa2
SP
5950 struct TCP_Server_Info *server;
5951 int retries = 0, cur_sleep = 1;
5952
5953replay_again:
5954 /* reinitialize for possible replay */
5955 flags = CIFS_NO_RSP_BUF;
5956 server = cifs_pick_channel(tcon->ses);
f7ba7fe6 5957
f96637be 5958 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
f7ba7fe6 5959
352d96f3
AA
5960 rc = smb2_plain_req_init(SMB2_LOCK, tcon, server,
5961 (void **) &req, &total_len);
f7ba7fe6
PS
5962 if (rc)
5963 return rc;
5964
5a77e75f 5965 if (smb3_encryption_required(tcon))
7fb8986e
PS
5966 flags |= CIFS_TRANSFORM_REQ;
5967
0d35e382 5968 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
f7ba7fe6
PS
5969 req->LockCount = cpu_to_le16(num_lock);
5970
5971 req->PersistentFileId = persist_fid;
5972 req->VolatileFileId = volatile_fid;
5973
5974 count = num_lock * sizeof(struct smb2_lock_element);
f7ba7fe6
PS
5975
5976 iov[0].iov_base = (char *)req;
ced93679 5977 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
f7ba7fe6
PS
5978 iov[1].iov_base = (char *)buf;
5979 iov[1].iov_len = count;
5980
5981 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
40eff45b
RS
5982
5983 memset(&rqst, 0, sizeof(struct smb_rqst));
5984 rqst.rq_iov = iov;
5985 rqst.rq_nvec = 2;
5986
4f1fffa2
SP
5987 if (retries)
5988 smb2_set_replay(server, &rqst);
5989
352d96f3
AA
5990 rc = cifs_send_recv(xid, tcon->ses, server,
5991 &rqst, &resp_buf_type, flags,
ced93679 5992 &rsp_iov);
da502f7d 5993 cifs_small_buf_release(req);
f7ba7fe6 5994 if (rc) {
f96637be 5995 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
f7ba7fe6 5996 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
eccb4422
SF
5997 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
5998 tcon->ses->Suid, rc);
f7ba7fe6
PS
5999 }
6000
4f1fffa2
SP
6001 if (is_replayable_error(rc) &&
6002 smb2_should_replay(tcon, &retries, &cur_sleep))
6003 goto replay_again;
6004
f7ba7fe6
PS
6005 return rc;
6006}
6007
6008int
6009SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
6010 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
6011 const __u64 length, const __u64 offset, const __u32 lock_flags,
6012 const bool wait)
6013{
6014 struct smb2_lock_element lock;
6015
6016 lock.Offset = cpu_to_le64(offset);
6017 lock.Length = cpu_to_le64(length);
6018 lock.Flags = cpu_to_le32(lock_flags);
6019 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
6020 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
6021
6022 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
6023}
0822f514
PS
6024
6025int
6026SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
6027 __u8 *lease_key, const __le32 lease_state)
6028{
40eff45b 6029 struct smb_rqst rqst;
0822f514
PS
6030 int rc;
6031 struct smb2_lease_ack *req = NULL;
8eb7998e 6032 struct cifs_ses *ses = tcon->ses;
7fb8986e 6033 int flags = CIFS_OBREAK_OP;
8eb7998e
RS
6034 unsigned int total_len;
6035 struct kvec iov[1];
6036 struct kvec rsp_iov;
6037 int resp_buf_type;
179e44d4
SF
6038 __u64 *please_key_high;
6039 __u64 *please_key_low;
352d96f3 6040 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
0822f514 6041
f96637be 6042 cifs_dbg(FYI, "SMB2_lease_break\n");
352d96f3
AA
6043 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
6044 (void **) &req, &total_len);
0822f514
PS
6045 if (rc)
6046 return rc;
6047
5a77e75f 6048 if (smb3_encryption_required(tcon))
7fb8986e
PS
6049 flags |= CIFS_TRANSFORM_REQ;
6050
0d35e382 6051 req->hdr.CreditRequest = cpu_to_le16(1);
0822f514 6052 req->StructureSize = cpu_to_le16(36);
8eb7998e 6053 total_len += 12;
0822f514
PS
6054
6055 memcpy(req->LeaseKey, lease_key, 16);
6056 req->LeaseState = lease_state;
6057
392e1c5d 6058 flags |= CIFS_NO_RSP_BUF;
8eb7998e
RS
6059
6060 iov[0].iov_base = (char *)req;
6061 iov[0].iov_len = total_len;
6062
40eff45b
RS
6063 memset(&rqst, 0, sizeof(struct smb_rqst));
6064 rqst.rq_iov = iov;
6065 rqst.rq_nvec = 1;
6066
352d96f3
AA
6067 rc = cifs_send_recv(xid, ses, server,
6068 &rqst, &resp_buf_type, flags, &rsp_iov);
da502f7d 6069 cifs_small_buf_release(req);
0822f514 6070
d339adc1
AA
6071 please_key_low = (__u64 *)lease_key;
6072 please_key_high = (__u64 *)(lease_key+8);
0822f514
PS
6073 if (rc) {
6074 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
179e44d4
SF
6075 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
6076 ses->Suid, *please_key_low, *please_key_high, rc);
f96637be 6077 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
179e44d4
SF
6078 } else
6079 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
6080 ses->Suid, *please_key_low, *please_key_high);
0822f514
PS
6081
6082 return rc;
6083}
This page took 1.940831 seconds and 4 git commands to generate.