]> git.ipfire.org Git - thirdparty/linux.git/blame - fs/cifs/smb2ops.c
CIFS: Introduce offset for the 1st page in data transfer structures
[thirdparty/linux.git] / fs / cifs / smb2ops.c
CommitLineData
1080ef75
SF
1/*
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
3a3bab50 20#include <linux/pagemap.h>
6fc05c25 21#include <linux/vfs.h>
f29ebb47 22#include <linux/falloc.h>
026e93dc 23#include <linux/scatterlist.h>
4fa8e504 24#include <linux/uuid.h>
026e93dc 25#include <crypto/aead.h>
1080ef75 26#include "cifsglob.h"
2dc7e1c0
PS
27#include "smb2pdu.h"
28#include "smb2proto.h"
28ea5290
PS
29#include "cifsproto.h"
30#include "cifs_debug.h"
b42bf888 31#include "cifs_unicode.h"
2e44b288 32#include "smb2status.h"
6fc05c25 33#include "smb2glob.h"
834170c8 34#include "cifs_ioctl.h"
09902f8d 35#include "smbdirect.h"
28ea5290
PS
36
37static int
38change_conf(struct TCP_Server_Info *server)
39{
40 server->credits += server->echo_credits + server->oplock_credits;
41 server->oplock_credits = server->echo_credits = 0;
42 switch (server->credits) {
43 case 0:
44 return -1;
45 case 1:
46 server->echoes = false;
47 server->oplocks = false;
f96637be 48 cifs_dbg(VFS, "disabling echoes and oplocks\n");
28ea5290
PS
49 break;
50 case 2:
51 server->echoes = true;
52 server->oplocks = false;
53 server->echo_credits = 1;
f96637be 54 cifs_dbg(FYI, "disabling oplocks\n");
28ea5290
PS
55 break;
56 default:
57 server->echoes = true;
e0ddde9d
SF
58 if (enable_oplocks) {
59 server->oplocks = true;
60 server->oplock_credits = 1;
61 } else
62 server->oplocks = false;
63
28ea5290 64 server->echo_credits = 1;
28ea5290
PS
65 }
66 server->credits -= server->echo_credits + server->oplock_credits;
67 return 0;
68}
69
70static void
71smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
72 const int optype)
73{
74 int *val, rc = 0;
75 spin_lock(&server->req_lock);
76 val = server->ops->get_credits_field(server, optype);
77 *val += add;
141891f4
SF
78 if (*val > 65000) {
79 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
80 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
81 }
28ea5290 82 server->in_flight--;
ec2e4523 83 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
28ea5290 84 rc = change_conf(server);
983c88a4
PS
85 /*
86 * Sometimes server returns 0 credits on oplock break ack - we need to
87 * rebalance credits in this case.
88 */
89 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
90 server->oplocks) {
91 if (server->credits > 1) {
92 server->credits--;
93 server->oplock_credits++;
94 }
95 }
28ea5290
PS
96 spin_unlock(&server->req_lock);
97 wake_up(&server->request_q);
98 if (rc)
99 cifs_reconnect(server);
100}
101
102static void
103smb2_set_credits(struct TCP_Server_Info *server, const int val)
104{
105 spin_lock(&server->req_lock);
106 server->credits = val;
107 spin_unlock(&server->req_lock);
108}
109
110static int *
111smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
112{
113 switch (optype) {
114 case CIFS_ECHO_OP:
115 return &server->echo_credits;
116 case CIFS_OBREAK_OP:
117 return &server->oplock_credits;
118 default:
119 return &server->credits;
120 }
121}
122
123static unsigned int
124smb2_get_credits(struct mid_q_entry *mid)
125{
31473fc4
PS
126 struct smb2_sync_hdr *shdr = get_sync_hdr(mid->resp_buf);
127
128 return le16_to_cpu(shdr->CreditRequest);
28ea5290 129}
2dc7e1c0 130
cb7e9eab
PS
131static int
132smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
133 unsigned int *num, unsigned int *credits)
134{
135 int rc = 0;
136 unsigned int scredits;
137
138 spin_lock(&server->req_lock);
139 while (1) {
140 if (server->credits <= 0) {
141 spin_unlock(&server->req_lock);
142 cifs_num_waiters_inc(server);
143 rc = wait_event_killable(server->request_q,
144 has_credits(server, &server->credits));
145 cifs_num_waiters_dec(server);
146 if (rc)
147 return rc;
148 spin_lock(&server->req_lock);
149 } else {
150 if (server->tcpStatus == CifsExiting) {
151 spin_unlock(&server->req_lock);
152 return -ENOENT;
153 }
154
155 scredits = server->credits;
156 /* can deadlock with reopen */
157 if (scredits == 1) {
158 *num = SMB2_MAX_BUFFER_SIZE;
159 *credits = 0;
160 break;
161 }
162
163 /* leave one credit for a possible reopen */
164 scredits--;
165 *num = min_t(unsigned int, size,
166 scredits * SMB2_MAX_BUFFER_SIZE);
167
168 *credits = DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
169 server->credits -= *credits;
170 server->in_flight++;
171 break;
172 }
173 }
174 spin_unlock(&server->req_lock);
175 return rc;
176}
177
2dc7e1c0
PS
178static __u64
179smb2_get_next_mid(struct TCP_Server_Info *server)
180{
181 __u64 mid;
182 /* for SMB2 we need the current value */
183 spin_lock(&GlobalMid_Lock);
184 mid = server->CurrentMid++;
185 spin_unlock(&GlobalMid_Lock);
186 return mid;
187}
1080ef75 188
093b2bda
PS
189static struct mid_q_entry *
190smb2_find_mid(struct TCP_Server_Info *server, char *buf)
191{
192 struct mid_q_entry *mid;
31473fc4
PS
193 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
194 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
093b2bda 195
31473fc4 196 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
373512ec
SF
197 cifs_dbg(VFS, "encrypted frame parsing not supported yet");
198 return NULL;
199 }
200
093b2bda
PS
201 spin_lock(&GlobalMid_Lock);
202 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
9235d098 203 if ((mid->mid == wire_mid) &&
093b2bda 204 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
31473fc4 205 (mid->command == shdr->Command)) {
093b2bda
PS
206 spin_unlock(&GlobalMid_Lock);
207 return mid;
208 }
209 }
210 spin_unlock(&GlobalMid_Lock);
211 return NULL;
212}
213
214static void
14547f7d 215smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
093b2bda
PS
216{
217#ifdef CONFIG_CIFS_DEBUG2
31473fc4 218 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
093b2bda 219
f96637be 220 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
31473fc4
PS
221 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
222 shdr->ProcessId);
14547f7d 223 cifs_dbg(VFS, "smb buf %p len %u\n", buf,
71992e62 224 server->ops->calc_smb_size(buf, server));
093b2bda
PS
225#endif
226}
227
ec2e4523
PS
228static bool
229smb2_need_neg(struct TCP_Server_Info *server)
230{
231 return server->max_read == 0;
232}
233
234static int
235smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
236{
237 int rc;
238 ses->server->CurrentMid = 0;
239 rc = SMB2_negotiate(xid, ses);
240 /* BB we probably don't need to retry with modern servers */
241 if (rc == -EAGAIN)
242 rc = -EHOSTDOWN;
243 return rc;
244}
245
3a3bab50
PS
246static unsigned int
247smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
248{
249 struct TCP_Server_Info *server = tcon->ses->server;
250 unsigned int wsize;
251
252 /* start with specified wsize, or default */
253 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
254 wsize = min_t(unsigned int, wsize, server->max_write);
09902f8d 255#ifdef CONFIG_CIFS_SMB_DIRECT
bb4c0419
LL
256 if (server->rdma) {
257 if (server->sign)
258 wsize = min_t(unsigned int,
259 wsize, server->smbd_conn->max_fragmented_send_size);
260 else
261 wsize = min_t(unsigned int,
09902f8d 262 wsize, server->smbd_conn->max_readwrite_size);
bb4c0419 263 }
09902f8d 264#endif
cb7e9eab
PS
265 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
266 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
3a3bab50 267
3a3bab50
PS
268 return wsize;
269}
270
271static unsigned int
272smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
273{
274 struct TCP_Server_Info *server = tcon->ses->server;
275 unsigned int rsize;
276
277 /* start with specified rsize, or default */
278 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
279 rsize = min_t(unsigned int, rsize, server->max_read);
09902f8d 280#ifdef CONFIG_CIFS_SMB_DIRECT
bb4c0419
LL
281 if (server->rdma) {
282 if (server->sign)
283 rsize = min_t(unsigned int,
284 rsize, server->smbd_conn->max_fragmented_recv_size);
285 else
286 rsize = min_t(unsigned int,
09902f8d 287 rsize, server->smbd_conn->max_readwrite_size);
bb4c0419 288 }
09902f8d 289#endif
bed9da02
PS
290
291 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
292 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
3a3bab50 293
3a3bab50
PS
294 return rsize;
295}
296
c481e9fe
SF
297#ifdef CONFIG_CIFS_STATS2
298static int
299SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
300{
301 int rc;
302 unsigned int ret_data_len = 0;
303 struct network_interface_info_ioctl_rsp *out_buf;
304
305 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
306 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
307 NULL /* no data input */, 0 /* no data input */,
308 (char **)&out_buf, &ret_data_len);
9ffc5412
SF
309 if (rc != 0)
310 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
311 else if (ret_data_len < sizeof(struct network_interface_info_ioctl_rsp)) {
312 cifs_dbg(VFS, "server returned bad net interface info buf\n");
313 rc = -EINVAL;
314 } else {
c481e9fe
SF
315 /* Dump info on first interface */
316 cifs_dbg(FYI, "Adapter Capability 0x%x\t",
317 le32_to_cpu(out_buf->Capability));
318 cifs_dbg(FYI, "Link Speed %lld\n",
319 le64_to_cpu(out_buf->LinkSpeed));
9ffc5412 320 }
24df1483 321 kfree(out_buf);
c481e9fe
SF
322 return rc;
323}
324#endif /* STATS2 */
325
3d4ef9a1
SF
326/*
327 * Open the directory at the root of a share
328 */
329int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
330{
331 struct cifs_open_parms oparams;
332 int rc;
333 __le16 srch_path = 0; /* Null - since an open of top of share */
334 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
335
336 mutex_lock(&tcon->prfid_mutex);
337 if (tcon->valid_root_fid) {
338 cifs_dbg(FYI, "found a cached root file handle\n");
339 memcpy(pfid, tcon->prfid, sizeof(struct cifs_fid));
340 mutex_unlock(&tcon->prfid_mutex);
341 return 0;
342 }
343
344 oparams.tcon = tcon;
345 oparams.create_options = 0;
346 oparams.desired_access = FILE_READ_ATTRIBUTES;
347 oparams.disposition = FILE_OPEN;
348 oparams.fid = pfid;
349 oparams.reconnect = false;
350
351 rc = SMB2_open(xid, &oparams, &srch_path, &oplock, NULL, NULL);
352 if (rc == 0) {
353 memcpy(tcon->prfid, pfid, sizeof(struct cifs_fid));
354 tcon->valid_root_fid = true;
355 }
356 mutex_unlock(&tcon->prfid_mutex);
357 return rc;
358}
359
af6a12ea
SF
360static void
361smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
362{
363 int rc;
364 __le16 srch_path = 0; /* Null - open root of share */
365 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
366 struct cifs_open_parms oparms;
367 struct cifs_fid fid;
3d4ef9a1 368 bool no_cached_open = tcon->nohandlecache;
af6a12ea
SF
369
370 oparms.tcon = tcon;
371 oparms.desired_access = FILE_READ_ATTRIBUTES;
372 oparms.disposition = FILE_OPEN;
373 oparms.create_options = 0;
374 oparms.fid = &fid;
375 oparms.reconnect = false;
376
3d4ef9a1
SF
377 if (no_cached_open)
378 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
379 else
380 rc = open_shroot(xid, tcon, &fid);
381
af6a12ea
SF
382 if (rc)
383 return;
384
c481e9fe
SF
385#ifdef CONFIG_CIFS_STATS2
386 SMB3_request_interfaces(xid, tcon);
387#endif /* STATS2 */
388
af6a12ea
SF
389 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
390 FS_ATTRIBUTE_INFORMATION);
391 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
392 FS_DEVICE_INFORMATION);
393 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
394 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
3d4ef9a1
SF
395 if (no_cached_open)
396 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
af6a12ea
SF
397 return;
398}
399
34f62640
SF
400static void
401smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
402{
403 int rc;
404 __le16 srch_path = 0; /* Null - open root of share */
405 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
406 struct cifs_open_parms oparms;
407 struct cifs_fid fid;
408
409 oparms.tcon = tcon;
410 oparms.desired_access = FILE_READ_ATTRIBUTES;
411 oparms.disposition = FILE_OPEN;
412 oparms.create_options = 0;
413 oparms.fid = &fid;
414 oparms.reconnect = false;
415
416 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
417 if (rc)
418 return;
419
2167114c
SF
420 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
421 FS_ATTRIBUTE_INFORMATION);
422 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
423 FS_DEVICE_INFORMATION);
34f62640
SF
424 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
425 return;
426}
427
2503a0db
PS
428static int
429smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
430 struct cifs_sb_info *cifs_sb, const char *full_path)
431{
432 int rc;
2503a0db 433 __le16 *utf16_path;
2e44b288 434 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
064f6047
PS
435 struct cifs_open_parms oparms;
436 struct cifs_fid fid;
2503a0db 437
3d4ef9a1
SF
438 if ((*full_path == 0) && tcon->valid_root_fid)
439 return 0;
440
2503a0db
PS
441 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
442 if (!utf16_path)
443 return -ENOMEM;
444
064f6047
PS
445 oparms.tcon = tcon;
446 oparms.desired_access = FILE_READ_ATTRIBUTES;
447 oparms.disposition = FILE_OPEN;
448 oparms.create_options = 0;
449 oparms.fid = &fid;
9cbc0b73 450 oparms.reconnect = false;
064f6047 451
b42bf888 452 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
2503a0db
PS
453 if (rc) {
454 kfree(utf16_path);
455 return rc;
456 }
457
064f6047 458 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2503a0db
PS
459 kfree(utf16_path);
460 return rc;
461}
462
be4cb9e3
PS
463static int
464smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
465 struct cifs_sb_info *cifs_sb, const char *full_path,
466 u64 *uniqueid, FILE_ALL_INFO *data)
467{
468 *uniqueid = le64_to_cpu(data->IndexNumber);
469 return 0;
470}
471
b7546bc5
PS
472static int
473smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
474 struct cifs_fid *fid, FILE_ALL_INFO *data)
475{
476 int rc;
477 struct smb2_file_all_info *smb2_data;
478
1bbe4997 479 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
b7546bc5
PS
480 GFP_KERNEL);
481 if (smb2_data == NULL)
482 return -ENOMEM;
483
484 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
485 smb2_data);
486 if (!rc)
487 move_smb2_info_to_cifs(data, smb2_data);
488 kfree(smb2_data);
489 return rc;
490}
491
1368f155 492#ifdef CONFIG_CIFS_XATTR
95907fea
RS
493static ssize_t
494move_smb2_ea_to_cifs(char *dst, size_t dst_size,
495 struct smb2_file_full_ea_info *src, size_t src_size,
496 const unsigned char *ea_name)
497{
498 int rc = 0;
499 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
500 char *name, *value;
501 size_t name_len, value_len, user_name_len;
502
503 while (src_size > 0) {
504 name = &src->ea_data[0];
505 name_len = (size_t)src->ea_name_length;
506 value = &src->ea_data[src->ea_name_length + 1];
507 value_len = (size_t)le16_to_cpu(src->ea_value_length);
508
509 if (name_len == 0) {
510 break;
511 }
512
513 if (src_size < 8 + name_len + 1 + value_len) {
514 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
515 rc = -EIO;
516 goto out;
517 }
518
519 if (ea_name) {
520 if (ea_name_len == name_len &&
521 memcmp(ea_name, name, name_len) == 0) {
522 rc = value_len;
523 if (dst_size == 0)
524 goto out;
525 if (dst_size < value_len) {
526 rc = -ERANGE;
527 goto out;
528 }
529 memcpy(dst, value, value_len);
530 goto out;
531 }
532 } else {
533 /* 'user.' plus a terminating null */
534 user_name_len = 5 + 1 + name_len;
535
536 rc += user_name_len;
537
538 if (dst_size >= user_name_len) {
539 dst_size -= user_name_len;
540 memcpy(dst, "user.", 5);
541 dst += 5;
542 memcpy(dst, src->ea_data, name_len);
543 dst += name_len;
544 *dst = 0;
545 ++dst;
546 } else if (dst_size == 0) {
547 /* skip copy - calc size only */
548 } else {
549 /* stop before overrun buffer */
550 rc = -ERANGE;
551 break;
552 }
553 }
554
555 if (!src->next_entry_offset)
556 break;
557
558 if (src_size < le32_to_cpu(src->next_entry_offset)) {
559 /* stop before overrun buffer */
560 rc = -ERANGE;
561 break;
562 }
563 src_size -= le32_to_cpu(src->next_entry_offset);
564 src = (void *)((char *)src +
565 le32_to_cpu(src->next_entry_offset));
566 }
567
568 /* didn't find the named attribute */
569 if (ea_name)
570 rc = -ENODATA;
571
572out:
573 return (ssize_t)rc;
574}
575
576static ssize_t
577smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
578 const unsigned char *path, const unsigned char *ea_name,
579 char *ea_data, size_t buf_size,
580 struct cifs_sb_info *cifs_sb)
581{
582 int rc;
583 __le16 *utf16_path;
584 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
585 struct cifs_open_parms oparms;
586 struct cifs_fid fid;
587 struct smb2_file_full_ea_info *smb2_data;
7cb3def4 588 int ea_buf_size = SMB2_MIN_EA_BUF;
95907fea
RS
589
590 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
591 if (!utf16_path)
592 return -ENOMEM;
593
594 oparms.tcon = tcon;
595 oparms.desired_access = FILE_READ_EA;
596 oparms.disposition = FILE_OPEN;
597 oparms.create_options = 0;
598 oparms.fid = &fid;
599 oparms.reconnect = false;
600
601 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
602 kfree(utf16_path);
603 if (rc) {
604 cifs_dbg(FYI, "open failed rc=%d\n", rc);
605 return rc;
606 }
607
7cb3def4
RS
608 while (1) {
609 smb2_data = kzalloc(ea_buf_size, GFP_KERNEL);
610 if (smb2_data == NULL) {
611 SMB2_close(xid, tcon, fid.persistent_fid,
612 fid.volatile_fid);
613 return -ENOMEM;
614 }
615
616 rc = SMB2_query_eas(xid, tcon, fid.persistent_fid,
617 fid.volatile_fid,
618 ea_buf_size, smb2_data);
619
620 if (rc != -E2BIG)
621 break;
622
623 kfree(smb2_data);
624 ea_buf_size <<= 1;
625
626 if (ea_buf_size > SMB2_MAX_EA_BUF) {
627 cifs_dbg(VFS, "EA size is too large\n");
628 SMB2_close(xid, tcon, fid.persistent_fid,
629 fid.volatile_fid);
630 return -ENOMEM;
631 }
95907fea
RS
632 }
633
95907fea
RS
634 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
635
ae2cd7fb
PA
636 /*
637 * If ea_name is NULL (listxattr) and there are no EAs, return 0 as it's
638 * not an error. Otherwise, the specified ea_name was not found.
639 */
95907fea
RS
640 if (!rc)
641 rc = move_smb2_ea_to_cifs(ea_data, buf_size, smb2_data,
642 SMB2_MAX_EA_BUF, ea_name);
ae2cd7fb
PA
643 else if (!ea_name && rc == -ENODATA)
644 rc = 0;
95907fea
RS
645
646 kfree(smb2_data);
647 return rc;
648}
649
5517554e
RS
650
651static int
652smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
653 const char *path, const char *ea_name, const void *ea_value,
654 const __u16 ea_value_len, const struct nls_table *nls_codepage,
655 struct cifs_sb_info *cifs_sb)
656{
657 int rc;
658 __le16 *utf16_path;
659 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
660 struct cifs_open_parms oparms;
661 struct cifs_fid fid;
662 struct smb2_file_full_ea_info *ea;
663 int ea_name_len = strlen(ea_name);
664 int len;
665
666 if (ea_name_len > 255)
667 return -EINVAL;
668
669 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
670 if (!utf16_path)
671 return -ENOMEM;
672
673 oparms.tcon = tcon;
674 oparms.desired_access = FILE_WRITE_EA;
675 oparms.disposition = FILE_OPEN;
676 oparms.create_options = 0;
677 oparms.fid = &fid;
678 oparms.reconnect = false;
679
680 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
681 kfree(utf16_path);
682 if (rc) {
683 cifs_dbg(FYI, "open failed rc=%d\n", rc);
684 return rc;
685 }
686
687 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
688 ea = kzalloc(len, GFP_KERNEL);
689 if (ea == NULL) {
690 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
691 return -ENOMEM;
692 }
693
694 ea->ea_name_length = ea_name_len;
695 ea->ea_value_length = cpu_to_le16(ea_value_len);
696 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
697 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
698
699 rc = SMB2_set_ea(xid, tcon, fid.persistent_fid, fid.volatile_fid, ea,
700 len);
701 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
702
703 return rc;
704}
1368f155 705#endif
5517554e 706
9094fad1
PS
707static bool
708smb2_can_echo(struct TCP_Server_Info *server)
709{
710 return server->echoes;
711}
712
d60622eb
PS
713static void
714smb2_clear_stats(struct cifs_tcon *tcon)
715{
716#ifdef CONFIG_CIFS_STATS
717 int i;
718 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
719 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
720 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
721 }
722#endif
723}
724
769ee6a4
SF
725static void
726smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
727{
728 seq_puts(m, "\n\tShare Capabilities:");
729 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
730 seq_puts(m, " DFS,");
731 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
732 seq_puts(m, " CONTINUOUS AVAILABILITY,");
733 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
734 seq_puts(m, " SCALEOUT,");
735 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
736 seq_puts(m, " CLUSTER,");
737 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
738 seq_puts(m, " ASYMMETRIC,");
739 if (tcon->capabilities == 0)
740 seq_puts(m, " None");
af6a12ea
SF
741 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
742 seq_puts(m, " Aligned,");
743 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
744 seq_puts(m, " Partition Aligned,");
745 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
746 seq_puts(m, " SSD,");
747 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
748 seq_puts(m, " TRIM-support,");
749
769ee6a4 750 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
af6a12ea
SF
751 if (tcon->perf_sector_size)
752 seq_printf(m, "\tOptimal sector size: 0x%x",
753 tcon->perf_sector_size);
769ee6a4
SF
754}
755
d60622eb
PS
756static void
757smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
758{
759#ifdef CONFIG_CIFS_STATS
760 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
761 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
762 seq_printf(m, "\nNegotiates: %d sent %d failed",
763 atomic_read(&sent[SMB2_NEGOTIATE_HE]),
764 atomic_read(&failed[SMB2_NEGOTIATE_HE]));
765 seq_printf(m, "\nSessionSetups: %d sent %d failed",
766 atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
767 atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
d60622eb
PS
768 seq_printf(m, "\nLogoffs: %d sent %d failed",
769 atomic_read(&sent[SMB2_LOGOFF_HE]),
770 atomic_read(&failed[SMB2_LOGOFF_HE]));
771 seq_printf(m, "\nTreeConnects: %d sent %d failed",
772 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
773 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
774 seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
775 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
776 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
777 seq_printf(m, "\nCreates: %d sent %d failed",
778 atomic_read(&sent[SMB2_CREATE_HE]),
779 atomic_read(&failed[SMB2_CREATE_HE]));
780 seq_printf(m, "\nCloses: %d sent %d failed",
781 atomic_read(&sent[SMB2_CLOSE_HE]),
782 atomic_read(&failed[SMB2_CLOSE_HE]));
783 seq_printf(m, "\nFlushes: %d sent %d failed",
784 atomic_read(&sent[SMB2_FLUSH_HE]),
785 atomic_read(&failed[SMB2_FLUSH_HE]));
786 seq_printf(m, "\nReads: %d sent %d failed",
787 atomic_read(&sent[SMB2_READ_HE]),
788 atomic_read(&failed[SMB2_READ_HE]));
789 seq_printf(m, "\nWrites: %d sent %d failed",
790 atomic_read(&sent[SMB2_WRITE_HE]),
791 atomic_read(&failed[SMB2_WRITE_HE]));
792 seq_printf(m, "\nLocks: %d sent %d failed",
793 atomic_read(&sent[SMB2_LOCK_HE]),
794 atomic_read(&failed[SMB2_LOCK_HE]));
795 seq_printf(m, "\nIOCTLs: %d sent %d failed",
796 atomic_read(&sent[SMB2_IOCTL_HE]),
797 atomic_read(&failed[SMB2_IOCTL_HE]));
798 seq_printf(m, "\nCancels: %d sent %d failed",
799 atomic_read(&sent[SMB2_CANCEL_HE]),
800 atomic_read(&failed[SMB2_CANCEL_HE]));
801 seq_printf(m, "\nEchos: %d sent %d failed",
802 atomic_read(&sent[SMB2_ECHO_HE]),
803 atomic_read(&failed[SMB2_ECHO_HE]));
804 seq_printf(m, "\nQueryDirectories: %d sent %d failed",
805 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
806 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
807 seq_printf(m, "\nChangeNotifies: %d sent %d failed",
808 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
809 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
810 seq_printf(m, "\nQueryInfos: %d sent %d failed",
811 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
812 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
813 seq_printf(m, "\nSetInfos: %d sent %d failed",
814 atomic_read(&sent[SMB2_SET_INFO_HE]),
815 atomic_read(&failed[SMB2_SET_INFO_HE]));
816 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
817 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
818 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
819#endif
820}
821
f0df737e
PS
822static void
823smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
824{
2b0143b5 825 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
53ef1016
PS
826 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
827
f0df737e
PS
828 cfile->fid.persistent_fid = fid->persistent_fid;
829 cfile->fid.volatile_fid = fid->volatile_fid;
42873b0a
PS
830 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
831 &fid->purge_cache);
18cceb6a 832 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
94f87371 833 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
f0df737e
PS
834}
835
760ad0ca 836static void
f0df737e
PS
837smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
838 struct cifs_fid *fid)
839{
760ad0ca 840 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
f0df737e
PS
841}
842
41c1358e
SF
843static int
844SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
845 u64 persistent_fid, u64 volatile_fid,
846 struct copychunk_ioctl *pcchunk)
847{
848 int rc;
849 unsigned int ret_data_len;
850 struct resume_key_req *res_key;
851
852 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
853 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
854 NULL, 0 /* no input */,
855 (char **)&res_key, &ret_data_len);
856
857 if (rc) {
858 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
859 goto req_res_key_exit;
860 }
861 if (ret_data_len < sizeof(struct resume_key_req)) {
862 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
863 rc = -EINVAL;
864 goto req_res_key_exit;
865 }
866 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
867
868req_res_key_exit:
869 kfree(res_key);
870 return rc;
871}
872
620d8745 873static ssize_t
312bbc59 874smb2_copychunk_range(const unsigned int xid,
41c1358e
SF
875 struct cifsFileInfo *srcfile,
876 struct cifsFileInfo *trgtfile, u64 src_off,
877 u64 len, u64 dest_off)
878{
879 int rc;
880 unsigned int ret_data_len;
881 struct copychunk_ioctl *pcchunk;
9bf0c9cd
SF
882 struct copychunk_ioctl_rsp *retbuf = NULL;
883 struct cifs_tcon *tcon;
884 int chunks_copied = 0;
885 bool chunk_sizes_updated = false;
620d8745 886 ssize_t bytes_written, total_bytes_written = 0;
41c1358e
SF
887
888 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
889
890 if (pcchunk == NULL)
891 return -ENOMEM;
892
312bbc59 893 cifs_dbg(FYI, "in smb2_copychunk_range - about to call request res key\n");
41c1358e
SF
894 /* Request a key from the server to identify the source of the copy */
895 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
896 srcfile->fid.persistent_fid,
897 srcfile->fid.volatile_fid, pcchunk);
898
899 /* Note: request_res_key sets res_key null only if rc !=0 */
900 if (rc)
9bf0c9cd 901 goto cchunk_out;
41c1358e
SF
902
903 /* For now array only one chunk long, will make more flexible later */
bc09d141 904 pcchunk->ChunkCount = cpu_to_le32(1);
41c1358e 905 pcchunk->Reserved = 0;
41c1358e
SF
906 pcchunk->Reserved2 = 0;
907
9bf0c9cd 908 tcon = tlink_tcon(trgtfile->tlink);
41c1358e 909
9bf0c9cd
SF
910 while (len > 0) {
911 pcchunk->SourceOffset = cpu_to_le64(src_off);
912 pcchunk->TargetOffset = cpu_to_le64(dest_off);
913 pcchunk->Length =
914 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
41c1358e 915
9bf0c9cd
SF
916 /* Request server copy to target from src identified by key */
917 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
918 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
63a83b86 919 true /* is_fsctl */, (char *)pcchunk,
9bf0c9cd
SF
920 sizeof(struct copychunk_ioctl), (char **)&retbuf,
921 &ret_data_len);
922 if (rc == 0) {
923 if (ret_data_len !=
924 sizeof(struct copychunk_ioctl_rsp)) {
925 cifs_dbg(VFS, "invalid cchunk response size\n");
926 rc = -EIO;
927 goto cchunk_out;
928 }
929 if (retbuf->TotalBytesWritten == 0) {
930 cifs_dbg(FYI, "no bytes copied\n");
931 rc = -EIO;
932 goto cchunk_out;
933 }
934 /*
935 * Check if server claimed to write more than we asked
936 */
937 if (le32_to_cpu(retbuf->TotalBytesWritten) >
938 le32_to_cpu(pcchunk->Length)) {
939 cifs_dbg(VFS, "invalid copy chunk response\n");
940 rc = -EIO;
941 goto cchunk_out;
942 }
943 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
944 cifs_dbg(VFS, "invalid num chunks written\n");
945 rc = -EIO;
946 goto cchunk_out;
947 }
948 chunks_copied++;
949
620d8745
SP
950 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
951 src_off += bytes_written;
952 dest_off += bytes_written;
953 len -= bytes_written;
954 total_bytes_written += bytes_written;
9bf0c9cd 955
620d8745 956 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
9bf0c9cd
SF
957 le32_to_cpu(retbuf->ChunksWritten),
958 le32_to_cpu(retbuf->ChunkBytesWritten),
620d8745 959 bytes_written);
9bf0c9cd
SF
960 } else if (rc == -EINVAL) {
961 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
962 goto cchunk_out;
963
964 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
965 le32_to_cpu(retbuf->ChunksWritten),
966 le32_to_cpu(retbuf->ChunkBytesWritten),
967 le32_to_cpu(retbuf->TotalBytesWritten));
968
969 /*
970 * Check if this is the first request using these sizes,
971 * (ie check if copy succeed once with original sizes
972 * and check if the server gave us different sizes after
973 * we already updated max sizes on previous request).
974 * if not then why is the server returning an error now
975 */
976 if ((chunks_copied != 0) || chunk_sizes_updated)
977 goto cchunk_out;
978
979 /* Check that server is not asking us to grow size */
980 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
981 tcon->max_bytes_chunk)
982 tcon->max_bytes_chunk =
983 le32_to_cpu(retbuf->ChunkBytesWritten);
984 else
985 goto cchunk_out; /* server gave us bogus size */
986
987 /* No need to change MaxChunks since already set to 1 */
988 chunk_sizes_updated = true;
2477bc58
SP
989 } else
990 goto cchunk_out;
9bf0c9cd 991 }
41c1358e 992
9bf0c9cd 993cchunk_out:
41c1358e 994 kfree(pcchunk);
24df1483 995 kfree(retbuf);
620d8745
SP
996 if (rc)
997 return rc;
998 else
999 return total_bytes_written;
41c1358e
SF
1000}
1001
7a5cfb19
PS
1002static int
1003smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1004 struct cifs_fid *fid)
1005{
1006 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1007}
1008
09a4707e
PS
1009static unsigned int
1010smb2_read_data_offset(char *buf)
1011{
1012 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1013 return rsp->DataOffset;
1014}
1015
1016static unsigned int
74dcf418 1017smb2_read_data_length(char *buf, bool in_remaining)
09a4707e
PS
1018{
1019 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
74dcf418
LL
1020
1021 if (in_remaining)
1022 return le32_to_cpu(rsp->DataRemaining);
1023
09a4707e
PS
1024 return le32_to_cpu(rsp->DataLength);
1025}
1026
d8e05039
PS
1027
1028static int
db8b631d 1029smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
d8e05039
PS
1030 struct cifs_io_parms *parms, unsigned int *bytes_read,
1031 char **buf, int *buf_type)
1032{
db8b631d
SF
1033 parms->persistent_fid = pfid->persistent_fid;
1034 parms->volatile_fid = pfid->volatile_fid;
d8e05039
PS
1035 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1036}
1037
009d3443 1038static int
db8b631d 1039smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
009d3443
PS
1040 struct cifs_io_parms *parms, unsigned int *written,
1041 struct kvec *iov, unsigned long nr_segs)
1042{
1043
db8b631d
SF
1044 parms->persistent_fid = pfid->persistent_fid;
1045 parms->volatile_fid = pfid->volatile_fid;
009d3443
PS
1046 return SMB2_write(xid, parms, written, iov, nr_segs);
1047}
1048
d43cc793
SF
1049/* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1050static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1051 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1052{
1053 struct cifsInodeInfo *cifsi;
1054 int rc;
1055
1056 cifsi = CIFS_I(inode);
1057
1058 /* if file already sparse don't bother setting sparse again */
1059 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1060 return true; /* already sparse */
1061
1062 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1063 return true; /* already not sparse */
1064
1065 /*
1066 * Can't check for sparse support on share the usual way via the
1067 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1068 * since Samba server doesn't set the flag on the share, yet
1069 * supports the set sparse FSCTL and returns sparse correctly
1070 * in the file attributes. If we fail setting sparse though we
1071 * mark that server does not support sparse files for this share
1072 * to avoid repeatedly sending the unsupported fsctl to server
1073 * if the file is repeatedly extended.
1074 */
1075 if (tcon->broken_sparse_sup)
1076 return false;
1077
1078 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1079 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
63a83b86 1080 true /* is_fctl */,
51146625 1081 &setsparse, 1, NULL, NULL);
d43cc793
SF
1082 if (rc) {
1083 tcon->broken_sparse_sup = true;
1084 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1085 return false;
1086 }
1087
1088 if (setsparse)
1089 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1090 else
1091 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1092
1093 return true;
1094}
1095
c839ff24
PS
1096static int
1097smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1098 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1099{
1100 __le64 eof = cpu_to_le64(size);
3d1a3745
SF
1101 struct inode *inode;
1102
1103 /*
1104 * If extending file more than one page make sparse. Many Linux fs
1105 * make files sparse by default when extending via ftruncate
1106 */
2b0143b5 1107 inode = d_inode(cfile->dentry);
3d1a3745
SF
1108
1109 if (!set_alloc && (size > inode->i_size + 8192)) {
3d1a3745 1110 __u8 set_sparse = 1;
d43cc793
SF
1111
1112 /* whether set sparse succeeds or not, extend the file */
1113 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
3d1a3745
SF
1114 }
1115
c839ff24 1116 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
f29ebb47 1117 cfile->fid.volatile_fid, cfile->pid, &eof, false);
c839ff24
PS
1118}
1119
02b16665
SF
1120static int
1121smb2_duplicate_extents(const unsigned int xid,
1122 struct cifsFileInfo *srcfile,
1123 struct cifsFileInfo *trgtfile, u64 src_off,
1124 u64 len, u64 dest_off)
1125{
1126 int rc;
1127 unsigned int ret_data_len;
02b16665
SF
1128 struct duplicate_extents_to_file dup_ext_buf;
1129 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1130
1131 /* server fileays advertise duplicate extent support with this flag */
1132 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1133 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1134 return -EOPNOTSUPP;
1135
1136 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1137 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1138 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1139 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1140 dup_ext_buf.ByteCount = cpu_to_le64(len);
1141 cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
1142 src_off, dest_off, len);
1143
1144 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1145 if (rc)
1146 goto duplicate_extents_out;
1147
1148 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1149 trgtfile->fid.volatile_fid,
1150 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
63a83b86 1151 true /* is_fsctl */,
51146625 1152 (char *)&dup_ext_buf,
02b16665 1153 sizeof(struct duplicate_extents_to_file),
24df1483 1154 NULL,
02b16665
SF
1155 &ret_data_len);
1156
1157 if (ret_data_len > 0)
1158 cifs_dbg(FYI, "non-zero response length in duplicate extents");
1159
1160duplicate_extents_out:
1161 return rc;
1162}
02b16665 1163
64a5cfa6
SF
1164static int
1165smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1166 struct cifsFileInfo *cfile)
1167{
1168 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1169 cfile->fid.volatile_fid);
1170}
1171
b3152e2c
SF
1172static int
1173smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1174 struct cifsFileInfo *cfile)
1175{
1176 struct fsctl_set_integrity_information_req integr_info;
b3152e2c
SF
1177 unsigned int ret_data_len;
1178
1179 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1180 integr_info.Flags = 0;
1181 integr_info.Reserved = 0;
1182
1183 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1184 cfile->fid.volatile_fid,
1185 FSCTL_SET_INTEGRITY_INFORMATION,
63a83b86 1186 true /* is_fsctl */,
51146625 1187 (char *)&integr_info,
b3152e2c 1188 sizeof(struct fsctl_set_integrity_information_req),
24df1483 1189 NULL,
b3152e2c
SF
1190 &ret_data_len);
1191
1192}
1193
834170c8
SF
1194static int
1195smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1196 struct cifsFileInfo *cfile, void __user *ioc_buf)
1197{
1198 char *retbuf = NULL;
1199 unsigned int ret_data_len = 0;
1200 int rc;
1201 struct smb_snapshot_array snapshot_in;
1202
1203 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1204 cfile->fid.volatile_fid,
1205 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
63a83b86 1206 true /* is_fsctl */,
51146625 1207 NULL, 0 /* no input data */,
834170c8
SF
1208 (char **)&retbuf,
1209 &ret_data_len);
1210 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1211 rc, ret_data_len);
1212 if (rc)
1213 return rc;
1214
1215 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1216 /* Fixup buffer */
1217 if (copy_from_user(&snapshot_in, ioc_buf,
1218 sizeof(struct smb_snapshot_array))) {
1219 rc = -EFAULT;
1220 kfree(retbuf);
1221 return rc;
1222 }
1223 if (snapshot_in.snapshot_array_size < sizeof(struct smb_snapshot_array)) {
1224 rc = -ERANGE;
0e5c7955 1225 kfree(retbuf);
834170c8
SF
1226 return rc;
1227 }
1228
1229 if (ret_data_len > snapshot_in.snapshot_array_size)
1230 ret_data_len = snapshot_in.snapshot_array_size;
1231
1232 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1233 rc = -EFAULT;
1234 }
1235
1236 kfree(retbuf);
1237 return rc;
1238}
1239
d324f08d
PS
1240static int
1241smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1242 const char *path, struct cifs_sb_info *cifs_sb,
1243 struct cifs_fid *fid, __u16 search_flags,
1244 struct cifs_search_info *srch_inf)
1245{
1246 __le16 *utf16_path;
1247 int rc;
2e44b288 1248 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
064f6047 1249 struct cifs_open_parms oparms;
d324f08d
PS
1250
1251 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1252 if (!utf16_path)
1253 return -ENOMEM;
1254
064f6047
PS
1255 oparms.tcon = tcon;
1256 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1257 oparms.disposition = FILE_OPEN;
1258 oparms.create_options = 0;
1259 oparms.fid = fid;
9cbc0b73 1260 oparms.reconnect = false;
064f6047 1261
b42bf888 1262 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
d324f08d
PS
1263 kfree(utf16_path);
1264 if (rc) {
dcd87838 1265 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
d324f08d
PS
1266 return rc;
1267 }
1268
1269 srch_inf->entries_in_buffer = 0;
1270 srch_inf->index_of_last_entry = 0;
d324f08d 1271
064f6047
PS
1272 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1273 fid->volatile_fid, 0, srch_inf);
d324f08d 1274 if (rc) {
dcd87838 1275 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
064f6047 1276 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
d324f08d
PS
1277 }
1278 return rc;
1279}
1280
1281static int
1282smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1283 struct cifs_fid *fid, __u16 search_flags,
1284 struct cifs_search_info *srch_inf)
1285{
1286 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1287 fid->volatile_fid, 0, srch_inf);
1288}
1289
1290static int
1291smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1292 struct cifs_fid *fid)
1293{
1294 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1295}
1296
2e44b288
PS
1297/*
1298* If we negotiate SMB2 protocol and get STATUS_PENDING - update
1299* the number of credits and return true. Otherwise - return false.
1300*/
1301static bool
1302smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
1303{
31473fc4 1304 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
2e44b288 1305
31473fc4 1306 if (shdr->Status != STATUS_PENDING)
2e44b288
PS
1307 return false;
1308
1309 if (!length) {
1310 spin_lock(&server->req_lock);
31473fc4 1311 server->credits += le16_to_cpu(shdr->CreditRequest);
2e44b288
PS
1312 spin_unlock(&server->req_lock);
1313 wake_up(&server->request_q);
1314 }
1315
1316 return true;
1317}
1318
511c54a2
PS
1319static bool
1320smb2_is_session_expired(char *buf)
1321{
1322 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
1323
1324 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED)
1325 return false;
1326
1327 cifs_dbg(FYI, "Session expired\n");
1328 return true;
1329}
1330
983c88a4
PS
1331static int
1332smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
1333 struct cifsInodeInfo *cinode)
1334{
0822f514
PS
1335 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
1336 return SMB2_lease_break(0, tcon, cinode->lease_key,
1337 smb2_get_lease_state(cinode));
1338
983c88a4
PS
1339 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
1340 fid->volatile_fid,
18cceb6a 1341 CIFS_CACHE_READ(cinode) ? 1 : 0);
983c88a4
PS
1342}
1343
6fc05c25
PS
1344static int
1345smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1346 struct kstatfs *buf)
1347{
1348 int rc;
6fc05c25
PS
1349 __le16 srch_path = 0; /* Null - open root of share */
1350 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
064f6047
PS
1351 struct cifs_open_parms oparms;
1352 struct cifs_fid fid;
1353
1354 oparms.tcon = tcon;
1355 oparms.desired_access = FILE_READ_ATTRIBUTES;
1356 oparms.disposition = FILE_OPEN;
1357 oparms.create_options = 0;
1358 oparms.fid = &fid;
9cbc0b73 1359 oparms.reconnect = false;
6fc05c25 1360
b42bf888 1361 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
6fc05c25
PS
1362 if (rc)
1363 return rc;
1364 buf->f_type = SMB2_MAGIC_NUMBER;
064f6047
PS
1365 rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1366 buf);
1367 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
6fc05c25
PS
1368 return rc;
1369}
1370
027e8eec
PS
1371static bool
1372smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
1373{
1374 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
1375 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
1376}
1377
f7ba7fe6
PS
1378static int
1379smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
1380 __u64 length, __u32 type, int lock, int unlock, bool wait)
1381{
1382 if (unlock && !lock)
1383 type = SMB2_LOCKFLAG_UNLOCK;
1384 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
1385 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
1386 current->tgid, length, offset, type, wait);
1387}
1388
b8c32dbb
PS
1389static void
1390smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
1391{
1392 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
1393}
1394
1395static void
1396smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
1397{
1398 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
1399}
1400
1401static void
1402smb2_new_lease_key(struct cifs_fid *fid)
1403{
fa70b87c 1404 generate_random_uuid(fid->lease_key);
b8c32dbb
PS
1405}
1406
9d49640a
AA
1407static int
1408smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
1409 const char *search_name,
1410 struct dfs_info3_param **target_nodes,
1411 unsigned int *num_of_nodes,
1412 const struct nls_table *nls_codepage, int remap)
1413{
1414 int rc;
1415 __le16 *utf16_path = NULL;
1416 int utf16_path_len = 0;
1417 struct cifs_tcon *tcon;
1418 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
1419 struct get_dfs_referral_rsp *dfs_rsp = NULL;
1420 u32 dfs_req_size = 0, dfs_rsp_size = 0;
1421
1422 cifs_dbg(FYI, "smb2_get_dfs_refer path <%s>\n", search_name);
1423
1424 /*
63a83b86 1425 * Try to use the IPC tcon, otherwise just use any
9d49640a 1426 */
63a83b86
AA
1427 tcon = ses->tcon_ipc;
1428 if (tcon == NULL) {
1429 spin_lock(&cifs_tcp_ses_lock);
1430 tcon = list_first_entry_or_null(&ses->tcon_list,
1431 struct cifs_tcon,
1432 tcon_list);
1433 if (tcon)
1434 tcon->tc_count++;
1435 spin_unlock(&cifs_tcp_ses_lock);
1436 }
1437
1438 if (tcon == NULL) {
9d49640a
AA
1439 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
1440 ses);
1441 rc = -ENOTCONN;
1442 goto out;
1443 }
1444
1445 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
1446 &utf16_path_len,
1447 nls_codepage, remap);
1448 if (!utf16_path) {
1449 rc = -ENOMEM;
1450 goto out;
1451 }
1452
1453 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
1454 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
1455 if (!dfs_req) {
1456 rc = -ENOMEM;
1457 goto out;
1458 }
1459
1460 /* Highest DFS referral version understood */
1461 dfs_req->MaxReferralLevel = DFS_VERSION;
1462
1463 /* Path to resolve in an UTF-16 null-terminated string */
1464 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
1465
1466 do {
9d49640a
AA
1467 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1468 FSCTL_DFS_GET_REFERRALS,
63a83b86 1469 true /* is_fsctl */,
9d49640a
AA
1470 (char *)dfs_req, dfs_req_size,
1471 (char **)&dfs_rsp, &dfs_rsp_size);
9d49640a
AA
1472 } while (rc == -EAGAIN);
1473
1474 if (rc) {
2564f2ff 1475 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
5702591f 1476 cifs_dbg(VFS, "ioctl error in smb2_get_dfs_refer rc=%d\n", rc);
9d49640a
AA
1477 goto out;
1478 }
1479
1480 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
1481 num_of_nodes, target_nodes,
1482 nls_codepage, remap, search_name,
1483 true /* is_unicode */);
1484 if (rc) {
1485 cifs_dbg(VFS, "parse error in smb2_get_dfs_refer rc=%d\n", rc);
1486 goto out;
1487 }
1488
1489 out:
63a83b86
AA
1490 if (tcon && !tcon->ipc) {
1491 /* ipc tcons are not refcounted */
9d49640a
AA
1492 spin_lock(&cifs_tcp_ses_lock);
1493 tcon->tc_count--;
1494 spin_unlock(&cifs_tcp_ses_lock);
1495 }
1496 kfree(utf16_path);
1497 kfree(dfs_req);
1498 kfree(dfs_rsp);
1499 return rc;
1500}
7893242e
PS
1501#define SMB2_SYMLINK_STRUCT_SIZE \
1502 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
1503
b42bf888
PS
1504static int
1505smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
1506 const char *full_path, char **target_path,
1507 struct cifs_sb_info *cifs_sb)
1508{
1509 int rc;
1510 __le16 *utf16_path;
1511 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1512 struct cifs_open_parms oparms;
1513 struct cifs_fid fid;
91cb74f5 1514 struct kvec err_iov = {NULL, 0};
0d568cd3 1515 struct smb2_err_rsp *err_buf;
b42bf888 1516 struct smb2_symlink_err_rsp *symlink;
7893242e
PS
1517 unsigned int sub_len;
1518 unsigned int sub_offset;
1519 unsigned int print_len;
1520 unsigned int print_offset;
93012bf9
RS
1521 struct cifs_ses *ses = tcon->ses;
1522 struct TCP_Server_Info *server = ses->server;
b42bf888
PS
1523
1524 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
1525
1526 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1527 if (!utf16_path)
1528 return -ENOMEM;
1529
1530 oparms.tcon = tcon;
1531 oparms.desired_access = FILE_READ_ATTRIBUTES;
1532 oparms.disposition = FILE_OPEN;
1533 oparms.create_options = 0;
1534 oparms.fid = &fid;
1535 oparms.reconnect = false;
1536
91cb74f5 1537 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_iov);
b42bf888 1538
0d568cd3 1539 if (!rc || !err_iov.iov_base) {
b42bf888
PS
1540 kfree(utf16_path);
1541 return -ENOENT;
1542 }
7893242e 1543
91cb74f5 1544 err_buf = err_iov.iov_base;
7893242e 1545 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
91cb74f5 1546 err_iov.iov_len + server->vals->header_preamble_size < SMB2_SYMLINK_STRUCT_SIZE) {
7893242e
PS
1547 kfree(utf16_path);
1548 return -ENOENT;
1549 }
1550
b42bf888
PS
1551 /* open must fail on symlink - reset rc */
1552 rc = 0;
1553 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
1554 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
1555 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
7893242e
PS
1556 print_len = le16_to_cpu(symlink->PrintNameLength);
1557 print_offset = le16_to_cpu(symlink->PrintNameOffset);
1558
91cb74f5 1559 if (err_iov.iov_len + server->vals->header_preamble_size <
7893242e
PS
1560 SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
1561 kfree(utf16_path);
1562 return -ENOENT;
1563 }
1564
91cb74f5 1565 if (err_iov.iov_len + server->vals->header_preamble_size <
7893242e
PS
1566 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
1567 kfree(utf16_path);
1568 return -ENOENT;
1569 }
1570
b42bf888
PS
1571 *target_path = cifs_strndup_from_utf16(
1572 (char *)symlink->PathBuffer + sub_offset,
1573 sub_len, true, cifs_sb->local_nls);
1574 if (!(*target_path)) {
1575 kfree(utf16_path);
1576 return -ENOMEM;
1577 }
1578 convert_delimiter(*target_path, '/');
1579 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
1580 kfree(utf16_path);
1581 return rc;
1582}
1583
84908426 1584#ifdef CONFIG_CIFS_ACL
2f1afe25
SP
1585static struct cifs_ntsd *
1586get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
1587 const struct cifs_fid *cifsfid, u32 *pacllen)
1588{
1589 struct cifs_ntsd *pntsd = NULL;
1590 unsigned int xid;
1591 int rc = -EOPNOTSUPP;
1592 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1593
1594 if (IS_ERR(tlink))
1595 return ERR_CAST(tlink);
1596
1597 xid = get_xid();
1598 cifs_dbg(FYI, "trying to get acl\n");
1599
1600 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
1601 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
1602 free_xid(xid);
1603
1604 cifs_put_tlink(tlink);
1605
1606 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
1607 if (rc)
1608 return ERR_PTR(rc);
1609 return pntsd;
1610
1611}
1612
1613static struct cifs_ntsd *
1614get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
1615 const char *path, u32 *pacllen)
1616{
1617 struct cifs_ntsd *pntsd = NULL;
1618 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1619 unsigned int xid;
1620 int rc;
1621 struct cifs_tcon *tcon;
1622 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1623 struct cifs_fid fid;
1624 struct cifs_open_parms oparms;
1625 __le16 *utf16_path;
1626
1627 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
1628 if (IS_ERR(tlink))
1629 return ERR_CAST(tlink);
1630
1631 tcon = tlink_tcon(tlink);
1632 xid = get_xid();
1633
1634 if (backup_cred(cifs_sb))
709340a0 1635 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2f1afe25
SP
1636 else
1637 oparms.create_options = 0;
1638
1639 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1640 if (!utf16_path)
1641 return ERR_PTR(-ENOMEM);
1642
1643 oparms.tcon = tcon;
1644 oparms.desired_access = READ_CONTROL;
1645 oparms.disposition = FILE_OPEN;
1646 oparms.fid = &fid;
1647 oparms.reconnect = false;
1648
1649 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
1650 kfree(utf16_path);
1651 if (!rc) {
1652 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
1653 fid.volatile_fid, (void **)&pntsd, pacllen);
1654 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1655 }
1656
1657 cifs_put_tlink(tlink);
1658 free_xid(xid);
1659
1660 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
1661 if (rc)
1662 return ERR_PTR(rc);
1663 return pntsd;
1664}
1665
366ed846
SP
1666#ifdef CONFIG_CIFS_ACL
1667static int
1668set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
1669 struct inode *inode, const char *path, int aclflag)
1670{
1671 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1672 unsigned int xid;
1673 int rc, access_flags = 0;
1674 struct cifs_tcon *tcon;
1675 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1676 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1677 struct cifs_fid fid;
1678 struct cifs_open_parms oparms;
1679 __le16 *utf16_path;
1680
1681 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
1682 if (IS_ERR(tlink))
1683 return PTR_ERR(tlink);
1684
1685 tcon = tlink_tcon(tlink);
1686 xid = get_xid();
1687
1688 if (backup_cred(cifs_sb))
1689 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1690 else
1691 oparms.create_options = 0;
1692
1693 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
1694 access_flags = WRITE_OWNER;
1695 else
1696 access_flags = WRITE_DAC;
1697
1698 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1699 if (!utf16_path)
1700 return -ENOMEM;
1701
1702 oparms.tcon = tcon;
1703 oparms.desired_access = access_flags;
1704 oparms.disposition = FILE_OPEN;
1705 oparms.path = path;
1706 oparms.fid = &fid;
1707 oparms.reconnect = false;
1708
1709 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
1710 kfree(utf16_path);
1711 if (!rc) {
1712 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
1713 fid.volatile_fid, pnntsd, acllen, aclflag);
1714 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1715 }
1716
1717 cifs_put_tlink(tlink);
1718 free_xid(xid);
1719 return rc;
1720}
1721#endif /* CIFS_ACL */
1722
2f1afe25
SP
1723/* Retrieve an ACL from the server */
1724static struct cifs_ntsd *
1725get_smb2_acl(struct cifs_sb_info *cifs_sb,
1726 struct inode *inode, const char *path,
1727 u32 *pacllen)
1728{
1729 struct cifs_ntsd *pntsd = NULL;
1730 struct cifsFileInfo *open_file = NULL;
1731
1732 if (inode)
1733 open_file = find_readable_file(CIFS_I(inode), true);
1734 if (!open_file)
1735 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
1736
1737 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
1738 cifsFileInfo_put(open_file);
1739 return pntsd;
1740}
84908426 1741#endif
2f1afe25 1742
30175628
SF
1743static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
1744 loff_t offset, loff_t len, bool keep_size)
1745{
1746 struct inode *inode;
1747 struct cifsInodeInfo *cifsi;
1748 struct cifsFileInfo *cfile = file->private_data;
1749 struct file_zero_data_information fsctl_buf;
1750 long rc;
1751 unsigned int xid;
1752
1753 xid = get_xid();
1754
2b0143b5 1755 inode = d_inode(cfile->dentry);
30175628
SF
1756 cifsi = CIFS_I(inode);
1757
1758 /* if file not oplocked can't be sure whether asking to extend size */
1759 if (!CIFS_CACHE_READ(cifsi))
1760 if (keep_size == false)
1761 return -EOPNOTSUPP;
1762
2bb93d24 1763 /*
30175628
SF
1764 * Must check if file sparse since fallocate -z (zero range) assumes
1765 * non-sparse allocation
1766 */
1767 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE))
1768 return -EOPNOTSUPP;
1769
1770 /*
1771 * need to make sure we are not asked to extend the file since the SMB3
1772 * fsctl does not change the file size. In the future we could change
1773 * this to zero the first part of the range then set the file size
1774 * which for a non sparse file would zero the newly extended range
1775 */
1776 if (keep_size == false)
1777 if (i_size_read(inode) < offset + len)
1778 return -EOPNOTSUPP;
1779
1780 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1781
1782 fsctl_buf.FileOffset = cpu_to_le64(offset);
1783 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1784
1785 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1786 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
63a83b86 1787 true /* is_fctl */, (char *)&fsctl_buf,
30175628
SF
1788 sizeof(struct file_zero_data_information), NULL, NULL);
1789 free_xid(xid);
1790 return rc;
1791}
1792
31742c5a
SF
1793static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
1794 loff_t offset, loff_t len)
1795{
1796 struct inode *inode;
1797 struct cifsInodeInfo *cifsi;
1798 struct cifsFileInfo *cfile = file->private_data;
1799 struct file_zero_data_information fsctl_buf;
1800 long rc;
1801 unsigned int xid;
1802 __u8 set_sparse = 1;
1803
1804 xid = get_xid();
1805
2b0143b5 1806 inode = d_inode(cfile->dentry);
31742c5a
SF
1807 cifsi = CIFS_I(inode);
1808
1809 /* Need to make file sparse, if not already, before freeing range. */
1810 /* Consider adding equivalent for compressed since it could also work */
1811 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse))
1812 return -EOPNOTSUPP;
1813
1814 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1815
1816 fsctl_buf.FileOffset = cpu_to_le64(offset);
1817 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1818
1819 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1820 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
63a83b86 1821 true /* is_fctl */, (char *)&fsctl_buf,
31742c5a
SF
1822 sizeof(struct file_zero_data_information), NULL, NULL);
1823 free_xid(xid);
1824 return rc;
1825}
1826
9ccf3216
SF
1827static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
1828 loff_t off, loff_t len, bool keep_size)
1829{
1830 struct inode *inode;
1831 struct cifsInodeInfo *cifsi;
1832 struct cifsFileInfo *cfile = file->private_data;
1833 long rc = -EOPNOTSUPP;
1834 unsigned int xid;
1835
1836 xid = get_xid();
1837
2b0143b5 1838 inode = d_inode(cfile->dentry);
9ccf3216
SF
1839 cifsi = CIFS_I(inode);
1840
1841 /* if file not oplocked can't be sure whether asking to extend size */
1842 if (!CIFS_CACHE_READ(cifsi))
1843 if (keep_size == false)
1844 return -EOPNOTSUPP;
1845
1846 /*
1847 * Files are non-sparse by default so falloc may be a no-op
1848 * Must check if file sparse. If not sparse, and not extending
1849 * then no need to do anything since file already allocated
1850 */
1851 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
1852 if (keep_size == true)
1853 return 0;
1854 /* check if extending file */
1855 else if (i_size_read(inode) >= off + len)
1856 /* not extending file and already not sparse */
1857 return 0;
1858 /* BB: in future add else clause to extend file */
1859 else
1860 return -EOPNOTSUPP;
1861 }
1862
1863 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
1864 /*
1865 * Check if falloc starts within first few pages of file
1866 * and ends within a few pages of the end of file to
1867 * ensure that most of file is being forced to be
1868 * fallocated now. If so then setting whole file sparse
1869 * ie potentially making a few extra pages at the beginning
1870 * or end of the file non-sparse via set_sparse is harmless.
1871 */
1872 if ((off > 8192) || (off + len + 8192 < i_size_read(inode)))
1873 return -EOPNOTSUPP;
1874
1875 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
1876 }
1877 /* BB: else ... in future add code to extend file and set sparse */
1878
1879
1880 free_xid(xid);
1881 return rc;
1882}
1883
1884
31742c5a
SF
1885static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
1886 loff_t off, loff_t len)
1887{
1888 /* KEEP_SIZE already checked for by do_fallocate */
1889 if (mode & FALLOC_FL_PUNCH_HOLE)
1890 return smb3_punch_hole(file, tcon, off, len);
30175628
SF
1891 else if (mode & FALLOC_FL_ZERO_RANGE) {
1892 if (mode & FALLOC_FL_KEEP_SIZE)
1893 return smb3_zero_range(file, tcon, off, len, true);
1894 return smb3_zero_range(file, tcon, off, len, false);
9ccf3216
SF
1895 } else if (mode == FALLOC_FL_KEEP_SIZE)
1896 return smb3_simple_falloc(file, tcon, off, len, true);
1897 else if (mode == 0)
1898 return smb3_simple_falloc(file, tcon, off, len, false);
31742c5a
SF
1899
1900 return -EOPNOTSUPP;
1901}
1902
c11f1df5
SP
1903static void
1904smb2_downgrade_oplock(struct TCP_Server_Info *server,
1905 struct cifsInodeInfo *cinode, bool set_level2)
1906{
1907 if (set_level2)
1908 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
1909 0, NULL);
1910 else
1911 server->ops->set_oplock_level(cinode, 0, 0, NULL);
1912}
1913
53ef1016 1914static void
42873b0a
PS
1915smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1916 unsigned int epoch, bool *purge_cache)
53ef1016
PS
1917{
1918 oplock &= 0xFF;
1919 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1920 return;
1921 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
42873b0a 1922 cinode->oplock = CIFS_CACHE_RHW_FLG;
53ef1016
PS
1923 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
1924 &cinode->vfs_inode);
1925 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
42873b0a 1926 cinode->oplock = CIFS_CACHE_RW_FLG;
53ef1016
PS
1927 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
1928 &cinode->vfs_inode);
1929 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
1930 cinode->oplock = CIFS_CACHE_READ_FLG;
1931 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
1932 &cinode->vfs_inode);
1933 } else
1934 cinode->oplock = 0;
1935}
1936
1937static void
42873b0a
PS
1938smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1939 unsigned int epoch, bool *purge_cache)
53ef1016
PS
1940{
1941 char message[5] = {0};
1942
1943 oplock &= 0xFF;
1944 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1945 return;
1946
1947 cinode->oplock = 0;
1948 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
1949 cinode->oplock |= CIFS_CACHE_READ_FLG;
1950 strcat(message, "R");
1951 }
1952 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
1953 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
1954 strcat(message, "H");
1955 }
1956 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
1957 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
1958 strcat(message, "W");
1959 }
1960 if (!cinode->oplock)
1961 strcat(message, "None");
1962 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
1963 &cinode->vfs_inode);
1964}
1965
42873b0a
PS
1966static void
1967smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1968 unsigned int epoch, bool *purge_cache)
1969{
1970 unsigned int old_oplock = cinode->oplock;
1971
1972 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
1973
1974 if (purge_cache) {
1975 *purge_cache = false;
1976 if (old_oplock == CIFS_CACHE_READ_FLG) {
1977 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
1978 (epoch - cinode->epoch > 0))
1979 *purge_cache = true;
1980 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1981 (epoch - cinode->epoch > 1))
1982 *purge_cache = true;
1983 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1984 (epoch - cinode->epoch > 1))
1985 *purge_cache = true;
1986 else if (cinode->oplock == 0 &&
1987 (epoch - cinode->epoch > 0))
1988 *purge_cache = true;
1989 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
1990 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1991 (epoch - cinode->epoch > 0))
1992 *purge_cache = true;
1993 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1994 (epoch - cinode->epoch > 1))
1995 *purge_cache = true;
1996 }
1997 cinode->epoch = epoch;
1998 }
1999}
2000
53ef1016
PS
2001static bool
2002smb2_is_read_op(__u32 oplock)
2003{
2004 return oplock == SMB2_OPLOCK_LEVEL_II;
2005}
2006
2007static bool
2008smb21_is_read_op(__u32 oplock)
2009{
2010 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
2011 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
2012}
2013
f047390a
PS
2014static __le32
2015map_oplock_to_lease(u8 oplock)
2016{
2017 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
2018 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
2019 else if (oplock == SMB2_OPLOCK_LEVEL_II)
2020 return SMB2_LEASE_READ_CACHING;
2021 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
2022 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
2023 SMB2_LEASE_WRITE_CACHING;
2024 return 0;
2025}
2026
a41a28bd
PS
2027static char *
2028smb2_create_lease_buf(u8 *lease_key, u8 oplock)
2029{
2030 struct create_lease *buf;
2031
2032 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
2033 if (!buf)
2034 return NULL;
2035
2036 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
2037 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
f047390a 2038 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
a41a28bd
PS
2039
2040 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2041 (struct create_lease, lcontext));
2042 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
2043 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2044 (struct create_lease, Name));
2045 buf->ccontext.NameLength = cpu_to_le16(4);
12197a7f 2046 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
a41a28bd
PS
2047 buf->Name[0] = 'R';
2048 buf->Name[1] = 'q';
2049 buf->Name[2] = 'L';
2050 buf->Name[3] = 's';
2051 return (char *)buf;
2052}
2053
f047390a
PS
2054static char *
2055smb3_create_lease_buf(u8 *lease_key, u8 oplock)
2056{
2057 struct create_lease_v2 *buf;
2058
2059 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
2060 if (!buf)
2061 return NULL;
2062
2063 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
2064 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
2065 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2066
2067 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2068 (struct create_lease_v2, lcontext));
2069 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
2070 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2071 (struct create_lease_v2, Name));
2072 buf->ccontext.NameLength = cpu_to_le16(4);
12197a7f 2073 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
f047390a
PS
2074 buf->Name[0] = 'R';
2075 buf->Name[1] = 'q';
2076 buf->Name[2] = 'L';
2077 buf->Name[3] = 's';
2078 return (char *)buf;
2079}
2080
b5c7cde3 2081static __u8
96164ab2 2082smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
b5c7cde3
PS
2083{
2084 struct create_lease *lc = (struct create_lease *)buf;
2085
42873b0a 2086 *epoch = 0; /* not used */
b5c7cde3
PS
2087 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2088 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2089 return le32_to_cpu(lc->lcontext.LeaseState);
2090}
2091
f047390a 2092static __u8
96164ab2 2093smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
f047390a
PS
2094{
2095 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
2096
42873b0a 2097 *epoch = le16_to_cpu(lc->lcontext.Epoch);
f047390a
PS
2098 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2099 return SMB2_OPLOCK_LEVEL_NOCHANGE;
96164ab2
RS
2100 if (lease_key)
2101 memcpy(lease_key, &lc->lcontext.LeaseKeyLow,
2102 SMB2_LEASE_KEY_SIZE);
f047390a
PS
2103 return le32_to_cpu(lc->lcontext.LeaseState);
2104}
2105
7f6c5008
PS
2106static unsigned int
2107smb2_wp_retry_size(struct inode *inode)
2108{
2109 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
2110 SMB2_MAX_BUFFER_SIZE);
2111}
2112
52755808
PS
2113static bool
2114smb2_dir_needs_close(struct cifsFileInfo *cfile)
2115{
2116 return !cfile->invalidHandle;
2117}
2118
026e93dc 2119static void
93012bf9
RS
2120fill_transform_hdr(struct TCP_Server_Info *server,
2121 struct smb2_transform_hdr *tr_hdr, struct smb_rqst *old_rq)
026e93dc
PS
2122{
2123 struct smb2_sync_hdr *shdr =
2124 (struct smb2_sync_hdr *)old_rq->rq_iov[1].iov_base;
2125 unsigned int orig_len = get_rfc1002_length(old_rq->rq_iov[0].iov_base);
2126
2127 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
2128 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
2129 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
2130 tr_hdr->Flags = cpu_to_le16(0x01);
2131 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2132 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
93012bf9 2133 inc_rfc1001_len(tr_hdr, sizeof(struct smb2_transform_hdr) - server->vals->header_preamble_size);
026e93dc
PS
2134 inc_rfc1001_len(tr_hdr, orig_len);
2135}
2136
262916bc
RS
2137/* We can not use the normal sg_set_buf() as we will sometimes pass a
2138 * stack object as buf.
2139 */
2140static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
2141 unsigned int buflen)
2142{
2143 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
2144}
2145
026e93dc
PS
2146static struct scatterlist *
2147init_sg(struct smb_rqst *rqst, u8 *sign)
2148{
2149 unsigned int sg_len = rqst->rq_nvec + rqst->rq_npages + 1;
2150 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 24;
2151 struct scatterlist *sg;
2152 unsigned int i;
2153 unsigned int j;
2154
2155 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
2156 if (!sg)
2157 return NULL;
2158
2159 sg_init_table(sg, sg_len);
262916bc 2160 smb2_sg_set_buf(&sg[0], rqst->rq_iov[0].iov_base + 24, assoc_data_len);
026e93dc 2161 for (i = 1; i < rqst->rq_nvec; i++)
262916bc 2162 smb2_sg_set_buf(&sg[i], rqst->rq_iov[i].iov_base,
026e93dc
PS
2163 rqst->rq_iov[i].iov_len);
2164 for (j = 0; i < sg_len - 1; i++, j++) {
2165 unsigned int len = (j < rqst->rq_npages - 1) ? rqst->rq_pagesz
2166 : rqst->rq_tailsz;
2167 sg_set_page(&sg[i], rqst->rq_pages[j], len, 0);
2168 }
262916bc 2169 smb2_sg_set_buf(&sg[sg_len - 1], sign, SMB2_SIGNATURE_SIZE);
026e93dc
PS
2170 return sg;
2171}
2172
61cfac6f
PS
2173static int
2174smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
2175{
2176 struct cifs_ses *ses;
2177 u8 *ses_enc_key;
2178
2179 spin_lock(&cifs_tcp_ses_lock);
2180 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2181 if (ses->Suid != ses_id)
2182 continue;
2183 ses_enc_key = enc ? ses->smb3encryptionkey :
2184 ses->smb3decryptionkey;
2185 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
2186 spin_unlock(&cifs_tcp_ses_lock);
2187 return 0;
2188 }
2189 spin_unlock(&cifs_tcp_ses_lock);
2190
2191 return 1;
2192}
026e93dc
PS
2193/*
2194 * Encrypt or decrypt @rqst message. @rqst has the following format:
2195 * iov[0] - transform header (associate data),
2196 * iov[1-N] and pages - data to encrypt.
2197 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
2198 * untouched.
2199 */
2200static int
2201crypt_message(struct TCP_Server_Info *server, struct smb_rqst *rqst, int enc)
2202{
2203 struct smb2_transform_hdr *tr_hdr =
2204 (struct smb2_transform_hdr *)rqst->rq_iov[0].iov_base;
93012bf9 2205 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20 - server->vals->header_preamble_size;
026e93dc
PS
2206 int rc = 0;
2207 struct scatterlist *sg;
2208 u8 sign[SMB2_SIGNATURE_SIZE] = {};
61cfac6f 2209 u8 key[SMB3_SIGN_KEY_SIZE];
026e93dc
PS
2210 struct aead_request *req;
2211 char *iv;
2212 unsigned int iv_len;
a5186b85 2213 DECLARE_CRYPTO_WAIT(wait);
026e93dc
PS
2214 struct crypto_aead *tfm;
2215 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2216
61cfac6f
PS
2217 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
2218 if (rc) {
2219 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
2220 enc ? "en" : "de");
026e93dc
PS
2221 return 0;
2222 }
2223
2224 rc = smb3_crypto_aead_allocate(server);
2225 if (rc) {
2226 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
2227 return rc;
2228 }
2229
2230 tfm = enc ? server->secmech.ccmaesencrypt :
2231 server->secmech.ccmaesdecrypt;
61cfac6f 2232 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
026e93dc
PS
2233 if (rc) {
2234 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
2235 return rc;
2236 }
2237
2238 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
2239 if (rc) {
2240 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
2241 return rc;
2242 }
2243
2244 req = aead_request_alloc(tfm, GFP_KERNEL);
2245 if (!req) {
2246 cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
2247 return -ENOMEM;
2248 }
2249
2250 if (!enc) {
2251 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
2252 crypt_len += SMB2_SIGNATURE_SIZE;
2253 }
2254
2255 sg = init_sg(rqst, sign);
2256 if (!sg) {
517a6e43
CJ
2257 cifs_dbg(VFS, "%s: Failed to init sg", __func__);
2258 rc = -ENOMEM;
026e93dc
PS
2259 goto free_req;
2260 }
2261
2262 iv_len = crypto_aead_ivsize(tfm);
2263 iv = kzalloc(iv_len, GFP_KERNEL);
2264 if (!iv) {
2265 cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
517a6e43 2266 rc = -ENOMEM;
026e93dc
PS
2267 goto free_sg;
2268 }
2269 iv[0] = 3;
2270 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2271
2272 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
2273 aead_request_set_ad(req, assoc_data_len);
2274
2275 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
a5186b85 2276 crypto_req_done, &wait);
026e93dc 2277
a5186b85
GBY
2278 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
2279 : crypto_aead_decrypt(req), &wait);
026e93dc
PS
2280
2281 if (!rc && enc)
2282 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
2283
2284 kfree(iv);
2285free_sg:
2286 kfree(sg);
2287free_req:
2288 kfree(req);
2289 return rc;
2290}
2291
2292static int
2293smb3_init_transform_rq(struct TCP_Server_Info *server, struct smb_rqst *new_rq,
2294 struct smb_rqst *old_rq)
2295{
2296 struct kvec *iov;
2297 struct page **pages;
2298 struct smb2_transform_hdr *tr_hdr;
2299 unsigned int npages = old_rq->rq_npages;
2300 int i;
2301 int rc = -ENOMEM;
2302
2303 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2304 if (!pages)
2305 return rc;
2306
2307 new_rq->rq_pages = pages;
2308 new_rq->rq_npages = old_rq->rq_npages;
2309 new_rq->rq_pagesz = old_rq->rq_pagesz;
2310 new_rq->rq_tailsz = old_rq->rq_tailsz;
2311
2312 for (i = 0; i < npages; i++) {
2313 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2314 if (!pages[i])
2315 goto err_free_pages;
2316 }
2317
2318 iov = kmalloc_array(old_rq->rq_nvec, sizeof(struct kvec), GFP_KERNEL);
2319 if (!iov)
2320 goto err_free_pages;
2321
2322 /* copy all iovs from the old except the 1st one (rfc1002 length) */
2323 memcpy(&iov[1], &old_rq->rq_iov[1],
2324 sizeof(struct kvec) * (old_rq->rq_nvec - 1));
2325 new_rq->rq_iov = iov;
2326 new_rq->rq_nvec = old_rq->rq_nvec;
2327
2328 tr_hdr = kmalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL);
2329 if (!tr_hdr)
2330 goto err_free_iov;
2331
2332 /* fill the 1st iov with a transform header */
93012bf9 2333 fill_transform_hdr(server, tr_hdr, old_rq);
026e93dc
PS
2334 new_rq->rq_iov[0].iov_base = tr_hdr;
2335 new_rq->rq_iov[0].iov_len = sizeof(struct smb2_transform_hdr);
2336
2337 /* copy pages form the old */
2338 for (i = 0; i < npages; i++) {
2339 char *dst = kmap(new_rq->rq_pages[i]);
2340 char *src = kmap(old_rq->rq_pages[i]);
2341 unsigned int len = (i < npages - 1) ? new_rq->rq_pagesz :
2342 new_rq->rq_tailsz;
2343 memcpy(dst, src, len);
2344 kunmap(new_rq->rq_pages[i]);
2345 kunmap(old_rq->rq_pages[i]);
2346 }
2347
2348 rc = crypt_message(server, new_rq, 1);
2349 cifs_dbg(FYI, "encrypt message returned %d", rc);
2350 if (rc)
2351 goto err_free_tr_hdr;
2352
2353 return rc;
2354
2355err_free_tr_hdr:
2356 kfree(tr_hdr);
2357err_free_iov:
2358 kfree(iov);
2359err_free_pages:
2360 for (i = i - 1; i >= 0; i--)
2361 put_page(pages[i]);
2362 kfree(pages);
2363 return rc;
2364}
2365
2366static void
2367smb3_free_transform_rq(struct smb_rqst *rqst)
2368{
2369 int i = rqst->rq_npages - 1;
2370
2371 for (; i >= 0; i--)
2372 put_page(rqst->rq_pages[i]);
2373 kfree(rqst->rq_pages);
2374 /* free transform header */
2375 kfree(rqst->rq_iov[0].iov_base);
2376 kfree(rqst->rq_iov);
2377}
2378
4326ed2f
PS
2379static int
2380smb3_is_transform_hdr(void *buf)
2381{
2382 struct smb2_transform_hdr *trhdr = buf;
2383
2384 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
2385}
2386
2387static int
2388decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
2389 unsigned int buf_data_size, struct page **pages,
2390 unsigned int npages, unsigned int page_data_size)
2391{
2392 struct kvec iov[2];
2393 struct smb_rqst rqst = {NULL};
2394 struct smb2_hdr *hdr;
2395 int rc;
2396
2397 iov[0].iov_base = buf;
2398 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
2399 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
2400 iov[1].iov_len = buf_data_size;
2401
2402 rqst.rq_iov = iov;
2403 rqst.rq_nvec = 2;
2404 rqst.rq_pages = pages;
2405 rqst.rq_npages = npages;
2406 rqst.rq_pagesz = PAGE_SIZE;
2407 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
2408
2409 rc = crypt_message(server, &rqst, 0);
2410 cifs_dbg(FYI, "decrypt message returned %d\n", rc);
2411
2412 if (rc)
2413 return rc;
2414
93012bf9 2415 memmove(buf + server->vals->header_preamble_size, iov[1].iov_base, buf_data_size);
4326ed2f
PS
2416 hdr = (struct smb2_hdr *)buf;
2417 hdr->smb2_buf_length = cpu_to_be32(buf_data_size + page_data_size);
93012bf9 2418 server->total_read = buf_data_size + page_data_size + server->vals->header_preamble_size;
4326ed2f
PS
2419
2420 return rc;
2421}
2422
c42a6abe
PS
2423static int
2424read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
2425 unsigned int npages, unsigned int len)
2426{
2427 int i;
2428 int length;
2429
2430 for (i = 0; i < npages; i++) {
2431 struct page *page = pages[i];
2432 size_t n;
2433
2434 n = len;
2435 if (len >= PAGE_SIZE) {
2436 /* enough data to fill the page */
2437 n = PAGE_SIZE;
2438 len -= n;
2439 } else {
2440 zero_user(page, len, PAGE_SIZE - len);
2441 len = 0;
2442 }
2443 length = cifs_read_page_from_socket(server, page, n);
2444 if (length < 0)
2445 return length;
2446 server->total_read += length;
2447 }
2448
2449 return 0;
2450}
2451
2452static int
2453init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
2454 unsigned int cur_off, struct bio_vec **page_vec)
2455{
2456 struct bio_vec *bvec;
2457 int i;
2458
2459 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
2460 if (!bvec)
2461 return -ENOMEM;
2462
2463 for (i = 0; i < npages; i++) {
2464 bvec[i].bv_page = pages[i];
2465 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
2466 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
2467 data_size -= bvec[i].bv_len;
2468 }
2469
2470 if (data_size != 0) {
2471 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
2472 kfree(bvec);
2473 return -EIO;
2474 }
2475
2476 *page_vec = bvec;
2477 return 0;
2478}
2479
4326ed2f
PS
2480static int
2481handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
2482 char *buf, unsigned int buf_len, struct page **pages,
2483 unsigned int npages, unsigned int page_data_size)
2484{
2485 unsigned int data_offset;
2486 unsigned int data_len;
c42a6abe
PS
2487 unsigned int cur_off;
2488 unsigned int cur_page_idx;
2489 unsigned int pad_len;
4326ed2f
PS
2490 struct cifs_readdata *rdata = mid->callback_data;
2491 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
2492 struct bio_vec *bvec = NULL;
2493 struct iov_iter iter;
2494 struct kvec iov;
2495 int length;
74dcf418 2496 bool use_rdma_mr = false;
4326ed2f
PS
2497
2498 if (shdr->Command != SMB2_READ) {
2499 cifs_dbg(VFS, "only big read responses are supported\n");
2500 return -ENOTSUPP;
2501 }
2502
511c54a2
PS
2503 if (server->ops->is_session_expired &&
2504 server->ops->is_session_expired(buf)) {
2505 cifs_reconnect(server);
2506 wake_up(&server->response_q);
2507 return -1;
2508 }
2509
4326ed2f
PS
2510 if (server->ops->is_status_pending &&
2511 server->ops->is_status_pending(buf, server, 0))
2512 return -1;
2513
2514 rdata->result = server->ops->map_error(buf, false);
2515 if (rdata->result != 0) {
2516 cifs_dbg(FYI, "%s: server returned error %d\n",
2517 __func__, rdata->result);
2518 dequeue_mid(mid, rdata->result);
2519 return 0;
2520 }
2521
93012bf9 2522 data_offset = server->ops->read_data_offset(buf) + server->vals->header_preamble_size;
74dcf418
LL
2523#ifdef CONFIG_CIFS_SMB_DIRECT
2524 use_rdma_mr = rdata->mr;
2525#endif
2526 data_len = server->ops->read_data_length(buf, use_rdma_mr);
4326ed2f
PS
2527
2528 if (data_offset < server->vals->read_rsp_size) {
2529 /*
2530 * win2k8 sometimes sends an offset of 0 when the read
2531 * is beyond the EOF. Treat it as if the data starts just after
2532 * the header.
2533 */
2534 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
2535 __func__, data_offset);
2536 data_offset = server->vals->read_rsp_size;
2537 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
2538 /* data_offset is beyond the end of smallbuf */
2539 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
2540 __func__, data_offset);
2541 rdata->result = -EIO;
2542 dequeue_mid(mid, rdata->result);
2543 return 0;
2544 }
2545
c42a6abe
PS
2546 pad_len = data_offset - server->vals->read_rsp_size;
2547
4326ed2f
PS
2548 if (buf_len <= data_offset) {
2549 /* read response payload is in pages */
c42a6abe
PS
2550 cur_page_idx = pad_len / PAGE_SIZE;
2551 cur_off = pad_len % PAGE_SIZE;
2552
2553 if (cur_page_idx != 0) {
2554 /* data offset is beyond the 1st page of response */
2555 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
2556 __func__, data_offset);
2557 rdata->result = -EIO;
2558 dequeue_mid(mid, rdata->result);
2559 return 0;
2560 }
2561
2562 if (data_len > page_data_size - pad_len) {
2563 /* data_len is corrupt -- discard frame */
2564 rdata->result = -EIO;
2565 dequeue_mid(mid, rdata->result);
2566 return 0;
2567 }
2568
2569 rdata->result = init_read_bvec(pages, npages, page_data_size,
2570 cur_off, &bvec);
2571 if (rdata->result != 0) {
2572 dequeue_mid(mid, rdata->result);
2573 return 0;
2574 }
2575
2576 iov_iter_bvec(&iter, WRITE | ITER_BVEC, bvec, npages, data_len);
4326ed2f
PS
2577 } else if (buf_len >= data_offset + data_len) {
2578 /* read response payload is in buf */
2579 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
2580 iov.iov_base = buf + data_offset;
2581 iov.iov_len = data_len;
2582 iov_iter_kvec(&iter, WRITE | ITER_KVEC, &iov, 1, data_len);
2583 } else {
2584 /* read response payload cannot be in both buf and pages */
2585 WARN_ONCE(1, "buf can not contain only a part of read data");
2586 rdata->result = -EIO;
2587 dequeue_mid(mid, rdata->result);
2588 return 0;
2589 }
2590
2591 /* set up first iov for signature check */
2592 rdata->iov[0].iov_base = buf;
2593 rdata->iov[0].iov_len = 4;
2594 rdata->iov[1].iov_base = buf + 4;
2595 rdata->iov[1].iov_len = server->vals->read_rsp_size - 4;
2596 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
2597 rdata->iov[0].iov_base, server->vals->read_rsp_size);
2598
2599 length = rdata->copy_into_pages(server, rdata, &iter);
2600
2601 kfree(bvec);
2602
2603 if (length < 0)
2604 return length;
2605
2606 dequeue_mid(mid, false);
2607 return length;
2608}
2609
c42a6abe
PS
2610static int
2611receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2612{
2613 char *buf = server->smallbuf;
2614 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2615 unsigned int npages;
2616 struct page **pages;
2617 unsigned int len;
2e96467d 2618 unsigned int buflen = server->pdu_size + server->vals->header_preamble_size;
c42a6abe
PS
2619 int rc;
2620 int i = 0;
2621
93012bf9
RS
2622 len = min_t(unsigned int, buflen, server->vals->read_rsp_size -
2623 server->vals->header_preamble_size +
c42a6abe
PS
2624 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
2625
2626 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
2627 if (rc < 0)
2628 return rc;
2629 server->total_read += rc;
2630
93012bf9
RS
2631 len = le32_to_cpu(tr_hdr->OriginalMessageSize) +
2632 server->vals->header_preamble_size -
2633 server->vals->read_rsp_size;
c42a6abe
PS
2634 npages = DIV_ROUND_UP(len, PAGE_SIZE);
2635
2636 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2637 if (!pages) {
2638 rc = -ENOMEM;
2639 goto discard_data;
2640 }
2641
2642 for (; i < npages; i++) {
2643 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2644 if (!pages[i]) {
2645 rc = -ENOMEM;
2646 goto discard_data;
2647 }
2648 }
2649
2650 /* read read data into pages */
2651 rc = read_data_into_pages(server, pages, npages, len);
2652 if (rc)
2653 goto free_pages;
2654
350be257 2655 rc = cifs_discard_remaining_data(server);
c42a6abe
PS
2656 if (rc)
2657 goto free_pages;
2658
93012bf9
RS
2659 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size -
2660 server->vals->header_preamble_size,
c42a6abe
PS
2661 pages, npages, len);
2662 if (rc)
2663 goto free_pages;
2664
2665 *mid = smb2_find_mid(server, buf);
2666 if (*mid == NULL)
2667 cifs_dbg(FYI, "mid not found\n");
2668 else {
2669 cifs_dbg(FYI, "mid found\n");
2670 (*mid)->decrypted = true;
2671 rc = handle_read_data(server, *mid, buf,
2672 server->vals->read_rsp_size,
2673 pages, npages, len);
2674 }
2675
2676free_pages:
2677 for (i = i - 1; i >= 0; i--)
2678 put_page(pages[i]);
2679 kfree(pages);
2680 return rc;
2681discard_data:
350be257 2682 cifs_discard_remaining_data(server);
c42a6abe
PS
2683 goto free_pages;
2684}
2685
4326ed2f
PS
2686static int
2687receive_encrypted_standard(struct TCP_Server_Info *server,
2688 struct mid_q_entry **mid)
2689{
2690 int length;
2691 char *buf = server->smallbuf;
2e96467d 2692 unsigned int pdu_length = server->pdu_size;
4326ed2f
PS
2693 unsigned int buf_size;
2694 struct mid_q_entry *mid_entry;
2695
2696 /* switch to large buffer if too big for a small one */
93012bf9 2697 if (pdu_length + server->vals->header_preamble_size > MAX_CIFS_SMALL_BUFFER_SIZE) {
4326ed2f
PS
2698 server->large_buf = true;
2699 memcpy(server->bigbuf, buf, server->total_read);
2700 buf = server->bigbuf;
2701 }
2702
2703 /* now read the rest */
2704 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
93012bf9
RS
2705 pdu_length - HEADER_SIZE(server) + 1 +
2706 server->vals->header_preamble_size);
4326ed2f
PS
2707 if (length < 0)
2708 return length;
2709 server->total_read += length;
2710
93012bf9 2711 buf_size = pdu_length + server->vals->header_preamble_size - sizeof(struct smb2_transform_hdr);
4326ed2f
PS
2712 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
2713 if (length)
2714 return length;
2715
2716 mid_entry = smb2_find_mid(server, buf);
2717 if (mid_entry == NULL)
2718 cifs_dbg(FYI, "mid not found\n");
2719 else {
2720 cifs_dbg(FYI, "mid found\n");
2721 mid_entry->decrypted = true;
2722 }
2723
2724 *mid = mid_entry;
2725
2726 if (mid_entry && mid_entry->handle)
2727 return mid_entry->handle(server, mid_entry);
2728
2729 return cifs_handle_standard(server, mid_entry);
2730}
2731
2732static int
2733smb3_receive_transform(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2734{
2735 char *buf = server->smallbuf;
2e96467d 2736 unsigned int pdu_length = server->pdu_size;
4326ed2f
PS
2737 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2738 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2739
93012bf9 2740 if (pdu_length + server->vals->header_preamble_size < sizeof(struct smb2_transform_hdr) +
4326ed2f
PS
2741 sizeof(struct smb2_sync_hdr)) {
2742 cifs_dbg(VFS, "Transform message is too small (%u)\n",
2743 pdu_length);
2744 cifs_reconnect(server);
2745 wake_up(&server->response_q);
2746 return -ECONNABORTED;
2747 }
2748
93012bf9 2749 if (pdu_length + server->vals->header_preamble_size < orig_len + sizeof(struct smb2_transform_hdr)) {
4326ed2f
PS
2750 cifs_dbg(VFS, "Transform message is broken\n");
2751 cifs_reconnect(server);
2752 wake_up(&server->response_q);
2753 return -ECONNABORTED;
2754 }
2755
93012bf9 2756 if (pdu_length + server->vals->header_preamble_size > CIFSMaxBufSize + MAX_HEADER_SIZE(server))
c42a6abe 2757 return receive_encrypted_read(server, mid);
4326ed2f
PS
2758
2759 return receive_encrypted_standard(server, mid);
2760}
2761
2762int
2763smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
2764{
2765 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
2766
2e96467d 2767 return handle_read_data(server, mid, buf, server->pdu_size +
93012bf9 2768 server->vals->header_preamble_size,
4326ed2f
PS
2769 NULL, 0, 0);
2770}
2771
53ef1016 2772struct smb_version_operations smb20_operations = {
027e8eec 2773 .compare_fids = smb2_compare_fids,
2dc7e1c0 2774 .setup_request = smb2_setup_request,
c95b8eed 2775 .setup_async_request = smb2_setup_async_request,
2dc7e1c0 2776 .check_receive = smb2_check_receive,
28ea5290
PS
2777 .add_credits = smb2_add_credits,
2778 .set_credits = smb2_set_credits,
2779 .get_credits_field = smb2_get_credits_field,
2780 .get_credits = smb2_get_credits,
cb7e9eab 2781 .wait_mtu_credits = cifs_wait_mtu_credits,
2dc7e1c0 2782 .get_next_mid = smb2_get_next_mid,
09a4707e
PS
2783 .read_data_offset = smb2_read_data_offset,
2784 .read_data_length = smb2_read_data_length,
2785 .map_error = map_smb2_to_linux_error,
093b2bda
PS
2786 .find_mid = smb2_find_mid,
2787 .check_message = smb2_check_message,
2788 .dump_detail = smb2_dump_detail,
d60622eb
PS
2789 .clear_stats = smb2_clear_stats,
2790 .print_stats = smb2_print_stats,
983c88a4 2791 .is_oplock_break = smb2_is_valid_oplock_break,
38bd4906 2792 .handle_cancelled_mid = smb2_handle_cancelled_mid,
c11f1df5 2793 .downgrade_oplock = smb2_downgrade_oplock,
ec2e4523
PS
2794 .need_neg = smb2_need_neg,
2795 .negotiate = smb2_negotiate,
3a3bab50
PS
2796 .negotiate_wsize = smb2_negotiate_wsize,
2797 .negotiate_rsize = smb2_negotiate_rsize,
5478f9ba
PS
2798 .sess_setup = SMB2_sess_setup,
2799 .logoff = SMB2_logoff,
faaf946a
PS
2800 .tree_connect = SMB2_tcon,
2801 .tree_disconnect = SMB2_tdis,
34f62640 2802 .qfs_tcon = smb2_qfs_tcon,
2503a0db 2803 .is_path_accessible = smb2_is_path_accessible,
9094fad1
PS
2804 .can_echo = smb2_can_echo,
2805 .echo = SMB2_echo,
be4cb9e3
PS
2806 .query_path_info = smb2_query_path_info,
2807 .get_srv_inum = smb2_get_srv_inum,
b7546bc5 2808 .query_file_info = smb2_query_file_info,
c839ff24
PS
2809 .set_path_size = smb2_set_path_size,
2810 .set_file_size = smb2_set_file_size,
1feeaac7 2811 .set_file_info = smb2_set_file_info,
64a5cfa6 2812 .set_compression = smb2_set_compression,
a0e73183
PS
2813 .mkdir = smb2_mkdir,
2814 .mkdir_setinfo = smb2_mkdir_setinfo,
1a500f01 2815 .rmdir = smb2_rmdir,
cbe6f439 2816 .unlink = smb2_unlink,
35143eb5 2817 .rename = smb2_rename_path,
568798cc 2818 .create_hardlink = smb2_create_hardlink,
b42bf888 2819 .query_symlink = smb2_query_symlink,
5b23c97d
SP
2820 .query_mf_symlink = smb3_query_mf_symlink,
2821 .create_mf_symlink = smb3_create_mf_symlink,
f0df737e
PS
2822 .open = smb2_open_file,
2823 .set_fid = smb2_set_fid,
2824 .close = smb2_close_file,
7a5cfb19 2825 .flush = smb2_flush_file,
09a4707e 2826 .async_readv = smb2_async_readv,
33319141 2827 .async_writev = smb2_async_writev,
d8e05039 2828 .sync_read = smb2_sync_read,
009d3443 2829 .sync_write = smb2_sync_write,
d324f08d
PS
2830 .query_dir_first = smb2_query_dir_first,
2831 .query_dir_next = smb2_query_dir_next,
2832 .close_dir = smb2_close_dir,
2833 .calc_smb_size = smb2_calc_size,
2e44b288 2834 .is_status_pending = smb2_is_status_pending,
511c54a2 2835 .is_session_expired = smb2_is_session_expired,
983c88a4 2836 .oplock_response = smb2_oplock_response,
6fc05c25 2837 .queryfs = smb2_queryfs,
f7ba7fe6
PS
2838 .mand_lock = smb2_mand_lock,
2839 .mand_unlock_range = smb2_unlock_range,
b140799a 2840 .push_mand_locks = smb2_push_mandatory_locks,
b8c32dbb
PS
2841 .get_lease_key = smb2_get_lease_key,
2842 .set_lease_key = smb2_set_lease_key,
2843 .new_lease_key = smb2_new_lease_key,
38107d45 2844 .calc_signature = smb2_calc_signature,
53ef1016
PS
2845 .is_read_op = smb2_is_read_op,
2846 .set_oplock_level = smb2_set_oplock_level,
a41a28bd 2847 .create_lease_buf = smb2_create_lease_buf,
b5c7cde3 2848 .parse_lease_buf = smb2_parse_lease_buf,
312bbc59 2849 .copychunk_range = smb2_copychunk_range,
7f6c5008 2850 .wp_retry_size = smb2_wp_retry_size,
52755808 2851 .dir_needs_close = smb2_dir_needs_close,
9d49640a 2852 .get_dfs_refer = smb2_get_dfs_refer,
ef65aaed 2853 .select_sectype = smb2_select_sectype,
95907fea
RS
2854#ifdef CONFIG_CIFS_XATTR
2855 .query_all_EAs = smb2_query_eas,
5517554e 2856 .set_EA = smb2_set_ea,
95907fea 2857#endif /* CIFS_XATTR */
2f1afe25
SP
2858#ifdef CONFIG_CIFS_ACL
2859 .get_acl = get_smb2_acl,
2860 .get_acl_by_fid = get_smb2_acl_by_fid,
366ed846 2861 .set_acl = set_smb2_acl,
2f1afe25 2862#endif /* CIFS_ACL */
38107d45
SF
2863};
2864
53ef1016
PS
2865struct smb_version_operations smb21_operations = {
2866 .compare_fids = smb2_compare_fids,
2867 .setup_request = smb2_setup_request,
2868 .setup_async_request = smb2_setup_async_request,
2869 .check_receive = smb2_check_receive,
2870 .add_credits = smb2_add_credits,
2871 .set_credits = smb2_set_credits,
2872 .get_credits_field = smb2_get_credits_field,
2873 .get_credits = smb2_get_credits,
cb7e9eab 2874 .wait_mtu_credits = smb2_wait_mtu_credits,
53ef1016
PS
2875 .get_next_mid = smb2_get_next_mid,
2876 .read_data_offset = smb2_read_data_offset,
2877 .read_data_length = smb2_read_data_length,
2878 .map_error = map_smb2_to_linux_error,
2879 .find_mid = smb2_find_mid,
2880 .check_message = smb2_check_message,
2881 .dump_detail = smb2_dump_detail,
2882 .clear_stats = smb2_clear_stats,
2883 .print_stats = smb2_print_stats,
2884 .is_oplock_break = smb2_is_valid_oplock_break,
38bd4906 2885 .handle_cancelled_mid = smb2_handle_cancelled_mid,
c11f1df5 2886 .downgrade_oplock = smb2_downgrade_oplock,
53ef1016
PS
2887 .need_neg = smb2_need_neg,
2888 .negotiate = smb2_negotiate,
2889 .negotiate_wsize = smb2_negotiate_wsize,
2890 .negotiate_rsize = smb2_negotiate_rsize,
2891 .sess_setup = SMB2_sess_setup,
2892 .logoff = SMB2_logoff,
2893 .tree_connect = SMB2_tcon,
2894 .tree_disconnect = SMB2_tdis,
34f62640 2895 .qfs_tcon = smb2_qfs_tcon,
53ef1016
PS
2896 .is_path_accessible = smb2_is_path_accessible,
2897 .can_echo = smb2_can_echo,
2898 .echo = SMB2_echo,
2899 .query_path_info = smb2_query_path_info,
2900 .get_srv_inum = smb2_get_srv_inum,
2901 .query_file_info = smb2_query_file_info,
2902 .set_path_size = smb2_set_path_size,
2903 .set_file_size = smb2_set_file_size,
2904 .set_file_info = smb2_set_file_info,
64a5cfa6 2905 .set_compression = smb2_set_compression,
53ef1016
PS
2906 .mkdir = smb2_mkdir,
2907 .mkdir_setinfo = smb2_mkdir_setinfo,
2908 .rmdir = smb2_rmdir,
2909 .unlink = smb2_unlink,
2910 .rename = smb2_rename_path,
2911 .create_hardlink = smb2_create_hardlink,
2912 .query_symlink = smb2_query_symlink,
c22870ea 2913 .query_mf_symlink = smb3_query_mf_symlink,
5ab97578 2914 .create_mf_symlink = smb3_create_mf_symlink,
53ef1016
PS
2915 .open = smb2_open_file,
2916 .set_fid = smb2_set_fid,
2917 .close = smb2_close_file,
2918 .flush = smb2_flush_file,
2919 .async_readv = smb2_async_readv,
2920 .async_writev = smb2_async_writev,
2921 .sync_read = smb2_sync_read,
2922 .sync_write = smb2_sync_write,
2923 .query_dir_first = smb2_query_dir_first,
2924 .query_dir_next = smb2_query_dir_next,
2925 .close_dir = smb2_close_dir,
2926 .calc_smb_size = smb2_calc_size,
2927 .is_status_pending = smb2_is_status_pending,
511c54a2 2928 .is_session_expired = smb2_is_session_expired,
53ef1016
PS
2929 .oplock_response = smb2_oplock_response,
2930 .queryfs = smb2_queryfs,
2931 .mand_lock = smb2_mand_lock,
2932 .mand_unlock_range = smb2_unlock_range,
2933 .push_mand_locks = smb2_push_mandatory_locks,
2934 .get_lease_key = smb2_get_lease_key,
2935 .set_lease_key = smb2_set_lease_key,
2936 .new_lease_key = smb2_new_lease_key,
2937 .calc_signature = smb2_calc_signature,
2938 .is_read_op = smb21_is_read_op,
2939 .set_oplock_level = smb21_set_oplock_level,
a41a28bd 2940 .create_lease_buf = smb2_create_lease_buf,
b5c7cde3 2941 .parse_lease_buf = smb2_parse_lease_buf,
312bbc59 2942 .copychunk_range = smb2_copychunk_range,
7f6c5008 2943 .wp_retry_size = smb2_wp_retry_size,
52755808 2944 .dir_needs_close = smb2_dir_needs_close,
834170c8 2945 .enum_snapshots = smb3_enum_snapshots,
9d49640a 2946 .get_dfs_refer = smb2_get_dfs_refer,
ef65aaed 2947 .select_sectype = smb2_select_sectype,
95907fea
RS
2948#ifdef CONFIG_CIFS_XATTR
2949 .query_all_EAs = smb2_query_eas,
5517554e 2950 .set_EA = smb2_set_ea,
95907fea 2951#endif /* CIFS_XATTR */
2f1afe25
SP
2952#ifdef CONFIG_CIFS_ACL
2953 .get_acl = get_smb2_acl,
2954 .get_acl_by_fid = get_smb2_acl_by_fid,
366ed846 2955 .set_acl = set_smb2_acl,
2f1afe25 2956#endif /* CIFS_ACL */
53ef1016 2957};
38107d45
SF
2958
2959struct smb_version_operations smb30_operations = {
2960 .compare_fids = smb2_compare_fids,
2961 .setup_request = smb2_setup_request,
2962 .setup_async_request = smb2_setup_async_request,
2963 .check_receive = smb2_check_receive,
2964 .add_credits = smb2_add_credits,
2965 .set_credits = smb2_set_credits,
2966 .get_credits_field = smb2_get_credits_field,
2967 .get_credits = smb2_get_credits,
cb7e9eab 2968 .wait_mtu_credits = smb2_wait_mtu_credits,
38107d45
SF
2969 .get_next_mid = smb2_get_next_mid,
2970 .read_data_offset = smb2_read_data_offset,
2971 .read_data_length = smb2_read_data_length,
2972 .map_error = map_smb2_to_linux_error,
2973 .find_mid = smb2_find_mid,
2974 .check_message = smb2_check_message,
2975 .dump_detail = smb2_dump_detail,
2976 .clear_stats = smb2_clear_stats,
2977 .print_stats = smb2_print_stats,
769ee6a4 2978 .dump_share_caps = smb2_dump_share_caps,
38107d45 2979 .is_oplock_break = smb2_is_valid_oplock_break,
38bd4906 2980 .handle_cancelled_mid = smb2_handle_cancelled_mid,
c11f1df5 2981 .downgrade_oplock = smb2_downgrade_oplock,
38107d45
SF
2982 .need_neg = smb2_need_neg,
2983 .negotiate = smb2_negotiate,
2984 .negotiate_wsize = smb2_negotiate_wsize,
2985 .negotiate_rsize = smb2_negotiate_rsize,
2986 .sess_setup = SMB2_sess_setup,
2987 .logoff = SMB2_logoff,
2988 .tree_connect = SMB2_tcon,
2989 .tree_disconnect = SMB2_tdis,
af6a12ea 2990 .qfs_tcon = smb3_qfs_tcon,
38107d45
SF
2991 .is_path_accessible = smb2_is_path_accessible,
2992 .can_echo = smb2_can_echo,
2993 .echo = SMB2_echo,
2994 .query_path_info = smb2_query_path_info,
2995 .get_srv_inum = smb2_get_srv_inum,
2996 .query_file_info = smb2_query_file_info,
2997 .set_path_size = smb2_set_path_size,
2998 .set_file_size = smb2_set_file_size,
2999 .set_file_info = smb2_set_file_info,
64a5cfa6 3000 .set_compression = smb2_set_compression,
38107d45
SF
3001 .mkdir = smb2_mkdir,
3002 .mkdir_setinfo = smb2_mkdir_setinfo,
3003 .rmdir = smb2_rmdir,
3004 .unlink = smb2_unlink,
3005 .rename = smb2_rename_path,
3006 .create_hardlink = smb2_create_hardlink,
b42bf888 3007 .query_symlink = smb2_query_symlink,
c22870ea 3008 .query_mf_symlink = smb3_query_mf_symlink,
5ab97578 3009 .create_mf_symlink = smb3_create_mf_symlink,
38107d45
SF
3010 .open = smb2_open_file,
3011 .set_fid = smb2_set_fid,
3012 .close = smb2_close_file,
3013 .flush = smb2_flush_file,
3014 .async_readv = smb2_async_readv,
3015 .async_writev = smb2_async_writev,
3016 .sync_read = smb2_sync_read,
3017 .sync_write = smb2_sync_write,
3018 .query_dir_first = smb2_query_dir_first,
3019 .query_dir_next = smb2_query_dir_next,
3020 .close_dir = smb2_close_dir,
3021 .calc_smb_size = smb2_calc_size,
3022 .is_status_pending = smb2_is_status_pending,
511c54a2 3023 .is_session_expired = smb2_is_session_expired,
38107d45
SF
3024 .oplock_response = smb2_oplock_response,
3025 .queryfs = smb2_queryfs,
3026 .mand_lock = smb2_mand_lock,
3027 .mand_unlock_range = smb2_unlock_range,
3028 .push_mand_locks = smb2_push_mandatory_locks,
3029 .get_lease_key = smb2_get_lease_key,
3030 .set_lease_key = smb2_set_lease_key,
3031 .new_lease_key = smb2_new_lease_key,
373512ec 3032 .generate_signingkey = generate_smb30signingkey,
38107d45 3033 .calc_signature = smb3_calc_signature,
b3152e2c 3034 .set_integrity = smb3_set_integrity,
53ef1016 3035 .is_read_op = smb21_is_read_op,
42873b0a 3036 .set_oplock_level = smb3_set_oplock_level,
f047390a
PS
3037 .create_lease_buf = smb3_create_lease_buf,
3038 .parse_lease_buf = smb3_parse_lease_buf,
312bbc59 3039 .copychunk_range = smb2_copychunk_range,
ca9e7a1c 3040 .duplicate_extents = smb2_duplicate_extents,
ff1c038a 3041 .validate_negotiate = smb3_validate_negotiate,
7f6c5008 3042 .wp_retry_size = smb2_wp_retry_size,
52755808 3043 .dir_needs_close = smb2_dir_needs_close,
31742c5a 3044 .fallocate = smb3_fallocate,
834170c8 3045 .enum_snapshots = smb3_enum_snapshots,
026e93dc
PS
3046 .init_transform_rq = smb3_init_transform_rq,
3047 .free_transform_rq = smb3_free_transform_rq,
4326ed2f
PS
3048 .is_transform_hdr = smb3_is_transform_hdr,
3049 .receive_transform = smb3_receive_transform,
9d49640a 3050 .get_dfs_refer = smb2_get_dfs_refer,
ef65aaed 3051 .select_sectype = smb2_select_sectype,
95907fea
RS
3052#ifdef CONFIG_CIFS_XATTR
3053 .query_all_EAs = smb2_query_eas,
5517554e 3054 .set_EA = smb2_set_ea,
95907fea 3055#endif /* CIFS_XATTR */
2f1afe25
SP
3056#ifdef CONFIG_CIFS_ACL
3057 .get_acl = get_smb2_acl,
3058 .get_acl_by_fid = get_smb2_acl_by_fid,
366ed846 3059 .set_acl = set_smb2_acl,
2f1afe25 3060#endif /* CIFS_ACL */
1080ef75
SF
3061};
3062
aab1893d
SF
3063#ifdef CONFIG_CIFS_SMB311
3064struct smb_version_operations smb311_operations = {
3065 .compare_fids = smb2_compare_fids,
3066 .setup_request = smb2_setup_request,
3067 .setup_async_request = smb2_setup_async_request,
3068 .check_receive = smb2_check_receive,
3069 .add_credits = smb2_add_credits,
3070 .set_credits = smb2_set_credits,
3071 .get_credits_field = smb2_get_credits_field,
3072 .get_credits = smb2_get_credits,
3073 .wait_mtu_credits = smb2_wait_mtu_credits,
3074 .get_next_mid = smb2_get_next_mid,
3075 .read_data_offset = smb2_read_data_offset,
3076 .read_data_length = smb2_read_data_length,
3077 .map_error = map_smb2_to_linux_error,
3078 .find_mid = smb2_find_mid,
3079 .check_message = smb2_check_message,
3080 .dump_detail = smb2_dump_detail,
3081 .clear_stats = smb2_clear_stats,
3082 .print_stats = smb2_print_stats,
3083 .dump_share_caps = smb2_dump_share_caps,
3084 .is_oplock_break = smb2_is_valid_oplock_break,
38bd4906 3085 .handle_cancelled_mid = smb2_handle_cancelled_mid,
aab1893d
SF
3086 .downgrade_oplock = smb2_downgrade_oplock,
3087 .need_neg = smb2_need_neg,
3088 .negotiate = smb2_negotiate,
3089 .negotiate_wsize = smb2_negotiate_wsize,
3090 .negotiate_rsize = smb2_negotiate_rsize,
3091 .sess_setup = SMB2_sess_setup,
3092 .logoff = SMB2_logoff,
3093 .tree_connect = SMB2_tcon,
3094 .tree_disconnect = SMB2_tdis,
3095 .qfs_tcon = smb3_qfs_tcon,
3096 .is_path_accessible = smb2_is_path_accessible,
3097 .can_echo = smb2_can_echo,
3098 .echo = SMB2_echo,
3099 .query_path_info = smb2_query_path_info,
3100 .get_srv_inum = smb2_get_srv_inum,
3101 .query_file_info = smb2_query_file_info,
3102 .set_path_size = smb2_set_path_size,
3103 .set_file_size = smb2_set_file_size,
3104 .set_file_info = smb2_set_file_info,
3105 .set_compression = smb2_set_compression,
3106 .mkdir = smb2_mkdir,
3107 .mkdir_setinfo = smb2_mkdir_setinfo,
3108 .rmdir = smb2_rmdir,
3109 .unlink = smb2_unlink,
3110 .rename = smb2_rename_path,
3111 .create_hardlink = smb2_create_hardlink,
3112 .query_symlink = smb2_query_symlink,
3113 .query_mf_symlink = smb3_query_mf_symlink,
3114 .create_mf_symlink = smb3_create_mf_symlink,
3115 .open = smb2_open_file,
3116 .set_fid = smb2_set_fid,
3117 .close = smb2_close_file,
3118 .flush = smb2_flush_file,
3119 .async_readv = smb2_async_readv,
3120 .async_writev = smb2_async_writev,
3121 .sync_read = smb2_sync_read,
3122 .sync_write = smb2_sync_write,
3123 .query_dir_first = smb2_query_dir_first,
3124 .query_dir_next = smb2_query_dir_next,
3125 .close_dir = smb2_close_dir,
3126 .calc_smb_size = smb2_calc_size,
3127 .is_status_pending = smb2_is_status_pending,
511c54a2 3128 .is_session_expired = smb2_is_session_expired,
aab1893d
SF
3129 .oplock_response = smb2_oplock_response,
3130 .queryfs = smb2_queryfs,
3131 .mand_lock = smb2_mand_lock,
3132 .mand_unlock_range = smb2_unlock_range,
3133 .push_mand_locks = smb2_push_mandatory_locks,
3134 .get_lease_key = smb2_get_lease_key,
3135 .set_lease_key = smb2_set_lease_key,
3136 .new_lease_key = smb2_new_lease_key,
373512ec 3137 .generate_signingkey = generate_smb311signingkey,
aab1893d 3138 .calc_signature = smb3_calc_signature,
b3152e2c 3139 .set_integrity = smb3_set_integrity,
aab1893d
SF
3140 .is_read_op = smb21_is_read_op,
3141 .set_oplock_level = smb3_set_oplock_level,
3142 .create_lease_buf = smb3_create_lease_buf,
3143 .parse_lease_buf = smb3_parse_lease_buf,
312bbc59 3144 .copychunk_range = smb2_copychunk_range,
02b16665 3145 .duplicate_extents = smb2_duplicate_extents,
aab1893d
SF
3146/* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
3147 .wp_retry_size = smb2_wp_retry_size,
3148 .dir_needs_close = smb2_dir_needs_close,
3149 .fallocate = smb3_fallocate,
834170c8 3150 .enum_snapshots = smb3_enum_snapshots,
026e93dc
PS
3151 .init_transform_rq = smb3_init_transform_rq,
3152 .free_transform_rq = smb3_free_transform_rq,
4326ed2f
PS
3153 .is_transform_hdr = smb3_is_transform_hdr,
3154 .receive_transform = smb3_receive_transform,
9d49640a 3155 .get_dfs_refer = smb2_get_dfs_refer,
ef65aaed 3156 .select_sectype = smb2_select_sectype,
95907fea
RS
3157#ifdef CONFIG_CIFS_XATTR
3158 .query_all_EAs = smb2_query_eas,
5517554e 3159 .set_EA = smb2_set_ea,
95907fea 3160#endif /* CIFS_XATTR */
aab1893d
SF
3161};
3162#endif /* CIFS_SMB311 */
3163
dd446b16
SF
3164struct smb_version_values smb20_values = {
3165 .version_string = SMB20_VERSION_STRING,
3166 .protocol_id = SMB20_PROT_ID,
3167 .req_capabilities = 0, /* MBZ */
3168 .large_lock_type = 0,
3169 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3170 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3171 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3172 .header_size = sizeof(struct smb2_hdr),
93012bf9 3173 .header_preamble_size = 4,
dd446b16
SF
3174 .max_header_size = MAX_SMB2_HDR_SIZE,
3175 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3176 .lock_cmd = SMB2_LOCK,
3177 .cap_unix = 0,
3178 .cap_nt_find = SMB2_NT_FIND,
3179 .cap_large_files = SMB2_LARGE_FILES,
50285882
JL
3180 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3181 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
a41a28bd 3182 .create_lease_size = sizeof(struct create_lease),
dd446b16
SF
3183};
3184
1080ef75
SF
3185struct smb_version_values smb21_values = {
3186 .version_string = SMB21_VERSION_STRING,
e4aa25e7
SF
3187 .protocol_id = SMB21_PROT_ID,
3188 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
3189 .large_lock_type = 0,
3190 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3191 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3192 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3193 .header_size = sizeof(struct smb2_hdr),
93012bf9 3194 .header_preamble_size = 4,
e4aa25e7
SF
3195 .max_header_size = MAX_SMB2_HDR_SIZE,
3196 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3197 .lock_cmd = SMB2_LOCK,
3198 .cap_unix = 0,
3199 .cap_nt_find = SMB2_NT_FIND,
3200 .cap_large_files = SMB2_LARGE_FILES,
50285882
JL
3201 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3202 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
a41a28bd 3203 .create_lease_size = sizeof(struct create_lease),
e4aa25e7
SF
3204};
3205
9764c02f
SF
3206struct smb_version_values smb3any_values = {
3207 .version_string = SMB3ANY_VERSION_STRING,
3208 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3209 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
3210 .large_lock_type = 0,
3211 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3212 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3213 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3214 .header_size = sizeof(struct smb2_hdr),
93012bf9 3215 .header_preamble_size = 4,
9764c02f
SF
3216 .max_header_size = MAX_SMB2_HDR_SIZE,
3217 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3218 .lock_cmd = SMB2_LOCK,
3219 .cap_unix = 0,
3220 .cap_nt_find = SMB2_NT_FIND,
3221 .cap_large_files = SMB2_LARGE_FILES,
3222 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3223 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3224 .create_lease_size = sizeof(struct create_lease_v2),
3225};
3226
3227struct smb_version_values smbdefault_values = {
3228 .version_string = SMBDEFAULT_VERSION_STRING,
3229 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3230 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
3231 .large_lock_type = 0,
3232 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3233 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3234 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3235 .header_size = sizeof(struct smb2_hdr),
93012bf9 3236 .header_preamble_size = 4,
9764c02f
SF
3237 .max_header_size = MAX_SMB2_HDR_SIZE,
3238 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3239 .lock_cmd = SMB2_LOCK,
3240 .cap_unix = 0,
3241 .cap_nt_find = SMB2_NT_FIND,
3242 .cap_large_files = SMB2_LARGE_FILES,
3243 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3244 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3245 .create_lease_size = sizeof(struct create_lease_v2),
3246};
3247
e4aa25e7
SF
3248struct smb_version_values smb30_values = {
3249 .version_string = SMB30_VERSION_STRING,
3250 .protocol_id = SMB30_PROT_ID,
373512ec 3251 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
027e8eec
PS
3252 .large_lock_type = 0,
3253 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3254 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3255 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
093b2bda 3256 .header_size = sizeof(struct smb2_hdr),
93012bf9 3257 .header_preamble_size = 4,
093b2bda 3258 .max_header_size = MAX_SMB2_HDR_SIZE,
09a4707e 3259 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
2dc7e1c0 3260 .lock_cmd = SMB2_LOCK,
29e20f9c
PS
3261 .cap_unix = 0,
3262 .cap_nt_find = SMB2_NT_FIND,
3263 .cap_large_files = SMB2_LARGE_FILES,
50285882
JL
3264 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3265 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
f047390a 3266 .create_lease_size = sizeof(struct create_lease_v2),
1080ef75 3267};
20b6d8b4
SF
3268
3269struct smb_version_values smb302_values = {
3270 .version_string = SMB302_VERSION_STRING,
3271 .protocol_id = SMB302_PROT_ID,
373512ec 3272 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
20b6d8b4
SF
3273 .large_lock_type = 0,
3274 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3275 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3276 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3277 .header_size = sizeof(struct smb2_hdr),
93012bf9 3278 .header_preamble_size = 4,
20b6d8b4
SF
3279 .max_header_size = MAX_SMB2_HDR_SIZE,
3280 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3281 .lock_cmd = SMB2_LOCK,
3282 .cap_unix = 0,
3283 .cap_nt_find = SMB2_NT_FIND,
3284 .cap_large_files = SMB2_LARGE_FILES,
50285882
JL
3285 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3286 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
f047390a 3287 .create_lease_size = sizeof(struct create_lease_v2),
20b6d8b4 3288};
5f7fbf73
SF
3289
3290#ifdef CONFIG_CIFS_SMB311
3291struct smb_version_values smb311_values = {
3292 .version_string = SMB311_VERSION_STRING,
3293 .protocol_id = SMB311_PROT_ID,
1955880b 3294 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
5f7fbf73
SF
3295 .large_lock_type = 0,
3296 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3297 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3298 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3299 .header_size = sizeof(struct smb2_hdr),
93012bf9 3300 .header_preamble_size = 4,
5f7fbf73
SF
3301 .max_header_size = MAX_SMB2_HDR_SIZE,
3302 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3303 .lock_cmd = SMB2_LOCK,
3304 .cap_unix = 0,
3305 .cap_nt_find = SMB2_NT_FIND,
3306 .cap_large_files = SMB2_LARGE_FILES,
3307 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3308 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3309 .create_lease_size = sizeof(struct create_lease_v2),
3310};
3311#endif /* SMB311 */