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