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 <crypto/aead.h>
16 #include "smb2proto.h"
17 #include "cifsproto.h"
18 #include "cifs_debug.h"
19 #include "cifs_unicode.h"
20 #include "smb2status.h"
22 #include "cifs_ioctl.h"
23 #include "smbdirect.h"
25 /* Change credits for different ops and return the total number of credits */
27 change_conf(struct TCP_Server_Info *server)
29 server->credits += server->echo_credits + server->oplock_credits;
30 server->oplock_credits = server->echo_credits = 0;
31 switch (server->credits) {
35 server->echoes = false;
36 server->oplocks = false;
39 server->echoes = true;
40 server->oplocks = false;
41 server->echo_credits = 1;
44 server->echoes = true;
46 server->oplocks = true;
47 server->oplock_credits = 1;
49 server->oplocks = false;
51 server->echo_credits = 1;
53 server->credits -= server->echo_credits + server->oplock_credits;
54 return server->credits + server->echo_credits + server->oplock_credits;
58 smb2_add_credits(struct TCP_Server_Info *server,
59 const struct cifs_credits *credits, const int optype)
62 unsigned int add = credits->value;
63 unsigned int instance = credits->instance;
64 bool reconnect_detected = false;
66 spin_lock(&server->req_lock);
67 val = server->ops->get_credits_field(server, optype);
69 /* eg found case where write overlapping reconnect messed up credits */
70 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
71 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
72 server->hostname, *val);
73 if ((instance == 0) || (instance == server->reconnect_instance))
76 reconnect_detected = true;
79 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
80 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
82 WARN_ON_ONCE(server->in_flight == 0);
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, bool dequeue)
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);
281 list_del_init(&mid->qhead);
282 mid->mid_flags |= MID_DELETED;
284 spin_unlock(&GlobalMid_Lock);
288 spin_unlock(&GlobalMid_Lock);
292 static struct mid_q_entry *
293 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
295 return __smb2_find_mid(server, buf, false);
298 static struct mid_q_entry *
299 smb2_find_dequeue_mid(struct TCP_Server_Info *server, char *buf)
301 return __smb2_find_mid(server, buf, true);
305 smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
307 #ifdef CONFIG_CIFS_DEBUG2
308 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
310 cifs_server_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
311 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
313 cifs_server_dbg(VFS, "smb buf %p len %u\n", buf,
314 server->ops->calc_smb_size(buf, server));
319 smb2_need_neg(struct TCP_Server_Info *server)
321 return server->max_read == 0;
325 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
329 ses->server->CurrentMid = 0;
330 rc = SMB2_negotiate(xid, ses);
331 /* BB we probably don't need to retry with modern servers */
338 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
340 struct TCP_Server_Info *server = tcon->ses->server;
343 /* start with specified wsize, or default */
344 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
345 wsize = min_t(unsigned int, wsize, server->max_write);
346 #ifdef CONFIG_CIFS_SMB_DIRECT
349 wsize = min_t(unsigned int,
350 wsize, server->smbd_conn->max_fragmented_send_size);
352 wsize = min_t(unsigned int,
353 wsize, server->smbd_conn->max_readwrite_size);
356 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
357 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
363 smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
365 struct TCP_Server_Info *server = tcon->ses->server;
368 /* start with specified wsize, or default */
369 wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
370 wsize = min_t(unsigned int, wsize, server->max_write);
371 #ifdef CONFIG_CIFS_SMB_DIRECT
374 wsize = min_t(unsigned int,
375 wsize, server->smbd_conn->max_fragmented_send_size);
377 wsize = min_t(unsigned int,
378 wsize, server->smbd_conn->max_readwrite_size);
381 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
382 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
388 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
390 struct TCP_Server_Info *server = tcon->ses->server;
393 /* start with specified rsize, or default */
394 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
395 rsize = min_t(unsigned int, rsize, server->max_read);
396 #ifdef CONFIG_CIFS_SMB_DIRECT
399 rsize = min_t(unsigned int,
400 rsize, server->smbd_conn->max_fragmented_recv_size);
402 rsize = min_t(unsigned int,
403 rsize, server->smbd_conn->max_readwrite_size);
407 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
408 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
414 smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
416 struct TCP_Server_Info *server = tcon->ses->server;
419 /* start with specified rsize, or default */
420 rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
421 rsize = min_t(unsigned int, rsize, server->max_read);
422 #ifdef CONFIG_CIFS_SMB_DIRECT
425 rsize = min_t(unsigned int,
426 rsize, server->smbd_conn->max_fragmented_recv_size);
428 rsize = min_t(unsigned int,
429 rsize, server->smbd_conn->max_readwrite_size);
433 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
434 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
440 parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
442 struct cifs_server_iface **iface_list,
445 struct network_interface_info_ioctl_rsp *p;
446 struct sockaddr_in *addr4;
447 struct sockaddr_in6 *addr6;
448 struct iface_info_ipv4 *p4;
449 struct iface_info_ipv6 *p6;
450 struct cifs_server_iface *info;
460 * Fist pass: count and sanity check
463 bytes_left = buf_len;
465 while (bytes_left >= sizeof(*p)) {
467 next = le32_to_cpu(p->Next);
469 bytes_left -= sizeof(*p);
472 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
477 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
482 /* Azure rounds the buffer size up 8, to a 16 byte boundary */
483 if ((bytes_left > 8) || p->Next)
484 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
488 * Second pass: extract info to internal structure
491 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
498 bytes_left = buf_len;
500 while (bytes_left >= sizeof(*p)) {
501 info->speed = le64_to_cpu(p->LinkSpeed);
502 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE) ? 1 : 0;
503 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE) ? 1 : 0;
505 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
506 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
507 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
508 le32_to_cpu(p->Capability));
512 * The kernel and wire socket structures have the same
513 * layout and use network byte order but make the
514 * conversion explicit in case either one changes.
517 addr4 = (struct sockaddr_in *)&info->sockaddr;
518 p4 = (struct iface_info_ipv4 *)p->Buffer;
519 addr4->sin_family = AF_INET;
520 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
522 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
523 addr4->sin_port = cpu_to_be16(CIFS_PORT);
525 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
529 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
530 p6 = (struct iface_info_ipv6 *)p->Buffer;
531 addr6->sin6_family = AF_INET6;
532 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
534 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
535 addr6->sin6_flowinfo = 0;
536 addr6->sin6_scope_id = 0;
537 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
539 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
544 "%s: skipping unsupported socket family\n",
552 next = le32_to_cpu(p->Next);
555 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
575 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
578 unsigned int ret_data_len = 0;
579 struct network_interface_info_ioctl_rsp *out_buf = NULL;
580 struct cifs_server_iface *iface_list;
582 struct cifs_ses *ses = tcon->ses;
584 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
585 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
586 NULL /* no data input */, 0 /* no data input */,
587 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
588 if (rc == -EOPNOTSUPP) {
590 "server does not support query network interfaces\n");
592 } else if (rc != 0) {
593 cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
597 rc = parse_server_interfaces(out_buf, ret_data_len,
598 &iface_list, &iface_count);
602 spin_lock(&ses->iface_lock);
603 kfree(ses->iface_list);
604 ses->iface_list = iface_list;
605 ses->iface_count = iface_count;
606 ses->iface_last_update = jiffies;
607 spin_unlock(&ses->iface_lock);
615 smb2_close_cached_fid(struct kref *ref)
617 struct cached_fid *cfid = container_of(ref, struct cached_fid,
620 if (cfid->is_valid) {
621 cifs_dbg(FYI, "clear cached root file handle\n");
622 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
623 cfid->fid->volatile_fid);
624 cfid->is_valid = false;
625 cfid->file_all_info_is_valid = false;
629 void close_shroot(struct cached_fid *cfid)
631 mutex_lock(&cfid->fid_mutex);
632 kref_put(&cfid->refcount, smb2_close_cached_fid);
633 mutex_unlock(&cfid->fid_mutex);
637 smb2_cached_lease_break(struct work_struct *work)
639 struct cached_fid *cfid = container_of(work,
640 struct cached_fid, lease_break);
646 * Open the directory at the root of a share
648 int open_shroot(unsigned int xid, struct cifs_tcon *tcon,
649 struct cifs_sb_info *cifs_sb, struct cifs_fid *pfid)
651 struct cifs_ses *ses = tcon->ses;
652 struct TCP_Server_Info *server = ses->server;
653 struct cifs_open_parms oparms;
654 struct smb2_create_rsp *o_rsp = NULL;
655 struct smb2_query_info_rsp *qi_rsp = NULL;
657 struct smb_rqst rqst[2];
658 struct kvec rsp_iov[2];
659 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
660 struct kvec qi_iov[1];
662 __le16 utf16_path = 0; /* Null - since an open of top of share */
663 u8 oplock = SMB2_OPLOCK_LEVEL_II;
665 mutex_lock(&tcon->crfid.fid_mutex);
666 if (tcon->crfid.is_valid) {
667 cifs_dbg(FYI, "found a cached root file handle\n");
668 memcpy(pfid, tcon->crfid.fid, sizeof(struct cifs_fid));
669 kref_get(&tcon->crfid.refcount);
670 mutex_unlock(&tcon->crfid.fid_mutex);
675 * We do not hold the lock for the open because in case
676 * SMB2_open needs to reconnect, it will end up calling
677 * cifs_mark_open_files_invalid() which takes the lock again
678 * thus causing a deadlock
681 mutex_unlock(&tcon->crfid.fid_mutex);
683 if (smb3_encryption_required(tcon))
684 flags |= CIFS_TRANSFORM_REQ;
686 if (!server->ops->new_lease_key)
689 server->ops->new_lease_key(pfid);
691 memset(rqst, 0, sizeof(rqst));
692 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
693 memset(rsp_iov, 0, sizeof(rsp_iov));
696 memset(&open_iov, 0, sizeof(open_iov));
697 rqst[0].rq_iov = open_iov;
698 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
701 oparms.create_options = cifs_create_options(cifs_sb, 0);
702 oparms.desired_access = FILE_READ_ATTRIBUTES;
703 oparms.disposition = FILE_OPEN;
705 oparms.reconnect = false;
707 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, &utf16_path);
710 smb2_set_next_command(tcon, &rqst[0]);
712 memset(&qi_iov, 0, sizeof(qi_iov));
713 rqst[1].rq_iov = qi_iov;
716 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
717 COMPOUND_FID, FILE_ALL_INFORMATION,
719 sizeof(struct smb2_file_all_info) +
720 PATH_MAX * 2, 0, NULL);
724 smb2_set_related(&rqst[1]);
726 rc = compound_send_recv(xid, ses, flags, 2, rqst,
727 resp_buftype, rsp_iov);
728 mutex_lock(&tcon->crfid.fid_mutex);
731 * Now we need to check again as the cached root might have
732 * been successfully re-opened from a concurrent process
735 if (tcon->crfid.is_valid) {
736 /* work was already done */
738 /* stash fids for close() later */
739 struct cifs_fid fid = {
740 .persistent_fid = pfid->persistent_fid,
741 .volatile_fid = pfid->volatile_fid,
745 * caller expects this func to set pfid to a valid
746 * cached root, so we copy the existing one and get a
749 memcpy(pfid, tcon->crfid.fid, sizeof(*pfid));
750 kref_get(&tcon->crfid.refcount);
752 mutex_unlock(&tcon->crfid.fid_mutex);
755 /* close extra handle outside of crit sec */
756 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
762 /* Cached root is still invalid, continue normaly */
765 if (rc == -EREMCHG) {
766 tcon->need_reconnect = true;
767 printk_once(KERN_WARNING "server share %s deleted\n",
773 atomic_inc(&tcon->num_remote_opens);
775 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
776 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
777 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
778 #ifdef CONFIG_CIFS_DEBUG2
779 oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
780 #endif /* CIFS_DEBUG2 */
782 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
783 tcon->crfid.tcon = tcon;
784 tcon->crfid.is_valid = true;
785 kref_init(&tcon->crfid.refcount);
787 /* BB TBD check to see if oplock level check can be removed below */
788 if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
789 kref_get(&tcon->crfid.refcount);
790 smb2_parse_contexts(server, o_rsp,
792 oparms.fid->lease_key, &oplock, NULL);
796 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
797 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
799 if (!smb2_validate_and_copy_iov(
800 le16_to_cpu(qi_rsp->OutputBufferOffset),
801 sizeof(struct smb2_file_all_info),
802 &rsp_iov[1], sizeof(struct smb2_file_all_info),
803 (char *)&tcon->crfid.file_all_info))
804 tcon->crfid.file_all_info_is_valid = 1;
807 mutex_unlock(&tcon->crfid.fid_mutex);
809 SMB2_open_free(&rqst[0]);
810 SMB2_query_info_free(&rqst[1]);
811 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
812 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
817 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
818 struct cifs_sb_info *cifs_sb)
821 __le16 srch_path = 0; /* Null - open root of share */
822 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
823 struct cifs_open_parms oparms;
825 bool no_cached_open = tcon->nohandlecache;
827 oparms = (struct cifs_open_parms) {
829 .desired_access = FILE_READ_ATTRIBUTES,
830 .disposition = FILE_OPEN,
831 .create_options = cifs_create_options(cifs_sb, 0),
836 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
839 rc = open_shroot(xid, tcon, cifs_sb, &fid);
844 SMB3_request_interfaces(xid, tcon);
846 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
847 FS_ATTRIBUTE_INFORMATION);
848 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
849 FS_DEVICE_INFORMATION);
850 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
851 FS_VOLUME_INFORMATION);
852 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
853 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
855 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
857 close_shroot(&tcon->crfid);
861 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
862 struct cifs_sb_info *cifs_sb)
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 = cifs_create_options(cifs_sb, 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 oparms.create_options = cifs_create_options(cifs_sb, 0);
910 oparms.reconnect = false;
912 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
918 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
924 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
925 struct cifs_sb_info *cifs_sb, const char *full_path,
926 u64 *uniqueid, FILE_ALL_INFO *data)
928 *uniqueid = le64_to_cpu(data->IndexNumber);
933 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
934 struct cifs_fid *fid, FILE_ALL_INFO *data)
937 struct smb2_file_all_info *smb2_data;
939 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
941 if (smb2_data == NULL)
944 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
947 move_smb2_info_to_cifs(data, smb2_data);
952 #ifdef CONFIG_CIFS_XATTR
954 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
955 struct smb2_file_full_ea_info *src, size_t src_size,
956 const unsigned char *ea_name)
959 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
961 size_t buf_size = dst_size;
962 size_t name_len, value_len, user_name_len;
964 while (src_size > 0) {
965 name_len = (size_t)src->ea_name_length;
966 value_len = (size_t)le16_to_cpu(src->ea_value_length);
971 if (src_size < 8 + name_len + 1 + value_len) {
972 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
977 name = &src->ea_data[0];
978 value = &src->ea_data[src->ea_name_length + 1];
981 if (ea_name_len == name_len &&
982 memcmp(ea_name, name, name_len) == 0) {
986 if (dst_size < value_len) {
990 memcpy(dst, value, value_len);
994 /* 'user.' plus a terminating null */
995 user_name_len = 5 + 1 + name_len;
998 /* skip copy - calc size only */
1000 } else if (dst_size >= user_name_len) {
1001 dst_size -= user_name_len;
1002 memcpy(dst, "user.", 5);
1004 memcpy(dst, src->ea_data, name_len);
1008 rc += user_name_len;
1010 /* stop before overrun buffer */
1016 if (!src->next_entry_offset)
1019 if (src_size < le32_to_cpu(src->next_entry_offset)) {
1020 /* stop before overrun buffer */
1024 src_size -= le32_to_cpu(src->next_entry_offset);
1025 src = (void *)((char *)src +
1026 le32_to_cpu(src->next_entry_offset));
1029 /* didn't find the named attribute */
1038 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1039 const unsigned char *path, const unsigned char *ea_name,
1040 char *ea_data, size_t buf_size,
1041 struct cifs_sb_info *cifs_sb)
1045 struct kvec rsp_iov = {NULL, 0};
1046 int buftype = CIFS_NO_BUFFER;
1047 struct smb2_query_info_rsp *rsp;
1048 struct smb2_file_full_ea_info *info = NULL;
1050 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1054 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1056 FILE_FULL_EA_INFORMATION,
1059 MAX_SMB2_CREATE_RESPONSE_SIZE -
1060 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1061 &rsp_iov, &buftype, cifs_sb);
1064 * If ea_name is NULL (listxattr) and there are no EAs,
1065 * return 0 as it's not an error. Otherwise, the specified
1066 * ea_name was not found.
1068 if (!ea_name && rc == -ENODATA)
1073 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1074 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1075 le32_to_cpu(rsp->OutputBufferLength),
1077 sizeof(struct smb2_file_full_ea_info));
1081 info = (struct smb2_file_full_ea_info *)(
1082 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1083 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1084 le32_to_cpu(rsp->OutputBufferLength), ea_name);
1088 free_rsp_buf(buftype, rsp_iov.iov_base);
1094 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1095 const char *path, const char *ea_name, const void *ea_value,
1096 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1097 struct cifs_sb_info *cifs_sb)
1099 struct cifs_ses *ses = tcon->ses;
1100 __le16 *utf16_path = NULL;
1101 int ea_name_len = strlen(ea_name);
1104 struct smb_rqst rqst[3];
1105 int resp_buftype[3];
1106 struct kvec rsp_iov[3];
1107 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1108 struct cifs_open_parms oparms;
1109 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1110 struct cifs_fid fid;
1111 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1112 unsigned int size[1];
1114 struct smb2_file_full_ea_info *ea = NULL;
1115 struct kvec close_iov[1];
1116 struct smb2_query_info_rsp *rsp;
1117 int rc, used_len = 0;
1119 if (smb3_encryption_required(tcon))
1120 flags |= CIFS_TRANSFORM_REQ;
1122 if (ea_name_len > 255)
1125 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1129 memset(rqst, 0, sizeof(rqst));
1130 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1131 memset(rsp_iov, 0, sizeof(rsp_iov));
1133 if (ses->server->ops->query_all_EAs) {
1135 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1141 /* If we are adding a attribute we should first check
1142 * if there will be enough space available to store
1143 * the new EA. If not we should not add it since we
1144 * would not be able to even read the EAs back.
1146 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1148 FILE_FULL_EA_INFORMATION,
1151 MAX_SMB2_CREATE_RESPONSE_SIZE -
1152 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1153 &rsp_iov[1], &resp_buftype[1], cifs_sb);
1155 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1156 used_len = le32_to_cpu(rsp->OutputBufferLength);
1158 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1159 resp_buftype[1] = CIFS_NO_BUFFER;
1160 memset(&rsp_iov[1], 0, sizeof(rsp_iov[1]));
1163 /* Use a fudge factor of 256 bytes in case we collide
1164 * with a different set_EAs command.
1166 if(CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1167 MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
1168 used_len + ea_name_len + ea_value_len + 1) {
1176 memset(&open_iov, 0, sizeof(open_iov));
1177 rqst[0].rq_iov = open_iov;
1178 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1180 memset(&oparms, 0, sizeof(oparms));
1182 oparms.desired_access = FILE_WRITE_EA;
1183 oparms.disposition = FILE_OPEN;
1184 oparms.create_options = cifs_create_options(cifs_sb, 0);
1186 oparms.reconnect = false;
1188 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
1191 smb2_set_next_command(tcon, &rqst[0]);
1195 memset(&si_iov, 0, sizeof(si_iov));
1196 rqst[1].rq_iov = si_iov;
1197 rqst[1].rq_nvec = 1;
1199 len = sizeof(*ea) + ea_name_len + ea_value_len + 1;
1200 ea = kzalloc(len, GFP_KERNEL);
1206 ea->ea_name_length = ea_name_len;
1207 ea->ea_value_length = cpu_to_le16(ea_value_len);
1208 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1209 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1214 rc = SMB2_set_info_init(tcon, &rqst[1], COMPOUND_FID,
1215 COMPOUND_FID, current->tgid,
1216 FILE_FULL_EA_INFORMATION,
1217 SMB2_O_INFO_FILE, 0, data, size);
1220 smb2_set_next_command(tcon, &rqst[1]);
1221 smb2_set_related(&rqst[1]);
1225 memset(&close_iov, 0, sizeof(close_iov));
1226 rqst[2].rq_iov = close_iov;
1227 rqst[2].rq_nvec = 1;
1228 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1229 smb2_set_related(&rqst[2]);
1231 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1232 resp_buftype, rsp_iov);
1233 /* no need to bump num_remote_opens because handle immediately closed */
1238 SMB2_open_free(&rqst[0]);
1239 SMB2_set_info_free(&rqst[1]);
1240 SMB2_close_free(&rqst[2]);
1241 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1242 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1243 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1249 smb2_can_echo(struct TCP_Server_Info *server)
1251 return server->echoes;
1255 smb2_clear_stats(struct cifs_tcon *tcon)
1259 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1260 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1261 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1266 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1268 seq_puts(m, "\n\tShare Capabilities:");
1269 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1270 seq_puts(m, " DFS,");
1271 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1272 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1273 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1274 seq_puts(m, " SCALEOUT,");
1275 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1276 seq_puts(m, " CLUSTER,");
1277 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1278 seq_puts(m, " ASYMMETRIC,");
1279 if (tcon->capabilities == 0)
1280 seq_puts(m, " None");
1281 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1282 seq_puts(m, " Aligned,");
1283 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1284 seq_puts(m, " Partition Aligned,");
1285 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1286 seq_puts(m, " SSD,");
1287 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1288 seq_puts(m, " TRIM-support,");
1290 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1291 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1292 if (tcon->perf_sector_size)
1293 seq_printf(m, "\tOptimal sector size: 0x%x",
1294 tcon->perf_sector_size);
1295 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1299 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1301 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1302 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1305 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1306 * totals (requests sent) since those SMBs are per-session not per tcon
1308 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1309 (long long)(tcon->bytes_read),
1310 (long long)(tcon->bytes_written));
1311 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1312 atomic_read(&tcon->num_local_opens),
1313 atomic_read(&tcon->num_remote_opens));
1314 seq_printf(m, "\nTreeConnects: %d total %d failed",
1315 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1316 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1317 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1318 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1319 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1320 seq_printf(m, "\nCreates: %d total %d failed",
1321 atomic_read(&sent[SMB2_CREATE_HE]),
1322 atomic_read(&failed[SMB2_CREATE_HE]));
1323 seq_printf(m, "\nCloses: %d total %d failed",
1324 atomic_read(&sent[SMB2_CLOSE_HE]),
1325 atomic_read(&failed[SMB2_CLOSE_HE]));
1326 seq_printf(m, "\nFlushes: %d total %d failed",
1327 atomic_read(&sent[SMB2_FLUSH_HE]),
1328 atomic_read(&failed[SMB2_FLUSH_HE]));
1329 seq_printf(m, "\nReads: %d total %d failed",
1330 atomic_read(&sent[SMB2_READ_HE]),
1331 atomic_read(&failed[SMB2_READ_HE]));
1332 seq_printf(m, "\nWrites: %d total %d failed",
1333 atomic_read(&sent[SMB2_WRITE_HE]),
1334 atomic_read(&failed[SMB2_WRITE_HE]));
1335 seq_printf(m, "\nLocks: %d total %d failed",
1336 atomic_read(&sent[SMB2_LOCK_HE]),
1337 atomic_read(&failed[SMB2_LOCK_HE]));
1338 seq_printf(m, "\nIOCTLs: %d total %d failed",
1339 atomic_read(&sent[SMB2_IOCTL_HE]),
1340 atomic_read(&failed[SMB2_IOCTL_HE]));
1341 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1342 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1343 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1344 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1345 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1346 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1347 seq_printf(m, "\nQueryInfos: %d total %d failed",
1348 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1349 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1350 seq_printf(m, "\nSetInfos: %d total %d failed",
1351 atomic_read(&sent[SMB2_SET_INFO_HE]),
1352 atomic_read(&failed[SMB2_SET_INFO_HE]));
1353 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1354 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1355 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1359 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1361 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1362 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1364 cfile->fid.persistent_fid = fid->persistent_fid;
1365 cfile->fid.volatile_fid = fid->volatile_fid;
1366 cfile->fid.access = fid->access;
1367 #ifdef CONFIG_CIFS_DEBUG2
1368 cfile->fid.mid = fid->mid;
1369 #endif /* CIFS_DEBUG2 */
1370 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1372 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1373 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1377 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1378 struct cifs_fid *fid)
1380 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1384 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1385 u64 persistent_fid, u64 volatile_fid,
1386 struct copychunk_ioctl *pcchunk)
1389 unsigned int ret_data_len;
1390 struct resume_key_req *res_key;
1392 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1393 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1394 NULL, 0 /* no input */, CIFSMaxBufSize,
1395 (char **)&res_key, &ret_data_len);
1398 cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1399 goto req_res_key_exit;
1401 if (ret_data_len < sizeof(struct resume_key_req)) {
1402 cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
1404 goto req_res_key_exit;
1406 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1414 smb2_ioctl_query_info(const unsigned int xid,
1415 struct cifs_tcon *tcon,
1416 struct cifs_sb_info *cifs_sb,
1417 __le16 *path, int is_dir,
1420 struct cifs_ses *ses = tcon->ses;
1421 char __user *arg = (char __user *)p;
1422 struct smb_query_info qi;
1423 struct smb_query_info __user *pqi;
1426 struct smb2_query_info_rsp *qi_rsp = NULL;
1427 struct smb2_ioctl_rsp *io_rsp = NULL;
1428 void *buffer = NULL;
1429 struct smb_rqst rqst[3];
1430 int resp_buftype[3];
1431 struct kvec rsp_iov[3];
1432 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1433 struct cifs_open_parms oparms;
1434 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1435 struct cifs_fid fid;
1436 struct kvec qi_iov[1];
1437 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1438 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1439 struct kvec close_iov[1];
1440 unsigned int size[2];
1442 int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
1444 memset(rqst, 0, sizeof(rqst));
1445 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1446 memset(rsp_iov, 0, sizeof(rsp_iov));
1448 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1451 if (qi.output_buffer_length > 1024)
1454 if (!ses || !(ses->server))
1457 if (smb3_encryption_required(tcon))
1458 flags |= CIFS_TRANSFORM_REQ;
1460 buffer = kmalloc(qi.output_buffer_length, GFP_KERNEL);
1464 if (copy_from_user(buffer, arg + sizeof(struct smb_query_info),
1465 qi.output_buffer_length)) {
1471 memset(&open_iov, 0, sizeof(open_iov));
1472 rqst[0].rq_iov = open_iov;
1473 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1475 memset(&oparms, 0, sizeof(oparms));
1477 oparms.disposition = FILE_OPEN;
1478 oparms.create_options = cifs_create_options(cifs_sb, create_options);
1480 oparms.reconnect = false;
1482 if (qi.flags & PASSTHRU_FSCTL) {
1483 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1484 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1485 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1487 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1488 oparms.desired_access = GENERIC_ALL;
1490 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1491 oparms.desired_access = GENERIC_READ;
1493 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1494 oparms.desired_access = GENERIC_WRITE;
1497 } else if (qi.flags & PASSTHRU_SET_INFO) {
1498 oparms.desired_access = GENERIC_WRITE;
1500 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1503 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, path);
1506 smb2_set_next_command(tcon, &rqst[0]);
1509 if (qi.flags & PASSTHRU_FSCTL) {
1510 /* Can eventually relax perm check since server enforces too */
1511 if (!capable(CAP_SYS_ADMIN))
1514 memset(&io_iov, 0, sizeof(io_iov));
1515 rqst[1].rq_iov = io_iov;
1516 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1518 rc = SMB2_ioctl_init(tcon, &rqst[1],
1519 COMPOUND_FID, COMPOUND_FID,
1520 qi.info_type, true, buffer,
1521 qi.output_buffer_length,
1523 MAX_SMB2_CREATE_RESPONSE_SIZE -
1524 MAX_SMB2_CLOSE_RESPONSE_SIZE);
1526 } else if (qi.flags == PASSTHRU_SET_INFO) {
1527 /* Can eventually relax perm check since server enforces too */
1528 if (!capable(CAP_SYS_ADMIN))
1531 memset(&si_iov, 0, sizeof(si_iov));
1532 rqst[1].rq_iov = si_iov;
1533 rqst[1].rq_nvec = 1;
1538 rc = SMB2_set_info_init(tcon, &rqst[1],
1539 COMPOUND_FID, COMPOUND_FID,
1541 FILE_END_OF_FILE_INFORMATION,
1542 SMB2_O_INFO_FILE, 0, data, size);
1544 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1545 memset(&qi_iov, 0, sizeof(qi_iov));
1546 rqst[1].rq_iov = qi_iov;
1547 rqst[1].rq_nvec = 1;
1549 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
1550 COMPOUND_FID, qi.file_info_class,
1551 qi.info_type, qi.additional_information,
1552 qi.input_buffer_length,
1553 qi.output_buffer_length, buffer);
1554 } else { /* unknown flags */
1555 cifs_tcon_dbg(VFS, "invalid passthru query flags: 0x%x\n", qi.flags);
1561 smb2_set_next_command(tcon, &rqst[1]);
1562 smb2_set_related(&rqst[1]);
1565 memset(&close_iov, 0, sizeof(close_iov));
1566 rqst[2].rq_iov = close_iov;
1567 rqst[2].rq_nvec = 1;
1569 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1572 smb2_set_related(&rqst[2]);
1574 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1575 resp_buftype, rsp_iov);
1579 /* No need to bump num_remote_opens since handle immediately closed */
1580 if (qi.flags & PASSTHRU_FSCTL) {
1581 pqi = (struct smb_query_info __user *)arg;
1582 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1583 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1584 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1585 if (qi.input_buffer_length > 0 &&
1586 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1587 > rsp_iov[1].iov_len)
1590 if (copy_to_user(&pqi->input_buffer_length,
1591 &qi.input_buffer_length,
1592 sizeof(qi.input_buffer_length)))
1595 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1596 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1597 qi.input_buffer_length))
1600 pqi = (struct smb_query_info __user *)arg;
1601 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1602 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1603 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1604 if (copy_to_user(&pqi->input_buffer_length,
1605 &qi.input_buffer_length,
1606 sizeof(qi.input_buffer_length)))
1609 if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1610 qi.input_buffer_length))
1616 SMB2_open_free(&rqst[0]);
1617 if (qi.flags & PASSTHRU_FSCTL)
1618 SMB2_ioctl_free(&rqst[1]);
1620 SMB2_query_info_free(&rqst[1]);
1622 SMB2_close_free(&rqst[2]);
1623 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1624 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1625 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1634 smb2_copychunk_range(const unsigned int xid,
1635 struct cifsFileInfo *srcfile,
1636 struct cifsFileInfo *trgtfile, u64 src_off,
1637 u64 len, u64 dest_off)
1640 unsigned int ret_data_len;
1641 struct copychunk_ioctl *pcchunk;
1642 struct copychunk_ioctl_rsp *retbuf = NULL;
1643 struct cifs_tcon *tcon;
1644 int chunks_copied = 0;
1645 bool chunk_sizes_updated = false;
1646 ssize_t bytes_written, total_bytes_written = 0;
1647 struct inode *inode;
1649 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1652 * We need to flush all unwritten data before we can send the
1653 * copychunk ioctl to the server.
1655 inode = d_inode(trgtfile->dentry);
1656 filemap_write_and_wait(inode->i_mapping);
1658 if (pcchunk == NULL)
1661 cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
1662 /* Request a key from the server to identify the source of the copy */
1663 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1664 srcfile->fid.persistent_fid,
1665 srcfile->fid.volatile_fid, pcchunk);
1667 /* Note: request_res_key sets res_key null only if rc !=0 */
1671 /* For now array only one chunk long, will make more flexible later */
1672 pcchunk->ChunkCount = cpu_to_le32(1);
1673 pcchunk->Reserved = 0;
1674 pcchunk->Reserved2 = 0;
1676 tcon = tlink_tcon(trgtfile->tlink);
1679 pcchunk->SourceOffset = cpu_to_le64(src_off);
1680 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1682 cpu_to_le32(min_t(u64, len, tcon->max_bytes_chunk));
1684 /* Request server copy to target from src identified by key */
1687 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1688 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1689 true /* is_fsctl */, (char *)pcchunk,
1690 sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1691 (char **)&retbuf, &ret_data_len);
1694 sizeof(struct copychunk_ioctl_rsp)) {
1695 cifs_tcon_dbg(VFS, "invalid cchunk response size\n");
1699 if (retbuf->TotalBytesWritten == 0) {
1700 cifs_dbg(FYI, "no bytes copied\n");
1705 * Check if server claimed to write more than we asked
1707 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1708 le32_to_cpu(pcchunk->Length)) {
1709 cifs_tcon_dbg(VFS, "invalid copy chunk response\n");
1713 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1714 cifs_tcon_dbg(VFS, "invalid num chunks written\n");
1720 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1721 src_off += bytes_written;
1722 dest_off += bytes_written;
1723 len -= bytes_written;
1724 total_bytes_written += bytes_written;
1726 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1727 le32_to_cpu(retbuf->ChunksWritten),
1728 le32_to_cpu(retbuf->ChunkBytesWritten),
1730 } else if (rc == -EINVAL) {
1731 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1734 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1735 le32_to_cpu(retbuf->ChunksWritten),
1736 le32_to_cpu(retbuf->ChunkBytesWritten),
1737 le32_to_cpu(retbuf->TotalBytesWritten));
1740 * Check if this is the first request using these sizes,
1741 * (ie check if copy succeed once with original sizes
1742 * and check if the server gave us different sizes after
1743 * we already updated max sizes on previous request).
1744 * if not then why is the server returning an error now
1746 if ((chunks_copied != 0) || chunk_sizes_updated)
1749 /* Check that server is not asking us to grow size */
1750 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1751 tcon->max_bytes_chunk)
1752 tcon->max_bytes_chunk =
1753 le32_to_cpu(retbuf->ChunkBytesWritten);
1755 goto cchunk_out; /* server gave us bogus size */
1757 /* No need to change MaxChunks since already set to 1 */
1758 chunk_sizes_updated = true;
1769 return total_bytes_written;
1773 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1774 struct cifs_fid *fid)
1776 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1780 smb2_read_data_offset(char *buf)
1782 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1784 return rsp->DataOffset;
1788 smb2_read_data_length(char *buf, bool in_remaining)
1790 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1793 return le32_to_cpu(rsp->DataRemaining);
1795 return le32_to_cpu(rsp->DataLength);
1800 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1801 struct cifs_io_parms *parms, unsigned int *bytes_read,
1802 char **buf, int *buf_type)
1804 parms->persistent_fid = pfid->persistent_fid;
1805 parms->volatile_fid = pfid->volatile_fid;
1806 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1810 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1811 struct cifs_io_parms *parms, unsigned int *written,
1812 struct kvec *iov, unsigned long nr_segs)
1815 parms->persistent_fid = pfid->persistent_fid;
1816 parms->volatile_fid = pfid->volatile_fid;
1817 return SMB2_write(xid, parms, written, iov, nr_segs);
1820 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1821 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1822 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1824 struct cifsInodeInfo *cifsi;
1827 cifsi = CIFS_I(inode);
1829 /* if file already sparse don't bother setting sparse again */
1830 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1831 return true; /* already sparse */
1833 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1834 return true; /* already not sparse */
1837 * Can't check for sparse support on share the usual way via the
1838 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1839 * since Samba server doesn't set the flag on the share, yet
1840 * supports the set sparse FSCTL and returns sparse correctly
1841 * in the file attributes. If we fail setting sparse though we
1842 * mark that server does not support sparse files for this share
1843 * to avoid repeatedly sending the unsupported fsctl to server
1844 * if the file is repeatedly extended.
1846 if (tcon->broken_sparse_sup)
1849 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1850 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1852 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
1854 tcon->broken_sparse_sup = true;
1855 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1860 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1862 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1868 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1869 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1871 __le64 eof = cpu_to_le64(size);
1872 struct inode *inode;
1875 * If extending file more than one page make sparse. Many Linux fs
1876 * make files sparse by default when extending via ftruncate
1878 inode = d_inode(cfile->dentry);
1880 if (!set_alloc && (size > inode->i_size + 8192)) {
1881 __u8 set_sparse = 1;
1883 /* whether set sparse succeeds or not, extend the file */
1884 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1887 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1888 cfile->fid.volatile_fid, cfile->pid, &eof);
1892 smb2_duplicate_extents(const unsigned int xid,
1893 struct cifsFileInfo *srcfile,
1894 struct cifsFileInfo *trgtfile, u64 src_off,
1895 u64 len, u64 dest_off)
1898 unsigned int ret_data_len;
1899 struct duplicate_extents_to_file dup_ext_buf;
1900 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1902 /* server fileays advertise duplicate extent support with this flag */
1903 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1904 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1907 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1908 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1909 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1910 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1911 dup_ext_buf.ByteCount = cpu_to_le64(len);
1912 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
1913 src_off, dest_off, len);
1915 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1917 goto duplicate_extents_out;
1919 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1920 trgtfile->fid.volatile_fid,
1921 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
1922 true /* is_fsctl */,
1923 (char *)&dup_ext_buf,
1924 sizeof(struct duplicate_extents_to_file),
1925 CIFSMaxBufSize, NULL,
1928 if (ret_data_len > 0)
1929 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
1931 duplicate_extents_out:
1936 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1937 struct cifsFileInfo *cfile)
1939 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1940 cfile->fid.volatile_fid);
1944 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1945 struct cifsFileInfo *cfile)
1947 struct fsctl_set_integrity_information_req integr_info;
1948 unsigned int ret_data_len;
1950 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1951 integr_info.Flags = 0;
1952 integr_info.Reserved = 0;
1954 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1955 cfile->fid.volatile_fid,
1956 FSCTL_SET_INTEGRITY_INFORMATION,
1957 true /* is_fsctl */,
1958 (char *)&integr_info,
1959 sizeof(struct fsctl_set_integrity_information_req),
1960 CIFSMaxBufSize, NULL,
1965 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
1966 #define GMT_TOKEN_SIZE 50
1968 #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
1971 * Input buffer contains (empty) struct smb_snapshot array with size filled in
1972 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
1975 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1976 struct cifsFileInfo *cfile, void __user *ioc_buf)
1978 char *retbuf = NULL;
1979 unsigned int ret_data_len = 0;
1981 u32 max_response_size;
1982 struct smb_snapshot_array snapshot_in;
1985 * On the first query to enumerate the list of snapshots available
1986 * for this volume the buffer begins with 0 (number of snapshots
1987 * which can be returned is zero since at that point we do not know
1988 * how big the buffer needs to be). On the second query,
1989 * it (ret_data_len) is set to number of snapshots so we can
1990 * know to set the maximum response size larger (see below).
1992 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
1996 * Note that for snapshot queries that servers like Azure expect that
1997 * the first query be minimal size (and just used to get the number/size
1998 * of previous versions) so response size must be specified as EXACTLY
1999 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2002 if (ret_data_len == 0)
2003 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
2005 max_response_size = CIFSMaxBufSize;
2007 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2008 cfile->fid.volatile_fid,
2009 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
2010 true /* is_fsctl */,
2011 NULL, 0 /* no input data */, max_response_size,
2014 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2019 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2021 if (copy_from_user(&snapshot_in, ioc_buf,
2022 sizeof(struct smb_snapshot_array))) {
2029 * Check for min size, ie not large enough to fit even one GMT
2030 * token (snapshot). On the first ioctl some users may pass in
2031 * smaller size (or zero) to simply get the size of the array
2032 * so the user space caller can allocate sufficient memory
2033 * and retry the ioctl again with larger array size sufficient
2034 * to hold all of the snapshot GMT tokens on the second try.
2036 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2037 ret_data_len = sizeof(struct smb_snapshot_array);
2040 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2041 * the snapshot array (of 50 byte GMT tokens) each
2042 * representing an available previous version of the data
2044 if (ret_data_len > (snapshot_in.snapshot_array_size +
2045 sizeof(struct smb_snapshot_array)))
2046 ret_data_len = snapshot_in.snapshot_array_size +
2047 sizeof(struct smb_snapshot_array);
2049 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2058 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2059 const char *path, struct cifs_sb_info *cifs_sb,
2060 struct cifs_fid *fid, __u16 search_flags,
2061 struct cifs_search_info *srch_inf)
2065 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2066 struct cifs_open_parms oparms;
2068 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2073 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2074 oparms.disposition = FILE_OPEN;
2075 oparms.create_options = cifs_create_options(cifs_sb, 0);
2077 oparms.reconnect = false;
2079 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2082 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
2086 srch_inf->entries_in_buffer = 0;
2087 srch_inf->index_of_last_entry = 2;
2089 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
2090 fid->volatile_fid, 0, srch_inf);
2092 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
2093 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2099 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2100 struct cifs_fid *fid, __u16 search_flags,
2101 struct cifs_search_info *srch_inf)
2103 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2104 fid->volatile_fid, 0, srch_inf);
2108 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2109 struct cifs_fid *fid)
2111 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2115 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2116 * the number of credits and return true. Otherwise - return false.
2119 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
2121 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2123 if (shdr->Status != STATUS_PENDING)
2126 if (shdr->CreditRequest) {
2127 spin_lock(&server->req_lock);
2128 server->credits += le16_to_cpu(shdr->CreditRequest);
2129 spin_unlock(&server->req_lock);
2130 wake_up(&server->request_q);
2137 smb2_is_session_expired(char *buf)
2139 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2141 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2142 shdr->Status != STATUS_USER_SESSION_DELETED)
2145 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2146 le16_to_cpu(shdr->Command),
2147 le64_to_cpu(shdr->MessageId));
2148 cifs_dbg(FYI, "Session expired or deleted\n");
2154 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2155 struct cifsInodeInfo *cinode)
2157 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2158 return SMB2_lease_break(0, tcon, cinode->lease_key,
2159 smb2_get_lease_state(cinode));
2161 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2163 CIFS_CACHE_READ(cinode) ? 1 : 0);
2167 smb2_set_related(struct smb_rqst *rqst)
2169 struct smb2_sync_hdr *shdr;
2171 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2173 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2176 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2179 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2182 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2184 struct smb2_sync_hdr *shdr;
2185 struct cifs_ses *ses = tcon->ses;
2186 struct TCP_Server_Info *server = ses->server;
2187 unsigned long len = smb_rqst_len(server, rqst);
2190 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2192 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2196 /* SMB headers in a compound are 8 byte aligned. */
2198 /* No padding needed */
2202 num_padding = 8 - (len & 7);
2203 if (!smb3_encryption_required(tcon)) {
2205 * If we do not have encryption then we can just add an extra
2206 * iov for the padding.
2208 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2209 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2214 * We can not add a small padding iov for the encryption case
2215 * because the encryption framework can not handle the padding
2217 * We have to flatten this into a single buffer and add
2218 * the padding to it.
2220 for (i = 1; i < rqst->rq_nvec; i++) {
2221 memcpy(rqst->rq_iov[0].iov_base +
2222 rqst->rq_iov[0].iov_len,
2223 rqst->rq_iov[i].iov_base,
2224 rqst->rq_iov[i].iov_len);
2225 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2227 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2229 rqst->rq_iov[0].iov_len += num_padding;
2235 shdr->NextCommand = cpu_to_le32(len);
2239 * Passes the query info response back to the caller on success.
2240 * Caller need to free this with free_rsp_buf().
2243 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2244 __le16 *utf16_path, u32 desired_access,
2245 u32 class, u32 type, u32 output_len,
2246 struct kvec *rsp, int *buftype,
2247 struct cifs_sb_info *cifs_sb)
2249 struct cifs_ses *ses = tcon->ses;
2251 struct smb_rqst rqst[3];
2252 int resp_buftype[3];
2253 struct kvec rsp_iov[3];
2254 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2255 struct kvec qi_iov[1];
2256 struct kvec close_iov[1];
2257 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2258 struct cifs_open_parms oparms;
2259 struct cifs_fid fid;
2262 if (smb3_encryption_required(tcon))
2263 flags |= CIFS_TRANSFORM_REQ;
2265 memset(rqst, 0, sizeof(rqst));
2266 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2267 memset(rsp_iov, 0, sizeof(rsp_iov));
2269 memset(&open_iov, 0, sizeof(open_iov));
2270 rqst[0].rq_iov = open_iov;
2271 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2274 oparms.desired_access = desired_access;
2275 oparms.disposition = FILE_OPEN;
2276 oparms.create_options = cifs_create_options(cifs_sb, 0);
2278 oparms.reconnect = false;
2280 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2283 smb2_set_next_command(tcon, &rqst[0]);
2285 memset(&qi_iov, 0, sizeof(qi_iov));
2286 rqst[1].rq_iov = qi_iov;
2287 rqst[1].rq_nvec = 1;
2289 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
2295 smb2_set_next_command(tcon, &rqst[1]);
2296 smb2_set_related(&rqst[1]);
2298 memset(&close_iov, 0, sizeof(close_iov));
2299 rqst[2].rq_iov = close_iov;
2300 rqst[2].rq_nvec = 1;
2302 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
2305 smb2_set_related(&rqst[2]);
2307 rc = compound_send_recv(xid, ses, flags, 3, rqst,
2308 resp_buftype, rsp_iov);
2310 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2311 if (rc == -EREMCHG) {
2312 tcon->need_reconnect = true;
2313 printk_once(KERN_WARNING "server share %s deleted\n",
2319 *buftype = resp_buftype[1];
2322 SMB2_open_free(&rqst[0]);
2323 SMB2_query_info_free(&rqst[1]);
2324 SMB2_close_free(&rqst[2]);
2325 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2326 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2331 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2332 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2334 struct smb2_query_info_rsp *rsp;
2335 struct smb2_fs_full_size_info *info = NULL;
2336 __le16 utf16_path = 0; /* Null - open root of share */
2337 struct kvec rsp_iov = {NULL, 0};
2338 int buftype = CIFS_NO_BUFFER;
2342 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2343 FILE_READ_ATTRIBUTES,
2344 FS_FULL_SIZE_INFORMATION,
2345 SMB2_O_INFO_FILESYSTEM,
2346 sizeof(struct smb2_fs_full_size_info),
2347 &rsp_iov, &buftype, cifs_sb);
2351 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2352 buf->f_type = SMB2_MAGIC_NUMBER;
2353 info = (struct smb2_fs_full_size_info *)(
2354 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2355 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2356 le32_to_cpu(rsp->OutputBufferLength),
2358 sizeof(struct smb2_fs_full_size_info));
2360 smb2_copy_fs_info_to_kstatfs(info, buf);
2363 free_rsp_buf(buftype, rsp_iov.iov_base);
2368 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2369 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2372 __le16 srch_path = 0; /* Null - open root of share */
2373 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2374 struct cifs_open_parms oparms;
2375 struct cifs_fid fid;
2377 if (!tcon->posix_extensions)
2378 return smb2_queryfs(xid, tcon, cifs_sb, buf);
2381 oparms.desired_access = FILE_READ_ATTRIBUTES;
2382 oparms.disposition = FILE_OPEN;
2383 oparms.create_options = cifs_create_options(cifs_sb, 0);
2385 oparms.reconnect = false;
2387 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
2391 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2392 fid.volatile_fid, buf);
2393 buf->f_type = SMB2_MAGIC_NUMBER;
2394 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2399 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2401 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2402 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2406 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2407 __u64 length, __u32 type, int lock, int unlock, bool wait)
2409 if (unlock && !lock)
2410 type = SMB2_LOCKFLAG_UNLOCK;
2411 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2412 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2413 current->tgid, length, offset, type, wait);
2417 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2419 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2423 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2425 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2429 smb2_new_lease_key(struct cifs_fid *fid)
2431 generate_random_uuid(fid->lease_key);
2435 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2436 const char *search_name,
2437 struct dfs_info3_param **target_nodes,
2438 unsigned int *num_of_nodes,
2439 const struct nls_table *nls_codepage, int remap)
2442 __le16 *utf16_path = NULL;
2443 int utf16_path_len = 0;
2444 struct cifs_tcon *tcon;
2445 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2446 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2447 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2449 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
2452 * Try to use the IPC tcon, otherwise just use any
2454 tcon = ses->tcon_ipc;
2456 spin_lock(&cifs_tcp_ses_lock);
2457 tcon = list_first_entry_or_null(&ses->tcon_list,
2462 spin_unlock(&cifs_tcp_ses_lock);
2466 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2472 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2474 nls_codepage, remap);
2480 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2481 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2487 /* Highest DFS referral version understood */
2488 dfs_req->MaxReferralLevel = DFS_VERSION;
2490 /* Path to resolve in an UTF-16 null-terminated string */
2491 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2494 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2495 FSCTL_DFS_GET_REFERRALS,
2496 true /* is_fsctl */,
2497 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
2498 (char **)&dfs_rsp, &dfs_rsp_size);
2499 } while (rc == -EAGAIN);
2502 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
2503 cifs_tcon_dbg(VFS, "ioctl error in %s rc=%d\n", __func__, rc);
2507 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2508 num_of_nodes, target_nodes,
2509 nls_codepage, remap, search_name,
2510 true /* is_unicode */);
2512 cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
2517 if (tcon && !tcon->ipc) {
2518 /* ipc tcons are not refcounted */
2519 spin_lock(&cifs_tcp_ses_lock);
2521 spin_unlock(&cifs_tcp_ses_lock);
2530 parse_reparse_posix(struct reparse_posix_data *symlink_buf,
2531 u32 plen, char **target_path,
2532 struct cifs_sb_info *cifs_sb)
2536 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
2537 len = le16_to_cpu(symlink_buf->ReparseDataLength);
2539 if (le64_to_cpu(symlink_buf->InodeType) != NFS_SPECFILE_LNK) {
2540 cifs_dbg(VFS, "%lld not a supported symlink type\n",
2541 le64_to_cpu(symlink_buf->InodeType));
2545 *target_path = cifs_strndup_from_utf16(
2546 symlink_buf->PathBuffer,
2547 len, true, cifs_sb->local_nls);
2548 if (!(*target_path))
2551 convert_delimiter(*target_path, '/');
2552 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2558 parse_reparse_symlink(struct reparse_symlink_data_buffer *symlink_buf,
2559 u32 plen, char **target_path,
2560 struct cifs_sb_info *cifs_sb)
2562 unsigned int sub_len;
2563 unsigned int sub_offset;
2565 /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
2567 sub_offset = le16_to_cpu(symlink_buf->SubstituteNameOffset);
2568 sub_len = le16_to_cpu(symlink_buf->SubstituteNameLength);
2569 if (sub_offset + 20 > plen ||
2570 sub_offset + sub_len + 20 > plen) {
2571 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
2575 *target_path = cifs_strndup_from_utf16(
2576 symlink_buf->PathBuffer + sub_offset,
2577 sub_len, true, cifs_sb->local_nls);
2578 if (!(*target_path))
2581 convert_delimiter(*target_path, '/');
2582 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2588 parse_reparse_point(struct reparse_data_buffer *buf,
2589 u32 plen, char **target_path,
2590 struct cifs_sb_info *cifs_sb)
2592 if (plen < sizeof(struct reparse_data_buffer)) {
2593 cifs_dbg(VFS, "reparse buffer is too small. Must be "
2594 "at least 8 bytes but was %d\n", plen);
2598 if (plen < le16_to_cpu(buf->ReparseDataLength) +
2599 sizeof(struct reparse_data_buffer)) {
2600 cifs_dbg(VFS, "srv returned invalid reparse buf "
2601 "length: %d\n", plen);
2605 /* See MS-FSCC 2.1.2 */
2606 switch (le32_to_cpu(buf->ReparseTag)) {
2607 case IO_REPARSE_TAG_NFS:
2608 return parse_reparse_posix(
2609 (struct reparse_posix_data *)buf,
2610 plen, target_path, cifs_sb);
2611 case IO_REPARSE_TAG_SYMLINK:
2612 return parse_reparse_symlink(
2613 (struct reparse_symlink_data_buffer *)buf,
2614 plen, target_path, cifs_sb);
2616 cifs_dbg(VFS, "srv returned unknown symlink buffer "
2617 "tag:0x%08x\n", le32_to_cpu(buf->ReparseTag));
2622 #define SMB2_SYMLINK_STRUCT_SIZE \
2623 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2626 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
2627 struct cifs_sb_info *cifs_sb, const char *full_path,
2628 char **target_path, bool is_reparse_point)
2631 __le16 *utf16_path = NULL;
2632 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2633 struct cifs_open_parms oparms;
2634 struct cifs_fid fid;
2635 struct kvec err_iov = {NULL, 0};
2636 struct smb2_err_rsp *err_buf = NULL;
2637 struct smb2_symlink_err_rsp *symlink;
2638 unsigned int sub_len;
2639 unsigned int sub_offset;
2640 unsigned int print_len;
2641 unsigned int print_offset;
2643 struct smb_rqst rqst[3];
2644 int resp_buftype[3];
2645 struct kvec rsp_iov[3];
2646 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2647 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2648 struct kvec close_iov[1];
2649 struct smb2_create_rsp *create_rsp;
2650 struct smb2_ioctl_rsp *ioctl_rsp;
2651 struct reparse_data_buffer *reparse_buf;
2652 int create_options = is_reparse_point ? OPEN_REPARSE_POINT : 0;
2655 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2657 *target_path = NULL;
2659 if (smb3_encryption_required(tcon))
2660 flags |= CIFS_TRANSFORM_REQ;
2662 memset(rqst, 0, sizeof(rqst));
2663 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2664 memset(rsp_iov, 0, sizeof(rsp_iov));
2666 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2671 memset(&open_iov, 0, sizeof(open_iov));
2672 rqst[0].rq_iov = open_iov;
2673 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2675 memset(&oparms, 0, sizeof(oparms));
2677 oparms.desired_access = FILE_READ_ATTRIBUTES;
2678 oparms.disposition = FILE_OPEN;
2679 oparms.create_options = cifs_create_options(cifs_sb, create_options);
2681 oparms.reconnect = false;
2683 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2686 smb2_set_next_command(tcon, &rqst[0]);
2690 memset(&io_iov, 0, sizeof(io_iov));
2691 rqst[1].rq_iov = io_iov;
2692 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
2694 rc = SMB2_ioctl_init(tcon, &rqst[1], fid.persistent_fid,
2695 fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
2696 true /* is_fctl */, NULL, 0,
2698 MAX_SMB2_CREATE_RESPONSE_SIZE -
2699 MAX_SMB2_CLOSE_RESPONSE_SIZE);
2703 smb2_set_next_command(tcon, &rqst[1]);
2704 smb2_set_related(&rqst[1]);
2708 memset(&close_iov, 0, sizeof(close_iov));
2709 rqst[2].rq_iov = close_iov;
2710 rqst[2].rq_nvec = 1;
2712 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
2716 smb2_set_related(&rqst[2]);
2718 rc = compound_send_recv(xid, tcon->ses, flags, 3, rqst,
2719 resp_buftype, rsp_iov);
2721 create_rsp = rsp_iov[0].iov_base;
2722 if (create_rsp && create_rsp->sync_hdr.Status)
2723 err_iov = rsp_iov[0];
2724 ioctl_rsp = rsp_iov[1].iov_base;
2727 * Open was successful and we got an ioctl response.
2729 if ((rc == 0) && (is_reparse_point)) {
2730 /* See MS-FSCC 2.3.23 */
2732 reparse_buf = (struct reparse_data_buffer *)
2733 ((char *)ioctl_rsp +
2734 le32_to_cpu(ioctl_rsp->OutputOffset));
2735 plen = le32_to_cpu(ioctl_rsp->OutputCount);
2737 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
2738 rsp_iov[1].iov_len) {
2739 cifs_tcon_dbg(VFS, "srv returned invalid ioctl len: %d\n",
2745 rc = parse_reparse_point(reparse_buf, plen, target_path,
2750 if (!rc || !err_iov.iov_base) {
2755 err_buf = err_iov.iov_base;
2756 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
2757 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
2762 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
2763 if (le32_to_cpu(symlink->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
2764 le32_to_cpu(symlink->ReparseTag) != IO_REPARSE_TAG_SYMLINK) {
2769 /* open must fail on symlink - reset rc */
2771 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
2772 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
2773 print_len = le16_to_cpu(symlink->PrintNameLength);
2774 print_offset = le16_to_cpu(symlink->PrintNameOffset);
2776 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
2781 if (err_iov.iov_len <
2782 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
2787 *target_path = cifs_strndup_from_utf16(
2788 (char *)symlink->PathBuffer + sub_offset,
2789 sub_len, true, cifs_sb->local_nls);
2790 if (!(*target_path)) {
2794 convert_delimiter(*target_path, '/');
2795 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2798 cifs_dbg(FYI, "query symlink rc %d\n", rc);
2800 SMB2_open_free(&rqst[0]);
2801 SMB2_ioctl_free(&rqst[1]);
2802 SMB2_close_free(&rqst[2]);
2803 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2804 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2805 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2809 static struct cifs_ntsd *
2810 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
2811 const struct cifs_fid *cifsfid, u32 *pacllen)
2813 struct cifs_ntsd *pntsd = NULL;
2815 int rc = -EOPNOTSUPP;
2816 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2819 return ERR_CAST(tlink);
2822 cifs_dbg(FYI, "trying to get acl\n");
2824 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
2825 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
2828 cifs_put_tlink(tlink);
2830 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2837 static struct cifs_ntsd *
2838 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
2839 const char *path, u32 *pacllen)
2841 struct cifs_ntsd *pntsd = NULL;
2842 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2845 struct cifs_tcon *tcon;
2846 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2847 struct cifs_fid fid;
2848 struct cifs_open_parms oparms;
2851 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
2853 return ERR_CAST(tlink);
2855 tcon = tlink_tcon(tlink);
2858 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2866 oparms.desired_access = READ_CONTROL;
2867 oparms.disposition = FILE_OPEN;
2868 oparms.create_options = cifs_create_options(cifs_sb, 0);
2870 oparms.reconnect = false;
2872 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2875 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2876 fid.volatile_fid, (void **)&pntsd, pacllen);
2877 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2880 cifs_put_tlink(tlink);
2883 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2890 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
2891 struct inode *inode, const char *path, int aclflag)
2893 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2895 int rc, access_flags = 0;
2896 struct cifs_tcon *tcon;
2897 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2898 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2899 struct cifs_fid fid;
2900 struct cifs_open_parms oparms;
2903 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
2905 return PTR_ERR(tlink);
2907 tcon = tlink_tcon(tlink);
2910 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
2911 access_flags = WRITE_OWNER;
2913 access_flags = WRITE_DAC;
2915 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2923 oparms.desired_access = access_flags;
2924 oparms.create_options = cifs_create_options(cifs_sb, 0);
2925 oparms.disposition = FILE_OPEN;
2928 oparms.reconnect = false;
2930 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2933 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2934 fid.volatile_fid, pnntsd, acllen, aclflag);
2935 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2938 cifs_put_tlink(tlink);
2943 /* Retrieve an ACL from the server */
2944 static struct cifs_ntsd *
2945 get_smb2_acl(struct cifs_sb_info *cifs_sb,
2946 struct inode *inode, const char *path,
2949 struct cifs_ntsd *pntsd = NULL;
2950 struct cifsFileInfo *open_file = NULL;
2953 open_file = find_readable_file(CIFS_I(inode), true);
2955 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
2957 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
2958 cifsFileInfo_put(open_file);
2962 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
2963 loff_t offset, loff_t len, bool keep_size)
2965 struct cifs_ses *ses = tcon->ses;
2966 struct inode *inode;
2967 struct cifsInodeInfo *cifsi;
2968 struct cifsFileInfo *cfile = file->private_data;
2969 struct file_zero_data_information fsctl_buf;
2976 inode = d_inode(cfile->dentry);
2977 cifsi = CIFS_I(inode);
2979 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
2980 ses->Suid, offset, len);
2983 * We zero the range through ioctl, so we need remove the page caches
2984 * first, otherwise the data may be inconsistent with the server.
2986 truncate_pagecache_range(inode, offset, offset + len - 1);
2988 /* if file not oplocked can't be sure whether asking to extend size */
2989 if (!CIFS_CACHE_READ(cifsi))
2990 if (keep_size == false) {
2992 trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
2993 tcon->tid, ses->Suid, offset, len, rc);
2998 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3000 fsctl_buf.FileOffset = cpu_to_le64(offset);
3001 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3003 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3004 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
3006 sizeof(struct file_zero_data_information),
3009 goto zero_range_exit;
3012 * do we also need to change the size of the file?
3014 if (keep_size == false && i_size_read(inode) < offset + len) {
3015 eof = cpu_to_le64(offset + len);
3016 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3017 cfile->fid.volatile_fid, cfile->pid, &eof);
3023 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3024 ses->Suid, offset, len, rc);
3026 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3027 ses->Suid, offset, len);
3031 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3032 loff_t offset, loff_t len)
3034 struct inode *inode = file_inode(file);
3035 struct cifsFileInfo *cfile = file->private_data;
3036 struct file_zero_data_information fsctl_buf;
3039 __u8 set_sparse = 1;
3044 /* Need to make file sparse, if not already, before freeing range. */
3045 /* Consider adding equivalent for compressed since it could also work */
3046 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3052 * We implement the punch hole through ioctl, so we need remove the page
3053 * caches first, otherwise the data may be inconsistent with the server.
3055 truncate_pagecache_range(inode, offset, offset + len - 1);
3057 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3059 fsctl_buf.FileOffset = cpu_to_le64(offset);
3060 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3062 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3063 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3064 true /* is_fctl */, (char *)&fsctl_buf,
3065 sizeof(struct file_zero_data_information),
3066 CIFSMaxBufSize, NULL, NULL);
3068 inode_unlock(inode);
3073 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3074 loff_t off, loff_t len, bool keep_size)
3076 struct inode *inode;
3077 struct cifsInodeInfo *cifsi;
3078 struct cifsFileInfo *cfile = file->private_data;
3079 long rc = -EOPNOTSUPP;
3085 inode = d_inode(cfile->dentry);
3086 cifsi = CIFS_I(inode);
3088 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3089 tcon->ses->Suid, off, len);
3090 /* if file not oplocked can't be sure whether asking to extend size */
3091 if (!CIFS_CACHE_READ(cifsi))
3092 if (keep_size == false) {
3093 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3094 tcon->tid, tcon->ses->Suid, off, len, rc);
3100 * Files are non-sparse by default so falloc may be a no-op
3101 * Must check if file sparse. If not sparse, and not extending
3102 * then no need to do anything since file already allocated
3104 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
3105 if (keep_size == true)
3107 /* check if extending file */
3108 else if (i_size_read(inode) >= off + len)
3109 /* not extending file and already not sparse */
3111 /* BB: in future add else clause to extend file */
3115 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3116 tcon->tid, tcon->ses->Suid, off, len, rc);
3118 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid,
3119 tcon->tid, tcon->ses->Suid, off, len);
3124 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3126 * Check if falloc starts within first few pages of file
3127 * and ends within a few pages of the end of file to
3128 * ensure that most of file is being forced to be
3129 * fallocated now. If so then setting whole file sparse
3130 * ie potentially making a few extra pages at the beginning
3131 * or end of the file non-sparse via set_sparse is harmless.
3133 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3135 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3136 tcon->tid, tcon->ses->Suid, off, len, rc);
3141 smb2_set_sparse(xid, tcon, cfile, inode, false);
3144 smb2_set_sparse(xid, tcon, cfile, inode, false);
3146 if (i_size_read(inode) < off + len) {
3147 eof = cpu_to_le64(off + len);
3148 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3149 cfile->fid.volatile_fid, cfile->pid,
3155 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3156 tcon->ses->Suid, off, len, rc);
3158 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3159 tcon->ses->Suid, off, len);
3165 static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3167 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3168 struct cifsInodeInfo *cifsi;
3169 struct inode *inode;
3171 struct file_allocated_range_buffer in_data, *out_data = NULL;
3175 if (whence != SEEK_HOLE && whence != SEEK_DATA)
3176 return generic_file_llseek(file, offset, whence);
3178 inode = d_inode(cfile->dentry);
3179 cifsi = CIFS_I(inode);
3181 if (offset < 0 || offset >= i_size_read(inode))
3186 * We need to be sure that all dirty pages are written as they
3187 * might fill holes on the server.
3188 * Note that we also MUST flush any written pages since at least
3189 * some servers (Windows2016) will not reflect recent writes in
3190 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3192 wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
3194 filemap_write_and_wait(inode->i_mapping);
3195 smb2_flush_file(xid, tcon, &wrcfile->fid);
3196 cifsFileInfo_put(wrcfile);
3199 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3200 if (whence == SEEK_HOLE)
3201 offset = i_size_read(inode);
3205 in_data.file_offset = cpu_to_le64(offset);
3206 in_data.length = cpu_to_le64(i_size_read(inode));
3208 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3209 cfile->fid.volatile_fid,
3210 FSCTL_QUERY_ALLOCATED_RANGES, true,
3211 (char *)&in_data, sizeof(in_data),
3212 sizeof(struct file_allocated_range_buffer),
3213 (char **)&out_data, &out_data_len);
3219 if (whence == SEEK_HOLE && out_data_len == 0)
3222 if (whence == SEEK_DATA && out_data_len == 0) {
3227 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3231 if (whence == SEEK_DATA) {
3232 offset = le64_to_cpu(out_data->file_offset);
3235 if (offset < le64_to_cpu(out_data->file_offset))
3238 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3244 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3249 static int smb3_fiemap(struct cifs_tcon *tcon,
3250 struct cifsFileInfo *cfile,
3251 struct fiemap_extent_info *fei, u64 start, u64 len)
3254 struct file_allocated_range_buffer in_data, *out_data;
3256 int i, num, rc, flags, last_blob;
3259 if (fiemap_check_flags(fei, FIEMAP_FLAG_SYNC))
3264 in_data.file_offset = cpu_to_le64(start);
3265 in_data.length = cpu_to_le64(len);
3267 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3268 cfile->fid.volatile_fid,
3269 FSCTL_QUERY_ALLOCATED_RANGES, true,
3270 (char *)&in_data, sizeof(in_data),
3271 1024 * sizeof(struct file_allocated_range_buffer),
3272 (char **)&out_data, &out_data_len);
3281 if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) {
3285 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3290 num = out_data_len / sizeof(struct file_allocated_range_buffer);
3291 for (i = 0; i < num; i++) {
3293 if (i == num - 1 && last_blob)
3294 flags |= FIEMAP_EXTENT_LAST;
3296 rc = fiemap_fill_next_extent(fei,
3297 le64_to_cpu(out_data[i].file_offset),
3298 le64_to_cpu(out_data[i].file_offset),
3299 le64_to_cpu(out_data[i].length),
3310 next = le64_to_cpu(out_data[num - 1].file_offset) +
3311 le64_to_cpu(out_data[num - 1].length);
3312 len = len - (next - start);
3323 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3324 loff_t off, loff_t len)
3326 /* KEEP_SIZE already checked for by do_fallocate */
3327 if (mode & FALLOC_FL_PUNCH_HOLE)
3328 return smb3_punch_hole(file, tcon, off, len);
3329 else if (mode & FALLOC_FL_ZERO_RANGE) {
3330 if (mode & FALLOC_FL_KEEP_SIZE)
3331 return smb3_zero_range(file, tcon, off, len, true);
3332 return smb3_zero_range(file, tcon, off, len, false);
3333 } else if (mode == FALLOC_FL_KEEP_SIZE)
3334 return smb3_simple_falloc(file, tcon, off, len, true);
3336 return smb3_simple_falloc(file, tcon, off, len, false);
3342 smb2_downgrade_oplock(struct TCP_Server_Info *server,
3343 struct cifsInodeInfo *cinode, __u32 oplock,
3344 unsigned int epoch, bool *purge_cache)
3346 server->ops->set_oplock_level(cinode, oplock, 0, NULL);
3350 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3351 unsigned int epoch, bool *purge_cache);
3354 smb3_downgrade_oplock(struct TCP_Server_Info *server,
3355 struct cifsInodeInfo *cinode, __u32 oplock,
3356 unsigned int epoch, bool *purge_cache)
3358 unsigned int old_state = cinode->oplock;
3359 unsigned int old_epoch = cinode->epoch;
3360 unsigned int new_state;
3362 if (epoch > old_epoch) {
3363 smb21_set_oplock_level(cinode, oplock, 0, NULL);
3364 cinode->epoch = epoch;
3367 new_state = cinode->oplock;
3368 *purge_cache = false;
3370 if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
3371 (new_state & CIFS_CACHE_READ_FLG) == 0)
3372 *purge_cache = true;
3373 else if (old_state == new_state && (epoch - old_epoch > 1))
3374 *purge_cache = true;
3378 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3379 unsigned int epoch, bool *purge_cache)
3382 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3384 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3385 cinode->oplock = CIFS_CACHE_RHW_FLG;
3386 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3387 &cinode->vfs_inode);
3388 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
3389 cinode->oplock = CIFS_CACHE_RW_FLG;
3390 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3391 &cinode->vfs_inode);
3392 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3393 cinode->oplock = CIFS_CACHE_READ_FLG;
3394 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3395 &cinode->vfs_inode);
3401 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3402 unsigned int epoch, bool *purge_cache)
3404 char message[5] = {0};
3405 unsigned int new_oplock = 0;
3408 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3411 /* Check if the server granted an oplock rather than a lease */
3412 if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3413 return smb2_set_oplock_level(cinode, oplock, epoch,
3416 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
3417 new_oplock |= CIFS_CACHE_READ_FLG;
3418 strcat(message, "R");
3420 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
3421 new_oplock |= CIFS_CACHE_HANDLE_FLG;
3422 strcat(message, "H");
3424 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
3425 new_oplock |= CIFS_CACHE_WRITE_FLG;
3426 strcat(message, "W");
3429 strncpy(message, "None", sizeof(message));
3431 cinode->oplock = new_oplock;
3432 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3433 &cinode->vfs_inode);
3437 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3438 unsigned int epoch, bool *purge_cache)
3440 unsigned int old_oplock = cinode->oplock;
3442 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3445 *purge_cache = false;
3446 if (old_oplock == CIFS_CACHE_READ_FLG) {
3447 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3448 (epoch - cinode->epoch > 0))
3449 *purge_cache = true;
3450 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3451 (epoch - cinode->epoch > 1))
3452 *purge_cache = true;
3453 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3454 (epoch - cinode->epoch > 1))
3455 *purge_cache = true;
3456 else if (cinode->oplock == 0 &&
3457 (epoch - cinode->epoch > 0))
3458 *purge_cache = true;
3459 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
3460 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3461 (epoch - cinode->epoch > 0))
3462 *purge_cache = true;
3463 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3464 (epoch - cinode->epoch > 1))
3465 *purge_cache = true;
3467 cinode->epoch = epoch;
3471 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3473 smb2_is_read_op(__u32 oplock)
3475 return oplock == SMB2_OPLOCK_LEVEL_II;
3477 #endif /* CIFS_ALLOW_INSECURE_LEGACY */
3480 smb21_is_read_op(__u32 oplock)
3482 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
3483 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
3487 map_oplock_to_lease(u8 oplock)
3489 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3490 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
3491 else if (oplock == SMB2_OPLOCK_LEVEL_II)
3492 return SMB2_LEASE_READ_CACHING;
3493 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3494 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
3495 SMB2_LEASE_WRITE_CACHING;
3500 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3502 struct create_lease *buf;
3504 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
3508 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3509 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3511 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3512 (struct create_lease, lcontext));
3513 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
3514 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3515 (struct create_lease, Name));
3516 buf->ccontext.NameLength = cpu_to_le16(4);
3517 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3526 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
3528 struct create_lease_v2 *buf;
3530 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
3534 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3535 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3537 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3538 (struct create_lease_v2, lcontext));
3539 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
3540 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3541 (struct create_lease_v2, Name));
3542 buf->ccontext.NameLength = cpu_to_le16(4);
3543 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3552 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3554 struct create_lease *lc = (struct create_lease *)buf;
3556 *epoch = 0; /* not used */
3557 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3558 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3559 return le32_to_cpu(lc->lcontext.LeaseState);
3563 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3565 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
3567 *epoch = le16_to_cpu(lc->lcontext.Epoch);
3568 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3569 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3571 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
3572 return le32_to_cpu(lc->lcontext.LeaseState);
3576 smb2_wp_retry_size(struct inode *inode)
3578 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
3579 SMB2_MAX_BUFFER_SIZE);
3583 smb2_dir_needs_close(struct cifsFileInfo *cfile)
3585 return !cfile->invalidHandle;
3589 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
3590 struct smb_rqst *old_rq, __le16 cipher_type)
3592 struct smb2_sync_hdr *shdr =
3593 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
3595 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
3596 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
3597 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
3598 tr_hdr->Flags = cpu_to_le16(0x01);
3599 if (cipher_type == SMB2_ENCRYPTION_AES128_GCM)
3600 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128GCM_NONCE);
3602 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CCM_NONCE);
3603 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
3606 static void *smb2_aead_req_alloc(struct crypto_aead *tfm, const struct smb_rqst *rqst,
3607 int num_rqst, const u8 *sig, u8 **iv,
3608 struct aead_request **req, struct scatterlist **sgl,
3609 unsigned int *num_sgs)
3611 unsigned int req_size = sizeof(**req) + crypto_aead_reqsize(tfm);
3612 unsigned int iv_size = crypto_aead_ivsize(tfm);
3616 *num_sgs = cifs_get_num_sgs(rqst, num_rqst, sig);
3619 len += crypto_aead_alignmask(tfm) & ~(crypto_tfm_ctx_alignment() - 1);
3620 len = ALIGN(len, crypto_tfm_ctx_alignment());
3622 len = ALIGN(len, __alignof__(struct scatterlist));
3623 len += *num_sgs * sizeof(**sgl);
3625 p = kmalloc(len, GFP_ATOMIC);
3629 *iv = (u8 *)PTR_ALIGN(p, crypto_aead_alignmask(tfm) + 1);
3630 *req = (struct aead_request *)PTR_ALIGN(*iv + iv_size,
3631 crypto_tfm_ctx_alignment());
3632 *sgl = (struct scatterlist *)PTR_ALIGN((u8 *)*req + req_size,
3633 __alignof__(struct scatterlist));
3637 static void *smb2_get_aead_req(struct crypto_aead *tfm, const struct smb_rqst *rqst,
3638 int num_rqst, const u8 *sig, u8 **iv,
3639 struct aead_request **req, struct scatterlist **sgl)
3641 unsigned int off, len, skip;
3642 struct scatterlist *sg;
3643 unsigned int num_sgs;
3648 p = smb2_aead_req_alloc(tfm, rqst, num_rqst, sig, iv, req, sgl, &num_sgs);
3652 sg_init_table(*sgl, num_sgs);
3655 /* Assumes the first rqst has a transform header as the first iov.
3657 * rqst[0].rq_iov[0] is transform header
3658 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
3659 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
3661 for (i = 0; i < num_rqst; i++) {
3663 * The first rqst has a transform header where the
3664 * first 20 bytes are not part of the encrypted blob.
3666 for (j = 0; j < rqst[i].rq_nvec; j++) {
3667 struct kvec *iov = &rqst[i].rq_iov[j];
3669 skip = (i == 0) && (j == 0) ? 20 : 0;
3670 addr = (unsigned long)iov->iov_base + skip;
3671 len = iov->iov_len - skip;
3672 sg = cifs_sg_set_buf(sg, (void *)addr, len);
3674 for (j = 0; j < rqst[i].rq_npages; j++) {
3675 rqst_page_get_length(&rqst[i], j, &len, &off);
3676 sg_set_page(sg++, rqst[i].rq_pages[j], len, off);
3679 cifs_sg_set_buf(sg, sig, SMB2_SIGNATURE_SIZE);
3685 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
3687 struct cifs_ses *ses;
3690 spin_lock(&cifs_tcp_ses_lock);
3691 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3692 if (ses->Suid != ses_id)
3694 ses_enc_key = enc ? ses->smb3encryptionkey :
3695 ses->smb3decryptionkey;
3696 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
3697 spin_unlock(&cifs_tcp_ses_lock);
3700 spin_unlock(&cifs_tcp_ses_lock);
3705 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
3706 * iov[0] - transform header (associate data),
3707 * iov[1-N] - SMB2 header and pages - data to encrypt.
3708 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
3712 crypt_message(struct TCP_Server_Info *server, int num_rqst,
3713 struct smb_rqst *rqst, int enc)
3715 struct smb2_transform_hdr *tr_hdr =
3716 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
3717 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
3719 struct scatterlist *sg;
3720 u8 sign[SMB2_SIGNATURE_SIZE] = {};
3721 u8 key[SMB3_SIGN_KEY_SIZE];
3722 struct aead_request *req;
3724 DECLARE_CRYPTO_WAIT(wait);
3725 struct crypto_aead *tfm;
3726 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3729 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
3731 cifs_server_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
3736 rc = smb3_crypto_aead_allocate(server);
3738 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
3742 tfm = enc ? server->secmech.ccmaesencrypt :
3743 server->secmech.ccmaesdecrypt;
3744 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
3746 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
3750 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
3752 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
3756 creq = smb2_get_aead_req(tfm, rqst, num_rqst, sign, &iv, &req, &sg);
3757 if (unlikely(!creq))
3761 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
3762 crypt_len += SMB2_SIGNATURE_SIZE;
3765 if (server->cipher_type == SMB2_ENCRYPTION_AES128_GCM)
3766 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES128GCM_NONCE);
3769 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CCM_NONCE);
3772 aead_request_set_tfm(req, tfm);
3773 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
3774 aead_request_set_ad(req, assoc_data_len);
3776 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3777 crypto_req_done, &wait);
3779 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
3780 : crypto_aead_decrypt(req), &wait);
3783 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
3790 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
3794 for (i = 0; i < num_rqst; i++) {
3795 if (rqst[i].rq_pages) {
3796 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
3797 put_page(rqst[i].rq_pages[j]);
3798 kfree(rqst[i].rq_pages);
3804 * This function will initialize new_rq and encrypt the content.
3805 * The first entry, new_rq[0], only contains a single iov which contains
3806 * a smb2_transform_hdr and is pre-allocated by the caller.
3807 * This function then populates new_rq[1+] with the content from olq_rq[0+].
3809 * The end result is an array of smb_rqst structures where the first structure
3810 * only contains a single iov for the transform header which we then can pass
3811 * to crypt_message().
3813 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
3814 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
3817 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
3818 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
3820 struct page **pages;
3821 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
3822 unsigned int npages;
3823 unsigned int orig_len = 0;
3827 for (i = 1; i < num_rqst; i++) {
3828 npages = old_rq[i - 1].rq_npages;
3829 pages = kmalloc_array(npages, sizeof(struct page *),
3834 new_rq[i].rq_pages = pages;
3835 new_rq[i].rq_npages = npages;
3836 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
3837 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
3838 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
3839 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
3840 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
3842 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
3844 for (j = 0; j < npages; j++) {
3845 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3850 /* copy pages form the old */
3851 for (j = 0; j < npages; j++) {
3853 unsigned int offset, len;
3855 rqst_page_get_length(&new_rq[i], j, &len, &offset);
3857 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
3858 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
3860 memcpy(dst, src, len);
3861 kunmap(new_rq[i].rq_pages[j]);
3862 kunmap(old_rq[i - 1].rq_pages[j]);
3866 /* fill the 1st iov with a transform header */
3867 fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
3869 rc = crypt_message(server, num_rqst, new_rq, 1);
3870 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
3877 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
3882 smb3_is_transform_hdr(void *buf)
3884 struct smb2_transform_hdr *trhdr = buf;
3886 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
3890 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
3891 unsigned int buf_data_size, struct page **pages,
3892 unsigned int npages, unsigned int page_data_size,
3896 struct smb_rqst rqst = {NULL};
3899 iov[0].iov_base = buf;
3900 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
3901 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
3902 iov[1].iov_len = buf_data_size;
3906 rqst.rq_pages = pages;
3907 rqst.rq_npages = npages;
3908 rqst.rq_pagesz = PAGE_SIZE;
3909 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
3911 rc = crypt_message(server, 1, &rqst, 0);
3912 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
3917 memmove(buf, iov[1].iov_base, buf_data_size);
3920 server->total_read = buf_data_size + page_data_size;
3926 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
3927 unsigned int npages, unsigned int len)
3932 for (i = 0; i < npages; i++) {
3933 struct page *page = pages[i];
3937 if (len >= PAGE_SIZE) {
3938 /* enough data to fill the page */
3942 zero_user(page, len, PAGE_SIZE - len);
3945 length = cifs_read_page_from_socket(server, page, 0, n);
3948 server->total_read += length;
3955 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
3956 unsigned int cur_off, struct bio_vec **page_vec)
3958 struct bio_vec *bvec;
3961 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
3965 for (i = 0; i < npages; i++) {
3966 bvec[i].bv_page = pages[i];
3967 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
3968 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
3969 data_size -= bvec[i].bv_len;
3972 if (data_size != 0) {
3973 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
3983 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
3984 char *buf, unsigned int buf_len, struct page **pages,
3985 unsigned int npages, unsigned int page_data_size,
3988 unsigned int data_offset;
3989 unsigned int data_len;
3990 unsigned int cur_off;
3991 unsigned int cur_page_idx;
3992 unsigned int pad_len;
3993 struct cifs_readdata *rdata = mid->callback_data;
3994 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
3995 struct bio_vec *bvec = NULL;
3996 struct iov_iter iter;
3999 bool use_rdma_mr = false;
4001 if (shdr->Command != SMB2_READ) {
4002 cifs_server_dbg(VFS, "only big read responses are supported\n");
4006 if (server->ops->is_session_expired &&
4007 server->ops->is_session_expired(buf)) {
4009 cifs_reconnect(server);
4010 wake_up(&server->response_q);
4014 if (server->ops->is_status_pending &&
4015 server->ops->is_status_pending(buf, server))
4018 /* set up first two iov to get credits */
4019 rdata->iov[0].iov_base = buf;
4020 rdata->iov[0].iov_len = 0;
4021 rdata->iov[1].iov_base = buf;
4022 rdata->iov[1].iov_len =
4023 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
4024 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4025 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4026 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4027 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4029 rdata->result = server->ops->map_error(buf, true);
4030 if (rdata->result != 0) {
4031 cifs_dbg(FYI, "%s: server returned error %d\n",
4032 __func__, rdata->result);
4033 /* normal error on read response */
4035 mid->mid_state = MID_RESPONSE_RECEIVED;
4037 dequeue_mid(mid, false);
4041 data_offset = server->ops->read_data_offset(buf);
4042 #ifdef CONFIG_CIFS_SMB_DIRECT
4043 use_rdma_mr = rdata->mr;
4045 data_len = server->ops->read_data_length(buf, use_rdma_mr);
4047 if (data_offset < server->vals->read_rsp_size) {
4049 * win2k8 sometimes sends an offset of 0 when the read
4050 * is beyond the EOF. Treat it as if the data starts just after
4053 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4054 __func__, data_offset);
4055 data_offset = server->vals->read_rsp_size;
4056 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4057 /* data_offset is beyond the end of smallbuf */
4058 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4059 __func__, data_offset);
4060 rdata->result = -EIO;
4062 mid->mid_state = MID_RESPONSE_MALFORMED;
4064 dequeue_mid(mid, rdata->result);
4068 pad_len = data_offset - server->vals->read_rsp_size;
4070 if (buf_len <= data_offset) {
4071 /* read response payload is in pages */
4072 cur_page_idx = pad_len / PAGE_SIZE;
4073 cur_off = pad_len % PAGE_SIZE;
4075 if (cur_page_idx != 0) {
4076 /* data offset is beyond the 1st page of response */
4077 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4078 __func__, data_offset);
4079 rdata->result = -EIO;
4081 mid->mid_state = MID_RESPONSE_MALFORMED;
4083 dequeue_mid(mid, rdata->result);
4087 if (data_len > page_data_size - pad_len) {
4088 /* data_len is corrupt -- discard frame */
4089 rdata->result = -EIO;
4091 mid->mid_state = MID_RESPONSE_MALFORMED;
4093 dequeue_mid(mid, rdata->result);
4097 rdata->result = init_read_bvec(pages, npages, page_data_size,
4099 if (rdata->result != 0) {
4101 mid->mid_state = MID_RESPONSE_MALFORMED;
4103 dequeue_mid(mid, rdata->result);
4107 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
4108 } else if (buf_len >= data_offset + data_len) {
4109 /* read response payload is in buf */
4110 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
4111 iov.iov_base = buf + data_offset;
4112 iov.iov_len = data_len;
4113 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
4115 /* read response payload cannot be in both buf and pages */
4116 WARN_ONCE(1, "buf can not contain only a part of read data");
4117 rdata->result = -EIO;
4119 mid->mid_state = MID_RESPONSE_MALFORMED;
4121 dequeue_mid(mid, rdata->result);
4125 length = rdata->copy_into_pages(server, rdata, &iter);
4133 mid->mid_state = MID_RESPONSE_RECEIVED;
4135 dequeue_mid(mid, false);
4139 struct smb2_decrypt_work {
4140 struct work_struct decrypt;
4141 struct TCP_Server_Info *server;
4142 struct page **ppages;
4144 unsigned int npages;
4149 static void smb2_decrypt_offload(struct work_struct *work)
4151 struct smb2_decrypt_work *dw = container_of(work,
4152 struct smb2_decrypt_work, decrypt);
4154 struct mid_q_entry *mid;
4156 rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
4157 dw->ppages, dw->npages, dw->len, true);
4159 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4163 dw->server->lstrp = jiffies;
4164 mid = smb2_find_dequeue_mid(dw->server, dw->buf);
4166 cifs_dbg(FYI, "mid not found\n");
4168 mid->decrypted = true;
4169 rc = handle_read_data(dw->server, mid, dw->buf,
4170 dw->server->vals->read_rsp_size,
4171 dw->ppages, dw->npages, dw->len,
4174 #ifdef CONFIG_CIFS_STATS2
4175 mid->when_received = jiffies;
4179 spin_lock(&GlobalMid_Lock);
4180 if (dw->server->tcpStatus == CifsNeedReconnect) {
4181 mid->mid_state = MID_RETRY_NEEDED;
4182 spin_unlock(&GlobalMid_Lock);
4185 mid->mid_state = MID_REQUEST_SUBMITTED;
4186 mid->mid_flags &= ~(MID_DELETED);
4187 list_add_tail(&mid->qhead,
4188 &dw->server->pending_mid_q);
4189 spin_unlock(&GlobalMid_Lock);
4192 cifs_mid_q_entry_release(mid);
4196 for (i = dw->npages-1; i >= 0; i--)
4197 put_page(dw->ppages[i]);
4200 cifs_small_buf_release(dw->buf);
4206 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4209 char *buf = server->smallbuf;
4210 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4211 unsigned int npages;
4212 struct page **pages;
4214 unsigned int buflen = server->pdu_size;
4217 struct smb2_decrypt_work *dw;
4220 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
4221 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
4223 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
4226 server->total_read += rc;
4228 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
4229 server->vals->read_rsp_size;
4230 npages = DIV_ROUND_UP(len, PAGE_SIZE);
4232 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
4238 for (; i < npages; i++) {
4239 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4246 /* read read data into pages */
4247 rc = read_data_into_pages(server, pages, npages, len);
4251 rc = cifs_discard_remaining_data(server);
4256 * For large reads, offload to different thread for better performance,
4257 * use more cores decrypting which can be expensive
4260 if ((server->min_offload) && (server->in_flight > 1) &&
4261 (server->pdu_size >= server->min_offload)) {
4262 dw = kmalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
4264 goto non_offloaded_decrypt;
4266 dw->buf = server->smallbuf;
4267 server->smallbuf = (char *)cifs_small_buf_get();
4269 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
4271 dw->npages = npages;
4272 dw->server = server;
4275 queue_work(decrypt_wq, &dw->decrypt);
4276 *num_mids = 0; /* worker thread takes care of finding mid */
4280 non_offloaded_decrypt:
4281 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
4282 pages, npages, len, false);
4286 *mid = smb2_find_mid(server, buf);
4288 cifs_dbg(FYI, "mid not found\n");
4290 cifs_dbg(FYI, "mid found\n");
4291 (*mid)->decrypted = true;
4292 rc = handle_read_data(server, *mid, buf,
4293 server->vals->read_rsp_size,
4294 pages, npages, len, false);
4298 for (i = i - 1; i >= 0; i--)
4303 cifs_discard_remaining_data(server);
4308 receive_encrypted_standard(struct TCP_Server_Info *server,
4309 struct mid_q_entry **mids, char **bufs,
4313 char *buf = server->smallbuf;
4314 struct smb2_sync_hdr *shdr;
4315 unsigned int pdu_length = server->pdu_size;
4316 unsigned int buf_size;
4317 struct mid_q_entry *mid_entry;
4319 char *next_buffer = NULL;
4323 /* switch to large buffer if too big for a small one */
4324 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
4325 server->large_buf = true;
4326 memcpy(server->bigbuf, buf, server->total_read);
4327 buf = server->bigbuf;
4330 /* now read the rest */
4331 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
4332 pdu_length - HEADER_SIZE(server) + 1);
4335 server->total_read += length;
4337 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
4338 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0, false);
4342 next_is_large = server->large_buf;
4344 shdr = (struct smb2_sync_hdr *)buf;
4345 if (shdr->NextCommand) {
4347 next_buffer = (char *)cifs_buf_get();
4349 next_buffer = (char *)cifs_small_buf_get();
4351 buf + le32_to_cpu(shdr->NextCommand),
4352 pdu_length - le32_to_cpu(shdr->NextCommand));
4355 mid_entry = smb2_find_mid(server, buf);
4356 if (mid_entry == NULL)
4357 cifs_dbg(FYI, "mid not found\n");
4359 cifs_dbg(FYI, "mid found\n");
4360 mid_entry->decrypted = true;
4361 mid_entry->resp_buf_size = server->pdu_size;
4364 if (*num_mids >= MAX_COMPOUND) {
4365 cifs_server_dbg(VFS, "too many PDUs in compound\n");
4368 bufs[*num_mids] = buf;
4369 mids[(*num_mids)++] = mid_entry;
4371 if (mid_entry && mid_entry->handle)
4372 ret = mid_entry->handle(server, mid_entry);
4374 ret = cifs_handle_standard(server, mid_entry);
4376 if (ret == 0 && shdr->NextCommand) {
4377 pdu_length -= le32_to_cpu(shdr->NextCommand);
4378 server->large_buf = next_is_large;
4380 server->bigbuf = buf = next_buffer;
4382 server->smallbuf = buf = next_buffer;
4384 } else if (ret != 0) {
4386 * ret != 0 here means that we didn't get to handle_mid() thus
4387 * server->smallbuf and server->bigbuf are still valid. We need
4388 * to free next_buffer because it is not going to be used
4392 free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
4394 free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
4401 smb3_receive_transform(struct TCP_Server_Info *server,
4402 struct mid_q_entry **mids, char **bufs, int *num_mids)
4404 char *buf = server->smallbuf;
4405 unsigned int pdu_length = server->pdu_size;
4406 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4407 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4409 if (pdu_length < sizeof(struct smb2_transform_hdr) +
4410 sizeof(struct smb2_sync_hdr)) {
4411 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
4413 cifs_reconnect(server);
4414 wake_up(&server->response_q);
4415 return -ECONNABORTED;
4418 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
4419 cifs_server_dbg(VFS, "Transform message is broken\n");
4420 cifs_reconnect(server);
4421 wake_up(&server->response_q);
4422 return -ECONNABORTED;
4425 /* TODO: add support for compounds containing READ. */
4426 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
4427 return receive_encrypted_read(server, &mids[0], num_mids);
4430 return receive_encrypted_standard(server, mids, bufs, num_mids);
4434 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
4436 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
4438 return handle_read_data(server, mid, buf, server->pdu_size,
4443 smb2_next_header(char *buf)
4445 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
4446 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
4448 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
4449 return sizeof(struct smb2_transform_hdr) +
4450 le32_to_cpu(t_hdr->OriginalMessageSize);
4452 return le32_to_cpu(hdr->NextCommand);
4456 smb2_make_node(unsigned int xid, struct inode *inode,
4457 struct dentry *dentry, struct cifs_tcon *tcon,
4458 char *full_path, umode_t mode, dev_t dev)
4460 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4462 FILE_ALL_INFO *buf = NULL;
4463 struct cifs_io_parms io_parms;
4465 struct cifs_fid fid;
4466 struct cifs_open_parms oparms;
4467 unsigned int bytes_written;
4468 struct win_dev *pdev;
4472 * Check if mounted with mount parm 'sfu' mount parm.
4473 * SFU emulation should work with all servers, but only
4474 * supports block and char device (no socket & fifo),
4475 * and was used by default in earlier versions of Windows
4477 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
4481 * TODO: Add ability to create instead via reparse point. Windows (e.g.
4482 * their current NFS server) uses this approach to expose special files
4483 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
4486 if (!S_ISCHR(mode) && !S_ISBLK(mode))
4489 cifs_dbg(FYI, "sfu compat create special file\n");
4491 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
4498 oparms.cifs_sb = cifs_sb;
4499 oparms.desired_access = GENERIC_WRITE;
4500 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
4501 CREATE_OPTION_SPECIAL);
4502 oparms.disposition = FILE_CREATE;
4503 oparms.path = full_path;
4505 oparms.reconnect = false;
4507 if (tcon->ses->server->oplocks)
4508 oplock = REQ_OPLOCK;
4511 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
4516 * BB Do not bother to decode buf since no local inode yet to put
4517 * timestamps in, but we can reuse it safely.
4520 pdev = (struct win_dev *)buf;
4521 io_parms.pid = current->tgid;
4522 io_parms.tcon = tcon;
4523 io_parms.offset = 0;
4524 io_parms.length = sizeof(struct win_dev);
4525 iov[1].iov_base = buf;
4526 iov[1].iov_len = sizeof(struct win_dev);
4527 if (S_ISCHR(mode)) {
4528 memcpy(pdev->type, "IntxCHR", 8);
4529 pdev->major = cpu_to_le64(MAJOR(dev));
4530 pdev->minor = cpu_to_le64(MINOR(dev));
4531 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4532 &bytes_written, iov, 1);
4533 } else if (S_ISBLK(mode)) {
4534 memcpy(pdev->type, "IntxBLK", 8);
4535 pdev->major = cpu_to_le64(MAJOR(dev));
4536 pdev->minor = cpu_to_le64(MINOR(dev));
4537 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4538 &bytes_written, iov, 1);
4540 tcon->ses->server->ops->close(xid, tcon, &fid);
4543 /* FIXME: add code here to set EAs */
4549 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
4550 struct smb_version_operations smb20_operations = {
4551 .compare_fids = smb2_compare_fids,
4552 .setup_request = smb2_setup_request,
4553 .setup_async_request = smb2_setup_async_request,
4554 .check_receive = smb2_check_receive,
4555 .add_credits = smb2_add_credits,
4556 .set_credits = smb2_set_credits,
4557 .get_credits_field = smb2_get_credits_field,
4558 .get_credits = smb2_get_credits,
4559 .wait_mtu_credits = cifs_wait_mtu_credits,
4560 .get_next_mid = smb2_get_next_mid,
4561 .revert_current_mid = smb2_revert_current_mid,
4562 .read_data_offset = smb2_read_data_offset,
4563 .read_data_length = smb2_read_data_length,
4564 .map_error = map_smb2_to_linux_error,
4565 .find_mid = smb2_find_mid,
4566 .check_message = smb2_check_message,
4567 .dump_detail = smb2_dump_detail,
4568 .clear_stats = smb2_clear_stats,
4569 .print_stats = smb2_print_stats,
4570 .is_oplock_break = smb2_is_valid_oplock_break,
4571 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4572 .downgrade_oplock = smb2_downgrade_oplock,
4573 .need_neg = smb2_need_neg,
4574 .negotiate = smb2_negotiate,
4575 .negotiate_wsize = smb2_negotiate_wsize,
4576 .negotiate_rsize = smb2_negotiate_rsize,
4577 .sess_setup = SMB2_sess_setup,
4578 .logoff = SMB2_logoff,
4579 .tree_connect = SMB2_tcon,
4580 .tree_disconnect = SMB2_tdis,
4581 .qfs_tcon = smb2_qfs_tcon,
4582 .is_path_accessible = smb2_is_path_accessible,
4583 .can_echo = smb2_can_echo,
4585 .query_path_info = smb2_query_path_info,
4586 .get_srv_inum = smb2_get_srv_inum,
4587 .query_file_info = smb2_query_file_info,
4588 .set_path_size = smb2_set_path_size,
4589 .set_file_size = smb2_set_file_size,
4590 .set_file_info = smb2_set_file_info,
4591 .set_compression = smb2_set_compression,
4592 .mkdir = smb2_mkdir,
4593 .mkdir_setinfo = smb2_mkdir_setinfo,
4594 .rmdir = smb2_rmdir,
4595 .unlink = smb2_unlink,
4596 .rename = smb2_rename_path,
4597 .create_hardlink = smb2_create_hardlink,
4598 .query_symlink = smb2_query_symlink,
4599 .query_mf_symlink = smb3_query_mf_symlink,
4600 .create_mf_symlink = smb3_create_mf_symlink,
4601 .open = smb2_open_file,
4602 .set_fid = smb2_set_fid,
4603 .close = smb2_close_file,
4604 .flush = smb2_flush_file,
4605 .async_readv = smb2_async_readv,
4606 .async_writev = smb2_async_writev,
4607 .sync_read = smb2_sync_read,
4608 .sync_write = smb2_sync_write,
4609 .query_dir_first = smb2_query_dir_first,
4610 .query_dir_next = smb2_query_dir_next,
4611 .close_dir = smb2_close_dir,
4612 .calc_smb_size = smb2_calc_size,
4613 .is_status_pending = smb2_is_status_pending,
4614 .is_session_expired = smb2_is_session_expired,
4615 .oplock_response = smb2_oplock_response,
4616 .queryfs = smb2_queryfs,
4617 .mand_lock = smb2_mand_lock,
4618 .mand_unlock_range = smb2_unlock_range,
4619 .push_mand_locks = smb2_push_mandatory_locks,
4620 .get_lease_key = smb2_get_lease_key,
4621 .set_lease_key = smb2_set_lease_key,
4622 .new_lease_key = smb2_new_lease_key,
4623 .calc_signature = smb2_calc_signature,
4624 .is_read_op = smb2_is_read_op,
4625 .set_oplock_level = smb2_set_oplock_level,
4626 .create_lease_buf = smb2_create_lease_buf,
4627 .parse_lease_buf = smb2_parse_lease_buf,
4628 .copychunk_range = smb2_copychunk_range,
4629 .wp_retry_size = smb2_wp_retry_size,
4630 .dir_needs_close = smb2_dir_needs_close,
4631 .get_dfs_refer = smb2_get_dfs_refer,
4632 .select_sectype = smb2_select_sectype,
4633 #ifdef CONFIG_CIFS_XATTR
4634 .query_all_EAs = smb2_query_eas,
4635 .set_EA = smb2_set_ea,
4636 #endif /* CIFS_XATTR */
4637 .get_acl = get_smb2_acl,
4638 .get_acl_by_fid = get_smb2_acl_by_fid,
4639 .set_acl = set_smb2_acl,
4640 .next_header = smb2_next_header,
4641 .ioctl_query_info = smb2_ioctl_query_info,
4642 .make_node = smb2_make_node,
4643 .fiemap = smb3_fiemap,
4644 .llseek = smb3_llseek,
4646 #endif /* CIFS_ALLOW_INSECURE_LEGACY */
4648 struct smb_version_operations smb21_operations = {
4649 .compare_fids = smb2_compare_fids,
4650 .setup_request = smb2_setup_request,
4651 .setup_async_request = smb2_setup_async_request,
4652 .check_receive = smb2_check_receive,
4653 .add_credits = smb2_add_credits,
4654 .set_credits = smb2_set_credits,
4655 .get_credits_field = smb2_get_credits_field,
4656 .get_credits = smb2_get_credits,
4657 .wait_mtu_credits = smb2_wait_mtu_credits,
4658 .adjust_credits = smb2_adjust_credits,
4659 .get_next_mid = smb2_get_next_mid,
4660 .revert_current_mid = smb2_revert_current_mid,
4661 .read_data_offset = smb2_read_data_offset,
4662 .read_data_length = smb2_read_data_length,
4663 .map_error = map_smb2_to_linux_error,
4664 .find_mid = smb2_find_mid,
4665 .check_message = smb2_check_message,
4666 .dump_detail = smb2_dump_detail,
4667 .clear_stats = smb2_clear_stats,
4668 .print_stats = smb2_print_stats,
4669 .is_oplock_break = smb2_is_valid_oplock_break,
4670 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4671 .downgrade_oplock = smb2_downgrade_oplock,
4672 .need_neg = smb2_need_neg,
4673 .negotiate = smb2_negotiate,
4674 .negotiate_wsize = smb2_negotiate_wsize,
4675 .negotiate_rsize = smb2_negotiate_rsize,
4676 .sess_setup = SMB2_sess_setup,
4677 .logoff = SMB2_logoff,
4678 .tree_connect = SMB2_tcon,
4679 .tree_disconnect = SMB2_tdis,
4680 .qfs_tcon = smb2_qfs_tcon,
4681 .is_path_accessible = smb2_is_path_accessible,
4682 .can_echo = smb2_can_echo,
4684 .query_path_info = smb2_query_path_info,
4685 .get_srv_inum = smb2_get_srv_inum,
4686 .query_file_info = smb2_query_file_info,
4687 .set_path_size = smb2_set_path_size,
4688 .set_file_size = smb2_set_file_size,
4689 .set_file_info = smb2_set_file_info,
4690 .set_compression = smb2_set_compression,
4691 .mkdir = smb2_mkdir,
4692 .mkdir_setinfo = smb2_mkdir_setinfo,
4693 .rmdir = smb2_rmdir,
4694 .unlink = smb2_unlink,
4695 .rename = smb2_rename_path,
4696 .create_hardlink = smb2_create_hardlink,
4697 .query_symlink = smb2_query_symlink,
4698 .query_mf_symlink = smb3_query_mf_symlink,
4699 .create_mf_symlink = smb3_create_mf_symlink,
4700 .open = smb2_open_file,
4701 .set_fid = smb2_set_fid,
4702 .close = smb2_close_file,
4703 .flush = smb2_flush_file,
4704 .async_readv = smb2_async_readv,
4705 .async_writev = smb2_async_writev,
4706 .sync_read = smb2_sync_read,
4707 .sync_write = smb2_sync_write,
4708 .query_dir_first = smb2_query_dir_first,
4709 .query_dir_next = smb2_query_dir_next,
4710 .close_dir = smb2_close_dir,
4711 .calc_smb_size = smb2_calc_size,
4712 .is_status_pending = smb2_is_status_pending,
4713 .is_session_expired = smb2_is_session_expired,
4714 .oplock_response = smb2_oplock_response,
4715 .queryfs = smb2_queryfs,
4716 .mand_lock = smb2_mand_lock,
4717 .mand_unlock_range = smb2_unlock_range,
4718 .push_mand_locks = smb2_push_mandatory_locks,
4719 .get_lease_key = smb2_get_lease_key,
4720 .set_lease_key = smb2_set_lease_key,
4721 .new_lease_key = smb2_new_lease_key,
4722 .calc_signature = smb2_calc_signature,
4723 .is_read_op = smb21_is_read_op,
4724 .set_oplock_level = smb21_set_oplock_level,
4725 .create_lease_buf = smb2_create_lease_buf,
4726 .parse_lease_buf = smb2_parse_lease_buf,
4727 .copychunk_range = smb2_copychunk_range,
4728 .wp_retry_size = smb2_wp_retry_size,
4729 .dir_needs_close = smb2_dir_needs_close,
4730 .enum_snapshots = smb3_enum_snapshots,
4731 .get_dfs_refer = smb2_get_dfs_refer,
4732 .select_sectype = smb2_select_sectype,
4733 #ifdef CONFIG_CIFS_XATTR
4734 .query_all_EAs = smb2_query_eas,
4735 .set_EA = smb2_set_ea,
4736 #endif /* CIFS_XATTR */
4737 .get_acl = get_smb2_acl,
4738 .get_acl_by_fid = get_smb2_acl_by_fid,
4739 .set_acl = set_smb2_acl,
4740 .next_header = smb2_next_header,
4741 .ioctl_query_info = smb2_ioctl_query_info,
4742 .make_node = smb2_make_node,
4743 .fiemap = smb3_fiemap,
4744 .llseek = smb3_llseek,
4747 struct smb_version_operations smb30_operations = {
4748 .compare_fids = smb2_compare_fids,
4749 .setup_request = smb2_setup_request,
4750 .setup_async_request = smb2_setup_async_request,
4751 .check_receive = smb2_check_receive,
4752 .add_credits = smb2_add_credits,
4753 .set_credits = smb2_set_credits,
4754 .get_credits_field = smb2_get_credits_field,
4755 .get_credits = smb2_get_credits,
4756 .wait_mtu_credits = smb2_wait_mtu_credits,
4757 .adjust_credits = smb2_adjust_credits,
4758 .get_next_mid = smb2_get_next_mid,
4759 .revert_current_mid = smb2_revert_current_mid,
4760 .read_data_offset = smb2_read_data_offset,
4761 .read_data_length = smb2_read_data_length,
4762 .map_error = map_smb2_to_linux_error,
4763 .find_mid = smb2_find_mid,
4764 .check_message = smb2_check_message,
4765 .dump_detail = smb2_dump_detail,
4766 .clear_stats = smb2_clear_stats,
4767 .print_stats = smb2_print_stats,
4768 .dump_share_caps = smb2_dump_share_caps,
4769 .is_oplock_break = smb2_is_valid_oplock_break,
4770 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4771 .downgrade_oplock = smb3_downgrade_oplock,
4772 .need_neg = smb2_need_neg,
4773 .negotiate = smb2_negotiate,
4774 .negotiate_wsize = smb3_negotiate_wsize,
4775 .negotiate_rsize = smb3_negotiate_rsize,
4776 .sess_setup = SMB2_sess_setup,
4777 .logoff = SMB2_logoff,
4778 .tree_connect = SMB2_tcon,
4779 .tree_disconnect = SMB2_tdis,
4780 .qfs_tcon = smb3_qfs_tcon,
4781 .is_path_accessible = smb2_is_path_accessible,
4782 .can_echo = smb2_can_echo,
4784 .query_path_info = smb2_query_path_info,
4785 .get_srv_inum = smb2_get_srv_inum,
4786 .query_file_info = smb2_query_file_info,
4787 .set_path_size = smb2_set_path_size,
4788 .set_file_size = smb2_set_file_size,
4789 .set_file_info = smb2_set_file_info,
4790 .set_compression = smb2_set_compression,
4791 .mkdir = smb2_mkdir,
4792 .mkdir_setinfo = smb2_mkdir_setinfo,
4793 .rmdir = smb2_rmdir,
4794 .unlink = smb2_unlink,
4795 .rename = smb2_rename_path,
4796 .create_hardlink = smb2_create_hardlink,
4797 .query_symlink = smb2_query_symlink,
4798 .query_mf_symlink = smb3_query_mf_symlink,
4799 .create_mf_symlink = smb3_create_mf_symlink,
4800 .open = smb2_open_file,
4801 .set_fid = smb2_set_fid,
4802 .close = smb2_close_file,
4803 .flush = smb2_flush_file,
4804 .async_readv = smb2_async_readv,
4805 .async_writev = smb2_async_writev,
4806 .sync_read = smb2_sync_read,
4807 .sync_write = smb2_sync_write,
4808 .query_dir_first = smb2_query_dir_first,
4809 .query_dir_next = smb2_query_dir_next,
4810 .close_dir = smb2_close_dir,
4811 .calc_smb_size = smb2_calc_size,
4812 .is_status_pending = smb2_is_status_pending,
4813 .is_session_expired = smb2_is_session_expired,
4814 .oplock_response = smb2_oplock_response,
4815 .queryfs = smb2_queryfs,
4816 .mand_lock = smb2_mand_lock,
4817 .mand_unlock_range = smb2_unlock_range,
4818 .push_mand_locks = smb2_push_mandatory_locks,
4819 .get_lease_key = smb2_get_lease_key,
4820 .set_lease_key = smb2_set_lease_key,
4821 .new_lease_key = smb2_new_lease_key,
4822 .generate_signingkey = generate_smb30signingkey,
4823 .calc_signature = smb3_calc_signature,
4824 .set_integrity = smb3_set_integrity,
4825 .is_read_op = smb21_is_read_op,
4826 .set_oplock_level = smb3_set_oplock_level,
4827 .create_lease_buf = smb3_create_lease_buf,
4828 .parse_lease_buf = smb3_parse_lease_buf,
4829 .copychunk_range = smb2_copychunk_range,
4830 .duplicate_extents = smb2_duplicate_extents,
4831 .validate_negotiate = smb3_validate_negotiate,
4832 .wp_retry_size = smb2_wp_retry_size,
4833 .dir_needs_close = smb2_dir_needs_close,
4834 .fallocate = smb3_fallocate,
4835 .enum_snapshots = smb3_enum_snapshots,
4836 .init_transform_rq = smb3_init_transform_rq,
4837 .is_transform_hdr = smb3_is_transform_hdr,
4838 .receive_transform = smb3_receive_transform,
4839 .get_dfs_refer = smb2_get_dfs_refer,
4840 .select_sectype = smb2_select_sectype,
4841 #ifdef CONFIG_CIFS_XATTR
4842 .query_all_EAs = smb2_query_eas,
4843 .set_EA = smb2_set_ea,
4844 #endif /* CIFS_XATTR */
4845 .get_acl = get_smb2_acl,
4846 .get_acl_by_fid = get_smb2_acl_by_fid,
4847 .set_acl = set_smb2_acl,
4848 .next_header = smb2_next_header,
4849 .ioctl_query_info = smb2_ioctl_query_info,
4850 .make_node = smb2_make_node,
4851 .fiemap = smb3_fiemap,
4852 .llseek = smb3_llseek,
4855 struct smb_version_operations smb311_operations = {
4856 .compare_fids = smb2_compare_fids,
4857 .setup_request = smb2_setup_request,
4858 .setup_async_request = smb2_setup_async_request,
4859 .check_receive = smb2_check_receive,
4860 .add_credits = smb2_add_credits,
4861 .set_credits = smb2_set_credits,
4862 .get_credits_field = smb2_get_credits_field,
4863 .get_credits = smb2_get_credits,
4864 .wait_mtu_credits = smb2_wait_mtu_credits,
4865 .adjust_credits = smb2_adjust_credits,
4866 .get_next_mid = smb2_get_next_mid,
4867 .revert_current_mid = smb2_revert_current_mid,
4868 .read_data_offset = smb2_read_data_offset,
4869 .read_data_length = smb2_read_data_length,
4870 .map_error = map_smb2_to_linux_error,
4871 .find_mid = smb2_find_mid,
4872 .check_message = smb2_check_message,
4873 .dump_detail = smb2_dump_detail,
4874 .clear_stats = smb2_clear_stats,
4875 .print_stats = smb2_print_stats,
4876 .dump_share_caps = smb2_dump_share_caps,
4877 .is_oplock_break = smb2_is_valid_oplock_break,
4878 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4879 .downgrade_oplock = smb3_downgrade_oplock,
4880 .need_neg = smb2_need_neg,
4881 .negotiate = smb2_negotiate,
4882 .negotiate_wsize = smb3_negotiate_wsize,
4883 .negotiate_rsize = smb3_negotiate_rsize,
4884 .sess_setup = SMB2_sess_setup,
4885 .logoff = SMB2_logoff,
4886 .tree_connect = SMB2_tcon,
4887 .tree_disconnect = SMB2_tdis,
4888 .qfs_tcon = smb3_qfs_tcon,
4889 .is_path_accessible = smb2_is_path_accessible,
4890 .can_echo = smb2_can_echo,
4892 .query_path_info = smb2_query_path_info,
4893 .get_srv_inum = smb2_get_srv_inum,
4894 .query_file_info = smb2_query_file_info,
4895 .set_path_size = smb2_set_path_size,
4896 .set_file_size = smb2_set_file_size,
4897 .set_file_info = smb2_set_file_info,
4898 .set_compression = smb2_set_compression,
4899 .mkdir = smb2_mkdir,
4900 .mkdir_setinfo = smb2_mkdir_setinfo,
4901 .posix_mkdir = smb311_posix_mkdir,
4902 .rmdir = smb2_rmdir,
4903 .unlink = smb2_unlink,
4904 .rename = smb2_rename_path,
4905 .create_hardlink = smb2_create_hardlink,
4906 .query_symlink = smb2_query_symlink,
4907 .query_mf_symlink = smb3_query_mf_symlink,
4908 .create_mf_symlink = smb3_create_mf_symlink,
4909 .open = smb2_open_file,
4910 .set_fid = smb2_set_fid,
4911 .close = smb2_close_file,
4912 .flush = smb2_flush_file,
4913 .async_readv = smb2_async_readv,
4914 .async_writev = smb2_async_writev,
4915 .sync_read = smb2_sync_read,
4916 .sync_write = smb2_sync_write,
4917 .query_dir_first = smb2_query_dir_first,
4918 .query_dir_next = smb2_query_dir_next,
4919 .close_dir = smb2_close_dir,
4920 .calc_smb_size = smb2_calc_size,
4921 .is_status_pending = smb2_is_status_pending,
4922 .is_session_expired = smb2_is_session_expired,
4923 .oplock_response = smb2_oplock_response,
4924 .queryfs = smb311_queryfs,
4925 .mand_lock = smb2_mand_lock,
4926 .mand_unlock_range = smb2_unlock_range,
4927 .push_mand_locks = smb2_push_mandatory_locks,
4928 .get_lease_key = smb2_get_lease_key,
4929 .set_lease_key = smb2_set_lease_key,
4930 .new_lease_key = smb2_new_lease_key,
4931 .generate_signingkey = generate_smb311signingkey,
4932 .calc_signature = smb3_calc_signature,
4933 .set_integrity = smb3_set_integrity,
4934 .is_read_op = smb21_is_read_op,
4935 .set_oplock_level = smb3_set_oplock_level,
4936 .create_lease_buf = smb3_create_lease_buf,
4937 .parse_lease_buf = smb3_parse_lease_buf,
4938 .copychunk_range = smb2_copychunk_range,
4939 .duplicate_extents = smb2_duplicate_extents,
4940 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
4941 .wp_retry_size = smb2_wp_retry_size,
4942 .dir_needs_close = smb2_dir_needs_close,
4943 .fallocate = smb3_fallocate,
4944 .enum_snapshots = smb3_enum_snapshots,
4945 .init_transform_rq = smb3_init_transform_rq,
4946 .is_transform_hdr = smb3_is_transform_hdr,
4947 .receive_transform = smb3_receive_transform,
4948 .get_dfs_refer = smb2_get_dfs_refer,
4949 .select_sectype = smb2_select_sectype,
4950 #ifdef CONFIG_CIFS_XATTR
4951 .query_all_EAs = smb2_query_eas,
4952 .set_EA = smb2_set_ea,
4953 #endif /* CIFS_XATTR */
4954 .get_acl = get_smb2_acl,
4955 .get_acl_by_fid = get_smb2_acl_by_fid,
4956 .set_acl = set_smb2_acl,
4957 .next_header = smb2_next_header,
4958 .ioctl_query_info = smb2_ioctl_query_info,
4959 .make_node = smb2_make_node,
4960 .fiemap = smb3_fiemap,
4961 .llseek = smb3_llseek,
4964 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
4965 struct smb_version_values smb20_values = {
4966 .version_string = SMB20_VERSION_STRING,
4967 .protocol_id = SMB20_PROT_ID,
4968 .req_capabilities = 0, /* MBZ */
4969 .large_lock_type = 0,
4970 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4971 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4972 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4973 .header_size = sizeof(struct smb2_sync_hdr),
4974 .header_preamble_size = 0,
4975 .max_header_size = MAX_SMB2_HDR_SIZE,
4976 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4977 .lock_cmd = SMB2_LOCK,
4979 .cap_nt_find = SMB2_NT_FIND,
4980 .cap_large_files = SMB2_LARGE_FILES,
4981 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4982 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4983 .create_lease_size = sizeof(struct create_lease),
4985 #endif /* ALLOW_INSECURE_LEGACY */
4987 struct smb_version_values smb21_values = {
4988 .version_string = SMB21_VERSION_STRING,
4989 .protocol_id = SMB21_PROT_ID,
4990 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
4991 .large_lock_type = 0,
4992 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4993 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4994 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4995 .header_size = sizeof(struct smb2_sync_hdr),
4996 .header_preamble_size = 0,
4997 .max_header_size = MAX_SMB2_HDR_SIZE,
4998 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4999 .lock_cmd = SMB2_LOCK,
5001 .cap_nt_find = SMB2_NT_FIND,
5002 .cap_large_files = SMB2_LARGE_FILES,
5003 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5004 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5005 .create_lease_size = sizeof(struct create_lease),
5008 struct smb_version_values smb3any_values = {
5009 .version_string = SMB3ANY_VERSION_STRING,
5010 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5011 .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,
5012 .large_lock_type = 0,
5013 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5014 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5015 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5016 .header_size = sizeof(struct smb2_sync_hdr),
5017 .header_preamble_size = 0,
5018 .max_header_size = MAX_SMB2_HDR_SIZE,
5019 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5020 .lock_cmd = SMB2_LOCK,
5022 .cap_nt_find = SMB2_NT_FIND,
5023 .cap_large_files = SMB2_LARGE_FILES,
5024 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5025 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5026 .create_lease_size = sizeof(struct create_lease_v2),
5029 struct smb_version_values smbdefault_values = {
5030 .version_string = SMBDEFAULT_VERSION_STRING,
5031 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5032 .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,
5033 .large_lock_type = 0,
5034 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5035 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5036 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5037 .header_size = sizeof(struct smb2_sync_hdr),
5038 .header_preamble_size = 0,
5039 .max_header_size = MAX_SMB2_HDR_SIZE,
5040 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5041 .lock_cmd = SMB2_LOCK,
5043 .cap_nt_find = SMB2_NT_FIND,
5044 .cap_large_files = SMB2_LARGE_FILES,
5045 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5046 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5047 .create_lease_size = sizeof(struct create_lease_v2),
5050 struct smb_version_values smb30_values = {
5051 .version_string = SMB30_VERSION_STRING,
5052 .protocol_id = SMB30_PROT_ID,
5053 .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,
5054 .large_lock_type = 0,
5055 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5056 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5057 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5058 .header_size = sizeof(struct smb2_sync_hdr),
5059 .header_preamble_size = 0,
5060 .max_header_size = MAX_SMB2_HDR_SIZE,
5061 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5062 .lock_cmd = SMB2_LOCK,
5064 .cap_nt_find = SMB2_NT_FIND,
5065 .cap_large_files = SMB2_LARGE_FILES,
5066 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5067 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5068 .create_lease_size = sizeof(struct create_lease_v2),
5071 struct smb_version_values smb302_values = {
5072 .version_string = SMB302_VERSION_STRING,
5073 .protocol_id = SMB302_PROT_ID,
5074 .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,
5075 .large_lock_type = 0,
5076 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5077 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5078 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5079 .header_size = sizeof(struct smb2_sync_hdr),
5080 .header_preamble_size = 0,
5081 .max_header_size = MAX_SMB2_HDR_SIZE,
5082 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5083 .lock_cmd = SMB2_LOCK,
5085 .cap_nt_find = SMB2_NT_FIND,
5086 .cap_large_files = SMB2_LARGE_FILES,
5087 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5088 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5089 .create_lease_size = sizeof(struct create_lease_v2),
5092 struct smb_version_values smb311_values = {
5093 .version_string = SMB311_VERSION_STRING,
5094 .protocol_id = SMB311_PROT_ID,
5095 .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,
5096 .large_lock_type = 0,
5097 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5098 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5099 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5100 .header_size = sizeof(struct smb2_sync_hdr),
5101 .header_preamble_size = 0,
5102 .max_header_size = MAX_SMB2_HDR_SIZE,
5103 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5104 .lock_cmd = SMB2_LOCK,
5106 .cap_nt_find = SMB2_NT_FIND,
5107 .cap_large_files = SMB2_LARGE_FILES,
5108 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5109 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5110 .create_lease_size = sizeof(struct create_lease_v2),