2 * SMB2 version specific operations
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License v2 as published
8 * by the Free Software Foundation.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/pagemap.h>
21 #include <linux/vfs.h>
22 #include <linux/falloc.h>
23 #include <linux/scatterlist.h>
24 #include <linux/uuid.h>
25 #include <crypto/aead.h>
28 #include "smb2proto.h"
29 #include "cifsproto.h"
30 #include "cifs_debug.h"
31 #include "cifs_unicode.h"
32 #include "smb2status.h"
34 #include "cifs_ioctl.h"
35 #include "smbdirect.h"
38 change_conf(struct TCP_Server_Info *server)
40 server->credits += server->echo_credits + server->oplock_credits;
41 server->oplock_credits = server->echo_credits = 0;
42 switch (server->credits) {
46 server->echoes = false;
47 server->oplocks = false;
48 cifs_dbg(VFS, "disabling echoes and oplocks\n");
51 server->echoes = true;
52 server->oplocks = false;
53 server->echo_credits = 1;
54 cifs_dbg(FYI, "disabling oplocks\n");
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;
71 smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
75 spin_lock(&server->req_lock);
76 val = server->ops->get_credits_field(server, optype);
78 /* eg found case where write overlapping reconnect messed up credits */
79 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
80 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
81 server->hostname, *val);
85 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
86 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
89 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
90 rc = change_conf(server);
92 * Sometimes server returns 0 credits on oplock break ack - we need to
93 * rebalance credits in this case.
95 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
97 if (server->credits > 1) {
99 server->oplock_credits++;
102 spin_unlock(&server->req_lock);
103 wake_up(&server->request_q);
105 cifs_reconnect(server);
109 smb2_set_credits(struct TCP_Server_Info *server, const int val)
111 spin_lock(&server->req_lock);
112 server->credits = val;
114 server->reconnect_instance++;
115 spin_unlock(&server->req_lock);
116 /* don't log while holding the lock */
118 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
122 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
126 return &server->echo_credits;
128 return &server->oplock_credits;
130 return &server->credits;
135 smb2_get_credits(struct mid_q_entry *mid)
137 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)mid->resp_buf;
139 return le16_to_cpu(shdr->CreditRequest);
143 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
144 unsigned int *num, unsigned int *credits)
147 unsigned int scredits;
149 spin_lock(&server->req_lock);
151 if (server->credits <= 0) {
152 spin_unlock(&server->req_lock);
153 cifs_num_waiters_inc(server);
154 rc = wait_event_killable(server->request_q,
155 has_credits(server, &server->credits));
156 cifs_num_waiters_dec(server);
159 spin_lock(&server->req_lock);
161 if (server->tcpStatus == CifsExiting) {
162 spin_unlock(&server->req_lock);
166 scredits = server->credits;
167 /* can deadlock with reopen */
169 *num = SMB2_MAX_BUFFER_SIZE;
174 /* leave one credit for a possible reopen */
176 *num = min_t(unsigned int, size,
177 scredits * SMB2_MAX_BUFFER_SIZE);
179 *credits = DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
180 server->credits -= *credits;
185 spin_unlock(&server->req_lock);
190 smb2_get_next_mid(struct TCP_Server_Info *server)
193 /* for SMB2 we need the current value */
194 spin_lock(&GlobalMid_Lock);
195 mid = server->CurrentMid++;
196 spin_unlock(&GlobalMid_Lock);
200 static struct mid_q_entry *
201 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
203 struct mid_q_entry *mid;
204 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
205 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
207 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
208 cifs_dbg(VFS, "encrypted frame parsing not supported yet");
212 spin_lock(&GlobalMid_Lock);
213 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
214 if ((mid->mid == wire_mid) &&
215 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
216 (mid->command == shdr->Command)) {
217 kref_get(&mid->refcount);
218 spin_unlock(&GlobalMid_Lock);
222 spin_unlock(&GlobalMid_Lock);
227 smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
229 #ifdef CONFIG_CIFS_DEBUG2
230 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
232 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
233 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
235 cifs_dbg(VFS, "smb buf %p len %u\n", buf,
236 server->ops->calc_smb_size(buf, server));
241 smb2_need_neg(struct TCP_Server_Info *server)
243 return server->max_read == 0;
247 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
250 ses->server->CurrentMid = 0;
251 rc = SMB2_negotiate(xid, ses);
252 /* BB we probably don't need to retry with modern servers */
259 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
261 struct TCP_Server_Info *server = tcon->ses->server;
264 /* start with specified wsize, or default */
265 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
266 wsize = min_t(unsigned int, wsize, server->max_write);
267 #ifdef CONFIG_CIFS_SMB_DIRECT
270 wsize = min_t(unsigned int,
271 wsize, server->smbd_conn->max_fragmented_send_size);
273 wsize = min_t(unsigned int,
274 wsize, server->smbd_conn->max_readwrite_size);
277 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
278 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
284 smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
286 struct TCP_Server_Info *server = tcon->ses->server;
289 /* start with specified wsize, or default */
290 wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
291 wsize = min_t(unsigned int, wsize, server->max_write);
292 #ifdef CONFIG_CIFS_SMB_DIRECT
295 wsize = min_t(unsigned int,
296 wsize, server->smbd_conn->max_fragmented_send_size);
298 wsize = min_t(unsigned int,
299 wsize, server->smbd_conn->max_readwrite_size);
302 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
303 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
309 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
311 struct TCP_Server_Info *server = tcon->ses->server;
314 /* start with specified rsize, or default */
315 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
316 rsize = min_t(unsigned int, rsize, server->max_read);
317 #ifdef CONFIG_CIFS_SMB_DIRECT
320 rsize = min_t(unsigned int,
321 rsize, server->smbd_conn->max_fragmented_recv_size);
323 rsize = min_t(unsigned int,
324 rsize, server->smbd_conn->max_readwrite_size);
328 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
329 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
335 smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
337 struct TCP_Server_Info *server = tcon->ses->server;
340 /* start with specified rsize, or default */
341 rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
342 rsize = min_t(unsigned int, rsize, server->max_read);
343 #ifdef CONFIG_CIFS_SMB_DIRECT
346 rsize = min_t(unsigned int,
347 rsize, server->smbd_conn->max_fragmented_recv_size);
349 rsize = min_t(unsigned int,
350 rsize, server->smbd_conn->max_readwrite_size);
354 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
355 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
361 parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
363 struct cifs_server_iface **iface_list,
366 struct network_interface_info_ioctl_rsp *p;
367 struct sockaddr_in *addr4;
368 struct sockaddr_in6 *addr6;
369 struct iface_info_ipv4 *p4;
370 struct iface_info_ipv6 *p6;
371 struct cifs_server_iface *info;
381 * Fist pass: count and sanity check
384 bytes_left = buf_len;
386 while (bytes_left >= sizeof(*p)) {
388 next = le32_to_cpu(p->Next);
390 bytes_left -= sizeof(*p);
393 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
398 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
403 if (bytes_left || p->Next)
404 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
408 * Second pass: extract info to internal structure
411 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
418 bytes_left = buf_len;
420 while (bytes_left >= sizeof(*p)) {
421 info->speed = le64_to_cpu(p->LinkSpeed);
422 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE);
423 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE);
425 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
426 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
427 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
428 le32_to_cpu(p->Capability));
432 * The kernel and wire socket structures have the same
433 * layout and use network byte order but make the
434 * conversion explicit in case either one changes.
437 addr4 = (struct sockaddr_in *)&info->sockaddr;
438 p4 = (struct iface_info_ipv4 *)p->Buffer;
439 addr4->sin_family = AF_INET;
440 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
442 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
443 addr4->sin_port = cpu_to_be16(CIFS_PORT);
445 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
449 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
450 p6 = (struct iface_info_ipv6 *)p->Buffer;
451 addr6->sin6_family = AF_INET6;
452 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
454 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
455 addr6->sin6_flowinfo = 0;
456 addr6->sin6_scope_id = 0;
457 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
459 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
464 "%s: skipping unsupported socket family\n",
472 next = le32_to_cpu(p->Next);
475 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
495 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
498 unsigned int ret_data_len = 0;
499 struct network_interface_info_ioctl_rsp *out_buf = NULL;
500 struct cifs_server_iface *iface_list;
502 struct cifs_ses *ses = tcon->ses;
504 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
505 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
506 NULL /* no data input */, 0 /* no data input */,
507 (char **)&out_buf, &ret_data_len);
508 if (rc == -EOPNOTSUPP) {
510 "server does not support query network interfaces\n");
512 } else if (rc != 0) {
513 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
517 rc = parse_server_interfaces(out_buf, ret_data_len,
518 &iface_list, &iface_count);
522 spin_lock(&ses->iface_lock);
523 kfree(ses->iface_list);
524 ses->iface_list = iface_list;
525 ses->iface_count = iface_count;
526 ses->iface_last_update = jiffies;
527 spin_unlock(&ses->iface_lock);
535 smb2_close_cached_fid(struct kref *ref)
537 struct cached_fid *cfid = container_of(ref, struct cached_fid,
540 if (cfid->is_valid) {
541 cifs_dbg(FYI, "clear cached root file handle\n");
542 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
543 cfid->fid->volatile_fid);
544 cfid->is_valid = false;
548 void close_shroot(struct cached_fid *cfid)
550 mutex_lock(&cfid->fid_mutex);
551 kref_put(&cfid->refcount, smb2_close_cached_fid);
552 mutex_unlock(&cfid->fid_mutex);
556 smb2_cached_lease_break(struct work_struct *work)
558 struct cached_fid *cfid = container_of(work,
559 struct cached_fid, lease_break);
565 * Open the directory at the root of a share
567 int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
569 struct cifs_open_parms oparams;
571 __le16 srch_path = 0; /* Null - since an open of top of share */
572 u8 oplock = SMB2_OPLOCK_LEVEL_II;
574 mutex_lock(&tcon->crfid.fid_mutex);
575 if (tcon->crfid.is_valid) {
576 cifs_dbg(FYI, "found a cached root file handle\n");
577 memcpy(pfid, tcon->crfid.fid, sizeof(struct cifs_fid));
578 kref_get(&tcon->crfid.refcount);
579 mutex_unlock(&tcon->crfid.fid_mutex);
584 oparams.create_options = 0;
585 oparams.desired_access = FILE_READ_ATTRIBUTES;
586 oparams.disposition = FILE_OPEN;
588 oparams.reconnect = false;
590 rc = SMB2_open(xid, &oparams, &srch_path, &oplock, NULL, NULL, NULL);
592 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
593 tcon->crfid.tcon = tcon;
594 tcon->crfid.is_valid = true;
595 kref_init(&tcon->crfid.refcount);
596 kref_get(&tcon->crfid.refcount);
598 mutex_unlock(&tcon->crfid.fid_mutex);
603 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
606 __le16 srch_path = 0; /* Null - open root of share */
607 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
608 struct cifs_open_parms oparms;
610 bool no_cached_open = tcon->nohandlecache;
613 oparms.desired_access = FILE_READ_ATTRIBUTES;
614 oparms.disposition = FILE_OPEN;
615 oparms.create_options = 0;
617 oparms.reconnect = false;
620 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
623 rc = open_shroot(xid, tcon, &fid);
628 SMB3_request_interfaces(xid, tcon);
630 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
631 FS_ATTRIBUTE_INFORMATION);
632 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
633 FS_DEVICE_INFORMATION);
634 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
635 FS_VOLUME_INFORMATION);
636 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
637 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
639 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
641 close_shroot(&tcon->crfid);
647 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
650 __le16 srch_path = 0; /* Null - open root of share */
651 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
652 struct cifs_open_parms oparms;
656 oparms.desired_access = FILE_READ_ATTRIBUTES;
657 oparms.disposition = FILE_OPEN;
658 oparms.create_options = 0;
660 oparms.reconnect = false;
662 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
666 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
667 FS_ATTRIBUTE_INFORMATION);
668 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
669 FS_DEVICE_INFORMATION);
670 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
675 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
676 struct cifs_sb_info *cifs_sb, const char *full_path)
680 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
681 struct cifs_open_parms oparms;
684 if ((*full_path == 0) && tcon->crfid.is_valid)
687 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
692 oparms.desired_access = FILE_READ_ATTRIBUTES;
693 oparms.disposition = FILE_OPEN;
694 if (backup_cred(cifs_sb))
695 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
697 oparms.create_options = 0;
699 oparms.reconnect = false;
701 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
707 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
713 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
714 struct cifs_sb_info *cifs_sb, const char *full_path,
715 u64 *uniqueid, FILE_ALL_INFO *data)
717 *uniqueid = le64_to_cpu(data->IndexNumber);
722 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
723 struct cifs_fid *fid, FILE_ALL_INFO *data)
726 struct smb2_file_all_info *smb2_data;
728 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
730 if (smb2_data == NULL)
733 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
736 move_smb2_info_to_cifs(data, smb2_data);
741 #ifdef CONFIG_CIFS_XATTR
743 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
744 struct smb2_file_full_ea_info *src, size_t src_size,
745 const unsigned char *ea_name)
748 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
750 size_t buf_size = dst_size;
751 size_t name_len, value_len, user_name_len;
753 while (src_size > 0) {
754 name = &src->ea_data[0];
755 name_len = (size_t)src->ea_name_length;
756 value = &src->ea_data[src->ea_name_length + 1];
757 value_len = (size_t)le16_to_cpu(src->ea_value_length);
763 if (src_size < 8 + name_len + 1 + value_len) {
764 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
770 if (ea_name_len == name_len &&
771 memcmp(ea_name, name, name_len) == 0) {
775 if (dst_size < value_len) {
779 memcpy(dst, value, value_len);
783 /* 'user.' plus a terminating null */
784 user_name_len = 5 + 1 + name_len;
787 /* skip copy - calc size only */
789 } else if (dst_size >= user_name_len) {
790 dst_size -= user_name_len;
791 memcpy(dst, "user.", 5);
793 memcpy(dst, src->ea_data, name_len);
799 /* stop before overrun buffer */
805 if (!src->next_entry_offset)
808 if (src_size < le32_to_cpu(src->next_entry_offset)) {
809 /* stop before overrun buffer */
813 src_size -= le32_to_cpu(src->next_entry_offset);
814 src = (void *)((char *)src +
815 le32_to_cpu(src->next_entry_offset));
818 /* didn't find the named attribute */
827 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
828 const unsigned char *path, const unsigned char *ea_name,
829 char *ea_data, size_t buf_size,
830 struct cifs_sb_info *cifs_sb)
834 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
835 struct cifs_open_parms oparms;
837 struct smb2_file_full_ea_info *smb2_data;
838 int ea_buf_size = SMB2_MIN_EA_BUF;
840 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
845 oparms.desired_access = FILE_READ_EA;
846 oparms.disposition = FILE_OPEN;
847 if (backup_cred(cifs_sb))
848 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
850 oparms.create_options = 0;
852 oparms.reconnect = false;
854 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
857 cifs_dbg(FYI, "open failed rc=%d\n", rc);
862 smb2_data = kzalloc(ea_buf_size, GFP_KERNEL);
863 if (smb2_data == NULL) {
864 SMB2_close(xid, tcon, fid.persistent_fid,
869 rc = SMB2_query_eas(xid, tcon, fid.persistent_fid,
871 ea_buf_size, smb2_data);
879 if (ea_buf_size > SMB2_MAX_EA_BUF) {
880 cifs_dbg(VFS, "EA size is too large\n");
881 SMB2_close(xid, tcon, fid.persistent_fid,
887 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
890 * If ea_name is NULL (listxattr) and there are no EAs, return 0 as it's
891 * not an error. Otherwise, the specified ea_name was not found.
894 rc = move_smb2_ea_to_cifs(ea_data, buf_size, smb2_data,
895 SMB2_MAX_EA_BUF, ea_name);
896 else if (!ea_name && rc == -ENODATA)
905 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
906 const char *path, const char *ea_name, const void *ea_value,
907 const __u16 ea_value_len, const struct nls_table *nls_codepage,
908 struct cifs_sb_info *cifs_sb)
912 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
913 struct cifs_open_parms oparms;
915 struct smb2_file_full_ea_info *ea;
916 int ea_name_len = strlen(ea_name);
919 if (ea_name_len > 255)
922 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
927 oparms.desired_access = FILE_WRITE_EA;
928 oparms.disposition = FILE_OPEN;
929 if (backup_cred(cifs_sb))
930 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
932 oparms.create_options = 0;
934 oparms.reconnect = false;
936 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
939 cifs_dbg(FYI, "open failed rc=%d\n", rc);
943 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
944 ea = kzalloc(len, GFP_KERNEL);
946 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
950 ea->ea_name_length = ea_name_len;
951 ea->ea_value_length = cpu_to_le16(ea_value_len);
952 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
953 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
955 rc = SMB2_set_ea(xid, tcon, fid.persistent_fid, fid.volatile_fid, ea,
959 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
966 smb2_can_echo(struct TCP_Server_Info *server)
968 return server->echoes;
972 smb2_clear_stats(struct cifs_tcon *tcon)
975 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
976 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
977 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
982 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
984 seq_puts(m, "\n\tShare Capabilities:");
985 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
986 seq_puts(m, " DFS,");
987 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
988 seq_puts(m, " CONTINUOUS AVAILABILITY,");
989 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
990 seq_puts(m, " SCALEOUT,");
991 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
992 seq_puts(m, " CLUSTER,");
993 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
994 seq_puts(m, " ASYMMETRIC,");
995 if (tcon->capabilities == 0)
996 seq_puts(m, " None");
997 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
998 seq_puts(m, " Aligned,");
999 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1000 seq_puts(m, " Partition Aligned,");
1001 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1002 seq_puts(m, " SSD,");
1003 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1004 seq_puts(m, " TRIM-support,");
1006 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1007 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1008 if (tcon->perf_sector_size)
1009 seq_printf(m, "\tOptimal sector size: 0x%x",
1010 tcon->perf_sector_size);
1011 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1015 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1017 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1018 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1021 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1022 * totals (requests sent) since those SMBs are per-session not per tcon
1024 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1025 (long long)(tcon->bytes_read),
1026 (long long)(tcon->bytes_written));
1027 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1028 atomic_read(&tcon->num_local_opens),
1029 atomic_read(&tcon->num_remote_opens));
1030 seq_printf(m, "\nTreeConnects: %d total %d failed",
1031 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1032 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1033 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1034 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1035 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1036 seq_printf(m, "\nCreates: %d total %d failed",
1037 atomic_read(&sent[SMB2_CREATE_HE]),
1038 atomic_read(&failed[SMB2_CREATE_HE]));
1039 seq_printf(m, "\nCloses: %d total %d failed",
1040 atomic_read(&sent[SMB2_CLOSE_HE]),
1041 atomic_read(&failed[SMB2_CLOSE_HE]));
1042 seq_printf(m, "\nFlushes: %d total %d failed",
1043 atomic_read(&sent[SMB2_FLUSH_HE]),
1044 atomic_read(&failed[SMB2_FLUSH_HE]));
1045 seq_printf(m, "\nReads: %d total %d failed",
1046 atomic_read(&sent[SMB2_READ_HE]),
1047 atomic_read(&failed[SMB2_READ_HE]));
1048 seq_printf(m, "\nWrites: %d total %d failed",
1049 atomic_read(&sent[SMB2_WRITE_HE]),
1050 atomic_read(&failed[SMB2_WRITE_HE]));
1051 seq_printf(m, "\nLocks: %d total %d failed",
1052 atomic_read(&sent[SMB2_LOCK_HE]),
1053 atomic_read(&failed[SMB2_LOCK_HE]));
1054 seq_printf(m, "\nIOCTLs: %d total %d failed",
1055 atomic_read(&sent[SMB2_IOCTL_HE]),
1056 atomic_read(&failed[SMB2_IOCTL_HE]));
1057 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1058 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1059 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1060 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1061 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1062 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1063 seq_printf(m, "\nQueryInfos: %d total %d failed",
1064 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1065 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1066 seq_printf(m, "\nSetInfos: %d total %d failed",
1067 atomic_read(&sent[SMB2_SET_INFO_HE]),
1068 atomic_read(&failed[SMB2_SET_INFO_HE]));
1069 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1070 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1071 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1075 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1077 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1078 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1080 cfile->fid.persistent_fid = fid->persistent_fid;
1081 cfile->fid.volatile_fid = fid->volatile_fid;
1082 #ifdef CONFIG_CIFS_DEBUG2
1083 cfile->fid.mid = fid->mid;
1084 #endif /* CIFS_DEBUG2 */
1085 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1087 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1088 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1092 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1093 struct cifs_fid *fid)
1095 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1099 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1100 u64 persistent_fid, u64 volatile_fid,
1101 struct copychunk_ioctl *pcchunk)
1104 unsigned int ret_data_len;
1105 struct resume_key_req *res_key;
1107 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1108 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1109 NULL, 0 /* no input */,
1110 (char **)&res_key, &ret_data_len);
1113 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1114 goto req_res_key_exit;
1116 if (ret_data_len < sizeof(struct resume_key_req)) {
1117 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
1119 goto req_res_key_exit;
1121 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1129 smb2_ioctl_query_info(const unsigned int xid,
1130 struct cifs_tcon *tcon,
1131 __le16 *path, int is_dir,
1134 struct cifs_ses *ses = tcon->ses;
1135 char __user *arg = (char __user *)p;
1136 struct smb_query_info qi;
1137 struct smb_query_info __user *pqi;
1140 struct smb2_query_info_rsp *rsp = NULL;
1141 void *buffer = NULL;
1142 struct smb_rqst rqst[3];
1143 int resp_buftype[3];
1144 struct kvec rsp_iov[3];
1145 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1146 struct cifs_open_parms oparms;
1147 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1148 struct cifs_fid fid;
1149 struct kvec qi_iov[1];
1150 struct kvec close_iov[1];
1152 memset(rqst, 0, sizeof(rqst));
1153 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1154 memset(rsp_iov, 0, sizeof(rsp_iov));
1156 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1159 if (qi.output_buffer_length > 1024)
1162 if (!ses || !(ses->server))
1165 if (smb3_encryption_required(tcon))
1166 flags |= CIFS_TRANSFORM_REQ;
1168 buffer = kmalloc(qi.output_buffer_length, GFP_KERNEL);
1172 if (copy_from_user(buffer, arg + sizeof(struct smb_query_info),
1173 qi.output_buffer_length)) {
1179 memset(&open_iov, 0, sizeof(open_iov));
1180 rqst[0].rq_iov = open_iov;
1181 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1183 memset(&oparms, 0, sizeof(oparms));
1185 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1186 oparms.disposition = FILE_OPEN;
1188 oparms.create_options = CREATE_NOT_FILE;
1190 oparms.create_options = CREATE_NOT_DIR;
1192 oparms.reconnect = false;
1194 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, path);
1197 smb2_set_next_command(ses->server, &rqst[0], 0);
1200 memset(&qi_iov, 0, sizeof(qi_iov));
1201 rqst[1].rq_iov = qi_iov;
1202 rqst[1].rq_nvec = 1;
1204 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1205 qi.file_info_class, qi.info_type,
1206 qi.additional_information,
1207 qi.input_buffer_length,
1208 qi.output_buffer_length, buffer);
1211 smb2_set_next_command(ses->server, &rqst[1], 0);
1212 smb2_set_related(&rqst[1]);
1215 memset(&close_iov, 0, sizeof(close_iov));
1216 rqst[2].rq_iov = close_iov;
1217 rqst[2].rq_nvec = 1;
1219 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1222 smb2_set_related(&rqst[2]);
1224 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1225 resp_buftype, rsp_iov);
1228 pqi = (struct smb_query_info __user *)arg;
1229 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1230 if (le32_to_cpu(rsp->OutputBufferLength) < qi.input_buffer_length)
1231 qi.input_buffer_length = le32_to_cpu(rsp->OutputBufferLength);
1232 if (copy_to_user(&pqi->input_buffer_length, &qi.input_buffer_length,
1233 sizeof(qi.input_buffer_length))) {
1237 if (copy_to_user(pqi + 1, rsp->Buffer, qi.input_buffer_length)) {
1244 SMB2_open_free(&rqst[0]);
1245 SMB2_query_info_free(&rqst[1]);
1246 SMB2_close_free(&rqst[2]);
1247 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1248 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1249 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1254 smb2_copychunk_range(const unsigned int xid,
1255 struct cifsFileInfo *srcfile,
1256 struct cifsFileInfo *trgtfile, u64 src_off,
1257 u64 len, u64 dest_off)
1260 unsigned int ret_data_len;
1261 struct copychunk_ioctl *pcchunk;
1262 struct copychunk_ioctl_rsp *retbuf = NULL;
1263 struct cifs_tcon *tcon;
1264 int chunks_copied = 0;
1265 bool chunk_sizes_updated = false;
1266 ssize_t bytes_written, total_bytes_written = 0;
1268 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1270 if (pcchunk == NULL)
1273 cifs_dbg(FYI, "in smb2_copychunk_range - about to call request res key\n");
1274 /* Request a key from the server to identify the source of the copy */
1275 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1276 srcfile->fid.persistent_fid,
1277 srcfile->fid.volatile_fid, pcchunk);
1279 /* Note: request_res_key sets res_key null only if rc !=0 */
1283 /* For now array only one chunk long, will make more flexible later */
1284 pcchunk->ChunkCount = cpu_to_le32(1);
1285 pcchunk->Reserved = 0;
1286 pcchunk->Reserved2 = 0;
1288 tcon = tlink_tcon(trgtfile->tlink);
1291 pcchunk->SourceOffset = cpu_to_le64(src_off);
1292 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1294 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1296 /* Request server copy to target from src identified by key */
1297 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1298 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1299 true /* is_fsctl */, (char *)pcchunk,
1300 sizeof(struct copychunk_ioctl), (char **)&retbuf,
1304 sizeof(struct copychunk_ioctl_rsp)) {
1305 cifs_dbg(VFS, "invalid cchunk response size\n");
1309 if (retbuf->TotalBytesWritten == 0) {
1310 cifs_dbg(FYI, "no bytes copied\n");
1315 * Check if server claimed to write more than we asked
1317 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1318 le32_to_cpu(pcchunk->Length)) {
1319 cifs_dbg(VFS, "invalid copy chunk response\n");
1323 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1324 cifs_dbg(VFS, "invalid num chunks written\n");
1330 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1331 src_off += bytes_written;
1332 dest_off += bytes_written;
1333 len -= bytes_written;
1334 total_bytes_written += bytes_written;
1336 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1337 le32_to_cpu(retbuf->ChunksWritten),
1338 le32_to_cpu(retbuf->ChunkBytesWritten),
1340 } else if (rc == -EINVAL) {
1341 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1344 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1345 le32_to_cpu(retbuf->ChunksWritten),
1346 le32_to_cpu(retbuf->ChunkBytesWritten),
1347 le32_to_cpu(retbuf->TotalBytesWritten));
1350 * Check if this is the first request using these sizes,
1351 * (ie check if copy succeed once with original sizes
1352 * and check if the server gave us different sizes after
1353 * we already updated max sizes on previous request).
1354 * if not then why is the server returning an error now
1356 if ((chunks_copied != 0) || chunk_sizes_updated)
1359 /* Check that server is not asking us to grow size */
1360 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1361 tcon->max_bytes_chunk)
1362 tcon->max_bytes_chunk =
1363 le32_to_cpu(retbuf->ChunkBytesWritten);
1365 goto cchunk_out; /* server gave us bogus size */
1367 /* No need to change MaxChunks since already set to 1 */
1368 chunk_sizes_updated = true;
1379 return total_bytes_written;
1383 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1384 struct cifs_fid *fid)
1386 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1390 smb2_read_data_offset(char *buf)
1392 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1393 return rsp->DataOffset;
1397 smb2_read_data_length(char *buf, bool in_remaining)
1399 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1402 return le32_to_cpu(rsp->DataRemaining);
1404 return le32_to_cpu(rsp->DataLength);
1409 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1410 struct cifs_io_parms *parms, unsigned int *bytes_read,
1411 char **buf, int *buf_type)
1413 parms->persistent_fid = pfid->persistent_fid;
1414 parms->volatile_fid = pfid->volatile_fid;
1415 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1419 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1420 struct cifs_io_parms *parms, unsigned int *written,
1421 struct kvec *iov, unsigned long nr_segs)
1424 parms->persistent_fid = pfid->persistent_fid;
1425 parms->volatile_fid = pfid->volatile_fid;
1426 return SMB2_write(xid, parms, written, iov, nr_segs);
1429 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1430 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1431 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1433 struct cifsInodeInfo *cifsi;
1436 cifsi = CIFS_I(inode);
1438 /* if file already sparse don't bother setting sparse again */
1439 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1440 return true; /* already sparse */
1442 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1443 return true; /* already not sparse */
1446 * Can't check for sparse support on share the usual way via the
1447 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1448 * since Samba server doesn't set the flag on the share, yet
1449 * supports the set sparse FSCTL and returns sparse correctly
1450 * in the file attributes. If we fail setting sparse though we
1451 * mark that server does not support sparse files for this share
1452 * to avoid repeatedly sending the unsupported fsctl to server
1453 * if the file is repeatedly extended.
1455 if (tcon->broken_sparse_sup)
1458 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1459 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1461 &setsparse, 1, NULL, NULL);
1463 tcon->broken_sparse_sup = true;
1464 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1469 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1471 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1477 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1478 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1480 __le64 eof = cpu_to_le64(size);
1481 struct inode *inode;
1484 * If extending file more than one page make sparse. Many Linux fs
1485 * make files sparse by default when extending via ftruncate
1487 inode = d_inode(cfile->dentry);
1489 if (!set_alloc && (size > inode->i_size + 8192)) {
1490 __u8 set_sparse = 1;
1492 /* whether set sparse succeeds or not, extend the file */
1493 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1496 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1497 cfile->fid.volatile_fid, cfile->pid, &eof);
1501 smb2_duplicate_extents(const unsigned int xid,
1502 struct cifsFileInfo *srcfile,
1503 struct cifsFileInfo *trgtfile, u64 src_off,
1504 u64 len, u64 dest_off)
1507 unsigned int ret_data_len;
1508 struct duplicate_extents_to_file dup_ext_buf;
1509 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1511 /* server fileays advertise duplicate extent support with this flag */
1512 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1513 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1516 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1517 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1518 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1519 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1520 dup_ext_buf.ByteCount = cpu_to_le64(len);
1521 cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
1522 src_off, dest_off, len);
1524 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1526 goto duplicate_extents_out;
1528 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1529 trgtfile->fid.volatile_fid,
1530 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
1531 true /* is_fsctl */,
1532 (char *)&dup_ext_buf,
1533 sizeof(struct duplicate_extents_to_file),
1537 if (ret_data_len > 0)
1538 cifs_dbg(FYI, "non-zero response length in duplicate extents");
1540 duplicate_extents_out:
1545 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1546 struct cifsFileInfo *cfile)
1548 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1549 cfile->fid.volatile_fid);
1553 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1554 struct cifsFileInfo *cfile)
1556 struct fsctl_set_integrity_information_req integr_info;
1557 unsigned int ret_data_len;
1559 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1560 integr_info.Flags = 0;
1561 integr_info.Reserved = 0;
1563 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1564 cfile->fid.volatile_fid,
1565 FSCTL_SET_INTEGRITY_INFORMATION,
1566 true /* is_fsctl */,
1567 (char *)&integr_info,
1568 sizeof(struct fsctl_set_integrity_information_req),
1574 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
1575 #define GMT_TOKEN_SIZE 50
1578 * Input buffer contains (empty) struct smb_snapshot array with size filled in
1579 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
1582 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1583 struct cifsFileInfo *cfile, void __user *ioc_buf)
1585 char *retbuf = NULL;
1586 unsigned int ret_data_len = 0;
1588 struct smb_snapshot_array snapshot_in;
1590 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1591 cfile->fid.volatile_fid,
1592 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
1593 true /* is_fsctl */,
1594 NULL, 0 /* no input data */,
1597 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1602 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1604 if (copy_from_user(&snapshot_in, ioc_buf,
1605 sizeof(struct smb_snapshot_array))) {
1612 * Check for min size, ie not large enough to fit even one GMT
1613 * token (snapshot). On the first ioctl some users may pass in
1614 * smaller size (or zero) to simply get the size of the array
1615 * so the user space caller can allocate sufficient memory
1616 * and retry the ioctl again with larger array size sufficient
1617 * to hold all of the snapshot GMT tokens on the second try.
1619 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
1620 ret_data_len = sizeof(struct smb_snapshot_array);
1623 * We return struct SRV_SNAPSHOT_ARRAY, followed by
1624 * the snapshot array (of 50 byte GMT tokens) each
1625 * representing an available previous version of the data
1627 if (ret_data_len > (snapshot_in.snapshot_array_size +
1628 sizeof(struct smb_snapshot_array)))
1629 ret_data_len = snapshot_in.snapshot_array_size +
1630 sizeof(struct smb_snapshot_array);
1632 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1641 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1642 const char *path, struct cifs_sb_info *cifs_sb,
1643 struct cifs_fid *fid, __u16 search_flags,
1644 struct cifs_search_info *srch_inf)
1648 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1649 struct cifs_open_parms oparms;
1651 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1656 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1657 oparms.disposition = FILE_OPEN;
1658 if (backup_cred(cifs_sb))
1659 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1661 oparms.create_options = 0;
1663 oparms.reconnect = false;
1665 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
1668 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
1672 srch_inf->entries_in_buffer = 0;
1673 srch_inf->index_of_last_entry = 2;
1675 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1676 fid->volatile_fid, 0, srch_inf);
1678 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
1679 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1685 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1686 struct cifs_fid *fid, __u16 search_flags,
1687 struct cifs_search_info *srch_inf)
1689 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1690 fid->volatile_fid, 0, srch_inf);
1694 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1695 struct cifs_fid *fid)
1697 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1701 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
1702 * the number of credits and return true. Otherwise - return false.
1705 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
1707 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
1709 if (shdr->Status != STATUS_PENDING)
1713 spin_lock(&server->req_lock);
1714 server->credits += le16_to_cpu(shdr->CreditRequest);
1715 spin_unlock(&server->req_lock);
1716 wake_up(&server->request_q);
1723 smb2_is_session_expired(char *buf)
1725 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
1727 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
1728 shdr->Status != STATUS_USER_SESSION_DELETED)
1731 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
1732 le16_to_cpu(shdr->Command),
1733 le64_to_cpu(shdr->MessageId));
1734 cifs_dbg(FYI, "Session expired or deleted\n");
1740 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
1741 struct cifsInodeInfo *cinode)
1743 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
1744 return SMB2_lease_break(0, tcon, cinode->lease_key,
1745 smb2_get_lease_state(cinode));
1747 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
1749 CIFS_CACHE_READ(cinode) ? 1 : 0);
1753 smb2_set_related(struct smb_rqst *rqst)
1755 struct smb2_sync_hdr *shdr;
1757 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
1758 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
1761 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
1764 smb2_set_next_command(struct TCP_Server_Info *server, struct smb_rqst *rqst,
1765 bool has_space_for_padding)
1767 struct smb2_sync_hdr *shdr;
1768 unsigned long len = smb_rqst_len(server, rqst);
1770 /* SMB headers in a compound are 8 byte aligned. */
1772 if (has_space_for_padding) {
1773 len = rqst->rq_iov[rqst->rq_nvec - 1].iov_len;
1774 rqst->rq_iov[rqst->rq_nvec - 1].iov_len =
1777 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
1778 rqst->rq_iov[rqst->rq_nvec].iov_len = 8 - (len & 7);
1781 len = smb_rqst_len(server, rqst);
1784 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
1785 shdr->NextCommand = cpu_to_le32(len);
1789 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1790 struct kstatfs *buf)
1792 struct smb2_query_info_rsp *rsp;
1793 struct smb2_fs_full_size_info *info = NULL;
1794 struct smb_rqst rqst[3];
1795 int resp_buftype[3];
1796 struct kvec rsp_iov[3];
1797 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1798 struct kvec qi_iov[1];
1799 struct kvec close_iov[1];
1800 struct cifs_ses *ses = tcon->ses;
1801 struct TCP_Server_Info *server = ses->server;
1802 __le16 srch_path = 0; /* Null - open root of share */
1803 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1804 struct cifs_open_parms oparms;
1805 struct cifs_fid fid;
1809 if (smb3_encryption_required(tcon))
1810 flags |= CIFS_TRANSFORM_REQ;
1812 memset(rqst, 0, sizeof(rqst));
1813 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1814 memset(rsp_iov, 0, sizeof(rsp_iov));
1816 memset(&open_iov, 0, sizeof(open_iov));
1817 rqst[0].rq_iov = open_iov;
1818 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1821 oparms.desired_access = FILE_READ_ATTRIBUTES;
1822 oparms.disposition = FILE_OPEN;
1823 oparms.create_options = 0;
1825 oparms.reconnect = false;
1827 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, &srch_path);
1830 smb2_set_next_command(server, &rqst[0], 0);
1832 memset(&qi_iov, 0, sizeof(qi_iov));
1833 rqst[1].rq_iov = qi_iov;
1834 rqst[1].rq_nvec = 1;
1836 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1837 FS_FULL_SIZE_INFORMATION,
1838 SMB2_O_INFO_FILESYSTEM, 0,
1839 sizeof(struct smb2_fs_full_size_info), 0,
1843 smb2_set_next_command(server, &rqst[1], 0);
1844 smb2_set_related(&rqst[1]);
1846 memset(&close_iov, 0, sizeof(close_iov));
1847 rqst[2].rq_iov = close_iov;
1848 rqst[2].rq_nvec = 1;
1850 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1853 smb2_set_related(&rqst[2]);
1855 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1856 resp_buftype, rsp_iov);
1860 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1861 buf->f_type = SMB2_MAGIC_NUMBER;
1862 info = (struct smb2_fs_full_size_info *)(
1863 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1864 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1865 le32_to_cpu(rsp->OutputBufferLength),
1867 sizeof(struct smb2_fs_full_size_info));
1869 smb2_copy_fs_info_to_kstatfs(info, buf);
1872 SMB2_open_free(&rqst[0]);
1873 SMB2_query_info_free(&rqst[1]);
1874 SMB2_close_free(&rqst[2]);
1875 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1876 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1877 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1882 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1883 struct kstatfs *buf)
1886 __le16 srch_path = 0; /* Null - open root of share */
1887 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1888 struct cifs_open_parms oparms;
1889 struct cifs_fid fid;
1891 if (!tcon->posix_extensions)
1892 return smb2_queryfs(xid, tcon, buf);
1895 oparms.desired_access = FILE_READ_ATTRIBUTES;
1896 oparms.disposition = FILE_OPEN;
1897 oparms.create_options = 0;
1899 oparms.reconnect = false;
1901 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
1905 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
1906 fid.volatile_fid, buf);
1907 buf->f_type = SMB2_MAGIC_NUMBER;
1908 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1913 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
1915 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
1916 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
1920 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
1921 __u64 length, __u32 type, int lock, int unlock, bool wait)
1923 if (unlock && !lock)
1924 type = SMB2_LOCKFLAG_UNLOCK;
1925 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
1926 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
1927 current->tgid, length, offset, type, wait);
1931 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
1933 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
1937 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
1939 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
1943 smb2_new_lease_key(struct cifs_fid *fid)
1945 generate_random_uuid(fid->lease_key);
1949 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
1950 const char *search_name,
1951 struct dfs_info3_param **target_nodes,
1952 unsigned int *num_of_nodes,
1953 const struct nls_table *nls_codepage, int remap)
1956 __le16 *utf16_path = NULL;
1957 int utf16_path_len = 0;
1958 struct cifs_tcon *tcon;
1959 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
1960 struct get_dfs_referral_rsp *dfs_rsp = NULL;
1961 u32 dfs_req_size = 0, dfs_rsp_size = 0;
1963 cifs_dbg(FYI, "smb2_get_dfs_refer path <%s>\n", search_name);
1966 * Try to use the IPC tcon, otherwise just use any
1968 tcon = ses->tcon_ipc;
1970 spin_lock(&cifs_tcp_ses_lock);
1971 tcon = list_first_entry_or_null(&ses->tcon_list,
1976 spin_unlock(&cifs_tcp_ses_lock);
1980 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
1986 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
1988 nls_codepage, remap);
1994 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
1995 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2001 /* Highest DFS referral version understood */
2002 dfs_req->MaxReferralLevel = DFS_VERSION;
2004 /* Path to resolve in an UTF-16 null-terminated string */
2005 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2008 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2009 FSCTL_DFS_GET_REFERRALS,
2010 true /* is_fsctl */,
2011 (char *)dfs_req, dfs_req_size,
2012 (char **)&dfs_rsp, &dfs_rsp_size);
2013 } while (rc == -EAGAIN);
2016 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
2017 cifs_dbg(VFS, "ioctl error in smb2_get_dfs_refer rc=%d\n", rc);
2021 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2022 num_of_nodes, target_nodes,
2023 nls_codepage, remap, search_name,
2024 true /* is_unicode */);
2026 cifs_dbg(VFS, "parse error in smb2_get_dfs_refer rc=%d\n", rc);
2031 if (tcon && !tcon->ipc) {
2032 /* ipc tcons are not refcounted */
2033 spin_lock(&cifs_tcp_ses_lock);
2035 spin_unlock(&cifs_tcp_ses_lock);
2042 #define SMB2_SYMLINK_STRUCT_SIZE \
2043 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2046 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
2047 const char *full_path, char **target_path,
2048 struct cifs_sb_info *cifs_sb)
2052 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2053 struct cifs_open_parms oparms;
2054 struct cifs_fid fid;
2055 struct kvec err_iov = {NULL, 0};
2056 struct smb2_err_rsp *err_buf = NULL;
2058 struct smb2_symlink_err_rsp *symlink;
2059 unsigned int sub_len;
2060 unsigned int sub_offset;
2061 unsigned int print_len;
2062 unsigned int print_offset;
2064 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2066 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2071 oparms.desired_access = FILE_READ_ATTRIBUTES;
2072 oparms.disposition = FILE_OPEN;
2073 if (backup_cred(cifs_sb))
2074 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2076 oparms.create_options = 0;
2078 oparms.reconnect = false;
2080 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_iov,
2082 if (!rc || !err_iov.iov_base) {
2087 err_buf = err_iov.iov_base;
2088 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
2089 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
2094 /* open must fail on symlink - reset rc */
2096 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
2097 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
2098 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
2099 print_len = le16_to_cpu(symlink->PrintNameLength);
2100 print_offset = le16_to_cpu(symlink->PrintNameOffset);
2102 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
2107 if (err_iov.iov_len <
2108 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
2113 *target_path = cifs_strndup_from_utf16(
2114 (char *)symlink->PathBuffer + sub_offset,
2115 sub_len, true, cifs_sb->local_nls);
2116 if (!(*target_path)) {
2120 convert_delimiter(*target_path, '/');
2121 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2124 free_rsp_buf(resp_buftype, err_buf);
2130 #ifdef CONFIG_CIFS_ACL
2131 static struct cifs_ntsd *
2132 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
2133 const struct cifs_fid *cifsfid, u32 *pacllen)
2135 struct cifs_ntsd *pntsd = NULL;
2137 int rc = -EOPNOTSUPP;
2138 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2141 return ERR_CAST(tlink);
2144 cifs_dbg(FYI, "trying to get acl\n");
2146 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
2147 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
2150 cifs_put_tlink(tlink);
2152 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2159 static struct cifs_ntsd *
2160 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
2161 const char *path, u32 *pacllen)
2163 struct cifs_ntsd *pntsd = NULL;
2164 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2167 struct cifs_tcon *tcon;
2168 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2169 struct cifs_fid fid;
2170 struct cifs_open_parms oparms;
2173 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
2175 return ERR_CAST(tlink);
2177 tcon = tlink_tcon(tlink);
2180 if (backup_cred(cifs_sb))
2181 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2183 oparms.create_options = 0;
2185 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2193 oparms.desired_access = READ_CONTROL;
2194 oparms.disposition = FILE_OPEN;
2196 oparms.reconnect = false;
2198 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2201 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2202 fid.volatile_fid, (void **)&pntsd, pacllen);
2203 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2206 cifs_put_tlink(tlink);
2209 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2215 #ifdef CONFIG_CIFS_ACL
2217 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
2218 struct inode *inode, const char *path, int aclflag)
2220 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2222 int rc, access_flags = 0;
2223 struct cifs_tcon *tcon;
2224 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2225 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2226 struct cifs_fid fid;
2227 struct cifs_open_parms oparms;
2230 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
2232 return PTR_ERR(tlink);
2234 tcon = tlink_tcon(tlink);
2237 if (backup_cred(cifs_sb))
2238 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2240 oparms.create_options = 0;
2242 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
2243 access_flags = WRITE_OWNER;
2245 access_flags = WRITE_DAC;
2247 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2255 oparms.desired_access = access_flags;
2256 oparms.disposition = FILE_OPEN;
2259 oparms.reconnect = false;
2261 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2264 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2265 fid.volatile_fid, pnntsd, acllen, aclflag);
2266 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2269 cifs_put_tlink(tlink);
2273 #endif /* CIFS_ACL */
2275 /* Retrieve an ACL from the server */
2276 static struct cifs_ntsd *
2277 get_smb2_acl(struct cifs_sb_info *cifs_sb,
2278 struct inode *inode, const char *path,
2281 struct cifs_ntsd *pntsd = NULL;
2282 struct cifsFileInfo *open_file = NULL;
2285 open_file = find_readable_file(CIFS_I(inode), true);
2287 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
2289 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
2290 cifsFileInfo_put(open_file);
2295 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
2296 loff_t offset, loff_t len, bool keep_size)
2298 struct inode *inode;
2299 struct cifsInodeInfo *cifsi;
2300 struct cifsFileInfo *cfile = file->private_data;
2301 struct file_zero_data_information fsctl_buf;
2307 inode = d_inode(cfile->dentry);
2308 cifsi = CIFS_I(inode);
2310 /* if file not oplocked can't be sure whether asking to extend size */
2311 if (!CIFS_CACHE_READ(cifsi))
2312 if (keep_size == false) {
2319 * Must check if file sparse since fallocate -z (zero range) assumes
2320 * non-sparse allocation
2322 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
2329 * need to make sure we are not asked to extend the file since the SMB3
2330 * fsctl does not change the file size. In the future we could change
2331 * this to zero the first part of the range then set the file size
2332 * which for a non sparse file would zero the newly extended range
2334 if (keep_size == false)
2335 if (i_size_read(inode) < offset + len) {
2341 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
2343 fsctl_buf.FileOffset = cpu_to_le64(offset);
2344 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2346 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2347 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
2348 true /* is_fctl */, (char *)&fsctl_buf,
2349 sizeof(struct file_zero_data_information), NULL, NULL);
2354 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
2355 loff_t offset, loff_t len)
2357 struct inode *inode;
2358 struct cifsInodeInfo *cifsi;
2359 struct cifsFileInfo *cfile = file->private_data;
2360 struct file_zero_data_information fsctl_buf;
2363 __u8 set_sparse = 1;
2367 inode = d_inode(cfile->dentry);
2368 cifsi = CIFS_I(inode);
2370 /* Need to make file sparse, if not already, before freeing range. */
2371 /* Consider adding equivalent for compressed since it could also work */
2372 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
2378 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
2380 fsctl_buf.FileOffset = cpu_to_le64(offset);
2381 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2383 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2384 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
2385 true /* is_fctl */, (char *)&fsctl_buf,
2386 sizeof(struct file_zero_data_information), NULL, NULL);
2391 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
2392 loff_t off, loff_t len, bool keep_size)
2394 struct inode *inode;
2395 struct cifsInodeInfo *cifsi;
2396 struct cifsFileInfo *cfile = file->private_data;
2397 long rc = -EOPNOTSUPP;
2402 inode = d_inode(cfile->dentry);
2403 cifsi = CIFS_I(inode);
2405 /* if file not oplocked can't be sure whether asking to extend size */
2406 if (!CIFS_CACHE_READ(cifsi))
2407 if (keep_size == false) {
2413 * Files are non-sparse by default so falloc may be a no-op
2414 * Must check if file sparse. If not sparse, and not extending
2415 * then no need to do anything since file already allocated
2417 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
2418 if (keep_size == true)
2420 /* check if extending file */
2421 else if (i_size_read(inode) >= off + len)
2422 /* not extending file and already not sparse */
2424 /* BB: in future add else clause to extend file */
2431 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
2433 * Check if falloc starts within first few pages of file
2434 * and ends within a few pages of the end of file to
2435 * ensure that most of file is being forced to be
2436 * fallocated now. If so then setting whole file sparse
2437 * ie potentially making a few extra pages at the beginning
2438 * or end of the file non-sparse via set_sparse is harmless.
2440 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
2446 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
2448 /* BB: else ... in future add code to extend file and set sparse */
2456 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
2457 loff_t off, loff_t len)
2459 /* KEEP_SIZE already checked for by do_fallocate */
2460 if (mode & FALLOC_FL_PUNCH_HOLE)
2461 return smb3_punch_hole(file, tcon, off, len);
2462 else if (mode & FALLOC_FL_ZERO_RANGE) {
2463 if (mode & FALLOC_FL_KEEP_SIZE)
2464 return smb3_zero_range(file, tcon, off, len, true);
2465 return smb3_zero_range(file, tcon, off, len, false);
2466 } else if (mode == FALLOC_FL_KEEP_SIZE)
2467 return smb3_simple_falloc(file, tcon, off, len, true);
2469 return smb3_simple_falloc(file, tcon, off, len, false);
2475 smb2_downgrade_oplock(struct TCP_Server_Info *server,
2476 struct cifsInodeInfo *cinode, bool set_level2)
2479 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
2482 server->ops->set_oplock_level(cinode, 0, 0, NULL);
2486 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2487 unsigned int epoch, bool *purge_cache)
2490 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
2492 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2493 cinode->oplock = CIFS_CACHE_RHW_FLG;
2494 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
2495 &cinode->vfs_inode);
2496 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
2497 cinode->oplock = CIFS_CACHE_RW_FLG;
2498 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
2499 &cinode->vfs_inode);
2500 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
2501 cinode->oplock = CIFS_CACHE_READ_FLG;
2502 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
2503 &cinode->vfs_inode);
2509 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2510 unsigned int epoch, bool *purge_cache)
2512 char message[5] = {0};
2515 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
2519 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
2520 cinode->oplock |= CIFS_CACHE_READ_FLG;
2521 strcat(message, "R");
2523 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
2524 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
2525 strcat(message, "H");
2527 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
2528 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
2529 strcat(message, "W");
2531 if (!cinode->oplock)
2532 strcat(message, "None");
2533 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
2534 &cinode->vfs_inode);
2538 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2539 unsigned int epoch, bool *purge_cache)
2541 unsigned int old_oplock = cinode->oplock;
2543 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
2546 *purge_cache = false;
2547 if (old_oplock == CIFS_CACHE_READ_FLG) {
2548 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
2549 (epoch - cinode->epoch > 0))
2550 *purge_cache = true;
2551 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2552 (epoch - cinode->epoch > 1))
2553 *purge_cache = true;
2554 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2555 (epoch - cinode->epoch > 1))
2556 *purge_cache = true;
2557 else if (cinode->oplock == 0 &&
2558 (epoch - cinode->epoch > 0))
2559 *purge_cache = true;
2560 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
2561 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2562 (epoch - cinode->epoch > 0))
2563 *purge_cache = true;
2564 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2565 (epoch - cinode->epoch > 1))
2566 *purge_cache = true;
2568 cinode->epoch = epoch;
2573 smb2_is_read_op(__u32 oplock)
2575 return oplock == SMB2_OPLOCK_LEVEL_II;
2579 smb21_is_read_op(__u32 oplock)
2581 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
2582 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
2586 map_oplock_to_lease(u8 oplock)
2588 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
2589 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
2590 else if (oplock == SMB2_OPLOCK_LEVEL_II)
2591 return SMB2_LEASE_READ_CACHING;
2592 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
2593 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
2594 SMB2_LEASE_WRITE_CACHING;
2599 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
2601 struct create_lease *buf;
2603 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
2607 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
2608 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2610 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2611 (struct create_lease, lcontext));
2612 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
2613 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2614 (struct create_lease, Name));
2615 buf->ccontext.NameLength = cpu_to_le16(4);
2616 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
2625 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
2627 struct create_lease_v2 *buf;
2629 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
2633 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
2634 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2636 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2637 (struct create_lease_v2, lcontext));
2638 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
2639 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2640 (struct create_lease_v2, Name));
2641 buf->ccontext.NameLength = cpu_to_le16(4);
2642 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
2651 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
2653 struct create_lease *lc = (struct create_lease *)buf;
2655 *epoch = 0; /* not used */
2656 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2657 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2658 return le32_to_cpu(lc->lcontext.LeaseState);
2662 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
2664 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
2666 *epoch = le16_to_cpu(lc->lcontext.Epoch);
2667 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2668 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2670 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
2671 return le32_to_cpu(lc->lcontext.LeaseState);
2675 smb2_wp_retry_size(struct inode *inode)
2677 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
2678 SMB2_MAX_BUFFER_SIZE);
2682 smb2_dir_needs_close(struct cifsFileInfo *cfile)
2684 return !cfile->invalidHandle;
2688 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
2689 struct smb_rqst *old_rq)
2691 struct smb2_sync_hdr *shdr =
2692 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
2694 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
2695 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
2696 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
2697 tr_hdr->Flags = cpu_to_le16(0x01);
2698 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2699 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
2702 /* We can not use the normal sg_set_buf() as we will sometimes pass a
2703 * stack object as buf.
2705 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
2706 unsigned int buflen)
2708 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
2711 /* Assumes the first rqst has a transform header as the first iov.
2713 * rqst[0].rq_iov[0] is transform header
2714 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
2715 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
2717 static struct scatterlist *
2718 init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
2720 unsigned int sg_len;
2721 struct scatterlist *sg;
2724 unsigned int idx = 0;
2728 for (i = 0; i < num_rqst; i++)
2729 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
2731 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
2735 sg_init_table(sg, sg_len);
2736 for (i = 0; i < num_rqst; i++) {
2737 for (j = 0; j < rqst[i].rq_nvec; j++) {
2739 * The first rqst has a transform header where the
2740 * first 20 bytes are not part of the encrypted blob
2742 skip = (i == 0) && (j == 0) ? 20 : 0;
2743 smb2_sg_set_buf(&sg[idx++],
2744 rqst[i].rq_iov[j].iov_base + skip,
2745 rqst[i].rq_iov[j].iov_len - skip);
2748 for (j = 0; j < rqst[i].rq_npages; j++) {
2749 unsigned int len, offset;
2751 rqst_page_get_length(&rqst[i], j, &len, &offset);
2752 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
2755 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
2760 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
2762 struct cifs_ses *ses;
2765 spin_lock(&cifs_tcp_ses_lock);
2766 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2767 if (ses->Suid != ses_id)
2769 ses_enc_key = enc ? ses->smb3encryptionkey :
2770 ses->smb3decryptionkey;
2771 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
2772 spin_unlock(&cifs_tcp_ses_lock);
2775 spin_unlock(&cifs_tcp_ses_lock);
2780 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
2781 * iov[0] - transform header (associate data),
2782 * iov[1-N] - SMB2 header and pages - data to encrypt.
2783 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
2787 crypt_message(struct TCP_Server_Info *server, int num_rqst,
2788 struct smb_rqst *rqst, int enc)
2790 struct smb2_transform_hdr *tr_hdr =
2791 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
2792 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
2794 struct scatterlist *sg;
2795 u8 sign[SMB2_SIGNATURE_SIZE] = {};
2796 u8 key[SMB3_SIGN_KEY_SIZE];
2797 struct aead_request *req;
2799 unsigned int iv_len;
2800 DECLARE_CRYPTO_WAIT(wait);
2801 struct crypto_aead *tfm;
2802 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2804 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
2806 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
2811 rc = smb3_crypto_aead_allocate(server);
2813 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
2817 tfm = enc ? server->secmech.ccmaesencrypt :
2818 server->secmech.ccmaesdecrypt;
2819 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
2821 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
2825 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
2827 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
2831 req = aead_request_alloc(tfm, GFP_KERNEL);
2833 cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
2838 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
2839 crypt_len += SMB2_SIGNATURE_SIZE;
2842 sg = init_sg(num_rqst, rqst, sign);
2844 cifs_dbg(VFS, "%s: Failed to init sg", __func__);
2849 iv_len = crypto_aead_ivsize(tfm);
2850 iv = kzalloc(iv_len, GFP_KERNEL);
2852 cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
2857 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2859 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
2860 aead_request_set_ad(req, assoc_data_len);
2862 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2863 crypto_req_done, &wait);
2865 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
2866 : crypto_aead_decrypt(req), &wait);
2869 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
2880 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
2884 for (i = 0; i < num_rqst; i++) {
2885 if (rqst[i].rq_pages) {
2886 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
2887 put_page(rqst[i].rq_pages[j]);
2888 kfree(rqst[i].rq_pages);
2894 * This function will initialize new_rq and encrypt the content.
2895 * The first entry, new_rq[0], only contains a single iov which contains
2896 * a smb2_transform_hdr and is pre-allocated by the caller.
2897 * This function then populates new_rq[1+] with the content from olq_rq[0+].
2899 * The end result is an array of smb_rqst structures where the first structure
2900 * only contains a single iov for the transform header which we then can pass
2901 * to crypt_message().
2903 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
2904 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
2907 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
2908 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
2910 struct page **pages;
2911 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
2912 unsigned int npages;
2913 unsigned int orig_len = 0;
2917 for (i = 1; i < num_rqst; i++) {
2918 npages = old_rq[i - 1].rq_npages;
2919 pages = kmalloc_array(npages, sizeof(struct page *),
2924 new_rq[i].rq_pages = pages;
2925 new_rq[i].rq_npages = npages;
2926 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
2927 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
2928 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
2929 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
2930 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
2932 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
2934 for (j = 0; j < npages; j++) {
2935 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2940 /* copy pages form the old */
2941 for (j = 0; j < npages; j++) {
2943 unsigned int offset, len;
2945 rqst_page_get_length(&new_rq[i], j, &len, &offset);
2947 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
2948 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
2950 memcpy(dst, src, len);
2951 kunmap(new_rq[i].rq_pages[j]);
2952 kunmap(old_rq[i - 1].rq_pages[j]);
2956 /* fill the 1st iov with a transform header */
2957 fill_transform_hdr(tr_hdr, orig_len, old_rq);
2959 rc = crypt_message(server, num_rqst, new_rq, 1);
2960 cifs_dbg(FYI, "encrypt message returned %d", rc);
2967 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
2972 smb3_is_transform_hdr(void *buf)
2974 struct smb2_transform_hdr *trhdr = buf;
2976 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
2980 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
2981 unsigned int buf_data_size, struct page **pages,
2982 unsigned int npages, unsigned int page_data_size)
2985 struct smb_rqst rqst = {NULL};
2988 iov[0].iov_base = buf;
2989 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
2990 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
2991 iov[1].iov_len = buf_data_size;
2995 rqst.rq_pages = pages;
2996 rqst.rq_npages = npages;
2997 rqst.rq_pagesz = PAGE_SIZE;
2998 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
3000 rc = crypt_message(server, 1, &rqst, 0);
3001 cifs_dbg(FYI, "decrypt message returned %d\n", rc);
3006 memmove(buf, iov[1].iov_base, buf_data_size);
3008 server->total_read = buf_data_size + page_data_size;
3014 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
3015 unsigned int npages, unsigned int len)
3020 for (i = 0; i < npages; i++) {
3021 struct page *page = pages[i];
3025 if (len >= PAGE_SIZE) {
3026 /* enough data to fill the page */
3030 zero_user(page, len, PAGE_SIZE - len);
3033 length = cifs_read_page_from_socket(server, page, 0, n);
3036 server->total_read += length;
3043 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
3044 unsigned int cur_off, struct bio_vec **page_vec)
3046 struct bio_vec *bvec;
3049 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
3053 for (i = 0; i < npages; i++) {
3054 bvec[i].bv_page = pages[i];
3055 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
3056 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
3057 data_size -= bvec[i].bv_len;
3060 if (data_size != 0) {
3061 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
3071 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
3072 char *buf, unsigned int buf_len, struct page **pages,
3073 unsigned int npages, unsigned int page_data_size)
3075 unsigned int data_offset;
3076 unsigned int data_len;
3077 unsigned int cur_off;
3078 unsigned int cur_page_idx;
3079 unsigned int pad_len;
3080 struct cifs_readdata *rdata = mid->callback_data;
3081 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
3082 struct bio_vec *bvec = NULL;
3083 struct iov_iter iter;
3086 bool use_rdma_mr = false;
3088 if (shdr->Command != SMB2_READ) {
3089 cifs_dbg(VFS, "only big read responses are supported\n");
3093 if (server->ops->is_session_expired &&
3094 server->ops->is_session_expired(buf)) {
3095 cifs_reconnect(server);
3096 wake_up(&server->response_q);
3100 if (server->ops->is_status_pending &&
3101 server->ops->is_status_pending(buf, server, 0))
3104 rdata->result = server->ops->map_error(buf, false);
3105 if (rdata->result != 0) {
3106 cifs_dbg(FYI, "%s: server returned error %d\n",
3107 __func__, rdata->result);
3108 dequeue_mid(mid, rdata->result);
3112 data_offset = server->ops->read_data_offset(buf);
3113 #ifdef CONFIG_CIFS_SMB_DIRECT
3114 use_rdma_mr = rdata->mr;
3116 data_len = server->ops->read_data_length(buf, use_rdma_mr);
3118 if (data_offset < server->vals->read_rsp_size) {
3120 * win2k8 sometimes sends an offset of 0 when the read
3121 * is beyond the EOF. Treat it as if the data starts just after
3124 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
3125 __func__, data_offset);
3126 data_offset = server->vals->read_rsp_size;
3127 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
3128 /* data_offset is beyond the end of smallbuf */
3129 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
3130 __func__, data_offset);
3131 rdata->result = -EIO;
3132 dequeue_mid(mid, rdata->result);
3136 pad_len = data_offset - server->vals->read_rsp_size;
3138 if (buf_len <= data_offset) {
3139 /* read response payload is in pages */
3140 cur_page_idx = pad_len / PAGE_SIZE;
3141 cur_off = pad_len % PAGE_SIZE;
3143 if (cur_page_idx != 0) {
3144 /* data offset is beyond the 1st page of response */
3145 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
3146 __func__, data_offset);
3147 rdata->result = -EIO;
3148 dequeue_mid(mid, rdata->result);
3152 if (data_len > page_data_size - pad_len) {
3153 /* data_len is corrupt -- discard frame */
3154 rdata->result = -EIO;
3155 dequeue_mid(mid, rdata->result);
3159 rdata->result = init_read_bvec(pages, npages, page_data_size,
3161 if (rdata->result != 0) {
3162 dequeue_mid(mid, rdata->result);
3166 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
3167 } else if (buf_len >= data_offset + data_len) {
3168 /* read response payload is in buf */
3169 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
3170 iov.iov_base = buf + data_offset;
3171 iov.iov_len = data_len;
3172 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
3174 /* read response payload cannot be in both buf and pages */
3175 WARN_ONCE(1, "buf can not contain only a part of read data");
3176 rdata->result = -EIO;
3177 dequeue_mid(mid, rdata->result);
3181 /* set up first iov for signature check */
3182 rdata->iov[0].iov_base = buf;
3183 rdata->iov[0].iov_len = 4;
3184 rdata->iov[1].iov_base = buf + 4;
3185 rdata->iov[1].iov_len = server->vals->read_rsp_size - 4;
3186 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
3187 rdata->iov[0].iov_base, server->vals->read_rsp_size);
3189 length = rdata->copy_into_pages(server, rdata, &iter);
3196 dequeue_mid(mid, false);
3201 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
3203 char *buf = server->smallbuf;
3204 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3205 unsigned int npages;
3206 struct page **pages;
3208 unsigned int buflen = server->pdu_size;
3212 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
3213 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
3215 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
3218 server->total_read += rc;
3220 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
3221 server->vals->read_rsp_size;
3222 npages = DIV_ROUND_UP(len, PAGE_SIZE);
3224 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
3230 for (; i < npages; i++) {
3231 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3238 /* read read data into pages */
3239 rc = read_data_into_pages(server, pages, npages, len);
3243 rc = cifs_discard_remaining_data(server);
3247 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
3248 pages, npages, len);
3252 *mid = smb2_find_mid(server, buf);
3254 cifs_dbg(FYI, "mid not found\n");
3256 cifs_dbg(FYI, "mid found\n");
3257 (*mid)->decrypted = true;
3258 rc = handle_read_data(server, *mid, buf,
3259 server->vals->read_rsp_size,
3260 pages, npages, len);
3264 for (i = i - 1; i >= 0; i--)
3269 cifs_discard_remaining_data(server);
3274 receive_encrypted_standard(struct TCP_Server_Info *server,
3275 struct mid_q_entry **mids, char **bufs,
3279 char *buf = server->smallbuf;
3281 struct smb2_sync_hdr *shdr;
3282 unsigned int pdu_length = server->pdu_size;
3283 unsigned int buf_size;
3284 struct mid_q_entry *mid_entry;
3286 char *next_buffer = NULL;
3290 /* switch to large buffer if too big for a small one */
3291 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
3292 server->large_buf = true;
3293 memcpy(server->bigbuf, buf, server->total_read);
3294 buf = server->bigbuf;
3297 /* now read the rest */
3298 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
3299 pdu_length - HEADER_SIZE(server) + 1);
3302 server->total_read += length;
3304 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
3305 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
3309 next_is_large = server->large_buf;
3311 shdr = (struct smb2_sync_hdr *)buf;
3312 if (shdr->NextCommand) {
3313 if (next_is_large) {
3314 tmpbuf = server->bigbuf;
3315 next_buffer = (char *)cifs_buf_get();
3317 tmpbuf = server->smallbuf;
3318 next_buffer = (char *)cifs_small_buf_get();
3321 tmpbuf + le32_to_cpu(shdr->NextCommand),
3322 pdu_length - le32_to_cpu(shdr->NextCommand));
3325 mid_entry = smb2_find_mid(server, buf);
3326 if (mid_entry == NULL)
3327 cifs_dbg(FYI, "mid not found\n");
3329 cifs_dbg(FYI, "mid found\n");
3330 mid_entry->decrypted = true;
3331 mid_entry->resp_buf_size = server->pdu_size;
3334 if (*num_mids >= MAX_COMPOUND) {
3335 cifs_dbg(VFS, "too many PDUs in compound\n");
3338 bufs[*num_mids] = buf;
3339 mids[(*num_mids)++] = mid_entry;
3341 if (mid_entry && mid_entry->handle)
3342 ret = mid_entry->handle(server, mid_entry);
3344 ret = cifs_handle_standard(server, mid_entry);
3346 if (ret == 0 && shdr->NextCommand) {
3347 pdu_length -= le32_to_cpu(shdr->NextCommand);
3348 server->large_buf = next_is_large;
3350 server->bigbuf = next_buffer;
3352 server->smallbuf = next_buffer;
3354 buf += le32_to_cpu(shdr->NextCommand);
3362 smb3_receive_transform(struct TCP_Server_Info *server,
3363 struct mid_q_entry **mids, char **bufs, int *num_mids)
3365 char *buf = server->smallbuf;
3366 unsigned int pdu_length = server->pdu_size;
3367 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3368 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3370 if (pdu_length < sizeof(struct smb2_transform_hdr) +
3371 sizeof(struct smb2_sync_hdr)) {
3372 cifs_dbg(VFS, "Transform message is too small (%u)\n",
3374 cifs_reconnect(server);
3375 wake_up(&server->response_q);
3376 return -ECONNABORTED;
3379 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
3380 cifs_dbg(VFS, "Transform message is broken\n");
3381 cifs_reconnect(server);
3382 wake_up(&server->response_q);
3383 return -ECONNABORTED;
3386 /* TODO: add support for compounds containing READ. */
3387 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server))
3388 return receive_encrypted_read(server, &mids[0]);
3390 return receive_encrypted_standard(server, mids, bufs, num_mids);
3394 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
3396 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
3398 return handle_read_data(server, mid, buf, server->pdu_size,
3403 smb2_next_header(char *buf)
3405 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
3406 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
3408 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
3409 return sizeof(struct smb2_transform_hdr) +
3410 le32_to_cpu(t_hdr->OriginalMessageSize);
3412 return le32_to_cpu(hdr->NextCommand);
3415 struct smb_version_operations smb20_operations = {
3416 .compare_fids = smb2_compare_fids,
3417 .setup_request = smb2_setup_request,
3418 .setup_async_request = smb2_setup_async_request,
3419 .check_receive = smb2_check_receive,
3420 .add_credits = smb2_add_credits,
3421 .set_credits = smb2_set_credits,
3422 .get_credits_field = smb2_get_credits_field,
3423 .get_credits = smb2_get_credits,
3424 .wait_mtu_credits = cifs_wait_mtu_credits,
3425 .get_next_mid = smb2_get_next_mid,
3426 .read_data_offset = smb2_read_data_offset,
3427 .read_data_length = smb2_read_data_length,
3428 .map_error = map_smb2_to_linux_error,
3429 .find_mid = smb2_find_mid,
3430 .check_message = smb2_check_message,
3431 .dump_detail = smb2_dump_detail,
3432 .clear_stats = smb2_clear_stats,
3433 .print_stats = smb2_print_stats,
3434 .is_oplock_break = smb2_is_valid_oplock_break,
3435 .handle_cancelled_mid = smb2_handle_cancelled_mid,
3436 .downgrade_oplock = smb2_downgrade_oplock,
3437 .need_neg = smb2_need_neg,
3438 .negotiate = smb2_negotiate,
3439 .negotiate_wsize = smb2_negotiate_wsize,
3440 .negotiate_rsize = smb2_negotiate_rsize,
3441 .sess_setup = SMB2_sess_setup,
3442 .logoff = SMB2_logoff,
3443 .tree_connect = SMB2_tcon,
3444 .tree_disconnect = SMB2_tdis,
3445 .qfs_tcon = smb2_qfs_tcon,
3446 .is_path_accessible = smb2_is_path_accessible,
3447 .can_echo = smb2_can_echo,
3449 .query_path_info = smb2_query_path_info,
3450 .get_srv_inum = smb2_get_srv_inum,
3451 .query_file_info = smb2_query_file_info,
3452 .set_path_size = smb2_set_path_size,
3453 .set_file_size = smb2_set_file_size,
3454 .set_file_info = smb2_set_file_info,
3455 .set_compression = smb2_set_compression,
3456 .mkdir = smb2_mkdir,
3457 .mkdir_setinfo = smb2_mkdir_setinfo,
3458 .rmdir = smb2_rmdir,
3459 .unlink = smb2_unlink,
3460 .rename = smb2_rename_path,
3461 .create_hardlink = smb2_create_hardlink,
3462 .query_symlink = smb2_query_symlink,
3463 .query_mf_symlink = smb3_query_mf_symlink,
3464 .create_mf_symlink = smb3_create_mf_symlink,
3465 .open = smb2_open_file,
3466 .set_fid = smb2_set_fid,
3467 .close = smb2_close_file,
3468 .flush = smb2_flush_file,
3469 .async_readv = smb2_async_readv,
3470 .async_writev = smb2_async_writev,
3471 .sync_read = smb2_sync_read,
3472 .sync_write = smb2_sync_write,
3473 .query_dir_first = smb2_query_dir_first,
3474 .query_dir_next = smb2_query_dir_next,
3475 .close_dir = smb2_close_dir,
3476 .calc_smb_size = smb2_calc_size,
3477 .is_status_pending = smb2_is_status_pending,
3478 .is_session_expired = smb2_is_session_expired,
3479 .oplock_response = smb2_oplock_response,
3480 .queryfs = smb2_queryfs,
3481 .mand_lock = smb2_mand_lock,
3482 .mand_unlock_range = smb2_unlock_range,
3483 .push_mand_locks = smb2_push_mandatory_locks,
3484 .get_lease_key = smb2_get_lease_key,
3485 .set_lease_key = smb2_set_lease_key,
3486 .new_lease_key = smb2_new_lease_key,
3487 .calc_signature = smb2_calc_signature,
3488 .is_read_op = smb2_is_read_op,
3489 .set_oplock_level = smb2_set_oplock_level,
3490 .create_lease_buf = smb2_create_lease_buf,
3491 .parse_lease_buf = smb2_parse_lease_buf,
3492 .copychunk_range = smb2_copychunk_range,
3493 .wp_retry_size = smb2_wp_retry_size,
3494 .dir_needs_close = smb2_dir_needs_close,
3495 .get_dfs_refer = smb2_get_dfs_refer,
3496 .select_sectype = smb2_select_sectype,
3497 #ifdef CONFIG_CIFS_XATTR
3498 .query_all_EAs = smb2_query_eas,
3499 .set_EA = smb2_set_ea,
3500 #endif /* CIFS_XATTR */
3501 #ifdef CONFIG_CIFS_ACL
3502 .get_acl = get_smb2_acl,
3503 .get_acl_by_fid = get_smb2_acl_by_fid,
3504 .set_acl = set_smb2_acl,
3505 #endif /* CIFS_ACL */
3506 .next_header = smb2_next_header,
3507 .ioctl_query_info = smb2_ioctl_query_info,
3510 struct smb_version_operations smb21_operations = {
3511 .compare_fids = smb2_compare_fids,
3512 .setup_request = smb2_setup_request,
3513 .setup_async_request = smb2_setup_async_request,
3514 .check_receive = smb2_check_receive,
3515 .add_credits = smb2_add_credits,
3516 .set_credits = smb2_set_credits,
3517 .get_credits_field = smb2_get_credits_field,
3518 .get_credits = smb2_get_credits,
3519 .wait_mtu_credits = smb2_wait_mtu_credits,
3520 .get_next_mid = smb2_get_next_mid,
3521 .read_data_offset = smb2_read_data_offset,
3522 .read_data_length = smb2_read_data_length,
3523 .map_error = map_smb2_to_linux_error,
3524 .find_mid = smb2_find_mid,
3525 .check_message = smb2_check_message,
3526 .dump_detail = smb2_dump_detail,
3527 .clear_stats = smb2_clear_stats,
3528 .print_stats = smb2_print_stats,
3529 .is_oplock_break = smb2_is_valid_oplock_break,
3530 .handle_cancelled_mid = smb2_handle_cancelled_mid,
3531 .downgrade_oplock = smb2_downgrade_oplock,
3532 .need_neg = smb2_need_neg,
3533 .negotiate = smb2_negotiate,
3534 .negotiate_wsize = smb2_negotiate_wsize,
3535 .negotiate_rsize = smb2_negotiate_rsize,
3536 .sess_setup = SMB2_sess_setup,
3537 .logoff = SMB2_logoff,
3538 .tree_connect = SMB2_tcon,
3539 .tree_disconnect = SMB2_tdis,
3540 .qfs_tcon = smb2_qfs_tcon,
3541 .is_path_accessible = smb2_is_path_accessible,
3542 .can_echo = smb2_can_echo,
3544 .query_path_info = smb2_query_path_info,
3545 .get_srv_inum = smb2_get_srv_inum,
3546 .query_file_info = smb2_query_file_info,
3547 .set_path_size = smb2_set_path_size,
3548 .set_file_size = smb2_set_file_size,
3549 .set_file_info = smb2_set_file_info,
3550 .set_compression = smb2_set_compression,
3551 .mkdir = smb2_mkdir,
3552 .mkdir_setinfo = smb2_mkdir_setinfo,
3553 .rmdir = smb2_rmdir,
3554 .unlink = smb2_unlink,
3555 .rename = smb2_rename_path,
3556 .create_hardlink = smb2_create_hardlink,
3557 .query_symlink = smb2_query_symlink,
3558 .query_mf_symlink = smb3_query_mf_symlink,
3559 .create_mf_symlink = smb3_create_mf_symlink,
3560 .open = smb2_open_file,
3561 .set_fid = smb2_set_fid,
3562 .close = smb2_close_file,
3563 .flush = smb2_flush_file,
3564 .async_readv = smb2_async_readv,
3565 .async_writev = smb2_async_writev,
3566 .sync_read = smb2_sync_read,
3567 .sync_write = smb2_sync_write,
3568 .query_dir_first = smb2_query_dir_first,
3569 .query_dir_next = smb2_query_dir_next,
3570 .close_dir = smb2_close_dir,
3571 .calc_smb_size = smb2_calc_size,
3572 .is_status_pending = smb2_is_status_pending,
3573 .is_session_expired = smb2_is_session_expired,
3574 .oplock_response = smb2_oplock_response,
3575 .queryfs = smb2_queryfs,
3576 .mand_lock = smb2_mand_lock,
3577 .mand_unlock_range = smb2_unlock_range,
3578 .push_mand_locks = smb2_push_mandatory_locks,
3579 .get_lease_key = smb2_get_lease_key,
3580 .set_lease_key = smb2_set_lease_key,
3581 .new_lease_key = smb2_new_lease_key,
3582 .calc_signature = smb2_calc_signature,
3583 .is_read_op = smb21_is_read_op,
3584 .set_oplock_level = smb21_set_oplock_level,
3585 .create_lease_buf = smb2_create_lease_buf,
3586 .parse_lease_buf = smb2_parse_lease_buf,
3587 .copychunk_range = smb2_copychunk_range,
3588 .wp_retry_size = smb2_wp_retry_size,
3589 .dir_needs_close = smb2_dir_needs_close,
3590 .enum_snapshots = smb3_enum_snapshots,
3591 .get_dfs_refer = smb2_get_dfs_refer,
3592 .select_sectype = smb2_select_sectype,
3593 #ifdef CONFIG_CIFS_XATTR
3594 .query_all_EAs = smb2_query_eas,
3595 .set_EA = smb2_set_ea,
3596 #endif /* CIFS_XATTR */
3597 #ifdef CONFIG_CIFS_ACL
3598 .get_acl = get_smb2_acl,
3599 .get_acl_by_fid = get_smb2_acl_by_fid,
3600 .set_acl = set_smb2_acl,
3601 #endif /* CIFS_ACL */
3602 .next_header = smb2_next_header,
3603 .ioctl_query_info = smb2_ioctl_query_info,
3606 struct smb_version_operations smb30_operations = {
3607 .compare_fids = smb2_compare_fids,
3608 .setup_request = smb2_setup_request,
3609 .setup_async_request = smb2_setup_async_request,
3610 .check_receive = smb2_check_receive,
3611 .add_credits = smb2_add_credits,
3612 .set_credits = smb2_set_credits,
3613 .get_credits_field = smb2_get_credits_field,
3614 .get_credits = smb2_get_credits,
3615 .wait_mtu_credits = smb2_wait_mtu_credits,
3616 .get_next_mid = smb2_get_next_mid,
3617 .read_data_offset = smb2_read_data_offset,
3618 .read_data_length = smb2_read_data_length,
3619 .map_error = map_smb2_to_linux_error,
3620 .find_mid = smb2_find_mid,
3621 .check_message = smb2_check_message,
3622 .dump_detail = smb2_dump_detail,
3623 .clear_stats = smb2_clear_stats,
3624 .print_stats = smb2_print_stats,
3625 .dump_share_caps = smb2_dump_share_caps,
3626 .is_oplock_break = smb2_is_valid_oplock_break,
3627 .handle_cancelled_mid = smb2_handle_cancelled_mid,
3628 .downgrade_oplock = smb2_downgrade_oplock,
3629 .need_neg = smb2_need_neg,
3630 .negotiate = smb2_negotiate,
3631 .negotiate_wsize = smb3_negotiate_wsize,
3632 .negotiate_rsize = smb3_negotiate_rsize,
3633 .sess_setup = SMB2_sess_setup,
3634 .logoff = SMB2_logoff,
3635 .tree_connect = SMB2_tcon,
3636 .tree_disconnect = SMB2_tdis,
3637 .qfs_tcon = smb3_qfs_tcon,
3638 .is_path_accessible = smb2_is_path_accessible,
3639 .can_echo = smb2_can_echo,
3641 .query_path_info = smb2_query_path_info,
3642 .get_srv_inum = smb2_get_srv_inum,
3643 .query_file_info = smb2_query_file_info,
3644 .set_path_size = smb2_set_path_size,
3645 .set_file_size = smb2_set_file_size,
3646 .set_file_info = smb2_set_file_info,
3647 .set_compression = smb2_set_compression,
3648 .mkdir = smb2_mkdir,
3649 .mkdir_setinfo = smb2_mkdir_setinfo,
3650 .rmdir = smb2_rmdir,
3651 .unlink = smb2_unlink,
3652 .rename = smb2_rename_path,
3653 .create_hardlink = smb2_create_hardlink,
3654 .query_symlink = smb2_query_symlink,
3655 .query_mf_symlink = smb3_query_mf_symlink,
3656 .create_mf_symlink = smb3_create_mf_symlink,
3657 .open = smb2_open_file,
3658 .set_fid = smb2_set_fid,
3659 .close = smb2_close_file,
3660 .flush = smb2_flush_file,
3661 .async_readv = smb2_async_readv,
3662 .async_writev = smb2_async_writev,
3663 .sync_read = smb2_sync_read,
3664 .sync_write = smb2_sync_write,
3665 .query_dir_first = smb2_query_dir_first,
3666 .query_dir_next = smb2_query_dir_next,
3667 .close_dir = smb2_close_dir,
3668 .calc_smb_size = smb2_calc_size,
3669 .is_status_pending = smb2_is_status_pending,
3670 .is_session_expired = smb2_is_session_expired,
3671 .oplock_response = smb2_oplock_response,
3672 .queryfs = smb2_queryfs,
3673 .mand_lock = smb2_mand_lock,
3674 .mand_unlock_range = smb2_unlock_range,
3675 .push_mand_locks = smb2_push_mandatory_locks,
3676 .get_lease_key = smb2_get_lease_key,
3677 .set_lease_key = smb2_set_lease_key,
3678 .new_lease_key = smb2_new_lease_key,
3679 .generate_signingkey = generate_smb30signingkey,
3680 .calc_signature = smb3_calc_signature,
3681 .set_integrity = smb3_set_integrity,
3682 .is_read_op = smb21_is_read_op,
3683 .set_oplock_level = smb3_set_oplock_level,
3684 .create_lease_buf = smb3_create_lease_buf,
3685 .parse_lease_buf = smb3_parse_lease_buf,
3686 .copychunk_range = smb2_copychunk_range,
3687 .duplicate_extents = smb2_duplicate_extents,
3688 .validate_negotiate = smb3_validate_negotiate,
3689 .wp_retry_size = smb2_wp_retry_size,
3690 .dir_needs_close = smb2_dir_needs_close,
3691 .fallocate = smb3_fallocate,
3692 .enum_snapshots = smb3_enum_snapshots,
3693 .init_transform_rq = smb3_init_transform_rq,
3694 .is_transform_hdr = smb3_is_transform_hdr,
3695 .receive_transform = smb3_receive_transform,
3696 .get_dfs_refer = smb2_get_dfs_refer,
3697 .select_sectype = smb2_select_sectype,
3698 #ifdef CONFIG_CIFS_XATTR
3699 .query_all_EAs = smb2_query_eas,
3700 .set_EA = smb2_set_ea,
3701 #endif /* CIFS_XATTR */
3702 #ifdef CONFIG_CIFS_ACL
3703 .get_acl = get_smb2_acl,
3704 .get_acl_by_fid = get_smb2_acl_by_fid,
3705 .set_acl = set_smb2_acl,
3706 #endif /* CIFS_ACL */
3707 .next_header = smb2_next_header,
3708 .ioctl_query_info = smb2_ioctl_query_info,
3711 struct smb_version_operations smb311_operations = {
3712 .compare_fids = smb2_compare_fids,
3713 .setup_request = smb2_setup_request,
3714 .setup_async_request = smb2_setup_async_request,
3715 .check_receive = smb2_check_receive,
3716 .add_credits = smb2_add_credits,
3717 .set_credits = smb2_set_credits,
3718 .get_credits_field = smb2_get_credits_field,
3719 .get_credits = smb2_get_credits,
3720 .wait_mtu_credits = smb2_wait_mtu_credits,
3721 .get_next_mid = smb2_get_next_mid,
3722 .read_data_offset = smb2_read_data_offset,
3723 .read_data_length = smb2_read_data_length,
3724 .map_error = map_smb2_to_linux_error,
3725 .find_mid = smb2_find_mid,
3726 .check_message = smb2_check_message,
3727 .dump_detail = smb2_dump_detail,
3728 .clear_stats = smb2_clear_stats,
3729 .print_stats = smb2_print_stats,
3730 .dump_share_caps = smb2_dump_share_caps,
3731 .is_oplock_break = smb2_is_valid_oplock_break,
3732 .handle_cancelled_mid = smb2_handle_cancelled_mid,
3733 .downgrade_oplock = smb2_downgrade_oplock,
3734 .need_neg = smb2_need_neg,
3735 .negotiate = smb2_negotiate,
3736 .negotiate_wsize = smb3_negotiate_wsize,
3737 .negotiate_rsize = smb3_negotiate_rsize,
3738 .sess_setup = SMB2_sess_setup,
3739 .logoff = SMB2_logoff,
3740 .tree_connect = SMB2_tcon,
3741 .tree_disconnect = SMB2_tdis,
3742 .qfs_tcon = smb3_qfs_tcon,
3743 .is_path_accessible = smb2_is_path_accessible,
3744 .can_echo = smb2_can_echo,
3746 .query_path_info = smb2_query_path_info,
3747 .get_srv_inum = smb2_get_srv_inum,
3748 .query_file_info = smb2_query_file_info,
3749 .set_path_size = smb2_set_path_size,
3750 .set_file_size = smb2_set_file_size,
3751 .set_file_info = smb2_set_file_info,
3752 .set_compression = smb2_set_compression,
3753 .mkdir = smb2_mkdir,
3754 .mkdir_setinfo = smb2_mkdir_setinfo,
3755 .posix_mkdir = smb311_posix_mkdir,
3756 .rmdir = smb2_rmdir,
3757 .unlink = smb2_unlink,
3758 .rename = smb2_rename_path,
3759 .create_hardlink = smb2_create_hardlink,
3760 .query_symlink = smb2_query_symlink,
3761 .query_mf_symlink = smb3_query_mf_symlink,
3762 .create_mf_symlink = smb3_create_mf_symlink,
3763 .open = smb2_open_file,
3764 .set_fid = smb2_set_fid,
3765 .close = smb2_close_file,
3766 .flush = smb2_flush_file,
3767 .async_readv = smb2_async_readv,
3768 .async_writev = smb2_async_writev,
3769 .sync_read = smb2_sync_read,
3770 .sync_write = smb2_sync_write,
3771 .query_dir_first = smb2_query_dir_first,
3772 .query_dir_next = smb2_query_dir_next,
3773 .close_dir = smb2_close_dir,
3774 .calc_smb_size = smb2_calc_size,
3775 .is_status_pending = smb2_is_status_pending,
3776 .is_session_expired = smb2_is_session_expired,
3777 .oplock_response = smb2_oplock_response,
3778 .queryfs = smb311_queryfs,
3779 .mand_lock = smb2_mand_lock,
3780 .mand_unlock_range = smb2_unlock_range,
3781 .push_mand_locks = smb2_push_mandatory_locks,
3782 .get_lease_key = smb2_get_lease_key,
3783 .set_lease_key = smb2_set_lease_key,
3784 .new_lease_key = smb2_new_lease_key,
3785 .generate_signingkey = generate_smb311signingkey,
3786 .calc_signature = smb3_calc_signature,
3787 .set_integrity = smb3_set_integrity,
3788 .is_read_op = smb21_is_read_op,
3789 .set_oplock_level = smb3_set_oplock_level,
3790 .create_lease_buf = smb3_create_lease_buf,
3791 .parse_lease_buf = smb3_parse_lease_buf,
3792 .copychunk_range = smb2_copychunk_range,
3793 .duplicate_extents = smb2_duplicate_extents,
3794 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
3795 .wp_retry_size = smb2_wp_retry_size,
3796 .dir_needs_close = smb2_dir_needs_close,
3797 .fallocate = smb3_fallocate,
3798 .enum_snapshots = smb3_enum_snapshots,
3799 .init_transform_rq = smb3_init_transform_rq,
3800 .is_transform_hdr = smb3_is_transform_hdr,
3801 .receive_transform = smb3_receive_transform,
3802 .get_dfs_refer = smb2_get_dfs_refer,
3803 .select_sectype = smb2_select_sectype,
3804 #ifdef CONFIG_CIFS_XATTR
3805 .query_all_EAs = smb2_query_eas,
3806 .set_EA = smb2_set_ea,
3807 #endif /* CIFS_XATTR */
3808 #ifdef CONFIG_CIFS_ACL
3809 .get_acl = get_smb2_acl,
3810 .get_acl_by_fid = get_smb2_acl_by_fid,
3811 .set_acl = set_smb2_acl,
3812 #endif /* CIFS_ACL */
3813 .next_header = smb2_next_header,
3814 .ioctl_query_info = smb2_ioctl_query_info,
3817 struct smb_version_values smb20_values = {
3818 .version_string = SMB20_VERSION_STRING,
3819 .protocol_id = SMB20_PROT_ID,
3820 .req_capabilities = 0, /* MBZ */
3821 .large_lock_type = 0,
3822 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3823 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3824 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3825 .header_size = sizeof(struct smb2_sync_hdr),
3826 .header_preamble_size = 0,
3827 .max_header_size = MAX_SMB2_HDR_SIZE,
3828 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3829 .lock_cmd = SMB2_LOCK,
3831 .cap_nt_find = SMB2_NT_FIND,
3832 .cap_large_files = SMB2_LARGE_FILES,
3833 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3834 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3835 .create_lease_size = sizeof(struct create_lease),
3838 struct smb_version_values smb21_values = {
3839 .version_string = SMB21_VERSION_STRING,
3840 .protocol_id = SMB21_PROT_ID,
3841 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
3842 .large_lock_type = 0,
3843 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3844 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3845 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3846 .header_size = sizeof(struct smb2_sync_hdr),
3847 .header_preamble_size = 0,
3848 .max_header_size = MAX_SMB2_HDR_SIZE,
3849 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3850 .lock_cmd = SMB2_LOCK,
3852 .cap_nt_find = SMB2_NT_FIND,
3853 .cap_large_files = SMB2_LARGE_FILES,
3854 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3855 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3856 .create_lease_size = sizeof(struct create_lease),
3859 struct smb_version_values smb3any_values = {
3860 .version_string = SMB3ANY_VERSION_STRING,
3861 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3862 .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,
3863 .large_lock_type = 0,
3864 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3865 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3866 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3867 .header_size = sizeof(struct smb2_sync_hdr),
3868 .header_preamble_size = 0,
3869 .max_header_size = MAX_SMB2_HDR_SIZE,
3870 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3871 .lock_cmd = SMB2_LOCK,
3873 .cap_nt_find = SMB2_NT_FIND,
3874 .cap_large_files = SMB2_LARGE_FILES,
3875 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3876 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3877 .create_lease_size = sizeof(struct create_lease_v2),
3880 struct smb_version_values smbdefault_values = {
3881 .version_string = SMBDEFAULT_VERSION_STRING,
3882 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3883 .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,
3884 .large_lock_type = 0,
3885 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3886 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3887 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3888 .header_size = sizeof(struct smb2_sync_hdr),
3889 .header_preamble_size = 0,
3890 .max_header_size = MAX_SMB2_HDR_SIZE,
3891 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3892 .lock_cmd = SMB2_LOCK,
3894 .cap_nt_find = SMB2_NT_FIND,
3895 .cap_large_files = SMB2_LARGE_FILES,
3896 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3897 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3898 .create_lease_size = sizeof(struct create_lease_v2),
3901 struct smb_version_values smb30_values = {
3902 .version_string = SMB30_VERSION_STRING,
3903 .protocol_id = SMB30_PROT_ID,
3904 .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,
3905 .large_lock_type = 0,
3906 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3907 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3908 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3909 .header_size = sizeof(struct smb2_sync_hdr),
3910 .header_preamble_size = 0,
3911 .max_header_size = MAX_SMB2_HDR_SIZE,
3912 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3913 .lock_cmd = SMB2_LOCK,
3915 .cap_nt_find = SMB2_NT_FIND,
3916 .cap_large_files = SMB2_LARGE_FILES,
3917 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3918 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3919 .create_lease_size = sizeof(struct create_lease_v2),
3922 struct smb_version_values smb302_values = {
3923 .version_string = SMB302_VERSION_STRING,
3924 .protocol_id = SMB302_PROT_ID,
3925 .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,
3926 .large_lock_type = 0,
3927 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3928 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3929 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3930 .header_size = sizeof(struct smb2_sync_hdr),
3931 .header_preamble_size = 0,
3932 .max_header_size = MAX_SMB2_HDR_SIZE,
3933 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3934 .lock_cmd = SMB2_LOCK,
3936 .cap_nt_find = SMB2_NT_FIND,
3937 .cap_large_files = SMB2_LARGE_FILES,
3938 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3939 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3940 .create_lease_size = sizeof(struct create_lease_v2),
3943 struct smb_version_values smb311_values = {
3944 .version_string = SMB311_VERSION_STRING,
3945 .protocol_id = SMB311_PROT_ID,
3946 .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,
3947 .large_lock_type = 0,
3948 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3949 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3950 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3951 .header_size = sizeof(struct smb2_sync_hdr),
3952 .header_preamble_size = 0,
3953 .max_header_size = MAX_SMB2_HDR_SIZE,
3954 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3955 .lock_cmd = SMB2_LOCK,
3957 .cap_nt_find = SMB2_NT_FIND,
3958 .cap_large_files = SMB2_LARGE_FILES,
3959 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3960 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3961 .create_lease_size = sizeof(struct create_lease_v2),