1 // SPDX-License-Identifier: GPL-2.0
3 * SMB2 version specific operations
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License v2 as published
9 * by the Free Software Foundation.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/pagemap.h>
22 #include <linux/vfs.h>
23 #include <linux/falloc.h>
24 #include <linux/scatterlist.h>
25 #include <linux/uuid.h>
26 #include <crypto/aead.h>
29 #include "smb2proto.h"
30 #include "cifsproto.h"
31 #include "cifs_debug.h"
32 #include "cifs_unicode.h"
33 #include "smb2status.h"
35 #include "cifs_ioctl.h"
36 #include "smbdirect.h"
38 /* Change credits for different ops and return the total number of credits */
40 change_conf(struct TCP_Server_Info *server)
42 server->credits += server->echo_credits + server->oplock_credits;
43 server->oplock_credits = server->echo_credits = 0;
44 switch (server->credits) {
48 server->echoes = false;
49 server->oplocks = false;
52 server->echoes = true;
53 server->oplocks = false;
54 server->echo_credits = 1;
57 server->echoes = true;
59 server->oplocks = true;
60 server->oplock_credits = 1;
62 server->oplocks = false;
64 server->echo_credits = 1;
66 server->credits -= server->echo_credits + server->oplock_credits;
67 return server->credits + server->echo_credits + server->oplock_credits;
71 smb2_add_credits(struct TCP_Server_Info *server,
72 const struct cifs_credits *credits, const int optype)
75 unsigned int add = credits->value;
76 unsigned int instance = credits->instance;
77 bool reconnect_detected = false;
79 spin_lock(&server->req_lock);
80 val = server->ops->get_credits_field(server, optype);
82 /* eg found case where write overlapping reconnect messed up credits */
83 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
84 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
85 server->hostname, *val);
86 if ((instance == 0) || (instance == server->reconnect_instance))
89 reconnect_detected = true;
92 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
93 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
96 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
97 rc = change_conf(server);
99 * Sometimes server returns 0 credits on oplock break ack - we need to
100 * rebalance credits in this case.
102 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
104 if (server->credits > 1) {
106 server->oplock_credits++;
109 spin_unlock(&server->req_lock);
110 wake_up(&server->request_q);
112 if (reconnect_detected)
113 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
116 if (server->tcpStatus == CifsNeedReconnect
117 || server->tcpStatus == CifsExiting)
122 /* change_conf hasn't been executed */
125 cifs_dbg(VFS, "Possible client or server bug - zero credits\n");
128 cifs_dbg(VFS, "disabling echoes and oplocks\n");
131 cifs_dbg(FYI, "disabling oplocks\n");
134 cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
139 smb2_set_credits(struct TCP_Server_Info *server, const int val)
141 spin_lock(&server->req_lock);
142 server->credits = val;
144 server->reconnect_instance++;
145 spin_unlock(&server->req_lock);
146 /* don't log while holding the lock */
148 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
152 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
156 return &server->echo_credits;
158 return &server->oplock_credits;
160 return &server->credits;
165 smb2_get_credits(struct mid_q_entry *mid)
167 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)mid->resp_buf;
169 if (mid->mid_state == MID_RESPONSE_RECEIVED
170 || mid->mid_state == MID_RESPONSE_MALFORMED)
171 return le16_to_cpu(shdr->CreditRequest);
177 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
178 unsigned int *num, struct cifs_credits *credits)
181 unsigned int scredits;
183 spin_lock(&server->req_lock);
185 if (server->credits <= 0) {
186 spin_unlock(&server->req_lock);
187 cifs_num_waiters_inc(server);
188 rc = wait_event_killable(server->request_q,
189 has_credits(server, &server->credits, 1));
190 cifs_num_waiters_dec(server);
193 spin_lock(&server->req_lock);
195 if (server->tcpStatus == CifsExiting) {
196 spin_unlock(&server->req_lock);
200 scredits = server->credits;
201 /* can deadlock with reopen */
203 *num = SMB2_MAX_BUFFER_SIZE;
205 credits->instance = 0;
209 /* leave some credits for reopen and other ops */
211 *num = min_t(unsigned int, size,
212 scredits * SMB2_MAX_BUFFER_SIZE);
215 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
216 credits->instance = server->reconnect_instance;
217 server->credits -= credits->value;
222 spin_unlock(&server->req_lock);
227 smb2_adjust_credits(struct TCP_Server_Info *server,
228 struct cifs_credits *credits,
229 const unsigned int payload_size)
231 int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
233 if (!credits->value || credits->value == new_val)
236 if (credits->value < new_val) {
237 WARN_ONCE(1, "request has less credits (%d) than required (%d)",
238 credits->value, new_val);
242 spin_lock(&server->req_lock);
244 if (server->reconnect_instance != credits->instance) {
245 spin_unlock(&server->req_lock);
246 cifs_dbg(VFS, "trying to return %d credits to old session\n",
247 credits->value - new_val);
251 server->credits += credits->value - new_val;
252 spin_unlock(&server->req_lock);
253 wake_up(&server->request_q);
254 credits->value = new_val;
259 smb2_get_next_mid(struct TCP_Server_Info *server)
262 /* for SMB2 we need the current value */
263 spin_lock(&GlobalMid_Lock);
264 mid = server->CurrentMid++;
265 spin_unlock(&GlobalMid_Lock);
270 smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
272 spin_lock(&GlobalMid_Lock);
273 if (server->CurrentMid >= val)
274 server->CurrentMid -= val;
275 spin_unlock(&GlobalMid_Lock);
278 static struct mid_q_entry *
279 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
281 struct mid_q_entry *mid;
282 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
283 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
285 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
286 cifs_dbg(VFS, "Encrypted frame parsing not supported yet\n");
290 spin_lock(&GlobalMid_Lock);
291 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
292 if ((mid->mid == wire_mid) &&
293 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
294 (mid->command == shdr->Command)) {
295 kref_get(&mid->refcount);
296 spin_unlock(&GlobalMid_Lock);
300 spin_unlock(&GlobalMid_Lock);
305 smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
307 #ifdef CONFIG_CIFS_DEBUG2
308 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
310 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
311 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
313 cifs_dbg(VFS, "smb buf %p len %u\n", buf,
314 server->ops->calc_smb_size(buf, server));
319 smb2_need_neg(struct TCP_Server_Info *server)
321 return server->max_read == 0;
325 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
329 ses->server->CurrentMid = 0;
330 rc = SMB2_negotiate(xid, ses);
331 /* BB we probably don't need to retry with modern servers */
338 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
340 struct TCP_Server_Info *server = tcon->ses->server;
343 /* start with specified wsize, or default */
344 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
345 wsize = min_t(unsigned int, wsize, server->max_write);
346 #ifdef CONFIG_CIFS_SMB_DIRECT
349 wsize = min_t(unsigned int,
350 wsize, server->smbd_conn->max_fragmented_send_size);
352 wsize = min_t(unsigned int,
353 wsize, server->smbd_conn->max_readwrite_size);
356 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
357 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
363 smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
365 struct TCP_Server_Info *server = tcon->ses->server;
368 /* start with specified wsize, or default */
369 wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
370 wsize = min_t(unsigned int, wsize, server->max_write);
371 #ifdef CONFIG_CIFS_SMB_DIRECT
374 wsize = min_t(unsigned int,
375 wsize, server->smbd_conn->max_fragmented_send_size);
377 wsize = min_t(unsigned int,
378 wsize, server->smbd_conn->max_readwrite_size);
381 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
382 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
388 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
390 struct TCP_Server_Info *server = tcon->ses->server;
393 /* start with specified rsize, or default */
394 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
395 rsize = min_t(unsigned int, rsize, server->max_read);
396 #ifdef CONFIG_CIFS_SMB_DIRECT
399 rsize = min_t(unsigned int,
400 rsize, server->smbd_conn->max_fragmented_recv_size);
402 rsize = min_t(unsigned int,
403 rsize, server->smbd_conn->max_readwrite_size);
407 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
408 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
414 smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
416 struct TCP_Server_Info *server = tcon->ses->server;
419 /* start with specified rsize, or default */
420 rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
421 rsize = min_t(unsigned int, rsize, server->max_read);
422 #ifdef CONFIG_CIFS_SMB_DIRECT
425 rsize = min_t(unsigned int,
426 rsize, server->smbd_conn->max_fragmented_recv_size);
428 rsize = min_t(unsigned int,
429 rsize, server->smbd_conn->max_readwrite_size);
433 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
434 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
440 parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
442 struct cifs_server_iface **iface_list,
445 struct network_interface_info_ioctl_rsp *p;
446 struct sockaddr_in *addr4;
447 struct sockaddr_in6 *addr6;
448 struct iface_info_ipv4 *p4;
449 struct iface_info_ipv6 *p6;
450 struct cifs_server_iface *info;
460 * Fist pass: count and sanity check
463 bytes_left = buf_len;
465 while (bytes_left >= sizeof(*p)) {
467 next = le32_to_cpu(p->Next);
469 bytes_left -= sizeof(*p);
472 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
477 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
482 if (bytes_left || p->Next)
483 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
487 * Second pass: extract info to internal structure
490 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
497 bytes_left = buf_len;
499 while (bytes_left >= sizeof(*p)) {
500 info->speed = le64_to_cpu(p->LinkSpeed);
501 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE);
502 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE);
504 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
505 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
506 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
507 le32_to_cpu(p->Capability));
511 * The kernel and wire socket structures have the same
512 * layout and use network byte order but make the
513 * conversion explicit in case either one changes.
516 addr4 = (struct sockaddr_in *)&info->sockaddr;
517 p4 = (struct iface_info_ipv4 *)p->Buffer;
518 addr4->sin_family = AF_INET;
519 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
521 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
522 addr4->sin_port = cpu_to_be16(CIFS_PORT);
524 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
528 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
529 p6 = (struct iface_info_ipv6 *)p->Buffer;
530 addr6->sin6_family = AF_INET6;
531 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
533 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
534 addr6->sin6_flowinfo = 0;
535 addr6->sin6_scope_id = 0;
536 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
538 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
543 "%s: skipping unsupported socket family\n",
551 next = le32_to_cpu(p->Next);
554 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
574 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
577 unsigned int ret_data_len = 0;
578 struct network_interface_info_ioctl_rsp *out_buf = NULL;
579 struct cifs_server_iface *iface_list;
581 struct cifs_ses *ses = tcon->ses;
583 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
584 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
585 NULL /* no data input */, 0 /* no data input */,
586 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
587 if (rc == -EOPNOTSUPP) {
589 "server does not support query network interfaces\n");
591 } else if (rc != 0) {
592 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
596 rc = parse_server_interfaces(out_buf, ret_data_len,
597 &iface_list, &iface_count);
601 spin_lock(&ses->iface_lock);
602 kfree(ses->iface_list);
603 ses->iface_list = iface_list;
604 ses->iface_count = iface_count;
605 ses->iface_last_update = jiffies;
606 spin_unlock(&ses->iface_lock);
614 smb2_close_cached_fid(struct kref *ref)
616 struct cached_fid *cfid = container_of(ref, struct cached_fid,
619 if (cfid->is_valid) {
620 cifs_dbg(FYI, "clear cached root file handle\n");
621 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
622 cfid->fid->volatile_fid);
623 cfid->is_valid = false;
624 cfid->file_all_info_is_valid = false;
628 void close_shroot(struct cached_fid *cfid)
630 mutex_lock(&cfid->fid_mutex);
631 kref_put(&cfid->refcount, smb2_close_cached_fid);
632 mutex_unlock(&cfid->fid_mutex);
636 smb2_cached_lease_break(struct work_struct *work)
638 struct cached_fid *cfid = container_of(work,
639 struct cached_fid, lease_break);
645 * Open the directory at the root of a share
647 int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
649 struct cifs_ses *ses = tcon->ses;
650 struct TCP_Server_Info *server = ses->server;
651 struct cifs_open_parms oparms;
652 struct smb2_create_rsp *o_rsp = NULL;
653 struct smb2_query_info_rsp *qi_rsp = NULL;
655 struct smb_rqst rqst[2];
656 struct kvec rsp_iov[2];
657 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
658 struct kvec qi_iov[1];
660 __le16 utf16_path = 0; /* Null - since an open of top of share */
661 u8 oplock = SMB2_OPLOCK_LEVEL_II;
663 mutex_lock(&tcon->crfid.fid_mutex);
664 if (tcon->crfid.is_valid) {
665 cifs_dbg(FYI, "found a cached root file handle\n");
666 memcpy(pfid, tcon->crfid.fid, sizeof(struct cifs_fid));
667 kref_get(&tcon->crfid.refcount);
668 mutex_unlock(&tcon->crfid.fid_mutex);
672 if (smb3_encryption_required(tcon))
673 flags |= CIFS_TRANSFORM_REQ;
675 memset(rqst, 0, sizeof(rqst));
676 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
677 memset(rsp_iov, 0, sizeof(rsp_iov));
680 memset(&open_iov, 0, sizeof(open_iov));
681 rqst[0].rq_iov = open_iov;
682 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
685 oparms.create_options = 0;
686 oparms.desired_access = FILE_READ_ATTRIBUTES;
687 oparms.disposition = FILE_OPEN;
689 oparms.reconnect = false;
691 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, &utf16_path);
694 smb2_set_next_command(tcon, &rqst[0]);
696 memset(&qi_iov, 0, sizeof(qi_iov));
697 rqst[1].rq_iov = qi_iov;
700 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
701 COMPOUND_FID, FILE_ALL_INFORMATION,
703 sizeof(struct smb2_file_all_info) +
704 PATH_MAX * 2, 0, NULL);
708 smb2_set_related(&rqst[1]);
710 rc = compound_send_recv(xid, ses, flags, 2, rqst,
711 resp_buftype, rsp_iov);
715 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
716 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
717 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
718 #ifdef CONFIG_CIFS_DEBUG2
719 oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
720 #endif /* CIFS_DEBUG2 */
722 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
723 tcon->crfid.tcon = tcon;
724 tcon->crfid.is_valid = true;
725 kref_init(&tcon->crfid.refcount);
727 if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
728 kref_get(&tcon->crfid.refcount);
729 oplock = smb2_parse_lease_state(server, o_rsp,
731 oparms.fid->lease_key);
735 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
736 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
738 if (!smb2_validate_and_copy_iov(
739 le16_to_cpu(qi_rsp->OutputBufferOffset),
740 sizeof(struct smb2_file_all_info),
741 &rsp_iov[1], sizeof(struct smb2_file_all_info),
742 (char *)&tcon->crfid.file_all_info))
743 tcon->crfid.file_all_info_is_valid = 1;
746 mutex_unlock(&tcon->crfid.fid_mutex);
747 SMB2_open_free(&rqst[0]);
748 SMB2_query_info_free(&rqst[1]);
749 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
750 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
755 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
758 __le16 srch_path = 0; /* Null - open root of share */
759 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
760 struct cifs_open_parms oparms;
762 bool no_cached_open = tcon->nohandlecache;
765 oparms.desired_access = FILE_READ_ATTRIBUTES;
766 oparms.disposition = FILE_OPEN;
767 oparms.create_options = 0;
769 oparms.reconnect = false;
772 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
775 rc = open_shroot(xid, tcon, &fid);
780 SMB3_request_interfaces(xid, tcon);
782 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
783 FS_ATTRIBUTE_INFORMATION);
784 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
785 FS_DEVICE_INFORMATION);
786 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
787 FS_VOLUME_INFORMATION);
788 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
789 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
791 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
793 close_shroot(&tcon->crfid);
797 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
800 __le16 srch_path = 0; /* Null - open root of share */
801 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
802 struct cifs_open_parms oparms;
806 oparms.desired_access = FILE_READ_ATTRIBUTES;
807 oparms.disposition = FILE_OPEN;
808 oparms.create_options = 0;
810 oparms.reconnect = false;
812 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
816 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
817 FS_ATTRIBUTE_INFORMATION);
818 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
819 FS_DEVICE_INFORMATION);
820 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
824 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
825 struct cifs_sb_info *cifs_sb, const char *full_path)
829 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
830 struct cifs_open_parms oparms;
833 if ((*full_path == 0) && tcon->crfid.is_valid)
836 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
841 oparms.desired_access = FILE_READ_ATTRIBUTES;
842 oparms.disposition = FILE_OPEN;
843 if (backup_cred(cifs_sb))
844 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
846 oparms.create_options = 0;
848 oparms.reconnect = false;
850 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
856 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
862 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
863 struct cifs_sb_info *cifs_sb, const char *full_path,
864 u64 *uniqueid, FILE_ALL_INFO *data)
866 *uniqueid = le64_to_cpu(data->IndexNumber);
871 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
872 struct cifs_fid *fid, FILE_ALL_INFO *data)
875 struct smb2_file_all_info *smb2_data;
877 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
879 if (smb2_data == NULL)
882 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
885 move_smb2_info_to_cifs(data, smb2_data);
890 #ifdef CONFIG_CIFS_XATTR
892 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
893 struct smb2_file_full_ea_info *src, size_t src_size,
894 const unsigned char *ea_name)
897 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
899 size_t buf_size = dst_size;
900 size_t name_len, value_len, user_name_len;
902 while (src_size > 0) {
903 name = &src->ea_data[0];
904 name_len = (size_t)src->ea_name_length;
905 value = &src->ea_data[src->ea_name_length + 1];
906 value_len = (size_t)le16_to_cpu(src->ea_value_length);
911 if (src_size < 8 + name_len + 1 + value_len) {
912 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
918 if (ea_name_len == name_len &&
919 memcmp(ea_name, name, name_len) == 0) {
923 if (dst_size < value_len) {
927 memcpy(dst, value, value_len);
931 /* 'user.' plus a terminating null */
932 user_name_len = 5 + 1 + name_len;
935 /* skip copy - calc size only */
937 } else if (dst_size >= user_name_len) {
938 dst_size -= user_name_len;
939 memcpy(dst, "user.", 5);
941 memcpy(dst, src->ea_data, name_len);
947 /* stop before overrun buffer */
953 if (!src->next_entry_offset)
956 if (src_size < le32_to_cpu(src->next_entry_offset)) {
957 /* stop before overrun buffer */
961 src_size -= le32_to_cpu(src->next_entry_offset);
962 src = (void *)((char *)src +
963 le32_to_cpu(src->next_entry_offset));
966 /* didn't find the named attribute */
975 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
976 const unsigned char *path, const unsigned char *ea_name,
977 char *ea_data, size_t buf_size,
978 struct cifs_sb_info *cifs_sb)
982 struct kvec rsp_iov = {NULL, 0};
983 int buftype = CIFS_NO_BUFFER;
984 struct smb2_query_info_rsp *rsp;
985 struct smb2_file_full_ea_info *info = NULL;
987 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
991 rc = smb2_query_info_compound(xid, tcon, utf16_path,
993 FILE_FULL_EA_INFORMATION,
996 MAX_SMB2_CREATE_RESPONSE_SIZE -
997 MAX_SMB2_CLOSE_RESPONSE_SIZE,
998 &rsp_iov, &buftype, cifs_sb);
1001 * If ea_name is NULL (listxattr) and there are no EAs,
1002 * return 0 as it's not an error. Otherwise, the specified
1003 * ea_name was not found.
1005 if (!ea_name && rc == -ENODATA)
1010 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1011 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1012 le32_to_cpu(rsp->OutputBufferLength),
1014 sizeof(struct smb2_file_full_ea_info));
1018 info = (struct smb2_file_full_ea_info *)(
1019 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1020 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1021 le32_to_cpu(rsp->OutputBufferLength), ea_name);
1025 free_rsp_buf(buftype, rsp_iov.iov_base);
1031 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1032 const char *path, const char *ea_name, const void *ea_value,
1033 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1034 struct cifs_sb_info *cifs_sb)
1036 struct cifs_ses *ses = tcon->ses;
1037 __le16 *utf16_path = NULL;
1038 int ea_name_len = strlen(ea_name);
1041 struct smb_rqst rqst[3];
1042 int resp_buftype[3];
1043 struct kvec rsp_iov[3];
1044 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1045 struct cifs_open_parms oparms;
1046 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1047 struct cifs_fid fid;
1048 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1049 unsigned int size[1];
1051 struct smb2_file_full_ea_info *ea = NULL;
1052 struct kvec close_iov[1];
1055 if (smb3_encryption_required(tcon))
1056 flags |= CIFS_TRANSFORM_REQ;
1058 if (ea_name_len > 255)
1061 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1065 memset(rqst, 0, sizeof(rqst));
1066 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1067 memset(rsp_iov, 0, sizeof(rsp_iov));
1069 if (ses->server->ops->query_all_EAs) {
1071 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1080 memset(&open_iov, 0, sizeof(open_iov));
1081 rqst[0].rq_iov = open_iov;
1082 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1084 memset(&oparms, 0, sizeof(oparms));
1086 oparms.desired_access = FILE_WRITE_EA;
1087 oparms.disposition = FILE_OPEN;
1088 if (backup_cred(cifs_sb))
1089 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1091 oparms.create_options = 0;
1093 oparms.reconnect = false;
1095 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
1098 smb2_set_next_command(tcon, &rqst[0]);
1102 memset(&si_iov, 0, sizeof(si_iov));
1103 rqst[1].rq_iov = si_iov;
1104 rqst[1].rq_nvec = 1;
1106 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
1107 ea = kzalloc(len, GFP_KERNEL);
1113 ea->ea_name_length = ea_name_len;
1114 ea->ea_value_length = cpu_to_le16(ea_value_len);
1115 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1116 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1121 rc = SMB2_set_info_init(tcon, &rqst[1], COMPOUND_FID,
1122 COMPOUND_FID, current->tgid,
1123 FILE_FULL_EA_INFORMATION,
1124 SMB2_O_INFO_FILE, 0, data, size);
1125 smb2_set_next_command(tcon, &rqst[1]);
1126 smb2_set_related(&rqst[1]);
1130 memset(&close_iov, 0, sizeof(close_iov));
1131 rqst[2].rq_iov = close_iov;
1132 rqst[2].rq_nvec = 1;
1133 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1134 smb2_set_related(&rqst[2]);
1136 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1137 resp_buftype, rsp_iov);
1142 SMB2_open_free(&rqst[0]);
1143 SMB2_set_info_free(&rqst[1]);
1144 SMB2_close_free(&rqst[2]);
1145 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1146 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1147 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1153 smb2_can_echo(struct TCP_Server_Info *server)
1155 return server->echoes;
1159 smb2_clear_stats(struct cifs_tcon *tcon)
1163 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1164 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1165 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1170 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1172 seq_puts(m, "\n\tShare Capabilities:");
1173 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1174 seq_puts(m, " DFS,");
1175 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1176 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1177 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1178 seq_puts(m, " SCALEOUT,");
1179 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1180 seq_puts(m, " CLUSTER,");
1181 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1182 seq_puts(m, " ASYMMETRIC,");
1183 if (tcon->capabilities == 0)
1184 seq_puts(m, " None");
1185 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1186 seq_puts(m, " Aligned,");
1187 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1188 seq_puts(m, " Partition Aligned,");
1189 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1190 seq_puts(m, " SSD,");
1191 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1192 seq_puts(m, " TRIM-support,");
1194 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1195 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1196 if (tcon->perf_sector_size)
1197 seq_printf(m, "\tOptimal sector size: 0x%x",
1198 tcon->perf_sector_size);
1199 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1203 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1205 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1206 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1209 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1210 * totals (requests sent) since those SMBs are per-session not per tcon
1212 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1213 (long long)(tcon->bytes_read),
1214 (long long)(tcon->bytes_written));
1215 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1216 atomic_read(&tcon->num_local_opens),
1217 atomic_read(&tcon->num_remote_opens));
1218 seq_printf(m, "\nTreeConnects: %d total %d failed",
1219 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1220 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1221 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1222 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1223 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1224 seq_printf(m, "\nCreates: %d total %d failed",
1225 atomic_read(&sent[SMB2_CREATE_HE]),
1226 atomic_read(&failed[SMB2_CREATE_HE]));
1227 seq_printf(m, "\nCloses: %d total %d failed",
1228 atomic_read(&sent[SMB2_CLOSE_HE]),
1229 atomic_read(&failed[SMB2_CLOSE_HE]));
1230 seq_printf(m, "\nFlushes: %d total %d failed",
1231 atomic_read(&sent[SMB2_FLUSH_HE]),
1232 atomic_read(&failed[SMB2_FLUSH_HE]));
1233 seq_printf(m, "\nReads: %d total %d failed",
1234 atomic_read(&sent[SMB2_READ_HE]),
1235 atomic_read(&failed[SMB2_READ_HE]));
1236 seq_printf(m, "\nWrites: %d total %d failed",
1237 atomic_read(&sent[SMB2_WRITE_HE]),
1238 atomic_read(&failed[SMB2_WRITE_HE]));
1239 seq_printf(m, "\nLocks: %d total %d failed",
1240 atomic_read(&sent[SMB2_LOCK_HE]),
1241 atomic_read(&failed[SMB2_LOCK_HE]));
1242 seq_printf(m, "\nIOCTLs: %d total %d failed",
1243 atomic_read(&sent[SMB2_IOCTL_HE]),
1244 atomic_read(&failed[SMB2_IOCTL_HE]));
1245 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1246 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1247 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1248 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1249 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1250 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1251 seq_printf(m, "\nQueryInfos: %d total %d failed",
1252 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1253 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1254 seq_printf(m, "\nSetInfos: %d total %d failed",
1255 atomic_read(&sent[SMB2_SET_INFO_HE]),
1256 atomic_read(&failed[SMB2_SET_INFO_HE]));
1257 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1258 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1259 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1263 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1265 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1266 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1268 cfile->fid.persistent_fid = fid->persistent_fid;
1269 cfile->fid.volatile_fid = fid->volatile_fid;
1270 #ifdef CONFIG_CIFS_DEBUG2
1271 cfile->fid.mid = fid->mid;
1272 #endif /* CIFS_DEBUG2 */
1273 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1275 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1276 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1280 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1281 struct cifs_fid *fid)
1283 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1287 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1288 u64 persistent_fid, u64 volatile_fid,
1289 struct copychunk_ioctl *pcchunk)
1292 unsigned int ret_data_len;
1293 struct resume_key_req *res_key;
1295 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1296 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1297 NULL, 0 /* no input */, CIFSMaxBufSize,
1298 (char **)&res_key, &ret_data_len);
1301 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1302 goto req_res_key_exit;
1304 if (ret_data_len < sizeof(struct resume_key_req)) {
1305 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
1307 goto req_res_key_exit;
1309 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1317 smb2_ioctl_query_info(const unsigned int xid,
1318 struct cifs_tcon *tcon,
1319 __le16 *path, int is_dir,
1322 struct cifs_ses *ses = tcon->ses;
1323 char __user *arg = (char __user *)p;
1324 struct smb_query_info qi;
1325 struct smb_query_info __user *pqi;
1328 struct smb2_query_info_rsp *qi_rsp = NULL;
1329 struct smb2_ioctl_rsp *io_rsp = NULL;
1330 void *buffer = NULL;
1331 struct smb_rqst rqst[3];
1332 int resp_buftype[3];
1333 struct kvec rsp_iov[3];
1334 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1335 struct cifs_open_parms oparms;
1336 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1337 struct cifs_fid fid;
1338 struct kvec qi_iov[1];
1339 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1340 struct kvec close_iov[1];
1342 memset(rqst, 0, sizeof(rqst));
1343 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1344 memset(rsp_iov, 0, sizeof(rsp_iov));
1346 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1349 if (qi.output_buffer_length > 1024)
1352 if (!ses || !(ses->server))
1355 if (smb3_encryption_required(tcon))
1356 flags |= CIFS_TRANSFORM_REQ;
1358 buffer = kmalloc(qi.output_buffer_length, GFP_KERNEL);
1362 if (copy_from_user(buffer, arg + sizeof(struct smb_query_info),
1363 qi.output_buffer_length)) {
1369 memset(&open_iov, 0, sizeof(open_iov));
1370 rqst[0].rq_iov = open_iov;
1371 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1373 memset(&oparms, 0, sizeof(oparms));
1375 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1376 oparms.disposition = FILE_OPEN;
1378 oparms.create_options = CREATE_NOT_FILE;
1380 oparms.create_options = CREATE_NOT_DIR;
1382 oparms.reconnect = false;
1385 * FSCTL codes encode the special access they need in the fsctl code.
1387 if (qi.flags & PASSTHRU_FSCTL) {
1388 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1389 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1390 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1392 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1393 oparms.desired_access = GENERIC_ALL;
1395 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1396 oparms.desired_access = GENERIC_READ;
1398 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1399 oparms.desired_access = GENERIC_WRITE;
1404 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, path);
1407 smb2_set_next_command(tcon, &rqst[0]);
1410 if (qi.flags & PASSTHRU_FSCTL) {
1411 /* Can eventually relax perm check since server enforces too */
1412 if (!capable(CAP_SYS_ADMIN))
1415 memset(&io_iov, 0, sizeof(io_iov));
1416 rqst[1].rq_iov = io_iov;
1417 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1419 rc = SMB2_ioctl_init(tcon, &rqst[1],
1420 COMPOUND_FID, COMPOUND_FID,
1421 qi.info_type, true, buffer,
1422 qi.output_buffer_length,
1425 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1426 memset(&qi_iov, 0, sizeof(qi_iov));
1427 rqst[1].rq_iov = qi_iov;
1428 rqst[1].rq_nvec = 1;
1430 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
1431 COMPOUND_FID, qi.file_info_class,
1432 qi.info_type, qi.additional_information,
1433 qi.input_buffer_length,
1434 qi.output_buffer_length, buffer);
1435 } else { /* unknown flags */
1436 cifs_dbg(VFS, "invalid passthru query flags: 0x%x\n", qi.flags);
1442 smb2_set_next_command(tcon, &rqst[1]);
1443 smb2_set_related(&rqst[1]);
1446 memset(&close_iov, 0, sizeof(close_iov));
1447 rqst[2].rq_iov = close_iov;
1448 rqst[2].rq_nvec = 1;
1450 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1453 smb2_set_related(&rqst[2]);
1455 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1456 resp_buftype, rsp_iov);
1459 if (qi.flags & PASSTHRU_FSCTL) {
1460 pqi = (struct smb_query_info __user *)arg;
1461 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1462 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1463 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1464 if (qi.input_buffer_length > 0 &&
1465 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length > rsp_iov[1].iov_len) {
1469 if (copy_to_user(&pqi->input_buffer_length, &qi.input_buffer_length,
1470 sizeof(qi.input_buffer_length))) {
1474 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1475 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1476 qi.input_buffer_length)) {
1481 pqi = (struct smb_query_info __user *)arg;
1482 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1483 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1484 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1485 if (copy_to_user(&pqi->input_buffer_length, &qi.input_buffer_length,
1486 sizeof(qi.input_buffer_length))) {
1490 if (copy_to_user(pqi + 1, qi_rsp->Buffer, qi.input_buffer_length)) {
1498 SMB2_open_free(&rqst[0]);
1499 if (qi.flags & PASSTHRU_FSCTL)
1500 SMB2_ioctl_free(&rqst[1]);
1502 SMB2_query_info_free(&rqst[1]);
1504 SMB2_close_free(&rqst[2]);
1505 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1506 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1507 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1512 smb2_copychunk_range(const unsigned int xid,
1513 struct cifsFileInfo *srcfile,
1514 struct cifsFileInfo *trgtfile, u64 src_off,
1515 u64 len, u64 dest_off)
1518 unsigned int ret_data_len;
1519 struct copychunk_ioctl *pcchunk;
1520 struct copychunk_ioctl_rsp *retbuf = NULL;
1521 struct cifs_tcon *tcon;
1522 int chunks_copied = 0;
1523 bool chunk_sizes_updated = false;
1524 ssize_t bytes_written, total_bytes_written = 0;
1526 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1528 if (pcchunk == NULL)
1531 cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
1532 /* Request a key from the server to identify the source of the copy */
1533 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1534 srcfile->fid.persistent_fid,
1535 srcfile->fid.volatile_fid, pcchunk);
1537 /* Note: request_res_key sets res_key null only if rc !=0 */
1541 /* For now array only one chunk long, will make more flexible later */
1542 pcchunk->ChunkCount = cpu_to_le32(1);
1543 pcchunk->Reserved = 0;
1544 pcchunk->Reserved2 = 0;
1546 tcon = tlink_tcon(trgtfile->tlink);
1549 pcchunk->SourceOffset = cpu_to_le64(src_off);
1550 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1552 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1554 /* Request server copy to target from src identified by key */
1555 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1556 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1557 true /* is_fsctl */, (char *)pcchunk,
1558 sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1559 (char **)&retbuf, &ret_data_len);
1562 sizeof(struct copychunk_ioctl_rsp)) {
1563 cifs_dbg(VFS, "invalid cchunk response size\n");
1567 if (retbuf->TotalBytesWritten == 0) {
1568 cifs_dbg(FYI, "no bytes copied\n");
1573 * Check if server claimed to write more than we asked
1575 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1576 le32_to_cpu(pcchunk->Length)) {
1577 cifs_dbg(VFS, "invalid copy chunk response\n");
1581 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1582 cifs_dbg(VFS, "invalid num chunks written\n");
1588 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1589 src_off += bytes_written;
1590 dest_off += bytes_written;
1591 len -= bytes_written;
1592 total_bytes_written += bytes_written;
1594 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1595 le32_to_cpu(retbuf->ChunksWritten),
1596 le32_to_cpu(retbuf->ChunkBytesWritten),
1598 } else if (rc == -EINVAL) {
1599 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1602 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1603 le32_to_cpu(retbuf->ChunksWritten),
1604 le32_to_cpu(retbuf->ChunkBytesWritten),
1605 le32_to_cpu(retbuf->TotalBytesWritten));
1608 * Check if this is the first request using these sizes,
1609 * (ie check if copy succeed once with original sizes
1610 * and check if the server gave us different sizes after
1611 * we already updated max sizes on previous request).
1612 * if not then why is the server returning an error now
1614 if ((chunks_copied != 0) || chunk_sizes_updated)
1617 /* Check that server is not asking us to grow size */
1618 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1619 tcon->max_bytes_chunk)
1620 tcon->max_bytes_chunk =
1621 le32_to_cpu(retbuf->ChunkBytesWritten);
1623 goto cchunk_out; /* server gave us bogus size */
1625 /* No need to change MaxChunks since already set to 1 */
1626 chunk_sizes_updated = true;
1637 return total_bytes_written;
1641 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1642 struct cifs_fid *fid)
1644 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1648 smb2_read_data_offset(char *buf)
1650 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1652 return rsp->DataOffset;
1656 smb2_read_data_length(char *buf, bool in_remaining)
1658 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1661 return le32_to_cpu(rsp->DataRemaining);
1663 return le32_to_cpu(rsp->DataLength);
1668 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1669 struct cifs_io_parms *parms, unsigned int *bytes_read,
1670 char **buf, int *buf_type)
1672 parms->persistent_fid = pfid->persistent_fid;
1673 parms->volatile_fid = pfid->volatile_fid;
1674 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1678 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1679 struct cifs_io_parms *parms, unsigned int *written,
1680 struct kvec *iov, unsigned long nr_segs)
1683 parms->persistent_fid = pfid->persistent_fid;
1684 parms->volatile_fid = pfid->volatile_fid;
1685 return SMB2_write(xid, parms, written, iov, nr_segs);
1688 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1689 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1690 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1692 struct cifsInodeInfo *cifsi;
1695 cifsi = CIFS_I(inode);
1697 /* if file already sparse don't bother setting sparse again */
1698 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1699 return true; /* already sparse */
1701 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1702 return true; /* already not sparse */
1705 * Can't check for sparse support on share the usual way via the
1706 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1707 * since Samba server doesn't set the flag on the share, yet
1708 * supports the set sparse FSCTL and returns sparse correctly
1709 * in the file attributes. If we fail setting sparse though we
1710 * mark that server does not support sparse files for this share
1711 * to avoid repeatedly sending the unsupported fsctl to server
1712 * if the file is repeatedly extended.
1714 if (tcon->broken_sparse_sup)
1717 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1718 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1720 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
1722 tcon->broken_sparse_sup = true;
1723 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1728 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1730 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1736 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1737 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1739 __le64 eof = cpu_to_le64(size);
1740 struct inode *inode;
1743 * If extending file more than one page make sparse. Many Linux fs
1744 * make files sparse by default when extending via ftruncate
1746 inode = d_inode(cfile->dentry);
1748 if (!set_alloc && (size > inode->i_size + 8192)) {
1749 __u8 set_sparse = 1;
1751 /* whether set sparse succeeds or not, extend the file */
1752 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1755 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1756 cfile->fid.volatile_fid, cfile->pid, &eof);
1760 smb2_duplicate_extents(const unsigned int xid,
1761 struct cifsFileInfo *srcfile,
1762 struct cifsFileInfo *trgtfile, u64 src_off,
1763 u64 len, u64 dest_off)
1766 unsigned int ret_data_len;
1767 struct duplicate_extents_to_file dup_ext_buf;
1768 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1770 /* server fileays advertise duplicate extent support with this flag */
1771 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1772 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1775 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1776 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1777 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1778 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1779 dup_ext_buf.ByteCount = cpu_to_le64(len);
1780 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
1781 src_off, dest_off, len);
1783 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1785 goto duplicate_extents_out;
1787 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1788 trgtfile->fid.volatile_fid,
1789 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
1790 true /* is_fsctl */,
1791 (char *)&dup_ext_buf,
1792 sizeof(struct duplicate_extents_to_file),
1793 CIFSMaxBufSize, NULL,
1796 if (ret_data_len > 0)
1797 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
1799 duplicate_extents_out:
1804 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1805 struct cifsFileInfo *cfile)
1807 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1808 cfile->fid.volatile_fid);
1812 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1813 struct cifsFileInfo *cfile)
1815 struct fsctl_set_integrity_information_req integr_info;
1816 unsigned int ret_data_len;
1818 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1819 integr_info.Flags = 0;
1820 integr_info.Reserved = 0;
1822 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1823 cfile->fid.volatile_fid,
1824 FSCTL_SET_INTEGRITY_INFORMATION,
1825 true /* is_fsctl */,
1826 (char *)&integr_info,
1827 sizeof(struct fsctl_set_integrity_information_req),
1828 CIFSMaxBufSize, NULL,
1833 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
1834 #define GMT_TOKEN_SIZE 50
1836 #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
1839 * Input buffer contains (empty) struct smb_snapshot array with size filled in
1840 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
1843 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1844 struct cifsFileInfo *cfile, void __user *ioc_buf)
1846 char *retbuf = NULL;
1847 unsigned int ret_data_len = 0;
1849 u32 max_response_size;
1850 struct smb_snapshot_array snapshot_in;
1853 * On the first query to enumerate the list of snapshots available
1854 * for this volume the buffer begins with 0 (number of snapshots
1855 * which can be returned is zero since at that point we do not know
1856 * how big the buffer needs to be). On the second query,
1857 * it (ret_data_len) is set to number of snapshots so we can
1858 * know to set the maximum response size larger (see below).
1860 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
1864 * Note that for snapshot queries that servers like Azure expect that
1865 * the first query be minimal size (and just used to get the number/size
1866 * of previous versions) so response size must be specified as EXACTLY
1867 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
1870 if (ret_data_len == 0)
1871 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
1873 max_response_size = CIFSMaxBufSize;
1875 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1876 cfile->fid.volatile_fid,
1877 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
1878 true /* is_fsctl */,
1879 NULL, 0 /* no input data */, max_response_size,
1882 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1887 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1889 if (copy_from_user(&snapshot_in, ioc_buf,
1890 sizeof(struct smb_snapshot_array))) {
1897 * Check for min size, ie not large enough to fit even one GMT
1898 * token (snapshot). On the first ioctl some users may pass in
1899 * smaller size (or zero) to simply get the size of the array
1900 * so the user space caller can allocate sufficient memory
1901 * and retry the ioctl again with larger array size sufficient
1902 * to hold all of the snapshot GMT tokens on the second try.
1904 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
1905 ret_data_len = sizeof(struct smb_snapshot_array);
1908 * We return struct SRV_SNAPSHOT_ARRAY, followed by
1909 * the snapshot array (of 50 byte GMT tokens) each
1910 * representing an available previous version of the data
1912 if (ret_data_len > (snapshot_in.snapshot_array_size +
1913 sizeof(struct smb_snapshot_array)))
1914 ret_data_len = snapshot_in.snapshot_array_size +
1915 sizeof(struct smb_snapshot_array);
1917 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1926 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1927 const char *path, struct cifs_sb_info *cifs_sb,
1928 struct cifs_fid *fid, __u16 search_flags,
1929 struct cifs_search_info *srch_inf)
1933 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1934 struct cifs_open_parms oparms;
1936 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1941 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1942 oparms.disposition = FILE_OPEN;
1943 if (backup_cred(cifs_sb))
1944 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1946 oparms.create_options = 0;
1948 oparms.reconnect = false;
1950 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
1953 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
1957 srch_inf->entries_in_buffer = 0;
1958 srch_inf->index_of_last_entry = 2;
1960 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1961 fid->volatile_fid, 0, srch_inf);
1963 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
1964 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1970 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1971 struct cifs_fid *fid, __u16 search_flags,
1972 struct cifs_search_info *srch_inf)
1974 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1975 fid->volatile_fid, 0, srch_inf);
1979 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1980 struct cifs_fid *fid)
1982 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1986 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
1987 * the number of credits and return true. Otherwise - return false.
1990 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
1992 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
1994 if (shdr->Status != STATUS_PENDING)
1997 if (shdr->CreditRequest) {
1998 spin_lock(&server->req_lock);
1999 server->credits += le16_to_cpu(shdr->CreditRequest);
2000 spin_unlock(&server->req_lock);
2001 wake_up(&server->request_q);
2008 smb2_is_session_expired(char *buf)
2010 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2012 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2013 shdr->Status != STATUS_USER_SESSION_DELETED)
2016 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2017 le16_to_cpu(shdr->Command),
2018 le64_to_cpu(shdr->MessageId));
2019 cifs_dbg(FYI, "Session expired or deleted\n");
2025 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2026 struct cifsInodeInfo *cinode)
2028 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2029 return SMB2_lease_break(0, tcon, cinode->lease_key,
2030 smb2_get_lease_state(cinode));
2032 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2034 CIFS_CACHE_READ(cinode) ? 1 : 0);
2038 smb2_set_related(struct smb_rqst *rqst)
2040 struct smb2_sync_hdr *shdr;
2042 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2043 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2046 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2049 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2051 struct smb2_sync_hdr *shdr;
2052 struct cifs_ses *ses = tcon->ses;
2053 struct TCP_Server_Info *server = ses->server;
2054 unsigned long len = smb_rqst_len(server, rqst);
2057 /* SMB headers in a compound are 8 byte aligned. */
2059 /* No padding needed */
2063 num_padding = 8 - (len & 7);
2064 if (!smb3_encryption_required(tcon)) {
2066 * If we do not have encryption then we can just add an extra
2067 * iov for the padding.
2069 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2070 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2075 * We can not add a small padding iov for the encryption case
2076 * because the encryption framework can not handle the padding
2078 * We have to flatten this into a single buffer and add
2079 * the padding to it.
2081 for (i = 1; i < rqst->rq_nvec; i++) {
2082 memcpy(rqst->rq_iov[0].iov_base +
2083 rqst->rq_iov[0].iov_len,
2084 rqst->rq_iov[i].iov_base,
2085 rqst->rq_iov[i].iov_len);
2086 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2088 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2090 rqst->rq_iov[0].iov_len += num_padding;
2096 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2097 shdr->NextCommand = cpu_to_le32(len);
2101 * Passes the query info response back to the caller on success.
2102 * Caller need to free this with free_rsp_buf().
2105 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2106 __le16 *utf16_path, u32 desired_access,
2107 u32 class, u32 type, u32 output_len,
2108 struct kvec *rsp, int *buftype,
2109 struct cifs_sb_info *cifs_sb)
2111 struct cifs_ses *ses = tcon->ses;
2113 struct smb_rqst rqst[3];
2114 int resp_buftype[3];
2115 struct kvec rsp_iov[3];
2116 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2117 struct kvec qi_iov[1];
2118 struct kvec close_iov[1];
2119 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2120 struct cifs_open_parms oparms;
2121 struct cifs_fid fid;
2124 if (smb3_encryption_required(tcon))
2125 flags |= CIFS_TRANSFORM_REQ;
2127 memset(rqst, 0, sizeof(rqst));
2128 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2129 memset(rsp_iov, 0, sizeof(rsp_iov));
2131 memset(&open_iov, 0, sizeof(open_iov));
2132 rqst[0].rq_iov = open_iov;
2133 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2136 oparms.desired_access = desired_access;
2137 oparms.disposition = FILE_OPEN;
2138 if (cifs_sb && backup_cred(cifs_sb))
2139 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2141 oparms.create_options = 0;
2143 oparms.reconnect = false;
2145 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2148 smb2_set_next_command(tcon, &rqst[0]);
2150 memset(&qi_iov, 0, sizeof(qi_iov));
2151 rqst[1].rq_iov = qi_iov;
2152 rqst[1].rq_nvec = 1;
2154 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
2160 smb2_set_next_command(tcon, &rqst[1]);
2161 smb2_set_related(&rqst[1]);
2163 memset(&close_iov, 0, sizeof(close_iov));
2164 rqst[2].rq_iov = close_iov;
2165 rqst[2].rq_nvec = 1;
2167 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
2170 smb2_set_related(&rqst[2]);
2172 rc = compound_send_recv(xid, ses, flags, 3, rqst,
2173 resp_buftype, rsp_iov);
2175 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2179 *buftype = resp_buftype[1];
2182 SMB2_open_free(&rqst[0]);
2183 SMB2_query_info_free(&rqst[1]);
2184 SMB2_close_free(&rqst[2]);
2185 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2186 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2191 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2192 struct kstatfs *buf)
2194 struct smb2_query_info_rsp *rsp;
2195 struct smb2_fs_full_size_info *info = NULL;
2196 __le16 utf16_path = 0; /* Null - open root of share */
2197 struct kvec rsp_iov = {NULL, 0};
2198 int buftype = CIFS_NO_BUFFER;
2202 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2203 FILE_READ_ATTRIBUTES,
2204 FS_FULL_SIZE_INFORMATION,
2205 SMB2_O_INFO_FILESYSTEM,
2206 sizeof(struct smb2_fs_full_size_info),
2207 &rsp_iov, &buftype, NULL);
2211 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2212 buf->f_type = SMB2_MAGIC_NUMBER;
2213 info = (struct smb2_fs_full_size_info *)(
2214 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2215 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2216 le32_to_cpu(rsp->OutputBufferLength),
2218 sizeof(struct smb2_fs_full_size_info));
2220 smb2_copy_fs_info_to_kstatfs(info, buf);
2223 free_rsp_buf(buftype, rsp_iov.iov_base);
2228 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2229 struct kstatfs *buf)
2232 __le16 srch_path = 0; /* Null - open root of share */
2233 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2234 struct cifs_open_parms oparms;
2235 struct cifs_fid fid;
2237 if (!tcon->posix_extensions)
2238 return smb2_queryfs(xid, tcon, buf);
2241 oparms.desired_access = FILE_READ_ATTRIBUTES;
2242 oparms.disposition = FILE_OPEN;
2243 oparms.create_options = 0;
2245 oparms.reconnect = false;
2247 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
2251 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2252 fid.volatile_fid, buf);
2253 buf->f_type = SMB2_MAGIC_NUMBER;
2254 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2259 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2261 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2262 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2266 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2267 __u64 length, __u32 type, int lock, int unlock, bool wait)
2269 if (unlock && !lock)
2270 type = SMB2_LOCKFLAG_UNLOCK;
2271 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2272 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2273 current->tgid, length, offset, type, wait);
2277 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2279 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2283 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2285 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2289 smb2_new_lease_key(struct cifs_fid *fid)
2291 generate_random_uuid(fid->lease_key);
2295 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2296 const char *search_name,
2297 struct dfs_info3_param **target_nodes,
2298 unsigned int *num_of_nodes,
2299 const struct nls_table *nls_codepage, int remap)
2302 __le16 *utf16_path = NULL;
2303 int utf16_path_len = 0;
2304 struct cifs_tcon *tcon;
2305 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2306 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2307 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2309 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
2312 * Try to use the IPC tcon, otherwise just use any
2314 tcon = ses->tcon_ipc;
2316 spin_lock(&cifs_tcp_ses_lock);
2317 tcon = list_first_entry_or_null(&ses->tcon_list,
2322 spin_unlock(&cifs_tcp_ses_lock);
2326 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2332 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2334 nls_codepage, remap);
2340 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2341 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2347 /* Highest DFS referral version understood */
2348 dfs_req->MaxReferralLevel = DFS_VERSION;
2350 /* Path to resolve in an UTF-16 null-terminated string */
2351 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2354 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2355 FSCTL_DFS_GET_REFERRALS,
2356 true /* is_fsctl */,
2357 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
2358 (char **)&dfs_rsp, &dfs_rsp_size);
2359 } while (rc == -EAGAIN);
2362 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
2363 cifs_dbg(VFS, "ioctl error in %s rc=%d\n", __func__, rc);
2367 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2368 num_of_nodes, target_nodes,
2369 nls_codepage, remap, search_name,
2370 true /* is_unicode */);
2372 cifs_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
2377 if (tcon && !tcon->ipc) {
2378 /* ipc tcons are not refcounted */
2379 spin_lock(&cifs_tcp_ses_lock);
2381 spin_unlock(&cifs_tcp_ses_lock);
2388 #define SMB2_SYMLINK_STRUCT_SIZE \
2389 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2392 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
2393 struct cifs_sb_info *cifs_sb, const char *full_path,
2394 char **target_path, bool is_reparse_point)
2397 __le16 *utf16_path = NULL;
2398 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2399 struct cifs_open_parms oparms;
2400 struct cifs_fid fid;
2401 struct kvec err_iov = {NULL, 0};
2402 struct smb2_err_rsp *err_buf = NULL;
2403 struct smb2_symlink_err_rsp *symlink;
2404 unsigned int sub_len;
2405 unsigned int sub_offset;
2406 unsigned int print_len;
2407 unsigned int print_offset;
2409 struct smb_rqst rqst[3];
2410 int resp_buftype[3];
2411 struct kvec rsp_iov[3];
2412 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2413 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2414 struct kvec close_iov[1];
2415 struct smb2_create_rsp *create_rsp;
2416 struct smb2_ioctl_rsp *ioctl_rsp;
2420 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2422 if (smb3_encryption_required(tcon))
2423 flags |= CIFS_TRANSFORM_REQ;
2425 memset(rqst, 0, sizeof(rqst));
2426 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2427 memset(rsp_iov, 0, sizeof(rsp_iov));
2429 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2434 memset(&open_iov, 0, sizeof(open_iov));
2435 rqst[0].rq_iov = open_iov;
2436 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2438 memset(&oparms, 0, sizeof(oparms));
2440 oparms.desired_access = FILE_READ_ATTRIBUTES;
2441 oparms.disposition = FILE_OPEN;
2443 if (backup_cred(cifs_sb))
2444 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2446 oparms.create_options = 0;
2447 if (is_reparse_point)
2448 oparms.create_options = OPEN_REPARSE_POINT;
2451 oparms.reconnect = false;
2453 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2456 smb2_set_next_command(tcon, &rqst[0]);
2460 memset(&io_iov, 0, sizeof(io_iov));
2461 rqst[1].rq_iov = io_iov;
2462 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
2464 rc = SMB2_ioctl_init(tcon, &rqst[1], fid.persistent_fid,
2465 fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
2466 true /* is_fctl */, NULL, 0, CIFSMaxBufSize);
2470 smb2_set_next_command(tcon, &rqst[1]);
2471 smb2_set_related(&rqst[1]);
2475 memset(&close_iov, 0, sizeof(close_iov));
2476 rqst[2].rq_iov = close_iov;
2477 rqst[2].rq_nvec = 1;
2479 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
2483 smb2_set_related(&rqst[2]);
2485 rc = compound_send_recv(xid, tcon->ses, flags, 3, rqst,
2486 resp_buftype, rsp_iov);
2488 create_rsp = rsp_iov[0].iov_base;
2489 if (create_rsp && create_rsp->sync_hdr.Status)
2490 err_iov = rsp_iov[0];
2491 ioctl_rsp = rsp_iov[1].iov_base;
2494 * Open was successful and we got an ioctl response.
2496 if ((rc == 0) && (is_reparse_point)) {
2497 /* See MS-FSCC 2.3.23 */
2499 ioctl_buf = (char *)ioctl_rsp + le32_to_cpu(ioctl_rsp->OutputOffset);
2500 plen = le32_to_cpu(ioctl_rsp->OutputCount);
2502 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
2503 rsp_iov[1].iov_len) {
2504 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", plen);
2509 /* Do stuff with ioctl_buf/plen */
2513 if (!rc || !err_iov.iov_base) {
2518 err_buf = err_iov.iov_base;
2519 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
2520 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
2525 /* open must fail on symlink - reset rc */
2527 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
2528 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
2529 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
2530 print_len = le16_to_cpu(symlink->PrintNameLength);
2531 print_offset = le16_to_cpu(symlink->PrintNameOffset);
2533 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
2538 if (err_iov.iov_len <
2539 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
2544 *target_path = cifs_strndup_from_utf16(
2545 (char *)symlink->PathBuffer + sub_offset,
2546 sub_len, true, cifs_sb->local_nls);
2547 if (!(*target_path)) {
2551 convert_delimiter(*target_path, '/');
2552 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2555 cifs_dbg(FYI, "query symlink rc %d\n", rc);
2557 SMB2_open_free(&rqst[0]);
2558 SMB2_ioctl_free(&rqst[1]);
2559 SMB2_close_free(&rqst[2]);
2560 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2561 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2562 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2566 #ifdef CONFIG_CIFS_ACL
2567 static struct cifs_ntsd *
2568 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
2569 const struct cifs_fid *cifsfid, u32 *pacllen)
2571 struct cifs_ntsd *pntsd = NULL;
2573 int rc = -EOPNOTSUPP;
2574 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2577 return ERR_CAST(tlink);
2580 cifs_dbg(FYI, "trying to get acl\n");
2582 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
2583 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
2586 cifs_put_tlink(tlink);
2588 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2595 static struct cifs_ntsd *
2596 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
2597 const char *path, u32 *pacllen)
2599 struct cifs_ntsd *pntsd = NULL;
2600 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2603 struct cifs_tcon *tcon;
2604 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2605 struct cifs_fid fid;
2606 struct cifs_open_parms oparms;
2609 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
2611 return ERR_CAST(tlink);
2613 tcon = tlink_tcon(tlink);
2616 if (backup_cred(cifs_sb))
2617 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2619 oparms.create_options = 0;
2621 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2629 oparms.desired_access = READ_CONTROL;
2630 oparms.disposition = FILE_OPEN;
2632 oparms.reconnect = false;
2634 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2637 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2638 fid.volatile_fid, (void **)&pntsd, pacllen);
2639 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2642 cifs_put_tlink(tlink);
2645 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2651 #ifdef CONFIG_CIFS_ACL
2653 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
2654 struct inode *inode, const char *path, int aclflag)
2656 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2658 int rc, access_flags = 0;
2659 struct cifs_tcon *tcon;
2660 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2661 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2662 struct cifs_fid fid;
2663 struct cifs_open_parms oparms;
2666 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
2668 return PTR_ERR(tlink);
2670 tcon = tlink_tcon(tlink);
2673 if (backup_cred(cifs_sb))
2674 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2676 oparms.create_options = 0;
2678 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
2679 access_flags = WRITE_OWNER;
2681 access_flags = WRITE_DAC;
2683 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2691 oparms.desired_access = access_flags;
2692 oparms.disposition = FILE_OPEN;
2695 oparms.reconnect = false;
2697 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2700 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2701 fid.volatile_fid, pnntsd, acllen, aclflag);
2702 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2705 cifs_put_tlink(tlink);
2709 #endif /* CIFS_ACL */
2711 /* Retrieve an ACL from the server */
2712 static struct cifs_ntsd *
2713 get_smb2_acl(struct cifs_sb_info *cifs_sb,
2714 struct inode *inode, const char *path,
2717 struct cifs_ntsd *pntsd = NULL;
2718 struct cifsFileInfo *open_file = NULL;
2721 open_file = find_readable_file(CIFS_I(inode), true);
2723 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
2725 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
2726 cifsFileInfo_put(open_file);
2731 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
2732 loff_t offset, loff_t len, bool keep_size)
2734 struct cifs_ses *ses = tcon->ses;
2735 struct inode *inode;
2736 struct cifsInodeInfo *cifsi;
2737 struct cifsFileInfo *cfile = file->private_data;
2738 struct file_zero_data_information fsctl_buf;
2745 inode = d_inode(cfile->dentry);
2746 cifsi = CIFS_I(inode);
2748 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
2749 ses->Suid, offset, len);
2752 /* if file not oplocked can't be sure whether asking to extend size */
2753 if (!CIFS_CACHE_READ(cifsi))
2754 if (keep_size == false) {
2756 trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
2757 tcon->tid, ses->Suid, offset, len, rc);
2762 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
2764 fsctl_buf.FileOffset = cpu_to_le64(offset);
2765 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2767 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2768 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
2770 sizeof(struct file_zero_data_information),
2773 goto zero_range_exit;
2776 * do we also need to change the size of the file?
2778 if (keep_size == false && i_size_read(inode) < offset + len) {
2779 eof = cpu_to_le64(offset + len);
2780 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
2781 cfile->fid.volatile_fid, cfile->pid, &eof);
2787 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
2788 ses->Suid, offset, len, rc);
2790 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
2791 ses->Suid, offset, len);
2795 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
2796 loff_t offset, loff_t len)
2798 struct inode *inode;
2799 struct cifsInodeInfo *cifsi;
2800 struct cifsFileInfo *cfile = file->private_data;
2801 struct file_zero_data_information fsctl_buf;
2804 __u8 set_sparse = 1;
2808 inode = d_inode(cfile->dentry);
2809 cifsi = CIFS_I(inode);
2811 /* Need to make file sparse, if not already, before freeing range. */
2812 /* Consider adding equivalent for compressed since it could also work */
2813 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
2819 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
2821 fsctl_buf.FileOffset = cpu_to_le64(offset);
2822 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2824 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2825 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
2826 true /* is_fctl */, (char *)&fsctl_buf,
2827 sizeof(struct file_zero_data_information),
2828 CIFSMaxBufSize, NULL, NULL);
2833 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
2834 loff_t off, loff_t len, bool keep_size)
2836 struct inode *inode;
2837 struct cifsInodeInfo *cifsi;
2838 struct cifsFileInfo *cfile = file->private_data;
2839 long rc = -EOPNOTSUPP;
2845 inode = d_inode(cfile->dentry);
2846 cifsi = CIFS_I(inode);
2848 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
2849 tcon->ses->Suid, off, len);
2850 /* if file not oplocked can't be sure whether asking to extend size */
2851 if (!CIFS_CACHE_READ(cifsi))
2852 if (keep_size == false) {
2853 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
2854 tcon->tid, tcon->ses->Suid, off, len, rc);
2860 * Files are non-sparse by default so falloc may be a no-op
2861 * Must check if file sparse. If not sparse, and not extending
2862 * then no need to do anything since file already allocated
2864 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
2865 if (keep_size == true)
2867 /* check if extending file */
2868 else if (i_size_read(inode) >= off + len)
2869 /* not extending file and already not sparse */
2871 /* BB: in future add else clause to extend file */
2875 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
2876 tcon->tid, tcon->ses->Suid, off, len, rc);
2878 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid,
2879 tcon->tid, tcon->ses->Suid, off, len);
2884 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
2886 * Check if falloc starts within first few pages of file
2887 * and ends within a few pages of the end of file to
2888 * ensure that most of file is being forced to be
2889 * fallocated now. If so then setting whole file sparse
2890 * ie potentially making a few extra pages at the beginning
2891 * or end of the file non-sparse via set_sparse is harmless.
2893 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
2895 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
2896 tcon->tid, tcon->ses->Suid, off, len, rc);
2901 smb2_set_sparse(xid, tcon, cfile, inode, false);
2904 smb2_set_sparse(xid, tcon, cfile, inode, false);
2906 if (i_size_read(inode) < off + len) {
2907 eof = cpu_to_le64(off + len);
2908 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
2909 cfile->fid.volatile_fid, cfile->pid,
2915 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
2916 tcon->ses->Suid, off, len, rc);
2918 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
2919 tcon->ses->Suid, off, len);
2925 static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
2927 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
2928 struct cifsInodeInfo *cifsi;
2929 struct inode *inode;
2931 struct file_allocated_range_buffer in_data, *out_data = NULL;
2935 if (whence != SEEK_HOLE && whence != SEEK_DATA)
2936 return generic_file_llseek(file, offset, whence);
2938 inode = d_inode(cfile->dentry);
2939 cifsi = CIFS_I(inode);
2941 if (offset < 0 || offset >= i_size_read(inode))
2946 * We need to be sure that all dirty pages are written as they
2947 * might fill holes on the server.
2948 * Note that we also MUST flush any written pages since at least
2949 * some servers (Windows2016) will not reflect recent writes in
2950 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
2952 wrcfile = find_writable_file(cifsi, false);
2954 filemap_write_and_wait(inode->i_mapping);
2955 smb2_flush_file(xid, tcon, &wrcfile->fid);
2956 cifsFileInfo_put(wrcfile);
2959 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
2960 if (whence == SEEK_HOLE)
2961 offset = i_size_read(inode);
2965 in_data.file_offset = cpu_to_le64(offset);
2966 in_data.length = cpu_to_le64(i_size_read(inode));
2968 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2969 cfile->fid.volatile_fid,
2970 FSCTL_QUERY_ALLOCATED_RANGES, true,
2971 (char *)&in_data, sizeof(in_data),
2972 sizeof(struct file_allocated_range_buffer),
2973 (char **)&out_data, &out_data_len);
2979 if (whence == SEEK_HOLE && out_data_len == 0)
2982 if (whence == SEEK_DATA && out_data_len == 0) {
2987 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
2991 if (whence == SEEK_DATA) {
2992 offset = le64_to_cpu(out_data->file_offset);
2995 if (offset < le64_to_cpu(out_data->file_offset))
2998 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3004 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3009 static int smb3_fiemap(struct cifs_tcon *tcon,
3010 struct cifsFileInfo *cfile,
3011 struct fiemap_extent_info *fei, u64 start, u64 len)
3014 struct file_allocated_range_buffer in_data, *out_data;
3016 int i, num, rc, flags, last_blob;
3019 if (fiemap_check_flags(fei, FIEMAP_FLAG_SYNC))
3024 in_data.file_offset = cpu_to_le64(start);
3025 in_data.length = cpu_to_le64(len);
3027 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3028 cfile->fid.volatile_fid,
3029 FSCTL_QUERY_ALLOCATED_RANGES, true,
3030 (char *)&in_data, sizeof(in_data),
3031 1024 * sizeof(struct file_allocated_range_buffer),
3032 (char **)&out_data, &out_data_len);
3041 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3045 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3050 num = out_data_len / sizeof(struct file_allocated_range_buffer);
3051 for (i = 0; i < num; i++) {
3053 if (i == num - 1 && last_blob)
3054 flags |= FIEMAP_EXTENT_LAST;
3056 rc = fiemap_fill_next_extent(fei,
3057 le64_to_cpu(out_data[i].file_offset),
3058 le64_to_cpu(out_data[i].file_offset),
3059 le64_to_cpu(out_data[i].length),
3070 next = le64_to_cpu(out_data[num - 1].file_offset) +
3071 le64_to_cpu(out_data[num - 1].length);
3072 len = len - (next - start);
3083 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3084 loff_t off, loff_t len)
3086 /* KEEP_SIZE already checked for by do_fallocate */
3087 if (mode & FALLOC_FL_PUNCH_HOLE)
3088 return smb3_punch_hole(file, tcon, off, len);
3089 else if (mode & FALLOC_FL_ZERO_RANGE) {
3090 if (mode & FALLOC_FL_KEEP_SIZE)
3091 return smb3_zero_range(file, tcon, off, len, true);
3092 return smb3_zero_range(file, tcon, off, len, false);
3093 } else if (mode == FALLOC_FL_KEEP_SIZE)
3094 return smb3_simple_falloc(file, tcon, off, len, true);
3096 return smb3_simple_falloc(file, tcon, off, len, false);
3102 smb2_downgrade_oplock(struct TCP_Server_Info *server,
3103 struct cifsInodeInfo *cinode, bool set_level2)
3106 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
3109 server->ops->set_oplock_level(cinode, 0, 0, NULL);
3113 smb21_downgrade_oplock(struct TCP_Server_Info *server,
3114 struct cifsInodeInfo *cinode, bool set_level2)
3116 server->ops->set_oplock_level(cinode,
3117 set_level2 ? SMB2_LEASE_READ_CACHING_HE :
3122 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3123 unsigned int epoch, bool *purge_cache)
3126 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3128 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3129 cinode->oplock = CIFS_CACHE_RHW_FLG;
3130 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3131 &cinode->vfs_inode);
3132 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
3133 cinode->oplock = CIFS_CACHE_RW_FLG;
3134 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3135 &cinode->vfs_inode);
3136 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3137 cinode->oplock = CIFS_CACHE_READ_FLG;
3138 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3139 &cinode->vfs_inode);
3145 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3146 unsigned int epoch, bool *purge_cache)
3148 char message[5] = {0};
3149 unsigned int new_oplock = 0;
3152 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3155 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
3156 new_oplock |= CIFS_CACHE_READ_FLG;
3157 strcat(message, "R");
3159 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
3160 new_oplock |= CIFS_CACHE_HANDLE_FLG;
3161 strcat(message, "H");
3163 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
3164 new_oplock |= CIFS_CACHE_WRITE_FLG;
3165 strcat(message, "W");
3168 strncpy(message, "None", sizeof(message));
3170 cinode->oplock = new_oplock;
3171 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3172 &cinode->vfs_inode);
3176 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3177 unsigned int epoch, bool *purge_cache)
3179 unsigned int old_oplock = cinode->oplock;
3181 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3184 *purge_cache = false;
3185 if (old_oplock == CIFS_CACHE_READ_FLG) {
3186 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3187 (epoch - cinode->epoch > 0))
3188 *purge_cache = true;
3189 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3190 (epoch - cinode->epoch > 1))
3191 *purge_cache = true;
3192 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3193 (epoch - cinode->epoch > 1))
3194 *purge_cache = true;
3195 else if (cinode->oplock == 0 &&
3196 (epoch - cinode->epoch > 0))
3197 *purge_cache = true;
3198 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
3199 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3200 (epoch - cinode->epoch > 0))
3201 *purge_cache = true;
3202 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3203 (epoch - cinode->epoch > 1))
3204 *purge_cache = true;
3206 cinode->epoch = epoch;
3211 smb2_is_read_op(__u32 oplock)
3213 return oplock == SMB2_OPLOCK_LEVEL_II;
3217 smb21_is_read_op(__u32 oplock)
3219 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
3220 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
3224 map_oplock_to_lease(u8 oplock)
3226 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3227 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
3228 else if (oplock == SMB2_OPLOCK_LEVEL_II)
3229 return SMB2_LEASE_READ_CACHING;
3230 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3231 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
3232 SMB2_LEASE_WRITE_CACHING;
3237 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3239 struct create_lease *buf;
3241 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
3245 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3246 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3248 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3249 (struct create_lease, lcontext));
3250 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
3251 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3252 (struct create_lease, Name));
3253 buf->ccontext.NameLength = cpu_to_le16(4);
3254 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3263 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
3265 struct create_lease_v2 *buf;
3267 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
3271 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3272 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3274 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3275 (struct create_lease_v2, lcontext));
3276 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
3277 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3278 (struct create_lease_v2, Name));
3279 buf->ccontext.NameLength = cpu_to_le16(4);
3280 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3289 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3291 struct create_lease *lc = (struct create_lease *)buf;
3293 *epoch = 0; /* not used */
3294 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3295 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3296 return le32_to_cpu(lc->lcontext.LeaseState);
3300 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3302 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
3304 *epoch = le16_to_cpu(lc->lcontext.Epoch);
3305 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3306 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3308 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
3309 return le32_to_cpu(lc->lcontext.LeaseState);
3313 smb2_wp_retry_size(struct inode *inode)
3315 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
3316 SMB2_MAX_BUFFER_SIZE);
3320 smb2_dir_needs_close(struct cifsFileInfo *cfile)
3322 return !cfile->invalidHandle;
3326 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
3327 struct smb_rqst *old_rq)
3329 struct smb2_sync_hdr *shdr =
3330 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
3332 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
3333 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
3334 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
3335 tr_hdr->Flags = cpu_to_le16(0x01);
3336 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
3337 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
3340 /* We can not use the normal sg_set_buf() as we will sometimes pass a
3341 * stack object as buf.
3343 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
3344 unsigned int buflen)
3346 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
3349 /* Assumes the first rqst has a transform header as the first iov.
3351 * rqst[0].rq_iov[0] is transform header
3352 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
3353 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
3355 static struct scatterlist *
3356 init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
3358 unsigned int sg_len;
3359 struct scatterlist *sg;
3362 unsigned int idx = 0;
3366 for (i = 0; i < num_rqst; i++)
3367 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
3369 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
3373 sg_init_table(sg, sg_len);
3374 for (i = 0; i < num_rqst; i++) {
3375 for (j = 0; j < rqst[i].rq_nvec; j++) {
3377 * The first rqst has a transform header where the
3378 * first 20 bytes are not part of the encrypted blob
3380 skip = (i == 0) && (j == 0) ? 20 : 0;
3381 smb2_sg_set_buf(&sg[idx++],
3382 rqst[i].rq_iov[j].iov_base + skip,
3383 rqst[i].rq_iov[j].iov_len - skip);
3386 for (j = 0; j < rqst[i].rq_npages; j++) {
3387 unsigned int len, offset;
3389 rqst_page_get_length(&rqst[i], j, &len, &offset);
3390 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
3393 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
3398 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
3400 struct cifs_ses *ses;
3403 spin_lock(&cifs_tcp_ses_lock);
3404 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3405 if (ses->Suid != ses_id)
3407 ses_enc_key = enc ? ses->smb3encryptionkey :
3408 ses->smb3decryptionkey;
3409 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
3410 spin_unlock(&cifs_tcp_ses_lock);
3413 spin_unlock(&cifs_tcp_ses_lock);
3418 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
3419 * iov[0] - transform header (associate data),
3420 * iov[1-N] - SMB2 header and pages - data to encrypt.
3421 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
3425 crypt_message(struct TCP_Server_Info *server, int num_rqst,
3426 struct smb_rqst *rqst, int enc)
3428 struct smb2_transform_hdr *tr_hdr =
3429 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
3430 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
3432 struct scatterlist *sg;
3433 u8 sign[SMB2_SIGNATURE_SIZE] = {};
3434 u8 key[SMB3_SIGN_KEY_SIZE];
3435 struct aead_request *req;
3437 unsigned int iv_len;
3438 DECLARE_CRYPTO_WAIT(wait);
3439 struct crypto_aead *tfm;
3440 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3442 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
3444 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
3449 rc = smb3_crypto_aead_allocate(server);
3451 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
3455 tfm = enc ? server->secmech.ccmaesencrypt :
3456 server->secmech.ccmaesdecrypt;
3457 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
3459 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
3463 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
3465 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
3469 req = aead_request_alloc(tfm, GFP_KERNEL);
3471 cifs_dbg(VFS, "%s: Failed to alloc aead request\n", __func__);
3476 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
3477 crypt_len += SMB2_SIGNATURE_SIZE;
3480 sg = init_sg(num_rqst, rqst, sign);
3482 cifs_dbg(VFS, "%s: Failed to init sg\n", __func__);
3487 iv_len = crypto_aead_ivsize(tfm);
3488 iv = kzalloc(iv_len, GFP_KERNEL);
3490 cifs_dbg(VFS, "%s: Failed to alloc iv\n", __func__);
3495 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
3497 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
3498 aead_request_set_ad(req, assoc_data_len);
3500 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3501 crypto_req_done, &wait);
3503 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
3504 : crypto_aead_decrypt(req), &wait);
3507 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
3518 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
3522 for (i = 0; i < num_rqst; i++) {
3523 if (rqst[i].rq_pages) {
3524 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
3525 put_page(rqst[i].rq_pages[j]);
3526 kfree(rqst[i].rq_pages);
3532 * This function will initialize new_rq and encrypt the content.
3533 * The first entry, new_rq[0], only contains a single iov which contains
3534 * a smb2_transform_hdr and is pre-allocated by the caller.
3535 * This function then populates new_rq[1+] with the content from olq_rq[0+].
3537 * The end result is an array of smb_rqst structures where the first structure
3538 * only contains a single iov for the transform header which we then can pass
3539 * to crypt_message().
3541 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
3542 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
3545 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
3546 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
3548 struct page **pages;
3549 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
3550 unsigned int npages;
3551 unsigned int orig_len = 0;
3555 for (i = 1; i < num_rqst; i++) {
3556 npages = old_rq[i - 1].rq_npages;
3557 pages = kmalloc_array(npages, sizeof(struct page *),
3562 new_rq[i].rq_pages = pages;
3563 new_rq[i].rq_npages = npages;
3564 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
3565 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
3566 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
3567 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
3568 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
3570 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
3572 for (j = 0; j < npages; j++) {
3573 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3578 /* copy pages form the old */
3579 for (j = 0; j < npages; j++) {
3581 unsigned int offset, len;
3583 rqst_page_get_length(&new_rq[i], j, &len, &offset);
3585 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
3586 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
3588 memcpy(dst, src, len);
3589 kunmap(new_rq[i].rq_pages[j]);
3590 kunmap(old_rq[i - 1].rq_pages[j]);
3594 /* fill the 1st iov with a transform header */
3595 fill_transform_hdr(tr_hdr, orig_len, old_rq);
3597 rc = crypt_message(server, num_rqst, new_rq, 1);
3598 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
3605 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
3610 smb3_is_transform_hdr(void *buf)
3612 struct smb2_transform_hdr *trhdr = buf;
3614 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
3618 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
3619 unsigned int buf_data_size, struct page **pages,
3620 unsigned int npages, unsigned int page_data_size)
3623 struct smb_rqst rqst = {NULL};
3626 iov[0].iov_base = buf;
3627 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
3628 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
3629 iov[1].iov_len = buf_data_size;
3633 rqst.rq_pages = pages;
3634 rqst.rq_npages = npages;
3635 rqst.rq_pagesz = PAGE_SIZE;
3636 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
3638 rc = crypt_message(server, 1, &rqst, 0);
3639 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
3644 memmove(buf, iov[1].iov_base, buf_data_size);
3646 server->total_read = buf_data_size + page_data_size;
3652 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
3653 unsigned int npages, unsigned int len)
3658 for (i = 0; i < npages; i++) {
3659 struct page *page = pages[i];
3663 if (len >= PAGE_SIZE) {
3664 /* enough data to fill the page */
3668 zero_user(page, len, PAGE_SIZE - len);
3671 length = cifs_read_page_from_socket(server, page, 0, n);
3674 server->total_read += length;
3681 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
3682 unsigned int cur_off, struct bio_vec **page_vec)
3684 struct bio_vec *bvec;
3687 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
3691 for (i = 0; i < npages; i++) {
3692 bvec[i].bv_page = pages[i];
3693 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
3694 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
3695 data_size -= bvec[i].bv_len;
3698 if (data_size != 0) {
3699 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
3709 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
3710 char *buf, unsigned int buf_len, struct page **pages,
3711 unsigned int npages, unsigned int page_data_size)
3713 unsigned int data_offset;
3714 unsigned int data_len;
3715 unsigned int cur_off;
3716 unsigned int cur_page_idx;
3717 unsigned int pad_len;
3718 struct cifs_readdata *rdata = mid->callback_data;
3719 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
3720 struct bio_vec *bvec = NULL;
3721 struct iov_iter iter;
3724 bool use_rdma_mr = false;
3726 if (shdr->Command != SMB2_READ) {
3727 cifs_dbg(VFS, "only big read responses are supported\n");
3731 if (server->ops->is_session_expired &&
3732 server->ops->is_session_expired(buf)) {
3733 cifs_reconnect(server);
3734 wake_up(&server->response_q);
3738 if (server->ops->is_status_pending &&
3739 server->ops->is_status_pending(buf, server))
3742 /* set up first two iov to get credits */
3743 rdata->iov[0].iov_base = buf;
3744 rdata->iov[0].iov_len = 0;
3745 rdata->iov[1].iov_base = buf;
3746 rdata->iov[1].iov_len =
3747 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
3748 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
3749 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
3750 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
3751 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
3753 rdata->result = server->ops->map_error(buf, true);
3754 if (rdata->result != 0) {
3755 cifs_dbg(FYI, "%s: server returned error %d\n",
3756 __func__, rdata->result);
3757 /* normal error on read response */
3758 dequeue_mid(mid, false);
3762 data_offset = server->ops->read_data_offset(buf);
3763 #ifdef CONFIG_CIFS_SMB_DIRECT
3764 use_rdma_mr = rdata->mr;
3766 data_len = server->ops->read_data_length(buf, use_rdma_mr);
3768 if (data_offset < server->vals->read_rsp_size) {
3770 * win2k8 sometimes sends an offset of 0 when the read
3771 * is beyond the EOF. Treat it as if the data starts just after
3774 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
3775 __func__, data_offset);
3776 data_offset = server->vals->read_rsp_size;
3777 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
3778 /* data_offset is beyond the end of smallbuf */
3779 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
3780 __func__, data_offset);
3781 rdata->result = -EIO;
3782 dequeue_mid(mid, rdata->result);
3786 pad_len = data_offset - server->vals->read_rsp_size;
3788 if (buf_len <= data_offset) {
3789 /* read response payload is in pages */
3790 cur_page_idx = pad_len / PAGE_SIZE;
3791 cur_off = pad_len % PAGE_SIZE;
3793 if (cur_page_idx != 0) {
3794 /* data offset is beyond the 1st page of response */
3795 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
3796 __func__, data_offset);
3797 rdata->result = -EIO;
3798 dequeue_mid(mid, rdata->result);
3802 if (data_len > page_data_size - pad_len) {
3803 /* data_len is corrupt -- discard frame */
3804 rdata->result = -EIO;
3805 dequeue_mid(mid, rdata->result);
3809 rdata->result = init_read_bvec(pages, npages, page_data_size,
3811 if (rdata->result != 0) {
3812 dequeue_mid(mid, rdata->result);
3816 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
3817 } else if (buf_len >= data_offset + data_len) {
3818 /* read response payload is in buf */
3819 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
3820 iov.iov_base = buf + data_offset;
3821 iov.iov_len = data_len;
3822 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
3824 /* read response payload cannot be in both buf and pages */
3825 WARN_ONCE(1, "buf can not contain only a part of read data");
3826 rdata->result = -EIO;
3827 dequeue_mid(mid, rdata->result);
3831 length = rdata->copy_into_pages(server, rdata, &iter);
3838 dequeue_mid(mid, false);
3843 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
3845 char *buf = server->smallbuf;
3846 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3847 unsigned int npages;
3848 struct page **pages;
3850 unsigned int buflen = server->pdu_size;
3854 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
3855 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
3857 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
3860 server->total_read += rc;
3862 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
3863 server->vals->read_rsp_size;
3864 npages = DIV_ROUND_UP(len, PAGE_SIZE);
3866 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
3872 for (; i < npages; i++) {
3873 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3880 /* read read data into pages */
3881 rc = read_data_into_pages(server, pages, npages, len);
3885 rc = cifs_discard_remaining_data(server);
3889 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
3890 pages, npages, len);
3894 *mid = smb2_find_mid(server, buf);
3896 cifs_dbg(FYI, "mid not found\n");
3898 cifs_dbg(FYI, "mid found\n");
3899 (*mid)->decrypted = true;
3900 rc = handle_read_data(server, *mid, buf,
3901 server->vals->read_rsp_size,
3902 pages, npages, len);
3906 for (i = i - 1; i >= 0; i--)
3911 cifs_discard_remaining_data(server);
3916 receive_encrypted_standard(struct TCP_Server_Info *server,
3917 struct mid_q_entry **mids, char **bufs,
3921 char *buf = server->smallbuf;
3923 struct smb2_sync_hdr *shdr;
3924 unsigned int pdu_length = server->pdu_size;
3925 unsigned int buf_size;
3926 struct mid_q_entry *mid_entry;
3928 char *next_buffer = NULL;
3932 /* switch to large buffer if too big for a small one */
3933 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
3934 server->large_buf = true;
3935 memcpy(server->bigbuf, buf, server->total_read);
3936 buf = server->bigbuf;
3939 /* now read the rest */
3940 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
3941 pdu_length - HEADER_SIZE(server) + 1);
3944 server->total_read += length;
3946 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
3947 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
3951 next_is_large = server->large_buf;
3953 shdr = (struct smb2_sync_hdr *)buf;
3954 if (shdr->NextCommand) {
3955 if (next_is_large) {
3956 tmpbuf = server->bigbuf;
3957 next_buffer = (char *)cifs_buf_get();
3959 tmpbuf = server->smallbuf;
3960 next_buffer = (char *)cifs_small_buf_get();
3963 tmpbuf + le32_to_cpu(shdr->NextCommand),
3964 pdu_length - le32_to_cpu(shdr->NextCommand));
3967 mid_entry = smb2_find_mid(server, buf);
3968 if (mid_entry == NULL)
3969 cifs_dbg(FYI, "mid not found\n");
3971 cifs_dbg(FYI, "mid found\n");
3972 mid_entry->decrypted = true;
3973 mid_entry->resp_buf_size = server->pdu_size;
3976 if (*num_mids >= MAX_COMPOUND) {
3977 cifs_dbg(VFS, "too many PDUs in compound\n");
3980 bufs[*num_mids] = buf;
3981 mids[(*num_mids)++] = mid_entry;
3983 if (mid_entry && mid_entry->handle)
3984 ret = mid_entry->handle(server, mid_entry);
3986 ret = cifs_handle_standard(server, mid_entry);
3988 if (ret == 0 && shdr->NextCommand) {
3989 pdu_length -= le32_to_cpu(shdr->NextCommand);
3990 server->large_buf = next_is_large;
3992 server->bigbuf = next_buffer;
3994 server->smallbuf = next_buffer;
3996 buf += le32_to_cpu(shdr->NextCommand);
4004 smb3_receive_transform(struct TCP_Server_Info *server,
4005 struct mid_q_entry **mids, char **bufs, int *num_mids)
4007 char *buf = server->smallbuf;
4008 unsigned int pdu_length = server->pdu_size;
4009 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4010 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4012 if (pdu_length < sizeof(struct smb2_transform_hdr) +
4013 sizeof(struct smb2_sync_hdr)) {
4014 cifs_dbg(VFS, "Transform message is too small (%u)\n",
4016 cifs_reconnect(server);
4017 wake_up(&server->response_q);
4018 return -ECONNABORTED;
4021 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
4022 cifs_dbg(VFS, "Transform message is broken\n");
4023 cifs_reconnect(server);
4024 wake_up(&server->response_q);
4025 return -ECONNABORTED;
4028 /* TODO: add support for compounds containing READ. */
4029 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
4031 return receive_encrypted_read(server, &mids[0]);
4034 return receive_encrypted_standard(server, mids, bufs, num_mids);
4038 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
4040 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
4042 return handle_read_data(server, mid, buf, server->pdu_size,
4047 smb2_next_header(char *buf)
4049 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
4050 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
4052 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
4053 return sizeof(struct smb2_transform_hdr) +
4054 le32_to_cpu(t_hdr->OriginalMessageSize);
4056 return le32_to_cpu(hdr->NextCommand);
4060 smb2_make_node(unsigned int xid, struct inode *inode,
4061 struct dentry *dentry, struct cifs_tcon *tcon,
4062 char *full_path, umode_t mode, dev_t dev)
4064 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4066 int create_options = CREATE_NOT_DIR | CREATE_OPTION_SPECIAL;
4067 FILE_ALL_INFO *buf = NULL;
4068 struct cifs_io_parms io_parms;
4070 struct cifs_fid fid;
4071 struct cifs_open_parms oparms;
4072 unsigned int bytes_written;
4073 struct win_dev *pdev;
4077 * Check if mounted with mount parm 'sfu' mount parm.
4078 * SFU emulation should work with all servers, but only
4079 * supports block and char device (no socket & fifo),
4080 * and was used by default in earlier versions of Windows
4082 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
4086 * TODO: Add ability to create instead via reparse point. Windows (e.g.
4087 * their current NFS server) uses this approach to expose special files
4088 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
4091 if (!S_ISCHR(mode) && !S_ISBLK(mode))
4094 cifs_dbg(FYI, "sfu compat create special file\n");
4096 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
4102 if (backup_cred(cifs_sb))
4103 create_options |= CREATE_OPEN_BACKUP_INTENT;
4106 oparms.cifs_sb = cifs_sb;
4107 oparms.desired_access = GENERIC_WRITE;
4108 oparms.create_options = create_options;
4109 oparms.disposition = FILE_CREATE;
4110 oparms.path = full_path;
4112 oparms.reconnect = false;
4114 if (tcon->ses->server->oplocks)
4115 oplock = REQ_OPLOCK;
4118 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
4123 * BB Do not bother to decode buf since no local inode yet to put
4124 * timestamps in, but we can reuse it safely.
4127 pdev = (struct win_dev *)buf;
4128 io_parms.pid = current->tgid;
4129 io_parms.tcon = tcon;
4130 io_parms.offset = 0;
4131 io_parms.length = sizeof(struct win_dev);
4132 iov[1].iov_base = buf;
4133 iov[1].iov_len = sizeof(struct win_dev);
4134 if (S_ISCHR(mode)) {
4135 memcpy(pdev->type, "IntxCHR", 8);
4136 pdev->major = cpu_to_le64(MAJOR(dev));
4137 pdev->minor = cpu_to_le64(MINOR(dev));
4138 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4139 &bytes_written, iov, 1);
4140 } else if (S_ISBLK(mode)) {
4141 memcpy(pdev->type, "IntxBLK", 8);
4142 pdev->major = cpu_to_le64(MAJOR(dev));
4143 pdev->minor = cpu_to_le64(MINOR(dev));
4144 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4145 &bytes_written, iov, 1);
4147 tcon->ses->server->ops->close(xid, tcon, &fid);
4150 /* FIXME: add code here to set EAs */
4157 struct smb_version_operations smb20_operations = {
4158 .compare_fids = smb2_compare_fids,
4159 .setup_request = smb2_setup_request,
4160 .setup_async_request = smb2_setup_async_request,
4161 .check_receive = smb2_check_receive,
4162 .add_credits = smb2_add_credits,
4163 .set_credits = smb2_set_credits,
4164 .get_credits_field = smb2_get_credits_field,
4165 .get_credits = smb2_get_credits,
4166 .wait_mtu_credits = cifs_wait_mtu_credits,
4167 .get_next_mid = smb2_get_next_mid,
4168 .revert_current_mid = smb2_revert_current_mid,
4169 .read_data_offset = smb2_read_data_offset,
4170 .read_data_length = smb2_read_data_length,
4171 .map_error = map_smb2_to_linux_error,
4172 .find_mid = smb2_find_mid,
4173 .check_message = smb2_check_message,
4174 .dump_detail = smb2_dump_detail,
4175 .clear_stats = smb2_clear_stats,
4176 .print_stats = smb2_print_stats,
4177 .is_oplock_break = smb2_is_valid_oplock_break,
4178 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4179 .downgrade_oplock = smb2_downgrade_oplock,
4180 .need_neg = smb2_need_neg,
4181 .negotiate = smb2_negotiate,
4182 .negotiate_wsize = smb2_negotiate_wsize,
4183 .negotiate_rsize = smb2_negotiate_rsize,
4184 .sess_setup = SMB2_sess_setup,
4185 .logoff = SMB2_logoff,
4186 .tree_connect = SMB2_tcon,
4187 .tree_disconnect = SMB2_tdis,
4188 .qfs_tcon = smb2_qfs_tcon,
4189 .is_path_accessible = smb2_is_path_accessible,
4190 .can_echo = smb2_can_echo,
4192 .query_path_info = smb2_query_path_info,
4193 .get_srv_inum = smb2_get_srv_inum,
4194 .query_file_info = smb2_query_file_info,
4195 .set_path_size = smb2_set_path_size,
4196 .set_file_size = smb2_set_file_size,
4197 .set_file_info = smb2_set_file_info,
4198 .set_compression = smb2_set_compression,
4199 .mkdir = smb2_mkdir,
4200 .mkdir_setinfo = smb2_mkdir_setinfo,
4201 .rmdir = smb2_rmdir,
4202 .unlink = smb2_unlink,
4203 .rename = smb2_rename_path,
4204 .create_hardlink = smb2_create_hardlink,
4205 .query_symlink = smb2_query_symlink,
4206 .query_mf_symlink = smb3_query_mf_symlink,
4207 .create_mf_symlink = smb3_create_mf_symlink,
4208 .open = smb2_open_file,
4209 .set_fid = smb2_set_fid,
4210 .close = smb2_close_file,
4211 .flush = smb2_flush_file,
4212 .async_readv = smb2_async_readv,
4213 .async_writev = smb2_async_writev,
4214 .sync_read = smb2_sync_read,
4215 .sync_write = smb2_sync_write,
4216 .query_dir_first = smb2_query_dir_first,
4217 .query_dir_next = smb2_query_dir_next,
4218 .close_dir = smb2_close_dir,
4219 .calc_smb_size = smb2_calc_size,
4220 .is_status_pending = smb2_is_status_pending,
4221 .is_session_expired = smb2_is_session_expired,
4222 .oplock_response = smb2_oplock_response,
4223 .queryfs = smb2_queryfs,
4224 .mand_lock = smb2_mand_lock,
4225 .mand_unlock_range = smb2_unlock_range,
4226 .push_mand_locks = smb2_push_mandatory_locks,
4227 .get_lease_key = smb2_get_lease_key,
4228 .set_lease_key = smb2_set_lease_key,
4229 .new_lease_key = smb2_new_lease_key,
4230 .calc_signature = smb2_calc_signature,
4231 .is_read_op = smb2_is_read_op,
4232 .set_oplock_level = smb2_set_oplock_level,
4233 .create_lease_buf = smb2_create_lease_buf,
4234 .parse_lease_buf = smb2_parse_lease_buf,
4235 .copychunk_range = smb2_copychunk_range,
4236 .wp_retry_size = smb2_wp_retry_size,
4237 .dir_needs_close = smb2_dir_needs_close,
4238 .get_dfs_refer = smb2_get_dfs_refer,
4239 .select_sectype = smb2_select_sectype,
4240 #ifdef CONFIG_CIFS_XATTR
4241 .query_all_EAs = smb2_query_eas,
4242 .set_EA = smb2_set_ea,
4243 #endif /* CIFS_XATTR */
4244 #ifdef CONFIG_CIFS_ACL
4245 .get_acl = get_smb2_acl,
4246 .get_acl_by_fid = get_smb2_acl_by_fid,
4247 .set_acl = set_smb2_acl,
4248 #endif /* CIFS_ACL */
4249 .next_header = smb2_next_header,
4250 .ioctl_query_info = smb2_ioctl_query_info,
4251 .make_node = smb2_make_node,
4252 .fiemap = smb3_fiemap,
4253 .llseek = smb3_llseek,
4256 struct smb_version_operations smb21_operations = {
4257 .compare_fids = smb2_compare_fids,
4258 .setup_request = smb2_setup_request,
4259 .setup_async_request = smb2_setup_async_request,
4260 .check_receive = smb2_check_receive,
4261 .add_credits = smb2_add_credits,
4262 .set_credits = smb2_set_credits,
4263 .get_credits_field = smb2_get_credits_field,
4264 .get_credits = smb2_get_credits,
4265 .wait_mtu_credits = smb2_wait_mtu_credits,
4266 .adjust_credits = smb2_adjust_credits,
4267 .get_next_mid = smb2_get_next_mid,
4268 .revert_current_mid = smb2_revert_current_mid,
4269 .read_data_offset = smb2_read_data_offset,
4270 .read_data_length = smb2_read_data_length,
4271 .map_error = map_smb2_to_linux_error,
4272 .find_mid = smb2_find_mid,
4273 .check_message = smb2_check_message,
4274 .dump_detail = smb2_dump_detail,
4275 .clear_stats = smb2_clear_stats,
4276 .print_stats = smb2_print_stats,
4277 .is_oplock_break = smb2_is_valid_oplock_break,
4278 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4279 .downgrade_oplock = smb21_downgrade_oplock,
4280 .need_neg = smb2_need_neg,
4281 .negotiate = smb2_negotiate,
4282 .negotiate_wsize = smb2_negotiate_wsize,
4283 .negotiate_rsize = smb2_negotiate_rsize,
4284 .sess_setup = SMB2_sess_setup,
4285 .logoff = SMB2_logoff,
4286 .tree_connect = SMB2_tcon,
4287 .tree_disconnect = SMB2_tdis,
4288 .qfs_tcon = smb2_qfs_tcon,
4289 .is_path_accessible = smb2_is_path_accessible,
4290 .can_echo = smb2_can_echo,
4292 .query_path_info = smb2_query_path_info,
4293 .get_srv_inum = smb2_get_srv_inum,
4294 .query_file_info = smb2_query_file_info,
4295 .set_path_size = smb2_set_path_size,
4296 .set_file_size = smb2_set_file_size,
4297 .set_file_info = smb2_set_file_info,
4298 .set_compression = smb2_set_compression,
4299 .mkdir = smb2_mkdir,
4300 .mkdir_setinfo = smb2_mkdir_setinfo,
4301 .rmdir = smb2_rmdir,
4302 .unlink = smb2_unlink,
4303 .rename = smb2_rename_path,
4304 .create_hardlink = smb2_create_hardlink,
4305 .query_symlink = smb2_query_symlink,
4306 .query_mf_symlink = smb3_query_mf_symlink,
4307 .create_mf_symlink = smb3_create_mf_symlink,
4308 .open = smb2_open_file,
4309 .set_fid = smb2_set_fid,
4310 .close = smb2_close_file,
4311 .flush = smb2_flush_file,
4312 .async_readv = smb2_async_readv,
4313 .async_writev = smb2_async_writev,
4314 .sync_read = smb2_sync_read,
4315 .sync_write = smb2_sync_write,
4316 .query_dir_first = smb2_query_dir_first,
4317 .query_dir_next = smb2_query_dir_next,
4318 .close_dir = smb2_close_dir,
4319 .calc_smb_size = smb2_calc_size,
4320 .is_status_pending = smb2_is_status_pending,
4321 .is_session_expired = smb2_is_session_expired,
4322 .oplock_response = smb2_oplock_response,
4323 .queryfs = smb2_queryfs,
4324 .mand_lock = smb2_mand_lock,
4325 .mand_unlock_range = smb2_unlock_range,
4326 .push_mand_locks = smb2_push_mandatory_locks,
4327 .get_lease_key = smb2_get_lease_key,
4328 .set_lease_key = smb2_set_lease_key,
4329 .new_lease_key = smb2_new_lease_key,
4330 .calc_signature = smb2_calc_signature,
4331 .is_read_op = smb21_is_read_op,
4332 .set_oplock_level = smb21_set_oplock_level,
4333 .create_lease_buf = smb2_create_lease_buf,
4334 .parse_lease_buf = smb2_parse_lease_buf,
4335 .copychunk_range = smb2_copychunk_range,
4336 .wp_retry_size = smb2_wp_retry_size,
4337 .dir_needs_close = smb2_dir_needs_close,
4338 .enum_snapshots = smb3_enum_snapshots,
4339 .get_dfs_refer = smb2_get_dfs_refer,
4340 .select_sectype = smb2_select_sectype,
4341 #ifdef CONFIG_CIFS_XATTR
4342 .query_all_EAs = smb2_query_eas,
4343 .set_EA = smb2_set_ea,
4344 #endif /* CIFS_XATTR */
4345 #ifdef CONFIG_CIFS_ACL
4346 .get_acl = get_smb2_acl,
4347 .get_acl_by_fid = get_smb2_acl_by_fid,
4348 .set_acl = set_smb2_acl,
4349 #endif /* CIFS_ACL */
4350 .next_header = smb2_next_header,
4351 .ioctl_query_info = smb2_ioctl_query_info,
4352 .make_node = smb2_make_node,
4353 .fiemap = smb3_fiemap,
4354 .llseek = smb3_llseek,
4357 struct smb_version_operations smb30_operations = {
4358 .compare_fids = smb2_compare_fids,
4359 .setup_request = smb2_setup_request,
4360 .setup_async_request = smb2_setup_async_request,
4361 .check_receive = smb2_check_receive,
4362 .add_credits = smb2_add_credits,
4363 .set_credits = smb2_set_credits,
4364 .get_credits_field = smb2_get_credits_field,
4365 .get_credits = smb2_get_credits,
4366 .wait_mtu_credits = smb2_wait_mtu_credits,
4367 .adjust_credits = smb2_adjust_credits,
4368 .get_next_mid = smb2_get_next_mid,
4369 .revert_current_mid = smb2_revert_current_mid,
4370 .read_data_offset = smb2_read_data_offset,
4371 .read_data_length = smb2_read_data_length,
4372 .map_error = map_smb2_to_linux_error,
4373 .find_mid = smb2_find_mid,
4374 .check_message = smb2_check_message,
4375 .dump_detail = smb2_dump_detail,
4376 .clear_stats = smb2_clear_stats,
4377 .print_stats = smb2_print_stats,
4378 .dump_share_caps = smb2_dump_share_caps,
4379 .is_oplock_break = smb2_is_valid_oplock_break,
4380 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4381 .downgrade_oplock = smb21_downgrade_oplock,
4382 .need_neg = smb2_need_neg,
4383 .negotiate = smb2_negotiate,
4384 .negotiate_wsize = smb3_negotiate_wsize,
4385 .negotiate_rsize = smb3_negotiate_rsize,
4386 .sess_setup = SMB2_sess_setup,
4387 .logoff = SMB2_logoff,
4388 .tree_connect = SMB2_tcon,
4389 .tree_disconnect = SMB2_tdis,
4390 .qfs_tcon = smb3_qfs_tcon,
4391 .is_path_accessible = smb2_is_path_accessible,
4392 .can_echo = smb2_can_echo,
4394 .query_path_info = smb2_query_path_info,
4395 .get_srv_inum = smb2_get_srv_inum,
4396 .query_file_info = smb2_query_file_info,
4397 .set_path_size = smb2_set_path_size,
4398 .set_file_size = smb2_set_file_size,
4399 .set_file_info = smb2_set_file_info,
4400 .set_compression = smb2_set_compression,
4401 .mkdir = smb2_mkdir,
4402 .mkdir_setinfo = smb2_mkdir_setinfo,
4403 .rmdir = smb2_rmdir,
4404 .unlink = smb2_unlink,
4405 .rename = smb2_rename_path,
4406 .create_hardlink = smb2_create_hardlink,
4407 .query_symlink = smb2_query_symlink,
4408 .query_mf_symlink = smb3_query_mf_symlink,
4409 .create_mf_symlink = smb3_create_mf_symlink,
4410 .open = smb2_open_file,
4411 .set_fid = smb2_set_fid,
4412 .close = smb2_close_file,
4413 .flush = smb2_flush_file,
4414 .async_readv = smb2_async_readv,
4415 .async_writev = smb2_async_writev,
4416 .sync_read = smb2_sync_read,
4417 .sync_write = smb2_sync_write,
4418 .query_dir_first = smb2_query_dir_first,
4419 .query_dir_next = smb2_query_dir_next,
4420 .close_dir = smb2_close_dir,
4421 .calc_smb_size = smb2_calc_size,
4422 .is_status_pending = smb2_is_status_pending,
4423 .is_session_expired = smb2_is_session_expired,
4424 .oplock_response = smb2_oplock_response,
4425 .queryfs = smb2_queryfs,
4426 .mand_lock = smb2_mand_lock,
4427 .mand_unlock_range = smb2_unlock_range,
4428 .push_mand_locks = smb2_push_mandatory_locks,
4429 .get_lease_key = smb2_get_lease_key,
4430 .set_lease_key = smb2_set_lease_key,
4431 .new_lease_key = smb2_new_lease_key,
4432 .generate_signingkey = generate_smb30signingkey,
4433 .calc_signature = smb3_calc_signature,
4434 .set_integrity = smb3_set_integrity,
4435 .is_read_op = smb21_is_read_op,
4436 .set_oplock_level = smb3_set_oplock_level,
4437 .create_lease_buf = smb3_create_lease_buf,
4438 .parse_lease_buf = smb3_parse_lease_buf,
4439 .copychunk_range = smb2_copychunk_range,
4440 .duplicate_extents = smb2_duplicate_extents,
4441 .validate_negotiate = smb3_validate_negotiate,
4442 .wp_retry_size = smb2_wp_retry_size,
4443 .dir_needs_close = smb2_dir_needs_close,
4444 .fallocate = smb3_fallocate,
4445 .enum_snapshots = smb3_enum_snapshots,
4446 .init_transform_rq = smb3_init_transform_rq,
4447 .is_transform_hdr = smb3_is_transform_hdr,
4448 .receive_transform = smb3_receive_transform,
4449 .get_dfs_refer = smb2_get_dfs_refer,
4450 .select_sectype = smb2_select_sectype,
4451 #ifdef CONFIG_CIFS_XATTR
4452 .query_all_EAs = smb2_query_eas,
4453 .set_EA = smb2_set_ea,
4454 #endif /* CIFS_XATTR */
4455 #ifdef CONFIG_CIFS_ACL
4456 .get_acl = get_smb2_acl,
4457 .get_acl_by_fid = get_smb2_acl_by_fid,
4458 .set_acl = set_smb2_acl,
4459 #endif /* CIFS_ACL */
4460 .next_header = smb2_next_header,
4461 .ioctl_query_info = smb2_ioctl_query_info,
4462 .make_node = smb2_make_node,
4463 .fiemap = smb3_fiemap,
4464 .llseek = smb3_llseek,
4467 struct smb_version_operations smb311_operations = {
4468 .compare_fids = smb2_compare_fids,
4469 .setup_request = smb2_setup_request,
4470 .setup_async_request = smb2_setup_async_request,
4471 .check_receive = smb2_check_receive,
4472 .add_credits = smb2_add_credits,
4473 .set_credits = smb2_set_credits,
4474 .get_credits_field = smb2_get_credits_field,
4475 .get_credits = smb2_get_credits,
4476 .wait_mtu_credits = smb2_wait_mtu_credits,
4477 .adjust_credits = smb2_adjust_credits,
4478 .get_next_mid = smb2_get_next_mid,
4479 .revert_current_mid = smb2_revert_current_mid,
4480 .read_data_offset = smb2_read_data_offset,
4481 .read_data_length = smb2_read_data_length,
4482 .map_error = map_smb2_to_linux_error,
4483 .find_mid = smb2_find_mid,
4484 .check_message = smb2_check_message,
4485 .dump_detail = smb2_dump_detail,
4486 .clear_stats = smb2_clear_stats,
4487 .print_stats = smb2_print_stats,
4488 .dump_share_caps = smb2_dump_share_caps,
4489 .is_oplock_break = smb2_is_valid_oplock_break,
4490 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4491 .downgrade_oplock = smb21_downgrade_oplock,
4492 .need_neg = smb2_need_neg,
4493 .negotiate = smb2_negotiate,
4494 .negotiate_wsize = smb3_negotiate_wsize,
4495 .negotiate_rsize = smb3_negotiate_rsize,
4496 .sess_setup = SMB2_sess_setup,
4497 .logoff = SMB2_logoff,
4498 .tree_connect = SMB2_tcon,
4499 .tree_disconnect = SMB2_tdis,
4500 .qfs_tcon = smb3_qfs_tcon,
4501 .is_path_accessible = smb2_is_path_accessible,
4502 .can_echo = smb2_can_echo,
4504 .query_path_info = smb2_query_path_info,
4505 .get_srv_inum = smb2_get_srv_inum,
4506 .query_file_info = smb2_query_file_info,
4507 .set_path_size = smb2_set_path_size,
4508 .set_file_size = smb2_set_file_size,
4509 .set_file_info = smb2_set_file_info,
4510 .set_compression = smb2_set_compression,
4511 .mkdir = smb2_mkdir,
4512 .mkdir_setinfo = smb2_mkdir_setinfo,
4513 .posix_mkdir = smb311_posix_mkdir,
4514 .rmdir = smb2_rmdir,
4515 .unlink = smb2_unlink,
4516 .rename = smb2_rename_path,
4517 .create_hardlink = smb2_create_hardlink,
4518 .query_symlink = smb2_query_symlink,
4519 .query_mf_symlink = smb3_query_mf_symlink,
4520 .create_mf_symlink = smb3_create_mf_symlink,
4521 .open = smb2_open_file,
4522 .set_fid = smb2_set_fid,
4523 .close = smb2_close_file,
4524 .flush = smb2_flush_file,
4525 .async_readv = smb2_async_readv,
4526 .async_writev = smb2_async_writev,
4527 .sync_read = smb2_sync_read,
4528 .sync_write = smb2_sync_write,
4529 .query_dir_first = smb2_query_dir_first,
4530 .query_dir_next = smb2_query_dir_next,
4531 .close_dir = smb2_close_dir,
4532 .calc_smb_size = smb2_calc_size,
4533 .is_status_pending = smb2_is_status_pending,
4534 .is_session_expired = smb2_is_session_expired,
4535 .oplock_response = smb2_oplock_response,
4536 .queryfs = smb311_queryfs,
4537 .mand_lock = smb2_mand_lock,
4538 .mand_unlock_range = smb2_unlock_range,
4539 .push_mand_locks = smb2_push_mandatory_locks,
4540 .get_lease_key = smb2_get_lease_key,
4541 .set_lease_key = smb2_set_lease_key,
4542 .new_lease_key = smb2_new_lease_key,
4543 .generate_signingkey = generate_smb311signingkey,
4544 .calc_signature = smb3_calc_signature,
4545 .set_integrity = smb3_set_integrity,
4546 .is_read_op = smb21_is_read_op,
4547 .set_oplock_level = smb3_set_oplock_level,
4548 .create_lease_buf = smb3_create_lease_buf,
4549 .parse_lease_buf = smb3_parse_lease_buf,
4550 .copychunk_range = smb2_copychunk_range,
4551 .duplicate_extents = smb2_duplicate_extents,
4552 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
4553 .wp_retry_size = smb2_wp_retry_size,
4554 .dir_needs_close = smb2_dir_needs_close,
4555 .fallocate = smb3_fallocate,
4556 .enum_snapshots = smb3_enum_snapshots,
4557 .init_transform_rq = smb3_init_transform_rq,
4558 .is_transform_hdr = smb3_is_transform_hdr,
4559 .receive_transform = smb3_receive_transform,
4560 .get_dfs_refer = smb2_get_dfs_refer,
4561 .select_sectype = smb2_select_sectype,
4562 #ifdef CONFIG_CIFS_XATTR
4563 .query_all_EAs = smb2_query_eas,
4564 .set_EA = smb2_set_ea,
4565 #endif /* CIFS_XATTR */
4566 #ifdef CONFIG_CIFS_ACL
4567 .get_acl = get_smb2_acl,
4568 .get_acl_by_fid = get_smb2_acl_by_fid,
4569 .set_acl = set_smb2_acl,
4570 #endif /* CIFS_ACL */
4571 .next_header = smb2_next_header,
4572 .ioctl_query_info = smb2_ioctl_query_info,
4573 .make_node = smb2_make_node,
4574 .fiemap = smb3_fiemap,
4575 .llseek = smb3_llseek,
4578 struct smb_version_values smb20_values = {
4579 .version_string = SMB20_VERSION_STRING,
4580 .protocol_id = SMB20_PROT_ID,
4581 .req_capabilities = 0, /* MBZ */
4582 .large_lock_type = 0,
4583 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4584 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4585 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4586 .header_size = sizeof(struct smb2_sync_hdr),
4587 .header_preamble_size = 0,
4588 .max_header_size = MAX_SMB2_HDR_SIZE,
4589 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4590 .lock_cmd = SMB2_LOCK,
4592 .cap_nt_find = SMB2_NT_FIND,
4593 .cap_large_files = SMB2_LARGE_FILES,
4594 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4595 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4596 .create_lease_size = sizeof(struct create_lease),
4599 struct smb_version_values smb21_values = {
4600 .version_string = SMB21_VERSION_STRING,
4601 .protocol_id = SMB21_PROT_ID,
4602 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
4603 .large_lock_type = 0,
4604 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4605 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4606 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4607 .header_size = sizeof(struct smb2_sync_hdr),
4608 .header_preamble_size = 0,
4609 .max_header_size = MAX_SMB2_HDR_SIZE,
4610 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4611 .lock_cmd = SMB2_LOCK,
4613 .cap_nt_find = SMB2_NT_FIND,
4614 .cap_large_files = SMB2_LARGE_FILES,
4615 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4616 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4617 .create_lease_size = sizeof(struct create_lease),
4620 struct smb_version_values smb3any_values = {
4621 .version_string = SMB3ANY_VERSION_STRING,
4622 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
4623 .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,
4624 .large_lock_type = 0,
4625 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4626 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4627 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4628 .header_size = sizeof(struct smb2_sync_hdr),
4629 .header_preamble_size = 0,
4630 .max_header_size = MAX_SMB2_HDR_SIZE,
4631 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4632 .lock_cmd = SMB2_LOCK,
4634 .cap_nt_find = SMB2_NT_FIND,
4635 .cap_large_files = SMB2_LARGE_FILES,
4636 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4637 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4638 .create_lease_size = sizeof(struct create_lease_v2),
4641 struct smb_version_values smbdefault_values = {
4642 .version_string = SMBDEFAULT_VERSION_STRING,
4643 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
4644 .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,
4645 .large_lock_type = 0,
4646 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4647 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4648 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4649 .header_size = sizeof(struct smb2_sync_hdr),
4650 .header_preamble_size = 0,
4651 .max_header_size = MAX_SMB2_HDR_SIZE,
4652 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4653 .lock_cmd = SMB2_LOCK,
4655 .cap_nt_find = SMB2_NT_FIND,
4656 .cap_large_files = SMB2_LARGE_FILES,
4657 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4658 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4659 .create_lease_size = sizeof(struct create_lease_v2),
4662 struct smb_version_values smb30_values = {
4663 .version_string = SMB30_VERSION_STRING,
4664 .protocol_id = SMB30_PROT_ID,
4665 .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,
4666 .large_lock_type = 0,
4667 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4668 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4669 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4670 .header_size = sizeof(struct smb2_sync_hdr),
4671 .header_preamble_size = 0,
4672 .max_header_size = MAX_SMB2_HDR_SIZE,
4673 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4674 .lock_cmd = SMB2_LOCK,
4676 .cap_nt_find = SMB2_NT_FIND,
4677 .cap_large_files = SMB2_LARGE_FILES,
4678 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4679 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4680 .create_lease_size = sizeof(struct create_lease_v2),
4683 struct smb_version_values smb302_values = {
4684 .version_string = SMB302_VERSION_STRING,
4685 .protocol_id = SMB302_PROT_ID,
4686 .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,
4687 .large_lock_type = 0,
4688 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4689 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4690 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4691 .header_size = sizeof(struct smb2_sync_hdr),
4692 .header_preamble_size = 0,
4693 .max_header_size = MAX_SMB2_HDR_SIZE,
4694 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4695 .lock_cmd = SMB2_LOCK,
4697 .cap_nt_find = SMB2_NT_FIND,
4698 .cap_large_files = SMB2_LARGE_FILES,
4699 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4700 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4701 .create_lease_size = sizeof(struct create_lease_v2),
4704 struct smb_version_values smb311_values = {
4705 .version_string = SMB311_VERSION_STRING,
4706 .protocol_id = SMB311_PROT_ID,
4707 .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,
4708 .large_lock_type = 0,
4709 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4710 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4711 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4712 .header_size = sizeof(struct smb2_sync_hdr),
4713 .header_preamble_size = 0,
4714 .max_header_size = MAX_SMB2_HDR_SIZE,
4715 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4716 .lock_cmd = SMB2_LOCK,
4718 .cap_nt_find = SMB2_NT_FIND,
4719 .cap_large_files = SMB2_LARGE_FILES,
4720 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4721 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4722 .create_lease_size = sizeof(struct create_lease_v2),