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