blob: f2bce73e23a02f3ba50442e44ff445f62c35e4be [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
Ronnie Sahlberg14547f72018-04-22 14:45:53 -0600215smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400216{
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);
Ronnie Sahlberg14547f72018-04-22 14:45:53 -0600223 cifs_dbg(VFS, "smb buf %p len %u\n", buf,
Steve French71992e622018-05-06 15:58:51 -0500224 server->ops->calc_smb_size(buf, server));
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400225#endif
226}
227
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400228static bool
229smb2_need_neg(struct TCP_Server_Info *server)
230{
231 return server->max_read == 0;
232}
233
234static int
235smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
236{
237 int rc;
238 ses->server->CurrentMid = 0;
239 rc = SMB2_negotiate(xid, ses);
240 /* BB we probably don't need to retry with modern servers */
241 if (rc == -EAGAIN)
242 rc = -EHOSTDOWN;
243 return rc;
244}
245
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700246static unsigned int
247smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
248{
249 struct TCP_Server_Info *server = tcon->ses->server;
250 unsigned int wsize;
251
252 /* start with specified wsize, or default */
253 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
254 wsize = min_t(unsigned int, wsize, server->max_write);
Long Li09902f82017-11-22 17:38:39 -0700255#ifdef CONFIG_CIFS_SMB_DIRECT
Long Libb4c0412018-04-17 12:17:08 -0700256 if (server->rdma) {
257 if (server->sign)
258 wsize = min_t(unsigned int,
259 wsize, server->smbd_conn->max_fragmented_send_size);
260 else
261 wsize = min_t(unsigned int,
Long Li09902f82017-11-22 17:38:39 -0700262 wsize, server->smbd_conn->max_readwrite_size);
Long Libb4c0412018-04-17 12:17:08 -0700263 }
Long Li09902f82017-11-22 17:38:39 -0700264#endif
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400265 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
266 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700267
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700268 return wsize;
269}
270
271static unsigned int
272smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
273{
274 struct TCP_Server_Info *server = tcon->ses->server;
275 unsigned int rsize;
276
277 /* start with specified rsize, or default */
278 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
279 rsize = min_t(unsigned int, rsize, server->max_read);
Long Li09902f82017-11-22 17:38:39 -0700280#ifdef CONFIG_CIFS_SMB_DIRECT
Long Libb4c0412018-04-17 12:17:08 -0700281 if (server->rdma) {
282 if (server->sign)
283 rsize = min_t(unsigned int,
284 rsize, server->smbd_conn->max_fragmented_recv_size);
285 else
286 rsize = min_t(unsigned int,
Long Li09902f82017-11-22 17:38:39 -0700287 rsize, server->smbd_conn->max_readwrite_size);
Long Libb4c0412018-04-17 12:17:08 -0700288 }
Long Li09902f82017-11-22 17:38:39 -0700289#endif
Pavel Shilovskybed9da02014-06-25 11:28:57 +0400290
291 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
292 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700293
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700294 return rsize;
295}
296
Steve Frenchc481e9f2013-10-14 01:21:53 -0500297#ifdef CONFIG_CIFS_STATS2
298static int
299SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
300{
301 int rc;
302 unsigned int ret_data_len = 0;
303 struct network_interface_info_ioctl_rsp *out_buf;
304
305 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
306 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
307 NULL /* no data input */, 0 /* no data input */,
308 (char **)&out_buf, &ret_data_len);
Steve French9ffc5412014-10-16 15:13:14 -0500309 if (rc != 0)
310 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
311 else if (ret_data_len < sizeof(struct network_interface_info_ioctl_rsp)) {
312 cifs_dbg(VFS, "server returned bad net interface info buf\n");
313 rc = -EINVAL;
314 } else {
Steve Frenchc481e9f2013-10-14 01:21:53 -0500315 /* Dump info on first interface */
316 cifs_dbg(FYI, "Adapter Capability 0x%x\t",
317 le32_to_cpu(out_buf->Capability));
318 cifs_dbg(FYI, "Link Speed %lld\n",
319 le64_to_cpu(out_buf->LinkSpeed));
Steve French9ffc5412014-10-16 15:13:14 -0500320 }
Steve French24df1482016-09-29 04:20:23 -0500321 kfree(out_buf);
Steve Frenchc481e9f2013-10-14 01:21:53 -0500322 return rc;
323}
324#endif /* STATS2 */
325
Steve French3d4ef9a2018-04-25 22:19:09 -0500326/*
327 * Open the directory at the root of a share
328 */
329int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
330{
331 struct cifs_open_parms oparams;
332 int rc;
333 __le16 srch_path = 0; /* Null - since an open of top of share */
334 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
335
336 mutex_lock(&tcon->prfid_mutex);
337 if (tcon->valid_root_fid) {
338 cifs_dbg(FYI, "found a cached root file handle\n");
339 memcpy(pfid, tcon->prfid, sizeof(struct cifs_fid));
340 mutex_unlock(&tcon->prfid_mutex);
341 return 0;
342 }
343
344 oparams.tcon = tcon;
345 oparams.create_options = 0;
346 oparams.desired_access = FILE_READ_ATTRIBUTES;
347 oparams.disposition = FILE_OPEN;
348 oparams.fid = pfid;
349 oparams.reconnect = false;
350
351 rc = SMB2_open(xid, &oparams, &srch_path, &oplock, NULL, NULL);
352 if (rc == 0) {
353 memcpy(tcon->prfid, pfid, sizeof(struct cifs_fid));
354 tcon->valid_root_fid = true;
355 }
356 mutex_unlock(&tcon->prfid_mutex);
357 return rc;
358}
359
Steve French34f62642013-10-09 02:07:00 -0500360static void
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500361smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
362{
363 int rc;
364 __le16 srch_path = 0; /* Null - open root of share */
365 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
366 struct cifs_open_parms oparms;
367 struct cifs_fid fid;
Steve French3d4ef9a2018-04-25 22:19:09 -0500368 bool no_cached_open = tcon->nohandlecache;
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500369
370 oparms.tcon = tcon;
371 oparms.desired_access = FILE_READ_ATTRIBUTES;
372 oparms.disposition = FILE_OPEN;
373 oparms.create_options = 0;
374 oparms.fid = &fid;
375 oparms.reconnect = false;
376
Steve French3d4ef9a2018-04-25 22:19:09 -0500377 if (no_cached_open)
378 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
379 else
380 rc = open_shroot(xid, tcon, &fid);
381
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500382 if (rc)
383 return;
384
Steve Frenchc481e9f2013-10-14 01:21:53 -0500385#ifdef CONFIG_CIFS_STATS2
386 SMB3_request_interfaces(xid, tcon);
387#endif /* STATS2 */
388
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500389 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
390 FS_ATTRIBUTE_INFORMATION);
391 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
392 FS_DEVICE_INFORMATION);
393 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
394 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
Steve French3d4ef9a2018-04-25 22:19:09 -0500395 if (no_cached_open)
396 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500397 return;
398}
399
400static void
Steve French34f62642013-10-09 02:07:00 -0500401smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
402{
403 int rc;
404 __le16 srch_path = 0; /* Null - open root of share */
405 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
406 struct cifs_open_parms oparms;
407 struct cifs_fid fid;
408
409 oparms.tcon = tcon;
410 oparms.desired_access = FILE_READ_ATTRIBUTES;
411 oparms.disposition = FILE_OPEN;
412 oparms.create_options = 0;
413 oparms.fid = &fid;
414 oparms.reconnect = false;
415
416 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
417 if (rc)
418 return;
419
Steven French21671142013-10-09 13:36:35 -0500420 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
421 FS_ATTRIBUTE_INFORMATION);
422 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
423 FS_DEVICE_INFORMATION);
Steve French34f62642013-10-09 02:07:00 -0500424 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
425 return;
426}
427
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400428static int
429smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
430 struct cifs_sb_info *cifs_sb, const char *full_path)
431{
432 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400433 __le16 *utf16_path;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700434 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400435 struct cifs_open_parms oparms;
436 struct cifs_fid fid;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400437
Steve French3d4ef9a2018-04-25 22:19:09 -0500438 if ((*full_path == 0) && tcon->valid_root_fid)
439 return 0;
440
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400441 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
442 if (!utf16_path)
443 return -ENOMEM;
444
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400445 oparms.tcon = tcon;
446 oparms.desired_access = FILE_READ_ATTRIBUTES;
447 oparms.disposition = FILE_OPEN;
448 oparms.create_options = 0;
449 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +0400450 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400451
Pavel Shilovskyb42bf882013-08-14 19:25:21 +0400452 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400453 if (rc) {
454 kfree(utf16_path);
455 return rc;
456 }
457
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400458 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400459 kfree(utf16_path);
460 return rc;
461}
462
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400463static int
464smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
465 struct cifs_sb_info *cifs_sb, const char *full_path,
466 u64 *uniqueid, FILE_ALL_INFO *data)
467{
468 *uniqueid = le64_to_cpu(data->IndexNumber);
469 return 0;
470}
471
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700472static int
473smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
474 struct cifs_fid *fid, FILE_ALL_INFO *data)
475{
476 int rc;
477 struct smb2_file_all_info *smb2_data;
478
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +0400479 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700480 GFP_KERNEL);
481 if (smb2_data == NULL)
482 return -ENOMEM;
483
484 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
485 smb2_data);
486 if (!rc)
487 move_smb2_info_to_cifs(data, smb2_data);
488 kfree(smb2_data);
489 return rc;
490}
491
Arnd Bergmann1368f152017-09-05 11:24:15 +0200492#ifdef CONFIG_CIFS_XATTR
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000493static ssize_t
494move_smb2_ea_to_cifs(char *dst, size_t dst_size,
495 struct smb2_file_full_ea_info *src, size_t src_size,
496 const unsigned char *ea_name)
497{
498 int rc = 0;
499 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
500 char *name, *value;
501 size_t name_len, value_len, user_name_len;
502
503 while (src_size > 0) {
504 name = &src->ea_data[0];
505 name_len = (size_t)src->ea_name_length;
506 value = &src->ea_data[src->ea_name_length + 1];
507 value_len = (size_t)le16_to_cpu(src->ea_value_length);
508
509 if (name_len == 0) {
510 break;
511 }
512
513 if (src_size < 8 + name_len + 1 + value_len) {
514 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
515 rc = -EIO;
516 goto out;
517 }
518
519 if (ea_name) {
520 if (ea_name_len == name_len &&
521 memcmp(ea_name, name, name_len) == 0) {
522 rc = value_len;
523 if (dst_size == 0)
524 goto out;
525 if (dst_size < value_len) {
526 rc = -ERANGE;
527 goto out;
528 }
529 memcpy(dst, value, value_len);
530 goto out;
531 }
532 } else {
533 /* 'user.' plus a terminating null */
534 user_name_len = 5 + 1 + name_len;
535
536 rc += user_name_len;
537
538 if (dst_size >= user_name_len) {
539 dst_size -= user_name_len;
540 memcpy(dst, "user.", 5);
541 dst += 5;
542 memcpy(dst, src->ea_data, name_len);
543 dst += name_len;
544 *dst = 0;
545 ++dst;
546 } else if (dst_size == 0) {
547 /* skip copy - calc size only */
548 } else {
549 /* stop before overrun buffer */
550 rc = -ERANGE;
551 break;
552 }
553 }
554
555 if (!src->next_entry_offset)
556 break;
557
558 if (src_size < le32_to_cpu(src->next_entry_offset)) {
559 /* stop before overrun buffer */
560 rc = -ERANGE;
561 break;
562 }
563 src_size -= le32_to_cpu(src->next_entry_offset);
564 src = (void *)((char *)src +
565 le32_to_cpu(src->next_entry_offset));
566 }
567
568 /* didn't find the named attribute */
569 if (ea_name)
570 rc = -ENODATA;
571
572out:
573 return (ssize_t)rc;
574}
575
576static ssize_t
577smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
578 const unsigned char *path, const unsigned char *ea_name,
579 char *ea_data, size_t buf_size,
580 struct cifs_sb_info *cifs_sb)
581{
582 int rc;
583 __le16 *utf16_path;
584 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
585 struct cifs_open_parms oparms;
586 struct cifs_fid fid;
587 struct smb2_file_full_ea_info *smb2_data;
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +1000588 int ea_buf_size = SMB2_MIN_EA_BUF;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000589
590 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
591 if (!utf16_path)
592 return -ENOMEM;
593
594 oparms.tcon = tcon;
595 oparms.desired_access = FILE_READ_EA;
596 oparms.disposition = FILE_OPEN;
597 oparms.create_options = 0;
598 oparms.fid = &fid;
599 oparms.reconnect = false;
600
601 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
602 kfree(utf16_path);
603 if (rc) {
604 cifs_dbg(FYI, "open failed rc=%d\n", rc);
605 return rc;
606 }
607
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +1000608 while (1) {
609 smb2_data = kzalloc(ea_buf_size, GFP_KERNEL);
610 if (smb2_data == NULL) {
611 SMB2_close(xid, tcon, fid.persistent_fid,
612 fid.volatile_fid);
613 return -ENOMEM;
614 }
615
616 rc = SMB2_query_eas(xid, tcon, fid.persistent_fid,
617 fid.volatile_fid,
618 ea_buf_size, smb2_data);
619
620 if (rc != -E2BIG)
621 break;
622
623 kfree(smb2_data);
624 ea_buf_size <<= 1;
625
626 if (ea_buf_size > SMB2_MAX_EA_BUF) {
627 cifs_dbg(VFS, "EA size is too large\n");
628 SMB2_close(xid, tcon, fid.persistent_fid,
629 fid.volatile_fid);
630 return -ENOMEM;
631 }
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000632 }
633
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000634 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
635
Paulo Alcantaraae2cd7f2018-05-04 11:25:26 -0300636 /*
637 * If ea_name is NULL (listxattr) and there are no EAs, return 0 as it's
638 * not an error. Otherwise, the specified ea_name was not found.
639 */
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000640 if (!rc)
641 rc = move_smb2_ea_to_cifs(ea_data, buf_size, smb2_data,
642 SMB2_MAX_EA_BUF, ea_name);
Paulo Alcantaraae2cd7f2018-05-04 11:25:26 -0300643 else if (!ea_name && rc == -ENODATA)
644 rc = 0;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000645
646 kfree(smb2_data);
647 return rc;
648}
649
Ronnie Sahlberg55175542017-08-24 11:24:56 +1000650
651static int
652smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
653 const char *path, const char *ea_name, const void *ea_value,
654 const __u16 ea_value_len, const struct nls_table *nls_codepage,
655 struct cifs_sb_info *cifs_sb)
656{
657 int rc;
658 __le16 *utf16_path;
659 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
660 struct cifs_open_parms oparms;
661 struct cifs_fid fid;
662 struct smb2_file_full_ea_info *ea;
663 int ea_name_len = strlen(ea_name);
664 int len;
665
666 if (ea_name_len > 255)
667 return -EINVAL;
668
669 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
670 if (!utf16_path)
671 return -ENOMEM;
672
673 oparms.tcon = tcon;
674 oparms.desired_access = FILE_WRITE_EA;
675 oparms.disposition = FILE_OPEN;
676 oparms.create_options = 0;
677 oparms.fid = &fid;
678 oparms.reconnect = false;
679
680 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
681 kfree(utf16_path);
682 if (rc) {
683 cifs_dbg(FYI, "open failed rc=%d\n", rc);
684 return rc;
685 }
686
687 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
688 ea = kzalloc(len, GFP_KERNEL);
689 if (ea == NULL) {
690 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
691 return -ENOMEM;
692 }
693
694 ea->ea_name_length = ea_name_len;
695 ea->ea_value_length = cpu_to_le16(ea_value_len);
696 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
697 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
698
699 rc = SMB2_set_ea(xid, tcon, fid.persistent_fid, fid.volatile_fid, ea,
700 len);
701 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
702
703 return rc;
704}
Arnd Bergmann1368f152017-09-05 11:24:15 +0200705#endif
Ronnie Sahlberg55175542017-08-24 11:24:56 +1000706
Pavel Shilovsky9094fad2012-07-12 18:30:44 +0400707static bool
708smb2_can_echo(struct TCP_Server_Info *server)
709{
710 return server->echoes;
711}
712
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400713static void
714smb2_clear_stats(struct cifs_tcon *tcon)
715{
716#ifdef CONFIG_CIFS_STATS
717 int i;
718 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
719 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
720 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
721 }
722#endif
723}
724
725static void
Steve French769ee6a2013-06-19 14:15:30 -0500726smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
727{
728 seq_puts(m, "\n\tShare Capabilities:");
729 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
730 seq_puts(m, " DFS,");
731 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
732 seq_puts(m, " CONTINUOUS AVAILABILITY,");
733 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
734 seq_puts(m, " SCALEOUT,");
735 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
736 seq_puts(m, " CLUSTER,");
737 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
738 seq_puts(m, " ASYMMETRIC,");
739 if (tcon->capabilities == 0)
740 seq_puts(m, " None");
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500741 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
742 seq_puts(m, " Aligned,");
743 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
744 seq_puts(m, " Partition Aligned,");
745 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
746 seq_puts(m, " SSD,");
747 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
748 seq_puts(m, " TRIM-support,");
749
Steve French769ee6a2013-06-19 14:15:30 -0500750 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
Steve Frenche0386e42018-05-20 01:27:03 -0500751 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500752 if (tcon->perf_sector_size)
753 seq_printf(m, "\tOptimal sector size: 0x%x",
754 tcon->perf_sector_size);
Steve Frenche0386e42018-05-20 01:27:03 -0500755 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
Steve French769ee6a2013-06-19 14:15:30 -0500756}
757
758static void
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400759smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
760{
761#ifdef CONFIG_CIFS_STATS
762 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
763 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
764 seq_printf(m, "\nNegotiates: %d sent %d failed",
765 atomic_read(&sent[SMB2_NEGOTIATE_HE]),
766 atomic_read(&failed[SMB2_NEGOTIATE_HE]));
767 seq_printf(m, "\nSessionSetups: %d sent %d failed",
768 atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
769 atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400770 seq_printf(m, "\nLogoffs: %d sent %d failed",
771 atomic_read(&sent[SMB2_LOGOFF_HE]),
772 atomic_read(&failed[SMB2_LOGOFF_HE]));
773 seq_printf(m, "\nTreeConnects: %d sent %d failed",
774 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
775 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
776 seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
777 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
778 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
779 seq_printf(m, "\nCreates: %d sent %d failed",
780 atomic_read(&sent[SMB2_CREATE_HE]),
781 atomic_read(&failed[SMB2_CREATE_HE]));
782 seq_printf(m, "\nCloses: %d sent %d failed",
783 atomic_read(&sent[SMB2_CLOSE_HE]),
784 atomic_read(&failed[SMB2_CLOSE_HE]));
785 seq_printf(m, "\nFlushes: %d sent %d failed",
786 atomic_read(&sent[SMB2_FLUSH_HE]),
787 atomic_read(&failed[SMB2_FLUSH_HE]));
788 seq_printf(m, "\nReads: %d sent %d failed",
789 atomic_read(&sent[SMB2_READ_HE]),
790 atomic_read(&failed[SMB2_READ_HE]));
791 seq_printf(m, "\nWrites: %d sent %d failed",
792 atomic_read(&sent[SMB2_WRITE_HE]),
793 atomic_read(&failed[SMB2_WRITE_HE]));
794 seq_printf(m, "\nLocks: %d sent %d failed",
795 atomic_read(&sent[SMB2_LOCK_HE]),
796 atomic_read(&failed[SMB2_LOCK_HE]));
797 seq_printf(m, "\nIOCTLs: %d sent %d failed",
798 atomic_read(&sent[SMB2_IOCTL_HE]),
799 atomic_read(&failed[SMB2_IOCTL_HE]));
800 seq_printf(m, "\nCancels: %d sent %d failed",
801 atomic_read(&sent[SMB2_CANCEL_HE]),
802 atomic_read(&failed[SMB2_CANCEL_HE]));
803 seq_printf(m, "\nEchos: %d sent %d failed",
804 atomic_read(&sent[SMB2_ECHO_HE]),
805 atomic_read(&failed[SMB2_ECHO_HE]));
806 seq_printf(m, "\nQueryDirectories: %d sent %d failed",
807 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
808 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
809 seq_printf(m, "\nChangeNotifies: %d sent %d failed",
810 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
811 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
812 seq_printf(m, "\nQueryInfos: %d sent %d failed",
813 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
814 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
815 seq_printf(m, "\nSetInfos: %d sent %d failed",
816 atomic_read(&sent[SMB2_SET_INFO_HE]),
817 atomic_read(&failed[SMB2_SET_INFO_HE]));
818 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
819 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
820 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
821#endif
822}
823
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700824static void
825smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
826{
David Howells2b0143b2015-03-17 22:25:59 +0000827 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovsky53ef1012013-09-05 16:11:28 +0400828 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
829
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700830 cfile->fid.persistent_fid = fid->persistent_fid;
831 cfile->fid.volatile_fid = fid->volatile_fid;
Pavel Shilovsky42873b02013-09-05 21:30:16 +0400832 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
833 &fid->purge_cache);
Pavel Shilovsky18cceb62013-09-05 13:01:06 +0400834 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
Aurelien Aptel94f87372016-09-22 07:38:50 +0200835 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700836}
837
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400838static void
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700839smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
840 struct cifs_fid *fid)
841{
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400842 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700843}
844
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -0700845static int
Steve French41c13582013-11-14 00:05:36 -0600846SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
847 u64 persistent_fid, u64 volatile_fid,
848 struct copychunk_ioctl *pcchunk)
849{
850 int rc;
851 unsigned int ret_data_len;
852 struct resume_key_req *res_key;
853
854 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
855 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
856 NULL, 0 /* no input */,
857 (char **)&res_key, &ret_data_len);
858
859 if (rc) {
860 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
861 goto req_res_key_exit;
862 }
863 if (ret_data_len < sizeof(struct resume_key_req)) {
864 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
865 rc = -EINVAL;
866 goto req_res_key_exit;
867 }
868 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
869
870req_res_key_exit:
871 kfree(res_key);
872 return rc;
873}
874
Sachin Prabhu620d8742017-02-10 16:03:51 +0530875static ssize_t
Sachin Prabhu312bbc52017-04-04 02:12:04 -0500876smb2_copychunk_range(const unsigned int xid,
Steve French41c13582013-11-14 00:05:36 -0600877 struct cifsFileInfo *srcfile,
878 struct cifsFileInfo *trgtfile, u64 src_off,
879 u64 len, u64 dest_off)
880{
881 int rc;
882 unsigned int ret_data_len;
883 struct copychunk_ioctl *pcchunk;
Steve French9bf0c9c2013-11-16 18:05:28 -0600884 struct copychunk_ioctl_rsp *retbuf = NULL;
885 struct cifs_tcon *tcon;
886 int chunks_copied = 0;
887 bool chunk_sizes_updated = false;
Sachin Prabhu620d8742017-02-10 16:03:51 +0530888 ssize_t bytes_written, total_bytes_written = 0;
Steve French41c13582013-11-14 00:05:36 -0600889
890 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
891
892 if (pcchunk == NULL)
893 return -ENOMEM;
894
Sachin Prabhu312bbc52017-04-04 02:12:04 -0500895 cifs_dbg(FYI, "in smb2_copychunk_range - about to call request res key\n");
Steve French41c13582013-11-14 00:05:36 -0600896 /* Request a key from the server to identify the source of the copy */
897 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
898 srcfile->fid.persistent_fid,
899 srcfile->fid.volatile_fid, pcchunk);
900
901 /* Note: request_res_key sets res_key null only if rc !=0 */
902 if (rc)
Steve French9bf0c9c2013-11-16 18:05:28 -0600903 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -0600904
905 /* For now array only one chunk long, will make more flexible later */
Fabian Frederickbc09d142014-12-10 15:41:15 -0800906 pcchunk->ChunkCount = cpu_to_le32(1);
Steve French41c13582013-11-14 00:05:36 -0600907 pcchunk->Reserved = 0;
Steve French41c13582013-11-14 00:05:36 -0600908 pcchunk->Reserved2 = 0;
909
Steve French9bf0c9c2013-11-16 18:05:28 -0600910 tcon = tlink_tcon(trgtfile->tlink);
911
912 while (len > 0) {
913 pcchunk->SourceOffset = cpu_to_le64(src_off);
914 pcchunk->TargetOffset = cpu_to_le64(dest_off);
915 pcchunk->Length =
916 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
917
918 /* Request server copy to target from src identified by key */
919 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
Steve French41c13582013-11-14 00:05:36 -0600920 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +0100921 true /* is_fsctl */, (char *)pcchunk,
Steve French9bf0c9c2013-11-16 18:05:28 -0600922 sizeof(struct copychunk_ioctl), (char **)&retbuf,
923 &ret_data_len);
924 if (rc == 0) {
925 if (ret_data_len !=
926 sizeof(struct copychunk_ioctl_rsp)) {
927 cifs_dbg(VFS, "invalid cchunk response size\n");
928 rc = -EIO;
929 goto cchunk_out;
930 }
931 if (retbuf->TotalBytesWritten == 0) {
932 cifs_dbg(FYI, "no bytes copied\n");
933 rc = -EIO;
934 goto cchunk_out;
935 }
936 /*
937 * Check if server claimed to write more than we asked
938 */
939 if (le32_to_cpu(retbuf->TotalBytesWritten) >
940 le32_to_cpu(pcchunk->Length)) {
941 cifs_dbg(VFS, "invalid copy chunk response\n");
942 rc = -EIO;
943 goto cchunk_out;
944 }
945 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
946 cifs_dbg(VFS, "invalid num chunks written\n");
947 rc = -EIO;
948 goto cchunk_out;
949 }
950 chunks_copied++;
Steve French41c13582013-11-14 00:05:36 -0600951
Sachin Prabhu620d8742017-02-10 16:03:51 +0530952 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
953 src_off += bytes_written;
954 dest_off += bytes_written;
955 len -= bytes_written;
956 total_bytes_written += bytes_written;
Steve French41c13582013-11-14 00:05:36 -0600957
Sachin Prabhu620d8742017-02-10 16:03:51 +0530958 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
Steve French9bf0c9c2013-11-16 18:05:28 -0600959 le32_to_cpu(retbuf->ChunksWritten),
960 le32_to_cpu(retbuf->ChunkBytesWritten),
Sachin Prabhu620d8742017-02-10 16:03:51 +0530961 bytes_written);
Steve French9bf0c9c2013-11-16 18:05:28 -0600962 } else if (rc == -EINVAL) {
963 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
964 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -0600965
Steve French9bf0c9c2013-11-16 18:05:28 -0600966 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
967 le32_to_cpu(retbuf->ChunksWritten),
968 le32_to_cpu(retbuf->ChunkBytesWritten),
969 le32_to_cpu(retbuf->TotalBytesWritten));
970
971 /*
972 * Check if this is the first request using these sizes,
973 * (ie check if copy succeed once with original sizes
974 * and check if the server gave us different sizes after
975 * we already updated max sizes on previous request).
976 * if not then why is the server returning an error now
977 */
978 if ((chunks_copied != 0) || chunk_sizes_updated)
979 goto cchunk_out;
980
981 /* Check that server is not asking us to grow size */
982 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
983 tcon->max_bytes_chunk)
984 tcon->max_bytes_chunk =
985 le32_to_cpu(retbuf->ChunkBytesWritten);
986 else
987 goto cchunk_out; /* server gave us bogus size */
988
989 /* No need to change MaxChunks since already set to 1 */
990 chunk_sizes_updated = true;
Sachin Prabhu2477bc52015-02-04 13:10:26 +0000991 } else
992 goto cchunk_out;
Steve French9bf0c9c2013-11-16 18:05:28 -0600993 }
994
995cchunk_out:
Steve French41c13582013-11-14 00:05:36 -0600996 kfree(pcchunk);
Steve French24df1482016-09-29 04:20:23 -0500997 kfree(retbuf);
Sachin Prabhu620d8742017-02-10 16:03:51 +0530998 if (rc)
999 return rc;
1000 else
1001 return total_bytes_written;
Steve French41c13582013-11-14 00:05:36 -06001002}
1003
1004static int
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001005smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1006 struct cifs_fid *fid)
1007{
1008 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1009}
1010
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001011static unsigned int
1012smb2_read_data_offset(char *buf)
1013{
1014 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1015 return rsp->DataOffset;
1016}
1017
1018static unsigned int
Long Li74dcf412017-11-22 17:38:46 -07001019smb2_read_data_length(char *buf, bool in_remaining)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001020{
1021 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
Long Li74dcf412017-11-22 17:38:46 -07001022
1023 if (in_remaining)
1024 return le32_to_cpu(rsp->DataRemaining);
1025
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001026 return le32_to_cpu(rsp->DataLength);
1027}
1028
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001029
1030static int
Steve Frenchdb8b6312014-09-22 05:13:55 -05001031smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001032 struct cifs_io_parms *parms, unsigned int *bytes_read,
1033 char **buf, int *buf_type)
1034{
Steve Frenchdb8b6312014-09-22 05:13:55 -05001035 parms->persistent_fid = pfid->persistent_fid;
1036 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001037 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1038}
1039
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001040static int
Steve Frenchdb8b6312014-09-22 05:13:55 -05001041smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001042 struct cifs_io_parms *parms, unsigned int *written,
1043 struct kvec *iov, unsigned long nr_segs)
1044{
1045
Steve Frenchdb8b6312014-09-22 05:13:55 -05001046 parms->persistent_fid = pfid->persistent_fid;
1047 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001048 return SMB2_write(xid, parms, written, iov, nr_segs);
1049}
1050
Steve Frenchd43cc792014-08-13 17:16:29 -05001051/* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1052static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1053 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1054{
1055 struct cifsInodeInfo *cifsi;
1056 int rc;
1057
1058 cifsi = CIFS_I(inode);
1059
1060 /* if file already sparse don't bother setting sparse again */
1061 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1062 return true; /* already sparse */
1063
1064 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1065 return true; /* already not sparse */
1066
1067 /*
1068 * Can't check for sparse support on share the usual way via the
1069 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1070 * since Samba server doesn't set the flag on the share, yet
1071 * supports the set sparse FSCTL and returns sparse correctly
1072 * in the file attributes. If we fail setting sparse though we
1073 * mark that server does not support sparse files for this share
1074 * to avoid repeatedly sending the unsupported fsctl to server
1075 * if the file is repeatedly extended.
1076 */
1077 if (tcon->broken_sparse_sup)
1078 return false;
1079
1080 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1081 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001082 true /* is_fctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001083 &setsparse, 1, NULL, NULL);
Steve Frenchd43cc792014-08-13 17:16:29 -05001084 if (rc) {
1085 tcon->broken_sparse_sup = true;
1086 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1087 return false;
1088 }
1089
1090 if (setsparse)
1091 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1092 else
1093 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1094
1095 return true;
1096}
1097
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001098static int
1099smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1100 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1101{
1102 __le64 eof = cpu_to_le64(size);
Steve French3d1a3742014-08-11 21:05:25 -05001103 struct inode *inode;
1104
1105 /*
1106 * If extending file more than one page make sparse. Many Linux fs
1107 * make files sparse by default when extending via ftruncate
1108 */
David Howells2b0143b2015-03-17 22:25:59 +00001109 inode = d_inode(cfile->dentry);
Steve French3d1a3742014-08-11 21:05:25 -05001110
1111 if (!set_alloc && (size > inode->i_size + 8192)) {
Steve French3d1a3742014-08-11 21:05:25 -05001112 __u8 set_sparse = 1;
Steve French3d1a3742014-08-11 21:05:25 -05001113
Steve Frenchd43cc792014-08-13 17:16:29 -05001114 /* whether set sparse succeeds or not, extend the file */
1115 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
Steve French3d1a3742014-08-11 21:05:25 -05001116 }
1117
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001118 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05001119 cfile->fid.volatile_fid, cfile->pid, &eof, false);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001120}
1121
Steve French02b16662015-06-27 21:18:36 -07001122static int
1123smb2_duplicate_extents(const unsigned int xid,
1124 struct cifsFileInfo *srcfile,
1125 struct cifsFileInfo *trgtfile, u64 src_off,
1126 u64 len, u64 dest_off)
1127{
1128 int rc;
1129 unsigned int ret_data_len;
Steve French02b16662015-06-27 21:18:36 -07001130 struct duplicate_extents_to_file dup_ext_buf;
1131 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1132
1133 /* server fileays advertise duplicate extent support with this flag */
1134 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1135 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1136 return -EOPNOTSUPP;
1137
1138 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1139 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1140 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1141 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1142 dup_ext_buf.ByteCount = cpu_to_le64(len);
1143 cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
1144 src_off, dest_off, len);
1145
1146 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1147 if (rc)
1148 goto duplicate_extents_out;
1149
1150 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1151 trgtfile->fid.volatile_fid,
1152 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001153 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001154 (char *)&dup_ext_buf,
Steve French02b16662015-06-27 21:18:36 -07001155 sizeof(struct duplicate_extents_to_file),
Steve French24df1482016-09-29 04:20:23 -05001156 NULL,
Steve French02b16662015-06-27 21:18:36 -07001157 &ret_data_len);
1158
1159 if (ret_data_len > 0)
1160 cifs_dbg(FYI, "non-zero response length in duplicate extents");
1161
1162duplicate_extents_out:
1163 return rc;
1164}
Steve French02b16662015-06-27 21:18:36 -07001165
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001166static int
Steve French64a5cfa2013-10-14 15:31:32 -05001167smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1168 struct cifsFileInfo *cfile)
1169{
1170 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1171 cfile->fid.volatile_fid);
1172}
1173
1174static int
Steve Frenchb3152e22015-06-24 03:17:02 -05001175smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1176 struct cifsFileInfo *cfile)
1177{
1178 struct fsctl_set_integrity_information_req integr_info;
Steve Frenchb3152e22015-06-24 03:17:02 -05001179 unsigned int ret_data_len;
1180
1181 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1182 integr_info.Flags = 0;
1183 integr_info.Reserved = 0;
1184
1185 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1186 cfile->fid.volatile_fid,
1187 FSCTL_SET_INTEGRITY_INFORMATION,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001188 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001189 (char *)&integr_info,
Steve Frenchb3152e22015-06-24 03:17:02 -05001190 sizeof(struct fsctl_set_integrity_information_req),
Steve French24df1482016-09-29 04:20:23 -05001191 NULL,
Steve Frenchb3152e22015-06-24 03:17:02 -05001192 &ret_data_len);
1193
1194}
1195
1196static int
Steve French834170c2016-09-30 21:14:26 -05001197smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1198 struct cifsFileInfo *cfile, void __user *ioc_buf)
1199{
1200 char *retbuf = NULL;
1201 unsigned int ret_data_len = 0;
1202 int rc;
1203 struct smb_snapshot_array snapshot_in;
1204
1205 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1206 cfile->fid.volatile_fid,
1207 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001208 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001209 NULL, 0 /* no input data */,
Steve French834170c2016-09-30 21:14:26 -05001210 (char **)&retbuf,
1211 &ret_data_len);
1212 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1213 rc, ret_data_len);
1214 if (rc)
1215 return rc;
1216
1217 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1218 /* Fixup buffer */
1219 if (copy_from_user(&snapshot_in, ioc_buf,
1220 sizeof(struct smb_snapshot_array))) {
1221 rc = -EFAULT;
1222 kfree(retbuf);
1223 return rc;
1224 }
1225 if (snapshot_in.snapshot_array_size < sizeof(struct smb_snapshot_array)) {
1226 rc = -ERANGE;
David Disseldorp0e5c7952017-05-03 17:39:09 +02001227 kfree(retbuf);
Steve French834170c2016-09-30 21:14:26 -05001228 return rc;
1229 }
1230
1231 if (ret_data_len > snapshot_in.snapshot_array_size)
1232 ret_data_len = snapshot_in.snapshot_array_size;
1233
1234 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1235 rc = -EFAULT;
1236 }
1237
1238 kfree(retbuf);
1239 return rc;
1240}
1241
1242static int
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001243smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1244 const char *path, struct cifs_sb_info *cifs_sb,
1245 struct cifs_fid *fid, __u16 search_flags,
1246 struct cifs_search_info *srch_inf)
1247{
1248 __le16 *utf16_path;
1249 int rc;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001250 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001251 struct cifs_open_parms oparms;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001252
1253 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1254 if (!utf16_path)
1255 return -ENOMEM;
1256
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001257 oparms.tcon = tcon;
1258 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1259 oparms.disposition = FILE_OPEN;
1260 oparms.create_options = 0;
1261 oparms.fid = fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001262 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001263
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001264 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001265 kfree(utf16_path);
1266 if (rc) {
Pavel Shilovskydcd878382017-06-06 16:58:58 -07001267 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001268 return rc;
1269 }
1270
1271 srch_inf->entries_in_buffer = 0;
1272 srch_inf->index_of_last_entry = 0;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001273
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001274 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1275 fid->volatile_fid, 0, srch_inf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001276 if (rc) {
Pavel Shilovskydcd878382017-06-06 16:58:58 -07001277 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001278 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001279 }
1280 return rc;
1281}
1282
1283static int
1284smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1285 struct cifs_fid *fid, __u16 search_flags,
1286 struct cifs_search_info *srch_inf)
1287{
1288 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1289 fid->volatile_fid, 0, srch_inf);
1290}
1291
1292static int
1293smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1294 struct cifs_fid *fid)
1295{
1296 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1297}
1298
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001299/*
1300* If we negotiate SMB2 protocol and get STATUS_PENDING - update
1301* the number of credits and return true. Otherwise - return false.
1302*/
1303static bool
1304smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
1305{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001306 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001307
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001308 if (shdr->Status != STATUS_PENDING)
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001309 return false;
1310
1311 if (!length) {
1312 spin_lock(&server->req_lock);
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001313 server->credits += le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001314 spin_unlock(&server->req_lock);
1315 wake_up(&server->request_q);
1316 }
1317
1318 return true;
1319}
1320
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001321static bool
1322smb2_is_session_expired(char *buf)
1323{
1324 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
1325
Mark Symsd81243c2018-05-24 09:47:31 +01001326 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
1327 shdr->Status != STATUS_USER_SESSION_DELETED)
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001328 return false;
1329
Mark Symsd81243c2018-05-24 09:47:31 +01001330 cifs_dbg(FYI, "Session expired or deleted\n");
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001331 return true;
1332}
1333
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001334static int
1335smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
1336 struct cifsInodeInfo *cinode)
1337{
Pavel Shilovsky0822f512012-09-19 06:22:45 -07001338 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
1339 return SMB2_lease_break(0, tcon, cinode->lease_key,
1340 smb2_get_lease_state(cinode));
1341
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001342 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
1343 fid->volatile_fid,
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001344 CIFS_CACHE_READ(cinode) ? 1 : 0);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001345}
1346
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001347static int
1348smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1349 struct kstatfs *buf)
1350{
1351 int rc;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001352 __le16 srch_path = 0; /* Null - open root of share */
1353 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001354 struct cifs_open_parms oparms;
1355 struct cifs_fid fid;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001356
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001357 oparms.tcon = tcon;
1358 oparms.desired_access = FILE_READ_ATTRIBUTES;
1359 oparms.disposition = FILE_OPEN;
1360 oparms.create_options = 0;
1361 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001362 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001363
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001364 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001365 if (rc)
1366 return rc;
1367 buf->f_type = SMB2_MAGIC_NUMBER;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001368 rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1369 buf);
1370 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001371 return rc;
1372}
1373
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07001374static bool
1375smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
1376{
1377 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
1378 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
1379}
1380
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07001381static int
1382smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
1383 __u64 length, __u32 type, int lock, int unlock, bool wait)
1384{
1385 if (unlock && !lock)
1386 type = SMB2_LOCKFLAG_UNLOCK;
1387 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
1388 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
1389 current->tgid, length, offset, type, wait);
1390}
1391
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001392static void
1393smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
1394{
1395 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
1396}
1397
1398static void
1399smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
1400{
1401 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
1402}
1403
1404static void
1405smb2_new_lease_key(struct cifs_fid *fid)
1406{
Steve Frenchfa70b872016-09-22 00:39:34 -05001407 generate_random_uuid(fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001408}
1409
Aurelien Aptel9d496402017-02-13 16:16:49 +01001410static int
1411smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
1412 const char *search_name,
1413 struct dfs_info3_param **target_nodes,
1414 unsigned int *num_of_nodes,
1415 const struct nls_table *nls_codepage, int remap)
1416{
1417 int rc;
1418 __le16 *utf16_path = NULL;
1419 int utf16_path_len = 0;
1420 struct cifs_tcon *tcon;
1421 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
1422 struct get_dfs_referral_rsp *dfs_rsp = NULL;
1423 u32 dfs_req_size = 0, dfs_rsp_size = 0;
1424
1425 cifs_dbg(FYI, "smb2_get_dfs_refer path <%s>\n", search_name);
1426
1427 /*
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001428 * Try to use the IPC tcon, otherwise just use any
Aurelien Aptel9d496402017-02-13 16:16:49 +01001429 */
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001430 tcon = ses->tcon_ipc;
1431 if (tcon == NULL) {
1432 spin_lock(&cifs_tcp_ses_lock);
1433 tcon = list_first_entry_or_null(&ses->tcon_list,
1434 struct cifs_tcon,
1435 tcon_list);
1436 if (tcon)
1437 tcon->tc_count++;
1438 spin_unlock(&cifs_tcp_ses_lock);
1439 }
Aurelien Aptel9d496402017-02-13 16:16:49 +01001440
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001441 if (tcon == NULL) {
Aurelien Aptel9d496402017-02-13 16:16:49 +01001442 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
1443 ses);
1444 rc = -ENOTCONN;
1445 goto out;
1446 }
1447
1448 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
1449 &utf16_path_len,
1450 nls_codepage, remap);
1451 if (!utf16_path) {
1452 rc = -ENOMEM;
1453 goto out;
1454 }
1455
1456 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
1457 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
1458 if (!dfs_req) {
1459 rc = -ENOMEM;
1460 goto out;
1461 }
1462
1463 /* Highest DFS referral version understood */
1464 dfs_req->MaxReferralLevel = DFS_VERSION;
1465
1466 /* Path to resolve in an UTF-16 null-terminated string */
1467 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
1468
1469 do {
Aurelien Aptel9d496402017-02-13 16:16:49 +01001470 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1471 FSCTL_DFS_GET_REFERRALS,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001472 true /* is_fsctl */,
Aurelien Aptel9d496402017-02-13 16:16:49 +01001473 (char *)dfs_req, dfs_req_size,
1474 (char **)&dfs_rsp, &dfs_rsp_size);
Aurelien Aptel9d496402017-02-13 16:16:49 +01001475 } while (rc == -EAGAIN);
1476
1477 if (rc) {
Steve French2564f2f2018-03-21 23:16:36 -05001478 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
Aurelien Aptel57025912017-11-21 14:47:56 +01001479 cifs_dbg(VFS, "ioctl error in smb2_get_dfs_refer rc=%d\n", rc);
Aurelien Aptel9d496402017-02-13 16:16:49 +01001480 goto out;
1481 }
1482
1483 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
1484 num_of_nodes, target_nodes,
1485 nls_codepage, remap, search_name,
1486 true /* is_unicode */);
1487 if (rc) {
1488 cifs_dbg(VFS, "parse error in smb2_get_dfs_refer rc=%d\n", rc);
1489 goto out;
1490 }
1491
1492 out:
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001493 if (tcon && !tcon->ipc) {
1494 /* ipc tcons are not refcounted */
Aurelien Aptel9d496402017-02-13 16:16:49 +01001495 spin_lock(&cifs_tcp_ses_lock);
1496 tcon->tc_count--;
1497 spin_unlock(&cifs_tcp_ses_lock);
1498 }
1499 kfree(utf16_path);
1500 kfree(dfs_req);
1501 kfree(dfs_rsp);
1502 return rc;
1503}
Pavel Shilovsky78932422016-07-24 10:37:38 +03001504#define SMB2_SYMLINK_STRUCT_SIZE \
1505 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
1506
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001507static int
1508smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
1509 const char *full_path, char **target_path,
1510 struct cifs_sb_info *cifs_sb)
1511{
1512 int rc;
1513 __le16 *utf16_path;
1514 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1515 struct cifs_open_parms oparms;
1516 struct cifs_fid fid;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001517 struct kvec err_iov = {NULL, 0};
Gustavo A. R. Silva0d568cd2018-04-13 10:13:29 -05001518 struct smb2_err_rsp *err_buf;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001519 struct smb2_symlink_err_rsp *symlink;
Pavel Shilovsky78932422016-07-24 10:37:38 +03001520 unsigned int sub_len;
1521 unsigned int sub_offset;
1522 unsigned int print_len;
1523 unsigned int print_offset;
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001524 struct cifs_ses *ses = tcon->ses;
1525 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001526
1527 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
1528
1529 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1530 if (!utf16_path)
1531 return -ENOMEM;
1532
1533 oparms.tcon = tcon;
1534 oparms.desired_access = FILE_READ_ATTRIBUTES;
1535 oparms.disposition = FILE_OPEN;
1536 oparms.create_options = 0;
1537 oparms.fid = &fid;
1538 oparms.reconnect = false;
1539
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001540 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_iov);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001541
Gustavo A. R. Silva0d568cd2018-04-13 10:13:29 -05001542 if (!rc || !err_iov.iov_base) {
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001543 kfree(utf16_path);
1544 return -ENOENT;
1545 }
Pavel Shilovsky78932422016-07-24 10:37:38 +03001546
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001547 err_buf = err_iov.iov_base;
Pavel Shilovsky78932422016-07-24 10:37:38 +03001548 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001549 err_iov.iov_len + server->vals->header_preamble_size < SMB2_SYMLINK_STRUCT_SIZE) {
Pavel Shilovsky78932422016-07-24 10:37:38 +03001550 kfree(utf16_path);
1551 return -ENOENT;
1552 }
1553
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001554 /* open must fail on symlink - reset rc */
1555 rc = 0;
1556 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
1557 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
1558 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
Pavel Shilovsky78932422016-07-24 10:37:38 +03001559 print_len = le16_to_cpu(symlink->PrintNameLength);
1560 print_offset = le16_to_cpu(symlink->PrintNameOffset);
1561
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001562 if (err_iov.iov_len + server->vals->header_preamble_size <
Pavel Shilovsky78932422016-07-24 10:37:38 +03001563 SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
1564 kfree(utf16_path);
1565 return -ENOENT;
1566 }
1567
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001568 if (err_iov.iov_len + server->vals->header_preamble_size <
Pavel Shilovsky78932422016-07-24 10:37:38 +03001569 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
1570 kfree(utf16_path);
1571 return -ENOENT;
1572 }
1573
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001574 *target_path = cifs_strndup_from_utf16(
1575 (char *)symlink->PathBuffer + sub_offset,
1576 sub_len, true, cifs_sb->local_nls);
1577 if (!(*target_path)) {
1578 kfree(utf16_path);
1579 return -ENOMEM;
1580 }
1581 convert_delimiter(*target_path, '/');
1582 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
1583 kfree(utf16_path);
1584 return rc;
1585}
1586
Arnd Bergmann84908422017-06-27 17:06:13 +02001587#ifdef CONFIG_CIFS_ACL
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001588static struct cifs_ntsd *
1589get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
1590 const struct cifs_fid *cifsfid, u32 *pacllen)
1591{
1592 struct cifs_ntsd *pntsd = NULL;
1593 unsigned int xid;
1594 int rc = -EOPNOTSUPP;
1595 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1596
1597 if (IS_ERR(tlink))
1598 return ERR_CAST(tlink);
1599
1600 xid = get_xid();
1601 cifs_dbg(FYI, "trying to get acl\n");
1602
1603 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
1604 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
1605 free_xid(xid);
1606
1607 cifs_put_tlink(tlink);
1608
1609 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
1610 if (rc)
1611 return ERR_PTR(rc);
1612 return pntsd;
1613
1614}
1615
1616static struct cifs_ntsd *
1617get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
1618 const char *path, u32 *pacllen)
1619{
1620 struct cifs_ntsd *pntsd = NULL;
1621 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1622 unsigned int xid;
1623 int rc;
1624 struct cifs_tcon *tcon;
1625 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1626 struct cifs_fid fid;
1627 struct cifs_open_parms oparms;
1628 __le16 *utf16_path;
1629
1630 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
1631 if (IS_ERR(tlink))
1632 return ERR_CAST(tlink);
1633
1634 tcon = tlink_tcon(tlink);
1635 xid = get_xid();
1636
1637 if (backup_cred(cifs_sb))
Colin Ian King709340a2017-07-05 13:47:34 +01001638 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001639 else
1640 oparms.create_options = 0;
1641
1642 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
Steve Frenchcfe89092018-05-19 02:04:55 -05001643 if (!utf16_path) {
1644 rc = -ENOMEM;
1645 free_xid(xid);
1646 return ERR_PTR(rc);
1647 }
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001648
1649 oparms.tcon = tcon;
1650 oparms.desired_access = READ_CONTROL;
1651 oparms.disposition = FILE_OPEN;
1652 oparms.fid = &fid;
1653 oparms.reconnect = false;
1654
1655 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
1656 kfree(utf16_path);
1657 if (!rc) {
1658 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
1659 fid.volatile_fid, (void **)&pntsd, pacllen);
1660 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1661 }
1662
1663 cifs_put_tlink(tlink);
1664 free_xid(xid);
1665
1666 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
1667 if (rc)
1668 return ERR_PTR(rc);
1669 return pntsd;
1670}
1671
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05001672#ifdef CONFIG_CIFS_ACL
1673static int
1674set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
1675 struct inode *inode, const char *path, int aclflag)
1676{
1677 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1678 unsigned int xid;
1679 int rc, access_flags = 0;
1680 struct cifs_tcon *tcon;
1681 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1682 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1683 struct cifs_fid fid;
1684 struct cifs_open_parms oparms;
1685 __le16 *utf16_path;
1686
1687 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
1688 if (IS_ERR(tlink))
1689 return PTR_ERR(tlink);
1690
1691 tcon = tlink_tcon(tlink);
1692 xid = get_xid();
1693
1694 if (backup_cred(cifs_sb))
1695 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1696 else
1697 oparms.create_options = 0;
1698
1699 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
1700 access_flags = WRITE_OWNER;
1701 else
1702 access_flags = WRITE_DAC;
1703
1704 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
Steve Frenchcfe89092018-05-19 02:04:55 -05001705 if (!utf16_path) {
1706 rc = -ENOMEM;
1707 free_xid(xid);
1708 return rc;
1709 }
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05001710
1711 oparms.tcon = tcon;
1712 oparms.desired_access = access_flags;
1713 oparms.disposition = FILE_OPEN;
1714 oparms.path = path;
1715 oparms.fid = &fid;
1716 oparms.reconnect = false;
1717
1718 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
1719 kfree(utf16_path);
1720 if (!rc) {
1721 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
1722 fid.volatile_fid, pnntsd, acllen, aclflag);
1723 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1724 }
1725
1726 cifs_put_tlink(tlink);
1727 free_xid(xid);
1728 return rc;
1729}
1730#endif /* CIFS_ACL */
1731
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001732/* Retrieve an ACL from the server */
1733static struct cifs_ntsd *
1734get_smb2_acl(struct cifs_sb_info *cifs_sb,
1735 struct inode *inode, const char *path,
1736 u32 *pacllen)
1737{
1738 struct cifs_ntsd *pntsd = NULL;
1739 struct cifsFileInfo *open_file = NULL;
1740
1741 if (inode)
1742 open_file = find_readable_file(CIFS_I(inode), true);
1743 if (!open_file)
1744 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
1745
1746 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
1747 cifsFileInfo_put(open_file);
1748 return pntsd;
1749}
Arnd Bergmann84908422017-06-27 17:06:13 +02001750#endif
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001751
Steve French30175622014-08-17 18:16:40 -05001752static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
1753 loff_t offset, loff_t len, bool keep_size)
1754{
1755 struct inode *inode;
1756 struct cifsInodeInfo *cifsi;
1757 struct cifsFileInfo *cfile = file->private_data;
1758 struct file_zero_data_information fsctl_buf;
1759 long rc;
1760 unsigned int xid;
1761
1762 xid = get_xid();
1763
David Howells2b0143b2015-03-17 22:25:59 +00001764 inode = d_inode(cfile->dentry);
Steve French30175622014-08-17 18:16:40 -05001765 cifsi = CIFS_I(inode);
1766
1767 /* if file not oplocked can't be sure whether asking to extend size */
1768 if (!CIFS_CACHE_READ(cifsi))
Steve Frenchcfe89092018-05-19 02:04:55 -05001769 if (keep_size == false) {
1770 rc = -EOPNOTSUPP;
1771 free_xid(xid);
1772 return rc;
1773 }
Steve French30175622014-08-17 18:16:40 -05001774
Steve French2bb93d22014-08-20 18:56:29 -05001775 /*
Steve French30175622014-08-17 18:16:40 -05001776 * Must check if file sparse since fallocate -z (zero range) assumes
1777 * non-sparse allocation
1778 */
Steve Frenchcfe89092018-05-19 02:04:55 -05001779 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
1780 rc = -EOPNOTSUPP;
1781 free_xid(xid);
1782 return rc;
1783 }
Steve French30175622014-08-17 18:16:40 -05001784
1785 /*
1786 * need to make sure we are not asked to extend the file since the SMB3
1787 * fsctl does not change the file size. In the future we could change
1788 * this to zero the first part of the range then set the file size
1789 * which for a non sparse file would zero the newly extended range
1790 */
1791 if (keep_size == false)
Steve Frenchcfe89092018-05-19 02:04:55 -05001792 if (i_size_read(inode) < offset + len) {
1793 rc = -EOPNOTSUPP;
1794 free_xid(xid);
1795 return rc;
1796 }
Steve French30175622014-08-17 18:16:40 -05001797
1798 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1799
1800 fsctl_buf.FileOffset = cpu_to_le64(offset);
1801 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1802
1803 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1804 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001805 true /* is_fctl */, (char *)&fsctl_buf,
Steve French30175622014-08-17 18:16:40 -05001806 sizeof(struct file_zero_data_information), NULL, NULL);
1807 free_xid(xid);
1808 return rc;
1809}
1810
Steve French31742c52014-08-17 08:38:47 -05001811static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
1812 loff_t offset, loff_t len)
1813{
1814 struct inode *inode;
1815 struct cifsInodeInfo *cifsi;
1816 struct cifsFileInfo *cfile = file->private_data;
1817 struct file_zero_data_information fsctl_buf;
1818 long rc;
1819 unsigned int xid;
1820 __u8 set_sparse = 1;
1821
1822 xid = get_xid();
1823
David Howells2b0143b2015-03-17 22:25:59 +00001824 inode = d_inode(cfile->dentry);
Steve French31742c52014-08-17 08:38:47 -05001825 cifsi = CIFS_I(inode);
1826
1827 /* Need to make file sparse, if not already, before freeing range. */
1828 /* Consider adding equivalent for compressed since it could also work */
Steve Frenchcfe89092018-05-19 02:04:55 -05001829 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
1830 rc = -EOPNOTSUPP;
1831 free_xid(xid);
1832 return rc;
1833 }
Steve French31742c52014-08-17 08:38:47 -05001834
1835 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1836
1837 fsctl_buf.FileOffset = cpu_to_le64(offset);
1838 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1839
1840 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1841 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001842 true /* is_fctl */, (char *)&fsctl_buf,
Steve French31742c52014-08-17 08:38:47 -05001843 sizeof(struct file_zero_data_information), NULL, NULL);
1844 free_xid(xid);
1845 return rc;
1846}
1847
Steve French9ccf3212014-10-18 17:01:15 -05001848static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
1849 loff_t off, loff_t len, bool keep_size)
1850{
1851 struct inode *inode;
1852 struct cifsInodeInfo *cifsi;
1853 struct cifsFileInfo *cfile = file->private_data;
1854 long rc = -EOPNOTSUPP;
1855 unsigned int xid;
1856
1857 xid = get_xid();
1858
David Howells2b0143b2015-03-17 22:25:59 +00001859 inode = d_inode(cfile->dentry);
Steve French9ccf3212014-10-18 17:01:15 -05001860 cifsi = CIFS_I(inode);
1861
1862 /* if file not oplocked can't be sure whether asking to extend size */
1863 if (!CIFS_CACHE_READ(cifsi))
Steve Frenchcfe89092018-05-19 02:04:55 -05001864 if (keep_size == false) {
1865 free_xid(xid);
1866 return rc;
1867 }
Steve French9ccf3212014-10-18 17:01:15 -05001868
1869 /*
1870 * Files are non-sparse by default so falloc may be a no-op
1871 * Must check if file sparse. If not sparse, and not extending
1872 * then no need to do anything since file already allocated
1873 */
1874 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
1875 if (keep_size == true)
Steve Frenchcfe89092018-05-19 02:04:55 -05001876 rc = 0;
Steve French9ccf3212014-10-18 17:01:15 -05001877 /* check if extending file */
1878 else if (i_size_read(inode) >= off + len)
1879 /* not extending file and already not sparse */
Steve Frenchcfe89092018-05-19 02:04:55 -05001880 rc = 0;
Steve French9ccf3212014-10-18 17:01:15 -05001881 /* BB: in future add else clause to extend file */
1882 else
Steve Frenchcfe89092018-05-19 02:04:55 -05001883 rc = -EOPNOTSUPP;
1884 free_xid(xid);
1885 return rc;
Steve French9ccf3212014-10-18 17:01:15 -05001886 }
1887
1888 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
1889 /*
1890 * Check if falloc starts within first few pages of file
1891 * and ends within a few pages of the end of file to
1892 * ensure that most of file is being forced to be
1893 * fallocated now. If so then setting whole file sparse
1894 * ie potentially making a few extra pages at the beginning
1895 * or end of the file non-sparse via set_sparse is harmless.
1896 */
Steve Frenchcfe89092018-05-19 02:04:55 -05001897 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
1898 rc = -EOPNOTSUPP;
1899 free_xid(xid);
1900 return rc;
1901 }
Steve French9ccf3212014-10-18 17:01:15 -05001902
1903 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
1904 }
1905 /* BB: else ... in future add code to extend file and set sparse */
1906
1907
1908 free_xid(xid);
1909 return rc;
1910}
1911
1912
Steve French31742c52014-08-17 08:38:47 -05001913static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
1914 loff_t off, loff_t len)
1915{
1916 /* KEEP_SIZE already checked for by do_fallocate */
1917 if (mode & FALLOC_FL_PUNCH_HOLE)
1918 return smb3_punch_hole(file, tcon, off, len);
Steve French30175622014-08-17 18:16:40 -05001919 else if (mode & FALLOC_FL_ZERO_RANGE) {
1920 if (mode & FALLOC_FL_KEEP_SIZE)
1921 return smb3_zero_range(file, tcon, off, len, true);
1922 return smb3_zero_range(file, tcon, off, len, false);
Steve French9ccf3212014-10-18 17:01:15 -05001923 } else if (mode == FALLOC_FL_KEEP_SIZE)
1924 return smb3_simple_falloc(file, tcon, off, len, true);
1925 else if (mode == 0)
1926 return smb3_simple_falloc(file, tcon, off, len, false);
Steve French31742c52014-08-17 08:38:47 -05001927
1928 return -EOPNOTSUPP;
1929}
1930
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001931static void
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00001932smb2_downgrade_oplock(struct TCP_Server_Info *server,
1933 struct cifsInodeInfo *cinode, bool set_level2)
1934{
1935 if (set_level2)
1936 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
1937 0, NULL);
1938 else
1939 server->ops->set_oplock_level(cinode, 0, 0, NULL);
1940}
1941
1942static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001943smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1944 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001945{
1946 oplock &= 0xFF;
1947 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1948 return;
1949 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001950 cinode->oplock = CIFS_CACHE_RHW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001951 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
1952 &cinode->vfs_inode);
1953 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001954 cinode->oplock = CIFS_CACHE_RW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001955 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
1956 &cinode->vfs_inode);
1957 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
1958 cinode->oplock = CIFS_CACHE_READ_FLG;
1959 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
1960 &cinode->vfs_inode);
1961 } else
1962 cinode->oplock = 0;
1963}
1964
1965static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001966smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1967 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001968{
1969 char message[5] = {0};
1970
1971 oplock &= 0xFF;
1972 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1973 return;
1974
1975 cinode->oplock = 0;
1976 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
1977 cinode->oplock |= CIFS_CACHE_READ_FLG;
1978 strcat(message, "R");
1979 }
1980 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
1981 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
1982 strcat(message, "H");
1983 }
1984 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
1985 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
1986 strcat(message, "W");
1987 }
1988 if (!cinode->oplock)
1989 strcat(message, "None");
1990 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
1991 &cinode->vfs_inode);
1992}
1993
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001994static void
1995smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1996 unsigned int epoch, bool *purge_cache)
1997{
1998 unsigned int old_oplock = cinode->oplock;
1999
2000 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
2001
2002 if (purge_cache) {
2003 *purge_cache = false;
2004 if (old_oplock == CIFS_CACHE_READ_FLG) {
2005 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
2006 (epoch - cinode->epoch > 0))
2007 *purge_cache = true;
2008 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2009 (epoch - cinode->epoch > 1))
2010 *purge_cache = true;
2011 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2012 (epoch - cinode->epoch > 1))
2013 *purge_cache = true;
2014 else if (cinode->oplock == 0 &&
2015 (epoch - cinode->epoch > 0))
2016 *purge_cache = true;
2017 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
2018 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2019 (epoch - cinode->epoch > 0))
2020 *purge_cache = true;
2021 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2022 (epoch - cinode->epoch > 1))
2023 *purge_cache = true;
2024 }
2025 cinode->epoch = epoch;
2026 }
2027}
2028
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002029static bool
2030smb2_is_read_op(__u32 oplock)
2031{
2032 return oplock == SMB2_OPLOCK_LEVEL_II;
2033}
2034
2035static bool
2036smb21_is_read_op(__u32 oplock)
2037{
2038 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
2039 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
2040}
2041
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002042static __le32
2043map_oplock_to_lease(u8 oplock)
2044{
2045 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
2046 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
2047 else if (oplock == SMB2_OPLOCK_LEVEL_II)
2048 return SMB2_LEASE_READ_CACHING;
2049 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
2050 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
2051 SMB2_LEASE_WRITE_CACHING;
2052 return 0;
2053}
2054
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002055static char *
2056smb2_create_lease_buf(u8 *lease_key, u8 oplock)
2057{
2058 struct create_lease *buf;
2059
2060 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
2061 if (!buf)
2062 return NULL;
2063
2064 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
2065 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002066 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002067
2068 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2069 (struct create_lease, lcontext));
2070 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
2071 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2072 (struct create_lease, Name));
2073 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07002074 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002075 buf->Name[0] = 'R';
2076 buf->Name[1] = 'q';
2077 buf->Name[2] = 'L';
2078 buf->Name[3] = 's';
2079 return (char *)buf;
2080}
2081
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002082static char *
2083smb3_create_lease_buf(u8 *lease_key, u8 oplock)
2084{
2085 struct create_lease_v2 *buf;
2086
2087 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
2088 if (!buf)
2089 return NULL;
2090
2091 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
2092 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
2093 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2094
2095 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2096 (struct create_lease_v2, lcontext));
2097 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
2098 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2099 (struct create_lease_v2, Name));
2100 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07002101 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002102 buf->Name[0] = 'R';
2103 buf->Name[1] = 'q';
2104 buf->Name[2] = 'L';
2105 buf->Name[3] = 's';
2106 return (char *)buf;
2107}
2108
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002109static __u8
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06002110smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002111{
2112 struct create_lease *lc = (struct create_lease *)buf;
2113
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002114 *epoch = 0; /* not used */
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002115 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2116 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2117 return le32_to_cpu(lc->lcontext.LeaseState);
2118}
2119
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002120static __u8
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06002121smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002122{
2123 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
2124
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002125 *epoch = le16_to_cpu(lc->lcontext.Epoch);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002126 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2127 return SMB2_OPLOCK_LEVEL_NOCHANGE;
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06002128 if (lease_key)
2129 memcpy(lease_key, &lc->lcontext.LeaseKeyLow,
2130 SMB2_LEASE_KEY_SIZE);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002131 return le32_to_cpu(lc->lcontext.LeaseState);
2132}
2133
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002134static unsigned int
2135smb2_wp_retry_size(struct inode *inode)
2136{
2137 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
2138 SMB2_MAX_BUFFER_SIZE);
2139}
2140
Pavel Shilovsky52755802014-08-18 20:49:57 +04002141static bool
2142smb2_dir_needs_close(struct cifsFileInfo *cfile)
2143{
2144 return !cfile->invalidHandle;
2145}
2146
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002147static void
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002148fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
2149 struct smb_rqst *old_rq)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002150{
2151 struct smb2_sync_hdr *shdr =
2152 (struct smb2_sync_hdr *)old_rq->rq_iov[1].iov_base;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002153
2154 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
2155 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
2156 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
2157 tr_hdr->Flags = cpu_to_le16(0x01);
2158 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2159 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002160}
2161
Ronnie Sahlberg262916b2018-02-20 12:45:21 +11002162/* We can not use the normal sg_set_buf() as we will sometimes pass a
2163 * stack object as buf.
2164 */
2165static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
2166 unsigned int buflen)
2167{
2168 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
2169}
2170
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002171/* Assumes:
2172 * rqst->rq_iov[0] is rfc1002 length
2173 * rqst->rq_iov[1] is tranform header
2174 * rqst->rq_iov[2+] data to be encrypted/decrypted
2175 */
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002176static struct scatterlist *
2177init_sg(struct smb_rqst *rqst, u8 *sign)
2178{
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002179 unsigned int sg_len = rqst->rq_nvec + rqst->rq_npages;
2180 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002181 struct scatterlist *sg;
2182 unsigned int i;
2183 unsigned int j;
2184
2185 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
2186 if (!sg)
2187 return NULL;
2188
2189 sg_init_table(sg, sg_len);
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002190 smb2_sg_set_buf(&sg[0], rqst->rq_iov[1].iov_base + 20, assoc_data_len);
2191 for (i = 1; i < rqst->rq_nvec - 1; i++)
2192 smb2_sg_set_buf(&sg[i], rqst->rq_iov[i+1].iov_base,
2193 rqst->rq_iov[i+1].iov_len);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002194 for (j = 0; i < sg_len - 1; i++, j++) {
2195 unsigned int len = (j < rqst->rq_npages - 1) ? rqst->rq_pagesz
2196 : rqst->rq_tailsz;
2197 sg_set_page(&sg[i], rqst->rq_pages[j], len, 0);
2198 }
Ronnie Sahlberg262916b2018-02-20 12:45:21 +11002199 smb2_sg_set_buf(&sg[sg_len - 1], sign, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002200 return sg;
2201}
2202
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002203static int
2204smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
2205{
2206 struct cifs_ses *ses;
2207 u8 *ses_enc_key;
2208
2209 spin_lock(&cifs_tcp_ses_lock);
2210 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2211 if (ses->Suid != ses_id)
2212 continue;
2213 ses_enc_key = enc ? ses->smb3encryptionkey :
2214 ses->smb3decryptionkey;
2215 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
2216 spin_unlock(&cifs_tcp_ses_lock);
2217 return 0;
2218 }
2219 spin_unlock(&cifs_tcp_ses_lock);
2220
2221 return 1;
2222}
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002223/*
2224 * Encrypt or decrypt @rqst message. @rqst has the following format:
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002225 * iov[0] - rfc1002 length
2226 * iov[1] - transform header (associate data),
2227 * iov[2-N] and pages - data to encrypt.
2228 * On success return encrypted data in iov[2-N] and pages, leave iov[0-1]
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002229 * untouched.
2230 */
2231static int
2232crypt_message(struct TCP_Server_Info *server, struct smb_rqst *rqst, int enc)
2233{
2234 struct smb2_transform_hdr *tr_hdr =
2235 (struct smb2_transform_hdr *)rqst->rq_iov[0].iov_base;
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002236 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20 - server->vals->header_preamble_size;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002237 int rc = 0;
2238 struct scatterlist *sg;
2239 u8 sign[SMB2_SIGNATURE_SIZE] = {};
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002240 u8 key[SMB3_SIGN_KEY_SIZE];
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002241 struct aead_request *req;
2242 char *iv;
2243 unsigned int iv_len;
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01002244 DECLARE_CRYPTO_WAIT(wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002245 struct crypto_aead *tfm;
2246 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2247
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002248 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
2249 if (rc) {
2250 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
2251 enc ? "en" : "de");
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002252 return 0;
2253 }
2254
2255 rc = smb3_crypto_aead_allocate(server);
2256 if (rc) {
2257 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
2258 return rc;
2259 }
2260
2261 tfm = enc ? server->secmech.ccmaesencrypt :
2262 server->secmech.ccmaesdecrypt;
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002263 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002264 if (rc) {
2265 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
2266 return rc;
2267 }
2268
2269 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
2270 if (rc) {
2271 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
2272 return rc;
2273 }
2274
2275 req = aead_request_alloc(tfm, GFP_KERNEL);
2276 if (!req) {
2277 cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
2278 return -ENOMEM;
2279 }
2280
2281 if (!enc) {
2282 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
2283 crypt_len += SMB2_SIGNATURE_SIZE;
2284 }
2285
2286 sg = init_sg(rqst, sign);
2287 if (!sg) {
Christophe Jaillet517a6e42017-06-11 09:12:47 +02002288 cifs_dbg(VFS, "%s: Failed to init sg", __func__);
2289 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002290 goto free_req;
2291 }
2292
2293 iv_len = crypto_aead_ivsize(tfm);
2294 iv = kzalloc(iv_len, GFP_KERNEL);
2295 if (!iv) {
2296 cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
Christophe Jaillet517a6e42017-06-11 09:12:47 +02002297 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002298 goto free_sg;
2299 }
2300 iv[0] = 3;
2301 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2302
2303 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
2304 aead_request_set_ad(req, assoc_data_len);
2305
2306 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01002307 crypto_req_done, &wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002308
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01002309 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
2310 : crypto_aead_decrypt(req), &wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002311
2312 if (!rc && enc)
2313 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
2314
2315 kfree(iv);
2316free_sg:
2317 kfree(sg);
2318free_req:
2319 kfree(req);
2320 return rc;
2321}
2322
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002323/*
2324 * This is called from smb_send_rqst. At this point we have the rfc1002
2325 * header as the first element in the vector.
2326 */
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002327static int
2328smb3_init_transform_rq(struct TCP_Server_Info *server, struct smb_rqst *new_rq,
2329 struct smb_rqst *old_rq)
2330{
2331 struct kvec *iov;
2332 struct page **pages;
2333 struct smb2_transform_hdr *tr_hdr;
2334 unsigned int npages = old_rq->rq_npages;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002335 unsigned int orig_len = get_rfc1002_length(old_rq->rq_iov[0].iov_base);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002336 int i;
2337 int rc = -ENOMEM;
2338
2339 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2340 if (!pages)
2341 return rc;
2342
2343 new_rq->rq_pages = pages;
2344 new_rq->rq_npages = old_rq->rq_npages;
2345 new_rq->rq_pagesz = old_rq->rq_pagesz;
2346 new_rq->rq_tailsz = old_rq->rq_tailsz;
2347
2348 for (i = 0; i < npages; i++) {
2349 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2350 if (!pages[i])
2351 goto err_free_pages;
2352 }
2353
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002354 /* Make space for one extra iov to hold the transform header */
2355 iov = kmalloc_array(old_rq->rq_nvec + 1, sizeof(struct kvec),
2356 GFP_KERNEL);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002357 if (!iov)
2358 goto err_free_pages;
2359
2360 /* copy all iovs from the old except the 1st one (rfc1002 length) */
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002361 memcpy(&iov[2], &old_rq->rq_iov[1],
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002362 sizeof(struct kvec) * (old_rq->rq_nvec - 1));
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002363 /* copy the rfc1002 iov */
2364 iov[0].iov_base = old_rq->rq_iov[0].iov_base;
2365 iov[0].iov_len = old_rq->rq_iov[0].iov_len;
2366
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002367 new_rq->rq_iov = iov;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002368 new_rq->rq_nvec = old_rq->rq_nvec + 1;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002369
2370 tr_hdr = kmalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL);
2371 if (!tr_hdr)
2372 goto err_free_iov;
2373
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002374 /* fill the 2nd iov with a transform header */
2375 fill_transform_hdr(tr_hdr, orig_len, old_rq);
2376 new_rq->rq_iov[1].iov_base = tr_hdr;
2377 new_rq->rq_iov[1].iov_len = sizeof(struct smb2_transform_hdr);
2378
2379 /* Update rfc1002 header */
2380 inc_rfc1001_len(new_rq->rq_iov[0].iov_base,
2381 sizeof(struct smb2_transform_hdr));
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002382
2383 /* copy pages form the old */
2384 for (i = 0; i < npages; i++) {
2385 char *dst = kmap(new_rq->rq_pages[i]);
2386 char *src = kmap(old_rq->rq_pages[i]);
2387 unsigned int len = (i < npages - 1) ? new_rq->rq_pagesz :
2388 new_rq->rq_tailsz;
2389 memcpy(dst, src, len);
2390 kunmap(new_rq->rq_pages[i]);
2391 kunmap(old_rq->rq_pages[i]);
2392 }
2393
2394 rc = crypt_message(server, new_rq, 1);
2395 cifs_dbg(FYI, "encrypt message returned %d", rc);
2396 if (rc)
2397 goto err_free_tr_hdr;
2398
2399 return rc;
2400
2401err_free_tr_hdr:
2402 kfree(tr_hdr);
2403err_free_iov:
2404 kfree(iov);
2405err_free_pages:
2406 for (i = i - 1; i >= 0; i--)
2407 put_page(pages[i]);
2408 kfree(pages);
2409 return rc;
2410}
2411
2412static void
2413smb3_free_transform_rq(struct smb_rqst *rqst)
2414{
2415 int i = rqst->rq_npages - 1;
2416
2417 for (; i >= 0; i--)
2418 put_page(rqst->rq_pages[i]);
2419 kfree(rqst->rq_pages);
2420 /* free transform header */
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002421 kfree(rqst->rq_iov[1].iov_base);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002422 kfree(rqst->rq_iov);
2423}
2424
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002425static int
2426smb3_is_transform_hdr(void *buf)
2427{
2428 struct smb2_transform_hdr *trhdr = buf;
2429
2430 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
2431}
2432
2433static int
2434decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
2435 unsigned int buf_data_size, struct page **pages,
2436 unsigned int npages, unsigned int page_data_size)
2437{
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002438 struct kvec iov[3];
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002439 struct smb_rqst rqst = {NULL};
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002440 int rc;
2441
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002442 iov[0].iov_base = NULL;
2443 iov[0].iov_len = 0;
2444 iov[1].iov_base = buf;
2445 iov[1].iov_len = sizeof(struct smb2_transform_hdr);
2446 iov[2].iov_base = buf + sizeof(struct smb2_transform_hdr);
2447 iov[2].iov_len = buf_data_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002448
2449 rqst.rq_iov = iov;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002450 rqst.rq_nvec = 3;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002451 rqst.rq_pages = pages;
2452 rqst.rq_npages = npages;
2453 rqst.rq_pagesz = PAGE_SIZE;
2454 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
2455
2456 rc = crypt_message(server, &rqst, 0);
2457 cifs_dbg(FYI, "decrypt message returned %d\n", rc);
2458
2459 if (rc)
2460 return rc;
2461
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002462 memmove(buf + server->vals->header_preamble_size, iov[2].iov_base, buf_data_size);
2463
2464 server->total_read = buf_data_size + page_data_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002465
2466 return rc;
2467}
2468
2469static int
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002470read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
2471 unsigned int npages, unsigned int len)
2472{
2473 int i;
2474 int length;
2475
2476 for (i = 0; i < npages; i++) {
2477 struct page *page = pages[i];
2478 size_t n;
2479
2480 n = len;
2481 if (len >= PAGE_SIZE) {
2482 /* enough data to fill the page */
2483 n = PAGE_SIZE;
2484 len -= n;
2485 } else {
2486 zero_user(page, len, PAGE_SIZE - len);
2487 len = 0;
2488 }
2489 length = cifs_read_page_from_socket(server, page, n);
2490 if (length < 0)
2491 return length;
2492 server->total_read += length;
2493 }
2494
2495 return 0;
2496}
2497
2498static int
2499init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
2500 unsigned int cur_off, struct bio_vec **page_vec)
2501{
2502 struct bio_vec *bvec;
2503 int i;
2504
2505 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
2506 if (!bvec)
2507 return -ENOMEM;
2508
2509 for (i = 0; i < npages; i++) {
2510 bvec[i].bv_page = pages[i];
2511 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
2512 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
2513 data_size -= bvec[i].bv_len;
2514 }
2515
2516 if (data_size != 0) {
2517 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
2518 kfree(bvec);
2519 return -EIO;
2520 }
2521
2522 *page_vec = bvec;
2523 return 0;
2524}
2525
2526static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002527handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
2528 char *buf, unsigned int buf_len, struct page **pages,
2529 unsigned int npages, unsigned int page_data_size)
2530{
2531 unsigned int data_offset;
2532 unsigned int data_len;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002533 unsigned int cur_off;
2534 unsigned int cur_page_idx;
2535 unsigned int pad_len;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002536 struct cifs_readdata *rdata = mid->callback_data;
2537 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
2538 struct bio_vec *bvec = NULL;
2539 struct iov_iter iter;
2540 struct kvec iov;
2541 int length;
Long Li74dcf412017-11-22 17:38:46 -07002542 bool use_rdma_mr = false;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002543
2544 if (shdr->Command != SMB2_READ) {
2545 cifs_dbg(VFS, "only big read responses are supported\n");
2546 return -ENOTSUPP;
2547 }
2548
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002549 if (server->ops->is_session_expired &&
2550 server->ops->is_session_expired(buf)) {
2551 cifs_reconnect(server);
2552 wake_up(&server->response_q);
2553 return -1;
2554 }
2555
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002556 if (server->ops->is_status_pending &&
2557 server->ops->is_status_pending(buf, server, 0))
2558 return -1;
2559
2560 rdata->result = server->ops->map_error(buf, false);
2561 if (rdata->result != 0) {
2562 cifs_dbg(FYI, "%s: server returned error %d\n",
2563 __func__, rdata->result);
2564 dequeue_mid(mid, rdata->result);
2565 return 0;
2566 }
2567
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002568 data_offset = server->ops->read_data_offset(buf) + server->vals->header_preamble_size;
Long Li74dcf412017-11-22 17:38:46 -07002569#ifdef CONFIG_CIFS_SMB_DIRECT
2570 use_rdma_mr = rdata->mr;
2571#endif
2572 data_len = server->ops->read_data_length(buf, use_rdma_mr);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002573
2574 if (data_offset < server->vals->read_rsp_size) {
2575 /*
2576 * win2k8 sometimes sends an offset of 0 when the read
2577 * is beyond the EOF. Treat it as if the data starts just after
2578 * the header.
2579 */
2580 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
2581 __func__, data_offset);
2582 data_offset = server->vals->read_rsp_size;
2583 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
2584 /* data_offset is beyond the end of smallbuf */
2585 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
2586 __func__, data_offset);
2587 rdata->result = -EIO;
2588 dequeue_mid(mid, rdata->result);
2589 return 0;
2590 }
2591
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002592 pad_len = data_offset - server->vals->read_rsp_size;
2593
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002594 if (buf_len <= data_offset) {
2595 /* read response payload is in pages */
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002596 cur_page_idx = pad_len / PAGE_SIZE;
2597 cur_off = pad_len % PAGE_SIZE;
2598
2599 if (cur_page_idx != 0) {
2600 /* data offset is beyond the 1st page of response */
2601 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
2602 __func__, data_offset);
2603 rdata->result = -EIO;
2604 dequeue_mid(mid, rdata->result);
2605 return 0;
2606 }
2607
2608 if (data_len > page_data_size - pad_len) {
2609 /* data_len is corrupt -- discard frame */
2610 rdata->result = -EIO;
2611 dequeue_mid(mid, rdata->result);
2612 return 0;
2613 }
2614
2615 rdata->result = init_read_bvec(pages, npages, page_data_size,
2616 cur_off, &bvec);
2617 if (rdata->result != 0) {
2618 dequeue_mid(mid, rdata->result);
2619 return 0;
2620 }
2621
2622 iov_iter_bvec(&iter, WRITE | ITER_BVEC, bvec, npages, data_len);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002623 } else if (buf_len >= data_offset + data_len) {
2624 /* read response payload is in buf */
2625 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
2626 iov.iov_base = buf + data_offset;
2627 iov.iov_len = data_len;
2628 iov_iter_kvec(&iter, WRITE | ITER_KVEC, &iov, 1, data_len);
2629 } else {
2630 /* read response payload cannot be in both buf and pages */
2631 WARN_ONCE(1, "buf can not contain only a part of read data");
2632 rdata->result = -EIO;
2633 dequeue_mid(mid, rdata->result);
2634 return 0;
2635 }
2636
2637 /* set up first iov for signature check */
2638 rdata->iov[0].iov_base = buf;
2639 rdata->iov[0].iov_len = 4;
2640 rdata->iov[1].iov_base = buf + 4;
2641 rdata->iov[1].iov_len = server->vals->read_rsp_size - 4;
2642 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
2643 rdata->iov[0].iov_base, server->vals->read_rsp_size);
2644
2645 length = rdata->copy_into_pages(server, rdata, &iter);
2646
2647 kfree(bvec);
2648
2649 if (length < 0)
2650 return length;
2651
2652 dequeue_mid(mid, false);
2653 return length;
2654}
2655
2656static int
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002657receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2658{
2659 char *buf = server->smallbuf;
2660 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2661 unsigned int npages;
2662 struct page **pages;
2663 unsigned int len;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10002664 unsigned int buflen = server->pdu_size + server->vals->header_preamble_size;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002665 int rc;
2666 int i = 0;
2667
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002668 len = min_t(unsigned int, buflen, server->vals->read_rsp_size -
2669 server->vals->header_preamble_size +
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002670 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
2671
2672 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
2673 if (rc < 0)
2674 return rc;
2675 server->total_read += rc;
2676
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002677 len = le32_to_cpu(tr_hdr->OriginalMessageSize) +
2678 server->vals->header_preamble_size -
2679 server->vals->read_rsp_size;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002680 npages = DIV_ROUND_UP(len, PAGE_SIZE);
2681
2682 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2683 if (!pages) {
2684 rc = -ENOMEM;
2685 goto discard_data;
2686 }
2687
2688 for (; i < npages; i++) {
2689 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2690 if (!pages[i]) {
2691 rc = -ENOMEM;
2692 goto discard_data;
2693 }
2694 }
2695
2696 /* read read data into pages */
2697 rc = read_data_into_pages(server, pages, npages, len);
2698 if (rc)
2699 goto free_pages;
2700
Pavel Shilovsky350be252017-04-10 10:31:33 -07002701 rc = cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002702 if (rc)
2703 goto free_pages;
2704
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002705 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size -
2706 server->vals->header_preamble_size,
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002707 pages, npages, len);
2708 if (rc)
2709 goto free_pages;
2710
2711 *mid = smb2_find_mid(server, buf);
2712 if (*mid == NULL)
2713 cifs_dbg(FYI, "mid not found\n");
2714 else {
2715 cifs_dbg(FYI, "mid found\n");
2716 (*mid)->decrypted = true;
2717 rc = handle_read_data(server, *mid, buf,
2718 server->vals->read_rsp_size,
2719 pages, npages, len);
2720 }
2721
2722free_pages:
2723 for (i = i - 1; i >= 0; i--)
2724 put_page(pages[i]);
2725 kfree(pages);
2726 return rc;
2727discard_data:
Pavel Shilovsky350be252017-04-10 10:31:33 -07002728 cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002729 goto free_pages;
2730}
2731
2732static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002733receive_encrypted_standard(struct TCP_Server_Info *server,
2734 struct mid_q_entry **mid)
2735{
2736 int length;
2737 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10002738 unsigned int pdu_length = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002739 unsigned int buf_size;
2740 struct mid_q_entry *mid_entry;
2741
2742 /* switch to large buffer if too big for a small one */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002743 if (pdu_length + server->vals->header_preamble_size > MAX_CIFS_SMALL_BUFFER_SIZE) {
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002744 server->large_buf = true;
2745 memcpy(server->bigbuf, buf, server->total_read);
2746 buf = server->bigbuf;
2747 }
2748
2749 /* now read the rest */
2750 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002751 pdu_length - HEADER_SIZE(server) + 1 +
2752 server->vals->header_preamble_size);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002753 if (length < 0)
2754 return length;
2755 server->total_read += length;
2756
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002757 buf_size = pdu_length + server->vals->header_preamble_size - sizeof(struct smb2_transform_hdr);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002758 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
2759 if (length)
2760 return length;
2761
2762 mid_entry = smb2_find_mid(server, buf);
2763 if (mid_entry == NULL)
2764 cifs_dbg(FYI, "mid not found\n");
2765 else {
2766 cifs_dbg(FYI, "mid found\n");
2767 mid_entry->decrypted = true;
2768 }
2769
2770 *mid = mid_entry;
2771
2772 if (mid_entry && mid_entry->handle)
2773 return mid_entry->handle(server, mid_entry);
2774
2775 return cifs_handle_standard(server, mid_entry);
2776}
2777
2778static int
2779smb3_receive_transform(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2780{
2781 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10002782 unsigned int pdu_length = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002783 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2784 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2785
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002786 if (pdu_length + server->vals->header_preamble_size < sizeof(struct smb2_transform_hdr) +
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002787 sizeof(struct smb2_sync_hdr)) {
2788 cifs_dbg(VFS, "Transform message is too small (%u)\n",
2789 pdu_length);
2790 cifs_reconnect(server);
2791 wake_up(&server->response_q);
2792 return -ECONNABORTED;
2793 }
2794
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002795 if (pdu_length + server->vals->header_preamble_size < orig_len + sizeof(struct smb2_transform_hdr)) {
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002796 cifs_dbg(VFS, "Transform message is broken\n");
2797 cifs_reconnect(server);
2798 wake_up(&server->response_q);
2799 return -ECONNABORTED;
2800 }
2801
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002802 if (pdu_length + server->vals->header_preamble_size > CIFSMaxBufSize + MAX_HEADER_SIZE(server))
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002803 return receive_encrypted_read(server, mid);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002804
2805 return receive_encrypted_standard(server, mid);
2806}
2807
2808int
2809smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
2810{
2811 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
2812
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10002813 return handle_read_data(server, mid, buf, server->pdu_size +
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002814 server->vals->header_preamble_size,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002815 NULL, 0, 0);
2816}
2817
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002818struct smb_version_operations smb20_operations = {
2819 .compare_fids = smb2_compare_fids,
2820 .setup_request = smb2_setup_request,
2821 .setup_async_request = smb2_setup_async_request,
2822 .check_receive = smb2_check_receive,
2823 .add_credits = smb2_add_credits,
2824 .set_credits = smb2_set_credits,
2825 .get_credits_field = smb2_get_credits_field,
2826 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002827 .wait_mtu_credits = cifs_wait_mtu_credits,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002828 .get_next_mid = smb2_get_next_mid,
2829 .read_data_offset = smb2_read_data_offset,
2830 .read_data_length = smb2_read_data_length,
2831 .map_error = map_smb2_to_linux_error,
2832 .find_mid = smb2_find_mid,
2833 .check_message = smb2_check_message,
2834 .dump_detail = smb2_dump_detail,
2835 .clear_stats = smb2_clear_stats,
2836 .print_stats = smb2_print_stats,
2837 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08002838 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002839 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002840 .need_neg = smb2_need_neg,
2841 .negotiate = smb2_negotiate,
2842 .negotiate_wsize = smb2_negotiate_wsize,
2843 .negotiate_rsize = smb2_negotiate_rsize,
2844 .sess_setup = SMB2_sess_setup,
2845 .logoff = SMB2_logoff,
2846 .tree_connect = SMB2_tcon,
2847 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05002848 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002849 .is_path_accessible = smb2_is_path_accessible,
2850 .can_echo = smb2_can_echo,
2851 .echo = SMB2_echo,
2852 .query_path_info = smb2_query_path_info,
2853 .get_srv_inum = smb2_get_srv_inum,
2854 .query_file_info = smb2_query_file_info,
2855 .set_path_size = smb2_set_path_size,
2856 .set_file_size = smb2_set_file_size,
2857 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05002858 .set_compression = smb2_set_compression,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002859 .mkdir = smb2_mkdir,
2860 .mkdir_setinfo = smb2_mkdir_setinfo,
2861 .rmdir = smb2_rmdir,
2862 .unlink = smb2_unlink,
2863 .rename = smb2_rename_path,
2864 .create_hardlink = smb2_create_hardlink,
2865 .query_symlink = smb2_query_symlink,
Sachin Prabhu5b23c972016-07-11 16:53:20 +01002866 .query_mf_symlink = smb3_query_mf_symlink,
2867 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002868 .open = smb2_open_file,
2869 .set_fid = smb2_set_fid,
2870 .close = smb2_close_file,
2871 .flush = smb2_flush_file,
2872 .async_readv = smb2_async_readv,
2873 .async_writev = smb2_async_writev,
2874 .sync_read = smb2_sync_read,
2875 .sync_write = smb2_sync_write,
2876 .query_dir_first = smb2_query_dir_first,
2877 .query_dir_next = smb2_query_dir_next,
2878 .close_dir = smb2_close_dir,
2879 .calc_smb_size = smb2_calc_size,
2880 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002881 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002882 .oplock_response = smb2_oplock_response,
2883 .queryfs = smb2_queryfs,
2884 .mand_lock = smb2_mand_lock,
2885 .mand_unlock_range = smb2_unlock_range,
2886 .push_mand_locks = smb2_push_mandatory_locks,
2887 .get_lease_key = smb2_get_lease_key,
2888 .set_lease_key = smb2_set_lease_key,
2889 .new_lease_key = smb2_new_lease_key,
2890 .calc_signature = smb2_calc_signature,
2891 .is_read_op = smb2_is_read_op,
2892 .set_oplock_level = smb2_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002893 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002894 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05002895 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002896 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04002897 .dir_needs_close = smb2_dir_needs_close,
Aurelien Aptel9d496402017-02-13 16:16:49 +01002898 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302899 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002900#ifdef CONFIG_CIFS_XATTR
2901 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10002902 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002903#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002904#ifdef CONFIG_CIFS_ACL
2905 .get_acl = get_smb2_acl,
2906 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05002907 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002908#endif /* CIFS_ACL */
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002909};
2910
Steve French1080ef72011-02-24 18:07:19 +00002911struct smb_version_operations smb21_operations = {
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07002912 .compare_fids = smb2_compare_fids,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002913 .setup_request = smb2_setup_request,
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +04002914 .setup_async_request = smb2_setup_async_request,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002915 .check_receive = smb2_check_receive,
Pavel Shilovsky28ea5292012-05-23 16:18:00 +04002916 .add_credits = smb2_add_credits,
2917 .set_credits = smb2_set_credits,
2918 .get_credits_field = smb2_get_credits_field,
2919 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002920 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002921 .get_next_mid = smb2_get_next_mid,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002922 .read_data_offset = smb2_read_data_offset,
2923 .read_data_length = smb2_read_data_length,
2924 .map_error = map_smb2_to_linux_error,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04002925 .find_mid = smb2_find_mid,
2926 .check_message = smb2_check_message,
2927 .dump_detail = smb2_dump_detail,
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04002928 .clear_stats = smb2_clear_stats,
2929 .print_stats = smb2_print_stats,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002930 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08002931 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002932 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04002933 .need_neg = smb2_need_neg,
2934 .negotiate = smb2_negotiate,
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -07002935 .negotiate_wsize = smb2_negotiate_wsize,
2936 .negotiate_rsize = smb2_negotiate_rsize,
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04002937 .sess_setup = SMB2_sess_setup,
2938 .logoff = SMB2_logoff,
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04002939 .tree_connect = SMB2_tcon,
2940 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05002941 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002942 .is_path_accessible = smb2_is_path_accessible,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002943 .can_echo = smb2_can_echo,
2944 .echo = SMB2_echo,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002945 .query_path_info = smb2_query_path_info,
2946 .get_srv_inum = smb2_get_srv_inum,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -07002947 .query_file_info = smb2_query_file_info,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002948 .set_path_size = smb2_set_path_size,
2949 .set_file_size = smb2_set_file_size,
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07002950 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05002951 .set_compression = smb2_set_compression,
Pavel Shilovskya0e73182011-07-19 12:56:37 +04002952 .mkdir = smb2_mkdir,
2953 .mkdir_setinfo = smb2_mkdir_setinfo,
Pavel Shilovsky1a500f02012-07-10 16:14:38 +04002954 .rmdir = smb2_rmdir,
Pavel Shilovskycbe6f432012-09-18 16:20:25 -07002955 .unlink = smb2_unlink,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002956 .rename = smb2_rename_path,
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002957 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002958 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05002959 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05002960 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002961 .open = smb2_open_file,
2962 .set_fid = smb2_set_fid,
2963 .close = smb2_close_file,
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002964 .flush = smb2_flush_file,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002965 .async_readv = smb2_async_readv,
Pavel Shilovsky33319142012-09-18 16:20:29 -07002966 .async_writev = smb2_async_writev,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002967 .sync_read = smb2_sync_read,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002968 .sync_write = smb2_sync_write,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002969 .query_dir_first = smb2_query_dir_first,
2970 .query_dir_next = smb2_query_dir_next,
2971 .close_dir = smb2_close_dir,
2972 .calc_smb_size = smb2_calc_size,
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002973 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002974 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002975 .oplock_response = smb2_oplock_response,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002976 .queryfs = smb2_queryfs,
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07002977 .mand_lock = smb2_mand_lock,
2978 .mand_unlock_range = smb2_unlock_range,
Pavel Shilovskyb1407992012-09-19 06:22:44 -07002979 .push_mand_locks = smb2_push_mandatory_locks,
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002980 .get_lease_key = smb2_get_lease_key,
2981 .set_lease_key = smb2_set_lease_key,
2982 .new_lease_key = smb2_new_lease_key,
Steve French38107d42012-12-08 22:08:06 -06002983 .calc_signature = smb2_calc_signature,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002984 .is_read_op = smb21_is_read_op,
2985 .set_oplock_level = smb21_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002986 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002987 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05002988 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002989 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04002990 .dir_needs_close = smb2_dir_needs_close,
Steve French834170c2016-09-30 21:14:26 -05002991 .enum_snapshots = smb3_enum_snapshots,
Aurelien Aptel9d496402017-02-13 16:16:49 +01002992 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302993 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002994#ifdef CONFIG_CIFS_XATTR
2995 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10002996 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002997#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002998#ifdef CONFIG_CIFS_ACL
2999 .get_acl = get_smb2_acl,
3000 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003001 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003002#endif /* CIFS_ACL */
Steve French38107d42012-12-08 22:08:06 -06003003};
3004
Steve French38107d42012-12-08 22:08:06 -06003005struct smb_version_operations smb30_operations = {
3006 .compare_fids = smb2_compare_fids,
3007 .setup_request = smb2_setup_request,
3008 .setup_async_request = smb2_setup_async_request,
3009 .check_receive = smb2_check_receive,
3010 .add_credits = smb2_add_credits,
3011 .set_credits = smb2_set_credits,
3012 .get_credits_field = smb2_get_credits_field,
3013 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003014 .wait_mtu_credits = smb2_wait_mtu_credits,
Steve French38107d42012-12-08 22:08:06 -06003015 .get_next_mid = smb2_get_next_mid,
3016 .read_data_offset = smb2_read_data_offset,
3017 .read_data_length = smb2_read_data_length,
3018 .map_error = map_smb2_to_linux_error,
3019 .find_mid = smb2_find_mid,
3020 .check_message = smb2_check_message,
3021 .dump_detail = smb2_dump_detail,
3022 .clear_stats = smb2_clear_stats,
3023 .print_stats = smb2_print_stats,
Steve French769ee6a2013-06-19 14:15:30 -05003024 .dump_share_caps = smb2_dump_share_caps,
Steve French38107d42012-12-08 22:08:06 -06003025 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08003026 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00003027 .downgrade_oplock = smb2_downgrade_oplock,
Steve French38107d42012-12-08 22:08:06 -06003028 .need_neg = smb2_need_neg,
3029 .negotiate = smb2_negotiate,
3030 .negotiate_wsize = smb2_negotiate_wsize,
3031 .negotiate_rsize = smb2_negotiate_rsize,
3032 .sess_setup = SMB2_sess_setup,
3033 .logoff = SMB2_logoff,
3034 .tree_connect = SMB2_tcon,
3035 .tree_disconnect = SMB2_tdis,
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003036 .qfs_tcon = smb3_qfs_tcon,
Steve French38107d42012-12-08 22:08:06 -06003037 .is_path_accessible = smb2_is_path_accessible,
3038 .can_echo = smb2_can_echo,
3039 .echo = SMB2_echo,
3040 .query_path_info = smb2_query_path_info,
3041 .get_srv_inum = smb2_get_srv_inum,
3042 .query_file_info = smb2_query_file_info,
3043 .set_path_size = smb2_set_path_size,
3044 .set_file_size = smb2_set_file_size,
3045 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05003046 .set_compression = smb2_set_compression,
Steve French38107d42012-12-08 22:08:06 -06003047 .mkdir = smb2_mkdir,
3048 .mkdir_setinfo = smb2_mkdir_setinfo,
3049 .rmdir = smb2_rmdir,
3050 .unlink = smb2_unlink,
3051 .rename = smb2_rename_path,
3052 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003053 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05003054 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05003055 .create_mf_symlink = smb3_create_mf_symlink,
Steve French38107d42012-12-08 22:08:06 -06003056 .open = smb2_open_file,
3057 .set_fid = smb2_set_fid,
3058 .close = smb2_close_file,
3059 .flush = smb2_flush_file,
3060 .async_readv = smb2_async_readv,
3061 .async_writev = smb2_async_writev,
3062 .sync_read = smb2_sync_read,
3063 .sync_write = smb2_sync_write,
3064 .query_dir_first = smb2_query_dir_first,
3065 .query_dir_next = smb2_query_dir_next,
3066 .close_dir = smb2_close_dir,
3067 .calc_smb_size = smb2_calc_size,
3068 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07003069 .is_session_expired = smb2_is_session_expired,
Steve French38107d42012-12-08 22:08:06 -06003070 .oplock_response = smb2_oplock_response,
3071 .queryfs = smb2_queryfs,
3072 .mand_lock = smb2_mand_lock,
3073 .mand_unlock_range = smb2_unlock_range,
3074 .push_mand_locks = smb2_push_mandatory_locks,
3075 .get_lease_key = smb2_get_lease_key,
3076 .set_lease_key = smb2_set_lease_key,
3077 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06003078 .generate_signingkey = generate_smb30signingkey,
Steve French38107d42012-12-08 22:08:06 -06003079 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05003080 .set_integrity = smb3_set_integrity,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003081 .is_read_op = smb21_is_read_op,
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003082 .set_oplock_level = smb3_set_oplock_level,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003083 .create_lease_buf = smb3_create_lease_buf,
3084 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05003085 .copychunk_range = smb2_copychunk_range,
Steve Frenchca9e7a12015-10-01 21:40:10 -05003086 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchff1c0382013-11-19 23:44:46 -06003087 .validate_negotiate = smb3_validate_negotiate,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04003088 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04003089 .dir_needs_close = smb2_dir_needs_close,
Steve French31742c52014-08-17 08:38:47 -05003090 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05003091 .enum_snapshots = smb3_enum_snapshots,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003092 .init_transform_rq = smb3_init_transform_rq,
3093 .free_transform_rq = smb3_free_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003094 .is_transform_hdr = smb3_is_transform_hdr,
3095 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01003096 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05303097 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003098#ifdef CONFIG_CIFS_XATTR
3099 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003100 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003101#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003102#ifdef CONFIG_CIFS_ACL
3103 .get_acl = get_smb2_acl,
3104 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003105 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003106#endif /* CIFS_ACL */
Steve French1080ef72011-02-24 18:07:19 +00003107};
3108
Steve Frenchaab18932015-06-23 23:37:11 -05003109#ifdef CONFIG_CIFS_SMB311
3110struct smb_version_operations smb311_operations = {
3111 .compare_fids = smb2_compare_fids,
3112 .setup_request = smb2_setup_request,
3113 .setup_async_request = smb2_setup_async_request,
3114 .check_receive = smb2_check_receive,
3115 .add_credits = smb2_add_credits,
3116 .set_credits = smb2_set_credits,
3117 .get_credits_field = smb2_get_credits_field,
3118 .get_credits = smb2_get_credits,
3119 .wait_mtu_credits = smb2_wait_mtu_credits,
3120 .get_next_mid = smb2_get_next_mid,
3121 .read_data_offset = smb2_read_data_offset,
3122 .read_data_length = smb2_read_data_length,
3123 .map_error = map_smb2_to_linux_error,
3124 .find_mid = smb2_find_mid,
3125 .check_message = smb2_check_message,
3126 .dump_detail = smb2_dump_detail,
3127 .clear_stats = smb2_clear_stats,
3128 .print_stats = smb2_print_stats,
3129 .dump_share_caps = smb2_dump_share_caps,
3130 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08003131 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Steve Frenchaab18932015-06-23 23:37:11 -05003132 .downgrade_oplock = smb2_downgrade_oplock,
3133 .need_neg = smb2_need_neg,
3134 .negotiate = smb2_negotiate,
3135 .negotiate_wsize = smb2_negotiate_wsize,
3136 .negotiate_rsize = smb2_negotiate_rsize,
3137 .sess_setup = SMB2_sess_setup,
3138 .logoff = SMB2_logoff,
3139 .tree_connect = SMB2_tcon,
3140 .tree_disconnect = SMB2_tdis,
3141 .qfs_tcon = smb3_qfs_tcon,
3142 .is_path_accessible = smb2_is_path_accessible,
3143 .can_echo = smb2_can_echo,
3144 .echo = SMB2_echo,
3145 .query_path_info = smb2_query_path_info,
3146 .get_srv_inum = smb2_get_srv_inum,
3147 .query_file_info = smb2_query_file_info,
3148 .set_path_size = smb2_set_path_size,
3149 .set_file_size = smb2_set_file_size,
3150 .set_file_info = smb2_set_file_info,
3151 .set_compression = smb2_set_compression,
3152 .mkdir = smb2_mkdir,
3153 .mkdir_setinfo = smb2_mkdir_setinfo,
3154 .rmdir = smb2_rmdir,
3155 .unlink = smb2_unlink,
3156 .rename = smb2_rename_path,
3157 .create_hardlink = smb2_create_hardlink,
3158 .query_symlink = smb2_query_symlink,
3159 .query_mf_symlink = smb3_query_mf_symlink,
3160 .create_mf_symlink = smb3_create_mf_symlink,
3161 .open = smb2_open_file,
3162 .set_fid = smb2_set_fid,
3163 .close = smb2_close_file,
3164 .flush = smb2_flush_file,
3165 .async_readv = smb2_async_readv,
3166 .async_writev = smb2_async_writev,
3167 .sync_read = smb2_sync_read,
3168 .sync_write = smb2_sync_write,
3169 .query_dir_first = smb2_query_dir_first,
3170 .query_dir_next = smb2_query_dir_next,
3171 .close_dir = smb2_close_dir,
3172 .calc_smb_size = smb2_calc_size,
3173 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07003174 .is_session_expired = smb2_is_session_expired,
Steve Frenchaab18932015-06-23 23:37:11 -05003175 .oplock_response = smb2_oplock_response,
3176 .queryfs = smb2_queryfs,
3177 .mand_lock = smb2_mand_lock,
3178 .mand_unlock_range = smb2_unlock_range,
3179 .push_mand_locks = smb2_push_mandatory_locks,
3180 .get_lease_key = smb2_get_lease_key,
3181 .set_lease_key = smb2_set_lease_key,
3182 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06003183 .generate_signingkey = generate_smb311signingkey,
Steve Frenchaab18932015-06-23 23:37:11 -05003184 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05003185 .set_integrity = smb3_set_integrity,
Steve Frenchaab18932015-06-23 23:37:11 -05003186 .is_read_op = smb21_is_read_op,
3187 .set_oplock_level = smb3_set_oplock_level,
3188 .create_lease_buf = smb3_create_lease_buf,
3189 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05003190 .copychunk_range = smb2_copychunk_range,
Steve French02b16662015-06-27 21:18:36 -07003191 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchaab18932015-06-23 23:37:11 -05003192/* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
3193 .wp_retry_size = smb2_wp_retry_size,
3194 .dir_needs_close = smb2_dir_needs_close,
3195 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05003196 .enum_snapshots = smb3_enum_snapshots,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003197 .init_transform_rq = smb3_init_transform_rq,
3198 .free_transform_rq = smb3_free_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003199 .is_transform_hdr = smb3_is_transform_hdr,
3200 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01003201 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05303202 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003203#ifdef CONFIG_CIFS_XATTR
3204 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003205 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003206#endif /* CIFS_XATTR */
Steve Frenchaab18932015-06-23 23:37:11 -05003207};
3208#endif /* CIFS_SMB311 */
3209
Steve Frenchdd446b12012-11-28 23:21:06 -06003210struct smb_version_values smb20_values = {
3211 .version_string = SMB20_VERSION_STRING,
3212 .protocol_id = SMB20_PROT_ID,
3213 .req_capabilities = 0, /* MBZ */
3214 .large_lock_type = 0,
3215 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3216 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3217 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003218 .header_size = sizeof(struct smb2_sync_hdr),
3219 .header_preamble_size = 0,
Steve Frenchdd446b12012-11-28 23:21:06 -06003220 .max_header_size = MAX_SMB2_HDR_SIZE,
3221 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3222 .lock_cmd = SMB2_LOCK,
3223 .cap_unix = 0,
3224 .cap_nt_find = SMB2_NT_FIND,
3225 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003226 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3227 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003228 .create_lease_size = sizeof(struct create_lease),
Steve Frenchdd446b12012-11-28 23:21:06 -06003229};
3230
Steve French1080ef72011-02-24 18:07:19 +00003231struct smb_version_values smb21_values = {
3232 .version_string = SMB21_VERSION_STRING,
Steve Frenche4aa25e2012-10-01 12:26:22 -05003233 .protocol_id = SMB21_PROT_ID,
3234 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
3235 .large_lock_type = 0,
3236 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3237 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3238 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003239 .header_size = sizeof(struct smb2_sync_hdr),
3240 .header_preamble_size = 0,
Steve Frenche4aa25e2012-10-01 12:26:22 -05003241 .max_header_size = MAX_SMB2_HDR_SIZE,
3242 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3243 .lock_cmd = SMB2_LOCK,
3244 .cap_unix = 0,
3245 .cap_nt_find = SMB2_NT_FIND,
3246 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003247 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3248 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003249 .create_lease_size = sizeof(struct create_lease),
Steve Frenche4aa25e2012-10-01 12:26:22 -05003250};
3251
Steve French9764c022017-09-17 10:41:35 -05003252struct smb_version_values smb3any_values = {
3253 .version_string = SMB3ANY_VERSION_STRING,
3254 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3255 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
3256 .large_lock_type = 0,
3257 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3258 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3259 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003260 .header_size = sizeof(struct smb2_sync_hdr),
3261 .header_preamble_size = 0,
Steve French9764c022017-09-17 10:41:35 -05003262 .max_header_size = MAX_SMB2_HDR_SIZE,
3263 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3264 .lock_cmd = SMB2_LOCK,
3265 .cap_unix = 0,
3266 .cap_nt_find = SMB2_NT_FIND,
3267 .cap_large_files = SMB2_LARGE_FILES,
3268 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3269 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3270 .create_lease_size = sizeof(struct create_lease_v2),
3271};
3272
3273struct smb_version_values smbdefault_values = {
3274 .version_string = SMBDEFAULT_VERSION_STRING,
3275 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3276 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
3277 .large_lock_type = 0,
3278 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3279 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3280 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003281 .header_size = sizeof(struct smb2_sync_hdr),
3282 .header_preamble_size = 0,
Steve French9764c022017-09-17 10:41:35 -05003283 .max_header_size = MAX_SMB2_HDR_SIZE,
3284 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3285 .lock_cmd = SMB2_LOCK,
3286 .cap_unix = 0,
3287 .cap_nt_find = SMB2_NT_FIND,
3288 .cap_large_files = SMB2_LARGE_FILES,
3289 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3290 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3291 .create_lease_size = sizeof(struct create_lease_v2),
3292};
3293
Steve Frenche4aa25e2012-10-01 12:26:22 -05003294struct smb_version_values smb30_values = {
3295 .version_string = SMB30_VERSION_STRING,
3296 .protocol_id = SMB30_PROT_ID,
Steve French373512e2015-12-18 13:05:30 -06003297 .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 -07003298 .large_lock_type = 0,
3299 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3300 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3301 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003302 .header_size = sizeof(struct smb2_sync_hdr),
3303 .header_preamble_size = 0,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04003304 .max_header_size = MAX_SMB2_HDR_SIZE,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003305 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04003306 .lock_cmd = SMB2_LOCK,
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04003307 .cap_unix = 0,
3308 .cap_nt_find = SMB2_NT_FIND,
3309 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003310 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3311 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003312 .create_lease_size = sizeof(struct create_lease_v2),
Steve French1080ef72011-02-24 18:07:19 +00003313};
Steve French20b6d8b2013-06-12 22:48:41 -05003314
3315struct smb_version_values smb302_values = {
3316 .version_string = SMB302_VERSION_STRING,
3317 .protocol_id = SMB302_PROT_ID,
Steve French373512e2015-12-18 13:05:30 -06003318 .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 -05003319 .large_lock_type = 0,
3320 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3321 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3322 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003323 .header_size = sizeof(struct smb2_sync_hdr),
3324 .header_preamble_size = 0,
Steve French20b6d8b2013-06-12 22:48:41 -05003325 .max_header_size = MAX_SMB2_HDR_SIZE,
3326 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3327 .lock_cmd = SMB2_LOCK,
3328 .cap_unix = 0,
3329 .cap_nt_find = SMB2_NT_FIND,
3330 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003331 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3332 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003333 .create_lease_size = sizeof(struct create_lease_v2),
Steve French20b6d8b2013-06-12 22:48:41 -05003334};
Steve French5f7fbf72014-12-17 22:52:58 -06003335
3336#ifdef CONFIG_CIFS_SMB311
3337struct smb_version_values smb311_values = {
3338 .version_string = SMB311_VERSION_STRING,
3339 .protocol_id = SMB311_PROT_ID,
Steve French19558802017-06-21 16:50:20 -05003340 .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 -06003341 .large_lock_type = 0,
3342 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3343 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3344 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003345 .header_size = sizeof(struct smb2_sync_hdr),
3346 .header_preamble_size = 0,
Steve French5f7fbf72014-12-17 22:52:58 -06003347 .max_header_size = MAX_SMB2_HDR_SIZE,
3348 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3349 .lock_cmd = SMB2_LOCK,
3350 .cap_unix = 0,
3351 .cap_nt_find = SMB2_NT_FIND,
3352 .cap_large_files = SMB2_LARGE_FILES,
3353 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3354 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3355 .create_lease_size = sizeof(struct create_lease_v2),
3356};
3357#endif /* SMB311 */