gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / fs / cifs / smb2ops.c
blobb36c46f48705c7cc6c46683313b6b02505fd3ebb
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * SMB2 version specific operations
5 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
6 */
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"
27 /* Change credits for different ops and return the total number of credits */
28 static int
29 change_conf(struct TCP_Server_Info *server)
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;
53 server->echo_credits = 1;
55 server->credits -= server->echo_credits + server->oplock_credits;
56 return server->credits + server->echo_credits + server->oplock_credits;
59 static void
60 smb2_add_credits(struct TCP_Server_Info *server,
61 const struct cifs_credits *credits, const int optype)
63 int *val, rc = -1;
64 unsigned int add = credits->value;
65 unsigned int instance = credits->instance;
66 bool reconnect_detected = false;
68 spin_lock(&server->req_lock);
69 val = server->ops->get_credits_field(server, optype);
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;
80 if (*val > 65000) {
81 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
82 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
84 server->in_flight--;
85 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
86 rc = change_conf(server);
88 * Sometimes server returns 0 credits on oplock break ack - we need to
89 * rebalance credits in this case.
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++;
98 spin_unlock(&server->req_lock);
99 wake_up(&server->request_q);
101 if (reconnect_detected)
102 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
103 add, instance);
105 if (server->tcpStatus == CifsNeedReconnect
106 || server->tcpStatus == CifsExiting)
107 return;
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);
127 static void
128 smb2_set_credits(struct TCP_Server_Info *server, const int val)
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");
140 static int *
141 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
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;
153 static unsigned int
154 smb2_get_credits(struct mid_q_entry *mid)
156 return mid->credits_received;
159 static int
160 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
161 unsigned int *num, struct cifs_credits *credits)
163 int rc = 0;
164 unsigned int scredits;
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;
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;
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);
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;
207 spin_unlock(&server->req_lock);
208 return rc;
211 static int
212 smb2_adjust_credits(struct TCP_Server_Info *server,
213 struct cifs_credits *credits,
214 const unsigned int payload_size)
216 int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
218 if (!credits->value || credits->value == new_val)
219 return 0;
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;
227 spin_lock(&server->req_lock);
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;
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;
243 static __u64
244 smb2_get_next_mid(struct TCP_Server_Info *server)
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;
254 static void
255 smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
257 spin_lock(&GlobalMid_Lock);
258 if (server->CurrentMid >= val)
259 server->CurrentMid -= val;
260 spin_unlock(&GlobalMid_Lock);
263 static struct mid_q_entry *
264 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
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);
270 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
271 cifs_server_dbg(VFS, "Encrypted frame parsing not supported yet\n");
272 return NULL;
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;
285 spin_unlock(&GlobalMid_Lock);
286 return NULL;
289 static void
290 smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
292 #ifdef CONFIG_CIFS_DEBUG2
293 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
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
303 static bool
304 smb2_need_neg(struct TCP_Server_Info *server)
306 return server->max_read == 0;
309 static int
310 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
312 int rc;
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;
322 static unsigned int
323 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
325 struct TCP_Server_Info *server = tcon->ses->server;
326 unsigned int wsize;
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);
334 return wsize;
337 static unsigned int
338 smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
340 struct TCP_Server_Info *server = tcon->ses->server;
341 unsigned int wsize;
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)
350 * Account for SMB2 data transfer packet header and
351 * possible encryption header
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);
362 #endif
363 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
364 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
366 return wsize;
369 static unsigned int
370 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
372 struct TCP_Server_Info *server = tcon->ses->server;
373 unsigned int rsize;
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);
379 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
380 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
382 return rsize;
385 static unsigned int
386 smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
388 struct TCP_Server_Info *server = tcon->ses->server;
389 unsigned int rsize;
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)
398 * Account for SMB2 data transfer packet header and
399 * possible encryption header
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);
410 #endif
412 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
413 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
415 return rsize;
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)
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;
435 *iface_list = NULL;
436 *iface_count = 0;
439 * Fist pass: count and sanity check
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;
451 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
452 bytes_left -= next;
455 if (!nb_iface) {
456 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
457 rc = -EINVAL;
458 goto out;
461 if (bytes_left || p->Next)
462 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
466 * Second pass: extract info to internal structure
469 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
470 if (!*iface_list) {
471 rc = -ENOMEM;
472 goto out;
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);
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));
488 switch (p->Family) {
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.
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);
500 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
501 addr4->sin_port = cpu_to_be16(CIFS_PORT);
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);
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);
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;
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;
537 if (!*iface_count) {
538 rc = -EINVAL;
539 goto out;
542 out:
543 if (rc) {
544 kfree(*iface_list);
545 *iface_count = 0;
546 *iface_list = NULL;
548 return rc;
551 static int compare_iface(const void *ia, const void *ib)
553 const struct cifs_server_iface *a = (struct cifs_server_iface *)ia;
554 const struct cifs_server_iface *b = (struct cifs_server_iface *)ib;
556 return a->speed == b->speed ? 0 : (a->speed > b->speed ? -1 : 1);
559 static int
560 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
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;
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;
582 rc = parse_server_interfaces(out_buf, ret_data_len,
583 &iface_list, &iface_count);
584 if (rc)
585 goto out;
587 /* sort interfaces from fastest to slowest */
588 sort(iface_list, iface_count, sizeof(*iface_list), compare_iface, NULL);
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);
597 out:
598 kfree(out_buf);
599 return rc;
602 static void
603 smb2_close_cached_fid(struct kref *ref)
605 struct cached_fid *cfid = container_of(ref, struct cached_fid,
606 refcount);
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;
618 void close_shroot(struct cached_fid *cfid)
620 mutex_lock(&cfid->fid_mutex);
621 kref_put(&cfid->refcount, smb2_close_cached_fid);
622 mutex_unlock(&cfid->fid_mutex);
625 void close_shroot_lease_locked(struct cached_fid *cfid)
627 if (cfid->has_lease) {
628 cfid->has_lease = false;
629 kref_put(&cfid->refcount, smb2_close_cached_fid);
633 void close_shroot_lease(struct cached_fid *cfid)
635 mutex_lock(&cfid->fid_mutex);
636 close_shroot_lease_locked(cfid);
637 mutex_unlock(&cfid->fid_mutex);
640 void
641 smb2_cached_lease_break(struct work_struct *work)
643 struct cached_fid *cfid = container_of(work,
644 struct cached_fid, lease_break);
646 close_shroot_lease(cfid);
650 * Open the directory at the root of a share
652 int open_shroot(unsigned int xid, struct cifs_tcon *tcon,
653 struct cifs_sb_info *cifs_sb, struct cifs_fid *pfid)
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;
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;
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
685 mutex_unlock(&tcon->crfid.fid_mutex);
687 if (smb3_encryption_required(tcon))
688 flags |= CIFS_TRANSFORM_REQ;
690 memset(rqst, 0, sizeof(rqst));
691 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
692 memset(rsp_iov, 0, sizeof(rsp_iov));
694 /* Open */
695 memset(&open_iov, 0, sizeof(open_iov));
696 rqst[0].rq_iov = open_iov;
697 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
699 oparms.tcon = tcon;
700 oparms.create_options = cifs_create_options(cifs_sb, 0);
701 oparms.desired_access = FILE_READ_ATTRIBUTES;
702 oparms.disposition = FILE_OPEN;
703 oparms.fid = pfid;
704 oparms.reconnect = false;
706 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, &utf16_path);
707 if (rc)
708 goto oshr_free;
709 smb2_set_next_command(tcon, &rqst[0]);
711 memset(&qi_iov, 0, sizeof(qi_iov));
712 rqst[1].rq_iov = qi_iov;
713 rqst[1].rq_nvec = 1;
715 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
716 COMPOUND_FID, FILE_ALL_INFORMATION,
717 SMB2_O_INFO_FILE, 0,
718 sizeof(struct smb2_file_all_info) +
719 PATH_MAX * 2, 0, NULL);
720 if (rc)
721 goto oshr_free;
723 smb2_set_related(&rqst[1]);
725 rc = compound_send_recv(xid, ses, flags, 2, rqst,
726 resp_buftype, rsp_iov);
727 mutex_lock(&tcon->crfid.fid_mutex);
730 * Now we need to check again as the cached root might have
731 * been successfully re-opened from a concurrent process
734 if (tcon->crfid.is_valid) {
735 /* work was already done */
737 /* stash fids for close() later */
738 struct cifs_fid fid = {
739 .persistent_fid = pfid->persistent_fid,
740 .volatile_fid = pfid->volatile_fid,
744 * caller expects this func to set pfid to a valid
745 * cached root, so we copy the existing one and get a
746 * reference.
748 memcpy(pfid, tcon->crfid.fid, sizeof(*pfid));
749 kref_get(&tcon->crfid.refcount);
751 mutex_unlock(&tcon->crfid.fid_mutex);
753 if (rc == 0) {
754 /* close extra handle outside of crit sec */
755 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
757 goto oshr_free;
760 /* Cached root is still invalid, continue normaly */
762 if (rc) {
763 if (rc == -EREMCHG) {
764 tcon->need_reconnect = true;
765 printk_once(KERN_WARNING "server share %s deleted\n",
766 tcon->treeName);
768 goto oshr_exit;
771 atomic_inc(&tcon->num_remote_opens);
773 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
774 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
775 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
776 #ifdef CONFIG_CIFS_DEBUG2
777 oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
778 #endif /* CIFS_DEBUG2 */
780 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
781 tcon->crfid.tcon = tcon;
782 tcon->crfid.is_valid = true;
783 kref_init(&tcon->crfid.refcount);
785 /* BB TBD check to see if oplock level check can be removed below */
786 if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
787 kref_get(&tcon->crfid.refcount);
788 tcon->crfid.has_lease = true;
789 smb2_parse_contexts(server, o_rsp,
790 &oparms.fid->epoch,
791 oparms.fid->lease_key, &oplock,
792 NULL, NULL);
793 } else
794 goto oshr_exit;
796 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
797 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
798 goto oshr_exit;
799 if (!smb2_validate_and_copy_iov(
800 le16_to_cpu(qi_rsp->OutputBufferOffset),
801 sizeof(struct smb2_file_all_info),
802 &rsp_iov[1], sizeof(struct smb2_file_all_info),
803 (char *)&tcon->crfid.file_all_info))
804 tcon->crfid.file_all_info_is_valid = true;
806 oshr_exit:
807 mutex_unlock(&tcon->crfid.fid_mutex);
808 oshr_free:
809 SMB2_open_free(&rqst[0]);
810 SMB2_query_info_free(&rqst[1]);
811 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
812 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
813 return rc;
816 static void
817 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
818 struct cifs_sb_info *cifs_sb)
820 int rc;
821 __le16 srch_path = 0; /* Null - open root of share */
822 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
823 struct cifs_open_parms oparms;
824 struct cifs_fid fid;
825 bool no_cached_open = tcon->nohandlecache;
827 oparms.tcon = tcon;
828 oparms.desired_access = FILE_READ_ATTRIBUTES;
829 oparms.disposition = FILE_OPEN;
830 oparms.create_options = cifs_create_options(cifs_sb, 0);
831 oparms.fid = &fid;
832 oparms.reconnect = false;
834 if (no_cached_open)
835 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
836 NULL, NULL);
837 else
838 rc = open_shroot(xid, tcon, cifs_sb, &fid);
840 if (rc)
841 return;
843 SMB3_request_interfaces(xid, tcon);
845 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
846 FS_ATTRIBUTE_INFORMATION);
847 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
848 FS_DEVICE_INFORMATION);
849 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
850 FS_VOLUME_INFORMATION);
851 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
852 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
853 if (no_cached_open)
854 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
855 else
856 close_shroot(&tcon->crfid);
859 static void
860 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
861 struct cifs_sb_info *cifs_sb)
863 int rc;
864 __le16 srch_path = 0; /* Null - open root of share */
865 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
866 struct cifs_open_parms oparms;
867 struct cifs_fid fid;
869 oparms.tcon = tcon;
870 oparms.desired_access = FILE_READ_ATTRIBUTES;
871 oparms.disposition = FILE_OPEN;
872 oparms.create_options = cifs_create_options(cifs_sb, 0);
873 oparms.fid = &fid;
874 oparms.reconnect = false;
876 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
877 NULL, NULL);
878 if (rc)
879 return;
881 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
882 FS_ATTRIBUTE_INFORMATION);
883 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
884 FS_DEVICE_INFORMATION);
885 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
888 static int
889 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
890 struct cifs_sb_info *cifs_sb, const char *full_path)
892 int rc;
893 __le16 *utf16_path;
894 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
895 struct cifs_open_parms oparms;
896 struct cifs_fid fid;
898 if ((*full_path == 0) && tcon->crfid.is_valid)
899 return 0;
901 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
902 if (!utf16_path)
903 return -ENOMEM;
905 oparms.tcon = tcon;
906 oparms.desired_access = FILE_READ_ATTRIBUTES;
907 oparms.disposition = FILE_OPEN;
908 oparms.create_options = cifs_create_options(cifs_sb, 0);
909 oparms.fid = &fid;
910 oparms.reconnect = false;
912 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
913 NULL);
914 if (rc) {
915 kfree(utf16_path);
916 return rc;
919 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
920 kfree(utf16_path);
921 return rc;
924 static int
925 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
926 struct cifs_sb_info *cifs_sb, const char *full_path,
927 u64 *uniqueid, FILE_ALL_INFO *data)
929 *uniqueid = le64_to_cpu(data->IndexNumber);
930 return 0;
933 static int
934 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
935 struct cifs_fid *fid, FILE_ALL_INFO *data)
937 int rc;
938 struct smb2_file_all_info *smb2_data;
940 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
941 GFP_KERNEL);
942 if (smb2_data == NULL)
943 return -ENOMEM;
945 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
946 smb2_data);
947 if (!rc)
948 move_smb2_info_to_cifs(data, smb2_data);
949 kfree(smb2_data);
950 return rc;
953 #ifdef CONFIG_CIFS_XATTR
954 static ssize_t
955 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
956 struct smb2_file_full_ea_info *src, size_t src_size,
957 const unsigned char *ea_name)
959 int rc = 0;
960 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
961 char *name, *value;
962 size_t buf_size = dst_size;
963 size_t name_len, value_len, user_name_len;
965 while (src_size > 0) {
966 name = &src->ea_data[0];
967 name_len = (size_t)src->ea_name_length;
968 value = &src->ea_data[src->ea_name_length + 1];
969 value_len = (size_t)le16_to_cpu(src->ea_value_length);
971 if (name_len == 0)
972 break;
974 if (src_size < 8 + name_len + 1 + value_len) {
975 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
976 rc = -EIO;
977 goto out;
980 if (ea_name) {
981 if (ea_name_len == name_len &&
982 memcmp(ea_name, name, name_len) == 0) {
983 rc = value_len;
984 if (dst_size == 0)
985 goto out;
986 if (dst_size < value_len) {
987 rc = -ERANGE;
988 goto out;
990 memcpy(dst, value, value_len);
991 goto out;
993 } else {
994 /* 'user.' plus a terminating null */
995 user_name_len = 5 + 1 + name_len;
997 if (buf_size == 0) {
998 /* skip copy - calc size only */
999 rc += user_name_len;
1000 } else if (dst_size >= user_name_len) {
1001 dst_size -= user_name_len;
1002 memcpy(dst, "user.", 5);
1003 dst += 5;
1004 memcpy(dst, src->ea_data, name_len);
1005 dst += name_len;
1006 *dst = 0;
1007 ++dst;
1008 rc += user_name_len;
1009 } else {
1010 /* stop before overrun buffer */
1011 rc = -ERANGE;
1012 break;
1016 if (!src->next_entry_offset)
1017 break;
1019 if (src_size < le32_to_cpu(src->next_entry_offset)) {
1020 /* stop before overrun buffer */
1021 rc = -ERANGE;
1022 break;
1024 src_size -= le32_to_cpu(src->next_entry_offset);
1025 src = (void *)((char *)src +
1026 le32_to_cpu(src->next_entry_offset));
1029 /* didn't find the named attribute */
1030 if (ea_name)
1031 rc = -ENODATA;
1033 out:
1034 return (ssize_t)rc;
1037 static ssize_t
1038 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1039 const unsigned char *path, const unsigned char *ea_name,
1040 char *ea_data, size_t buf_size,
1041 struct cifs_sb_info *cifs_sb)
1043 int rc;
1044 __le16 *utf16_path;
1045 struct kvec rsp_iov = {NULL, 0};
1046 int buftype = CIFS_NO_BUFFER;
1047 struct smb2_query_info_rsp *rsp;
1048 struct smb2_file_full_ea_info *info = NULL;
1050 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1051 if (!utf16_path)
1052 return -ENOMEM;
1054 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1055 FILE_READ_EA,
1056 FILE_FULL_EA_INFORMATION,
1057 SMB2_O_INFO_FILE,
1058 CIFSMaxBufSize -
1059 MAX_SMB2_CREATE_RESPONSE_SIZE -
1060 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1061 &rsp_iov, &buftype, cifs_sb);
1062 if (rc) {
1064 * If ea_name is NULL (listxattr) and there are no EAs,
1065 * return 0 as it's not an error. Otherwise, the specified
1066 * ea_name was not found.
1068 if (!ea_name && rc == -ENODATA)
1069 rc = 0;
1070 goto qeas_exit;
1073 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1074 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1075 le32_to_cpu(rsp->OutputBufferLength),
1076 &rsp_iov,
1077 sizeof(struct smb2_file_full_ea_info));
1078 if (rc)
1079 goto qeas_exit;
1081 info = (struct smb2_file_full_ea_info *)(
1082 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1083 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1084 le32_to_cpu(rsp->OutputBufferLength), ea_name);
1086 qeas_exit:
1087 kfree(utf16_path);
1088 free_rsp_buf(buftype, rsp_iov.iov_base);
1089 return rc;
1093 static int
1094 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1095 const char *path, const char *ea_name, const void *ea_value,
1096 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1097 struct cifs_sb_info *cifs_sb)
1099 struct cifs_ses *ses = tcon->ses;
1100 __le16 *utf16_path = NULL;
1101 int ea_name_len = strlen(ea_name);
1102 int flags = 0;
1103 int len;
1104 struct smb_rqst rqst[3];
1105 int resp_buftype[3];
1106 struct kvec rsp_iov[3];
1107 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1108 struct cifs_open_parms oparms;
1109 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1110 struct cifs_fid fid;
1111 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1112 unsigned int size[1];
1113 void *data[1];
1114 struct smb2_file_full_ea_info *ea = NULL;
1115 struct kvec close_iov[1];
1116 struct smb2_query_info_rsp *rsp;
1117 int rc, used_len = 0;
1119 if (smb3_encryption_required(tcon))
1120 flags |= CIFS_TRANSFORM_REQ;
1122 if (ea_name_len > 255)
1123 return -EINVAL;
1125 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1126 if (!utf16_path)
1127 return -ENOMEM;
1129 memset(rqst, 0, sizeof(rqst));
1130 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1131 memset(rsp_iov, 0, sizeof(rsp_iov));
1133 if (ses->server->ops->query_all_EAs) {
1134 if (!ea_value) {
1135 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1136 ea_name, NULL, 0,
1137 cifs_sb);
1138 if (rc == -ENODATA)
1139 goto sea_exit;
1140 } else {
1141 /* If we are adding a attribute we should first check
1142 * if there will be enough space available to store
1143 * the new EA. If not we should not add it since we
1144 * would not be able to even read the EAs back.
1146 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1147 FILE_READ_EA,
1148 FILE_FULL_EA_INFORMATION,
1149 SMB2_O_INFO_FILE,
1150 CIFSMaxBufSize -
1151 MAX_SMB2_CREATE_RESPONSE_SIZE -
1152 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1153 &rsp_iov[1], &resp_buftype[1], cifs_sb);
1154 if (rc == 0) {
1155 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1156 used_len = le32_to_cpu(rsp->OutputBufferLength);
1158 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1159 resp_buftype[1] = CIFS_NO_BUFFER;
1160 memset(&rsp_iov[1], 0, sizeof(rsp_iov[1]));
1161 rc = 0;
1163 /* Use a fudge factor of 256 bytes in case we collide
1164 * with a different set_EAs command.
1166 if(CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1167 MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
1168 used_len + ea_name_len + ea_value_len + 1) {
1169 rc = -ENOSPC;
1170 goto sea_exit;
1175 /* Open */
1176 memset(&open_iov, 0, sizeof(open_iov));
1177 rqst[0].rq_iov = open_iov;
1178 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1180 memset(&oparms, 0, sizeof(oparms));
1181 oparms.tcon = tcon;
1182 oparms.desired_access = FILE_WRITE_EA;
1183 oparms.disposition = FILE_OPEN;
1184 oparms.create_options = cifs_create_options(cifs_sb, 0);
1185 oparms.fid = &fid;
1186 oparms.reconnect = false;
1188 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
1189 if (rc)
1190 goto sea_exit;
1191 smb2_set_next_command(tcon, &rqst[0]);
1194 /* Set Info */
1195 memset(&si_iov, 0, sizeof(si_iov));
1196 rqst[1].rq_iov = si_iov;
1197 rqst[1].rq_nvec = 1;
1199 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
1200 ea = kzalloc(len, GFP_KERNEL);
1201 if (ea == NULL) {
1202 rc = -ENOMEM;
1203 goto sea_exit;
1206 ea->ea_name_length = ea_name_len;
1207 ea->ea_value_length = cpu_to_le16(ea_value_len);
1208 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1209 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1211 size[0] = len;
1212 data[0] = ea;
1214 rc = SMB2_set_info_init(tcon, &rqst[1], COMPOUND_FID,
1215 COMPOUND_FID, current->tgid,
1216 FILE_FULL_EA_INFORMATION,
1217 SMB2_O_INFO_FILE, 0, data, size);
1218 smb2_set_next_command(tcon, &rqst[1]);
1219 smb2_set_related(&rqst[1]);
1222 /* Close */
1223 memset(&close_iov, 0, sizeof(close_iov));
1224 rqst[2].rq_iov = close_iov;
1225 rqst[2].rq_nvec = 1;
1226 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1227 smb2_set_related(&rqst[2]);
1229 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1230 resp_buftype, rsp_iov);
1231 /* no need to bump num_remote_opens because handle immediately closed */
1233 sea_exit:
1234 kfree(ea);
1235 kfree(utf16_path);
1236 SMB2_open_free(&rqst[0]);
1237 SMB2_set_info_free(&rqst[1]);
1238 SMB2_close_free(&rqst[2]);
1239 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1240 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1241 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1242 return rc;
1244 #endif
1246 static bool
1247 smb2_can_echo(struct TCP_Server_Info *server)
1249 return server->echoes;
1252 static void
1253 smb2_clear_stats(struct cifs_tcon *tcon)
1255 int i;
1257 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1258 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1259 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1263 static void
1264 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1266 seq_puts(m, "\n\tShare Capabilities:");
1267 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1268 seq_puts(m, " DFS,");
1269 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1270 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1271 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1272 seq_puts(m, " SCALEOUT,");
1273 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1274 seq_puts(m, " CLUSTER,");
1275 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1276 seq_puts(m, " ASYMMETRIC,");
1277 if (tcon->capabilities == 0)
1278 seq_puts(m, " None");
1279 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1280 seq_puts(m, " Aligned,");
1281 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1282 seq_puts(m, " Partition Aligned,");
1283 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1284 seq_puts(m, " SSD,");
1285 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1286 seq_puts(m, " TRIM-support,");
1288 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1289 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1290 if (tcon->perf_sector_size)
1291 seq_printf(m, "\tOptimal sector size: 0x%x",
1292 tcon->perf_sector_size);
1293 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1296 static void
1297 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1299 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1300 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1303 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1304 * totals (requests sent) since those SMBs are per-session not per tcon
1306 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1307 (long long)(tcon->bytes_read),
1308 (long long)(tcon->bytes_written));
1309 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1310 atomic_read(&tcon->num_local_opens),
1311 atomic_read(&tcon->num_remote_opens));
1312 seq_printf(m, "\nTreeConnects: %d total %d failed",
1313 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1314 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1315 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1316 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1317 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1318 seq_printf(m, "\nCreates: %d total %d failed",
1319 atomic_read(&sent[SMB2_CREATE_HE]),
1320 atomic_read(&failed[SMB2_CREATE_HE]));
1321 seq_printf(m, "\nCloses: %d total %d failed",
1322 atomic_read(&sent[SMB2_CLOSE_HE]),
1323 atomic_read(&failed[SMB2_CLOSE_HE]));
1324 seq_printf(m, "\nFlushes: %d total %d failed",
1325 atomic_read(&sent[SMB2_FLUSH_HE]),
1326 atomic_read(&failed[SMB2_FLUSH_HE]));
1327 seq_printf(m, "\nReads: %d total %d failed",
1328 atomic_read(&sent[SMB2_READ_HE]),
1329 atomic_read(&failed[SMB2_READ_HE]));
1330 seq_printf(m, "\nWrites: %d total %d failed",
1331 atomic_read(&sent[SMB2_WRITE_HE]),
1332 atomic_read(&failed[SMB2_WRITE_HE]));
1333 seq_printf(m, "\nLocks: %d total %d failed",
1334 atomic_read(&sent[SMB2_LOCK_HE]),
1335 atomic_read(&failed[SMB2_LOCK_HE]));
1336 seq_printf(m, "\nIOCTLs: %d total %d failed",
1337 atomic_read(&sent[SMB2_IOCTL_HE]),
1338 atomic_read(&failed[SMB2_IOCTL_HE]));
1339 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1340 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1341 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1342 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1343 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1344 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1345 seq_printf(m, "\nQueryInfos: %d total %d failed",
1346 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1347 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1348 seq_printf(m, "\nSetInfos: %d total %d failed",
1349 atomic_read(&sent[SMB2_SET_INFO_HE]),
1350 atomic_read(&failed[SMB2_SET_INFO_HE]));
1351 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1352 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1353 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1356 static void
1357 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1359 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1360 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1362 cfile->fid.persistent_fid = fid->persistent_fid;
1363 cfile->fid.volatile_fid = fid->volatile_fid;
1364 cfile->fid.access = fid->access;
1365 #ifdef CONFIG_CIFS_DEBUG2
1366 cfile->fid.mid = fid->mid;
1367 #endif /* CIFS_DEBUG2 */
1368 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1369 &fid->purge_cache);
1370 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1371 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1374 static void
1375 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1376 struct cifs_fid *fid)
1378 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1381 static void
1382 smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
1383 struct cifsFileInfo *cfile)
1385 struct smb2_file_network_open_info file_inf;
1386 struct inode *inode;
1387 int rc;
1389 rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
1390 cfile->fid.volatile_fid, &file_inf);
1391 if (rc)
1392 return;
1394 inode = d_inode(cfile->dentry);
1396 spin_lock(&inode->i_lock);
1397 CIFS_I(inode)->time = jiffies;
1399 /* Creation time should not need to be updated on close */
1400 if (file_inf.LastWriteTime)
1401 inode->i_mtime = cifs_NTtimeToUnix(file_inf.LastWriteTime);
1402 if (file_inf.ChangeTime)
1403 inode->i_ctime = cifs_NTtimeToUnix(file_inf.ChangeTime);
1404 if (file_inf.LastAccessTime)
1405 inode->i_atime = cifs_NTtimeToUnix(file_inf.LastAccessTime);
1408 * i_blocks is not related to (i_size / i_blksize),
1409 * but instead 512 byte (2**9) size is required for
1410 * calculating num blocks.
1412 if (le64_to_cpu(file_inf.AllocationSize) > 4096)
1413 inode->i_blocks =
1414 (512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
1416 /* End of file and Attributes should not have to be updated on close */
1417 spin_unlock(&inode->i_lock);
1420 static int
1421 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1422 u64 persistent_fid, u64 volatile_fid,
1423 struct copychunk_ioctl *pcchunk)
1425 int rc;
1426 unsigned int ret_data_len;
1427 struct resume_key_req *res_key;
1429 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1430 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1431 NULL, 0 /* no input */, CIFSMaxBufSize,
1432 (char **)&res_key, &ret_data_len);
1434 if (rc) {
1435 cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1436 goto req_res_key_exit;
1438 if (ret_data_len < sizeof(struct resume_key_req)) {
1439 cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
1440 rc = -EINVAL;
1441 goto req_res_key_exit;
1443 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1445 req_res_key_exit:
1446 kfree(res_key);
1447 return rc;
1450 static int
1451 smb2_ioctl_query_info(const unsigned int xid,
1452 struct cifs_tcon *tcon,
1453 struct cifs_sb_info *cifs_sb,
1454 __le16 *path, int is_dir,
1455 unsigned long p)
1457 struct cifs_ses *ses = tcon->ses;
1458 char __user *arg = (char __user *)p;
1459 struct smb_query_info qi;
1460 struct smb_query_info __user *pqi;
1461 int rc = 0;
1462 int flags = 0;
1463 struct smb2_query_info_rsp *qi_rsp = NULL;
1464 struct smb2_ioctl_rsp *io_rsp = NULL;
1465 void *buffer = NULL;
1466 struct smb_rqst rqst[3];
1467 int resp_buftype[3];
1468 struct kvec rsp_iov[3];
1469 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1470 struct cifs_open_parms oparms;
1471 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1472 struct cifs_fid fid;
1473 struct kvec qi_iov[1];
1474 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1475 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1476 struct kvec close_iov[1];
1477 unsigned int size[2];
1478 void *data[2];
1479 int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
1481 memset(rqst, 0, sizeof(rqst));
1482 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1483 memset(rsp_iov, 0, sizeof(rsp_iov));
1485 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1486 return -EFAULT;
1488 if (qi.output_buffer_length > 1024)
1489 return -EINVAL;
1491 if (!ses || !(ses->server))
1492 return -EIO;
1494 if (smb3_encryption_required(tcon))
1495 flags |= CIFS_TRANSFORM_REQ;
1497 buffer = memdup_user(arg + sizeof(struct smb_query_info),
1498 qi.output_buffer_length);
1499 if (IS_ERR(buffer))
1500 return PTR_ERR(buffer);
1502 /* Open */
1503 memset(&open_iov, 0, sizeof(open_iov));
1504 rqst[0].rq_iov = open_iov;
1505 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1507 memset(&oparms, 0, sizeof(oparms));
1508 oparms.tcon = tcon;
1509 oparms.disposition = FILE_OPEN;
1510 oparms.create_options = cifs_create_options(cifs_sb, create_options);
1511 oparms.fid = &fid;
1512 oparms.reconnect = false;
1514 if (qi.flags & PASSTHRU_FSCTL) {
1515 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1516 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1517 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1518 break;
1519 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1520 oparms.desired_access = GENERIC_ALL;
1521 break;
1522 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1523 oparms.desired_access = GENERIC_READ;
1524 break;
1525 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1526 oparms.desired_access = GENERIC_WRITE;
1527 break;
1529 } else if (qi.flags & PASSTHRU_SET_INFO) {
1530 oparms.desired_access = GENERIC_WRITE;
1531 } else {
1532 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1535 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, path);
1536 if (rc)
1537 goto iqinf_exit;
1538 smb2_set_next_command(tcon, &rqst[0]);
1540 /* Query */
1541 if (qi.flags & PASSTHRU_FSCTL) {
1542 /* Can eventually relax perm check since server enforces too */
1543 if (!capable(CAP_SYS_ADMIN))
1544 rc = -EPERM;
1545 else {
1546 memset(&io_iov, 0, sizeof(io_iov));
1547 rqst[1].rq_iov = io_iov;
1548 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1550 rc = SMB2_ioctl_init(tcon, &rqst[1],
1551 COMPOUND_FID, COMPOUND_FID,
1552 qi.info_type, true, buffer,
1553 qi.output_buffer_length,
1554 CIFSMaxBufSize -
1555 MAX_SMB2_CREATE_RESPONSE_SIZE -
1556 MAX_SMB2_CLOSE_RESPONSE_SIZE);
1558 } else if (qi.flags == PASSTHRU_SET_INFO) {
1559 /* Can eventually relax perm check since server enforces too */
1560 if (!capable(CAP_SYS_ADMIN))
1561 rc = -EPERM;
1562 else {
1563 memset(&si_iov, 0, sizeof(si_iov));
1564 rqst[1].rq_iov = si_iov;
1565 rqst[1].rq_nvec = 1;
1567 size[0] = 8;
1568 data[0] = buffer;
1570 rc = SMB2_set_info_init(tcon, &rqst[1],
1571 COMPOUND_FID, COMPOUND_FID,
1572 current->tgid,
1573 FILE_END_OF_FILE_INFORMATION,
1574 SMB2_O_INFO_FILE, 0, data, size);
1576 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1577 memset(&qi_iov, 0, sizeof(qi_iov));
1578 rqst[1].rq_iov = qi_iov;
1579 rqst[1].rq_nvec = 1;
1581 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
1582 COMPOUND_FID, qi.file_info_class,
1583 qi.info_type, qi.additional_information,
1584 qi.input_buffer_length,
1585 qi.output_buffer_length, buffer);
1586 } else { /* unknown flags */
1587 cifs_tcon_dbg(VFS, "invalid passthru query flags: 0x%x\n", qi.flags);
1588 rc = -EINVAL;
1591 if (rc)
1592 goto iqinf_exit;
1593 smb2_set_next_command(tcon, &rqst[1]);
1594 smb2_set_related(&rqst[1]);
1596 /* Close */
1597 memset(&close_iov, 0, sizeof(close_iov));
1598 rqst[2].rq_iov = close_iov;
1599 rqst[2].rq_nvec = 1;
1601 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1602 if (rc)
1603 goto iqinf_exit;
1604 smb2_set_related(&rqst[2]);
1606 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1607 resp_buftype, rsp_iov);
1608 if (rc)
1609 goto iqinf_exit;
1611 /* No need to bump num_remote_opens since handle immediately closed */
1612 if (qi.flags & PASSTHRU_FSCTL) {
1613 pqi = (struct smb_query_info __user *)arg;
1614 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1615 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1616 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1617 if (qi.input_buffer_length > 0 &&
1618 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1619 > rsp_iov[1].iov_len)
1620 goto e_fault;
1622 if (copy_to_user(&pqi->input_buffer_length,
1623 &qi.input_buffer_length,
1624 sizeof(qi.input_buffer_length)))
1625 goto e_fault;
1627 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1628 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1629 qi.input_buffer_length))
1630 goto e_fault;
1631 } else {
1632 pqi = (struct smb_query_info __user *)arg;
1633 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1634 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1635 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1636 if (copy_to_user(&pqi->input_buffer_length,
1637 &qi.input_buffer_length,
1638 sizeof(qi.input_buffer_length)))
1639 goto e_fault;
1641 if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1642 qi.input_buffer_length))
1643 goto e_fault;
1646 iqinf_exit:
1647 kfree(buffer);
1648 SMB2_open_free(&rqst[0]);
1649 if (qi.flags & PASSTHRU_FSCTL)
1650 SMB2_ioctl_free(&rqst[1]);
1651 else
1652 SMB2_query_info_free(&rqst[1]);
1654 SMB2_close_free(&rqst[2]);
1655 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1656 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1657 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1658 return rc;
1660 e_fault:
1661 rc = -EFAULT;
1662 goto iqinf_exit;
1665 static ssize_t
1666 smb2_copychunk_range(const unsigned int xid,
1667 struct cifsFileInfo *srcfile,
1668 struct cifsFileInfo *trgtfile, u64 src_off,
1669 u64 len, u64 dest_off)
1671 int rc;
1672 unsigned int ret_data_len;
1673 struct copychunk_ioctl *pcchunk;
1674 struct copychunk_ioctl_rsp *retbuf = NULL;
1675 struct cifs_tcon *tcon;
1676 int chunks_copied = 0;
1677 bool chunk_sizes_updated = false;
1678 ssize_t bytes_written, total_bytes_written = 0;
1680 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1682 if (pcchunk == NULL)
1683 return -ENOMEM;
1685 cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
1686 /* Request a key from the server to identify the source of the copy */
1687 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1688 srcfile->fid.persistent_fid,
1689 srcfile->fid.volatile_fid, pcchunk);
1691 /* Note: request_res_key sets res_key null only if rc !=0 */
1692 if (rc)
1693 goto cchunk_out;
1695 /* For now array only one chunk long, will make more flexible later */
1696 pcchunk->ChunkCount = cpu_to_le32(1);
1697 pcchunk->Reserved = 0;
1698 pcchunk->Reserved2 = 0;
1700 tcon = tlink_tcon(trgtfile->tlink);
1702 while (len > 0) {
1703 pcchunk->SourceOffset = cpu_to_le64(src_off);
1704 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1705 pcchunk->Length =
1706 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1708 /* Request server copy to target from src identified by key */
1709 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1710 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1711 true /* is_fsctl */, (char *)pcchunk,
1712 sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1713 (char **)&retbuf, &ret_data_len);
1714 if (rc == 0) {
1715 if (ret_data_len !=
1716 sizeof(struct copychunk_ioctl_rsp)) {
1717 cifs_tcon_dbg(VFS, "invalid cchunk response size\n");
1718 rc = -EIO;
1719 goto cchunk_out;
1721 if (retbuf->TotalBytesWritten == 0) {
1722 cifs_dbg(FYI, "no bytes copied\n");
1723 rc = -EIO;
1724 goto cchunk_out;
1727 * Check if server claimed to write more than we asked
1729 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1730 le32_to_cpu(pcchunk->Length)) {
1731 cifs_tcon_dbg(VFS, "invalid copy chunk response\n");
1732 rc = -EIO;
1733 goto cchunk_out;
1735 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1736 cifs_tcon_dbg(VFS, "invalid num chunks written\n");
1737 rc = -EIO;
1738 goto cchunk_out;
1740 chunks_copied++;
1742 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1743 src_off += bytes_written;
1744 dest_off += bytes_written;
1745 len -= bytes_written;
1746 total_bytes_written += bytes_written;
1748 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1749 le32_to_cpu(retbuf->ChunksWritten),
1750 le32_to_cpu(retbuf->ChunkBytesWritten),
1751 bytes_written);
1752 } else if (rc == -EINVAL) {
1753 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1754 goto cchunk_out;
1756 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1757 le32_to_cpu(retbuf->ChunksWritten),
1758 le32_to_cpu(retbuf->ChunkBytesWritten),
1759 le32_to_cpu(retbuf->TotalBytesWritten));
1762 * Check if this is the first request using these sizes,
1763 * (ie check if copy succeed once with original sizes
1764 * and check if the server gave us different sizes after
1765 * we already updated max sizes on previous request).
1766 * if not then why is the server returning an error now
1768 if ((chunks_copied != 0) || chunk_sizes_updated)
1769 goto cchunk_out;
1771 /* Check that server is not asking us to grow size */
1772 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1773 tcon->max_bytes_chunk)
1774 tcon->max_bytes_chunk =
1775 le32_to_cpu(retbuf->ChunkBytesWritten);
1776 else
1777 goto cchunk_out; /* server gave us bogus size */
1779 /* No need to change MaxChunks since already set to 1 */
1780 chunk_sizes_updated = true;
1781 } else
1782 goto cchunk_out;
1785 cchunk_out:
1786 kfree(pcchunk);
1787 kfree(retbuf);
1788 if (rc)
1789 return rc;
1790 else
1791 return total_bytes_written;
1794 static int
1795 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1796 struct cifs_fid *fid)
1798 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1801 static unsigned int
1802 smb2_read_data_offset(char *buf)
1804 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1806 return rsp->DataOffset;
1809 static unsigned int
1810 smb2_read_data_length(char *buf, bool in_remaining)
1812 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1814 if (in_remaining)
1815 return le32_to_cpu(rsp->DataRemaining);
1817 return le32_to_cpu(rsp->DataLength);
1821 static int
1822 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1823 struct cifs_io_parms *parms, unsigned int *bytes_read,
1824 char **buf, int *buf_type)
1826 parms->persistent_fid = pfid->persistent_fid;
1827 parms->volatile_fid = pfid->volatile_fid;
1828 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1831 static int
1832 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1833 struct cifs_io_parms *parms, unsigned int *written,
1834 struct kvec *iov, unsigned long nr_segs)
1837 parms->persistent_fid = pfid->persistent_fid;
1838 parms->volatile_fid = pfid->volatile_fid;
1839 return SMB2_write(xid, parms, written, iov, nr_segs);
1842 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1843 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1844 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1846 struct cifsInodeInfo *cifsi;
1847 int rc;
1849 cifsi = CIFS_I(inode);
1851 /* if file already sparse don't bother setting sparse again */
1852 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1853 return true; /* already sparse */
1855 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1856 return true; /* already not sparse */
1859 * Can't check for sparse support on share the usual way via the
1860 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1861 * since Samba server doesn't set the flag on the share, yet
1862 * supports the set sparse FSCTL and returns sparse correctly
1863 * in the file attributes. If we fail setting sparse though we
1864 * mark that server does not support sparse files for this share
1865 * to avoid repeatedly sending the unsupported fsctl to server
1866 * if the file is repeatedly extended.
1868 if (tcon->broken_sparse_sup)
1869 return false;
1871 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1872 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1873 true /* is_fctl */,
1874 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
1875 if (rc) {
1876 tcon->broken_sparse_sup = true;
1877 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1878 return false;
1881 if (setsparse)
1882 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1883 else
1884 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1886 return true;
1889 static int
1890 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1891 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1893 __le64 eof = cpu_to_le64(size);
1894 struct inode *inode;
1897 * If extending file more than one page make sparse. Many Linux fs
1898 * make files sparse by default when extending via ftruncate
1900 inode = d_inode(cfile->dentry);
1902 if (!set_alloc && (size > inode->i_size + 8192)) {
1903 __u8 set_sparse = 1;
1905 /* whether set sparse succeeds or not, extend the file */
1906 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1909 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1910 cfile->fid.volatile_fid, cfile->pid, &eof);
1913 static int
1914 smb2_duplicate_extents(const unsigned int xid,
1915 struct cifsFileInfo *srcfile,
1916 struct cifsFileInfo *trgtfile, u64 src_off,
1917 u64 len, u64 dest_off)
1919 int rc;
1920 unsigned int ret_data_len;
1921 struct duplicate_extents_to_file dup_ext_buf;
1922 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1924 /* server fileays advertise duplicate extent support with this flag */
1925 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1926 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1927 return -EOPNOTSUPP;
1929 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1930 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1931 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1932 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1933 dup_ext_buf.ByteCount = cpu_to_le64(len);
1934 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
1935 src_off, dest_off, len);
1937 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1938 if (rc)
1939 goto duplicate_extents_out;
1941 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1942 trgtfile->fid.volatile_fid,
1943 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
1944 true /* is_fsctl */,
1945 (char *)&dup_ext_buf,
1946 sizeof(struct duplicate_extents_to_file),
1947 CIFSMaxBufSize, NULL,
1948 &ret_data_len);
1950 if (ret_data_len > 0)
1951 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
1953 duplicate_extents_out:
1954 return rc;
1957 static int
1958 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1959 struct cifsFileInfo *cfile)
1961 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1962 cfile->fid.volatile_fid);
1965 static int
1966 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1967 struct cifsFileInfo *cfile)
1969 struct fsctl_set_integrity_information_req integr_info;
1970 unsigned int ret_data_len;
1972 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1973 integr_info.Flags = 0;
1974 integr_info.Reserved = 0;
1976 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1977 cfile->fid.volatile_fid,
1978 FSCTL_SET_INTEGRITY_INFORMATION,
1979 true /* is_fsctl */,
1980 (char *)&integr_info,
1981 sizeof(struct fsctl_set_integrity_information_req),
1982 CIFSMaxBufSize, NULL,
1983 &ret_data_len);
1987 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
1988 #define GMT_TOKEN_SIZE 50
1990 #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
1993 * Input buffer contains (empty) struct smb_snapshot array with size filled in
1994 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
1996 static int
1997 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1998 struct cifsFileInfo *cfile, void __user *ioc_buf)
2000 char *retbuf = NULL;
2001 unsigned int ret_data_len = 0;
2002 int rc;
2003 u32 max_response_size;
2004 struct smb_snapshot_array snapshot_in;
2007 * On the first query to enumerate the list of snapshots available
2008 * for this volume the buffer begins with 0 (number of snapshots
2009 * which can be returned is zero since at that point we do not know
2010 * how big the buffer needs to be). On the second query,
2011 * it (ret_data_len) is set to number of snapshots so we can
2012 * know to set the maximum response size larger (see below).
2014 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
2015 return -EFAULT;
2018 * Note that for snapshot queries that servers like Azure expect that
2019 * the first query be minimal size (and just used to get the number/size
2020 * of previous versions) so response size must be specified as EXACTLY
2021 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2022 * of eight bytes.
2024 if (ret_data_len == 0)
2025 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
2026 else
2027 max_response_size = CIFSMaxBufSize;
2029 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2030 cfile->fid.volatile_fid,
2031 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
2032 true /* is_fsctl */,
2033 NULL, 0 /* no input data */, max_response_size,
2034 (char **)&retbuf,
2035 &ret_data_len);
2036 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2037 rc, ret_data_len);
2038 if (rc)
2039 return rc;
2041 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2042 /* Fixup buffer */
2043 if (copy_from_user(&snapshot_in, ioc_buf,
2044 sizeof(struct smb_snapshot_array))) {
2045 rc = -EFAULT;
2046 kfree(retbuf);
2047 return rc;
2051 * Check for min size, ie not large enough to fit even one GMT
2052 * token (snapshot). On the first ioctl some users may pass in
2053 * smaller size (or zero) to simply get the size of the array
2054 * so the user space caller can allocate sufficient memory
2055 * and retry the ioctl again with larger array size sufficient
2056 * to hold all of the snapshot GMT tokens on the second try.
2058 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2059 ret_data_len = sizeof(struct smb_snapshot_array);
2062 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2063 * the snapshot array (of 50 byte GMT tokens) each
2064 * representing an available previous version of the data
2066 if (ret_data_len > (snapshot_in.snapshot_array_size +
2067 sizeof(struct smb_snapshot_array)))
2068 ret_data_len = snapshot_in.snapshot_array_size +
2069 sizeof(struct smb_snapshot_array);
2071 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2072 rc = -EFAULT;
2075 kfree(retbuf);
2076 return rc;
2081 static int
2082 smb3_notify(const unsigned int xid, struct file *pfile,
2083 void __user *ioc_buf)
2085 struct smb3_notify notify;
2086 struct dentry *dentry = pfile->f_path.dentry;
2087 struct inode *inode = file_inode(pfile);
2088 struct cifs_sb_info *cifs_sb;
2089 struct cifs_open_parms oparms;
2090 struct cifs_fid fid;
2091 struct cifs_tcon *tcon;
2092 unsigned char *path = NULL;
2093 __le16 *utf16_path = NULL;
2094 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2095 int rc = 0;
2097 path = build_path_from_dentry(dentry);
2098 if (path == NULL)
2099 return -ENOMEM;
2101 cifs_sb = CIFS_SB(inode->i_sb);
2103 utf16_path = cifs_convert_path_to_utf16(path + 1, cifs_sb);
2104 if (utf16_path == NULL) {
2105 rc = -ENOMEM;
2106 goto notify_exit;
2109 if (copy_from_user(&notify, ioc_buf, sizeof(struct smb3_notify))) {
2110 rc = -EFAULT;
2111 goto notify_exit;
2114 tcon = cifs_sb_master_tcon(cifs_sb);
2115 oparms.tcon = tcon;
2116 oparms.desired_access = FILE_READ_ATTRIBUTES;
2117 oparms.disposition = FILE_OPEN;
2118 oparms.create_options = cifs_create_options(cifs_sb, 0);
2119 oparms.fid = &fid;
2120 oparms.reconnect = false;
2122 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
2123 NULL);
2124 if (rc)
2125 goto notify_exit;
2127 rc = SMB2_change_notify(xid, tcon, fid.persistent_fid, fid.volatile_fid,
2128 notify.watch_tree, notify.completion_filter);
2130 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2132 cifs_dbg(FYI, "change notify for path %s rc %d\n", path, rc);
2134 notify_exit:
2135 kfree(path);
2136 kfree(utf16_path);
2137 return rc;
2140 static int
2141 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2142 const char *path, struct cifs_sb_info *cifs_sb,
2143 struct cifs_fid *fid, __u16 search_flags,
2144 struct cifs_search_info *srch_inf)
2146 __le16 *utf16_path;
2147 struct smb_rqst rqst[2];
2148 struct kvec rsp_iov[2];
2149 int resp_buftype[2];
2150 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2151 struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
2152 int rc, flags = 0;
2153 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2154 struct cifs_open_parms oparms;
2155 struct smb2_query_directory_rsp *qd_rsp = NULL;
2156 struct smb2_create_rsp *op_rsp = NULL;
2158 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2159 if (!utf16_path)
2160 return -ENOMEM;
2162 if (smb3_encryption_required(tcon))
2163 flags |= CIFS_TRANSFORM_REQ;
2165 memset(rqst, 0, sizeof(rqst));
2166 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2167 memset(rsp_iov, 0, sizeof(rsp_iov));
2169 /* Open */
2170 memset(&open_iov, 0, sizeof(open_iov));
2171 rqst[0].rq_iov = open_iov;
2172 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2174 oparms.tcon = tcon;
2175 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2176 oparms.disposition = FILE_OPEN;
2177 oparms.create_options = cifs_create_options(cifs_sb, 0);
2178 oparms.fid = fid;
2179 oparms.reconnect = false;
2181 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2182 if (rc)
2183 goto qdf_free;
2184 smb2_set_next_command(tcon, &rqst[0]);
2186 /* Query directory */
2187 srch_inf->entries_in_buffer = 0;
2188 srch_inf->index_of_last_entry = 2;
2190 memset(&qd_iov, 0, sizeof(qd_iov));
2191 rqst[1].rq_iov = qd_iov;
2192 rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
2194 rc = SMB2_query_directory_init(xid, tcon, &rqst[1],
2195 COMPOUND_FID, COMPOUND_FID,
2196 0, srch_inf->info_level);
2197 if (rc)
2198 goto qdf_free;
2200 smb2_set_related(&rqst[1]);
2202 rc = compound_send_recv(xid, tcon->ses, flags, 2, rqst,
2203 resp_buftype, rsp_iov);
2205 /* If the open failed there is nothing to do */
2206 op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
2207 if (op_rsp == NULL || op_rsp->sync_hdr.Status != STATUS_SUCCESS) {
2208 cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
2209 goto qdf_free;
2211 fid->persistent_fid = op_rsp->PersistentFileId;
2212 fid->volatile_fid = op_rsp->VolatileFileId;
2214 /* Anything else than ENODATA means a genuine error */
2215 if (rc && rc != -ENODATA) {
2216 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2217 cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
2218 trace_smb3_query_dir_err(xid, fid->persistent_fid,
2219 tcon->tid, tcon->ses->Suid, 0, 0, rc);
2220 goto qdf_free;
2223 atomic_inc(&tcon->num_remote_opens);
2225 qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
2226 if (qd_rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
2227 trace_smb3_query_dir_done(xid, fid->persistent_fid,
2228 tcon->tid, tcon->ses->Suid, 0, 0);
2229 srch_inf->endOfSearch = true;
2230 rc = 0;
2231 goto qdf_free;
2234 rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
2235 srch_inf);
2236 if (rc) {
2237 trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
2238 tcon->ses->Suid, 0, 0, rc);
2239 goto qdf_free;
2241 resp_buftype[1] = CIFS_NO_BUFFER;
2243 trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
2244 tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
2246 qdf_free:
2247 kfree(utf16_path);
2248 SMB2_open_free(&rqst[0]);
2249 SMB2_query_directory_free(&rqst[1]);
2250 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2251 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2252 return rc;
2255 static int
2256 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2257 struct cifs_fid *fid, __u16 search_flags,
2258 struct cifs_search_info *srch_inf)
2260 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2261 fid->volatile_fid, 0, srch_inf);
2264 static int
2265 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2266 struct cifs_fid *fid)
2268 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2272 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2273 * the number of credits and return true. Otherwise - return false.
2275 static bool
2276 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
2278 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2280 if (shdr->Status != STATUS_PENDING)
2281 return false;
2283 if (shdr->CreditRequest) {
2284 spin_lock(&server->req_lock);
2285 server->credits += le16_to_cpu(shdr->CreditRequest);
2286 spin_unlock(&server->req_lock);
2287 wake_up(&server->request_q);
2290 return true;
2293 static bool
2294 smb2_is_session_expired(char *buf)
2296 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2298 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2299 shdr->Status != STATUS_USER_SESSION_DELETED)
2300 return false;
2302 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2303 le16_to_cpu(shdr->Command),
2304 le64_to_cpu(shdr->MessageId));
2305 cifs_dbg(FYI, "Session expired or deleted\n");
2307 return true;
2310 static int
2311 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2312 struct cifsInodeInfo *cinode)
2314 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2315 return SMB2_lease_break(0, tcon, cinode->lease_key,
2316 smb2_get_lease_state(cinode));
2318 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2319 fid->volatile_fid,
2320 CIFS_CACHE_READ(cinode) ? 1 : 0);
2323 void
2324 smb2_set_related(struct smb_rqst *rqst)
2326 struct smb2_sync_hdr *shdr;
2328 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2329 if (shdr == NULL) {
2330 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2331 return;
2333 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2336 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2338 void
2339 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2341 struct smb2_sync_hdr *shdr;
2342 struct cifs_ses *ses = tcon->ses;
2343 struct TCP_Server_Info *server = ses->server;
2344 unsigned long len = smb_rqst_len(server, rqst);
2345 int i, num_padding;
2347 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2348 if (shdr == NULL) {
2349 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2350 return;
2353 /* SMB headers in a compound are 8 byte aligned. */
2355 /* No padding needed */
2356 if (!(len & 7))
2357 goto finished;
2359 num_padding = 8 - (len & 7);
2360 if (!smb3_encryption_required(tcon)) {
2362 * If we do not have encryption then we can just add an extra
2363 * iov for the padding.
2365 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2366 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2367 rqst->rq_nvec++;
2368 len += num_padding;
2369 } else {
2371 * We can not add a small padding iov for the encryption case
2372 * because the encryption framework can not handle the padding
2373 * iovs.
2374 * We have to flatten this into a single buffer and add
2375 * the padding to it.
2377 for (i = 1; i < rqst->rq_nvec; i++) {
2378 memcpy(rqst->rq_iov[0].iov_base +
2379 rqst->rq_iov[0].iov_len,
2380 rqst->rq_iov[i].iov_base,
2381 rqst->rq_iov[i].iov_len);
2382 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2384 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2385 0, num_padding);
2386 rqst->rq_iov[0].iov_len += num_padding;
2387 len += num_padding;
2388 rqst->rq_nvec = 1;
2391 finished:
2392 shdr->NextCommand = cpu_to_le32(len);
2396 * Passes the query info response back to the caller on success.
2397 * Caller need to free this with free_rsp_buf().
2400 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2401 __le16 *utf16_path, u32 desired_access,
2402 u32 class, u32 type, u32 output_len,
2403 struct kvec *rsp, int *buftype,
2404 struct cifs_sb_info *cifs_sb)
2406 struct cifs_ses *ses = tcon->ses;
2407 int flags = 0;
2408 struct smb_rqst rqst[3];
2409 int resp_buftype[3];
2410 struct kvec rsp_iov[3];
2411 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2412 struct kvec qi_iov[1];
2413 struct kvec close_iov[1];
2414 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2415 struct cifs_open_parms oparms;
2416 struct cifs_fid fid;
2417 int rc;
2419 if (smb3_encryption_required(tcon))
2420 flags |= CIFS_TRANSFORM_REQ;
2422 memset(rqst, 0, sizeof(rqst));
2423 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2424 memset(rsp_iov, 0, sizeof(rsp_iov));
2426 memset(&open_iov, 0, sizeof(open_iov));
2427 rqst[0].rq_iov = open_iov;
2428 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2430 oparms.tcon = tcon;
2431 oparms.desired_access = desired_access;
2432 oparms.disposition = FILE_OPEN;
2433 oparms.create_options = cifs_create_options(cifs_sb, 0);
2434 oparms.fid = &fid;
2435 oparms.reconnect = false;
2437 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2438 if (rc)
2439 goto qic_exit;
2440 smb2_set_next_command(tcon, &rqst[0]);
2442 memset(&qi_iov, 0, sizeof(qi_iov));
2443 rqst[1].rq_iov = qi_iov;
2444 rqst[1].rq_nvec = 1;
2446 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
2447 class, type, 0,
2448 output_len, 0,
2449 NULL);
2450 if (rc)
2451 goto qic_exit;
2452 smb2_set_next_command(tcon, &rqst[1]);
2453 smb2_set_related(&rqst[1]);
2455 memset(&close_iov, 0, sizeof(close_iov));
2456 rqst[2].rq_iov = close_iov;
2457 rqst[2].rq_nvec = 1;
2459 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2460 if (rc)
2461 goto qic_exit;
2462 smb2_set_related(&rqst[2]);
2464 rc = compound_send_recv(xid, ses, flags, 3, rqst,
2465 resp_buftype, rsp_iov);
2466 if (rc) {
2467 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2468 if (rc == -EREMCHG) {
2469 tcon->need_reconnect = true;
2470 printk_once(KERN_WARNING "server share %s deleted\n",
2471 tcon->treeName);
2473 goto qic_exit;
2475 *rsp = rsp_iov[1];
2476 *buftype = resp_buftype[1];
2478 qic_exit:
2479 SMB2_open_free(&rqst[0]);
2480 SMB2_query_info_free(&rqst[1]);
2481 SMB2_close_free(&rqst[2]);
2482 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2483 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2484 return rc;
2487 static int
2488 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2489 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2491 struct smb2_query_info_rsp *rsp;
2492 struct smb2_fs_full_size_info *info = NULL;
2493 __le16 utf16_path = 0; /* Null - open root of share */
2494 struct kvec rsp_iov = {NULL, 0};
2495 int buftype = CIFS_NO_BUFFER;
2496 int rc;
2499 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2500 FILE_READ_ATTRIBUTES,
2501 FS_FULL_SIZE_INFORMATION,
2502 SMB2_O_INFO_FILESYSTEM,
2503 sizeof(struct smb2_fs_full_size_info),
2504 &rsp_iov, &buftype, cifs_sb);
2505 if (rc)
2506 goto qfs_exit;
2508 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2509 buf->f_type = SMB2_MAGIC_NUMBER;
2510 info = (struct smb2_fs_full_size_info *)(
2511 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2512 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2513 le32_to_cpu(rsp->OutputBufferLength),
2514 &rsp_iov,
2515 sizeof(struct smb2_fs_full_size_info));
2516 if (!rc)
2517 smb2_copy_fs_info_to_kstatfs(info, buf);
2519 qfs_exit:
2520 free_rsp_buf(buftype, rsp_iov.iov_base);
2521 return rc;
2524 static int
2525 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2526 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2528 int rc;
2529 __le16 srch_path = 0; /* Null - open root of share */
2530 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2531 struct cifs_open_parms oparms;
2532 struct cifs_fid fid;
2534 if (!tcon->posix_extensions)
2535 return smb2_queryfs(xid, tcon, cifs_sb, buf);
2537 oparms.tcon = tcon;
2538 oparms.desired_access = FILE_READ_ATTRIBUTES;
2539 oparms.disposition = FILE_OPEN;
2540 oparms.create_options = cifs_create_options(cifs_sb, 0);
2541 oparms.fid = &fid;
2542 oparms.reconnect = false;
2544 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
2545 NULL, NULL);
2546 if (rc)
2547 return rc;
2549 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2550 fid.volatile_fid, buf);
2551 buf->f_type = SMB2_MAGIC_NUMBER;
2552 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2553 return rc;
2556 static bool
2557 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2559 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2560 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2563 static int
2564 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2565 __u64 length, __u32 type, int lock, int unlock, bool wait)
2567 if (unlock && !lock)
2568 type = SMB2_LOCKFLAG_UNLOCK;
2569 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2570 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2571 current->tgid, length, offset, type, wait);
2574 static void
2575 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2577 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2580 static void
2581 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2583 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2586 static void
2587 smb2_new_lease_key(struct cifs_fid *fid)
2589 generate_random_uuid(fid->lease_key);
2592 static int
2593 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2594 const char *search_name,
2595 struct dfs_info3_param **target_nodes,
2596 unsigned int *num_of_nodes,
2597 const struct nls_table *nls_codepage, int remap)
2599 int rc;
2600 __le16 *utf16_path = NULL;
2601 int utf16_path_len = 0;
2602 struct cifs_tcon *tcon;
2603 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2604 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2605 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2607 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
2610 * Try to use the IPC tcon, otherwise just use any
2612 tcon = ses->tcon_ipc;
2613 if (tcon == NULL) {
2614 spin_lock(&cifs_tcp_ses_lock);
2615 tcon = list_first_entry_or_null(&ses->tcon_list,
2616 struct cifs_tcon,
2617 tcon_list);
2618 if (tcon)
2619 tcon->tc_count++;
2620 spin_unlock(&cifs_tcp_ses_lock);
2623 if (tcon == NULL) {
2624 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2625 ses);
2626 rc = -ENOTCONN;
2627 goto out;
2630 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2631 &utf16_path_len,
2632 nls_codepage, remap);
2633 if (!utf16_path) {
2634 rc = -ENOMEM;
2635 goto out;
2638 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2639 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2640 if (!dfs_req) {
2641 rc = -ENOMEM;
2642 goto out;
2645 /* Highest DFS referral version understood */
2646 dfs_req->MaxReferralLevel = DFS_VERSION;
2648 /* Path to resolve in an UTF-16 null-terminated string */
2649 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2651 do {
2652 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2653 FSCTL_DFS_GET_REFERRALS,
2654 true /* is_fsctl */,
2655 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
2656 (char **)&dfs_rsp, &dfs_rsp_size);
2657 } while (rc == -EAGAIN);
2659 if (rc) {
2660 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
2661 cifs_tcon_dbg(VFS, "ioctl error in %s rc=%d\n", __func__, rc);
2662 goto out;
2665 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2666 num_of_nodes, target_nodes,
2667 nls_codepage, remap, search_name,
2668 true /* is_unicode */);
2669 if (rc) {
2670 cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
2671 goto out;
2674 out:
2675 if (tcon && !tcon->ipc) {
2676 /* ipc tcons are not refcounted */
2677 spin_lock(&cifs_tcp_ses_lock);
2678 tcon->tc_count--;
2679 spin_unlock(&cifs_tcp_ses_lock);
2681 kfree(utf16_path);
2682 kfree(dfs_req);
2683 kfree(dfs_rsp);
2684 return rc;
2687 static int
2688 parse_reparse_posix(struct reparse_posix_data *symlink_buf,
2689 u32 plen, char **target_path,
2690 struct cifs_sb_info *cifs_sb)
2692 unsigned int len;
2694 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
2695 len = le16_to_cpu(symlink_buf->ReparseDataLength);
2697 if (le64_to_cpu(symlink_buf->InodeType) != NFS_SPECFILE_LNK) {
2698 cifs_dbg(VFS, "%lld not a supported symlink type\n",
2699 le64_to_cpu(symlink_buf->InodeType));
2700 return -EOPNOTSUPP;
2703 *target_path = cifs_strndup_from_utf16(
2704 symlink_buf->PathBuffer,
2705 len, true, cifs_sb->local_nls);
2706 if (!(*target_path))
2707 return -ENOMEM;
2709 convert_delimiter(*target_path, '/');
2710 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2712 return 0;
2715 static int
2716 parse_reparse_symlink(struct reparse_symlink_data_buffer *symlink_buf,
2717 u32 plen, char **target_path,
2718 struct cifs_sb_info *cifs_sb)
2720 unsigned int sub_len;
2721 unsigned int sub_offset;
2723 /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
2725 sub_offset = le16_to_cpu(symlink_buf->SubstituteNameOffset);
2726 sub_len = le16_to_cpu(symlink_buf->SubstituteNameLength);
2727 if (sub_offset + 20 > plen ||
2728 sub_offset + sub_len + 20 > plen) {
2729 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
2730 return -EIO;
2733 *target_path = cifs_strndup_from_utf16(
2734 symlink_buf->PathBuffer + sub_offset,
2735 sub_len, true, cifs_sb->local_nls);
2736 if (!(*target_path))
2737 return -ENOMEM;
2739 convert_delimiter(*target_path, '/');
2740 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2742 return 0;
2745 static int
2746 parse_reparse_point(struct reparse_data_buffer *buf,
2747 u32 plen, char **target_path,
2748 struct cifs_sb_info *cifs_sb)
2750 if (plen < sizeof(struct reparse_data_buffer)) {
2751 cifs_dbg(VFS, "reparse buffer is too small. Must be "
2752 "at least 8 bytes but was %d\n", plen);
2753 return -EIO;
2756 if (plen < le16_to_cpu(buf->ReparseDataLength) +
2757 sizeof(struct reparse_data_buffer)) {
2758 cifs_dbg(VFS, "srv returned invalid reparse buf "
2759 "length: %d\n", plen);
2760 return -EIO;
2763 /* See MS-FSCC 2.1.2 */
2764 switch (le32_to_cpu(buf->ReparseTag)) {
2765 case IO_REPARSE_TAG_NFS:
2766 return parse_reparse_posix(
2767 (struct reparse_posix_data *)buf,
2768 plen, target_path, cifs_sb);
2769 case IO_REPARSE_TAG_SYMLINK:
2770 return parse_reparse_symlink(
2771 (struct reparse_symlink_data_buffer *)buf,
2772 plen, target_path, cifs_sb);
2773 default:
2774 cifs_dbg(VFS, "srv returned unknown symlink buffer "
2775 "tag:0x%08x\n", le32_to_cpu(buf->ReparseTag));
2776 return -EOPNOTSUPP;
2780 #define SMB2_SYMLINK_STRUCT_SIZE \
2781 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2783 static int
2784 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
2785 struct cifs_sb_info *cifs_sb, const char *full_path,
2786 char **target_path, bool is_reparse_point)
2788 int rc;
2789 __le16 *utf16_path = NULL;
2790 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2791 struct cifs_open_parms oparms;
2792 struct cifs_fid fid;
2793 struct kvec err_iov = {NULL, 0};
2794 struct smb2_err_rsp *err_buf = NULL;
2795 struct smb2_symlink_err_rsp *symlink;
2796 unsigned int sub_len;
2797 unsigned int sub_offset;
2798 unsigned int print_len;
2799 unsigned int print_offset;
2800 int flags = 0;
2801 struct smb_rqst rqst[3];
2802 int resp_buftype[3];
2803 struct kvec rsp_iov[3];
2804 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2805 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2806 struct kvec close_iov[1];
2807 struct smb2_create_rsp *create_rsp;
2808 struct smb2_ioctl_rsp *ioctl_rsp;
2809 struct reparse_data_buffer *reparse_buf;
2810 int create_options = is_reparse_point ? OPEN_REPARSE_POINT : 0;
2811 u32 plen;
2813 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2815 *target_path = NULL;
2817 if (smb3_encryption_required(tcon))
2818 flags |= CIFS_TRANSFORM_REQ;
2820 memset(rqst, 0, sizeof(rqst));
2821 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2822 memset(rsp_iov, 0, sizeof(rsp_iov));
2824 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2825 if (!utf16_path)
2826 return -ENOMEM;
2828 /* Open */
2829 memset(&open_iov, 0, sizeof(open_iov));
2830 rqst[0].rq_iov = open_iov;
2831 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2833 memset(&oparms, 0, sizeof(oparms));
2834 oparms.tcon = tcon;
2835 oparms.desired_access = FILE_READ_ATTRIBUTES;
2836 oparms.disposition = FILE_OPEN;
2837 oparms.create_options = cifs_create_options(cifs_sb, create_options);
2838 oparms.fid = &fid;
2839 oparms.reconnect = false;
2841 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2842 if (rc)
2843 goto querty_exit;
2844 smb2_set_next_command(tcon, &rqst[0]);
2847 /* IOCTL */
2848 memset(&io_iov, 0, sizeof(io_iov));
2849 rqst[1].rq_iov = io_iov;
2850 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
2852 rc = SMB2_ioctl_init(tcon, &rqst[1], fid.persistent_fid,
2853 fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
2854 true /* is_fctl */, NULL, 0,
2855 CIFSMaxBufSize -
2856 MAX_SMB2_CREATE_RESPONSE_SIZE -
2857 MAX_SMB2_CLOSE_RESPONSE_SIZE);
2858 if (rc)
2859 goto querty_exit;
2861 smb2_set_next_command(tcon, &rqst[1]);
2862 smb2_set_related(&rqst[1]);
2865 /* Close */
2866 memset(&close_iov, 0, sizeof(close_iov));
2867 rqst[2].rq_iov = close_iov;
2868 rqst[2].rq_nvec = 1;
2870 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2871 if (rc)
2872 goto querty_exit;
2874 smb2_set_related(&rqst[2]);
2876 rc = compound_send_recv(xid, tcon->ses, flags, 3, rqst,
2877 resp_buftype, rsp_iov);
2879 create_rsp = rsp_iov[0].iov_base;
2880 if (create_rsp && create_rsp->sync_hdr.Status)
2881 err_iov = rsp_iov[0];
2882 ioctl_rsp = rsp_iov[1].iov_base;
2885 * Open was successful and we got an ioctl response.
2887 if ((rc == 0) && (is_reparse_point)) {
2888 /* See MS-FSCC 2.3.23 */
2890 reparse_buf = (struct reparse_data_buffer *)
2891 ((char *)ioctl_rsp +
2892 le32_to_cpu(ioctl_rsp->OutputOffset));
2893 plen = le32_to_cpu(ioctl_rsp->OutputCount);
2895 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
2896 rsp_iov[1].iov_len) {
2897 cifs_tcon_dbg(VFS, "srv returned invalid ioctl len: %d\n",
2898 plen);
2899 rc = -EIO;
2900 goto querty_exit;
2903 rc = parse_reparse_point(reparse_buf, plen, target_path,
2904 cifs_sb);
2905 goto querty_exit;
2908 if (!rc || !err_iov.iov_base) {
2909 rc = -ENOENT;
2910 goto querty_exit;
2913 err_buf = err_iov.iov_base;
2914 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
2915 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
2916 rc = -EINVAL;
2917 goto querty_exit;
2920 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
2921 if (le32_to_cpu(symlink->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
2922 le32_to_cpu(symlink->ReparseTag) != IO_REPARSE_TAG_SYMLINK) {
2923 rc = -EINVAL;
2924 goto querty_exit;
2927 /* open must fail on symlink - reset rc */
2928 rc = 0;
2929 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
2930 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
2931 print_len = le16_to_cpu(symlink->PrintNameLength);
2932 print_offset = le16_to_cpu(symlink->PrintNameOffset);
2934 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
2935 rc = -EINVAL;
2936 goto querty_exit;
2939 if (err_iov.iov_len <
2940 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
2941 rc = -EINVAL;
2942 goto querty_exit;
2945 *target_path = cifs_strndup_from_utf16(
2946 (char *)symlink->PathBuffer + sub_offset,
2947 sub_len, true, cifs_sb->local_nls);
2948 if (!(*target_path)) {
2949 rc = -ENOMEM;
2950 goto querty_exit;
2952 convert_delimiter(*target_path, '/');
2953 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2955 querty_exit:
2956 cifs_dbg(FYI, "query symlink rc %d\n", rc);
2957 kfree(utf16_path);
2958 SMB2_open_free(&rqst[0]);
2959 SMB2_ioctl_free(&rqst[1]);
2960 SMB2_close_free(&rqst[2]);
2961 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2962 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2963 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2964 return rc;
2967 static struct cifs_ntsd *
2968 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
2969 const struct cifs_fid *cifsfid, u32 *pacllen)
2971 struct cifs_ntsd *pntsd = NULL;
2972 unsigned int xid;
2973 int rc = -EOPNOTSUPP;
2974 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2976 if (IS_ERR(tlink))
2977 return ERR_CAST(tlink);
2979 xid = get_xid();
2980 cifs_dbg(FYI, "trying to get acl\n");
2982 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
2983 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
2984 free_xid(xid);
2986 cifs_put_tlink(tlink);
2988 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2989 if (rc)
2990 return ERR_PTR(rc);
2991 return pntsd;
2995 static struct cifs_ntsd *
2996 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
2997 const char *path, u32 *pacllen)
2999 struct cifs_ntsd *pntsd = NULL;
3000 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3001 unsigned int xid;
3002 int rc;
3003 struct cifs_tcon *tcon;
3004 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3005 struct cifs_fid fid;
3006 struct cifs_open_parms oparms;
3007 __le16 *utf16_path;
3009 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
3010 if (IS_ERR(tlink))
3011 return ERR_CAST(tlink);
3013 tcon = tlink_tcon(tlink);
3014 xid = get_xid();
3016 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3017 if (!utf16_path) {
3018 rc = -ENOMEM;
3019 free_xid(xid);
3020 return ERR_PTR(rc);
3023 oparms.tcon = tcon;
3024 oparms.desired_access = READ_CONTROL;
3025 oparms.disposition = FILE_OPEN;
3026 oparms.create_options = cifs_create_options(cifs_sb, 0);
3027 oparms.fid = &fid;
3028 oparms.reconnect = false;
3030 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
3031 NULL);
3032 kfree(utf16_path);
3033 if (!rc) {
3034 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3035 fid.volatile_fid, (void **)&pntsd, pacllen);
3036 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3039 cifs_put_tlink(tlink);
3040 free_xid(xid);
3042 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3043 if (rc)
3044 return ERR_PTR(rc);
3045 return pntsd;
3048 static int
3049 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
3050 struct inode *inode, const char *path, int aclflag)
3052 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3053 unsigned int xid;
3054 int rc, access_flags = 0;
3055 struct cifs_tcon *tcon;
3056 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3057 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3058 struct cifs_fid fid;
3059 struct cifs_open_parms oparms;
3060 __le16 *utf16_path;
3062 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
3063 if (IS_ERR(tlink))
3064 return PTR_ERR(tlink);
3066 tcon = tlink_tcon(tlink);
3067 xid = get_xid();
3069 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
3070 access_flags = WRITE_OWNER;
3071 else
3072 access_flags = WRITE_DAC;
3074 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3075 if (!utf16_path) {
3076 rc = -ENOMEM;
3077 free_xid(xid);
3078 return rc;
3081 oparms.tcon = tcon;
3082 oparms.desired_access = access_flags;
3083 oparms.create_options = cifs_create_options(cifs_sb, 0);
3084 oparms.disposition = FILE_OPEN;
3085 oparms.path = path;
3086 oparms.fid = &fid;
3087 oparms.reconnect = false;
3089 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
3090 NULL, NULL);
3091 kfree(utf16_path);
3092 if (!rc) {
3093 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3094 fid.volatile_fid, pnntsd, acllen, aclflag);
3095 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3098 cifs_put_tlink(tlink);
3099 free_xid(xid);
3100 return rc;
3103 /* Retrieve an ACL from the server */
3104 static struct cifs_ntsd *
3105 get_smb2_acl(struct cifs_sb_info *cifs_sb,
3106 struct inode *inode, const char *path,
3107 u32 *pacllen)
3109 struct cifs_ntsd *pntsd = NULL;
3110 struct cifsFileInfo *open_file = NULL;
3112 if (inode)
3113 open_file = find_readable_file(CIFS_I(inode), true);
3114 if (!open_file)
3115 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
3117 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
3118 cifsFileInfo_put(open_file);
3119 return pntsd;
3122 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3123 loff_t offset, loff_t len, bool keep_size)
3125 struct cifs_ses *ses = tcon->ses;
3126 struct inode *inode;
3127 struct cifsInodeInfo *cifsi;
3128 struct cifsFileInfo *cfile = file->private_data;
3129 struct file_zero_data_information fsctl_buf;
3130 long rc;
3131 unsigned int xid;
3132 __le64 eof;
3134 xid = get_xid();
3136 inode = d_inode(cfile->dentry);
3137 cifsi = CIFS_I(inode);
3139 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3140 ses->Suid, offset, len);
3143 /* if file not oplocked can't be sure whether asking to extend size */
3144 if (!CIFS_CACHE_READ(cifsi))
3145 if (keep_size == false) {
3146 rc = -EOPNOTSUPP;
3147 trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
3148 tcon->tid, ses->Suid, offset, len, rc);
3149 free_xid(xid);
3150 return rc;
3153 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3155 fsctl_buf.FileOffset = cpu_to_le64(offset);
3156 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3158 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3159 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
3160 (char *)&fsctl_buf,
3161 sizeof(struct file_zero_data_information),
3162 0, NULL, NULL);
3163 if (rc)
3164 goto zero_range_exit;
3167 * do we also need to change the size of the file?
3169 if (keep_size == false && i_size_read(inode) < offset + len) {
3170 eof = cpu_to_le64(offset + len);
3171 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3172 cfile->fid.volatile_fid, cfile->pid, &eof);
3175 zero_range_exit:
3176 free_xid(xid);
3177 if (rc)
3178 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3179 ses->Suid, offset, len, rc);
3180 else
3181 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3182 ses->Suid, offset, len);
3183 return rc;
3186 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3187 loff_t offset, loff_t len)
3189 struct inode *inode;
3190 struct cifsFileInfo *cfile = file->private_data;
3191 struct file_zero_data_information fsctl_buf;
3192 long rc;
3193 unsigned int xid;
3194 __u8 set_sparse = 1;
3196 xid = get_xid();
3198 inode = d_inode(cfile->dentry);
3200 /* Need to make file sparse, if not already, before freeing range. */
3201 /* Consider adding equivalent for compressed since it could also work */
3202 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3203 rc = -EOPNOTSUPP;
3204 free_xid(xid);
3205 return rc;
3208 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3210 fsctl_buf.FileOffset = cpu_to_le64(offset);
3211 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3213 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3214 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3215 true /* is_fctl */, (char *)&fsctl_buf,
3216 sizeof(struct file_zero_data_information),
3217 CIFSMaxBufSize, NULL, NULL);
3218 free_xid(xid);
3219 return rc;
3222 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3223 loff_t off, loff_t len, bool keep_size)
3225 struct inode *inode;
3226 struct cifsInodeInfo *cifsi;
3227 struct cifsFileInfo *cfile = file->private_data;
3228 long rc = -EOPNOTSUPP;
3229 unsigned int xid;
3230 __le64 eof;
3232 xid = get_xid();
3234 inode = d_inode(cfile->dentry);
3235 cifsi = CIFS_I(inode);
3237 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3238 tcon->ses->Suid, off, len);
3239 /* if file not oplocked can't be sure whether asking to extend size */
3240 if (!CIFS_CACHE_READ(cifsi))
3241 if (keep_size == false) {
3242 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3243 tcon->tid, tcon->ses->Suid, off, len, rc);
3244 free_xid(xid);
3245 return rc;
3249 * Extending the file
3251 if ((keep_size == false) && i_size_read(inode) < off + len) {
3252 rc = inode_newsize_ok(inode, off + len);
3253 if (rc)
3254 goto out;
3256 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0)
3257 smb2_set_sparse(xid, tcon, cfile, inode, false);
3259 eof = cpu_to_le64(off + len);
3260 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3261 cfile->fid.volatile_fid, cfile->pid, &eof);
3262 if (rc == 0) {
3263 cifsi->server_eof = off + len;
3264 cifs_setsize(inode, off + len);
3265 cifs_truncate_page(inode->i_mapping, inode->i_size);
3266 truncate_setsize(inode, off + len);
3268 goto out;
3272 * Files are non-sparse by default so falloc may be a no-op
3273 * Must check if file sparse. If not sparse, and since we are not
3274 * extending then no need to do anything since file already allocated
3276 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
3277 rc = 0;
3278 goto out;
3281 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3283 * Check if falloc starts within first few pages of file
3284 * and ends within a few pages of the end of file to
3285 * ensure that most of file is being forced to be
3286 * fallocated now. If so then setting whole file sparse
3287 * ie potentially making a few extra pages at the beginning
3288 * or end of the file non-sparse via set_sparse is harmless.
3290 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3291 rc = -EOPNOTSUPP;
3292 goto out;
3296 smb2_set_sparse(xid, tcon, cfile, inode, false);
3297 rc = 0;
3299 out:
3300 if (rc)
3301 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3302 tcon->ses->Suid, off, len, rc);
3303 else
3304 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3305 tcon->ses->Suid, off, len);
3307 free_xid(xid);
3308 return rc;
3311 static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3313 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3314 struct cifsInodeInfo *cifsi;
3315 struct inode *inode;
3316 int rc = 0;
3317 struct file_allocated_range_buffer in_data, *out_data = NULL;
3318 u32 out_data_len;
3319 unsigned int xid;
3321 if (whence != SEEK_HOLE && whence != SEEK_DATA)
3322 return generic_file_llseek(file, offset, whence);
3324 inode = d_inode(cfile->dentry);
3325 cifsi = CIFS_I(inode);
3327 if (offset < 0 || offset >= i_size_read(inode))
3328 return -ENXIO;
3330 xid = get_xid();
3332 * We need to be sure that all dirty pages are written as they
3333 * might fill holes on the server.
3334 * Note that we also MUST flush any written pages since at least
3335 * some servers (Windows2016) will not reflect recent writes in
3336 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3338 wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
3339 if (wrcfile) {
3340 filemap_write_and_wait(inode->i_mapping);
3341 smb2_flush_file(xid, tcon, &wrcfile->fid);
3342 cifsFileInfo_put(wrcfile);
3345 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3346 if (whence == SEEK_HOLE)
3347 offset = i_size_read(inode);
3348 goto lseek_exit;
3351 in_data.file_offset = cpu_to_le64(offset);
3352 in_data.length = cpu_to_le64(i_size_read(inode));
3354 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3355 cfile->fid.volatile_fid,
3356 FSCTL_QUERY_ALLOCATED_RANGES, true,
3357 (char *)&in_data, sizeof(in_data),
3358 sizeof(struct file_allocated_range_buffer),
3359 (char **)&out_data, &out_data_len);
3360 if (rc == -E2BIG)
3361 rc = 0;
3362 if (rc)
3363 goto lseek_exit;
3365 if (whence == SEEK_HOLE && out_data_len == 0)
3366 goto lseek_exit;
3368 if (whence == SEEK_DATA && out_data_len == 0) {
3369 rc = -ENXIO;
3370 goto lseek_exit;
3373 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3374 rc = -EINVAL;
3375 goto lseek_exit;
3377 if (whence == SEEK_DATA) {
3378 offset = le64_to_cpu(out_data->file_offset);
3379 goto lseek_exit;
3381 if (offset < le64_to_cpu(out_data->file_offset))
3382 goto lseek_exit;
3384 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3386 lseek_exit:
3387 free_xid(xid);
3388 kfree(out_data);
3389 if (!rc)
3390 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3391 else
3392 return rc;
3395 static int smb3_fiemap(struct cifs_tcon *tcon,
3396 struct cifsFileInfo *cfile,
3397 struct fiemap_extent_info *fei, u64 start, u64 len)
3399 unsigned int xid;
3400 struct file_allocated_range_buffer in_data, *out_data;
3401 u32 out_data_len;
3402 int i, num, rc, flags, last_blob;
3403 u64 next;
3405 if (fiemap_check_flags(fei, FIEMAP_FLAG_SYNC))
3406 return -EBADR;
3408 xid = get_xid();
3409 again:
3410 in_data.file_offset = cpu_to_le64(start);
3411 in_data.length = cpu_to_le64(len);
3413 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3414 cfile->fid.volatile_fid,
3415 FSCTL_QUERY_ALLOCATED_RANGES, true,
3416 (char *)&in_data, sizeof(in_data),
3417 1024 * sizeof(struct file_allocated_range_buffer),
3418 (char **)&out_data, &out_data_len);
3419 if (rc == -E2BIG) {
3420 last_blob = 0;
3421 rc = 0;
3422 } else
3423 last_blob = 1;
3424 if (rc)
3425 goto out;
3427 if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) {
3428 rc = -EINVAL;
3429 goto out;
3431 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3432 rc = -EINVAL;
3433 goto out;
3436 num = out_data_len / sizeof(struct file_allocated_range_buffer);
3437 for (i = 0; i < num; i++) {
3438 flags = 0;
3439 if (i == num - 1 && last_blob)
3440 flags |= FIEMAP_EXTENT_LAST;
3442 rc = fiemap_fill_next_extent(fei,
3443 le64_to_cpu(out_data[i].file_offset),
3444 le64_to_cpu(out_data[i].file_offset),
3445 le64_to_cpu(out_data[i].length),
3446 flags);
3447 if (rc < 0)
3448 goto out;
3449 if (rc == 1) {
3450 rc = 0;
3451 goto out;
3455 if (!last_blob) {
3456 next = le64_to_cpu(out_data[num - 1].file_offset) +
3457 le64_to_cpu(out_data[num - 1].length);
3458 len = len - (next - start);
3459 start = next;
3460 goto again;
3463 out:
3464 free_xid(xid);
3465 kfree(out_data);
3466 return rc;
3469 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3470 loff_t off, loff_t len)
3472 /* KEEP_SIZE already checked for by do_fallocate */
3473 if (mode & FALLOC_FL_PUNCH_HOLE)
3474 return smb3_punch_hole(file, tcon, off, len);
3475 else if (mode & FALLOC_FL_ZERO_RANGE) {
3476 if (mode & FALLOC_FL_KEEP_SIZE)
3477 return smb3_zero_range(file, tcon, off, len, true);
3478 return smb3_zero_range(file, tcon, off, len, false);
3479 } else if (mode == FALLOC_FL_KEEP_SIZE)
3480 return smb3_simple_falloc(file, tcon, off, len, true);
3481 else if (mode == 0)
3482 return smb3_simple_falloc(file, tcon, off, len, false);
3484 return -EOPNOTSUPP;
3487 static void
3488 smb2_downgrade_oplock(struct TCP_Server_Info *server,
3489 struct cifsInodeInfo *cinode, __u32 oplock,
3490 unsigned int epoch, bool *purge_cache)
3492 server->ops->set_oplock_level(cinode, oplock, 0, NULL);
3495 static void
3496 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3497 unsigned int epoch, bool *purge_cache);
3499 static void
3500 smb3_downgrade_oplock(struct TCP_Server_Info *server,
3501 struct cifsInodeInfo *cinode, __u32 oplock,
3502 unsigned int epoch, bool *purge_cache)
3504 unsigned int old_state = cinode->oplock;
3505 unsigned int old_epoch = cinode->epoch;
3506 unsigned int new_state;
3508 if (epoch > old_epoch) {
3509 smb21_set_oplock_level(cinode, oplock, 0, NULL);
3510 cinode->epoch = epoch;
3513 new_state = cinode->oplock;
3514 *purge_cache = false;
3516 if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
3517 (new_state & CIFS_CACHE_READ_FLG) == 0)
3518 *purge_cache = true;
3519 else if (old_state == new_state && (epoch - old_epoch > 1))
3520 *purge_cache = true;
3523 static void
3524 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3525 unsigned int epoch, bool *purge_cache)
3527 oplock &= 0xFF;
3528 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3529 return;
3530 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3531 cinode->oplock = CIFS_CACHE_RHW_FLG;
3532 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3533 &cinode->vfs_inode);
3534 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
3535 cinode->oplock = CIFS_CACHE_RW_FLG;
3536 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3537 &cinode->vfs_inode);
3538 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3539 cinode->oplock = CIFS_CACHE_READ_FLG;
3540 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3541 &cinode->vfs_inode);
3542 } else
3543 cinode->oplock = 0;
3546 static void
3547 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3548 unsigned int epoch, bool *purge_cache)
3550 char message[5] = {0};
3551 unsigned int new_oplock = 0;
3553 oplock &= 0xFF;
3554 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3555 return;
3557 /* Check if the server granted an oplock rather than a lease */
3558 if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3559 return smb2_set_oplock_level(cinode, oplock, epoch,
3560 purge_cache);
3562 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
3563 new_oplock |= CIFS_CACHE_READ_FLG;
3564 strcat(message, "R");
3566 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
3567 new_oplock |= CIFS_CACHE_HANDLE_FLG;
3568 strcat(message, "H");
3570 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
3571 new_oplock |= CIFS_CACHE_WRITE_FLG;
3572 strcat(message, "W");
3574 if (!new_oplock)
3575 strncpy(message, "None", sizeof(message));
3577 cinode->oplock = new_oplock;
3578 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3579 &cinode->vfs_inode);
3582 static void
3583 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3584 unsigned int epoch, bool *purge_cache)
3586 unsigned int old_oplock = cinode->oplock;
3588 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3590 if (purge_cache) {
3591 *purge_cache = false;
3592 if (old_oplock == CIFS_CACHE_READ_FLG) {
3593 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3594 (epoch - cinode->epoch > 0))
3595 *purge_cache = true;
3596 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3597 (epoch - cinode->epoch > 1))
3598 *purge_cache = true;
3599 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3600 (epoch - cinode->epoch > 1))
3601 *purge_cache = true;
3602 else if (cinode->oplock == 0 &&
3603 (epoch - cinode->epoch > 0))
3604 *purge_cache = true;
3605 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
3606 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3607 (epoch - cinode->epoch > 0))
3608 *purge_cache = true;
3609 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3610 (epoch - cinode->epoch > 1))
3611 *purge_cache = true;
3613 cinode->epoch = epoch;
3617 static bool
3618 smb2_is_read_op(__u32 oplock)
3620 return oplock == SMB2_OPLOCK_LEVEL_II;
3623 static bool
3624 smb21_is_read_op(__u32 oplock)
3626 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
3627 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
3630 static __le32
3631 map_oplock_to_lease(u8 oplock)
3633 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3634 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
3635 else if (oplock == SMB2_OPLOCK_LEVEL_II)
3636 return SMB2_LEASE_READ_CACHING;
3637 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3638 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
3639 SMB2_LEASE_WRITE_CACHING;
3640 return 0;
3643 static char *
3644 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3646 struct create_lease *buf;
3648 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
3649 if (!buf)
3650 return NULL;
3652 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3653 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3655 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3656 (struct create_lease, lcontext));
3657 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
3658 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3659 (struct create_lease, Name));
3660 buf->ccontext.NameLength = cpu_to_le16(4);
3661 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3662 buf->Name[0] = 'R';
3663 buf->Name[1] = 'q';
3664 buf->Name[2] = 'L';
3665 buf->Name[3] = 's';
3666 return (char *)buf;
3669 static char *
3670 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
3672 struct create_lease_v2 *buf;
3674 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
3675 if (!buf)
3676 return NULL;
3678 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3679 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3681 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3682 (struct create_lease_v2, lcontext));
3683 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
3684 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3685 (struct create_lease_v2, Name));
3686 buf->ccontext.NameLength = cpu_to_le16(4);
3687 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3688 buf->Name[0] = 'R';
3689 buf->Name[1] = 'q';
3690 buf->Name[2] = 'L';
3691 buf->Name[3] = 's';
3692 return (char *)buf;
3695 static __u8
3696 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3698 struct create_lease *lc = (struct create_lease *)buf;
3700 *epoch = 0; /* not used */
3701 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3702 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3703 return le32_to_cpu(lc->lcontext.LeaseState);
3706 static __u8
3707 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3709 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
3711 *epoch = le16_to_cpu(lc->lcontext.Epoch);
3712 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3713 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3714 if (lease_key)
3715 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
3716 return le32_to_cpu(lc->lcontext.LeaseState);
3719 static unsigned int
3720 smb2_wp_retry_size(struct inode *inode)
3722 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
3723 SMB2_MAX_BUFFER_SIZE);
3726 static bool
3727 smb2_dir_needs_close(struct cifsFileInfo *cfile)
3729 return !cfile->invalidHandle;
3732 static void
3733 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
3734 struct smb_rqst *old_rq, __le16 cipher_type)
3736 struct smb2_sync_hdr *shdr =
3737 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
3739 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
3740 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
3741 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
3742 tr_hdr->Flags = cpu_to_le16(0x01);
3743 if (cipher_type == SMB2_ENCRYPTION_AES128_GCM)
3744 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128GCM_NONCE);
3745 else
3746 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CCM_NONCE);
3747 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
3750 /* We can not use the normal sg_set_buf() as we will sometimes pass a
3751 * stack object as buf.
3753 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
3754 unsigned int buflen)
3756 void *addr;
3758 * VMAP_STACK (at least) puts stack into the vmalloc address space
3760 if (is_vmalloc_addr(buf))
3761 addr = vmalloc_to_page(buf);
3762 else
3763 addr = virt_to_page(buf);
3764 sg_set_page(sg, addr, buflen, offset_in_page(buf));
3767 /* Assumes the first rqst has a transform header as the first iov.
3768 * I.e.
3769 * rqst[0].rq_iov[0] is transform header
3770 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
3771 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
3773 static struct scatterlist *
3774 init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
3776 unsigned int sg_len;
3777 struct scatterlist *sg;
3778 unsigned int i;
3779 unsigned int j;
3780 unsigned int idx = 0;
3781 int skip;
3783 sg_len = 1;
3784 for (i = 0; i < num_rqst; i++)
3785 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
3787 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
3788 if (!sg)
3789 return NULL;
3791 sg_init_table(sg, sg_len);
3792 for (i = 0; i < num_rqst; i++) {
3793 for (j = 0; j < rqst[i].rq_nvec; j++) {
3795 * The first rqst has a transform header where the
3796 * first 20 bytes are not part of the encrypted blob
3798 skip = (i == 0) && (j == 0) ? 20 : 0;
3799 smb2_sg_set_buf(&sg[idx++],
3800 rqst[i].rq_iov[j].iov_base + skip,
3801 rqst[i].rq_iov[j].iov_len - skip);
3804 for (j = 0; j < rqst[i].rq_npages; j++) {
3805 unsigned int len, offset;
3807 rqst_page_get_length(&rqst[i], j, &len, &offset);
3808 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
3811 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
3812 return sg;
3815 static int
3816 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
3818 struct cifs_ses *ses;
3819 u8 *ses_enc_key;
3821 spin_lock(&cifs_tcp_ses_lock);
3822 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
3823 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3824 if (ses->Suid == ses_id) {
3825 ses_enc_key = enc ? ses->smb3encryptionkey :
3826 ses->smb3decryptionkey;
3827 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
3828 spin_unlock(&cifs_tcp_ses_lock);
3829 return 0;
3833 spin_unlock(&cifs_tcp_ses_lock);
3835 return 1;
3838 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
3839 * iov[0] - transform header (associate data),
3840 * iov[1-N] - SMB2 header and pages - data to encrypt.
3841 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
3842 * untouched.
3844 static int
3845 crypt_message(struct TCP_Server_Info *server, int num_rqst,
3846 struct smb_rqst *rqst, int enc)
3848 struct smb2_transform_hdr *tr_hdr =
3849 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
3850 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
3851 int rc = 0;
3852 struct scatterlist *sg;
3853 u8 sign[SMB2_SIGNATURE_SIZE] = {};
3854 u8 key[SMB3_SIGN_KEY_SIZE];
3855 struct aead_request *req;
3856 char *iv;
3857 unsigned int iv_len;
3858 DECLARE_CRYPTO_WAIT(wait);
3859 struct crypto_aead *tfm;
3860 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3862 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
3863 if (rc) {
3864 cifs_server_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
3865 enc ? "en" : "de");
3866 return 0;
3869 rc = smb3_crypto_aead_allocate(server);
3870 if (rc) {
3871 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
3872 return rc;
3875 tfm = enc ? server->secmech.ccmaesencrypt :
3876 server->secmech.ccmaesdecrypt;
3877 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
3878 if (rc) {
3879 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
3880 return rc;
3883 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
3884 if (rc) {
3885 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
3886 return rc;
3889 req = aead_request_alloc(tfm, GFP_KERNEL);
3890 if (!req) {
3891 cifs_server_dbg(VFS, "%s: Failed to alloc aead request\n", __func__);
3892 return -ENOMEM;
3895 if (!enc) {
3896 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
3897 crypt_len += SMB2_SIGNATURE_SIZE;
3900 sg = init_sg(num_rqst, rqst, sign);
3901 if (!sg) {
3902 cifs_server_dbg(VFS, "%s: Failed to init sg\n", __func__);
3903 rc = -ENOMEM;
3904 goto free_req;
3907 iv_len = crypto_aead_ivsize(tfm);
3908 iv = kzalloc(iv_len, GFP_KERNEL);
3909 if (!iv) {
3910 cifs_server_dbg(VFS, "%s: Failed to alloc iv\n", __func__);
3911 rc = -ENOMEM;
3912 goto free_sg;
3915 if (server->cipher_type == SMB2_ENCRYPTION_AES128_GCM)
3916 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES128GCM_NONCE);
3917 else {
3918 iv[0] = 3;
3919 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CCM_NONCE);
3922 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
3923 aead_request_set_ad(req, assoc_data_len);
3925 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3926 crypto_req_done, &wait);
3928 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
3929 : crypto_aead_decrypt(req), &wait);
3931 if (!rc && enc)
3932 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
3934 kfree(iv);
3935 free_sg:
3936 kfree(sg);
3937 free_req:
3938 kfree(req);
3939 return rc;
3942 void
3943 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
3945 int i, j;
3947 for (i = 0; i < num_rqst; i++) {
3948 if (rqst[i].rq_pages) {
3949 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
3950 put_page(rqst[i].rq_pages[j]);
3951 kfree(rqst[i].rq_pages);
3957 * This function will initialize new_rq and encrypt the content.
3958 * The first entry, new_rq[0], only contains a single iov which contains
3959 * a smb2_transform_hdr and is pre-allocated by the caller.
3960 * This function then populates new_rq[1+] with the content from olq_rq[0+].
3962 * The end result is an array of smb_rqst structures where the first structure
3963 * only contains a single iov for the transform header which we then can pass
3964 * to crypt_message().
3966 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
3967 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
3969 static int
3970 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
3971 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
3973 struct page **pages;
3974 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
3975 unsigned int npages;
3976 unsigned int orig_len = 0;
3977 int i, j;
3978 int rc = -ENOMEM;
3980 for (i = 1; i < num_rqst; i++) {
3981 npages = old_rq[i - 1].rq_npages;
3982 pages = kmalloc_array(npages, sizeof(struct page *),
3983 GFP_KERNEL);
3984 if (!pages)
3985 goto err_free;
3987 new_rq[i].rq_pages = pages;
3988 new_rq[i].rq_npages = npages;
3989 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
3990 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
3991 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
3992 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
3993 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
3995 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
3997 for (j = 0; j < npages; j++) {
3998 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3999 if (!pages[j])
4000 goto err_free;
4003 /* copy pages form the old */
4004 for (j = 0; j < npages; j++) {
4005 char *dst, *src;
4006 unsigned int offset, len;
4008 rqst_page_get_length(&new_rq[i], j, &len, &offset);
4010 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
4011 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
4013 memcpy(dst, src, len);
4014 kunmap(new_rq[i].rq_pages[j]);
4015 kunmap(old_rq[i - 1].rq_pages[j]);
4019 /* fill the 1st iov with a transform header */
4020 fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
4022 rc = crypt_message(server, num_rqst, new_rq, 1);
4023 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
4024 if (rc)
4025 goto err_free;
4027 return rc;
4029 err_free:
4030 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
4031 return rc;
4034 static int
4035 smb3_is_transform_hdr(void *buf)
4037 struct smb2_transform_hdr *trhdr = buf;
4039 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
4042 static int
4043 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
4044 unsigned int buf_data_size, struct page **pages,
4045 unsigned int npages, unsigned int page_data_size)
4047 struct kvec iov[2];
4048 struct smb_rqst rqst = {NULL};
4049 int rc;
4051 iov[0].iov_base = buf;
4052 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
4053 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
4054 iov[1].iov_len = buf_data_size;
4056 rqst.rq_iov = iov;
4057 rqst.rq_nvec = 2;
4058 rqst.rq_pages = pages;
4059 rqst.rq_npages = npages;
4060 rqst.rq_pagesz = PAGE_SIZE;
4061 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
4063 rc = crypt_message(server, 1, &rqst, 0);
4064 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
4066 if (rc)
4067 return rc;
4069 memmove(buf, iov[1].iov_base, buf_data_size);
4071 server->total_read = buf_data_size + page_data_size;
4073 return rc;
4076 static int
4077 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
4078 unsigned int npages, unsigned int len)
4080 int i;
4081 int length;
4083 for (i = 0; i < npages; i++) {
4084 struct page *page = pages[i];
4085 size_t n;
4087 n = len;
4088 if (len >= PAGE_SIZE) {
4089 /* enough data to fill the page */
4090 n = PAGE_SIZE;
4091 len -= n;
4092 } else {
4093 zero_user(page, len, PAGE_SIZE - len);
4094 len = 0;
4096 length = cifs_read_page_from_socket(server, page, 0, n);
4097 if (length < 0)
4098 return length;
4099 server->total_read += length;
4102 return 0;
4105 static int
4106 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
4107 unsigned int cur_off, struct bio_vec **page_vec)
4109 struct bio_vec *bvec;
4110 int i;
4112 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
4113 if (!bvec)
4114 return -ENOMEM;
4116 for (i = 0; i < npages; i++) {
4117 bvec[i].bv_page = pages[i];
4118 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
4119 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
4120 data_size -= bvec[i].bv_len;
4123 if (data_size != 0) {
4124 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4125 kfree(bvec);
4126 return -EIO;
4129 *page_vec = bvec;
4130 return 0;
4133 static int
4134 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4135 char *buf, unsigned int buf_len, struct page **pages,
4136 unsigned int npages, unsigned int page_data_size)
4138 unsigned int data_offset;
4139 unsigned int data_len;
4140 unsigned int cur_off;
4141 unsigned int cur_page_idx;
4142 unsigned int pad_len;
4143 struct cifs_readdata *rdata = mid->callback_data;
4144 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
4145 struct bio_vec *bvec = NULL;
4146 struct iov_iter iter;
4147 struct kvec iov;
4148 int length;
4149 bool use_rdma_mr = false;
4151 if (shdr->Command != SMB2_READ) {
4152 cifs_server_dbg(VFS, "only big read responses are supported\n");
4153 return -ENOTSUPP;
4156 if (server->ops->is_session_expired &&
4157 server->ops->is_session_expired(buf)) {
4158 cifs_reconnect(server);
4159 return -1;
4162 if (server->ops->is_status_pending &&
4163 server->ops->is_status_pending(buf, server))
4164 return -1;
4166 /* set up first two iov to get credits */
4167 rdata->iov[0].iov_base = buf;
4168 rdata->iov[0].iov_len = 0;
4169 rdata->iov[1].iov_base = buf;
4170 rdata->iov[1].iov_len =
4171 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
4172 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4173 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4174 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4175 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4177 rdata->result = server->ops->map_error(buf, true);
4178 if (rdata->result != 0) {
4179 cifs_dbg(FYI, "%s: server returned error %d\n",
4180 __func__, rdata->result);
4181 /* normal error on read response */
4182 dequeue_mid(mid, false);
4183 return 0;
4186 data_offset = server->ops->read_data_offset(buf);
4187 #ifdef CONFIG_CIFS_SMB_DIRECT
4188 use_rdma_mr = rdata->mr;
4189 #endif
4190 data_len = server->ops->read_data_length(buf, use_rdma_mr);
4192 if (data_offset < server->vals->read_rsp_size) {
4194 * win2k8 sometimes sends an offset of 0 when the read
4195 * is beyond the EOF. Treat it as if the data starts just after
4196 * the header.
4198 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4199 __func__, data_offset);
4200 data_offset = server->vals->read_rsp_size;
4201 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4202 /* data_offset is beyond the end of smallbuf */
4203 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4204 __func__, data_offset);
4205 rdata->result = -EIO;
4206 dequeue_mid(mid, rdata->result);
4207 return 0;
4210 pad_len = data_offset - server->vals->read_rsp_size;
4212 if (buf_len <= data_offset) {
4213 /* read response payload is in pages */
4214 cur_page_idx = pad_len / PAGE_SIZE;
4215 cur_off = pad_len % PAGE_SIZE;
4217 if (cur_page_idx != 0) {
4218 /* data offset is beyond the 1st page of response */
4219 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4220 __func__, data_offset);
4221 rdata->result = -EIO;
4222 dequeue_mid(mid, rdata->result);
4223 return 0;
4226 if (data_len > page_data_size - pad_len) {
4227 /* data_len is corrupt -- discard frame */
4228 rdata->result = -EIO;
4229 dequeue_mid(mid, rdata->result);
4230 return 0;
4233 rdata->result = init_read_bvec(pages, npages, page_data_size,
4234 cur_off, &bvec);
4235 if (rdata->result != 0) {
4236 dequeue_mid(mid, rdata->result);
4237 return 0;
4240 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
4241 } else if (buf_len >= data_offset + data_len) {
4242 /* read response payload is in buf */
4243 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
4244 iov.iov_base = buf + data_offset;
4245 iov.iov_len = data_len;
4246 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
4247 } else {
4248 /* read response payload cannot be in both buf and pages */
4249 WARN_ONCE(1, "buf can not contain only a part of read data");
4250 rdata->result = -EIO;
4251 dequeue_mid(mid, rdata->result);
4252 return 0;
4255 length = rdata->copy_into_pages(server, rdata, &iter);
4257 kfree(bvec);
4259 if (length < 0)
4260 return length;
4262 dequeue_mid(mid, false);
4263 return length;
4266 struct smb2_decrypt_work {
4267 struct work_struct decrypt;
4268 struct TCP_Server_Info *server;
4269 struct page **ppages;
4270 char *buf;
4271 unsigned int npages;
4272 unsigned int len;
4276 static void smb2_decrypt_offload(struct work_struct *work)
4278 struct smb2_decrypt_work *dw = container_of(work,
4279 struct smb2_decrypt_work, decrypt);
4280 int i, rc;
4281 struct mid_q_entry *mid;
4283 rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
4284 dw->ppages, dw->npages, dw->len);
4285 if (rc) {
4286 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4287 goto free_pages;
4290 dw->server->lstrp = jiffies;
4291 mid = smb2_find_mid(dw->server, dw->buf);
4292 if (mid == NULL)
4293 cifs_dbg(FYI, "mid not found\n");
4294 else {
4295 mid->decrypted = true;
4296 rc = handle_read_data(dw->server, mid, dw->buf,
4297 dw->server->vals->read_rsp_size,
4298 dw->ppages, dw->npages, dw->len);
4299 mid->callback(mid);
4300 cifs_mid_q_entry_release(mid);
4303 free_pages:
4304 for (i = dw->npages-1; i >= 0; i--)
4305 put_page(dw->ppages[i]);
4307 kfree(dw->ppages);
4308 cifs_small_buf_release(dw->buf);
4309 kfree(dw);
4313 static int
4314 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4315 int *num_mids)
4317 char *buf = server->smallbuf;
4318 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4319 unsigned int npages;
4320 struct page **pages;
4321 unsigned int len;
4322 unsigned int buflen = server->pdu_size;
4323 int rc;
4324 int i = 0;
4325 struct smb2_decrypt_work *dw;
4327 *num_mids = 1;
4328 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
4329 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
4331 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
4332 if (rc < 0)
4333 return rc;
4334 server->total_read += rc;
4336 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
4337 server->vals->read_rsp_size;
4338 npages = DIV_ROUND_UP(len, PAGE_SIZE);
4340 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
4341 if (!pages) {
4342 rc = -ENOMEM;
4343 goto discard_data;
4346 for (; i < npages; i++) {
4347 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4348 if (!pages[i]) {
4349 rc = -ENOMEM;
4350 goto discard_data;
4354 /* read read data into pages */
4355 rc = read_data_into_pages(server, pages, npages, len);
4356 if (rc)
4357 goto free_pages;
4359 rc = cifs_discard_remaining_data(server);
4360 if (rc)
4361 goto free_pages;
4364 * For large reads, offload to different thread for better performance,
4365 * use more cores decrypting which can be expensive
4368 if ((server->min_offload) && (server->in_flight > 1) &&
4369 (server->pdu_size >= server->min_offload)) {
4370 dw = kmalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
4371 if (dw == NULL)
4372 goto non_offloaded_decrypt;
4374 dw->buf = server->smallbuf;
4375 server->smallbuf = (char *)cifs_small_buf_get();
4377 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
4379 dw->npages = npages;
4380 dw->server = server;
4381 dw->ppages = pages;
4382 dw->len = len;
4383 queue_work(decrypt_wq, &dw->decrypt);
4384 *num_mids = 0; /* worker thread takes care of finding mid */
4385 return -1;
4388 non_offloaded_decrypt:
4389 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
4390 pages, npages, len);
4391 if (rc)
4392 goto free_pages;
4394 *mid = smb2_find_mid(server, buf);
4395 if (*mid == NULL)
4396 cifs_dbg(FYI, "mid not found\n");
4397 else {
4398 cifs_dbg(FYI, "mid found\n");
4399 (*mid)->decrypted = true;
4400 rc = handle_read_data(server, *mid, buf,
4401 server->vals->read_rsp_size,
4402 pages, npages, len);
4405 free_pages:
4406 for (i = i - 1; i >= 0; i--)
4407 put_page(pages[i]);
4408 kfree(pages);
4409 return rc;
4410 discard_data:
4411 cifs_discard_remaining_data(server);
4412 goto free_pages;
4415 static int
4416 receive_encrypted_standard(struct TCP_Server_Info *server,
4417 struct mid_q_entry **mids, char **bufs,
4418 int *num_mids)
4420 int ret, length;
4421 char *buf = server->smallbuf;
4422 struct smb2_sync_hdr *shdr;
4423 unsigned int pdu_length = server->pdu_size;
4424 unsigned int buf_size;
4425 struct mid_q_entry *mid_entry;
4426 int next_is_large;
4427 char *next_buffer = NULL;
4429 *num_mids = 0;
4431 /* switch to large buffer if too big for a small one */
4432 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
4433 server->large_buf = true;
4434 memcpy(server->bigbuf, buf, server->total_read);
4435 buf = server->bigbuf;
4438 /* now read the rest */
4439 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
4440 pdu_length - HEADER_SIZE(server) + 1);
4441 if (length < 0)
4442 return length;
4443 server->total_read += length;
4445 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
4446 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
4447 if (length)
4448 return length;
4450 next_is_large = server->large_buf;
4451 one_more:
4452 shdr = (struct smb2_sync_hdr *)buf;
4453 if (shdr->NextCommand) {
4454 if (next_is_large)
4455 next_buffer = (char *)cifs_buf_get();
4456 else
4457 next_buffer = (char *)cifs_small_buf_get();
4458 memcpy(next_buffer,
4459 buf + le32_to_cpu(shdr->NextCommand),
4460 pdu_length - le32_to_cpu(shdr->NextCommand));
4463 mid_entry = smb2_find_mid(server, buf);
4464 if (mid_entry == NULL)
4465 cifs_dbg(FYI, "mid not found\n");
4466 else {
4467 cifs_dbg(FYI, "mid found\n");
4468 mid_entry->decrypted = true;
4469 mid_entry->resp_buf_size = server->pdu_size;
4472 if (*num_mids >= MAX_COMPOUND) {
4473 cifs_server_dbg(VFS, "too many PDUs in compound\n");
4474 return -1;
4476 bufs[*num_mids] = buf;
4477 mids[(*num_mids)++] = mid_entry;
4479 if (mid_entry && mid_entry->handle)
4480 ret = mid_entry->handle(server, mid_entry);
4481 else
4482 ret = cifs_handle_standard(server, mid_entry);
4484 if (ret == 0 && shdr->NextCommand) {
4485 pdu_length -= le32_to_cpu(shdr->NextCommand);
4486 server->large_buf = next_is_large;
4487 if (next_is_large)
4488 server->bigbuf = buf = next_buffer;
4489 else
4490 server->smallbuf = buf = next_buffer;
4491 goto one_more;
4492 } else if (ret != 0) {
4494 * ret != 0 here means that we didn't get to handle_mid() thus
4495 * server->smallbuf and server->bigbuf are still valid. We need
4496 * to free next_buffer because it is not going to be used
4497 * anywhere.
4499 if (next_is_large)
4500 free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
4501 else
4502 free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
4505 return ret;
4508 static int
4509 smb3_receive_transform(struct TCP_Server_Info *server,
4510 struct mid_q_entry **mids, char **bufs, int *num_mids)
4512 char *buf = server->smallbuf;
4513 unsigned int pdu_length = server->pdu_size;
4514 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4515 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4517 if (pdu_length < sizeof(struct smb2_transform_hdr) +
4518 sizeof(struct smb2_sync_hdr)) {
4519 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
4520 pdu_length);
4521 cifs_reconnect(server);
4522 return -ECONNABORTED;
4525 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
4526 cifs_server_dbg(VFS, "Transform message is broken\n");
4527 cifs_reconnect(server);
4528 return -ECONNABORTED;
4531 /* TODO: add support for compounds containing READ. */
4532 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
4533 return receive_encrypted_read(server, &mids[0], num_mids);
4536 return receive_encrypted_standard(server, mids, bufs, num_mids);
4540 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
4542 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
4544 return handle_read_data(server, mid, buf, server->pdu_size,
4545 NULL, 0, 0);
4548 static int
4549 smb2_next_header(char *buf)
4551 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
4552 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
4554 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
4555 return sizeof(struct smb2_transform_hdr) +
4556 le32_to_cpu(t_hdr->OriginalMessageSize);
4558 return le32_to_cpu(hdr->NextCommand);
4561 static int
4562 smb2_make_node(unsigned int xid, struct inode *inode,
4563 struct dentry *dentry, struct cifs_tcon *tcon,
4564 char *full_path, umode_t mode, dev_t dev)
4566 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4567 int rc = -EPERM;
4568 FILE_ALL_INFO *buf = NULL;
4569 struct cifs_io_parms io_parms;
4570 __u32 oplock = 0;
4571 struct cifs_fid fid;
4572 struct cifs_open_parms oparms;
4573 unsigned int bytes_written;
4574 struct win_dev *pdev;
4575 struct kvec iov[2];
4578 * Check if mounted with mount parm 'sfu' mount parm.
4579 * SFU emulation should work with all servers, but only
4580 * supports block and char device (no socket & fifo),
4581 * and was used by default in earlier versions of Windows
4583 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
4584 goto out;
4587 * TODO: Add ability to create instead via reparse point. Windows (e.g.
4588 * their current NFS server) uses this approach to expose special files
4589 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
4592 if (!S_ISCHR(mode) && !S_ISBLK(mode))
4593 goto out;
4595 cifs_dbg(FYI, "sfu compat create special file\n");
4597 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
4598 if (buf == NULL) {
4599 rc = -ENOMEM;
4600 goto out;
4603 oparms.tcon = tcon;
4604 oparms.cifs_sb = cifs_sb;
4605 oparms.desired_access = GENERIC_WRITE;
4606 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
4607 CREATE_OPTION_SPECIAL);
4608 oparms.disposition = FILE_CREATE;
4609 oparms.path = full_path;
4610 oparms.fid = &fid;
4611 oparms.reconnect = false;
4613 if (tcon->ses->server->oplocks)
4614 oplock = REQ_OPLOCK;
4615 else
4616 oplock = 0;
4617 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
4618 if (rc)
4619 goto out;
4622 * BB Do not bother to decode buf since no local inode yet to put
4623 * timestamps in, but we can reuse it safely.
4626 pdev = (struct win_dev *)buf;
4627 io_parms.pid = current->tgid;
4628 io_parms.tcon = tcon;
4629 io_parms.offset = 0;
4630 io_parms.length = sizeof(struct win_dev);
4631 iov[1].iov_base = buf;
4632 iov[1].iov_len = sizeof(struct win_dev);
4633 if (S_ISCHR(mode)) {
4634 memcpy(pdev->type, "IntxCHR", 8);
4635 pdev->major = cpu_to_le64(MAJOR(dev));
4636 pdev->minor = cpu_to_le64(MINOR(dev));
4637 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4638 &bytes_written, iov, 1);
4639 } else if (S_ISBLK(mode)) {
4640 memcpy(pdev->type, "IntxBLK", 8);
4641 pdev->major = cpu_to_le64(MAJOR(dev));
4642 pdev->minor = cpu_to_le64(MINOR(dev));
4643 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4644 &bytes_written, iov, 1);
4646 tcon->ses->server->ops->close(xid, tcon, &fid);
4647 d_drop(dentry);
4649 /* FIXME: add code here to set EAs */
4650 out:
4651 kfree(buf);
4652 return rc;
4656 struct smb_version_operations smb20_operations = {
4657 .compare_fids = smb2_compare_fids,
4658 .setup_request = smb2_setup_request,
4659 .setup_async_request = smb2_setup_async_request,
4660 .check_receive = smb2_check_receive,
4661 .add_credits = smb2_add_credits,
4662 .set_credits = smb2_set_credits,
4663 .get_credits_field = smb2_get_credits_field,
4664 .get_credits = smb2_get_credits,
4665 .wait_mtu_credits = cifs_wait_mtu_credits,
4666 .get_next_mid = smb2_get_next_mid,
4667 .revert_current_mid = smb2_revert_current_mid,
4668 .read_data_offset = smb2_read_data_offset,
4669 .read_data_length = smb2_read_data_length,
4670 .map_error = map_smb2_to_linux_error,
4671 .find_mid = smb2_find_mid,
4672 .check_message = smb2_check_message,
4673 .dump_detail = smb2_dump_detail,
4674 .clear_stats = smb2_clear_stats,
4675 .print_stats = smb2_print_stats,
4676 .is_oplock_break = smb2_is_valid_oplock_break,
4677 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4678 .downgrade_oplock = smb2_downgrade_oplock,
4679 .need_neg = smb2_need_neg,
4680 .negotiate = smb2_negotiate,
4681 .negotiate_wsize = smb2_negotiate_wsize,
4682 .negotiate_rsize = smb2_negotiate_rsize,
4683 .sess_setup = SMB2_sess_setup,
4684 .logoff = SMB2_logoff,
4685 .tree_connect = SMB2_tcon,
4686 .tree_disconnect = SMB2_tdis,
4687 .qfs_tcon = smb2_qfs_tcon,
4688 .is_path_accessible = smb2_is_path_accessible,
4689 .can_echo = smb2_can_echo,
4690 .echo = SMB2_echo,
4691 .query_path_info = smb2_query_path_info,
4692 .get_srv_inum = smb2_get_srv_inum,
4693 .query_file_info = smb2_query_file_info,
4694 .set_path_size = smb2_set_path_size,
4695 .set_file_size = smb2_set_file_size,
4696 .set_file_info = smb2_set_file_info,
4697 .set_compression = smb2_set_compression,
4698 .mkdir = smb2_mkdir,
4699 .mkdir_setinfo = smb2_mkdir_setinfo,
4700 .rmdir = smb2_rmdir,
4701 .unlink = smb2_unlink,
4702 .rename = smb2_rename_path,
4703 .create_hardlink = smb2_create_hardlink,
4704 .query_symlink = smb2_query_symlink,
4705 .query_mf_symlink = smb3_query_mf_symlink,
4706 .create_mf_symlink = smb3_create_mf_symlink,
4707 .open = smb2_open_file,
4708 .set_fid = smb2_set_fid,
4709 .close = smb2_close_file,
4710 .flush = smb2_flush_file,
4711 .async_readv = smb2_async_readv,
4712 .async_writev = smb2_async_writev,
4713 .sync_read = smb2_sync_read,
4714 .sync_write = smb2_sync_write,
4715 .query_dir_first = smb2_query_dir_first,
4716 .query_dir_next = smb2_query_dir_next,
4717 .close_dir = smb2_close_dir,
4718 .calc_smb_size = smb2_calc_size,
4719 .is_status_pending = smb2_is_status_pending,
4720 .is_session_expired = smb2_is_session_expired,
4721 .oplock_response = smb2_oplock_response,
4722 .queryfs = smb2_queryfs,
4723 .mand_lock = smb2_mand_lock,
4724 .mand_unlock_range = smb2_unlock_range,
4725 .push_mand_locks = smb2_push_mandatory_locks,
4726 .get_lease_key = smb2_get_lease_key,
4727 .set_lease_key = smb2_set_lease_key,
4728 .new_lease_key = smb2_new_lease_key,
4729 .calc_signature = smb2_calc_signature,
4730 .is_read_op = smb2_is_read_op,
4731 .set_oplock_level = smb2_set_oplock_level,
4732 .create_lease_buf = smb2_create_lease_buf,
4733 .parse_lease_buf = smb2_parse_lease_buf,
4734 .copychunk_range = smb2_copychunk_range,
4735 .wp_retry_size = smb2_wp_retry_size,
4736 .dir_needs_close = smb2_dir_needs_close,
4737 .get_dfs_refer = smb2_get_dfs_refer,
4738 .select_sectype = smb2_select_sectype,
4739 #ifdef CONFIG_CIFS_XATTR
4740 .query_all_EAs = smb2_query_eas,
4741 .set_EA = smb2_set_ea,
4742 #endif /* CIFS_XATTR */
4743 .get_acl = get_smb2_acl,
4744 .get_acl_by_fid = get_smb2_acl_by_fid,
4745 .set_acl = set_smb2_acl,
4746 .next_header = smb2_next_header,
4747 .ioctl_query_info = smb2_ioctl_query_info,
4748 .make_node = smb2_make_node,
4749 .fiemap = smb3_fiemap,
4750 .llseek = smb3_llseek,
4753 struct smb_version_operations smb21_operations = {
4754 .compare_fids = smb2_compare_fids,
4755 .setup_request = smb2_setup_request,
4756 .setup_async_request = smb2_setup_async_request,
4757 .check_receive = smb2_check_receive,
4758 .add_credits = smb2_add_credits,
4759 .set_credits = smb2_set_credits,
4760 .get_credits_field = smb2_get_credits_field,
4761 .get_credits = smb2_get_credits,
4762 .wait_mtu_credits = smb2_wait_mtu_credits,
4763 .adjust_credits = smb2_adjust_credits,
4764 .get_next_mid = smb2_get_next_mid,
4765 .revert_current_mid = smb2_revert_current_mid,
4766 .read_data_offset = smb2_read_data_offset,
4767 .read_data_length = smb2_read_data_length,
4768 .map_error = map_smb2_to_linux_error,
4769 .find_mid = smb2_find_mid,
4770 .check_message = smb2_check_message,
4771 .dump_detail = smb2_dump_detail,
4772 .clear_stats = smb2_clear_stats,
4773 .print_stats = smb2_print_stats,
4774 .is_oplock_break = smb2_is_valid_oplock_break,
4775 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4776 .downgrade_oplock = smb2_downgrade_oplock,
4777 .need_neg = smb2_need_neg,
4778 .negotiate = smb2_negotiate,
4779 .negotiate_wsize = smb2_negotiate_wsize,
4780 .negotiate_rsize = smb2_negotiate_rsize,
4781 .sess_setup = SMB2_sess_setup,
4782 .logoff = SMB2_logoff,
4783 .tree_connect = SMB2_tcon,
4784 .tree_disconnect = SMB2_tdis,
4785 .qfs_tcon = smb2_qfs_tcon,
4786 .is_path_accessible = smb2_is_path_accessible,
4787 .can_echo = smb2_can_echo,
4788 .echo = SMB2_echo,
4789 .query_path_info = smb2_query_path_info,
4790 .get_srv_inum = smb2_get_srv_inum,
4791 .query_file_info = smb2_query_file_info,
4792 .set_path_size = smb2_set_path_size,
4793 .set_file_size = smb2_set_file_size,
4794 .set_file_info = smb2_set_file_info,
4795 .set_compression = smb2_set_compression,
4796 .mkdir = smb2_mkdir,
4797 .mkdir_setinfo = smb2_mkdir_setinfo,
4798 .rmdir = smb2_rmdir,
4799 .unlink = smb2_unlink,
4800 .rename = smb2_rename_path,
4801 .create_hardlink = smb2_create_hardlink,
4802 .query_symlink = smb2_query_symlink,
4803 .query_mf_symlink = smb3_query_mf_symlink,
4804 .create_mf_symlink = smb3_create_mf_symlink,
4805 .open = smb2_open_file,
4806 .set_fid = smb2_set_fid,
4807 .close = smb2_close_file,
4808 .flush = smb2_flush_file,
4809 .async_readv = smb2_async_readv,
4810 .async_writev = smb2_async_writev,
4811 .sync_read = smb2_sync_read,
4812 .sync_write = smb2_sync_write,
4813 .query_dir_first = smb2_query_dir_first,
4814 .query_dir_next = smb2_query_dir_next,
4815 .close_dir = smb2_close_dir,
4816 .calc_smb_size = smb2_calc_size,
4817 .is_status_pending = smb2_is_status_pending,
4818 .is_session_expired = smb2_is_session_expired,
4819 .oplock_response = smb2_oplock_response,
4820 .queryfs = smb2_queryfs,
4821 .mand_lock = smb2_mand_lock,
4822 .mand_unlock_range = smb2_unlock_range,
4823 .push_mand_locks = smb2_push_mandatory_locks,
4824 .get_lease_key = smb2_get_lease_key,
4825 .set_lease_key = smb2_set_lease_key,
4826 .new_lease_key = smb2_new_lease_key,
4827 .calc_signature = smb2_calc_signature,
4828 .is_read_op = smb21_is_read_op,
4829 .set_oplock_level = smb21_set_oplock_level,
4830 .create_lease_buf = smb2_create_lease_buf,
4831 .parse_lease_buf = smb2_parse_lease_buf,
4832 .copychunk_range = smb2_copychunk_range,
4833 .wp_retry_size = smb2_wp_retry_size,
4834 .dir_needs_close = smb2_dir_needs_close,
4835 .enum_snapshots = smb3_enum_snapshots,
4836 .notify = smb3_notify,
4837 .get_dfs_refer = smb2_get_dfs_refer,
4838 .select_sectype = smb2_select_sectype,
4839 #ifdef CONFIG_CIFS_XATTR
4840 .query_all_EAs = smb2_query_eas,
4841 .set_EA = smb2_set_ea,
4842 #endif /* CIFS_XATTR */
4843 .get_acl = get_smb2_acl,
4844 .get_acl_by_fid = get_smb2_acl_by_fid,
4845 .set_acl = set_smb2_acl,
4846 .next_header = smb2_next_header,
4847 .ioctl_query_info = smb2_ioctl_query_info,
4848 .make_node = smb2_make_node,
4849 .fiemap = smb3_fiemap,
4850 .llseek = smb3_llseek,
4853 struct smb_version_operations smb30_operations = {
4854 .compare_fids = smb2_compare_fids,
4855 .setup_request = smb2_setup_request,
4856 .setup_async_request = smb2_setup_async_request,
4857 .check_receive = smb2_check_receive,
4858 .add_credits = smb2_add_credits,
4859 .set_credits = smb2_set_credits,
4860 .get_credits_field = smb2_get_credits_field,
4861 .get_credits = smb2_get_credits,
4862 .wait_mtu_credits = smb2_wait_mtu_credits,
4863 .adjust_credits = smb2_adjust_credits,
4864 .get_next_mid = smb2_get_next_mid,
4865 .revert_current_mid = smb2_revert_current_mid,
4866 .read_data_offset = smb2_read_data_offset,
4867 .read_data_length = smb2_read_data_length,
4868 .map_error = map_smb2_to_linux_error,
4869 .find_mid = smb2_find_mid,
4870 .check_message = smb2_check_message,
4871 .dump_detail = smb2_dump_detail,
4872 .clear_stats = smb2_clear_stats,
4873 .print_stats = smb2_print_stats,
4874 .dump_share_caps = smb2_dump_share_caps,
4875 .is_oplock_break = smb2_is_valid_oplock_break,
4876 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4877 .downgrade_oplock = smb3_downgrade_oplock,
4878 .need_neg = smb2_need_neg,
4879 .negotiate = smb2_negotiate,
4880 .negotiate_wsize = smb3_negotiate_wsize,
4881 .negotiate_rsize = smb3_negotiate_rsize,
4882 .sess_setup = SMB2_sess_setup,
4883 .logoff = SMB2_logoff,
4884 .tree_connect = SMB2_tcon,
4885 .tree_disconnect = SMB2_tdis,
4886 .qfs_tcon = smb3_qfs_tcon,
4887 .is_path_accessible = smb2_is_path_accessible,
4888 .can_echo = smb2_can_echo,
4889 .echo = SMB2_echo,
4890 .query_path_info = smb2_query_path_info,
4891 .get_srv_inum = smb2_get_srv_inum,
4892 .query_file_info = smb2_query_file_info,
4893 .set_path_size = smb2_set_path_size,
4894 .set_file_size = smb2_set_file_size,
4895 .set_file_info = smb2_set_file_info,
4896 .set_compression = smb2_set_compression,
4897 .mkdir = smb2_mkdir,
4898 .mkdir_setinfo = smb2_mkdir_setinfo,
4899 .rmdir = smb2_rmdir,
4900 .unlink = smb2_unlink,
4901 .rename = smb2_rename_path,
4902 .create_hardlink = smb2_create_hardlink,
4903 .query_symlink = smb2_query_symlink,
4904 .query_mf_symlink = smb3_query_mf_symlink,
4905 .create_mf_symlink = smb3_create_mf_symlink,
4906 .open = smb2_open_file,
4907 .set_fid = smb2_set_fid,
4908 .close = smb2_close_file,
4909 .close_getattr = smb2_close_getattr,
4910 .flush = smb2_flush_file,
4911 .async_readv = smb2_async_readv,
4912 .async_writev = smb2_async_writev,
4913 .sync_read = smb2_sync_read,
4914 .sync_write = smb2_sync_write,
4915 .query_dir_first = smb2_query_dir_first,
4916 .query_dir_next = smb2_query_dir_next,
4917 .close_dir = smb2_close_dir,
4918 .calc_smb_size = smb2_calc_size,
4919 .is_status_pending = smb2_is_status_pending,
4920 .is_session_expired = smb2_is_session_expired,
4921 .oplock_response = smb2_oplock_response,
4922 .queryfs = smb2_queryfs,
4923 .mand_lock = smb2_mand_lock,
4924 .mand_unlock_range = smb2_unlock_range,
4925 .push_mand_locks = smb2_push_mandatory_locks,
4926 .get_lease_key = smb2_get_lease_key,
4927 .set_lease_key = smb2_set_lease_key,
4928 .new_lease_key = smb2_new_lease_key,
4929 .generate_signingkey = generate_smb30signingkey,
4930 .calc_signature = smb3_calc_signature,
4931 .set_integrity = smb3_set_integrity,
4932 .is_read_op = smb21_is_read_op,
4933 .set_oplock_level = smb3_set_oplock_level,
4934 .create_lease_buf = smb3_create_lease_buf,
4935 .parse_lease_buf = smb3_parse_lease_buf,
4936 .copychunk_range = smb2_copychunk_range,
4937 .duplicate_extents = smb2_duplicate_extents,
4938 .validate_negotiate = smb3_validate_negotiate,
4939 .wp_retry_size = smb2_wp_retry_size,
4940 .dir_needs_close = smb2_dir_needs_close,
4941 .fallocate = smb3_fallocate,
4942 .enum_snapshots = smb3_enum_snapshots,
4943 .notify = smb3_notify,
4944 .init_transform_rq = smb3_init_transform_rq,
4945 .is_transform_hdr = smb3_is_transform_hdr,
4946 .receive_transform = smb3_receive_transform,
4947 .get_dfs_refer = smb2_get_dfs_refer,
4948 .select_sectype = smb2_select_sectype,
4949 #ifdef CONFIG_CIFS_XATTR
4950 .query_all_EAs = smb2_query_eas,
4951 .set_EA = smb2_set_ea,
4952 #endif /* CIFS_XATTR */
4953 .get_acl = get_smb2_acl,
4954 .get_acl_by_fid = get_smb2_acl_by_fid,
4955 .set_acl = set_smb2_acl,
4956 .next_header = smb2_next_header,
4957 .ioctl_query_info = smb2_ioctl_query_info,
4958 .make_node = smb2_make_node,
4959 .fiemap = smb3_fiemap,
4960 .llseek = smb3_llseek,
4963 struct smb_version_operations smb311_operations = {
4964 .compare_fids = smb2_compare_fids,
4965 .setup_request = smb2_setup_request,
4966 .setup_async_request = smb2_setup_async_request,
4967 .check_receive = smb2_check_receive,
4968 .add_credits = smb2_add_credits,
4969 .set_credits = smb2_set_credits,
4970 .get_credits_field = smb2_get_credits_field,
4971 .get_credits = smb2_get_credits,
4972 .wait_mtu_credits = smb2_wait_mtu_credits,
4973 .adjust_credits = smb2_adjust_credits,
4974 .get_next_mid = smb2_get_next_mid,
4975 .revert_current_mid = smb2_revert_current_mid,
4976 .read_data_offset = smb2_read_data_offset,
4977 .read_data_length = smb2_read_data_length,
4978 .map_error = map_smb2_to_linux_error,
4979 .find_mid = smb2_find_mid,
4980 .check_message = smb2_check_message,
4981 .dump_detail = smb2_dump_detail,
4982 .clear_stats = smb2_clear_stats,
4983 .print_stats = smb2_print_stats,
4984 .dump_share_caps = smb2_dump_share_caps,
4985 .is_oplock_break = smb2_is_valid_oplock_break,
4986 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4987 .downgrade_oplock = smb3_downgrade_oplock,
4988 .need_neg = smb2_need_neg,
4989 .negotiate = smb2_negotiate,
4990 .negotiate_wsize = smb3_negotiate_wsize,
4991 .negotiate_rsize = smb3_negotiate_rsize,
4992 .sess_setup = SMB2_sess_setup,
4993 .logoff = SMB2_logoff,
4994 .tree_connect = SMB2_tcon,
4995 .tree_disconnect = SMB2_tdis,
4996 .qfs_tcon = smb3_qfs_tcon,
4997 .is_path_accessible = smb2_is_path_accessible,
4998 .can_echo = smb2_can_echo,
4999 .echo = SMB2_echo,
5000 .query_path_info = smb2_query_path_info,
5001 .get_srv_inum = smb2_get_srv_inum,
5002 .query_file_info = smb2_query_file_info,
5003 .set_path_size = smb2_set_path_size,
5004 .set_file_size = smb2_set_file_size,
5005 .set_file_info = smb2_set_file_info,
5006 .set_compression = smb2_set_compression,
5007 .mkdir = smb2_mkdir,
5008 .mkdir_setinfo = smb2_mkdir_setinfo,
5009 .posix_mkdir = smb311_posix_mkdir,
5010 .rmdir = smb2_rmdir,
5011 .unlink = smb2_unlink,
5012 .rename = smb2_rename_path,
5013 .create_hardlink = smb2_create_hardlink,
5014 .query_symlink = smb2_query_symlink,
5015 .query_mf_symlink = smb3_query_mf_symlink,
5016 .create_mf_symlink = smb3_create_mf_symlink,
5017 .open = smb2_open_file,
5018 .set_fid = smb2_set_fid,
5019 .close = smb2_close_file,
5020 .close_getattr = smb2_close_getattr,
5021 .flush = smb2_flush_file,
5022 .async_readv = smb2_async_readv,
5023 .async_writev = smb2_async_writev,
5024 .sync_read = smb2_sync_read,
5025 .sync_write = smb2_sync_write,
5026 .query_dir_first = smb2_query_dir_first,
5027 .query_dir_next = smb2_query_dir_next,
5028 .close_dir = smb2_close_dir,
5029 .calc_smb_size = smb2_calc_size,
5030 .is_status_pending = smb2_is_status_pending,
5031 .is_session_expired = smb2_is_session_expired,
5032 .oplock_response = smb2_oplock_response,
5033 .queryfs = smb311_queryfs,
5034 .mand_lock = smb2_mand_lock,
5035 .mand_unlock_range = smb2_unlock_range,
5036 .push_mand_locks = smb2_push_mandatory_locks,
5037 .get_lease_key = smb2_get_lease_key,
5038 .set_lease_key = smb2_set_lease_key,
5039 .new_lease_key = smb2_new_lease_key,
5040 .generate_signingkey = generate_smb311signingkey,
5041 .calc_signature = smb3_calc_signature,
5042 .set_integrity = smb3_set_integrity,
5043 .is_read_op = smb21_is_read_op,
5044 .set_oplock_level = smb3_set_oplock_level,
5045 .create_lease_buf = smb3_create_lease_buf,
5046 .parse_lease_buf = smb3_parse_lease_buf,
5047 .copychunk_range = smb2_copychunk_range,
5048 .duplicate_extents = smb2_duplicate_extents,
5049 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5050 .wp_retry_size = smb2_wp_retry_size,
5051 .dir_needs_close = smb2_dir_needs_close,
5052 .fallocate = smb3_fallocate,
5053 .enum_snapshots = smb3_enum_snapshots,
5054 .notify = smb3_notify,
5055 .init_transform_rq = smb3_init_transform_rq,
5056 .is_transform_hdr = smb3_is_transform_hdr,
5057 .receive_transform = smb3_receive_transform,
5058 .get_dfs_refer = smb2_get_dfs_refer,
5059 .select_sectype = smb2_select_sectype,
5060 #ifdef CONFIG_CIFS_XATTR
5061 .query_all_EAs = smb2_query_eas,
5062 .set_EA = smb2_set_ea,
5063 #endif /* CIFS_XATTR */
5064 .get_acl = get_smb2_acl,
5065 .get_acl_by_fid = get_smb2_acl_by_fid,
5066 .set_acl = set_smb2_acl,
5067 .next_header = smb2_next_header,
5068 .ioctl_query_info = smb2_ioctl_query_info,
5069 .make_node = smb2_make_node,
5070 .fiemap = smb3_fiemap,
5071 .llseek = smb3_llseek,
5074 struct smb_version_values smb20_values = {
5075 .version_string = SMB20_VERSION_STRING,
5076 .protocol_id = SMB20_PROT_ID,
5077 .req_capabilities = 0, /* MBZ */
5078 .large_lock_type = 0,
5079 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5080 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5081 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5082 .header_size = sizeof(struct smb2_sync_hdr),
5083 .header_preamble_size = 0,
5084 .max_header_size = MAX_SMB2_HDR_SIZE,
5085 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5086 .lock_cmd = SMB2_LOCK,
5087 .cap_unix = 0,
5088 .cap_nt_find = SMB2_NT_FIND,
5089 .cap_large_files = SMB2_LARGE_FILES,
5090 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5091 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5092 .create_lease_size = sizeof(struct create_lease),
5095 struct smb_version_values smb21_values = {
5096 .version_string = SMB21_VERSION_STRING,
5097 .protocol_id = SMB21_PROT_ID,
5098 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5099 .large_lock_type = 0,
5100 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5101 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5102 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5103 .header_size = sizeof(struct smb2_sync_hdr),
5104 .header_preamble_size = 0,
5105 .max_header_size = MAX_SMB2_HDR_SIZE,
5106 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5107 .lock_cmd = SMB2_LOCK,
5108 .cap_unix = 0,
5109 .cap_nt_find = SMB2_NT_FIND,
5110 .cap_large_files = SMB2_LARGE_FILES,
5111 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5112 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5113 .create_lease_size = sizeof(struct create_lease),
5116 struct smb_version_values smb3any_values = {
5117 .version_string = SMB3ANY_VERSION_STRING,
5118 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5119 .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,
5120 .large_lock_type = 0,
5121 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5122 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5123 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5124 .header_size = sizeof(struct smb2_sync_hdr),
5125 .header_preamble_size = 0,
5126 .max_header_size = MAX_SMB2_HDR_SIZE,
5127 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5128 .lock_cmd = SMB2_LOCK,
5129 .cap_unix = 0,
5130 .cap_nt_find = SMB2_NT_FIND,
5131 .cap_large_files = SMB2_LARGE_FILES,
5132 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5133 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5134 .create_lease_size = sizeof(struct create_lease_v2),
5137 struct smb_version_values smbdefault_values = {
5138 .version_string = SMBDEFAULT_VERSION_STRING,
5139 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5140 .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,
5141 .large_lock_type = 0,
5142 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5143 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5144 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5145 .header_size = sizeof(struct smb2_sync_hdr),
5146 .header_preamble_size = 0,
5147 .max_header_size = MAX_SMB2_HDR_SIZE,
5148 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5149 .lock_cmd = SMB2_LOCK,
5150 .cap_unix = 0,
5151 .cap_nt_find = SMB2_NT_FIND,
5152 .cap_large_files = SMB2_LARGE_FILES,
5153 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5154 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5155 .create_lease_size = sizeof(struct create_lease_v2),
5158 struct smb_version_values smb30_values = {
5159 .version_string = SMB30_VERSION_STRING,
5160 .protocol_id = SMB30_PROT_ID,
5161 .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,
5162 .large_lock_type = 0,
5163 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5164 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5165 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5166 .header_size = sizeof(struct smb2_sync_hdr),
5167 .header_preamble_size = 0,
5168 .max_header_size = MAX_SMB2_HDR_SIZE,
5169 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5170 .lock_cmd = SMB2_LOCK,
5171 .cap_unix = 0,
5172 .cap_nt_find = SMB2_NT_FIND,
5173 .cap_large_files = SMB2_LARGE_FILES,
5174 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5175 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5176 .create_lease_size = sizeof(struct create_lease_v2),
5179 struct smb_version_values smb302_values = {
5180 .version_string = SMB302_VERSION_STRING,
5181 .protocol_id = SMB302_PROT_ID,
5182 .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,
5183 .large_lock_type = 0,
5184 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5185 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5186 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5187 .header_size = sizeof(struct smb2_sync_hdr),
5188 .header_preamble_size = 0,
5189 .max_header_size = MAX_SMB2_HDR_SIZE,
5190 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5191 .lock_cmd = SMB2_LOCK,
5192 .cap_unix = 0,
5193 .cap_nt_find = SMB2_NT_FIND,
5194 .cap_large_files = SMB2_LARGE_FILES,
5195 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5196 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5197 .create_lease_size = sizeof(struct create_lease_v2),
5200 struct smb_version_values smb311_values = {
5201 .version_string = SMB311_VERSION_STRING,
5202 .protocol_id = SMB311_PROT_ID,
5203 .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,
5204 .large_lock_type = 0,
5205 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5206 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5207 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5208 .header_size = sizeof(struct smb2_sync_hdr),
5209 .header_preamble_size = 0,
5210 .max_header_size = MAX_SMB2_HDR_SIZE,
5211 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5212 .lock_cmd = SMB2_LOCK,
5213 .cap_unix = 0,
5214 .cap_nt_find = SMB2_NT_FIND,
5215 .cap_large_files = SMB2_LARGE_FILES,
5216 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5217 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5218 .create_lease_size = sizeof(struct create_lease_v2),