~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/cifs/smb2pdu.c

Version: ~ [ linux-5.10-rc5 ] ~ [ linux-5.9.10 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.79 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.159 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.208 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.245 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.245 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp