blob: f6ba2c03f7cce1b6a7ec0f855d3170dfd02a9d5b [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
Pavel Shilovskycb200bd2016-10-24 16:59:57 -070082smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040083 const struct cifs_tcon *tcon)
84{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -070085 shdr->ProtocolId = SMB2_PROTO_NUMBER;
86 shdr->StructureSize = cpu_to_le16(64);
87 shdr->Command = smb2_cmd;
Ross Lagerwall7d414f32016-09-20 13:37:13 +010088 if (tcon && tcon->ses && tcon->ses->server) {
89 struct TCP_Server_Info *server = tcon->ses->server;
90
91 spin_lock(&server->req_lock);
92 /* Request up to 2 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -050093 if (server->credits >= server->max_credits)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -070094 shdr->CreditRequest = cpu_to_le16(0);
Ross Lagerwall7d414f32016-09-20 13:37:13 +010095 else
Pavel Shilovsky31473fc2016-10-24 15:33:04 -070096 shdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -050097 min_t(int, server->max_credits -
Ross Lagerwall7d414f32016-09-20 13:37:13 +010098 server->credits, 2));
99 spin_unlock(&server->req_lock);
100 } else {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700101 shdr->CreditRequest = cpu_to_le16(2);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100102 }
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700103 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400104
105 if (!tcon)
106 goto out;
107
Steve French2b80d042013-06-23 18:43:37 -0500108 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
109 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500110 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500111 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700112 shdr->CreditCharge = cpu_to_le16(1);
Steve French2b80d042013-06-23 18:43:37 -0500113 /* else CreditCharge MBZ */
114
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700115 shdr->TreeId = tcon->tid;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400116 /* Uid is not converted */
117 if (tcon->ses)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700118 shdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500119
120 /*
121 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
122 * to pass the path on the Open SMB prefixed by \\server\share.
123 * Not sure when we would need to do the augmented path (if ever) and
124 * setting this flag breaks the SMB2 open operation since it is
125 * illegal to send an empty path name (without \\server\share prefix)
126 * when the DFS flag is set in the SMB open header. We could
127 * consider setting the flag on all operations other than open
128 * but it is safer to net set it for now.
129 */
130/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700131 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
Steve Frenchf87ab882013-06-26 19:14:55 -0500132
Jeff Layton38d77c52013-05-26 07:01:00 -0400133 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700134 shdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400135out:
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400136 return;
137}
138
139static int
140smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
141{
142 int rc = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400143 struct nls_table *nls_codepage;
144 struct cifs_ses *ses;
145 struct TCP_Server_Info *server;
146
147 /*
148 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
149 * check for tcp and smb session status done differently
150 * for those three - in the calling routine.
151 */
152 if (tcon == NULL)
153 return rc;
154
155 if (smb2_command == SMB2_TREE_CONNECT)
156 return rc;
157
158 if (tcon->tidStatus == CifsExiting) {
159 /*
160 * only tree disconnect, open, and write,
161 * (and ulogoff which does not have tcon)
162 * are allowed as we start force umount.
163 */
164 if ((smb2_command != SMB2_WRITE) &&
165 (smb2_command != SMB2_CREATE) &&
166 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500167 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
168 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400169 return -ENODEV;
170 }
171 }
172 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
173 (!tcon->ses->server))
174 return -EIO;
175
176 ses = tcon->ses;
177 server = ses->server;
178
179 /*
180 * Give demultiplex thread up to 10 seconds to reconnect, should be
181 * greater than cifs socket timeout which is 7 seconds
182 */
183 while (server->tcpStatus == CifsNeedReconnect) {
184 /*
185 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
186 * here since they are implicitly done when session drops.
187 */
188 switch (smb2_command) {
189 /*
190 * BB Should we keep oplock break and add flush to exceptions?
191 */
192 case SMB2_TREE_DISCONNECT:
193 case SMB2_CANCEL:
194 case SMB2_CLOSE:
195 case SMB2_OPLOCK_BREAK:
196 return -EAGAIN;
197 }
198
199 wait_event_interruptible_timeout(server->response_q,
200 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
201
202 /* are we still trying to reconnect? */
203 if (server->tcpStatus != CifsNeedReconnect)
204 break;
205
206 /*
207 * on "soft" mounts we wait once. Hard mounts keep
208 * retrying until process is killed or server comes
209 * back on-line
210 */
211 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500212 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400213 return -EHOSTDOWN;
214 }
215 }
216
217 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
218 return rc;
219
220 nls_codepage = load_nls_default();
221
222 /*
223 * need to prevent multiple threads trying to simultaneously reconnect
224 * the same SMB session
225 */
226 mutex_lock(&tcon->ses->session_mutex);
227 rc = cifs_negotiate_protocol(0, tcon->ses);
228 if (!rc && tcon->ses->need_reconnect)
229 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
230
231 if (rc || !tcon->need_reconnect) {
232 mutex_unlock(&tcon->ses->session_mutex);
233 goto out;
234 }
235
236 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800237 if (tcon->use_persistent)
238 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500239
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400240 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
241 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500242
Joe Perchesf96637b2013-05-04 22:12:25 -0500243 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400244 if (rc)
245 goto out;
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800246
247 if (smb2_command != SMB2_INTERNAL_CMD)
248 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
249
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400250 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400251out:
252 /*
253 * Check if handle based operation so we know whether we can continue
254 * or not without returning to caller to reset file handle.
255 */
256 /*
257 * BB Is flush done by server on drop of tcp session? Should we special
258 * case it and skip above?
259 */
260 switch (smb2_command) {
261 case SMB2_FLUSH:
262 case SMB2_READ:
263 case SMB2_WRITE:
264 case SMB2_LOCK:
265 case SMB2_IOCTL:
266 case SMB2_QUERY_DIRECTORY:
267 case SMB2_CHANGE_NOTIFY:
268 case SMB2_QUERY_INFO:
269 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800270 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400271 }
272 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400273 return rc;
274}
275
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700276static void
277fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
278 unsigned int *total_len)
279{
280 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
281 /* lookup word count ie StructureSize from table */
282 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
283
284 /*
285 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
286 * largest operations (Create)
287 */
288 memset(buf, 0, 256);
289
290 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
291 spdu->StructureSize2 = cpu_to_le16(parmsize);
292
293 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
294}
295
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400296/*
297 * Allocate and return pointer to an SMB request hdr, and set basic
298 * SMB information in the SMB header. If the return code is zero, this
299 * function must have filled in request_buf pointer.
300 */
301static int
302small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
303 void **request_buf)
304{
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700305 int rc;
306 unsigned int total_len;
307 struct smb2_pdu *pdu;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400308
309 rc = smb2_reconnect(smb2_command, tcon);
310 if (rc)
311 return rc;
312
313 /* BB eventually switch this to SMB2 specific small buf size */
314 *request_buf = cifs_small_buf_get();
315 if (*request_buf == NULL) {
316 /* BB should we add a retry in here if not a writepage? */
317 return -ENOMEM;
318 }
319
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700320 pdu = (struct smb2_pdu *)(*request_buf);
321
322 fill_small_buf(smb2_command, tcon, get_sync_hdr(pdu), &total_len);
323
324 /* Note this is only network field converted to big endian */
325 pdu->hdr.smb2_buf_length = cpu_to_be32(total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400326
327 if (tcon != NULL) {
328#ifdef CONFIG_CIFS_STATS2
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400329 uint16_t com_code = le16_to_cpu(smb2_command);
330 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400331#endif
332 cifs_stats_inc(&tcon->num_smbs_sent);
333 }
334
335 return rc;
336}
337
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500338#ifdef CONFIG_CIFS_SMB311
339/* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
340#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
341
342
343#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
344#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
345
346static void
347build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
348{
349 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
350 pneg_ctxt->DataLength = cpu_to_le16(38);
351 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
352 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
353 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
354 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
355}
356
357static void
358build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
359{
360 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
361 pneg_ctxt->DataLength = cpu_to_le16(6);
362 pneg_ctxt->CipherCount = cpu_to_le16(2);
363 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
364 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
365}
366
367static void
368assemble_neg_contexts(struct smb2_negotiate_req *req)
369{
370
371 /* +4 is to account for the RFC1001 len field */
372 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
373
374 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
375 /* Add 2 to size to round to 8 byte boundary */
376 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
377 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
378 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
379 req->NegotiateContextCount = cpu_to_le16(2);
380 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context) + 2
381 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
382}
383#else
384static void assemble_neg_contexts(struct smb2_negotiate_req *req)
385{
386 return;
387}
388#endif /* SMB311 */
389
390
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400391/*
392 *
393 * SMB2 Worker functions follow:
394 *
395 * The general structure of the worker functions is:
396 * 1) Call smb2_init (assembles SMB2 header)
397 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
398 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
399 * 4) Decode SMB2 command specific fields in the fixed length area
400 * 5) Decode variable length data area (if any for this SMB2 command type)
401 * 6) Call free smb buffer
402 * 7) return
403 *
404 */
405
406int
407SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
408{
409 struct smb2_negotiate_req *req;
410 struct smb2_negotiate_rsp *rsp;
411 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700412 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400413 int rc = 0;
414 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400415 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400416 int blob_offset, blob_length;
417 char *security_blob;
418 int flags = CIFS_NEG_OP;
419
Joe Perchesf96637b2013-05-04 22:12:25 -0500420 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400421
Jeff Layton3534b852013-05-24 07:41:01 -0400422 if (!server) {
423 WARN(1, "%s: server is NULL!\n", __func__);
424 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400425 }
426
427 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
428 if (rc)
429 return rc;
430
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700431 req->hdr.sync_hdr.SessionId = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400432
Steve Frenche4aa25e2012-10-01 12:26:22 -0500433 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400434
Steve Frenche4aa25e2012-10-01 12:26:22 -0500435 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
436 inc_rfc1001_len(req, 2);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400437
438 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400439 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500440 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400441 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500442 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400443 else
444 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400445
Steve Frenche4aa25e2012-10-01 12:26:22 -0500446 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400447
Steve French3c5f9be12014-05-13 13:37:45 -0700448 /* ClientGUID must be zero for SMB2.02 dialect */
449 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
450 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500451 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700452 memcpy(req->ClientGUID, server->client_guid,
453 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500454 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
455 assemble_neg_contexts(req);
456 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400457 iov[0].iov_base = (char *)req;
458 /* 4 for rfc1002 length field */
459 iov[0].iov_len = get_rfc1002_length(req) + 4;
460
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700461 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
462 cifs_small_buf_release(req);
463 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400464 /*
465 * No tcon so can't do
466 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
467 */
468 if (rc != 0)
469 goto neg_exit;
470
Joe Perchesf96637b2013-05-04 22:12:25 -0500471 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400472
Steve Frenche4aa25e2012-10-01 12:26:22 -0500473 /* BB we may eventually want to match the negotiated vs. requested
474 dialect, even though we are only requesting one at a time */
475 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500476 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500477 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500478 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500479 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500480 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500481 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
482 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600483#ifdef CONFIG_CIFS_SMB311
484 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
485 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
486#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400487 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500488 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500489 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400490 rc = -EIO;
491 goto neg_exit;
492 }
493 server->dialect = le16_to_cpu(rsp->DialectRevision);
494
Jeff Laytone598d1d82013-05-26 07:00:59 -0400495 /* SMB2 only has an extended negflavor */
496 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400497 /* set it to the maximum buffer size value we can send with 1 credit */
498 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
499 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400500 server->max_read = le32_to_cpu(rsp->MaxReadSize);
501 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
502 /* BB Do we need to validate the SecurityMode? */
503 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
504 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400505 /* Internal types */
506 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400507
508 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
509 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500510 /*
511 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
512 * for us will be
513 * ses->sectype = RawNTLMSSP;
514 * but for time being this is our only auth choice so doesn't matter.
515 * We just found a server which sets blob length to zero expecting raw.
516 */
517 if (blob_length == 0)
518 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700519
Jeff Layton38d77c52013-05-26 07:01:00 -0400520 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400521 if (rc)
522 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500523 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500524 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500525 if (rc == 1)
526 rc = 0;
527 else if (rc == 0)
528 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400529 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400530neg_exit:
531 free_rsp_buf(resp_buftype, rsp);
532 return rc;
533}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400534
Steve Frenchff1c0382013-11-19 23:44:46 -0600535int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
536{
537 int rc = 0;
538 struct validate_negotiate_info_req vneg_inbuf;
539 struct validate_negotiate_info_rsp *pneg_rsp;
540 u32 rsplen;
541
542 cifs_dbg(FYI, "validate negotiate\n");
543
544 /*
545 * validation ioctl must be signed, so no point sending this if we
546 * can not sign it. We could eventually change this to selectively
547 * sign just this, the first and only signed request on a connection.
548 * This is good enough for now since a user who wants better security
549 * would also enable signing on the mount. Having validation of
550 * negotiate info for signed connections helps reduce attack vectors
551 */
552 if (tcon->ses->server->sign == false)
553 return 0; /* validation requires signing */
554
555 vneg_inbuf.Capabilities =
556 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100557 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
558 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600559
560 if (tcon->ses->sign)
561 vneg_inbuf.SecurityMode =
562 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
563 else if (global_secflags & CIFSSEC_MAY_SIGN)
564 vneg_inbuf.SecurityMode =
565 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
566 else
567 vneg_inbuf.SecurityMode = 0;
568
569 vneg_inbuf.DialectCount = cpu_to_le16(1);
570 vneg_inbuf.Dialects[0] =
571 cpu_to_le16(tcon->ses->server->vals->protocol_id);
572
573 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
574 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
575 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
576 (char **)&pneg_rsp, &rsplen);
577
578 if (rc != 0) {
579 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
580 return -EIO;
581 }
582
583 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
584 cifs_dbg(VFS, "invalid size of protocol negotiate response\n");
585 return -EIO;
586 }
587
588 /* check validate negotiate info response matches what we got earlier */
589 if (pneg_rsp->Dialect !=
590 cpu_to_le16(tcon->ses->server->vals->protocol_id))
591 goto vneg_out;
592
593 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
594 goto vneg_out;
595
596 /* do not validate server guid because not saved at negprot time yet */
597
598 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
599 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
600 goto vneg_out;
601
602 /* validate negotiate successful */
603 cifs_dbg(FYI, "validate negotiate info successful\n");
604 return 0;
605
606vneg_out:
607 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
608 return -EIO;
609}
610
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100611struct SMB2_sess_data {
612 unsigned int xid;
613 struct cifs_ses *ses;
614 struct nls_table *nls_cp;
615 void (*func)(struct SMB2_sess_data *);
616 int result;
617 u64 previous_session;
618
619 /* we will send the SMB in three pieces:
620 * a fixed length beginning part, an optional
621 * SPNEGO blob (which can be zero length), and a
622 * last part which will include the strings
623 * and rest of bcc area. This allows us to avoid
624 * a large buffer 17K allocation
625 */
626 int buf0_type;
627 struct kvec iov[2];
628};
629
630static int
631SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
632{
633 int rc;
634 struct cifs_ses *ses = sess_data->ses;
635 struct smb2_sess_setup_req *req;
636 struct TCP_Server_Info *server = ses->server;
637
638 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
639 if (rc)
640 return rc;
641
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700642 /* First session, not a reauthenticate */
643 req->hdr.sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100644
645 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
646 req->PreviousSessionId = sess_data->previous_session;
647
648 req->Flags = 0; /* MBZ */
649 /* to enable echos and oplocks */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700650 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(3);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100651
652 /* only one of SMB2 signing flags may be set in SMB2 request */
653 if (server->sign)
654 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
655 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
656 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
657 else
658 req->SecurityMode = 0;
659
660 req->Capabilities = 0;
661 req->Channel = 0; /* MBZ */
662
663 sess_data->iov[0].iov_base = (char *)req;
664 /* 4 for rfc1002 length field and 1 for pad */
665 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
666 /*
667 * This variable will be used to clear the buffer
668 * allocated above in case of any error in the calling function.
669 */
670 sess_data->buf0_type = CIFS_SMALL_BUFFER;
671
672 return 0;
673}
674
675static void
676SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
677{
678 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
679 sess_data->buf0_type = CIFS_NO_BUFFER;
680}
681
682static int
683SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
684{
685 int rc;
686 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700687 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100688
689 /* Testing shows that buffer offset must be at location of Buffer[0] */
690 req->SecurityBufferOffset =
691 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
692 1 /* pad */ - 4 /* rfc1001 len */);
693 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
694
695 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
696
697 /* BB add code to build os and lm fields */
698
699 rc = SendReceive2(sess_data->xid, sess_data->ses,
700 sess_data->iov, 2,
701 &sess_data->buf0_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700702 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
703 cifs_small_buf_release(sess_data->iov[0].iov_base);
704 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100705
706 return rc;
707}
708
709static int
710SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
711{
712 int rc = 0;
713 struct cifs_ses *ses = sess_data->ses;
714
715 mutex_lock(&ses->server->srv_mutex);
716 if (ses->server->sign && ses->server->ops->generate_signingkey) {
717 rc = ses->server->ops->generate_signingkey(ses);
718 kfree(ses->auth_key.response);
719 ses->auth_key.response = NULL;
720 if (rc) {
721 cifs_dbg(FYI,
722 "SMB3 session key generation failed\n");
723 mutex_unlock(&ses->server->srv_mutex);
724 goto keygen_exit;
725 }
726 }
727 if (!ses->server->session_estab) {
728 ses->server->sequence_number = 0x2;
729 ses->server->session_estab = true;
730 }
731 mutex_unlock(&ses->server->srv_mutex);
732
733 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
734 spin_lock(&GlobalMid_Lock);
735 ses->status = CifsGood;
736 ses->need_reconnect = false;
737 spin_unlock(&GlobalMid_Lock);
738
739keygen_exit:
740 if (!ses->server->sign) {
741 kfree(ses->auth_key.response);
742 ses->auth_key.response = NULL;
743 }
744 return rc;
745}
746
747#ifdef CONFIG_CIFS_UPCALL
748static void
749SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
750{
751 int rc;
752 struct cifs_ses *ses = sess_data->ses;
753 struct cifs_spnego_msg *msg;
754 struct key *spnego_key = NULL;
755 struct smb2_sess_setup_rsp *rsp = NULL;
756
757 rc = SMB2_sess_alloc_buffer(sess_data);
758 if (rc)
759 goto out;
760
761 spnego_key = cifs_get_spnego_key(ses);
762 if (IS_ERR(spnego_key)) {
763 rc = PTR_ERR(spnego_key);
764 spnego_key = NULL;
765 goto out;
766 }
767
768 msg = spnego_key->payload.data[0];
769 /*
770 * check version field to make sure that cifs.upcall is
771 * sending us a response in an expected form
772 */
773 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
774 cifs_dbg(VFS,
775 "bad cifs.upcall version. Expected %d got %d",
776 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
777 rc = -EKEYREJECTED;
778 goto out_put_spnego_key;
779 }
780
781 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
782 GFP_KERNEL);
783 if (!ses->auth_key.response) {
784 cifs_dbg(VFS,
785 "Kerberos can't allocate (%u bytes) memory",
786 msg->sesskey_len);
787 rc = -ENOMEM;
788 goto out_put_spnego_key;
789 }
790 ses->auth_key.len = msg->sesskey_len;
791
792 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
793 sess_data->iov[1].iov_len = msg->secblob_len;
794
795 rc = SMB2_sess_sendreceive(sess_data);
796 if (rc)
797 goto out_put_spnego_key;
798
799 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700800 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100801
802 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
803 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
804 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
805
806 rc = SMB2_sess_establish_session(sess_data);
807out_put_spnego_key:
808 key_invalidate(spnego_key);
809 key_put(spnego_key);
810out:
811 sess_data->result = rc;
812 sess_data->func = NULL;
813 SMB2_sess_free_buffer(sess_data);
814}
815#else
816static void
817SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
818{
819 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
820 sess_data->result = -EOPNOTSUPP;
821 sess_data->func = NULL;
822}
823#endif
824
Sachin Prabhu166cea42016-10-07 19:11:22 +0100825static void
826SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
827
828static void
829SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
830{
831 int rc;
832 struct cifs_ses *ses = sess_data->ses;
833 struct smb2_sess_setup_rsp *rsp = NULL;
834 char *ntlmssp_blob = NULL;
835 bool use_spnego = false; /* else use raw ntlmssp */
836 u16 blob_length = 0;
837
838 /*
839 * If memory allocation is successful, caller of this function
840 * frees it.
841 */
842 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
843 if (!ses->ntlmssp) {
844 rc = -ENOMEM;
845 goto out_err;
846 }
847 ses->ntlmssp->sesskey_per_smbsess = true;
848
849 rc = SMB2_sess_alloc_buffer(sess_data);
850 if (rc)
851 goto out_err;
852
853 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
854 GFP_KERNEL);
855 if (ntlmssp_blob == NULL) {
856 rc = -ENOMEM;
857 goto out;
858 }
859
860 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
861 if (use_spnego) {
862 /* BB eventually need to add this */
863 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
864 rc = -EOPNOTSUPP;
865 goto out;
866 } else {
867 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
868 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
869 }
870 sess_data->iov[1].iov_base = ntlmssp_blob;
871 sess_data->iov[1].iov_len = blob_length;
872
873 rc = SMB2_sess_sendreceive(sess_data);
874 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
875
876 /* If true, rc here is expected and not an error */
877 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700878 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +0100879 rc = 0;
880
881 if (rc)
882 goto out;
883
884 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
885 le16_to_cpu(rsp->SecurityBufferOffset)) {
886 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
887 le16_to_cpu(rsp->SecurityBufferOffset));
888 rc = -EIO;
889 goto out;
890 }
891 rc = decode_ntlmssp_challenge(rsp->Buffer,
892 le16_to_cpu(rsp->SecurityBufferLength), ses);
893 if (rc)
894 goto out;
895
896 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
897
898
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700899 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +0100900 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
901 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
902 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
903
904out:
905 kfree(ntlmssp_blob);
906 SMB2_sess_free_buffer(sess_data);
907 if (!rc) {
908 sess_data->result = 0;
909 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
910 return;
911 }
912out_err:
913 kfree(ses->ntlmssp);
914 ses->ntlmssp = NULL;
915 sess_data->result = rc;
916 sess_data->func = NULL;
917}
918
919static void
920SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
921{
922 int rc;
923 struct cifs_ses *ses = sess_data->ses;
924 struct smb2_sess_setup_req *req;
925 struct smb2_sess_setup_rsp *rsp = NULL;
926 unsigned char *ntlmssp_blob = NULL;
927 bool use_spnego = false; /* else use raw ntlmssp */
928 u16 blob_length = 0;
929
930 rc = SMB2_sess_alloc_buffer(sess_data);
931 if (rc)
932 goto out;
933
934 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700935 req->hdr.sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +0100936
937 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
938 sess_data->nls_cp);
939 if (rc) {
940 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
941 goto out;
942 }
943
944 if (use_spnego) {
945 /* BB eventually need to add this */
946 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
947 rc = -EOPNOTSUPP;
948 goto out;
949 }
950 sess_data->iov[1].iov_base = ntlmssp_blob;
951 sess_data->iov[1].iov_len = blob_length;
952
953 rc = SMB2_sess_sendreceive(sess_data);
954 if (rc)
955 goto out;
956
957 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
958
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700959 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +0100960 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
961 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
962 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
963
964 rc = SMB2_sess_establish_session(sess_data);
965out:
966 kfree(ntlmssp_blob);
967 SMB2_sess_free_buffer(sess_data);
968 kfree(ses->ntlmssp);
969 ses->ntlmssp = NULL;
970 sess_data->result = rc;
971 sess_data->func = NULL;
972}
973
974static int
975SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
976{
977 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
978 ses->sectype = RawNTLMSSP;
979
980 switch (ses->sectype) {
981 case Kerberos:
982 sess_data->func = SMB2_auth_kerberos;
983 break;
984 case RawNTLMSSP:
985 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
986 break;
987 default:
988 cifs_dbg(VFS, "secType %d not supported!\n", ses->sectype);
989 return -EOPNOTSUPP;
990 }
991
992 return 0;
993}
994
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400995int
996SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
997 const struct nls_table *nls_cp)
998{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400999 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001000 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001001 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001002
Joe Perchesf96637b2013-05-04 22:12:25 -05001003 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001004
Jeff Layton3534b852013-05-24 07:41:01 -04001005 if (!server) {
1006 WARN(1, "%s: server is NULL!\n", __func__);
1007 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001008 }
1009
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001010 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1011 if (!sess_data)
1012 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001013
1014 rc = SMB2_select_sec(ses, sess_data);
1015 if (rc)
1016 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001017 sess_data->xid = xid;
1018 sess_data->ses = ses;
1019 sess_data->buf0_type = CIFS_NO_BUFFER;
1020 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001021
Sachin Prabhu166cea42016-10-07 19:11:22 +01001022 while (sess_data->func)
1023 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001024
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001025 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001026out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001027 kfree(sess_data);
1028 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001029}
1030
1031int
1032SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1033{
1034 struct smb2_logoff_req *req; /* response is also trivial struct */
1035 int rc = 0;
1036 struct TCP_Server_Info *server;
1037
Joe Perchesf96637b2013-05-04 22:12:25 -05001038 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001039
1040 if (ses && (ses->server))
1041 server = ses->server;
1042 else
1043 return -EIO;
1044
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001045 /* no need to send SMB logoff if uid already closed due to reconnect */
1046 if (ses->need_reconnect)
1047 goto smb2_session_already_dead;
1048
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001049 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1050 if (rc)
1051 return rc;
1052
1053 /* since no tcon, smb2_init can not do this, so do here */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001054 req->hdr.sync_hdr.SessionId = ses->Suid;
Jeff Layton38d77c52013-05-26 07:01:00 -04001055 if (server->sign)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001056 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001057
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001058 rc = SendReceiveNoRsp(xid, ses, (char *) req, 0);
1059 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001060 /*
1061 * No tcon so can't do
1062 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1063 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001064
1065smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001066 return rc;
1067}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001068
1069static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1070{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001071 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001072}
1073
1074#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1075
Steve Frenchde9f68d2013-11-15 11:26:24 -06001076/* These are similar values to what Windows uses */
1077static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1078{
1079 tcon->max_chunks = 256;
1080 tcon->max_bytes_chunk = 1048576;
1081 tcon->max_bytes_copy = 16777216;
1082}
1083
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001084int
1085SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1086 struct cifs_tcon *tcon, const struct nls_table *cp)
1087{
1088 struct smb2_tree_connect_req *req;
1089 struct smb2_tree_connect_rsp *rsp = NULL;
1090 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001091 struct kvec rsp_iov;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001092 int rc = 0;
1093 int resp_buftype;
1094 int unc_path_len;
1095 struct TCP_Server_Info *server;
1096 __le16 *unc_path = NULL;
1097
Joe Perchesf96637b2013-05-04 22:12:25 -05001098 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001099
1100 if ((ses->server) && tree)
1101 server = ses->server;
1102 else
1103 return -EIO;
1104
1105 if (tcon && tcon->bad_network_name)
1106 return -ENOENT;
1107
Steve Frenchff9f84b2015-09-26 09:48:58 -05001108 if ((tcon && tcon->seal) &&
Steve French88627142015-09-22 03:16:27 -05001109 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
1110 cifs_dbg(VFS, "encryption requested but no server support");
1111 return -EOPNOTSUPP;
1112 }
1113
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001114 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1115 if (unc_path == NULL)
1116 return -ENOMEM;
1117
1118 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1119 unc_path_len *= 2;
1120 if (unc_path_len < 2) {
1121 kfree(unc_path);
1122 return -EINVAL;
1123 }
1124
1125 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1126 if (rc) {
1127 kfree(unc_path);
1128 return rc;
1129 }
1130
1131 if (tcon == NULL) {
1132 /* since no tcon, smb2_init can not do this, so do here */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001133 req->hdr.sync_hdr.SessionId = ses->Suid;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001134 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
1135 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
1136 }
1137
1138 iov[0].iov_base = (char *)req;
1139 /* 4 for rfc1002 length field and 1 for pad */
1140 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1141
1142 /* Testing shows that buffer offset must be at location of Buffer[0] */
1143 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1144 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1145 req->PathLength = cpu_to_le16(unc_path_len - 2);
1146 iov[1].iov_base = unc_path;
1147 iov[1].iov_len = unc_path_len;
1148
1149 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1150
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001151 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0, &rsp_iov);
1152 cifs_small_buf_release(req);
1153 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001154
1155 if (rc != 0) {
1156 if (tcon) {
1157 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1158 tcon->need_reconnect = true;
1159 }
1160 goto tcon_error_exit;
1161 }
1162
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001163 if (tcon == NULL) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001164 ses->ipc_tid = rsp->hdr.sync_hdr.TreeId;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001165 goto tcon_exit;
1166 }
1167
1168 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
Joe Perchesf96637b2013-05-04 22:12:25 -05001169 cifs_dbg(FYI, "connection to disk share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001170 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
1171 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001172 cifs_dbg(FYI, "connection to pipe share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001173 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
1174 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001175 cifs_dbg(FYI, "connection to printer\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001176 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001177 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001178 rc = -EOPNOTSUPP;
1179 goto tcon_error_exit;
1180 }
1181
1182 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001183 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001184 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1185 tcon->tidStatus = CifsGood;
1186 tcon->need_reconnect = false;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001187 tcon->tid = rsp->hdr.sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001188 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001189
1190 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1191 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001192 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Steve Frenchde9f68d2013-11-15 11:26:24 -06001193 init_copy_chunk_defaults(tcon);
Steve French88627142015-09-22 03:16:27 -05001194 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1195 cifs_dbg(VFS, "Encrypted shares not supported");
Steve Frenchff1c0382013-11-19 23:44:46 -06001196 if (tcon->ses->server->ops->validate_negotiate)
1197 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001198tcon_exit:
1199 free_rsp_buf(resp_buftype, rsp);
1200 kfree(unc_path);
1201 return rc;
1202
1203tcon_error_exit:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001204 if (rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001205 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Steve French18f39e72014-08-17 00:22:24 -05001206 if (tcon)
1207 tcon->bad_network_name = true;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001208 }
1209 goto tcon_exit;
1210}
1211
1212int
1213SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1214{
1215 struct smb2_tree_disconnect_req *req; /* response is trivial */
1216 int rc = 0;
1217 struct TCP_Server_Info *server;
1218 struct cifs_ses *ses = tcon->ses;
1219
Joe Perchesf96637b2013-05-04 22:12:25 -05001220 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001221
1222 if (ses && (ses->server))
1223 server = ses->server;
1224 else
1225 return -EIO;
1226
1227 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1228 return 0;
1229
1230 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1231 if (rc)
1232 return rc;
1233
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001234 rc = SendReceiveNoRsp(xid, ses, (char *)req, 0);
1235 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001236 if (rc)
1237 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1238
1239 return rc;
1240}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001241
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001242
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001243static struct create_durable *
1244create_durable_buf(void)
1245{
1246 struct create_durable *buf;
1247
1248 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1249 if (!buf)
1250 return NULL;
1251
1252 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001253 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001254 buf->ccontext.DataLength = cpu_to_le32(16);
1255 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1256 (struct create_durable, Name));
1257 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001258 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001259 buf->Name[0] = 'D';
1260 buf->Name[1] = 'H';
1261 buf->Name[2] = 'n';
1262 buf->Name[3] = 'Q';
1263 return buf;
1264}
1265
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001266static struct create_durable *
1267create_reconnect_durable_buf(struct cifs_fid *fid)
1268{
1269 struct create_durable *buf;
1270
1271 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1272 if (!buf)
1273 return NULL;
1274
1275 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1276 (struct create_durable, Data));
1277 buf->ccontext.DataLength = cpu_to_le32(16);
1278 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1279 (struct create_durable, Name));
1280 buf->ccontext.NameLength = cpu_to_le16(4);
1281 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1282 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001283 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001284 buf->Name[0] = 'D';
1285 buf->Name[1] = 'H';
1286 buf->Name[2] = 'n';
1287 buf->Name[3] = 'C';
1288 return buf;
1289}
1290
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001291static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001292parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1293 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001294{
1295 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001296 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001297 unsigned int next;
1298 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001299 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001300
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001301 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001302 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001303 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001304 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001305 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001306 if (le16_to_cpu(cc->NameLength) == 4 &&
1307 strncmp(name, "RqLs", 4) == 0)
1308 return server->ops->parse_lease_buf(cc, epoch);
1309
1310 next = le32_to_cpu(cc->Next);
1311 if (!next)
1312 break;
1313 remaining -= next;
1314 cc = (struct create_context *)((char *)cc + next);
1315 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001316
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001317 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001318}
1319
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001320static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001321add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1322 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001323{
1324 struct smb2_create_req *req = iov[0].iov_base;
1325 unsigned int num = *num_iovec;
1326
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001327 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001328 if (iov[num].iov_base == NULL)
1329 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001330 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001331 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1332 if (!req->CreateContextsOffset)
1333 req->CreateContextsOffset = cpu_to_le32(
1334 sizeof(struct smb2_create_req) - 4 +
1335 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001336 le32_add_cpu(&req->CreateContextsLength,
1337 server->vals->create_lease_size);
1338 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001339 *num_iovec = num + 1;
1340 return 0;
1341}
1342
Steve Frenchb56eae42015-11-03 09:26:27 -06001343static struct create_durable_v2 *
1344create_durable_v2_buf(struct cifs_fid *pfid)
1345{
1346 struct create_durable_v2 *buf;
1347
1348 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1349 if (!buf)
1350 return NULL;
1351
1352 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1353 (struct create_durable_v2, dcontext));
1354 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1355 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1356 (struct create_durable_v2, Name));
1357 buf->ccontext.NameLength = cpu_to_le16(4);
1358
1359 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1360 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001361 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001362 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1363
1364 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1365 buf->Name[0] = 'D';
1366 buf->Name[1] = 'H';
1367 buf->Name[2] = '2';
1368 buf->Name[3] = 'Q';
1369 return buf;
1370}
1371
1372static struct create_durable_handle_reconnect_v2 *
1373create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1374{
1375 struct create_durable_handle_reconnect_v2 *buf;
1376
1377 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1378 GFP_KERNEL);
1379 if (!buf)
1380 return NULL;
1381
1382 buf->ccontext.DataOffset =
1383 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1384 dcontext));
1385 buf->ccontext.DataLength =
1386 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1387 buf->ccontext.NameOffset =
1388 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1389 Name));
1390 buf->ccontext.NameLength = cpu_to_le16(4);
1391
1392 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1393 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1394 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1395 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1396
1397 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1398 buf->Name[0] = 'D';
1399 buf->Name[1] = 'H';
1400 buf->Name[2] = '2';
1401 buf->Name[3] = 'C';
1402 return buf;
1403}
1404
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001405static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001406add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001407 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001408{
1409 struct smb2_create_req *req = iov[0].iov_base;
1410 unsigned int num = *num_iovec;
1411
Steve Frenchb56eae42015-11-03 09:26:27 -06001412 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1413 if (iov[num].iov_base == NULL)
1414 return -ENOMEM;
1415 iov[num].iov_len = sizeof(struct create_durable_v2);
1416 if (!req->CreateContextsOffset)
1417 req->CreateContextsOffset =
1418 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1419 iov[1].iov_len);
1420 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1421 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1422 *num_iovec = num + 1;
1423 return 0;
1424}
1425
1426static int
1427add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1428 struct cifs_open_parms *oparms)
1429{
1430 struct smb2_create_req *req = iov[0].iov_base;
1431 unsigned int num = *num_iovec;
1432
1433 /* indicate that we don't need to relock the file */
1434 oparms->reconnect = false;
1435
1436 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1437 if (iov[num].iov_base == NULL)
1438 return -ENOMEM;
1439 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1440 if (!req->CreateContextsOffset)
1441 req->CreateContextsOffset =
1442 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1443 iov[1].iov_len);
1444 le32_add_cpu(&req->CreateContextsLength,
1445 sizeof(struct create_durable_handle_reconnect_v2));
1446 inc_rfc1001_len(&req->hdr,
1447 sizeof(struct create_durable_handle_reconnect_v2));
1448 *num_iovec = num + 1;
1449 return 0;
1450}
1451
1452static int
1453add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1454 struct cifs_open_parms *oparms, bool use_persistent)
1455{
1456 struct smb2_create_req *req = iov[0].iov_base;
1457 unsigned int num = *num_iovec;
1458
1459 if (use_persistent) {
1460 if (oparms->reconnect)
1461 return add_durable_reconnect_v2_context(iov, num_iovec,
1462 oparms);
1463 else
1464 return add_durable_v2_context(iov, num_iovec, oparms);
1465 }
1466
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001467 if (oparms->reconnect) {
1468 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1469 /* indicate that we don't need to relock the file */
1470 oparms->reconnect = false;
1471 } else
1472 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001473 if (iov[num].iov_base == NULL)
1474 return -ENOMEM;
1475 iov[num].iov_len = sizeof(struct create_durable);
1476 if (!req->CreateContextsOffset)
1477 req->CreateContextsOffset =
1478 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1479 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001480 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001481 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1482 *num_iovec = num + 1;
1483 return 0;
1484}
1485
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001486int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001487SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001488 __u8 *oplock, struct smb2_file_all_info *buf,
1489 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001490{
1491 struct smb2_create_req *req;
1492 struct smb2_create_rsp *rsp;
1493 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001494 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001495 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001496 struct kvec iov[4];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001497 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001498 int resp_buftype;
1499 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001500 __le16 *copy_path = NULL;
1501 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001502 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001503 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001504 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001505 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001506
Joe Perchesf96637b2013-05-04 22:12:25 -05001507 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001508
1509 if (ses && (ses->server))
1510 server = ses->server;
1511 else
1512 return -EIO;
1513
1514 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1515 if (rc)
1516 return rc;
1517
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001518 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001519 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001520 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1521 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001522
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001523 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001524 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001525 /* File attributes ignored on open (used in create though) */
1526 req->FileAttributes = cpu_to_le32(file_attributes);
1527 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001528 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1529 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001530 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001531 /* do not count rfc1001 len field */
1532 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001533
1534 iov[0].iov_base = (char *)req;
1535 /* 4 for rfc1002 length field */
1536 iov[0].iov_len = get_rfc1002_length(req) + 4;
1537
1538 /* MUST set path len (NameLength) to 0 opening root of share */
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001539 req->NameLength = cpu_to_le16(uni_path_len - 2);
1540 /* -1 since last byte is buf[0] which is sent below (path) */
1541 iov[0].iov_len--;
1542 if (uni_path_len % 8 != 0) {
1543 copy_size = uni_path_len / 8 * 8;
1544 if (copy_size < uni_path_len)
1545 copy_size += 8;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001546
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001547 copy_path = kzalloc(copy_size, GFP_KERNEL);
1548 if (!copy_path)
1549 return -ENOMEM;
1550 memcpy((char *)copy_path, (const char *)path,
1551 uni_path_len);
1552 uni_path_len = copy_size;
1553 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001554 }
1555
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001556 iov[1].iov_len = uni_path_len;
1557 iov[1].iov_base = path;
1558 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1559 inc_rfc1001_len(req, uni_path_len - 1);
1560
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001561 if (!server->oplocks)
1562 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1563
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001564 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001565 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1566 req->RequestedOplockLevel = *oplock;
1567 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001568 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001569 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001570 cifs_small_buf_release(req);
1571 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001572 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001573 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001574 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001575 }
1576
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001577 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1578 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001579 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001580 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001581 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001582 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001583 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001584 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001585
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001586 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06001587 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001588 if (rc) {
1589 cifs_small_buf_release(req);
1590 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001591 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001592 return rc;
1593 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001594 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001595 }
1596
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001597 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, 0, &rsp_iov);
1598 cifs_small_buf_release(req);
1599 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001600
1601 if (rc != 0) {
1602 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001603 if (err_buf)
1604 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1605 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001606 goto creat_exit;
1607 }
1608
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001609 oparms->fid->persistent_fid = rsp->PersistentFileId;
1610 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001611
1612 if (buf) {
1613 memcpy(buf, &rsp->CreationTime, 32);
1614 buf->AllocationSize = rsp->AllocationSize;
1615 buf->EndOfFile = rsp->EndofFile;
1616 buf->Attributes = rsp->FileAttributes;
1617 buf->NumberOfLinks = cpu_to_le32(1);
1618 buf->DeletePending = 0;
1619 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001620
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001621 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001622 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001623 else
1624 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001625creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001626 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001627 kfree(lc_buf);
1628 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001629 free_rsp_buf(resp_buftype, rsp);
1630 return rc;
1631}
1632
Steve French4a72daf2013-06-25 00:20:49 -05001633/*
1634 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1635 */
1636int
1637SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1638 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1639 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1640{
1641 struct smb2_ioctl_req *req;
1642 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001643 struct smb2_sync_hdr *shdr;
Steve French4a72daf2013-06-25 00:20:49 -05001644 struct TCP_Server_Info *server;
Steve French8e353102015-03-26 19:47:02 -05001645 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001646 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001647 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001648 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001649 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001650 int rc = 0;
1651
1652 cifs_dbg(FYI, "SMB2 IOCTL\n");
1653
Steve French3d1a3742014-08-11 21:05:25 -05001654 if (out_data != NULL)
1655 *out_data = NULL;
1656
Steve French4a72daf2013-06-25 00:20:49 -05001657 /* zero out returned data len, in case of error */
1658 if (plen)
1659 *plen = 0;
1660
Steve French8e353102015-03-26 19:47:02 -05001661 if (tcon)
1662 ses = tcon->ses;
1663 else
1664 return -EIO;
1665
Steve French4a72daf2013-06-25 00:20:49 -05001666 if (ses && (ses->server))
1667 server = ses->server;
1668 else
1669 return -EIO;
1670
1671 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1672 if (rc)
1673 return rc;
1674
1675 req->CtlCode = cpu_to_le32(opcode);
1676 req->PersistentFileId = persistent_fid;
1677 req->VolatileFileId = volatile_fid;
1678
1679 if (indatalen) {
1680 req->InputCount = cpu_to_le32(indatalen);
1681 /* do not set InputOffset if no input data */
1682 req->InputOffset =
1683 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1684 iov[1].iov_base = in_data;
1685 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001686 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05001687 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001688 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05001689
1690 req->OutputOffset = 0;
1691 req->OutputCount = 0; /* MBZ */
1692
1693 /*
1694 * Could increase MaxOutputResponse, but that would require more
1695 * than one credit. Windows typically sets this smaller, but for some
1696 * ioctls it may be useful to allow server to send more. No point
1697 * limiting what the server can send as long as fits in one credit
1698 */
1699 req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1700
1701 if (is_fsctl)
1702 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1703 else
1704 req->Flags = 0;
1705
1706 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001707
Steve French7ff8d452013-10-14 00:44:19 -05001708 /*
1709 * If no input data, the size of ioctl struct in
1710 * protocol spec still includes a 1 byte data buffer,
1711 * but if input data passed to ioctl, we do not
1712 * want to double count this, so we do not send
1713 * the dummy one byte of data in iovec[0] if sending
1714 * input data (in iovec[1]). We also must add 4 bytes
1715 * in first iovec to allow for rfc1002 length field.
1716 */
1717
1718 if (indatalen) {
1719 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1720 inc_rfc1001_len(req, indatalen - 1);
1721 } else
1722 iov[0].iov_len = get_rfc1002_length(req) + 4;
1723
Steve French4a72daf2013-06-25 00:20:49 -05001724
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001725 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, 0, &rsp_iov);
1726 cifs_small_buf_release(req);
1727 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05001728
Steve French9bf0c9c2013-11-16 18:05:28 -06001729 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001730 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001731 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001732 } else if (rc == -EINVAL) {
1733 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1734 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001735 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001736 goto ioctl_exit;
1737 }
Steve French4a72daf2013-06-25 00:20:49 -05001738 }
1739
1740 /* check if caller wants to look at return data or just return rc */
1741 if ((plen == NULL) || (out_data == NULL))
1742 goto ioctl_exit;
1743
1744 *plen = le32_to_cpu(rsp->OutputCount);
1745
1746 /* We check for obvious errors in the output buffer length and offset */
1747 if (*plen == 0)
1748 goto ioctl_exit; /* server returned no data */
1749 else if (*plen > 0xFF00) {
1750 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1751 *plen = 0;
1752 rc = -EIO;
1753 goto ioctl_exit;
1754 }
1755
1756 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1757 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1758 le32_to_cpu(rsp->OutputOffset));
1759 *plen = 0;
1760 rc = -EIO;
1761 goto ioctl_exit;
1762 }
1763
1764 *out_data = kmalloc(*plen, GFP_KERNEL);
1765 if (*out_data == NULL) {
1766 rc = -ENOMEM;
1767 goto ioctl_exit;
1768 }
1769
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001770 shdr = get_sync_hdr(rsp);
1771 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05001772ioctl_exit:
1773 free_rsp_buf(resp_buftype, rsp);
1774 return rc;
1775}
1776
Steve French64a5cfa2013-10-14 15:31:32 -05001777/*
1778 * Individual callers to ioctl worker function follow
1779 */
1780
1781int
1782SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1783 u64 persistent_fid, u64 volatile_fid)
1784{
1785 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05001786 struct compress_ioctl fsctl_input;
1787 char *ret_data = NULL;
1788
1789 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08001790 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05001791
1792 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1793 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1794 (char *)&fsctl_input /* data input */,
1795 2 /* in data len */, &ret_data /* out data */, NULL);
1796
1797 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05001798
1799 return rc;
1800}
1801
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001802int
1803SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1804 u64 persistent_fid, u64 volatile_fid)
1805{
1806 struct smb2_close_req *req;
1807 struct smb2_close_rsp *rsp;
1808 struct TCP_Server_Info *server;
1809 struct cifs_ses *ses = tcon->ses;
1810 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001811 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001812 int resp_buftype;
1813 int rc = 0;
1814
Joe Perchesf96637b2013-05-04 22:12:25 -05001815 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001816
1817 if (ses && (ses->server))
1818 server = ses->server;
1819 else
1820 return -EIO;
1821
1822 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1823 if (rc)
1824 return rc;
1825
1826 req->PersistentFileId = persistent_fid;
1827 req->VolatileFileId = volatile_fid;
1828
1829 iov[0].iov_base = (char *)req;
1830 /* 4 for rfc1002 length field */
1831 iov[0].iov_len = get_rfc1002_length(req) + 4;
1832
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001833 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0, &rsp_iov);
1834 cifs_small_buf_release(req);
1835 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001836
1837 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09001838 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001839 goto close_exit;
1840 }
1841
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001842 /* BB FIXME - decode close response, update inode for caching */
1843
1844close_exit:
1845 free_rsp_buf(resp_buftype, rsp);
1846 return rc;
1847}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001848
1849static int
1850validate_buf(unsigned int offset, unsigned int buffer_length,
1851 struct smb2_hdr *hdr, unsigned int min_buf_size)
1852
1853{
1854 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1855 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1856 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1857 char *end_of_buf = begin_of_buf + buffer_length;
1858
1859
1860 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001861 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1862 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001863 return -EINVAL;
1864 }
1865
1866 /* check if beyond RFC1001 maximum length */
1867 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001868 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1869 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001870 return -EINVAL;
1871 }
1872
1873 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001874 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001875 return -EINVAL;
1876 }
1877
1878 return 0;
1879}
1880
1881/*
1882 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1883 * Caller must free buffer.
1884 */
1885static int
1886validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1887 struct smb2_hdr *hdr, unsigned int minbufsize,
1888 char *data)
1889
1890{
1891 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1892 int rc;
1893
1894 if (!data)
1895 return -EINVAL;
1896
1897 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1898 if (rc)
1899 return rc;
1900
1901 memcpy(data, begin_of_buf, buffer_length);
1902
1903 return 0;
1904}
1905
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001906static int
1907query_info(const unsigned int xid, struct cifs_tcon *tcon,
1908 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1909 size_t output_len, size_t min_len, void *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001910{
1911 struct smb2_query_info_req *req;
1912 struct smb2_query_info_rsp *rsp = NULL;
1913 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001914 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001915 int rc = 0;
1916 int resp_buftype;
1917 struct TCP_Server_Info *server;
1918 struct cifs_ses *ses = tcon->ses;
1919
Joe Perchesf96637b2013-05-04 22:12:25 -05001920 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001921
1922 if (ses && (ses->server))
1923 server = ses->server;
1924 else
1925 return -EIO;
1926
1927 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1928 if (rc)
1929 return rc;
1930
1931 req->InfoType = SMB2_O_INFO_FILE;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001932 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001933 req->PersistentFileId = persistent_fid;
1934 req->VolatileFileId = volatile_fid;
1935 /* 4 for rfc1002 length field and 1 for Buffer */
1936 req->InputBufferOffset =
1937 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001938 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001939
1940 iov[0].iov_base = (char *)req;
1941 /* 4 for rfc1002 length field */
1942 iov[0].iov_len = get_rfc1002_length(req) + 4;
1943
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001944 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0, &rsp_iov);
1945 cifs_small_buf_release(req);
1946 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04001947
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001948 if (rc) {
1949 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1950 goto qinf_exit;
1951 }
1952
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001953 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1954 le32_to_cpu(rsp->OutputBufferLength),
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001955 &rsp->hdr, min_len, data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001956
1957qinf_exit:
1958 free_rsp_buf(resp_buftype, rsp);
1959 return rc;
1960}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001961
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001962int
1963SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1964 u64 persistent_fid, u64 volatile_fid,
1965 struct smb2_file_all_info *data)
1966{
1967 return query_info(xid, tcon, persistent_fid, volatile_fid,
1968 FILE_ALL_INFORMATION,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04001969 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001970 sizeof(struct smb2_file_all_info), data);
1971}
1972
1973int
1974SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1975 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1976{
1977 return query_info(xid, tcon, persistent_fid, volatile_fid,
1978 FILE_INTERNAL_INFORMATION,
1979 sizeof(struct smb2_file_internal_info),
1980 sizeof(struct smb2_file_internal_info), uniqueid);
1981}
1982
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001983/*
1984 * This is a no-op for now. We're not really interested in the reply, but
1985 * rather in the fact that the server sent one and that server->lstrp
1986 * gets updated.
1987 *
1988 * FIXME: maybe we should consider checking that the reply matches request?
1989 */
1990static void
1991smb2_echo_callback(struct mid_q_entry *mid)
1992{
1993 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001994 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001995 unsigned int credits_received = 1;
1996
1997 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001998 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001999
Christopher Oo5fb4e282015-06-25 16:10:48 -07002000 mutex_lock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002001 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002002 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002003 add_credits(server, credits_received, CIFS_ECHO_OP);
2004}
2005
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002006void smb2_reconnect_server(struct work_struct *work)
2007{
2008 struct TCP_Server_Info *server = container_of(work,
2009 struct TCP_Server_Info, reconnect.work);
2010 struct cifs_ses *ses;
2011 struct cifs_tcon *tcon, *tcon2;
2012 struct list_head tmp_list;
2013 int tcon_exist = false;
2014
2015 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2016 mutex_lock(&server->reconnect_mutex);
2017
2018 INIT_LIST_HEAD(&tmp_list);
2019 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2020
2021 spin_lock(&cifs_tcp_ses_lock);
2022 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2023 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002024 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002025 tcon->tc_count++;
2026 list_add_tail(&tcon->rlist, &tmp_list);
2027 tcon_exist = true;
2028 }
2029 }
2030 }
2031 /*
2032 * Get the reference to server struct to be sure that the last call of
2033 * cifs_put_tcon() in the loop below won't release the server pointer.
2034 */
2035 if (tcon_exist)
2036 server->srv_count++;
2037
2038 spin_unlock(&cifs_tcp_ses_lock);
2039
2040 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002041 if (!smb2_reconnect(SMB2_INTERNAL_CMD, tcon))
2042 cifs_reopen_persistent_handles(tcon);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002043 list_del_init(&tcon->rlist);
2044 cifs_put_tcon(tcon);
2045 }
2046
2047 cifs_dbg(FYI, "Reconnecting tcons finished\n");
2048 mutex_unlock(&server->reconnect_mutex);
2049
2050 /* now we can safely release srv struct */
2051 if (tcon_exist)
2052 cifs_put_tcp_session(server, 1);
2053}
2054
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002055int
2056SMB2_echo(struct TCP_Server_Info *server)
2057{
2058 struct smb2_echo_req *req;
2059 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002060 struct kvec iov[2];
2061 struct smb_rqst rqst = { .rq_iov = iov,
2062 .rq_nvec = 2 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002063
Joe Perchesf96637b2013-05-04 22:12:25 -05002064 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002065
Steve French4fcd1812016-06-22 20:12:05 -05002066 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002067 /* No need to send echo on newly established connections */
2068 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2069 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002070 }
2071
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002072 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2073 if (rc)
2074 return rc;
2075
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002076 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002077
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002078 /* 4 for rfc1002 length field */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002079 iov[0].iov_len = 4;
2080 iov[0].iov_base = (char *)req;
2081 iov[1].iov_len = get_rfc1002_length(req);
2082 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002083
Jeff Laytonfec344e2012-09-18 16:20:35 -07002084 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002085 CIFS_ECHO_OP);
2086 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002087 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002088
2089 cifs_small_buf_release(req);
2090 return rc;
2091}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002092
2093int
2094SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2095 u64 volatile_fid)
2096{
2097 struct smb2_flush_req *req;
2098 struct TCP_Server_Info *server;
2099 struct cifs_ses *ses = tcon->ses;
2100 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002101 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002102 int resp_buftype;
2103 int rc = 0;
2104
Joe Perchesf96637b2013-05-04 22:12:25 -05002105 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002106
2107 if (ses && (ses->server))
2108 server = ses->server;
2109 else
2110 return -EIO;
2111
2112 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2113 if (rc)
2114 return rc;
2115
2116 req->PersistentFileId = persistent_fid;
2117 req->VolatileFileId = volatile_fid;
2118
2119 iov[0].iov_base = (char *)req;
2120 /* 4 for rfc1002 length field */
2121 iov[0].iov_len = get_rfc1002_length(req) + 4;
2122
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002123 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0, &rsp_iov);
2124 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002125
Steve Frenchdfebe402015-03-27 01:00:06 -05002126 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002127 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2128
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002129 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002130 return rc;
2131}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002132
2133/*
2134 * To form a chain of read requests, any read requests after the first should
2135 * have the end_of_chain boolean set to true.
2136 */
2137static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002138smb2_new_read_req(void **buf, unsigned int *total_len,
2139 struct cifs_io_parms *io_parms, unsigned int remaining_bytes,
2140 int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002141{
2142 int rc = -EACCES;
2143 struct smb2_read_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002144 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002145
2146 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
2147 if (rc)
2148 return rc;
2149 if (io_parms->tcon->ses->server == NULL)
2150 return -ECONNABORTED;
2151
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002152 shdr = get_sync_hdr(req);
2153 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002154
2155 req->PersistentFileId = io_parms->persistent_fid;
2156 req->VolatileFileId = io_parms->volatile_fid;
2157 req->ReadChannelInfoOffset = 0; /* reserved */
2158 req->ReadChannelInfoLength = 0; /* reserved */
2159 req->Channel = 0; /* reserved */
2160 req->MinimumCount = 0;
2161 req->Length = cpu_to_le32(io_parms->length);
2162 req->Offset = cpu_to_le64(io_parms->offset);
2163
2164 if (request_type & CHAINED_REQUEST) {
2165 if (!(request_type & END_OF_CHAIN)) {
2166 /* 4 for rfc1002 length field */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002167 shdr->NextCommand =
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002168 cpu_to_le32(get_rfc1002_length(req) + 4);
2169 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002170 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002171 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002172 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002173 /*
2174 * Related requests use info from previous read request
2175 * in chain.
2176 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002177 shdr->SessionId = 0xFFFFFFFF;
2178 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002179 req->PersistentFileId = 0xFFFFFFFF;
2180 req->VolatileFileId = 0xFFFFFFFF;
2181 }
2182 }
2183 if (remaining_bytes > io_parms->length)
2184 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2185 else
2186 req->RemainingBytes = 0;
2187
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002188 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002189 /* 4 for rfc1002 length field */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002190 *total_len = get_rfc1002_length(req) + 4;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002191 return rc;
2192}
2193
2194static void
2195smb2_readv_callback(struct mid_q_entry *mid)
2196{
2197 struct cifs_readdata *rdata = mid->callback_data;
2198 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2199 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002200 struct smb2_sync_hdr *shdr =
2201 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002202 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002203 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2204 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002205 .rq_pages = rdata->pages,
2206 .rq_npages = rdata->nr_pages,
2207 .rq_pagesz = rdata->pagesz,
2208 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002209
Joe Perchesf96637b2013-05-04 22:12:25 -05002210 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2211 __func__, mid->mid, mid->mid_state, rdata->result,
2212 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002213
2214 switch (mid->mid_state) {
2215 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002216 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002217 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04002218 if (server->sign) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002219 int rc;
2220
Jeff Layton0b688cf2012-09-18 16:20:34 -07002221 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002222 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002223 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2224 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002225 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002226 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002227 task_io_account_read(rdata->got_bytes);
2228 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002229 break;
2230 case MID_REQUEST_SUBMITTED:
2231 case MID_RETRY_NEEDED:
2232 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002233 if (server->sign && rdata->got_bytes)
2234 /* reset bytes number since we can not check a sign */
2235 rdata->got_bytes = 0;
2236 /* FIXME: should this be counted toward the initiating task? */
2237 task_io_account_read(rdata->got_bytes);
2238 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002239 break;
2240 default:
2241 if (rdata->result != -ENODATA)
2242 rdata->result = -EIO;
2243 }
2244
2245 if (rdata->result)
2246 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2247
2248 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002249 mutex_lock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002250 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002251 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002252 add_credits(server, credits_received, 0);
2253}
2254
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002255/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002256int
2257smb2_async_readv(struct cifs_readdata *rdata)
2258{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002259 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002260 char *buf;
2261 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002262 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002263 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2264 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002265 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002266 unsigned int total_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002267
Joe Perchesf96637b2013-05-04 22:12:25 -05002268 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2269 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002270
2271 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2272 io_parms.offset = rdata->offset;
2273 io_parms.length = rdata->bytes;
2274 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2275 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2276 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002277
2278 server = io_parms.tcon->ses->server;
2279
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002280 rc = smb2_new_read_req((void **) &buf, &total_len, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002281 if (rc) {
2282 if (rc == -EAGAIN && rdata->credits) {
2283 /* credits was reset by reconnect */
2284 rdata->credits = 0;
2285 /* reduce in_flight value since we won't send the req */
2286 spin_lock(&server->req_lock);
2287 server->in_flight--;
2288 spin_unlock(&server->req_lock);
2289 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002290 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002291 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002292
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002293 shdr = get_sync_hdr(buf);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002294 /* 4 for rfc1002 length field */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002295 rdata->iov[0].iov_len = 4;
2296 rdata->iov[0].iov_base = buf;
2297 rdata->iov[1].iov_len = total_len - 4;
2298 rdata->iov[1].iov_base = buf + 4;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002299
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002300 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002301 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002302 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002303 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002304 spin_lock(&server->req_lock);
2305 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002306 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002307 spin_unlock(&server->req_lock);
2308 wake_up(&server->request_q);
2309 flags = CIFS_HAS_CREDITS;
2310 }
2311
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002312 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002313 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002314 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002315 rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002316 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002317 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002318 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2319 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002320
2321 cifs_small_buf_release(buf);
2322 return rc;
2323}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002324
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002325int
2326SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2327 unsigned int *nbytes, char **buf, int *buf_type)
2328{
2329 int resp_buftype, rc = -EACCES;
2330 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002331 struct smb2_sync_hdr *shdr;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002332 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002333 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002334 unsigned int total_len;
2335 char *req;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002336
2337 *nbytes = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002338 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002339 if (rc)
2340 return rc;
2341
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002342 iov[0].iov_base = buf;
2343 iov[0].iov_len = total_len;
2344
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002345 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002346 &resp_buftype, CIFS_LOG_ERROR, &rsp_iov);
2347 cifs_small_buf_release(iov[0].iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002348
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002349 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002350 shdr = get_sync_hdr(rsp);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002351
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002352 if (shdr->Status == STATUS_END_OF_FILE) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002353 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002354 return 0;
2355 }
2356
2357 if (rc) {
2358 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002359 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002360 } else {
2361 *nbytes = le32_to_cpu(rsp->DataLength);
2362 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2363 (*nbytes > io_parms->length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002364 cifs_dbg(FYI, "bad length %d for count %d\n",
2365 *nbytes, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002366 rc = -EIO;
2367 *nbytes = 0;
2368 }
2369 }
2370
2371 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002372 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002373 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002374 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002375 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002376 if (resp_buftype == CIFS_SMALL_BUFFER)
2377 *buf_type = CIFS_SMALL_BUFFER;
2378 else if (resp_buftype == CIFS_LARGE_BUFFER)
2379 *buf_type = CIFS_LARGE_BUFFER;
2380 }
2381 return rc;
2382}
2383
Pavel Shilovsky33319142012-09-18 16:20:29 -07002384/*
2385 * Check the mid_state and signature on received buffer (if any), and queue the
2386 * workqueue completion task.
2387 */
2388static void
2389smb2_writev_callback(struct mid_q_entry *mid)
2390{
2391 struct cifs_writedata *wdata = mid->callback_data;
2392 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002393 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002394 unsigned int written;
2395 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2396 unsigned int credits_received = 1;
2397
2398 switch (mid->mid_state) {
2399 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002400 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002401 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2402 if (wdata->result != 0)
2403 break;
2404
2405 written = le32_to_cpu(rsp->DataLength);
2406 /*
2407 * Mask off high 16 bits when bytes written as returned
2408 * by the server is greater than bytes requested by the
2409 * client. OS/2 servers are known to set incorrect
2410 * CountHigh values.
2411 */
2412 if (written > wdata->bytes)
2413 written &= 0xFFFF;
2414
2415 if (written < wdata->bytes)
2416 wdata->result = -ENOSPC;
2417 else
2418 wdata->bytes = written;
2419 break;
2420 case MID_REQUEST_SUBMITTED:
2421 case MID_RETRY_NEEDED:
2422 wdata->result = -EAGAIN;
2423 break;
2424 default:
2425 wdata->result = -EIO;
2426 break;
2427 }
2428
2429 if (wdata->result)
2430 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2431
2432 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002433 mutex_lock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002434 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002435 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002436 add_credits(tcon->ses->server, credits_received, 0);
2437}
2438
2439/* smb2_async_writev - send an async write, and set up mid to handle result */
2440int
Steve French4a5c80d2014-02-07 20:45:12 -06002441smb2_async_writev(struct cifs_writedata *wdata,
2442 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002443{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002444 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002445 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002446 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002447 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002448 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002449 struct kvec iov[2];
2450 struct smb_rqst rqst = { };
Pavel Shilovsky33319142012-09-18 16:20:29 -07002451
2452 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002453 if (rc) {
2454 if (rc == -EAGAIN && wdata->credits) {
2455 /* credits was reset by reconnect */
2456 wdata->credits = 0;
2457 /* reduce in_flight value since we won't send the req */
2458 spin_lock(&server->req_lock);
2459 server->in_flight--;
2460 spin_unlock(&server->req_lock);
2461 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002462 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002463 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002464
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002465 shdr = get_sync_hdr(req);
2466 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002467
2468 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2469 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2470 req->WriteChannelInfoOffset = 0;
2471 req->WriteChannelInfoLength = 0;
2472 req->Channel = 0;
2473 req->Offset = cpu_to_le64(wdata->offset);
2474 /* 4 for rfc1002 length field */
2475 req->DataOffset = cpu_to_le16(
2476 offsetof(struct smb2_write_req, Buffer) - 4);
2477 req->RemainingBytes = 0;
2478
2479 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002480 iov[0].iov_len = 4;
2481 iov[0].iov_base = req;
2482 iov[1].iov_len = get_rfc1002_length(req) - 1;
2483 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002484
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002485 rqst.rq_iov = iov;
2486 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002487 rqst.rq_pages = wdata->pages;
2488 rqst.rq_npages = wdata->nr_pages;
2489 rqst.rq_pagesz = wdata->pagesz;
2490 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002491
Joe Perchesf96637b2013-05-04 22:12:25 -05002492 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2493 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002494
2495 req->Length = cpu_to_le32(wdata->bytes);
2496
2497 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2498
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002499 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002500 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002501 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002502 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002503 spin_lock(&server->req_lock);
2504 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002505 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002506 spin_unlock(&server->req_lock);
2507 wake_up(&server->request_q);
2508 flags = CIFS_HAS_CREDITS;
2509 }
2510
Pavel Shilovsky33319142012-09-18 16:20:29 -07002511 kref_get(&wdata->refcount);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002512 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2513 flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002514
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002515 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002516 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002517 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2518 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002519
Pavel Shilovsky33319142012-09-18 16:20:29 -07002520async_writev_out:
2521 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002522 return rc;
2523}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002524
2525/*
2526 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2527 * The length field from io_parms must be at least 1 and indicates a number of
2528 * elements with data to write that begins with position 1 in iov array. All
2529 * data length is specified by count.
2530 */
2531int
2532SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2533 unsigned int *nbytes, struct kvec *iov, int n_vec)
2534{
2535 int rc = 0;
2536 struct smb2_write_req *req = NULL;
2537 struct smb2_write_rsp *rsp = NULL;
2538 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002539 struct kvec rsp_iov;
2540
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002541 *nbytes = 0;
2542
2543 if (n_vec < 1)
2544 return rc;
2545
2546 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2547 if (rc)
2548 return rc;
2549
2550 if (io_parms->tcon->ses->server == NULL)
2551 return -ECONNABORTED;
2552
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002553 req->hdr.sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002554
2555 req->PersistentFileId = io_parms->persistent_fid;
2556 req->VolatileFileId = io_parms->volatile_fid;
2557 req->WriteChannelInfoOffset = 0;
2558 req->WriteChannelInfoLength = 0;
2559 req->Channel = 0;
2560 req->Length = cpu_to_le32(io_parms->length);
2561 req->Offset = cpu_to_le64(io_parms->offset);
2562 /* 4 for rfc1002 length field */
2563 req->DataOffset = cpu_to_le16(
2564 offsetof(struct smb2_write_req, Buffer) - 4);
2565 req->RemainingBytes = 0;
2566
2567 iov[0].iov_base = (char *)req;
2568 /* 4 for rfc1002 length field and 1 for Buffer */
2569 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2570
2571 /* length of entire message including data to be written */
2572 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2573
2574 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002575 &resp_buftype, 0, &rsp_iov);
2576 cifs_small_buf_release(req);
2577 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002578
2579 if (rc) {
2580 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002581 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002582 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002583 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002584
2585 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002586 return rc;
2587}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002588
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002589static unsigned int
2590num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2591{
2592 int len;
2593 unsigned int entrycount = 0;
2594 unsigned int next_offset = 0;
2595 FILE_DIRECTORY_INFO *entryptr;
2596
2597 if (bufstart == NULL)
2598 return 0;
2599
2600 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2601
2602 while (1) {
2603 entryptr = (FILE_DIRECTORY_INFO *)
2604 ((char *)entryptr + next_offset);
2605
2606 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002607 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002608 break;
2609 }
2610
2611 len = le32_to_cpu(entryptr->FileNameLength);
2612 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002613 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2614 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002615 break;
2616 }
2617
2618 *lastentry = (char *)entryptr;
2619 entrycount++;
2620
2621 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2622 if (!next_offset)
2623 break;
2624 }
2625
2626 return entrycount;
2627}
2628
2629/*
2630 * Readdir/FindFirst
2631 */
2632int
2633SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2634 u64 persistent_fid, u64 volatile_fid, int index,
2635 struct cifs_search_info *srch_inf)
2636{
2637 struct smb2_query_directory_req *req;
2638 struct smb2_query_directory_rsp *rsp = NULL;
2639 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002640 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002641 int rc = 0;
2642 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002643 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002644 unsigned char *bufptr;
2645 struct TCP_Server_Info *server;
2646 struct cifs_ses *ses = tcon->ses;
2647 __le16 asteriks = cpu_to_le16('*');
2648 char *end_of_smb;
2649 unsigned int output_size = CIFSMaxBufSize;
2650 size_t info_buf_size;
2651
2652 if (ses && (ses->server))
2653 server = ses->server;
2654 else
2655 return -EIO;
2656
2657 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2658 if (rc)
2659 return rc;
2660
2661 switch (srch_inf->info_level) {
2662 case SMB_FIND_FILE_DIRECTORY_INFO:
2663 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2664 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2665 break;
2666 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2667 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2668 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2669 break;
2670 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002671 cifs_dbg(VFS, "info level %u isn't supported\n",
2672 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002673 rc = -EINVAL;
2674 goto qdir_exit;
2675 }
2676
2677 req->FileIndex = cpu_to_le32(index);
2678 req->PersistentFileId = persistent_fid;
2679 req->VolatileFileId = volatile_fid;
2680
2681 len = 0x2;
2682 bufptr = req->Buffer;
2683 memcpy(bufptr, &asteriks, len);
2684
2685 req->FileNameOffset =
2686 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2687 req->FileNameLength = cpu_to_le16(len);
2688 /*
2689 * BB could be 30 bytes or so longer if we used SMB2 specific
2690 * buffer lengths, but this is safe and close enough.
2691 */
2692 output_size = min_t(unsigned int, output_size, server->maxBuf);
2693 output_size = min_t(unsigned int, output_size, 2 << 15);
2694 req->OutputBufferLength = cpu_to_le32(output_size);
2695
2696 iov[0].iov_base = (char *)req;
2697 /* 4 for RFC1001 length and 1 for Buffer */
2698 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2699
2700 iov[1].iov_base = (char *)(req->Buffer);
2701 iov[1].iov_len = len;
2702
2703 inc_rfc1001_len(req, len - 1 /* Buffer */);
2704
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002705 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0, &rsp_iov);
2706 cifs_small_buf_release(req);
2707 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002708
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002709 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002710 if (rc == -ENODATA &&
2711 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04002712 srch_inf->endOfSearch = true;
2713 rc = 0;
2714 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002715 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2716 goto qdir_exit;
2717 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002718
2719 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2720 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2721 info_buf_size);
2722 if (rc)
2723 goto qdir_exit;
2724
2725 srch_inf->unicode = true;
2726
2727 if (srch_inf->ntwrk_buf_start) {
2728 if (srch_inf->smallBuf)
2729 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2730 else
2731 cifs_buf_release(srch_inf->ntwrk_buf_start);
2732 }
2733 srch_inf->ntwrk_buf_start = (char *)rsp;
2734 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2735 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2736 /* 4 for rfc1002 length field */
2737 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2738 srch_inf->entries_in_buffer =
2739 num_entries(srch_inf->srch_entries_start, end_of_smb,
2740 &srch_inf->last_entry, info_buf_size);
2741 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05002742 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2743 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2744 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002745 if (resp_buftype == CIFS_LARGE_BUFFER)
2746 srch_inf->smallBuf = false;
2747 else if (resp_buftype == CIFS_SMALL_BUFFER)
2748 srch_inf->smallBuf = true;
2749 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002750 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002751
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002752 return rc;
2753
2754qdir_exit:
2755 free_rsp_buf(resp_buftype, rsp);
2756 return rc;
2757}
2758
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002759static int
2760send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002761 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002762 unsigned int num, void **data, unsigned int *size)
2763{
2764 struct smb2_set_info_req *req;
2765 struct smb2_set_info_rsp *rsp = NULL;
2766 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002767 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002768 int rc = 0;
2769 int resp_buftype;
2770 unsigned int i;
2771 struct TCP_Server_Info *server;
2772 struct cifs_ses *ses = tcon->ses;
2773
2774 if (ses && (ses->server))
2775 server = ses->server;
2776 else
2777 return -EIO;
2778
2779 if (!num)
2780 return -EINVAL;
2781
2782 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2783 if (!iov)
2784 return -ENOMEM;
2785
2786 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2787 if (rc) {
2788 kfree(iov);
2789 return rc;
2790 }
2791
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002792 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002793
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002794 req->InfoType = SMB2_O_INFO_FILE;
2795 req->FileInfoClass = info_class;
2796 req->PersistentFileId = persistent_fid;
2797 req->VolatileFileId = volatile_fid;
2798
2799 /* 4 for RFC1001 length and 1 for Buffer */
2800 req->BufferOffset =
2801 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2802 req->BufferLength = cpu_to_le32(*size);
2803
2804 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2805
2806 memcpy(req->Buffer, *data, *size);
2807
2808 iov[0].iov_base = (char *)req;
2809 /* 4 for RFC1001 length */
2810 iov[0].iov_len = get_rfc1002_length(req) + 4;
2811
2812 for (i = 1; i < num; i++) {
2813 inc_rfc1001_len(req, size[i]);
2814 le32_add_cpu(&req->BufferLength, size[i]);
2815 iov[i].iov_base = (char *)data[i];
2816 iov[i].iov_len = size[i];
2817 }
2818
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002819 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0, &rsp_iov);
2820 cifs_small_buf_release(req);
2821 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002822
Steve French7d3fb242013-11-18 09:56:28 -06002823 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002824 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06002825
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002826 free_rsp_buf(resp_buftype, rsp);
2827 kfree(iov);
2828 return rc;
2829}
2830
2831int
2832SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2833 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2834{
2835 struct smb2_file_rename_info info;
2836 void **data;
2837 unsigned int size[2];
2838 int rc;
2839 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2840
2841 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2842 if (!data)
2843 return -ENOMEM;
2844
2845 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2846 /* 0 = fail if target already exists */
2847 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2848 info.FileNameLength = cpu_to_le32(len);
2849
2850 data[0] = &info;
2851 size[0] = sizeof(struct smb2_file_rename_info);
2852
2853 data[1] = target_file;
2854 size[1] = len + 2 /* null */;
2855
2856 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002857 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2858 size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002859 kfree(data);
2860 return rc;
2861}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002862
2863int
Steve French897fba12016-05-12 21:20:36 -05002864SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2865 u64 persistent_fid, u64 volatile_fid)
2866{
2867 __u8 delete_pending = 1;
2868 void *data;
2869 unsigned int size;
2870
2871 data = &delete_pending;
2872 size = 1; /* sizeof __u8 */
2873
2874 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2875 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2876 &size);
2877}
2878
2879int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002880SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2881 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2882{
2883 struct smb2_file_link_info info;
2884 void **data;
2885 unsigned int size[2];
2886 int rc;
2887 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2888
2889 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2890 if (!data)
2891 return -ENOMEM;
2892
2893 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2894 /* 0 = fail if link already exists */
2895 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2896 info.FileNameLength = cpu_to_le32(len);
2897
2898 data[0] = &info;
2899 size[0] = sizeof(struct smb2_file_link_info);
2900
2901 data[1] = target_file;
2902 size[1] = len + 2 /* null */;
2903
2904 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002905 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002906 kfree(data);
2907 return rc;
2908}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002909
2910int
2911SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05002912 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002913{
2914 struct smb2_file_eof_info info;
2915 void *data;
2916 unsigned int size;
2917
2918 info.EndOfFile = *eof;
2919
2920 data = &info;
2921 size = sizeof(struct smb2_file_eof_info);
2922
Steve Frenchf29ebb42014-07-19 21:44:58 -05002923 if (is_falloc)
2924 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2925 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2926 else
2927 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2928 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002929}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07002930
2931int
2932SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2933 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2934{
2935 unsigned int size;
2936 size = sizeof(FILE_BASIC_INFO);
2937 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2938 current->tgid, FILE_BASIC_INFORMATION, 1,
2939 (void **)&buf, &size);
2940}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002941
2942int
2943SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2944 const u64 persistent_fid, const u64 volatile_fid,
2945 __u8 oplock_level)
2946{
2947 int rc;
2948 struct smb2_oplock_break *req = NULL;
2949
Joe Perchesf96637b2013-05-04 22:12:25 -05002950 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002951 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2952
2953 if (rc)
2954 return rc;
2955
2956 req->VolatileFid = volatile_fid;
2957 req->PersistentFid = persistent_fid;
2958 req->OplockLevel = oplock_level;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002959 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002960
2961 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002962 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002963
2964 if (rc) {
2965 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002966 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002967 }
2968
2969 return rc;
2970}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002971
2972static void
2973copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2974 struct kstatfs *kst)
2975{
2976 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2977 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2978 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
2979 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
2980 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
2981 return;
2982}
2983
2984static int
2985build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2986 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2987{
2988 int rc;
2989 struct smb2_query_info_req *req;
2990
Joe Perchesf96637b2013-05-04 22:12:25 -05002991 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002992
2993 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2994 return -EIO;
2995
2996 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2997 if (rc)
2998 return rc;
2999
3000 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3001 req->FileInfoClass = level;
3002 req->PersistentFileId = persistent_fid;
3003 req->VolatileFileId = volatile_fid;
3004 /* 4 for rfc1002 length field and 1 for pad */
3005 req->InputBufferOffset =
3006 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
3007 req->OutputBufferLength = cpu_to_le32(
3008 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3009
3010 iov->iov_base = (char *)req;
3011 /* 4 for rfc1002 length field */
3012 iov->iov_len = get_rfc1002_length(req) + 4;
3013 return 0;
3014}
3015
3016int
3017SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3018 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3019{
3020 struct smb2_query_info_rsp *rsp = NULL;
3021 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003022 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003023 int rc = 0;
3024 int resp_buftype;
3025 struct cifs_ses *ses = tcon->ses;
3026 struct smb2_fs_full_size_info *info = NULL;
3027
3028 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3029 sizeof(struct smb2_fs_full_size_info),
3030 persistent_fid, volatile_fid);
3031 if (rc)
3032 return rc;
3033
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003034 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0, &rsp_iov);
3035 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003036 if (rc) {
3037 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003038 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003039 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003040 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003041
3042 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3043 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3044 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3045 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3046 sizeof(struct smb2_fs_full_size_info));
3047 if (!rc)
3048 copy_fs_info_to_kstatfs(info, fsdata);
3049
Steve French34f62642013-10-09 02:07:00 -05003050qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003051 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003052 return rc;
3053}
3054
3055int
3056SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003057 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003058{
3059 struct smb2_query_info_rsp *rsp = NULL;
3060 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003061 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003062 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003063 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003064 struct cifs_ses *ses = tcon->ses;
3065 unsigned int rsp_len, offset;
3066
Steven French21671142013-10-09 13:36:35 -05003067 if (level == FS_DEVICE_INFORMATION) {
3068 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3069 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3070 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3071 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3072 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003073 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3074 max_len = sizeof(struct smb3_fs_ss_info);
3075 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003076 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003077 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003078 return -EINVAL;
3079 }
3080
3081 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003082 persistent_fid, volatile_fid);
3083 if (rc)
3084 return rc;
3085
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003086 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0, &rsp_iov);
3087 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003088 if (rc) {
3089 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3090 goto qfsattr_exit;
3091 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003092 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003093
3094 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3095 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003096 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3097 if (rc)
3098 goto qfsattr_exit;
3099
3100 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003101 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3102 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003103 rsp_len, max_len));
3104 else if (level == FS_DEVICE_INFORMATION)
3105 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3106 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003107 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3108 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3109 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3110 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3111 tcon->perf_sector_size =
3112 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3113 }
Steve French34f62642013-10-09 02:07:00 -05003114
3115qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003116 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003117 return rc;
3118}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003119
3120int
3121smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3122 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3123 const __u32 num_lock, struct smb2_lock_element *buf)
3124{
3125 int rc = 0;
3126 struct smb2_lock_req *req = NULL;
3127 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003128 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003129 int resp_buf_type;
3130 unsigned int count;
3131
Joe Perchesf96637b2013-05-04 22:12:25 -05003132 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003133
3134 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3135 if (rc)
3136 return rc;
3137
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003138 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003139 req->LockCount = cpu_to_le16(num_lock);
3140
3141 req->PersistentFileId = persist_fid;
3142 req->VolatileFileId = volatile_fid;
3143
3144 count = num_lock * sizeof(struct smb2_lock_element);
3145 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3146
3147 iov[0].iov_base = (char *)req;
3148 /* 4 for rfc1002 length field and count for all locks */
3149 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3150 iov[1].iov_base = (char *)buf;
3151 iov[1].iov_len = count;
3152
3153 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003154 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP,
3155 &rsp_iov);
3156 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003157 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003158 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003159 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3160 }
3161
3162 return rc;
3163}
3164
3165int
3166SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3167 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3168 const __u64 length, const __u64 offset, const __u32 lock_flags,
3169 const bool wait)
3170{
3171 struct smb2_lock_element lock;
3172
3173 lock.Offset = cpu_to_le64(offset);
3174 lock.Length = cpu_to_le64(length);
3175 lock.Flags = cpu_to_le32(lock_flags);
3176 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3177 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3178
3179 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3180}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003181
3182int
3183SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3184 __u8 *lease_key, const __le32 lease_state)
3185{
3186 int rc;
3187 struct smb2_lease_ack *req = NULL;
3188
Joe Perchesf96637b2013-05-04 22:12:25 -05003189 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003190 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3191
3192 if (rc)
3193 return rc;
3194
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003195 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003196 req->StructureSize = cpu_to_le16(36);
3197 inc_rfc1001_len(req, 12);
3198
3199 memcpy(req->LeaseKey, lease_key, 16);
3200 req->LeaseState = lease_state;
3201
3202 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003203 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003204
3205 if (rc) {
3206 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003207 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003208 }
3209
3210 return rc;
3211}