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