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