cifs: dynamic allocation of ntlmssp blob
[firefly-linux-kernel-4.4.55.git] / fs / cifs / smb2pdu.c
1 /*
2  *   fs/cifs/smb2pdu.c
3  *
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
8  *
9  *   Contains the routines for constructing the SMB2 PDUs themselves
10  *
11  *   This library is free software; you can redistribute it and/or modify
12  *   it under the terms of the GNU Lesser General Public License as published
13  *   by the Free Software Foundation; either version 2.1 of the License, or
14  *   (at your option) any later version.
15  *
16  *   This library is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
19  *   the GNU Lesser General Public License for more details.
20  *
21  *   You should have received a copy of the GNU Lesser General Public License
22  *   along with this library; if not, write to the Free Software
23  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24  */
25
26  /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27  /* Note that there are handle based routines which must be                   */
28  /* treated slightly differently for reconnection purposes since we never     */
29  /* want to reuse a stale file handle and only the caller knows the file info */
30
31 #include <linux/fs.h>
32 #include <linux/kernel.h>
33 #include <linux/vfs.h>
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"
49 #include "cifs_spnego.h"
50
51 /*
52  *  The following table defines the expected "StructureSize" of SMB2 requests
53  *  in order by SMB2 command.  This is similar to "wct" in SMB/CIFS requests.
54  *
55  *  Note that commands are defined in smb2pdu.h in le16 but the array below is
56  *  indexed by command in host byte order.
57  */
58 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
59         /* SMB2_NEGOTIATE */ 36,
60         /* SMB2_SESSION_SETUP */ 25,
61         /* SMB2_LOGOFF */ 4,
62         /* SMB2_TREE_CONNECT */ 9,
63         /* SMB2_TREE_DISCONNECT */ 4,
64         /* SMB2_CREATE */ 57,
65         /* SMB2_CLOSE */ 24,
66         /* SMB2_FLUSH */ 24,
67         /* SMB2_READ */ 49,
68         /* SMB2_WRITE */ 49,
69         /* SMB2_LOCK */ 48,
70         /* SMB2_IOCTL */ 57,
71         /* SMB2_CANCEL */ 4,
72         /* SMB2_ECHO */ 4,
73         /* SMB2_QUERY_DIRECTORY */ 33,
74         /* SMB2_CHANGE_NOTIFY */ 32,
75         /* SMB2_QUERY_INFO */ 41,
76         /* SMB2_SET_INFO */ 33,
77         /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
78 };
79
80
81 static void
82 smb2_hdr_assemble(struct smb2_hdr *hdr, __le16 smb2_cmd /* command */ ,
83                   const struct cifs_tcon *tcon)
84 {
85         struct smb2_pdu *pdu = (struct smb2_pdu *)hdr;
86         char *temp = (char *)hdr;
87         /* lookup word count ie StructureSize from table */
88         __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_cmd)];
89
90         /*
91          * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
92          * largest operations (Create)
93          */
94         memset(temp, 0, 256);
95
96         /* Note this is only network field converted to big endian */
97         hdr->smb2_buf_length = cpu_to_be32(parmsize + sizeof(struct smb2_hdr)
98                         - 4 /*  RFC 1001 length field itself not counted */);
99
100         hdr->ProtocolId[0] = 0xFE;
101         hdr->ProtocolId[1] = 'S';
102         hdr->ProtocolId[2] = 'M';
103         hdr->ProtocolId[3] = 'B';
104         hdr->StructureSize = cpu_to_le16(64);
105         hdr->Command = smb2_cmd;
106         hdr->CreditRequest = cpu_to_le16(2); /* BB make this dynamic */
107         hdr->ProcessId = cpu_to_le32((__u16)current->tgid);
108
109         if (!tcon)
110                 goto out;
111
112         /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
113         /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
114         if ((tcon->ses) && (tcon->ses->server) &&
115             (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
116                 hdr->CreditCharge = cpu_to_le16(1);
117         /* else CreditCharge MBZ */
118
119         hdr->TreeId = tcon->tid;
120         /* Uid is not converted */
121         if (tcon->ses)
122                 hdr->SessionId = tcon->ses->Suid;
123
124         /*
125          * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
126          * to pass the path on the Open SMB prefixed by \\server\share.
127          * Not sure when we would need to do the augmented path (if ever) and
128          * setting this flag breaks the SMB2 open operation since it is
129          * illegal to send an empty path name (without \\server\share prefix)
130          * when the DFS flag is set in the SMB open header. We could
131          * consider setting the flag on all operations other than open
132          * but it is safer to net set it for now.
133          */
134 /*      if (tcon->share_flags & SHI1005_FLAGS_DFS)
135                 hdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
136
137         if (tcon->ses && tcon->ses->server && tcon->ses->server->sign)
138                 hdr->Flags |= SMB2_FLAGS_SIGNED;
139 out:
140         pdu->StructureSize2 = cpu_to_le16(parmsize);
141         return;
142 }
143
144 static int
145 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
146 {
147         int rc = 0;
148         struct nls_table *nls_codepage;
149         struct cifs_ses *ses;
150         struct TCP_Server_Info *server;
151
152         /*
153          * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
154          * check for tcp and smb session status done differently
155          * for those three - in the calling routine.
156          */
157         if (tcon == NULL)
158                 return rc;
159
160         if (smb2_command == SMB2_TREE_CONNECT)
161                 return rc;
162
163         if (tcon->tidStatus == CifsExiting) {
164                 /*
165                  * only tree disconnect, open, and write,
166                  * (and ulogoff which does not have tcon)
167                  * are allowed as we start force umount.
168                  */
169                 if ((smb2_command != SMB2_WRITE) &&
170                    (smb2_command != SMB2_CREATE) &&
171                    (smb2_command != SMB2_TREE_DISCONNECT)) {
172                         cifs_dbg(FYI, "can not send cmd %d while umounting\n",
173                                  smb2_command);
174                         return -ENODEV;
175                 }
176         }
177         if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
178             (!tcon->ses->server))
179                 return -EIO;
180
181         ses = tcon->ses;
182         server = ses->server;
183
184         /*
185          * Give demultiplex thread up to 10 seconds to reconnect, should be
186          * greater than cifs socket timeout which is 7 seconds
187          */
188         while (server->tcpStatus == CifsNeedReconnect) {
189                 /*
190                  * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
191                  * here since they are implicitly done when session drops.
192                  */
193                 switch (smb2_command) {
194                 /*
195                  * BB Should we keep oplock break and add flush to exceptions?
196                  */
197                 case SMB2_TREE_DISCONNECT:
198                 case SMB2_CANCEL:
199                 case SMB2_CLOSE:
200                 case SMB2_OPLOCK_BREAK:
201                         return -EAGAIN;
202                 }
203
204                 wait_event_interruptible_timeout(server->response_q,
205                         (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
206
207                 /* are we still trying to reconnect? */
208                 if (server->tcpStatus != CifsNeedReconnect)
209                         break;
210
211                 /*
212                  * on "soft" mounts we wait once. Hard mounts keep
213                  * retrying until process is killed or server comes
214                  * back on-line
215                  */
216                 if (!tcon->retry) {
217                         cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
218                         return -EHOSTDOWN;
219                 }
220         }
221
222         if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
223                 return rc;
224
225         nls_codepage = load_nls_default();
226
227         /*
228          * need to prevent multiple threads trying to simultaneously reconnect
229          * the same SMB session
230          */
231         mutex_lock(&tcon->ses->session_mutex);
232         rc = cifs_negotiate_protocol(0, tcon->ses);
233         if (!rc && tcon->ses->need_reconnect)
234                 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
235
236         if (rc || !tcon->need_reconnect) {
237                 mutex_unlock(&tcon->ses->session_mutex);
238                 goto out;
239         }
240
241         cifs_mark_open_files_invalid(tcon);
242         rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
243         mutex_unlock(&tcon->ses->session_mutex);
244         cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
245         if (rc)
246                 goto out;
247         atomic_inc(&tconInfoReconnectCount);
248 out:
249         /*
250          * Check if handle based operation so we know whether we can continue
251          * or not without returning to caller to reset file handle.
252          */
253         /*
254          * BB Is flush done by server on drop of tcp session? Should we special
255          * case it and skip above?
256          */
257         switch (smb2_command) {
258         case SMB2_FLUSH:
259         case SMB2_READ:
260         case SMB2_WRITE:
261         case SMB2_LOCK:
262         case SMB2_IOCTL:
263         case SMB2_QUERY_DIRECTORY:
264         case SMB2_CHANGE_NOTIFY:
265         case SMB2_QUERY_INFO:
266         case SMB2_SET_INFO:
267                 return -EAGAIN;
268         }
269         unload_nls(nls_codepage);
270         return rc;
271 }
272
273 /*
274  * Allocate and return pointer to an SMB request hdr, and set basic
275  * SMB information in the SMB header. If the return code is zero, this
276  * function must have filled in request_buf pointer.
277  */
278 static int
279 small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
280                 void **request_buf)
281 {
282         int rc = 0;
283
284         rc = smb2_reconnect(smb2_command, tcon);
285         if (rc)
286                 return rc;
287
288         /* BB eventually switch this to SMB2 specific small buf size */
289         *request_buf = cifs_small_buf_get();
290         if (*request_buf == NULL) {
291                 /* BB should we add a retry in here if not a writepage? */
292                 return -ENOMEM;
293         }
294
295         smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
296
297         if (tcon != NULL) {
298 #ifdef CONFIG_CIFS_STATS2
299                 uint16_t com_code = le16_to_cpu(smb2_command);
300                 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
301 #endif
302                 cifs_stats_inc(&tcon->num_smbs_sent);
303         }
304
305         return rc;
306 }
307
308 #ifdef CONFIG_CIFS_SMB311
309 /* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
310 #define OFFSET_OF_NEG_CONTEXT 0x68  /* sizeof(struct smb2_negotiate_req) - 4 */
311
312
313 #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES     cpu_to_le16(1)
314 #define SMB2_ENCRYPTION_CAPABILITIES            cpu_to_le16(2)
315
316 static void
317 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
318 {
319         pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
320         pneg_ctxt->DataLength = cpu_to_le16(38);
321         pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
322         pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
323         get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
324         pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
325 }
326
327 static void
328 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
329 {
330         pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
331         pneg_ctxt->DataLength = cpu_to_le16(6);
332         pneg_ctxt->CipherCount = cpu_to_le16(2);
333         pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
334         pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
335 }
336
337 static void
338 assemble_neg_contexts(struct smb2_negotiate_req *req)
339 {
340
341         /* +4 is to account for the RFC1001 len field */
342         char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
343
344         build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
345         /* Add 2 to size to round to 8 byte boundary */
346         pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
347         build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
348         req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
349         req->NegotiateContextCount = cpu_to_le16(2);
350         inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context) + 2
351                         + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
352 }
353 #else
354 static void assemble_neg_contexts(struct smb2_negotiate_req *req)
355 {
356         return;
357 }
358 #endif /* SMB311 */
359
360
361 /*
362  *
363  *      SMB2 Worker functions follow:
364  *
365  *      The general structure of the worker functions is:
366  *      1) Call smb2_init (assembles SMB2 header)
367  *      2) Initialize SMB2 command specific fields in fixed length area of SMB
368  *      3) Call smb_sendrcv2 (sends request on socket and waits for response)
369  *      4) Decode SMB2 command specific fields in the fixed length area
370  *      5) Decode variable length data area (if any for this SMB2 command type)
371  *      6) Call free smb buffer
372  *      7) return
373  *
374  */
375
376 int
377 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
378 {
379         struct smb2_negotiate_req *req;
380         struct smb2_negotiate_rsp *rsp;
381         struct kvec iov[1];
382         int rc = 0;
383         int resp_buftype;
384         struct TCP_Server_Info *server = ses->server;
385         int blob_offset, blob_length;
386         char *security_blob;
387         int flags = CIFS_NEG_OP;
388
389         cifs_dbg(FYI, "Negotiate protocol\n");
390
391         if (!server) {
392                 WARN(1, "%s: server is NULL!\n", __func__);
393                 return -EIO;
394         }
395
396         rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
397         if (rc)
398                 return rc;
399
400         req->hdr.SessionId = 0;
401
402         req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
403
404         req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
405         inc_rfc1001_len(req, 2);
406
407         /* only one of SMB2 signing flags may be set in SMB2 request */
408         if (ses->sign)
409                 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
410         else if (global_secflags & CIFSSEC_MAY_SIGN)
411                 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
412         else
413                 req->SecurityMode = 0;
414
415         req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
416
417         /* ClientGUID must be zero for SMB2.02 dialect */
418         if (ses->server->vals->protocol_id == SMB20_PROT_ID)
419                 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
420         else {
421                 memcpy(req->ClientGUID, server->client_guid,
422                         SMB2_CLIENT_GUID_SIZE);
423                 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
424                         assemble_neg_contexts(req);
425         }
426         iov[0].iov_base = (char *)req;
427         /* 4 for rfc1002 length field */
428         iov[0].iov_len = get_rfc1002_length(req) + 4;
429
430         rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
431
432         rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
433         /*
434          * No tcon so can't do
435          * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
436          */
437         if (rc != 0)
438                 goto neg_exit;
439
440         cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
441
442         /* BB we may eventually want to match the negotiated vs. requested
443            dialect, even though we are only requesting one at a time */
444         if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
445                 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
446         else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
447                 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
448         else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
449                 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
450         else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
451                 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
452 #ifdef CONFIG_CIFS_SMB311
453         else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
454                 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
455 #endif /* SMB311 */
456         else {
457                 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
458                          le16_to_cpu(rsp->DialectRevision));
459                 rc = -EIO;
460                 goto neg_exit;
461         }
462         server->dialect = le16_to_cpu(rsp->DialectRevision);
463
464         /* SMB2 only has an extended negflavor */
465         server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
466         /* set it to the maximum buffer size value we can send with 1 credit */
467         server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
468                                SMB2_MAX_BUFFER_SIZE);
469         server->max_read = le32_to_cpu(rsp->MaxReadSize);
470         server->max_write = le32_to_cpu(rsp->MaxWriteSize);
471         /* BB Do we need to validate the SecurityMode? */
472         server->sec_mode = le16_to_cpu(rsp->SecurityMode);
473         server->capabilities = le32_to_cpu(rsp->Capabilities);
474         /* Internal types */
475         server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
476
477         security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
478                                                &rsp->hdr);
479         /*
480          * See MS-SMB2 section 2.2.4: if no blob, client picks default which
481          * for us will be
482          *      ses->sectype = RawNTLMSSP;
483          * but for time being this is our only auth choice so doesn't matter.
484          * We just found a server which sets blob length to zero expecting raw.
485          */
486         if (blob_length == 0)
487                 cifs_dbg(FYI, "missing security blob on negprot\n");
488
489         rc = cifs_enable_signing(server, ses->sign);
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         }
499 neg_exit:
500         free_rsp_buf(resp_buftype, rsp);
501         return rc;
502 }
503
504 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
505 {
506         int rc = 0;
507         struct validate_negotiate_info_req vneg_inbuf;
508         struct validate_negotiate_info_rsp *pneg_rsp;
509         u32 rsplen;
510
511         cifs_dbg(FYI, "validate negotiate\n");
512
513         /*
514          * validation ioctl must be signed, so no point sending this if we
515          * can not sign it.  We could eventually change this to selectively
516          * sign just this, the first and only signed request on a connection.
517          * This is good enough for now since a user who wants better security
518          * would also enable signing on the mount. Having validation of
519          * negotiate info for signed connections helps reduce attack vectors
520          */
521         if (tcon->ses->server->sign == false)
522                 return 0; /* validation requires signing */
523
524         vneg_inbuf.Capabilities =
525                         cpu_to_le32(tcon->ses->server->vals->req_capabilities);
526         memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
527                                         SMB2_CLIENT_GUID_SIZE);
528
529         if (tcon->ses->sign)
530                 vneg_inbuf.SecurityMode =
531                         cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
532         else if (global_secflags & CIFSSEC_MAY_SIGN)
533                 vneg_inbuf.SecurityMode =
534                         cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
535         else
536                 vneg_inbuf.SecurityMode = 0;
537
538         vneg_inbuf.DialectCount = cpu_to_le16(1);
539         vneg_inbuf.Dialects[0] =
540                 cpu_to_le16(tcon->ses->server->vals->protocol_id);
541
542         rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
543                 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
544                 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
545                 (char **)&pneg_rsp, &rsplen);
546
547         if (rc != 0) {
548                 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
549                 return -EIO;
550         }
551
552         if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
553                 cifs_dbg(VFS, "invalid size of protocol negotiate response\n");
554                 return -EIO;
555         }
556
557         /* check validate negotiate info response matches what we got earlier */
558         if (pneg_rsp->Dialect !=
559                         cpu_to_le16(tcon->ses->server->vals->protocol_id))
560                 goto vneg_out;
561
562         if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
563                 goto vneg_out;
564
565         /* do not validate server guid because not saved at negprot time yet */
566
567         if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
568               SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
569                 goto vneg_out;
570
571         /* validate negotiate successful */
572         cifs_dbg(FYI, "validate negotiate info successful\n");
573         return 0;
574
575 vneg_out:
576         cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
577         return -EIO;
578 }
579
580 int
581 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
582                 const struct nls_table *nls_cp)
583 {
584         struct smb2_sess_setup_req *req;
585         struct smb2_sess_setup_rsp *rsp = NULL;
586         struct kvec iov[2];
587         int rc = 0;
588         int resp_buftype = CIFS_NO_BUFFER;
589         __le32 phase = NtLmNegotiate; /* NTLMSSP, if needed, is multistage */
590         struct TCP_Server_Info *server = ses->server;
591         u16 blob_length = 0;
592         struct key *spnego_key = NULL;
593         char *security_blob = NULL;
594         unsigned char *ntlmssp_blob = NULL;
595         bool use_spnego = false; /* else use raw ntlmssp */
596
597         cifs_dbg(FYI, "Session Setup\n");
598
599         if (!server) {
600                 WARN(1, "%s: server is NULL!\n", __func__);
601                 return -EIO;
602         }
603
604         /*
605          * If we are here due to reconnect, free per-smb session key
606          * in case signing was required.
607          */
608         kfree(ses->auth_key.response);
609         ses->auth_key.response = NULL;
610
611         /*
612          * If memory allocation is successful, caller of this function
613          * frees it.
614          */
615         ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
616         if (!ses->ntlmssp)
617                 return -ENOMEM;
618         ses->ntlmssp->sesskey_per_smbsess = true;
619
620         /* FIXME: allow for other auth types besides NTLMSSP (e.g. krb5) */
621         if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
622                 ses->sectype = RawNTLMSSP;
623
624 ssetup_ntlmssp_authenticate:
625         if (phase == NtLmChallenge)
626                 phase = NtLmAuthenticate; /* if ntlmssp, now final phase */
627
628         rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
629         if (rc)
630                 return rc;
631
632         req->hdr.SessionId = 0; /* First session, not a reauthenticate */
633         req->Flags = 0; /* MBZ */
634         /* to enable echos and oplocks */
635         req->hdr.CreditRequest = cpu_to_le16(3);
636
637         /* only one of SMB2 signing flags may be set in SMB2 request */
638         if (server->sign)
639                 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
640         else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
641                 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
642         else
643                 req->SecurityMode = 0;
644
645         req->Capabilities = 0;
646         req->Channel = 0; /* MBZ */
647
648         iov[0].iov_base = (char *)req;
649         /* 4 for rfc1002 length field and 1 for pad */
650         iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
651
652         if (ses->sectype == Kerberos) {
653 #ifdef CONFIG_CIFS_UPCALL
654                 struct cifs_spnego_msg *msg;
655
656                 spnego_key = cifs_get_spnego_key(ses);
657                 if (IS_ERR(spnego_key)) {
658                         rc = PTR_ERR(spnego_key);
659                         spnego_key = NULL;
660                         goto ssetup_exit;
661                 }
662
663                 msg = spnego_key->payload.data[0];
664                 /*
665                  * check version field to make sure that cifs.upcall is
666                  * sending us a response in an expected form
667                  */
668                 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
669                         cifs_dbg(VFS,
670                                   "bad cifs.upcall version. Expected %d got %d",
671                                   CIFS_SPNEGO_UPCALL_VERSION, msg->version);
672                         rc = -EKEYREJECTED;
673                         goto ssetup_exit;
674                 }
675                 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
676                                                  GFP_KERNEL);
677                 if (!ses->auth_key.response) {
678                         cifs_dbg(VFS,
679                                 "Kerberos can't allocate (%u bytes) memory",
680                                 msg->sesskey_len);
681                         rc = -ENOMEM;
682                         goto ssetup_exit;
683                 }
684                 ses->auth_key.len = msg->sesskey_len;
685                 blob_length = msg->secblob_len;
686                 iov[1].iov_base = msg->data + msg->sesskey_len;
687                 iov[1].iov_len = blob_length;
688 #else
689                 rc = -EOPNOTSUPP;
690                 goto ssetup_exit;
691 #endif /* CONFIG_CIFS_UPCALL */
692         } else if (phase == NtLmNegotiate) { /* if not krb5 must be ntlmssp */
693                 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
694                                        GFP_KERNEL);
695                 if (ntlmssp_blob == NULL) {
696                         rc = -ENOMEM;
697                         goto ssetup_exit;
698                 }
699                 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
700                 if (use_spnego) {
701                         /* blob_length = build_spnego_ntlmssp_blob(
702                                         &security_blob,
703                                         sizeof(struct _NEGOTIATE_MESSAGE),
704                                         ntlmssp_blob); */
705                         /* BB eventually need to add this */
706                         cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
707                         rc = -EOPNOTSUPP;
708                         kfree(ntlmssp_blob);
709                         goto ssetup_exit;
710                 } else {
711                         blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
712                         /* with raw NTLMSSP we don't encapsulate in SPNEGO */
713                         security_blob = ntlmssp_blob;
714                 }
715                 iov[1].iov_base = security_blob;
716                 iov[1].iov_len = blob_length;
717         } else if (phase == NtLmAuthenticate) {
718                 req->hdr.SessionId = ses->Suid;
719                 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
720                                              nls_cp);
721                 if (rc) {
722                         cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n",
723                                  rc);
724                         goto ssetup_exit; /* BB double check error handling */
725                 }
726                 if (use_spnego) {
727                         /* blob_length = build_spnego_ntlmssp_blob(
728                                                         &security_blob,
729                                                         blob_length,
730                                                         ntlmssp_blob); */
731                         cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
732                         rc = -EOPNOTSUPP;
733                         kfree(ntlmssp_blob);
734                         goto ssetup_exit;
735                 } else {
736                         security_blob = ntlmssp_blob;
737                 }
738                 iov[1].iov_base = security_blob;
739                 iov[1].iov_len = blob_length;
740         } else {
741                 cifs_dbg(VFS, "illegal ntlmssp phase\n");
742                 rc = -EIO;
743                 goto ssetup_exit;
744         }
745
746         /* Testing shows that buffer offset must be at location of Buffer[0] */
747         req->SecurityBufferOffset =
748                                 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
749                                             1 /* pad */ - 4 /* rfc1001 len */);
750         req->SecurityBufferLength = cpu_to_le16(blob_length);
751
752         inc_rfc1001_len(req, blob_length - 1 /* pad */);
753
754         /* BB add code to build os and lm fields */
755
756         rc = SendReceive2(xid, ses, iov, 2, &resp_buftype,
757                           CIFS_LOG_ERROR | CIFS_NEG_OP);
758
759         kfree(security_blob);
760         rsp = (struct smb2_sess_setup_rsp *)iov[0].iov_base;
761         ses->Suid = rsp->hdr.SessionId;
762         if (resp_buftype != CIFS_NO_BUFFER &&
763             rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) {
764                 if (phase != NtLmNegotiate) {
765                         cifs_dbg(VFS, "Unexpected more processing error\n");
766                         goto ssetup_exit;
767                 }
768                 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
769                                 le16_to_cpu(rsp->SecurityBufferOffset)) {
770                         cifs_dbg(VFS, "Invalid security buffer offset %d\n",
771                                  le16_to_cpu(rsp->SecurityBufferOffset));
772                         rc = -EIO;
773                         goto ssetup_exit;
774                 }
775
776                 /* NTLMSSP Negotiate sent now processing challenge (response) */
777                 phase = NtLmChallenge; /* process ntlmssp challenge */
778                 rc = 0; /* MORE_PROCESSING is not an error here but expected */
779                 rc = decode_ntlmssp_challenge(rsp->Buffer,
780                                 le16_to_cpu(rsp->SecurityBufferLength), ses);
781         }
782
783         /*
784          * BB eventually add code for SPNEGO decoding of NtlmChallenge blob,
785          * but at least the raw NTLMSSP case works.
786          */
787         /*
788          * No tcon so can't do
789          * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
790          */
791         if (rc != 0)
792                 goto ssetup_exit;
793
794         ses->session_flags = le16_to_cpu(rsp->SessionFlags);
795         if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
796                 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
797 ssetup_exit:
798         free_rsp_buf(resp_buftype, rsp);
799
800         /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
801         if ((phase == NtLmChallenge) && (rc == 0))
802                 goto ssetup_ntlmssp_authenticate;
803
804         if (!rc) {
805                 mutex_lock(&server->srv_mutex);
806                 if (server->sign && server->ops->generate_signingkey) {
807                         rc = server->ops->generate_signingkey(ses);
808                         kfree(ses->auth_key.response);
809                         ses->auth_key.response = NULL;
810                         if (rc) {
811                                 cifs_dbg(FYI,
812                                         "SMB3 session key generation failed\n");
813                                 mutex_unlock(&server->srv_mutex);
814                                 goto keygen_exit;
815                         }
816                 }
817                 if (!server->session_estab) {
818                         server->sequence_number = 0x2;
819                         server->session_estab = true;
820                 }
821                 mutex_unlock(&server->srv_mutex);
822
823                 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
824                 spin_lock(&GlobalMid_Lock);
825                 ses->status = CifsGood;
826                 ses->need_reconnect = false;
827                 spin_unlock(&GlobalMid_Lock);
828         }
829
830 keygen_exit:
831         if (!server->sign) {
832                 kfree(ses->auth_key.response);
833                 ses->auth_key.response = NULL;
834         }
835         if (spnego_key) {
836                 key_invalidate(spnego_key);
837                 key_put(spnego_key);
838         }
839         kfree(ses->ntlmssp);
840
841         return rc;
842 }
843
844 int
845 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
846 {
847         struct smb2_logoff_req *req; /* response is also trivial struct */
848         int rc = 0;
849         struct TCP_Server_Info *server;
850
851         cifs_dbg(FYI, "disconnect session %p\n", ses);
852
853         if (ses && (ses->server))
854                 server = ses->server;
855         else
856                 return -EIO;
857
858         /* no need to send SMB logoff if uid already closed due to reconnect */
859         if (ses->need_reconnect)
860                 goto smb2_session_already_dead;
861
862         rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
863         if (rc)
864                 return rc;
865
866          /* since no tcon, smb2_init can not do this, so do here */
867         req->hdr.SessionId = ses->Suid;
868         if (server->sign)
869                 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
870
871         rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
872         /*
873          * No tcon so can't do
874          * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
875          */
876
877 smb2_session_already_dead:
878         return rc;
879 }
880
881 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
882 {
883         cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
884 }
885
886 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
887
888 /* These are similar values to what Windows uses */
889 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
890 {
891         tcon->max_chunks = 256;
892         tcon->max_bytes_chunk = 1048576;
893         tcon->max_bytes_copy = 16777216;
894 }
895
896 int
897 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
898           struct cifs_tcon *tcon, const struct nls_table *cp)
899 {
900         struct smb2_tree_connect_req *req;
901         struct smb2_tree_connect_rsp *rsp = NULL;
902         struct kvec iov[2];
903         int rc = 0;
904         int resp_buftype;
905         int unc_path_len;
906         struct TCP_Server_Info *server;
907         __le16 *unc_path = NULL;
908
909         cifs_dbg(FYI, "TCON\n");
910
911         if ((ses->server) && tree)
912                 server = ses->server;
913         else
914                 return -EIO;
915
916         if (tcon && tcon->bad_network_name)
917                 return -ENOENT;
918
919         if ((tcon && tcon->seal) &&
920             ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
921                 cifs_dbg(VFS, "encryption requested but no server support");
922                 return -EOPNOTSUPP;
923         }
924
925         unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
926         if (unc_path == NULL)
927                 return -ENOMEM;
928
929         unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
930         unc_path_len *= 2;
931         if (unc_path_len < 2) {
932                 kfree(unc_path);
933                 return -EINVAL;
934         }
935
936         rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
937         if (rc) {
938                 kfree(unc_path);
939                 return rc;
940         }
941
942         if (tcon == NULL) {
943                 /* since no tcon, smb2_init can not do this, so do here */
944                 req->hdr.SessionId = ses->Suid;
945                 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
946                         req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
947         }
948
949         iov[0].iov_base = (char *)req;
950         /* 4 for rfc1002 length field and 1 for pad */
951         iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
952
953         /* Testing shows that buffer offset must be at location of Buffer[0] */
954         req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
955                         - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
956         req->PathLength = cpu_to_le16(unc_path_len - 2);
957         iov[1].iov_base = unc_path;
958         iov[1].iov_len = unc_path_len;
959
960         inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
961
962         rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
963         rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
964
965         if (rc != 0) {
966                 if (tcon) {
967                         cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
968                         tcon->need_reconnect = true;
969                 }
970                 goto tcon_error_exit;
971         }
972
973         if (tcon == NULL) {
974                 ses->ipc_tid = rsp->hdr.TreeId;
975                 goto tcon_exit;
976         }
977
978         if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
979                 cifs_dbg(FYI, "connection to disk share\n");
980         else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
981                 tcon->ipc = true;
982                 cifs_dbg(FYI, "connection to pipe share\n");
983         } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
984                 tcon->print = true;
985                 cifs_dbg(FYI, "connection to printer\n");
986         } else {
987                 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
988                 rc = -EOPNOTSUPP;
989                 goto tcon_error_exit;
990         }
991
992         tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
993         tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
994         tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
995         tcon->tidStatus = CifsGood;
996         tcon->need_reconnect = false;
997         tcon->tid = rsp->hdr.TreeId;
998         strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
999
1000         if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1001             ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1002                 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1003         init_copy_chunk_defaults(tcon);
1004         if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1005                 cifs_dbg(VFS, "Encrypted shares not supported");
1006         if (tcon->ses->server->ops->validate_negotiate)
1007                 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1008 tcon_exit:
1009         free_rsp_buf(resp_buftype, rsp);
1010         kfree(unc_path);
1011         return rc;
1012
1013 tcon_error_exit:
1014         if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
1015                 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1016                 if (tcon)
1017                         tcon->bad_network_name = true;
1018         }
1019         goto tcon_exit;
1020 }
1021
1022 int
1023 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1024 {
1025         struct smb2_tree_disconnect_req *req; /* response is trivial */
1026         int rc = 0;
1027         struct TCP_Server_Info *server;
1028         struct cifs_ses *ses = tcon->ses;
1029
1030         cifs_dbg(FYI, "Tree Disconnect\n");
1031
1032         if (ses && (ses->server))
1033                 server = ses->server;
1034         else
1035                 return -EIO;
1036
1037         if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1038                 return 0;
1039
1040         rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1041         if (rc)
1042                 return rc;
1043
1044         rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
1045         if (rc)
1046                 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1047
1048         return rc;
1049 }
1050
1051
1052 static struct create_durable *
1053 create_durable_buf(void)
1054 {
1055         struct create_durable *buf;
1056
1057         buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1058         if (!buf)
1059                 return NULL;
1060
1061         buf->ccontext.DataOffset = cpu_to_le16(offsetof
1062                                         (struct create_durable, Data));
1063         buf->ccontext.DataLength = cpu_to_le32(16);
1064         buf->ccontext.NameOffset = cpu_to_le16(offsetof
1065                                 (struct create_durable, Name));
1066         buf->ccontext.NameLength = cpu_to_le16(4);
1067         /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1068         buf->Name[0] = 'D';
1069         buf->Name[1] = 'H';
1070         buf->Name[2] = 'n';
1071         buf->Name[3] = 'Q';
1072         return buf;
1073 }
1074
1075 static struct create_durable *
1076 create_reconnect_durable_buf(struct cifs_fid *fid)
1077 {
1078         struct create_durable *buf;
1079
1080         buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1081         if (!buf)
1082                 return NULL;
1083
1084         buf->ccontext.DataOffset = cpu_to_le16(offsetof
1085                                         (struct create_durable, Data));
1086         buf->ccontext.DataLength = cpu_to_le32(16);
1087         buf->ccontext.NameOffset = cpu_to_le16(offsetof
1088                                 (struct create_durable, Name));
1089         buf->ccontext.NameLength = cpu_to_le16(4);
1090         buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1091         buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1092         /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1093         buf->Name[0] = 'D';
1094         buf->Name[1] = 'H';
1095         buf->Name[2] = 'n';
1096         buf->Name[3] = 'C';
1097         return buf;
1098 }
1099
1100 static __u8
1101 parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1102                   unsigned int *epoch)
1103 {
1104         char *data_offset;
1105         struct create_context *cc;
1106         unsigned int next;
1107         unsigned int remaining;
1108         char *name;
1109
1110         data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
1111         remaining = le32_to_cpu(rsp->CreateContextsLength);
1112         cc = (struct create_context *)data_offset;
1113         while (remaining >= sizeof(struct create_context)) {
1114                 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1115                 if (le16_to_cpu(cc->NameLength) == 4 &&
1116                     strncmp(name, "RqLs", 4) == 0)
1117                         return server->ops->parse_lease_buf(cc, epoch);
1118
1119                 next = le32_to_cpu(cc->Next);
1120                 if (!next)
1121                         break;
1122                 remaining -= next;
1123                 cc = (struct create_context *)((char *)cc + next);
1124         }
1125
1126         return 0;
1127 }
1128
1129 static int
1130 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1131                   unsigned int *num_iovec, __u8 *oplock)
1132 {
1133         struct smb2_create_req *req = iov[0].iov_base;
1134         unsigned int num = *num_iovec;
1135
1136         iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
1137         if (iov[num].iov_base == NULL)
1138                 return -ENOMEM;
1139         iov[num].iov_len = server->vals->create_lease_size;
1140         req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1141         if (!req->CreateContextsOffset)
1142                 req->CreateContextsOffset = cpu_to_le32(
1143                                 sizeof(struct smb2_create_req) - 4 +
1144                                 iov[num - 1].iov_len);
1145         le32_add_cpu(&req->CreateContextsLength,
1146                      server->vals->create_lease_size);
1147         inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
1148         *num_iovec = num + 1;
1149         return 0;
1150 }
1151
1152 static struct create_durable_v2 *
1153 create_durable_v2_buf(struct cifs_fid *pfid)
1154 {
1155         struct create_durable_v2 *buf;
1156
1157         buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1158         if (!buf)
1159                 return NULL;
1160
1161         buf->ccontext.DataOffset = cpu_to_le16(offsetof
1162                                         (struct create_durable_v2, dcontext));
1163         buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1164         buf->ccontext.NameOffset = cpu_to_le16(offsetof
1165                                 (struct create_durable_v2, Name));
1166         buf->ccontext.NameLength = cpu_to_le16(4);
1167
1168         buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1169         buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1170         get_random_bytes(buf->dcontext.CreateGuid, 16);
1171         memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1172
1173         /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1174         buf->Name[0] = 'D';
1175         buf->Name[1] = 'H';
1176         buf->Name[2] = '2';
1177         buf->Name[3] = 'Q';
1178         return buf;
1179 }
1180
1181 static struct create_durable_handle_reconnect_v2 *
1182 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1183 {
1184         struct create_durable_handle_reconnect_v2 *buf;
1185
1186         buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1187                         GFP_KERNEL);
1188         if (!buf)
1189                 return NULL;
1190
1191         buf->ccontext.DataOffset =
1192                 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1193                                      dcontext));
1194         buf->ccontext.DataLength =
1195                 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1196         buf->ccontext.NameOffset =
1197                 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1198                             Name));
1199         buf->ccontext.NameLength = cpu_to_le16(4);
1200
1201         buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1202         buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1203         buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1204         memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1205
1206         /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1207         buf->Name[0] = 'D';
1208         buf->Name[1] = 'H';
1209         buf->Name[2] = '2';
1210         buf->Name[3] = 'C';
1211         return buf;
1212 }
1213
1214 static int
1215 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1216                     struct cifs_open_parms *oparms)
1217 {
1218         struct smb2_create_req *req = iov[0].iov_base;
1219         unsigned int num = *num_iovec;
1220
1221         iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1222         if (iov[num].iov_base == NULL)
1223                 return -ENOMEM;
1224         iov[num].iov_len = sizeof(struct create_durable_v2);
1225         if (!req->CreateContextsOffset)
1226                 req->CreateContextsOffset =
1227                         cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1228                                                                 iov[1].iov_len);
1229         le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1230         inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1231         *num_iovec = num + 1;
1232         return 0;
1233 }
1234
1235 static int
1236 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1237                     struct cifs_open_parms *oparms)
1238 {
1239         struct smb2_create_req *req = iov[0].iov_base;
1240         unsigned int num = *num_iovec;
1241
1242         /* indicate that we don't need to relock the file */
1243         oparms->reconnect = false;
1244
1245         iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1246         if (iov[num].iov_base == NULL)
1247                 return -ENOMEM;
1248         iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1249         if (!req->CreateContextsOffset)
1250                 req->CreateContextsOffset =
1251                         cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1252                                                                 iov[1].iov_len);
1253         le32_add_cpu(&req->CreateContextsLength,
1254                         sizeof(struct create_durable_handle_reconnect_v2));
1255         inc_rfc1001_len(&req->hdr,
1256                         sizeof(struct create_durable_handle_reconnect_v2));
1257         *num_iovec = num + 1;
1258         return 0;
1259 }
1260
1261 static int
1262 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1263                     struct cifs_open_parms *oparms, bool use_persistent)
1264 {
1265         struct smb2_create_req *req = iov[0].iov_base;
1266         unsigned int num = *num_iovec;
1267
1268         if (use_persistent) {
1269                 if (oparms->reconnect)
1270                         return add_durable_reconnect_v2_context(iov, num_iovec,
1271                                                                 oparms);
1272                 else
1273                         return add_durable_v2_context(iov, num_iovec, oparms);
1274         }
1275
1276         if (oparms->reconnect) {
1277                 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1278                 /* indicate that we don't need to relock the file */
1279                 oparms->reconnect = false;
1280         } else
1281                 iov[num].iov_base = create_durable_buf();
1282         if (iov[num].iov_base == NULL)
1283                 return -ENOMEM;
1284         iov[num].iov_len = sizeof(struct create_durable);
1285         if (!req->CreateContextsOffset)
1286                 req->CreateContextsOffset =
1287                         cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1288                                                                 iov[1].iov_len);
1289         le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1290         inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1291         *num_iovec = num + 1;
1292         return 0;
1293 }
1294
1295 int
1296 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
1297           __u8 *oplock, struct smb2_file_all_info *buf,
1298           struct smb2_err_rsp **err_buf)
1299 {
1300         struct smb2_create_req *req;
1301         struct smb2_create_rsp *rsp;
1302         struct TCP_Server_Info *server;
1303         struct cifs_tcon *tcon = oparms->tcon;
1304         struct cifs_ses *ses = tcon->ses;
1305         struct kvec iov[4];
1306         int resp_buftype;
1307         int uni_path_len;
1308         __le16 *copy_path = NULL;
1309         int copy_size;
1310         int rc = 0;
1311         unsigned int num_iovecs = 2;
1312         __u32 file_attributes = 0;
1313         char *dhc_buf = NULL, *lc_buf = NULL;
1314
1315         cifs_dbg(FYI, "create/open\n");
1316
1317         if (ses && (ses->server))
1318                 server = ses->server;
1319         else
1320                 return -EIO;
1321
1322         rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1323         if (rc)
1324                 return rc;
1325
1326         if (oparms->create_options & CREATE_OPTION_READONLY)
1327                 file_attributes |= ATTR_READONLY;
1328         if (oparms->create_options & CREATE_OPTION_SPECIAL)
1329                 file_attributes |= ATTR_SYSTEM;
1330
1331         req->ImpersonationLevel = IL_IMPERSONATION;
1332         req->DesiredAccess = cpu_to_le32(oparms->desired_access);
1333         /* File attributes ignored on open (used in create though) */
1334         req->FileAttributes = cpu_to_le32(file_attributes);
1335         req->ShareAccess = FILE_SHARE_ALL_LE;
1336         req->CreateDisposition = cpu_to_le32(oparms->disposition);
1337         req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
1338         uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1339         /* do not count rfc1001 len field */
1340         req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1341
1342         iov[0].iov_base = (char *)req;
1343         /* 4 for rfc1002 length field */
1344         iov[0].iov_len = get_rfc1002_length(req) + 4;
1345
1346         /* MUST set path len (NameLength) to 0 opening root of share */
1347         req->NameLength = cpu_to_le16(uni_path_len - 2);
1348         /* -1 since last byte is buf[0] which is sent below (path) */
1349         iov[0].iov_len--;
1350         if (uni_path_len % 8 != 0) {
1351                 copy_size = uni_path_len / 8 * 8;
1352                 if (copy_size < uni_path_len)
1353                         copy_size += 8;
1354
1355                 copy_path = kzalloc(copy_size, GFP_KERNEL);
1356                 if (!copy_path)
1357                         return -ENOMEM;
1358                 memcpy((char *)copy_path, (const char *)path,
1359                         uni_path_len);
1360                 uni_path_len = copy_size;
1361                 path = copy_path;
1362         }
1363
1364         iov[1].iov_len = uni_path_len;
1365         iov[1].iov_base = path;
1366         /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1367         inc_rfc1001_len(req, uni_path_len - 1);
1368
1369         if (!server->oplocks)
1370                 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1371
1372         if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
1373             *oplock == SMB2_OPLOCK_LEVEL_NONE)
1374                 req->RequestedOplockLevel = *oplock;
1375         else {
1376                 rc = add_lease_context(server, iov, &num_iovecs, oplock);
1377                 if (rc) {
1378                         cifs_small_buf_release(req);
1379                         kfree(copy_path);
1380                         return rc;
1381                 }
1382                 lc_buf = iov[num_iovecs-1].iov_base;
1383         }
1384
1385         if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1386                 /* need to set Next field of lease context if we request it */
1387                 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
1388                         struct create_context *ccontext =
1389                             (struct create_context *)iov[num_iovecs-1].iov_base;
1390                         ccontext->Next =
1391                                 cpu_to_le32(server->vals->create_lease_size);
1392                 }
1393
1394                 rc = add_durable_context(iov, &num_iovecs, oparms,
1395                                         tcon->use_persistent);
1396                 if (rc) {
1397                         cifs_small_buf_release(req);
1398                         kfree(copy_path);
1399                         kfree(lc_buf);
1400                         return rc;
1401                 }
1402                 dhc_buf = iov[num_iovecs-1].iov_base;
1403         }
1404
1405         rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1406         rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1407
1408         if (rc != 0) {
1409                 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
1410                 if (err_buf)
1411                         *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1412                                            GFP_KERNEL);
1413                 goto creat_exit;
1414         }
1415
1416         oparms->fid->persistent_fid = rsp->PersistentFileId;
1417         oparms->fid->volatile_fid = rsp->VolatileFileId;
1418
1419         if (buf) {
1420                 memcpy(buf, &rsp->CreationTime, 32);
1421                 buf->AllocationSize = rsp->AllocationSize;
1422                 buf->EndOfFile = rsp->EndofFile;
1423                 buf->Attributes = rsp->FileAttributes;
1424                 buf->NumberOfLinks = cpu_to_le32(1);
1425                 buf->DeletePending = 0;
1426         }
1427
1428         if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
1429                 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
1430         else
1431                 *oplock = rsp->OplockLevel;
1432 creat_exit:
1433         kfree(copy_path);
1434         kfree(lc_buf);
1435         kfree(dhc_buf);
1436         free_rsp_buf(resp_buftype, rsp);
1437         return rc;
1438 }
1439
1440 /*
1441  *      SMB2 IOCTL is used for both IOCTLs and FSCTLs
1442  */
1443 int
1444 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1445            u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1446            u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1447 {
1448         struct smb2_ioctl_req *req;
1449         struct smb2_ioctl_rsp *rsp;
1450         struct TCP_Server_Info *server;
1451         struct cifs_ses *ses;
1452         struct kvec iov[2];
1453         int resp_buftype;
1454         int num_iovecs;
1455         int rc = 0;
1456
1457         cifs_dbg(FYI, "SMB2 IOCTL\n");
1458
1459         if (out_data != NULL)
1460                 *out_data = NULL;
1461
1462         /* zero out returned data len, in case of error */
1463         if (plen)
1464                 *plen = 0;
1465
1466         if (tcon)
1467                 ses = tcon->ses;
1468         else
1469                 return -EIO;
1470
1471         if (ses && (ses->server))
1472                 server = ses->server;
1473         else
1474                 return -EIO;
1475
1476         rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1477         if (rc)
1478                 return rc;
1479
1480         req->CtlCode = cpu_to_le32(opcode);
1481         req->PersistentFileId = persistent_fid;
1482         req->VolatileFileId = volatile_fid;
1483
1484         if (indatalen) {
1485                 req->InputCount = cpu_to_le32(indatalen);
1486                 /* do not set InputOffset if no input data */
1487                 req->InputOffset =
1488                        cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1489                 iov[1].iov_base = in_data;
1490                 iov[1].iov_len = indatalen;
1491                 num_iovecs = 2;
1492         } else
1493                 num_iovecs = 1;
1494
1495         req->OutputOffset = 0;
1496         req->OutputCount = 0; /* MBZ */
1497
1498         /*
1499          * Could increase MaxOutputResponse, but that would require more
1500          * than one credit. Windows typically sets this smaller, but for some
1501          * ioctls it may be useful to allow server to send more. No point
1502          * limiting what the server can send as long as fits in one credit
1503          */
1504         req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1505
1506         if (is_fsctl)
1507                 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1508         else
1509                 req->Flags = 0;
1510
1511         iov[0].iov_base = (char *)req;
1512
1513         /*
1514          * If no input data, the size of ioctl struct in
1515          * protocol spec still includes a 1 byte data buffer,
1516          * but if input data passed to ioctl, we do not
1517          * want to double count this, so we do not send
1518          * the dummy one byte of data in iovec[0] if sending
1519          * input data (in iovec[1]). We also must add 4 bytes
1520          * in first iovec to allow for rfc1002 length field.
1521          */
1522
1523         if (indatalen) {
1524                 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1525                 inc_rfc1001_len(req, indatalen - 1);
1526         } else
1527                 iov[0].iov_len = get_rfc1002_length(req) + 4;
1528
1529
1530         rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1531         rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1532
1533         if ((rc != 0) && (rc != -EINVAL)) {
1534                 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1535                 goto ioctl_exit;
1536         } else if (rc == -EINVAL) {
1537                 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1538                     (opcode != FSCTL_SRV_COPYCHUNK)) {
1539                         cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1540                         goto ioctl_exit;
1541                 }
1542         }
1543
1544         /* check if caller wants to look at return data or just return rc */
1545         if ((plen == NULL) || (out_data == NULL))
1546                 goto ioctl_exit;
1547
1548         *plen = le32_to_cpu(rsp->OutputCount);
1549
1550         /* We check for obvious errors in the output buffer length and offset */
1551         if (*plen == 0)
1552                 goto ioctl_exit; /* server returned no data */
1553         else if (*plen > 0xFF00) {
1554                 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1555                 *plen = 0;
1556                 rc = -EIO;
1557                 goto ioctl_exit;
1558         }
1559
1560         if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1561                 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1562                         le32_to_cpu(rsp->OutputOffset));
1563                 *plen = 0;
1564                 rc = -EIO;
1565                 goto ioctl_exit;
1566         }
1567
1568         *out_data = kmalloc(*plen, GFP_KERNEL);
1569         if (*out_data == NULL) {
1570                 rc = -ENOMEM;
1571                 goto ioctl_exit;
1572         }
1573
1574         memcpy(*out_data, rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
1575                *plen);
1576 ioctl_exit:
1577         free_rsp_buf(resp_buftype, rsp);
1578         return rc;
1579 }
1580
1581 /*
1582  *   Individual callers to ioctl worker function follow
1583  */
1584
1585 int
1586 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1587                      u64 persistent_fid, u64 volatile_fid)
1588 {
1589         int rc;
1590         struct  compress_ioctl fsctl_input;
1591         char *ret_data = NULL;
1592
1593         fsctl_input.CompressionState =
1594                         cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
1595
1596         rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1597                         FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1598                         (char *)&fsctl_input /* data input */,
1599                         2 /* in data len */, &ret_data /* out data */, NULL);
1600
1601         cifs_dbg(FYI, "set compression rc %d\n", rc);
1602
1603         return rc;
1604 }
1605
1606 int
1607 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1608            u64 persistent_fid, u64 volatile_fid)
1609 {
1610         struct smb2_close_req *req;
1611         struct smb2_close_rsp *rsp;
1612         struct TCP_Server_Info *server;
1613         struct cifs_ses *ses = tcon->ses;
1614         struct kvec iov[1];
1615         int resp_buftype;
1616         int rc = 0;
1617
1618         cifs_dbg(FYI, "Close\n");
1619
1620         if (ses && (ses->server))
1621                 server = ses->server;
1622         else
1623                 return -EIO;
1624
1625         rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1626         if (rc)
1627                 return rc;
1628
1629         req->PersistentFileId = persistent_fid;
1630         req->VolatileFileId = volatile_fid;
1631
1632         iov[0].iov_base = (char *)req;
1633         /* 4 for rfc1002 length field */
1634         iov[0].iov_len = get_rfc1002_length(req) + 4;
1635
1636         rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1637         rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1638
1639         if (rc != 0) {
1640                 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
1641                 goto close_exit;
1642         }
1643
1644         /* BB FIXME - decode close response, update inode for caching */
1645
1646 close_exit:
1647         free_rsp_buf(resp_buftype, rsp);
1648         return rc;
1649 }
1650
1651 static int
1652 validate_buf(unsigned int offset, unsigned int buffer_length,
1653              struct smb2_hdr *hdr, unsigned int min_buf_size)
1654
1655 {
1656         unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1657         char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1658         char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1659         char *end_of_buf = begin_of_buf + buffer_length;
1660
1661
1662         if (buffer_length < min_buf_size) {
1663                 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1664                          buffer_length, min_buf_size);
1665                 return -EINVAL;
1666         }
1667
1668         /* check if beyond RFC1001 maximum length */
1669         if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
1670                 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1671                          buffer_length, smb_len);
1672                 return -EINVAL;
1673         }
1674
1675         if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
1676                 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
1677                 return -EINVAL;
1678         }
1679
1680         return 0;
1681 }
1682
1683 /*
1684  * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1685  * Caller must free buffer.
1686  */
1687 static int
1688 validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1689                       struct smb2_hdr *hdr, unsigned int minbufsize,
1690                       char *data)
1691
1692 {
1693         char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1694         int rc;
1695
1696         if (!data)
1697                 return -EINVAL;
1698
1699         rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1700         if (rc)
1701                 return rc;
1702
1703         memcpy(data, begin_of_buf, buffer_length);
1704
1705         return 0;
1706 }
1707
1708 static int
1709 query_info(const unsigned int xid, struct cifs_tcon *tcon,
1710            u64 persistent_fid, u64 volatile_fid, u8 info_class,
1711            size_t output_len, size_t min_len, void *data)
1712 {
1713         struct smb2_query_info_req *req;
1714         struct smb2_query_info_rsp *rsp = NULL;
1715         struct kvec iov[2];
1716         int rc = 0;
1717         int resp_buftype;
1718         struct TCP_Server_Info *server;
1719         struct cifs_ses *ses = tcon->ses;
1720
1721         cifs_dbg(FYI, "Query Info\n");
1722
1723         if (ses && (ses->server))
1724                 server = ses->server;
1725         else
1726                 return -EIO;
1727
1728         rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1729         if (rc)
1730                 return rc;
1731
1732         req->InfoType = SMB2_O_INFO_FILE;
1733         req->FileInfoClass = info_class;
1734         req->PersistentFileId = persistent_fid;
1735         req->VolatileFileId = volatile_fid;
1736         /* 4 for rfc1002 length field and 1 for Buffer */
1737         req->InputBufferOffset =
1738                 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
1739         req->OutputBufferLength = cpu_to_le32(output_len);
1740
1741         iov[0].iov_base = (char *)req;
1742         /* 4 for rfc1002 length field */
1743         iov[0].iov_len = get_rfc1002_length(req) + 4;
1744
1745         rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1746         rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1747
1748         if (rc) {
1749                 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1750                 goto qinf_exit;
1751         }
1752
1753         rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1754                                    le32_to_cpu(rsp->OutputBufferLength),
1755                                    &rsp->hdr, min_len, data);
1756
1757 qinf_exit:
1758         free_rsp_buf(resp_buftype, rsp);
1759         return rc;
1760 }
1761
1762 int
1763 SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1764                 u64 persistent_fid, u64 volatile_fid,
1765                 struct smb2_file_all_info *data)
1766 {
1767         return query_info(xid, tcon, persistent_fid, volatile_fid,
1768                           FILE_ALL_INFORMATION,
1769                           sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
1770                           sizeof(struct smb2_file_all_info), data);
1771 }
1772
1773 int
1774 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1775                  u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1776 {
1777         return query_info(xid, tcon, persistent_fid, volatile_fid,
1778                           FILE_INTERNAL_INFORMATION,
1779                           sizeof(struct smb2_file_internal_info),
1780                           sizeof(struct smb2_file_internal_info), uniqueid);
1781 }
1782
1783 /*
1784  * This is a no-op for now. We're not really interested in the reply, but
1785  * rather in the fact that the server sent one and that server->lstrp
1786  * gets updated.
1787  *
1788  * FIXME: maybe we should consider checking that the reply matches request?
1789  */
1790 static void
1791 smb2_echo_callback(struct mid_q_entry *mid)
1792 {
1793         struct TCP_Server_Info *server = mid->callback_data;
1794         struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1795         unsigned int credits_received = 1;
1796
1797         if (mid->mid_state == MID_RESPONSE_RECEIVED)
1798                 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1799
1800         mutex_lock(&server->srv_mutex);
1801         DeleteMidQEntry(mid);
1802         mutex_unlock(&server->srv_mutex);
1803         add_credits(server, credits_received, CIFS_ECHO_OP);
1804 }
1805
1806 int
1807 SMB2_echo(struct TCP_Server_Info *server)
1808 {
1809         struct smb2_echo_req *req;
1810         int rc = 0;
1811         struct kvec iov;
1812         struct smb_rqst rqst = { .rq_iov = &iov,
1813                                  .rq_nvec = 1 };
1814
1815         cifs_dbg(FYI, "In echo request\n");
1816
1817         if (server->tcpStatus == CifsNeedNegotiate) {
1818                 struct list_head *tmp, *tmp2;
1819                 struct cifs_ses *ses;
1820                 struct cifs_tcon *tcon;
1821
1822                 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
1823                 spin_lock(&cifs_tcp_ses_lock);
1824                 list_for_each(tmp, &server->smb_ses_list) {
1825                         ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
1826                         list_for_each(tmp2, &ses->tcon_list) {
1827                                 tcon = list_entry(tmp2, struct cifs_tcon,
1828                                                   tcon_list);
1829                                 /* add check for persistent handle reconnect */
1830                                 if (tcon && tcon->need_reconnect) {
1831                                         spin_unlock(&cifs_tcp_ses_lock);
1832                                         rc = smb2_reconnect(SMB2_ECHO, tcon);
1833                                         spin_lock(&cifs_tcp_ses_lock);
1834                                 }
1835                         }
1836                 }
1837                 spin_unlock(&cifs_tcp_ses_lock);
1838         }
1839
1840         /* if no session, renegotiate failed above */
1841         if (server->tcpStatus == CifsNeedNegotiate)
1842                 return -EIO;
1843
1844         rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
1845         if (rc)
1846                 return rc;
1847
1848         req->hdr.CreditRequest = cpu_to_le16(1);
1849
1850         iov.iov_base = (char *)req;
1851         /* 4 for rfc1002 length field */
1852         iov.iov_len = get_rfc1002_length(req) + 4;
1853
1854         rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
1855                              CIFS_ECHO_OP);
1856         if (rc)
1857                 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
1858
1859         cifs_small_buf_release(req);
1860         return rc;
1861 }
1862
1863 int
1864 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1865            u64 volatile_fid)
1866 {
1867         struct smb2_flush_req *req;
1868         struct TCP_Server_Info *server;
1869         struct cifs_ses *ses = tcon->ses;
1870         struct kvec iov[1];
1871         int resp_buftype;
1872         int rc = 0;
1873
1874         cifs_dbg(FYI, "Flush\n");
1875
1876         if (ses && (ses->server))
1877                 server = ses->server;
1878         else
1879                 return -EIO;
1880
1881         rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
1882         if (rc)
1883                 return rc;
1884
1885         req->PersistentFileId = persistent_fid;
1886         req->VolatileFileId = volatile_fid;
1887
1888         iov[0].iov_base = (char *)req;
1889         /* 4 for rfc1002 length field */
1890         iov[0].iov_len = get_rfc1002_length(req) + 4;
1891
1892         rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1893
1894         if (rc != 0)
1895                 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
1896
1897         free_rsp_buf(resp_buftype, iov[0].iov_base);
1898         return rc;
1899 }
1900
1901 /*
1902  * To form a chain of read requests, any read requests after the first should
1903  * have the end_of_chain boolean set to true.
1904  */
1905 static int
1906 smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
1907                   unsigned int remaining_bytes, int request_type)
1908 {
1909         int rc = -EACCES;
1910         struct smb2_read_req *req = NULL;
1911
1912         rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
1913         if (rc)
1914                 return rc;
1915         if (io_parms->tcon->ses->server == NULL)
1916                 return -ECONNABORTED;
1917
1918         req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
1919
1920         req->PersistentFileId = io_parms->persistent_fid;
1921         req->VolatileFileId = io_parms->volatile_fid;
1922         req->ReadChannelInfoOffset = 0; /* reserved */
1923         req->ReadChannelInfoLength = 0; /* reserved */
1924         req->Channel = 0; /* reserved */
1925         req->MinimumCount = 0;
1926         req->Length = cpu_to_le32(io_parms->length);
1927         req->Offset = cpu_to_le64(io_parms->offset);
1928
1929         if (request_type & CHAINED_REQUEST) {
1930                 if (!(request_type & END_OF_CHAIN)) {
1931                         /* 4 for rfc1002 length field */
1932                         req->hdr.NextCommand =
1933                                 cpu_to_le32(get_rfc1002_length(req) + 4);
1934                 } else /* END_OF_CHAIN */
1935                         req->hdr.NextCommand = 0;
1936                 if (request_type & RELATED_REQUEST) {
1937                         req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
1938                         /*
1939                          * Related requests use info from previous read request
1940                          * in chain.
1941                          */
1942                         req->hdr.SessionId = 0xFFFFFFFF;
1943                         req->hdr.TreeId = 0xFFFFFFFF;
1944                         req->PersistentFileId = 0xFFFFFFFF;
1945                         req->VolatileFileId = 0xFFFFFFFF;
1946                 }
1947         }
1948         if (remaining_bytes > io_parms->length)
1949                 req->RemainingBytes = cpu_to_le32(remaining_bytes);
1950         else
1951                 req->RemainingBytes = 0;
1952
1953         iov[0].iov_base = (char *)req;
1954         /* 4 for rfc1002 length field */
1955         iov[0].iov_len = get_rfc1002_length(req) + 4;
1956         return rc;
1957 }
1958
1959 static void
1960 smb2_readv_callback(struct mid_q_entry *mid)
1961 {
1962         struct cifs_readdata *rdata = mid->callback_data;
1963         struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1964         struct TCP_Server_Info *server = tcon->ses->server;
1965         struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
1966         unsigned int credits_received = 1;
1967         struct smb_rqst rqst = { .rq_iov = &rdata->iov,
1968                                  .rq_nvec = 1,
1969                                  .rq_pages = rdata->pages,
1970                                  .rq_npages = rdata->nr_pages,
1971                                  .rq_pagesz = rdata->pagesz,
1972                                  .rq_tailsz = rdata->tailsz };
1973
1974         cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1975                  __func__, mid->mid, mid->mid_state, rdata->result,
1976                  rdata->bytes);
1977
1978         switch (mid->mid_state) {
1979         case MID_RESPONSE_RECEIVED:
1980                 credits_received = le16_to_cpu(buf->CreditRequest);
1981                 /* result already set, check signature */
1982                 if (server->sign) {
1983                         int rc;
1984
1985                         rc = smb2_verify_signature(&rqst, server);
1986                         if (rc)
1987                                 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1988                                          rc);
1989                 }
1990                 /* FIXME: should this be counted toward the initiating task? */
1991                 task_io_account_read(rdata->got_bytes);
1992                 cifs_stats_bytes_read(tcon, rdata->got_bytes);
1993                 break;
1994         case MID_REQUEST_SUBMITTED:
1995         case MID_RETRY_NEEDED:
1996                 rdata->result = -EAGAIN;
1997                 if (server->sign && rdata->got_bytes)
1998                         /* reset bytes number since we can not check a sign */
1999                         rdata->got_bytes = 0;
2000                 /* FIXME: should this be counted toward the initiating task? */
2001                 task_io_account_read(rdata->got_bytes);
2002                 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2003                 break;
2004         default:
2005                 if (rdata->result != -ENODATA)
2006                         rdata->result = -EIO;
2007         }
2008
2009         if (rdata->result)
2010                 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2011
2012         queue_work(cifsiod_wq, &rdata->work);
2013         mutex_lock(&server->srv_mutex);
2014         DeleteMidQEntry(mid);
2015         mutex_unlock(&server->srv_mutex);
2016         add_credits(server, credits_received, 0);
2017 }
2018
2019 /* smb2_async_readv - send an async write, and set up mid to handle result */
2020 int
2021 smb2_async_readv(struct cifs_readdata *rdata)
2022 {
2023         int rc, flags = 0;
2024         struct smb2_hdr *buf;
2025         struct cifs_io_parms io_parms;
2026         struct smb_rqst rqst = { .rq_iov = &rdata->iov,
2027                                  .rq_nvec = 1 };
2028         struct TCP_Server_Info *server;
2029
2030         cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2031                  __func__, rdata->offset, rdata->bytes);
2032
2033         io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2034         io_parms.offset = rdata->offset;
2035         io_parms.length = rdata->bytes;
2036         io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2037         io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2038         io_parms.pid = rdata->pid;
2039
2040         server = io_parms.tcon->ses->server;
2041
2042         rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
2043         if (rc) {
2044                 if (rc == -EAGAIN && rdata->credits) {
2045                         /* credits was reset by reconnect */
2046                         rdata->credits = 0;
2047                         /* reduce in_flight value since we won't send the req */
2048                         spin_lock(&server->req_lock);
2049                         server->in_flight--;
2050                         spin_unlock(&server->req_lock);
2051                 }
2052                 return rc;
2053         }
2054
2055         buf = (struct smb2_hdr *)rdata->iov.iov_base;
2056         /* 4 for rfc1002 length field */
2057         rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
2058
2059         if (rdata->credits) {
2060                 buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2061                                                 SMB2_MAX_BUFFER_SIZE));
2062                 spin_lock(&server->req_lock);
2063                 server->credits += rdata->credits -
2064                                                 le16_to_cpu(buf->CreditCharge);
2065                 spin_unlock(&server->req_lock);
2066                 wake_up(&server->request_q);
2067                 flags = CIFS_HAS_CREDITS;
2068         }
2069
2070         kref_get(&rdata->refcount);
2071         rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
2072                              cifs_readv_receive, smb2_readv_callback,
2073                              rdata, flags);
2074         if (rc) {
2075                 kref_put(&rdata->refcount, cifs_readdata_release);
2076                 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2077         }
2078
2079         cifs_small_buf_release(buf);
2080         return rc;
2081 }
2082
2083 int
2084 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2085           unsigned int *nbytes, char **buf, int *buf_type)
2086 {
2087         int resp_buftype, rc = -EACCES;
2088         struct smb2_read_rsp *rsp = NULL;
2089         struct kvec iov[1];
2090
2091         *nbytes = 0;
2092         rc = smb2_new_read_req(iov, io_parms, 0, 0);
2093         if (rc)
2094                 return rc;
2095
2096         rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
2097                           &resp_buftype, CIFS_LOG_ERROR);
2098
2099         rsp = (struct smb2_read_rsp *)iov[0].iov_base;
2100
2101         if (rsp->hdr.Status == STATUS_END_OF_FILE) {
2102                 free_rsp_buf(resp_buftype, iov[0].iov_base);
2103                 return 0;
2104         }
2105
2106         if (rc) {
2107                 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2108                 cifs_dbg(VFS, "Send error in read = %d\n", rc);
2109         } else {
2110                 *nbytes = le32_to_cpu(rsp->DataLength);
2111                 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2112                     (*nbytes > io_parms->length)) {
2113                         cifs_dbg(FYI, "bad length %d for count %d\n",
2114                                  *nbytes, io_parms->length);
2115                         rc = -EIO;
2116                         *nbytes = 0;
2117                 }
2118         }
2119
2120         if (*buf) {
2121                 memcpy(*buf, (char *)rsp->hdr.ProtocolId + rsp->DataOffset,
2122                        *nbytes);
2123                 free_rsp_buf(resp_buftype, iov[0].iov_base);
2124         } else if (resp_buftype != CIFS_NO_BUFFER) {
2125                 *buf = iov[0].iov_base;
2126                 if (resp_buftype == CIFS_SMALL_BUFFER)
2127                         *buf_type = CIFS_SMALL_BUFFER;
2128                 else if (resp_buftype == CIFS_LARGE_BUFFER)
2129                         *buf_type = CIFS_LARGE_BUFFER;
2130         }
2131         return rc;
2132 }
2133
2134 /*
2135  * Check the mid_state and signature on received buffer (if any), and queue the
2136  * workqueue completion task.
2137  */
2138 static void
2139 smb2_writev_callback(struct mid_q_entry *mid)
2140 {
2141         struct cifs_writedata *wdata = mid->callback_data;
2142         struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2143         struct TCP_Server_Info *server = tcon->ses->server;
2144         unsigned int written;
2145         struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2146         unsigned int credits_received = 1;
2147
2148         switch (mid->mid_state) {
2149         case MID_RESPONSE_RECEIVED:
2150                 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
2151                 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2152                 if (wdata->result != 0)
2153                         break;
2154
2155                 written = le32_to_cpu(rsp->DataLength);
2156                 /*
2157                  * Mask off high 16 bits when bytes written as returned
2158                  * by the server is greater than bytes requested by the
2159                  * client. OS/2 servers are known to set incorrect
2160                  * CountHigh values.
2161                  */
2162                 if (written > wdata->bytes)
2163                         written &= 0xFFFF;
2164
2165                 if (written < wdata->bytes)
2166                         wdata->result = -ENOSPC;
2167                 else
2168                         wdata->bytes = written;
2169                 break;
2170         case MID_REQUEST_SUBMITTED:
2171         case MID_RETRY_NEEDED:
2172                 wdata->result = -EAGAIN;
2173                 break;
2174         default:
2175                 wdata->result = -EIO;
2176                 break;
2177         }
2178
2179         if (wdata->result)
2180                 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2181
2182         queue_work(cifsiod_wq, &wdata->work);
2183         mutex_lock(&server->srv_mutex);
2184         DeleteMidQEntry(mid);
2185         mutex_unlock(&server->srv_mutex);
2186         add_credits(tcon->ses->server, credits_received, 0);
2187 }
2188
2189 /* smb2_async_writev - send an async write, and set up mid to handle result */
2190 int
2191 smb2_async_writev(struct cifs_writedata *wdata,
2192                   void (*release)(struct kref *kref))
2193 {
2194         int rc = -EACCES, flags = 0;
2195         struct smb2_write_req *req = NULL;
2196         struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2197         struct TCP_Server_Info *server = tcon->ses->server;
2198         struct kvec iov;
2199         struct smb_rqst rqst;
2200
2201         rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
2202         if (rc) {
2203                 if (rc == -EAGAIN && wdata->credits) {
2204                         /* credits was reset by reconnect */
2205                         wdata->credits = 0;
2206                         /* reduce in_flight value since we won't send the req */
2207                         spin_lock(&server->req_lock);
2208                         server->in_flight--;
2209                         spin_unlock(&server->req_lock);
2210                 }
2211                 goto async_writev_out;
2212         }
2213
2214         req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
2215
2216         req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2217         req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2218         req->WriteChannelInfoOffset = 0;
2219         req->WriteChannelInfoLength = 0;
2220         req->Channel = 0;
2221         req->Offset = cpu_to_le64(wdata->offset);
2222         /* 4 for rfc1002 length field */
2223         req->DataOffset = cpu_to_le16(
2224                                 offsetof(struct smb2_write_req, Buffer) - 4);
2225         req->RemainingBytes = 0;
2226
2227         /* 4 for rfc1002 length field and 1 for Buffer */
2228         iov.iov_len = get_rfc1002_length(req) + 4 - 1;
2229         iov.iov_base = req;
2230
2231         rqst.rq_iov = &iov;
2232         rqst.rq_nvec = 1;
2233         rqst.rq_pages = wdata->pages;
2234         rqst.rq_npages = wdata->nr_pages;
2235         rqst.rq_pagesz = wdata->pagesz;
2236         rqst.rq_tailsz = wdata->tailsz;
2237
2238         cifs_dbg(FYI, "async write at %llu %u bytes\n",
2239                  wdata->offset, wdata->bytes);
2240
2241         req->Length = cpu_to_le32(wdata->bytes);
2242
2243         inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2244
2245         if (wdata->credits) {
2246                 req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2247                                                     SMB2_MAX_BUFFER_SIZE));
2248                 spin_lock(&server->req_lock);
2249                 server->credits += wdata->credits -
2250                                         le16_to_cpu(req->hdr.CreditCharge);
2251                 spin_unlock(&server->req_lock);
2252                 wake_up(&server->request_q);
2253                 flags = CIFS_HAS_CREDITS;
2254         }
2255
2256         kref_get(&wdata->refcount);
2257         rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2258                              flags);
2259
2260         if (rc) {
2261                 kref_put(&wdata->refcount, release);
2262                 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2263         }
2264
2265 async_writev_out:
2266         cifs_small_buf_release(req);
2267         return rc;
2268 }
2269
2270 /*
2271  * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2272  * The length field from io_parms must be at least 1 and indicates a number of
2273  * elements with data to write that begins with position 1 in iov array. All
2274  * data length is specified by count.
2275  */
2276 int
2277 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2278            unsigned int *nbytes, struct kvec *iov, int n_vec)
2279 {
2280         int rc = 0;
2281         struct smb2_write_req *req = NULL;
2282         struct smb2_write_rsp *rsp = NULL;
2283         int resp_buftype;
2284         *nbytes = 0;
2285
2286         if (n_vec < 1)
2287                 return rc;
2288
2289         rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2290         if (rc)
2291                 return rc;
2292
2293         if (io_parms->tcon->ses->server == NULL)
2294                 return -ECONNABORTED;
2295
2296         req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2297
2298         req->PersistentFileId = io_parms->persistent_fid;
2299         req->VolatileFileId = io_parms->volatile_fid;
2300         req->WriteChannelInfoOffset = 0;
2301         req->WriteChannelInfoLength = 0;
2302         req->Channel = 0;
2303         req->Length = cpu_to_le32(io_parms->length);
2304         req->Offset = cpu_to_le64(io_parms->offset);
2305         /* 4 for rfc1002 length field */
2306         req->DataOffset = cpu_to_le16(
2307                                 offsetof(struct smb2_write_req, Buffer) - 4);
2308         req->RemainingBytes = 0;
2309
2310         iov[0].iov_base = (char *)req;
2311         /* 4 for rfc1002 length field and 1 for Buffer */
2312         iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2313
2314         /* length of entire message including data to be written */
2315         inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2316
2317         rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2318                           &resp_buftype, 0);
2319         rsp = (struct smb2_write_rsp *)iov[0].iov_base;
2320
2321         if (rc) {
2322                 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
2323                 cifs_dbg(VFS, "Send error in write = %d\n", rc);
2324         } else
2325                 *nbytes = le32_to_cpu(rsp->DataLength);
2326
2327         free_rsp_buf(resp_buftype, rsp);
2328         return rc;
2329 }
2330
2331 static unsigned int
2332 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2333 {
2334         int len;
2335         unsigned int entrycount = 0;
2336         unsigned int next_offset = 0;
2337         FILE_DIRECTORY_INFO *entryptr;
2338
2339         if (bufstart == NULL)
2340                 return 0;
2341
2342         entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2343
2344         while (1) {
2345                 entryptr = (FILE_DIRECTORY_INFO *)
2346                                         ((char *)entryptr + next_offset);
2347
2348                 if ((char *)entryptr + size > end_of_buf) {
2349                         cifs_dbg(VFS, "malformed search entry would overflow\n");
2350                         break;
2351                 }
2352
2353                 len = le32_to_cpu(entryptr->FileNameLength);
2354                 if ((char *)entryptr + len + size > end_of_buf) {
2355                         cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2356                                  end_of_buf);
2357                         break;
2358                 }
2359
2360                 *lastentry = (char *)entryptr;
2361                 entrycount++;
2362
2363                 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2364                 if (!next_offset)
2365                         break;
2366         }
2367
2368         return entrycount;
2369 }
2370
2371 /*
2372  * Readdir/FindFirst
2373  */
2374 int
2375 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2376                      u64 persistent_fid, u64 volatile_fid, int index,
2377                      struct cifs_search_info *srch_inf)
2378 {
2379         struct smb2_query_directory_req *req;
2380         struct smb2_query_directory_rsp *rsp = NULL;
2381         struct kvec iov[2];
2382         int rc = 0;
2383         int len;
2384         int resp_buftype = CIFS_NO_BUFFER;
2385         unsigned char *bufptr;
2386         struct TCP_Server_Info *server;
2387         struct cifs_ses *ses = tcon->ses;
2388         __le16 asteriks = cpu_to_le16('*');
2389         char *end_of_smb;
2390         unsigned int output_size = CIFSMaxBufSize;
2391         size_t info_buf_size;
2392
2393         if (ses && (ses->server))
2394                 server = ses->server;
2395         else
2396                 return -EIO;
2397
2398         rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2399         if (rc)
2400                 return rc;
2401
2402         switch (srch_inf->info_level) {
2403         case SMB_FIND_FILE_DIRECTORY_INFO:
2404                 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2405                 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2406                 break;
2407         case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2408                 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2409                 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2410                 break;
2411         default:
2412                 cifs_dbg(VFS, "info level %u isn't supported\n",
2413                          srch_inf->info_level);
2414                 rc = -EINVAL;
2415                 goto qdir_exit;
2416         }
2417
2418         req->FileIndex = cpu_to_le32(index);
2419         req->PersistentFileId = persistent_fid;
2420         req->VolatileFileId = volatile_fid;
2421
2422         len = 0x2;
2423         bufptr = req->Buffer;
2424         memcpy(bufptr, &asteriks, len);
2425
2426         req->FileNameOffset =
2427                 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2428         req->FileNameLength = cpu_to_le16(len);
2429         /*
2430          * BB could be 30 bytes or so longer if we used SMB2 specific
2431          * buffer lengths, but this is safe and close enough.
2432          */
2433         output_size = min_t(unsigned int, output_size, server->maxBuf);
2434         output_size = min_t(unsigned int, output_size, 2 << 15);
2435         req->OutputBufferLength = cpu_to_le32(output_size);
2436
2437         iov[0].iov_base = (char *)req;
2438         /* 4 for RFC1001 length and 1 for Buffer */
2439         iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2440
2441         iov[1].iov_base = (char *)(req->Buffer);
2442         iov[1].iov_len = len;
2443
2444         inc_rfc1001_len(req, len - 1 /* Buffer */);
2445
2446         rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
2447         rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2448
2449         if (rc) {
2450                 if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2451                         srch_inf->endOfSearch = true;
2452                         rc = 0;
2453                 }
2454                 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2455                 goto qdir_exit;
2456         }
2457
2458         rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2459                           le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2460                           info_buf_size);
2461         if (rc)
2462                 goto qdir_exit;
2463
2464         srch_inf->unicode = true;
2465
2466         if (srch_inf->ntwrk_buf_start) {
2467                 if (srch_inf->smallBuf)
2468                         cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2469                 else
2470                         cifs_buf_release(srch_inf->ntwrk_buf_start);
2471         }
2472         srch_inf->ntwrk_buf_start = (char *)rsp;
2473         srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2474                 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2475         /* 4 for rfc1002 length field */
2476         end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2477         srch_inf->entries_in_buffer =
2478                         num_entries(srch_inf->srch_entries_start, end_of_smb,
2479                                     &srch_inf->last_entry, info_buf_size);
2480         srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
2481         cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2482                  srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2483                  srch_inf->srch_entries_start, srch_inf->last_entry);
2484         if (resp_buftype == CIFS_LARGE_BUFFER)
2485                 srch_inf->smallBuf = false;
2486         else if (resp_buftype == CIFS_SMALL_BUFFER)
2487                 srch_inf->smallBuf = true;
2488         else
2489                 cifs_dbg(VFS, "illegal search buffer type\n");
2490
2491         return rc;
2492
2493 qdir_exit:
2494         free_rsp_buf(resp_buftype, rsp);
2495         return rc;
2496 }
2497
2498 static int
2499 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2500                u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
2501                unsigned int num, void **data, unsigned int *size)
2502 {
2503         struct smb2_set_info_req *req;
2504         struct smb2_set_info_rsp *rsp = NULL;
2505         struct kvec *iov;
2506         int rc = 0;
2507         int resp_buftype;
2508         unsigned int i;
2509         struct TCP_Server_Info *server;
2510         struct cifs_ses *ses = tcon->ses;
2511
2512         if (ses && (ses->server))
2513                 server = ses->server;
2514         else
2515                 return -EIO;
2516
2517         if (!num)
2518                 return -EINVAL;
2519
2520         iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2521         if (!iov)
2522                 return -ENOMEM;
2523
2524         rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2525         if (rc) {
2526                 kfree(iov);
2527                 return rc;
2528         }
2529
2530         req->hdr.ProcessId = cpu_to_le32(pid);
2531
2532         req->InfoType = SMB2_O_INFO_FILE;
2533         req->FileInfoClass = info_class;
2534         req->PersistentFileId = persistent_fid;
2535         req->VolatileFileId = volatile_fid;
2536
2537         /* 4 for RFC1001 length and 1 for Buffer */
2538         req->BufferOffset =
2539                         cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2540         req->BufferLength = cpu_to_le32(*size);
2541
2542         inc_rfc1001_len(req, *size - 1 /* Buffer */);
2543
2544         memcpy(req->Buffer, *data, *size);
2545
2546         iov[0].iov_base = (char *)req;
2547         /* 4 for RFC1001 length */
2548         iov[0].iov_len = get_rfc1002_length(req) + 4;
2549
2550         for (i = 1; i < num; i++) {
2551                 inc_rfc1001_len(req, size[i]);
2552                 le32_add_cpu(&req->BufferLength, size[i]);
2553                 iov[i].iov_base = (char *)data[i];
2554                 iov[i].iov_len = size[i];
2555         }
2556
2557         rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2558         rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2559
2560         if (rc != 0)
2561                 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
2562
2563         free_rsp_buf(resp_buftype, rsp);
2564         kfree(iov);
2565         return rc;
2566 }
2567
2568 int
2569 SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2570             u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2571 {
2572         struct smb2_file_rename_info info;
2573         void **data;
2574         unsigned int size[2];
2575         int rc;
2576         int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2577
2578         data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2579         if (!data)
2580                 return -ENOMEM;
2581
2582         info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2583                               /* 0 = fail if target already exists */
2584         info.RootDirectory = 0;  /* MBZ for network ops (why does spec say?) */
2585         info.FileNameLength = cpu_to_le32(len);
2586
2587         data[0] = &info;
2588         size[0] = sizeof(struct smb2_file_rename_info);
2589
2590         data[1] = target_file;
2591         size[1] = len + 2 /* null */;
2592
2593         rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2594                            current->tgid, FILE_RENAME_INFORMATION, 2, data,
2595                            size);
2596         kfree(data);
2597         return rc;
2598 }
2599
2600 int
2601 SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2602                   u64 persistent_fid, u64 volatile_fid)
2603 {
2604         __u8 delete_pending = 1;
2605         void *data;
2606         unsigned int size;
2607
2608         data = &delete_pending;
2609         size = 1; /* sizeof __u8 */
2610
2611         return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2612                         current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2613                         &size);
2614 }
2615
2616 int
2617 SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2618                   u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2619 {
2620         struct smb2_file_link_info info;
2621         void **data;
2622         unsigned int size[2];
2623         int rc;
2624         int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2625
2626         data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2627         if (!data)
2628                 return -ENOMEM;
2629
2630         info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2631                               /* 0 = fail if link already exists */
2632         info.RootDirectory = 0;  /* MBZ for network ops (why does spec say?) */
2633         info.FileNameLength = cpu_to_le32(len);
2634
2635         data[0] = &info;
2636         size[0] = sizeof(struct smb2_file_link_info);
2637
2638         data[1] = target_file;
2639         size[1] = len + 2 /* null */;
2640
2641         rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2642                            current->tgid, FILE_LINK_INFORMATION, 2, data, size);
2643         kfree(data);
2644         return rc;
2645 }
2646
2647 int
2648 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2649              u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
2650 {
2651         struct smb2_file_eof_info info;
2652         void *data;
2653         unsigned int size;
2654
2655         info.EndOfFile = *eof;
2656
2657         data = &info;
2658         size = sizeof(struct smb2_file_eof_info);
2659
2660         if (is_falloc)
2661                 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2662                         pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2663         else
2664                 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2665                         pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
2666 }
2667
2668 int
2669 SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2670               u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2671 {
2672         unsigned int size;
2673         size = sizeof(FILE_BASIC_INFO);
2674         return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2675                              current->tgid, FILE_BASIC_INFORMATION, 1,
2676                              (void **)&buf, &size);
2677 }
2678
2679 int
2680 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2681                   const u64 persistent_fid, const u64 volatile_fid,
2682                   __u8 oplock_level)
2683 {
2684         int rc;
2685         struct smb2_oplock_break *req = NULL;
2686
2687         cifs_dbg(FYI, "SMB2_oplock_break\n");
2688         rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2689
2690         if (rc)
2691                 return rc;
2692
2693         req->VolatileFid = volatile_fid;
2694         req->PersistentFid = persistent_fid;
2695         req->OplockLevel = oplock_level;
2696         req->hdr.CreditRequest = cpu_to_le16(1);
2697
2698         rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2699         /* SMB2 buffer freed by function above */
2700
2701         if (rc) {
2702                 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
2703                 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
2704         }
2705
2706         return rc;
2707 }
2708
2709 static void
2710 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2711                         struct kstatfs *kst)
2712 {
2713         kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2714                           le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2715         kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
2716         kst->f_bfree  = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
2717         kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
2718         return;
2719 }
2720
2721 static int
2722 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2723                    int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2724 {
2725         int rc;
2726         struct smb2_query_info_req *req;
2727
2728         cifs_dbg(FYI, "Query FSInfo level %d\n", level);
2729
2730         if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2731                 return -EIO;
2732
2733         rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2734         if (rc)
2735                 return rc;
2736
2737         req->InfoType = SMB2_O_INFO_FILESYSTEM;
2738         req->FileInfoClass = level;
2739         req->PersistentFileId = persistent_fid;
2740         req->VolatileFileId = volatile_fid;
2741         /* 4 for rfc1002 length field and 1 for pad */
2742         req->InputBufferOffset =
2743                         cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2744         req->OutputBufferLength = cpu_to_le32(
2745                 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2746
2747         iov->iov_base = (char *)req;
2748         /* 4 for rfc1002 length field */
2749         iov->iov_len = get_rfc1002_length(req) + 4;
2750         return 0;
2751 }
2752
2753 int
2754 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2755               u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2756 {
2757         struct smb2_query_info_rsp *rsp = NULL;
2758         struct kvec iov;
2759         int rc = 0;
2760         int resp_buftype;
2761         struct cifs_ses *ses = tcon->ses;
2762         struct smb2_fs_full_size_info *info = NULL;
2763
2764         rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
2765                                 sizeof(struct smb2_fs_full_size_info),
2766                                 persistent_fid, volatile_fid);
2767         if (rc)
2768                 return rc;
2769
2770         rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2771         if (rc) {
2772                 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2773                 goto qfsinf_exit;
2774         }
2775         rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2776
2777         info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
2778                 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
2779         rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2780                           le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2781                           sizeof(struct smb2_fs_full_size_info));
2782         if (!rc)
2783                 copy_fs_info_to_kstatfs(info, fsdata);
2784
2785 qfsinf_exit:
2786         free_rsp_buf(resp_buftype, iov.iov_base);
2787         return rc;
2788 }
2789
2790 int
2791 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
2792               u64 persistent_fid, u64 volatile_fid, int level)
2793 {
2794         struct smb2_query_info_rsp *rsp = NULL;
2795         struct kvec iov;
2796         int rc = 0;
2797         int resp_buftype, max_len, min_len;
2798         struct cifs_ses *ses = tcon->ses;
2799         unsigned int rsp_len, offset;
2800
2801         if (level == FS_DEVICE_INFORMATION) {
2802                 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
2803                 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
2804         } else if (level == FS_ATTRIBUTE_INFORMATION) {
2805                 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
2806                 min_len = MIN_FS_ATTR_INFO_SIZE;
2807         } else if (level == FS_SECTOR_SIZE_INFORMATION) {
2808                 max_len = sizeof(struct smb3_fs_ss_info);
2809                 min_len = sizeof(struct smb3_fs_ss_info);
2810         } else {
2811                 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
2812                 return -EINVAL;
2813         }
2814
2815         rc = build_qfs_info_req(&iov, tcon, level, max_len,
2816                                 persistent_fid, volatile_fid);
2817         if (rc)
2818                 return rc;
2819
2820         rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2821         if (rc) {
2822                 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2823                 goto qfsattr_exit;
2824         }
2825         rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2826
2827         rsp_len = le32_to_cpu(rsp->OutputBufferLength);
2828         offset = le16_to_cpu(rsp->OutputBufferOffset);
2829         rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
2830         if (rc)
2831                 goto qfsattr_exit;
2832
2833         if (level == FS_ATTRIBUTE_INFORMATION)
2834                 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
2835                         + (char *)&rsp->hdr, min_t(unsigned int,
2836                         rsp_len, max_len));
2837         else if (level == FS_DEVICE_INFORMATION)
2838                 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
2839                         + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
2840         else if (level == FS_SECTOR_SIZE_INFORMATION) {
2841                 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
2842                         (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
2843                 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
2844                 tcon->perf_sector_size =
2845                         le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
2846         }
2847
2848 qfsattr_exit:
2849         free_rsp_buf(resp_buftype, iov.iov_base);
2850         return rc;
2851 }
2852
2853 int
2854 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2855            const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2856            const __u32 num_lock, struct smb2_lock_element *buf)
2857 {
2858         int rc = 0;
2859         struct smb2_lock_req *req = NULL;
2860         struct kvec iov[2];
2861         int resp_buf_type;
2862         unsigned int count;
2863
2864         cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
2865
2866         rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
2867         if (rc)
2868                 return rc;
2869
2870         req->hdr.ProcessId = cpu_to_le32(pid);
2871         req->LockCount = cpu_to_le16(num_lock);
2872
2873         req->PersistentFileId = persist_fid;
2874         req->VolatileFileId = volatile_fid;
2875
2876         count = num_lock * sizeof(struct smb2_lock_element);
2877         inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
2878
2879         iov[0].iov_base = (char *)req;
2880         /* 4 for rfc1002 length field and count for all locks */
2881         iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
2882         iov[1].iov_base = (char *)buf;
2883         iov[1].iov_len = count;
2884
2885         cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2886         rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2887         if (rc) {
2888                 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
2889                 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
2890         }
2891
2892         return rc;
2893 }
2894
2895 int
2896 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
2897           const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2898           const __u64 length, const __u64 offset, const __u32 lock_flags,
2899           const bool wait)
2900 {
2901         struct smb2_lock_element lock;
2902
2903         lock.Offset = cpu_to_le64(offset);
2904         lock.Length = cpu_to_le64(length);
2905         lock.Flags = cpu_to_le32(lock_flags);
2906         if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
2907                 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
2908
2909         return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
2910 }
2911
2912 int
2913 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
2914                  __u8 *lease_key, const __le32 lease_state)
2915 {
2916         int rc;
2917         struct smb2_lease_ack *req = NULL;
2918
2919         cifs_dbg(FYI, "SMB2_lease_break\n");
2920         rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2921
2922         if (rc)
2923                 return rc;
2924
2925         req->hdr.CreditRequest = cpu_to_le16(1);
2926         req->StructureSize = cpu_to_le16(36);
2927         inc_rfc1001_len(req, 12);
2928
2929         memcpy(req->LeaseKey, lease_key, 16);
2930         req->LeaseState = lease_state;
2931
2932         rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2933         /* SMB2 buffer freed by function above */
2934
2935         if (rc) {
2936                 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
2937                 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
2938         }
2939
2940         return rc;
2941 }