blob: 9153407f97e8aa469c436cc9b6f8fd2a6bd910e5 [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
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000326void
327smb2_cached_lease_break(struct work_struct *work)
328{
329 struct cached_fid *cfid = container_of(work,
330 struct cached_fid, lease_break);
331 mutex_lock(&cfid->fid_mutex);
332 if (cfid->is_valid) {
333 cifs_dbg(FYI, "clear cached root file handle\n");
334 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
335 cfid->fid->volatile_fid);
336 cfid->is_valid = false;
337 }
338 mutex_unlock(&cfid->fid_mutex);
339}
340
Steve French3d4ef9a2018-04-25 22:19:09 -0500341/*
342 * Open the directory at the root of a share
343 */
344int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
345{
346 struct cifs_open_parms oparams;
347 int rc;
348 __le16 srch_path = 0; /* Null - since an open of top of share */
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000349 u8 oplock = SMB2_OPLOCK_LEVEL_II;
Steve French3d4ef9a2018-04-25 22:19:09 -0500350
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000351 mutex_lock(&tcon->crfid.fid_mutex);
352 if (tcon->crfid.is_valid) {
Steve French3d4ef9a2018-04-25 22:19:09 -0500353 cifs_dbg(FYI, "found a cached root file handle\n");
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000354 memcpy(pfid, tcon->crfid.fid, sizeof(struct cifs_fid));
355 mutex_unlock(&tcon->crfid.fid_mutex);
Steve French3d4ef9a2018-04-25 22:19:09 -0500356 return 0;
357 }
358
359 oparams.tcon = tcon;
360 oparams.create_options = 0;
361 oparams.desired_access = FILE_READ_ATTRIBUTES;
362 oparams.disposition = FILE_OPEN;
363 oparams.fid = pfid;
364 oparams.reconnect = false;
365
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000366 rc = SMB2_open(xid, &oparams, &srch_path, &oplock, NULL, NULL, NULL);
Steve French3d4ef9a2018-04-25 22:19:09 -0500367 if (rc == 0) {
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000368 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
369 tcon->crfid.tcon = tcon;
370 tcon->crfid.is_valid = true;
Steve French3d4ef9a2018-04-25 22:19:09 -0500371 }
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000372 mutex_unlock(&tcon->crfid.fid_mutex);
Steve French3d4ef9a2018-04-25 22:19:09 -0500373 return rc;
374}
375
Steve French34f62642013-10-09 02:07:00 -0500376static void
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500377smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
378{
379 int rc;
380 __le16 srch_path = 0; /* Null - open root of share */
381 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
382 struct cifs_open_parms oparms;
383 struct cifs_fid fid;
Steve French3d4ef9a2018-04-25 22:19:09 -0500384 bool no_cached_open = tcon->nohandlecache;
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500385
386 oparms.tcon = tcon;
387 oparms.desired_access = FILE_READ_ATTRIBUTES;
388 oparms.disposition = FILE_OPEN;
389 oparms.create_options = 0;
390 oparms.fid = &fid;
391 oparms.reconnect = false;
392
Steve French3d4ef9a2018-04-25 22:19:09 -0500393 if (no_cached_open)
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000394 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
395 NULL);
Steve French3d4ef9a2018-04-25 22:19:09 -0500396 else
397 rc = open_shroot(xid, tcon, &fid);
398
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500399 if (rc)
400 return;
401
Steve Frenchc481e9f2013-10-14 01:21:53 -0500402#ifdef CONFIG_CIFS_STATS2
403 SMB3_request_interfaces(xid, tcon);
404#endif /* STATS2 */
405
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500406 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
407 FS_ATTRIBUTE_INFORMATION);
408 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
409 FS_DEVICE_INFORMATION);
410 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
411 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
Steve French3d4ef9a2018-04-25 22:19:09 -0500412 if (no_cached_open)
413 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500414 return;
415}
416
417static void
Steve French34f62642013-10-09 02:07:00 -0500418smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
419{
420 int rc;
421 __le16 srch_path = 0; /* Null - open root of share */
422 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
423 struct cifs_open_parms oparms;
424 struct cifs_fid fid;
425
426 oparms.tcon = tcon;
427 oparms.desired_access = FILE_READ_ATTRIBUTES;
428 oparms.disposition = FILE_OPEN;
429 oparms.create_options = 0;
430 oparms.fid = &fid;
431 oparms.reconnect = false;
432
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000433 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
Steve French34f62642013-10-09 02:07:00 -0500434 if (rc)
435 return;
436
Steven French21671142013-10-09 13:36:35 -0500437 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
438 FS_ATTRIBUTE_INFORMATION);
439 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
440 FS_DEVICE_INFORMATION);
Steve French34f62642013-10-09 02:07:00 -0500441 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
442 return;
443}
444
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400445static int
446smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
447 struct cifs_sb_info *cifs_sb, const char *full_path)
448{
449 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400450 __le16 *utf16_path;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700451 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400452 struct cifs_open_parms oparms;
453 struct cifs_fid fid;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400454
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000455 if ((*full_path == 0) && tcon->crfid.is_valid)
Steve French3d4ef9a2018-04-25 22:19:09 -0500456 return 0;
457
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400458 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
459 if (!utf16_path)
460 return -ENOMEM;
461
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400462 oparms.tcon = tcon;
463 oparms.desired_access = FILE_READ_ATTRIBUTES;
464 oparms.disposition = FILE_OPEN;
465 oparms.create_options = 0;
466 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +0400467 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400468
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000469 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400470 if (rc) {
471 kfree(utf16_path);
472 return rc;
473 }
474
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400475 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400476 kfree(utf16_path);
477 return rc;
478}
479
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400480static int
481smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
482 struct cifs_sb_info *cifs_sb, const char *full_path,
483 u64 *uniqueid, FILE_ALL_INFO *data)
484{
485 *uniqueid = le64_to_cpu(data->IndexNumber);
486 return 0;
487}
488
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700489static int
490smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
491 struct cifs_fid *fid, FILE_ALL_INFO *data)
492{
493 int rc;
494 struct smb2_file_all_info *smb2_data;
495
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +0400496 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700497 GFP_KERNEL);
498 if (smb2_data == NULL)
499 return -ENOMEM;
500
501 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
502 smb2_data);
503 if (!rc)
504 move_smb2_info_to_cifs(data, smb2_data);
505 kfree(smb2_data);
506 return rc;
507}
508
Arnd Bergmann1368f152017-09-05 11:24:15 +0200509#ifdef CONFIG_CIFS_XATTR
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000510static ssize_t
511move_smb2_ea_to_cifs(char *dst, size_t dst_size,
512 struct smb2_file_full_ea_info *src, size_t src_size,
513 const unsigned char *ea_name)
514{
515 int rc = 0;
516 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
517 char *name, *value;
518 size_t name_len, value_len, user_name_len;
519
520 while (src_size > 0) {
521 name = &src->ea_data[0];
522 name_len = (size_t)src->ea_name_length;
523 value = &src->ea_data[src->ea_name_length + 1];
524 value_len = (size_t)le16_to_cpu(src->ea_value_length);
525
526 if (name_len == 0) {
527 break;
528 }
529
530 if (src_size < 8 + name_len + 1 + value_len) {
531 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
532 rc = -EIO;
533 goto out;
534 }
535
536 if (ea_name) {
537 if (ea_name_len == name_len &&
538 memcmp(ea_name, name, name_len) == 0) {
539 rc = value_len;
540 if (dst_size == 0)
541 goto out;
542 if (dst_size < value_len) {
543 rc = -ERANGE;
544 goto out;
545 }
546 memcpy(dst, value, value_len);
547 goto out;
548 }
549 } else {
550 /* 'user.' plus a terminating null */
551 user_name_len = 5 + 1 + name_len;
552
553 rc += user_name_len;
554
555 if (dst_size >= user_name_len) {
556 dst_size -= user_name_len;
557 memcpy(dst, "user.", 5);
558 dst += 5;
559 memcpy(dst, src->ea_data, name_len);
560 dst += name_len;
561 *dst = 0;
562 ++dst;
563 } else if (dst_size == 0) {
564 /* skip copy - calc size only */
565 } else {
566 /* stop before overrun buffer */
567 rc = -ERANGE;
568 break;
569 }
570 }
571
572 if (!src->next_entry_offset)
573 break;
574
575 if (src_size < le32_to_cpu(src->next_entry_offset)) {
576 /* stop before overrun buffer */
577 rc = -ERANGE;
578 break;
579 }
580 src_size -= le32_to_cpu(src->next_entry_offset);
581 src = (void *)((char *)src +
582 le32_to_cpu(src->next_entry_offset));
583 }
584
585 /* didn't find the named attribute */
586 if (ea_name)
587 rc = -ENODATA;
588
589out:
590 return (ssize_t)rc;
591}
592
593static ssize_t
594smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
595 const unsigned char *path, const unsigned char *ea_name,
596 char *ea_data, size_t buf_size,
597 struct cifs_sb_info *cifs_sb)
598{
599 int rc;
600 __le16 *utf16_path;
601 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
602 struct cifs_open_parms oparms;
603 struct cifs_fid fid;
604 struct smb2_file_full_ea_info *smb2_data;
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +1000605 int ea_buf_size = SMB2_MIN_EA_BUF;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000606
607 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
608 if (!utf16_path)
609 return -ENOMEM;
610
611 oparms.tcon = tcon;
612 oparms.desired_access = FILE_READ_EA;
613 oparms.disposition = FILE_OPEN;
614 oparms.create_options = 0;
615 oparms.fid = &fid;
616 oparms.reconnect = false;
617
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000618 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000619 kfree(utf16_path);
620 if (rc) {
621 cifs_dbg(FYI, "open failed rc=%d\n", rc);
622 return rc;
623 }
624
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +1000625 while (1) {
626 smb2_data = kzalloc(ea_buf_size, GFP_KERNEL);
627 if (smb2_data == NULL) {
628 SMB2_close(xid, tcon, fid.persistent_fid,
629 fid.volatile_fid);
630 return -ENOMEM;
631 }
632
633 rc = SMB2_query_eas(xid, tcon, fid.persistent_fid,
634 fid.volatile_fid,
635 ea_buf_size, smb2_data);
636
637 if (rc != -E2BIG)
638 break;
639
640 kfree(smb2_data);
641 ea_buf_size <<= 1;
642
643 if (ea_buf_size > SMB2_MAX_EA_BUF) {
644 cifs_dbg(VFS, "EA size is too large\n");
645 SMB2_close(xid, tcon, fid.persistent_fid,
646 fid.volatile_fid);
647 return -ENOMEM;
648 }
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000649 }
650
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000651 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
652
Paulo Alcantaraae2cd7f2018-05-04 11:25:26 -0300653 /*
654 * If ea_name is NULL (listxattr) and there are no EAs, return 0 as it's
655 * not an error. Otherwise, the specified ea_name was not found.
656 */
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000657 if (!rc)
658 rc = move_smb2_ea_to_cifs(ea_data, buf_size, smb2_data,
659 SMB2_MAX_EA_BUF, ea_name);
Paulo Alcantaraae2cd7f2018-05-04 11:25:26 -0300660 else if (!ea_name && rc == -ENODATA)
661 rc = 0;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000662
663 kfree(smb2_data);
664 return rc;
665}
666
Ronnie Sahlberg55175542017-08-24 11:24:56 +1000667
668static int
669smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
670 const char *path, const char *ea_name, const void *ea_value,
671 const __u16 ea_value_len, const struct nls_table *nls_codepage,
672 struct cifs_sb_info *cifs_sb)
673{
674 int rc;
675 __le16 *utf16_path;
676 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
677 struct cifs_open_parms oparms;
678 struct cifs_fid fid;
679 struct smb2_file_full_ea_info *ea;
680 int ea_name_len = strlen(ea_name);
681 int len;
682
683 if (ea_name_len > 255)
684 return -EINVAL;
685
686 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
687 if (!utf16_path)
688 return -ENOMEM;
689
690 oparms.tcon = tcon;
691 oparms.desired_access = FILE_WRITE_EA;
692 oparms.disposition = FILE_OPEN;
693 oparms.create_options = 0;
694 oparms.fid = &fid;
695 oparms.reconnect = false;
696
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000697 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Ronnie Sahlberg55175542017-08-24 11:24:56 +1000698 kfree(utf16_path);
699 if (rc) {
700 cifs_dbg(FYI, "open failed rc=%d\n", rc);
701 return rc;
702 }
703
704 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
705 ea = kzalloc(len, GFP_KERNEL);
706 if (ea == NULL) {
707 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
708 return -ENOMEM;
709 }
710
711 ea->ea_name_length = ea_name_len;
712 ea->ea_value_length = cpu_to_le16(ea_value_len);
713 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
714 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
715
716 rc = SMB2_set_ea(xid, tcon, fid.persistent_fid, fid.volatile_fid, ea,
717 len);
718 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
719
720 return rc;
721}
Arnd Bergmann1368f152017-09-05 11:24:15 +0200722#endif
Ronnie Sahlberg55175542017-08-24 11:24:56 +1000723
Pavel Shilovsky9094fad2012-07-12 18:30:44 +0400724static bool
725smb2_can_echo(struct TCP_Server_Info *server)
726{
727 return server->echoes;
728}
729
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400730static void
731smb2_clear_stats(struct cifs_tcon *tcon)
732{
733#ifdef CONFIG_CIFS_STATS
734 int i;
735 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
736 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
737 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
738 }
739#endif
740}
741
742static void
Steve French769ee6a2013-06-19 14:15:30 -0500743smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
744{
745 seq_puts(m, "\n\tShare Capabilities:");
746 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
747 seq_puts(m, " DFS,");
748 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
749 seq_puts(m, " CONTINUOUS AVAILABILITY,");
750 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
751 seq_puts(m, " SCALEOUT,");
752 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
753 seq_puts(m, " CLUSTER,");
754 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
755 seq_puts(m, " ASYMMETRIC,");
756 if (tcon->capabilities == 0)
757 seq_puts(m, " None");
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500758 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
759 seq_puts(m, " Aligned,");
760 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
761 seq_puts(m, " Partition Aligned,");
762 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
763 seq_puts(m, " SSD,");
764 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
765 seq_puts(m, " TRIM-support,");
766
Steve French769ee6a2013-06-19 14:15:30 -0500767 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
Steve Frenche0386e42018-05-20 01:27:03 -0500768 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500769 if (tcon->perf_sector_size)
770 seq_printf(m, "\tOptimal sector size: 0x%x",
771 tcon->perf_sector_size);
Steve Frenche0386e42018-05-20 01:27:03 -0500772 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
Steve French769ee6a2013-06-19 14:15:30 -0500773}
774
775static void
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400776smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
777{
778#ifdef CONFIG_CIFS_STATS
779 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
780 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
781 seq_printf(m, "\nNegotiates: %d sent %d failed",
782 atomic_read(&sent[SMB2_NEGOTIATE_HE]),
783 atomic_read(&failed[SMB2_NEGOTIATE_HE]));
784 seq_printf(m, "\nSessionSetups: %d sent %d failed",
785 atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
786 atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400787 seq_printf(m, "\nLogoffs: %d sent %d failed",
788 atomic_read(&sent[SMB2_LOGOFF_HE]),
789 atomic_read(&failed[SMB2_LOGOFF_HE]));
790 seq_printf(m, "\nTreeConnects: %d sent %d failed",
791 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
792 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
793 seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
794 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
795 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
796 seq_printf(m, "\nCreates: %d sent %d failed",
797 atomic_read(&sent[SMB2_CREATE_HE]),
798 atomic_read(&failed[SMB2_CREATE_HE]));
799 seq_printf(m, "\nCloses: %d sent %d failed",
800 atomic_read(&sent[SMB2_CLOSE_HE]),
801 atomic_read(&failed[SMB2_CLOSE_HE]));
802 seq_printf(m, "\nFlushes: %d sent %d failed",
803 atomic_read(&sent[SMB2_FLUSH_HE]),
804 atomic_read(&failed[SMB2_FLUSH_HE]));
805 seq_printf(m, "\nReads: %d sent %d failed",
806 atomic_read(&sent[SMB2_READ_HE]),
807 atomic_read(&failed[SMB2_READ_HE]));
808 seq_printf(m, "\nWrites: %d sent %d failed",
809 atomic_read(&sent[SMB2_WRITE_HE]),
810 atomic_read(&failed[SMB2_WRITE_HE]));
811 seq_printf(m, "\nLocks: %d sent %d failed",
812 atomic_read(&sent[SMB2_LOCK_HE]),
813 atomic_read(&failed[SMB2_LOCK_HE]));
814 seq_printf(m, "\nIOCTLs: %d sent %d failed",
815 atomic_read(&sent[SMB2_IOCTL_HE]),
816 atomic_read(&failed[SMB2_IOCTL_HE]));
817 seq_printf(m, "\nCancels: %d sent %d failed",
818 atomic_read(&sent[SMB2_CANCEL_HE]),
819 atomic_read(&failed[SMB2_CANCEL_HE]));
820 seq_printf(m, "\nEchos: %d sent %d failed",
821 atomic_read(&sent[SMB2_ECHO_HE]),
822 atomic_read(&failed[SMB2_ECHO_HE]));
823 seq_printf(m, "\nQueryDirectories: %d sent %d failed",
824 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
825 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
826 seq_printf(m, "\nChangeNotifies: %d sent %d failed",
827 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
828 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
829 seq_printf(m, "\nQueryInfos: %d sent %d failed",
830 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
831 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
832 seq_printf(m, "\nSetInfos: %d sent %d failed",
833 atomic_read(&sent[SMB2_SET_INFO_HE]),
834 atomic_read(&failed[SMB2_SET_INFO_HE]));
835 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
836 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
837 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
838#endif
839}
840
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700841static void
842smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
843{
David Howells2b0143b2015-03-17 22:25:59 +0000844 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovsky53ef1012013-09-05 16:11:28 +0400845 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
846
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700847 cfile->fid.persistent_fid = fid->persistent_fid;
848 cfile->fid.volatile_fid = fid->volatile_fid;
Pavel Shilovsky42873b02013-09-05 21:30:16 +0400849 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
850 &fid->purge_cache);
Pavel Shilovsky18cceb62013-09-05 13:01:06 +0400851 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
Aurelien Aptel94f87372016-09-22 07:38:50 +0200852 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700853}
854
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400855static void
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700856smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
857 struct cifs_fid *fid)
858{
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400859 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700860}
861
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -0700862static int
Steve French41c13582013-11-14 00:05:36 -0600863SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
864 u64 persistent_fid, u64 volatile_fid,
865 struct copychunk_ioctl *pcchunk)
866{
867 int rc;
868 unsigned int ret_data_len;
869 struct resume_key_req *res_key;
870
871 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
872 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
873 NULL, 0 /* no input */,
874 (char **)&res_key, &ret_data_len);
875
876 if (rc) {
877 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
878 goto req_res_key_exit;
879 }
880 if (ret_data_len < sizeof(struct resume_key_req)) {
881 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
882 rc = -EINVAL;
883 goto req_res_key_exit;
884 }
885 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
886
887req_res_key_exit:
888 kfree(res_key);
889 return rc;
890}
891
Sachin Prabhu620d8742017-02-10 16:03:51 +0530892static ssize_t
Sachin Prabhu312bbc52017-04-04 02:12:04 -0500893smb2_copychunk_range(const unsigned int xid,
Steve French41c13582013-11-14 00:05:36 -0600894 struct cifsFileInfo *srcfile,
895 struct cifsFileInfo *trgtfile, u64 src_off,
896 u64 len, u64 dest_off)
897{
898 int rc;
899 unsigned int ret_data_len;
900 struct copychunk_ioctl *pcchunk;
Steve French9bf0c9c2013-11-16 18:05:28 -0600901 struct copychunk_ioctl_rsp *retbuf = NULL;
902 struct cifs_tcon *tcon;
903 int chunks_copied = 0;
904 bool chunk_sizes_updated = false;
Sachin Prabhu620d8742017-02-10 16:03:51 +0530905 ssize_t bytes_written, total_bytes_written = 0;
Steve French41c13582013-11-14 00:05:36 -0600906
907 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
908
909 if (pcchunk == NULL)
910 return -ENOMEM;
911
Sachin Prabhu312bbc52017-04-04 02:12:04 -0500912 cifs_dbg(FYI, "in smb2_copychunk_range - about to call request res key\n");
Steve French41c13582013-11-14 00:05:36 -0600913 /* Request a key from the server to identify the source of the copy */
914 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
915 srcfile->fid.persistent_fid,
916 srcfile->fid.volatile_fid, pcchunk);
917
918 /* Note: request_res_key sets res_key null only if rc !=0 */
919 if (rc)
Steve French9bf0c9c2013-11-16 18:05:28 -0600920 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -0600921
922 /* For now array only one chunk long, will make more flexible later */
Fabian Frederickbc09d142014-12-10 15:41:15 -0800923 pcchunk->ChunkCount = cpu_to_le32(1);
Steve French41c13582013-11-14 00:05:36 -0600924 pcchunk->Reserved = 0;
Steve French41c13582013-11-14 00:05:36 -0600925 pcchunk->Reserved2 = 0;
926
Steve French9bf0c9c2013-11-16 18:05:28 -0600927 tcon = tlink_tcon(trgtfile->tlink);
928
929 while (len > 0) {
930 pcchunk->SourceOffset = cpu_to_le64(src_off);
931 pcchunk->TargetOffset = cpu_to_le64(dest_off);
932 pcchunk->Length =
933 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
934
935 /* Request server copy to target from src identified by key */
936 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
Steve French41c13582013-11-14 00:05:36 -0600937 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +0100938 true /* is_fsctl */, (char *)pcchunk,
Steve French9bf0c9c2013-11-16 18:05:28 -0600939 sizeof(struct copychunk_ioctl), (char **)&retbuf,
940 &ret_data_len);
941 if (rc == 0) {
942 if (ret_data_len !=
943 sizeof(struct copychunk_ioctl_rsp)) {
944 cifs_dbg(VFS, "invalid cchunk response size\n");
945 rc = -EIO;
946 goto cchunk_out;
947 }
948 if (retbuf->TotalBytesWritten == 0) {
949 cifs_dbg(FYI, "no bytes copied\n");
950 rc = -EIO;
951 goto cchunk_out;
952 }
953 /*
954 * Check if server claimed to write more than we asked
955 */
956 if (le32_to_cpu(retbuf->TotalBytesWritten) >
957 le32_to_cpu(pcchunk->Length)) {
958 cifs_dbg(VFS, "invalid copy chunk response\n");
959 rc = -EIO;
960 goto cchunk_out;
961 }
962 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
963 cifs_dbg(VFS, "invalid num chunks written\n");
964 rc = -EIO;
965 goto cchunk_out;
966 }
967 chunks_copied++;
Steve French41c13582013-11-14 00:05:36 -0600968
Sachin Prabhu620d8742017-02-10 16:03:51 +0530969 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
970 src_off += bytes_written;
971 dest_off += bytes_written;
972 len -= bytes_written;
973 total_bytes_written += bytes_written;
Steve French41c13582013-11-14 00:05:36 -0600974
Sachin Prabhu620d8742017-02-10 16:03:51 +0530975 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
Steve French9bf0c9c2013-11-16 18:05:28 -0600976 le32_to_cpu(retbuf->ChunksWritten),
977 le32_to_cpu(retbuf->ChunkBytesWritten),
Sachin Prabhu620d8742017-02-10 16:03:51 +0530978 bytes_written);
Steve French9bf0c9c2013-11-16 18:05:28 -0600979 } else if (rc == -EINVAL) {
980 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
981 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -0600982
Steve French9bf0c9c2013-11-16 18:05:28 -0600983 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
984 le32_to_cpu(retbuf->ChunksWritten),
985 le32_to_cpu(retbuf->ChunkBytesWritten),
986 le32_to_cpu(retbuf->TotalBytesWritten));
987
988 /*
989 * Check if this is the first request using these sizes,
990 * (ie check if copy succeed once with original sizes
991 * and check if the server gave us different sizes after
992 * we already updated max sizes on previous request).
993 * if not then why is the server returning an error now
994 */
995 if ((chunks_copied != 0) || chunk_sizes_updated)
996 goto cchunk_out;
997
998 /* Check that server is not asking us to grow size */
999 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1000 tcon->max_bytes_chunk)
1001 tcon->max_bytes_chunk =
1002 le32_to_cpu(retbuf->ChunkBytesWritten);
1003 else
1004 goto cchunk_out; /* server gave us bogus size */
1005
1006 /* No need to change MaxChunks since already set to 1 */
1007 chunk_sizes_updated = true;
Sachin Prabhu2477bc52015-02-04 13:10:26 +00001008 } else
1009 goto cchunk_out;
Steve French9bf0c9c2013-11-16 18:05:28 -06001010 }
1011
1012cchunk_out:
Steve French41c13582013-11-14 00:05:36 -06001013 kfree(pcchunk);
Steve French24df1482016-09-29 04:20:23 -05001014 kfree(retbuf);
Sachin Prabhu620d8742017-02-10 16:03:51 +05301015 if (rc)
1016 return rc;
1017 else
1018 return total_bytes_written;
Steve French41c13582013-11-14 00:05:36 -06001019}
1020
1021static int
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001022smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1023 struct cifs_fid *fid)
1024{
1025 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1026}
1027
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001028static unsigned int
1029smb2_read_data_offset(char *buf)
1030{
1031 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1032 return rsp->DataOffset;
1033}
1034
1035static unsigned int
Long Li74dcf412017-11-22 17:38:46 -07001036smb2_read_data_length(char *buf, bool in_remaining)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001037{
1038 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
Long Li74dcf412017-11-22 17:38:46 -07001039
1040 if (in_remaining)
1041 return le32_to_cpu(rsp->DataRemaining);
1042
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001043 return le32_to_cpu(rsp->DataLength);
1044}
1045
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001046
1047static int
Steve Frenchdb8b6312014-09-22 05:13:55 -05001048smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001049 struct cifs_io_parms *parms, unsigned int *bytes_read,
1050 char **buf, int *buf_type)
1051{
Steve Frenchdb8b6312014-09-22 05:13:55 -05001052 parms->persistent_fid = pfid->persistent_fid;
1053 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001054 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1055}
1056
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001057static int
Steve Frenchdb8b6312014-09-22 05:13:55 -05001058smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001059 struct cifs_io_parms *parms, unsigned int *written,
1060 struct kvec *iov, unsigned long nr_segs)
1061{
1062
Steve Frenchdb8b6312014-09-22 05:13:55 -05001063 parms->persistent_fid = pfid->persistent_fid;
1064 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001065 return SMB2_write(xid, parms, written, iov, nr_segs);
1066}
1067
Steve Frenchd43cc792014-08-13 17:16:29 -05001068/* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1069static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1070 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1071{
1072 struct cifsInodeInfo *cifsi;
1073 int rc;
1074
1075 cifsi = CIFS_I(inode);
1076
1077 /* if file already sparse don't bother setting sparse again */
1078 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1079 return true; /* already sparse */
1080
1081 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1082 return true; /* already not sparse */
1083
1084 /*
1085 * Can't check for sparse support on share the usual way via the
1086 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1087 * since Samba server doesn't set the flag on the share, yet
1088 * supports the set sparse FSCTL and returns sparse correctly
1089 * in the file attributes. If we fail setting sparse though we
1090 * mark that server does not support sparse files for this share
1091 * to avoid repeatedly sending the unsupported fsctl to server
1092 * if the file is repeatedly extended.
1093 */
1094 if (tcon->broken_sparse_sup)
1095 return false;
1096
1097 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1098 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001099 true /* is_fctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001100 &setsparse, 1, NULL, NULL);
Steve Frenchd43cc792014-08-13 17:16:29 -05001101 if (rc) {
1102 tcon->broken_sparse_sup = true;
1103 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1104 return false;
1105 }
1106
1107 if (setsparse)
1108 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1109 else
1110 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1111
1112 return true;
1113}
1114
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001115static int
1116smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1117 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1118{
1119 __le64 eof = cpu_to_le64(size);
Steve French3d1a3742014-08-11 21:05:25 -05001120 struct inode *inode;
1121
1122 /*
1123 * If extending file more than one page make sparse. Many Linux fs
1124 * make files sparse by default when extending via ftruncate
1125 */
David Howells2b0143b2015-03-17 22:25:59 +00001126 inode = d_inode(cfile->dentry);
Steve French3d1a3742014-08-11 21:05:25 -05001127
1128 if (!set_alloc && (size > inode->i_size + 8192)) {
Steve French3d1a3742014-08-11 21:05:25 -05001129 __u8 set_sparse = 1;
Steve French3d1a3742014-08-11 21:05:25 -05001130
Steve Frenchd43cc792014-08-13 17:16:29 -05001131 /* whether set sparse succeeds or not, extend the file */
1132 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
Steve French3d1a3742014-08-11 21:05:25 -05001133 }
1134
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001135 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05001136 cfile->fid.volatile_fid, cfile->pid, &eof, false);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001137}
1138
Steve French02b16662015-06-27 21:18:36 -07001139static int
1140smb2_duplicate_extents(const unsigned int xid,
1141 struct cifsFileInfo *srcfile,
1142 struct cifsFileInfo *trgtfile, u64 src_off,
1143 u64 len, u64 dest_off)
1144{
1145 int rc;
1146 unsigned int ret_data_len;
Steve French02b16662015-06-27 21:18:36 -07001147 struct duplicate_extents_to_file dup_ext_buf;
1148 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1149
1150 /* server fileays advertise duplicate extent support with this flag */
1151 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1152 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1153 return -EOPNOTSUPP;
1154
1155 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1156 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1157 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1158 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1159 dup_ext_buf.ByteCount = cpu_to_le64(len);
1160 cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
1161 src_off, dest_off, len);
1162
1163 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1164 if (rc)
1165 goto duplicate_extents_out;
1166
1167 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1168 trgtfile->fid.volatile_fid,
1169 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001170 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001171 (char *)&dup_ext_buf,
Steve French02b16662015-06-27 21:18:36 -07001172 sizeof(struct duplicate_extents_to_file),
Steve French24df1482016-09-29 04:20:23 -05001173 NULL,
Steve French02b16662015-06-27 21:18:36 -07001174 &ret_data_len);
1175
1176 if (ret_data_len > 0)
1177 cifs_dbg(FYI, "non-zero response length in duplicate extents");
1178
1179duplicate_extents_out:
1180 return rc;
1181}
Steve French02b16662015-06-27 21:18:36 -07001182
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001183static int
Steve French64a5cfa2013-10-14 15:31:32 -05001184smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1185 struct cifsFileInfo *cfile)
1186{
1187 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1188 cfile->fid.volatile_fid);
1189}
1190
1191static int
Steve Frenchb3152e22015-06-24 03:17:02 -05001192smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1193 struct cifsFileInfo *cfile)
1194{
1195 struct fsctl_set_integrity_information_req integr_info;
Steve Frenchb3152e22015-06-24 03:17:02 -05001196 unsigned int ret_data_len;
1197
1198 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1199 integr_info.Flags = 0;
1200 integr_info.Reserved = 0;
1201
1202 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1203 cfile->fid.volatile_fid,
1204 FSCTL_SET_INTEGRITY_INFORMATION,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001205 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001206 (char *)&integr_info,
Steve Frenchb3152e22015-06-24 03:17:02 -05001207 sizeof(struct fsctl_set_integrity_information_req),
Steve French24df1482016-09-29 04:20:23 -05001208 NULL,
Steve Frenchb3152e22015-06-24 03:17:02 -05001209 &ret_data_len);
1210
1211}
1212
1213static int
Steve French834170c2016-09-30 21:14:26 -05001214smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1215 struct cifsFileInfo *cfile, void __user *ioc_buf)
1216{
1217 char *retbuf = NULL;
1218 unsigned int ret_data_len = 0;
1219 int rc;
1220 struct smb_snapshot_array snapshot_in;
1221
1222 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1223 cfile->fid.volatile_fid,
1224 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001225 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001226 NULL, 0 /* no input data */,
Steve French834170c2016-09-30 21:14:26 -05001227 (char **)&retbuf,
1228 &ret_data_len);
1229 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1230 rc, ret_data_len);
1231 if (rc)
1232 return rc;
1233
1234 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1235 /* Fixup buffer */
1236 if (copy_from_user(&snapshot_in, ioc_buf,
1237 sizeof(struct smb_snapshot_array))) {
1238 rc = -EFAULT;
1239 kfree(retbuf);
1240 return rc;
1241 }
1242 if (snapshot_in.snapshot_array_size < sizeof(struct smb_snapshot_array)) {
1243 rc = -ERANGE;
David Disseldorp0e5c7952017-05-03 17:39:09 +02001244 kfree(retbuf);
Steve French834170c2016-09-30 21:14:26 -05001245 return rc;
1246 }
1247
1248 if (ret_data_len > snapshot_in.snapshot_array_size)
1249 ret_data_len = snapshot_in.snapshot_array_size;
1250
1251 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1252 rc = -EFAULT;
1253 }
1254
1255 kfree(retbuf);
1256 return rc;
1257}
1258
1259static int
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001260smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1261 const char *path, struct cifs_sb_info *cifs_sb,
1262 struct cifs_fid *fid, __u16 search_flags,
1263 struct cifs_search_info *srch_inf)
1264{
1265 __le16 *utf16_path;
1266 int rc;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001267 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001268 struct cifs_open_parms oparms;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001269
1270 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1271 if (!utf16_path)
1272 return -ENOMEM;
1273
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001274 oparms.tcon = tcon;
1275 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1276 oparms.disposition = FILE_OPEN;
1277 oparms.create_options = 0;
1278 oparms.fid = fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001279 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001280
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001281 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001282 kfree(utf16_path);
1283 if (rc) {
Pavel Shilovskydcd878382017-06-06 16:58:58 -07001284 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001285 return rc;
1286 }
1287
1288 srch_inf->entries_in_buffer = 0;
1289 srch_inf->index_of_last_entry = 0;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001290
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001291 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1292 fid->volatile_fid, 0, srch_inf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001293 if (rc) {
Pavel Shilovskydcd878382017-06-06 16:58:58 -07001294 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001295 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001296 }
1297 return rc;
1298}
1299
1300static int
1301smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1302 struct cifs_fid *fid, __u16 search_flags,
1303 struct cifs_search_info *srch_inf)
1304{
1305 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1306 fid->volatile_fid, 0, srch_inf);
1307}
1308
1309static int
1310smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1311 struct cifs_fid *fid)
1312{
1313 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1314}
1315
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001316/*
1317* If we negotiate SMB2 protocol and get STATUS_PENDING - update
1318* the number of credits and return true. Otherwise - return false.
1319*/
1320static bool
1321smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
1322{
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001323 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001324
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001325 if (shdr->Status != STATUS_PENDING)
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001326 return false;
1327
1328 if (!length) {
1329 spin_lock(&server->req_lock);
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001330 server->credits += le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001331 spin_unlock(&server->req_lock);
1332 wake_up(&server->request_q);
1333 }
1334
1335 return true;
1336}
1337
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001338static bool
1339smb2_is_session_expired(char *buf)
1340{
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001341 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001342
Mark Symsd81243c2018-05-24 09:47:31 +01001343 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
1344 shdr->Status != STATUS_USER_SESSION_DELETED)
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001345 return false;
1346
Mark Symsd81243c2018-05-24 09:47:31 +01001347 cifs_dbg(FYI, "Session expired or deleted\n");
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001348 return true;
1349}
1350
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001351static int
1352smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
1353 struct cifsInodeInfo *cinode)
1354{
Pavel Shilovsky0822f512012-09-19 06:22:45 -07001355 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
1356 return SMB2_lease_break(0, tcon, cinode->lease_key,
1357 smb2_get_lease_state(cinode));
1358
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001359 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
1360 fid->volatile_fid,
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001361 CIFS_CACHE_READ(cinode) ? 1 : 0);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001362}
1363
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001364static int
1365smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1366 struct kstatfs *buf)
1367{
1368 int rc;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001369 __le16 srch_path = 0; /* Null - open root of share */
1370 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001371 struct cifs_open_parms oparms;
1372 struct cifs_fid fid;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001373
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001374 oparms.tcon = tcon;
1375 oparms.desired_access = FILE_READ_ATTRIBUTES;
1376 oparms.disposition = FILE_OPEN;
1377 oparms.create_options = 0;
1378 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001379 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001380
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001381 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001382 if (rc)
1383 return rc;
1384 buf->f_type = SMB2_MAGIC_NUMBER;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001385 rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1386 buf);
1387 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001388 return rc;
1389}
1390
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07001391static bool
1392smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
1393{
1394 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
1395 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
1396}
1397
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07001398static int
1399smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
1400 __u64 length, __u32 type, int lock, int unlock, bool wait)
1401{
1402 if (unlock && !lock)
1403 type = SMB2_LOCKFLAG_UNLOCK;
1404 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
1405 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
1406 current->tgid, length, offset, type, wait);
1407}
1408
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001409static void
1410smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
1411{
1412 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
1413}
1414
1415static void
1416smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
1417{
1418 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
1419}
1420
1421static void
1422smb2_new_lease_key(struct cifs_fid *fid)
1423{
Steve Frenchfa70b872016-09-22 00:39:34 -05001424 generate_random_uuid(fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001425}
1426
Aurelien Aptel9d496402017-02-13 16:16:49 +01001427static int
1428smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
1429 const char *search_name,
1430 struct dfs_info3_param **target_nodes,
1431 unsigned int *num_of_nodes,
1432 const struct nls_table *nls_codepage, int remap)
1433{
1434 int rc;
1435 __le16 *utf16_path = NULL;
1436 int utf16_path_len = 0;
1437 struct cifs_tcon *tcon;
1438 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
1439 struct get_dfs_referral_rsp *dfs_rsp = NULL;
1440 u32 dfs_req_size = 0, dfs_rsp_size = 0;
1441
1442 cifs_dbg(FYI, "smb2_get_dfs_refer path <%s>\n", search_name);
1443
1444 /*
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001445 * Try to use the IPC tcon, otherwise just use any
Aurelien Aptel9d496402017-02-13 16:16:49 +01001446 */
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001447 tcon = ses->tcon_ipc;
1448 if (tcon == NULL) {
1449 spin_lock(&cifs_tcp_ses_lock);
1450 tcon = list_first_entry_or_null(&ses->tcon_list,
1451 struct cifs_tcon,
1452 tcon_list);
1453 if (tcon)
1454 tcon->tc_count++;
1455 spin_unlock(&cifs_tcp_ses_lock);
1456 }
Aurelien Aptel9d496402017-02-13 16:16:49 +01001457
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001458 if (tcon == NULL) {
Aurelien Aptel9d496402017-02-13 16:16:49 +01001459 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
1460 ses);
1461 rc = -ENOTCONN;
1462 goto out;
1463 }
1464
1465 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
1466 &utf16_path_len,
1467 nls_codepage, remap);
1468 if (!utf16_path) {
1469 rc = -ENOMEM;
1470 goto out;
1471 }
1472
1473 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
1474 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
1475 if (!dfs_req) {
1476 rc = -ENOMEM;
1477 goto out;
1478 }
1479
1480 /* Highest DFS referral version understood */
1481 dfs_req->MaxReferralLevel = DFS_VERSION;
1482
1483 /* Path to resolve in an UTF-16 null-terminated string */
1484 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
1485
1486 do {
Aurelien Aptel9d496402017-02-13 16:16:49 +01001487 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1488 FSCTL_DFS_GET_REFERRALS,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001489 true /* is_fsctl */,
Aurelien Aptel9d496402017-02-13 16:16:49 +01001490 (char *)dfs_req, dfs_req_size,
1491 (char **)&dfs_rsp, &dfs_rsp_size);
Aurelien Aptel9d496402017-02-13 16:16:49 +01001492 } while (rc == -EAGAIN);
1493
1494 if (rc) {
Steve French2564f2f2018-03-21 23:16:36 -05001495 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
Aurelien Aptel57025912017-11-21 14:47:56 +01001496 cifs_dbg(VFS, "ioctl error in smb2_get_dfs_refer rc=%d\n", rc);
Aurelien Aptel9d496402017-02-13 16:16:49 +01001497 goto out;
1498 }
1499
1500 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
1501 num_of_nodes, target_nodes,
1502 nls_codepage, remap, search_name,
1503 true /* is_unicode */);
1504 if (rc) {
1505 cifs_dbg(VFS, "parse error in smb2_get_dfs_refer rc=%d\n", rc);
1506 goto out;
1507 }
1508
1509 out:
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001510 if (tcon && !tcon->ipc) {
1511 /* ipc tcons are not refcounted */
Aurelien Aptel9d496402017-02-13 16:16:49 +01001512 spin_lock(&cifs_tcp_ses_lock);
1513 tcon->tc_count--;
1514 spin_unlock(&cifs_tcp_ses_lock);
1515 }
1516 kfree(utf16_path);
1517 kfree(dfs_req);
1518 kfree(dfs_rsp);
1519 return rc;
1520}
Pavel Shilovsky78932422016-07-24 10:37:38 +03001521#define SMB2_SYMLINK_STRUCT_SIZE \
1522 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
1523
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001524static int
1525smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
1526 const char *full_path, char **target_path,
1527 struct cifs_sb_info *cifs_sb)
1528{
1529 int rc;
1530 __le16 *utf16_path;
1531 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1532 struct cifs_open_parms oparms;
1533 struct cifs_fid fid;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001534 struct kvec err_iov = {NULL, 0};
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001535 struct smb2_err_rsp *err_buf = NULL;
1536 int resp_buftype;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001537 struct smb2_symlink_err_rsp *symlink;
Pavel Shilovsky78932422016-07-24 10:37:38 +03001538 unsigned int sub_len;
1539 unsigned int sub_offset;
1540 unsigned int print_len;
1541 unsigned int print_offset;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001542
1543 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
1544
1545 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1546 if (!utf16_path)
1547 return -ENOMEM;
1548
1549 oparms.tcon = tcon;
1550 oparms.desired_access = FILE_READ_ATTRIBUTES;
1551 oparms.disposition = FILE_OPEN;
1552 oparms.create_options = 0;
1553 oparms.fid = &fid;
1554 oparms.reconnect = false;
1555
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001556 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_iov,
1557 &resp_buftype);
Gustavo A. R. Silva0d568cd2018-04-13 10:13:29 -05001558 if (!rc || !err_iov.iov_base) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001559 rc = -ENOENT;
1560 goto querty_exit;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001561 }
Pavel Shilovsky78932422016-07-24 10:37:38 +03001562
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10001563 err_buf = err_iov.iov_base;
Pavel Shilovsky78932422016-07-24 10:37:38 +03001564 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10001565 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001566 rc = -ENOENT;
1567 goto querty_exit;
Pavel Shilovsky78932422016-07-24 10:37:38 +03001568 }
1569
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001570 /* open must fail on symlink - reset rc */
1571 rc = 0;
1572 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
1573 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
1574 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
Pavel Shilovsky78932422016-07-24 10:37:38 +03001575 print_len = le16_to_cpu(symlink->PrintNameLength);
1576 print_offset = le16_to_cpu(symlink->PrintNameOffset);
1577
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10001578 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001579 rc = -ENOENT;
1580 goto querty_exit;
Pavel Shilovsky78932422016-07-24 10:37:38 +03001581 }
1582
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10001583 if (err_iov.iov_len <
1584 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001585 rc = -ENOENT;
1586 goto querty_exit;
Pavel Shilovsky78932422016-07-24 10:37:38 +03001587 }
1588
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001589 *target_path = cifs_strndup_from_utf16(
1590 (char *)symlink->PathBuffer + sub_offset,
1591 sub_len, true, cifs_sb->local_nls);
1592 if (!(*target_path)) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001593 rc = -ENOMEM;
1594 goto querty_exit;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001595 }
1596 convert_delimiter(*target_path, '/');
1597 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001598
1599 querty_exit:
1600 free_rsp_buf(resp_buftype, err_buf);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001601 kfree(utf16_path);
1602 return rc;
1603}
1604
Arnd Bergmann84908422017-06-27 17:06:13 +02001605#ifdef CONFIG_CIFS_ACL
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001606static struct cifs_ntsd *
1607get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
1608 const struct cifs_fid *cifsfid, u32 *pacllen)
1609{
1610 struct cifs_ntsd *pntsd = NULL;
1611 unsigned int xid;
1612 int rc = -EOPNOTSUPP;
1613 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1614
1615 if (IS_ERR(tlink))
1616 return ERR_CAST(tlink);
1617
1618 xid = get_xid();
1619 cifs_dbg(FYI, "trying to get acl\n");
1620
1621 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
1622 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
1623 free_xid(xid);
1624
1625 cifs_put_tlink(tlink);
1626
1627 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
1628 if (rc)
1629 return ERR_PTR(rc);
1630 return pntsd;
1631
1632}
1633
1634static struct cifs_ntsd *
1635get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
1636 const char *path, u32 *pacllen)
1637{
1638 struct cifs_ntsd *pntsd = NULL;
1639 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1640 unsigned int xid;
1641 int rc;
1642 struct cifs_tcon *tcon;
1643 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1644 struct cifs_fid fid;
1645 struct cifs_open_parms oparms;
1646 __le16 *utf16_path;
1647
1648 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
1649 if (IS_ERR(tlink))
1650 return ERR_CAST(tlink);
1651
1652 tcon = tlink_tcon(tlink);
1653 xid = get_xid();
1654
1655 if (backup_cred(cifs_sb))
Colin Ian King709340a2017-07-05 13:47:34 +01001656 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001657 else
1658 oparms.create_options = 0;
1659
1660 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
Steve Frenchcfe89092018-05-19 02:04:55 -05001661 if (!utf16_path) {
1662 rc = -ENOMEM;
1663 free_xid(xid);
1664 return ERR_PTR(rc);
1665 }
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001666
1667 oparms.tcon = tcon;
1668 oparms.desired_access = READ_CONTROL;
1669 oparms.disposition = FILE_OPEN;
1670 oparms.fid = &fid;
1671 oparms.reconnect = false;
1672
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001673 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001674 kfree(utf16_path);
1675 if (!rc) {
1676 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
1677 fid.volatile_fid, (void **)&pntsd, pacllen);
1678 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1679 }
1680
1681 cifs_put_tlink(tlink);
1682 free_xid(xid);
1683
1684 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
1685 if (rc)
1686 return ERR_PTR(rc);
1687 return pntsd;
1688}
1689
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05001690#ifdef CONFIG_CIFS_ACL
1691static int
1692set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
1693 struct inode *inode, const char *path, int aclflag)
1694{
1695 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1696 unsigned int xid;
1697 int rc, access_flags = 0;
1698 struct cifs_tcon *tcon;
1699 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1700 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1701 struct cifs_fid fid;
1702 struct cifs_open_parms oparms;
1703 __le16 *utf16_path;
1704
1705 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
1706 if (IS_ERR(tlink))
1707 return PTR_ERR(tlink);
1708
1709 tcon = tlink_tcon(tlink);
1710 xid = get_xid();
1711
1712 if (backup_cred(cifs_sb))
1713 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1714 else
1715 oparms.create_options = 0;
1716
1717 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
1718 access_flags = WRITE_OWNER;
1719 else
1720 access_flags = WRITE_DAC;
1721
1722 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
Steve Frenchcfe89092018-05-19 02:04:55 -05001723 if (!utf16_path) {
1724 rc = -ENOMEM;
1725 free_xid(xid);
1726 return rc;
1727 }
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05001728
1729 oparms.tcon = tcon;
1730 oparms.desired_access = access_flags;
1731 oparms.disposition = FILE_OPEN;
1732 oparms.path = path;
1733 oparms.fid = &fid;
1734 oparms.reconnect = false;
1735
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001736 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05001737 kfree(utf16_path);
1738 if (!rc) {
1739 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
1740 fid.volatile_fid, pnntsd, acllen, aclflag);
1741 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1742 }
1743
1744 cifs_put_tlink(tlink);
1745 free_xid(xid);
1746 return rc;
1747}
1748#endif /* CIFS_ACL */
1749
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001750/* Retrieve an ACL from the server */
1751static struct cifs_ntsd *
1752get_smb2_acl(struct cifs_sb_info *cifs_sb,
1753 struct inode *inode, const char *path,
1754 u32 *pacllen)
1755{
1756 struct cifs_ntsd *pntsd = NULL;
1757 struct cifsFileInfo *open_file = NULL;
1758
1759 if (inode)
1760 open_file = find_readable_file(CIFS_I(inode), true);
1761 if (!open_file)
1762 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
1763
1764 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
1765 cifsFileInfo_put(open_file);
1766 return pntsd;
1767}
Arnd Bergmann84908422017-06-27 17:06:13 +02001768#endif
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001769
Steve French30175622014-08-17 18:16:40 -05001770static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
1771 loff_t offset, loff_t len, bool keep_size)
1772{
1773 struct inode *inode;
1774 struct cifsInodeInfo *cifsi;
1775 struct cifsFileInfo *cfile = file->private_data;
1776 struct file_zero_data_information fsctl_buf;
1777 long rc;
1778 unsigned int xid;
1779
1780 xid = get_xid();
1781
David Howells2b0143b2015-03-17 22:25:59 +00001782 inode = d_inode(cfile->dentry);
Steve French30175622014-08-17 18:16:40 -05001783 cifsi = CIFS_I(inode);
1784
1785 /* if file not oplocked can't be sure whether asking to extend size */
1786 if (!CIFS_CACHE_READ(cifsi))
Steve Frenchcfe89092018-05-19 02:04:55 -05001787 if (keep_size == false) {
1788 rc = -EOPNOTSUPP;
1789 free_xid(xid);
1790 return rc;
1791 }
Steve French30175622014-08-17 18:16:40 -05001792
Steve French2bb93d22014-08-20 18:56:29 -05001793 /*
Steve French30175622014-08-17 18:16:40 -05001794 * Must check if file sparse since fallocate -z (zero range) assumes
1795 * non-sparse allocation
1796 */
Steve Frenchcfe89092018-05-19 02:04:55 -05001797 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
1798 rc = -EOPNOTSUPP;
1799 free_xid(xid);
1800 return rc;
1801 }
Steve French30175622014-08-17 18:16:40 -05001802
1803 /*
1804 * need to make sure we are not asked to extend the file since the SMB3
1805 * fsctl does not change the file size. In the future we could change
1806 * this to zero the first part of the range then set the file size
1807 * which for a non sparse file would zero the newly extended range
1808 */
1809 if (keep_size == false)
Steve Frenchcfe89092018-05-19 02:04:55 -05001810 if (i_size_read(inode) < offset + len) {
1811 rc = -EOPNOTSUPP;
1812 free_xid(xid);
1813 return rc;
1814 }
Steve French30175622014-08-17 18:16:40 -05001815
1816 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1817
1818 fsctl_buf.FileOffset = cpu_to_le64(offset);
1819 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1820
1821 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1822 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001823 true /* is_fctl */, (char *)&fsctl_buf,
Steve French30175622014-08-17 18:16:40 -05001824 sizeof(struct file_zero_data_information), NULL, NULL);
1825 free_xid(xid);
1826 return rc;
1827}
1828
Steve French31742c52014-08-17 08:38:47 -05001829static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
1830 loff_t offset, loff_t len)
1831{
1832 struct inode *inode;
1833 struct cifsInodeInfo *cifsi;
1834 struct cifsFileInfo *cfile = file->private_data;
1835 struct file_zero_data_information fsctl_buf;
1836 long rc;
1837 unsigned int xid;
1838 __u8 set_sparse = 1;
1839
1840 xid = get_xid();
1841
David Howells2b0143b2015-03-17 22:25:59 +00001842 inode = d_inode(cfile->dentry);
Steve French31742c52014-08-17 08:38:47 -05001843 cifsi = CIFS_I(inode);
1844
1845 /* Need to make file sparse, if not already, before freeing range. */
1846 /* Consider adding equivalent for compressed since it could also work */
Steve Frenchcfe89092018-05-19 02:04:55 -05001847 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
1848 rc = -EOPNOTSUPP;
1849 free_xid(xid);
1850 return rc;
1851 }
Steve French31742c52014-08-17 08:38:47 -05001852
1853 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1854
1855 fsctl_buf.FileOffset = cpu_to_le64(offset);
1856 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1857
1858 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1859 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001860 true /* is_fctl */, (char *)&fsctl_buf,
Steve French31742c52014-08-17 08:38:47 -05001861 sizeof(struct file_zero_data_information), NULL, NULL);
1862 free_xid(xid);
1863 return rc;
1864}
1865
Steve French9ccf3212014-10-18 17:01:15 -05001866static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
1867 loff_t off, loff_t len, bool keep_size)
1868{
1869 struct inode *inode;
1870 struct cifsInodeInfo *cifsi;
1871 struct cifsFileInfo *cfile = file->private_data;
1872 long rc = -EOPNOTSUPP;
1873 unsigned int xid;
1874
1875 xid = get_xid();
1876
David Howells2b0143b2015-03-17 22:25:59 +00001877 inode = d_inode(cfile->dentry);
Steve French9ccf3212014-10-18 17:01:15 -05001878 cifsi = CIFS_I(inode);
1879
1880 /* if file not oplocked can't be sure whether asking to extend size */
1881 if (!CIFS_CACHE_READ(cifsi))
Steve Frenchcfe89092018-05-19 02:04:55 -05001882 if (keep_size == false) {
1883 free_xid(xid);
1884 return rc;
1885 }
Steve French9ccf3212014-10-18 17:01:15 -05001886
1887 /*
1888 * Files are non-sparse by default so falloc may be a no-op
1889 * Must check if file sparse. If not sparse, and not extending
1890 * then no need to do anything since file already allocated
1891 */
1892 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
1893 if (keep_size == true)
Steve Frenchcfe89092018-05-19 02:04:55 -05001894 rc = 0;
Steve French9ccf3212014-10-18 17:01:15 -05001895 /* check if extending file */
1896 else if (i_size_read(inode) >= off + len)
1897 /* not extending file and already not sparse */
Steve Frenchcfe89092018-05-19 02:04:55 -05001898 rc = 0;
Steve French9ccf3212014-10-18 17:01:15 -05001899 /* BB: in future add else clause to extend file */
1900 else
Steve Frenchcfe89092018-05-19 02:04:55 -05001901 rc = -EOPNOTSUPP;
1902 free_xid(xid);
1903 return rc;
Steve French9ccf3212014-10-18 17:01:15 -05001904 }
1905
1906 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
1907 /*
1908 * Check if falloc starts within first few pages of file
1909 * and ends within a few pages of the end of file to
1910 * ensure that most of file is being forced to be
1911 * fallocated now. If so then setting whole file sparse
1912 * ie potentially making a few extra pages at the beginning
1913 * or end of the file non-sparse via set_sparse is harmless.
1914 */
Steve Frenchcfe89092018-05-19 02:04:55 -05001915 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
1916 rc = -EOPNOTSUPP;
1917 free_xid(xid);
1918 return rc;
1919 }
Steve French9ccf3212014-10-18 17:01:15 -05001920
1921 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
1922 }
1923 /* BB: else ... in future add code to extend file and set sparse */
1924
1925
1926 free_xid(xid);
1927 return rc;
1928}
1929
1930
Steve French31742c52014-08-17 08:38:47 -05001931static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
1932 loff_t off, loff_t len)
1933{
1934 /* KEEP_SIZE already checked for by do_fallocate */
1935 if (mode & FALLOC_FL_PUNCH_HOLE)
1936 return smb3_punch_hole(file, tcon, off, len);
Steve French30175622014-08-17 18:16:40 -05001937 else if (mode & FALLOC_FL_ZERO_RANGE) {
1938 if (mode & FALLOC_FL_KEEP_SIZE)
1939 return smb3_zero_range(file, tcon, off, len, true);
1940 return smb3_zero_range(file, tcon, off, len, false);
Steve French9ccf3212014-10-18 17:01:15 -05001941 } else if (mode == FALLOC_FL_KEEP_SIZE)
1942 return smb3_simple_falloc(file, tcon, off, len, true);
1943 else if (mode == 0)
1944 return smb3_simple_falloc(file, tcon, off, len, false);
Steve French31742c52014-08-17 08:38:47 -05001945
1946 return -EOPNOTSUPP;
1947}
1948
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001949static void
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00001950smb2_downgrade_oplock(struct TCP_Server_Info *server,
1951 struct cifsInodeInfo *cinode, bool set_level2)
1952{
1953 if (set_level2)
1954 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
1955 0, NULL);
1956 else
1957 server->ops->set_oplock_level(cinode, 0, 0, NULL);
1958}
1959
1960static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001961smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1962 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001963{
1964 oplock &= 0xFF;
1965 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1966 return;
1967 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001968 cinode->oplock = CIFS_CACHE_RHW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001969 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
1970 &cinode->vfs_inode);
1971 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001972 cinode->oplock = CIFS_CACHE_RW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001973 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
1974 &cinode->vfs_inode);
1975 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
1976 cinode->oplock = CIFS_CACHE_READ_FLG;
1977 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
1978 &cinode->vfs_inode);
1979 } else
1980 cinode->oplock = 0;
1981}
1982
1983static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001984smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1985 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001986{
1987 char message[5] = {0};
1988
1989 oplock &= 0xFF;
1990 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1991 return;
1992
1993 cinode->oplock = 0;
1994 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
1995 cinode->oplock |= CIFS_CACHE_READ_FLG;
1996 strcat(message, "R");
1997 }
1998 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
1999 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
2000 strcat(message, "H");
2001 }
2002 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
2003 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
2004 strcat(message, "W");
2005 }
2006 if (!cinode->oplock)
2007 strcat(message, "None");
2008 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
2009 &cinode->vfs_inode);
2010}
2011
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002012static void
2013smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2014 unsigned int epoch, bool *purge_cache)
2015{
2016 unsigned int old_oplock = cinode->oplock;
2017
2018 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
2019
2020 if (purge_cache) {
2021 *purge_cache = false;
2022 if (old_oplock == CIFS_CACHE_READ_FLG) {
2023 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
2024 (epoch - cinode->epoch > 0))
2025 *purge_cache = true;
2026 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2027 (epoch - cinode->epoch > 1))
2028 *purge_cache = true;
2029 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2030 (epoch - cinode->epoch > 1))
2031 *purge_cache = true;
2032 else if (cinode->oplock == 0 &&
2033 (epoch - cinode->epoch > 0))
2034 *purge_cache = true;
2035 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
2036 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2037 (epoch - cinode->epoch > 0))
2038 *purge_cache = true;
2039 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2040 (epoch - cinode->epoch > 1))
2041 *purge_cache = true;
2042 }
2043 cinode->epoch = epoch;
2044 }
2045}
2046
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002047static bool
2048smb2_is_read_op(__u32 oplock)
2049{
2050 return oplock == SMB2_OPLOCK_LEVEL_II;
2051}
2052
2053static bool
2054smb21_is_read_op(__u32 oplock)
2055{
2056 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
2057 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
2058}
2059
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002060static __le32
2061map_oplock_to_lease(u8 oplock)
2062{
2063 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
2064 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
2065 else if (oplock == SMB2_OPLOCK_LEVEL_II)
2066 return SMB2_LEASE_READ_CACHING;
2067 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
2068 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
2069 SMB2_LEASE_WRITE_CACHING;
2070 return 0;
2071}
2072
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002073static char *
2074smb2_create_lease_buf(u8 *lease_key, u8 oplock)
2075{
2076 struct create_lease *buf;
2077
2078 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
2079 if (!buf)
2080 return NULL;
2081
2082 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
2083 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002084 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002085
2086 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2087 (struct create_lease, lcontext));
2088 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
2089 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2090 (struct create_lease, Name));
2091 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07002092 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002093 buf->Name[0] = 'R';
2094 buf->Name[1] = 'q';
2095 buf->Name[2] = 'L';
2096 buf->Name[3] = 's';
2097 return (char *)buf;
2098}
2099
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002100static char *
2101smb3_create_lease_buf(u8 *lease_key, u8 oplock)
2102{
2103 struct create_lease_v2 *buf;
2104
2105 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
2106 if (!buf)
2107 return NULL;
2108
2109 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
2110 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
2111 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2112
2113 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2114 (struct create_lease_v2, lcontext));
2115 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
2116 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2117 (struct create_lease_v2, Name));
2118 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07002119 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002120 buf->Name[0] = 'R';
2121 buf->Name[1] = 'q';
2122 buf->Name[2] = 'L';
2123 buf->Name[3] = 's';
2124 return (char *)buf;
2125}
2126
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002127static __u8
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06002128smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002129{
2130 struct create_lease *lc = (struct create_lease *)buf;
2131
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002132 *epoch = 0; /* not used */
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002133 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2134 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2135 return le32_to_cpu(lc->lcontext.LeaseState);
2136}
2137
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002138static __u8
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06002139smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002140{
2141 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
2142
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002143 *epoch = le16_to_cpu(lc->lcontext.Epoch);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002144 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2145 return SMB2_OPLOCK_LEVEL_NOCHANGE;
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06002146 if (lease_key)
2147 memcpy(lease_key, &lc->lcontext.LeaseKeyLow,
2148 SMB2_LEASE_KEY_SIZE);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002149 return le32_to_cpu(lc->lcontext.LeaseState);
2150}
2151
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002152static unsigned int
2153smb2_wp_retry_size(struct inode *inode)
2154{
2155 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
2156 SMB2_MAX_BUFFER_SIZE);
2157}
2158
Pavel Shilovsky52755802014-08-18 20:49:57 +04002159static bool
2160smb2_dir_needs_close(struct cifsFileInfo *cfile)
2161{
2162 return !cfile->invalidHandle;
2163}
2164
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002165static void
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002166fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
2167 struct smb_rqst *old_rq)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002168{
2169 struct smb2_sync_hdr *shdr =
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002170 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002171
2172 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
2173 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
2174 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
2175 tr_hdr->Flags = cpu_to_le16(0x01);
2176 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2177 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002178}
2179
Ronnie Sahlberg262916b2018-02-20 12:45:21 +11002180/* We can not use the normal sg_set_buf() as we will sometimes pass a
2181 * stack object as buf.
2182 */
2183static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
2184 unsigned int buflen)
2185{
2186 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
2187}
2188
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002189/* Assumes:
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002190 * rqst->rq_iov[0] is tranform header
2191 * rqst->rq_iov[1+] data to be encrypted/decrypted
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002192 */
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002193static struct scatterlist *
2194init_sg(struct smb_rqst *rqst, u8 *sign)
2195{
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002196 unsigned int sg_len = rqst->rq_nvec + rqst->rq_npages + 1;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002197 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002198 struct scatterlist *sg;
2199 unsigned int i;
2200 unsigned int j;
2201
2202 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
2203 if (!sg)
2204 return NULL;
2205
2206 sg_init_table(sg, sg_len);
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002207 smb2_sg_set_buf(&sg[0], rqst->rq_iov[0].iov_base + 20, assoc_data_len);
2208 for (i = 1; i < rqst->rq_nvec; i++)
2209 smb2_sg_set_buf(&sg[i], rqst->rq_iov[i].iov_base,
2210 rqst->rq_iov[i].iov_len);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002211 for (j = 0; i < sg_len - 1; i++, j++) {
Steve Frenchd5f07fb2018-06-05 17:46:24 -05002212 unsigned int len, offset;
2213
2214 rqst_page_get_length(rqst, j, &len, &offset);
2215 sg_set_page(&sg[i], rqst->rq_pages[j], len, offset);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002216 }
Ronnie Sahlberg262916b2018-02-20 12:45:21 +11002217 smb2_sg_set_buf(&sg[sg_len - 1], sign, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002218 return sg;
2219}
2220
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002221static int
2222smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
2223{
2224 struct cifs_ses *ses;
2225 u8 *ses_enc_key;
2226
2227 spin_lock(&cifs_tcp_ses_lock);
2228 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2229 if (ses->Suid != ses_id)
2230 continue;
2231 ses_enc_key = enc ? ses->smb3encryptionkey :
2232 ses->smb3decryptionkey;
2233 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
2234 spin_unlock(&cifs_tcp_ses_lock);
2235 return 0;
2236 }
2237 spin_unlock(&cifs_tcp_ses_lock);
2238
2239 return 1;
2240}
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002241/*
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002242 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
2243 * iov[0] - transform header (associate data),
2244 * iov[1-N] - SMB2 header and pages - data to encrypt.
2245 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002246 * untouched.
2247 */
2248static int
2249crypt_message(struct TCP_Server_Info *server, struct smb_rqst *rqst, int enc)
2250{
2251 struct smb2_transform_hdr *tr_hdr =
Aurelien Aptel83210ba2018-06-06 16:42:51 +02002252 (struct smb2_transform_hdr *)rqst->rq_iov[1].iov_base;
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002253 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002254 int rc = 0;
2255 struct scatterlist *sg;
2256 u8 sign[SMB2_SIGNATURE_SIZE] = {};
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002257 u8 key[SMB3_SIGN_KEY_SIZE];
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002258 struct aead_request *req;
2259 char *iv;
2260 unsigned int iv_len;
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01002261 DECLARE_CRYPTO_WAIT(wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002262 struct crypto_aead *tfm;
2263 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2264
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002265 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
2266 if (rc) {
2267 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
2268 enc ? "en" : "de");
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002269 return 0;
2270 }
2271
2272 rc = smb3_crypto_aead_allocate(server);
2273 if (rc) {
2274 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
2275 return rc;
2276 }
2277
2278 tfm = enc ? server->secmech.ccmaesencrypt :
2279 server->secmech.ccmaesdecrypt;
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002280 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002281 if (rc) {
2282 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
2283 return rc;
2284 }
2285
2286 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
2287 if (rc) {
2288 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
2289 return rc;
2290 }
2291
2292 req = aead_request_alloc(tfm, GFP_KERNEL);
2293 if (!req) {
2294 cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
2295 return -ENOMEM;
2296 }
2297
2298 if (!enc) {
2299 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
2300 crypt_len += SMB2_SIGNATURE_SIZE;
2301 }
2302
2303 sg = init_sg(rqst, sign);
2304 if (!sg) {
Christophe Jaillet517a6e42017-06-11 09:12:47 +02002305 cifs_dbg(VFS, "%s: Failed to init sg", __func__);
2306 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002307 goto free_req;
2308 }
2309
2310 iv_len = crypto_aead_ivsize(tfm);
2311 iv = kzalloc(iv_len, GFP_KERNEL);
2312 if (!iv) {
2313 cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
Christophe Jaillet517a6e42017-06-11 09:12:47 +02002314 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002315 goto free_sg;
2316 }
2317 iv[0] = 3;
2318 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2319
2320 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
2321 aead_request_set_ad(req, assoc_data_len);
2322
2323 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01002324 crypto_req_done, &wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002325
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01002326 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
2327 : crypto_aead_decrypt(req), &wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002328
2329 if (!rc && enc)
2330 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
2331
2332 kfree(iv);
2333free_sg:
2334 kfree(sg);
2335free_req:
2336 kfree(req);
2337 return rc;
2338}
2339
2340static int
2341smb3_init_transform_rq(struct TCP_Server_Info *server, struct smb_rqst *new_rq,
2342 struct smb_rqst *old_rq)
2343{
2344 struct kvec *iov;
2345 struct page **pages;
2346 struct smb2_transform_hdr *tr_hdr;
2347 unsigned int npages = old_rq->rq_npages;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002348 unsigned int orig_len = 0;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002349 int i;
2350 int rc = -ENOMEM;
2351
2352 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2353 if (!pages)
2354 return rc;
2355
2356 new_rq->rq_pages = pages;
Steve Frenchd5f07fb2018-06-05 17:46:24 -05002357 new_rq->rq_offset = old_rq->rq_offset;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002358 new_rq->rq_npages = old_rq->rq_npages;
2359 new_rq->rq_pagesz = old_rq->rq_pagesz;
2360 new_rq->rq_tailsz = old_rq->rq_tailsz;
2361
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002362 for (i = 0; i < old_rq->rq_nvec; i++)
2363 orig_len += old_rq->rq_iov[i].iov_len;
2364
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002365 for (i = 0; i < npages; i++) {
2366 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2367 if (!pages[i])
2368 goto err_free_pages;
2369 }
2370
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002371 iov = kmalloc_array(old_rq->rq_nvec + 1, sizeof(struct kvec),
2372 GFP_KERNEL);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002373 if (!iov)
2374 goto err_free_pages;
2375
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002376 /* copy all iovs from the old */
2377 memcpy(&iov[1], &old_rq->rq_iov[0],
2378 sizeof(struct kvec) * old_rq->rq_nvec);
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002379
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002380 new_rq->rq_iov = iov;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002381 new_rq->rq_nvec = old_rq->rq_nvec + 1;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002382
2383 tr_hdr = kmalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL);
2384 if (!tr_hdr)
2385 goto err_free_iov;
2386
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002387 /* fill the 2nd iov with a transform header */
2388 fill_transform_hdr(tr_hdr, orig_len, old_rq);
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002389 new_rq->rq_iov[0].iov_base = tr_hdr;
2390 new_rq->rq_iov[0].iov_len = sizeof(struct smb2_transform_hdr);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002391
2392 /* copy pages form the old */
2393 for (i = 0; i < npages; i++) {
Steve Frenchd5f07fb2018-06-05 17:46:24 -05002394 char *dst, *src;
2395 unsigned int offset, len;
2396
2397 rqst_page_get_length(new_rq, i, &len, &offset);
2398
2399 dst = (char *) kmap(new_rq->rq_pages[i]) + offset;
2400 src = (char *) kmap(old_rq->rq_pages[i]) + offset;
2401
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002402 memcpy(dst, src, len);
2403 kunmap(new_rq->rq_pages[i]);
2404 kunmap(old_rq->rq_pages[i]);
2405 }
2406
2407 rc = crypt_message(server, new_rq, 1);
2408 cifs_dbg(FYI, "encrypt message returned %d", rc);
2409 if (rc)
2410 goto err_free_tr_hdr;
2411
2412 return rc;
2413
2414err_free_tr_hdr:
2415 kfree(tr_hdr);
2416err_free_iov:
2417 kfree(iov);
2418err_free_pages:
2419 for (i = i - 1; i >= 0; i--)
2420 put_page(pages[i]);
2421 kfree(pages);
2422 return rc;
2423}
2424
2425static void
2426smb3_free_transform_rq(struct smb_rqst *rqst)
2427{
2428 int i = rqst->rq_npages - 1;
2429
2430 for (; i >= 0; i--)
2431 put_page(rqst->rq_pages[i]);
2432 kfree(rqst->rq_pages);
2433 /* free transform header */
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002434 kfree(rqst->rq_iov[0].iov_base);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002435 kfree(rqst->rq_iov);
2436}
2437
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002438static int
2439smb3_is_transform_hdr(void *buf)
2440{
2441 struct smb2_transform_hdr *trhdr = buf;
2442
2443 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
2444}
2445
2446static int
2447decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
2448 unsigned int buf_data_size, struct page **pages,
2449 unsigned int npages, unsigned int page_data_size)
2450{
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002451 struct kvec iov[2];
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002452 struct smb_rqst rqst = {NULL};
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002453 int rc;
2454
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002455 iov[0].iov_base = buf;
2456 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
2457 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
2458 iov[1].iov_len = buf_data_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002459
2460 rqst.rq_iov = iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002461 rqst.rq_nvec = 2;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002462 rqst.rq_pages = pages;
2463 rqst.rq_npages = npages;
2464 rqst.rq_pagesz = PAGE_SIZE;
2465 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
2466
2467 rc = crypt_message(server, &rqst, 0);
2468 cifs_dbg(FYI, "decrypt message returned %d\n", rc);
2469
2470 if (rc)
2471 return rc;
2472
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002473 memmove(buf, iov[1].iov_base, buf_data_size);
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002474
2475 server->total_read = buf_data_size + page_data_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002476
2477 return rc;
2478}
2479
2480static int
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002481read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
2482 unsigned int npages, unsigned int len)
2483{
2484 int i;
2485 int length;
2486
2487 for (i = 0; i < npages; i++) {
2488 struct page *page = pages[i];
2489 size_t n;
2490
2491 n = len;
2492 if (len >= PAGE_SIZE) {
2493 /* enough data to fill the page */
2494 n = PAGE_SIZE;
2495 len -= n;
2496 } else {
2497 zero_user(page, len, PAGE_SIZE - len);
2498 len = 0;
2499 }
Long Li1dbe3462018-05-30 12:47:55 -07002500 length = cifs_read_page_from_socket(server, page, 0, n);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002501 if (length < 0)
2502 return length;
2503 server->total_read += length;
2504 }
2505
2506 return 0;
2507}
2508
2509static int
2510init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
2511 unsigned int cur_off, struct bio_vec **page_vec)
2512{
2513 struct bio_vec *bvec;
2514 int i;
2515
2516 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
2517 if (!bvec)
2518 return -ENOMEM;
2519
2520 for (i = 0; i < npages; i++) {
2521 bvec[i].bv_page = pages[i];
2522 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
2523 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
2524 data_size -= bvec[i].bv_len;
2525 }
2526
2527 if (data_size != 0) {
2528 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
2529 kfree(bvec);
2530 return -EIO;
2531 }
2532
2533 *page_vec = bvec;
2534 return 0;
2535}
2536
2537static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002538handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
2539 char *buf, unsigned int buf_len, struct page **pages,
2540 unsigned int npages, unsigned int page_data_size)
2541{
2542 unsigned int data_offset;
2543 unsigned int data_len;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002544 unsigned int cur_off;
2545 unsigned int cur_page_idx;
2546 unsigned int pad_len;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002547 struct cifs_readdata *rdata = mid->callback_data;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10002548 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002549 struct bio_vec *bvec = NULL;
2550 struct iov_iter iter;
2551 struct kvec iov;
2552 int length;
Long Li74dcf412017-11-22 17:38:46 -07002553 bool use_rdma_mr = false;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002554
2555 if (shdr->Command != SMB2_READ) {
2556 cifs_dbg(VFS, "only big read responses are supported\n");
2557 return -ENOTSUPP;
2558 }
2559
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002560 if (server->ops->is_session_expired &&
2561 server->ops->is_session_expired(buf)) {
2562 cifs_reconnect(server);
2563 wake_up(&server->response_q);
2564 return -1;
2565 }
2566
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002567 if (server->ops->is_status_pending &&
2568 server->ops->is_status_pending(buf, server, 0))
2569 return -1;
2570
2571 rdata->result = server->ops->map_error(buf, false);
2572 if (rdata->result != 0) {
2573 cifs_dbg(FYI, "%s: server returned error %d\n",
2574 __func__, rdata->result);
2575 dequeue_mid(mid, rdata->result);
2576 return 0;
2577 }
2578
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002579 data_offset = server->ops->read_data_offset(buf);
Long Li74dcf412017-11-22 17:38:46 -07002580#ifdef CONFIG_CIFS_SMB_DIRECT
2581 use_rdma_mr = rdata->mr;
2582#endif
2583 data_len = server->ops->read_data_length(buf, use_rdma_mr);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002584
2585 if (data_offset < server->vals->read_rsp_size) {
2586 /*
2587 * win2k8 sometimes sends an offset of 0 when the read
2588 * is beyond the EOF. Treat it as if the data starts just after
2589 * the header.
2590 */
2591 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
2592 __func__, data_offset);
2593 data_offset = server->vals->read_rsp_size;
2594 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
2595 /* data_offset is beyond the end of smallbuf */
2596 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
2597 __func__, data_offset);
2598 rdata->result = -EIO;
2599 dequeue_mid(mid, rdata->result);
2600 return 0;
2601 }
2602
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002603 pad_len = data_offset - server->vals->read_rsp_size;
2604
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002605 if (buf_len <= data_offset) {
2606 /* read response payload is in pages */
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002607 cur_page_idx = pad_len / PAGE_SIZE;
2608 cur_off = pad_len % PAGE_SIZE;
2609
2610 if (cur_page_idx != 0) {
2611 /* data offset is beyond the 1st page of response */
2612 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
2613 __func__, data_offset);
2614 rdata->result = -EIO;
2615 dequeue_mid(mid, rdata->result);
2616 return 0;
2617 }
2618
2619 if (data_len > page_data_size - pad_len) {
2620 /* data_len is corrupt -- discard frame */
2621 rdata->result = -EIO;
2622 dequeue_mid(mid, rdata->result);
2623 return 0;
2624 }
2625
2626 rdata->result = init_read_bvec(pages, npages, page_data_size,
2627 cur_off, &bvec);
2628 if (rdata->result != 0) {
2629 dequeue_mid(mid, rdata->result);
2630 return 0;
2631 }
2632
2633 iov_iter_bvec(&iter, WRITE | ITER_BVEC, bvec, npages, data_len);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002634 } else if (buf_len >= data_offset + data_len) {
2635 /* read response payload is in buf */
2636 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
2637 iov.iov_base = buf + data_offset;
2638 iov.iov_len = data_len;
2639 iov_iter_kvec(&iter, WRITE | ITER_KVEC, &iov, 1, data_len);
2640 } else {
2641 /* read response payload cannot be in both buf and pages */
2642 WARN_ONCE(1, "buf can not contain only a part of read data");
2643 rdata->result = -EIO;
2644 dequeue_mid(mid, rdata->result);
2645 return 0;
2646 }
2647
2648 /* set up first iov for signature check */
2649 rdata->iov[0].iov_base = buf;
2650 rdata->iov[0].iov_len = 4;
2651 rdata->iov[1].iov_base = buf + 4;
2652 rdata->iov[1].iov_len = server->vals->read_rsp_size - 4;
2653 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
2654 rdata->iov[0].iov_base, server->vals->read_rsp_size);
2655
2656 length = rdata->copy_into_pages(server, rdata, &iter);
2657
2658 kfree(bvec);
2659
2660 if (length < 0)
2661 return length;
2662
2663 dequeue_mid(mid, false);
2664 return length;
2665}
2666
2667static int
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002668receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2669{
2670 char *buf = server->smallbuf;
2671 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2672 unsigned int npages;
2673 struct page **pages;
2674 unsigned int len;
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002675 unsigned int buflen = server->pdu_size;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002676 int rc;
2677 int i = 0;
2678
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002679 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002680 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
2681
2682 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
2683 if (rc < 0)
2684 return rc;
2685 server->total_read += rc;
2686
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002687 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11002688 server->vals->read_rsp_size;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002689 npages = DIV_ROUND_UP(len, PAGE_SIZE);
2690
2691 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2692 if (!pages) {
2693 rc = -ENOMEM;
2694 goto discard_data;
2695 }
2696
2697 for (; i < npages; i++) {
2698 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2699 if (!pages[i]) {
2700 rc = -ENOMEM;
2701 goto discard_data;
2702 }
2703 }
2704
2705 /* read read data into pages */
2706 rc = read_data_into_pages(server, pages, npages, len);
2707 if (rc)
2708 goto free_pages;
2709
Pavel Shilovsky350be252017-04-10 10:31:33 -07002710 rc = cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002711 if (rc)
2712 goto free_pages;
2713
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002714 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002715 pages, npages, len);
2716 if (rc)
2717 goto free_pages;
2718
2719 *mid = smb2_find_mid(server, buf);
2720 if (*mid == NULL)
2721 cifs_dbg(FYI, "mid not found\n");
2722 else {
2723 cifs_dbg(FYI, "mid found\n");
2724 (*mid)->decrypted = true;
2725 rc = handle_read_data(server, *mid, buf,
2726 server->vals->read_rsp_size,
2727 pages, npages, len);
2728 }
2729
2730free_pages:
2731 for (i = i - 1; i >= 0; i--)
2732 put_page(pages[i]);
2733 kfree(pages);
2734 return rc;
2735discard_data:
Pavel Shilovsky350be252017-04-10 10:31:33 -07002736 cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002737 goto free_pages;
2738}
2739
2740static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002741receive_encrypted_standard(struct TCP_Server_Info *server,
2742 struct mid_q_entry **mid)
2743{
2744 int length;
2745 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10002746 unsigned int pdu_length = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002747 unsigned int buf_size;
2748 struct mid_q_entry *mid_entry;
2749
2750 /* switch to large buffer if too big for a small one */
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002751 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002752 server->large_buf = true;
2753 memcpy(server->bigbuf, buf, server->total_read);
2754 buf = server->bigbuf;
2755 }
2756
2757 /* now read the rest */
2758 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002759 pdu_length - HEADER_SIZE(server) + 1);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002760 if (length < 0)
2761 return length;
2762 server->total_read += length;
2763
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002764 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002765 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
2766 if (length)
2767 return length;
2768
2769 mid_entry = smb2_find_mid(server, buf);
2770 if (mid_entry == NULL)
2771 cifs_dbg(FYI, "mid not found\n");
2772 else {
2773 cifs_dbg(FYI, "mid found\n");
2774 mid_entry->decrypted = true;
2775 }
2776
2777 *mid = mid_entry;
2778
2779 if (mid_entry && mid_entry->handle)
2780 return mid_entry->handle(server, mid_entry);
2781
2782 return cifs_handle_standard(server, mid_entry);
2783}
2784
2785static int
2786smb3_receive_transform(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2787{
2788 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10002789 unsigned int pdu_length = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002790 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2791 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2792
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002793 if (pdu_length < sizeof(struct smb2_transform_hdr) +
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002794 sizeof(struct smb2_sync_hdr)) {
2795 cifs_dbg(VFS, "Transform message is too small (%u)\n",
2796 pdu_length);
2797 cifs_reconnect(server);
2798 wake_up(&server->response_q);
2799 return -ECONNABORTED;
2800 }
2801
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002802 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002803 cifs_dbg(VFS, "Transform message is broken\n");
2804 cifs_reconnect(server);
2805 wake_up(&server->response_q);
2806 return -ECONNABORTED;
2807 }
2808
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002809 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server))
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002810 return receive_encrypted_read(server, mid);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002811
2812 return receive_encrypted_standard(server, mid);
2813}
2814
2815int
2816smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
2817{
2818 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
2819
Ronnie Sahlberg1fc6ad2f2018-06-01 10:53:07 +10002820 return handle_read_data(server, mid, buf, server->pdu_size,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002821 NULL, 0, 0);
2822}
2823
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10002824static int
2825smb2_next_header(char *buf)
2826{
2827 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
2828 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
2829
2830 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
2831 return sizeof(struct smb2_transform_hdr) +
2832 le32_to_cpu(t_hdr->OriginalMessageSize);
2833
2834 return le32_to_cpu(hdr->NextCommand);
2835}
2836
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002837struct smb_version_operations smb20_operations = {
2838 .compare_fids = smb2_compare_fids,
2839 .setup_request = smb2_setup_request,
2840 .setup_async_request = smb2_setup_async_request,
2841 .check_receive = smb2_check_receive,
2842 .add_credits = smb2_add_credits,
2843 .set_credits = smb2_set_credits,
2844 .get_credits_field = smb2_get_credits_field,
2845 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002846 .wait_mtu_credits = cifs_wait_mtu_credits,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002847 .get_next_mid = smb2_get_next_mid,
2848 .read_data_offset = smb2_read_data_offset,
2849 .read_data_length = smb2_read_data_length,
2850 .map_error = map_smb2_to_linux_error,
2851 .find_mid = smb2_find_mid,
2852 .check_message = smb2_check_message,
2853 .dump_detail = smb2_dump_detail,
2854 .clear_stats = smb2_clear_stats,
2855 .print_stats = smb2_print_stats,
2856 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08002857 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002858 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002859 .need_neg = smb2_need_neg,
2860 .negotiate = smb2_negotiate,
2861 .negotiate_wsize = smb2_negotiate_wsize,
2862 .negotiate_rsize = smb2_negotiate_rsize,
2863 .sess_setup = SMB2_sess_setup,
2864 .logoff = SMB2_logoff,
2865 .tree_connect = SMB2_tcon,
2866 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05002867 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002868 .is_path_accessible = smb2_is_path_accessible,
2869 .can_echo = smb2_can_echo,
2870 .echo = SMB2_echo,
2871 .query_path_info = smb2_query_path_info,
2872 .get_srv_inum = smb2_get_srv_inum,
2873 .query_file_info = smb2_query_file_info,
2874 .set_path_size = smb2_set_path_size,
2875 .set_file_size = smb2_set_file_size,
2876 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05002877 .set_compression = smb2_set_compression,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002878 .mkdir = smb2_mkdir,
2879 .mkdir_setinfo = smb2_mkdir_setinfo,
2880 .rmdir = smb2_rmdir,
2881 .unlink = smb2_unlink,
2882 .rename = smb2_rename_path,
2883 .create_hardlink = smb2_create_hardlink,
2884 .query_symlink = smb2_query_symlink,
Sachin Prabhu5b23c972016-07-11 16:53:20 +01002885 .query_mf_symlink = smb3_query_mf_symlink,
2886 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002887 .open = smb2_open_file,
2888 .set_fid = smb2_set_fid,
2889 .close = smb2_close_file,
2890 .flush = smb2_flush_file,
2891 .async_readv = smb2_async_readv,
2892 .async_writev = smb2_async_writev,
2893 .sync_read = smb2_sync_read,
2894 .sync_write = smb2_sync_write,
2895 .query_dir_first = smb2_query_dir_first,
2896 .query_dir_next = smb2_query_dir_next,
2897 .close_dir = smb2_close_dir,
2898 .calc_smb_size = smb2_calc_size,
2899 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002900 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002901 .oplock_response = smb2_oplock_response,
2902 .queryfs = smb2_queryfs,
2903 .mand_lock = smb2_mand_lock,
2904 .mand_unlock_range = smb2_unlock_range,
2905 .push_mand_locks = smb2_push_mandatory_locks,
2906 .get_lease_key = smb2_get_lease_key,
2907 .set_lease_key = smb2_set_lease_key,
2908 .new_lease_key = smb2_new_lease_key,
2909 .calc_signature = smb2_calc_signature,
2910 .is_read_op = smb2_is_read_op,
2911 .set_oplock_level = smb2_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002912 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002913 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05002914 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002915 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04002916 .dir_needs_close = smb2_dir_needs_close,
Aurelien Aptel9d496402017-02-13 16:16:49 +01002917 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302918 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002919#ifdef CONFIG_CIFS_XATTR
2920 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10002921 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002922#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002923#ifdef CONFIG_CIFS_ACL
2924 .get_acl = get_smb2_acl,
2925 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05002926 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002927#endif /* CIFS_ACL */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10002928 .next_header = smb2_next_header,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002929};
2930
Steve French1080ef72011-02-24 18:07:19 +00002931struct smb_version_operations smb21_operations = {
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07002932 .compare_fids = smb2_compare_fids,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002933 .setup_request = smb2_setup_request,
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +04002934 .setup_async_request = smb2_setup_async_request,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002935 .check_receive = smb2_check_receive,
Pavel Shilovsky28ea5292012-05-23 16:18:00 +04002936 .add_credits = smb2_add_credits,
2937 .set_credits = smb2_set_credits,
2938 .get_credits_field = smb2_get_credits_field,
2939 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002940 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002941 .get_next_mid = smb2_get_next_mid,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002942 .read_data_offset = smb2_read_data_offset,
2943 .read_data_length = smb2_read_data_length,
2944 .map_error = map_smb2_to_linux_error,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04002945 .find_mid = smb2_find_mid,
2946 .check_message = smb2_check_message,
2947 .dump_detail = smb2_dump_detail,
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04002948 .clear_stats = smb2_clear_stats,
2949 .print_stats = smb2_print_stats,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002950 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08002951 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002952 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04002953 .need_neg = smb2_need_neg,
2954 .negotiate = smb2_negotiate,
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -07002955 .negotiate_wsize = smb2_negotiate_wsize,
2956 .negotiate_rsize = smb2_negotiate_rsize,
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04002957 .sess_setup = SMB2_sess_setup,
2958 .logoff = SMB2_logoff,
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04002959 .tree_connect = SMB2_tcon,
2960 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05002961 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002962 .is_path_accessible = smb2_is_path_accessible,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002963 .can_echo = smb2_can_echo,
2964 .echo = SMB2_echo,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002965 .query_path_info = smb2_query_path_info,
2966 .get_srv_inum = smb2_get_srv_inum,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -07002967 .query_file_info = smb2_query_file_info,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002968 .set_path_size = smb2_set_path_size,
2969 .set_file_size = smb2_set_file_size,
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07002970 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05002971 .set_compression = smb2_set_compression,
Pavel Shilovskya0e73182011-07-19 12:56:37 +04002972 .mkdir = smb2_mkdir,
2973 .mkdir_setinfo = smb2_mkdir_setinfo,
Pavel Shilovsky1a500f02012-07-10 16:14:38 +04002974 .rmdir = smb2_rmdir,
Pavel Shilovskycbe6f432012-09-18 16:20:25 -07002975 .unlink = smb2_unlink,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002976 .rename = smb2_rename_path,
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002977 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002978 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05002979 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05002980 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002981 .open = smb2_open_file,
2982 .set_fid = smb2_set_fid,
2983 .close = smb2_close_file,
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002984 .flush = smb2_flush_file,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002985 .async_readv = smb2_async_readv,
Pavel Shilovsky33319142012-09-18 16:20:29 -07002986 .async_writev = smb2_async_writev,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002987 .sync_read = smb2_sync_read,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002988 .sync_write = smb2_sync_write,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002989 .query_dir_first = smb2_query_dir_first,
2990 .query_dir_next = smb2_query_dir_next,
2991 .close_dir = smb2_close_dir,
2992 .calc_smb_size = smb2_calc_size,
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002993 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002994 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002995 .oplock_response = smb2_oplock_response,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002996 .queryfs = smb2_queryfs,
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07002997 .mand_lock = smb2_mand_lock,
2998 .mand_unlock_range = smb2_unlock_range,
Pavel Shilovskyb1407992012-09-19 06:22:44 -07002999 .push_mand_locks = smb2_push_mandatory_locks,
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07003000 .get_lease_key = smb2_get_lease_key,
3001 .set_lease_key = smb2_set_lease_key,
3002 .new_lease_key = smb2_new_lease_key,
Steve French38107d42012-12-08 22:08:06 -06003003 .calc_signature = smb2_calc_signature,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003004 .is_read_op = smb21_is_read_op,
3005 .set_oplock_level = smb21_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003006 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04003007 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05003008 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04003009 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04003010 .dir_needs_close = smb2_dir_needs_close,
Steve French834170c2016-09-30 21:14:26 -05003011 .enum_snapshots = smb3_enum_snapshots,
Aurelien Aptel9d496402017-02-13 16:16:49 +01003012 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05303013 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003014#ifdef CONFIG_CIFS_XATTR
3015 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003016 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003017#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003018#ifdef CONFIG_CIFS_ACL
3019 .get_acl = get_smb2_acl,
3020 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003021 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003022#endif /* CIFS_ACL */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10003023 .next_header = smb2_next_header,
Steve French38107d42012-12-08 22:08:06 -06003024};
3025
Steve French38107d42012-12-08 22:08:06 -06003026struct smb_version_operations smb30_operations = {
3027 .compare_fids = smb2_compare_fids,
3028 .setup_request = smb2_setup_request,
3029 .setup_async_request = smb2_setup_async_request,
3030 .check_receive = smb2_check_receive,
3031 .add_credits = smb2_add_credits,
3032 .set_credits = smb2_set_credits,
3033 .get_credits_field = smb2_get_credits_field,
3034 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003035 .wait_mtu_credits = smb2_wait_mtu_credits,
Steve French38107d42012-12-08 22:08:06 -06003036 .get_next_mid = smb2_get_next_mid,
3037 .read_data_offset = smb2_read_data_offset,
3038 .read_data_length = smb2_read_data_length,
3039 .map_error = map_smb2_to_linux_error,
3040 .find_mid = smb2_find_mid,
3041 .check_message = smb2_check_message,
3042 .dump_detail = smb2_dump_detail,
3043 .clear_stats = smb2_clear_stats,
3044 .print_stats = smb2_print_stats,
Steve French769ee6a2013-06-19 14:15:30 -05003045 .dump_share_caps = smb2_dump_share_caps,
Steve French38107d42012-12-08 22:08:06 -06003046 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08003047 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00003048 .downgrade_oplock = smb2_downgrade_oplock,
Steve French38107d42012-12-08 22:08:06 -06003049 .need_neg = smb2_need_neg,
3050 .negotiate = smb2_negotiate,
3051 .negotiate_wsize = smb2_negotiate_wsize,
3052 .negotiate_rsize = smb2_negotiate_rsize,
3053 .sess_setup = SMB2_sess_setup,
3054 .logoff = SMB2_logoff,
3055 .tree_connect = SMB2_tcon,
3056 .tree_disconnect = SMB2_tdis,
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003057 .qfs_tcon = smb3_qfs_tcon,
Steve French38107d42012-12-08 22:08:06 -06003058 .is_path_accessible = smb2_is_path_accessible,
3059 .can_echo = smb2_can_echo,
3060 .echo = SMB2_echo,
3061 .query_path_info = smb2_query_path_info,
3062 .get_srv_inum = smb2_get_srv_inum,
3063 .query_file_info = smb2_query_file_info,
3064 .set_path_size = smb2_set_path_size,
3065 .set_file_size = smb2_set_file_size,
3066 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05003067 .set_compression = smb2_set_compression,
Steve French38107d42012-12-08 22:08:06 -06003068 .mkdir = smb2_mkdir,
3069 .mkdir_setinfo = smb2_mkdir_setinfo,
3070 .rmdir = smb2_rmdir,
3071 .unlink = smb2_unlink,
3072 .rename = smb2_rename_path,
3073 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003074 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05003075 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05003076 .create_mf_symlink = smb3_create_mf_symlink,
Steve French38107d42012-12-08 22:08:06 -06003077 .open = smb2_open_file,
3078 .set_fid = smb2_set_fid,
3079 .close = smb2_close_file,
3080 .flush = smb2_flush_file,
3081 .async_readv = smb2_async_readv,
3082 .async_writev = smb2_async_writev,
3083 .sync_read = smb2_sync_read,
3084 .sync_write = smb2_sync_write,
3085 .query_dir_first = smb2_query_dir_first,
3086 .query_dir_next = smb2_query_dir_next,
3087 .close_dir = smb2_close_dir,
3088 .calc_smb_size = smb2_calc_size,
3089 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07003090 .is_session_expired = smb2_is_session_expired,
Steve French38107d42012-12-08 22:08:06 -06003091 .oplock_response = smb2_oplock_response,
3092 .queryfs = smb2_queryfs,
3093 .mand_lock = smb2_mand_lock,
3094 .mand_unlock_range = smb2_unlock_range,
3095 .push_mand_locks = smb2_push_mandatory_locks,
3096 .get_lease_key = smb2_get_lease_key,
3097 .set_lease_key = smb2_set_lease_key,
3098 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06003099 .generate_signingkey = generate_smb30signingkey,
Steve French38107d42012-12-08 22:08:06 -06003100 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05003101 .set_integrity = smb3_set_integrity,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003102 .is_read_op = smb21_is_read_op,
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003103 .set_oplock_level = smb3_set_oplock_level,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003104 .create_lease_buf = smb3_create_lease_buf,
3105 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05003106 .copychunk_range = smb2_copychunk_range,
Steve Frenchca9e7a12015-10-01 21:40:10 -05003107 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchff1c0382013-11-19 23:44:46 -06003108 .validate_negotiate = smb3_validate_negotiate,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04003109 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04003110 .dir_needs_close = smb2_dir_needs_close,
Steve French31742c52014-08-17 08:38:47 -05003111 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05003112 .enum_snapshots = smb3_enum_snapshots,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003113 .init_transform_rq = smb3_init_transform_rq,
3114 .free_transform_rq = smb3_free_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003115 .is_transform_hdr = smb3_is_transform_hdr,
3116 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01003117 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05303118 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003119#ifdef CONFIG_CIFS_XATTR
3120 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003121 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003122#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003123#ifdef CONFIG_CIFS_ACL
3124 .get_acl = get_smb2_acl,
3125 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003126 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003127#endif /* CIFS_ACL */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10003128 .next_header = smb2_next_header,
Steve French1080ef72011-02-24 18:07:19 +00003129};
3130
Steve Frenchaab18932015-06-23 23:37:11 -05003131#ifdef CONFIG_CIFS_SMB311
3132struct smb_version_operations smb311_operations = {
3133 .compare_fids = smb2_compare_fids,
3134 .setup_request = smb2_setup_request,
3135 .setup_async_request = smb2_setup_async_request,
3136 .check_receive = smb2_check_receive,
3137 .add_credits = smb2_add_credits,
3138 .set_credits = smb2_set_credits,
3139 .get_credits_field = smb2_get_credits_field,
3140 .get_credits = smb2_get_credits,
3141 .wait_mtu_credits = smb2_wait_mtu_credits,
3142 .get_next_mid = smb2_get_next_mid,
3143 .read_data_offset = smb2_read_data_offset,
3144 .read_data_length = smb2_read_data_length,
3145 .map_error = map_smb2_to_linux_error,
3146 .find_mid = smb2_find_mid,
3147 .check_message = smb2_check_message,
3148 .dump_detail = smb2_dump_detail,
3149 .clear_stats = smb2_clear_stats,
3150 .print_stats = smb2_print_stats,
3151 .dump_share_caps = smb2_dump_share_caps,
3152 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08003153 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Steve Frenchaab18932015-06-23 23:37:11 -05003154 .downgrade_oplock = smb2_downgrade_oplock,
3155 .need_neg = smb2_need_neg,
3156 .negotiate = smb2_negotiate,
3157 .negotiate_wsize = smb2_negotiate_wsize,
3158 .negotiate_rsize = smb2_negotiate_rsize,
3159 .sess_setup = SMB2_sess_setup,
3160 .logoff = SMB2_logoff,
3161 .tree_connect = SMB2_tcon,
3162 .tree_disconnect = SMB2_tdis,
3163 .qfs_tcon = smb3_qfs_tcon,
3164 .is_path_accessible = smb2_is_path_accessible,
3165 .can_echo = smb2_can_echo,
3166 .echo = SMB2_echo,
3167 .query_path_info = smb2_query_path_info,
3168 .get_srv_inum = smb2_get_srv_inum,
3169 .query_file_info = smb2_query_file_info,
3170 .set_path_size = smb2_set_path_size,
3171 .set_file_size = smb2_set_file_size,
3172 .set_file_info = smb2_set_file_info,
3173 .set_compression = smb2_set_compression,
3174 .mkdir = smb2_mkdir,
3175 .mkdir_setinfo = smb2_mkdir_setinfo,
3176 .rmdir = smb2_rmdir,
3177 .unlink = smb2_unlink,
3178 .rename = smb2_rename_path,
3179 .create_hardlink = smb2_create_hardlink,
3180 .query_symlink = smb2_query_symlink,
3181 .query_mf_symlink = smb3_query_mf_symlink,
3182 .create_mf_symlink = smb3_create_mf_symlink,
3183 .open = smb2_open_file,
3184 .set_fid = smb2_set_fid,
3185 .close = smb2_close_file,
3186 .flush = smb2_flush_file,
3187 .async_readv = smb2_async_readv,
3188 .async_writev = smb2_async_writev,
3189 .sync_read = smb2_sync_read,
3190 .sync_write = smb2_sync_write,
3191 .query_dir_first = smb2_query_dir_first,
3192 .query_dir_next = smb2_query_dir_next,
3193 .close_dir = smb2_close_dir,
3194 .calc_smb_size = smb2_calc_size,
3195 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07003196 .is_session_expired = smb2_is_session_expired,
Steve Frenchaab18932015-06-23 23:37:11 -05003197 .oplock_response = smb2_oplock_response,
3198 .queryfs = smb2_queryfs,
3199 .mand_lock = smb2_mand_lock,
3200 .mand_unlock_range = smb2_unlock_range,
3201 .push_mand_locks = smb2_push_mandatory_locks,
3202 .get_lease_key = smb2_get_lease_key,
3203 .set_lease_key = smb2_set_lease_key,
3204 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06003205 .generate_signingkey = generate_smb311signingkey,
Steve Frenchaab18932015-06-23 23:37:11 -05003206 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05003207 .set_integrity = smb3_set_integrity,
Steve Frenchaab18932015-06-23 23:37:11 -05003208 .is_read_op = smb21_is_read_op,
3209 .set_oplock_level = smb3_set_oplock_level,
3210 .create_lease_buf = smb3_create_lease_buf,
3211 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05003212 .copychunk_range = smb2_copychunk_range,
Steve French02b16662015-06-27 21:18:36 -07003213 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchaab18932015-06-23 23:37:11 -05003214/* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
3215 .wp_retry_size = smb2_wp_retry_size,
3216 .dir_needs_close = smb2_dir_needs_close,
3217 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05003218 .enum_snapshots = smb3_enum_snapshots,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003219 .init_transform_rq = smb3_init_transform_rq,
3220 .free_transform_rq = smb3_free_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003221 .is_transform_hdr = smb3_is_transform_hdr,
3222 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01003223 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05303224 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003225#ifdef CONFIG_CIFS_XATTR
3226 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003227 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003228#endif /* CIFS_XATTR */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10003229 .next_header = smb2_next_header,
Steve Frenchaab18932015-06-23 23:37:11 -05003230};
3231#endif /* CIFS_SMB311 */
3232
Steve Frenchdd446b12012-11-28 23:21:06 -06003233struct smb_version_values smb20_values = {
3234 .version_string = SMB20_VERSION_STRING,
3235 .protocol_id = SMB20_PROT_ID,
3236 .req_capabilities = 0, /* MBZ */
3237 .large_lock_type = 0,
3238 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3239 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3240 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003241 .header_size = sizeof(struct smb2_sync_hdr),
3242 .header_preamble_size = 0,
Steve Frenchdd446b12012-11-28 23:21:06 -06003243 .max_header_size = MAX_SMB2_HDR_SIZE,
3244 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3245 .lock_cmd = SMB2_LOCK,
3246 .cap_unix = 0,
3247 .cap_nt_find = SMB2_NT_FIND,
3248 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton50285882013-06-27 12:45:00 -04003249 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3250 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003251 .create_lease_size = sizeof(struct create_lease),
Steve Frenchdd446b12012-11-28 23:21:06 -06003252};
3253
Steve French1080ef72011-02-24 18:07:19 +00003254struct smb_version_values smb21_values = {
3255 .version_string = SMB21_VERSION_STRING,
Steve Frenche4aa25e2012-10-01 12:26:22 -05003256 .protocol_id = SMB21_PROT_ID,
3257 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
3258 .large_lock_type = 0,
3259 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3260 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3261 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003262 .header_size = sizeof(struct smb2_sync_hdr),
3263 .header_preamble_size = 0,
Steve Frenche4aa25e2012-10-01 12:26:22 -05003264 .max_header_size = MAX_SMB2_HDR_SIZE,
3265 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3266 .lock_cmd = SMB2_LOCK,
3267 .cap_unix = 0,
3268 .cap_nt_find = SMB2_NT_FIND,
3269 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton50285882013-06-27 12:45:00 -04003270 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3271 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003272 .create_lease_size = sizeof(struct create_lease),
Steve Frenche4aa25e2012-10-01 12:26:22 -05003273};
3274
Steve French9764c022017-09-17 10:41:35 -05003275struct smb_version_values smb3any_values = {
3276 .version_string = SMB3ANY_VERSION_STRING,
3277 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3278 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
3279 .large_lock_type = 0,
3280 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3281 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3282 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003283 .header_size = sizeof(struct smb2_sync_hdr),
3284 .header_preamble_size = 0,
Steve French9764c022017-09-17 10:41:35 -05003285 .max_header_size = MAX_SMB2_HDR_SIZE,
3286 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3287 .lock_cmd = SMB2_LOCK,
3288 .cap_unix = 0,
3289 .cap_nt_find = SMB2_NT_FIND,
3290 .cap_large_files = SMB2_LARGE_FILES,
3291 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3292 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3293 .create_lease_size = sizeof(struct create_lease_v2),
3294};
3295
3296struct smb_version_values smbdefault_values = {
3297 .version_string = SMBDEFAULT_VERSION_STRING,
3298 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3299 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
3300 .large_lock_type = 0,
3301 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3302 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3303 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003304 .header_size = sizeof(struct smb2_sync_hdr),
3305 .header_preamble_size = 0,
Steve French9764c022017-09-17 10:41:35 -05003306 .max_header_size = MAX_SMB2_HDR_SIZE,
3307 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3308 .lock_cmd = SMB2_LOCK,
3309 .cap_unix = 0,
3310 .cap_nt_find = SMB2_NT_FIND,
3311 .cap_large_files = SMB2_LARGE_FILES,
3312 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3313 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3314 .create_lease_size = sizeof(struct create_lease_v2),
3315};
3316
Steve Frenche4aa25e2012-10-01 12:26:22 -05003317struct smb_version_values smb30_values = {
3318 .version_string = SMB30_VERSION_STRING,
3319 .protocol_id = SMB30_PROT_ID,
Steve French373512e2015-12-18 13:05:30 -06003320 .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 -07003321 .large_lock_type = 0,
3322 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3323 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3324 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003325 .header_size = sizeof(struct smb2_sync_hdr),
3326 .header_preamble_size = 0,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04003327 .max_header_size = MAX_SMB2_HDR_SIZE,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003328 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04003329 .lock_cmd = SMB2_LOCK,
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04003330 .cap_unix = 0,
3331 .cap_nt_find = SMB2_NT_FIND,
3332 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton50285882013-06-27 12:45:00 -04003333 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3334 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003335 .create_lease_size = sizeof(struct create_lease_v2),
Steve French1080ef72011-02-24 18:07:19 +00003336};
Steve French20b6d8b2013-06-12 22:48:41 -05003337
3338struct smb_version_values smb302_values = {
3339 .version_string = SMB302_VERSION_STRING,
3340 .protocol_id = SMB302_PROT_ID,
Steve French373512e2015-12-18 13:05:30 -06003341 .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 -05003342 .large_lock_type = 0,
3343 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3344 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3345 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003346 .header_size = sizeof(struct smb2_sync_hdr),
3347 .header_preamble_size = 0,
Steve French20b6d8b2013-06-12 22:48:41 -05003348 .max_header_size = MAX_SMB2_HDR_SIZE,
3349 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3350 .lock_cmd = SMB2_LOCK,
3351 .cap_unix = 0,
3352 .cap_nt_find = SMB2_NT_FIND,
3353 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton50285882013-06-27 12:45:00 -04003354 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3355 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003356 .create_lease_size = sizeof(struct create_lease_v2),
Steve French20b6d8b2013-06-12 22:48:41 -05003357};
Steve French5f7fbf72014-12-17 22:52:58 -06003358
3359#ifdef CONFIG_CIFS_SMB311
3360struct smb_version_values smb311_values = {
3361 .version_string = SMB311_VERSION_STRING,
3362 .protocol_id = SMB311_PROT_ID,
Steve French19558802017-06-21 16:50:20 -05003363 .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 -06003364 .large_lock_type = 0,
3365 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3366 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3367 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003368 .header_size = sizeof(struct smb2_sync_hdr),
3369 .header_preamble_size = 0,
Steve French5f7fbf72014-12-17 22:52:58 -06003370 .max_header_size = MAX_SMB2_HDR_SIZE,
3371 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3372 .lock_cmd = SMB2_LOCK,
3373 .cap_unix = 0,
3374 .cap_nt_find = SMB2_NT_FIND,
3375 .cap_large_files = SMB2_LARGE_FILES,
3376 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3377 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3378 .create_lease_size = sizeof(struct create_lease_v2),
3379};
3380#endif /* SMB311 */