Linux 4.2.1
[linux/fpc-iii.git] / fs / cifs / smb2pdu.c
blobb8b4f08ee094e2f8a2b811f076ca0eb4cc3e68b9
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/pagemap.h>
37 #include <linux/xattr.h>
38 #include "smb2pdu.h"
39 #include "cifsglob.h"
40 #include "cifsacl.h"
41 #include "cifsproto.h"
42 #include "smb2proto.h"
43 #include "cifs_unicode.h"
44 #include "cifs_debug.h"
45 #include "ntlmssp.h"
46 #include "smb2status.h"
47 #include "smb2glob.h"
48 #include "cifspdu.h"
51 * The following table defines the expected "StructureSize" of SMB2 requests
52 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
54 * Note that commands are defined in smb2pdu.h in le16 but the array below is
55 * indexed by command in host byte order.
57 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
58 /* SMB2_NEGOTIATE */ 36,
59 /* SMB2_SESSION_SETUP */ 25,
60 /* SMB2_LOGOFF */ 4,
61 /* SMB2_TREE_CONNECT */ 9,
62 /* SMB2_TREE_DISCONNECT */ 4,
63 /* SMB2_CREATE */ 57,
64 /* SMB2_CLOSE */ 24,
65 /* SMB2_FLUSH */ 24,
66 /* SMB2_READ */ 49,
67 /* SMB2_WRITE */ 49,
68 /* SMB2_LOCK */ 48,
69 /* SMB2_IOCTL */ 57,
70 /* SMB2_CANCEL */ 4,
71 /* SMB2_ECHO */ 4,
72 /* SMB2_QUERY_DIRECTORY */ 33,
73 /* SMB2_CHANGE_NOTIFY */ 32,
74 /* SMB2_QUERY_INFO */ 41,
75 /* SMB2_SET_INFO */ 33,
76 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
80 static void
81 smb2_hdr_assemble(struct smb2_hdr *hdr, __le16 smb2_cmd /* command */ ,
82 const struct cifs_tcon *tcon)
84 struct smb2_pdu *pdu = (struct smb2_pdu *)hdr;
85 char *temp = (char *)hdr;
86 /* lookup word count ie StructureSize from table */
87 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_cmd)];
90 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
91 * largest operations (Create)
93 memset(temp, 0, 256);
95 /* Note this is only network field converted to big endian */
96 hdr->smb2_buf_length = cpu_to_be32(parmsize + sizeof(struct smb2_hdr)
97 - 4 /* RFC 1001 length field itself not counted */);
99 hdr->ProtocolId[0] = 0xFE;
100 hdr->ProtocolId[1] = 'S';
101 hdr->ProtocolId[2] = 'M';
102 hdr->ProtocolId[3] = 'B';
103 hdr->StructureSize = cpu_to_le16(64);
104 hdr->Command = smb2_cmd;
105 hdr->CreditRequest = cpu_to_le16(2); /* BB make this dynamic */
106 hdr->ProcessId = cpu_to_le32((__u16)current->tgid);
108 if (!tcon)
109 goto out;
111 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
112 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
113 if ((tcon->ses) && (tcon->ses->server) &&
114 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
115 hdr->CreditCharge = cpu_to_le16(1);
116 /* else CreditCharge MBZ */
118 hdr->TreeId = tcon->tid;
119 /* Uid is not converted */
120 if (tcon->ses)
121 hdr->SessionId = tcon->ses->Suid;
124 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
125 * to pass the path on the Open SMB prefixed by \\server\share.
126 * Not sure when we would need to do the augmented path (if ever) and
127 * setting this flag breaks the SMB2 open operation since it is
128 * illegal to send an empty path name (without \\server\share prefix)
129 * when the DFS flag is set in the SMB open header. We could
130 * consider setting the flag on all operations other than open
131 * but it is safer to net set it for now.
133 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
134 hdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
136 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign)
137 hdr->Flags |= SMB2_FLAGS_SIGNED;
138 out:
139 pdu->StructureSize2 = cpu_to_le16(parmsize);
140 return;
143 static int
144 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
146 int rc = 0;
147 struct nls_table *nls_codepage;
148 struct cifs_ses *ses;
149 struct TCP_Server_Info *server;
152 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
153 * check for tcp and smb session status done differently
154 * for those three - in the calling routine.
156 if (tcon == NULL)
157 return rc;
159 if (smb2_command == SMB2_TREE_CONNECT)
160 return rc;
162 if (tcon->tidStatus == CifsExiting) {
164 * only tree disconnect, open, and write,
165 * (and ulogoff which does not have tcon)
166 * are allowed as we start force umount.
168 if ((smb2_command != SMB2_WRITE) &&
169 (smb2_command != SMB2_CREATE) &&
170 (smb2_command != SMB2_TREE_DISCONNECT)) {
171 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
172 smb2_command);
173 return -ENODEV;
176 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
177 (!tcon->ses->server))
178 return -EIO;
180 ses = tcon->ses;
181 server = ses->server;
184 * Give demultiplex thread up to 10 seconds to reconnect, should be
185 * greater than cifs socket timeout which is 7 seconds
187 while (server->tcpStatus == CifsNeedReconnect) {
189 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
190 * here since they are implicitly done when session drops.
192 switch (smb2_command) {
194 * BB Should we keep oplock break and add flush to exceptions?
196 case SMB2_TREE_DISCONNECT:
197 case SMB2_CANCEL:
198 case SMB2_CLOSE:
199 case SMB2_OPLOCK_BREAK:
200 return -EAGAIN;
203 wait_event_interruptible_timeout(server->response_q,
204 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
206 /* are we still trying to reconnect? */
207 if (server->tcpStatus != CifsNeedReconnect)
208 break;
211 * on "soft" mounts we wait once. Hard mounts keep
212 * retrying until process is killed or server comes
213 * back on-line
215 if (!tcon->retry) {
216 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
217 return -EHOSTDOWN;
221 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
222 return rc;
224 nls_codepage = load_nls_default();
227 * need to prevent multiple threads trying to simultaneously reconnect
228 * the same SMB session
230 mutex_lock(&tcon->ses->session_mutex);
231 rc = cifs_negotiate_protocol(0, tcon->ses);
232 if (!rc && tcon->ses->need_reconnect)
233 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
235 if (rc || !tcon->need_reconnect) {
236 mutex_unlock(&tcon->ses->session_mutex);
237 goto out;
240 cifs_mark_open_files_invalid(tcon);
241 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
242 mutex_unlock(&tcon->ses->session_mutex);
243 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
244 if (rc)
245 goto out;
246 atomic_inc(&tconInfoReconnectCount);
247 out:
249 * Check if handle based operation so we know whether we can continue
250 * or not without returning to caller to reset file handle.
253 * BB Is flush done by server on drop of tcp session? Should we special
254 * case it and skip above?
256 switch (smb2_command) {
257 case SMB2_FLUSH:
258 case SMB2_READ:
259 case SMB2_WRITE:
260 case SMB2_LOCK:
261 case SMB2_IOCTL:
262 case SMB2_QUERY_DIRECTORY:
263 case SMB2_CHANGE_NOTIFY:
264 case SMB2_QUERY_INFO:
265 case SMB2_SET_INFO:
266 return -EAGAIN;
268 unload_nls(nls_codepage);
269 return rc;
273 * Allocate and return pointer to an SMB request hdr, and set basic
274 * SMB information in the SMB header. If the return code is zero, this
275 * function must have filled in request_buf pointer.
277 static int
278 small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
279 void **request_buf)
281 int rc = 0;
283 rc = smb2_reconnect(smb2_command, tcon);
284 if (rc)
285 return rc;
287 /* BB eventually switch this to SMB2 specific small buf size */
288 *request_buf = cifs_small_buf_get();
289 if (*request_buf == NULL) {
290 /* BB should we add a retry in here if not a writepage? */
291 return -ENOMEM;
294 smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
296 if (tcon != NULL) {
297 #ifdef CONFIG_CIFS_STATS2
298 uint16_t com_code = le16_to_cpu(smb2_command);
299 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
300 #endif
301 cifs_stats_inc(&tcon->num_smbs_sent);
304 return rc;
307 #ifdef CONFIG_CIFS_SMB311
308 /* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
309 #define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
312 #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
313 #define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
315 static void
316 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
318 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
319 pneg_ctxt->DataLength = cpu_to_le16(38);
320 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
321 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
322 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
323 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
326 static void
327 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
329 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
330 pneg_ctxt->DataLength = cpu_to_le16(6);
331 pneg_ctxt->CipherCount = cpu_to_le16(2);
332 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
333 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
336 static void
337 assemble_neg_contexts(struct smb2_negotiate_req *req)
340 /* +4 is to account for the RFC1001 len field */
341 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
343 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
344 /* Add 2 to size to round to 8 byte boundary */
345 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
346 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
347 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
348 req->NegotiateContextCount = cpu_to_le16(2);
349 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context) + 2
350 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
352 #else
353 static void assemble_neg_contexts(struct smb2_negotiate_req *req)
355 return;
357 #endif /* SMB311 */
362 * SMB2 Worker functions follow:
364 * The general structure of the worker functions is:
365 * 1) Call smb2_init (assembles SMB2 header)
366 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
367 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
368 * 4) Decode SMB2 command specific fields in the fixed length area
369 * 5) Decode variable length data area (if any for this SMB2 command type)
370 * 6) Call free smb buffer
371 * 7) return
376 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
378 struct smb2_negotiate_req *req;
379 struct smb2_negotiate_rsp *rsp;
380 struct kvec iov[1];
381 int rc = 0;
382 int resp_buftype;
383 struct TCP_Server_Info *server = ses->server;
384 int blob_offset, blob_length;
385 char *security_blob;
386 int flags = CIFS_NEG_OP;
388 cifs_dbg(FYI, "Negotiate protocol\n");
390 if (!server) {
391 WARN(1, "%s: server is NULL!\n", __func__);
392 return -EIO;
395 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
396 if (rc)
397 return rc;
399 req->hdr.SessionId = 0;
401 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
403 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
404 inc_rfc1001_len(req, 2);
406 /* only one of SMB2 signing flags may be set in SMB2 request */
407 if (ses->sign)
408 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
409 else if (global_secflags & CIFSSEC_MAY_SIGN)
410 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
411 else
412 req->SecurityMode = 0;
414 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
416 /* ClientGUID must be zero for SMB2.02 dialect */
417 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
418 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
419 else {
420 memcpy(req->ClientGUID, server->client_guid,
421 SMB2_CLIENT_GUID_SIZE);
422 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
423 assemble_neg_contexts(req);
425 iov[0].iov_base = (char *)req;
426 /* 4 for rfc1002 length field */
427 iov[0].iov_len = get_rfc1002_length(req) + 4;
429 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
431 rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
433 * No tcon so can't do
434 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
436 if (rc != 0)
437 goto neg_exit;
439 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
441 /* BB we may eventually want to match the negotiated vs. requested
442 dialect, even though we are only requesting one at a time */
443 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
444 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
445 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
446 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
447 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
448 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
449 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
450 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
451 #ifdef CONFIG_CIFS_SMB311
452 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
453 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
454 #endif /* SMB311 */
455 else {
456 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
457 le16_to_cpu(rsp->DialectRevision));
458 rc = -EIO;
459 goto neg_exit;
461 server->dialect = le16_to_cpu(rsp->DialectRevision);
463 /* SMB2 only has an extended negflavor */
464 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
465 /* set it to the maximum buffer size value we can send with 1 credit */
466 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
467 SMB2_MAX_BUFFER_SIZE);
468 server->max_read = le32_to_cpu(rsp->MaxReadSize);
469 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
470 /* BB Do we need to validate the SecurityMode? */
471 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
472 server->capabilities = le32_to_cpu(rsp->Capabilities);
473 /* Internal types */
474 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
476 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
477 &rsp->hdr);
479 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
480 * for us will be
481 * ses->sectype = RawNTLMSSP;
482 * but for time being this is our only auth choice so doesn't matter.
483 * We just found a server which sets blob length to zero expecting raw.
485 if (blob_length == 0)
486 cifs_dbg(FYI, "missing security blob on negprot\n");
488 rc = cifs_enable_signing(server, ses->sign);
489 #ifdef CONFIG_SMB2_ASN1 /* BB REMOVEME when updated asn1.c ready */
490 if (rc)
491 goto neg_exit;
492 if (blob_length)
493 rc = decode_negTokenInit(security_blob, blob_length, server);
494 if (rc == 1)
495 rc = 0;
496 else if (rc == 0) {
497 rc = -EIO;
498 goto neg_exit;
500 #endif
502 neg_exit:
503 free_rsp_buf(resp_buftype, rsp);
504 return rc;
507 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
509 int rc = 0;
510 struct validate_negotiate_info_req vneg_inbuf;
511 struct validate_negotiate_info_rsp *pneg_rsp;
512 u32 rsplen;
514 cifs_dbg(FYI, "validate negotiate\n");
517 * validation ioctl must be signed, so no point sending this if we
518 * can not sign it. We could eventually change this to selectively
519 * sign just this, the first and only signed request on a connection.
520 * This is good enough for now since a user who wants better security
521 * would also enable signing on the mount. Having validation of
522 * negotiate info for signed connections helps reduce attack vectors
524 if (tcon->ses->server->sign == false)
525 return 0; /* validation requires signing */
527 vneg_inbuf.Capabilities =
528 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
529 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
530 SMB2_CLIENT_GUID_SIZE);
532 if (tcon->ses->sign)
533 vneg_inbuf.SecurityMode =
534 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
535 else if (global_secflags & CIFSSEC_MAY_SIGN)
536 vneg_inbuf.SecurityMode =
537 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
538 else
539 vneg_inbuf.SecurityMode = 0;
541 vneg_inbuf.DialectCount = cpu_to_le16(1);
542 vneg_inbuf.Dialects[0] =
543 cpu_to_le16(tcon->ses->server->vals->protocol_id);
545 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
546 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
547 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
548 (char **)&pneg_rsp, &rsplen);
550 if (rc != 0) {
551 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
552 return -EIO;
555 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
556 cifs_dbg(VFS, "invalid size of protocol negotiate response\n");
557 return -EIO;
560 /* check validate negotiate info response matches what we got earlier */
561 if (pneg_rsp->Dialect !=
562 cpu_to_le16(tcon->ses->server->vals->protocol_id))
563 goto vneg_out;
565 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
566 goto vneg_out;
568 /* do not validate server guid because not saved at negprot time yet */
570 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
571 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
572 goto vneg_out;
574 /* validate negotiate successful */
575 cifs_dbg(FYI, "validate negotiate info successful\n");
576 return 0;
578 vneg_out:
579 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
580 return -EIO;
584 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
585 const struct nls_table *nls_cp)
587 struct smb2_sess_setup_req *req;
588 struct smb2_sess_setup_rsp *rsp = NULL;
589 struct kvec iov[2];
590 int rc = 0;
591 int resp_buftype = CIFS_NO_BUFFER;
592 __le32 phase = NtLmNegotiate; /* NTLMSSP, if needed, is multistage */
593 struct TCP_Server_Info *server = ses->server;
594 u16 blob_length = 0;
595 char *security_blob;
596 char *ntlmssp_blob = NULL;
597 bool use_spnego = false; /* else use raw ntlmssp */
599 cifs_dbg(FYI, "Session Setup\n");
601 if (!server) {
602 WARN(1, "%s: server is NULL!\n", __func__);
603 return -EIO;
607 * If we are here due to reconnect, free per-smb session key
608 * in case signing was required.
610 kfree(ses->auth_key.response);
611 ses->auth_key.response = NULL;
614 * If memory allocation is successful, caller of this function
615 * frees it.
617 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
618 if (!ses->ntlmssp)
619 return -ENOMEM;
620 ses->ntlmssp->sesskey_per_smbsess = true;
622 /* FIXME: allow for other auth types besides NTLMSSP (e.g. krb5) */
623 ses->sectype = RawNTLMSSP;
625 ssetup_ntlmssp_authenticate:
626 if (phase == NtLmChallenge)
627 phase = NtLmAuthenticate; /* if ntlmssp, now final phase */
629 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
630 if (rc)
631 return rc;
633 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
634 req->Flags = 0; /* MBZ */
635 /* to enable echos and oplocks */
636 req->hdr.CreditRequest = cpu_to_le16(3);
638 /* only one of SMB2 signing flags may be set in SMB2 request */
639 if (server->sign)
640 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
641 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
642 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
643 else
644 req->SecurityMode = 0;
646 req->Capabilities = 0;
647 req->Channel = 0; /* MBZ */
649 iov[0].iov_base = (char *)req;
650 /* 4 for rfc1002 length field and 1 for pad */
651 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
652 if (phase == NtLmNegotiate) {
653 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
654 GFP_KERNEL);
655 if (ntlmssp_blob == NULL) {
656 rc = -ENOMEM;
657 goto ssetup_exit;
659 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
660 if (use_spnego) {
661 /* blob_length = build_spnego_ntlmssp_blob(
662 &security_blob,
663 sizeof(struct _NEGOTIATE_MESSAGE),
664 ntlmssp_blob); */
665 /* BB eventually need to add this */
666 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
667 rc = -EOPNOTSUPP;
668 kfree(ntlmssp_blob);
669 goto ssetup_exit;
670 } else {
671 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
672 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
673 security_blob = ntlmssp_blob;
675 } else if (phase == NtLmAuthenticate) {
676 req->hdr.SessionId = ses->Suid;
677 ntlmssp_blob = kzalloc(sizeof(struct _NEGOTIATE_MESSAGE) + 500,
678 GFP_KERNEL);
679 if (ntlmssp_blob == NULL) {
680 rc = -ENOMEM;
681 goto ssetup_exit;
683 rc = build_ntlmssp_auth_blob(ntlmssp_blob, &blob_length, ses,
684 nls_cp);
685 if (rc) {
686 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n",
687 rc);
688 goto ssetup_exit; /* BB double check error handling */
690 if (use_spnego) {
691 /* blob_length = build_spnego_ntlmssp_blob(
692 &security_blob,
693 blob_length,
694 ntlmssp_blob); */
695 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
696 rc = -EOPNOTSUPP;
697 kfree(ntlmssp_blob);
698 goto ssetup_exit;
699 } else {
700 security_blob = ntlmssp_blob;
702 } else {
703 cifs_dbg(VFS, "illegal ntlmssp phase\n");
704 rc = -EIO;
705 goto ssetup_exit;
708 /* Testing shows that buffer offset must be at location of Buffer[0] */
709 req->SecurityBufferOffset =
710 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
711 1 /* pad */ - 4 /* rfc1001 len */);
712 req->SecurityBufferLength = cpu_to_le16(blob_length);
713 iov[1].iov_base = security_blob;
714 iov[1].iov_len = blob_length;
716 inc_rfc1001_len(req, blob_length - 1 /* pad */);
718 /* BB add code to build os and lm fields */
720 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype,
721 CIFS_LOG_ERROR | CIFS_NEG_OP);
723 kfree(security_blob);
724 rsp = (struct smb2_sess_setup_rsp *)iov[0].iov_base;
725 if (resp_buftype != CIFS_NO_BUFFER &&
726 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) {
727 if (phase != NtLmNegotiate) {
728 cifs_dbg(VFS, "Unexpected more processing error\n");
729 goto ssetup_exit;
731 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
732 le16_to_cpu(rsp->SecurityBufferOffset)) {
733 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
734 le16_to_cpu(rsp->SecurityBufferOffset));
735 rc = -EIO;
736 goto ssetup_exit;
739 /* NTLMSSP Negotiate sent now processing challenge (response) */
740 phase = NtLmChallenge; /* process ntlmssp challenge */
741 rc = 0; /* MORE_PROCESSING is not an error here but expected */
742 ses->Suid = rsp->hdr.SessionId;
743 rc = decode_ntlmssp_challenge(rsp->Buffer,
744 le16_to_cpu(rsp->SecurityBufferLength), ses);
748 * BB eventually add code for SPNEGO decoding of NtlmChallenge blob,
749 * but at least the raw NTLMSSP case works.
752 * No tcon so can't do
753 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
755 if (rc != 0)
756 goto ssetup_exit;
758 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
759 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
760 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
761 ssetup_exit:
762 free_rsp_buf(resp_buftype, rsp);
764 /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
765 if ((phase == NtLmChallenge) && (rc == 0))
766 goto ssetup_ntlmssp_authenticate;
768 if (!rc) {
769 mutex_lock(&server->srv_mutex);
770 if (server->sign && server->ops->generate_signingkey) {
771 rc = server->ops->generate_signingkey(ses);
772 kfree(ses->auth_key.response);
773 ses->auth_key.response = NULL;
774 if (rc) {
775 cifs_dbg(FYI,
776 "SMB3 session key generation failed\n");
777 mutex_unlock(&server->srv_mutex);
778 goto keygen_exit;
781 if (!server->session_estab) {
782 server->sequence_number = 0x2;
783 server->session_estab = true;
785 mutex_unlock(&server->srv_mutex);
787 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
788 spin_lock(&GlobalMid_Lock);
789 ses->status = CifsGood;
790 ses->need_reconnect = false;
791 spin_unlock(&GlobalMid_Lock);
794 keygen_exit:
795 if (!server->sign) {
796 kfree(ses->auth_key.response);
797 ses->auth_key.response = NULL;
799 kfree(ses->ntlmssp);
801 return rc;
805 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
807 struct smb2_logoff_req *req; /* response is also trivial struct */
808 int rc = 0;
809 struct TCP_Server_Info *server;
811 cifs_dbg(FYI, "disconnect session %p\n", ses);
813 if (ses && (ses->server))
814 server = ses->server;
815 else
816 return -EIO;
818 /* no need to send SMB logoff if uid already closed due to reconnect */
819 if (ses->need_reconnect)
820 goto smb2_session_already_dead;
822 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
823 if (rc)
824 return rc;
826 /* since no tcon, smb2_init can not do this, so do here */
827 req->hdr.SessionId = ses->Suid;
828 if (server->sign)
829 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
831 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
833 * No tcon so can't do
834 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
837 smb2_session_already_dead:
838 return rc;
841 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
843 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
846 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
848 /* These are similar values to what Windows uses */
849 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
851 tcon->max_chunks = 256;
852 tcon->max_bytes_chunk = 1048576;
853 tcon->max_bytes_copy = 16777216;
857 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
858 struct cifs_tcon *tcon, const struct nls_table *cp)
860 struct smb2_tree_connect_req *req;
861 struct smb2_tree_connect_rsp *rsp = NULL;
862 struct kvec iov[2];
863 int rc = 0;
864 int resp_buftype;
865 int unc_path_len;
866 struct TCP_Server_Info *server;
867 __le16 *unc_path = NULL;
869 cifs_dbg(FYI, "TCON\n");
871 if ((ses->server) && tree)
872 server = ses->server;
873 else
874 return -EIO;
876 if (tcon && tcon->bad_network_name)
877 return -ENOENT;
879 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
880 if (unc_path == NULL)
881 return -ENOMEM;
883 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
884 unc_path_len *= 2;
885 if (unc_path_len < 2) {
886 kfree(unc_path);
887 return -EINVAL;
890 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
891 if (rc) {
892 kfree(unc_path);
893 return rc;
896 if (tcon == NULL) {
897 /* since no tcon, smb2_init can not do this, so do here */
898 req->hdr.SessionId = ses->Suid;
899 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
900 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
903 iov[0].iov_base = (char *)req;
904 /* 4 for rfc1002 length field and 1 for pad */
905 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
907 /* Testing shows that buffer offset must be at location of Buffer[0] */
908 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
909 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
910 req->PathLength = cpu_to_le16(unc_path_len - 2);
911 iov[1].iov_base = unc_path;
912 iov[1].iov_len = unc_path_len;
914 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
916 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
917 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
919 if (rc != 0) {
920 if (tcon) {
921 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
922 tcon->need_reconnect = true;
924 goto tcon_error_exit;
927 if (tcon == NULL) {
928 ses->ipc_tid = rsp->hdr.TreeId;
929 goto tcon_exit;
932 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
933 cifs_dbg(FYI, "connection to disk share\n");
934 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
935 tcon->ipc = true;
936 cifs_dbg(FYI, "connection to pipe share\n");
937 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
938 tcon->print = true;
939 cifs_dbg(FYI, "connection to printer\n");
940 } else {
941 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
942 rc = -EOPNOTSUPP;
943 goto tcon_error_exit;
946 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
947 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
948 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
949 tcon->tidStatus = CifsGood;
950 tcon->need_reconnect = false;
951 tcon->tid = rsp->hdr.TreeId;
952 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
954 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
955 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
956 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
957 init_copy_chunk_defaults(tcon);
958 if (tcon->ses->server->ops->validate_negotiate)
959 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
960 tcon_exit:
961 free_rsp_buf(resp_buftype, rsp);
962 kfree(unc_path);
963 return rc;
965 tcon_error_exit:
966 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
967 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
968 if (tcon)
969 tcon->bad_network_name = true;
971 goto tcon_exit;
975 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
977 struct smb2_tree_disconnect_req *req; /* response is trivial */
978 int rc = 0;
979 struct TCP_Server_Info *server;
980 struct cifs_ses *ses = tcon->ses;
982 cifs_dbg(FYI, "Tree Disconnect\n");
984 if (ses && (ses->server))
985 server = ses->server;
986 else
987 return -EIO;
989 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
990 return 0;
992 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
993 if (rc)
994 return rc;
996 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
997 if (rc)
998 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1000 return rc;
1004 static struct create_durable *
1005 create_durable_buf(void)
1007 struct create_durable *buf;
1009 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1010 if (!buf)
1011 return NULL;
1013 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1014 (struct create_durable, Data));
1015 buf->ccontext.DataLength = cpu_to_le32(16);
1016 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1017 (struct create_durable, Name));
1018 buf->ccontext.NameLength = cpu_to_le16(4);
1019 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1020 buf->Name[0] = 'D';
1021 buf->Name[1] = 'H';
1022 buf->Name[2] = 'n';
1023 buf->Name[3] = 'Q';
1024 return buf;
1027 static struct create_durable *
1028 create_reconnect_durable_buf(struct cifs_fid *fid)
1030 struct create_durable *buf;
1032 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1033 if (!buf)
1034 return NULL;
1036 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1037 (struct create_durable, Data));
1038 buf->ccontext.DataLength = cpu_to_le32(16);
1039 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1040 (struct create_durable, Name));
1041 buf->ccontext.NameLength = cpu_to_le16(4);
1042 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1043 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1044 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1045 buf->Name[0] = 'D';
1046 buf->Name[1] = 'H';
1047 buf->Name[2] = 'n';
1048 buf->Name[3] = 'C';
1049 return buf;
1052 static __u8
1053 parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1054 unsigned int *epoch)
1056 char *data_offset;
1057 struct create_context *cc;
1058 unsigned int next = 0;
1059 char *name;
1061 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
1062 cc = (struct create_context *)data_offset;
1063 do {
1064 cc = (struct create_context *)((char *)cc + next);
1065 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1066 if (le16_to_cpu(cc->NameLength) != 4 ||
1067 strncmp(name, "RqLs", 4)) {
1068 next = le32_to_cpu(cc->Next);
1069 continue;
1071 return server->ops->parse_lease_buf(cc, epoch);
1072 } while (next != 0);
1074 return 0;
1077 static int
1078 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1079 unsigned int *num_iovec, __u8 *oplock)
1081 struct smb2_create_req *req = iov[0].iov_base;
1082 unsigned int num = *num_iovec;
1084 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
1085 if (iov[num].iov_base == NULL)
1086 return -ENOMEM;
1087 iov[num].iov_len = server->vals->create_lease_size;
1088 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1089 if (!req->CreateContextsOffset)
1090 req->CreateContextsOffset = cpu_to_le32(
1091 sizeof(struct smb2_create_req) - 4 +
1092 iov[num - 1].iov_len);
1093 le32_add_cpu(&req->CreateContextsLength,
1094 server->vals->create_lease_size);
1095 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
1096 *num_iovec = num + 1;
1097 return 0;
1100 static int
1101 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1102 struct cifs_open_parms *oparms)
1104 struct smb2_create_req *req = iov[0].iov_base;
1105 unsigned int num = *num_iovec;
1107 if (oparms->reconnect) {
1108 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1109 /* indicate that we don't need to relock the file */
1110 oparms->reconnect = false;
1111 } else
1112 iov[num].iov_base = create_durable_buf();
1113 if (iov[num].iov_base == NULL)
1114 return -ENOMEM;
1115 iov[num].iov_len = sizeof(struct create_durable);
1116 if (!req->CreateContextsOffset)
1117 req->CreateContextsOffset =
1118 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1119 iov[1].iov_len);
1120 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1121 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1122 *num_iovec = num + 1;
1123 return 0;
1127 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
1128 __u8 *oplock, struct smb2_file_all_info *buf,
1129 struct smb2_err_rsp **err_buf)
1131 struct smb2_create_req *req;
1132 struct smb2_create_rsp *rsp;
1133 struct TCP_Server_Info *server;
1134 struct cifs_tcon *tcon = oparms->tcon;
1135 struct cifs_ses *ses = tcon->ses;
1136 struct kvec iov[4];
1137 int resp_buftype;
1138 int uni_path_len;
1139 __le16 *copy_path = NULL;
1140 int copy_size;
1141 int rc = 0;
1142 unsigned int num_iovecs = 2;
1143 __u32 file_attributes = 0;
1144 char *dhc_buf = NULL, *lc_buf = NULL;
1146 cifs_dbg(FYI, "create/open\n");
1148 if (ses && (ses->server))
1149 server = ses->server;
1150 else
1151 return -EIO;
1153 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1154 if (rc)
1155 return rc;
1157 if (oparms->create_options & CREATE_OPTION_READONLY)
1158 file_attributes |= ATTR_READONLY;
1159 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1160 file_attributes |= ATTR_SYSTEM;
1162 req->ImpersonationLevel = IL_IMPERSONATION;
1163 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
1164 /* File attributes ignored on open (used in create though) */
1165 req->FileAttributes = cpu_to_le32(file_attributes);
1166 req->ShareAccess = FILE_SHARE_ALL_LE;
1167 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1168 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
1169 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1170 /* do not count rfc1001 len field */
1171 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1173 iov[0].iov_base = (char *)req;
1174 /* 4 for rfc1002 length field */
1175 iov[0].iov_len = get_rfc1002_length(req) + 4;
1177 /* MUST set path len (NameLength) to 0 opening root of share */
1178 req->NameLength = cpu_to_le16(uni_path_len - 2);
1179 /* -1 since last byte is buf[0] which is sent below (path) */
1180 iov[0].iov_len--;
1181 if (uni_path_len % 8 != 0) {
1182 copy_size = uni_path_len / 8 * 8;
1183 if (copy_size < uni_path_len)
1184 copy_size += 8;
1186 copy_path = kzalloc(copy_size, GFP_KERNEL);
1187 if (!copy_path)
1188 return -ENOMEM;
1189 memcpy((char *)copy_path, (const char *)path,
1190 uni_path_len);
1191 uni_path_len = copy_size;
1192 path = copy_path;
1195 iov[1].iov_len = uni_path_len;
1196 iov[1].iov_base = path;
1197 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1198 inc_rfc1001_len(req, uni_path_len - 1);
1200 if (!server->oplocks)
1201 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1203 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
1204 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1205 req->RequestedOplockLevel = *oplock;
1206 else {
1207 rc = add_lease_context(server, iov, &num_iovecs, oplock);
1208 if (rc) {
1209 cifs_small_buf_release(req);
1210 kfree(copy_path);
1211 return rc;
1213 lc_buf = iov[num_iovecs-1].iov_base;
1216 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1217 /* need to set Next field of lease context if we request it */
1218 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
1219 struct create_context *ccontext =
1220 (struct create_context *)iov[num_iovecs-1].iov_base;
1221 ccontext->Next =
1222 cpu_to_le32(server->vals->create_lease_size);
1224 rc = add_durable_context(iov, &num_iovecs, oparms);
1225 if (rc) {
1226 cifs_small_buf_release(req);
1227 kfree(copy_path);
1228 kfree(lc_buf);
1229 return rc;
1231 dhc_buf = iov[num_iovecs-1].iov_base;
1234 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1235 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1237 if (rc != 0) {
1238 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
1239 if (err_buf)
1240 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1241 GFP_KERNEL);
1242 goto creat_exit;
1245 oparms->fid->persistent_fid = rsp->PersistentFileId;
1246 oparms->fid->volatile_fid = rsp->VolatileFileId;
1248 if (buf) {
1249 memcpy(buf, &rsp->CreationTime, 32);
1250 buf->AllocationSize = rsp->AllocationSize;
1251 buf->EndOfFile = rsp->EndofFile;
1252 buf->Attributes = rsp->FileAttributes;
1253 buf->NumberOfLinks = cpu_to_le32(1);
1254 buf->DeletePending = 0;
1257 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
1258 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
1259 else
1260 *oplock = rsp->OplockLevel;
1261 creat_exit:
1262 kfree(copy_path);
1263 kfree(lc_buf);
1264 kfree(dhc_buf);
1265 free_rsp_buf(resp_buftype, rsp);
1266 return rc;
1270 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1273 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1274 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1275 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1277 struct smb2_ioctl_req *req;
1278 struct smb2_ioctl_rsp *rsp;
1279 struct TCP_Server_Info *server;
1280 struct cifs_ses *ses;
1281 struct kvec iov[2];
1282 int resp_buftype;
1283 int num_iovecs;
1284 int rc = 0;
1286 cifs_dbg(FYI, "SMB2 IOCTL\n");
1288 if (out_data != NULL)
1289 *out_data = NULL;
1291 /* zero out returned data len, in case of error */
1292 if (plen)
1293 *plen = 0;
1295 if (tcon)
1296 ses = tcon->ses;
1297 else
1298 return -EIO;
1300 if (ses && (ses->server))
1301 server = ses->server;
1302 else
1303 return -EIO;
1305 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1306 if (rc)
1307 return rc;
1309 req->CtlCode = cpu_to_le32(opcode);
1310 req->PersistentFileId = persistent_fid;
1311 req->VolatileFileId = volatile_fid;
1313 if (indatalen) {
1314 req->InputCount = cpu_to_le32(indatalen);
1315 /* do not set InputOffset if no input data */
1316 req->InputOffset =
1317 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1318 iov[1].iov_base = in_data;
1319 iov[1].iov_len = indatalen;
1320 num_iovecs = 2;
1321 } else
1322 num_iovecs = 1;
1324 req->OutputOffset = 0;
1325 req->OutputCount = 0; /* MBZ */
1328 * Could increase MaxOutputResponse, but that would require more
1329 * than one credit. Windows typically sets this smaller, but for some
1330 * ioctls it may be useful to allow server to send more. No point
1331 * limiting what the server can send as long as fits in one credit
1333 req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1335 if (is_fsctl)
1336 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1337 else
1338 req->Flags = 0;
1340 iov[0].iov_base = (char *)req;
1343 * If no input data, the size of ioctl struct in
1344 * protocol spec still includes a 1 byte data buffer,
1345 * but if input data passed to ioctl, we do not
1346 * want to double count this, so we do not send
1347 * the dummy one byte of data in iovec[0] if sending
1348 * input data (in iovec[1]). We also must add 4 bytes
1349 * in first iovec to allow for rfc1002 length field.
1352 if (indatalen) {
1353 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1354 inc_rfc1001_len(req, indatalen - 1);
1355 } else
1356 iov[0].iov_len = get_rfc1002_length(req) + 4;
1359 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1360 rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1362 if ((rc != 0) && (rc != -EINVAL)) {
1363 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1364 goto ioctl_exit;
1365 } else if (rc == -EINVAL) {
1366 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1367 (opcode != FSCTL_SRV_COPYCHUNK)) {
1368 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1369 goto ioctl_exit;
1373 /* check if caller wants to look at return data or just return rc */
1374 if ((plen == NULL) || (out_data == NULL))
1375 goto ioctl_exit;
1377 *plen = le32_to_cpu(rsp->OutputCount);
1379 /* We check for obvious errors in the output buffer length and offset */
1380 if (*plen == 0)
1381 goto ioctl_exit; /* server returned no data */
1382 else if (*plen > 0xFF00) {
1383 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1384 *plen = 0;
1385 rc = -EIO;
1386 goto ioctl_exit;
1389 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1390 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1391 le32_to_cpu(rsp->OutputOffset));
1392 *plen = 0;
1393 rc = -EIO;
1394 goto ioctl_exit;
1397 *out_data = kmalloc(*plen, GFP_KERNEL);
1398 if (*out_data == NULL) {
1399 rc = -ENOMEM;
1400 goto ioctl_exit;
1403 memcpy(*out_data, rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
1404 *plen);
1405 ioctl_exit:
1406 free_rsp_buf(resp_buftype, rsp);
1407 return rc;
1411 * Individual callers to ioctl worker function follow
1415 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1416 u64 persistent_fid, u64 volatile_fid)
1418 int rc;
1419 struct compress_ioctl fsctl_input;
1420 char *ret_data = NULL;
1422 fsctl_input.CompressionState =
1423 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
1425 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1426 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1427 (char *)&fsctl_input /* data input */,
1428 2 /* in data len */, &ret_data /* out data */, NULL);
1430 cifs_dbg(FYI, "set compression rc %d\n", rc);
1432 return rc;
1436 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1437 u64 persistent_fid, u64 volatile_fid)
1439 struct smb2_close_req *req;
1440 struct smb2_close_rsp *rsp;
1441 struct TCP_Server_Info *server;
1442 struct cifs_ses *ses = tcon->ses;
1443 struct kvec iov[1];
1444 int resp_buftype;
1445 int rc = 0;
1447 cifs_dbg(FYI, "Close\n");
1449 if (ses && (ses->server))
1450 server = ses->server;
1451 else
1452 return -EIO;
1454 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1455 if (rc)
1456 return rc;
1458 req->PersistentFileId = persistent_fid;
1459 req->VolatileFileId = volatile_fid;
1461 iov[0].iov_base = (char *)req;
1462 /* 4 for rfc1002 length field */
1463 iov[0].iov_len = get_rfc1002_length(req) + 4;
1465 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1466 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1468 if (rc != 0) {
1469 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
1470 goto close_exit;
1473 /* BB FIXME - decode close response, update inode for caching */
1475 close_exit:
1476 free_rsp_buf(resp_buftype, rsp);
1477 return rc;
1480 static int
1481 validate_buf(unsigned int offset, unsigned int buffer_length,
1482 struct smb2_hdr *hdr, unsigned int min_buf_size)
1485 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1486 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1487 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1488 char *end_of_buf = begin_of_buf + buffer_length;
1491 if (buffer_length < min_buf_size) {
1492 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1493 buffer_length, min_buf_size);
1494 return -EINVAL;
1497 /* check if beyond RFC1001 maximum length */
1498 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
1499 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1500 buffer_length, smb_len);
1501 return -EINVAL;
1504 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
1505 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
1506 return -EINVAL;
1509 return 0;
1513 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1514 * Caller must free buffer.
1516 static int
1517 validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1518 struct smb2_hdr *hdr, unsigned int minbufsize,
1519 char *data)
1522 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1523 int rc;
1525 if (!data)
1526 return -EINVAL;
1528 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1529 if (rc)
1530 return rc;
1532 memcpy(data, begin_of_buf, buffer_length);
1534 return 0;
1537 static int
1538 query_info(const unsigned int xid, struct cifs_tcon *tcon,
1539 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1540 size_t output_len, size_t min_len, void *data)
1542 struct smb2_query_info_req *req;
1543 struct smb2_query_info_rsp *rsp = NULL;
1544 struct kvec iov[2];
1545 int rc = 0;
1546 int resp_buftype;
1547 struct TCP_Server_Info *server;
1548 struct cifs_ses *ses = tcon->ses;
1550 cifs_dbg(FYI, "Query Info\n");
1552 if (ses && (ses->server))
1553 server = ses->server;
1554 else
1555 return -EIO;
1557 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1558 if (rc)
1559 return rc;
1561 req->InfoType = SMB2_O_INFO_FILE;
1562 req->FileInfoClass = info_class;
1563 req->PersistentFileId = persistent_fid;
1564 req->VolatileFileId = volatile_fid;
1565 /* 4 for rfc1002 length field and 1 for Buffer */
1566 req->InputBufferOffset =
1567 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
1568 req->OutputBufferLength = cpu_to_le32(output_len);
1570 iov[0].iov_base = (char *)req;
1571 /* 4 for rfc1002 length field */
1572 iov[0].iov_len = get_rfc1002_length(req) + 4;
1574 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1575 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1577 if (rc) {
1578 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1579 goto qinf_exit;
1582 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1583 le32_to_cpu(rsp->OutputBufferLength),
1584 &rsp->hdr, min_len, data);
1586 qinf_exit:
1587 free_rsp_buf(resp_buftype, rsp);
1588 return rc;
1592 SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1593 u64 persistent_fid, u64 volatile_fid,
1594 struct smb2_file_all_info *data)
1596 return query_info(xid, tcon, persistent_fid, volatile_fid,
1597 FILE_ALL_INFORMATION,
1598 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
1599 sizeof(struct smb2_file_all_info), data);
1603 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1604 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1606 return query_info(xid, tcon, persistent_fid, volatile_fid,
1607 FILE_INTERNAL_INFORMATION,
1608 sizeof(struct smb2_file_internal_info),
1609 sizeof(struct smb2_file_internal_info), uniqueid);
1613 * This is a no-op for now. We're not really interested in the reply, but
1614 * rather in the fact that the server sent one and that server->lstrp
1615 * gets updated.
1617 * FIXME: maybe we should consider checking that the reply matches request?
1619 static void
1620 smb2_echo_callback(struct mid_q_entry *mid)
1622 struct TCP_Server_Info *server = mid->callback_data;
1623 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1624 unsigned int credits_received = 1;
1626 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1627 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1629 DeleteMidQEntry(mid);
1630 add_credits(server, credits_received, CIFS_ECHO_OP);
1634 SMB2_echo(struct TCP_Server_Info *server)
1636 struct smb2_echo_req *req;
1637 int rc = 0;
1638 struct kvec iov;
1639 struct smb_rqst rqst = { .rq_iov = &iov,
1640 .rq_nvec = 1 };
1642 cifs_dbg(FYI, "In echo request\n");
1644 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
1645 if (rc)
1646 return rc;
1648 req->hdr.CreditRequest = cpu_to_le16(1);
1650 iov.iov_base = (char *)req;
1651 /* 4 for rfc1002 length field */
1652 iov.iov_len = get_rfc1002_length(req) + 4;
1654 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
1655 CIFS_ECHO_OP);
1656 if (rc)
1657 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
1659 cifs_small_buf_release(req);
1660 return rc;
1664 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1665 u64 volatile_fid)
1667 struct smb2_flush_req *req;
1668 struct TCP_Server_Info *server;
1669 struct cifs_ses *ses = tcon->ses;
1670 struct kvec iov[1];
1671 int resp_buftype;
1672 int rc = 0;
1674 cifs_dbg(FYI, "Flush\n");
1676 if (ses && (ses->server))
1677 server = ses->server;
1678 else
1679 return -EIO;
1681 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
1682 if (rc)
1683 return rc;
1685 req->PersistentFileId = persistent_fid;
1686 req->VolatileFileId = volatile_fid;
1688 iov[0].iov_base = (char *)req;
1689 /* 4 for rfc1002 length field */
1690 iov[0].iov_len = get_rfc1002_length(req) + 4;
1692 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1694 if (rc != 0)
1695 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
1697 free_rsp_buf(resp_buftype, iov[0].iov_base);
1698 return rc;
1702 * To form a chain of read requests, any read requests after the first should
1703 * have the end_of_chain boolean set to true.
1705 static int
1706 smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
1707 unsigned int remaining_bytes, int request_type)
1709 int rc = -EACCES;
1710 struct smb2_read_req *req = NULL;
1712 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
1713 if (rc)
1714 return rc;
1715 if (io_parms->tcon->ses->server == NULL)
1716 return -ECONNABORTED;
1718 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
1720 req->PersistentFileId = io_parms->persistent_fid;
1721 req->VolatileFileId = io_parms->volatile_fid;
1722 req->ReadChannelInfoOffset = 0; /* reserved */
1723 req->ReadChannelInfoLength = 0; /* reserved */
1724 req->Channel = 0; /* reserved */
1725 req->MinimumCount = 0;
1726 req->Length = cpu_to_le32(io_parms->length);
1727 req->Offset = cpu_to_le64(io_parms->offset);
1729 if (request_type & CHAINED_REQUEST) {
1730 if (!(request_type & END_OF_CHAIN)) {
1731 /* 4 for rfc1002 length field */
1732 req->hdr.NextCommand =
1733 cpu_to_le32(get_rfc1002_length(req) + 4);
1734 } else /* END_OF_CHAIN */
1735 req->hdr.NextCommand = 0;
1736 if (request_type & RELATED_REQUEST) {
1737 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
1739 * Related requests use info from previous read request
1740 * in chain.
1742 req->hdr.SessionId = 0xFFFFFFFF;
1743 req->hdr.TreeId = 0xFFFFFFFF;
1744 req->PersistentFileId = 0xFFFFFFFF;
1745 req->VolatileFileId = 0xFFFFFFFF;
1748 if (remaining_bytes > io_parms->length)
1749 req->RemainingBytes = cpu_to_le32(remaining_bytes);
1750 else
1751 req->RemainingBytes = 0;
1753 iov[0].iov_base = (char *)req;
1754 /* 4 for rfc1002 length field */
1755 iov[0].iov_len = get_rfc1002_length(req) + 4;
1756 return rc;
1759 static void
1760 smb2_readv_callback(struct mid_q_entry *mid)
1762 struct cifs_readdata *rdata = mid->callback_data;
1763 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1764 struct TCP_Server_Info *server = tcon->ses->server;
1765 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
1766 unsigned int credits_received = 1;
1767 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
1768 .rq_nvec = 1,
1769 .rq_pages = rdata->pages,
1770 .rq_npages = rdata->nr_pages,
1771 .rq_pagesz = rdata->pagesz,
1772 .rq_tailsz = rdata->tailsz };
1774 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1775 __func__, mid->mid, mid->mid_state, rdata->result,
1776 rdata->bytes);
1778 switch (mid->mid_state) {
1779 case MID_RESPONSE_RECEIVED:
1780 credits_received = le16_to_cpu(buf->CreditRequest);
1781 /* result already set, check signature */
1782 if (server->sign) {
1783 int rc;
1785 rc = smb2_verify_signature(&rqst, server);
1786 if (rc)
1787 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1788 rc);
1790 /* FIXME: should this be counted toward the initiating task? */
1791 task_io_account_read(rdata->got_bytes);
1792 cifs_stats_bytes_read(tcon, rdata->got_bytes);
1793 break;
1794 case MID_REQUEST_SUBMITTED:
1795 case MID_RETRY_NEEDED:
1796 rdata->result = -EAGAIN;
1797 if (server->sign && rdata->got_bytes)
1798 /* reset bytes number since we can not check a sign */
1799 rdata->got_bytes = 0;
1800 /* FIXME: should this be counted toward the initiating task? */
1801 task_io_account_read(rdata->got_bytes);
1802 cifs_stats_bytes_read(tcon, rdata->got_bytes);
1803 break;
1804 default:
1805 if (rdata->result != -ENODATA)
1806 rdata->result = -EIO;
1809 if (rdata->result)
1810 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
1812 queue_work(cifsiod_wq, &rdata->work);
1813 DeleteMidQEntry(mid);
1814 add_credits(server, credits_received, 0);
1817 /* smb2_async_readv - send an async write, and set up mid to handle result */
1819 smb2_async_readv(struct cifs_readdata *rdata)
1821 int rc, flags = 0;
1822 struct smb2_hdr *buf;
1823 struct cifs_io_parms io_parms;
1824 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
1825 .rq_nvec = 1 };
1826 struct TCP_Server_Info *server;
1828 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1829 __func__, rdata->offset, rdata->bytes);
1831 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
1832 io_parms.offset = rdata->offset;
1833 io_parms.length = rdata->bytes;
1834 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
1835 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
1836 io_parms.pid = rdata->pid;
1838 server = io_parms.tcon->ses->server;
1840 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
1841 if (rc) {
1842 if (rc == -EAGAIN && rdata->credits) {
1843 /* credits was reset by reconnect */
1844 rdata->credits = 0;
1845 /* reduce in_flight value since we won't send the req */
1846 spin_lock(&server->req_lock);
1847 server->in_flight--;
1848 spin_unlock(&server->req_lock);
1850 return rc;
1853 buf = (struct smb2_hdr *)rdata->iov.iov_base;
1854 /* 4 for rfc1002 length field */
1855 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
1857 if (rdata->credits) {
1858 buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
1859 SMB2_MAX_BUFFER_SIZE));
1860 spin_lock(&server->req_lock);
1861 server->credits += rdata->credits -
1862 le16_to_cpu(buf->CreditCharge);
1863 spin_unlock(&server->req_lock);
1864 wake_up(&server->request_q);
1865 flags = CIFS_HAS_CREDITS;
1868 kref_get(&rdata->refcount);
1869 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
1870 cifs_readv_receive, smb2_readv_callback,
1871 rdata, flags);
1872 if (rc) {
1873 kref_put(&rdata->refcount, cifs_readdata_release);
1874 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
1877 cifs_small_buf_release(buf);
1878 return rc;
1882 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
1883 unsigned int *nbytes, char **buf, int *buf_type)
1885 int resp_buftype, rc = -EACCES;
1886 struct smb2_read_rsp *rsp = NULL;
1887 struct kvec iov[1];
1889 *nbytes = 0;
1890 rc = smb2_new_read_req(iov, io_parms, 0, 0);
1891 if (rc)
1892 return rc;
1894 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
1895 &resp_buftype, CIFS_LOG_ERROR);
1897 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
1899 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
1900 free_rsp_buf(resp_buftype, iov[0].iov_base);
1901 return 0;
1904 if (rc) {
1905 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
1906 cifs_dbg(VFS, "Send error in read = %d\n", rc);
1907 } else {
1908 *nbytes = le32_to_cpu(rsp->DataLength);
1909 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
1910 (*nbytes > io_parms->length)) {
1911 cifs_dbg(FYI, "bad length %d for count %d\n",
1912 *nbytes, io_parms->length);
1913 rc = -EIO;
1914 *nbytes = 0;
1918 if (*buf) {
1919 memcpy(*buf, (char *)rsp->hdr.ProtocolId + rsp->DataOffset,
1920 *nbytes);
1921 free_rsp_buf(resp_buftype, iov[0].iov_base);
1922 } else if (resp_buftype != CIFS_NO_BUFFER) {
1923 *buf = iov[0].iov_base;
1924 if (resp_buftype == CIFS_SMALL_BUFFER)
1925 *buf_type = CIFS_SMALL_BUFFER;
1926 else if (resp_buftype == CIFS_LARGE_BUFFER)
1927 *buf_type = CIFS_LARGE_BUFFER;
1929 return rc;
1933 * Check the mid_state and signature on received buffer (if any), and queue the
1934 * workqueue completion task.
1936 static void
1937 smb2_writev_callback(struct mid_q_entry *mid)
1939 struct cifs_writedata *wdata = mid->callback_data;
1940 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
1941 unsigned int written;
1942 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
1943 unsigned int credits_received = 1;
1945 switch (mid->mid_state) {
1946 case MID_RESPONSE_RECEIVED:
1947 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
1948 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
1949 if (wdata->result != 0)
1950 break;
1952 written = le32_to_cpu(rsp->DataLength);
1954 * Mask off high 16 bits when bytes written as returned
1955 * by the server is greater than bytes requested by the
1956 * client. OS/2 servers are known to set incorrect
1957 * CountHigh values.
1959 if (written > wdata->bytes)
1960 written &= 0xFFFF;
1962 if (written < wdata->bytes)
1963 wdata->result = -ENOSPC;
1964 else
1965 wdata->bytes = written;
1966 break;
1967 case MID_REQUEST_SUBMITTED:
1968 case MID_RETRY_NEEDED:
1969 wdata->result = -EAGAIN;
1970 break;
1971 default:
1972 wdata->result = -EIO;
1973 break;
1976 if (wdata->result)
1977 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
1979 queue_work(cifsiod_wq, &wdata->work);
1980 DeleteMidQEntry(mid);
1981 add_credits(tcon->ses->server, credits_received, 0);
1984 /* smb2_async_writev - send an async write, and set up mid to handle result */
1986 smb2_async_writev(struct cifs_writedata *wdata,
1987 void (*release)(struct kref *kref))
1989 int rc = -EACCES, flags = 0;
1990 struct smb2_write_req *req = NULL;
1991 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
1992 struct TCP_Server_Info *server = tcon->ses->server;
1993 struct kvec iov;
1994 struct smb_rqst rqst;
1996 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
1997 if (rc) {
1998 if (rc == -EAGAIN && wdata->credits) {
1999 /* credits was reset by reconnect */
2000 wdata->credits = 0;
2001 /* reduce in_flight value since we won't send the req */
2002 spin_lock(&server->req_lock);
2003 server->in_flight--;
2004 spin_unlock(&server->req_lock);
2006 goto async_writev_out;
2009 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
2011 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2012 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2013 req->WriteChannelInfoOffset = 0;
2014 req->WriteChannelInfoLength = 0;
2015 req->Channel = 0;
2016 req->Offset = cpu_to_le64(wdata->offset);
2017 /* 4 for rfc1002 length field */
2018 req->DataOffset = cpu_to_le16(
2019 offsetof(struct smb2_write_req, Buffer) - 4);
2020 req->RemainingBytes = 0;
2022 /* 4 for rfc1002 length field and 1 for Buffer */
2023 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
2024 iov.iov_base = req;
2026 rqst.rq_iov = &iov;
2027 rqst.rq_nvec = 1;
2028 rqst.rq_pages = wdata->pages;
2029 rqst.rq_npages = wdata->nr_pages;
2030 rqst.rq_pagesz = wdata->pagesz;
2031 rqst.rq_tailsz = wdata->tailsz;
2033 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2034 wdata->offset, wdata->bytes);
2036 req->Length = cpu_to_le32(wdata->bytes);
2038 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2040 if (wdata->credits) {
2041 req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2042 SMB2_MAX_BUFFER_SIZE));
2043 spin_lock(&server->req_lock);
2044 server->credits += wdata->credits -
2045 le16_to_cpu(req->hdr.CreditCharge);
2046 spin_unlock(&server->req_lock);
2047 wake_up(&server->request_q);
2048 flags = CIFS_HAS_CREDITS;
2051 kref_get(&wdata->refcount);
2052 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2053 flags);
2055 if (rc) {
2056 kref_put(&wdata->refcount, release);
2057 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2060 async_writev_out:
2061 cifs_small_buf_release(req);
2062 return rc;
2066 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2067 * The length field from io_parms must be at least 1 and indicates a number of
2068 * elements with data to write that begins with position 1 in iov array. All
2069 * data length is specified by count.
2072 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2073 unsigned int *nbytes, struct kvec *iov, int n_vec)
2075 int rc = 0;
2076 struct smb2_write_req *req = NULL;
2077 struct smb2_write_rsp *rsp = NULL;
2078 int resp_buftype;
2079 *nbytes = 0;
2081 if (n_vec < 1)
2082 return rc;
2084 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2085 if (rc)
2086 return rc;
2088 if (io_parms->tcon->ses->server == NULL)
2089 return -ECONNABORTED;
2091 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2093 req->PersistentFileId = io_parms->persistent_fid;
2094 req->VolatileFileId = io_parms->volatile_fid;
2095 req->WriteChannelInfoOffset = 0;
2096 req->WriteChannelInfoLength = 0;
2097 req->Channel = 0;
2098 req->Length = cpu_to_le32(io_parms->length);
2099 req->Offset = cpu_to_le64(io_parms->offset);
2100 /* 4 for rfc1002 length field */
2101 req->DataOffset = cpu_to_le16(
2102 offsetof(struct smb2_write_req, Buffer) - 4);
2103 req->RemainingBytes = 0;
2105 iov[0].iov_base = (char *)req;
2106 /* 4 for rfc1002 length field and 1 for Buffer */
2107 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2109 /* length of entire message including data to be written */
2110 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2112 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2113 &resp_buftype, 0);
2114 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
2116 if (rc) {
2117 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
2118 cifs_dbg(VFS, "Send error in write = %d\n", rc);
2119 } else
2120 *nbytes = le32_to_cpu(rsp->DataLength);
2122 free_rsp_buf(resp_buftype, rsp);
2123 return rc;
2126 static unsigned int
2127 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2129 int len;
2130 unsigned int entrycount = 0;
2131 unsigned int next_offset = 0;
2132 FILE_DIRECTORY_INFO *entryptr;
2134 if (bufstart == NULL)
2135 return 0;
2137 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2139 while (1) {
2140 entryptr = (FILE_DIRECTORY_INFO *)
2141 ((char *)entryptr + next_offset);
2143 if ((char *)entryptr + size > end_of_buf) {
2144 cifs_dbg(VFS, "malformed search entry would overflow\n");
2145 break;
2148 len = le32_to_cpu(entryptr->FileNameLength);
2149 if ((char *)entryptr + len + size > end_of_buf) {
2150 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2151 end_of_buf);
2152 break;
2155 *lastentry = (char *)entryptr;
2156 entrycount++;
2158 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2159 if (!next_offset)
2160 break;
2163 return entrycount;
2167 * Readdir/FindFirst
2170 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2171 u64 persistent_fid, u64 volatile_fid, int index,
2172 struct cifs_search_info *srch_inf)
2174 struct smb2_query_directory_req *req;
2175 struct smb2_query_directory_rsp *rsp = NULL;
2176 struct kvec iov[2];
2177 int rc = 0;
2178 int len;
2179 int resp_buftype = CIFS_NO_BUFFER;
2180 unsigned char *bufptr;
2181 struct TCP_Server_Info *server;
2182 struct cifs_ses *ses = tcon->ses;
2183 __le16 asteriks = cpu_to_le16('*');
2184 char *end_of_smb;
2185 unsigned int output_size = CIFSMaxBufSize;
2186 size_t info_buf_size;
2188 if (ses && (ses->server))
2189 server = ses->server;
2190 else
2191 return -EIO;
2193 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2194 if (rc)
2195 return rc;
2197 switch (srch_inf->info_level) {
2198 case SMB_FIND_FILE_DIRECTORY_INFO:
2199 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2200 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2201 break;
2202 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2203 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2204 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2205 break;
2206 default:
2207 cifs_dbg(VFS, "info level %u isn't supported\n",
2208 srch_inf->info_level);
2209 rc = -EINVAL;
2210 goto qdir_exit;
2213 req->FileIndex = cpu_to_le32(index);
2214 req->PersistentFileId = persistent_fid;
2215 req->VolatileFileId = volatile_fid;
2217 len = 0x2;
2218 bufptr = req->Buffer;
2219 memcpy(bufptr, &asteriks, len);
2221 req->FileNameOffset =
2222 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2223 req->FileNameLength = cpu_to_le16(len);
2225 * BB could be 30 bytes or so longer if we used SMB2 specific
2226 * buffer lengths, but this is safe and close enough.
2228 output_size = min_t(unsigned int, output_size, server->maxBuf);
2229 output_size = min_t(unsigned int, output_size, 2 << 15);
2230 req->OutputBufferLength = cpu_to_le32(output_size);
2232 iov[0].iov_base = (char *)req;
2233 /* 4 for RFC1001 length and 1 for Buffer */
2234 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2236 iov[1].iov_base = (char *)(req->Buffer);
2237 iov[1].iov_len = len;
2239 inc_rfc1001_len(req, len - 1 /* Buffer */);
2241 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
2242 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2244 if (rc) {
2245 if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2246 srch_inf->endOfSearch = true;
2247 rc = 0;
2249 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2250 goto qdir_exit;
2253 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2254 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2255 info_buf_size);
2256 if (rc)
2257 goto qdir_exit;
2259 srch_inf->unicode = true;
2261 if (srch_inf->ntwrk_buf_start) {
2262 if (srch_inf->smallBuf)
2263 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2264 else
2265 cifs_buf_release(srch_inf->ntwrk_buf_start);
2267 srch_inf->ntwrk_buf_start = (char *)rsp;
2268 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2269 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2270 /* 4 for rfc1002 length field */
2271 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2272 srch_inf->entries_in_buffer =
2273 num_entries(srch_inf->srch_entries_start, end_of_smb,
2274 &srch_inf->last_entry, info_buf_size);
2275 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
2276 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2277 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2278 srch_inf->srch_entries_start, srch_inf->last_entry);
2279 if (resp_buftype == CIFS_LARGE_BUFFER)
2280 srch_inf->smallBuf = false;
2281 else if (resp_buftype == CIFS_SMALL_BUFFER)
2282 srch_inf->smallBuf = true;
2283 else
2284 cifs_dbg(VFS, "illegal search buffer type\n");
2286 return rc;
2288 qdir_exit:
2289 free_rsp_buf(resp_buftype, rsp);
2290 return rc;
2293 static int
2294 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2295 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
2296 unsigned int num, void **data, unsigned int *size)
2298 struct smb2_set_info_req *req;
2299 struct smb2_set_info_rsp *rsp = NULL;
2300 struct kvec *iov;
2301 int rc = 0;
2302 int resp_buftype;
2303 unsigned int i;
2304 struct TCP_Server_Info *server;
2305 struct cifs_ses *ses = tcon->ses;
2307 if (ses && (ses->server))
2308 server = ses->server;
2309 else
2310 return -EIO;
2312 if (!num)
2313 return -EINVAL;
2315 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2316 if (!iov)
2317 return -ENOMEM;
2319 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2320 if (rc) {
2321 kfree(iov);
2322 return rc;
2325 req->hdr.ProcessId = cpu_to_le32(pid);
2327 req->InfoType = SMB2_O_INFO_FILE;
2328 req->FileInfoClass = info_class;
2329 req->PersistentFileId = persistent_fid;
2330 req->VolatileFileId = volatile_fid;
2332 /* 4 for RFC1001 length and 1 for Buffer */
2333 req->BufferOffset =
2334 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2335 req->BufferLength = cpu_to_le32(*size);
2337 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2339 memcpy(req->Buffer, *data, *size);
2341 iov[0].iov_base = (char *)req;
2342 /* 4 for RFC1001 length */
2343 iov[0].iov_len = get_rfc1002_length(req) + 4;
2345 for (i = 1; i < num; i++) {
2346 inc_rfc1001_len(req, size[i]);
2347 le32_add_cpu(&req->BufferLength, size[i]);
2348 iov[i].iov_base = (char *)data[i];
2349 iov[i].iov_len = size[i];
2352 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2353 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2355 if (rc != 0)
2356 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
2358 free_rsp_buf(resp_buftype, rsp);
2359 kfree(iov);
2360 return rc;
2364 SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2365 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2367 struct smb2_file_rename_info info;
2368 void **data;
2369 unsigned int size[2];
2370 int rc;
2371 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2373 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2374 if (!data)
2375 return -ENOMEM;
2377 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2378 /* 0 = fail if target already exists */
2379 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2380 info.FileNameLength = cpu_to_le32(len);
2382 data[0] = &info;
2383 size[0] = sizeof(struct smb2_file_rename_info);
2385 data[1] = target_file;
2386 size[1] = len + 2 /* null */;
2388 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2389 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2390 size);
2391 kfree(data);
2392 return rc;
2396 SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2397 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2399 struct smb2_file_link_info info;
2400 void **data;
2401 unsigned int size[2];
2402 int rc;
2403 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2405 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2406 if (!data)
2407 return -ENOMEM;
2409 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2410 /* 0 = fail if link already exists */
2411 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2412 info.FileNameLength = cpu_to_le32(len);
2414 data[0] = &info;
2415 size[0] = sizeof(struct smb2_file_link_info);
2417 data[1] = target_file;
2418 size[1] = len + 2 /* null */;
2420 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2421 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
2422 kfree(data);
2423 return rc;
2427 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2428 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
2430 struct smb2_file_eof_info info;
2431 void *data;
2432 unsigned int size;
2434 info.EndOfFile = *eof;
2436 data = &info;
2437 size = sizeof(struct smb2_file_eof_info);
2439 if (is_falloc)
2440 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2441 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2442 else
2443 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2444 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
2448 SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2449 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2451 unsigned int size;
2452 size = sizeof(FILE_BASIC_INFO);
2453 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2454 current->tgid, FILE_BASIC_INFORMATION, 1,
2455 (void **)&buf, &size);
2459 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2460 const u64 persistent_fid, const u64 volatile_fid,
2461 __u8 oplock_level)
2463 int rc;
2464 struct smb2_oplock_break *req = NULL;
2466 cifs_dbg(FYI, "SMB2_oplock_break\n");
2467 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2469 if (rc)
2470 return rc;
2472 req->VolatileFid = volatile_fid;
2473 req->PersistentFid = persistent_fid;
2474 req->OplockLevel = oplock_level;
2475 req->hdr.CreditRequest = cpu_to_le16(1);
2477 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2478 /* SMB2 buffer freed by function above */
2480 if (rc) {
2481 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
2482 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
2485 return rc;
2488 static void
2489 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2490 struct kstatfs *kst)
2492 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2493 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2494 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
2495 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
2496 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
2497 return;
2500 static int
2501 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2502 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2504 int rc;
2505 struct smb2_query_info_req *req;
2507 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
2509 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2510 return -EIO;
2512 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2513 if (rc)
2514 return rc;
2516 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2517 req->FileInfoClass = level;
2518 req->PersistentFileId = persistent_fid;
2519 req->VolatileFileId = volatile_fid;
2520 /* 4 for rfc1002 length field and 1 for pad */
2521 req->InputBufferOffset =
2522 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2523 req->OutputBufferLength = cpu_to_le32(
2524 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2526 iov->iov_base = (char *)req;
2527 /* 4 for rfc1002 length field */
2528 iov->iov_len = get_rfc1002_length(req) + 4;
2529 return 0;
2533 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2534 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2536 struct smb2_query_info_rsp *rsp = NULL;
2537 struct kvec iov;
2538 int rc = 0;
2539 int resp_buftype;
2540 struct cifs_ses *ses = tcon->ses;
2541 struct smb2_fs_full_size_info *info = NULL;
2543 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
2544 sizeof(struct smb2_fs_full_size_info),
2545 persistent_fid, volatile_fid);
2546 if (rc)
2547 return rc;
2549 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2550 if (rc) {
2551 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2552 goto qfsinf_exit;
2554 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2556 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
2557 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
2558 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2559 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2560 sizeof(struct smb2_fs_full_size_info));
2561 if (!rc)
2562 copy_fs_info_to_kstatfs(info, fsdata);
2564 qfsinf_exit:
2565 free_rsp_buf(resp_buftype, iov.iov_base);
2566 return rc;
2570 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
2571 u64 persistent_fid, u64 volatile_fid, int level)
2573 struct smb2_query_info_rsp *rsp = NULL;
2574 struct kvec iov;
2575 int rc = 0;
2576 int resp_buftype, max_len, min_len;
2577 struct cifs_ses *ses = tcon->ses;
2578 unsigned int rsp_len, offset;
2580 if (level == FS_DEVICE_INFORMATION) {
2581 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
2582 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
2583 } else if (level == FS_ATTRIBUTE_INFORMATION) {
2584 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
2585 min_len = MIN_FS_ATTR_INFO_SIZE;
2586 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
2587 max_len = sizeof(struct smb3_fs_ss_info);
2588 min_len = sizeof(struct smb3_fs_ss_info);
2589 } else {
2590 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
2591 return -EINVAL;
2594 rc = build_qfs_info_req(&iov, tcon, level, max_len,
2595 persistent_fid, volatile_fid);
2596 if (rc)
2597 return rc;
2599 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2600 if (rc) {
2601 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2602 goto qfsattr_exit;
2604 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2606 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
2607 offset = le16_to_cpu(rsp->OutputBufferOffset);
2608 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
2609 if (rc)
2610 goto qfsattr_exit;
2612 if (level == FS_ATTRIBUTE_INFORMATION)
2613 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
2614 + (char *)&rsp->hdr, min_t(unsigned int,
2615 rsp_len, max_len));
2616 else if (level == FS_DEVICE_INFORMATION)
2617 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
2618 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
2619 else if (level == FS_SECTOR_SIZE_INFORMATION) {
2620 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
2621 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
2622 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
2623 tcon->perf_sector_size =
2624 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
2627 qfsattr_exit:
2628 free_rsp_buf(resp_buftype, iov.iov_base);
2629 return rc;
2633 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2634 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2635 const __u32 num_lock, struct smb2_lock_element *buf)
2637 int rc = 0;
2638 struct smb2_lock_req *req = NULL;
2639 struct kvec iov[2];
2640 int resp_buf_type;
2641 unsigned int count;
2643 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
2645 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
2646 if (rc)
2647 return rc;
2649 req->hdr.ProcessId = cpu_to_le32(pid);
2650 req->LockCount = cpu_to_le16(num_lock);
2652 req->PersistentFileId = persist_fid;
2653 req->VolatileFileId = volatile_fid;
2655 count = num_lock * sizeof(struct smb2_lock_element);
2656 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
2658 iov[0].iov_base = (char *)req;
2659 /* 4 for rfc1002 length field and count for all locks */
2660 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
2661 iov[1].iov_base = (char *)buf;
2662 iov[1].iov_len = count;
2664 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2665 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2666 if (rc) {
2667 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
2668 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
2671 return rc;
2675 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
2676 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2677 const __u64 length, const __u64 offset, const __u32 lock_flags,
2678 const bool wait)
2680 struct smb2_lock_element lock;
2682 lock.Offset = cpu_to_le64(offset);
2683 lock.Length = cpu_to_le64(length);
2684 lock.Flags = cpu_to_le32(lock_flags);
2685 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
2686 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
2688 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
2692 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
2693 __u8 *lease_key, const __le32 lease_state)
2695 int rc;
2696 struct smb2_lease_ack *req = NULL;
2698 cifs_dbg(FYI, "SMB2_lease_break\n");
2699 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2701 if (rc)
2702 return rc;
2704 req->hdr.CreditRequest = cpu_to_le16(1);
2705 req->StructureSize = cpu_to_le16(36);
2706 inc_rfc1001_len(req, 12);
2708 memcpy(req->LeaseKey, lease_key, 16);
2709 req->LeaseState = lease_state;
2711 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2712 /* SMB2 buffer freed by function above */
2714 if (rc) {
2715 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
2716 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
2719 return rc;