blob: 12875d55c5a9fcce9ee9b8cc71ddced3b93e2141 [file] [log] [blame]
Steve French1080ef72011-02-24 18:07:19 +00001/*
2 * SMB2 version specific operations
3 *
4 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
5 *
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License v2 as published
8 * by the Free Software Foundation.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -070020#include <linux/pagemap.h>
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -070021#include <linux/vfs.h>
Steve Frenchf29ebb42014-07-19 21:44:58 -050022#include <linux/falloc.h>
Pavel Shilovsky026e93d2016-11-03 16:47:37 -070023#include <linux/scatterlist.h>
Tobias Regnery4fa8e502017-03-30 12:34:14 +020024#include <linux/uuid.h>
Pavel Shilovsky026e93d2016-11-03 16:47:37 -070025#include <crypto/aead.h>
Steve French1080ef72011-02-24 18:07:19 +000026#include "cifsglob.h"
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040027#include "smb2pdu.h"
28#include "smb2proto.h"
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040029#include "cifsproto.h"
30#include "cifs_debug.h"
Pavel Shilovskyb42bf882013-08-14 19:25:21 +040031#include "cifs_unicode.h"
Pavel Shilovsky2e44b282012-09-18 16:20:33 -070032#include "smb2status.h"
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -070033#include "smb2glob.h"
Steve French834170c2016-09-30 21:14:26 -050034#include "cifs_ioctl.h"
Long Li09902f82017-11-22 17:38:39 -070035#include "smbdirect.h"
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040036
37static int
38change_conf(struct TCP_Server_Info *server)
39{
40 server->credits += server->echo_credits + server->oplock_credits;
41 server->oplock_credits = server->echo_credits = 0;
42 switch (server->credits) {
43 case 0:
44 return -1;
45 case 1:
46 server->echoes = false;
47 server->oplocks = false;
Joe Perchesf96637b2013-05-04 22:12:25 -050048 cifs_dbg(VFS, "disabling echoes and oplocks\n");
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040049 break;
50 case 2:
51 server->echoes = true;
52 server->oplocks = false;
53 server->echo_credits = 1;
Joe Perchesf96637b2013-05-04 22:12:25 -050054 cifs_dbg(FYI, "disabling oplocks\n");
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040055 break;
56 default:
57 server->echoes = true;
Steve Frenche0ddde92015-09-22 09:29:38 -050058 if (enable_oplocks) {
59 server->oplocks = true;
60 server->oplock_credits = 1;
61 } else
62 server->oplocks = false;
63
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040064 server->echo_credits = 1;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040065 }
66 server->credits -= server->echo_credits + server->oplock_credits;
67 return 0;
68}
69
70static void
71smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
72 const int optype)
73{
74 int *val, rc = 0;
75 spin_lock(&server->req_lock);
76 val = server->ops->get_credits_field(server, optype);
77 *val += add;
Steve French141891f2016-09-23 00:44:16 -050078 if (*val > 65000) {
79 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
80 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
81 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040082 server->in_flight--;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040083 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040084 rc = change_conf(server);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -070085 /*
86 * Sometimes server returns 0 credits on oplock break ack - we need to
87 * rebalance credits in this case.
88 */
89 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
90 server->oplocks) {
91 if (server->credits > 1) {
92 server->credits--;
93 server->oplock_credits++;
94 }
95 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040096 spin_unlock(&server->req_lock);
97 wake_up(&server->request_q);
98 if (rc)
99 cifs_reconnect(server);
100}
101
102static void
103smb2_set_credits(struct TCP_Server_Info *server, const int val)
104{
105 spin_lock(&server->req_lock);
106 server->credits = val;
107 spin_unlock(&server->req_lock);
108}
109
110static int *
111smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
112{
113 switch (optype) {
114 case CIFS_ECHO_OP:
115 return &server->echo_credits;
116 case CIFS_OBREAK_OP:
117 return &server->oplock_credits;
118 default:
119 return &server->credits;
120 }
121}
122
123static unsigned int
124smb2_get_credits(struct mid_q_entry *mid)
125{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700126 struct smb2_sync_hdr *shdr = get_sync_hdr(mid->resp_buf);
127
128 return le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400129}
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400130
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400131static int
132smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
133 unsigned int *num, unsigned int *credits)
134{
135 int rc = 0;
136 unsigned int scredits;
137
138 spin_lock(&server->req_lock);
139 while (1) {
140 if (server->credits <= 0) {
141 spin_unlock(&server->req_lock);
142 cifs_num_waiters_inc(server);
143 rc = wait_event_killable(server->request_q,
144 has_credits(server, &server->credits));
145 cifs_num_waiters_dec(server);
146 if (rc)
147 return rc;
148 spin_lock(&server->req_lock);
149 } else {
150 if (server->tcpStatus == CifsExiting) {
151 spin_unlock(&server->req_lock);
152 return -ENOENT;
153 }
154
155 scredits = server->credits;
156 /* can deadlock with reopen */
157 if (scredits == 1) {
158 *num = SMB2_MAX_BUFFER_SIZE;
159 *credits = 0;
160 break;
161 }
162
163 /* leave one credit for a possible reopen */
164 scredits--;
165 *num = min_t(unsigned int, size,
166 scredits * SMB2_MAX_BUFFER_SIZE);
167
168 *credits = DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
169 server->credits -= *credits;
170 server->in_flight++;
171 break;
172 }
173 }
174 spin_unlock(&server->req_lock);
175 return rc;
176}
177
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400178static __u64
179smb2_get_next_mid(struct TCP_Server_Info *server)
180{
181 __u64 mid;
182 /* for SMB2 we need the current value */
183 spin_lock(&GlobalMid_Lock);
184 mid = server->CurrentMid++;
185 spin_unlock(&GlobalMid_Lock);
186 return mid;
187}
Steve French1080ef72011-02-24 18:07:19 +0000188
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400189static struct mid_q_entry *
190smb2_find_mid(struct TCP_Server_Info *server, char *buf)
191{
192 struct mid_q_entry *mid;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700193 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
194 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400195
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700196 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
Steve French373512e2015-12-18 13:05:30 -0600197 cifs_dbg(VFS, "encrypted frame parsing not supported yet");
198 return NULL;
199 }
200
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400201 spin_lock(&GlobalMid_Lock);
202 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
Sachin Prabhu9235d092014-12-09 17:37:00 +0000203 if ((mid->mid == wire_mid) &&
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400204 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700205 (mid->command == shdr->Command)) {
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400206 spin_unlock(&GlobalMid_Lock);
207 return mid;
208 }
209 }
210 spin_unlock(&GlobalMid_Lock);
211 return NULL;
212}
213
214static void
215smb2_dump_detail(void *buf)
216{
217#ifdef CONFIG_CIFS_DEBUG2
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700218 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400219
Joe Perchesf96637b2013-05-04 22:12:25 -0500220 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700221 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
222 shdr->ProcessId);
223 cifs_dbg(VFS, "smb buf %p len %u\n", buf, smb2_calc_size(buf));
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400224#endif
225}
226
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400227static bool
228smb2_need_neg(struct TCP_Server_Info *server)
229{
230 return server->max_read == 0;
231}
232
233static int
234smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
235{
236 int rc;
237 ses->server->CurrentMid = 0;
238 rc = SMB2_negotiate(xid, ses);
239 /* BB we probably don't need to retry with modern servers */
240 if (rc == -EAGAIN)
241 rc = -EHOSTDOWN;
242 return rc;
243}
244
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700245static unsigned int
246smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
247{
248 struct TCP_Server_Info *server = tcon->ses->server;
249 unsigned int wsize;
250
251 /* start with specified wsize, or default */
252 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
253 wsize = min_t(unsigned int, wsize, server->max_write);
Long Li09902f82017-11-22 17:38:39 -0700254#ifdef CONFIG_CIFS_SMB_DIRECT
Long Libb4c0412018-04-17 12:17:08 -0700255 if (server->rdma) {
256 if (server->sign)
257 wsize = min_t(unsigned int,
258 wsize, server->smbd_conn->max_fragmented_send_size);
259 else
260 wsize = min_t(unsigned int,
Long Li09902f82017-11-22 17:38:39 -0700261 wsize, server->smbd_conn->max_readwrite_size);
Long Libb4c0412018-04-17 12:17:08 -0700262 }
Long Li09902f82017-11-22 17:38:39 -0700263#endif
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400264 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
265 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700266
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700267 return wsize;
268}
269
270static unsigned int
271smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
272{
273 struct TCP_Server_Info *server = tcon->ses->server;
274 unsigned int rsize;
275
276 /* start with specified rsize, or default */
277 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
278 rsize = min_t(unsigned int, rsize, server->max_read);
Long Li09902f82017-11-22 17:38:39 -0700279#ifdef CONFIG_CIFS_SMB_DIRECT
Long Libb4c0412018-04-17 12:17:08 -0700280 if (server->rdma) {
281 if (server->sign)
282 rsize = min_t(unsigned int,
283 rsize, server->smbd_conn->max_fragmented_recv_size);
284 else
285 rsize = min_t(unsigned int,
Long Li09902f82017-11-22 17:38:39 -0700286 rsize, server->smbd_conn->max_readwrite_size);
Long Libb4c0412018-04-17 12:17:08 -0700287 }
Long Li09902f82017-11-22 17:38:39 -0700288#endif
Pavel Shilovskybed9da02014-06-25 11:28:57 +0400289
290 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
291 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700292
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700293 return rsize;
294}
295
Steve Frenchc481e9f2013-10-14 01:21:53 -0500296#ifdef CONFIG_CIFS_STATS2
297static int
298SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
299{
300 int rc;
301 unsigned int ret_data_len = 0;
302 struct network_interface_info_ioctl_rsp *out_buf;
303
304 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
305 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
306 NULL /* no data input */, 0 /* no data input */,
307 (char **)&out_buf, &ret_data_len);
Steve French9ffc5412014-10-16 15:13:14 -0500308 if (rc != 0)
309 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
310 else if (ret_data_len < sizeof(struct network_interface_info_ioctl_rsp)) {
311 cifs_dbg(VFS, "server returned bad net interface info buf\n");
312 rc = -EINVAL;
313 } else {
Steve Frenchc481e9f2013-10-14 01:21:53 -0500314 /* Dump info on first interface */
315 cifs_dbg(FYI, "Adapter Capability 0x%x\t",
316 le32_to_cpu(out_buf->Capability));
317 cifs_dbg(FYI, "Link Speed %lld\n",
318 le64_to_cpu(out_buf->LinkSpeed));
Steve French9ffc5412014-10-16 15:13:14 -0500319 }
Steve French24df1482016-09-29 04:20:23 -0500320 kfree(out_buf);
Steve Frenchc481e9f2013-10-14 01:21:53 -0500321 return rc;
322}
323#endif /* STATS2 */
324
Steve French3d4ef9a2018-04-25 22:19:09 -0500325/*
326 * Open the directory at the root of a share
327 */
328int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
329{
330 struct cifs_open_parms oparams;
331 int rc;
332 __le16 srch_path = 0; /* Null - since an open of top of share */
333 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
334
335 mutex_lock(&tcon->prfid_mutex);
336 if (tcon->valid_root_fid) {
337 cifs_dbg(FYI, "found a cached root file handle\n");
338 memcpy(pfid, tcon->prfid, sizeof(struct cifs_fid));
339 mutex_unlock(&tcon->prfid_mutex);
340 return 0;
341 }
342
343 oparams.tcon = tcon;
344 oparams.create_options = 0;
345 oparams.desired_access = FILE_READ_ATTRIBUTES;
346 oparams.disposition = FILE_OPEN;
347 oparams.fid = pfid;
348 oparams.reconnect = false;
349
350 rc = SMB2_open(xid, &oparams, &srch_path, &oplock, NULL, NULL);
351 if (rc == 0) {
352 memcpy(tcon->prfid, pfid, sizeof(struct cifs_fid));
353 tcon->valid_root_fid = true;
354 }
355 mutex_unlock(&tcon->prfid_mutex);
356 return rc;
357}
358
Steve French34f62642013-10-09 02:07:00 -0500359static void
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500360smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
361{
362 int rc;
363 __le16 srch_path = 0; /* Null - open root of share */
364 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
365 struct cifs_open_parms oparms;
366 struct cifs_fid fid;
Steve French3d4ef9a2018-04-25 22:19:09 -0500367 bool no_cached_open = tcon->nohandlecache;
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500368
369 oparms.tcon = tcon;
370 oparms.desired_access = FILE_READ_ATTRIBUTES;
371 oparms.disposition = FILE_OPEN;
372 oparms.create_options = 0;
373 oparms.fid = &fid;
374 oparms.reconnect = false;
375
Steve French3d4ef9a2018-04-25 22:19:09 -0500376 if (no_cached_open)
377 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
378 else
379 rc = open_shroot(xid, tcon, &fid);
380
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500381 if (rc)
382 return;
383
Steve Frenchc481e9f2013-10-14 01:21:53 -0500384#ifdef CONFIG_CIFS_STATS2
385 SMB3_request_interfaces(xid, tcon);
386#endif /* STATS2 */
387
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500388 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
389 FS_ATTRIBUTE_INFORMATION);
390 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
391 FS_DEVICE_INFORMATION);
392 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
393 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
Steve French3d4ef9a2018-04-25 22:19:09 -0500394 if (no_cached_open)
395 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500396 return;
397}
398
399static void
Steve French34f62642013-10-09 02:07:00 -0500400smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
401{
402 int rc;
403 __le16 srch_path = 0; /* Null - open root of share */
404 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
405 struct cifs_open_parms oparms;
406 struct cifs_fid fid;
407
408 oparms.tcon = tcon;
409 oparms.desired_access = FILE_READ_ATTRIBUTES;
410 oparms.disposition = FILE_OPEN;
411 oparms.create_options = 0;
412 oparms.fid = &fid;
413 oparms.reconnect = false;
414
415 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
416 if (rc)
417 return;
418
Steven French21671142013-10-09 13:36:35 -0500419 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
420 FS_ATTRIBUTE_INFORMATION);
421 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
422 FS_DEVICE_INFORMATION);
Steve French34f62642013-10-09 02:07:00 -0500423 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
424 return;
425}
426
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400427static int
428smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
429 struct cifs_sb_info *cifs_sb, const char *full_path)
430{
431 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400432 __le16 *utf16_path;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700433 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400434 struct cifs_open_parms oparms;
435 struct cifs_fid fid;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400436
Steve French3d4ef9a2018-04-25 22:19:09 -0500437 if ((*full_path == 0) && tcon->valid_root_fid)
438 return 0;
439
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400440 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
441 if (!utf16_path)
442 return -ENOMEM;
443
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400444 oparms.tcon = tcon;
445 oparms.desired_access = FILE_READ_ATTRIBUTES;
446 oparms.disposition = FILE_OPEN;
447 oparms.create_options = 0;
448 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +0400449 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400450
Pavel Shilovskyb42bf882013-08-14 19:25:21 +0400451 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400452 if (rc) {
453 kfree(utf16_path);
454 return rc;
455 }
456
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400457 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400458 kfree(utf16_path);
459 return rc;
460}
461
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400462static int
463smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
464 struct cifs_sb_info *cifs_sb, const char *full_path,
465 u64 *uniqueid, FILE_ALL_INFO *data)
466{
467 *uniqueid = le64_to_cpu(data->IndexNumber);
468 return 0;
469}
470
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700471static int
472smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
473 struct cifs_fid *fid, FILE_ALL_INFO *data)
474{
475 int rc;
476 struct smb2_file_all_info *smb2_data;
477
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +0400478 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700479 GFP_KERNEL);
480 if (smb2_data == NULL)
481 return -ENOMEM;
482
483 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
484 smb2_data);
485 if (!rc)
486 move_smb2_info_to_cifs(data, smb2_data);
487 kfree(smb2_data);
488 return rc;
489}
490
Arnd Bergmann1368f152017-09-05 11:24:15 +0200491#ifdef CONFIG_CIFS_XATTR
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000492static ssize_t
493move_smb2_ea_to_cifs(char *dst, size_t dst_size,
494 struct smb2_file_full_ea_info *src, size_t src_size,
495 const unsigned char *ea_name)
496{
497 int rc = 0;
498 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
499 char *name, *value;
500 size_t name_len, value_len, user_name_len;
501
502 while (src_size > 0) {
503 name = &src->ea_data[0];
504 name_len = (size_t)src->ea_name_length;
505 value = &src->ea_data[src->ea_name_length + 1];
506 value_len = (size_t)le16_to_cpu(src->ea_value_length);
507
508 if (name_len == 0) {
509 break;
510 }
511
512 if (src_size < 8 + name_len + 1 + value_len) {
513 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
514 rc = -EIO;
515 goto out;
516 }
517
518 if (ea_name) {
519 if (ea_name_len == name_len &&
520 memcmp(ea_name, name, name_len) == 0) {
521 rc = value_len;
522 if (dst_size == 0)
523 goto out;
524 if (dst_size < value_len) {
525 rc = -ERANGE;
526 goto out;
527 }
528 memcpy(dst, value, value_len);
529 goto out;
530 }
531 } else {
532 /* 'user.' plus a terminating null */
533 user_name_len = 5 + 1 + name_len;
534
535 rc += user_name_len;
536
537 if (dst_size >= user_name_len) {
538 dst_size -= user_name_len;
539 memcpy(dst, "user.", 5);
540 dst += 5;
541 memcpy(dst, src->ea_data, name_len);
542 dst += name_len;
543 *dst = 0;
544 ++dst;
545 } else if (dst_size == 0) {
546 /* skip copy - calc size only */
547 } else {
548 /* stop before overrun buffer */
549 rc = -ERANGE;
550 break;
551 }
552 }
553
554 if (!src->next_entry_offset)
555 break;
556
557 if (src_size < le32_to_cpu(src->next_entry_offset)) {
558 /* stop before overrun buffer */
559 rc = -ERANGE;
560 break;
561 }
562 src_size -= le32_to_cpu(src->next_entry_offset);
563 src = (void *)((char *)src +
564 le32_to_cpu(src->next_entry_offset));
565 }
566
567 /* didn't find the named attribute */
568 if (ea_name)
569 rc = -ENODATA;
570
571out:
572 return (ssize_t)rc;
573}
574
575static ssize_t
576smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
577 const unsigned char *path, const unsigned char *ea_name,
578 char *ea_data, size_t buf_size,
579 struct cifs_sb_info *cifs_sb)
580{
581 int rc;
582 __le16 *utf16_path;
583 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
584 struct cifs_open_parms oparms;
585 struct cifs_fid fid;
586 struct smb2_file_full_ea_info *smb2_data;
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +1000587 int ea_buf_size = SMB2_MIN_EA_BUF;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000588
589 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
590 if (!utf16_path)
591 return -ENOMEM;
592
593 oparms.tcon = tcon;
594 oparms.desired_access = FILE_READ_EA;
595 oparms.disposition = FILE_OPEN;
596 oparms.create_options = 0;
597 oparms.fid = &fid;
598 oparms.reconnect = false;
599
600 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
601 kfree(utf16_path);
602 if (rc) {
603 cifs_dbg(FYI, "open failed rc=%d\n", rc);
604 return rc;
605 }
606
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +1000607 while (1) {
608 smb2_data = kzalloc(ea_buf_size, GFP_KERNEL);
609 if (smb2_data == NULL) {
610 SMB2_close(xid, tcon, fid.persistent_fid,
611 fid.volatile_fid);
612 return -ENOMEM;
613 }
614
615 rc = SMB2_query_eas(xid, tcon, fid.persistent_fid,
616 fid.volatile_fid,
617 ea_buf_size, smb2_data);
618
619 if (rc != -E2BIG)
620 break;
621
622 kfree(smb2_data);
623 ea_buf_size <<= 1;
624
625 if (ea_buf_size > SMB2_MAX_EA_BUF) {
626 cifs_dbg(VFS, "EA size is too large\n");
627 SMB2_close(xid, tcon, fid.persistent_fid,
628 fid.volatile_fid);
629 return -ENOMEM;
630 }
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000631 }
632
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000633 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
634
Paulo Alcantaraae2cd7f2018-05-04 11:25:26 -0300635 /*
636 * If ea_name is NULL (listxattr) and there are no EAs, return 0 as it's
637 * not an error. Otherwise, the specified ea_name was not found.
638 */
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000639 if (!rc)
640 rc = move_smb2_ea_to_cifs(ea_data, buf_size, smb2_data,
641 SMB2_MAX_EA_BUF, ea_name);
Paulo Alcantaraae2cd7f2018-05-04 11:25:26 -0300642 else if (!ea_name && rc == -ENODATA)
643 rc = 0;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000644
645 kfree(smb2_data);
646 return rc;
647}
648
Ronnie Sahlberg55175542017-08-24 11:24:56 +1000649
650static int
651smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
652 const char *path, const char *ea_name, const void *ea_value,
653 const __u16 ea_value_len, const struct nls_table *nls_codepage,
654 struct cifs_sb_info *cifs_sb)
655{
656 int rc;
657 __le16 *utf16_path;
658 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
659 struct cifs_open_parms oparms;
660 struct cifs_fid fid;
661 struct smb2_file_full_ea_info *ea;
662 int ea_name_len = strlen(ea_name);
663 int len;
664
665 if (ea_name_len > 255)
666 return -EINVAL;
667
668 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
669 if (!utf16_path)
670 return -ENOMEM;
671
672 oparms.tcon = tcon;
673 oparms.desired_access = FILE_WRITE_EA;
674 oparms.disposition = FILE_OPEN;
675 oparms.create_options = 0;
676 oparms.fid = &fid;
677 oparms.reconnect = false;
678
679 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
680 kfree(utf16_path);
681 if (rc) {
682 cifs_dbg(FYI, "open failed rc=%d\n", rc);
683 return rc;
684 }
685
686 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
687 ea = kzalloc(len, GFP_KERNEL);
688 if (ea == NULL) {
689 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
690 return -ENOMEM;
691 }
692
693 ea->ea_name_length = ea_name_len;
694 ea->ea_value_length = cpu_to_le16(ea_value_len);
695 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
696 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
697
698 rc = SMB2_set_ea(xid, tcon, fid.persistent_fid, fid.volatile_fid, ea,
699 len);
700 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
701
702 return rc;
703}
Arnd Bergmann1368f152017-09-05 11:24:15 +0200704#endif
Ronnie Sahlberg55175542017-08-24 11:24:56 +1000705
Pavel Shilovsky9094fad2012-07-12 18:30:44 +0400706static bool
707smb2_can_echo(struct TCP_Server_Info *server)
708{
709 return server->echoes;
710}
711
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400712static void
713smb2_clear_stats(struct cifs_tcon *tcon)
714{
715#ifdef CONFIG_CIFS_STATS
716 int i;
717 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
718 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
719 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
720 }
721#endif
722}
723
724static void
Steve French769ee6a2013-06-19 14:15:30 -0500725smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
726{
727 seq_puts(m, "\n\tShare Capabilities:");
728 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
729 seq_puts(m, " DFS,");
730 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
731 seq_puts(m, " CONTINUOUS AVAILABILITY,");
732 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
733 seq_puts(m, " SCALEOUT,");
734 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
735 seq_puts(m, " CLUSTER,");
736 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
737 seq_puts(m, " ASYMMETRIC,");
738 if (tcon->capabilities == 0)
739 seq_puts(m, " None");
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500740 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
741 seq_puts(m, " Aligned,");
742 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
743 seq_puts(m, " Partition Aligned,");
744 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
745 seq_puts(m, " SSD,");
746 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
747 seq_puts(m, " TRIM-support,");
748
Steve French769ee6a2013-06-19 14:15:30 -0500749 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500750 if (tcon->perf_sector_size)
751 seq_printf(m, "\tOptimal sector size: 0x%x",
752 tcon->perf_sector_size);
Steve French769ee6a2013-06-19 14:15:30 -0500753}
754
755static void
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400756smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
757{
758#ifdef CONFIG_CIFS_STATS
759 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
760 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
761 seq_printf(m, "\nNegotiates: %d sent %d failed",
762 atomic_read(&sent[SMB2_NEGOTIATE_HE]),
763 atomic_read(&failed[SMB2_NEGOTIATE_HE]));
764 seq_printf(m, "\nSessionSetups: %d sent %d failed",
765 atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
766 atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400767 seq_printf(m, "\nLogoffs: %d sent %d failed",
768 atomic_read(&sent[SMB2_LOGOFF_HE]),
769 atomic_read(&failed[SMB2_LOGOFF_HE]));
770 seq_printf(m, "\nTreeConnects: %d sent %d failed",
771 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
772 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
773 seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
774 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
775 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
776 seq_printf(m, "\nCreates: %d sent %d failed",
777 atomic_read(&sent[SMB2_CREATE_HE]),
778 atomic_read(&failed[SMB2_CREATE_HE]));
779 seq_printf(m, "\nCloses: %d sent %d failed",
780 atomic_read(&sent[SMB2_CLOSE_HE]),
781 atomic_read(&failed[SMB2_CLOSE_HE]));
782 seq_printf(m, "\nFlushes: %d sent %d failed",
783 atomic_read(&sent[SMB2_FLUSH_HE]),
784 atomic_read(&failed[SMB2_FLUSH_HE]));
785 seq_printf(m, "\nReads: %d sent %d failed",
786 atomic_read(&sent[SMB2_READ_HE]),
787 atomic_read(&failed[SMB2_READ_HE]));
788 seq_printf(m, "\nWrites: %d sent %d failed",
789 atomic_read(&sent[SMB2_WRITE_HE]),
790 atomic_read(&failed[SMB2_WRITE_HE]));
791 seq_printf(m, "\nLocks: %d sent %d failed",
792 atomic_read(&sent[SMB2_LOCK_HE]),
793 atomic_read(&failed[SMB2_LOCK_HE]));
794 seq_printf(m, "\nIOCTLs: %d sent %d failed",
795 atomic_read(&sent[SMB2_IOCTL_HE]),
796 atomic_read(&failed[SMB2_IOCTL_HE]));
797 seq_printf(m, "\nCancels: %d sent %d failed",
798 atomic_read(&sent[SMB2_CANCEL_HE]),
799 atomic_read(&failed[SMB2_CANCEL_HE]));
800 seq_printf(m, "\nEchos: %d sent %d failed",
801 atomic_read(&sent[SMB2_ECHO_HE]),
802 atomic_read(&failed[SMB2_ECHO_HE]));
803 seq_printf(m, "\nQueryDirectories: %d sent %d failed",
804 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
805 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
806 seq_printf(m, "\nChangeNotifies: %d sent %d failed",
807 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
808 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
809 seq_printf(m, "\nQueryInfos: %d sent %d failed",
810 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
811 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
812 seq_printf(m, "\nSetInfos: %d sent %d failed",
813 atomic_read(&sent[SMB2_SET_INFO_HE]),
814 atomic_read(&failed[SMB2_SET_INFO_HE]));
815 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
816 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
817 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
818#endif
819}
820
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700821static void
822smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
823{
David Howells2b0143b2015-03-17 22:25:59 +0000824 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovsky53ef1012013-09-05 16:11:28 +0400825 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
826
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700827 cfile->fid.persistent_fid = fid->persistent_fid;
828 cfile->fid.volatile_fid = fid->volatile_fid;
Pavel Shilovsky42873b02013-09-05 21:30:16 +0400829 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
830 &fid->purge_cache);
Pavel Shilovsky18cceb62013-09-05 13:01:06 +0400831 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
Aurelien Aptel94f87372016-09-22 07:38:50 +0200832 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700833}
834
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400835static void
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700836smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
837 struct cifs_fid *fid)
838{
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400839 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700840}
841
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -0700842static int
Steve French41c13582013-11-14 00:05:36 -0600843SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
844 u64 persistent_fid, u64 volatile_fid,
845 struct copychunk_ioctl *pcchunk)
846{
847 int rc;
848 unsigned int ret_data_len;
849 struct resume_key_req *res_key;
850
851 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
852 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
853 NULL, 0 /* no input */,
854 (char **)&res_key, &ret_data_len);
855
856 if (rc) {
857 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
858 goto req_res_key_exit;
859 }
860 if (ret_data_len < sizeof(struct resume_key_req)) {
861 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
862 rc = -EINVAL;
863 goto req_res_key_exit;
864 }
865 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
866
867req_res_key_exit:
868 kfree(res_key);
869 return rc;
870}
871
Sachin Prabhu620d8742017-02-10 16:03:51 +0530872static ssize_t
Sachin Prabhu312bbc52017-04-04 02:12:04 -0500873smb2_copychunk_range(const unsigned int xid,
Steve French41c13582013-11-14 00:05:36 -0600874 struct cifsFileInfo *srcfile,
875 struct cifsFileInfo *trgtfile, u64 src_off,
876 u64 len, u64 dest_off)
877{
878 int rc;
879 unsigned int ret_data_len;
880 struct copychunk_ioctl *pcchunk;
Steve French9bf0c9c2013-11-16 18:05:28 -0600881 struct copychunk_ioctl_rsp *retbuf = NULL;
882 struct cifs_tcon *tcon;
883 int chunks_copied = 0;
884 bool chunk_sizes_updated = false;
Sachin Prabhu620d8742017-02-10 16:03:51 +0530885 ssize_t bytes_written, total_bytes_written = 0;
Steve French41c13582013-11-14 00:05:36 -0600886
887 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
888
889 if (pcchunk == NULL)
890 return -ENOMEM;
891
Sachin Prabhu312bbc52017-04-04 02:12:04 -0500892 cifs_dbg(FYI, "in smb2_copychunk_range - about to call request res key\n");
Steve French41c13582013-11-14 00:05:36 -0600893 /* Request a key from the server to identify the source of the copy */
894 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
895 srcfile->fid.persistent_fid,
896 srcfile->fid.volatile_fid, pcchunk);
897
898 /* Note: request_res_key sets res_key null only if rc !=0 */
899 if (rc)
Steve French9bf0c9c2013-11-16 18:05:28 -0600900 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -0600901
902 /* For now array only one chunk long, will make more flexible later */
Fabian Frederickbc09d142014-12-10 15:41:15 -0800903 pcchunk->ChunkCount = cpu_to_le32(1);
Steve French41c13582013-11-14 00:05:36 -0600904 pcchunk->Reserved = 0;
Steve French41c13582013-11-14 00:05:36 -0600905 pcchunk->Reserved2 = 0;
906
Steve French9bf0c9c2013-11-16 18:05:28 -0600907 tcon = tlink_tcon(trgtfile->tlink);
908
909 while (len > 0) {
910 pcchunk->SourceOffset = cpu_to_le64(src_off);
911 pcchunk->TargetOffset = cpu_to_le64(dest_off);
912 pcchunk->Length =
913 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
914
915 /* Request server copy to target from src identified by key */
916 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
Steve French41c13582013-11-14 00:05:36 -0600917 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +0100918 true /* is_fsctl */, (char *)pcchunk,
Steve French9bf0c9c2013-11-16 18:05:28 -0600919 sizeof(struct copychunk_ioctl), (char **)&retbuf,
920 &ret_data_len);
921 if (rc == 0) {
922 if (ret_data_len !=
923 sizeof(struct copychunk_ioctl_rsp)) {
924 cifs_dbg(VFS, "invalid cchunk response size\n");
925 rc = -EIO;
926 goto cchunk_out;
927 }
928 if (retbuf->TotalBytesWritten == 0) {
929 cifs_dbg(FYI, "no bytes copied\n");
930 rc = -EIO;
931 goto cchunk_out;
932 }
933 /*
934 * Check if server claimed to write more than we asked
935 */
936 if (le32_to_cpu(retbuf->TotalBytesWritten) >
937 le32_to_cpu(pcchunk->Length)) {
938 cifs_dbg(VFS, "invalid copy chunk response\n");
939 rc = -EIO;
940 goto cchunk_out;
941 }
942 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
943 cifs_dbg(VFS, "invalid num chunks written\n");
944 rc = -EIO;
945 goto cchunk_out;
946 }
947 chunks_copied++;
Steve French41c13582013-11-14 00:05:36 -0600948
Sachin Prabhu620d8742017-02-10 16:03:51 +0530949 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
950 src_off += bytes_written;
951 dest_off += bytes_written;
952 len -= bytes_written;
953 total_bytes_written += bytes_written;
Steve French41c13582013-11-14 00:05:36 -0600954
Sachin Prabhu620d8742017-02-10 16:03:51 +0530955 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
Steve French9bf0c9c2013-11-16 18:05:28 -0600956 le32_to_cpu(retbuf->ChunksWritten),
957 le32_to_cpu(retbuf->ChunkBytesWritten),
Sachin Prabhu620d8742017-02-10 16:03:51 +0530958 bytes_written);
Steve French9bf0c9c2013-11-16 18:05:28 -0600959 } else if (rc == -EINVAL) {
960 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
961 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -0600962
Steve French9bf0c9c2013-11-16 18:05:28 -0600963 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
964 le32_to_cpu(retbuf->ChunksWritten),
965 le32_to_cpu(retbuf->ChunkBytesWritten),
966 le32_to_cpu(retbuf->TotalBytesWritten));
967
968 /*
969 * Check if this is the first request using these sizes,
970 * (ie check if copy succeed once with original sizes
971 * and check if the server gave us different sizes after
972 * we already updated max sizes on previous request).
973 * if not then why is the server returning an error now
974 */
975 if ((chunks_copied != 0) || chunk_sizes_updated)
976 goto cchunk_out;
977
978 /* Check that server is not asking us to grow size */
979 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
980 tcon->max_bytes_chunk)
981 tcon->max_bytes_chunk =
982 le32_to_cpu(retbuf->ChunkBytesWritten);
983 else
984 goto cchunk_out; /* server gave us bogus size */
985
986 /* No need to change MaxChunks since already set to 1 */
987 chunk_sizes_updated = true;
Sachin Prabhu2477bc52015-02-04 13:10:26 +0000988 } else
989 goto cchunk_out;
Steve French9bf0c9c2013-11-16 18:05:28 -0600990 }
991
992cchunk_out:
Steve French41c13582013-11-14 00:05:36 -0600993 kfree(pcchunk);
Steve French24df1482016-09-29 04:20:23 -0500994 kfree(retbuf);
Sachin Prabhu620d8742017-02-10 16:03:51 +0530995 if (rc)
996 return rc;
997 else
998 return total_bytes_written;
Steve French41c13582013-11-14 00:05:36 -0600999}
1000
1001static int
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001002smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1003 struct cifs_fid *fid)
1004{
1005 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1006}
1007
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001008static unsigned int
1009smb2_read_data_offset(char *buf)
1010{
1011 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1012 return rsp->DataOffset;
1013}
1014
1015static unsigned int
Long Li74dcf412017-11-22 17:38:46 -07001016smb2_read_data_length(char *buf, bool in_remaining)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001017{
1018 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
Long Li74dcf412017-11-22 17:38:46 -07001019
1020 if (in_remaining)
1021 return le32_to_cpu(rsp->DataRemaining);
1022
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001023 return le32_to_cpu(rsp->DataLength);
1024}
1025
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001026
1027static int
Steve Frenchdb8b6312014-09-22 05:13:55 -05001028smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001029 struct cifs_io_parms *parms, unsigned int *bytes_read,
1030 char **buf, int *buf_type)
1031{
Steve Frenchdb8b6312014-09-22 05:13:55 -05001032 parms->persistent_fid = pfid->persistent_fid;
1033 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001034 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1035}
1036
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001037static int
Steve Frenchdb8b6312014-09-22 05:13:55 -05001038smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001039 struct cifs_io_parms *parms, unsigned int *written,
1040 struct kvec *iov, unsigned long nr_segs)
1041{
1042
Steve Frenchdb8b6312014-09-22 05:13:55 -05001043 parms->persistent_fid = pfid->persistent_fid;
1044 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001045 return SMB2_write(xid, parms, written, iov, nr_segs);
1046}
1047
Steve Frenchd43cc792014-08-13 17:16:29 -05001048/* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1049static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1050 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1051{
1052 struct cifsInodeInfo *cifsi;
1053 int rc;
1054
1055 cifsi = CIFS_I(inode);
1056
1057 /* if file already sparse don't bother setting sparse again */
1058 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1059 return true; /* already sparse */
1060
1061 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1062 return true; /* already not sparse */
1063
1064 /*
1065 * Can't check for sparse support on share the usual way via the
1066 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1067 * since Samba server doesn't set the flag on the share, yet
1068 * supports the set sparse FSCTL and returns sparse correctly
1069 * in the file attributes. If we fail setting sparse though we
1070 * mark that server does not support sparse files for this share
1071 * to avoid repeatedly sending the unsupported fsctl to server
1072 * if the file is repeatedly extended.
1073 */
1074 if (tcon->broken_sparse_sup)
1075 return false;
1076
1077 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1078 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001079 true /* is_fctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001080 &setsparse, 1, NULL, NULL);
Steve Frenchd43cc792014-08-13 17:16:29 -05001081 if (rc) {
1082 tcon->broken_sparse_sup = true;
1083 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1084 return false;
1085 }
1086
1087 if (setsparse)
1088 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1089 else
1090 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1091
1092 return true;
1093}
1094
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001095static int
1096smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1097 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1098{
1099 __le64 eof = cpu_to_le64(size);
Steve French3d1a3742014-08-11 21:05:25 -05001100 struct inode *inode;
1101
1102 /*
1103 * If extending file more than one page make sparse. Many Linux fs
1104 * make files sparse by default when extending via ftruncate
1105 */
David Howells2b0143b2015-03-17 22:25:59 +00001106 inode = d_inode(cfile->dentry);
Steve French3d1a3742014-08-11 21:05:25 -05001107
1108 if (!set_alloc && (size > inode->i_size + 8192)) {
Steve French3d1a3742014-08-11 21:05:25 -05001109 __u8 set_sparse = 1;
Steve French3d1a3742014-08-11 21:05:25 -05001110
Steve Frenchd43cc792014-08-13 17:16:29 -05001111 /* whether set sparse succeeds or not, extend the file */
1112 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
Steve French3d1a3742014-08-11 21:05:25 -05001113 }
1114
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001115 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05001116 cfile->fid.volatile_fid, cfile->pid, &eof, false);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001117}
1118
Steve French02b16662015-06-27 21:18:36 -07001119static int
1120smb2_duplicate_extents(const unsigned int xid,
1121 struct cifsFileInfo *srcfile,
1122 struct cifsFileInfo *trgtfile, u64 src_off,
1123 u64 len, u64 dest_off)
1124{
1125 int rc;
1126 unsigned int ret_data_len;
Steve French02b16662015-06-27 21:18:36 -07001127 struct duplicate_extents_to_file dup_ext_buf;
1128 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1129
1130 /* server fileays advertise duplicate extent support with this flag */
1131 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1132 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1133 return -EOPNOTSUPP;
1134
1135 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1136 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1137 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1138 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1139 dup_ext_buf.ByteCount = cpu_to_le64(len);
1140 cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
1141 src_off, dest_off, len);
1142
1143 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1144 if (rc)
1145 goto duplicate_extents_out;
1146
1147 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1148 trgtfile->fid.volatile_fid,
1149 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001150 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001151 (char *)&dup_ext_buf,
Steve French02b16662015-06-27 21:18:36 -07001152 sizeof(struct duplicate_extents_to_file),
Steve French24df1482016-09-29 04:20:23 -05001153 NULL,
Steve French02b16662015-06-27 21:18:36 -07001154 &ret_data_len);
1155
1156 if (ret_data_len > 0)
1157 cifs_dbg(FYI, "non-zero response length in duplicate extents");
1158
1159duplicate_extents_out:
1160 return rc;
1161}
Steve French02b16662015-06-27 21:18:36 -07001162
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001163static int
Steve French64a5cfa2013-10-14 15:31:32 -05001164smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1165 struct cifsFileInfo *cfile)
1166{
1167 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1168 cfile->fid.volatile_fid);
1169}
1170
1171static int
Steve Frenchb3152e22015-06-24 03:17:02 -05001172smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1173 struct cifsFileInfo *cfile)
1174{
1175 struct fsctl_set_integrity_information_req integr_info;
Steve Frenchb3152e22015-06-24 03:17:02 -05001176 unsigned int ret_data_len;
1177
1178 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1179 integr_info.Flags = 0;
1180 integr_info.Reserved = 0;
1181
1182 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1183 cfile->fid.volatile_fid,
1184 FSCTL_SET_INTEGRITY_INFORMATION,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001185 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001186 (char *)&integr_info,
Steve Frenchb3152e22015-06-24 03:17:02 -05001187 sizeof(struct fsctl_set_integrity_information_req),
Steve French24df1482016-09-29 04:20:23 -05001188 NULL,
Steve Frenchb3152e22015-06-24 03:17:02 -05001189 &ret_data_len);
1190
1191}
1192
1193static int
Steve French834170c2016-09-30 21:14:26 -05001194smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1195 struct cifsFileInfo *cfile, void __user *ioc_buf)
1196{
1197 char *retbuf = NULL;
1198 unsigned int ret_data_len = 0;
1199 int rc;
1200 struct smb_snapshot_array snapshot_in;
1201
1202 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1203 cfile->fid.volatile_fid,
1204 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001205 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001206 NULL, 0 /* no input data */,
Steve French834170c2016-09-30 21:14:26 -05001207 (char **)&retbuf,
1208 &ret_data_len);
1209 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1210 rc, ret_data_len);
1211 if (rc)
1212 return rc;
1213
1214 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1215 /* Fixup buffer */
1216 if (copy_from_user(&snapshot_in, ioc_buf,
1217 sizeof(struct smb_snapshot_array))) {
1218 rc = -EFAULT;
1219 kfree(retbuf);
1220 return rc;
1221 }
1222 if (snapshot_in.snapshot_array_size < sizeof(struct smb_snapshot_array)) {
1223 rc = -ERANGE;
David Disseldorp0e5c7952017-05-03 17:39:09 +02001224 kfree(retbuf);
Steve French834170c2016-09-30 21:14:26 -05001225 return rc;
1226 }
1227
1228 if (ret_data_len > snapshot_in.snapshot_array_size)
1229 ret_data_len = snapshot_in.snapshot_array_size;
1230
1231 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1232 rc = -EFAULT;
1233 }
1234
1235 kfree(retbuf);
1236 return rc;
1237}
1238
1239static int
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001240smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1241 const char *path, struct cifs_sb_info *cifs_sb,
1242 struct cifs_fid *fid, __u16 search_flags,
1243 struct cifs_search_info *srch_inf)
1244{
1245 __le16 *utf16_path;
1246 int rc;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001247 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001248 struct cifs_open_parms oparms;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001249
1250 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1251 if (!utf16_path)
1252 return -ENOMEM;
1253
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001254 oparms.tcon = tcon;
1255 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1256 oparms.disposition = FILE_OPEN;
1257 oparms.create_options = 0;
1258 oparms.fid = fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001259 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001260
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001261 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001262 kfree(utf16_path);
1263 if (rc) {
Pavel Shilovskydcd878382017-06-06 16:58:58 -07001264 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001265 return rc;
1266 }
1267
1268 srch_inf->entries_in_buffer = 0;
1269 srch_inf->index_of_last_entry = 0;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001270
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001271 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1272 fid->volatile_fid, 0, srch_inf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001273 if (rc) {
Pavel Shilovskydcd878382017-06-06 16:58:58 -07001274 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001275 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001276 }
1277 return rc;
1278}
1279
1280static int
1281smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1282 struct cifs_fid *fid, __u16 search_flags,
1283 struct cifs_search_info *srch_inf)
1284{
1285 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1286 fid->volatile_fid, 0, srch_inf);
1287}
1288
1289static int
1290smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1291 struct cifs_fid *fid)
1292{
1293 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1294}
1295
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001296/*
1297* If we negotiate SMB2 protocol and get STATUS_PENDING - update
1298* the number of credits and return true. Otherwise - return false.
1299*/
1300static bool
1301smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
1302{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001303 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001304
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001305 if (shdr->Status != STATUS_PENDING)
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001306 return false;
1307
1308 if (!length) {
1309 spin_lock(&server->req_lock);
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001310 server->credits += le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001311 spin_unlock(&server->req_lock);
1312 wake_up(&server->request_q);
1313 }
1314
1315 return true;
1316}
1317
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001318static bool
1319smb2_is_session_expired(char *buf)
1320{
1321 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
1322
1323 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED)
1324 return false;
1325
1326 cifs_dbg(FYI, "Session expired\n");
1327 return true;
1328}
1329
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001330static int
1331smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
1332 struct cifsInodeInfo *cinode)
1333{
Pavel Shilovsky0822f512012-09-19 06:22:45 -07001334 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
1335 return SMB2_lease_break(0, tcon, cinode->lease_key,
1336 smb2_get_lease_state(cinode));
1337
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001338 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
1339 fid->volatile_fid,
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001340 CIFS_CACHE_READ(cinode) ? 1 : 0);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001341}
1342
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001343static int
1344smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1345 struct kstatfs *buf)
1346{
1347 int rc;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001348 __le16 srch_path = 0; /* Null - open root of share */
1349 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001350 struct cifs_open_parms oparms;
1351 struct cifs_fid fid;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001352
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001353 oparms.tcon = tcon;
1354 oparms.desired_access = FILE_READ_ATTRIBUTES;
1355 oparms.disposition = FILE_OPEN;
1356 oparms.create_options = 0;
1357 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001358 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001359
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001360 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001361 if (rc)
1362 return rc;
1363 buf->f_type = SMB2_MAGIC_NUMBER;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001364 rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1365 buf);
1366 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001367 return rc;
1368}
1369
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07001370static bool
1371smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
1372{
1373 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
1374 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
1375}
1376
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07001377static int
1378smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
1379 __u64 length, __u32 type, int lock, int unlock, bool wait)
1380{
1381 if (unlock && !lock)
1382 type = SMB2_LOCKFLAG_UNLOCK;
1383 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
1384 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
1385 current->tgid, length, offset, type, wait);
1386}
1387
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001388static void
1389smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
1390{
1391 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
1392}
1393
1394static void
1395smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
1396{
1397 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
1398}
1399
1400static void
1401smb2_new_lease_key(struct cifs_fid *fid)
1402{
Steve Frenchfa70b872016-09-22 00:39:34 -05001403 generate_random_uuid(fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001404}
1405
Aurelien Aptel9d496402017-02-13 16:16:49 +01001406static int
1407smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
1408 const char *search_name,
1409 struct dfs_info3_param **target_nodes,
1410 unsigned int *num_of_nodes,
1411 const struct nls_table *nls_codepage, int remap)
1412{
1413 int rc;
1414 __le16 *utf16_path = NULL;
1415 int utf16_path_len = 0;
1416 struct cifs_tcon *tcon;
1417 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
1418 struct get_dfs_referral_rsp *dfs_rsp = NULL;
1419 u32 dfs_req_size = 0, dfs_rsp_size = 0;
1420
1421 cifs_dbg(FYI, "smb2_get_dfs_refer path <%s>\n", search_name);
1422
1423 /*
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001424 * Try to use the IPC tcon, otherwise just use any
Aurelien Aptel9d496402017-02-13 16:16:49 +01001425 */
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001426 tcon = ses->tcon_ipc;
1427 if (tcon == NULL) {
1428 spin_lock(&cifs_tcp_ses_lock);
1429 tcon = list_first_entry_or_null(&ses->tcon_list,
1430 struct cifs_tcon,
1431 tcon_list);
1432 if (tcon)
1433 tcon->tc_count++;
1434 spin_unlock(&cifs_tcp_ses_lock);
1435 }
Aurelien Aptel9d496402017-02-13 16:16:49 +01001436
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001437 if (tcon == NULL) {
Aurelien Aptel9d496402017-02-13 16:16:49 +01001438 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
1439 ses);
1440 rc = -ENOTCONN;
1441 goto out;
1442 }
1443
1444 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
1445 &utf16_path_len,
1446 nls_codepage, remap);
1447 if (!utf16_path) {
1448 rc = -ENOMEM;
1449 goto out;
1450 }
1451
1452 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
1453 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
1454 if (!dfs_req) {
1455 rc = -ENOMEM;
1456 goto out;
1457 }
1458
1459 /* Highest DFS referral version understood */
1460 dfs_req->MaxReferralLevel = DFS_VERSION;
1461
1462 /* Path to resolve in an UTF-16 null-terminated string */
1463 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
1464
1465 do {
Aurelien Aptel9d496402017-02-13 16:16:49 +01001466 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1467 FSCTL_DFS_GET_REFERRALS,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001468 true /* is_fsctl */,
Aurelien Aptel9d496402017-02-13 16:16:49 +01001469 (char *)dfs_req, dfs_req_size,
1470 (char **)&dfs_rsp, &dfs_rsp_size);
Aurelien Aptel9d496402017-02-13 16:16:49 +01001471 } while (rc == -EAGAIN);
1472
1473 if (rc) {
Steve French2564f2f2018-03-21 23:16:36 -05001474 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
Aurelien Aptel57025912017-11-21 14:47:56 +01001475 cifs_dbg(VFS, "ioctl error in smb2_get_dfs_refer rc=%d\n", rc);
Aurelien Aptel9d496402017-02-13 16:16:49 +01001476 goto out;
1477 }
1478
1479 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
1480 num_of_nodes, target_nodes,
1481 nls_codepage, remap, search_name,
1482 true /* is_unicode */);
1483 if (rc) {
1484 cifs_dbg(VFS, "parse error in smb2_get_dfs_refer rc=%d\n", rc);
1485 goto out;
1486 }
1487
1488 out:
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001489 if (tcon && !tcon->ipc) {
1490 /* ipc tcons are not refcounted */
Aurelien Aptel9d496402017-02-13 16:16:49 +01001491 spin_lock(&cifs_tcp_ses_lock);
1492 tcon->tc_count--;
1493 spin_unlock(&cifs_tcp_ses_lock);
1494 }
1495 kfree(utf16_path);
1496 kfree(dfs_req);
1497 kfree(dfs_rsp);
1498 return rc;
1499}
Pavel Shilovsky78932422016-07-24 10:37:38 +03001500#define SMB2_SYMLINK_STRUCT_SIZE \
1501 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
1502
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001503static int
1504smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
1505 const char *full_path, char **target_path,
1506 struct cifs_sb_info *cifs_sb)
1507{
1508 int rc;
1509 __le16 *utf16_path;
1510 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1511 struct cifs_open_parms oparms;
1512 struct cifs_fid fid;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001513 struct kvec err_iov = {NULL, 0};
Gustavo A. R. Silva0d568cd2018-04-13 10:13:29 -05001514 struct smb2_err_rsp *err_buf;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001515 struct smb2_symlink_err_rsp *symlink;
Pavel Shilovsky78932422016-07-24 10:37:38 +03001516 unsigned int sub_len;
1517 unsigned int sub_offset;
1518 unsigned int print_len;
1519 unsigned int print_offset;
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001520 struct cifs_ses *ses = tcon->ses;
1521 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001522
1523 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
1524
1525 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1526 if (!utf16_path)
1527 return -ENOMEM;
1528
1529 oparms.tcon = tcon;
1530 oparms.desired_access = FILE_READ_ATTRIBUTES;
1531 oparms.disposition = FILE_OPEN;
1532 oparms.create_options = 0;
1533 oparms.fid = &fid;
1534 oparms.reconnect = false;
1535
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001536 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_iov);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001537
Gustavo A. R. Silva0d568cd2018-04-13 10:13:29 -05001538 if (!rc || !err_iov.iov_base) {
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001539 kfree(utf16_path);
1540 return -ENOENT;
1541 }
Pavel Shilovsky78932422016-07-24 10:37:38 +03001542
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001543 err_buf = err_iov.iov_base;
Pavel Shilovsky78932422016-07-24 10:37:38 +03001544 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001545 err_iov.iov_len + server->vals->header_preamble_size < SMB2_SYMLINK_STRUCT_SIZE) {
Pavel Shilovsky78932422016-07-24 10:37:38 +03001546 kfree(utf16_path);
1547 return -ENOENT;
1548 }
1549
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001550 /* open must fail on symlink - reset rc */
1551 rc = 0;
1552 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
1553 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
1554 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
Pavel Shilovsky78932422016-07-24 10:37:38 +03001555 print_len = le16_to_cpu(symlink->PrintNameLength);
1556 print_offset = le16_to_cpu(symlink->PrintNameOffset);
1557
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001558 if (err_iov.iov_len + server->vals->header_preamble_size <
Pavel Shilovsky78932422016-07-24 10:37:38 +03001559 SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
1560 kfree(utf16_path);
1561 return -ENOENT;
1562 }
1563
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001564 if (err_iov.iov_len + server->vals->header_preamble_size <
Pavel Shilovsky78932422016-07-24 10:37:38 +03001565 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
1566 kfree(utf16_path);
1567 return -ENOENT;
1568 }
1569
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001570 *target_path = cifs_strndup_from_utf16(
1571 (char *)symlink->PathBuffer + sub_offset,
1572 sub_len, true, cifs_sb->local_nls);
1573 if (!(*target_path)) {
1574 kfree(utf16_path);
1575 return -ENOMEM;
1576 }
1577 convert_delimiter(*target_path, '/');
1578 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
1579 kfree(utf16_path);
1580 return rc;
1581}
1582
Arnd Bergmann84908422017-06-27 17:06:13 +02001583#ifdef CONFIG_CIFS_ACL
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001584static struct cifs_ntsd *
1585get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
1586 const struct cifs_fid *cifsfid, u32 *pacllen)
1587{
1588 struct cifs_ntsd *pntsd = NULL;
1589 unsigned int xid;
1590 int rc = -EOPNOTSUPP;
1591 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1592
1593 if (IS_ERR(tlink))
1594 return ERR_CAST(tlink);
1595
1596 xid = get_xid();
1597 cifs_dbg(FYI, "trying to get acl\n");
1598
1599 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
1600 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
1601 free_xid(xid);
1602
1603 cifs_put_tlink(tlink);
1604
1605 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
1606 if (rc)
1607 return ERR_PTR(rc);
1608 return pntsd;
1609
1610}
1611
1612static struct cifs_ntsd *
1613get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
1614 const char *path, u32 *pacllen)
1615{
1616 struct cifs_ntsd *pntsd = NULL;
1617 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1618 unsigned int xid;
1619 int rc;
1620 struct cifs_tcon *tcon;
1621 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1622 struct cifs_fid fid;
1623 struct cifs_open_parms oparms;
1624 __le16 *utf16_path;
1625
1626 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
1627 if (IS_ERR(tlink))
1628 return ERR_CAST(tlink);
1629
1630 tcon = tlink_tcon(tlink);
1631 xid = get_xid();
1632
1633 if (backup_cred(cifs_sb))
Colin Ian King709340a2017-07-05 13:47:34 +01001634 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001635 else
1636 oparms.create_options = 0;
1637
1638 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1639 if (!utf16_path)
1640 return ERR_PTR(-ENOMEM);
1641
1642 oparms.tcon = tcon;
1643 oparms.desired_access = READ_CONTROL;
1644 oparms.disposition = FILE_OPEN;
1645 oparms.fid = &fid;
1646 oparms.reconnect = false;
1647
1648 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
1649 kfree(utf16_path);
1650 if (!rc) {
1651 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
1652 fid.volatile_fid, (void **)&pntsd, pacllen);
1653 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1654 }
1655
1656 cifs_put_tlink(tlink);
1657 free_xid(xid);
1658
1659 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
1660 if (rc)
1661 return ERR_PTR(rc);
1662 return pntsd;
1663}
1664
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05001665#ifdef CONFIG_CIFS_ACL
1666static int
1667set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
1668 struct inode *inode, const char *path, int aclflag)
1669{
1670 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1671 unsigned int xid;
1672 int rc, access_flags = 0;
1673 struct cifs_tcon *tcon;
1674 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1675 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1676 struct cifs_fid fid;
1677 struct cifs_open_parms oparms;
1678 __le16 *utf16_path;
1679
1680 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
1681 if (IS_ERR(tlink))
1682 return PTR_ERR(tlink);
1683
1684 tcon = tlink_tcon(tlink);
1685 xid = get_xid();
1686
1687 if (backup_cred(cifs_sb))
1688 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1689 else
1690 oparms.create_options = 0;
1691
1692 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
1693 access_flags = WRITE_OWNER;
1694 else
1695 access_flags = WRITE_DAC;
1696
1697 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1698 if (!utf16_path)
1699 return -ENOMEM;
1700
1701 oparms.tcon = tcon;
1702 oparms.desired_access = access_flags;
1703 oparms.disposition = FILE_OPEN;
1704 oparms.path = path;
1705 oparms.fid = &fid;
1706 oparms.reconnect = false;
1707
1708 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
1709 kfree(utf16_path);
1710 if (!rc) {
1711 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
1712 fid.volatile_fid, pnntsd, acllen, aclflag);
1713 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1714 }
1715
1716 cifs_put_tlink(tlink);
1717 free_xid(xid);
1718 return rc;
1719}
1720#endif /* CIFS_ACL */
1721
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001722/* Retrieve an ACL from the server */
1723static struct cifs_ntsd *
1724get_smb2_acl(struct cifs_sb_info *cifs_sb,
1725 struct inode *inode, const char *path,
1726 u32 *pacllen)
1727{
1728 struct cifs_ntsd *pntsd = NULL;
1729 struct cifsFileInfo *open_file = NULL;
1730
1731 if (inode)
1732 open_file = find_readable_file(CIFS_I(inode), true);
1733 if (!open_file)
1734 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
1735
1736 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
1737 cifsFileInfo_put(open_file);
1738 return pntsd;
1739}
Arnd Bergmann84908422017-06-27 17:06:13 +02001740#endif
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001741
Steve French30175622014-08-17 18:16:40 -05001742static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
1743 loff_t offset, loff_t len, bool keep_size)
1744{
1745 struct inode *inode;
1746 struct cifsInodeInfo *cifsi;
1747 struct cifsFileInfo *cfile = file->private_data;
1748 struct file_zero_data_information fsctl_buf;
1749 long rc;
1750 unsigned int xid;
1751
1752 xid = get_xid();
1753
David Howells2b0143b2015-03-17 22:25:59 +00001754 inode = d_inode(cfile->dentry);
Steve French30175622014-08-17 18:16:40 -05001755 cifsi = CIFS_I(inode);
1756
1757 /* if file not oplocked can't be sure whether asking to extend size */
1758 if (!CIFS_CACHE_READ(cifsi))
1759 if (keep_size == false)
1760 return -EOPNOTSUPP;
1761
Steve French2bb93d22014-08-20 18:56:29 -05001762 /*
Steve French30175622014-08-17 18:16:40 -05001763 * Must check if file sparse since fallocate -z (zero range) assumes
1764 * non-sparse allocation
1765 */
1766 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE))
1767 return -EOPNOTSUPP;
1768
1769 /*
1770 * need to make sure we are not asked to extend the file since the SMB3
1771 * fsctl does not change the file size. In the future we could change
1772 * this to zero the first part of the range then set the file size
1773 * which for a non sparse file would zero the newly extended range
1774 */
1775 if (keep_size == false)
1776 if (i_size_read(inode) < offset + len)
1777 return -EOPNOTSUPP;
1778
1779 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1780
1781 fsctl_buf.FileOffset = cpu_to_le64(offset);
1782 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1783
1784 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1785 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001786 true /* is_fctl */, (char *)&fsctl_buf,
Steve French30175622014-08-17 18:16:40 -05001787 sizeof(struct file_zero_data_information), NULL, NULL);
1788 free_xid(xid);
1789 return rc;
1790}
1791
Steve French31742c52014-08-17 08:38:47 -05001792static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
1793 loff_t offset, loff_t len)
1794{
1795 struct inode *inode;
1796 struct cifsInodeInfo *cifsi;
1797 struct cifsFileInfo *cfile = file->private_data;
1798 struct file_zero_data_information fsctl_buf;
1799 long rc;
1800 unsigned int xid;
1801 __u8 set_sparse = 1;
1802
1803 xid = get_xid();
1804
David Howells2b0143b2015-03-17 22:25:59 +00001805 inode = d_inode(cfile->dentry);
Steve French31742c52014-08-17 08:38:47 -05001806 cifsi = CIFS_I(inode);
1807
1808 /* Need to make file sparse, if not already, before freeing range. */
1809 /* Consider adding equivalent for compressed since it could also work */
1810 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse))
1811 return -EOPNOTSUPP;
1812
1813 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1814
1815 fsctl_buf.FileOffset = cpu_to_le64(offset);
1816 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1817
1818 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1819 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001820 true /* is_fctl */, (char *)&fsctl_buf,
Steve French31742c52014-08-17 08:38:47 -05001821 sizeof(struct file_zero_data_information), NULL, NULL);
1822 free_xid(xid);
1823 return rc;
1824}
1825
Steve French9ccf3212014-10-18 17:01:15 -05001826static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
1827 loff_t off, loff_t len, bool keep_size)
1828{
1829 struct inode *inode;
1830 struct cifsInodeInfo *cifsi;
1831 struct cifsFileInfo *cfile = file->private_data;
1832 long rc = -EOPNOTSUPP;
1833 unsigned int xid;
1834
1835 xid = get_xid();
1836
David Howells2b0143b2015-03-17 22:25:59 +00001837 inode = d_inode(cfile->dentry);
Steve French9ccf3212014-10-18 17:01:15 -05001838 cifsi = CIFS_I(inode);
1839
1840 /* if file not oplocked can't be sure whether asking to extend size */
1841 if (!CIFS_CACHE_READ(cifsi))
1842 if (keep_size == false)
1843 return -EOPNOTSUPP;
1844
1845 /*
1846 * Files are non-sparse by default so falloc may be a no-op
1847 * Must check if file sparse. If not sparse, and not extending
1848 * then no need to do anything since file already allocated
1849 */
1850 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
1851 if (keep_size == true)
1852 return 0;
1853 /* check if extending file */
1854 else if (i_size_read(inode) >= off + len)
1855 /* not extending file and already not sparse */
1856 return 0;
1857 /* BB: in future add else clause to extend file */
1858 else
1859 return -EOPNOTSUPP;
1860 }
1861
1862 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
1863 /*
1864 * Check if falloc starts within first few pages of file
1865 * and ends within a few pages of the end of file to
1866 * ensure that most of file is being forced to be
1867 * fallocated now. If so then setting whole file sparse
1868 * ie potentially making a few extra pages at the beginning
1869 * or end of the file non-sparse via set_sparse is harmless.
1870 */
1871 if ((off > 8192) || (off + len + 8192 < i_size_read(inode)))
1872 return -EOPNOTSUPP;
1873
1874 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
1875 }
1876 /* BB: else ... in future add code to extend file and set sparse */
1877
1878
1879 free_xid(xid);
1880 return rc;
1881}
1882
1883
Steve French31742c52014-08-17 08:38:47 -05001884static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
1885 loff_t off, loff_t len)
1886{
1887 /* KEEP_SIZE already checked for by do_fallocate */
1888 if (mode & FALLOC_FL_PUNCH_HOLE)
1889 return smb3_punch_hole(file, tcon, off, len);
Steve French30175622014-08-17 18:16:40 -05001890 else if (mode & FALLOC_FL_ZERO_RANGE) {
1891 if (mode & FALLOC_FL_KEEP_SIZE)
1892 return smb3_zero_range(file, tcon, off, len, true);
1893 return smb3_zero_range(file, tcon, off, len, false);
Steve French9ccf3212014-10-18 17:01:15 -05001894 } else if (mode == FALLOC_FL_KEEP_SIZE)
1895 return smb3_simple_falloc(file, tcon, off, len, true);
1896 else if (mode == 0)
1897 return smb3_simple_falloc(file, tcon, off, len, false);
Steve French31742c52014-08-17 08:38:47 -05001898
1899 return -EOPNOTSUPP;
1900}
1901
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001902static void
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00001903smb2_downgrade_oplock(struct TCP_Server_Info *server,
1904 struct cifsInodeInfo *cinode, bool set_level2)
1905{
1906 if (set_level2)
1907 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
1908 0, NULL);
1909 else
1910 server->ops->set_oplock_level(cinode, 0, 0, NULL);
1911}
1912
1913static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001914smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1915 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001916{
1917 oplock &= 0xFF;
1918 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1919 return;
1920 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001921 cinode->oplock = CIFS_CACHE_RHW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001922 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
1923 &cinode->vfs_inode);
1924 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001925 cinode->oplock = CIFS_CACHE_RW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001926 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
1927 &cinode->vfs_inode);
1928 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
1929 cinode->oplock = CIFS_CACHE_READ_FLG;
1930 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
1931 &cinode->vfs_inode);
1932 } else
1933 cinode->oplock = 0;
1934}
1935
1936static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001937smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1938 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001939{
1940 char message[5] = {0};
1941
1942 oplock &= 0xFF;
1943 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1944 return;
1945
1946 cinode->oplock = 0;
1947 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
1948 cinode->oplock |= CIFS_CACHE_READ_FLG;
1949 strcat(message, "R");
1950 }
1951 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
1952 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
1953 strcat(message, "H");
1954 }
1955 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
1956 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
1957 strcat(message, "W");
1958 }
1959 if (!cinode->oplock)
1960 strcat(message, "None");
1961 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
1962 &cinode->vfs_inode);
1963}
1964
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001965static void
1966smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1967 unsigned int epoch, bool *purge_cache)
1968{
1969 unsigned int old_oplock = cinode->oplock;
1970
1971 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
1972
1973 if (purge_cache) {
1974 *purge_cache = false;
1975 if (old_oplock == CIFS_CACHE_READ_FLG) {
1976 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
1977 (epoch - cinode->epoch > 0))
1978 *purge_cache = true;
1979 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1980 (epoch - cinode->epoch > 1))
1981 *purge_cache = true;
1982 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1983 (epoch - cinode->epoch > 1))
1984 *purge_cache = true;
1985 else if (cinode->oplock == 0 &&
1986 (epoch - cinode->epoch > 0))
1987 *purge_cache = true;
1988 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
1989 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1990 (epoch - cinode->epoch > 0))
1991 *purge_cache = true;
1992 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1993 (epoch - cinode->epoch > 1))
1994 *purge_cache = true;
1995 }
1996 cinode->epoch = epoch;
1997 }
1998}
1999
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002000static bool
2001smb2_is_read_op(__u32 oplock)
2002{
2003 return oplock == SMB2_OPLOCK_LEVEL_II;
2004}
2005
2006static bool
2007smb21_is_read_op(__u32 oplock)
2008{
2009 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
2010 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
2011}
2012
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002013static __le32
2014map_oplock_to_lease(u8 oplock)
2015{
2016 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
2017 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
2018 else if (oplock == SMB2_OPLOCK_LEVEL_II)
2019 return SMB2_LEASE_READ_CACHING;
2020 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
2021 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
2022 SMB2_LEASE_WRITE_CACHING;
2023 return 0;
2024}
2025
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002026static char *
2027smb2_create_lease_buf(u8 *lease_key, u8 oplock)
2028{
2029 struct create_lease *buf;
2030
2031 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
2032 if (!buf)
2033 return NULL;
2034
2035 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
2036 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002037 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002038
2039 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2040 (struct create_lease, lcontext));
2041 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
2042 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2043 (struct create_lease, Name));
2044 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07002045 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002046 buf->Name[0] = 'R';
2047 buf->Name[1] = 'q';
2048 buf->Name[2] = 'L';
2049 buf->Name[3] = 's';
2050 return (char *)buf;
2051}
2052
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002053static char *
2054smb3_create_lease_buf(u8 *lease_key, u8 oplock)
2055{
2056 struct create_lease_v2 *buf;
2057
2058 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
2059 if (!buf)
2060 return NULL;
2061
2062 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
2063 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
2064 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2065
2066 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2067 (struct create_lease_v2, lcontext));
2068 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
2069 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2070 (struct create_lease_v2, Name));
2071 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07002072 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002073 buf->Name[0] = 'R';
2074 buf->Name[1] = 'q';
2075 buf->Name[2] = 'L';
2076 buf->Name[3] = 's';
2077 return (char *)buf;
2078}
2079
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002080static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002081smb2_parse_lease_buf(void *buf, unsigned int *epoch)
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002082{
2083 struct create_lease *lc = (struct create_lease *)buf;
2084
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002085 *epoch = 0; /* not used */
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002086 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2087 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2088 return le32_to_cpu(lc->lcontext.LeaseState);
2089}
2090
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002091static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002092smb3_parse_lease_buf(void *buf, unsigned int *epoch)
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002093{
2094 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
2095
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002096 *epoch = le16_to_cpu(lc->lcontext.Epoch);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002097 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2098 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2099 return le32_to_cpu(lc->lcontext.LeaseState);
2100}
2101
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002102static unsigned int
2103smb2_wp_retry_size(struct inode *inode)
2104{
2105 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
2106 SMB2_MAX_BUFFER_SIZE);
2107}
2108
Pavel Shilovsky52755802014-08-18 20:49:57 +04002109static bool
2110smb2_dir_needs_close(struct cifsFileInfo *cfile)
2111{
2112 return !cfile->invalidHandle;
2113}
2114
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002115static void
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002116fill_transform_hdr(struct TCP_Server_Info *server,
2117 struct smb2_transform_hdr *tr_hdr, struct smb_rqst *old_rq)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002118{
2119 struct smb2_sync_hdr *shdr =
2120 (struct smb2_sync_hdr *)old_rq->rq_iov[1].iov_base;
2121 unsigned int orig_len = get_rfc1002_length(old_rq->rq_iov[0].iov_base);
2122
2123 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
2124 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
2125 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
2126 tr_hdr->Flags = cpu_to_le16(0x01);
2127 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2128 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002129 inc_rfc1001_len(tr_hdr, sizeof(struct smb2_transform_hdr) - server->vals->header_preamble_size);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002130 inc_rfc1001_len(tr_hdr, orig_len);
2131}
2132
Ronnie Sahlberg262916b2018-02-20 12:45:21 +11002133/* We can not use the normal sg_set_buf() as we will sometimes pass a
2134 * stack object as buf.
2135 */
2136static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
2137 unsigned int buflen)
2138{
2139 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
2140}
2141
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002142static struct scatterlist *
2143init_sg(struct smb_rqst *rqst, u8 *sign)
2144{
2145 unsigned int sg_len = rqst->rq_nvec + rqst->rq_npages + 1;
2146 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 24;
2147 struct scatterlist *sg;
2148 unsigned int i;
2149 unsigned int j;
2150
2151 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
2152 if (!sg)
2153 return NULL;
2154
2155 sg_init_table(sg, sg_len);
Ronnie Sahlberg262916b2018-02-20 12:45:21 +11002156 smb2_sg_set_buf(&sg[0], rqst->rq_iov[0].iov_base + 24, assoc_data_len);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002157 for (i = 1; i < rqst->rq_nvec; i++)
Ronnie Sahlberg262916b2018-02-20 12:45:21 +11002158 smb2_sg_set_buf(&sg[i], rqst->rq_iov[i].iov_base,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002159 rqst->rq_iov[i].iov_len);
2160 for (j = 0; i < sg_len - 1; i++, j++) {
2161 unsigned int len = (j < rqst->rq_npages - 1) ? rqst->rq_pagesz
2162 : rqst->rq_tailsz;
2163 sg_set_page(&sg[i], rqst->rq_pages[j], len, 0);
2164 }
Ronnie Sahlberg262916b2018-02-20 12:45:21 +11002165 smb2_sg_set_buf(&sg[sg_len - 1], sign, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002166 return sg;
2167}
2168
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002169static int
2170smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
2171{
2172 struct cifs_ses *ses;
2173 u8 *ses_enc_key;
2174
2175 spin_lock(&cifs_tcp_ses_lock);
2176 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2177 if (ses->Suid != ses_id)
2178 continue;
2179 ses_enc_key = enc ? ses->smb3encryptionkey :
2180 ses->smb3decryptionkey;
2181 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
2182 spin_unlock(&cifs_tcp_ses_lock);
2183 return 0;
2184 }
2185 spin_unlock(&cifs_tcp_ses_lock);
2186
2187 return 1;
2188}
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002189/*
2190 * Encrypt or decrypt @rqst message. @rqst has the following format:
2191 * iov[0] - transform header (associate data),
2192 * iov[1-N] and pages - data to encrypt.
2193 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
2194 * untouched.
2195 */
2196static int
2197crypt_message(struct TCP_Server_Info *server, struct smb_rqst *rqst, int enc)
2198{
2199 struct smb2_transform_hdr *tr_hdr =
2200 (struct smb2_transform_hdr *)rqst->rq_iov[0].iov_base;
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002201 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20 - server->vals->header_preamble_size;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002202 int rc = 0;
2203 struct scatterlist *sg;
2204 u8 sign[SMB2_SIGNATURE_SIZE] = {};
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002205 u8 key[SMB3_SIGN_KEY_SIZE];
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002206 struct aead_request *req;
2207 char *iv;
2208 unsigned int iv_len;
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01002209 DECLARE_CRYPTO_WAIT(wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002210 struct crypto_aead *tfm;
2211 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2212
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002213 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
2214 if (rc) {
2215 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
2216 enc ? "en" : "de");
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002217 return 0;
2218 }
2219
2220 rc = smb3_crypto_aead_allocate(server);
2221 if (rc) {
2222 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
2223 return rc;
2224 }
2225
2226 tfm = enc ? server->secmech.ccmaesencrypt :
2227 server->secmech.ccmaesdecrypt;
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002228 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002229 if (rc) {
2230 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
2231 return rc;
2232 }
2233
2234 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
2235 if (rc) {
2236 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
2237 return rc;
2238 }
2239
2240 req = aead_request_alloc(tfm, GFP_KERNEL);
2241 if (!req) {
2242 cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
2243 return -ENOMEM;
2244 }
2245
2246 if (!enc) {
2247 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
2248 crypt_len += SMB2_SIGNATURE_SIZE;
2249 }
2250
2251 sg = init_sg(rqst, sign);
2252 if (!sg) {
Christophe Jaillet517a6e42017-06-11 09:12:47 +02002253 cifs_dbg(VFS, "%s: Failed to init sg", __func__);
2254 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002255 goto free_req;
2256 }
2257
2258 iv_len = crypto_aead_ivsize(tfm);
2259 iv = kzalloc(iv_len, GFP_KERNEL);
2260 if (!iv) {
2261 cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
Christophe Jaillet517a6e42017-06-11 09:12:47 +02002262 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002263 goto free_sg;
2264 }
2265 iv[0] = 3;
2266 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2267
2268 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
2269 aead_request_set_ad(req, assoc_data_len);
2270
2271 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01002272 crypto_req_done, &wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002273
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01002274 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
2275 : crypto_aead_decrypt(req), &wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002276
2277 if (!rc && enc)
2278 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
2279
2280 kfree(iv);
2281free_sg:
2282 kfree(sg);
2283free_req:
2284 kfree(req);
2285 return rc;
2286}
2287
2288static int
2289smb3_init_transform_rq(struct TCP_Server_Info *server, struct smb_rqst *new_rq,
2290 struct smb_rqst *old_rq)
2291{
2292 struct kvec *iov;
2293 struct page **pages;
2294 struct smb2_transform_hdr *tr_hdr;
2295 unsigned int npages = old_rq->rq_npages;
2296 int i;
2297 int rc = -ENOMEM;
2298
2299 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2300 if (!pages)
2301 return rc;
2302
2303 new_rq->rq_pages = pages;
2304 new_rq->rq_npages = old_rq->rq_npages;
2305 new_rq->rq_pagesz = old_rq->rq_pagesz;
2306 new_rq->rq_tailsz = old_rq->rq_tailsz;
2307
2308 for (i = 0; i < npages; i++) {
2309 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2310 if (!pages[i])
2311 goto err_free_pages;
2312 }
2313
2314 iov = kmalloc_array(old_rq->rq_nvec, sizeof(struct kvec), GFP_KERNEL);
2315 if (!iov)
2316 goto err_free_pages;
2317
2318 /* copy all iovs from the old except the 1st one (rfc1002 length) */
2319 memcpy(&iov[1], &old_rq->rq_iov[1],
2320 sizeof(struct kvec) * (old_rq->rq_nvec - 1));
2321 new_rq->rq_iov = iov;
2322 new_rq->rq_nvec = old_rq->rq_nvec;
2323
2324 tr_hdr = kmalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL);
2325 if (!tr_hdr)
2326 goto err_free_iov;
2327
2328 /* fill the 1st iov with a transform header */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002329 fill_transform_hdr(server, tr_hdr, old_rq);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002330 new_rq->rq_iov[0].iov_base = tr_hdr;
2331 new_rq->rq_iov[0].iov_len = sizeof(struct smb2_transform_hdr);
2332
2333 /* copy pages form the old */
2334 for (i = 0; i < npages; i++) {
2335 char *dst = kmap(new_rq->rq_pages[i]);
2336 char *src = kmap(old_rq->rq_pages[i]);
2337 unsigned int len = (i < npages - 1) ? new_rq->rq_pagesz :
2338 new_rq->rq_tailsz;
2339 memcpy(dst, src, len);
2340 kunmap(new_rq->rq_pages[i]);
2341 kunmap(old_rq->rq_pages[i]);
2342 }
2343
2344 rc = crypt_message(server, new_rq, 1);
2345 cifs_dbg(FYI, "encrypt message returned %d", rc);
2346 if (rc)
2347 goto err_free_tr_hdr;
2348
2349 return rc;
2350
2351err_free_tr_hdr:
2352 kfree(tr_hdr);
2353err_free_iov:
2354 kfree(iov);
2355err_free_pages:
2356 for (i = i - 1; i >= 0; i--)
2357 put_page(pages[i]);
2358 kfree(pages);
2359 return rc;
2360}
2361
2362static void
2363smb3_free_transform_rq(struct smb_rqst *rqst)
2364{
2365 int i = rqst->rq_npages - 1;
2366
2367 for (; i >= 0; i--)
2368 put_page(rqst->rq_pages[i]);
2369 kfree(rqst->rq_pages);
2370 /* free transform header */
2371 kfree(rqst->rq_iov[0].iov_base);
2372 kfree(rqst->rq_iov);
2373}
2374
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002375static int
2376smb3_is_transform_hdr(void *buf)
2377{
2378 struct smb2_transform_hdr *trhdr = buf;
2379
2380 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
2381}
2382
2383static int
2384decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
2385 unsigned int buf_data_size, struct page **pages,
2386 unsigned int npages, unsigned int page_data_size)
2387{
2388 struct kvec iov[2];
2389 struct smb_rqst rqst = {NULL};
2390 struct smb2_hdr *hdr;
2391 int rc;
2392
2393 iov[0].iov_base = buf;
2394 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
2395 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
2396 iov[1].iov_len = buf_data_size;
2397
2398 rqst.rq_iov = iov;
2399 rqst.rq_nvec = 2;
2400 rqst.rq_pages = pages;
2401 rqst.rq_npages = npages;
2402 rqst.rq_pagesz = PAGE_SIZE;
2403 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
2404
2405 rc = crypt_message(server, &rqst, 0);
2406 cifs_dbg(FYI, "decrypt message returned %d\n", rc);
2407
2408 if (rc)
2409 return rc;
2410
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002411 memmove(buf + server->vals->header_preamble_size, iov[1].iov_base, buf_data_size);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002412 hdr = (struct smb2_hdr *)buf;
2413 hdr->smb2_buf_length = cpu_to_be32(buf_data_size + page_data_size);
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002414 server->total_read = buf_data_size + page_data_size + server->vals->header_preamble_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002415
2416 return rc;
2417}
2418
2419static int
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002420read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
2421 unsigned int npages, unsigned int len)
2422{
2423 int i;
2424 int length;
2425
2426 for (i = 0; i < npages; i++) {
2427 struct page *page = pages[i];
2428 size_t n;
2429
2430 n = len;
2431 if (len >= PAGE_SIZE) {
2432 /* enough data to fill the page */
2433 n = PAGE_SIZE;
2434 len -= n;
2435 } else {
2436 zero_user(page, len, PAGE_SIZE - len);
2437 len = 0;
2438 }
2439 length = cifs_read_page_from_socket(server, page, n);
2440 if (length < 0)
2441 return length;
2442 server->total_read += length;
2443 }
2444
2445 return 0;
2446}
2447
2448static int
2449init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
2450 unsigned int cur_off, struct bio_vec **page_vec)
2451{
2452 struct bio_vec *bvec;
2453 int i;
2454
2455 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
2456 if (!bvec)
2457 return -ENOMEM;
2458
2459 for (i = 0; i < npages; i++) {
2460 bvec[i].bv_page = pages[i];
2461 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
2462 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
2463 data_size -= bvec[i].bv_len;
2464 }
2465
2466 if (data_size != 0) {
2467 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
2468 kfree(bvec);
2469 return -EIO;
2470 }
2471
2472 *page_vec = bvec;
2473 return 0;
2474}
2475
2476static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002477handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
2478 char *buf, unsigned int buf_len, struct page **pages,
2479 unsigned int npages, unsigned int page_data_size)
2480{
2481 unsigned int data_offset;
2482 unsigned int data_len;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002483 unsigned int cur_off;
2484 unsigned int cur_page_idx;
2485 unsigned int pad_len;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002486 struct cifs_readdata *rdata = mid->callback_data;
2487 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
2488 struct bio_vec *bvec = NULL;
2489 struct iov_iter iter;
2490 struct kvec iov;
2491 int length;
Long Li74dcf412017-11-22 17:38:46 -07002492 bool use_rdma_mr = false;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002493
2494 if (shdr->Command != SMB2_READ) {
2495 cifs_dbg(VFS, "only big read responses are supported\n");
2496 return -ENOTSUPP;
2497 }
2498
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002499 if (server->ops->is_session_expired &&
2500 server->ops->is_session_expired(buf)) {
2501 cifs_reconnect(server);
2502 wake_up(&server->response_q);
2503 return -1;
2504 }
2505
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002506 if (server->ops->is_status_pending &&
2507 server->ops->is_status_pending(buf, server, 0))
2508 return -1;
2509
2510 rdata->result = server->ops->map_error(buf, false);
2511 if (rdata->result != 0) {
2512 cifs_dbg(FYI, "%s: server returned error %d\n",
2513 __func__, rdata->result);
2514 dequeue_mid(mid, rdata->result);
2515 return 0;
2516 }
2517
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002518 data_offset = server->ops->read_data_offset(buf) + server->vals->header_preamble_size;
Long Li74dcf412017-11-22 17:38:46 -07002519#ifdef CONFIG_CIFS_SMB_DIRECT
2520 use_rdma_mr = rdata->mr;
2521#endif
2522 data_len = server->ops->read_data_length(buf, use_rdma_mr);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002523
2524 if (data_offset < server->vals->read_rsp_size) {
2525 /*
2526 * win2k8 sometimes sends an offset of 0 when the read
2527 * is beyond the EOF. Treat it as if the data starts just after
2528 * the header.
2529 */
2530 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
2531 __func__, data_offset);
2532 data_offset = server->vals->read_rsp_size;
2533 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
2534 /* data_offset is beyond the end of smallbuf */
2535 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
2536 __func__, data_offset);
2537 rdata->result = -EIO;
2538 dequeue_mid(mid, rdata->result);
2539 return 0;
2540 }
2541
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002542 pad_len = data_offset - server->vals->read_rsp_size;
2543
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002544 if (buf_len <= data_offset) {
2545 /* read response payload is in pages */
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002546 cur_page_idx = pad_len / PAGE_SIZE;
2547 cur_off = pad_len % PAGE_SIZE;
2548
2549 if (cur_page_idx != 0) {
2550 /* data offset is beyond the 1st page of response */
2551 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
2552 __func__, data_offset);
2553 rdata->result = -EIO;
2554 dequeue_mid(mid, rdata->result);
2555 return 0;
2556 }
2557
2558 if (data_len > page_data_size - pad_len) {
2559 /* data_len is corrupt -- discard frame */
2560 rdata->result = -EIO;
2561 dequeue_mid(mid, rdata->result);
2562 return 0;
2563 }
2564
2565 rdata->result = init_read_bvec(pages, npages, page_data_size,
2566 cur_off, &bvec);
2567 if (rdata->result != 0) {
2568 dequeue_mid(mid, rdata->result);
2569 return 0;
2570 }
2571
2572 iov_iter_bvec(&iter, WRITE | ITER_BVEC, bvec, npages, data_len);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002573 } else if (buf_len >= data_offset + data_len) {
2574 /* read response payload is in buf */
2575 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
2576 iov.iov_base = buf + data_offset;
2577 iov.iov_len = data_len;
2578 iov_iter_kvec(&iter, WRITE | ITER_KVEC, &iov, 1, data_len);
2579 } else {
2580 /* read response payload cannot be in both buf and pages */
2581 WARN_ONCE(1, "buf can not contain only a part of read data");
2582 rdata->result = -EIO;
2583 dequeue_mid(mid, rdata->result);
2584 return 0;
2585 }
2586
2587 /* set up first iov for signature check */
2588 rdata->iov[0].iov_base = buf;
2589 rdata->iov[0].iov_len = 4;
2590 rdata->iov[1].iov_base = buf + 4;
2591 rdata->iov[1].iov_len = server->vals->read_rsp_size - 4;
2592 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
2593 rdata->iov[0].iov_base, server->vals->read_rsp_size);
2594
2595 length = rdata->copy_into_pages(server, rdata, &iter);
2596
2597 kfree(bvec);
2598
2599 if (length < 0)
2600 return length;
2601
2602 dequeue_mid(mid, false);
2603 return length;
2604}
2605
2606static int
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002607receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2608{
2609 char *buf = server->smallbuf;
2610 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2611 unsigned int npages;
2612 struct page **pages;
2613 unsigned int len;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10002614 unsigned int buflen = server->pdu_size + server->vals->header_preamble_size;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002615 int rc;
2616 int i = 0;
2617
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002618 len = min_t(unsigned int, buflen, server->vals->read_rsp_size -
2619 server->vals->header_preamble_size +
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002620 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
2621
2622 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
2623 if (rc < 0)
2624 return rc;
2625 server->total_read += rc;
2626
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002627 len = le32_to_cpu(tr_hdr->OriginalMessageSize) +
2628 server->vals->header_preamble_size -
2629 server->vals->read_rsp_size;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002630 npages = DIV_ROUND_UP(len, PAGE_SIZE);
2631
2632 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2633 if (!pages) {
2634 rc = -ENOMEM;
2635 goto discard_data;
2636 }
2637
2638 for (; i < npages; i++) {
2639 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2640 if (!pages[i]) {
2641 rc = -ENOMEM;
2642 goto discard_data;
2643 }
2644 }
2645
2646 /* read read data into pages */
2647 rc = read_data_into_pages(server, pages, npages, len);
2648 if (rc)
2649 goto free_pages;
2650
Pavel Shilovsky350be252017-04-10 10:31:33 -07002651 rc = cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002652 if (rc)
2653 goto free_pages;
2654
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002655 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size -
2656 server->vals->header_preamble_size,
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002657 pages, npages, len);
2658 if (rc)
2659 goto free_pages;
2660
2661 *mid = smb2_find_mid(server, buf);
2662 if (*mid == NULL)
2663 cifs_dbg(FYI, "mid not found\n");
2664 else {
2665 cifs_dbg(FYI, "mid found\n");
2666 (*mid)->decrypted = true;
2667 rc = handle_read_data(server, *mid, buf,
2668 server->vals->read_rsp_size,
2669 pages, npages, len);
2670 }
2671
2672free_pages:
2673 for (i = i - 1; i >= 0; i--)
2674 put_page(pages[i]);
2675 kfree(pages);
2676 return rc;
2677discard_data:
Pavel Shilovsky350be252017-04-10 10:31:33 -07002678 cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002679 goto free_pages;
2680}
2681
2682static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002683receive_encrypted_standard(struct TCP_Server_Info *server,
2684 struct mid_q_entry **mid)
2685{
2686 int length;
2687 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10002688 unsigned int pdu_length = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002689 unsigned int buf_size;
2690 struct mid_q_entry *mid_entry;
2691
2692 /* switch to large buffer if too big for a small one */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002693 if (pdu_length + server->vals->header_preamble_size > MAX_CIFS_SMALL_BUFFER_SIZE) {
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002694 server->large_buf = true;
2695 memcpy(server->bigbuf, buf, server->total_read);
2696 buf = server->bigbuf;
2697 }
2698
2699 /* now read the rest */
2700 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002701 pdu_length - HEADER_SIZE(server) + 1 +
2702 server->vals->header_preamble_size);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002703 if (length < 0)
2704 return length;
2705 server->total_read += length;
2706
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002707 buf_size = pdu_length + server->vals->header_preamble_size - sizeof(struct smb2_transform_hdr);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002708 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
2709 if (length)
2710 return length;
2711
2712 mid_entry = smb2_find_mid(server, buf);
2713 if (mid_entry == NULL)
2714 cifs_dbg(FYI, "mid not found\n");
2715 else {
2716 cifs_dbg(FYI, "mid found\n");
2717 mid_entry->decrypted = true;
2718 }
2719
2720 *mid = mid_entry;
2721
2722 if (mid_entry && mid_entry->handle)
2723 return mid_entry->handle(server, mid_entry);
2724
2725 return cifs_handle_standard(server, mid_entry);
2726}
2727
2728static int
2729smb3_receive_transform(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2730{
2731 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10002732 unsigned int pdu_length = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002733 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2734 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2735
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002736 if (pdu_length + server->vals->header_preamble_size < sizeof(struct smb2_transform_hdr) +
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002737 sizeof(struct smb2_sync_hdr)) {
2738 cifs_dbg(VFS, "Transform message is too small (%u)\n",
2739 pdu_length);
2740 cifs_reconnect(server);
2741 wake_up(&server->response_q);
2742 return -ECONNABORTED;
2743 }
2744
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002745 if (pdu_length + server->vals->header_preamble_size < orig_len + sizeof(struct smb2_transform_hdr)) {
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002746 cifs_dbg(VFS, "Transform message is broken\n");
2747 cifs_reconnect(server);
2748 wake_up(&server->response_q);
2749 return -ECONNABORTED;
2750 }
2751
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002752 if (pdu_length + server->vals->header_preamble_size > CIFSMaxBufSize + MAX_HEADER_SIZE(server))
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002753 return receive_encrypted_read(server, mid);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002754
2755 return receive_encrypted_standard(server, mid);
2756}
2757
2758int
2759smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
2760{
2761 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
2762
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10002763 return handle_read_data(server, mid, buf, server->pdu_size +
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002764 server->vals->header_preamble_size,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002765 NULL, 0, 0);
2766}
2767
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002768struct smb_version_operations smb20_operations = {
2769 .compare_fids = smb2_compare_fids,
2770 .setup_request = smb2_setup_request,
2771 .setup_async_request = smb2_setup_async_request,
2772 .check_receive = smb2_check_receive,
2773 .add_credits = smb2_add_credits,
2774 .set_credits = smb2_set_credits,
2775 .get_credits_field = smb2_get_credits_field,
2776 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002777 .wait_mtu_credits = cifs_wait_mtu_credits,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002778 .get_next_mid = smb2_get_next_mid,
2779 .read_data_offset = smb2_read_data_offset,
2780 .read_data_length = smb2_read_data_length,
2781 .map_error = map_smb2_to_linux_error,
2782 .find_mid = smb2_find_mid,
2783 .check_message = smb2_check_message,
2784 .dump_detail = smb2_dump_detail,
2785 .clear_stats = smb2_clear_stats,
2786 .print_stats = smb2_print_stats,
2787 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08002788 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002789 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002790 .need_neg = smb2_need_neg,
2791 .negotiate = smb2_negotiate,
2792 .negotiate_wsize = smb2_negotiate_wsize,
2793 .negotiate_rsize = smb2_negotiate_rsize,
2794 .sess_setup = SMB2_sess_setup,
2795 .logoff = SMB2_logoff,
2796 .tree_connect = SMB2_tcon,
2797 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05002798 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002799 .is_path_accessible = smb2_is_path_accessible,
2800 .can_echo = smb2_can_echo,
2801 .echo = SMB2_echo,
2802 .query_path_info = smb2_query_path_info,
2803 .get_srv_inum = smb2_get_srv_inum,
2804 .query_file_info = smb2_query_file_info,
2805 .set_path_size = smb2_set_path_size,
2806 .set_file_size = smb2_set_file_size,
2807 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05002808 .set_compression = smb2_set_compression,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002809 .mkdir = smb2_mkdir,
2810 .mkdir_setinfo = smb2_mkdir_setinfo,
2811 .rmdir = smb2_rmdir,
2812 .unlink = smb2_unlink,
2813 .rename = smb2_rename_path,
2814 .create_hardlink = smb2_create_hardlink,
2815 .query_symlink = smb2_query_symlink,
Sachin Prabhu5b23c972016-07-11 16:53:20 +01002816 .query_mf_symlink = smb3_query_mf_symlink,
2817 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002818 .open = smb2_open_file,
2819 .set_fid = smb2_set_fid,
2820 .close = smb2_close_file,
2821 .flush = smb2_flush_file,
2822 .async_readv = smb2_async_readv,
2823 .async_writev = smb2_async_writev,
2824 .sync_read = smb2_sync_read,
2825 .sync_write = smb2_sync_write,
2826 .query_dir_first = smb2_query_dir_first,
2827 .query_dir_next = smb2_query_dir_next,
2828 .close_dir = smb2_close_dir,
2829 .calc_smb_size = smb2_calc_size,
2830 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002831 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002832 .oplock_response = smb2_oplock_response,
2833 .queryfs = smb2_queryfs,
2834 .mand_lock = smb2_mand_lock,
2835 .mand_unlock_range = smb2_unlock_range,
2836 .push_mand_locks = smb2_push_mandatory_locks,
2837 .get_lease_key = smb2_get_lease_key,
2838 .set_lease_key = smb2_set_lease_key,
2839 .new_lease_key = smb2_new_lease_key,
2840 .calc_signature = smb2_calc_signature,
2841 .is_read_op = smb2_is_read_op,
2842 .set_oplock_level = smb2_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002843 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002844 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05002845 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002846 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04002847 .dir_needs_close = smb2_dir_needs_close,
Aurelien Aptel9d496402017-02-13 16:16:49 +01002848 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302849 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002850#ifdef CONFIG_CIFS_XATTR
2851 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10002852 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002853#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002854#ifdef CONFIG_CIFS_ACL
2855 .get_acl = get_smb2_acl,
2856 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05002857 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002858#endif /* CIFS_ACL */
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002859};
2860
Steve French1080ef72011-02-24 18:07:19 +00002861struct smb_version_operations smb21_operations = {
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07002862 .compare_fids = smb2_compare_fids,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002863 .setup_request = smb2_setup_request,
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +04002864 .setup_async_request = smb2_setup_async_request,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002865 .check_receive = smb2_check_receive,
Pavel Shilovsky28ea5292012-05-23 16:18:00 +04002866 .add_credits = smb2_add_credits,
2867 .set_credits = smb2_set_credits,
2868 .get_credits_field = smb2_get_credits_field,
2869 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002870 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002871 .get_next_mid = smb2_get_next_mid,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002872 .read_data_offset = smb2_read_data_offset,
2873 .read_data_length = smb2_read_data_length,
2874 .map_error = map_smb2_to_linux_error,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04002875 .find_mid = smb2_find_mid,
2876 .check_message = smb2_check_message,
2877 .dump_detail = smb2_dump_detail,
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04002878 .clear_stats = smb2_clear_stats,
2879 .print_stats = smb2_print_stats,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002880 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08002881 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002882 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04002883 .need_neg = smb2_need_neg,
2884 .negotiate = smb2_negotiate,
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -07002885 .negotiate_wsize = smb2_negotiate_wsize,
2886 .negotiate_rsize = smb2_negotiate_rsize,
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04002887 .sess_setup = SMB2_sess_setup,
2888 .logoff = SMB2_logoff,
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04002889 .tree_connect = SMB2_tcon,
2890 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05002891 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002892 .is_path_accessible = smb2_is_path_accessible,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002893 .can_echo = smb2_can_echo,
2894 .echo = SMB2_echo,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002895 .query_path_info = smb2_query_path_info,
2896 .get_srv_inum = smb2_get_srv_inum,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -07002897 .query_file_info = smb2_query_file_info,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002898 .set_path_size = smb2_set_path_size,
2899 .set_file_size = smb2_set_file_size,
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07002900 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05002901 .set_compression = smb2_set_compression,
Pavel Shilovskya0e73182011-07-19 12:56:37 +04002902 .mkdir = smb2_mkdir,
2903 .mkdir_setinfo = smb2_mkdir_setinfo,
Pavel Shilovsky1a500f02012-07-10 16:14:38 +04002904 .rmdir = smb2_rmdir,
Pavel Shilovskycbe6f432012-09-18 16:20:25 -07002905 .unlink = smb2_unlink,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002906 .rename = smb2_rename_path,
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002907 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002908 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05002909 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05002910 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002911 .open = smb2_open_file,
2912 .set_fid = smb2_set_fid,
2913 .close = smb2_close_file,
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002914 .flush = smb2_flush_file,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002915 .async_readv = smb2_async_readv,
Pavel Shilovsky33319142012-09-18 16:20:29 -07002916 .async_writev = smb2_async_writev,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002917 .sync_read = smb2_sync_read,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002918 .sync_write = smb2_sync_write,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002919 .query_dir_first = smb2_query_dir_first,
2920 .query_dir_next = smb2_query_dir_next,
2921 .close_dir = smb2_close_dir,
2922 .calc_smb_size = smb2_calc_size,
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002923 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002924 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002925 .oplock_response = smb2_oplock_response,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002926 .queryfs = smb2_queryfs,
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07002927 .mand_lock = smb2_mand_lock,
2928 .mand_unlock_range = smb2_unlock_range,
Pavel Shilovskyb1407992012-09-19 06:22:44 -07002929 .push_mand_locks = smb2_push_mandatory_locks,
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002930 .get_lease_key = smb2_get_lease_key,
2931 .set_lease_key = smb2_set_lease_key,
2932 .new_lease_key = smb2_new_lease_key,
Steve French38107d42012-12-08 22:08:06 -06002933 .calc_signature = smb2_calc_signature,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002934 .is_read_op = smb21_is_read_op,
2935 .set_oplock_level = smb21_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002936 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002937 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05002938 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002939 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04002940 .dir_needs_close = smb2_dir_needs_close,
Steve French834170c2016-09-30 21:14:26 -05002941 .enum_snapshots = smb3_enum_snapshots,
Aurelien Aptel9d496402017-02-13 16:16:49 +01002942 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302943 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002944#ifdef CONFIG_CIFS_XATTR
2945 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10002946 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002947#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002948#ifdef CONFIG_CIFS_ACL
2949 .get_acl = get_smb2_acl,
2950 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05002951 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002952#endif /* CIFS_ACL */
Steve French38107d42012-12-08 22:08:06 -06002953};
2954
Steve French38107d42012-12-08 22:08:06 -06002955struct smb_version_operations smb30_operations = {
2956 .compare_fids = smb2_compare_fids,
2957 .setup_request = smb2_setup_request,
2958 .setup_async_request = smb2_setup_async_request,
2959 .check_receive = smb2_check_receive,
2960 .add_credits = smb2_add_credits,
2961 .set_credits = smb2_set_credits,
2962 .get_credits_field = smb2_get_credits_field,
2963 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002964 .wait_mtu_credits = smb2_wait_mtu_credits,
Steve French38107d42012-12-08 22:08:06 -06002965 .get_next_mid = smb2_get_next_mid,
2966 .read_data_offset = smb2_read_data_offset,
2967 .read_data_length = smb2_read_data_length,
2968 .map_error = map_smb2_to_linux_error,
2969 .find_mid = smb2_find_mid,
2970 .check_message = smb2_check_message,
2971 .dump_detail = smb2_dump_detail,
2972 .clear_stats = smb2_clear_stats,
2973 .print_stats = smb2_print_stats,
Steve French769ee6a2013-06-19 14:15:30 -05002974 .dump_share_caps = smb2_dump_share_caps,
Steve French38107d42012-12-08 22:08:06 -06002975 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08002976 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002977 .downgrade_oplock = smb2_downgrade_oplock,
Steve French38107d42012-12-08 22:08:06 -06002978 .need_neg = smb2_need_neg,
2979 .negotiate = smb2_negotiate,
2980 .negotiate_wsize = smb2_negotiate_wsize,
2981 .negotiate_rsize = smb2_negotiate_rsize,
2982 .sess_setup = SMB2_sess_setup,
2983 .logoff = SMB2_logoff,
2984 .tree_connect = SMB2_tcon,
2985 .tree_disconnect = SMB2_tdis,
Steven Frenchaf6a12e2013-10-09 20:55:53 -05002986 .qfs_tcon = smb3_qfs_tcon,
Steve French38107d42012-12-08 22:08:06 -06002987 .is_path_accessible = smb2_is_path_accessible,
2988 .can_echo = smb2_can_echo,
2989 .echo = SMB2_echo,
2990 .query_path_info = smb2_query_path_info,
2991 .get_srv_inum = smb2_get_srv_inum,
2992 .query_file_info = smb2_query_file_info,
2993 .set_path_size = smb2_set_path_size,
2994 .set_file_size = smb2_set_file_size,
2995 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05002996 .set_compression = smb2_set_compression,
Steve French38107d42012-12-08 22:08:06 -06002997 .mkdir = smb2_mkdir,
2998 .mkdir_setinfo = smb2_mkdir_setinfo,
2999 .rmdir = smb2_rmdir,
3000 .unlink = smb2_unlink,
3001 .rename = smb2_rename_path,
3002 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003003 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05003004 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05003005 .create_mf_symlink = smb3_create_mf_symlink,
Steve French38107d42012-12-08 22:08:06 -06003006 .open = smb2_open_file,
3007 .set_fid = smb2_set_fid,
3008 .close = smb2_close_file,
3009 .flush = smb2_flush_file,
3010 .async_readv = smb2_async_readv,
3011 .async_writev = smb2_async_writev,
3012 .sync_read = smb2_sync_read,
3013 .sync_write = smb2_sync_write,
3014 .query_dir_first = smb2_query_dir_first,
3015 .query_dir_next = smb2_query_dir_next,
3016 .close_dir = smb2_close_dir,
3017 .calc_smb_size = smb2_calc_size,
3018 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07003019 .is_session_expired = smb2_is_session_expired,
Steve French38107d42012-12-08 22:08:06 -06003020 .oplock_response = smb2_oplock_response,
3021 .queryfs = smb2_queryfs,
3022 .mand_lock = smb2_mand_lock,
3023 .mand_unlock_range = smb2_unlock_range,
3024 .push_mand_locks = smb2_push_mandatory_locks,
3025 .get_lease_key = smb2_get_lease_key,
3026 .set_lease_key = smb2_set_lease_key,
3027 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06003028 .generate_signingkey = generate_smb30signingkey,
Steve French38107d42012-12-08 22:08:06 -06003029 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05003030 .set_integrity = smb3_set_integrity,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003031 .is_read_op = smb21_is_read_op,
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003032 .set_oplock_level = smb3_set_oplock_level,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003033 .create_lease_buf = smb3_create_lease_buf,
3034 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05003035 .copychunk_range = smb2_copychunk_range,
Steve Frenchca9e7a12015-10-01 21:40:10 -05003036 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchff1c0382013-11-19 23:44:46 -06003037 .validate_negotiate = smb3_validate_negotiate,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04003038 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04003039 .dir_needs_close = smb2_dir_needs_close,
Steve French31742c52014-08-17 08:38:47 -05003040 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05003041 .enum_snapshots = smb3_enum_snapshots,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003042 .init_transform_rq = smb3_init_transform_rq,
3043 .free_transform_rq = smb3_free_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003044 .is_transform_hdr = smb3_is_transform_hdr,
3045 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01003046 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05303047 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003048#ifdef CONFIG_CIFS_XATTR
3049 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003050 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003051#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003052#ifdef CONFIG_CIFS_ACL
3053 .get_acl = get_smb2_acl,
3054 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003055 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003056#endif /* CIFS_ACL */
Steve French1080ef72011-02-24 18:07:19 +00003057};
3058
Steve Frenchaab18932015-06-23 23:37:11 -05003059#ifdef CONFIG_CIFS_SMB311
3060struct smb_version_operations smb311_operations = {
3061 .compare_fids = smb2_compare_fids,
3062 .setup_request = smb2_setup_request,
3063 .setup_async_request = smb2_setup_async_request,
3064 .check_receive = smb2_check_receive,
3065 .add_credits = smb2_add_credits,
3066 .set_credits = smb2_set_credits,
3067 .get_credits_field = smb2_get_credits_field,
3068 .get_credits = smb2_get_credits,
3069 .wait_mtu_credits = smb2_wait_mtu_credits,
3070 .get_next_mid = smb2_get_next_mid,
3071 .read_data_offset = smb2_read_data_offset,
3072 .read_data_length = smb2_read_data_length,
3073 .map_error = map_smb2_to_linux_error,
3074 .find_mid = smb2_find_mid,
3075 .check_message = smb2_check_message,
3076 .dump_detail = smb2_dump_detail,
3077 .clear_stats = smb2_clear_stats,
3078 .print_stats = smb2_print_stats,
3079 .dump_share_caps = smb2_dump_share_caps,
3080 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08003081 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Steve Frenchaab18932015-06-23 23:37:11 -05003082 .downgrade_oplock = smb2_downgrade_oplock,
3083 .need_neg = smb2_need_neg,
3084 .negotiate = smb2_negotiate,
3085 .negotiate_wsize = smb2_negotiate_wsize,
3086 .negotiate_rsize = smb2_negotiate_rsize,
3087 .sess_setup = SMB2_sess_setup,
3088 .logoff = SMB2_logoff,
3089 .tree_connect = SMB2_tcon,
3090 .tree_disconnect = SMB2_tdis,
3091 .qfs_tcon = smb3_qfs_tcon,
3092 .is_path_accessible = smb2_is_path_accessible,
3093 .can_echo = smb2_can_echo,
3094 .echo = SMB2_echo,
3095 .query_path_info = smb2_query_path_info,
3096 .get_srv_inum = smb2_get_srv_inum,
3097 .query_file_info = smb2_query_file_info,
3098 .set_path_size = smb2_set_path_size,
3099 .set_file_size = smb2_set_file_size,
3100 .set_file_info = smb2_set_file_info,
3101 .set_compression = smb2_set_compression,
3102 .mkdir = smb2_mkdir,
3103 .mkdir_setinfo = smb2_mkdir_setinfo,
3104 .rmdir = smb2_rmdir,
3105 .unlink = smb2_unlink,
3106 .rename = smb2_rename_path,
3107 .create_hardlink = smb2_create_hardlink,
3108 .query_symlink = smb2_query_symlink,
3109 .query_mf_symlink = smb3_query_mf_symlink,
3110 .create_mf_symlink = smb3_create_mf_symlink,
3111 .open = smb2_open_file,
3112 .set_fid = smb2_set_fid,
3113 .close = smb2_close_file,
3114 .flush = smb2_flush_file,
3115 .async_readv = smb2_async_readv,
3116 .async_writev = smb2_async_writev,
3117 .sync_read = smb2_sync_read,
3118 .sync_write = smb2_sync_write,
3119 .query_dir_first = smb2_query_dir_first,
3120 .query_dir_next = smb2_query_dir_next,
3121 .close_dir = smb2_close_dir,
3122 .calc_smb_size = smb2_calc_size,
3123 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07003124 .is_session_expired = smb2_is_session_expired,
Steve Frenchaab18932015-06-23 23:37:11 -05003125 .oplock_response = smb2_oplock_response,
3126 .queryfs = smb2_queryfs,
3127 .mand_lock = smb2_mand_lock,
3128 .mand_unlock_range = smb2_unlock_range,
3129 .push_mand_locks = smb2_push_mandatory_locks,
3130 .get_lease_key = smb2_get_lease_key,
3131 .set_lease_key = smb2_set_lease_key,
3132 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06003133 .generate_signingkey = generate_smb311signingkey,
Steve Frenchaab18932015-06-23 23:37:11 -05003134 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05003135 .set_integrity = smb3_set_integrity,
Steve Frenchaab18932015-06-23 23:37:11 -05003136 .is_read_op = smb21_is_read_op,
3137 .set_oplock_level = smb3_set_oplock_level,
3138 .create_lease_buf = smb3_create_lease_buf,
3139 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05003140 .copychunk_range = smb2_copychunk_range,
Steve French02b16662015-06-27 21:18:36 -07003141 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchaab18932015-06-23 23:37:11 -05003142/* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
3143 .wp_retry_size = smb2_wp_retry_size,
3144 .dir_needs_close = smb2_dir_needs_close,
3145 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05003146 .enum_snapshots = smb3_enum_snapshots,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003147 .init_transform_rq = smb3_init_transform_rq,
3148 .free_transform_rq = smb3_free_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003149 .is_transform_hdr = smb3_is_transform_hdr,
3150 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01003151 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05303152 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003153#ifdef CONFIG_CIFS_XATTR
3154 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003155 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003156#endif /* CIFS_XATTR */
Steve Frenchaab18932015-06-23 23:37:11 -05003157};
3158#endif /* CIFS_SMB311 */
3159
Steve Frenchdd446b12012-11-28 23:21:06 -06003160struct smb_version_values smb20_values = {
3161 .version_string = SMB20_VERSION_STRING,
3162 .protocol_id = SMB20_PROT_ID,
3163 .req_capabilities = 0, /* MBZ */
3164 .large_lock_type = 0,
3165 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3166 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3167 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3168 .header_size = sizeof(struct smb2_hdr),
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003169 .header_preamble_size = 4,
Steve Frenchdd446b12012-11-28 23:21:06 -06003170 .max_header_size = MAX_SMB2_HDR_SIZE,
3171 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3172 .lock_cmd = SMB2_LOCK,
3173 .cap_unix = 0,
3174 .cap_nt_find = SMB2_NT_FIND,
3175 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003176 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3177 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003178 .create_lease_size = sizeof(struct create_lease),
Steve Frenchdd446b12012-11-28 23:21:06 -06003179};
3180
Steve French1080ef72011-02-24 18:07:19 +00003181struct smb_version_values smb21_values = {
3182 .version_string = SMB21_VERSION_STRING,
Steve Frenche4aa25e2012-10-01 12:26:22 -05003183 .protocol_id = SMB21_PROT_ID,
3184 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
3185 .large_lock_type = 0,
3186 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3187 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3188 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3189 .header_size = sizeof(struct smb2_hdr),
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003190 .header_preamble_size = 4,
Steve Frenche4aa25e2012-10-01 12:26:22 -05003191 .max_header_size = MAX_SMB2_HDR_SIZE,
3192 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3193 .lock_cmd = SMB2_LOCK,
3194 .cap_unix = 0,
3195 .cap_nt_find = SMB2_NT_FIND,
3196 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003197 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3198 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003199 .create_lease_size = sizeof(struct create_lease),
Steve Frenche4aa25e2012-10-01 12:26:22 -05003200};
3201
Steve French9764c022017-09-17 10:41:35 -05003202struct smb_version_values smb3any_values = {
3203 .version_string = SMB3ANY_VERSION_STRING,
3204 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3205 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
3206 .large_lock_type = 0,
3207 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3208 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3209 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3210 .header_size = sizeof(struct smb2_hdr),
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003211 .header_preamble_size = 4,
Steve French9764c022017-09-17 10:41:35 -05003212 .max_header_size = MAX_SMB2_HDR_SIZE,
3213 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3214 .lock_cmd = SMB2_LOCK,
3215 .cap_unix = 0,
3216 .cap_nt_find = SMB2_NT_FIND,
3217 .cap_large_files = SMB2_LARGE_FILES,
3218 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3219 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3220 .create_lease_size = sizeof(struct create_lease_v2),
3221};
3222
3223struct smb_version_values smbdefault_values = {
3224 .version_string = SMBDEFAULT_VERSION_STRING,
3225 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3226 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
3227 .large_lock_type = 0,
3228 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3229 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3230 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3231 .header_size = sizeof(struct smb2_hdr),
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003232 .header_preamble_size = 4,
Steve French9764c022017-09-17 10:41:35 -05003233 .max_header_size = MAX_SMB2_HDR_SIZE,
3234 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3235 .lock_cmd = SMB2_LOCK,
3236 .cap_unix = 0,
3237 .cap_nt_find = SMB2_NT_FIND,
3238 .cap_large_files = SMB2_LARGE_FILES,
3239 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3240 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3241 .create_lease_size = sizeof(struct create_lease_v2),
3242};
3243
Steve Frenche4aa25e2012-10-01 12:26:22 -05003244struct smb_version_values smb30_values = {
3245 .version_string = SMB30_VERSION_STRING,
3246 .protocol_id = SMB30_PROT_ID,
Steve French373512e2015-12-18 13:05:30 -06003247 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07003248 .large_lock_type = 0,
3249 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3250 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3251 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04003252 .header_size = sizeof(struct smb2_hdr),
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003253 .header_preamble_size = 4,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04003254 .max_header_size = MAX_SMB2_HDR_SIZE,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003255 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04003256 .lock_cmd = SMB2_LOCK,
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04003257 .cap_unix = 0,
3258 .cap_nt_find = SMB2_NT_FIND,
3259 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003260 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3261 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003262 .create_lease_size = sizeof(struct create_lease_v2),
Steve French1080ef72011-02-24 18:07:19 +00003263};
Steve French20b6d8b2013-06-12 22:48:41 -05003264
3265struct smb_version_values smb302_values = {
3266 .version_string = SMB302_VERSION_STRING,
3267 .protocol_id = SMB302_PROT_ID,
Steve French373512e2015-12-18 13:05:30 -06003268 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
Steve French20b6d8b2013-06-12 22:48:41 -05003269 .large_lock_type = 0,
3270 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3271 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3272 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3273 .header_size = sizeof(struct smb2_hdr),
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003274 .header_preamble_size = 4,
Steve French20b6d8b2013-06-12 22:48:41 -05003275 .max_header_size = MAX_SMB2_HDR_SIZE,
3276 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3277 .lock_cmd = SMB2_LOCK,
3278 .cap_unix = 0,
3279 .cap_nt_find = SMB2_NT_FIND,
3280 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003281 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3282 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003283 .create_lease_size = sizeof(struct create_lease_v2),
Steve French20b6d8b2013-06-12 22:48:41 -05003284};
Steve French5f7fbf72014-12-17 22:52:58 -06003285
3286#ifdef CONFIG_CIFS_SMB311
3287struct smb_version_values smb311_values = {
3288 .version_string = SMB311_VERSION_STRING,
3289 .protocol_id = SMB311_PROT_ID,
Steve French19558802017-06-21 16:50:20 -05003290 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
Steve French5f7fbf72014-12-17 22:52:58 -06003291 .large_lock_type = 0,
3292 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3293 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3294 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3295 .header_size = sizeof(struct smb2_hdr),
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003296 .header_preamble_size = 4,
Steve French5f7fbf72014-12-17 22:52:58 -06003297 .max_header_size = MAX_SMB2_HDR_SIZE,
3298 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3299 .lock_cmd = SMB2_LOCK,
3300 .cap_unix = 0,
3301 .cap_nt_find = SMB2_NT_FIND,
3302 .cap_large_files = SMB2_LARGE_FILES,
3303 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3304 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3305 .create_lease_size = sizeof(struct create_lease_v2),
3306};
3307#endif /* SMB311 */