cifs: turn the pages list in cifs_readdata into an array
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / cifs / smb2pdu.c
CommitLineData
ec2e4523
PS
1/*
2 * fs/cifs/smb2pdu.c
3 *
4 * Copyright (C) International Business Machines Corp., 2009, 2011
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
111 hdr->TreeId = tcon->tid;
112 /* Uid is not converted */
113 if (tcon->ses)
114 hdr->SessionId = tcon->ses->Suid;
115 /* BB check following DFS flags BB */
116 /* BB do we have to add check for SHI1005_FLAGS_DFS_ROOT too? */
faaf946a
PS
117 if (tcon->share_flags & SHI1005_FLAGS_DFS)
118 hdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS;
ec2e4523
PS
119 /* BB how does SMB2 do case sensitive? */
120 /* if (tcon->nocase)
121 hdr->Flags |= SMBFLG_CASELESS; */
3c1bf7e4 122 if (tcon->ses && tcon->ses->server &&
ec2e4523 123 (tcon->ses->server->sec_mode & SECMODE_SIGN_REQUIRED))
3c1bf7e4 124 hdr->Flags |= SMB2_FLAGS_SIGNED;
ec2e4523
PS
125out:
126 pdu->StructureSize2 = cpu_to_le16(parmsize);
127 return;
128}
129
130static int
131smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
132{
133 int rc = 0;
aa24d1e9
PS
134 struct nls_table *nls_codepage;
135 struct cifs_ses *ses;
136 struct TCP_Server_Info *server;
137
138 /*
139 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
140 * check for tcp and smb session status done differently
141 * for those three - in the calling routine.
142 */
143 if (tcon == NULL)
144 return rc;
145
146 if (smb2_command == SMB2_TREE_CONNECT)
147 return rc;
148
149 if (tcon->tidStatus == CifsExiting) {
150 /*
151 * only tree disconnect, open, and write,
152 * (and ulogoff which does not have tcon)
153 * are allowed as we start force umount.
154 */
155 if ((smb2_command != SMB2_WRITE) &&
156 (smb2_command != SMB2_CREATE) &&
157 (smb2_command != SMB2_TREE_DISCONNECT)) {
158 cFYI(1, "can not send cmd %d while umounting",
159 smb2_command);
160 return -ENODEV;
161 }
162 }
163 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
164 (!tcon->ses->server))
165 return -EIO;
166
167 ses = tcon->ses;
168 server = ses->server;
169
170 /*
171 * Give demultiplex thread up to 10 seconds to reconnect, should be
172 * greater than cifs socket timeout which is 7 seconds
173 */
174 while (server->tcpStatus == CifsNeedReconnect) {
175 /*
176 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
177 * here since they are implicitly done when session drops.
178 */
179 switch (smb2_command) {
180 /*
181 * BB Should we keep oplock break and add flush to exceptions?
182 */
183 case SMB2_TREE_DISCONNECT:
184 case SMB2_CANCEL:
185 case SMB2_CLOSE:
186 case SMB2_OPLOCK_BREAK:
187 return -EAGAIN;
188 }
189
190 wait_event_interruptible_timeout(server->response_q,
191 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
192
193 /* are we still trying to reconnect? */
194 if (server->tcpStatus != CifsNeedReconnect)
195 break;
196
197 /*
198 * on "soft" mounts we wait once. Hard mounts keep
199 * retrying until process is killed or server comes
200 * back on-line
201 */
202 if (!tcon->retry) {
203 cFYI(1, "gave up waiting on reconnect in smb_init");
204 return -EHOSTDOWN;
205 }
206 }
207
208 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
209 return rc;
210
211 nls_codepage = load_nls_default();
212
213 /*
214 * need to prevent multiple threads trying to simultaneously reconnect
215 * the same SMB session
216 */
217 mutex_lock(&tcon->ses->session_mutex);
218 rc = cifs_negotiate_protocol(0, tcon->ses);
219 if (!rc && tcon->ses->need_reconnect)
220 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
221
222 if (rc || !tcon->need_reconnect) {
223 mutex_unlock(&tcon->ses->session_mutex);
224 goto out;
225 }
226
227 cifs_mark_open_files_invalid(tcon);
228 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
229 mutex_unlock(&tcon->ses->session_mutex);
230 cFYI(1, "reconnect tcon rc = %d", rc);
231 if (rc)
232 goto out;
233 atomic_inc(&tconInfoReconnectCount);
234 /*
235 * BB FIXME add code to check if wsize needs update due to negotiated
236 * smb buffer size shrinking.
237 */
238out:
239 /*
240 * Check if handle based operation so we know whether we can continue
241 * or not without returning to caller to reset file handle.
242 */
243 /*
244 * BB Is flush done by server on drop of tcp session? Should we special
245 * case it and skip above?
246 */
247 switch (smb2_command) {
248 case SMB2_FLUSH:
249 case SMB2_READ:
250 case SMB2_WRITE:
251 case SMB2_LOCK:
252 case SMB2_IOCTL:
253 case SMB2_QUERY_DIRECTORY:
254 case SMB2_CHANGE_NOTIFY:
255 case SMB2_QUERY_INFO:
256 case SMB2_SET_INFO:
257 return -EAGAIN;
258 }
259 unload_nls(nls_codepage);
ec2e4523
PS
260 return rc;
261}
262
263/*
264 * Allocate and return pointer to an SMB request hdr, and set basic
265 * SMB information in the SMB header. If the return code is zero, this
266 * function must have filled in request_buf pointer.
267 */
268static int
269small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
270 void **request_buf)
271{
272 int rc = 0;
273
274 rc = smb2_reconnect(smb2_command, tcon);
275 if (rc)
276 return rc;
277
278 /* BB eventually switch this to SMB2 specific small buf size */
279 *request_buf = cifs_small_buf_get();
280 if (*request_buf == NULL) {
281 /* BB should we add a retry in here if not a writepage? */
282 return -ENOMEM;
283 }
284
285 smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
286
287 if (tcon != NULL) {
288#ifdef CONFIG_CIFS_STATS2
ec2e4523
PS
289 uint16_t com_code = le16_to_cpu(smb2_command);
290 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
ec2e4523
PS
291#endif
292 cifs_stats_inc(&tcon->num_smbs_sent);
293 }
294
295 return rc;
296}
297
298static void
299free_rsp_buf(int resp_buftype, void *rsp)
300{
301 if (resp_buftype == CIFS_SMALL_BUFFER)
302 cifs_small_buf_release(rsp);
303 else if (resp_buftype == CIFS_LARGE_BUFFER)
304 cifs_buf_release(rsp);
305}
306
307#define SMB2_NUM_PROT 1
308
309#define SMB2_PROT 0
310#define SMB21_PROT 1
311#define BAD_PROT 0xFFFF
312
313#define SMB2_PROT_ID 0x0202
314#define SMB21_PROT_ID 0x0210
315#define BAD_PROT_ID 0xFFFF
316
317static struct {
318 int index;
319 __le16 name;
320} smb2protocols[] = {
321 {SMB2_PROT, cpu_to_le16(SMB2_PROT_ID)},
322 {SMB21_PROT, cpu_to_le16(SMB21_PROT_ID)},
323 {BAD_PROT, cpu_to_le16(BAD_PROT_ID)}
324};
325
326/*
327 *
328 * SMB2 Worker functions follow:
329 *
330 * The general structure of the worker functions is:
331 * 1) Call smb2_init (assembles SMB2 header)
332 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
333 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
334 * 4) Decode SMB2 command specific fields in the fixed length area
335 * 5) Decode variable length data area (if any for this SMB2 command type)
336 * 6) Call free smb buffer
337 * 7) return
338 *
339 */
340
341int
342SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
343{
344 struct smb2_negotiate_req *req;
345 struct smb2_negotiate_rsp *rsp;
346 struct kvec iov[1];
347 int rc = 0;
348 int resp_buftype;
349 struct TCP_Server_Info *server;
350 unsigned int sec_flags;
351 u16 i;
352 u16 temp = 0;
353 int blob_offset, blob_length;
354 char *security_blob;
355 int flags = CIFS_NEG_OP;
356
357 cFYI(1, "Negotiate protocol");
358
359 if (ses->server)
360 server = ses->server;
361 else {
362 rc = -EIO;
363 return rc;
364 }
365
366 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
367 if (rc)
368 return rc;
369
370 /* if any of auth flags (ie not sign or seal) are overriden use them */
371 if (ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
372 sec_flags = ses->overrideSecFlg; /* BB FIXME fix sign flags?*/
373 else /* if override flags set only sign/seal OR them with global auth */
374 sec_flags = global_secflags | ses->overrideSecFlg;
375
376 cFYI(1, "sec_flags 0x%x", sec_flags);
377
378 req->hdr.SessionId = 0;
379
380 for (i = 0; i < SMB2_NUM_PROT; i++)
381 req->Dialects[i] = smb2protocols[i].name;
382
383 req->DialectCount = cpu_to_le16(i);
384 inc_rfc1001_len(req, i * 2);
385
386 /* only one of SMB2 signing flags may be set in SMB2 request */
387 if ((sec_flags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN)
388 temp = SMB2_NEGOTIATE_SIGNING_REQUIRED;
389 else if (sec_flags & CIFSSEC_MAY_SIGN) /* MAY_SIGN is a single flag */
390 temp = SMB2_NEGOTIATE_SIGNING_ENABLED;
391
392 req->SecurityMode = cpu_to_le16(temp);
393
394 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
395
396 iov[0].iov_base = (char *)req;
397 /* 4 for rfc1002 length field */
398 iov[0].iov_len = get_rfc1002_length(req) + 4;
399
400 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
401
402 rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
403 /*
404 * No tcon so can't do
405 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
406 */
407 if (rc != 0)
408 goto neg_exit;
409
410 if (rsp == NULL) {
411 rc = -EIO;
412 goto neg_exit;
413 }
414
415 cFYI(1, "mode 0x%x", rsp->SecurityMode);
416
417 if (rsp->DialectRevision == smb2protocols[SMB21_PROT].name)
418 cFYI(1, "negotiated smb2.1 dialect");
419 else if (rsp->DialectRevision == smb2protocols[SMB2_PROT].name)
420 cFYI(1, "negotiated smb2 dialect");
421 else {
422 cERROR(1, "Illegal dialect returned by server %d",
423 le16_to_cpu(rsp->DialectRevision));
424 rc = -EIO;
425 goto neg_exit;
426 }
427 server->dialect = le16_to_cpu(rsp->DialectRevision);
428
429 server->maxBuf = le32_to_cpu(rsp->MaxTransactSize);
430 server->max_read = le32_to_cpu(rsp->MaxReadSize);
431 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
432 /* BB Do we need to validate the SecurityMode? */
433 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
434 server->capabilities = le32_to_cpu(rsp->Capabilities);
29e20f9c
PS
435 /* Internal types */
436 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
ec2e4523
PS
437
438 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
439 &rsp->hdr);
440 if (blob_length == 0) {
441 cERROR(1, "missing security blob on negprot");
442 rc = -EIO;
443 goto neg_exit;
444 }
3c1bf7e4
PS
445
446 cFYI(1, "sec_flags 0x%x", sec_flags);
447 if (sec_flags & CIFSSEC_MUST_SIGN) {
448 cFYI(1, "Signing required");
449 if (!(server->sec_mode & (SMB2_NEGOTIATE_SIGNING_REQUIRED |
450 SMB2_NEGOTIATE_SIGNING_ENABLED))) {
451 cERROR(1, "signing required but server lacks support");
452 rc = -EOPNOTSUPP;
453 goto neg_exit;
454 }
455 server->sec_mode |= SECMODE_SIGN_REQUIRED;
456 } else if (sec_flags & CIFSSEC_MAY_SIGN) {
457 cFYI(1, "Signing optional");
458 if (server->sec_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
459 cFYI(1, "Server requires signing");
460 server->sec_mode |= SECMODE_SIGN_REQUIRED;
461 } else {
462 server->sec_mode &=
463 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
464 }
465 } else {
466 cFYI(1, "Signing disabled");
467 if (server->sec_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
468 cERROR(1, "Server requires packet signing to be enabled"
469 " in /proc/fs/cifs/SecurityFlags.");
470 rc = -EOPNOTSUPP;
471 goto neg_exit;
472 }
473 server->sec_mode &=
474 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
475 }
476
ec2e4523
PS
477#ifdef CONFIG_SMB2_ASN1 /* BB REMOVEME when updated asn1.c ready */
478 rc = decode_neg_token_init(security_blob, blob_length,
479 &server->sec_type);
480 if (rc == 1)
481 rc = 0;
482 else if (rc == 0) {
483 rc = -EIO;
484 goto neg_exit;
485 }
486#endif
487
488neg_exit:
489 free_rsp_buf(resp_buftype, rsp);
490 return rc;
491}
5478f9ba
PS
492
493int
494SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
495 const struct nls_table *nls_cp)
496{
497 struct smb2_sess_setup_req *req;
498 struct smb2_sess_setup_rsp *rsp = NULL;
499 struct kvec iov[2];
500 int rc = 0;
501 int resp_buftype;
502 __le32 phase = NtLmNegotiate; /* NTLMSSP, if needed, is multistage */
503 struct TCP_Server_Info *server;
504 unsigned int sec_flags;
505 u8 temp = 0;
506 u16 blob_length = 0;
507 char *security_blob;
508 char *ntlmssp_blob = NULL;
509 bool use_spnego = false; /* else use raw ntlmssp */
510
511 cFYI(1, "Session Setup");
512
513 if (ses->server)
514 server = ses->server;
515 else {
516 rc = -EIO;
517 return rc;
518 }
519
520 /*
521 * If memory allocation is successful, caller of this function
522 * frees it.
523 */
524 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
525 if (!ses->ntlmssp)
526 return -ENOMEM;
527
528 ses->server->secType = RawNTLMSSP;
529
530ssetup_ntlmssp_authenticate:
531 if (phase == NtLmChallenge)
532 phase = NtLmAuthenticate; /* if ntlmssp, now final phase */
533
534 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
535 if (rc)
536 return rc;
537
538 /* if any of auth flags (ie not sign or seal) are overriden use them */
539 if (ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
540 sec_flags = ses->overrideSecFlg; /* BB FIXME fix sign flags?*/
541 else /* if override flags set only sign/seal OR them with global auth */
542 sec_flags = global_secflags | ses->overrideSecFlg;
543
544 cFYI(1, "sec_flags 0x%x", sec_flags);
545
546 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
547 req->VcNumber = 0; /* MBZ */
548 /* to enable echos and oplocks */
549 req->hdr.CreditRequest = cpu_to_le16(3);
550
551 /* only one of SMB2 signing flags may be set in SMB2 request */
552 if ((sec_flags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN)
553 temp = SMB2_NEGOTIATE_SIGNING_REQUIRED;
554 else if (ses->server->sec_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED)
555 temp = SMB2_NEGOTIATE_SIGNING_REQUIRED;
556 else if (sec_flags & CIFSSEC_MAY_SIGN) /* MAY_SIGN is a single flag */
557 temp = SMB2_NEGOTIATE_SIGNING_ENABLED;
558
559 req->SecurityMode = temp;
560 req->Capabilities = 0;
561 req->Channel = 0; /* MBZ */
562
563 iov[0].iov_base = (char *)req;
564 /* 4 for rfc1002 length field and 1 for pad */
565 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
566 if (phase == NtLmNegotiate) {
567 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
568 GFP_KERNEL);
569 if (ntlmssp_blob == NULL) {
570 rc = -ENOMEM;
571 goto ssetup_exit;
572 }
573 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
574 if (use_spnego) {
575 /* blob_length = build_spnego_ntlmssp_blob(
576 &security_blob,
577 sizeof(struct _NEGOTIATE_MESSAGE),
578 ntlmssp_blob); */
579 /* BB eventually need to add this */
580 cERROR(1, "spnego not supported for SMB2 yet");
581 rc = -EOPNOTSUPP;
582 kfree(ntlmssp_blob);
583 goto ssetup_exit;
584 } else {
585 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
586 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
587 security_blob = ntlmssp_blob;
588 }
589 } else if (phase == NtLmAuthenticate) {
590 req->hdr.SessionId = ses->Suid;
591 ntlmssp_blob = kzalloc(sizeof(struct _NEGOTIATE_MESSAGE) + 500,
592 GFP_KERNEL);
593 if (ntlmssp_blob == NULL) {
594 cERROR(1, "failed to malloc ntlmssp blob");
595 rc = -ENOMEM;
596 goto ssetup_exit;
597 }
598 rc = build_ntlmssp_auth_blob(ntlmssp_blob, &blob_length, ses,
599 nls_cp);
600 if (rc) {
601 cFYI(1, "build_ntlmssp_auth_blob failed %d", rc);
602 goto ssetup_exit; /* BB double check error handling */
603 }
604 if (use_spnego) {
605 /* blob_length = build_spnego_ntlmssp_blob(
606 &security_blob,
607 blob_length,
608 ntlmssp_blob); */
609 cERROR(1, "spnego not supported for SMB2 yet");
610 rc = -EOPNOTSUPP;
611 kfree(ntlmssp_blob);
612 goto ssetup_exit;
613 } else {
614 security_blob = ntlmssp_blob;
615 }
616 } else {
617 cERROR(1, "illegal ntlmssp phase");
618 rc = -EIO;
619 goto ssetup_exit;
620 }
621
622 /* Testing shows that buffer offset must be at location of Buffer[0] */
623 req->SecurityBufferOffset =
624 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
625 1 /* pad */ - 4 /* rfc1001 len */);
626 req->SecurityBufferLength = cpu_to_le16(blob_length);
627 iov[1].iov_base = security_blob;
628 iov[1].iov_len = blob_length;
629
630 inc_rfc1001_len(req, blob_length - 1 /* pad */);
631
632 /* BB add code to build os and lm fields */
633
634 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, CIFS_LOG_ERROR);
635
636 kfree(security_blob);
637 rsp = (struct smb2_sess_setup_rsp *)iov[0].iov_base;
638 if (rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) {
639 if (phase != NtLmNegotiate) {
640 cERROR(1, "Unexpected more processing error");
641 goto ssetup_exit;
642 }
643 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
644 le16_to_cpu(rsp->SecurityBufferOffset)) {
645 cERROR(1, "Invalid security buffer offset %d",
646 le16_to_cpu(rsp->SecurityBufferOffset));
647 rc = -EIO;
648 goto ssetup_exit;
649 }
650
651 /* NTLMSSP Negotiate sent now processing challenge (response) */
652 phase = NtLmChallenge; /* process ntlmssp challenge */
653 rc = 0; /* MORE_PROCESSING is not an error here but expected */
654 ses->Suid = rsp->hdr.SessionId;
655 rc = decode_ntlmssp_challenge(rsp->Buffer,
656 le16_to_cpu(rsp->SecurityBufferLength), ses);
657 }
658
659 /*
660 * BB eventually add code for SPNEGO decoding of NtlmChallenge blob,
661 * but at least the raw NTLMSSP case works.
662 */
663 /*
664 * No tcon so can't do
665 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
666 */
667 if (rc != 0)
668 goto ssetup_exit;
669
670 if (rsp == NULL) {
671 rc = -EIO;
672 goto ssetup_exit;
673 }
674
675 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
676ssetup_exit:
677 free_rsp_buf(resp_buftype, rsp);
678
679 /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
680 if ((phase == NtLmChallenge) && (rc == 0))
681 goto ssetup_ntlmssp_authenticate;
682 return rc;
683}
684
685int
686SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
687{
688 struct smb2_logoff_req *req; /* response is also trivial struct */
689 int rc = 0;
690 struct TCP_Server_Info *server;
691
692 cFYI(1, "disconnect session %p", ses);
693
694 if (ses && (ses->server))
695 server = ses->server;
696 else
697 return -EIO;
698
699 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
700 if (rc)
701 return rc;
702
703 /* since no tcon, smb2_init can not do this, so do here */
704 req->hdr.SessionId = ses->Suid;
3c1bf7e4
PS
705 if (server->sec_mode & SECMODE_SIGN_REQUIRED)
706 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
5478f9ba
PS
707
708 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
709 /*
710 * No tcon so can't do
711 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
712 */
713 return rc;
714}
faaf946a
PS
715
716static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
717{
d60622eb 718 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
faaf946a
PS
719}
720
721#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
722
723int
724SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
725 struct cifs_tcon *tcon, const struct nls_table *cp)
726{
727 struct smb2_tree_connect_req *req;
728 struct smb2_tree_connect_rsp *rsp = NULL;
729 struct kvec iov[2];
730 int rc = 0;
731 int resp_buftype;
732 int unc_path_len;
733 struct TCP_Server_Info *server;
734 __le16 *unc_path = NULL;
735
736 cFYI(1, "TCON");
737
738 if ((ses->server) && tree)
739 server = ses->server;
740 else
741 return -EIO;
742
743 if (tcon && tcon->bad_network_name)
744 return -ENOENT;
745
746 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
747 if (unc_path == NULL)
748 return -ENOMEM;
749
750 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
751 unc_path_len *= 2;
752 if (unc_path_len < 2) {
753 kfree(unc_path);
754 return -EINVAL;
755 }
756
757 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
758 if (rc) {
759 kfree(unc_path);
760 return rc;
761 }
762
763 if (tcon == NULL) {
764 /* since no tcon, smb2_init can not do this, so do here */
765 req->hdr.SessionId = ses->Suid;
766 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
767 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
768 }
769
770 iov[0].iov_base = (char *)req;
771 /* 4 for rfc1002 length field and 1 for pad */
772 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
773
774 /* Testing shows that buffer offset must be at location of Buffer[0] */
775 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
776 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
777 req->PathLength = cpu_to_le16(unc_path_len - 2);
778 iov[1].iov_base = unc_path;
779 iov[1].iov_len = unc_path_len;
780
781 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
782
783 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
784 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
785
786 if (rc != 0) {
787 if (tcon) {
788 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
789 tcon->need_reconnect = true;
790 }
791 goto tcon_error_exit;
792 }
793
794 if (rsp == NULL) {
795 rc = -EIO;
796 goto tcon_exit;
797 }
798
799 if (tcon == NULL) {
800 ses->ipc_tid = rsp->hdr.TreeId;
801 goto tcon_exit;
802 }
803
804 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
805 cFYI(1, "connection to disk share");
806 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
807 tcon->ipc = true;
808 cFYI(1, "connection to pipe share");
809 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
810 tcon->print = true;
811 cFYI(1, "connection to printer");
812 } else {
813 cERROR(1, "unknown share type %d", rsp->ShareType);
814 rc = -EOPNOTSUPP;
815 goto tcon_error_exit;
816 }
817
818 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
819 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
820 tcon->tidStatus = CifsGood;
821 tcon->need_reconnect = false;
822 tcon->tid = rsp->hdr.TreeId;
823 strncpy(tcon->treeName, tree, MAX_TREE_SIZE);
824
825 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
826 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
827 cERROR(1, "DFS capability contradicts DFS flag");
828
829tcon_exit:
830 free_rsp_buf(resp_buftype, rsp);
831 kfree(unc_path);
832 return rc;
833
834tcon_error_exit:
835 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
836 cERROR(1, "BAD_NETWORK_NAME: %s", tree);
837 tcon->bad_network_name = true;
838 }
839 goto tcon_exit;
840}
841
842int
843SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
844{
845 struct smb2_tree_disconnect_req *req; /* response is trivial */
846 int rc = 0;
847 struct TCP_Server_Info *server;
848 struct cifs_ses *ses = tcon->ses;
849
850 cFYI(1, "Tree Disconnect");
851
852 if (ses && (ses->server))
853 server = ses->server;
854 else
855 return -EIO;
856
857 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
858 return 0;
859
860 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
861 if (rc)
862 return rc;
863
864 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
865 if (rc)
866 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
867
868 return rc;
869}
2503a0db
PS
870
871int
872SMB2_open(const unsigned int xid, struct cifs_tcon *tcon, __le16 *path,
873 u64 *persistent_fid, u64 *volatile_fid, __u32 desired_access,
f0df737e 874 __u32 create_disposition, __u32 file_attributes, __u32 create_options,
2e44b288 875 __u8 *oplock, struct smb2_file_all_info *buf)
2503a0db
PS
876{
877 struct smb2_create_req *req;
878 struct smb2_create_rsp *rsp;
879 struct TCP_Server_Info *server;
880 struct cifs_ses *ses = tcon->ses;
881 struct kvec iov[2];
882 int resp_buftype;
883 int uni_path_len;
884 int rc = 0;
885 int num_iovecs = 2;
886
887 cFYI(1, "create/open");
888
889 if (ses && (ses->server))
890 server = ses->server;
891 else
892 return -EIO;
893
894 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
895 if (rc)
896 return rc;
897
2e44b288
PS
898 if (server->oplocks)
899 req->RequestedOplockLevel = *oplock;
900 else
2503a0db
PS
901 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
902 req->ImpersonationLevel = IL_IMPERSONATION;
903 req->DesiredAccess = cpu_to_le32(desired_access);
904 /* File attributes ignored on open (used in create though) */
905 req->FileAttributes = cpu_to_le32(file_attributes);
906 req->ShareAccess = FILE_SHARE_ALL_LE;
907 req->CreateDisposition = cpu_to_le32(create_disposition);
908 req->CreateOptions = cpu_to_le32(create_options);
909 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
910 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req)
911 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
912
913 iov[0].iov_base = (char *)req;
914 /* 4 for rfc1002 length field */
915 iov[0].iov_len = get_rfc1002_length(req) + 4;
916
917 /* MUST set path len (NameLength) to 0 opening root of share */
918 if (uni_path_len >= 4) {
919 req->NameLength = cpu_to_le16(uni_path_len - 2);
920 /* -1 since last byte is buf[0] which is sent below (path) */
921 iov[0].iov_len--;
922 iov[1].iov_len = uni_path_len;
923 iov[1].iov_base = path;
924 /*
925 * -1 since last byte is buf[0] which was counted in
926 * smb2_buf_len.
927 */
928 inc_rfc1001_len(req, uni_path_len - 1);
929 } else {
930 num_iovecs = 1;
931 req->NameLength = 0;
932 }
933
934 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
935 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
936
937 if (rc != 0) {
938 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
939 goto creat_exit;
940 }
941
942 if (rsp == NULL) {
943 rc = -EIO;
944 goto creat_exit;
945 }
946 *persistent_fid = rsp->PersistentFileId;
947 *volatile_fid = rsp->VolatileFileId;
f0df737e
PS
948
949 if (buf) {
950 memcpy(buf, &rsp->CreationTime, 32);
951 buf->AllocationSize = rsp->AllocationSize;
952 buf->EndOfFile = rsp->EndofFile;
953 buf->Attributes = rsp->FileAttributes;
954 buf->NumberOfLinks = cpu_to_le32(1);
955 buf->DeletePending = 0;
956 }
2e44b288
PS
957
958 *oplock = rsp->OplockLevel;
2503a0db
PS
959creat_exit:
960 free_rsp_buf(resp_buftype, rsp);
961 return rc;
962}
963
964int
965SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
966 u64 persistent_fid, u64 volatile_fid)
967{
968 struct smb2_close_req *req;
969 struct smb2_close_rsp *rsp;
970 struct TCP_Server_Info *server;
971 struct cifs_ses *ses = tcon->ses;
972 struct kvec iov[1];
973 int resp_buftype;
974 int rc = 0;
975
976 cFYI(1, "Close");
977
978 if (ses && (ses->server))
979 server = ses->server;
980 else
981 return -EIO;
982
983 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
984 if (rc)
985 return rc;
986
987 req->PersistentFileId = persistent_fid;
988 req->VolatileFileId = volatile_fid;
989
990 iov[0].iov_base = (char *)req;
991 /* 4 for rfc1002 length field */
992 iov[0].iov_len = get_rfc1002_length(req) + 4;
993
994 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
995 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
996
997 if (rc != 0) {
998 if (tcon)
999 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
1000 goto close_exit;
1001 }
1002
1003 if (rsp == NULL) {
1004 rc = -EIO;
1005 goto close_exit;
1006 }
1007
1008 /* BB FIXME - decode close response, update inode for caching */
1009
1010close_exit:
1011 free_rsp_buf(resp_buftype, rsp);
1012 return rc;
1013}
be4cb9e3
PS
1014
1015static int
1016validate_buf(unsigned int offset, unsigned int buffer_length,
1017 struct smb2_hdr *hdr, unsigned int min_buf_size)
1018
1019{
1020 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1021 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1022 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1023 char *end_of_buf = begin_of_buf + buffer_length;
1024
1025
1026 if (buffer_length < min_buf_size) {
1027 cERROR(1, "buffer length %d smaller than minimum size %d",
1028 buffer_length, min_buf_size);
1029 return -EINVAL;
1030 }
1031
1032 /* check if beyond RFC1001 maximum length */
1033 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
1034 cERROR(1, "buffer length %d or smb length %d too large",
1035 buffer_length, smb_len);
1036 return -EINVAL;
1037 }
1038
1039 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
1040 cERROR(1, "illegal server response, bad offset to data");
1041 return -EINVAL;
1042 }
1043
1044 return 0;
1045}
1046
1047/*
1048 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1049 * Caller must free buffer.
1050 */
1051static int
1052validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1053 struct smb2_hdr *hdr, unsigned int minbufsize,
1054 char *data)
1055
1056{
1057 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1058 int rc;
1059
1060 if (!data)
1061 return -EINVAL;
1062
1063 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1064 if (rc)
1065 return rc;
1066
1067 memcpy(data, begin_of_buf, buffer_length);
1068
1069 return 0;
1070}
1071
f0df737e
PS
1072static int
1073query_info(const unsigned int xid, struct cifs_tcon *tcon,
1074 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1075 size_t output_len, size_t min_len, void *data)
be4cb9e3
PS
1076{
1077 struct smb2_query_info_req *req;
1078 struct smb2_query_info_rsp *rsp = NULL;
1079 struct kvec iov[2];
1080 int rc = 0;
1081 int resp_buftype;
1082 struct TCP_Server_Info *server;
1083 struct cifs_ses *ses = tcon->ses;
1084
1085 cFYI(1, "Query Info");
1086
1087 if (ses && (ses->server))
1088 server = ses->server;
1089 else
1090 return -EIO;
1091
1092 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1093 if (rc)
1094 return rc;
1095
1096 req->InfoType = SMB2_O_INFO_FILE;
f0df737e 1097 req->FileInfoClass = info_class;
be4cb9e3
PS
1098 req->PersistentFileId = persistent_fid;
1099 req->VolatileFileId = volatile_fid;
1100 /* 4 for rfc1002 length field and 1 for Buffer */
1101 req->InputBufferOffset =
1102 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
f0df737e 1103 req->OutputBufferLength = cpu_to_le32(output_len);
be4cb9e3
PS
1104
1105 iov[0].iov_base = (char *)req;
1106 /* 4 for rfc1002 length field */
1107 iov[0].iov_len = get_rfc1002_length(req) + 4;
1108
1109 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1110 if (rc) {
1111 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1112 goto qinf_exit;
1113 }
1114
1115 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1116
1117 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1118 le32_to_cpu(rsp->OutputBufferLength),
f0df737e 1119 &rsp->hdr, min_len, data);
be4cb9e3
PS
1120
1121qinf_exit:
1122 free_rsp_buf(resp_buftype, rsp);
1123 return rc;
1124}
9094fad1 1125
f0df737e
PS
1126int
1127SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1128 u64 persistent_fid, u64 volatile_fid,
1129 struct smb2_file_all_info *data)
1130{
1131 return query_info(xid, tcon, persistent_fid, volatile_fid,
1132 FILE_ALL_INFORMATION,
1133 sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
1134 sizeof(struct smb2_file_all_info), data);
1135}
1136
1137int
1138SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1139 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1140{
1141 return query_info(xid, tcon, persistent_fid, volatile_fid,
1142 FILE_INTERNAL_INFORMATION,
1143 sizeof(struct smb2_file_internal_info),
1144 sizeof(struct smb2_file_internal_info), uniqueid);
1145}
1146
9094fad1
PS
1147/*
1148 * This is a no-op for now. We're not really interested in the reply, but
1149 * rather in the fact that the server sent one and that server->lstrp
1150 * gets updated.
1151 *
1152 * FIXME: maybe we should consider checking that the reply matches request?
1153 */
1154static void
1155smb2_echo_callback(struct mid_q_entry *mid)
1156{
1157 struct TCP_Server_Info *server = mid->callback_data;
1158 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1159 unsigned int credits_received = 1;
1160
1161 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1162 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1163
1164 DeleteMidQEntry(mid);
1165 add_credits(server, credits_received, CIFS_ECHO_OP);
1166}
1167
1168int
1169SMB2_echo(struct TCP_Server_Info *server)
1170{
1171 struct smb2_echo_req *req;
1172 int rc = 0;
1173 struct kvec iov;
fec344e3
JL
1174 struct smb_rqst rqst = { .rq_iov = &iov,
1175 .rq_nvec = 1 };
9094fad1
PS
1176
1177 cFYI(1, "In echo request");
1178
1179 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
1180 if (rc)
1181 return rc;
1182
1183 req->hdr.CreditRequest = cpu_to_le16(1);
1184
1185 iov.iov_base = (char *)req;
1186 /* 4 for rfc1002 length field */
1187 iov.iov_len = get_rfc1002_length(req) + 4;
1188
fec344e3 1189 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
9094fad1
PS
1190 CIFS_ECHO_OP);
1191 if (rc)
1192 cFYI(1, "Echo request failed: %d", rc);
1193
1194 cifs_small_buf_release(req);
1195 return rc;
1196}
7a5cfb19
PS
1197
1198int
1199SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1200 u64 volatile_fid)
1201{
1202 struct smb2_flush_req *req;
1203 struct TCP_Server_Info *server;
1204 struct cifs_ses *ses = tcon->ses;
1205 struct kvec iov[1];
1206 int resp_buftype;
1207 int rc = 0;
1208
1209 cFYI(1, "Flush");
1210
1211 if (ses && (ses->server))
1212 server = ses->server;
1213 else
1214 return -EIO;
1215
1216 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
1217 if (rc)
1218 return rc;
1219
1220 req->PersistentFileId = persistent_fid;
1221 req->VolatileFileId = volatile_fid;
1222
1223 iov[0].iov_base = (char *)req;
1224 /* 4 for rfc1002 length field */
1225 iov[0].iov_len = get_rfc1002_length(req) + 4;
1226
1227 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1228
1229 if ((rc != 0) && tcon)
1230 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
1231
1232 free_rsp_buf(resp_buftype, iov[0].iov_base);
1233 return rc;
1234}
09a4707e
PS
1235
1236/*
1237 * To form a chain of read requests, any read requests after the first should
1238 * have the end_of_chain boolean set to true.
1239 */
1240static int
1241smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
1242 unsigned int remaining_bytes, int request_type)
1243{
1244 int rc = -EACCES;
1245 struct smb2_read_req *req = NULL;
1246
1247 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
1248 if (rc)
1249 return rc;
1250 if (io_parms->tcon->ses->server == NULL)
1251 return -ECONNABORTED;
1252
1253 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
1254
1255 req->PersistentFileId = io_parms->persistent_fid;
1256 req->VolatileFileId = io_parms->volatile_fid;
1257 req->ReadChannelInfoOffset = 0; /* reserved */
1258 req->ReadChannelInfoLength = 0; /* reserved */
1259 req->Channel = 0; /* reserved */
1260 req->MinimumCount = 0;
1261 req->Length = cpu_to_le32(io_parms->length);
1262 req->Offset = cpu_to_le64(io_parms->offset);
1263
1264 if (request_type & CHAINED_REQUEST) {
1265 if (!(request_type & END_OF_CHAIN)) {
1266 /* 4 for rfc1002 length field */
1267 req->hdr.NextCommand =
1268 cpu_to_le32(get_rfc1002_length(req) + 4);
1269 } else /* END_OF_CHAIN */
1270 req->hdr.NextCommand = 0;
1271 if (request_type & RELATED_REQUEST) {
1272 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
1273 /*
1274 * Related requests use info from previous read request
1275 * in chain.
1276 */
1277 req->hdr.SessionId = 0xFFFFFFFF;
1278 req->hdr.TreeId = 0xFFFFFFFF;
1279 req->PersistentFileId = 0xFFFFFFFF;
1280 req->VolatileFileId = 0xFFFFFFFF;
1281 }
1282 }
1283 if (remaining_bytes > io_parms->length)
1284 req->RemainingBytes = cpu_to_le32(remaining_bytes);
1285 else
1286 req->RemainingBytes = 0;
1287
1288 iov[0].iov_base = (char *)req;
1289 /* 4 for rfc1002 length field */
1290 iov[0].iov_len = get_rfc1002_length(req) + 4;
1291 return rc;
1292}
1293
1294static void
1295smb2_readv_callback(struct mid_q_entry *mid)
1296{
1297 struct cifs_readdata *rdata = mid->callback_data;
1298 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1299 struct TCP_Server_Info *server = tcon->ses->server;
1300 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov[0].iov_base;
1301 unsigned int credits_received = 1;
0b688cfc
JL
1302 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1303 .rq_nvec = rdata->nr_iov };
09a4707e
PS
1304
1305 cFYI(1, "%s: mid=%llu state=%d result=%d bytes=%u", __func__,
1306 mid->mid, mid->mid_state, rdata->result, rdata->bytes);
1307
1308 switch (mid->mid_state) {
1309 case MID_RESPONSE_RECEIVED:
1310 credits_received = le16_to_cpu(buf->CreditRequest);
1311 /* result already set, check signature */
3c1bf7e4
PS
1312 if (server->sec_mode &
1313 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
1314 int rc;
1315
0b688cfc 1316 rc = smb2_verify_signature(&rqst, server);
3c1bf7e4
PS
1317 if (rc)
1318 cERROR(1, "SMB signature verification returned "
1319 "error = %d", rc);
1320 }
09a4707e
PS
1321 /* FIXME: should this be counted toward the initiating task? */
1322 task_io_account_read(rdata->bytes);
1323 cifs_stats_bytes_read(tcon, rdata->bytes);
1324 break;
1325 case MID_REQUEST_SUBMITTED:
1326 case MID_RETRY_NEEDED:
1327 rdata->result = -EAGAIN;
1328 break;
1329 default:
1330 if (rdata->result != -ENODATA)
1331 rdata->result = -EIO;
1332 }
1333
1334 if (rdata->result)
1335 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
1336
1337 queue_work(cifsiod_wq, &rdata->work);
1338 DeleteMidQEntry(mid);
1339 add_credits(server, credits_received, 0);
1340}
1341
1342/* smb2_async_readv - send an async write, and set up mid to handle result */
1343int
1344smb2_async_readv(struct cifs_readdata *rdata)
1345{
1346 int rc;
1347 struct smb2_hdr *buf;
1348 struct cifs_io_parms io_parms;
fec344e3
JL
1349 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1350 .rq_nvec = 1 };
09a4707e
PS
1351
1352 cFYI(1, "%s: offset=%llu bytes=%u", __func__,
1353 rdata->offset, rdata->bytes);
1354
1355 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
1356 io_parms.offset = rdata->offset;
1357 io_parms.length = rdata->bytes;
1358 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
1359 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
1360 io_parms.pid = rdata->pid;
1361 rc = smb2_new_read_req(&rdata->iov[0], &io_parms, 0, 0);
1362 if (rc)
1363 return rc;
1364
1365 buf = (struct smb2_hdr *)rdata->iov[0].iov_base;
1366 /* 4 for rfc1002 length field */
1367 rdata->iov[0].iov_len = get_rfc1002_length(rdata->iov[0].iov_base) + 4;
1368
1369 kref_get(&rdata->refcount);
fec344e3 1370 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
09a4707e
PS
1371 cifs_readv_receive, smb2_readv_callback,
1372 rdata, 0);
1373 if (rc)
1374 kref_put(&rdata->refcount, cifs_readdata_release);
1375
1376 cifs_small_buf_release(buf);
1377 return rc;
1378}
33319141 1379
d8e05039
PS
1380int
1381SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
1382 unsigned int *nbytes, char **buf, int *buf_type)
1383{
1384 int resp_buftype, rc = -EACCES;
1385 struct smb2_read_rsp *rsp = NULL;
1386 struct kvec iov[1];
1387
1388 *nbytes = 0;
1389 rc = smb2_new_read_req(iov, io_parms, 0, 0);
1390 if (rc)
1391 return rc;
1392
1393 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
1394 &resp_buftype, CIFS_LOG_ERROR);
1395
1396 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
1397
1398 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
1399 free_rsp_buf(resp_buftype, iov[0].iov_base);
1400 return 0;
1401 }
1402
1403 if (rc) {
1404 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
1405 cERROR(1, "Send error in read = %d", rc);
1406 } else {
1407 *nbytes = le32_to_cpu(rsp->DataLength);
1408 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
1409 (*nbytes > io_parms->length)) {
1410 cFYI(1, "bad length %d for count %d", *nbytes,
1411 io_parms->length);
1412 rc = -EIO;
1413 *nbytes = 0;
1414 }
1415 }
1416
1417 if (*buf) {
1418 memcpy(*buf, (char *)rsp->hdr.ProtocolId + rsp->DataOffset,
1419 *nbytes);
1420 free_rsp_buf(resp_buftype, iov[0].iov_base);
1421 } else if (resp_buftype != CIFS_NO_BUFFER) {
1422 *buf = iov[0].iov_base;
1423 if (resp_buftype == CIFS_SMALL_BUFFER)
1424 *buf_type = CIFS_SMALL_BUFFER;
1425 else if (resp_buftype == CIFS_LARGE_BUFFER)
1426 *buf_type = CIFS_LARGE_BUFFER;
1427 }
1428 return rc;
1429}
1430
33319141
PS
1431/*
1432 * Check the mid_state and signature on received buffer (if any), and queue the
1433 * workqueue completion task.
1434 */
1435static void
1436smb2_writev_callback(struct mid_q_entry *mid)
1437{
1438 struct cifs_writedata *wdata = mid->callback_data;
1439 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
1440 unsigned int written;
1441 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
1442 unsigned int credits_received = 1;
1443
1444 switch (mid->mid_state) {
1445 case MID_RESPONSE_RECEIVED:
1446 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
1447 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
1448 if (wdata->result != 0)
1449 break;
1450
1451 written = le32_to_cpu(rsp->DataLength);
1452 /*
1453 * Mask off high 16 bits when bytes written as returned
1454 * by the server is greater than bytes requested by the
1455 * client. OS/2 servers are known to set incorrect
1456 * CountHigh values.
1457 */
1458 if (written > wdata->bytes)
1459 written &= 0xFFFF;
1460
1461 if (written < wdata->bytes)
1462 wdata->result = -ENOSPC;
1463 else
1464 wdata->bytes = written;
1465 break;
1466 case MID_REQUEST_SUBMITTED:
1467 case MID_RETRY_NEEDED:
1468 wdata->result = -EAGAIN;
1469 break;
1470 default:
1471 wdata->result = -EIO;
1472 break;
1473 }
1474
1475 if (wdata->result)
1476 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
1477
1478 queue_work(cifsiod_wq, &wdata->work);
1479 DeleteMidQEntry(mid);
1480 add_credits(tcon->ses->server, credits_received, 0);
1481}
1482
1483/* smb2_async_writev - send an async write, and set up mid to handle result */
1484int
1485smb2_async_writev(struct cifs_writedata *wdata)
1486{
eddb079d 1487 int rc = -EACCES;
33319141
PS
1488 struct smb2_write_req *req = NULL;
1489 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
eddb079d 1490 struct kvec iov;
fec344e3 1491 struct smb_rqst rqst;
33319141
PS
1492
1493 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
1494 if (rc)
1495 goto async_writev_out;
1496
33319141
PS
1497 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
1498
1499 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
1500 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
1501 req->WriteChannelInfoOffset = 0;
1502 req->WriteChannelInfoLength = 0;
1503 req->Channel = 0;
1504 req->Offset = cpu_to_le64(wdata->offset);
1505 /* 4 for rfc1002 length field */
1506 req->DataOffset = cpu_to_le16(
1507 offsetof(struct smb2_write_req, Buffer) - 4);
1508 req->RemainingBytes = 0;
1509
1510 /* 4 for rfc1002 length field and 1 for Buffer */
eddb079d
JL
1511 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
1512 iov.iov_base = req;
33319141 1513
eddb079d
JL
1514 rqst.rq_iov = &iov;
1515 rqst.rq_nvec = 1;
1516 rqst.rq_pages = wdata->pages;
1517 rqst.rq_npages = wdata->nr_pages;
1518 rqst.rq_pagesz = wdata->pagesz;
1519 rqst.rq_tailsz = wdata->tailsz;
33319141
PS
1520
1521 cFYI(1, "async write at %llu %u bytes", wdata->offset, wdata->bytes);
1522
1523 req->Length = cpu_to_le32(wdata->bytes);
1524
1525 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
1526
1527 kref_get(&wdata->refcount);
fec344e3
JL
1528 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
1529 smb2_writev_callback, wdata, 0);
33319141
PS
1530
1531 if (rc)
1532 kref_put(&wdata->refcount, cifs_writedata_release);
1533
33319141
PS
1534async_writev_out:
1535 cifs_small_buf_release(req);
33319141
PS
1536 return rc;
1537}
009d3443
PS
1538
1539/*
1540 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
1541 * The length field from io_parms must be at least 1 and indicates a number of
1542 * elements with data to write that begins with position 1 in iov array. All
1543 * data length is specified by count.
1544 */
1545int
1546SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
1547 unsigned int *nbytes, struct kvec *iov, int n_vec)
1548{
1549 int rc = 0;
1550 struct smb2_write_req *req = NULL;
1551 struct smb2_write_rsp *rsp = NULL;
1552 int resp_buftype;
1553 *nbytes = 0;
1554
1555 if (n_vec < 1)
1556 return rc;
1557
1558 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
1559 if (rc)
1560 return rc;
1561
1562 if (io_parms->tcon->ses->server == NULL)
1563 return -ECONNABORTED;
1564
1565 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
1566
1567 req->PersistentFileId = io_parms->persistent_fid;
1568 req->VolatileFileId = io_parms->volatile_fid;
1569 req->WriteChannelInfoOffset = 0;
1570 req->WriteChannelInfoLength = 0;
1571 req->Channel = 0;
1572 req->Length = cpu_to_le32(io_parms->length);
1573 req->Offset = cpu_to_le64(io_parms->offset);
1574 /* 4 for rfc1002 length field */
1575 req->DataOffset = cpu_to_le16(
1576 offsetof(struct smb2_write_req, Buffer) - 4);
1577 req->RemainingBytes = 0;
1578
1579 iov[0].iov_base = (char *)req;
1580 /* 4 for rfc1002 length field and 1 for Buffer */
1581 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1582
1583 /* length of entire message including data to be written */
1584 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
1585
1586 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
1587 &resp_buftype, 0);
1588
1589 if (rc) {
1590 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
1591 cERROR(1, "Send error in write = %d", rc);
1592 } else {
1593 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
1594 *nbytes = le32_to_cpu(rsp->DataLength);
1595 free_rsp_buf(resp_buftype, rsp);
1596 }
1597 return rc;
1598}
35143eb5 1599
d324f08d
PS
1600static unsigned int
1601num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
1602{
1603 int len;
1604 unsigned int entrycount = 0;
1605 unsigned int next_offset = 0;
1606 FILE_DIRECTORY_INFO *entryptr;
1607
1608 if (bufstart == NULL)
1609 return 0;
1610
1611 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
1612
1613 while (1) {
1614 entryptr = (FILE_DIRECTORY_INFO *)
1615 ((char *)entryptr + next_offset);
1616
1617 if ((char *)entryptr + size > end_of_buf) {
1618 cERROR(1, "malformed search entry would overflow");
1619 break;
1620 }
1621
1622 len = le32_to_cpu(entryptr->FileNameLength);
1623 if ((char *)entryptr + len + size > end_of_buf) {
1624 cERROR(1, "directory entry name would overflow frame "
1625 "end of buf %p", end_of_buf);
1626 break;
1627 }
1628
1629 *lastentry = (char *)entryptr;
1630 entrycount++;
1631
1632 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
1633 if (!next_offset)
1634 break;
1635 }
1636
1637 return entrycount;
1638}
1639
1640/*
1641 * Readdir/FindFirst
1642 */
1643int
1644SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
1645 u64 persistent_fid, u64 volatile_fid, int index,
1646 struct cifs_search_info *srch_inf)
1647{
1648 struct smb2_query_directory_req *req;
1649 struct smb2_query_directory_rsp *rsp = NULL;
1650 struct kvec iov[2];
1651 int rc = 0;
1652 int len;
1653 int resp_buftype;
1654 unsigned char *bufptr;
1655 struct TCP_Server_Info *server;
1656 struct cifs_ses *ses = tcon->ses;
1657 __le16 asteriks = cpu_to_le16('*');
1658 char *end_of_smb;
1659 unsigned int output_size = CIFSMaxBufSize;
1660 size_t info_buf_size;
1661
1662 if (ses && (ses->server))
1663 server = ses->server;
1664 else
1665 return -EIO;
1666
1667 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
1668 if (rc)
1669 return rc;
1670
1671 switch (srch_inf->info_level) {
1672 case SMB_FIND_FILE_DIRECTORY_INFO:
1673 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
1674 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
1675 break;
1676 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
1677 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
1678 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
1679 break;
1680 default:
1681 cERROR(1, "info level %u isn't supported",
1682 srch_inf->info_level);
1683 rc = -EINVAL;
1684 goto qdir_exit;
1685 }
1686
1687 req->FileIndex = cpu_to_le32(index);
1688 req->PersistentFileId = persistent_fid;
1689 req->VolatileFileId = volatile_fid;
1690
1691 len = 0x2;
1692 bufptr = req->Buffer;
1693 memcpy(bufptr, &asteriks, len);
1694
1695 req->FileNameOffset =
1696 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
1697 req->FileNameLength = cpu_to_le16(len);
1698 /*
1699 * BB could be 30 bytes or so longer if we used SMB2 specific
1700 * buffer lengths, but this is safe and close enough.
1701 */
1702 output_size = min_t(unsigned int, output_size, server->maxBuf);
1703 output_size = min_t(unsigned int, output_size, 2 << 15);
1704 req->OutputBufferLength = cpu_to_le32(output_size);
1705
1706 iov[0].iov_base = (char *)req;
1707 /* 4 for RFC1001 length and 1 for Buffer */
1708 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1709
1710 iov[1].iov_base = (char *)(req->Buffer);
1711 iov[1].iov_len = len;
1712
1713 inc_rfc1001_len(req, len - 1 /* Buffer */);
1714
1715 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
1716 if (rc) {
1717 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
1718 goto qdir_exit;
1719 }
1720 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
1721
1722 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
1723 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
1724 info_buf_size);
1725 if (rc)
1726 goto qdir_exit;
1727
1728 srch_inf->unicode = true;
1729
1730 if (srch_inf->ntwrk_buf_start) {
1731 if (srch_inf->smallBuf)
1732 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
1733 else
1734 cifs_buf_release(srch_inf->ntwrk_buf_start);
1735 }
1736 srch_inf->ntwrk_buf_start = (char *)rsp;
1737 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
1738 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
1739 /* 4 for rfc1002 length field */
1740 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
1741 srch_inf->entries_in_buffer =
1742 num_entries(srch_inf->srch_entries_start, end_of_smb,
1743 &srch_inf->last_entry, info_buf_size);
1744 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
1745 cFYI(1, "num entries %d last_index %lld srch start %p srch end %p",
1746 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
1747 srch_inf->srch_entries_start, srch_inf->last_entry);
1748 if (resp_buftype == CIFS_LARGE_BUFFER)
1749 srch_inf->smallBuf = false;
1750 else if (resp_buftype == CIFS_SMALL_BUFFER)
1751 srch_inf->smallBuf = true;
1752 else
1753 cERROR(1, "illegal search buffer type");
1754
1755 if (rsp->hdr.Status == STATUS_NO_MORE_FILES)
1756 srch_inf->endOfSearch = 1;
1757 else
1758 srch_inf->endOfSearch = 0;
1759
1760 return rc;
1761
1762qdir_exit:
1763 free_rsp_buf(resp_buftype, rsp);
1764 return rc;
1765}
1766
35143eb5
PS
1767static int
1768send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
c839ff24 1769 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
35143eb5
PS
1770 unsigned int num, void **data, unsigned int *size)
1771{
1772 struct smb2_set_info_req *req;
1773 struct smb2_set_info_rsp *rsp = NULL;
1774 struct kvec *iov;
1775 int rc = 0;
1776 int resp_buftype;
1777 unsigned int i;
1778 struct TCP_Server_Info *server;
1779 struct cifs_ses *ses = tcon->ses;
1780
1781 if (ses && (ses->server))
1782 server = ses->server;
1783 else
1784 return -EIO;
1785
1786 if (!num)
1787 return -EINVAL;
1788
1789 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
1790 if (!iov)
1791 return -ENOMEM;
1792
1793 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
1794 if (rc) {
1795 kfree(iov);
1796 return rc;
1797 }
1798
c839ff24
PS
1799 req->hdr.ProcessId = cpu_to_le32(pid);
1800
35143eb5
PS
1801 req->InfoType = SMB2_O_INFO_FILE;
1802 req->FileInfoClass = info_class;
1803 req->PersistentFileId = persistent_fid;
1804 req->VolatileFileId = volatile_fid;
1805
1806 /* 4 for RFC1001 length and 1 for Buffer */
1807 req->BufferOffset =
1808 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
1809 req->BufferLength = cpu_to_le32(*size);
1810
1811 inc_rfc1001_len(req, *size - 1 /* Buffer */);
1812
1813 memcpy(req->Buffer, *data, *size);
1814
1815 iov[0].iov_base = (char *)req;
1816 /* 4 for RFC1001 length */
1817 iov[0].iov_len = get_rfc1002_length(req) + 4;
1818
1819 for (i = 1; i < num; i++) {
1820 inc_rfc1001_len(req, size[i]);
1821 le32_add_cpu(&req->BufferLength, size[i]);
1822 iov[i].iov_base = (char *)data[i];
1823 iov[i].iov_len = size[i];
1824 }
1825
1826 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
1827 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
1828
1829 if (rc != 0) {
1830 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
1831 goto out;
1832 }
1833
1834 if (rsp == NULL) {
1835 rc = -EIO;
1836 goto out;
1837 }
1838
1839out:
1840 free_rsp_buf(resp_buftype, rsp);
1841 kfree(iov);
1842 return rc;
1843}
1844
1845int
1846SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
1847 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
1848{
1849 struct smb2_file_rename_info info;
1850 void **data;
1851 unsigned int size[2];
1852 int rc;
1853 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
1854
1855 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
1856 if (!data)
1857 return -ENOMEM;
1858
1859 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
1860 /* 0 = fail if target already exists */
1861 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
1862 info.FileNameLength = cpu_to_le32(len);
1863
1864 data[0] = &info;
1865 size[0] = sizeof(struct smb2_file_rename_info);
1866
1867 data[1] = target_file;
1868 size[1] = len + 2 /* null */;
1869
1870 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
c839ff24
PS
1871 current->tgid, FILE_RENAME_INFORMATION, 2, data,
1872 size);
35143eb5
PS
1873 kfree(data);
1874 return rc;
1875}
568798cc
PS
1876
1877int
1878SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
1879 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
1880{
1881 struct smb2_file_link_info info;
1882 void **data;
1883 unsigned int size[2];
1884 int rc;
1885 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
1886
1887 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
1888 if (!data)
1889 return -ENOMEM;
1890
1891 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
1892 /* 0 = fail if link already exists */
1893 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
1894 info.FileNameLength = cpu_to_le32(len);
1895
1896 data[0] = &info;
1897 size[0] = sizeof(struct smb2_file_link_info);
1898
1899 data[1] = target_file;
1900 size[1] = len + 2 /* null */;
1901
1902 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
c839ff24 1903 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
568798cc
PS
1904 kfree(data);
1905 return rc;
1906}
c839ff24
PS
1907
1908int
1909SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1910 u64 volatile_fid, u32 pid, __le64 *eof)
1911{
1912 struct smb2_file_eof_info info;
1913 void *data;
1914 unsigned int size;
1915
1916 info.EndOfFile = *eof;
1917
1918 data = &info;
1919 size = sizeof(struct smb2_file_eof_info);
1920
1921 return send_set_info(xid, tcon, persistent_fid, volatile_fid, pid,
1922 FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
1923}
1feeaac7
PS
1924
1925int
1926SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
1927 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
1928{
1929 unsigned int size;
1930 size = sizeof(FILE_BASIC_INFO);
1931 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
1932 current->tgid, FILE_BASIC_INFORMATION, 1,
1933 (void **)&buf, &size);
1934}
983c88a4
PS
1935
1936int
1937SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
1938 const u64 persistent_fid, const u64 volatile_fid,
1939 __u8 oplock_level)
1940{
1941 int rc;
1942 struct smb2_oplock_break *req = NULL;
1943
1944 cFYI(1, "SMB2_oplock_break");
1945 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
1946
1947 if (rc)
1948 return rc;
1949
1950 req->VolatileFid = volatile_fid;
1951 req->PersistentFid = persistent_fid;
1952 req->OplockLevel = oplock_level;
1953 req->hdr.CreditRequest = cpu_to_le16(1);
1954
1955 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
1956 /* SMB2 buffer freed by function above */
1957
1958 if (rc) {
1959 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
1960 cFYI(1, "Send error in Oplock Break = %d", rc);
1961 }
1962
1963 return rc;
1964}
6fc05c25
PS
1965
1966static void
1967copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
1968 struct kstatfs *kst)
1969{
1970 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
1971 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
1972 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
1973 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
1974 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
1975 return;
1976}
1977
1978static int
1979build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
1980 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
1981{
1982 int rc;
1983 struct smb2_query_info_req *req;
1984
1985 cFYI(1, "Query FSInfo level %d", level);
1986
1987 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
1988 return -EIO;
1989
1990 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1991 if (rc)
1992 return rc;
1993
1994 req->InfoType = SMB2_O_INFO_FILESYSTEM;
1995 req->FileInfoClass = level;
1996 req->PersistentFileId = persistent_fid;
1997 req->VolatileFileId = volatile_fid;
1998 /* 4 for rfc1002 length field and 1 for pad */
1999 req->InputBufferOffset =
2000 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2001 req->OutputBufferLength = cpu_to_le32(
2002 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2003
2004 iov->iov_base = (char *)req;
2005 /* 4 for rfc1002 length field */
2006 iov->iov_len = get_rfc1002_length(req) + 4;
2007 return 0;
2008}
2009
2010int
2011SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2012 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2013{
2014 struct smb2_query_info_rsp *rsp = NULL;
2015 struct kvec iov;
2016 int rc = 0;
2017 int resp_buftype;
2018 struct cifs_ses *ses = tcon->ses;
2019 struct smb2_fs_full_size_info *info = NULL;
2020
2021 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
2022 sizeof(struct smb2_fs_full_size_info),
2023 persistent_fid, volatile_fid);
2024 if (rc)
2025 return rc;
2026
2027 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2028 if (rc) {
2029 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2030 goto qinf_exit;
2031 }
2032 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2033
2034 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
2035 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
2036 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2037 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2038 sizeof(struct smb2_fs_full_size_info));
2039 if (!rc)
2040 copy_fs_info_to_kstatfs(info, fsdata);
2041
2042qinf_exit:
2043 free_rsp_buf(resp_buftype, iov.iov_base);
2044 return rc;
2045}