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