blob: 78516d3a133c4ab04b8f3c4b136849be787f5fe2 [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
Pavel Shilovsky9094fad2012-07-12 18:30:44 +0400561static bool
562smb2_can_echo(struct TCP_Server_Info *server)
563{
564 return server->echoes;
565}
566
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400567static void
568smb2_clear_stats(struct cifs_tcon *tcon)
569{
570#ifdef CONFIG_CIFS_STATS
571 int i;
572 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
573 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
574 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
575 }
576#endif
577}
578
579static void
Steve French769ee6a2013-06-19 14:15:30 -0500580smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
581{
582 seq_puts(m, "\n\tShare Capabilities:");
583 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
584 seq_puts(m, " DFS,");
585 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
586 seq_puts(m, " CONTINUOUS AVAILABILITY,");
587 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
588 seq_puts(m, " SCALEOUT,");
589 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
590 seq_puts(m, " CLUSTER,");
591 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
592 seq_puts(m, " ASYMMETRIC,");
593 if (tcon->capabilities == 0)
594 seq_puts(m, " None");
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500595 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
596 seq_puts(m, " Aligned,");
597 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
598 seq_puts(m, " Partition Aligned,");
599 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
600 seq_puts(m, " SSD,");
601 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
602 seq_puts(m, " TRIM-support,");
603
Steve French769ee6a2013-06-19 14:15:30 -0500604 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500605 if (tcon->perf_sector_size)
606 seq_printf(m, "\tOptimal sector size: 0x%x",
607 tcon->perf_sector_size);
Steve French769ee6a2013-06-19 14:15:30 -0500608}
609
610static void
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400611smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
612{
613#ifdef CONFIG_CIFS_STATS
614 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
615 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
616 seq_printf(m, "\nNegotiates: %d sent %d failed",
617 atomic_read(&sent[SMB2_NEGOTIATE_HE]),
618 atomic_read(&failed[SMB2_NEGOTIATE_HE]));
619 seq_printf(m, "\nSessionSetups: %d sent %d failed",
620 atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
621 atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400622 seq_printf(m, "\nLogoffs: %d sent %d failed",
623 atomic_read(&sent[SMB2_LOGOFF_HE]),
624 atomic_read(&failed[SMB2_LOGOFF_HE]));
625 seq_printf(m, "\nTreeConnects: %d sent %d failed",
626 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
627 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
628 seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
629 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
630 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
631 seq_printf(m, "\nCreates: %d sent %d failed",
632 atomic_read(&sent[SMB2_CREATE_HE]),
633 atomic_read(&failed[SMB2_CREATE_HE]));
634 seq_printf(m, "\nCloses: %d sent %d failed",
635 atomic_read(&sent[SMB2_CLOSE_HE]),
636 atomic_read(&failed[SMB2_CLOSE_HE]));
637 seq_printf(m, "\nFlushes: %d sent %d failed",
638 atomic_read(&sent[SMB2_FLUSH_HE]),
639 atomic_read(&failed[SMB2_FLUSH_HE]));
640 seq_printf(m, "\nReads: %d sent %d failed",
641 atomic_read(&sent[SMB2_READ_HE]),
642 atomic_read(&failed[SMB2_READ_HE]));
643 seq_printf(m, "\nWrites: %d sent %d failed",
644 atomic_read(&sent[SMB2_WRITE_HE]),
645 atomic_read(&failed[SMB2_WRITE_HE]));
646 seq_printf(m, "\nLocks: %d sent %d failed",
647 atomic_read(&sent[SMB2_LOCK_HE]),
648 atomic_read(&failed[SMB2_LOCK_HE]));
649 seq_printf(m, "\nIOCTLs: %d sent %d failed",
650 atomic_read(&sent[SMB2_IOCTL_HE]),
651 atomic_read(&failed[SMB2_IOCTL_HE]));
652 seq_printf(m, "\nCancels: %d sent %d failed",
653 atomic_read(&sent[SMB2_CANCEL_HE]),
654 atomic_read(&failed[SMB2_CANCEL_HE]));
655 seq_printf(m, "\nEchos: %d sent %d failed",
656 atomic_read(&sent[SMB2_ECHO_HE]),
657 atomic_read(&failed[SMB2_ECHO_HE]));
658 seq_printf(m, "\nQueryDirectories: %d sent %d failed",
659 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
660 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
661 seq_printf(m, "\nChangeNotifies: %d sent %d failed",
662 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
663 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
664 seq_printf(m, "\nQueryInfos: %d sent %d failed",
665 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
666 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
667 seq_printf(m, "\nSetInfos: %d sent %d failed",
668 atomic_read(&sent[SMB2_SET_INFO_HE]),
669 atomic_read(&failed[SMB2_SET_INFO_HE]));
670 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
671 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
672 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
673#endif
674}
675
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700676static void
677smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
678{
David Howells2b0143b2015-03-17 22:25:59 +0000679 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovsky53ef1012013-09-05 16:11:28 +0400680 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
681
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700682 cfile->fid.persistent_fid = fid->persistent_fid;
683 cfile->fid.volatile_fid = fid->volatile_fid;
Pavel Shilovsky42873b02013-09-05 21:30:16 +0400684 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
685 &fid->purge_cache);
Pavel Shilovsky18cceb62013-09-05 13:01:06 +0400686 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
Aurelien Aptel94f87372016-09-22 07:38:50 +0200687 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700688}
689
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400690static void
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700691smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
692 struct cifs_fid *fid)
693{
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400694 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700695}
696
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -0700697static int
Steve French41c13582013-11-14 00:05:36 -0600698SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
699 u64 persistent_fid, u64 volatile_fid,
700 struct copychunk_ioctl *pcchunk)
701{
702 int rc;
703 unsigned int ret_data_len;
704 struct resume_key_req *res_key;
705
706 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
707 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +0100708 false /* use_ipc */,
Steve French41c13582013-11-14 00:05:36 -0600709 NULL, 0 /* no input */,
710 (char **)&res_key, &ret_data_len);
711
712 if (rc) {
713 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
714 goto req_res_key_exit;
715 }
716 if (ret_data_len < sizeof(struct resume_key_req)) {
717 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
718 rc = -EINVAL;
719 goto req_res_key_exit;
720 }
721 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
722
723req_res_key_exit:
724 kfree(res_key);
725 return rc;
726}
727
Sachin Prabhu620d8742017-02-10 16:03:51 +0530728static ssize_t
Sachin Prabhu312bbc52017-04-04 02:12:04 -0500729smb2_copychunk_range(const unsigned int xid,
Steve French41c13582013-11-14 00:05:36 -0600730 struct cifsFileInfo *srcfile,
731 struct cifsFileInfo *trgtfile, u64 src_off,
732 u64 len, u64 dest_off)
733{
734 int rc;
735 unsigned int ret_data_len;
736 struct copychunk_ioctl *pcchunk;
Steve French9bf0c9c2013-11-16 18:05:28 -0600737 struct copychunk_ioctl_rsp *retbuf = NULL;
738 struct cifs_tcon *tcon;
739 int chunks_copied = 0;
740 bool chunk_sizes_updated = false;
Sachin Prabhu620d8742017-02-10 16:03:51 +0530741 ssize_t bytes_written, total_bytes_written = 0;
Steve French41c13582013-11-14 00:05:36 -0600742
743 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
744
745 if (pcchunk == NULL)
746 return -ENOMEM;
747
Sachin Prabhu312bbc52017-04-04 02:12:04 -0500748 cifs_dbg(FYI, "in smb2_copychunk_range - about to call request res key\n");
Steve French41c13582013-11-14 00:05:36 -0600749 /* Request a key from the server to identify the source of the copy */
750 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
751 srcfile->fid.persistent_fid,
752 srcfile->fid.volatile_fid, pcchunk);
753
754 /* Note: request_res_key sets res_key null only if rc !=0 */
755 if (rc)
Steve French9bf0c9c2013-11-16 18:05:28 -0600756 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -0600757
758 /* For now array only one chunk long, will make more flexible later */
Fabian Frederickbc09d142014-12-10 15:41:15 -0800759 pcchunk->ChunkCount = cpu_to_le32(1);
Steve French41c13582013-11-14 00:05:36 -0600760 pcchunk->Reserved = 0;
Steve French41c13582013-11-14 00:05:36 -0600761 pcchunk->Reserved2 = 0;
762
Steve French9bf0c9c2013-11-16 18:05:28 -0600763 tcon = tlink_tcon(trgtfile->tlink);
764
765 while (len > 0) {
766 pcchunk->SourceOffset = cpu_to_le64(src_off);
767 pcchunk->TargetOffset = cpu_to_le64(dest_off);
768 pcchunk->Length =
769 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
770
771 /* Request server copy to target from src identified by key */
772 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
Steve French41c13582013-11-14 00:05:36 -0600773 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
Aurelien Aptel51146622017-02-28 15:08:41 +0100774 true /* is_fsctl */, false /* use_ipc */,
775 (char *)pcchunk,
Steve French9bf0c9c2013-11-16 18:05:28 -0600776 sizeof(struct copychunk_ioctl), (char **)&retbuf,
777 &ret_data_len);
778 if (rc == 0) {
779 if (ret_data_len !=
780 sizeof(struct copychunk_ioctl_rsp)) {
781 cifs_dbg(VFS, "invalid cchunk response size\n");
782 rc = -EIO;
783 goto cchunk_out;
784 }
785 if (retbuf->TotalBytesWritten == 0) {
786 cifs_dbg(FYI, "no bytes copied\n");
787 rc = -EIO;
788 goto cchunk_out;
789 }
790 /*
791 * Check if server claimed to write more than we asked
792 */
793 if (le32_to_cpu(retbuf->TotalBytesWritten) >
794 le32_to_cpu(pcchunk->Length)) {
795 cifs_dbg(VFS, "invalid copy chunk response\n");
796 rc = -EIO;
797 goto cchunk_out;
798 }
799 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
800 cifs_dbg(VFS, "invalid num chunks written\n");
801 rc = -EIO;
802 goto cchunk_out;
803 }
804 chunks_copied++;
Steve French41c13582013-11-14 00:05:36 -0600805
Sachin Prabhu620d8742017-02-10 16:03:51 +0530806 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
807 src_off += bytes_written;
808 dest_off += bytes_written;
809 len -= bytes_written;
810 total_bytes_written += bytes_written;
Steve French41c13582013-11-14 00:05:36 -0600811
Sachin Prabhu620d8742017-02-10 16:03:51 +0530812 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
Steve French9bf0c9c2013-11-16 18:05:28 -0600813 le32_to_cpu(retbuf->ChunksWritten),
814 le32_to_cpu(retbuf->ChunkBytesWritten),
Sachin Prabhu620d8742017-02-10 16:03:51 +0530815 bytes_written);
Steve French9bf0c9c2013-11-16 18:05:28 -0600816 } else if (rc == -EINVAL) {
817 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
818 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -0600819
Steve French9bf0c9c2013-11-16 18:05:28 -0600820 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
821 le32_to_cpu(retbuf->ChunksWritten),
822 le32_to_cpu(retbuf->ChunkBytesWritten),
823 le32_to_cpu(retbuf->TotalBytesWritten));
824
825 /*
826 * Check if this is the first request using these sizes,
827 * (ie check if copy succeed once with original sizes
828 * and check if the server gave us different sizes after
829 * we already updated max sizes on previous request).
830 * if not then why is the server returning an error now
831 */
832 if ((chunks_copied != 0) || chunk_sizes_updated)
833 goto cchunk_out;
834
835 /* Check that server is not asking us to grow size */
836 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
837 tcon->max_bytes_chunk)
838 tcon->max_bytes_chunk =
839 le32_to_cpu(retbuf->ChunkBytesWritten);
840 else
841 goto cchunk_out; /* server gave us bogus size */
842
843 /* No need to change MaxChunks since already set to 1 */
844 chunk_sizes_updated = true;
Sachin Prabhu2477bc52015-02-04 13:10:26 +0000845 } else
846 goto cchunk_out;
Steve French9bf0c9c2013-11-16 18:05:28 -0600847 }
848
849cchunk_out:
Steve French41c13582013-11-14 00:05:36 -0600850 kfree(pcchunk);
Steve French24df1482016-09-29 04:20:23 -0500851 kfree(retbuf);
Sachin Prabhu620d8742017-02-10 16:03:51 +0530852 if (rc)
853 return rc;
854 else
855 return total_bytes_written;
Steve French41c13582013-11-14 00:05:36 -0600856}
857
858static int
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -0700859smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
860 struct cifs_fid *fid)
861{
862 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
863}
864
Pavel Shilovsky09a47072012-09-18 16:20:29 -0700865static unsigned int
866smb2_read_data_offset(char *buf)
867{
868 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
869 return rsp->DataOffset;
870}
871
872static unsigned int
873smb2_read_data_length(char *buf)
874{
875 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
876 return le32_to_cpu(rsp->DataLength);
877}
878
Pavel Shilovskyd8e05032012-09-18 16:20:30 -0700879
880static int
Steve Frenchdb8b6312014-09-22 05:13:55 -0500881smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -0700882 struct cifs_io_parms *parms, unsigned int *bytes_read,
883 char **buf, int *buf_type)
884{
Steve Frenchdb8b6312014-09-22 05:13:55 -0500885 parms->persistent_fid = pfid->persistent_fid;
886 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -0700887 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
888}
889
Pavel Shilovsky009d3442012-09-18 16:20:30 -0700890static int
Steve Frenchdb8b6312014-09-22 05:13:55 -0500891smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovsky009d3442012-09-18 16:20:30 -0700892 struct cifs_io_parms *parms, unsigned int *written,
893 struct kvec *iov, unsigned long nr_segs)
894{
895
Steve Frenchdb8b6312014-09-22 05:13:55 -0500896 parms->persistent_fid = pfid->persistent_fid;
897 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovsky009d3442012-09-18 16:20:30 -0700898 return SMB2_write(xid, parms, written, iov, nr_segs);
899}
900
Steve Frenchd43cc792014-08-13 17:16:29 -0500901/* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
902static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
903 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
904{
905 struct cifsInodeInfo *cifsi;
906 int rc;
907
908 cifsi = CIFS_I(inode);
909
910 /* if file already sparse don't bother setting sparse again */
911 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
912 return true; /* already sparse */
913
914 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
915 return true; /* already not sparse */
916
917 /*
918 * Can't check for sparse support on share the usual way via the
919 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
920 * since Samba server doesn't set the flag on the share, yet
921 * supports the set sparse FSCTL and returns sparse correctly
922 * in the file attributes. If we fail setting sparse though we
923 * mark that server does not support sparse files for this share
924 * to avoid repeatedly sending the unsupported fsctl to server
925 * if the file is repeatedly extended.
926 */
927 if (tcon->broken_sparse_sup)
928 return false;
929
930 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
931 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
Aurelien Aptel51146622017-02-28 15:08:41 +0100932 true /* is_fctl */, false /* use_ipc */,
933 &setsparse, 1, NULL, NULL);
Steve Frenchd43cc792014-08-13 17:16:29 -0500934 if (rc) {
935 tcon->broken_sparse_sup = true;
936 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
937 return false;
938 }
939
940 if (setsparse)
941 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
942 else
943 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
944
945 return true;
946}
947
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700948static int
949smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
950 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
951{
952 __le64 eof = cpu_to_le64(size);
Steve French3d1a3742014-08-11 21:05:25 -0500953 struct inode *inode;
954
955 /*
956 * If extending file more than one page make sparse. Many Linux fs
957 * make files sparse by default when extending via ftruncate
958 */
David Howells2b0143b2015-03-17 22:25:59 +0000959 inode = d_inode(cfile->dentry);
Steve French3d1a3742014-08-11 21:05:25 -0500960
961 if (!set_alloc && (size > inode->i_size + 8192)) {
Steve French3d1a3742014-08-11 21:05:25 -0500962 __u8 set_sparse = 1;
Steve French3d1a3742014-08-11 21:05:25 -0500963
Steve Frenchd43cc792014-08-13 17:16:29 -0500964 /* whether set sparse succeeds or not, extend the file */
965 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
Steve French3d1a3742014-08-11 21:05:25 -0500966 }
967
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700968 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -0500969 cfile->fid.volatile_fid, cfile->pid, &eof, false);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700970}
971
Steve French02b16662015-06-27 21:18:36 -0700972static int
973smb2_duplicate_extents(const unsigned int xid,
974 struct cifsFileInfo *srcfile,
975 struct cifsFileInfo *trgtfile, u64 src_off,
976 u64 len, u64 dest_off)
977{
978 int rc;
979 unsigned int ret_data_len;
Steve French02b16662015-06-27 21:18:36 -0700980 struct duplicate_extents_to_file dup_ext_buf;
981 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
982
983 /* server fileays advertise duplicate extent support with this flag */
984 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
985 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
986 return -EOPNOTSUPP;
987
988 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
989 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
990 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
991 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
992 dup_ext_buf.ByteCount = cpu_to_le64(len);
993 cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
994 src_off, dest_off, len);
995
996 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
997 if (rc)
998 goto duplicate_extents_out;
999
1000 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1001 trgtfile->fid.volatile_fid,
1002 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
Aurelien Aptel51146622017-02-28 15:08:41 +01001003 true /* is_fsctl */, false /* use_ipc */,
1004 (char *)&dup_ext_buf,
Steve French02b16662015-06-27 21:18:36 -07001005 sizeof(struct duplicate_extents_to_file),
Steve French24df1482016-09-29 04:20:23 -05001006 NULL,
Steve French02b16662015-06-27 21:18:36 -07001007 &ret_data_len);
1008
1009 if (ret_data_len > 0)
1010 cifs_dbg(FYI, "non-zero response length in duplicate extents");
1011
1012duplicate_extents_out:
1013 return rc;
1014}
Steve French02b16662015-06-27 21:18:36 -07001015
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001016static int
Steve French64a5cfa2013-10-14 15:31:32 -05001017smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1018 struct cifsFileInfo *cfile)
1019{
1020 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1021 cfile->fid.volatile_fid);
1022}
1023
1024static int
Steve Frenchb3152e22015-06-24 03:17:02 -05001025smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1026 struct cifsFileInfo *cfile)
1027{
1028 struct fsctl_set_integrity_information_req integr_info;
Steve Frenchb3152e22015-06-24 03:17:02 -05001029 unsigned int ret_data_len;
1030
1031 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1032 integr_info.Flags = 0;
1033 integr_info.Reserved = 0;
1034
1035 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1036 cfile->fid.volatile_fid,
1037 FSCTL_SET_INTEGRITY_INFORMATION,
Aurelien Aptel51146622017-02-28 15:08:41 +01001038 true /* is_fsctl */, false /* use_ipc */,
1039 (char *)&integr_info,
Steve Frenchb3152e22015-06-24 03:17:02 -05001040 sizeof(struct fsctl_set_integrity_information_req),
Steve French24df1482016-09-29 04:20:23 -05001041 NULL,
Steve Frenchb3152e22015-06-24 03:17:02 -05001042 &ret_data_len);
1043
1044}
1045
1046static int
Steve French834170c2016-09-30 21:14:26 -05001047smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1048 struct cifsFileInfo *cfile, void __user *ioc_buf)
1049{
1050 char *retbuf = NULL;
1051 unsigned int ret_data_len = 0;
1052 int rc;
1053 struct smb_snapshot_array snapshot_in;
1054
1055 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1056 cfile->fid.volatile_fid,
1057 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
Aurelien Aptel51146622017-02-28 15:08:41 +01001058 true /* is_fsctl */, false /* use_ipc */,
1059 NULL, 0 /* no input data */,
Steve French834170c2016-09-30 21:14:26 -05001060 (char **)&retbuf,
1061 &ret_data_len);
1062 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1063 rc, ret_data_len);
1064 if (rc)
1065 return rc;
1066
1067 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1068 /* Fixup buffer */
1069 if (copy_from_user(&snapshot_in, ioc_buf,
1070 sizeof(struct smb_snapshot_array))) {
1071 rc = -EFAULT;
1072 kfree(retbuf);
1073 return rc;
1074 }
1075 if (snapshot_in.snapshot_array_size < sizeof(struct smb_snapshot_array)) {
1076 rc = -ERANGE;
David Disseldorp0e5c7952017-05-03 17:39:09 +02001077 kfree(retbuf);
Steve French834170c2016-09-30 21:14:26 -05001078 return rc;
1079 }
1080
1081 if (ret_data_len > snapshot_in.snapshot_array_size)
1082 ret_data_len = snapshot_in.snapshot_array_size;
1083
1084 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1085 rc = -EFAULT;
1086 }
1087
1088 kfree(retbuf);
1089 return rc;
1090}
1091
1092static int
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001093smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1094 const char *path, struct cifs_sb_info *cifs_sb,
1095 struct cifs_fid *fid, __u16 search_flags,
1096 struct cifs_search_info *srch_inf)
1097{
1098 __le16 *utf16_path;
1099 int rc;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001100 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001101 struct cifs_open_parms oparms;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001102
1103 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1104 if (!utf16_path)
1105 return -ENOMEM;
1106
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001107 oparms.tcon = tcon;
1108 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1109 oparms.disposition = FILE_OPEN;
1110 oparms.create_options = 0;
1111 oparms.fid = fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001112 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001113
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001114 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001115 kfree(utf16_path);
1116 if (rc) {
Pavel Shilovskydcd878382017-06-06 16:58:58 -07001117 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001118 return rc;
1119 }
1120
1121 srch_inf->entries_in_buffer = 0;
1122 srch_inf->index_of_last_entry = 0;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001123
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001124 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1125 fid->volatile_fid, 0, srch_inf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001126 if (rc) {
Pavel Shilovskydcd878382017-06-06 16:58:58 -07001127 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001128 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001129 }
1130 return rc;
1131}
1132
1133static int
1134smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1135 struct cifs_fid *fid, __u16 search_flags,
1136 struct cifs_search_info *srch_inf)
1137{
1138 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1139 fid->volatile_fid, 0, srch_inf);
1140}
1141
1142static int
1143smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1144 struct cifs_fid *fid)
1145{
1146 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1147}
1148
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001149/*
1150* If we negotiate SMB2 protocol and get STATUS_PENDING - update
1151* the number of credits and return true. Otherwise - return false.
1152*/
1153static bool
1154smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
1155{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001156 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001157
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001158 if (shdr->Status != STATUS_PENDING)
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001159 return false;
1160
1161 if (!length) {
1162 spin_lock(&server->req_lock);
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001163 server->credits += le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001164 spin_unlock(&server->req_lock);
1165 wake_up(&server->request_q);
1166 }
1167
1168 return true;
1169}
1170
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001171static bool
1172smb2_is_session_expired(char *buf)
1173{
1174 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
1175
1176 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED)
1177 return false;
1178
1179 cifs_dbg(FYI, "Session expired\n");
1180 return true;
1181}
1182
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001183static int
1184smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
1185 struct cifsInodeInfo *cinode)
1186{
Pavel Shilovsky0822f512012-09-19 06:22:45 -07001187 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
1188 return SMB2_lease_break(0, tcon, cinode->lease_key,
1189 smb2_get_lease_state(cinode));
1190
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001191 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
1192 fid->volatile_fid,
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001193 CIFS_CACHE_READ(cinode) ? 1 : 0);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001194}
1195
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001196static int
1197smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1198 struct kstatfs *buf)
1199{
1200 int rc;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001201 __le16 srch_path = 0; /* Null - open root of share */
1202 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001203 struct cifs_open_parms oparms;
1204 struct cifs_fid fid;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001205
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001206 oparms.tcon = tcon;
1207 oparms.desired_access = FILE_READ_ATTRIBUTES;
1208 oparms.disposition = FILE_OPEN;
1209 oparms.create_options = 0;
1210 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001211 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001212
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001213 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001214 if (rc)
1215 return rc;
1216 buf->f_type = SMB2_MAGIC_NUMBER;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001217 rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1218 buf);
1219 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001220 return rc;
1221}
1222
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07001223static bool
1224smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
1225{
1226 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
1227 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
1228}
1229
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07001230static int
1231smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
1232 __u64 length, __u32 type, int lock, int unlock, bool wait)
1233{
1234 if (unlock && !lock)
1235 type = SMB2_LOCKFLAG_UNLOCK;
1236 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
1237 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
1238 current->tgid, length, offset, type, wait);
1239}
1240
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001241static void
1242smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
1243{
1244 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
1245}
1246
1247static void
1248smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
1249{
1250 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
1251}
1252
1253static void
1254smb2_new_lease_key(struct cifs_fid *fid)
1255{
Steve Frenchfa70b872016-09-22 00:39:34 -05001256 generate_random_uuid(fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001257}
1258
Aurelien Aptel9d496402017-02-13 16:16:49 +01001259static int
1260smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
1261 const char *search_name,
1262 struct dfs_info3_param **target_nodes,
1263 unsigned int *num_of_nodes,
1264 const struct nls_table *nls_codepage, int remap)
1265{
1266 int rc;
1267 __le16 *utf16_path = NULL;
1268 int utf16_path_len = 0;
1269 struct cifs_tcon *tcon;
1270 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
1271 struct get_dfs_referral_rsp *dfs_rsp = NULL;
1272 u32 dfs_req_size = 0, dfs_rsp_size = 0;
1273
1274 cifs_dbg(FYI, "smb2_get_dfs_refer path <%s>\n", search_name);
1275
1276 /*
1277 * Use any tcon from the current session. Here, the first one.
1278 */
1279 spin_lock(&cifs_tcp_ses_lock);
1280 tcon = list_first_entry_or_null(&ses->tcon_list, struct cifs_tcon,
1281 tcon_list);
1282 if (tcon)
1283 tcon->tc_count++;
1284 spin_unlock(&cifs_tcp_ses_lock);
1285
1286 if (!tcon) {
1287 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
1288 ses);
1289 rc = -ENOTCONN;
1290 goto out;
1291 }
1292
1293 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
1294 &utf16_path_len,
1295 nls_codepage, remap);
1296 if (!utf16_path) {
1297 rc = -ENOMEM;
1298 goto out;
1299 }
1300
1301 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
1302 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
1303 if (!dfs_req) {
1304 rc = -ENOMEM;
1305 goto out;
1306 }
1307
1308 /* Highest DFS referral version understood */
1309 dfs_req->MaxReferralLevel = DFS_VERSION;
1310
1311 /* Path to resolve in an UTF-16 null-terminated string */
1312 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
1313
1314 do {
1315 /* try first with IPC */
1316 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1317 FSCTL_DFS_GET_REFERRALS,
1318 true /* is_fsctl */, true /* use_ipc */,
1319 (char *)dfs_req, dfs_req_size,
1320 (char **)&dfs_rsp, &dfs_rsp_size);
1321 if (rc == -ENOTCONN) {
1322 /* try with normal tcon */
1323 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1324 FSCTL_DFS_GET_REFERRALS,
1325 true /* is_fsctl */, false /*use_ipc*/,
1326 (char *)dfs_req, dfs_req_size,
1327 (char **)&dfs_rsp, &dfs_rsp_size);
1328 }
1329 } while (rc == -EAGAIN);
1330
1331 if (rc) {
1332 cifs_dbg(VFS, "ioctl error in smb2_get_dfs_refer rc=%d\n", rc);
1333 goto out;
1334 }
1335
1336 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
1337 num_of_nodes, target_nodes,
1338 nls_codepage, remap, search_name,
1339 true /* is_unicode */);
1340 if (rc) {
1341 cifs_dbg(VFS, "parse error in smb2_get_dfs_refer rc=%d\n", rc);
1342 goto out;
1343 }
1344
1345 out:
1346 if (tcon) {
1347 spin_lock(&cifs_tcp_ses_lock);
1348 tcon->tc_count--;
1349 spin_unlock(&cifs_tcp_ses_lock);
1350 }
1351 kfree(utf16_path);
1352 kfree(dfs_req);
1353 kfree(dfs_rsp);
1354 return rc;
1355}
Pavel Shilovsky78932422016-07-24 10:37:38 +03001356#define SMB2_SYMLINK_STRUCT_SIZE \
1357 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
1358
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001359static int
1360smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
1361 const char *full_path, char **target_path,
1362 struct cifs_sb_info *cifs_sb)
1363{
1364 int rc;
1365 __le16 *utf16_path;
1366 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1367 struct cifs_open_parms oparms;
1368 struct cifs_fid fid;
1369 struct smb2_err_rsp *err_buf = NULL;
1370 struct smb2_symlink_err_rsp *symlink;
Pavel Shilovsky78932422016-07-24 10:37:38 +03001371 unsigned int sub_len;
1372 unsigned int sub_offset;
1373 unsigned int print_len;
1374 unsigned int print_offset;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001375
1376 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
1377
1378 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1379 if (!utf16_path)
1380 return -ENOMEM;
1381
1382 oparms.tcon = tcon;
1383 oparms.desired_access = FILE_READ_ATTRIBUTES;
1384 oparms.disposition = FILE_OPEN;
1385 oparms.create_options = 0;
1386 oparms.fid = &fid;
1387 oparms.reconnect = false;
1388
1389 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_buf);
1390
1391 if (!rc || !err_buf) {
1392 kfree(utf16_path);
1393 return -ENOENT;
1394 }
Pavel Shilovsky78932422016-07-24 10:37:38 +03001395
1396 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
1397 get_rfc1002_length(err_buf) + 4 < SMB2_SYMLINK_STRUCT_SIZE) {
1398 kfree(utf16_path);
1399 return -ENOENT;
1400 }
1401
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001402 /* open must fail on symlink - reset rc */
1403 rc = 0;
1404 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
1405 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
1406 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
Pavel Shilovsky78932422016-07-24 10:37:38 +03001407 print_len = le16_to_cpu(symlink->PrintNameLength);
1408 print_offset = le16_to_cpu(symlink->PrintNameOffset);
1409
1410 if (get_rfc1002_length(err_buf) + 4 <
1411 SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
1412 kfree(utf16_path);
1413 return -ENOENT;
1414 }
1415
1416 if (get_rfc1002_length(err_buf) + 4 <
1417 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
1418 kfree(utf16_path);
1419 return -ENOENT;
1420 }
1421
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001422 *target_path = cifs_strndup_from_utf16(
1423 (char *)symlink->PathBuffer + sub_offset,
1424 sub_len, true, cifs_sb->local_nls);
1425 if (!(*target_path)) {
1426 kfree(utf16_path);
1427 return -ENOMEM;
1428 }
1429 convert_delimiter(*target_path, '/');
1430 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
1431 kfree(utf16_path);
1432 return rc;
1433}
1434
Arnd Bergmann84908422017-06-27 17:06:13 +02001435#ifdef CONFIG_CIFS_ACL
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001436static struct cifs_ntsd *
1437get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
1438 const struct cifs_fid *cifsfid, u32 *pacllen)
1439{
1440 struct cifs_ntsd *pntsd = NULL;
1441 unsigned int xid;
1442 int rc = -EOPNOTSUPP;
1443 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1444
1445 if (IS_ERR(tlink))
1446 return ERR_CAST(tlink);
1447
1448 xid = get_xid();
1449 cifs_dbg(FYI, "trying to get acl\n");
1450
1451 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
1452 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
1453 free_xid(xid);
1454
1455 cifs_put_tlink(tlink);
1456
1457 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
1458 if (rc)
1459 return ERR_PTR(rc);
1460 return pntsd;
1461
1462}
1463
1464static struct cifs_ntsd *
1465get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
1466 const char *path, u32 *pacllen)
1467{
1468 struct cifs_ntsd *pntsd = NULL;
1469 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1470 unsigned int xid;
1471 int rc;
1472 struct cifs_tcon *tcon;
1473 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1474 struct cifs_fid fid;
1475 struct cifs_open_parms oparms;
1476 __le16 *utf16_path;
1477
1478 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
1479 if (IS_ERR(tlink))
1480 return ERR_CAST(tlink);
1481
1482 tcon = tlink_tcon(tlink);
1483 xid = get_xid();
1484
1485 if (backup_cred(cifs_sb))
Colin Ian King709340a2017-07-05 13:47:34 +01001486 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001487 else
1488 oparms.create_options = 0;
1489
1490 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1491 if (!utf16_path)
1492 return ERR_PTR(-ENOMEM);
1493
1494 oparms.tcon = tcon;
1495 oparms.desired_access = READ_CONTROL;
1496 oparms.disposition = FILE_OPEN;
1497 oparms.fid = &fid;
1498 oparms.reconnect = false;
1499
1500 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
1501 kfree(utf16_path);
1502 if (!rc) {
1503 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
1504 fid.volatile_fid, (void **)&pntsd, pacllen);
1505 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1506 }
1507
1508 cifs_put_tlink(tlink);
1509 free_xid(xid);
1510
1511 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
1512 if (rc)
1513 return ERR_PTR(rc);
1514 return pntsd;
1515}
1516
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05001517#ifdef CONFIG_CIFS_ACL
1518static int
1519set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
1520 struct inode *inode, const char *path, int aclflag)
1521{
1522 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1523 unsigned int xid;
1524 int rc, access_flags = 0;
1525 struct cifs_tcon *tcon;
1526 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1527 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1528 struct cifs_fid fid;
1529 struct cifs_open_parms oparms;
1530 __le16 *utf16_path;
1531
1532 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
1533 if (IS_ERR(tlink))
1534 return PTR_ERR(tlink);
1535
1536 tcon = tlink_tcon(tlink);
1537 xid = get_xid();
1538
1539 if (backup_cred(cifs_sb))
1540 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1541 else
1542 oparms.create_options = 0;
1543
1544 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
1545 access_flags = WRITE_OWNER;
1546 else
1547 access_flags = WRITE_DAC;
1548
1549 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1550 if (!utf16_path)
1551 return -ENOMEM;
1552
1553 oparms.tcon = tcon;
1554 oparms.desired_access = access_flags;
1555 oparms.disposition = FILE_OPEN;
1556 oparms.path = path;
1557 oparms.fid = &fid;
1558 oparms.reconnect = false;
1559
1560 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
1561 kfree(utf16_path);
1562 if (!rc) {
1563 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
1564 fid.volatile_fid, pnntsd, acllen, aclflag);
1565 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1566 }
1567
1568 cifs_put_tlink(tlink);
1569 free_xid(xid);
1570 return rc;
1571}
1572#endif /* CIFS_ACL */
1573
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001574/* Retrieve an ACL from the server */
1575static struct cifs_ntsd *
1576get_smb2_acl(struct cifs_sb_info *cifs_sb,
1577 struct inode *inode, const char *path,
1578 u32 *pacllen)
1579{
1580 struct cifs_ntsd *pntsd = NULL;
1581 struct cifsFileInfo *open_file = NULL;
1582
1583 if (inode)
1584 open_file = find_readable_file(CIFS_I(inode), true);
1585 if (!open_file)
1586 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
1587
1588 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
1589 cifsFileInfo_put(open_file);
1590 return pntsd;
1591}
Arnd Bergmann84908422017-06-27 17:06:13 +02001592#endif
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05001593
Steve French30175622014-08-17 18:16:40 -05001594static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
1595 loff_t offset, loff_t len, bool keep_size)
1596{
1597 struct inode *inode;
1598 struct cifsInodeInfo *cifsi;
1599 struct cifsFileInfo *cfile = file->private_data;
1600 struct file_zero_data_information fsctl_buf;
1601 long rc;
1602 unsigned int xid;
1603
1604 xid = get_xid();
1605
David Howells2b0143b2015-03-17 22:25:59 +00001606 inode = d_inode(cfile->dentry);
Steve French30175622014-08-17 18:16:40 -05001607 cifsi = CIFS_I(inode);
1608
1609 /* if file not oplocked can't be sure whether asking to extend size */
1610 if (!CIFS_CACHE_READ(cifsi))
1611 if (keep_size == false)
1612 return -EOPNOTSUPP;
1613
Steve French2bb93d22014-08-20 18:56:29 -05001614 /*
Steve French30175622014-08-17 18:16:40 -05001615 * Must check if file sparse since fallocate -z (zero range) assumes
1616 * non-sparse allocation
1617 */
1618 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE))
1619 return -EOPNOTSUPP;
1620
1621 /*
1622 * need to make sure we are not asked to extend the file since the SMB3
1623 * fsctl does not change the file size. In the future we could change
1624 * this to zero the first part of the range then set the file size
1625 * which for a non sparse file would zero the newly extended range
1626 */
1627 if (keep_size == false)
1628 if (i_size_read(inode) < offset + len)
1629 return -EOPNOTSUPP;
1630
1631 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1632
1633 fsctl_buf.FileOffset = cpu_to_le64(offset);
1634 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1635
1636 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1637 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
Aurelien Aptel51146622017-02-28 15:08:41 +01001638 true /* is_fctl */, false /* use_ipc */,
1639 (char *)&fsctl_buf,
Steve French30175622014-08-17 18:16:40 -05001640 sizeof(struct file_zero_data_information), NULL, NULL);
1641 free_xid(xid);
1642 return rc;
1643}
1644
Steve French31742c52014-08-17 08:38:47 -05001645static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
1646 loff_t offset, loff_t len)
1647{
1648 struct inode *inode;
1649 struct cifsInodeInfo *cifsi;
1650 struct cifsFileInfo *cfile = file->private_data;
1651 struct file_zero_data_information fsctl_buf;
1652 long rc;
1653 unsigned int xid;
1654 __u8 set_sparse = 1;
1655
1656 xid = get_xid();
1657
David Howells2b0143b2015-03-17 22:25:59 +00001658 inode = d_inode(cfile->dentry);
Steve French31742c52014-08-17 08:38:47 -05001659 cifsi = CIFS_I(inode);
1660
1661 /* Need to make file sparse, if not already, before freeing range. */
1662 /* Consider adding equivalent for compressed since it could also work */
1663 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse))
1664 return -EOPNOTSUPP;
1665
1666 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1667
1668 fsctl_buf.FileOffset = cpu_to_le64(offset);
1669 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1670
1671 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1672 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
Aurelien Aptel51146622017-02-28 15:08:41 +01001673 true /* is_fctl */, false /* use_ipc */,
1674 (char *)&fsctl_buf,
Steve French31742c52014-08-17 08:38:47 -05001675 sizeof(struct file_zero_data_information), NULL, NULL);
1676 free_xid(xid);
1677 return rc;
1678}
1679
Steve French9ccf3212014-10-18 17:01:15 -05001680static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
1681 loff_t off, loff_t len, bool keep_size)
1682{
1683 struct inode *inode;
1684 struct cifsInodeInfo *cifsi;
1685 struct cifsFileInfo *cfile = file->private_data;
1686 long rc = -EOPNOTSUPP;
1687 unsigned int xid;
1688
1689 xid = get_xid();
1690
David Howells2b0143b2015-03-17 22:25:59 +00001691 inode = d_inode(cfile->dentry);
Steve French9ccf3212014-10-18 17:01:15 -05001692 cifsi = CIFS_I(inode);
1693
1694 /* if file not oplocked can't be sure whether asking to extend size */
1695 if (!CIFS_CACHE_READ(cifsi))
1696 if (keep_size == false)
1697 return -EOPNOTSUPP;
1698
1699 /*
1700 * Files are non-sparse by default so falloc may be a no-op
1701 * Must check if file sparse. If not sparse, and not extending
1702 * then no need to do anything since file already allocated
1703 */
1704 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
1705 if (keep_size == true)
1706 return 0;
1707 /* check if extending file */
1708 else if (i_size_read(inode) >= off + len)
1709 /* not extending file and already not sparse */
1710 return 0;
1711 /* BB: in future add else clause to extend file */
1712 else
1713 return -EOPNOTSUPP;
1714 }
1715
1716 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
1717 /*
1718 * Check if falloc starts within first few pages of file
1719 * and ends within a few pages of the end of file to
1720 * ensure that most of file is being forced to be
1721 * fallocated now. If so then setting whole file sparse
1722 * ie potentially making a few extra pages at the beginning
1723 * or end of the file non-sparse via set_sparse is harmless.
1724 */
1725 if ((off > 8192) || (off + len + 8192 < i_size_read(inode)))
1726 return -EOPNOTSUPP;
1727
1728 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
1729 }
1730 /* BB: else ... in future add code to extend file and set sparse */
1731
1732
1733 free_xid(xid);
1734 return rc;
1735}
1736
1737
Steve French31742c52014-08-17 08:38:47 -05001738static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
1739 loff_t off, loff_t len)
1740{
1741 /* KEEP_SIZE already checked for by do_fallocate */
1742 if (mode & FALLOC_FL_PUNCH_HOLE)
1743 return smb3_punch_hole(file, tcon, off, len);
Steve French30175622014-08-17 18:16:40 -05001744 else if (mode & FALLOC_FL_ZERO_RANGE) {
1745 if (mode & FALLOC_FL_KEEP_SIZE)
1746 return smb3_zero_range(file, tcon, off, len, true);
1747 return smb3_zero_range(file, tcon, off, len, false);
Steve French9ccf3212014-10-18 17:01:15 -05001748 } else if (mode == FALLOC_FL_KEEP_SIZE)
1749 return smb3_simple_falloc(file, tcon, off, len, true);
1750 else if (mode == 0)
1751 return smb3_simple_falloc(file, tcon, off, len, false);
Steve French31742c52014-08-17 08:38:47 -05001752
1753 return -EOPNOTSUPP;
1754}
1755
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001756static void
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00001757smb2_downgrade_oplock(struct TCP_Server_Info *server,
1758 struct cifsInodeInfo *cinode, bool set_level2)
1759{
1760 if (set_level2)
1761 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
1762 0, NULL);
1763 else
1764 server->ops->set_oplock_level(cinode, 0, 0, NULL);
1765}
1766
1767static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001768smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1769 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001770{
1771 oplock &= 0xFF;
1772 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1773 return;
1774 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001775 cinode->oplock = CIFS_CACHE_RHW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001776 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
1777 &cinode->vfs_inode);
1778 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001779 cinode->oplock = CIFS_CACHE_RW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001780 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
1781 &cinode->vfs_inode);
1782 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
1783 cinode->oplock = CIFS_CACHE_READ_FLG;
1784 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
1785 &cinode->vfs_inode);
1786 } else
1787 cinode->oplock = 0;
1788}
1789
1790static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001791smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1792 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001793{
1794 char message[5] = {0};
1795
1796 oplock &= 0xFF;
1797 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1798 return;
1799
1800 cinode->oplock = 0;
1801 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
1802 cinode->oplock |= CIFS_CACHE_READ_FLG;
1803 strcat(message, "R");
1804 }
1805 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
1806 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
1807 strcat(message, "H");
1808 }
1809 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
1810 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
1811 strcat(message, "W");
1812 }
1813 if (!cinode->oplock)
1814 strcat(message, "None");
1815 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
1816 &cinode->vfs_inode);
1817}
1818
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001819static void
1820smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1821 unsigned int epoch, bool *purge_cache)
1822{
1823 unsigned int old_oplock = cinode->oplock;
1824
1825 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
1826
1827 if (purge_cache) {
1828 *purge_cache = false;
1829 if (old_oplock == CIFS_CACHE_READ_FLG) {
1830 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
1831 (epoch - cinode->epoch > 0))
1832 *purge_cache = true;
1833 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1834 (epoch - cinode->epoch > 1))
1835 *purge_cache = true;
1836 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1837 (epoch - cinode->epoch > 1))
1838 *purge_cache = true;
1839 else if (cinode->oplock == 0 &&
1840 (epoch - cinode->epoch > 0))
1841 *purge_cache = true;
1842 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
1843 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1844 (epoch - cinode->epoch > 0))
1845 *purge_cache = true;
1846 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1847 (epoch - cinode->epoch > 1))
1848 *purge_cache = true;
1849 }
1850 cinode->epoch = epoch;
1851 }
1852}
1853
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001854static bool
1855smb2_is_read_op(__u32 oplock)
1856{
1857 return oplock == SMB2_OPLOCK_LEVEL_II;
1858}
1859
1860static bool
1861smb21_is_read_op(__u32 oplock)
1862{
1863 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
1864 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
1865}
1866
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001867static __le32
1868map_oplock_to_lease(u8 oplock)
1869{
1870 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
1871 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
1872 else if (oplock == SMB2_OPLOCK_LEVEL_II)
1873 return SMB2_LEASE_READ_CACHING;
1874 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
1875 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
1876 SMB2_LEASE_WRITE_CACHING;
1877 return 0;
1878}
1879
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001880static char *
1881smb2_create_lease_buf(u8 *lease_key, u8 oplock)
1882{
1883 struct create_lease *buf;
1884
1885 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
1886 if (!buf)
1887 return NULL;
1888
1889 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1890 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001891 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001892
1893 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1894 (struct create_lease, lcontext));
1895 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
1896 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1897 (struct create_lease, Name));
1898 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001899 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001900 buf->Name[0] = 'R';
1901 buf->Name[1] = 'q';
1902 buf->Name[2] = 'L';
1903 buf->Name[3] = 's';
1904 return (char *)buf;
1905}
1906
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001907static char *
1908smb3_create_lease_buf(u8 *lease_key, u8 oplock)
1909{
1910 struct create_lease_v2 *buf;
1911
1912 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
1913 if (!buf)
1914 return NULL;
1915
1916 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1917 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
1918 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
1919
1920 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1921 (struct create_lease_v2, lcontext));
1922 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
1923 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1924 (struct create_lease_v2, Name));
1925 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001926 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001927 buf->Name[0] = 'R';
1928 buf->Name[1] = 'q';
1929 buf->Name[2] = 'L';
1930 buf->Name[3] = 's';
1931 return (char *)buf;
1932}
1933
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001934static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001935smb2_parse_lease_buf(void *buf, unsigned int *epoch)
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001936{
1937 struct create_lease *lc = (struct create_lease *)buf;
1938
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001939 *epoch = 0; /* not used */
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001940 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1941 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1942 return le32_to_cpu(lc->lcontext.LeaseState);
1943}
1944
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001945static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001946smb3_parse_lease_buf(void *buf, unsigned int *epoch)
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001947{
1948 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
1949
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001950 *epoch = le16_to_cpu(lc->lcontext.Epoch);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001951 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1952 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1953 return le32_to_cpu(lc->lcontext.LeaseState);
1954}
1955
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001956static unsigned int
1957smb2_wp_retry_size(struct inode *inode)
1958{
1959 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
1960 SMB2_MAX_BUFFER_SIZE);
1961}
1962
Pavel Shilovsky52755802014-08-18 20:49:57 +04001963static bool
1964smb2_dir_needs_close(struct cifsFileInfo *cfile)
1965{
1966 return !cfile->invalidHandle;
1967}
1968
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07001969static void
1970fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, struct smb_rqst *old_rq)
1971{
1972 struct smb2_sync_hdr *shdr =
1973 (struct smb2_sync_hdr *)old_rq->rq_iov[1].iov_base;
1974 unsigned int orig_len = get_rfc1002_length(old_rq->rq_iov[0].iov_base);
1975
1976 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
1977 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
1978 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
1979 tr_hdr->Flags = cpu_to_le16(0x01);
1980 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
1981 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
1982 inc_rfc1001_len(tr_hdr, sizeof(struct smb2_transform_hdr) - 4);
1983 inc_rfc1001_len(tr_hdr, orig_len);
1984}
1985
1986static struct scatterlist *
1987init_sg(struct smb_rqst *rqst, u8 *sign)
1988{
1989 unsigned int sg_len = rqst->rq_nvec + rqst->rq_npages + 1;
1990 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 24;
1991 struct scatterlist *sg;
1992 unsigned int i;
1993 unsigned int j;
1994
1995 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
1996 if (!sg)
1997 return NULL;
1998
1999 sg_init_table(sg, sg_len);
2000 sg_set_buf(&sg[0], rqst->rq_iov[0].iov_base + 24, assoc_data_len);
2001 for (i = 1; i < rqst->rq_nvec; i++)
2002 sg_set_buf(&sg[i], rqst->rq_iov[i].iov_base,
2003 rqst->rq_iov[i].iov_len);
2004 for (j = 0; i < sg_len - 1; i++, j++) {
2005 unsigned int len = (j < rqst->rq_npages - 1) ? rqst->rq_pagesz
2006 : rqst->rq_tailsz;
2007 sg_set_page(&sg[i], rqst->rq_pages[j], len, 0);
2008 }
2009 sg_set_buf(&sg[sg_len - 1], sign, SMB2_SIGNATURE_SIZE);
2010 return sg;
2011}
2012
2013struct cifs_crypt_result {
2014 int err;
2015 struct completion completion;
2016};
2017
2018static void cifs_crypt_complete(struct crypto_async_request *req, int err)
2019{
2020 struct cifs_crypt_result *res = req->data;
2021
2022 if (err == -EINPROGRESS)
2023 return;
2024
2025 res->err = err;
2026 complete(&res->completion);
2027}
2028
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002029static int
2030smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
2031{
2032 struct cifs_ses *ses;
2033 u8 *ses_enc_key;
2034
2035 spin_lock(&cifs_tcp_ses_lock);
2036 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2037 if (ses->Suid != ses_id)
2038 continue;
2039 ses_enc_key = enc ? ses->smb3encryptionkey :
2040 ses->smb3decryptionkey;
2041 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
2042 spin_unlock(&cifs_tcp_ses_lock);
2043 return 0;
2044 }
2045 spin_unlock(&cifs_tcp_ses_lock);
2046
2047 return 1;
2048}
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002049/*
2050 * Encrypt or decrypt @rqst message. @rqst has the following format:
2051 * iov[0] - transform header (associate data),
2052 * iov[1-N] and pages - data to encrypt.
2053 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
2054 * untouched.
2055 */
2056static int
2057crypt_message(struct TCP_Server_Info *server, struct smb_rqst *rqst, int enc)
2058{
2059 struct smb2_transform_hdr *tr_hdr =
2060 (struct smb2_transform_hdr *)rqst->rq_iov[0].iov_base;
2061 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 24;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002062 int rc = 0;
2063 struct scatterlist *sg;
2064 u8 sign[SMB2_SIGNATURE_SIZE] = {};
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002065 u8 key[SMB3_SIGN_KEY_SIZE];
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002066 struct aead_request *req;
2067 char *iv;
2068 unsigned int iv_len;
2069 struct cifs_crypt_result result = {0, };
2070 struct crypto_aead *tfm;
2071 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2072
2073 init_completion(&result.completion);
2074
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002075 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
2076 if (rc) {
2077 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
2078 enc ? "en" : "de");
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002079 return 0;
2080 }
2081
2082 rc = smb3_crypto_aead_allocate(server);
2083 if (rc) {
2084 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
2085 return rc;
2086 }
2087
2088 tfm = enc ? server->secmech.ccmaesencrypt :
2089 server->secmech.ccmaesdecrypt;
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002090 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002091 if (rc) {
2092 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
2093 return rc;
2094 }
2095
2096 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
2097 if (rc) {
2098 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
2099 return rc;
2100 }
2101
2102 req = aead_request_alloc(tfm, GFP_KERNEL);
2103 if (!req) {
2104 cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
2105 return -ENOMEM;
2106 }
2107
2108 if (!enc) {
2109 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
2110 crypt_len += SMB2_SIGNATURE_SIZE;
2111 }
2112
2113 sg = init_sg(rqst, sign);
2114 if (!sg) {
Christophe Jaillet517a6e42017-06-11 09:12:47 +02002115 cifs_dbg(VFS, "%s: Failed to init sg", __func__);
2116 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002117 goto free_req;
2118 }
2119
2120 iv_len = crypto_aead_ivsize(tfm);
2121 iv = kzalloc(iv_len, GFP_KERNEL);
2122 if (!iv) {
2123 cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
Christophe Jaillet517a6e42017-06-11 09:12:47 +02002124 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002125 goto free_sg;
2126 }
2127 iv[0] = 3;
2128 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2129
2130 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
2131 aead_request_set_ad(req, assoc_data_len);
2132
2133 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2134 cifs_crypt_complete, &result);
2135
2136 rc = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
2137
2138 if (rc == -EINPROGRESS || rc == -EBUSY) {
2139 wait_for_completion(&result.completion);
2140 rc = result.err;
2141 }
2142
2143 if (!rc && enc)
2144 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
2145
2146 kfree(iv);
2147free_sg:
2148 kfree(sg);
2149free_req:
2150 kfree(req);
2151 return rc;
2152}
2153
2154static int
2155smb3_init_transform_rq(struct TCP_Server_Info *server, struct smb_rqst *new_rq,
2156 struct smb_rqst *old_rq)
2157{
2158 struct kvec *iov;
2159 struct page **pages;
2160 struct smb2_transform_hdr *tr_hdr;
2161 unsigned int npages = old_rq->rq_npages;
2162 int i;
2163 int rc = -ENOMEM;
2164
2165 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2166 if (!pages)
2167 return rc;
2168
2169 new_rq->rq_pages = pages;
2170 new_rq->rq_npages = old_rq->rq_npages;
2171 new_rq->rq_pagesz = old_rq->rq_pagesz;
2172 new_rq->rq_tailsz = old_rq->rq_tailsz;
2173
2174 for (i = 0; i < npages; i++) {
2175 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2176 if (!pages[i])
2177 goto err_free_pages;
2178 }
2179
2180 iov = kmalloc_array(old_rq->rq_nvec, sizeof(struct kvec), GFP_KERNEL);
2181 if (!iov)
2182 goto err_free_pages;
2183
2184 /* copy all iovs from the old except the 1st one (rfc1002 length) */
2185 memcpy(&iov[1], &old_rq->rq_iov[1],
2186 sizeof(struct kvec) * (old_rq->rq_nvec - 1));
2187 new_rq->rq_iov = iov;
2188 new_rq->rq_nvec = old_rq->rq_nvec;
2189
2190 tr_hdr = kmalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL);
2191 if (!tr_hdr)
2192 goto err_free_iov;
2193
2194 /* fill the 1st iov with a transform header */
2195 fill_transform_hdr(tr_hdr, old_rq);
2196 new_rq->rq_iov[0].iov_base = tr_hdr;
2197 new_rq->rq_iov[0].iov_len = sizeof(struct smb2_transform_hdr);
2198
2199 /* copy pages form the old */
2200 for (i = 0; i < npages; i++) {
2201 char *dst = kmap(new_rq->rq_pages[i]);
2202 char *src = kmap(old_rq->rq_pages[i]);
2203 unsigned int len = (i < npages - 1) ? new_rq->rq_pagesz :
2204 new_rq->rq_tailsz;
2205 memcpy(dst, src, len);
2206 kunmap(new_rq->rq_pages[i]);
2207 kunmap(old_rq->rq_pages[i]);
2208 }
2209
2210 rc = crypt_message(server, new_rq, 1);
2211 cifs_dbg(FYI, "encrypt message returned %d", rc);
2212 if (rc)
2213 goto err_free_tr_hdr;
2214
2215 return rc;
2216
2217err_free_tr_hdr:
2218 kfree(tr_hdr);
2219err_free_iov:
2220 kfree(iov);
2221err_free_pages:
2222 for (i = i - 1; i >= 0; i--)
2223 put_page(pages[i]);
2224 kfree(pages);
2225 return rc;
2226}
2227
2228static void
2229smb3_free_transform_rq(struct smb_rqst *rqst)
2230{
2231 int i = rqst->rq_npages - 1;
2232
2233 for (; i >= 0; i--)
2234 put_page(rqst->rq_pages[i]);
2235 kfree(rqst->rq_pages);
2236 /* free transform header */
2237 kfree(rqst->rq_iov[0].iov_base);
2238 kfree(rqst->rq_iov);
2239}
2240
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002241static int
2242smb3_is_transform_hdr(void *buf)
2243{
2244 struct smb2_transform_hdr *trhdr = buf;
2245
2246 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
2247}
2248
2249static int
2250decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
2251 unsigned int buf_data_size, struct page **pages,
2252 unsigned int npages, unsigned int page_data_size)
2253{
2254 struct kvec iov[2];
2255 struct smb_rqst rqst = {NULL};
2256 struct smb2_hdr *hdr;
2257 int rc;
2258
2259 iov[0].iov_base = buf;
2260 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
2261 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
2262 iov[1].iov_len = buf_data_size;
2263
2264 rqst.rq_iov = iov;
2265 rqst.rq_nvec = 2;
2266 rqst.rq_pages = pages;
2267 rqst.rq_npages = npages;
2268 rqst.rq_pagesz = PAGE_SIZE;
2269 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
2270
2271 rc = crypt_message(server, &rqst, 0);
2272 cifs_dbg(FYI, "decrypt message returned %d\n", rc);
2273
2274 if (rc)
2275 return rc;
2276
2277 memmove(buf + 4, iov[1].iov_base, buf_data_size);
2278 hdr = (struct smb2_hdr *)buf;
2279 hdr->smb2_buf_length = cpu_to_be32(buf_data_size + page_data_size);
2280 server->total_read = buf_data_size + page_data_size + 4;
2281
2282 return rc;
2283}
2284
2285static int
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002286read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
2287 unsigned int npages, unsigned int len)
2288{
2289 int i;
2290 int length;
2291
2292 for (i = 0; i < npages; i++) {
2293 struct page *page = pages[i];
2294 size_t n;
2295
2296 n = len;
2297 if (len >= PAGE_SIZE) {
2298 /* enough data to fill the page */
2299 n = PAGE_SIZE;
2300 len -= n;
2301 } else {
2302 zero_user(page, len, PAGE_SIZE - len);
2303 len = 0;
2304 }
2305 length = cifs_read_page_from_socket(server, page, n);
2306 if (length < 0)
2307 return length;
2308 server->total_read += length;
2309 }
2310
2311 return 0;
2312}
2313
2314static int
2315init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
2316 unsigned int cur_off, struct bio_vec **page_vec)
2317{
2318 struct bio_vec *bvec;
2319 int i;
2320
2321 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
2322 if (!bvec)
2323 return -ENOMEM;
2324
2325 for (i = 0; i < npages; i++) {
2326 bvec[i].bv_page = pages[i];
2327 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
2328 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
2329 data_size -= bvec[i].bv_len;
2330 }
2331
2332 if (data_size != 0) {
2333 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
2334 kfree(bvec);
2335 return -EIO;
2336 }
2337
2338 *page_vec = bvec;
2339 return 0;
2340}
2341
2342static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002343handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
2344 char *buf, unsigned int buf_len, struct page **pages,
2345 unsigned int npages, unsigned int page_data_size)
2346{
2347 unsigned int data_offset;
2348 unsigned int data_len;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002349 unsigned int cur_off;
2350 unsigned int cur_page_idx;
2351 unsigned int pad_len;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002352 struct cifs_readdata *rdata = mid->callback_data;
2353 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
2354 struct bio_vec *bvec = NULL;
2355 struct iov_iter iter;
2356 struct kvec iov;
2357 int length;
2358
2359 if (shdr->Command != SMB2_READ) {
2360 cifs_dbg(VFS, "only big read responses are supported\n");
2361 return -ENOTSUPP;
2362 }
2363
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002364 if (server->ops->is_session_expired &&
2365 server->ops->is_session_expired(buf)) {
2366 cifs_reconnect(server);
2367 wake_up(&server->response_q);
2368 return -1;
2369 }
2370
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002371 if (server->ops->is_status_pending &&
2372 server->ops->is_status_pending(buf, server, 0))
2373 return -1;
2374
2375 rdata->result = server->ops->map_error(buf, false);
2376 if (rdata->result != 0) {
2377 cifs_dbg(FYI, "%s: server returned error %d\n",
2378 __func__, rdata->result);
2379 dequeue_mid(mid, rdata->result);
2380 return 0;
2381 }
2382
2383 data_offset = server->ops->read_data_offset(buf) + 4;
2384 data_len = server->ops->read_data_length(buf);
2385
2386 if (data_offset < server->vals->read_rsp_size) {
2387 /*
2388 * win2k8 sometimes sends an offset of 0 when the read
2389 * is beyond the EOF. Treat it as if the data starts just after
2390 * the header.
2391 */
2392 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
2393 __func__, data_offset);
2394 data_offset = server->vals->read_rsp_size;
2395 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
2396 /* data_offset is beyond the end of smallbuf */
2397 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
2398 __func__, data_offset);
2399 rdata->result = -EIO;
2400 dequeue_mid(mid, rdata->result);
2401 return 0;
2402 }
2403
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002404 pad_len = data_offset - server->vals->read_rsp_size;
2405
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002406 if (buf_len <= data_offset) {
2407 /* read response payload is in pages */
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002408 cur_page_idx = pad_len / PAGE_SIZE;
2409 cur_off = pad_len % PAGE_SIZE;
2410
2411 if (cur_page_idx != 0) {
2412 /* data offset is beyond the 1st page of response */
2413 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
2414 __func__, data_offset);
2415 rdata->result = -EIO;
2416 dequeue_mid(mid, rdata->result);
2417 return 0;
2418 }
2419
2420 if (data_len > page_data_size - pad_len) {
2421 /* data_len is corrupt -- discard frame */
2422 rdata->result = -EIO;
2423 dequeue_mid(mid, rdata->result);
2424 return 0;
2425 }
2426
2427 rdata->result = init_read_bvec(pages, npages, page_data_size,
2428 cur_off, &bvec);
2429 if (rdata->result != 0) {
2430 dequeue_mid(mid, rdata->result);
2431 return 0;
2432 }
2433
2434 iov_iter_bvec(&iter, WRITE | ITER_BVEC, bvec, npages, data_len);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002435 } else if (buf_len >= data_offset + data_len) {
2436 /* read response payload is in buf */
2437 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
2438 iov.iov_base = buf + data_offset;
2439 iov.iov_len = data_len;
2440 iov_iter_kvec(&iter, WRITE | ITER_KVEC, &iov, 1, data_len);
2441 } else {
2442 /* read response payload cannot be in both buf and pages */
2443 WARN_ONCE(1, "buf can not contain only a part of read data");
2444 rdata->result = -EIO;
2445 dequeue_mid(mid, rdata->result);
2446 return 0;
2447 }
2448
2449 /* set up first iov for signature check */
2450 rdata->iov[0].iov_base = buf;
2451 rdata->iov[0].iov_len = 4;
2452 rdata->iov[1].iov_base = buf + 4;
2453 rdata->iov[1].iov_len = server->vals->read_rsp_size - 4;
2454 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
2455 rdata->iov[0].iov_base, server->vals->read_rsp_size);
2456
2457 length = rdata->copy_into_pages(server, rdata, &iter);
2458
2459 kfree(bvec);
2460
2461 if (length < 0)
2462 return length;
2463
2464 dequeue_mid(mid, false);
2465 return length;
2466}
2467
2468static int
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002469receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2470{
2471 char *buf = server->smallbuf;
2472 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2473 unsigned int npages;
2474 struct page **pages;
2475 unsigned int len;
2476 unsigned int buflen = get_rfc1002_length(buf) + 4;
2477 int rc;
2478 int i = 0;
2479
2480 len = min_t(unsigned int, buflen, server->vals->read_rsp_size - 4 +
2481 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
2482
2483 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
2484 if (rc < 0)
2485 return rc;
2486 server->total_read += rc;
2487
2488 len = le32_to_cpu(tr_hdr->OriginalMessageSize) + 4 -
2489 server->vals->read_rsp_size;
2490 npages = DIV_ROUND_UP(len, PAGE_SIZE);
2491
2492 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2493 if (!pages) {
2494 rc = -ENOMEM;
2495 goto discard_data;
2496 }
2497
2498 for (; i < npages; i++) {
2499 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2500 if (!pages[i]) {
2501 rc = -ENOMEM;
2502 goto discard_data;
2503 }
2504 }
2505
2506 /* read read data into pages */
2507 rc = read_data_into_pages(server, pages, npages, len);
2508 if (rc)
2509 goto free_pages;
2510
Pavel Shilovsky350be252017-04-10 10:31:33 -07002511 rc = cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002512 if (rc)
2513 goto free_pages;
2514
2515 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size - 4,
2516 pages, npages, len);
2517 if (rc)
2518 goto free_pages;
2519
2520 *mid = smb2_find_mid(server, buf);
2521 if (*mid == NULL)
2522 cifs_dbg(FYI, "mid not found\n");
2523 else {
2524 cifs_dbg(FYI, "mid found\n");
2525 (*mid)->decrypted = true;
2526 rc = handle_read_data(server, *mid, buf,
2527 server->vals->read_rsp_size,
2528 pages, npages, len);
2529 }
2530
2531free_pages:
2532 for (i = i - 1; i >= 0; i--)
2533 put_page(pages[i]);
2534 kfree(pages);
2535 return rc;
2536discard_data:
Pavel Shilovsky350be252017-04-10 10:31:33 -07002537 cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002538 goto free_pages;
2539}
2540
2541static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002542receive_encrypted_standard(struct TCP_Server_Info *server,
2543 struct mid_q_entry **mid)
2544{
2545 int length;
2546 char *buf = server->smallbuf;
2547 unsigned int pdu_length = get_rfc1002_length(buf);
2548 unsigned int buf_size;
2549 struct mid_q_entry *mid_entry;
2550
2551 /* switch to large buffer if too big for a small one */
2552 if (pdu_length + 4 > MAX_CIFS_SMALL_BUFFER_SIZE) {
2553 server->large_buf = true;
2554 memcpy(server->bigbuf, buf, server->total_read);
2555 buf = server->bigbuf;
2556 }
2557
2558 /* now read the rest */
2559 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
2560 pdu_length - HEADER_SIZE(server) + 1 + 4);
2561 if (length < 0)
2562 return length;
2563 server->total_read += length;
2564
2565 buf_size = pdu_length + 4 - sizeof(struct smb2_transform_hdr);
2566 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
2567 if (length)
2568 return length;
2569
2570 mid_entry = smb2_find_mid(server, buf);
2571 if (mid_entry == NULL)
2572 cifs_dbg(FYI, "mid not found\n");
2573 else {
2574 cifs_dbg(FYI, "mid found\n");
2575 mid_entry->decrypted = true;
2576 }
2577
2578 *mid = mid_entry;
2579
2580 if (mid_entry && mid_entry->handle)
2581 return mid_entry->handle(server, mid_entry);
2582
2583 return cifs_handle_standard(server, mid_entry);
2584}
2585
2586static int
2587smb3_receive_transform(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2588{
2589 char *buf = server->smallbuf;
2590 unsigned int pdu_length = get_rfc1002_length(buf);
2591 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2592 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2593
2594 if (pdu_length + 4 < sizeof(struct smb2_transform_hdr) +
2595 sizeof(struct smb2_sync_hdr)) {
2596 cifs_dbg(VFS, "Transform message is too small (%u)\n",
2597 pdu_length);
2598 cifs_reconnect(server);
2599 wake_up(&server->response_q);
2600 return -ECONNABORTED;
2601 }
2602
2603 if (pdu_length + 4 < orig_len + sizeof(struct smb2_transform_hdr)) {
2604 cifs_dbg(VFS, "Transform message is broken\n");
2605 cifs_reconnect(server);
2606 wake_up(&server->response_q);
2607 return -ECONNABORTED;
2608 }
2609
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002610 if (pdu_length + 4 > CIFSMaxBufSize + MAX_HEADER_SIZE(server))
2611 return receive_encrypted_read(server, mid);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002612
2613 return receive_encrypted_standard(server, mid);
2614}
2615
2616int
2617smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
2618{
2619 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
2620
2621 return handle_read_data(server, mid, buf, get_rfc1002_length(buf) + 4,
2622 NULL, 0, 0);
2623}
2624
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002625struct smb_version_operations smb20_operations = {
2626 .compare_fids = smb2_compare_fids,
2627 .setup_request = smb2_setup_request,
2628 .setup_async_request = smb2_setup_async_request,
2629 .check_receive = smb2_check_receive,
2630 .add_credits = smb2_add_credits,
2631 .set_credits = smb2_set_credits,
2632 .get_credits_field = smb2_get_credits_field,
2633 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002634 .wait_mtu_credits = cifs_wait_mtu_credits,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002635 .get_next_mid = smb2_get_next_mid,
2636 .read_data_offset = smb2_read_data_offset,
2637 .read_data_length = smb2_read_data_length,
2638 .map_error = map_smb2_to_linux_error,
2639 .find_mid = smb2_find_mid,
2640 .check_message = smb2_check_message,
2641 .dump_detail = smb2_dump_detail,
2642 .clear_stats = smb2_clear_stats,
2643 .print_stats = smb2_print_stats,
2644 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08002645 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002646 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002647 .need_neg = smb2_need_neg,
2648 .negotiate = smb2_negotiate,
2649 .negotiate_wsize = smb2_negotiate_wsize,
2650 .negotiate_rsize = smb2_negotiate_rsize,
2651 .sess_setup = SMB2_sess_setup,
2652 .logoff = SMB2_logoff,
2653 .tree_connect = SMB2_tcon,
2654 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05002655 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002656 .is_path_accessible = smb2_is_path_accessible,
2657 .can_echo = smb2_can_echo,
2658 .echo = SMB2_echo,
2659 .query_path_info = smb2_query_path_info,
2660 .get_srv_inum = smb2_get_srv_inum,
2661 .query_file_info = smb2_query_file_info,
2662 .set_path_size = smb2_set_path_size,
2663 .set_file_size = smb2_set_file_size,
2664 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05002665 .set_compression = smb2_set_compression,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002666 .mkdir = smb2_mkdir,
2667 .mkdir_setinfo = smb2_mkdir_setinfo,
2668 .rmdir = smb2_rmdir,
2669 .unlink = smb2_unlink,
2670 .rename = smb2_rename_path,
2671 .create_hardlink = smb2_create_hardlink,
2672 .query_symlink = smb2_query_symlink,
Sachin Prabhu5b23c972016-07-11 16:53:20 +01002673 .query_mf_symlink = smb3_query_mf_symlink,
2674 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002675 .open = smb2_open_file,
2676 .set_fid = smb2_set_fid,
2677 .close = smb2_close_file,
2678 .flush = smb2_flush_file,
2679 .async_readv = smb2_async_readv,
2680 .async_writev = smb2_async_writev,
2681 .sync_read = smb2_sync_read,
2682 .sync_write = smb2_sync_write,
2683 .query_dir_first = smb2_query_dir_first,
2684 .query_dir_next = smb2_query_dir_next,
2685 .close_dir = smb2_close_dir,
2686 .calc_smb_size = smb2_calc_size,
2687 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002688 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002689 .oplock_response = smb2_oplock_response,
2690 .queryfs = smb2_queryfs,
2691 .mand_lock = smb2_mand_lock,
2692 .mand_unlock_range = smb2_unlock_range,
2693 .push_mand_locks = smb2_push_mandatory_locks,
2694 .get_lease_key = smb2_get_lease_key,
2695 .set_lease_key = smb2_set_lease_key,
2696 .new_lease_key = smb2_new_lease_key,
2697 .calc_signature = smb2_calc_signature,
2698 .is_read_op = smb2_is_read_op,
2699 .set_oplock_level = smb2_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002700 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002701 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05002702 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002703 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04002704 .dir_needs_close = smb2_dir_needs_close,
Aurelien Aptel9d496402017-02-13 16:16:49 +01002705 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302706 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002707#ifdef CONFIG_CIFS_XATTR
2708 .query_all_EAs = smb2_query_eas,
2709#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002710#ifdef CONFIG_CIFS_ACL
2711 .get_acl = get_smb2_acl,
2712 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05002713 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002714#endif /* CIFS_ACL */
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002715};
2716
Steve French1080ef72011-02-24 18:07:19 +00002717struct smb_version_operations smb21_operations = {
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07002718 .compare_fids = smb2_compare_fids,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002719 .setup_request = smb2_setup_request,
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +04002720 .setup_async_request = smb2_setup_async_request,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002721 .check_receive = smb2_check_receive,
Pavel Shilovsky28ea5292012-05-23 16:18:00 +04002722 .add_credits = smb2_add_credits,
2723 .set_credits = smb2_set_credits,
2724 .get_credits_field = smb2_get_credits_field,
2725 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002726 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002727 .get_next_mid = smb2_get_next_mid,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002728 .read_data_offset = smb2_read_data_offset,
2729 .read_data_length = smb2_read_data_length,
2730 .map_error = map_smb2_to_linux_error,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04002731 .find_mid = smb2_find_mid,
2732 .check_message = smb2_check_message,
2733 .dump_detail = smb2_dump_detail,
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04002734 .clear_stats = smb2_clear_stats,
2735 .print_stats = smb2_print_stats,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002736 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08002737 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002738 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04002739 .need_neg = smb2_need_neg,
2740 .negotiate = smb2_negotiate,
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -07002741 .negotiate_wsize = smb2_negotiate_wsize,
2742 .negotiate_rsize = smb2_negotiate_rsize,
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04002743 .sess_setup = SMB2_sess_setup,
2744 .logoff = SMB2_logoff,
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04002745 .tree_connect = SMB2_tcon,
2746 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05002747 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002748 .is_path_accessible = smb2_is_path_accessible,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002749 .can_echo = smb2_can_echo,
2750 .echo = SMB2_echo,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002751 .query_path_info = smb2_query_path_info,
2752 .get_srv_inum = smb2_get_srv_inum,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -07002753 .query_file_info = smb2_query_file_info,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002754 .set_path_size = smb2_set_path_size,
2755 .set_file_size = smb2_set_file_size,
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07002756 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05002757 .set_compression = smb2_set_compression,
Pavel Shilovskya0e73182011-07-19 12:56:37 +04002758 .mkdir = smb2_mkdir,
2759 .mkdir_setinfo = smb2_mkdir_setinfo,
Pavel Shilovsky1a500f02012-07-10 16:14:38 +04002760 .rmdir = smb2_rmdir,
Pavel Shilovskycbe6f432012-09-18 16:20:25 -07002761 .unlink = smb2_unlink,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002762 .rename = smb2_rename_path,
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002763 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002764 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05002765 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05002766 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002767 .open = smb2_open_file,
2768 .set_fid = smb2_set_fid,
2769 .close = smb2_close_file,
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002770 .flush = smb2_flush_file,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002771 .async_readv = smb2_async_readv,
Pavel Shilovsky33319142012-09-18 16:20:29 -07002772 .async_writev = smb2_async_writev,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002773 .sync_read = smb2_sync_read,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002774 .sync_write = smb2_sync_write,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002775 .query_dir_first = smb2_query_dir_first,
2776 .query_dir_next = smb2_query_dir_next,
2777 .close_dir = smb2_close_dir,
2778 .calc_smb_size = smb2_calc_size,
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002779 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002780 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002781 .oplock_response = smb2_oplock_response,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002782 .queryfs = smb2_queryfs,
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07002783 .mand_lock = smb2_mand_lock,
2784 .mand_unlock_range = smb2_unlock_range,
Pavel Shilovskyb1407992012-09-19 06:22:44 -07002785 .push_mand_locks = smb2_push_mandatory_locks,
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002786 .get_lease_key = smb2_get_lease_key,
2787 .set_lease_key = smb2_set_lease_key,
2788 .new_lease_key = smb2_new_lease_key,
Steve French38107d42012-12-08 22:08:06 -06002789 .calc_signature = smb2_calc_signature,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002790 .is_read_op = smb21_is_read_op,
2791 .set_oplock_level = smb21_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002792 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002793 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05002794 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002795 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04002796 .dir_needs_close = smb2_dir_needs_close,
Steve French834170c2016-09-30 21:14:26 -05002797 .enum_snapshots = smb3_enum_snapshots,
Aurelien Aptel9d496402017-02-13 16:16:49 +01002798 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302799 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002800#ifdef CONFIG_CIFS_XATTR
2801 .query_all_EAs = smb2_query_eas,
2802#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002803#ifdef CONFIG_CIFS_ACL
2804 .get_acl = get_smb2_acl,
2805 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05002806 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002807#endif /* CIFS_ACL */
Steve French38107d42012-12-08 22:08:06 -06002808};
2809
Steve French38107d42012-12-08 22:08:06 -06002810struct smb_version_operations smb30_operations = {
2811 .compare_fids = smb2_compare_fids,
2812 .setup_request = smb2_setup_request,
2813 .setup_async_request = smb2_setup_async_request,
2814 .check_receive = smb2_check_receive,
2815 .add_credits = smb2_add_credits,
2816 .set_credits = smb2_set_credits,
2817 .get_credits_field = smb2_get_credits_field,
2818 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002819 .wait_mtu_credits = smb2_wait_mtu_credits,
Steve French38107d42012-12-08 22:08:06 -06002820 .get_next_mid = smb2_get_next_mid,
2821 .read_data_offset = smb2_read_data_offset,
2822 .read_data_length = smb2_read_data_length,
2823 .map_error = map_smb2_to_linux_error,
2824 .find_mid = smb2_find_mid,
2825 .check_message = smb2_check_message,
2826 .dump_detail = smb2_dump_detail,
2827 .clear_stats = smb2_clear_stats,
2828 .print_stats = smb2_print_stats,
Steve French769ee6a2013-06-19 14:15:30 -05002829 .dump_share_caps = smb2_dump_share_caps,
Steve French38107d42012-12-08 22:08:06 -06002830 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08002831 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002832 .downgrade_oplock = smb2_downgrade_oplock,
Steve French38107d42012-12-08 22:08:06 -06002833 .need_neg = smb2_need_neg,
2834 .negotiate = smb2_negotiate,
2835 .negotiate_wsize = smb2_negotiate_wsize,
2836 .negotiate_rsize = smb2_negotiate_rsize,
2837 .sess_setup = SMB2_sess_setup,
2838 .logoff = SMB2_logoff,
2839 .tree_connect = SMB2_tcon,
2840 .tree_disconnect = SMB2_tdis,
Steven Frenchaf6a12e2013-10-09 20:55:53 -05002841 .qfs_tcon = smb3_qfs_tcon,
Steve French38107d42012-12-08 22:08:06 -06002842 .is_path_accessible = smb2_is_path_accessible,
2843 .can_echo = smb2_can_echo,
2844 .echo = SMB2_echo,
2845 .query_path_info = smb2_query_path_info,
2846 .get_srv_inum = smb2_get_srv_inum,
2847 .query_file_info = smb2_query_file_info,
2848 .set_path_size = smb2_set_path_size,
2849 .set_file_size = smb2_set_file_size,
2850 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05002851 .set_compression = smb2_set_compression,
Steve French38107d42012-12-08 22:08:06 -06002852 .mkdir = smb2_mkdir,
2853 .mkdir_setinfo = smb2_mkdir_setinfo,
2854 .rmdir = smb2_rmdir,
2855 .unlink = smb2_unlink,
2856 .rename = smb2_rename_path,
2857 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002858 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05002859 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05002860 .create_mf_symlink = smb3_create_mf_symlink,
Steve French38107d42012-12-08 22:08:06 -06002861 .open = smb2_open_file,
2862 .set_fid = smb2_set_fid,
2863 .close = smb2_close_file,
2864 .flush = smb2_flush_file,
2865 .async_readv = smb2_async_readv,
2866 .async_writev = smb2_async_writev,
2867 .sync_read = smb2_sync_read,
2868 .sync_write = smb2_sync_write,
2869 .query_dir_first = smb2_query_dir_first,
2870 .query_dir_next = smb2_query_dir_next,
2871 .close_dir = smb2_close_dir,
2872 .calc_smb_size = smb2_calc_size,
2873 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002874 .is_session_expired = smb2_is_session_expired,
Steve French38107d42012-12-08 22:08:06 -06002875 .oplock_response = smb2_oplock_response,
2876 .queryfs = smb2_queryfs,
2877 .mand_lock = smb2_mand_lock,
2878 .mand_unlock_range = smb2_unlock_range,
2879 .push_mand_locks = smb2_push_mandatory_locks,
2880 .get_lease_key = smb2_get_lease_key,
2881 .set_lease_key = smb2_set_lease_key,
2882 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06002883 .generate_signingkey = generate_smb30signingkey,
Steve French38107d42012-12-08 22:08:06 -06002884 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05002885 .set_integrity = smb3_set_integrity,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002886 .is_read_op = smb21_is_read_op,
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002887 .set_oplock_level = smb3_set_oplock_level,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002888 .create_lease_buf = smb3_create_lease_buf,
2889 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05002890 .copychunk_range = smb2_copychunk_range,
Steve Frenchca9e7a12015-10-01 21:40:10 -05002891 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchff1c0382013-11-19 23:44:46 -06002892 .validate_negotiate = smb3_validate_negotiate,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002893 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04002894 .dir_needs_close = smb2_dir_needs_close,
Steve French31742c52014-08-17 08:38:47 -05002895 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05002896 .enum_snapshots = smb3_enum_snapshots,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002897 .init_transform_rq = smb3_init_transform_rq,
2898 .free_transform_rq = smb3_free_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002899 .is_transform_hdr = smb3_is_transform_hdr,
2900 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01002901 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302902 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002903#ifdef CONFIG_CIFS_XATTR
2904 .query_all_EAs = smb2_query_eas,
2905#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002906#ifdef CONFIG_CIFS_ACL
2907 .get_acl = get_smb2_acl,
2908 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05002909 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002910#endif /* CIFS_ACL */
Steve French1080ef72011-02-24 18:07:19 +00002911};
2912
Steve Frenchaab18932015-06-23 23:37:11 -05002913#ifdef CONFIG_CIFS_SMB311
2914struct smb_version_operations smb311_operations = {
2915 .compare_fids = smb2_compare_fids,
2916 .setup_request = smb2_setup_request,
2917 .setup_async_request = smb2_setup_async_request,
2918 .check_receive = smb2_check_receive,
2919 .add_credits = smb2_add_credits,
2920 .set_credits = smb2_set_credits,
2921 .get_credits_field = smb2_get_credits_field,
2922 .get_credits = smb2_get_credits,
2923 .wait_mtu_credits = smb2_wait_mtu_credits,
2924 .get_next_mid = smb2_get_next_mid,
2925 .read_data_offset = smb2_read_data_offset,
2926 .read_data_length = smb2_read_data_length,
2927 .map_error = map_smb2_to_linux_error,
2928 .find_mid = smb2_find_mid,
2929 .check_message = smb2_check_message,
2930 .dump_detail = smb2_dump_detail,
2931 .clear_stats = smb2_clear_stats,
2932 .print_stats = smb2_print_stats,
2933 .dump_share_caps = smb2_dump_share_caps,
2934 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08002935 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Steve Frenchaab18932015-06-23 23:37:11 -05002936 .downgrade_oplock = smb2_downgrade_oplock,
2937 .need_neg = smb2_need_neg,
2938 .negotiate = smb2_negotiate,
2939 .negotiate_wsize = smb2_negotiate_wsize,
2940 .negotiate_rsize = smb2_negotiate_rsize,
2941 .sess_setup = SMB2_sess_setup,
2942 .logoff = SMB2_logoff,
2943 .tree_connect = SMB2_tcon,
2944 .tree_disconnect = SMB2_tdis,
2945 .qfs_tcon = smb3_qfs_tcon,
2946 .is_path_accessible = smb2_is_path_accessible,
2947 .can_echo = smb2_can_echo,
2948 .echo = SMB2_echo,
2949 .query_path_info = smb2_query_path_info,
2950 .get_srv_inum = smb2_get_srv_inum,
2951 .query_file_info = smb2_query_file_info,
2952 .set_path_size = smb2_set_path_size,
2953 .set_file_size = smb2_set_file_size,
2954 .set_file_info = smb2_set_file_info,
2955 .set_compression = smb2_set_compression,
2956 .mkdir = smb2_mkdir,
2957 .mkdir_setinfo = smb2_mkdir_setinfo,
2958 .rmdir = smb2_rmdir,
2959 .unlink = smb2_unlink,
2960 .rename = smb2_rename_path,
2961 .create_hardlink = smb2_create_hardlink,
2962 .query_symlink = smb2_query_symlink,
2963 .query_mf_symlink = smb3_query_mf_symlink,
2964 .create_mf_symlink = smb3_create_mf_symlink,
2965 .open = smb2_open_file,
2966 .set_fid = smb2_set_fid,
2967 .close = smb2_close_file,
2968 .flush = smb2_flush_file,
2969 .async_readv = smb2_async_readv,
2970 .async_writev = smb2_async_writev,
2971 .sync_read = smb2_sync_read,
2972 .sync_write = smb2_sync_write,
2973 .query_dir_first = smb2_query_dir_first,
2974 .query_dir_next = smb2_query_dir_next,
2975 .close_dir = smb2_close_dir,
2976 .calc_smb_size = smb2_calc_size,
2977 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002978 .is_session_expired = smb2_is_session_expired,
Steve Frenchaab18932015-06-23 23:37:11 -05002979 .oplock_response = smb2_oplock_response,
2980 .queryfs = smb2_queryfs,
2981 .mand_lock = smb2_mand_lock,
2982 .mand_unlock_range = smb2_unlock_range,
2983 .push_mand_locks = smb2_push_mandatory_locks,
2984 .get_lease_key = smb2_get_lease_key,
2985 .set_lease_key = smb2_set_lease_key,
2986 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06002987 .generate_signingkey = generate_smb311signingkey,
Steve Frenchaab18932015-06-23 23:37:11 -05002988 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05002989 .set_integrity = smb3_set_integrity,
Steve Frenchaab18932015-06-23 23:37:11 -05002990 .is_read_op = smb21_is_read_op,
2991 .set_oplock_level = smb3_set_oplock_level,
2992 .create_lease_buf = smb3_create_lease_buf,
2993 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05002994 .copychunk_range = smb2_copychunk_range,
Steve French02b16662015-06-27 21:18:36 -07002995 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchaab18932015-06-23 23:37:11 -05002996/* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
2997 .wp_retry_size = smb2_wp_retry_size,
2998 .dir_needs_close = smb2_dir_needs_close,
2999 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05003000 .enum_snapshots = smb3_enum_snapshots,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003001 .init_transform_rq = smb3_init_transform_rq,
3002 .free_transform_rq = smb3_free_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003003 .is_transform_hdr = smb3_is_transform_hdr,
3004 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01003005 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05303006 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003007#ifdef CONFIG_CIFS_XATTR
3008 .query_all_EAs = smb2_query_eas,
3009#endif /* CIFS_XATTR */
Steve Frenchaab18932015-06-23 23:37:11 -05003010};
3011#endif /* CIFS_SMB311 */
3012
Steve Frenchdd446b12012-11-28 23:21:06 -06003013struct smb_version_values smb20_values = {
3014 .version_string = SMB20_VERSION_STRING,
3015 .protocol_id = SMB20_PROT_ID,
3016 .req_capabilities = 0, /* MBZ */
3017 .large_lock_type = 0,
3018 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3019 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3020 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3021 .header_size = sizeof(struct smb2_hdr),
3022 .max_header_size = MAX_SMB2_HDR_SIZE,
3023 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3024 .lock_cmd = SMB2_LOCK,
3025 .cap_unix = 0,
3026 .cap_nt_find = SMB2_NT_FIND,
3027 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003028 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3029 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003030 .create_lease_size = sizeof(struct create_lease),
Steve Frenchdd446b12012-11-28 23:21:06 -06003031};
3032
Steve French1080ef72011-02-24 18:07:19 +00003033struct smb_version_values smb21_values = {
3034 .version_string = SMB21_VERSION_STRING,
Steve Frenche4aa25e2012-10-01 12:26:22 -05003035 .protocol_id = SMB21_PROT_ID,
3036 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
3037 .large_lock_type = 0,
3038 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3039 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3040 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3041 .header_size = sizeof(struct smb2_hdr),
3042 .max_header_size = MAX_SMB2_HDR_SIZE,
3043 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3044 .lock_cmd = SMB2_LOCK,
3045 .cap_unix = 0,
3046 .cap_nt_find = SMB2_NT_FIND,
3047 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003048 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3049 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003050 .create_lease_size = sizeof(struct create_lease),
Steve Frenche4aa25e2012-10-01 12:26:22 -05003051};
3052
3053struct smb_version_values smb30_values = {
3054 .version_string = SMB30_VERSION_STRING,
3055 .protocol_id = SMB30_PROT_ID,
Steve French373512e2015-12-18 13:05:30 -06003056 .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 -07003057 .large_lock_type = 0,
3058 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3059 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3060 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04003061 .header_size = sizeof(struct smb2_hdr),
3062 .max_header_size = MAX_SMB2_HDR_SIZE,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003063 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04003064 .lock_cmd = SMB2_LOCK,
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04003065 .cap_unix = 0,
3066 .cap_nt_find = SMB2_NT_FIND,
3067 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003068 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3069 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003070 .create_lease_size = sizeof(struct create_lease_v2),
Steve French1080ef72011-02-24 18:07:19 +00003071};
Steve French20b6d8b2013-06-12 22:48:41 -05003072
3073struct smb_version_values smb302_values = {
3074 .version_string = SMB302_VERSION_STRING,
3075 .protocol_id = SMB302_PROT_ID,
Steve French373512e2015-12-18 13:05:30 -06003076 .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 -05003077 .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 Shilovskyf0473902013-09-04 13:44:05 +04003090 .create_lease_size = sizeof(struct create_lease_v2),
Steve French20b6d8b2013-06-12 22:48:41 -05003091};
Steve French5f7fbf72014-12-17 22:52:58 -06003092
3093#ifdef CONFIG_CIFS_SMB311
3094struct smb_version_values smb311_values = {
3095 .version_string = SMB311_VERSION_STRING,
3096 .protocol_id = SMB311_PROT_ID,
Steve French19558802017-06-21 16:50:20 -05003097 .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 -06003098 .large_lock_type = 0,
3099 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3100 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3101 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3102 .header_size = sizeof(struct smb2_hdr),
3103 .max_header_size = MAX_SMB2_HDR_SIZE,
3104 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3105 .lock_cmd = SMB2_LOCK,
3106 .cap_unix = 0,
3107 .cap_nt_find = SMB2_NT_FIND,
3108 .cap_large_files = SMB2_LARGE_FILES,
3109 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3110 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3111 .create_lease_size = sizeof(struct create_lease_v2),
3112};
3113#endif /* SMB311 */