blob: 10f8d5cf5681e26b843c0ac15821eab69e6eeb22 [file] [log] [blame]
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001/*
2 * fs/cifs/smb2pdu.c
3 *
Steve French2b80d042013-06-23 18:43:37 -05004 * Copyright (C) International Business Machines Corp., 2009, 2013
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04005 * 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>
Pavel Shilovsky09a47072012-09-18 16:20:29 -070034#include <linux/task_io_accounting_ops.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040035#include <linux/uaccess.h>
Pavel Shilovsky33319142012-09-18 16:20:29 -070036#include <linux/pagemap.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040037#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"
Pavel Shilovsky09a47072012-09-18 16:20:29 -070047#include "smb2glob.h"
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -070048#include "cifspdu.h"
Steve Frenchceb1b0b2015-09-24 00:52:37 -050049#include "cifs_spnego.h"
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040050
51/*
52 * The following table defines the expected "StructureSize" of SMB2 requests
53 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
54 *
55 * Note that commands are defined in smb2pdu.h in le16 but the array below is
56 * indexed by command in host byte order.
57 */
58static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
59 /* SMB2_NEGOTIATE */ 36,
60 /* SMB2_SESSION_SETUP */ 25,
61 /* SMB2_LOGOFF */ 4,
62 /* SMB2_TREE_CONNECT */ 9,
63 /* SMB2_TREE_DISCONNECT */ 4,
64 /* SMB2_CREATE */ 57,
65 /* SMB2_CLOSE */ 24,
66 /* SMB2_FLUSH */ 24,
67 /* SMB2_READ */ 49,
68 /* SMB2_WRITE */ 49,
69 /* SMB2_LOCK */ 48,
70 /* SMB2_IOCTL */ 57,
71 /* SMB2_CANCEL */ 4,
72 /* SMB2_ECHO */ 4,
73 /* SMB2_QUERY_DIRECTORY */ 33,
74 /* SMB2_CHANGE_NOTIFY */ 32,
75 /* SMB2_QUERY_INFO */ 41,
76 /* SMB2_SET_INFO */ 33,
77 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
78};
79
80
81static void
82smb2_hdr_assemble(struct smb2_hdr *hdr, __le16 smb2_cmd /* command */ ,
83 const struct cifs_tcon *tcon)
84{
85 struct smb2_pdu *pdu = (struct smb2_pdu *)hdr;
86 char *temp = (char *)hdr;
87 /* lookup word count ie StructureSize from table */
88 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_cmd)];
89
90 /*
91 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
92 * largest operations (Create)
93 */
94 memset(temp, 0, 256);
95
96 /* Note this is only network field converted to big endian */
97 hdr->smb2_buf_length = cpu_to_be32(parmsize + sizeof(struct smb2_hdr)
98 - 4 /* RFC 1001 length field itself not counted */);
99
Steve French373512e2015-12-18 13:05:30 -0600100 hdr->ProtocolId = SMB2_PROTO_NUMBER;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400101 hdr->StructureSize = cpu_to_le16(64);
102 hdr->Command = smb2_cmd;
103 hdr->CreditRequest = cpu_to_le16(2); /* BB make this dynamic */
104 hdr->ProcessId = cpu_to_le32((__u16)current->tgid);
105
106 if (!tcon)
107 goto out;
108
Steve French2b80d042013-06-23 18:43:37 -0500109 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
110 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500111 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500112 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Steve French2b80d042013-06-23 18:43:37 -0500113 hdr->CreditCharge = cpu_to_le16(1);
114 /* else CreditCharge MBZ */
115
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400116 hdr->TreeId = tcon->tid;
117 /* Uid is not converted */
118 if (tcon->ses)
119 hdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500120
121 /*
122 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
123 * to pass the path on the Open SMB prefixed by \\server\share.
124 * Not sure when we would need to do the augmented path (if ever) and
125 * setting this flag breaks the SMB2 open operation since it is
126 * illegal to send an empty path name (without \\server\share prefix)
127 * when the DFS flag is set in the SMB open header. We could
128 * consider setting the flag on all operations other than open
129 * but it is safer to net set it for now.
130 */
131/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
132 hdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
133
Jeff Layton38d77c52013-05-26 07:01:00 -0400134 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700135 hdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400136out:
137 pdu->StructureSize2 = cpu_to_le16(parmsize);
138 return;
139}
140
141static int
142smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
143{
144 int rc = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400145 struct nls_table *nls_codepage;
146 struct cifs_ses *ses;
147 struct TCP_Server_Info *server;
148
149 /*
150 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
151 * check for tcp and smb session status done differently
152 * for those three - in the calling routine.
153 */
154 if (tcon == NULL)
155 return rc;
156
157 if (smb2_command == SMB2_TREE_CONNECT)
158 return rc;
159
160 if (tcon->tidStatus == CifsExiting) {
161 /*
162 * only tree disconnect, open, and write,
163 * (and ulogoff which does not have tcon)
164 * are allowed as we start force umount.
165 */
166 if ((smb2_command != SMB2_WRITE) &&
167 (smb2_command != SMB2_CREATE) &&
168 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500169 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
170 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400171 return -ENODEV;
172 }
173 }
174 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
175 (!tcon->ses->server))
176 return -EIO;
177
178 ses = tcon->ses;
179 server = ses->server;
180
181 /*
182 * Give demultiplex thread up to 10 seconds to reconnect, should be
183 * greater than cifs socket timeout which is 7 seconds
184 */
185 while (server->tcpStatus == CifsNeedReconnect) {
186 /*
187 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
188 * here since they are implicitly done when session drops.
189 */
190 switch (smb2_command) {
191 /*
192 * BB Should we keep oplock break and add flush to exceptions?
193 */
194 case SMB2_TREE_DISCONNECT:
195 case SMB2_CANCEL:
196 case SMB2_CLOSE:
197 case SMB2_OPLOCK_BREAK:
198 return -EAGAIN;
199 }
200
201 wait_event_interruptible_timeout(server->response_q,
202 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
203
204 /* are we still trying to reconnect? */
205 if (server->tcpStatus != CifsNeedReconnect)
206 break;
207
208 /*
209 * on "soft" mounts we wait once. Hard mounts keep
210 * retrying until process is killed or server comes
211 * back on-line
212 */
213 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500214 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400215 return -EHOSTDOWN;
216 }
217 }
218
219 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
220 return rc;
221
222 nls_codepage = load_nls_default();
223
224 /*
225 * need to prevent multiple threads trying to simultaneously reconnect
226 * the same SMB session
227 */
228 mutex_lock(&tcon->ses->session_mutex);
229 rc = cifs_negotiate_protocol(0, tcon->ses);
230 if (!rc && tcon->ses->need_reconnect)
231 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
232
233 if (rc || !tcon->need_reconnect) {
234 mutex_unlock(&tcon->ses->session_mutex);
235 goto out;
236 }
237
238 cifs_mark_open_files_invalid(tcon);
239 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
240 mutex_unlock(&tcon->ses->session_mutex);
Joe Perchesf96637b2013-05-04 22:12:25 -0500241 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400242 if (rc)
243 goto out;
244 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400245out:
246 /*
247 * Check if handle based operation so we know whether we can continue
248 * or not without returning to caller to reset file handle.
249 */
250 /*
251 * BB Is flush done by server on drop of tcp session? Should we special
252 * case it and skip above?
253 */
254 switch (smb2_command) {
255 case SMB2_FLUSH:
256 case SMB2_READ:
257 case SMB2_WRITE:
258 case SMB2_LOCK:
259 case SMB2_IOCTL:
260 case SMB2_QUERY_DIRECTORY:
261 case SMB2_CHANGE_NOTIFY:
262 case SMB2_QUERY_INFO:
263 case SMB2_SET_INFO:
264 return -EAGAIN;
265 }
266 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400267 return rc;
268}
269
270/*
271 * Allocate and return pointer to an SMB request hdr, and set basic
272 * SMB information in the SMB header. If the return code is zero, this
273 * function must have filled in request_buf pointer.
274 */
275static int
276small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
277 void **request_buf)
278{
279 int rc = 0;
280
281 rc = smb2_reconnect(smb2_command, tcon);
282 if (rc)
283 return rc;
284
285 /* BB eventually switch this to SMB2 specific small buf size */
286 *request_buf = cifs_small_buf_get();
287 if (*request_buf == NULL) {
288 /* BB should we add a retry in here if not a writepage? */
289 return -ENOMEM;
290 }
291
292 smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
293
294 if (tcon != NULL) {
295#ifdef CONFIG_CIFS_STATS2
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400296 uint16_t com_code = le16_to_cpu(smb2_command);
297 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400298#endif
299 cifs_stats_inc(&tcon->num_smbs_sent);
300 }
301
302 return rc;
303}
304
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500305#ifdef CONFIG_CIFS_SMB311
306/* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
307#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
308
309
310#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
311#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
312
313static void
314build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
315{
316 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
317 pneg_ctxt->DataLength = cpu_to_le16(38);
318 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
319 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
320 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
321 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
322}
323
324static void
325build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
326{
327 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
328 pneg_ctxt->DataLength = cpu_to_le16(6);
329 pneg_ctxt->CipherCount = cpu_to_le16(2);
330 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
331 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
332}
333
334static void
335assemble_neg_contexts(struct smb2_negotiate_req *req)
336{
337
338 /* +4 is to account for the RFC1001 len field */
339 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
340
341 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
342 /* Add 2 to size to round to 8 byte boundary */
343 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
344 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
345 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
346 req->NegotiateContextCount = cpu_to_le16(2);
347 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context) + 2
348 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
349}
350#else
351static void assemble_neg_contexts(struct smb2_negotiate_req *req)
352{
353 return;
354}
355#endif /* SMB311 */
356
357
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400358/*
359 *
360 * SMB2 Worker functions follow:
361 *
362 * The general structure of the worker functions is:
363 * 1) Call smb2_init (assembles SMB2 header)
364 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
365 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
366 * 4) Decode SMB2 command specific fields in the fixed length area
367 * 5) Decode variable length data area (if any for this SMB2 command type)
368 * 6) Call free smb buffer
369 * 7) return
370 *
371 */
372
373int
374SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
375{
376 struct smb2_negotiate_req *req;
377 struct smb2_negotiate_rsp *rsp;
378 struct kvec iov[1];
379 int rc = 0;
380 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400381 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400382 int blob_offset, blob_length;
383 char *security_blob;
384 int flags = CIFS_NEG_OP;
385
Joe Perchesf96637b2013-05-04 22:12:25 -0500386 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400387
Jeff Layton3534b852013-05-24 07:41:01 -0400388 if (!server) {
389 WARN(1, "%s: server is NULL!\n", __func__);
390 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400391 }
392
393 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
394 if (rc)
395 return rc;
396
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400397 req->hdr.SessionId = 0;
398
Steve Frenche4aa25e2012-10-01 12:26:22 -0500399 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400400
Steve Frenche4aa25e2012-10-01 12:26:22 -0500401 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
402 inc_rfc1001_len(req, 2);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400403
404 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400405 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500406 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400407 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500408 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400409 else
410 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400411
Steve Frenche4aa25e2012-10-01 12:26:22 -0500412 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400413
Steve French3c5f9be12014-05-13 13:37:45 -0700414 /* ClientGUID must be zero for SMB2.02 dialect */
415 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
416 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500417 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700418 memcpy(req->ClientGUID, server->client_guid,
419 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500420 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
421 assemble_neg_contexts(req);
422 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400423 iov[0].iov_base = (char *)req;
424 /* 4 for rfc1002 length field */
425 iov[0].iov_len = get_rfc1002_length(req) + 4;
426
427 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
428
429 rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
430 /*
431 * No tcon so can't do
432 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
433 */
434 if (rc != 0)
435 goto neg_exit;
436
Joe Perchesf96637b2013-05-04 22:12:25 -0500437 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400438
Steve Frenche4aa25e2012-10-01 12:26:22 -0500439 /* BB we may eventually want to match the negotiated vs. requested
440 dialect, even though we are only requesting one at a time */
441 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500442 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500443 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500444 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500445 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500446 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500447 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
448 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600449#ifdef CONFIG_CIFS_SMB311
450 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
451 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
452#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400453 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500454 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500455 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400456 rc = -EIO;
457 goto neg_exit;
458 }
459 server->dialect = le16_to_cpu(rsp->DialectRevision);
460
Jeff Laytone598d1d82013-05-26 07:00:59 -0400461 /* SMB2 only has an extended negflavor */
462 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400463 /* set it to the maximum buffer size value we can send with 1 credit */
464 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
465 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400466 server->max_read = le32_to_cpu(rsp->MaxReadSize);
467 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
468 /* BB Do we need to validate the SecurityMode? */
469 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
470 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400471 /* Internal types */
472 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400473
474 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
475 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500476 /*
477 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
478 * for us will be
479 * ses->sectype = RawNTLMSSP;
480 * but for time being this is our only auth choice so doesn't matter.
481 * We just found a server which sets blob length to zero expecting raw.
482 */
483 if (blob_length == 0)
484 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700485
Jeff Layton38d77c52013-05-26 07:01:00 -0400486 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400487 if (rc)
488 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500489 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500490 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500491 if (rc == 1)
492 rc = 0;
493 else if (rc == 0)
494 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400495 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400496neg_exit:
497 free_rsp_buf(resp_buftype, rsp);
498 return rc;
499}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400500
Steve Frenchff1c0382013-11-19 23:44:46 -0600501int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
502{
503 int rc = 0;
504 struct validate_negotiate_info_req vneg_inbuf;
505 struct validate_negotiate_info_rsp *pneg_rsp;
506 u32 rsplen;
507
508 cifs_dbg(FYI, "validate negotiate\n");
509
510 /*
511 * validation ioctl must be signed, so no point sending this if we
512 * can not sign it. We could eventually change this to selectively
513 * sign just this, the first and only signed request on a connection.
514 * This is good enough for now since a user who wants better security
515 * would also enable signing on the mount. Having validation of
516 * negotiate info for signed connections helps reduce attack vectors
517 */
518 if (tcon->ses->server->sign == false)
519 return 0; /* validation requires signing */
520
521 vneg_inbuf.Capabilities =
522 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100523 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
524 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600525
526 if (tcon->ses->sign)
527 vneg_inbuf.SecurityMode =
528 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
529 else if (global_secflags & CIFSSEC_MAY_SIGN)
530 vneg_inbuf.SecurityMode =
531 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
532 else
533 vneg_inbuf.SecurityMode = 0;
534
535 vneg_inbuf.DialectCount = cpu_to_le16(1);
536 vneg_inbuf.Dialects[0] =
537 cpu_to_le16(tcon->ses->server->vals->protocol_id);
538
539 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
540 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
541 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
542 (char **)&pneg_rsp, &rsplen);
543
544 if (rc != 0) {
545 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
546 return -EIO;
547 }
548
549 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
550 cifs_dbg(VFS, "invalid size of protocol negotiate response\n");
551 return -EIO;
552 }
553
554 /* check validate negotiate info response matches what we got earlier */
555 if (pneg_rsp->Dialect !=
556 cpu_to_le16(tcon->ses->server->vals->protocol_id))
557 goto vneg_out;
558
559 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
560 goto vneg_out;
561
562 /* do not validate server guid because not saved at negprot time yet */
563
564 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
565 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
566 goto vneg_out;
567
568 /* validate negotiate successful */
569 cifs_dbg(FYI, "validate negotiate info successful\n");
570 return 0;
571
572vneg_out:
573 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
574 return -EIO;
575}
576
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400577int
578SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
579 const struct nls_table *nls_cp)
580{
581 struct smb2_sess_setup_req *req;
582 struct smb2_sess_setup_rsp *rsp = NULL;
583 struct kvec iov[2];
584 int rc = 0;
Namjae Jeon7de975e2014-08-20 19:39:41 +0900585 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400586 __le32 phase = NtLmNegotiate; /* NTLMSSP, if needed, is multistage */
Jeff Layton3534b852013-05-24 07:41:01 -0400587 struct TCP_Server_Info *server = ses->server;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400588 u16 blob_length = 0;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500589 struct key *spnego_key = NULL;
590 char *security_blob = NULL;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400591 char *ntlmssp_blob = NULL;
592 bool use_spnego = false; /* else use raw ntlmssp */
593
Joe Perchesf96637b2013-05-04 22:12:25 -0500594 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400595
Jeff Layton3534b852013-05-24 07:41:01 -0400596 if (!server) {
597 WARN(1, "%s: server is NULL!\n", __func__);
598 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400599 }
600
601 /*
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -0500602 * If we are here due to reconnect, free per-smb session key
603 * in case signing was required.
604 */
605 kfree(ses->auth_key.response);
606 ses->auth_key.response = NULL;
607
608 /*
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400609 * If memory allocation is successful, caller of this function
610 * frees it.
611 */
612 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
613 if (!ses->ntlmssp)
614 return -ENOMEM;
Shirish Pargaonkar5c234aa2013-08-29 08:35:10 -0500615 ses->ntlmssp->sesskey_per_smbsess = true;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400616
Jeff Layton3f618222013-06-12 19:52:14 -0500617 /* FIXME: allow for other auth types besides NTLMSSP (e.g. krb5) */
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500618 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
619 ses->sectype = RawNTLMSSP;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400620
621ssetup_ntlmssp_authenticate:
622 if (phase == NtLmChallenge)
623 phase = NtLmAuthenticate; /* if ntlmssp, now final phase */
624
625 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
626 if (rc)
627 return rc;
628
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400629 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
Steve Frencheed0e172015-02-06 00:03:52 -0600630 req->Flags = 0; /* MBZ */
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400631 /* to enable echos and oplocks */
632 req->hdr.CreditRequest = cpu_to_le16(3);
633
634 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400635 if (server->sign)
636 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
637 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
638 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
639 else
640 req->SecurityMode = 0;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400641
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400642 req->Capabilities = 0;
643 req->Channel = 0; /* MBZ */
644
645 iov[0].iov_base = (char *)req;
646 /* 4 for rfc1002 length field and 1 for pad */
647 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500648
649 if (ses->sectype == Kerberos) {
650#ifdef CONFIG_CIFS_UPCALL
651 struct cifs_spnego_msg *msg;
652
653 spnego_key = cifs_get_spnego_key(ses);
654 if (IS_ERR(spnego_key)) {
655 rc = PTR_ERR(spnego_key);
656 spnego_key = NULL;
657 goto ssetup_exit;
658 }
659
David Howells146aa8b2015-10-21 14:04:48 +0100660 msg = spnego_key->payload.data[0];
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500661 /*
662 * check version field to make sure that cifs.upcall is
663 * sending us a response in an expected form
664 */
665 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
666 cifs_dbg(VFS,
667 "bad cifs.upcall version. Expected %d got %d",
668 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
669 rc = -EKEYREJECTED;
670 goto ssetup_exit;
671 }
672 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
673 GFP_KERNEL);
674 if (!ses->auth_key.response) {
675 cifs_dbg(VFS,
676 "Kerberos can't allocate (%u bytes) memory",
677 msg->sesskey_len);
678 rc = -ENOMEM;
679 goto ssetup_exit;
680 }
681 ses->auth_key.len = msg->sesskey_len;
682 blob_length = msg->secblob_len;
683 iov[1].iov_base = msg->data + msg->sesskey_len;
684 iov[1].iov_len = blob_length;
685#else
686 rc = -EOPNOTSUPP;
687 goto ssetup_exit;
688#endif /* CONFIG_CIFS_UPCALL */
689 } else if (phase == NtLmNegotiate) { /* if not krb5 must be ntlmssp */
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400690 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
691 GFP_KERNEL);
692 if (ntlmssp_blob == NULL) {
693 rc = -ENOMEM;
694 goto ssetup_exit;
695 }
696 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
697 if (use_spnego) {
698 /* blob_length = build_spnego_ntlmssp_blob(
699 &security_blob,
700 sizeof(struct _NEGOTIATE_MESSAGE),
701 ntlmssp_blob); */
702 /* BB eventually need to add this */
Joe Perchesf96637b2013-05-04 22:12:25 -0500703 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400704 rc = -EOPNOTSUPP;
705 kfree(ntlmssp_blob);
706 goto ssetup_exit;
707 } else {
708 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
709 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
710 security_blob = ntlmssp_blob;
711 }
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500712 iov[1].iov_base = security_blob;
713 iov[1].iov_len = blob_length;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400714 } else if (phase == NtLmAuthenticate) {
715 req->hdr.SessionId = ses->Suid;
716 ntlmssp_blob = kzalloc(sizeof(struct _NEGOTIATE_MESSAGE) + 500,
717 GFP_KERNEL);
718 if (ntlmssp_blob == NULL) {
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400719 rc = -ENOMEM;
720 goto ssetup_exit;
721 }
722 rc = build_ntlmssp_auth_blob(ntlmssp_blob, &blob_length, ses,
723 nls_cp);
724 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500725 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n",
726 rc);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400727 goto ssetup_exit; /* BB double check error handling */
728 }
729 if (use_spnego) {
730 /* blob_length = build_spnego_ntlmssp_blob(
731 &security_blob,
732 blob_length,
733 ntlmssp_blob); */
Joe Perchesf96637b2013-05-04 22:12:25 -0500734 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400735 rc = -EOPNOTSUPP;
736 kfree(ntlmssp_blob);
737 goto ssetup_exit;
738 } else {
739 security_blob = ntlmssp_blob;
740 }
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500741 iov[1].iov_base = security_blob;
742 iov[1].iov_len = blob_length;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400743 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -0500744 cifs_dbg(VFS, "illegal ntlmssp phase\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400745 rc = -EIO;
746 goto ssetup_exit;
747 }
748
749 /* Testing shows that buffer offset must be at location of Buffer[0] */
750 req->SecurityBufferOffset =
751 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
752 1 /* pad */ - 4 /* rfc1001 len */);
753 req->SecurityBufferLength = cpu_to_le16(blob_length);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400754
755 inc_rfc1001_len(req, blob_length - 1 /* pad */);
756
757 /* BB add code to build os and lm fields */
758
Steve French6d8b59d2012-12-08 22:36:29 -0600759 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype,
760 CIFS_LOG_ERROR | CIFS_NEG_OP);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400761
762 kfree(security_blob);
763 rsp = (struct smb2_sess_setup_rsp *)iov[0].iov_base;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500764 ses->Suid = rsp->hdr.SessionId;
Pavel Shilovsky4ca3a992012-09-25 11:00:09 +0400765 if (resp_buftype != CIFS_NO_BUFFER &&
766 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) {
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400767 if (phase != NtLmNegotiate) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500768 cifs_dbg(VFS, "Unexpected more processing error\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400769 goto ssetup_exit;
770 }
771 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
Pavel Shilovsky4ca3a992012-09-25 11:00:09 +0400772 le16_to_cpu(rsp->SecurityBufferOffset)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500773 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
774 le16_to_cpu(rsp->SecurityBufferOffset));
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400775 rc = -EIO;
776 goto ssetup_exit;
777 }
778
779 /* NTLMSSP Negotiate sent now processing challenge (response) */
780 phase = NtLmChallenge; /* process ntlmssp challenge */
781 rc = 0; /* MORE_PROCESSING is not an error here but expected */
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400782 rc = decode_ntlmssp_challenge(rsp->Buffer,
783 le16_to_cpu(rsp->SecurityBufferLength), ses);
784 }
785
786 /*
787 * BB eventually add code for SPNEGO decoding of NtlmChallenge blob,
788 * but at least the raw NTLMSSP case works.
789 */
790 /*
791 * No tcon so can't do
792 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
793 */
794 if (rc != 0)
795 goto ssetup_exit;
796
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400797 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Steve French0cbaa532013-11-15 23:50:24 -0600798 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
799 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400800ssetup_exit:
801 free_rsp_buf(resp_buftype, rsp);
802
803 /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
804 if ((phase == NtLmChallenge) && (rc == 0))
805 goto ssetup_ntlmssp_authenticate;
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -0500806
807 if (!rc) {
808 mutex_lock(&server->srv_mutex);
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500809 if (server->sign && server->ops->generate_signingkey) {
810 rc = server->ops->generate_signingkey(ses);
811 kfree(ses->auth_key.response);
812 ses->auth_key.response = NULL;
813 if (rc) {
814 cifs_dbg(FYI,
815 "SMB3 session key generation failed\n");
816 mutex_unlock(&server->srv_mutex);
817 goto keygen_exit;
818 }
819 }
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -0500820 if (!server->session_estab) {
821 server->sequence_number = 0x2;
822 server->session_estab = true;
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -0500823 }
824 mutex_unlock(&server->srv_mutex);
825
826 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
827 spin_lock(&GlobalMid_Lock);
828 ses->status = CifsGood;
829 ses->need_reconnect = false;
830 spin_unlock(&GlobalMid_Lock);
831 }
832
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500833keygen_exit:
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -0500834 if (!server->sign) {
835 kfree(ses->auth_key.response);
836 ses->auth_key.response = NULL;
837 }
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500838 if (spnego_key) {
839 key_invalidate(spnego_key);
840 key_put(spnego_key);
841 }
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -0500842 kfree(ses->ntlmssp);
843
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400844 return rc;
845}
846
847int
848SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
849{
850 struct smb2_logoff_req *req; /* response is also trivial struct */
851 int rc = 0;
852 struct TCP_Server_Info *server;
853
Joe Perchesf96637b2013-05-04 22:12:25 -0500854 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400855
856 if (ses && (ses->server))
857 server = ses->server;
858 else
859 return -EIO;
860
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -0500861 /* no need to send SMB logoff if uid already closed due to reconnect */
862 if (ses->need_reconnect)
863 goto smb2_session_already_dead;
864
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400865 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
866 if (rc)
867 return rc;
868
869 /* since no tcon, smb2_init can not do this, so do here */
870 req->hdr.SessionId = ses->Suid;
Jeff Layton38d77c52013-05-26 07:01:00 -0400871 if (server->sign)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700872 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400873
874 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
875 /*
876 * No tcon so can't do
877 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
878 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -0500879
880smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400881 return rc;
882}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400883
884static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
885{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400886 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400887}
888
889#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
890
Steve Frenchde9f68d2013-11-15 11:26:24 -0600891/* These are similar values to what Windows uses */
892static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
893{
894 tcon->max_chunks = 256;
895 tcon->max_bytes_chunk = 1048576;
896 tcon->max_bytes_copy = 16777216;
897}
898
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400899int
900SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
901 struct cifs_tcon *tcon, const struct nls_table *cp)
902{
903 struct smb2_tree_connect_req *req;
904 struct smb2_tree_connect_rsp *rsp = NULL;
905 struct kvec iov[2];
906 int rc = 0;
907 int resp_buftype;
908 int unc_path_len;
909 struct TCP_Server_Info *server;
910 __le16 *unc_path = NULL;
911
Joe Perchesf96637b2013-05-04 22:12:25 -0500912 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400913
914 if ((ses->server) && tree)
915 server = ses->server;
916 else
917 return -EIO;
918
919 if (tcon && tcon->bad_network_name)
920 return -ENOENT;
921
Steve Frenchff9f84b2015-09-26 09:48:58 -0500922 if ((tcon && tcon->seal) &&
Steve French88627142015-09-22 03:16:27 -0500923 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
924 cifs_dbg(VFS, "encryption requested but no server support");
925 return -EOPNOTSUPP;
926 }
927
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400928 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
929 if (unc_path == NULL)
930 return -ENOMEM;
931
932 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
933 unc_path_len *= 2;
934 if (unc_path_len < 2) {
935 kfree(unc_path);
936 return -EINVAL;
937 }
938
939 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
940 if (rc) {
941 kfree(unc_path);
942 return rc;
943 }
944
945 if (tcon == NULL) {
946 /* since no tcon, smb2_init can not do this, so do here */
947 req->hdr.SessionId = ses->Suid;
948 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
949 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
950 }
951
952 iov[0].iov_base = (char *)req;
953 /* 4 for rfc1002 length field and 1 for pad */
954 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
955
956 /* Testing shows that buffer offset must be at location of Buffer[0] */
957 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
958 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
959 req->PathLength = cpu_to_le16(unc_path_len - 2);
960 iov[1].iov_base = unc_path;
961 iov[1].iov_len = unc_path_len;
962
963 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
964
965 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
966 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
967
968 if (rc != 0) {
969 if (tcon) {
970 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
971 tcon->need_reconnect = true;
972 }
973 goto tcon_error_exit;
974 }
975
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400976 if (tcon == NULL) {
977 ses->ipc_tid = rsp->hdr.TreeId;
978 goto tcon_exit;
979 }
980
981 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
Joe Perchesf96637b2013-05-04 22:12:25 -0500982 cifs_dbg(FYI, "connection to disk share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400983 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
984 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -0500985 cifs_dbg(FYI, "connection to pipe share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400986 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
987 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -0500988 cifs_dbg(FYI, "connection to printer\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400989 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -0500990 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400991 rc = -EOPNOTSUPP;
992 goto tcon_error_exit;
993 }
994
995 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -0500996 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400997 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
998 tcon->tidStatus = CifsGood;
999 tcon->need_reconnect = false;
1000 tcon->tid = rsp->hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001001 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001002
1003 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1004 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001005 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Steve Frenchde9f68d2013-11-15 11:26:24 -06001006 init_copy_chunk_defaults(tcon);
Steve French88627142015-09-22 03:16:27 -05001007 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1008 cifs_dbg(VFS, "Encrypted shares not supported");
Steve Frenchff1c0382013-11-19 23:44:46 -06001009 if (tcon->ses->server->ops->validate_negotiate)
1010 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001011tcon_exit:
1012 free_rsp_buf(resp_buftype, rsp);
1013 kfree(unc_path);
1014 return rc;
1015
1016tcon_error_exit:
1017 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001018 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Steve French18f39e72014-08-17 00:22:24 -05001019 if (tcon)
1020 tcon->bad_network_name = true;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001021 }
1022 goto tcon_exit;
1023}
1024
1025int
1026SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1027{
1028 struct smb2_tree_disconnect_req *req; /* response is trivial */
1029 int rc = 0;
1030 struct TCP_Server_Info *server;
1031 struct cifs_ses *ses = tcon->ses;
1032
Joe Perchesf96637b2013-05-04 22:12:25 -05001033 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001034
1035 if (ses && (ses->server))
1036 server = ses->server;
1037 else
1038 return -EIO;
1039
1040 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1041 return 0;
1042
1043 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1044 if (rc)
1045 return rc;
1046
1047 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
1048 if (rc)
1049 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1050
1051 return rc;
1052}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001053
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001054
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001055static struct create_durable *
1056create_durable_buf(void)
1057{
1058 struct create_durable *buf;
1059
1060 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1061 if (!buf)
1062 return NULL;
1063
1064 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001065 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001066 buf->ccontext.DataLength = cpu_to_le32(16);
1067 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1068 (struct create_durable, Name));
1069 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001070 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001071 buf->Name[0] = 'D';
1072 buf->Name[1] = 'H';
1073 buf->Name[2] = 'n';
1074 buf->Name[3] = 'Q';
1075 return buf;
1076}
1077
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001078static struct create_durable *
1079create_reconnect_durable_buf(struct cifs_fid *fid)
1080{
1081 struct create_durable *buf;
1082
1083 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1084 if (!buf)
1085 return NULL;
1086
1087 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1088 (struct create_durable, Data));
1089 buf->ccontext.DataLength = cpu_to_le32(16);
1090 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1091 (struct create_durable, Name));
1092 buf->ccontext.NameLength = cpu_to_le16(4);
1093 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1094 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001095 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001096 buf->Name[0] = 'D';
1097 buf->Name[1] = 'H';
1098 buf->Name[2] = 'n';
1099 buf->Name[3] = 'C';
1100 return buf;
1101}
1102
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001103static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001104parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1105 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001106{
1107 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001108 struct create_context *cc;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001109 unsigned int next = 0;
1110 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001111
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001112 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001113 cc = (struct create_context *)data_offset;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001114 do {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001115 cc = (struct create_context *)((char *)cc + next);
1116 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1117 if (le16_to_cpu(cc->NameLength) != 4 ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001118 strncmp(name, "RqLs", 4)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001119 next = le32_to_cpu(cc->Next);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001120 continue;
1121 }
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001122 return server->ops->parse_lease_buf(cc, epoch);
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001123 } while (next != 0);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001124
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001125 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001126}
1127
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001128static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001129add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1130 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001131{
1132 struct smb2_create_req *req = iov[0].iov_base;
1133 unsigned int num = *num_iovec;
1134
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001135 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001136 if (iov[num].iov_base == NULL)
1137 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001138 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001139 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1140 if (!req->CreateContextsOffset)
1141 req->CreateContextsOffset = cpu_to_le32(
1142 sizeof(struct smb2_create_req) - 4 +
1143 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001144 le32_add_cpu(&req->CreateContextsLength,
1145 server->vals->create_lease_size);
1146 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001147 *num_iovec = num + 1;
1148 return 0;
1149}
1150
Steve Frenchb56eae42015-11-03 09:26:27 -06001151static struct create_durable_v2 *
1152create_durable_v2_buf(struct cifs_fid *pfid)
1153{
1154 struct create_durable_v2 *buf;
1155
1156 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1157 if (!buf)
1158 return NULL;
1159
1160 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1161 (struct create_durable_v2, dcontext));
1162 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1163 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1164 (struct create_durable_v2, Name));
1165 buf->ccontext.NameLength = cpu_to_le16(4);
1166
1167 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1168 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1169 get_random_bytes(buf->dcontext.CreateGuid, 16);
1170 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1171
1172 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1173 buf->Name[0] = 'D';
1174 buf->Name[1] = 'H';
1175 buf->Name[2] = '2';
1176 buf->Name[3] = 'Q';
1177 return buf;
1178}
1179
1180static struct create_durable_handle_reconnect_v2 *
1181create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1182{
1183 struct create_durable_handle_reconnect_v2 *buf;
1184
1185 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1186 GFP_KERNEL);
1187 if (!buf)
1188 return NULL;
1189
1190 buf->ccontext.DataOffset =
1191 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1192 dcontext));
1193 buf->ccontext.DataLength =
1194 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1195 buf->ccontext.NameOffset =
1196 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1197 Name));
1198 buf->ccontext.NameLength = cpu_to_le16(4);
1199
1200 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1201 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1202 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1203 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1204
1205 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1206 buf->Name[0] = 'D';
1207 buf->Name[1] = 'H';
1208 buf->Name[2] = '2';
1209 buf->Name[3] = 'C';
1210 return buf;
1211}
1212
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001213static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001214add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001215 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001216{
1217 struct smb2_create_req *req = iov[0].iov_base;
1218 unsigned int num = *num_iovec;
1219
Steve Frenchb56eae42015-11-03 09:26:27 -06001220 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1221 if (iov[num].iov_base == NULL)
1222 return -ENOMEM;
1223 iov[num].iov_len = sizeof(struct create_durable_v2);
1224 if (!req->CreateContextsOffset)
1225 req->CreateContextsOffset =
1226 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1227 iov[1].iov_len);
1228 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1229 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1230 *num_iovec = num + 1;
1231 return 0;
1232}
1233
1234static int
1235add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1236 struct cifs_open_parms *oparms)
1237{
1238 struct smb2_create_req *req = iov[0].iov_base;
1239 unsigned int num = *num_iovec;
1240
1241 /* indicate that we don't need to relock the file */
1242 oparms->reconnect = false;
1243
1244 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1245 if (iov[num].iov_base == NULL)
1246 return -ENOMEM;
1247 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1248 if (!req->CreateContextsOffset)
1249 req->CreateContextsOffset =
1250 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1251 iov[1].iov_len);
1252 le32_add_cpu(&req->CreateContextsLength,
1253 sizeof(struct create_durable_handle_reconnect_v2));
1254 inc_rfc1001_len(&req->hdr,
1255 sizeof(struct create_durable_handle_reconnect_v2));
1256 *num_iovec = num + 1;
1257 return 0;
1258}
1259
1260static int
1261add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1262 struct cifs_open_parms *oparms, bool use_persistent)
1263{
1264 struct smb2_create_req *req = iov[0].iov_base;
1265 unsigned int num = *num_iovec;
1266
1267 if (use_persistent) {
1268 if (oparms->reconnect)
1269 return add_durable_reconnect_v2_context(iov, num_iovec,
1270 oparms);
1271 else
1272 return add_durable_v2_context(iov, num_iovec, oparms);
1273 }
1274
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001275 if (oparms->reconnect) {
1276 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1277 /* indicate that we don't need to relock the file */
1278 oparms->reconnect = false;
1279 } else
1280 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001281 if (iov[num].iov_base == NULL)
1282 return -ENOMEM;
1283 iov[num].iov_len = sizeof(struct create_durable);
1284 if (!req->CreateContextsOffset)
1285 req->CreateContextsOffset =
1286 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1287 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001288 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001289 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1290 *num_iovec = num + 1;
1291 return 0;
1292}
1293
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001294int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001295SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001296 __u8 *oplock, struct smb2_file_all_info *buf,
1297 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001298{
1299 struct smb2_create_req *req;
1300 struct smb2_create_rsp *rsp;
1301 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001302 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001303 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001304 struct kvec iov[4];
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001305 int resp_buftype;
1306 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001307 __le16 *copy_path = NULL;
1308 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001309 int rc = 0;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001310 unsigned int num_iovecs = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001311 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001312 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001313
Joe Perchesf96637b2013-05-04 22:12:25 -05001314 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001315
1316 if (ses && (ses->server))
1317 server = ses->server;
1318 else
1319 return -EIO;
1320
1321 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1322 if (rc)
1323 return rc;
1324
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001325 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001326 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001327 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1328 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001329
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001330 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001331 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001332 /* File attributes ignored on open (used in create though) */
1333 req->FileAttributes = cpu_to_le32(file_attributes);
1334 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001335 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1336 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001337 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001338 /* do not count rfc1001 len field */
1339 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001340
1341 iov[0].iov_base = (char *)req;
1342 /* 4 for rfc1002 length field */
1343 iov[0].iov_len = get_rfc1002_length(req) + 4;
1344
1345 /* MUST set path len (NameLength) to 0 opening root of share */
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001346 req->NameLength = cpu_to_le16(uni_path_len - 2);
1347 /* -1 since last byte is buf[0] which is sent below (path) */
1348 iov[0].iov_len--;
1349 if (uni_path_len % 8 != 0) {
1350 copy_size = uni_path_len / 8 * 8;
1351 if (copy_size < uni_path_len)
1352 copy_size += 8;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001353
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001354 copy_path = kzalloc(copy_size, GFP_KERNEL);
1355 if (!copy_path)
1356 return -ENOMEM;
1357 memcpy((char *)copy_path, (const char *)path,
1358 uni_path_len);
1359 uni_path_len = copy_size;
1360 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001361 }
1362
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001363 iov[1].iov_len = uni_path_len;
1364 iov[1].iov_base = path;
1365 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1366 inc_rfc1001_len(req, uni_path_len - 1);
1367
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001368 if (!server->oplocks)
1369 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1370
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001371 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001372 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1373 req->RequestedOplockLevel = *oplock;
1374 else {
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001375 rc = add_lease_context(server, iov, &num_iovecs, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001376 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001377 cifs_small_buf_release(req);
1378 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001379 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001380 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001381 lc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001382 }
1383
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001384 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1385 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001386 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001387 struct create_context *ccontext =
1388 (struct create_context *)iov[num_iovecs-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001389 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001390 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001391 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001392
1393 rc = add_durable_context(iov, &num_iovecs, oparms,
1394 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001395 if (rc) {
1396 cifs_small_buf_release(req);
1397 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001398 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001399 return rc;
1400 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001401 dhc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001402 }
1403
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001404 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1405 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1406
1407 if (rc != 0) {
1408 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001409 if (err_buf)
1410 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1411 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001412 goto creat_exit;
1413 }
1414
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001415 oparms->fid->persistent_fid = rsp->PersistentFileId;
1416 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001417
1418 if (buf) {
1419 memcpy(buf, &rsp->CreationTime, 32);
1420 buf->AllocationSize = rsp->AllocationSize;
1421 buf->EndOfFile = rsp->EndofFile;
1422 buf->Attributes = rsp->FileAttributes;
1423 buf->NumberOfLinks = cpu_to_le32(1);
1424 buf->DeletePending = 0;
1425 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001426
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001427 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001428 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001429 else
1430 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001431creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001432 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001433 kfree(lc_buf);
1434 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001435 free_rsp_buf(resp_buftype, rsp);
1436 return rc;
1437}
1438
Steve French4a72daf2013-06-25 00:20:49 -05001439/*
1440 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1441 */
1442int
1443SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1444 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1445 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1446{
1447 struct smb2_ioctl_req *req;
1448 struct smb2_ioctl_rsp *rsp;
1449 struct TCP_Server_Info *server;
Steve French8e353102015-03-26 19:47:02 -05001450 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001451 struct kvec iov[2];
1452 int resp_buftype;
1453 int num_iovecs;
1454 int rc = 0;
1455
1456 cifs_dbg(FYI, "SMB2 IOCTL\n");
1457
Steve French3d1a3742014-08-11 21:05:25 -05001458 if (out_data != NULL)
1459 *out_data = NULL;
1460
Steve French4a72daf2013-06-25 00:20:49 -05001461 /* zero out returned data len, in case of error */
1462 if (plen)
1463 *plen = 0;
1464
Steve French8e353102015-03-26 19:47:02 -05001465 if (tcon)
1466 ses = tcon->ses;
1467 else
1468 return -EIO;
1469
Steve French4a72daf2013-06-25 00:20:49 -05001470 if (ses && (ses->server))
1471 server = ses->server;
1472 else
1473 return -EIO;
1474
1475 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1476 if (rc)
1477 return rc;
1478
1479 req->CtlCode = cpu_to_le32(opcode);
1480 req->PersistentFileId = persistent_fid;
1481 req->VolatileFileId = volatile_fid;
1482
1483 if (indatalen) {
1484 req->InputCount = cpu_to_le32(indatalen);
1485 /* do not set InputOffset if no input data */
1486 req->InputOffset =
1487 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1488 iov[1].iov_base = in_data;
1489 iov[1].iov_len = indatalen;
1490 num_iovecs = 2;
1491 } else
1492 num_iovecs = 1;
1493
1494 req->OutputOffset = 0;
1495 req->OutputCount = 0; /* MBZ */
1496
1497 /*
1498 * Could increase MaxOutputResponse, but that would require more
1499 * than one credit. Windows typically sets this smaller, but for some
1500 * ioctls it may be useful to allow server to send more. No point
1501 * limiting what the server can send as long as fits in one credit
1502 */
1503 req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1504
1505 if (is_fsctl)
1506 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1507 else
1508 req->Flags = 0;
1509
1510 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001511
Steve French7ff8d452013-10-14 00:44:19 -05001512 /*
1513 * If no input data, the size of ioctl struct in
1514 * protocol spec still includes a 1 byte data buffer,
1515 * but if input data passed to ioctl, we do not
1516 * want to double count this, so we do not send
1517 * the dummy one byte of data in iovec[0] if sending
1518 * input data (in iovec[1]). We also must add 4 bytes
1519 * in first iovec to allow for rfc1002 length field.
1520 */
1521
1522 if (indatalen) {
1523 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1524 inc_rfc1001_len(req, indatalen - 1);
1525 } else
1526 iov[0].iov_len = get_rfc1002_length(req) + 4;
1527
Steve French4a72daf2013-06-25 00:20:49 -05001528
1529 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1530 rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1531
Steve French9bf0c9c2013-11-16 18:05:28 -06001532 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001533 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001534 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001535 } else if (rc == -EINVAL) {
1536 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1537 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001538 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001539 goto ioctl_exit;
1540 }
Steve French4a72daf2013-06-25 00:20:49 -05001541 }
1542
1543 /* check if caller wants to look at return data or just return rc */
1544 if ((plen == NULL) || (out_data == NULL))
1545 goto ioctl_exit;
1546
1547 *plen = le32_to_cpu(rsp->OutputCount);
1548
1549 /* We check for obvious errors in the output buffer length and offset */
1550 if (*plen == 0)
1551 goto ioctl_exit; /* server returned no data */
1552 else if (*plen > 0xFF00) {
1553 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1554 *plen = 0;
1555 rc = -EIO;
1556 goto ioctl_exit;
1557 }
1558
1559 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1560 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1561 le32_to_cpu(rsp->OutputOffset));
1562 *plen = 0;
1563 rc = -EIO;
1564 goto ioctl_exit;
1565 }
1566
1567 *out_data = kmalloc(*plen, GFP_KERNEL);
1568 if (*out_data == NULL) {
1569 rc = -ENOMEM;
1570 goto ioctl_exit;
1571 }
1572
Steve French373512e2015-12-18 13:05:30 -06001573 memcpy(*out_data,
1574 (char *)&rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
Steve French4a72daf2013-06-25 00:20:49 -05001575 *plen);
1576ioctl_exit:
1577 free_rsp_buf(resp_buftype, rsp);
1578 return rc;
1579}
1580
Steve French64a5cfa2013-10-14 15:31:32 -05001581/*
1582 * Individual callers to ioctl worker function follow
1583 */
1584
1585int
1586SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1587 u64 persistent_fid, u64 volatile_fid)
1588{
1589 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05001590 struct compress_ioctl fsctl_input;
1591 char *ret_data = NULL;
1592
1593 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08001594 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05001595
1596 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1597 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1598 (char *)&fsctl_input /* data input */,
1599 2 /* in data len */, &ret_data /* out data */, NULL);
1600
1601 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05001602
1603 return rc;
1604}
1605
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001606int
1607SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1608 u64 persistent_fid, u64 volatile_fid)
1609{
1610 struct smb2_close_req *req;
1611 struct smb2_close_rsp *rsp;
1612 struct TCP_Server_Info *server;
1613 struct cifs_ses *ses = tcon->ses;
1614 struct kvec iov[1];
1615 int resp_buftype;
1616 int rc = 0;
1617
Joe Perchesf96637b2013-05-04 22:12:25 -05001618 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001619
1620 if (ses && (ses->server))
1621 server = ses->server;
1622 else
1623 return -EIO;
1624
1625 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1626 if (rc)
1627 return rc;
1628
1629 req->PersistentFileId = persistent_fid;
1630 req->VolatileFileId = volatile_fid;
1631
1632 iov[0].iov_base = (char *)req;
1633 /* 4 for rfc1002 length field */
1634 iov[0].iov_len = get_rfc1002_length(req) + 4;
1635
1636 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1637 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1638
1639 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09001640 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001641 goto close_exit;
1642 }
1643
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001644 /* BB FIXME - decode close response, update inode for caching */
1645
1646close_exit:
1647 free_rsp_buf(resp_buftype, rsp);
1648 return rc;
1649}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001650
1651static int
1652validate_buf(unsigned int offset, unsigned int buffer_length,
1653 struct smb2_hdr *hdr, unsigned int min_buf_size)
1654
1655{
1656 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1657 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1658 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1659 char *end_of_buf = begin_of_buf + buffer_length;
1660
1661
1662 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001663 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1664 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001665 return -EINVAL;
1666 }
1667
1668 /* check if beyond RFC1001 maximum length */
1669 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001670 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1671 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001672 return -EINVAL;
1673 }
1674
1675 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001676 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001677 return -EINVAL;
1678 }
1679
1680 return 0;
1681}
1682
1683/*
1684 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1685 * Caller must free buffer.
1686 */
1687static int
1688validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1689 struct smb2_hdr *hdr, unsigned int minbufsize,
1690 char *data)
1691
1692{
1693 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1694 int rc;
1695
1696 if (!data)
1697 return -EINVAL;
1698
1699 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1700 if (rc)
1701 return rc;
1702
1703 memcpy(data, begin_of_buf, buffer_length);
1704
1705 return 0;
1706}
1707
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001708static int
1709query_info(const unsigned int xid, struct cifs_tcon *tcon,
1710 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1711 size_t output_len, size_t min_len, void *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001712{
1713 struct smb2_query_info_req *req;
1714 struct smb2_query_info_rsp *rsp = NULL;
1715 struct kvec iov[2];
1716 int rc = 0;
1717 int resp_buftype;
1718 struct TCP_Server_Info *server;
1719 struct cifs_ses *ses = tcon->ses;
1720
Joe Perchesf96637b2013-05-04 22:12:25 -05001721 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001722
1723 if (ses && (ses->server))
1724 server = ses->server;
1725 else
1726 return -EIO;
1727
1728 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1729 if (rc)
1730 return rc;
1731
1732 req->InfoType = SMB2_O_INFO_FILE;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001733 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001734 req->PersistentFileId = persistent_fid;
1735 req->VolatileFileId = volatile_fid;
1736 /* 4 for rfc1002 length field and 1 for Buffer */
1737 req->InputBufferOffset =
1738 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001739 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001740
1741 iov[0].iov_base = (char *)req;
1742 /* 4 for rfc1002 length field */
1743 iov[0].iov_len = get_rfc1002_length(req) + 4;
1744
1745 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04001746 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1747
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001748 if (rc) {
1749 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1750 goto qinf_exit;
1751 }
1752
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001753 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1754 le32_to_cpu(rsp->OutputBufferLength),
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001755 &rsp->hdr, min_len, data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001756
1757qinf_exit:
1758 free_rsp_buf(resp_buftype, rsp);
1759 return rc;
1760}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001761
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001762int
1763SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1764 u64 persistent_fid, u64 volatile_fid,
1765 struct smb2_file_all_info *data)
1766{
1767 return query_info(xid, tcon, persistent_fid, volatile_fid,
1768 FILE_ALL_INFORMATION,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04001769 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001770 sizeof(struct smb2_file_all_info), data);
1771}
1772
1773int
1774SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1775 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1776{
1777 return query_info(xid, tcon, persistent_fid, volatile_fid,
1778 FILE_INTERNAL_INFORMATION,
1779 sizeof(struct smb2_file_internal_info),
1780 sizeof(struct smb2_file_internal_info), uniqueid);
1781}
1782
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001783/*
1784 * This is a no-op for now. We're not really interested in the reply, but
1785 * rather in the fact that the server sent one and that server->lstrp
1786 * gets updated.
1787 *
1788 * FIXME: maybe we should consider checking that the reply matches request?
1789 */
1790static void
1791smb2_echo_callback(struct mid_q_entry *mid)
1792{
1793 struct TCP_Server_Info *server = mid->callback_data;
1794 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1795 unsigned int credits_received = 1;
1796
1797 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1798 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1799
Christopher Oo5fb4e282015-06-25 16:10:48 -07001800 mutex_lock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001801 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07001802 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001803 add_credits(server, credits_received, CIFS_ECHO_OP);
1804}
1805
1806int
1807SMB2_echo(struct TCP_Server_Info *server)
1808{
1809 struct smb2_echo_req *req;
1810 int rc = 0;
1811 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07001812 struct smb_rqst rqst = { .rq_iov = &iov,
1813 .rq_nvec = 1 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001814
Joe Perchesf96637b2013-05-04 22:12:25 -05001815 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001816
1817 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
1818 if (rc)
1819 return rc;
1820
1821 req->hdr.CreditRequest = cpu_to_le16(1);
1822
1823 iov.iov_base = (char *)req;
1824 /* 4 for rfc1002 length field */
1825 iov.iov_len = get_rfc1002_length(req) + 4;
1826
Jeff Laytonfec344e2012-09-18 16:20:35 -07001827 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001828 CIFS_ECHO_OP);
1829 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001830 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001831
1832 cifs_small_buf_release(req);
1833 return rc;
1834}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001835
1836int
1837SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1838 u64 volatile_fid)
1839{
1840 struct smb2_flush_req *req;
1841 struct TCP_Server_Info *server;
1842 struct cifs_ses *ses = tcon->ses;
1843 struct kvec iov[1];
1844 int resp_buftype;
1845 int rc = 0;
1846
Joe Perchesf96637b2013-05-04 22:12:25 -05001847 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001848
1849 if (ses && (ses->server))
1850 server = ses->server;
1851 else
1852 return -EIO;
1853
1854 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
1855 if (rc)
1856 return rc;
1857
1858 req->PersistentFileId = persistent_fid;
1859 req->VolatileFileId = volatile_fid;
1860
1861 iov[0].iov_base = (char *)req;
1862 /* 4 for rfc1002 length field */
1863 iov[0].iov_len = get_rfc1002_length(req) + 4;
1864
1865 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1866
Steve Frenchdfebe402015-03-27 01:00:06 -05001867 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001868 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
1869
1870 free_rsp_buf(resp_buftype, iov[0].iov_base);
1871 return rc;
1872}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001873
1874/*
1875 * To form a chain of read requests, any read requests after the first should
1876 * have the end_of_chain boolean set to true.
1877 */
1878static int
1879smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
1880 unsigned int remaining_bytes, int request_type)
1881{
1882 int rc = -EACCES;
1883 struct smb2_read_req *req = NULL;
1884
1885 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
1886 if (rc)
1887 return rc;
1888 if (io_parms->tcon->ses->server == NULL)
1889 return -ECONNABORTED;
1890
1891 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
1892
1893 req->PersistentFileId = io_parms->persistent_fid;
1894 req->VolatileFileId = io_parms->volatile_fid;
1895 req->ReadChannelInfoOffset = 0; /* reserved */
1896 req->ReadChannelInfoLength = 0; /* reserved */
1897 req->Channel = 0; /* reserved */
1898 req->MinimumCount = 0;
1899 req->Length = cpu_to_le32(io_parms->length);
1900 req->Offset = cpu_to_le64(io_parms->offset);
1901
1902 if (request_type & CHAINED_REQUEST) {
1903 if (!(request_type & END_OF_CHAIN)) {
1904 /* 4 for rfc1002 length field */
1905 req->hdr.NextCommand =
1906 cpu_to_le32(get_rfc1002_length(req) + 4);
1907 } else /* END_OF_CHAIN */
1908 req->hdr.NextCommand = 0;
1909 if (request_type & RELATED_REQUEST) {
1910 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
1911 /*
1912 * Related requests use info from previous read request
1913 * in chain.
1914 */
1915 req->hdr.SessionId = 0xFFFFFFFF;
1916 req->hdr.TreeId = 0xFFFFFFFF;
1917 req->PersistentFileId = 0xFFFFFFFF;
1918 req->VolatileFileId = 0xFFFFFFFF;
1919 }
1920 }
1921 if (remaining_bytes > io_parms->length)
1922 req->RemainingBytes = cpu_to_le32(remaining_bytes);
1923 else
1924 req->RemainingBytes = 0;
1925
1926 iov[0].iov_base = (char *)req;
1927 /* 4 for rfc1002 length field */
1928 iov[0].iov_len = get_rfc1002_length(req) + 4;
1929 return rc;
1930}
1931
1932static void
1933smb2_readv_callback(struct mid_q_entry *mid)
1934{
1935 struct cifs_readdata *rdata = mid->callback_data;
1936 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1937 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton58195752012-09-19 06:22:34 -07001938 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001939 unsigned int credits_received = 1;
Jeff Layton58195752012-09-19 06:22:34 -07001940 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Layton8321fec2012-09-19 06:22:32 -07001941 .rq_nvec = 1,
1942 .rq_pages = rdata->pages,
1943 .rq_npages = rdata->nr_pages,
1944 .rq_pagesz = rdata->pagesz,
1945 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001946
Joe Perchesf96637b2013-05-04 22:12:25 -05001947 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1948 __func__, mid->mid, mid->mid_state, rdata->result,
1949 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001950
1951 switch (mid->mid_state) {
1952 case MID_RESPONSE_RECEIVED:
1953 credits_received = le16_to_cpu(buf->CreditRequest);
1954 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04001955 if (server->sign) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001956 int rc;
1957
Jeff Layton0b688cf2012-09-18 16:20:34 -07001958 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001959 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001960 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1961 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001962 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001963 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04001964 task_io_account_read(rdata->got_bytes);
1965 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001966 break;
1967 case MID_REQUEST_SUBMITTED:
1968 case MID_RETRY_NEEDED:
1969 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04001970 if (server->sign && rdata->got_bytes)
1971 /* reset bytes number since we can not check a sign */
1972 rdata->got_bytes = 0;
1973 /* FIXME: should this be counted toward the initiating task? */
1974 task_io_account_read(rdata->got_bytes);
1975 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001976 break;
1977 default:
1978 if (rdata->result != -ENODATA)
1979 rdata->result = -EIO;
1980 }
1981
1982 if (rdata->result)
1983 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
1984
1985 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07001986 mutex_lock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001987 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07001988 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001989 add_credits(server, credits_received, 0);
1990}
1991
1992/* smb2_async_readv - send an async write, and set up mid to handle result */
1993int
1994smb2_async_readv(struct cifs_readdata *rdata)
1995{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04001996 int rc, flags = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001997 struct smb2_hdr *buf;
1998 struct cifs_io_parms io_parms;
Jeff Layton58195752012-09-19 06:22:34 -07001999 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Laytonfec344e2012-09-18 16:20:35 -07002000 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002001 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002002
Joe Perchesf96637b2013-05-04 22:12:25 -05002003 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2004 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002005
2006 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2007 io_parms.offset = rdata->offset;
2008 io_parms.length = rdata->bytes;
2009 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2010 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2011 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002012
2013 server = io_parms.tcon->ses->server;
2014
Jeff Layton58195752012-09-19 06:22:34 -07002015 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002016 if (rc) {
2017 if (rc == -EAGAIN && rdata->credits) {
2018 /* credits was reset by reconnect */
2019 rdata->credits = 0;
2020 /* reduce in_flight value since we won't send the req */
2021 spin_lock(&server->req_lock);
2022 server->in_flight--;
2023 spin_unlock(&server->req_lock);
2024 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002025 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002026 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002027
Jeff Layton58195752012-09-19 06:22:34 -07002028 buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002029 /* 4 for rfc1002 length field */
Jeff Layton58195752012-09-19 06:22:34 -07002030 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002031
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002032 if (rdata->credits) {
2033 buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2034 SMB2_MAX_BUFFER_SIZE));
2035 spin_lock(&server->req_lock);
2036 server->credits += rdata->credits -
2037 le16_to_cpu(buf->CreditCharge);
2038 spin_unlock(&server->req_lock);
2039 wake_up(&server->request_q);
2040 flags = CIFS_HAS_CREDITS;
2041 }
2042
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002043 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002044 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002045 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002046 rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002047 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002048 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002049 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2050 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002051
2052 cifs_small_buf_release(buf);
2053 return rc;
2054}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002055
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002056int
2057SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2058 unsigned int *nbytes, char **buf, int *buf_type)
2059{
2060 int resp_buftype, rc = -EACCES;
2061 struct smb2_read_rsp *rsp = NULL;
2062 struct kvec iov[1];
2063
2064 *nbytes = 0;
2065 rc = smb2_new_read_req(iov, io_parms, 0, 0);
2066 if (rc)
2067 return rc;
2068
2069 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
2070 &resp_buftype, CIFS_LOG_ERROR);
2071
2072 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
2073
2074 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
2075 free_rsp_buf(resp_buftype, iov[0].iov_base);
2076 return 0;
2077 }
2078
2079 if (rc) {
2080 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002081 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002082 } else {
2083 *nbytes = le32_to_cpu(rsp->DataLength);
2084 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2085 (*nbytes > io_parms->length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002086 cifs_dbg(FYI, "bad length %d for count %d\n",
2087 *nbytes, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002088 rc = -EIO;
2089 *nbytes = 0;
2090 }
2091 }
2092
2093 if (*buf) {
Steve French373512e2015-12-18 13:05:30 -06002094 memcpy(*buf, (char *)&rsp->hdr.ProtocolId + rsp->DataOffset,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002095 *nbytes);
2096 free_rsp_buf(resp_buftype, iov[0].iov_base);
2097 } else if (resp_buftype != CIFS_NO_BUFFER) {
2098 *buf = iov[0].iov_base;
2099 if (resp_buftype == CIFS_SMALL_BUFFER)
2100 *buf_type = CIFS_SMALL_BUFFER;
2101 else if (resp_buftype == CIFS_LARGE_BUFFER)
2102 *buf_type = CIFS_LARGE_BUFFER;
2103 }
2104 return rc;
2105}
2106
Pavel Shilovsky33319142012-09-18 16:20:29 -07002107/*
2108 * Check the mid_state and signature on received buffer (if any), and queue the
2109 * workqueue completion task.
2110 */
2111static void
2112smb2_writev_callback(struct mid_q_entry *mid)
2113{
2114 struct cifs_writedata *wdata = mid->callback_data;
2115 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002116 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002117 unsigned int written;
2118 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2119 unsigned int credits_received = 1;
2120
2121 switch (mid->mid_state) {
2122 case MID_RESPONSE_RECEIVED:
2123 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
2124 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2125 if (wdata->result != 0)
2126 break;
2127
2128 written = le32_to_cpu(rsp->DataLength);
2129 /*
2130 * Mask off high 16 bits when bytes written as returned
2131 * by the server is greater than bytes requested by the
2132 * client. OS/2 servers are known to set incorrect
2133 * CountHigh values.
2134 */
2135 if (written > wdata->bytes)
2136 written &= 0xFFFF;
2137
2138 if (written < wdata->bytes)
2139 wdata->result = -ENOSPC;
2140 else
2141 wdata->bytes = written;
2142 break;
2143 case MID_REQUEST_SUBMITTED:
2144 case MID_RETRY_NEEDED:
2145 wdata->result = -EAGAIN;
2146 break;
2147 default:
2148 wdata->result = -EIO;
2149 break;
2150 }
2151
2152 if (wdata->result)
2153 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2154
2155 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002156 mutex_lock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002157 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002158 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002159 add_credits(tcon->ses->server, credits_received, 0);
2160}
2161
2162/* smb2_async_writev - send an async write, and set up mid to handle result */
2163int
Steve French4a5c80d2014-02-07 20:45:12 -06002164smb2_async_writev(struct cifs_writedata *wdata,
2165 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002166{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002167 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002168 struct smb2_write_req *req = NULL;
2169 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002170 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002171 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002172 struct smb_rqst rqst;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002173
2174 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002175 if (rc) {
2176 if (rc == -EAGAIN && wdata->credits) {
2177 /* credits was reset by reconnect */
2178 wdata->credits = 0;
2179 /* reduce in_flight value since we won't send the req */
2180 spin_lock(&server->req_lock);
2181 server->in_flight--;
2182 spin_unlock(&server->req_lock);
2183 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002184 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002185 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002186
Pavel Shilovsky33319142012-09-18 16:20:29 -07002187 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
2188
2189 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2190 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2191 req->WriteChannelInfoOffset = 0;
2192 req->WriteChannelInfoLength = 0;
2193 req->Channel = 0;
2194 req->Offset = cpu_to_le64(wdata->offset);
2195 /* 4 for rfc1002 length field */
2196 req->DataOffset = cpu_to_le16(
2197 offsetof(struct smb2_write_req, Buffer) - 4);
2198 req->RemainingBytes = 0;
2199
2200 /* 4 for rfc1002 length field and 1 for Buffer */
Jeff Laytoneddb0792012-09-18 16:20:35 -07002201 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
2202 iov.iov_base = req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002203
Jeff Laytoneddb0792012-09-18 16:20:35 -07002204 rqst.rq_iov = &iov;
2205 rqst.rq_nvec = 1;
2206 rqst.rq_pages = wdata->pages;
2207 rqst.rq_npages = wdata->nr_pages;
2208 rqst.rq_pagesz = wdata->pagesz;
2209 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002210
Joe Perchesf96637b2013-05-04 22:12:25 -05002211 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2212 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002213
2214 req->Length = cpu_to_le32(wdata->bytes);
2215
2216 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2217
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002218 if (wdata->credits) {
2219 req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2220 SMB2_MAX_BUFFER_SIZE));
2221 spin_lock(&server->req_lock);
2222 server->credits += wdata->credits -
2223 le16_to_cpu(req->hdr.CreditCharge);
2224 spin_unlock(&server->req_lock);
2225 wake_up(&server->request_q);
2226 flags = CIFS_HAS_CREDITS;
2227 }
2228
Pavel Shilovsky33319142012-09-18 16:20:29 -07002229 kref_get(&wdata->refcount);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002230 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2231 flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002232
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002233 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002234 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002235 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2236 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002237
Pavel Shilovsky33319142012-09-18 16:20:29 -07002238async_writev_out:
2239 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002240 return rc;
2241}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002242
2243/*
2244 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2245 * The length field from io_parms must be at least 1 and indicates a number of
2246 * elements with data to write that begins with position 1 in iov array. All
2247 * data length is specified by count.
2248 */
2249int
2250SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2251 unsigned int *nbytes, struct kvec *iov, int n_vec)
2252{
2253 int rc = 0;
2254 struct smb2_write_req *req = NULL;
2255 struct smb2_write_rsp *rsp = NULL;
2256 int resp_buftype;
2257 *nbytes = 0;
2258
2259 if (n_vec < 1)
2260 return rc;
2261
2262 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2263 if (rc)
2264 return rc;
2265
2266 if (io_parms->tcon->ses->server == NULL)
2267 return -ECONNABORTED;
2268
2269 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2270
2271 req->PersistentFileId = io_parms->persistent_fid;
2272 req->VolatileFileId = io_parms->volatile_fid;
2273 req->WriteChannelInfoOffset = 0;
2274 req->WriteChannelInfoLength = 0;
2275 req->Channel = 0;
2276 req->Length = cpu_to_le32(io_parms->length);
2277 req->Offset = cpu_to_le64(io_parms->offset);
2278 /* 4 for rfc1002 length field */
2279 req->DataOffset = cpu_to_le16(
2280 offsetof(struct smb2_write_req, Buffer) - 4);
2281 req->RemainingBytes = 0;
2282
2283 iov[0].iov_base = (char *)req;
2284 /* 4 for rfc1002 length field and 1 for Buffer */
2285 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2286
2287 /* length of entire message including data to be written */
2288 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2289
2290 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2291 &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002292 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002293
2294 if (rc) {
2295 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002296 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002297 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002298 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002299
2300 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002301 return rc;
2302}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002303
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002304static unsigned int
2305num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2306{
2307 int len;
2308 unsigned int entrycount = 0;
2309 unsigned int next_offset = 0;
2310 FILE_DIRECTORY_INFO *entryptr;
2311
2312 if (bufstart == NULL)
2313 return 0;
2314
2315 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2316
2317 while (1) {
2318 entryptr = (FILE_DIRECTORY_INFO *)
2319 ((char *)entryptr + next_offset);
2320
2321 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002322 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002323 break;
2324 }
2325
2326 len = le32_to_cpu(entryptr->FileNameLength);
2327 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002328 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2329 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002330 break;
2331 }
2332
2333 *lastentry = (char *)entryptr;
2334 entrycount++;
2335
2336 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2337 if (!next_offset)
2338 break;
2339 }
2340
2341 return entrycount;
2342}
2343
2344/*
2345 * Readdir/FindFirst
2346 */
2347int
2348SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2349 u64 persistent_fid, u64 volatile_fid, int index,
2350 struct cifs_search_info *srch_inf)
2351{
2352 struct smb2_query_directory_req *req;
2353 struct smb2_query_directory_rsp *rsp = NULL;
2354 struct kvec iov[2];
2355 int rc = 0;
2356 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002357 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002358 unsigned char *bufptr;
2359 struct TCP_Server_Info *server;
2360 struct cifs_ses *ses = tcon->ses;
2361 __le16 asteriks = cpu_to_le16('*');
2362 char *end_of_smb;
2363 unsigned int output_size = CIFSMaxBufSize;
2364 size_t info_buf_size;
2365
2366 if (ses && (ses->server))
2367 server = ses->server;
2368 else
2369 return -EIO;
2370
2371 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2372 if (rc)
2373 return rc;
2374
2375 switch (srch_inf->info_level) {
2376 case SMB_FIND_FILE_DIRECTORY_INFO:
2377 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2378 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2379 break;
2380 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2381 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2382 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2383 break;
2384 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002385 cifs_dbg(VFS, "info level %u isn't supported\n",
2386 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002387 rc = -EINVAL;
2388 goto qdir_exit;
2389 }
2390
2391 req->FileIndex = cpu_to_le32(index);
2392 req->PersistentFileId = persistent_fid;
2393 req->VolatileFileId = volatile_fid;
2394
2395 len = 0x2;
2396 bufptr = req->Buffer;
2397 memcpy(bufptr, &asteriks, len);
2398
2399 req->FileNameOffset =
2400 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2401 req->FileNameLength = cpu_to_le16(len);
2402 /*
2403 * BB could be 30 bytes or so longer if we used SMB2 specific
2404 * buffer lengths, but this is safe and close enough.
2405 */
2406 output_size = min_t(unsigned int, output_size, server->maxBuf);
2407 output_size = min_t(unsigned int, output_size, 2 << 15);
2408 req->OutputBufferLength = cpu_to_le32(output_size);
2409
2410 iov[0].iov_base = (char *)req;
2411 /* 4 for RFC1001 length and 1 for Buffer */
2412 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2413
2414 iov[1].iov_base = (char *)(req->Buffer);
2415 iov[1].iov_len = len;
2416
2417 inc_rfc1001_len(req, len - 1 /* Buffer */);
2418
2419 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002420 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2421
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002422 if (rc) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04002423 if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2424 srch_inf->endOfSearch = true;
2425 rc = 0;
2426 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002427 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2428 goto qdir_exit;
2429 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002430
2431 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2432 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2433 info_buf_size);
2434 if (rc)
2435 goto qdir_exit;
2436
2437 srch_inf->unicode = true;
2438
2439 if (srch_inf->ntwrk_buf_start) {
2440 if (srch_inf->smallBuf)
2441 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2442 else
2443 cifs_buf_release(srch_inf->ntwrk_buf_start);
2444 }
2445 srch_inf->ntwrk_buf_start = (char *)rsp;
2446 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2447 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2448 /* 4 for rfc1002 length field */
2449 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2450 srch_inf->entries_in_buffer =
2451 num_entries(srch_inf->srch_entries_start, end_of_smb,
2452 &srch_inf->last_entry, info_buf_size);
2453 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05002454 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2455 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2456 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002457 if (resp_buftype == CIFS_LARGE_BUFFER)
2458 srch_inf->smallBuf = false;
2459 else if (resp_buftype == CIFS_SMALL_BUFFER)
2460 srch_inf->smallBuf = true;
2461 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002462 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002463
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002464 return rc;
2465
2466qdir_exit:
2467 free_rsp_buf(resp_buftype, rsp);
2468 return rc;
2469}
2470
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002471static int
2472send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002473 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002474 unsigned int num, void **data, unsigned int *size)
2475{
2476 struct smb2_set_info_req *req;
2477 struct smb2_set_info_rsp *rsp = NULL;
2478 struct kvec *iov;
2479 int rc = 0;
2480 int resp_buftype;
2481 unsigned int i;
2482 struct TCP_Server_Info *server;
2483 struct cifs_ses *ses = tcon->ses;
2484
2485 if (ses && (ses->server))
2486 server = ses->server;
2487 else
2488 return -EIO;
2489
2490 if (!num)
2491 return -EINVAL;
2492
2493 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2494 if (!iov)
2495 return -ENOMEM;
2496
2497 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2498 if (rc) {
2499 kfree(iov);
2500 return rc;
2501 }
2502
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002503 req->hdr.ProcessId = cpu_to_le32(pid);
2504
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002505 req->InfoType = SMB2_O_INFO_FILE;
2506 req->FileInfoClass = info_class;
2507 req->PersistentFileId = persistent_fid;
2508 req->VolatileFileId = volatile_fid;
2509
2510 /* 4 for RFC1001 length and 1 for Buffer */
2511 req->BufferOffset =
2512 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2513 req->BufferLength = cpu_to_le32(*size);
2514
2515 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2516
2517 memcpy(req->Buffer, *data, *size);
2518
2519 iov[0].iov_base = (char *)req;
2520 /* 4 for RFC1001 length */
2521 iov[0].iov_len = get_rfc1002_length(req) + 4;
2522
2523 for (i = 1; i < num; i++) {
2524 inc_rfc1001_len(req, size[i]);
2525 le32_add_cpu(&req->BufferLength, size[i]);
2526 iov[i].iov_base = (char *)data[i];
2527 iov[i].iov_len = size[i];
2528 }
2529
2530 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2531 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2532
Steve French7d3fb242013-11-18 09:56:28 -06002533 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002534 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06002535
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002536 free_rsp_buf(resp_buftype, rsp);
2537 kfree(iov);
2538 return rc;
2539}
2540
2541int
2542SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2543 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2544{
2545 struct smb2_file_rename_info info;
2546 void **data;
2547 unsigned int size[2];
2548 int rc;
2549 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2550
2551 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2552 if (!data)
2553 return -ENOMEM;
2554
2555 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2556 /* 0 = fail if target already exists */
2557 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2558 info.FileNameLength = cpu_to_le32(len);
2559
2560 data[0] = &info;
2561 size[0] = sizeof(struct smb2_file_rename_info);
2562
2563 data[1] = target_file;
2564 size[1] = len + 2 /* null */;
2565
2566 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002567 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2568 size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002569 kfree(data);
2570 return rc;
2571}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002572
2573int
2574SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2575 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2576{
2577 struct smb2_file_link_info info;
2578 void **data;
2579 unsigned int size[2];
2580 int rc;
2581 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2582
2583 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2584 if (!data)
2585 return -ENOMEM;
2586
2587 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2588 /* 0 = fail if link already exists */
2589 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2590 info.FileNameLength = cpu_to_le32(len);
2591
2592 data[0] = &info;
2593 size[0] = sizeof(struct smb2_file_link_info);
2594
2595 data[1] = target_file;
2596 size[1] = len + 2 /* null */;
2597
2598 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002599 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002600 kfree(data);
2601 return rc;
2602}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002603
2604int
2605SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05002606 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002607{
2608 struct smb2_file_eof_info info;
2609 void *data;
2610 unsigned int size;
2611
2612 info.EndOfFile = *eof;
2613
2614 data = &info;
2615 size = sizeof(struct smb2_file_eof_info);
2616
Steve Frenchf29ebb42014-07-19 21:44:58 -05002617 if (is_falloc)
2618 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2619 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2620 else
2621 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2622 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002623}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07002624
2625int
2626SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2627 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2628{
2629 unsigned int size;
2630 size = sizeof(FILE_BASIC_INFO);
2631 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2632 current->tgid, FILE_BASIC_INFORMATION, 1,
2633 (void **)&buf, &size);
2634}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002635
2636int
2637SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2638 const u64 persistent_fid, const u64 volatile_fid,
2639 __u8 oplock_level)
2640{
2641 int rc;
2642 struct smb2_oplock_break *req = NULL;
2643
Joe Perchesf96637b2013-05-04 22:12:25 -05002644 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002645 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2646
2647 if (rc)
2648 return rc;
2649
2650 req->VolatileFid = volatile_fid;
2651 req->PersistentFid = persistent_fid;
2652 req->OplockLevel = oplock_level;
2653 req->hdr.CreditRequest = cpu_to_le16(1);
2654
2655 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2656 /* SMB2 buffer freed by function above */
2657
2658 if (rc) {
2659 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002660 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002661 }
2662
2663 return rc;
2664}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002665
2666static void
2667copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2668 struct kstatfs *kst)
2669{
2670 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2671 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2672 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
2673 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
2674 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
2675 return;
2676}
2677
2678static int
2679build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2680 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2681{
2682 int rc;
2683 struct smb2_query_info_req *req;
2684
Joe Perchesf96637b2013-05-04 22:12:25 -05002685 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002686
2687 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2688 return -EIO;
2689
2690 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2691 if (rc)
2692 return rc;
2693
2694 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2695 req->FileInfoClass = level;
2696 req->PersistentFileId = persistent_fid;
2697 req->VolatileFileId = volatile_fid;
2698 /* 4 for rfc1002 length field and 1 for pad */
2699 req->InputBufferOffset =
2700 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2701 req->OutputBufferLength = cpu_to_le32(
2702 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2703
2704 iov->iov_base = (char *)req;
2705 /* 4 for rfc1002 length field */
2706 iov->iov_len = get_rfc1002_length(req) + 4;
2707 return 0;
2708}
2709
2710int
2711SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2712 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2713{
2714 struct smb2_query_info_rsp *rsp = NULL;
2715 struct kvec iov;
2716 int rc = 0;
2717 int resp_buftype;
2718 struct cifs_ses *ses = tcon->ses;
2719 struct smb2_fs_full_size_info *info = NULL;
2720
2721 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
2722 sizeof(struct smb2_fs_full_size_info),
2723 persistent_fid, volatile_fid);
2724 if (rc)
2725 return rc;
2726
2727 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2728 if (rc) {
2729 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05002730 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002731 }
2732 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2733
2734 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
2735 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
2736 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2737 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2738 sizeof(struct smb2_fs_full_size_info));
2739 if (!rc)
2740 copy_fs_info_to_kstatfs(info, fsdata);
2741
Steve French34f62642013-10-09 02:07:00 -05002742qfsinf_exit:
2743 free_rsp_buf(resp_buftype, iov.iov_base);
2744 return rc;
2745}
2746
2747int
2748SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05002749 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05002750{
2751 struct smb2_query_info_rsp *rsp = NULL;
2752 struct kvec iov;
2753 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05002754 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05002755 struct cifs_ses *ses = tcon->ses;
2756 unsigned int rsp_len, offset;
2757
Steven French21671142013-10-09 13:36:35 -05002758 if (level == FS_DEVICE_INFORMATION) {
2759 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
2760 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
2761 } else if (level == FS_ATTRIBUTE_INFORMATION) {
2762 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
2763 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05002764 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
2765 max_len = sizeof(struct smb3_fs_ss_info);
2766 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05002767 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05002768 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05002769 return -EINVAL;
2770 }
2771
2772 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05002773 persistent_fid, volatile_fid);
2774 if (rc)
2775 return rc;
2776
2777 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2778 if (rc) {
2779 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2780 goto qfsattr_exit;
2781 }
2782 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2783
2784 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
2785 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05002786 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
2787 if (rc)
2788 goto qfsattr_exit;
2789
2790 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05002791 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
2792 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05002793 rsp_len, max_len));
2794 else if (level == FS_DEVICE_INFORMATION)
2795 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
2796 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05002797 else if (level == FS_SECTOR_SIZE_INFORMATION) {
2798 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
2799 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
2800 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
2801 tcon->perf_sector_size =
2802 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
2803 }
Steve French34f62642013-10-09 02:07:00 -05002804
2805qfsattr_exit:
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002806 free_rsp_buf(resp_buftype, iov.iov_base);
2807 return rc;
2808}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07002809
2810int
2811smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2812 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2813 const __u32 num_lock, struct smb2_lock_element *buf)
2814{
2815 int rc = 0;
2816 struct smb2_lock_req *req = NULL;
2817 struct kvec iov[2];
2818 int resp_buf_type;
2819 unsigned int count;
2820
Joe Perchesf96637b2013-05-04 22:12:25 -05002821 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07002822
2823 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
2824 if (rc)
2825 return rc;
2826
2827 req->hdr.ProcessId = cpu_to_le32(pid);
2828 req->LockCount = cpu_to_le16(num_lock);
2829
2830 req->PersistentFileId = persist_fid;
2831 req->VolatileFileId = volatile_fid;
2832
2833 count = num_lock * sizeof(struct smb2_lock_element);
2834 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
2835
2836 iov[0].iov_base = (char *)req;
2837 /* 4 for rfc1002 length field and count for all locks */
2838 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
2839 iov[1].iov_base = (char *)buf;
2840 iov[1].iov_len = count;
2841
2842 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2843 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2844 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002845 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07002846 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
2847 }
2848
2849 return rc;
2850}
2851
2852int
2853SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
2854 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2855 const __u64 length, const __u64 offset, const __u32 lock_flags,
2856 const bool wait)
2857{
2858 struct smb2_lock_element lock;
2859
2860 lock.Offset = cpu_to_le64(offset);
2861 lock.Length = cpu_to_le64(length);
2862 lock.Flags = cpu_to_le32(lock_flags);
2863 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
2864 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
2865
2866 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
2867}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07002868
2869int
2870SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
2871 __u8 *lease_key, const __le32 lease_state)
2872{
2873 int rc;
2874 struct smb2_lease_ack *req = NULL;
2875
Joe Perchesf96637b2013-05-04 22:12:25 -05002876 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07002877 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2878
2879 if (rc)
2880 return rc;
2881
2882 req->hdr.CreditRequest = cpu_to_le16(1);
2883 req->StructureSize = cpu_to_le16(36);
2884 inc_rfc1001_len(req, 12);
2885
2886 memcpy(req->LeaseKey, lease_key, 16);
2887 req->LeaseState = lease_state;
2888
2889 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2890 /* SMB2 buffer freed by function above */
2891
2892 if (rc) {
2893 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002894 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07002895 }
2896
2897 return rc;
2898}