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