1 // SPDX-License-Identifier: GPL-2.0
3 * SMB2 version specific operations
5 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
8 #include <linux/pagemap.h>
10 #include <linux/falloc.h>
11 #include <linux/scatterlist.h>
12 #include <linux/uuid.h>
13 #include <linux/sort.h>
14 #include <crypto/aead.h>
15 #include <linux/fiemap.h>
19 #include "smb2proto.h"
20 #include "cifsproto.h"
21 #include "cifs_debug.h"
22 #include "cifs_unicode.h"
23 #include "smb2status.h"
25 #include "cifs_ioctl.h"
26 #include "smbdirect.h"
28 /* Change credits for different ops and return the total number of credits */
30 change_conf(struct TCP_Server_Info *server)
32 server->credits += server->echo_credits + server->oplock_credits;
33 server->oplock_credits = server->echo_credits = 0;
34 switch (server->credits) {
38 server->echoes = false;
39 server->oplocks = false;
42 server->echoes = true;
43 server->oplocks = false;
44 server->echo_credits = 1;
47 server->echoes = true;
49 server->oplocks = true;
50 server->oplock_credits = 1;
52 server->oplocks = false;
54 server->echo_credits = 1;
56 server->credits -= server->echo_credits + server->oplock_credits;
57 return server->credits + server->echo_credits + server->oplock_credits;
61 smb2_add_credits(struct TCP_Server_Info *server,
62 const struct cifs_credits *credits, const int optype)
65 unsigned int add = credits->value;
66 unsigned int instance = credits->instance;
67 bool reconnect_detected = false;
69 spin_lock(&server->req_lock);
70 val = server->ops->get_credits_field(server, optype);
72 /* eg found case where write overlapping reconnect messed up credits */
73 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
74 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
75 server->hostname, *val);
76 if ((instance == 0) || (instance == server->reconnect_instance))
79 reconnect_detected = true;
82 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
83 pr_warn_once("server overflowed SMB3 credits\n");
86 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
87 rc = change_conf(server);
89 * Sometimes server returns 0 credits on oplock break ack - we need to
90 * rebalance credits in this case.
92 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
94 if (server->credits > 1) {
96 server->oplock_credits++;
99 spin_unlock(&server->req_lock);
100 wake_up(&server->request_q);
102 if (reconnect_detected)
103 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
106 if (server->tcpStatus == CifsNeedReconnect
107 || server->tcpStatus == CifsExiting)
112 /* change_conf hasn't been executed */
115 cifs_server_dbg(VFS, "Possible client or server bug - zero credits\n");
118 cifs_server_dbg(VFS, "disabling echoes and oplocks\n");
121 cifs_dbg(FYI, "disabling oplocks\n");
124 cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
129 smb2_set_credits(struct TCP_Server_Info *server, const int val)
131 spin_lock(&server->req_lock);
132 server->credits = val;
134 server->reconnect_instance++;
135 spin_unlock(&server->req_lock);
136 /* don't log while holding the lock */
138 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
142 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
146 return &server->echo_credits;
148 return &server->oplock_credits;
150 return &server->credits;
155 smb2_get_credits(struct mid_q_entry *mid)
157 return mid->credits_received;
161 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
162 unsigned int *num, struct cifs_credits *credits)
165 unsigned int scredits;
167 spin_lock(&server->req_lock);
169 if (server->credits <= 0) {
170 spin_unlock(&server->req_lock);
171 cifs_num_waiters_inc(server);
172 rc = wait_event_killable(server->request_q,
173 has_credits(server, &server->credits, 1));
174 cifs_num_waiters_dec(server);
177 spin_lock(&server->req_lock);
179 if (server->tcpStatus == CifsExiting) {
180 spin_unlock(&server->req_lock);
184 scredits = server->credits;
185 /* can deadlock with reopen */
187 *num = SMB2_MAX_BUFFER_SIZE;
189 credits->instance = 0;
193 /* leave some credits for reopen and other ops */
195 *num = min_t(unsigned int, size,
196 scredits * SMB2_MAX_BUFFER_SIZE);
199 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
200 credits->instance = server->reconnect_instance;
201 server->credits -= credits->value;
203 if (server->in_flight > server->max_in_flight)
204 server->max_in_flight = server->in_flight;
208 spin_unlock(&server->req_lock);
213 smb2_adjust_credits(struct TCP_Server_Info *server,
214 struct cifs_credits *credits,
215 const unsigned int payload_size)
217 int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
219 if (!credits->value || credits->value == new_val)
222 if (credits->value < new_val) {
223 WARN_ONCE(1, "request has less credits (%d) than required (%d)",
224 credits->value, new_val);
228 spin_lock(&server->req_lock);
230 if (server->reconnect_instance != credits->instance) {
231 spin_unlock(&server->req_lock);
232 cifs_server_dbg(VFS, "trying to return %d credits to old session\n",
233 credits->value - new_val);
237 server->credits += credits->value - new_val;
238 spin_unlock(&server->req_lock);
239 wake_up(&server->request_q);
240 credits->value = new_val;
245 smb2_get_next_mid(struct TCP_Server_Info *server)
248 /* for SMB2 we need the current value */
249 spin_lock(&GlobalMid_Lock);
250 mid = server->CurrentMid++;
251 spin_unlock(&GlobalMid_Lock);
256 smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
258 spin_lock(&GlobalMid_Lock);
259 if (server->CurrentMid >= val)
260 server->CurrentMid -= val;
261 spin_unlock(&GlobalMid_Lock);
264 static struct mid_q_entry *
265 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
267 struct mid_q_entry *mid;
268 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
269 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
271 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
272 cifs_server_dbg(VFS, "Encrypted frame parsing not supported yet\n");
276 spin_lock(&GlobalMid_Lock);
277 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
278 if ((mid->mid == wire_mid) &&
279 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
280 (mid->command == shdr->Command)) {
281 kref_get(&mid->refcount);
282 spin_unlock(&GlobalMid_Lock);
286 spin_unlock(&GlobalMid_Lock);
291 smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
293 #ifdef CONFIG_CIFS_DEBUG2
294 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
296 cifs_server_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
297 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
299 cifs_server_dbg(VFS, "smb buf %p len %u\n", buf,
300 server->ops->calc_smb_size(buf, server));
305 smb2_need_neg(struct TCP_Server_Info *server)
307 return server->max_read == 0;
311 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
315 cifs_ses_server(ses)->CurrentMid = 0;
316 rc = SMB2_negotiate(xid, ses);
317 /* BB we probably don't need to retry with modern servers */
324 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
326 struct TCP_Server_Info *server = tcon->ses->server;
329 /* start with specified wsize, or default */
330 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
331 wsize = min_t(unsigned int, wsize, server->max_write);
332 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
333 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
339 smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
341 struct TCP_Server_Info *server = tcon->ses->server;
344 /* start with specified wsize, or default */
345 wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
346 wsize = min_t(unsigned int, wsize, server->max_write);
347 #ifdef CONFIG_CIFS_SMB_DIRECT
351 * Account for SMB2 data transfer packet header and
352 * possible encryption header
354 wsize = min_t(unsigned int,
356 server->smbd_conn->max_fragmented_send_size -
357 SMB2_READWRITE_PDU_HEADER_SIZE -
358 sizeof(struct smb2_transform_hdr));
360 wsize = min_t(unsigned int,
361 wsize, server->smbd_conn->max_readwrite_size);
364 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
365 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
371 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
373 struct TCP_Server_Info *server = tcon->ses->server;
376 /* start with specified rsize, or default */
377 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
378 rsize = min_t(unsigned int, rsize, server->max_read);
380 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
381 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
387 smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
389 struct TCP_Server_Info *server = tcon->ses->server;
392 /* start with specified rsize, or default */
393 rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
394 rsize = min_t(unsigned int, rsize, server->max_read);
395 #ifdef CONFIG_CIFS_SMB_DIRECT
399 * Account for SMB2 data transfer packet header and
400 * possible encryption header
402 rsize = min_t(unsigned int,
404 server->smbd_conn->max_fragmented_recv_size -
405 SMB2_READWRITE_PDU_HEADER_SIZE -
406 sizeof(struct smb2_transform_hdr));
408 rsize = min_t(unsigned int,
409 rsize, server->smbd_conn->max_readwrite_size);
413 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
414 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
420 parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
422 struct cifs_server_iface **iface_list,
425 struct network_interface_info_ioctl_rsp *p;
426 struct sockaddr_in *addr4;
427 struct sockaddr_in6 *addr6;
428 struct iface_info_ipv4 *p4;
429 struct iface_info_ipv6 *p6;
430 struct cifs_server_iface *info;
440 * Fist pass: count and sanity check
443 bytes_left = buf_len;
445 while (bytes_left >= sizeof(*p)) {
447 next = le32_to_cpu(p->Next);
449 bytes_left -= sizeof(*p);
452 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
457 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
462 if (bytes_left || p->Next)
463 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
467 * Second pass: extract info to internal structure
470 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
477 bytes_left = buf_len;
479 while (bytes_left >= sizeof(*p)) {
480 info->speed = le64_to_cpu(p->LinkSpeed);
481 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE);
482 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE);
484 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
485 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
486 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
487 le32_to_cpu(p->Capability));
491 * The kernel and wire socket structures have the same
492 * layout and use network byte order but make the
493 * conversion explicit in case either one changes.
496 addr4 = (struct sockaddr_in *)&info->sockaddr;
497 p4 = (struct iface_info_ipv4 *)p->Buffer;
498 addr4->sin_family = AF_INET;
499 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
501 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
502 addr4->sin_port = cpu_to_be16(CIFS_PORT);
504 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
508 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
509 p6 = (struct iface_info_ipv6 *)p->Buffer;
510 addr6->sin6_family = AF_INET6;
511 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
513 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
514 addr6->sin6_flowinfo = 0;
515 addr6->sin6_scope_id = 0;
516 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
518 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
523 "%s: skipping unsupported socket family\n",
531 next = le32_to_cpu(p->Next);
534 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
552 static int compare_iface(const void *ia, const void *ib)
554 const struct cifs_server_iface *a = (struct cifs_server_iface *)ia;
555 const struct cifs_server_iface *b = (struct cifs_server_iface *)ib;
557 return a->speed == b->speed ? 0 : (a->speed > b->speed ? -1 : 1);
561 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
564 unsigned int ret_data_len = 0;
565 struct network_interface_info_ioctl_rsp *out_buf = NULL;
566 struct cifs_server_iface *iface_list;
568 struct cifs_ses *ses = tcon->ses;
570 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
571 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
572 NULL /* no data input */, 0 /* no data input */,
573 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
574 if (rc == -EOPNOTSUPP) {
576 "server does not support query network interfaces\n");
578 } else if (rc != 0) {
579 cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
583 rc = parse_server_interfaces(out_buf, ret_data_len,
584 &iface_list, &iface_count);
588 /* sort interfaces from fastest to slowest */
589 sort(iface_list, iface_count, sizeof(*iface_list), compare_iface, NULL);
591 spin_lock(&ses->iface_lock);
592 kfree(ses->iface_list);
593 ses->iface_list = iface_list;
594 ses->iface_count = iface_count;
595 ses->iface_last_update = jiffies;
596 spin_unlock(&ses->iface_lock);
604 smb2_close_cached_fid(struct kref *ref)
606 struct cached_fid *cfid = container_of(ref, struct cached_fid,
609 if (cfid->is_valid) {
610 cifs_dbg(FYI, "clear cached root file handle\n");
611 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
612 cfid->fid->volatile_fid);
613 cfid->is_valid = false;
614 cfid->file_all_info_is_valid = false;
615 cfid->has_lease = false;
619 void close_shroot(struct cached_fid *cfid)
621 mutex_lock(&cfid->fid_mutex);
622 kref_put(&cfid->refcount, smb2_close_cached_fid);
623 mutex_unlock(&cfid->fid_mutex);
626 void close_shroot_lease_locked(struct cached_fid *cfid)
628 if (cfid->has_lease) {
629 cfid->has_lease = false;
630 kref_put(&cfid->refcount, smb2_close_cached_fid);
634 void close_shroot_lease(struct cached_fid *cfid)
636 mutex_lock(&cfid->fid_mutex);
637 close_shroot_lease_locked(cfid);
638 mutex_unlock(&cfid->fid_mutex);
642 smb2_cached_lease_break(struct work_struct *work)
644 struct cached_fid *cfid = container_of(work,
645 struct cached_fid, lease_break);
647 close_shroot_lease(cfid);
651 * Open the directory at the root of a share
653 int open_shroot(unsigned int xid, struct cifs_tcon *tcon,
654 struct cifs_sb_info *cifs_sb,
655 struct cached_fid **cfid)
657 struct cifs_ses *ses = tcon->ses;
658 struct TCP_Server_Info *server = ses->server;
659 struct cifs_open_parms oparms;
660 struct smb2_create_rsp *o_rsp = NULL;
661 struct smb2_query_info_rsp *qi_rsp = NULL;
663 struct smb_rqst rqst[2];
664 struct kvec rsp_iov[2];
665 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
666 struct kvec qi_iov[1];
668 __le16 utf16_path = 0; /* Null - since an open of top of share */
669 u8 oplock = SMB2_OPLOCK_LEVEL_II;
670 struct cifs_fid *pfid;
672 mutex_lock(&tcon->crfid.fid_mutex);
673 if (tcon->crfid.is_valid) {
674 cifs_dbg(FYI, "found a cached root file handle\n");
675 *cfid = &tcon->crfid;
676 kref_get(&tcon->crfid.refcount);
677 mutex_unlock(&tcon->crfid.fid_mutex);
682 * We do not hold the lock for the open because in case
683 * SMB2_open needs to reconnect, it will end up calling
684 * cifs_mark_open_files_invalid() which takes the lock again
685 * thus causing a deadlock
688 mutex_unlock(&tcon->crfid.fid_mutex);
690 if (smb3_encryption_required(tcon))
691 flags |= CIFS_TRANSFORM_REQ;
693 if (!server->ops->new_lease_key)
696 pfid = tcon->crfid.fid;
697 server->ops->new_lease_key(pfid);
699 memset(rqst, 0, sizeof(rqst));
700 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
701 memset(rsp_iov, 0, sizeof(rsp_iov));
704 memset(&open_iov, 0, sizeof(open_iov));
705 rqst[0].rq_iov = open_iov;
706 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
709 oparms.create_options = cifs_create_options(cifs_sb, 0);
710 oparms.desired_access = FILE_READ_ATTRIBUTES;
711 oparms.disposition = FILE_OPEN;
713 oparms.reconnect = false;
715 rc = SMB2_open_init(tcon, server,
716 &rqst[0], &oplock, &oparms, &utf16_path);
719 smb2_set_next_command(tcon, &rqst[0]);
721 memset(&qi_iov, 0, sizeof(qi_iov));
722 rqst[1].rq_iov = qi_iov;
725 rc = SMB2_query_info_init(tcon, server,
726 &rqst[1], COMPOUND_FID,
727 COMPOUND_FID, FILE_ALL_INFORMATION,
729 sizeof(struct smb2_file_all_info) +
730 PATH_MAX * 2, 0, NULL);
734 smb2_set_related(&rqst[1]);
736 rc = compound_send_recv(xid, ses, server,
738 resp_buftype, rsp_iov);
739 mutex_lock(&tcon->crfid.fid_mutex);
742 * Now we need to check again as the cached root might have
743 * been successfully re-opened from a concurrent process
746 if (tcon->crfid.is_valid) {
747 /* work was already done */
749 /* stash fids for close() later */
750 struct cifs_fid fid = {
751 .persistent_fid = pfid->persistent_fid,
752 .volatile_fid = pfid->volatile_fid,
756 * caller expects this func to set pfid to a valid
757 * cached root, so we copy the existing one and get a
760 memcpy(pfid, tcon->crfid.fid, sizeof(*pfid));
761 kref_get(&tcon->crfid.refcount);
763 mutex_unlock(&tcon->crfid.fid_mutex);
766 /* close extra handle outside of crit sec */
767 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
773 /* Cached root is still invalid, continue normaly */
776 if (rc == -EREMCHG) {
777 tcon->need_reconnect = true;
778 pr_warn_once("server share %s deleted\n",
784 atomic_inc(&tcon->num_remote_opens);
786 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
787 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
788 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
789 #ifdef CONFIG_CIFS_DEBUG2
790 oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
791 #endif /* CIFS_DEBUG2 */
793 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
794 tcon->crfid.tcon = tcon;
795 tcon->crfid.is_valid = true;
796 kref_init(&tcon->crfid.refcount);
798 /* BB TBD check to see if oplock level check can be removed below */
799 if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
800 kref_get(&tcon->crfid.refcount);
801 tcon->crfid.has_lease = true;
802 smb2_parse_contexts(server, o_rsp,
804 oparms.fid->lease_key, &oplock,
809 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
810 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
812 if (!smb2_validate_and_copy_iov(
813 le16_to_cpu(qi_rsp->OutputBufferOffset),
814 sizeof(struct smb2_file_all_info),
815 &rsp_iov[1], sizeof(struct smb2_file_all_info),
816 (char *)&tcon->crfid.file_all_info))
817 tcon->crfid.file_all_info_is_valid = true;
820 mutex_unlock(&tcon->crfid.fid_mutex);
822 SMB2_open_free(&rqst[0]);
823 SMB2_query_info_free(&rqst[1]);
824 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
825 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
827 *cfid = &tcon->crfid;
832 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
833 struct cifs_sb_info *cifs_sb)
836 __le16 srch_path = 0; /* Null - open root of share */
837 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
838 struct cifs_open_parms oparms;
840 bool no_cached_open = tcon->nohandlecache;
841 struct cached_fid *cfid = NULL;
844 oparms.desired_access = FILE_READ_ATTRIBUTES;
845 oparms.disposition = FILE_OPEN;
846 oparms.create_options = cifs_create_options(cifs_sb, 0);
848 oparms.reconnect = false;
850 if (no_cached_open) {
851 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
854 rc = open_shroot(xid, tcon, cifs_sb, &cfid);
856 memcpy(&fid, cfid->fid, sizeof(struct cifs_fid));
861 SMB3_request_interfaces(xid, tcon);
863 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
864 FS_ATTRIBUTE_INFORMATION);
865 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
866 FS_DEVICE_INFORMATION);
867 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
868 FS_VOLUME_INFORMATION);
869 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
870 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
872 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
878 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
879 struct cifs_sb_info *cifs_sb)
882 __le16 srch_path = 0; /* Null - open root of share */
883 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
884 struct cifs_open_parms oparms;
888 oparms.desired_access = FILE_READ_ATTRIBUTES;
889 oparms.disposition = FILE_OPEN;
890 oparms.create_options = cifs_create_options(cifs_sb, 0);
892 oparms.reconnect = false;
894 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
899 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
900 FS_ATTRIBUTE_INFORMATION);
901 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
902 FS_DEVICE_INFORMATION);
903 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
907 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
908 struct cifs_sb_info *cifs_sb, const char *full_path)
912 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
913 struct cifs_open_parms oparms;
916 if ((*full_path == 0) && tcon->crfid.is_valid)
919 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
924 oparms.desired_access = FILE_READ_ATTRIBUTES;
925 oparms.disposition = FILE_OPEN;
926 oparms.create_options = cifs_create_options(cifs_sb, 0);
928 oparms.reconnect = false;
930 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
937 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
943 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
944 struct cifs_sb_info *cifs_sb, const char *full_path,
945 u64 *uniqueid, FILE_ALL_INFO *data)
947 *uniqueid = le64_to_cpu(data->IndexNumber);
952 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
953 struct cifs_fid *fid, FILE_ALL_INFO *data)
956 struct smb2_file_all_info *smb2_data;
958 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
960 if (smb2_data == NULL)
963 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
966 move_smb2_info_to_cifs(data, smb2_data);
971 #ifdef CONFIG_CIFS_XATTR
973 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
974 struct smb2_file_full_ea_info *src, size_t src_size,
975 const unsigned char *ea_name)
978 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
980 size_t buf_size = dst_size;
981 size_t name_len, value_len, user_name_len;
983 while (src_size > 0) {
984 name = &src->ea_data[0];
985 name_len = (size_t)src->ea_name_length;
986 value = &src->ea_data[src->ea_name_length + 1];
987 value_len = (size_t)le16_to_cpu(src->ea_value_length);
992 if (src_size < 8 + name_len + 1 + value_len) {
993 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
999 if (ea_name_len == name_len &&
1000 memcmp(ea_name, name, name_len) == 0) {
1004 if (dst_size < value_len) {
1008 memcpy(dst, value, value_len);
1012 /* 'user.' plus a terminating null */
1013 user_name_len = 5 + 1 + name_len;
1015 if (buf_size == 0) {
1016 /* skip copy - calc size only */
1017 rc += user_name_len;
1018 } else if (dst_size >= user_name_len) {
1019 dst_size -= user_name_len;
1020 memcpy(dst, "user.", 5);
1022 memcpy(dst, src->ea_data, name_len);
1026 rc += user_name_len;
1028 /* stop before overrun buffer */
1034 if (!src->next_entry_offset)
1037 if (src_size < le32_to_cpu(src->next_entry_offset)) {
1038 /* stop before overrun buffer */
1042 src_size -= le32_to_cpu(src->next_entry_offset);
1043 src = (void *)((char *)src +
1044 le32_to_cpu(src->next_entry_offset));
1047 /* didn't find the named attribute */
1056 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1057 const unsigned char *path, const unsigned char *ea_name,
1058 char *ea_data, size_t buf_size,
1059 struct cifs_sb_info *cifs_sb)
1063 struct kvec rsp_iov = {NULL, 0};
1064 int buftype = CIFS_NO_BUFFER;
1065 struct smb2_query_info_rsp *rsp;
1066 struct smb2_file_full_ea_info *info = NULL;
1068 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1072 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1074 FILE_FULL_EA_INFORMATION,
1077 MAX_SMB2_CREATE_RESPONSE_SIZE -
1078 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1079 &rsp_iov, &buftype, cifs_sb);
1082 * If ea_name is NULL (listxattr) and there are no EAs,
1083 * return 0 as it's not an error. Otherwise, the specified
1084 * ea_name was not found.
1086 if (!ea_name && rc == -ENODATA)
1091 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1092 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1093 le32_to_cpu(rsp->OutputBufferLength),
1095 sizeof(struct smb2_file_full_ea_info));
1099 info = (struct smb2_file_full_ea_info *)(
1100 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1101 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1102 le32_to_cpu(rsp->OutputBufferLength), ea_name);
1106 free_rsp_buf(buftype, rsp_iov.iov_base);
1112 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1113 const char *path, const char *ea_name, const void *ea_value,
1114 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1115 struct cifs_sb_info *cifs_sb)
1117 struct cifs_ses *ses = tcon->ses;
1118 struct TCP_Server_Info *server = cifs_pick_channel(ses);
1119 __le16 *utf16_path = NULL;
1120 int ea_name_len = strlen(ea_name);
1123 struct smb_rqst rqst[3];
1124 int resp_buftype[3];
1125 struct kvec rsp_iov[3];
1126 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1127 struct cifs_open_parms oparms;
1128 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1129 struct cifs_fid fid;
1130 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1131 unsigned int size[1];
1133 struct smb2_file_full_ea_info *ea = NULL;
1134 struct kvec close_iov[1];
1135 struct smb2_query_info_rsp *rsp;
1136 int rc, used_len = 0;
1138 if (smb3_encryption_required(tcon))
1139 flags |= CIFS_TRANSFORM_REQ;
1141 if (ea_name_len > 255)
1144 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1148 memset(rqst, 0, sizeof(rqst));
1149 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1150 memset(rsp_iov, 0, sizeof(rsp_iov));
1152 if (ses->server->ops->query_all_EAs) {
1154 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1160 /* If we are adding a attribute we should first check
1161 * if there will be enough space available to store
1162 * the new EA. If not we should not add it since we
1163 * would not be able to even read the EAs back.
1165 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1167 FILE_FULL_EA_INFORMATION,
1170 MAX_SMB2_CREATE_RESPONSE_SIZE -
1171 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1172 &rsp_iov[1], &resp_buftype[1], cifs_sb);
1174 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1175 used_len = le32_to_cpu(rsp->OutputBufferLength);
1177 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1178 resp_buftype[1] = CIFS_NO_BUFFER;
1179 memset(&rsp_iov[1], 0, sizeof(rsp_iov[1]));
1182 /* Use a fudge factor of 256 bytes in case we collide
1183 * with a different set_EAs command.
1185 if(CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1186 MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
1187 used_len + ea_name_len + ea_value_len + 1) {
1195 memset(&open_iov, 0, sizeof(open_iov));
1196 rqst[0].rq_iov = open_iov;
1197 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1199 memset(&oparms, 0, sizeof(oparms));
1201 oparms.desired_access = FILE_WRITE_EA;
1202 oparms.disposition = FILE_OPEN;
1203 oparms.create_options = cifs_create_options(cifs_sb, 0);
1205 oparms.reconnect = false;
1207 rc = SMB2_open_init(tcon, server,
1208 &rqst[0], &oplock, &oparms, utf16_path);
1211 smb2_set_next_command(tcon, &rqst[0]);
1215 memset(&si_iov, 0, sizeof(si_iov));
1216 rqst[1].rq_iov = si_iov;
1217 rqst[1].rq_nvec = 1;
1219 len = sizeof(*ea) + ea_name_len + ea_value_len + 1;
1220 ea = kzalloc(len, GFP_KERNEL);
1226 ea->ea_name_length = ea_name_len;
1227 ea->ea_value_length = cpu_to_le16(ea_value_len);
1228 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1229 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1234 rc = SMB2_set_info_init(tcon, server,
1235 &rqst[1], COMPOUND_FID,
1236 COMPOUND_FID, current->tgid,
1237 FILE_FULL_EA_INFORMATION,
1238 SMB2_O_INFO_FILE, 0, data, size);
1239 smb2_set_next_command(tcon, &rqst[1]);
1240 smb2_set_related(&rqst[1]);
1244 memset(&close_iov, 0, sizeof(close_iov));
1245 rqst[2].rq_iov = close_iov;
1246 rqst[2].rq_nvec = 1;
1247 rc = SMB2_close_init(tcon, server,
1248 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1249 smb2_set_related(&rqst[2]);
1251 rc = compound_send_recv(xid, ses, server,
1253 resp_buftype, rsp_iov);
1254 /* no need to bump num_remote_opens because handle immediately closed */
1259 SMB2_open_free(&rqst[0]);
1260 SMB2_set_info_free(&rqst[1]);
1261 SMB2_close_free(&rqst[2]);
1262 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1263 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1264 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1270 smb2_can_echo(struct TCP_Server_Info *server)
1272 return server->echoes;
1276 smb2_clear_stats(struct cifs_tcon *tcon)
1280 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1281 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1282 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1287 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1289 seq_puts(m, "\n\tShare Capabilities:");
1290 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1291 seq_puts(m, " DFS,");
1292 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1293 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1294 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1295 seq_puts(m, " SCALEOUT,");
1296 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1297 seq_puts(m, " CLUSTER,");
1298 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1299 seq_puts(m, " ASYMMETRIC,");
1300 if (tcon->capabilities == 0)
1301 seq_puts(m, " None");
1302 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1303 seq_puts(m, " Aligned,");
1304 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1305 seq_puts(m, " Partition Aligned,");
1306 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1307 seq_puts(m, " SSD,");
1308 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1309 seq_puts(m, " TRIM-support,");
1311 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1312 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1313 if (tcon->perf_sector_size)
1314 seq_printf(m, "\tOptimal sector size: 0x%x",
1315 tcon->perf_sector_size);
1316 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1320 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1322 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1323 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1326 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1327 * totals (requests sent) since those SMBs are per-session not per tcon
1329 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1330 (long long)(tcon->bytes_read),
1331 (long long)(tcon->bytes_written));
1332 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1333 atomic_read(&tcon->num_local_opens),
1334 atomic_read(&tcon->num_remote_opens));
1335 seq_printf(m, "\nTreeConnects: %d total %d failed",
1336 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1337 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1338 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1339 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1340 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1341 seq_printf(m, "\nCreates: %d total %d failed",
1342 atomic_read(&sent[SMB2_CREATE_HE]),
1343 atomic_read(&failed[SMB2_CREATE_HE]));
1344 seq_printf(m, "\nCloses: %d total %d failed",
1345 atomic_read(&sent[SMB2_CLOSE_HE]),
1346 atomic_read(&failed[SMB2_CLOSE_HE]));
1347 seq_printf(m, "\nFlushes: %d total %d failed",
1348 atomic_read(&sent[SMB2_FLUSH_HE]),
1349 atomic_read(&failed[SMB2_FLUSH_HE]));
1350 seq_printf(m, "\nReads: %d total %d failed",
1351 atomic_read(&sent[SMB2_READ_HE]),
1352 atomic_read(&failed[SMB2_READ_HE]));
1353 seq_printf(m, "\nWrites: %d total %d failed",
1354 atomic_read(&sent[SMB2_WRITE_HE]),
1355 atomic_read(&failed[SMB2_WRITE_HE]));
1356 seq_printf(m, "\nLocks: %d total %d failed",
1357 atomic_read(&sent[SMB2_LOCK_HE]),
1358 atomic_read(&failed[SMB2_LOCK_HE]));
1359 seq_printf(m, "\nIOCTLs: %d total %d failed",
1360 atomic_read(&sent[SMB2_IOCTL_HE]),
1361 atomic_read(&failed[SMB2_IOCTL_HE]));
1362 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1363 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1364 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1365 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1366 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1367 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1368 seq_printf(m, "\nQueryInfos: %d total %d failed",
1369 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1370 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1371 seq_printf(m, "\nSetInfos: %d total %d failed",
1372 atomic_read(&sent[SMB2_SET_INFO_HE]),
1373 atomic_read(&failed[SMB2_SET_INFO_HE]));
1374 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1375 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1376 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1380 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1382 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1383 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1385 cfile->fid.persistent_fid = fid->persistent_fid;
1386 cfile->fid.volatile_fid = fid->volatile_fid;
1387 cfile->fid.access = fid->access;
1388 #ifdef CONFIG_CIFS_DEBUG2
1389 cfile->fid.mid = fid->mid;
1390 #endif /* CIFS_DEBUG2 */
1391 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1393 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1394 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1398 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1399 struct cifs_fid *fid)
1401 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1405 smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
1406 struct cifsFileInfo *cfile)
1408 struct smb2_file_network_open_info file_inf;
1409 struct inode *inode;
1412 rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
1413 cfile->fid.volatile_fid, &file_inf);
1417 inode = d_inode(cfile->dentry);
1419 spin_lock(&inode->i_lock);
1420 CIFS_I(inode)->time = jiffies;
1422 /* Creation time should not need to be updated on close */
1423 if (file_inf.LastWriteTime)
1424 inode->i_mtime = cifs_NTtimeToUnix(file_inf.LastWriteTime);
1425 if (file_inf.ChangeTime)
1426 inode->i_ctime = cifs_NTtimeToUnix(file_inf.ChangeTime);
1427 if (file_inf.LastAccessTime)
1428 inode->i_atime = cifs_NTtimeToUnix(file_inf.LastAccessTime);
1431 * i_blocks is not related to (i_size / i_blksize),
1432 * but instead 512 byte (2**9) size is required for
1433 * calculating num blocks.
1435 if (le64_to_cpu(file_inf.AllocationSize) > 4096)
1437 (512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
1439 /* End of file and Attributes should not have to be updated on close */
1440 spin_unlock(&inode->i_lock);
1444 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1445 u64 persistent_fid, u64 volatile_fid,
1446 struct copychunk_ioctl *pcchunk)
1449 unsigned int ret_data_len;
1450 struct resume_key_req *res_key;
1452 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1453 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1454 NULL, 0 /* no input */, CIFSMaxBufSize,
1455 (char **)&res_key, &ret_data_len);
1458 cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1459 goto req_res_key_exit;
1461 if (ret_data_len < sizeof(struct resume_key_req)) {
1462 cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
1464 goto req_res_key_exit;
1466 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1474 struct smb_rqst rqst[3];
1475 struct kvec rsp_iov[3];
1476 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1477 struct kvec qi_iov[1];
1478 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1479 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1480 struct kvec close_iov[1];
1484 smb2_ioctl_query_info(const unsigned int xid,
1485 struct cifs_tcon *tcon,
1486 struct cifs_sb_info *cifs_sb,
1487 __le16 *path, int is_dir,
1490 struct iqi_vars *vars;
1491 struct smb_rqst *rqst;
1492 struct kvec *rsp_iov;
1493 struct cifs_ses *ses = tcon->ses;
1494 struct TCP_Server_Info *server = cifs_pick_channel(ses);
1495 char __user *arg = (char __user *)p;
1496 struct smb_query_info qi;
1497 struct smb_query_info __user *pqi;
1500 struct smb2_query_info_rsp *qi_rsp = NULL;
1501 struct smb2_ioctl_rsp *io_rsp = NULL;
1502 void *buffer = NULL;
1503 int resp_buftype[3];
1504 struct cifs_open_parms oparms;
1505 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1506 struct cifs_fid fid;
1507 unsigned int size[2];
1509 int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
1511 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
1514 rqst = &vars->rqst[0];
1515 rsp_iov = &vars->rsp_iov[0];
1517 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1519 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1522 if (qi.output_buffer_length > 1024) {
1527 if (!ses || !server) {
1532 if (smb3_encryption_required(tcon))
1533 flags |= CIFS_TRANSFORM_REQ;
1535 buffer = memdup_user(arg + sizeof(struct smb_query_info),
1536 qi.output_buffer_length);
1537 if (IS_ERR(buffer)) {
1539 return PTR_ERR(buffer);
1543 rqst[0].rq_iov = &vars->open_iov[0];
1544 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1546 memset(&oparms, 0, sizeof(oparms));
1548 oparms.disposition = FILE_OPEN;
1549 oparms.create_options = cifs_create_options(cifs_sb, create_options);
1551 oparms.reconnect = false;
1553 if (qi.flags & PASSTHRU_FSCTL) {
1554 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1555 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1556 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1558 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1559 oparms.desired_access = GENERIC_ALL;
1561 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1562 oparms.desired_access = GENERIC_READ;
1564 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1565 oparms.desired_access = GENERIC_WRITE;
1568 } else if (qi.flags & PASSTHRU_SET_INFO) {
1569 oparms.desired_access = GENERIC_WRITE;
1571 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1574 rc = SMB2_open_init(tcon, server,
1575 &rqst[0], &oplock, &oparms, path);
1578 smb2_set_next_command(tcon, &rqst[0]);
1581 if (qi.flags & PASSTHRU_FSCTL) {
1582 /* Can eventually relax perm check since server enforces too */
1583 if (!capable(CAP_SYS_ADMIN))
1586 rqst[1].rq_iov = &vars->io_iov[0];
1587 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1589 rc = SMB2_ioctl_init(tcon, server,
1591 COMPOUND_FID, COMPOUND_FID,
1592 qi.info_type, true, buffer,
1593 qi.output_buffer_length,
1595 MAX_SMB2_CREATE_RESPONSE_SIZE -
1596 MAX_SMB2_CLOSE_RESPONSE_SIZE);
1598 } else if (qi.flags == PASSTHRU_SET_INFO) {
1599 /* Can eventually relax perm check since server enforces too */
1600 if (!capable(CAP_SYS_ADMIN))
1603 rqst[1].rq_iov = &vars->si_iov[0];
1604 rqst[1].rq_nvec = 1;
1609 rc = SMB2_set_info_init(tcon, server,
1611 COMPOUND_FID, COMPOUND_FID,
1613 FILE_END_OF_FILE_INFORMATION,
1614 SMB2_O_INFO_FILE, 0, data, size);
1616 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1617 rqst[1].rq_iov = &vars->qi_iov[0];
1618 rqst[1].rq_nvec = 1;
1620 rc = SMB2_query_info_init(tcon, server,
1621 &rqst[1], COMPOUND_FID,
1622 COMPOUND_FID, qi.file_info_class,
1623 qi.info_type, qi.additional_information,
1624 qi.input_buffer_length,
1625 qi.output_buffer_length, buffer);
1626 } else { /* unknown flags */
1627 cifs_tcon_dbg(VFS, "Invalid passthru query flags: 0x%x\n",
1634 smb2_set_next_command(tcon, &rqst[1]);
1635 smb2_set_related(&rqst[1]);
1638 rqst[2].rq_iov = &vars->close_iov[0];
1639 rqst[2].rq_nvec = 1;
1641 rc = SMB2_close_init(tcon, server,
1642 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1645 smb2_set_related(&rqst[2]);
1647 rc = compound_send_recv(xid, ses, server,
1649 resp_buftype, rsp_iov);
1653 /* No need to bump num_remote_opens since handle immediately closed */
1654 if (qi.flags & PASSTHRU_FSCTL) {
1655 pqi = (struct smb_query_info __user *)arg;
1656 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1657 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1658 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1659 if (qi.input_buffer_length > 0 &&
1660 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1661 > rsp_iov[1].iov_len)
1664 if (copy_to_user(&pqi->input_buffer_length,
1665 &qi.input_buffer_length,
1666 sizeof(qi.input_buffer_length)))
1669 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1670 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1671 qi.input_buffer_length))
1674 pqi = (struct smb_query_info __user *)arg;
1675 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1676 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1677 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1678 if (copy_to_user(&pqi->input_buffer_length,
1679 &qi.input_buffer_length,
1680 sizeof(qi.input_buffer_length)))
1683 if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1684 qi.input_buffer_length))
1691 SMB2_open_free(&rqst[0]);
1692 if (qi.flags & PASSTHRU_FSCTL)
1693 SMB2_ioctl_free(&rqst[1]);
1695 SMB2_query_info_free(&rqst[1]);
1697 SMB2_close_free(&rqst[2]);
1698 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1699 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1700 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1709 smb2_copychunk_range(const unsigned int xid,
1710 struct cifsFileInfo *srcfile,
1711 struct cifsFileInfo *trgtfile, u64 src_off,
1712 u64 len, u64 dest_off)
1715 unsigned int ret_data_len;
1716 struct copychunk_ioctl *pcchunk;
1717 struct copychunk_ioctl_rsp *retbuf = NULL;
1718 struct cifs_tcon *tcon;
1719 int chunks_copied = 0;
1720 bool chunk_sizes_updated = false;
1721 ssize_t bytes_written, total_bytes_written = 0;
1723 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1725 if (pcchunk == NULL)
1728 cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
1729 /* Request a key from the server to identify the source of the copy */
1730 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1731 srcfile->fid.persistent_fid,
1732 srcfile->fid.volatile_fid, pcchunk);
1734 /* Note: request_res_key sets res_key null only if rc !=0 */
1738 /* For now array only one chunk long, will make more flexible later */
1739 pcchunk->ChunkCount = cpu_to_le32(1);
1740 pcchunk->Reserved = 0;
1741 pcchunk->Reserved2 = 0;
1743 tcon = tlink_tcon(trgtfile->tlink);
1746 pcchunk->SourceOffset = cpu_to_le64(src_off);
1747 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1749 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1751 /* Request server copy to target from src identified by key */
1752 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1753 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1754 true /* is_fsctl */, (char *)pcchunk,
1755 sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1756 (char **)&retbuf, &ret_data_len);
1759 sizeof(struct copychunk_ioctl_rsp)) {
1760 cifs_tcon_dbg(VFS, "Invalid cchunk response size\n");
1764 if (retbuf->TotalBytesWritten == 0) {
1765 cifs_dbg(FYI, "no bytes copied\n");
1770 * Check if server claimed to write more than we asked
1772 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1773 le32_to_cpu(pcchunk->Length)) {
1774 cifs_tcon_dbg(VFS, "Invalid copy chunk response\n");
1778 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1779 cifs_tcon_dbg(VFS, "Invalid num chunks written\n");
1785 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1786 src_off += bytes_written;
1787 dest_off += bytes_written;
1788 len -= bytes_written;
1789 total_bytes_written += bytes_written;
1791 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1792 le32_to_cpu(retbuf->ChunksWritten),
1793 le32_to_cpu(retbuf->ChunkBytesWritten),
1795 } else if (rc == -EINVAL) {
1796 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1799 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1800 le32_to_cpu(retbuf->ChunksWritten),
1801 le32_to_cpu(retbuf->ChunkBytesWritten),
1802 le32_to_cpu(retbuf->TotalBytesWritten));
1805 * Check if this is the first request using these sizes,
1806 * (ie check if copy succeed once with original sizes
1807 * and check if the server gave us different sizes after
1808 * we already updated max sizes on previous request).
1809 * if not then why is the server returning an error now
1811 if ((chunks_copied != 0) || chunk_sizes_updated)
1814 /* Check that server is not asking us to grow size */
1815 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1816 tcon->max_bytes_chunk)
1817 tcon->max_bytes_chunk =
1818 le32_to_cpu(retbuf->ChunkBytesWritten);
1820 goto cchunk_out; /* server gave us bogus size */
1822 /* No need to change MaxChunks since already set to 1 */
1823 chunk_sizes_updated = true;
1834 return total_bytes_written;
1838 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1839 struct cifs_fid *fid)
1841 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1845 smb2_read_data_offset(char *buf)
1847 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1849 return rsp->DataOffset;
1853 smb2_read_data_length(char *buf, bool in_remaining)
1855 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1858 return le32_to_cpu(rsp->DataRemaining);
1860 return le32_to_cpu(rsp->DataLength);
1865 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1866 struct cifs_io_parms *parms, unsigned int *bytes_read,
1867 char **buf, int *buf_type)
1869 parms->persistent_fid = pfid->persistent_fid;
1870 parms->volatile_fid = pfid->volatile_fid;
1871 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1875 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1876 struct cifs_io_parms *parms, unsigned int *written,
1877 struct kvec *iov, unsigned long nr_segs)
1880 parms->persistent_fid = pfid->persistent_fid;
1881 parms->volatile_fid = pfid->volatile_fid;
1882 return SMB2_write(xid, parms, written, iov, nr_segs);
1885 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1886 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1887 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1889 struct cifsInodeInfo *cifsi;
1892 cifsi = CIFS_I(inode);
1894 /* if file already sparse don't bother setting sparse again */
1895 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1896 return true; /* already sparse */
1898 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1899 return true; /* already not sparse */
1902 * Can't check for sparse support on share the usual way via the
1903 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1904 * since Samba server doesn't set the flag on the share, yet
1905 * supports the set sparse FSCTL and returns sparse correctly
1906 * in the file attributes. If we fail setting sparse though we
1907 * mark that server does not support sparse files for this share
1908 * to avoid repeatedly sending the unsupported fsctl to server
1909 * if the file is repeatedly extended.
1911 if (tcon->broken_sparse_sup)
1914 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1915 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1917 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
1919 tcon->broken_sparse_sup = true;
1920 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1925 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1927 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1933 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1934 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1936 __le64 eof = cpu_to_le64(size);
1937 struct inode *inode;
1940 * If extending file more than one page make sparse. Many Linux fs
1941 * make files sparse by default when extending via ftruncate
1943 inode = d_inode(cfile->dentry);
1945 if (!set_alloc && (size > inode->i_size + 8192)) {
1946 __u8 set_sparse = 1;
1948 /* whether set sparse succeeds or not, extend the file */
1949 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1952 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1953 cfile->fid.volatile_fid, cfile->pid, &eof);
1957 smb2_duplicate_extents(const unsigned int xid,
1958 struct cifsFileInfo *srcfile,
1959 struct cifsFileInfo *trgtfile, u64 src_off,
1960 u64 len, u64 dest_off)
1963 unsigned int ret_data_len;
1964 struct duplicate_extents_to_file dup_ext_buf;
1965 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1967 /* server fileays advertise duplicate extent support with this flag */
1968 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1969 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1972 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1973 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1974 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1975 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1976 dup_ext_buf.ByteCount = cpu_to_le64(len);
1977 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
1978 src_off, dest_off, len);
1980 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1982 goto duplicate_extents_out;
1984 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1985 trgtfile->fid.volatile_fid,
1986 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
1987 true /* is_fsctl */,
1988 (char *)&dup_ext_buf,
1989 sizeof(struct duplicate_extents_to_file),
1990 CIFSMaxBufSize, NULL,
1993 if (ret_data_len > 0)
1994 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
1996 duplicate_extents_out:
2001 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2002 struct cifsFileInfo *cfile)
2004 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
2005 cfile->fid.volatile_fid);
2009 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
2010 struct cifsFileInfo *cfile)
2012 struct fsctl_set_integrity_information_req integr_info;
2013 unsigned int ret_data_len;
2015 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
2016 integr_info.Flags = 0;
2017 integr_info.Reserved = 0;
2019 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2020 cfile->fid.volatile_fid,
2021 FSCTL_SET_INTEGRITY_INFORMATION,
2022 true /* is_fsctl */,
2023 (char *)&integr_info,
2024 sizeof(struct fsctl_set_integrity_information_req),
2025 CIFSMaxBufSize, NULL,
2030 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
2031 #define GMT_TOKEN_SIZE 50
2033 #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
2036 * Input buffer contains (empty) struct smb_snapshot array with size filled in
2037 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
2040 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
2041 struct cifsFileInfo *cfile, void __user *ioc_buf)
2043 char *retbuf = NULL;
2044 unsigned int ret_data_len = 0;
2046 u32 max_response_size;
2047 struct smb_snapshot_array snapshot_in;
2050 * On the first query to enumerate the list of snapshots available
2051 * for this volume the buffer begins with 0 (number of snapshots
2052 * which can be returned is zero since at that point we do not know
2053 * how big the buffer needs to be). On the second query,
2054 * it (ret_data_len) is set to number of snapshots so we can
2055 * know to set the maximum response size larger (see below).
2057 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
2061 * Note that for snapshot queries that servers like Azure expect that
2062 * the first query be minimal size (and just used to get the number/size
2063 * of previous versions) so response size must be specified as EXACTLY
2064 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2067 if (ret_data_len == 0)
2068 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
2070 max_response_size = CIFSMaxBufSize;
2072 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2073 cfile->fid.volatile_fid,
2074 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
2075 true /* is_fsctl */,
2076 NULL, 0 /* no input data */, max_response_size,
2079 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2084 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2086 if (copy_from_user(&snapshot_in, ioc_buf,
2087 sizeof(struct smb_snapshot_array))) {
2094 * Check for min size, ie not large enough to fit even one GMT
2095 * token (snapshot). On the first ioctl some users may pass in
2096 * smaller size (or zero) to simply get the size of the array
2097 * so the user space caller can allocate sufficient memory
2098 * and retry the ioctl again with larger array size sufficient
2099 * to hold all of the snapshot GMT tokens on the second try.
2101 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2102 ret_data_len = sizeof(struct smb_snapshot_array);
2105 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2106 * the snapshot array (of 50 byte GMT tokens) each
2107 * representing an available previous version of the data
2109 if (ret_data_len > (snapshot_in.snapshot_array_size +
2110 sizeof(struct smb_snapshot_array)))
2111 ret_data_len = snapshot_in.snapshot_array_size +
2112 sizeof(struct smb_snapshot_array);
2114 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2125 smb3_notify(const unsigned int xid, struct file *pfile,
2126 void __user *ioc_buf)
2128 struct smb3_notify notify;
2129 struct dentry *dentry = pfile->f_path.dentry;
2130 struct inode *inode = file_inode(pfile);
2131 struct cifs_sb_info *cifs_sb;
2132 struct cifs_open_parms oparms;
2133 struct cifs_fid fid;
2134 struct cifs_tcon *tcon;
2135 unsigned char *path = NULL;
2136 __le16 *utf16_path = NULL;
2137 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2140 path = build_path_from_dentry(dentry);
2144 cifs_sb = CIFS_SB(inode->i_sb);
2146 utf16_path = cifs_convert_path_to_utf16(path + 1, cifs_sb);
2147 if (utf16_path == NULL) {
2152 if (copy_from_user(¬ify, ioc_buf, sizeof(struct smb3_notify))) {
2157 tcon = cifs_sb_master_tcon(cifs_sb);
2159 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2160 oparms.disposition = FILE_OPEN;
2161 oparms.create_options = cifs_create_options(cifs_sb, 0);
2163 oparms.reconnect = false;
2165 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
2170 rc = SMB2_change_notify(xid, tcon, fid.persistent_fid, fid.volatile_fid,
2171 notify.watch_tree, notify.completion_filter);
2173 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2175 cifs_dbg(FYI, "change notify for path %s rc %d\n", path, rc);
2184 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2185 const char *path, struct cifs_sb_info *cifs_sb,
2186 struct cifs_fid *fid, __u16 search_flags,
2187 struct cifs_search_info *srch_inf)
2190 struct smb_rqst rqst[2];
2191 struct kvec rsp_iov[2];
2192 int resp_buftype[2];
2193 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2194 struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
2196 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2197 struct cifs_open_parms oparms;
2198 struct smb2_query_directory_rsp *qd_rsp = NULL;
2199 struct smb2_create_rsp *op_rsp = NULL;
2200 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
2202 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2206 if (smb3_encryption_required(tcon))
2207 flags |= CIFS_TRANSFORM_REQ;
2209 memset(rqst, 0, sizeof(rqst));
2210 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2211 memset(rsp_iov, 0, sizeof(rsp_iov));
2214 memset(&open_iov, 0, sizeof(open_iov));
2215 rqst[0].rq_iov = open_iov;
2216 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2219 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2220 oparms.disposition = FILE_OPEN;
2221 oparms.create_options = cifs_create_options(cifs_sb, 0);
2223 oparms.reconnect = false;
2225 rc = SMB2_open_init(tcon, server,
2226 &rqst[0], &oplock, &oparms, utf16_path);
2229 smb2_set_next_command(tcon, &rqst[0]);
2231 /* Query directory */
2232 srch_inf->entries_in_buffer = 0;
2233 srch_inf->index_of_last_entry = 2;
2235 memset(&qd_iov, 0, sizeof(qd_iov));
2236 rqst[1].rq_iov = qd_iov;
2237 rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
2239 rc = SMB2_query_directory_init(xid, tcon, server,
2241 COMPOUND_FID, COMPOUND_FID,
2242 0, srch_inf->info_level);
2246 smb2_set_related(&rqst[1]);
2248 rc = compound_send_recv(xid, tcon->ses, server,
2250 resp_buftype, rsp_iov);
2252 /* If the open failed there is nothing to do */
2253 op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
2254 if (op_rsp == NULL || op_rsp->sync_hdr.Status != STATUS_SUCCESS) {
2255 cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
2258 fid->persistent_fid = op_rsp->PersistentFileId;
2259 fid->volatile_fid = op_rsp->VolatileFileId;
2261 /* Anything else than ENODATA means a genuine error */
2262 if (rc && rc != -ENODATA) {
2263 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2264 cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
2265 trace_smb3_query_dir_err(xid, fid->persistent_fid,
2266 tcon->tid, tcon->ses->Suid, 0, 0, rc);
2270 atomic_inc(&tcon->num_remote_opens);
2272 qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
2273 if (qd_rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
2274 trace_smb3_query_dir_done(xid, fid->persistent_fid,
2275 tcon->tid, tcon->ses->Suid, 0, 0);
2276 srch_inf->endOfSearch = true;
2281 rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
2284 trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
2285 tcon->ses->Suid, 0, 0, rc);
2288 resp_buftype[1] = CIFS_NO_BUFFER;
2290 trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
2291 tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
2295 SMB2_open_free(&rqst[0]);
2296 SMB2_query_directory_free(&rqst[1]);
2297 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2298 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2303 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2304 struct cifs_fid *fid, __u16 search_flags,
2305 struct cifs_search_info *srch_inf)
2307 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2308 fid->volatile_fid, 0, srch_inf);
2312 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2313 struct cifs_fid *fid)
2315 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2319 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2320 * the number of credits and return true. Otherwise - return false.
2323 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
2325 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2327 if (shdr->Status != STATUS_PENDING)
2330 if (shdr->CreditRequest) {
2331 spin_lock(&server->req_lock);
2332 server->credits += le16_to_cpu(shdr->CreditRequest);
2333 spin_unlock(&server->req_lock);
2334 wake_up(&server->request_q);
2341 smb2_is_session_expired(char *buf)
2343 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2345 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2346 shdr->Status != STATUS_USER_SESSION_DELETED)
2349 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2350 le16_to_cpu(shdr->Command),
2351 le64_to_cpu(shdr->MessageId));
2352 cifs_dbg(FYI, "Session expired or deleted\n");
2358 smb2_is_status_io_timeout(char *buf)
2360 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2362 if (shdr->Status == STATUS_IO_TIMEOUT)
2369 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2370 struct cifsInodeInfo *cinode)
2372 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2373 return SMB2_lease_break(0, tcon, cinode->lease_key,
2374 smb2_get_lease_state(cinode));
2376 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2378 CIFS_CACHE_READ(cinode) ? 1 : 0);
2382 smb2_set_related(struct smb_rqst *rqst)
2384 struct smb2_sync_hdr *shdr;
2386 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2388 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2391 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2394 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2397 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2399 struct smb2_sync_hdr *shdr;
2400 struct cifs_ses *ses = tcon->ses;
2401 struct TCP_Server_Info *server = ses->server;
2402 unsigned long len = smb_rqst_len(server, rqst);
2405 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2407 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2411 /* SMB headers in a compound are 8 byte aligned. */
2413 /* No padding needed */
2417 num_padding = 8 - (len & 7);
2418 if (!smb3_encryption_required(tcon)) {
2420 * If we do not have encryption then we can just add an extra
2421 * iov for the padding.
2423 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2424 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2429 * We can not add a small padding iov for the encryption case
2430 * because the encryption framework can not handle the padding
2432 * We have to flatten this into a single buffer and add
2433 * the padding to it.
2435 for (i = 1; i < rqst->rq_nvec; i++) {
2436 memcpy(rqst->rq_iov[0].iov_base +
2437 rqst->rq_iov[0].iov_len,
2438 rqst->rq_iov[i].iov_base,
2439 rqst->rq_iov[i].iov_len);
2440 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2442 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2444 rqst->rq_iov[0].iov_len += num_padding;
2450 shdr->NextCommand = cpu_to_le32(len);
2454 * Passes the query info response back to the caller on success.
2455 * Caller need to free this with free_rsp_buf().
2458 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2459 __le16 *utf16_path, u32 desired_access,
2460 u32 class, u32 type, u32 output_len,
2461 struct kvec *rsp, int *buftype,
2462 struct cifs_sb_info *cifs_sb)
2464 struct cifs_ses *ses = tcon->ses;
2465 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2467 struct smb_rqst rqst[3];
2468 int resp_buftype[3];
2469 struct kvec rsp_iov[3];
2470 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2471 struct kvec qi_iov[1];
2472 struct kvec close_iov[1];
2473 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2474 struct cifs_open_parms oparms;
2475 struct cifs_fid fid;
2478 if (smb3_encryption_required(tcon))
2479 flags |= CIFS_TRANSFORM_REQ;
2481 memset(rqst, 0, sizeof(rqst));
2482 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2483 memset(rsp_iov, 0, sizeof(rsp_iov));
2485 memset(&open_iov, 0, sizeof(open_iov));
2486 rqst[0].rq_iov = open_iov;
2487 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2490 oparms.desired_access = desired_access;
2491 oparms.disposition = FILE_OPEN;
2492 oparms.create_options = cifs_create_options(cifs_sb, 0);
2494 oparms.reconnect = false;
2496 rc = SMB2_open_init(tcon, server,
2497 &rqst[0], &oplock, &oparms, utf16_path);
2500 smb2_set_next_command(tcon, &rqst[0]);
2502 memset(&qi_iov, 0, sizeof(qi_iov));
2503 rqst[1].rq_iov = qi_iov;
2504 rqst[1].rq_nvec = 1;
2506 rc = SMB2_query_info_init(tcon, server,
2507 &rqst[1], COMPOUND_FID, COMPOUND_FID,
2513 smb2_set_next_command(tcon, &rqst[1]);
2514 smb2_set_related(&rqst[1]);
2516 memset(&close_iov, 0, sizeof(close_iov));
2517 rqst[2].rq_iov = close_iov;
2518 rqst[2].rq_nvec = 1;
2520 rc = SMB2_close_init(tcon, server,
2521 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2524 smb2_set_related(&rqst[2]);
2526 rc = compound_send_recv(xid, ses, server,
2528 resp_buftype, rsp_iov);
2530 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2531 if (rc == -EREMCHG) {
2532 tcon->need_reconnect = true;
2533 pr_warn_once("server share %s deleted\n",
2539 *buftype = resp_buftype[1];
2542 SMB2_open_free(&rqst[0]);
2543 SMB2_query_info_free(&rqst[1]);
2544 SMB2_close_free(&rqst[2]);
2545 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2546 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2551 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2552 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2554 struct smb2_query_info_rsp *rsp;
2555 struct smb2_fs_full_size_info *info = NULL;
2556 __le16 utf16_path = 0; /* Null - open root of share */
2557 struct kvec rsp_iov = {NULL, 0};
2558 int buftype = CIFS_NO_BUFFER;
2562 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2563 FILE_READ_ATTRIBUTES,
2564 FS_FULL_SIZE_INFORMATION,
2565 SMB2_O_INFO_FILESYSTEM,
2566 sizeof(struct smb2_fs_full_size_info),
2567 &rsp_iov, &buftype, cifs_sb);
2571 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2572 buf->f_type = SMB2_MAGIC_NUMBER;
2573 info = (struct smb2_fs_full_size_info *)(
2574 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2575 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2576 le32_to_cpu(rsp->OutputBufferLength),
2578 sizeof(struct smb2_fs_full_size_info));
2580 smb2_copy_fs_info_to_kstatfs(info, buf);
2583 free_rsp_buf(buftype, rsp_iov.iov_base);
2588 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2589 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2592 __le16 srch_path = 0; /* Null - open root of share */
2593 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2594 struct cifs_open_parms oparms;
2595 struct cifs_fid fid;
2597 if (!tcon->posix_extensions)
2598 return smb2_queryfs(xid, tcon, cifs_sb, buf);
2601 oparms.desired_access = FILE_READ_ATTRIBUTES;
2602 oparms.disposition = FILE_OPEN;
2603 oparms.create_options = cifs_create_options(cifs_sb, 0);
2605 oparms.reconnect = false;
2607 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
2612 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2613 fid.volatile_fid, buf);
2614 buf->f_type = SMB2_MAGIC_NUMBER;
2615 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2620 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2622 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2623 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2627 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2628 __u64 length, __u32 type, int lock, int unlock, bool wait)
2630 if (unlock && !lock)
2631 type = SMB2_LOCKFLAG_UNLOCK;
2632 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2633 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2634 current->tgid, length, offset, type, wait);
2638 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2640 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2644 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2646 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2650 smb2_new_lease_key(struct cifs_fid *fid)
2652 generate_random_uuid(fid->lease_key);
2656 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2657 const char *search_name,
2658 struct dfs_info3_param **target_nodes,
2659 unsigned int *num_of_nodes,
2660 const struct nls_table *nls_codepage, int remap)
2663 __le16 *utf16_path = NULL;
2664 int utf16_path_len = 0;
2665 struct cifs_tcon *tcon;
2666 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2667 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2668 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2670 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
2673 * Try to use the IPC tcon, otherwise just use any
2675 tcon = ses->tcon_ipc;
2677 spin_lock(&cifs_tcp_ses_lock);
2678 tcon = list_first_entry_or_null(&ses->tcon_list,
2683 spin_unlock(&cifs_tcp_ses_lock);
2687 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2693 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2695 nls_codepage, remap);
2701 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2702 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2708 /* Highest DFS referral version understood */
2709 dfs_req->MaxReferralLevel = DFS_VERSION;
2711 /* Path to resolve in an UTF-16 null-terminated string */
2712 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2715 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2716 FSCTL_DFS_GET_REFERRALS,
2717 true /* is_fsctl */,
2718 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
2719 (char **)&dfs_rsp, &dfs_rsp_size);
2720 } while (rc == -EAGAIN);
2723 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
2724 cifs_tcon_dbg(VFS, "ioctl error in %s rc=%d\n", __func__, rc);
2728 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2729 num_of_nodes, target_nodes,
2730 nls_codepage, remap, search_name,
2731 true /* is_unicode */);
2733 cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
2738 if (tcon && !tcon->ipc) {
2739 /* ipc tcons are not refcounted */
2740 spin_lock(&cifs_tcp_ses_lock);
2742 spin_unlock(&cifs_tcp_ses_lock);
2751 parse_reparse_posix(struct reparse_posix_data *symlink_buf,
2752 u32 plen, char **target_path,
2753 struct cifs_sb_info *cifs_sb)
2757 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
2758 len = le16_to_cpu(symlink_buf->ReparseDataLength);
2760 if (le64_to_cpu(symlink_buf->InodeType) != NFS_SPECFILE_LNK) {
2761 cifs_dbg(VFS, "%lld not a supported symlink type\n",
2762 le64_to_cpu(symlink_buf->InodeType));
2766 *target_path = cifs_strndup_from_utf16(
2767 symlink_buf->PathBuffer,
2768 len, true, cifs_sb->local_nls);
2769 if (!(*target_path))
2772 convert_delimiter(*target_path, '/');
2773 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2779 parse_reparse_symlink(struct reparse_symlink_data_buffer *symlink_buf,
2780 u32 plen, char **target_path,
2781 struct cifs_sb_info *cifs_sb)
2783 unsigned int sub_len;
2784 unsigned int sub_offset;
2786 /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
2788 sub_offset = le16_to_cpu(symlink_buf->SubstituteNameOffset);
2789 sub_len = le16_to_cpu(symlink_buf->SubstituteNameLength);
2790 if (sub_offset + 20 > plen ||
2791 sub_offset + sub_len + 20 > plen) {
2792 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
2796 *target_path = cifs_strndup_from_utf16(
2797 symlink_buf->PathBuffer + sub_offset,
2798 sub_len, true, cifs_sb->local_nls);
2799 if (!(*target_path))
2802 convert_delimiter(*target_path, '/');
2803 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2809 parse_reparse_point(struct reparse_data_buffer *buf,
2810 u32 plen, char **target_path,
2811 struct cifs_sb_info *cifs_sb)
2813 if (plen < sizeof(struct reparse_data_buffer)) {
2814 cifs_dbg(VFS, "reparse buffer is too small. Must be at least 8 bytes but was %d\n",
2819 if (plen < le16_to_cpu(buf->ReparseDataLength) +
2820 sizeof(struct reparse_data_buffer)) {
2821 cifs_dbg(VFS, "srv returned invalid reparse buf length: %d\n",
2826 /* See MS-FSCC 2.1.2 */
2827 switch (le32_to_cpu(buf->ReparseTag)) {
2828 case IO_REPARSE_TAG_NFS:
2829 return parse_reparse_posix(
2830 (struct reparse_posix_data *)buf,
2831 plen, target_path, cifs_sb);
2832 case IO_REPARSE_TAG_SYMLINK:
2833 return parse_reparse_symlink(
2834 (struct reparse_symlink_data_buffer *)buf,
2835 plen, target_path, cifs_sb);
2837 cifs_dbg(VFS, "srv returned unknown symlink buffer tag:0x%08x\n",
2838 le32_to_cpu(buf->ReparseTag));
2843 #define SMB2_SYMLINK_STRUCT_SIZE \
2844 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2847 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
2848 struct cifs_sb_info *cifs_sb, const char *full_path,
2849 char **target_path, bool is_reparse_point)
2852 __le16 *utf16_path = NULL;
2853 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2854 struct cifs_open_parms oparms;
2855 struct cifs_fid fid;
2856 struct kvec err_iov = {NULL, 0};
2857 struct smb2_err_rsp *err_buf = NULL;
2858 struct smb2_symlink_err_rsp *symlink;
2859 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
2860 unsigned int sub_len;
2861 unsigned int sub_offset;
2862 unsigned int print_len;
2863 unsigned int print_offset;
2865 struct smb_rqst rqst[3];
2866 int resp_buftype[3];
2867 struct kvec rsp_iov[3];
2868 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2869 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2870 struct kvec close_iov[1];
2871 struct smb2_create_rsp *create_rsp;
2872 struct smb2_ioctl_rsp *ioctl_rsp;
2873 struct reparse_data_buffer *reparse_buf;
2874 int create_options = is_reparse_point ? OPEN_REPARSE_POINT : 0;
2877 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2879 *target_path = NULL;
2881 if (smb3_encryption_required(tcon))
2882 flags |= CIFS_TRANSFORM_REQ;
2884 memset(rqst, 0, sizeof(rqst));
2885 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2886 memset(rsp_iov, 0, sizeof(rsp_iov));
2888 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2893 memset(&open_iov, 0, sizeof(open_iov));
2894 rqst[0].rq_iov = open_iov;
2895 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2897 memset(&oparms, 0, sizeof(oparms));
2899 oparms.desired_access = FILE_READ_ATTRIBUTES;
2900 oparms.disposition = FILE_OPEN;
2901 oparms.create_options = cifs_create_options(cifs_sb, create_options);
2903 oparms.reconnect = false;
2905 rc = SMB2_open_init(tcon, server,
2906 &rqst[0], &oplock, &oparms, utf16_path);
2909 smb2_set_next_command(tcon, &rqst[0]);
2913 memset(&io_iov, 0, sizeof(io_iov));
2914 rqst[1].rq_iov = io_iov;
2915 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
2917 rc = SMB2_ioctl_init(tcon, server,
2918 &rqst[1], fid.persistent_fid,
2919 fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
2920 true /* is_fctl */, NULL, 0,
2922 MAX_SMB2_CREATE_RESPONSE_SIZE -
2923 MAX_SMB2_CLOSE_RESPONSE_SIZE);
2927 smb2_set_next_command(tcon, &rqst[1]);
2928 smb2_set_related(&rqst[1]);
2932 memset(&close_iov, 0, sizeof(close_iov));
2933 rqst[2].rq_iov = close_iov;
2934 rqst[2].rq_nvec = 1;
2936 rc = SMB2_close_init(tcon, server,
2937 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2941 smb2_set_related(&rqst[2]);
2943 rc = compound_send_recv(xid, tcon->ses, server,
2945 resp_buftype, rsp_iov);
2947 create_rsp = rsp_iov[0].iov_base;
2948 if (create_rsp && create_rsp->sync_hdr.Status)
2949 err_iov = rsp_iov[0];
2950 ioctl_rsp = rsp_iov[1].iov_base;
2953 * Open was successful and we got an ioctl response.
2955 if ((rc == 0) && (is_reparse_point)) {
2956 /* See MS-FSCC 2.3.23 */
2958 reparse_buf = (struct reparse_data_buffer *)
2959 ((char *)ioctl_rsp +
2960 le32_to_cpu(ioctl_rsp->OutputOffset));
2961 plen = le32_to_cpu(ioctl_rsp->OutputCount);
2963 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
2964 rsp_iov[1].iov_len) {
2965 cifs_tcon_dbg(VFS, "srv returned invalid ioctl len: %d\n",
2971 rc = parse_reparse_point(reparse_buf, plen, target_path,
2976 if (!rc || !err_iov.iov_base) {
2981 err_buf = err_iov.iov_base;
2982 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
2983 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
2988 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
2989 if (le32_to_cpu(symlink->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
2990 le32_to_cpu(symlink->ReparseTag) != IO_REPARSE_TAG_SYMLINK) {
2995 /* open must fail on symlink - reset rc */
2997 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
2998 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
2999 print_len = le16_to_cpu(symlink->PrintNameLength);
3000 print_offset = le16_to_cpu(symlink->PrintNameOffset);
3002 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
3007 if (err_iov.iov_len <
3008 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
3013 *target_path = cifs_strndup_from_utf16(
3014 (char *)symlink->PathBuffer + sub_offset,
3015 sub_len, true, cifs_sb->local_nls);
3016 if (!(*target_path)) {
3020 convert_delimiter(*target_path, '/');
3021 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
3024 cifs_dbg(FYI, "query symlink rc %d\n", rc);
3026 SMB2_open_free(&rqst[0]);
3027 SMB2_ioctl_free(&rqst[1]);
3028 SMB2_close_free(&rqst[2]);
3029 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
3030 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3031 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
3035 static struct cifs_ntsd *
3036 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
3037 const struct cifs_fid *cifsfid, u32 *pacllen)
3039 struct cifs_ntsd *pntsd = NULL;
3041 int rc = -EOPNOTSUPP;
3042 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3045 return ERR_CAST(tlink);
3048 cifs_dbg(FYI, "trying to get acl\n");
3050 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
3051 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
3054 cifs_put_tlink(tlink);
3056 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3063 static struct cifs_ntsd *
3064 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
3065 const char *path, u32 *pacllen)
3067 struct cifs_ntsd *pntsd = NULL;
3068 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3071 struct cifs_tcon *tcon;
3072 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3073 struct cifs_fid fid;
3074 struct cifs_open_parms oparms;
3077 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
3079 return ERR_CAST(tlink);
3081 tcon = tlink_tcon(tlink);
3084 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3092 oparms.desired_access = READ_CONTROL;
3093 oparms.disposition = FILE_OPEN;
3094 oparms.create_options = cifs_create_options(cifs_sb, 0);
3096 oparms.reconnect = false;
3098 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
3102 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3103 fid.volatile_fid, (void **)&pntsd, pacllen);
3104 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3107 cifs_put_tlink(tlink);
3110 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3117 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
3118 struct inode *inode, const char *path, int aclflag)
3120 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3122 int rc, access_flags = 0;
3123 struct cifs_tcon *tcon;
3124 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3125 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3126 struct cifs_fid fid;
3127 struct cifs_open_parms oparms;
3130 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
3132 return PTR_ERR(tlink);
3134 tcon = tlink_tcon(tlink);
3137 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
3138 access_flags = WRITE_OWNER;
3140 access_flags = WRITE_DAC;
3142 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3150 oparms.desired_access = access_flags;
3151 oparms.create_options = cifs_create_options(cifs_sb, 0);
3152 oparms.disposition = FILE_OPEN;
3155 oparms.reconnect = false;
3157 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
3161 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3162 fid.volatile_fid, pnntsd, acllen, aclflag);
3163 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3166 cifs_put_tlink(tlink);
3171 /* Retrieve an ACL from the server */
3172 static struct cifs_ntsd *
3173 get_smb2_acl(struct cifs_sb_info *cifs_sb,
3174 struct inode *inode, const char *path,
3177 struct cifs_ntsd *pntsd = NULL;
3178 struct cifsFileInfo *open_file = NULL;
3181 open_file = find_readable_file(CIFS_I(inode), true);
3183 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
3185 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
3186 cifsFileInfo_put(open_file);
3190 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3191 loff_t offset, loff_t len, bool keep_size)
3193 struct cifs_ses *ses = tcon->ses;
3194 struct inode *inode;
3195 struct cifsInodeInfo *cifsi;
3196 struct cifsFileInfo *cfile = file->private_data;
3197 struct file_zero_data_information fsctl_buf;
3204 inode = d_inode(cfile->dentry);
3205 cifsi = CIFS_I(inode);
3207 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3208 ses->Suid, offset, len);
3211 * We zero the range through ioctl, so we need remove the page caches
3212 * first, otherwise the data may be inconsistent with the server.
3214 truncate_pagecache_range(inode, offset, offset + len - 1);
3216 /* if file not oplocked can't be sure whether asking to extend size */
3217 if (!CIFS_CACHE_READ(cifsi))
3218 if (keep_size == false) {
3220 trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
3221 tcon->tid, ses->Suid, offset, len, rc);
3226 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3228 fsctl_buf.FileOffset = cpu_to_le64(offset);
3229 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3231 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3232 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
3234 sizeof(struct file_zero_data_information),
3237 goto zero_range_exit;
3240 * do we also need to change the size of the file?
3242 if (keep_size == false && i_size_read(inode) < offset + len) {
3243 eof = cpu_to_le64(offset + len);
3244 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3245 cfile->fid.volatile_fid, cfile->pid, &eof);
3251 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3252 ses->Suid, offset, len, rc);
3254 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3255 ses->Suid, offset, len);
3259 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3260 loff_t offset, loff_t len)
3262 struct inode *inode;
3263 struct cifsFileInfo *cfile = file->private_data;
3264 struct file_zero_data_information fsctl_buf;
3267 __u8 set_sparse = 1;
3271 inode = d_inode(cfile->dentry);
3273 /* Need to make file sparse, if not already, before freeing range. */
3274 /* Consider adding equivalent for compressed since it could also work */
3275 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3282 * We implement the punch hole through ioctl, so we need remove the page
3283 * caches first, otherwise the data may be inconsistent with the server.
3285 truncate_pagecache_range(inode, offset, offset + len - 1);
3287 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3289 fsctl_buf.FileOffset = cpu_to_le64(offset);
3290 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3292 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3293 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3294 true /* is_fctl */, (char *)&fsctl_buf,
3295 sizeof(struct file_zero_data_information),
3296 CIFSMaxBufSize, NULL, NULL);
3301 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3302 loff_t off, loff_t len, bool keep_size)
3304 struct inode *inode;
3305 struct cifsInodeInfo *cifsi;
3306 struct cifsFileInfo *cfile = file->private_data;
3307 long rc = -EOPNOTSUPP;
3313 inode = d_inode(cfile->dentry);
3314 cifsi = CIFS_I(inode);
3316 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3317 tcon->ses->Suid, off, len);
3318 /* if file not oplocked can't be sure whether asking to extend size */
3319 if (!CIFS_CACHE_READ(cifsi))
3320 if (keep_size == false) {
3321 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3322 tcon->tid, tcon->ses->Suid, off, len, rc);
3328 * Extending the file
3330 if ((keep_size == false) && i_size_read(inode) < off + len) {
3331 rc = inode_newsize_ok(inode, off + len);
3335 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0)
3336 smb2_set_sparse(xid, tcon, cfile, inode, false);
3338 eof = cpu_to_le64(off + len);
3339 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3340 cfile->fid.volatile_fid, cfile->pid, &eof);
3342 cifsi->server_eof = off + len;
3343 cifs_setsize(inode, off + len);
3344 cifs_truncate_page(inode->i_mapping, inode->i_size);
3345 truncate_setsize(inode, off + len);
3351 * Files are non-sparse by default so falloc may be a no-op
3352 * Must check if file sparse. If not sparse, and since we are not
3353 * extending then no need to do anything since file already allocated
3355 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
3360 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3362 * Check if falloc starts within first few pages of file
3363 * and ends within a few pages of the end of file to
3364 * ensure that most of file is being forced to be
3365 * fallocated now. If so then setting whole file sparse
3366 * ie potentially making a few extra pages at the beginning
3367 * or end of the file non-sparse via set_sparse is harmless.
3369 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3375 smb2_set_sparse(xid, tcon, cfile, inode, false);
3380 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3381 tcon->ses->Suid, off, len, rc);
3383 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3384 tcon->ses->Suid, off, len);
3390 static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3392 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3393 struct cifsInodeInfo *cifsi;
3394 struct inode *inode;
3396 struct file_allocated_range_buffer in_data, *out_data = NULL;
3400 if (whence != SEEK_HOLE && whence != SEEK_DATA)
3401 return generic_file_llseek(file, offset, whence);
3403 inode = d_inode(cfile->dentry);
3404 cifsi = CIFS_I(inode);
3406 if (offset < 0 || offset >= i_size_read(inode))
3411 * We need to be sure that all dirty pages are written as they
3412 * might fill holes on the server.
3413 * Note that we also MUST flush any written pages since at least
3414 * some servers (Windows2016) will not reflect recent writes in
3415 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3417 wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
3419 filemap_write_and_wait(inode->i_mapping);
3420 smb2_flush_file(xid, tcon, &wrcfile->fid);
3421 cifsFileInfo_put(wrcfile);
3424 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3425 if (whence == SEEK_HOLE)
3426 offset = i_size_read(inode);
3430 in_data.file_offset = cpu_to_le64(offset);
3431 in_data.length = cpu_to_le64(i_size_read(inode));
3433 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3434 cfile->fid.volatile_fid,
3435 FSCTL_QUERY_ALLOCATED_RANGES, true,
3436 (char *)&in_data, sizeof(in_data),
3437 sizeof(struct file_allocated_range_buffer),
3438 (char **)&out_data, &out_data_len);
3444 if (whence == SEEK_HOLE && out_data_len == 0)
3447 if (whence == SEEK_DATA && out_data_len == 0) {
3452 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3456 if (whence == SEEK_DATA) {
3457 offset = le64_to_cpu(out_data->file_offset);
3460 if (offset < le64_to_cpu(out_data->file_offset))
3463 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3469 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3474 static int smb3_fiemap(struct cifs_tcon *tcon,
3475 struct cifsFileInfo *cfile,
3476 struct fiemap_extent_info *fei, u64 start, u64 len)
3479 struct file_allocated_range_buffer in_data, *out_data;
3481 int i, num, rc, flags, last_blob;
3484 rc = fiemap_prep(d_inode(cfile->dentry), fei, start, &len, 0);
3490 in_data.file_offset = cpu_to_le64(start);
3491 in_data.length = cpu_to_le64(len);
3493 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3494 cfile->fid.volatile_fid,
3495 FSCTL_QUERY_ALLOCATED_RANGES, true,
3496 (char *)&in_data, sizeof(in_data),
3497 1024 * sizeof(struct file_allocated_range_buffer),
3498 (char **)&out_data, &out_data_len);
3507 if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) {
3511 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3516 num = out_data_len / sizeof(struct file_allocated_range_buffer);
3517 for (i = 0; i < num; i++) {
3519 if (i == num - 1 && last_blob)
3520 flags |= FIEMAP_EXTENT_LAST;
3522 rc = fiemap_fill_next_extent(fei,
3523 le64_to_cpu(out_data[i].file_offset),
3524 le64_to_cpu(out_data[i].file_offset),
3525 le64_to_cpu(out_data[i].length),
3536 next = le64_to_cpu(out_data[num - 1].file_offset) +
3537 le64_to_cpu(out_data[num - 1].length);
3538 len = len - (next - start);
3549 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3550 loff_t off, loff_t len)
3552 /* KEEP_SIZE already checked for by do_fallocate */
3553 if (mode & FALLOC_FL_PUNCH_HOLE)
3554 return smb3_punch_hole(file, tcon, off, len);
3555 else if (mode & FALLOC_FL_ZERO_RANGE) {
3556 if (mode & FALLOC_FL_KEEP_SIZE)
3557 return smb3_zero_range(file, tcon, off, len, true);
3558 return smb3_zero_range(file, tcon, off, len, false);
3559 } else if (mode == FALLOC_FL_KEEP_SIZE)
3560 return smb3_simple_falloc(file, tcon, off, len, true);
3562 return smb3_simple_falloc(file, tcon, off, len, false);
3568 smb2_downgrade_oplock(struct TCP_Server_Info *server,
3569 struct cifsInodeInfo *cinode, __u32 oplock,
3570 unsigned int epoch, bool *purge_cache)
3572 server->ops->set_oplock_level(cinode, oplock, 0, NULL);
3576 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3577 unsigned int epoch, bool *purge_cache);
3580 smb3_downgrade_oplock(struct TCP_Server_Info *server,
3581 struct cifsInodeInfo *cinode, __u32 oplock,
3582 unsigned int epoch, bool *purge_cache)
3584 unsigned int old_state = cinode->oplock;
3585 unsigned int old_epoch = cinode->epoch;
3586 unsigned int new_state;
3588 if (epoch > old_epoch) {
3589 smb21_set_oplock_level(cinode, oplock, 0, NULL);
3590 cinode->epoch = epoch;
3593 new_state = cinode->oplock;
3594 *purge_cache = false;
3596 if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
3597 (new_state & CIFS_CACHE_READ_FLG) == 0)
3598 *purge_cache = true;
3599 else if (old_state == new_state && (epoch - old_epoch > 1))
3600 *purge_cache = true;
3604 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3605 unsigned int epoch, bool *purge_cache)
3608 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3610 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3611 cinode->oplock = CIFS_CACHE_RHW_FLG;
3612 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3613 &cinode->vfs_inode);
3614 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
3615 cinode->oplock = CIFS_CACHE_RW_FLG;
3616 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3617 &cinode->vfs_inode);
3618 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3619 cinode->oplock = CIFS_CACHE_READ_FLG;
3620 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3621 &cinode->vfs_inode);
3627 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3628 unsigned int epoch, bool *purge_cache)
3630 char message[5] = {0};
3631 unsigned int new_oplock = 0;
3634 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3637 /* Check if the server granted an oplock rather than a lease */
3638 if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3639 return smb2_set_oplock_level(cinode, oplock, epoch,
3642 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
3643 new_oplock |= CIFS_CACHE_READ_FLG;
3644 strcat(message, "R");
3646 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
3647 new_oplock |= CIFS_CACHE_HANDLE_FLG;
3648 strcat(message, "H");
3650 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
3651 new_oplock |= CIFS_CACHE_WRITE_FLG;
3652 strcat(message, "W");
3655 strncpy(message, "None", sizeof(message));
3657 cinode->oplock = new_oplock;
3658 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3659 &cinode->vfs_inode);
3663 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3664 unsigned int epoch, bool *purge_cache)
3666 unsigned int old_oplock = cinode->oplock;
3668 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3671 *purge_cache = false;
3672 if (old_oplock == CIFS_CACHE_READ_FLG) {
3673 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3674 (epoch - cinode->epoch > 0))
3675 *purge_cache = true;
3676 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3677 (epoch - cinode->epoch > 1))
3678 *purge_cache = true;
3679 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3680 (epoch - cinode->epoch > 1))
3681 *purge_cache = true;
3682 else if (cinode->oplock == 0 &&
3683 (epoch - cinode->epoch > 0))
3684 *purge_cache = true;
3685 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
3686 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3687 (epoch - cinode->epoch > 0))
3688 *purge_cache = true;
3689 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3690 (epoch - cinode->epoch > 1))
3691 *purge_cache = true;
3693 cinode->epoch = epoch;
3698 smb2_is_read_op(__u32 oplock)
3700 return oplock == SMB2_OPLOCK_LEVEL_II;
3704 smb21_is_read_op(__u32 oplock)
3706 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
3707 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
3711 map_oplock_to_lease(u8 oplock)
3713 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3714 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
3715 else if (oplock == SMB2_OPLOCK_LEVEL_II)
3716 return SMB2_LEASE_READ_CACHING;
3717 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3718 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
3719 SMB2_LEASE_WRITE_CACHING;
3724 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3726 struct create_lease *buf;
3728 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
3732 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3733 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3735 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3736 (struct create_lease, lcontext));
3737 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
3738 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3739 (struct create_lease, Name));
3740 buf->ccontext.NameLength = cpu_to_le16(4);
3741 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3750 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
3752 struct create_lease_v2 *buf;
3754 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
3758 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3759 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3761 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3762 (struct create_lease_v2, lcontext));
3763 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
3764 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3765 (struct create_lease_v2, Name));
3766 buf->ccontext.NameLength = cpu_to_le16(4);
3767 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3776 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3778 struct create_lease *lc = (struct create_lease *)buf;
3780 *epoch = 0; /* not used */
3781 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3782 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3783 return le32_to_cpu(lc->lcontext.LeaseState);
3787 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3789 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
3791 *epoch = le16_to_cpu(lc->lcontext.Epoch);
3792 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3793 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3795 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
3796 return le32_to_cpu(lc->lcontext.LeaseState);
3800 smb2_wp_retry_size(struct inode *inode)
3802 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
3803 SMB2_MAX_BUFFER_SIZE);
3807 smb2_dir_needs_close(struct cifsFileInfo *cfile)
3809 return !cfile->invalidHandle;
3813 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
3814 struct smb_rqst *old_rq, __le16 cipher_type)
3816 struct smb2_sync_hdr *shdr =
3817 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
3819 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
3820 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
3821 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
3822 tr_hdr->Flags = cpu_to_le16(0x01);
3823 if (cipher_type == SMB2_ENCRYPTION_AES128_GCM)
3824 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
3826 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
3827 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
3830 /* We can not use the normal sg_set_buf() as we will sometimes pass a
3831 * stack object as buf.
3833 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
3834 unsigned int buflen)
3838 * VMAP_STACK (at least) puts stack into the vmalloc address space
3840 if (is_vmalloc_addr(buf))
3841 addr = vmalloc_to_page(buf);
3843 addr = virt_to_page(buf);
3844 sg_set_page(sg, addr, buflen, offset_in_page(buf));
3847 /* Assumes the first rqst has a transform header as the first iov.
3849 * rqst[0].rq_iov[0] is transform header
3850 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
3851 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
3853 static struct scatterlist *
3854 init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
3856 unsigned int sg_len;
3857 struct scatterlist *sg;
3860 unsigned int idx = 0;
3864 for (i = 0; i < num_rqst; i++)
3865 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
3867 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
3871 sg_init_table(sg, sg_len);
3872 for (i = 0; i < num_rqst; i++) {
3873 for (j = 0; j < rqst[i].rq_nvec; j++) {
3875 * The first rqst has a transform header where the
3876 * first 20 bytes are not part of the encrypted blob
3878 skip = (i == 0) && (j == 0) ? 20 : 0;
3879 smb2_sg_set_buf(&sg[idx++],
3880 rqst[i].rq_iov[j].iov_base + skip,
3881 rqst[i].rq_iov[j].iov_len - skip);
3884 for (j = 0; j < rqst[i].rq_npages; j++) {
3885 unsigned int len, offset;
3887 rqst_page_get_length(&rqst[i], j, &len, &offset);
3888 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
3891 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
3896 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
3898 struct cifs_ses *ses;
3901 spin_lock(&cifs_tcp_ses_lock);
3902 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
3903 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3904 if (ses->Suid == ses_id) {
3905 ses_enc_key = enc ? ses->smb3encryptionkey :
3906 ses->smb3decryptionkey;
3907 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
3908 spin_unlock(&cifs_tcp_ses_lock);
3913 spin_unlock(&cifs_tcp_ses_lock);
3918 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
3919 * iov[0] - transform header (associate data),
3920 * iov[1-N] - SMB2 header and pages - data to encrypt.
3921 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
3925 crypt_message(struct TCP_Server_Info *server, int num_rqst,
3926 struct smb_rqst *rqst, int enc)
3928 struct smb2_transform_hdr *tr_hdr =
3929 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
3930 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
3932 struct scatterlist *sg;
3933 u8 sign[SMB2_SIGNATURE_SIZE] = {};
3934 u8 key[SMB3_SIGN_KEY_SIZE];
3935 struct aead_request *req;
3937 unsigned int iv_len;
3938 DECLARE_CRYPTO_WAIT(wait);
3939 struct crypto_aead *tfm;
3940 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3942 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
3944 cifs_server_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
3949 rc = smb3_crypto_aead_allocate(server);
3951 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
3955 tfm = enc ? server->secmech.ccmaesencrypt :
3956 server->secmech.ccmaesdecrypt;
3957 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
3959 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
3963 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
3965 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
3969 req = aead_request_alloc(tfm, GFP_KERNEL);
3971 cifs_server_dbg(VFS, "%s: Failed to alloc aead request\n", __func__);
3976 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
3977 crypt_len += SMB2_SIGNATURE_SIZE;
3980 sg = init_sg(num_rqst, rqst, sign);
3982 cifs_server_dbg(VFS, "%s: Failed to init sg\n", __func__);
3987 iv_len = crypto_aead_ivsize(tfm);
3988 iv = kzalloc(iv_len, GFP_KERNEL);
3990 cifs_server_dbg(VFS, "%s: Failed to alloc iv\n", __func__);
3995 if (server->cipher_type == SMB2_ENCRYPTION_AES128_GCM)
3996 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
3999 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
4002 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
4003 aead_request_set_ad(req, assoc_data_len);
4005 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
4006 crypto_req_done, &wait);
4008 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
4009 : crypto_aead_decrypt(req), &wait);
4012 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
4023 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
4027 for (i = 0; i < num_rqst; i++) {
4028 if (rqst[i].rq_pages) {
4029 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
4030 put_page(rqst[i].rq_pages[j]);
4031 kfree(rqst[i].rq_pages);
4037 * This function will initialize new_rq and encrypt the content.
4038 * The first entry, new_rq[0], only contains a single iov which contains
4039 * a smb2_transform_hdr and is pre-allocated by the caller.
4040 * This function then populates new_rq[1+] with the content from olq_rq[0+].
4042 * The end result is an array of smb_rqst structures where the first structure
4043 * only contains a single iov for the transform header which we then can pass
4044 * to crypt_message().
4046 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
4047 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
4050 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
4051 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
4053 struct page **pages;
4054 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
4055 unsigned int npages;
4056 unsigned int orig_len = 0;
4060 for (i = 1; i < num_rqst; i++) {
4061 npages = old_rq[i - 1].rq_npages;
4062 pages = kmalloc_array(npages, sizeof(struct page *),
4067 new_rq[i].rq_pages = pages;
4068 new_rq[i].rq_npages = npages;
4069 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
4070 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
4071 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
4072 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
4073 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
4075 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
4077 for (j = 0; j < npages; j++) {
4078 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4083 /* copy pages form the old */
4084 for (j = 0; j < npages; j++) {
4086 unsigned int offset, len;
4088 rqst_page_get_length(&new_rq[i], j, &len, &offset);
4090 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
4091 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
4093 memcpy(dst, src, len);
4094 kunmap(new_rq[i].rq_pages[j]);
4095 kunmap(old_rq[i - 1].rq_pages[j]);
4099 /* fill the 1st iov with a transform header */
4100 fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
4102 rc = crypt_message(server, num_rqst, new_rq, 1);
4103 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
4110 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
4115 smb3_is_transform_hdr(void *buf)
4117 struct smb2_transform_hdr *trhdr = buf;
4119 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
4123 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
4124 unsigned int buf_data_size, struct page **pages,
4125 unsigned int npages, unsigned int page_data_size)
4128 struct smb_rqst rqst = {NULL};
4131 iov[0].iov_base = buf;
4132 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
4133 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
4134 iov[1].iov_len = buf_data_size;
4138 rqst.rq_pages = pages;
4139 rqst.rq_npages = npages;
4140 rqst.rq_pagesz = PAGE_SIZE;
4141 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
4143 rc = crypt_message(server, 1, &rqst, 0);
4144 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
4149 memmove(buf, iov[1].iov_base, buf_data_size);
4151 server->total_read = buf_data_size + page_data_size;
4157 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
4158 unsigned int npages, unsigned int len)
4163 for (i = 0; i < npages; i++) {
4164 struct page *page = pages[i];
4168 if (len >= PAGE_SIZE) {
4169 /* enough data to fill the page */
4173 zero_user(page, len, PAGE_SIZE - len);
4176 length = cifs_read_page_from_socket(server, page, 0, n);
4179 server->total_read += length;
4186 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
4187 unsigned int cur_off, struct bio_vec **page_vec)
4189 struct bio_vec *bvec;
4192 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
4196 for (i = 0; i < npages; i++) {
4197 bvec[i].bv_page = pages[i];
4198 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
4199 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
4200 data_size -= bvec[i].bv_len;
4203 if (data_size != 0) {
4204 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4214 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4215 char *buf, unsigned int buf_len, struct page **pages,
4216 unsigned int npages, unsigned int page_data_size)
4218 unsigned int data_offset;
4219 unsigned int data_len;
4220 unsigned int cur_off;
4221 unsigned int cur_page_idx;
4222 unsigned int pad_len;
4223 struct cifs_readdata *rdata = mid->callback_data;
4224 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
4225 struct bio_vec *bvec = NULL;
4226 struct iov_iter iter;
4229 bool use_rdma_mr = false;
4231 if (shdr->Command != SMB2_READ) {
4232 cifs_server_dbg(VFS, "only big read responses are supported\n");
4236 if (server->ops->is_session_expired &&
4237 server->ops->is_session_expired(buf)) {
4238 cifs_reconnect(server);
4242 if (server->ops->is_status_pending &&
4243 server->ops->is_status_pending(buf, server))
4246 /* set up first two iov to get credits */
4247 rdata->iov[0].iov_base = buf;
4248 rdata->iov[0].iov_len = 0;
4249 rdata->iov[1].iov_base = buf;
4250 rdata->iov[1].iov_len =
4251 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
4252 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4253 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4254 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4255 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4257 rdata->result = server->ops->map_error(buf, true);
4258 if (rdata->result != 0) {
4259 cifs_dbg(FYI, "%s: server returned error %d\n",
4260 __func__, rdata->result);
4261 /* normal error on read response */
4262 dequeue_mid(mid, false);
4266 data_offset = server->ops->read_data_offset(buf);
4267 #ifdef CONFIG_CIFS_SMB_DIRECT
4268 use_rdma_mr = rdata->mr;
4270 data_len = server->ops->read_data_length(buf, use_rdma_mr);
4272 if (data_offset < server->vals->read_rsp_size) {
4274 * win2k8 sometimes sends an offset of 0 when the read
4275 * is beyond the EOF. Treat it as if the data starts just after
4278 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4279 __func__, data_offset);
4280 data_offset = server->vals->read_rsp_size;
4281 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4282 /* data_offset is beyond the end of smallbuf */
4283 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4284 __func__, data_offset);
4285 rdata->result = -EIO;
4286 dequeue_mid(mid, rdata->result);
4290 pad_len = data_offset - server->vals->read_rsp_size;
4292 if (buf_len <= data_offset) {
4293 /* read response payload is in pages */
4294 cur_page_idx = pad_len / PAGE_SIZE;
4295 cur_off = pad_len % PAGE_SIZE;
4297 if (cur_page_idx != 0) {
4298 /* data offset is beyond the 1st page of response */
4299 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4300 __func__, data_offset);
4301 rdata->result = -EIO;
4302 dequeue_mid(mid, rdata->result);
4306 if (data_len > page_data_size - pad_len) {
4307 /* data_len is corrupt -- discard frame */
4308 rdata->result = -EIO;
4309 dequeue_mid(mid, rdata->result);
4313 rdata->result = init_read_bvec(pages, npages, page_data_size,
4315 if (rdata->result != 0) {
4316 dequeue_mid(mid, rdata->result);
4320 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
4321 } else if (buf_len >= data_offset + data_len) {
4322 /* read response payload is in buf */
4323 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
4324 iov.iov_base = buf + data_offset;
4325 iov.iov_len = data_len;
4326 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
4328 /* read response payload cannot be in both buf and pages */
4329 WARN_ONCE(1, "buf can not contain only a part of read data");
4330 rdata->result = -EIO;
4331 dequeue_mid(mid, rdata->result);
4335 length = rdata->copy_into_pages(server, rdata, &iter);
4342 dequeue_mid(mid, false);
4346 struct smb2_decrypt_work {
4347 struct work_struct decrypt;
4348 struct TCP_Server_Info *server;
4349 struct page **ppages;
4351 unsigned int npages;
4356 static void smb2_decrypt_offload(struct work_struct *work)
4358 struct smb2_decrypt_work *dw = container_of(work,
4359 struct smb2_decrypt_work, decrypt);
4361 struct mid_q_entry *mid;
4363 rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
4364 dw->ppages, dw->npages, dw->len);
4366 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4370 dw->server->lstrp = jiffies;
4371 mid = smb2_find_mid(dw->server, dw->buf);
4373 cifs_dbg(FYI, "mid not found\n");
4375 mid->decrypted = true;
4376 rc = handle_read_data(dw->server, mid, dw->buf,
4377 dw->server->vals->read_rsp_size,
4378 dw->ppages, dw->npages, dw->len);
4380 cifs_mid_q_entry_release(mid);
4384 for (i = dw->npages-1; i >= 0; i--)
4385 put_page(dw->ppages[i]);
4388 cifs_small_buf_release(dw->buf);
4394 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4397 char *buf = server->smallbuf;
4398 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4399 unsigned int npages;
4400 struct page **pages;
4402 unsigned int buflen = server->pdu_size;
4405 struct smb2_decrypt_work *dw;
4408 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
4409 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
4411 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
4414 server->total_read += rc;
4416 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
4417 server->vals->read_rsp_size;
4418 npages = DIV_ROUND_UP(len, PAGE_SIZE);
4420 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
4426 for (; i < npages; i++) {
4427 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4434 /* read read data into pages */
4435 rc = read_data_into_pages(server, pages, npages, len);
4439 rc = cifs_discard_remaining_data(server);
4444 * For large reads, offload to different thread for better performance,
4445 * use more cores decrypting which can be expensive
4448 if ((server->min_offload) && (server->in_flight > 1) &&
4449 (server->pdu_size >= server->min_offload)) {
4450 dw = kmalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
4452 goto non_offloaded_decrypt;
4454 dw->buf = server->smallbuf;
4455 server->smallbuf = (char *)cifs_small_buf_get();
4457 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
4459 dw->npages = npages;
4460 dw->server = server;
4463 queue_work(decrypt_wq, &dw->decrypt);
4464 *num_mids = 0; /* worker thread takes care of finding mid */
4468 non_offloaded_decrypt:
4469 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
4470 pages, npages, len);
4474 *mid = smb2_find_mid(server, buf);
4476 cifs_dbg(FYI, "mid not found\n");
4478 cifs_dbg(FYI, "mid found\n");
4479 (*mid)->decrypted = true;
4480 rc = handle_read_data(server, *mid, buf,
4481 server->vals->read_rsp_size,
4482 pages, npages, len);
4486 for (i = i - 1; i >= 0; i--)
4491 cifs_discard_remaining_data(server);
4496 receive_encrypted_standard(struct TCP_Server_Info *server,
4497 struct mid_q_entry **mids, char **bufs,
4501 char *buf = server->smallbuf;
4502 struct smb2_sync_hdr *shdr;
4503 unsigned int pdu_length = server->pdu_size;
4504 unsigned int buf_size;
4505 struct mid_q_entry *mid_entry;
4507 char *next_buffer = NULL;
4511 /* switch to large buffer if too big for a small one */
4512 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
4513 server->large_buf = true;
4514 memcpy(server->bigbuf, buf, server->total_read);
4515 buf = server->bigbuf;
4518 /* now read the rest */
4519 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
4520 pdu_length - HEADER_SIZE(server) + 1);
4523 server->total_read += length;
4525 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
4526 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
4530 next_is_large = server->large_buf;
4532 shdr = (struct smb2_sync_hdr *)buf;
4533 if (shdr->NextCommand) {
4535 next_buffer = (char *)cifs_buf_get();
4537 next_buffer = (char *)cifs_small_buf_get();
4539 buf + le32_to_cpu(shdr->NextCommand),
4540 pdu_length - le32_to_cpu(shdr->NextCommand));
4543 mid_entry = smb2_find_mid(server, buf);
4544 if (mid_entry == NULL)
4545 cifs_dbg(FYI, "mid not found\n");
4547 cifs_dbg(FYI, "mid found\n");
4548 mid_entry->decrypted = true;
4549 mid_entry->resp_buf_size = server->pdu_size;
4552 if (*num_mids >= MAX_COMPOUND) {
4553 cifs_server_dbg(VFS, "too many PDUs in compound\n");
4556 bufs[*num_mids] = buf;
4557 mids[(*num_mids)++] = mid_entry;
4559 if (mid_entry && mid_entry->handle)
4560 ret = mid_entry->handle(server, mid_entry);
4562 ret = cifs_handle_standard(server, mid_entry);
4564 if (ret == 0 && shdr->NextCommand) {
4565 pdu_length -= le32_to_cpu(shdr->NextCommand);
4566 server->large_buf = next_is_large;
4568 server->bigbuf = buf = next_buffer;
4570 server->smallbuf = buf = next_buffer;
4572 } else if (ret != 0) {
4574 * ret != 0 here means that we didn't get to handle_mid() thus
4575 * server->smallbuf and server->bigbuf are still valid. We need
4576 * to free next_buffer because it is not going to be used
4580 free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
4582 free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
4589 smb3_receive_transform(struct TCP_Server_Info *server,
4590 struct mid_q_entry **mids, char **bufs, int *num_mids)
4592 char *buf = server->smallbuf;
4593 unsigned int pdu_length = server->pdu_size;
4594 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4595 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4597 if (pdu_length < sizeof(struct smb2_transform_hdr) +
4598 sizeof(struct smb2_sync_hdr)) {
4599 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
4601 cifs_reconnect(server);
4602 return -ECONNABORTED;
4605 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
4606 cifs_server_dbg(VFS, "Transform message is broken\n");
4607 cifs_reconnect(server);
4608 return -ECONNABORTED;
4611 /* TODO: add support for compounds containing READ. */
4612 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
4613 return receive_encrypted_read(server, &mids[0], num_mids);
4616 return receive_encrypted_standard(server, mids, bufs, num_mids);
4620 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
4622 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
4624 return handle_read_data(server, mid, buf, server->pdu_size,
4629 smb2_next_header(char *buf)
4631 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
4632 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
4634 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
4635 return sizeof(struct smb2_transform_hdr) +
4636 le32_to_cpu(t_hdr->OriginalMessageSize);
4638 return le32_to_cpu(hdr->NextCommand);
4642 smb2_make_node(unsigned int xid, struct inode *inode,
4643 struct dentry *dentry, struct cifs_tcon *tcon,
4644 char *full_path, umode_t mode, dev_t dev)
4646 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4648 FILE_ALL_INFO *buf = NULL;
4649 struct cifs_io_parms io_parms = {0};
4651 struct cifs_fid fid;
4652 struct cifs_open_parms oparms;
4653 unsigned int bytes_written;
4654 struct win_dev *pdev;
4658 * Check if mounted with mount parm 'sfu' mount parm.
4659 * SFU emulation should work with all servers, but only
4660 * supports block and char device (no socket & fifo),
4661 * and was used by default in earlier versions of Windows
4663 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
4667 * TODO: Add ability to create instead via reparse point. Windows (e.g.
4668 * their current NFS server) uses this approach to expose special files
4669 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
4672 if (!S_ISCHR(mode) && !S_ISBLK(mode))
4675 cifs_dbg(FYI, "sfu compat create special file\n");
4677 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
4684 oparms.cifs_sb = cifs_sb;
4685 oparms.desired_access = GENERIC_WRITE;
4686 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
4687 CREATE_OPTION_SPECIAL);
4688 oparms.disposition = FILE_CREATE;
4689 oparms.path = full_path;
4691 oparms.reconnect = false;
4693 if (tcon->ses->server->oplocks)
4694 oplock = REQ_OPLOCK;
4697 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
4702 * BB Do not bother to decode buf since no local inode yet to put
4703 * timestamps in, but we can reuse it safely.
4706 pdev = (struct win_dev *)buf;
4707 io_parms.pid = current->tgid;
4708 io_parms.tcon = tcon;
4709 io_parms.offset = 0;
4710 io_parms.length = sizeof(struct win_dev);
4711 iov[1].iov_base = buf;
4712 iov[1].iov_len = sizeof(struct win_dev);
4713 if (S_ISCHR(mode)) {
4714 memcpy(pdev->type, "IntxCHR", 8);
4715 pdev->major = cpu_to_le64(MAJOR(dev));
4716 pdev->minor = cpu_to_le64(MINOR(dev));
4717 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4718 &bytes_written, iov, 1);
4719 } else if (S_ISBLK(mode)) {
4720 memcpy(pdev->type, "IntxBLK", 8);
4721 pdev->major = cpu_to_le64(MAJOR(dev));
4722 pdev->minor = cpu_to_le64(MINOR(dev));
4723 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4724 &bytes_written, iov, 1);
4726 tcon->ses->server->ops->close(xid, tcon, &fid);
4729 /* FIXME: add code here to set EAs */
4736 struct smb_version_operations smb20_operations = {
4737 .compare_fids = smb2_compare_fids,
4738 .setup_request = smb2_setup_request,
4739 .setup_async_request = smb2_setup_async_request,
4740 .check_receive = smb2_check_receive,
4741 .add_credits = smb2_add_credits,
4742 .set_credits = smb2_set_credits,
4743 .get_credits_field = smb2_get_credits_field,
4744 .get_credits = smb2_get_credits,
4745 .wait_mtu_credits = cifs_wait_mtu_credits,
4746 .get_next_mid = smb2_get_next_mid,
4747 .revert_current_mid = smb2_revert_current_mid,
4748 .read_data_offset = smb2_read_data_offset,
4749 .read_data_length = smb2_read_data_length,
4750 .map_error = map_smb2_to_linux_error,
4751 .find_mid = smb2_find_mid,
4752 .check_message = smb2_check_message,
4753 .dump_detail = smb2_dump_detail,
4754 .clear_stats = smb2_clear_stats,
4755 .print_stats = smb2_print_stats,
4756 .is_oplock_break = smb2_is_valid_oplock_break,
4757 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4758 .downgrade_oplock = smb2_downgrade_oplock,
4759 .need_neg = smb2_need_neg,
4760 .negotiate = smb2_negotiate,
4761 .negotiate_wsize = smb2_negotiate_wsize,
4762 .negotiate_rsize = smb2_negotiate_rsize,
4763 .sess_setup = SMB2_sess_setup,
4764 .logoff = SMB2_logoff,
4765 .tree_connect = SMB2_tcon,
4766 .tree_disconnect = SMB2_tdis,
4767 .qfs_tcon = smb2_qfs_tcon,
4768 .is_path_accessible = smb2_is_path_accessible,
4769 .can_echo = smb2_can_echo,
4771 .query_path_info = smb2_query_path_info,
4772 .get_srv_inum = smb2_get_srv_inum,
4773 .query_file_info = smb2_query_file_info,
4774 .set_path_size = smb2_set_path_size,
4775 .set_file_size = smb2_set_file_size,
4776 .set_file_info = smb2_set_file_info,
4777 .set_compression = smb2_set_compression,
4778 .mkdir = smb2_mkdir,
4779 .mkdir_setinfo = smb2_mkdir_setinfo,
4780 .rmdir = smb2_rmdir,
4781 .unlink = smb2_unlink,
4782 .rename = smb2_rename_path,
4783 .create_hardlink = smb2_create_hardlink,
4784 .query_symlink = smb2_query_symlink,
4785 .query_mf_symlink = smb3_query_mf_symlink,
4786 .create_mf_symlink = smb3_create_mf_symlink,
4787 .open = smb2_open_file,
4788 .set_fid = smb2_set_fid,
4789 .close = smb2_close_file,
4790 .flush = smb2_flush_file,
4791 .async_readv = smb2_async_readv,
4792 .async_writev = smb2_async_writev,
4793 .sync_read = smb2_sync_read,
4794 .sync_write = smb2_sync_write,
4795 .query_dir_first = smb2_query_dir_first,
4796 .query_dir_next = smb2_query_dir_next,
4797 .close_dir = smb2_close_dir,
4798 .calc_smb_size = smb2_calc_size,
4799 .is_status_pending = smb2_is_status_pending,
4800 .is_session_expired = smb2_is_session_expired,
4801 .oplock_response = smb2_oplock_response,
4802 .queryfs = smb2_queryfs,
4803 .mand_lock = smb2_mand_lock,
4804 .mand_unlock_range = smb2_unlock_range,
4805 .push_mand_locks = smb2_push_mandatory_locks,
4806 .get_lease_key = smb2_get_lease_key,
4807 .set_lease_key = smb2_set_lease_key,
4808 .new_lease_key = smb2_new_lease_key,
4809 .calc_signature = smb2_calc_signature,
4810 .is_read_op = smb2_is_read_op,
4811 .set_oplock_level = smb2_set_oplock_level,
4812 .create_lease_buf = smb2_create_lease_buf,
4813 .parse_lease_buf = smb2_parse_lease_buf,
4814 .copychunk_range = smb2_copychunk_range,
4815 .wp_retry_size = smb2_wp_retry_size,
4816 .dir_needs_close = smb2_dir_needs_close,
4817 .get_dfs_refer = smb2_get_dfs_refer,
4818 .select_sectype = smb2_select_sectype,
4819 #ifdef CONFIG_CIFS_XATTR
4820 .query_all_EAs = smb2_query_eas,
4821 .set_EA = smb2_set_ea,
4822 #endif /* CIFS_XATTR */
4823 .get_acl = get_smb2_acl,
4824 .get_acl_by_fid = get_smb2_acl_by_fid,
4825 .set_acl = set_smb2_acl,
4826 .next_header = smb2_next_header,
4827 .ioctl_query_info = smb2_ioctl_query_info,
4828 .make_node = smb2_make_node,
4829 .fiemap = smb3_fiemap,
4830 .llseek = smb3_llseek,
4831 .is_status_io_timeout = smb2_is_status_io_timeout,
4834 struct smb_version_operations smb21_operations = {
4835 .compare_fids = smb2_compare_fids,
4836 .setup_request = smb2_setup_request,
4837 .setup_async_request = smb2_setup_async_request,
4838 .check_receive = smb2_check_receive,
4839 .add_credits = smb2_add_credits,
4840 .set_credits = smb2_set_credits,
4841 .get_credits_field = smb2_get_credits_field,
4842 .get_credits = smb2_get_credits,
4843 .wait_mtu_credits = smb2_wait_mtu_credits,
4844 .adjust_credits = smb2_adjust_credits,
4845 .get_next_mid = smb2_get_next_mid,
4846 .revert_current_mid = smb2_revert_current_mid,
4847 .read_data_offset = smb2_read_data_offset,
4848 .read_data_length = smb2_read_data_length,
4849 .map_error = map_smb2_to_linux_error,
4850 .find_mid = smb2_find_mid,
4851 .check_message = smb2_check_message,
4852 .dump_detail = smb2_dump_detail,
4853 .clear_stats = smb2_clear_stats,
4854 .print_stats = smb2_print_stats,
4855 .is_oplock_break = smb2_is_valid_oplock_break,
4856 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4857 .downgrade_oplock = smb2_downgrade_oplock,
4858 .need_neg = smb2_need_neg,
4859 .negotiate = smb2_negotiate,
4860 .negotiate_wsize = smb2_negotiate_wsize,
4861 .negotiate_rsize = smb2_negotiate_rsize,
4862 .sess_setup = SMB2_sess_setup,
4863 .logoff = SMB2_logoff,
4864 .tree_connect = SMB2_tcon,
4865 .tree_disconnect = SMB2_tdis,
4866 .qfs_tcon = smb2_qfs_tcon,
4867 .is_path_accessible = smb2_is_path_accessible,
4868 .can_echo = smb2_can_echo,
4870 .query_path_info = smb2_query_path_info,
4871 .get_srv_inum = smb2_get_srv_inum,
4872 .query_file_info = smb2_query_file_info,
4873 .set_path_size = smb2_set_path_size,
4874 .set_file_size = smb2_set_file_size,
4875 .set_file_info = smb2_set_file_info,
4876 .set_compression = smb2_set_compression,
4877 .mkdir = smb2_mkdir,
4878 .mkdir_setinfo = smb2_mkdir_setinfo,
4879 .rmdir = smb2_rmdir,
4880 .unlink = smb2_unlink,
4881 .rename = smb2_rename_path,
4882 .create_hardlink = smb2_create_hardlink,
4883 .query_symlink = smb2_query_symlink,
4884 .query_mf_symlink = smb3_query_mf_symlink,
4885 .create_mf_symlink = smb3_create_mf_symlink,
4886 .open = smb2_open_file,
4887 .set_fid = smb2_set_fid,
4888 .close = smb2_close_file,
4889 .flush = smb2_flush_file,
4890 .async_readv = smb2_async_readv,
4891 .async_writev = smb2_async_writev,
4892 .sync_read = smb2_sync_read,
4893 .sync_write = smb2_sync_write,
4894 .query_dir_first = smb2_query_dir_first,
4895 .query_dir_next = smb2_query_dir_next,
4896 .close_dir = smb2_close_dir,
4897 .calc_smb_size = smb2_calc_size,
4898 .is_status_pending = smb2_is_status_pending,
4899 .is_session_expired = smb2_is_session_expired,
4900 .oplock_response = smb2_oplock_response,
4901 .queryfs = smb2_queryfs,
4902 .mand_lock = smb2_mand_lock,
4903 .mand_unlock_range = smb2_unlock_range,
4904 .push_mand_locks = smb2_push_mandatory_locks,
4905 .get_lease_key = smb2_get_lease_key,
4906 .set_lease_key = smb2_set_lease_key,
4907 .new_lease_key = smb2_new_lease_key,
4908 .calc_signature = smb2_calc_signature,
4909 .is_read_op = smb21_is_read_op,
4910 .set_oplock_level = smb21_set_oplock_level,
4911 .create_lease_buf = smb2_create_lease_buf,
4912 .parse_lease_buf = smb2_parse_lease_buf,
4913 .copychunk_range = smb2_copychunk_range,
4914 .wp_retry_size = smb2_wp_retry_size,
4915 .dir_needs_close = smb2_dir_needs_close,
4916 .enum_snapshots = smb3_enum_snapshots,
4917 .notify = smb3_notify,
4918 .get_dfs_refer = smb2_get_dfs_refer,
4919 .select_sectype = smb2_select_sectype,
4920 #ifdef CONFIG_CIFS_XATTR
4921 .query_all_EAs = smb2_query_eas,
4922 .set_EA = smb2_set_ea,
4923 #endif /* CIFS_XATTR */
4924 .get_acl = get_smb2_acl,
4925 .get_acl_by_fid = get_smb2_acl_by_fid,
4926 .set_acl = set_smb2_acl,
4927 .next_header = smb2_next_header,
4928 .ioctl_query_info = smb2_ioctl_query_info,
4929 .make_node = smb2_make_node,
4930 .fiemap = smb3_fiemap,
4931 .llseek = smb3_llseek,
4932 .is_status_io_timeout = smb2_is_status_io_timeout,
4935 struct smb_version_operations smb30_operations = {
4936 .compare_fids = smb2_compare_fids,
4937 .setup_request = smb2_setup_request,
4938 .setup_async_request = smb2_setup_async_request,
4939 .check_receive = smb2_check_receive,
4940 .add_credits = smb2_add_credits,
4941 .set_credits = smb2_set_credits,
4942 .get_credits_field = smb2_get_credits_field,
4943 .get_credits = smb2_get_credits,
4944 .wait_mtu_credits = smb2_wait_mtu_credits,
4945 .adjust_credits = smb2_adjust_credits,
4946 .get_next_mid = smb2_get_next_mid,
4947 .revert_current_mid = smb2_revert_current_mid,
4948 .read_data_offset = smb2_read_data_offset,
4949 .read_data_length = smb2_read_data_length,
4950 .map_error = map_smb2_to_linux_error,
4951 .find_mid = smb2_find_mid,
4952 .check_message = smb2_check_message,
4953 .dump_detail = smb2_dump_detail,
4954 .clear_stats = smb2_clear_stats,
4955 .print_stats = smb2_print_stats,
4956 .dump_share_caps = smb2_dump_share_caps,
4957 .is_oplock_break = smb2_is_valid_oplock_break,
4958 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4959 .downgrade_oplock = smb3_downgrade_oplock,
4960 .need_neg = smb2_need_neg,
4961 .negotiate = smb2_negotiate,
4962 .negotiate_wsize = smb3_negotiate_wsize,
4963 .negotiate_rsize = smb3_negotiate_rsize,
4964 .sess_setup = SMB2_sess_setup,
4965 .logoff = SMB2_logoff,
4966 .tree_connect = SMB2_tcon,
4967 .tree_disconnect = SMB2_tdis,
4968 .qfs_tcon = smb3_qfs_tcon,
4969 .is_path_accessible = smb2_is_path_accessible,
4970 .can_echo = smb2_can_echo,
4972 .query_path_info = smb2_query_path_info,
4973 .get_srv_inum = smb2_get_srv_inum,
4974 .query_file_info = smb2_query_file_info,
4975 .set_path_size = smb2_set_path_size,
4976 .set_file_size = smb2_set_file_size,
4977 .set_file_info = smb2_set_file_info,
4978 .set_compression = smb2_set_compression,
4979 .mkdir = smb2_mkdir,
4980 .mkdir_setinfo = smb2_mkdir_setinfo,
4981 .rmdir = smb2_rmdir,
4982 .unlink = smb2_unlink,
4983 .rename = smb2_rename_path,
4984 .create_hardlink = smb2_create_hardlink,
4985 .query_symlink = smb2_query_symlink,
4986 .query_mf_symlink = smb3_query_mf_symlink,
4987 .create_mf_symlink = smb3_create_mf_symlink,
4988 .open = smb2_open_file,
4989 .set_fid = smb2_set_fid,
4990 .close = smb2_close_file,
4991 .close_getattr = smb2_close_getattr,
4992 .flush = smb2_flush_file,
4993 .async_readv = smb2_async_readv,
4994 .async_writev = smb2_async_writev,
4995 .sync_read = smb2_sync_read,
4996 .sync_write = smb2_sync_write,
4997 .query_dir_first = smb2_query_dir_first,
4998 .query_dir_next = smb2_query_dir_next,
4999 .close_dir = smb2_close_dir,
5000 .calc_smb_size = smb2_calc_size,
5001 .is_status_pending = smb2_is_status_pending,
5002 .is_session_expired = smb2_is_session_expired,
5003 .oplock_response = smb2_oplock_response,
5004 .queryfs = smb2_queryfs,
5005 .mand_lock = smb2_mand_lock,
5006 .mand_unlock_range = smb2_unlock_range,
5007 .push_mand_locks = smb2_push_mandatory_locks,
5008 .get_lease_key = smb2_get_lease_key,
5009 .set_lease_key = smb2_set_lease_key,
5010 .new_lease_key = smb2_new_lease_key,
5011 .generate_signingkey = generate_smb30signingkey,
5012 .calc_signature = smb3_calc_signature,
5013 .set_integrity = smb3_set_integrity,
5014 .is_read_op = smb21_is_read_op,
5015 .set_oplock_level = smb3_set_oplock_level,
5016 .create_lease_buf = smb3_create_lease_buf,
5017 .parse_lease_buf = smb3_parse_lease_buf,
5018 .copychunk_range = smb2_copychunk_range,
5019 .duplicate_extents = smb2_duplicate_extents,
5020 .validate_negotiate = smb3_validate_negotiate,
5021 .wp_retry_size = smb2_wp_retry_size,
5022 .dir_needs_close = smb2_dir_needs_close,
5023 .fallocate = smb3_fallocate,
5024 .enum_snapshots = smb3_enum_snapshots,
5025 .notify = smb3_notify,
5026 .init_transform_rq = smb3_init_transform_rq,
5027 .is_transform_hdr = smb3_is_transform_hdr,
5028 .receive_transform = smb3_receive_transform,
5029 .get_dfs_refer = smb2_get_dfs_refer,
5030 .select_sectype = smb2_select_sectype,
5031 #ifdef CONFIG_CIFS_XATTR
5032 .query_all_EAs = smb2_query_eas,
5033 .set_EA = smb2_set_ea,
5034 #endif /* CIFS_XATTR */
5035 .get_acl = get_smb2_acl,
5036 .get_acl_by_fid = get_smb2_acl_by_fid,
5037 .set_acl = set_smb2_acl,
5038 .next_header = smb2_next_header,
5039 .ioctl_query_info = smb2_ioctl_query_info,
5040 .make_node = smb2_make_node,
5041 .fiemap = smb3_fiemap,
5042 .llseek = smb3_llseek,
5043 .is_status_io_timeout = smb2_is_status_io_timeout,
5046 struct smb_version_operations smb311_operations = {
5047 .compare_fids = smb2_compare_fids,
5048 .setup_request = smb2_setup_request,
5049 .setup_async_request = smb2_setup_async_request,
5050 .check_receive = smb2_check_receive,
5051 .add_credits = smb2_add_credits,
5052 .set_credits = smb2_set_credits,
5053 .get_credits_field = smb2_get_credits_field,
5054 .get_credits = smb2_get_credits,
5055 .wait_mtu_credits = smb2_wait_mtu_credits,
5056 .adjust_credits = smb2_adjust_credits,
5057 .get_next_mid = smb2_get_next_mid,
5058 .revert_current_mid = smb2_revert_current_mid,
5059 .read_data_offset = smb2_read_data_offset,
5060 .read_data_length = smb2_read_data_length,
5061 .map_error = map_smb2_to_linux_error,
5062 .find_mid = smb2_find_mid,
5063 .check_message = smb2_check_message,
5064 .dump_detail = smb2_dump_detail,
5065 .clear_stats = smb2_clear_stats,
5066 .print_stats = smb2_print_stats,
5067 .dump_share_caps = smb2_dump_share_caps,
5068 .is_oplock_break = smb2_is_valid_oplock_break,
5069 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5070 .downgrade_oplock = smb3_downgrade_oplock,
5071 .need_neg = smb2_need_neg,
5072 .negotiate = smb2_negotiate,
5073 .negotiate_wsize = smb3_negotiate_wsize,
5074 .negotiate_rsize = smb3_negotiate_rsize,
5075 .sess_setup = SMB2_sess_setup,
5076 .logoff = SMB2_logoff,
5077 .tree_connect = SMB2_tcon,
5078 .tree_disconnect = SMB2_tdis,
5079 .qfs_tcon = smb3_qfs_tcon,
5080 .is_path_accessible = smb2_is_path_accessible,
5081 .can_echo = smb2_can_echo,
5083 .query_path_info = smb2_query_path_info,
5084 .get_srv_inum = smb2_get_srv_inum,
5085 .query_file_info = smb2_query_file_info,
5086 .set_path_size = smb2_set_path_size,
5087 .set_file_size = smb2_set_file_size,
5088 .set_file_info = smb2_set_file_info,
5089 .set_compression = smb2_set_compression,
5090 .mkdir = smb2_mkdir,
5091 .mkdir_setinfo = smb2_mkdir_setinfo,
5092 .posix_mkdir = smb311_posix_mkdir,
5093 .rmdir = smb2_rmdir,
5094 .unlink = smb2_unlink,
5095 .rename = smb2_rename_path,
5096 .create_hardlink = smb2_create_hardlink,
5097 .query_symlink = smb2_query_symlink,
5098 .query_mf_symlink = smb3_query_mf_symlink,
5099 .create_mf_symlink = smb3_create_mf_symlink,
5100 .open = smb2_open_file,
5101 .set_fid = smb2_set_fid,
5102 .close = smb2_close_file,
5103 .close_getattr = smb2_close_getattr,
5104 .flush = smb2_flush_file,
5105 .async_readv = smb2_async_readv,
5106 .async_writev = smb2_async_writev,
5107 .sync_read = smb2_sync_read,
5108 .sync_write = smb2_sync_write,
5109 .query_dir_first = smb2_query_dir_first,
5110 .query_dir_next = smb2_query_dir_next,
5111 .close_dir = smb2_close_dir,
5112 .calc_smb_size = smb2_calc_size,
5113 .is_status_pending = smb2_is_status_pending,
5114 .is_session_expired = smb2_is_session_expired,
5115 .oplock_response = smb2_oplock_response,
5116 .queryfs = smb311_queryfs,
5117 .mand_lock = smb2_mand_lock,
5118 .mand_unlock_range = smb2_unlock_range,
5119 .push_mand_locks = smb2_push_mandatory_locks,
5120 .get_lease_key = smb2_get_lease_key,
5121 .set_lease_key = smb2_set_lease_key,
5122 .new_lease_key = smb2_new_lease_key,
5123 .generate_signingkey = generate_smb311signingkey,
5124 .calc_signature = smb3_calc_signature,
5125 .set_integrity = smb3_set_integrity,
5126 .is_read_op = smb21_is_read_op,
5127 .set_oplock_level = smb3_set_oplock_level,
5128 .create_lease_buf = smb3_create_lease_buf,
5129 .parse_lease_buf = smb3_parse_lease_buf,
5130 .copychunk_range = smb2_copychunk_range,
5131 .duplicate_extents = smb2_duplicate_extents,
5132 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5133 .wp_retry_size = smb2_wp_retry_size,
5134 .dir_needs_close = smb2_dir_needs_close,
5135 .fallocate = smb3_fallocate,
5136 .enum_snapshots = smb3_enum_snapshots,
5137 .notify = smb3_notify,
5138 .init_transform_rq = smb3_init_transform_rq,
5139 .is_transform_hdr = smb3_is_transform_hdr,
5140 .receive_transform = smb3_receive_transform,
5141 .get_dfs_refer = smb2_get_dfs_refer,
5142 .select_sectype = smb2_select_sectype,
5143 #ifdef CONFIG_CIFS_XATTR
5144 .query_all_EAs = smb2_query_eas,
5145 .set_EA = smb2_set_ea,
5146 #endif /* CIFS_XATTR */
5147 .get_acl = get_smb2_acl,
5148 .get_acl_by_fid = get_smb2_acl_by_fid,
5149 .set_acl = set_smb2_acl,
5150 .next_header = smb2_next_header,
5151 .ioctl_query_info = smb2_ioctl_query_info,
5152 .make_node = smb2_make_node,
5153 .fiemap = smb3_fiemap,
5154 .llseek = smb3_llseek,
5155 .is_status_io_timeout = smb2_is_status_io_timeout,
5158 struct smb_version_values smb20_values = {
5159 .version_string = SMB20_VERSION_STRING,
5160 .protocol_id = SMB20_PROT_ID,
5161 .req_capabilities = 0, /* MBZ */
5162 .large_lock_type = 0,
5163 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5164 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5165 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5166 .header_size = sizeof(struct smb2_sync_hdr),
5167 .header_preamble_size = 0,
5168 .max_header_size = MAX_SMB2_HDR_SIZE,
5169 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5170 .lock_cmd = SMB2_LOCK,
5172 .cap_nt_find = SMB2_NT_FIND,
5173 .cap_large_files = SMB2_LARGE_FILES,
5174 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5175 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5176 .create_lease_size = sizeof(struct create_lease),
5179 struct smb_version_values smb21_values = {
5180 .version_string = SMB21_VERSION_STRING,
5181 .protocol_id = SMB21_PROT_ID,
5182 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5183 .large_lock_type = 0,
5184 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5185 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5186 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5187 .header_size = sizeof(struct smb2_sync_hdr),
5188 .header_preamble_size = 0,
5189 .max_header_size = MAX_SMB2_HDR_SIZE,
5190 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5191 .lock_cmd = SMB2_LOCK,
5193 .cap_nt_find = SMB2_NT_FIND,
5194 .cap_large_files = SMB2_LARGE_FILES,
5195 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5196 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5197 .create_lease_size = sizeof(struct create_lease),
5200 struct smb_version_values smb3any_values = {
5201 .version_string = SMB3ANY_VERSION_STRING,
5202 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5203 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
5204 .large_lock_type = 0,
5205 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5206 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5207 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5208 .header_size = sizeof(struct smb2_sync_hdr),
5209 .header_preamble_size = 0,
5210 .max_header_size = MAX_SMB2_HDR_SIZE,
5211 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5212 .lock_cmd = SMB2_LOCK,
5214 .cap_nt_find = SMB2_NT_FIND,
5215 .cap_large_files = SMB2_LARGE_FILES,
5216 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5217 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5218 .create_lease_size = sizeof(struct create_lease_v2),
5221 struct smb_version_values smbdefault_values = {
5222 .version_string = SMBDEFAULT_VERSION_STRING,
5223 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5224 .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,
5225 .large_lock_type = 0,
5226 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5227 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5228 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5229 .header_size = sizeof(struct smb2_sync_hdr),
5230 .header_preamble_size = 0,
5231 .max_header_size = MAX_SMB2_HDR_SIZE,
5232 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5233 .lock_cmd = SMB2_LOCK,
5235 .cap_nt_find = SMB2_NT_FIND,
5236 .cap_large_files = SMB2_LARGE_FILES,
5237 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5238 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5239 .create_lease_size = sizeof(struct create_lease_v2),
5242 struct smb_version_values smb30_values = {
5243 .version_string = SMB30_VERSION_STRING,
5244 .protocol_id = SMB30_PROT_ID,
5245 .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,
5246 .large_lock_type = 0,
5247 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5248 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5249 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5250 .header_size = sizeof(struct smb2_sync_hdr),
5251 .header_preamble_size = 0,
5252 .max_header_size = MAX_SMB2_HDR_SIZE,
5253 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5254 .lock_cmd = SMB2_LOCK,
5256 .cap_nt_find = SMB2_NT_FIND,
5257 .cap_large_files = SMB2_LARGE_FILES,
5258 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5259 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5260 .create_lease_size = sizeof(struct create_lease_v2),
5263 struct smb_version_values smb302_values = {
5264 .version_string = SMB302_VERSION_STRING,
5265 .protocol_id = SMB302_PROT_ID,
5266 .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,
5267 .large_lock_type = 0,
5268 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5269 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5270 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5271 .header_size = sizeof(struct smb2_sync_hdr),
5272 .header_preamble_size = 0,
5273 .max_header_size = MAX_SMB2_HDR_SIZE,
5274 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5275 .lock_cmd = SMB2_LOCK,
5277 .cap_nt_find = SMB2_NT_FIND,
5278 .cap_large_files = SMB2_LARGE_FILES,
5279 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5280 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5281 .create_lease_size = sizeof(struct create_lease_v2),
5284 struct smb_version_values smb311_values = {
5285 .version_string = SMB311_VERSION_STRING,
5286 .protocol_id = SMB311_PROT_ID,
5287 .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,
5288 .large_lock_type = 0,
5289 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5290 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5291 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5292 .header_size = sizeof(struct smb2_sync_hdr),
5293 .header_preamble_size = 0,
5294 .max_header_size = MAX_SMB2_HDR_SIZE,
5295 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5296 .lock_cmd = SMB2_LOCK,
5298 .cap_nt_find = SMB2_NT_FIND,
5299 .cap_large_files = SMB2_LARGE_FILES,
5300 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5301 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5302 .create_lease_size = sizeof(struct create_lease_v2),