blob: b15f5957d64591f0af611670088dd4dd8439fb43 [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{
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000126 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)mid->resp_buf;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700127
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;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000193 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700194 __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
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000218 struct smb2_sync_hdr *shdr = (struct smb2_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
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000351 rc = SMB2_open(xid, &oparams, &srch_path, &oplock, NULL, NULL, NULL);
Steve French3d4ef9a2018-04-25 22:19:09 -0500352 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)
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000378 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
379 NULL);
Steve French3d4ef9a2018-04-25 22:19:09 -0500380 else
381 rc = open_shroot(xid, tcon, &fid);
382
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500383 if (rc)
384 return;
385
Steve Frenchc481e9f2013-10-14 01:21:53 -0500386#ifdef CONFIG_CIFS_STATS2
387 SMB3_request_interfaces(xid, tcon);
388#endif /* STATS2 */
389
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500390 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
391 FS_ATTRIBUTE_INFORMATION);
392 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
393 FS_DEVICE_INFORMATION);
394 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
395 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
Steve French3d4ef9a2018-04-25 22:19:09 -0500396 if (no_cached_open)
397 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500398 return;
399}
400
401static void
Steve French34f62642013-10-09 02:07:00 -0500402smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
403{
404 int rc;
405 __le16 srch_path = 0; /* Null - open root of share */
406 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
407 struct cifs_open_parms oparms;
408 struct cifs_fid fid;
409
410 oparms.tcon = tcon;
411 oparms.desired_access = FILE_READ_ATTRIBUTES;
412 oparms.disposition = FILE_OPEN;
413 oparms.create_options = 0;
414 oparms.fid = &fid;
415 oparms.reconnect = false;
416
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000417 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
Steve French34f62642013-10-09 02:07:00 -0500418 if (rc)
419 return;
420
Steven French21671142013-10-09 13:36:35 -0500421 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
422 FS_ATTRIBUTE_INFORMATION);
423 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
424 FS_DEVICE_INFORMATION);
Steve French34f62642013-10-09 02:07:00 -0500425 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
426 return;
427}
428
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400429static int
430smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
431 struct cifs_sb_info *cifs_sb, const char *full_path)
432{
433 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400434 __le16 *utf16_path;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700435 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400436 struct cifs_open_parms oparms;
437 struct cifs_fid fid;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400438
Steve French3d4ef9a2018-04-25 22:19:09 -0500439 if ((*full_path == 0) && tcon->valid_root_fid)
440 return 0;
441
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400442 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
443 if (!utf16_path)
444 return -ENOMEM;
445
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400446 oparms.tcon = tcon;
447 oparms.desired_access = FILE_READ_ATTRIBUTES;
448 oparms.disposition = FILE_OPEN;
449 oparms.create_options = 0;
450 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +0400451 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400452
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000453 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400454 if (rc) {
455 kfree(utf16_path);
456 return rc;
457 }
458
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400459 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400460 kfree(utf16_path);
461 return rc;
462}
463
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400464static int
465smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
466 struct cifs_sb_info *cifs_sb, const char *full_path,
467 u64 *uniqueid, FILE_ALL_INFO *data)
468{
469 *uniqueid = le64_to_cpu(data->IndexNumber);
470 return 0;
471}
472
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700473static int
474smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
475 struct cifs_fid *fid, FILE_ALL_INFO *data)
476{
477 int rc;
478 struct smb2_file_all_info *smb2_data;
479
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +0400480 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700481 GFP_KERNEL);
482 if (smb2_data == NULL)
483 return -ENOMEM;
484
485 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
486 smb2_data);
487 if (!rc)
488 move_smb2_info_to_cifs(data, smb2_data);
489 kfree(smb2_data);
490 return rc;
491}
492
Arnd Bergmann1368f152017-09-05 11:24:15 +0200493#ifdef CONFIG_CIFS_XATTR
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000494static ssize_t
495move_smb2_ea_to_cifs(char *dst, size_t dst_size,
496 struct smb2_file_full_ea_info *src, size_t src_size,
497 const unsigned char *ea_name)
498{
499 int rc = 0;
500 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
501 char *name, *value;
502 size_t name_len, value_len, user_name_len;
503
504 while (src_size > 0) {
505 name = &src->ea_data[0];
506 name_len = (size_t)src->ea_name_length;
507 value = &src->ea_data[src->ea_name_length + 1];
508 value_len = (size_t)le16_to_cpu(src->ea_value_length);
509
510 if (name_len == 0) {
511 break;
512 }
513
514 if (src_size < 8 + name_len + 1 + value_len) {
515 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
516 rc = -EIO;
517 goto out;
518 }
519
520 if (ea_name) {
521 if (ea_name_len == name_len &&
522 memcmp(ea_name, name, name_len) == 0) {
523 rc = value_len;
524 if (dst_size == 0)
525 goto out;
526 if (dst_size < value_len) {
527 rc = -ERANGE;
528 goto out;
529 }
530 memcpy(dst, value, value_len);
531 goto out;
532 }
533 } else {
534 /* 'user.' plus a terminating null */
535 user_name_len = 5 + 1 + name_len;
536
537 rc += user_name_len;
538
539 if (dst_size >= user_name_len) {
540 dst_size -= user_name_len;
541 memcpy(dst, "user.", 5);
542 dst += 5;
543 memcpy(dst, src->ea_data, name_len);
544 dst += name_len;
545 *dst = 0;
546 ++dst;
547 } else if (dst_size == 0) {
548 /* skip copy - calc size only */
549 } else {
550 /* stop before overrun buffer */
551 rc = -ERANGE;
552 break;
553 }
554 }
555
556 if (!src->next_entry_offset)
557 break;
558
559 if (src_size < le32_to_cpu(src->next_entry_offset)) {
560 /* stop before overrun buffer */
561 rc = -ERANGE;
562 break;
563 }
564 src_size -= le32_to_cpu(src->next_entry_offset);
565 src = (void *)((char *)src +
566 le32_to_cpu(src->next_entry_offset));
567 }
568
569 /* didn't find the named attribute */
570 if (ea_name)
571 rc = -ENODATA;
572
573out:
574 return (ssize_t)rc;
575}
576
577static ssize_t
578smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
579 const unsigned char *path, const unsigned char *ea_name,
580 char *ea_data, size_t buf_size,
581 struct cifs_sb_info *cifs_sb)
582{
583 int rc;
584 __le16 *utf16_path;
585 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
586 struct cifs_open_parms oparms;
587 struct cifs_fid fid;
588 struct smb2_file_full_ea_info *smb2_data;
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +1000589 int ea_buf_size = SMB2_MIN_EA_BUF;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000590
591 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
592 if (!utf16_path)
593 return -ENOMEM;
594
595 oparms.tcon = tcon;
596 oparms.desired_access = FILE_READ_EA;
597 oparms.disposition = FILE_OPEN;
598 oparms.create_options = 0;
599 oparms.fid = &fid;
600 oparms.reconnect = false;
601
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000602 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000603 kfree(utf16_path);
604 if (rc) {
605 cifs_dbg(FYI, "open failed rc=%d\n", rc);
606 return rc;
607 }
608
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +1000609 while (1) {
610 smb2_data = kzalloc(ea_buf_size, GFP_KERNEL);
611 if (smb2_data == NULL) {
612 SMB2_close(xid, tcon, fid.persistent_fid,
613 fid.volatile_fid);
614 return -ENOMEM;
615 }
616
617 rc = SMB2_query_eas(xid, tcon, fid.persistent_fid,
618 fid.volatile_fid,
619 ea_buf_size, smb2_data);
620
621 if (rc != -E2BIG)
622 break;
623
624 kfree(smb2_data);
625 ea_buf_size <<= 1;
626
627 if (ea_buf_size > SMB2_MAX_EA_BUF) {
628 cifs_dbg(VFS, "EA size is too large\n");
629 SMB2_close(xid, tcon, fid.persistent_fid,
630 fid.volatile_fid);
631 return -ENOMEM;
632 }
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000633 }
634
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000635 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
636
Paulo Alcantaraae2cd7f2018-05-04 11:25:26 -0300637 /*
638 * If ea_name is NULL (listxattr) and there are no EAs, return 0 as it's
639 * not an error. Otherwise, the specified ea_name was not found.
640 */
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000641 if (!rc)
642 rc = move_smb2_ea_to_cifs(ea_data, buf_size, smb2_data,
643 SMB2_MAX_EA_BUF, ea_name);
Paulo Alcantaraae2cd7f2018-05-04 11:25:26 -0300644 else if (!ea_name && rc == -ENODATA)
645 rc = 0;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000646
647 kfree(smb2_data);
648 return rc;
649}
650
Ronnie Sahlberg55175542017-08-24 11:24:56 +1000651
652static int
653smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
654 const char *path, const char *ea_name, const void *ea_value,
655 const __u16 ea_value_len, const struct nls_table *nls_codepage,
656 struct cifs_sb_info *cifs_sb)
657{
658 int rc;
659 __le16 *utf16_path;
660 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
661 struct cifs_open_parms oparms;
662 struct cifs_fid fid;
663 struct smb2_file_full_ea_info *ea;
664 int ea_name_len = strlen(ea_name);
665 int len;
666
667 if (ea_name_len > 255)
668 return -EINVAL;
669
670 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
671 if (!utf16_path)
672 return -ENOMEM;
673
674 oparms.tcon = tcon;
675 oparms.desired_access = FILE_WRITE_EA;
676 oparms.disposition = FILE_OPEN;
677 oparms.create_options = 0;
678 oparms.fid = &fid;
679 oparms.reconnect = false;
680
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000681 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Ronnie Sahlberg55175542017-08-24 11:24:56 +1000682 kfree(utf16_path);
683 if (rc) {
684 cifs_dbg(FYI, "open failed rc=%d\n", rc);
685 return rc;
686 }
687
688 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
689 ea = kzalloc(len, GFP_KERNEL);
690 if (ea == NULL) {
691 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
692 return -ENOMEM;
693 }
694
695 ea->ea_name_length = ea_name_len;
696 ea->ea_value_length = cpu_to_le16(ea_value_len);
697 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
698 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
699
700 rc = SMB2_set_ea(xid, tcon, fid.persistent_fid, fid.volatile_fid, ea,
701 len);
702 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
703
704 return rc;
705}
Arnd Bergmann1368f152017-09-05 11:24:15 +0200706#endif
Ronnie Sahlberg55175542017-08-24 11:24:56 +1000707
Pavel Shilovsky9094fad2012-07-12 18:30:44 +0400708static bool
709smb2_can_echo(struct TCP_Server_Info *server)
710{
711 return server->echoes;
712}
713
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400714static void
715smb2_clear_stats(struct cifs_tcon *tcon)
716{
717#ifdef CONFIG_CIFS_STATS
718 int i;
719 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
720 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
721 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
722 }
723#endif
724}
725
726static void
Steve French769ee6a2013-06-19 14:15:30 -0500727smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
728{
729 seq_puts(m, "\n\tShare Capabilities:");
730 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
731 seq_puts(m, " DFS,");
732 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
733 seq_puts(m, " CONTINUOUS AVAILABILITY,");
734 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
735 seq_puts(m, " SCALEOUT,");
736 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
737 seq_puts(m, " CLUSTER,");
738 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
739 seq_puts(m, " ASYMMETRIC,");
740 if (tcon->capabilities == 0)
741 seq_puts(m, " None");
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500742 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
743 seq_puts(m, " Aligned,");
744 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
745 seq_puts(m, " Partition Aligned,");
746 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
747 seq_puts(m, " SSD,");
748 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
749 seq_puts(m, " TRIM-support,");
750
Steve French769ee6a2013-06-19 14:15:30 -0500751 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
Steve Frenche0386e42018-05-20 01:27:03 -0500752 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500753 if (tcon->perf_sector_size)
754 seq_printf(m, "\tOptimal sector size: 0x%x",
755 tcon->perf_sector_size);
Steve Frenche0386e42018-05-20 01:27:03 -0500756 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
Steve French769ee6a2013-06-19 14:15:30 -0500757}
758
759static void
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400760smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
761{
762#ifdef CONFIG_CIFS_STATS
763 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
764 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
765 seq_printf(m, "\nNegotiates: %d sent %d failed",
766 atomic_read(&sent[SMB2_NEGOTIATE_HE]),
767 atomic_read(&failed[SMB2_NEGOTIATE_HE]));
768 seq_printf(m, "\nSessionSetups: %d sent %d failed",
769 atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
770 atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400771 seq_printf(m, "\nLogoffs: %d sent %d failed",
772 atomic_read(&sent[SMB2_LOGOFF_HE]),
773 atomic_read(&failed[SMB2_LOGOFF_HE]));
774 seq_printf(m, "\nTreeConnects: %d sent %d failed",
775 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
776 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
777 seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
778 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
779 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
780 seq_printf(m, "\nCreates: %d sent %d failed",
781 atomic_read(&sent[SMB2_CREATE_HE]),
782 atomic_read(&failed[SMB2_CREATE_HE]));
783 seq_printf(m, "\nCloses: %d sent %d failed",
784 atomic_read(&sent[SMB2_CLOSE_HE]),
785 atomic_read(&failed[SMB2_CLOSE_HE]));
786 seq_printf(m, "\nFlushes: %d sent %d failed",
787 atomic_read(&sent[SMB2_FLUSH_HE]),
788 atomic_read(&failed[SMB2_FLUSH_HE]));
789 seq_printf(m, "\nReads: %d sent %d failed",
790 atomic_read(&sent[SMB2_READ_HE]),
791 atomic_read(&failed[SMB2_READ_HE]));
792 seq_printf(m, "\nWrites: %d sent %d failed",
793 atomic_read(&sent[SMB2_WRITE_HE]),
794 atomic_read(&failed[SMB2_WRITE_HE]));
795 seq_printf(m, "\nLocks: %d sent %d failed",
796 atomic_read(&sent[SMB2_LOCK_HE]),
797 atomic_read(&failed[SMB2_LOCK_HE]));
798 seq_printf(m, "\nIOCTLs: %d sent %d failed",
799 atomic_read(&sent[SMB2_IOCTL_HE]),
800 atomic_read(&failed[SMB2_IOCTL_HE]));
801 seq_printf(m, "\nCancels: %d sent %d failed",
802 atomic_read(&sent[SMB2_CANCEL_HE]),
803 atomic_read(&failed[SMB2_CANCEL_HE]));
804 seq_printf(m, "\nEchos: %d sent %d failed",
805 atomic_read(&sent[SMB2_ECHO_HE]),
806 atomic_read(&failed[SMB2_ECHO_HE]));
807 seq_printf(m, "\nQueryDirectories: %d sent %d failed",
808 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
809 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
810 seq_printf(m, "\nChangeNotifies: %d sent %d failed",
811 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
812 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
813 seq_printf(m, "\nQueryInfos: %d sent %d failed",
814 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
815 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
816 seq_printf(m, "\nSetInfos: %d sent %d failed",
817 atomic_read(&sent[SMB2_SET_INFO_HE]),
818 atomic_read(&failed[SMB2_SET_INFO_HE]));
819 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
820 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
821 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
822#endif
823}
824
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700825static void
826smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
827{
David Howells2b0143b2015-03-17 22:25:59 +0000828 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovsky53ef1012013-09-05 16:11:28 +0400829 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
830
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700831 cfile->fid.persistent_fid = fid->persistent_fid;
832 cfile->fid.volatile_fid = fid->volatile_fid;
Pavel Shilovsky42873b02013-09-05 21:30:16 +0400833 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
834 &fid->purge_cache);
Pavel Shilovsky18cceb62013-09-05 13:01:06 +0400835 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
Aurelien Aptel94f87372016-09-22 07:38:50 +0200836 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700837}
838
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400839static void
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700840smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
841 struct cifs_fid *fid)
842{
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400843 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700844}
845
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -0700846static int
Steve French41c13582013-11-14 00:05:36 -0600847SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
848 u64 persistent_fid, u64 volatile_fid,
849 struct copychunk_ioctl *pcchunk)
850{
851 int rc;
852 unsigned int ret_data_len;
853 struct resume_key_req *res_key;
854
855 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
856 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
857 NULL, 0 /* no input */,
858 (char **)&res_key, &ret_data_len);
859
860 if (rc) {
861 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
862 goto req_res_key_exit;
863 }
864 if (ret_data_len < sizeof(struct resume_key_req)) {
865 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
866 rc = -EINVAL;
867 goto req_res_key_exit;
868 }
869 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
870
871req_res_key_exit:
872 kfree(res_key);
873 return rc;
874}
875
Sachin Prabhu620d8742017-02-10 16:03:51 +0530876static ssize_t
Sachin Prabhu312bbc52017-04-04 02:12:04 -0500877smb2_copychunk_range(const unsigned int xid,
Steve French41c13582013-11-14 00:05:36 -0600878 struct cifsFileInfo *srcfile,
879 struct cifsFileInfo *trgtfile, u64 src_off,
880 u64 len, u64 dest_off)
881{
882 int rc;
883 unsigned int ret_data_len;
884 struct copychunk_ioctl *pcchunk;
Steve French9bf0c9c2013-11-16 18:05:28 -0600885 struct copychunk_ioctl_rsp *retbuf = NULL;
886 struct cifs_tcon *tcon;
887 int chunks_copied = 0;
888 bool chunk_sizes_updated = false;
Sachin Prabhu620d8742017-02-10 16:03:51 +0530889 ssize_t bytes_written, total_bytes_written = 0;
Steve French41c13582013-11-14 00:05:36 -0600890
891 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
892
893 if (pcchunk == NULL)
894 return -ENOMEM;
895
Sachin Prabhu312bbc52017-04-04 02:12:04 -0500896 cifs_dbg(FYI, "in smb2_copychunk_range - about to call request res key\n");
Steve French41c13582013-11-14 00:05:36 -0600897 /* Request a key from the server to identify the source of the copy */
898 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
899 srcfile->fid.persistent_fid,
900 srcfile->fid.volatile_fid, pcchunk);
901
902 /* Note: request_res_key sets res_key null only if rc !=0 */
903 if (rc)
Steve French9bf0c9c2013-11-16 18:05:28 -0600904 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -0600905
906 /* For now array only one chunk long, will make more flexible later */
Fabian Frederickbc09d142014-12-10 15:41:15 -0800907 pcchunk->ChunkCount = cpu_to_le32(1);
Steve French41c13582013-11-14 00:05:36 -0600908 pcchunk->Reserved = 0;
Steve French41c13582013-11-14 00:05:36 -0600909 pcchunk->Reserved2 = 0;
910
Steve French9bf0c9c2013-11-16 18:05:28 -0600911 tcon = tlink_tcon(trgtfile->tlink);
912
913 while (len > 0) {
914 pcchunk->SourceOffset = cpu_to_le64(src_off);
915 pcchunk->TargetOffset = cpu_to_le64(dest_off);
916 pcchunk->Length =
917 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
918
919 /* Request server copy to target from src identified by key */
920 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
Steve French41c13582013-11-14 00:05:36 -0600921 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +0100922 true /* is_fsctl */, (char *)pcchunk,
Steve French9bf0c9c2013-11-16 18:05:28 -0600923 sizeof(struct copychunk_ioctl), (char **)&retbuf,
924 &ret_data_len);
925 if (rc == 0) {
926 if (ret_data_len !=
927 sizeof(struct copychunk_ioctl_rsp)) {
928 cifs_dbg(VFS, "invalid cchunk response size\n");
929 rc = -EIO;
930 goto cchunk_out;
931 }
932 if (retbuf->TotalBytesWritten == 0) {
933 cifs_dbg(FYI, "no bytes copied\n");
934 rc = -EIO;
935 goto cchunk_out;
936 }
937 /*
938 * Check if server claimed to write more than we asked
939 */
940 if (le32_to_cpu(retbuf->TotalBytesWritten) >
941 le32_to_cpu(pcchunk->Length)) {
942 cifs_dbg(VFS, "invalid copy chunk response\n");
943 rc = -EIO;
944 goto cchunk_out;
945 }
946 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
947 cifs_dbg(VFS, "invalid num chunks written\n");
948 rc = -EIO;
949 goto cchunk_out;
950 }
951 chunks_copied++;
Steve French41c13582013-11-14 00:05:36 -0600952
Sachin Prabhu620d8742017-02-10 16:03:51 +0530953 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
954 src_off += bytes_written;
955 dest_off += bytes_written;
956 len -= bytes_written;
957 total_bytes_written += bytes_written;
Steve French41c13582013-11-14 00:05:36 -0600958
Sachin Prabhu620d8742017-02-10 16:03:51 +0530959 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
Steve French9bf0c9c2013-11-16 18:05:28 -0600960 le32_to_cpu(retbuf->ChunksWritten),
961 le32_to_cpu(retbuf->ChunkBytesWritten),
Sachin Prabhu620d8742017-02-10 16:03:51 +0530962 bytes_written);
Steve French9bf0c9c2013-11-16 18:05:28 -0600963 } else if (rc == -EINVAL) {
964 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
965 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -0600966
Steve French9bf0c9c2013-11-16 18:05:28 -0600967 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
968 le32_to_cpu(retbuf->ChunksWritten),
969 le32_to_cpu(retbuf->ChunkBytesWritten),
970 le32_to_cpu(retbuf->TotalBytesWritten));
971
972 /*
973 * Check if this is the first request using these sizes,
974 * (ie check if copy succeed once with original sizes
975 * and check if the server gave us different sizes after
976 * we already updated max sizes on previous request).
977 * if not then why is the server returning an error now
978 */
979 if ((chunks_copied != 0) || chunk_sizes_updated)
980 goto cchunk_out;
981
982 /* Check that server is not asking us to grow size */
983 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
984 tcon->max_bytes_chunk)
985 tcon->max_bytes_chunk =
986 le32_to_cpu(retbuf->ChunkBytesWritten);
987 else
988 goto cchunk_out; /* server gave us bogus size */
989
990 /* No need to change MaxChunks since already set to 1 */
991 chunk_sizes_updated = true;
Sachin Prabhu2477bc52015-02-04 13:10:26 +0000992 } else
993 goto cchunk_out;
Steve French9bf0c9c2013-11-16 18:05:28 -0600994 }
995
996cchunk_out:
Steve French41c13582013-11-14 00:05:36 -0600997 kfree(pcchunk);
Steve French24df1482016-09-29 04:20:23 -0500998 kfree(retbuf);
Sachin Prabhu620d8742017-02-10 16:03:51 +0530999 if (rc)
1000 return rc;
1001 else
1002 return total_bytes_written;
Steve French41c13582013-11-14 00:05:36 -06001003}
1004
1005static int
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001006smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1007 struct cifs_fid *fid)
1008{
1009 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1010}
1011
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001012static unsigned int
1013smb2_read_data_offset(char *buf)
1014{
1015 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1016 return rsp->DataOffset;
1017}
1018
1019static unsigned int
Long Li74dcf412017-11-22 17:38:46 -07001020smb2_read_data_length(char *buf, bool in_remaining)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001021{
1022 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
Long Li74dcf412017-11-22 17:38:46 -07001023
1024 if (in_remaining)
1025 return le32_to_cpu(rsp->DataRemaining);
1026
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001027 return le32_to_cpu(rsp->DataLength);
1028}
1029
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001030
1031static int
Steve Frenchdb8b6312014-09-22 05:13:55 -05001032smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001033 struct cifs_io_parms *parms, unsigned int *bytes_read,
1034 char **buf, int *buf_type)
1035{
Steve Frenchdb8b6312014-09-22 05:13:55 -05001036 parms->persistent_fid = pfid->persistent_fid;
1037 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001038 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1039}
1040
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001041static int
Steve Frenchdb8b6312014-09-22 05:13:55 -05001042smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001043 struct cifs_io_parms *parms, unsigned int *written,
1044 struct kvec *iov, unsigned long nr_segs)
1045{
1046
Steve Frenchdb8b6312014-09-22 05:13:55 -05001047 parms->persistent_fid = pfid->persistent_fid;
1048 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001049 return SMB2_write(xid, parms, written, iov, nr_segs);
1050}
1051
Steve Frenchd43cc792014-08-13 17:16:29 -05001052/* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1053static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1054 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1055{
1056 struct cifsInodeInfo *cifsi;
1057 int rc;
1058
1059 cifsi = CIFS_I(inode);
1060
1061 /* if file already sparse don't bother setting sparse again */
1062 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1063 return true; /* already sparse */
1064
1065 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1066 return true; /* already not sparse */
1067
1068 /*
1069 * Can't check for sparse support on share the usual way via the
1070 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1071 * since Samba server doesn't set the flag on the share, yet
1072 * supports the set sparse FSCTL and returns sparse correctly
1073 * in the file attributes. If we fail setting sparse though we
1074 * mark that server does not support sparse files for this share
1075 * to avoid repeatedly sending the unsupported fsctl to server
1076 * if the file is repeatedly extended.
1077 */
1078 if (tcon->broken_sparse_sup)
1079 return false;
1080
1081 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1082 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001083 true /* is_fctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001084 &setsparse, 1, NULL, NULL);
Steve Frenchd43cc792014-08-13 17:16:29 -05001085 if (rc) {
1086 tcon->broken_sparse_sup = true;
1087 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1088 return false;
1089 }
1090
1091 if (setsparse)
1092 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1093 else
1094 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1095
1096 return true;
1097}
1098
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001099static int
1100smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1101 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1102{
1103 __le64 eof = cpu_to_le64(size);
Steve French3d1a3742014-08-11 21:05:25 -05001104 struct inode *inode;
1105
1106 /*
1107 * If extending file more than one page make sparse. Many Linux fs
1108 * make files sparse by default when extending via ftruncate
1109 */
David Howells2b0143b2015-03-17 22:25:59 +00001110 inode = d_inode(cfile->dentry);
Steve French3d1a3742014-08-11 21:05:25 -05001111
1112 if (!set_alloc && (size > inode->i_size + 8192)) {
Steve French3d1a3742014-08-11 21:05:25 -05001113 __u8 set_sparse = 1;
Steve French3d1a3742014-08-11 21:05:25 -05001114
Steve Frenchd43cc792014-08-13 17:16:29 -05001115 /* whether set sparse succeeds or not, extend the file */
1116 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
Steve French3d1a3742014-08-11 21:05:25 -05001117 }
1118
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001119 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05001120 cfile->fid.volatile_fid, cfile->pid, &eof, false);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001121}
1122
Steve French02b16662015-06-27 21:18:36 -07001123static int
1124smb2_duplicate_extents(const unsigned int xid,
1125 struct cifsFileInfo *srcfile,
1126 struct cifsFileInfo *trgtfile, u64 src_off,
1127 u64 len, u64 dest_off)
1128{
1129 int rc;
1130 unsigned int ret_data_len;
Steve French02b16662015-06-27 21:18:36 -07001131 struct duplicate_extents_to_file dup_ext_buf;
1132 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1133
1134 /* server fileays advertise duplicate extent support with this flag */
1135 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1136 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1137 return -EOPNOTSUPP;
1138
1139 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1140 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1141 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1142 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1143 dup_ext_buf.ByteCount = cpu_to_le64(len);
1144 cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
1145 src_off, dest_off, len);
1146
1147 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1148 if (rc)
1149 goto duplicate_extents_out;
1150
1151 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1152 trgtfile->fid.volatile_fid,
1153 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001154 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001155 (char *)&dup_ext_buf,
Steve French02b16662015-06-27 21:18:36 -07001156 sizeof(struct duplicate_extents_to_file),
Steve French24df1482016-09-29 04:20:23 -05001157 NULL,
Steve French02b16662015-06-27 21:18:36 -07001158 &ret_data_len);
1159
1160 if (ret_data_len > 0)
1161 cifs_dbg(FYI, "non-zero response length in duplicate extents");
1162
1163duplicate_extents_out:
1164 return rc;
1165}
Steve French02b16662015-06-27 21:18:36 -07001166
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001167static int
Steve French64a5cfa2013-10-14 15:31:32 -05001168smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1169 struct cifsFileInfo *cfile)
1170{
1171 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1172 cfile->fid.volatile_fid);
1173}
1174
1175static int
Steve Frenchb3152e22015-06-24 03:17:02 -05001176smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1177 struct cifsFileInfo *cfile)
1178{
1179 struct fsctl_set_integrity_information_req integr_info;
Steve Frenchb3152e22015-06-24 03:17:02 -05001180 unsigned int ret_data_len;
1181
1182 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1183 integr_info.Flags = 0;
1184 integr_info.Reserved = 0;
1185
1186 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1187 cfile->fid.volatile_fid,
1188 FSCTL_SET_INTEGRITY_INFORMATION,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001189 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001190 (char *)&integr_info,
Steve Frenchb3152e22015-06-24 03:17:02 -05001191 sizeof(struct fsctl_set_integrity_information_req),
Steve French24df1482016-09-29 04:20:23 -05001192 NULL,
Steve Frenchb3152e22015-06-24 03:17:02 -05001193 &ret_data_len);
1194
1195}
1196
1197static int
Steve French834170c2016-09-30 21:14:26 -05001198smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1199 struct cifsFileInfo *cfile, void __user *ioc_buf)
1200{
1201 char *retbuf = NULL;
1202 unsigned int ret_data_len = 0;
1203 int rc;
1204 struct smb_snapshot_array snapshot_in;
1205
1206 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1207 cfile->fid.volatile_fid,
1208 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001209 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001210 NULL, 0 /* no input data */,
Steve French834170c2016-09-30 21:14:26 -05001211 (char **)&retbuf,
1212 &ret_data_len);
1213 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1214 rc, ret_data_len);
1215 if (rc)
1216 return rc;
1217
1218 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1219 /* Fixup buffer */
1220 if (copy_from_user(&snapshot_in, ioc_buf,
1221 sizeof(struct smb_snapshot_array))) {
1222 rc = -EFAULT;
1223 kfree(retbuf);
1224 return rc;
1225 }
1226 if (snapshot_in.snapshot_array_size < sizeof(struct smb_snapshot_array)) {
1227 rc = -ERANGE;
David Disseldorp0e5c7952017-05-03 17:39:09 +02001228 kfree(retbuf);
Steve French834170c2016-09-30 21:14:26 -05001229 return rc;
1230 }
1231
1232 if (ret_data_len > snapshot_in.snapshot_array_size)
1233 ret_data_len = snapshot_in.snapshot_array_size;
1234
1235 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1236 rc = -EFAULT;
1237 }
1238
1239 kfree(retbuf);
1240 return rc;
1241}
1242
1243static int
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001244smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1245 const char *path, struct cifs_sb_info *cifs_sb,
1246 struct cifs_fid *fid, __u16 search_flags,
1247 struct cifs_search_info *srch_inf)
1248{
1249 __le16 *utf16_path;
1250 int rc;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001251 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001252 struct cifs_open_parms oparms;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001253
1254 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1255 if (!utf16_path)
1256 return -ENOMEM;
1257
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001258 oparms.tcon = tcon;
1259 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1260 oparms.disposition = FILE_OPEN;
1261 oparms.create_options = 0;
1262 oparms.fid = fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001263 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001264
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001265 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001266 kfree(utf16_path);
1267 if (rc) {
Pavel Shilovskydcd878382017-06-06 16:58:58 -07001268 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001269 return rc;
1270 }
1271
1272 srch_inf->entries_in_buffer = 0;
1273 srch_inf->index_of_last_entry = 0;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001274
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001275 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1276 fid->volatile_fid, 0, srch_inf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001277 if (rc) {
Pavel Shilovskydcd878382017-06-06 16:58:58 -07001278 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001279 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001280 }
1281 return rc;
1282}
1283
1284static int
1285smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1286 struct cifs_fid *fid, __u16 search_flags,
1287 struct cifs_search_info *srch_inf)
1288{
1289 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1290 fid->volatile_fid, 0, srch_inf);
1291}
1292
1293static int
1294smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1295 struct cifs_fid *fid)
1296{
1297 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1298}
1299
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001300/*
1301* If we negotiate SMB2 protocol and get STATUS_PENDING - update
1302* the number of credits and return true. Otherwise - return false.
1303*/
1304static bool
1305smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
1306{
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001307 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001308
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001309 if (shdr->Status != STATUS_PENDING)
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001310 return false;
1311
1312 if (!length) {
1313 spin_lock(&server->req_lock);
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001314 server->credits += le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001315 spin_unlock(&server->req_lock);
1316 wake_up(&server->request_q);
1317 }
1318
1319 return true;
1320}
1321
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001322static bool
1323smb2_is_session_expired(char *buf)
1324{
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001325 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001326
Mark Symsd81243c2018-05-24 09:47:31 +01001327 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
1328 shdr->Status != STATUS_USER_SESSION_DELETED)
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001329 return false;
1330
Mark Symsd81243c2018-05-24 09:47:31 +01001331 cifs_dbg(FYI, "Session expired or deleted\n");
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001332 return true;
1333}
1334
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001335static int
1336smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
1337 struct cifsInodeInfo *cinode)
1338{
Pavel Shilovsky0822f512012-09-19 06:22:45 -07001339 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
1340 return SMB2_lease_break(0, tcon, cinode->lease_key,
1341 smb2_get_lease_state(cinode));
1342
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001343 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
1344 fid->volatile_fid,
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001345 CIFS_CACHE_READ(cinode) ? 1 : 0);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001346}
1347
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001348static int
1349smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1350 struct kstatfs *buf)
1351{
1352 int rc;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001353 __le16 srch_path = 0; /* Null - open root of share */
1354 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001355 struct cifs_open_parms oparms;
1356 struct cifs_fid fid;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001357
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001358 oparms.tcon = tcon;
1359 oparms.desired_access = FILE_READ_ATTRIBUTES;
1360 oparms.disposition = FILE_OPEN;
1361 oparms.create_options = 0;
1362 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001363 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001364
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001365 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001366 if (rc)
1367 return rc;
1368 buf->f_type = SMB2_MAGIC_NUMBER;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001369 rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1370 buf);
1371 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001372 return rc;
1373}
1374
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07001375static bool
1376smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
1377{
1378 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
1379 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
1380}
1381
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07001382static int
1383smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
1384 __u64 length, __u32 type, int lock, int unlock, bool wait)
1385{
1386 if (unlock && !lock)
1387 type = SMB2_LOCKFLAG_UNLOCK;
1388 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
1389 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
1390 current->tgid, length, offset, type, wait);
1391}
1392
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001393static void
1394smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
1395{
1396 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
1397}
1398
1399static void
1400smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
1401{
1402 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
1403}
1404
1405static void
1406smb2_new_lease_key(struct cifs_fid *fid)
1407{
Steve Frenchfa70b872016-09-22 00:39:34 -05001408 generate_random_uuid(fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001409}
1410
Aurelien Aptel9d496402017-02-13 16:16:49 +01001411static int
1412smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
1413 const char *search_name,
1414 struct dfs_info3_param **target_nodes,
1415 unsigned int *num_of_nodes,
1416 const struct nls_table *nls_codepage, int remap)
1417{
1418 int rc;
1419 __le16 *utf16_path = NULL;
1420 int utf16_path_len = 0;
1421 struct cifs_tcon *tcon;
1422 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
1423 struct get_dfs_referral_rsp *dfs_rsp = NULL;
1424 u32 dfs_req_size = 0, dfs_rsp_size = 0;
1425
1426 cifs_dbg(FYI, "smb2_get_dfs_refer path <%s>\n", search_name);
1427
1428 /*
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001429 * Try to use the IPC tcon, otherwise just use any
Aurelien Aptel9d496402017-02-13 16:16:49 +01001430 */
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001431 tcon = ses->tcon_ipc;
1432 if (tcon == NULL) {
1433 spin_lock(&cifs_tcp_ses_lock);
1434 tcon = list_first_entry_or_null(&ses->tcon_list,
1435 struct cifs_tcon,
1436 tcon_list);
1437 if (tcon)
1438 tcon->tc_count++;
1439 spin_unlock(&cifs_tcp_ses_lock);
1440 }
Aurelien Aptel9d496402017-02-13 16:16:49 +01001441
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001442 if (tcon == NULL) {
Aurelien Aptel9d496402017-02-13 16:16:49 +01001443 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
1444 ses);
1445 rc = -ENOTCONN;
1446 goto out;
1447 }
1448
1449 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
1450 &utf16_path_len,
1451 nls_codepage, remap);
1452 if (!utf16_path) {
1453 rc = -ENOMEM;
1454 goto out;
1455 }
1456
1457 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
1458 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
1459 if (!dfs_req) {
1460 rc = -ENOMEM;
1461 goto out;
1462 }
1463
1464 /* Highest DFS referral version understood */
1465 dfs_req->MaxReferralLevel = DFS_VERSION;
1466
1467 /* Path to resolve in an UTF-16 null-terminated string */
1468 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
1469
1470 do {
Aurelien Aptel9d496402017-02-13 16:16:49 +01001471 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1472 FSCTL_DFS_GET_REFERRALS,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001473 true /* is_fsctl */,
Aurelien Aptel9d496402017-02-13 16:16:49 +01001474 (char *)dfs_req, dfs_req_size,
1475 (char **)&dfs_rsp, &dfs_rsp_size);
Aurelien Aptel9d496402017-02-13 16:16:49 +01001476 } while (rc == -EAGAIN);
1477
1478 if (rc) {
Steve French2564f2f2018-03-21 23:16:36 -05001479 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
Aurelien Aptel57025912017-11-21 14:47:56 +01001480 cifs_dbg(VFS, "ioctl error in smb2_get_dfs_refer rc=%d\n", rc);
Aurelien Aptel9d496402017-02-13 16:16:49 +01001481 goto out;
1482 }
1483
1484 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
1485 num_of_nodes, target_nodes,
1486 nls_codepage, remap, search_name,
1487 true /* is_unicode */);
1488 if (rc) {
1489 cifs_dbg(VFS, "parse error in smb2_get_dfs_refer rc=%d\n", rc);
1490 goto out;
1491 }
1492
1493 out:
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001494 if (tcon && !tcon->ipc) {
1495 /* ipc tcons are not refcounted */
Aurelien Aptel9d496402017-02-13 16:16:49 +01001496 spin_lock(&cifs_tcp_ses_lock);
1497 tcon->tc_count--;
1498 spin_unlock(&cifs_tcp_ses_lock);
1499 }
1500 kfree(utf16_path);
1501 kfree(dfs_req);
1502 kfree(dfs_rsp);
1503 return rc;
1504}
Pavel Shilovsky78932422016-07-24 10:37:38 +03001505#define SMB2_SYMLINK_STRUCT_SIZE \
1506 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
1507
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001508static int
1509smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
1510 const char *full_path, char **target_path,
1511 struct cifs_sb_info *cifs_sb)
1512{
1513 int rc;
1514 __le16 *utf16_path;
1515 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1516 struct cifs_open_parms oparms;
1517 struct cifs_fid fid;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001518 struct kvec err_iov = {NULL, 0};
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001519 struct smb2_err_rsp *err_buf = NULL;
1520 int resp_buftype;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001521 struct smb2_symlink_err_rsp *symlink;
Pavel Shilovsky78932422016-07-24 10:37:38 +03001522 unsigned int sub_len;
1523 unsigned int sub_offset;
1524 unsigned int print_len;
1525 unsigned int print_offset;
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 Sahlberg9d874c32018-06-08 13:21:18 +10001540 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_iov,
1541 &resp_buftype);
Gustavo A. R. Silva0d568cd2018-04-13 10:13:29 -05001542 if (!rc || !err_iov.iov_base) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001543 rc = -ENOENT;
1544 goto querty_exit;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001545 }
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 Sahlberg1fc6ad22018-06-01 10:53:07 +10001549 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001550 rc = -ENOENT;
1551 goto querty_exit;
Pavel Shilovsky78932422016-07-24 10:37:38 +03001552 }
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 Sahlberg1fc6ad22018-06-01 10:53:07 +10001562 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001563 rc = -ENOENT;
1564 goto querty_exit;
Pavel Shilovsky78932422016-07-24 10:37:38 +03001565 }
1566
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001567 if (err_iov.iov_len <
1568 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001569 rc = -ENOENT;
1570 goto querty_exit;
Pavel Shilovsky78932422016-07-24 10:37:38 +03001571 }
1572
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001573 *target_path = cifs_strndup_from_utf16(
1574 (char *)symlink->PathBuffer + sub_offset,
1575 sub_len, true, cifs_sb->local_nls);
1576 if (!(*target_path)) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001577 rc = -ENOMEM;
1578 goto querty_exit;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001579 }
1580 convert_delimiter(*target_path, '/');
1581 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001582
1583 querty_exit:
1584 free_rsp_buf(resp_buftype, err_buf);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001585 kfree(utf16_path);
1586 return rc;
1587}
1588
Arnd Bergmann84908422017-06-27 17:06:13 +02001589#ifdef CONFIG_CIFS_ACL
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001590static struct cifs_ntsd *
1591get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
1592 const struct cifs_fid *cifsfid, u32 *pacllen)
1593{
1594 struct cifs_ntsd *pntsd = NULL;
1595 unsigned int xid;
1596 int rc = -EOPNOTSUPP;
1597 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1598
1599 if (IS_ERR(tlink))
1600 return ERR_CAST(tlink);
1601
1602 xid = get_xid();
1603 cifs_dbg(FYI, "trying to get acl\n");
1604
1605 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
1606 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
1607 free_xid(xid);
1608
1609 cifs_put_tlink(tlink);
1610
1611 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
1612 if (rc)
1613 return ERR_PTR(rc);
1614 return pntsd;
1615
1616}
1617
1618static struct cifs_ntsd *
1619get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
1620 const char *path, u32 *pacllen)
1621{
1622 struct cifs_ntsd *pntsd = NULL;
1623 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1624 unsigned int xid;
1625 int rc;
1626 struct cifs_tcon *tcon;
1627 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1628 struct cifs_fid fid;
1629 struct cifs_open_parms oparms;
1630 __le16 *utf16_path;
1631
1632 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
1633 if (IS_ERR(tlink))
1634 return ERR_CAST(tlink);
1635
1636 tcon = tlink_tcon(tlink);
1637 xid = get_xid();
1638
1639 if (backup_cred(cifs_sb))
Colin Ian King709340a2017-07-05 13:47:34 +01001640 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001641 else
1642 oparms.create_options = 0;
1643
1644 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
Steve Frenchcfe89092018-05-19 02:04:55 -05001645 if (!utf16_path) {
1646 rc = -ENOMEM;
1647 free_xid(xid);
1648 return ERR_PTR(rc);
1649 }
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001650
1651 oparms.tcon = tcon;
1652 oparms.desired_access = READ_CONTROL;
1653 oparms.disposition = FILE_OPEN;
1654 oparms.fid = &fid;
1655 oparms.reconnect = false;
1656
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001657 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001658 kfree(utf16_path);
1659 if (!rc) {
1660 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
1661 fid.volatile_fid, (void **)&pntsd, pacllen);
1662 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1663 }
1664
1665 cifs_put_tlink(tlink);
1666 free_xid(xid);
1667
1668 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
1669 if (rc)
1670 return ERR_PTR(rc);
1671 return pntsd;
1672}
1673
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05001674#ifdef CONFIG_CIFS_ACL
1675static int
1676set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
1677 struct inode *inode, const char *path, int aclflag)
1678{
1679 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1680 unsigned int xid;
1681 int rc, access_flags = 0;
1682 struct cifs_tcon *tcon;
1683 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1684 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1685 struct cifs_fid fid;
1686 struct cifs_open_parms oparms;
1687 __le16 *utf16_path;
1688
1689 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
1690 if (IS_ERR(tlink))
1691 return PTR_ERR(tlink);
1692
1693 tcon = tlink_tcon(tlink);
1694 xid = get_xid();
1695
1696 if (backup_cred(cifs_sb))
1697 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1698 else
1699 oparms.create_options = 0;
1700
1701 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
1702 access_flags = WRITE_OWNER;
1703 else
1704 access_flags = WRITE_DAC;
1705
1706 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
Steve Frenchcfe89092018-05-19 02:04:55 -05001707 if (!utf16_path) {
1708 rc = -ENOMEM;
1709 free_xid(xid);
1710 return rc;
1711 }
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05001712
1713 oparms.tcon = tcon;
1714 oparms.desired_access = access_flags;
1715 oparms.disposition = FILE_OPEN;
1716 oparms.path = path;
1717 oparms.fid = &fid;
1718 oparms.reconnect = false;
1719
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001720 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05001721 kfree(utf16_path);
1722 if (!rc) {
1723 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
1724 fid.volatile_fid, pnntsd, acllen, aclflag);
1725 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1726 }
1727
1728 cifs_put_tlink(tlink);
1729 free_xid(xid);
1730 return rc;
1731}
1732#endif /* CIFS_ACL */
1733
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001734/* Retrieve an ACL from the server */
1735static struct cifs_ntsd *
1736get_smb2_acl(struct cifs_sb_info *cifs_sb,
1737 struct inode *inode, const char *path,
1738 u32 *pacllen)
1739{
1740 struct cifs_ntsd *pntsd = NULL;
1741 struct cifsFileInfo *open_file = NULL;
1742
1743 if (inode)
1744 open_file = find_readable_file(CIFS_I(inode), true);
1745 if (!open_file)
1746 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
1747
1748 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
1749 cifsFileInfo_put(open_file);
1750 return pntsd;
1751}
Arnd Bergmann84908422017-06-27 17:06:13 +02001752#endif
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001753
Steve French30175622014-08-17 18:16:40 -05001754static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
1755 loff_t offset, loff_t len, bool keep_size)
1756{
1757 struct inode *inode;
1758 struct cifsInodeInfo *cifsi;
1759 struct cifsFileInfo *cfile = file->private_data;
1760 struct file_zero_data_information fsctl_buf;
1761 long rc;
1762 unsigned int xid;
1763
1764 xid = get_xid();
1765
David Howells2b0143b2015-03-17 22:25:59 +00001766 inode = d_inode(cfile->dentry);
Steve French30175622014-08-17 18:16:40 -05001767 cifsi = CIFS_I(inode);
1768
1769 /* if file not oplocked can't be sure whether asking to extend size */
1770 if (!CIFS_CACHE_READ(cifsi))
Steve Frenchcfe89092018-05-19 02:04:55 -05001771 if (keep_size == false) {
1772 rc = -EOPNOTSUPP;
1773 free_xid(xid);
1774 return rc;
1775 }
Steve French30175622014-08-17 18:16:40 -05001776
Steve French2bb93d22014-08-20 18:56:29 -05001777 /*
Steve French30175622014-08-17 18:16:40 -05001778 * Must check if file sparse since fallocate -z (zero range) assumes
1779 * non-sparse allocation
1780 */
Steve Frenchcfe89092018-05-19 02:04:55 -05001781 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
1782 rc = -EOPNOTSUPP;
1783 free_xid(xid);
1784 return rc;
1785 }
Steve French30175622014-08-17 18:16:40 -05001786
1787 /*
1788 * need to make sure we are not asked to extend the file since the SMB3
1789 * fsctl does not change the file size. In the future we could change
1790 * this to zero the first part of the range then set the file size
1791 * which for a non sparse file would zero the newly extended range
1792 */
1793 if (keep_size == false)
Steve Frenchcfe89092018-05-19 02:04:55 -05001794 if (i_size_read(inode) < offset + len) {
1795 rc = -EOPNOTSUPP;
1796 free_xid(xid);
1797 return rc;
1798 }
Steve French30175622014-08-17 18:16:40 -05001799
1800 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1801
1802 fsctl_buf.FileOffset = cpu_to_le64(offset);
1803 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1804
1805 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1806 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001807 true /* is_fctl */, (char *)&fsctl_buf,
Steve French30175622014-08-17 18:16:40 -05001808 sizeof(struct file_zero_data_information), NULL, NULL);
1809 free_xid(xid);
1810 return rc;
1811}
1812
Steve French31742c52014-08-17 08:38:47 -05001813static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
1814 loff_t offset, loff_t len)
1815{
1816 struct inode *inode;
1817 struct cifsInodeInfo *cifsi;
1818 struct cifsFileInfo *cfile = file->private_data;
1819 struct file_zero_data_information fsctl_buf;
1820 long rc;
1821 unsigned int xid;
1822 __u8 set_sparse = 1;
1823
1824 xid = get_xid();
1825
David Howells2b0143b2015-03-17 22:25:59 +00001826 inode = d_inode(cfile->dentry);
Steve French31742c52014-08-17 08:38:47 -05001827 cifsi = CIFS_I(inode);
1828
1829 /* Need to make file sparse, if not already, before freeing range. */
1830 /* Consider adding equivalent for compressed since it could also work */
Steve Frenchcfe89092018-05-19 02:04:55 -05001831 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
1832 rc = -EOPNOTSUPP;
1833 free_xid(xid);
1834 return rc;
1835 }
Steve French31742c52014-08-17 08:38:47 -05001836
1837 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1838
1839 fsctl_buf.FileOffset = cpu_to_le64(offset);
1840 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1841
1842 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1843 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001844 true /* is_fctl */, (char *)&fsctl_buf,
Steve French31742c52014-08-17 08:38:47 -05001845 sizeof(struct file_zero_data_information), NULL, NULL);
1846 free_xid(xid);
1847 return rc;
1848}
1849
Steve French9ccf3212014-10-18 17:01:15 -05001850static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
1851 loff_t off, loff_t len, bool keep_size)
1852{
1853 struct inode *inode;
1854 struct cifsInodeInfo *cifsi;
1855 struct cifsFileInfo *cfile = file->private_data;
1856 long rc = -EOPNOTSUPP;
1857 unsigned int xid;
1858
1859 xid = get_xid();
1860
David Howells2b0143b2015-03-17 22:25:59 +00001861 inode = d_inode(cfile->dentry);
Steve French9ccf3212014-10-18 17:01:15 -05001862 cifsi = CIFS_I(inode);
1863
1864 /* if file not oplocked can't be sure whether asking to extend size */
1865 if (!CIFS_CACHE_READ(cifsi))
Steve Frenchcfe89092018-05-19 02:04:55 -05001866 if (keep_size == false) {
1867 free_xid(xid);
1868 return rc;
1869 }
Steve French9ccf3212014-10-18 17:01:15 -05001870
1871 /*
1872 * Files are non-sparse by default so falloc may be a no-op
1873 * Must check if file sparse. If not sparse, and not extending
1874 * then no need to do anything since file already allocated
1875 */
1876 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
1877 if (keep_size == true)
Steve Frenchcfe89092018-05-19 02:04:55 -05001878 rc = 0;
Steve French9ccf3212014-10-18 17:01:15 -05001879 /* check if extending file */
1880 else if (i_size_read(inode) >= off + len)
1881 /* not extending file and already not sparse */
Steve Frenchcfe89092018-05-19 02:04:55 -05001882 rc = 0;
Steve French9ccf3212014-10-18 17:01:15 -05001883 /* BB: in future add else clause to extend file */
1884 else
Steve Frenchcfe89092018-05-19 02:04:55 -05001885 rc = -EOPNOTSUPP;
1886 free_xid(xid);
1887 return rc;
Steve French9ccf3212014-10-18 17:01:15 -05001888 }
1889
1890 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
1891 /*
1892 * Check if falloc starts within first few pages of file
1893 * and ends within a few pages of the end of file to
1894 * ensure that most of file is being forced to be
1895 * fallocated now. If so then setting whole file sparse
1896 * ie potentially making a few extra pages at the beginning
1897 * or end of the file non-sparse via set_sparse is harmless.
1898 */
Steve Frenchcfe89092018-05-19 02:04:55 -05001899 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
1900 rc = -EOPNOTSUPP;
1901 free_xid(xid);
1902 return rc;
1903 }
Steve French9ccf3212014-10-18 17:01:15 -05001904
1905 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
1906 }
1907 /* BB: else ... in future add code to extend file and set sparse */
1908
1909
1910 free_xid(xid);
1911 return rc;
1912}
1913
1914
Steve French31742c52014-08-17 08:38:47 -05001915static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
1916 loff_t off, loff_t len)
1917{
1918 /* KEEP_SIZE already checked for by do_fallocate */
1919 if (mode & FALLOC_FL_PUNCH_HOLE)
1920 return smb3_punch_hole(file, tcon, off, len);
Steve French30175622014-08-17 18:16:40 -05001921 else if (mode & FALLOC_FL_ZERO_RANGE) {
1922 if (mode & FALLOC_FL_KEEP_SIZE)
1923 return smb3_zero_range(file, tcon, off, len, true);
1924 return smb3_zero_range(file, tcon, off, len, false);
Steve French9ccf3212014-10-18 17:01:15 -05001925 } else if (mode == FALLOC_FL_KEEP_SIZE)
1926 return smb3_simple_falloc(file, tcon, off, len, true);
1927 else if (mode == 0)
1928 return smb3_simple_falloc(file, tcon, off, len, false);
Steve French31742c52014-08-17 08:38:47 -05001929
1930 return -EOPNOTSUPP;
1931}
1932
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001933static void
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00001934smb2_downgrade_oplock(struct TCP_Server_Info *server,
1935 struct cifsInodeInfo *cinode, bool set_level2)
1936{
1937 if (set_level2)
1938 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
1939 0, NULL);
1940 else
1941 server->ops->set_oplock_level(cinode, 0, 0, NULL);
1942}
1943
1944static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001945smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1946 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001947{
1948 oplock &= 0xFF;
1949 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1950 return;
1951 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001952 cinode->oplock = CIFS_CACHE_RHW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001953 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
1954 &cinode->vfs_inode);
1955 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001956 cinode->oplock = CIFS_CACHE_RW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001957 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
1958 &cinode->vfs_inode);
1959 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
1960 cinode->oplock = CIFS_CACHE_READ_FLG;
1961 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
1962 &cinode->vfs_inode);
1963 } else
1964 cinode->oplock = 0;
1965}
1966
1967static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001968smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1969 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001970{
1971 char message[5] = {0};
1972
1973 oplock &= 0xFF;
1974 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1975 return;
1976
1977 cinode->oplock = 0;
1978 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
1979 cinode->oplock |= CIFS_CACHE_READ_FLG;
1980 strcat(message, "R");
1981 }
1982 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
1983 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
1984 strcat(message, "H");
1985 }
1986 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
1987 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
1988 strcat(message, "W");
1989 }
1990 if (!cinode->oplock)
1991 strcat(message, "None");
1992 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
1993 &cinode->vfs_inode);
1994}
1995
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001996static void
1997smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1998 unsigned int epoch, bool *purge_cache)
1999{
2000 unsigned int old_oplock = cinode->oplock;
2001
2002 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
2003
2004 if (purge_cache) {
2005 *purge_cache = false;
2006 if (old_oplock == CIFS_CACHE_READ_FLG) {
2007 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
2008 (epoch - cinode->epoch > 0))
2009 *purge_cache = true;
2010 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2011 (epoch - cinode->epoch > 1))
2012 *purge_cache = true;
2013 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2014 (epoch - cinode->epoch > 1))
2015 *purge_cache = true;
2016 else if (cinode->oplock == 0 &&
2017 (epoch - cinode->epoch > 0))
2018 *purge_cache = true;
2019 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
2020 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2021 (epoch - cinode->epoch > 0))
2022 *purge_cache = true;
2023 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2024 (epoch - cinode->epoch > 1))
2025 *purge_cache = true;
2026 }
2027 cinode->epoch = epoch;
2028 }
2029}
2030
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002031static bool
2032smb2_is_read_op(__u32 oplock)
2033{
2034 return oplock == SMB2_OPLOCK_LEVEL_II;
2035}
2036
2037static bool
2038smb21_is_read_op(__u32 oplock)
2039{
2040 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
2041 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
2042}
2043
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002044static __le32
2045map_oplock_to_lease(u8 oplock)
2046{
2047 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
2048 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
2049 else if (oplock == SMB2_OPLOCK_LEVEL_II)
2050 return SMB2_LEASE_READ_CACHING;
2051 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
2052 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
2053 SMB2_LEASE_WRITE_CACHING;
2054 return 0;
2055}
2056
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002057static char *
2058smb2_create_lease_buf(u8 *lease_key, u8 oplock)
2059{
2060 struct create_lease *buf;
2061
2062 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
2063 if (!buf)
2064 return NULL;
2065
2066 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
2067 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002068 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002069
2070 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2071 (struct create_lease, lcontext));
2072 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
2073 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2074 (struct create_lease, Name));
2075 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07002076 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002077 buf->Name[0] = 'R';
2078 buf->Name[1] = 'q';
2079 buf->Name[2] = 'L';
2080 buf->Name[3] = 's';
2081 return (char *)buf;
2082}
2083
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002084static char *
2085smb3_create_lease_buf(u8 *lease_key, u8 oplock)
2086{
2087 struct create_lease_v2 *buf;
2088
2089 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
2090 if (!buf)
2091 return NULL;
2092
2093 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
2094 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
2095 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2096
2097 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2098 (struct create_lease_v2, lcontext));
2099 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
2100 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2101 (struct create_lease_v2, Name));
2102 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07002103 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002104 buf->Name[0] = 'R';
2105 buf->Name[1] = 'q';
2106 buf->Name[2] = 'L';
2107 buf->Name[3] = 's';
2108 return (char *)buf;
2109}
2110
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002111static __u8
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06002112smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002113{
2114 struct create_lease *lc = (struct create_lease *)buf;
2115
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002116 *epoch = 0; /* not used */
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002117 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2118 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2119 return le32_to_cpu(lc->lcontext.LeaseState);
2120}
2121
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002122static __u8
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06002123smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002124{
2125 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
2126
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002127 *epoch = le16_to_cpu(lc->lcontext.Epoch);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002128 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2129 return SMB2_OPLOCK_LEVEL_NOCHANGE;
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06002130 if (lease_key)
2131 memcpy(lease_key, &lc->lcontext.LeaseKeyLow,
2132 SMB2_LEASE_KEY_SIZE);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002133 return le32_to_cpu(lc->lcontext.LeaseState);
2134}
2135
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002136static unsigned int
2137smb2_wp_retry_size(struct inode *inode)
2138{
2139 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
2140 SMB2_MAX_BUFFER_SIZE);
2141}
2142
Pavel Shilovsky52755802014-08-18 20:49:57 +04002143static bool
2144smb2_dir_needs_close(struct cifsFileInfo *cfile)
2145{
2146 return !cfile->invalidHandle;
2147}
2148
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002149static void
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002150fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
2151 struct smb_rqst *old_rq)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002152{
2153 struct smb2_sync_hdr *shdr =
2154 (struct smb2_sync_hdr *)old_rq->rq_iov[1].iov_base;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002155
2156 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
2157 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
2158 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
2159 tr_hdr->Flags = cpu_to_le16(0x01);
2160 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2161 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002162}
2163
Ronnie Sahlberg262916b2018-02-20 12:45:21 +11002164/* We can not use the normal sg_set_buf() as we will sometimes pass a
2165 * stack object as buf.
2166 */
2167static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
2168 unsigned int buflen)
2169{
2170 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
2171}
2172
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002173/* Assumes:
2174 * rqst->rq_iov[0] is rfc1002 length
2175 * rqst->rq_iov[1] is tranform header
2176 * rqst->rq_iov[2+] data to be encrypted/decrypted
2177 */
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002178static struct scatterlist *
2179init_sg(struct smb_rqst *rqst, u8 *sign)
2180{
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002181 unsigned int sg_len = rqst->rq_nvec + rqst->rq_npages;
2182 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002183 struct scatterlist *sg;
2184 unsigned int i;
2185 unsigned int j;
2186
2187 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
2188 if (!sg)
2189 return NULL;
2190
2191 sg_init_table(sg, sg_len);
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002192 smb2_sg_set_buf(&sg[0], rqst->rq_iov[1].iov_base + 20, assoc_data_len);
2193 for (i = 1; i < rqst->rq_nvec - 1; i++)
2194 smb2_sg_set_buf(&sg[i], rqst->rq_iov[i+1].iov_base,
2195 rqst->rq_iov[i+1].iov_len);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002196 for (j = 0; i < sg_len - 1; i++, j++) {
Steve Frenchd5f07fb2018-06-05 17:46:24 -05002197 unsigned int len, offset;
2198
2199 rqst_page_get_length(rqst, j, &len, &offset);
2200 sg_set_page(&sg[i], rqst->rq_pages[j], len, offset);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002201 }
Ronnie Sahlberg262916b2018-02-20 12:45:21 +11002202 smb2_sg_set_buf(&sg[sg_len - 1], sign, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002203 return sg;
2204}
2205
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002206static int
2207smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
2208{
2209 struct cifs_ses *ses;
2210 u8 *ses_enc_key;
2211
2212 spin_lock(&cifs_tcp_ses_lock);
2213 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2214 if (ses->Suid != ses_id)
2215 continue;
2216 ses_enc_key = enc ? ses->smb3encryptionkey :
2217 ses->smb3decryptionkey;
2218 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
2219 spin_unlock(&cifs_tcp_ses_lock);
2220 return 0;
2221 }
2222 spin_unlock(&cifs_tcp_ses_lock);
2223
2224 return 1;
2225}
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002226/*
2227 * Encrypt or decrypt @rqst message. @rqst has the following format:
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002228 * iov[0] - rfc1002 length
2229 * iov[1] - transform header (associate data),
2230 * iov[2-N] and pages - data to encrypt.
2231 * On success return encrypted data in iov[2-N] and pages, leave iov[0-1]
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002232 * untouched.
2233 */
2234static int
2235crypt_message(struct TCP_Server_Info *server, struct smb_rqst *rqst, int enc)
2236{
2237 struct smb2_transform_hdr *tr_hdr =
Aurelien Aptel83210ba2018-06-06 16:42:51 +02002238 (struct smb2_transform_hdr *)rqst->rq_iov[1].iov_base;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002239 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002240 int rc = 0;
2241 struct scatterlist *sg;
2242 u8 sign[SMB2_SIGNATURE_SIZE] = {};
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002243 u8 key[SMB3_SIGN_KEY_SIZE];
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002244 struct aead_request *req;
2245 char *iv;
2246 unsigned int iv_len;
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01002247 DECLARE_CRYPTO_WAIT(wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002248 struct crypto_aead *tfm;
2249 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2250
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002251 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
2252 if (rc) {
2253 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
2254 enc ? "en" : "de");
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002255 return 0;
2256 }
2257
2258 rc = smb3_crypto_aead_allocate(server);
2259 if (rc) {
2260 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
2261 return rc;
2262 }
2263
2264 tfm = enc ? server->secmech.ccmaesencrypt :
2265 server->secmech.ccmaesdecrypt;
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002266 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002267 if (rc) {
2268 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
2269 return rc;
2270 }
2271
2272 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
2273 if (rc) {
2274 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
2275 return rc;
2276 }
2277
2278 req = aead_request_alloc(tfm, GFP_KERNEL);
2279 if (!req) {
2280 cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
2281 return -ENOMEM;
2282 }
2283
2284 if (!enc) {
2285 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
2286 crypt_len += SMB2_SIGNATURE_SIZE;
2287 }
2288
2289 sg = init_sg(rqst, sign);
2290 if (!sg) {
Christophe Jaillet517a6e42017-06-11 09:12:47 +02002291 cifs_dbg(VFS, "%s: Failed to init sg", __func__);
2292 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002293 goto free_req;
2294 }
2295
2296 iv_len = crypto_aead_ivsize(tfm);
2297 iv = kzalloc(iv_len, GFP_KERNEL);
2298 if (!iv) {
2299 cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
Christophe Jaillet517a6e42017-06-11 09:12:47 +02002300 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002301 goto free_sg;
2302 }
2303 iv[0] = 3;
2304 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2305
2306 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
2307 aead_request_set_ad(req, assoc_data_len);
2308
2309 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01002310 crypto_req_done, &wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002311
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01002312 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
2313 : crypto_aead_decrypt(req), &wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002314
2315 if (!rc && enc)
2316 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
2317
2318 kfree(iv);
2319free_sg:
2320 kfree(sg);
2321free_req:
2322 kfree(req);
2323 return rc;
2324}
2325
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002326/*
2327 * This is called from smb_send_rqst. At this point we have the rfc1002
2328 * header as the first element in the vector.
2329 */
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002330static int
2331smb3_init_transform_rq(struct TCP_Server_Info *server, struct smb_rqst *new_rq,
2332 struct smb_rqst *old_rq)
2333{
2334 struct kvec *iov;
2335 struct page **pages;
2336 struct smb2_transform_hdr *tr_hdr;
2337 unsigned int npages = old_rq->rq_npages;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002338 unsigned int orig_len = get_rfc1002_length(old_rq->rq_iov[0].iov_base);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002339 int i;
2340 int rc = -ENOMEM;
2341
2342 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2343 if (!pages)
2344 return rc;
2345
2346 new_rq->rq_pages = pages;
Steve Frenchd5f07fb2018-06-05 17:46:24 -05002347 new_rq->rq_offset = old_rq->rq_offset;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002348 new_rq->rq_npages = old_rq->rq_npages;
2349 new_rq->rq_pagesz = old_rq->rq_pagesz;
2350 new_rq->rq_tailsz = old_rq->rq_tailsz;
2351
2352 for (i = 0; i < npages; i++) {
2353 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2354 if (!pages[i])
2355 goto err_free_pages;
2356 }
2357
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002358 /* Make space for one extra iov to hold the transform header */
2359 iov = kmalloc_array(old_rq->rq_nvec + 1, sizeof(struct kvec),
2360 GFP_KERNEL);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002361 if (!iov)
2362 goto err_free_pages;
2363
2364 /* copy all iovs from the old except the 1st one (rfc1002 length) */
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002365 memcpy(&iov[2], &old_rq->rq_iov[1],
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002366 sizeof(struct kvec) * (old_rq->rq_nvec - 1));
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002367 /* copy the rfc1002 iov */
2368 iov[0].iov_base = old_rq->rq_iov[0].iov_base;
2369 iov[0].iov_len = old_rq->rq_iov[0].iov_len;
2370
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002371 new_rq->rq_iov = iov;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002372 new_rq->rq_nvec = old_rq->rq_nvec + 1;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002373
2374 tr_hdr = kmalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL);
2375 if (!tr_hdr)
2376 goto err_free_iov;
2377
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002378 /* fill the 2nd iov with a transform header */
2379 fill_transform_hdr(tr_hdr, orig_len, old_rq);
2380 new_rq->rq_iov[1].iov_base = tr_hdr;
2381 new_rq->rq_iov[1].iov_len = sizeof(struct smb2_transform_hdr);
2382
2383 /* Update rfc1002 header */
2384 inc_rfc1001_len(new_rq->rq_iov[0].iov_base,
2385 sizeof(struct smb2_transform_hdr));
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002386
2387 /* copy pages form the old */
2388 for (i = 0; i < npages; i++) {
Steve Frenchd5f07fb2018-06-05 17:46:24 -05002389 char *dst, *src;
2390 unsigned int offset, len;
2391
2392 rqst_page_get_length(new_rq, i, &len, &offset);
2393
2394 dst = (char *) kmap(new_rq->rq_pages[i]) + offset;
2395 src = (char *) kmap(old_rq->rq_pages[i]) + offset;
2396
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002397 memcpy(dst, src, len);
2398 kunmap(new_rq->rq_pages[i]);
2399 kunmap(old_rq->rq_pages[i]);
2400 }
2401
2402 rc = crypt_message(server, new_rq, 1);
2403 cifs_dbg(FYI, "encrypt message returned %d", rc);
2404 if (rc)
2405 goto err_free_tr_hdr;
2406
2407 return rc;
2408
2409err_free_tr_hdr:
2410 kfree(tr_hdr);
2411err_free_iov:
2412 kfree(iov);
2413err_free_pages:
2414 for (i = i - 1; i >= 0; i--)
2415 put_page(pages[i]);
2416 kfree(pages);
2417 return rc;
2418}
2419
2420static void
2421smb3_free_transform_rq(struct smb_rqst *rqst)
2422{
2423 int i = rqst->rq_npages - 1;
2424
2425 for (; i >= 0; i--)
2426 put_page(rqst->rq_pages[i]);
2427 kfree(rqst->rq_pages);
2428 /* free transform header */
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002429 kfree(rqst->rq_iov[1].iov_base);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002430 kfree(rqst->rq_iov);
2431}
2432
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002433static int
2434smb3_is_transform_hdr(void *buf)
2435{
2436 struct smb2_transform_hdr *trhdr = buf;
2437
2438 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
2439}
2440
2441static int
2442decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
2443 unsigned int buf_data_size, struct page **pages,
2444 unsigned int npages, unsigned int page_data_size)
2445{
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002446 struct kvec iov[3];
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002447 struct smb_rqst rqst = {NULL};
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002448 int rc;
2449
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002450 iov[0].iov_base = NULL;
2451 iov[0].iov_len = 0;
2452 iov[1].iov_base = buf;
2453 iov[1].iov_len = sizeof(struct smb2_transform_hdr);
2454 iov[2].iov_base = buf + sizeof(struct smb2_transform_hdr);
2455 iov[2].iov_len = buf_data_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002456
2457 rqst.rq_iov = iov;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002458 rqst.rq_nvec = 3;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002459 rqst.rq_pages = pages;
2460 rqst.rq_npages = npages;
2461 rqst.rq_pagesz = PAGE_SIZE;
2462 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
2463
2464 rc = crypt_message(server, &rqst, 0);
2465 cifs_dbg(FYI, "decrypt message returned %d\n", rc);
2466
2467 if (rc)
2468 return rc;
2469
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002470 memmove(buf, iov[2].iov_base, buf_data_size);
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002471
2472 server->total_read = buf_data_size + page_data_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002473
2474 return rc;
2475}
2476
2477static int
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002478read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
2479 unsigned int npages, unsigned int len)
2480{
2481 int i;
2482 int length;
2483
2484 for (i = 0; i < npages; i++) {
2485 struct page *page = pages[i];
2486 size_t n;
2487
2488 n = len;
2489 if (len >= PAGE_SIZE) {
2490 /* enough data to fill the page */
2491 n = PAGE_SIZE;
2492 len -= n;
2493 } else {
2494 zero_user(page, len, PAGE_SIZE - len);
2495 len = 0;
2496 }
Long Li1dbe3462018-05-30 12:47:55 -07002497 length = cifs_read_page_from_socket(server, page, 0, n);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002498 if (length < 0)
2499 return length;
2500 server->total_read += length;
2501 }
2502
2503 return 0;
2504}
2505
2506static int
2507init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
2508 unsigned int cur_off, struct bio_vec **page_vec)
2509{
2510 struct bio_vec *bvec;
2511 int i;
2512
2513 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
2514 if (!bvec)
2515 return -ENOMEM;
2516
2517 for (i = 0; i < npages; i++) {
2518 bvec[i].bv_page = pages[i];
2519 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
2520 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
2521 data_size -= bvec[i].bv_len;
2522 }
2523
2524 if (data_size != 0) {
2525 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
2526 kfree(bvec);
2527 return -EIO;
2528 }
2529
2530 *page_vec = bvec;
2531 return 0;
2532}
2533
2534static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002535handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
2536 char *buf, unsigned int buf_len, struct page **pages,
2537 unsigned int npages, unsigned int page_data_size)
2538{
2539 unsigned int data_offset;
2540 unsigned int data_len;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002541 unsigned int cur_off;
2542 unsigned int cur_page_idx;
2543 unsigned int pad_len;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002544 struct cifs_readdata *rdata = mid->callback_data;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10002545 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002546 struct bio_vec *bvec = NULL;
2547 struct iov_iter iter;
2548 struct kvec iov;
2549 int length;
Long Li74dcf412017-11-22 17:38:46 -07002550 bool use_rdma_mr = false;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002551
2552 if (shdr->Command != SMB2_READ) {
2553 cifs_dbg(VFS, "only big read responses are supported\n");
2554 return -ENOTSUPP;
2555 }
2556
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002557 if (server->ops->is_session_expired &&
2558 server->ops->is_session_expired(buf)) {
2559 cifs_reconnect(server);
2560 wake_up(&server->response_q);
2561 return -1;
2562 }
2563
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002564 if (server->ops->is_status_pending &&
2565 server->ops->is_status_pending(buf, server, 0))
2566 return -1;
2567
2568 rdata->result = server->ops->map_error(buf, false);
2569 if (rdata->result != 0) {
2570 cifs_dbg(FYI, "%s: server returned error %d\n",
2571 __func__, rdata->result);
2572 dequeue_mid(mid, rdata->result);
2573 return 0;
2574 }
2575
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002576 data_offset = server->ops->read_data_offset(buf);
Long Li74dcf412017-11-22 17:38:46 -07002577#ifdef CONFIG_CIFS_SMB_DIRECT
2578 use_rdma_mr = rdata->mr;
2579#endif
2580 data_len = server->ops->read_data_length(buf, use_rdma_mr);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002581
2582 if (data_offset < server->vals->read_rsp_size) {
2583 /*
2584 * win2k8 sometimes sends an offset of 0 when the read
2585 * is beyond the EOF. Treat it as if the data starts just after
2586 * the header.
2587 */
2588 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
2589 __func__, data_offset);
2590 data_offset = server->vals->read_rsp_size;
2591 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
2592 /* data_offset is beyond the end of smallbuf */
2593 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
2594 __func__, data_offset);
2595 rdata->result = -EIO;
2596 dequeue_mid(mid, rdata->result);
2597 return 0;
2598 }
2599
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002600 pad_len = data_offset - server->vals->read_rsp_size;
2601
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002602 if (buf_len <= data_offset) {
2603 /* read response payload is in pages */
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002604 cur_page_idx = pad_len / PAGE_SIZE;
2605 cur_off = pad_len % PAGE_SIZE;
2606
2607 if (cur_page_idx != 0) {
2608 /* data offset is beyond the 1st page of response */
2609 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
2610 __func__, data_offset);
2611 rdata->result = -EIO;
2612 dequeue_mid(mid, rdata->result);
2613 return 0;
2614 }
2615
2616 if (data_len > page_data_size - pad_len) {
2617 /* data_len is corrupt -- discard frame */
2618 rdata->result = -EIO;
2619 dequeue_mid(mid, rdata->result);
2620 return 0;
2621 }
2622
2623 rdata->result = init_read_bvec(pages, npages, page_data_size,
2624 cur_off, &bvec);
2625 if (rdata->result != 0) {
2626 dequeue_mid(mid, rdata->result);
2627 return 0;
2628 }
2629
2630 iov_iter_bvec(&iter, WRITE | ITER_BVEC, bvec, npages, data_len);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002631 } else if (buf_len >= data_offset + data_len) {
2632 /* read response payload is in buf */
2633 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
2634 iov.iov_base = buf + data_offset;
2635 iov.iov_len = data_len;
2636 iov_iter_kvec(&iter, WRITE | ITER_KVEC, &iov, 1, data_len);
2637 } else {
2638 /* read response payload cannot be in both buf and pages */
2639 WARN_ONCE(1, "buf can not contain only a part of read data");
2640 rdata->result = -EIO;
2641 dequeue_mid(mid, rdata->result);
2642 return 0;
2643 }
2644
2645 /* set up first iov for signature check */
2646 rdata->iov[0].iov_base = buf;
2647 rdata->iov[0].iov_len = 4;
2648 rdata->iov[1].iov_base = buf + 4;
2649 rdata->iov[1].iov_len = server->vals->read_rsp_size - 4;
2650 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
2651 rdata->iov[0].iov_base, server->vals->read_rsp_size);
2652
2653 length = rdata->copy_into_pages(server, rdata, &iter);
2654
2655 kfree(bvec);
2656
2657 if (length < 0)
2658 return length;
2659
2660 dequeue_mid(mid, false);
2661 return length;
2662}
2663
2664static int
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002665receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2666{
2667 char *buf = server->smallbuf;
2668 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2669 unsigned int npages;
2670 struct page **pages;
2671 unsigned int len;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002672 unsigned int buflen = server->pdu_size;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002673 int rc;
2674 int i = 0;
2675
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002676 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002677 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
2678
2679 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
2680 if (rc < 0)
2681 return rc;
2682 server->total_read += rc;
2683
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002684 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002685 server->vals->read_rsp_size;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002686 npages = DIV_ROUND_UP(len, PAGE_SIZE);
2687
2688 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2689 if (!pages) {
2690 rc = -ENOMEM;
2691 goto discard_data;
2692 }
2693
2694 for (; i < npages; i++) {
2695 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2696 if (!pages[i]) {
2697 rc = -ENOMEM;
2698 goto discard_data;
2699 }
2700 }
2701
2702 /* read read data into pages */
2703 rc = read_data_into_pages(server, pages, npages, len);
2704 if (rc)
2705 goto free_pages;
2706
Pavel Shilovsky350be252017-04-10 10:31:33 -07002707 rc = cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002708 if (rc)
2709 goto free_pages;
2710
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002711 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002712 pages, npages, len);
2713 if (rc)
2714 goto free_pages;
2715
2716 *mid = smb2_find_mid(server, buf);
2717 if (*mid == NULL)
2718 cifs_dbg(FYI, "mid not found\n");
2719 else {
2720 cifs_dbg(FYI, "mid found\n");
2721 (*mid)->decrypted = true;
2722 rc = handle_read_data(server, *mid, buf,
2723 server->vals->read_rsp_size,
2724 pages, npages, len);
2725 }
2726
2727free_pages:
2728 for (i = i - 1; i >= 0; i--)
2729 put_page(pages[i]);
2730 kfree(pages);
2731 return rc;
2732discard_data:
Pavel Shilovsky350be252017-04-10 10:31:33 -07002733 cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002734 goto free_pages;
2735}
2736
2737static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002738receive_encrypted_standard(struct TCP_Server_Info *server,
2739 struct mid_q_entry **mid)
2740{
2741 int length;
2742 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10002743 unsigned int pdu_length = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002744 unsigned int buf_size;
2745 struct mid_q_entry *mid_entry;
2746
2747 /* switch to large buffer if too big for a small one */
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002748 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002749 server->large_buf = true;
2750 memcpy(server->bigbuf, buf, server->total_read);
2751 buf = server->bigbuf;
2752 }
2753
2754 /* now read the rest */
2755 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002756 pdu_length - HEADER_SIZE(server) + 1);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002757 if (length < 0)
2758 return length;
2759 server->total_read += length;
2760
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002761 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002762 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
2763 if (length)
2764 return length;
2765
2766 mid_entry = smb2_find_mid(server, buf);
2767 if (mid_entry == NULL)
2768 cifs_dbg(FYI, "mid not found\n");
2769 else {
2770 cifs_dbg(FYI, "mid found\n");
2771 mid_entry->decrypted = true;
2772 }
2773
2774 *mid = mid_entry;
2775
2776 if (mid_entry && mid_entry->handle)
2777 return mid_entry->handle(server, mid_entry);
2778
2779 return cifs_handle_standard(server, mid_entry);
2780}
2781
2782static int
2783smb3_receive_transform(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2784{
2785 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10002786 unsigned int pdu_length = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002787 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2788 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2789
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002790 if (pdu_length < sizeof(struct smb2_transform_hdr) +
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002791 sizeof(struct smb2_sync_hdr)) {
2792 cifs_dbg(VFS, "Transform message is too small (%u)\n",
2793 pdu_length);
2794 cifs_reconnect(server);
2795 wake_up(&server->response_q);
2796 return -ECONNABORTED;
2797 }
2798
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002799 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002800 cifs_dbg(VFS, "Transform message is broken\n");
2801 cifs_reconnect(server);
2802 wake_up(&server->response_q);
2803 return -ECONNABORTED;
2804 }
2805
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002806 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server))
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002807 return receive_encrypted_read(server, mid);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002808
2809 return receive_encrypted_standard(server, mid);
2810}
2811
2812int
2813smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
2814{
2815 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
2816
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002817 return handle_read_data(server, mid, buf, server->pdu_size,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002818 NULL, 0, 0);
2819}
2820
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10002821static int
2822smb2_next_header(char *buf)
2823{
2824 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
2825 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
2826
2827 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
2828 return sizeof(struct smb2_transform_hdr) +
2829 le32_to_cpu(t_hdr->OriginalMessageSize);
2830
2831 return le32_to_cpu(hdr->NextCommand);
2832}
2833
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002834struct smb_version_operations smb20_operations = {
2835 .compare_fids = smb2_compare_fids,
2836 .setup_request = smb2_setup_request,
2837 .setup_async_request = smb2_setup_async_request,
2838 .check_receive = smb2_check_receive,
2839 .add_credits = smb2_add_credits,
2840 .set_credits = smb2_set_credits,
2841 .get_credits_field = smb2_get_credits_field,
2842 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002843 .wait_mtu_credits = cifs_wait_mtu_credits,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002844 .get_next_mid = smb2_get_next_mid,
2845 .read_data_offset = smb2_read_data_offset,
2846 .read_data_length = smb2_read_data_length,
2847 .map_error = map_smb2_to_linux_error,
2848 .find_mid = smb2_find_mid,
2849 .check_message = smb2_check_message,
2850 .dump_detail = smb2_dump_detail,
2851 .clear_stats = smb2_clear_stats,
2852 .print_stats = smb2_print_stats,
2853 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08002854 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002855 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002856 .need_neg = smb2_need_neg,
2857 .negotiate = smb2_negotiate,
2858 .negotiate_wsize = smb2_negotiate_wsize,
2859 .negotiate_rsize = smb2_negotiate_rsize,
2860 .sess_setup = SMB2_sess_setup,
2861 .logoff = SMB2_logoff,
2862 .tree_connect = SMB2_tcon,
2863 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05002864 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002865 .is_path_accessible = smb2_is_path_accessible,
2866 .can_echo = smb2_can_echo,
2867 .echo = SMB2_echo,
2868 .query_path_info = smb2_query_path_info,
2869 .get_srv_inum = smb2_get_srv_inum,
2870 .query_file_info = smb2_query_file_info,
2871 .set_path_size = smb2_set_path_size,
2872 .set_file_size = smb2_set_file_size,
2873 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05002874 .set_compression = smb2_set_compression,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002875 .mkdir = smb2_mkdir,
2876 .mkdir_setinfo = smb2_mkdir_setinfo,
2877 .rmdir = smb2_rmdir,
2878 .unlink = smb2_unlink,
2879 .rename = smb2_rename_path,
2880 .create_hardlink = smb2_create_hardlink,
2881 .query_symlink = smb2_query_symlink,
Sachin Prabhu5b23c972016-07-11 16:53:20 +01002882 .query_mf_symlink = smb3_query_mf_symlink,
2883 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002884 .open = smb2_open_file,
2885 .set_fid = smb2_set_fid,
2886 .close = smb2_close_file,
2887 .flush = smb2_flush_file,
2888 .async_readv = smb2_async_readv,
2889 .async_writev = smb2_async_writev,
2890 .sync_read = smb2_sync_read,
2891 .sync_write = smb2_sync_write,
2892 .query_dir_first = smb2_query_dir_first,
2893 .query_dir_next = smb2_query_dir_next,
2894 .close_dir = smb2_close_dir,
2895 .calc_smb_size = smb2_calc_size,
2896 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002897 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002898 .oplock_response = smb2_oplock_response,
2899 .queryfs = smb2_queryfs,
2900 .mand_lock = smb2_mand_lock,
2901 .mand_unlock_range = smb2_unlock_range,
2902 .push_mand_locks = smb2_push_mandatory_locks,
2903 .get_lease_key = smb2_get_lease_key,
2904 .set_lease_key = smb2_set_lease_key,
2905 .new_lease_key = smb2_new_lease_key,
2906 .calc_signature = smb2_calc_signature,
2907 .is_read_op = smb2_is_read_op,
2908 .set_oplock_level = smb2_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002909 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002910 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05002911 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002912 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04002913 .dir_needs_close = smb2_dir_needs_close,
Aurelien Aptel9d496402017-02-13 16:16:49 +01002914 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302915 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002916#ifdef CONFIG_CIFS_XATTR
2917 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10002918 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002919#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002920#ifdef CONFIG_CIFS_ACL
2921 .get_acl = get_smb2_acl,
2922 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05002923 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002924#endif /* CIFS_ACL */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10002925 .next_header = smb2_next_header,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002926};
2927
Steve French1080ef72011-02-24 18:07:19 +00002928struct smb_version_operations smb21_operations = {
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07002929 .compare_fids = smb2_compare_fids,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002930 .setup_request = smb2_setup_request,
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +04002931 .setup_async_request = smb2_setup_async_request,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002932 .check_receive = smb2_check_receive,
Pavel Shilovsky28ea5292012-05-23 16:18:00 +04002933 .add_credits = smb2_add_credits,
2934 .set_credits = smb2_set_credits,
2935 .get_credits_field = smb2_get_credits_field,
2936 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002937 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002938 .get_next_mid = smb2_get_next_mid,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002939 .read_data_offset = smb2_read_data_offset,
2940 .read_data_length = smb2_read_data_length,
2941 .map_error = map_smb2_to_linux_error,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04002942 .find_mid = smb2_find_mid,
2943 .check_message = smb2_check_message,
2944 .dump_detail = smb2_dump_detail,
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04002945 .clear_stats = smb2_clear_stats,
2946 .print_stats = smb2_print_stats,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002947 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08002948 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002949 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04002950 .need_neg = smb2_need_neg,
2951 .negotiate = smb2_negotiate,
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -07002952 .negotiate_wsize = smb2_negotiate_wsize,
2953 .negotiate_rsize = smb2_negotiate_rsize,
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04002954 .sess_setup = SMB2_sess_setup,
2955 .logoff = SMB2_logoff,
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04002956 .tree_connect = SMB2_tcon,
2957 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05002958 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002959 .is_path_accessible = smb2_is_path_accessible,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002960 .can_echo = smb2_can_echo,
2961 .echo = SMB2_echo,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002962 .query_path_info = smb2_query_path_info,
2963 .get_srv_inum = smb2_get_srv_inum,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -07002964 .query_file_info = smb2_query_file_info,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002965 .set_path_size = smb2_set_path_size,
2966 .set_file_size = smb2_set_file_size,
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07002967 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05002968 .set_compression = smb2_set_compression,
Pavel Shilovskya0e73182011-07-19 12:56:37 +04002969 .mkdir = smb2_mkdir,
2970 .mkdir_setinfo = smb2_mkdir_setinfo,
Pavel Shilovsky1a500f02012-07-10 16:14:38 +04002971 .rmdir = smb2_rmdir,
Pavel Shilovskycbe6f432012-09-18 16:20:25 -07002972 .unlink = smb2_unlink,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002973 .rename = smb2_rename_path,
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002974 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002975 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05002976 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05002977 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002978 .open = smb2_open_file,
2979 .set_fid = smb2_set_fid,
2980 .close = smb2_close_file,
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002981 .flush = smb2_flush_file,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002982 .async_readv = smb2_async_readv,
Pavel Shilovsky33319142012-09-18 16:20:29 -07002983 .async_writev = smb2_async_writev,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002984 .sync_read = smb2_sync_read,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002985 .sync_write = smb2_sync_write,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002986 .query_dir_first = smb2_query_dir_first,
2987 .query_dir_next = smb2_query_dir_next,
2988 .close_dir = smb2_close_dir,
2989 .calc_smb_size = smb2_calc_size,
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002990 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002991 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002992 .oplock_response = smb2_oplock_response,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002993 .queryfs = smb2_queryfs,
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07002994 .mand_lock = smb2_mand_lock,
2995 .mand_unlock_range = smb2_unlock_range,
Pavel Shilovskyb1407992012-09-19 06:22:44 -07002996 .push_mand_locks = smb2_push_mandatory_locks,
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002997 .get_lease_key = smb2_get_lease_key,
2998 .set_lease_key = smb2_set_lease_key,
2999 .new_lease_key = smb2_new_lease_key,
Steve French38107d42012-12-08 22:08:06 -06003000 .calc_signature = smb2_calc_signature,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003001 .is_read_op = smb21_is_read_op,
3002 .set_oplock_level = smb21_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003003 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04003004 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05003005 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04003006 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04003007 .dir_needs_close = smb2_dir_needs_close,
Steve French834170c2016-09-30 21:14:26 -05003008 .enum_snapshots = smb3_enum_snapshots,
Aurelien Aptel9d496402017-02-13 16:16:49 +01003009 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05303010 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003011#ifdef CONFIG_CIFS_XATTR
3012 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003013 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003014#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003015#ifdef CONFIG_CIFS_ACL
3016 .get_acl = get_smb2_acl,
3017 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003018 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003019#endif /* CIFS_ACL */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10003020 .next_header = smb2_next_header,
Steve French38107d42012-12-08 22:08:06 -06003021};
3022
Steve French38107d42012-12-08 22:08:06 -06003023struct smb_version_operations smb30_operations = {
3024 .compare_fids = smb2_compare_fids,
3025 .setup_request = smb2_setup_request,
3026 .setup_async_request = smb2_setup_async_request,
3027 .check_receive = smb2_check_receive,
3028 .add_credits = smb2_add_credits,
3029 .set_credits = smb2_set_credits,
3030 .get_credits_field = smb2_get_credits_field,
3031 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003032 .wait_mtu_credits = smb2_wait_mtu_credits,
Steve French38107d42012-12-08 22:08:06 -06003033 .get_next_mid = smb2_get_next_mid,
3034 .read_data_offset = smb2_read_data_offset,
3035 .read_data_length = smb2_read_data_length,
3036 .map_error = map_smb2_to_linux_error,
3037 .find_mid = smb2_find_mid,
3038 .check_message = smb2_check_message,
3039 .dump_detail = smb2_dump_detail,
3040 .clear_stats = smb2_clear_stats,
3041 .print_stats = smb2_print_stats,
Steve French769ee6a2013-06-19 14:15:30 -05003042 .dump_share_caps = smb2_dump_share_caps,
Steve French38107d42012-12-08 22:08:06 -06003043 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08003044 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00003045 .downgrade_oplock = smb2_downgrade_oplock,
Steve French38107d42012-12-08 22:08:06 -06003046 .need_neg = smb2_need_neg,
3047 .negotiate = smb2_negotiate,
3048 .negotiate_wsize = smb2_negotiate_wsize,
3049 .negotiate_rsize = smb2_negotiate_rsize,
3050 .sess_setup = SMB2_sess_setup,
3051 .logoff = SMB2_logoff,
3052 .tree_connect = SMB2_tcon,
3053 .tree_disconnect = SMB2_tdis,
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003054 .qfs_tcon = smb3_qfs_tcon,
Steve French38107d42012-12-08 22:08:06 -06003055 .is_path_accessible = smb2_is_path_accessible,
3056 .can_echo = smb2_can_echo,
3057 .echo = SMB2_echo,
3058 .query_path_info = smb2_query_path_info,
3059 .get_srv_inum = smb2_get_srv_inum,
3060 .query_file_info = smb2_query_file_info,
3061 .set_path_size = smb2_set_path_size,
3062 .set_file_size = smb2_set_file_size,
3063 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05003064 .set_compression = smb2_set_compression,
Steve French38107d42012-12-08 22:08:06 -06003065 .mkdir = smb2_mkdir,
3066 .mkdir_setinfo = smb2_mkdir_setinfo,
3067 .rmdir = smb2_rmdir,
3068 .unlink = smb2_unlink,
3069 .rename = smb2_rename_path,
3070 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003071 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05003072 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05003073 .create_mf_symlink = smb3_create_mf_symlink,
Steve French38107d42012-12-08 22:08:06 -06003074 .open = smb2_open_file,
3075 .set_fid = smb2_set_fid,
3076 .close = smb2_close_file,
3077 .flush = smb2_flush_file,
3078 .async_readv = smb2_async_readv,
3079 .async_writev = smb2_async_writev,
3080 .sync_read = smb2_sync_read,
3081 .sync_write = smb2_sync_write,
3082 .query_dir_first = smb2_query_dir_first,
3083 .query_dir_next = smb2_query_dir_next,
3084 .close_dir = smb2_close_dir,
3085 .calc_smb_size = smb2_calc_size,
3086 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07003087 .is_session_expired = smb2_is_session_expired,
Steve French38107d42012-12-08 22:08:06 -06003088 .oplock_response = smb2_oplock_response,
3089 .queryfs = smb2_queryfs,
3090 .mand_lock = smb2_mand_lock,
3091 .mand_unlock_range = smb2_unlock_range,
3092 .push_mand_locks = smb2_push_mandatory_locks,
3093 .get_lease_key = smb2_get_lease_key,
3094 .set_lease_key = smb2_set_lease_key,
3095 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06003096 .generate_signingkey = generate_smb30signingkey,
Steve French38107d42012-12-08 22:08:06 -06003097 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05003098 .set_integrity = smb3_set_integrity,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003099 .is_read_op = smb21_is_read_op,
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003100 .set_oplock_level = smb3_set_oplock_level,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003101 .create_lease_buf = smb3_create_lease_buf,
3102 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05003103 .copychunk_range = smb2_copychunk_range,
Steve Frenchca9e7a12015-10-01 21:40:10 -05003104 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchff1c0382013-11-19 23:44:46 -06003105 .validate_negotiate = smb3_validate_negotiate,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04003106 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04003107 .dir_needs_close = smb2_dir_needs_close,
Steve French31742c52014-08-17 08:38:47 -05003108 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05003109 .enum_snapshots = smb3_enum_snapshots,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003110 .init_transform_rq = smb3_init_transform_rq,
3111 .free_transform_rq = smb3_free_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003112 .is_transform_hdr = smb3_is_transform_hdr,
3113 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01003114 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05303115 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003116#ifdef CONFIG_CIFS_XATTR
3117 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003118 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003119#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003120#ifdef CONFIG_CIFS_ACL
3121 .get_acl = get_smb2_acl,
3122 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003123 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003124#endif /* CIFS_ACL */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10003125 .next_header = smb2_next_header,
Steve French1080ef72011-02-24 18:07:19 +00003126};
3127
Steve Frenchaab18932015-06-23 23:37:11 -05003128#ifdef CONFIG_CIFS_SMB311
3129struct smb_version_operations smb311_operations = {
3130 .compare_fids = smb2_compare_fids,
3131 .setup_request = smb2_setup_request,
3132 .setup_async_request = smb2_setup_async_request,
3133 .check_receive = smb2_check_receive,
3134 .add_credits = smb2_add_credits,
3135 .set_credits = smb2_set_credits,
3136 .get_credits_field = smb2_get_credits_field,
3137 .get_credits = smb2_get_credits,
3138 .wait_mtu_credits = smb2_wait_mtu_credits,
3139 .get_next_mid = smb2_get_next_mid,
3140 .read_data_offset = smb2_read_data_offset,
3141 .read_data_length = smb2_read_data_length,
3142 .map_error = map_smb2_to_linux_error,
3143 .find_mid = smb2_find_mid,
3144 .check_message = smb2_check_message,
3145 .dump_detail = smb2_dump_detail,
3146 .clear_stats = smb2_clear_stats,
3147 .print_stats = smb2_print_stats,
3148 .dump_share_caps = smb2_dump_share_caps,
3149 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08003150 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Steve Frenchaab18932015-06-23 23:37:11 -05003151 .downgrade_oplock = smb2_downgrade_oplock,
3152 .need_neg = smb2_need_neg,
3153 .negotiate = smb2_negotiate,
3154 .negotiate_wsize = smb2_negotiate_wsize,
3155 .negotiate_rsize = smb2_negotiate_rsize,
3156 .sess_setup = SMB2_sess_setup,
3157 .logoff = SMB2_logoff,
3158 .tree_connect = SMB2_tcon,
3159 .tree_disconnect = SMB2_tdis,
3160 .qfs_tcon = smb3_qfs_tcon,
3161 .is_path_accessible = smb2_is_path_accessible,
3162 .can_echo = smb2_can_echo,
3163 .echo = SMB2_echo,
3164 .query_path_info = smb2_query_path_info,
3165 .get_srv_inum = smb2_get_srv_inum,
3166 .query_file_info = smb2_query_file_info,
3167 .set_path_size = smb2_set_path_size,
3168 .set_file_size = smb2_set_file_size,
3169 .set_file_info = smb2_set_file_info,
3170 .set_compression = smb2_set_compression,
3171 .mkdir = smb2_mkdir,
3172 .mkdir_setinfo = smb2_mkdir_setinfo,
3173 .rmdir = smb2_rmdir,
3174 .unlink = smb2_unlink,
3175 .rename = smb2_rename_path,
3176 .create_hardlink = smb2_create_hardlink,
3177 .query_symlink = smb2_query_symlink,
3178 .query_mf_symlink = smb3_query_mf_symlink,
3179 .create_mf_symlink = smb3_create_mf_symlink,
3180 .open = smb2_open_file,
3181 .set_fid = smb2_set_fid,
3182 .close = smb2_close_file,
3183 .flush = smb2_flush_file,
3184 .async_readv = smb2_async_readv,
3185 .async_writev = smb2_async_writev,
3186 .sync_read = smb2_sync_read,
3187 .sync_write = smb2_sync_write,
3188 .query_dir_first = smb2_query_dir_first,
3189 .query_dir_next = smb2_query_dir_next,
3190 .close_dir = smb2_close_dir,
3191 .calc_smb_size = smb2_calc_size,
3192 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07003193 .is_session_expired = smb2_is_session_expired,
Steve Frenchaab18932015-06-23 23:37:11 -05003194 .oplock_response = smb2_oplock_response,
3195 .queryfs = smb2_queryfs,
3196 .mand_lock = smb2_mand_lock,
3197 .mand_unlock_range = smb2_unlock_range,
3198 .push_mand_locks = smb2_push_mandatory_locks,
3199 .get_lease_key = smb2_get_lease_key,
3200 .set_lease_key = smb2_set_lease_key,
3201 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06003202 .generate_signingkey = generate_smb311signingkey,
Steve Frenchaab18932015-06-23 23:37:11 -05003203 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05003204 .set_integrity = smb3_set_integrity,
Steve Frenchaab18932015-06-23 23:37:11 -05003205 .is_read_op = smb21_is_read_op,
3206 .set_oplock_level = smb3_set_oplock_level,
3207 .create_lease_buf = smb3_create_lease_buf,
3208 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05003209 .copychunk_range = smb2_copychunk_range,
Steve French02b16662015-06-27 21:18:36 -07003210 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchaab18932015-06-23 23:37:11 -05003211/* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
3212 .wp_retry_size = smb2_wp_retry_size,
3213 .dir_needs_close = smb2_dir_needs_close,
3214 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05003215 .enum_snapshots = smb3_enum_snapshots,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003216 .init_transform_rq = smb3_init_transform_rq,
3217 .free_transform_rq = smb3_free_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003218 .is_transform_hdr = smb3_is_transform_hdr,
3219 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01003220 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05303221 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003222#ifdef CONFIG_CIFS_XATTR
3223 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003224 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003225#endif /* CIFS_XATTR */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10003226 .next_header = smb2_next_header,
Steve Frenchaab18932015-06-23 23:37:11 -05003227};
3228#endif /* CIFS_SMB311 */
3229
Steve Frenchdd446b12012-11-28 23:21:06 -06003230struct smb_version_values smb20_values = {
3231 .version_string = SMB20_VERSION_STRING,
3232 .protocol_id = SMB20_PROT_ID,
3233 .req_capabilities = 0, /* MBZ */
3234 .large_lock_type = 0,
3235 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3236 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3237 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003238 .header_size = sizeof(struct smb2_sync_hdr),
3239 .header_preamble_size = 0,
Steve Frenchdd446b12012-11-28 23:21:06 -06003240 .max_header_size = MAX_SMB2_HDR_SIZE,
3241 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3242 .lock_cmd = SMB2_LOCK,
3243 .cap_unix = 0,
3244 .cap_nt_find = SMB2_NT_FIND,
3245 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003246 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3247 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003248 .create_lease_size = sizeof(struct create_lease),
Steve Frenchdd446b12012-11-28 23:21:06 -06003249};
3250
Steve French1080ef72011-02-24 18:07:19 +00003251struct smb_version_values smb21_values = {
3252 .version_string = SMB21_VERSION_STRING,
Steve Frenche4aa25e2012-10-01 12:26:22 -05003253 .protocol_id = SMB21_PROT_ID,
3254 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
3255 .large_lock_type = 0,
3256 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3257 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3258 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003259 .header_size = sizeof(struct smb2_sync_hdr),
3260 .header_preamble_size = 0,
Steve Frenche4aa25e2012-10-01 12:26:22 -05003261 .max_header_size = MAX_SMB2_HDR_SIZE,
3262 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3263 .lock_cmd = SMB2_LOCK,
3264 .cap_unix = 0,
3265 .cap_nt_find = SMB2_NT_FIND,
3266 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003267 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3268 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003269 .create_lease_size = sizeof(struct create_lease),
Steve Frenche4aa25e2012-10-01 12:26:22 -05003270};
3271
Steve French9764c022017-09-17 10:41:35 -05003272struct smb_version_values smb3any_values = {
3273 .version_string = SMB3ANY_VERSION_STRING,
3274 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3275 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
3276 .large_lock_type = 0,
3277 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3278 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3279 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003280 .header_size = sizeof(struct smb2_sync_hdr),
3281 .header_preamble_size = 0,
Steve French9764c022017-09-17 10:41:35 -05003282 .max_header_size = MAX_SMB2_HDR_SIZE,
3283 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3284 .lock_cmd = SMB2_LOCK,
3285 .cap_unix = 0,
3286 .cap_nt_find = SMB2_NT_FIND,
3287 .cap_large_files = SMB2_LARGE_FILES,
3288 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3289 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3290 .create_lease_size = sizeof(struct create_lease_v2),
3291};
3292
3293struct smb_version_values smbdefault_values = {
3294 .version_string = SMBDEFAULT_VERSION_STRING,
3295 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3296 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
3297 .large_lock_type = 0,
3298 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3299 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3300 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003301 .header_size = sizeof(struct smb2_sync_hdr),
3302 .header_preamble_size = 0,
Steve French9764c022017-09-17 10:41:35 -05003303 .max_header_size = MAX_SMB2_HDR_SIZE,
3304 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3305 .lock_cmd = SMB2_LOCK,
3306 .cap_unix = 0,
3307 .cap_nt_find = SMB2_NT_FIND,
3308 .cap_large_files = SMB2_LARGE_FILES,
3309 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3310 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3311 .create_lease_size = sizeof(struct create_lease_v2),
3312};
3313
Steve Frenche4aa25e2012-10-01 12:26:22 -05003314struct smb_version_values smb30_values = {
3315 .version_string = SMB30_VERSION_STRING,
3316 .protocol_id = SMB30_PROT_ID,
Steve French373512e2015-12-18 13:05:30 -06003317 .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 -07003318 .large_lock_type = 0,
3319 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3320 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3321 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003322 .header_size = sizeof(struct smb2_sync_hdr),
3323 .header_preamble_size = 0,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04003324 .max_header_size = MAX_SMB2_HDR_SIZE,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003325 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04003326 .lock_cmd = SMB2_LOCK,
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04003327 .cap_unix = 0,
3328 .cap_nt_find = SMB2_NT_FIND,
3329 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003330 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3331 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003332 .create_lease_size = sizeof(struct create_lease_v2),
Steve French1080ef72011-02-24 18:07:19 +00003333};
Steve French20b6d8b2013-06-12 22:48:41 -05003334
3335struct smb_version_values smb302_values = {
3336 .version_string = SMB302_VERSION_STRING,
3337 .protocol_id = SMB302_PROT_ID,
Steve French373512e2015-12-18 13:05:30 -06003338 .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 -05003339 .large_lock_type = 0,
3340 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3341 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3342 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003343 .header_size = sizeof(struct smb2_sync_hdr),
3344 .header_preamble_size = 0,
Steve French20b6d8b2013-06-12 22:48:41 -05003345 .max_header_size = MAX_SMB2_HDR_SIZE,
3346 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3347 .lock_cmd = SMB2_LOCK,
3348 .cap_unix = 0,
3349 .cap_nt_find = SMB2_NT_FIND,
3350 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003351 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3352 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003353 .create_lease_size = sizeof(struct create_lease_v2),
Steve French20b6d8b2013-06-12 22:48:41 -05003354};
Steve French5f7fbf72014-12-17 22:52:58 -06003355
3356#ifdef CONFIG_CIFS_SMB311
3357struct smb_version_values smb311_values = {
3358 .version_string = SMB311_VERSION_STRING,
3359 .protocol_id = SMB311_PROT_ID,
Steve French19558802017-06-21 16:50:20 -05003360 .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 -06003361 .large_lock_type = 0,
3362 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3363 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3364 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003365 .header_size = sizeof(struct smb2_sync_hdr),
3366 .header_preamble_size = 0,
Steve French5f7fbf72014-12-17 22:52:58 -06003367 .max_header_size = MAX_SMB2_HDR_SIZE,
3368 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3369 .lock_cmd = SMB2_LOCK,
3370 .cap_unix = 0,
3371 .cap_nt_find = SMB2_NT_FIND,
3372 .cap_large_files = SMB2_LARGE_FILES,
3373 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3374 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3375 .create_lease_size = sizeof(struct create_lease_v2),
3376};
3377#endif /* SMB311 */