]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - fs/cifs/smb2ops.c
cifs: Standardize logging output
[mirror_ubuntu-jammy-kernel.git] / fs / cifs / smb2ops.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * SMB2 version specific operations
4 *
5 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
6 */
7
8 #include <linux/pagemap.h>
9 #include <linux/vfs.h>
10 #include <linux/falloc.h>
11 #include <linux/scatterlist.h>
12 #include <linux/uuid.h>
13 #include <linux/sort.h>
14 #include <crypto/aead.h>
15 #include "cifsfs.h"
16 #include "cifsglob.h"
17 #include "smb2pdu.h"
18 #include "smb2proto.h"
19 #include "cifsproto.h"
20 #include "cifs_debug.h"
21 #include "cifs_unicode.h"
22 #include "smb2status.h"
23 #include "smb2glob.h"
24 #include "cifs_ioctl.h"
25 #include "smbdirect.h"
26
27 /* Change credits for different ops and return the total number of credits */
28 static int
29 change_conf(struct TCP_Server_Info *server)
30 {
31 server->credits += server->echo_credits + server->oplock_credits;
32 server->oplock_credits = server->echo_credits = 0;
33 switch (server->credits) {
34 case 0:
35 return 0;
36 case 1:
37 server->echoes = false;
38 server->oplocks = false;
39 break;
40 case 2:
41 server->echoes = true;
42 server->oplocks = false;
43 server->echo_credits = 1;
44 break;
45 default:
46 server->echoes = true;
47 if (enable_oplocks) {
48 server->oplocks = true;
49 server->oplock_credits = 1;
50 } else
51 server->oplocks = false;
52
53 server->echo_credits = 1;
54 }
55 server->credits -= server->echo_credits + server->oplock_credits;
56 return server->credits + server->echo_credits + server->oplock_credits;
57 }
58
59 static void
60 smb2_add_credits(struct TCP_Server_Info *server,
61 const struct cifs_credits *credits, const int optype)
62 {
63 int *val, rc = -1;
64 unsigned int add = credits->value;
65 unsigned int instance = credits->instance;
66 bool reconnect_detected = false;
67
68 spin_lock(&server->req_lock);
69 val = server->ops->get_credits_field(server, optype);
70
71 /* eg found case where write overlapping reconnect messed up credits */
72 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
73 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
74 server->hostname, *val);
75 if ((instance == 0) || (instance == server->reconnect_instance))
76 *val += add;
77 else
78 reconnect_detected = true;
79
80 if (*val > 65000) {
81 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
82 pr_warn_once("server overflowed SMB3 credits\n");
83 }
84 server->in_flight--;
85 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
86 rc = change_conf(server);
87 /*
88 * Sometimes server returns 0 credits on oplock break ack - we need to
89 * rebalance credits in this case.
90 */
91 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
92 server->oplocks) {
93 if (server->credits > 1) {
94 server->credits--;
95 server->oplock_credits++;
96 }
97 }
98 spin_unlock(&server->req_lock);
99 wake_up(&server->request_q);
100
101 if (reconnect_detected)
102 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
103 add, instance);
104
105 if (server->tcpStatus == CifsNeedReconnect
106 || server->tcpStatus == CifsExiting)
107 return;
108
109 switch (rc) {
110 case -1:
111 /* change_conf hasn't been executed */
112 break;
113 case 0:
114 cifs_server_dbg(VFS, "Possible client or server bug - zero credits\n");
115 break;
116 case 1:
117 cifs_server_dbg(VFS, "disabling echoes and oplocks\n");
118 break;
119 case 2:
120 cifs_dbg(FYI, "disabling oplocks\n");
121 break;
122 default:
123 cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
124 }
125 }
126
127 static void
128 smb2_set_credits(struct TCP_Server_Info *server, const int val)
129 {
130 spin_lock(&server->req_lock);
131 server->credits = val;
132 if (val == 1)
133 server->reconnect_instance++;
134 spin_unlock(&server->req_lock);
135 /* don't log while holding the lock */
136 if (val == 1)
137 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
138 }
139
140 static int *
141 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
142 {
143 switch (optype) {
144 case CIFS_ECHO_OP:
145 return &server->echo_credits;
146 case CIFS_OBREAK_OP:
147 return &server->oplock_credits;
148 default:
149 return &server->credits;
150 }
151 }
152
153 static unsigned int
154 smb2_get_credits(struct mid_q_entry *mid)
155 {
156 return mid->credits_received;
157 }
158
159 static int
160 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
161 unsigned int *num, struct cifs_credits *credits)
162 {
163 int rc = 0;
164 unsigned int scredits;
165
166 spin_lock(&server->req_lock);
167 while (1) {
168 if (server->credits <= 0) {
169 spin_unlock(&server->req_lock);
170 cifs_num_waiters_inc(server);
171 rc = wait_event_killable(server->request_q,
172 has_credits(server, &server->credits, 1));
173 cifs_num_waiters_dec(server);
174 if (rc)
175 return rc;
176 spin_lock(&server->req_lock);
177 } else {
178 if (server->tcpStatus == CifsExiting) {
179 spin_unlock(&server->req_lock);
180 return -ENOENT;
181 }
182
183 scredits = server->credits;
184 /* can deadlock with reopen */
185 if (scredits <= 8) {
186 *num = SMB2_MAX_BUFFER_SIZE;
187 credits->value = 0;
188 credits->instance = 0;
189 break;
190 }
191
192 /* leave some credits for reopen and other ops */
193 scredits -= 8;
194 *num = min_t(unsigned int, size,
195 scredits * SMB2_MAX_BUFFER_SIZE);
196
197 credits->value =
198 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
199 credits->instance = server->reconnect_instance;
200 server->credits -= credits->value;
201 server->in_flight++;
202 if (server->in_flight > server->max_in_flight)
203 server->max_in_flight = server->in_flight;
204 break;
205 }
206 }
207 spin_unlock(&server->req_lock);
208 return rc;
209 }
210
211 static int
212 smb2_adjust_credits(struct TCP_Server_Info *server,
213 struct cifs_credits *credits,
214 const unsigned int payload_size)
215 {
216 int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
217
218 if (!credits->value || credits->value == new_val)
219 return 0;
220
221 if (credits->value < new_val) {
222 WARN_ONCE(1, "request has less credits (%d) than required (%d)",
223 credits->value, new_val);
224 return -ENOTSUPP;
225 }
226
227 spin_lock(&server->req_lock);
228
229 if (server->reconnect_instance != credits->instance) {
230 spin_unlock(&server->req_lock);
231 cifs_server_dbg(VFS, "trying to return %d credits to old session\n",
232 credits->value - new_val);
233 return -EAGAIN;
234 }
235
236 server->credits += credits->value - new_val;
237 spin_unlock(&server->req_lock);
238 wake_up(&server->request_q);
239 credits->value = new_val;
240 return 0;
241 }
242
243 static __u64
244 smb2_get_next_mid(struct TCP_Server_Info *server)
245 {
246 __u64 mid;
247 /* for SMB2 we need the current value */
248 spin_lock(&GlobalMid_Lock);
249 mid = server->CurrentMid++;
250 spin_unlock(&GlobalMid_Lock);
251 return mid;
252 }
253
254 static void
255 smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
256 {
257 spin_lock(&GlobalMid_Lock);
258 if (server->CurrentMid >= val)
259 server->CurrentMid -= val;
260 spin_unlock(&GlobalMid_Lock);
261 }
262
263 static struct mid_q_entry *
264 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
265 {
266 struct mid_q_entry *mid;
267 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
268 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
269
270 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
271 cifs_server_dbg(VFS, "Encrypted frame parsing not supported yet\n");
272 return NULL;
273 }
274
275 spin_lock(&GlobalMid_Lock);
276 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
277 if ((mid->mid == wire_mid) &&
278 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
279 (mid->command == shdr->Command)) {
280 kref_get(&mid->refcount);
281 spin_unlock(&GlobalMid_Lock);
282 return mid;
283 }
284 }
285 spin_unlock(&GlobalMid_Lock);
286 return NULL;
287 }
288
289 static void
290 smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
291 {
292 #ifdef CONFIG_CIFS_DEBUG2
293 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
294
295 cifs_server_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
296 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
297 shdr->ProcessId);
298 cifs_server_dbg(VFS, "smb buf %p len %u\n", buf,
299 server->ops->calc_smb_size(buf, server));
300 #endif
301 }
302
303 static bool
304 smb2_need_neg(struct TCP_Server_Info *server)
305 {
306 return server->max_read == 0;
307 }
308
309 static int
310 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
311 {
312 int rc;
313
314 cifs_ses_server(ses)->CurrentMid = 0;
315 rc = SMB2_negotiate(xid, ses);
316 /* BB we probably don't need to retry with modern servers */
317 if (rc == -EAGAIN)
318 rc = -EHOSTDOWN;
319 return rc;
320 }
321
322 static unsigned int
323 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
324 {
325 struct TCP_Server_Info *server = tcon->ses->server;
326 unsigned int wsize;
327
328 /* start with specified wsize, or default */
329 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
330 wsize = min_t(unsigned int, wsize, server->max_write);
331 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
332 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
333
334 return wsize;
335 }
336
337 static unsigned int
338 smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
339 {
340 struct TCP_Server_Info *server = tcon->ses->server;
341 unsigned int wsize;
342
343 /* start with specified wsize, or default */
344 wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
345 wsize = min_t(unsigned int, wsize, server->max_write);
346 #ifdef CONFIG_CIFS_SMB_DIRECT
347 if (server->rdma) {
348 if (server->sign)
349 /*
350 * Account for SMB2 data transfer packet header and
351 * possible encryption header
352 */
353 wsize = min_t(unsigned int,
354 wsize,
355 server->smbd_conn->max_fragmented_send_size -
356 SMB2_READWRITE_PDU_HEADER_SIZE -
357 sizeof(struct smb2_transform_hdr));
358 else
359 wsize = min_t(unsigned int,
360 wsize, server->smbd_conn->max_readwrite_size);
361 }
362 #endif
363 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
364 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
365
366 return wsize;
367 }
368
369 static unsigned int
370 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
371 {
372 struct TCP_Server_Info *server = tcon->ses->server;
373 unsigned int rsize;
374
375 /* start with specified rsize, or default */
376 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
377 rsize = min_t(unsigned int, rsize, server->max_read);
378
379 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
380 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
381
382 return rsize;
383 }
384
385 static unsigned int
386 smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
387 {
388 struct TCP_Server_Info *server = tcon->ses->server;
389 unsigned int rsize;
390
391 /* start with specified rsize, or default */
392 rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
393 rsize = min_t(unsigned int, rsize, server->max_read);
394 #ifdef CONFIG_CIFS_SMB_DIRECT
395 if (server->rdma) {
396 if (server->sign)
397 /*
398 * Account for SMB2 data transfer packet header and
399 * possible encryption header
400 */
401 rsize = min_t(unsigned int,
402 rsize,
403 server->smbd_conn->max_fragmented_recv_size -
404 SMB2_READWRITE_PDU_HEADER_SIZE -
405 sizeof(struct smb2_transform_hdr));
406 else
407 rsize = min_t(unsigned int,
408 rsize, server->smbd_conn->max_readwrite_size);
409 }
410 #endif
411
412 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
413 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
414
415 return rsize;
416 }
417
418 static int
419 parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
420 size_t buf_len,
421 struct cifs_server_iface **iface_list,
422 size_t *iface_count)
423 {
424 struct network_interface_info_ioctl_rsp *p;
425 struct sockaddr_in *addr4;
426 struct sockaddr_in6 *addr6;
427 struct iface_info_ipv4 *p4;
428 struct iface_info_ipv6 *p6;
429 struct cifs_server_iface *info;
430 ssize_t bytes_left;
431 size_t next = 0;
432 int nb_iface = 0;
433 int rc = 0;
434
435 *iface_list = NULL;
436 *iface_count = 0;
437
438 /*
439 * Fist pass: count and sanity check
440 */
441
442 bytes_left = buf_len;
443 p = buf;
444 while (bytes_left >= sizeof(*p)) {
445 nb_iface++;
446 next = le32_to_cpu(p->Next);
447 if (!next) {
448 bytes_left -= sizeof(*p);
449 break;
450 }
451 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
452 bytes_left -= next;
453 }
454
455 if (!nb_iface) {
456 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
457 rc = -EINVAL;
458 goto out;
459 }
460
461 if (bytes_left || p->Next)
462 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
463
464
465 /*
466 * Second pass: extract info to internal structure
467 */
468
469 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
470 if (!*iface_list) {
471 rc = -ENOMEM;
472 goto out;
473 }
474
475 info = *iface_list;
476 bytes_left = buf_len;
477 p = buf;
478 while (bytes_left >= sizeof(*p)) {
479 info->speed = le64_to_cpu(p->LinkSpeed);
480 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE);
481 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE);
482
483 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
484 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
485 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
486 le32_to_cpu(p->Capability));
487
488 switch (p->Family) {
489 /*
490 * The kernel and wire socket structures have the same
491 * layout and use network byte order but make the
492 * conversion explicit in case either one changes.
493 */
494 case INTERNETWORK:
495 addr4 = (struct sockaddr_in *)&info->sockaddr;
496 p4 = (struct iface_info_ipv4 *)p->Buffer;
497 addr4->sin_family = AF_INET;
498 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
499
500 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
501 addr4->sin_port = cpu_to_be16(CIFS_PORT);
502
503 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
504 &addr4->sin_addr);
505 break;
506 case INTERNETWORKV6:
507 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
508 p6 = (struct iface_info_ipv6 *)p->Buffer;
509 addr6->sin6_family = AF_INET6;
510 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
511
512 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
513 addr6->sin6_flowinfo = 0;
514 addr6->sin6_scope_id = 0;
515 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
516
517 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
518 &addr6->sin6_addr);
519 break;
520 default:
521 cifs_dbg(VFS,
522 "%s: skipping unsupported socket family\n",
523 __func__);
524 goto next_iface;
525 }
526
527 (*iface_count)++;
528 info++;
529 next_iface:
530 next = le32_to_cpu(p->Next);
531 if (!next)
532 break;
533 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
534 bytes_left -= next;
535 }
536
537 if (!*iface_count) {
538 rc = -EINVAL;
539 goto out;
540 }
541
542 out:
543 if (rc) {
544 kfree(*iface_list);
545 *iface_count = 0;
546 *iface_list = NULL;
547 }
548 return rc;
549 }
550
551 static int compare_iface(const void *ia, const void *ib)
552 {
553 const struct cifs_server_iface *a = (struct cifs_server_iface *)ia;
554 const struct cifs_server_iface *b = (struct cifs_server_iface *)ib;
555
556 return a->speed == b->speed ? 0 : (a->speed > b->speed ? -1 : 1);
557 }
558
559 static int
560 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
561 {
562 int rc;
563 unsigned int ret_data_len = 0;
564 struct network_interface_info_ioctl_rsp *out_buf = NULL;
565 struct cifs_server_iface *iface_list;
566 size_t iface_count;
567 struct cifs_ses *ses = tcon->ses;
568
569 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
570 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
571 NULL /* no data input */, 0 /* no data input */,
572 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
573 if (rc == -EOPNOTSUPP) {
574 cifs_dbg(FYI,
575 "server does not support query network interfaces\n");
576 goto out;
577 } else if (rc != 0) {
578 cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
579 goto out;
580 }
581
582 rc = parse_server_interfaces(out_buf, ret_data_len,
583 &iface_list, &iface_count);
584 if (rc)
585 goto out;
586
587 /* sort interfaces from fastest to slowest */
588 sort(iface_list, iface_count, sizeof(*iface_list), compare_iface, NULL);
589
590 spin_lock(&ses->iface_lock);
591 kfree(ses->iface_list);
592 ses->iface_list = iface_list;
593 ses->iface_count = iface_count;
594 ses->iface_last_update = jiffies;
595 spin_unlock(&ses->iface_lock);
596
597 out:
598 kfree(out_buf);
599 return rc;
600 }
601
602 static void
603 smb2_close_cached_fid(struct kref *ref)
604 {
605 struct cached_fid *cfid = container_of(ref, struct cached_fid,
606 refcount);
607
608 if (cfid->is_valid) {
609 cifs_dbg(FYI, "clear cached root file handle\n");
610 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
611 cfid->fid->volatile_fid);
612 cfid->is_valid = false;
613 cfid->file_all_info_is_valid = false;
614 cfid->has_lease = false;
615 }
616 }
617
618 void close_shroot(struct cached_fid *cfid)
619 {
620 mutex_lock(&cfid->fid_mutex);
621 kref_put(&cfid->refcount, smb2_close_cached_fid);
622 mutex_unlock(&cfid->fid_mutex);
623 }
624
625 void close_shroot_lease_locked(struct cached_fid *cfid)
626 {
627 if (cfid->has_lease) {
628 cfid->has_lease = false;
629 kref_put(&cfid->refcount, smb2_close_cached_fid);
630 }
631 }
632
633 void close_shroot_lease(struct cached_fid *cfid)
634 {
635 mutex_lock(&cfid->fid_mutex);
636 close_shroot_lease_locked(cfid);
637 mutex_unlock(&cfid->fid_mutex);
638 }
639
640 void
641 smb2_cached_lease_break(struct work_struct *work)
642 {
643 struct cached_fid *cfid = container_of(work,
644 struct cached_fid, lease_break);
645
646 close_shroot_lease(cfid);
647 }
648
649 /*
650 * Open the directory at the root of a share
651 */
652 int open_shroot(unsigned int xid, struct cifs_tcon *tcon,
653 struct cifs_sb_info *cifs_sb, struct cifs_fid *pfid)
654 {
655 struct cifs_ses *ses = tcon->ses;
656 struct TCP_Server_Info *server = ses->server;
657 struct cifs_open_parms oparms;
658 struct smb2_create_rsp *o_rsp = NULL;
659 struct smb2_query_info_rsp *qi_rsp = NULL;
660 int resp_buftype[2];
661 struct smb_rqst rqst[2];
662 struct kvec rsp_iov[2];
663 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
664 struct kvec qi_iov[1];
665 int rc, flags = 0;
666 __le16 utf16_path = 0; /* Null - since an open of top of share */
667 u8 oplock = SMB2_OPLOCK_LEVEL_II;
668
669 mutex_lock(&tcon->crfid.fid_mutex);
670 if (tcon->crfid.is_valid) {
671 cifs_dbg(FYI, "found a cached root file handle\n");
672 memcpy(pfid, tcon->crfid.fid, sizeof(struct cifs_fid));
673 kref_get(&tcon->crfid.refcount);
674 mutex_unlock(&tcon->crfid.fid_mutex);
675 return 0;
676 }
677
678 /*
679 * We do not hold the lock for the open because in case
680 * SMB2_open needs to reconnect, it will end up calling
681 * cifs_mark_open_files_invalid() which takes the lock again
682 * thus causing a deadlock
683 */
684
685 mutex_unlock(&tcon->crfid.fid_mutex);
686
687 if (smb3_encryption_required(tcon))
688 flags |= CIFS_TRANSFORM_REQ;
689
690 if (!server->ops->new_lease_key)
691 return -EIO;
692
693 server->ops->new_lease_key(pfid);
694
695 memset(rqst, 0, sizeof(rqst));
696 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
697 memset(rsp_iov, 0, sizeof(rsp_iov));
698
699 /* Open */
700 memset(&open_iov, 0, sizeof(open_iov));
701 rqst[0].rq_iov = open_iov;
702 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
703
704 oparms.tcon = tcon;
705 oparms.create_options = cifs_create_options(cifs_sb, 0);
706 oparms.desired_access = FILE_READ_ATTRIBUTES;
707 oparms.disposition = FILE_OPEN;
708 oparms.fid = pfid;
709 oparms.reconnect = false;
710
711 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, &utf16_path);
712 if (rc)
713 goto oshr_free;
714 smb2_set_next_command(tcon, &rqst[0]);
715
716 memset(&qi_iov, 0, sizeof(qi_iov));
717 rqst[1].rq_iov = qi_iov;
718 rqst[1].rq_nvec = 1;
719
720 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
721 COMPOUND_FID, FILE_ALL_INFORMATION,
722 SMB2_O_INFO_FILE, 0,
723 sizeof(struct smb2_file_all_info) +
724 PATH_MAX * 2, 0, NULL);
725 if (rc)
726 goto oshr_free;
727
728 smb2_set_related(&rqst[1]);
729
730 rc = compound_send_recv(xid, ses, flags, 2, rqst,
731 resp_buftype, rsp_iov);
732 mutex_lock(&tcon->crfid.fid_mutex);
733
734 /*
735 * Now we need to check again as the cached root might have
736 * been successfully re-opened from a concurrent process
737 */
738
739 if (tcon->crfid.is_valid) {
740 /* work was already done */
741
742 /* stash fids for close() later */
743 struct cifs_fid fid = {
744 .persistent_fid = pfid->persistent_fid,
745 .volatile_fid = pfid->volatile_fid,
746 };
747
748 /*
749 * caller expects this func to set pfid to a valid
750 * cached root, so we copy the existing one and get a
751 * reference.
752 */
753 memcpy(pfid, tcon->crfid.fid, sizeof(*pfid));
754 kref_get(&tcon->crfid.refcount);
755
756 mutex_unlock(&tcon->crfid.fid_mutex);
757
758 if (rc == 0) {
759 /* close extra handle outside of crit sec */
760 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
761 }
762 goto oshr_free;
763 }
764
765 /* Cached root is still invalid, continue normaly */
766
767 if (rc) {
768 if (rc == -EREMCHG) {
769 tcon->need_reconnect = true;
770 pr_warn_once("server share %s deleted\n",
771 tcon->treeName);
772 }
773 goto oshr_exit;
774 }
775
776 atomic_inc(&tcon->num_remote_opens);
777
778 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
779 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
780 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
781 #ifdef CONFIG_CIFS_DEBUG2
782 oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
783 #endif /* CIFS_DEBUG2 */
784
785 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
786 tcon->crfid.tcon = tcon;
787 tcon->crfid.is_valid = true;
788 kref_init(&tcon->crfid.refcount);
789
790 /* BB TBD check to see if oplock level check can be removed below */
791 if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
792 kref_get(&tcon->crfid.refcount);
793 tcon->crfid.has_lease = true;
794 smb2_parse_contexts(server, o_rsp,
795 &oparms.fid->epoch,
796 oparms.fid->lease_key, &oplock,
797 NULL, NULL);
798 } else
799 goto oshr_exit;
800
801 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
802 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
803 goto oshr_exit;
804 if (!smb2_validate_and_copy_iov(
805 le16_to_cpu(qi_rsp->OutputBufferOffset),
806 sizeof(struct smb2_file_all_info),
807 &rsp_iov[1], sizeof(struct smb2_file_all_info),
808 (char *)&tcon->crfid.file_all_info))
809 tcon->crfid.file_all_info_is_valid = true;
810
811 oshr_exit:
812 mutex_unlock(&tcon->crfid.fid_mutex);
813 oshr_free:
814 SMB2_open_free(&rqst[0]);
815 SMB2_query_info_free(&rqst[1]);
816 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
817 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
818 return rc;
819 }
820
821 static void
822 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
823 struct cifs_sb_info *cifs_sb)
824 {
825 int rc;
826 __le16 srch_path = 0; /* Null - open root of share */
827 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
828 struct cifs_open_parms oparms;
829 struct cifs_fid fid;
830 bool no_cached_open = tcon->nohandlecache;
831
832 oparms.tcon = tcon;
833 oparms.desired_access = FILE_READ_ATTRIBUTES;
834 oparms.disposition = FILE_OPEN;
835 oparms.create_options = cifs_create_options(cifs_sb, 0);
836 oparms.fid = &fid;
837 oparms.reconnect = false;
838
839 if (no_cached_open)
840 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
841 NULL, NULL);
842 else
843 rc = open_shroot(xid, tcon, cifs_sb, &fid);
844
845 if (rc)
846 return;
847
848 SMB3_request_interfaces(xid, tcon);
849
850 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
851 FS_ATTRIBUTE_INFORMATION);
852 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
853 FS_DEVICE_INFORMATION);
854 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
855 FS_VOLUME_INFORMATION);
856 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
857 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
858 if (no_cached_open)
859 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
860 else
861 close_shroot(&tcon->crfid);
862 }
863
864 static void
865 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
866 struct cifs_sb_info *cifs_sb)
867 {
868 int rc;
869 __le16 srch_path = 0; /* Null - open root of share */
870 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
871 struct cifs_open_parms oparms;
872 struct cifs_fid fid;
873
874 oparms.tcon = tcon;
875 oparms.desired_access = FILE_READ_ATTRIBUTES;
876 oparms.disposition = FILE_OPEN;
877 oparms.create_options = cifs_create_options(cifs_sb, 0);
878 oparms.fid = &fid;
879 oparms.reconnect = false;
880
881 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
882 NULL, NULL);
883 if (rc)
884 return;
885
886 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
887 FS_ATTRIBUTE_INFORMATION);
888 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
889 FS_DEVICE_INFORMATION);
890 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
891 }
892
893 static int
894 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
895 struct cifs_sb_info *cifs_sb, const char *full_path)
896 {
897 int rc;
898 __le16 *utf16_path;
899 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
900 struct cifs_open_parms oparms;
901 struct cifs_fid fid;
902
903 if ((*full_path == 0) && tcon->crfid.is_valid)
904 return 0;
905
906 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
907 if (!utf16_path)
908 return -ENOMEM;
909
910 oparms.tcon = tcon;
911 oparms.desired_access = FILE_READ_ATTRIBUTES;
912 oparms.disposition = FILE_OPEN;
913 oparms.create_options = cifs_create_options(cifs_sb, 0);
914 oparms.fid = &fid;
915 oparms.reconnect = false;
916
917 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
918 NULL);
919 if (rc) {
920 kfree(utf16_path);
921 return rc;
922 }
923
924 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
925 kfree(utf16_path);
926 return rc;
927 }
928
929 static int
930 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
931 struct cifs_sb_info *cifs_sb, const char *full_path,
932 u64 *uniqueid, FILE_ALL_INFO *data)
933 {
934 *uniqueid = le64_to_cpu(data->IndexNumber);
935 return 0;
936 }
937
938 static int
939 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
940 struct cifs_fid *fid, FILE_ALL_INFO *data)
941 {
942 int rc;
943 struct smb2_file_all_info *smb2_data;
944
945 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
946 GFP_KERNEL);
947 if (smb2_data == NULL)
948 return -ENOMEM;
949
950 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
951 smb2_data);
952 if (!rc)
953 move_smb2_info_to_cifs(data, smb2_data);
954 kfree(smb2_data);
955 return rc;
956 }
957
958 #ifdef CONFIG_CIFS_XATTR
959 static ssize_t
960 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
961 struct smb2_file_full_ea_info *src, size_t src_size,
962 const unsigned char *ea_name)
963 {
964 int rc = 0;
965 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
966 char *name, *value;
967 size_t buf_size = dst_size;
968 size_t name_len, value_len, user_name_len;
969
970 while (src_size > 0) {
971 name = &src->ea_data[0];
972 name_len = (size_t)src->ea_name_length;
973 value = &src->ea_data[src->ea_name_length + 1];
974 value_len = (size_t)le16_to_cpu(src->ea_value_length);
975
976 if (name_len == 0)
977 break;
978
979 if (src_size < 8 + name_len + 1 + value_len) {
980 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
981 rc = -EIO;
982 goto out;
983 }
984
985 if (ea_name) {
986 if (ea_name_len == name_len &&
987 memcmp(ea_name, name, name_len) == 0) {
988 rc = value_len;
989 if (dst_size == 0)
990 goto out;
991 if (dst_size < value_len) {
992 rc = -ERANGE;
993 goto out;
994 }
995 memcpy(dst, value, value_len);
996 goto out;
997 }
998 } else {
999 /* 'user.' plus a terminating null */
1000 user_name_len = 5 + 1 + name_len;
1001
1002 if (buf_size == 0) {
1003 /* skip copy - calc size only */
1004 rc += user_name_len;
1005 } else if (dst_size >= user_name_len) {
1006 dst_size -= user_name_len;
1007 memcpy(dst, "user.", 5);
1008 dst += 5;
1009 memcpy(dst, src->ea_data, name_len);
1010 dst += name_len;
1011 *dst = 0;
1012 ++dst;
1013 rc += user_name_len;
1014 } else {
1015 /* stop before overrun buffer */
1016 rc = -ERANGE;
1017 break;
1018 }
1019 }
1020
1021 if (!src->next_entry_offset)
1022 break;
1023
1024 if (src_size < le32_to_cpu(src->next_entry_offset)) {
1025 /* stop before overrun buffer */
1026 rc = -ERANGE;
1027 break;
1028 }
1029 src_size -= le32_to_cpu(src->next_entry_offset);
1030 src = (void *)((char *)src +
1031 le32_to_cpu(src->next_entry_offset));
1032 }
1033
1034 /* didn't find the named attribute */
1035 if (ea_name)
1036 rc = -ENODATA;
1037
1038 out:
1039 return (ssize_t)rc;
1040 }
1041
1042 static ssize_t
1043 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1044 const unsigned char *path, const unsigned char *ea_name,
1045 char *ea_data, size_t buf_size,
1046 struct cifs_sb_info *cifs_sb)
1047 {
1048 int rc;
1049 __le16 *utf16_path;
1050 struct kvec rsp_iov = {NULL, 0};
1051 int buftype = CIFS_NO_BUFFER;
1052 struct smb2_query_info_rsp *rsp;
1053 struct smb2_file_full_ea_info *info = NULL;
1054
1055 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1056 if (!utf16_path)
1057 return -ENOMEM;
1058
1059 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1060 FILE_READ_EA,
1061 FILE_FULL_EA_INFORMATION,
1062 SMB2_O_INFO_FILE,
1063 CIFSMaxBufSize -
1064 MAX_SMB2_CREATE_RESPONSE_SIZE -
1065 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1066 &rsp_iov, &buftype, cifs_sb);
1067 if (rc) {
1068 /*
1069 * If ea_name is NULL (listxattr) and there are no EAs,
1070 * return 0 as it's not an error. Otherwise, the specified
1071 * ea_name was not found.
1072 */
1073 if (!ea_name && rc == -ENODATA)
1074 rc = 0;
1075 goto qeas_exit;
1076 }
1077
1078 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1079 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1080 le32_to_cpu(rsp->OutputBufferLength),
1081 &rsp_iov,
1082 sizeof(struct smb2_file_full_ea_info));
1083 if (rc)
1084 goto qeas_exit;
1085
1086 info = (struct smb2_file_full_ea_info *)(
1087 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1088 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1089 le32_to_cpu(rsp->OutputBufferLength), ea_name);
1090
1091 qeas_exit:
1092 kfree(utf16_path);
1093 free_rsp_buf(buftype, rsp_iov.iov_base);
1094 return rc;
1095 }
1096
1097
1098 static int
1099 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1100 const char *path, const char *ea_name, const void *ea_value,
1101 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1102 struct cifs_sb_info *cifs_sb)
1103 {
1104 struct cifs_ses *ses = tcon->ses;
1105 __le16 *utf16_path = NULL;
1106 int ea_name_len = strlen(ea_name);
1107 int flags = 0;
1108 int len;
1109 struct smb_rqst rqst[3];
1110 int resp_buftype[3];
1111 struct kvec rsp_iov[3];
1112 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1113 struct cifs_open_parms oparms;
1114 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1115 struct cifs_fid fid;
1116 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1117 unsigned int size[1];
1118 void *data[1];
1119 struct smb2_file_full_ea_info *ea = NULL;
1120 struct kvec close_iov[1];
1121 struct smb2_query_info_rsp *rsp;
1122 int rc, used_len = 0;
1123
1124 if (smb3_encryption_required(tcon))
1125 flags |= CIFS_TRANSFORM_REQ;
1126
1127 if (ea_name_len > 255)
1128 return -EINVAL;
1129
1130 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1131 if (!utf16_path)
1132 return -ENOMEM;
1133
1134 memset(rqst, 0, sizeof(rqst));
1135 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1136 memset(rsp_iov, 0, sizeof(rsp_iov));
1137
1138 if (ses->server->ops->query_all_EAs) {
1139 if (!ea_value) {
1140 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1141 ea_name, NULL, 0,
1142 cifs_sb);
1143 if (rc == -ENODATA)
1144 goto sea_exit;
1145 } else {
1146 /* If we are adding a attribute we should first check
1147 * if there will be enough space available to store
1148 * the new EA. If not we should not add it since we
1149 * would not be able to even read the EAs back.
1150 */
1151 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1152 FILE_READ_EA,
1153 FILE_FULL_EA_INFORMATION,
1154 SMB2_O_INFO_FILE,
1155 CIFSMaxBufSize -
1156 MAX_SMB2_CREATE_RESPONSE_SIZE -
1157 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1158 &rsp_iov[1], &resp_buftype[1], cifs_sb);
1159 if (rc == 0) {
1160 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1161 used_len = le32_to_cpu(rsp->OutputBufferLength);
1162 }
1163 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1164 resp_buftype[1] = CIFS_NO_BUFFER;
1165 memset(&rsp_iov[1], 0, sizeof(rsp_iov[1]));
1166 rc = 0;
1167
1168 /* Use a fudge factor of 256 bytes in case we collide
1169 * with a different set_EAs command.
1170 */
1171 if(CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1172 MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
1173 used_len + ea_name_len + ea_value_len + 1) {
1174 rc = -ENOSPC;
1175 goto sea_exit;
1176 }
1177 }
1178 }
1179
1180 /* Open */
1181 memset(&open_iov, 0, sizeof(open_iov));
1182 rqst[0].rq_iov = open_iov;
1183 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1184
1185 memset(&oparms, 0, sizeof(oparms));
1186 oparms.tcon = tcon;
1187 oparms.desired_access = FILE_WRITE_EA;
1188 oparms.disposition = FILE_OPEN;
1189 oparms.create_options = cifs_create_options(cifs_sb, 0);
1190 oparms.fid = &fid;
1191 oparms.reconnect = false;
1192
1193 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
1194 if (rc)
1195 goto sea_exit;
1196 smb2_set_next_command(tcon, &rqst[0]);
1197
1198
1199 /* Set Info */
1200 memset(&si_iov, 0, sizeof(si_iov));
1201 rqst[1].rq_iov = si_iov;
1202 rqst[1].rq_nvec = 1;
1203
1204 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
1205 ea = kzalloc(len, GFP_KERNEL);
1206 if (ea == NULL) {
1207 rc = -ENOMEM;
1208 goto sea_exit;
1209 }
1210
1211 ea->ea_name_length = ea_name_len;
1212 ea->ea_value_length = cpu_to_le16(ea_value_len);
1213 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1214 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1215
1216 size[0] = len;
1217 data[0] = ea;
1218
1219 rc = SMB2_set_info_init(tcon, &rqst[1], COMPOUND_FID,
1220 COMPOUND_FID, current->tgid,
1221 FILE_FULL_EA_INFORMATION,
1222 SMB2_O_INFO_FILE, 0, data, size);
1223 smb2_set_next_command(tcon, &rqst[1]);
1224 smb2_set_related(&rqst[1]);
1225
1226
1227 /* Close */
1228 memset(&close_iov, 0, sizeof(close_iov));
1229 rqst[2].rq_iov = close_iov;
1230 rqst[2].rq_nvec = 1;
1231 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1232 smb2_set_related(&rqst[2]);
1233
1234 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1235 resp_buftype, rsp_iov);
1236 /* no need to bump num_remote_opens because handle immediately closed */
1237
1238 sea_exit:
1239 kfree(ea);
1240 kfree(utf16_path);
1241 SMB2_open_free(&rqst[0]);
1242 SMB2_set_info_free(&rqst[1]);
1243 SMB2_close_free(&rqst[2]);
1244 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1245 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1246 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1247 return rc;
1248 }
1249 #endif
1250
1251 static bool
1252 smb2_can_echo(struct TCP_Server_Info *server)
1253 {
1254 return server->echoes;
1255 }
1256
1257 static void
1258 smb2_clear_stats(struct cifs_tcon *tcon)
1259 {
1260 int i;
1261
1262 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1263 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1264 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1265 }
1266 }
1267
1268 static void
1269 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1270 {
1271 seq_puts(m, "\n\tShare Capabilities:");
1272 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1273 seq_puts(m, " DFS,");
1274 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1275 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1276 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1277 seq_puts(m, " SCALEOUT,");
1278 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1279 seq_puts(m, " CLUSTER,");
1280 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1281 seq_puts(m, " ASYMMETRIC,");
1282 if (tcon->capabilities == 0)
1283 seq_puts(m, " None");
1284 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1285 seq_puts(m, " Aligned,");
1286 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1287 seq_puts(m, " Partition Aligned,");
1288 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1289 seq_puts(m, " SSD,");
1290 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1291 seq_puts(m, " TRIM-support,");
1292
1293 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1294 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1295 if (tcon->perf_sector_size)
1296 seq_printf(m, "\tOptimal sector size: 0x%x",
1297 tcon->perf_sector_size);
1298 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1299 }
1300
1301 static void
1302 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1303 {
1304 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1305 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1306
1307 /*
1308 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1309 * totals (requests sent) since those SMBs are per-session not per tcon
1310 */
1311 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1312 (long long)(tcon->bytes_read),
1313 (long long)(tcon->bytes_written));
1314 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1315 atomic_read(&tcon->num_local_opens),
1316 atomic_read(&tcon->num_remote_opens));
1317 seq_printf(m, "\nTreeConnects: %d total %d failed",
1318 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1319 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1320 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1321 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1322 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1323 seq_printf(m, "\nCreates: %d total %d failed",
1324 atomic_read(&sent[SMB2_CREATE_HE]),
1325 atomic_read(&failed[SMB2_CREATE_HE]));
1326 seq_printf(m, "\nCloses: %d total %d failed",
1327 atomic_read(&sent[SMB2_CLOSE_HE]),
1328 atomic_read(&failed[SMB2_CLOSE_HE]));
1329 seq_printf(m, "\nFlushes: %d total %d failed",
1330 atomic_read(&sent[SMB2_FLUSH_HE]),
1331 atomic_read(&failed[SMB2_FLUSH_HE]));
1332 seq_printf(m, "\nReads: %d total %d failed",
1333 atomic_read(&sent[SMB2_READ_HE]),
1334 atomic_read(&failed[SMB2_READ_HE]));
1335 seq_printf(m, "\nWrites: %d total %d failed",
1336 atomic_read(&sent[SMB2_WRITE_HE]),
1337 atomic_read(&failed[SMB2_WRITE_HE]));
1338 seq_printf(m, "\nLocks: %d total %d failed",
1339 atomic_read(&sent[SMB2_LOCK_HE]),
1340 atomic_read(&failed[SMB2_LOCK_HE]));
1341 seq_printf(m, "\nIOCTLs: %d total %d failed",
1342 atomic_read(&sent[SMB2_IOCTL_HE]),
1343 atomic_read(&failed[SMB2_IOCTL_HE]));
1344 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1345 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1346 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1347 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1348 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1349 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1350 seq_printf(m, "\nQueryInfos: %d total %d failed",
1351 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1352 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1353 seq_printf(m, "\nSetInfos: %d total %d failed",
1354 atomic_read(&sent[SMB2_SET_INFO_HE]),
1355 atomic_read(&failed[SMB2_SET_INFO_HE]));
1356 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1357 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1358 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1359 }
1360
1361 static void
1362 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1363 {
1364 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1365 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1366
1367 cfile->fid.persistent_fid = fid->persistent_fid;
1368 cfile->fid.volatile_fid = fid->volatile_fid;
1369 cfile->fid.access = fid->access;
1370 #ifdef CONFIG_CIFS_DEBUG2
1371 cfile->fid.mid = fid->mid;
1372 #endif /* CIFS_DEBUG2 */
1373 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1374 &fid->purge_cache);
1375 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1376 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1377 }
1378
1379 static void
1380 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1381 struct cifs_fid *fid)
1382 {
1383 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1384 }
1385
1386 static void
1387 smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
1388 struct cifsFileInfo *cfile)
1389 {
1390 struct smb2_file_network_open_info file_inf;
1391 struct inode *inode;
1392 int rc;
1393
1394 rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
1395 cfile->fid.volatile_fid, &file_inf);
1396 if (rc)
1397 return;
1398
1399 inode = d_inode(cfile->dentry);
1400
1401 spin_lock(&inode->i_lock);
1402 CIFS_I(inode)->time = jiffies;
1403
1404 /* Creation time should not need to be updated on close */
1405 if (file_inf.LastWriteTime)
1406 inode->i_mtime = cifs_NTtimeToUnix(file_inf.LastWriteTime);
1407 if (file_inf.ChangeTime)
1408 inode->i_ctime = cifs_NTtimeToUnix(file_inf.ChangeTime);
1409 if (file_inf.LastAccessTime)
1410 inode->i_atime = cifs_NTtimeToUnix(file_inf.LastAccessTime);
1411
1412 /*
1413 * i_blocks is not related to (i_size / i_blksize),
1414 * but instead 512 byte (2**9) size is required for
1415 * calculating num blocks.
1416 */
1417 if (le64_to_cpu(file_inf.AllocationSize) > 4096)
1418 inode->i_blocks =
1419 (512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
1420
1421 /* End of file and Attributes should not have to be updated on close */
1422 spin_unlock(&inode->i_lock);
1423 }
1424
1425 static int
1426 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1427 u64 persistent_fid, u64 volatile_fid,
1428 struct copychunk_ioctl *pcchunk)
1429 {
1430 int rc;
1431 unsigned int ret_data_len;
1432 struct resume_key_req *res_key;
1433
1434 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1435 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1436 NULL, 0 /* no input */, CIFSMaxBufSize,
1437 (char **)&res_key, &ret_data_len);
1438
1439 if (rc) {
1440 cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1441 goto req_res_key_exit;
1442 }
1443 if (ret_data_len < sizeof(struct resume_key_req)) {
1444 cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
1445 rc = -EINVAL;
1446 goto req_res_key_exit;
1447 }
1448 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1449
1450 req_res_key_exit:
1451 kfree(res_key);
1452 return rc;
1453 }
1454
1455 struct iqi_vars {
1456 struct smb_rqst rqst[3];
1457 struct kvec rsp_iov[3];
1458 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1459 struct kvec qi_iov[1];
1460 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1461 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1462 struct kvec close_iov[1];
1463 };
1464
1465 static int
1466 smb2_ioctl_query_info(const unsigned int xid,
1467 struct cifs_tcon *tcon,
1468 struct cifs_sb_info *cifs_sb,
1469 __le16 *path, int is_dir,
1470 unsigned long p)
1471 {
1472 struct iqi_vars *vars;
1473 struct smb_rqst *rqst;
1474 struct kvec *rsp_iov;
1475 struct cifs_ses *ses = tcon->ses;
1476 char __user *arg = (char __user *)p;
1477 struct smb_query_info qi;
1478 struct smb_query_info __user *pqi;
1479 int rc = 0;
1480 int flags = 0;
1481 struct smb2_query_info_rsp *qi_rsp = NULL;
1482 struct smb2_ioctl_rsp *io_rsp = NULL;
1483 void *buffer = NULL;
1484 int resp_buftype[3];
1485 struct cifs_open_parms oparms;
1486 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1487 struct cifs_fid fid;
1488 unsigned int size[2];
1489 void *data[2];
1490 int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
1491
1492 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
1493 if (vars == NULL)
1494 return -ENOMEM;
1495 rqst = &vars->rqst[0];
1496 rsp_iov = &vars->rsp_iov[0];
1497
1498 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1499
1500 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1501 goto e_fault;
1502
1503 if (qi.output_buffer_length > 1024) {
1504 kfree(vars);
1505 return -EINVAL;
1506 }
1507
1508 if (!ses || !(ses->server)) {
1509 kfree(vars);
1510 return -EIO;
1511 }
1512
1513 if (smb3_encryption_required(tcon))
1514 flags |= CIFS_TRANSFORM_REQ;
1515
1516 buffer = memdup_user(arg + sizeof(struct smb_query_info),
1517 qi.output_buffer_length);
1518 if (IS_ERR(buffer)) {
1519 kfree(vars);
1520 return PTR_ERR(buffer);
1521 }
1522
1523 /* Open */
1524 rqst[0].rq_iov = &vars->open_iov[0];
1525 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1526
1527 memset(&oparms, 0, sizeof(oparms));
1528 oparms.tcon = tcon;
1529 oparms.disposition = FILE_OPEN;
1530 oparms.create_options = cifs_create_options(cifs_sb, create_options);
1531 oparms.fid = &fid;
1532 oparms.reconnect = false;
1533
1534 if (qi.flags & PASSTHRU_FSCTL) {
1535 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1536 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1537 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1538 break;
1539 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1540 oparms.desired_access = GENERIC_ALL;
1541 break;
1542 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1543 oparms.desired_access = GENERIC_READ;
1544 break;
1545 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1546 oparms.desired_access = GENERIC_WRITE;
1547 break;
1548 }
1549 } else if (qi.flags & PASSTHRU_SET_INFO) {
1550 oparms.desired_access = GENERIC_WRITE;
1551 } else {
1552 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1553 }
1554
1555 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, path);
1556 if (rc)
1557 goto iqinf_exit;
1558 smb2_set_next_command(tcon, &rqst[0]);
1559
1560 /* Query */
1561 if (qi.flags & PASSTHRU_FSCTL) {
1562 /* Can eventually relax perm check since server enforces too */
1563 if (!capable(CAP_SYS_ADMIN))
1564 rc = -EPERM;
1565 else {
1566 rqst[1].rq_iov = &vars->io_iov[0];
1567 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1568
1569 rc = SMB2_ioctl_init(tcon, &rqst[1],
1570 COMPOUND_FID, COMPOUND_FID,
1571 qi.info_type, true, buffer,
1572 qi.output_buffer_length,
1573 CIFSMaxBufSize -
1574 MAX_SMB2_CREATE_RESPONSE_SIZE -
1575 MAX_SMB2_CLOSE_RESPONSE_SIZE);
1576 }
1577 } else if (qi.flags == PASSTHRU_SET_INFO) {
1578 /* Can eventually relax perm check since server enforces too */
1579 if (!capable(CAP_SYS_ADMIN))
1580 rc = -EPERM;
1581 else {
1582 rqst[1].rq_iov = &vars->si_iov[0];
1583 rqst[1].rq_nvec = 1;
1584
1585 size[0] = 8;
1586 data[0] = buffer;
1587
1588 rc = SMB2_set_info_init(tcon, &rqst[1],
1589 COMPOUND_FID, COMPOUND_FID,
1590 current->tgid,
1591 FILE_END_OF_FILE_INFORMATION,
1592 SMB2_O_INFO_FILE, 0, data, size);
1593 }
1594 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1595 rqst[1].rq_iov = &vars->qi_iov[0];
1596 rqst[1].rq_nvec = 1;
1597
1598 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
1599 COMPOUND_FID, qi.file_info_class,
1600 qi.info_type, qi.additional_information,
1601 qi.input_buffer_length,
1602 qi.output_buffer_length, buffer);
1603 } else { /* unknown flags */
1604 cifs_tcon_dbg(VFS, "Invalid passthru query flags: 0x%x\n",
1605 qi.flags);
1606 rc = -EINVAL;
1607 }
1608
1609 if (rc)
1610 goto iqinf_exit;
1611 smb2_set_next_command(tcon, &rqst[1]);
1612 smb2_set_related(&rqst[1]);
1613
1614 /* Close */
1615 rqst[2].rq_iov = &vars->close_iov[0];
1616 rqst[2].rq_nvec = 1;
1617
1618 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1619 if (rc)
1620 goto iqinf_exit;
1621 smb2_set_related(&rqst[2]);
1622
1623 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1624 resp_buftype, rsp_iov);
1625 if (rc)
1626 goto iqinf_exit;
1627
1628 /* No need to bump num_remote_opens since handle immediately closed */
1629 if (qi.flags & PASSTHRU_FSCTL) {
1630 pqi = (struct smb_query_info __user *)arg;
1631 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1632 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1633 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1634 if (qi.input_buffer_length > 0 &&
1635 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1636 > rsp_iov[1].iov_len)
1637 goto e_fault;
1638
1639 if (copy_to_user(&pqi->input_buffer_length,
1640 &qi.input_buffer_length,
1641 sizeof(qi.input_buffer_length)))
1642 goto e_fault;
1643
1644 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1645 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1646 qi.input_buffer_length))
1647 goto e_fault;
1648 } else {
1649 pqi = (struct smb_query_info __user *)arg;
1650 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1651 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1652 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1653 if (copy_to_user(&pqi->input_buffer_length,
1654 &qi.input_buffer_length,
1655 sizeof(qi.input_buffer_length)))
1656 goto e_fault;
1657
1658 if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1659 qi.input_buffer_length))
1660 goto e_fault;
1661 }
1662
1663 iqinf_exit:
1664 kfree(vars);
1665 kfree(buffer);
1666 SMB2_open_free(&rqst[0]);
1667 if (qi.flags & PASSTHRU_FSCTL)
1668 SMB2_ioctl_free(&rqst[1]);
1669 else
1670 SMB2_query_info_free(&rqst[1]);
1671
1672 SMB2_close_free(&rqst[2]);
1673 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1674 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1675 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1676 return rc;
1677
1678 e_fault:
1679 rc = -EFAULT;
1680 goto iqinf_exit;
1681 }
1682
1683 static ssize_t
1684 smb2_copychunk_range(const unsigned int xid,
1685 struct cifsFileInfo *srcfile,
1686 struct cifsFileInfo *trgtfile, u64 src_off,
1687 u64 len, u64 dest_off)
1688 {
1689 int rc;
1690 unsigned int ret_data_len;
1691 struct copychunk_ioctl *pcchunk;
1692 struct copychunk_ioctl_rsp *retbuf = NULL;
1693 struct cifs_tcon *tcon;
1694 int chunks_copied = 0;
1695 bool chunk_sizes_updated = false;
1696 ssize_t bytes_written, total_bytes_written = 0;
1697
1698 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1699
1700 if (pcchunk == NULL)
1701 return -ENOMEM;
1702
1703 cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
1704 /* Request a key from the server to identify the source of the copy */
1705 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1706 srcfile->fid.persistent_fid,
1707 srcfile->fid.volatile_fid, pcchunk);
1708
1709 /* Note: request_res_key sets res_key null only if rc !=0 */
1710 if (rc)
1711 goto cchunk_out;
1712
1713 /* For now array only one chunk long, will make more flexible later */
1714 pcchunk->ChunkCount = cpu_to_le32(1);
1715 pcchunk->Reserved = 0;
1716 pcchunk->Reserved2 = 0;
1717
1718 tcon = tlink_tcon(trgtfile->tlink);
1719
1720 while (len > 0) {
1721 pcchunk->SourceOffset = cpu_to_le64(src_off);
1722 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1723 pcchunk->Length =
1724 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1725
1726 /* Request server copy to target from src identified by key */
1727 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1728 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1729 true /* is_fsctl */, (char *)pcchunk,
1730 sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1731 (char **)&retbuf, &ret_data_len);
1732 if (rc == 0) {
1733 if (ret_data_len !=
1734 sizeof(struct copychunk_ioctl_rsp)) {
1735 cifs_tcon_dbg(VFS, "Invalid cchunk response size\n");
1736 rc = -EIO;
1737 goto cchunk_out;
1738 }
1739 if (retbuf->TotalBytesWritten == 0) {
1740 cifs_dbg(FYI, "no bytes copied\n");
1741 rc = -EIO;
1742 goto cchunk_out;
1743 }
1744 /*
1745 * Check if server claimed to write more than we asked
1746 */
1747 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1748 le32_to_cpu(pcchunk->Length)) {
1749 cifs_tcon_dbg(VFS, "Invalid copy chunk response\n");
1750 rc = -EIO;
1751 goto cchunk_out;
1752 }
1753 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1754 cifs_tcon_dbg(VFS, "Invalid num chunks written\n");
1755 rc = -EIO;
1756 goto cchunk_out;
1757 }
1758 chunks_copied++;
1759
1760 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1761 src_off += bytes_written;
1762 dest_off += bytes_written;
1763 len -= bytes_written;
1764 total_bytes_written += bytes_written;
1765
1766 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1767 le32_to_cpu(retbuf->ChunksWritten),
1768 le32_to_cpu(retbuf->ChunkBytesWritten),
1769 bytes_written);
1770 } else if (rc == -EINVAL) {
1771 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1772 goto cchunk_out;
1773
1774 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1775 le32_to_cpu(retbuf->ChunksWritten),
1776 le32_to_cpu(retbuf->ChunkBytesWritten),
1777 le32_to_cpu(retbuf->TotalBytesWritten));
1778
1779 /*
1780 * Check if this is the first request using these sizes,
1781 * (ie check if copy succeed once with original sizes
1782 * and check if the server gave us different sizes after
1783 * we already updated max sizes on previous request).
1784 * if not then why is the server returning an error now
1785 */
1786 if ((chunks_copied != 0) || chunk_sizes_updated)
1787 goto cchunk_out;
1788
1789 /* Check that server is not asking us to grow size */
1790 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1791 tcon->max_bytes_chunk)
1792 tcon->max_bytes_chunk =
1793 le32_to_cpu(retbuf->ChunkBytesWritten);
1794 else
1795 goto cchunk_out; /* server gave us bogus size */
1796
1797 /* No need to change MaxChunks since already set to 1 */
1798 chunk_sizes_updated = true;
1799 } else
1800 goto cchunk_out;
1801 }
1802
1803 cchunk_out:
1804 kfree(pcchunk);
1805 kfree(retbuf);
1806 if (rc)
1807 return rc;
1808 else
1809 return total_bytes_written;
1810 }
1811
1812 static int
1813 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1814 struct cifs_fid *fid)
1815 {
1816 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1817 }
1818
1819 static unsigned int
1820 smb2_read_data_offset(char *buf)
1821 {
1822 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1823
1824 return rsp->DataOffset;
1825 }
1826
1827 static unsigned int
1828 smb2_read_data_length(char *buf, bool in_remaining)
1829 {
1830 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1831
1832 if (in_remaining)
1833 return le32_to_cpu(rsp->DataRemaining);
1834
1835 return le32_to_cpu(rsp->DataLength);
1836 }
1837
1838
1839 static int
1840 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1841 struct cifs_io_parms *parms, unsigned int *bytes_read,
1842 char **buf, int *buf_type)
1843 {
1844 parms->persistent_fid = pfid->persistent_fid;
1845 parms->volatile_fid = pfid->volatile_fid;
1846 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1847 }
1848
1849 static int
1850 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1851 struct cifs_io_parms *parms, unsigned int *written,
1852 struct kvec *iov, unsigned long nr_segs)
1853 {
1854
1855 parms->persistent_fid = pfid->persistent_fid;
1856 parms->volatile_fid = pfid->volatile_fid;
1857 return SMB2_write(xid, parms, written, iov, nr_segs);
1858 }
1859
1860 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1861 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1862 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1863 {
1864 struct cifsInodeInfo *cifsi;
1865 int rc;
1866
1867 cifsi = CIFS_I(inode);
1868
1869 /* if file already sparse don't bother setting sparse again */
1870 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1871 return true; /* already sparse */
1872
1873 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1874 return true; /* already not sparse */
1875
1876 /*
1877 * Can't check for sparse support on share the usual way via the
1878 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1879 * since Samba server doesn't set the flag on the share, yet
1880 * supports the set sparse FSCTL and returns sparse correctly
1881 * in the file attributes. If we fail setting sparse though we
1882 * mark that server does not support sparse files for this share
1883 * to avoid repeatedly sending the unsupported fsctl to server
1884 * if the file is repeatedly extended.
1885 */
1886 if (tcon->broken_sparse_sup)
1887 return false;
1888
1889 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1890 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1891 true /* is_fctl */,
1892 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
1893 if (rc) {
1894 tcon->broken_sparse_sup = true;
1895 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1896 return false;
1897 }
1898
1899 if (setsparse)
1900 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1901 else
1902 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1903
1904 return true;
1905 }
1906
1907 static int
1908 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1909 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1910 {
1911 __le64 eof = cpu_to_le64(size);
1912 struct inode *inode;
1913
1914 /*
1915 * If extending file more than one page make sparse. Many Linux fs
1916 * make files sparse by default when extending via ftruncate
1917 */
1918 inode = d_inode(cfile->dentry);
1919
1920 if (!set_alloc && (size > inode->i_size + 8192)) {
1921 __u8 set_sparse = 1;
1922
1923 /* whether set sparse succeeds or not, extend the file */
1924 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1925 }
1926
1927 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1928 cfile->fid.volatile_fid, cfile->pid, &eof);
1929 }
1930
1931 static int
1932 smb2_duplicate_extents(const unsigned int xid,
1933 struct cifsFileInfo *srcfile,
1934 struct cifsFileInfo *trgtfile, u64 src_off,
1935 u64 len, u64 dest_off)
1936 {
1937 int rc;
1938 unsigned int ret_data_len;
1939 struct duplicate_extents_to_file dup_ext_buf;
1940 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1941
1942 /* server fileays advertise duplicate extent support with this flag */
1943 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1944 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1945 return -EOPNOTSUPP;
1946
1947 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1948 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1949 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1950 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1951 dup_ext_buf.ByteCount = cpu_to_le64(len);
1952 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
1953 src_off, dest_off, len);
1954
1955 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1956 if (rc)
1957 goto duplicate_extents_out;
1958
1959 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1960 trgtfile->fid.volatile_fid,
1961 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
1962 true /* is_fsctl */,
1963 (char *)&dup_ext_buf,
1964 sizeof(struct duplicate_extents_to_file),
1965 CIFSMaxBufSize, NULL,
1966 &ret_data_len);
1967
1968 if (ret_data_len > 0)
1969 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
1970
1971 duplicate_extents_out:
1972 return rc;
1973 }
1974
1975 static int
1976 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1977 struct cifsFileInfo *cfile)
1978 {
1979 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1980 cfile->fid.volatile_fid);
1981 }
1982
1983 static int
1984 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1985 struct cifsFileInfo *cfile)
1986 {
1987 struct fsctl_set_integrity_information_req integr_info;
1988 unsigned int ret_data_len;
1989
1990 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1991 integr_info.Flags = 0;
1992 integr_info.Reserved = 0;
1993
1994 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1995 cfile->fid.volatile_fid,
1996 FSCTL_SET_INTEGRITY_INFORMATION,
1997 true /* is_fsctl */,
1998 (char *)&integr_info,
1999 sizeof(struct fsctl_set_integrity_information_req),
2000 CIFSMaxBufSize, NULL,
2001 &ret_data_len);
2002
2003 }
2004
2005 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
2006 #define GMT_TOKEN_SIZE 50
2007
2008 #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
2009
2010 /*
2011 * Input buffer contains (empty) struct smb_snapshot array with size filled in
2012 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
2013 */
2014 static int
2015 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
2016 struct cifsFileInfo *cfile, void __user *ioc_buf)
2017 {
2018 char *retbuf = NULL;
2019 unsigned int ret_data_len = 0;
2020 int rc;
2021 u32 max_response_size;
2022 struct smb_snapshot_array snapshot_in;
2023
2024 /*
2025 * On the first query to enumerate the list of snapshots available
2026 * for this volume the buffer begins with 0 (number of snapshots
2027 * which can be returned is zero since at that point we do not know
2028 * how big the buffer needs to be). On the second query,
2029 * it (ret_data_len) is set to number of snapshots so we can
2030 * know to set the maximum response size larger (see below).
2031 */
2032 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
2033 return -EFAULT;
2034
2035 /*
2036 * Note that for snapshot queries that servers like Azure expect that
2037 * the first query be minimal size (and just used to get the number/size
2038 * of previous versions) so response size must be specified as EXACTLY
2039 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2040 * of eight bytes.
2041 */
2042 if (ret_data_len == 0)
2043 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
2044 else
2045 max_response_size = CIFSMaxBufSize;
2046
2047 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2048 cfile->fid.volatile_fid,
2049 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
2050 true /* is_fsctl */,
2051 NULL, 0 /* no input data */, max_response_size,
2052 (char **)&retbuf,
2053 &ret_data_len);
2054 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2055 rc, ret_data_len);
2056 if (rc)
2057 return rc;
2058
2059 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2060 /* Fixup buffer */
2061 if (copy_from_user(&snapshot_in, ioc_buf,
2062 sizeof(struct smb_snapshot_array))) {
2063 rc = -EFAULT;
2064 kfree(retbuf);
2065 return rc;
2066 }
2067
2068 /*
2069 * Check for min size, ie not large enough to fit even one GMT
2070 * token (snapshot). On the first ioctl some users may pass in
2071 * smaller size (or zero) to simply get the size of the array
2072 * so the user space caller can allocate sufficient memory
2073 * and retry the ioctl again with larger array size sufficient
2074 * to hold all of the snapshot GMT tokens on the second try.
2075 */
2076 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2077 ret_data_len = sizeof(struct smb_snapshot_array);
2078
2079 /*
2080 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2081 * the snapshot array (of 50 byte GMT tokens) each
2082 * representing an available previous version of the data
2083 */
2084 if (ret_data_len > (snapshot_in.snapshot_array_size +
2085 sizeof(struct smb_snapshot_array)))
2086 ret_data_len = snapshot_in.snapshot_array_size +
2087 sizeof(struct smb_snapshot_array);
2088
2089 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2090 rc = -EFAULT;
2091 }
2092
2093 kfree(retbuf);
2094 return rc;
2095 }
2096
2097
2098
2099 static int
2100 smb3_notify(const unsigned int xid, struct file *pfile,
2101 void __user *ioc_buf)
2102 {
2103 struct smb3_notify notify;
2104 struct dentry *dentry = pfile->f_path.dentry;
2105 struct inode *inode = file_inode(pfile);
2106 struct cifs_sb_info *cifs_sb;
2107 struct cifs_open_parms oparms;
2108 struct cifs_fid fid;
2109 struct cifs_tcon *tcon;
2110 unsigned char *path = NULL;
2111 __le16 *utf16_path = NULL;
2112 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2113 int rc = 0;
2114
2115 path = build_path_from_dentry(dentry);
2116 if (path == NULL)
2117 return -ENOMEM;
2118
2119 cifs_sb = CIFS_SB(inode->i_sb);
2120
2121 utf16_path = cifs_convert_path_to_utf16(path + 1, cifs_sb);
2122 if (utf16_path == NULL) {
2123 rc = -ENOMEM;
2124 goto notify_exit;
2125 }
2126
2127 if (copy_from_user(&notify, ioc_buf, sizeof(struct smb3_notify))) {
2128 rc = -EFAULT;
2129 goto notify_exit;
2130 }
2131
2132 tcon = cifs_sb_master_tcon(cifs_sb);
2133 oparms.tcon = tcon;
2134 oparms.desired_access = FILE_READ_ATTRIBUTES;
2135 oparms.disposition = FILE_OPEN;
2136 oparms.create_options = cifs_create_options(cifs_sb, 0);
2137 oparms.fid = &fid;
2138 oparms.reconnect = false;
2139
2140 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
2141 NULL);
2142 if (rc)
2143 goto notify_exit;
2144
2145 rc = SMB2_change_notify(xid, tcon, fid.persistent_fid, fid.volatile_fid,
2146 notify.watch_tree, notify.completion_filter);
2147
2148 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2149
2150 cifs_dbg(FYI, "change notify for path %s rc %d\n", path, rc);
2151
2152 notify_exit:
2153 kfree(path);
2154 kfree(utf16_path);
2155 return rc;
2156 }
2157
2158 static int
2159 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2160 const char *path, struct cifs_sb_info *cifs_sb,
2161 struct cifs_fid *fid, __u16 search_flags,
2162 struct cifs_search_info *srch_inf)
2163 {
2164 __le16 *utf16_path;
2165 struct smb_rqst rqst[2];
2166 struct kvec rsp_iov[2];
2167 int resp_buftype[2];
2168 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2169 struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
2170 int rc, flags = 0;
2171 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2172 struct cifs_open_parms oparms;
2173 struct smb2_query_directory_rsp *qd_rsp = NULL;
2174 struct smb2_create_rsp *op_rsp = NULL;
2175
2176 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2177 if (!utf16_path)
2178 return -ENOMEM;
2179
2180 if (smb3_encryption_required(tcon))
2181 flags |= CIFS_TRANSFORM_REQ;
2182
2183 memset(rqst, 0, sizeof(rqst));
2184 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2185 memset(rsp_iov, 0, sizeof(rsp_iov));
2186
2187 /* Open */
2188 memset(&open_iov, 0, sizeof(open_iov));
2189 rqst[0].rq_iov = open_iov;
2190 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2191
2192 oparms.tcon = tcon;
2193 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2194 oparms.disposition = FILE_OPEN;
2195 oparms.create_options = cifs_create_options(cifs_sb, 0);
2196 oparms.fid = fid;
2197 oparms.reconnect = false;
2198
2199 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2200 if (rc)
2201 goto qdf_free;
2202 smb2_set_next_command(tcon, &rqst[0]);
2203
2204 /* Query directory */
2205 srch_inf->entries_in_buffer = 0;
2206 srch_inf->index_of_last_entry = 2;
2207
2208 memset(&qd_iov, 0, sizeof(qd_iov));
2209 rqst[1].rq_iov = qd_iov;
2210 rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
2211
2212 rc = SMB2_query_directory_init(xid, tcon, &rqst[1],
2213 COMPOUND_FID, COMPOUND_FID,
2214 0, srch_inf->info_level);
2215 if (rc)
2216 goto qdf_free;
2217
2218 smb2_set_related(&rqst[1]);
2219
2220 rc = compound_send_recv(xid, tcon->ses, flags, 2, rqst,
2221 resp_buftype, rsp_iov);
2222
2223 /* If the open failed there is nothing to do */
2224 op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
2225 if (op_rsp == NULL || op_rsp->sync_hdr.Status != STATUS_SUCCESS) {
2226 cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
2227 goto qdf_free;
2228 }
2229 fid->persistent_fid = op_rsp->PersistentFileId;
2230 fid->volatile_fid = op_rsp->VolatileFileId;
2231
2232 /* Anything else than ENODATA means a genuine error */
2233 if (rc && rc != -ENODATA) {
2234 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2235 cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
2236 trace_smb3_query_dir_err(xid, fid->persistent_fid,
2237 tcon->tid, tcon->ses->Suid, 0, 0, rc);
2238 goto qdf_free;
2239 }
2240
2241 atomic_inc(&tcon->num_remote_opens);
2242
2243 qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
2244 if (qd_rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
2245 trace_smb3_query_dir_done(xid, fid->persistent_fid,
2246 tcon->tid, tcon->ses->Suid, 0, 0);
2247 srch_inf->endOfSearch = true;
2248 rc = 0;
2249 goto qdf_free;
2250 }
2251
2252 rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
2253 srch_inf);
2254 if (rc) {
2255 trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
2256 tcon->ses->Suid, 0, 0, rc);
2257 goto qdf_free;
2258 }
2259 resp_buftype[1] = CIFS_NO_BUFFER;
2260
2261 trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
2262 tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
2263
2264 qdf_free:
2265 kfree(utf16_path);
2266 SMB2_open_free(&rqst[0]);
2267 SMB2_query_directory_free(&rqst[1]);
2268 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2269 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2270 return rc;
2271 }
2272
2273 static int
2274 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2275 struct cifs_fid *fid, __u16 search_flags,
2276 struct cifs_search_info *srch_inf)
2277 {
2278 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2279 fid->volatile_fid, 0, srch_inf);
2280 }
2281
2282 static int
2283 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2284 struct cifs_fid *fid)
2285 {
2286 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2287 }
2288
2289 /*
2290 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2291 * the number of credits and return true. Otherwise - return false.
2292 */
2293 static bool
2294 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
2295 {
2296 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2297
2298 if (shdr->Status != STATUS_PENDING)
2299 return false;
2300
2301 if (shdr->CreditRequest) {
2302 spin_lock(&server->req_lock);
2303 server->credits += le16_to_cpu(shdr->CreditRequest);
2304 spin_unlock(&server->req_lock);
2305 wake_up(&server->request_q);
2306 }
2307
2308 return true;
2309 }
2310
2311 static bool
2312 smb2_is_session_expired(char *buf)
2313 {
2314 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2315
2316 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2317 shdr->Status != STATUS_USER_SESSION_DELETED)
2318 return false;
2319
2320 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2321 le16_to_cpu(shdr->Command),
2322 le64_to_cpu(shdr->MessageId));
2323 cifs_dbg(FYI, "Session expired or deleted\n");
2324
2325 return true;
2326 }
2327
2328 static int
2329 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2330 struct cifsInodeInfo *cinode)
2331 {
2332 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2333 return SMB2_lease_break(0, tcon, cinode->lease_key,
2334 smb2_get_lease_state(cinode));
2335
2336 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2337 fid->volatile_fid,
2338 CIFS_CACHE_READ(cinode) ? 1 : 0);
2339 }
2340
2341 void
2342 smb2_set_related(struct smb_rqst *rqst)
2343 {
2344 struct smb2_sync_hdr *shdr;
2345
2346 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2347 if (shdr == NULL) {
2348 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2349 return;
2350 }
2351 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2352 }
2353
2354 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2355
2356 void
2357 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2358 {
2359 struct smb2_sync_hdr *shdr;
2360 struct cifs_ses *ses = tcon->ses;
2361 struct TCP_Server_Info *server = ses->server;
2362 unsigned long len = smb_rqst_len(server, rqst);
2363 int i, num_padding;
2364
2365 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2366 if (shdr == NULL) {
2367 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2368 return;
2369 }
2370
2371 /* SMB headers in a compound are 8 byte aligned. */
2372
2373 /* No padding needed */
2374 if (!(len & 7))
2375 goto finished;
2376
2377 num_padding = 8 - (len & 7);
2378 if (!smb3_encryption_required(tcon)) {
2379 /*
2380 * If we do not have encryption then we can just add an extra
2381 * iov for the padding.
2382 */
2383 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2384 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2385 rqst->rq_nvec++;
2386 len += num_padding;
2387 } else {
2388 /*
2389 * We can not add a small padding iov for the encryption case
2390 * because the encryption framework can not handle the padding
2391 * iovs.
2392 * We have to flatten this into a single buffer and add
2393 * the padding to it.
2394 */
2395 for (i = 1; i < rqst->rq_nvec; i++) {
2396 memcpy(rqst->rq_iov[0].iov_base +
2397 rqst->rq_iov[0].iov_len,
2398 rqst->rq_iov[i].iov_base,
2399 rqst->rq_iov[i].iov_len);
2400 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2401 }
2402 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2403 0, num_padding);
2404 rqst->rq_iov[0].iov_len += num_padding;
2405 len += num_padding;
2406 rqst->rq_nvec = 1;
2407 }
2408
2409 finished:
2410 shdr->NextCommand = cpu_to_le32(len);
2411 }
2412
2413 /*
2414 * Passes the query info response back to the caller on success.
2415 * Caller need to free this with free_rsp_buf().
2416 */
2417 int
2418 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2419 __le16 *utf16_path, u32 desired_access,
2420 u32 class, u32 type, u32 output_len,
2421 struct kvec *rsp, int *buftype,
2422 struct cifs_sb_info *cifs_sb)
2423 {
2424 struct cifs_ses *ses = tcon->ses;
2425 int flags = 0;
2426 struct smb_rqst rqst[3];
2427 int resp_buftype[3];
2428 struct kvec rsp_iov[3];
2429 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2430 struct kvec qi_iov[1];
2431 struct kvec close_iov[1];
2432 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2433 struct cifs_open_parms oparms;
2434 struct cifs_fid fid;
2435 int rc;
2436
2437 if (smb3_encryption_required(tcon))
2438 flags |= CIFS_TRANSFORM_REQ;
2439
2440 memset(rqst, 0, sizeof(rqst));
2441 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2442 memset(rsp_iov, 0, sizeof(rsp_iov));
2443
2444 memset(&open_iov, 0, sizeof(open_iov));
2445 rqst[0].rq_iov = open_iov;
2446 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2447
2448 oparms.tcon = tcon;
2449 oparms.desired_access = desired_access;
2450 oparms.disposition = FILE_OPEN;
2451 oparms.create_options = cifs_create_options(cifs_sb, 0);
2452 oparms.fid = &fid;
2453 oparms.reconnect = false;
2454
2455 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2456 if (rc)
2457 goto qic_exit;
2458 smb2_set_next_command(tcon, &rqst[0]);
2459
2460 memset(&qi_iov, 0, sizeof(qi_iov));
2461 rqst[1].rq_iov = qi_iov;
2462 rqst[1].rq_nvec = 1;
2463
2464 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
2465 class, type, 0,
2466 output_len, 0,
2467 NULL);
2468 if (rc)
2469 goto qic_exit;
2470 smb2_set_next_command(tcon, &rqst[1]);
2471 smb2_set_related(&rqst[1]);
2472
2473 memset(&close_iov, 0, sizeof(close_iov));
2474 rqst[2].rq_iov = close_iov;
2475 rqst[2].rq_nvec = 1;
2476
2477 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2478 if (rc)
2479 goto qic_exit;
2480 smb2_set_related(&rqst[2]);
2481
2482 rc = compound_send_recv(xid, ses, flags, 3, rqst,
2483 resp_buftype, rsp_iov);
2484 if (rc) {
2485 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2486 if (rc == -EREMCHG) {
2487 tcon->need_reconnect = true;
2488 pr_warn_once("server share %s deleted\n",
2489 tcon->treeName);
2490 }
2491 goto qic_exit;
2492 }
2493 *rsp = rsp_iov[1];
2494 *buftype = resp_buftype[1];
2495
2496 qic_exit:
2497 SMB2_open_free(&rqst[0]);
2498 SMB2_query_info_free(&rqst[1]);
2499 SMB2_close_free(&rqst[2]);
2500 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2501 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2502 return rc;
2503 }
2504
2505 static int
2506 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2507 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2508 {
2509 struct smb2_query_info_rsp *rsp;
2510 struct smb2_fs_full_size_info *info = NULL;
2511 __le16 utf16_path = 0; /* Null - open root of share */
2512 struct kvec rsp_iov = {NULL, 0};
2513 int buftype = CIFS_NO_BUFFER;
2514 int rc;
2515
2516
2517 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2518 FILE_READ_ATTRIBUTES,
2519 FS_FULL_SIZE_INFORMATION,
2520 SMB2_O_INFO_FILESYSTEM,
2521 sizeof(struct smb2_fs_full_size_info),
2522 &rsp_iov, &buftype, cifs_sb);
2523 if (rc)
2524 goto qfs_exit;
2525
2526 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2527 buf->f_type = SMB2_MAGIC_NUMBER;
2528 info = (struct smb2_fs_full_size_info *)(
2529 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2530 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2531 le32_to_cpu(rsp->OutputBufferLength),
2532 &rsp_iov,
2533 sizeof(struct smb2_fs_full_size_info));
2534 if (!rc)
2535 smb2_copy_fs_info_to_kstatfs(info, buf);
2536
2537 qfs_exit:
2538 free_rsp_buf(buftype, rsp_iov.iov_base);
2539 return rc;
2540 }
2541
2542 static int
2543 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2544 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2545 {
2546 int rc;
2547 __le16 srch_path = 0; /* Null - open root of share */
2548 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2549 struct cifs_open_parms oparms;
2550 struct cifs_fid fid;
2551
2552 if (!tcon->posix_extensions)
2553 return smb2_queryfs(xid, tcon, cifs_sb, buf);
2554
2555 oparms.tcon = tcon;
2556 oparms.desired_access = FILE_READ_ATTRIBUTES;
2557 oparms.disposition = FILE_OPEN;
2558 oparms.create_options = cifs_create_options(cifs_sb, 0);
2559 oparms.fid = &fid;
2560 oparms.reconnect = false;
2561
2562 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
2563 NULL, NULL);
2564 if (rc)
2565 return rc;
2566
2567 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2568 fid.volatile_fid, buf);
2569 buf->f_type = SMB2_MAGIC_NUMBER;
2570 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2571 return rc;
2572 }
2573
2574 static bool
2575 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2576 {
2577 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2578 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2579 }
2580
2581 static int
2582 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2583 __u64 length, __u32 type, int lock, int unlock, bool wait)
2584 {
2585 if (unlock && !lock)
2586 type = SMB2_LOCKFLAG_UNLOCK;
2587 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2588 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2589 current->tgid, length, offset, type, wait);
2590 }
2591
2592 static void
2593 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2594 {
2595 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2596 }
2597
2598 static void
2599 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2600 {
2601 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2602 }
2603
2604 static void
2605 smb2_new_lease_key(struct cifs_fid *fid)
2606 {
2607 generate_random_uuid(fid->lease_key);
2608 }
2609
2610 static int
2611 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2612 const char *search_name,
2613 struct dfs_info3_param **target_nodes,
2614 unsigned int *num_of_nodes,
2615 const struct nls_table *nls_codepage, int remap)
2616 {
2617 int rc;
2618 __le16 *utf16_path = NULL;
2619 int utf16_path_len = 0;
2620 struct cifs_tcon *tcon;
2621 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2622 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2623 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2624
2625 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
2626
2627 /*
2628 * Try to use the IPC tcon, otherwise just use any
2629 */
2630 tcon = ses->tcon_ipc;
2631 if (tcon == NULL) {
2632 spin_lock(&cifs_tcp_ses_lock);
2633 tcon = list_first_entry_or_null(&ses->tcon_list,
2634 struct cifs_tcon,
2635 tcon_list);
2636 if (tcon)
2637 tcon->tc_count++;
2638 spin_unlock(&cifs_tcp_ses_lock);
2639 }
2640
2641 if (tcon == NULL) {
2642 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2643 ses);
2644 rc = -ENOTCONN;
2645 goto out;
2646 }
2647
2648 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2649 &utf16_path_len,
2650 nls_codepage, remap);
2651 if (!utf16_path) {
2652 rc = -ENOMEM;
2653 goto out;
2654 }
2655
2656 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2657 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2658 if (!dfs_req) {
2659 rc = -ENOMEM;
2660 goto out;
2661 }
2662
2663 /* Highest DFS referral version understood */
2664 dfs_req->MaxReferralLevel = DFS_VERSION;
2665
2666 /* Path to resolve in an UTF-16 null-terminated string */
2667 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2668
2669 do {
2670 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2671 FSCTL_DFS_GET_REFERRALS,
2672 true /* is_fsctl */,
2673 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
2674 (char **)&dfs_rsp, &dfs_rsp_size);
2675 } while (rc == -EAGAIN);
2676
2677 if (rc) {
2678 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
2679 cifs_tcon_dbg(VFS, "ioctl error in %s rc=%d\n", __func__, rc);
2680 goto out;
2681 }
2682
2683 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2684 num_of_nodes, target_nodes,
2685 nls_codepage, remap, search_name,
2686 true /* is_unicode */);
2687 if (rc) {
2688 cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
2689 goto out;
2690 }
2691
2692 out:
2693 if (tcon && !tcon->ipc) {
2694 /* ipc tcons are not refcounted */
2695 spin_lock(&cifs_tcp_ses_lock);
2696 tcon->tc_count--;
2697 spin_unlock(&cifs_tcp_ses_lock);
2698 }
2699 kfree(utf16_path);
2700 kfree(dfs_req);
2701 kfree(dfs_rsp);
2702 return rc;
2703 }
2704
2705 static int
2706 parse_reparse_posix(struct reparse_posix_data *symlink_buf,
2707 u32 plen, char **target_path,
2708 struct cifs_sb_info *cifs_sb)
2709 {
2710 unsigned int len;
2711
2712 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
2713 len = le16_to_cpu(symlink_buf->ReparseDataLength);
2714
2715 if (le64_to_cpu(symlink_buf->InodeType) != NFS_SPECFILE_LNK) {
2716 cifs_dbg(VFS, "%lld not a supported symlink type\n",
2717 le64_to_cpu(symlink_buf->InodeType));
2718 return -EOPNOTSUPP;
2719 }
2720
2721 *target_path = cifs_strndup_from_utf16(
2722 symlink_buf->PathBuffer,
2723 len, true, cifs_sb->local_nls);
2724 if (!(*target_path))
2725 return -ENOMEM;
2726
2727 convert_delimiter(*target_path, '/');
2728 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2729
2730 return 0;
2731 }
2732
2733 static int
2734 parse_reparse_symlink(struct reparse_symlink_data_buffer *symlink_buf,
2735 u32 plen, char **target_path,
2736 struct cifs_sb_info *cifs_sb)
2737 {
2738 unsigned int sub_len;
2739 unsigned int sub_offset;
2740
2741 /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
2742
2743 sub_offset = le16_to_cpu(symlink_buf->SubstituteNameOffset);
2744 sub_len = le16_to_cpu(symlink_buf->SubstituteNameLength);
2745 if (sub_offset + 20 > plen ||
2746 sub_offset + sub_len + 20 > plen) {
2747 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
2748 return -EIO;
2749 }
2750
2751 *target_path = cifs_strndup_from_utf16(
2752 symlink_buf->PathBuffer + sub_offset,
2753 sub_len, true, cifs_sb->local_nls);
2754 if (!(*target_path))
2755 return -ENOMEM;
2756
2757 convert_delimiter(*target_path, '/');
2758 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2759
2760 return 0;
2761 }
2762
2763 static int
2764 parse_reparse_point(struct reparse_data_buffer *buf,
2765 u32 plen, char **target_path,
2766 struct cifs_sb_info *cifs_sb)
2767 {
2768 if (plen < sizeof(struct reparse_data_buffer)) {
2769 cifs_dbg(VFS, "reparse buffer is too small. Must be at least 8 bytes but was %d\n",
2770 plen);
2771 return -EIO;
2772 }
2773
2774 if (plen < le16_to_cpu(buf->ReparseDataLength) +
2775 sizeof(struct reparse_data_buffer)) {
2776 cifs_dbg(VFS, "srv returned invalid reparse buf length: %d\n",
2777 plen);
2778 return -EIO;
2779 }
2780
2781 /* See MS-FSCC 2.1.2 */
2782 switch (le32_to_cpu(buf->ReparseTag)) {
2783 case IO_REPARSE_TAG_NFS:
2784 return parse_reparse_posix(
2785 (struct reparse_posix_data *)buf,
2786 plen, target_path, cifs_sb);
2787 case IO_REPARSE_TAG_SYMLINK:
2788 return parse_reparse_symlink(
2789 (struct reparse_symlink_data_buffer *)buf,
2790 plen, target_path, cifs_sb);
2791 default:
2792 cifs_dbg(VFS, "srv returned unknown symlink buffer tag:0x%08x\n",
2793 le32_to_cpu(buf->ReparseTag));
2794 return -EOPNOTSUPP;
2795 }
2796 }
2797
2798 #define SMB2_SYMLINK_STRUCT_SIZE \
2799 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2800
2801 static int
2802 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
2803 struct cifs_sb_info *cifs_sb, const char *full_path,
2804 char **target_path, bool is_reparse_point)
2805 {
2806 int rc;
2807 __le16 *utf16_path = NULL;
2808 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2809 struct cifs_open_parms oparms;
2810 struct cifs_fid fid;
2811 struct kvec err_iov = {NULL, 0};
2812 struct smb2_err_rsp *err_buf = NULL;
2813 struct smb2_symlink_err_rsp *symlink;
2814 unsigned int sub_len;
2815 unsigned int sub_offset;
2816 unsigned int print_len;
2817 unsigned int print_offset;
2818 int flags = 0;
2819 struct smb_rqst rqst[3];
2820 int resp_buftype[3];
2821 struct kvec rsp_iov[3];
2822 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2823 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2824 struct kvec close_iov[1];
2825 struct smb2_create_rsp *create_rsp;
2826 struct smb2_ioctl_rsp *ioctl_rsp;
2827 struct reparse_data_buffer *reparse_buf;
2828 int create_options = is_reparse_point ? OPEN_REPARSE_POINT : 0;
2829 u32 plen;
2830
2831 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2832
2833 *target_path = NULL;
2834
2835 if (smb3_encryption_required(tcon))
2836 flags |= CIFS_TRANSFORM_REQ;
2837
2838 memset(rqst, 0, sizeof(rqst));
2839 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2840 memset(rsp_iov, 0, sizeof(rsp_iov));
2841
2842 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2843 if (!utf16_path)
2844 return -ENOMEM;
2845
2846 /* Open */
2847 memset(&open_iov, 0, sizeof(open_iov));
2848 rqst[0].rq_iov = open_iov;
2849 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2850
2851 memset(&oparms, 0, sizeof(oparms));
2852 oparms.tcon = tcon;
2853 oparms.desired_access = FILE_READ_ATTRIBUTES;
2854 oparms.disposition = FILE_OPEN;
2855 oparms.create_options = cifs_create_options(cifs_sb, create_options);
2856 oparms.fid = &fid;
2857 oparms.reconnect = false;
2858
2859 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2860 if (rc)
2861 goto querty_exit;
2862 smb2_set_next_command(tcon, &rqst[0]);
2863
2864
2865 /* IOCTL */
2866 memset(&io_iov, 0, sizeof(io_iov));
2867 rqst[1].rq_iov = io_iov;
2868 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
2869
2870 rc = SMB2_ioctl_init(tcon, &rqst[1], fid.persistent_fid,
2871 fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
2872 true /* is_fctl */, NULL, 0,
2873 CIFSMaxBufSize -
2874 MAX_SMB2_CREATE_RESPONSE_SIZE -
2875 MAX_SMB2_CLOSE_RESPONSE_SIZE);
2876 if (rc)
2877 goto querty_exit;
2878
2879 smb2_set_next_command(tcon, &rqst[1]);
2880 smb2_set_related(&rqst[1]);
2881
2882
2883 /* Close */
2884 memset(&close_iov, 0, sizeof(close_iov));
2885 rqst[2].rq_iov = close_iov;
2886 rqst[2].rq_nvec = 1;
2887
2888 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2889 if (rc)
2890 goto querty_exit;
2891
2892 smb2_set_related(&rqst[2]);
2893
2894 rc = compound_send_recv(xid, tcon->ses, flags, 3, rqst,
2895 resp_buftype, rsp_iov);
2896
2897 create_rsp = rsp_iov[0].iov_base;
2898 if (create_rsp && create_rsp->sync_hdr.Status)
2899 err_iov = rsp_iov[0];
2900 ioctl_rsp = rsp_iov[1].iov_base;
2901
2902 /*
2903 * Open was successful and we got an ioctl response.
2904 */
2905 if ((rc == 0) && (is_reparse_point)) {
2906 /* See MS-FSCC 2.3.23 */
2907
2908 reparse_buf = (struct reparse_data_buffer *)
2909 ((char *)ioctl_rsp +
2910 le32_to_cpu(ioctl_rsp->OutputOffset));
2911 plen = le32_to_cpu(ioctl_rsp->OutputCount);
2912
2913 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
2914 rsp_iov[1].iov_len) {
2915 cifs_tcon_dbg(VFS, "srv returned invalid ioctl len: %d\n",
2916 plen);
2917 rc = -EIO;
2918 goto querty_exit;
2919 }
2920
2921 rc = parse_reparse_point(reparse_buf, plen, target_path,
2922 cifs_sb);
2923 goto querty_exit;
2924 }
2925
2926 if (!rc || !err_iov.iov_base) {
2927 rc = -ENOENT;
2928 goto querty_exit;
2929 }
2930
2931 err_buf = err_iov.iov_base;
2932 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
2933 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
2934 rc = -EINVAL;
2935 goto querty_exit;
2936 }
2937
2938 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
2939 if (le32_to_cpu(symlink->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
2940 le32_to_cpu(symlink->ReparseTag) != IO_REPARSE_TAG_SYMLINK) {
2941 rc = -EINVAL;
2942 goto querty_exit;
2943 }
2944
2945 /* open must fail on symlink - reset rc */
2946 rc = 0;
2947 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
2948 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
2949 print_len = le16_to_cpu(symlink->PrintNameLength);
2950 print_offset = le16_to_cpu(symlink->PrintNameOffset);
2951
2952 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
2953 rc = -EINVAL;
2954 goto querty_exit;
2955 }
2956
2957 if (err_iov.iov_len <
2958 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
2959 rc = -EINVAL;
2960 goto querty_exit;
2961 }
2962
2963 *target_path = cifs_strndup_from_utf16(
2964 (char *)symlink->PathBuffer + sub_offset,
2965 sub_len, true, cifs_sb->local_nls);
2966 if (!(*target_path)) {
2967 rc = -ENOMEM;
2968 goto querty_exit;
2969 }
2970 convert_delimiter(*target_path, '/');
2971 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2972
2973 querty_exit:
2974 cifs_dbg(FYI, "query symlink rc %d\n", rc);
2975 kfree(utf16_path);
2976 SMB2_open_free(&rqst[0]);
2977 SMB2_ioctl_free(&rqst[1]);
2978 SMB2_close_free(&rqst[2]);
2979 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2980 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2981 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2982 return rc;
2983 }
2984
2985 static struct cifs_ntsd *
2986 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
2987 const struct cifs_fid *cifsfid, u32 *pacllen)
2988 {
2989 struct cifs_ntsd *pntsd = NULL;
2990 unsigned int xid;
2991 int rc = -EOPNOTSUPP;
2992 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2993
2994 if (IS_ERR(tlink))
2995 return ERR_CAST(tlink);
2996
2997 xid = get_xid();
2998 cifs_dbg(FYI, "trying to get acl\n");
2999
3000 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
3001 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
3002 free_xid(xid);
3003
3004 cifs_put_tlink(tlink);
3005
3006 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3007 if (rc)
3008 return ERR_PTR(rc);
3009 return pntsd;
3010
3011 }
3012
3013 static struct cifs_ntsd *
3014 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
3015 const char *path, u32 *pacllen)
3016 {
3017 struct cifs_ntsd *pntsd = NULL;
3018 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3019 unsigned int xid;
3020 int rc;
3021 struct cifs_tcon *tcon;
3022 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3023 struct cifs_fid fid;
3024 struct cifs_open_parms oparms;
3025 __le16 *utf16_path;
3026
3027 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
3028 if (IS_ERR(tlink))
3029 return ERR_CAST(tlink);
3030
3031 tcon = tlink_tcon(tlink);
3032 xid = get_xid();
3033
3034 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3035 if (!utf16_path) {
3036 rc = -ENOMEM;
3037 free_xid(xid);
3038 return ERR_PTR(rc);
3039 }
3040
3041 oparms.tcon = tcon;
3042 oparms.desired_access = READ_CONTROL;
3043 oparms.disposition = FILE_OPEN;
3044 oparms.create_options = cifs_create_options(cifs_sb, 0);
3045 oparms.fid = &fid;
3046 oparms.reconnect = false;
3047
3048 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
3049 NULL);
3050 kfree(utf16_path);
3051 if (!rc) {
3052 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3053 fid.volatile_fid, (void **)&pntsd, pacllen);
3054 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3055 }
3056
3057 cifs_put_tlink(tlink);
3058 free_xid(xid);
3059
3060 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3061 if (rc)
3062 return ERR_PTR(rc);
3063 return pntsd;
3064 }
3065
3066 static int
3067 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
3068 struct inode *inode, const char *path, int aclflag)
3069 {
3070 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3071 unsigned int xid;
3072 int rc, access_flags = 0;
3073 struct cifs_tcon *tcon;
3074 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3075 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3076 struct cifs_fid fid;
3077 struct cifs_open_parms oparms;
3078 __le16 *utf16_path;
3079
3080 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
3081 if (IS_ERR(tlink))
3082 return PTR_ERR(tlink);
3083
3084 tcon = tlink_tcon(tlink);
3085 xid = get_xid();
3086
3087 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
3088 access_flags = WRITE_OWNER;
3089 else
3090 access_flags = WRITE_DAC;
3091
3092 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3093 if (!utf16_path) {
3094 rc = -ENOMEM;
3095 free_xid(xid);
3096 return rc;
3097 }
3098
3099 oparms.tcon = tcon;
3100 oparms.desired_access = access_flags;
3101 oparms.create_options = cifs_create_options(cifs_sb, 0);
3102 oparms.disposition = FILE_OPEN;
3103 oparms.path = path;
3104 oparms.fid = &fid;
3105 oparms.reconnect = false;
3106
3107 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
3108 NULL, NULL);
3109 kfree(utf16_path);
3110 if (!rc) {
3111 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3112 fid.volatile_fid, pnntsd, acllen, aclflag);
3113 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3114 }
3115
3116 cifs_put_tlink(tlink);
3117 free_xid(xid);
3118 return rc;
3119 }
3120
3121 /* Retrieve an ACL from the server */
3122 static struct cifs_ntsd *
3123 get_smb2_acl(struct cifs_sb_info *cifs_sb,
3124 struct inode *inode, const char *path,
3125 u32 *pacllen)
3126 {
3127 struct cifs_ntsd *pntsd = NULL;
3128 struct cifsFileInfo *open_file = NULL;
3129
3130 if (inode)
3131 open_file = find_readable_file(CIFS_I(inode), true);
3132 if (!open_file)
3133 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
3134
3135 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
3136 cifsFileInfo_put(open_file);
3137 return pntsd;
3138 }
3139
3140 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3141 loff_t offset, loff_t len, bool keep_size)
3142 {
3143 struct cifs_ses *ses = tcon->ses;
3144 struct inode *inode;
3145 struct cifsInodeInfo *cifsi;
3146 struct cifsFileInfo *cfile = file->private_data;
3147 struct file_zero_data_information fsctl_buf;
3148 long rc;
3149 unsigned int xid;
3150 __le64 eof;
3151
3152 xid = get_xid();
3153
3154 inode = d_inode(cfile->dentry);
3155 cifsi = CIFS_I(inode);
3156
3157 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3158 ses->Suid, offset, len);
3159
3160
3161 /* if file not oplocked can't be sure whether asking to extend size */
3162 if (!CIFS_CACHE_READ(cifsi))
3163 if (keep_size == false) {
3164 rc = -EOPNOTSUPP;
3165 trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
3166 tcon->tid, ses->Suid, offset, len, rc);
3167 free_xid(xid);
3168 return rc;
3169 }
3170
3171 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3172
3173 fsctl_buf.FileOffset = cpu_to_le64(offset);
3174 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3175
3176 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3177 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
3178 (char *)&fsctl_buf,
3179 sizeof(struct file_zero_data_information),
3180 0, NULL, NULL);
3181 if (rc)
3182 goto zero_range_exit;
3183
3184 /*
3185 * do we also need to change the size of the file?
3186 */
3187 if (keep_size == false && i_size_read(inode) < offset + len) {
3188 eof = cpu_to_le64(offset + len);
3189 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3190 cfile->fid.volatile_fid, cfile->pid, &eof);
3191 }
3192
3193 zero_range_exit:
3194 free_xid(xid);
3195 if (rc)
3196 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3197 ses->Suid, offset, len, rc);
3198 else
3199 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3200 ses->Suid, offset, len);
3201 return rc;
3202 }
3203
3204 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3205 loff_t offset, loff_t len)
3206 {
3207 struct inode *inode;
3208 struct cifsFileInfo *cfile = file->private_data;
3209 struct file_zero_data_information fsctl_buf;
3210 long rc;
3211 unsigned int xid;
3212 __u8 set_sparse = 1;
3213
3214 xid = get_xid();
3215
3216 inode = d_inode(cfile->dentry);
3217
3218 /* Need to make file sparse, if not already, before freeing range. */
3219 /* Consider adding equivalent for compressed since it could also work */
3220 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3221 rc = -EOPNOTSUPP;
3222 free_xid(xid);
3223 return rc;
3224 }
3225
3226 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3227
3228 fsctl_buf.FileOffset = cpu_to_le64(offset);
3229 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3230
3231 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3232 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3233 true /* is_fctl */, (char *)&fsctl_buf,
3234 sizeof(struct file_zero_data_information),
3235 CIFSMaxBufSize, NULL, NULL);
3236 free_xid(xid);
3237 return rc;
3238 }
3239
3240 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3241 loff_t off, loff_t len, bool keep_size)
3242 {
3243 struct inode *inode;
3244 struct cifsInodeInfo *cifsi;
3245 struct cifsFileInfo *cfile = file->private_data;
3246 long rc = -EOPNOTSUPP;
3247 unsigned int xid;
3248 __le64 eof;
3249
3250 xid = get_xid();
3251
3252 inode = d_inode(cfile->dentry);
3253 cifsi = CIFS_I(inode);
3254
3255 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3256 tcon->ses->Suid, off, len);
3257 /* if file not oplocked can't be sure whether asking to extend size */
3258 if (!CIFS_CACHE_READ(cifsi))
3259 if (keep_size == false) {
3260 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3261 tcon->tid, tcon->ses->Suid, off, len, rc);
3262 free_xid(xid);
3263 return rc;
3264 }
3265
3266 /*
3267 * Extending the file
3268 */
3269 if ((keep_size == false) && i_size_read(inode) < off + len) {
3270 rc = inode_newsize_ok(inode, off + len);
3271 if (rc)
3272 goto out;
3273
3274 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0)
3275 smb2_set_sparse(xid, tcon, cfile, inode, false);
3276
3277 eof = cpu_to_le64(off + len);
3278 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3279 cfile->fid.volatile_fid, cfile->pid, &eof);
3280 if (rc == 0) {
3281 cifsi->server_eof = off + len;
3282 cifs_setsize(inode, off + len);
3283 cifs_truncate_page(inode->i_mapping, inode->i_size);
3284 truncate_setsize(inode, off + len);
3285 }
3286 goto out;
3287 }
3288
3289 /*
3290 * Files are non-sparse by default so falloc may be a no-op
3291 * Must check if file sparse. If not sparse, and since we are not
3292 * extending then no need to do anything since file already allocated
3293 */
3294 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
3295 rc = 0;
3296 goto out;
3297 }
3298
3299 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3300 /*
3301 * Check if falloc starts within first few pages of file
3302 * and ends within a few pages of the end of file to
3303 * ensure that most of file is being forced to be
3304 * fallocated now. If so then setting whole file sparse
3305 * ie potentially making a few extra pages at the beginning
3306 * or end of the file non-sparse via set_sparse is harmless.
3307 */
3308 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3309 rc = -EOPNOTSUPP;
3310 goto out;
3311 }
3312 }
3313
3314 smb2_set_sparse(xid, tcon, cfile, inode, false);
3315 rc = 0;
3316
3317 out:
3318 if (rc)
3319 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3320 tcon->ses->Suid, off, len, rc);
3321 else
3322 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3323 tcon->ses->Suid, off, len);
3324
3325 free_xid(xid);
3326 return rc;
3327 }
3328
3329 static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3330 {
3331 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3332 struct cifsInodeInfo *cifsi;
3333 struct inode *inode;
3334 int rc = 0;
3335 struct file_allocated_range_buffer in_data, *out_data = NULL;
3336 u32 out_data_len;
3337 unsigned int xid;
3338
3339 if (whence != SEEK_HOLE && whence != SEEK_DATA)
3340 return generic_file_llseek(file, offset, whence);
3341
3342 inode = d_inode(cfile->dentry);
3343 cifsi = CIFS_I(inode);
3344
3345 if (offset < 0 || offset >= i_size_read(inode))
3346 return -ENXIO;
3347
3348 xid = get_xid();
3349 /*
3350 * We need to be sure that all dirty pages are written as they
3351 * might fill holes on the server.
3352 * Note that we also MUST flush any written pages since at least
3353 * some servers (Windows2016) will not reflect recent writes in
3354 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3355 */
3356 wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
3357 if (wrcfile) {
3358 filemap_write_and_wait(inode->i_mapping);
3359 smb2_flush_file(xid, tcon, &wrcfile->fid);
3360 cifsFileInfo_put(wrcfile);
3361 }
3362
3363 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3364 if (whence == SEEK_HOLE)
3365 offset = i_size_read(inode);
3366 goto lseek_exit;
3367 }
3368
3369 in_data.file_offset = cpu_to_le64(offset);
3370 in_data.length = cpu_to_le64(i_size_read(inode));
3371
3372 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3373 cfile->fid.volatile_fid,
3374 FSCTL_QUERY_ALLOCATED_RANGES, true,
3375 (char *)&in_data, sizeof(in_data),
3376 sizeof(struct file_allocated_range_buffer),
3377 (char **)&out_data, &out_data_len);
3378 if (rc == -E2BIG)
3379 rc = 0;
3380 if (rc)
3381 goto lseek_exit;
3382
3383 if (whence == SEEK_HOLE && out_data_len == 0)
3384 goto lseek_exit;
3385
3386 if (whence == SEEK_DATA && out_data_len == 0) {
3387 rc = -ENXIO;
3388 goto lseek_exit;
3389 }
3390
3391 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3392 rc = -EINVAL;
3393 goto lseek_exit;
3394 }
3395 if (whence == SEEK_DATA) {
3396 offset = le64_to_cpu(out_data->file_offset);
3397 goto lseek_exit;
3398 }
3399 if (offset < le64_to_cpu(out_data->file_offset))
3400 goto lseek_exit;
3401
3402 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3403
3404 lseek_exit:
3405 free_xid(xid);
3406 kfree(out_data);
3407 if (!rc)
3408 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3409 else
3410 return rc;
3411 }
3412
3413 static int smb3_fiemap(struct cifs_tcon *tcon,
3414 struct cifsFileInfo *cfile,
3415 struct fiemap_extent_info *fei, u64 start, u64 len)
3416 {
3417 unsigned int xid;
3418 struct file_allocated_range_buffer in_data, *out_data;
3419 u32 out_data_len;
3420 int i, num, rc, flags, last_blob;
3421 u64 next;
3422
3423 if (fiemap_check_flags(fei, FIEMAP_FLAG_SYNC))
3424 return -EBADR;
3425
3426 xid = get_xid();
3427 again:
3428 in_data.file_offset = cpu_to_le64(start);
3429 in_data.length = cpu_to_le64(len);
3430
3431 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3432 cfile->fid.volatile_fid,
3433 FSCTL_QUERY_ALLOCATED_RANGES, true,
3434 (char *)&in_data, sizeof(in_data),
3435 1024 * sizeof(struct file_allocated_range_buffer),
3436 (char **)&out_data, &out_data_len);
3437 if (rc == -E2BIG) {
3438 last_blob = 0;
3439 rc = 0;
3440 } else
3441 last_blob = 1;
3442 if (rc)
3443 goto out;
3444
3445 if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) {
3446 rc = -EINVAL;
3447 goto out;
3448 }
3449 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3450 rc = -EINVAL;
3451 goto out;
3452 }
3453
3454 num = out_data_len / sizeof(struct file_allocated_range_buffer);
3455 for (i = 0; i < num; i++) {
3456 flags = 0;
3457 if (i == num - 1 && last_blob)
3458 flags |= FIEMAP_EXTENT_LAST;
3459
3460 rc = fiemap_fill_next_extent(fei,
3461 le64_to_cpu(out_data[i].file_offset),
3462 le64_to_cpu(out_data[i].file_offset),
3463 le64_to_cpu(out_data[i].length),
3464 flags);
3465 if (rc < 0)
3466 goto out;
3467 if (rc == 1) {
3468 rc = 0;
3469 goto out;
3470 }
3471 }
3472
3473 if (!last_blob) {
3474 next = le64_to_cpu(out_data[num - 1].file_offset) +
3475 le64_to_cpu(out_data[num - 1].length);
3476 len = len - (next - start);
3477 start = next;
3478 goto again;
3479 }
3480
3481 out:
3482 free_xid(xid);
3483 kfree(out_data);
3484 return rc;
3485 }
3486
3487 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3488 loff_t off, loff_t len)
3489 {
3490 /* KEEP_SIZE already checked for by do_fallocate */
3491 if (mode & FALLOC_FL_PUNCH_HOLE)
3492 return smb3_punch_hole(file, tcon, off, len);
3493 else if (mode & FALLOC_FL_ZERO_RANGE) {
3494 if (mode & FALLOC_FL_KEEP_SIZE)
3495 return smb3_zero_range(file, tcon, off, len, true);
3496 return smb3_zero_range(file, tcon, off, len, false);
3497 } else if (mode == FALLOC_FL_KEEP_SIZE)
3498 return smb3_simple_falloc(file, tcon, off, len, true);
3499 else if (mode == 0)
3500 return smb3_simple_falloc(file, tcon, off, len, false);
3501
3502 return -EOPNOTSUPP;
3503 }
3504
3505 static void
3506 smb2_downgrade_oplock(struct TCP_Server_Info *server,
3507 struct cifsInodeInfo *cinode, __u32 oplock,
3508 unsigned int epoch, bool *purge_cache)
3509 {
3510 server->ops->set_oplock_level(cinode, oplock, 0, NULL);
3511 }
3512
3513 static void
3514 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3515 unsigned int epoch, bool *purge_cache);
3516
3517 static void
3518 smb3_downgrade_oplock(struct TCP_Server_Info *server,
3519 struct cifsInodeInfo *cinode, __u32 oplock,
3520 unsigned int epoch, bool *purge_cache)
3521 {
3522 unsigned int old_state = cinode->oplock;
3523 unsigned int old_epoch = cinode->epoch;
3524 unsigned int new_state;
3525
3526 if (epoch > old_epoch) {
3527 smb21_set_oplock_level(cinode, oplock, 0, NULL);
3528 cinode->epoch = epoch;
3529 }
3530
3531 new_state = cinode->oplock;
3532 *purge_cache = false;
3533
3534 if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
3535 (new_state & CIFS_CACHE_READ_FLG) == 0)
3536 *purge_cache = true;
3537 else if (old_state == new_state && (epoch - old_epoch > 1))
3538 *purge_cache = true;
3539 }
3540
3541 static void
3542 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3543 unsigned int epoch, bool *purge_cache)
3544 {
3545 oplock &= 0xFF;
3546 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3547 return;
3548 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3549 cinode->oplock = CIFS_CACHE_RHW_FLG;
3550 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3551 &cinode->vfs_inode);
3552 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
3553 cinode->oplock = CIFS_CACHE_RW_FLG;
3554 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3555 &cinode->vfs_inode);
3556 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3557 cinode->oplock = CIFS_CACHE_READ_FLG;
3558 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3559 &cinode->vfs_inode);
3560 } else
3561 cinode->oplock = 0;
3562 }
3563
3564 static void
3565 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3566 unsigned int epoch, bool *purge_cache)
3567 {
3568 char message[5] = {0};
3569 unsigned int new_oplock = 0;
3570
3571 oplock &= 0xFF;
3572 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3573 return;
3574
3575 /* Check if the server granted an oplock rather than a lease */
3576 if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3577 return smb2_set_oplock_level(cinode, oplock, epoch,
3578 purge_cache);
3579
3580 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
3581 new_oplock |= CIFS_CACHE_READ_FLG;
3582 strcat(message, "R");
3583 }
3584 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
3585 new_oplock |= CIFS_CACHE_HANDLE_FLG;
3586 strcat(message, "H");
3587 }
3588 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
3589 new_oplock |= CIFS_CACHE_WRITE_FLG;
3590 strcat(message, "W");
3591 }
3592 if (!new_oplock)
3593 strncpy(message, "None", sizeof(message));
3594
3595 cinode->oplock = new_oplock;
3596 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3597 &cinode->vfs_inode);
3598 }
3599
3600 static void
3601 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3602 unsigned int epoch, bool *purge_cache)
3603 {
3604 unsigned int old_oplock = cinode->oplock;
3605
3606 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3607
3608 if (purge_cache) {
3609 *purge_cache = false;
3610 if (old_oplock == CIFS_CACHE_READ_FLG) {
3611 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3612 (epoch - cinode->epoch > 0))
3613 *purge_cache = true;
3614 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3615 (epoch - cinode->epoch > 1))
3616 *purge_cache = true;
3617 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3618 (epoch - cinode->epoch > 1))
3619 *purge_cache = true;
3620 else if (cinode->oplock == 0 &&
3621 (epoch - cinode->epoch > 0))
3622 *purge_cache = true;
3623 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
3624 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3625 (epoch - cinode->epoch > 0))
3626 *purge_cache = true;
3627 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3628 (epoch - cinode->epoch > 1))
3629 *purge_cache = true;
3630 }
3631 cinode->epoch = epoch;
3632 }
3633 }
3634
3635 static bool
3636 smb2_is_read_op(__u32 oplock)
3637 {
3638 return oplock == SMB2_OPLOCK_LEVEL_II;
3639 }
3640
3641 static bool
3642 smb21_is_read_op(__u32 oplock)
3643 {
3644 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
3645 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
3646 }
3647
3648 static __le32
3649 map_oplock_to_lease(u8 oplock)
3650 {
3651 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3652 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
3653 else if (oplock == SMB2_OPLOCK_LEVEL_II)
3654 return SMB2_LEASE_READ_CACHING;
3655 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3656 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
3657 SMB2_LEASE_WRITE_CACHING;
3658 return 0;
3659 }
3660
3661 static char *
3662 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3663 {
3664 struct create_lease *buf;
3665
3666 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
3667 if (!buf)
3668 return NULL;
3669
3670 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3671 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3672
3673 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3674 (struct create_lease, lcontext));
3675 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
3676 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3677 (struct create_lease, Name));
3678 buf->ccontext.NameLength = cpu_to_le16(4);
3679 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3680 buf->Name[0] = 'R';
3681 buf->Name[1] = 'q';
3682 buf->Name[2] = 'L';
3683 buf->Name[3] = 's';
3684 return (char *)buf;
3685 }
3686
3687 static char *
3688 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
3689 {
3690 struct create_lease_v2 *buf;
3691
3692 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
3693 if (!buf)
3694 return NULL;
3695
3696 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3697 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3698
3699 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3700 (struct create_lease_v2, lcontext));
3701 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
3702 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3703 (struct create_lease_v2, Name));
3704 buf->ccontext.NameLength = cpu_to_le16(4);
3705 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3706 buf->Name[0] = 'R';
3707 buf->Name[1] = 'q';
3708 buf->Name[2] = 'L';
3709 buf->Name[3] = 's';
3710 return (char *)buf;
3711 }
3712
3713 static __u8
3714 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3715 {
3716 struct create_lease *lc = (struct create_lease *)buf;
3717
3718 *epoch = 0; /* not used */
3719 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3720 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3721 return le32_to_cpu(lc->lcontext.LeaseState);
3722 }
3723
3724 static __u8
3725 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3726 {
3727 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
3728
3729 *epoch = le16_to_cpu(lc->lcontext.Epoch);
3730 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3731 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3732 if (lease_key)
3733 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
3734 return le32_to_cpu(lc->lcontext.LeaseState);
3735 }
3736
3737 static unsigned int
3738 smb2_wp_retry_size(struct inode *inode)
3739 {
3740 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
3741 SMB2_MAX_BUFFER_SIZE);
3742 }
3743
3744 static bool
3745 smb2_dir_needs_close(struct cifsFileInfo *cfile)
3746 {
3747 return !cfile->invalidHandle;
3748 }
3749
3750 static void
3751 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
3752 struct smb_rqst *old_rq, __le16 cipher_type)
3753 {
3754 struct smb2_sync_hdr *shdr =
3755 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
3756
3757 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
3758 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
3759 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
3760 tr_hdr->Flags = cpu_to_le16(0x01);
3761 if (cipher_type == SMB2_ENCRYPTION_AES128_GCM)
3762 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128GCM_NONCE);
3763 else
3764 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CCM_NONCE);
3765 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
3766 }
3767
3768 /* We can not use the normal sg_set_buf() as we will sometimes pass a
3769 * stack object as buf.
3770 */
3771 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
3772 unsigned int buflen)
3773 {
3774 void *addr;
3775 /*
3776 * VMAP_STACK (at least) puts stack into the vmalloc address space
3777 */
3778 if (is_vmalloc_addr(buf))
3779 addr = vmalloc_to_page(buf);
3780 else
3781 addr = virt_to_page(buf);
3782 sg_set_page(sg, addr, buflen, offset_in_page(buf));
3783 }
3784
3785 /* Assumes the first rqst has a transform header as the first iov.
3786 * I.e.
3787 * rqst[0].rq_iov[0] is transform header
3788 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
3789 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
3790 */
3791 static struct scatterlist *
3792 init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
3793 {
3794 unsigned int sg_len;
3795 struct scatterlist *sg;
3796 unsigned int i;
3797 unsigned int j;
3798 unsigned int idx = 0;
3799 int skip;
3800
3801 sg_len = 1;
3802 for (i = 0; i < num_rqst; i++)
3803 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
3804
3805 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
3806 if (!sg)
3807 return NULL;
3808
3809 sg_init_table(sg, sg_len);
3810 for (i = 0; i < num_rqst; i++) {
3811 for (j = 0; j < rqst[i].rq_nvec; j++) {
3812 /*
3813 * The first rqst has a transform header where the
3814 * first 20 bytes are not part of the encrypted blob
3815 */
3816 skip = (i == 0) && (j == 0) ? 20 : 0;
3817 smb2_sg_set_buf(&sg[idx++],
3818 rqst[i].rq_iov[j].iov_base + skip,
3819 rqst[i].rq_iov[j].iov_len - skip);
3820 }
3821
3822 for (j = 0; j < rqst[i].rq_npages; j++) {
3823 unsigned int len, offset;
3824
3825 rqst_page_get_length(&rqst[i], j, &len, &offset);
3826 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
3827 }
3828 }
3829 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
3830 return sg;
3831 }
3832
3833 static int
3834 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
3835 {
3836 struct cifs_ses *ses;
3837 u8 *ses_enc_key;
3838
3839 spin_lock(&cifs_tcp_ses_lock);
3840 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
3841 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3842 if (ses->Suid == ses_id) {
3843 ses_enc_key = enc ? ses->smb3encryptionkey :
3844 ses->smb3decryptionkey;
3845 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
3846 spin_unlock(&cifs_tcp_ses_lock);
3847 return 0;
3848 }
3849 }
3850 }
3851 spin_unlock(&cifs_tcp_ses_lock);
3852
3853 return 1;
3854 }
3855 /*
3856 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
3857 * iov[0] - transform header (associate data),
3858 * iov[1-N] - SMB2 header and pages - data to encrypt.
3859 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
3860 * untouched.
3861 */
3862 static int
3863 crypt_message(struct TCP_Server_Info *server, int num_rqst,
3864 struct smb_rqst *rqst, int enc)
3865 {
3866 struct smb2_transform_hdr *tr_hdr =
3867 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
3868 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
3869 int rc = 0;
3870 struct scatterlist *sg;
3871 u8 sign[SMB2_SIGNATURE_SIZE] = {};
3872 u8 key[SMB3_SIGN_KEY_SIZE];
3873 struct aead_request *req;
3874 char *iv;
3875 unsigned int iv_len;
3876 DECLARE_CRYPTO_WAIT(wait);
3877 struct crypto_aead *tfm;
3878 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3879
3880 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
3881 if (rc) {
3882 cifs_server_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
3883 enc ? "en" : "de");
3884 return 0;
3885 }
3886
3887 rc = smb3_crypto_aead_allocate(server);
3888 if (rc) {
3889 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
3890 return rc;
3891 }
3892
3893 tfm = enc ? server->secmech.ccmaesencrypt :
3894 server->secmech.ccmaesdecrypt;
3895 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
3896 if (rc) {
3897 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
3898 return rc;
3899 }
3900
3901 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
3902 if (rc) {
3903 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
3904 return rc;
3905 }
3906
3907 req = aead_request_alloc(tfm, GFP_KERNEL);
3908 if (!req) {
3909 cifs_server_dbg(VFS, "%s: Failed to alloc aead request\n", __func__);
3910 return -ENOMEM;
3911 }
3912
3913 if (!enc) {
3914 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
3915 crypt_len += SMB2_SIGNATURE_SIZE;
3916 }
3917
3918 sg = init_sg(num_rqst, rqst, sign);
3919 if (!sg) {
3920 cifs_server_dbg(VFS, "%s: Failed to init sg\n", __func__);
3921 rc = -ENOMEM;
3922 goto free_req;
3923 }
3924
3925 iv_len = crypto_aead_ivsize(tfm);
3926 iv = kzalloc(iv_len, GFP_KERNEL);
3927 if (!iv) {
3928 cifs_server_dbg(VFS, "%s: Failed to alloc iv\n", __func__);
3929 rc = -ENOMEM;
3930 goto free_sg;
3931 }
3932
3933 if (server->cipher_type == SMB2_ENCRYPTION_AES128_GCM)
3934 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES128GCM_NONCE);
3935 else {
3936 iv[0] = 3;
3937 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CCM_NONCE);
3938 }
3939
3940 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
3941 aead_request_set_ad(req, assoc_data_len);
3942
3943 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3944 crypto_req_done, &wait);
3945
3946 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
3947 : crypto_aead_decrypt(req), &wait);
3948
3949 if (!rc && enc)
3950 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
3951
3952 kfree(iv);
3953 free_sg:
3954 kfree(sg);
3955 free_req:
3956 kfree(req);
3957 return rc;
3958 }
3959
3960 void
3961 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
3962 {
3963 int i, j;
3964
3965 for (i = 0; i < num_rqst; i++) {
3966 if (rqst[i].rq_pages) {
3967 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
3968 put_page(rqst[i].rq_pages[j]);
3969 kfree(rqst[i].rq_pages);
3970 }
3971 }
3972 }
3973
3974 /*
3975 * This function will initialize new_rq and encrypt the content.
3976 * The first entry, new_rq[0], only contains a single iov which contains
3977 * a smb2_transform_hdr and is pre-allocated by the caller.
3978 * This function then populates new_rq[1+] with the content from olq_rq[0+].
3979 *
3980 * The end result is an array of smb_rqst structures where the first structure
3981 * only contains a single iov for the transform header which we then can pass
3982 * to crypt_message().
3983 *
3984 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
3985 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
3986 */
3987 static int
3988 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
3989 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
3990 {
3991 struct page **pages;
3992 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
3993 unsigned int npages;
3994 unsigned int orig_len = 0;
3995 int i, j;
3996 int rc = -ENOMEM;
3997
3998 for (i = 1; i < num_rqst; i++) {
3999 npages = old_rq[i - 1].rq_npages;
4000 pages = kmalloc_array(npages, sizeof(struct page *),
4001 GFP_KERNEL);
4002 if (!pages)
4003 goto err_free;
4004
4005 new_rq[i].rq_pages = pages;
4006 new_rq[i].rq_npages = npages;
4007 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
4008 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
4009 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
4010 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
4011 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
4012
4013 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
4014
4015 for (j = 0; j < npages; j++) {
4016 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4017 if (!pages[j])
4018 goto err_free;
4019 }
4020
4021 /* copy pages form the old */
4022 for (j = 0; j < npages; j++) {
4023 char *dst, *src;
4024 unsigned int offset, len;
4025
4026 rqst_page_get_length(&new_rq[i], j, &len, &offset);
4027
4028 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
4029 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
4030
4031 memcpy(dst, src, len);
4032 kunmap(new_rq[i].rq_pages[j]);
4033 kunmap(old_rq[i - 1].rq_pages[j]);
4034 }
4035 }
4036
4037 /* fill the 1st iov with a transform header */
4038 fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
4039
4040 rc = crypt_message(server, num_rqst, new_rq, 1);
4041 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
4042 if (rc)
4043 goto err_free;
4044
4045 return rc;
4046
4047 err_free:
4048 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
4049 return rc;
4050 }
4051
4052 static int
4053 smb3_is_transform_hdr(void *buf)
4054 {
4055 struct smb2_transform_hdr *trhdr = buf;
4056
4057 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
4058 }
4059
4060 static int
4061 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
4062 unsigned int buf_data_size, struct page **pages,
4063 unsigned int npages, unsigned int page_data_size)
4064 {
4065 struct kvec iov[2];
4066 struct smb_rqst rqst = {NULL};
4067 int rc;
4068
4069 iov[0].iov_base = buf;
4070 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
4071 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
4072 iov[1].iov_len = buf_data_size;
4073
4074 rqst.rq_iov = iov;
4075 rqst.rq_nvec = 2;
4076 rqst.rq_pages = pages;
4077 rqst.rq_npages = npages;
4078 rqst.rq_pagesz = PAGE_SIZE;
4079 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
4080
4081 rc = crypt_message(server, 1, &rqst, 0);
4082 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
4083
4084 if (rc)
4085 return rc;
4086
4087 memmove(buf, iov[1].iov_base, buf_data_size);
4088
4089 server->total_read = buf_data_size + page_data_size;
4090
4091 return rc;
4092 }
4093
4094 static int
4095 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
4096 unsigned int npages, unsigned int len)
4097 {
4098 int i;
4099 int length;
4100
4101 for (i = 0; i < npages; i++) {
4102 struct page *page = pages[i];
4103 size_t n;
4104
4105 n = len;
4106 if (len >= PAGE_SIZE) {
4107 /* enough data to fill the page */
4108 n = PAGE_SIZE;
4109 len -= n;
4110 } else {
4111 zero_user(page, len, PAGE_SIZE - len);
4112 len = 0;
4113 }
4114 length = cifs_read_page_from_socket(server, page, 0, n);
4115 if (length < 0)
4116 return length;
4117 server->total_read += length;
4118 }
4119
4120 return 0;
4121 }
4122
4123 static int
4124 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
4125 unsigned int cur_off, struct bio_vec **page_vec)
4126 {
4127 struct bio_vec *bvec;
4128 int i;
4129
4130 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
4131 if (!bvec)
4132 return -ENOMEM;
4133
4134 for (i = 0; i < npages; i++) {
4135 bvec[i].bv_page = pages[i];
4136 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
4137 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
4138 data_size -= bvec[i].bv_len;
4139 }
4140
4141 if (data_size != 0) {
4142 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4143 kfree(bvec);
4144 return -EIO;
4145 }
4146
4147 *page_vec = bvec;
4148 return 0;
4149 }
4150
4151 static int
4152 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4153 char *buf, unsigned int buf_len, struct page **pages,
4154 unsigned int npages, unsigned int page_data_size)
4155 {
4156 unsigned int data_offset;
4157 unsigned int data_len;
4158 unsigned int cur_off;
4159 unsigned int cur_page_idx;
4160 unsigned int pad_len;
4161 struct cifs_readdata *rdata = mid->callback_data;
4162 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
4163 struct bio_vec *bvec = NULL;
4164 struct iov_iter iter;
4165 struct kvec iov;
4166 int length;
4167 bool use_rdma_mr = false;
4168
4169 if (shdr->Command != SMB2_READ) {
4170 cifs_server_dbg(VFS, "only big read responses are supported\n");
4171 return -ENOTSUPP;
4172 }
4173
4174 if (server->ops->is_session_expired &&
4175 server->ops->is_session_expired(buf)) {
4176 cifs_reconnect(server);
4177 return -1;
4178 }
4179
4180 if (server->ops->is_status_pending &&
4181 server->ops->is_status_pending(buf, server))
4182 return -1;
4183
4184 /* set up first two iov to get credits */
4185 rdata->iov[0].iov_base = buf;
4186 rdata->iov[0].iov_len = 0;
4187 rdata->iov[1].iov_base = buf;
4188 rdata->iov[1].iov_len =
4189 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
4190 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4191 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4192 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4193 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4194
4195 rdata->result = server->ops->map_error(buf, true);
4196 if (rdata->result != 0) {
4197 cifs_dbg(FYI, "%s: server returned error %d\n",
4198 __func__, rdata->result);
4199 /* normal error on read response */
4200 dequeue_mid(mid, false);
4201 return 0;
4202 }
4203
4204 data_offset = server->ops->read_data_offset(buf);
4205 #ifdef CONFIG_CIFS_SMB_DIRECT
4206 use_rdma_mr = rdata->mr;
4207 #endif
4208 data_len = server->ops->read_data_length(buf, use_rdma_mr);
4209
4210 if (data_offset < server->vals->read_rsp_size) {
4211 /*
4212 * win2k8 sometimes sends an offset of 0 when the read
4213 * is beyond the EOF. Treat it as if the data starts just after
4214 * the header.
4215 */
4216 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4217 __func__, data_offset);
4218 data_offset = server->vals->read_rsp_size;
4219 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4220 /* data_offset is beyond the end of smallbuf */
4221 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4222 __func__, data_offset);
4223 rdata->result = -EIO;
4224 dequeue_mid(mid, rdata->result);
4225 return 0;
4226 }
4227
4228 pad_len = data_offset - server->vals->read_rsp_size;
4229
4230 if (buf_len <= data_offset) {
4231 /* read response payload is in pages */
4232 cur_page_idx = pad_len / PAGE_SIZE;
4233 cur_off = pad_len % PAGE_SIZE;
4234
4235 if (cur_page_idx != 0) {
4236 /* data offset is beyond the 1st page of response */
4237 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4238 __func__, data_offset);
4239 rdata->result = -EIO;
4240 dequeue_mid(mid, rdata->result);
4241 return 0;
4242 }
4243
4244 if (data_len > page_data_size - pad_len) {
4245 /* data_len is corrupt -- discard frame */
4246 rdata->result = -EIO;
4247 dequeue_mid(mid, rdata->result);
4248 return 0;
4249 }
4250
4251 rdata->result = init_read_bvec(pages, npages, page_data_size,
4252 cur_off, &bvec);
4253 if (rdata->result != 0) {
4254 dequeue_mid(mid, rdata->result);
4255 return 0;
4256 }
4257
4258 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
4259 } else if (buf_len >= data_offset + data_len) {
4260 /* read response payload is in buf */
4261 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
4262 iov.iov_base = buf + data_offset;
4263 iov.iov_len = data_len;
4264 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
4265 } else {
4266 /* read response payload cannot be in both buf and pages */
4267 WARN_ONCE(1, "buf can not contain only a part of read data");
4268 rdata->result = -EIO;
4269 dequeue_mid(mid, rdata->result);
4270 return 0;
4271 }
4272
4273 length = rdata->copy_into_pages(server, rdata, &iter);
4274
4275 kfree(bvec);
4276
4277 if (length < 0)
4278 return length;
4279
4280 dequeue_mid(mid, false);
4281 return length;
4282 }
4283
4284 struct smb2_decrypt_work {
4285 struct work_struct decrypt;
4286 struct TCP_Server_Info *server;
4287 struct page **ppages;
4288 char *buf;
4289 unsigned int npages;
4290 unsigned int len;
4291 };
4292
4293
4294 static void smb2_decrypt_offload(struct work_struct *work)
4295 {
4296 struct smb2_decrypt_work *dw = container_of(work,
4297 struct smb2_decrypt_work, decrypt);
4298 int i, rc;
4299 struct mid_q_entry *mid;
4300
4301 rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
4302 dw->ppages, dw->npages, dw->len);
4303 if (rc) {
4304 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4305 goto free_pages;
4306 }
4307
4308 dw->server->lstrp = jiffies;
4309 mid = smb2_find_mid(dw->server, dw->buf);
4310 if (mid == NULL)
4311 cifs_dbg(FYI, "mid not found\n");
4312 else {
4313 mid->decrypted = true;
4314 rc = handle_read_data(dw->server, mid, dw->buf,
4315 dw->server->vals->read_rsp_size,
4316 dw->ppages, dw->npages, dw->len);
4317 mid->callback(mid);
4318 cifs_mid_q_entry_release(mid);
4319 }
4320
4321 free_pages:
4322 for (i = dw->npages-1; i >= 0; i--)
4323 put_page(dw->ppages[i]);
4324
4325 kfree(dw->ppages);
4326 cifs_small_buf_release(dw->buf);
4327 kfree(dw);
4328 }
4329
4330
4331 static int
4332 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4333 int *num_mids)
4334 {
4335 char *buf = server->smallbuf;
4336 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4337 unsigned int npages;
4338 struct page **pages;
4339 unsigned int len;
4340 unsigned int buflen = server->pdu_size;
4341 int rc;
4342 int i = 0;
4343 struct smb2_decrypt_work *dw;
4344
4345 *num_mids = 1;
4346 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
4347 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
4348
4349 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
4350 if (rc < 0)
4351 return rc;
4352 server->total_read += rc;
4353
4354 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
4355 server->vals->read_rsp_size;
4356 npages = DIV_ROUND_UP(len, PAGE_SIZE);
4357
4358 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
4359 if (!pages) {
4360 rc = -ENOMEM;
4361 goto discard_data;
4362 }
4363
4364 for (; i < npages; i++) {
4365 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4366 if (!pages[i]) {
4367 rc = -ENOMEM;
4368 goto discard_data;
4369 }
4370 }
4371
4372 /* read read data into pages */
4373 rc = read_data_into_pages(server, pages, npages, len);
4374 if (rc)
4375 goto free_pages;
4376
4377 rc = cifs_discard_remaining_data(server);
4378 if (rc)
4379 goto free_pages;
4380
4381 /*
4382 * For large reads, offload to different thread for better performance,
4383 * use more cores decrypting which can be expensive
4384 */
4385
4386 if ((server->min_offload) && (server->in_flight > 1) &&
4387 (server->pdu_size >= server->min_offload)) {
4388 dw = kmalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
4389 if (dw == NULL)
4390 goto non_offloaded_decrypt;
4391
4392 dw->buf = server->smallbuf;
4393 server->smallbuf = (char *)cifs_small_buf_get();
4394
4395 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
4396
4397 dw->npages = npages;
4398 dw->server = server;
4399 dw->ppages = pages;
4400 dw->len = len;
4401 queue_work(decrypt_wq, &dw->decrypt);
4402 *num_mids = 0; /* worker thread takes care of finding mid */
4403 return -1;
4404 }
4405
4406 non_offloaded_decrypt:
4407 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
4408 pages, npages, len);
4409 if (rc)
4410 goto free_pages;
4411
4412 *mid = smb2_find_mid(server, buf);
4413 if (*mid == NULL)
4414 cifs_dbg(FYI, "mid not found\n");
4415 else {
4416 cifs_dbg(FYI, "mid found\n");
4417 (*mid)->decrypted = true;
4418 rc = handle_read_data(server, *mid, buf,
4419 server->vals->read_rsp_size,
4420 pages, npages, len);
4421 }
4422
4423 free_pages:
4424 for (i = i - 1; i >= 0; i--)
4425 put_page(pages[i]);
4426 kfree(pages);
4427 return rc;
4428 discard_data:
4429 cifs_discard_remaining_data(server);
4430 goto free_pages;
4431 }
4432
4433 static int
4434 receive_encrypted_standard(struct TCP_Server_Info *server,
4435 struct mid_q_entry **mids, char **bufs,
4436 int *num_mids)
4437 {
4438 int ret, length;
4439 char *buf = server->smallbuf;
4440 struct smb2_sync_hdr *shdr;
4441 unsigned int pdu_length = server->pdu_size;
4442 unsigned int buf_size;
4443 struct mid_q_entry *mid_entry;
4444 int next_is_large;
4445 char *next_buffer = NULL;
4446
4447 *num_mids = 0;
4448
4449 /* switch to large buffer if too big for a small one */
4450 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
4451 server->large_buf = true;
4452 memcpy(server->bigbuf, buf, server->total_read);
4453 buf = server->bigbuf;
4454 }
4455
4456 /* now read the rest */
4457 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
4458 pdu_length - HEADER_SIZE(server) + 1);
4459 if (length < 0)
4460 return length;
4461 server->total_read += length;
4462
4463 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
4464 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
4465 if (length)
4466 return length;
4467
4468 next_is_large = server->large_buf;
4469 one_more:
4470 shdr = (struct smb2_sync_hdr *)buf;
4471 if (shdr->NextCommand) {
4472 if (next_is_large)
4473 next_buffer = (char *)cifs_buf_get();
4474 else
4475 next_buffer = (char *)cifs_small_buf_get();
4476 memcpy(next_buffer,
4477 buf + le32_to_cpu(shdr->NextCommand),
4478 pdu_length - le32_to_cpu(shdr->NextCommand));
4479 }
4480
4481 mid_entry = smb2_find_mid(server, buf);
4482 if (mid_entry == NULL)
4483 cifs_dbg(FYI, "mid not found\n");
4484 else {
4485 cifs_dbg(FYI, "mid found\n");
4486 mid_entry->decrypted = true;
4487 mid_entry->resp_buf_size = server->pdu_size;
4488 }
4489
4490 if (*num_mids >= MAX_COMPOUND) {
4491 cifs_server_dbg(VFS, "too many PDUs in compound\n");
4492 return -1;
4493 }
4494 bufs[*num_mids] = buf;
4495 mids[(*num_mids)++] = mid_entry;
4496
4497 if (mid_entry && mid_entry->handle)
4498 ret = mid_entry->handle(server, mid_entry);
4499 else
4500 ret = cifs_handle_standard(server, mid_entry);
4501
4502 if (ret == 0 && shdr->NextCommand) {
4503 pdu_length -= le32_to_cpu(shdr->NextCommand);
4504 server->large_buf = next_is_large;
4505 if (next_is_large)
4506 server->bigbuf = buf = next_buffer;
4507 else
4508 server->smallbuf = buf = next_buffer;
4509 goto one_more;
4510 } else if (ret != 0) {
4511 /*
4512 * ret != 0 here means that we didn't get to handle_mid() thus
4513 * server->smallbuf and server->bigbuf are still valid. We need
4514 * to free next_buffer because it is not going to be used
4515 * anywhere.
4516 */
4517 if (next_is_large)
4518 free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
4519 else
4520 free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
4521 }
4522
4523 return ret;
4524 }
4525
4526 static int
4527 smb3_receive_transform(struct TCP_Server_Info *server,
4528 struct mid_q_entry **mids, char **bufs, int *num_mids)
4529 {
4530 char *buf = server->smallbuf;
4531 unsigned int pdu_length = server->pdu_size;
4532 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4533 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4534
4535 if (pdu_length < sizeof(struct smb2_transform_hdr) +
4536 sizeof(struct smb2_sync_hdr)) {
4537 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
4538 pdu_length);
4539 cifs_reconnect(server);
4540 return -ECONNABORTED;
4541 }
4542
4543 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
4544 cifs_server_dbg(VFS, "Transform message is broken\n");
4545 cifs_reconnect(server);
4546 return -ECONNABORTED;
4547 }
4548
4549 /* TODO: add support for compounds containing READ. */
4550 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
4551 return receive_encrypted_read(server, &mids[0], num_mids);
4552 }
4553
4554 return receive_encrypted_standard(server, mids, bufs, num_mids);
4555 }
4556
4557 int
4558 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
4559 {
4560 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
4561
4562 return handle_read_data(server, mid, buf, server->pdu_size,
4563 NULL, 0, 0);
4564 }
4565
4566 static int
4567 smb2_next_header(char *buf)
4568 {
4569 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
4570 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
4571
4572 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
4573 return sizeof(struct smb2_transform_hdr) +
4574 le32_to_cpu(t_hdr->OriginalMessageSize);
4575
4576 return le32_to_cpu(hdr->NextCommand);
4577 }
4578
4579 static int
4580 smb2_make_node(unsigned int xid, struct inode *inode,
4581 struct dentry *dentry, struct cifs_tcon *tcon,
4582 char *full_path, umode_t mode, dev_t dev)
4583 {
4584 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4585 int rc = -EPERM;
4586 FILE_ALL_INFO *buf = NULL;
4587 struct cifs_io_parms io_parms;
4588 __u32 oplock = 0;
4589 struct cifs_fid fid;
4590 struct cifs_open_parms oparms;
4591 unsigned int bytes_written;
4592 struct win_dev *pdev;
4593 struct kvec iov[2];
4594
4595 /*
4596 * Check if mounted with mount parm 'sfu' mount parm.
4597 * SFU emulation should work with all servers, but only
4598 * supports block and char device (no socket & fifo),
4599 * and was used by default in earlier versions of Windows
4600 */
4601 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
4602 goto out;
4603
4604 /*
4605 * TODO: Add ability to create instead via reparse point. Windows (e.g.
4606 * their current NFS server) uses this approach to expose special files
4607 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
4608 */
4609
4610 if (!S_ISCHR(mode) && !S_ISBLK(mode))
4611 goto out;
4612
4613 cifs_dbg(FYI, "sfu compat create special file\n");
4614
4615 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
4616 if (buf == NULL) {
4617 rc = -ENOMEM;
4618 goto out;
4619 }
4620
4621 oparms.tcon = tcon;
4622 oparms.cifs_sb = cifs_sb;
4623 oparms.desired_access = GENERIC_WRITE;
4624 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
4625 CREATE_OPTION_SPECIAL);
4626 oparms.disposition = FILE_CREATE;
4627 oparms.path = full_path;
4628 oparms.fid = &fid;
4629 oparms.reconnect = false;
4630
4631 if (tcon->ses->server->oplocks)
4632 oplock = REQ_OPLOCK;
4633 else
4634 oplock = 0;
4635 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
4636 if (rc)
4637 goto out;
4638
4639 /*
4640 * BB Do not bother to decode buf since no local inode yet to put
4641 * timestamps in, but we can reuse it safely.
4642 */
4643
4644 pdev = (struct win_dev *)buf;
4645 io_parms.pid = current->tgid;
4646 io_parms.tcon = tcon;
4647 io_parms.offset = 0;
4648 io_parms.length = sizeof(struct win_dev);
4649 iov[1].iov_base = buf;
4650 iov[1].iov_len = sizeof(struct win_dev);
4651 if (S_ISCHR(mode)) {
4652 memcpy(pdev->type, "IntxCHR", 8);
4653 pdev->major = cpu_to_le64(MAJOR(dev));
4654 pdev->minor = cpu_to_le64(MINOR(dev));
4655 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4656 &bytes_written, iov, 1);
4657 } else if (S_ISBLK(mode)) {
4658 memcpy(pdev->type, "IntxBLK", 8);
4659 pdev->major = cpu_to_le64(MAJOR(dev));
4660 pdev->minor = cpu_to_le64(MINOR(dev));
4661 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4662 &bytes_written, iov, 1);
4663 }
4664 tcon->ses->server->ops->close(xid, tcon, &fid);
4665 d_drop(dentry);
4666
4667 /* FIXME: add code here to set EAs */
4668 out:
4669 kfree(buf);
4670 return rc;
4671 }
4672
4673
4674 struct smb_version_operations smb20_operations = {
4675 .compare_fids = smb2_compare_fids,
4676 .setup_request = smb2_setup_request,
4677 .setup_async_request = smb2_setup_async_request,
4678 .check_receive = smb2_check_receive,
4679 .add_credits = smb2_add_credits,
4680 .set_credits = smb2_set_credits,
4681 .get_credits_field = smb2_get_credits_field,
4682 .get_credits = smb2_get_credits,
4683 .wait_mtu_credits = cifs_wait_mtu_credits,
4684 .get_next_mid = smb2_get_next_mid,
4685 .revert_current_mid = smb2_revert_current_mid,
4686 .read_data_offset = smb2_read_data_offset,
4687 .read_data_length = smb2_read_data_length,
4688 .map_error = map_smb2_to_linux_error,
4689 .find_mid = smb2_find_mid,
4690 .check_message = smb2_check_message,
4691 .dump_detail = smb2_dump_detail,
4692 .clear_stats = smb2_clear_stats,
4693 .print_stats = smb2_print_stats,
4694 .is_oplock_break = smb2_is_valid_oplock_break,
4695 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4696 .downgrade_oplock = smb2_downgrade_oplock,
4697 .need_neg = smb2_need_neg,
4698 .negotiate = smb2_negotiate,
4699 .negotiate_wsize = smb2_negotiate_wsize,
4700 .negotiate_rsize = smb2_negotiate_rsize,
4701 .sess_setup = SMB2_sess_setup,
4702 .logoff = SMB2_logoff,
4703 .tree_connect = SMB2_tcon,
4704 .tree_disconnect = SMB2_tdis,
4705 .qfs_tcon = smb2_qfs_tcon,
4706 .is_path_accessible = smb2_is_path_accessible,
4707 .can_echo = smb2_can_echo,
4708 .echo = SMB2_echo,
4709 .query_path_info = smb2_query_path_info,
4710 .get_srv_inum = smb2_get_srv_inum,
4711 .query_file_info = smb2_query_file_info,
4712 .set_path_size = smb2_set_path_size,
4713 .set_file_size = smb2_set_file_size,
4714 .set_file_info = smb2_set_file_info,
4715 .set_compression = smb2_set_compression,
4716 .mkdir = smb2_mkdir,
4717 .mkdir_setinfo = smb2_mkdir_setinfo,
4718 .rmdir = smb2_rmdir,
4719 .unlink = smb2_unlink,
4720 .rename = smb2_rename_path,
4721 .create_hardlink = smb2_create_hardlink,
4722 .query_symlink = smb2_query_symlink,
4723 .query_mf_symlink = smb3_query_mf_symlink,
4724 .create_mf_symlink = smb3_create_mf_symlink,
4725 .open = smb2_open_file,
4726 .set_fid = smb2_set_fid,
4727 .close = smb2_close_file,
4728 .flush = smb2_flush_file,
4729 .async_readv = smb2_async_readv,
4730 .async_writev = smb2_async_writev,
4731 .sync_read = smb2_sync_read,
4732 .sync_write = smb2_sync_write,
4733 .query_dir_first = smb2_query_dir_first,
4734 .query_dir_next = smb2_query_dir_next,
4735 .close_dir = smb2_close_dir,
4736 .calc_smb_size = smb2_calc_size,
4737 .is_status_pending = smb2_is_status_pending,
4738 .is_session_expired = smb2_is_session_expired,
4739 .oplock_response = smb2_oplock_response,
4740 .queryfs = smb2_queryfs,
4741 .mand_lock = smb2_mand_lock,
4742 .mand_unlock_range = smb2_unlock_range,
4743 .push_mand_locks = smb2_push_mandatory_locks,
4744 .get_lease_key = smb2_get_lease_key,
4745 .set_lease_key = smb2_set_lease_key,
4746 .new_lease_key = smb2_new_lease_key,
4747 .calc_signature = smb2_calc_signature,
4748 .is_read_op = smb2_is_read_op,
4749 .set_oplock_level = smb2_set_oplock_level,
4750 .create_lease_buf = smb2_create_lease_buf,
4751 .parse_lease_buf = smb2_parse_lease_buf,
4752 .copychunk_range = smb2_copychunk_range,
4753 .wp_retry_size = smb2_wp_retry_size,
4754 .dir_needs_close = smb2_dir_needs_close,
4755 .get_dfs_refer = smb2_get_dfs_refer,
4756 .select_sectype = smb2_select_sectype,
4757 #ifdef CONFIG_CIFS_XATTR
4758 .query_all_EAs = smb2_query_eas,
4759 .set_EA = smb2_set_ea,
4760 #endif /* CIFS_XATTR */
4761 .get_acl = get_smb2_acl,
4762 .get_acl_by_fid = get_smb2_acl_by_fid,
4763 .set_acl = set_smb2_acl,
4764 .next_header = smb2_next_header,
4765 .ioctl_query_info = smb2_ioctl_query_info,
4766 .make_node = smb2_make_node,
4767 .fiemap = smb3_fiemap,
4768 .llseek = smb3_llseek,
4769 };
4770
4771 struct smb_version_operations smb21_operations = {
4772 .compare_fids = smb2_compare_fids,
4773 .setup_request = smb2_setup_request,
4774 .setup_async_request = smb2_setup_async_request,
4775 .check_receive = smb2_check_receive,
4776 .add_credits = smb2_add_credits,
4777 .set_credits = smb2_set_credits,
4778 .get_credits_field = smb2_get_credits_field,
4779 .get_credits = smb2_get_credits,
4780 .wait_mtu_credits = smb2_wait_mtu_credits,
4781 .adjust_credits = smb2_adjust_credits,
4782 .get_next_mid = smb2_get_next_mid,
4783 .revert_current_mid = smb2_revert_current_mid,
4784 .read_data_offset = smb2_read_data_offset,
4785 .read_data_length = smb2_read_data_length,
4786 .map_error = map_smb2_to_linux_error,
4787 .find_mid = smb2_find_mid,
4788 .check_message = smb2_check_message,
4789 .dump_detail = smb2_dump_detail,
4790 .clear_stats = smb2_clear_stats,
4791 .print_stats = smb2_print_stats,
4792 .is_oplock_break = smb2_is_valid_oplock_break,
4793 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4794 .downgrade_oplock = smb2_downgrade_oplock,
4795 .need_neg = smb2_need_neg,
4796 .negotiate = smb2_negotiate,
4797 .negotiate_wsize = smb2_negotiate_wsize,
4798 .negotiate_rsize = smb2_negotiate_rsize,
4799 .sess_setup = SMB2_sess_setup,
4800 .logoff = SMB2_logoff,
4801 .tree_connect = SMB2_tcon,
4802 .tree_disconnect = SMB2_tdis,
4803 .qfs_tcon = smb2_qfs_tcon,
4804 .is_path_accessible = smb2_is_path_accessible,
4805 .can_echo = smb2_can_echo,
4806 .echo = SMB2_echo,
4807 .query_path_info = smb2_query_path_info,
4808 .get_srv_inum = smb2_get_srv_inum,
4809 .query_file_info = smb2_query_file_info,
4810 .set_path_size = smb2_set_path_size,
4811 .set_file_size = smb2_set_file_size,
4812 .set_file_info = smb2_set_file_info,
4813 .set_compression = smb2_set_compression,
4814 .mkdir = smb2_mkdir,
4815 .mkdir_setinfo = smb2_mkdir_setinfo,
4816 .rmdir = smb2_rmdir,
4817 .unlink = smb2_unlink,
4818 .rename = smb2_rename_path,
4819 .create_hardlink = smb2_create_hardlink,
4820 .query_symlink = smb2_query_symlink,
4821 .query_mf_symlink = smb3_query_mf_symlink,
4822 .create_mf_symlink = smb3_create_mf_symlink,
4823 .open = smb2_open_file,
4824 .set_fid = smb2_set_fid,
4825 .close = smb2_close_file,
4826 .flush = smb2_flush_file,
4827 .async_readv = smb2_async_readv,
4828 .async_writev = smb2_async_writev,
4829 .sync_read = smb2_sync_read,
4830 .sync_write = smb2_sync_write,
4831 .query_dir_first = smb2_query_dir_first,
4832 .query_dir_next = smb2_query_dir_next,
4833 .close_dir = smb2_close_dir,
4834 .calc_smb_size = smb2_calc_size,
4835 .is_status_pending = smb2_is_status_pending,
4836 .is_session_expired = smb2_is_session_expired,
4837 .oplock_response = smb2_oplock_response,
4838 .queryfs = smb2_queryfs,
4839 .mand_lock = smb2_mand_lock,
4840 .mand_unlock_range = smb2_unlock_range,
4841 .push_mand_locks = smb2_push_mandatory_locks,
4842 .get_lease_key = smb2_get_lease_key,
4843 .set_lease_key = smb2_set_lease_key,
4844 .new_lease_key = smb2_new_lease_key,
4845 .calc_signature = smb2_calc_signature,
4846 .is_read_op = smb21_is_read_op,
4847 .set_oplock_level = smb21_set_oplock_level,
4848 .create_lease_buf = smb2_create_lease_buf,
4849 .parse_lease_buf = smb2_parse_lease_buf,
4850 .copychunk_range = smb2_copychunk_range,
4851 .wp_retry_size = smb2_wp_retry_size,
4852 .dir_needs_close = smb2_dir_needs_close,
4853 .enum_snapshots = smb3_enum_snapshots,
4854 .notify = smb3_notify,
4855 .get_dfs_refer = smb2_get_dfs_refer,
4856 .select_sectype = smb2_select_sectype,
4857 #ifdef CONFIG_CIFS_XATTR
4858 .query_all_EAs = smb2_query_eas,
4859 .set_EA = smb2_set_ea,
4860 #endif /* CIFS_XATTR */
4861 .get_acl = get_smb2_acl,
4862 .get_acl_by_fid = get_smb2_acl_by_fid,
4863 .set_acl = set_smb2_acl,
4864 .next_header = smb2_next_header,
4865 .ioctl_query_info = smb2_ioctl_query_info,
4866 .make_node = smb2_make_node,
4867 .fiemap = smb3_fiemap,
4868 .llseek = smb3_llseek,
4869 };
4870
4871 struct smb_version_operations smb30_operations = {
4872 .compare_fids = smb2_compare_fids,
4873 .setup_request = smb2_setup_request,
4874 .setup_async_request = smb2_setup_async_request,
4875 .check_receive = smb2_check_receive,
4876 .add_credits = smb2_add_credits,
4877 .set_credits = smb2_set_credits,
4878 .get_credits_field = smb2_get_credits_field,
4879 .get_credits = smb2_get_credits,
4880 .wait_mtu_credits = smb2_wait_mtu_credits,
4881 .adjust_credits = smb2_adjust_credits,
4882 .get_next_mid = smb2_get_next_mid,
4883 .revert_current_mid = smb2_revert_current_mid,
4884 .read_data_offset = smb2_read_data_offset,
4885 .read_data_length = smb2_read_data_length,
4886 .map_error = map_smb2_to_linux_error,
4887 .find_mid = smb2_find_mid,
4888 .check_message = smb2_check_message,
4889 .dump_detail = smb2_dump_detail,
4890 .clear_stats = smb2_clear_stats,
4891 .print_stats = smb2_print_stats,
4892 .dump_share_caps = smb2_dump_share_caps,
4893 .is_oplock_break = smb2_is_valid_oplock_break,
4894 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4895 .downgrade_oplock = smb3_downgrade_oplock,
4896 .need_neg = smb2_need_neg,
4897 .negotiate = smb2_negotiate,
4898 .negotiate_wsize = smb3_negotiate_wsize,
4899 .negotiate_rsize = smb3_negotiate_rsize,
4900 .sess_setup = SMB2_sess_setup,
4901 .logoff = SMB2_logoff,
4902 .tree_connect = SMB2_tcon,
4903 .tree_disconnect = SMB2_tdis,
4904 .qfs_tcon = smb3_qfs_tcon,
4905 .is_path_accessible = smb2_is_path_accessible,
4906 .can_echo = smb2_can_echo,
4907 .echo = SMB2_echo,
4908 .query_path_info = smb2_query_path_info,
4909 .get_srv_inum = smb2_get_srv_inum,
4910 .query_file_info = smb2_query_file_info,
4911 .set_path_size = smb2_set_path_size,
4912 .set_file_size = smb2_set_file_size,
4913 .set_file_info = smb2_set_file_info,
4914 .set_compression = smb2_set_compression,
4915 .mkdir = smb2_mkdir,
4916 .mkdir_setinfo = smb2_mkdir_setinfo,
4917 .rmdir = smb2_rmdir,
4918 .unlink = smb2_unlink,
4919 .rename = smb2_rename_path,
4920 .create_hardlink = smb2_create_hardlink,
4921 .query_symlink = smb2_query_symlink,
4922 .query_mf_symlink = smb3_query_mf_symlink,
4923 .create_mf_symlink = smb3_create_mf_symlink,
4924 .open = smb2_open_file,
4925 .set_fid = smb2_set_fid,
4926 .close = smb2_close_file,
4927 .close_getattr = smb2_close_getattr,
4928 .flush = smb2_flush_file,
4929 .async_readv = smb2_async_readv,
4930 .async_writev = smb2_async_writev,
4931 .sync_read = smb2_sync_read,
4932 .sync_write = smb2_sync_write,
4933 .query_dir_first = smb2_query_dir_first,
4934 .query_dir_next = smb2_query_dir_next,
4935 .close_dir = smb2_close_dir,
4936 .calc_smb_size = smb2_calc_size,
4937 .is_status_pending = smb2_is_status_pending,
4938 .is_session_expired = smb2_is_session_expired,
4939 .oplock_response = smb2_oplock_response,
4940 .queryfs = smb2_queryfs,
4941 .mand_lock = smb2_mand_lock,
4942 .mand_unlock_range = smb2_unlock_range,
4943 .push_mand_locks = smb2_push_mandatory_locks,
4944 .get_lease_key = smb2_get_lease_key,
4945 .set_lease_key = smb2_set_lease_key,
4946 .new_lease_key = smb2_new_lease_key,
4947 .generate_signingkey = generate_smb30signingkey,
4948 .calc_signature = smb3_calc_signature,
4949 .set_integrity = smb3_set_integrity,
4950 .is_read_op = smb21_is_read_op,
4951 .set_oplock_level = smb3_set_oplock_level,
4952 .create_lease_buf = smb3_create_lease_buf,
4953 .parse_lease_buf = smb3_parse_lease_buf,
4954 .copychunk_range = smb2_copychunk_range,
4955 .duplicate_extents = smb2_duplicate_extents,
4956 .validate_negotiate = smb3_validate_negotiate,
4957 .wp_retry_size = smb2_wp_retry_size,
4958 .dir_needs_close = smb2_dir_needs_close,
4959 .fallocate = smb3_fallocate,
4960 .enum_snapshots = smb3_enum_snapshots,
4961 .notify = smb3_notify,
4962 .init_transform_rq = smb3_init_transform_rq,
4963 .is_transform_hdr = smb3_is_transform_hdr,
4964 .receive_transform = smb3_receive_transform,
4965 .get_dfs_refer = smb2_get_dfs_refer,
4966 .select_sectype = smb2_select_sectype,
4967 #ifdef CONFIG_CIFS_XATTR
4968 .query_all_EAs = smb2_query_eas,
4969 .set_EA = smb2_set_ea,
4970 #endif /* CIFS_XATTR */
4971 .get_acl = get_smb2_acl,
4972 .get_acl_by_fid = get_smb2_acl_by_fid,
4973 .set_acl = set_smb2_acl,
4974 .next_header = smb2_next_header,
4975 .ioctl_query_info = smb2_ioctl_query_info,
4976 .make_node = smb2_make_node,
4977 .fiemap = smb3_fiemap,
4978 .llseek = smb3_llseek,
4979 };
4980
4981 struct smb_version_operations smb311_operations = {
4982 .compare_fids = smb2_compare_fids,
4983 .setup_request = smb2_setup_request,
4984 .setup_async_request = smb2_setup_async_request,
4985 .check_receive = smb2_check_receive,
4986 .add_credits = smb2_add_credits,
4987 .set_credits = smb2_set_credits,
4988 .get_credits_field = smb2_get_credits_field,
4989 .get_credits = smb2_get_credits,
4990 .wait_mtu_credits = smb2_wait_mtu_credits,
4991 .adjust_credits = smb2_adjust_credits,
4992 .get_next_mid = smb2_get_next_mid,
4993 .revert_current_mid = smb2_revert_current_mid,
4994 .read_data_offset = smb2_read_data_offset,
4995 .read_data_length = smb2_read_data_length,
4996 .map_error = map_smb2_to_linux_error,
4997 .find_mid = smb2_find_mid,
4998 .check_message = smb2_check_message,
4999 .dump_detail = smb2_dump_detail,
5000 .clear_stats = smb2_clear_stats,
5001 .print_stats = smb2_print_stats,
5002 .dump_share_caps = smb2_dump_share_caps,
5003 .is_oplock_break = smb2_is_valid_oplock_break,
5004 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5005 .downgrade_oplock = smb3_downgrade_oplock,
5006 .need_neg = smb2_need_neg,
5007 .negotiate = smb2_negotiate,
5008 .negotiate_wsize = smb3_negotiate_wsize,
5009 .negotiate_rsize = smb3_negotiate_rsize,
5010 .sess_setup = SMB2_sess_setup,
5011 .logoff = SMB2_logoff,
5012 .tree_connect = SMB2_tcon,
5013 .tree_disconnect = SMB2_tdis,
5014 .qfs_tcon = smb3_qfs_tcon,
5015 .is_path_accessible = smb2_is_path_accessible,
5016 .can_echo = smb2_can_echo,
5017 .echo = SMB2_echo,
5018 .query_path_info = smb2_query_path_info,
5019 .get_srv_inum = smb2_get_srv_inum,
5020 .query_file_info = smb2_query_file_info,
5021 .set_path_size = smb2_set_path_size,
5022 .set_file_size = smb2_set_file_size,
5023 .set_file_info = smb2_set_file_info,
5024 .set_compression = smb2_set_compression,
5025 .mkdir = smb2_mkdir,
5026 .mkdir_setinfo = smb2_mkdir_setinfo,
5027 .posix_mkdir = smb311_posix_mkdir,
5028 .rmdir = smb2_rmdir,
5029 .unlink = smb2_unlink,
5030 .rename = smb2_rename_path,
5031 .create_hardlink = smb2_create_hardlink,
5032 .query_symlink = smb2_query_symlink,
5033 .query_mf_symlink = smb3_query_mf_symlink,
5034 .create_mf_symlink = smb3_create_mf_symlink,
5035 .open = smb2_open_file,
5036 .set_fid = smb2_set_fid,
5037 .close = smb2_close_file,
5038 .close_getattr = smb2_close_getattr,
5039 .flush = smb2_flush_file,
5040 .async_readv = smb2_async_readv,
5041 .async_writev = smb2_async_writev,
5042 .sync_read = smb2_sync_read,
5043 .sync_write = smb2_sync_write,
5044 .query_dir_first = smb2_query_dir_first,
5045 .query_dir_next = smb2_query_dir_next,
5046 .close_dir = smb2_close_dir,
5047 .calc_smb_size = smb2_calc_size,
5048 .is_status_pending = smb2_is_status_pending,
5049 .is_session_expired = smb2_is_session_expired,
5050 .oplock_response = smb2_oplock_response,
5051 .queryfs = smb311_queryfs,
5052 .mand_lock = smb2_mand_lock,
5053 .mand_unlock_range = smb2_unlock_range,
5054 .push_mand_locks = smb2_push_mandatory_locks,
5055 .get_lease_key = smb2_get_lease_key,
5056 .set_lease_key = smb2_set_lease_key,
5057 .new_lease_key = smb2_new_lease_key,
5058 .generate_signingkey = generate_smb311signingkey,
5059 .calc_signature = smb3_calc_signature,
5060 .set_integrity = smb3_set_integrity,
5061 .is_read_op = smb21_is_read_op,
5062 .set_oplock_level = smb3_set_oplock_level,
5063 .create_lease_buf = smb3_create_lease_buf,
5064 .parse_lease_buf = smb3_parse_lease_buf,
5065 .copychunk_range = smb2_copychunk_range,
5066 .duplicate_extents = smb2_duplicate_extents,
5067 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5068 .wp_retry_size = smb2_wp_retry_size,
5069 .dir_needs_close = smb2_dir_needs_close,
5070 .fallocate = smb3_fallocate,
5071 .enum_snapshots = smb3_enum_snapshots,
5072 .notify = smb3_notify,
5073 .init_transform_rq = smb3_init_transform_rq,
5074 .is_transform_hdr = smb3_is_transform_hdr,
5075 .receive_transform = smb3_receive_transform,
5076 .get_dfs_refer = smb2_get_dfs_refer,
5077 .select_sectype = smb2_select_sectype,
5078 #ifdef CONFIG_CIFS_XATTR
5079 .query_all_EAs = smb2_query_eas,
5080 .set_EA = smb2_set_ea,
5081 #endif /* CIFS_XATTR */
5082 .get_acl = get_smb2_acl,
5083 .get_acl_by_fid = get_smb2_acl_by_fid,
5084 .set_acl = set_smb2_acl,
5085 .next_header = smb2_next_header,
5086 .ioctl_query_info = smb2_ioctl_query_info,
5087 .make_node = smb2_make_node,
5088 .fiemap = smb3_fiemap,
5089 .llseek = smb3_llseek,
5090 };
5091
5092 struct smb_version_values smb20_values = {
5093 .version_string = SMB20_VERSION_STRING,
5094 .protocol_id = SMB20_PROT_ID,
5095 .req_capabilities = 0, /* MBZ */
5096 .large_lock_type = 0,
5097 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5098 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5099 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5100 .header_size = sizeof(struct smb2_sync_hdr),
5101 .header_preamble_size = 0,
5102 .max_header_size = MAX_SMB2_HDR_SIZE,
5103 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5104 .lock_cmd = SMB2_LOCK,
5105 .cap_unix = 0,
5106 .cap_nt_find = SMB2_NT_FIND,
5107 .cap_large_files = SMB2_LARGE_FILES,
5108 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5109 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5110 .create_lease_size = sizeof(struct create_lease),
5111 };
5112
5113 struct smb_version_values smb21_values = {
5114 .version_string = SMB21_VERSION_STRING,
5115 .protocol_id = SMB21_PROT_ID,
5116 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5117 .large_lock_type = 0,
5118 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5119 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5120 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5121 .header_size = sizeof(struct smb2_sync_hdr),
5122 .header_preamble_size = 0,
5123 .max_header_size = MAX_SMB2_HDR_SIZE,
5124 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5125 .lock_cmd = SMB2_LOCK,
5126 .cap_unix = 0,
5127 .cap_nt_find = SMB2_NT_FIND,
5128 .cap_large_files = SMB2_LARGE_FILES,
5129 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5130 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5131 .create_lease_size = sizeof(struct create_lease),
5132 };
5133
5134 struct smb_version_values smb3any_values = {
5135 .version_string = SMB3ANY_VERSION_STRING,
5136 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5137 .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,
5138 .large_lock_type = 0,
5139 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5140 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5141 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5142 .header_size = sizeof(struct smb2_sync_hdr),
5143 .header_preamble_size = 0,
5144 .max_header_size = MAX_SMB2_HDR_SIZE,
5145 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5146 .lock_cmd = SMB2_LOCK,
5147 .cap_unix = 0,
5148 .cap_nt_find = SMB2_NT_FIND,
5149 .cap_large_files = SMB2_LARGE_FILES,
5150 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5151 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5152 .create_lease_size = sizeof(struct create_lease_v2),
5153 };
5154
5155 struct smb_version_values smbdefault_values = {
5156 .version_string = SMBDEFAULT_VERSION_STRING,
5157 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5158 .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,
5159 .large_lock_type = 0,
5160 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5161 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5162 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5163 .header_size = sizeof(struct smb2_sync_hdr),
5164 .header_preamble_size = 0,
5165 .max_header_size = MAX_SMB2_HDR_SIZE,
5166 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5167 .lock_cmd = SMB2_LOCK,
5168 .cap_unix = 0,
5169 .cap_nt_find = SMB2_NT_FIND,
5170 .cap_large_files = SMB2_LARGE_FILES,
5171 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5172 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5173 .create_lease_size = sizeof(struct create_lease_v2),
5174 };
5175
5176 struct smb_version_values smb30_values = {
5177 .version_string = SMB30_VERSION_STRING,
5178 .protocol_id = SMB30_PROT_ID,
5179 .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,
5180 .large_lock_type = 0,
5181 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5182 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5183 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5184 .header_size = sizeof(struct smb2_sync_hdr),
5185 .header_preamble_size = 0,
5186 .max_header_size = MAX_SMB2_HDR_SIZE,
5187 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5188 .lock_cmd = SMB2_LOCK,
5189 .cap_unix = 0,
5190 .cap_nt_find = SMB2_NT_FIND,
5191 .cap_large_files = SMB2_LARGE_FILES,
5192 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5193 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5194 .create_lease_size = sizeof(struct create_lease_v2),
5195 };
5196
5197 struct smb_version_values smb302_values = {
5198 .version_string = SMB302_VERSION_STRING,
5199 .protocol_id = SMB302_PROT_ID,
5200 .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,
5201 .large_lock_type = 0,
5202 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5203 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5204 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5205 .header_size = sizeof(struct smb2_sync_hdr),
5206 .header_preamble_size = 0,
5207 .max_header_size = MAX_SMB2_HDR_SIZE,
5208 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5209 .lock_cmd = SMB2_LOCK,
5210 .cap_unix = 0,
5211 .cap_nt_find = SMB2_NT_FIND,
5212 .cap_large_files = SMB2_LARGE_FILES,
5213 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5214 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5215 .create_lease_size = sizeof(struct create_lease_v2),
5216 };
5217
5218 struct smb_version_values smb311_values = {
5219 .version_string = SMB311_VERSION_STRING,
5220 .protocol_id = SMB311_PROT_ID,
5221 .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,
5222 .large_lock_type = 0,
5223 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5224 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5225 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5226 .header_size = sizeof(struct smb2_sync_hdr),
5227 .header_preamble_size = 0,
5228 .max_header_size = MAX_SMB2_HDR_SIZE,
5229 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5230 .lock_cmd = SMB2_LOCK,
5231 .cap_unix = 0,
5232 .cap_nt_find = SMB2_NT_FIND,
5233 .cap_large_files = SMB2_LARGE_FILES,
5234 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5235 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5236 .create_lease_size = sizeof(struct create_lease_v2),
5237 };