blob: 29e06db5f187bea7d4f5ce29d2cf5c0faad6ae09 [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;
Jerome Marchandb8da3442016-05-26 11:52:25 +0200591 unsigned char *ntlmssp_blob = NULL;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400592 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;
Jerome Marchandb8da3442016-05-26 11:52:25 +0200716 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400717 nls_cp);
718 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500719 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n",
720 rc);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400721 goto ssetup_exit; /* BB double check error handling */
722 }
723 if (use_spnego) {
724 /* blob_length = build_spnego_ntlmssp_blob(
725 &security_blob,
726 blob_length,
727 ntlmssp_blob); */
Joe Perchesf96637b2013-05-04 22:12:25 -0500728 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400729 rc = -EOPNOTSUPP;
730 kfree(ntlmssp_blob);
731 goto ssetup_exit;
732 } else {
733 security_blob = ntlmssp_blob;
734 }
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500735 iov[1].iov_base = security_blob;
736 iov[1].iov_len = blob_length;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400737 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -0500738 cifs_dbg(VFS, "illegal ntlmssp phase\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400739 rc = -EIO;
740 goto ssetup_exit;
741 }
742
743 /* Testing shows that buffer offset must be at location of Buffer[0] */
744 req->SecurityBufferOffset =
745 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
746 1 /* pad */ - 4 /* rfc1001 len */);
747 req->SecurityBufferLength = cpu_to_le16(blob_length);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400748
749 inc_rfc1001_len(req, blob_length - 1 /* pad */);
750
751 /* BB add code to build os and lm fields */
752
Steve French6d8b59d2012-12-08 22:36:29 -0600753 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype,
754 CIFS_LOG_ERROR | CIFS_NEG_OP);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400755
756 kfree(security_blob);
757 rsp = (struct smb2_sess_setup_rsp *)iov[0].iov_base;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500758 ses->Suid = rsp->hdr.SessionId;
Pavel Shilovsky4ca3a992012-09-25 11:00:09 +0400759 if (resp_buftype != CIFS_NO_BUFFER &&
760 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) {
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400761 if (phase != NtLmNegotiate) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500762 cifs_dbg(VFS, "Unexpected more processing error\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400763 goto ssetup_exit;
764 }
765 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
Pavel Shilovsky4ca3a992012-09-25 11:00:09 +0400766 le16_to_cpu(rsp->SecurityBufferOffset)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500767 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
768 le16_to_cpu(rsp->SecurityBufferOffset));
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400769 rc = -EIO;
770 goto ssetup_exit;
771 }
772
773 /* NTLMSSP Negotiate sent now processing challenge (response) */
774 phase = NtLmChallenge; /* process ntlmssp challenge */
775 rc = 0; /* MORE_PROCESSING is not an error here but expected */
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400776 rc = decode_ntlmssp_challenge(rsp->Buffer,
777 le16_to_cpu(rsp->SecurityBufferLength), ses);
778 }
779
780 /*
781 * BB eventually add code for SPNEGO decoding of NtlmChallenge blob,
782 * but at least the raw NTLMSSP case works.
783 */
784 /*
785 * No tcon so can't do
786 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
787 */
788 if (rc != 0)
789 goto ssetup_exit;
790
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400791 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Steve French0cbaa532013-11-15 23:50:24 -0600792 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
793 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400794ssetup_exit:
795 free_rsp_buf(resp_buftype, rsp);
796
797 /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
798 if ((phase == NtLmChallenge) && (rc == 0))
799 goto ssetup_ntlmssp_authenticate;
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -0500800
801 if (!rc) {
802 mutex_lock(&server->srv_mutex);
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500803 if (server->sign && server->ops->generate_signingkey) {
804 rc = server->ops->generate_signingkey(ses);
805 kfree(ses->auth_key.response);
806 ses->auth_key.response = NULL;
807 if (rc) {
808 cifs_dbg(FYI,
809 "SMB3 session key generation failed\n");
810 mutex_unlock(&server->srv_mutex);
811 goto keygen_exit;
812 }
813 }
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -0500814 if (!server->session_estab) {
815 server->sequence_number = 0x2;
816 server->session_estab = true;
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -0500817 }
818 mutex_unlock(&server->srv_mutex);
819
820 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
821 spin_lock(&GlobalMid_Lock);
822 ses->status = CifsGood;
823 ses->need_reconnect = false;
824 spin_unlock(&GlobalMid_Lock);
825 }
826
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500827keygen_exit:
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -0500828 if (!server->sign) {
829 kfree(ses->auth_key.response);
830 ses->auth_key.response = NULL;
831 }
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500832 if (spnego_key) {
833 key_invalidate(spnego_key);
834 key_put(spnego_key);
835 }
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -0500836 kfree(ses->ntlmssp);
837
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400838 return rc;
839}
840
841int
842SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
843{
844 struct smb2_logoff_req *req; /* response is also trivial struct */
845 int rc = 0;
846 struct TCP_Server_Info *server;
847
Joe Perchesf96637b2013-05-04 22:12:25 -0500848 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400849
850 if (ses && (ses->server))
851 server = ses->server;
852 else
853 return -EIO;
854
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -0500855 /* no need to send SMB logoff if uid already closed due to reconnect */
856 if (ses->need_reconnect)
857 goto smb2_session_already_dead;
858
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400859 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
860 if (rc)
861 return rc;
862
863 /* since no tcon, smb2_init can not do this, so do here */
864 req->hdr.SessionId = ses->Suid;
Jeff Layton38d77c52013-05-26 07:01:00 -0400865 if (server->sign)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700866 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400867
868 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
869 /*
870 * No tcon so can't do
871 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
872 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -0500873
874smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400875 return rc;
876}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400877
878static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
879{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400880 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400881}
882
883#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
884
Steve Frenchde9f68d2013-11-15 11:26:24 -0600885/* These are similar values to what Windows uses */
886static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
887{
888 tcon->max_chunks = 256;
889 tcon->max_bytes_chunk = 1048576;
890 tcon->max_bytes_copy = 16777216;
891}
892
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400893int
894SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
895 struct cifs_tcon *tcon, const struct nls_table *cp)
896{
897 struct smb2_tree_connect_req *req;
898 struct smb2_tree_connect_rsp *rsp = NULL;
899 struct kvec iov[2];
900 int rc = 0;
901 int resp_buftype;
902 int unc_path_len;
903 struct TCP_Server_Info *server;
904 __le16 *unc_path = NULL;
905
Joe Perchesf96637b2013-05-04 22:12:25 -0500906 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400907
908 if ((ses->server) && tree)
909 server = ses->server;
910 else
911 return -EIO;
912
913 if (tcon && tcon->bad_network_name)
914 return -ENOENT;
915
Steve Frenchff9f84b2015-09-26 09:48:58 -0500916 if ((tcon && tcon->seal) &&
Steve French88627142015-09-22 03:16:27 -0500917 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
918 cifs_dbg(VFS, "encryption requested but no server support");
919 return -EOPNOTSUPP;
920 }
921
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400922 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
923 if (unc_path == NULL)
924 return -ENOMEM;
925
926 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
927 unc_path_len *= 2;
928 if (unc_path_len < 2) {
929 kfree(unc_path);
930 return -EINVAL;
931 }
932
933 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
934 if (rc) {
935 kfree(unc_path);
936 return rc;
937 }
938
939 if (tcon == NULL) {
940 /* since no tcon, smb2_init can not do this, so do here */
941 req->hdr.SessionId = ses->Suid;
942 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
943 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
944 }
945
946 iov[0].iov_base = (char *)req;
947 /* 4 for rfc1002 length field and 1 for pad */
948 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
949
950 /* Testing shows that buffer offset must be at location of Buffer[0] */
951 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
952 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
953 req->PathLength = cpu_to_le16(unc_path_len - 2);
954 iov[1].iov_base = unc_path;
955 iov[1].iov_len = unc_path_len;
956
957 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
958
959 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
960 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
961
962 if (rc != 0) {
963 if (tcon) {
964 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
965 tcon->need_reconnect = true;
966 }
967 goto tcon_error_exit;
968 }
969
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400970 if (tcon == NULL) {
971 ses->ipc_tid = rsp->hdr.TreeId;
972 goto tcon_exit;
973 }
974
975 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
Joe Perchesf96637b2013-05-04 22:12:25 -0500976 cifs_dbg(FYI, "connection to disk share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400977 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
978 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -0500979 cifs_dbg(FYI, "connection to pipe share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400980 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
981 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -0500982 cifs_dbg(FYI, "connection to printer\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400983 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -0500984 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400985 rc = -EOPNOTSUPP;
986 goto tcon_error_exit;
987 }
988
989 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -0500990 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400991 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
992 tcon->tidStatus = CifsGood;
993 tcon->need_reconnect = false;
994 tcon->tid = rsp->hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -0500995 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400996
997 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
998 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -0500999 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Steve Frenchde9f68d2013-11-15 11:26:24 -06001000 init_copy_chunk_defaults(tcon);
Steve French88627142015-09-22 03:16:27 -05001001 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1002 cifs_dbg(VFS, "Encrypted shares not supported");
Steve Frenchff1c0382013-11-19 23:44:46 -06001003 if (tcon->ses->server->ops->validate_negotiate)
1004 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001005tcon_exit:
1006 free_rsp_buf(resp_buftype, rsp);
1007 kfree(unc_path);
1008 return rc;
1009
1010tcon_error_exit:
1011 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001012 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Steve French18f39e72014-08-17 00:22:24 -05001013 if (tcon)
1014 tcon->bad_network_name = true;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001015 }
1016 goto tcon_exit;
1017}
1018
1019int
1020SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1021{
1022 struct smb2_tree_disconnect_req *req; /* response is trivial */
1023 int rc = 0;
1024 struct TCP_Server_Info *server;
1025 struct cifs_ses *ses = tcon->ses;
1026
Joe Perchesf96637b2013-05-04 22:12:25 -05001027 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001028
1029 if (ses && (ses->server))
1030 server = ses->server;
1031 else
1032 return -EIO;
1033
1034 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1035 return 0;
1036
1037 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1038 if (rc)
1039 return rc;
1040
1041 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
1042 if (rc)
1043 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1044
1045 return rc;
1046}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001047
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001048
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001049static struct create_durable *
1050create_durable_buf(void)
1051{
1052 struct create_durable *buf;
1053
1054 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1055 if (!buf)
1056 return NULL;
1057
1058 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001059 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001060 buf->ccontext.DataLength = cpu_to_le32(16);
1061 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1062 (struct create_durable, Name));
1063 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001064 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001065 buf->Name[0] = 'D';
1066 buf->Name[1] = 'H';
1067 buf->Name[2] = 'n';
1068 buf->Name[3] = 'Q';
1069 return buf;
1070}
1071
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001072static struct create_durable *
1073create_reconnect_durable_buf(struct cifs_fid *fid)
1074{
1075 struct create_durable *buf;
1076
1077 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1078 if (!buf)
1079 return NULL;
1080
1081 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1082 (struct create_durable, Data));
1083 buf->ccontext.DataLength = cpu_to_le32(16);
1084 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1085 (struct create_durable, Name));
1086 buf->ccontext.NameLength = cpu_to_le16(4);
1087 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1088 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001089 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001090 buf->Name[0] = 'D';
1091 buf->Name[1] = 'H';
1092 buf->Name[2] = 'n';
1093 buf->Name[3] = 'C';
1094 return buf;
1095}
1096
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001097static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001098parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1099 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001100{
1101 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001102 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001103 unsigned int next;
1104 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001105 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001106
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001107 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001108 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001109 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001110 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001111 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001112 if (le16_to_cpu(cc->NameLength) == 4 &&
1113 strncmp(name, "RqLs", 4) == 0)
1114 return server->ops->parse_lease_buf(cc, epoch);
1115
1116 next = le32_to_cpu(cc->Next);
1117 if (!next)
1118 break;
1119 remaining -= next;
1120 cc = (struct create_context *)((char *)cc + next);
1121 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001122
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001123 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001124}
1125
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001126static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001127add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1128 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001129{
1130 struct smb2_create_req *req = iov[0].iov_base;
1131 unsigned int num = *num_iovec;
1132
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001133 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001134 if (iov[num].iov_base == NULL)
1135 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001136 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001137 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1138 if (!req->CreateContextsOffset)
1139 req->CreateContextsOffset = cpu_to_le32(
1140 sizeof(struct smb2_create_req) - 4 +
1141 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001142 le32_add_cpu(&req->CreateContextsLength,
1143 server->vals->create_lease_size);
1144 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001145 *num_iovec = num + 1;
1146 return 0;
1147}
1148
Steve Frenchb56eae42015-11-03 09:26:27 -06001149static struct create_durable_v2 *
1150create_durable_v2_buf(struct cifs_fid *pfid)
1151{
1152 struct create_durable_v2 *buf;
1153
1154 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1155 if (!buf)
1156 return NULL;
1157
1158 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1159 (struct create_durable_v2, dcontext));
1160 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1161 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1162 (struct create_durable_v2, Name));
1163 buf->ccontext.NameLength = cpu_to_le16(4);
1164
1165 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1166 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1167 get_random_bytes(buf->dcontext.CreateGuid, 16);
1168 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1169
1170 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1171 buf->Name[0] = 'D';
1172 buf->Name[1] = 'H';
1173 buf->Name[2] = '2';
1174 buf->Name[3] = 'Q';
1175 return buf;
1176}
1177
1178static struct create_durable_handle_reconnect_v2 *
1179create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1180{
1181 struct create_durable_handle_reconnect_v2 *buf;
1182
1183 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1184 GFP_KERNEL);
1185 if (!buf)
1186 return NULL;
1187
1188 buf->ccontext.DataOffset =
1189 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1190 dcontext));
1191 buf->ccontext.DataLength =
1192 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1193 buf->ccontext.NameOffset =
1194 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1195 Name));
1196 buf->ccontext.NameLength = cpu_to_le16(4);
1197
1198 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1199 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1200 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1201 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1202
1203 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1204 buf->Name[0] = 'D';
1205 buf->Name[1] = 'H';
1206 buf->Name[2] = '2';
1207 buf->Name[3] = 'C';
1208 return buf;
1209}
1210
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001211static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001212add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001213 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001214{
1215 struct smb2_create_req *req = iov[0].iov_base;
1216 unsigned int num = *num_iovec;
1217
Steve Frenchb56eae42015-11-03 09:26:27 -06001218 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1219 if (iov[num].iov_base == NULL)
1220 return -ENOMEM;
1221 iov[num].iov_len = sizeof(struct create_durable_v2);
1222 if (!req->CreateContextsOffset)
1223 req->CreateContextsOffset =
1224 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1225 iov[1].iov_len);
1226 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1227 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1228 *num_iovec = num + 1;
1229 return 0;
1230}
1231
1232static int
1233add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1234 struct cifs_open_parms *oparms)
1235{
1236 struct smb2_create_req *req = iov[0].iov_base;
1237 unsigned int num = *num_iovec;
1238
1239 /* indicate that we don't need to relock the file */
1240 oparms->reconnect = false;
1241
1242 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1243 if (iov[num].iov_base == NULL)
1244 return -ENOMEM;
1245 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1246 if (!req->CreateContextsOffset)
1247 req->CreateContextsOffset =
1248 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1249 iov[1].iov_len);
1250 le32_add_cpu(&req->CreateContextsLength,
1251 sizeof(struct create_durable_handle_reconnect_v2));
1252 inc_rfc1001_len(&req->hdr,
1253 sizeof(struct create_durable_handle_reconnect_v2));
1254 *num_iovec = num + 1;
1255 return 0;
1256}
1257
1258static int
1259add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1260 struct cifs_open_parms *oparms, bool use_persistent)
1261{
1262 struct smb2_create_req *req = iov[0].iov_base;
1263 unsigned int num = *num_iovec;
1264
1265 if (use_persistent) {
1266 if (oparms->reconnect)
1267 return add_durable_reconnect_v2_context(iov, num_iovec,
1268 oparms);
1269 else
1270 return add_durable_v2_context(iov, num_iovec, oparms);
1271 }
1272
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001273 if (oparms->reconnect) {
1274 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1275 /* indicate that we don't need to relock the file */
1276 oparms->reconnect = false;
1277 } else
1278 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001279 if (iov[num].iov_base == NULL)
1280 return -ENOMEM;
1281 iov[num].iov_len = sizeof(struct create_durable);
1282 if (!req->CreateContextsOffset)
1283 req->CreateContextsOffset =
1284 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1285 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001286 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001287 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1288 *num_iovec = num + 1;
1289 return 0;
1290}
1291
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001292int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001293SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001294 __u8 *oplock, struct smb2_file_all_info *buf,
1295 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001296{
1297 struct smb2_create_req *req;
1298 struct smb2_create_rsp *rsp;
1299 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001300 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001301 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001302 struct kvec iov[4];
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001303 int resp_buftype;
1304 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001305 __le16 *copy_path = NULL;
1306 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001307 int rc = 0;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001308 unsigned int num_iovecs = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001309 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001310 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001311
Joe Perchesf96637b2013-05-04 22:12:25 -05001312 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001313
1314 if (ses && (ses->server))
1315 server = ses->server;
1316 else
1317 return -EIO;
1318
1319 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1320 if (rc)
1321 return rc;
1322
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001323 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001324 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001325 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1326 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001327
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001328 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001329 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001330 /* File attributes ignored on open (used in create though) */
1331 req->FileAttributes = cpu_to_le32(file_attributes);
1332 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001333 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1334 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001335 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001336 /* do not count rfc1001 len field */
1337 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001338
1339 iov[0].iov_base = (char *)req;
1340 /* 4 for rfc1002 length field */
1341 iov[0].iov_len = get_rfc1002_length(req) + 4;
1342
1343 /* MUST set path len (NameLength) to 0 opening root of share */
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001344 req->NameLength = cpu_to_le16(uni_path_len - 2);
1345 /* -1 since last byte is buf[0] which is sent below (path) */
1346 iov[0].iov_len--;
1347 if (uni_path_len % 8 != 0) {
1348 copy_size = uni_path_len / 8 * 8;
1349 if (copy_size < uni_path_len)
1350 copy_size += 8;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001351
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001352 copy_path = kzalloc(copy_size, GFP_KERNEL);
1353 if (!copy_path)
1354 return -ENOMEM;
1355 memcpy((char *)copy_path, (const char *)path,
1356 uni_path_len);
1357 uni_path_len = copy_size;
1358 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001359 }
1360
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001361 iov[1].iov_len = uni_path_len;
1362 iov[1].iov_base = path;
1363 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1364 inc_rfc1001_len(req, uni_path_len - 1);
1365
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001366 if (!server->oplocks)
1367 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1368
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001369 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001370 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1371 req->RequestedOplockLevel = *oplock;
1372 else {
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001373 rc = add_lease_context(server, iov, &num_iovecs, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001374 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001375 cifs_small_buf_release(req);
1376 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001377 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001378 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001379 lc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001380 }
1381
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001382 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1383 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001384 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001385 struct create_context *ccontext =
1386 (struct create_context *)iov[num_iovecs-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001387 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001388 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001389 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001390
1391 rc = add_durable_context(iov, &num_iovecs, oparms,
1392 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001393 if (rc) {
1394 cifs_small_buf_release(req);
1395 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001396 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001397 return rc;
1398 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001399 dhc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001400 }
1401
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001402 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1403 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1404
1405 if (rc != 0) {
1406 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001407 if (err_buf)
1408 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1409 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001410 goto creat_exit;
1411 }
1412
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001413 oparms->fid->persistent_fid = rsp->PersistentFileId;
1414 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001415
1416 if (buf) {
1417 memcpy(buf, &rsp->CreationTime, 32);
1418 buf->AllocationSize = rsp->AllocationSize;
1419 buf->EndOfFile = rsp->EndofFile;
1420 buf->Attributes = rsp->FileAttributes;
1421 buf->NumberOfLinks = cpu_to_le32(1);
1422 buf->DeletePending = 0;
1423 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001424
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001425 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001426 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001427 else
1428 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001429creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001430 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001431 kfree(lc_buf);
1432 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001433 free_rsp_buf(resp_buftype, rsp);
1434 return rc;
1435}
1436
Steve French4a72daf2013-06-25 00:20:49 -05001437/*
1438 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1439 */
1440int
1441SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1442 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1443 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1444{
1445 struct smb2_ioctl_req *req;
1446 struct smb2_ioctl_rsp *rsp;
1447 struct TCP_Server_Info *server;
Steve French8e353102015-03-26 19:47:02 -05001448 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001449 struct kvec iov[2];
1450 int resp_buftype;
1451 int num_iovecs;
1452 int rc = 0;
1453
1454 cifs_dbg(FYI, "SMB2 IOCTL\n");
1455
Steve French3d1a3742014-08-11 21:05:25 -05001456 if (out_data != NULL)
1457 *out_data = NULL;
1458
Steve French4a72daf2013-06-25 00:20:49 -05001459 /* zero out returned data len, in case of error */
1460 if (plen)
1461 *plen = 0;
1462
Steve French8e353102015-03-26 19:47:02 -05001463 if (tcon)
1464 ses = tcon->ses;
1465 else
1466 return -EIO;
1467
Steve French4a72daf2013-06-25 00:20:49 -05001468 if (ses && (ses->server))
1469 server = ses->server;
1470 else
1471 return -EIO;
1472
1473 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1474 if (rc)
1475 return rc;
1476
1477 req->CtlCode = cpu_to_le32(opcode);
1478 req->PersistentFileId = persistent_fid;
1479 req->VolatileFileId = volatile_fid;
1480
1481 if (indatalen) {
1482 req->InputCount = cpu_to_le32(indatalen);
1483 /* do not set InputOffset if no input data */
1484 req->InputOffset =
1485 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1486 iov[1].iov_base = in_data;
1487 iov[1].iov_len = indatalen;
1488 num_iovecs = 2;
1489 } else
1490 num_iovecs = 1;
1491
1492 req->OutputOffset = 0;
1493 req->OutputCount = 0; /* MBZ */
1494
1495 /*
1496 * Could increase MaxOutputResponse, but that would require more
1497 * than one credit. Windows typically sets this smaller, but for some
1498 * ioctls it may be useful to allow server to send more. No point
1499 * limiting what the server can send as long as fits in one credit
1500 */
1501 req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1502
1503 if (is_fsctl)
1504 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1505 else
1506 req->Flags = 0;
1507
1508 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001509
Steve French7ff8d452013-10-14 00:44:19 -05001510 /*
1511 * If no input data, the size of ioctl struct in
1512 * protocol spec still includes a 1 byte data buffer,
1513 * but if input data passed to ioctl, we do not
1514 * want to double count this, so we do not send
1515 * the dummy one byte of data in iovec[0] if sending
1516 * input data (in iovec[1]). We also must add 4 bytes
1517 * in first iovec to allow for rfc1002 length field.
1518 */
1519
1520 if (indatalen) {
1521 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1522 inc_rfc1001_len(req, indatalen - 1);
1523 } else
1524 iov[0].iov_len = get_rfc1002_length(req) + 4;
1525
Steve French4a72daf2013-06-25 00:20:49 -05001526
1527 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1528 rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1529
Steve French9bf0c9c2013-11-16 18:05:28 -06001530 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001531 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001532 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001533 } else if (rc == -EINVAL) {
1534 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1535 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001536 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001537 goto ioctl_exit;
1538 }
Steve French4a72daf2013-06-25 00:20:49 -05001539 }
1540
1541 /* check if caller wants to look at return data or just return rc */
1542 if ((plen == NULL) || (out_data == NULL))
1543 goto ioctl_exit;
1544
1545 *plen = le32_to_cpu(rsp->OutputCount);
1546
1547 /* We check for obvious errors in the output buffer length and offset */
1548 if (*plen == 0)
1549 goto ioctl_exit; /* server returned no data */
1550 else if (*plen > 0xFF00) {
1551 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1552 *plen = 0;
1553 rc = -EIO;
1554 goto ioctl_exit;
1555 }
1556
1557 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1558 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1559 le32_to_cpu(rsp->OutputOffset));
1560 *plen = 0;
1561 rc = -EIO;
1562 goto ioctl_exit;
1563 }
1564
1565 *out_data = kmalloc(*plen, GFP_KERNEL);
1566 if (*out_data == NULL) {
1567 rc = -ENOMEM;
1568 goto ioctl_exit;
1569 }
1570
Steve French373512e2015-12-18 13:05:30 -06001571 memcpy(*out_data,
1572 (char *)&rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
Steve French4a72daf2013-06-25 00:20:49 -05001573 *plen);
1574ioctl_exit:
1575 free_rsp_buf(resp_buftype, rsp);
1576 return rc;
1577}
1578
Steve French64a5cfa2013-10-14 15:31:32 -05001579/*
1580 * Individual callers to ioctl worker function follow
1581 */
1582
1583int
1584SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1585 u64 persistent_fid, u64 volatile_fid)
1586{
1587 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05001588 struct compress_ioctl fsctl_input;
1589 char *ret_data = NULL;
1590
1591 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08001592 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05001593
1594 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1595 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1596 (char *)&fsctl_input /* data input */,
1597 2 /* in data len */, &ret_data /* out data */, NULL);
1598
1599 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05001600
1601 return rc;
1602}
1603
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001604int
1605SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1606 u64 persistent_fid, u64 volatile_fid)
1607{
1608 struct smb2_close_req *req;
1609 struct smb2_close_rsp *rsp;
1610 struct TCP_Server_Info *server;
1611 struct cifs_ses *ses = tcon->ses;
1612 struct kvec iov[1];
1613 int resp_buftype;
1614 int rc = 0;
1615
Joe Perchesf96637b2013-05-04 22:12:25 -05001616 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001617
1618 if (ses && (ses->server))
1619 server = ses->server;
1620 else
1621 return -EIO;
1622
1623 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1624 if (rc)
1625 return rc;
1626
1627 req->PersistentFileId = persistent_fid;
1628 req->VolatileFileId = volatile_fid;
1629
1630 iov[0].iov_base = (char *)req;
1631 /* 4 for rfc1002 length field */
1632 iov[0].iov_len = get_rfc1002_length(req) + 4;
1633
1634 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1635 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1636
1637 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09001638 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001639 goto close_exit;
1640 }
1641
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001642 /* BB FIXME - decode close response, update inode for caching */
1643
1644close_exit:
1645 free_rsp_buf(resp_buftype, rsp);
1646 return rc;
1647}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001648
1649static int
1650validate_buf(unsigned int offset, unsigned int buffer_length,
1651 struct smb2_hdr *hdr, unsigned int min_buf_size)
1652
1653{
1654 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1655 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1656 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1657 char *end_of_buf = begin_of_buf + buffer_length;
1658
1659
1660 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001661 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1662 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001663 return -EINVAL;
1664 }
1665
1666 /* check if beyond RFC1001 maximum length */
1667 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001668 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1669 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001670 return -EINVAL;
1671 }
1672
1673 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001674 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001675 return -EINVAL;
1676 }
1677
1678 return 0;
1679}
1680
1681/*
1682 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1683 * Caller must free buffer.
1684 */
1685static int
1686validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1687 struct smb2_hdr *hdr, unsigned int minbufsize,
1688 char *data)
1689
1690{
1691 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1692 int rc;
1693
1694 if (!data)
1695 return -EINVAL;
1696
1697 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1698 if (rc)
1699 return rc;
1700
1701 memcpy(data, begin_of_buf, buffer_length);
1702
1703 return 0;
1704}
1705
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001706static int
1707query_info(const unsigned int xid, struct cifs_tcon *tcon,
1708 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1709 size_t output_len, size_t min_len, void *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001710{
1711 struct smb2_query_info_req *req;
1712 struct smb2_query_info_rsp *rsp = NULL;
1713 struct kvec iov[2];
1714 int rc = 0;
1715 int resp_buftype;
1716 struct TCP_Server_Info *server;
1717 struct cifs_ses *ses = tcon->ses;
1718
Joe Perchesf96637b2013-05-04 22:12:25 -05001719 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001720
1721 if (ses && (ses->server))
1722 server = ses->server;
1723 else
1724 return -EIO;
1725
1726 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1727 if (rc)
1728 return rc;
1729
1730 req->InfoType = SMB2_O_INFO_FILE;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001731 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001732 req->PersistentFileId = persistent_fid;
1733 req->VolatileFileId = volatile_fid;
1734 /* 4 for rfc1002 length field and 1 for Buffer */
1735 req->InputBufferOffset =
1736 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001737 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001738
1739 iov[0].iov_base = (char *)req;
1740 /* 4 for rfc1002 length field */
1741 iov[0].iov_len = get_rfc1002_length(req) + 4;
1742
1743 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04001744 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1745
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001746 if (rc) {
1747 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1748 goto qinf_exit;
1749 }
1750
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001751 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1752 le32_to_cpu(rsp->OutputBufferLength),
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001753 &rsp->hdr, min_len, data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001754
1755qinf_exit:
1756 free_rsp_buf(resp_buftype, rsp);
1757 return rc;
1758}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001759
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001760int
1761SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1762 u64 persistent_fid, u64 volatile_fid,
1763 struct smb2_file_all_info *data)
1764{
1765 return query_info(xid, tcon, persistent_fid, volatile_fid,
1766 FILE_ALL_INFORMATION,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04001767 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001768 sizeof(struct smb2_file_all_info), data);
1769}
1770
1771int
1772SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1773 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1774{
1775 return query_info(xid, tcon, persistent_fid, volatile_fid,
1776 FILE_INTERNAL_INFORMATION,
1777 sizeof(struct smb2_file_internal_info),
1778 sizeof(struct smb2_file_internal_info), uniqueid);
1779}
1780
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001781/*
1782 * This is a no-op for now. We're not really interested in the reply, but
1783 * rather in the fact that the server sent one and that server->lstrp
1784 * gets updated.
1785 *
1786 * FIXME: maybe we should consider checking that the reply matches request?
1787 */
1788static void
1789smb2_echo_callback(struct mid_q_entry *mid)
1790{
1791 struct TCP_Server_Info *server = mid->callback_data;
1792 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1793 unsigned int credits_received = 1;
1794
1795 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1796 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1797
Christopher Oo5fb4e282015-06-25 16:10:48 -07001798 mutex_lock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001799 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07001800 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001801 add_credits(server, credits_received, CIFS_ECHO_OP);
1802}
1803
1804int
1805SMB2_echo(struct TCP_Server_Info *server)
1806{
1807 struct smb2_echo_req *req;
1808 int rc = 0;
1809 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07001810 struct smb_rqst rqst = { .rq_iov = &iov,
1811 .rq_nvec = 1 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001812
Joe Perchesf96637b2013-05-04 22:12:25 -05001813 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001814
Steve French4fcd1812016-06-22 20:12:05 -05001815 if (server->tcpStatus == CifsNeedNegotiate) {
1816 struct list_head *tmp, *tmp2;
1817 struct cifs_ses *ses;
1818 struct cifs_tcon *tcon;
1819
1820 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
1821 spin_lock(&cifs_tcp_ses_lock);
1822 list_for_each(tmp, &server->smb_ses_list) {
1823 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
1824 list_for_each(tmp2, &ses->tcon_list) {
1825 tcon = list_entry(tmp2, struct cifs_tcon,
1826 tcon_list);
1827 /* add check for persistent handle reconnect */
1828 if (tcon && tcon->need_reconnect) {
1829 spin_unlock(&cifs_tcp_ses_lock);
1830 rc = smb2_reconnect(SMB2_ECHO, tcon);
1831 spin_lock(&cifs_tcp_ses_lock);
1832 }
1833 }
1834 }
1835 spin_unlock(&cifs_tcp_ses_lock);
1836 }
1837
1838 /* if no session, renegotiate failed above */
1839 if (server->tcpStatus == CifsNeedNegotiate)
1840 return -EIO;
1841
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001842 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
1843 if (rc)
1844 return rc;
1845
1846 req->hdr.CreditRequest = cpu_to_le16(1);
1847
1848 iov.iov_base = (char *)req;
1849 /* 4 for rfc1002 length field */
1850 iov.iov_len = get_rfc1002_length(req) + 4;
1851
Jeff Laytonfec344e2012-09-18 16:20:35 -07001852 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001853 CIFS_ECHO_OP);
1854 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001855 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001856
1857 cifs_small_buf_release(req);
1858 return rc;
1859}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001860
1861int
1862SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1863 u64 volatile_fid)
1864{
1865 struct smb2_flush_req *req;
1866 struct TCP_Server_Info *server;
1867 struct cifs_ses *ses = tcon->ses;
1868 struct kvec iov[1];
1869 int resp_buftype;
1870 int rc = 0;
1871
Joe Perchesf96637b2013-05-04 22:12:25 -05001872 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001873
1874 if (ses && (ses->server))
1875 server = ses->server;
1876 else
1877 return -EIO;
1878
1879 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
1880 if (rc)
1881 return rc;
1882
1883 req->PersistentFileId = persistent_fid;
1884 req->VolatileFileId = volatile_fid;
1885
1886 iov[0].iov_base = (char *)req;
1887 /* 4 for rfc1002 length field */
1888 iov[0].iov_len = get_rfc1002_length(req) + 4;
1889
1890 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1891
Steve Frenchdfebe402015-03-27 01:00:06 -05001892 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001893 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
1894
1895 free_rsp_buf(resp_buftype, iov[0].iov_base);
1896 return rc;
1897}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001898
1899/*
1900 * To form a chain of read requests, any read requests after the first should
1901 * have the end_of_chain boolean set to true.
1902 */
1903static int
1904smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
1905 unsigned int remaining_bytes, int request_type)
1906{
1907 int rc = -EACCES;
1908 struct smb2_read_req *req = NULL;
1909
1910 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
1911 if (rc)
1912 return rc;
1913 if (io_parms->tcon->ses->server == NULL)
1914 return -ECONNABORTED;
1915
1916 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
1917
1918 req->PersistentFileId = io_parms->persistent_fid;
1919 req->VolatileFileId = io_parms->volatile_fid;
1920 req->ReadChannelInfoOffset = 0; /* reserved */
1921 req->ReadChannelInfoLength = 0; /* reserved */
1922 req->Channel = 0; /* reserved */
1923 req->MinimumCount = 0;
1924 req->Length = cpu_to_le32(io_parms->length);
1925 req->Offset = cpu_to_le64(io_parms->offset);
1926
1927 if (request_type & CHAINED_REQUEST) {
1928 if (!(request_type & END_OF_CHAIN)) {
1929 /* 4 for rfc1002 length field */
1930 req->hdr.NextCommand =
1931 cpu_to_le32(get_rfc1002_length(req) + 4);
1932 } else /* END_OF_CHAIN */
1933 req->hdr.NextCommand = 0;
1934 if (request_type & RELATED_REQUEST) {
1935 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
1936 /*
1937 * Related requests use info from previous read request
1938 * in chain.
1939 */
1940 req->hdr.SessionId = 0xFFFFFFFF;
1941 req->hdr.TreeId = 0xFFFFFFFF;
1942 req->PersistentFileId = 0xFFFFFFFF;
1943 req->VolatileFileId = 0xFFFFFFFF;
1944 }
1945 }
1946 if (remaining_bytes > io_parms->length)
1947 req->RemainingBytes = cpu_to_le32(remaining_bytes);
1948 else
1949 req->RemainingBytes = 0;
1950
1951 iov[0].iov_base = (char *)req;
1952 /* 4 for rfc1002 length field */
1953 iov[0].iov_len = get_rfc1002_length(req) + 4;
1954 return rc;
1955}
1956
1957static void
1958smb2_readv_callback(struct mid_q_entry *mid)
1959{
1960 struct cifs_readdata *rdata = mid->callback_data;
1961 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1962 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton58195752012-09-19 06:22:34 -07001963 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001964 unsigned int credits_received = 1;
Jeff Layton58195752012-09-19 06:22:34 -07001965 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Layton8321fec2012-09-19 06:22:32 -07001966 .rq_nvec = 1,
1967 .rq_pages = rdata->pages,
1968 .rq_npages = rdata->nr_pages,
1969 .rq_pagesz = rdata->pagesz,
1970 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001971
Joe Perchesf96637b2013-05-04 22:12:25 -05001972 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1973 __func__, mid->mid, mid->mid_state, rdata->result,
1974 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001975
1976 switch (mid->mid_state) {
1977 case MID_RESPONSE_RECEIVED:
1978 credits_received = le16_to_cpu(buf->CreditRequest);
1979 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04001980 if (server->sign) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001981 int rc;
1982
Jeff Layton0b688cf2012-09-18 16:20:34 -07001983 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001984 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001985 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1986 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001987 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001988 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04001989 task_io_account_read(rdata->got_bytes);
1990 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001991 break;
1992 case MID_REQUEST_SUBMITTED:
1993 case MID_RETRY_NEEDED:
1994 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04001995 if (server->sign && rdata->got_bytes)
1996 /* reset bytes number since we can not check a sign */
1997 rdata->got_bytes = 0;
1998 /* FIXME: should this be counted toward the initiating task? */
1999 task_io_account_read(rdata->got_bytes);
2000 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002001 break;
2002 default:
2003 if (rdata->result != -ENODATA)
2004 rdata->result = -EIO;
2005 }
2006
2007 if (rdata->result)
2008 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2009
2010 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002011 mutex_lock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002012 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002013 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002014 add_credits(server, credits_received, 0);
2015}
2016
2017/* smb2_async_readv - send an async write, and set up mid to handle result */
2018int
2019smb2_async_readv(struct cifs_readdata *rdata)
2020{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002021 int rc, flags = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002022 struct smb2_hdr *buf;
2023 struct cifs_io_parms io_parms;
Jeff Layton58195752012-09-19 06:22:34 -07002024 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Laytonfec344e2012-09-18 16:20:35 -07002025 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002026 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002027
Joe Perchesf96637b2013-05-04 22:12:25 -05002028 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2029 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002030
2031 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2032 io_parms.offset = rdata->offset;
2033 io_parms.length = rdata->bytes;
2034 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2035 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2036 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002037
2038 server = io_parms.tcon->ses->server;
2039
Jeff Layton58195752012-09-19 06:22:34 -07002040 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002041 if (rc) {
2042 if (rc == -EAGAIN && rdata->credits) {
2043 /* credits was reset by reconnect */
2044 rdata->credits = 0;
2045 /* reduce in_flight value since we won't send the req */
2046 spin_lock(&server->req_lock);
2047 server->in_flight--;
2048 spin_unlock(&server->req_lock);
2049 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002050 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002051 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002052
Jeff Layton58195752012-09-19 06:22:34 -07002053 buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002054 /* 4 for rfc1002 length field */
Jeff Layton58195752012-09-19 06:22:34 -07002055 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002056
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002057 if (rdata->credits) {
2058 buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2059 SMB2_MAX_BUFFER_SIZE));
2060 spin_lock(&server->req_lock);
2061 server->credits += rdata->credits -
2062 le16_to_cpu(buf->CreditCharge);
2063 spin_unlock(&server->req_lock);
2064 wake_up(&server->request_q);
2065 flags = CIFS_HAS_CREDITS;
2066 }
2067
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002068 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002069 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002070 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002071 rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002072 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002073 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002074 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2075 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002076
2077 cifs_small_buf_release(buf);
2078 return rc;
2079}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002080
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002081int
2082SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2083 unsigned int *nbytes, char **buf, int *buf_type)
2084{
2085 int resp_buftype, rc = -EACCES;
2086 struct smb2_read_rsp *rsp = NULL;
2087 struct kvec iov[1];
2088
2089 *nbytes = 0;
2090 rc = smb2_new_read_req(iov, io_parms, 0, 0);
2091 if (rc)
2092 return rc;
2093
2094 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
2095 &resp_buftype, CIFS_LOG_ERROR);
2096
2097 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
2098
2099 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
2100 free_rsp_buf(resp_buftype, iov[0].iov_base);
2101 return 0;
2102 }
2103
2104 if (rc) {
2105 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002106 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002107 } else {
2108 *nbytes = le32_to_cpu(rsp->DataLength);
2109 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2110 (*nbytes > io_parms->length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002111 cifs_dbg(FYI, "bad length %d for count %d\n",
2112 *nbytes, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002113 rc = -EIO;
2114 *nbytes = 0;
2115 }
2116 }
2117
2118 if (*buf) {
Steve French373512e2015-12-18 13:05:30 -06002119 memcpy(*buf, (char *)&rsp->hdr.ProtocolId + rsp->DataOffset,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002120 *nbytes);
2121 free_rsp_buf(resp_buftype, iov[0].iov_base);
2122 } else if (resp_buftype != CIFS_NO_BUFFER) {
2123 *buf = iov[0].iov_base;
2124 if (resp_buftype == CIFS_SMALL_BUFFER)
2125 *buf_type = CIFS_SMALL_BUFFER;
2126 else if (resp_buftype == CIFS_LARGE_BUFFER)
2127 *buf_type = CIFS_LARGE_BUFFER;
2128 }
2129 return rc;
2130}
2131
Pavel Shilovsky33319142012-09-18 16:20:29 -07002132/*
2133 * Check the mid_state and signature on received buffer (if any), and queue the
2134 * workqueue completion task.
2135 */
2136static void
2137smb2_writev_callback(struct mid_q_entry *mid)
2138{
2139 struct cifs_writedata *wdata = mid->callback_data;
2140 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002141 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002142 unsigned int written;
2143 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2144 unsigned int credits_received = 1;
2145
2146 switch (mid->mid_state) {
2147 case MID_RESPONSE_RECEIVED:
2148 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
2149 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2150 if (wdata->result != 0)
2151 break;
2152
2153 written = le32_to_cpu(rsp->DataLength);
2154 /*
2155 * Mask off high 16 bits when bytes written as returned
2156 * by the server is greater than bytes requested by the
2157 * client. OS/2 servers are known to set incorrect
2158 * CountHigh values.
2159 */
2160 if (written > wdata->bytes)
2161 written &= 0xFFFF;
2162
2163 if (written < wdata->bytes)
2164 wdata->result = -ENOSPC;
2165 else
2166 wdata->bytes = written;
2167 break;
2168 case MID_REQUEST_SUBMITTED:
2169 case MID_RETRY_NEEDED:
2170 wdata->result = -EAGAIN;
2171 break;
2172 default:
2173 wdata->result = -EIO;
2174 break;
2175 }
2176
2177 if (wdata->result)
2178 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2179
2180 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002181 mutex_lock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002182 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002183 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002184 add_credits(tcon->ses->server, credits_received, 0);
2185}
2186
2187/* smb2_async_writev - send an async write, and set up mid to handle result */
2188int
Steve French4a5c80d2014-02-07 20:45:12 -06002189smb2_async_writev(struct cifs_writedata *wdata,
2190 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002191{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002192 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002193 struct smb2_write_req *req = NULL;
2194 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002195 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002196 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002197 struct smb_rqst rqst;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002198
2199 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002200 if (rc) {
2201 if (rc == -EAGAIN && wdata->credits) {
2202 /* credits was reset by reconnect */
2203 wdata->credits = 0;
2204 /* reduce in_flight value since we won't send the req */
2205 spin_lock(&server->req_lock);
2206 server->in_flight--;
2207 spin_unlock(&server->req_lock);
2208 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002209 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002210 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002211
Pavel Shilovsky33319142012-09-18 16:20:29 -07002212 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
2213
2214 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2215 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2216 req->WriteChannelInfoOffset = 0;
2217 req->WriteChannelInfoLength = 0;
2218 req->Channel = 0;
2219 req->Offset = cpu_to_le64(wdata->offset);
2220 /* 4 for rfc1002 length field */
2221 req->DataOffset = cpu_to_le16(
2222 offsetof(struct smb2_write_req, Buffer) - 4);
2223 req->RemainingBytes = 0;
2224
2225 /* 4 for rfc1002 length field and 1 for Buffer */
Jeff Laytoneddb0792012-09-18 16:20:35 -07002226 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
2227 iov.iov_base = req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002228
Jeff Laytoneddb0792012-09-18 16:20:35 -07002229 rqst.rq_iov = &iov;
2230 rqst.rq_nvec = 1;
2231 rqst.rq_pages = wdata->pages;
2232 rqst.rq_npages = wdata->nr_pages;
2233 rqst.rq_pagesz = wdata->pagesz;
2234 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002235
Joe Perchesf96637b2013-05-04 22:12:25 -05002236 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2237 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002238
2239 req->Length = cpu_to_le32(wdata->bytes);
2240
2241 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2242
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002243 if (wdata->credits) {
2244 req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2245 SMB2_MAX_BUFFER_SIZE));
2246 spin_lock(&server->req_lock);
2247 server->credits += wdata->credits -
2248 le16_to_cpu(req->hdr.CreditCharge);
2249 spin_unlock(&server->req_lock);
2250 wake_up(&server->request_q);
2251 flags = CIFS_HAS_CREDITS;
2252 }
2253
Pavel Shilovsky33319142012-09-18 16:20:29 -07002254 kref_get(&wdata->refcount);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002255 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2256 flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002257
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002258 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002259 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002260 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2261 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002262
Pavel Shilovsky33319142012-09-18 16:20:29 -07002263async_writev_out:
2264 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002265 return rc;
2266}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002267
2268/*
2269 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2270 * The length field from io_parms must be at least 1 and indicates a number of
2271 * elements with data to write that begins with position 1 in iov array. All
2272 * data length is specified by count.
2273 */
2274int
2275SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2276 unsigned int *nbytes, struct kvec *iov, int n_vec)
2277{
2278 int rc = 0;
2279 struct smb2_write_req *req = NULL;
2280 struct smb2_write_rsp *rsp = NULL;
2281 int resp_buftype;
2282 *nbytes = 0;
2283
2284 if (n_vec < 1)
2285 return rc;
2286
2287 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2288 if (rc)
2289 return rc;
2290
2291 if (io_parms->tcon->ses->server == NULL)
2292 return -ECONNABORTED;
2293
2294 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2295
2296 req->PersistentFileId = io_parms->persistent_fid;
2297 req->VolatileFileId = io_parms->volatile_fid;
2298 req->WriteChannelInfoOffset = 0;
2299 req->WriteChannelInfoLength = 0;
2300 req->Channel = 0;
2301 req->Length = cpu_to_le32(io_parms->length);
2302 req->Offset = cpu_to_le64(io_parms->offset);
2303 /* 4 for rfc1002 length field */
2304 req->DataOffset = cpu_to_le16(
2305 offsetof(struct smb2_write_req, Buffer) - 4);
2306 req->RemainingBytes = 0;
2307
2308 iov[0].iov_base = (char *)req;
2309 /* 4 for rfc1002 length field and 1 for Buffer */
2310 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2311
2312 /* length of entire message including data to be written */
2313 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2314
2315 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2316 &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002317 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002318
2319 if (rc) {
2320 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002321 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002322 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002323 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002324
2325 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002326 return rc;
2327}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002328
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002329static unsigned int
2330num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2331{
2332 int len;
2333 unsigned int entrycount = 0;
2334 unsigned int next_offset = 0;
2335 FILE_DIRECTORY_INFO *entryptr;
2336
2337 if (bufstart == NULL)
2338 return 0;
2339
2340 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2341
2342 while (1) {
2343 entryptr = (FILE_DIRECTORY_INFO *)
2344 ((char *)entryptr + next_offset);
2345
2346 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002347 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002348 break;
2349 }
2350
2351 len = le32_to_cpu(entryptr->FileNameLength);
2352 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002353 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2354 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002355 break;
2356 }
2357
2358 *lastentry = (char *)entryptr;
2359 entrycount++;
2360
2361 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2362 if (!next_offset)
2363 break;
2364 }
2365
2366 return entrycount;
2367}
2368
2369/*
2370 * Readdir/FindFirst
2371 */
2372int
2373SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2374 u64 persistent_fid, u64 volatile_fid, int index,
2375 struct cifs_search_info *srch_inf)
2376{
2377 struct smb2_query_directory_req *req;
2378 struct smb2_query_directory_rsp *rsp = NULL;
2379 struct kvec iov[2];
2380 int rc = 0;
2381 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002382 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002383 unsigned char *bufptr;
2384 struct TCP_Server_Info *server;
2385 struct cifs_ses *ses = tcon->ses;
2386 __le16 asteriks = cpu_to_le16('*');
2387 char *end_of_smb;
2388 unsigned int output_size = CIFSMaxBufSize;
2389 size_t info_buf_size;
2390
2391 if (ses && (ses->server))
2392 server = ses->server;
2393 else
2394 return -EIO;
2395
2396 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2397 if (rc)
2398 return rc;
2399
2400 switch (srch_inf->info_level) {
2401 case SMB_FIND_FILE_DIRECTORY_INFO:
2402 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2403 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2404 break;
2405 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2406 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2407 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2408 break;
2409 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002410 cifs_dbg(VFS, "info level %u isn't supported\n",
2411 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002412 rc = -EINVAL;
2413 goto qdir_exit;
2414 }
2415
2416 req->FileIndex = cpu_to_le32(index);
2417 req->PersistentFileId = persistent_fid;
2418 req->VolatileFileId = volatile_fid;
2419
2420 len = 0x2;
2421 bufptr = req->Buffer;
2422 memcpy(bufptr, &asteriks, len);
2423
2424 req->FileNameOffset =
2425 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2426 req->FileNameLength = cpu_to_le16(len);
2427 /*
2428 * BB could be 30 bytes or so longer if we used SMB2 specific
2429 * buffer lengths, but this is safe and close enough.
2430 */
2431 output_size = min_t(unsigned int, output_size, server->maxBuf);
2432 output_size = min_t(unsigned int, output_size, 2 << 15);
2433 req->OutputBufferLength = cpu_to_le32(output_size);
2434
2435 iov[0].iov_base = (char *)req;
2436 /* 4 for RFC1001 length and 1 for Buffer */
2437 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2438
2439 iov[1].iov_base = (char *)(req->Buffer);
2440 iov[1].iov_len = len;
2441
2442 inc_rfc1001_len(req, len - 1 /* Buffer */);
2443
2444 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002445 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2446
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002447 if (rc) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04002448 if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2449 srch_inf->endOfSearch = true;
2450 rc = 0;
2451 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002452 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2453 goto qdir_exit;
2454 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002455
2456 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2457 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2458 info_buf_size);
2459 if (rc)
2460 goto qdir_exit;
2461
2462 srch_inf->unicode = true;
2463
2464 if (srch_inf->ntwrk_buf_start) {
2465 if (srch_inf->smallBuf)
2466 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2467 else
2468 cifs_buf_release(srch_inf->ntwrk_buf_start);
2469 }
2470 srch_inf->ntwrk_buf_start = (char *)rsp;
2471 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2472 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2473 /* 4 for rfc1002 length field */
2474 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2475 srch_inf->entries_in_buffer =
2476 num_entries(srch_inf->srch_entries_start, end_of_smb,
2477 &srch_inf->last_entry, info_buf_size);
2478 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05002479 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2480 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2481 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002482 if (resp_buftype == CIFS_LARGE_BUFFER)
2483 srch_inf->smallBuf = false;
2484 else if (resp_buftype == CIFS_SMALL_BUFFER)
2485 srch_inf->smallBuf = true;
2486 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002487 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002488
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002489 return rc;
2490
2491qdir_exit:
2492 free_rsp_buf(resp_buftype, rsp);
2493 return rc;
2494}
2495
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002496static int
2497send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002498 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002499 unsigned int num, void **data, unsigned int *size)
2500{
2501 struct smb2_set_info_req *req;
2502 struct smb2_set_info_rsp *rsp = NULL;
2503 struct kvec *iov;
2504 int rc = 0;
2505 int resp_buftype;
2506 unsigned int i;
2507 struct TCP_Server_Info *server;
2508 struct cifs_ses *ses = tcon->ses;
2509
2510 if (ses && (ses->server))
2511 server = ses->server;
2512 else
2513 return -EIO;
2514
2515 if (!num)
2516 return -EINVAL;
2517
2518 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2519 if (!iov)
2520 return -ENOMEM;
2521
2522 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2523 if (rc) {
2524 kfree(iov);
2525 return rc;
2526 }
2527
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002528 req->hdr.ProcessId = cpu_to_le32(pid);
2529
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002530 req->InfoType = SMB2_O_INFO_FILE;
2531 req->FileInfoClass = info_class;
2532 req->PersistentFileId = persistent_fid;
2533 req->VolatileFileId = volatile_fid;
2534
2535 /* 4 for RFC1001 length and 1 for Buffer */
2536 req->BufferOffset =
2537 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2538 req->BufferLength = cpu_to_le32(*size);
2539
2540 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2541
2542 memcpy(req->Buffer, *data, *size);
2543
2544 iov[0].iov_base = (char *)req;
2545 /* 4 for RFC1001 length */
2546 iov[0].iov_len = get_rfc1002_length(req) + 4;
2547
2548 for (i = 1; i < num; i++) {
2549 inc_rfc1001_len(req, size[i]);
2550 le32_add_cpu(&req->BufferLength, size[i]);
2551 iov[i].iov_base = (char *)data[i];
2552 iov[i].iov_len = size[i];
2553 }
2554
2555 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2556 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2557
Steve French7d3fb242013-11-18 09:56:28 -06002558 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002559 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06002560
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002561 free_rsp_buf(resp_buftype, rsp);
2562 kfree(iov);
2563 return rc;
2564}
2565
2566int
2567SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2568 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2569{
2570 struct smb2_file_rename_info info;
2571 void **data;
2572 unsigned int size[2];
2573 int rc;
2574 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2575
2576 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2577 if (!data)
2578 return -ENOMEM;
2579
2580 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2581 /* 0 = fail if target already exists */
2582 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2583 info.FileNameLength = cpu_to_le32(len);
2584
2585 data[0] = &info;
2586 size[0] = sizeof(struct smb2_file_rename_info);
2587
2588 data[1] = target_file;
2589 size[1] = len + 2 /* null */;
2590
2591 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002592 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2593 size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002594 kfree(data);
2595 return rc;
2596}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002597
2598int
Steve French897fba12016-05-12 21:20:36 -05002599SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2600 u64 persistent_fid, u64 volatile_fid)
2601{
2602 __u8 delete_pending = 1;
2603 void *data;
2604 unsigned int size;
2605
2606 data = &delete_pending;
2607 size = 1; /* sizeof __u8 */
2608
2609 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2610 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2611 &size);
2612}
2613
2614int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002615SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2616 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2617{
2618 struct smb2_file_link_info info;
2619 void **data;
2620 unsigned int size[2];
2621 int rc;
2622 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2623
2624 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2625 if (!data)
2626 return -ENOMEM;
2627
2628 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2629 /* 0 = fail if link already exists */
2630 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2631 info.FileNameLength = cpu_to_le32(len);
2632
2633 data[0] = &info;
2634 size[0] = sizeof(struct smb2_file_link_info);
2635
2636 data[1] = target_file;
2637 size[1] = len + 2 /* null */;
2638
2639 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002640 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002641 kfree(data);
2642 return rc;
2643}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002644
2645int
2646SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05002647 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002648{
2649 struct smb2_file_eof_info info;
2650 void *data;
2651 unsigned int size;
2652
2653 info.EndOfFile = *eof;
2654
2655 data = &info;
2656 size = sizeof(struct smb2_file_eof_info);
2657
Steve Frenchf29ebb42014-07-19 21:44:58 -05002658 if (is_falloc)
2659 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2660 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2661 else
2662 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2663 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002664}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07002665
2666int
2667SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2668 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2669{
2670 unsigned int size;
2671 size = sizeof(FILE_BASIC_INFO);
2672 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2673 current->tgid, FILE_BASIC_INFORMATION, 1,
2674 (void **)&buf, &size);
2675}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002676
2677int
2678SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2679 const u64 persistent_fid, const u64 volatile_fid,
2680 __u8 oplock_level)
2681{
2682 int rc;
2683 struct smb2_oplock_break *req = NULL;
2684
Joe Perchesf96637b2013-05-04 22:12:25 -05002685 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002686 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2687
2688 if (rc)
2689 return rc;
2690
2691 req->VolatileFid = volatile_fid;
2692 req->PersistentFid = persistent_fid;
2693 req->OplockLevel = oplock_level;
2694 req->hdr.CreditRequest = cpu_to_le16(1);
2695
2696 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2697 /* SMB2 buffer freed by function above */
2698
2699 if (rc) {
2700 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002701 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002702 }
2703
2704 return rc;
2705}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002706
2707static void
2708copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2709 struct kstatfs *kst)
2710{
2711 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2712 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2713 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
2714 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
2715 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
2716 return;
2717}
2718
2719static int
2720build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2721 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2722{
2723 int rc;
2724 struct smb2_query_info_req *req;
2725
Joe Perchesf96637b2013-05-04 22:12:25 -05002726 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002727
2728 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2729 return -EIO;
2730
2731 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2732 if (rc)
2733 return rc;
2734
2735 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2736 req->FileInfoClass = level;
2737 req->PersistentFileId = persistent_fid;
2738 req->VolatileFileId = volatile_fid;
2739 /* 4 for rfc1002 length field and 1 for pad */
2740 req->InputBufferOffset =
2741 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2742 req->OutputBufferLength = cpu_to_le32(
2743 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2744
2745 iov->iov_base = (char *)req;
2746 /* 4 for rfc1002 length field */
2747 iov->iov_len = get_rfc1002_length(req) + 4;
2748 return 0;
2749}
2750
2751int
2752SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2753 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2754{
2755 struct smb2_query_info_rsp *rsp = NULL;
2756 struct kvec iov;
2757 int rc = 0;
2758 int resp_buftype;
2759 struct cifs_ses *ses = tcon->ses;
2760 struct smb2_fs_full_size_info *info = NULL;
2761
2762 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
2763 sizeof(struct smb2_fs_full_size_info),
2764 persistent_fid, volatile_fid);
2765 if (rc)
2766 return rc;
2767
2768 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2769 if (rc) {
2770 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05002771 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002772 }
2773 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2774
2775 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
2776 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
2777 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2778 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2779 sizeof(struct smb2_fs_full_size_info));
2780 if (!rc)
2781 copy_fs_info_to_kstatfs(info, fsdata);
2782
Steve French34f62642013-10-09 02:07:00 -05002783qfsinf_exit:
2784 free_rsp_buf(resp_buftype, iov.iov_base);
2785 return rc;
2786}
2787
2788int
2789SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05002790 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05002791{
2792 struct smb2_query_info_rsp *rsp = NULL;
2793 struct kvec iov;
2794 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05002795 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05002796 struct cifs_ses *ses = tcon->ses;
2797 unsigned int rsp_len, offset;
2798
Steven French21671142013-10-09 13:36:35 -05002799 if (level == FS_DEVICE_INFORMATION) {
2800 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
2801 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
2802 } else if (level == FS_ATTRIBUTE_INFORMATION) {
2803 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
2804 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05002805 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
2806 max_len = sizeof(struct smb3_fs_ss_info);
2807 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05002808 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05002809 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05002810 return -EINVAL;
2811 }
2812
2813 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05002814 persistent_fid, volatile_fid);
2815 if (rc)
2816 return rc;
2817
2818 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2819 if (rc) {
2820 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2821 goto qfsattr_exit;
2822 }
2823 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2824
2825 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
2826 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05002827 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
2828 if (rc)
2829 goto qfsattr_exit;
2830
2831 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05002832 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
2833 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05002834 rsp_len, max_len));
2835 else if (level == FS_DEVICE_INFORMATION)
2836 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
2837 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05002838 else if (level == FS_SECTOR_SIZE_INFORMATION) {
2839 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
2840 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
2841 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
2842 tcon->perf_sector_size =
2843 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
2844 }
Steve French34f62642013-10-09 02:07:00 -05002845
2846qfsattr_exit:
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002847 free_rsp_buf(resp_buftype, iov.iov_base);
2848 return rc;
2849}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07002850
2851int
2852smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2853 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2854 const __u32 num_lock, struct smb2_lock_element *buf)
2855{
2856 int rc = 0;
2857 struct smb2_lock_req *req = NULL;
2858 struct kvec iov[2];
2859 int resp_buf_type;
2860 unsigned int count;
2861
Joe Perchesf96637b2013-05-04 22:12:25 -05002862 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07002863
2864 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
2865 if (rc)
2866 return rc;
2867
2868 req->hdr.ProcessId = cpu_to_le32(pid);
2869 req->LockCount = cpu_to_le16(num_lock);
2870
2871 req->PersistentFileId = persist_fid;
2872 req->VolatileFileId = volatile_fid;
2873
2874 count = num_lock * sizeof(struct smb2_lock_element);
2875 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
2876
2877 iov[0].iov_base = (char *)req;
2878 /* 4 for rfc1002 length field and count for all locks */
2879 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
2880 iov[1].iov_base = (char *)buf;
2881 iov[1].iov_len = count;
2882
2883 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2884 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2885 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002886 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07002887 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
2888 }
2889
2890 return rc;
2891}
2892
2893int
2894SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
2895 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2896 const __u64 length, const __u64 offset, const __u32 lock_flags,
2897 const bool wait)
2898{
2899 struct smb2_lock_element lock;
2900
2901 lock.Offset = cpu_to_le64(offset);
2902 lock.Length = cpu_to_le64(length);
2903 lock.Flags = cpu_to_le32(lock_flags);
2904 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
2905 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
2906
2907 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
2908}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07002909
2910int
2911SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
2912 __u8 *lease_key, const __le32 lease_state)
2913{
2914 int rc;
2915 struct smb2_lease_ack *req = NULL;
2916
Joe Perchesf96637b2013-05-04 22:12:25 -05002917 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07002918 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2919
2920 if (rc)
2921 return rc;
2922
2923 req->hdr.CreditRequest = cpu_to_le16(1);
2924 req->StructureSize = cpu_to_le16(36);
2925 inc_rfc1001_len(req, 12);
2926
2927 memcpy(req->LeaseKey, lease_key, 16);
2928 req->LeaseState = lease_state;
2929
2930 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2931 /* SMB2 buffer freed by function above */
2932
2933 if (rc) {
2934 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002935 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07002936 }
2937
2938 return rc;
2939}