2 * SMB2 version specific operations
4 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
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"
37 /* Change credits for different ops and return the total number of credits */
39 change_conf(struct TCP_Server_Info *server)
41 server->credits += server->echo_credits + server->oplock_credits;
42 server->oplock_credits = server->echo_credits = 0;
43 switch (server->credits) {
47 server->echoes = false;
48 server->oplocks = false;
51 server->echoes = true;
52 server->oplocks = false;
53 server->echo_credits = 1;
56 server->echoes = true;
58 server->oplocks = true;
59 server->oplock_credits = 1;
61 server->oplocks = false;
63 server->echo_credits = 1;
65 server->credits -= server->echo_credits + server->oplock_credits;
66 return server->credits + server->echo_credits + server->oplock_credits;
70 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 if (server->tcpStatus == CifsNeedReconnect)
110 /* change_conf hasn't been executed */
113 cifs_dbg(VFS, "Possible client or server bug - zero credits\n");
116 cifs_dbg(VFS, "disabling echoes and oplocks\n");
119 cifs_dbg(FYI, "disabling oplocks\n");
122 cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
127 smb2_set_credits(struct TCP_Server_Info *server, const int val)
129 spin_lock(&server->req_lock);
130 server->credits = val;
132 server->reconnect_instance++;
133 spin_unlock(&server->req_lock);
134 /* don't log while holding the lock */
136 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
140 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
144 return &server->echo_credits;
146 return &server->oplock_credits;
148 return &server->credits;
153 smb2_get_credits(struct mid_q_entry *mid)
155 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)mid->resp_buf;
157 if (mid->mid_state == MID_RESPONSE_RECEIVED
158 || mid->mid_state == MID_RESPONSE_MALFORMED)
159 return le16_to_cpu(shdr->CreditRequest);
165 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
166 unsigned int *num, unsigned int *credits)
169 unsigned int scredits;
171 spin_lock(&server->req_lock);
173 if (server->credits <= 0) {
174 spin_unlock(&server->req_lock);
175 cifs_num_waiters_inc(server);
176 rc = wait_event_killable(server->request_q,
177 has_credits(server, &server->credits));
178 cifs_num_waiters_dec(server);
181 spin_lock(&server->req_lock);
183 if (server->tcpStatus == CifsExiting) {
184 spin_unlock(&server->req_lock);
188 scredits = server->credits;
189 /* can deadlock with reopen */
191 *num = SMB2_MAX_BUFFER_SIZE;
196 /* leave some credits for reopen and other ops */
198 *num = min_t(unsigned int, size,
199 scredits * SMB2_MAX_BUFFER_SIZE);
201 *credits = DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
202 server->credits -= *credits;
207 spin_unlock(&server->req_lock);
212 smb2_get_next_mid(struct TCP_Server_Info *server)
215 /* for SMB2 we need the current value */
216 spin_lock(&GlobalMid_Lock);
217 mid = server->CurrentMid++;
218 spin_unlock(&GlobalMid_Lock);
222 static struct mid_q_entry *
223 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
225 struct mid_q_entry *mid;
226 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
227 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
229 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
230 cifs_dbg(VFS, "encrypted frame parsing not supported yet");
234 spin_lock(&GlobalMid_Lock);
235 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
236 if ((mid->mid == wire_mid) &&
237 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
238 (mid->command == shdr->Command)) {
239 kref_get(&mid->refcount);
240 spin_unlock(&GlobalMid_Lock);
244 spin_unlock(&GlobalMid_Lock);
249 smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
251 #ifdef CONFIG_CIFS_DEBUG2
252 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
254 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
255 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
257 cifs_dbg(VFS, "smb buf %p len %u\n", buf,
258 server->ops->calc_smb_size(buf, server));
263 smb2_need_neg(struct TCP_Server_Info *server)
265 return server->max_read == 0;
269 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
272 ses->server->CurrentMid = 0;
273 rc = SMB2_negotiate(xid, ses);
274 /* BB we probably don't need to retry with modern servers */
281 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
283 struct TCP_Server_Info *server = tcon->ses->server;
286 /* start with specified wsize, or default */
287 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
288 wsize = min_t(unsigned int, wsize, server->max_write);
289 #ifdef CONFIG_CIFS_SMB_DIRECT
292 wsize = min_t(unsigned int,
293 wsize, server->smbd_conn->max_fragmented_send_size);
295 wsize = min_t(unsigned int,
296 wsize, server->smbd_conn->max_readwrite_size);
299 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
300 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
306 smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
308 struct TCP_Server_Info *server = tcon->ses->server;
311 /* start with specified wsize, or default */
312 wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
313 wsize = min_t(unsigned int, wsize, server->max_write);
314 #ifdef CONFIG_CIFS_SMB_DIRECT
317 wsize = min_t(unsigned int,
318 wsize, server->smbd_conn->max_fragmented_send_size);
320 wsize = min_t(unsigned int,
321 wsize, server->smbd_conn->max_readwrite_size);
324 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
325 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
331 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
333 struct TCP_Server_Info *server = tcon->ses->server;
336 /* start with specified rsize, or default */
337 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
338 rsize = min_t(unsigned int, rsize, server->max_read);
339 #ifdef CONFIG_CIFS_SMB_DIRECT
342 rsize = min_t(unsigned int,
343 rsize, server->smbd_conn->max_fragmented_recv_size);
345 rsize = min_t(unsigned int,
346 rsize, server->smbd_conn->max_readwrite_size);
350 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
351 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
357 smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
359 struct TCP_Server_Info *server = tcon->ses->server;
362 /* start with specified rsize, or default */
363 rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
364 rsize = min_t(unsigned int, rsize, server->max_read);
365 #ifdef CONFIG_CIFS_SMB_DIRECT
368 rsize = min_t(unsigned int,
369 rsize, server->smbd_conn->max_fragmented_recv_size);
371 rsize = min_t(unsigned int,
372 rsize, server->smbd_conn->max_readwrite_size);
376 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
377 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
383 parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
385 struct cifs_server_iface **iface_list,
388 struct network_interface_info_ioctl_rsp *p;
389 struct sockaddr_in *addr4;
390 struct sockaddr_in6 *addr6;
391 struct iface_info_ipv4 *p4;
392 struct iface_info_ipv6 *p6;
393 struct cifs_server_iface *info;
403 * Fist pass: count and sanity check
406 bytes_left = buf_len;
408 while (bytes_left >= sizeof(*p)) {
410 next = le32_to_cpu(p->Next);
412 bytes_left -= sizeof(*p);
415 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
420 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
425 if (bytes_left || p->Next)
426 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
430 * Second pass: extract info to internal structure
433 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
440 bytes_left = buf_len;
442 while (bytes_left >= sizeof(*p)) {
443 info->speed = le64_to_cpu(p->LinkSpeed);
444 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE);
445 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE);
447 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
448 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
449 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
450 le32_to_cpu(p->Capability));
454 * The kernel and wire socket structures have the same
455 * layout and use network byte order but make the
456 * conversion explicit in case either one changes.
459 addr4 = (struct sockaddr_in *)&info->sockaddr;
460 p4 = (struct iface_info_ipv4 *)p->Buffer;
461 addr4->sin_family = AF_INET;
462 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
464 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
465 addr4->sin_port = cpu_to_be16(CIFS_PORT);
467 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
471 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
472 p6 = (struct iface_info_ipv6 *)p->Buffer;
473 addr6->sin6_family = AF_INET6;
474 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
476 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
477 addr6->sin6_flowinfo = 0;
478 addr6->sin6_scope_id = 0;
479 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
481 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
486 "%s: skipping unsupported socket family\n",
494 next = le32_to_cpu(p->Next);
497 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
517 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
520 unsigned int ret_data_len = 0;
521 struct network_interface_info_ioctl_rsp *out_buf = NULL;
522 struct cifs_server_iface *iface_list;
524 struct cifs_ses *ses = tcon->ses;
526 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
527 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
528 NULL /* no data input */, 0 /* no data input */,
529 (char **)&out_buf, &ret_data_len);
530 if (rc == -EOPNOTSUPP) {
532 "server does not support query network interfaces\n");
534 } else if (rc != 0) {
535 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
539 rc = parse_server_interfaces(out_buf, ret_data_len,
540 &iface_list, &iface_count);
544 spin_lock(&ses->iface_lock);
545 kfree(ses->iface_list);
546 ses->iface_list = iface_list;
547 ses->iface_count = iface_count;
548 ses->iface_last_update = jiffies;
549 spin_unlock(&ses->iface_lock);
557 smb2_close_cached_fid(struct kref *ref)
559 struct cached_fid *cfid = container_of(ref, struct cached_fid,
562 if (cfid->is_valid) {
563 cifs_dbg(FYI, "clear cached root file handle\n");
564 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
565 cfid->fid->volatile_fid);
566 cfid->is_valid = false;
570 void close_shroot(struct cached_fid *cfid)
572 mutex_lock(&cfid->fid_mutex);
573 kref_put(&cfid->refcount, smb2_close_cached_fid);
574 mutex_unlock(&cfid->fid_mutex);
578 smb2_cached_lease_break(struct work_struct *work)
580 struct cached_fid *cfid = container_of(work,
581 struct cached_fid, lease_break);
587 * Open the directory at the root of a share
589 int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
591 struct cifs_open_parms oparams;
593 __le16 srch_path = 0; /* Null - since an open of top of share */
594 u8 oplock = SMB2_OPLOCK_LEVEL_II;
596 mutex_lock(&tcon->crfid.fid_mutex);
597 if (tcon->crfid.is_valid) {
598 cifs_dbg(FYI, "found a cached root file handle\n");
599 memcpy(pfid, tcon->crfid.fid, sizeof(struct cifs_fid));
600 kref_get(&tcon->crfid.refcount);
601 mutex_unlock(&tcon->crfid.fid_mutex);
606 oparams.create_options = 0;
607 oparams.desired_access = FILE_READ_ATTRIBUTES;
608 oparams.disposition = FILE_OPEN;
610 oparams.reconnect = false;
612 rc = SMB2_open(xid, &oparams, &srch_path, &oplock, NULL, NULL, NULL);
614 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
615 tcon->crfid.tcon = tcon;
616 tcon->crfid.is_valid = true;
617 kref_init(&tcon->crfid.refcount);
618 kref_get(&tcon->crfid.refcount);
620 mutex_unlock(&tcon->crfid.fid_mutex);
625 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
628 __le16 srch_path = 0; /* Null - open root of share */
629 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
630 struct cifs_open_parms oparms;
632 bool no_cached_open = tcon->nohandlecache;
635 oparms.desired_access = FILE_READ_ATTRIBUTES;
636 oparms.disposition = FILE_OPEN;
637 oparms.create_options = 0;
639 oparms.reconnect = false;
642 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
645 rc = open_shroot(xid, tcon, &fid);
650 SMB3_request_interfaces(xid, tcon);
652 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
653 FS_ATTRIBUTE_INFORMATION);
654 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
655 FS_DEVICE_INFORMATION);
656 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
657 FS_VOLUME_INFORMATION);
658 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
659 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
661 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
663 close_shroot(&tcon->crfid);
669 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
672 __le16 srch_path = 0; /* Null - open root of share */
673 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
674 struct cifs_open_parms oparms;
678 oparms.desired_access = FILE_READ_ATTRIBUTES;
679 oparms.disposition = FILE_OPEN;
680 oparms.create_options = 0;
682 oparms.reconnect = false;
684 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
688 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
689 FS_ATTRIBUTE_INFORMATION);
690 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
691 FS_DEVICE_INFORMATION);
692 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
697 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
698 struct cifs_sb_info *cifs_sb, const char *full_path)
702 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
703 struct cifs_open_parms oparms;
706 if ((*full_path == 0) && tcon->crfid.is_valid)
709 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
714 oparms.desired_access = FILE_READ_ATTRIBUTES;
715 oparms.disposition = FILE_OPEN;
716 if (backup_cred(cifs_sb))
717 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
719 oparms.create_options = 0;
721 oparms.reconnect = false;
723 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
729 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
735 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
736 struct cifs_sb_info *cifs_sb, const char *full_path,
737 u64 *uniqueid, FILE_ALL_INFO *data)
739 *uniqueid = le64_to_cpu(data->IndexNumber);
744 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
745 struct cifs_fid *fid, FILE_ALL_INFO *data)
748 struct smb2_file_all_info *smb2_data;
750 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
752 if (smb2_data == NULL)
755 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
758 move_smb2_info_to_cifs(data, smb2_data);
763 #ifdef CONFIG_CIFS_XATTR
765 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
766 struct smb2_file_full_ea_info *src, size_t src_size,
767 const unsigned char *ea_name)
770 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
772 size_t buf_size = dst_size;
773 size_t name_len, value_len, user_name_len;
775 while (src_size > 0) {
776 name = &src->ea_data[0];
777 name_len = (size_t)src->ea_name_length;
778 value = &src->ea_data[src->ea_name_length + 1];
779 value_len = (size_t)le16_to_cpu(src->ea_value_length);
785 if (src_size < 8 + name_len + 1 + value_len) {
786 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
792 if (ea_name_len == name_len &&
793 memcmp(ea_name, name, name_len) == 0) {
797 if (dst_size < value_len) {
801 memcpy(dst, value, value_len);
805 /* 'user.' plus a terminating null */
806 user_name_len = 5 + 1 + name_len;
809 /* skip copy - calc size only */
811 } else if (dst_size >= user_name_len) {
812 dst_size -= user_name_len;
813 memcpy(dst, "user.", 5);
815 memcpy(dst, src->ea_data, name_len);
821 /* stop before overrun buffer */
827 if (!src->next_entry_offset)
830 if (src_size < le32_to_cpu(src->next_entry_offset)) {
831 /* stop before overrun buffer */
835 src_size -= le32_to_cpu(src->next_entry_offset);
836 src = (void *)((char *)src +
837 le32_to_cpu(src->next_entry_offset));
840 /* didn't find the named attribute */
849 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
850 const unsigned char *path, const unsigned char *ea_name,
851 char *ea_data, size_t buf_size,
852 struct cifs_sb_info *cifs_sb)
856 struct kvec rsp_iov = {NULL, 0};
857 int buftype = CIFS_NO_BUFFER;
858 struct smb2_query_info_rsp *rsp;
859 struct smb2_file_full_ea_info *info = NULL;
861 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
865 rc = smb2_query_info_compound(xid, tcon, utf16_path,
867 FILE_FULL_EA_INFORMATION,
870 MAX_SMB2_CREATE_RESPONSE_SIZE -
871 MAX_SMB2_CLOSE_RESPONSE_SIZE,
872 &rsp_iov, &buftype, cifs_sb);
875 * If ea_name is NULL (listxattr) and there are no EAs,
876 * return 0 as it's not an error. Otherwise, the specified
877 * ea_name was not found.
879 if (!ea_name && rc == -ENODATA)
884 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
885 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
886 le32_to_cpu(rsp->OutputBufferLength),
888 sizeof(struct smb2_file_full_ea_info));
892 info = (struct smb2_file_full_ea_info *)(
893 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
894 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
895 le32_to_cpu(rsp->OutputBufferLength), ea_name);
899 free_rsp_buf(buftype, rsp_iov.iov_base);
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)
910 struct cifs_ses *ses = tcon->ses;
911 __le16 *utf16_path = NULL;
912 int ea_name_len = strlen(ea_name);
915 struct smb_rqst rqst[3];
917 struct kvec rsp_iov[3];
918 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
919 struct cifs_open_parms oparms;
920 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
922 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
923 unsigned int size[1];
925 struct smb2_file_full_ea_info *ea = NULL;
926 struct kvec close_iov[1];
929 if (smb3_encryption_required(tcon))
930 flags |= CIFS_TRANSFORM_REQ;
932 if (ea_name_len > 255)
935 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
939 memset(rqst, 0, sizeof(rqst));
940 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
941 memset(rsp_iov, 0, sizeof(rsp_iov));
944 memset(&open_iov, 0, sizeof(open_iov));
945 rqst[0].rq_iov = open_iov;
946 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
948 memset(&oparms, 0, sizeof(oparms));
950 oparms.desired_access = FILE_WRITE_EA;
951 oparms.disposition = FILE_OPEN;
952 if (backup_cred(cifs_sb))
953 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
955 oparms.create_options = 0;
957 oparms.reconnect = false;
959 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
962 smb2_set_next_command(tcon, &rqst[0]);
966 memset(&si_iov, 0, sizeof(si_iov));
967 rqst[1].rq_iov = si_iov;
970 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
971 ea = kzalloc(len, GFP_KERNEL);
977 ea->ea_name_length = ea_name_len;
978 ea->ea_value_length = cpu_to_le16(ea_value_len);
979 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
980 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
985 rc = SMB2_set_info_init(tcon, &rqst[1], COMPOUND_FID,
986 COMPOUND_FID, current->tgid,
987 FILE_FULL_EA_INFORMATION,
988 SMB2_O_INFO_FILE, 0, data, size);
989 smb2_set_next_command(tcon, &rqst[1]);
990 smb2_set_related(&rqst[1]);
994 memset(&close_iov, 0, sizeof(close_iov));
995 rqst[2].rq_iov = close_iov;
997 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
998 smb2_set_related(&rqst[2]);
1000 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1001 resp_buftype, rsp_iov);
1006 SMB2_open_free(&rqst[0]);
1007 SMB2_set_info_free(&rqst[1]);
1008 SMB2_close_free(&rqst[2]);
1009 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1010 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1011 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1017 smb2_can_echo(struct TCP_Server_Info *server)
1019 return server->echoes;
1023 smb2_clear_stats(struct cifs_tcon *tcon)
1026 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1027 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1028 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1033 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1035 seq_puts(m, "\n\tShare Capabilities:");
1036 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1037 seq_puts(m, " DFS,");
1038 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1039 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1040 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1041 seq_puts(m, " SCALEOUT,");
1042 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1043 seq_puts(m, " CLUSTER,");
1044 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1045 seq_puts(m, " ASYMMETRIC,");
1046 if (tcon->capabilities == 0)
1047 seq_puts(m, " None");
1048 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1049 seq_puts(m, " Aligned,");
1050 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1051 seq_puts(m, " Partition Aligned,");
1052 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1053 seq_puts(m, " SSD,");
1054 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1055 seq_puts(m, " TRIM-support,");
1057 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1058 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1059 if (tcon->perf_sector_size)
1060 seq_printf(m, "\tOptimal sector size: 0x%x",
1061 tcon->perf_sector_size);
1062 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1066 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1068 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1069 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1072 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1073 * totals (requests sent) since those SMBs are per-session not per tcon
1075 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1076 (long long)(tcon->bytes_read),
1077 (long long)(tcon->bytes_written));
1078 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1079 atomic_read(&tcon->num_local_opens),
1080 atomic_read(&tcon->num_remote_opens));
1081 seq_printf(m, "\nTreeConnects: %d total %d failed",
1082 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1083 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1084 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1085 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1086 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1087 seq_printf(m, "\nCreates: %d total %d failed",
1088 atomic_read(&sent[SMB2_CREATE_HE]),
1089 atomic_read(&failed[SMB2_CREATE_HE]));
1090 seq_printf(m, "\nCloses: %d total %d failed",
1091 atomic_read(&sent[SMB2_CLOSE_HE]),
1092 atomic_read(&failed[SMB2_CLOSE_HE]));
1093 seq_printf(m, "\nFlushes: %d total %d failed",
1094 atomic_read(&sent[SMB2_FLUSH_HE]),
1095 atomic_read(&failed[SMB2_FLUSH_HE]));
1096 seq_printf(m, "\nReads: %d total %d failed",
1097 atomic_read(&sent[SMB2_READ_HE]),
1098 atomic_read(&failed[SMB2_READ_HE]));
1099 seq_printf(m, "\nWrites: %d total %d failed",
1100 atomic_read(&sent[SMB2_WRITE_HE]),
1101 atomic_read(&failed[SMB2_WRITE_HE]));
1102 seq_printf(m, "\nLocks: %d total %d failed",
1103 atomic_read(&sent[SMB2_LOCK_HE]),
1104 atomic_read(&failed[SMB2_LOCK_HE]));
1105 seq_printf(m, "\nIOCTLs: %d total %d failed",
1106 atomic_read(&sent[SMB2_IOCTL_HE]),
1107 atomic_read(&failed[SMB2_IOCTL_HE]));
1108 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1109 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1110 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1111 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1112 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1113 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1114 seq_printf(m, "\nQueryInfos: %d total %d failed",
1115 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1116 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1117 seq_printf(m, "\nSetInfos: %d total %d failed",
1118 atomic_read(&sent[SMB2_SET_INFO_HE]),
1119 atomic_read(&failed[SMB2_SET_INFO_HE]));
1120 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1121 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1122 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1126 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1128 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1129 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1131 cfile->fid.persistent_fid = fid->persistent_fid;
1132 cfile->fid.volatile_fid = fid->volatile_fid;
1133 #ifdef CONFIG_CIFS_DEBUG2
1134 cfile->fid.mid = fid->mid;
1135 #endif /* CIFS_DEBUG2 */
1136 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1138 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1139 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1143 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1144 struct cifs_fid *fid)
1146 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1150 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1151 u64 persistent_fid, u64 volatile_fid,
1152 struct copychunk_ioctl *pcchunk)
1155 unsigned int ret_data_len;
1156 struct resume_key_req *res_key;
1158 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1159 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1160 NULL, 0 /* no input */,
1161 (char **)&res_key, &ret_data_len);
1164 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1165 goto req_res_key_exit;
1167 if (ret_data_len < sizeof(struct resume_key_req)) {
1168 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
1170 goto req_res_key_exit;
1172 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1180 smb2_ioctl_query_info(const unsigned int xid,
1181 struct cifs_tcon *tcon,
1182 __le16 *path, int is_dir,
1185 struct cifs_ses *ses = tcon->ses;
1186 char __user *arg = (char __user *)p;
1187 struct smb_query_info qi;
1188 struct smb_query_info __user *pqi;
1191 struct smb2_query_info_rsp *rsp = NULL;
1192 void *buffer = NULL;
1193 struct smb_rqst rqst[3];
1194 int resp_buftype[3];
1195 struct kvec rsp_iov[3];
1196 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1197 struct cifs_open_parms oparms;
1198 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1199 struct cifs_fid fid;
1200 struct kvec qi_iov[1];
1201 struct kvec close_iov[1];
1203 memset(rqst, 0, sizeof(rqst));
1204 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1205 memset(rsp_iov, 0, sizeof(rsp_iov));
1207 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1210 if (qi.output_buffer_length > 1024)
1213 if (!ses || !(ses->server))
1216 if (smb3_encryption_required(tcon))
1217 flags |= CIFS_TRANSFORM_REQ;
1219 buffer = kmalloc(qi.output_buffer_length, GFP_KERNEL);
1223 if (copy_from_user(buffer, arg + sizeof(struct smb_query_info),
1224 qi.output_buffer_length)) {
1230 memset(&open_iov, 0, sizeof(open_iov));
1231 rqst[0].rq_iov = open_iov;
1232 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1234 memset(&oparms, 0, sizeof(oparms));
1236 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1237 oparms.disposition = FILE_OPEN;
1239 oparms.create_options = CREATE_NOT_FILE;
1241 oparms.create_options = CREATE_NOT_DIR;
1243 oparms.reconnect = false;
1245 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, path);
1248 smb2_set_next_command(tcon, &rqst[0]);
1251 memset(&qi_iov, 0, sizeof(qi_iov));
1252 rqst[1].rq_iov = qi_iov;
1253 rqst[1].rq_nvec = 1;
1255 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1256 qi.file_info_class, qi.info_type,
1257 qi.additional_information,
1258 qi.input_buffer_length,
1259 qi.output_buffer_length, buffer);
1262 smb2_set_next_command(tcon, &rqst[1]);
1263 smb2_set_related(&rqst[1]);
1266 memset(&close_iov, 0, sizeof(close_iov));
1267 rqst[2].rq_iov = close_iov;
1268 rqst[2].rq_nvec = 1;
1270 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1273 smb2_set_related(&rqst[2]);
1275 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1276 resp_buftype, rsp_iov);
1279 pqi = (struct smb_query_info __user *)arg;
1280 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1281 if (le32_to_cpu(rsp->OutputBufferLength) < qi.input_buffer_length)
1282 qi.input_buffer_length = le32_to_cpu(rsp->OutputBufferLength);
1283 if (copy_to_user(&pqi->input_buffer_length, &qi.input_buffer_length,
1284 sizeof(qi.input_buffer_length))) {
1288 if (copy_to_user(pqi + 1, rsp->Buffer, qi.input_buffer_length)) {
1295 SMB2_open_free(&rqst[0]);
1296 SMB2_query_info_free(&rqst[1]);
1297 SMB2_close_free(&rqst[2]);
1298 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1299 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1300 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1305 smb2_copychunk_range(const unsigned int xid,
1306 struct cifsFileInfo *srcfile,
1307 struct cifsFileInfo *trgtfile, u64 src_off,
1308 u64 len, u64 dest_off)
1311 unsigned int ret_data_len;
1312 struct copychunk_ioctl *pcchunk;
1313 struct copychunk_ioctl_rsp *retbuf = NULL;
1314 struct cifs_tcon *tcon;
1315 int chunks_copied = 0;
1316 bool chunk_sizes_updated = false;
1317 ssize_t bytes_written, total_bytes_written = 0;
1319 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1321 if (pcchunk == NULL)
1324 cifs_dbg(FYI, "in smb2_copychunk_range - about to call request res key\n");
1325 /* Request a key from the server to identify the source of the copy */
1326 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1327 srcfile->fid.persistent_fid,
1328 srcfile->fid.volatile_fid, pcchunk);
1330 /* Note: request_res_key sets res_key null only if rc !=0 */
1334 /* For now array only one chunk long, will make more flexible later */
1335 pcchunk->ChunkCount = cpu_to_le32(1);
1336 pcchunk->Reserved = 0;
1337 pcchunk->Reserved2 = 0;
1339 tcon = tlink_tcon(trgtfile->tlink);
1342 pcchunk->SourceOffset = cpu_to_le64(src_off);
1343 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1345 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1347 /* Request server copy to target from src identified by key */
1348 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1349 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1350 true /* is_fsctl */, (char *)pcchunk,
1351 sizeof(struct copychunk_ioctl), (char **)&retbuf,
1355 sizeof(struct copychunk_ioctl_rsp)) {
1356 cifs_dbg(VFS, "invalid cchunk response size\n");
1360 if (retbuf->TotalBytesWritten == 0) {
1361 cifs_dbg(FYI, "no bytes copied\n");
1366 * Check if server claimed to write more than we asked
1368 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1369 le32_to_cpu(pcchunk->Length)) {
1370 cifs_dbg(VFS, "invalid copy chunk response\n");
1374 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1375 cifs_dbg(VFS, "invalid num chunks written\n");
1381 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1382 src_off += bytes_written;
1383 dest_off += bytes_written;
1384 len -= bytes_written;
1385 total_bytes_written += bytes_written;
1387 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1388 le32_to_cpu(retbuf->ChunksWritten),
1389 le32_to_cpu(retbuf->ChunkBytesWritten),
1391 } else if (rc == -EINVAL) {
1392 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1395 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1396 le32_to_cpu(retbuf->ChunksWritten),
1397 le32_to_cpu(retbuf->ChunkBytesWritten),
1398 le32_to_cpu(retbuf->TotalBytesWritten));
1401 * Check if this is the first request using these sizes,
1402 * (ie check if copy succeed once with original sizes
1403 * and check if the server gave us different sizes after
1404 * we already updated max sizes on previous request).
1405 * if not then why is the server returning an error now
1407 if ((chunks_copied != 0) || chunk_sizes_updated)
1410 /* Check that server is not asking us to grow size */
1411 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1412 tcon->max_bytes_chunk)
1413 tcon->max_bytes_chunk =
1414 le32_to_cpu(retbuf->ChunkBytesWritten);
1416 goto cchunk_out; /* server gave us bogus size */
1418 /* No need to change MaxChunks since already set to 1 */
1419 chunk_sizes_updated = true;
1430 return total_bytes_written;
1434 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1435 struct cifs_fid *fid)
1437 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1441 smb2_read_data_offset(char *buf)
1443 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1444 return rsp->DataOffset;
1448 smb2_read_data_length(char *buf, bool in_remaining)
1450 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1453 return le32_to_cpu(rsp->DataRemaining);
1455 return le32_to_cpu(rsp->DataLength);
1460 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1461 struct cifs_io_parms *parms, unsigned int *bytes_read,
1462 char **buf, int *buf_type)
1464 parms->persistent_fid = pfid->persistent_fid;
1465 parms->volatile_fid = pfid->volatile_fid;
1466 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1470 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1471 struct cifs_io_parms *parms, unsigned int *written,
1472 struct kvec *iov, unsigned long nr_segs)
1475 parms->persistent_fid = pfid->persistent_fid;
1476 parms->volatile_fid = pfid->volatile_fid;
1477 return SMB2_write(xid, parms, written, iov, nr_segs);
1480 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1481 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1482 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1484 struct cifsInodeInfo *cifsi;
1487 cifsi = CIFS_I(inode);
1489 /* if file already sparse don't bother setting sparse again */
1490 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1491 return true; /* already sparse */
1493 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1494 return true; /* already not sparse */
1497 * Can't check for sparse support on share the usual way via the
1498 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1499 * since Samba server doesn't set the flag on the share, yet
1500 * supports the set sparse FSCTL and returns sparse correctly
1501 * in the file attributes. If we fail setting sparse though we
1502 * mark that server does not support sparse files for this share
1503 * to avoid repeatedly sending the unsupported fsctl to server
1504 * if the file is repeatedly extended.
1506 if (tcon->broken_sparse_sup)
1509 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1510 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1512 &setsparse, 1, NULL, NULL);
1514 tcon->broken_sparse_sup = true;
1515 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1520 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1522 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1528 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1529 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1531 __le64 eof = cpu_to_le64(size);
1532 struct inode *inode;
1535 * If extending file more than one page make sparse. Many Linux fs
1536 * make files sparse by default when extending via ftruncate
1538 inode = d_inode(cfile->dentry);
1540 if (!set_alloc && (size > inode->i_size + 8192)) {
1541 __u8 set_sparse = 1;
1543 /* whether set sparse succeeds or not, extend the file */
1544 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1547 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1548 cfile->fid.volatile_fid, cfile->pid, &eof);
1552 smb2_duplicate_extents(const unsigned int xid,
1553 struct cifsFileInfo *srcfile,
1554 struct cifsFileInfo *trgtfile, u64 src_off,
1555 u64 len, u64 dest_off)
1558 unsigned int ret_data_len;
1559 struct duplicate_extents_to_file dup_ext_buf;
1560 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1562 /* server fileays advertise duplicate extent support with this flag */
1563 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1564 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1567 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1568 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1569 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1570 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1571 dup_ext_buf.ByteCount = cpu_to_le64(len);
1572 cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
1573 src_off, dest_off, len);
1575 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1577 goto duplicate_extents_out;
1579 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1580 trgtfile->fid.volatile_fid,
1581 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
1582 true /* is_fsctl */,
1583 (char *)&dup_ext_buf,
1584 sizeof(struct duplicate_extents_to_file),
1588 if (ret_data_len > 0)
1589 cifs_dbg(FYI, "non-zero response length in duplicate extents");
1591 duplicate_extents_out:
1596 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1597 struct cifsFileInfo *cfile)
1599 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1600 cfile->fid.volatile_fid);
1604 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1605 struct cifsFileInfo *cfile)
1607 struct fsctl_set_integrity_information_req integr_info;
1608 unsigned int ret_data_len;
1610 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1611 integr_info.Flags = 0;
1612 integr_info.Reserved = 0;
1614 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1615 cfile->fid.volatile_fid,
1616 FSCTL_SET_INTEGRITY_INFORMATION,
1617 true /* is_fsctl */,
1618 (char *)&integr_info,
1619 sizeof(struct fsctl_set_integrity_information_req),
1625 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
1626 #define GMT_TOKEN_SIZE 50
1629 * Input buffer contains (empty) struct smb_snapshot array with size filled in
1630 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
1633 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1634 struct cifsFileInfo *cfile, void __user *ioc_buf)
1636 char *retbuf = NULL;
1637 unsigned int ret_data_len = 0;
1639 struct smb_snapshot_array snapshot_in;
1641 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1642 cfile->fid.volatile_fid,
1643 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
1644 true /* is_fsctl */,
1645 NULL, 0 /* no input data */,
1648 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1653 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1655 if (copy_from_user(&snapshot_in, ioc_buf,
1656 sizeof(struct smb_snapshot_array))) {
1663 * Check for min size, ie not large enough to fit even one GMT
1664 * token (snapshot). On the first ioctl some users may pass in
1665 * smaller size (or zero) to simply get the size of the array
1666 * so the user space caller can allocate sufficient memory
1667 * and retry the ioctl again with larger array size sufficient
1668 * to hold all of the snapshot GMT tokens on the second try.
1670 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
1671 ret_data_len = sizeof(struct smb_snapshot_array);
1674 * We return struct SRV_SNAPSHOT_ARRAY, followed by
1675 * the snapshot array (of 50 byte GMT tokens) each
1676 * representing an available previous version of the data
1678 if (ret_data_len > (snapshot_in.snapshot_array_size +
1679 sizeof(struct smb_snapshot_array)))
1680 ret_data_len = snapshot_in.snapshot_array_size +
1681 sizeof(struct smb_snapshot_array);
1683 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1692 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1693 const char *path, struct cifs_sb_info *cifs_sb,
1694 struct cifs_fid *fid, __u16 search_flags,
1695 struct cifs_search_info *srch_inf)
1699 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1700 struct cifs_open_parms oparms;
1702 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1707 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1708 oparms.disposition = FILE_OPEN;
1709 if (backup_cred(cifs_sb))
1710 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1712 oparms.create_options = 0;
1714 oparms.reconnect = false;
1716 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
1719 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
1723 srch_inf->entries_in_buffer = 0;
1724 srch_inf->index_of_last_entry = 2;
1726 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1727 fid->volatile_fid, 0, srch_inf);
1729 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
1730 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1736 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1737 struct cifs_fid *fid, __u16 search_flags,
1738 struct cifs_search_info *srch_inf)
1740 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1741 fid->volatile_fid, 0, srch_inf);
1745 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1746 struct cifs_fid *fid)
1748 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1752 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
1753 * the number of credits and return true. Otherwise - return false.
1756 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
1758 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
1760 if (shdr->Status != STATUS_PENDING)
1764 spin_lock(&server->req_lock);
1765 server->credits += le16_to_cpu(shdr->CreditRequest);
1766 spin_unlock(&server->req_lock);
1767 wake_up(&server->request_q);
1774 smb2_is_session_expired(char *buf)
1776 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
1778 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
1779 shdr->Status != STATUS_USER_SESSION_DELETED)
1782 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
1783 le16_to_cpu(shdr->Command),
1784 le64_to_cpu(shdr->MessageId));
1785 cifs_dbg(FYI, "Session expired or deleted\n");
1791 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
1792 struct cifsInodeInfo *cinode)
1794 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
1795 return SMB2_lease_break(0, tcon, cinode->lease_key,
1796 smb2_get_lease_state(cinode));
1798 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
1800 CIFS_CACHE_READ(cinode) ? 1 : 0);
1804 smb2_set_related(struct smb_rqst *rqst)
1806 struct smb2_sync_hdr *shdr;
1808 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
1809 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
1812 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
1815 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
1817 struct smb2_sync_hdr *shdr;
1818 struct cifs_ses *ses = tcon->ses;
1819 struct TCP_Server_Info *server = ses->server;
1820 unsigned long len = smb_rqst_len(server, rqst);
1823 /* SMB headers in a compound are 8 byte aligned. */
1825 /* No padding needed */
1829 num_padding = 8 - (len & 7);
1830 if (!smb3_encryption_required(tcon)) {
1832 * If we do not have encryption then we can just add an extra
1833 * iov for the padding.
1835 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
1836 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
1841 * We can not add a small padding iov for the encryption case
1842 * because the encryption framework can not handle the padding
1844 * We have to flatten this into a single buffer and add
1845 * the padding to it.
1847 for (i = 1; i < rqst->rq_nvec; i++) {
1848 memcpy(rqst->rq_iov[0].iov_base +
1849 rqst->rq_iov[0].iov_len,
1850 rqst->rq_iov[i].iov_base,
1851 rqst->rq_iov[i].iov_len);
1852 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
1854 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
1856 rqst->rq_iov[0].iov_len += num_padding;
1862 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
1863 shdr->NextCommand = cpu_to_le32(len);
1867 * Passes the query info response back to the caller on success.
1868 * Caller need to free this with free_rsp_buf().
1871 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
1872 __le16 *utf16_path, u32 desired_access,
1873 u32 class, u32 type, u32 output_len,
1874 struct kvec *rsp, int *buftype,
1875 struct cifs_sb_info *cifs_sb)
1877 struct cifs_ses *ses = tcon->ses;
1879 struct smb_rqst rqst[3];
1880 int resp_buftype[3];
1881 struct kvec rsp_iov[3];
1882 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1883 struct kvec qi_iov[1];
1884 struct kvec close_iov[1];
1885 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1886 struct cifs_open_parms oparms;
1887 struct cifs_fid fid;
1890 if (smb3_encryption_required(tcon))
1891 flags |= CIFS_TRANSFORM_REQ;
1893 memset(rqst, 0, sizeof(rqst));
1894 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1895 memset(rsp_iov, 0, sizeof(rsp_iov));
1897 memset(&open_iov, 0, sizeof(open_iov));
1898 rqst[0].rq_iov = open_iov;
1899 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1902 oparms.desired_access = desired_access;
1903 oparms.disposition = FILE_OPEN;
1904 if (cifs_sb && backup_cred(cifs_sb))
1905 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1907 oparms.create_options = 0;
1909 oparms.reconnect = false;
1911 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
1914 smb2_set_next_command(tcon, &rqst[0]);
1916 memset(&qi_iov, 0, sizeof(qi_iov));
1917 rqst[1].rq_iov = qi_iov;
1918 rqst[1].rq_nvec = 1;
1920 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1926 smb2_set_next_command(tcon, &rqst[1]);
1927 smb2_set_related(&rqst[1]);
1929 memset(&close_iov, 0, sizeof(close_iov));
1930 rqst[2].rq_iov = close_iov;
1931 rqst[2].rq_nvec = 1;
1933 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1936 smb2_set_related(&rqst[2]);
1938 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1939 resp_buftype, rsp_iov);
1941 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1945 *buftype = resp_buftype[1];
1948 SMB2_open_free(&rqst[0]);
1949 SMB2_query_info_free(&rqst[1]);
1950 SMB2_close_free(&rqst[2]);
1951 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1952 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1957 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1958 struct kstatfs *buf)
1960 struct smb2_query_info_rsp *rsp;
1961 struct smb2_fs_full_size_info *info = NULL;
1962 __le16 utf16_path = 0; /* Null - open root of share */
1963 struct kvec rsp_iov = {NULL, 0};
1964 int buftype = CIFS_NO_BUFFER;
1968 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
1969 FILE_READ_ATTRIBUTES,
1970 FS_FULL_SIZE_INFORMATION,
1971 SMB2_O_INFO_FILESYSTEM,
1972 sizeof(struct smb2_fs_full_size_info),
1973 &rsp_iov, &buftype, NULL);
1977 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1978 buf->f_type = SMB2_MAGIC_NUMBER;
1979 info = (struct smb2_fs_full_size_info *)(
1980 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1981 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1982 le32_to_cpu(rsp->OutputBufferLength),
1984 sizeof(struct smb2_fs_full_size_info));
1986 smb2_copy_fs_info_to_kstatfs(info, buf);
1989 free_rsp_buf(buftype, rsp_iov.iov_base);
1994 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1995 struct kstatfs *buf)
1998 __le16 srch_path = 0; /* Null - open root of share */
1999 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2000 struct cifs_open_parms oparms;
2001 struct cifs_fid fid;
2003 if (!tcon->posix_extensions)
2004 return smb2_queryfs(xid, tcon, buf);
2007 oparms.desired_access = FILE_READ_ATTRIBUTES;
2008 oparms.disposition = FILE_OPEN;
2009 oparms.create_options = 0;
2011 oparms.reconnect = false;
2013 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
2017 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2018 fid.volatile_fid, buf);
2019 buf->f_type = SMB2_MAGIC_NUMBER;
2020 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2025 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2027 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2028 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2032 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2033 __u64 length, __u32 type, int lock, int unlock, bool wait)
2035 if (unlock && !lock)
2036 type = SMB2_LOCKFLAG_UNLOCK;
2037 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2038 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2039 current->tgid, length, offset, type, wait);
2043 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2045 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2049 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2051 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2055 smb2_new_lease_key(struct cifs_fid *fid)
2057 generate_random_uuid(fid->lease_key);
2061 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2062 const char *search_name,
2063 struct dfs_info3_param **target_nodes,
2064 unsigned int *num_of_nodes,
2065 const struct nls_table *nls_codepage, int remap)
2068 __le16 *utf16_path = NULL;
2069 int utf16_path_len = 0;
2070 struct cifs_tcon *tcon;
2071 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2072 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2073 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2075 cifs_dbg(FYI, "smb2_get_dfs_refer path <%s>\n", search_name);
2078 * Try to use the IPC tcon, otherwise just use any
2080 tcon = ses->tcon_ipc;
2082 spin_lock(&cifs_tcp_ses_lock);
2083 tcon = list_first_entry_or_null(&ses->tcon_list,
2088 spin_unlock(&cifs_tcp_ses_lock);
2092 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2098 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2100 nls_codepage, remap);
2106 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2107 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2113 /* Highest DFS referral version understood */
2114 dfs_req->MaxReferralLevel = DFS_VERSION;
2116 /* Path to resolve in an UTF-16 null-terminated string */
2117 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2120 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2121 FSCTL_DFS_GET_REFERRALS,
2122 true /* is_fsctl */,
2123 (char *)dfs_req, dfs_req_size,
2124 (char **)&dfs_rsp, &dfs_rsp_size);
2125 } while (rc == -EAGAIN);
2128 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
2129 cifs_dbg(VFS, "ioctl error in smb2_get_dfs_refer rc=%d\n", rc);
2133 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2134 num_of_nodes, target_nodes,
2135 nls_codepage, remap, search_name,
2136 true /* is_unicode */);
2138 cifs_dbg(VFS, "parse error in smb2_get_dfs_refer rc=%d\n", rc);
2143 if (tcon && !tcon->ipc) {
2144 /* ipc tcons are not refcounted */
2145 spin_lock(&cifs_tcp_ses_lock);
2147 spin_unlock(&cifs_tcp_ses_lock);
2154 #define SMB2_SYMLINK_STRUCT_SIZE \
2155 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2158 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
2159 const char *full_path, char **target_path,
2160 struct cifs_sb_info *cifs_sb)
2164 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2165 struct cifs_open_parms oparms;
2166 struct cifs_fid fid;
2167 struct kvec err_iov = {NULL, 0};
2168 struct smb2_err_rsp *err_buf = NULL;
2170 struct smb2_symlink_err_rsp *symlink;
2171 unsigned int sub_len;
2172 unsigned int sub_offset;
2173 unsigned int print_len;
2174 unsigned int print_offset;
2176 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2178 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2183 oparms.desired_access = FILE_READ_ATTRIBUTES;
2184 oparms.disposition = FILE_OPEN;
2185 if (backup_cred(cifs_sb))
2186 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2188 oparms.create_options = 0;
2190 oparms.reconnect = false;
2192 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_iov,
2194 if (!rc || !err_iov.iov_base) {
2199 err_buf = err_iov.iov_base;
2200 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
2201 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
2206 /* open must fail on symlink - reset rc */
2208 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
2209 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
2210 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
2211 print_len = le16_to_cpu(symlink->PrintNameLength);
2212 print_offset = le16_to_cpu(symlink->PrintNameOffset);
2214 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
2219 if (err_iov.iov_len <
2220 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
2225 *target_path = cifs_strndup_from_utf16(
2226 (char *)symlink->PathBuffer + sub_offset,
2227 sub_len, true, cifs_sb->local_nls);
2228 if (!(*target_path)) {
2232 convert_delimiter(*target_path, '/');
2233 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2236 free_rsp_buf(resp_buftype, err_buf);
2242 #ifdef CONFIG_CIFS_ACL
2243 static struct cifs_ntsd *
2244 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
2245 const struct cifs_fid *cifsfid, u32 *pacllen)
2247 struct cifs_ntsd *pntsd = NULL;
2249 int rc = -EOPNOTSUPP;
2250 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2253 return ERR_CAST(tlink);
2256 cifs_dbg(FYI, "trying to get acl\n");
2258 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
2259 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
2262 cifs_put_tlink(tlink);
2264 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2271 static struct cifs_ntsd *
2272 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
2273 const char *path, u32 *pacllen)
2275 struct cifs_ntsd *pntsd = NULL;
2276 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2279 struct cifs_tcon *tcon;
2280 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2281 struct cifs_fid fid;
2282 struct cifs_open_parms oparms;
2285 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
2287 return ERR_CAST(tlink);
2289 tcon = tlink_tcon(tlink);
2292 if (backup_cred(cifs_sb))
2293 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2295 oparms.create_options = 0;
2297 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2305 oparms.desired_access = READ_CONTROL;
2306 oparms.disposition = FILE_OPEN;
2308 oparms.reconnect = false;
2310 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2313 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2314 fid.volatile_fid, (void **)&pntsd, pacllen);
2315 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2318 cifs_put_tlink(tlink);
2321 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2327 #ifdef CONFIG_CIFS_ACL
2329 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
2330 struct inode *inode, const char *path, int aclflag)
2332 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2334 int rc, access_flags = 0;
2335 struct cifs_tcon *tcon;
2336 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2337 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2338 struct cifs_fid fid;
2339 struct cifs_open_parms oparms;
2342 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
2344 return PTR_ERR(tlink);
2346 tcon = tlink_tcon(tlink);
2349 if (backup_cred(cifs_sb))
2350 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2352 oparms.create_options = 0;
2354 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
2355 access_flags = WRITE_OWNER;
2357 access_flags = WRITE_DAC;
2359 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2367 oparms.desired_access = access_flags;
2368 oparms.disposition = FILE_OPEN;
2371 oparms.reconnect = false;
2373 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2376 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2377 fid.volatile_fid, pnntsd, acllen, aclflag);
2378 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2381 cifs_put_tlink(tlink);
2385 #endif /* CIFS_ACL */
2387 /* Retrieve an ACL from the server */
2388 static struct cifs_ntsd *
2389 get_smb2_acl(struct cifs_sb_info *cifs_sb,
2390 struct inode *inode, const char *path,
2393 struct cifs_ntsd *pntsd = NULL;
2394 struct cifsFileInfo *open_file = NULL;
2397 open_file = find_readable_file(CIFS_I(inode), true);
2399 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
2401 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
2402 cifsFileInfo_put(open_file);
2407 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
2408 loff_t offset, loff_t len, bool keep_size)
2410 struct inode *inode;
2411 struct cifsInodeInfo *cifsi;
2412 struct cifsFileInfo *cfile = file->private_data;
2413 struct file_zero_data_information fsctl_buf;
2419 inode = d_inode(cfile->dentry);
2420 cifsi = CIFS_I(inode);
2422 /* if file not oplocked can't be sure whether asking to extend size */
2423 if (!CIFS_CACHE_READ(cifsi))
2424 if (keep_size == false) {
2431 * Must check if file sparse since fallocate -z (zero range) assumes
2432 * non-sparse allocation
2434 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
2441 * need to make sure we are not asked to extend the file since the SMB3
2442 * fsctl does not change the file size. In the future we could change
2443 * this to zero the first part of the range then set the file size
2444 * which for a non sparse file would zero the newly extended range
2446 if (keep_size == false)
2447 if (i_size_read(inode) < offset + len) {
2453 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
2455 fsctl_buf.FileOffset = cpu_to_le64(offset);
2456 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2458 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2459 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
2460 true /* is_fctl */, (char *)&fsctl_buf,
2461 sizeof(struct file_zero_data_information), NULL, NULL);
2466 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
2467 loff_t offset, loff_t len)
2469 struct inode *inode;
2470 struct cifsInodeInfo *cifsi;
2471 struct cifsFileInfo *cfile = file->private_data;
2472 struct file_zero_data_information fsctl_buf;
2475 __u8 set_sparse = 1;
2479 inode = d_inode(cfile->dentry);
2480 cifsi = CIFS_I(inode);
2482 /* Need to make file sparse, if not already, before freeing range. */
2483 /* Consider adding equivalent for compressed since it could also work */
2484 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
2490 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
2492 fsctl_buf.FileOffset = cpu_to_le64(offset);
2493 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2495 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2496 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
2497 true /* is_fctl */, (char *)&fsctl_buf,
2498 sizeof(struct file_zero_data_information), NULL, NULL);
2503 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
2504 loff_t off, loff_t len, bool keep_size)
2506 struct inode *inode;
2507 struct cifsInodeInfo *cifsi;
2508 struct cifsFileInfo *cfile = file->private_data;
2509 long rc = -EOPNOTSUPP;
2514 inode = d_inode(cfile->dentry);
2515 cifsi = CIFS_I(inode);
2517 /* if file not oplocked can't be sure whether asking to extend size */
2518 if (!CIFS_CACHE_READ(cifsi))
2519 if (keep_size == false) {
2525 * Files are non-sparse by default so falloc may be a no-op
2526 * Must check if file sparse. If not sparse, and not extending
2527 * then no need to do anything since file already allocated
2529 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
2530 if (keep_size == true)
2532 /* check if extending file */
2533 else if (i_size_read(inode) >= off + len)
2534 /* not extending file and already not sparse */
2536 /* BB: in future add else clause to extend file */
2543 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
2545 * Check if falloc starts within first few pages of file
2546 * and ends within a few pages of the end of file to
2547 * ensure that most of file is being forced to be
2548 * fallocated now. If so then setting whole file sparse
2549 * ie potentially making a few extra pages at the beginning
2550 * or end of the file non-sparse via set_sparse is harmless.
2552 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
2558 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
2560 /* BB: else ... in future add code to extend file and set sparse */
2568 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
2569 loff_t off, loff_t len)
2571 /* KEEP_SIZE already checked for by do_fallocate */
2572 if (mode & FALLOC_FL_PUNCH_HOLE)
2573 return smb3_punch_hole(file, tcon, off, len);
2574 else if (mode & FALLOC_FL_ZERO_RANGE) {
2575 if (mode & FALLOC_FL_KEEP_SIZE)
2576 return smb3_zero_range(file, tcon, off, len, true);
2577 return smb3_zero_range(file, tcon, off, len, false);
2578 } else if (mode == FALLOC_FL_KEEP_SIZE)
2579 return smb3_simple_falloc(file, tcon, off, len, true);
2581 return smb3_simple_falloc(file, tcon, off, len, false);
2587 smb2_downgrade_oplock(struct TCP_Server_Info *server,
2588 struct cifsInodeInfo *cinode, bool set_level2)
2591 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
2594 server->ops->set_oplock_level(cinode, 0, 0, NULL);
2598 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2599 unsigned int epoch, bool *purge_cache)
2602 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
2604 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2605 cinode->oplock = CIFS_CACHE_RHW_FLG;
2606 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
2607 &cinode->vfs_inode);
2608 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
2609 cinode->oplock = CIFS_CACHE_RW_FLG;
2610 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
2611 &cinode->vfs_inode);
2612 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
2613 cinode->oplock = CIFS_CACHE_READ_FLG;
2614 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
2615 &cinode->vfs_inode);
2621 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2622 unsigned int epoch, bool *purge_cache)
2624 char message[5] = {0};
2627 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
2631 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
2632 cinode->oplock |= CIFS_CACHE_READ_FLG;
2633 strcat(message, "R");
2635 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
2636 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
2637 strcat(message, "H");
2639 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
2640 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
2641 strcat(message, "W");
2643 if (!cinode->oplock)
2644 strcat(message, "None");
2645 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
2646 &cinode->vfs_inode);
2650 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2651 unsigned int epoch, bool *purge_cache)
2653 unsigned int old_oplock = cinode->oplock;
2655 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
2658 *purge_cache = false;
2659 if (old_oplock == CIFS_CACHE_READ_FLG) {
2660 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
2661 (epoch - cinode->epoch > 0))
2662 *purge_cache = true;
2663 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2664 (epoch - cinode->epoch > 1))
2665 *purge_cache = true;
2666 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2667 (epoch - cinode->epoch > 1))
2668 *purge_cache = true;
2669 else if (cinode->oplock == 0 &&
2670 (epoch - cinode->epoch > 0))
2671 *purge_cache = true;
2672 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
2673 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2674 (epoch - cinode->epoch > 0))
2675 *purge_cache = true;
2676 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2677 (epoch - cinode->epoch > 1))
2678 *purge_cache = true;
2680 cinode->epoch = epoch;
2685 smb2_is_read_op(__u32 oplock)
2687 return oplock == SMB2_OPLOCK_LEVEL_II;
2691 smb21_is_read_op(__u32 oplock)
2693 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
2694 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
2698 map_oplock_to_lease(u8 oplock)
2700 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
2701 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
2702 else if (oplock == SMB2_OPLOCK_LEVEL_II)
2703 return SMB2_LEASE_READ_CACHING;
2704 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
2705 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
2706 SMB2_LEASE_WRITE_CACHING;
2711 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
2713 struct create_lease *buf;
2715 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
2719 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
2720 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2722 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2723 (struct create_lease, lcontext));
2724 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
2725 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2726 (struct create_lease, Name));
2727 buf->ccontext.NameLength = cpu_to_le16(4);
2728 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
2737 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
2739 struct create_lease_v2 *buf;
2741 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
2745 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
2746 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2748 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2749 (struct create_lease_v2, lcontext));
2750 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
2751 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2752 (struct create_lease_v2, Name));
2753 buf->ccontext.NameLength = cpu_to_le16(4);
2754 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
2763 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
2765 struct create_lease *lc = (struct create_lease *)buf;
2767 *epoch = 0; /* not used */
2768 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2769 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2770 return le32_to_cpu(lc->lcontext.LeaseState);
2774 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
2776 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
2778 *epoch = le16_to_cpu(lc->lcontext.Epoch);
2779 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2780 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2782 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
2783 return le32_to_cpu(lc->lcontext.LeaseState);
2787 smb2_wp_retry_size(struct inode *inode)
2789 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
2790 SMB2_MAX_BUFFER_SIZE);
2794 smb2_dir_needs_close(struct cifsFileInfo *cfile)
2796 return !cfile->invalidHandle;
2800 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
2801 struct smb_rqst *old_rq)
2803 struct smb2_sync_hdr *shdr =
2804 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
2806 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
2807 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
2808 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
2809 tr_hdr->Flags = cpu_to_le16(0x01);
2810 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2811 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
2814 /* We can not use the normal sg_set_buf() as we will sometimes pass a
2815 * stack object as buf.
2817 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
2818 unsigned int buflen)
2820 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
2823 /* Assumes the first rqst has a transform header as the first iov.
2825 * rqst[0].rq_iov[0] is transform header
2826 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
2827 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
2829 static struct scatterlist *
2830 init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
2832 unsigned int sg_len;
2833 struct scatterlist *sg;
2836 unsigned int idx = 0;
2840 for (i = 0; i < num_rqst; i++)
2841 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
2843 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
2847 sg_init_table(sg, sg_len);
2848 for (i = 0; i < num_rqst; i++) {
2849 for (j = 0; j < rqst[i].rq_nvec; j++) {
2851 * The first rqst has a transform header where the
2852 * first 20 bytes are not part of the encrypted blob
2854 skip = (i == 0) && (j == 0) ? 20 : 0;
2855 smb2_sg_set_buf(&sg[idx++],
2856 rqst[i].rq_iov[j].iov_base + skip,
2857 rqst[i].rq_iov[j].iov_len - skip);
2860 for (j = 0; j < rqst[i].rq_npages; j++) {
2861 unsigned int len, offset;
2863 rqst_page_get_length(&rqst[i], j, &len, &offset);
2864 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
2867 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
2872 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
2874 struct cifs_ses *ses;
2877 spin_lock(&cifs_tcp_ses_lock);
2878 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2879 if (ses->Suid != ses_id)
2881 ses_enc_key = enc ? ses->smb3encryptionkey :
2882 ses->smb3decryptionkey;
2883 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
2884 spin_unlock(&cifs_tcp_ses_lock);
2887 spin_unlock(&cifs_tcp_ses_lock);
2892 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
2893 * iov[0] - transform header (associate data),
2894 * iov[1-N] - SMB2 header and pages - data to encrypt.
2895 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
2899 crypt_message(struct TCP_Server_Info *server, int num_rqst,
2900 struct smb_rqst *rqst, int enc)
2902 struct smb2_transform_hdr *tr_hdr =
2903 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
2904 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
2906 struct scatterlist *sg;
2907 u8 sign[SMB2_SIGNATURE_SIZE] = {};
2908 u8 key[SMB3_SIGN_KEY_SIZE];
2909 struct aead_request *req;
2911 unsigned int iv_len;
2912 DECLARE_CRYPTO_WAIT(wait);
2913 struct crypto_aead *tfm;
2914 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2916 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
2918 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
2923 rc = smb3_crypto_aead_allocate(server);
2925 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
2929 tfm = enc ? server->secmech.ccmaesencrypt :
2930 server->secmech.ccmaesdecrypt;
2931 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
2933 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
2937 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
2939 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
2943 req = aead_request_alloc(tfm, GFP_KERNEL);
2945 cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
2950 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
2951 crypt_len += SMB2_SIGNATURE_SIZE;
2954 sg = init_sg(num_rqst, rqst, sign);
2956 cifs_dbg(VFS, "%s: Failed to init sg", __func__);
2961 iv_len = crypto_aead_ivsize(tfm);
2962 iv = kzalloc(iv_len, GFP_KERNEL);
2964 cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
2969 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2971 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
2972 aead_request_set_ad(req, assoc_data_len);
2974 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2975 crypto_req_done, &wait);
2977 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
2978 : crypto_aead_decrypt(req), &wait);
2981 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
2992 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
2996 for (i = 0; i < num_rqst; i++) {
2997 if (rqst[i].rq_pages) {
2998 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
2999 put_page(rqst[i].rq_pages[j]);
3000 kfree(rqst[i].rq_pages);
3006 * This function will initialize new_rq and encrypt the content.
3007 * The first entry, new_rq[0], only contains a single iov which contains
3008 * a smb2_transform_hdr and is pre-allocated by the caller.
3009 * This function then populates new_rq[1+] with the content from olq_rq[0+].
3011 * The end result is an array of smb_rqst structures where the first structure
3012 * only contains a single iov for the transform header which we then can pass
3013 * to crypt_message().
3015 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
3016 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
3019 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
3020 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
3022 struct page **pages;
3023 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
3024 unsigned int npages;
3025 unsigned int orig_len = 0;
3029 for (i = 1; i < num_rqst; i++) {
3030 npages = old_rq[i - 1].rq_npages;
3031 pages = kmalloc_array(npages, sizeof(struct page *),
3036 new_rq[i].rq_pages = pages;
3037 new_rq[i].rq_npages = npages;
3038 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
3039 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
3040 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
3041 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
3042 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
3044 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
3046 for (j = 0; j < npages; j++) {
3047 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3052 /* copy pages form the old */
3053 for (j = 0; j < npages; j++) {
3055 unsigned int offset, len;
3057 rqst_page_get_length(&new_rq[i], j, &len, &offset);
3059 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
3060 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
3062 memcpy(dst, src, len);
3063 kunmap(new_rq[i].rq_pages[j]);
3064 kunmap(old_rq[i - 1].rq_pages[j]);
3068 /* fill the 1st iov with a transform header */
3069 fill_transform_hdr(tr_hdr, orig_len, old_rq);
3071 rc = crypt_message(server, num_rqst, new_rq, 1);
3072 cifs_dbg(FYI, "encrypt message returned %d", rc);
3079 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
3084 smb3_is_transform_hdr(void *buf)
3086 struct smb2_transform_hdr *trhdr = buf;
3088 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
3092 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
3093 unsigned int buf_data_size, struct page **pages,
3094 unsigned int npages, unsigned int page_data_size)
3097 struct smb_rqst rqst = {NULL};
3100 iov[0].iov_base = buf;
3101 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
3102 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
3103 iov[1].iov_len = buf_data_size;
3107 rqst.rq_pages = pages;
3108 rqst.rq_npages = npages;
3109 rqst.rq_pagesz = PAGE_SIZE;
3110 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
3112 rc = crypt_message(server, 1, &rqst, 0);
3113 cifs_dbg(FYI, "decrypt message returned %d\n", rc);
3118 memmove(buf, iov[1].iov_base, buf_data_size);
3120 server->total_read = buf_data_size + page_data_size;
3126 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
3127 unsigned int npages, unsigned int len)
3132 for (i = 0; i < npages; i++) {
3133 struct page *page = pages[i];
3137 if (len >= PAGE_SIZE) {
3138 /* enough data to fill the page */
3142 zero_user(page, len, PAGE_SIZE - len);
3145 length = cifs_read_page_from_socket(server, page, 0, n);
3148 server->total_read += length;
3155 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
3156 unsigned int cur_off, struct bio_vec **page_vec)
3158 struct bio_vec *bvec;
3161 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
3165 for (i = 0; i < npages; i++) {
3166 bvec[i].bv_page = pages[i];
3167 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
3168 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
3169 data_size -= bvec[i].bv_len;
3172 if (data_size != 0) {
3173 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
3183 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
3184 char *buf, unsigned int buf_len, struct page **pages,
3185 unsigned int npages, unsigned int page_data_size)
3187 unsigned int data_offset;
3188 unsigned int data_len;
3189 unsigned int cur_off;
3190 unsigned int cur_page_idx;
3191 unsigned int pad_len;
3192 struct cifs_readdata *rdata = mid->callback_data;
3193 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
3194 struct bio_vec *bvec = NULL;
3195 struct iov_iter iter;
3198 bool use_rdma_mr = false;
3200 if (shdr->Command != SMB2_READ) {
3201 cifs_dbg(VFS, "only big read responses are supported\n");
3205 if (server->ops->is_session_expired &&
3206 server->ops->is_session_expired(buf)) {
3207 cifs_reconnect(server);
3208 wake_up(&server->response_q);
3212 if (server->ops->is_status_pending &&
3213 server->ops->is_status_pending(buf, server, 0))
3216 /* set up first two iov to get credits */
3217 rdata->iov[0].iov_base = buf;
3218 rdata->iov[0].iov_len = 4;
3219 rdata->iov[1].iov_base = buf + 4;
3220 rdata->iov[1].iov_len =
3221 min_t(unsigned int, buf_len, server->vals->read_rsp_size) - 4;
3222 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
3223 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
3224 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
3225 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
3227 rdata->result = server->ops->map_error(buf, true);
3228 if (rdata->result != 0) {
3229 cifs_dbg(FYI, "%s: server returned error %d\n",
3230 __func__, rdata->result);
3231 /* normal error on read response */
3232 dequeue_mid(mid, false);
3236 data_offset = server->ops->read_data_offset(buf);
3237 #ifdef CONFIG_CIFS_SMB_DIRECT
3238 use_rdma_mr = rdata->mr;
3240 data_len = server->ops->read_data_length(buf, use_rdma_mr);
3242 if (data_offset < server->vals->read_rsp_size) {
3244 * win2k8 sometimes sends an offset of 0 when the read
3245 * is beyond the EOF. Treat it as if the data starts just after
3248 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
3249 __func__, data_offset);
3250 data_offset = server->vals->read_rsp_size;
3251 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
3252 /* data_offset is beyond the end of smallbuf */
3253 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
3254 __func__, data_offset);
3255 rdata->result = -EIO;
3256 dequeue_mid(mid, rdata->result);
3260 pad_len = data_offset - server->vals->read_rsp_size;
3262 if (buf_len <= data_offset) {
3263 /* read response payload is in pages */
3264 cur_page_idx = pad_len / PAGE_SIZE;
3265 cur_off = pad_len % PAGE_SIZE;
3267 if (cur_page_idx != 0) {
3268 /* data offset is beyond the 1st page of response */
3269 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
3270 __func__, data_offset);
3271 rdata->result = -EIO;
3272 dequeue_mid(mid, rdata->result);
3276 if (data_len > page_data_size - pad_len) {
3277 /* data_len is corrupt -- discard frame */
3278 rdata->result = -EIO;
3279 dequeue_mid(mid, rdata->result);
3283 rdata->result = init_read_bvec(pages, npages, page_data_size,
3285 if (rdata->result != 0) {
3286 dequeue_mid(mid, rdata->result);
3290 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
3291 } else if (buf_len >= data_offset + data_len) {
3292 /* read response payload is in buf */
3293 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
3294 iov.iov_base = buf + data_offset;
3295 iov.iov_len = data_len;
3296 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
3298 /* read response payload cannot be in both buf and pages */
3299 WARN_ONCE(1, "buf can not contain only a part of read data");
3300 rdata->result = -EIO;
3301 dequeue_mid(mid, rdata->result);
3305 length = rdata->copy_into_pages(server, rdata, &iter);
3312 dequeue_mid(mid, false);
3317 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
3319 char *buf = server->smallbuf;
3320 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3321 unsigned int npages;
3322 struct page **pages;
3324 unsigned int buflen = server->pdu_size;
3328 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
3329 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
3331 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
3334 server->total_read += rc;
3336 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
3337 server->vals->read_rsp_size;
3338 npages = DIV_ROUND_UP(len, PAGE_SIZE);
3340 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
3346 for (; i < npages; i++) {
3347 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3354 /* read read data into pages */
3355 rc = read_data_into_pages(server, pages, npages, len);
3359 rc = cifs_discard_remaining_data(server);
3363 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
3364 pages, npages, len);
3368 *mid = smb2_find_mid(server, buf);
3370 cifs_dbg(FYI, "mid not found\n");
3372 cifs_dbg(FYI, "mid found\n");
3373 (*mid)->decrypted = true;
3374 rc = handle_read_data(server, *mid, buf,
3375 server->vals->read_rsp_size,
3376 pages, npages, len);
3380 for (i = i - 1; i >= 0; i--)
3385 cifs_discard_remaining_data(server);
3390 receive_encrypted_standard(struct TCP_Server_Info *server,
3391 struct mid_q_entry **mids, char **bufs,
3395 char *buf = server->smallbuf;
3397 struct smb2_sync_hdr *shdr;
3398 unsigned int pdu_length = server->pdu_size;
3399 unsigned int buf_size;
3400 struct mid_q_entry *mid_entry;
3402 char *next_buffer = NULL;
3406 /* switch to large buffer if too big for a small one */
3407 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
3408 server->large_buf = true;
3409 memcpy(server->bigbuf, buf, server->total_read);
3410 buf = server->bigbuf;
3413 /* now read the rest */
3414 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
3415 pdu_length - HEADER_SIZE(server) + 1);
3418 server->total_read += length;
3420 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
3421 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
3425 next_is_large = server->large_buf;
3427 shdr = (struct smb2_sync_hdr *)buf;
3428 if (shdr->NextCommand) {
3429 if (next_is_large) {
3430 tmpbuf = server->bigbuf;
3431 next_buffer = (char *)cifs_buf_get();
3433 tmpbuf = server->smallbuf;
3434 next_buffer = (char *)cifs_small_buf_get();
3437 tmpbuf + le32_to_cpu(shdr->NextCommand),
3438 pdu_length - le32_to_cpu(shdr->NextCommand));
3441 mid_entry = smb2_find_mid(server, buf);
3442 if (mid_entry == NULL)
3443 cifs_dbg(FYI, "mid not found\n");
3445 cifs_dbg(FYI, "mid found\n");
3446 mid_entry->decrypted = true;
3447 mid_entry->resp_buf_size = server->pdu_size;
3450 if (*num_mids >= MAX_COMPOUND) {
3451 cifs_dbg(VFS, "too many PDUs in compound\n");
3454 bufs[*num_mids] = buf;
3455 mids[(*num_mids)++] = mid_entry;
3457 if (mid_entry && mid_entry->handle)
3458 ret = mid_entry->handle(server, mid_entry);
3460 ret = cifs_handle_standard(server, mid_entry);
3462 if (ret == 0 && shdr->NextCommand) {
3463 pdu_length -= le32_to_cpu(shdr->NextCommand);
3464 server->large_buf = next_is_large;
3466 server->bigbuf = next_buffer;
3468 server->smallbuf = next_buffer;
3470 buf += le32_to_cpu(shdr->NextCommand);
3478 smb3_receive_transform(struct TCP_Server_Info *server,
3479 struct mid_q_entry **mids, char **bufs, int *num_mids)
3481 char *buf = server->smallbuf;
3482 unsigned int pdu_length = server->pdu_size;
3483 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3484 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3486 if (pdu_length < sizeof(struct smb2_transform_hdr) +
3487 sizeof(struct smb2_sync_hdr)) {
3488 cifs_dbg(VFS, "Transform message is too small (%u)\n",
3490 cifs_reconnect(server);
3491 wake_up(&server->response_q);
3492 return -ECONNABORTED;
3495 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
3496 cifs_dbg(VFS, "Transform message is broken\n");
3497 cifs_reconnect(server);
3498 wake_up(&server->response_q);
3499 return -ECONNABORTED;
3502 /* TODO: add support for compounds containing READ. */
3503 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
3505 return receive_encrypted_read(server, &mids[0]);
3508 return receive_encrypted_standard(server, mids, bufs, num_mids);
3512 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
3514 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
3516 return handle_read_data(server, mid, buf, server->pdu_size,
3521 smb2_next_header(char *buf)
3523 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
3524 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
3526 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
3527 return sizeof(struct smb2_transform_hdr) +
3528 le32_to_cpu(t_hdr->OriginalMessageSize);
3530 return le32_to_cpu(hdr->NextCommand);
3533 struct smb_version_operations smb20_operations = {
3534 .compare_fids = smb2_compare_fids,
3535 .setup_request = smb2_setup_request,
3536 .setup_async_request = smb2_setup_async_request,
3537 .check_receive = smb2_check_receive,
3538 .add_credits = smb2_add_credits,
3539 .set_credits = smb2_set_credits,
3540 .get_credits_field = smb2_get_credits_field,
3541 .get_credits = smb2_get_credits,
3542 .wait_mtu_credits = cifs_wait_mtu_credits,
3543 .get_next_mid = smb2_get_next_mid,
3544 .read_data_offset = smb2_read_data_offset,
3545 .read_data_length = smb2_read_data_length,
3546 .map_error = map_smb2_to_linux_error,
3547 .find_mid = smb2_find_mid,
3548 .check_message = smb2_check_message,
3549 .dump_detail = smb2_dump_detail,
3550 .clear_stats = smb2_clear_stats,
3551 .print_stats = smb2_print_stats,
3552 .is_oplock_break = smb2_is_valid_oplock_break,
3553 .handle_cancelled_mid = smb2_handle_cancelled_mid,
3554 .downgrade_oplock = smb2_downgrade_oplock,
3555 .need_neg = smb2_need_neg,
3556 .negotiate = smb2_negotiate,
3557 .negotiate_wsize = smb2_negotiate_wsize,
3558 .negotiate_rsize = smb2_negotiate_rsize,
3559 .sess_setup = SMB2_sess_setup,
3560 .logoff = SMB2_logoff,
3561 .tree_connect = SMB2_tcon,
3562 .tree_disconnect = SMB2_tdis,
3563 .qfs_tcon = smb2_qfs_tcon,
3564 .is_path_accessible = smb2_is_path_accessible,
3565 .can_echo = smb2_can_echo,
3567 .query_path_info = smb2_query_path_info,
3568 .get_srv_inum = smb2_get_srv_inum,
3569 .query_file_info = smb2_query_file_info,
3570 .set_path_size = smb2_set_path_size,
3571 .set_file_size = smb2_set_file_size,
3572 .set_file_info = smb2_set_file_info,
3573 .set_compression = smb2_set_compression,
3574 .mkdir = smb2_mkdir,
3575 .mkdir_setinfo = smb2_mkdir_setinfo,
3576 .rmdir = smb2_rmdir,
3577 .unlink = smb2_unlink,
3578 .rename = smb2_rename_path,
3579 .create_hardlink = smb2_create_hardlink,
3580 .query_symlink = smb2_query_symlink,
3581 .query_mf_symlink = smb3_query_mf_symlink,
3582 .create_mf_symlink = smb3_create_mf_symlink,
3583 .open = smb2_open_file,
3584 .set_fid = smb2_set_fid,
3585 .close = smb2_close_file,
3586 .flush = smb2_flush_file,
3587 .async_readv = smb2_async_readv,
3588 .async_writev = smb2_async_writev,
3589 .sync_read = smb2_sync_read,
3590 .sync_write = smb2_sync_write,
3591 .query_dir_first = smb2_query_dir_first,
3592 .query_dir_next = smb2_query_dir_next,
3593 .close_dir = smb2_close_dir,
3594 .calc_smb_size = smb2_calc_size,
3595 .is_status_pending = smb2_is_status_pending,
3596 .is_session_expired = smb2_is_session_expired,
3597 .oplock_response = smb2_oplock_response,
3598 .queryfs = smb2_queryfs,
3599 .mand_lock = smb2_mand_lock,
3600 .mand_unlock_range = smb2_unlock_range,
3601 .push_mand_locks = smb2_push_mandatory_locks,
3602 .get_lease_key = smb2_get_lease_key,
3603 .set_lease_key = smb2_set_lease_key,
3604 .new_lease_key = smb2_new_lease_key,
3605 .calc_signature = smb2_calc_signature,
3606 .is_read_op = smb2_is_read_op,
3607 .set_oplock_level = smb2_set_oplock_level,
3608 .create_lease_buf = smb2_create_lease_buf,
3609 .parse_lease_buf = smb2_parse_lease_buf,
3610 .copychunk_range = smb2_copychunk_range,
3611 .wp_retry_size = smb2_wp_retry_size,
3612 .dir_needs_close = smb2_dir_needs_close,
3613 .get_dfs_refer = smb2_get_dfs_refer,
3614 .select_sectype = smb2_select_sectype,
3615 #ifdef CONFIG_CIFS_XATTR
3616 .query_all_EAs = smb2_query_eas,
3617 .set_EA = smb2_set_ea,
3618 #endif /* CIFS_XATTR */
3619 #ifdef CONFIG_CIFS_ACL
3620 .get_acl = get_smb2_acl,
3621 .get_acl_by_fid = get_smb2_acl_by_fid,
3622 .set_acl = set_smb2_acl,
3623 #endif /* CIFS_ACL */
3624 .next_header = smb2_next_header,
3625 .ioctl_query_info = smb2_ioctl_query_info,
3628 struct smb_version_operations smb21_operations = {
3629 .compare_fids = smb2_compare_fids,
3630 .setup_request = smb2_setup_request,
3631 .setup_async_request = smb2_setup_async_request,
3632 .check_receive = smb2_check_receive,
3633 .add_credits = smb2_add_credits,
3634 .set_credits = smb2_set_credits,
3635 .get_credits_field = smb2_get_credits_field,
3636 .get_credits = smb2_get_credits,
3637 .wait_mtu_credits = smb2_wait_mtu_credits,
3638 .get_next_mid = smb2_get_next_mid,
3639 .read_data_offset = smb2_read_data_offset,
3640 .read_data_length = smb2_read_data_length,
3641 .map_error = map_smb2_to_linux_error,
3642 .find_mid = smb2_find_mid,
3643 .check_message = smb2_check_message,
3644 .dump_detail = smb2_dump_detail,
3645 .clear_stats = smb2_clear_stats,
3646 .print_stats = smb2_print_stats,
3647 .is_oplock_break = smb2_is_valid_oplock_break,
3648 .handle_cancelled_mid = smb2_handle_cancelled_mid,
3649 .downgrade_oplock = smb2_downgrade_oplock,
3650 .need_neg = smb2_need_neg,
3651 .negotiate = smb2_negotiate,
3652 .negotiate_wsize = smb2_negotiate_wsize,
3653 .negotiate_rsize = smb2_negotiate_rsize,
3654 .sess_setup = SMB2_sess_setup,
3655 .logoff = SMB2_logoff,
3656 .tree_connect = SMB2_tcon,
3657 .tree_disconnect = SMB2_tdis,
3658 .qfs_tcon = smb2_qfs_tcon,
3659 .is_path_accessible = smb2_is_path_accessible,
3660 .can_echo = smb2_can_echo,
3662 .query_path_info = smb2_query_path_info,
3663 .get_srv_inum = smb2_get_srv_inum,
3664 .query_file_info = smb2_query_file_info,
3665 .set_path_size = smb2_set_path_size,
3666 .set_file_size = smb2_set_file_size,
3667 .set_file_info = smb2_set_file_info,
3668 .set_compression = smb2_set_compression,
3669 .mkdir = smb2_mkdir,
3670 .mkdir_setinfo = smb2_mkdir_setinfo,
3671 .rmdir = smb2_rmdir,
3672 .unlink = smb2_unlink,
3673 .rename = smb2_rename_path,
3674 .create_hardlink = smb2_create_hardlink,
3675 .query_symlink = smb2_query_symlink,
3676 .query_mf_symlink = smb3_query_mf_symlink,
3677 .create_mf_symlink = smb3_create_mf_symlink,
3678 .open = smb2_open_file,
3679 .set_fid = smb2_set_fid,
3680 .close = smb2_close_file,
3681 .flush = smb2_flush_file,
3682 .async_readv = smb2_async_readv,
3683 .async_writev = smb2_async_writev,
3684 .sync_read = smb2_sync_read,
3685 .sync_write = smb2_sync_write,
3686 .query_dir_first = smb2_query_dir_first,
3687 .query_dir_next = smb2_query_dir_next,
3688 .close_dir = smb2_close_dir,
3689 .calc_smb_size = smb2_calc_size,
3690 .is_status_pending = smb2_is_status_pending,
3691 .is_session_expired = smb2_is_session_expired,
3692 .oplock_response = smb2_oplock_response,
3693 .queryfs = smb2_queryfs,
3694 .mand_lock = smb2_mand_lock,
3695 .mand_unlock_range = smb2_unlock_range,
3696 .push_mand_locks = smb2_push_mandatory_locks,
3697 .get_lease_key = smb2_get_lease_key,
3698 .set_lease_key = smb2_set_lease_key,
3699 .new_lease_key = smb2_new_lease_key,
3700 .calc_signature = smb2_calc_signature,
3701 .is_read_op = smb21_is_read_op,
3702 .set_oplock_level = smb21_set_oplock_level,
3703 .create_lease_buf = smb2_create_lease_buf,
3704 .parse_lease_buf = smb2_parse_lease_buf,
3705 .copychunk_range = smb2_copychunk_range,
3706 .wp_retry_size = smb2_wp_retry_size,
3707 .dir_needs_close = smb2_dir_needs_close,
3708 .enum_snapshots = smb3_enum_snapshots,
3709 .get_dfs_refer = smb2_get_dfs_refer,
3710 .select_sectype = smb2_select_sectype,
3711 #ifdef CONFIG_CIFS_XATTR
3712 .query_all_EAs = smb2_query_eas,
3713 .set_EA = smb2_set_ea,
3714 #endif /* CIFS_XATTR */
3715 #ifdef CONFIG_CIFS_ACL
3716 .get_acl = get_smb2_acl,
3717 .get_acl_by_fid = get_smb2_acl_by_fid,
3718 .set_acl = set_smb2_acl,
3719 #endif /* CIFS_ACL */
3720 .next_header = smb2_next_header,
3721 .ioctl_query_info = smb2_ioctl_query_info,
3724 struct smb_version_operations smb30_operations = {
3725 .compare_fids = smb2_compare_fids,
3726 .setup_request = smb2_setup_request,
3727 .setup_async_request = smb2_setup_async_request,
3728 .check_receive = smb2_check_receive,
3729 .add_credits = smb2_add_credits,
3730 .set_credits = smb2_set_credits,
3731 .get_credits_field = smb2_get_credits_field,
3732 .get_credits = smb2_get_credits,
3733 .wait_mtu_credits = smb2_wait_mtu_credits,
3734 .get_next_mid = smb2_get_next_mid,
3735 .read_data_offset = smb2_read_data_offset,
3736 .read_data_length = smb2_read_data_length,
3737 .map_error = map_smb2_to_linux_error,
3738 .find_mid = smb2_find_mid,
3739 .check_message = smb2_check_message,
3740 .dump_detail = smb2_dump_detail,
3741 .clear_stats = smb2_clear_stats,
3742 .print_stats = smb2_print_stats,
3743 .dump_share_caps = smb2_dump_share_caps,
3744 .is_oplock_break = smb2_is_valid_oplock_break,
3745 .handle_cancelled_mid = smb2_handle_cancelled_mid,
3746 .downgrade_oplock = smb2_downgrade_oplock,
3747 .need_neg = smb2_need_neg,
3748 .negotiate = smb2_negotiate,
3749 .negotiate_wsize = smb3_negotiate_wsize,
3750 .negotiate_rsize = smb3_negotiate_rsize,
3751 .sess_setup = SMB2_sess_setup,
3752 .logoff = SMB2_logoff,
3753 .tree_connect = SMB2_tcon,
3754 .tree_disconnect = SMB2_tdis,
3755 .qfs_tcon = smb3_qfs_tcon,
3756 .is_path_accessible = smb2_is_path_accessible,
3757 .can_echo = smb2_can_echo,
3759 .query_path_info = smb2_query_path_info,
3760 .get_srv_inum = smb2_get_srv_inum,
3761 .query_file_info = smb2_query_file_info,
3762 .set_path_size = smb2_set_path_size,
3763 .set_file_size = smb2_set_file_size,
3764 .set_file_info = smb2_set_file_info,
3765 .set_compression = smb2_set_compression,
3766 .mkdir = smb2_mkdir,
3767 .mkdir_setinfo = smb2_mkdir_setinfo,
3768 .rmdir = smb2_rmdir,
3769 .unlink = smb2_unlink,
3770 .rename = smb2_rename_path,
3771 .create_hardlink = smb2_create_hardlink,
3772 .query_symlink = smb2_query_symlink,
3773 .query_mf_symlink = smb3_query_mf_symlink,
3774 .create_mf_symlink = smb3_create_mf_symlink,
3775 .open = smb2_open_file,
3776 .set_fid = smb2_set_fid,
3777 .close = smb2_close_file,
3778 .flush = smb2_flush_file,
3779 .async_readv = smb2_async_readv,
3780 .async_writev = smb2_async_writev,
3781 .sync_read = smb2_sync_read,
3782 .sync_write = smb2_sync_write,
3783 .query_dir_first = smb2_query_dir_first,
3784 .query_dir_next = smb2_query_dir_next,
3785 .close_dir = smb2_close_dir,
3786 .calc_smb_size = smb2_calc_size,
3787 .is_status_pending = smb2_is_status_pending,
3788 .is_session_expired = smb2_is_session_expired,
3789 .oplock_response = smb2_oplock_response,
3790 .queryfs = smb2_queryfs,
3791 .mand_lock = smb2_mand_lock,
3792 .mand_unlock_range = smb2_unlock_range,
3793 .push_mand_locks = smb2_push_mandatory_locks,
3794 .get_lease_key = smb2_get_lease_key,
3795 .set_lease_key = smb2_set_lease_key,
3796 .new_lease_key = smb2_new_lease_key,
3797 .generate_signingkey = generate_smb30signingkey,
3798 .calc_signature = smb3_calc_signature,
3799 .set_integrity = smb3_set_integrity,
3800 .is_read_op = smb21_is_read_op,
3801 .set_oplock_level = smb3_set_oplock_level,
3802 .create_lease_buf = smb3_create_lease_buf,
3803 .parse_lease_buf = smb3_parse_lease_buf,
3804 .copychunk_range = smb2_copychunk_range,
3805 .duplicate_extents = smb2_duplicate_extents,
3806 .validate_negotiate = smb3_validate_negotiate,
3807 .wp_retry_size = smb2_wp_retry_size,
3808 .dir_needs_close = smb2_dir_needs_close,
3809 .fallocate = smb3_fallocate,
3810 .enum_snapshots = smb3_enum_snapshots,
3811 .init_transform_rq = smb3_init_transform_rq,
3812 .is_transform_hdr = smb3_is_transform_hdr,
3813 .receive_transform = smb3_receive_transform,
3814 .get_dfs_refer = smb2_get_dfs_refer,
3815 .select_sectype = smb2_select_sectype,
3816 #ifdef CONFIG_CIFS_XATTR
3817 .query_all_EAs = smb2_query_eas,
3818 .set_EA = smb2_set_ea,
3819 #endif /* CIFS_XATTR */
3820 #ifdef CONFIG_CIFS_ACL
3821 .get_acl = get_smb2_acl,
3822 .get_acl_by_fid = get_smb2_acl_by_fid,
3823 .set_acl = set_smb2_acl,
3824 #endif /* CIFS_ACL */
3825 .next_header = smb2_next_header,
3826 .ioctl_query_info = smb2_ioctl_query_info,
3829 struct smb_version_operations smb311_operations = {
3830 .compare_fids = smb2_compare_fids,
3831 .setup_request = smb2_setup_request,
3832 .setup_async_request = smb2_setup_async_request,
3833 .check_receive = smb2_check_receive,
3834 .add_credits = smb2_add_credits,
3835 .set_credits = smb2_set_credits,
3836 .get_credits_field = smb2_get_credits_field,
3837 .get_credits = smb2_get_credits,
3838 .wait_mtu_credits = smb2_wait_mtu_credits,
3839 .get_next_mid = smb2_get_next_mid,
3840 .read_data_offset = smb2_read_data_offset,
3841 .read_data_length = smb2_read_data_length,
3842 .map_error = map_smb2_to_linux_error,
3843 .find_mid = smb2_find_mid,
3844 .check_message = smb2_check_message,
3845 .dump_detail = smb2_dump_detail,
3846 .clear_stats = smb2_clear_stats,
3847 .print_stats = smb2_print_stats,
3848 .dump_share_caps = smb2_dump_share_caps,
3849 .is_oplock_break = smb2_is_valid_oplock_break,
3850 .handle_cancelled_mid = smb2_handle_cancelled_mid,
3851 .downgrade_oplock = smb2_downgrade_oplock,
3852 .need_neg = smb2_need_neg,
3853 .negotiate = smb2_negotiate,
3854 .negotiate_wsize = smb3_negotiate_wsize,
3855 .negotiate_rsize = smb3_negotiate_rsize,
3856 .sess_setup = SMB2_sess_setup,
3857 .logoff = SMB2_logoff,
3858 .tree_connect = SMB2_tcon,
3859 .tree_disconnect = SMB2_tdis,
3860 .qfs_tcon = smb3_qfs_tcon,
3861 .is_path_accessible = smb2_is_path_accessible,
3862 .can_echo = smb2_can_echo,
3864 .query_path_info = smb2_query_path_info,
3865 .get_srv_inum = smb2_get_srv_inum,
3866 .query_file_info = smb2_query_file_info,
3867 .set_path_size = smb2_set_path_size,
3868 .set_file_size = smb2_set_file_size,
3869 .set_file_info = smb2_set_file_info,
3870 .set_compression = smb2_set_compression,
3871 .mkdir = smb2_mkdir,
3872 .mkdir_setinfo = smb2_mkdir_setinfo,
3873 .posix_mkdir = smb311_posix_mkdir,
3874 .rmdir = smb2_rmdir,
3875 .unlink = smb2_unlink,
3876 .rename = smb2_rename_path,
3877 .create_hardlink = smb2_create_hardlink,
3878 .query_symlink = smb2_query_symlink,
3879 .query_mf_symlink = smb3_query_mf_symlink,
3880 .create_mf_symlink = smb3_create_mf_symlink,
3881 .open = smb2_open_file,
3882 .set_fid = smb2_set_fid,
3883 .close = smb2_close_file,
3884 .flush = smb2_flush_file,
3885 .async_readv = smb2_async_readv,
3886 .async_writev = smb2_async_writev,
3887 .sync_read = smb2_sync_read,
3888 .sync_write = smb2_sync_write,
3889 .query_dir_first = smb2_query_dir_first,
3890 .query_dir_next = smb2_query_dir_next,
3891 .close_dir = smb2_close_dir,
3892 .calc_smb_size = smb2_calc_size,
3893 .is_status_pending = smb2_is_status_pending,
3894 .is_session_expired = smb2_is_session_expired,
3895 .oplock_response = smb2_oplock_response,
3896 .queryfs = smb311_queryfs,
3897 .mand_lock = smb2_mand_lock,
3898 .mand_unlock_range = smb2_unlock_range,
3899 .push_mand_locks = smb2_push_mandatory_locks,
3900 .get_lease_key = smb2_get_lease_key,
3901 .set_lease_key = smb2_set_lease_key,
3902 .new_lease_key = smb2_new_lease_key,
3903 .generate_signingkey = generate_smb311signingkey,
3904 .calc_signature = smb3_calc_signature,
3905 .set_integrity = smb3_set_integrity,
3906 .is_read_op = smb21_is_read_op,
3907 .set_oplock_level = smb3_set_oplock_level,
3908 .create_lease_buf = smb3_create_lease_buf,
3909 .parse_lease_buf = smb3_parse_lease_buf,
3910 .copychunk_range = smb2_copychunk_range,
3911 .duplicate_extents = smb2_duplicate_extents,
3912 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
3913 .wp_retry_size = smb2_wp_retry_size,
3914 .dir_needs_close = smb2_dir_needs_close,
3915 .fallocate = smb3_fallocate,
3916 .enum_snapshots = smb3_enum_snapshots,
3917 .init_transform_rq = smb3_init_transform_rq,
3918 .is_transform_hdr = smb3_is_transform_hdr,
3919 .receive_transform = smb3_receive_transform,
3920 .get_dfs_refer = smb2_get_dfs_refer,
3921 .select_sectype = smb2_select_sectype,
3922 #ifdef CONFIG_CIFS_XATTR
3923 .query_all_EAs = smb2_query_eas,
3924 .set_EA = smb2_set_ea,
3925 #endif /* CIFS_XATTR */
3926 #ifdef CONFIG_CIFS_ACL
3927 .get_acl = get_smb2_acl,
3928 .get_acl_by_fid = get_smb2_acl_by_fid,
3929 .set_acl = set_smb2_acl,
3930 #endif /* CIFS_ACL */
3931 .next_header = smb2_next_header,
3932 .ioctl_query_info = smb2_ioctl_query_info,
3935 struct smb_version_values smb20_values = {
3936 .version_string = SMB20_VERSION_STRING,
3937 .protocol_id = SMB20_PROT_ID,
3938 .req_capabilities = 0, /* MBZ */
3939 .large_lock_type = 0,
3940 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3941 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3942 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3943 .header_size = sizeof(struct smb2_sync_hdr),
3944 .header_preamble_size = 0,
3945 .max_header_size = MAX_SMB2_HDR_SIZE,
3946 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3947 .lock_cmd = SMB2_LOCK,
3949 .cap_nt_find = SMB2_NT_FIND,
3950 .cap_large_files = SMB2_LARGE_FILES,
3951 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3952 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3953 .create_lease_size = sizeof(struct create_lease),
3956 struct smb_version_values smb21_values = {
3957 .version_string = SMB21_VERSION_STRING,
3958 .protocol_id = SMB21_PROT_ID,
3959 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
3960 .large_lock_type = 0,
3961 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3962 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3963 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3964 .header_size = sizeof(struct smb2_sync_hdr),
3965 .header_preamble_size = 0,
3966 .max_header_size = MAX_SMB2_HDR_SIZE,
3967 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3968 .lock_cmd = SMB2_LOCK,
3970 .cap_nt_find = SMB2_NT_FIND,
3971 .cap_large_files = SMB2_LARGE_FILES,
3972 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3973 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3974 .create_lease_size = sizeof(struct create_lease),
3977 struct smb_version_values smb3any_values = {
3978 .version_string = SMB3ANY_VERSION_STRING,
3979 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3980 .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,
3981 .large_lock_type = 0,
3982 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3983 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3984 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3985 .header_size = sizeof(struct smb2_sync_hdr),
3986 .header_preamble_size = 0,
3987 .max_header_size = MAX_SMB2_HDR_SIZE,
3988 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3989 .lock_cmd = SMB2_LOCK,
3991 .cap_nt_find = SMB2_NT_FIND,
3992 .cap_large_files = SMB2_LARGE_FILES,
3993 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3994 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3995 .create_lease_size = sizeof(struct create_lease_v2),
3998 struct smb_version_values smbdefault_values = {
3999 .version_string = SMBDEFAULT_VERSION_STRING,
4000 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
4001 .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,
4002 .large_lock_type = 0,
4003 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4004 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4005 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4006 .header_size = sizeof(struct smb2_sync_hdr),
4007 .header_preamble_size = 0,
4008 .max_header_size = MAX_SMB2_HDR_SIZE,
4009 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4010 .lock_cmd = SMB2_LOCK,
4012 .cap_nt_find = SMB2_NT_FIND,
4013 .cap_large_files = SMB2_LARGE_FILES,
4014 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4015 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4016 .create_lease_size = sizeof(struct create_lease_v2),
4019 struct smb_version_values smb30_values = {
4020 .version_string = SMB30_VERSION_STRING,
4021 .protocol_id = SMB30_PROT_ID,
4022 .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,
4023 .large_lock_type = 0,
4024 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4025 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4026 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4027 .header_size = sizeof(struct smb2_sync_hdr),
4028 .header_preamble_size = 0,
4029 .max_header_size = MAX_SMB2_HDR_SIZE,
4030 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4031 .lock_cmd = SMB2_LOCK,
4033 .cap_nt_find = SMB2_NT_FIND,
4034 .cap_large_files = SMB2_LARGE_FILES,
4035 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4036 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4037 .create_lease_size = sizeof(struct create_lease_v2),
4040 struct smb_version_values smb302_values = {
4041 .version_string = SMB302_VERSION_STRING,
4042 .protocol_id = SMB302_PROT_ID,
4043 .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,
4044 .large_lock_type = 0,
4045 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4046 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4047 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4048 .header_size = sizeof(struct smb2_sync_hdr),
4049 .header_preamble_size = 0,
4050 .max_header_size = MAX_SMB2_HDR_SIZE,
4051 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4052 .lock_cmd = SMB2_LOCK,
4054 .cap_nt_find = SMB2_NT_FIND,
4055 .cap_large_files = SMB2_LARGE_FILES,
4056 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4057 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4058 .create_lease_size = sizeof(struct create_lease_v2),
4061 struct smb_version_values smb311_values = {
4062 .version_string = SMB311_VERSION_STRING,
4063 .protocol_id = SMB311_PROT_ID,
4064 .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,
4065 .large_lock_type = 0,
4066 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4067 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4068 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4069 .header_size = sizeof(struct smb2_sync_hdr),
4070 .header_preamble_size = 0,
4071 .max_header_size = MAX_SMB2_HDR_SIZE,
4072 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4073 .lock_cmd = SMB2_LOCK,
4075 .cap_nt_find = SMB2_NT_FIND,
4076 .cap_large_files = SMB2_LARGE_FILES,
4077 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4078 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4079 .create_lease_size = sizeof(struct create_lease_v2),