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