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 &rsp_iov, &buftype, cifs_sb);
873 * If ea_name is NULL (listxattr) and there are no EAs,
874 * return 0 as it's not an error. Otherwise, the specified
875 * ea_name was not found.
877 if (!ea_name && rc == -ENODATA)
882 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
883 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
884 le32_to_cpu(rsp->OutputBufferLength),
886 sizeof(struct smb2_file_full_ea_info));
890 info = (struct smb2_file_full_ea_info *)(
891 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
892 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
893 le32_to_cpu(rsp->OutputBufferLength), ea_name);
897 free_rsp_buf(buftype, rsp_iov.iov_base);
903 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
904 const char *path, const char *ea_name, const void *ea_value,
905 const __u16 ea_value_len, const struct nls_table *nls_codepage,
906 struct cifs_sb_info *cifs_sb)
908 struct cifs_ses *ses = tcon->ses;
909 __le16 *utf16_path = NULL;
910 int ea_name_len = strlen(ea_name);
913 struct smb_rqst rqst[3];
915 struct kvec rsp_iov[3];
916 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
917 struct cifs_open_parms oparms;
918 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
920 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
921 unsigned int size[1];
923 struct smb2_file_full_ea_info *ea = NULL;
924 struct kvec close_iov[1];
927 if (smb3_encryption_required(tcon))
928 flags |= CIFS_TRANSFORM_REQ;
930 if (ea_name_len > 255)
933 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
937 memset(rqst, 0, sizeof(rqst));
938 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
939 memset(rsp_iov, 0, sizeof(rsp_iov));
942 memset(&open_iov, 0, sizeof(open_iov));
943 rqst[0].rq_iov = open_iov;
944 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
946 memset(&oparms, 0, sizeof(oparms));
948 oparms.desired_access = FILE_WRITE_EA;
949 oparms.disposition = FILE_OPEN;
950 if (backup_cred(cifs_sb))
951 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
953 oparms.create_options = 0;
955 oparms.reconnect = false;
957 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
960 smb2_set_next_command(tcon, &rqst[0]);
964 memset(&si_iov, 0, sizeof(si_iov));
965 rqst[1].rq_iov = si_iov;
968 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
969 ea = kzalloc(len, GFP_KERNEL);
975 ea->ea_name_length = ea_name_len;
976 ea->ea_value_length = cpu_to_le16(ea_value_len);
977 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
978 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
983 rc = SMB2_set_info_init(tcon, &rqst[1], COMPOUND_FID,
984 COMPOUND_FID, current->tgid,
985 FILE_FULL_EA_INFORMATION,
986 SMB2_O_INFO_FILE, 0, data, size);
987 smb2_set_next_command(tcon, &rqst[1]);
988 smb2_set_related(&rqst[1]);
992 memset(&close_iov, 0, sizeof(close_iov));
993 rqst[2].rq_iov = close_iov;
995 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
996 smb2_set_related(&rqst[2]);
998 rc = compound_send_recv(xid, ses, flags, 3, rqst,
999 resp_buftype, rsp_iov);
1004 SMB2_open_free(&rqst[0]);
1005 SMB2_set_info_free(&rqst[1]);
1006 SMB2_close_free(&rqst[2]);
1007 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1008 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1009 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1015 smb2_can_echo(struct TCP_Server_Info *server)
1017 return server->echoes;
1021 smb2_clear_stats(struct cifs_tcon *tcon)
1024 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1025 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1026 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1031 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1033 seq_puts(m, "\n\tShare Capabilities:");
1034 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1035 seq_puts(m, " DFS,");
1036 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1037 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1038 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1039 seq_puts(m, " SCALEOUT,");
1040 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1041 seq_puts(m, " CLUSTER,");
1042 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1043 seq_puts(m, " ASYMMETRIC,");
1044 if (tcon->capabilities == 0)
1045 seq_puts(m, " None");
1046 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1047 seq_puts(m, " Aligned,");
1048 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1049 seq_puts(m, " Partition Aligned,");
1050 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1051 seq_puts(m, " SSD,");
1052 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1053 seq_puts(m, " TRIM-support,");
1055 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1056 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1057 if (tcon->perf_sector_size)
1058 seq_printf(m, "\tOptimal sector size: 0x%x",
1059 tcon->perf_sector_size);
1060 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1064 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1066 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1067 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1070 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1071 * totals (requests sent) since those SMBs are per-session not per tcon
1073 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1074 (long long)(tcon->bytes_read),
1075 (long long)(tcon->bytes_written));
1076 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1077 atomic_read(&tcon->num_local_opens),
1078 atomic_read(&tcon->num_remote_opens));
1079 seq_printf(m, "\nTreeConnects: %d total %d failed",
1080 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1081 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1082 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1083 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1084 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1085 seq_printf(m, "\nCreates: %d total %d failed",
1086 atomic_read(&sent[SMB2_CREATE_HE]),
1087 atomic_read(&failed[SMB2_CREATE_HE]));
1088 seq_printf(m, "\nCloses: %d total %d failed",
1089 atomic_read(&sent[SMB2_CLOSE_HE]),
1090 atomic_read(&failed[SMB2_CLOSE_HE]));
1091 seq_printf(m, "\nFlushes: %d total %d failed",
1092 atomic_read(&sent[SMB2_FLUSH_HE]),
1093 atomic_read(&failed[SMB2_FLUSH_HE]));
1094 seq_printf(m, "\nReads: %d total %d failed",
1095 atomic_read(&sent[SMB2_READ_HE]),
1096 atomic_read(&failed[SMB2_READ_HE]));
1097 seq_printf(m, "\nWrites: %d total %d failed",
1098 atomic_read(&sent[SMB2_WRITE_HE]),
1099 atomic_read(&failed[SMB2_WRITE_HE]));
1100 seq_printf(m, "\nLocks: %d total %d failed",
1101 atomic_read(&sent[SMB2_LOCK_HE]),
1102 atomic_read(&failed[SMB2_LOCK_HE]));
1103 seq_printf(m, "\nIOCTLs: %d total %d failed",
1104 atomic_read(&sent[SMB2_IOCTL_HE]),
1105 atomic_read(&failed[SMB2_IOCTL_HE]));
1106 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1107 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1108 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1109 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1110 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1111 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1112 seq_printf(m, "\nQueryInfos: %d total %d failed",
1113 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1114 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1115 seq_printf(m, "\nSetInfos: %d total %d failed",
1116 atomic_read(&sent[SMB2_SET_INFO_HE]),
1117 atomic_read(&failed[SMB2_SET_INFO_HE]));
1118 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1119 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1120 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1124 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1126 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1127 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1129 cfile->fid.persistent_fid = fid->persistent_fid;
1130 cfile->fid.volatile_fid = fid->volatile_fid;
1131 #ifdef CONFIG_CIFS_DEBUG2
1132 cfile->fid.mid = fid->mid;
1133 #endif /* CIFS_DEBUG2 */
1134 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1136 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1137 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1141 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1142 struct cifs_fid *fid)
1144 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1148 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1149 u64 persistent_fid, u64 volatile_fid,
1150 struct copychunk_ioctl *pcchunk)
1153 unsigned int ret_data_len;
1154 struct resume_key_req *res_key;
1156 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1157 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1158 NULL, 0 /* no input */,
1159 (char **)&res_key, &ret_data_len);
1162 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1163 goto req_res_key_exit;
1165 if (ret_data_len < sizeof(struct resume_key_req)) {
1166 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
1168 goto req_res_key_exit;
1170 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1178 smb2_ioctl_query_info(const unsigned int xid,
1179 struct cifs_tcon *tcon,
1180 __le16 *path, int is_dir,
1183 struct cifs_ses *ses = tcon->ses;
1184 char __user *arg = (char __user *)p;
1185 struct smb_query_info qi;
1186 struct smb_query_info __user *pqi;
1189 struct smb2_query_info_rsp *rsp = NULL;
1190 void *buffer = NULL;
1191 struct smb_rqst rqst[3];
1192 int resp_buftype[3];
1193 struct kvec rsp_iov[3];
1194 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1195 struct cifs_open_parms oparms;
1196 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1197 struct cifs_fid fid;
1198 struct kvec qi_iov[1];
1199 struct kvec close_iov[1];
1201 memset(rqst, 0, sizeof(rqst));
1202 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1203 memset(rsp_iov, 0, sizeof(rsp_iov));
1205 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1208 if (qi.output_buffer_length > 1024)
1211 if (!ses || !(ses->server))
1214 if (smb3_encryption_required(tcon))
1215 flags |= CIFS_TRANSFORM_REQ;
1217 buffer = kmalloc(qi.output_buffer_length, GFP_KERNEL);
1221 if (copy_from_user(buffer, arg + sizeof(struct smb_query_info),
1222 qi.output_buffer_length)) {
1228 memset(&open_iov, 0, sizeof(open_iov));
1229 rqst[0].rq_iov = open_iov;
1230 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1232 memset(&oparms, 0, sizeof(oparms));
1234 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1235 oparms.disposition = FILE_OPEN;
1237 oparms.create_options = CREATE_NOT_FILE;
1239 oparms.create_options = CREATE_NOT_DIR;
1241 oparms.reconnect = false;
1243 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, path);
1246 smb2_set_next_command(tcon, &rqst[0]);
1249 memset(&qi_iov, 0, sizeof(qi_iov));
1250 rqst[1].rq_iov = qi_iov;
1251 rqst[1].rq_nvec = 1;
1253 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1254 qi.file_info_class, qi.info_type,
1255 qi.additional_information,
1256 qi.input_buffer_length,
1257 qi.output_buffer_length, buffer);
1260 smb2_set_next_command(tcon, &rqst[1]);
1261 smb2_set_related(&rqst[1]);
1264 memset(&close_iov, 0, sizeof(close_iov));
1265 rqst[2].rq_iov = close_iov;
1266 rqst[2].rq_nvec = 1;
1268 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1271 smb2_set_related(&rqst[2]);
1273 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1274 resp_buftype, rsp_iov);
1277 pqi = (struct smb_query_info __user *)arg;
1278 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1279 if (le32_to_cpu(rsp->OutputBufferLength) < qi.input_buffer_length)
1280 qi.input_buffer_length = le32_to_cpu(rsp->OutputBufferLength);
1281 if (copy_to_user(&pqi->input_buffer_length, &qi.input_buffer_length,
1282 sizeof(qi.input_buffer_length))) {
1286 if (copy_to_user(pqi + 1, rsp->Buffer, qi.input_buffer_length)) {
1293 SMB2_open_free(&rqst[0]);
1294 SMB2_query_info_free(&rqst[1]);
1295 SMB2_close_free(&rqst[2]);
1296 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1297 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1298 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1303 smb2_copychunk_range(const unsigned int xid,
1304 struct cifsFileInfo *srcfile,
1305 struct cifsFileInfo *trgtfile, u64 src_off,
1306 u64 len, u64 dest_off)
1309 unsigned int ret_data_len;
1310 struct copychunk_ioctl *pcchunk;
1311 struct copychunk_ioctl_rsp *retbuf = NULL;
1312 struct cifs_tcon *tcon;
1313 int chunks_copied = 0;
1314 bool chunk_sizes_updated = false;
1315 ssize_t bytes_written, total_bytes_written = 0;
1317 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1319 if (pcchunk == NULL)
1322 cifs_dbg(FYI, "in smb2_copychunk_range - about to call request res key\n");
1323 /* Request a key from the server to identify the source of the copy */
1324 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1325 srcfile->fid.persistent_fid,
1326 srcfile->fid.volatile_fid, pcchunk);
1328 /* Note: request_res_key sets res_key null only if rc !=0 */
1332 /* For now array only one chunk long, will make more flexible later */
1333 pcchunk->ChunkCount = cpu_to_le32(1);
1334 pcchunk->Reserved = 0;
1335 pcchunk->Reserved2 = 0;
1337 tcon = tlink_tcon(trgtfile->tlink);
1340 pcchunk->SourceOffset = cpu_to_le64(src_off);
1341 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1343 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1345 /* Request server copy to target from src identified by key */
1346 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1347 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1348 true /* is_fsctl */, (char *)pcchunk,
1349 sizeof(struct copychunk_ioctl), (char **)&retbuf,
1353 sizeof(struct copychunk_ioctl_rsp)) {
1354 cifs_dbg(VFS, "invalid cchunk response size\n");
1358 if (retbuf->TotalBytesWritten == 0) {
1359 cifs_dbg(FYI, "no bytes copied\n");
1364 * Check if server claimed to write more than we asked
1366 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1367 le32_to_cpu(pcchunk->Length)) {
1368 cifs_dbg(VFS, "invalid copy chunk response\n");
1372 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1373 cifs_dbg(VFS, "invalid num chunks written\n");
1379 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1380 src_off += bytes_written;
1381 dest_off += bytes_written;
1382 len -= bytes_written;
1383 total_bytes_written += bytes_written;
1385 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1386 le32_to_cpu(retbuf->ChunksWritten),
1387 le32_to_cpu(retbuf->ChunkBytesWritten),
1389 } else if (rc == -EINVAL) {
1390 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1393 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1394 le32_to_cpu(retbuf->ChunksWritten),
1395 le32_to_cpu(retbuf->ChunkBytesWritten),
1396 le32_to_cpu(retbuf->TotalBytesWritten));
1399 * Check if this is the first request using these sizes,
1400 * (ie check if copy succeed once with original sizes
1401 * and check if the server gave us different sizes after
1402 * we already updated max sizes on previous request).
1403 * if not then why is the server returning an error now
1405 if ((chunks_copied != 0) || chunk_sizes_updated)
1408 /* Check that server is not asking us to grow size */
1409 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1410 tcon->max_bytes_chunk)
1411 tcon->max_bytes_chunk =
1412 le32_to_cpu(retbuf->ChunkBytesWritten);
1414 goto cchunk_out; /* server gave us bogus size */
1416 /* No need to change MaxChunks since already set to 1 */
1417 chunk_sizes_updated = true;
1428 return total_bytes_written;
1432 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1433 struct cifs_fid *fid)
1435 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1439 smb2_read_data_offset(char *buf)
1441 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1442 return rsp->DataOffset;
1446 smb2_read_data_length(char *buf, bool in_remaining)
1448 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1451 return le32_to_cpu(rsp->DataRemaining);
1453 return le32_to_cpu(rsp->DataLength);
1458 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1459 struct cifs_io_parms *parms, unsigned int *bytes_read,
1460 char **buf, int *buf_type)
1462 parms->persistent_fid = pfid->persistent_fid;
1463 parms->volatile_fid = pfid->volatile_fid;
1464 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1468 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1469 struct cifs_io_parms *parms, unsigned int *written,
1470 struct kvec *iov, unsigned long nr_segs)
1473 parms->persistent_fid = pfid->persistent_fid;
1474 parms->volatile_fid = pfid->volatile_fid;
1475 return SMB2_write(xid, parms, written, iov, nr_segs);
1478 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1479 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1480 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1482 struct cifsInodeInfo *cifsi;
1485 cifsi = CIFS_I(inode);
1487 /* if file already sparse don't bother setting sparse again */
1488 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1489 return true; /* already sparse */
1491 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1492 return true; /* already not sparse */
1495 * Can't check for sparse support on share the usual way via the
1496 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1497 * since Samba server doesn't set the flag on the share, yet
1498 * supports the set sparse FSCTL and returns sparse correctly
1499 * in the file attributes. If we fail setting sparse though we
1500 * mark that server does not support sparse files for this share
1501 * to avoid repeatedly sending the unsupported fsctl to server
1502 * if the file is repeatedly extended.
1504 if (tcon->broken_sparse_sup)
1507 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1508 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1510 &setsparse, 1, NULL, NULL);
1512 tcon->broken_sparse_sup = true;
1513 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1518 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1520 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1526 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1527 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1529 __le64 eof = cpu_to_le64(size);
1530 struct inode *inode;
1533 * If extending file more than one page make sparse. Many Linux fs
1534 * make files sparse by default when extending via ftruncate
1536 inode = d_inode(cfile->dentry);
1538 if (!set_alloc && (size > inode->i_size + 8192)) {
1539 __u8 set_sparse = 1;
1541 /* whether set sparse succeeds or not, extend the file */
1542 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1545 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1546 cfile->fid.volatile_fid, cfile->pid, &eof);
1550 smb2_duplicate_extents(const unsigned int xid,
1551 struct cifsFileInfo *srcfile,
1552 struct cifsFileInfo *trgtfile, u64 src_off,
1553 u64 len, u64 dest_off)
1556 unsigned int ret_data_len;
1557 struct duplicate_extents_to_file dup_ext_buf;
1558 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1560 /* server fileays advertise duplicate extent support with this flag */
1561 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1562 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1565 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1566 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1567 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1568 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1569 dup_ext_buf.ByteCount = cpu_to_le64(len);
1570 cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
1571 src_off, dest_off, len);
1573 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1575 goto duplicate_extents_out;
1577 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1578 trgtfile->fid.volatile_fid,
1579 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
1580 true /* is_fsctl */,
1581 (char *)&dup_ext_buf,
1582 sizeof(struct duplicate_extents_to_file),
1586 if (ret_data_len > 0)
1587 cifs_dbg(FYI, "non-zero response length in duplicate extents");
1589 duplicate_extents_out:
1594 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1595 struct cifsFileInfo *cfile)
1597 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1598 cfile->fid.volatile_fid);
1602 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1603 struct cifsFileInfo *cfile)
1605 struct fsctl_set_integrity_information_req integr_info;
1606 unsigned int ret_data_len;
1608 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1609 integr_info.Flags = 0;
1610 integr_info.Reserved = 0;
1612 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1613 cfile->fid.volatile_fid,
1614 FSCTL_SET_INTEGRITY_INFORMATION,
1615 true /* is_fsctl */,
1616 (char *)&integr_info,
1617 sizeof(struct fsctl_set_integrity_information_req),
1623 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
1624 #define GMT_TOKEN_SIZE 50
1627 * Input buffer contains (empty) struct smb_snapshot array with size filled in
1628 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
1631 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1632 struct cifsFileInfo *cfile, void __user *ioc_buf)
1634 char *retbuf = NULL;
1635 unsigned int ret_data_len = 0;
1637 struct smb_snapshot_array snapshot_in;
1639 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1640 cfile->fid.volatile_fid,
1641 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
1642 true /* is_fsctl */,
1643 NULL, 0 /* no input data */,
1646 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1651 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1653 if (copy_from_user(&snapshot_in, ioc_buf,
1654 sizeof(struct smb_snapshot_array))) {
1661 * Check for min size, ie not large enough to fit even one GMT
1662 * token (snapshot). On the first ioctl some users may pass in
1663 * smaller size (or zero) to simply get the size of the array
1664 * so the user space caller can allocate sufficient memory
1665 * and retry the ioctl again with larger array size sufficient
1666 * to hold all of the snapshot GMT tokens on the second try.
1668 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
1669 ret_data_len = sizeof(struct smb_snapshot_array);
1672 * We return struct SRV_SNAPSHOT_ARRAY, followed by
1673 * the snapshot array (of 50 byte GMT tokens) each
1674 * representing an available previous version of the data
1676 if (ret_data_len > (snapshot_in.snapshot_array_size +
1677 sizeof(struct smb_snapshot_array)))
1678 ret_data_len = snapshot_in.snapshot_array_size +
1679 sizeof(struct smb_snapshot_array);
1681 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1690 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1691 const char *path, struct cifs_sb_info *cifs_sb,
1692 struct cifs_fid *fid, __u16 search_flags,
1693 struct cifs_search_info *srch_inf)
1697 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1698 struct cifs_open_parms oparms;
1700 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1705 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1706 oparms.disposition = FILE_OPEN;
1707 if (backup_cred(cifs_sb))
1708 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1710 oparms.create_options = 0;
1712 oparms.reconnect = false;
1714 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
1717 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
1721 srch_inf->entries_in_buffer = 0;
1722 srch_inf->index_of_last_entry = 2;
1724 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1725 fid->volatile_fid, 0, srch_inf);
1727 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
1728 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1734 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1735 struct cifs_fid *fid, __u16 search_flags,
1736 struct cifs_search_info *srch_inf)
1738 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1739 fid->volatile_fid, 0, srch_inf);
1743 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1744 struct cifs_fid *fid)
1746 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1750 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
1751 * the number of credits and return true. Otherwise - return false.
1754 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
1756 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
1758 if (shdr->Status != STATUS_PENDING)
1762 spin_lock(&server->req_lock);
1763 server->credits += le16_to_cpu(shdr->CreditRequest);
1764 spin_unlock(&server->req_lock);
1765 wake_up(&server->request_q);
1772 smb2_is_session_expired(char *buf)
1774 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
1776 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
1777 shdr->Status != STATUS_USER_SESSION_DELETED)
1780 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
1781 le16_to_cpu(shdr->Command),
1782 le64_to_cpu(shdr->MessageId));
1783 cifs_dbg(FYI, "Session expired or deleted\n");
1789 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
1790 struct cifsInodeInfo *cinode)
1792 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
1793 return SMB2_lease_break(0, tcon, cinode->lease_key,
1794 smb2_get_lease_state(cinode));
1796 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
1798 CIFS_CACHE_READ(cinode) ? 1 : 0);
1802 smb2_set_related(struct smb_rqst *rqst)
1804 struct smb2_sync_hdr *shdr;
1806 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
1807 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
1810 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
1813 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
1815 struct smb2_sync_hdr *shdr;
1816 struct cifs_ses *ses = tcon->ses;
1817 struct TCP_Server_Info *server = ses->server;
1818 unsigned long len = smb_rqst_len(server, rqst);
1821 /* SMB headers in a compound are 8 byte aligned. */
1823 /* No padding needed */
1827 num_padding = 8 - (len & 7);
1828 if (!smb3_encryption_required(tcon)) {
1830 * If we do not have encryption then we can just add an extra
1831 * iov for the padding.
1833 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
1834 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
1839 * We can not add a small padding iov for the encryption case
1840 * because the encryption framework can not handle the padding
1842 * We have to flatten this into a single buffer and add
1843 * the padding to it.
1845 for (i = 1; i < rqst->rq_nvec; i++) {
1846 memcpy(rqst->rq_iov[0].iov_base +
1847 rqst->rq_iov[0].iov_len,
1848 rqst->rq_iov[i].iov_base,
1849 rqst->rq_iov[i].iov_len);
1850 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
1852 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
1854 rqst->rq_iov[0].iov_len += num_padding;
1860 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
1861 shdr->NextCommand = cpu_to_le32(len);
1865 * Passes the query info response back to the caller on success.
1866 * Caller need to free this with free_rsp_buf().
1869 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
1870 __le16 *utf16_path, u32 desired_access,
1871 u32 class, u32 type, u32 output_len,
1872 struct kvec *rsp, int *buftype,
1873 struct cifs_sb_info *cifs_sb)
1875 struct cifs_ses *ses = tcon->ses;
1877 struct smb_rqst rqst[3];
1878 int resp_buftype[3];
1879 struct kvec rsp_iov[3];
1880 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1881 struct kvec qi_iov[1];
1882 struct kvec close_iov[1];
1883 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1884 struct cifs_open_parms oparms;
1885 struct cifs_fid fid;
1888 if (smb3_encryption_required(tcon))
1889 flags |= CIFS_TRANSFORM_REQ;
1891 memset(rqst, 0, sizeof(rqst));
1892 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1893 memset(rsp_iov, 0, sizeof(rsp_iov));
1895 memset(&open_iov, 0, sizeof(open_iov));
1896 rqst[0].rq_iov = open_iov;
1897 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1900 oparms.desired_access = desired_access;
1901 oparms.disposition = FILE_OPEN;
1902 if (cifs_sb && backup_cred(cifs_sb))
1903 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1905 oparms.create_options = 0;
1907 oparms.reconnect = false;
1909 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
1912 smb2_set_next_command(tcon, &rqst[0]);
1914 memset(&qi_iov, 0, sizeof(qi_iov));
1915 rqst[1].rq_iov = qi_iov;
1916 rqst[1].rq_nvec = 1;
1918 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1924 smb2_set_next_command(tcon, &rqst[1]);
1925 smb2_set_related(&rqst[1]);
1927 memset(&close_iov, 0, sizeof(close_iov));
1928 rqst[2].rq_iov = close_iov;
1929 rqst[2].rq_nvec = 1;
1931 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1934 smb2_set_related(&rqst[2]);
1936 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1937 resp_buftype, rsp_iov);
1939 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1943 *buftype = resp_buftype[1];
1946 SMB2_open_free(&rqst[0]);
1947 SMB2_query_info_free(&rqst[1]);
1948 SMB2_close_free(&rqst[2]);
1949 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1950 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1955 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1956 struct kstatfs *buf)
1958 struct smb2_query_info_rsp *rsp;
1959 struct smb2_fs_full_size_info *info = NULL;
1960 __le16 utf16_path = 0; /* Null - open root of share */
1961 struct kvec rsp_iov = {NULL, 0};
1962 int buftype = CIFS_NO_BUFFER;
1966 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
1967 FILE_READ_ATTRIBUTES,
1968 FS_FULL_SIZE_INFORMATION,
1969 SMB2_O_INFO_FILESYSTEM,
1970 sizeof(struct smb2_fs_full_size_info),
1971 &rsp_iov, &buftype, NULL);
1975 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1976 buf->f_type = SMB2_MAGIC_NUMBER;
1977 info = (struct smb2_fs_full_size_info *)(
1978 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1979 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1980 le32_to_cpu(rsp->OutputBufferLength),
1982 sizeof(struct smb2_fs_full_size_info));
1984 smb2_copy_fs_info_to_kstatfs(info, buf);
1987 free_rsp_buf(buftype, rsp_iov.iov_base);
1992 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1993 struct kstatfs *buf)
1996 __le16 srch_path = 0; /* Null - open root of share */
1997 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1998 struct cifs_open_parms oparms;
1999 struct cifs_fid fid;
2001 if (!tcon->posix_extensions)
2002 return smb2_queryfs(xid, tcon, buf);
2005 oparms.desired_access = FILE_READ_ATTRIBUTES;
2006 oparms.disposition = FILE_OPEN;
2007 oparms.create_options = 0;
2009 oparms.reconnect = false;
2011 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
2015 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2016 fid.volatile_fid, buf);
2017 buf->f_type = SMB2_MAGIC_NUMBER;
2018 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2023 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2025 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2026 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2030 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2031 __u64 length, __u32 type, int lock, int unlock, bool wait)
2033 if (unlock && !lock)
2034 type = SMB2_LOCKFLAG_UNLOCK;
2035 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2036 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2037 current->tgid, length, offset, type, wait);
2041 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2043 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2047 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2049 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2053 smb2_new_lease_key(struct cifs_fid *fid)
2055 generate_random_uuid(fid->lease_key);
2059 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2060 const char *search_name,
2061 struct dfs_info3_param **target_nodes,
2062 unsigned int *num_of_nodes,
2063 const struct nls_table *nls_codepage, int remap)
2066 __le16 *utf16_path = NULL;
2067 int utf16_path_len = 0;
2068 struct cifs_tcon *tcon;
2069 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2070 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2071 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2073 cifs_dbg(FYI, "smb2_get_dfs_refer path <%s>\n", search_name);
2076 * Try to use the IPC tcon, otherwise just use any
2078 tcon = ses->tcon_ipc;
2080 spin_lock(&cifs_tcp_ses_lock);
2081 tcon = list_first_entry_or_null(&ses->tcon_list,
2086 spin_unlock(&cifs_tcp_ses_lock);
2090 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2096 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2098 nls_codepage, remap);
2104 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2105 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2111 /* Highest DFS referral version understood */
2112 dfs_req->MaxReferralLevel = DFS_VERSION;
2114 /* Path to resolve in an UTF-16 null-terminated string */
2115 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2118 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2119 FSCTL_DFS_GET_REFERRALS,
2120 true /* is_fsctl */,
2121 (char *)dfs_req, dfs_req_size,
2122 (char **)&dfs_rsp, &dfs_rsp_size);
2123 } while (rc == -EAGAIN);
2126 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
2127 cifs_dbg(VFS, "ioctl error in smb2_get_dfs_refer rc=%d\n", rc);
2131 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2132 num_of_nodes, target_nodes,
2133 nls_codepage, remap, search_name,
2134 true /* is_unicode */);
2136 cifs_dbg(VFS, "parse error in smb2_get_dfs_refer rc=%d\n", rc);
2141 if (tcon && !tcon->ipc) {
2142 /* ipc tcons are not refcounted */
2143 spin_lock(&cifs_tcp_ses_lock);
2145 spin_unlock(&cifs_tcp_ses_lock);
2152 #define SMB2_SYMLINK_STRUCT_SIZE \
2153 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2156 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
2157 const char *full_path, char **target_path,
2158 struct cifs_sb_info *cifs_sb)
2162 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2163 struct cifs_open_parms oparms;
2164 struct cifs_fid fid;
2165 struct kvec err_iov = {NULL, 0};
2166 struct smb2_err_rsp *err_buf = NULL;
2168 struct smb2_symlink_err_rsp *symlink;
2169 unsigned int sub_len;
2170 unsigned int sub_offset;
2171 unsigned int print_len;
2172 unsigned int print_offset;
2174 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2176 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2181 oparms.desired_access = FILE_READ_ATTRIBUTES;
2182 oparms.disposition = FILE_OPEN;
2183 if (backup_cred(cifs_sb))
2184 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2186 oparms.create_options = 0;
2188 oparms.reconnect = false;
2190 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_iov,
2192 if (!rc || !err_iov.iov_base) {
2197 err_buf = err_iov.iov_base;
2198 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
2199 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
2204 /* open must fail on symlink - reset rc */
2206 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
2207 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
2208 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
2209 print_len = le16_to_cpu(symlink->PrintNameLength);
2210 print_offset = le16_to_cpu(symlink->PrintNameOffset);
2212 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
2217 if (err_iov.iov_len <
2218 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
2223 *target_path = cifs_strndup_from_utf16(
2224 (char *)symlink->PathBuffer + sub_offset,
2225 sub_len, true, cifs_sb->local_nls);
2226 if (!(*target_path)) {
2230 convert_delimiter(*target_path, '/');
2231 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2234 free_rsp_buf(resp_buftype, err_buf);
2240 #ifdef CONFIG_CIFS_ACL
2241 static struct cifs_ntsd *
2242 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
2243 const struct cifs_fid *cifsfid, u32 *pacllen)
2245 struct cifs_ntsd *pntsd = NULL;
2247 int rc = -EOPNOTSUPP;
2248 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2251 return ERR_CAST(tlink);
2254 cifs_dbg(FYI, "trying to get acl\n");
2256 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
2257 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
2260 cifs_put_tlink(tlink);
2262 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2269 static struct cifs_ntsd *
2270 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
2271 const char *path, u32 *pacllen)
2273 struct cifs_ntsd *pntsd = NULL;
2274 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2277 struct cifs_tcon *tcon;
2278 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2279 struct cifs_fid fid;
2280 struct cifs_open_parms oparms;
2283 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
2285 return ERR_CAST(tlink);
2287 tcon = tlink_tcon(tlink);
2290 if (backup_cred(cifs_sb))
2291 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2293 oparms.create_options = 0;
2295 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2303 oparms.desired_access = READ_CONTROL;
2304 oparms.disposition = FILE_OPEN;
2306 oparms.reconnect = false;
2308 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2311 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2312 fid.volatile_fid, (void **)&pntsd, pacllen);
2313 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2316 cifs_put_tlink(tlink);
2319 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2325 #ifdef CONFIG_CIFS_ACL
2327 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
2328 struct inode *inode, const char *path, int aclflag)
2330 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2332 int rc, access_flags = 0;
2333 struct cifs_tcon *tcon;
2334 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2335 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2336 struct cifs_fid fid;
2337 struct cifs_open_parms oparms;
2340 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
2342 return PTR_ERR(tlink);
2344 tcon = tlink_tcon(tlink);
2347 if (backup_cred(cifs_sb))
2348 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2350 oparms.create_options = 0;
2352 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
2353 access_flags = WRITE_OWNER;
2355 access_flags = WRITE_DAC;
2357 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2365 oparms.desired_access = access_flags;
2366 oparms.disposition = FILE_OPEN;
2369 oparms.reconnect = false;
2371 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2374 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2375 fid.volatile_fid, pnntsd, acllen, aclflag);
2376 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2379 cifs_put_tlink(tlink);
2383 #endif /* CIFS_ACL */
2385 /* Retrieve an ACL from the server */
2386 static struct cifs_ntsd *
2387 get_smb2_acl(struct cifs_sb_info *cifs_sb,
2388 struct inode *inode, const char *path,
2391 struct cifs_ntsd *pntsd = NULL;
2392 struct cifsFileInfo *open_file = NULL;
2395 open_file = find_readable_file(CIFS_I(inode), true);
2397 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
2399 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
2400 cifsFileInfo_put(open_file);
2405 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
2406 loff_t offset, loff_t len, bool keep_size)
2408 struct inode *inode;
2409 struct cifsInodeInfo *cifsi;
2410 struct cifsFileInfo *cfile = file->private_data;
2411 struct file_zero_data_information fsctl_buf;
2417 inode = d_inode(cfile->dentry);
2418 cifsi = CIFS_I(inode);
2420 /* if file not oplocked can't be sure whether asking to extend size */
2421 if (!CIFS_CACHE_READ(cifsi))
2422 if (keep_size == false) {
2429 * Must check if file sparse since fallocate -z (zero range) assumes
2430 * non-sparse allocation
2432 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
2439 * need to make sure we are not asked to extend the file since the SMB3
2440 * fsctl does not change the file size. In the future we could change
2441 * this to zero the first part of the range then set the file size
2442 * which for a non sparse file would zero the newly extended range
2444 if (keep_size == false)
2445 if (i_size_read(inode) < offset + len) {
2451 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
2453 fsctl_buf.FileOffset = cpu_to_le64(offset);
2454 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2456 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2457 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
2458 true /* is_fctl */, (char *)&fsctl_buf,
2459 sizeof(struct file_zero_data_information), NULL, NULL);
2464 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
2465 loff_t offset, loff_t len)
2467 struct inode *inode;
2468 struct cifsInodeInfo *cifsi;
2469 struct cifsFileInfo *cfile = file->private_data;
2470 struct file_zero_data_information fsctl_buf;
2473 __u8 set_sparse = 1;
2477 inode = d_inode(cfile->dentry);
2478 cifsi = CIFS_I(inode);
2480 /* Need to make file sparse, if not already, before freeing range. */
2481 /* Consider adding equivalent for compressed since it could also work */
2482 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
2488 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
2490 fsctl_buf.FileOffset = cpu_to_le64(offset);
2491 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2493 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2494 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
2495 true /* is_fctl */, (char *)&fsctl_buf,
2496 sizeof(struct file_zero_data_information), NULL, NULL);
2501 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
2502 loff_t off, loff_t len, bool keep_size)
2504 struct inode *inode;
2505 struct cifsInodeInfo *cifsi;
2506 struct cifsFileInfo *cfile = file->private_data;
2507 long rc = -EOPNOTSUPP;
2512 inode = d_inode(cfile->dentry);
2513 cifsi = CIFS_I(inode);
2515 /* if file not oplocked can't be sure whether asking to extend size */
2516 if (!CIFS_CACHE_READ(cifsi))
2517 if (keep_size == false) {
2523 * Files are non-sparse by default so falloc may be a no-op
2524 * Must check if file sparse. If not sparse, and not extending
2525 * then no need to do anything since file already allocated
2527 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
2528 if (keep_size == true)
2530 /* check if extending file */
2531 else if (i_size_read(inode) >= off + len)
2532 /* not extending file and already not sparse */
2534 /* BB: in future add else clause to extend file */
2541 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
2543 * Check if falloc starts within first few pages of file
2544 * and ends within a few pages of the end of file to
2545 * ensure that most of file is being forced to be
2546 * fallocated now. If so then setting whole file sparse
2547 * ie potentially making a few extra pages at the beginning
2548 * or end of the file non-sparse via set_sparse is harmless.
2550 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
2556 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
2558 /* BB: else ... in future add code to extend file and set sparse */
2566 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
2567 loff_t off, loff_t len)
2569 /* KEEP_SIZE already checked for by do_fallocate */
2570 if (mode & FALLOC_FL_PUNCH_HOLE)
2571 return smb3_punch_hole(file, tcon, off, len);
2572 else if (mode & FALLOC_FL_ZERO_RANGE) {
2573 if (mode & FALLOC_FL_KEEP_SIZE)
2574 return smb3_zero_range(file, tcon, off, len, true);
2575 return smb3_zero_range(file, tcon, off, len, false);
2576 } else if (mode == FALLOC_FL_KEEP_SIZE)
2577 return smb3_simple_falloc(file, tcon, off, len, true);
2579 return smb3_simple_falloc(file, tcon, off, len, false);
2585 smb2_downgrade_oplock(struct TCP_Server_Info *server,
2586 struct cifsInodeInfo *cinode, bool set_level2)
2589 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
2592 server->ops->set_oplock_level(cinode, 0, 0, NULL);
2596 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2597 unsigned int epoch, bool *purge_cache)
2600 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
2602 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2603 cinode->oplock = CIFS_CACHE_RHW_FLG;
2604 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
2605 &cinode->vfs_inode);
2606 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
2607 cinode->oplock = CIFS_CACHE_RW_FLG;
2608 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
2609 &cinode->vfs_inode);
2610 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
2611 cinode->oplock = CIFS_CACHE_READ_FLG;
2612 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
2613 &cinode->vfs_inode);
2619 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2620 unsigned int epoch, bool *purge_cache)
2622 char message[5] = {0};
2625 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
2629 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
2630 cinode->oplock |= CIFS_CACHE_READ_FLG;
2631 strcat(message, "R");
2633 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
2634 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
2635 strcat(message, "H");
2637 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
2638 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
2639 strcat(message, "W");
2641 if (!cinode->oplock)
2642 strcat(message, "None");
2643 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
2644 &cinode->vfs_inode);
2648 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2649 unsigned int epoch, bool *purge_cache)
2651 unsigned int old_oplock = cinode->oplock;
2653 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
2656 *purge_cache = false;
2657 if (old_oplock == CIFS_CACHE_READ_FLG) {
2658 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
2659 (epoch - cinode->epoch > 0))
2660 *purge_cache = true;
2661 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2662 (epoch - cinode->epoch > 1))
2663 *purge_cache = true;
2664 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2665 (epoch - cinode->epoch > 1))
2666 *purge_cache = true;
2667 else if (cinode->oplock == 0 &&
2668 (epoch - cinode->epoch > 0))
2669 *purge_cache = true;
2670 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
2671 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2672 (epoch - cinode->epoch > 0))
2673 *purge_cache = true;
2674 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2675 (epoch - cinode->epoch > 1))
2676 *purge_cache = true;
2678 cinode->epoch = epoch;
2683 smb2_is_read_op(__u32 oplock)
2685 return oplock == SMB2_OPLOCK_LEVEL_II;
2689 smb21_is_read_op(__u32 oplock)
2691 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
2692 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
2696 map_oplock_to_lease(u8 oplock)
2698 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
2699 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
2700 else if (oplock == SMB2_OPLOCK_LEVEL_II)
2701 return SMB2_LEASE_READ_CACHING;
2702 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
2703 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
2704 SMB2_LEASE_WRITE_CACHING;
2709 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
2711 struct create_lease *buf;
2713 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
2717 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
2718 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2720 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2721 (struct create_lease, lcontext));
2722 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
2723 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2724 (struct create_lease, Name));
2725 buf->ccontext.NameLength = cpu_to_le16(4);
2726 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
2735 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
2737 struct create_lease_v2 *buf;
2739 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
2743 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
2744 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2746 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2747 (struct create_lease_v2, lcontext));
2748 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
2749 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2750 (struct create_lease_v2, Name));
2751 buf->ccontext.NameLength = cpu_to_le16(4);
2752 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
2761 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
2763 struct create_lease *lc = (struct create_lease *)buf;
2765 *epoch = 0; /* not used */
2766 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2767 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2768 return le32_to_cpu(lc->lcontext.LeaseState);
2772 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
2774 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
2776 *epoch = le16_to_cpu(lc->lcontext.Epoch);
2777 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2778 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2780 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
2781 return le32_to_cpu(lc->lcontext.LeaseState);
2785 smb2_wp_retry_size(struct inode *inode)
2787 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
2788 SMB2_MAX_BUFFER_SIZE);
2792 smb2_dir_needs_close(struct cifsFileInfo *cfile)
2794 return !cfile->invalidHandle;
2798 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
2799 struct smb_rqst *old_rq)
2801 struct smb2_sync_hdr *shdr =
2802 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
2804 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
2805 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
2806 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
2807 tr_hdr->Flags = cpu_to_le16(0x01);
2808 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2809 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
2812 /* We can not use the normal sg_set_buf() as we will sometimes pass a
2813 * stack object as buf.
2815 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
2816 unsigned int buflen)
2818 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
2821 /* Assumes the first rqst has a transform header as the first iov.
2823 * rqst[0].rq_iov[0] is transform header
2824 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
2825 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
2827 static struct scatterlist *
2828 init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
2830 unsigned int sg_len;
2831 struct scatterlist *sg;
2834 unsigned int idx = 0;
2838 for (i = 0; i < num_rqst; i++)
2839 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
2841 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
2845 sg_init_table(sg, sg_len);
2846 for (i = 0; i < num_rqst; i++) {
2847 for (j = 0; j < rqst[i].rq_nvec; j++) {
2849 * The first rqst has a transform header where the
2850 * first 20 bytes are not part of the encrypted blob
2852 skip = (i == 0) && (j == 0) ? 20 : 0;
2853 smb2_sg_set_buf(&sg[idx++],
2854 rqst[i].rq_iov[j].iov_base + skip,
2855 rqst[i].rq_iov[j].iov_len - skip);
2858 for (j = 0; j < rqst[i].rq_npages; j++) {
2859 unsigned int len, offset;
2861 rqst_page_get_length(&rqst[i], j, &len, &offset);
2862 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
2865 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
2870 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
2872 struct cifs_ses *ses;
2875 spin_lock(&cifs_tcp_ses_lock);
2876 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2877 if (ses->Suid != ses_id)
2879 ses_enc_key = enc ? ses->smb3encryptionkey :
2880 ses->smb3decryptionkey;
2881 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
2882 spin_unlock(&cifs_tcp_ses_lock);
2885 spin_unlock(&cifs_tcp_ses_lock);
2890 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
2891 * iov[0] - transform header (associate data),
2892 * iov[1-N] - SMB2 header and pages - data to encrypt.
2893 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
2897 crypt_message(struct TCP_Server_Info *server, int num_rqst,
2898 struct smb_rqst *rqst, int enc)
2900 struct smb2_transform_hdr *tr_hdr =
2901 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
2902 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
2904 struct scatterlist *sg;
2905 u8 sign[SMB2_SIGNATURE_SIZE] = {};
2906 u8 key[SMB3_SIGN_KEY_SIZE];
2907 struct aead_request *req;
2909 unsigned int iv_len;
2910 DECLARE_CRYPTO_WAIT(wait);
2911 struct crypto_aead *tfm;
2912 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2914 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
2916 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
2921 rc = smb3_crypto_aead_allocate(server);
2923 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
2927 tfm = enc ? server->secmech.ccmaesencrypt :
2928 server->secmech.ccmaesdecrypt;
2929 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
2931 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
2935 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
2937 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
2941 req = aead_request_alloc(tfm, GFP_KERNEL);
2943 cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
2948 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
2949 crypt_len += SMB2_SIGNATURE_SIZE;
2952 sg = init_sg(num_rqst, rqst, sign);
2954 cifs_dbg(VFS, "%s: Failed to init sg", __func__);
2959 iv_len = crypto_aead_ivsize(tfm);
2960 iv = kzalloc(iv_len, GFP_KERNEL);
2962 cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
2967 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2969 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
2970 aead_request_set_ad(req, assoc_data_len);
2972 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2973 crypto_req_done, &wait);
2975 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
2976 : crypto_aead_decrypt(req), &wait);
2979 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
2990 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
2994 for (i = 0; i < num_rqst; i++) {
2995 if (rqst[i].rq_pages) {
2996 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
2997 put_page(rqst[i].rq_pages[j]);
2998 kfree(rqst[i].rq_pages);
3004 * This function will initialize new_rq and encrypt the content.
3005 * The first entry, new_rq[0], only contains a single iov which contains
3006 * a smb2_transform_hdr and is pre-allocated by the caller.
3007 * This function then populates new_rq[1+] with the content from olq_rq[0+].
3009 * The end result is an array of smb_rqst structures where the first structure
3010 * only contains a single iov for the transform header which we then can pass
3011 * to crypt_message().
3013 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
3014 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
3017 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
3018 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
3020 struct page **pages;
3021 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
3022 unsigned int npages;
3023 unsigned int orig_len = 0;
3027 for (i = 1; i < num_rqst; i++) {
3028 npages = old_rq[i - 1].rq_npages;
3029 pages = kmalloc_array(npages, sizeof(struct page *),
3034 new_rq[i].rq_pages = pages;
3035 new_rq[i].rq_npages = npages;
3036 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
3037 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
3038 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
3039 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
3040 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
3042 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
3044 for (j = 0; j < npages; j++) {
3045 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3050 /* copy pages form the old */
3051 for (j = 0; j < npages; j++) {
3053 unsigned int offset, len;
3055 rqst_page_get_length(&new_rq[i], j, &len, &offset);
3057 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
3058 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
3060 memcpy(dst, src, len);
3061 kunmap(new_rq[i].rq_pages[j]);
3062 kunmap(old_rq[i - 1].rq_pages[j]);
3066 /* fill the 1st iov with a transform header */
3067 fill_transform_hdr(tr_hdr, orig_len, old_rq);
3069 rc = crypt_message(server, num_rqst, new_rq, 1);
3070 cifs_dbg(FYI, "encrypt message returned %d", rc);
3077 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
3082 smb3_is_transform_hdr(void *buf)
3084 struct smb2_transform_hdr *trhdr = buf;
3086 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
3090 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
3091 unsigned int buf_data_size, struct page **pages,
3092 unsigned int npages, unsigned int page_data_size)
3095 struct smb_rqst rqst = {NULL};
3098 iov[0].iov_base = buf;
3099 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
3100 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
3101 iov[1].iov_len = buf_data_size;
3105 rqst.rq_pages = pages;
3106 rqst.rq_npages = npages;
3107 rqst.rq_pagesz = PAGE_SIZE;
3108 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
3110 rc = crypt_message(server, 1, &rqst, 0);
3111 cifs_dbg(FYI, "decrypt message returned %d\n", rc);
3116 memmove(buf, iov[1].iov_base, buf_data_size);
3118 server->total_read = buf_data_size + page_data_size;
3124 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
3125 unsigned int npages, unsigned int len)
3130 for (i = 0; i < npages; i++) {
3131 struct page *page = pages[i];
3135 if (len >= PAGE_SIZE) {
3136 /* enough data to fill the page */
3140 zero_user(page, len, PAGE_SIZE - len);
3143 length = cifs_read_page_from_socket(server, page, 0, n);
3146 server->total_read += length;
3153 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
3154 unsigned int cur_off, struct bio_vec **page_vec)
3156 struct bio_vec *bvec;
3159 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
3163 for (i = 0; i < npages; i++) {
3164 bvec[i].bv_page = pages[i];
3165 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
3166 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
3167 data_size -= bvec[i].bv_len;
3170 if (data_size != 0) {
3171 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
3181 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
3182 char *buf, unsigned int buf_len, struct page **pages,
3183 unsigned int npages, unsigned int page_data_size)
3185 unsigned int data_offset;
3186 unsigned int data_len;
3187 unsigned int cur_off;
3188 unsigned int cur_page_idx;
3189 unsigned int pad_len;
3190 struct cifs_readdata *rdata = mid->callback_data;
3191 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
3192 struct bio_vec *bvec = NULL;
3193 struct iov_iter iter;
3196 bool use_rdma_mr = false;
3198 if (shdr->Command != SMB2_READ) {
3199 cifs_dbg(VFS, "only big read responses are supported\n");
3203 if (server->ops->is_session_expired &&
3204 server->ops->is_session_expired(buf)) {
3205 cifs_reconnect(server);
3206 wake_up(&server->response_q);
3210 if (server->ops->is_status_pending &&
3211 server->ops->is_status_pending(buf, server, 0))
3214 /* set up first two iov to get credits */
3215 rdata->iov[0].iov_base = buf;
3216 rdata->iov[0].iov_len = 4;
3217 rdata->iov[1].iov_base = buf + 4;
3218 rdata->iov[1].iov_len =
3219 min_t(unsigned int, buf_len, server->vals->read_rsp_size) - 4;
3220 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
3221 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
3222 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
3223 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
3225 rdata->result = server->ops->map_error(buf, true);
3226 if (rdata->result != 0) {
3227 cifs_dbg(FYI, "%s: server returned error %d\n",
3228 __func__, rdata->result);
3229 /* normal error on read response */
3230 dequeue_mid(mid, false);
3234 data_offset = server->ops->read_data_offset(buf);
3235 #ifdef CONFIG_CIFS_SMB_DIRECT
3236 use_rdma_mr = rdata->mr;
3238 data_len = server->ops->read_data_length(buf, use_rdma_mr);
3240 if (data_offset < server->vals->read_rsp_size) {
3242 * win2k8 sometimes sends an offset of 0 when the read
3243 * is beyond the EOF. Treat it as if the data starts just after
3246 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
3247 __func__, data_offset);
3248 data_offset = server->vals->read_rsp_size;
3249 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
3250 /* data_offset is beyond the end of smallbuf */
3251 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
3252 __func__, data_offset);
3253 rdata->result = -EIO;
3254 dequeue_mid(mid, rdata->result);
3258 pad_len = data_offset - server->vals->read_rsp_size;
3260 if (buf_len <= data_offset) {
3261 /* read response payload is in pages */
3262 cur_page_idx = pad_len / PAGE_SIZE;
3263 cur_off = pad_len % PAGE_SIZE;
3265 if (cur_page_idx != 0) {
3266 /* data offset is beyond the 1st page of response */
3267 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
3268 __func__, data_offset);
3269 rdata->result = -EIO;
3270 dequeue_mid(mid, rdata->result);
3274 if (data_len > page_data_size - pad_len) {
3275 /* data_len is corrupt -- discard frame */
3276 rdata->result = -EIO;
3277 dequeue_mid(mid, rdata->result);
3281 rdata->result = init_read_bvec(pages, npages, page_data_size,
3283 if (rdata->result != 0) {
3284 dequeue_mid(mid, rdata->result);
3288 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
3289 } else if (buf_len >= data_offset + data_len) {
3290 /* read response payload is in buf */
3291 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
3292 iov.iov_base = buf + data_offset;
3293 iov.iov_len = data_len;
3294 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
3296 /* read response payload cannot be in both buf and pages */
3297 WARN_ONCE(1, "buf can not contain only a part of read data");
3298 rdata->result = -EIO;
3299 dequeue_mid(mid, rdata->result);
3303 length = rdata->copy_into_pages(server, rdata, &iter);
3310 dequeue_mid(mid, false);
3315 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
3317 char *buf = server->smallbuf;
3318 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3319 unsigned int npages;
3320 struct page **pages;
3322 unsigned int buflen = server->pdu_size;
3326 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
3327 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
3329 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
3332 server->total_read += rc;
3334 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
3335 server->vals->read_rsp_size;
3336 npages = DIV_ROUND_UP(len, PAGE_SIZE);
3338 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
3344 for (; i < npages; i++) {
3345 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3352 /* read read data into pages */
3353 rc = read_data_into_pages(server, pages, npages, len);
3357 rc = cifs_discard_remaining_data(server);
3361 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
3362 pages, npages, len);
3366 *mid = smb2_find_mid(server, buf);
3368 cifs_dbg(FYI, "mid not found\n");
3370 cifs_dbg(FYI, "mid found\n");
3371 (*mid)->decrypted = true;
3372 rc = handle_read_data(server, *mid, buf,
3373 server->vals->read_rsp_size,
3374 pages, npages, len);
3378 for (i = i - 1; i >= 0; i--)
3383 cifs_discard_remaining_data(server);
3388 receive_encrypted_standard(struct TCP_Server_Info *server,
3389 struct mid_q_entry **mids, char **bufs,
3393 char *buf = server->smallbuf;
3395 struct smb2_sync_hdr *shdr;
3396 unsigned int pdu_length = server->pdu_size;
3397 unsigned int buf_size;
3398 struct mid_q_entry *mid_entry;
3400 char *next_buffer = NULL;
3404 /* switch to large buffer if too big for a small one */
3405 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
3406 server->large_buf = true;
3407 memcpy(server->bigbuf, buf, server->total_read);
3408 buf = server->bigbuf;
3411 /* now read the rest */
3412 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
3413 pdu_length - HEADER_SIZE(server) + 1);
3416 server->total_read += length;
3418 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
3419 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
3423 next_is_large = server->large_buf;
3425 shdr = (struct smb2_sync_hdr *)buf;
3426 if (shdr->NextCommand) {
3427 if (next_is_large) {
3428 tmpbuf = server->bigbuf;
3429 next_buffer = (char *)cifs_buf_get();
3431 tmpbuf = server->smallbuf;
3432 next_buffer = (char *)cifs_small_buf_get();
3435 tmpbuf + le32_to_cpu(shdr->NextCommand),
3436 pdu_length - le32_to_cpu(shdr->NextCommand));
3439 mid_entry = smb2_find_mid(server, buf);
3440 if (mid_entry == NULL)
3441 cifs_dbg(FYI, "mid not found\n");
3443 cifs_dbg(FYI, "mid found\n");
3444 mid_entry->decrypted = true;
3445 mid_entry->resp_buf_size = server->pdu_size;
3448 if (*num_mids >= MAX_COMPOUND) {
3449 cifs_dbg(VFS, "too many PDUs in compound\n");
3452 bufs[*num_mids] = buf;
3453 mids[(*num_mids)++] = mid_entry;
3455 if (mid_entry && mid_entry->handle)
3456 ret = mid_entry->handle(server, mid_entry);
3458 ret = cifs_handle_standard(server, mid_entry);
3460 if (ret == 0 && shdr->NextCommand) {
3461 pdu_length -= le32_to_cpu(shdr->NextCommand);
3462 server->large_buf = next_is_large;
3464 server->bigbuf = next_buffer;
3466 server->smallbuf = next_buffer;
3468 buf += le32_to_cpu(shdr->NextCommand);
3476 smb3_receive_transform(struct TCP_Server_Info *server,
3477 struct mid_q_entry **mids, char **bufs, int *num_mids)
3479 char *buf = server->smallbuf;
3480 unsigned int pdu_length = server->pdu_size;
3481 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3482 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3484 if (pdu_length < sizeof(struct smb2_transform_hdr) +
3485 sizeof(struct smb2_sync_hdr)) {
3486 cifs_dbg(VFS, "Transform message is too small (%u)\n",
3488 cifs_reconnect(server);
3489 wake_up(&server->response_q);
3490 return -ECONNABORTED;
3493 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
3494 cifs_dbg(VFS, "Transform message is broken\n");
3495 cifs_reconnect(server);
3496 wake_up(&server->response_q);
3497 return -ECONNABORTED;
3500 /* TODO: add support for compounds containing READ. */
3501 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
3503 return receive_encrypted_read(server, &mids[0]);
3506 return receive_encrypted_standard(server, mids, bufs, num_mids);
3510 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
3512 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
3514 return handle_read_data(server, mid, buf, server->pdu_size,
3519 smb2_next_header(char *buf)
3521 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
3522 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
3524 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
3525 return sizeof(struct smb2_transform_hdr) +
3526 le32_to_cpu(t_hdr->OriginalMessageSize);
3528 return le32_to_cpu(hdr->NextCommand);
3531 struct smb_version_operations smb20_operations = {
3532 .compare_fids = smb2_compare_fids,
3533 .setup_request = smb2_setup_request,
3534 .setup_async_request = smb2_setup_async_request,
3535 .check_receive = smb2_check_receive,
3536 .add_credits = smb2_add_credits,
3537 .set_credits = smb2_set_credits,
3538 .get_credits_field = smb2_get_credits_field,
3539 .get_credits = smb2_get_credits,
3540 .wait_mtu_credits = cifs_wait_mtu_credits,
3541 .get_next_mid = smb2_get_next_mid,
3542 .read_data_offset = smb2_read_data_offset,
3543 .read_data_length = smb2_read_data_length,
3544 .map_error = map_smb2_to_linux_error,
3545 .find_mid = smb2_find_mid,
3546 .check_message = smb2_check_message,
3547 .dump_detail = smb2_dump_detail,
3548 .clear_stats = smb2_clear_stats,
3549 .print_stats = smb2_print_stats,
3550 .is_oplock_break = smb2_is_valid_oplock_break,
3551 .handle_cancelled_mid = smb2_handle_cancelled_mid,
3552 .downgrade_oplock = smb2_downgrade_oplock,
3553 .need_neg = smb2_need_neg,
3554 .negotiate = smb2_negotiate,
3555 .negotiate_wsize = smb2_negotiate_wsize,
3556 .negotiate_rsize = smb2_negotiate_rsize,
3557 .sess_setup = SMB2_sess_setup,
3558 .logoff = SMB2_logoff,
3559 .tree_connect = SMB2_tcon,
3560 .tree_disconnect = SMB2_tdis,
3561 .qfs_tcon = smb2_qfs_tcon,
3562 .is_path_accessible = smb2_is_path_accessible,
3563 .can_echo = smb2_can_echo,
3565 .query_path_info = smb2_query_path_info,
3566 .get_srv_inum = smb2_get_srv_inum,
3567 .query_file_info = smb2_query_file_info,
3568 .set_path_size = smb2_set_path_size,
3569 .set_file_size = smb2_set_file_size,
3570 .set_file_info = smb2_set_file_info,
3571 .set_compression = smb2_set_compression,
3572 .mkdir = smb2_mkdir,
3573 .mkdir_setinfo = smb2_mkdir_setinfo,
3574 .rmdir = smb2_rmdir,
3575 .unlink = smb2_unlink,
3576 .rename = smb2_rename_path,
3577 .create_hardlink = smb2_create_hardlink,
3578 .query_symlink = smb2_query_symlink,
3579 .query_mf_symlink = smb3_query_mf_symlink,
3580 .create_mf_symlink = smb3_create_mf_symlink,
3581 .open = smb2_open_file,
3582 .set_fid = smb2_set_fid,
3583 .close = smb2_close_file,
3584 .flush = smb2_flush_file,
3585 .async_readv = smb2_async_readv,
3586 .async_writev = smb2_async_writev,
3587 .sync_read = smb2_sync_read,
3588 .sync_write = smb2_sync_write,
3589 .query_dir_first = smb2_query_dir_first,
3590 .query_dir_next = smb2_query_dir_next,
3591 .close_dir = smb2_close_dir,
3592 .calc_smb_size = smb2_calc_size,
3593 .is_status_pending = smb2_is_status_pending,
3594 .is_session_expired = smb2_is_session_expired,
3595 .oplock_response = smb2_oplock_response,
3596 .queryfs = smb2_queryfs,
3597 .mand_lock = smb2_mand_lock,
3598 .mand_unlock_range = smb2_unlock_range,
3599 .push_mand_locks = smb2_push_mandatory_locks,
3600 .get_lease_key = smb2_get_lease_key,
3601 .set_lease_key = smb2_set_lease_key,
3602 .new_lease_key = smb2_new_lease_key,
3603 .calc_signature = smb2_calc_signature,
3604 .is_read_op = smb2_is_read_op,
3605 .set_oplock_level = smb2_set_oplock_level,
3606 .create_lease_buf = smb2_create_lease_buf,
3607 .parse_lease_buf = smb2_parse_lease_buf,
3608 .copychunk_range = smb2_copychunk_range,
3609 .wp_retry_size = smb2_wp_retry_size,
3610 .dir_needs_close = smb2_dir_needs_close,
3611 .get_dfs_refer = smb2_get_dfs_refer,
3612 .select_sectype = smb2_select_sectype,
3613 #ifdef CONFIG_CIFS_XATTR
3614 .query_all_EAs = smb2_query_eas,
3615 .set_EA = smb2_set_ea,
3616 #endif /* CIFS_XATTR */
3617 #ifdef CONFIG_CIFS_ACL
3618 .get_acl = get_smb2_acl,
3619 .get_acl_by_fid = get_smb2_acl_by_fid,
3620 .set_acl = set_smb2_acl,
3621 #endif /* CIFS_ACL */
3622 .next_header = smb2_next_header,
3623 .ioctl_query_info = smb2_ioctl_query_info,
3626 struct smb_version_operations smb21_operations = {
3627 .compare_fids = smb2_compare_fids,
3628 .setup_request = smb2_setup_request,
3629 .setup_async_request = smb2_setup_async_request,
3630 .check_receive = smb2_check_receive,
3631 .add_credits = smb2_add_credits,
3632 .set_credits = smb2_set_credits,
3633 .get_credits_field = smb2_get_credits_field,
3634 .get_credits = smb2_get_credits,
3635 .wait_mtu_credits = smb2_wait_mtu_credits,
3636 .get_next_mid = smb2_get_next_mid,
3637 .read_data_offset = smb2_read_data_offset,
3638 .read_data_length = smb2_read_data_length,
3639 .map_error = map_smb2_to_linux_error,
3640 .find_mid = smb2_find_mid,
3641 .check_message = smb2_check_message,
3642 .dump_detail = smb2_dump_detail,
3643 .clear_stats = smb2_clear_stats,
3644 .print_stats = smb2_print_stats,
3645 .is_oplock_break = smb2_is_valid_oplock_break,
3646 .handle_cancelled_mid = smb2_handle_cancelled_mid,
3647 .downgrade_oplock = smb2_downgrade_oplock,
3648 .need_neg = smb2_need_neg,
3649 .negotiate = smb2_negotiate,
3650 .negotiate_wsize = smb2_negotiate_wsize,
3651 .negotiate_rsize = smb2_negotiate_rsize,
3652 .sess_setup = SMB2_sess_setup,
3653 .logoff = SMB2_logoff,
3654 .tree_connect = SMB2_tcon,
3655 .tree_disconnect = SMB2_tdis,
3656 .qfs_tcon = smb2_qfs_tcon,
3657 .is_path_accessible = smb2_is_path_accessible,
3658 .can_echo = smb2_can_echo,
3660 .query_path_info = smb2_query_path_info,
3661 .get_srv_inum = smb2_get_srv_inum,
3662 .query_file_info = smb2_query_file_info,
3663 .set_path_size = smb2_set_path_size,
3664 .set_file_size = smb2_set_file_size,
3665 .set_file_info = smb2_set_file_info,
3666 .set_compression = smb2_set_compression,
3667 .mkdir = smb2_mkdir,
3668 .mkdir_setinfo = smb2_mkdir_setinfo,
3669 .rmdir = smb2_rmdir,
3670 .unlink = smb2_unlink,
3671 .rename = smb2_rename_path,
3672 .create_hardlink = smb2_create_hardlink,
3673 .query_symlink = smb2_query_symlink,
3674 .query_mf_symlink = smb3_query_mf_symlink,
3675 .create_mf_symlink = smb3_create_mf_symlink,
3676 .open = smb2_open_file,
3677 .set_fid = smb2_set_fid,
3678 .close = smb2_close_file,
3679 .flush = smb2_flush_file,
3680 .async_readv = smb2_async_readv,
3681 .async_writev = smb2_async_writev,
3682 .sync_read = smb2_sync_read,
3683 .sync_write = smb2_sync_write,
3684 .query_dir_first = smb2_query_dir_first,
3685 .query_dir_next = smb2_query_dir_next,
3686 .close_dir = smb2_close_dir,
3687 .calc_smb_size = smb2_calc_size,
3688 .is_status_pending = smb2_is_status_pending,
3689 .is_session_expired = smb2_is_session_expired,
3690 .oplock_response = smb2_oplock_response,
3691 .queryfs = smb2_queryfs,
3692 .mand_lock = smb2_mand_lock,
3693 .mand_unlock_range = smb2_unlock_range,
3694 .push_mand_locks = smb2_push_mandatory_locks,
3695 .get_lease_key = smb2_get_lease_key,
3696 .set_lease_key = smb2_set_lease_key,
3697 .new_lease_key = smb2_new_lease_key,
3698 .calc_signature = smb2_calc_signature,
3699 .is_read_op = smb21_is_read_op,
3700 .set_oplock_level = smb21_set_oplock_level,
3701 .create_lease_buf = smb2_create_lease_buf,
3702 .parse_lease_buf = smb2_parse_lease_buf,
3703 .copychunk_range = smb2_copychunk_range,
3704 .wp_retry_size = smb2_wp_retry_size,
3705 .dir_needs_close = smb2_dir_needs_close,
3706 .enum_snapshots = smb3_enum_snapshots,
3707 .get_dfs_refer = smb2_get_dfs_refer,
3708 .select_sectype = smb2_select_sectype,
3709 #ifdef CONFIG_CIFS_XATTR
3710 .query_all_EAs = smb2_query_eas,
3711 .set_EA = smb2_set_ea,
3712 #endif /* CIFS_XATTR */
3713 #ifdef CONFIG_CIFS_ACL
3714 .get_acl = get_smb2_acl,
3715 .get_acl_by_fid = get_smb2_acl_by_fid,
3716 .set_acl = set_smb2_acl,
3717 #endif /* CIFS_ACL */
3718 .next_header = smb2_next_header,
3719 .ioctl_query_info = smb2_ioctl_query_info,
3722 struct smb_version_operations smb30_operations = {
3723 .compare_fids = smb2_compare_fids,
3724 .setup_request = smb2_setup_request,
3725 .setup_async_request = smb2_setup_async_request,
3726 .check_receive = smb2_check_receive,
3727 .add_credits = smb2_add_credits,
3728 .set_credits = smb2_set_credits,
3729 .get_credits_field = smb2_get_credits_field,
3730 .get_credits = smb2_get_credits,
3731 .wait_mtu_credits = smb2_wait_mtu_credits,
3732 .get_next_mid = smb2_get_next_mid,
3733 .read_data_offset = smb2_read_data_offset,
3734 .read_data_length = smb2_read_data_length,
3735 .map_error = map_smb2_to_linux_error,
3736 .find_mid = smb2_find_mid,
3737 .check_message = smb2_check_message,
3738 .dump_detail = smb2_dump_detail,
3739 .clear_stats = smb2_clear_stats,
3740 .print_stats = smb2_print_stats,
3741 .dump_share_caps = smb2_dump_share_caps,
3742 .is_oplock_break = smb2_is_valid_oplock_break,
3743 .handle_cancelled_mid = smb2_handle_cancelled_mid,
3744 .downgrade_oplock = smb2_downgrade_oplock,
3745 .need_neg = smb2_need_neg,
3746 .negotiate = smb2_negotiate,
3747 .negotiate_wsize = smb3_negotiate_wsize,
3748 .negotiate_rsize = smb3_negotiate_rsize,
3749 .sess_setup = SMB2_sess_setup,
3750 .logoff = SMB2_logoff,
3751 .tree_connect = SMB2_tcon,
3752 .tree_disconnect = SMB2_tdis,
3753 .qfs_tcon = smb3_qfs_tcon,
3754 .is_path_accessible = smb2_is_path_accessible,
3755 .can_echo = smb2_can_echo,
3757 .query_path_info = smb2_query_path_info,
3758 .get_srv_inum = smb2_get_srv_inum,
3759 .query_file_info = smb2_query_file_info,
3760 .set_path_size = smb2_set_path_size,
3761 .set_file_size = smb2_set_file_size,
3762 .set_file_info = smb2_set_file_info,
3763 .set_compression = smb2_set_compression,
3764 .mkdir = smb2_mkdir,
3765 .mkdir_setinfo = smb2_mkdir_setinfo,
3766 .rmdir = smb2_rmdir,
3767 .unlink = smb2_unlink,
3768 .rename = smb2_rename_path,
3769 .create_hardlink = smb2_create_hardlink,
3770 .query_symlink = smb2_query_symlink,
3771 .query_mf_symlink = smb3_query_mf_symlink,
3772 .create_mf_symlink = smb3_create_mf_symlink,
3773 .open = smb2_open_file,
3774 .set_fid = smb2_set_fid,
3775 .close = smb2_close_file,
3776 .flush = smb2_flush_file,
3777 .async_readv = smb2_async_readv,
3778 .async_writev = smb2_async_writev,
3779 .sync_read = smb2_sync_read,
3780 .sync_write = smb2_sync_write,
3781 .query_dir_first = smb2_query_dir_first,
3782 .query_dir_next = smb2_query_dir_next,
3783 .close_dir = smb2_close_dir,
3784 .calc_smb_size = smb2_calc_size,
3785 .is_status_pending = smb2_is_status_pending,
3786 .is_session_expired = smb2_is_session_expired,
3787 .oplock_response = smb2_oplock_response,
3788 .queryfs = smb2_queryfs,
3789 .mand_lock = smb2_mand_lock,
3790 .mand_unlock_range = smb2_unlock_range,
3791 .push_mand_locks = smb2_push_mandatory_locks,
3792 .get_lease_key = smb2_get_lease_key,
3793 .set_lease_key = smb2_set_lease_key,
3794 .new_lease_key = smb2_new_lease_key,
3795 .generate_signingkey = generate_smb30signingkey,
3796 .calc_signature = smb3_calc_signature,
3797 .set_integrity = smb3_set_integrity,
3798 .is_read_op = smb21_is_read_op,
3799 .set_oplock_level = smb3_set_oplock_level,
3800 .create_lease_buf = smb3_create_lease_buf,
3801 .parse_lease_buf = smb3_parse_lease_buf,
3802 .copychunk_range = smb2_copychunk_range,
3803 .duplicate_extents = smb2_duplicate_extents,
3804 .validate_negotiate = smb3_validate_negotiate,
3805 .wp_retry_size = smb2_wp_retry_size,
3806 .dir_needs_close = smb2_dir_needs_close,
3807 .fallocate = smb3_fallocate,
3808 .enum_snapshots = smb3_enum_snapshots,
3809 .init_transform_rq = smb3_init_transform_rq,
3810 .is_transform_hdr = smb3_is_transform_hdr,
3811 .receive_transform = smb3_receive_transform,
3812 .get_dfs_refer = smb2_get_dfs_refer,
3813 .select_sectype = smb2_select_sectype,
3814 #ifdef CONFIG_CIFS_XATTR
3815 .query_all_EAs = smb2_query_eas,
3816 .set_EA = smb2_set_ea,
3817 #endif /* CIFS_XATTR */
3818 #ifdef CONFIG_CIFS_ACL
3819 .get_acl = get_smb2_acl,
3820 .get_acl_by_fid = get_smb2_acl_by_fid,
3821 .set_acl = set_smb2_acl,
3822 #endif /* CIFS_ACL */
3823 .next_header = smb2_next_header,
3824 .ioctl_query_info = smb2_ioctl_query_info,
3827 struct smb_version_operations smb311_operations = {
3828 .compare_fids = smb2_compare_fids,
3829 .setup_request = smb2_setup_request,
3830 .setup_async_request = smb2_setup_async_request,
3831 .check_receive = smb2_check_receive,
3832 .add_credits = smb2_add_credits,
3833 .set_credits = smb2_set_credits,
3834 .get_credits_field = smb2_get_credits_field,
3835 .get_credits = smb2_get_credits,
3836 .wait_mtu_credits = smb2_wait_mtu_credits,
3837 .get_next_mid = smb2_get_next_mid,
3838 .read_data_offset = smb2_read_data_offset,
3839 .read_data_length = smb2_read_data_length,
3840 .map_error = map_smb2_to_linux_error,
3841 .find_mid = smb2_find_mid,
3842 .check_message = smb2_check_message,
3843 .dump_detail = smb2_dump_detail,
3844 .clear_stats = smb2_clear_stats,
3845 .print_stats = smb2_print_stats,
3846 .dump_share_caps = smb2_dump_share_caps,
3847 .is_oplock_break = smb2_is_valid_oplock_break,
3848 .handle_cancelled_mid = smb2_handle_cancelled_mid,
3849 .downgrade_oplock = smb2_downgrade_oplock,
3850 .need_neg = smb2_need_neg,
3851 .negotiate = smb2_negotiate,
3852 .negotiate_wsize = smb3_negotiate_wsize,
3853 .negotiate_rsize = smb3_negotiate_rsize,
3854 .sess_setup = SMB2_sess_setup,
3855 .logoff = SMB2_logoff,
3856 .tree_connect = SMB2_tcon,
3857 .tree_disconnect = SMB2_tdis,
3858 .qfs_tcon = smb3_qfs_tcon,
3859 .is_path_accessible = smb2_is_path_accessible,
3860 .can_echo = smb2_can_echo,
3862 .query_path_info = smb2_query_path_info,
3863 .get_srv_inum = smb2_get_srv_inum,
3864 .query_file_info = smb2_query_file_info,
3865 .set_path_size = smb2_set_path_size,
3866 .set_file_size = smb2_set_file_size,
3867 .set_file_info = smb2_set_file_info,
3868 .set_compression = smb2_set_compression,
3869 .mkdir = smb2_mkdir,
3870 .mkdir_setinfo = smb2_mkdir_setinfo,
3871 .posix_mkdir = smb311_posix_mkdir,
3872 .rmdir = smb2_rmdir,
3873 .unlink = smb2_unlink,
3874 .rename = smb2_rename_path,
3875 .create_hardlink = smb2_create_hardlink,
3876 .query_symlink = smb2_query_symlink,
3877 .query_mf_symlink = smb3_query_mf_symlink,
3878 .create_mf_symlink = smb3_create_mf_symlink,
3879 .open = smb2_open_file,
3880 .set_fid = smb2_set_fid,
3881 .close = smb2_close_file,
3882 .flush = smb2_flush_file,
3883 .async_readv = smb2_async_readv,
3884 .async_writev = smb2_async_writev,
3885 .sync_read = smb2_sync_read,
3886 .sync_write = smb2_sync_write,
3887 .query_dir_first = smb2_query_dir_first,
3888 .query_dir_next = smb2_query_dir_next,
3889 .close_dir = smb2_close_dir,
3890 .calc_smb_size = smb2_calc_size,
3891 .is_status_pending = smb2_is_status_pending,
3892 .is_session_expired = smb2_is_session_expired,
3893 .oplock_response = smb2_oplock_response,
3894 .queryfs = smb311_queryfs,
3895 .mand_lock = smb2_mand_lock,
3896 .mand_unlock_range = smb2_unlock_range,
3897 .push_mand_locks = smb2_push_mandatory_locks,
3898 .get_lease_key = smb2_get_lease_key,
3899 .set_lease_key = smb2_set_lease_key,
3900 .new_lease_key = smb2_new_lease_key,
3901 .generate_signingkey = generate_smb311signingkey,
3902 .calc_signature = smb3_calc_signature,
3903 .set_integrity = smb3_set_integrity,
3904 .is_read_op = smb21_is_read_op,
3905 .set_oplock_level = smb3_set_oplock_level,
3906 .create_lease_buf = smb3_create_lease_buf,
3907 .parse_lease_buf = smb3_parse_lease_buf,
3908 .copychunk_range = smb2_copychunk_range,
3909 .duplicate_extents = smb2_duplicate_extents,
3910 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
3911 .wp_retry_size = smb2_wp_retry_size,
3912 .dir_needs_close = smb2_dir_needs_close,
3913 .fallocate = smb3_fallocate,
3914 .enum_snapshots = smb3_enum_snapshots,
3915 .init_transform_rq = smb3_init_transform_rq,
3916 .is_transform_hdr = smb3_is_transform_hdr,
3917 .receive_transform = smb3_receive_transform,
3918 .get_dfs_refer = smb2_get_dfs_refer,
3919 .select_sectype = smb2_select_sectype,
3920 #ifdef CONFIG_CIFS_XATTR
3921 .query_all_EAs = smb2_query_eas,
3922 .set_EA = smb2_set_ea,
3923 #endif /* CIFS_XATTR */
3924 #ifdef CONFIG_CIFS_ACL
3925 .get_acl = get_smb2_acl,
3926 .get_acl_by_fid = get_smb2_acl_by_fid,
3927 .set_acl = set_smb2_acl,
3928 #endif /* CIFS_ACL */
3929 .next_header = smb2_next_header,
3930 .ioctl_query_info = smb2_ioctl_query_info,
3933 struct smb_version_values smb20_values = {
3934 .version_string = SMB20_VERSION_STRING,
3935 .protocol_id = SMB20_PROT_ID,
3936 .req_capabilities = 0, /* MBZ */
3937 .large_lock_type = 0,
3938 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3939 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3940 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3941 .header_size = sizeof(struct smb2_sync_hdr),
3942 .header_preamble_size = 0,
3943 .max_header_size = MAX_SMB2_HDR_SIZE,
3944 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3945 .lock_cmd = SMB2_LOCK,
3947 .cap_nt_find = SMB2_NT_FIND,
3948 .cap_large_files = SMB2_LARGE_FILES,
3949 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3950 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3951 .create_lease_size = sizeof(struct create_lease),
3954 struct smb_version_values smb21_values = {
3955 .version_string = SMB21_VERSION_STRING,
3956 .protocol_id = SMB21_PROT_ID,
3957 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
3958 .large_lock_type = 0,
3959 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3960 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3961 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3962 .header_size = sizeof(struct smb2_sync_hdr),
3963 .header_preamble_size = 0,
3964 .max_header_size = MAX_SMB2_HDR_SIZE,
3965 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3966 .lock_cmd = SMB2_LOCK,
3968 .cap_nt_find = SMB2_NT_FIND,
3969 .cap_large_files = SMB2_LARGE_FILES,
3970 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3971 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3972 .create_lease_size = sizeof(struct create_lease),
3975 struct smb_version_values smb3any_values = {
3976 .version_string = SMB3ANY_VERSION_STRING,
3977 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3978 .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,
3979 .large_lock_type = 0,
3980 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3981 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3982 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3983 .header_size = sizeof(struct smb2_sync_hdr),
3984 .header_preamble_size = 0,
3985 .max_header_size = MAX_SMB2_HDR_SIZE,
3986 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3987 .lock_cmd = SMB2_LOCK,
3989 .cap_nt_find = SMB2_NT_FIND,
3990 .cap_large_files = SMB2_LARGE_FILES,
3991 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3992 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3993 .create_lease_size = sizeof(struct create_lease_v2),
3996 struct smb_version_values smbdefault_values = {
3997 .version_string = SMBDEFAULT_VERSION_STRING,
3998 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3999 .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,
4000 .large_lock_type = 0,
4001 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4002 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4003 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4004 .header_size = sizeof(struct smb2_sync_hdr),
4005 .header_preamble_size = 0,
4006 .max_header_size = MAX_SMB2_HDR_SIZE,
4007 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4008 .lock_cmd = SMB2_LOCK,
4010 .cap_nt_find = SMB2_NT_FIND,
4011 .cap_large_files = SMB2_LARGE_FILES,
4012 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4013 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4014 .create_lease_size = sizeof(struct create_lease_v2),
4017 struct smb_version_values smb30_values = {
4018 .version_string = SMB30_VERSION_STRING,
4019 .protocol_id = SMB30_PROT_ID,
4020 .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,
4021 .large_lock_type = 0,
4022 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4023 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4024 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4025 .header_size = sizeof(struct smb2_sync_hdr),
4026 .header_preamble_size = 0,
4027 .max_header_size = MAX_SMB2_HDR_SIZE,
4028 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4029 .lock_cmd = SMB2_LOCK,
4031 .cap_nt_find = SMB2_NT_FIND,
4032 .cap_large_files = SMB2_LARGE_FILES,
4033 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4034 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4035 .create_lease_size = sizeof(struct create_lease_v2),
4038 struct smb_version_values smb302_values = {
4039 .version_string = SMB302_VERSION_STRING,
4040 .protocol_id = SMB302_PROT_ID,
4041 .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,
4042 .large_lock_type = 0,
4043 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4044 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4045 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4046 .header_size = sizeof(struct smb2_sync_hdr),
4047 .header_preamble_size = 0,
4048 .max_header_size = MAX_SMB2_HDR_SIZE,
4049 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4050 .lock_cmd = SMB2_LOCK,
4052 .cap_nt_find = SMB2_NT_FIND,
4053 .cap_large_files = SMB2_LARGE_FILES,
4054 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4055 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4056 .create_lease_size = sizeof(struct create_lease_v2),
4059 struct smb_version_values smb311_values = {
4060 .version_string = SMB311_VERSION_STRING,
4061 .protocol_id = SMB311_PROT_ID,
4062 .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,
4063 .large_lock_type = 0,
4064 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4065 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4066 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4067 .header_size = sizeof(struct smb2_sync_hdr),
4068 .header_preamble_size = 0,
4069 .max_header_size = MAX_SMB2_HDR_SIZE,
4070 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4071 .lock_cmd = SMB2_LOCK,
4073 .cap_nt_find = SMB2_NT_FIND,
4074 .cap_large_files = SMB2_LARGE_FILES,
4075 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4076 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4077 .create_lease_size = sizeof(struct create_lease_v2),