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>
17 #include "smb2proto.h"
18 #include "cifsproto.h"
19 #include "cifs_debug.h"
20 #include "cifs_unicode.h"
21 #include "smb2status.h"
23 #include "cifs_ioctl.h"
24 #include "smbdirect.h"
26 /* Change credits for different ops and return the total number of credits */
28 change_conf(struct TCP_Server_Info *server)
30 server->credits += server->echo_credits + server->oplock_credits;
31 server->oplock_credits = server->echo_credits = 0;
32 switch (server->credits) {
36 server->echoes = false;
37 server->oplocks = false;
40 server->echoes = true;
41 server->oplocks = false;
42 server->echo_credits = 1;
45 server->echoes = true;
47 server->oplocks = true;
48 server->oplock_credits = 1;
50 server->oplocks = false;
52 server->echo_credits = 1;
54 server->credits -= server->echo_credits + server->oplock_credits;
55 return server->credits + server->echo_credits + server->oplock_credits;
59 smb2_add_credits(struct TCP_Server_Info *server,
60 const struct cifs_credits *credits, const int optype)
63 unsigned int add = credits->value;
64 unsigned int instance = credits->instance;
65 bool reconnect_detected = false;
67 spin_lock(&server->req_lock);
68 val = server->ops->get_credits_field(server, optype);
70 /* eg found case where write overlapping reconnect messed up credits */
71 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
72 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
73 server->hostname, *val);
74 if ((instance == 0) || (instance == server->reconnect_instance))
77 reconnect_detected = true;
80 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
81 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
84 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
85 rc = change_conf(server);
87 * Sometimes server returns 0 credits on oplock break ack - we need to
88 * rebalance credits in this case.
90 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
92 if (server->credits > 1) {
94 server->oplock_credits++;
97 spin_unlock(&server->req_lock);
98 wake_up(&server->request_q);
100 if (reconnect_detected)
101 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
104 if (server->tcpStatus == CifsNeedReconnect
105 || server->tcpStatus == CifsExiting)
110 /* change_conf hasn't been executed */
113 cifs_server_dbg(VFS, "Possible client or server bug - zero credits\n");
116 cifs_server_dbg(VFS, "disabling echoes and oplocks\n");
119 cifs_dbg(FYI, "disabling oplocks\n");
122 cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
127 smb2_set_credits(struct TCP_Server_Info *server, const int val)
129 spin_lock(&server->req_lock);
130 server->credits = val;
132 server->reconnect_instance++;
133 spin_unlock(&server->req_lock);
134 /* don't log while holding the lock */
136 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
140 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
144 return &server->echo_credits;
146 return &server->oplock_credits;
148 return &server->credits;
153 smb2_get_credits(struct mid_q_entry *mid)
155 return mid->credits_received;
159 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
160 unsigned int *num, struct cifs_credits *credits)
163 unsigned int scredits;
165 spin_lock(&server->req_lock);
167 if (server->credits <= 0) {
168 spin_unlock(&server->req_lock);
169 cifs_num_waiters_inc(server);
170 rc = wait_event_killable(server->request_q,
171 has_credits(server, &server->credits, 1));
172 cifs_num_waiters_dec(server);
175 spin_lock(&server->req_lock);
177 if (server->tcpStatus == CifsExiting) {
178 spin_unlock(&server->req_lock);
182 scredits = server->credits;
183 /* can deadlock with reopen */
185 *num = SMB2_MAX_BUFFER_SIZE;
187 credits->instance = 0;
191 /* leave some credits for reopen and other ops */
193 *num = min_t(unsigned int, size,
194 scredits * SMB2_MAX_BUFFER_SIZE);
197 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
198 credits->instance = server->reconnect_instance;
199 server->credits -= credits->value;
201 if (server->in_flight > server->max_in_flight)
202 server->max_in_flight = server->in_flight;
206 spin_unlock(&server->req_lock);
211 smb2_adjust_credits(struct TCP_Server_Info *server,
212 struct cifs_credits *credits,
213 const unsigned int payload_size)
215 int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
217 if (!credits->value || credits->value == new_val)
220 if (credits->value < new_val) {
221 WARN_ONCE(1, "request has less credits (%d) than required (%d)",
222 credits->value, new_val);
226 spin_lock(&server->req_lock);
228 if (server->reconnect_instance != credits->instance) {
229 spin_unlock(&server->req_lock);
230 cifs_server_dbg(VFS, "trying to return %d credits to old session\n",
231 credits->value - new_val);
235 server->credits += credits->value - new_val;
236 spin_unlock(&server->req_lock);
237 wake_up(&server->request_q);
238 credits->value = new_val;
243 smb2_get_next_mid(struct TCP_Server_Info *server)
246 /* for SMB2 we need the current value */
247 spin_lock(&GlobalMid_Lock);
248 mid = server->CurrentMid++;
249 spin_unlock(&GlobalMid_Lock);
254 smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
256 spin_lock(&GlobalMid_Lock);
257 if (server->CurrentMid >= val)
258 server->CurrentMid -= val;
259 spin_unlock(&GlobalMid_Lock);
262 static struct mid_q_entry *
263 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
265 struct mid_q_entry *mid;
266 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
267 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
269 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
270 cifs_server_dbg(VFS, "Encrypted frame parsing not supported yet\n");
274 spin_lock(&GlobalMid_Lock);
275 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
276 if ((mid->mid == wire_mid) &&
277 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
278 (mid->command == shdr->Command)) {
279 kref_get(&mid->refcount);
280 spin_unlock(&GlobalMid_Lock);
284 spin_unlock(&GlobalMid_Lock);
289 smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
291 #ifdef CONFIG_CIFS_DEBUG2
292 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
294 cifs_server_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
295 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
297 cifs_server_dbg(VFS, "smb buf %p len %u\n", buf,
298 server->ops->calc_smb_size(buf, server));
303 smb2_need_neg(struct TCP_Server_Info *server)
305 return server->max_read == 0;
309 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
313 cifs_ses_server(ses)->CurrentMid = 0;
314 rc = SMB2_negotiate(xid, ses);
315 /* BB we probably don't need to retry with modern servers */
322 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
324 struct TCP_Server_Info *server = tcon->ses->server;
327 /* start with specified wsize, or default */
328 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
329 wsize = min_t(unsigned int, wsize, server->max_write);
330 #ifdef CONFIG_CIFS_SMB_DIRECT
333 wsize = min_t(unsigned int,
334 wsize, server->smbd_conn->max_fragmented_send_size);
336 wsize = min_t(unsigned int,
337 wsize, server->smbd_conn->max_readwrite_size);
340 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
341 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
347 smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
349 struct TCP_Server_Info *server = tcon->ses->server;
352 /* start with specified wsize, or default */
353 wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
354 wsize = min_t(unsigned int, wsize, server->max_write);
355 #ifdef CONFIG_CIFS_SMB_DIRECT
358 wsize = min_t(unsigned int,
359 wsize, server->smbd_conn->max_fragmented_send_size);
361 wsize = min_t(unsigned int,
362 wsize, server->smbd_conn->max_readwrite_size);
365 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
366 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
372 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
374 struct TCP_Server_Info *server = tcon->ses->server;
377 /* start with specified rsize, or default */
378 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
379 rsize = min_t(unsigned int, rsize, server->max_read);
380 #ifdef CONFIG_CIFS_SMB_DIRECT
383 rsize = min_t(unsigned int,
384 rsize, server->smbd_conn->max_fragmented_recv_size);
386 rsize = min_t(unsigned int,
387 rsize, server->smbd_conn->max_readwrite_size);
391 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
392 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
398 smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
400 struct TCP_Server_Info *server = tcon->ses->server;
403 /* start with specified rsize, or default */
404 rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
405 rsize = min_t(unsigned int, rsize, server->max_read);
406 #ifdef CONFIG_CIFS_SMB_DIRECT
409 rsize = min_t(unsigned int,
410 rsize, server->smbd_conn->max_fragmented_recv_size);
412 rsize = min_t(unsigned int,
413 rsize, server->smbd_conn->max_readwrite_size);
417 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
418 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
424 parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
426 struct cifs_server_iface **iface_list,
429 struct network_interface_info_ioctl_rsp *p;
430 struct sockaddr_in *addr4;
431 struct sockaddr_in6 *addr6;
432 struct iface_info_ipv4 *p4;
433 struct iface_info_ipv6 *p6;
434 struct cifs_server_iface *info;
444 * Fist pass: count and sanity check
447 bytes_left = buf_len;
449 while (bytes_left >= sizeof(*p)) {
451 next = le32_to_cpu(p->Next);
453 bytes_left -= sizeof(*p);
456 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
461 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
466 if (bytes_left || p->Next)
467 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
471 * Second pass: extract info to internal structure
474 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
481 bytes_left = buf_len;
483 while (bytes_left >= sizeof(*p)) {
484 info->speed = le64_to_cpu(p->LinkSpeed);
485 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE);
486 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE);
488 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
489 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
490 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
491 le32_to_cpu(p->Capability));
495 * The kernel and wire socket structures have the same
496 * layout and use network byte order but make the
497 * conversion explicit in case either one changes.
500 addr4 = (struct sockaddr_in *)&info->sockaddr;
501 p4 = (struct iface_info_ipv4 *)p->Buffer;
502 addr4->sin_family = AF_INET;
503 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
505 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
506 addr4->sin_port = cpu_to_be16(CIFS_PORT);
508 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
512 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
513 p6 = (struct iface_info_ipv6 *)p->Buffer;
514 addr6->sin6_family = AF_INET6;
515 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
517 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
518 addr6->sin6_flowinfo = 0;
519 addr6->sin6_scope_id = 0;
520 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
522 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
527 "%s: skipping unsupported socket family\n",
535 next = le32_to_cpu(p->Next);
538 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
556 static int compare_iface(const void *ia, const void *ib)
558 const struct cifs_server_iface *a = (struct cifs_server_iface *)ia;
559 const struct cifs_server_iface *b = (struct cifs_server_iface *)ib;
561 return a->speed == b->speed ? 0 : (a->speed > b->speed ? -1 : 1);
565 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
568 unsigned int ret_data_len = 0;
569 struct network_interface_info_ioctl_rsp *out_buf = NULL;
570 struct cifs_server_iface *iface_list;
572 struct cifs_ses *ses = tcon->ses;
574 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
575 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
576 NULL /* no data input */, 0 /* no data input */,
577 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
578 if (rc == -EOPNOTSUPP) {
580 "server does not support query network interfaces\n");
582 } else if (rc != 0) {
583 cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
587 rc = parse_server_interfaces(out_buf, ret_data_len,
588 &iface_list, &iface_count);
592 /* sort interfaces from fastest to slowest */
593 sort(iface_list, iface_count, sizeof(*iface_list), compare_iface, NULL);
595 spin_lock(&ses->iface_lock);
596 kfree(ses->iface_list);
597 ses->iface_list = iface_list;
598 ses->iface_count = iface_count;
599 ses->iface_last_update = jiffies;
600 spin_unlock(&ses->iface_lock);
608 smb2_close_cached_fid(struct kref *ref)
610 struct cached_fid *cfid = container_of(ref, struct cached_fid,
613 if (cfid->is_valid) {
614 cifs_dbg(FYI, "clear cached root file handle\n");
615 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
616 cfid->fid->volatile_fid);
617 cfid->is_valid = false;
618 cfid->file_all_info_is_valid = false;
619 cfid->has_lease = false;
623 void close_shroot(struct cached_fid *cfid)
625 mutex_lock(&cfid->fid_mutex);
626 kref_put(&cfid->refcount, smb2_close_cached_fid);
627 mutex_unlock(&cfid->fid_mutex);
630 void close_shroot_lease_locked(struct cached_fid *cfid)
632 if (cfid->has_lease) {
633 cfid->has_lease = false;
634 kref_put(&cfid->refcount, smb2_close_cached_fid);
638 void close_shroot_lease(struct cached_fid *cfid)
640 mutex_lock(&cfid->fid_mutex);
641 close_shroot_lease_locked(cfid);
642 mutex_unlock(&cfid->fid_mutex);
646 smb2_cached_lease_break(struct work_struct *work)
648 struct cached_fid *cfid = container_of(work,
649 struct cached_fid, lease_break);
651 close_shroot_lease(cfid);
655 * Open the directory at the root of a share
657 int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
659 struct cifs_ses *ses = tcon->ses;
660 struct TCP_Server_Info *server = ses->server;
661 struct cifs_open_parms oparms;
662 struct smb2_create_rsp *o_rsp = NULL;
663 struct smb2_query_info_rsp *qi_rsp = NULL;
665 struct smb_rqst rqst[2];
666 struct kvec rsp_iov[2];
667 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
668 struct kvec qi_iov[1];
670 __le16 utf16_path = 0; /* Null - since an open of top of share */
671 u8 oplock = SMB2_OPLOCK_LEVEL_II;
673 mutex_lock(&tcon->crfid.fid_mutex);
674 if (tcon->crfid.is_valid) {
675 cifs_dbg(FYI, "found a cached root file handle\n");
676 memcpy(pfid, tcon->crfid.fid, sizeof(struct cifs_fid));
677 kref_get(&tcon->crfid.refcount);
678 mutex_unlock(&tcon->crfid.fid_mutex);
683 * We do not hold the lock for the open because in case
684 * SMB2_open needs to reconnect, it will end up calling
685 * cifs_mark_open_files_invalid() which takes the lock again
686 * thus causing a deadlock
689 mutex_unlock(&tcon->crfid.fid_mutex);
691 if (smb3_encryption_required(tcon))
692 flags |= CIFS_TRANSFORM_REQ;
694 memset(rqst, 0, sizeof(rqst));
695 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
696 memset(rsp_iov, 0, sizeof(rsp_iov));
699 memset(&open_iov, 0, sizeof(open_iov));
700 rqst[0].rq_iov = open_iov;
701 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
704 oparms.create_options = 0;
705 oparms.desired_access = FILE_READ_ATTRIBUTES;
706 oparms.disposition = FILE_OPEN;
708 oparms.reconnect = false;
710 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, &utf16_path);
713 smb2_set_next_command(tcon, &rqst[0]);
715 memset(&qi_iov, 0, sizeof(qi_iov));
716 rqst[1].rq_iov = qi_iov;
719 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
720 COMPOUND_FID, FILE_ALL_INFORMATION,
722 sizeof(struct smb2_file_all_info) +
723 PATH_MAX * 2, 0, NULL);
727 smb2_set_related(&rqst[1]);
729 rc = compound_send_recv(xid, ses, flags, 2, rqst,
730 resp_buftype, rsp_iov);
731 mutex_lock(&tcon->crfid.fid_mutex);
734 * Now we need to check again as the cached root might have
735 * been successfully re-opened from a concurrent process
738 if (tcon->crfid.is_valid) {
739 /* work was already done */
741 /* stash fids for close() later */
742 struct cifs_fid fid = {
743 .persistent_fid = pfid->persistent_fid,
744 .volatile_fid = pfid->volatile_fid,
748 * caller expects this func to set pfid to a valid
749 * cached root, so we copy the existing one and get a
752 memcpy(pfid, tcon->crfid.fid, sizeof(*pfid));
753 kref_get(&tcon->crfid.refcount);
755 mutex_unlock(&tcon->crfid.fid_mutex);
758 /* close extra handle outside of crit sec */
759 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
764 /* Cached root is still invalid, continue normaly */
767 if (rc == -EREMCHG) {
768 tcon->need_reconnect = true;
769 printk_once(KERN_WARNING "server share %s deleted\n",
775 atomic_inc(&tcon->num_remote_opens);
777 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
778 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
779 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
780 #ifdef CONFIG_CIFS_DEBUG2
781 oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
782 #endif /* CIFS_DEBUG2 */
784 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
785 tcon->crfid.tcon = tcon;
786 tcon->crfid.is_valid = true;
787 kref_init(&tcon->crfid.refcount);
789 /* BB TBD check to see if oplock level check can be removed below */
790 if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
791 kref_get(&tcon->crfid.refcount);
792 tcon->crfid.has_lease = true;
793 smb2_parse_contexts(server, o_rsp,
795 oparms.fid->lease_key, &oplock, NULL);
799 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
800 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
802 if (!smb2_validate_and_copy_iov(
803 le16_to_cpu(qi_rsp->OutputBufferOffset),
804 sizeof(struct smb2_file_all_info),
805 &rsp_iov[1], sizeof(struct smb2_file_all_info),
806 (char *)&tcon->crfid.file_all_info))
807 tcon->crfid.file_all_info_is_valid = true;
810 mutex_unlock(&tcon->crfid.fid_mutex);
812 SMB2_open_free(&rqst[0]);
813 SMB2_query_info_free(&rqst[1]);
814 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
815 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
820 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
823 __le16 srch_path = 0; /* Null - open root of share */
824 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
825 struct cifs_open_parms oparms;
827 bool no_cached_open = tcon->nohandlecache;
830 oparms.desired_access = FILE_READ_ATTRIBUTES;
831 oparms.disposition = FILE_OPEN;
832 oparms.create_options = 0;
834 oparms.reconnect = false;
837 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
840 rc = open_shroot(xid, tcon, &fid);
845 SMB3_request_interfaces(xid, tcon);
847 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
848 FS_ATTRIBUTE_INFORMATION);
849 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
850 FS_DEVICE_INFORMATION);
851 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
852 FS_VOLUME_INFORMATION);
853 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
854 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
856 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
858 close_shroot(&tcon->crfid);
862 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
865 __le16 srch_path = 0; /* Null - open root of share */
866 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
867 struct cifs_open_parms oparms;
871 oparms.desired_access = FILE_READ_ATTRIBUTES;
872 oparms.disposition = FILE_OPEN;
873 oparms.create_options = 0;
875 oparms.reconnect = false;
877 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
881 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
882 FS_ATTRIBUTE_INFORMATION);
883 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
884 FS_DEVICE_INFORMATION);
885 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
889 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
890 struct cifs_sb_info *cifs_sb, const char *full_path)
894 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
895 struct cifs_open_parms oparms;
898 if ((*full_path == 0) && tcon->crfid.is_valid)
901 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
906 oparms.desired_access = FILE_READ_ATTRIBUTES;
907 oparms.disposition = FILE_OPEN;
908 if (backup_cred(cifs_sb))
909 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
911 oparms.create_options = 0;
913 oparms.reconnect = false;
915 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
921 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
927 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
928 struct cifs_sb_info *cifs_sb, const char *full_path,
929 u64 *uniqueid, FILE_ALL_INFO *data)
931 *uniqueid = le64_to_cpu(data->IndexNumber);
936 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
937 struct cifs_fid *fid, FILE_ALL_INFO *data)
940 struct smb2_file_all_info *smb2_data;
942 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
944 if (smb2_data == NULL)
947 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
950 move_smb2_info_to_cifs(data, smb2_data);
955 #ifdef CONFIG_CIFS_XATTR
957 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
958 struct smb2_file_full_ea_info *src, size_t src_size,
959 const unsigned char *ea_name)
962 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
964 size_t buf_size = dst_size;
965 size_t name_len, value_len, user_name_len;
967 while (src_size > 0) {
968 name = &src->ea_data[0];
969 name_len = (size_t)src->ea_name_length;
970 value = &src->ea_data[src->ea_name_length + 1];
971 value_len = (size_t)le16_to_cpu(src->ea_value_length);
976 if (src_size < 8 + name_len + 1 + value_len) {
977 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
983 if (ea_name_len == name_len &&
984 memcmp(ea_name, name, name_len) == 0) {
988 if (dst_size < value_len) {
992 memcpy(dst, value, value_len);
996 /* 'user.' plus a terminating null */
997 user_name_len = 5 + 1 + name_len;
1000 /* skip copy - calc size only */
1001 rc += user_name_len;
1002 } else if (dst_size >= user_name_len) {
1003 dst_size -= user_name_len;
1004 memcpy(dst, "user.", 5);
1006 memcpy(dst, src->ea_data, name_len);
1010 rc += user_name_len;
1012 /* stop before overrun buffer */
1018 if (!src->next_entry_offset)
1021 if (src_size < le32_to_cpu(src->next_entry_offset)) {
1022 /* stop before overrun buffer */
1026 src_size -= le32_to_cpu(src->next_entry_offset);
1027 src = (void *)((char *)src +
1028 le32_to_cpu(src->next_entry_offset));
1031 /* didn't find the named attribute */
1040 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1041 const unsigned char *path, const unsigned char *ea_name,
1042 char *ea_data, size_t buf_size,
1043 struct cifs_sb_info *cifs_sb)
1047 struct kvec rsp_iov = {NULL, 0};
1048 int buftype = CIFS_NO_BUFFER;
1049 struct smb2_query_info_rsp *rsp;
1050 struct smb2_file_full_ea_info *info = NULL;
1052 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1056 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1058 FILE_FULL_EA_INFORMATION,
1061 MAX_SMB2_CREATE_RESPONSE_SIZE -
1062 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1063 &rsp_iov, &buftype, cifs_sb);
1066 * If ea_name is NULL (listxattr) and there are no EAs,
1067 * return 0 as it's not an error. Otherwise, the specified
1068 * ea_name was not found.
1070 if (!ea_name && rc == -ENODATA)
1075 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1076 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1077 le32_to_cpu(rsp->OutputBufferLength),
1079 sizeof(struct smb2_file_full_ea_info));
1083 info = (struct smb2_file_full_ea_info *)(
1084 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1085 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1086 le32_to_cpu(rsp->OutputBufferLength), ea_name);
1090 free_rsp_buf(buftype, rsp_iov.iov_base);
1096 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1097 const char *path, const char *ea_name, const void *ea_value,
1098 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1099 struct cifs_sb_info *cifs_sb)
1101 struct cifs_ses *ses = tcon->ses;
1102 __le16 *utf16_path = NULL;
1103 int ea_name_len = strlen(ea_name);
1106 struct smb_rqst rqst[3];
1107 int resp_buftype[3];
1108 struct kvec rsp_iov[3];
1109 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1110 struct cifs_open_parms oparms;
1111 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1112 struct cifs_fid fid;
1113 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1114 unsigned int size[1];
1116 struct smb2_file_full_ea_info *ea = NULL;
1117 struct kvec close_iov[1];
1120 if (smb3_encryption_required(tcon))
1121 flags |= CIFS_TRANSFORM_REQ;
1123 if (ea_name_len > 255)
1126 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1130 memset(rqst, 0, sizeof(rqst));
1131 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1132 memset(rsp_iov, 0, sizeof(rsp_iov));
1134 if (ses->server->ops->query_all_EAs) {
1136 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1145 memset(&open_iov, 0, sizeof(open_iov));
1146 rqst[0].rq_iov = open_iov;
1147 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1149 memset(&oparms, 0, sizeof(oparms));
1151 oparms.desired_access = FILE_WRITE_EA;
1152 oparms.disposition = FILE_OPEN;
1153 if (backup_cred(cifs_sb))
1154 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1156 oparms.create_options = 0;
1158 oparms.reconnect = false;
1160 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
1163 smb2_set_next_command(tcon, &rqst[0]);
1167 memset(&si_iov, 0, sizeof(si_iov));
1168 rqst[1].rq_iov = si_iov;
1169 rqst[1].rq_nvec = 1;
1171 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
1172 ea = kzalloc(len, GFP_KERNEL);
1178 ea->ea_name_length = ea_name_len;
1179 ea->ea_value_length = cpu_to_le16(ea_value_len);
1180 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1181 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1186 rc = SMB2_set_info_init(tcon, &rqst[1], COMPOUND_FID,
1187 COMPOUND_FID, current->tgid,
1188 FILE_FULL_EA_INFORMATION,
1189 SMB2_O_INFO_FILE, 0, data, size);
1190 smb2_set_next_command(tcon, &rqst[1]);
1191 smb2_set_related(&rqst[1]);
1195 memset(&close_iov, 0, sizeof(close_iov));
1196 rqst[2].rq_iov = close_iov;
1197 rqst[2].rq_nvec = 1;
1198 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1199 smb2_set_related(&rqst[2]);
1201 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1202 resp_buftype, rsp_iov);
1203 /* no need to bump num_remote_opens because handle immediately closed */
1208 SMB2_open_free(&rqst[0]);
1209 SMB2_set_info_free(&rqst[1]);
1210 SMB2_close_free(&rqst[2]);
1211 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1212 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1213 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1219 smb2_can_echo(struct TCP_Server_Info *server)
1221 return server->echoes;
1225 smb2_clear_stats(struct cifs_tcon *tcon)
1229 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1230 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1231 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1236 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1238 seq_puts(m, "\n\tShare Capabilities:");
1239 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1240 seq_puts(m, " DFS,");
1241 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1242 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1243 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1244 seq_puts(m, " SCALEOUT,");
1245 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1246 seq_puts(m, " CLUSTER,");
1247 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1248 seq_puts(m, " ASYMMETRIC,");
1249 if (tcon->capabilities == 0)
1250 seq_puts(m, " None");
1251 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1252 seq_puts(m, " Aligned,");
1253 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1254 seq_puts(m, " Partition Aligned,");
1255 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1256 seq_puts(m, " SSD,");
1257 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1258 seq_puts(m, " TRIM-support,");
1260 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1261 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1262 if (tcon->perf_sector_size)
1263 seq_printf(m, "\tOptimal sector size: 0x%x",
1264 tcon->perf_sector_size);
1265 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1269 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1271 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1272 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1275 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1276 * totals (requests sent) since those SMBs are per-session not per tcon
1278 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1279 (long long)(tcon->bytes_read),
1280 (long long)(tcon->bytes_written));
1281 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1282 atomic_read(&tcon->num_local_opens),
1283 atomic_read(&tcon->num_remote_opens));
1284 seq_printf(m, "\nTreeConnects: %d total %d failed",
1285 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1286 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1287 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1288 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1289 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1290 seq_printf(m, "\nCreates: %d total %d failed",
1291 atomic_read(&sent[SMB2_CREATE_HE]),
1292 atomic_read(&failed[SMB2_CREATE_HE]));
1293 seq_printf(m, "\nCloses: %d total %d failed",
1294 atomic_read(&sent[SMB2_CLOSE_HE]),
1295 atomic_read(&failed[SMB2_CLOSE_HE]));
1296 seq_printf(m, "\nFlushes: %d total %d failed",
1297 atomic_read(&sent[SMB2_FLUSH_HE]),
1298 atomic_read(&failed[SMB2_FLUSH_HE]));
1299 seq_printf(m, "\nReads: %d total %d failed",
1300 atomic_read(&sent[SMB2_READ_HE]),
1301 atomic_read(&failed[SMB2_READ_HE]));
1302 seq_printf(m, "\nWrites: %d total %d failed",
1303 atomic_read(&sent[SMB2_WRITE_HE]),
1304 atomic_read(&failed[SMB2_WRITE_HE]));
1305 seq_printf(m, "\nLocks: %d total %d failed",
1306 atomic_read(&sent[SMB2_LOCK_HE]),
1307 atomic_read(&failed[SMB2_LOCK_HE]));
1308 seq_printf(m, "\nIOCTLs: %d total %d failed",
1309 atomic_read(&sent[SMB2_IOCTL_HE]),
1310 atomic_read(&failed[SMB2_IOCTL_HE]));
1311 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1312 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1313 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1314 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1315 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1316 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1317 seq_printf(m, "\nQueryInfos: %d total %d failed",
1318 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1319 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1320 seq_printf(m, "\nSetInfos: %d total %d failed",
1321 atomic_read(&sent[SMB2_SET_INFO_HE]),
1322 atomic_read(&failed[SMB2_SET_INFO_HE]));
1323 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1324 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1325 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1329 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1331 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1332 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1334 cfile->fid.persistent_fid = fid->persistent_fid;
1335 cfile->fid.volatile_fid = fid->volatile_fid;
1336 #ifdef CONFIG_CIFS_DEBUG2
1337 cfile->fid.mid = fid->mid;
1338 #endif /* CIFS_DEBUG2 */
1339 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1341 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1342 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1346 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1347 struct cifs_fid *fid)
1349 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1353 smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
1354 struct cifsFileInfo *cfile)
1356 struct smb2_file_network_open_info file_inf;
1357 struct inode *inode;
1360 rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
1361 cfile->fid.volatile_fid, &file_inf);
1365 inode = d_inode(cfile->dentry);
1367 spin_lock(&inode->i_lock);
1368 CIFS_I(inode)->time = jiffies;
1370 /* Creation time should not need to be updated on close */
1371 if (file_inf.LastWriteTime)
1372 inode->i_mtime = cifs_NTtimeToUnix(file_inf.LastWriteTime);
1373 if (file_inf.ChangeTime)
1374 inode->i_ctime = cifs_NTtimeToUnix(file_inf.ChangeTime);
1375 if (file_inf.LastAccessTime)
1376 inode->i_atime = cifs_NTtimeToUnix(file_inf.LastAccessTime);
1379 * i_blocks is not related to (i_size / i_blksize),
1380 * but instead 512 byte (2**9) size is required for
1381 * calculating num blocks.
1383 if (le64_to_cpu(file_inf.AllocationSize) > 4096)
1385 (512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
1387 /* End of file and Attributes should not have to be updated on close */
1388 spin_unlock(&inode->i_lock);
1392 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1393 u64 persistent_fid, u64 volatile_fid,
1394 struct copychunk_ioctl *pcchunk)
1397 unsigned int ret_data_len;
1398 struct resume_key_req *res_key;
1400 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1401 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1402 NULL, 0 /* no input */, CIFSMaxBufSize,
1403 (char **)&res_key, &ret_data_len);
1406 cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1407 goto req_res_key_exit;
1409 if (ret_data_len < sizeof(struct resume_key_req)) {
1410 cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
1412 goto req_res_key_exit;
1414 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1422 smb2_ioctl_query_info(const unsigned int xid,
1423 struct cifs_tcon *tcon,
1424 __le16 *path, int is_dir,
1427 struct cifs_ses *ses = tcon->ses;
1428 char __user *arg = (char __user *)p;
1429 struct smb_query_info qi;
1430 struct smb_query_info __user *pqi;
1433 struct smb2_query_info_rsp *qi_rsp = NULL;
1434 struct smb2_ioctl_rsp *io_rsp = NULL;
1435 void *buffer = NULL;
1436 struct smb_rqst rqst[3];
1437 int resp_buftype[3];
1438 struct kvec rsp_iov[3];
1439 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1440 struct cifs_open_parms oparms;
1441 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1442 struct cifs_fid fid;
1443 struct kvec qi_iov[1];
1444 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1445 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1446 struct kvec close_iov[1];
1447 unsigned int size[2];
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;
1480 oparms.create_options = CREATE_NOT_FILE;
1482 oparms.create_options = CREATE_NOT_DIR;
1484 oparms.reconnect = false;
1486 if (qi.flags & PASSTHRU_FSCTL) {
1487 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1488 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1489 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1491 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1492 oparms.desired_access = GENERIC_ALL;
1494 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1495 oparms.desired_access = GENERIC_READ;
1497 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1498 oparms.desired_access = GENERIC_WRITE;
1501 } else if (qi.flags & PASSTHRU_SET_INFO) {
1502 oparms.desired_access = GENERIC_WRITE;
1504 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1507 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, path);
1510 smb2_set_next_command(tcon, &rqst[0]);
1513 if (qi.flags & PASSTHRU_FSCTL) {
1514 /* Can eventually relax perm check since server enforces too */
1515 if (!capable(CAP_SYS_ADMIN))
1518 memset(&io_iov, 0, sizeof(io_iov));
1519 rqst[1].rq_iov = io_iov;
1520 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1522 rc = SMB2_ioctl_init(tcon, &rqst[1],
1523 COMPOUND_FID, COMPOUND_FID,
1524 qi.info_type, true, buffer,
1525 qi.output_buffer_length,
1528 } else if (qi.flags == PASSTHRU_SET_INFO) {
1529 /* Can eventually relax perm check since server enforces too */
1530 if (!capable(CAP_SYS_ADMIN))
1533 memset(&si_iov, 0, sizeof(si_iov));
1534 rqst[1].rq_iov = si_iov;
1535 rqst[1].rq_nvec = 1;
1540 rc = SMB2_set_info_init(tcon, &rqst[1],
1541 COMPOUND_FID, COMPOUND_FID,
1543 FILE_END_OF_FILE_INFORMATION,
1544 SMB2_O_INFO_FILE, 0, data, size);
1546 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1547 memset(&qi_iov, 0, sizeof(qi_iov));
1548 rqst[1].rq_iov = qi_iov;
1549 rqst[1].rq_nvec = 1;
1551 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
1552 COMPOUND_FID, qi.file_info_class,
1553 qi.info_type, qi.additional_information,
1554 qi.input_buffer_length,
1555 qi.output_buffer_length, buffer);
1556 } else { /* unknown flags */
1557 cifs_tcon_dbg(VFS, "invalid passthru query flags: 0x%x\n", qi.flags);
1563 smb2_set_next_command(tcon, &rqst[1]);
1564 smb2_set_related(&rqst[1]);
1567 memset(&close_iov, 0, sizeof(close_iov));
1568 rqst[2].rq_iov = close_iov;
1569 rqst[2].rq_nvec = 1;
1571 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1574 smb2_set_related(&rqst[2]);
1576 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1577 resp_buftype, rsp_iov);
1581 /* No need to bump num_remote_opens since handle immediately closed */
1582 if (qi.flags & PASSTHRU_FSCTL) {
1583 pqi = (struct smb_query_info __user *)arg;
1584 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1585 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1586 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1587 if (qi.input_buffer_length > 0 &&
1588 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1589 > rsp_iov[1].iov_len)
1592 if (copy_to_user(&pqi->input_buffer_length,
1593 &qi.input_buffer_length,
1594 sizeof(qi.input_buffer_length)))
1597 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1598 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1599 qi.input_buffer_length))
1602 pqi = (struct smb_query_info __user *)arg;
1603 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1604 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1605 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1606 if (copy_to_user(&pqi->input_buffer_length,
1607 &qi.input_buffer_length,
1608 sizeof(qi.input_buffer_length)))
1611 if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1612 qi.input_buffer_length))
1618 SMB2_open_free(&rqst[0]);
1619 if (qi.flags & PASSTHRU_FSCTL)
1620 SMB2_ioctl_free(&rqst[1]);
1622 SMB2_query_info_free(&rqst[1]);
1624 SMB2_close_free(&rqst[2]);
1625 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1626 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1627 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1636 smb2_copychunk_range(const unsigned int xid,
1637 struct cifsFileInfo *srcfile,
1638 struct cifsFileInfo *trgtfile, u64 src_off,
1639 u64 len, u64 dest_off)
1642 unsigned int ret_data_len;
1643 struct copychunk_ioctl *pcchunk;
1644 struct copychunk_ioctl_rsp *retbuf = NULL;
1645 struct cifs_tcon *tcon;
1646 int chunks_copied = 0;
1647 bool chunk_sizes_updated = false;
1648 ssize_t bytes_written, total_bytes_written = 0;
1650 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1652 if (pcchunk == NULL)
1655 cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
1656 /* Request a key from the server to identify the source of the copy */
1657 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1658 srcfile->fid.persistent_fid,
1659 srcfile->fid.volatile_fid, pcchunk);
1661 /* Note: request_res_key sets res_key null only if rc !=0 */
1665 /* For now array only one chunk long, will make more flexible later */
1666 pcchunk->ChunkCount = cpu_to_le32(1);
1667 pcchunk->Reserved = 0;
1668 pcchunk->Reserved2 = 0;
1670 tcon = tlink_tcon(trgtfile->tlink);
1673 pcchunk->SourceOffset = cpu_to_le64(src_off);
1674 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1676 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1678 /* Request server copy to target from src identified by key */
1679 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1680 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1681 true /* is_fsctl */, (char *)pcchunk,
1682 sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1683 (char **)&retbuf, &ret_data_len);
1686 sizeof(struct copychunk_ioctl_rsp)) {
1687 cifs_tcon_dbg(VFS, "invalid cchunk response size\n");
1691 if (retbuf->TotalBytesWritten == 0) {
1692 cifs_dbg(FYI, "no bytes copied\n");
1697 * Check if server claimed to write more than we asked
1699 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1700 le32_to_cpu(pcchunk->Length)) {
1701 cifs_tcon_dbg(VFS, "invalid copy chunk response\n");
1705 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1706 cifs_tcon_dbg(VFS, "invalid num chunks written\n");
1712 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1713 src_off += bytes_written;
1714 dest_off += bytes_written;
1715 len -= bytes_written;
1716 total_bytes_written += bytes_written;
1718 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1719 le32_to_cpu(retbuf->ChunksWritten),
1720 le32_to_cpu(retbuf->ChunkBytesWritten),
1722 } else if (rc == -EINVAL) {
1723 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1726 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1727 le32_to_cpu(retbuf->ChunksWritten),
1728 le32_to_cpu(retbuf->ChunkBytesWritten),
1729 le32_to_cpu(retbuf->TotalBytesWritten));
1732 * Check if this is the first request using these sizes,
1733 * (ie check if copy succeed once with original sizes
1734 * and check if the server gave us different sizes after
1735 * we already updated max sizes on previous request).
1736 * if not then why is the server returning an error now
1738 if ((chunks_copied != 0) || chunk_sizes_updated)
1741 /* Check that server is not asking us to grow size */
1742 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1743 tcon->max_bytes_chunk)
1744 tcon->max_bytes_chunk =
1745 le32_to_cpu(retbuf->ChunkBytesWritten);
1747 goto cchunk_out; /* server gave us bogus size */
1749 /* No need to change MaxChunks since already set to 1 */
1750 chunk_sizes_updated = true;
1761 return total_bytes_written;
1765 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1766 struct cifs_fid *fid)
1768 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1772 smb2_read_data_offset(char *buf)
1774 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1776 return rsp->DataOffset;
1780 smb2_read_data_length(char *buf, bool in_remaining)
1782 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1785 return le32_to_cpu(rsp->DataRemaining);
1787 return le32_to_cpu(rsp->DataLength);
1792 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1793 struct cifs_io_parms *parms, unsigned int *bytes_read,
1794 char **buf, int *buf_type)
1796 parms->persistent_fid = pfid->persistent_fid;
1797 parms->volatile_fid = pfid->volatile_fid;
1798 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1802 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1803 struct cifs_io_parms *parms, unsigned int *written,
1804 struct kvec *iov, unsigned long nr_segs)
1807 parms->persistent_fid = pfid->persistent_fid;
1808 parms->volatile_fid = pfid->volatile_fid;
1809 return SMB2_write(xid, parms, written, iov, nr_segs);
1812 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1813 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1814 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1816 struct cifsInodeInfo *cifsi;
1819 cifsi = CIFS_I(inode);
1821 /* if file already sparse don't bother setting sparse again */
1822 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1823 return true; /* already sparse */
1825 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1826 return true; /* already not sparse */
1829 * Can't check for sparse support on share the usual way via the
1830 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1831 * since Samba server doesn't set the flag on the share, yet
1832 * supports the set sparse FSCTL and returns sparse correctly
1833 * in the file attributes. If we fail setting sparse though we
1834 * mark that server does not support sparse files for this share
1835 * to avoid repeatedly sending the unsupported fsctl to server
1836 * if the file is repeatedly extended.
1838 if (tcon->broken_sparse_sup)
1841 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1842 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1844 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
1846 tcon->broken_sparse_sup = true;
1847 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1852 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1854 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1860 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1861 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1863 __le64 eof = cpu_to_le64(size);
1864 struct inode *inode;
1867 * If extending file more than one page make sparse. Many Linux fs
1868 * make files sparse by default when extending via ftruncate
1870 inode = d_inode(cfile->dentry);
1872 if (!set_alloc && (size > inode->i_size + 8192)) {
1873 __u8 set_sparse = 1;
1875 /* whether set sparse succeeds or not, extend the file */
1876 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1879 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1880 cfile->fid.volatile_fid, cfile->pid, &eof);
1884 smb2_duplicate_extents(const unsigned int xid,
1885 struct cifsFileInfo *srcfile,
1886 struct cifsFileInfo *trgtfile, u64 src_off,
1887 u64 len, u64 dest_off)
1890 unsigned int ret_data_len;
1891 struct duplicate_extents_to_file dup_ext_buf;
1892 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1894 /* server fileays advertise duplicate extent support with this flag */
1895 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1896 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1899 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1900 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1901 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1902 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1903 dup_ext_buf.ByteCount = cpu_to_le64(len);
1904 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
1905 src_off, dest_off, len);
1907 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1909 goto duplicate_extents_out;
1911 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1912 trgtfile->fid.volatile_fid,
1913 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
1914 true /* is_fsctl */,
1915 (char *)&dup_ext_buf,
1916 sizeof(struct duplicate_extents_to_file),
1917 CIFSMaxBufSize, NULL,
1920 if (ret_data_len > 0)
1921 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
1923 duplicate_extents_out:
1928 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1929 struct cifsFileInfo *cfile)
1931 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1932 cfile->fid.volatile_fid);
1936 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1937 struct cifsFileInfo *cfile)
1939 struct fsctl_set_integrity_information_req integr_info;
1940 unsigned int ret_data_len;
1942 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1943 integr_info.Flags = 0;
1944 integr_info.Reserved = 0;
1946 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1947 cfile->fid.volatile_fid,
1948 FSCTL_SET_INTEGRITY_INFORMATION,
1949 true /* is_fsctl */,
1950 (char *)&integr_info,
1951 sizeof(struct fsctl_set_integrity_information_req),
1952 CIFSMaxBufSize, NULL,
1957 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
1958 #define GMT_TOKEN_SIZE 50
1960 #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
1963 * Input buffer contains (empty) struct smb_snapshot array with size filled in
1964 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
1967 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1968 struct cifsFileInfo *cfile, void __user *ioc_buf)
1970 char *retbuf = NULL;
1971 unsigned int ret_data_len = 0;
1973 u32 max_response_size;
1974 struct smb_snapshot_array snapshot_in;
1977 * On the first query to enumerate the list of snapshots available
1978 * for this volume the buffer begins with 0 (number of snapshots
1979 * which can be returned is zero since at that point we do not know
1980 * how big the buffer needs to be). On the second query,
1981 * it (ret_data_len) is set to number of snapshots so we can
1982 * know to set the maximum response size larger (see below).
1984 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
1988 * Note that for snapshot queries that servers like Azure expect that
1989 * the first query be minimal size (and just used to get the number/size
1990 * of previous versions) so response size must be specified as EXACTLY
1991 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
1994 if (ret_data_len == 0)
1995 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
1997 max_response_size = CIFSMaxBufSize;
1999 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2000 cfile->fid.volatile_fid,
2001 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
2002 true /* is_fsctl */,
2003 NULL, 0 /* no input data */, max_response_size,
2006 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2011 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2013 if (copy_from_user(&snapshot_in, ioc_buf,
2014 sizeof(struct smb_snapshot_array))) {
2021 * Check for min size, ie not large enough to fit even one GMT
2022 * token (snapshot). On the first ioctl some users may pass in
2023 * smaller size (or zero) to simply get the size of the array
2024 * so the user space caller can allocate sufficient memory
2025 * and retry the ioctl again with larger array size sufficient
2026 * to hold all of the snapshot GMT tokens on the second try.
2028 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2029 ret_data_len = sizeof(struct smb_snapshot_array);
2032 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2033 * the snapshot array (of 50 byte GMT tokens) each
2034 * representing an available previous version of the data
2036 if (ret_data_len > (snapshot_in.snapshot_array_size +
2037 sizeof(struct smb_snapshot_array)))
2038 ret_data_len = snapshot_in.snapshot_array_size +
2039 sizeof(struct smb_snapshot_array);
2041 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2050 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2051 const char *path, struct cifs_sb_info *cifs_sb,
2052 struct cifs_fid *fid, __u16 search_flags,
2053 struct cifs_search_info *srch_inf)
2056 struct smb_rqst rqst[2];
2057 struct kvec rsp_iov[2];
2058 int resp_buftype[2];
2059 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2060 struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
2062 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2063 struct cifs_open_parms oparms;
2064 struct smb2_query_directory_rsp *qd_rsp = NULL;
2065 struct smb2_create_rsp *op_rsp = NULL;
2067 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2071 if (smb3_encryption_required(tcon))
2072 flags |= CIFS_TRANSFORM_REQ;
2074 memset(rqst, 0, sizeof(rqst));
2075 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2076 memset(rsp_iov, 0, sizeof(rsp_iov));
2079 memset(&open_iov, 0, sizeof(open_iov));
2080 rqst[0].rq_iov = open_iov;
2081 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2084 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2085 oparms.disposition = FILE_OPEN;
2086 if (backup_cred(cifs_sb))
2087 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2089 oparms.create_options = 0;
2091 oparms.reconnect = false;
2093 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2096 smb2_set_next_command(tcon, &rqst[0]);
2098 /* Query directory */
2099 srch_inf->entries_in_buffer = 0;
2100 srch_inf->index_of_last_entry = 2;
2102 memset(&qd_iov, 0, sizeof(qd_iov));
2103 rqst[1].rq_iov = qd_iov;
2104 rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
2106 rc = SMB2_query_directory_init(xid, tcon, &rqst[1],
2107 COMPOUND_FID, COMPOUND_FID,
2108 0, srch_inf->info_level);
2112 smb2_set_related(&rqst[1]);
2114 rc = compound_send_recv(xid, tcon->ses, flags, 2, rqst,
2115 resp_buftype, rsp_iov);
2117 /* If the open failed there is nothing to do */
2118 op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
2119 if (op_rsp == NULL || op_rsp->sync_hdr.Status != STATUS_SUCCESS) {
2120 cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
2123 fid->persistent_fid = op_rsp->PersistentFileId;
2124 fid->volatile_fid = op_rsp->VolatileFileId;
2126 /* Anything else than ENODATA means a genuine error */
2127 if (rc && rc != -ENODATA) {
2128 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2129 cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
2130 trace_smb3_query_dir_err(xid, fid->persistent_fid,
2131 tcon->tid, tcon->ses->Suid, 0, 0, rc);
2135 qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
2136 if (qd_rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
2137 trace_smb3_query_dir_done(xid, fid->persistent_fid,
2138 tcon->tid, tcon->ses->Suid, 0, 0);
2139 srch_inf->endOfSearch = true;
2144 rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
2147 trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
2148 tcon->ses->Suid, 0, 0, rc);
2151 resp_buftype[1] = CIFS_NO_BUFFER;
2153 trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
2154 tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
2158 SMB2_open_free(&rqst[0]);
2159 SMB2_query_directory_free(&rqst[1]);
2160 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2161 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2166 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2167 struct cifs_fid *fid, __u16 search_flags,
2168 struct cifs_search_info *srch_inf)
2170 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2171 fid->volatile_fid, 0, srch_inf);
2175 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2176 struct cifs_fid *fid)
2178 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2182 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2183 * the number of credits and return true. Otherwise - return false.
2186 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
2188 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2190 if (shdr->Status != STATUS_PENDING)
2193 if (shdr->CreditRequest) {
2194 spin_lock(&server->req_lock);
2195 server->credits += le16_to_cpu(shdr->CreditRequest);
2196 spin_unlock(&server->req_lock);
2197 wake_up(&server->request_q);
2204 smb2_is_session_expired(char *buf)
2206 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2208 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2209 shdr->Status != STATUS_USER_SESSION_DELETED)
2212 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2213 le16_to_cpu(shdr->Command),
2214 le64_to_cpu(shdr->MessageId));
2215 cifs_dbg(FYI, "Session expired or deleted\n");
2221 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2222 struct cifsInodeInfo *cinode)
2224 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2225 return SMB2_lease_break(0, tcon, cinode->lease_key,
2226 smb2_get_lease_state(cinode));
2228 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2230 CIFS_CACHE_READ(cinode) ? 1 : 0);
2234 smb2_set_related(struct smb_rqst *rqst)
2236 struct smb2_sync_hdr *shdr;
2238 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2240 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2243 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2246 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2249 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2251 struct smb2_sync_hdr *shdr;
2252 struct cifs_ses *ses = tcon->ses;
2253 struct TCP_Server_Info *server = ses->server;
2254 unsigned long len = smb_rqst_len(server, rqst);
2257 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2259 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2263 /* SMB headers in a compound are 8 byte aligned. */
2265 /* No padding needed */
2269 num_padding = 8 - (len & 7);
2270 if (!smb3_encryption_required(tcon)) {
2272 * If we do not have encryption then we can just add an extra
2273 * iov for the padding.
2275 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2276 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2281 * We can not add a small padding iov for the encryption case
2282 * because the encryption framework can not handle the padding
2284 * We have to flatten this into a single buffer and add
2285 * the padding to it.
2287 for (i = 1; i < rqst->rq_nvec; i++) {
2288 memcpy(rqst->rq_iov[0].iov_base +
2289 rqst->rq_iov[0].iov_len,
2290 rqst->rq_iov[i].iov_base,
2291 rqst->rq_iov[i].iov_len);
2292 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2294 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2296 rqst->rq_iov[0].iov_len += num_padding;
2302 shdr->NextCommand = cpu_to_le32(len);
2306 * Passes the query info response back to the caller on success.
2307 * Caller need to free this with free_rsp_buf().
2310 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2311 __le16 *utf16_path, u32 desired_access,
2312 u32 class, u32 type, u32 output_len,
2313 struct kvec *rsp, int *buftype,
2314 struct cifs_sb_info *cifs_sb)
2316 struct cifs_ses *ses = tcon->ses;
2318 struct smb_rqst rqst[3];
2319 int resp_buftype[3];
2320 struct kvec rsp_iov[3];
2321 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2322 struct kvec qi_iov[1];
2323 struct kvec close_iov[1];
2324 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2325 struct cifs_open_parms oparms;
2326 struct cifs_fid fid;
2329 if (smb3_encryption_required(tcon))
2330 flags |= CIFS_TRANSFORM_REQ;
2332 memset(rqst, 0, sizeof(rqst));
2333 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2334 memset(rsp_iov, 0, sizeof(rsp_iov));
2336 memset(&open_iov, 0, sizeof(open_iov));
2337 rqst[0].rq_iov = open_iov;
2338 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2341 oparms.desired_access = desired_access;
2342 oparms.disposition = FILE_OPEN;
2343 if (cifs_sb && backup_cred(cifs_sb))
2344 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2346 oparms.create_options = 0;
2348 oparms.reconnect = false;
2350 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2353 smb2_set_next_command(tcon, &rqst[0]);
2355 memset(&qi_iov, 0, sizeof(qi_iov));
2356 rqst[1].rq_iov = qi_iov;
2357 rqst[1].rq_nvec = 1;
2359 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
2365 smb2_set_next_command(tcon, &rqst[1]);
2366 smb2_set_related(&rqst[1]);
2368 memset(&close_iov, 0, sizeof(close_iov));
2369 rqst[2].rq_iov = close_iov;
2370 rqst[2].rq_nvec = 1;
2372 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2375 smb2_set_related(&rqst[2]);
2377 rc = compound_send_recv(xid, ses, flags, 3, rqst,
2378 resp_buftype, rsp_iov);
2380 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2381 if (rc == -EREMCHG) {
2382 tcon->need_reconnect = true;
2383 printk_once(KERN_WARNING "server share %s deleted\n",
2389 *buftype = resp_buftype[1];
2392 SMB2_open_free(&rqst[0]);
2393 SMB2_query_info_free(&rqst[1]);
2394 SMB2_close_free(&rqst[2]);
2395 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2396 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2401 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2402 struct kstatfs *buf)
2404 struct smb2_query_info_rsp *rsp;
2405 struct smb2_fs_full_size_info *info = NULL;
2406 __le16 utf16_path = 0; /* Null - open root of share */
2407 struct kvec rsp_iov = {NULL, 0};
2408 int buftype = CIFS_NO_BUFFER;
2412 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2413 FILE_READ_ATTRIBUTES,
2414 FS_FULL_SIZE_INFORMATION,
2415 SMB2_O_INFO_FILESYSTEM,
2416 sizeof(struct smb2_fs_full_size_info),
2417 &rsp_iov, &buftype, NULL);
2421 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2422 buf->f_type = SMB2_MAGIC_NUMBER;
2423 info = (struct smb2_fs_full_size_info *)(
2424 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2425 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2426 le32_to_cpu(rsp->OutputBufferLength),
2428 sizeof(struct smb2_fs_full_size_info));
2430 smb2_copy_fs_info_to_kstatfs(info, buf);
2433 free_rsp_buf(buftype, rsp_iov.iov_base);
2438 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2439 struct kstatfs *buf)
2442 __le16 srch_path = 0; /* Null - open root of share */
2443 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2444 struct cifs_open_parms oparms;
2445 struct cifs_fid fid;
2447 if (!tcon->posix_extensions)
2448 return smb2_queryfs(xid, tcon, buf);
2451 oparms.desired_access = FILE_READ_ATTRIBUTES;
2452 oparms.disposition = FILE_OPEN;
2453 oparms.create_options = 0;
2455 oparms.reconnect = false;
2457 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
2461 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2462 fid.volatile_fid, buf);
2463 buf->f_type = SMB2_MAGIC_NUMBER;
2464 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2469 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2471 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2472 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2476 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2477 __u64 length, __u32 type, int lock, int unlock, bool wait)
2479 if (unlock && !lock)
2480 type = SMB2_LOCKFLAG_UNLOCK;
2481 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2482 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2483 current->tgid, length, offset, type, wait);
2487 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2489 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2493 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2495 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2499 smb2_new_lease_key(struct cifs_fid *fid)
2501 generate_random_uuid(fid->lease_key);
2505 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2506 const char *search_name,
2507 struct dfs_info3_param **target_nodes,
2508 unsigned int *num_of_nodes,
2509 const struct nls_table *nls_codepage, int remap)
2512 __le16 *utf16_path = NULL;
2513 int utf16_path_len = 0;
2514 struct cifs_tcon *tcon;
2515 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2516 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2517 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2519 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
2522 * Try to use the IPC tcon, otherwise just use any
2524 tcon = ses->tcon_ipc;
2526 spin_lock(&cifs_tcp_ses_lock);
2527 tcon = list_first_entry_or_null(&ses->tcon_list,
2532 spin_unlock(&cifs_tcp_ses_lock);
2536 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2542 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2544 nls_codepage, remap);
2550 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2551 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2557 /* Highest DFS referral version understood */
2558 dfs_req->MaxReferralLevel = DFS_VERSION;
2560 /* Path to resolve in an UTF-16 null-terminated string */
2561 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2564 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2565 FSCTL_DFS_GET_REFERRALS,
2566 true /* is_fsctl */,
2567 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
2568 (char **)&dfs_rsp, &dfs_rsp_size);
2569 } while (rc == -EAGAIN);
2572 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
2573 cifs_tcon_dbg(VFS, "ioctl error in %s rc=%d\n", __func__, rc);
2577 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2578 num_of_nodes, target_nodes,
2579 nls_codepage, remap, search_name,
2580 true /* is_unicode */);
2582 cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
2587 if (tcon && !tcon->ipc) {
2588 /* ipc tcons are not refcounted */
2589 spin_lock(&cifs_tcp_ses_lock);
2591 spin_unlock(&cifs_tcp_ses_lock);
2600 parse_reparse_posix(struct reparse_posix_data *symlink_buf,
2601 u32 plen, char **target_path,
2602 struct cifs_sb_info *cifs_sb)
2606 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
2607 len = le16_to_cpu(symlink_buf->ReparseDataLength);
2609 if (le64_to_cpu(symlink_buf->InodeType) != NFS_SPECFILE_LNK) {
2610 cifs_dbg(VFS, "%lld not a supported symlink type\n",
2611 le64_to_cpu(symlink_buf->InodeType));
2615 *target_path = cifs_strndup_from_utf16(
2616 symlink_buf->PathBuffer,
2617 len, true, cifs_sb->local_nls);
2618 if (!(*target_path))
2621 convert_delimiter(*target_path, '/');
2622 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2628 parse_reparse_symlink(struct reparse_symlink_data_buffer *symlink_buf,
2629 u32 plen, char **target_path,
2630 struct cifs_sb_info *cifs_sb)
2632 unsigned int sub_len;
2633 unsigned int sub_offset;
2635 /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
2637 sub_offset = le16_to_cpu(symlink_buf->SubstituteNameOffset);
2638 sub_len = le16_to_cpu(symlink_buf->SubstituteNameLength);
2639 if (sub_offset + 20 > plen ||
2640 sub_offset + sub_len + 20 > plen) {
2641 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
2645 *target_path = cifs_strndup_from_utf16(
2646 symlink_buf->PathBuffer + sub_offset,
2647 sub_len, true, cifs_sb->local_nls);
2648 if (!(*target_path))
2651 convert_delimiter(*target_path, '/');
2652 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2658 parse_reparse_point(struct reparse_data_buffer *buf,
2659 u32 plen, char **target_path,
2660 struct cifs_sb_info *cifs_sb)
2662 if (plen < sizeof(struct reparse_data_buffer)) {
2663 cifs_dbg(VFS, "reparse buffer is too small. Must be "
2664 "at least 8 bytes but was %d\n", plen);
2668 if (plen < le16_to_cpu(buf->ReparseDataLength) +
2669 sizeof(struct reparse_data_buffer)) {
2670 cifs_dbg(VFS, "srv returned invalid reparse buf "
2671 "length: %d\n", plen);
2675 /* See MS-FSCC 2.1.2 */
2676 switch (le32_to_cpu(buf->ReparseTag)) {
2677 case IO_REPARSE_TAG_NFS:
2678 return parse_reparse_posix(
2679 (struct reparse_posix_data *)buf,
2680 plen, target_path, cifs_sb);
2681 case IO_REPARSE_TAG_SYMLINK:
2682 return parse_reparse_symlink(
2683 (struct reparse_symlink_data_buffer *)buf,
2684 plen, target_path, cifs_sb);
2686 cifs_dbg(VFS, "srv returned unknown symlink buffer "
2687 "tag:0x%08x\n", le32_to_cpu(buf->ReparseTag));
2692 #define SMB2_SYMLINK_STRUCT_SIZE \
2693 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2696 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
2697 struct cifs_sb_info *cifs_sb, const char *full_path,
2698 char **target_path, bool is_reparse_point)
2701 __le16 *utf16_path = NULL;
2702 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2703 struct cifs_open_parms oparms;
2704 struct cifs_fid fid;
2705 struct kvec err_iov = {NULL, 0};
2706 struct smb2_err_rsp *err_buf = NULL;
2707 struct smb2_symlink_err_rsp *symlink;
2708 unsigned int sub_len;
2709 unsigned int sub_offset;
2710 unsigned int print_len;
2711 unsigned int print_offset;
2713 struct smb_rqst rqst[3];
2714 int resp_buftype[3];
2715 struct kvec rsp_iov[3];
2716 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2717 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2718 struct kvec close_iov[1];
2719 struct smb2_create_rsp *create_rsp;
2720 struct smb2_ioctl_rsp *ioctl_rsp;
2721 struct reparse_data_buffer *reparse_buf;
2724 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2726 *target_path = NULL;
2728 if (smb3_encryption_required(tcon))
2729 flags |= CIFS_TRANSFORM_REQ;
2731 memset(rqst, 0, sizeof(rqst));
2732 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2733 memset(rsp_iov, 0, sizeof(rsp_iov));
2735 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2740 memset(&open_iov, 0, sizeof(open_iov));
2741 rqst[0].rq_iov = open_iov;
2742 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2744 memset(&oparms, 0, sizeof(oparms));
2746 oparms.desired_access = FILE_READ_ATTRIBUTES;
2747 oparms.disposition = FILE_OPEN;
2749 if (backup_cred(cifs_sb))
2750 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2752 oparms.create_options = 0;
2753 if (is_reparse_point)
2754 oparms.create_options = OPEN_REPARSE_POINT;
2757 oparms.reconnect = false;
2759 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2762 smb2_set_next_command(tcon, &rqst[0]);
2766 memset(&io_iov, 0, sizeof(io_iov));
2767 rqst[1].rq_iov = io_iov;
2768 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
2770 rc = SMB2_ioctl_init(tcon, &rqst[1], fid.persistent_fid,
2771 fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
2772 true /* is_fctl */, NULL, 0, CIFSMaxBufSize);
2776 smb2_set_next_command(tcon, &rqst[1]);
2777 smb2_set_related(&rqst[1]);
2781 memset(&close_iov, 0, sizeof(close_iov));
2782 rqst[2].rq_iov = close_iov;
2783 rqst[2].rq_nvec = 1;
2785 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2789 smb2_set_related(&rqst[2]);
2791 rc = compound_send_recv(xid, tcon->ses, flags, 3, rqst,
2792 resp_buftype, rsp_iov);
2794 create_rsp = rsp_iov[0].iov_base;
2795 if (create_rsp && create_rsp->sync_hdr.Status)
2796 err_iov = rsp_iov[0];
2797 ioctl_rsp = rsp_iov[1].iov_base;
2800 * Open was successful and we got an ioctl response.
2802 if ((rc == 0) && (is_reparse_point)) {
2803 /* See MS-FSCC 2.3.23 */
2805 reparse_buf = (struct reparse_data_buffer *)
2806 ((char *)ioctl_rsp +
2807 le32_to_cpu(ioctl_rsp->OutputOffset));
2808 plen = le32_to_cpu(ioctl_rsp->OutputCount);
2810 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
2811 rsp_iov[1].iov_len) {
2812 cifs_tcon_dbg(VFS, "srv returned invalid ioctl len: %d\n",
2818 rc = parse_reparse_point(reparse_buf, plen, target_path,
2823 if (!rc || !err_iov.iov_base) {
2828 err_buf = err_iov.iov_base;
2829 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
2830 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
2835 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
2836 if (le32_to_cpu(symlink->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
2837 le32_to_cpu(symlink->ReparseTag) != IO_REPARSE_TAG_SYMLINK) {
2842 /* open must fail on symlink - reset rc */
2844 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
2845 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
2846 print_len = le16_to_cpu(symlink->PrintNameLength);
2847 print_offset = le16_to_cpu(symlink->PrintNameOffset);
2849 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
2854 if (err_iov.iov_len <
2855 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
2860 *target_path = cifs_strndup_from_utf16(
2861 (char *)symlink->PathBuffer + sub_offset,
2862 sub_len, true, cifs_sb->local_nls);
2863 if (!(*target_path)) {
2867 convert_delimiter(*target_path, '/');
2868 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2871 cifs_dbg(FYI, "query symlink rc %d\n", rc);
2873 SMB2_open_free(&rqst[0]);
2874 SMB2_ioctl_free(&rqst[1]);
2875 SMB2_close_free(&rqst[2]);
2876 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2877 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2878 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2882 static struct cifs_ntsd *
2883 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
2884 const struct cifs_fid *cifsfid, u32 *pacllen)
2886 struct cifs_ntsd *pntsd = NULL;
2888 int rc = -EOPNOTSUPP;
2889 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2892 return ERR_CAST(tlink);
2895 cifs_dbg(FYI, "trying to get acl\n");
2897 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
2898 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
2901 cifs_put_tlink(tlink);
2903 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2910 static struct cifs_ntsd *
2911 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
2912 const char *path, u32 *pacllen)
2914 struct cifs_ntsd *pntsd = NULL;
2915 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2918 struct cifs_tcon *tcon;
2919 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2920 struct cifs_fid fid;
2921 struct cifs_open_parms oparms;
2924 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
2926 return ERR_CAST(tlink);
2928 tcon = tlink_tcon(tlink);
2931 if (backup_cred(cifs_sb))
2932 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2934 oparms.create_options = 0;
2936 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2944 oparms.desired_access = READ_CONTROL;
2945 oparms.disposition = FILE_OPEN;
2947 oparms.reconnect = false;
2949 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2952 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2953 fid.volatile_fid, (void **)&pntsd, pacllen);
2954 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2957 cifs_put_tlink(tlink);
2960 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2967 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
2968 struct inode *inode, const char *path, int aclflag)
2970 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2972 int rc, access_flags = 0;
2973 struct cifs_tcon *tcon;
2974 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2975 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2976 struct cifs_fid fid;
2977 struct cifs_open_parms oparms;
2980 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
2982 return PTR_ERR(tlink);
2984 tcon = tlink_tcon(tlink);
2987 if (backup_cred(cifs_sb))
2988 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2990 oparms.create_options = 0;
2992 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
2993 access_flags = WRITE_OWNER;
2995 access_flags = WRITE_DAC;
2997 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3005 oparms.desired_access = access_flags;
3006 oparms.disposition = FILE_OPEN;
3009 oparms.reconnect = false;
3011 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
3014 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3015 fid.volatile_fid, pnntsd, acllen, aclflag);
3016 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3019 cifs_put_tlink(tlink);
3024 /* Retrieve an ACL from the server */
3025 static struct cifs_ntsd *
3026 get_smb2_acl(struct cifs_sb_info *cifs_sb,
3027 struct inode *inode, const char *path,
3030 struct cifs_ntsd *pntsd = NULL;
3031 struct cifsFileInfo *open_file = NULL;
3034 open_file = find_readable_file(CIFS_I(inode), true);
3036 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
3038 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
3039 cifsFileInfo_put(open_file);
3043 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3044 loff_t offset, loff_t len, bool keep_size)
3046 struct cifs_ses *ses = tcon->ses;
3047 struct inode *inode;
3048 struct cifsInodeInfo *cifsi;
3049 struct cifsFileInfo *cfile = file->private_data;
3050 struct file_zero_data_information fsctl_buf;
3057 inode = d_inode(cfile->dentry);
3058 cifsi = CIFS_I(inode);
3060 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3061 ses->Suid, offset, len);
3064 /* if file not oplocked can't be sure whether asking to extend size */
3065 if (!CIFS_CACHE_READ(cifsi))
3066 if (keep_size == false) {
3068 trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
3069 tcon->tid, ses->Suid, offset, len, rc);
3074 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3076 fsctl_buf.FileOffset = cpu_to_le64(offset);
3077 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3079 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3080 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
3082 sizeof(struct file_zero_data_information),
3085 goto zero_range_exit;
3088 * do we also need to change the size of the file?
3090 if (keep_size == false && i_size_read(inode) < offset + len) {
3091 eof = cpu_to_le64(offset + len);
3092 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3093 cfile->fid.volatile_fid, cfile->pid, &eof);
3099 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3100 ses->Suid, offset, len, rc);
3102 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3103 ses->Suid, offset, len);
3107 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3108 loff_t offset, loff_t len)
3110 struct inode *inode;
3111 struct cifsFileInfo *cfile = file->private_data;
3112 struct file_zero_data_information fsctl_buf;
3115 __u8 set_sparse = 1;
3119 inode = d_inode(cfile->dentry);
3121 /* Need to make file sparse, if not already, before freeing range. */
3122 /* Consider adding equivalent for compressed since it could also work */
3123 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3129 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3131 fsctl_buf.FileOffset = cpu_to_le64(offset);
3132 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3134 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3135 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3136 true /* is_fctl */, (char *)&fsctl_buf,
3137 sizeof(struct file_zero_data_information),
3138 CIFSMaxBufSize, NULL, NULL);
3143 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3144 loff_t off, loff_t len, bool keep_size)
3146 struct inode *inode;
3147 struct cifsInodeInfo *cifsi;
3148 struct cifsFileInfo *cfile = file->private_data;
3149 long rc = -EOPNOTSUPP;
3155 inode = d_inode(cfile->dentry);
3156 cifsi = CIFS_I(inode);
3158 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3159 tcon->ses->Suid, off, len);
3160 /* if file not oplocked can't be sure whether asking to extend size */
3161 if (!CIFS_CACHE_READ(cifsi))
3162 if (keep_size == false) {
3163 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3164 tcon->tid, tcon->ses->Suid, off, len, rc);
3170 * Files are non-sparse by default so falloc may be a no-op
3171 * Must check if file sparse. If not sparse, and not extending
3172 * then no need to do anything since file already allocated
3174 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
3175 if (keep_size == true)
3177 /* check if extending file */
3178 else if (i_size_read(inode) >= off + len)
3179 /* not extending file and already not sparse */
3181 /* BB: in future add else clause to extend file */
3185 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3186 tcon->tid, tcon->ses->Suid, off, len, rc);
3188 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid,
3189 tcon->tid, tcon->ses->Suid, off, len);
3194 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3196 * Check if falloc starts within first few pages of file
3197 * and ends within a few pages of the end of file to
3198 * ensure that most of file is being forced to be
3199 * fallocated now. If so then setting whole file sparse
3200 * ie potentially making a few extra pages at the beginning
3201 * or end of the file non-sparse via set_sparse is harmless.
3203 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3205 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3206 tcon->tid, tcon->ses->Suid, off, len, rc);
3211 smb2_set_sparse(xid, tcon, cfile, inode, false);
3214 smb2_set_sparse(xid, tcon, cfile, inode, false);
3216 if (i_size_read(inode) < off + len) {
3217 eof = cpu_to_le64(off + len);
3218 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3219 cfile->fid.volatile_fid, cfile->pid,
3225 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3226 tcon->ses->Suid, off, len, rc);
3228 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3229 tcon->ses->Suid, off, len);
3235 static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3237 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3238 struct cifsInodeInfo *cifsi;
3239 struct inode *inode;
3241 struct file_allocated_range_buffer in_data, *out_data = NULL;
3245 if (whence != SEEK_HOLE && whence != SEEK_DATA)
3246 return generic_file_llseek(file, offset, whence);
3248 inode = d_inode(cfile->dentry);
3249 cifsi = CIFS_I(inode);
3251 if (offset < 0 || offset >= i_size_read(inode))
3256 * We need to be sure that all dirty pages are written as they
3257 * might fill holes on the server.
3258 * Note that we also MUST flush any written pages since at least
3259 * some servers (Windows2016) will not reflect recent writes in
3260 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3262 wrcfile = find_writable_file(cifsi, false);
3264 filemap_write_and_wait(inode->i_mapping);
3265 smb2_flush_file(xid, tcon, &wrcfile->fid);
3266 cifsFileInfo_put(wrcfile);
3269 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3270 if (whence == SEEK_HOLE)
3271 offset = i_size_read(inode);
3275 in_data.file_offset = cpu_to_le64(offset);
3276 in_data.length = cpu_to_le64(i_size_read(inode));
3278 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3279 cfile->fid.volatile_fid,
3280 FSCTL_QUERY_ALLOCATED_RANGES, true,
3281 (char *)&in_data, sizeof(in_data),
3282 sizeof(struct file_allocated_range_buffer),
3283 (char **)&out_data, &out_data_len);
3289 if (whence == SEEK_HOLE && out_data_len == 0)
3292 if (whence == SEEK_DATA && out_data_len == 0) {
3297 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3301 if (whence == SEEK_DATA) {
3302 offset = le64_to_cpu(out_data->file_offset);
3305 if (offset < le64_to_cpu(out_data->file_offset))
3308 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3314 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3319 static int smb3_fiemap(struct cifs_tcon *tcon,
3320 struct cifsFileInfo *cfile,
3321 struct fiemap_extent_info *fei, u64 start, u64 len)
3324 struct file_allocated_range_buffer in_data, *out_data;
3326 int i, num, rc, flags, last_blob;
3329 if (fiemap_check_flags(fei, FIEMAP_FLAG_SYNC))
3334 in_data.file_offset = cpu_to_le64(start);
3335 in_data.length = cpu_to_le64(len);
3337 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3338 cfile->fid.volatile_fid,
3339 FSCTL_QUERY_ALLOCATED_RANGES, true,
3340 (char *)&in_data, sizeof(in_data),
3341 1024 * sizeof(struct file_allocated_range_buffer),
3342 (char **)&out_data, &out_data_len);
3351 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3355 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3360 num = out_data_len / sizeof(struct file_allocated_range_buffer);
3361 for (i = 0; i < num; i++) {
3363 if (i == num - 1 && last_blob)
3364 flags |= FIEMAP_EXTENT_LAST;
3366 rc = fiemap_fill_next_extent(fei,
3367 le64_to_cpu(out_data[i].file_offset),
3368 le64_to_cpu(out_data[i].file_offset),
3369 le64_to_cpu(out_data[i].length),
3380 next = le64_to_cpu(out_data[num - 1].file_offset) +
3381 le64_to_cpu(out_data[num - 1].length);
3382 len = len - (next - start);
3393 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3394 loff_t off, loff_t len)
3396 /* KEEP_SIZE already checked for by do_fallocate */
3397 if (mode & FALLOC_FL_PUNCH_HOLE)
3398 return smb3_punch_hole(file, tcon, off, len);
3399 else if (mode & FALLOC_FL_ZERO_RANGE) {
3400 if (mode & FALLOC_FL_KEEP_SIZE)
3401 return smb3_zero_range(file, tcon, off, len, true);
3402 return smb3_zero_range(file, tcon, off, len, false);
3403 } else if (mode == FALLOC_FL_KEEP_SIZE)
3404 return smb3_simple_falloc(file, tcon, off, len, true);
3406 return smb3_simple_falloc(file, tcon, off, len, false);
3412 smb2_downgrade_oplock(struct TCP_Server_Info *server,
3413 struct cifsInodeInfo *cinode, __u32 oplock,
3414 unsigned int epoch, bool *purge_cache)
3416 server->ops->set_oplock_level(cinode, oplock, 0, NULL);
3420 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3421 unsigned int epoch, bool *purge_cache);
3424 smb3_downgrade_oplock(struct TCP_Server_Info *server,
3425 struct cifsInodeInfo *cinode, __u32 oplock,
3426 unsigned int epoch, bool *purge_cache)
3428 unsigned int old_state = cinode->oplock;
3429 unsigned int old_epoch = cinode->epoch;
3430 unsigned int new_state;
3432 if (epoch > old_epoch) {
3433 smb21_set_oplock_level(cinode, oplock, 0, NULL);
3434 cinode->epoch = epoch;
3437 new_state = cinode->oplock;
3438 *purge_cache = false;
3440 if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
3441 (new_state & CIFS_CACHE_READ_FLG) == 0)
3442 *purge_cache = true;
3443 else if (old_state == new_state && (epoch - old_epoch > 1))
3444 *purge_cache = true;
3448 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3449 unsigned int epoch, bool *purge_cache)
3452 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3454 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3455 cinode->oplock = CIFS_CACHE_RHW_FLG;
3456 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3457 &cinode->vfs_inode);
3458 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
3459 cinode->oplock = CIFS_CACHE_RW_FLG;
3460 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3461 &cinode->vfs_inode);
3462 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3463 cinode->oplock = CIFS_CACHE_READ_FLG;
3464 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3465 &cinode->vfs_inode);
3471 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3472 unsigned int epoch, bool *purge_cache)
3474 char message[5] = {0};
3475 unsigned int new_oplock = 0;
3478 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3481 /* Check if the server granted an oplock rather than a lease */
3482 if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3483 return smb2_set_oplock_level(cinode, oplock, epoch,
3486 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
3487 new_oplock |= CIFS_CACHE_READ_FLG;
3488 strcat(message, "R");
3490 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
3491 new_oplock |= CIFS_CACHE_HANDLE_FLG;
3492 strcat(message, "H");
3494 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
3495 new_oplock |= CIFS_CACHE_WRITE_FLG;
3496 strcat(message, "W");
3499 strncpy(message, "None", sizeof(message));
3501 cinode->oplock = new_oplock;
3502 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3503 &cinode->vfs_inode);
3507 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3508 unsigned int epoch, bool *purge_cache)
3510 unsigned int old_oplock = cinode->oplock;
3512 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3515 *purge_cache = false;
3516 if (old_oplock == CIFS_CACHE_READ_FLG) {
3517 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3518 (epoch - cinode->epoch > 0))
3519 *purge_cache = true;
3520 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3521 (epoch - cinode->epoch > 1))
3522 *purge_cache = true;
3523 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3524 (epoch - cinode->epoch > 1))
3525 *purge_cache = true;
3526 else if (cinode->oplock == 0 &&
3527 (epoch - cinode->epoch > 0))
3528 *purge_cache = true;
3529 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
3530 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3531 (epoch - cinode->epoch > 0))
3532 *purge_cache = true;
3533 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3534 (epoch - cinode->epoch > 1))
3535 *purge_cache = true;
3537 cinode->epoch = epoch;
3542 smb2_is_read_op(__u32 oplock)
3544 return oplock == SMB2_OPLOCK_LEVEL_II;
3548 smb21_is_read_op(__u32 oplock)
3550 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
3551 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
3555 map_oplock_to_lease(u8 oplock)
3557 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3558 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
3559 else if (oplock == SMB2_OPLOCK_LEVEL_II)
3560 return SMB2_LEASE_READ_CACHING;
3561 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3562 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
3563 SMB2_LEASE_WRITE_CACHING;
3568 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3570 struct create_lease *buf;
3572 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
3576 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3577 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3579 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3580 (struct create_lease, lcontext));
3581 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
3582 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3583 (struct create_lease, Name));
3584 buf->ccontext.NameLength = cpu_to_le16(4);
3585 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3594 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
3596 struct create_lease_v2 *buf;
3598 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
3602 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3603 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3605 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3606 (struct create_lease_v2, lcontext));
3607 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
3608 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3609 (struct create_lease_v2, Name));
3610 buf->ccontext.NameLength = cpu_to_le16(4);
3611 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3620 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3622 struct create_lease *lc = (struct create_lease *)buf;
3624 *epoch = 0; /* not used */
3625 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3626 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3627 return le32_to_cpu(lc->lcontext.LeaseState);
3631 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3633 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
3635 *epoch = le16_to_cpu(lc->lcontext.Epoch);
3636 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3637 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3639 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
3640 return le32_to_cpu(lc->lcontext.LeaseState);
3644 smb2_wp_retry_size(struct inode *inode)
3646 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
3647 SMB2_MAX_BUFFER_SIZE);
3651 smb2_dir_needs_close(struct cifsFileInfo *cfile)
3653 return !cfile->invalidHandle;
3657 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
3658 struct smb_rqst *old_rq, __le16 cipher_type)
3660 struct smb2_sync_hdr *shdr =
3661 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
3663 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
3664 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
3665 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
3666 tr_hdr->Flags = cpu_to_le16(0x01);
3667 if (cipher_type == SMB2_ENCRYPTION_AES128_GCM)
3668 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128GCM_NONCE);
3670 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CCM_NONCE);
3671 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
3674 /* We can not use the normal sg_set_buf() as we will sometimes pass a
3675 * stack object as buf.
3677 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
3678 unsigned int buflen)
3682 * VMAP_STACK (at least) puts stack into the vmalloc address space
3684 if (is_vmalloc_addr(buf))
3685 addr = vmalloc_to_page(buf);
3687 addr = virt_to_page(buf);
3688 sg_set_page(sg, addr, buflen, offset_in_page(buf));
3691 /* Assumes the first rqst has a transform header as the first iov.
3693 * rqst[0].rq_iov[0] is transform header
3694 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
3695 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
3697 static struct scatterlist *
3698 init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
3700 unsigned int sg_len;
3701 struct scatterlist *sg;
3704 unsigned int idx = 0;
3708 for (i = 0; i < num_rqst; i++)
3709 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
3711 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
3715 sg_init_table(sg, sg_len);
3716 for (i = 0; i < num_rqst; i++) {
3717 for (j = 0; j < rqst[i].rq_nvec; j++) {
3719 * The first rqst has a transform header where the
3720 * first 20 bytes are not part of the encrypted blob
3722 skip = (i == 0) && (j == 0) ? 20 : 0;
3723 smb2_sg_set_buf(&sg[idx++],
3724 rqst[i].rq_iov[j].iov_base + skip,
3725 rqst[i].rq_iov[j].iov_len - skip);
3728 for (j = 0; j < rqst[i].rq_npages; j++) {
3729 unsigned int len, offset;
3731 rqst_page_get_length(&rqst[i], j, &len, &offset);
3732 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
3735 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
3740 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
3742 struct cifs_ses *ses;
3745 spin_lock(&cifs_tcp_ses_lock);
3746 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
3747 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3748 if (ses->Suid == ses_id) {
3749 ses_enc_key = enc ? ses->smb3encryptionkey :
3750 ses->smb3decryptionkey;
3751 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
3752 spin_unlock(&cifs_tcp_ses_lock);
3757 spin_unlock(&cifs_tcp_ses_lock);
3762 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
3763 * iov[0] - transform header (associate data),
3764 * iov[1-N] - SMB2 header and pages - data to encrypt.
3765 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
3769 crypt_message(struct TCP_Server_Info *server, int num_rqst,
3770 struct smb_rqst *rqst, int enc)
3772 struct smb2_transform_hdr *tr_hdr =
3773 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
3774 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
3776 struct scatterlist *sg;
3777 u8 sign[SMB2_SIGNATURE_SIZE] = {};
3778 u8 key[SMB3_SIGN_KEY_SIZE];
3779 struct aead_request *req;
3781 unsigned int iv_len;
3782 DECLARE_CRYPTO_WAIT(wait);
3783 struct crypto_aead *tfm;
3784 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3786 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
3788 cifs_server_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
3793 rc = smb3_crypto_aead_allocate(server);
3795 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
3799 tfm = enc ? server->secmech.ccmaesencrypt :
3800 server->secmech.ccmaesdecrypt;
3801 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
3803 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
3807 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
3809 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
3813 req = aead_request_alloc(tfm, GFP_KERNEL);
3815 cifs_server_dbg(VFS, "%s: Failed to alloc aead request\n", __func__);
3820 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
3821 crypt_len += SMB2_SIGNATURE_SIZE;
3824 sg = init_sg(num_rqst, rqst, sign);
3826 cifs_server_dbg(VFS, "%s: Failed to init sg\n", __func__);
3831 iv_len = crypto_aead_ivsize(tfm);
3832 iv = kzalloc(iv_len, GFP_KERNEL);
3834 cifs_server_dbg(VFS, "%s: Failed to alloc iv\n", __func__);
3839 if (server->cipher_type == SMB2_ENCRYPTION_AES128_GCM)
3840 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES128GCM_NONCE);
3843 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CCM_NONCE);
3846 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
3847 aead_request_set_ad(req, assoc_data_len);
3849 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3850 crypto_req_done, &wait);
3852 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
3853 : crypto_aead_decrypt(req), &wait);
3856 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
3867 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
3871 for (i = 0; i < num_rqst; i++) {
3872 if (rqst[i].rq_pages) {
3873 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
3874 put_page(rqst[i].rq_pages[j]);
3875 kfree(rqst[i].rq_pages);
3881 * This function will initialize new_rq and encrypt the content.
3882 * The first entry, new_rq[0], only contains a single iov which contains
3883 * a smb2_transform_hdr and is pre-allocated by the caller.
3884 * This function then populates new_rq[1+] with the content from olq_rq[0+].
3886 * The end result is an array of smb_rqst structures where the first structure
3887 * only contains a single iov for the transform header which we then can pass
3888 * to crypt_message().
3890 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
3891 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
3894 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
3895 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
3897 struct page **pages;
3898 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
3899 unsigned int npages;
3900 unsigned int orig_len = 0;
3904 for (i = 1; i < num_rqst; i++) {
3905 npages = old_rq[i - 1].rq_npages;
3906 pages = kmalloc_array(npages, sizeof(struct page *),
3911 new_rq[i].rq_pages = pages;
3912 new_rq[i].rq_npages = npages;
3913 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
3914 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
3915 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
3916 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
3917 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
3919 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
3921 for (j = 0; j < npages; j++) {
3922 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3927 /* copy pages form the old */
3928 for (j = 0; j < npages; j++) {
3930 unsigned int offset, len;
3932 rqst_page_get_length(&new_rq[i], j, &len, &offset);
3934 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
3935 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
3937 memcpy(dst, src, len);
3938 kunmap(new_rq[i].rq_pages[j]);
3939 kunmap(old_rq[i - 1].rq_pages[j]);
3943 /* fill the 1st iov with a transform header */
3944 fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
3946 rc = crypt_message(server, num_rqst, new_rq, 1);
3947 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
3954 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
3959 smb3_is_transform_hdr(void *buf)
3961 struct smb2_transform_hdr *trhdr = buf;
3963 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
3967 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
3968 unsigned int buf_data_size, struct page **pages,
3969 unsigned int npages, unsigned int page_data_size)
3972 struct smb_rqst rqst = {NULL};
3975 iov[0].iov_base = buf;
3976 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
3977 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
3978 iov[1].iov_len = buf_data_size;
3982 rqst.rq_pages = pages;
3983 rqst.rq_npages = npages;
3984 rqst.rq_pagesz = PAGE_SIZE;
3985 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
3987 rc = crypt_message(server, 1, &rqst, 0);
3988 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
3993 memmove(buf, iov[1].iov_base, buf_data_size);
3995 server->total_read = buf_data_size + page_data_size;
4001 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
4002 unsigned int npages, unsigned int len)
4007 for (i = 0; i < npages; i++) {
4008 struct page *page = pages[i];
4012 if (len >= PAGE_SIZE) {
4013 /* enough data to fill the page */
4017 zero_user(page, len, PAGE_SIZE - len);
4020 length = cifs_read_page_from_socket(server, page, 0, n);
4023 server->total_read += length;
4030 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
4031 unsigned int cur_off, struct bio_vec **page_vec)
4033 struct bio_vec *bvec;
4036 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
4040 for (i = 0; i < npages; i++) {
4041 bvec[i].bv_page = pages[i];
4042 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
4043 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
4044 data_size -= bvec[i].bv_len;
4047 if (data_size != 0) {
4048 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4058 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4059 char *buf, unsigned int buf_len, struct page **pages,
4060 unsigned int npages, unsigned int page_data_size)
4062 unsigned int data_offset;
4063 unsigned int data_len;
4064 unsigned int cur_off;
4065 unsigned int cur_page_idx;
4066 unsigned int pad_len;
4067 struct cifs_readdata *rdata = mid->callback_data;
4068 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
4069 struct bio_vec *bvec = NULL;
4070 struct iov_iter iter;
4073 bool use_rdma_mr = false;
4075 if (shdr->Command != SMB2_READ) {
4076 cifs_server_dbg(VFS, "only big read responses are supported\n");
4080 if (server->ops->is_session_expired &&
4081 server->ops->is_session_expired(buf)) {
4082 cifs_reconnect(server);
4083 wake_up(&server->response_q);
4087 if (server->ops->is_status_pending &&
4088 server->ops->is_status_pending(buf, server))
4091 /* set up first two iov to get credits */
4092 rdata->iov[0].iov_base = buf;
4093 rdata->iov[0].iov_len = 0;
4094 rdata->iov[1].iov_base = buf;
4095 rdata->iov[1].iov_len =
4096 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
4097 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4098 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4099 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4100 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4102 rdata->result = server->ops->map_error(buf, true);
4103 if (rdata->result != 0) {
4104 cifs_dbg(FYI, "%s: server returned error %d\n",
4105 __func__, rdata->result);
4106 /* normal error on read response */
4107 dequeue_mid(mid, false);
4111 data_offset = server->ops->read_data_offset(buf);
4112 #ifdef CONFIG_CIFS_SMB_DIRECT
4113 use_rdma_mr = rdata->mr;
4115 data_len = server->ops->read_data_length(buf, use_rdma_mr);
4117 if (data_offset < server->vals->read_rsp_size) {
4119 * win2k8 sometimes sends an offset of 0 when the read
4120 * is beyond the EOF. Treat it as if the data starts just after
4123 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4124 __func__, data_offset);
4125 data_offset = server->vals->read_rsp_size;
4126 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4127 /* data_offset is beyond the end of smallbuf */
4128 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4129 __func__, data_offset);
4130 rdata->result = -EIO;
4131 dequeue_mid(mid, rdata->result);
4135 pad_len = data_offset - server->vals->read_rsp_size;
4137 if (buf_len <= data_offset) {
4138 /* read response payload is in pages */
4139 cur_page_idx = pad_len / PAGE_SIZE;
4140 cur_off = pad_len % PAGE_SIZE;
4142 if (cur_page_idx != 0) {
4143 /* data offset is beyond the 1st page of response */
4144 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4145 __func__, data_offset);
4146 rdata->result = -EIO;
4147 dequeue_mid(mid, rdata->result);
4151 if (data_len > page_data_size - pad_len) {
4152 /* data_len is corrupt -- discard frame */
4153 rdata->result = -EIO;
4154 dequeue_mid(mid, rdata->result);
4158 rdata->result = init_read_bvec(pages, npages, page_data_size,
4160 if (rdata->result != 0) {
4161 dequeue_mid(mid, rdata->result);
4165 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
4166 } else if (buf_len >= data_offset + data_len) {
4167 /* read response payload is in buf */
4168 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
4169 iov.iov_base = buf + data_offset;
4170 iov.iov_len = data_len;
4171 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
4173 /* read response payload cannot be in both buf and pages */
4174 WARN_ONCE(1, "buf can not contain only a part of read data");
4175 rdata->result = -EIO;
4176 dequeue_mid(mid, rdata->result);
4180 length = rdata->copy_into_pages(server, rdata, &iter);
4187 dequeue_mid(mid, false);
4191 struct smb2_decrypt_work {
4192 struct work_struct decrypt;
4193 struct TCP_Server_Info *server;
4194 struct page **ppages;
4196 unsigned int npages;
4201 static void smb2_decrypt_offload(struct work_struct *work)
4203 struct smb2_decrypt_work *dw = container_of(work,
4204 struct smb2_decrypt_work, decrypt);
4206 struct mid_q_entry *mid;
4208 rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
4209 dw->ppages, dw->npages, dw->len);
4211 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4215 dw->server->lstrp = jiffies;
4216 mid = smb2_find_mid(dw->server, dw->buf);
4218 cifs_dbg(FYI, "mid not found\n");
4220 mid->decrypted = true;
4221 rc = handle_read_data(dw->server, mid, dw->buf,
4222 dw->server->vals->read_rsp_size,
4223 dw->ppages, dw->npages, dw->len);
4225 cifs_mid_q_entry_release(mid);
4229 for (i = dw->npages-1; i >= 0; i--)
4230 put_page(dw->ppages[i]);
4233 cifs_small_buf_release(dw->buf);
4239 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4242 char *buf = server->smallbuf;
4243 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4244 unsigned int npages;
4245 struct page **pages;
4247 unsigned int buflen = server->pdu_size;
4250 struct smb2_decrypt_work *dw;
4253 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
4254 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
4256 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
4259 server->total_read += rc;
4261 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
4262 server->vals->read_rsp_size;
4263 npages = DIV_ROUND_UP(len, PAGE_SIZE);
4265 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
4271 for (; i < npages; i++) {
4272 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4279 /* read read data into pages */
4280 rc = read_data_into_pages(server, pages, npages, len);
4284 rc = cifs_discard_remaining_data(server);
4289 * For large reads, offload to different thread for better performance,
4290 * use more cores decrypting which can be expensive
4293 if ((server->min_offload) && (server->in_flight > 1) &&
4294 (server->pdu_size >= server->min_offload)) {
4295 dw = kmalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
4297 goto non_offloaded_decrypt;
4299 dw->buf = server->smallbuf;
4300 server->smallbuf = (char *)cifs_small_buf_get();
4302 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
4304 dw->npages = npages;
4305 dw->server = server;
4308 queue_work(decrypt_wq, &dw->decrypt);
4309 *num_mids = 0; /* worker thread takes care of finding mid */
4313 non_offloaded_decrypt:
4314 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
4315 pages, npages, len);
4319 *mid = smb2_find_mid(server, buf);
4321 cifs_dbg(FYI, "mid not found\n");
4323 cifs_dbg(FYI, "mid found\n");
4324 (*mid)->decrypted = true;
4325 rc = handle_read_data(server, *mid, buf,
4326 server->vals->read_rsp_size,
4327 pages, npages, len);
4331 for (i = i - 1; i >= 0; i--)
4336 cifs_discard_remaining_data(server);
4341 receive_encrypted_standard(struct TCP_Server_Info *server,
4342 struct mid_q_entry **mids, char **bufs,
4346 char *buf = server->smallbuf;
4347 struct smb2_sync_hdr *shdr;
4348 unsigned int pdu_length = server->pdu_size;
4349 unsigned int buf_size;
4350 struct mid_q_entry *mid_entry;
4352 char *next_buffer = NULL;
4356 /* switch to large buffer if too big for a small one */
4357 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
4358 server->large_buf = true;
4359 memcpy(server->bigbuf, buf, server->total_read);
4360 buf = server->bigbuf;
4363 /* now read the rest */
4364 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
4365 pdu_length - HEADER_SIZE(server) + 1);
4368 server->total_read += length;
4370 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
4371 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
4375 next_is_large = server->large_buf;
4377 shdr = (struct smb2_sync_hdr *)buf;
4378 if (shdr->NextCommand) {
4380 next_buffer = (char *)cifs_buf_get();
4382 next_buffer = (char *)cifs_small_buf_get();
4384 buf + le32_to_cpu(shdr->NextCommand),
4385 pdu_length - le32_to_cpu(shdr->NextCommand));
4388 mid_entry = smb2_find_mid(server, buf);
4389 if (mid_entry == NULL)
4390 cifs_dbg(FYI, "mid not found\n");
4392 cifs_dbg(FYI, "mid found\n");
4393 mid_entry->decrypted = true;
4394 mid_entry->resp_buf_size = server->pdu_size;
4397 if (*num_mids >= MAX_COMPOUND) {
4398 cifs_server_dbg(VFS, "too many PDUs in compound\n");
4401 bufs[*num_mids] = buf;
4402 mids[(*num_mids)++] = mid_entry;
4404 if (mid_entry && mid_entry->handle)
4405 ret = mid_entry->handle(server, mid_entry);
4407 ret = cifs_handle_standard(server, mid_entry);
4409 if (ret == 0 && shdr->NextCommand) {
4410 pdu_length -= le32_to_cpu(shdr->NextCommand);
4411 server->large_buf = next_is_large;
4413 server->bigbuf = buf = next_buffer;
4415 server->smallbuf = buf = next_buffer;
4417 } else if (ret != 0) {
4419 * ret != 0 here means that we didn't get to handle_mid() thus
4420 * server->smallbuf and server->bigbuf are still valid. We need
4421 * to free next_buffer because it is not going to be used
4425 free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
4427 free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
4434 smb3_receive_transform(struct TCP_Server_Info *server,
4435 struct mid_q_entry **mids, char **bufs, int *num_mids)
4437 char *buf = server->smallbuf;
4438 unsigned int pdu_length = server->pdu_size;
4439 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4440 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4442 if (pdu_length < sizeof(struct smb2_transform_hdr) +
4443 sizeof(struct smb2_sync_hdr)) {
4444 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
4446 cifs_reconnect(server);
4447 wake_up(&server->response_q);
4448 return -ECONNABORTED;
4451 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
4452 cifs_server_dbg(VFS, "Transform message is broken\n");
4453 cifs_reconnect(server);
4454 wake_up(&server->response_q);
4455 return -ECONNABORTED;
4458 /* TODO: add support for compounds containing READ. */
4459 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
4460 return receive_encrypted_read(server, &mids[0], num_mids);
4463 return receive_encrypted_standard(server, mids, bufs, num_mids);
4467 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
4469 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
4471 return handle_read_data(server, mid, buf, server->pdu_size,
4476 smb2_next_header(char *buf)
4478 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
4479 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
4481 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
4482 return sizeof(struct smb2_transform_hdr) +
4483 le32_to_cpu(t_hdr->OriginalMessageSize);
4485 return le32_to_cpu(hdr->NextCommand);
4489 smb2_make_node(unsigned int xid, struct inode *inode,
4490 struct dentry *dentry, struct cifs_tcon *tcon,
4491 char *full_path, umode_t mode, dev_t dev)
4493 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4495 int create_options = CREATE_NOT_DIR | CREATE_OPTION_SPECIAL;
4496 FILE_ALL_INFO *buf = NULL;
4497 struct cifs_io_parms io_parms;
4499 struct cifs_fid fid;
4500 struct cifs_open_parms oparms;
4501 unsigned int bytes_written;
4502 struct win_dev *pdev;
4506 * Check if mounted with mount parm 'sfu' mount parm.
4507 * SFU emulation should work with all servers, but only
4508 * supports block and char device (no socket & fifo),
4509 * and was used by default in earlier versions of Windows
4511 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
4515 * TODO: Add ability to create instead via reparse point. Windows (e.g.
4516 * their current NFS server) uses this approach to expose special files
4517 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
4520 if (!S_ISCHR(mode) && !S_ISBLK(mode))
4523 cifs_dbg(FYI, "sfu compat create special file\n");
4525 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
4531 if (backup_cred(cifs_sb))
4532 create_options |= CREATE_OPEN_BACKUP_INTENT;
4535 oparms.cifs_sb = cifs_sb;
4536 oparms.desired_access = GENERIC_WRITE;
4537 oparms.create_options = create_options;
4538 oparms.disposition = FILE_CREATE;
4539 oparms.path = full_path;
4541 oparms.reconnect = false;
4543 if (tcon->ses->server->oplocks)
4544 oplock = REQ_OPLOCK;
4547 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
4552 * BB Do not bother to decode buf since no local inode yet to put
4553 * timestamps in, but we can reuse it safely.
4556 pdev = (struct win_dev *)buf;
4557 io_parms.pid = current->tgid;
4558 io_parms.tcon = tcon;
4559 io_parms.offset = 0;
4560 io_parms.length = sizeof(struct win_dev);
4561 iov[1].iov_base = buf;
4562 iov[1].iov_len = sizeof(struct win_dev);
4563 if (S_ISCHR(mode)) {
4564 memcpy(pdev->type, "IntxCHR", 8);
4565 pdev->major = cpu_to_le64(MAJOR(dev));
4566 pdev->minor = cpu_to_le64(MINOR(dev));
4567 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4568 &bytes_written, iov, 1);
4569 } else if (S_ISBLK(mode)) {
4570 memcpy(pdev->type, "IntxBLK", 8);
4571 pdev->major = cpu_to_le64(MAJOR(dev));
4572 pdev->minor = cpu_to_le64(MINOR(dev));
4573 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4574 &bytes_written, iov, 1);
4576 tcon->ses->server->ops->close(xid, tcon, &fid);
4579 /* FIXME: add code here to set EAs */
4586 struct smb_version_operations smb20_operations = {
4587 .compare_fids = smb2_compare_fids,
4588 .setup_request = smb2_setup_request,
4589 .setup_async_request = smb2_setup_async_request,
4590 .check_receive = smb2_check_receive,
4591 .add_credits = smb2_add_credits,
4592 .set_credits = smb2_set_credits,
4593 .get_credits_field = smb2_get_credits_field,
4594 .get_credits = smb2_get_credits,
4595 .wait_mtu_credits = cifs_wait_mtu_credits,
4596 .get_next_mid = smb2_get_next_mid,
4597 .revert_current_mid = smb2_revert_current_mid,
4598 .read_data_offset = smb2_read_data_offset,
4599 .read_data_length = smb2_read_data_length,
4600 .map_error = map_smb2_to_linux_error,
4601 .find_mid = smb2_find_mid,
4602 .check_message = smb2_check_message,
4603 .dump_detail = smb2_dump_detail,
4604 .clear_stats = smb2_clear_stats,
4605 .print_stats = smb2_print_stats,
4606 .is_oplock_break = smb2_is_valid_oplock_break,
4607 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4608 .downgrade_oplock = smb2_downgrade_oplock,
4609 .need_neg = smb2_need_neg,
4610 .negotiate = smb2_negotiate,
4611 .negotiate_wsize = smb2_negotiate_wsize,
4612 .negotiate_rsize = smb2_negotiate_rsize,
4613 .sess_setup = SMB2_sess_setup,
4614 .logoff = SMB2_logoff,
4615 .tree_connect = SMB2_tcon,
4616 .tree_disconnect = SMB2_tdis,
4617 .qfs_tcon = smb2_qfs_tcon,
4618 .is_path_accessible = smb2_is_path_accessible,
4619 .can_echo = smb2_can_echo,
4621 .query_path_info = smb2_query_path_info,
4622 .get_srv_inum = smb2_get_srv_inum,
4623 .query_file_info = smb2_query_file_info,
4624 .set_path_size = smb2_set_path_size,
4625 .set_file_size = smb2_set_file_size,
4626 .set_file_info = smb2_set_file_info,
4627 .set_compression = smb2_set_compression,
4628 .mkdir = smb2_mkdir,
4629 .mkdir_setinfo = smb2_mkdir_setinfo,
4630 .rmdir = smb2_rmdir,
4631 .unlink = smb2_unlink,
4632 .rename = smb2_rename_path,
4633 .create_hardlink = smb2_create_hardlink,
4634 .query_symlink = smb2_query_symlink,
4635 .query_mf_symlink = smb3_query_mf_symlink,
4636 .create_mf_symlink = smb3_create_mf_symlink,
4637 .open = smb2_open_file,
4638 .set_fid = smb2_set_fid,
4639 .close = smb2_close_file,
4640 .flush = smb2_flush_file,
4641 .async_readv = smb2_async_readv,
4642 .async_writev = smb2_async_writev,
4643 .sync_read = smb2_sync_read,
4644 .sync_write = smb2_sync_write,
4645 .query_dir_first = smb2_query_dir_first,
4646 .query_dir_next = smb2_query_dir_next,
4647 .close_dir = smb2_close_dir,
4648 .calc_smb_size = smb2_calc_size,
4649 .is_status_pending = smb2_is_status_pending,
4650 .is_session_expired = smb2_is_session_expired,
4651 .oplock_response = smb2_oplock_response,
4652 .queryfs = smb2_queryfs,
4653 .mand_lock = smb2_mand_lock,
4654 .mand_unlock_range = smb2_unlock_range,
4655 .push_mand_locks = smb2_push_mandatory_locks,
4656 .get_lease_key = smb2_get_lease_key,
4657 .set_lease_key = smb2_set_lease_key,
4658 .new_lease_key = smb2_new_lease_key,
4659 .calc_signature = smb2_calc_signature,
4660 .is_read_op = smb2_is_read_op,
4661 .set_oplock_level = smb2_set_oplock_level,
4662 .create_lease_buf = smb2_create_lease_buf,
4663 .parse_lease_buf = smb2_parse_lease_buf,
4664 .copychunk_range = smb2_copychunk_range,
4665 .wp_retry_size = smb2_wp_retry_size,
4666 .dir_needs_close = smb2_dir_needs_close,
4667 .get_dfs_refer = smb2_get_dfs_refer,
4668 .select_sectype = smb2_select_sectype,
4669 #ifdef CONFIG_CIFS_XATTR
4670 .query_all_EAs = smb2_query_eas,
4671 .set_EA = smb2_set_ea,
4672 #endif /* CIFS_XATTR */
4673 .get_acl = get_smb2_acl,
4674 .get_acl_by_fid = get_smb2_acl_by_fid,
4675 .set_acl = set_smb2_acl,
4676 .next_header = smb2_next_header,
4677 .ioctl_query_info = smb2_ioctl_query_info,
4678 .make_node = smb2_make_node,
4679 .fiemap = smb3_fiemap,
4680 .llseek = smb3_llseek,
4683 struct smb_version_operations smb21_operations = {
4684 .compare_fids = smb2_compare_fids,
4685 .setup_request = smb2_setup_request,
4686 .setup_async_request = smb2_setup_async_request,
4687 .check_receive = smb2_check_receive,
4688 .add_credits = smb2_add_credits,
4689 .set_credits = smb2_set_credits,
4690 .get_credits_field = smb2_get_credits_field,
4691 .get_credits = smb2_get_credits,
4692 .wait_mtu_credits = smb2_wait_mtu_credits,
4693 .adjust_credits = smb2_adjust_credits,
4694 .get_next_mid = smb2_get_next_mid,
4695 .revert_current_mid = smb2_revert_current_mid,
4696 .read_data_offset = smb2_read_data_offset,
4697 .read_data_length = smb2_read_data_length,
4698 .map_error = map_smb2_to_linux_error,
4699 .find_mid = smb2_find_mid,
4700 .check_message = smb2_check_message,
4701 .dump_detail = smb2_dump_detail,
4702 .clear_stats = smb2_clear_stats,
4703 .print_stats = smb2_print_stats,
4704 .is_oplock_break = smb2_is_valid_oplock_break,
4705 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4706 .downgrade_oplock = smb2_downgrade_oplock,
4707 .need_neg = smb2_need_neg,
4708 .negotiate = smb2_negotiate,
4709 .negotiate_wsize = smb2_negotiate_wsize,
4710 .negotiate_rsize = smb2_negotiate_rsize,
4711 .sess_setup = SMB2_sess_setup,
4712 .logoff = SMB2_logoff,
4713 .tree_connect = SMB2_tcon,
4714 .tree_disconnect = SMB2_tdis,
4715 .qfs_tcon = smb2_qfs_tcon,
4716 .is_path_accessible = smb2_is_path_accessible,
4717 .can_echo = smb2_can_echo,
4719 .query_path_info = smb2_query_path_info,
4720 .get_srv_inum = smb2_get_srv_inum,
4721 .query_file_info = smb2_query_file_info,
4722 .set_path_size = smb2_set_path_size,
4723 .set_file_size = smb2_set_file_size,
4724 .set_file_info = smb2_set_file_info,
4725 .set_compression = smb2_set_compression,
4726 .mkdir = smb2_mkdir,
4727 .mkdir_setinfo = smb2_mkdir_setinfo,
4728 .rmdir = smb2_rmdir,
4729 .unlink = smb2_unlink,
4730 .rename = smb2_rename_path,
4731 .create_hardlink = smb2_create_hardlink,
4732 .query_symlink = smb2_query_symlink,
4733 .query_mf_symlink = smb3_query_mf_symlink,
4734 .create_mf_symlink = smb3_create_mf_symlink,
4735 .open = smb2_open_file,
4736 .set_fid = smb2_set_fid,
4737 .close = smb2_close_file,
4738 .flush = smb2_flush_file,
4739 .async_readv = smb2_async_readv,
4740 .async_writev = smb2_async_writev,
4741 .sync_read = smb2_sync_read,
4742 .sync_write = smb2_sync_write,
4743 .query_dir_first = smb2_query_dir_first,
4744 .query_dir_next = smb2_query_dir_next,
4745 .close_dir = smb2_close_dir,
4746 .calc_smb_size = smb2_calc_size,
4747 .is_status_pending = smb2_is_status_pending,
4748 .is_session_expired = smb2_is_session_expired,
4749 .oplock_response = smb2_oplock_response,
4750 .queryfs = smb2_queryfs,
4751 .mand_lock = smb2_mand_lock,
4752 .mand_unlock_range = smb2_unlock_range,
4753 .push_mand_locks = smb2_push_mandatory_locks,
4754 .get_lease_key = smb2_get_lease_key,
4755 .set_lease_key = smb2_set_lease_key,
4756 .new_lease_key = smb2_new_lease_key,
4757 .calc_signature = smb2_calc_signature,
4758 .is_read_op = smb21_is_read_op,
4759 .set_oplock_level = smb21_set_oplock_level,
4760 .create_lease_buf = smb2_create_lease_buf,
4761 .parse_lease_buf = smb2_parse_lease_buf,
4762 .copychunk_range = smb2_copychunk_range,
4763 .wp_retry_size = smb2_wp_retry_size,
4764 .dir_needs_close = smb2_dir_needs_close,
4765 .enum_snapshots = smb3_enum_snapshots,
4766 .get_dfs_refer = smb2_get_dfs_refer,
4767 .select_sectype = smb2_select_sectype,
4768 #ifdef CONFIG_CIFS_XATTR
4769 .query_all_EAs = smb2_query_eas,
4770 .set_EA = smb2_set_ea,
4771 #endif /* CIFS_XATTR */
4772 .get_acl = get_smb2_acl,
4773 .get_acl_by_fid = get_smb2_acl_by_fid,
4774 .set_acl = set_smb2_acl,
4775 .next_header = smb2_next_header,
4776 .ioctl_query_info = smb2_ioctl_query_info,
4777 .make_node = smb2_make_node,
4778 .fiemap = smb3_fiemap,
4779 .llseek = smb3_llseek,
4782 struct smb_version_operations smb30_operations = {
4783 .compare_fids = smb2_compare_fids,
4784 .setup_request = smb2_setup_request,
4785 .setup_async_request = smb2_setup_async_request,
4786 .check_receive = smb2_check_receive,
4787 .add_credits = smb2_add_credits,
4788 .set_credits = smb2_set_credits,
4789 .get_credits_field = smb2_get_credits_field,
4790 .get_credits = smb2_get_credits,
4791 .wait_mtu_credits = smb2_wait_mtu_credits,
4792 .adjust_credits = smb2_adjust_credits,
4793 .get_next_mid = smb2_get_next_mid,
4794 .revert_current_mid = smb2_revert_current_mid,
4795 .read_data_offset = smb2_read_data_offset,
4796 .read_data_length = smb2_read_data_length,
4797 .map_error = map_smb2_to_linux_error,
4798 .find_mid = smb2_find_mid,
4799 .check_message = smb2_check_message,
4800 .dump_detail = smb2_dump_detail,
4801 .clear_stats = smb2_clear_stats,
4802 .print_stats = smb2_print_stats,
4803 .dump_share_caps = smb2_dump_share_caps,
4804 .is_oplock_break = smb2_is_valid_oplock_break,
4805 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4806 .downgrade_oplock = smb3_downgrade_oplock,
4807 .need_neg = smb2_need_neg,
4808 .negotiate = smb2_negotiate,
4809 .negotiate_wsize = smb3_negotiate_wsize,
4810 .negotiate_rsize = smb3_negotiate_rsize,
4811 .sess_setup = SMB2_sess_setup,
4812 .logoff = SMB2_logoff,
4813 .tree_connect = SMB2_tcon,
4814 .tree_disconnect = SMB2_tdis,
4815 .qfs_tcon = smb3_qfs_tcon,
4816 .is_path_accessible = smb2_is_path_accessible,
4817 .can_echo = smb2_can_echo,
4819 .query_path_info = smb2_query_path_info,
4820 .get_srv_inum = smb2_get_srv_inum,
4821 .query_file_info = smb2_query_file_info,
4822 .set_path_size = smb2_set_path_size,
4823 .set_file_size = smb2_set_file_size,
4824 .set_file_info = smb2_set_file_info,
4825 .set_compression = smb2_set_compression,
4826 .mkdir = smb2_mkdir,
4827 .mkdir_setinfo = smb2_mkdir_setinfo,
4828 .rmdir = smb2_rmdir,
4829 .unlink = smb2_unlink,
4830 .rename = smb2_rename_path,
4831 .create_hardlink = smb2_create_hardlink,
4832 .query_symlink = smb2_query_symlink,
4833 .query_mf_symlink = smb3_query_mf_symlink,
4834 .create_mf_symlink = smb3_create_mf_symlink,
4835 .open = smb2_open_file,
4836 .set_fid = smb2_set_fid,
4837 .close = smb2_close_file,
4838 .close_getattr = smb2_close_getattr,
4839 .flush = smb2_flush_file,
4840 .async_readv = smb2_async_readv,
4841 .async_writev = smb2_async_writev,
4842 .sync_read = smb2_sync_read,
4843 .sync_write = smb2_sync_write,
4844 .query_dir_first = smb2_query_dir_first,
4845 .query_dir_next = smb2_query_dir_next,
4846 .close_dir = smb2_close_dir,
4847 .calc_smb_size = smb2_calc_size,
4848 .is_status_pending = smb2_is_status_pending,
4849 .is_session_expired = smb2_is_session_expired,
4850 .oplock_response = smb2_oplock_response,
4851 .queryfs = smb2_queryfs,
4852 .mand_lock = smb2_mand_lock,
4853 .mand_unlock_range = smb2_unlock_range,
4854 .push_mand_locks = smb2_push_mandatory_locks,
4855 .get_lease_key = smb2_get_lease_key,
4856 .set_lease_key = smb2_set_lease_key,
4857 .new_lease_key = smb2_new_lease_key,
4858 .generate_signingkey = generate_smb30signingkey,
4859 .calc_signature = smb3_calc_signature,
4860 .set_integrity = smb3_set_integrity,
4861 .is_read_op = smb21_is_read_op,
4862 .set_oplock_level = smb3_set_oplock_level,
4863 .create_lease_buf = smb3_create_lease_buf,
4864 .parse_lease_buf = smb3_parse_lease_buf,
4865 .copychunk_range = smb2_copychunk_range,
4866 .duplicate_extents = smb2_duplicate_extents,
4867 .validate_negotiate = smb3_validate_negotiate,
4868 .wp_retry_size = smb2_wp_retry_size,
4869 .dir_needs_close = smb2_dir_needs_close,
4870 .fallocate = smb3_fallocate,
4871 .enum_snapshots = smb3_enum_snapshots,
4872 .init_transform_rq = smb3_init_transform_rq,
4873 .is_transform_hdr = smb3_is_transform_hdr,
4874 .receive_transform = smb3_receive_transform,
4875 .get_dfs_refer = smb2_get_dfs_refer,
4876 .select_sectype = smb2_select_sectype,
4877 #ifdef CONFIG_CIFS_XATTR
4878 .query_all_EAs = smb2_query_eas,
4879 .set_EA = smb2_set_ea,
4880 #endif /* CIFS_XATTR */
4881 .get_acl = get_smb2_acl,
4882 .get_acl_by_fid = get_smb2_acl_by_fid,
4883 .set_acl = set_smb2_acl,
4884 .next_header = smb2_next_header,
4885 .ioctl_query_info = smb2_ioctl_query_info,
4886 .make_node = smb2_make_node,
4887 .fiemap = smb3_fiemap,
4888 .llseek = smb3_llseek,
4891 struct smb_version_operations smb311_operations = {
4892 .compare_fids = smb2_compare_fids,
4893 .setup_request = smb2_setup_request,
4894 .setup_async_request = smb2_setup_async_request,
4895 .check_receive = smb2_check_receive,
4896 .add_credits = smb2_add_credits,
4897 .set_credits = smb2_set_credits,
4898 .get_credits_field = smb2_get_credits_field,
4899 .get_credits = smb2_get_credits,
4900 .wait_mtu_credits = smb2_wait_mtu_credits,
4901 .adjust_credits = smb2_adjust_credits,
4902 .get_next_mid = smb2_get_next_mid,
4903 .revert_current_mid = smb2_revert_current_mid,
4904 .read_data_offset = smb2_read_data_offset,
4905 .read_data_length = smb2_read_data_length,
4906 .map_error = map_smb2_to_linux_error,
4907 .find_mid = smb2_find_mid,
4908 .check_message = smb2_check_message,
4909 .dump_detail = smb2_dump_detail,
4910 .clear_stats = smb2_clear_stats,
4911 .print_stats = smb2_print_stats,
4912 .dump_share_caps = smb2_dump_share_caps,
4913 .is_oplock_break = smb2_is_valid_oplock_break,
4914 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4915 .downgrade_oplock = smb3_downgrade_oplock,
4916 .need_neg = smb2_need_neg,
4917 .negotiate = smb2_negotiate,
4918 .negotiate_wsize = smb3_negotiate_wsize,
4919 .negotiate_rsize = smb3_negotiate_rsize,
4920 .sess_setup = SMB2_sess_setup,
4921 .logoff = SMB2_logoff,
4922 .tree_connect = SMB2_tcon,
4923 .tree_disconnect = SMB2_tdis,
4924 .qfs_tcon = smb3_qfs_tcon,
4925 .is_path_accessible = smb2_is_path_accessible,
4926 .can_echo = smb2_can_echo,
4928 .query_path_info = smb2_query_path_info,
4929 .get_srv_inum = smb2_get_srv_inum,
4930 .query_file_info = smb2_query_file_info,
4931 .set_path_size = smb2_set_path_size,
4932 .set_file_size = smb2_set_file_size,
4933 .set_file_info = smb2_set_file_info,
4934 .set_compression = smb2_set_compression,
4935 .mkdir = smb2_mkdir,
4936 .mkdir_setinfo = smb2_mkdir_setinfo,
4937 .posix_mkdir = smb311_posix_mkdir,
4938 .rmdir = smb2_rmdir,
4939 .unlink = smb2_unlink,
4940 .rename = smb2_rename_path,
4941 .create_hardlink = smb2_create_hardlink,
4942 .query_symlink = smb2_query_symlink,
4943 .query_mf_symlink = smb3_query_mf_symlink,
4944 .create_mf_symlink = smb3_create_mf_symlink,
4945 .open = smb2_open_file,
4946 .set_fid = smb2_set_fid,
4947 .close = smb2_close_file,
4948 .close_getattr = smb2_close_getattr,
4949 .flush = smb2_flush_file,
4950 .async_readv = smb2_async_readv,
4951 .async_writev = smb2_async_writev,
4952 .sync_read = smb2_sync_read,
4953 .sync_write = smb2_sync_write,
4954 .query_dir_first = smb2_query_dir_first,
4955 .query_dir_next = smb2_query_dir_next,
4956 .close_dir = smb2_close_dir,
4957 .calc_smb_size = smb2_calc_size,
4958 .is_status_pending = smb2_is_status_pending,
4959 .is_session_expired = smb2_is_session_expired,
4960 .oplock_response = smb2_oplock_response,
4961 .queryfs = smb311_queryfs,
4962 .mand_lock = smb2_mand_lock,
4963 .mand_unlock_range = smb2_unlock_range,
4964 .push_mand_locks = smb2_push_mandatory_locks,
4965 .get_lease_key = smb2_get_lease_key,
4966 .set_lease_key = smb2_set_lease_key,
4967 .new_lease_key = smb2_new_lease_key,
4968 .generate_signingkey = generate_smb311signingkey,
4969 .calc_signature = smb3_calc_signature,
4970 .set_integrity = smb3_set_integrity,
4971 .is_read_op = smb21_is_read_op,
4972 .set_oplock_level = smb3_set_oplock_level,
4973 .create_lease_buf = smb3_create_lease_buf,
4974 .parse_lease_buf = smb3_parse_lease_buf,
4975 .copychunk_range = smb2_copychunk_range,
4976 .duplicate_extents = smb2_duplicate_extents,
4977 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
4978 .wp_retry_size = smb2_wp_retry_size,
4979 .dir_needs_close = smb2_dir_needs_close,
4980 .fallocate = smb3_fallocate,
4981 .enum_snapshots = smb3_enum_snapshots,
4982 .init_transform_rq = smb3_init_transform_rq,
4983 .is_transform_hdr = smb3_is_transform_hdr,
4984 .receive_transform = smb3_receive_transform,
4985 .get_dfs_refer = smb2_get_dfs_refer,
4986 .select_sectype = smb2_select_sectype,
4987 #ifdef CONFIG_CIFS_XATTR
4988 .query_all_EAs = smb2_query_eas,
4989 .set_EA = smb2_set_ea,
4990 #endif /* CIFS_XATTR */
4991 .get_acl = get_smb2_acl,
4992 .get_acl_by_fid = get_smb2_acl_by_fid,
4993 .set_acl = set_smb2_acl,
4994 .next_header = smb2_next_header,
4995 .ioctl_query_info = smb2_ioctl_query_info,
4996 .make_node = smb2_make_node,
4997 .fiemap = smb3_fiemap,
4998 .llseek = smb3_llseek,
5001 struct smb_version_values smb20_values = {
5002 .version_string = SMB20_VERSION_STRING,
5003 .protocol_id = SMB20_PROT_ID,
5004 .req_capabilities = 0, /* MBZ */
5005 .large_lock_type = 0,
5006 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5007 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5008 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5009 .header_size = sizeof(struct smb2_sync_hdr),
5010 .header_preamble_size = 0,
5011 .max_header_size = MAX_SMB2_HDR_SIZE,
5012 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5013 .lock_cmd = SMB2_LOCK,
5015 .cap_nt_find = SMB2_NT_FIND,
5016 .cap_large_files = SMB2_LARGE_FILES,
5017 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5018 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5019 .create_lease_size = sizeof(struct create_lease),
5022 struct smb_version_values smb21_values = {
5023 .version_string = SMB21_VERSION_STRING,
5024 .protocol_id = SMB21_PROT_ID,
5025 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5026 .large_lock_type = 0,
5027 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5028 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5029 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5030 .header_size = sizeof(struct smb2_sync_hdr),
5031 .header_preamble_size = 0,
5032 .max_header_size = MAX_SMB2_HDR_SIZE,
5033 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5034 .lock_cmd = SMB2_LOCK,
5036 .cap_nt_find = SMB2_NT_FIND,
5037 .cap_large_files = SMB2_LARGE_FILES,
5038 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5039 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5040 .create_lease_size = sizeof(struct create_lease),
5043 struct smb_version_values smb3any_values = {
5044 .version_string = SMB3ANY_VERSION_STRING,
5045 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5046 .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,
5047 .large_lock_type = 0,
5048 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5049 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5050 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5051 .header_size = sizeof(struct smb2_sync_hdr),
5052 .header_preamble_size = 0,
5053 .max_header_size = MAX_SMB2_HDR_SIZE,
5054 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5055 .lock_cmd = SMB2_LOCK,
5057 .cap_nt_find = SMB2_NT_FIND,
5058 .cap_large_files = SMB2_LARGE_FILES,
5059 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5060 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5061 .create_lease_size = sizeof(struct create_lease_v2),
5064 struct smb_version_values smbdefault_values = {
5065 .version_string = SMBDEFAULT_VERSION_STRING,
5066 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5067 .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,
5068 .large_lock_type = 0,
5069 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5070 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5071 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5072 .header_size = sizeof(struct smb2_sync_hdr),
5073 .header_preamble_size = 0,
5074 .max_header_size = MAX_SMB2_HDR_SIZE,
5075 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5076 .lock_cmd = SMB2_LOCK,
5078 .cap_nt_find = SMB2_NT_FIND,
5079 .cap_large_files = SMB2_LARGE_FILES,
5080 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5081 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5082 .create_lease_size = sizeof(struct create_lease_v2),
5085 struct smb_version_values smb30_values = {
5086 .version_string = SMB30_VERSION_STRING,
5087 .protocol_id = SMB30_PROT_ID,
5088 .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,
5089 .large_lock_type = 0,
5090 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5091 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5092 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5093 .header_size = sizeof(struct smb2_sync_hdr),
5094 .header_preamble_size = 0,
5095 .max_header_size = MAX_SMB2_HDR_SIZE,
5096 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5097 .lock_cmd = SMB2_LOCK,
5099 .cap_nt_find = SMB2_NT_FIND,
5100 .cap_large_files = SMB2_LARGE_FILES,
5101 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5102 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5103 .create_lease_size = sizeof(struct create_lease_v2),
5106 struct smb_version_values smb302_values = {
5107 .version_string = SMB302_VERSION_STRING,
5108 .protocol_id = SMB302_PROT_ID,
5109 .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,
5110 .large_lock_type = 0,
5111 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5112 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5113 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5114 .header_size = sizeof(struct smb2_sync_hdr),
5115 .header_preamble_size = 0,
5116 .max_header_size = MAX_SMB2_HDR_SIZE,
5117 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5118 .lock_cmd = SMB2_LOCK,
5120 .cap_nt_find = SMB2_NT_FIND,
5121 .cap_large_files = SMB2_LARGE_FILES,
5122 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5123 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5124 .create_lease_size = sizeof(struct create_lease_v2),
5127 struct smb_version_values smb311_values = {
5128 .version_string = SMB311_VERSION_STRING,
5129 .protocol_id = SMB311_PROT_ID,
5130 .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,
5131 .large_lock_type = 0,
5132 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5133 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5134 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5135 .header_size = sizeof(struct smb2_sync_hdr),
5136 .header_preamble_size = 0,
5137 .max_header_size = MAX_SMB2_HDR_SIZE,
5138 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5139 .lock_cmd = SMB2_LOCK,
5141 .cap_nt_find = SMB2_NT_FIND,
5142 .cap_large_files = SMB2_LARGE_FILES,
5143 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5144 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5145 .create_lease_size = sizeof(struct create_lease_v2),