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