1 // SPDX-License-Identifier: GPL-2.0
3 * SMB2 version specific operations
5 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
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>
18 #include "smb2proto.h"
19 #include "cifsproto.h"
20 #include "cifs_debug.h"
21 #include "cifs_unicode.h"
22 #include "smb2status.h"
24 #include "cifs_ioctl.h"
25 #include "smbdirect.h"
27 /* Change credits for different ops and return the total number of credits */
29 change_conf(struct TCP_Server_Info *server)
31 server->credits += server->echo_credits + server->oplock_credits;
32 server->oplock_credits = server->echo_credits = 0;
33 switch (server->credits) {
37 server->echoes = false;
38 server->oplocks = false;
41 server->echoes = true;
42 server->oplocks = false;
43 server->echo_credits = 1;
46 server->echoes = true;
48 server->oplocks = true;
49 server->oplock_credits = 1;
51 server->oplocks = false;
53 server->echo_credits = 1;
55 server->credits -= server->echo_credits + server->oplock_credits;
56 return server->credits + server->echo_credits + server->oplock_credits;
60 smb2_add_credits(struct TCP_Server_Info *server,
61 const struct cifs_credits *credits, const int optype)
64 unsigned int add = credits->value;
65 unsigned int instance = credits->instance;
66 bool reconnect_detected = false;
68 spin_lock(&server->req_lock);
69 val = server->ops->get_credits_field(server, optype);
71 /* eg found case where write overlapping reconnect messed up credits */
72 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
73 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
74 server->hostname, *val);
75 if ((instance == 0) || (instance == server->reconnect_instance))
78 reconnect_detected = true;
81 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
82 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
85 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
86 rc = change_conf(server);
88 * Sometimes server returns 0 credits on oplock break ack - we need to
89 * rebalance credits in this case.
91 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
93 if (server->credits > 1) {
95 server->oplock_credits++;
98 spin_unlock(&server->req_lock);
99 wake_up(&server->request_q);
101 if (reconnect_detected)
102 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
105 if (server->tcpStatus == CifsNeedReconnect
106 || server->tcpStatus == CifsExiting)
111 /* change_conf hasn't been executed */
114 cifs_server_dbg(VFS, "Possible client or server bug - zero credits\n");
117 cifs_server_dbg(VFS, "disabling echoes and oplocks\n");
120 cifs_dbg(FYI, "disabling oplocks\n");
123 cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
128 smb2_set_credits(struct TCP_Server_Info *server, const int val)
130 spin_lock(&server->req_lock);
131 server->credits = val;
133 server->reconnect_instance++;
134 spin_unlock(&server->req_lock);
135 /* don't log while holding the lock */
137 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
141 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
145 return &server->echo_credits;
147 return &server->oplock_credits;
149 return &server->credits;
154 smb2_get_credits(struct mid_q_entry *mid)
156 return mid->credits_received;
160 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
161 unsigned int *num, struct cifs_credits *credits)
164 unsigned int scredits;
166 spin_lock(&server->req_lock);
168 if (server->credits <= 0) {
169 spin_unlock(&server->req_lock);
170 cifs_num_waiters_inc(server);
171 rc = wait_event_killable(server->request_q,
172 has_credits(server, &server->credits, 1));
173 cifs_num_waiters_dec(server);
176 spin_lock(&server->req_lock);
178 if (server->tcpStatus == CifsExiting) {
179 spin_unlock(&server->req_lock);
183 scredits = server->credits;
184 /* can deadlock with reopen */
186 *num = SMB2_MAX_BUFFER_SIZE;
188 credits->instance = 0;
192 /* leave some credits for reopen and other ops */
194 *num = min_t(unsigned int, size,
195 scredits * SMB2_MAX_BUFFER_SIZE);
198 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
199 credits->instance = server->reconnect_instance;
200 server->credits -= credits->value;
202 if (server->in_flight > server->max_in_flight)
203 server->max_in_flight = server->in_flight;
207 spin_unlock(&server->req_lock);
212 smb2_adjust_credits(struct TCP_Server_Info *server,
213 struct cifs_credits *credits,
214 const unsigned int payload_size)
216 int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
218 if (!credits->value || credits->value == new_val)
221 if (credits->value < new_val) {
222 WARN_ONCE(1, "request has less credits (%d) than required (%d)",
223 credits->value, new_val);
227 spin_lock(&server->req_lock);
229 if (server->reconnect_instance != credits->instance) {
230 spin_unlock(&server->req_lock);
231 cifs_server_dbg(VFS, "trying to return %d credits to old session\n",
232 credits->value - new_val);
236 server->credits += credits->value - new_val;
237 spin_unlock(&server->req_lock);
238 wake_up(&server->request_q);
239 credits->value = new_val;
244 smb2_get_next_mid(struct TCP_Server_Info *server)
247 /* for SMB2 we need the current value */
248 spin_lock(&GlobalMid_Lock);
249 mid = server->CurrentMid++;
250 spin_unlock(&GlobalMid_Lock);
255 smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
257 spin_lock(&GlobalMid_Lock);
258 if (server->CurrentMid >= val)
259 server->CurrentMid -= val;
260 spin_unlock(&GlobalMid_Lock);
263 static struct mid_q_entry *
264 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
266 struct mid_q_entry *mid;
267 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
268 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
270 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
271 cifs_server_dbg(VFS, "Encrypted frame parsing not supported yet\n");
275 spin_lock(&GlobalMid_Lock);
276 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
277 if ((mid->mid == wire_mid) &&
278 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
279 (mid->command == shdr->Command)) {
280 kref_get(&mid->refcount);
281 spin_unlock(&GlobalMid_Lock);
285 spin_unlock(&GlobalMid_Lock);
290 smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
292 #ifdef CONFIG_CIFS_DEBUG2
293 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
295 cifs_server_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
296 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
298 cifs_server_dbg(VFS, "smb buf %p len %u\n", buf,
299 server->ops->calc_smb_size(buf, server));
304 smb2_need_neg(struct TCP_Server_Info *server)
306 return server->max_read == 0;
310 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
314 cifs_ses_server(ses)->CurrentMid = 0;
315 rc = SMB2_negotiate(xid, ses);
316 /* BB we probably don't need to retry with modern servers */
323 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
325 struct TCP_Server_Info *server = tcon->ses->server;
328 /* start with specified wsize, or default */
329 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
330 wsize = min_t(unsigned int, wsize, server->max_write);
331 #ifdef CONFIG_CIFS_SMB_DIRECT
334 wsize = min_t(unsigned int,
335 wsize, server->smbd_conn->max_fragmented_send_size);
337 wsize = min_t(unsigned int,
338 wsize, server->smbd_conn->max_readwrite_size);
341 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
342 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
348 smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
350 struct TCP_Server_Info *server = tcon->ses->server;
353 /* start with specified wsize, or default */
354 wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
355 wsize = min_t(unsigned int, wsize, server->max_write);
356 #ifdef CONFIG_CIFS_SMB_DIRECT
359 wsize = min_t(unsigned int,
360 wsize, server->smbd_conn->max_fragmented_send_size);
362 wsize = min_t(unsigned int,
363 wsize, server->smbd_conn->max_readwrite_size);
366 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
367 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
373 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
375 struct TCP_Server_Info *server = tcon->ses->server;
378 /* start with specified rsize, or default */
379 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
380 rsize = min_t(unsigned int, rsize, server->max_read);
381 #ifdef CONFIG_CIFS_SMB_DIRECT
384 rsize = min_t(unsigned int,
385 rsize, server->smbd_conn->max_fragmented_recv_size);
387 rsize = min_t(unsigned int,
388 rsize, server->smbd_conn->max_readwrite_size);
392 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
393 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
399 smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
401 struct TCP_Server_Info *server = tcon->ses->server;
404 /* start with specified rsize, or default */
405 rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
406 rsize = min_t(unsigned int, rsize, server->max_read);
407 #ifdef CONFIG_CIFS_SMB_DIRECT
410 rsize = min_t(unsigned int,
411 rsize, server->smbd_conn->max_fragmented_recv_size);
413 rsize = min_t(unsigned int,
414 rsize, server->smbd_conn->max_readwrite_size);
418 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
419 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
425 parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
427 struct cifs_server_iface **iface_list,
430 struct network_interface_info_ioctl_rsp *p;
431 struct sockaddr_in *addr4;
432 struct sockaddr_in6 *addr6;
433 struct iface_info_ipv4 *p4;
434 struct iface_info_ipv6 *p6;
435 struct cifs_server_iface *info;
445 * Fist pass: count and sanity check
448 bytes_left = buf_len;
450 while (bytes_left >= sizeof(*p)) {
452 next = le32_to_cpu(p->Next);
454 bytes_left -= sizeof(*p);
457 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
462 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
467 if (bytes_left || p->Next)
468 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
472 * Second pass: extract info to internal structure
475 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
482 bytes_left = buf_len;
484 while (bytes_left >= sizeof(*p)) {
485 info->speed = le64_to_cpu(p->LinkSpeed);
486 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE);
487 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE);
489 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
490 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
491 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
492 le32_to_cpu(p->Capability));
496 * The kernel and wire socket structures have the same
497 * layout and use network byte order but make the
498 * conversion explicit in case either one changes.
501 addr4 = (struct sockaddr_in *)&info->sockaddr;
502 p4 = (struct iface_info_ipv4 *)p->Buffer;
503 addr4->sin_family = AF_INET;
504 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
506 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
507 addr4->sin_port = cpu_to_be16(CIFS_PORT);
509 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
513 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
514 p6 = (struct iface_info_ipv6 *)p->Buffer;
515 addr6->sin6_family = AF_INET6;
516 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
518 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
519 addr6->sin6_flowinfo = 0;
520 addr6->sin6_scope_id = 0;
521 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
523 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
528 "%s: skipping unsupported socket family\n",
536 next = le32_to_cpu(p->Next);
539 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
557 static int compare_iface(const void *ia, const void *ib)
559 const struct cifs_server_iface *a = (struct cifs_server_iface *)ia;
560 const struct cifs_server_iface *b = (struct cifs_server_iface *)ib;
562 return a->speed == b->speed ? 0 : (a->speed > b->speed ? -1 : 1);
566 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
569 unsigned int ret_data_len = 0;
570 struct network_interface_info_ioctl_rsp *out_buf = NULL;
571 struct cifs_server_iface *iface_list;
573 struct cifs_ses *ses = tcon->ses;
575 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
576 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
577 NULL /* no data input */, 0 /* no data input */,
578 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
579 if (rc == -EOPNOTSUPP) {
581 "server does not support query network interfaces\n");
583 } else if (rc != 0) {
584 cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
588 rc = parse_server_interfaces(out_buf, ret_data_len,
589 &iface_list, &iface_count);
593 /* sort interfaces from fastest to slowest */
594 sort(iface_list, iface_count, sizeof(*iface_list), compare_iface, NULL);
596 spin_lock(&ses->iface_lock);
597 kfree(ses->iface_list);
598 ses->iface_list = iface_list;
599 ses->iface_count = iface_count;
600 ses->iface_last_update = jiffies;
601 spin_unlock(&ses->iface_lock);
609 smb2_close_cached_fid(struct kref *ref)
611 struct cached_fid *cfid = container_of(ref, struct cached_fid,
614 if (cfid->is_valid) {
615 cifs_dbg(FYI, "clear cached root file handle\n");
616 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
617 cfid->fid->volatile_fid);
618 cfid->is_valid = false;
619 cfid->file_all_info_is_valid = false;
620 cfid->has_lease = false;
624 void close_shroot(struct cached_fid *cfid)
626 mutex_lock(&cfid->fid_mutex);
627 kref_put(&cfid->refcount, smb2_close_cached_fid);
628 mutex_unlock(&cfid->fid_mutex);
631 void close_shroot_lease_locked(struct cached_fid *cfid)
633 if (cfid->has_lease) {
634 cfid->has_lease = false;
635 kref_put(&cfid->refcount, smb2_close_cached_fid);
639 void close_shroot_lease(struct cached_fid *cfid)
641 mutex_lock(&cfid->fid_mutex);
642 close_shroot_lease_locked(cfid);
643 mutex_unlock(&cfid->fid_mutex);
647 smb2_cached_lease_break(struct work_struct *work)
649 struct cached_fid *cfid = container_of(work,
650 struct cached_fid, lease_break);
652 close_shroot_lease(cfid);
656 * Open the directory at the root of a share
658 int open_shroot(unsigned int xid, struct cifs_tcon *tcon,
659 struct cifs_sb_info *cifs_sb, struct cifs_fid *pfid)
661 struct cifs_ses *ses = tcon->ses;
662 struct TCP_Server_Info *server = ses->server;
663 struct cifs_open_parms oparms;
664 struct smb2_create_rsp *o_rsp = NULL;
665 struct smb2_query_info_rsp *qi_rsp = NULL;
667 struct smb_rqst rqst[2];
668 struct kvec rsp_iov[2];
669 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
670 struct kvec qi_iov[1];
672 __le16 utf16_path = 0; /* Null - since an open of top of share */
673 u8 oplock = SMB2_OPLOCK_LEVEL_II;
675 mutex_lock(&tcon->crfid.fid_mutex);
676 if (tcon->crfid.is_valid) {
677 cifs_dbg(FYI, "found a cached root file handle\n");
678 memcpy(pfid, tcon->crfid.fid, sizeof(struct cifs_fid));
679 kref_get(&tcon->crfid.refcount);
680 mutex_unlock(&tcon->crfid.fid_mutex);
685 * We do not hold the lock for the open because in case
686 * SMB2_open needs to reconnect, it will end up calling
687 * cifs_mark_open_files_invalid() which takes the lock again
688 * thus causing a deadlock
691 mutex_unlock(&tcon->crfid.fid_mutex);
693 if (smb3_encryption_required(tcon))
694 flags |= CIFS_TRANSFORM_REQ;
696 memset(rqst, 0, sizeof(rqst));
697 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
698 memset(rsp_iov, 0, sizeof(rsp_iov));
701 memset(&open_iov, 0, sizeof(open_iov));
702 rqst[0].rq_iov = open_iov;
703 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
706 oparms.create_options = cifs_create_options(cifs_sb, 0);
707 oparms.desired_access = FILE_READ_ATTRIBUTES;
708 oparms.disposition = FILE_OPEN;
710 oparms.reconnect = false;
712 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, &utf16_path);
715 smb2_set_next_command(tcon, &rqst[0]);
717 memset(&qi_iov, 0, sizeof(qi_iov));
718 rqst[1].rq_iov = qi_iov;
721 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
722 COMPOUND_FID, FILE_ALL_INFORMATION,
724 sizeof(struct smb2_file_all_info) +
725 PATH_MAX * 2, 0, NULL);
729 smb2_set_related(&rqst[1]);
731 rc = compound_send_recv(xid, ses, flags, 2, rqst,
732 resp_buftype, rsp_iov);
733 mutex_lock(&tcon->crfid.fid_mutex);
736 * Now we need to check again as the cached root might have
737 * been successfully re-opened from a concurrent process
740 if (tcon->crfid.is_valid) {
741 /* work was already done */
743 /* stash fids for close() later */
744 struct cifs_fid fid = {
745 .persistent_fid = pfid->persistent_fid,
746 .volatile_fid = pfid->volatile_fid,
750 * caller expects this func to set pfid to a valid
751 * cached root, so we copy the existing one and get a
754 memcpy(pfid, tcon->crfid.fid, sizeof(*pfid));
755 kref_get(&tcon->crfid.refcount);
757 mutex_unlock(&tcon->crfid.fid_mutex);
760 /* close extra handle outside of crit sec */
761 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
766 /* Cached root is still invalid, continue normaly */
769 if (rc == -EREMCHG) {
770 tcon->need_reconnect = true;
771 printk_once(KERN_WARNING "server share %s deleted\n",
777 atomic_inc(&tcon->num_remote_opens);
779 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
780 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
781 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
782 #ifdef CONFIG_CIFS_DEBUG2
783 oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
784 #endif /* CIFS_DEBUG2 */
786 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
787 tcon->crfid.tcon = tcon;
788 tcon->crfid.is_valid = true;
789 kref_init(&tcon->crfid.refcount);
791 /* BB TBD check to see if oplock level check can be removed below */
792 if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
793 kref_get(&tcon->crfid.refcount);
794 tcon->crfid.has_lease = true;
795 smb2_parse_contexts(server, o_rsp,
797 oparms.fid->lease_key, &oplock, NULL);
801 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
802 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
804 if (!smb2_validate_and_copy_iov(
805 le16_to_cpu(qi_rsp->OutputBufferOffset),
806 sizeof(struct smb2_file_all_info),
807 &rsp_iov[1], sizeof(struct smb2_file_all_info),
808 (char *)&tcon->crfid.file_all_info))
809 tcon->crfid.file_all_info_is_valid = true;
812 mutex_unlock(&tcon->crfid.fid_mutex);
814 SMB2_open_free(&rqst[0]);
815 SMB2_query_info_free(&rqst[1]);
816 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
817 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
822 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
823 struct cifs_sb_info *cifs_sb)
826 __le16 srch_path = 0; /* Null - open root of share */
827 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
828 struct cifs_open_parms oparms;
830 bool no_cached_open = tcon->nohandlecache;
833 oparms.desired_access = FILE_READ_ATTRIBUTES;
834 oparms.disposition = FILE_OPEN;
835 oparms.create_options = cifs_create_options(cifs_sb, 0);
837 oparms.reconnect = false;
840 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
843 rc = open_shroot(xid, tcon, cifs_sb, &fid);
848 SMB3_request_interfaces(xid, tcon);
850 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
851 FS_ATTRIBUTE_INFORMATION);
852 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
853 FS_DEVICE_INFORMATION);
854 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
855 FS_VOLUME_INFORMATION);
856 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
857 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
859 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
861 close_shroot(&tcon->crfid);
865 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
866 struct cifs_sb_info *cifs_sb)
869 __le16 srch_path = 0; /* Null - open root of share */
870 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
871 struct cifs_open_parms oparms;
875 oparms.desired_access = FILE_READ_ATTRIBUTES;
876 oparms.disposition = FILE_OPEN;
877 oparms.create_options = cifs_create_options(cifs_sb, 0);
879 oparms.reconnect = false;
881 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
885 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
886 FS_ATTRIBUTE_INFORMATION);
887 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
888 FS_DEVICE_INFORMATION);
889 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
893 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
894 struct cifs_sb_info *cifs_sb, const char *full_path)
898 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
899 struct cifs_open_parms oparms;
902 if ((*full_path == 0) && tcon->crfid.is_valid)
905 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
910 oparms.desired_access = FILE_READ_ATTRIBUTES;
911 oparms.disposition = FILE_OPEN;
912 oparms.create_options = cifs_create_options(cifs_sb, 0);
914 oparms.reconnect = false;
916 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
922 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
928 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
929 struct cifs_sb_info *cifs_sb, const char *full_path,
930 u64 *uniqueid, FILE_ALL_INFO *data)
932 *uniqueid = le64_to_cpu(data->IndexNumber);
937 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
938 struct cifs_fid *fid, FILE_ALL_INFO *data)
941 struct smb2_file_all_info *smb2_data;
943 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
945 if (smb2_data == NULL)
948 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
951 move_smb2_info_to_cifs(data, smb2_data);
956 #ifdef CONFIG_CIFS_XATTR
958 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
959 struct smb2_file_full_ea_info *src, size_t src_size,
960 const unsigned char *ea_name)
963 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
965 size_t buf_size = dst_size;
966 size_t name_len, value_len, user_name_len;
968 while (src_size > 0) {
969 name = &src->ea_data[0];
970 name_len = (size_t)src->ea_name_length;
971 value = &src->ea_data[src->ea_name_length + 1];
972 value_len = (size_t)le16_to_cpu(src->ea_value_length);
977 if (src_size < 8 + name_len + 1 + value_len) {
978 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
984 if (ea_name_len == name_len &&
985 memcmp(ea_name, name, name_len) == 0) {
989 if (dst_size < value_len) {
993 memcpy(dst, value, value_len);
997 /* 'user.' plus a terminating null */
998 user_name_len = 5 + 1 + name_len;
1000 if (buf_size == 0) {
1001 /* skip copy - calc size only */
1002 rc += user_name_len;
1003 } else if (dst_size >= user_name_len) {
1004 dst_size -= user_name_len;
1005 memcpy(dst, "user.", 5);
1007 memcpy(dst, src->ea_data, name_len);
1011 rc += user_name_len;
1013 /* stop before overrun buffer */
1019 if (!src->next_entry_offset)
1022 if (src_size < le32_to_cpu(src->next_entry_offset)) {
1023 /* stop before overrun buffer */
1027 src_size -= le32_to_cpu(src->next_entry_offset);
1028 src = (void *)((char *)src +
1029 le32_to_cpu(src->next_entry_offset));
1032 /* didn't find the named attribute */
1041 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1042 const unsigned char *path, const unsigned char *ea_name,
1043 char *ea_data, size_t buf_size,
1044 struct cifs_sb_info *cifs_sb)
1048 struct kvec rsp_iov = {NULL, 0};
1049 int buftype = CIFS_NO_BUFFER;
1050 struct smb2_query_info_rsp *rsp;
1051 struct smb2_file_full_ea_info *info = NULL;
1053 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1057 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1059 FILE_FULL_EA_INFORMATION,
1062 MAX_SMB2_CREATE_RESPONSE_SIZE -
1063 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1064 &rsp_iov, &buftype, cifs_sb);
1067 * If ea_name is NULL (listxattr) and there are no EAs,
1068 * return 0 as it's not an error. Otherwise, the specified
1069 * ea_name was not found.
1071 if (!ea_name && rc == -ENODATA)
1076 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1077 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1078 le32_to_cpu(rsp->OutputBufferLength),
1080 sizeof(struct smb2_file_full_ea_info));
1084 info = (struct smb2_file_full_ea_info *)(
1085 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1086 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1087 le32_to_cpu(rsp->OutputBufferLength), ea_name);
1091 free_rsp_buf(buftype, rsp_iov.iov_base);
1097 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1098 const char *path, const char *ea_name, const void *ea_value,
1099 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1100 struct cifs_sb_info *cifs_sb)
1102 struct cifs_ses *ses = tcon->ses;
1103 __le16 *utf16_path = NULL;
1104 int ea_name_len = strlen(ea_name);
1107 struct smb_rqst rqst[3];
1108 int resp_buftype[3];
1109 struct kvec rsp_iov[3];
1110 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1111 struct cifs_open_parms oparms;
1112 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1113 struct cifs_fid fid;
1114 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1115 unsigned int size[1];
1117 struct smb2_file_full_ea_info *ea = NULL;
1118 struct kvec close_iov[1];
1121 if (smb3_encryption_required(tcon))
1122 flags |= CIFS_TRANSFORM_REQ;
1124 if (ea_name_len > 255)
1127 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1131 memset(rqst, 0, sizeof(rqst));
1132 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1133 memset(rsp_iov, 0, sizeof(rsp_iov));
1135 if (ses->server->ops->query_all_EAs) {
1137 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1146 memset(&open_iov, 0, sizeof(open_iov));
1147 rqst[0].rq_iov = open_iov;
1148 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1150 memset(&oparms, 0, sizeof(oparms));
1152 oparms.desired_access = FILE_WRITE_EA;
1153 oparms.disposition = FILE_OPEN;
1154 oparms.create_options = cifs_create_options(cifs_sb, 0);
1156 oparms.reconnect = false;
1158 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
1161 smb2_set_next_command(tcon, &rqst[0]);
1165 memset(&si_iov, 0, sizeof(si_iov));
1166 rqst[1].rq_iov = si_iov;
1167 rqst[1].rq_nvec = 1;
1169 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
1170 ea = kzalloc(len, GFP_KERNEL);
1176 ea->ea_name_length = ea_name_len;
1177 ea->ea_value_length = cpu_to_le16(ea_value_len);
1178 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1179 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1184 rc = SMB2_set_info_init(tcon, &rqst[1], COMPOUND_FID,
1185 COMPOUND_FID, current->tgid,
1186 FILE_FULL_EA_INFORMATION,
1187 SMB2_O_INFO_FILE, 0, data, size);
1188 smb2_set_next_command(tcon, &rqst[1]);
1189 smb2_set_related(&rqst[1]);
1193 memset(&close_iov, 0, sizeof(close_iov));
1194 rqst[2].rq_iov = close_iov;
1195 rqst[2].rq_nvec = 1;
1196 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1197 smb2_set_related(&rqst[2]);
1199 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1200 resp_buftype, rsp_iov);
1201 /* no need to bump num_remote_opens because handle immediately closed */
1206 SMB2_open_free(&rqst[0]);
1207 SMB2_set_info_free(&rqst[1]);
1208 SMB2_close_free(&rqst[2]);
1209 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1210 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1211 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1217 smb2_can_echo(struct TCP_Server_Info *server)
1219 return server->echoes;
1223 smb2_clear_stats(struct cifs_tcon *tcon)
1227 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1228 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1229 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1234 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1236 seq_puts(m, "\n\tShare Capabilities:");
1237 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1238 seq_puts(m, " DFS,");
1239 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1240 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1241 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1242 seq_puts(m, " SCALEOUT,");
1243 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1244 seq_puts(m, " CLUSTER,");
1245 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1246 seq_puts(m, " ASYMMETRIC,");
1247 if (tcon->capabilities == 0)
1248 seq_puts(m, " None");
1249 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1250 seq_puts(m, " Aligned,");
1251 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1252 seq_puts(m, " Partition Aligned,");
1253 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1254 seq_puts(m, " SSD,");
1255 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1256 seq_puts(m, " TRIM-support,");
1258 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1259 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1260 if (tcon->perf_sector_size)
1261 seq_printf(m, "\tOptimal sector size: 0x%x",
1262 tcon->perf_sector_size);
1263 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1267 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1269 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1270 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1273 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1274 * totals (requests sent) since those SMBs are per-session not per tcon
1276 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1277 (long long)(tcon->bytes_read),
1278 (long long)(tcon->bytes_written));
1279 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1280 atomic_read(&tcon->num_local_opens),
1281 atomic_read(&tcon->num_remote_opens));
1282 seq_printf(m, "\nTreeConnects: %d total %d failed",
1283 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1284 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1285 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1286 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1287 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1288 seq_printf(m, "\nCreates: %d total %d failed",
1289 atomic_read(&sent[SMB2_CREATE_HE]),
1290 atomic_read(&failed[SMB2_CREATE_HE]));
1291 seq_printf(m, "\nCloses: %d total %d failed",
1292 atomic_read(&sent[SMB2_CLOSE_HE]),
1293 atomic_read(&failed[SMB2_CLOSE_HE]));
1294 seq_printf(m, "\nFlushes: %d total %d failed",
1295 atomic_read(&sent[SMB2_FLUSH_HE]),
1296 atomic_read(&failed[SMB2_FLUSH_HE]));
1297 seq_printf(m, "\nReads: %d total %d failed",
1298 atomic_read(&sent[SMB2_READ_HE]),
1299 atomic_read(&failed[SMB2_READ_HE]));
1300 seq_printf(m, "\nWrites: %d total %d failed",
1301 atomic_read(&sent[SMB2_WRITE_HE]),
1302 atomic_read(&failed[SMB2_WRITE_HE]));
1303 seq_printf(m, "\nLocks: %d total %d failed",
1304 atomic_read(&sent[SMB2_LOCK_HE]),
1305 atomic_read(&failed[SMB2_LOCK_HE]));
1306 seq_printf(m, "\nIOCTLs: %d total %d failed",
1307 atomic_read(&sent[SMB2_IOCTL_HE]),
1308 atomic_read(&failed[SMB2_IOCTL_HE]));
1309 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1310 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1311 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1312 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1313 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1314 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1315 seq_printf(m, "\nQueryInfos: %d total %d failed",
1316 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1317 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1318 seq_printf(m, "\nSetInfos: %d total %d failed",
1319 atomic_read(&sent[SMB2_SET_INFO_HE]),
1320 atomic_read(&failed[SMB2_SET_INFO_HE]));
1321 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1322 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1323 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1327 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1329 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1330 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1332 cfile->fid.persistent_fid = fid->persistent_fid;
1333 cfile->fid.volatile_fid = fid->volatile_fid;
1334 #ifdef CONFIG_CIFS_DEBUG2
1335 cfile->fid.mid = fid->mid;
1336 #endif /* CIFS_DEBUG2 */
1337 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1339 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1340 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1344 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1345 struct cifs_fid *fid)
1347 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1351 smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
1352 struct cifsFileInfo *cfile)
1354 struct smb2_file_network_open_info file_inf;
1355 struct inode *inode;
1358 rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
1359 cfile->fid.volatile_fid, &file_inf);
1363 inode = d_inode(cfile->dentry);
1365 spin_lock(&inode->i_lock);
1366 CIFS_I(inode)->time = jiffies;
1368 /* Creation time should not need to be updated on close */
1369 if (file_inf.LastWriteTime)
1370 inode->i_mtime = cifs_NTtimeToUnix(file_inf.LastWriteTime);
1371 if (file_inf.ChangeTime)
1372 inode->i_ctime = cifs_NTtimeToUnix(file_inf.ChangeTime);
1373 if (file_inf.LastAccessTime)
1374 inode->i_atime = cifs_NTtimeToUnix(file_inf.LastAccessTime);
1377 * i_blocks is not related to (i_size / i_blksize),
1378 * but instead 512 byte (2**9) size is required for
1379 * calculating num blocks.
1381 if (le64_to_cpu(file_inf.AllocationSize) > 4096)
1383 (512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
1385 /* End of file and Attributes should not have to be updated on close */
1386 spin_unlock(&inode->i_lock);
1390 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1391 u64 persistent_fid, u64 volatile_fid,
1392 struct copychunk_ioctl *pcchunk)
1395 unsigned int ret_data_len;
1396 struct resume_key_req *res_key;
1398 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1399 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1400 NULL, 0 /* no input */, CIFSMaxBufSize,
1401 (char **)&res_key, &ret_data_len);
1404 cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1405 goto req_res_key_exit;
1407 if (ret_data_len < sizeof(struct resume_key_req)) {
1408 cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
1410 goto req_res_key_exit;
1412 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1420 smb2_ioctl_query_info(const unsigned int xid,
1421 struct cifs_tcon *tcon,
1422 struct cifs_sb_info *cifs_sb,
1423 __le16 *path, int is_dir,
1426 struct cifs_ses *ses = tcon->ses;
1427 char __user *arg = (char __user *)p;
1428 struct smb_query_info qi;
1429 struct smb_query_info __user *pqi;
1432 struct smb2_query_info_rsp *qi_rsp = NULL;
1433 struct smb2_ioctl_rsp *io_rsp = NULL;
1434 void *buffer = NULL;
1435 struct smb_rqst rqst[3];
1436 int resp_buftype[3];
1437 struct kvec rsp_iov[3];
1438 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1439 struct cifs_open_parms oparms;
1440 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1441 struct cifs_fid fid;
1442 struct kvec qi_iov[1];
1443 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1444 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1445 struct kvec close_iov[1];
1446 unsigned int size[2];
1448 int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
1450 memset(rqst, 0, sizeof(rqst));
1451 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1452 memset(rsp_iov, 0, sizeof(rsp_iov));
1454 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1457 if (qi.output_buffer_length > 1024)
1460 if (!ses || !(ses->server))
1463 if (smb3_encryption_required(tcon))
1464 flags |= CIFS_TRANSFORM_REQ;
1466 buffer = memdup_user(arg + sizeof(struct smb_query_info),
1467 qi.output_buffer_length);
1469 return PTR_ERR(buffer);
1472 memset(&open_iov, 0, sizeof(open_iov));
1473 rqst[0].rq_iov = open_iov;
1474 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1476 memset(&oparms, 0, sizeof(oparms));
1478 oparms.disposition = FILE_OPEN;
1479 oparms.create_options = cifs_create_options(cifs_sb, create_options);
1481 oparms.reconnect = false;
1483 if (qi.flags & PASSTHRU_FSCTL) {
1484 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1485 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1486 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1488 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1489 oparms.desired_access = GENERIC_ALL;
1491 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1492 oparms.desired_access = GENERIC_READ;
1494 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1495 oparms.desired_access = GENERIC_WRITE;
1498 } else if (qi.flags & PASSTHRU_SET_INFO) {
1499 oparms.desired_access = GENERIC_WRITE;
1501 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1504 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, path);
1507 smb2_set_next_command(tcon, &rqst[0]);
1510 if (qi.flags & PASSTHRU_FSCTL) {
1511 /* Can eventually relax perm check since server enforces too */
1512 if (!capable(CAP_SYS_ADMIN))
1515 memset(&io_iov, 0, sizeof(io_iov));
1516 rqst[1].rq_iov = io_iov;
1517 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1519 rc = SMB2_ioctl_init(tcon, &rqst[1],
1520 COMPOUND_FID, COMPOUND_FID,
1521 qi.info_type, true, buffer,
1522 qi.output_buffer_length,
1524 MAX_SMB2_CREATE_RESPONSE_SIZE -
1525 MAX_SMB2_CLOSE_RESPONSE_SIZE);
1527 } else if (qi.flags == PASSTHRU_SET_INFO) {
1528 /* Can eventually relax perm check since server enforces too */
1529 if (!capable(CAP_SYS_ADMIN))
1532 memset(&si_iov, 0, sizeof(si_iov));
1533 rqst[1].rq_iov = si_iov;
1534 rqst[1].rq_nvec = 1;
1539 rc = SMB2_set_info_init(tcon, &rqst[1],
1540 COMPOUND_FID, COMPOUND_FID,
1542 FILE_END_OF_FILE_INFORMATION,
1543 SMB2_O_INFO_FILE, 0, data, size);
1545 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1546 memset(&qi_iov, 0, sizeof(qi_iov));
1547 rqst[1].rq_iov = qi_iov;
1548 rqst[1].rq_nvec = 1;
1550 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
1551 COMPOUND_FID, qi.file_info_class,
1552 qi.info_type, qi.additional_information,
1553 qi.input_buffer_length,
1554 qi.output_buffer_length, buffer);
1555 } else { /* unknown flags */
1556 cifs_tcon_dbg(VFS, "invalid passthru query flags: 0x%x\n", qi.flags);
1562 smb2_set_next_command(tcon, &rqst[1]);
1563 smb2_set_related(&rqst[1]);
1566 memset(&close_iov, 0, sizeof(close_iov));
1567 rqst[2].rq_iov = close_iov;
1568 rqst[2].rq_nvec = 1;
1570 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1573 smb2_set_related(&rqst[2]);
1575 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1576 resp_buftype, rsp_iov);
1580 /* No need to bump num_remote_opens since handle immediately closed */
1581 if (qi.flags & PASSTHRU_FSCTL) {
1582 pqi = (struct smb_query_info __user *)arg;
1583 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1584 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1585 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1586 if (qi.input_buffer_length > 0 &&
1587 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1588 > rsp_iov[1].iov_len)
1591 if (copy_to_user(&pqi->input_buffer_length,
1592 &qi.input_buffer_length,
1593 sizeof(qi.input_buffer_length)))
1596 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1597 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1598 qi.input_buffer_length))
1601 pqi = (struct smb_query_info __user *)arg;
1602 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1603 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1604 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1605 if (copy_to_user(&pqi->input_buffer_length,
1606 &qi.input_buffer_length,
1607 sizeof(qi.input_buffer_length)))
1610 if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1611 qi.input_buffer_length))
1617 SMB2_open_free(&rqst[0]);
1618 if (qi.flags & PASSTHRU_FSCTL)
1619 SMB2_ioctl_free(&rqst[1]);
1621 SMB2_query_info_free(&rqst[1]);
1623 SMB2_close_free(&rqst[2]);
1624 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1625 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1626 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1635 smb2_copychunk_range(const unsigned int xid,
1636 struct cifsFileInfo *srcfile,
1637 struct cifsFileInfo *trgtfile, u64 src_off,
1638 u64 len, u64 dest_off)
1641 unsigned int ret_data_len;
1642 struct copychunk_ioctl *pcchunk;
1643 struct copychunk_ioctl_rsp *retbuf = NULL;
1644 struct cifs_tcon *tcon;
1645 int chunks_copied = 0;
1646 bool chunk_sizes_updated = false;
1647 ssize_t bytes_written, total_bytes_written = 0;
1649 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1651 if (pcchunk == NULL)
1654 cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
1655 /* Request a key from the server to identify the source of the copy */
1656 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1657 srcfile->fid.persistent_fid,
1658 srcfile->fid.volatile_fid, pcchunk);
1660 /* Note: request_res_key sets res_key null only if rc !=0 */
1664 /* For now array only one chunk long, will make more flexible later */
1665 pcchunk->ChunkCount = cpu_to_le32(1);
1666 pcchunk->Reserved = 0;
1667 pcchunk->Reserved2 = 0;
1669 tcon = tlink_tcon(trgtfile->tlink);
1672 pcchunk->SourceOffset = cpu_to_le64(src_off);
1673 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1675 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1677 /* Request server copy to target from src identified by key */
1678 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1679 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1680 true /* is_fsctl */, (char *)pcchunk,
1681 sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1682 (char **)&retbuf, &ret_data_len);
1685 sizeof(struct copychunk_ioctl_rsp)) {
1686 cifs_tcon_dbg(VFS, "invalid cchunk response size\n");
1690 if (retbuf->TotalBytesWritten == 0) {
1691 cifs_dbg(FYI, "no bytes copied\n");
1696 * Check if server claimed to write more than we asked
1698 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1699 le32_to_cpu(pcchunk->Length)) {
1700 cifs_tcon_dbg(VFS, "invalid copy chunk response\n");
1704 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1705 cifs_tcon_dbg(VFS, "invalid num chunks written\n");
1711 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1712 src_off += bytes_written;
1713 dest_off += bytes_written;
1714 len -= bytes_written;
1715 total_bytes_written += bytes_written;
1717 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1718 le32_to_cpu(retbuf->ChunksWritten),
1719 le32_to_cpu(retbuf->ChunkBytesWritten),
1721 } else if (rc == -EINVAL) {
1722 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1725 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1726 le32_to_cpu(retbuf->ChunksWritten),
1727 le32_to_cpu(retbuf->ChunkBytesWritten),
1728 le32_to_cpu(retbuf->TotalBytesWritten));
1731 * Check if this is the first request using these sizes,
1732 * (ie check if copy succeed once with original sizes
1733 * and check if the server gave us different sizes after
1734 * we already updated max sizes on previous request).
1735 * if not then why is the server returning an error now
1737 if ((chunks_copied != 0) || chunk_sizes_updated)
1740 /* Check that server is not asking us to grow size */
1741 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1742 tcon->max_bytes_chunk)
1743 tcon->max_bytes_chunk =
1744 le32_to_cpu(retbuf->ChunkBytesWritten);
1746 goto cchunk_out; /* server gave us bogus size */
1748 /* No need to change MaxChunks since already set to 1 */
1749 chunk_sizes_updated = true;
1760 return total_bytes_written;
1764 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1765 struct cifs_fid *fid)
1767 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1771 smb2_read_data_offset(char *buf)
1773 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1775 return rsp->DataOffset;
1779 smb2_read_data_length(char *buf, bool in_remaining)
1781 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1784 return le32_to_cpu(rsp->DataRemaining);
1786 return le32_to_cpu(rsp->DataLength);
1791 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1792 struct cifs_io_parms *parms, unsigned int *bytes_read,
1793 char **buf, int *buf_type)
1795 parms->persistent_fid = pfid->persistent_fid;
1796 parms->volatile_fid = pfid->volatile_fid;
1797 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1801 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1802 struct cifs_io_parms *parms, unsigned int *written,
1803 struct kvec *iov, unsigned long nr_segs)
1806 parms->persistent_fid = pfid->persistent_fid;
1807 parms->volatile_fid = pfid->volatile_fid;
1808 return SMB2_write(xid, parms, written, iov, nr_segs);
1811 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1812 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1813 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1815 struct cifsInodeInfo *cifsi;
1818 cifsi = CIFS_I(inode);
1820 /* if file already sparse don't bother setting sparse again */
1821 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1822 return true; /* already sparse */
1824 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1825 return true; /* already not sparse */
1828 * Can't check for sparse support on share the usual way via the
1829 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1830 * since Samba server doesn't set the flag on the share, yet
1831 * supports the set sparse FSCTL and returns sparse correctly
1832 * in the file attributes. If we fail setting sparse though we
1833 * mark that server does not support sparse files for this share
1834 * to avoid repeatedly sending the unsupported fsctl to server
1835 * if the file is repeatedly extended.
1837 if (tcon->broken_sparse_sup)
1840 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1841 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1843 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
1845 tcon->broken_sparse_sup = true;
1846 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1851 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1853 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1859 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1860 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1862 __le64 eof = cpu_to_le64(size);
1863 struct inode *inode;
1866 * If extending file more than one page make sparse. Many Linux fs
1867 * make files sparse by default when extending via ftruncate
1869 inode = d_inode(cfile->dentry);
1871 if (!set_alloc && (size > inode->i_size + 8192)) {
1872 __u8 set_sparse = 1;
1874 /* whether set sparse succeeds or not, extend the file */
1875 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1878 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1879 cfile->fid.volatile_fid, cfile->pid, &eof);
1883 smb2_duplicate_extents(const unsigned int xid,
1884 struct cifsFileInfo *srcfile,
1885 struct cifsFileInfo *trgtfile, u64 src_off,
1886 u64 len, u64 dest_off)
1889 unsigned int ret_data_len;
1890 struct duplicate_extents_to_file dup_ext_buf;
1891 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1893 /* server fileays advertise duplicate extent support with this flag */
1894 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1895 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1898 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1899 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1900 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1901 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1902 dup_ext_buf.ByteCount = cpu_to_le64(len);
1903 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
1904 src_off, dest_off, len);
1906 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1908 goto duplicate_extents_out;
1910 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1911 trgtfile->fid.volatile_fid,
1912 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
1913 true /* is_fsctl */,
1914 (char *)&dup_ext_buf,
1915 sizeof(struct duplicate_extents_to_file),
1916 CIFSMaxBufSize, NULL,
1919 if (ret_data_len > 0)
1920 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
1922 duplicate_extents_out:
1927 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1928 struct cifsFileInfo *cfile)
1930 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1931 cfile->fid.volatile_fid);
1935 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1936 struct cifsFileInfo *cfile)
1938 struct fsctl_set_integrity_information_req integr_info;
1939 unsigned int ret_data_len;
1941 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1942 integr_info.Flags = 0;
1943 integr_info.Reserved = 0;
1945 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1946 cfile->fid.volatile_fid,
1947 FSCTL_SET_INTEGRITY_INFORMATION,
1948 true /* is_fsctl */,
1949 (char *)&integr_info,
1950 sizeof(struct fsctl_set_integrity_information_req),
1951 CIFSMaxBufSize, NULL,
1956 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
1957 #define GMT_TOKEN_SIZE 50
1959 #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
1962 * Input buffer contains (empty) struct smb_snapshot array with size filled in
1963 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
1966 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1967 struct cifsFileInfo *cfile, void __user *ioc_buf)
1969 char *retbuf = NULL;
1970 unsigned int ret_data_len = 0;
1972 u32 max_response_size;
1973 struct smb_snapshot_array snapshot_in;
1976 * On the first query to enumerate the list of snapshots available
1977 * for this volume the buffer begins with 0 (number of snapshots
1978 * which can be returned is zero since at that point we do not know
1979 * how big the buffer needs to be). On the second query,
1980 * it (ret_data_len) is set to number of snapshots so we can
1981 * know to set the maximum response size larger (see below).
1983 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
1987 * Note that for snapshot queries that servers like Azure expect that
1988 * the first query be minimal size (and just used to get the number/size
1989 * of previous versions) so response size must be specified as EXACTLY
1990 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
1993 if (ret_data_len == 0)
1994 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
1996 max_response_size = CIFSMaxBufSize;
1998 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1999 cfile->fid.volatile_fid,
2000 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
2001 true /* is_fsctl */,
2002 NULL, 0 /* no input data */, max_response_size,
2005 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2010 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2012 if (copy_from_user(&snapshot_in, ioc_buf,
2013 sizeof(struct smb_snapshot_array))) {
2020 * Check for min size, ie not large enough to fit even one GMT
2021 * token (snapshot). On the first ioctl some users may pass in
2022 * smaller size (or zero) to simply get the size of the array
2023 * so the user space caller can allocate sufficient memory
2024 * and retry the ioctl again with larger array size sufficient
2025 * to hold all of the snapshot GMT tokens on the second try.
2027 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2028 ret_data_len = sizeof(struct smb_snapshot_array);
2031 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2032 * the snapshot array (of 50 byte GMT tokens) each
2033 * representing an available previous version of the data
2035 if (ret_data_len > (snapshot_in.snapshot_array_size +
2036 sizeof(struct smb_snapshot_array)))
2037 ret_data_len = snapshot_in.snapshot_array_size +
2038 sizeof(struct smb_snapshot_array);
2040 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2051 smb3_notify(const unsigned int xid, struct file *pfile,
2052 void __user *ioc_buf)
2054 struct smb3_notify notify;
2055 struct dentry *dentry = pfile->f_path.dentry;
2056 struct inode *inode = file_inode(pfile);
2057 struct cifs_sb_info *cifs_sb;
2058 struct cifs_open_parms oparms;
2059 struct cifs_fid fid;
2060 struct cifs_tcon *tcon;
2061 unsigned char *path = NULL;
2062 __le16 *utf16_path = NULL;
2063 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2066 path = build_path_from_dentry(dentry);
2070 cifs_sb = CIFS_SB(inode->i_sb);
2072 utf16_path = cifs_convert_path_to_utf16(path + 1, cifs_sb);
2073 if (utf16_path == NULL) {
2078 if (copy_from_user(¬ify, ioc_buf, sizeof(struct smb3_notify))) {
2083 tcon = cifs_sb_master_tcon(cifs_sb);
2085 oparms.desired_access = FILE_READ_ATTRIBUTES;
2086 oparms.disposition = FILE_OPEN;
2087 oparms.create_options = cifs_create_options(cifs_sb, 0);
2089 oparms.reconnect = false;
2091 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2095 rc = SMB2_change_notify(xid, tcon, fid.persistent_fid, fid.volatile_fid,
2096 notify.watch_tree, notify.completion_filter);
2098 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2100 cifs_dbg(FYI, "change notify for path %s rc %d\n", path, rc);
2109 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2110 const char *path, struct cifs_sb_info *cifs_sb,
2111 struct cifs_fid *fid, __u16 search_flags,
2112 struct cifs_search_info *srch_inf)
2115 struct smb_rqst rqst[2];
2116 struct kvec rsp_iov[2];
2117 int resp_buftype[2];
2118 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2119 struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
2121 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2122 struct cifs_open_parms oparms;
2123 struct smb2_query_directory_rsp *qd_rsp = NULL;
2124 struct smb2_create_rsp *op_rsp = NULL;
2126 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2130 if (smb3_encryption_required(tcon))
2131 flags |= CIFS_TRANSFORM_REQ;
2133 memset(rqst, 0, sizeof(rqst));
2134 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2135 memset(rsp_iov, 0, sizeof(rsp_iov));
2138 memset(&open_iov, 0, sizeof(open_iov));
2139 rqst[0].rq_iov = open_iov;
2140 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2143 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2144 oparms.disposition = FILE_OPEN;
2145 oparms.create_options = cifs_create_options(cifs_sb, 0);
2147 oparms.reconnect = false;
2149 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2152 smb2_set_next_command(tcon, &rqst[0]);
2154 /* Query directory */
2155 srch_inf->entries_in_buffer = 0;
2156 srch_inf->index_of_last_entry = 2;
2158 memset(&qd_iov, 0, sizeof(qd_iov));
2159 rqst[1].rq_iov = qd_iov;
2160 rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
2162 rc = SMB2_query_directory_init(xid, tcon, &rqst[1],
2163 COMPOUND_FID, COMPOUND_FID,
2164 0, srch_inf->info_level);
2168 smb2_set_related(&rqst[1]);
2170 rc = compound_send_recv(xid, tcon->ses, flags, 2, rqst,
2171 resp_buftype, rsp_iov);
2173 /* If the open failed there is nothing to do */
2174 op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
2175 if (op_rsp == NULL || op_rsp->sync_hdr.Status != STATUS_SUCCESS) {
2176 cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
2179 fid->persistent_fid = op_rsp->PersistentFileId;
2180 fid->volatile_fid = op_rsp->VolatileFileId;
2182 /* Anything else than ENODATA means a genuine error */
2183 if (rc && rc != -ENODATA) {
2184 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2185 cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
2186 trace_smb3_query_dir_err(xid, fid->persistent_fid,
2187 tcon->tid, tcon->ses->Suid, 0, 0, rc);
2191 qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
2192 if (qd_rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
2193 trace_smb3_query_dir_done(xid, fid->persistent_fid,
2194 tcon->tid, tcon->ses->Suid, 0, 0);
2195 srch_inf->endOfSearch = true;
2200 rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
2203 trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
2204 tcon->ses->Suid, 0, 0, rc);
2207 resp_buftype[1] = CIFS_NO_BUFFER;
2209 trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
2210 tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
2214 SMB2_open_free(&rqst[0]);
2215 SMB2_query_directory_free(&rqst[1]);
2216 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2217 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2222 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2223 struct cifs_fid *fid, __u16 search_flags,
2224 struct cifs_search_info *srch_inf)
2226 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2227 fid->volatile_fid, 0, srch_inf);
2231 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2232 struct cifs_fid *fid)
2234 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2238 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2239 * the number of credits and return true. Otherwise - return false.
2242 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
2244 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2246 if (shdr->Status != STATUS_PENDING)
2249 if (shdr->CreditRequest) {
2250 spin_lock(&server->req_lock);
2251 server->credits += le16_to_cpu(shdr->CreditRequest);
2252 spin_unlock(&server->req_lock);
2253 wake_up(&server->request_q);
2260 smb2_is_session_expired(char *buf)
2262 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2264 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2265 shdr->Status != STATUS_USER_SESSION_DELETED)
2268 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2269 le16_to_cpu(shdr->Command),
2270 le64_to_cpu(shdr->MessageId));
2271 cifs_dbg(FYI, "Session expired or deleted\n");
2277 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2278 struct cifsInodeInfo *cinode)
2280 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2281 return SMB2_lease_break(0, tcon, cinode->lease_key,
2282 smb2_get_lease_state(cinode));
2284 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2286 CIFS_CACHE_READ(cinode) ? 1 : 0);
2290 smb2_set_related(struct smb_rqst *rqst)
2292 struct smb2_sync_hdr *shdr;
2294 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2296 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2299 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2302 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2305 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2307 struct smb2_sync_hdr *shdr;
2308 struct cifs_ses *ses = tcon->ses;
2309 struct TCP_Server_Info *server = ses->server;
2310 unsigned long len = smb_rqst_len(server, rqst);
2313 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2315 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2319 /* SMB headers in a compound are 8 byte aligned. */
2321 /* No padding needed */
2325 num_padding = 8 - (len & 7);
2326 if (!smb3_encryption_required(tcon)) {
2328 * If we do not have encryption then we can just add an extra
2329 * iov for the padding.
2331 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2332 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2337 * We can not add a small padding iov for the encryption case
2338 * because the encryption framework can not handle the padding
2340 * We have to flatten this into a single buffer and add
2341 * the padding to it.
2343 for (i = 1; i < rqst->rq_nvec; i++) {
2344 memcpy(rqst->rq_iov[0].iov_base +
2345 rqst->rq_iov[0].iov_len,
2346 rqst->rq_iov[i].iov_base,
2347 rqst->rq_iov[i].iov_len);
2348 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2350 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2352 rqst->rq_iov[0].iov_len += num_padding;
2358 shdr->NextCommand = cpu_to_le32(len);
2362 * Passes the query info response back to the caller on success.
2363 * Caller need to free this with free_rsp_buf().
2366 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2367 __le16 *utf16_path, u32 desired_access,
2368 u32 class, u32 type, u32 output_len,
2369 struct kvec *rsp, int *buftype,
2370 struct cifs_sb_info *cifs_sb)
2372 struct cifs_ses *ses = tcon->ses;
2374 struct smb_rqst rqst[3];
2375 int resp_buftype[3];
2376 struct kvec rsp_iov[3];
2377 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2378 struct kvec qi_iov[1];
2379 struct kvec close_iov[1];
2380 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2381 struct cifs_open_parms oparms;
2382 struct cifs_fid fid;
2385 if (smb3_encryption_required(tcon))
2386 flags |= CIFS_TRANSFORM_REQ;
2388 memset(rqst, 0, sizeof(rqst));
2389 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2390 memset(rsp_iov, 0, sizeof(rsp_iov));
2392 memset(&open_iov, 0, sizeof(open_iov));
2393 rqst[0].rq_iov = open_iov;
2394 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2397 oparms.desired_access = desired_access;
2398 oparms.disposition = FILE_OPEN;
2399 oparms.create_options = cifs_create_options(cifs_sb, 0);
2401 oparms.reconnect = false;
2403 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2406 smb2_set_next_command(tcon, &rqst[0]);
2408 memset(&qi_iov, 0, sizeof(qi_iov));
2409 rqst[1].rq_iov = qi_iov;
2410 rqst[1].rq_nvec = 1;
2412 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
2418 smb2_set_next_command(tcon, &rqst[1]);
2419 smb2_set_related(&rqst[1]);
2421 memset(&close_iov, 0, sizeof(close_iov));
2422 rqst[2].rq_iov = close_iov;
2423 rqst[2].rq_nvec = 1;
2425 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2428 smb2_set_related(&rqst[2]);
2430 rc = compound_send_recv(xid, ses, flags, 3, rqst,
2431 resp_buftype, rsp_iov);
2433 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2434 if (rc == -EREMCHG) {
2435 tcon->need_reconnect = true;
2436 printk_once(KERN_WARNING "server share %s deleted\n",
2442 *buftype = resp_buftype[1];
2445 SMB2_open_free(&rqst[0]);
2446 SMB2_query_info_free(&rqst[1]);
2447 SMB2_close_free(&rqst[2]);
2448 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2449 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2454 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2455 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2457 struct smb2_query_info_rsp *rsp;
2458 struct smb2_fs_full_size_info *info = NULL;
2459 __le16 utf16_path = 0; /* Null - open root of share */
2460 struct kvec rsp_iov = {NULL, 0};
2461 int buftype = CIFS_NO_BUFFER;
2465 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2466 FILE_READ_ATTRIBUTES,
2467 FS_FULL_SIZE_INFORMATION,
2468 SMB2_O_INFO_FILESYSTEM,
2469 sizeof(struct smb2_fs_full_size_info),
2470 &rsp_iov, &buftype, cifs_sb);
2474 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2475 buf->f_type = SMB2_MAGIC_NUMBER;
2476 info = (struct smb2_fs_full_size_info *)(
2477 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2478 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2479 le32_to_cpu(rsp->OutputBufferLength),
2481 sizeof(struct smb2_fs_full_size_info));
2483 smb2_copy_fs_info_to_kstatfs(info, buf);
2486 free_rsp_buf(buftype, rsp_iov.iov_base);
2491 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2492 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2495 __le16 srch_path = 0; /* Null - open root of share */
2496 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2497 struct cifs_open_parms oparms;
2498 struct cifs_fid fid;
2500 if (!tcon->posix_extensions)
2501 return smb2_queryfs(xid, tcon, cifs_sb, buf);
2504 oparms.desired_access = FILE_READ_ATTRIBUTES;
2505 oparms.disposition = FILE_OPEN;
2506 oparms.create_options = cifs_create_options(cifs_sb, 0);
2508 oparms.reconnect = false;
2510 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
2514 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2515 fid.volatile_fid, buf);
2516 buf->f_type = SMB2_MAGIC_NUMBER;
2517 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2522 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2524 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2525 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2529 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2530 __u64 length, __u32 type, int lock, int unlock, bool wait)
2532 if (unlock && !lock)
2533 type = SMB2_LOCKFLAG_UNLOCK;
2534 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2535 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2536 current->tgid, length, offset, type, wait);
2540 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2542 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2546 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2548 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2552 smb2_new_lease_key(struct cifs_fid *fid)
2554 generate_random_uuid(fid->lease_key);
2558 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2559 const char *search_name,
2560 struct dfs_info3_param **target_nodes,
2561 unsigned int *num_of_nodes,
2562 const struct nls_table *nls_codepage, int remap)
2565 __le16 *utf16_path = NULL;
2566 int utf16_path_len = 0;
2567 struct cifs_tcon *tcon;
2568 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2569 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2570 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2572 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
2575 * Try to use the IPC tcon, otherwise just use any
2577 tcon = ses->tcon_ipc;
2579 spin_lock(&cifs_tcp_ses_lock);
2580 tcon = list_first_entry_or_null(&ses->tcon_list,
2585 spin_unlock(&cifs_tcp_ses_lock);
2589 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2595 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2597 nls_codepage, remap);
2603 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2604 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2610 /* Highest DFS referral version understood */
2611 dfs_req->MaxReferralLevel = DFS_VERSION;
2613 /* Path to resolve in an UTF-16 null-terminated string */
2614 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2617 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2618 FSCTL_DFS_GET_REFERRALS,
2619 true /* is_fsctl */,
2620 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
2621 (char **)&dfs_rsp, &dfs_rsp_size);
2622 } while (rc == -EAGAIN);
2625 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
2626 cifs_tcon_dbg(VFS, "ioctl error in %s rc=%d\n", __func__, rc);
2630 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2631 num_of_nodes, target_nodes,
2632 nls_codepage, remap, search_name,
2633 true /* is_unicode */);
2635 cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
2640 if (tcon && !tcon->ipc) {
2641 /* ipc tcons are not refcounted */
2642 spin_lock(&cifs_tcp_ses_lock);
2644 spin_unlock(&cifs_tcp_ses_lock);
2653 parse_reparse_posix(struct reparse_posix_data *symlink_buf,
2654 u32 plen, char **target_path,
2655 struct cifs_sb_info *cifs_sb)
2659 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
2660 len = le16_to_cpu(symlink_buf->ReparseDataLength);
2662 if (le64_to_cpu(symlink_buf->InodeType) != NFS_SPECFILE_LNK) {
2663 cifs_dbg(VFS, "%lld not a supported symlink type\n",
2664 le64_to_cpu(symlink_buf->InodeType));
2668 *target_path = cifs_strndup_from_utf16(
2669 symlink_buf->PathBuffer,
2670 len, true, cifs_sb->local_nls);
2671 if (!(*target_path))
2674 convert_delimiter(*target_path, '/');
2675 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2681 parse_reparse_symlink(struct reparse_symlink_data_buffer *symlink_buf,
2682 u32 plen, char **target_path,
2683 struct cifs_sb_info *cifs_sb)
2685 unsigned int sub_len;
2686 unsigned int sub_offset;
2688 /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
2690 sub_offset = le16_to_cpu(symlink_buf->SubstituteNameOffset);
2691 sub_len = le16_to_cpu(symlink_buf->SubstituteNameLength);
2692 if (sub_offset + 20 > plen ||
2693 sub_offset + sub_len + 20 > plen) {
2694 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
2698 *target_path = cifs_strndup_from_utf16(
2699 symlink_buf->PathBuffer + sub_offset,
2700 sub_len, true, cifs_sb->local_nls);
2701 if (!(*target_path))
2704 convert_delimiter(*target_path, '/');
2705 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2711 parse_reparse_point(struct reparse_data_buffer *buf,
2712 u32 plen, char **target_path,
2713 struct cifs_sb_info *cifs_sb)
2715 if (plen < sizeof(struct reparse_data_buffer)) {
2716 cifs_dbg(VFS, "reparse buffer is too small. Must be "
2717 "at least 8 bytes but was %d\n", plen);
2721 if (plen < le16_to_cpu(buf->ReparseDataLength) +
2722 sizeof(struct reparse_data_buffer)) {
2723 cifs_dbg(VFS, "srv returned invalid reparse buf "
2724 "length: %d\n", plen);
2728 /* See MS-FSCC 2.1.2 */
2729 switch (le32_to_cpu(buf->ReparseTag)) {
2730 case IO_REPARSE_TAG_NFS:
2731 return parse_reparse_posix(
2732 (struct reparse_posix_data *)buf,
2733 plen, target_path, cifs_sb);
2734 case IO_REPARSE_TAG_SYMLINK:
2735 return parse_reparse_symlink(
2736 (struct reparse_symlink_data_buffer *)buf,
2737 plen, target_path, cifs_sb);
2739 cifs_dbg(VFS, "srv returned unknown symlink buffer "
2740 "tag:0x%08x\n", le32_to_cpu(buf->ReparseTag));
2745 #define SMB2_SYMLINK_STRUCT_SIZE \
2746 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2749 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
2750 struct cifs_sb_info *cifs_sb, const char *full_path,
2751 char **target_path, bool is_reparse_point)
2754 __le16 *utf16_path = NULL;
2755 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2756 struct cifs_open_parms oparms;
2757 struct cifs_fid fid;
2758 struct kvec err_iov = {NULL, 0};
2759 struct smb2_err_rsp *err_buf = NULL;
2760 struct smb2_symlink_err_rsp *symlink;
2761 unsigned int sub_len;
2762 unsigned int sub_offset;
2763 unsigned int print_len;
2764 unsigned int print_offset;
2766 struct smb_rqst rqst[3];
2767 int resp_buftype[3];
2768 struct kvec rsp_iov[3];
2769 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2770 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2771 struct kvec close_iov[1];
2772 struct smb2_create_rsp *create_rsp;
2773 struct smb2_ioctl_rsp *ioctl_rsp;
2774 struct reparse_data_buffer *reparse_buf;
2775 int create_options = is_reparse_point ? OPEN_REPARSE_POINT : 0;
2778 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2780 *target_path = NULL;
2782 if (smb3_encryption_required(tcon))
2783 flags |= CIFS_TRANSFORM_REQ;
2785 memset(rqst, 0, sizeof(rqst));
2786 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2787 memset(rsp_iov, 0, sizeof(rsp_iov));
2789 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2794 memset(&open_iov, 0, sizeof(open_iov));
2795 rqst[0].rq_iov = open_iov;
2796 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2798 memset(&oparms, 0, sizeof(oparms));
2800 oparms.desired_access = FILE_READ_ATTRIBUTES;
2801 oparms.disposition = FILE_OPEN;
2802 oparms.create_options = cifs_create_options(cifs_sb, create_options);
2804 oparms.reconnect = false;
2806 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2809 smb2_set_next_command(tcon, &rqst[0]);
2813 memset(&io_iov, 0, sizeof(io_iov));
2814 rqst[1].rq_iov = io_iov;
2815 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
2817 rc = SMB2_ioctl_init(tcon, &rqst[1], fid.persistent_fid,
2818 fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
2819 true /* is_fctl */, NULL, 0,
2821 MAX_SMB2_CREATE_RESPONSE_SIZE -
2822 MAX_SMB2_CLOSE_RESPONSE_SIZE);
2826 smb2_set_next_command(tcon, &rqst[1]);
2827 smb2_set_related(&rqst[1]);
2831 memset(&close_iov, 0, sizeof(close_iov));
2832 rqst[2].rq_iov = close_iov;
2833 rqst[2].rq_nvec = 1;
2835 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2839 smb2_set_related(&rqst[2]);
2841 rc = compound_send_recv(xid, tcon->ses, flags, 3, rqst,
2842 resp_buftype, rsp_iov);
2844 create_rsp = rsp_iov[0].iov_base;
2845 if (create_rsp && create_rsp->sync_hdr.Status)
2846 err_iov = rsp_iov[0];
2847 ioctl_rsp = rsp_iov[1].iov_base;
2850 * Open was successful and we got an ioctl response.
2852 if ((rc == 0) && (is_reparse_point)) {
2853 /* See MS-FSCC 2.3.23 */
2855 reparse_buf = (struct reparse_data_buffer *)
2856 ((char *)ioctl_rsp +
2857 le32_to_cpu(ioctl_rsp->OutputOffset));
2858 plen = le32_to_cpu(ioctl_rsp->OutputCount);
2860 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
2861 rsp_iov[1].iov_len) {
2862 cifs_tcon_dbg(VFS, "srv returned invalid ioctl len: %d\n",
2868 rc = parse_reparse_point(reparse_buf, plen, target_path,
2873 if (!rc || !err_iov.iov_base) {
2878 err_buf = err_iov.iov_base;
2879 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
2880 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
2885 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
2886 if (le32_to_cpu(symlink->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
2887 le32_to_cpu(symlink->ReparseTag) != IO_REPARSE_TAG_SYMLINK) {
2892 /* open must fail on symlink - reset rc */
2894 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
2895 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
2896 print_len = le16_to_cpu(symlink->PrintNameLength);
2897 print_offset = le16_to_cpu(symlink->PrintNameOffset);
2899 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
2904 if (err_iov.iov_len <
2905 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
2910 *target_path = cifs_strndup_from_utf16(
2911 (char *)symlink->PathBuffer + sub_offset,
2912 sub_len, true, cifs_sb->local_nls);
2913 if (!(*target_path)) {
2917 convert_delimiter(*target_path, '/');
2918 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2921 cifs_dbg(FYI, "query symlink rc %d\n", rc);
2923 SMB2_open_free(&rqst[0]);
2924 SMB2_ioctl_free(&rqst[1]);
2925 SMB2_close_free(&rqst[2]);
2926 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2927 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2928 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2932 static struct cifs_ntsd *
2933 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
2934 const struct cifs_fid *cifsfid, u32 *pacllen)
2936 struct cifs_ntsd *pntsd = NULL;
2938 int rc = -EOPNOTSUPP;
2939 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2942 return ERR_CAST(tlink);
2945 cifs_dbg(FYI, "trying to get acl\n");
2947 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
2948 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
2951 cifs_put_tlink(tlink);
2953 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2960 static struct cifs_ntsd *
2961 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
2962 const char *path, u32 *pacllen)
2964 struct cifs_ntsd *pntsd = NULL;
2965 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2968 struct cifs_tcon *tcon;
2969 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2970 struct cifs_fid fid;
2971 struct cifs_open_parms oparms;
2974 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
2976 return ERR_CAST(tlink);
2978 tcon = tlink_tcon(tlink);
2981 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2989 oparms.desired_access = READ_CONTROL;
2990 oparms.disposition = FILE_OPEN;
2991 oparms.create_options = cifs_create_options(cifs_sb, 0);
2993 oparms.reconnect = false;
2995 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2998 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2999 fid.volatile_fid, (void **)&pntsd, pacllen);
3000 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3003 cifs_put_tlink(tlink);
3006 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3013 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
3014 struct inode *inode, const char *path, int aclflag)
3016 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3018 int rc, access_flags = 0;
3019 struct cifs_tcon *tcon;
3020 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3021 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3022 struct cifs_fid fid;
3023 struct cifs_open_parms oparms;
3026 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
3028 return PTR_ERR(tlink);
3030 tcon = tlink_tcon(tlink);
3033 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
3034 access_flags = WRITE_OWNER;
3036 access_flags = WRITE_DAC;
3038 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3046 oparms.desired_access = access_flags;
3047 oparms.create_options = cifs_create_options(cifs_sb, 0);
3048 oparms.disposition = FILE_OPEN;
3051 oparms.reconnect = false;
3053 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
3056 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3057 fid.volatile_fid, pnntsd, acllen, aclflag);
3058 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3061 cifs_put_tlink(tlink);
3066 /* Retrieve an ACL from the server */
3067 static struct cifs_ntsd *
3068 get_smb2_acl(struct cifs_sb_info *cifs_sb,
3069 struct inode *inode, const char *path,
3072 struct cifs_ntsd *pntsd = NULL;
3073 struct cifsFileInfo *open_file = NULL;
3076 open_file = find_readable_file(CIFS_I(inode), true);
3078 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
3080 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
3081 cifsFileInfo_put(open_file);
3085 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3086 loff_t offset, loff_t len, bool keep_size)
3088 struct cifs_ses *ses = tcon->ses;
3089 struct inode *inode;
3090 struct cifsInodeInfo *cifsi;
3091 struct cifsFileInfo *cfile = file->private_data;
3092 struct file_zero_data_information fsctl_buf;
3099 inode = d_inode(cfile->dentry);
3100 cifsi = CIFS_I(inode);
3102 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3103 ses->Suid, offset, len);
3106 /* if file not oplocked can't be sure whether asking to extend size */
3107 if (!CIFS_CACHE_READ(cifsi))
3108 if (keep_size == false) {
3110 trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
3111 tcon->tid, ses->Suid, offset, len, rc);
3116 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3118 fsctl_buf.FileOffset = cpu_to_le64(offset);
3119 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3121 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3122 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
3124 sizeof(struct file_zero_data_information),
3127 goto zero_range_exit;
3130 * do we also need to change the size of the file?
3132 if (keep_size == false && i_size_read(inode) < offset + len) {
3133 eof = cpu_to_le64(offset + len);
3134 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3135 cfile->fid.volatile_fid, cfile->pid, &eof);
3141 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3142 ses->Suid, offset, len, rc);
3144 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3145 ses->Suid, offset, len);
3149 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3150 loff_t offset, loff_t len)
3152 struct inode *inode;
3153 struct cifsFileInfo *cfile = file->private_data;
3154 struct file_zero_data_information fsctl_buf;
3157 __u8 set_sparse = 1;
3161 inode = d_inode(cfile->dentry);
3163 /* Need to make file sparse, if not already, before freeing range. */
3164 /* Consider adding equivalent for compressed since it could also work */
3165 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3171 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3173 fsctl_buf.FileOffset = cpu_to_le64(offset);
3174 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3176 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3177 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3178 true /* is_fctl */, (char *)&fsctl_buf,
3179 sizeof(struct file_zero_data_information),
3180 CIFSMaxBufSize, NULL, NULL);
3185 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3186 loff_t off, loff_t len, bool keep_size)
3188 struct inode *inode;
3189 struct cifsInodeInfo *cifsi;
3190 struct cifsFileInfo *cfile = file->private_data;
3191 long rc = -EOPNOTSUPP;
3197 inode = d_inode(cfile->dentry);
3198 cifsi = CIFS_I(inode);
3200 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3201 tcon->ses->Suid, off, len);
3202 /* if file not oplocked can't be sure whether asking to extend size */
3203 if (!CIFS_CACHE_READ(cifsi))
3204 if (keep_size == false) {
3205 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3206 tcon->tid, tcon->ses->Suid, off, len, rc);
3212 * Extending the file
3214 if ((keep_size == false) && i_size_read(inode) < off + len) {
3215 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0)
3216 smb2_set_sparse(xid, tcon, cfile, inode, false);
3218 eof = cpu_to_le64(off + len);
3219 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3220 cfile->fid.volatile_fid, cfile->pid, &eof);
3222 cifsi->server_eof = off + len;
3223 cifs_setsize(inode, off + len);
3224 cifs_truncate_page(inode->i_mapping, inode->i_size);
3225 truncate_setsize(inode, off + len);
3231 * Files are non-sparse by default so falloc may be a no-op
3232 * Must check if file sparse. If not sparse, and since we are not
3233 * extending then no need to do anything since file already allocated
3235 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
3240 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3242 * Check if falloc starts within first few pages of file
3243 * and ends within a few pages of the end of file to
3244 * ensure that most of file is being forced to be
3245 * fallocated now. If so then setting whole file sparse
3246 * ie potentially making a few extra pages at the beginning
3247 * or end of the file non-sparse via set_sparse is harmless.
3249 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3255 smb2_set_sparse(xid, tcon, cfile, inode, false);
3260 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3261 tcon->ses->Suid, off, len, rc);
3263 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3264 tcon->ses->Suid, off, len);
3270 static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3272 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3273 struct cifsInodeInfo *cifsi;
3274 struct inode *inode;
3276 struct file_allocated_range_buffer in_data, *out_data = NULL;
3280 if (whence != SEEK_HOLE && whence != SEEK_DATA)
3281 return generic_file_llseek(file, offset, whence);
3283 inode = d_inode(cfile->dentry);
3284 cifsi = CIFS_I(inode);
3286 if (offset < 0 || offset >= i_size_read(inode))
3291 * We need to be sure that all dirty pages are written as they
3292 * might fill holes on the server.
3293 * Note that we also MUST flush any written pages since at least
3294 * some servers (Windows2016) will not reflect recent writes in
3295 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3297 wrcfile = find_writable_file(cifsi, false);
3299 filemap_write_and_wait(inode->i_mapping);
3300 smb2_flush_file(xid, tcon, &wrcfile->fid);
3301 cifsFileInfo_put(wrcfile);
3304 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3305 if (whence == SEEK_HOLE)
3306 offset = i_size_read(inode);
3310 in_data.file_offset = cpu_to_le64(offset);
3311 in_data.length = cpu_to_le64(i_size_read(inode));
3313 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3314 cfile->fid.volatile_fid,
3315 FSCTL_QUERY_ALLOCATED_RANGES, true,
3316 (char *)&in_data, sizeof(in_data),
3317 sizeof(struct file_allocated_range_buffer),
3318 (char **)&out_data, &out_data_len);
3324 if (whence == SEEK_HOLE && out_data_len == 0)
3327 if (whence == SEEK_DATA && out_data_len == 0) {
3332 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3336 if (whence == SEEK_DATA) {
3337 offset = le64_to_cpu(out_data->file_offset);
3340 if (offset < le64_to_cpu(out_data->file_offset))
3343 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3349 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3354 static int smb3_fiemap(struct cifs_tcon *tcon,
3355 struct cifsFileInfo *cfile,
3356 struct fiemap_extent_info *fei, u64 start, u64 len)
3359 struct file_allocated_range_buffer in_data, *out_data;
3361 int i, num, rc, flags, last_blob;
3364 if (fiemap_check_flags(fei, FIEMAP_FLAG_SYNC))
3369 in_data.file_offset = cpu_to_le64(start);
3370 in_data.length = cpu_to_le64(len);
3372 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3373 cfile->fid.volatile_fid,
3374 FSCTL_QUERY_ALLOCATED_RANGES, true,
3375 (char *)&in_data, sizeof(in_data),
3376 1024 * sizeof(struct file_allocated_range_buffer),
3377 (char **)&out_data, &out_data_len);
3386 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3390 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3395 num = out_data_len / sizeof(struct file_allocated_range_buffer);
3396 for (i = 0; i < num; i++) {
3398 if (i == num - 1 && last_blob)
3399 flags |= FIEMAP_EXTENT_LAST;
3401 rc = fiemap_fill_next_extent(fei,
3402 le64_to_cpu(out_data[i].file_offset),
3403 le64_to_cpu(out_data[i].file_offset),
3404 le64_to_cpu(out_data[i].length),
3415 next = le64_to_cpu(out_data[num - 1].file_offset) +
3416 le64_to_cpu(out_data[num - 1].length);
3417 len = len - (next - start);
3428 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3429 loff_t off, loff_t len)
3431 /* KEEP_SIZE already checked for by do_fallocate */
3432 if (mode & FALLOC_FL_PUNCH_HOLE)
3433 return smb3_punch_hole(file, tcon, off, len);
3434 else if (mode & FALLOC_FL_ZERO_RANGE) {
3435 if (mode & FALLOC_FL_KEEP_SIZE)
3436 return smb3_zero_range(file, tcon, off, len, true);
3437 return smb3_zero_range(file, tcon, off, len, false);
3438 } else if (mode == FALLOC_FL_KEEP_SIZE)
3439 return smb3_simple_falloc(file, tcon, off, len, true);
3441 return smb3_simple_falloc(file, tcon, off, len, false);
3447 smb2_downgrade_oplock(struct TCP_Server_Info *server,
3448 struct cifsInodeInfo *cinode, __u32 oplock,
3449 unsigned int epoch, bool *purge_cache)
3451 server->ops->set_oplock_level(cinode, oplock, 0, NULL);
3455 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3456 unsigned int epoch, bool *purge_cache);
3459 smb3_downgrade_oplock(struct TCP_Server_Info *server,
3460 struct cifsInodeInfo *cinode, __u32 oplock,
3461 unsigned int epoch, bool *purge_cache)
3463 unsigned int old_state = cinode->oplock;
3464 unsigned int old_epoch = cinode->epoch;
3465 unsigned int new_state;
3467 if (epoch > old_epoch) {
3468 smb21_set_oplock_level(cinode, oplock, 0, NULL);
3469 cinode->epoch = epoch;
3472 new_state = cinode->oplock;
3473 *purge_cache = false;
3475 if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
3476 (new_state & CIFS_CACHE_READ_FLG) == 0)
3477 *purge_cache = true;
3478 else if (old_state == new_state && (epoch - old_epoch > 1))
3479 *purge_cache = true;
3483 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3484 unsigned int epoch, bool *purge_cache)
3487 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3489 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3490 cinode->oplock = CIFS_CACHE_RHW_FLG;
3491 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3492 &cinode->vfs_inode);
3493 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
3494 cinode->oplock = CIFS_CACHE_RW_FLG;
3495 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3496 &cinode->vfs_inode);
3497 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3498 cinode->oplock = CIFS_CACHE_READ_FLG;
3499 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3500 &cinode->vfs_inode);
3506 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3507 unsigned int epoch, bool *purge_cache)
3509 char message[5] = {0};
3510 unsigned int new_oplock = 0;
3513 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3516 /* Check if the server granted an oplock rather than a lease */
3517 if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3518 return smb2_set_oplock_level(cinode, oplock, epoch,
3521 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
3522 new_oplock |= CIFS_CACHE_READ_FLG;
3523 strcat(message, "R");
3525 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
3526 new_oplock |= CIFS_CACHE_HANDLE_FLG;
3527 strcat(message, "H");
3529 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
3530 new_oplock |= CIFS_CACHE_WRITE_FLG;
3531 strcat(message, "W");
3534 strncpy(message, "None", sizeof(message));
3536 cinode->oplock = new_oplock;
3537 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3538 &cinode->vfs_inode);
3542 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3543 unsigned int epoch, bool *purge_cache)
3545 unsigned int old_oplock = cinode->oplock;
3547 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3550 *purge_cache = false;
3551 if (old_oplock == CIFS_CACHE_READ_FLG) {
3552 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3553 (epoch - cinode->epoch > 0))
3554 *purge_cache = true;
3555 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3556 (epoch - cinode->epoch > 1))
3557 *purge_cache = true;
3558 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3559 (epoch - cinode->epoch > 1))
3560 *purge_cache = true;
3561 else if (cinode->oplock == 0 &&
3562 (epoch - cinode->epoch > 0))
3563 *purge_cache = true;
3564 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
3565 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3566 (epoch - cinode->epoch > 0))
3567 *purge_cache = true;
3568 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3569 (epoch - cinode->epoch > 1))
3570 *purge_cache = true;
3572 cinode->epoch = epoch;
3577 smb2_is_read_op(__u32 oplock)
3579 return oplock == SMB2_OPLOCK_LEVEL_II;
3583 smb21_is_read_op(__u32 oplock)
3585 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
3586 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
3590 map_oplock_to_lease(u8 oplock)
3592 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3593 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
3594 else if (oplock == SMB2_OPLOCK_LEVEL_II)
3595 return SMB2_LEASE_READ_CACHING;
3596 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3597 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
3598 SMB2_LEASE_WRITE_CACHING;
3603 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3605 struct create_lease *buf;
3607 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
3611 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3612 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3614 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3615 (struct create_lease, lcontext));
3616 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
3617 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3618 (struct create_lease, Name));
3619 buf->ccontext.NameLength = cpu_to_le16(4);
3620 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3629 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
3631 struct create_lease_v2 *buf;
3633 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
3637 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3638 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3640 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3641 (struct create_lease_v2, lcontext));
3642 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
3643 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3644 (struct create_lease_v2, Name));
3645 buf->ccontext.NameLength = cpu_to_le16(4);
3646 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3655 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3657 struct create_lease *lc = (struct create_lease *)buf;
3659 *epoch = 0; /* not used */
3660 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3661 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3662 return le32_to_cpu(lc->lcontext.LeaseState);
3666 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3668 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
3670 *epoch = le16_to_cpu(lc->lcontext.Epoch);
3671 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3672 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3674 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
3675 return le32_to_cpu(lc->lcontext.LeaseState);
3679 smb2_wp_retry_size(struct inode *inode)
3681 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
3682 SMB2_MAX_BUFFER_SIZE);
3686 smb2_dir_needs_close(struct cifsFileInfo *cfile)
3688 return !cfile->invalidHandle;
3692 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
3693 struct smb_rqst *old_rq, __le16 cipher_type)
3695 struct smb2_sync_hdr *shdr =
3696 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
3698 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
3699 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
3700 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
3701 tr_hdr->Flags = cpu_to_le16(0x01);
3702 if (cipher_type == SMB2_ENCRYPTION_AES128_GCM)
3703 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128GCM_NONCE);
3705 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CCM_NONCE);
3706 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
3709 /* We can not use the normal sg_set_buf() as we will sometimes pass a
3710 * stack object as buf.
3712 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
3713 unsigned int buflen)
3717 * VMAP_STACK (at least) puts stack into the vmalloc address space
3719 if (is_vmalloc_addr(buf))
3720 addr = vmalloc_to_page(buf);
3722 addr = virt_to_page(buf);
3723 sg_set_page(sg, addr, buflen, offset_in_page(buf));
3726 /* Assumes the first rqst has a transform header as the first iov.
3728 * rqst[0].rq_iov[0] is transform header
3729 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
3730 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
3732 static struct scatterlist *
3733 init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
3735 unsigned int sg_len;
3736 struct scatterlist *sg;
3739 unsigned int idx = 0;
3743 for (i = 0; i < num_rqst; i++)
3744 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
3746 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
3750 sg_init_table(sg, sg_len);
3751 for (i = 0; i < num_rqst; i++) {
3752 for (j = 0; j < rqst[i].rq_nvec; j++) {
3754 * The first rqst has a transform header where the
3755 * first 20 bytes are not part of the encrypted blob
3757 skip = (i == 0) && (j == 0) ? 20 : 0;
3758 smb2_sg_set_buf(&sg[idx++],
3759 rqst[i].rq_iov[j].iov_base + skip,
3760 rqst[i].rq_iov[j].iov_len - skip);
3763 for (j = 0; j < rqst[i].rq_npages; j++) {
3764 unsigned int len, offset;
3766 rqst_page_get_length(&rqst[i], j, &len, &offset);
3767 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
3770 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
3775 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
3777 struct cifs_ses *ses;
3780 spin_lock(&cifs_tcp_ses_lock);
3781 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
3782 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3783 if (ses->Suid == ses_id) {
3784 ses_enc_key = enc ? ses->smb3encryptionkey :
3785 ses->smb3decryptionkey;
3786 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
3787 spin_unlock(&cifs_tcp_ses_lock);
3792 spin_unlock(&cifs_tcp_ses_lock);
3797 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
3798 * iov[0] - transform header (associate data),
3799 * iov[1-N] - SMB2 header and pages - data to encrypt.
3800 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
3804 crypt_message(struct TCP_Server_Info *server, int num_rqst,
3805 struct smb_rqst *rqst, int enc)
3807 struct smb2_transform_hdr *tr_hdr =
3808 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
3809 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
3811 struct scatterlist *sg;
3812 u8 sign[SMB2_SIGNATURE_SIZE] = {};
3813 u8 key[SMB3_SIGN_KEY_SIZE];
3814 struct aead_request *req;
3816 unsigned int iv_len;
3817 DECLARE_CRYPTO_WAIT(wait);
3818 struct crypto_aead *tfm;
3819 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3821 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
3823 cifs_server_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
3828 rc = smb3_crypto_aead_allocate(server);
3830 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
3834 tfm = enc ? server->secmech.ccmaesencrypt :
3835 server->secmech.ccmaesdecrypt;
3836 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
3838 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
3842 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
3844 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
3848 req = aead_request_alloc(tfm, GFP_KERNEL);
3850 cifs_server_dbg(VFS, "%s: Failed to alloc aead request\n", __func__);
3855 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
3856 crypt_len += SMB2_SIGNATURE_SIZE;
3859 sg = init_sg(num_rqst, rqst, sign);
3861 cifs_server_dbg(VFS, "%s: Failed to init sg\n", __func__);
3866 iv_len = crypto_aead_ivsize(tfm);
3867 iv = kzalloc(iv_len, GFP_KERNEL);
3869 cifs_server_dbg(VFS, "%s: Failed to alloc iv\n", __func__);
3874 if (server->cipher_type == SMB2_ENCRYPTION_AES128_GCM)
3875 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES128GCM_NONCE);
3878 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CCM_NONCE);
3881 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
3882 aead_request_set_ad(req, assoc_data_len);
3884 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3885 crypto_req_done, &wait);
3887 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
3888 : crypto_aead_decrypt(req), &wait);
3891 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
3902 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
3906 for (i = 0; i < num_rqst; i++) {
3907 if (rqst[i].rq_pages) {
3908 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
3909 put_page(rqst[i].rq_pages[j]);
3910 kfree(rqst[i].rq_pages);
3916 * This function will initialize new_rq and encrypt the content.
3917 * The first entry, new_rq[0], only contains a single iov which contains
3918 * a smb2_transform_hdr and is pre-allocated by the caller.
3919 * This function then populates new_rq[1+] with the content from olq_rq[0+].
3921 * The end result is an array of smb_rqst structures where the first structure
3922 * only contains a single iov for the transform header which we then can pass
3923 * to crypt_message().
3925 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
3926 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
3929 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
3930 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
3932 struct page **pages;
3933 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
3934 unsigned int npages;
3935 unsigned int orig_len = 0;
3939 for (i = 1; i < num_rqst; i++) {
3940 npages = old_rq[i - 1].rq_npages;
3941 pages = kmalloc_array(npages, sizeof(struct page *),
3946 new_rq[i].rq_pages = pages;
3947 new_rq[i].rq_npages = npages;
3948 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
3949 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
3950 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
3951 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
3952 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
3954 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
3956 for (j = 0; j < npages; j++) {
3957 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3962 /* copy pages form the old */
3963 for (j = 0; j < npages; j++) {
3965 unsigned int offset, len;
3967 rqst_page_get_length(&new_rq[i], j, &len, &offset);
3969 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
3970 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
3972 memcpy(dst, src, len);
3973 kunmap(new_rq[i].rq_pages[j]);
3974 kunmap(old_rq[i - 1].rq_pages[j]);
3978 /* fill the 1st iov with a transform header */
3979 fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
3981 rc = crypt_message(server, num_rqst, new_rq, 1);
3982 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
3989 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
3994 smb3_is_transform_hdr(void *buf)
3996 struct smb2_transform_hdr *trhdr = buf;
3998 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
4002 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
4003 unsigned int buf_data_size, struct page **pages,
4004 unsigned int npages, unsigned int page_data_size)
4007 struct smb_rqst rqst = {NULL};
4010 iov[0].iov_base = buf;
4011 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
4012 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
4013 iov[1].iov_len = buf_data_size;
4017 rqst.rq_pages = pages;
4018 rqst.rq_npages = npages;
4019 rqst.rq_pagesz = PAGE_SIZE;
4020 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
4022 rc = crypt_message(server, 1, &rqst, 0);
4023 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
4028 memmove(buf, iov[1].iov_base, buf_data_size);
4030 server->total_read = buf_data_size + page_data_size;
4036 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
4037 unsigned int npages, unsigned int len)
4042 for (i = 0; i < npages; i++) {
4043 struct page *page = pages[i];
4047 if (len >= PAGE_SIZE) {
4048 /* enough data to fill the page */
4052 zero_user(page, len, PAGE_SIZE - len);
4055 length = cifs_read_page_from_socket(server, page, 0, n);
4058 server->total_read += length;
4065 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
4066 unsigned int cur_off, struct bio_vec **page_vec)
4068 struct bio_vec *bvec;
4071 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
4075 for (i = 0; i < npages; i++) {
4076 bvec[i].bv_page = pages[i];
4077 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
4078 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
4079 data_size -= bvec[i].bv_len;
4082 if (data_size != 0) {
4083 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4093 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4094 char *buf, unsigned int buf_len, struct page **pages,
4095 unsigned int npages, unsigned int page_data_size)
4097 unsigned int data_offset;
4098 unsigned int data_len;
4099 unsigned int cur_off;
4100 unsigned int cur_page_idx;
4101 unsigned int pad_len;
4102 struct cifs_readdata *rdata = mid->callback_data;
4103 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
4104 struct bio_vec *bvec = NULL;
4105 struct iov_iter iter;
4108 bool use_rdma_mr = false;
4110 if (shdr->Command != SMB2_READ) {
4111 cifs_server_dbg(VFS, "only big read responses are supported\n");
4115 if (server->ops->is_session_expired &&
4116 server->ops->is_session_expired(buf)) {
4117 cifs_reconnect(server);
4118 wake_up(&server->response_q);
4122 if (server->ops->is_status_pending &&
4123 server->ops->is_status_pending(buf, server))
4126 /* set up first two iov to get credits */
4127 rdata->iov[0].iov_base = buf;
4128 rdata->iov[0].iov_len = 0;
4129 rdata->iov[1].iov_base = buf;
4130 rdata->iov[1].iov_len =
4131 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
4132 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4133 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4134 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4135 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4137 rdata->result = server->ops->map_error(buf, true);
4138 if (rdata->result != 0) {
4139 cifs_dbg(FYI, "%s: server returned error %d\n",
4140 __func__, rdata->result);
4141 /* normal error on read response */
4142 dequeue_mid(mid, false);
4146 data_offset = server->ops->read_data_offset(buf);
4147 #ifdef CONFIG_CIFS_SMB_DIRECT
4148 use_rdma_mr = rdata->mr;
4150 data_len = server->ops->read_data_length(buf, use_rdma_mr);
4152 if (data_offset < server->vals->read_rsp_size) {
4154 * win2k8 sometimes sends an offset of 0 when the read
4155 * is beyond the EOF. Treat it as if the data starts just after
4158 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4159 __func__, data_offset);
4160 data_offset = server->vals->read_rsp_size;
4161 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4162 /* data_offset is beyond the end of smallbuf */
4163 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4164 __func__, data_offset);
4165 rdata->result = -EIO;
4166 dequeue_mid(mid, rdata->result);
4170 pad_len = data_offset - server->vals->read_rsp_size;
4172 if (buf_len <= data_offset) {
4173 /* read response payload is in pages */
4174 cur_page_idx = pad_len / PAGE_SIZE;
4175 cur_off = pad_len % PAGE_SIZE;
4177 if (cur_page_idx != 0) {
4178 /* data offset is beyond the 1st page of response */
4179 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4180 __func__, data_offset);
4181 rdata->result = -EIO;
4182 dequeue_mid(mid, rdata->result);
4186 if (data_len > page_data_size - pad_len) {
4187 /* data_len is corrupt -- discard frame */
4188 rdata->result = -EIO;
4189 dequeue_mid(mid, rdata->result);
4193 rdata->result = init_read_bvec(pages, npages, page_data_size,
4195 if (rdata->result != 0) {
4196 dequeue_mid(mid, rdata->result);
4200 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
4201 } else if (buf_len >= data_offset + data_len) {
4202 /* read response payload is in buf */
4203 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
4204 iov.iov_base = buf + data_offset;
4205 iov.iov_len = data_len;
4206 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
4208 /* read response payload cannot be in both buf and pages */
4209 WARN_ONCE(1, "buf can not contain only a part of read data");
4210 rdata->result = -EIO;
4211 dequeue_mid(mid, rdata->result);
4215 length = rdata->copy_into_pages(server, rdata, &iter);
4222 dequeue_mid(mid, false);
4226 struct smb2_decrypt_work {
4227 struct work_struct decrypt;
4228 struct TCP_Server_Info *server;
4229 struct page **ppages;
4231 unsigned int npages;
4236 static void smb2_decrypt_offload(struct work_struct *work)
4238 struct smb2_decrypt_work *dw = container_of(work,
4239 struct smb2_decrypt_work, decrypt);
4241 struct mid_q_entry *mid;
4243 rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
4244 dw->ppages, dw->npages, dw->len);
4246 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4250 dw->server->lstrp = jiffies;
4251 mid = smb2_find_mid(dw->server, dw->buf);
4253 cifs_dbg(FYI, "mid not found\n");
4255 mid->decrypted = true;
4256 rc = handle_read_data(dw->server, mid, dw->buf,
4257 dw->server->vals->read_rsp_size,
4258 dw->ppages, dw->npages, dw->len);
4260 cifs_mid_q_entry_release(mid);
4264 for (i = dw->npages-1; i >= 0; i--)
4265 put_page(dw->ppages[i]);
4268 cifs_small_buf_release(dw->buf);
4274 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4277 char *buf = server->smallbuf;
4278 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4279 unsigned int npages;
4280 struct page **pages;
4282 unsigned int buflen = server->pdu_size;
4285 struct smb2_decrypt_work *dw;
4288 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
4289 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
4291 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
4294 server->total_read += rc;
4296 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
4297 server->vals->read_rsp_size;
4298 npages = DIV_ROUND_UP(len, PAGE_SIZE);
4300 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
4306 for (; i < npages; i++) {
4307 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4314 /* read read data into pages */
4315 rc = read_data_into_pages(server, pages, npages, len);
4319 rc = cifs_discard_remaining_data(server);
4324 * For large reads, offload to different thread for better performance,
4325 * use more cores decrypting which can be expensive
4328 if ((server->min_offload) && (server->in_flight > 1) &&
4329 (server->pdu_size >= server->min_offload)) {
4330 dw = kmalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
4332 goto non_offloaded_decrypt;
4334 dw->buf = server->smallbuf;
4335 server->smallbuf = (char *)cifs_small_buf_get();
4337 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
4339 dw->npages = npages;
4340 dw->server = server;
4343 queue_work(decrypt_wq, &dw->decrypt);
4344 *num_mids = 0; /* worker thread takes care of finding mid */
4348 non_offloaded_decrypt:
4349 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
4350 pages, npages, len);
4354 *mid = smb2_find_mid(server, buf);
4356 cifs_dbg(FYI, "mid not found\n");
4358 cifs_dbg(FYI, "mid found\n");
4359 (*mid)->decrypted = true;
4360 rc = handle_read_data(server, *mid, buf,
4361 server->vals->read_rsp_size,
4362 pages, npages, len);
4366 for (i = i - 1; i >= 0; i--)
4371 cifs_discard_remaining_data(server);
4376 receive_encrypted_standard(struct TCP_Server_Info *server,
4377 struct mid_q_entry **mids, char **bufs,
4381 char *buf = server->smallbuf;
4382 struct smb2_sync_hdr *shdr;
4383 unsigned int pdu_length = server->pdu_size;
4384 unsigned int buf_size;
4385 struct mid_q_entry *mid_entry;
4387 char *next_buffer = NULL;
4391 /* switch to large buffer if too big for a small one */
4392 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
4393 server->large_buf = true;
4394 memcpy(server->bigbuf, buf, server->total_read);
4395 buf = server->bigbuf;
4398 /* now read the rest */
4399 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
4400 pdu_length - HEADER_SIZE(server) + 1);
4403 server->total_read += length;
4405 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
4406 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
4410 next_is_large = server->large_buf;
4412 shdr = (struct smb2_sync_hdr *)buf;
4413 if (shdr->NextCommand) {
4415 next_buffer = (char *)cifs_buf_get();
4417 next_buffer = (char *)cifs_small_buf_get();
4419 buf + le32_to_cpu(shdr->NextCommand),
4420 pdu_length - le32_to_cpu(shdr->NextCommand));
4423 mid_entry = smb2_find_mid(server, buf);
4424 if (mid_entry == NULL)
4425 cifs_dbg(FYI, "mid not found\n");
4427 cifs_dbg(FYI, "mid found\n");
4428 mid_entry->decrypted = true;
4429 mid_entry->resp_buf_size = server->pdu_size;
4432 if (*num_mids >= MAX_COMPOUND) {
4433 cifs_server_dbg(VFS, "too many PDUs in compound\n");
4436 bufs[*num_mids] = buf;
4437 mids[(*num_mids)++] = mid_entry;
4439 if (mid_entry && mid_entry->handle)
4440 ret = mid_entry->handle(server, mid_entry);
4442 ret = cifs_handle_standard(server, mid_entry);
4444 if (ret == 0 && shdr->NextCommand) {
4445 pdu_length -= le32_to_cpu(shdr->NextCommand);
4446 server->large_buf = next_is_large;
4448 server->bigbuf = buf = next_buffer;
4450 server->smallbuf = buf = next_buffer;
4452 } else if (ret != 0) {
4454 * ret != 0 here means that we didn't get to handle_mid() thus
4455 * server->smallbuf and server->bigbuf are still valid. We need
4456 * to free next_buffer because it is not going to be used
4460 free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
4462 free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
4469 smb3_receive_transform(struct TCP_Server_Info *server,
4470 struct mid_q_entry **mids, char **bufs, int *num_mids)
4472 char *buf = server->smallbuf;
4473 unsigned int pdu_length = server->pdu_size;
4474 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4475 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4477 if (pdu_length < sizeof(struct smb2_transform_hdr) +
4478 sizeof(struct smb2_sync_hdr)) {
4479 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
4481 cifs_reconnect(server);
4482 wake_up(&server->response_q);
4483 return -ECONNABORTED;
4486 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
4487 cifs_server_dbg(VFS, "Transform message is broken\n");
4488 cifs_reconnect(server);
4489 wake_up(&server->response_q);
4490 return -ECONNABORTED;
4493 /* TODO: add support for compounds containing READ. */
4494 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
4495 return receive_encrypted_read(server, &mids[0], num_mids);
4498 return receive_encrypted_standard(server, mids, bufs, num_mids);
4502 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
4504 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
4506 return handle_read_data(server, mid, buf, server->pdu_size,
4511 smb2_next_header(char *buf)
4513 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
4514 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
4516 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
4517 return sizeof(struct smb2_transform_hdr) +
4518 le32_to_cpu(t_hdr->OriginalMessageSize);
4520 return le32_to_cpu(hdr->NextCommand);
4524 smb2_make_node(unsigned int xid, struct inode *inode,
4525 struct dentry *dentry, struct cifs_tcon *tcon,
4526 char *full_path, umode_t mode, dev_t dev)
4528 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4530 FILE_ALL_INFO *buf = NULL;
4531 struct cifs_io_parms io_parms;
4533 struct cifs_fid fid;
4534 struct cifs_open_parms oparms;
4535 unsigned int bytes_written;
4536 struct win_dev *pdev;
4540 * Check if mounted with mount parm 'sfu' mount parm.
4541 * SFU emulation should work with all servers, but only
4542 * supports block and char device (no socket & fifo),
4543 * and was used by default in earlier versions of Windows
4545 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
4549 * TODO: Add ability to create instead via reparse point. Windows (e.g.
4550 * their current NFS server) uses this approach to expose special files
4551 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
4554 if (!S_ISCHR(mode) && !S_ISBLK(mode))
4557 cifs_dbg(FYI, "sfu compat create special file\n");
4559 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
4566 oparms.cifs_sb = cifs_sb;
4567 oparms.desired_access = GENERIC_WRITE;
4568 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
4569 CREATE_OPTION_SPECIAL);
4570 oparms.disposition = FILE_CREATE;
4571 oparms.path = full_path;
4573 oparms.reconnect = false;
4575 if (tcon->ses->server->oplocks)
4576 oplock = REQ_OPLOCK;
4579 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
4584 * BB Do not bother to decode buf since no local inode yet to put
4585 * timestamps in, but we can reuse it safely.
4588 pdev = (struct win_dev *)buf;
4589 io_parms.pid = current->tgid;
4590 io_parms.tcon = tcon;
4591 io_parms.offset = 0;
4592 io_parms.length = sizeof(struct win_dev);
4593 iov[1].iov_base = buf;
4594 iov[1].iov_len = sizeof(struct win_dev);
4595 if (S_ISCHR(mode)) {
4596 memcpy(pdev->type, "IntxCHR", 8);
4597 pdev->major = cpu_to_le64(MAJOR(dev));
4598 pdev->minor = cpu_to_le64(MINOR(dev));
4599 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4600 &bytes_written, iov, 1);
4601 } else if (S_ISBLK(mode)) {
4602 memcpy(pdev->type, "IntxBLK", 8);
4603 pdev->major = cpu_to_le64(MAJOR(dev));
4604 pdev->minor = cpu_to_le64(MINOR(dev));
4605 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4606 &bytes_written, iov, 1);
4608 tcon->ses->server->ops->close(xid, tcon, &fid);
4611 /* FIXME: add code here to set EAs */
4618 struct smb_version_operations smb20_operations = {
4619 .compare_fids = smb2_compare_fids,
4620 .setup_request = smb2_setup_request,
4621 .setup_async_request = smb2_setup_async_request,
4622 .check_receive = smb2_check_receive,
4623 .add_credits = smb2_add_credits,
4624 .set_credits = smb2_set_credits,
4625 .get_credits_field = smb2_get_credits_field,
4626 .get_credits = smb2_get_credits,
4627 .wait_mtu_credits = cifs_wait_mtu_credits,
4628 .get_next_mid = smb2_get_next_mid,
4629 .revert_current_mid = smb2_revert_current_mid,
4630 .read_data_offset = smb2_read_data_offset,
4631 .read_data_length = smb2_read_data_length,
4632 .map_error = map_smb2_to_linux_error,
4633 .find_mid = smb2_find_mid,
4634 .check_message = smb2_check_message,
4635 .dump_detail = smb2_dump_detail,
4636 .clear_stats = smb2_clear_stats,
4637 .print_stats = smb2_print_stats,
4638 .is_oplock_break = smb2_is_valid_oplock_break,
4639 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4640 .downgrade_oplock = smb2_downgrade_oplock,
4641 .need_neg = smb2_need_neg,
4642 .negotiate = smb2_negotiate,
4643 .negotiate_wsize = smb2_negotiate_wsize,
4644 .negotiate_rsize = smb2_negotiate_rsize,
4645 .sess_setup = SMB2_sess_setup,
4646 .logoff = SMB2_logoff,
4647 .tree_connect = SMB2_tcon,
4648 .tree_disconnect = SMB2_tdis,
4649 .qfs_tcon = smb2_qfs_tcon,
4650 .is_path_accessible = smb2_is_path_accessible,
4651 .can_echo = smb2_can_echo,
4653 .query_path_info = smb2_query_path_info,
4654 .get_srv_inum = smb2_get_srv_inum,
4655 .query_file_info = smb2_query_file_info,
4656 .set_path_size = smb2_set_path_size,
4657 .set_file_size = smb2_set_file_size,
4658 .set_file_info = smb2_set_file_info,
4659 .set_compression = smb2_set_compression,
4660 .mkdir = smb2_mkdir,
4661 .mkdir_setinfo = smb2_mkdir_setinfo,
4662 .rmdir = smb2_rmdir,
4663 .unlink = smb2_unlink,
4664 .rename = smb2_rename_path,
4665 .create_hardlink = smb2_create_hardlink,
4666 .query_symlink = smb2_query_symlink,
4667 .query_mf_symlink = smb3_query_mf_symlink,
4668 .create_mf_symlink = smb3_create_mf_symlink,
4669 .open = smb2_open_file,
4670 .set_fid = smb2_set_fid,
4671 .close = smb2_close_file,
4672 .flush = smb2_flush_file,
4673 .async_readv = smb2_async_readv,
4674 .async_writev = smb2_async_writev,
4675 .sync_read = smb2_sync_read,
4676 .sync_write = smb2_sync_write,
4677 .query_dir_first = smb2_query_dir_first,
4678 .query_dir_next = smb2_query_dir_next,
4679 .close_dir = smb2_close_dir,
4680 .calc_smb_size = smb2_calc_size,
4681 .is_status_pending = smb2_is_status_pending,
4682 .is_session_expired = smb2_is_session_expired,
4683 .oplock_response = smb2_oplock_response,
4684 .queryfs = smb2_queryfs,
4685 .mand_lock = smb2_mand_lock,
4686 .mand_unlock_range = smb2_unlock_range,
4687 .push_mand_locks = smb2_push_mandatory_locks,
4688 .get_lease_key = smb2_get_lease_key,
4689 .set_lease_key = smb2_set_lease_key,
4690 .new_lease_key = smb2_new_lease_key,
4691 .calc_signature = smb2_calc_signature,
4692 .is_read_op = smb2_is_read_op,
4693 .set_oplock_level = smb2_set_oplock_level,
4694 .create_lease_buf = smb2_create_lease_buf,
4695 .parse_lease_buf = smb2_parse_lease_buf,
4696 .copychunk_range = smb2_copychunk_range,
4697 .wp_retry_size = smb2_wp_retry_size,
4698 .dir_needs_close = smb2_dir_needs_close,
4699 .get_dfs_refer = smb2_get_dfs_refer,
4700 .select_sectype = smb2_select_sectype,
4701 #ifdef CONFIG_CIFS_XATTR
4702 .query_all_EAs = smb2_query_eas,
4703 .set_EA = smb2_set_ea,
4704 #endif /* CIFS_XATTR */
4705 .get_acl = get_smb2_acl,
4706 .get_acl_by_fid = get_smb2_acl_by_fid,
4707 .set_acl = set_smb2_acl,
4708 .next_header = smb2_next_header,
4709 .ioctl_query_info = smb2_ioctl_query_info,
4710 .make_node = smb2_make_node,
4711 .fiemap = smb3_fiemap,
4712 .llseek = smb3_llseek,
4715 struct smb_version_operations smb21_operations = {
4716 .compare_fids = smb2_compare_fids,
4717 .setup_request = smb2_setup_request,
4718 .setup_async_request = smb2_setup_async_request,
4719 .check_receive = smb2_check_receive,
4720 .add_credits = smb2_add_credits,
4721 .set_credits = smb2_set_credits,
4722 .get_credits_field = smb2_get_credits_field,
4723 .get_credits = smb2_get_credits,
4724 .wait_mtu_credits = smb2_wait_mtu_credits,
4725 .adjust_credits = smb2_adjust_credits,
4726 .get_next_mid = smb2_get_next_mid,
4727 .revert_current_mid = smb2_revert_current_mid,
4728 .read_data_offset = smb2_read_data_offset,
4729 .read_data_length = smb2_read_data_length,
4730 .map_error = map_smb2_to_linux_error,
4731 .find_mid = smb2_find_mid,
4732 .check_message = smb2_check_message,
4733 .dump_detail = smb2_dump_detail,
4734 .clear_stats = smb2_clear_stats,
4735 .print_stats = smb2_print_stats,
4736 .is_oplock_break = smb2_is_valid_oplock_break,
4737 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4738 .downgrade_oplock = smb2_downgrade_oplock,
4739 .need_neg = smb2_need_neg,
4740 .negotiate = smb2_negotiate,
4741 .negotiate_wsize = smb2_negotiate_wsize,
4742 .negotiate_rsize = smb2_negotiate_rsize,
4743 .sess_setup = SMB2_sess_setup,
4744 .logoff = SMB2_logoff,
4745 .tree_connect = SMB2_tcon,
4746 .tree_disconnect = SMB2_tdis,
4747 .qfs_tcon = smb2_qfs_tcon,
4748 .is_path_accessible = smb2_is_path_accessible,
4749 .can_echo = smb2_can_echo,
4751 .query_path_info = smb2_query_path_info,
4752 .get_srv_inum = smb2_get_srv_inum,
4753 .query_file_info = smb2_query_file_info,
4754 .set_path_size = smb2_set_path_size,
4755 .set_file_size = smb2_set_file_size,
4756 .set_file_info = smb2_set_file_info,
4757 .set_compression = smb2_set_compression,
4758 .mkdir = smb2_mkdir,
4759 .mkdir_setinfo = smb2_mkdir_setinfo,
4760 .rmdir = smb2_rmdir,
4761 .unlink = smb2_unlink,
4762 .rename = smb2_rename_path,
4763 .create_hardlink = smb2_create_hardlink,
4764 .query_symlink = smb2_query_symlink,
4765 .query_mf_symlink = smb3_query_mf_symlink,
4766 .create_mf_symlink = smb3_create_mf_symlink,
4767 .open = smb2_open_file,
4768 .set_fid = smb2_set_fid,
4769 .close = smb2_close_file,
4770 .flush = smb2_flush_file,
4771 .async_readv = smb2_async_readv,
4772 .async_writev = smb2_async_writev,
4773 .sync_read = smb2_sync_read,
4774 .sync_write = smb2_sync_write,
4775 .query_dir_first = smb2_query_dir_first,
4776 .query_dir_next = smb2_query_dir_next,
4777 .close_dir = smb2_close_dir,
4778 .calc_smb_size = smb2_calc_size,
4779 .is_status_pending = smb2_is_status_pending,
4780 .is_session_expired = smb2_is_session_expired,
4781 .oplock_response = smb2_oplock_response,
4782 .queryfs = smb2_queryfs,
4783 .mand_lock = smb2_mand_lock,
4784 .mand_unlock_range = smb2_unlock_range,
4785 .push_mand_locks = smb2_push_mandatory_locks,
4786 .get_lease_key = smb2_get_lease_key,
4787 .set_lease_key = smb2_set_lease_key,
4788 .new_lease_key = smb2_new_lease_key,
4789 .calc_signature = smb2_calc_signature,
4790 .is_read_op = smb21_is_read_op,
4791 .set_oplock_level = smb21_set_oplock_level,
4792 .create_lease_buf = smb2_create_lease_buf,
4793 .parse_lease_buf = smb2_parse_lease_buf,
4794 .copychunk_range = smb2_copychunk_range,
4795 .wp_retry_size = smb2_wp_retry_size,
4796 .dir_needs_close = smb2_dir_needs_close,
4797 .enum_snapshots = smb3_enum_snapshots,
4798 .get_dfs_refer = smb2_get_dfs_refer,
4799 .select_sectype = smb2_select_sectype,
4800 #ifdef CONFIG_CIFS_XATTR
4801 .query_all_EAs = smb2_query_eas,
4802 .set_EA = smb2_set_ea,
4803 #endif /* CIFS_XATTR */
4804 .get_acl = get_smb2_acl,
4805 .get_acl_by_fid = get_smb2_acl_by_fid,
4806 .set_acl = set_smb2_acl,
4807 .next_header = smb2_next_header,
4808 .ioctl_query_info = smb2_ioctl_query_info,
4809 .make_node = smb2_make_node,
4810 .fiemap = smb3_fiemap,
4811 .llseek = smb3_llseek,
4814 struct smb_version_operations smb30_operations = {
4815 .compare_fids = smb2_compare_fids,
4816 .setup_request = smb2_setup_request,
4817 .setup_async_request = smb2_setup_async_request,
4818 .check_receive = smb2_check_receive,
4819 .add_credits = smb2_add_credits,
4820 .set_credits = smb2_set_credits,
4821 .get_credits_field = smb2_get_credits_field,
4822 .get_credits = smb2_get_credits,
4823 .wait_mtu_credits = smb2_wait_mtu_credits,
4824 .adjust_credits = smb2_adjust_credits,
4825 .get_next_mid = smb2_get_next_mid,
4826 .revert_current_mid = smb2_revert_current_mid,
4827 .read_data_offset = smb2_read_data_offset,
4828 .read_data_length = smb2_read_data_length,
4829 .map_error = map_smb2_to_linux_error,
4830 .find_mid = smb2_find_mid,
4831 .check_message = smb2_check_message,
4832 .dump_detail = smb2_dump_detail,
4833 .clear_stats = smb2_clear_stats,
4834 .print_stats = smb2_print_stats,
4835 .dump_share_caps = smb2_dump_share_caps,
4836 .is_oplock_break = smb2_is_valid_oplock_break,
4837 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4838 .downgrade_oplock = smb3_downgrade_oplock,
4839 .need_neg = smb2_need_neg,
4840 .negotiate = smb2_negotiate,
4841 .negotiate_wsize = smb3_negotiate_wsize,
4842 .negotiate_rsize = smb3_negotiate_rsize,
4843 .sess_setup = SMB2_sess_setup,
4844 .logoff = SMB2_logoff,
4845 .tree_connect = SMB2_tcon,
4846 .tree_disconnect = SMB2_tdis,
4847 .qfs_tcon = smb3_qfs_tcon,
4848 .is_path_accessible = smb2_is_path_accessible,
4849 .can_echo = smb2_can_echo,
4851 .query_path_info = smb2_query_path_info,
4852 .get_srv_inum = smb2_get_srv_inum,
4853 .query_file_info = smb2_query_file_info,
4854 .set_path_size = smb2_set_path_size,
4855 .set_file_size = smb2_set_file_size,
4856 .set_file_info = smb2_set_file_info,
4857 .set_compression = smb2_set_compression,
4858 .mkdir = smb2_mkdir,
4859 .mkdir_setinfo = smb2_mkdir_setinfo,
4860 .rmdir = smb2_rmdir,
4861 .unlink = smb2_unlink,
4862 .rename = smb2_rename_path,
4863 .create_hardlink = smb2_create_hardlink,
4864 .query_symlink = smb2_query_symlink,
4865 .query_mf_symlink = smb3_query_mf_symlink,
4866 .create_mf_symlink = smb3_create_mf_symlink,
4867 .open = smb2_open_file,
4868 .set_fid = smb2_set_fid,
4869 .close = smb2_close_file,
4870 .close_getattr = smb2_close_getattr,
4871 .flush = smb2_flush_file,
4872 .async_readv = smb2_async_readv,
4873 .async_writev = smb2_async_writev,
4874 .sync_read = smb2_sync_read,
4875 .sync_write = smb2_sync_write,
4876 .query_dir_first = smb2_query_dir_first,
4877 .query_dir_next = smb2_query_dir_next,
4878 .close_dir = smb2_close_dir,
4879 .calc_smb_size = smb2_calc_size,
4880 .is_status_pending = smb2_is_status_pending,
4881 .is_session_expired = smb2_is_session_expired,
4882 .oplock_response = smb2_oplock_response,
4883 .queryfs = smb2_queryfs,
4884 .mand_lock = smb2_mand_lock,
4885 .mand_unlock_range = smb2_unlock_range,
4886 .push_mand_locks = smb2_push_mandatory_locks,
4887 .get_lease_key = smb2_get_lease_key,
4888 .set_lease_key = smb2_set_lease_key,
4889 .new_lease_key = smb2_new_lease_key,
4890 .generate_signingkey = generate_smb30signingkey,
4891 .calc_signature = smb3_calc_signature,
4892 .set_integrity = smb3_set_integrity,
4893 .is_read_op = smb21_is_read_op,
4894 .set_oplock_level = smb3_set_oplock_level,
4895 .create_lease_buf = smb3_create_lease_buf,
4896 .parse_lease_buf = smb3_parse_lease_buf,
4897 .copychunk_range = smb2_copychunk_range,
4898 .duplicate_extents = smb2_duplicate_extents,
4899 .validate_negotiate = smb3_validate_negotiate,
4900 .wp_retry_size = smb2_wp_retry_size,
4901 .dir_needs_close = smb2_dir_needs_close,
4902 .fallocate = smb3_fallocate,
4903 .enum_snapshots = smb3_enum_snapshots,
4904 .notify = smb3_notify,
4905 .init_transform_rq = smb3_init_transform_rq,
4906 .is_transform_hdr = smb3_is_transform_hdr,
4907 .receive_transform = smb3_receive_transform,
4908 .get_dfs_refer = smb2_get_dfs_refer,
4909 .select_sectype = smb2_select_sectype,
4910 #ifdef CONFIG_CIFS_XATTR
4911 .query_all_EAs = smb2_query_eas,
4912 .set_EA = smb2_set_ea,
4913 #endif /* CIFS_XATTR */
4914 .get_acl = get_smb2_acl,
4915 .get_acl_by_fid = get_smb2_acl_by_fid,
4916 .set_acl = set_smb2_acl,
4917 .next_header = smb2_next_header,
4918 .ioctl_query_info = smb2_ioctl_query_info,
4919 .make_node = smb2_make_node,
4920 .fiemap = smb3_fiemap,
4921 .llseek = smb3_llseek,
4924 struct smb_version_operations smb311_operations = {
4925 .compare_fids = smb2_compare_fids,
4926 .setup_request = smb2_setup_request,
4927 .setup_async_request = smb2_setup_async_request,
4928 .check_receive = smb2_check_receive,
4929 .add_credits = smb2_add_credits,
4930 .set_credits = smb2_set_credits,
4931 .get_credits_field = smb2_get_credits_field,
4932 .get_credits = smb2_get_credits,
4933 .wait_mtu_credits = smb2_wait_mtu_credits,
4934 .adjust_credits = smb2_adjust_credits,
4935 .get_next_mid = smb2_get_next_mid,
4936 .revert_current_mid = smb2_revert_current_mid,
4937 .read_data_offset = smb2_read_data_offset,
4938 .read_data_length = smb2_read_data_length,
4939 .map_error = map_smb2_to_linux_error,
4940 .find_mid = smb2_find_mid,
4941 .check_message = smb2_check_message,
4942 .dump_detail = smb2_dump_detail,
4943 .clear_stats = smb2_clear_stats,
4944 .print_stats = smb2_print_stats,
4945 .dump_share_caps = smb2_dump_share_caps,
4946 .is_oplock_break = smb2_is_valid_oplock_break,
4947 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4948 .downgrade_oplock = smb3_downgrade_oplock,
4949 .need_neg = smb2_need_neg,
4950 .negotiate = smb2_negotiate,
4951 .negotiate_wsize = smb3_negotiate_wsize,
4952 .negotiate_rsize = smb3_negotiate_rsize,
4953 .sess_setup = SMB2_sess_setup,
4954 .logoff = SMB2_logoff,
4955 .tree_connect = SMB2_tcon,
4956 .tree_disconnect = SMB2_tdis,
4957 .qfs_tcon = smb3_qfs_tcon,
4958 .is_path_accessible = smb2_is_path_accessible,
4959 .can_echo = smb2_can_echo,
4961 .query_path_info = smb2_query_path_info,
4962 .get_srv_inum = smb2_get_srv_inum,
4963 .query_file_info = smb2_query_file_info,
4964 .set_path_size = smb2_set_path_size,
4965 .set_file_size = smb2_set_file_size,
4966 .set_file_info = smb2_set_file_info,
4967 .set_compression = smb2_set_compression,
4968 .mkdir = smb2_mkdir,
4969 .mkdir_setinfo = smb2_mkdir_setinfo,
4970 .posix_mkdir = smb311_posix_mkdir,
4971 .rmdir = smb2_rmdir,
4972 .unlink = smb2_unlink,
4973 .rename = smb2_rename_path,
4974 .create_hardlink = smb2_create_hardlink,
4975 .query_symlink = smb2_query_symlink,
4976 .query_mf_symlink = smb3_query_mf_symlink,
4977 .create_mf_symlink = smb3_create_mf_symlink,
4978 .open = smb2_open_file,
4979 .set_fid = smb2_set_fid,
4980 .close = smb2_close_file,
4981 .close_getattr = smb2_close_getattr,
4982 .flush = smb2_flush_file,
4983 .async_readv = smb2_async_readv,
4984 .async_writev = smb2_async_writev,
4985 .sync_read = smb2_sync_read,
4986 .sync_write = smb2_sync_write,
4987 .query_dir_first = smb2_query_dir_first,
4988 .query_dir_next = smb2_query_dir_next,
4989 .close_dir = smb2_close_dir,
4990 .calc_smb_size = smb2_calc_size,
4991 .is_status_pending = smb2_is_status_pending,
4992 .is_session_expired = smb2_is_session_expired,
4993 .oplock_response = smb2_oplock_response,
4994 .queryfs = smb311_queryfs,
4995 .mand_lock = smb2_mand_lock,
4996 .mand_unlock_range = smb2_unlock_range,
4997 .push_mand_locks = smb2_push_mandatory_locks,
4998 .get_lease_key = smb2_get_lease_key,
4999 .set_lease_key = smb2_set_lease_key,
5000 .new_lease_key = smb2_new_lease_key,
5001 .generate_signingkey = generate_smb311signingkey,
5002 .calc_signature = smb3_calc_signature,
5003 .set_integrity = smb3_set_integrity,
5004 .is_read_op = smb21_is_read_op,
5005 .set_oplock_level = smb3_set_oplock_level,
5006 .create_lease_buf = smb3_create_lease_buf,
5007 .parse_lease_buf = smb3_parse_lease_buf,
5008 .copychunk_range = smb2_copychunk_range,
5009 .duplicate_extents = smb2_duplicate_extents,
5010 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5011 .wp_retry_size = smb2_wp_retry_size,
5012 .dir_needs_close = smb2_dir_needs_close,
5013 .fallocate = smb3_fallocate,
5014 .enum_snapshots = smb3_enum_snapshots,
5015 .notify = smb3_notify,
5016 .init_transform_rq = smb3_init_transform_rq,
5017 .is_transform_hdr = smb3_is_transform_hdr,
5018 .receive_transform = smb3_receive_transform,
5019 .get_dfs_refer = smb2_get_dfs_refer,
5020 .select_sectype = smb2_select_sectype,
5021 #ifdef CONFIG_CIFS_XATTR
5022 .query_all_EAs = smb2_query_eas,
5023 .set_EA = smb2_set_ea,
5024 #endif /* CIFS_XATTR */
5025 .get_acl = get_smb2_acl,
5026 .get_acl_by_fid = get_smb2_acl_by_fid,
5027 .set_acl = set_smb2_acl,
5028 .next_header = smb2_next_header,
5029 .ioctl_query_info = smb2_ioctl_query_info,
5030 .make_node = smb2_make_node,
5031 .fiemap = smb3_fiemap,
5032 .llseek = smb3_llseek,
5035 struct smb_version_values smb20_values = {
5036 .version_string = SMB20_VERSION_STRING,
5037 .protocol_id = SMB20_PROT_ID,
5038 .req_capabilities = 0, /* MBZ */
5039 .large_lock_type = 0,
5040 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5041 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5042 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5043 .header_size = sizeof(struct smb2_sync_hdr),
5044 .header_preamble_size = 0,
5045 .max_header_size = MAX_SMB2_HDR_SIZE,
5046 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5047 .lock_cmd = SMB2_LOCK,
5049 .cap_nt_find = SMB2_NT_FIND,
5050 .cap_large_files = SMB2_LARGE_FILES,
5051 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5052 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5053 .create_lease_size = sizeof(struct create_lease),
5056 struct smb_version_values smb21_values = {
5057 .version_string = SMB21_VERSION_STRING,
5058 .protocol_id = SMB21_PROT_ID,
5059 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5060 .large_lock_type = 0,
5061 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5062 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5063 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5064 .header_size = sizeof(struct smb2_sync_hdr),
5065 .header_preamble_size = 0,
5066 .max_header_size = MAX_SMB2_HDR_SIZE,
5067 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5068 .lock_cmd = SMB2_LOCK,
5070 .cap_nt_find = SMB2_NT_FIND,
5071 .cap_large_files = SMB2_LARGE_FILES,
5072 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5073 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5074 .create_lease_size = sizeof(struct create_lease),
5077 struct smb_version_values smb3any_values = {
5078 .version_string = SMB3ANY_VERSION_STRING,
5079 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5080 .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,
5081 .large_lock_type = 0,
5082 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5083 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5084 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5085 .header_size = sizeof(struct smb2_sync_hdr),
5086 .header_preamble_size = 0,
5087 .max_header_size = MAX_SMB2_HDR_SIZE,
5088 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5089 .lock_cmd = SMB2_LOCK,
5091 .cap_nt_find = SMB2_NT_FIND,
5092 .cap_large_files = SMB2_LARGE_FILES,
5093 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5094 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5095 .create_lease_size = sizeof(struct create_lease_v2),
5098 struct smb_version_values smbdefault_values = {
5099 .version_string = SMBDEFAULT_VERSION_STRING,
5100 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5101 .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,
5102 .large_lock_type = 0,
5103 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5104 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5105 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5106 .header_size = sizeof(struct smb2_sync_hdr),
5107 .header_preamble_size = 0,
5108 .max_header_size = MAX_SMB2_HDR_SIZE,
5109 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5110 .lock_cmd = SMB2_LOCK,
5112 .cap_nt_find = SMB2_NT_FIND,
5113 .cap_large_files = SMB2_LARGE_FILES,
5114 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5115 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5116 .create_lease_size = sizeof(struct create_lease_v2),
5119 struct smb_version_values smb30_values = {
5120 .version_string = SMB30_VERSION_STRING,
5121 .protocol_id = SMB30_PROT_ID,
5122 .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,
5123 .large_lock_type = 0,
5124 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5125 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5126 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5127 .header_size = sizeof(struct smb2_sync_hdr),
5128 .header_preamble_size = 0,
5129 .max_header_size = MAX_SMB2_HDR_SIZE,
5130 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5131 .lock_cmd = SMB2_LOCK,
5133 .cap_nt_find = SMB2_NT_FIND,
5134 .cap_large_files = SMB2_LARGE_FILES,
5135 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5136 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5137 .create_lease_size = sizeof(struct create_lease_v2),
5140 struct smb_version_values smb302_values = {
5141 .version_string = SMB302_VERSION_STRING,
5142 .protocol_id = SMB302_PROT_ID,
5143 .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,
5144 .large_lock_type = 0,
5145 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5146 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5147 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5148 .header_size = sizeof(struct smb2_sync_hdr),
5149 .header_preamble_size = 0,
5150 .max_header_size = MAX_SMB2_HDR_SIZE,
5151 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5152 .lock_cmd = SMB2_LOCK,
5154 .cap_nt_find = SMB2_NT_FIND,
5155 .cap_large_files = SMB2_LARGE_FILES,
5156 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5157 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5158 .create_lease_size = sizeof(struct create_lease_v2),
5161 struct smb_version_values smb311_values = {
5162 .version_string = SMB311_VERSION_STRING,
5163 .protocol_id = SMB311_PROT_ID,
5164 .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,
5165 .large_lock_type = 0,
5166 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5167 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5168 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5169 .header_size = sizeof(struct smb2_sync_hdr),
5170 .header_preamble_size = 0,
5171 .max_header_size = MAX_SMB2_HDR_SIZE,
5172 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5173 .lock_cmd = SMB2_LOCK,
5175 .cap_nt_find = SMB2_NT_FIND,
5176 .cap_large_files = SMB2_LARGE_FILES,
5177 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5178 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5179 .create_lease_size = sizeof(struct create_lease_v2),