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
106 || server->tcpStatus == CifsExiting)
111 /* change_conf hasn't been executed */
114 cifs_dbg(VFS, "Possible client or server bug - zero credits\n");
117 cifs_dbg(VFS, "disabling echoes and oplocks\n");
120 cifs_dbg(FYI, "disabling oplocks\n");
123 cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
128 smb2_set_credits(struct TCP_Server_Info *server, const int val)
130 spin_lock(&server->req_lock);
131 server->credits = val;
133 server->reconnect_instance++;
134 spin_unlock(&server->req_lock);
135 /* don't log while holding the lock */
137 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
141 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
145 return &server->echo_credits;
147 return &server->oplock_credits;
149 return &server->credits;
154 smb2_get_credits(struct mid_q_entry *mid)
156 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)mid->resp_buf;
158 if (mid->mid_state == MID_RESPONSE_RECEIVED
159 || mid->mid_state == MID_RESPONSE_MALFORMED)
160 return le16_to_cpu(shdr->CreditRequest);
166 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
167 unsigned int *num, unsigned int *credits)
170 unsigned int scredits;
172 spin_lock(&server->req_lock);
174 if (server->credits <= 0) {
175 spin_unlock(&server->req_lock);
176 cifs_num_waiters_inc(server);
177 rc = wait_event_killable(server->request_q,
178 has_credits(server, &server->credits));
179 cifs_num_waiters_dec(server);
182 spin_lock(&server->req_lock);
184 if (server->tcpStatus == CifsExiting) {
185 spin_unlock(&server->req_lock);
189 scredits = server->credits;
190 /* can deadlock with reopen */
192 *num = SMB2_MAX_BUFFER_SIZE;
197 /* leave some credits for reopen and other ops */
199 *num = min_t(unsigned int, size,
200 scredits * SMB2_MAX_BUFFER_SIZE);
202 *credits = DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
203 server->credits -= *credits;
208 spin_unlock(&server->req_lock);
213 smb2_get_next_mid(struct TCP_Server_Info *server)
216 /* for SMB2 we need the current value */
217 spin_lock(&GlobalMid_Lock);
218 mid = server->CurrentMid++;
219 spin_unlock(&GlobalMid_Lock);
224 smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
226 spin_lock(&GlobalMid_Lock);
227 if (server->CurrentMid >= val)
228 server->CurrentMid -= val;
229 spin_unlock(&GlobalMid_Lock);
232 static struct mid_q_entry *
233 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
235 struct mid_q_entry *mid;
236 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
237 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
239 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
240 cifs_dbg(VFS, "encrypted frame parsing not supported yet");
244 spin_lock(&GlobalMid_Lock);
245 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
246 if ((mid->mid == wire_mid) &&
247 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
248 (mid->command == shdr->Command)) {
249 kref_get(&mid->refcount);
250 spin_unlock(&GlobalMid_Lock);
254 spin_unlock(&GlobalMid_Lock);
259 smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
261 #ifdef CONFIG_CIFS_DEBUG2
262 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
264 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
265 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
267 cifs_dbg(VFS, "smb buf %p len %u\n", buf,
268 server->ops->calc_smb_size(buf, server));
273 smb2_need_neg(struct TCP_Server_Info *server)
275 return server->max_read == 0;
279 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
282 ses->server->CurrentMid = 0;
283 rc = SMB2_negotiate(xid, ses);
284 /* BB we probably don't need to retry with modern servers */
291 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
293 struct TCP_Server_Info *server = tcon->ses->server;
296 /* start with specified wsize, or default */
297 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
298 wsize = min_t(unsigned int, wsize, server->max_write);
299 #ifdef CONFIG_CIFS_SMB_DIRECT
302 wsize = min_t(unsigned int,
303 wsize, server->smbd_conn->max_fragmented_send_size);
305 wsize = min_t(unsigned int,
306 wsize, server->smbd_conn->max_readwrite_size);
309 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
310 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
316 smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
318 struct TCP_Server_Info *server = tcon->ses->server;
321 /* start with specified wsize, or default */
322 wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
323 wsize = min_t(unsigned int, wsize, server->max_write);
324 #ifdef CONFIG_CIFS_SMB_DIRECT
327 wsize = min_t(unsigned int,
328 wsize, server->smbd_conn->max_fragmented_send_size);
330 wsize = min_t(unsigned int,
331 wsize, server->smbd_conn->max_readwrite_size);
334 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
335 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
341 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
343 struct TCP_Server_Info *server = tcon->ses->server;
346 /* start with specified rsize, or default */
347 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
348 rsize = min_t(unsigned int, rsize, server->max_read);
349 #ifdef CONFIG_CIFS_SMB_DIRECT
352 rsize = min_t(unsigned int,
353 rsize, server->smbd_conn->max_fragmented_recv_size);
355 rsize = min_t(unsigned int,
356 rsize, server->smbd_conn->max_readwrite_size);
360 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
361 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
367 smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
369 struct TCP_Server_Info *server = tcon->ses->server;
372 /* start with specified rsize, or default */
373 rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
374 rsize = min_t(unsigned int, rsize, server->max_read);
375 #ifdef CONFIG_CIFS_SMB_DIRECT
378 rsize = min_t(unsigned int,
379 rsize, server->smbd_conn->max_fragmented_recv_size);
381 rsize = min_t(unsigned int,
382 rsize, server->smbd_conn->max_readwrite_size);
386 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
387 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
393 parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
395 struct cifs_server_iface **iface_list,
398 struct network_interface_info_ioctl_rsp *p;
399 struct sockaddr_in *addr4;
400 struct sockaddr_in6 *addr6;
401 struct iface_info_ipv4 *p4;
402 struct iface_info_ipv6 *p6;
403 struct cifs_server_iface *info;
413 * Fist pass: count and sanity check
416 bytes_left = buf_len;
418 while (bytes_left >= sizeof(*p)) {
420 next = le32_to_cpu(p->Next);
422 bytes_left -= sizeof(*p);
425 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
430 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
435 if (bytes_left || p->Next)
436 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
440 * Second pass: extract info to internal structure
443 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
450 bytes_left = buf_len;
452 while (bytes_left >= sizeof(*p)) {
453 info->speed = le64_to_cpu(p->LinkSpeed);
454 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE);
455 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE);
457 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
458 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
459 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
460 le32_to_cpu(p->Capability));
464 * The kernel and wire socket structures have the same
465 * layout and use network byte order but make the
466 * conversion explicit in case either one changes.
469 addr4 = (struct sockaddr_in *)&info->sockaddr;
470 p4 = (struct iface_info_ipv4 *)p->Buffer;
471 addr4->sin_family = AF_INET;
472 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
474 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
475 addr4->sin_port = cpu_to_be16(CIFS_PORT);
477 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
481 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
482 p6 = (struct iface_info_ipv6 *)p->Buffer;
483 addr6->sin6_family = AF_INET6;
484 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
486 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
487 addr6->sin6_flowinfo = 0;
488 addr6->sin6_scope_id = 0;
489 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
491 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
496 "%s: skipping unsupported socket family\n",
504 next = le32_to_cpu(p->Next);
507 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
527 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
530 unsigned int ret_data_len = 0;
531 struct network_interface_info_ioctl_rsp *out_buf = NULL;
532 struct cifs_server_iface *iface_list;
534 struct cifs_ses *ses = tcon->ses;
536 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
537 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
538 NULL /* no data input */, 0 /* no data input */,
539 (char **)&out_buf, &ret_data_len);
540 if (rc == -EOPNOTSUPP) {
542 "server does not support query network interfaces\n");
544 } else if (rc != 0) {
545 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
549 rc = parse_server_interfaces(out_buf, ret_data_len,
550 &iface_list, &iface_count);
554 spin_lock(&ses->iface_lock);
555 kfree(ses->iface_list);
556 ses->iface_list = iface_list;
557 ses->iface_count = iface_count;
558 ses->iface_last_update = jiffies;
559 spin_unlock(&ses->iface_lock);
567 smb2_close_cached_fid(struct kref *ref)
569 struct cached_fid *cfid = container_of(ref, struct cached_fid,
572 if (cfid->is_valid) {
573 cifs_dbg(FYI, "clear cached root file handle\n");
574 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
575 cfid->fid->volatile_fid);
576 cfid->is_valid = false;
580 void close_shroot(struct cached_fid *cfid)
582 mutex_lock(&cfid->fid_mutex);
583 kref_put(&cfid->refcount, smb2_close_cached_fid);
584 mutex_unlock(&cfid->fid_mutex);
588 smb2_cached_lease_break(struct work_struct *work)
590 struct cached_fid *cfid = container_of(work,
591 struct cached_fid, lease_break);
597 * Open the directory at the root of a share
599 int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
601 struct cifs_open_parms oparams;
603 __le16 srch_path = 0; /* Null - since an open of top of share */
604 u8 oplock = SMB2_OPLOCK_LEVEL_II;
606 mutex_lock(&tcon->crfid.fid_mutex);
607 if (tcon->crfid.is_valid) {
608 cifs_dbg(FYI, "found a cached root file handle\n");
609 memcpy(pfid, tcon->crfid.fid, sizeof(struct cifs_fid));
610 kref_get(&tcon->crfid.refcount);
611 mutex_unlock(&tcon->crfid.fid_mutex);
616 oparams.create_options = 0;
617 oparams.desired_access = FILE_READ_ATTRIBUTES;
618 oparams.disposition = FILE_OPEN;
620 oparams.reconnect = false;
622 rc = SMB2_open(xid, &oparams, &srch_path, &oplock, NULL, NULL, NULL);
624 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
625 tcon->crfid.tcon = tcon;
626 tcon->crfid.is_valid = true;
627 kref_init(&tcon->crfid.refcount);
628 kref_get(&tcon->crfid.refcount);
630 mutex_unlock(&tcon->crfid.fid_mutex);
635 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
638 __le16 srch_path = 0; /* Null - open root of share */
639 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
640 struct cifs_open_parms oparms;
642 bool no_cached_open = tcon->nohandlecache;
645 oparms.desired_access = FILE_READ_ATTRIBUTES;
646 oparms.disposition = FILE_OPEN;
647 oparms.create_options = 0;
649 oparms.reconnect = false;
652 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
655 rc = open_shroot(xid, tcon, &fid);
660 SMB3_request_interfaces(xid, tcon);
662 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
663 FS_ATTRIBUTE_INFORMATION);
664 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
665 FS_DEVICE_INFORMATION);
666 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
667 FS_VOLUME_INFORMATION);
668 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
669 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
671 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
673 close_shroot(&tcon->crfid);
679 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
682 __le16 srch_path = 0; /* Null - open root of share */
683 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
684 struct cifs_open_parms oparms;
688 oparms.desired_access = FILE_READ_ATTRIBUTES;
689 oparms.disposition = FILE_OPEN;
690 oparms.create_options = 0;
692 oparms.reconnect = false;
694 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
698 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
699 FS_ATTRIBUTE_INFORMATION);
700 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
701 FS_DEVICE_INFORMATION);
702 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
707 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
708 struct cifs_sb_info *cifs_sb, const char *full_path)
712 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
713 struct cifs_open_parms oparms;
716 if ((*full_path == 0) && tcon->crfid.is_valid)
719 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
724 oparms.desired_access = FILE_READ_ATTRIBUTES;
725 oparms.disposition = FILE_OPEN;
726 if (backup_cred(cifs_sb))
727 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
729 oparms.create_options = 0;
731 oparms.reconnect = false;
733 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
739 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
745 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
746 struct cifs_sb_info *cifs_sb, const char *full_path,
747 u64 *uniqueid, FILE_ALL_INFO *data)
749 *uniqueid = le64_to_cpu(data->IndexNumber);
754 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
755 struct cifs_fid *fid, FILE_ALL_INFO *data)
758 struct smb2_file_all_info *smb2_data;
760 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
762 if (smb2_data == NULL)
765 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
768 move_smb2_info_to_cifs(data, smb2_data);
773 #ifdef CONFIG_CIFS_XATTR
775 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
776 struct smb2_file_full_ea_info *src, size_t src_size,
777 const unsigned char *ea_name)
780 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
782 size_t buf_size = dst_size;
783 size_t name_len, value_len, user_name_len;
785 while (src_size > 0) {
786 name = &src->ea_data[0];
787 name_len = (size_t)src->ea_name_length;
788 value = &src->ea_data[src->ea_name_length + 1];
789 value_len = (size_t)le16_to_cpu(src->ea_value_length);
795 if (src_size < 8 + name_len + 1 + value_len) {
796 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
802 if (ea_name_len == name_len &&
803 memcmp(ea_name, name, name_len) == 0) {
807 if (dst_size < value_len) {
811 memcpy(dst, value, value_len);
815 /* 'user.' plus a terminating null */
816 user_name_len = 5 + 1 + name_len;
819 /* skip copy - calc size only */
821 } else if (dst_size >= user_name_len) {
822 dst_size -= user_name_len;
823 memcpy(dst, "user.", 5);
825 memcpy(dst, src->ea_data, name_len);
831 /* stop before overrun buffer */
837 if (!src->next_entry_offset)
840 if (src_size < le32_to_cpu(src->next_entry_offset)) {
841 /* stop before overrun buffer */
845 src_size -= le32_to_cpu(src->next_entry_offset);
846 src = (void *)((char *)src +
847 le32_to_cpu(src->next_entry_offset));
850 /* didn't find the named attribute */
859 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
860 const unsigned char *path, const unsigned char *ea_name,
861 char *ea_data, size_t buf_size,
862 struct cifs_sb_info *cifs_sb)
866 struct kvec rsp_iov = {NULL, 0};
867 int buftype = CIFS_NO_BUFFER;
868 struct smb2_query_info_rsp *rsp;
869 struct smb2_file_full_ea_info *info = NULL;
871 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
875 rc = smb2_query_info_compound(xid, tcon, utf16_path,
877 FILE_FULL_EA_INFORMATION,
880 MAX_SMB2_CREATE_RESPONSE_SIZE -
881 MAX_SMB2_CLOSE_RESPONSE_SIZE,
882 &rsp_iov, &buftype, cifs_sb);
885 * If ea_name is NULL (listxattr) and there are no EAs,
886 * return 0 as it's not an error. Otherwise, the specified
887 * ea_name was not found.
889 if (!ea_name && rc == -ENODATA)
894 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
895 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
896 le32_to_cpu(rsp->OutputBufferLength),
898 sizeof(struct smb2_file_full_ea_info));
902 info = (struct smb2_file_full_ea_info *)(
903 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
904 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
905 le32_to_cpu(rsp->OutputBufferLength), ea_name);
909 free_rsp_buf(buftype, rsp_iov.iov_base);
915 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
916 const char *path, const char *ea_name, const void *ea_value,
917 const __u16 ea_value_len, const struct nls_table *nls_codepage,
918 struct cifs_sb_info *cifs_sb)
920 struct cifs_ses *ses = tcon->ses;
921 __le16 *utf16_path = NULL;
922 int ea_name_len = strlen(ea_name);
925 struct smb_rqst rqst[3];
927 struct kvec rsp_iov[3];
928 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
929 struct cifs_open_parms oparms;
930 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
932 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
933 unsigned int size[1];
935 struct smb2_file_full_ea_info *ea = NULL;
936 struct kvec close_iov[1];
939 if (smb3_encryption_required(tcon))
940 flags |= CIFS_TRANSFORM_REQ;
942 if (ea_name_len > 255)
945 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
949 memset(rqst, 0, sizeof(rqst));
950 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
951 memset(rsp_iov, 0, sizeof(rsp_iov));
953 if (ses->server->ops->query_all_EAs) {
955 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
964 memset(&open_iov, 0, sizeof(open_iov));
965 rqst[0].rq_iov = open_iov;
966 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
968 memset(&oparms, 0, sizeof(oparms));
970 oparms.desired_access = FILE_WRITE_EA;
971 oparms.disposition = FILE_OPEN;
972 if (backup_cred(cifs_sb))
973 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
975 oparms.create_options = 0;
977 oparms.reconnect = false;
979 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
982 smb2_set_next_command(tcon, &rqst[0]);
986 memset(&si_iov, 0, sizeof(si_iov));
987 rqst[1].rq_iov = si_iov;
990 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
991 ea = kzalloc(len, GFP_KERNEL);
997 ea->ea_name_length = ea_name_len;
998 ea->ea_value_length = cpu_to_le16(ea_value_len);
999 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1000 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1005 rc = SMB2_set_info_init(tcon, &rqst[1], COMPOUND_FID,
1006 COMPOUND_FID, current->tgid,
1007 FILE_FULL_EA_INFORMATION,
1008 SMB2_O_INFO_FILE, 0, data, size);
1009 smb2_set_next_command(tcon, &rqst[1]);
1010 smb2_set_related(&rqst[1]);
1014 memset(&close_iov, 0, sizeof(close_iov));
1015 rqst[2].rq_iov = close_iov;
1016 rqst[2].rq_nvec = 1;
1017 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1018 smb2_set_related(&rqst[2]);
1020 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1021 resp_buftype, rsp_iov);
1026 SMB2_open_free(&rqst[0]);
1027 SMB2_set_info_free(&rqst[1]);
1028 SMB2_close_free(&rqst[2]);
1029 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1030 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1031 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1037 smb2_can_echo(struct TCP_Server_Info *server)
1039 return server->echoes;
1043 smb2_clear_stats(struct cifs_tcon *tcon)
1046 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1047 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1048 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1053 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1055 seq_puts(m, "\n\tShare Capabilities:");
1056 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1057 seq_puts(m, " DFS,");
1058 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1059 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1060 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1061 seq_puts(m, " SCALEOUT,");
1062 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1063 seq_puts(m, " CLUSTER,");
1064 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1065 seq_puts(m, " ASYMMETRIC,");
1066 if (tcon->capabilities == 0)
1067 seq_puts(m, " None");
1068 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1069 seq_puts(m, " Aligned,");
1070 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1071 seq_puts(m, " Partition Aligned,");
1072 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1073 seq_puts(m, " SSD,");
1074 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1075 seq_puts(m, " TRIM-support,");
1077 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1078 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1079 if (tcon->perf_sector_size)
1080 seq_printf(m, "\tOptimal sector size: 0x%x",
1081 tcon->perf_sector_size);
1082 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1086 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1088 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1089 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1092 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1093 * totals (requests sent) since those SMBs are per-session not per tcon
1095 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1096 (long long)(tcon->bytes_read),
1097 (long long)(tcon->bytes_written));
1098 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1099 atomic_read(&tcon->num_local_opens),
1100 atomic_read(&tcon->num_remote_opens));
1101 seq_printf(m, "\nTreeConnects: %d total %d failed",
1102 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1103 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1104 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1105 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1106 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1107 seq_printf(m, "\nCreates: %d total %d failed",
1108 atomic_read(&sent[SMB2_CREATE_HE]),
1109 atomic_read(&failed[SMB2_CREATE_HE]));
1110 seq_printf(m, "\nCloses: %d total %d failed",
1111 atomic_read(&sent[SMB2_CLOSE_HE]),
1112 atomic_read(&failed[SMB2_CLOSE_HE]));
1113 seq_printf(m, "\nFlushes: %d total %d failed",
1114 atomic_read(&sent[SMB2_FLUSH_HE]),
1115 atomic_read(&failed[SMB2_FLUSH_HE]));
1116 seq_printf(m, "\nReads: %d total %d failed",
1117 atomic_read(&sent[SMB2_READ_HE]),
1118 atomic_read(&failed[SMB2_READ_HE]));
1119 seq_printf(m, "\nWrites: %d total %d failed",
1120 atomic_read(&sent[SMB2_WRITE_HE]),
1121 atomic_read(&failed[SMB2_WRITE_HE]));
1122 seq_printf(m, "\nLocks: %d total %d failed",
1123 atomic_read(&sent[SMB2_LOCK_HE]),
1124 atomic_read(&failed[SMB2_LOCK_HE]));
1125 seq_printf(m, "\nIOCTLs: %d total %d failed",
1126 atomic_read(&sent[SMB2_IOCTL_HE]),
1127 atomic_read(&failed[SMB2_IOCTL_HE]));
1128 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1129 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1130 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1131 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1132 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1133 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1134 seq_printf(m, "\nQueryInfos: %d total %d failed",
1135 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1136 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1137 seq_printf(m, "\nSetInfos: %d total %d failed",
1138 atomic_read(&sent[SMB2_SET_INFO_HE]),
1139 atomic_read(&failed[SMB2_SET_INFO_HE]));
1140 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1141 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1142 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1146 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1148 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1149 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1151 cfile->fid.persistent_fid = fid->persistent_fid;
1152 cfile->fid.volatile_fid = fid->volatile_fid;
1153 #ifdef CONFIG_CIFS_DEBUG2
1154 cfile->fid.mid = fid->mid;
1155 #endif /* CIFS_DEBUG2 */
1156 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1158 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1159 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1163 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1164 struct cifs_fid *fid)
1166 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1170 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1171 u64 persistent_fid, u64 volatile_fid,
1172 struct copychunk_ioctl *pcchunk)
1175 unsigned int ret_data_len;
1176 struct resume_key_req *res_key;
1178 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1179 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1180 NULL, 0 /* no input */,
1181 (char **)&res_key, &ret_data_len);
1184 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1185 goto req_res_key_exit;
1187 if (ret_data_len < sizeof(struct resume_key_req)) {
1188 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
1190 goto req_res_key_exit;
1192 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1200 smb2_ioctl_query_info(const unsigned int xid,
1201 struct cifs_tcon *tcon,
1202 __le16 *path, int is_dir,
1205 struct cifs_ses *ses = tcon->ses;
1206 char __user *arg = (char __user *)p;
1207 struct smb_query_info qi;
1208 struct smb_query_info __user *pqi;
1211 struct smb2_query_info_rsp *rsp = NULL;
1212 void *buffer = NULL;
1213 struct smb_rqst rqst[3];
1214 int resp_buftype[3];
1215 struct kvec rsp_iov[3];
1216 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1217 struct cifs_open_parms oparms;
1218 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1219 struct cifs_fid fid;
1220 struct kvec qi_iov[1];
1221 struct kvec close_iov[1];
1223 memset(rqst, 0, sizeof(rqst));
1224 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1225 memset(rsp_iov, 0, sizeof(rsp_iov));
1227 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1230 if (qi.output_buffer_length > 1024)
1233 if (!ses || !(ses->server))
1236 if (smb3_encryption_required(tcon))
1237 flags |= CIFS_TRANSFORM_REQ;
1239 buffer = kmalloc(qi.output_buffer_length, GFP_KERNEL);
1243 if (copy_from_user(buffer, arg + sizeof(struct smb_query_info),
1244 qi.output_buffer_length)) {
1250 memset(&open_iov, 0, sizeof(open_iov));
1251 rqst[0].rq_iov = open_iov;
1252 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1254 memset(&oparms, 0, sizeof(oparms));
1256 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1257 oparms.disposition = FILE_OPEN;
1259 oparms.create_options = CREATE_NOT_FILE;
1261 oparms.create_options = CREATE_NOT_DIR;
1263 oparms.reconnect = false;
1265 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, path);
1268 smb2_set_next_command(tcon, &rqst[0]);
1271 memset(&qi_iov, 0, sizeof(qi_iov));
1272 rqst[1].rq_iov = qi_iov;
1273 rqst[1].rq_nvec = 1;
1275 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1276 qi.file_info_class, qi.info_type,
1277 qi.additional_information,
1278 qi.input_buffer_length,
1279 qi.output_buffer_length, buffer);
1282 smb2_set_next_command(tcon, &rqst[1]);
1283 smb2_set_related(&rqst[1]);
1286 memset(&close_iov, 0, sizeof(close_iov));
1287 rqst[2].rq_iov = close_iov;
1288 rqst[2].rq_nvec = 1;
1290 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1293 smb2_set_related(&rqst[2]);
1295 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1296 resp_buftype, rsp_iov);
1299 pqi = (struct smb_query_info __user *)arg;
1300 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1301 if (le32_to_cpu(rsp->OutputBufferLength) < qi.input_buffer_length)
1302 qi.input_buffer_length = le32_to_cpu(rsp->OutputBufferLength);
1303 if (copy_to_user(&pqi->input_buffer_length, &qi.input_buffer_length,
1304 sizeof(qi.input_buffer_length))) {
1308 if (copy_to_user(pqi + 1, rsp->Buffer, qi.input_buffer_length)) {
1315 SMB2_open_free(&rqst[0]);
1316 SMB2_query_info_free(&rqst[1]);
1317 SMB2_close_free(&rqst[2]);
1318 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1319 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1320 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1325 smb2_copychunk_range(const unsigned int xid,
1326 struct cifsFileInfo *srcfile,
1327 struct cifsFileInfo *trgtfile, u64 src_off,
1328 u64 len, u64 dest_off)
1331 unsigned int ret_data_len;
1332 struct copychunk_ioctl *pcchunk;
1333 struct copychunk_ioctl_rsp *retbuf = NULL;
1334 struct cifs_tcon *tcon;
1335 int chunks_copied = 0;
1336 bool chunk_sizes_updated = false;
1337 ssize_t bytes_written, total_bytes_written = 0;
1339 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1341 if (pcchunk == NULL)
1344 cifs_dbg(FYI, "in smb2_copychunk_range - about to call request res key\n");
1345 /* Request a key from the server to identify the source of the copy */
1346 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1347 srcfile->fid.persistent_fid,
1348 srcfile->fid.volatile_fid, pcchunk);
1350 /* Note: request_res_key sets res_key null only if rc !=0 */
1354 /* For now array only one chunk long, will make more flexible later */
1355 pcchunk->ChunkCount = cpu_to_le32(1);
1356 pcchunk->Reserved = 0;
1357 pcchunk->Reserved2 = 0;
1359 tcon = tlink_tcon(trgtfile->tlink);
1362 pcchunk->SourceOffset = cpu_to_le64(src_off);
1363 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1365 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1367 /* Request server copy to target from src identified by key */
1368 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1369 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1370 true /* is_fsctl */, (char *)pcchunk,
1371 sizeof(struct copychunk_ioctl), (char **)&retbuf,
1375 sizeof(struct copychunk_ioctl_rsp)) {
1376 cifs_dbg(VFS, "invalid cchunk response size\n");
1380 if (retbuf->TotalBytesWritten == 0) {
1381 cifs_dbg(FYI, "no bytes copied\n");
1386 * Check if server claimed to write more than we asked
1388 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1389 le32_to_cpu(pcchunk->Length)) {
1390 cifs_dbg(VFS, "invalid copy chunk response\n");
1394 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1395 cifs_dbg(VFS, "invalid num chunks written\n");
1401 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1402 src_off += bytes_written;
1403 dest_off += bytes_written;
1404 len -= bytes_written;
1405 total_bytes_written += bytes_written;
1407 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1408 le32_to_cpu(retbuf->ChunksWritten),
1409 le32_to_cpu(retbuf->ChunkBytesWritten),
1411 } else if (rc == -EINVAL) {
1412 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1415 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1416 le32_to_cpu(retbuf->ChunksWritten),
1417 le32_to_cpu(retbuf->ChunkBytesWritten),
1418 le32_to_cpu(retbuf->TotalBytesWritten));
1421 * Check if this is the first request using these sizes,
1422 * (ie check if copy succeed once with original sizes
1423 * and check if the server gave us different sizes after
1424 * we already updated max sizes on previous request).
1425 * if not then why is the server returning an error now
1427 if ((chunks_copied != 0) || chunk_sizes_updated)
1430 /* Check that server is not asking us to grow size */
1431 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1432 tcon->max_bytes_chunk)
1433 tcon->max_bytes_chunk =
1434 le32_to_cpu(retbuf->ChunkBytesWritten);
1436 goto cchunk_out; /* server gave us bogus size */
1438 /* No need to change MaxChunks since already set to 1 */
1439 chunk_sizes_updated = true;
1450 return total_bytes_written;
1454 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1455 struct cifs_fid *fid)
1457 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1461 smb2_read_data_offset(char *buf)
1463 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1464 return rsp->DataOffset;
1468 smb2_read_data_length(char *buf, bool in_remaining)
1470 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1473 return le32_to_cpu(rsp->DataRemaining);
1475 return le32_to_cpu(rsp->DataLength);
1480 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1481 struct cifs_io_parms *parms, unsigned int *bytes_read,
1482 char **buf, int *buf_type)
1484 parms->persistent_fid = pfid->persistent_fid;
1485 parms->volatile_fid = pfid->volatile_fid;
1486 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1490 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1491 struct cifs_io_parms *parms, unsigned int *written,
1492 struct kvec *iov, unsigned long nr_segs)
1495 parms->persistent_fid = pfid->persistent_fid;
1496 parms->volatile_fid = pfid->volatile_fid;
1497 return SMB2_write(xid, parms, written, iov, nr_segs);
1500 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1501 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1502 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1504 struct cifsInodeInfo *cifsi;
1507 cifsi = CIFS_I(inode);
1509 /* if file already sparse don't bother setting sparse again */
1510 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1511 return true; /* already sparse */
1513 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1514 return true; /* already not sparse */
1517 * Can't check for sparse support on share the usual way via the
1518 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1519 * since Samba server doesn't set the flag on the share, yet
1520 * supports the set sparse FSCTL and returns sparse correctly
1521 * in the file attributes. If we fail setting sparse though we
1522 * mark that server does not support sparse files for this share
1523 * to avoid repeatedly sending the unsupported fsctl to server
1524 * if the file is repeatedly extended.
1526 if (tcon->broken_sparse_sup)
1529 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1530 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1532 &setsparse, 1, NULL, NULL);
1534 tcon->broken_sparse_sup = true;
1535 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1540 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1542 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1548 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1549 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1551 __le64 eof = cpu_to_le64(size);
1552 struct inode *inode;
1555 * If extending file more than one page make sparse. Many Linux fs
1556 * make files sparse by default when extending via ftruncate
1558 inode = d_inode(cfile->dentry);
1560 if (!set_alloc && (size > inode->i_size + 8192)) {
1561 __u8 set_sparse = 1;
1563 /* whether set sparse succeeds or not, extend the file */
1564 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1567 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1568 cfile->fid.volatile_fid, cfile->pid, &eof);
1572 smb2_duplicate_extents(const unsigned int xid,
1573 struct cifsFileInfo *srcfile,
1574 struct cifsFileInfo *trgtfile, u64 src_off,
1575 u64 len, u64 dest_off)
1578 unsigned int ret_data_len;
1579 struct duplicate_extents_to_file dup_ext_buf;
1580 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1582 /* server fileays advertise duplicate extent support with this flag */
1583 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1584 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1587 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1588 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1589 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1590 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1591 dup_ext_buf.ByteCount = cpu_to_le64(len);
1592 cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
1593 src_off, dest_off, len);
1595 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1597 goto duplicate_extents_out;
1599 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1600 trgtfile->fid.volatile_fid,
1601 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
1602 true /* is_fsctl */,
1603 (char *)&dup_ext_buf,
1604 sizeof(struct duplicate_extents_to_file),
1608 if (ret_data_len > 0)
1609 cifs_dbg(FYI, "non-zero response length in duplicate extents");
1611 duplicate_extents_out:
1616 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1617 struct cifsFileInfo *cfile)
1619 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1620 cfile->fid.volatile_fid);
1624 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1625 struct cifsFileInfo *cfile)
1627 struct fsctl_set_integrity_information_req integr_info;
1628 unsigned int ret_data_len;
1630 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1631 integr_info.Flags = 0;
1632 integr_info.Reserved = 0;
1634 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1635 cfile->fid.volatile_fid,
1636 FSCTL_SET_INTEGRITY_INFORMATION,
1637 true /* is_fsctl */,
1638 (char *)&integr_info,
1639 sizeof(struct fsctl_set_integrity_information_req),
1645 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
1646 #define GMT_TOKEN_SIZE 50
1649 * Input buffer contains (empty) struct smb_snapshot array with size filled in
1650 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
1653 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1654 struct cifsFileInfo *cfile, void __user *ioc_buf)
1656 char *retbuf = NULL;
1657 unsigned int ret_data_len = 0;
1659 struct smb_snapshot_array snapshot_in;
1661 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1662 cfile->fid.volatile_fid,
1663 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
1664 true /* is_fsctl */,
1665 NULL, 0 /* no input data */,
1668 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1673 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1675 if (copy_from_user(&snapshot_in, ioc_buf,
1676 sizeof(struct smb_snapshot_array))) {
1683 * Check for min size, ie not large enough to fit even one GMT
1684 * token (snapshot). On the first ioctl some users may pass in
1685 * smaller size (or zero) to simply get the size of the array
1686 * so the user space caller can allocate sufficient memory
1687 * and retry the ioctl again with larger array size sufficient
1688 * to hold all of the snapshot GMT tokens on the second try.
1690 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
1691 ret_data_len = sizeof(struct smb_snapshot_array);
1694 * We return struct SRV_SNAPSHOT_ARRAY, followed by
1695 * the snapshot array (of 50 byte GMT tokens) each
1696 * representing an available previous version of the data
1698 if (ret_data_len > (snapshot_in.snapshot_array_size +
1699 sizeof(struct smb_snapshot_array)))
1700 ret_data_len = snapshot_in.snapshot_array_size +
1701 sizeof(struct smb_snapshot_array);
1703 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1712 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1713 const char *path, struct cifs_sb_info *cifs_sb,
1714 struct cifs_fid *fid, __u16 search_flags,
1715 struct cifs_search_info *srch_inf)
1719 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1720 struct cifs_open_parms oparms;
1722 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1727 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1728 oparms.disposition = FILE_OPEN;
1729 if (backup_cred(cifs_sb))
1730 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1732 oparms.create_options = 0;
1734 oparms.reconnect = false;
1736 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
1739 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
1743 srch_inf->entries_in_buffer = 0;
1744 srch_inf->index_of_last_entry = 2;
1746 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1747 fid->volatile_fid, 0, srch_inf);
1749 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
1750 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1756 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1757 struct cifs_fid *fid, __u16 search_flags,
1758 struct cifs_search_info *srch_inf)
1760 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1761 fid->volatile_fid, 0, srch_inf);
1765 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1766 struct cifs_fid *fid)
1768 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1772 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
1773 * the number of credits and return true. Otherwise - return false.
1776 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
1778 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
1780 if (shdr->Status != STATUS_PENDING)
1784 spin_lock(&server->req_lock);
1785 server->credits += le16_to_cpu(shdr->CreditRequest);
1786 spin_unlock(&server->req_lock);
1787 wake_up(&server->request_q);
1794 smb2_is_session_expired(char *buf)
1796 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
1798 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
1799 shdr->Status != STATUS_USER_SESSION_DELETED)
1802 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
1803 le16_to_cpu(shdr->Command),
1804 le64_to_cpu(shdr->MessageId));
1805 cifs_dbg(FYI, "Session expired or deleted\n");
1811 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
1812 struct cifsInodeInfo *cinode)
1814 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
1815 return SMB2_lease_break(0, tcon, cinode->lease_key,
1816 smb2_get_lease_state(cinode));
1818 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
1820 CIFS_CACHE_READ(cinode) ? 1 : 0);
1824 smb2_set_related(struct smb_rqst *rqst)
1826 struct smb2_sync_hdr *shdr;
1828 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
1829 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
1832 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
1835 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
1837 struct smb2_sync_hdr *shdr;
1838 struct cifs_ses *ses = tcon->ses;
1839 struct TCP_Server_Info *server = ses->server;
1840 unsigned long len = smb_rqst_len(server, rqst);
1843 /* SMB headers in a compound are 8 byte aligned. */
1845 /* No padding needed */
1849 num_padding = 8 - (len & 7);
1850 if (!smb3_encryption_required(tcon)) {
1852 * If we do not have encryption then we can just add an extra
1853 * iov for the padding.
1855 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
1856 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
1861 * We can not add a small padding iov for the encryption case
1862 * because the encryption framework can not handle the padding
1864 * We have to flatten this into a single buffer and add
1865 * the padding to it.
1867 for (i = 1; i < rqst->rq_nvec; i++) {
1868 memcpy(rqst->rq_iov[0].iov_base +
1869 rqst->rq_iov[0].iov_len,
1870 rqst->rq_iov[i].iov_base,
1871 rqst->rq_iov[i].iov_len);
1872 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
1874 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
1876 rqst->rq_iov[0].iov_len += num_padding;
1882 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
1883 shdr->NextCommand = cpu_to_le32(len);
1887 * Passes the query info response back to the caller on success.
1888 * Caller need to free this with free_rsp_buf().
1891 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
1892 __le16 *utf16_path, u32 desired_access,
1893 u32 class, u32 type, u32 output_len,
1894 struct kvec *rsp, int *buftype,
1895 struct cifs_sb_info *cifs_sb)
1897 struct cifs_ses *ses = tcon->ses;
1899 struct smb_rqst rqst[3];
1900 int resp_buftype[3];
1901 struct kvec rsp_iov[3];
1902 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1903 struct kvec qi_iov[1];
1904 struct kvec close_iov[1];
1905 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1906 struct cifs_open_parms oparms;
1907 struct cifs_fid fid;
1910 if (smb3_encryption_required(tcon))
1911 flags |= CIFS_TRANSFORM_REQ;
1913 memset(rqst, 0, sizeof(rqst));
1914 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1915 memset(rsp_iov, 0, sizeof(rsp_iov));
1917 memset(&open_iov, 0, sizeof(open_iov));
1918 rqst[0].rq_iov = open_iov;
1919 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1922 oparms.desired_access = desired_access;
1923 oparms.disposition = FILE_OPEN;
1924 if (cifs_sb && backup_cred(cifs_sb))
1925 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1927 oparms.create_options = 0;
1929 oparms.reconnect = false;
1931 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
1934 smb2_set_next_command(tcon, &rqst[0]);
1936 memset(&qi_iov, 0, sizeof(qi_iov));
1937 rqst[1].rq_iov = qi_iov;
1938 rqst[1].rq_nvec = 1;
1940 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1946 smb2_set_next_command(tcon, &rqst[1]);
1947 smb2_set_related(&rqst[1]);
1949 memset(&close_iov, 0, sizeof(close_iov));
1950 rqst[2].rq_iov = close_iov;
1951 rqst[2].rq_nvec = 1;
1953 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1956 smb2_set_related(&rqst[2]);
1958 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1959 resp_buftype, rsp_iov);
1961 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1965 *buftype = resp_buftype[1];
1968 SMB2_open_free(&rqst[0]);
1969 SMB2_query_info_free(&rqst[1]);
1970 SMB2_close_free(&rqst[2]);
1971 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1972 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1977 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1978 struct kstatfs *buf)
1980 struct smb2_query_info_rsp *rsp;
1981 struct smb2_fs_full_size_info *info = NULL;
1982 __le16 utf16_path = 0; /* Null - open root of share */
1983 struct kvec rsp_iov = {NULL, 0};
1984 int buftype = CIFS_NO_BUFFER;
1988 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
1989 FILE_READ_ATTRIBUTES,
1990 FS_FULL_SIZE_INFORMATION,
1991 SMB2_O_INFO_FILESYSTEM,
1992 sizeof(struct smb2_fs_full_size_info),
1993 &rsp_iov, &buftype, NULL);
1997 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1998 buf->f_type = SMB2_MAGIC_NUMBER;
1999 info = (struct smb2_fs_full_size_info *)(
2000 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2001 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2002 le32_to_cpu(rsp->OutputBufferLength),
2004 sizeof(struct smb2_fs_full_size_info));
2006 smb2_copy_fs_info_to_kstatfs(info, buf);
2009 free_rsp_buf(buftype, rsp_iov.iov_base);
2014 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2015 struct kstatfs *buf)
2018 __le16 srch_path = 0; /* Null - open root of share */
2019 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2020 struct cifs_open_parms oparms;
2021 struct cifs_fid fid;
2023 if (!tcon->posix_extensions)
2024 return smb2_queryfs(xid, tcon, buf);
2027 oparms.desired_access = FILE_READ_ATTRIBUTES;
2028 oparms.disposition = FILE_OPEN;
2029 oparms.create_options = 0;
2031 oparms.reconnect = false;
2033 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
2037 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2038 fid.volatile_fid, buf);
2039 buf->f_type = SMB2_MAGIC_NUMBER;
2040 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2045 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2047 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2048 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2052 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2053 __u64 length, __u32 type, int lock, int unlock, bool wait)
2055 if (unlock && !lock)
2056 type = SMB2_LOCKFLAG_UNLOCK;
2057 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2058 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2059 current->tgid, length, offset, type, wait);
2063 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2065 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2069 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2071 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2075 smb2_new_lease_key(struct cifs_fid *fid)
2077 generate_random_uuid(fid->lease_key);
2081 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2082 const char *search_name,
2083 struct dfs_info3_param **target_nodes,
2084 unsigned int *num_of_nodes,
2085 const struct nls_table *nls_codepage, int remap)
2088 __le16 *utf16_path = NULL;
2089 int utf16_path_len = 0;
2090 struct cifs_tcon *tcon;
2091 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2092 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2093 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2095 cifs_dbg(FYI, "smb2_get_dfs_refer path <%s>\n", search_name);
2098 * Try to use the IPC tcon, otherwise just use any
2100 tcon = ses->tcon_ipc;
2102 spin_lock(&cifs_tcp_ses_lock);
2103 tcon = list_first_entry_or_null(&ses->tcon_list,
2108 spin_unlock(&cifs_tcp_ses_lock);
2112 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2118 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2120 nls_codepage, remap);
2126 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2127 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2133 /* Highest DFS referral version understood */
2134 dfs_req->MaxReferralLevel = DFS_VERSION;
2136 /* Path to resolve in an UTF-16 null-terminated string */
2137 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2140 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2141 FSCTL_DFS_GET_REFERRALS,
2142 true /* is_fsctl */,
2143 (char *)dfs_req, dfs_req_size,
2144 (char **)&dfs_rsp, &dfs_rsp_size);
2145 } while (rc == -EAGAIN);
2148 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
2149 cifs_dbg(VFS, "ioctl error in smb2_get_dfs_refer rc=%d\n", rc);
2153 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2154 num_of_nodes, target_nodes,
2155 nls_codepage, remap, search_name,
2156 true /* is_unicode */);
2158 cifs_dbg(VFS, "parse error in smb2_get_dfs_refer rc=%d\n", rc);
2163 if (tcon && !tcon->ipc) {
2164 /* ipc tcons are not refcounted */
2165 spin_lock(&cifs_tcp_ses_lock);
2167 spin_unlock(&cifs_tcp_ses_lock);
2174 #define SMB2_SYMLINK_STRUCT_SIZE \
2175 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2178 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
2179 const char *full_path, char **target_path,
2180 struct cifs_sb_info *cifs_sb)
2184 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2185 struct cifs_open_parms oparms;
2186 struct cifs_fid fid;
2187 struct kvec err_iov = {NULL, 0};
2188 struct smb2_err_rsp *err_buf = NULL;
2190 struct smb2_symlink_err_rsp *symlink;
2191 unsigned int sub_len;
2192 unsigned int sub_offset;
2193 unsigned int print_len;
2194 unsigned int print_offset;
2196 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2198 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2203 oparms.desired_access = FILE_READ_ATTRIBUTES;
2204 oparms.disposition = FILE_OPEN;
2205 if (backup_cred(cifs_sb))
2206 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2208 oparms.create_options = 0;
2210 oparms.reconnect = false;
2212 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_iov,
2214 if (!rc || !err_iov.iov_base) {
2219 err_buf = err_iov.iov_base;
2220 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
2221 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
2226 /* open must fail on symlink - reset rc */
2228 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
2229 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
2230 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
2231 print_len = le16_to_cpu(symlink->PrintNameLength);
2232 print_offset = le16_to_cpu(symlink->PrintNameOffset);
2234 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
2239 if (err_iov.iov_len <
2240 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
2245 *target_path = cifs_strndup_from_utf16(
2246 (char *)symlink->PathBuffer + sub_offset,
2247 sub_len, true, cifs_sb->local_nls);
2248 if (!(*target_path)) {
2252 convert_delimiter(*target_path, '/');
2253 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2256 free_rsp_buf(resp_buftype, err_buf);
2262 #ifdef CONFIG_CIFS_ACL
2263 static struct cifs_ntsd *
2264 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
2265 const struct cifs_fid *cifsfid, u32 *pacllen)
2267 struct cifs_ntsd *pntsd = NULL;
2269 int rc = -EOPNOTSUPP;
2270 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2273 return ERR_CAST(tlink);
2276 cifs_dbg(FYI, "trying to get acl\n");
2278 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
2279 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
2282 cifs_put_tlink(tlink);
2284 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2291 static struct cifs_ntsd *
2292 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
2293 const char *path, u32 *pacllen)
2295 struct cifs_ntsd *pntsd = NULL;
2296 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2299 struct cifs_tcon *tcon;
2300 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2301 struct cifs_fid fid;
2302 struct cifs_open_parms oparms;
2305 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
2307 return ERR_CAST(tlink);
2309 tcon = tlink_tcon(tlink);
2312 if (backup_cred(cifs_sb))
2313 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2315 oparms.create_options = 0;
2317 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2325 oparms.desired_access = READ_CONTROL;
2326 oparms.disposition = FILE_OPEN;
2328 oparms.reconnect = false;
2330 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2333 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2334 fid.volatile_fid, (void **)&pntsd, pacllen);
2335 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2338 cifs_put_tlink(tlink);
2341 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2347 #ifdef CONFIG_CIFS_ACL
2349 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
2350 struct inode *inode, const char *path, int aclflag)
2352 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2354 int rc, access_flags = 0;
2355 struct cifs_tcon *tcon;
2356 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2357 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2358 struct cifs_fid fid;
2359 struct cifs_open_parms oparms;
2362 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
2364 return PTR_ERR(tlink);
2366 tcon = tlink_tcon(tlink);
2369 if (backup_cred(cifs_sb))
2370 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2372 oparms.create_options = 0;
2374 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
2375 access_flags = WRITE_OWNER;
2377 access_flags = WRITE_DAC;
2379 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2387 oparms.desired_access = access_flags;
2388 oparms.disposition = FILE_OPEN;
2391 oparms.reconnect = false;
2393 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2396 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2397 fid.volatile_fid, pnntsd, acllen, aclflag);
2398 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2401 cifs_put_tlink(tlink);
2405 #endif /* CIFS_ACL */
2407 /* Retrieve an ACL from the server */
2408 static struct cifs_ntsd *
2409 get_smb2_acl(struct cifs_sb_info *cifs_sb,
2410 struct inode *inode, const char *path,
2413 struct cifs_ntsd *pntsd = NULL;
2414 struct cifsFileInfo *open_file = NULL;
2417 open_file = find_readable_file(CIFS_I(inode), true);
2419 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
2421 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
2422 cifsFileInfo_put(open_file);
2427 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
2428 loff_t offset, loff_t len, bool keep_size)
2430 struct inode *inode;
2431 struct cifsInodeInfo *cifsi;
2432 struct cifsFileInfo *cfile = file->private_data;
2433 struct file_zero_data_information fsctl_buf;
2439 inode = d_inode(cfile->dentry);
2440 cifsi = CIFS_I(inode);
2442 /* if file not oplocked can't be sure whether asking to extend size */
2443 if (!CIFS_CACHE_READ(cifsi))
2444 if (keep_size == false) {
2451 * Must check if file sparse since fallocate -z (zero range) assumes
2452 * non-sparse allocation
2454 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
2461 * need to make sure we are not asked to extend the file since the SMB3
2462 * fsctl does not change the file size. In the future we could change
2463 * this to zero the first part of the range then set the file size
2464 * which for a non sparse file would zero the newly extended range
2466 if (keep_size == false)
2467 if (i_size_read(inode) < offset + len) {
2473 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
2475 fsctl_buf.FileOffset = cpu_to_le64(offset);
2476 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2478 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2479 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
2480 true /* is_fctl */, (char *)&fsctl_buf,
2481 sizeof(struct file_zero_data_information), NULL, NULL);
2486 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
2487 loff_t offset, loff_t len)
2489 struct inode *inode;
2490 struct cifsInodeInfo *cifsi;
2491 struct cifsFileInfo *cfile = file->private_data;
2492 struct file_zero_data_information fsctl_buf;
2495 __u8 set_sparse = 1;
2499 inode = d_inode(cfile->dentry);
2500 cifsi = CIFS_I(inode);
2502 /* Need to make file sparse, if not already, before freeing range. */
2503 /* Consider adding equivalent for compressed since it could also work */
2504 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
2510 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
2512 fsctl_buf.FileOffset = cpu_to_le64(offset);
2513 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2515 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2516 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
2517 true /* is_fctl */, (char *)&fsctl_buf,
2518 sizeof(struct file_zero_data_information), NULL, NULL);
2523 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
2524 loff_t off, loff_t len, bool keep_size)
2526 struct inode *inode;
2527 struct cifsInodeInfo *cifsi;
2528 struct cifsFileInfo *cfile = file->private_data;
2529 long rc = -EOPNOTSUPP;
2534 inode = d_inode(cfile->dentry);
2535 cifsi = CIFS_I(inode);
2537 /* if file not oplocked can't be sure whether asking to extend size */
2538 if (!CIFS_CACHE_READ(cifsi))
2539 if (keep_size == false) {
2545 * Files are non-sparse by default so falloc may be a no-op
2546 * Must check if file sparse. If not sparse, and not extending
2547 * then no need to do anything since file already allocated
2549 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
2550 if (keep_size == true)
2552 /* check if extending file */
2553 else if (i_size_read(inode) >= off + len)
2554 /* not extending file and already not sparse */
2556 /* BB: in future add else clause to extend file */
2563 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
2565 * Check if falloc starts within first few pages of file
2566 * and ends within a few pages of the end of file to
2567 * ensure that most of file is being forced to be
2568 * fallocated now. If so then setting whole file sparse
2569 * ie potentially making a few extra pages at the beginning
2570 * or end of the file non-sparse via set_sparse is harmless.
2572 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
2578 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
2580 /* BB: else ... in future add code to extend file and set sparse */
2588 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
2589 loff_t off, loff_t len)
2591 /* KEEP_SIZE already checked for by do_fallocate */
2592 if (mode & FALLOC_FL_PUNCH_HOLE)
2593 return smb3_punch_hole(file, tcon, off, len);
2594 else if (mode & FALLOC_FL_ZERO_RANGE) {
2595 if (mode & FALLOC_FL_KEEP_SIZE)
2596 return smb3_zero_range(file, tcon, off, len, true);
2597 return smb3_zero_range(file, tcon, off, len, false);
2598 } else if (mode == FALLOC_FL_KEEP_SIZE)
2599 return smb3_simple_falloc(file, tcon, off, len, true);
2601 return smb3_simple_falloc(file, tcon, off, len, false);
2607 smb2_downgrade_oplock(struct TCP_Server_Info *server,
2608 struct cifsInodeInfo *cinode, bool set_level2)
2611 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
2614 server->ops->set_oplock_level(cinode, 0, 0, NULL);
2618 smb21_downgrade_oplock(struct TCP_Server_Info *server,
2619 struct cifsInodeInfo *cinode, bool set_level2)
2621 server->ops->set_oplock_level(cinode,
2622 set_level2 ? SMB2_LEASE_READ_CACHING_HE :
2627 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2628 unsigned int epoch, bool *purge_cache)
2631 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
2633 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2634 cinode->oplock = CIFS_CACHE_RHW_FLG;
2635 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
2636 &cinode->vfs_inode);
2637 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
2638 cinode->oplock = CIFS_CACHE_RW_FLG;
2639 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
2640 &cinode->vfs_inode);
2641 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
2642 cinode->oplock = CIFS_CACHE_READ_FLG;
2643 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
2644 &cinode->vfs_inode);
2650 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2651 unsigned int epoch, bool *purge_cache)
2653 char message[5] = {0};
2656 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
2660 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
2661 cinode->oplock |= CIFS_CACHE_READ_FLG;
2662 strcat(message, "R");
2664 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
2665 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
2666 strcat(message, "H");
2668 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
2669 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
2670 strcat(message, "W");
2672 if (!cinode->oplock)
2673 strcat(message, "None");
2674 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
2675 &cinode->vfs_inode);
2679 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2680 unsigned int epoch, bool *purge_cache)
2682 unsigned int old_oplock = cinode->oplock;
2684 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
2687 *purge_cache = false;
2688 if (old_oplock == CIFS_CACHE_READ_FLG) {
2689 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
2690 (epoch - cinode->epoch > 0))
2691 *purge_cache = true;
2692 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2693 (epoch - cinode->epoch > 1))
2694 *purge_cache = true;
2695 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2696 (epoch - cinode->epoch > 1))
2697 *purge_cache = true;
2698 else if (cinode->oplock == 0 &&
2699 (epoch - cinode->epoch > 0))
2700 *purge_cache = true;
2701 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
2702 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2703 (epoch - cinode->epoch > 0))
2704 *purge_cache = true;
2705 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2706 (epoch - cinode->epoch > 1))
2707 *purge_cache = true;
2709 cinode->epoch = epoch;
2714 smb2_is_read_op(__u32 oplock)
2716 return oplock == SMB2_OPLOCK_LEVEL_II;
2720 smb21_is_read_op(__u32 oplock)
2722 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
2723 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
2727 map_oplock_to_lease(u8 oplock)
2729 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
2730 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
2731 else if (oplock == SMB2_OPLOCK_LEVEL_II)
2732 return SMB2_LEASE_READ_CACHING;
2733 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
2734 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
2735 SMB2_LEASE_WRITE_CACHING;
2740 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
2742 struct create_lease *buf;
2744 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
2748 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
2749 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2751 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2752 (struct create_lease, lcontext));
2753 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
2754 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2755 (struct create_lease, Name));
2756 buf->ccontext.NameLength = cpu_to_le16(4);
2757 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
2766 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
2768 struct create_lease_v2 *buf;
2770 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
2774 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
2775 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2777 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2778 (struct create_lease_v2, lcontext));
2779 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
2780 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2781 (struct create_lease_v2, Name));
2782 buf->ccontext.NameLength = cpu_to_le16(4);
2783 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
2792 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
2794 struct create_lease *lc = (struct create_lease *)buf;
2796 *epoch = 0; /* not used */
2797 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2798 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2799 return le32_to_cpu(lc->lcontext.LeaseState);
2803 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
2805 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
2807 *epoch = le16_to_cpu(lc->lcontext.Epoch);
2808 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2809 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2811 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
2812 return le32_to_cpu(lc->lcontext.LeaseState);
2816 smb2_wp_retry_size(struct inode *inode)
2818 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
2819 SMB2_MAX_BUFFER_SIZE);
2823 smb2_dir_needs_close(struct cifsFileInfo *cfile)
2825 return !cfile->invalidHandle;
2829 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
2830 struct smb_rqst *old_rq)
2832 struct smb2_sync_hdr *shdr =
2833 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
2835 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
2836 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
2837 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
2838 tr_hdr->Flags = cpu_to_le16(0x01);
2839 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2840 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
2843 /* We can not use the normal sg_set_buf() as we will sometimes pass a
2844 * stack object as buf.
2846 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
2847 unsigned int buflen)
2849 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
2852 /* Assumes the first rqst has a transform header as the first iov.
2854 * rqst[0].rq_iov[0] is transform header
2855 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
2856 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
2858 static struct scatterlist *
2859 init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
2861 unsigned int sg_len;
2862 struct scatterlist *sg;
2865 unsigned int idx = 0;
2869 for (i = 0; i < num_rqst; i++)
2870 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
2872 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
2876 sg_init_table(sg, sg_len);
2877 for (i = 0; i < num_rqst; i++) {
2878 for (j = 0; j < rqst[i].rq_nvec; j++) {
2880 * The first rqst has a transform header where the
2881 * first 20 bytes are not part of the encrypted blob
2883 skip = (i == 0) && (j == 0) ? 20 : 0;
2884 smb2_sg_set_buf(&sg[idx++],
2885 rqst[i].rq_iov[j].iov_base + skip,
2886 rqst[i].rq_iov[j].iov_len - skip);
2889 for (j = 0; j < rqst[i].rq_npages; j++) {
2890 unsigned int len, offset;
2892 rqst_page_get_length(&rqst[i], j, &len, &offset);
2893 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
2896 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
2901 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
2903 struct cifs_ses *ses;
2906 spin_lock(&cifs_tcp_ses_lock);
2907 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2908 if (ses->Suid != ses_id)
2910 ses_enc_key = enc ? ses->smb3encryptionkey :
2911 ses->smb3decryptionkey;
2912 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
2913 spin_unlock(&cifs_tcp_ses_lock);
2916 spin_unlock(&cifs_tcp_ses_lock);
2921 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
2922 * iov[0] - transform header (associate data),
2923 * iov[1-N] - SMB2 header and pages - data to encrypt.
2924 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
2928 crypt_message(struct TCP_Server_Info *server, int num_rqst,
2929 struct smb_rqst *rqst, int enc)
2931 struct smb2_transform_hdr *tr_hdr =
2932 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
2933 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
2935 struct scatterlist *sg;
2936 u8 sign[SMB2_SIGNATURE_SIZE] = {};
2937 u8 key[SMB3_SIGN_KEY_SIZE];
2938 struct aead_request *req;
2940 unsigned int iv_len;
2941 DECLARE_CRYPTO_WAIT(wait);
2942 struct crypto_aead *tfm;
2943 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2945 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
2947 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
2952 rc = smb3_crypto_aead_allocate(server);
2954 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
2958 tfm = enc ? server->secmech.ccmaesencrypt :
2959 server->secmech.ccmaesdecrypt;
2960 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
2962 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
2966 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
2968 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
2972 req = aead_request_alloc(tfm, GFP_KERNEL);
2974 cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
2979 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
2980 crypt_len += SMB2_SIGNATURE_SIZE;
2983 sg = init_sg(num_rqst, rqst, sign);
2985 cifs_dbg(VFS, "%s: Failed to init sg", __func__);
2990 iv_len = crypto_aead_ivsize(tfm);
2991 iv = kzalloc(iv_len, GFP_KERNEL);
2993 cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
2998 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
3000 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
3001 aead_request_set_ad(req, assoc_data_len);
3003 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3004 crypto_req_done, &wait);
3006 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
3007 : crypto_aead_decrypt(req), &wait);
3010 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
3021 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
3025 for (i = 0; i < num_rqst; i++) {
3026 if (rqst[i].rq_pages) {
3027 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
3028 put_page(rqst[i].rq_pages[j]);
3029 kfree(rqst[i].rq_pages);
3035 * This function will initialize new_rq and encrypt the content.
3036 * The first entry, new_rq[0], only contains a single iov which contains
3037 * a smb2_transform_hdr and is pre-allocated by the caller.
3038 * This function then populates new_rq[1+] with the content from olq_rq[0+].
3040 * The end result is an array of smb_rqst structures where the first structure
3041 * only contains a single iov for the transform header which we then can pass
3042 * to crypt_message().
3044 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
3045 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
3048 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
3049 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
3051 struct page **pages;
3052 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
3053 unsigned int npages;
3054 unsigned int orig_len = 0;
3058 for (i = 1; i < num_rqst; i++) {
3059 npages = old_rq[i - 1].rq_npages;
3060 pages = kmalloc_array(npages, sizeof(struct page *),
3065 new_rq[i].rq_pages = pages;
3066 new_rq[i].rq_npages = npages;
3067 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
3068 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
3069 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
3070 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
3071 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
3073 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
3075 for (j = 0; j < npages; j++) {
3076 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3081 /* copy pages form the old */
3082 for (j = 0; j < npages; j++) {
3084 unsigned int offset, len;
3086 rqst_page_get_length(&new_rq[i], j, &len, &offset);
3088 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
3089 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
3091 memcpy(dst, src, len);
3092 kunmap(new_rq[i].rq_pages[j]);
3093 kunmap(old_rq[i - 1].rq_pages[j]);
3097 /* fill the 1st iov with a transform header */
3098 fill_transform_hdr(tr_hdr, orig_len, old_rq);
3100 rc = crypt_message(server, num_rqst, new_rq, 1);
3101 cifs_dbg(FYI, "encrypt message returned %d", rc);
3108 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
3113 smb3_is_transform_hdr(void *buf)
3115 struct smb2_transform_hdr *trhdr = buf;
3117 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
3121 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
3122 unsigned int buf_data_size, struct page **pages,
3123 unsigned int npages, unsigned int page_data_size)
3126 struct smb_rqst rqst = {NULL};
3129 iov[0].iov_base = buf;
3130 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
3131 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
3132 iov[1].iov_len = buf_data_size;
3136 rqst.rq_pages = pages;
3137 rqst.rq_npages = npages;
3138 rqst.rq_pagesz = PAGE_SIZE;
3139 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
3141 rc = crypt_message(server, 1, &rqst, 0);
3142 cifs_dbg(FYI, "decrypt message returned %d\n", rc);
3147 memmove(buf, iov[1].iov_base, buf_data_size);
3149 server->total_read = buf_data_size + page_data_size;
3155 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
3156 unsigned int npages, unsigned int len)
3161 for (i = 0; i < npages; i++) {
3162 struct page *page = pages[i];
3166 if (len >= PAGE_SIZE) {
3167 /* enough data to fill the page */
3171 zero_user(page, len, PAGE_SIZE - len);
3174 length = cifs_read_page_from_socket(server, page, 0, n);
3177 server->total_read += length;
3184 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
3185 unsigned int cur_off, struct bio_vec **page_vec)
3187 struct bio_vec *bvec;
3190 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
3194 for (i = 0; i < npages; i++) {
3195 bvec[i].bv_page = pages[i];
3196 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
3197 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
3198 data_size -= bvec[i].bv_len;
3201 if (data_size != 0) {
3202 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
3212 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
3213 char *buf, unsigned int buf_len, struct page **pages,
3214 unsigned int npages, unsigned int page_data_size)
3216 unsigned int data_offset;
3217 unsigned int data_len;
3218 unsigned int cur_off;
3219 unsigned int cur_page_idx;
3220 unsigned int pad_len;
3221 struct cifs_readdata *rdata = mid->callback_data;
3222 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
3223 struct bio_vec *bvec = NULL;
3224 struct iov_iter iter;
3227 bool use_rdma_mr = false;
3229 if (shdr->Command != SMB2_READ) {
3230 cifs_dbg(VFS, "only big read responses are supported\n");
3234 if (server->ops->is_session_expired &&
3235 server->ops->is_session_expired(buf)) {
3236 cifs_reconnect(server);
3237 wake_up(&server->response_q);
3241 if (server->ops->is_status_pending &&
3242 server->ops->is_status_pending(buf, server, 0))
3245 /* set up first two iov to get credits */
3246 rdata->iov[0].iov_base = buf;
3247 rdata->iov[0].iov_len = 4;
3248 rdata->iov[1].iov_base = buf + 4;
3249 rdata->iov[1].iov_len =
3250 min_t(unsigned int, buf_len, server->vals->read_rsp_size) - 4;
3251 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
3252 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
3253 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
3254 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
3256 rdata->result = server->ops->map_error(buf, true);
3257 if (rdata->result != 0) {
3258 cifs_dbg(FYI, "%s: server returned error %d\n",
3259 __func__, rdata->result);
3260 /* normal error on read response */
3261 dequeue_mid(mid, false);
3265 data_offset = server->ops->read_data_offset(buf);
3266 #ifdef CONFIG_CIFS_SMB_DIRECT
3267 use_rdma_mr = rdata->mr;
3269 data_len = server->ops->read_data_length(buf, use_rdma_mr);
3271 if (data_offset < server->vals->read_rsp_size) {
3273 * win2k8 sometimes sends an offset of 0 when the read
3274 * is beyond the EOF. Treat it as if the data starts just after
3277 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
3278 __func__, data_offset);
3279 data_offset = server->vals->read_rsp_size;
3280 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
3281 /* data_offset is beyond the end of smallbuf */
3282 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
3283 __func__, data_offset);
3284 rdata->result = -EIO;
3285 dequeue_mid(mid, rdata->result);
3289 pad_len = data_offset - server->vals->read_rsp_size;
3291 if (buf_len <= data_offset) {
3292 /* read response payload is in pages */
3293 cur_page_idx = pad_len / PAGE_SIZE;
3294 cur_off = pad_len % PAGE_SIZE;
3296 if (cur_page_idx != 0) {
3297 /* data offset is beyond the 1st page of response */
3298 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
3299 __func__, data_offset);
3300 rdata->result = -EIO;
3301 dequeue_mid(mid, rdata->result);
3305 if (data_len > page_data_size - pad_len) {
3306 /* data_len is corrupt -- discard frame */
3307 rdata->result = -EIO;
3308 dequeue_mid(mid, rdata->result);
3312 rdata->result = init_read_bvec(pages, npages, page_data_size,
3314 if (rdata->result != 0) {
3315 dequeue_mid(mid, rdata->result);
3319 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
3320 } else if (buf_len >= data_offset + data_len) {
3321 /* read response payload is in buf */
3322 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
3323 iov.iov_base = buf + data_offset;
3324 iov.iov_len = data_len;
3325 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
3327 /* read response payload cannot be in both buf and pages */
3328 WARN_ONCE(1, "buf can not contain only a part of read data");
3329 rdata->result = -EIO;
3330 dequeue_mid(mid, rdata->result);
3334 length = rdata->copy_into_pages(server, rdata, &iter);
3341 dequeue_mid(mid, false);
3346 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
3348 char *buf = server->smallbuf;
3349 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3350 unsigned int npages;
3351 struct page **pages;
3353 unsigned int buflen = server->pdu_size;
3357 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
3358 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
3360 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
3363 server->total_read += rc;
3365 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
3366 server->vals->read_rsp_size;
3367 npages = DIV_ROUND_UP(len, PAGE_SIZE);
3369 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
3375 for (; i < npages; i++) {
3376 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3383 /* read read data into pages */
3384 rc = read_data_into_pages(server, pages, npages, len);
3388 rc = cifs_discard_remaining_data(server);
3392 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
3393 pages, npages, len);
3397 *mid = smb2_find_mid(server, buf);
3399 cifs_dbg(FYI, "mid not found\n");
3401 cifs_dbg(FYI, "mid found\n");
3402 (*mid)->decrypted = true;
3403 rc = handle_read_data(server, *mid, buf,
3404 server->vals->read_rsp_size,
3405 pages, npages, len);
3409 for (i = i - 1; i >= 0; i--)
3414 cifs_discard_remaining_data(server);
3419 receive_encrypted_standard(struct TCP_Server_Info *server,
3420 struct mid_q_entry **mids, char **bufs,
3424 char *buf = server->smallbuf;
3426 struct smb2_sync_hdr *shdr;
3427 unsigned int pdu_length = server->pdu_size;
3428 unsigned int buf_size;
3429 struct mid_q_entry *mid_entry;
3431 char *next_buffer = NULL;
3435 /* switch to large buffer if too big for a small one */
3436 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
3437 server->large_buf = true;
3438 memcpy(server->bigbuf, buf, server->total_read);
3439 buf = server->bigbuf;
3442 /* now read the rest */
3443 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
3444 pdu_length - HEADER_SIZE(server) + 1);
3447 server->total_read += length;
3449 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
3450 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
3454 next_is_large = server->large_buf;
3456 shdr = (struct smb2_sync_hdr *)buf;
3457 if (shdr->NextCommand) {
3458 if (next_is_large) {
3459 tmpbuf = server->bigbuf;
3460 next_buffer = (char *)cifs_buf_get();
3462 tmpbuf = server->smallbuf;
3463 next_buffer = (char *)cifs_small_buf_get();
3466 tmpbuf + le32_to_cpu(shdr->NextCommand),
3467 pdu_length - le32_to_cpu(shdr->NextCommand));
3470 mid_entry = smb2_find_mid(server, buf);
3471 if (mid_entry == NULL)
3472 cifs_dbg(FYI, "mid not found\n");
3474 cifs_dbg(FYI, "mid found\n");
3475 mid_entry->decrypted = true;
3476 mid_entry->resp_buf_size = server->pdu_size;
3479 if (*num_mids >= MAX_COMPOUND) {
3480 cifs_dbg(VFS, "too many PDUs in compound\n");
3483 bufs[*num_mids] = buf;
3484 mids[(*num_mids)++] = mid_entry;
3486 if (mid_entry && mid_entry->handle)
3487 ret = mid_entry->handle(server, mid_entry);
3489 ret = cifs_handle_standard(server, mid_entry);
3491 if (ret == 0 && shdr->NextCommand) {
3492 pdu_length -= le32_to_cpu(shdr->NextCommand);
3493 server->large_buf = next_is_large;
3495 server->bigbuf = next_buffer;
3497 server->smallbuf = next_buffer;
3499 buf += le32_to_cpu(shdr->NextCommand);
3507 smb3_receive_transform(struct TCP_Server_Info *server,
3508 struct mid_q_entry **mids, char **bufs, int *num_mids)
3510 char *buf = server->smallbuf;
3511 unsigned int pdu_length = server->pdu_size;
3512 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3513 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3515 if (pdu_length < sizeof(struct smb2_transform_hdr) +
3516 sizeof(struct smb2_sync_hdr)) {
3517 cifs_dbg(VFS, "Transform message is too small (%u)\n",
3519 cifs_reconnect(server);
3520 wake_up(&server->response_q);
3521 return -ECONNABORTED;
3524 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
3525 cifs_dbg(VFS, "Transform message is broken\n");
3526 cifs_reconnect(server);
3527 wake_up(&server->response_q);
3528 return -ECONNABORTED;
3531 /* TODO: add support for compounds containing READ. */
3532 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
3534 return receive_encrypted_read(server, &mids[0]);
3537 return receive_encrypted_standard(server, mids, bufs, num_mids);
3541 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
3543 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
3545 return handle_read_data(server, mid, buf, server->pdu_size,
3550 smb2_next_header(char *buf)
3552 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
3553 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
3555 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
3556 return sizeof(struct smb2_transform_hdr) +
3557 le32_to_cpu(t_hdr->OriginalMessageSize);
3559 return le32_to_cpu(hdr->NextCommand);
3562 struct smb_version_operations smb20_operations = {
3563 .compare_fids = smb2_compare_fids,
3564 .setup_request = smb2_setup_request,
3565 .setup_async_request = smb2_setup_async_request,
3566 .check_receive = smb2_check_receive,
3567 .add_credits = smb2_add_credits,
3568 .set_credits = smb2_set_credits,
3569 .get_credits_field = smb2_get_credits_field,
3570 .get_credits = smb2_get_credits,
3571 .wait_mtu_credits = cifs_wait_mtu_credits,
3572 .get_next_mid = smb2_get_next_mid,
3573 .revert_current_mid = smb2_revert_current_mid,
3574 .read_data_offset = smb2_read_data_offset,
3575 .read_data_length = smb2_read_data_length,
3576 .map_error = map_smb2_to_linux_error,
3577 .find_mid = smb2_find_mid,
3578 .check_message = smb2_check_message,
3579 .dump_detail = smb2_dump_detail,
3580 .clear_stats = smb2_clear_stats,
3581 .print_stats = smb2_print_stats,
3582 .is_oplock_break = smb2_is_valid_oplock_break,
3583 .handle_cancelled_mid = smb2_handle_cancelled_mid,
3584 .downgrade_oplock = smb2_downgrade_oplock,
3585 .need_neg = smb2_need_neg,
3586 .negotiate = smb2_negotiate,
3587 .negotiate_wsize = smb2_negotiate_wsize,
3588 .negotiate_rsize = smb2_negotiate_rsize,
3589 .sess_setup = SMB2_sess_setup,
3590 .logoff = SMB2_logoff,
3591 .tree_connect = SMB2_tcon,
3592 .tree_disconnect = SMB2_tdis,
3593 .qfs_tcon = smb2_qfs_tcon,
3594 .is_path_accessible = smb2_is_path_accessible,
3595 .can_echo = smb2_can_echo,
3597 .query_path_info = smb2_query_path_info,
3598 .get_srv_inum = smb2_get_srv_inum,
3599 .query_file_info = smb2_query_file_info,
3600 .set_path_size = smb2_set_path_size,
3601 .set_file_size = smb2_set_file_size,
3602 .set_file_info = smb2_set_file_info,
3603 .set_compression = smb2_set_compression,
3604 .mkdir = smb2_mkdir,
3605 .mkdir_setinfo = smb2_mkdir_setinfo,
3606 .rmdir = smb2_rmdir,
3607 .unlink = smb2_unlink,
3608 .rename = smb2_rename_path,
3609 .create_hardlink = smb2_create_hardlink,
3610 .query_symlink = smb2_query_symlink,
3611 .query_mf_symlink = smb3_query_mf_symlink,
3612 .create_mf_symlink = smb3_create_mf_symlink,
3613 .open = smb2_open_file,
3614 .set_fid = smb2_set_fid,
3615 .close = smb2_close_file,
3616 .flush = smb2_flush_file,
3617 .async_readv = smb2_async_readv,
3618 .async_writev = smb2_async_writev,
3619 .sync_read = smb2_sync_read,
3620 .sync_write = smb2_sync_write,
3621 .query_dir_first = smb2_query_dir_first,
3622 .query_dir_next = smb2_query_dir_next,
3623 .close_dir = smb2_close_dir,
3624 .calc_smb_size = smb2_calc_size,
3625 .is_status_pending = smb2_is_status_pending,
3626 .is_session_expired = smb2_is_session_expired,
3627 .oplock_response = smb2_oplock_response,
3628 .queryfs = smb2_queryfs,
3629 .mand_lock = smb2_mand_lock,
3630 .mand_unlock_range = smb2_unlock_range,
3631 .push_mand_locks = smb2_push_mandatory_locks,
3632 .get_lease_key = smb2_get_lease_key,
3633 .set_lease_key = smb2_set_lease_key,
3634 .new_lease_key = smb2_new_lease_key,
3635 .calc_signature = smb2_calc_signature,
3636 .is_read_op = smb2_is_read_op,
3637 .set_oplock_level = smb2_set_oplock_level,
3638 .create_lease_buf = smb2_create_lease_buf,
3639 .parse_lease_buf = smb2_parse_lease_buf,
3640 .copychunk_range = smb2_copychunk_range,
3641 .wp_retry_size = smb2_wp_retry_size,
3642 .dir_needs_close = smb2_dir_needs_close,
3643 .get_dfs_refer = smb2_get_dfs_refer,
3644 .select_sectype = smb2_select_sectype,
3645 #ifdef CONFIG_CIFS_XATTR
3646 .query_all_EAs = smb2_query_eas,
3647 .set_EA = smb2_set_ea,
3648 #endif /* CIFS_XATTR */
3649 #ifdef CONFIG_CIFS_ACL
3650 .get_acl = get_smb2_acl,
3651 .get_acl_by_fid = get_smb2_acl_by_fid,
3652 .set_acl = set_smb2_acl,
3653 #endif /* CIFS_ACL */
3654 .next_header = smb2_next_header,
3655 .ioctl_query_info = smb2_ioctl_query_info,
3658 struct smb_version_operations smb21_operations = {
3659 .compare_fids = smb2_compare_fids,
3660 .setup_request = smb2_setup_request,
3661 .setup_async_request = smb2_setup_async_request,
3662 .check_receive = smb2_check_receive,
3663 .add_credits = smb2_add_credits,
3664 .set_credits = smb2_set_credits,
3665 .get_credits_field = smb2_get_credits_field,
3666 .get_credits = smb2_get_credits,
3667 .wait_mtu_credits = smb2_wait_mtu_credits,
3668 .get_next_mid = smb2_get_next_mid,
3669 .revert_current_mid = smb2_revert_current_mid,
3670 .read_data_offset = smb2_read_data_offset,
3671 .read_data_length = smb2_read_data_length,
3672 .map_error = map_smb2_to_linux_error,
3673 .find_mid = smb2_find_mid,
3674 .check_message = smb2_check_message,
3675 .dump_detail = smb2_dump_detail,
3676 .clear_stats = smb2_clear_stats,
3677 .print_stats = smb2_print_stats,
3678 .is_oplock_break = smb2_is_valid_oplock_break,
3679 .handle_cancelled_mid = smb2_handle_cancelled_mid,
3680 .downgrade_oplock = smb21_downgrade_oplock,
3681 .need_neg = smb2_need_neg,
3682 .negotiate = smb2_negotiate,
3683 .negotiate_wsize = smb2_negotiate_wsize,
3684 .negotiate_rsize = smb2_negotiate_rsize,
3685 .sess_setup = SMB2_sess_setup,
3686 .logoff = SMB2_logoff,
3687 .tree_connect = SMB2_tcon,
3688 .tree_disconnect = SMB2_tdis,
3689 .qfs_tcon = smb2_qfs_tcon,
3690 .is_path_accessible = smb2_is_path_accessible,
3691 .can_echo = smb2_can_echo,
3693 .query_path_info = smb2_query_path_info,
3694 .get_srv_inum = smb2_get_srv_inum,
3695 .query_file_info = smb2_query_file_info,
3696 .set_path_size = smb2_set_path_size,
3697 .set_file_size = smb2_set_file_size,
3698 .set_file_info = smb2_set_file_info,
3699 .set_compression = smb2_set_compression,
3700 .mkdir = smb2_mkdir,
3701 .mkdir_setinfo = smb2_mkdir_setinfo,
3702 .rmdir = smb2_rmdir,
3703 .unlink = smb2_unlink,
3704 .rename = smb2_rename_path,
3705 .create_hardlink = smb2_create_hardlink,
3706 .query_symlink = smb2_query_symlink,
3707 .query_mf_symlink = smb3_query_mf_symlink,
3708 .create_mf_symlink = smb3_create_mf_symlink,
3709 .open = smb2_open_file,
3710 .set_fid = smb2_set_fid,
3711 .close = smb2_close_file,
3712 .flush = smb2_flush_file,
3713 .async_readv = smb2_async_readv,
3714 .async_writev = smb2_async_writev,
3715 .sync_read = smb2_sync_read,
3716 .sync_write = smb2_sync_write,
3717 .query_dir_first = smb2_query_dir_first,
3718 .query_dir_next = smb2_query_dir_next,
3719 .close_dir = smb2_close_dir,
3720 .calc_smb_size = smb2_calc_size,
3721 .is_status_pending = smb2_is_status_pending,
3722 .is_session_expired = smb2_is_session_expired,
3723 .oplock_response = smb2_oplock_response,
3724 .queryfs = smb2_queryfs,
3725 .mand_lock = smb2_mand_lock,
3726 .mand_unlock_range = smb2_unlock_range,
3727 .push_mand_locks = smb2_push_mandatory_locks,
3728 .get_lease_key = smb2_get_lease_key,
3729 .set_lease_key = smb2_set_lease_key,
3730 .new_lease_key = smb2_new_lease_key,
3731 .calc_signature = smb2_calc_signature,
3732 .is_read_op = smb21_is_read_op,
3733 .set_oplock_level = smb21_set_oplock_level,
3734 .create_lease_buf = smb2_create_lease_buf,
3735 .parse_lease_buf = smb2_parse_lease_buf,
3736 .copychunk_range = smb2_copychunk_range,
3737 .wp_retry_size = smb2_wp_retry_size,
3738 .dir_needs_close = smb2_dir_needs_close,
3739 .enum_snapshots = smb3_enum_snapshots,
3740 .get_dfs_refer = smb2_get_dfs_refer,
3741 .select_sectype = smb2_select_sectype,
3742 #ifdef CONFIG_CIFS_XATTR
3743 .query_all_EAs = smb2_query_eas,
3744 .set_EA = smb2_set_ea,
3745 #endif /* CIFS_XATTR */
3746 #ifdef CONFIG_CIFS_ACL
3747 .get_acl = get_smb2_acl,
3748 .get_acl_by_fid = get_smb2_acl_by_fid,
3749 .set_acl = set_smb2_acl,
3750 #endif /* CIFS_ACL */
3751 .next_header = smb2_next_header,
3752 .ioctl_query_info = smb2_ioctl_query_info,
3755 struct smb_version_operations smb30_operations = {
3756 .compare_fids = smb2_compare_fids,
3757 .setup_request = smb2_setup_request,
3758 .setup_async_request = smb2_setup_async_request,
3759 .check_receive = smb2_check_receive,
3760 .add_credits = smb2_add_credits,
3761 .set_credits = smb2_set_credits,
3762 .get_credits_field = smb2_get_credits_field,
3763 .get_credits = smb2_get_credits,
3764 .wait_mtu_credits = smb2_wait_mtu_credits,
3765 .get_next_mid = smb2_get_next_mid,
3766 .revert_current_mid = smb2_revert_current_mid,
3767 .read_data_offset = smb2_read_data_offset,
3768 .read_data_length = smb2_read_data_length,
3769 .map_error = map_smb2_to_linux_error,
3770 .find_mid = smb2_find_mid,
3771 .check_message = smb2_check_message,
3772 .dump_detail = smb2_dump_detail,
3773 .clear_stats = smb2_clear_stats,
3774 .print_stats = smb2_print_stats,
3775 .dump_share_caps = smb2_dump_share_caps,
3776 .is_oplock_break = smb2_is_valid_oplock_break,
3777 .handle_cancelled_mid = smb2_handle_cancelled_mid,
3778 .downgrade_oplock = smb21_downgrade_oplock,
3779 .need_neg = smb2_need_neg,
3780 .negotiate = smb2_negotiate,
3781 .negotiate_wsize = smb3_negotiate_wsize,
3782 .negotiate_rsize = smb3_negotiate_rsize,
3783 .sess_setup = SMB2_sess_setup,
3784 .logoff = SMB2_logoff,
3785 .tree_connect = SMB2_tcon,
3786 .tree_disconnect = SMB2_tdis,
3787 .qfs_tcon = smb3_qfs_tcon,
3788 .is_path_accessible = smb2_is_path_accessible,
3789 .can_echo = smb2_can_echo,
3791 .query_path_info = smb2_query_path_info,
3792 .get_srv_inum = smb2_get_srv_inum,
3793 .query_file_info = smb2_query_file_info,
3794 .set_path_size = smb2_set_path_size,
3795 .set_file_size = smb2_set_file_size,
3796 .set_file_info = smb2_set_file_info,
3797 .set_compression = smb2_set_compression,
3798 .mkdir = smb2_mkdir,
3799 .mkdir_setinfo = smb2_mkdir_setinfo,
3800 .rmdir = smb2_rmdir,
3801 .unlink = smb2_unlink,
3802 .rename = smb2_rename_path,
3803 .create_hardlink = smb2_create_hardlink,
3804 .query_symlink = smb2_query_symlink,
3805 .query_mf_symlink = smb3_query_mf_symlink,
3806 .create_mf_symlink = smb3_create_mf_symlink,
3807 .open = smb2_open_file,
3808 .set_fid = smb2_set_fid,
3809 .close = smb2_close_file,
3810 .flush = smb2_flush_file,
3811 .async_readv = smb2_async_readv,
3812 .async_writev = smb2_async_writev,
3813 .sync_read = smb2_sync_read,
3814 .sync_write = smb2_sync_write,
3815 .query_dir_first = smb2_query_dir_first,
3816 .query_dir_next = smb2_query_dir_next,
3817 .close_dir = smb2_close_dir,
3818 .calc_smb_size = smb2_calc_size,
3819 .is_status_pending = smb2_is_status_pending,
3820 .is_session_expired = smb2_is_session_expired,
3821 .oplock_response = smb2_oplock_response,
3822 .queryfs = smb2_queryfs,
3823 .mand_lock = smb2_mand_lock,
3824 .mand_unlock_range = smb2_unlock_range,
3825 .push_mand_locks = smb2_push_mandatory_locks,
3826 .get_lease_key = smb2_get_lease_key,
3827 .set_lease_key = smb2_set_lease_key,
3828 .new_lease_key = smb2_new_lease_key,
3829 .generate_signingkey = generate_smb30signingkey,
3830 .calc_signature = smb3_calc_signature,
3831 .set_integrity = smb3_set_integrity,
3832 .is_read_op = smb21_is_read_op,
3833 .set_oplock_level = smb3_set_oplock_level,
3834 .create_lease_buf = smb3_create_lease_buf,
3835 .parse_lease_buf = smb3_parse_lease_buf,
3836 .copychunk_range = smb2_copychunk_range,
3837 .duplicate_extents = smb2_duplicate_extents,
3838 .validate_negotiate = smb3_validate_negotiate,
3839 .wp_retry_size = smb2_wp_retry_size,
3840 .dir_needs_close = smb2_dir_needs_close,
3841 .fallocate = smb3_fallocate,
3842 .enum_snapshots = smb3_enum_snapshots,
3843 .init_transform_rq = smb3_init_transform_rq,
3844 .is_transform_hdr = smb3_is_transform_hdr,
3845 .receive_transform = smb3_receive_transform,
3846 .get_dfs_refer = smb2_get_dfs_refer,
3847 .select_sectype = smb2_select_sectype,
3848 #ifdef CONFIG_CIFS_XATTR
3849 .query_all_EAs = smb2_query_eas,
3850 .set_EA = smb2_set_ea,
3851 #endif /* CIFS_XATTR */
3852 #ifdef CONFIG_CIFS_ACL
3853 .get_acl = get_smb2_acl,
3854 .get_acl_by_fid = get_smb2_acl_by_fid,
3855 .set_acl = set_smb2_acl,
3856 #endif /* CIFS_ACL */
3857 .next_header = smb2_next_header,
3858 .ioctl_query_info = smb2_ioctl_query_info,
3861 struct smb_version_operations smb311_operations = {
3862 .compare_fids = smb2_compare_fids,
3863 .setup_request = smb2_setup_request,
3864 .setup_async_request = smb2_setup_async_request,
3865 .check_receive = smb2_check_receive,
3866 .add_credits = smb2_add_credits,
3867 .set_credits = smb2_set_credits,
3868 .get_credits_field = smb2_get_credits_field,
3869 .get_credits = smb2_get_credits,
3870 .wait_mtu_credits = smb2_wait_mtu_credits,
3871 .get_next_mid = smb2_get_next_mid,
3872 .revert_current_mid = smb2_revert_current_mid,
3873 .read_data_offset = smb2_read_data_offset,
3874 .read_data_length = smb2_read_data_length,
3875 .map_error = map_smb2_to_linux_error,
3876 .find_mid = smb2_find_mid,
3877 .check_message = smb2_check_message,
3878 .dump_detail = smb2_dump_detail,
3879 .clear_stats = smb2_clear_stats,
3880 .print_stats = smb2_print_stats,
3881 .dump_share_caps = smb2_dump_share_caps,
3882 .is_oplock_break = smb2_is_valid_oplock_break,
3883 .handle_cancelled_mid = smb2_handle_cancelled_mid,
3884 .downgrade_oplock = smb21_downgrade_oplock,
3885 .need_neg = smb2_need_neg,
3886 .negotiate = smb2_negotiate,
3887 .negotiate_wsize = smb3_negotiate_wsize,
3888 .negotiate_rsize = smb3_negotiate_rsize,
3889 .sess_setup = SMB2_sess_setup,
3890 .logoff = SMB2_logoff,
3891 .tree_connect = SMB2_tcon,
3892 .tree_disconnect = SMB2_tdis,
3893 .qfs_tcon = smb3_qfs_tcon,
3894 .is_path_accessible = smb2_is_path_accessible,
3895 .can_echo = smb2_can_echo,
3897 .query_path_info = smb2_query_path_info,
3898 .get_srv_inum = smb2_get_srv_inum,
3899 .query_file_info = smb2_query_file_info,
3900 .set_path_size = smb2_set_path_size,
3901 .set_file_size = smb2_set_file_size,
3902 .set_file_info = smb2_set_file_info,
3903 .set_compression = smb2_set_compression,
3904 .mkdir = smb2_mkdir,
3905 .mkdir_setinfo = smb2_mkdir_setinfo,
3906 .posix_mkdir = smb311_posix_mkdir,
3907 .rmdir = smb2_rmdir,
3908 .unlink = smb2_unlink,
3909 .rename = smb2_rename_path,
3910 .create_hardlink = smb2_create_hardlink,
3911 .query_symlink = smb2_query_symlink,
3912 .query_mf_symlink = smb3_query_mf_symlink,
3913 .create_mf_symlink = smb3_create_mf_symlink,
3914 .open = smb2_open_file,
3915 .set_fid = smb2_set_fid,
3916 .close = smb2_close_file,
3917 .flush = smb2_flush_file,
3918 .async_readv = smb2_async_readv,
3919 .async_writev = smb2_async_writev,
3920 .sync_read = smb2_sync_read,
3921 .sync_write = smb2_sync_write,
3922 .query_dir_first = smb2_query_dir_first,
3923 .query_dir_next = smb2_query_dir_next,
3924 .close_dir = smb2_close_dir,
3925 .calc_smb_size = smb2_calc_size,
3926 .is_status_pending = smb2_is_status_pending,
3927 .is_session_expired = smb2_is_session_expired,
3928 .oplock_response = smb2_oplock_response,
3929 .queryfs = smb311_queryfs,
3930 .mand_lock = smb2_mand_lock,
3931 .mand_unlock_range = smb2_unlock_range,
3932 .push_mand_locks = smb2_push_mandatory_locks,
3933 .get_lease_key = smb2_get_lease_key,
3934 .set_lease_key = smb2_set_lease_key,
3935 .new_lease_key = smb2_new_lease_key,
3936 .generate_signingkey = generate_smb311signingkey,
3937 .calc_signature = smb3_calc_signature,
3938 .set_integrity = smb3_set_integrity,
3939 .is_read_op = smb21_is_read_op,
3940 .set_oplock_level = smb3_set_oplock_level,
3941 .create_lease_buf = smb3_create_lease_buf,
3942 .parse_lease_buf = smb3_parse_lease_buf,
3943 .copychunk_range = smb2_copychunk_range,
3944 .duplicate_extents = smb2_duplicate_extents,
3945 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
3946 .wp_retry_size = smb2_wp_retry_size,
3947 .dir_needs_close = smb2_dir_needs_close,
3948 .fallocate = smb3_fallocate,
3949 .enum_snapshots = smb3_enum_snapshots,
3950 .init_transform_rq = smb3_init_transform_rq,
3951 .is_transform_hdr = smb3_is_transform_hdr,
3952 .receive_transform = smb3_receive_transform,
3953 .get_dfs_refer = smb2_get_dfs_refer,
3954 .select_sectype = smb2_select_sectype,
3955 #ifdef CONFIG_CIFS_XATTR
3956 .query_all_EAs = smb2_query_eas,
3957 .set_EA = smb2_set_ea,
3958 #endif /* CIFS_XATTR */
3959 #ifdef CONFIG_CIFS_ACL
3960 .get_acl = get_smb2_acl,
3961 .get_acl_by_fid = get_smb2_acl_by_fid,
3962 .set_acl = set_smb2_acl,
3963 #endif /* CIFS_ACL */
3964 .next_header = smb2_next_header,
3965 .ioctl_query_info = smb2_ioctl_query_info,
3968 struct smb_version_values smb20_values = {
3969 .version_string = SMB20_VERSION_STRING,
3970 .protocol_id = SMB20_PROT_ID,
3971 .req_capabilities = 0, /* MBZ */
3972 .large_lock_type = 0,
3973 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3974 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3975 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3976 .header_size = sizeof(struct smb2_sync_hdr),
3977 .header_preamble_size = 0,
3978 .max_header_size = MAX_SMB2_HDR_SIZE,
3979 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3980 .lock_cmd = SMB2_LOCK,
3982 .cap_nt_find = SMB2_NT_FIND,
3983 .cap_large_files = SMB2_LARGE_FILES,
3984 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3985 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3986 .create_lease_size = sizeof(struct create_lease),
3989 struct smb_version_values smb21_values = {
3990 .version_string = SMB21_VERSION_STRING,
3991 .protocol_id = SMB21_PROT_ID,
3992 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
3993 .large_lock_type = 0,
3994 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3995 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3996 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3997 .header_size = sizeof(struct smb2_sync_hdr),
3998 .header_preamble_size = 0,
3999 .max_header_size = MAX_SMB2_HDR_SIZE,
4000 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4001 .lock_cmd = SMB2_LOCK,
4003 .cap_nt_find = SMB2_NT_FIND,
4004 .cap_large_files = SMB2_LARGE_FILES,
4005 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4006 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4007 .create_lease_size = sizeof(struct create_lease),
4010 struct smb_version_values smb3any_values = {
4011 .version_string = SMB3ANY_VERSION_STRING,
4012 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
4013 .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,
4014 .large_lock_type = 0,
4015 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4016 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4017 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4018 .header_size = sizeof(struct smb2_sync_hdr),
4019 .header_preamble_size = 0,
4020 .max_header_size = MAX_SMB2_HDR_SIZE,
4021 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4022 .lock_cmd = SMB2_LOCK,
4024 .cap_nt_find = SMB2_NT_FIND,
4025 .cap_large_files = SMB2_LARGE_FILES,
4026 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4027 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4028 .create_lease_size = sizeof(struct create_lease_v2),
4031 struct smb_version_values smbdefault_values = {
4032 .version_string = SMBDEFAULT_VERSION_STRING,
4033 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
4034 .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,
4035 .large_lock_type = 0,
4036 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4037 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4038 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4039 .header_size = sizeof(struct smb2_sync_hdr),
4040 .header_preamble_size = 0,
4041 .max_header_size = MAX_SMB2_HDR_SIZE,
4042 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4043 .lock_cmd = SMB2_LOCK,
4045 .cap_nt_find = SMB2_NT_FIND,
4046 .cap_large_files = SMB2_LARGE_FILES,
4047 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4048 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4049 .create_lease_size = sizeof(struct create_lease_v2),
4052 struct smb_version_values smb30_values = {
4053 .version_string = SMB30_VERSION_STRING,
4054 .protocol_id = SMB30_PROT_ID,
4055 .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,
4056 .large_lock_type = 0,
4057 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4058 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4059 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4060 .header_size = sizeof(struct smb2_sync_hdr),
4061 .header_preamble_size = 0,
4062 .max_header_size = MAX_SMB2_HDR_SIZE,
4063 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4064 .lock_cmd = SMB2_LOCK,
4066 .cap_nt_find = SMB2_NT_FIND,
4067 .cap_large_files = SMB2_LARGE_FILES,
4068 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4069 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4070 .create_lease_size = sizeof(struct create_lease_v2),
4073 struct smb_version_values smb302_values = {
4074 .version_string = SMB302_VERSION_STRING,
4075 .protocol_id = SMB302_PROT_ID,
4076 .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,
4077 .large_lock_type = 0,
4078 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4079 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4080 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4081 .header_size = sizeof(struct smb2_sync_hdr),
4082 .header_preamble_size = 0,
4083 .max_header_size = MAX_SMB2_HDR_SIZE,
4084 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4085 .lock_cmd = SMB2_LOCK,
4087 .cap_nt_find = SMB2_NT_FIND,
4088 .cap_large_files = SMB2_LARGE_FILES,
4089 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4090 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4091 .create_lease_size = sizeof(struct create_lease_v2),
4094 struct smb_version_values smb311_values = {
4095 .version_string = SMB311_VERSION_STRING,
4096 .protocol_id = SMB311_PROT_ID,
4097 .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,
4098 .large_lock_type = 0,
4099 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4100 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4101 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4102 .header_size = sizeof(struct smb2_sync_hdr),
4103 .header_preamble_size = 0,
4104 .max_header_size = MAX_SMB2_HDR_SIZE,
4105 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4106 .lock_cmd = SMB2_LOCK,
4108 .cap_nt_find = SMB2_NT_FIND,
4109 .cap_large_files = SMB2_LARGE_FILES,
4110 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4111 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4112 .create_lease_size = sizeof(struct create_lease_v2),