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