fix a series of Documentation/ broken file name references
[linux-2.6/btrfs-unstable.git] / fs / cifs / smb2pdu.c
blobaf032e1a3eac7adaf0570f5923e0ba6164e8ed6b
1 /*
2 * fs/cifs/smb2pdu.c
4 * Copyright (C) International Business Machines Corp., 2009, 2013
5 * Etersoft, 2012
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
9 * Contains the routines for constructing the SMB2 PDUs themselves
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.
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.
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
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 */
31 #include <linux/fs.h>
32 #include <linux/kernel.h>
33 #include <linux/vfs.h>
34 #include <linux/task_io_accounting_ops.h>
35 #include <linux/uaccess.h>
36 #include <linux/uuid.h>
37 #include <linux/pagemap.h>
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"
48 #include "smb2glob.h"
49 #include "cifspdu.h"
50 #include "cifs_spnego.h"
51 #include "smbdirect.h"
52 #include "trace.h"
55 * The following table defines the expected "StructureSize" of SMB2 requests
56 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
58 * Note that commands are defined in smb2pdu.h in le16 but the array below is
59 * indexed by command in host byte order.
61 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
62 /* SMB2_NEGOTIATE */ 36,
63 /* SMB2_SESSION_SETUP */ 25,
64 /* SMB2_LOGOFF */ 4,
65 /* SMB2_TREE_CONNECT */ 9,
66 /* SMB2_TREE_DISCONNECT */ 4,
67 /* SMB2_CREATE */ 57,
68 /* SMB2_CLOSE */ 24,
69 /* SMB2_FLUSH */ 24,
70 /* SMB2_READ */ 49,
71 /* SMB2_WRITE */ 49,
72 /* SMB2_LOCK */ 48,
73 /* SMB2_IOCTL */ 57,
74 /* SMB2_CANCEL */ 4,
75 /* SMB2_ECHO */ 4,
76 /* SMB2_QUERY_DIRECTORY */ 33,
77 /* SMB2_CHANGE_NOTIFY */ 32,
78 /* SMB2_QUERY_INFO */ 41,
79 /* SMB2_SET_INFO */ 33,
80 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
83 static int smb3_encryption_required(const struct cifs_tcon *tcon)
85 if (!tcon)
86 return 0;
87 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
88 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
89 return 1;
90 if (tcon->seal &&
91 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
92 return 1;
93 return 0;
96 static void
97 smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
98 const struct cifs_tcon *tcon)
100 shdr->ProtocolId = SMB2_PROTO_NUMBER;
101 shdr->StructureSize = cpu_to_le16(64);
102 shdr->Command = smb2_cmd;
103 if (tcon && tcon->ses && tcon->ses->server) {
104 struct TCP_Server_Info *server = tcon->ses->server;
106 spin_lock(&server->req_lock);
107 /* Request up to 2 credits but don't go over the limit. */
108 if (server->credits >= server->max_credits)
109 shdr->CreditRequest = cpu_to_le16(0);
110 else
111 shdr->CreditRequest = cpu_to_le16(
112 min_t(int, server->max_credits -
113 server->credits, 2));
114 spin_unlock(&server->req_lock);
115 } else {
116 shdr->CreditRequest = cpu_to_le16(2);
118 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
120 if (!tcon)
121 goto out;
123 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
124 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
125 if ((tcon->ses) && (tcon->ses->server) &&
126 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
127 shdr->CreditCharge = cpu_to_le16(1);
128 /* else CreditCharge MBZ */
130 shdr->TreeId = tcon->tid;
131 /* Uid is not converted */
132 if (tcon->ses)
133 shdr->SessionId = tcon->ses->Suid;
136 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
137 * to pass the path on the Open SMB prefixed by \\server\share.
138 * Not sure when we would need to do the augmented path (if ever) and
139 * setting this flag breaks the SMB2 open operation since it is
140 * illegal to send an empty path name (without \\server\share prefix)
141 * when the DFS flag is set in the SMB open header. We could
142 * consider setting the flag on all operations other than open
143 * but it is safer to net set it for now.
145 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
146 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
148 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
149 !smb3_encryption_required(tcon))
150 shdr->Flags |= SMB2_FLAGS_SIGNED;
151 out:
152 return;
155 static int
156 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
158 int rc = 0;
159 struct nls_table *nls_codepage;
160 struct cifs_ses *ses;
161 struct TCP_Server_Info *server;
164 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
165 * check for tcp and smb session status done differently
166 * for those three - in the calling routine.
168 if (tcon == NULL)
169 return rc;
171 if (smb2_command == SMB2_TREE_CONNECT)
172 return rc;
174 if (tcon->tidStatus == CifsExiting) {
176 * only tree disconnect, open, and write,
177 * (and ulogoff which does not have tcon)
178 * are allowed as we start force umount.
180 if ((smb2_command != SMB2_WRITE) &&
181 (smb2_command != SMB2_CREATE) &&
182 (smb2_command != SMB2_TREE_DISCONNECT)) {
183 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
184 smb2_command);
185 return -ENODEV;
188 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
189 (!tcon->ses->server))
190 return -EIO;
192 ses = tcon->ses;
193 server = ses->server;
196 * Give demultiplex thread up to 10 seconds to reconnect, should be
197 * greater than cifs socket timeout which is 7 seconds
199 while (server->tcpStatus == CifsNeedReconnect) {
201 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
202 * here since they are implicitly done when session drops.
204 switch (smb2_command) {
206 * BB Should we keep oplock break and add flush to exceptions?
208 case SMB2_TREE_DISCONNECT:
209 case SMB2_CANCEL:
210 case SMB2_CLOSE:
211 case SMB2_OPLOCK_BREAK:
212 return -EAGAIN;
215 wait_event_interruptible_timeout(server->response_q,
216 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
218 /* are we still trying to reconnect? */
219 if (server->tcpStatus != CifsNeedReconnect)
220 break;
223 * on "soft" mounts we wait once. Hard mounts keep
224 * retrying until process is killed or server comes
225 * back on-line
227 if (!tcon->retry) {
228 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
229 return -EHOSTDOWN;
233 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
234 return rc;
236 nls_codepage = load_nls_default();
239 * need to prevent multiple threads trying to simultaneously reconnect
240 * the same SMB session
242 mutex_lock(&tcon->ses->session_mutex);
245 * Recheck after acquire mutex. If another thread is negotiating
246 * and the server never sends an answer the socket will be closed
247 * and tcpStatus set to reconnect.
249 if (server->tcpStatus == CifsNeedReconnect) {
250 rc = -EHOSTDOWN;
251 mutex_unlock(&tcon->ses->session_mutex);
252 goto out;
255 rc = cifs_negotiate_protocol(0, tcon->ses);
256 if (!rc && tcon->ses->need_reconnect)
257 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
259 if (rc || !tcon->need_reconnect) {
260 mutex_unlock(&tcon->ses->session_mutex);
261 goto out;
264 cifs_mark_open_files_invalid(tcon);
265 if (tcon->use_persistent)
266 tcon->need_reopen_files = true;
268 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
269 mutex_unlock(&tcon->ses->session_mutex);
271 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
272 if (rc) {
273 /* If sess reconnected but tcon didn't, something strange ... */
274 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
275 goto out;
278 if (smb2_command != SMB2_INTERNAL_CMD)
279 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
281 atomic_inc(&tconInfoReconnectCount);
282 out:
284 * Check if handle based operation so we know whether we can continue
285 * or not without returning to caller to reset file handle.
288 * BB Is flush done by server on drop of tcp session? Should we special
289 * case it and skip above?
291 switch (smb2_command) {
292 case SMB2_FLUSH:
293 case SMB2_READ:
294 case SMB2_WRITE:
295 case SMB2_LOCK:
296 case SMB2_IOCTL:
297 case SMB2_QUERY_DIRECTORY:
298 case SMB2_CHANGE_NOTIFY:
299 case SMB2_QUERY_INFO:
300 case SMB2_SET_INFO:
301 rc = -EAGAIN;
303 unload_nls(nls_codepage);
304 return rc;
307 static void
308 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
309 unsigned int *total_len)
311 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
312 /* lookup word count ie StructureSize from table */
313 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
316 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
317 * largest operations (Create)
319 memset(buf, 0, 256);
321 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
322 spdu->StructureSize2 = cpu_to_le16(parmsize);
324 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
328 * Allocate and return pointer to an SMB request hdr, and set basic
329 * SMB information in the SMB header. If the return code is zero, this
330 * function must have filled in request_buf pointer.
332 static int
333 smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
334 void **request_buf, unsigned int *total_len)
336 int rc;
338 rc = smb2_reconnect(smb2_command, tcon);
339 if (rc)
340 return rc;
342 /* BB eventually switch this to SMB2 specific small buf size */
343 *request_buf = cifs_small_buf_get();
344 if (*request_buf == NULL) {
345 /* BB should we add a retry in here if not a writepage? */
346 return -ENOMEM;
349 fill_small_buf(smb2_command, tcon,
350 (struct smb2_sync_hdr *)(*request_buf),
351 total_len);
353 if (tcon != NULL) {
354 #ifdef CONFIG_CIFS_STATS2
355 uint16_t com_code = le16_to_cpu(smb2_command);
356 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
357 #endif
358 cifs_stats_inc(&tcon->num_smbs_sent);
361 return rc;
364 #ifdef CONFIG_CIFS_SMB311
365 /* offset is sizeof smb2_negotiate_req but rounded up to 8 bytes */
366 #define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) */
369 #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
370 #define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
371 #define SMB2_POSIX_EXTENSIONS_AVAILABLE cpu_to_le16(0x100)
373 static void
374 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
376 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
377 pneg_ctxt->DataLength = cpu_to_le16(38);
378 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
379 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
380 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
381 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
384 static void
385 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
387 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
388 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + le16 cipher */
389 pneg_ctxt->CipherCount = cpu_to_le16(1);
390 /* pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;*/ /* not supported yet */
391 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_CCM;
394 static void
395 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
397 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
398 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
401 static void
402 assemble_neg_contexts(struct smb2_negotiate_req *req,
403 unsigned int *total_len)
405 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT;
406 unsigned int ctxt_len;
408 *total_len += 2; /* Add 2 due to round to 8 byte boundary for 1st ctxt */
409 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
410 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
411 *total_len += ctxt_len;
412 pneg_ctxt += ctxt_len;
414 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
415 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
416 *total_len += ctxt_len;
417 pneg_ctxt += ctxt_len;
419 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
420 *total_len += sizeof(struct smb2_posix_neg_context);
422 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
423 req->NegotiateContextCount = cpu_to_le16(3);
426 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
428 unsigned int len = le16_to_cpu(ctxt->DataLength);
430 /* If invalid preauth context warn but use what we requested, SHA-512 */
431 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
432 printk_once(KERN_WARNING "server sent bad preauth context\n");
433 return;
435 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
436 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
437 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
438 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
441 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
442 struct smb2_encryption_neg_context *ctxt)
444 unsigned int len = le16_to_cpu(ctxt->DataLength);
446 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
447 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
448 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
449 return -EINVAL;
452 if (le16_to_cpu(ctxt->CipherCount) != 1) {
453 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
454 return -EINVAL;
456 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
457 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
458 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
459 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
460 return -EINVAL;
462 server->cipher_type = ctxt->Ciphers[0];
463 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
464 return 0;
467 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
468 struct TCP_Server_Info *server,
469 unsigned int len_of_smb)
471 struct smb2_neg_context *pctx;
472 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
473 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
474 unsigned int len_of_ctxts, i;
475 int rc = 0;
477 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
478 if (len_of_smb <= offset) {
479 cifs_dbg(VFS, "Invalid response: negotiate context offset\n");
480 return -EINVAL;
483 len_of_ctxts = len_of_smb - offset;
485 for (i = 0; i < ctxt_cnt; i++) {
486 int clen;
487 /* check that offset is not beyond end of SMB */
488 if (len_of_ctxts == 0)
489 break;
491 if (len_of_ctxts < sizeof(struct smb2_neg_context))
492 break;
494 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
495 clen = le16_to_cpu(pctx->DataLength);
496 if (clen > len_of_ctxts)
497 break;
499 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
500 decode_preauth_context(
501 (struct smb2_preauth_neg_context *)pctx);
502 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
503 rc = decode_encrypt_ctx(server,
504 (struct smb2_encryption_neg_context *)pctx);
505 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
506 server->posix_ext_supported = true;
507 else
508 cifs_dbg(VFS, "unknown negcontext of type %d ignored\n",
509 le16_to_cpu(pctx->ContextType));
511 if (rc)
512 break;
513 /* offsets must be 8 byte aligned */
514 clen = (clen + 7) & ~0x7;
515 offset += clen + sizeof(struct smb2_neg_context);
516 len_of_ctxts -= clen;
518 return rc;
521 static struct create_posix *
522 create_posix_buf(umode_t mode)
524 struct create_posix *buf;
526 buf = kzalloc(sizeof(struct create_posix),
527 GFP_KERNEL);
528 if (!buf)
529 return NULL;
531 buf->ccontext.DataOffset =
532 cpu_to_le16(offsetof(struct create_posix, Mode));
533 buf->ccontext.DataLength = cpu_to_le32(4);
534 buf->ccontext.NameOffset =
535 cpu_to_le16(offsetof(struct create_posix, Name));
536 buf->ccontext.NameLength = cpu_to_le16(16);
538 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
539 buf->Name[0] = 0x93;
540 buf->Name[1] = 0xAD;
541 buf->Name[2] = 0x25;
542 buf->Name[3] = 0x50;
543 buf->Name[4] = 0x9C;
544 buf->Name[5] = 0xB4;
545 buf->Name[6] = 0x11;
546 buf->Name[7] = 0xE7;
547 buf->Name[8] = 0xB4;
548 buf->Name[9] = 0x23;
549 buf->Name[10] = 0x83;
550 buf->Name[11] = 0xDE;
551 buf->Name[12] = 0x96;
552 buf->Name[13] = 0x8B;
553 buf->Name[14] = 0xCD;
554 buf->Name[15] = 0x7C;
555 buf->Mode = cpu_to_le32(mode);
556 cifs_dbg(FYI, "mode on posix create 0%o", mode);
557 return buf;
560 static int
561 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
563 struct smb2_create_req *req = iov[0].iov_base;
564 unsigned int num = *num_iovec;
566 iov[num].iov_base = create_posix_buf(mode);
567 if (iov[num].iov_base == NULL)
568 return -ENOMEM;
569 iov[num].iov_len = sizeof(struct create_posix);
570 if (!req->CreateContextsOffset)
571 req->CreateContextsOffset = cpu_to_le32(
572 sizeof(struct smb2_create_req) +
573 iov[num - 1].iov_len);
574 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
575 *num_iovec = num + 1;
576 return 0;
579 #else
580 static void assemble_neg_contexts(struct smb2_negotiate_req *req,
581 unsigned int *total_len)
583 return;
585 #endif /* SMB311 */
589 * SMB2 Worker functions follow:
591 * The general structure of the worker functions is:
592 * 1) Call smb2_init (assembles SMB2 header)
593 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
594 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
595 * 4) Decode SMB2 command specific fields in the fixed length area
596 * 5) Decode variable length data area (if any for this SMB2 command type)
597 * 6) Call free smb buffer
598 * 7) return
603 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
605 struct smb2_negotiate_req *req;
606 struct smb2_negotiate_rsp *rsp;
607 struct kvec iov[1];
608 struct kvec rsp_iov;
609 int rc = 0;
610 int resp_buftype;
611 struct TCP_Server_Info *server = ses->server;
612 int blob_offset, blob_length;
613 char *security_blob;
614 int flags = CIFS_NEG_OP;
615 unsigned int total_len;
617 cifs_dbg(FYI, "Negotiate protocol\n");
619 if (!server) {
620 WARN(1, "%s: server is NULL!\n", __func__);
621 return -EIO;
624 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
625 if (rc)
626 return rc;
628 req->sync_hdr.SessionId = 0;
629 #ifdef CONFIG_CIFS_SMB311
630 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
631 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
632 #endif
634 if (strcmp(ses->server->vals->version_string,
635 SMB3ANY_VERSION_STRING) == 0) {
636 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
637 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
638 req->DialectCount = cpu_to_le16(2);
639 total_len += 4;
640 } else if (strcmp(ses->server->vals->version_string,
641 SMBDEFAULT_VERSION_STRING) == 0) {
642 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
643 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
644 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
645 req->DialectCount = cpu_to_le16(3);
646 total_len += 6;
647 } else {
648 /* otherwise send specific dialect */
649 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
650 req->DialectCount = cpu_to_le16(1);
651 total_len += 2;
654 /* only one of SMB2 signing flags may be set in SMB2 request */
655 if (ses->sign)
656 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
657 else if (global_secflags & CIFSSEC_MAY_SIGN)
658 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
659 else
660 req->SecurityMode = 0;
662 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
664 /* ClientGUID must be zero for SMB2.02 dialect */
665 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
666 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
667 else {
668 memcpy(req->ClientGUID, server->client_guid,
669 SMB2_CLIENT_GUID_SIZE);
670 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
671 assemble_neg_contexts(req, &total_len);
673 iov[0].iov_base = (char *)req;
674 iov[0].iov_len = total_len;
676 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
677 cifs_small_buf_release(req);
678 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
680 * No tcon so can't do
681 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
683 if (rc == -EOPNOTSUPP) {
684 cifs_dbg(VFS, "Dialect not supported by server. Consider "
685 "specifying vers=1.0 or vers=2.0 on mount for accessing"
686 " older servers\n");
687 goto neg_exit;
688 } else if (rc != 0)
689 goto neg_exit;
691 if (strcmp(ses->server->vals->version_string,
692 SMB3ANY_VERSION_STRING) == 0) {
693 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
694 cifs_dbg(VFS,
695 "SMB2 dialect returned but not requested\n");
696 return -EIO;
697 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
698 cifs_dbg(VFS,
699 "SMB2.1 dialect returned but not requested\n");
700 return -EIO;
702 } else if (strcmp(ses->server->vals->version_string,
703 SMBDEFAULT_VERSION_STRING) == 0) {
704 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
705 cifs_dbg(VFS,
706 "SMB2 dialect returned but not requested\n");
707 return -EIO;
708 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
709 /* ops set to 3.0 by default for default so update */
710 ses->server->ops = &smb21_operations;
712 } else if (le16_to_cpu(rsp->DialectRevision) !=
713 ses->server->vals->protocol_id) {
714 /* if requested single dialect ensure returned dialect matched */
715 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
716 le16_to_cpu(rsp->DialectRevision));
717 return -EIO;
720 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
722 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
723 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
724 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
725 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
726 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
727 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
728 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
729 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
730 #ifdef CONFIG_CIFS_SMB311
731 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
732 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
733 #endif /* SMB311 */
734 else {
735 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
736 le16_to_cpu(rsp->DialectRevision));
737 rc = -EIO;
738 goto neg_exit;
740 server->dialect = le16_to_cpu(rsp->DialectRevision);
742 /* BB: add check that dialect was valid given dialect(s) we asked for */
744 #ifdef CONFIG_CIFS_SMB311
746 * Keep a copy of the hash after negprot. This hash will be
747 * the starting hash value for all sessions made from this
748 * server.
750 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
751 SMB2_PREAUTH_HASH_SIZE);
752 #endif
753 /* SMB2 only has an extended negflavor */
754 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
755 /* set it to the maximum buffer size value we can send with 1 credit */
756 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
757 SMB2_MAX_BUFFER_SIZE);
758 server->max_read = le32_to_cpu(rsp->MaxReadSize);
759 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
760 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
761 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
762 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
763 server->sec_mode);
764 server->capabilities = le32_to_cpu(rsp->Capabilities);
765 /* Internal types */
766 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
768 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
769 (struct smb2_sync_hdr *)rsp);
771 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
772 * for us will be
773 * ses->sectype = RawNTLMSSP;
774 * but for time being this is our only auth choice so doesn't matter.
775 * We just found a server which sets blob length to zero expecting raw.
777 if (blob_length == 0) {
778 cifs_dbg(FYI, "missing security blob on negprot\n");
779 server->sec_ntlmssp = true;
782 rc = cifs_enable_signing(server, ses->sign);
783 if (rc)
784 goto neg_exit;
785 if (blob_length) {
786 rc = decode_negTokenInit(security_blob, blob_length, server);
787 if (rc == 1)
788 rc = 0;
789 else if (rc == 0)
790 rc = -EIO;
793 #ifdef CONFIG_CIFS_SMB311
794 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
795 if (rsp->NegotiateContextCount)
796 rc = smb311_decode_neg_context(rsp, server,
797 rsp_iov.iov_len);
798 else
799 cifs_dbg(VFS, "Missing expected negotiate contexts\n");
801 #endif /* CONFIG_CIFS_SMB311 */
802 neg_exit:
803 free_rsp_buf(resp_buftype, rsp);
804 return rc;
807 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
809 int rc;
810 struct validate_negotiate_info_req *pneg_inbuf;
811 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
812 u32 rsplen;
813 u32 inbuflen; /* max of 4 dialects */
815 cifs_dbg(FYI, "validate negotiate\n");
817 /* In SMB3.11 preauth integrity supersedes validate negotiate */
818 if (tcon->ses->server->dialect == SMB311_PROT_ID)
819 return 0;
822 * validation ioctl must be signed, so no point sending this if we
823 * can not sign it (ie are not known user). Even if signing is not
824 * required (enabled but not negotiated), in those cases we selectively
825 * sign just this, the first and only signed request on a connection.
826 * Having validation of negotiate info helps reduce attack vectors.
828 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
829 return 0; /* validation requires signing */
831 if (tcon->ses->user_name == NULL) {
832 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
833 return 0; /* validation requires signing */
836 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
837 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
839 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
840 if (!pneg_inbuf)
841 return -ENOMEM;
843 pneg_inbuf->Capabilities =
844 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
845 memcpy(pneg_inbuf->Guid, tcon->ses->server->client_guid,
846 SMB2_CLIENT_GUID_SIZE);
848 if (tcon->ses->sign)
849 pneg_inbuf->SecurityMode =
850 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
851 else if (global_secflags & CIFSSEC_MAY_SIGN)
852 pneg_inbuf->SecurityMode =
853 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
854 else
855 pneg_inbuf->SecurityMode = 0;
858 if (strcmp(tcon->ses->server->vals->version_string,
859 SMB3ANY_VERSION_STRING) == 0) {
860 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
861 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
862 pneg_inbuf->DialectCount = cpu_to_le16(2);
863 /* structure is big enough for 3 dialects, sending only 2 */
864 inbuflen = sizeof(*pneg_inbuf) -
865 sizeof(pneg_inbuf->Dialects[0]);
866 } else if (strcmp(tcon->ses->server->vals->version_string,
867 SMBDEFAULT_VERSION_STRING) == 0) {
868 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
869 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
870 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
871 pneg_inbuf->DialectCount = cpu_to_le16(3);
872 /* structure is big enough for 3 dialects */
873 inbuflen = sizeof(*pneg_inbuf);
874 } else {
875 /* otherwise specific dialect was requested */
876 pneg_inbuf->Dialects[0] =
877 cpu_to_le16(tcon->ses->server->vals->protocol_id);
878 pneg_inbuf->DialectCount = cpu_to_le16(1);
879 /* structure is big enough for 3 dialects, sending only 1 */
880 inbuflen = sizeof(*pneg_inbuf) -
881 sizeof(pneg_inbuf->Dialects[0]) * 2;
884 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
885 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
886 (char *)pneg_inbuf, inbuflen, (char **)&pneg_rsp, &rsplen);
888 if (rc != 0) {
889 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
890 rc = -EIO;
891 goto out_free_inbuf;
894 rc = -EIO;
895 if (rsplen != sizeof(*pneg_rsp)) {
896 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
897 rsplen);
899 /* relax check since Mac returns max bufsize allowed on ioctl */
900 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
901 goto out_free_rsp;
904 /* check validate negotiate info response matches what we got earlier */
905 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
906 goto vneg_out;
908 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
909 goto vneg_out;
911 /* do not validate server guid because not saved at negprot time yet */
913 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
914 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
915 goto vneg_out;
917 /* validate negotiate successful */
918 rc = 0;
919 cifs_dbg(FYI, "validate negotiate info successful\n");
920 goto out_free_rsp;
922 vneg_out:
923 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
924 out_free_rsp:
925 kfree(pneg_rsp);
926 out_free_inbuf:
927 kfree(pneg_inbuf);
928 return rc;
931 enum securityEnum
932 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
934 switch (requested) {
935 case Kerberos:
936 case RawNTLMSSP:
937 return requested;
938 case NTLMv2:
939 return RawNTLMSSP;
940 case Unspecified:
941 if (server->sec_ntlmssp &&
942 (global_secflags & CIFSSEC_MAY_NTLMSSP))
943 return RawNTLMSSP;
944 if ((server->sec_kerberos || server->sec_mskerberos) &&
945 (global_secflags & CIFSSEC_MAY_KRB5))
946 return Kerberos;
947 /* Fallthrough */
948 default:
949 return Unspecified;
953 struct SMB2_sess_data {
954 unsigned int xid;
955 struct cifs_ses *ses;
956 struct nls_table *nls_cp;
957 void (*func)(struct SMB2_sess_data *);
958 int result;
959 u64 previous_session;
961 /* we will send the SMB in three pieces:
962 * a fixed length beginning part, an optional
963 * SPNEGO blob (which can be zero length), and a
964 * last part which will include the strings
965 * and rest of bcc area. This allows us to avoid
966 * a large buffer 17K allocation
968 int buf0_type;
969 struct kvec iov[2];
972 static int
973 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
975 int rc;
976 struct cifs_ses *ses = sess_data->ses;
977 struct smb2_sess_setup_req *req;
978 struct TCP_Server_Info *server = ses->server;
979 unsigned int total_len;
981 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
982 &total_len);
983 if (rc)
984 return rc;
986 /* First session, not a reauthenticate */
987 req->sync_hdr.SessionId = 0;
989 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
990 req->PreviousSessionId = sess_data->previous_session;
992 req->Flags = 0; /* MBZ */
993 /* to enable echos and oplocks */
994 req->sync_hdr.CreditRequest = cpu_to_le16(3);
996 /* only one of SMB2 signing flags may be set in SMB2 request */
997 if (server->sign)
998 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
999 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1000 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1001 else
1002 req->SecurityMode = 0;
1004 req->Capabilities = 0;
1005 req->Channel = 0; /* MBZ */
1007 sess_data->iov[0].iov_base = (char *)req;
1008 /* 1 for pad */
1009 sess_data->iov[0].iov_len = total_len - 1;
1011 * This variable will be used to clear the buffer
1012 * allocated above in case of any error in the calling function.
1014 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1016 return 0;
1019 static void
1020 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1022 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1023 sess_data->buf0_type = CIFS_NO_BUFFER;
1026 static int
1027 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1029 int rc;
1030 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1031 struct kvec rsp_iov = { NULL, 0 };
1033 /* Testing shows that buffer offset must be at location of Buffer[0] */
1034 req->SecurityBufferOffset =
1035 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
1036 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1038 /* BB add code to build os and lm fields */
1040 rc = smb2_send_recv(sess_data->xid, sess_data->ses,
1041 sess_data->iov, 2,
1042 &sess_data->buf0_type,
1043 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
1044 cifs_small_buf_release(sess_data->iov[0].iov_base);
1045 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1047 return rc;
1050 static int
1051 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1053 int rc = 0;
1054 struct cifs_ses *ses = sess_data->ses;
1056 mutex_lock(&ses->server->srv_mutex);
1057 if (ses->server->ops->generate_signingkey) {
1058 rc = ses->server->ops->generate_signingkey(ses);
1059 if (rc) {
1060 cifs_dbg(FYI,
1061 "SMB3 session key generation failed\n");
1062 mutex_unlock(&ses->server->srv_mutex);
1063 return rc;
1066 if (!ses->server->session_estab) {
1067 ses->server->sequence_number = 0x2;
1068 ses->server->session_estab = true;
1070 mutex_unlock(&ses->server->srv_mutex);
1072 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1073 spin_lock(&GlobalMid_Lock);
1074 ses->status = CifsGood;
1075 ses->need_reconnect = false;
1076 spin_unlock(&GlobalMid_Lock);
1077 return rc;
1080 #ifdef CONFIG_CIFS_UPCALL
1081 static void
1082 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1084 int rc;
1085 struct cifs_ses *ses = sess_data->ses;
1086 struct cifs_spnego_msg *msg;
1087 struct key *spnego_key = NULL;
1088 struct smb2_sess_setup_rsp *rsp = NULL;
1090 rc = SMB2_sess_alloc_buffer(sess_data);
1091 if (rc)
1092 goto out;
1094 spnego_key = cifs_get_spnego_key(ses);
1095 if (IS_ERR(spnego_key)) {
1096 rc = PTR_ERR(spnego_key);
1097 spnego_key = NULL;
1098 goto out;
1101 msg = spnego_key->payload.data[0];
1103 * check version field to make sure that cifs.upcall is
1104 * sending us a response in an expected form
1106 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1107 cifs_dbg(VFS,
1108 "bad cifs.upcall version. Expected %d got %d",
1109 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1110 rc = -EKEYREJECTED;
1111 goto out_put_spnego_key;
1114 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1115 GFP_KERNEL);
1116 if (!ses->auth_key.response) {
1117 cifs_dbg(VFS,
1118 "Kerberos can't allocate (%u bytes) memory",
1119 msg->sesskey_len);
1120 rc = -ENOMEM;
1121 goto out_put_spnego_key;
1123 ses->auth_key.len = msg->sesskey_len;
1125 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1126 sess_data->iov[1].iov_len = msg->secblob_len;
1128 rc = SMB2_sess_sendreceive(sess_data);
1129 if (rc)
1130 goto out_put_spnego_key;
1132 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1133 ses->Suid = rsp->sync_hdr.SessionId;
1135 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1137 rc = SMB2_sess_establish_session(sess_data);
1138 out_put_spnego_key:
1139 key_invalidate(spnego_key);
1140 key_put(spnego_key);
1141 out:
1142 sess_data->result = rc;
1143 sess_data->func = NULL;
1144 SMB2_sess_free_buffer(sess_data);
1146 #else
1147 static void
1148 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1150 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1151 sess_data->result = -EOPNOTSUPP;
1152 sess_data->func = NULL;
1154 #endif
1156 static void
1157 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1159 static void
1160 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1162 int rc;
1163 struct cifs_ses *ses = sess_data->ses;
1164 struct smb2_sess_setup_rsp *rsp = NULL;
1165 char *ntlmssp_blob = NULL;
1166 bool use_spnego = false; /* else use raw ntlmssp */
1167 u16 blob_length = 0;
1170 * If memory allocation is successful, caller of this function
1171 * frees it.
1173 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1174 if (!ses->ntlmssp) {
1175 rc = -ENOMEM;
1176 goto out_err;
1178 ses->ntlmssp->sesskey_per_smbsess = true;
1180 rc = SMB2_sess_alloc_buffer(sess_data);
1181 if (rc)
1182 goto out_err;
1184 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1185 GFP_KERNEL);
1186 if (ntlmssp_blob == NULL) {
1187 rc = -ENOMEM;
1188 goto out;
1191 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1192 if (use_spnego) {
1193 /* BB eventually need to add this */
1194 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1195 rc = -EOPNOTSUPP;
1196 goto out;
1197 } else {
1198 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1199 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1201 sess_data->iov[1].iov_base = ntlmssp_blob;
1202 sess_data->iov[1].iov_len = blob_length;
1204 rc = SMB2_sess_sendreceive(sess_data);
1205 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1207 /* If true, rc here is expected and not an error */
1208 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1209 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1210 rc = 0;
1212 if (rc)
1213 goto out;
1215 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1216 le16_to_cpu(rsp->SecurityBufferOffset)) {
1217 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1218 le16_to_cpu(rsp->SecurityBufferOffset));
1219 rc = -EIO;
1220 goto out;
1222 rc = decode_ntlmssp_challenge(rsp->Buffer,
1223 le16_to_cpu(rsp->SecurityBufferLength), ses);
1224 if (rc)
1225 goto out;
1227 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1230 ses->Suid = rsp->sync_hdr.SessionId;
1231 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1233 out:
1234 kfree(ntlmssp_blob);
1235 SMB2_sess_free_buffer(sess_data);
1236 if (!rc) {
1237 sess_data->result = 0;
1238 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1239 return;
1241 out_err:
1242 kfree(ses->ntlmssp);
1243 ses->ntlmssp = NULL;
1244 sess_data->result = rc;
1245 sess_data->func = NULL;
1248 static void
1249 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1251 int rc;
1252 struct cifs_ses *ses = sess_data->ses;
1253 struct smb2_sess_setup_req *req;
1254 struct smb2_sess_setup_rsp *rsp = NULL;
1255 unsigned char *ntlmssp_blob = NULL;
1256 bool use_spnego = false; /* else use raw ntlmssp */
1257 u16 blob_length = 0;
1259 rc = SMB2_sess_alloc_buffer(sess_data);
1260 if (rc)
1261 goto out;
1263 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1264 req->sync_hdr.SessionId = ses->Suid;
1266 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1267 sess_data->nls_cp);
1268 if (rc) {
1269 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1270 goto out;
1273 if (use_spnego) {
1274 /* BB eventually need to add this */
1275 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1276 rc = -EOPNOTSUPP;
1277 goto out;
1279 sess_data->iov[1].iov_base = ntlmssp_blob;
1280 sess_data->iov[1].iov_len = blob_length;
1282 rc = SMB2_sess_sendreceive(sess_data);
1283 if (rc)
1284 goto out;
1286 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1288 ses->Suid = rsp->sync_hdr.SessionId;
1289 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1291 rc = SMB2_sess_establish_session(sess_data);
1292 out:
1293 kfree(ntlmssp_blob);
1294 SMB2_sess_free_buffer(sess_data);
1295 kfree(ses->ntlmssp);
1296 ses->ntlmssp = NULL;
1297 sess_data->result = rc;
1298 sess_data->func = NULL;
1301 static int
1302 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1304 int type;
1306 type = smb2_select_sectype(ses->server, ses->sectype);
1307 cifs_dbg(FYI, "sess setup type %d\n", type);
1308 if (type == Unspecified) {
1309 cifs_dbg(VFS,
1310 "Unable to select appropriate authentication method!");
1311 return -EINVAL;
1314 switch (type) {
1315 case Kerberos:
1316 sess_data->func = SMB2_auth_kerberos;
1317 break;
1318 case RawNTLMSSP:
1319 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1320 break;
1321 default:
1322 cifs_dbg(VFS, "secType %d not supported!\n", type);
1323 return -EOPNOTSUPP;
1326 return 0;
1330 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1331 const struct nls_table *nls_cp)
1333 int rc = 0;
1334 struct TCP_Server_Info *server = ses->server;
1335 struct SMB2_sess_data *sess_data;
1337 cifs_dbg(FYI, "Session Setup\n");
1339 if (!server) {
1340 WARN(1, "%s: server is NULL!\n", __func__);
1341 return -EIO;
1344 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1345 if (!sess_data)
1346 return -ENOMEM;
1348 rc = SMB2_select_sec(ses, sess_data);
1349 if (rc)
1350 goto out;
1351 sess_data->xid = xid;
1352 sess_data->ses = ses;
1353 sess_data->buf0_type = CIFS_NO_BUFFER;
1354 sess_data->nls_cp = (struct nls_table *) nls_cp;
1355 sess_data->previous_session = ses->Suid;
1357 #ifdef CONFIG_CIFS_SMB311
1359 * Initialize the session hash with the server one.
1361 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1362 SMB2_PREAUTH_HASH_SIZE);
1363 #endif
1365 while (sess_data->func)
1366 sess_data->func(sess_data);
1368 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1369 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
1370 rc = sess_data->result;
1371 out:
1372 kfree(sess_data);
1373 return rc;
1377 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1379 struct smb2_logoff_req *req; /* response is also trivial struct */
1380 int rc = 0;
1381 struct TCP_Server_Info *server;
1382 int flags = 0;
1383 unsigned int total_len;
1384 struct kvec iov[1];
1385 struct kvec rsp_iov;
1386 int resp_buf_type;
1388 cifs_dbg(FYI, "disconnect session %p\n", ses);
1390 if (ses && (ses->server))
1391 server = ses->server;
1392 else
1393 return -EIO;
1395 /* no need to send SMB logoff if uid already closed due to reconnect */
1396 if (ses->need_reconnect)
1397 goto smb2_session_already_dead;
1399 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
1400 if (rc)
1401 return rc;
1403 /* since no tcon, smb2_init can not do this, so do here */
1404 req->sync_hdr.SessionId = ses->Suid;
1406 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1407 flags |= CIFS_TRANSFORM_REQ;
1408 else if (server->sign)
1409 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1411 flags |= CIFS_NO_RESP;
1413 iov[0].iov_base = (char *)req;
1414 iov[0].iov_len = total_len;
1416 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
1417 cifs_small_buf_release(req);
1419 * No tcon so can't do
1420 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1423 smb2_session_already_dead:
1424 return rc;
1427 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1429 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1432 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1434 /* These are similar values to what Windows uses */
1435 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1437 tcon->max_chunks = 256;
1438 tcon->max_bytes_chunk = 1048576;
1439 tcon->max_bytes_copy = 16777216;
1443 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1444 struct cifs_tcon *tcon, const struct nls_table *cp)
1446 struct smb2_tree_connect_req *req;
1447 struct smb2_tree_connect_rsp *rsp = NULL;
1448 struct kvec iov[2];
1449 struct kvec rsp_iov = { NULL, 0 };
1450 int rc = 0;
1451 int resp_buftype;
1452 int unc_path_len;
1453 __le16 *unc_path = NULL;
1454 int flags = 0;
1455 unsigned int total_len;
1457 cifs_dbg(FYI, "TCON\n");
1459 if (!(ses->server) || !tree)
1460 return -EIO;
1462 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1463 if (unc_path == NULL)
1464 return -ENOMEM;
1466 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1467 unc_path_len *= 2;
1468 if (unc_path_len < 2) {
1469 kfree(unc_path);
1470 return -EINVAL;
1473 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1474 tcon->tid = 0;
1476 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1477 &total_len);
1478 if (rc) {
1479 kfree(unc_path);
1480 return rc;
1483 if (smb3_encryption_required(tcon))
1484 flags |= CIFS_TRANSFORM_REQ;
1486 iov[0].iov_base = (char *)req;
1487 /* 1 for pad */
1488 iov[0].iov_len = total_len - 1;
1490 /* Testing shows that buffer offset must be at location of Buffer[0] */
1491 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1492 - 1 /* pad */);
1493 req->PathLength = cpu_to_le16(unc_path_len - 2);
1494 iov[1].iov_base = unc_path;
1495 iov[1].iov_len = unc_path_len;
1497 /* 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1 */
1498 if ((ses->server->dialect == SMB311_PROT_ID) &&
1499 !smb3_encryption_required(tcon))
1500 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1502 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
1503 cifs_small_buf_release(req);
1504 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1506 if (rc != 0) {
1507 if (tcon) {
1508 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1509 tcon->need_reconnect = true;
1511 goto tcon_error_exit;
1514 switch (rsp->ShareType) {
1515 case SMB2_SHARE_TYPE_DISK:
1516 cifs_dbg(FYI, "connection to disk share\n");
1517 break;
1518 case SMB2_SHARE_TYPE_PIPE:
1519 tcon->pipe = true;
1520 cifs_dbg(FYI, "connection to pipe share\n");
1521 break;
1522 case SMB2_SHARE_TYPE_PRINT:
1523 tcon->print = true;
1524 cifs_dbg(FYI, "connection to printer\n");
1525 break;
1526 default:
1527 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1528 rc = -EOPNOTSUPP;
1529 goto tcon_error_exit;
1532 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1533 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1534 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1535 tcon->tidStatus = CifsGood;
1536 tcon->need_reconnect = false;
1537 tcon->tid = rsp->sync_hdr.TreeId;
1538 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1540 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1541 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1542 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1544 if (tcon->seal &&
1545 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1546 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1548 init_copy_chunk_defaults(tcon);
1549 if (tcon->ses->server->ops->validate_negotiate)
1550 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1551 tcon_exit:
1552 free_rsp_buf(resp_buftype, rsp);
1553 kfree(unc_path);
1554 return rc;
1556 tcon_error_exit:
1557 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1558 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1560 goto tcon_exit;
1564 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1566 struct smb2_tree_disconnect_req *req; /* response is trivial */
1567 int rc = 0;
1568 struct cifs_ses *ses = tcon->ses;
1569 int flags = 0;
1570 unsigned int total_len;
1571 struct kvec iov[1];
1572 struct kvec rsp_iov;
1573 int resp_buf_type;
1575 cifs_dbg(FYI, "Tree Disconnect\n");
1577 if (!ses || !(ses->server))
1578 return -EIO;
1580 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1581 return 0;
1583 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1584 &total_len);
1585 if (rc)
1586 return rc;
1588 if (smb3_encryption_required(tcon))
1589 flags |= CIFS_TRANSFORM_REQ;
1591 flags |= CIFS_NO_RESP;
1593 iov[0].iov_base = (char *)req;
1594 iov[0].iov_len = total_len;
1596 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
1597 cifs_small_buf_release(req);
1598 if (rc)
1599 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1601 return rc;
1605 static struct create_durable *
1606 create_durable_buf(void)
1608 struct create_durable *buf;
1610 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1611 if (!buf)
1612 return NULL;
1614 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1615 (struct create_durable, Data));
1616 buf->ccontext.DataLength = cpu_to_le32(16);
1617 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1618 (struct create_durable, Name));
1619 buf->ccontext.NameLength = cpu_to_le16(4);
1620 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1621 buf->Name[0] = 'D';
1622 buf->Name[1] = 'H';
1623 buf->Name[2] = 'n';
1624 buf->Name[3] = 'Q';
1625 return buf;
1628 static struct create_durable *
1629 create_reconnect_durable_buf(struct cifs_fid *fid)
1631 struct create_durable *buf;
1633 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1634 if (!buf)
1635 return NULL;
1637 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1638 (struct create_durable, Data));
1639 buf->ccontext.DataLength = cpu_to_le32(16);
1640 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1641 (struct create_durable, Name));
1642 buf->ccontext.NameLength = cpu_to_le16(4);
1643 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1644 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1645 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1646 buf->Name[0] = 'D';
1647 buf->Name[1] = 'H';
1648 buf->Name[2] = 'n';
1649 buf->Name[3] = 'C';
1650 return buf;
1653 static __u8
1654 parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1655 unsigned int *epoch, char *lease_key)
1657 char *data_offset;
1658 struct create_context *cc;
1659 unsigned int next;
1660 unsigned int remaining;
1661 char *name;
1663 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
1664 remaining = le32_to_cpu(rsp->CreateContextsLength);
1665 cc = (struct create_context *)data_offset;
1666 while (remaining >= sizeof(struct create_context)) {
1667 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1668 if (le16_to_cpu(cc->NameLength) == 4 &&
1669 strncmp(name, "RqLs", 4) == 0)
1670 return server->ops->parse_lease_buf(cc, epoch,
1671 lease_key);
1673 next = le32_to_cpu(cc->Next);
1674 if (!next)
1675 break;
1676 remaining -= next;
1677 cc = (struct create_context *)((char *)cc + next);
1680 return 0;
1683 static int
1684 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1685 unsigned int *num_iovec, __u8 *oplock)
1687 struct smb2_create_req *req = iov[0].iov_base;
1688 unsigned int num = *num_iovec;
1690 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
1691 if (iov[num].iov_base == NULL)
1692 return -ENOMEM;
1693 iov[num].iov_len = server->vals->create_lease_size;
1694 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1695 if (!req->CreateContextsOffset)
1696 req->CreateContextsOffset = cpu_to_le32(
1697 sizeof(struct smb2_create_req) +
1698 iov[num - 1].iov_len);
1699 le32_add_cpu(&req->CreateContextsLength,
1700 server->vals->create_lease_size);
1701 *num_iovec = num + 1;
1702 return 0;
1705 static struct create_durable_v2 *
1706 create_durable_v2_buf(struct cifs_fid *pfid)
1708 struct create_durable_v2 *buf;
1710 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1711 if (!buf)
1712 return NULL;
1714 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1715 (struct create_durable_v2, dcontext));
1716 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1717 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1718 (struct create_durable_v2, Name));
1719 buf->ccontext.NameLength = cpu_to_le16(4);
1721 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1722 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1723 generate_random_uuid(buf->dcontext.CreateGuid);
1724 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1726 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1727 buf->Name[0] = 'D';
1728 buf->Name[1] = 'H';
1729 buf->Name[2] = '2';
1730 buf->Name[3] = 'Q';
1731 return buf;
1734 static struct create_durable_handle_reconnect_v2 *
1735 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1737 struct create_durable_handle_reconnect_v2 *buf;
1739 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1740 GFP_KERNEL);
1741 if (!buf)
1742 return NULL;
1744 buf->ccontext.DataOffset =
1745 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1746 dcontext));
1747 buf->ccontext.DataLength =
1748 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1749 buf->ccontext.NameOffset =
1750 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1751 Name));
1752 buf->ccontext.NameLength = cpu_to_le16(4);
1754 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1755 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1756 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1757 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1759 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1760 buf->Name[0] = 'D';
1761 buf->Name[1] = 'H';
1762 buf->Name[2] = '2';
1763 buf->Name[3] = 'C';
1764 return buf;
1767 static int
1768 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1769 struct cifs_open_parms *oparms)
1771 struct smb2_create_req *req = iov[0].iov_base;
1772 unsigned int num = *num_iovec;
1774 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1775 if (iov[num].iov_base == NULL)
1776 return -ENOMEM;
1777 iov[num].iov_len = sizeof(struct create_durable_v2);
1778 if (!req->CreateContextsOffset)
1779 req->CreateContextsOffset =
1780 cpu_to_le32(sizeof(struct smb2_create_req) +
1781 iov[1].iov_len);
1782 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1783 *num_iovec = num + 1;
1784 return 0;
1787 static int
1788 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1789 struct cifs_open_parms *oparms)
1791 struct smb2_create_req *req = iov[0].iov_base;
1792 unsigned int num = *num_iovec;
1794 /* indicate that we don't need to relock the file */
1795 oparms->reconnect = false;
1797 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1798 if (iov[num].iov_base == NULL)
1799 return -ENOMEM;
1800 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1801 if (!req->CreateContextsOffset)
1802 req->CreateContextsOffset =
1803 cpu_to_le32(sizeof(struct smb2_create_req) +
1804 iov[1].iov_len);
1805 le32_add_cpu(&req->CreateContextsLength,
1806 sizeof(struct create_durable_handle_reconnect_v2));
1807 *num_iovec = num + 1;
1808 return 0;
1811 static int
1812 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1813 struct cifs_open_parms *oparms, bool use_persistent)
1815 struct smb2_create_req *req = iov[0].iov_base;
1816 unsigned int num = *num_iovec;
1818 if (use_persistent) {
1819 if (oparms->reconnect)
1820 return add_durable_reconnect_v2_context(iov, num_iovec,
1821 oparms);
1822 else
1823 return add_durable_v2_context(iov, num_iovec, oparms);
1826 if (oparms->reconnect) {
1827 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1828 /* indicate that we don't need to relock the file */
1829 oparms->reconnect = false;
1830 } else
1831 iov[num].iov_base = create_durable_buf();
1832 if (iov[num].iov_base == NULL)
1833 return -ENOMEM;
1834 iov[num].iov_len = sizeof(struct create_durable);
1835 if (!req->CreateContextsOffset)
1836 req->CreateContextsOffset =
1837 cpu_to_le32(sizeof(struct smb2_create_req) +
1838 iov[1].iov_len);
1839 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1840 *num_iovec = num + 1;
1841 return 0;
1844 static int
1845 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1846 const char *treename, const __le16 *path)
1848 int treename_len, path_len;
1849 struct nls_table *cp;
1850 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1853 * skip leading "\\"
1855 treename_len = strlen(treename);
1856 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1857 return -EINVAL;
1859 treename += 2;
1860 treename_len -= 2;
1862 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1865 * make room for one path separator between the treename and
1866 * path
1868 *out_len = treename_len + 1 + path_len;
1871 * final path needs to be null-terminated UTF16 with a
1872 * size aligned to 8
1875 *out_size = roundup((*out_len+1)*2, 8);
1876 *out_path = kzalloc(*out_size, GFP_KERNEL);
1877 if (!*out_path)
1878 return -ENOMEM;
1880 cp = load_nls_default();
1881 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1882 UniStrcat(*out_path, sep);
1883 UniStrcat(*out_path, path);
1884 unload_nls(cp);
1886 return 0;
1890 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
1891 __u8 *oplock, struct smb2_file_all_info *buf,
1892 struct kvec *err_iov, int *buftype)
1894 struct smb2_create_req *req;
1895 struct smb2_create_rsp *rsp;
1896 struct TCP_Server_Info *server;
1897 struct cifs_tcon *tcon = oparms->tcon;
1898 struct cifs_ses *ses = tcon->ses;
1899 struct kvec iov[5]; /* make sure at least one for each open context */
1900 struct kvec rsp_iov = {NULL, 0};
1901 int resp_buftype;
1902 int uni_path_len;
1903 __le16 *copy_path = NULL;
1904 int copy_size;
1905 int rc = 0;
1906 unsigned int n_iov = 2;
1907 __u32 file_attributes = 0;
1908 char *dhc_buf = NULL, *lc_buf = NULL, *pc_buf = NULL;
1909 int flags = 0;
1910 unsigned int total_len;
1912 cifs_dbg(FYI, "create/open\n");
1914 if (ses && (ses->server))
1915 server = ses->server;
1916 else
1917 return -EIO;
1919 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
1921 if (rc)
1922 return rc;
1924 if (smb3_encryption_required(tcon))
1925 flags |= CIFS_TRANSFORM_REQ;
1927 if (oparms->create_options & CREATE_OPTION_READONLY)
1928 file_attributes |= ATTR_READONLY;
1929 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1930 file_attributes |= ATTR_SYSTEM;
1932 req->ImpersonationLevel = IL_IMPERSONATION;
1933 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
1934 /* File attributes ignored on open (used in create though) */
1935 req->FileAttributes = cpu_to_le32(file_attributes);
1936 req->ShareAccess = FILE_SHARE_ALL_LE;
1937 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1938 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
1940 iov[0].iov_base = (char *)req;
1941 /* -1 since last byte is buf[0] which is sent below (path) */
1942 iov[0].iov_len = total_len - 1;
1944 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
1946 /* [MS-SMB2] 2.2.13 NameOffset:
1947 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1948 * the SMB2 header, the file name includes a prefix that will
1949 * be processed during DFS name normalization as specified in
1950 * section 3.3.5.9. Otherwise, the file name is relative to
1951 * the share that is identified by the TreeId in the SMB2
1952 * header.
1954 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1955 int name_len;
1957 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
1958 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1959 &name_len,
1960 tcon->treeName, path);
1961 if (rc) {
1962 cifs_small_buf_release(req);
1963 return rc;
1965 req->NameLength = cpu_to_le16(name_len * 2);
1966 uni_path_len = copy_size;
1967 path = copy_path;
1968 } else {
1969 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1970 /* MUST set path len (NameLength) to 0 opening root of share */
1971 req->NameLength = cpu_to_le16(uni_path_len - 2);
1972 if (uni_path_len % 8 != 0) {
1973 copy_size = roundup(uni_path_len, 8);
1974 copy_path = kzalloc(copy_size, GFP_KERNEL);
1975 if (!copy_path) {
1976 cifs_small_buf_release(req);
1977 return -ENOMEM;
1979 memcpy((char *)copy_path, (const char *)path,
1980 uni_path_len);
1981 uni_path_len = copy_size;
1982 path = copy_path;
1986 iov[1].iov_len = uni_path_len;
1987 iov[1].iov_base = path;
1989 if (!server->oplocks)
1990 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1992 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
1993 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1994 req->RequestedOplockLevel = *oplock;
1995 else {
1996 rc = add_lease_context(server, iov, &n_iov, oplock);
1997 if (rc) {
1998 cifs_small_buf_release(req);
1999 kfree(copy_path);
2000 return rc;
2002 lc_buf = iov[n_iov-1].iov_base;
2005 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2006 /* need to set Next field of lease context if we request it */
2007 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2008 struct create_context *ccontext =
2009 (struct create_context *)iov[n_iov-1].iov_base;
2010 ccontext->Next =
2011 cpu_to_le32(server->vals->create_lease_size);
2014 rc = add_durable_context(iov, &n_iov, oparms,
2015 tcon->use_persistent);
2016 if (rc) {
2017 cifs_small_buf_release(req);
2018 kfree(copy_path);
2019 kfree(lc_buf);
2020 return rc;
2022 dhc_buf = iov[n_iov-1].iov_base;
2025 #ifdef CONFIG_CIFS_SMB311
2026 if (tcon->posix_extensions) {
2027 if (n_iov > 2) {
2028 struct create_context *ccontext =
2029 (struct create_context *)iov[n_iov-1].iov_base;
2030 ccontext->Next =
2031 cpu_to_le32(iov[n_iov-1].iov_len);
2034 rc = add_posix_context(iov, &n_iov, oparms->mode);
2035 if (rc) {
2036 cifs_small_buf_release(req);
2037 kfree(copy_path);
2038 kfree(lc_buf);
2039 kfree(dhc_buf);
2040 return rc;
2042 pc_buf = iov[n_iov-1].iov_base;
2044 #endif /* SMB311 */
2046 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
2047 &rsp_iov);
2048 cifs_small_buf_release(req);
2049 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2051 if (rc != 0) {
2052 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2053 if (err_iov && rsp) {
2054 *err_iov = rsp_iov;
2055 *buftype = resp_buftype;
2056 resp_buftype = CIFS_NO_BUFFER;
2057 rsp = NULL;
2059 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2060 oparms->create_options, oparms->desired_access, rc);
2061 goto creat_exit;
2062 } else
2063 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2064 ses->Suid, oparms->create_options,
2065 oparms->desired_access);
2067 oparms->fid->persistent_fid = rsp->PersistentFileId;
2068 oparms->fid->volatile_fid = rsp->VolatileFileId;
2070 if (buf) {
2071 memcpy(buf, &rsp->CreationTime, 32);
2072 buf->AllocationSize = rsp->AllocationSize;
2073 buf->EndOfFile = rsp->EndofFile;
2074 buf->Attributes = rsp->FileAttributes;
2075 buf->NumberOfLinks = cpu_to_le32(1);
2076 buf->DeletePending = 0;
2079 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
2080 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch,
2081 oparms->fid->lease_key);
2082 else
2083 *oplock = rsp->OplockLevel;
2084 creat_exit:
2085 kfree(copy_path);
2086 kfree(lc_buf);
2087 kfree(dhc_buf);
2088 kfree(pc_buf);
2089 free_rsp_buf(resp_buftype, rsp);
2090 return rc;
2094 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2097 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2098 u64 volatile_fid, u32 opcode, bool is_fsctl,
2099 char *in_data, u32 indatalen,
2100 char **out_data, u32 *plen /* returned data len */)
2102 struct smb2_ioctl_req *req;
2103 struct smb2_ioctl_rsp *rsp;
2104 struct cifs_ses *ses;
2105 struct kvec iov[2];
2106 struct kvec rsp_iov;
2107 int resp_buftype;
2108 int n_iov;
2109 int rc = 0;
2110 int flags = 0;
2111 unsigned int total_len;
2113 cifs_dbg(FYI, "SMB2 IOCTL\n");
2115 if (out_data != NULL)
2116 *out_data = NULL;
2118 /* zero out returned data len, in case of error */
2119 if (plen)
2120 *plen = 0;
2122 if (tcon)
2123 ses = tcon->ses;
2124 else
2125 return -EIO;
2127 if (!ses || !(ses->server))
2128 return -EIO;
2130 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
2131 if (rc)
2132 return rc;
2134 if (smb3_encryption_required(tcon))
2135 flags |= CIFS_TRANSFORM_REQ;
2137 req->CtlCode = cpu_to_le32(opcode);
2138 req->PersistentFileId = persistent_fid;
2139 req->VolatileFileId = volatile_fid;
2141 if (indatalen) {
2142 req->InputCount = cpu_to_le32(indatalen);
2143 /* do not set InputOffset if no input data */
2144 req->InputOffset =
2145 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
2146 iov[1].iov_base = in_data;
2147 iov[1].iov_len = indatalen;
2148 n_iov = 2;
2149 } else
2150 n_iov = 1;
2152 req->OutputOffset = 0;
2153 req->OutputCount = 0; /* MBZ */
2156 * Could increase MaxOutputResponse, but that would require more
2157 * than one credit. Windows typically sets this smaller, but for some
2158 * ioctls it may be useful to allow server to send more. No point
2159 * limiting what the server can send as long as fits in one credit
2160 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
2161 * (by default, note that it can be overridden to make max larger)
2162 * in responses (except for read responses which can be bigger.
2163 * We may want to bump this limit up
2165 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
2167 if (is_fsctl)
2168 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2169 else
2170 req->Flags = 0;
2172 iov[0].iov_base = (char *)req;
2175 * If no input data, the size of ioctl struct in
2176 * protocol spec still includes a 1 byte data buffer,
2177 * but if input data passed to ioctl, we do not
2178 * want to double count this, so we do not send
2179 * the dummy one byte of data in iovec[0] if sending
2180 * input data (in iovec[1]).
2183 if (indatalen) {
2184 iov[0].iov_len = total_len - 1;
2185 } else
2186 iov[0].iov_len = total_len;
2188 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2189 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
2190 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
2192 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
2193 &rsp_iov);
2194 cifs_small_buf_release(req);
2195 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
2197 if (rc != 0)
2198 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2199 ses->Suid, 0, opcode, rc);
2201 if ((rc != 0) && (rc != -EINVAL)) {
2202 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2203 goto ioctl_exit;
2204 } else if (rc == -EINVAL) {
2205 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2206 (opcode != FSCTL_SRV_COPYCHUNK)) {
2207 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2208 goto ioctl_exit;
2212 /* check if caller wants to look at return data or just return rc */
2213 if ((plen == NULL) || (out_data == NULL))
2214 goto ioctl_exit;
2216 *plen = le32_to_cpu(rsp->OutputCount);
2218 /* We check for obvious errors in the output buffer length and offset */
2219 if (*plen == 0)
2220 goto ioctl_exit; /* server returned no data */
2221 else if (*plen > 0xFF00) {
2222 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2223 *plen = 0;
2224 rc = -EIO;
2225 goto ioctl_exit;
2228 if (rsp_iov.iov_len < le32_to_cpu(rsp->OutputOffset) + *plen) {
2229 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2230 le32_to_cpu(rsp->OutputOffset));
2231 *plen = 0;
2232 rc = -EIO;
2233 goto ioctl_exit;
2236 *out_data = kmalloc(*plen, GFP_KERNEL);
2237 if (*out_data == NULL) {
2238 rc = -ENOMEM;
2239 goto ioctl_exit;
2242 memcpy(*out_data, (char *)rsp + le32_to_cpu(rsp->OutputOffset), *plen);
2243 ioctl_exit:
2244 free_rsp_buf(resp_buftype, rsp);
2245 return rc;
2249 * Individual callers to ioctl worker function follow
2253 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2254 u64 persistent_fid, u64 volatile_fid)
2256 int rc;
2257 struct compress_ioctl fsctl_input;
2258 char *ret_data = NULL;
2260 fsctl_input.CompressionState =
2261 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
2263 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2264 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2265 (char *)&fsctl_input /* data input */,
2266 2 /* in data len */, &ret_data /* out data */, NULL);
2268 cifs_dbg(FYI, "set compression rc %d\n", rc);
2270 return rc;
2274 SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2275 u64 persistent_fid, u64 volatile_fid, int flags)
2277 struct smb2_close_req *req;
2278 struct smb2_close_rsp *rsp;
2279 struct cifs_ses *ses = tcon->ses;
2280 struct kvec iov[1];
2281 struct kvec rsp_iov;
2282 int resp_buftype;
2283 int rc = 0;
2284 unsigned int total_len;
2286 cifs_dbg(FYI, "Close\n");
2288 if (!ses || !(ses->server))
2289 return -EIO;
2291 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
2292 if (rc)
2293 return rc;
2295 if (smb3_encryption_required(tcon))
2296 flags |= CIFS_TRANSFORM_REQ;
2298 req->PersistentFileId = persistent_fid;
2299 req->VolatileFileId = volatile_fid;
2301 iov[0].iov_base = (char *)req;
2302 iov[0].iov_len = total_len;
2304 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2305 cifs_small_buf_release(req);
2306 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
2308 if (rc != 0) {
2309 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
2310 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2311 rc);
2312 goto close_exit;
2315 /* BB FIXME - decode close response, update inode for caching */
2317 close_exit:
2318 free_rsp_buf(resp_buftype, rsp);
2319 return rc;
2323 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2324 u64 persistent_fid, u64 volatile_fid)
2326 return SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
2329 static int
2330 validate_iov(unsigned int offset, unsigned int buffer_length,
2331 struct kvec *iov, unsigned int min_buf_size)
2333 unsigned int smb_len = iov->iov_len;
2334 char *end_of_smb = smb_len + (char *)iov->iov_base;
2335 char *begin_of_buf = offset + (char *)iov->iov_base;
2336 char *end_of_buf = begin_of_buf + buffer_length;
2339 if (buffer_length < min_buf_size) {
2340 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2341 buffer_length, min_buf_size);
2342 return -EINVAL;
2345 /* check if beyond RFC1001 maximum length */
2346 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
2347 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2348 buffer_length, smb_len);
2349 return -EINVAL;
2352 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
2353 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
2354 return -EINVAL;
2357 return 0;
2361 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2362 * Caller must free buffer.
2364 static int
2365 validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
2366 struct kvec *iov, unsigned int minbufsize,
2367 char *data)
2369 char *begin_of_buf = offset + (char *)iov->iov_base;
2370 int rc;
2372 if (!data)
2373 return -EINVAL;
2375 rc = validate_iov(offset, buffer_length, iov, minbufsize);
2376 if (rc)
2377 return rc;
2379 memcpy(data, begin_of_buf, buffer_length);
2381 return 0;
2384 static int
2385 query_info(const unsigned int xid, struct cifs_tcon *tcon,
2386 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2387 u32 additional_info, size_t output_len, size_t min_len, void **data,
2388 u32 *dlen)
2390 struct smb2_query_info_req *req;
2391 struct smb2_query_info_rsp *rsp = NULL;
2392 struct kvec iov[2];
2393 struct kvec rsp_iov;
2394 int rc = 0;
2395 int resp_buftype;
2396 struct cifs_ses *ses = tcon->ses;
2397 int flags = 0;
2398 unsigned int total_len;
2400 cifs_dbg(FYI, "Query Info\n");
2402 if (!ses || !(ses->server))
2403 return -EIO;
2405 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2406 &total_len);
2407 if (rc)
2408 return rc;
2410 if (smb3_encryption_required(tcon))
2411 flags |= CIFS_TRANSFORM_REQ;
2413 req->InfoType = info_type;
2414 req->FileInfoClass = info_class;
2415 req->PersistentFileId = persistent_fid;
2416 req->VolatileFileId = volatile_fid;
2417 req->AdditionalInformation = cpu_to_le32(additional_info);
2420 * We do not use the input buffer (do not send extra byte)
2422 req->InputBufferOffset = 0;
2424 req->OutputBufferLength = cpu_to_le32(output_len);
2426 iov[0].iov_base = (char *)req;
2427 /* 1 for Buffer */
2428 iov[0].iov_len = total_len - 1;
2430 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2431 cifs_small_buf_release(req);
2432 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2434 if (rc) {
2435 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2436 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
2437 ses->Suid, info_class, (__u32)info_type, rc);
2438 goto qinf_exit;
2441 if (dlen) {
2442 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2443 if (!*data) {
2444 *data = kmalloc(*dlen, GFP_KERNEL);
2445 if (!*data) {
2446 cifs_dbg(VFS,
2447 "Error %d allocating memory for acl\n",
2448 rc);
2449 *dlen = 0;
2450 goto qinf_exit;
2455 rc = validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
2456 le32_to_cpu(rsp->OutputBufferLength),
2457 &rsp_iov, min_len, *data);
2459 qinf_exit:
2460 free_rsp_buf(resp_buftype, rsp);
2461 return rc;
2464 int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
2465 u64 persistent_fid, u64 volatile_fid,
2466 int ea_buf_size, struct smb2_file_full_ea_info *data)
2468 return query_info(xid, tcon, persistent_fid, volatile_fid,
2469 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
2470 ea_buf_size,
2471 sizeof(struct smb2_file_full_ea_info),
2472 (void **)&data,
2473 NULL);
2476 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2477 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
2479 return query_info(xid, tcon, persistent_fid, volatile_fid,
2480 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
2481 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
2482 sizeof(struct smb2_file_all_info), (void **)&data,
2483 NULL);
2487 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2488 u64 persistent_fid, u64 volatile_fid,
2489 void **data, u32 *plen)
2491 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2492 *plen = 0;
2494 return query_info(xid, tcon, persistent_fid, volatile_fid,
2495 0, SMB2_O_INFO_SECURITY, additional_info,
2496 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
2500 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2501 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2503 return query_info(xid, tcon, persistent_fid, volatile_fid,
2504 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
2505 sizeof(struct smb2_file_internal_info),
2506 sizeof(struct smb2_file_internal_info),
2507 (void **)&uniqueid, NULL);
2511 * This is a no-op for now. We're not really interested in the reply, but
2512 * rather in the fact that the server sent one and that server->lstrp
2513 * gets updated.
2515 * FIXME: maybe we should consider checking that the reply matches request?
2517 static void
2518 smb2_echo_callback(struct mid_q_entry *mid)
2520 struct TCP_Server_Info *server = mid->callback_data;
2521 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
2522 unsigned int credits_received = 1;
2524 if (mid->mid_state == MID_RESPONSE_RECEIVED)
2525 credits_received = le16_to_cpu(rsp->sync_hdr.CreditRequest);
2527 DeleteMidQEntry(mid);
2528 add_credits(server, credits_received, CIFS_ECHO_OP);
2531 void smb2_reconnect_server(struct work_struct *work)
2533 struct TCP_Server_Info *server = container_of(work,
2534 struct TCP_Server_Info, reconnect.work);
2535 struct cifs_ses *ses;
2536 struct cifs_tcon *tcon, *tcon2;
2537 struct list_head tmp_list;
2538 int tcon_exist = false;
2539 int rc;
2540 int resched = false;
2543 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2544 mutex_lock(&server->reconnect_mutex);
2546 INIT_LIST_HEAD(&tmp_list);
2547 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2549 spin_lock(&cifs_tcp_ses_lock);
2550 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2551 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2552 if (tcon->need_reconnect || tcon->need_reopen_files) {
2553 tcon->tc_count++;
2554 list_add_tail(&tcon->rlist, &tmp_list);
2555 tcon_exist = true;
2558 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
2559 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
2560 tcon_exist = true;
2564 * Get the reference to server struct to be sure that the last call of
2565 * cifs_put_tcon() in the loop below won't release the server pointer.
2567 if (tcon_exist)
2568 server->srv_count++;
2570 spin_unlock(&cifs_tcp_ses_lock);
2572 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
2573 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2574 if (!rc)
2575 cifs_reopen_persistent_handles(tcon);
2576 else
2577 resched = true;
2578 list_del_init(&tcon->rlist);
2579 cifs_put_tcon(tcon);
2582 cifs_dbg(FYI, "Reconnecting tcons finished\n");
2583 if (resched)
2584 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
2585 mutex_unlock(&server->reconnect_mutex);
2587 /* now we can safely release srv struct */
2588 if (tcon_exist)
2589 cifs_put_tcp_session(server, 1);
2593 SMB2_echo(struct TCP_Server_Info *server)
2595 struct smb2_echo_req *req;
2596 int rc = 0;
2597 struct kvec iov[2];
2598 struct smb_rqst rqst = { .rq_iov = iov,
2599 .rq_nvec = 2 };
2600 unsigned int total_len;
2601 __be32 rfc1002_marker;
2603 cifs_dbg(FYI, "In echo request\n");
2605 if (server->tcpStatus == CifsNeedNegotiate) {
2606 /* No need to send echo on newly established connections */
2607 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2608 return rc;
2611 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
2612 if (rc)
2613 return rc;
2615 req->sync_hdr.CreditRequest = cpu_to_le16(1);
2617 iov[0].iov_len = 4;
2618 rfc1002_marker = cpu_to_be32(total_len);
2619 iov[0].iov_base = &rfc1002_marker;
2620 iov[1].iov_len = total_len;
2621 iov[1].iov_base = (char *)req;
2623 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2624 server, CIFS_ECHO_OP);
2625 if (rc)
2626 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
2628 cifs_small_buf_release(req);
2629 return rc;
2633 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2634 u64 volatile_fid)
2636 struct smb2_flush_req *req;
2637 struct cifs_ses *ses = tcon->ses;
2638 struct kvec iov[1];
2639 struct kvec rsp_iov;
2640 int resp_buftype;
2641 int rc = 0;
2642 int flags = 0;
2643 unsigned int total_len;
2645 cifs_dbg(FYI, "Flush\n");
2647 if (!ses || !(ses->server))
2648 return -EIO;
2650 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
2651 if (rc)
2652 return rc;
2654 if (smb3_encryption_required(tcon))
2655 flags |= CIFS_TRANSFORM_REQ;
2657 req->PersistentFileId = persistent_fid;
2658 req->VolatileFileId = volatile_fid;
2660 iov[0].iov_base = (char *)req;
2661 iov[0].iov_len = total_len;
2663 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2664 cifs_small_buf_release(req);
2666 if (rc != 0) {
2667 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2668 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
2669 rc);
2672 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2673 return rc;
2677 * To form a chain of read requests, any read requests after the first should
2678 * have the end_of_chain boolean set to true.
2680 static int
2681 smb2_new_read_req(void **buf, unsigned int *total_len,
2682 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
2683 unsigned int remaining_bytes, int request_type)
2685 int rc = -EACCES;
2686 struct smb2_read_plain_req *req = NULL;
2687 struct smb2_sync_hdr *shdr;
2688 struct TCP_Server_Info *server;
2690 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2691 total_len);
2692 if (rc)
2693 return rc;
2695 server = io_parms->tcon->ses->server;
2696 if (server == NULL)
2697 return -ECONNABORTED;
2699 shdr = &req->sync_hdr;
2700 shdr->ProcessId = cpu_to_le32(io_parms->pid);
2702 req->PersistentFileId = io_parms->persistent_fid;
2703 req->VolatileFileId = io_parms->volatile_fid;
2704 req->ReadChannelInfoOffset = 0; /* reserved */
2705 req->ReadChannelInfoLength = 0; /* reserved */
2706 req->Channel = 0; /* reserved */
2707 req->MinimumCount = 0;
2708 req->Length = cpu_to_le32(io_parms->length);
2709 req->Offset = cpu_to_le64(io_parms->offset);
2710 #ifdef CONFIG_CIFS_SMB_DIRECT
2712 * If we want to do a RDMA write, fill in and append
2713 * smbd_buffer_descriptor_v1 to the end of read request
2715 if (server->rdma && rdata && !server->sign &&
2716 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
2718 struct smbd_buffer_descriptor_v1 *v1;
2719 bool need_invalidate =
2720 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
2722 rdata->mr = smbd_register_mr(
2723 server->smbd_conn, rdata->pages,
2724 rdata->nr_pages, rdata->page_offset,
2725 rdata->tailsz, true, need_invalidate);
2726 if (!rdata->mr)
2727 return -ENOBUFS;
2729 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2730 if (need_invalidate)
2731 req->Channel = SMB2_CHANNEL_RDMA_V1;
2732 req->ReadChannelInfoOffset =
2733 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
2734 req->ReadChannelInfoLength =
2735 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
2736 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
2737 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
2738 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
2739 v1->length = cpu_to_le32(rdata->mr->mr->length);
2741 *total_len += sizeof(*v1) - 1;
2743 #endif
2744 if (request_type & CHAINED_REQUEST) {
2745 if (!(request_type & END_OF_CHAIN)) {
2746 /* next 8-byte aligned request */
2747 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2748 shdr->NextCommand = cpu_to_le32(*total_len);
2749 } else /* END_OF_CHAIN */
2750 shdr->NextCommand = 0;
2751 if (request_type & RELATED_REQUEST) {
2752 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2754 * Related requests use info from previous read request
2755 * in chain.
2757 shdr->SessionId = 0xFFFFFFFF;
2758 shdr->TreeId = 0xFFFFFFFF;
2759 req->PersistentFileId = 0xFFFFFFFF;
2760 req->VolatileFileId = 0xFFFFFFFF;
2763 if (remaining_bytes > io_parms->length)
2764 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2765 else
2766 req->RemainingBytes = 0;
2768 *buf = req;
2769 return rc;
2772 static void
2773 smb2_readv_callback(struct mid_q_entry *mid)
2775 struct cifs_readdata *rdata = mid->callback_data;
2776 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2777 struct TCP_Server_Info *server = tcon->ses->server;
2778 struct smb2_sync_hdr *shdr =
2779 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
2780 unsigned int credits_received = 1;
2781 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2782 .rq_nvec = 2,
2783 .rq_pages = rdata->pages,
2784 .rq_offset = rdata->page_offset,
2785 .rq_npages = rdata->nr_pages,
2786 .rq_pagesz = rdata->pagesz,
2787 .rq_tailsz = rdata->tailsz };
2789 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2790 __func__, mid->mid, mid->mid_state, rdata->result,
2791 rdata->bytes);
2793 switch (mid->mid_state) {
2794 case MID_RESPONSE_RECEIVED:
2795 credits_received = le16_to_cpu(shdr->CreditRequest);
2796 /* result already set, check signature */
2797 if (server->sign && !mid->decrypted) {
2798 int rc;
2800 rc = smb2_verify_signature(&rqst, server);
2801 if (rc)
2802 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2803 rc);
2805 /* FIXME: should this be counted toward the initiating task? */
2806 task_io_account_read(rdata->got_bytes);
2807 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2808 break;
2809 case MID_REQUEST_SUBMITTED:
2810 case MID_RETRY_NEEDED:
2811 rdata->result = -EAGAIN;
2812 if (server->sign && rdata->got_bytes)
2813 /* reset bytes number since we can not check a sign */
2814 rdata->got_bytes = 0;
2815 /* FIXME: should this be counted toward the initiating task? */
2816 task_io_account_read(rdata->got_bytes);
2817 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2818 break;
2819 default:
2820 if (rdata->result != -ENODATA)
2821 rdata->result = -EIO;
2823 #ifdef CONFIG_CIFS_SMB_DIRECT
2825 * If this rdata has a memmory registered, the MR can be freed
2826 * MR needs to be freed as soon as I/O finishes to prevent deadlock
2827 * because they have limited number and are used for future I/Os
2829 if (rdata->mr) {
2830 smbd_deregister_mr(rdata->mr);
2831 rdata->mr = NULL;
2833 #endif
2834 if (rdata->result)
2835 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2837 queue_work(cifsiod_wq, &rdata->work);
2838 DeleteMidQEntry(mid);
2839 add_credits(server, credits_received, 0);
2842 /* smb2_async_readv - send an async read, and set up mid to handle result */
2844 smb2_async_readv(struct cifs_readdata *rdata)
2846 int rc, flags = 0;
2847 char *buf;
2848 struct smb2_sync_hdr *shdr;
2849 struct cifs_io_parms io_parms;
2850 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2851 .rq_nvec = 2 };
2852 struct TCP_Server_Info *server;
2853 unsigned int total_len;
2854 __be32 req_len;
2856 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2857 __func__, rdata->offset, rdata->bytes);
2859 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2860 io_parms.offset = rdata->offset;
2861 io_parms.length = rdata->bytes;
2862 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2863 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2864 io_parms.pid = rdata->pid;
2866 server = io_parms.tcon->ses->server;
2868 rc = smb2_new_read_req(
2869 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
2870 if (rc) {
2871 if (rc == -EAGAIN && rdata->credits) {
2872 /* credits was reset by reconnect */
2873 rdata->credits = 0;
2874 /* reduce in_flight value since we won't send the req */
2875 spin_lock(&server->req_lock);
2876 server->in_flight--;
2877 spin_unlock(&server->req_lock);
2879 return rc;
2882 if (smb3_encryption_required(io_parms.tcon))
2883 flags |= CIFS_TRANSFORM_REQ;
2885 req_len = cpu_to_be32(total_len);
2887 rdata->iov[0].iov_base = &req_len;
2888 rdata->iov[0].iov_len = sizeof(__be32);
2889 rdata->iov[1].iov_base = buf;
2890 rdata->iov[1].iov_len = total_len;
2892 shdr = (struct smb2_sync_hdr *)buf;
2894 if (rdata->credits) {
2895 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2896 SMB2_MAX_BUFFER_SIZE));
2897 shdr->CreditRequest = shdr->CreditCharge;
2898 spin_lock(&server->req_lock);
2899 server->credits += rdata->credits -
2900 le16_to_cpu(shdr->CreditCharge);
2901 spin_unlock(&server->req_lock);
2902 wake_up(&server->request_q);
2903 flags |= CIFS_HAS_CREDITS;
2906 kref_get(&rdata->refcount);
2907 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
2908 cifs_readv_receive, smb2_readv_callback,
2909 smb3_handle_read_data, rdata, flags);
2910 if (rc) {
2911 kref_put(&rdata->refcount, cifs_readdata_release);
2912 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2913 trace_smb3_read_err(rc, 0 /* xid */, io_parms.persistent_fid,
2914 io_parms.tcon->tid, io_parms.tcon->ses->Suid,
2915 io_parms.offset, io_parms.length);
2916 } else
2917 trace_smb3_read_done(0 /* xid */, io_parms.persistent_fid,
2918 io_parms.tcon->tid, io_parms.tcon->ses->Suid,
2919 io_parms.offset, io_parms.length);
2921 cifs_small_buf_release(buf);
2922 return rc;
2926 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2927 unsigned int *nbytes, char **buf, int *buf_type)
2929 int resp_buftype, rc = -EACCES;
2930 struct smb2_read_plain_req *req = NULL;
2931 struct smb2_read_rsp *rsp = NULL;
2932 struct kvec iov[1];
2933 struct kvec rsp_iov;
2934 unsigned int total_len;
2935 int flags = CIFS_LOG_ERROR;
2936 struct cifs_ses *ses = io_parms->tcon->ses;
2938 *nbytes = 0;
2939 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
2940 if (rc)
2941 return rc;
2943 if (smb3_encryption_required(io_parms->tcon))
2944 flags |= CIFS_TRANSFORM_REQ;
2946 iov[0].iov_base = (char *)req;
2947 iov[0].iov_len = total_len;
2949 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2950 cifs_small_buf_release(req);
2952 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
2954 if (rc) {
2955 if (rc != -ENODATA) {
2956 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2957 cifs_dbg(VFS, "Send error in read = %d\n", rc);
2959 trace_smb3_read_err(rc, xid, req->PersistentFileId,
2960 io_parms->tcon->tid, ses->Suid,
2961 io_parms->offset, io_parms->length);
2962 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2963 return rc == -ENODATA ? 0 : rc;
2964 } else
2965 trace_smb3_read_done(xid, req->PersistentFileId,
2966 io_parms->tcon->tid, ses->Suid,
2967 io_parms->offset, io_parms->length);
2969 *nbytes = le32_to_cpu(rsp->DataLength);
2970 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2971 (*nbytes > io_parms->length)) {
2972 cifs_dbg(FYI, "bad length %d for count %d\n",
2973 *nbytes, io_parms->length);
2974 rc = -EIO;
2975 *nbytes = 0;
2978 if (*buf) {
2979 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
2980 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2981 } else if (resp_buftype != CIFS_NO_BUFFER) {
2982 *buf = rsp_iov.iov_base;
2983 if (resp_buftype == CIFS_SMALL_BUFFER)
2984 *buf_type = CIFS_SMALL_BUFFER;
2985 else if (resp_buftype == CIFS_LARGE_BUFFER)
2986 *buf_type = CIFS_LARGE_BUFFER;
2988 return rc;
2992 * Check the mid_state and signature on received buffer (if any), and queue the
2993 * workqueue completion task.
2995 static void
2996 smb2_writev_callback(struct mid_q_entry *mid)
2998 struct cifs_writedata *wdata = mid->callback_data;
2999 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3000 unsigned int written;
3001 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
3002 unsigned int credits_received = 1;
3004 switch (mid->mid_state) {
3005 case MID_RESPONSE_RECEIVED:
3006 credits_received = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3007 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
3008 if (wdata->result != 0)
3009 break;
3011 written = le32_to_cpu(rsp->DataLength);
3013 * Mask off high 16 bits when bytes written as returned
3014 * by the server is greater than bytes requested by the
3015 * client. OS/2 servers are known to set incorrect
3016 * CountHigh values.
3018 if (written > wdata->bytes)
3019 written &= 0xFFFF;
3021 if (written < wdata->bytes)
3022 wdata->result = -ENOSPC;
3023 else
3024 wdata->bytes = written;
3025 break;
3026 case MID_REQUEST_SUBMITTED:
3027 case MID_RETRY_NEEDED:
3028 wdata->result = -EAGAIN;
3029 break;
3030 default:
3031 wdata->result = -EIO;
3032 break;
3034 #ifdef CONFIG_CIFS_SMB_DIRECT
3036 * If this wdata has a memory registered, the MR can be freed
3037 * The number of MRs available is limited, it's important to recover
3038 * used MR as soon as I/O is finished. Hold MR longer in the later
3039 * I/O process can possibly result in I/O deadlock due to lack of MR
3040 * to send request on I/O retry
3042 if (wdata->mr) {
3043 smbd_deregister_mr(wdata->mr);
3044 wdata->mr = NULL;
3046 #endif
3047 if (wdata->result)
3048 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3050 queue_work(cifsiod_wq, &wdata->work);
3051 DeleteMidQEntry(mid);
3052 add_credits(tcon->ses->server, credits_received, 0);
3055 /* smb2_async_writev - send an async write, and set up mid to handle result */
3057 smb2_async_writev(struct cifs_writedata *wdata,
3058 void (*release)(struct kref *kref))
3060 int rc = -EACCES, flags = 0;
3061 struct smb2_write_req *req = NULL;
3062 struct smb2_sync_hdr *shdr;
3063 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3064 struct TCP_Server_Info *server = tcon->ses->server;
3065 struct kvec iov[2];
3066 struct smb_rqst rqst = { };
3067 unsigned int total_len;
3068 __be32 rfc1002_marker;
3070 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
3071 if (rc) {
3072 if (rc == -EAGAIN && wdata->credits) {
3073 /* credits was reset by reconnect */
3074 wdata->credits = 0;
3075 /* reduce in_flight value since we won't send the req */
3076 spin_lock(&server->req_lock);
3077 server->in_flight--;
3078 spin_unlock(&server->req_lock);
3080 goto async_writev_out;
3083 if (smb3_encryption_required(tcon))
3084 flags |= CIFS_TRANSFORM_REQ;
3086 shdr = (struct smb2_sync_hdr *)req;
3087 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
3089 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3090 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3091 req->WriteChannelInfoOffset = 0;
3092 req->WriteChannelInfoLength = 0;
3093 req->Channel = 0;
3094 req->Offset = cpu_to_le64(wdata->offset);
3095 req->DataOffset = cpu_to_le16(
3096 offsetof(struct smb2_write_req, Buffer));
3097 req->RemainingBytes = 0;
3098 #ifdef CONFIG_CIFS_SMB_DIRECT
3100 * If we want to do a server RDMA read, fill in and append
3101 * smbd_buffer_descriptor_v1 to the end of write request
3103 if (server->rdma && !server->sign && wdata->bytes >=
3104 server->smbd_conn->rdma_readwrite_threshold) {
3106 struct smbd_buffer_descriptor_v1 *v1;
3107 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3109 wdata->mr = smbd_register_mr(
3110 server->smbd_conn, wdata->pages,
3111 wdata->nr_pages, wdata->page_offset,
3112 wdata->tailsz, false, need_invalidate);
3113 if (!wdata->mr) {
3114 rc = -ENOBUFS;
3115 goto async_writev_out;
3117 req->Length = 0;
3118 req->DataOffset = 0;
3119 if (wdata->nr_pages > 1)
3120 req->RemainingBytes =
3121 cpu_to_le32(
3122 (wdata->nr_pages - 1) * wdata->pagesz -
3123 wdata->page_offset + wdata->tailsz
3125 else
3126 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
3127 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3128 if (need_invalidate)
3129 req->Channel = SMB2_CHANNEL_RDMA_V1;
3130 req->WriteChannelInfoOffset =
3131 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
3132 req->WriteChannelInfoLength =
3133 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3134 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3135 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3136 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3137 v1->length = cpu_to_le32(wdata->mr->mr->length);
3139 #endif
3140 /* 4 for rfc1002 length field and 1 for Buffer */
3141 iov[0].iov_len = 4;
3142 rfc1002_marker = cpu_to_be32(total_len - 1 + wdata->bytes);
3143 iov[0].iov_base = &rfc1002_marker;
3144 iov[1].iov_len = total_len - 1;
3145 iov[1].iov_base = (char *)req;
3147 rqst.rq_iov = iov;
3148 rqst.rq_nvec = 2;
3149 rqst.rq_pages = wdata->pages;
3150 rqst.rq_offset = wdata->page_offset;
3151 rqst.rq_npages = wdata->nr_pages;
3152 rqst.rq_pagesz = wdata->pagesz;
3153 rqst.rq_tailsz = wdata->tailsz;
3154 #ifdef CONFIG_CIFS_SMB_DIRECT
3155 if (wdata->mr) {
3156 iov[1].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
3157 rqst.rq_npages = 0;
3159 #endif
3160 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3161 wdata->offset, wdata->bytes);
3163 #ifdef CONFIG_CIFS_SMB_DIRECT
3164 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3165 if (!wdata->mr)
3166 req->Length = cpu_to_le32(wdata->bytes);
3167 #else
3168 req->Length = cpu_to_le32(wdata->bytes);
3169 #endif
3171 if (wdata->credits) {
3172 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
3173 SMB2_MAX_BUFFER_SIZE));
3174 shdr->CreditRequest = shdr->CreditCharge;
3175 spin_lock(&server->req_lock);
3176 server->credits += wdata->credits -
3177 le16_to_cpu(shdr->CreditCharge);
3178 spin_unlock(&server->req_lock);
3179 wake_up(&server->request_q);
3180 flags |= CIFS_HAS_CREDITS;
3183 kref_get(&wdata->refcount);
3184 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
3185 wdata, flags);
3187 if (rc) {
3188 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
3189 tcon->tid, tcon->ses->Suid, wdata->offset,
3190 wdata->bytes, rc);
3191 kref_put(&wdata->refcount, release);
3192 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3193 } else
3194 trace_smb3_write_done(0 /* no xid */, req->PersistentFileId,
3195 tcon->tid, tcon->ses->Suid, wdata->offset,
3196 wdata->bytes);
3198 async_writev_out:
3199 cifs_small_buf_release(req);
3200 return rc;
3204 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3205 * The length field from io_parms must be at least 1 and indicates a number of
3206 * elements with data to write that begins with position 1 in iov array. All
3207 * data length is specified by count.
3210 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3211 unsigned int *nbytes, struct kvec *iov, int n_vec)
3213 int rc = 0;
3214 struct smb2_write_req *req = NULL;
3215 struct smb2_write_rsp *rsp = NULL;
3216 int resp_buftype;
3217 struct kvec rsp_iov;
3218 int flags = 0;
3219 unsigned int total_len;
3221 *nbytes = 0;
3223 if (n_vec < 1)
3224 return rc;
3226 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3227 &total_len);
3228 if (rc)
3229 return rc;
3231 if (io_parms->tcon->ses->server == NULL)
3232 return -ECONNABORTED;
3234 if (smb3_encryption_required(io_parms->tcon))
3235 flags |= CIFS_TRANSFORM_REQ;
3237 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
3239 req->PersistentFileId = io_parms->persistent_fid;
3240 req->VolatileFileId = io_parms->volatile_fid;
3241 req->WriteChannelInfoOffset = 0;
3242 req->WriteChannelInfoLength = 0;
3243 req->Channel = 0;
3244 req->Length = cpu_to_le32(io_parms->length);
3245 req->Offset = cpu_to_le64(io_parms->offset);
3246 req->DataOffset = cpu_to_le16(
3247 offsetof(struct smb2_write_req, Buffer));
3248 req->RemainingBytes = 0;
3250 iov[0].iov_base = (char *)req;
3251 /* 1 for Buffer */
3252 iov[0].iov_len = total_len - 1;
3254 rc = smb2_send_recv(xid, io_parms->tcon->ses, iov, n_vec + 1,
3255 &resp_buftype, flags, &rsp_iov);
3256 cifs_small_buf_release(req);
3257 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
3259 if (rc) {
3260 trace_smb3_write_err(xid, req->PersistentFileId,
3261 io_parms->tcon->tid,
3262 io_parms->tcon->ses->Suid,
3263 io_parms->offset, io_parms->length, rc);
3264 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
3265 cifs_dbg(VFS, "Send error in write = %d\n", rc);
3266 } else {
3267 *nbytes = le32_to_cpu(rsp->DataLength);
3268 trace_smb3_write_done(xid, req->PersistentFileId,
3269 io_parms->tcon->tid,
3270 io_parms->tcon->ses->Suid,
3271 io_parms->offset, *nbytes);
3274 free_rsp_buf(resp_buftype, rsp);
3275 return rc;
3278 static unsigned int
3279 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3281 int len;
3282 unsigned int entrycount = 0;
3283 unsigned int next_offset = 0;
3284 FILE_DIRECTORY_INFO *entryptr;
3286 if (bufstart == NULL)
3287 return 0;
3289 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
3291 while (1) {
3292 entryptr = (FILE_DIRECTORY_INFO *)
3293 ((char *)entryptr + next_offset);
3295 if ((char *)entryptr + size > end_of_buf) {
3296 cifs_dbg(VFS, "malformed search entry would overflow\n");
3297 break;
3300 len = le32_to_cpu(entryptr->FileNameLength);
3301 if ((char *)entryptr + len + size > end_of_buf) {
3302 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3303 end_of_buf);
3304 break;
3307 *lastentry = (char *)entryptr;
3308 entrycount++;
3310 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
3311 if (!next_offset)
3312 break;
3315 return entrycount;
3319 * Readdir/FindFirst
3322 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3323 u64 persistent_fid, u64 volatile_fid, int index,
3324 struct cifs_search_info *srch_inf)
3326 struct smb2_query_directory_req *req;
3327 struct smb2_query_directory_rsp *rsp = NULL;
3328 struct kvec iov[2];
3329 struct kvec rsp_iov;
3330 int rc = 0;
3331 int len;
3332 int resp_buftype = CIFS_NO_BUFFER;
3333 unsigned char *bufptr;
3334 struct TCP_Server_Info *server;
3335 struct cifs_ses *ses = tcon->ses;
3336 __le16 asteriks = cpu_to_le16('*');
3337 char *end_of_smb;
3338 unsigned int output_size = CIFSMaxBufSize;
3339 size_t info_buf_size;
3340 int flags = 0;
3341 unsigned int total_len;
3343 if (ses && (ses->server))
3344 server = ses->server;
3345 else
3346 return -EIO;
3348 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3349 &total_len);
3350 if (rc)
3351 return rc;
3353 if (smb3_encryption_required(tcon))
3354 flags |= CIFS_TRANSFORM_REQ;
3356 switch (srch_inf->info_level) {
3357 case SMB_FIND_FILE_DIRECTORY_INFO:
3358 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3359 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3360 break;
3361 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3362 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3363 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3364 break;
3365 default:
3366 cifs_dbg(VFS, "info level %u isn't supported\n",
3367 srch_inf->info_level);
3368 rc = -EINVAL;
3369 goto qdir_exit;
3372 req->FileIndex = cpu_to_le32(index);
3373 req->PersistentFileId = persistent_fid;
3374 req->VolatileFileId = volatile_fid;
3376 len = 0x2;
3377 bufptr = req->Buffer;
3378 memcpy(bufptr, &asteriks, len);
3380 req->FileNameOffset =
3381 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
3382 req->FileNameLength = cpu_to_le16(len);
3384 * BB could be 30 bytes or so longer if we used SMB2 specific
3385 * buffer lengths, but this is safe and close enough.
3387 output_size = min_t(unsigned int, output_size, server->maxBuf);
3388 output_size = min_t(unsigned int, output_size, 2 << 15);
3389 req->OutputBufferLength = cpu_to_le32(output_size);
3391 iov[0].iov_base = (char *)req;
3392 /* 1 for Buffer */
3393 iov[0].iov_len = total_len - 1;
3395 iov[1].iov_base = (char *)(req->Buffer);
3396 iov[1].iov_len = len;
3398 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
3399 cifs_small_buf_release(req);
3400 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
3402 if (rc) {
3403 if (rc == -ENODATA &&
3404 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
3405 srch_inf->endOfSearch = true;
3406 rc = 0;
3408 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3409 goto qdir_exit;
3412 rc = validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
3413 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
3414 info_buf_size);
3415 if (rc)
3416 goto qdir_exit;
3418 srch_inf->unicode = true;
3420 if (srch_inf->ntwrk_buf_start) {
3421 if (srch_inf->smallBuf)
3422 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3423 else
3424 cifs_buf_release(srch_inf->ntwrk_buf_start);
3426 srch_inf->ntwrk_buf_start = (char *)rsp;
3427 srch_inf->srch_entries_start = srch_inf->last_entry =
3428 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
3429 end_of_smb = rsp_iov.iov_len + (char *)rsp;
3430 srch_inf->entries_in_buffer =
3431 num_entries(srch_inf->srch_entries_start, end_of_smb,
3432 &srch_inf->last_entry, info_buf_size);
3433 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
3434 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3435 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3436 srch_inf->srch_entries_start, srch_inf->last_entry);
3437 if (resp_buftype == CIFS_LARGE_BUFFER)
3438 srch_inf->smallBuf = false;
3439 else if (resp_buftype == CIFS_SMALL_BUFFER)
3440 srch_inf->smallBuf = true;
3441 else
3442 cifs_dbg(VFS, "illegal search buffer type\n");
3444 return rc;
3446 qdir_exit:
3447 free_rsp_buf(resp_buftype, rsp);
3448 return rc;
3451 static int
3452 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3453 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3454 u8 info_type, u32 additional_info, unsigned int num,
3455 void **data, unsigned int *size)
3457 struct smb2_set_info_req *req;
3458 struct smb2_set_info_rsp *rsp = NULL;
3459 struct kvec *iov;
3460 struct kvec rsp_iov;
3461 int rc = 0;
3462 int resp_buftype;
3463 unsigned int i;
3464 struct cifs_ses *ses = tcon->ses;
3465 int flags = 0;
3466 unsigned int total_len;
3468 if (!ses || !(ses->server))
3469 return -EIO;
3471 if (!num)
3472 return -EINVAL;
3474 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
3475 if (!iov)
3476 return -ENOMEM;
3478 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
3479 if (rc) {
3480 kfree(iov);
3481 return rc;
3484 if (smb3_encryption_required(tcon))
3485 flags |= CIFS_TRANSFORM_REQ;
3487 req->sync_hdr.ProcessId = cpu_to_le32(pid);
3489 req->InfoType = info_type;
3490 req->FileInfoClass = info_class;
3491 req->PersistentFileId = persistent_fid;
3492 req->VolatileFileId = volatile_fid;
3493 req->AdditionalInformation = cpu_to_le32(additional_info);
3495 req->BufferOffset =
3496 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
3497 req->BufferLength = cpu_to_le32(*size);
3499 memcpy(req->Buffer, *data, *size);
3500 total_len += *size;
3502 iov[0].iov_base = (char *)req;
3503 /* 1 for Buffer */
3504 iov[0].iov_len = total_len - 1;
3506 for (i = 1; i < num; i++) {
3507 le32_add_cpu(&req->BufferLength, size[i]);
3508 iov[i].iov_base = (char *)data[i];
3509 iov[i].iov_len = size[i];
3512 rc = smb2_send_recv(xid, ses, iov, num, &resp_buftype, flags,
3513 &rsp_iov);
3514 cifs_small_buf_release(req);
3515 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
3517 if (rc != 0) {
3518 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
3519 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
3520 ses->Suid, info_class, (__u32)info_type, rc);
3523 free_rsp_buf(resp_buftype, rsp);
3524 kfree(iov);
3525 return rc;
3529 SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3530 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3532 struct smb2_file_rename_info info;
3533 void **data;
3534 unsigned int size[2];
3535 int rc;
3536 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3538 data = kmalloc_array(2, sizeof(void *), GFP_KERNEL);
3539 if (!data)
3540 return -ENOMEM;
3542 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3543 /* 0 = fail if target already exists */
3544 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3545 info.FileNameLength = cpu_to_le32(len);
3547 data[0] = &info;
3548 size[0] = sizeof(struct smb2_file_rename_info);
3550 data[1] = target_file;
3551 size[1] = len + 2 /* null */;
3553 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
3554 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3555 0, 2, data, size);
3556 kfree(data);
3557 return rc;
3561 SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3562 u64 persistent_fid, u64 volatile_fid)
3564 __u8 delete_pending = 1;
3565 void *data;
3566 unsigned int size;
3568 data = &delete_pending;
3569 size = 1; /* sizeof __u8 */
3571 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3572 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3573 0, 1, &data, &size);
3577 SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3578 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3580 struct smb2_file_link_info info;
3581 void **data;
3582 unsigned int size[2];
3583 int rc;
3584 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3586 data = kmalloc_array(2, sizeof(void *), GFP_KERNEL);
3587 if (!data)
3588 return -ENOMEM;
3590 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3591 /* 0 = fail if link already exists */
3592 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3593 info.FileNameLength = cpu_to_le32(len);
3595 data[0] = &info;
3596 size[0] = sizeof(struct smb2_file_link_info);
3598 data[1] = target_file;
3599 size[1] = len + 2 /* null */;
3601 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
3602 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3603 0, 2, data, size);
3604 kfree(data);
3605 return rc;
3609 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3610 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
3612 struct smb2_file_eof_info info;
3613 void *data;
3614 unsigned int size;
3616 info.EndOfFile = *eof;
3618 data = &info;
3619 size = sizeof(struct smb2_file_eof_info);
3621 if (is_falloc)
3622 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3623 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3624 0, 1, &data, &size);
3625 else
3626 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3627 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3628 0, 1, &data, &size);
3632 SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3633 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3635 unsigned int size;
3636 size = sizeof(FILE_BASIC_INFO);
3637 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3638 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3639 0, 1, (void **)&buf, &size);
3643 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3644 u64 persistent_fid, u64 volatile_fid,
3645 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3647 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3648 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3649 1, (void **)&pnntsd, &pacllen);
3653 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3654 u64 persistent_fid, u64 volatile_fid,
3655 struct smb2_file_full_ea_info *buf, int len)
3657 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3658 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3659 0, 1, (void **)&buf, &len);
3663 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3664 const u64 persistent_fid, const u64 volatile_fid,
3665 __u8 oplock_level)
3667 int rc;
3668 struct smb2_oplock_break *req = NULL;
3669 struct cifs_ses *ses = tcon->ses;
3670 int flags = CIFS_OBREAK_OP;
3671 unsigned int total_len;
3672 struct kvec iov[1];
3673 struct kvec rsp_iov;
3674 int resp_buf_type;
3676 cifs_dbg(FYI, "SMB2_oplock_break\n");
3677 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3678 &total_len);
3679 if (rc)
3680 return rc;
3682 if (smb3_encryption_required(tcon))
3683 flags |= CIFS_TRANSFORM_REQ;
3685 req->VolatileFid = volatile_fid;
3686 req->PersistentFid = persistent_fid;
3687 req->OplockLevel = oplock_level;
3688 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3690 flags |= CIFS_NO_RESP;
3692 iov[0].iov_base = (char *)req;
3693 iov[0].iov_len = total_len;
3695 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
3696 cifs_small_buf_release(req);
3698 if (rc) {
3699 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3700 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
3703 return rc;
3706 static void
3707 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3708 struct kstatfs *kst)
3710 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3711 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3712 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
3713 kst->f_bfree = kst->f_bavail =
3714 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
3715 return;
3718 static int
3719 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3720 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3722 struct TCP_Server_Info *server;
3723 int rc;
3724 struct smb2_query_info_req *req;
3725 unsigned int total_len;
3727 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
3729 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3730 return -EIO;
3732 server = tcon->ses->server;
3734 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3735 &total_len);
3736 if (rc)
3737 return rc;
3739 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3740 req->FileInfoClass = level;
3741 req->PersistentFileId = persistent_fid;
3742 req->VolatileFileId = volatile_fid;
3743 /* 1 for pad */
3744 req->InputBufferOffset =
3745 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
3746 req->OutputBufferLength = cpu_to_le32(
3747 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
3749 iov->iov_base = (char *)req;
3750 iov->iov_len = total_len;
3751 return 0;
3755 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3756 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3758 struct smb2_query_info_rsp *rsp = NULL;
3759 struct kvec iov;
3760 struct kvec rsp_iov;
3761 int rc = 0;
3762 int resp_buftype;
3763 struct cifs_ses *ses = tcon->ses;
3764 struct smb2_fs_full_size_info *info = NULL;
3765 int flags = 0;
3767 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3768 sizeof(struct smb2_fs_full_size_info),
3769 persistent_fid, volatile_fid);
3770 if (rc)
3771 return rc;
3773 if (smb3_encryption_required(tcon))
3774 flags |= CIFS_TRANSFORM_REQ;
3776 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
3777 cifs_small_buf_release(iov.iov_base);
3778 if (rc) {
3779 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3780 goto qfsinf_exit;
3782 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3784 info = (struct smb2_fs_full_size_info *)(
3785 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
3786 rc = validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
3787 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
3788 sizeof(struct smb2_fs_full_size_info));
3789 if (!rc)
3790 copy_fs_info_to_kstatfs(info, fsdata);
3792 qfsinf_exit:
3793 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3794 return rc;
3798 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
3799 u64 persistent_fid, u64 volatile_fid, int level)
3801 struct smb2_query_info_rsp *rsp = NULL;
3802 struct kvec iov;
3803 struct kvec rsp_iov;
3804 int rc = 0;
3805 int resp_buftype, max_len, min_len;
3806 struct cifs_ses *ses = tcon->ses;
3807 unsigned int rsp_len, offset;
3808 int flags = 0;
3810 if (level == FS_DEVICE_INFORMATION) {
3811 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3812 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3813 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3814 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3815 min_len = MIN_FS_ATTR_INFO_SIZE;
3816 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3817 max_len = sizeof(struct smb3_fs_ss_info);
3818 min_len = sizeof(struct smb3_fs_ss_info);
3819 } else {
3820 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
3821 return -EINVAL;
3824 rc = build_qfs_info_req(&iov, tcon, level, max_len,
3825 persistent_fid, volatile_fid);
3826 if (rc)
3827 return rc;
3829 if (smb3_encryption_required(tcon))
3830 flags |= CIFS_TRANSFORM_REQ;
3832 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
3833 cifs_small_buf_release(iov.iov_base);
3834 if (rc) {
3835 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3836 goto qfsattr_exit;
3838 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3840 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3841 offset = le16_to_cpu(rsp->OutputBufferOffset);
3842 rc = validate_iov(offset, rsp_len, &rsp_iov, min_len);
3843 if (rc)
3844 goto qfsattr_exit;
3846 if (level == FS_ATTRIBUTE_INFORMATION)
3847 memcpy(&tcon->fsAttrInfo, offset
3848 + (char *)rsp, min_t(unsigned int,
3849 rsp_len, max_len));
3850 else if (level == FS_DEVICE_INFORMATION)
3851 memcpy(&tcon->fsDevInfo, offset
3852 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
3853 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3854 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3855 (offset + (char *)rsp);
3856 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3857 tcon->perf_sector_size =
3858 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3861 qfsattr_exit:
3862 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3863 return rc;
3867 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3868 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3869 const __u32 num_lock, struct smb2_lock_element *buf)
3871 int rc = 0;
3872 struct smb2_lock_req *req = NULL;
3873 struct kvec iov[2];
3874 struct kvec rsp_iov;
3875 int resp_buf_type;
3876 unsigned int count;
3877 int flags = CIFS_NO_RESP;
3878 unsigned int total_len;
3880 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
3882 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
3883 if (rc)
3884 return rc;
3886 if (smb3_encryption_required(tcon))
3887 flags |= CIFS_TRANSFORM_REQ;
3889 req->sync_hdr.ProcessId = cpu_to_le32(pid);
3890 req->LockCount = cpu_to_le16(num_lock);
3892 req->PersistentFileId = persist_fid;
3893 req->VolatileFileId = volatile_fid;
3895 count = num_lock * sizeof(struct smb2_lock_element);
3897 iov[0].iov_base = (char *)req;
3898 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
3899 iov[1].iov_base = (char *)buf;
3900 iov[1].iov_len = count;
3902 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3903 rc = smb2_send_recv(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
3904 &rsp_iov);
3905 cifs_small_buf_release(req);
3906 if (rc) {
3907 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
3908 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3909 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
3910 tcon->ses->Suid, rc);
3913 return rc;
3917 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3918 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3919 const __u64 length, const __u64 offset, const __u32 lock_flags,
3920 const bool wait)
3922 struct smb2_lock_element lock;
3924 lock.Offset = cpu_to_le64(offset);
3925 lock.Length = cpu_to_le64(length);
3926 lock.Flags = cpu_to_le32(lock_flags);
3927 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3928 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3930 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3934 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3935 __u8 *lease_key, const __le32 lease_state)
3937 int rc;
3938 struct smb2_lease_ack *req = NULL;
3939 struct cifs_ses *ses = tcon->ses;
3940 int flags = CIFS_OBREAK_OP;
3941 unsigned int total_len;
3942 struct kvec iov[1];
3943 struct kvec rsp_iov;
3944 int resp_buf_type;
3946 cifs_dbg(FYI, "SMB2_lease_break\n");
3947 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3948 &total_len);
3949 if (rc)
3950 return rc;
3952 if (smb3_encryption_required(tcon))
3953 flags |= CIFS_TRANSFORM_REQ;
3955 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3956 req->StructureSize = cpu_to_le16(36);
3957 total_len += 12;
3959 memcpy(req->LeaseKey, lease_key, 16);
3960 req->LeaseState = lease_state;
3962 flags |= CIFS_NO_RESP;
3964 iov[0].iov_base = (char *)req;
3965 iov[0].iov_len = total_len;
3967 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
3968 cifs_small_buf_release(req);
3970 if (rc) {
3971 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3972 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
3975 return rc;