blob: fb2934b9b97cf56f16359702a7bcb81cd6624252 [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"
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040035
36static int
37change_conf(struct TCP_Server_Info *server)
38{
39 server->credits += server->echo_credits + server->oplock_credits;
40 server->oplock_credits = server->echo_credits = 0;
41 switch (server->credits) {
42 case 0:
43 return -1;
44 case 1:
45 server->echoes = false;
46 server->oplocks = false;
Joe Perchesf96637b2013-05-04 22:12:25 -050047 cifs_dbg(VFS, "disabling echoes and oplocks\n");
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040048 break;
49 case 2:
50 server->echoes = true;
51 server->oplocks = false;
52 server->echo_credits = 1;
Joe Perchesf96637b2013-05-04 22:12:25 -050053 cifs_dbg(FYI, "disabling oplocks\n");
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040054 break;
55 default:
56 server->echoes = true;
Steve Frenche0ddde92015-09-22 09:29:38 -050057 if (enable_oplocks) {
58 server->oplocks = true;
59 server->oplock_credits = 1;
60 } else
61 server->oplocks = false;
62
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040063 server->echo_credits = 1;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040064 }
65 server->credits -= server->echo_credits + server->oplock_credits;
66 return 0;
67}
68
69static void
70smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
71 const int optype)
72{
73 int *val, rc = 0;
74 spin_lock(&server->req_lock);
75 val = server->ops->get_credits_field(server, optype);
76 *val += add;
Steve French141891f2016-09-23 00:44:16 -050077 if (*val > 65000) {
78 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
79 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
80 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040081 server->in_flight--;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040082 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040083 rc = change_conf(server);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -070084 /*
85 * Sometimes server returns 0 credits on oplock break ack - we need to
86 * rebalance credits in this case.
87 */
88 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
89 server->oplocks) {
90 if (server->credits > 1) {
91 server->credits--;
92 server->oplock_credits++;
93 }
94 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040095 spin_unlock(&server->req_lock);
96 wake_up(&server->request_q);
97 if (rc)
98 cifs_reconnect(server);
99}
100
101static void
102smb2_set_credits(struct TCP_Server_Info *server, const int val)
103{
104 spin_lock(&server->req_lock);
105 server->credits = val;
106 spin_unlock(&server->req_lock);
107}
108
109static int *
110smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
111{
112 switch (optype) {
113 case CIFS_ECHO_OP:
114 return &server->echo_credits;
115 case CIFS_OBREAK_OP:
116 return &server->oplock_credits;
117 default:
118 return &server->credits;
119 }
120}
121
122static unsigned int
123smb2_get_credits(struct mid_q_entry *mid)
124{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700125 struct smb2_sync_hdr *shdr = get_sync_hdr(mid->resp_buf);
126
127 return le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400128}
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400129
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400130static int
131smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
132 unsigned int *num, unsigned int *credits)
133{
134 int rc = 0;
135 unsigned int scredits;
136
137 spin_lock(&server->req_lock);
138 while (1) {
139 if (server->credits <= 0) {
140 spin_unlock(&server->req_lock);
141 cifs_num_waiters_inc(server);
142 rc = wait_event_killable(server->request_q,
143 has_credits(server, &server->credits));
144 cifs_num_waiters_dec(server);
145 if (rc)
146 return rc;
147 spin_lock(&server->req_lock);
148 } else {
149 if (server->tcpStatus == CifsExiting) {
150 spin_unlock(&server->req_lock);
151 return -ENOENT;
152 }
153
154 scredits = server->credits;
155 /* can deadlock with reopen */
156 if (scredits == 1) {
157 *num = SMB2_MAX_BUFFER_SIZE;
158 *credits = 0;
159 break;
160 }
161
162 /* leave one credit for a possible reopen */
163 scredits--;
164 *num = min_t(unsigned int, size,
165 scredits * SMB2_MAX_BUFFER_SIZE);
166
167 *credits = DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
168 server->credits -= *credits;
169 server->in_flight++;
170 break;
171 }
172 }
173 spin_unlock(&server->req_lock);
174 return rc;
175}
176
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400177static __u64
178smb2_get_next_mid(struct TCP_Server_Info *server)
179{
180 __u64 mid;
181 /* for SMB2 we need the current value */
182 spin_lock(&GlobalMid_Lock);
183 mid = server->CurrentMid++;
184 spin_unlock(&GlobalMid_Lock);
185 return mid;
186}
Steve French1080ef72011-02-24 18:07:19 +0000187
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400188static struct mid_q_entry *
189smb2_find_mid(struct TCP_Server_Info *server, char *buf)
190{
191 struct mid_q_entry *mid;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700192 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
193 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400194
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700195 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
Steve French373512e2015-12-18 13:05:30 -0600196 cifs_dbg(VFS, "encrypted frame parsing not supported yet");
197 return NULL;
198 }
199
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400200 spin_lock(&GlobalMid_Lock);
201 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
Sachin Prabhu9235d092014-12-09 17:37:00 +0000202 if ((mid->mid == wire_mid) &&
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400203 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700204 (mid->command == shdr->Command)) {
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400205 spin_unlock(&GlobalMid_Lock);
206 return mid;
207 }
208 }
209 spin_unlock(&GlobalMid_Lock);
210 return NULL;
211}
212
213static void
214smb2_dump_detail(void *buf)
215{
216#ifdef CONFIG_CIFS_DEBUG2
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700217 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400218
Joe Perchesf96637b2013-05-04 22:12:25 -0500219 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700220 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
221 shdr->ProcessId);
222 cifs_dbg(VFS, "smb buf %p len %u\n", buf, smb2_calc_size(buf));
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400223#endif
224}
225
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400226static bool
227smb2_need_neg(struct TCP_Server_Info *server)
228{
229 return server->max_read == 0;
230}
231
232static int
233smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
234{
235 int rc;
236 ses->server->CurrentMid = 0;
237 rc = SMB2_negotiate(xid, ses);
238 /* BB we probably don't need to retry with modern servers */
239 if (rc == -EAGAIN)
240 rc = -EHOSTDOWN;
241 return rc;
242}
243
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700244static unsigned int
245smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
246{
247 struct TCP_Server_Info *server = tcon->ses->server;
248 unsigned int wsize;
249
250 /* start with specified wsize, or default */
251 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
252 wsize = min_t(unsigned int, wsize, server->max_write);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400253
254 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
255 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700256
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700257 return wsize;
258}
259
260static unsigned int
261smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
262{
263 struct TCP_Server_Info *server = tcon->ses->server;
264 unsigned int rsize;
265
266 /* start with specified rsize, or default */
267 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
268 rsize = min_t(unsigned int, rsize, server->max_read);
Pavel Shilovskybed9da02014-06-25 11:28:57 +0400269
270 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
271 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700272
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700273 return rsize;
274}
275
Steve Frenchc481e9f2013-10-14 01:21:53 -0500276#ifdef CONFIG_CIFS_STATS2
277static int
278SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
279{
280 int rc;
281 unsigned int ret_data_len = 0;
282 struct network_interface_info_ioctl_rsp *out_buf;
283
284 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
285 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +0100286 false /* use_ipc */,
Steve Frenchc481e9f2013-10-14 01:21:53 -0500287 NULL /* no data input */, 0 /* no data input */,
288 (char **)&out_buf, &ret_data_len);
Steve French9ffc5412014-10-16 15:13:14 -0500289 if (rc != 0)
290 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
291 else if (ret_data_len < sizeof(struct network_interface_info_ioctl_rsp)) {
292 cifs_dbg(VFS, "server returned bad net interface info buf\n");
293 rc = -EINVAL;
294 } else {
Steve Frenchc481e9f2013-10-14 01:21:53 -0500295 /* Dump info on first interface */
296 cifs_dbg(FYI, "Adapter Capability 0x%x\t",
297 le32_to_cpu(out_buf->Capability));
298 cifs_dbg(FYI, "Link Speed %lld\n",
299 le64_to_cpu(out_buf->LinkSpeed));
Steve French9ffc5412014-10-16 15:13:14 -0500300 }
Steve French24df1482016-09-29 04:20:23 -0500301 kfree(out_buf);
Steve Frenchc481e9f2013-10-14 01:21:53 -0500302 return rc;
303}
304#endif /* STATS2 */
305
Steve French34f62642013-10-09 02:07:00 -0500306static void
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500307smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
308{
309 int rc;
310 __le16 srch_path = 0; /* Null - open root of share */
311 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
312 struct cifs_open_parms oparms;
313 struct cifs_fid fid;
314
315 oparms.tcon = tcon;
316 oparms.desired_access = FILE_READ_ATTRIBUTES;
317 oparms.disposition = FILE_OPEN;
318 oparms.create_options = 0;
319 oparms.fid = &fid;
320 oparms.reconnect = false;
321
322 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
323 if (rc)
324 return;
325
Steve Frenchc481e9f2013-10-14 01:21:53 -0500326#ifdef CONFIG_CIFS_STATS2
327 SMB3_request_interfaces(xid, tcon);
328#endif /* STATS2 */
329
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500330 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
331 FS_ATTRIBUTE_INFORMATION);
332 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
333 FS_DEVICE_INFORMATION);
334 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
335 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
336 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
337 return;
338}
339
340static void
Steve French34f62642013-10-09 02:07:00 -0500341smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
342{
343 int rc;
344 __le16 srch_path = 0; /* Null - open root of share */
345 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
346 struct cifs_open_parms oparms;
347 struct cifs_fid fid;
348
349 oparms.tcon = tcon;
350 oparms.desired_access = FILE_READ_ATTRIBUTES;
351 oparms.disposition = FILE_OPEN;
352 oparms.create_options = 0;
353 oparms.fid = &fid;
354 oparms.reconnect = false;
355
356 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
357 if (rc)
358 return;
359
Steven French21671142013-10-09 13:36:35 -0500360 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
361 FS_ATTRIBUTE_INFORMATION);
362 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
363 FS_DEVICE_INFORMATION);
Steve French34f62642013-10-09 02:07:00 -0500364 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
365 return;
366}
367
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400368static int
369smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
370 struct cifs_sb_info *cifs_sb, const char *full_path)
371{
372 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400373 __le16 *utf16_path;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700374 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400375 struct cifs_open_parms oparms;
376 struct cifs_fid fid;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400377
378 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
379 if (!utf16_path)
380 return -ENOMEM;
381
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400382 oparms.tcon = tcon;
383 oparms.desired_access = FILE_READ_ATTRIBUTES;
384 oparms.disposition = FILE_OPEN;
385 oparms.create_options = 0;
386 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +0400387 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400388
Pavel Shilovskyb42bf882013-08-14 19:25:21 +0400389 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400390 if (rc) {
391 kfree(utf16_path);
392 return rc;
393 }
394
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400395 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400396 kfree(utf16_path);
397 return rc;
398}
399
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400400static int
401smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
402 struct cifs_sb_info *cifs_sb, const char *full_path,
403 u64 *uniqueid, FILE_ALL_INFO *data)
404{
405 *uniqueid = le64_to_cpu(data->IndexNumber);
406 return 0;
407}
408
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700409static int
410smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
411 struct cifs_fid *fid, FILE_ALL_INFO *data)
412{
413 int rc;
414 struct smb2_file_all_info *smb2_data;
415
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +0400416 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700417 GFP_KERNEL);
418 if (smb2_data == NULL)
419 return -ENOMEM;
420
421 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
422 smb2_data);
423 if (!rc)
424 move_smb2_info_to_cifs(data, smb2_data);
425 kfree(smb2_data);
426 return rc;
427}
428
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000429static ssize_t
430move_smb2_ea_to_cifs(char *dst, size_t dst_size,
431 struct smb2_file_full_ea_info *src, size_t src_size,
432 const unsigned char *ea_name)
433{
434 int rc = 0;
435 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
436 char *name, *value;
437 size_t name_len, value_len, user_name_len;
438
439 while (src_size > 0) {
440 name = &src->ea_data[0];
441 name_len = (size_t)src->ea_name_length;
442 value = &src->ea_data[src->ea_name_length + 1];
443 value_len = (size_t)le16_to_cpu(src->ea_value_length);
444
445 if (name_len == 0) {
446 break;
447 }
448
449 if (src_size < 8 + name_len + 1 + value_len) {
450 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
451 rc = -EIO;
452 goto out;
453 }
454
455 if (ea_name) {
456 if (ea_name_len == name_len &&
457 memcmp(ea_name, name, name_len) == 0) {
458 rc = value_len;
459 if (dst_size == 0)
460 goto out;
461 if (dst_size < value_len) {
462 rc = -ERANGE;
463 goto out;
464 }
465 memcpy(dst, value, value_len);
466 goto out;
467 }
468 } else {
469 /* 'user.' plus a terminating null */
470 user_name_len = 5 + 1 + name_len;
471
472 rc += user_name_len;
473
474 if (dst_size >= user_name_len) {
475 dst_size -= user_name_len;
476 memcpy(dst, "user.", 5);
477 dst += 5;
478 memcpy(dst, src->ea_data, name_len);
479 dst += name_len;
480 *dst = 0;
481 ++dst;
482 } else if (dst_size == 0) {
483 /* skip copy - calc size only */
484 } else {
485 /* stop before overrun buffer */
486 rc = -ERANGE;
487 break;
488 }
489 }
490
491 if (!src->next_entry_offset)
492 break;
493
494 if (src_size < le32_to_cpu(src->next_entry_offset)) {
495 /* stop before overrun buffer */
496 rc = -ERANGE;
497 break;
498 }
499 src_size -= le32_to_cpu(src->next_entry_offset);
500 src = (void *)((char *)src +
501 le32_to_cpu(src->next_entry_offset));
502 }
503
504 /* didn't find the named attribute */
505 if (ea_name)
506 rc = -ENODATA;
507
508out:
509 return (ssize_t)rc;
510}
511
512static ssize_t
513smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
514 const unsigned char *path, const unsigned char *ea_name,
515 char *ea_data, size_t buf_size,
516 struct cifs_sb_info *cifs_sb)
517{
518 int rc;
519 __le16 *utf16_path;
520 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
521 struct cifs_open_parms oparms;
522 struct cifs_fid fid;
523 struct smb2_file_full_ea_info *smb2_data;
524
525 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
526 if (!utf16_path)
527 return -ENOMEM;
528
529 oparms.tcon = tcon;
530 oparms.desired_access = FILE_READ_EA;
531 oparms.disposition = FILE_OPEN;
532 oparms.create_options = 0;
533 oparms.fid = &fid;
534 oparms.reconnect = false;
535
536 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
537 kfree(utf16_path);
538 if (rc) {
539 cifs_dbg(FYI, "open failed rc=%d\n", rc);
540 return rc;
541 }
542
543 smb2_data = kzalloc(SMB2_MAX_EA_BUF, GFP_KERNEL);
544 if (smb2_data == NULL) {
545 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
546 return -ENOMEM;
547 }
548
549 rc = SMB2_query_eas(xid, tcon, fid.persistent_fid, fid.volatile_fid,
550 smb2_data);
551 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
552
553 if (!rc)
554 rc = move_smb2_ea_to_cifs(ea_data, buf_size, smb2_data,
555 SMB2_MAX_EA_BUF, ea_name);
556
557 kfree(smb2_data);
558 return rc;
559}
560
Ronnie Sahlberg55175542017-08-24 11:24:56 +1000561
562static int
563smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
564 const char *path, const char *ea_name, const void *ea_value,
565 const __u16 ea_value_len, const struct nls_table *nls_codepage,
566 struct cifs_sb_info *cifs_sb)
567{
568 int rc;
569 __le16 *utf16_path;
570 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
571 struct cifs_open_parms oparms;
572 struct cifs_fid fid;
573 struct smb2_file_full_ea_info *ea;
574 int ea_name_len = strlen(ea_name);
575 int len;
576
577 if (ea_name_len > 255)
578 return -EINVAL;
579
580 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
581 if (!utf16_path)
582 return -ENOMEM;
583
584 oparms.tcon = tcon;
585 oparms.desired_access = FILE_WRITE_EA;
586 oparms.disposition = FILE_OPEN;
587 oparms.create_options = 0;
588 oparms.fid = &fid;
589 oparms.reconnect = false;
590
591 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
592 kfree(utf16_path);
593 if (rc) {
594 cifs_dbg(FYI, "open failed rc=%d\n", rc);
595 return rc;
596 }
597
598 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
599 ea = kzalloc(len, GFP_KERNEL);
600 if (ea == NULL) {
601 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
602 return -ENOMEM;
603 }
604
605 ea->ea_name_length = ea_name_len;
606 ea->ea_value_length = cpu_to_le16(ea_value_len);
607 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
608 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
609
610 rc = SMB2_set_ea(xid, tcon, fid.persistent_fid, fid.volatile_fid, ea,
611 len);
612 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
613
614 return rc;
615}
616
Pavel Shilovsky9094fad2012-07-12 18:30:44 +0400617static bool
618smb2_can_echo(struct TCP_Server_Info *server)
619{
620 return server->echoes;
621}
622
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400623static void
624smb2_clear_stats(struct cifs_tcon *tcon)
625{
626#ifdef CONFIG_CIFS_STATS
627 int i;
628 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
629 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
630 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
631 }
632#endif
633}
634
635static void
Steve French769ee6a2013-06-19 14:15:30 -0500636smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
637{
638 seq_puts(m, "\n\tShare Capabilities:");
639 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
640 seq_puts(m, " DFS,");
641 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
642 seq_puts(m, " CONTINUOUS AVAILABILITY,");
643 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
644 seq_puts(m, " SCALEOUT,");
645 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
646 seq_puts(m, " CLUSTER,");
647 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
648 seq_puts(m, " ASYMMETRIC,");
649 if (tcon->capabilities == 0)
650 seq_puts(m, " None");
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500651 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
652 seq_puts(m, " Aligned,");
653 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
654 seq_puts(m, " Partition Aligned,");
655 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
656 seq_puts(m, " SSD,");
657 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
658 seq_puts(m, " TRIM-support,");
659
Steve French769ee6a2013-06-19 14:15:30 -0500660 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500661 if (tcon->perf_sector_size)
662 seq_printf(m, "\tOptimal sector size: 0x%x",
663 tcon->perf_sector_size);
Steve French769ee6a2013-06-19 14:15:30 -0500664}
665
666static void
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400667smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
668{
669#ifdef CONFIG_CIFS_STATS
670 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
671 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
672 seq_printf(m, "\nNegotiates: %d sent %d failed",
673 atomic_read(&sent[SMB2_NEGOTIATE_HE]),
674 atomic_read(&failed[SMB2_NEGOTIATE_HE]));
675 seq_printf(m, "\nSessionSetups: %d sent %d failed",
676 atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
677 atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400678 seq_printf(m, "\nLogoffs: %d sent %d failed",
679 atomic_read(&sent[SMB2_LOGOFF_HE]),
680 atomic_read(&failed[SMB2_LOGOFF_HE]));
681 seq_printf(m, "\nTreeConnects: %d sent %d failed",
682 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
683 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
684 seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
685 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
686 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
687 seq_printf(m, "\nCreates: %d sent %d failed",
688 atomic_read(&sent[SMB2_CREATE_HE]),
689 atomic_read(&failed[SMB2_CREATE_HE]));
690 seq_printf(m, "\nCloses: %d sent %d failed",
691 atomic_read(&sent[SMB2_CLOSE_HE]),
692 atomic_read(&failed[SMB2_CLOSE_HE]));
693 seq_printf(m, "\nFlushes: %d sent %d failed",
694 atomic_read(&sent[SMB2_FLUSH_HE]),
695 atomic_read(&failed[SMB2_FLUSH_HE]));
696 seq_printf(m, "\nReads: %d sent %d failed",
697 atomic_read(&sent[SMB2_READ_HE]),
698 atomic_read(&failed[SMB2_READ_HE]));
699 seq_printf(m, "\nWrites: %d sent %d failed",
700 atomic_read(&sent[SMB2_WRITE_HE]),
701 atomic_read(&failed[SMB2_WRITE_HE]));
702 seq_printf(m, "\nLocks: %d sent %d failed",
703 atomic_read(&sent[SMB2_LOCK_HE]),
704 atomic_read(&failed[SMB2_LOCK_HE]));
705 seq_printf(m, "\nIOCTLs: %d sent %d failed",
706 atomic_read(&sent[SMB2_IOCTL_HE]),
707 atomic_read(&failed[SMB2_IOCTL_HE]));
708 seq_printf(m, "\nCancels: %d sent %d failed",
709 atomic_read(&sent[SMB2_CANCEL_HE]),
710 atomic_read(&failed[SMB2_CANCEL_HE]));
711 seq_printf(m, "\nEchos: %d sent %d failed",
712 atomic_read(&sent[SMB2_ECHO_HE]),
713 atomic_read(&failed[SMB2_ECHO_HE]));
714 seq_printf(m, "\nQueryDirectories: %d sent %d failed",
715 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
716 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
717 seq_printf(m, "\nChangeNotifies: %d sent %d failed",
718 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
719 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
720 seq_printf(m, "\nQueryInfos: %d sent %d failed",
721 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
722 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
723 seq_printf(m, "\nSetInfos: %d sent %d failed",
724 atomic_read(&sent[SMB2_SET_INFO_HE]),
725 atomic_read(&failed[SMB2_SET_INFO_HE]));
726 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
727 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
728 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
729#endif
730}
731
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700732static void
733smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
734{
David Howells2b0143b2015-03-17 22:25:59 +0000735 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovsky53ef1012013-09-05 16:11:28 +0400736 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
737
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700738 cfile->fid.persistent_fid = fid->persistent_fid;
739 cfile->fid.volatile_fid = fid->volatile_fid;
Pavel Shilovsky42873b02013-09-05 21:30:16 +0400740 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
741 &fid->purge_cache);
Pavel Shilovsky18cceb62013-09-05 13:01:06 +0400742 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
Aurelien Aptel94f87372016-09-22 07:38:50 +0200743 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700744}
745
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400746static void
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700747smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
748 struct cifs_fid *fid)
749{
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400750 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700751}
752
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -0700753static int
Steve French41c13582013-11-14 00:05:36 -0600754SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
755 u64 persistent_fid, u64 volatile_fid,
756 struct copychunk_ioctl *pcchunk)
757{
758 int rc;
759 unsigned int ret_data_len;
760 struct resume_key_req *res_key;
761
762 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
763 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +0100764 false /* use_ipc */,
Steve French41c13582013-11-14 00:05:36 -0600765 NULL, 0 /* no input */,
766 (char **)&res_key, &ret_data_len);
767
768 if (rc) {
769 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
770 goto req_res_key_exit;
771 }
772 if (ret_data_len < sizeof(struct resume_key_req)) {
773 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
774 rc = -EINVAL;
775 goto req_res_key_exit;
776 }
777 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
778
779req_res_key_exit:
780 kfree(res_key);
781 return rc;
782}
783
Sachin Prabhu620d8742017-02-10 16:03:51 +0530784static ssize_t
Sachin Prabhu312bbc52017-04-04 02:12:04 -0500785smb2_copychunk_range(const unsigned int xid,
Steve French41c13582013-11-14 00:05:36 -0600786 struct cifsFileInfo *srcfile,
787 struct cifsFileInfo *trgtfile, u64 src_off,
788 u64 len, u64 dest_off)
789{
790 int rc;
791 unsigned int ret_data_len;
792 struct copychunk_ioctl *pcchunk;
Steve French9bf0c9c2013-11-16 18:05:28 -0600793 struct copychunk_ioctl_rsp *retbuf = NULL;
794 struct cifs_tcon *tcon;
795 int chunks_copied = 0;
796 bool chunk_sizes_updated = false;
Sachin Prabhu620d8742017-02-10 16:03:51 +0530797 ssize_t bytes_written, total_bytes_written = 0;
Steve French41c13582013-11-14 00:05:36 -0600798
799 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
800
801 if (pcchunk == NULL)
802 return -ENOMEM;
803
Sachin Prabhu312bbc52017-04-04 02:12:04 -0500804 cifs_dbg(FYI, "in smb2_copychunk_range - about to call request res key\n");
Steve French41c13582013-11-14 00:05:36 -0600805 /* Request a key from the server to identify the source of the copy */
806 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
807 srcfile->fid.persistent_fid,
808 srcfile->fid.volatile_fid, pcchunk);
809
810 /* Note: request_res_key sets res_key null only if rc !=0 */
811 if (rc)
Steve French9bf0c9c2013-11-16 18:05:28 -0600812 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -0600813
814 /* For now array only one chunk long, will make more flexible later */
Fabian Frederickbc09d142014-12-10 15:41:15 -0800815 pcchunk->ChunkCount = cpu_to_le32(1);
Steve French41c13582013-11-14 00:05:36 -0600816 pcchunk->Reserved = 0;
Steve French41c13582013-11-14 00:05:36 -0600817 pcchunk->Reserved2 = 0;
818
Steve French9bf0c9c2013-11-16 18:05:28 -0600819 tcon = tlink_tcon(trgtfile->tlink);
820
821 while (len > 0) {
822 pcchunk->SourceOffset = cpu_to_le64(src_off);
823 pcchunk->TargetOffset = cpu_to_le64(dest_off);
824 pcchunk->Length =
825 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
826
827 /* Request server copy to target from src identified by key */
828 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
Steve French41c13582013-11-14 00:05:36 -0600829 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
Aurelien Aptel51146622017-02-28 15:08:41 +0100830 true /* is_fsctl */, false /* use_ipc */,
831 (char *)pcchunk,
Steve French9bf0c9c2013-11-16 18:05:28 -0600832 sizeof(struct copychunk_ioctl), (char **)&retbuf,
833 &ret_data_len);
834 if (rc == 0) {
835 if (ret_data_len !=
836 sizeof(struct copychunk_ioctl_rsp)) {
837 cifs_dbg(VFS, "invalid cchunk response size\n");
838 rc = -EIO;
839 goto cchunk_out;
840 }
841 if (retbuf->TotalBytesWritten == 0) {
842 cifs_dbg(FYI, "no bytes copied\n");
843 rc = -EIO;
844 goto cchunk_out;
845 }
846 /*
847 * Check if server claimed to write more than we asked
848 */
849 if (le32_to_cpu(retbuf->TotalBytesWritten) >
850 le32_to_cpu(pcchunk->Length)) {
851 cifs_dbg(VFS, "invalid copy chunk response\n");
852 rc = -EIO;
853 goto cchunk_out;
854 }
855 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
856 cifs_dbg(VFS, "invalid num chunks written\n");
857 rc = -EIO;
858 goto cchunk_out;
859 }
860 chunks_copied++;
Steve French41c13582013-11-14 00:05:36 -0600861
Sachin Prabhu620d8742017-02-10 16:03:51 +0530862 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
863 src_off += bytes_written;
864 dest_off += bytes_written;
865 len -= bytes_written;
866 total_bytes_written += bytes_written;
Steve French41c13582013-11-14 00:05:36 -0600867
Sachin Prabhu620d8742017-02-10 16:03:51 +0530868 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
Steve French9bf0c9c2013-11-16 18:05:28 -0600869 le32_to_cpu(retbuf->ChunksWritten),
870 le32_to_cpu(retbuf->ChunkBytesWritten),
Sachin Prabhu620d8742017-02-10 16:03:51 +0530871 bytes_written);
Steve French9bf0c9c2013-11-16 18:05:28 -0600872 } else if (rc == -EINVAL) {
873 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
874 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -0600875
Steve French9bf0c9c2013-11-16 18:05:28 -0600876 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
877 le32_to_cpu(retbuf->ChunksWritten),
878 le32_to_cpu(retbuf->ChunkBytesWritten),
879 le32_to_cpu(retbuf->TotalBytesWritten));
880
881 /*
882 * Check if this is the first request using these sizes,
883 * (ie check if copy succeed once with original sizes
884 * and check if the server gave us different sizes after
885 * we already updated max sizes on previous request).
886 * if not then why is the server returning an error now
887 */
888 if ((chunks_copied != 0) || chunk_sizes_updated)
889 goto cchunk_out;
890
891 /* Check that server is not asking us to grow size */
892 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
893 tcon->max_bytes_chunk)
894 tcon->max_bytes_chunk =
895 le32_to_cpu(retbuf->ChunkBytesWritten);
896 else
897 goto cchunk_out; /* server gave us bogus size */
898
899 /* No need to change MaxChunks since already set to 1 */
900 chunk_sizes_updated = true;
Sachin Prabhu2477bc52015-02-04 13:10:26 +0000901 } else
902 goto cchunk_out;
Steve French9bf0c9c2013-11-16 18:05:28 -0600903 }
904
905cchunk_out:
Steve French41c13582013-11-14 00:05:36 -0600906 kfree(pcchunk);
Steve French24df1482016-09-29 04:20:23 -0500907 kfree(retbuf);
Sachin Prabhu620d8742017-02-10 16:03:51 +0530908 if (rc)
909 return rc;
910 else
911 return total_bytes_written;
Steve French41c13582013-11-14 00:05:36 -0600912}
913
914static int
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -0700915smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
916 struct cifs_fid *fid)
917{
918 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
919}
920
Pavel Shilovsky09a47072012-09-18 16:20:29 -0700921static unsigned int
922smb2_read_data_offset(char *buf)
923{
924 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
925 return rsp->DataOffset;
926}
927
928static unsigned int
929smb2_read_data_length(char *buf)
930{
931 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
932 return le32_to_cpu(rsp->DataLength);
933}
934
Pavel Shilovskyd8e05032012-09-18 16:20:30 -0700935
936static int
Steve Frenchdb8b6312014-09-22 05:13:55 -0500937smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -0700938 struct cifs_io_parms *parms, unsigned int *bytes_read,
939 char **buf, int *buf_type)
940{
Steve Frenchdb8b6312014-09-22 05:13:55 -0500941 parms->persistent_fid = pfid->persistent_fid;
942 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -0700943 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
944}
945
Pavel Shilovsky009d3442012-09-18 16:20:30 -0700946static int
Steve Frenchdb8b6312014-09-22 05:13:55 -0500947smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovsky009d3442012-09-18 16:20:30 -0700948 struct cifs_io_parms *parms, unsigned int *written,
949 struct kvec *iov, unsigned long nr_segs)
950{
951
Steve Frenchdb8b6312014-09-22 05:13:55 -0500952 parms->persistent_fid = pfid->persistent_fid;
953 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovsky009d3442012-09-18 16:20:30 -0700954 return SMB2_write(xid, parms, written, iov, nr_segs);
955}
956
Steve Frenchd43cc792014-08-13 17:16:29 -0500957/* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
958static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
959 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
960{
961 struct cifsInodeInfo *cifsi;
962 int rc;
963
964 cifsi = CIFS_I(inode);
965
966 /* if file already sparse don't bother setting sparse again */
967 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
968 return true; /* already sparse */
969
970 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
971 return true; /* already not sparse */
972
973 /*
974 * Can't check for sparse support on share the usual way via the
975 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
976 * since Samba server doesn't set the flag on the share, yet
977 * supports the set sparse FSCTL and returns sparse correctly
978 * in the file attributes. If we fail setting sparse though we
979 * mark that server does not support sparse files for this share
980 * to avoid repeatedly sending the unsupported fsctl to server
981 * if the file is repeatedly extended.
982 */
983 if (tcon->broken_sparse_sup)
984 return false;
985
986 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
987 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
Aurelien Aptel51146622017-02-28 15:08:41 +0100988 true /* is_fctl */, false /* use_ipc */,
989 &setsparse, 1, NULL, NULL);
Steve Frenchd43cc792014-08-13 17:16:29 -0500990 if (rc) {
991 tcon->broken_sparse_sup = true;
992 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
993 return false;
994 }
995
996 if (setsparse)
997 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
998 else
999 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1000
1001 return true;
1002}
1003
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001004static int
1005smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1006 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1007{
1008 __le64 eof = cpu_to_le64(size);
Steve French3d1a3742014-08-11 21:05:25 -05001009 struct inode *inode;
1010
1011 /*
1012 * If extending file more than one page make sparse. Many Linux fs
1013 * make files sparse by default when extending via ftruncate
1014 */
David Howells2b0143b2015-03-17 22:25:59 +00001015 inode = d_inode(cfile->dentry);
Steve French3d1a3742014-08-11 21:05:25 -05001016
1017 if (!set_alloc && (size > inode->i_size + 8192)) {
Steve French3d1a3742014-08-11 21:05:25 -05001018 __u8 set_sparse = 1;
Steve French3d1a3742014-08-11 21:05:25 -05001019
Steve Frenchd43cc792014-08-13 17:16:29 -05001020 /* whether set sparse succeeds or not, extend the file */
1021 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
Steve French3d1a3742014-08-11 21:05:25 -05001022 }
1023
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001024 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05001025 cfile->fid.volatile_fid, cfile->pid, &eof, false);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001026}
1027
Steve French02b16662015-06-27 21:18:36 -07001028static int
1029smb2_duplicate_extents(const unsigned int xid,
1030 struct cifsFileInfo *srcfile,
1031 struct cifsFileInfo *trgtfile, u64 src_off,
1032 u64 len, u64 dest_off)
1033{
1034 int rc;
1035 unsigned int ret_data_len;
Steve French02b16662015-06-27 21:18:36 -07001036 struct duplicate_extents_to_file dup_ext_buf;
1037 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1038
1039 /* server fileays advertise duplicate extent support with this flag */
1040 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1041 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1042 return -EOPNOTSUPP;
1043
1044 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1045 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1046 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1047 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1048 dup_ext_buf.ByteCount = cpu_to_le64(len);
1049 cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
1050 src_off, dest_off, len);
1051
1052 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1053 if (rc)
1054 goto duplicate_extents_out;
1055
1056 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1057 trgtfile->fid.volatile_fid,
1058 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
Aurelien Aptel51146622017-02-28 15:08:41 +01001059 true /* is_fsctl */, false /* use_ipc */,
1060 (char *)&dup_ext_buf,
Steve French02b16662015-06-27 21:18:36 -07001061 sizeof(struct duplicate_extents_to_file),
Steve French24df1482016-09-29 04:20:23 -05001062 NULL,
Steve French02b16662015-06-27 21:18:36 -07001063 &ret_data_len);
1064
1065 if (ret_data_len > 0)
1066 cifs_dbg(FYI, "non-zero response length in duplicate extents");
1067
1068duplicate_extents_out:
1069 return rc;
1070}
Steve French02b16662015-06-27 21:18:36 -07001071
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001072static int
Steve French64a5cfa2013-10-14 15:31:32 -05001073smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1074 struct cifsFileInfo *cfile)
1075{
1076 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1077 cfile->fid.volatile_fid);
1078}
1079
1080static int
Steve Frenchb3152e22015-06-24 03:17:02 -05001081smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1082 struct cifsFileInfo *cfile)
1083{
1084 struct fsctl_set_integrity_information_req integr_info;
Steve Frenchb3152e22015-06-24 03:17:02 -05001085 unsigned int ret_data_len;
1086
1087 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1088 integr_info.Flags = 0;
1089 integr_info.Reserved = 0;
1090
1091 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1092 cfile->fid.volatile_fid,
1093 FSCTL_SET_INTEGRITY_INFORMATION,
Aurelien Aptel51146622017-02-28 15:08:41 +01001094 true /* is_fsctl */, false /* use_ipc */,
1095 (char *)&integr_info,
Steve Frenchb3152e22015-06-24 03:17:02 -05001096 sizeof(struct fsctl_set_integrity_information_req),
Steve French24df1482016-09-29 04:20:23 -05001097 NULL,
Steve Frenchb3152e22015-06-24 03:17:02 -05001098 &ret_data_len);
1099
1100}
1101
1102static int
Steve French834170c2016-09-30 21:14:26 -05001103smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1104 struct cifsFileInfo *cfile, void __user *ioc_buf)
1105{
1106 char *retbuf = NULL;
1107 unsigned int ret_data_len = 0;
1108 int rc;
1109 struct smb_snapshot_array snapshot_in;
1110
1111 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1112 cfile->fid.volatile_fid,
1113 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
Aurelien Aptel51146622017-02-28 15:08:41 +01001114 true /* is_fsctl */, false /* use_ipc */,
1115 NULL, 0 /* no input data */,
Steve French834170c2016-09-30 21:14:26 -05001116 (char **)&retbuf,
1117 &ret_data_len);
1118 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1119 rc, ret_data_len);
1120 if (rc)
1121 return rc;
1122
1123 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1124 /* Fixup buffer */
1125 if (copy_from_user(&snapshot_in, ioc_buf,
1126 sizeof(struct smb_snapshot_array))) {
1127 rc = -EFAULT;
1128 kfree(retbuf);
1129 return rc;
1130 }
1131 if (snapshot_in.snapshot_array_size < sizeof(struct smb_snapshot_array)) {
1132 rc = -ERANGE;
David Disseldorp0e5c7952017-05-03 17:39:09 +02001133 kfree(retbuf);
Steve French834170c2016-09-30 21:14:26 -05001134 return rc;
1135 }
1136
1137 if (ret_data_len > snapshot_in.snapshot_array_size)
1138 ret_data_len = snapshot_in.snapshot_array_size;
1139
1140 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1141 rc = -EFAULT;
1142 }
1143
1144 kfree(retbuf);
1145 return rc;
1146}
1147
1148static int
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001149smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1150 const char *path, struct cifs_sb_info *cifs_sb,
1151 struct cifs_fid *fid, __u16 search_flags,
1152 struct cifs_search_info *srch_inf)
1153{
1154 __le16 *utf16_path;
1155 int rc;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001156 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001157 struct cifs_open_parms oparms;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001158
1159 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1160 if (!utf16_path)
1161 return -ENOMEM;
1162
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001163 oparms.tcon = tcon;
1164 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1165 oparms.disposition = FILE_OPEN;
1166 oparms.create_options = 0;
1167 oparms.fid = fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001168 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001169
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001170 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001171 kfree(utf16_path);
1172 if (rc) {
Pavel Shilovskydcd878382017-06-06 16:58:58 -07001173 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001174 return rc;
1175 }
1176
1177 srch_inf->entries_in_buffer = 0;
1178 srch_inf->index_of_last_entry = 0;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001179
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001180 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1181 fid->volatile_fid, 0, srch_inf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001182 if (rc) {
Pavel Shilovskydcd878382017-06-06 16:58:58 -07001183 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001184 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001185 }
1186 return rc;
1187}
1188
1189static int
1190smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1191 struct cifs_fid *fid, __u16 search_flags,
1192 struct cifs_search_info *srch_inf)
1193{
1194 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1195 fid->volatile_fid, 0, srch_inf);
1196}
1197
1198static int
1199smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1200 struct cifs_fid *fid)
1201{
1202 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1203}
1204
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001205/*
1206* If we negotiate SMB2 protocol and get STATUS_PENDING - update
1207* the number of credits and return true. Otherwise - return false.
1208*/
1209static bool
1210smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
1211{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001212 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001213
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001214 if (shdr->Status != STATUS_PENDING)
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001215 return false;
1216
1217 if (!length) {
1218 spin_lock(&server->req_lock);
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001219 server->credits += le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001220 spin_unlock(&server->req_lock);
1221 wake_up(&server->request_q);
1222 }
1223
1224 return true;
1225}
1226
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001227static bool
1228smb2_is_session_expired(char *buf)
1229{
1230 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
1231
1232 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED)
1233 return false;
1234
1235 cifs_dbg(FYI, "Session expired\n");
1236 return true;
1237}
1238
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001239static int
1240smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
1241 struct cifsInodeInfo *cinode)
1242{
Pavel Shilovsky0822f512012-09-19 06:22:45 -07001243 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
1244 return SMB2_lease_break(0, tcon, cinode->lease_key,
1245 smb2_get_lease_state(cinode));
1246
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001247 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
1248 fid->volatile_fid,
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001249 CIFS_CACHE_READ(cinode) ? 1 : 0);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001250}
1251
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001252static int
1253smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1254 struct kstatfs *buf)
1255{
1256 int rc;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001257 __le16 srch_path = 0; /* Null - open root of share */
1258 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001259 struct cifs_open_parms oparms;
1260 struct cifs_fid fid;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001261
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001262 oparms.tcon = tcon;
1263 oparms.desired_access = FILE_READ_ATTRIBUTES;
1264 oparms.disposition = FILE_OPEN;
1265 oparms.create_options = 0;
1266 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001267 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001268
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001269 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001270 if (rc)
1271 return rc;
1272 buf->f_type = SMB2_MAGIC_NUMBER;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001273 rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1274 buf);
1275 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001276 return rc;
1277}
1278
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07001279static bool
1280smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
1281{
1282 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
1283 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
1284}
1285
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07001286static int
1287smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
1288 __u64 length, __u32 type, int lock, int unlock, bool wait)
1289{
1290 if (unlock && !lock)
1291 type = SMB2_LOCKFLAG_UNLOCK;
1292 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
1293 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
1294 current->tgid, length, offset, type, wait);
1295}
1296
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001297static void
1298smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
1299{
1300 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
1301}
1302
1303static void
1304smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
1305{
1306 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
1307}
1308
1309static void
1310smb2_new_lease_key(struct cifs_fid *fid)
1311{
Steve Frenchfa70b872016-09-22 00:39:34 -05001312 generate_random_uuid(fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001313}
1314
Aurelien Aptel9d496402017-02-13 16:16:49 +01001315static int
1316smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
1317 const char *search_name,
1318 struct dfs_info3_param **target_nodes,
1319 unsigned int *num_of_nodes,
1320 const struct nls_table *nls_codepage, int remap)
1321{
1322 int rc;
1323 __le16 *utf16_path = NULL;
1324 int utf16_path_len = 0;
1325 struct cifs_tcon *tcon;
1326 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
1327 struct get_dfs_referral_rsp *dfs_rsp = NULL;
1328 u32 dfs_req_size = 0, dfs_rsp_size = 0;
1329
1330 cifs_dbg(FYI, "smb2_get_dfs_refer path <%s>\n", search_name);
1331
1332 /*
1333 * Use any tcon from the current session. Here, the first one.
1334 */
1335 spin_lock(&cifs_tcp_ses_lock);
1336 tcon = list_first_entry_or_null(&ses->tcon_list, struct cifs_tcon,
1337 tcon_list);
1338 if (tcon)
1339 tcon->tc_count++;
1340 spin_unlock(&cifs_tcp_ses_lock);
1341
1342 if (!tcon) {
1343 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
1344 ses);
1345 rc = -ENOTCONN;
1346 goto out;
1347 }
1348
1349 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
1350 &utf16_path_len,
1351 nls_codepage, remap);
1352 if (!utf16_path) {
1353 rc = -ENOMEM;
1354 goto out;
1355 }
1356
1357 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
1358 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
1359 if (!dfs_req) {
1360 rc = -ENOMEM;
1361 goto out;
1362 }
1363
1364 /* Highest DFS referral version understood */
1365 dfs_req->MaxReferralLevel = DFS_VERSION;
1366
1367 /* Path to resolve in an UTF-16 null-terminated string */
1368 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
1369
1370 do {
1371 /* try first with IPC */
1372 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1373 FSCTL_DFS_GET_REFERRALS,
1374 true /* is_fsctl */, true /* use_ipc */,
1375 (char *)dfs_req, dfs_req_size,
1376 (char **)&dfs_rsp, &dfs_rsp_size);
1377 if (rc == -ENOTCONN) {
1378 /* try with normal tcon */
1379 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1380 FSCTL_DFS_GET_REFERRALS,
1381 true /* is_fsctl */, false /*use_ipc*/,
1382 (char *)dfs_req, dfs_req_size,
1383 (char **)&dfs_rsp, &dfs_rsp_size);
1384 }
1385 } while (rc == -EAGAIN);
1386
1387 if (rc) {
1388 cifs_dbg(VFS, "ioctl error in smb2_get_dfs_refer rc=%d\n", rc);
1389 goto out;
1390 }
1391
1392 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
1393 num_of_nodes, target_nodes,
1394 nls_codepage, remap, search_name,
1395 true /* is_unicode */);
1396 if (rc) {
1397 cifs_dbg(VFS, "parse error in smb2_get_dfs_refer rc=%d\n", rc);
1398 goto out;
1399 }
1400
1401 out:
1402 if (tcon) {
1403 spin_lock(&cifs_tcp_ses_lock);
1404 tcon->tc_count--;
1405 spin_unlock(&cifs_tcp_ses_lock);
1406 }
1407 kfree(utf16_path);
1408 kfree(dfs_req);
1409 kfree(dfs_rsp);
1410 return rc;
1411}
Pavel Shilovsky78932422016-07-24 10:37:38 +03001412#define SMB2_SYMLINK_STRUCT_SIZE \
1413 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
1414
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001415static int
1416smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
1417 const char *full_path, char **target_path,
1418 struct cifs_sb_info *cifs_sb)
1419{
1420 int rc;
1421 __le16 *utf16_path;
1422 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1423 struct cifs_open_parms oparms;
1424 struct cifs_fid fid;
1425 struct smb2_err_rsp *err_buf = NULL;
1426 struct smb2_symlink_err_rsp *symlink;
Pavel Shilovsky78932422016-07-24 10:37:38 +03001427 unsigned int sub_len;
1428 unsigned int sub_offset;
1429 unsigned int print_len;
1430 unsigned int print_offset;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001431
1432 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
1433
1434 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1435 if (!utf16_path)
1436 return -ENOMEM;
1437
1438 oparms.tcon = tcon;
1439 oparms.desired_access = FILE_READ_ATTRIBUTES;
1440 oparms.disposition = FILE_OPEN;
1441 oparms.create_options = 0;
1442 oparms.fid = &fid;
1443 oparms.reconnect = false;
1444
1445 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_buf);
1446
1447 if (!rc || !err_buf) {
1448 kfree(utf16_path);
1449 return -ENOENT;
1450 }
Pavel Shilovsky78932422016-07-24 10:37:38 +03001451
1452 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
1453 get_rfc1002_length(err_buf) + 4 < SMB2_SYMLINK_STRUCT_SIZE) {
1454 kfree(utf16_path);
1455 return -ENOENT;
1456 }
1457
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001458 /* open must fail on symlink - reset rc */
1459 rc = 0;
1460 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
1461 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
1462 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
Pavel Shilovsky78932422016-07-24 10:37:38 +03001463 print_len = le16_to_cpu(symlink->PrintNameLength);
1464 print_offset = le16_to_cpu(symlink->PrintNameOffset);
1465
1466 if (get_rfc1002_length(err_buf) + 4 <
1467 SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
1468 kfree(utf16_path);
1469 return -ENOENT;
1470 }
1471
1472 if (get_rfc1002_length(err_buf) + 4 <
1473 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
1474 kfree(utf16_path);
1475 return -ENOENT;
1476 }
1477
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001478 *target_path = cifs_strndup_from_utf16(
1479 (char *)symlink->PathBuffer + sub_offset,
1480 sub_len, true, cifs_sb->local_nls);
1481 if (!(*target_path)) {
1482 kfree(utf16_path);
1483 return -ENOMEM;
1484 }
1485 convert_delimiter(*target_path, '/');
1486 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
1487 kfree(utf16_path);
1488 return rc;
1489}
1490
Arnd Bergmann84908422017-06-27 17:06:13 +02001491#ifdef CONFIG_CIFS_ACL
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001492static struct cifs_ntsd *
1493get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
1494 const struct cifs_fid *cifsfid, u32 *pacllen)
1495{
1496 struct cifs_ntsd *pntsd = NULL;
1497 unsigned int xid;
1498 int rc = -EOPNOTSUPP;
1499 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1500
1501 if (IS_ERR(tlink))
1502 return ERR_CAST(tlink);
1503
1504 xid = get_xid();
1505 cifs_dbg(FYI, "trying to get acl\n");
1506
1507 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
1508 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
1509 free_xid(xid);
1510
1511 cifs_put_tlink(tlink);
1512
1513 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
1514 if (rc)
1515 return ERR_PTR(rc);
1516 return pntsd;
1517
1518}
1519
1520static struct cifs_ntsd *
1521get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
1522 const char *path, u32 *pacllen)
1523{
1524 struct cifs_ntsd *pntsd = NULL;
1525 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1526 unsigned int xid;
1527 int rc;
1528 struct cifs_tcon *tcon;
1529 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1530 struct cifs_fid fid;
1531 struct cifs_open_parms oparms;
1532 __le16 *utf16_path;
1533
1534 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
1535 if (IS_ERR(tlink))
1536 return ERR_CAST(tlink);
1537
1538 tcon = tlink_tcon(tlink);
1539 xid = get_xid();
1540
1541 if (backup_cred(cifs_sb))
Colin Ian King709340a2017-07-05 13:47:34 +01001542 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001543 else
1544 oparms.create_options = 0;
1545
1546 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1547 if (!utf16_path)
1548 return ERR_PTR(-ENOMEM);
1549
1550 oparms.tcon = tcon;
1551 oparms.desired_access = READ_CONTROL;
1552 oparms.disposition = FILE_OPEN;
1553 oparms.fid = &fid;
1554 oparms.reconnect = false;
1555
1556 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
1557 kfree(utf16_path);
1558 if (!rc) {
1559 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
1560 fid.volatile_fid, (void **)&pntsd, pacllen);
1561 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1562 }
1563
1564 cifs_put_tlink(tlink);
1565 free_xid(xid);
1566
1567 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
1568 if (rc)
1569 return ERR_PTR(rc);
1570 return pntsd;
1571}
1572
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05001573#ifdef CONFIG_CIFS_ACL
1574static int
1575set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
1576 struct inode *inode, const char *path, int aclflag)
1577{
1578 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1579 unsigned int xid;
1580 int rc, access_flags = 0;
1581 struct cifs_tcon *tcon;
1582 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1583 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1584 struct cifs_fid fid;
1585 struct cifs_open_parms oparms;
1586 __le16 *utf16_path;
1587
1588 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
1589 if (IS_ERR(tlink))
1590 return PTR_ERR(tlink);
1591
1592 tcon = tlink_tcon(tlink);
1593 xid = get_xid();
1594
1595 if (backup_cred(cifs_sb))
1596 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1597 else
1598 oparms.create_options = 0;
1599
1600 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
1601 access_flags = WRITE_OWNER;
1602 else
1603 access_flags = WRITE_DAC;
1604
1605 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1606 if (!utf16_path)
1607 return -ENOMEM;
1608
1609 oparms.tcon = tcon;
1610 oparms.desired_access = access_flags;
1611 oparms.disposition = FILE_OPEN;
1612 oparms.path = path;
1613 oparms.fid = &fid;
1614 oparms.reconnect = false;
1615
1616 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
1617 kfree(utf16_path);
1618 if (!rc) {
1619 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
1620 fid.volatile_fid, pnntsd, acllen, aclflag);
1621 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1622 }
1623
1624 cifs_put_tlink(tlink);
1625 free_xid(xid);
1626 return rc;
1627}
1628#endif /* CIFS_ACL */
1629
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001630/* Retrieve an ACL from the server */
1631static struct cifs_ntsd *
1632get_smb2_acl(struct cifs_sb_info *cifs_sb,
1633 struct inode *inode, const char *path,
1634 u32 *pacllen)
1635{
1636 struct cifs_ntsd *pntsd = NULL;
1637 struct cifsFileInfo *open_file = NULL;
1638
1639 if (inode)
1640 open_file = find_readable_file(CIFS_I(inode), true);
1641 if (!open_file)
1642 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
1643
1644 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
1645 cifsFileInfo_put(open_file);
1646 return pntsd;
1647}
Arnd Bergmann84908422017-06-27 17:06:13 +02001648#endif
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001649
Steve French30175622014-08-17 18:16:40 -05001650static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
1651 loff_t offset, loff_t len, bool keep_size)
1652{
1653 struct inode *inode;
1654 struct cifsInodeInfo *cifsi;
1655 struct cifsFileInfo *cfile = file->private_data;
1656 struct file_zero_data_information fsctl_buf;
1657 long rc;
1658 unsigned int xid;
1659
1660 xid = get_xid();
1661
David Howells2b0143b2015-03-17 22:25:59 +00001662 inode = d_inode(cfile->dentry);
Steve French30175622014-08-17 18:16:40 -05001663 cifsi = CIFS_I(inode);
1664
1665 /* if file not oplocked can't be sure whether asking to extend size */
1666 if (!CIFS_CACHE_READ(cifsi))
1667 if (keep_size == false)
1668 return -EOPNOTSUPP;
1669
Steve French2bb93d22014-08-20 18:56:29 -05001670 /*
Steve French30175622014-08-17 18:16:40 -05001671 * Must check if file sparse since fallocate -z (zero range) assumes
1672 * non-sparse allocation
1673 */
1674 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE))
1675 return -EOPNOTSUPP;
1676
1677 /*
1678 * need to make sure we are not asked to extend the file since the SMB3
1679 * fsctl does not change the file size. In the future we could change
1680 * this to zero the first part of the range then set the file size
1681 * which for a non sparse file would zero the newly extended range
1682 */
1683 if (keep_size == false)
1684 if (i_size_read(inode) < offset + len)
1685 return -EOPNOTSUPP;
1686
1687 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1688
1689 fsctl_buf.FileOffset = cpu_to_le64(offset);
1690 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1691
1692 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1693 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
Aurelien Aptel51146622017-02-28 15:08:41 +01001694 true /* is_fctl */, false /* use_ipc */,
1695 (char *)&fsctl_buf,
Steve French30175622014-08-17 18:16:40 -05001696 sizeof(struct file_zero_data_information), NULL, NULL);
1697 free_xid(xid);
1698 return rc;
1699}
1700
Steve French31742c52014-08-17 08:38:47 -05001701static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
1702 loff_t offset, loff_t len)
1703{
1704 struct inode *inode;
1705 struct cifsInodeInfo *cifsi;
1706 struct cifsFileInfo *cfile = file->private_data;
1707 struct file_zero_data_information fsctl_buf;
1708 long rc;
1709 unsigned int xid;
1710 __u8 set_sparse = 1;
1711
1712 xid = get_xid();
1713
David Howells2b0143b2015-03-17 22:25:59 +00001714 inode = d_inode(cfile->dentry);
Steve French31742c52014-08-17 08:38:47 -05001715 cifsi = CIFS_I(inode);
1716
1717 /* Need to make file sparse, if not already, before freeing range. */
1718 /* Consider adding equivalent for compressed since it could also work */
1719 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse))
1720 return -EOPNOTSUPP;
1721
1722 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1723
1724 fsctl_buf.FileOffset = cpu_to_le64(offset);
1725 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1726
1727 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1728 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
Aurelien Aptel51146622017-02-28 15:08:41 +01001729 true /* is_fctl */, false /* use_ipc */,
1730 (char *)&fsctl_buf,
Steve French31742c52014-08-17 08:38:47 -05001731 sizeof(struct file_zero_data_information), NULL, NULL);
1732 free_xid(xid);
1733 return rc;
1734}
1735
Steve French9ccf3212014-10-18 17:01:15 -05001736static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
1737 loff_t off, loff_t len, bool keep_size)
1738{
1739 struct inode *inode;
1740 struct cifsInodeInfo *cifsi;
1741 struct cifsFileInfo *cfile = file->private_data;
1742 long rc = -EOPNOTSUPP;
1743 unsigned int xid;
1744
1745 xid = get_xid();
1746
David Howells2b0143b2015-03-17 22:25:59 +00001747 inode = d_inode(cfile->dentry);
Steve French9ccf3212014-10-18 17:01:15 -05001748 cifsi = CIFS_I(inode);
1749
1750 /* if file not oplocked can't be sure whether asking to extend size */
1751 if (!CIFS_CACHE_READ(cifsi))
1752 if (keep_size == false)
1753 return -EOPNOTSUPP;
1754
1755 /*
1756 * Files are non-sparse by default so falloc may be a no-op
1757 * Must check if file sparse. If not sparse, and not extending
1758 * then no need to do anything since file already allocated
1759 */
1760 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
1761 if (keep_size == true)
1762 return 0;
1763 /* check if extending file */
1764 else if (i_size_read(inode) >= off + len)
1765 /* not extending file and already not sparse */
1766 return 0;
1767 /* BB: in future add else clause to extend file */
1768 else
1769 return -EOPNOTSUPP;
1770 }
1771
1772 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
1773 /*
1774 * Check if falloc starts within first few pages of file
1775 * and ends within a few pages of the end of file to
1776 * ensure that most of file is being forced to be
1777 * fallocated now. If so then setting whole file sparse
1778 * ie potentially making a few extra pages at the beginning
1779 * or end of the file non-sparse via set_sparse is harmless.
1780 */
1781 if ((off > 8192) || (off + len + 8192 < i_size_read(inode)))
1782 return -EOPNOTSUPP;
1783
1784 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
1785 }
1786 /* BB: else ... in future add code to extend file and set sparse */
1787
1788
1789 free_xid(xid);
1790 return rc;
1791}
1792
1793
Steve French31742c52014-08-17 08:38:47 -05001794static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
1795 loff_t off, loff_t len)
1796{
1797 /* KEEP_SIZE already checked for by do_fallocate */
1798 if (mode & FALLOC_FL_PUNCH_HOLE)
1799 return smb3_punch_hole(file, tcon, off, len);
Steve French30175622014-08-17 18:16:40 -05001800 else if (mode & FALLOC_FL_ZERO_RANGE) {
1801 if (mode & FALLOC_FL_KEEP_SIZE)
1802 return smb3_zero_range(file, tcon, off, len, true);
1803 return smb3_zero_range(file, tcon, off, len, false);
Steve French9ccf3212014-10-18 17:01:15 -05001804 } else if (mode == FALLOC_FL_KEEP_SIZE)
1805 return smb3_simple_falloc(file, tcon, off, len, true);
1806 else if (mode == 0)
1807 return smb3_simple_falloc(file, tcon, off, len, false);
Steve French31742c52014-08-17 08:38:47 -05001808
1809 return -EOPNOTSUPP;
1810}
1811
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001812static void
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00001813smb2_downgrade_oplock(struct TCP_Server_Info *server,
1814 struct cifsInodeInfo *cinode, bool set_level2)
1815{
1816 if (set_level2)
1817 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
1818 0, NULL);
1819 else
1820 server->ops->set_oplock_level(cinode, 0, 0, NULL);
1821}
1822
1823static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001824smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1825 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001826{
1827 oplock &= 0xFF;
1828 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1829 return;
1830 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001831 cinode->oplock = CIFS_CACHE_RHW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001832 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
1833 &cinode->vfs_inode);
1834 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001835 cinode->oplock = CIFS_CACHE_RW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001836 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
1837 &cinode->vfs_inode);
1838 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
1839 cinode->oplock = CIFS_CACHE_READ_FLG;
1840 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
1841 &cinode->vfs_inode);
1842 } else
1843 cinode->oplock = 0;
1844}
1845
1846static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001847smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1848 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001849{
1850 char message[5] = {0};
1851
1852 oplock &= 0xFF;
1853 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1854 return;
1855
1856 cinode->oplock = 0;
1857 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
1858 cinode->oplock |= CIFS_CACHE_READ_FLG;
1859 strcat(message, "R");
1860 }
1861 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
1862 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
1863 strcat(message, "H");
1864 }
1865 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
1866 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
1867 strcat(message, "W");
1868 }
1869 if (!cinode->oplock)
1870 strcat(message, "None");
1871 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
1872 &cinode->vfs_inode);
1873}
1874
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001875static void
1876smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1877 unsigned int epoch, bool *purge_cache)
1878{
1879 unsigned int old_oplock = cinode->oplock;
1880
1881 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
1882
1883 if (purge_cache) {
1884 *purge_cache = false;
1885 if (old_oplock == CIFS_CACHE_READ_FLG) {
1886 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
1887 (epoch - cinode->epoch > 0))
1888 *purge_cache = true;
1889 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1890 (epoch - cinode->epoch > 1))
1891 *purge_cache = true;
1892 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1893 (epoch - cinode->epoch > 1))
1894 *purge_cache = true;
1895 else if (cinode->oplock == 0 &&
1896 (epoch - cinode->epoch > 0))
1897 *purge_cache = true;
1898 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
1899 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1900 (epoch - cinode->epoch > 0))
1901 *purge_cache = true;
1902 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1903 (epoch - cinode->epoch > 1))
1904 *purge_cache = true;
1905 }
1906 cinode->epoch = epoch;
1907 }
1908}
1909
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001910static bool
1911smb2_is_read_op(__u32 oplock)
1912{
1913 return oplock == SMB2_OPLOCK_LEVEL_II;
1914}
1915
1916static bool
1917smb21_is_read_op(__u32 oplock)
1918{
1919 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
1920 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
1921}
1922
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001923static __le32
1924map_oplock_to_lease(u8 oplock)
1925{
1926 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
1927 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
1928 else if (oplock == SMB2_OPLOCK_LEVEL_II)
1929 return SMB2_LEASE_READ_CACHING;
1930 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
1931 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
1932 SMB2_LEASE_WRITE_CACHING;
1933 return 0;
1934}
1935
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001936static char *
1937smb2_create_lease_buf(u8 *lease_key, u8 oplock)
1938{
1939 struct create_lease *buf;
1940
1941 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
1942 if (!buf)
1943 return NULL;
1944
1945 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1946 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001947 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001948
1949 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1950 (struct create_lease, lcontext));
1951 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
1952 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1953 (struct create_lease, Name));
1954 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001955 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001956 buf->Name[0] = 'R';
1957 buf->Name[1] = 'q';
1958 buf->Name[2] = 'L';
1959 buf->Name[3] = 's';
1960 return (char *)buf;
1961}
1962
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001963static char *
1964smb3_create_lease_buf(u8 *lease_key, u8 oplock)
1965{
1966 struct create_lease_v2 *buf;
1967
1968 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
1969 if (!buf)
1970 return NULL;
1971
1972 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1973 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
1974 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
1975
1976 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1977 (struct create_lease_v2, lcontext));
1978 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
1979 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1980 (struct create_lease_v2, Name));
1981 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001982 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001983 buf->Name[0] = 'R';
1984 buf->Name[1] = 'q';
1985 buf->Name[2] = 'L';
1986 buf->Name[3] = 's';
1987 return (char *)buf;
1988}
1989
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001990static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001991smb2_parse_lease_buf(void *buf, unsigned int *epoch)
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001992{
1993 struct create_lease *lc = (struct create_lease *)buf;
1994
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001995 *epoch = 0; /* not used */
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001996 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1997 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1998 return le32_to_cpu(lc->lcontext.LeaseState);
1999}
2000
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002001static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002002smb3_parse_lease_buf(void *buf, unsigned int *epoch)
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002003{
2004 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
2005
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002006 *epoch = le16_to_cpu(lc->lcontext.Epoch);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002007 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2008 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2009 return le32_to_cpu(lc->lcontext.LeaseState);
2010}
2011
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002012static unsigned int
2013smb2_wp_retry_size(struct inode *inode)
2014{
2015 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
2016 SMB2_MAX_BUFFER_SIZE);
2017}
2018
Pavel Shilovsky52755802014-08-18 20:49:57 +04002019static bool
2020smb2_dir_needs_close(struct cifsFileInfo *cfile)
2021{
2022 return !cfile->invalidHandle;
2023}
2024
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002025static void
2026fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, struct smb_rqst *old_rq)
2027{
2028 struct smb2_sync_hdr *shdr =
2029 (struct smb2_sync_hdr *)old_rq->rq_iov[1].iov_base;
2030 unsigned int orig_len = get_rfc1002_length(old_rq->rq_iov[0].iov_base);
2031
2032 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
2033 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
2034 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
2035 tr_hdr->Flags = cpu_to_le16(0x01);
2036 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2037 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
2038 inc_rfc1001_len(tr_hdr, sizeof(struct smb2_transform_hdr) - 4);
2039 inc_rfc1001_len(tr_hdr, orig_len);
2040}
2041
2042static struct scatterlist *
2043init_sg(struct smb_rqst *rqst, u8 *sign)
2044{
2045 unsigned int sg_len = rqst->rq_nvec + rqst->rq_npages + 1;
2046 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 24;
2047 struct scatterlist *sg;
2048 unsigned int i;
2049 unsigned int j;
2050
2051 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
2052 if (!sg)
2053 return NULL;
2054
2055 sg_init_table(sg, sg_len);
2056 sg_set_buf(&sg[0], rqst->rq_iov[0].iov_base + 24, assoc_data_len);
2057 for (i = 1; i < rqst->rq_nvec; i++)
2058 sg_set_buf(&sg[i], rqst->rq_iov[i].iov_base,
2059 rqst->rq_iov[i].iov_len);
2060 for (j = 0; i < sg_len - 1; i++, j++) {
2061 unsigned int len = (j < rqst->rq_npages - 1) ? rqst->rq_pagesz
2062 : rqst->rq_tailsz;
2063 sg_set_page(&sg[i], rqst->rq_pages[j], len, 0);
2064 }
2065 sg_set_buf(&sg[sg_len - 1], sign, SMB2_SIGNATURE_SIZE);
2066 return sg;
2067}
2068
2069struct cifs_crypt_result {
2070 int err;
2071 struct completion completion;
2072};
2073
2074static void cifs_crypt_complete(struct crypto_async_request *req, int err)
2075{
2076 struct cifs_crypt_result *res = req->data;
2077
2078 if (err == -EINPROGRESS)
2079 return;
2080
2081 res->err = err;
2082 complete(&res->completion);
2083}
2084
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002085static int
2086smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
2087{
2088 struct cifs_ses *ses;
2089 u8 *ses_enc_key;
2090
2091 spin_lock(&cifs_tcp_ses_lock);
2092 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2093 if (ses->Suid != ses_id)
2094 continue;
2095 ses_enc_key = enc ? ses->smb3encryptionkey :
2096 ses->smb3decryptionkey;
2097 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
2098 spin_unlock(&cifs_tcp_ses_lock);
2099 return 0;
2100 }
2101 spin_unlock(&cifs_tcp_ses_lock);
2102
2103 return 1;
2104}
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002105/*
2106 * Encrypt or decrypt @rqst message. @rqst has the following format:
2107 * iov[0] - transform header (associate data),
2108 * iov[1-N] and pages - data to encrypt.
2109 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
2110 * untouched.
2111 */
2112static int
2113crypt_message(struct TCP_Server_Info *server, struct smb_rqst *rqst, int enc)
2114{
2115 struct smb2_transform_hdr *tr_hdr =
2116 (struct smb2_transform_hdr *)rqst->rq_iov[0].iov_base;
2117 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 24;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002118 int rc = 0;
2119 struct scatterlist *sg;
2120 u8 sign[SMB2_SIGNATURE_SIZE] = {};
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002121 u8 key[SMB3_SIGN_KEY_SIZE];
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002122 struct aead_request *req;
2123 char *iv;
2124 unsigned int iv_len;
2125 struct cifs_crypt_result result = {0, };
2126 struct crypto_aead *tfm;
2127 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2128
2129 init_completion(&result.completion);
2130
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002131 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
2132 if (rc) {
2133 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
2134 enc ? "en" : "de");
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002135 return 0;
2136 }
2137
2138 rc = smb3_crypto_aead_allocate(server);
2139 if (rc) {
2140 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
2141 return rc;
2142 }
2143
2144 tfm = enc ? server->secmech.ccmaesencrypt :
2145 server->secmech.ccmaesdecrypt;
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002146 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002147 if (rc) {
2148 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
2149 return rc;
2150 }
2151
2152 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
2153 if (rc) {
2154 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
2155 return rc;
2156 }
2157
2158 req = aead_request_alloc(tfm, GFP_KERNEL);
2159 if (!req) {
2160 cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
2161 return -ENOMEM;
2162 }
2163
2164 if (!enc) {
2165 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
2166 crypt_len += SMB2_SIGNATURE_SIZE;
2167 }
2168
2169 sg = init_sg(rqst, sign);
2170 if (!sg) {
Christophe Jaillet517a6e42017-06-11 09:12:47 +02002171 cifs_dbg(VFS, "%s: Failed to init sg", __func__);
2172 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002173 goto free_req;
2174 }
2175
2176 iv_len = crypto_aead_ivsize(tfm);
2177 iv = kzalloc(iv_len, GFP_KERNEL);
2178 if (!iv) {
2179 cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
Christophe Jaillet517a6e42017-06-11 09:12:47 +02002180 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002181 goto free_sg;
2182 }
2183 iv[0] = 3;
2184 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2185
2186 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
2187 aead_request_set_ad(req, assoc_data_len);
2188
2189 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2190 cifs_crypt_complete, &result);
2191
2192 rc = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
2193
2194 if (rc == -EINPROGRESS || rc == -EBUSY) {
2195 wait_for_completion(&result.completion);
2196 rc = result.err;
2197 }
2198
2199 if (!rc && enc)
2200 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
2201
2202 kfree(iv);
2203free_sg:
2204 kfree(sg);
2205free_req:
2206 kfree(req);
2207 return rc;
2208}
2209
2210static int
2211smb3_init_transform_rq(struct TCP_Server_Info *server, struct smb_rqst *new_rq,
2212 struct smb_rqst *old_rq)
2213{
2214 struct kvec *iov;
2215 struct page **pages;
2216 struct smb2_transform_hdr *tr_hdr;
2217 unsigned int npages = old_rq->rq_npages;
2218 int i;
2219 int rc = -ENOMEM;
2220
2221 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2222 if (!pages)
2223 return rc;
2224
2225 new_rq->rq_pages = pages;
2226 new_rq->rq_npages = old_rq->rq_npages;
2227 new_rq->rq_pagesz = old_rq->rq_pagesz;
2228 new_rq->rq_tailsz = old_rq->rq_tailsz;
2229
2230 for (i = 0; i < npages; i++) {
2231 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2232 if (!pages[i])
2233 goto err_free_pages;
2234 }
2235
2236 iov = kmalloc_array(old_rq->rq_nvec, sizeof(struct kvec), GFP_KERNEL);
2237 if (!iov)
2238 goto err_free_pages;
2239
2240 /* copy all iovs from the old except the 1st one (rfc1002 length) */
2241 memcpy(&iov[1], &old_rq->rq_iov[1],
2242 sizeof(struct kvec) * (old_rq->rq_nvec - 1));
2243 new_rq->rq_iov = iov;
2244 new_rq->rq_nvec = old_rq->rq_nvec;
2245
2246 tr_hdr = kmalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL);
2247 if (!tr_hdr)
2248 goto err_free_iov;
2249
2250 /* fill the 1st iov with a transform header */
2251 fill_transform_hdr(tr_hdr, old_rq);
2252 new_rq->rq_iov[0].iov_base = tr_hdr;
2253 new_rq->rq_iov[0].iov_len = sizeof(struct smb2_transform_hdr);
2254
2255 /* copy pages form the old */
2256 for (i = 0; i < npages; i++) {
2257 char *dst = kmap(new_rq->rq_pages[i]);
2258 char *src = kmap(old_rq->rq_pages[i]);
2259 unsigned int len = (i < npages - 1) ? new_rq->rq_pagesz :
2260 new_rq->rq_tailsz;
2261 memcpy(dst, src, len);
2262 kunmap(new_rq->rq_pages[i]);
2263 kunmap(old_rq->rq_pages[i]);
2264 }
2265
2266 rc = crypt_message(server, new_rq, 1);
2267 cifs_dbg(FYI, "encrypt message returned %d", rc);
2268 if (rc)
2269 goto err_free_tr_hdr;
2270
2271 return rc;
2272
2273err_free_tr_hdr:
2274 kfree(tr_hdr);
2275err_free_iov:
2276 kfree(iov);
2277err_free_pages:
2278 for (i = i - 1; i >= 0; i--)
2279 put_page(pages[i]);
2280 kfree(pages);
2281 return rc;
2282}
2283
2284static void
2285smb3_free_transform_rq(struct smb_rqst *rqst)
2286{
2287 int i = rqst->rq_npages - 1;
2288
2289 for (; i >= 0; i--)
2290 put_page(rqst->rq_pages[i]);
2291 kfree(rqst->rq_pages);
2292 /* free transform header */
2293 kfree(rqst->rq_iov[0].iov_base);
2294 kfree(rqst->rq_iov);
2295}
2296
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002297static int
2298smb3_is_transform_hdr(void *buf)
2299{
2300 struct smb2_transform_hdr *trhdr = buf;
2301
2302 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
2303}
2304
2305static int
2306decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
2307 unsigned int buf_data_size, struct page **pages,
2308 unsigned int npages, unsigned int page_data_size)
2309{
2310 struct kvec iov[2];
2311 struct smb_rqst rqst = {NULL};
2312 struct smb2_hdr *hdr;
2313 int rc;
2314
2315 iov[0].iov_base = buf;
2316 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
2317 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
2318 iov[1].iov_len = buf_data_size;
2319
2320 rqst.rq_iov = iov;
2321 rqst.rq_nvec = 2;
2322 rqst.rq_pages = pages;
2323 rqst.rq_npages = npages;
2324 rqst.rq_pagesz = PAGE_SIZE;
2325 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
2326
2327 rc = crypt_message(server, &rqst, 0);
2328 cifs_dbg(FYI, "decrypt message returned %d\n", rc);
2329
2330 if (rc)
2331 return rc;
2332
2333 memmove(buf + 4, iov[1].iov_base, buf_data_size);
2334 hdr = (struct smb2_hdr *)buf;
2335 hdr->smb2_buf_length = cpu_to_be32(buf_data_size + page_data_size);
2336 server->total_read = buf_data_size + page_data_size + 4;
2337
2338 return rc;
2339}
2340
2341static int
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002342read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
2343 unsigned int npages, unsigned int len)
2344{
2345 int i;
2346 int length;
2347
2348 for (i = 0; i < npages; i++) {
2349 struct page *page = pages[i];
2350 size_t n;
2351
2352 n = len;
2353 if (len >= PAGE_SIZE) {
2354 /* enough data to fill the page */
2355 n = PAGE_SIZE;
2356 len -= n;
2357 } else {
2358 zero_user(page, len, PAGE_SIZE - len);
2359 len = 0;
2360 }
2361 length = cifs_read_page_from_socket(server, page, n);
2362 if (length < 0)
2363 return length;
2364 server->total_read += length;
2365 }
2366
2367 return 0;
2368}
2369
2370static int
2371init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
2372 unsigned int cur_off, struct bio_vec **page_vec)
2373{
2374 struct bio_vec *bvec;
2375 int i;
2376
2377 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
2378 if (!bvec)
2379 return -ENOMEM;
2380
2381 for (i = 0; i < npages; i++) {
2382 bvec[i].bv_page = pages[i];
2383 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
2384 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
2385 data_size -= bvec[i].bv_len;
2386 }
2387
2388 if (data_size != 0) {
2389 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
2390 kfree(bvec);
2391 return -EIO;
2392 }
2393
2394 *page_vec = bvec;
2395 return 0;
2396}
2397
2398static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002399handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
2400 char *buf, unsigned int buf_len, struct page **pages,
2401 unsigned int npages, unsigned int page_data_size)
2402{
2403 unsigned int data_offset;
2404 unsigned int data_len;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002405 unsigned int cur_off;
2406 unsigned int cur_page_idx;
2407 unsigned int pad_len;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002408 struct cifs_readdata *rdata = mid->callback_data;
2409 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
2410 struct bio_vec *bvec = NULL;
2411 struct iov_iter iter;
2412 struct kvec iov;
2413 int length;
2414
2415 if (shdr->Command != SMB2_READ) {
2416 cifs_dbg(VFS, "only big read responses are supported\n");
2417 return -ENOTSUPP;
2418 }
2419
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002420 if (server->ops->is_session_expired &&
2421 server->ops->is_session_expired(buf)) {
2422 cifs_reconnect(server);
2423 wake_up(&server->response_q);
2424 return -1;
2425 }
2426
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002427 if (server->ops->is_status_pending &&
2428 server->ops->is_status_pending(buf, server, 0))
2429 return -1;
2430
2431 rdata->result = server->ops->map_error(buf, false);
2432 if (rdata->result != 0) {
2433 cifs_dbg(FYI, "%s: server returned error %d\n",
2434 __func__, rdata->result);
2435 dequeue_mid(mid, rdata->result);
2436 return 0;
2437 }
2438
2439 data_offset = server->ops->read_data_offset(buf) + 4;
2440 data_len = server->ops->read_data_length(buf);
2441
2442 if (data_offset < server->vals->read_rsp_size) {
2443 /*
2444 * win2k8 sometimes sends an offset of 0 when the read
2445 * is beyond the EOF. Treat it as if the data starts just after
2446 * the header.
2447 */
2448 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
2449 __func__, data_offset);
2450 data_offset = server->vals->read_rsp_size;
2451 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
2452 /* data_offset is beyond the end of smallbuf */
2453 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
2454 __func__, data_offset);
2455 rdata->result = -EIO;
2456 dequeue_mid(mid, rdata->result);
2457 return 0;
2458 }
2459
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002460 pad_len = data_offset - server->vals->read_rsp_size;
2461
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002462 if (buf_len <= data_offset) {
2463 /* read response payload is in pages */
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002464 cur_page_idx = pad_len / PAGE_SIZE;
2465 cur_off = pad_len % PAGE_SIZE;
2466
2467 if (cur_page_idx != 0) {
2468 /* data offset is beyond the 1st page of response */
2469 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
2470 __func__, data_offset);
2471 rdata->result = -EIO;
2472 dequeue_mid(mid, rdata->result);
2473 return 0;
2474 }
2475
2476 if (data_len > page_data_size - pad_len) {
2477 /* data_len is corrupt -- discard frame */
2478 rdata->result = -EIO;
2479 dequeue_mid(mid, rdata->result);
2480 return 0;
2481 }
2482
2483 rdata->result = init_read_bvec(pages, npages, page_data_size,
2484 cur_off, &bvec);
2485 if (rdata->result != 0) {
2486 dequeue_mid(mid, rdata->result);
2487 return 0;
2488 }
2489
2490 iov_iter_bvec(&iter, WRITE | ITER_BVEC, bvec, npages, data_len);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002491 } else if (buf_len >= data_offset + data_len) {
2492 /* read response payload is in buf */
2493 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
2494 iov.iov_base = buf + data_offset;
2495 iov.iov_len = data_len;
2496 iov_iter_kvec(&iter, WRITE | ITER_KVEC, &iov, 1, data_len);
2497 } else {
2498 /* read response payload cannot be in both buf and pages */
2499 WARN_ONCE(1, "buf can not contain only a part of read data");
2500 rdata->result = -EIO;
2501 dequeue_mid(mid, rdata->result);
2502 return 0;
2503 }
2504
2505 /* set up first iov for signature check */
2506 rdata->iov[0].iov_base = buf;
2507 rdata->iov[0].iov_len = 4;
2508 rdata->iov[1].iov_base = buf + 4;
2509 rdata->iov[1].iov_len = server->vals->read_rsp_size - 4;
2510 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
2511 rdata->iov[0].iov_base, server->vals->read_rsp_size);
2512
2513 length = rdata->copy_into_pages(server, rdata, &iter);
2514
2515 kfree(bvec);
2516
2517 if (length < 0)
2518 return length;
2519
2520 dequeue_mid(mid, false);
2521 return length;
2522}
2523
2524static int
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002525receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2526{
2527 char *buf = server->smallbuf;
2528 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2529 unsigned int npages;
2530 struct page **pages;
2531 unsigned int len;
2532 unsigned int buflen = get_rfc1002_length(buf) + 4;
2533 int rc;
2534 int i = 0;
2535
2536 len = min_t(unsigned int, buflen, server->vals->read_rsp_size - 4 +
2537 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
2538
2539 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
2540 if (rc < 0)
2541 return rc;
2542 server->total_read += rc;
2543
2544 len = le32_to_cpu(tr_hdr->OriginalMessageSize) + 4 -
2545 server->vals->read_rsp_size;
2546 npages = DIV_ROUND_UP(len, PAGE_SIZE);
2547
2548 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2549 if (!pages) {
2550 rc = -ENOMEM;
2551 goto discard_data;
2552 }
2553
2554 for (; i < npages; i++) {
2555 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2556 if (!pages[i]) {
2557 rc = -ENOMEM;
2558 goto discard_data;
2559 }
2560 }
2561
2562 /* read read data into pages */
2563 rc = read_data_into_pages(server, pages, npages, len);
2564 if (rc)
2565 goto free_pages;
2566
Pavel Shilovsky350be252017-04-10 10:31:33 -07002567 rc = cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002568 if (rc)
2569 goto free_pages;
2570
2571 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size - 4,
2572 pages, npages, len);
2573 if (rc)
2574 goto free_pages;
2575
2576 *mid = smb2_find_mid(server, buf);
2577 if (*mid == NULL)
2578 cifs_dbg(FYI, "mid not found\n");
2579 else {
2580 cifs_dbg(FYI, "mid found\n");
2581 (*mid)->decrypted = true;
2582 rc = handle_read_data(server, *mid, buf,
2583 server->vals->read_rsp_size,
2584 pages, npages, len);
2585 }
2586
2587free_pages:
2588 for (i = i - 1; i >= 0; i--)
2589 put_page(pages[i]);
2590 kfree(pages);
2591 return rc;
2592discard_data:
Pavel Shilovsky350be252017-04-10 10:31:33 -07002593 cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002594 goto free_pages;
2595}
2596
2597static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002598receive_encrypted_standard(struct TCP_Server_Info *server,
2599 struct mid_q_entry **mid)
2600{
2601 int length;
2602 char *buf = server->smallbuf;
2603 unsigned int pdu_length = get_rfc1002_length(buf);
2604 unsigned int buf_size;
2605 struct mid_q_entry *mid_entry;
2606
2607 /* switch to large buffer if too big for a small one */
2608 if (pdu_length + 4 > MAX_CIFS_SMALL_BUFFER_SIZE) {
2609 server->large_buf = true;
2610 memcpy(server->bigbuf, buf, server->total_read);
2611 buf = server->bigbuf;
2612 }
2613
2614 /* now read the rest */
2615 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
2616 pdu_length - HEADER_SIZE(server) + 1 + 4);
2617 if (length < 0)
2618 return length;
2619 server->total_read += length;
2620
2621 buf_size = pdu_length + 4 - sizeof(struct smb2_transform_hdr);
2622 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
2623 if (length)
2624 return length;
2625
2626 mid_entry = smb2_find_mid(server, buf);
2627 if (mid_entry == NULL)
2628 cifs_dbg(FYI, "mid not found\n");
2629 else {
2630 cifs_dbg(FYI, "mid found\n");
2631 mid_entry->decrypted = true;
2632 }
2633
2634 *mid = mid_entry;
2635
2636 if (mid_entry && mid_entry->handle)
2637 return mid_entry->handle(server, mid_entry);
2638
2639 return cifs_handle_standard(server, mid_entry);
2640}
2641
2642static int
2643smb3_receive_transform(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2644{
2645 char *buf = server->smallbuf;
2646 unsigned int pdu_length = get_rfc1002_length(buf);
2647 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2648 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2649
2650 if (pdu_length + 4 < sizeof(struct smb2_transform_hdr) +
2651 sizeof(struct smb2_sync_hdr)) {
2652 cifs_dbg(VFS, "Transform message is too small (%u)\n",
2653 pdu_length);
2654 cifs_reconnect(server);
2655 wake_up(&server->response_q);
2656 return -ECONNABORTED;
2657 }
2658
2659 if (pdu_length + 4 < orig_len + sizeof(struct smb2_transform_hdr)) {
2660 cifs_dbg(VFS, "Transform message is broken\n");
2661 cifs_reconnect(server);
2662 wake_up(&server->response_q);
2663 return -ECONNABORTED;
2664 }
2665
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002666 if (pdu_length + 4 > CIFSMaxBufSize + MAX_HEADER_SIZE(server))
2667 return receive_encrypted_read(server, mid);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002668
2669 return receive_encrypted_standard(server, mid);
2670}
2671
2672int
2673smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
2674{
2675 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
2676
2677 return handle_read_data(server, mid, buf, get_rfc1002_length(buf) + 4,
2678 NULL, 0, 0);
2679}
2680
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002681struct smb_version_operations smb20_operations = {
2682 .compare_fids = smb2_compare_fids,
2683 .setup_request = smb2_setup_request,
2684 .setup_async_request = smb2_setup_async_request,
2685 .check_receive = smb2_check_receive,
2686 .add_credits = smb2_add_credits,
2687 .set_credits = smb2_set_credits,
2688 .get_credits_field = smb2_get_credits_field,
2689 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002690 .wait_mtu_credits = cifs_wait_mtu_credits,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002691 .get_next_mid = smb2_get_next_mid,
2692 .read_data_offset = smb2_read_data_offset,
2693 .read_data_length = smb2_read_data_length,
2694 .map_error = map_smb2_to_linux_error,
2695 .find_mid = smb2_find_mid,
2696 .check_message = smb2_check_message,
2697 .dump_detail = smb2_dump_detail,
2698 .clear_stats = smb2_clear_stats,
2699 .print_stats = smb2_print_stats,
2700 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08002701 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002702 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002703 .need_neg = smb2_need_neg,
2704 .negotiate = smb2_negotiate,
2705 .negotiate_wsize = smb2_negotiate_wsize,
2706 .negotiate_rsize = smb2_negotiate_rsize,
2707 .sess_setup = SMB2_sess_setup,
2708 .logoff = SMB2_logoff,
2709 .tree_connect = SMB2_tcon,
2710 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05002711 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002712 .is_path_accessible = smb2_is_path_accessible,
2713 .can_echo = smb2_can_echo,
2714 .echo = SMB2_echo,
2715 .query_path_info = smb2_query_path_info,
2716 .get_srv_inum = smb2_get_srv_inum,
2717 .query_file_info = smb2_query_file_info,
2718 .set_path_size = smb2_set_path_size,
2719 .set_file_size = smb2_set_file_size,
2720 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05002721 .set_compression = smb2_set_compression,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002722 .mkdir = smb2_mkdir,
2723 .mkdir_setinfo = smb2_mkdir_setinfo,
2724 .rmdir = smb2_rmdir,
2725 .unlink = smb2_unlink,
2726 .rename = smb2_rename_path,
2727 .create_hardlink = smb2_create_hardlink,
2728 .query_symlink = smb2_query_symlink,
Sachin Prabhu5b23c972016-07-11 16:53:20 +01002729 .query_mf_symlink = smb3_query_mf_symlink,
2730 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002731 .open = smb2_open_file,
2732 .set_fid = smb2_set_fid,
2733 .close = smb2_close_file,
2734 .flush = smb2_flush_file,
2735 .async_readv = smb2_async_readv,
2736 .async_writev = smb2_async_writev,
2737 .sync_read = smb2_sync_read,
2738 .sync_write = smb2_sync_write,
2739 .query_dir_first = smb2_query_dir_first,
2740 .query_dir_next = smb2_query_dir_next,
2741 .close_dir = smb2_close_dir,
2742 .calc_smb_size = smb2_calc_size,
2743 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002744 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002745 .oplock_response = smb2_oplock_response,
2746 .queryfs = smb2_queryfs,
2747 .mand_lock = smb2_mand_lock,
2748 .mand_unlock_range = smb2_unlock_range,
2749 .push_mand_locks = smb2_push_mandatory_locks,
2750 .get_lease_key = smb2_get_lease_key,
2751 .set_lease_key = smb2_set_lease_key,
2752 .new_lease_key = smb2_new_lease_key,
2753 .calc_signature = smb2_calc_signature,
2754 .is_read_op = smb2_is_read_op,
2755 .set_oplock_level = smb2_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002756 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002757 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05002758 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002759 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04002760 .dir_needs_close = smb2_dir_needs_close,
Aurelien Aptel9d496402017-02-13 16:16:49 +01002761 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302762 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002763#ifdef CONFIG_CIFS_XATTR
2764 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10002765 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002766#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002767#ifdef CONFIG_CIFS_ACL
2768 .get_acl = get_smb2_acl,
2769 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05002770 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002771#endif /* CIFS_ACL */
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002772};
2773
Steve French1080ef72011-02-24 18:07:19 +00002774struct smb_version_operations smb21_operations = {
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07002775 .compare_fids = smb2_compare_fids,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002776 .setup_request = smb2_setup_request,
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +04002777 .setup_async_request = smb2_setup_async_request,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002778 .check_receive = smb2_check_receive,
Pavel Shilovsky28ea5292012-05-23 16:18:00 +04002779 .add_credits = smb2_add_credits,
2780 .set_credits = smb2_set_credits,
2781 .get_credits_field = smb2_get_credits_field,
2782 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002783 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002784 .get_next_mid = smb2_get_next_mid,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002785 .read_data_offset = smb2_read_data_offset,
2786 .read_data_length = smb2_read_data_length,
2787 .map_error = map_smb2_to_linux_error,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04002788 .find_mid = smb2_find_mid,
2789 .check_message = smb2_check_message,
2790 .dump_detail = smb2_dump_detail,
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04002791 .clear_stats = smb2_clear_stats,
2792 .print_stats = smb2_print_stats,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002793 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08002794 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002795 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04002796 .need_neg = smb2_need_neg,
2797 .negotiate = smb2_negotiate,
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -07002798 .negotiate_wsize = smb2_negotiate_wsize,
2799 .negotiate_rsize = smb2_negotiate_rsize,
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04002800 .sess_setup = SMB2_sess_setup,
2801 .logoff = SMB2_logoff,
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04002802 .tree_connect = SMB2_tcon,
2803 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05002804 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002805 .is_path_accessible = smb2_is_path_accessible,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002806 .can_echo = smb2_can_echo,
2807 .echo = SMB2_echo,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002808 .query_path_info = smb2_query_path_info,
2809 .get_srv_inum = smb2_get_srv_inum,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -07002810 .query_file_info = smb2_query_file_info,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002811 .set_path_size = smb2_set_path_size,
2812 .set_file_size = smb2_set_file_size,
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07002813 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05002814 .set_compression = smb2_set_compression,
Pavel Shilovskya0e73182011-07-19 12:56:37 +04002815 .mkdir = smb2_mkdir,
2816 .mkdir_setinfo = smb2_mkdir_setinfo,
Pavel Shilovsky1a500f02012-07-10 16:14:38 +04002817 .rmdir = smb2_rmdir,
Pavel Shilovskycbe6f432012-09-18 16:20:25 -07002818 .unlink = smb2_unlink,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002819 .rename = smb2_rename_path,
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002820 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002821 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05002822 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05002823 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002824 .open = smb2_open_file,
2825 .set_fid = smb2_set_fid,
2826 .close = smb2_close_file,
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002827 .flush = smb2_flush_file,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002828 .async_readv = smb2_async_readv,
Pavel Shilovsky33319142012-09-18 16:20:29 -07002829 .async_writev = smb2_async_writev,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002830 .sync_read = smb2_sync_read,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002831 .sync_write = smb2_sync_write,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002832 .query_dir_first = smb2_query_dir_first,
2833 .query_dir_next = smb2_query_dir_next,
2834 .close_dir = smb2_close_dir,
2835 .calc_smb_size = smb2_calc_size,
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002836 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002837 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002838 .oplock_response = smb2_oplock_response,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002839 .queryfs = smb2_queryfs,
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07002840 .mand_lock = smb2_mand_lock,
2841 .mand_unlock_range = smb2_unlock_range,
Pavel Shilovskyb1407992012-09-19 06:22:44 -07002842 .push_mand_locks = smb2_push_mandatory_locks,
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002843 .get_lease_key = smb2_get_lease_key,
2844 .set_lease_key = smb2_set_lease_key,
2845 .new_lease_key = smb2_new_lease_key,
Steve French38107d42012-12-08 22:08:06 -06002846 .calc_signature = smb2_calc_signature,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002847 .is_read_op = smb21_is_read_op,
2848 .set_oplock_level = smb21_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002849 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002850 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05002851 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002852 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04002853 .dir_needs_close = smb2_dir_needs_close,
Steve French834170c2016-09-30 21:14:26 -05002854 .enum_snapshots = smb3_enum_snapshots,
Aurelien Aptel9d496402017-02-13 16:16:49 +01002855 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302856 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002857#ifdef CONFIG_CIFS_XATTR
2858 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10002859 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002860#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002861#ifdef CONFIG_CIFS_ACL
2862 .get_acl = get_smb2_acl,
2863 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05002864 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002865#endif /* CIFS_ACL */
Steve French38107d42012-12-08 22:08:06 -06002866};
2867
Steve French38107d42012-12-08 22:08:06 -06002868struct smb_version_operations smb30_operations = {
2869 .compare_fids = smb2_compare_fids,
2870 .setup_request = smb2_setup_request,
2871 .setup_async_request = smb2_setup_async_request,
2872 .check_receive = smb2_check_receive,
2873 .add_credits = smb2_add_credits,
2874 .set_credits = smb2_set_credits,
2875 .get_credits_field = smb2_get_credits_field,
2876 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002877 .wait_mtu_credits = smb2_wait_mtu_credits,
Steve French38107d42012-12-08 22:08:06 -06002878 .get_next_mid = smb2_get_next_mid,
2879 .read_data_offset = smb2_read_data_offset,
2880 .read_data_length = smb2_read_data_length,
2881 .map_error = map_smb2_to_linux_error,
2882 .find_mid = smb2_find_mid,
2883 .check_message = smb2_check_message,
2884 .dump_detail = smb2_dump_detail,
2885 .clear_stats = smb2_clear_stats,
2886 .print_stats = smb2_print_stats,
Steve French769ee6a2013-06-19 14:15:30 -05002887 .dump_share_caps = smb2_dump_share_caps,
Steve French38107d42012-12-08 22:08:06 -06002888 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08002889 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002890 .downgrade_oplock = smb2_downgrade_oplock,
Steve French38107d42012-12-08 22:08:06 -06002891 .need_neg = smb2_need_neg,
2892 .negotiate = smb2_negotiate,
2893 .negotiate_wsize = smb2_negotiate_wsize,
2894 .negotiate_rsize = smb2_negotiate_rsize,
2895 .sess_setup = SMB2_sess_setup,
2896 .logoff = SMB2_logoff,
2897 .tree_connect = SMB2_tcon,
2898 .tree_disconnect = SMB2_tdis,
Steven Frenchaf6a12e2013-10-09 20:55:53 -05002899 .qfs_tcon = smb3_qfs_tcon,
Steve French38107d42012-12-08 22:08:06 -06002900 .is_path_accessible = smb2_is_path_accessible,
2901 .can_echo = smb2_can_echo,
2902 .echo = SMB2_echo,
2903 .query_path_info = smb2_query_path_info,
2904 .get_srv_inum = smb2_get_srv_inum,
2905 .query_file_info = smb2_query_file_info,
2906 .set_path_size = smb2_set_path_size,
2907 .set_file_size = smb2_set_file_size,
2908 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05002909 .set_compression = smb2_set_compression,
Steve French38107d42012-12-08 22:08:06 -06002910 .mkdir = smb2_mkdir,
2911 .mkdir_setinfo = smb2_mkdir_setinfo,
2912 .rmdir = smb2_rmdir,
2913 .unlink = smb2_unlink,
2914 .rename = smb2_rename_path,
2915 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002916 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05002917 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05002918 .create_mf_symlink = smb3_create_mf_symlink,
Steve French38107d42012-12-08 22:08:06 -06002919 .open = smb2_open_file,
2920 .set_fid = smb2_set_fid,
2921 .close = smb2_close_file,
2922 .flush = smb2_flush_file,
2923 .async_readv = smb2_async_readv,
2924 .async_writev = smb2_async_writev,
2925 .sync_read = smb2_sync_read,
2926 .sync_write = smb2_sync_write,
2927 .query_dir_first = smb2_query_dir_first,
2928 .query_dir_next = smb2_query_dir_next,
2929 .close_dir = smb2_close_dir,
2930 .calc_smb_size = smb2_calc_size,
2931 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002932 .is_session_expired = smb2_is_session_expired,
Steve French38107d42012-12-08 22:08:06 -06002933 .oplock_response = smb2_oplock_response,
2934 .queryfs = smb2_queryfs,
2935 .mand_lock = smb2_mand_lock,
2936 .mand_unlock_range = smb2_unlock_range,
2937 .push_mand_locks = smb2_push_mandatory_locks,
2938 .get_lease_key = smb2_get_lease_key,
2939 .set_lease_key = smb2_set_lease_key,
2940 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06002941 .generate_signingkey = generate_smb30signingkey,
Steve French38107d42012-12-08 22:08:06 -06002942 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05002943 .set_integrity = smb3_set_integrity,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002944 .is_read_op = smb21_is_read_op,
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002945 .set_oplock_level = smb3_set_oplock_level,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002946 .create_lease_buf = smb3_create_lease_buf,
2947 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05002948 .copychunk_range = smb2_copychunk_range,
Steve Frenchca9e7a12015-10-01 21:40:10 -05002949 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchff1c0382013-11-19 23:44:46 -06002950 .validate_negotiate = smb3_validate_negotiate,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002951 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04002952 .dir_needs_close = smb2_dir_needs_close,
Steve French31742c52014-08-17 08:38:47 -05002953 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05002954 .enum_snapshots = smb3_enum_snapshots,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002955 .init_transform_rq = smb3_init_transform_rq,
2956 .free_transform_rq = smb3_free_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002957 .is_transform_hdr = smb3_is_transform_hdr,
2958 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01002959 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302960 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002961#ifdef CONFIG_CIFS_XATTR
2962 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10002963 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002964#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002965#ifdef CONFIG_CIFS_ACL
2966 .get_acl = get_smb2_acl,
2967 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05002968 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002969#endif /* CIFS_ACL */
Steve French1080ef72011-02-24 18:07:19 +00002970};
2971
Steve Frenchaab18932015-06-23 23:37:11 -05002972#ifdef CONFIG_CIFS_SMB311
2973struct smb_version_operations smb311_operations = {
2974 .compare_fids = smb2_compare_fids,
2975 .setup_request = smb2_setup_request,
2976 .setup_async_request = smb2_setup_async_request,
2977 .check_receive = smb2_check_receive,
2978 .add_credits = smb2_add_credits,
2979 .set_credits = smb2_set_credits,
2980 .get_credits_field = smb2_get_credits_field,
2981 .get_credits = smb2_get_credits,
2982 .wait_mtu_credits = smb2_wait_mtu_credits,
2983 .get_next_mid = smb2_get_next_mid,
2984 .read_data_offset = smb2_read_data_offset,
2985 .read_data_length = smb2_read_data_length,
2986 .map_error = map_smb2_to_linux_error,
2987 .find_mid = smb2_find_mid,
2988 .check_message = smb2_check_message,
2989 .dump_detail = smb2_dump_detail,
2990 .clear_stats = smb2_clear_stats,
2991 .print_stats = smb2_print_stats,
2992 .dump_share_caps = smb2_dump_share_caps,
2993 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08002994 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Steve Frenchaab18932015-06-23 23:37:11 -05002995 .downgrade_oplock = smb2_downgrade_oplock,
2996 .need_neg = smb2_need_neg,
2997 .negotiate = smb2_negotiate,
2998 .negotiate_wsize = smb2_negotiate_wsize,
2999 .negotiate_rsize = smb2_negotiate_rsize,
3000 .sess_setup = SMB2_sess_setup,
3001 .logoff = SMB2_logoff,
3002 .tree_connect = SMB2_tcon,
3003 .tree_disconnect = SMB2_tdis,
3004 .qfs_tcon = smb3_qfs_tcon,
3005 .is_path_accessible = smb2_is_path_accessible,
3006 .can_echo = smb2_can_echo,
3007 .echo = SMB2_echo,
3008 .query_path_info = smb2_query_path_info,
3009 .get_srv_inum = smb2_get_srv_inum,
3010 .query_file_info = smb2_query_file_info,
3011 .set_path_size = smb2_set_path_size,
3012 .set_file_size = smb2_set_file_size,
3013 .set_file_info = smb2_set_file_info,
3014 .set_compression = smb2_set_compression,
3015 .mkdir = smb2_mkdir,
3016 .mkdir_setinfo = smb2_mkdir_setinfo,
3017 .rmdir = smb2_rmdir,
3018 .unlink = smb2_unlink,
3019 .rename = smb2_rename_path,
3020 .create_hardlink = smb2_create_hardlink,
3021 .query_symlink = smb2_query_symlink,
3022 .query_mf_symlink = smb3_query_mf_symlink,
3023 .create_mf_symlink = smb3_create_mf_symlink,
3024 .open = smb2_open_file,
3025 .set_fid = smb2_set_fid,
3026 .close = smb2_close_file,
3027 .flush = smb2_flush_file,
3028 .async_readv = smb2_async_readv,
3029 .async_writev = smb2_async_writev,
3030 .sync_read = smb2_sync_read,
3031 .sync_write = smb2_sync_write,
3032 .query_dir_first = smb2_query_dir_first,
3033 .query_dir_next = smb2_query_dir_next,
3034 .close_dir = smb2_close_dir,
3035 .calc_smb_size = smb2_calc_size,
3036 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07003037 .is_session_expired = smb2_is_session_expired,
Steve Frenchaab18932015-06-23 23:37:11 -05003038 .oplock_response = smb2_oplock_response,
3039 .queryfs = smb2_queryfs,
3040 .mand_lock = smb2_mand_lock,
3041 .mand_unlock_range = smb2_unlock_range,
3042 .push_mand_locks = smb2_push_mandatory_locks,
3043 .get_lease_key = smb2_get_lease_key,
3044 .set_lease_key = smb2_set_lease_key,
3045 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06003046 .generate_signingkey = generate_smb311signingkey,
Steve Frenchaab18932015-06-23 23:37:11 -05003047 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05003048 .set_integrity = smb3_set_integrity,
Steve Frenchaab18932015-06-23 23:37:11 -05003049 .is_read_op = smb21_is_read_op,
3050 .set_oplock_level = smb3_set_oplock_level,
3051 .create_lease_buf = smb3_create_lease_buf,
3052 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05003053 .copychunk_range = smb2_copychunk_range,
Steve French02b16662015-06-27 21:18:36 -07003054 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchaab18932015-06-23 23:37:11 -05003055/* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
3056 .wp_retry_size = smb2_wp_retry_size,
3057 .dir_needs_close = smb2_dir_needs_close,
3058 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05003059 .enum_snapshots = smb3_enum_snapshots,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003060 .init_transform_rq = smb3_init_transform_rq,
3061 .free_transform_rq = smb3_free_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003062 .is_transform_hdr = smb3_is_transform_hdr,
3063 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01003064 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05303065 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003066#ifdef CONFIG_CIFS_XATTR
3067 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003068 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003069#endif /* CIFS_XATTR */
Steve Frenchaab18932015-06-23 23:37:11 -05003070};
3071#endif /* CIFS_SMB311 */
3072
Steve Frenchdd446b12012-11-28 23:21:06 -06003073struct smb_version_values smb20_values = {
3074 .version_string = SMB20_VERSION_STRING,
3075 .protocol_id = SMB20_PROT_ID,
3076 .req_capabilities = 0, /* MBZ */
3077 .large_lock_type = 0,
3078 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3079 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3080 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3081 .header_size = sizeof(struct smb2_hdr),
3082 .max_header_size = MAX_SMB2_HDR_SIZE,
3083 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3084 .lock_cmd = SMB2_LOCK,
3085 .cap_unix = 0,
3086 .cap_nt_find = SMB2_NT_FIND,
3087 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003088 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3089 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003090 .create_lease_size = sizeof(struct create_lease),
Steve Frenchdd446b12012-11-28 23:21:06 -06003091};
3092
Steve French1080ef72011-02-24 18:07:19 +00003093struct smb_version_values smb21_values = {
3094 .version_string = SMB21_VERSION_STRING,
Steve Frenche4aa25e2012-10-01 12:26:22 -05003095 .protocol_id = SMB21_PROT_ID,
3096 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
3097 .large_lock_type = 0,
3098 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3099 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3100 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3101 .header_size = sizeof(struct smb2_hdr),
3102 .max_header_size = MAX_SMB2_HDR_SIZE,
3103 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3104 .lock_cmd = SMB2_LOCK,
3105 .cap_unix = 0,
3106 .cap_nt_find = SMB2_NT_FIND,
3107 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003108 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3109 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003110 .create_lease_size = sizeof(struct create_lease),
Steve Frenche4aa25e2012-10-01 12:26:22 -05003111};
3112
3113struct smb_version_values smb30_values = {
3114 .version_string = SMB30_VERSION_STRING,
3115 .protocol_id = SMB30_PROT_ID,
Steve French373512e2015-12-18 13:05:30 -06003116 .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 -07003117 .large_lock_type = 0,
3118 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3119 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3120 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04003121 .header_size = sizeof(struct smb2_hdr),
3122 .max_header_size = MAX_SMB2_HDR_SIZE,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003123 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04003124 .lock_cmd = SMB2_LOCK,
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04003125 .cap_unix = 0,
3126 .cap_nt_find = SMB2_NT_FIND,
3127 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003128 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3129 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003130 .create_lease_size = sizeof(struct create_lease_v2),
Steve French1080ef72011-02-24 18:07:19 +00003131};
Steve French20b6d8b2013-06-12 22:48:41 -05003132
3133struct smb_version_values smb302_values = {
3134 .version_string = SMB302_VERSION_STRING,
3135 .protocol_id = SMB302_PROT_ID,
Steve French373512e2015-12-18 13:05:30 -06003136 .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 -05003137 .large_lock_type = 0,
3138 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3139 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3140 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3141 .header_size = sizeof(struct smb2_hdr),
3142 .max_header_size = MAX_SMB2_HDR_SIZE,
3143 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3144 .lock_cmd = SMB2_LOCK,
3145 .cap_unix = 0,
3146 .cap_nt_find = SMB2_NT_FIND,
3147 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003148 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3149 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003150 .create_lease_size = sizeof(struct create_lease_v2),
Steve French20b6d8b2013-06-12 22:48:41 -05003151};
Steve French5f7fbf72014-12-17 22:52:58 -06003152
3153#ifdef CONFIG_CIFS_SMB311
3154struct smb_version_values smb311_values = {
3155 .version_string = SMB311_VERSION_STRING,
3156 .protocol_id = SMB311_PROT_ID,
Steve French19558802017-06-21 16:50:20 -05003157 .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 -06003158 .large_lock_type = 0,
3159 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3160 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3161 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3162 .header_size = sizeof(struct smb2_hdr),
3163 .max_header_size = MAX_SMB2_HDR_SIZE,
3164 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3165 .lock_cmd = SMB2_LOCK,
3166 .cap_unix = 0,
3167 .cap_nt_find = SMB2_NT_FIND,
3168 .cap_large_files = SMB2_LARGE_FILES,
3169 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3170 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3171 .create_lease_size = sizeof(struct create_lease_v2),
3172};
3173#endif /* SMB311 */