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