1 // SPDX-License-Identifier: GPL-2.0
3 * SMB2 version specific operations
8 #include <linux/pagemap.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 <linux/fiemap.h>
19 #include "smb2proto.h"
20 #include "cifsproto.h"
21 #include "cifs_debug.h"
22 #include "cifs_unicode.h"
23 #include "smb2status.h"
25 #include "cifs_ioctl.h"
26 #include "smbdirect.h"
28 /* Change credits for different ops and return the total number of credits */
30 change_conf(struct TCP_Server_Info *server)
32 server->credits += server->echo_credits + server->oplock_credits;
33 server->oplock_credits = server->echo_credits = 0;
34 switch (server->credits) {
38 server->echoes = false;
39 server->oplocks = false;
42 server->echoes = true;
43 server->oplocks = false;
44 server->echo_credits = 1;
47 server->echoes = true;
49 server->oplocks = true;
50 server->oplock_credits = 1;
52 server->oplocks = false;
54 server->echo_credits = 1;
56 server->credits -= server->echo_credits + server->oplock_credits;
57 return server->credits + server->echo_credits + server->oplock_credits;
61 smb2_add_credits(struct TCP_Server_Info *server,
62 const struct cifs_credits *credits, const int optype)
65 unsigned int add = credits->value;
66 unsigned int instance = credits->instance;
67 bool reconnect_detected = false;
69 spin_lock(&server->req_lock);
70 val = server->ops->get_credits_field(server, optype);
72 /* eg found case where write overlapping reconnect messed up credits */
73 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
74 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
75 server->hostname, *val, add);
76 if ((instance == 0) || (instance == server->reconnect_instance))
79 reconnect_detected = true;
82 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
83 pr_warn_once("server overflowed SMB3 credits\n");
86 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
87 rc = change_conf(server);
89 * Sometimes server returns 0 credits on oplock break ack - we need to
90 * rebalance credits in this case.
92 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
94 if (server->credits > 1) {
96 server->oplock_credits++;
99 spin_unlock(&server->req_lock);
100 wake_up(&server->request_q);
102 if (reconnect_detected)
103 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
106 if (server->tcpStatus == CifsNeedReconnect
107 || server->tcpStatus == CifsExiting)
112 /* change_conf hasn't been executed */
115 cifs_server_dbg(VFS, "Possible client or server bug - zero credits\n");
118 cifs_server_dbg(VFS, "disabling echoes and oplocks\n");
121 cifs_dbg(FYI, "disabling oplocks\n");
124 trace_smb3_add_credits(server->CurrentMid,
125 server->hostname, rc, add);
126 cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
131 smb2_set_credits(struct TCP_Server_Info *server, const int val)
133 spin_lock(&server->req_lock);
134 server->credits = val;
136 server->reconnect_instance++;
137 spin_unlock(&server->req_lock);
138 /* don't log while holding the lock */
140 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
144 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
148 return &server->echo_credits;
150 return &server->oplock_credits;
152 return &server->credits;
157 smb2_get_credits(struct mid_q_entry *mid)
159 return mid->credits_received;
163 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
164 unsigned int *num, struct cifs_credits *credits)
167 unsigned int scredits;
169 spin_lock(&server->req_lock);
171 if (server->credits <= 0) {
172 spin_unlock(&server->req_lock);
173 cifs_num_waiters_inc(server);
174 rc = wait_event_killable(server->request_q,
175 has_credits(server, &server->credits, 1));
176 cifs_num_waiters_dec(server);
179 spin_lock(&server->req_lock);
181 if (server->tcpStatus == CifsExiting) {
182 spin_unlock(&server->req_lock);
186 scredits = server->credits;
187 /* can deadlock with reopen */
189 *num = SMB2_MAX_BUFFER_SIZE;
191 credits->instance = 0;
195 /* leave some credits for reopen and other ops */
197 *num = min_t(unsigned int, size,
198 scredits * SMB2_MAX_BUFFER_SIZE);
201 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
202 credits->instance = server->reconnect_instance;
203 server->credits -= credits->value;
205 if (server->in_flight > server->max_in_flight)
206 server->max_in_flight = server->in_flight;
210 spin_unlock(&server->req_lock);
215 smb2_adjust_credits(struct TCP_Server_Info *server,
216 struct cifs_credits *credits,
217 const unsigned int payload_size)
219 int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
221 if (!credits->value || credits->value == new_val)
224 if (credits->value < new_val) {
225 WARN_ONCE(1, "request has less credits (%d) than required (%d)",
226 credits->value, new_val);
230 spin_lock(&server->req_lock);
232 if (server->reconnect_instance != credits->instance) {
233 spin_unlock(&server->req_lock);
234 cifs_server_dbg(VFS, "trying to return %d credits to old session\n",
235 credits->value - new_val);
239 server->credits += credits->value - new_val;
240 spin_unlock(&server->req_lock);
241 wake_up(&server->request_q);
242 credits->value = new_val;
247 smb2_get_next_mid(struct TCP_Server_Info *server)
250 /* for SMB2 we need the current value */
251 spin_lock(&GlobalMid_Lock);
252 mid = server->CurrentMid++;
253 spin_unlock(&GlobalMid_Lock);
258 smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
260 spin_lock(&GlobalMid_Lock);
261 if (server->CurrentMid >= val)
262 server->CurrentMid -= val;
263 spin_unlock(&GlobalMid_Lock);
266 static struct mid_q_entry *
267 __smb2_find_mid(struct TCP_Server_Info *server, char *buf, bool dequeue)
269 struct mid_q_entry *mid;
270 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
271 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
273 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
274 cifs_server_dbg(VFS, "Encrypted frame parsing not supported yet\n");
278 spin_lock(&GlobalMid_Lock);
279 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
280 if ((mid->mid == wire_mid) &&
281 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
282 (mid->command == shdr->Command)) {
283 kref_get(&mid->refcount);
285 list_del_init(&mid->qhead);
286 mid->mid_flags |= MID_DELETED;
288 spin_unlock(&GlobalMid_Lock);
292 spin_unlock(&GlobalMid_Lock);
296 static struct mid_q_entry *
297 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
299 return __smb2_find_mid(server, buf, false);
302 static struct mid_q_entry *
303 smb2_find_dequeue_mid(struct TCP_Server_Info *server, char *buf)
305 return __smb2_find_mid(server, buf, true);
309 smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
311 #ifdef CONFIG_CIFS_DEBUG2
312 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
314 cifs_server_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
315 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
317 cifs_server_dbg(VFS, "smb buf %p len %u\n", buf,
318 server->ops->calc_smb_size(buf, server));
323 smb2_need_neg(struct TCP_Server_Info *server)
325 return server->max_read == 0;
329 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
333 cifs_ses_server(ses)->CurrentMid = 0;
334 rc = SMB2_negotiate(xid, ses);
335 /* BB we probably don't need to retry with modern servers */
342 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
344 struct TCP_Server_Info *server = tcon->ses->server;
347 /* start with specified wsize, or default */
348 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
349 wsize = min_t(unsigned int, wsize, server->max_write);
350 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
351 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
357 smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
359 struct TCP_Server_Info *server = tcon->ses->server;
362 /* start with specified wsize, or default */
363 wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
364 wsize = min_t(unsigned int, wsize, server->max_write);
365 #ifdef CONFIG_CIFS_SMB_DIRECT
369 * Account for SMB2 data transfer packet header and
370 * possible encryption header
372 wsize = min_t(unsigned int,
374 server->smbd_conn->max_fragmented_send_size -
375 SMB2_READWRITE_PDU_HEADER_SIZE -
376 sizeof(struct smb2_transform_hdr));
378 wsize = min_t(unsigned int,
379 wsize, server->smbd_conn->max_readwrite_size);
382 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
383 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
389 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
391 struct TCP_Server_Info *server = tcon->ses->server;
394 /* start with specified rsize, or default */
395 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
396 rsize = min_t(unsigned int, rsize, server->max_read);
398 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
399 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
405 smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
407 struct TCP_Server_Info *server = tcon->ses->server;
410 /* start with specified rsize, or default */
411 rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
412 rsize = min_t(unsigned int, rsize, server->max_read);
413 #ifdef CONFIG_CIFS_SMB_DIRECT
417 * Account for SMB2 data transfer packet header and
418 * possible encryption header
420 rsize = min_t(unsigned int,
422 server->smbd_conn->max_fragmented_recv_size -
423 SMB2_READWRITE_PDU_HEADER_SIZE -
424 sizeof(struct smb2_transform_hdr));
426 rsize = min_t(unsigned int,
427 rsize, server->smbd_conn->max_readwrite_size);
431 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
432 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
438 parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
440 struct cifs_server_iface **iface_list,
443 struct network_interface_info_ioctl_rsp *p;
444 struct sockaddr_in *addr4;
445 struct sockaddr_in6 *addr6;
446 struct iface_info_ipv4 *p4;
447 struct iface_info_ipv6 *p6;
448 struct cifs_server_iface *info;
458 * Fist pass: count and sanity check
461 bytes_left = buf_len;
463 while (bytes_left >= sizeof(*p)) {
465 next = le32_to_cpu(p->Next);
467 bytes_left -= sizeof(*p);
470 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
475 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
480 if (bytes_left || p->Next)
481 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
485 * Second pass: extract info to internal structure
488 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
495 bytes_left = buf_len;
497 while (bytes_left >= sizeof(*p)) {
498 info->speed = le64_to_cpu(p->LinkSpeed);
499 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE);
500 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE);
502 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
503 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
504 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
505 le32_to_cpu(p->Capability));
509 * The kernel and wire socket structures have the same
510 * layout and use network byte order but make the
511 * conversion explicit in case either one changes.
514 addr4 = (struct sockaddr_in *)&info->sockaddr;
515 p4 = (struct iface_info_ipv4 *)p->Buffer;
516 addr4->sin_family = AF_INET;
517 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
519 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
520 addr4->sin_port = cpu_to_be16(CIFS_PORT);
522 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
526 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
527 p6 = (struct iface_info_ipv6 *)p->Buffer;
528 addr6->sin6_family = AF_INET6;
529 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
531 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
532 addr6->sin6_flowinfo = 0;
533 addr6->sin6_scope_id = 0;
534 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
536 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
541 "%s: skipping unsupported socket family\n",
549 next = le32_to_cpu(p->Next);
552 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
570 static int compare_iface(const void *ia, const void *ib)
572 const struct cifs_server_iface *a = (struct cifs_server_iface *)ia;
573 const struct cifs_server_iface *b = (struct cifs_server_iface *)ib;
575 return a->speed == b->speed ? 0 : (a->speed > b->speed ? -1 : 1);
579 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
582 unsigned int ret_data_len = 0;
583 struct network_interface_info_ioctl_rsp *out_buf = NULL;
584 struct cifs_server_iface *iface_list;
586 struct cifs_ses *ses = tcon->ses;
588 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
589 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
590 NULL /* no data input */, 0 /* no data input */,
591 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
592 if (rc == -EOPNOTSUPP) {
594 "server does not support query network interfaces\n");
596 } else if (rc != 0) {
597 cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
601 rc = parse_server_interfaces(out_buf, ret_data_len,
602 &iface_list, &iface_count);
606 /* sort interfaces from fastest to slowest */
607 sort(iface_list, iface_count, sizeof(*iface_list), compare_iface, NULL);
609 spin_lock(&ses->iface_lock);
610 kfree(ses->iface_list);
611 ses->iface_list = iface_list;
612 ses->iface_count = iface_count;
613 ses->iface_last_update = jiffies;
614 spin_unlock(&ses->iface_lock);
622 smb2_close_cached_fid(struct kref *ref)
624 struct cached_fid *cfid = container_of(ref, struct cached_fid,
627 if (cfid->is_valid) {
628 cifs_dbg(FYI, "clear cached root file handle\n");
629 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
630 cfid->fid->volatile_fid);
631 cfid->is_valid = false;
632 cfid->file_all_info_is_valid = false;
633 cfid->has_lease = false;
637 void close_shroot(struct cached_fid *cfid)
639 mutex_lock(&cfid->fid_mutex);
640 kref_put(&cfid->refcount, smb2_close_cached_fid);
641 mutex_unlock(&cfid->fid_mutex);
644 void close_shroot_lease_locked(struct cached_fid *cfid)
646 if (cfid->has_lease) {
647 cfid->has_lease = false;
648 kref_put(&cfid->refcount, smb2_close_cached_fid);
652 void close_shroot_lease(struct cached_fid *cfid)
654 mutex_lock(&cfid->fid_mutex);
655 close_shroot_lease_locked(cfid);
656 mutex_unlock(&cfid->fid_mutex);
660 smb2_cached_lease_break(struct work_struct *work)
662 struct cached_fid *cfid = container_of(work,
663 struct cached_fid, lease_break);
665 close_shroot_lease(cfid);
669 * Open the directory at the root of a share
671 int open_shroot(unsigned int xid, struct cifs_tcon *tcon,
672 struct cifs_sb_info *cifs_sb,
673 struct cached_fid **cfid)
675 struct cifs_ses *ses = tcon->ses;
676 struct TCP_Server_Info *server = ses->server;
677 struct cifs_open_parms oparms;
678 struct smb2_create_rsp *o_rsp = NULL;
679 struct smb2_query_info_rsp *qi_rsp = NULL;
681 struct smb_rqst rqst[2];
682 struct kvec rsp_iov[2];
683 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
684 struct kvec qi_iov[1];
686 __le16 utf16_path = 0; /* Null - since an open of top of share */
687 u8 oplock = SMB2_OPLOCK_LEVEL_II;
688 struct cifs_fid *pfid;
690 mutex_lock(&tcon->crfid.fid_mutex);
691 if (tcon->crfid.is_valid) {
692 cifs_dbg(FYI, "found a cached root file handle\n");
693 *cfid = &tcon->crfid;
694 kref_get(&tcon->crfid.refcount);
695 mutex_unlock(&tcon->crfid.fid_mutex);
700 * We do not hold the lock for the open because in case
701 * SMB2_open needs to reconnect, it will end up calling
702 * cifs_mark_open_files_invalid() which takes the lock again
703 * thus causing a deadlock
706 mutex_unlock(&tcon->crfid.fid_mutex);
708 if (smb3_encryption_required(tcon))
709 flags |= CIFS_TRANSFORM_REQ;
711 if (!server->ops->new_lease_key)
714 pfid = tcon->crfid.fid;
715 server->ops->new_lease_key(pfid);
717 memset(rqst, 0, sizeof(rqst));
718 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
719 memset(rsp_iov, 0, sizeof(rsp_iov));
722 memset(&open_iov, 0, sizeof(open_iov));
723 rqst[0].rq_iov = open_iov;
724 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
727 oparms.create_options = cifs_create_options(cifs_sb, 0);
728 oparms.desired_access = FILE_READ_ATTRIBUTES;
729 oparms.disposition = FILE_OPEN;
731 oparms.reconnect = false;
733 rc = SMB2_open_init(tcon, server,
734 &rqst[0], &oplock, &oparms, &utf16_path);
737 smb2_set_next_command(tcon, &rqst[0]);
739 memset(&qi_iov, 0, sizeof(qi_iov));
740 rqst[1].rq_iov = qi_iov;
743 rc = SMB2_query_info_init(tcon, server,
744 &rqst[1], COMPOUND_FID,
745 COMPOUND_FID, FILE_ALL_INFORMATION,
747 sizeof(struct smb2_file_all_info) +
748 PATH_MAX * 2, 0, NULL);
752 smb2_set_related(&rqst[1]);
754 rc = compound_send_recv(xid, ses, server,
756 resp_buftype, rsp_iov);
757 mutex_lock(&tcon->crfid.fid_mutex);
760 * Now we need to check again as the cached root might have
761 * been successfully re-opened from a concurrent process
764 if (tcon->crfid.is_valid) {
765 /* work was already done */
767 /* stash fids for close() later */
768 struct cifs_fid fid = {
769 .persistent_fid = pfid->persistent_fid,
770 .volatile_fid = pfid->volatile_fid,
774 * caller expects this func to set pfid to a valid
775 * cached root, so we copy the existing one and get a
778 memcpy(pfid, tcon->crfid.fid, sizeof(*pfid));
779 kref_get(&tcon->crfid.refcount);
781 mutex_unlock(&tcon->crfid.fid_mutex);
784 /* close extra handle outside of crit sec */
785 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
791 /* Cached root is still invalid, continue normaly */
794 if (rc == -EREMCHG) {
795 tcon->need_reconnect = true;
796 pr_warn_once("server share %s deleted\n",
802 atomic_inc(&tcon->num_remote_opens);
804 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
805 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
806 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
807 #ifdef CONFIG_CIFS_DEBUG2
808 oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
809 #endif /* CIFS_DEBUG2 */
811 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
812 tcon->crfid.tcon = tcon;
813 tcon->crfid.is_valid = true;
814 kref_init(&tcon->crfid.refcount);
816 /* BB TBD check to see if oplock level check can be removed below */
817 if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
818 kref_get(&tcon->crfid.refcount);
819 tcon->crfid.has_lease = true;
820 smb2_parse_contexts(server, o_rsp,
822 oparms.fid->lease_key, &oplock,
827 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
828 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
830 if (!smb2_validate_and_copy_iov(
831 le16_to_cpu(qi_rsp->OutputBufferOffset),
832 sizeof(struct smb2_file_all_info),
833 &rsp_iov[1], sizeof(struct smb2_file_all_info),
834 (char *)&tcon->crfid.file_all_info))
835 tcon->crfid.file_all_info_is_valid = true;
838 mutex_unlock(&tcon->crfid.fid_mutex);
840 SMB2_open_free(&rqst[0]);
841 SMB2_query_info_free(&rqst[1]);
842 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
843 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
845 *cfid = &tcon->crfid;
850 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
851 struct cifs_sb_info *cifs_sb)
854 __le16 srch_path = 0; /* Null - open root of share */
855 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
856 struct cifs_open_parms oparms;
858 bool no_cached_open = tcon->nohandlecache;
859 struct cached_fid *cfid = NULL;
862 oparms.desired_access = FILE_READ_ATTRIBUTES;
863 oparms.disposition = FILE_OPEN;
864 oparms.create_options = cifs_create_options(cifs_sb, 0);
866 oparms.reconnect = false;
868 if (no_cached_open) {
869 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
872 rc = open_shroot(xid, tcon, cifs_sb, &cfid);
874 memcpy(&fid, cfid->fid, sizeof(struct cifs_fid));
879 SMB3_request_interfaces(xid, tcon);
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_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
886 FS_VOLUME_INFORMATION);
887 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
888 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
890 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
896 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
897 struct cifs_sb_info *cifs_sb)
900 __le16 srch_path = 0; /* Null - open root of share */
901 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
902 struct cifs_open_parms oparms;
906 oparms.desired_access = FILE_READ_ATTRIBUTES;
907 oparms.disposition = FILE_OPEN;
908 oparms.create_options = cifs_create_options(cifs_sb, 0);
910 oparms.reconnect = false;
912 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
917 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
918 FS_ATTRIBUTE_INFORMATION);
919 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
920 FS_DEVICE_INFORMATION);
921 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
925 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
926 struct cifs_sb_info *cifs_sb, const char *full_path)
930 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
931 struct cifs_open_parms oparms;
934 if ((*full_path == 0) && tcon->crfid.is_valid)
937 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
942 oparms.desired_access = FILE_READ_ATTRIBUTES;
943 oparms.disposition = FILE_OPEN;
944 oparms.create_options = cifs_create_options(cifs_sb, 0);
946 oparms.reconnect = false;
948 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
955 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
961 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
962 struct cifs_sb_info *cifs_sb, const char *full_path,
963 u64 *uniqueid, FILE_ALL_INFO *data)
965 *uniqueid = le64_to_cpu(data->IndexNumber);
970 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
971 struct cifs_fid *fid, FILE_ALL_INFO *data)
974 struct smb2_file_all_info *smb2_data;
976 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
978 if (smb2_data == NULL)
981 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
984 move_smb2_info_to_cifs(data, smb2_data);
989 #ifdef CONFIG_CIFS_XATTR
991 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
992 struct smb2_file_full_ea_info *src, size_t src_size,
993 const unsigned char *ea_name)
996 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
998 size_t buf_size = dst_size;
999 size_t name_len, value_len, user_name_len;
1001 while (src_size > 0) {
1002 name = &src->ea_data[0];
1003 name_len = (size_t)src->ea_name_length;
1004 value = &src->ea_data[src->ea_name_length + 1];
1005 value_len = (size_t)le16_to_cpu(src->ea_value_length);
1010 if (src_size < 8 + name_len + 1 + value_len) {
1011 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
1017 if (ea_name_len == name_len &&
1018 memcmp(ea_name, name, name_len) == 0) {
1022 if (dst_size < value_len) {
1026 memcpy(dst, value, value_len);
1030 /* 'user.' plus a terminating null */
1031 user_name_len = 5 + 1 + name_len;
1033 if (buf_size == 0) {
1034 /* skip copy - calc size only */
1035 rc += user_name_len;
1036 } else if (dst_size >= user_name_len) {
1037 dst_size -= user_name_len;
1038 memcpy(dst, "user.", 5);
1040 memcpy(dst, src->ea_data, name_len);
1044 rc += user_name_len;
1046 /* stop before overrun buffer */
1052 if (!src->next_entry_offset)
1055 if (src_size < le32_to_cpu(src->next_entry_offset)) {
1056 /* stop before overrun buffer */
1060 src_size -= le32_to_cpu(src->next_entry_offset);
1061 src = (void *)((char *)src +
1062 le32_to_cpu(src->next_entry_offset));
1065 /* didn't find the named attribute */
1074 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1075 const unsigned char *path, const unsigned char *ea_name,
1076 char *ea_data, size_t buf_size,
1077 struct cifs_sb_info *cifs_sb)
1081 struct kvec rsp_iov = {NULL, 0};
1082 int buftype = CIFS_NO_BUFFER;
1083 struct smb2_query_info_rsp *rsp;
1084 struct smb2_file_full_ea_info *info = NULL;
1086 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1090 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1092 FILE_FULL_EA_INFORMATION,
1095 MAX_SMB2_CREATE_RESPONSE_SIZE -
1096 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1097 &rsp_iov, &buftype, cifs_sb);
1100 * If ea_name is NULL (listxattr) and there are no EAs,
1101 * return 0 as it's not an error. Otherwise, the specified
1102 * ea_name was not found.
1104 if (!ea_name && rc == -ENODATA)
1109 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1110 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1111 le32_to_cpu(rsp->OutputBufferLength),
1113 sizeof(struct smb2_file_full_ea_info));
1117 info = (struct smb2_file_full_ea_info *)(
1118 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1119 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1120 le32_to_cpu(rsp->OutputBufferLength), ea_name);
1124 free_rsp_buf(buftype, rsp_iov.iov_base);
1130 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1131 const char *path, const char *ea_name, const void *ea_value,
1132 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1133 struct cifs_sb_info *cifs_sb)
1135 struct cifs_ses *ses = tcon->ses;
1136 struct TCP_Server_Info *server = cifs_pick_channel(ses);
1137 __le16 *utf16_path = NULL;
1138 int ea_name_len = strlen(ea_name);
1141 struct smb_rqst rqst[3];
1142 int resp_buftype[3];
1143 struct kvec rsp_iov[3];
1144 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1145 struct cifs_open_parms oparms;
1146 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1147 struct cifs_fid fid;
1148 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1149 unsigned int size[1];
1151 struct smb2_file_full_ea_info *ea = NULL;
1152 struct kvec close_iov[1];
1153 struct smb2_query_info_rsp *rsp;
1154 int rc, used_len = 0;
1156 if (smb3_encryption_required(tcon))
1157 flags |= CIFS_TRANSFORM_REQ;
1159 if (ea_name_len > 255)
1162 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1166 memset(rqst, 0, sizeof(rqst));
1167 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1168 memset(rsp_iov, 0, sizeof(rsp_iov));
1170 if (ses->server->ops->query_all_EAs) {
1172 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1178 /* If we are adding a attribute we should first check
1179 * if there will be enough space available to store
1180 * the new EA. If not we should not add it since we
1181 * would not be able to even read the EAs back.
1183 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1185 FILE_FULL_EA_INFORMATION,
1188 MAX_SMB2_CREATE_RESPONSE_SIZE -
1189 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1190 &rsp_iov[1], &resp_buftype[1], cifs_sb);
1192 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1193 used_len = le32_to_cpu(rsp->OutputBufferLength);
1195 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1196 resp_buftype[1] = CIFS_NO_BUFFER;
1197 memset(&rsp_iov[1], 0, sizeof(rsp_iov[1]));
1200 /* Use a fudge factor of 256 bytes in case we collide
1201 * with a different set_EAs command.
1203 if(CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1204 MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
1205 used_len + ea_name_len + ea_value_len + 1) {
1213 memset(&open_iov, 0, sizeof(open_iov));
1214 rqst[0].rq_iov = open_iov;
1215 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1217 memset(&oparms, 0, sizeof(oparms));
1219 oparms.desired_access = FILE_WRITE_EA;
1220 oparms.disposition = FILE_OPEN;
1221 oparms.create_options = cifs_create_options(cifs_sb, 0);
1223 oparms.reconnect = false;
1225 rc = SMB2_open_init(tcon, server,
1226 &rqst[0], &oplock, &oparms, utf16_path);
1229 smb2_set_next_command(tcon, &rqst[0]);
1233 memset(&si_iov, 0, sizeof(si_iov));
1234 rqst[1].rq_iov = si_iov;
1235 rqst[1].rq_nvec = 1;
1237 len = sizeof(*ea) + ea_name_len + ea_value_len + 1;
1238 ea = kzalloc(len, GFP_KERNEL);
1244 ea->ea_name_length = ea_name_len;
1245 ea->ea_value_length = cpu_to_le16(ea_value_len);
1246 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1247 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1252 rc = SMB2_set_info_init(tcon, server,
1253 &rqst[1], COMPOUND_FID,
1254 COMPOUND_FID, current->tgid,
1255 FILE_FULL_EA_INFORMATION,
1256 SMB2_O_INFO_FILE, 0, data, size);
1257 smb2_set_next_command(tcon, &rqst[1]);
1258 smb2_set_related(&rqst[1]);
1262 memset(&close_iov, 0, sizeof(close_iov));
1263 rqst[2].rq_iov = close_iov;
1264 rqst[2].rq_nvec = 1;
1265 rc = SMB2_close_init(tcon, server,
1266 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1267 smb2_set_related(&rqst[2]);
1269 rc = compound_send_recv(xid, ses, server,
1271 resp_buftype, rsp_iov);
1272 /* no need to bump num_remote_opens because handle immediately closed */
1277 SMB2_open_free(&rqst[0]);
1278 SMB2_set_info_free(&rqst[1]);
1279 SMB2_close_free(&rqst[2]);
1280 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1281 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1282 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1288 smb2_can_echo(struct TCP_Server_Info *server)
1290 return server->echoes;
1294 smb2_clear_stats(struct cifs_tcon *tcon)
1298 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1299 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1300 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1305 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1307 seq_puts(m, "\n\tShare Capabilities:");
1308 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1309 seq_puts(m, " DFS,");
1310 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1311 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1312 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1313 seq_puts(m, " SCALEOUT,");
1314 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1315 seq_puts(m, " CLUSTER,");
1316 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1317 seq_puts(m, " ASYMMETRIC,");
1318 if (tcon->capabilities == 0)
1319 seq_puts(m, " None");
1320 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1321 seq_puts(m, " Aligned,");
1322 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1323 seq_puts(m, " Partition Aligned,");
1324 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1325 seq_puts(m, " SSD,");
1326 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1327 seq_puts(m, " TRIM-support,");
1329 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1330 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1331 if (tcon->perf_sector_size)
1332 seq_printf(m, "\tOptimal sector size: 0x%x",
1333 tcon->perf_sector_size);
1334 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1338 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1340 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1341 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1344 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1345 * totals (requests sent) since those SMBs are per-session not per tcon
1347 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1348 (long long)(tcon->bytes_read),
1349 (long long)(tcon->bytes_written));
1350 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1351 atomic_read(&tcon->num_local_opens),
1352 atomic_read(&tcon->num_remote_opens));
1353 seq_printf(m, "\nTreeConnects: %d total %d failed",
1354 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1355 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1356 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1357 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1358 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1359 seq_printf(m, "\nCreates: %d total %d failed",
1360 atomic_read(&sent[SMB2_CREATE_HE]),
1361 atomic_read(&failed[SMB2_CREATE_HE]));
1362 seq_printf(m, "\nCloses: %d total %d failed",
1363 atomic_read(&sent[SMB2_CLOSE_HE]),
1364 atomic_read(&failed[SMB2_CLOSE_HE]));
1365 seq_printf(m, "\nFlushes: %d total %d failed",
1366 atomic_read(&sent[SMB2_FLUSH_HE]),
1367 atomic_read(&failed[SMB2_FLUSH_HE]));
1368 seq_printf(m, "\nReads: %d total %d failed",
1369 atomic_read(&sent[SMB2_READ_HE]),
1370 atomic_read(&failed[SMB2_READ_HE]));
1371 seq_printf(m, "\nWrites: %d total %d failed",
1372 atomic_read(&sent[SMB2_WRITE_HE]),
1373 atomic_read(&failed[SMB2_WRITE_HE]));
1374 seq_printf(m, "\nLocks: %d total %d failed",
1375 atomic_read(&sent[SMB2_LOCK_HE]),
1376 atomic_read(&failed[SMB2_LOCK_HE]));
1377 seq_printf(m, "\nIOCTLs: %d total %d failed",
1378 atomic_read(&sent[SMB2_IOCTL_HE]),
1379 atomic_read(&failed[SMB2_IOCTL_HE]));
1380 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1381 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1382 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1383 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1384 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1385 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1386 seq_printf(m, "\nQueryInfos: %d total %d failed",
1387 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1388 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1389 seq_printf(m, "\nSetInfos: %d total %d failed",
1390 atomic_read(&sent[SMB2_SET_INFO_HE]),
1391 atomic_read(&failed[SMB2_SET_INFO_HE]));
1392 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1393 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1394 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1398 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1400 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1401 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1403 cfile->fid.persistent_fid = fid->persistent_fid;
1404 cfile->fid.volatile_fid = fid->volatile_fid;
1405 cfile->fid.access = fid->access;
1406 #ifdef CONFIG_CIFS_DEBUG2
1407 cfile->fid.mid = fid->mid;
1408 #endif /* CIFS_DEBUG2 */
1409 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1411 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1412 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1416 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1417 struct cifs_fid *fid)
1419 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1423 smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
1424 struct cifsFileInfo *cfile)
1426 struct smb2_file_network_open_info file_inf;
1427 struct inode *inode;
1430 rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
1431 cfile->fid.volatile_fid, &file_inf);
1435 inode = d_inode(cfile->dentry);
1437 spin_lock(&inode->i_lock);
1438 CIFS_I(inode)->time = jiffies;
1440 /* Creation time should not need to be updated on close */
1441 if (file_inf.LastWriteTime)
1442 inode->i_mtime = cifs_NTtimeToUnix(file_inf.LastWriteTime);
1443 if (file_inf.ChangeTime)
1444 inode->i_ctime = cifs_NTtimeToUnix(file_inf.ChangeTime);
1445 if (file_inf.LastAccessTime)
1446 inode->i_atime = cifs_NTtimeToUnix(file_inf.LastAccessTime);
1449 * i_blocks is not related to (i_size / i_blksize),
1450 * but instead 512 byte (2**9) size is required for
1451 * calculating num blocks.
1453 if (le64_to_cpu(file_inf.AllocationSize) > 4096)
1455 (512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
1457 /* End of file and Attributes should not have to be updated on close */
1458 spin_unlock(&inode->i_lock);
1462 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1463 u64 persistent_fid, u64 volatile_fid,
1464 struct copychunk_ioctl *pcchunk)
1467 unsigned int ret_data_len;
1468 struct resume_key_req *res_key;
1470 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1471 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1472 NULL, 0 /* no input */, CIFSMaxBufSize,
1473 (char **)&res_key, &ret_data_len);
1476 cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1477 goto req_res_key_exit;
1479 if (ret_data_len < sizeof(struct resume_key_req)) {
1480 cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
1482 goto req_res_key_exit;
1484 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1492 struct smb_rqst rqst[3];
1493 struct kvec rsp_iov[3];
1494 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1495 struct kvec qi_iov[1];
1496 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1497 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1498 struct kvec close_iov[1];
1502 smb2_ioctl_query_info(const unsigned int xid,
1503 struct cifs_tcon *tcon,
1504 struct cifs_sb_info *cifs_sb,
1505 __le16 *path, int is_dir,
1508 struct iqi_vars *vars;
1509 struct smb_rqst *rqst;
1510 struct kvec *rsp_iov;
1511 struct cifs_ses *ses = tcon->ses;
1512 struct TCP_Server_Info *server = cifs_pick_channel(ses);
1513 char __user *arg = (char __user *)p;
1514 struct smb_query_info qi;
1515 struct smb_query_info __user *pqi;
1518 struct smb2_query_info_rsp *qi_rsp = NULL;
1519 struct smb2_ioctl_rsp *io_rsp = NULL;
1520 void *buffer = NULL;
1521 int resp_buftype[3];
1522 struct cifs_open_parms oparms;
1523 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1524 struct cifs_fid fid;
1525 unsigned int size[2];
1527 int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
1529 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
1532 rqst = &vars->rqst[0];
1533 rsp_iov = &vars->rsp_iov[0];
1535 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1537 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1540 if (qi.output_buffer_length > 1024) {
1545 if (!ses || !server) {
1550 if (smb3_encryption_required(tcon))
1551 flags |= CIFS_TRANSFORM_REQ;
1553 buffer = memdup_user(arg + sizeof(struct smb_query_info),
1554 qi.output_buffer_length);
1555 if (IS_ERR(buffer)) {
1557 return PTR_ERR(buffer);
1561 rqst[0].rq_iov = &vars->open_iov[0];
1562 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1564 memset(&oparms, 0, sizeof(oparms));
1566 oparms.disposition = FILE_OPEN;
1567 oparms.create_options = cifs_create_options(cifs_sb, create_options);
1569 oparms.reconnect = false;
1571 if (qi.flags & PASSTHRU_FSCTL) {
1572 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1573 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1574 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1576 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1577 oparms.desired_access = GENERIC_ALL;
1579 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1580 oparms.desired_access = GENERIC_READ;
1582 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1583 oparms.desired_access = GENERIC_WRITE;
1586 } else if (qi.flags & PASSTHRU_SET_INFO) {
1587 oparms.desired_access = GENERIC_WRITE;
1589 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1592 rc = SMB2_open_init(tcon, server,
1593 &rqst[0], &oplock, &oparms, path);
1596 smb2_set_next_command(tcon, &rqst[0]);
1599 if (qi.flags & PASSTHRU_FSCTL) {
1600 /* Can eventually relax perm check since server enforces too */
1601 if (!capable(CAP_SYS_ADMIN))
1604 rqst[1].rq_iov = &vars->io_iov[0];
1605 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1607 rc = SMB2_ioctl_init(tcon, server,
1609 COMPOUND_FID, COMPOUND_FID,
1610 qi.info_type, true, buffer,
1611 qi.output_buffer_length,
1613 MAX_SMB2_CREATE_RESPONSE_SIZE -
1614 MAX_SMB2_CLOSE_RESPONSE_SIZE);
1616 } else if (qi.flags == PASSTHRU_SET_INFO) {
1617 /* Can eventually relax perm check since server enforces too */
1618 if (!capable(CAP_SYS_ADMIN))
1621 rqst[1].rq_iov = &vars->si_iov[0];
1622 rqst[1].rq_nvec = 1;
1627 rc = SMB2_set_info_init(tcon, server,
1629 COMPOUND_FID, COMPOUND_FID,
1631 FILE_END_OF_FILE_INFORMATION,
1632 SMB2_O_INFO_FILE, 0, data, size);
1634 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1635 rqst[1].rq_iov = &vars->qi_iov[0];
1636 rqst[1].rq_nvec = 1;
1638 rc = SMB2_query_info_init(tcon, server,
1639 &rqst[1], COMPOUND_FID,
1640 COMPOUND_FID, qi.file_info_class,
1641 qi.info_type, qi.additional_information,
1642 qi.input_buffer_length,
1643 qi.output_buffer_length, buffer);
1644 } else { /* unknown flags */
1645 cifs_tcon_dbg(VFS, "Invalid passthru query flags: 0x%x\n",
1652 smb2_set_next_command(tcon, &rqst[1]);
1653 smb2_set_related(&rqst[1]);
1656 rqst[2].rq_iov = &vars->close_iov[0];
1657 rqst[2].rq_nvec = 1;
1659 rc = SMB2_close_init(tcon, server,
1660 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1663 smb2_set_related(&rqst[2]);
1665 rc = compound_send_recv(xid, ses, server,
1667 resp_buftype, rsp_iov);
1671 /* No need to bump num_remote_opens since handle immediately closed */
1672 if (qi.flags & PASSTHRU_FSCTL) {
1673 pqi = (struct smb_query_info __user *)arg;
1674 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1675 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1676 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1677 if (qi.input_buffer_length > 0 &&
1678 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1679 > rsp_iov[1].iov_len)
1682 if (copy_to_user(&pqi->input_buffer_length,
1683 &qi.input_buffer_length,
1684 sizeof(qi.input_buffer_length)))
1687 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1688 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1689 qi.input_buffer_length))
1692 pqi = (struct smb_query_info __user *)arg;
1693 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1694 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1695 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1696 if (copy_to_user(&pqi->input_buffer_length,
1697 &qi.input_buffer_length,
1698 sizeof(qi.input_buffer_length)))
1701 if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1702 qi.input_buffer_length))
1709 SMB2_open_free(&rqst[0]);
1710 if (qi.flags & PASSTHRU_FSCTL)
1711 SMB2_ioctl_free(&rqst[1]);
1713 SMB2_query_info_free(&rqst[1]);
1715 SMB2_close_free(&rqst[2]);
1716 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1717 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1718 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1727 smb2_copychunk_range(const unsigned int xid,
1728 struct cifsFileInfo *srcfile,
1729 struct cifsFileInfo *trgtfile, u64 src_off,
1730 u64 len, u64 dest_off)
1733 unsigned int ret_data_len;
1734 struct copychunk_ioctl *pcchunk;
1735 struct copychunk_ioctl_rsp *retbuf = NULL;
1736 struct cifs_tcon *tcon;
1737 int chunks_copied = 0;
1738 bool chunk_sizes_updated = false;
1739 ssize_t bytes_written, total_bytes_written = 0;
1741 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1743 if (pcchunk == NULL)
1746 cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
1747 /* Request a key from the server to identify the source of the copy */
1748 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1749 srcfile->fid.persistent_fid,
1750 srcfile->fid.volatile_fid, pcchunk);
1752 /* Note: request_res_key sets res_key null only if rc !=0 */
1756 /* For now array only one chunk long, will make more flexible later */
1757 pcchunk->ChunkCount = cpu_to_le32(1);
1758 pcchunk->Reserved = 0;
1759 pcchunk->Reserved2 = 0;
1761 tcon = tlink_tcon(trgtfile->tlink);
1764 pcchunk->SourceOffset = cpu_to_le64(src_off);
1765 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1767 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1769 /* Request server copy to target from src identified by key */
1770 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1771 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1772 true /* is_fsctl */, (char *)pcchunk,
1773 sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1774 (char **)&retbuf, &ret_data_len);
1777 sizeof(struct copychunk_ioctl_rsp)) {
1778 cifs_tcon_dbg(VFS, "Invalid cchunk response size\n");
1782 if (retbuf->TotalBytesWritten == 0) {
1783 cifs_dbg(FYI, "no bytes copied\n");
1788 * Check if server claimed to write more than we asked
1790 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1791 le32_to_cpu(pcchunk->Length)) {
1792 cifs_tcon_dbg(VFS, "Invalid copy chunk response\n");
1796 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1797 cifs_tcon_dbg(VFS, "Invalid num chunks written\n");
1803 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1804 src_off += bytes_written;
1805 dest_off += bytes_written;
1806 len -= bytes_written;
1807 total_bytes_written += bytes_written;
1809 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1810 le32_to_cpu(retbuf->ChunksWritten),
1811 le32_to_cpu(retbuf->ChunkBytesWritten),
1813 } else if (rc == -EINVAL) {
1814 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1817 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1818 le32_to_cpu(retbuf->ChunksWritten),
1819 le32_to_cpu(retbuf->ChunkBytesWritten),
1820 le32_to_cpu(retbuf->TotalBytesWritten));
1823 * Check if this is the first request using these sizes,
1824 * (ie check if copy succeed once with original sizes
1825 * and check if the server gave us different sizes after
1826 * we already updated max sizes on previous request).
1827 * if not then why is the server returning an error now
1829 if ((chunks_copied != 0) || chunk_sizes_updated)
1832 /* Check that server is not asking us to grow size */
1833 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1834 tcon->max_bytes_chunk)
1835 tcon->max_bytes_chunk =
1836 le32_to_cpu(retbuf->ChunkBytesWritten);
1838 goto cchunk_out; /* server gave us bogus size */
1840 /* No need to change MaxChunks since already set to 1 */
1841 chunk_sizes_updated = true;
1852 return total_bytes_written;
1856 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1857 struct cifs_fid *fid)
1859 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1863 smb2_read_data_offset(char *buf)
1865 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1867 return rsp->DataOffset;
1871 smb2_read_data_length(char *buf, bool in_remaining)
1873 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1876 return le32_to_cpu(rsp->DataRemaining);
1878 return le32_to_cpu(rsp->DataLength);
1883 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1884 struct cifs_io_parms *parms, unsigned int *bytes_read,
1885 char **buf, int *buf_type)
1887 parms->persistent_fid = pfid->persistent_fid;
1888 parms->volatile_fid = pfid->volatile_fid;
1889 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1893 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1894 struct cifs_io_parms *parms, unsigned int *written,
1895 struct kvec *iov, unsigned long nr_segs)
1898 parms->persistent_fid = pfid->persistent_fid;
1899 parms->volatile_fid = pfid->volatile_fid;
1900 return SMB2_write(xid, parms, written, iov, nr_segs);
1903 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1904 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1905 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1907 struct cifsInodeInfo *cifsi;
1910 cifsi = CIFS_I(inode);
1912 /* if file already sparse don't bother setting sparse again */
1913 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1914 return true; /* already sparse */
1916 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1917 return true; /* already not sparse */
1920 * Can't check for sparse support on share the usual way via the
1921 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1922 * since Samba server doesn't set the flag on the share, yet
1923 * supports the set sparse FSCTL and returns sparse correctly
1924 * in the file attributes. If we fail setting sparse though we
1925 * mark that server does not support sparse files for this share
1926 * to avoid repeatedly sending the unsupported fsctl to server
1927 * if the file is repeatedly extended.
1929 if (tcon->broken_sparse_sup)
1932 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1933 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1935 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
1937 tcon->broken_sparse_sup = true;
1938 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1943 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1945 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1951 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1952 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1954 __le64 eof = cpu_to_le64(size);
1955 struct inode *inode;
1958 * If extending file more than one page make sparse. Many Linux fs
1959 * make files sparse by default when extending via ftruncate
1961 inode = d_inode(cfile->dentry);
1963 if (!set_alloc && (size > inode->i_size + 8192)) {
1964 __u8 set_sparse = 1;
1966 /* whether set sparse succeeds or not, extend the file */
1967 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1970 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1971 cfile->fid.volatile_fid, cfile->pid, &eof);
1975 smb2_duplicate_extents(const unsigned int xid,
1976 struct cifsFileInfo *srcfile,
1977 struct cifsFileInfo *trgtfile, u64 src_off,
1978 u64 len, u64 dest_off)
1981 unsigned int ret_data_len;
1982 struct duplicate_extents_to_file dup_ext_buf;
1983 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1985 /* server fileays advertise duplicate extent support with this flag */
1986 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1987 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1990 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1991 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1992 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1993 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1994 dup_ext_buf.ByteCount = cpu_to_le64(len);
1995 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
1996 src_off, dest_off, len);
1998 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
2000 goto duplicate_extents_out;
2002 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
2003 trgtfile->fid.volatile_fid,
2004 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
2005 true /* is_fsctl */,
2006 (char *)&dup_ext_buf,
2007 sizeof(struct duplicate_extents_to_file),
2008 CIFSMaxBufSize, NULL,
2011 if (ret_data_len > 0)
2012 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
2014 duplicate_extents_out:
2019 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2020 struct cifsFileInfo *cfile)
2022 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
2023 cfile->fid.volatile_fid);
2027 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
2028 struct cifsFileInfo *cfile)
2030 struct fsctl_set_integrity_information_req integr_info;
2031 unsigned int ret_data_len;
2033 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
2034 integr_info.Flags = 0;
2035 integr_info.Reserved = 0;
2037 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2038 cfile->fid.volatile_fid,
2039 FSCTL_SET_INTEGRITY_INFORMATION,
2040 true /* is_fsctl */,
2041 (char *)&integr_info,
2042 sizeof(struct fsctl_set_integrity_information_req),
2043 CIFSMaxBufSize, NULL,
2048 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
2049 #define GMT_TOKEN_SIZE 50
2051 #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
2054 * Input buffer contains (empty) struct smb_snapshot array with size filled in
2055 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
2058 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
2059 struct cifsFileInfo *cfile, void __user *ioc_buf)
2061 char *retbuf = NULL;
2062 unsigned int ret_data_len = 0;
2064 u32 max_response_size;
2065 struct smb_snapshot_array snapshot_in;
2068 * On the first query to enumerate the list of snapshots available
2069 * for this volume the buffer begins with 0 (number of snapshots
2070 * which can be returned is zero since at that point we do not know
2071 * how big the buffer needs to be). On the second query,
2072 * it (ret_data_len) is set to number of snapshots so we can
2073 * know to set the maximum response size larger (see below).
2075 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
2079 * Note that for snapshot queries that servers like Azure expect that
2080 * the first query be minimal size (and just used to get the number/size
2081 * of previous versions) so response size must be specified as EXACTLY
2082 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2085 if (ret_data_len == 0)
2086 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
2088 max_response_size = CIFSMaxBufSize;
2090 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2091 cfile->fid.volatile_fid,
2092 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
2093 true /* is_fsctl */,
2094 NULL, 0 /* no input data */, max_response_size,
2097 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2102 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2104 if (copy_from_user(&snapshot_in, ioc_buf,
2105 sizeof(struct smb_snapshot_array))) {
2112 * Check for min size, ie not large enough to fit even one GMT
2113 * token (snapshot). On the first ioctl some users may pass in
2114 * smaller size (or zero) to simply get the size of the array
2115 * so the user space caller can allocate sufficient memory
2116 * and retry the ioctl again with larger array size sufficient
2117 * to hold all of the snapshot GMT tokens on the second try.
2119 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2120 ret_data_len = sizeof(struct smb_snapshot_array);
2123 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2124 * the snapshot array (of 50 byte GMT tokens) each
2125 * representing an available previous version of the data
2127 if (ret_data_len > (snapshot_in.snapshot_array_size +
2128 sizeof(struct smb_snapshot_array)))
2129 ret_data_len = snapshot_in.snapshot_array_size +
2130 sizeof(struct smb_snapshot_array);
2132 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2143 smb3_notify(const unsigned int xid, struct file *pfile,
2144 void __user *ioc_buf)
2146 struct smb3_notify notify;
2147 struct dentry *dentry = pfile->f_path.dentry;
2148 struct inode *inode = file_inode(pfile);
2149 struct cifs_sb_info *cifs_sb;
2150 struct cifs_open_parms oparms;
2151 struct cifs_fid fid;
2152 struct cifs_tcon *tcon;
2153 unsigned char *path = NULL;
2154 __le16 *utf16_path = NULL;
2155 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2158 path = build_path_from_dentry(dentry);
2162 cifs_sb = CIFS_SB(inode->i_sb);
2164 utf16_path = cifs_convert_path_to_utf16(path + 1, cifs_sb);
2165 if (utf16_path == NULL) {
2170 if (copy_from_user(¬ify, ioc_buf, sizeof(struct smb3_notify))) {
2175 tcon = cifs_sb_master_tcon(cifs_sb);
2177 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2178 oparms.disposition = FILE_OPEN;
2179 oparms.create_options = cifs_create_options(cifs_sb, 0);
2181 oparms.reconnect = false;
2183 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
2188 rc = SMB2_change_notify(xid, tcon, fid.persistent_fid, fid.volatile_fid,
2189 notify.watch_tree, notify.completion_filter);
2191 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2193 cifs_dbg(FYI, "change notify for path %s rc %d\n", path, rc);
2202 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2203 const char *path, struct cifs_sb_info *cifs_sb,
2204 struct cifs_fid *fid, __u16 search_flags,
2205 struct cifs_search_info *srch_inf)
2208 struct smb_rqst rqst[2];
2209 struct kvec rsp_iov[2];
2210 int resp_buftype[2];
2211 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2212 struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
2214 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2215 struct cifs_open_parms oparms;
2216 struct smb2_query_directory_rsp *qd_rsp = NULL;
2217 struct smb2_create_rsp *op_rsp = NULL;
2218 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
2220 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2224 if (smb3_encryption_required(tcon))
2225 flags |= CIFS_TRANSFORM_REQ;
2227 memset(rqst, 0, sizeof(rqst));
2228 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2229 memset(rsp_iov, 0, sizeof(rsp_iov));
2232 memset(&open_iov, 0, sizeof(open_iov));
2233 rqst[0].rq_iov = open_iov;
2234 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2237 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2238 oparms.disposition = FILE_OPEN;
2239 oparms.create_options = cifs_create_options(cifs_sb, 0);
2241 oparms.reconnect = false;
2243 rc = SMB2_open_init(tcon, server,
2244 &rqst[0], &oplock, &oparms, utf16_path);
2247 smb2_set_next_command(tcon, &rqst[0]);
2249 /* Query directory */
2250 srch_inf->entries_in_buffer = 0;
2251 srch_inf->index_of_last_entry = 2;
2253 memset(&qd_iov, 0, sizeof(qd_iov));
2254 rqst[1].rq_iov = qd_iov;
2255 rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
2257 rc = SMB2_query_directory_init(xid, tcon, server,
2259 COMPOUND_FID, COMPOUND_FID,
2260 0, srch_inf->info_level);
2264 smb2_set_related(&rqst[1]);
2266 rc = compound_send_recv(xid, tcon->ses, server,
2268 resp_buftype, rsp_iov);
2270 /* If the open failed there is nothing to do */
2271 op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
2272 if (op_rsp == NULL || op_rsp->sync_hdr.Status != STATUS_SUCCESS) {
2273 cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
2276 fid->persistent_fid = op_rsp->PersistentFileId;
2277 fid->volatile_fid = op_rsp->VolatileFileId;
2279 /* Anything else than ENODATA means a genuine error */
2280 if (rc && rc != -ENODATA) {
2281 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2282 cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
2283 trace_smb3_query_dir_err(xid, fid->persistent_fid,
2284 tcon->tid, tcon->ses->Suid, 0, 0, rc);
2288 atomic_inc(&tcon->num_remote_opens);
2290 qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
2291 if (qd_rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
2292 trace_smb3_query_dir_done(xid, fid->persistent_fid,
2293 tcon->tid, tcon->ses->Suid, 0, 0);
2294 srch_inf->endOfSearch = true;
2299 rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
2302 trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
2303 tcon->ses->Suid, 0, 0, rc);
2306 resp_buftype[1] = CIFS_NO_BUFFER;
2308 trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
2309 tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
2313 SMB2_open_free(&rqst[0]);
2314 SMB2_query_directory_free(&rqst[1]);
2315 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2316 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2321 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2322 struct cifs_fid *fid, __u16 search_flags,
2323 struct cifs_search_info *srch_inf)
2325 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2326 fid->volatile_fid, 0, srch_inf);
2330 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2331 struct cifs_fid *fid)
2333 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2337 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2338 * the number of credits and return true. Otherwise - return false.
2341 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
2343 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2345 if (shdr->Status != STATUS_PENDING)
2348 if (shdr->CreditRequest) {
2349 spin_lock(&server->req_lock);
2350 server->credits += le16_to_cpu(shdr->CreditRequest);
2351 spin_unlock(&server->req_lock);
2352 wake_up(&server->request_q);
2359 smb2_is_session_expired(char *buf)
2361 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2363 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2364 shdr->Status != STATUS_USER_SESSION_DELETED)
2367 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2368 le16_to_cpu(shdr->Command),
2369 le64_to_cpu(shdr->MessageId));
2370 cifs_dbg(FYI, "Session expired or deleted\n");
2376 smb2_is_status_io_timeout(char *buf)
2378 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2380 if (shdr->Status == STATUS_IO_TIMEOUT)
2387 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2388 struct cifsInodeInfo *cinode)
2390 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2391 return SMB2_lease_break(0, tcon, cinode->lease_key,
2392 smb2_get_lease_state(cinode));
2394 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2396 CIFS_CACHE_READ(cinode) ? 1 : 0);
2400 smb2_set_related(struct smb_rqst *rqst)
2402 struct smb2_sync_hdr *shdr;
2404 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2406 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2409 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2412 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2415 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2417 struct smb2_sync_hdr *shdr;
2418 struct cifs_ses *ses = tcon->ses;
2419 struct TCP_Server_Info *server = ses->server;
2420 unsigned long len = smb_rqst_len(server, rqst);
2423 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2425 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2429 /* SMB headers in a compound are 8 byte aligned. */
2431 /* No padding needed */
2435 num_padding = 8 - (len & 7);
2436 if (!smb3_encryption_required(tcon)) {
2438 * If we do not have encryption then we can just add an extra
2439 * iov for the padding.
2441 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2442 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2447 * We can not add a small padding iov for the encryption case
2448 * because the encryption framework can not handle the padding
2450 * We have to flatten this into a single buffer and add
2451 * the padding to it.
2453 for (i = 1; i < rqst->rq_nvec; i++) {
2454 memcpy(rqst->rq_iov[0].iov_base +
2455 rqst->rq_iov[0].iov_len,
2456 rqst->rq_iov[i].iov_base,
2457 rqst->rq_iov[i].iov_len);
2458 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2460 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2462 rqst->rq_iov[0].iov_len += num_padding;
2468 shdr->NextCommand = cpu_to_le32(len);
2472 * Passes the query info response back to the caller on success.
2473 * Caller need to free this with free_rsp_buf().
2476 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2477 __le16 *utf16_path, u32 desired_access,
2478 u32 class, u32 type, u32 output_len,
2479 struct kvec *rsp, int *buftype,
2480 struct cifs_sb_info *cifs_sb)
2482 struct cifs_ses *ses = tcon->ses;
2483 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2485 struct smb_rqst rqst[3];
2486 int resp_buftype[3];
2487 struct kvec rsp_iov[3];
2488 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2489 struct kvec qi_iov[1];
2490 struct kvec close_iov[1];
2491 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2492 struct cifs_open_parms oparms;
2493 struct cifs_fid fid;
2496 if (smb3_encryption_required(tcon))
2497 flags |= CIFS_TRANSFORM_REQ;
2499 memset(rqst, 0, sizeof(rqst));
2500 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2501 memset(rsp_iov, 0, sizeof(rsp_iov));
2503 memset(&open_iov, 0, sizeof(open_iov));
2504 rqst[0].rq_iov = open_iov;
2505 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2508 oparms.desired_access = desired_access;
2509 oparms.disposition = FILE_OPEN;
2510 oparms.create_options = cifs_create_options(cifs_sb, 0);
2512 oparms.reconnect = false;
2514 rc = SMB2_open_init(tcon, server,
2515 &rqst[0], &oplock, &oparms, utf16_path);
2518 smb2_set_next_command(tcon, &rqst[0]);
2520 memset(&qi_iov, 0, sizeof(qi_iov));
2521 rqst[1].rq_iov = qi_iov;
2522 rqst[1].rq_nvec = 1;
2524 rc = SMB2_query_info_init(tcon, server,
2525 &rqst[1], COMPOUND_FID, COMPOUND_FID,
2531 smb2_set_next_command(tcon, &rqst[1]);
2532 smb2_set_related(&rqst[1]);
2534 memset(&close_iov, 0, sizeof(close_iov));
2535 rqst[2].rq_iov = close_iov;
2536 rqst[2].rq_nvec = 1;
2538 rc = SMB2_close_init(tcon, server,
2539 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2542 smb2_set_related(&rqst[2]);
2544 rc = compound_send_recv(xid, ses, server,
2546 resp_buftype, rsp_iov);
2548 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2549 if (rc == -EREMCHG) {
2550 tcon->need_reconnect = true;
2551 pr_warn_once("server share %s deleted\n",
2557 *buftype = resp_buftype[1];
2560 SMB2_open_free(&rqst[0]);
2561 SMB2_query_info_free(&rqst[1]);
2562 SMB2_close_free(&rqst[2]);
2563 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2564 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2569 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2570 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2572 struct smb2_query_info_rsp *rsp;
2573 struct smb2_fs_full_size_info *info = NULL;
2574 __le16 utf16_path = 0; /* Null - open root of share */
2575 struct kvec rsp_iov = {NULL, 0};
2576 int buftype = CIFS_NO_BUFFER;
2580 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2581 FILE_READ_ATTRIBUTES,
2582 FS_FULL_SIZE_INFORMATION,
2583 SMB2_O_INFO_FILESYSTEM,
2584 sizeof(struct smb2_fs_full_size_info),
2585 &rsp_iov, &buftype, cifs_sb);
2589 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2590 buf->f_type = SMB2_MAGIC_NUMBER;
2591 info = (struct smb2_fs_full_size_info *)(
2592 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2593 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2594 le32_to_cpu(rsp->OutputBufferLength),
2596 sizeof(struct smb2_fs_full_size_info));
2598 smb2_copy_fs_info_to_kstatfs(info, buf);
2601 free_rsp_buf(buftype, rsp_iov.iov_base);
2606 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2607 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2610 __le16 srch_path = 0; /* Null - open root of share */
2611 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2612 struct cifs_open_parms oparms;
2613 struct cifs_fid fid;
2615 if (!tcon->posix_extensions)
2616 return smb2_queryfs(xid, tcon, cifs_sb, buf);
2619 oparms.desired_access = FILE_READ_ATTRIBUTES;
2620 oparms.disposition = FILE_OPEN;
2621 oparms.create_options = cifs_create_options(cifs_sb, 0);
2623 oparms.reconnect = false;
2625 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
2630 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2631 fid.volatile_fid, buf);
2632 buf->f_type = SMB2_MAGIC_NUMBER;
2633 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2638 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2640 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2641 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2645 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2646 __u64 length, __u32 type, int lock, int unlock, bool wait)
2648 if (unlock && !lock)
2649 type = SMB2_LOCKFLAG_UNLOCK;
2650 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2651 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2652 current->tgid, length, offset, type, wait);
2656 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2658 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2662 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2664 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2668 smb2_new_lease_key(struct cifs_fid *fid)
2670 generate_random_uuid(fid->lease_key);
2674 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2675 const char *search_name,
2676 struct dfs_info3_param **target_nodes,
2677 unsigned int *num_of_nodes,
2678 const struct nls_table *nls_codepage, int remap)
2681 __le16 *utf16_path = NULL;
2682 int utf16_path_len = 0;
2683 struct cifs_tcon *tcon;
2684 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2685 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2686 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2688 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
2691 * Try to use the IPC tcon, otherwise just use any
2693 tcon = ses->tcon_ipc;
2695 spin_lock(&cifs_tcp_ses_lock);
2696 tcon = list_first_entry_or_null(&ses->tcon_list,
2701 spin_unlock(&cifs_tcp_ses_lock);
2705 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2711 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2713 nls_codepage, remap);
2719 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2720 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2726 /* Highest DFS referral version understood */
2727 dfs_req->MaxReferralLevel = DFS_VERSION;
2729 /* Path to resolve in an UTF-16 null-terminated string */
2730 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2733 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2734 FSCTL_DFS_GET_REFERRALS,
2735 true /* is_fsctl */,
2736 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
2737 (char **)&dfs_rsp, &dfs_rsp_size);
2738 } while (rc == -EAGAIN);
2741 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
2742 cifs_tcon_dbg(VFS, "ioctl error in %s rc=%d\n", __func__, rc);
2746 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2747 num_of_nodes, target_nodes,
2748 nls_codepage, remap, search_name,
2749 true /* is_unicode */);
2751 cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
2756 if (tcon && !tcon->ipc) {
2757 /* ipc tcons are not refcounted */
2758 spin_lock(&cifs_tcp_ses_lock);
2760 spin_unlock(&cifs_tcp_ses_lock);
2769 parse_reparse_posix(struct reparse_posix_data *symlink_buf,
2770 u32 plen, char **target_path,
2771 struct cifs_sb_info *cifs_sb)
2775 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
2776 len = le16_to_cpu(symlink_buf->ReparseDataLength);
2778 if (le64_to_cpu(symlink_buf->InodeType) != NFS_SPECFILE_LNK) {
2779 cifs_dbg(VFS, "%lld not a supported symlink type\n",
2780 le64_to_cpu(symlink_buf->InodeType));
2784 *target_path = cifs_strndup_from_utf16(
2785 symlink_buf->PathBuffer,
2786 len, true, cifs_sb->local_nls);
2787 if (!(*target_path))
2790 convert_delimiter(*target_path, '/');
2791 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2797 parse_reparse_symlink(struct reparse_symlink_data_buffer *symlink_buf,
2798 u32 plen, char **target_path,
2799 struct cifs_sb_info *cifs_sb)
2801 unsigned int sub_len;
2802 unsigned int sub_offset;
2804 /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
2806 sub_offset = le16_to_cpu(symlink_buf->SubstituteNameOffset);
2807 sub_len = le16_to_cpu(symlink_buf->SubstituteNameLength);
2808 if (sub_offset + 20 > plen ||
2809 sub_offset + sub_len + 20 > plen) {
2810 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
2814 *target_path = cifs_strndup_from_utf16(
2815 symlink_buf->PathBuffer + sub_offset,
2816 sub_len, true, cifs_sb->local_nls);
2817 if (!(*target_path))
2820 convert_delimiter(*target_path, '/');
2821 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2827 parse_reparse_point(struct reparse_data_buffer *buf,
2828 u32 plen, char **target_path,
2829 struct cifs_sb_info *cifs_sb)
2831 if (plen < sizeof(struct reparse_data_buffer)) {
2832 cifs_dbg(VFS, "reparse buffer is too small. Must be at least 8 bytes but was %d\n",
2837 if (plen < le16_to_cpu(buf->ReparseDataLength) +
2838 sizeof(struct reparse_data_buffer)) {
2839 cifs_dbg(VFS, "srv returned invalid reparse buf length: %d\n",
2844 /* See MS-FSCC 2.1.2 */
2845 switch (le32_to_cpu(buf->ReparseTag)) {
2846 case IO_REPARSE_TAG_NFS:
2847 return parse_reparse_posix(
2848 (struct reparse_posix_data *)buf,
2849 plen, target_path, cifs_sb);
2850 case IO_REPARSE_TAG_SYMLINK:
2851 return parse_reparse_symlink(
2852 (struct reparse_symlink_data_buffer *)buf,
2853 plen, target_path, cifs_sb);
2855 cifs_dbg(VFS, "srv returned unknown symlink buffer tag:0x%08x\n",
2856 le32_to_cpu(buf->ReparseTag));
2861 #define SMB2_SYMLINK_STRUCT_SIZE \
2862 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2865 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
2866 struct cifs_sb_info *cifs_sb, const char *full_path,
2867 char **target_path, bool is_reparse_point)
2870 __le16 *utf16_path = NULL;
2871 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2872 struct cifs_open_parms oparms;
2873 struct cifs_fid fid;
2874 struct kvec err_iov = {NULL, 0};
2875 struct smb2_err_rsp *err_buf = NULL;
2876 struct smb2_symlink_err_rsp *symlink;
2877 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
2878 unsigned int sub_len;
2879 unsigned int sub_offset;
2880 unsigned int print_len;
2881 unsigned int print_offset;
2883 struct smb_rqst rqst[3];
2884 int resp_buftype[3];
2885 struct kvec rsp_iov[3];
2886 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2887 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2888 struct kvec close_iov[1];
2889 struct smb2_create_rsp *create_rsp;
2890 struct smb2_ioctl_rsp *ioctl_rsp;
2891 struct reparse_data_buffer *reparse_buf;
2892 int create_options = is_reparse_point ? OPEN_REPARSE_POINT : 0;
2895 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2897 *target_path = NULL;
2899 if (smb3_encryption_required(tcon))
2900 flags |= CIFS_TRANSFORM_REQ;
2902 memset(rqst, 0, sizeof(rqst));
2903 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2904 memset(rsp_iov, 0, sizeof(rsp_iov));
2906 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2911 memset(&open_iov, 0, sizeof(open_iov));
2912 rqst[0].rq_iov = open_iov;
2913 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2915 memset(&oparms, 0, sizeof(oparms));
2917 oparms.desired_access = FILE_READ_ATTRIBUTES;
2918 oparms.disposition = FILE_OPEN;
2919 oparms.create_options = cifs_create_options(cifs_sb, create_options);
2921 oparms.reconnect = false;
2923 rc = SMB2_open_init(tcon, server,
2924 &rqst[0], &oplock, &oparms, utf16_path);
2927 smb2_set_next_command(tcon, &rqst[0]);
2931 memset(&io_iov, 0, sizeof(io_iov));
2932 rqst[1].rq_iov = io_iov;
2933 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
2935 rc = SMB2_ioctl_init(tcon, server,
2936 &rqst[1], fid.persistent_fid,
2937 fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
2938 true /* is_fctl */, NULL, 0,
2940 MAX_SMB2_CREATE_RESPONSE_SIZE -
2941 MAX_SMB2_CLOSE_RESPONSE_SIZE);
2945 smb2_set_next_command(tcon, &rqst[1]);
2946 smb2_set_related(&rqst[1]);
2950 memset(&close_iov, 0, sizeof(close_iov));
2951 rqst[2].rq_iov = close_iov;
2952 rqst[2].rq_nvec = 1;
2954 rc = SMB2_close_init(tcon, server,
2955 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2959 smb2_set_related(&rqst[2]);
2961 rc = compound_send_recv(xid, tcon->ses, server,
2963 resp_buftype, rsp_iov);
2965 create_rsp = rsp_iov[0].iov_base;
2966 if (create_rsp && create_rsp->sync_hdr.Status)
2967 err_iov = rsp_iov[0];
2968 ioctl_rsp = rsp_iov[1].iov_base;
2971 * Open was successful and we got an ioctl response.
2973 if ((rc == 0) && (is_reparse_point)) {
2974 /* See MS-FSCC 2.3.23 */
2976 reparse_buf = (struct reparse_data_buffer *)
2977 ((char *)ioctl_rsp +
2978 le32_to_cpu(ioctl_rsp->OutputOffset));
2979 plen = le32_to_cpu(ioctl_rsp->OutputCount);
2981 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
2982 rsp_iov[1].iov_len) {
2983 cifs_tcon_dbg(VFS, "srv returned invalid ioctl len: %d\n",
2989 rc = parse_reparse_point(reparse_buf, plen, target_path,
2994 if (!rc || !err_iov.iov_base) {
2999 err_buf = err_iov.iov_base;
3000 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
3001 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
3006 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
3007 if (le32_to_cpu(symlink->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
3008 le32_to_cpu(symlink->ReparseTag) != IO_REPARSE_TAG_SYMLINK) {
3013 /* open must fail on symlink - reset rc */
3015 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
3016 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
3017 print_len = le16_to_cpu(symlink->PrintNameLength);
3018 print_offset = le16_to_cpu(symlink->PrintNameOffset);
3020 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
3025 if (err_iov.iov_len <
3026 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
3031 *target_path = cifs_strndup_from_utf16(
3032 (char *)symlink->PathBuffer + sub_offset,
3033 sub_len, true, cifs_sb->local_nls);
3034 if (!(*target_path)) {
3038 convert_delimiter(*target_path, '/');
3039 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
3042 cifs_dbg(FYI, "query symlink rc %d\n", rc);
3044 SMB2_open_free(&rqst[0]);
3045 SMB2_ioctl_free(&rqst[1]);
3046 SMB2_close_free(&rqst[2]);
3047 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
3048 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3049 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
3054 smb2_query_reparse_tag(const unsigned int xid, struct cifs_tcon *tcon,
3055 struct cifs_sb_info *cifs_sb, const char *full_path,
3059 __le16 *utf16_path = NULL;
3060 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3061 struct cifs_open_parms oparms;
3062 struct cifs_fid fid;
3063 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
3065 struct smb_rqst rqst[3];
3066 int resp_buftype[3];
3067 struct kvec rsp_iov[3];
3068 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
3069 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
3070 struct kvec close_iov[1];
3071 struct smb2_ioctl_rsp *ioctl_rsp;
3072 struct reparse_data_buffer *reparse_buf;
3075 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
3077 if (smb3_encryption_required(tcon))
3078 flags |= CIFS_TRANSFORM_REQ;
3080 memset(rqst, 0, sizeof(rqst));
3081 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
3082 memset(rsp_iov, 0, sizeof(rsp_iov));
3084 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
3089 * setup smb2open - TODO add optimization to call cifs_get_readable_path
3090 * to see if there is a handle already open that we can use
3092 memset(&open_iov, 0, sizeof(open_iov));
3093 rqst[0].rq_iov = open_iov;
3094 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
3096 memset(&oparms, 0, sizeof(oparms));
3098 oparms.desired_access = FILE_READ_ATTRIBUTES;
3099 oparms.disposition = FILE_OPEN;
3100 oparms.create_options = cifs_create_options(cifs_sb, OPEN_REPARSE_POINT);
3102 oparms.reconnect = false;
3104 rc = SMB2_open_init(tcon, server,
3105 &rqst[0], &oplock, &oparms, utf16_path);
3108 smb2_set_next_command(tcon, &rqst[0]);
3112 memset(&io_iov, 0, sizeof(io_iov));
3113 rqst[1].rq_iov = io_iov;
3114 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
3116 rc = SMB2_ioctl_init(tcon, server,
3117 &rqst[1], COMPOUND_FID,
3118 COMPOUND_FID, FSCTL_GET_REPARSE_POINT,
3119 true /* is_fctl */, NULL, 0,
3121 MAX_SMB2_CREATE_RESPONSE_SIZE -
3122 MAX_SMB2_CLOSE_RESPONSE_SIZE);
3126 smb2_set_next_command(tcon, &rqst[1]);
3127 smb2_set_related(&rqst[1]);
3131 memset(&close_iov, 0, sizeof(close_iov));
3132 rqst[2].rq_iov = close_iov;
3133 rqst[2].rq_nvec = 1;
3135 rc = SMB2_close_init(tcon, server,
3136 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
3140 smb2_set_related(&rqst[2]);
3142 rc = compound_send_recv(xid, tcon->ses, server,
3144 resp_buftype, rsp_iov);
3146 ioctl_rsp = rsp_iov[1].iov_base;
3149 * Open was successful and we got an ioctl response.
3152 /* See MS-FSCC 2.3.23 */
3154 reparse_buf = (struct reparse_data_buffer *)
3155 ((char *)ioctl_rsp +
3156 le32_to_cpu(ioctl_rsp->OutputOffset));
3157 plen = le32_to_cpu(ioctl_rsp->OutputCount);
3159 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
3160 rsp_iov[1].iov_len) {
3161 cifs_tcon_dbg(FYI, "srv returned invalid ioctl len: %d\n",
3166 *tag = le32_to_cpu(reparse_buf->ReparseTag);
3171 SMB2_open_free(&rqst[0]);
3172 SMB2_ioctl_free(&rqst[1]);
3173 SMB2_close_free(&rqst[2]);
3174 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
3175 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3176 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
3180 static struct cifs_ntsd *
3181 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
3182 const struct cifs_fid *cifsfid, u32 *pacllen)
3184 struct cifs_ntsd *pntsd = NULL;
3186 int rc = -EOPNOTSUPP;
3187 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3190 return ERR_CAST(tlink);
3193 cifs_dbg(FYI, "trying to get acl\n");
3195 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
3196 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
3199 cifs_put_tlink(tlink);
3201 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3208 static struct cifs_ntsd *
3209 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
3210 const char *path, u32 *pacllen)
3212 struct cifs_ntsd *pntsd = NULL;
3213 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3216 struct cifs_tcon *tcon;
3217 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3218 struct cifs_fid fid;
3219 struct cifs_open_parms oparms;
3222 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
3224 return ERR_CAST(tlink);
3226 tcon = tlink_tcon(tlink);
3229 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3237 oparms.desired_access = READ_CONTROL;
3238 oparms.disposition = FILE_OPEN;
3240 * When querying an ACL, even if the file is a symlink we want to open
3241 * the source not the target, and so the protocol requires that the
3242 * client specify this flag when opening a reparse point
3244 oparms.create_options = cifs_create_options(cifs_sb, 0) | OPEN_REPARSE_POINT;
3246 oparms.reconnect = false;
3248 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
3252 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3253 fid.volatile_fid, (void **)&pntsd, pacllen);
3254 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3257 cifs_put_tlink(tlink);
3260 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3267 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
3268 struct inode *inode, const char *path, int aclflag)
3270 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3272 int rc, access_flags = 0;
3273 struct cifs_tcon *tcon;
3274 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3275 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3276 struct cifs_fid fid;
3277 struct cifs_open_parms oparms;
3280 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
3282 return PTR_ERR(tlink);
3284 tcon = tlink_tcon(tlink);
3287 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
3288 access_flags = WRITE_OWNER;
3290 access_flags = WRITE_DAC;
3292 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3300 oparms.desired_access = access_flags;
3301 oparms.create_options = cifs_create_options(cifs_sb, 0);
3302 oparms.disposition = FILE_OPEN;
3305 oparms.reconnect = false;
3307 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
3311 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3312 fid.volatile_fid, pnntsd, acllen, aclflag);
3313 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3316 cifs_put_tlink(tlink);
3321 /* Retrieve an ACL from the server */
3322 static struct cifs_ntsd *
3323 get_smb2_acl(struct cifs_sb_info *cifs_sb,
3324 struct inode *inode, const char *path,
3327 struct cifs_ntsd *pntsd = NULL;
3328 struct cifsFileInfo *open_file = NULL;
3331 open_file = find_readable_file(CIFS_I(inode), true);
3333 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
3335 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
3336 cifsFileInfo_put(open_file);
3340 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3341 loff_t offset, loff_t len, bool keep_size)
3343 struct cifs_ses *ses = tcon->ses;
3344 struct inode *inode;
3345 struct cifsInodeInfo *cifsi;
3346 struct cifsFileInfo *cfile = file->private_data;
3347 struct file_zero_data_information fsctl_buf;
3354 inode = d_inode(cfile->dentry);
3355 cifsi = CIFS_I(inode);
3357 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3358 ses->Suid, offset, len);
3361 * We zero the range through ioctl, so we need remove the page caches
3362 * first, otherwise the data may be inconsistent with the server.
3364 truncate_pagecache_range(inode, offset, offset + len - 1);
3366 /* if file not oplocked can't be sure whether asking to extend size */
3367 if (!CIFS_CACHE_READ(cifsi))
3368 if (keep_size == false) {
3370 trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
3371 tcon->tid, ses->Suid, offset, len, rc);
3376 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3378 fsctl_buf.FileOffset = cpu_to_le64(offset);
3379 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3381 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3382 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
3384 sizeof(struct file_zero_data_information),
3387 goto zero_range_exit;
3390 * do we also need to change the size of the file?
3392 if (keep_size == false && i_size_read(inode) < offset + len) {
3393 eof = cpu_to_le64(offset + len);
3394 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3395 cfile->fid.volatile_fid, cfile->pid, &eof);
3401 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3402 ses->Suid, offset, len, rc);
3404 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3405 ses->Suid, offset, len);
3409 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3410 loff_t offset, loff_t len)
3412 struct inode *inode;
3413 struct cifsFileInfo *cfile = file->private_data;
3414 struct file_zero_data_information fsctl_buf;
3417 __u8 set_sparse = 1;
3421 inode = d_inode(cfile->dentry);
3423 /* Need to make file sparse, if not already, before freeing range. */
3424 /* Consider adding equivalent for compressed since it could also work */
3425 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3432 * We implement the punch hole through ioctl, so we need remove the page
3433 * caches first, otherwise the data may be inconsistent with the server.
3435 truncate_pagecache_range(inode, offset, offset + len - 1);
3437 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3439 fsctl_buf.FileOffset = cpu_to_le64(offset);
3440 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3442 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3443 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3444 true /* is_fctl */, (char *)&fsctl_buf,
3445 sizeof(struct file_zero_data_information),
3446 CIFSMaxBufSize, NULL, NULL);
3451 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3452 loff_t off, loff_t len, bool keep_size)
3454 struct inode *inode;
3455 struct cifsInodeInfo *cifsi;
3456 struct cifsFileInfo *cfile = file->private_data;
3457 long rc = -EOPNOTSUPP;
3463 inode = d_inode(cfile->dentry);
3464 cifsi = CIFS_I(inode);
3466 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3467 tcon->ses->Suid, off, len);
3468 /* if file not oplocked can't be sure whether asking to extend size */
3469 if (!CIFS_CACHE_READ(cifsi))
3470 if (keep_size == false) {
3471 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3472 tcon->tid, tcon->ses->Suid, off, len, rc);
3478 * Extending the file
3480 if ((keep_size == false) && i_size_read(inode) < off + len) {
3481 rc = inode_newsize_ok(inode, off + len);
3485 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0)
3486 smb2_set_sparse(xid, tcon, cfile, inode, false);
3488 eof = cpu_to_le64(off + len);
3489 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3490 cfile->fid.volatile_fid, cfile->pid, &eof);
3492 cifsi->server_eof = off + len;
3493 cifs_setsize(inode, off + len);
3494 cifs_truncate_page(inode->i_mapping, inode->i_size);
3495 truncate_setsize(inode, off + len);
3501 * Files are non-sparse by default so falloc may be a no-op
3502 * Must check if file sparse. If not sparse, and since we are not
3503 * extending then no need to do anything since file already allocated
3505 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
3510 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3512 * Check if falloc starts within first few pages of file
3513 * and ends within a few pages of the end of file to
3514 * ensure that most of file is being forced to be
3515 * fallocated now. If so then setting whole file sparse
3516 * ie potentially making a few extra pages at the beginning
3517 * or end of the file non-sparse via set_sparse is harmless.
3519 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3525 smb2_set_sparse(xid, tcon, cfile, inode, false);
3530 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3531 tcon->ses->Suid, off, len, rc);
3533 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3534 tcon->ses->Suid, off, len);
3540 static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3542 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3543 struct cifsInodeInfo *cifsi;
3544 struct inode *inode;
3546 struct file_allocated_range_buffer in_data, *out_data = NULL;
3550 if (whence != SEEK_HOLE && whence != SEEK_DATA)
3551 return generic_file_llseek(file, offset, whence);
3553 inode = d_inode(cfile->dentry);
3554 cifsi = CIFS_I(inode);
3556 if (offset < 0 || offset >= i_size_read(inode))
3561 * We need to be sure that all dirty pages are written as they
3562 * might fill holes on the server.
3563 * Note that we also MUST flush any written pages since at least
3564 * some servers (Windows2016) will not reflect recent writes in
3565 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3567 wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
3569 filemap_write_and_wait(inode->i_mapping);
3570 smb2_flush_file(xid, tcon, &wrcfile->fid);
3571 cifsFileInfo_put(wrcfile);
3574 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3575 if (whence == SEEK_HOLE)
3576 offset = i_size_read(inode);
3580 in_data.file_offset = cpu_to_le64(offset);
3581 in_data.length = cpu_to_le64(i_size_read(inode));
3583 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3584 cfile->fid.volatile_fid,
3585 FSCTL_QUERY_ALLOCATED_RANGES, true,
3586 (char *)&in_data, sizeof(in_data),
3587 sizeof(struct file_allocated_range_buffer),
3588 (char **)&out_data, &out_data_len);
3594 if (whence == SEEK_HOLE && out_data_len == 0)
3597 if (whence == SEEK_DATA && out_data_len == 0) {
3602 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3606 if (whence == SEEK_DATA) {
3607 offset = le64_to_cpu(out_data->file_offset);
3610 if (offset < le64_to_cpu(out_data->file_offset))
3613 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3619 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3624 static int smb3_fiemap(struct cifs_tcon *tcon,
3625 struct cifsFileInfo *cfile,
3626 struct fiemap_extent_info *fei, u64 start, u64 len)
3629 struct file_allocated_range_buffer in_data, *out_data;
3631 int i, num, rc, flags, last_blob;
3634 rc = fiemap_prep(d_inode(cfile->dentry), fei, start, &len, 0);
3640 in_data.file_offset = cpu_to_le64(start);
3641 in_data.length = cpu_to_le64(len);
3643 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3644 cfile->fid.volatile_fid,
3645 FSCTL_QUERY_ALLOCATED_RANGES, true,
3646 (char *)&in_data, sizeof(in_data),
3647 1024 * sizeof(struct file_allocated_range_buffer),
3648 (char **)&out_data, &out_data_len);
3657 if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) {
3661 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3666 num = out_data_len / sizeof(struct file_allocated_range_buffer);
3667 for (i = 0; i < num; i++) {
3669 if (i == num - 1 && last_blob)
3670 flags |= FIEMAP_EXTENT_LAST;
3672 rc = fiemap_fill_next_extent(fei,
3673 le64_to_cpu(out_data[i].file_offset),
3674 le64_to_cpu(out_data[i].file_offset),
3675 le64_to_cpu(out_data[i].length),
3686 next = le64_to_cpu(out_data[num - 1].file_offset) +
3687 le64_to_cpu(out_data[num - 1].length);
3688 len = len - (next - start);
3699 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3700 loff_t off, loff_t len)
3702 /* KEEP_SIZE already checked for by do_fallocate */
3703 if (mode & FALLOC_FL_PUNCH_HOLE)
3704 return smb3_punch_hole(file, tcon, off, len);
3705 else if (mode & FALLOC_FL_ZERO_RANGE) {
3706 if (mode & FALLOC_FL_KEEP_SIZE)
3707 return smb3_zero_range(file, tcon, off, len, true);
3708 return smb3_zero_range(file, tcon, off, len, false);
3709 } else if (mode == FALLOC_FL_KEEP_SIZE)
3710 return smb3_simple_falloc(file, tcon, off, len, true);
3712 return smb3_simple_falloc(file, tcon, off, len, false);
3718 smb2_downgrade_oplock(struct TCP_Server_Info *server,
3719 struct cifsInodeInfo *cinode, __u32 oplock,
3720 unsigned int epoch, bool *purge_cache)
3722 server->ops->set_oplock_level(cinode, oplock, 0, NULL);
3726 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3727 unsigned int epoch, bool *purge_cache);
3730 smb3_downgrade_oplock(struct TCP_Server_Info *server,
3731 struct cifsInodeInfo *cinode, __u32 oplock,
3732 unsigned int epoch, bool *purge_cache)
3734 unsigned int old_state = cinode->oplock;
3735 unsigned int old_epoch = cinode->epoch;
3736 unsigned int new_state;
3738 if (epoch > old_epoch) {
3739 smb21_set_oplock_level(cinode, oplock, 0, NULL);
3740 cinode->epoch = epoch;
3743 new_state = cinode->oplock;
3744 *purge_cache = false;
3746 if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
3747 (new_state & CIFS_CACHE_READ_FLG) == 0)
3748 *purge_cache = true;
3749 else if (old_state == new_state && (epoch - old_epoch > 1))
3750 *purge_cache = true;
3754 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3755 unsigned int epoch, bool *purge_cache)
3758 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3760 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3761 cinode->oplock = CIFS_CACHE_RHW_FLG;
3762 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3763 &cinode->vfs_inode);
3764 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
3765 cinode->oplock = CIFS_CACHE_RW_FLG;
3766 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3767 &cinode->vfs_inode);
3768 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3769 cinode->oplock = CIFS_CACHE_READ_FLG;
3770 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3771 &cinode->vfs_inode);
3777 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3778 unsigned int epoch, bool *purge_cache)
3780 char message[5] = {0};
3781 unsigned int new_oplock = 0;
3784 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3787 /* Check if the server granted an oplock rather than a lease */
3788 if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3789 return smb2_set_oplock_level(cinode, oplock, epoch,
3792 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
3793 new_oplock |= CIFS_CACHE_READ_FLG;
3794 strcat(message, "R");
3796 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
3797 new_oplock |= CIFS_CACHE_HANDLE_FLG;
3798 strcat(message, "H");
3800 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
3801 new_oplock |= CIFS_CACHE_WRITE_FLG;
3802 strcat(message, "W");
3805 strncpy(message, "None", sizeof(message));
3807 cinode->oplock = new_oplock;
3808 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3809 &cinode->vfs_inode);
3813 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3814 unsigned int epoch, bool *purge_cache)
3816 unsigned int old_oplock = cinode->oplock;
3818 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3821 *purge_cache = false;
3822 if (old_oplock == CIFS_CACHE_READ_FLG) {
3823 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3824 (epoch - cinode->epoch > 0))
3825 *purge_cache = true;
3826 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3827 (epoch - cinode->epoch > 1))
3828 *purge_cache = true;
3829 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3830 (epoch - cinode->epoch > 1))
3831 *purge_cache = true;
3832 else if (cinode->oplock == 0 &&
3833 (epoch - cinode->epoch > 0))
3834 *purge_cache = true;
3835 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
3836 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3837 (epoch - cinode->epoch > 0))
3838 *purge_cache = true;
3839 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3840 (epoch - cinode->epoch > 1))
3841 *purge_cache = true;
3843 cinode->epoch = epoch;
3848 smb2_is_read_op(__u32 oplock)
3850 return oplock == SMB2_OPLOCK_LEVEL_II;
3854 smb21_is_read_op(__u32 oplock)
3856 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
3857 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
3861 map_oplock_to_lease(u8 oplock)
3863 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3864 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
3865 else if (oplock == SMB2_OPLOCK_LEVEL_II)
3866 return SMB2_LEASE_READ_CACHING;
3867 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3868 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
3869 SMB2_LEASE_WRITE_CACHING;
3874 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3876 struct create_lease *buf;
3878 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
3882 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3883 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3885 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3886 (struct create_lease, lcontext));
3887 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
3888 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3889 (struct create_lease, Name));
3890 buf->ccontext.NameLength = cpu_to_le16(4);
3891 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3900 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
3902 struct create_lease_v2 *buf;
3904 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
3908 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3909 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3911 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3912 (struct create_lease_v2, lcontext));
3913 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
3914 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3915 (struct create_lease_v2, Name));
3916 buf->ccontext.NameLength = cpu_to_le16(4);
3917 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3926 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3928 struct create_lease *lc = (struct create_lease *)buf;
3930 *epoch = 0; /* not used */
3931 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3932 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3933 return le32_to_cpu(lc->lcontext.LeaseState);
3937 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3939 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
3941 *epoch = le16_to_cpu(lc->lcontext.Epoch);
3942 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3943 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3945 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
3946 return le32_to_cpu(lc->lcontext.LeaseState);
3950 smb2_wp_retry_size(struct inode *inode)
3952 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
3953 SMB2_MAX_BUFFER_SIZE);
3957 smb2_dir_needs_close(struct cifsFileInfo *cfile)
3959 return !cfile->invalidHandle;
3963 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
3964 struct smb_rqst *old_rq, __le16 cipher_type)
3966 struct smb2_sync_hdr *shdr =
3967 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
3969 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
3970 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
3971 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
3972 tr_hdr->Flags = cpu_to_le16(0x01);
3973 if ((cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
3974 (cipher_type == SMB2_ENCRYPTION_AES256_GCM))
3975 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
3977 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
3978 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
3981 /* We can not use the normal sg_set_buf() as we will sometimes pass a
3982 * stack object as buf.
3984 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
3985 unsigned int buflen)
3989 * VMAP_STACK (at least) puts stack into the vmalloc address space
3991 if (is_vmalloc_addr(buf))
3992 addr = vmalloc_to_page(buf);
3994 addr = virt_to_page(buf);
3995 sg_set_page(sg, addr, buflen, offset_in_page(buf));
3998 /* Assumes the first rqst has a transform header as the first iov.
4000 * rqst[0].rq_iov[0] is transform header
4001 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
4002 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
4004 static struct scatterlist *
4005 init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
4007 unsigned int sg_len;
4008 struct scatterlist *sg;
4011 unsigned int idx = 0;
4015 for (i = 0; i < num_rqst; i++)
4016 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
4018 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
4022 sg_init_table(sg, sg_len);
4023 for (i = 0; i < num_rqst; i++) {
4024 for (j = 0; j < rqst[i].rq_nvec; j++) {
4026 * The first rqst has a transform header where the
4027 * first 20 bytes are not part of the encrypted blob
4029 skip = (i == 0) && (j == 0) ? 20 : 0;
4030 smb2_sg_set_buf(&sg[idx++],
4031 rqst[i].rq_iov[j].iov_base + skip,
4032 rqst[i].rq_iov[j].iov_len - skip);
4035 for (j = 0; j < rqst[i].rq_npages; j++) {
4036 unsigned int len, offset;
4038 rqst_page_get_length(&rqst[i], j, &len, &offset);
4039 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
4042 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
4047 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
4049 struct cifs_ses *ses;
4052 spin_lock(&cifs_tcp_ses_lock);
4053 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
4054 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
4055 if (ses->Suid == ses_id) {
4056 ses_enc_key = enc ? ses->smb3encryptionkey :
4057 ses->smb3decryptionkey;
4058 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
4059 spin_unlock(&cifs_tcp_ses_lock);
4064 spin_unlock(&cifs_tcp_ses_lock);
4069 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
4070 * iov[0] - transform header (associate data),
4071 * iov[1-N] - SMB2 header and pages - data to encrypt.
4072 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
4076 crypt_message(struct TCP_Server_Info *server, int num_rqst,
4077 struct smb_rqst *rqst, int enc)
4079 struct smb2_transform_hdr *tr_hdr =
4080 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
4081 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
4083 struct scatterlist *sg;
4084 u8 sign[SMB2_SIGNATURE_SIZE] = {};
4085 u8 key[SMB3_SIGN_KEY_SIZE];
4086 struct aead_request *req;
4088 unsigned int iv_len;
4089 DECLARE_CRYPTO_WAIT(wait);
4090 struct crypto_aead *tfm;
4091 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4093 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
4095 cifs_server_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
4100 rc = smb3_crypto_aead_allocate(server);
4102 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
4106 tfm = enc ? server->secmech.ccmaesencrypt :
4107 server->secmech.ccmaesdecrypt;
4109 if (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM)
4110 rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
4112 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
4115 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
4119 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
4121 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
4125 req = aead_request_alloc(tfm, GFP_KERNEL);
4127 cifs_server_dbg(VFS, "%s: Failed to alloc aead request\n", __func__);
4132 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
4133 crypt_len += SMB2_SIGNATURE_SIZE;
4136 sg = init_sg(num_rqst, rqst, sign);
4138 cifs_server_dbg(VFS, "%s: Failed to init sg\n", __func__);
4143 iv_len = crypto_aead_ivsize(tfm);
4144 iv = kzalloc(iv_len, GFP_KERNEL);
4146 cifs_server_dbg(VFS, "%s: Failed to alloc iv\n", __func__);
4151 if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4152 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4153 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
4156 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
4159 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
4160 aead_request_set_ad(req, assoc_data_len);
4162 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
4163 crypto_req_done, &wait);
4165 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
4166 : crypto_aead_decrypt(req), &wait);
4169 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
4180 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
4184 for (i = 0; i < num_rqst; i++) {
4185 if (rqst[i].rq_pages) {
4186 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
4187 put_page(rqst[i].rq_pages[j]);
4188 kfree(rqst[i].rq_pages);
4194 * This function will initialize new_rq and encrypt the content.
4195 * The first entry, new_rq[0], only contains a single iov which contains
4196 * a smb2_transform_hdr and is pre-allocated by the caller.
4197 * This function then populates new_rq[1+] with the content from olq_rq[0+].
4199 * The end result is an array of smb_rqst structures where the first structure
4200 * only contains a single iov for the transform header which we then can pass
4201 * to crypt_message().
4203 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
4204 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
4207 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
4208 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
4210 struct page **pages;
4211 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
4212 unsigned int npages;
4213 unsigned int orig_len = 0;
4217 for (i = 1; i < num_rqst; i++) {
4218 npages = old_rq[i - 1].rq_npages;
4219 pages = kmalloc_array(npages, sizeof(struct page *),
4224 new_rq[i].rq_pages = pages;
4225 new_rq[i].rq_npages = npages;
4226 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
4227 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
4228 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
4229 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
4230 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
4232 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
4234 for (j = 0; j < npages; j++) {
4235 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4240 /* copy pages form the old */
4241 for (j = 0; j < npages; j++) {
4243 unsigned int offset, len;
4245 rqst_page_get_length(&new_rq[i], j, &len, &offset);
4247 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
4248 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
4250 memcpy(dst, src, len);
4251 kunmap(new_rq[i].rq_pages[j]);
4252 kunmap(old_rq[i - 1].rq_pages[j]);
4256 /* fill the 1st iov with a transform header */
4257 fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
4259 rc = crypt_message(server, num_rqst, new_rq, 1);
4260 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
4267 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
4272 smb3_is_transform_hdr(void *buf)
4274 struct smb2_transform_hdr *trhdr = buf;
4276 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
4280 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
4281 unsigned int buf_data_size, struct page **pages,
4282 unsigned int npages, unsigned int page_data_size,
4286 struct smb_rqst rqst = {NULL};
4289 iov[0].iov_base = buf;
4290 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
4291 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
4292 iov[1].iov_len = buf_data_size;
4296 rqst.rq_pages = pages;
4297 rqst.rq_npages = npages;
4298 rqst.rq_pagesz = PAGE_SIZE;
4299 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
4301 rc = crypt_message(server, 1, &rqst, 0);
4302 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
4307 memmove(buf, iov[1].iov_base, buf_data_size);
4310 server->total_read = buf_data_size + page_data_size;
4316 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
4317 unsigned int npages, unsigned int len)
4322 for (i = 0; i < npages; i++) {
4323 struct page *page = pages[i];
4327 if (len >= PAGE_SIZE) {
4328 /* enough data to fill the page */
4332 zero_user(page, len, PAGE_SIZE - len);
4335 length = cifs_read_page_from_socket(server, page, 0, n);
4338 server->total_read += length;
4345 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
4346 unsigned int cur_off, struct bio_vec **page_vec)
4348 struct bio_vec *bvec;
4351 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
4355 for (i = 0; i < npages; i++) {
4356 bvec[i].bv_page = pages[i];
4357 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
4358 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
4359 data_size -= bvec[i].bv_len;
4362 if (data_size != 0) {
4363 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4373 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4374 char *buf, unsigned int buf_len, struct page **pages,
4375 unsigned int npages, unsigned int page_data_size,
4378 unsigned int data_offset;
4379 unsigned int data_len;
4380 unsigned int cur_off;
4381 unsigned int cur_page_idx;
4382 unsigned int pad_len;
4383 struct cifs_readdata *rdata = mid->callback_data;
4384 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
4385 struct bio_vec *bvec = NULL;
4386 struct iov_iter iter;
4389 bool use_rdma_mr = false;
4391 if (shdr->Command != SMB2_READ) {
4392 cifs_server_dbg(VFS, "only big read responses are supported\n");
4396 if (server->ops->is_session_expired &&
4397 server->ops->is_session_expired(buf)) {
4399 cifs_reconnect(server);
4403 if (server->ops->is_status_pending &&
4404 server->ops->is_status_pending(buf, server))
4407 /* set up first two iov to get credits */
4408 rdata->iov[0].iov_base = buf;
4409 rdata->iov[0].iov_len = 0;
4410 rdata->iov[1].iov_base = buf;
4411 rdata->iov[1].iov_len =
4412 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
4413 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4414 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4415 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4416 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4418 rdata->result = server->ops->map_error(buf, true);
4419 if (rdata->result != 0) {
4420 cifs_dbg(FYI, "%s: server returned error %d\n",
4421 __func__, rdata->result);
4422 /* normal error on read response */
4424 mid->mid_state = MID_RESPONSE_RECEIVED;
4426 dequeue_mid(mid, false);
4430 data_offset = server->ops->read_data_offset(buf);
4431 #ifdef CONFIG_CIFS_SMB_DIRECT
4432 use_rdma_mr = rdata->mr;
4434 data_len = server->ops->read_data_length(buf, use_rdma_mr);
4436 if (data_offset < server->vals->read_rsp_size) {
4438 * win2k8 sometimes sends an offset of 0 when the read
4439 * is beyond the EOF. Treat it as if the data starts just after
4442 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4443 __func__, data_offset);
4444 data_offset = server->vals->read_rsp_size;
4445 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4446 /* data_offset is beyond the end of smallbuf */
4447 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4448 __func__, data_offset);
4449 rdata->result = -EIO;
4451 mid->mid_state = MID_RESPONSE_MALFORMED;
4453 dequeue_mid(mid, rdata->result);
4457 pad_len = data_offset - server->vals->read_rsp_size;
4459 if (buf_len <= data_offset) {
4460 /* read response payload is in pages */
4461 cur_page_idx = pad_len / PAGE_SIZE;
4462 cur_off = pad_len % PAGE_SIZE;
4464 if (cur_page_idx != 0) {
4465 /* data offset is beyond the 1st page of response */
4466 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4467 __func__, data_offset);
4468 rdata->result = -EIO;
4470 mid->mid_state = MID_RESPONSE_MALFORMED;
4472 dequeue_mid(mid, rdata->result);
4476 if (data_len > page_data_size - pad_len) {
4477 /* data_len is corrupt -- discard frame */
4478 rdata->result = -EIO;
4480 mid->mid_state = MID_RESPONSE_MALFORMED;
4482 dequeue_mid(mid, rdata->result);
4486 rdata->result = init_read_bvec(pages, npages, page_data_size,
4488 if (rdata->result != 0) {
4490 mid->mid_state = MID_RESPONSE_MALFORMED;
4492 dequeue_mid(mid, rdata->result);
4496 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
4497 } else if (buf_len >= data_offset + data_len) {
4498 /* read response payload is in buf */
4499 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
4500 iov.iov_base = buf + data_offset;
4501 iov.iov_len = data_len;
4502 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
4504 /* read response payload cannot be in both buf and pages */
4505 WARN_ONCE(1, "buf can not contain only a part of read data");
4506 rdata->result = -EIO;
4508 mid->mid_state = MID_RESPONSE_MALFORMED;
4510 dequeue_mid(mid, rdata->result);
4514 length = rdata->copy_into_pages(server, rdata, &iter);
4522 mid->mid_state = MID_RESPONSE_RECEIVED;
4524 dequeue_mid(mid, false);
4528 struct smb2_decrypt_work {
4529 struct work_struct decrypt;
4530 struct TCP_Server_Info *server;
4531 struct page **ppages;
4533 unsigned int npages;
4538 static void smb2_decrypt_offload(struct work_struct *work)
4540 struct smb2_decrypt_work *dw = container_of(work,
4541 struct smb2_decrypt_work, decrypt);
4543 struct mid_q_entry *mid;
4545 rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
4546 dw->ppages, dw->npages, dw->len, true);
4548 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4552 dw->server->lstrp = jiffies;
4553 mid = smb2_find_dequeue_mid(dw->server, dw->buf);
4555 cifs_dbg(FYI, "mid not found\n");
4557 mid->decrypted = true;
4558 rc = handle_read_data(dw->server, mid, dw->buf,
4559 dw->server->vals->read_rsp_size,
4560 dw->ppages, dw->npages, dw->len,
4563 #ifdef CONFIG_CIFS_STATS2
4564 mid->when_received = jiffies;
4568 spin_lock(&GlobalMid_Lock);
4569 if (dw->server->tcpStatus == CifsNeedReconnect) {
4570 mid->mid_state = MID_RETRY_NEEDED;
4571 spin_unlock(&GlobalMid_Lock);
4574 mid->mid_state = MID_REQUEST_SUBMITTED;
4575 mid->mid_flags &= ~(MID_DELETED);
4576 list_add_tail(&mid->qhead,
4577 &dw->server->pending_mid_q);
4578 spin_unlock(&GlobalMid_Lock);
4581 cifs_mid_q_entry_release(mid);
4585 for (i = dw->npages-1; i >= 0; i--)
4586 put_page(dw->ppages[i]);
4589 cifs_small_buf_release(dw->buf);
4595 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4598 char *buf = server->smallbuf;
4599 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4600 unsigned int npages;
4601 struct page **pages;
4603 unsigned int buflen = server->pdu_size;
4606 struct smb2_decrypt_work *dw;
4609 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
4610 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
4612 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
4615 server->total_read += rc;
4617 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
4618 server->vals->read_rsp_size;
4619 npages = DIV_ROUND_UP(len, PAGE_SIZE);
4621 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
4627 for (; i < npages; i++) {
4628 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4635 /* read read data into pages */
4636 rc = read_data_into_pages(server, pages, npages, len);
4640 rc = cifs_discard_remaining_data(server);
4645 * For large reads, offload to different thread for better performance,
4646 * use more cores decrypting which can be expensive
4649 if ((server->min_offload) && (server->in_flight > 1) &&
4650 (server->pdu_size >= server->min_offload)) {
4651 dw = kmalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
4653 goto non_offloaded_decrypt;
4655 dw->buf = server->smallbuf;
4656 server->smallbuf = (char *)cifs_small_buf_get();
4658 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
4660 dw->npages = npages;
4661 dw->server = server;
4664 queue_work(decrypt_wq, &dw->decrypt);
4665 *num_mids = 0; /* worker thread takes care of finding mid */
4669 non_offloaded_decrypt:
4670 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
4671 pages, npages, len, false);
4675 *mid = smb2_find_mid(server, buf);
4677 cifs_dbg(FYI, "mid not found\n");
4679 cifs_dbg(FYI, "mid found\n");
4680 (*mid)->decrypted = true;
4681 rc = handle_read_data(server, *mid, buf,
4682 server->vals->read_rsp_size,
4683 pages, npages, len, false);
4687 for (i = i - 1; i >= 0; i--)
4692 cifs_discard_remaining_data(server);
4697 receive_encrypted_standard(struct TCP_Server_Info *server,
4698 struct mid_q_entry **mids, char **bufs,
4702 char *buf = server->smallbuf;
4703 struct smb2_sync_hdr *shdr;
4704 unsigned int pdu_length = server->pdu_size;
4705 unsigned int buf_size;
4706 struct mid_q_entry *mid_entry;
4708 char *next_buffer = NULL;
4712 /* switch to large buffer if too big for a small one */
4713 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
4714 server->large_buf = true;
4715 memcpy(server->bigbuf, buf, server->total_read);
4716 buf = server->bigbuf;
4719 /* now read the rest */
4720 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
4721 pdu_length - HEADER_SIZE(server) + 1);
4724 server->total_read += length;
4726 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
4727 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0, false);
4731 next_is_large = server->large_buf;
4733 shdr = (struct smb2_sync_hdr *)buf;
4734 if (shdr->NextCommand) {
4736 next_buffer = (char *)cifs_buf_get();
4738 next_buffer = (char *)cifs_small_buf_get();
4740 buf + le32_to_cpu(shdr->NextCommand),
4741 pdu_length - le32_to_cpu(shdr->NextCommand));
4744 mid_entry = smb2_find_mid(server, buf);
4745 if (mid_entry == NULL)
4746 cifs_dbg(FYI, "mid not found\n");
4748 cifs_dbg(FYI, "mid found\n");
4749 mid_entry->decrypted = true;
4750 mid_entry->resp_buf_size = server->pdu_size;
4753 if (*num_mids >= MAX_COMPOUND) {
4754 cifs_server_dbg(VFS, "too many PDUs in compound\n");
4757 bufs[*num_mids] = buf;
4758 mids[(*num_mids)++] = mid_entry;
4760 if (mid_entry && mid_entry->handle)
4761 ret = mid_entry->handle(server, mid_entry);
4763 ret = cifs_handle_standard(server, mid_entry);
4765 if (ret == 0 && shdr->NextCommand) {
4766 pdu_length -= le32_to_cpu(shdr->NextCommand);
4767 server->large_buf = next_is_large;
4769 server->bigbuf = buf = next_buffer;
4771 server->smallbuf = buf = next_buffer;
4773 } else if (ret != 0) {
4775 * ret != 0 here means that we didn't get to handle_mid() thus
4776 * server->smallbuf and server->bigbuf are still valid. We need
4777 * to free next_buffer because it is not going to be used
4781 free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
4783 free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
4790 smb3_receive_transform(struct TCP_Server_Info *server,
4791 struct mid_q_entry **mids, char **bufs, int *num_mids)
4793 char *buf = server->smallbuf;
4794 unsigned int pdu_length = server->pdu_size;
4795 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4796 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4798 if (pdu_length < sizeof(struct smb2_transform_hdr) +
4799 sizeof(struct smb2_sync_hdr)) {
4800 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
4802 cifs_reconnect(server);
4803 return -ECONNABORTED;
4806 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
4807 cifs_server_dbg(VFS, "Transform message is broken\n");
4808 cifs_reconnect(server);
4809 return -ECONNABORTED;
4812 /* TODO: add support for compounds containing READ. */
4813 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
4814 return receive_encrypted_read(server, &mids[0], num_mids);
4817 return receive_encrypted_standard(server, mids, bufs, num_mids);
4821 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
4823 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
4825 return handle_read_data(server, mid, buf, server->pdu_size,
4830 smb2_next_header(char *buf)
4832 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
4833 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
4835 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
4836 return sizeof(struct smb2_transform_hdr) +
4837 le32_to_cpu(t_hdr->OriginalMessageSize);
4839 return le32_to_cpu(hdr->NextCommand);
4843 smb2_make_node(unsigned int xid, struct inode *inode,
4844 struct dentry *dentry, struct cifs_tcon *tcon,
4845 char *full_path, umode_t mode, dev_t dev)
4847 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4849 FILE_ALL_INFO *buf = NULL;
4850 struct cifs_io_parms io_parms = {0};
4852 struct cifs_fid fid;
4853 struct cifs_open_parms oparms;
4854 unsigned int bytes_written;
4855 struct win_dev *pdev;
4859 * Check if mounted with mount parm 'sfu' mount parm.
4860 * SFU emulation should work with all servers, but only
4861 * supports block and char device (no socket & fifo),
4862 * and was used by default in earlier versions of Windows
4864 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
4868 * TODO: Add ability to create instead via reparse point. Windows (e.g.
4869 * their current NFS server) uses this approach to expose special files
4870 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
4873 if (!S_ISCHR(mode) && !S_ISBLK(mode))
4876 cifs_dbg(FYI, "sfu compat create special file\n");
4878 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
4885 oparms.cifs_sb = cifs_sb;
4886 oparms.desired_access = GENERIC_WRITE;
4887 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
4888 CREATE_OPTION_SPECIAL);
4889 oparms.disposition = FILE_CREATE;
4890 oparms.path = full_path;
4892 oparms.reconnect = false;
4894 if (tcon->ses->server->oplocks)
4895 oplock = REQ_OPLOCK;
4898 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
4903 * BB Do not bother to decode buf since no local inode yet to put
4904 * timestamps in, but we can reuse it safely.
4907 pdev = (struct win_dev *)buf;
4908 io_parms.pid = current->tgid;
4909 io_parms.tcon = tcon;
4910 io_parms.offset = 0;
4911 io_parms.length = sizeof(struct win_dev);
4912 iov[1].iov_base = buf;
4913 iov[1].iov_len = sizeof(struct win_dev);
4914 if (S_ISCHR(mode)) {
4915 memcpy(pdev->type, "IntxCHR", 8);
4916 pdev->major = cpu_to_le64(MAJOR(dev));
4917 pdev->minor = cpu_to_le64(MINOR(dev));
4918 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4919 &bytes_written, iov, 1);
4920 } else if (S_ISBLK(mode)) {
4921 memcpy(pdev->type, "IntxBLK", 8);
4922 pdev->major = cpu_to_le64(MAJOR(dev));
4923 pdev->minor = cpu_to_le64(MINOR(dev));
4924 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4925 &bytes_written, iov, 1);
4927 tcon->ses->server->ops->close(xid, tcon, &fid);
4930 /* FIXME: add code here to set EAs */
4937 struct smb_version_operations smb20_operations = {
4938 .compare_fids = smb2_compare_fids,
4939 .setup_request = smb2_setup_request,
4940 .setup_async_request = smb2_setup_async_request,
4941 .check_receive = smb2_check_receive,
4942 .add_credits = smb2_add_credits,
4943 .set_credits = smb2_set_credits,
4944 .get_credits_field = smb2_get_credits_field,
4945 .get_credits = smb2_get_credits,
4946 .wait_mtu_credits = cifs_wait_mtu_credits,
4947 .get_next_mid = smb2_get_next_mid,
4948 .revert_current_mid = smb2_revert_current_mid,
4949 .read_data_offset = smb2_read_data_offset,
4950 .read_data_length = smb2_read_data_length,
4951 .map_error = map_smb2_to_linux_error,
4952 .find_mid = smb2_find_mid,
4953 .check_message = smb2_check_message,
4954 .dump_detail = smb2_dump_detail,
4955 .clear_stats = smb2_clear_stats,
4956 .print_stats = smb2_print_stats,
4957 .is_oplock_break = smb2_is_valid_oplock_break,
4958 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4959 .downgrade_oplock = smb2_downgrade_oplock,
4960 .need_neg = smb2_need_neg,
4961 .negotiate = smb2_negotiate,
4962 .negotiate_wsize = smb2_negotiate_wsize,
4963 .negotiate_rsize = smb2_negotiate_rsize,
4964 .sess_setup = SMB2_sess_setup,
4965 .logoff = SMB2_logoff,
4966 .tree_connect = SMB2_tcon,
4967 .tree_disconnect = SMB2_tdis,
4968 .qfs_tcon = smb2_qfs_tcon,
4969 .is_path_accessible = smb2_is_path_accessible,
4970 .can_echo = smb2_can_echo,
4972 .query_path_info = smb2_query_path_info,
4973 .get_srv_inum = smb2_get_srv_inum,
4974 .query_file_info = smb2_query_file_info,
4975 .set_path_size = smb2_set_path_size,
4976 .set_file_size = smb2_set_file_size,
4977 .set_file_info = smb2_set_file_info,
4978 .set_compression = smb2_set_compression,
4979 .mkdir = smb2_mkdir,
4980 .mkdir_setinfo = smb2_mkdir_setinfo,
4981 .rmdir = smb2_rmdir,
4982 .unlink = smb2_unlink,
4983 .rename = smb2_rename_path,
4984 .create_hardlink = smb2_create_hardlink,
4985 .query_symlink = smb2_query_symlink,
4986 .query_mf_symlink = smb3_query_mf_symlink,
4987 .create_mf_symlink = smb3_create_mf_symlink,
4988 .open = smb2_open_file,
4989 .set_fid = smb2_set_fid,
4990 .close = smb2_close_file,
4991 .flush = smb2_flush_file,
4992 .async_readv = smb2_async_readv,
4993 .async_writev = smb2_async_writev,
4994 .sync_read = smb2_sync_read,
4995 .sync_write = smb2_sync_write,
4996 .query_dir_first = smb2_query_dir_first,
4997 .query_dir_next = smb2_query_dir_next,
4998 .close_dir = smb2_close_dir,
4999 .calc_smb_size = smb2_calc_size,
5000 .is_status_pending = smb2_is_status_pending,
5001 .is_session_expired = smb2_is_session_expired,
5002 .oplock_response = smb2_oplock_response,
5003 .queryfs = smb2_queryfs,
5004 .mand_lock = smb2_mand_lock,
5005 .mand_unlock_range = smb2_unlock_range,
5006 .push_mand_locks = smb2_push_mandatory_locks,
5007 .get_lease_key = smb2_get_lease_key,
5008 .set_lease_key = smb2_set_lease_key,
5009 .new_lease_key = smb2_new_lease_key,
5010 .calc_signature = smb2_calc_signature,
5011 .is_read_op = smb2_is_read_op,
5012 .set_oplock_level = smb2_set_oplock_level,
5013 .create_lease_buf = smb2_create_lease_buf,
5014 .parse_lease_buf = smb2_parse_lease_buf,
5015 .copychunk_range = smb2_copychunk_range,
5016 .wp_retry_size = smb2_wp_retry_size,
5017 .dir_needs_close = smb2_dir_needs_close,
5018 .get_dfs_refer = smb2_get_dfs_refer,
5019 .select_sectype = smb2_select_sectype,
5020 #ifdef CONFIG_CIFS_XATTR
5021 .query_all_EAs = smb2_query_eas,
5022 .set_EA = smb2_set_ea,
5023 #endif /* CIFS_XATTR */
5024 .get_acl = get_smb2_acl,
5025 .get_acl_by_fid = get_smb2_acl_by_fid,
5026 .set_acl = set_smb2_acl,
5027 .next_header = smb2_next_header,
5028 .ioctl_query_info = smb2_ioctl_query_info,
5029 .make_node = smb2_make_node,
5030 .fiemap = smb3_fiemap,
5031 .llseek = smb3_llseek,
5032 .is_status_io_timeout = smb2_is_status_io_timeout,
5035 struct smb_version_operations smb21_operations = {
5036 .compare_fids = smb2_compare_fids,
5037 .setup_request = smb2_setup_request,
5038 .setup_async_request = smb2_setup_async_request,
5039 .check_receive = smb2_check_receive,
5040 .add_credits = smb2_add_credits,
5041 .set_credits = smb2_set_credits,
5042 .get_credits_field = smb2_get_credits_field,
5043 .get_credits = smb2_get_credits,
5044 .wait_mtu_credits = smb2_wait_mtu_credits,
5045 .adjust_credits = smb2_adjust_credits,
5046 .get_next_mid = smb2_get_next_mid,
5047 .revert_current_mid = smb2_revert_current_mid,
5048 .read_data_offset = smb2_read_data_offset,
5049 .read_data_length = smb2_read_data_length,
5050 .map_error = map_smb2_to_linux_error,
5051 .find_mid = smb2_find_mid,
5052 .check_message = smb2_check_message,
5053 .dump_detail = smb2_dump_detail,
5054 .clear_stats = smb2_clear_stats,
5055 .print_stats = smb2_print_stats,
5056 .is_oplock_break = smb2_is_valid_oplock_break,
5057 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5058 .downgrade_oplock = smb2_downgrade_oplock,
5059 .need_neg = smb2_need_neg,
5060 .negotiate = smb2_negotiate,
5061 .negotiate_wsize = smb2_negotiate_wsize,
5062 .negotiate_rsize = smb2_negotiate_rsize,
5063 .sess_setup = SMB2_sess_setup,
5064 .logoff = SMB2_logoff,
5065 .tree_connect = SMB2_tcon,
5066 .tree_disconnect = SMB2_tdis,
5067 .qfs_tcon = smb2_qfs_tcon,
5068 .is_path_accessible = smb2_is_path_accessible,
5069 .can_echo = smb2_can_echo,
5071 .query_path_info = smb2_query_path_info,
5072 .get_srv_inum = smb2_get_srv_inum,
5073 .query_file_info = smb2_query_file_info,
5074 .set_path_size = smb2_set_path_size,
5075 .set_file_size = smb2_set_file_size,
5076 .set_file_info = smb2_set_file_info,
5077 .set_compression = smb2_set_compression,
5078 .mkdir = smb2_mkdir,
5079 .mkdir_setinfo = smb2_mkdir_setinfo,
5080 .rmdir = smb2_rmdir,
5081 .unlink = smb2_unlink,
5082 .rename = smb2_rename_path,
5083 .create_hardlink = smb2_create_hardlink,
5084 .query_symlink = smb2_query_symlink,
5085 .query_mf_symlink = smb3_query_mf_symlink,
5086 .create_mf_symlink = smb3_create_mf_symlink,
5087 .open = smb2_open_file,
5088 .set_fid = smb2_set_fid,
5089 .close = smb2_close_file,
5090 .flush = smb2_flush_file,
5091 .async_readv = smb2_async_readv,
5092 .async_writev = smb2_async_writev,
5093 .sync_read = smb2_sync_read,
5094 .sync_write = smb2_sync_write,
5095 .query_dir_first = smb2_query_dir_first,
5096 .query_dir_next = smb2_query_dir_next,
5097 .close_dir = smb2_close_dir,
5098 .calc_smb_size = smb2_calc_size,
5099 .is_status_pending = smb2_is_status_pending,
5100 .is_session_expired = smb2_is_session_expired,
5101 .oplock_response = smb2_oplock_response,
5102 .queryfs = smb2_queryfs,
5103 .mand_lock = smb2_mand_lock,
5104 .mand_unlock_range = smb2_unlock_range,
5105 .push_mand_locks = smb2_push_mandatory_locks,
5106 .get_lease_key = smb2_get_lease_key,
5107 .set_lease_key = smb2_set_lease_key,
5108 .new_lease_key = smb2_new_lease_key,
5109 .calc_signature = smb2_calc_signature,
5110 .is_read_op = smb21_is_read_op,
5111 .set_oplock_level = smb21_set_oplock_level,
5112 .create_lease_buf = smb2_create_lease_buf,
5113 .parse_lease_buf = smb2_parse_lease_buf,
5114 .copychunk_range = smb2_copychunk_range,
5115 .wp_retry_size = smb2_wp_retry_size,
5116 .dir_needs_close = smb2_dir_needs_close,
5117 .enum_snapshots = smb3_enum_snapshots,
5118 .notify = smb3_notify,
5119 .get_dfs_refer = smb2_get_dfs_refer,
5120 .select_sectype = smb2_select_sectype,
5121 #ifdef CONFIG_CIFS_XATTR
5122 .query_all_EAs = smb2_query_eas,
5123 .set_EA = smb2_set_ea,
5124 #endif /* CIFS_XATTR */
5125 .get_acl = get_smb2_acl,
5126 .get_acl_by_fid = get_smb2_acl_by_fid,
5127 .set_acl = set_smb2_acl,
5128 .next_header = smb2_next_header,
5129 .ioctl_query_info = smb2_ioctl_query_info,
5130 .make_node = smb2_make_node,
5131 .fiemap = smb3_fiemap,
5132 .llseek = smb3_llseek,
5133 .is_status_io_timeout = smb2_is_status_io_timeout,
5136 struct smb_version_operations smb30_operations = {
5137 .compare_fids = smb2_compare_fids,
5138 .setup_request = smb2_setup_request,
5139 .setup_async_request = smb2_setup_async_request,
5140 .check_receive = smb2_check_receive,
5141 .add_credits = smb2_add_credits,
5142 .set_credits = smb2_set_credits,
5143 .get_credits_field = smb2_get_credits_field,
5144 .get_credits = smb2_get_credits,
5145 .wait_mtu_credits = smb2_wait_mtu_credits,
5146 .adjust_credits = smb2_adjust_credits,
5147 .get_next_mid = smb2_get_next_mid,
5148 .revert_current_mid = smb2_revert_current_mid,
5149 .read_data_offset = smb2_read_data_offset,
5150 .read_data_length = smb2_read_data_length,
5151 .map_error = map_smb2_to_linux_error,
5152 .find_mid = smb2_find_mid,
5153 .check_message = smb2_check_message,
5154 .dump_detail = smb2_dump_detail,
5155 .clear_stats = smb2_clear_stats,
5156 .print_stats = smb2_print_stats,
5157 .dump_share_caps = smb2_dump_share_caps,
5158 .is_oplock_break = smb2_is_valid_oplock_break,
5159 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5160 .downgrade_oplock = smb3_downgrade_oplock,
5161 .need_neg = smb2_need_neg,
5162 .negotiate = smb2_negotiate,
5163 .negotiate_wsize = smb3_negotiate_wsize,
5164 .negotiate_rsize = smb3_negotiate_rsize,
5165 .sess_setup = SMB2_sess_setup,
5166 .logoff = SMB2_logoff,
5167 .tree_connect = SMB2_tcon,
5168 .tree_disconnect = SMB2_tdis,
5169 .qfs_tcon = smb3_qfs_tcon,
5170 .is_path_accessible = smb2_is_path_accessible,
5171 .can_echo = smb2_can_echo,
5173 .query_path_info = smb2_query_path_info,
5174 /* WSL tags introduced long after smb2.1, enable for SMB3, 3.11 only */
5175 .query_reparse_tag = smb2_query_reparse_tag,
5176 .get_srv_inum = smb2_get_srv_inum,
5177 .query_file_info = smb2_query_file_info,
5178 .set_path_size = smb2_set_path_size,
5179 .set_file_size = smb2_set_file_size,
5180 .set_file_info = smb2_set_file_info,
5181 .set_compression = smb2_set_compression,
5182 .mkdir = smb2_mkdir,
5183 .mkdir_setinfo = smb2_mkdir_setinfo,
5184 .rmdir = smb2_rmdir,
5185 .unlink = smb2_unlink,
5186 .rename = smb2_rename_path,
5187 .create_hardlink = smb2_create_hardlink,
5188 .query_symlink = smb2_query_symlink,
5189 .query_mf_symlink = smb3_query_mf_symlink,
5190 .create_mf_symlink = smb3_create_mf_symlink,
5191 .open = smb2_open_file,
5192 .set_fid = smb2_set_fid,
5193 .close = smb2_close_file,
5194 .close_getattr = smb2_close_getattr,
5195 .flush = smb2_flush_file,
5196 .async_readv = smb2_async_readv,
5197 .async_writev = smb2_async_writev,
5198 .sync_read = smb2_sync_read,
5199 .sync_write = smb2_sync_write,
5200 .query_dir_first = smb2_query_dir_first,
5201 .query_dir_next = smb2_query_dir_next,
5202 .close_dir = smb2_close_dir,
5203 .calc_smb_size = smb2_calc_size,
5204 .is_status_pending = smb2_is_status_pending,
5205 .is_session_expired = smb2_is_session_expired,
5206 .oplock_response = smb2_oplock_response,
5207 .queryfs = smb2_queryfs,
5208 .mand_lock = smb2_mand_lock,
5209 .mand_unlock_range = smb2_unlock_range,
5210 .push_mand_locks = smb2_push_mandatory_locks,
5211 .get_lease_key = smb2_get_lease_key,
5212 .set_lease_key = smb2_set_lease_key,
5213 .new_lease_key = smb2_new_lease_key,
5214 .generate_signingkey = generate_smb30signingkey,
5215 .calc_signature = smb3_calc_signature,
5216 .set_integrity = smb3_set_integrity,
5217 .is_read_op = smb21_is_read_op,
5218 .set_oplock_level = smb3_set_oplock_level,
5219 .create_lease_buf = smb3_create_lease_buf,
5220 .parse_lease_buf = smb3_parse_lease_buf,
5221 .copychunk_range = smb2_copychunk_range,
5222 .duplicate_extents = smb2_duplicate_extents,
5223 .validate_negotiate = smb3_validate_negotiate,
5224 .wp_retry_size = smb2_wp_retry_size,
5225 .dir_needs_close = smb2_dir_needs_close,
5226 .fallocate = smb3_fallocate,
5227 .enum_snapshots = smb3_enum_snapshots,
5228 .notify = smb3_notify,
5229 .init_transform_rq = smb3_init_transform_rq,
5230 .is_transform_hdr = smb3_is_transform_hdr,
5231 .receive_transform = smb3_receive_transform,
5232 .get_dfs_refer = smb2_get_dfs_refer,
5233 .select_sectype = smb2_select_sectype,
5234 #ifdef CONFIG_CIFS_XATTR
5235 .query_all_EAs = smb2_query_eas,
5236 .set_EA = smb2_set_ea,
5237 #endif /* CIFS_XATTR */
5238 .get_acl = get_smb2_acl,
5239 .get_acl_by_fid = get_smb2_acl_by_fid,
5240 .set_acl = set_smb2_acl,
5241 .next_header = smb2_next_header,
5242 .ioctl_query_info = smb2_ioctl_query_info,
5243 .make_node = smb2_make_node,
5244 .fiemap = smb3_fiemap,
5245 .llseek = smb3_llseek,
5246 .is_status_io_timeout = smb2_is_status_io_timeout,
5249 struct smb_version_operations smb311_operations = {
5250 .compare_fids = smb2_compare_fids,
5251 .setup_request = smb2_setup_request,
5252 .setup_async_request = smb2_setup_async_request,
5253 .check_receive = smb2_check_receive,
5254 .add_credits = smb2_add_credits,
5255 .set_credits = smb2_set_credits,
5256 .get_credits_field = smb2_get_credits_field,
5257 .get_credits = smb2_get_credits,
5258 .wait_mtu_credits = smb2_wait_mtu_credits,
5259 .adjust_credits = smb2_adjust_credits,
5260 .get_next_mid = smb2_get_next_mid,
5261 .revert_current_mid = smb2_revert_current_mid,
5262 .read_data_offset = smb2_read_data_offset,
5263 .read_data_length = smb2_read_data_length,
5264 .map_error = map_smb2_to_linux_error,
5265 .find_mid = smb2_find_mid,
5266 .check_message = smb2_check_message,
5267 .dump_detail = smb2_dump_detail,
5268 .clear_stats = smb2_clear_stats,
5269 .print_stats = smb2_print_stats,
5270 .dump_share_caps = smb2_dump_share_caps,
5271 .is_oplock_break = smb2_is_valid_oplock_break,
5272 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5273 .downgrade_oplock = smb3_downgrade_oplock,
5274 .need_neg = smb2_need_neg,
5275 .negotiate = smb2_negotiate,
5276 .negotiate_wsize = smb3_negotiate_wsize,
5277 .negotiate_rsize = smb3_negotiate_rsize,
5278 .sess_setup = SMB2_sess_setup,
5279 .logoff = SMB2_logoff,
5280 .tree_connect = SMB2_tcon,
5281 .tree_disconnect = SMB2_tdis,
5282 .qfs_tcon = smb3_qfs_tcon,
5283 .is_path_accessible = smb2_is_path_accessible,
5284 .can_echo = smb2_can_echo,
5286 .query_path_info = smb2_query_path_info,
5287 .query_reparse_tag = smb2_query_reparse_tag,
5288 .get_srv_inum = smb2_get_srv_inum,
5289 .query_file_info = smb2_query_file_info,
5290 .set_path_size = smb2_set_path_size,
5291 .set_file_size = smb2_set_file_size,
5292 .set_file_info = smb2_set_file_info,
5293 .set_compression = smb2_set_compression,
5294 .mkdir = smb2_mkdir,
5295 .mkdir_setinfo = smb2_mkdir_setinfo,
5296 .posix_mkdir = smb311_posix_mkdir,
5297 .rmdir = smb2_rmdir,
5298 .unlink = smb2_unlink,
5299 .rename = smb2_rename_path,
5300 .create_hardlink = smb2_create_hardlink,
5301 .query_symlink = smb2_query_symlink,
5302 .query_mf_symlink = smb3_query_mf_symlink,
5303 .create_mf_symlink = smb3_create_mf_symlink,
5304 .open = smb2_open_file,
5305 .set_fid = smb2_set_fid,
5306 .close = smb2_close_file,
5307 .close_getattr = smb2_close_getattr,
5308 .flush = smb2_flush_file,
5309 .async_readv = smb2_async_readv,
5310 .async_writev = smb2_async_writev,
5311 .sync_read = smb2_sync_read,
5312 .sync_write = smb2_sync_write,
5313 .query_dir_first = smb2_query_dir_first,
5314 .query_dir_next = smb2_query_dir_next,
5315 .close_dir = smb2_close_dir,
5316 .calc_smb_size = smb2_calc_size,
5317 .is_status_pending = smb2_is_status_pending,
5318 .is_session_expired = smb2_is_session_expired,
5319 .oplock_response = smb2_oplock_response,
5320 .queryfs = smb311_queryfs,
5321 .mand_lock = smb2_mand_lock,
5322 .mand_unlock_range = smb2_unlock_range,
5323 .push_mand_locks = smb2_push_mandatory_locks,
5324 .get_lease_key = smb2_get_lease_key,
5325 .set_lease_key = smb2_set_lease_key,
5326 .new_lease_key = smb2_new_lease_key,
5327 .generate_signingkey = generate_smb311signingkey,
5328 .calc_signature = smb3_calc_signature,
5329 .set_integrity = smb3_set_integrity,
5330 .is_read_op = smb21_is_read_op,
5331 .set_oplock_level = smb3_set_oplock_level,
5332 .create_lease_buf = smb3_create_lease_buf,
5333 .parse_lease_buf = smb3_parse_lease_buf,
5334 .copychunk_range = smb2_copychunk_range,
5335 .duplicate_extents = smb2_duplicate_extents,
5336 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5337 .wp_retry_size = smb2_wp_retry_size,
5338 .dir_needs_close = smb2_dir_needs_close,
5339 .fallocate = smb3_fallocate,
5340 .enum_snapshots = smb3_enum_snapshots,
5341 .notify = smb3_notify,
5342 .init_transform_rq = smb3_init_transform_rq,
5343 .is_transform_hdr = smb3_is_transform_hdr,
5344 .receive_transform = smb3_receive_transform,
5345 .get_dfs_refer = smb2_get_dfs_refer,
5346 .select_sectype = smb2_select_sectype,
5347 #ifdef CONFIG_CIFS_XATTR
5348 .query_all_EAs = smb2_query_eas,
5349 .set_EA = smb2_set_ea,
5350 #endif /* CIFS_XATTR */
5351 .get_acl = get_smb2_acl,
5352 .get_acl_by_fid = get_smb2_acl_by_fid,
5353 .set_acl = set_smb2_acl,
5354 .next_header = smb2_next_header,
5355 .ioctl_query_info = smb2_ioctl_query_info,
5356 .make_node = smb2_make_node,
5357 .fiemap = smb3_fiemap,
5358 .llseek = smb3_llseek,
5359 .is_status_io_timeout = smb2_is_status_io_timeout,
5362 struct smb_version_values smb20_values = {
5363 .version_string = SMB20_VERSION_STRING,
5364 .protocol_id = SMB20_PROT_ID,
5365 .req_capabilities = 0, /* MBZ */
5366 .large_lock_type = 0,
5367 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5368 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5369 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5370 .header_size = sizeof(struct smb2_sync_hdr),
5371 .header_preamble_size = 0,
5372 .max_header_size = MAX_SMB2_HDR_SIZE,
5373 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5374 .lock_cmd = SMB2_LOCK,
5376 .cap_nt_find = SMB2_NT_FIND,
5377 .cap_large_files = SMB2_LARGE_FILES,
5378 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5379 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5380 .create_lease_size = sizeof(struct create_lease),
5383 struct smb_version_values smb21_values = {
5384 .version_string = SMB21_VERSION_STRING,
5385 .protocol_id = SMB21_PROT_ID,
5386 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5387 .large_lock_type = 0,
5388 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5389 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5390 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5391 .header_size = sizeof(struct smb2_sync_hdr),
5392 .header_preamble_size = 0,
5393 .max_header_size = MAX_SMB2_HDR_SIZE,
5394 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5395 .lock_cmd = SMB2_LOCK,
5397 .cap_nt_find = SMB2_NT_FIND,
5398 .cap_large_files = SMB2_LARGE_FILES,
5399 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5400 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5401 .create_lease_size = sizeof(struct create_lease),
5404 struct smb_version_values smb3any_values = {
5405 .version_string = SMB3ANY_VERSION_STRING,
5406 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5407 .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,
5408 .large_lock_type = 0,
5409 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5410 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5411 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5412 .header_size = sizeof(struct smb2_sync_hdr),
5413 .header_preamble_size = 0,
5414 .max_header_size = MAX_SMB2_HDR_SIZE,
5415 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5416 .lock_cmd = SMB2_LOCK,
5418 .cap_nt_find = SMB2_NT_FIND,
5419 .cap_large_files = SMB2_LARGE_FILES,
5420 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5421 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5422 .create_lease_size = sizeof(struct create_lease_v2),
5425 struct smb_version_values smbdefault_values = {
5426 .version_string = SMBDEFAULT_VERSION_STRING,
5427 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5428 .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,
5429 .large_lock_type = 0,
5430 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5431 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5432 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5433 .header_size = sizeof(struct smb2_sync_hdr),
5434 .header_preamble_size = 0,
5435 .max_header_size = MAX_SMB2_HDR_SIZE,
5436 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5437 .lock_cmd = SMB2_LOCK,
5439 .cap_nt_find = SMB2_NT_FIND,
5440 .cap_large_files = SMB2_LARGE_FILES,
5441 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5442 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5443 .create_lease_size = sizeof(struct create_lease_v2),
5446 struct smb_version_values smb30_values = {
5447 .version_string = SMB30_VERSION_STRING,
5448 .protocol_id = SMB30_PROT_ID,
5449 .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,
5450 .large_lock_type = 0,
5451 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5452 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5453 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5454 .header_size = sizeof(struct smb2_sync_hdr),
5455 .header_preamble_size = 0,
5456 .max_header_size = MAX_SMB2_HDR_SIZE,
5457 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5458 .lock_cmd = SMB2_LOCK,
5460 .cap_nt_find = SMB2_NT_FIND,
5461 .cap_large_files = SMB2_LARGE_FILES,
5462 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5463 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5464 .create_lease_size = sizeof(struct create_lease_v2),
5467 struct smb_version_values smb302_values = {
5468 .version_string = SMB302_VERSION_STRING,
5469 .protocol_id = SMB302_PROT_ID,
5470 .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,
5471 .large_lock_type = 0,
5472 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5473 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5474 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5475 .header_size = sizeof(struct smb2_sync_hdr),
5476 .header_preamble_size = 0,
5477 .max_header_size = MAX_SMB2_HDR_SIZE,
5478 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5479 .lock_cmd = SMB2_LOCK,
5481 .cap_nt_find = SMB2_NT_FIND,
5482 .cap_large_files = SMB2_LARGE_FILES,
5483 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5484 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5485 .create_lease_size = sizeof(struct create_lease_v2),
5488 struct smb_version_values smb311_values = {
5489 .version_string = SMB311_VERSION_STRING,
5490 .protocol_id = SMB311_PROT_ID,
5491 .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,
5492 .large_lock_type = 0,
5493 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5494 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5495 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5496 .header_size = sizeof(struct smb2_sync_hdr),
5497 .header_preamble_size = 0,
5498 .max_header_size = MAX_SMB2_HDR_SIZE,
5499 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5500 .lock_cmd = SMB2_LOCK,
5502 .cap_nt_find = SMB2_NT_FIND,
5503 .cap_large_files = SMB2_LARGE_FILES,
5504 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5505 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5506 .create_lease_size = sizeof(struct create_lease_v2),