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