1 // SPDX-License-Identifier: GPL-2.0
3 * SMB2 version specific operations
8 #include <linux/pagemap.h>
10 #include <linux/falloc.h>
11 #include <linux/scatterlist.h>
12 #include <linux/uuid.h>
13 #include <crypto/aead.h>
16 #include "smb2proto.h"
17 #include "cifsproto.h"
18 #include "cifs_debug.h"
19 #include "cifs_unicode.h"
20 #include "smb2status.h"
22 #include "cifs_ioctl.h"
23 #include "smbdirect.h"
25 /* Change credits for different ops and return the total number of credits */
27 change_conf(struct TCP_Server_Info *server)
29 server->credits += server->echo_credits + server->oplock_credits;
30 server->oplock_credits = server->echo_credits = 0;
31 switch (server->credits) {
35 server->echoes = false;
36 server->oplocks = false;
39 server->echoes = true;
40 server->oplocks = false;
41 server->echo_credits = 1;
44 server->echoes = true;
46 server->oplocks = true;
47 server->oplock_credits = 1;
49 server->oplocks = false;
51 server->echo_credits = 1;
53 server->credits -= server->echo_credits + server->oplock_credits;
54 return server->credits + server->echo_credits + server->oplock_credits;
58 smb2_add_credits(struct TCP_Server_Info *server,
59 const struct cifs_credits *credits, const int optype)
62 unsigned int add = credits->value;
63 unsigned int instance = credits->instance;
64 bool reconnect_detected = false;
66 spin_lock(&server->req_lock);
67 val = server->ops->get_credits_field(server, optype);
69 /* eg found case where write overlapping reconnect messed up credits */
70 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
71 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
72 server->hostname, *val);
73 if ((instance == 0) || (instance == server->reconnect_instance))
76 reconnect_detected = true;
79 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
80 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
83 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
84 rc = change_conf(server);
86 * Sometimes server returns 0 credits on oplock break ack - we need to
87 * rebalance credits in this case.
89 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
91 if (server->credits > 1) {
93 server->oplock_credits++;
96 spin_unlock(&server->req_lock);
97 wake_up(&server->request_q);
99 if (reconnect_detected)
100 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
103 if (server->tcpStatus == CifsNeedReconnect
104 || server->tcpStatus == CifsExiting)
109 /* change_conf hasn't been executed */
112 cifs_dbg(VFS, "Possible client or server bug - zero credits\n");
115 cifs_dbg(VFS, "disabling echoes and oplocks\n");
118 cifs_dbg(FYI, "disabling oplocks\n");
121 cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
126 smb2_set_credits(struct TCP_Server_Info *server, const int val)
128 spin_lock(&server->req_lock);
129 server->credits = val;
131 server->reconnect_instance++;
132 spin_unlock(&server->req_lock);
133 /* don't log while holding the lock */
135 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
139 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
143 return &server->echo_credits;
145 return &server->oplock_credits;
147 return &server->credits;
152 smb2_get_credits(struct mid_q_entry *mid)
154 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)mid->resp_buf;
156 if (mid->mid_state == MID_RESPONSE_RECEIVED
157 || mid->mid_state == MID_RESPONSE_MALFORMED)
158 return le16_to_cpu(shdr->CreditRequest);
164 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
165 unsigned int *num, struct cifs_credits *credits)
168 unsigned int scredits;
170 spin_lock(&server->req_lock);
172 if (server->credits <= 0) {
173 spin_unlock(&server->req_lock);
174 cifs_num_waiters_inc(server);
175 rc = wait_event_killable(server->request_q,
176 has_credits(server, &server->credits, 1));
177 cifs_num_waiters_dec(server);
180 spin_lock(&server->req_lock);
182 if (server->tcpStatus == CifsExiting) {
183 spin_unlock(&server->req_lock);
187 scredits = server->credits;
188 /* can deadlock with reopen */
190 *num = SMB2_MAX_BUFFER_SIZE;
192 credits->instance = 0;
196 /* leave some credits for reopen and other ops */
198 *num = min_t(unsigned int, size,
199 scredits * SMB2_MAX_BUFFER_SIZE);
202 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
203 credits->instance = server->reconnect_instance;
204 server->credits -= credits->value;
209 spin_unlock(&server->req_lock);
214 smb2_adjust_credits(struct TCP_Server_Info *server,
215 struct cifs_credits *credits,
216 const unsigned int payload_size)
218 int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
220 if (!credits->value || credits->value == new_val)
223 if (credits->value < new_val) {
224 WARN_ONCE(1, "request has less credits (%d) than required (%d)",
225 credits->value, new_val);
229 spin_lock(&server->req_lock);
231 if (server->reconnect_instance != credits->instance) {
232 spin_unlock(&server->req_lock);
233 cifs_dbg(VFS, "trying to return %d credits to old session\n",
234 credits->value - new_val);
238 server->credits += credits->value - new_val;
239 spin_unlock(&server->req_lock);
240 wake_up(&server->request_q);
241 credits->value = new_val;
246 smb2_get_next_mid(struct TCP_Server_Info *server)
249 /* for SMB2 we need the current value */
250 spin_lock(&GlobalMid_Lock);
251 mid = server->CurrentMid++;
252 spin_unlock(&GlobalMid_Lock);
257 smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
259 spin_lock(&GlobalMid_Lock);
260 if (server->CurrentMid >= val)
261 server->CurrentMid -= val;
262 spin_unlock(&GlobalMid_Lock);
265 static struct mid_q_entry *
266 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
268 struct mid_q_entry *mid;
269 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
270 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
272 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
273 cifs_dbg(VFS, "Encrypted frame parsing not supported yet\n");
277 spin_lock(&GlobalMid_Lock);
278 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
279 if ((mid->mid == wire_mid) &&
280 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
281 (mid->command == shdr->Command)) {
282 kref_get(&mid->refcount);
283 spin_unlock(&GlobalMid_Lock);
287 spin_unlock(&GlobalMid_Lock);
292 smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
294 #ifdef CONFIG_CIFS_DEBUG2
295 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
297 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
298 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
300 cifs_dbg(VFS, "smb buf %p len %u\n", buf,
301 server->ops->calc_smb_size(buf, server));
306 smb2_need_neg(struct TCP_Server_Info *server)
308 return server->max_read == 0;
312 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
316 ses->server->CurrentMid = 0;
317 rc = SMB2_negotiate(xid, ses);
318 /* BB we probably don't need to retry with modern servers */
325 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
327 struct TCP_Server_Info *server = tcon->ses->server;
330 /* start with specified wsize, or default */
331 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
332 wsize = min_t(unsigned int, wsize, server->max_write);
333 #ifdef CONFIG_CIFS_SMB_DIRECT
336 wsize = min_t(unsigned int,
337 wsize, server->smbd_conn->max_fragmented_send_size);
339 wsize = min_t(unsigned int,
340 wsize, server->smbd_conn->max_readwrite_size);
343 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
344 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
350 smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
352 struct TCP_Server_Info *server = tcon->ses->server;
355 /* start with specified wsize, or default */
356 wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
357 wsize = min_t(unsigned int, wsize, server->max_write);
358 #ifdef CONFIG_CIFS_SMB_DIRECT
361 wsize = min_t(unsigned int,
362 wsize, server->smbd_conn->max_fragmented_send_size);
364 wsize = min_t(unsigned int,
365 wsize, server->smbd_conn->max_readwrite_size);
368 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
369 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
375 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
377 struct TCP_Server_Info *server = tcon->ses->server;
380 /* start with specified rsize, or default */
381 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
382 rsize = min_t(unsigned int, rsize, server->max_read);
383 #ifdef CONFIG_CIFS_SMB_DIRECT
386 rsize = min_t(unsigned int,
387 rsize, server->smbd_conn->max_fragmented_recv_size);
389 rsize = min_t(unsigned int,
390 rsize, server->smbd_conn->max_readwrite_size);
394 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
395 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
401 smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
403 struct TCP_Server_Info *server = tcon->ses->server;
406 /* start with specified rsize, or default */
407 rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
408 rsize = min_t(unsigned int, rsize, server->max_read);
409 #ifdef CONFIG_CIFS_SMB_DIRECT
412 rsize = min_t(unsigned int,
413 rsize, server->smbd_conn->max_fragmented_recv_size);
415 rsize = min_t(unsigned int,
416 rsize, server->smbd_conn->max_readwrite_size);
420 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
421 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
427 parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
429 struct cifs_server_iface **iface_list,
432 struct network_interface_info_ioctl_rsp *p;
433 struct sockaddr_in *addr4;
434 struct sockaddr_in6 *addr6;
435 struct iface_info_ipv4 *p4;
436 struct iface_info_ipv6 *p6;
437 struct cifs_server_iface *info;
447 * Fist pass: count and sanity check
450 bytes_left = buf_len;
452 while (bytes_left >= sizeof(*p)) {
454 next = le32_to_cpu(p->Next);
456 bytes_left -= sizeof(*p);
459 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
464 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
469 if (bytes_left || p->Next)
470 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
474 * Second pass: extract info to internal structure
477 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
484 bytes_left = buf_len;
486 while (bytes_left >= sizeof(*p)) {
487 info->speed = le64_to_cpu(p->LinkSpeed);
488 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE);
489 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE);
491 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
492 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
493 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
494 le32_to_cpu(p->Capability));
498 * The kernel and wire socket structures have the same
499 * layout and use network byte order but make the
500 * conversion explicit in case either one changes.
503 addr4 = (struct sockaddr_in *)&info->sockaddr;
504 p4 = (struct iface_info_ipv4 *)p->Buffer;
505 addr4->sin_family = AF_INET;
506 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
508 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
509 addr4->sin_port = cpu_to_be16(CIFS_PORT);
511 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
515 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
516 p6 = (struct iface_info_ipv6 *)p->Buffer;
517 addr6->sin6_family = AF_INET6;
518 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
520 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
521 addr6->sin6_flowinfo = 0;
522 addr6->sin6_scope_id = 0;
523 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
525 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
530 "%s: skipping unsupported socket family\n",
538 next = le32_to_cpu(p->Next);
541 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
561 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
564 unsigned int ret_data_len = 0;
565 struct network_interface_info_ioctl_rsp *out_buf = NULL;
566 struct cifs_server_iface *iface_list;
568 struct cifs_ses *ses = tcon->ses;
570 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
571 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
572 NULL /* no data input */, 0 /* no data input */,
573 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
574 if (rc == -EOPNOTSUPP) {
576 "server does not support query network interfaces\n");
578 } else if (rc != 0) {
579 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
583 rc = parse_server_interfaces(out_buf, ret_data_len,
584 &iface_list, &iface_count);
588 spin_lock(&ses->iface_lock);
589 kfree(ses->iface_list);
590 ses->iface_list = iface_list;
591 ses->iface_count = iface_count;
592 ses->iface_last_update = jiffies;
593 spin_unlock(&ses->iface_lock);
601 smb2_close_cached_fid(struct kref *ref)
603 struct cached_fid *cfid = container_of(ref, struct cached_fid,
606 if (cfid->is_valid) {
607 cifs_dbg(FYI, "clear cached root file handle\n");
608 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
609 cfid->fid->volatile_fid);
610 cfid->is_valid = false;
611 cfid->file_all_info_is_valid = false;
615 void close_shroot(struct cached_fid *cfid)
617 mutex_lock(&cfid->fid_mutex);
618 kref_put(&cfid->refcount, smb2_close_cached_fid);
619 mutex_unlock(&cfid->fid_mutex);
623 smb2_cached_lease_break(struct work_struct *work)
625 struct cached_fid *cfid = container_of(work,
626 struct cached_fid, lease_break);
632 * Open the directory at the root of a share
634 int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
636 struct cifs_ses *ses = tcon->ses;
637 struct TCP_Server_Info *server = ses->server;
638 struct cifs_open_parms oparms;
639 struct smb2_create_rsp *o_rsp = NULL;
640 struct smb2_query_info_rsp *qi_rsp = NULL;
642 struct smb_rqst rqst[2];
643 struct kvec rsp_iov[2];
644 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
645 struct kvec qi_iov[1];
647 __le16 utf16_path = 0; /* Null - since an open of top of share */
648 u8 oplock = SMB2_OPLOCK_LEVEL_II;
650 mutex_lock(&tcon->crfid.fid_mutex);
651 if (tcon->crfid.is_valid) {
652 cifs_dbg(FYI, "found a cached root file handle\n");
653 memcpy(pfid, tcon->crfid.fid, sizeof(struct cifs_fid));
654 kref_get(&tcon->crfid.refcount);
655 mutex_unlock(&tcon->crfid.fid_mutex);
659 if (smb3_encryption_required(tcon))
660 flags |= CIFS_TRANSFORM_REQ;
662 memset(rqst, 0, sizeof(rqst));
663 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
664 memset(rsp_iov, 0, sizeof(rsp_iov));
667 memset(&open_iov, 0, sizeof(open_iov));
668 rqst[0].rq_iov = open_iov;
669 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
672 oparms.create_options = 0;
673 oparms.desired_access = FILE_READ_ATTRIBUTES;
674 oparms.disposition = FILE_OPEN;
676 oparms.reconnect = false;
678 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, &utf16_path);
681 smb2_set_next_command(tcon, &rqst[0]);
683 memset(&qi_iov, 0, sizeof(qi_iov));
684 rqst[1].rq_iov = qi_iov;
687 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
688 COMPOUND_FID, FILE_ALL_INFORMATION,
690 sizeof(struct smb2_file_all_info) +
691 PATH_MAX * 2, 0, NULL);
695 smb2_set_related(&rqst[1]);
697 rc = compound_send_recv(xid, ses, flags, 2, rqst,
698 resp_buftype, rsp_iov);
702 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
703 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
704 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
705 #ifdef CONFIG_CIFS_DEBUG2
706 oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
707 #endif /* CIFS_DEBUG2 */
709 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
710 tcon->crfid.tcon = tcon;
711 tcon->crfid.is_valid = true;
712 kref_init(&tcon->crfid.refcount);
714 if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
715 kref_get(&tcon->crfid.refcount);
716 oplock = smb2_parse_lease_state(server, o_rsp,
718 oparms.fid->lease_key);
722 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
723 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
725 if (!smb2_validate_and_copy_iov(
726 le16_to_cpu(qi_rsp->OutputBufferOffset),
727 sizeof(struct smb2_file_all_info),
728 &rsp_iov[1], sizeof(struct smb2_file_all_info),
729 (char *)&tcon->crfid.file_all_info))
730 tcon->crfid.file_all_info_is_valid = 1;
733 mutex_unlock(&tcon->crfid.fid_mutex);
734 SMB2_open_free(&rqst[0]);
735 SMB2_query_info_free(&rqst[1]);
736 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
737 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
742 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
745 __le16 srch_path = 0; /* Null - open root of share */
746 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
747 struct cifs_open_parms oparms;
749 bool no_cached_open = tcon->nohandlecache;
752 oparms.desired_access = FILE_READ_ATTRIBUTES;
753 oparms.disposition = FILE_OPEN;
754 oparms.create_options = 0;
756 oparms.reconnect = false;
759 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
762 rc = open_shroot(xid, tcon, &fid);
767 SMB3_request_interfaces(xid, tcon);
769 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
770 FS_ATTRIBUTE_INFORMATION);
771 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
772 FS_DEVICE_INFORMATION);
773 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
774 FS_VOLUME_INFORMATION);
775 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
776 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
778 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
780 close_shroot(&tcon->crfid);
784 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
787 __le16 srch_path = 0; /* Null - open root of share */
788 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
789 struct cifs_open_parms oparms;
793 oparms.desired_access = FILE_READ_ATTRIBUTES;
794 oparms.disposition = FILE_OPEN;
795 oparms.create_options = 0;
797 oparms.reconnect = false;
799 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
803 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
804 FS_ATTRIBUTE_INFORMATION);
805 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
806 FS_DEVICE_INFORMATION);
807 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
811 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
812 struct cifs_sb_info *cifs_sb, const char *full_path)
816 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
817 struct cifs_open_parms oparms;
820 if ((*full_path == 0) && tcon->crfid.is_valid)
823 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
828 oparms.desired_access = FILE_READ_ATTRIBUTES;
829 oparms.disposition = FILE_OPEN;
830 if (backup_cred(cifs_sb))
831 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
833 oparms.create_options = 0;
835 oparms.reconnect = false;
837 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
843 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
849 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
850 struct cifs_sb_info *cifs_sb, const char *full_path,
851 u64 *uniqueid, FILE_ALL_INFO *data)
853 *uniqueid = le64_to_cpu(data->IndexNumber);
858 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
859 struct cifs_fid *fid, FILE_ALL_INFO *data)
862 struct smb2_file_all_info *smb2_data;
864 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
866 if (smb2_data == NULL)
869 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
872 move_smb2_info_to_cifs(data, smb2_data);
877 #ifdef CONFIG_CIFS_XATTR
879 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
880 struct smb2_file_full_ea_info *src, size_t src_size,
881 const unsigned char *ea_name)
884 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
886 size_t buf_size = dst_size;
887 size_t name_len, value_len, user_name_len;
889 while (src_size > 0) {
890 name = &src->ea_data[0];
891 name_len = (size_t)src->ea_name_length;
892 value = &src->ea_data[src->ea_name_length + 1];
893 value_len = (size_t)le16_to_cpu(src->ea_value_length);
898 if (src_size < 8 + name_len + 1 + value_len) {
899 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
905 if (ea_name_len == name_len &&
906 memcmp(ea_name, name, name_len) == 0) {
910 if (dst_size < value_len) {
914 memcpy(dst, value, value_len);
918 /* 'user.' plus a terminating null */
919 user_name_len = 5 + 1 + name_len;
922 /* skip copy - calc size only */
924 } else if (dst_size >= user_name_len) {
925 dst_size -= user_name_len;
926 memcpy(dst, "user.", 5);
928 memcpy(dst, src->ea_data, name_len);
934 /* stop before overrun buffer */
940 if (!src->next_entry_offset)
943 if (src_size < le32_to_cpu(src->next_entry_offset)) {
944 /* stop before overrun buffer */
948 src_size -= le32_to_cpu(src->next_entry_offset);
949 src = (void *)((char *)src +
950 le32_to_cpu(src->next_entry_offset));
953 /* didn't find the named attribute */
962 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
963 const unsigned char *path, const unsigned char *ea_name,
964 char *ea_data, size_t buf_size,
965 struct cifs_sb_info *cifs_sb)
969 struct kvec rsp_iov = {NULL, 0};
970 int buftype = CIFS_NO_BUFFER;
971 struct smb2_query_info_rsp *rsp;
972 struct smb2_file_full_ea_info *info = NULL;
974 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
978 rc = smb2_query_info_compound(xid, tcon, utf16_path,
980 FILE_FULL_EA_INFORMATION,
983 MAX_SMB2_CREATE_RESPONSE_SIZE -
984 MAX_SMB2_CLOSE_RESPONSE_SIZE,
985 &rsp_iov, &buftype, cifs_sb);
988 * If ea_name is NULL (listxattr) and there are no EAs,
989 * return 0 as it's not an error. Otherwise, the specified
990 * ea_name was not found.
992 if (!ea_name && rc == -ENODATA)
997 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
998 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
999 le32_to_cpu(rsp->OutputBufferLength),
1001 sizeof(struct smb2_file_full_ea_info));
1005 info = (struct smb2_file_full_ea_info *)(
1006 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1007 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1008 le32_to_cpu(rsp->OutputBufferLength), ea_name);
1012 free_rsp_buf(buftype, rsp_iov.iov_base);
1018 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1019 const char *path, const char *ea_name, const void *ea_value,
1020 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1021 struct cifs_sb_info *cifs_sb)
1023 struct cifs_ses *ses = tcon->ses;
1024 __le16 *utf16_path = NULL;
1025 int ea_name_len = strlen(ea_name);
1028 struct smb_rqst rqst[3];
1029 int resp_buftype[3];
1030 struct kvec rsp_iov[3];
1031 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1032 struct cifs_open_parms oparms;
1033 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1034 struct cifs_fid fid;
1035 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1036 unsigned int size[1];
1038 struct smb2_file_full_ea_info *ea = NULL;
1039 struct kvec close_iov[1];
1042 if (smb3_encryption_required(tcon))
1043 flags |= CIFS_TRANSFORM_REQ;
1045 if (ea_name_len > 255)
1048 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1052 memset(rqst, 0, sizeof(rqst));
1053 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1054 memset(rsp_iov, 0, sizeof(rsp_iov));
1056 if (ses->server->ops->query_all_EAs) {
1058 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1067 memset(&open_iov, 0, sizeof(open_iov));
1068 rqst[0].rq_iov = open_iov;
1069 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1071 memset(&oparms, 0, sizeof(oparms));
1073 oparms.desired_access = FILE_WRITE_EA;
1074 oparms.disposition = FILE_OPEN;
1075 if (backup_cred(cifs_sb))
1076 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1078 oparms.create_options = 0;
1080 oparms.reconnect = false;
1082 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
1085 smb2_set_next_command(tcon, &rqst[0]);
1089 memset(&si_iov, 0, sizeof(si_iov));
1090 rqst[1].rq_iov = si_iov;
1091 rqst[1].rq_nvec = 1;
1093 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
1094 ea = kzalloc(len, GFP_KERNEL);
1100 ea->ea_name_length = ea_name_len;
1101 ea->ea_value_length = cpu_to_le16(ea_value_len);
1102 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1103 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1108 rc = SMB2_set_info_init(tcon, &rqst[1], COMPOUND_FID,
1109 COMPOUND_FID, current->tgid,
1110 FILE_FULL_EA_INFORMATION,
1111 SMB2_O_INFO_FILE, 0, data, size);
1112 smb2_set_next_command(tcon, &rqst[1]);
1113 smb2_set_related(&rqst[1]);
1117 memset(&close_iov, 0, sizeof(close_iov));
1118 rqst[2].rq_iov = close_iov;
1119 rqst[2].rq_nvec = 1;
1120 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1121 smb2_set_related(&rqst[2]);
1123 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1124 resp_buftype, rsp_iov);
1129 SMB2_open_free(&rqst[0]);
1130 SMB2_set_info_free(&rqst[1]);
1131 SMB2_close_free(&rqst[2]);
1132 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1133 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1134 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1140 smb2_can_echo(struct TCP_Server_Info *server)
1142 return server->echoes;
1146 smb2_clear_stats(struct cifs_tcon *tcon)
1150 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1151 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1152 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1157 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1159 seq_puts(m, "\n\tShare Capabilities:");
1160 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1161 seq_puts(m, " DFS,");
1162 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1163 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1164 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1165 seq_puts(m, " SCALEOUT,");
1166 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1167 seq_puts(m, " CLUSTER,");
1168 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1169 seq_puts(m, " ASYMMETRIC,");
1170 if (tcon->capabilities == 0)
1171 seq_puts(m, " None");
1172 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1173 seq_puts(m, " Aligned,");
1174 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1175 seq_puts(m, " Partition Aligned,");
1176 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1177 seq_puts(m, " SSD,");
1178 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1179 seq_puts(m, " TRIM-support,");
1181 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1182 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1183 if (tcon->perf_sector_size)
1184 seq_printf(m, "\tOptimal sector size: 0x%x",
1185 tcon->perf_sector_size);
1186 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1190 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1192 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1193 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1196 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1197 * totals (requests sent) since those SMBs are per-session not per tcon
1199 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1200 (long long)(tcon->bytes_read),
1201 (long long)(tcon->bytes_written));
1202 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1203 atomic_read(&tcon->num_local_opens),
1204 atomic_read(&tcon->num_remote_opens));
1205 seq_printf(m, "\nTreeConnects: %d total %d failed",
1206 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1207 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1208 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1209 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1210 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1211 seq_printf(m, "\nCreates: %d total %d failed",
1212 atomic_read(&sent[SMB2_CREATE_HE]),
1213 atomic_read(&failed[SMB2_CREATE_HE]));
1214 seq_printf(m, "\nCloses: %d total %d failed",
1215 atomic_read(&sent[SMB2_CLOSE_HE]),
1216 atomic_read(&failed[SMB2_CLOSE_HE]));
1217 seq_printf(m, "\nFlushes: %d total %d failed",
1218 atomic_read(&sent[SMB2_FLUSH_HE]),
1219 atomic_read(&failed[SMB2_FLUSH_HE]));
1220 seq_printf(m, "\nReads: %d total %d failed",
1221 atomic_read(&sent[SMB2_READ_HE]),
1222 atomic_read(&failed[SMB2_READ_HE]));
1223 seq_printf(m, "\nWrites: %d total %d failed",
1224 atomic_read(&sent[SMB2_WRITE_HE]),
1225 atomic_read(&failed[SMB2_WRITE_HE]));
1226 seq_printf(m, "\nLocks: %d total %d failed",
1227 atomic_read(&sent[SMB2_LOCK_HE]),
1228 atomic_read(&failed[SMB2_LOCK_HE]));
1229 seq_printf(m, "\nIOCTLs: %d total %d failed",
1230 atomic_read(&sent[SMB2_IOCTL_HE]),
1231 atomic_read(&failed[SMB2_IOCTL_HE]));
1232 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1233 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1234 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1235 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1236 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1237 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1238 seq_printf(m, "\nQueryInfos: %d total %d failed",
1239 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1240 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1241 seq_printf(m, "\nSetInfos: %d total %d failed",
1242 atomic_read(&sent[SMB2_SET_INFO_HE]),
1243 atomic_read(&failed[SMB2_SET_INFO_HE]));
1244 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1245 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1246 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1250 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1252 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1253 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1255 cfile->fid.persistent_fid = fid->persistent_fid;
1256 cfile->fid.volatile_fid = fid->volatile_fid;
1257 #ifdef CONFIG_CIFS_DEBUG2
1258 cfile->fid.mid = fid->mid;
1259 #endif /* CIFS_DEBUG2 */
1260 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1262 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1263 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1267 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1268 struct cifs_fid *fid)
1270 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1274 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1275 u64 persistent_fid, u64 volatile_fid,
1276 struct copychunk_ioctl *pcchunk)
1279 unsigned int ret_data_len;
1280 struct resume_key_req *res_key;
1282 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1283 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1284 NULL, 0 /* no input */, CIFSMaxBufSize,
1285 (char **)&res_key, &ret_data_len);
1288 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1289 goto req_res_key_exit;
1291 if (ret_data_len < sizeof(struct resume_key_req)) {
1292 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
1294 goto req_res_key_exit;
1296 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1304 smb2_ioctl_query_info(const unsigned int xid,
1305 struct cifs_tcon *tcon,
1306 __le16 *path, int is_dir,
1309 struct cifs_ses *ses = tcon->ses;
1310 char __user *arg = (char __user *)p;
1311 struct smb_query_info qi;
1312 struct smb_query_info __user *pqi;
1315 struct smb2_query_info_rsp *qi_rsp = NULL;
1316 struct smb2_ioctl_rsp *io_rsp = NULL;
1317 void *buffer = NULL;
1318 struct smb_rqst rqst[3];
1319 int resp_buftype[3];
1320 struct kvec rsp_iov[3];
1321 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1322 struct cifs_open_parms oparms;
1323 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1324 struct cifs_fid fid;
1325 struct kvec qi_iov[1];
1326 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1327 struct kvec close_iov[1];
1329 memset(rqst, 0, sizeof(rqst));
1330 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1331 memset(rsp_iov, 0, sizeof(rsp_iov));
1333 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1336 if (qi.output_buffer_length > 1024)
1339 if (!ses || !(ses->server))
1342 if (smb3_encryption_required(tcon))
1343 flags |= CIFS_TRANSFORM_REQ;
1345 buffer = kmalloc(qi.output_buffer_length, GFP_KERNEL);
1349 if (copy_from_user(buffer, arg + sizeof(struct smb_query_info),
1350 qi.output_buffer_length)) {
1356 memset(&open_iov, 0, sizeof(open_iov));
1357 rqst[0].rq_iov = open_iov;
1358 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1360 memset(&oparms, 0, sizeof(oparms));
1362 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1363 oparms.disposition = FILE_OPEN;
1365 oparms.create_options = CREATE_NOT_FILE;
1367 oparms.create_options = CREATE_NOT_DIR;
1369 oparms.reconnect = false;
1372 * FSCTL codes encode the special access they need in the fsctl code.
1374 if (qi.flags & PASSTHRU_FSCTL) {
1375 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1376 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1377 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1379 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1380 oparms.desired_access = GENERIC_ALL;
1382 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1383 oparms.desired_access = GENERIC_READ;
1385 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1386 oparms.desired_access = GENERIC_WRITE;
1391 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, path);
1394 smb2_set_next_command(tcon, &rqst[0]);
1397 if (qi.flags & PASSTHRU_FSCTL) {
1398 /* Can eventually relax perm check since server enforces too */
1399 if (!capable(CAP_SYS_ADMIN))
1402 memset(&io_iov, 0, sizeof(io_iov));
1403 rqst[1].rq_iov = io_iov;
1404 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1406 rc = SMB2_ioctl_init(tcon, &rqst[1],
1407 COMPOUND_FID, COMPOUND_FID,
1408 qi.info_type, true, buffer,
1409 qi.output_buffer_length,
1412 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1413 memset(&qi_iov, 0, sizeof(qi_iov));
1414 rqst[1].rq_iov = qi_iov;
1415 rqst[1].rq_nvec = 1;
1417 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
1418 COMPOUND_FID, qi.file_info_class,
1419 qi.info_type, qi.additional_information,
1420 qi.input_buffer_length,
1421 qi.output_buffer_length, buffer);
1422 } else { /* unknown flags */
1423 cifs_dbg(VFS, "invalid passthru query flags: 0x%x\n", qi.flags);
1429 smb2_set_next_command(tcon, &rqst[1]);
1430 smb2_set_related(&rqst[1]);
1433 memset(&close_iov, 0, sizeof(close_iov));
1434 rqst[2].rq_iov = close_iov;
1435 rqst[2].rq_nvec = 1;
1437 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1440 smb2_set_related(&rqst[2]);
1442 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1443 resp_buftype, rsp_iov);
1446 if (qi.flags & PASSTHRU_FSCTL) {
1447 pqi = (struct smb_query_info __user *)arg;
1448 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1449 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1450 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1451 if (qi.input_buffer_length > 0 &&
1452 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length > rsp_iov[1].iov_len) {
1456 if (copy_to_user(&pqi->input_buffer_length, &qi.input_buffer_length,
1457 sizeof(qi.input_buffer_length))) {
1461 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1462 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1463 qi.input_buffer_length)) {
1468 pqi = (struct smb_query_info __user *)arg;
1469 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1470 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1471 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1472 if (copy_to_user(&pqi->input_buffer_length, &qi.input_buffer_length,
1473 sizeof(qi.input_buffer_length))) {
1477 if (copy_to_user(pqi + 1, qi_rsp->Buffer, qi.input_buffer_length)) {
1485 SMB2_open_free(&rqst[0]);
1486 if (qi.flags & PASSTHRU_FSCTL)
1487 SMB2_ioctl_free(&rqst[1]);
1489 SMB2_query_info_free(&rqst[1]);
1491 SMB2_close_free(&rqst[2]);
1492 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1493 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1494 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1499 smb2_copychunk_range(const unsigned int xid,
1500 struct cifsFileInfo *srcfile,
1501 struct cifsFileInfo *trgtfile, u64 src_off,
1502 u64 len, u64 dest_off)
1505 unsigned int ret_data_len;
1506 struct copychunk_ioctl *pcchunk;
1507 struct copychunk_ioctl_rsp *retbuf = NULL;
1508 struct cifs_tcon *tcon;
1509 int chunks_copied = 0;
1510 bool chunk_sizes_updated = false;
1511 ssize_t bytes_written, total_bytes_written = 0;
1513 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1515 if (pcchunk == NULL)
1518 cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
1519 /* Request a key from the server to identify the source of the copy */
1520 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1521 srcfile->fid.persistent_fid,
1522 srcfile->fid.volatile_fid, pcchunk);
1524 /* Note: request_res_key sets res_key null only if rc !=0 */
1528 /* For now array only one chunk long, will make more flexible later */
1529 pcchunk->ChunkCount = cpu_to_le32(1);
1530 pcchunk->Reserved = 0;
1531 pcchunk->Reserved2 = 0;
1533 tcon = tlink_tcon(trgtfile->tlink);
1536 pcchunk->SourceOffset = cpu_to_le64(src_off);
1537 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1539 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1541 /* Request server copy to target from src identified by key */
1542 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1543 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1544 true /* is_fsctl */, (char *)pcchunk,
1545 sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1546 (char **)&retbuf, &ret_data_len);
1549 sizeof(struct copychunk_ioctl_rsp)) {
1550 cifs_dbg(VFS, "invalid cchunk response size\n");
1554 if (retbuf->TotalBytesWritten == 0) {
1555 cifs_dbg(FYI, "no bytes copied\n");
1560 * Check if server claimed to write more than we asked
1562 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1563 le32_to_cpu(pcchunk->Length)) {
1564 cifs_dbg(VFS, "invalid copy chunk response\n");
1568 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1569 cifs_dbg(VFS, "invalid num chunks written\n");
1575 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1576 src_off += bytes_written;
1577 dest_off += bytes_written;
1578 len -= bytes_written;
1579 total_bytes_written += bytes_written;
1581 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1582 le32_to_cpu(retbuf->ChunksWritten),
1583 le32_to_cpu(retbuf->ChunkBytesWritten),
1585 } else if (rc == -EINVAL) {
1586 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1589 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1590 le32_to_cpu(retbuf->ChunksWritten),
1591 le32_to_cpu(retbuf->ChunkBytesWritten),
1592 le32_to_cpu(retbuf->TotalBytesWritten));
1595 * Check if this is the first request using these sizes,
1596 * (ie check if copy succeed once with original sizes
1597 * and check if the server gave us different sizes after
1598 * we already updated max sizes on previous request).
1599 * if not then why is the server returning an error now
1601 if ((chunks_copied != 0) || chunk_sizes_updated)
1604 /* Check that server is not asking us to grow size */
1605 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1606 tcon->max_bytes_chunk)
1607 tcon->max_bytes_chunk =
1608 le32_to_cpu(retbuf->ChunkBytesWritten);
1610 goto cchunk_out; /* server gave us bogus size */
1612 /* No need to change MaxChunks since already set to 1 */
1613 chunk_sizes_updated = true;
1624 return total_bytes_written;
1628 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1629 struct cifs_fid *fid)
1631 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1635 smb2_read_data_offset(char *buf)
1637 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1639 return rsp->DataOffset;
1643 smb2_read_data_length(char *buf, bool in_remaining)
1645 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1648 return le32_to_cpu(rsp->DataRemaining);
1650 return le32_to_cpu(rsp->DataLength);
1655 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1656 struct cifs_io_parms *parms, unsigned int *bytes_read,
1657 char **buf, int *buf_type)
1659 parms->persistent_fid = pfid->persistent_fid;
1660 parms->volatile_fid = pfid->volatile_fid;
1661 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1665 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1666 struct cifs_io_parms *parms, unsigned int *written,
1667 struct kvec *iov, unsigned long nr_segs)
1670 parms->persistent_fid = pfid->persistent_fid;
1671 parms->volatile_fid = pfid->volatile_fid;
1672 return SMB2_write(xid, parms, written, iov, nr_segs);
1675 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1676 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1677 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1679 struct cifsInodeInfo *cifsi;
1682 cifsi = CIFS_I(inode);
1684 /* if file already sparse don't bother setting sparse again */
1685 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1686 return true; /* already sparse */
1688 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1689 return true; /* already not sparse */
1692 * Can't check for sparse support on share the usual way via the
1693 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1694 * since Samba server doesn't set the flag on the share, yet
1695 * supports the set sparse FSCTL and returns sparse correctly
1696 * in the file attributes. If we fail setting sparse though we
1697 * mark that server does not support sparse files for this share
1698 * to avoid repeatedly sending the unsupported fsctl to server
1699 * if the file is repeatedly extended.
1701 if (tcon->broken_sparse_sup)
1704 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1705 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1707 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
1709 tcon->broken_sparse_sup = true;
1710 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1715 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1717 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1723 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1724 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1726 __le64 eof = cpu_to_le64(size);
1727 struct inode *inode;
1730 * If extending file more than one page make sparse. Many Linux fs
1731 * make files sparse by default when extending via ftruncate
1733 inode = d_inode(cfile->dentry);
1735 if (!set_alloc && (size > inode->i_size + 8192)) {
1736 __u8 set_sparse = 1;
1738 /* whether set sparse succeeds or not, extend the file */
1739 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1742 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1743 cfile->fid.volatile_fid, cfile->pid, &eof);
1747 smb2_duplicate_extents(const unsigned int xid,
1748 struct cifsFileInfo *srcfile,
1749 struct cifsFileInfo *trgtfile, u64 src_off,
1750 u64 len, u64 dest_off)
1753 unsigned int ret_data_len;
1754 struct duplicate_extents_to_file dup_ext_buf;
1755 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1757 /* server fileays advertise duplicate extent support with this flag */
1758 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1759 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1762 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1763 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1764 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1765 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1766 dup_ext_buf.ByteCount = cpu_to_le64(len);
1767 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
1768 src_off, dest_off, len);
1770 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1772 goto duplicate_extents_out;
1774 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1775 trgtfile->fid.volatile_fid,
1776 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
1777 true /* is_fsctl */,
1778 (char *)&dup_ext_buf,
1779 sizeof(struct duplicate_extents_to_file),
1780 CIFSMaxBufSize, NULL,
1783 if (ret_data_len > 0)
1784 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
1786 duplicate_extents_out:
1791 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1792 struct cifsFileInfo *cfile)
1794 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1795 cfile->fid.volatile_fid);
1799 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1800 struct cifsFileInfo *cfile)
1802 struct fsctl_set_integrity_information_req integr_info;
1803 unsigned int ret_data_len;
1805 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1806 integr_info.Flags = 0;
1807 integr_info.Reserved = 0;
1809 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1810 cfile->fid.volatile_fid,
1811 FSCTL_SET_INTEGRITY_INFORMATION,
1812 true /* is_fsctl */,
1813 (char *)&integr_info,
1814 sizeof(struct fsctl_set_integrity_information_req),
1815 CIFSMaxBufSize, NULL,
1820 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
1821 #define GMT_TOKEN_SIZE 50
1823 #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
1826 * Input buffer contains (empty) struct smb_snapshot array with size filled in
1827 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
1830 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1831 struct cifsFileInfo *cfile, void __user *ioc_buf)
1833 char *retbuf = NULL;
1834 unsigned int ret_data_len = 0;
1836 u32 max_response_size;
1837 struct smb_snapshot_array snapshot_in;
1840 * On the first query to enumerate the list of snapshots available
1841 * for this volume the buffer begins with 0 (number of snapshots
1842 * which can be returned is zero since at that point we do not know
1843 * how big the buffer needs to be). On the second query,
1844 * it (ret_data_len) is set to number of snapshots so we can
1845 * know to set the maximum response size larger (see below).
1847 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
1851 * Note that for snapshot queries that servers like Azure expect that
1852 * the first query be minimal size (and just used to get the number/size
1853 * of previous versions) so response size must be specified as EXACTLY
1854 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
1857 if (ret_data_len == 0)
1858 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
1860 max_response_size = CIFSMaxBufSize;
1862 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1863 cfile->fid.volatile_fid,
1864 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
1865 true /* is_fsctl */,
1866 NULL, 0 /* no input data */, max_response_size,
1869 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1874 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1876 if (copy_from_user(&snapshot_in, ioc_buf,
1877 sizeof(struct smb_snapshot_array))) {
1884 * Check for min size, ie not large enough to fit even one GMT
1885 * token (snapshot). On the first ioctl some users may pass in
1886 * smaller size (or zero) to simply get the size of the array
1887 * so the user space caller can allocate sufficient memory
1888 * and retry the ioctl again with larger array size sufficient
1889 * to hold all of the snapshot GMT tokens on the second try.
1891 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
1892 ret_data_len = sizeof(struct smb_snapshot_array);
1895 * We return struct SRV_SNAPSHOT_ARRAY, followed by
1896 * the snapshot array (of 50 byte GMT tokens) each
1897 * representing an available previous version of the data
1899 if (ret_data_len > (snapshot_in.snapshot_array_size +
1900 sizeof(struct smb_snapshot_array)))
1901 ret_data_len = snapshot_in.snapshot_array_size +
1902 sizeof(struct smb_snapshot_array);
1904 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1913 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1914 const char *path, struct cifs_sb_info *cifs_sb,
1915 struct cifs_fid *fid, __u16 search_flags,
1916 struct cifs_search_info *srch_inf)
1920 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1921 struct cifs_open_parms oparms;
1923 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1928 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1929 oparms.disposition = FILE_OPEN;
1930 if (backup_cred(cifs_sb))
1931 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1933 oparms.create_options = 0;
1935 oparms.reconnect = false;
1937 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
1940 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
1944 srch_inf->entries_in_buffer = 0;
1945 srch_inf->index_of_last_entry = 2;
1947 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1948 fid->volatile_fid, 0, srch_inf);
1950 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
1951 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1957 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1958 struct cifs_fid *fid, __u16 search_flags,
1959 struct cifs_search_info *srch_inf)
1961 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1962 fid->volatile_fid, 0, srch_inf);
1966 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1967 struct cifs_fid *fid)
1969 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1973 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
1974 * the number of credits and return true. Otherwise - return false.
1977 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
1979 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
1981 if (shdr->Status != STATUS_PENDING)
1984 if (shdr->CreditRequest) {
1985 spin_lock(&server->req_lock);
1986 server->credits += le16_to_cpu(shdr->CreditRequest);
1987 spin_unlock(&server->req_lock);
1988 wake_up(&server->request_q);
1995 smb2_is_session_expired(char *buf)
1997 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
1999 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2000 shdr->Status != STATUS_USER_SESSION_DELETED)
2003 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2004 le16_to_cpu(shdr->Command),
2005 le64_to_cpu(shdr->MessageId));
2006 cifs_dbg(FYI, "Session expired or deleted\n");
2012 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2013 struct cifsInodeInfo *cinode)
2015 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2016 return SMB2_lease_break(0, tcon, cinode->lease_key,
2017 smb2_get_lease_state(cinode));
2019 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2021 CIFS_CACHE_READ(cinode) ? 1 : 0);
2025 smb2_set_related(struct smb_rqst *rqst)
2027 struct smb2_sync_hdr *shdr;
2029 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2030 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2033 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2036 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2038 struct smb2_sync_hdr *shdr;
2039 struct cifs_ses *ses = tcon->ses;
2040 struct TCP_Server_Info *server = ses->server;
2041 unsigned long len = smb_rqst_len(server, rqst);
2044 /* SMB headers in a compound are 8 byte aligned. */
2046 /* No padding needed */
2050 num_padding = 8 - (len & 7);
2051 if (!smb3_encryption_required(tcon)) {
2053 * If we do not have encryption then we can just add an extra
2054 * iov for the padding.
2056 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2057 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2062 * We can not add a small padding iov for the encryption case
2063 * because the encryption framework can not handle the padding
2065 * We have to flatten this into a single buffer and add
2066 * the padding to it.
2068 for (i = 1; i < rqst->rq_nvec; i++) {
2069 memcpy(rqst->rq_iov[0].iov_base +
2070 rqst->rq_iov[0].iov_len,
2071 rqst->rq_iov[i].iov_base,
2072 rqst->rq_iov[i].iov_len);
2073 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2075 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2077 rqst->rq_iov[0].iov_len += num_padding;
2083 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2084 shdr->NextCommand = cpu_to_le32(len);
2088 * Passes the query info response back to the caller on success.
2089 * Caller need to free this with free_rsp_buf().
2092 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2093 __le16 *utf16_path, u32 desired_access,
2094 u32 class, u32 type, u32 output_len,
2095 struct kvec *rsp, int *buftype,
2096 struct cifs_sb_info *cifs_sb)
2098 struct cifs_ses *ses = tcon->ses;
2100 struct smb_rqst rqst[3];
2101 int resp_buftype[3];
2102 struct kvec rsp_iov[3];
2103 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2104 struct kvec qi_iov[1];
2105 struct kvec close_iov[1];
2106 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2107 struct cifs_open_parms oparms;
2108 struct cifs_fid fid;
2111 if (smb3_encryption_required(tcon))
2112 flags |= CIFS_TRANSFORM_REQ;
2114 memset(rqst, 0, sizeof(rqst));
2115 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2116 memset(rsp_iov, 0, sizeof(rsp_iov));
2118 memset(&open_iov, 0, sizeof(open_iov));
2119 rqst[0].rq_iov = open_iov;
2120 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2123 oparms.desired_access = desired_access;
2124 oparms.disposition = FILE_OPEN;
2125 if (cifs_sb && backup_cred(cifs_sb))
2126 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2128 oparms.create_options = 0;
2130 oparms.reconnect = false;
2132 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2135 smb2_set_next_command(tcon, &rqst[0]);
2137 memset(&qi_iov, 0, sizeof(qi_iov));
2138 rqst[1].rq_iov = qi_iov;
2139 rqst[1].rq_nvec = 1;
2141 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
2147 smb2_set_next_command(tcon, &rqst[1]);
2148 smb2_set_related(&rqst[1]);
2150 memset(&close_iov, 0, sizeof(close_iov));
2151 rqst[2].rq_iov = close_iov;
2152 rqst[2].rq_nvec = 1;
2154 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
2157 smb2_set_related(&rqst[2]);
2159 rc = compound_send_recv(xid, ses, flags, 3, rqst,
2160 resp_buftype, rsp_iov);
2162 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2166 *buftype = resp_buftype[1];
2169 SMB2_open_free(&rqst[0]);
2170 SMB2_query_info_free(&rqst[1]);
2171 SMB2_close_free(&rqst[2]);
2172 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2173 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2178 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2179 struct kstatfs *buf)
2181 struct smb2_query_info_rsp *rsp;
2182 struct smb2_fs_full_size_info *info = NULL;
2183 __le16 utf16_path = 0; /* Null - open root of share */
2184 struct kvec rsp_iov = {NULL, 0};
2185 int buftype = CIFS_NO_BUFFER;
2189 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2190 FILE_READ_ATTRIBUTES,
2191 FS_FULL_SIZE_INFORMATION,
2192 SMB2_O_INFO_FILESYSTEM,
2193 sizeof(struct smb2_fs_full_size_info),
2194 &rsp_iov, &buftype, NULL);
2198 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2199 buf->f_type = SMB2_MAGIC_NUMBER;
2200 info = (struct smb2_fs_full_size_info *)(
2201 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2202 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2203 le32_to_cpu(rsp->OutputBufferLength),
2205 sizeof(struct smb2_fs_full_size_info));
2207 smb2_copy_fs_info_to_kstatfs(info, buf);
2210 free_rsp_buf(buftype, rsp_iov.iov_base);
2215 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2216 struct kstatfs *buf)
2219 __le16 srch_path = 0; /* Null - open root of share */
2220 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2221 struct cifs_open_parms oparms;
2222 struct cifs_fid fid;
2224 if (!tcon->posix_extensions)
2225 return smb2_queryfs(xid, tcon, buf);
2228 oparms.desired_access = FILE_READ_ATTRIBUTES;
2229 oparms.disposition = FILE_OPEN;
2230 oparms.create_options = 0;
2232 oparms.reconnect = false;
2234 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
2238 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2239 fid.volatile_fid, buf);
2240 buf->f_type = SMB2_MAGIC_NUMBER;
2241 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2246 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2248 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2249 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2253 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2254 __u64 length, __u32 type, int lock, int unlock, bool wait)
2256 if (unlock && !lock)
2257 type = SMB2_LOCKFLAG_UNLOCK;
2258 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2259 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2260 current->tgid, length, offset, type, wait);
2264 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2266 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2270 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2272 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2276 smb2_new_lease_key(struct cifs_fid *fid)
2278 generate_random_uuid(fid->lease_key);
2282 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2283 const char *search_name,
2284 struct dfs_info3_param **target_nodes,
2285 unsigned int *num_of_nodes,
2286 const struct nls_table *nls_codepage, int remap)
2289 __le16 *utf16_path = NULL;
2290 int utf16_path_len = 0;
2291 struct cifs_tcon *tcon;
2292 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2293 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2294 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2296 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
2299 * Try to use the IPC tcon, otherwise just use any
2301 tcon = ses->tcon_ipc;
2303 spin_lock(&cifs_tcp_ses_lock);
2304 tcon = list_first_entry_or_null(&ses->tcon_list,
2309 spin_unlock(&cifs_tcp_ses_lock);
2313 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2319 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2321 nls_codepage, remap);
2327 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2328 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2334 /* Highest DFS referral version understood */
2335 dfs_req->MaxReferralLevel = DFS_VERSION;
2337 /* Path to resolve in an UTF-16 null-terminated string */
2338 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2341 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2342 FSCTL_DFS_GET_REFERRALS,
2343 true /* is_fsctl */,
2344 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
2345 (char **)&dfs_rsp, &dfs_rsp_size);
2346 } while (rc == -EAGAIN);
2349 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
2350 cifs_dbg(VFS, "ioctl error in %s rc=%d\n", __func__, rc);
2354 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2355 num_of_nodes, target_nodes,
2356 nls_codepage, remap, search_name,
2357 true /* is_unicode */);
2359 cifs_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
2364 if (tcon && !tcon->ipc) {
2365 /* ipc tcons are not refcounted */
2366 spin_lock(&cifs_tcp_ses_lock);
2368 spin_unlock(&cifs_tcp_ses_lock);
2375 #define SMB2_SYMLINK_STRUCT_SIZE \
2376 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2379 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
2380 struct cifs_sb_info *cifs_sb, const char *full_path,
2381 char **target_path, bool is_reparse_point)
2384 __le16 *utf16_path = NULL;
2385 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2386 struct cifs_open_parms oparms;
2387 struct cifs_fid fid;
2388 struct kvec err_iov = {NULL, 0};
2389 struct smb2_err_rsp *err_buf = NULL;
2390 struct smb2_symlink_err_rsp *symlink;
2391 unsigned int sub_len;
2392 unsigned int sub_offset;
2393 unsigned int print_len;
2394 unsigned int print_offset;
2396 struct smb_rqst rqst[3];
2397 int resp_buftype[3];
2398 struct kvec rsp_iov[3];
2399 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2400 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2401 struct kvec close_iov[1];
2402 struct smb2_create_rsp *create_rsp;
2403 struct smb2_ioctl_rsp *ioctl_rsp;
2407 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2409 if (smb3_encryption_required(tcon))
2410 flags |= CIFS_TRANSFORM_REQ;
2412 memset(rqst, 0, sizeof(rqst));
2413 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2414 memset(rsp_iov, 0, sizeof(rsp_iov));
2416 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2421 memset(&open_iov, 0, sizeof(open_iov));
2422 rqst[0].rq_iov = open_iov;
2423 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2425 memset(&oparms, 0, sizeof(oparms));
2427 oparms.desired_access = FILE_READ_ATTRIBUTES;
2428 oparms.disposition = FILE_OPEN;
2430 if (backup_cred(cifs_sb))
2431 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2433 oparms.create_options = 0;
2434 if (is_reparse_point)
2435 oparms.create_options = OPEN_REPARSE_POINT;
2438 oparms.reconnect = false;
2440 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2443 smb2_set_next_command(tcon, &rqst[0]);
2447 memset(&io_iov, 0, sizeof(io_iov));
2448 rqst[1].rq_iov = io_iov;
2449 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
2451 rc = SMB2_ioctl_init(tcon, &rqst[1], fid.persistent_fid,
2452 fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
2453 true /* is_fctl */, NULL, 0, CIFSMaxBufSize);
2457 smb2_set_next_command(tcon, &rqst[1]);
2458 smb2_set_related(&rqst[1]);
2462 memset(&close_iov, 0, sizeof(close_iov));
2463 rqst[2].rq_iov = close_iov;
2464 rqst[2].rq_nvec = 1;
2466 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
2470 smb2_set_related(&rqst[2]);
2472 rc = compound_send_recv(xid, tcon->ses, flags, 3, rqst,
2473 resp_buftype, rsp_iov);
2475 create_rsp = rsp_iov[0].iov_base;
2476 if (create_rsp && create_rsp->sync_hdr.Status)
2477 err_iov = rsp_iov[0];
2478 ioctl_rsp = rsp_iov[1].iov_base;
2481 * Open was successful and we got an ioctl response.
2483 if ((rc == 0) && (is_reparse_point)) {
2484 /* See MS-FSCC 2.3.23 */
2486 ioctl_buf = (char *)ioctl_rsp + le32_to_cpu(ioctl_rsp->OutputOffset);
2487 plen = le32_to_cpu(ioctl_rsp->OutputCount);
2489 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
2490 rsp_iov[1].iov_len) {
2491 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", plen);
2496 /* Do stuff with ioctl_buf/plen */
2500 if (!rc || !err_iov.iov_base) {
2505 err_buf = err_iov.iov_base;
2506 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
2507 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
2512 /* open must fail on symlink - reset rc */
2514 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
2515 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
2516 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
2517 print_len = le16_to_cpu(symlink->PrintNameLength);
2518 print_offset = le16_to_cpu(symlink->PrintNameOffset);
2520 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
2525 if (err_iov.iov_len <
2526 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
2531 *target_path = cifs_strndup_from_utf16(
2532 (char *)symlink->PathBuffer + sub_offset,
2533 sub_len, true, cifs_sb->local_nls);
2534 if (!(*target_path)) {
2538 convert_delimiter(*target_path, '/');
2539 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2542 cifs_dbg(FYI, "query symlink rc %d\n", rc);
2544 SMB2_open_free(&rqst[0]);
2545 SMB2_ioctl_free(&rqst[1]);
2546 SMB2_close_free(&rqst[2]);
2547 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2548 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2549 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2553 #ifdef CONFIG_CIFS_ACL
2554 static struct cifs_ntsd *
2555 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
2556 const struct cifs_fid *cifsfid, u32 *pacllen)
2558 struct cifs_ntsd *pntsd = NULL;
2560 int rc = -EOPNOTSUPP;
2561 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2564 return ERR_CAST(tlink);
2567 cifs_dbg(FYI, "trying to get acl\n");
2569 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
2570 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
2573 cifs_put_tlink(tlink);
2575 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2582 static struct cifs_ntsd *
2583 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
2584 const char *path, u32 *pacllen)
2586 struct cifs_ntsd *pntsd = NULL;
2587 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2590 struct cifs_tcon *tcon;
2591 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2592 struct cifs_fid fid;
2593 struct cifs_open_parms oparms;
2596 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
2598 return ERR_CAST(tlink);
2600 tcon = tlink_tcon(tlink);
2603 if (backup_cred(cifs_sb))
2604 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2606 oparms.create_options = 0;
2608 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2616 oparms.desired_access = READ_CONTROL;
2617 oparms.disposition = FILE_OPEN;
2619 oparms.reconnect = false;
2621 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2624 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2625 fid.volatile_fid, (void **)&pntsd, pacllen);
2626 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2629 cifs_put_tlink(tlink);
2632 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2638 #ifdef CONFIG_CIFS_ACL
2640 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
2641 struct inode *inode, const char *path, int aclflag)
2643 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2645 int rc, access_flags = 0;
2646 struct cifs_tcon *tcon;
2647 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2648 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2649 struct cifs_fid fid;
2650 struct cifs_open_parms oparms;
2653 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
2655 return PTR_ERR(tlink);
2657 tcon = tlink_tcon(tlink);
2660 if (backup_cred(cifs_sb))
2661 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2663 oparms.create_options = 0;
2665 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
2666 access_flags = WRITE_OWNER;
2668 access_flags = WRITE_DAC;
2670 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2678 oparms.desired_access = access_flags;
2679 oparms.disposition = FILE_OPEN;
2682 oparms.reconnect = false;
2684 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2687 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2688 fid.volatile_fid, pnntsd, acllen, aclflag);
2689 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2692 cifs_put_tlink(tlink);
2696 #endif /* CIFS_ACL */
2698 /* Retrieve an ACL from the server */
2699 static struct cifs_ntsd *
2700 get_smb2_acl(struct cifs_sb_info *cifs_sb,
2701 struct inode *inode, const char *path,
2704 struct cifs_ntsd *pntsd = NULL;
2705 struct cifsFileInfo *open_file = NULL;
2708 open_file = find_readable_file(CIFS_I(inode), true);
2710 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
2712 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
2713 cifsFileInfo_put(open_file);
2718 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
2719 loff_t offset, loff_t len, bool keep_size)
2721 struct cifs_ses *ses = tcon->ses;
2722 struct inode *inode;
2723 struct cifsInodeInfo *cifsi;
2724 struct cifsFileInfo *cfile = file->private_data;
2725 struct file_zero_data_information fsctl_buf;
2732 inode = d_inode(cfile->dentry);
2733 cifsi = CIFS_I(inode);
2735 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
2736 ses->Suid, offset, len);
2739 /* if file not oplocked can't be sure whether asking to extend size */
2740 if (!CIFS_CACHE_READ(cifsi))
2741 if (keep_size == false) {
2743 trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
2744 tcon->tid, ses->Suid, offset, len, rc);
2749 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
2751 fsctl_buf.FileOffset = cpu_to_le64(offset);
2752 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2754 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2755 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
2757 sizeof(struct file_zero_data_information),
2760 goto zero_range_exit;
2763 * do we also need to change the size of the file?
2765 if (keep_size == false && i_size_read(inode) < offset + len) {
2766 eof = cpu_to_le64(offset + len);
2767 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
2768 cfile->fid.volatile_fid, cfile->pid, &eof);
2774 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
2775 ses->Suid, offset, len, rc);
2777 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
2778 ses->Suid, offset, len);
2782 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
2783 loff_t offset, loff_t len)
2785 struct inode *inode;
2786 struct cifsInodeInfo *cifsi;
2787 struct cifsFileInfo *cfile = file->private_data;
2788 struct file_zero_data_information fsctl_buf;
2791 __u8 set_sparse = 1;
2795 inode = d_inode(cfile->dentry);
2796 cifsi = CIFS_I(inode);
2798 /* Need to make file sparse, if not already, before freeing range. */
2799 /* Consider adding equivalent for compressed since it could also work */
2800 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
2806 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
2808 fsctl_buf.FileOffset = cpu_to_le64(offset);
2809 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2811 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2812 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
2813 true /* is_fctl */, (char *)&fsctl_buf,
2814 sizeof(struct file_zero_data_information),
2815 CIFSMaxBufSize, NULL, NULL);
2820 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
2821 loff_t off, loff_t len, bool keep_size)
2823 struct inode *inode;
2824 struct cifsInodeInfo *cifsi;
2825 struct cifsFileInfo *cfile = file->private_data;
2826 long rc = -EOPNOTSUPP;
2832 inode = d_inode(cfile->dentry);
2833 cifsi = CIFS_I(inode);
2835 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
2836 tcon->ses->Suid, off, len);
2837 /* if file not oplocked can't be sure whether asking to extend size */
2838 if (!CIFS_CACHE_READ(cifsi))
2839 if (keep_size == false) {
2840 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
2841 tcon->tid, tcon->ses->Suid, off, len, rc);
2847 * Files are non-sparse by default so falloc may be a no-op
2848 * Must check if file sparse. If not sparse, and not extending
2849 * then no need to do anything since file already allocated
2851 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
2852 if (keep_size == true)
2854 /* check if extending file */
2855 else if (i_size_read(inode) >= off + len)
2856 /* not extending file and already not sparse */
2858 /* BB: in future add else clause to extend file */
2862 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
2863 tcon->tid, tcon->ses->Suid, off, len, rc);
2865 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid,
2866 tcon->tid, tcon->ses->Suid, off, len);
2871 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
2873 * Check if falloc starts within first few pages of file
2874 * and ends within a few pages of the end of file to
2875 * ensure that most of file is being forced to be
2876 * fallocated now. If so then setting whole file sparse
2877 * ie potentially making a few extra pages at the beginning
2878 * or end of the file non-sparse via set_sparse is harmless.
2880 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
2882 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
2883 tcon->tid, tcon->ses->Suid, off, len, rc);
2888 smb2_set_sparse(xid, tcon, cfile, inode, false);
2891 smb2_set_sparse(xid, tcon, cfile, inode, false);
2893 if (i_size_read(inode) < off + len) {
2894 eof = cpu_to_le64(off + len);
2895 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
2896 cfile->fid.volatile_fid, cfile->pid,
2902 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
2903 tcon->ses->Suid, off, len, rc);
2905 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
2906 tcon->ses->Suid, off, len);
2912 static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
2914 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
2915 struct cifsInodeInfo *cifsi;
2916 struct inode *inode;
2918 struct file_allocated_range_buffer in_data, *out_data = NULL;
2922 if (whence != SEEK_HOLE && whence != SEEK_DATA)
2923 return generic_file_llseek(file, offset, whence);
2925 inode = d_inode(cfile->dentry);
2926 cifsi = CIFS_I(inode);
2928 if (offset < 0 || offset >= i_size_read(inode))
2933 * We need to be sure that all dirty pages are written as they
2934 * might fill holes on the server.
2935 * Note that we also MUST flush any written pages since at least
2936 * some servers (Windows2016) will not reflect recent writes in
2937 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
2939 wrcfile = find_writable_file(cifsi, false);
2941 filemap_write_and_wait(inode->i_mapping);
2942 smb2_flush_file(xid, tcon, &wrcfile->fid);
2943 cifsFileInfo_put(wrcfile);
2946 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
2947 if (whence == SEEK_HOLE)
2948 offset = i_size_read(inode);
2952 in_data.file_offset = cpu_to_le64(offset);
2953 in_data.length = cpu_to_le64(i_size_read(inode));
2955 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2956 cfile->fid.volatile_fid,
2957 FSCTL_QUERY_ALLOCATED_RANGES, true,
2958 (char *)&in_data, sizeof(in_data),
2959 sizeof(struct file_allocated_range_buffer),
2960 (char **)&out_data, &out_data_len);
2966 if (whence == SEEK_HOLE && out_data_len == 0)
2969 if (whence == SEEK_DATA && out_data_len == 0) {
2974 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
2978 if (whence == SEEK_DATA) {
2979 offset = le64_to_cpu(out_data->file_offset);
2982 if (offset < le64_to_cpu(out_data->file_offset))
2985 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
2991 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
2996 static int smb3_fiemap(struct cifs_tcon *tcon,
2997 struct cifsFileInfo *cfile,
2998 struct fiemap_extent_info *fei, u64 start, u64 len)
3001 struct file_allocated_range_buffer in_data, *out_data;
3003 int i, num, rc, flags, last_blob;
3006 if (fiemap_check_flags(fei, FIEMAP_FLAG_SYNC))
3011 in_data.file_offset = cpu_to_le64(start);
3012 in_data.length = cpu_to_le64(len);
3014 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3015 cfile->fid.volatile_fid,
3016 FSCTL_QUERY_ALLOCATED_RANGES, true,
3017 (char *)&in_data, sizeof(in_data),
3018 1024 * sizeof(struct file_allocated_range_buffer),
3019 (char **)&out_data, &out_data_len);
3028 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3032 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3037 num = out_data_len / sizeof(struct file_allocated_range_buffer);
3038 for (i = 0; i < num; i++) {
3040 if (i == num - 1 && last_blob)
3041 flags |= FIEMAP_EXTENT_LAST;
3043 rc = fiemap_fill_next_extent(fei,
3044 le64_to_cpu(out_data[i].file_offset),
3045 le64_to_cpu(out_data[i].file_offset),
3046 le64_to_cpu(out_data[i].length),
3057 next = le64_to_cpu(out_data[num - 1].file_offset) +
3058 le64_to_cpu(out_data[num - 1].length);
3059 len = len - (next - start);
3070 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3071 loff_t off, loff_t len)
3073 /* KEEP_SIZE already checked for by do_fallocate */
3074 if (mode & FALLOC_FL_PUNCH_HOLE)
3075 return smb3_punch_hole(file, tcon, off, len);
3076 else if (mode & FALLOC_FL_ZERO_RANGE) {
3077 if (mode & FALLOC_FL_KEEP_SIZE)
3078 return smb3_zero_range(file, tcon, off, len, true);
3079 return smb3_zero_range(file, tcon, off, len, false);
3080 } else if (mode == FALLOC_FL_KEEP_SIZE)
3081 return smb3_simple_falloc(file, tcon, off, len, true);
3083 return smb3_simple_falloc(file, tcon, off, len, false);
3089 smb2_downgrade_oplock(struct TCP_Server_Info *server,
3090 struct cifsInodeInfo *cinode, bool set_level2)
3093 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
3096 server->ops->set_oplock_level(cinode, 0, 0, NULL);
3100 smb21_downgrade_oplock(struct TCP_Server_Info *server,
3101 struct cifsInodeInfo *cinode, bool set_level2)
3103 server->ops->set_oplock_level(cinode,
3104 set_level2 ? SMB2_LEASE_READ_CACHING_HE :
3109 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3110 unsigned int epoch, bool *purge_cache)
3113 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3115 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3116 cinode->oplock = CIFS_CACHE_RHW_FLG;
3117 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3118 &cinode->vfs_inode);
3119 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
3120 cinode->oplock = CIFS_CACHE_RW_FLG;
3121 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3122 &cinode->vfs_inode);
3123 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3124 cinode->oplock = CIFS_CACHE_READ_FLG;
3125 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3126 &cinode->vfs_inode);
3132 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3133 unsigned int epoch, bool *purge_cache)
3135 char message[5] = {0};
3136 unsigned int new_oplock = 0;
3139 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3142 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
3143 new_oplock |= CIFS_CACHE_READ_FLG;
3144 strcat(message, "R");
3146 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
3147 new_oplock |= CIFS_CACHE_HANDLE_FLG;
3148 strcat(message, "H");
3150 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
3151 new_oplock |= CIFS_CACHE_WRITE_FLG;
3152 strcat(message, "W");
3155 strncpy(message, "None", sizeof(message));
3157 cinode->oplock = new_oplock;
3158 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3159 &cinode->vfs_inode);
3163 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3164 unsigned int epoch, bool *purge_cache)
3166 unsigned int old_oplock = cinode->oplock;
3168 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3171 *purge_cache = false;
3172 if (old_oplock == CIFS_CACHE_READ_FLG) {
3173 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3174 (epoch - cinode->epoch > 0))
3175 *purge_cache = true;
3176 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3177 (epoch - cinode->epoch > 1))
3178 *purge_cache = true;
3179 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3180 (epoch - cinode->epoch > 1))
3181 *purge_cache = true;
3182 else if (cinode->oplock == 0 &&
3183 (epoch - cinode->epoch > 0))
3184 *purge_cache = true;
3185 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
3186 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3187 (epoch - cinode->epoch > 0))
3188 *purge_cache = true;
3189 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3190 (epoch - cinode->epoch > 1))
3191 *purge_cache = true;
3193 cinode->epoch = epoch;
3198 smb2_is_read_op(__u32 oplock)
3200 return oplock == SMB2_OPLOCK_LEVEL_II;
3204 smb21_is_read_op(__u32 oplock)
3206 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
3207 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
3211 map_oplock_to_lease(u8 oplock)
3213 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3214 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
3215 else if (oplock == SMB2_OPLOCK_LEVEL_II)
3216 return SMB2_LEASE_READ_CACHING;
3217 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3218 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
3219 SMB2_LEASE_WRITE_CACHING;
3224 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3226 struct create_lease *buf;
3228 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
3232 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3233 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3235 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3236 (struct create_lease, lcontext));
3237 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
3238 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3239 (struct create_lease, Name));
3240 buf->ccontext.NameLength = cpu_to_le16(4);
3241 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3250 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
3252 struct create_lease_v2 *buf;
3254 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
3258 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3259 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3261 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3262 (struct create_lease_v2, lcontext));
3263 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
3264 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3265 (struct create_lease_v2, Name));
3266 buf->ccontext.NameLength = cpu_to_le16(4);
3267 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3276 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3278 struct create_lease *lc = (struct create_lease *)buf;
3280 *epoch = 0; /* not used */
3281 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3282 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3283 return le32_to_cpu(lc->lcontext.LeaseState);
3287 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3289 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
3291 *epoch = le16_to_cpu(lc->lcontext.Epoch);
3292 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3293 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3295 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
3296 return le32_to_cpu(lc->lcontext.LeaseState);
3300 smb2_wp_retry_size(struct inode *inode)
3302 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
3303 SMB2_MAX_BUFFER_SIZE);
3307 smb2_dir_needs_close(struct cifsFileInfo *cfile)
3309 return !cfile->invalidHandle;
3313 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
3314 struct smb_rqst *old_rq)
3316 struct smb2_sync_hdr *shdr =
3317 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
3319 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
3320 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
3321 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
3322 tr_hdr->Flags = cpu_to_le16(0x01);
3323 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
3324 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
3327 /* We can not use the normal sg_set_buf() as we will sometimes pass a
3328 * stack object as buf.
3330 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
3331 unsigned int buflen)
3333 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
3336 /* Assumes the first rqst has a transform header as the first iov.
3338 * rqst[0].rq_iov[0] is transform header
3339 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
3340 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
3342 static struct scatterlist *
3343 init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
3345 unsigned int sg_len;
3346 struct scatterlist *sg;
3349 unsigned int idx = 0;
3353 for (i = 0; i < num_rqst; i++)
3354 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
3356 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
3360 sg_init_table(sg, sg_len);
3361 for (i = 0; i < num_rqst; i++) {
3362 for (j = 0; j < rqst[i].rq_nvec; j++) {
3364 * The first rqst has a transform header where the
3365 * first 20 bytes are not part of the encrypted blob
3367 skip = (i == 0) && (j == 0) ? 20 : 0;
3368 smb2_sg_set_buf(&sg[idx++],
3369 rqst[i].rq_iov[j].iov_base + skip,
3370 rqst[i].rq_iov[j].iov_len - skip);
3373 for (j = 0; j < rqst[i].rq_npages; j++) {
3374 unsigned int len, offset;
3376 rqst_page_get_length(&rqst[i], j, &len, &offset);
3377 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
3380 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
3385 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
3387 struct cifs_ses *ses;
3390 spin_lock(&cifs_tcp_ses_lock);
3391 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3392 if (ses->Suid != ses_id)
3394 ses_enc_key = enc ? ses->smb3encryptionkey :
3395 ses->smb3decryptionkey;
3396 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
3397 spin_unlock(&cifs_tcp_ses_lock);
3400 spin_unlock(&cifs_tcp_ses_lock);
3405 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
3406 * iov[0] - transform header (associate data),
3407 * iov[1-N] - SMB2 header and pages - data to encrypt.
3408 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
3412 crypt_message(struct TCP_Server_Info *server, int num_rqst,
3413 struct smb_rqst *rqst, int enc)
3415 struct smb2_transform_hdr *tr_hdr =
3416 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
3417 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
3419 struct scatterlist *sg;
3420 u8 sign[SMB2_SIGNATURE_SIZE] = {};
3421 u8 key[SMB3_SIGN_KEY_SIZE];
3422 struct aead_request *req;
3424 unsigned int iv_len;
3425 DECLARE_CRYPTO_WAIT(wait);
3426 struct crypto_aead *tfm;
3427 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3429 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
3431 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
3436 rc = smb3_crypto_aead_allocate(server);
3438 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
3442 tfm = enc ? server->secmech.ccmaesencrypt :
3443 server->secmech.ccmaesdecrypt;
3444 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
3446 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
3450 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
3452 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
3456 req = aead_request_alloc(tfm, GFP_KERNEL);
3458 cifs_dbg(VFS, "%s: Failed to alloc aead request\n", __func__);
3463 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
3464 crypt_len += SMB2_SIGNATURE_SIZE;
3467 sg = init_sg(num_rqst, rqst, sign);
3469 cifs_dbg(VFS, "%s: Failed to init sg\n", __func__);
3474 iv_len = crypto_aead_ivsize(tfm);
3475 iv = kzalloc(iv_len, GFP_KERNEL);
3477 cifs_dbg(VFS, "%s: Failed to alloc iv\n", __func__);
3482 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
3484 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
3485 aead_request_set_ad(req, assoc_data_len);
3487 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3488 crypto_req_done, &wait);
3490 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
3491 : crypto_aead_decrypt(req), &wait);
3494 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
3505 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
3509 for (i = 0; i < num_rqst; i++) {
3510 if (rqst[i].rq_pages) {
3511 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
3512 put_page(rqst[i].rq_pages[j]);
3513 kfree(rqst[i].rq_pages);
3519 * This function will initialize new_rq and encrypt the content.
3520 * The first entry, new_rq[0], only contains a single iov which contains
3521 * a smb2_transform_hdr and is pre-allocated by the caller.
3522 * This function then populates new_rq[1+] with the content from olq_rq[0+].
3524 * The end result is an array of smb_rqst structures where the first structure
3525 * only contains a single iov for the transform header which we then can pass
3526 * to crypt_message().
3528 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
3529 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
3532 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
3533 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
3535 struct page **pages;
3536 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
3537 unsigned int npages;
3538 unsigned int orig_len = 0;
3542 for (i = 1; i < num_rqst; i++) {
3543 npages = old_rq[i - 1].rq_npages;
3544 pages = kmalloc_array(npages, sizeof(struct page *),
3549 new_rq[i].rq_pages = pages;
3550 new_rq[i].rq_npages = npages;
3551 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
3552 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
3553 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
3554 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
3555 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
3557 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
3559 for (j = 0; j < npages; j++) {
3560 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3565 /* copy pages form the old */
3566 for (j = 0; j < npages; j++) {
3568 unsigned int offset, len;
3570 rqst_page_get_length(&new_rq[i], j, &len, &offset);
3572 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
3573 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
3575 memcpy(dst, src, len);
3576 kunmap(new_rq[i].rq_pages[j]);
3577 kunmap(old_rq[i - 1].rq_pages[j]);
3581 /* fill the 1st iov with a transform header */
3582 fill_transform_hdr(tr_hdr, orig_len, old_rq);
3584 rc = crypt_message(server, num_rqst, new_rq, 1);
3585 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
3592 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
3597 smb3_is_transform_hdr(void *buf)
3599 struct smb2_transform_hdr *trhdr = buf;
3601 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
3605 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
3606 unsigned int buf_data_size, struct page **pages,
3607 unsigned int npages, unsigned int page_data_size)
3610 struct smb_rqst rqst = {NULL};
3613 iov[0].iov_base = buf;
3614 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
3615 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
3616 iov[1].iov_len = buf_data_size;
3620 rqst.rq_pages = pages;
3621 rqst.rq_npages = npages;
3622 rqst.rq_pagesz = PAGE_SIZE;
3623 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
3625 rc = crypt_message(server, 1, &rqst, 0);
3626 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
3631 memmove(buf, iov[1].iov_base, buf_data_size);
3633 server->total_read = buf_data_size + page_data_size;
3639 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
3640 unsigned int npages, unsigned int len)
3645 for (i = 0; i < npages; i++) {
3646 struct page *page = pages[i];
3650 if (len >= PAGE_SIZE) {
3651 /* enough data to fill the page */
3655 zero_user(page, len, PAGE_SIZE - len);
3658 length = cifs_read_page_from_socket(server, page, 0, n);
3661 server->total_read += length;
3668 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
3669 unsigned int cur_off, struct bio_vec **page_vec)
3671 struct bio_vec *bvec;
3674 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
3678 for (i = 0; i < npages; i++) {
3679 bvec[i].bv_page = pages[i];
3680 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
3681 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
3682 data_size -= bvec[i].bv_len;
3685 if (data_size != 0) {
3686 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
3696 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
3697 char *buf, unsigned int buf_len, struct page **pages,
3698 unsigned int npages, unsigned int page_data_size)
3700 unsigned int data_offset;
3701 unsigned int data_len;
3702 unsigned int cur_off;
3703 unsigned int cur_page_idx;
3704 unsigned int pad_len;
3705 struct cifs_readdata *rdata = mid->callback_data;
3706 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
3707 struct bio_vec *bvec = NULL;
3708 struct iov_iter iter;
3711 bool use_rdma_mr = false;
3713 if (shdr->Command != SMB2_READ) {
3714 cifs_dbg(VFS, "only big read responses are supported\n");
3718 if (server->ops->is_session_expired &&
3719 server->ops->is_session_expired(buf)) {
3720 cifs_reconnect(server);
3721 wake_up(&server->response_q);
3725 if (server->ops->is_status_pending &&
3726 server->ops->is_status_pending(buf, server))
3729 /* set up first two iov to get credits */
3730 rdata->iov[0].iov_base = buf;
3731 rdata->iov[0].iov_len = 0;
3732 rdata->iov[1].iov_base = buf;
3733 rdata->iov[1].iov_len =
3734 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
3735 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
3736 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
3737 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
3738 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
3740 rdata->result = server->ops->map_error(buf, true);
3741 if (rdata->result != 0) {
3742 cifs_dbg(FYI, "%s: server returned error %d\n",
3743 __func__, rdata->result);
3744 /* normal error on read response */
3745 dequeue_mid(mid, false);
3749 data_offset = server->ops->read_data_offset(buf);
3750 #ifdef CONFIG_CIFS_SMB_DIRECT
3751 use_rdma_mr = rdata->mr;
3753 data_len = server->ops->read_data_length(buf, use_rdma_mr);
3755 if (data_offset < server->vals->read_rsp_size) {
3757 * win2k8 sometimes sends an offset of 0 when the read
3758 * is beyond the EOF. Treat it as if the data starts just after
3761 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
3762 __func__, data_offset);
3763 data_offset = server->vals->read_rsp_size;
3764 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
3765 /* data_offset is beyond the end of smallbuf */
3766 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
3767 __func__, data_offset);
3768 rdata->result = -EIO;
3769 dequeue_mid(mid, rdata->result);
3773 pad_len = data_offset - server->vals->read_rsp_size;
3775 if (buf_len <= data_offset) {
3776 /* read response payload is in pages */
3777 cur_page_idx = pad_len / PAGE_SIZE;
3778 cur_off = pad_len % PAGE_SIZE;
3780 if (cur_page_idx != 0) {
3781 /* data offset is beyond the 1st page of response */
3782 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
3783 __func__, data_offset);
3784 rdata->result = -EIO;
3785 dequeue_mid(mid, rdata->result);
3789 if (data_len > page_data_size - pad_len) {
3790 /* data_len is corrupt -- discard frame */
3791 rdata->result = -EIO;
3792 dequeue_mid(mid, rdata->result);
3796 rdata->result = init_read_bvec(pages, npages, page_data_size,
3798 if (rdata->result != 0) {
3799 dequeue_mid(mid, rdata->result);
3803 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
3804 } else if (buf_len >= data_offset + data_len) {
3805 /* read response payload is in buf */
3806 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
3807 iov.iov_base = buf + data_offset;
3808 iov.iov_len = data_len;
3809 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
3811 /* read response payload cannot be in both buf and pages */
3812 WARN_ONCE(1, "buf can not contain only a part of read data");
3813 rdata->result = -EIO;
3814 dequeue_mid(mid, rdata->result);
3818 length = rdata->copy_into_pages(server, rdata, &iter);
3825 dequeue_mid(mid, false);
3830 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
3832 char *buf = server->smallbuf;
3833 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3834 unsigned int npages;
3835 struct page **pages;
3837 unsigned int buflen = server->pdu_size;
3841 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
3842 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
3844 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
3847 server->total_read += rc;
3849 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
3850 server->vals->read_rsp_size;
3851 npages = DIV_ROUND_UP(len, PAGE_SIZE);
3853 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
3859 for (; i < npages; i++) {
3860 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3867 /* read read data into pages */
3868 rc = read_data_into_pages(server, pages, npages, len);
3872 rc = cifs_discard_remaining_data(server);
3876 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
3877 pages, npages, len);
3881 *mid = smb2_find_mid(server, buf);
3883 cifs_dbg(FYI, "mid not found\n");
3885 cifs_dbg(FYI, "mid found\n");
3886 (*mid)->decrypted = true;
3887 rc = handle_read_data(server, *mid, buf,
3888 server->vals->read_rsp_size,
3889 pages, npages, len);
3893 for (i = i - 1; i >= 0; i--)
3898 cifs_discard_remaining_data(server);
3903 receive_encrypted_standard(struct TCP_Server_Info *server,
3904 struct mid_q_entry **mids, char **bufs,
3908 char *buf = server->smallbuf;
3910 struct smb2_sync_hdr *shdr;
3911 unsigned int pdu_length = server->pdu_size;
3912 unsigned int buf_size;
3913 struct mid_q_entry *mid_entry;
3915 char *next_buffer = NULL;
3919 /* switch to large buffer if too big for a small one */
3920 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
3921 server->large_buf = true;
3922 memcpy(server->bigbuf, buf, server->total_read);
3923 buf = server->bigbuf;
3926 /* now read the rest */
3927 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
3928 pdu_length - HEADER_SIZE(server) + 1);
3931 server->total_read += length;
3933 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
3934 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
3938 next_is_large = server->large_buf;
3940 shdr = (struct smb2_sync_hdr *)buf;
3941 if (shdr->NextCommand) {
3942 if (next_is_large) {
3943 tmpbuf = server->bigbuf;
3944 next_buffer = (char *)cifs_buf_get();
3946 tmpbuf = server->smallbuf;
3947 next_buffer = (char *)cifs_small_buf_get();
3950 tmpbuf + le32_to_cpu(shdr->NextCommand),
3951 pdu_length - le32_to_cpu(shdr->NextCommand));
3954 mid_entry = smb2_find_mid(server, buf);
3955 if (mid_entry == NULL)
3956 cifs_dbg(FYI, "mid not found\n");
3958 cifs_dbg(FYI, "mid found\n");
3959 mid_entry->decrypted = true;
3960 mid_entry->resp_buf_size = server->pdu_size;
3963 if (*num_mids >= MAX_COMPOUND) {
3964 cifs_dbg(VFS, "too many PDUs in compound\n");
3967 bufs[*num_mids] = buf;
3968 mids[(*num_mids)++] = mid_entry;
3970 if (mid_entry && mid_entry->handle)
3971 ret = mid_entry->handle(server, mid_entry);
3973 ret = cifs_handle_standard(server, mid_entry);
3975 if (ret == 0 && shdr->NextCommand) {
3976 pdu_length -= le32_to_cpu(shdr->NextCommand);
3977 server->large_buf = next_is_large;
3979 server->bigbuf = next_buffer;
3981 server->smallbuf = next_buffer;
3983 buf += le32_to_cpu(shdr->NextCommand);
3991 smb3_receive_transform(struct TCP_Server_Info *server,
3992 struct mid_q_entry **mids, char **bufs, int *num_mids)
3994 char *buf = server->smallbuf;
3995 unsigned int pdu_length = server->pdu_size;
3996 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3997 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3999 if (pdu_length < sizeof(struct smb2_transform_hdr) +
4000 sizeof(struct smb2_sync_hdr)) {
4001 cifs_dbg(VFS, "Transform message is too small (%u)\n",
4003 cifs_reconnect(server);
4004 wake_up(&server->response_q);
4005 return -ECONNABORTED;
4008 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
4009 cifs_dbg(VFS, "Transform message is broken\n");
4010 cifs_reconnect(server);
4011 wake_up(&server->response_q);
4012 return -ECONNABORTED;
4015 /* TODO: add support for compounds containing READ. */
4016 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
4018 return receive_encrypted_read(server, &mids[0]);
4021 return receive_encrypted_standard(server, mids, bufs, num_mids);
4025 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
4027 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
4029 return handle_read_data(server, mid, buf, server->pdu_size,
4034 smb2_next_header(char *buf)
4036 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
4037 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
4039 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
4040 return sizeof(struct smb2_transform_hdr) +
4041 le32_to_cpu(t_hdr->OriginalMessageSize);
4043 return le32_to_cpu(hdr->NextCommand);
4047 smb2_make_node(unsigned int xid, struct inode *inode,
4048 struct dentry *dentry, struct cifs_tcon *tcon,
4049 char *full_path, umode_t mode, dev_t dev)
4051 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4053 int create_options = CREATE_NOT_DIR | CREATE_OPTION_SPECIAL;
4054 FILE_ALL_INFO *buf = NULL;
4055 struct cifs_io_parms io_parms;
4057 struct cifs_fid fid;
4058 struct cifs_open_parms oparms;
4059 unsigned int bytes_written;
4060 struct win_dev *pdev;
4064 * Check if mounted with mount parm 'sfu' mount parm.
4065 * SFU emulation should work with all servers, but only
4066 * supports block and char device (no socket & fifo),
4067 * and was used by default in earlier versions of Windows
4069 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
4073 * TODO: Add ability to create instead via reparse point. Windows (e.g.
4074 * their current NFS server) uses this approach to expose special files
4075 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
4078 if (!S_ISCHR(mode) && !S_ISBLK(mode))
4081 cifs_dbg(FYI, "sfu compat create special file\n");
4083 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
4089 if (backup_cred(cifs_sb))
4090 create_options |= CREATE_OPEN_BACKUP_INTENT;
4093 oparms.cifs_sb = cifs_sb;
4094 oparms.desired_access = GENERIC_WRITE;
4095 oparms.create_options = create_options;
4096 oparms.disposition = FILE_CREATE;
4097 oparms.path = full_path;
4099 oparms.reconnect = false;
4101 if (tcon->ses->server->oplocks)
4102 oplock = REQ_OPLOCK;
4105 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
4110 * BB Do not bother to decode buf since no local inode yet to put
4111 * timestamps in, but we can reuse it safely.
4114 pdev = (struct win_dev *)buf;
4115 io_parms.pid = current->tgid;
4116 io_parms.tcon = tcon;
4117 io_parms.offset = 0;
4118 io_parms.length = sizeof(struct win_dev);
4119 iov[1].iov_base = buf;
4120 iov[1].iov_len = sizeof(struct win_dev);
4121 if (S_ISCHR(mode)) {
4122 memcpy(pdev->type, "IntxCHR", 8);
4123 pdev->major = cpu_to_le64(MAJOR(dev));
4124 pdev->minor = cpu_to_le64(MINOR(dev));
4125 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4126 &bytes_written, iov, 1);
4127 } else if (S_ISBLK(mode)) {
4128 memcpy(pdev->type, "IntxBLK", 8);
4129 pdev->major = cpu_to_le64(MAJOR(dev));
4130 pdev->minor = cpu_to_le64(MINOR(dev));
4131 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4132 &bytes_written, iov, 1);
4134 tcon->ses->server->ops->close(xid, tcon, &fid);
4137 /* FIXME: add code here to set EAs */
4144 struct smb_version_operations smb20_operations = {
4145 .compare_fids = smb2_compare_fids,
4146 .setup_request = smb2_setup_request,
4147 .setup_async_request = smb2_setup_async_request,
4148 .check_receive = smb2_check_receive,
4149 .add_credits = smb2_add_credits,
4150 .set_credits = smb2_set_credits,
4151 .get_credits_field = smb2_get_credits_field,
4152 .get_credits = smb2_get_credits,
4153 .wait_mtu_credits = cifs_wait_mtu_credits,
4154 .get_next_mid = smb2_get_next_mid,
4155 .revert_current_mid = smb2_revert_current_mid,
4156 .read_data_offset = smb2_read_data_offset,
4157 .read_data_length = smb2_read_data_length,
4158 .map_error = map_smb2_to_linux_error,
4159 .find_mid = smb2_find_mid,
4160 .check_message = smb2_check_message,
4161 .dump_detail = smb2_dump_detail,
4162 .clear_stats = smb2_clear_stats,
4163 .print_stats = smb2_print_stats,
4164 .is_oplock_break = smb2_is_valid_oplock_break,
4165 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4166 .downgrade_oplock = smb2_downgrade_oplock,
4167 .need_neg = smb2_need_neg,
4168 .negotiate = smb2_negotiate,
4169 .negotiate_wsize = smb2_negotiate_wsize,
4170 .negotiate_rsize = smb2_negotiate_rsize,
4171 .sess_setup = SMB2_sess_setup,
4172 .logoff = SMB2_logoff,
4173 .tree_connect = SMB2_tcon,
4174 .tree_disconnect = SMB2_tdis,
4175 .qfs_tcon = smb2_qfs_tcon,
4176 .is_path_accessible = smb2_is_path_accessible,
4177 .can_echo = smb2_can_echo,
4179 .query_path_info = smb2_query_path_info,
4180 .get_srv_inum = smb2_get_srv_inum,
4181 .query_file_info = smb2_query_file_info,
4182 .set_path_size = smb2_set_path_size,
4183 .set_file_size = smb2_set_file_size,
4184 .set_file_info = smb2_set_file_info,
4185 .set_compression = smb2_set_compression,
4186 .mkdir = smb2_mkdir,
4187 .mkdir_setinfo = smb2_mkdir_setinfo,
4188 .rmdir = smb2_rmdir,
4189 .unlink = smb2_unlink,
4190 .rename = smb2_rename_path,
4191 .create_hardlink = smb2_create_hardlink,
4192 .query_symlink = smb2_query_symlink,
4193 .query_mf_symlink = smb3_query_mf_symlink,
4194 .create_mf_symlink = smb3_create_mf_symlink,
4195 .open = smb2_open_file,
4196 .set_fid = smb2_set_fid,
4197 .close = smb2_close_file,
4198 .flush = smb2_flush_file,
4199 .async_readv = smb2_async_readv,
4200 .async_writev = smb2_async_writev,
4201 .sync_read = smb2_sync_read,
4202 .sync_write = smb2_sync_write,
4203 .query_dir_first = smb2_query_dir_first,
4204 .query_dir_next = smb2_query_dir_next,
4205 .close_dir = smb2_close_dir,
4206 .calc_smb_size = smb2_calc_size,
4207 .is_status_pending = smb2_is_status_pending,
4208 .is_session_expired = smb2_is_session_expired,
4209 .oplock_response = smb2_oplock_response,
4210 .queryfs = smb2_queryfs,
4211 .mand_lock = smb2_mand_lock,
4212 .mand_unlock_range = smb2_unlock_range,
4213 .push_mand_locks = smb2_push_mandatory_locks,
4214 .get_lease_key = smb2_get_lease_key,
4215 .set_lease_key = smb2_set_lease_key,
4216 .new_lease_key = smb2_new_lease_key,
4217 .calc_signature = smb2_calc_signature,
4218 .is_read_op = smb2_is_read_op,
4219 .set_oplock_level = smb2_set_oplock_level,
4220 .create_lease_buf = smb2_create_lease_buf,
4221 .parse_lease_buf = smb2_parse_lease_buf,
4222 .copychunk_range = smb2_copychunk_range,
4223 .wp_retry_size = smb2_wp_retry_size,
4224 .dir_needs_close = smb2_dir_needs_close,
4225 .get_dfs_refer = smb2_get_dfs_refer,
4226 .select_sectype = smb2_select_sectype,
4227 #ifdef CONFIG_CIFS_XATTR
4228 .query_all_EAs = smb2_query_eas,
4229 .set_EA = smb2_set_ea,
4230 #endif /* CIFS_XATTR */
4231 #ifdef CONFIG_CIFS_ACL
4232 .get_acl = get_smb2_acl,
4233 .get_acl_by_fid = get_smb2_acl_by_fid,
4234 .set_acl = set_smb2_acl,
4235 #endif /* CIFS_ACL */
4236 .next_header = smb2_next_header,
4237 .ioctl_query_info = smb2_ioctl_query_info,
4238 .make_node = smb2_make_node,
4239 .fiemap = smb3_fiemap,
4240 .llseek = smb3_llseek,
4243 struct smb_version_operations smb21_operations = {
4244 .compare_fids = smb2_compare_fids,
4245 .setup_request = smb2_setup_request,
4246 .setup_async_request = smb2_setup_async_request,
4247 .check_receive = smb2_check_receive,
4248 .add_credits = smb2_add_credits,
4249 .set_credits = smb2_set_credits,
4250 .get_credits_field = smb2_get_credits_field,
4251 .get_credits = smb2_get_credits,
4252 .wait_mtu_credits = smb2_wait_mtu_credits,
4253 .adjust_credits = smb2_adjust_credits,
4254 .get_next_mid = smb2_get_next_mid,
4255 .revert_current_mid = smb2_revert_current_mid,
4256 .read_data_offset = smb2_read_data_offset,
4257 .read_data_length = smb2_read_data_length,
4258 .map_error = map_smb2_to_linux_error,
4259 .find_mid = smb2_find_mid,
4260 .check_message = smb2_check_message,
4261 .dump_detail = smb2_dump_detail,
4262 .clear_stats = smb2_clear_stats,
4263 .print_stats = smb2_print_stats,
4264 .is_oplock_break = smb2_is_valid_oplock_break,
4265 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4266 .downgrade_oplock = smb21_downgrade_oplock,
4267 .need_neg = smb2_need_neg,
4268 .negotiate = smb2_negotiate,
4269 .negotiate_wsize = smb2_negotiate_wsize,
4270 .negotiate_rsize = smb2_negotiate_rsize,
4271 .sess_setup = SMB2_sess_setup,
4272 .logoff = SMB2_logoff,
4273 .tree_connect = SMB2_tcon,
4274 .tree_disconnect = SMB2_tdis,
4275 .qfs_tcon = smb2_qfs_tcon,
4276 .is_path_accessible = smb2_is_path_accessible,
4277 .can_echo = smb2_can_echo,
4279 .query_path_info = smb2_query_path_info,
4280 .get_srv_inum = smb2_get_srv_inum,
4281 .query_file_info = smb2_query_file_info,
4282 .set_path_size = smb2_set_path_size,
4283 .set_file_size = smb2_set_file_size,
4284 .set_file_info = smb2_set_file_info,
4285 .set_compression = smb2_set_compression,
4286 .mkdir = smb2_mkdir,
4287 .mkdir_setinfo = smb2_mkdir_setinfo,
4288 .rmdir = smb2_rmdir,
4289 .unlink = smb2_unlink,
4290 .rename = smb2_rename_path,
4291 .create_hardlink = smb2_create_hardlink,
4292 .query_symlink = smb2_query_symlink,
4293 .query_mf_symlink = smb3_query_mf_symlink,
4294 .create_mf_symlink = smb3_create_mf_symlink,
4295 .open = smb2_open_file,
4296 .set_fid = smb2_set_fid,
4297 .close = smb2_close_file,
4298 .flush = smb2_flush_file,
4299 .async_readv = smb2_async_readv,
4300 .async_writev = smb2_async_writev,
4301 .sync_read = smb2_sync_read,
4302 .sync_write = smb2_sync_write,
4303 .query_dir_first = smb2_query_dir_first,
4304 .query_dir_next = smb2_query_dir_next,
4305 .close_dir = smb2_close_dir,
4306 .calc_smb_size = smb2_calc_size,
4307 .is_status_pending = smb2_is_status_pending,
4308 .is_session_expired = smb2_is_session_expired,
4309 .oplock_response = smb2_oplock_response,
4310 .queryfs = smb2_queryfs,
4311 .mand_lock = smb2_mand_lock,
4312 .mand_unlock_range = smb2_unlock_range,
4313 .push_mand_locks = smb2_push_mandatory_locks,
4314 .get_lease_key = smb2_get_lease_key,
4315 .set_lease_key = smb2_set_lease_key,
4316 .new_lease_key = smb2_new_lease_key,
4317 .calc_signature = smb2_calc_signature,
4318 .is_read_op = smb21_is_read_op,
4319 .set_oplock_level = smb21_set_oplock_level,
4320 .create_lease_buf = smb2_create_lease_buf,
4321 .parse_lease_buf = smb2_parse_lease_buf,
4322 .copychunk_range = smb2_copychunk_range,
4323 .wp_retry_size = smb2_wp_retry_size,
4324 .dir_needs_close = smb2_dir_needs_close,
4325 .enum_snapshots = smb3_enum_snapshots,
4326 .get_dfs_refer = smb2_get_dfs_refer,
4327 .select_sectype = smb2_select_sectype,
4328 #ifdef CONFIG_CIFS_XATTR
4329 .query_all_EAs = smb2_query_eas,
4330 .set_EA = smb2_set_ea,
4331 #endif /* CIFS_XATTR */
4332 #ifdef CONFIG_CIFS_ACL
4333 .get_acl = get_smb2_acl,
4334 .get_acl_by_fid = get_smb2_acl_by_fid,
4335 .set_acl = set_smb2_acl,
4336 #endif /* CIFS_ACL */
4337 .next_header = smb2_next_header,
4338 .ioctl_query_info = smb2_ioctl_query_info,
4339 .make_node = smb2_make_node,
4340 .fiemap = smb3_fiemap,
4341 .llseek = smb3_llseek,
4344 struct smb_version_operations smb30_operations = {
4345 .compare_fids = smb2_compare_fids,
4346 .setup_request = smb2_setup_request,
4347 .setup_async_request = smb2_setup_async_request,
4348 .check_receive = smb2_check_receive,
4349 .add_credits = smb2_add_credits,
4350 .set_credits = smb2_set_credits,
4351 .get_credits_field = smb2_get_credits_field,
4352 .get_credits = smb2_get_credits,
4353 .wait_mtu_credits = smb2_wait_mtu_credits,
4354 .adjust_credits = smb2_adjust_credits,
4355 .get_next_mid = smb2_get_next_mid,
4356 .revert_current_mid = smb2_revert_current_mid,
4357 .read_data_offset = smb2_read_data_offset,
4358 .read_data_length = smb2_read_data_length,
4359 .map_error = map_smb2_to_linux_error,
4360 .find_mid = smb2_find_mid,
4361 .check_message = smb2_check_message,
4362 .dump_detail = smb2_dump_detail,
4363 .clear_stats = smb2_clear_stats,
4364 .print_stats = smb2_print_stats,
4365 .dump_share_caps = smb2_dump_share_caps,
4366 .is_oplock_break = smb2_is_valid_oplock_break,
4367 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4368 .downgrade_oplock = smb21_downgrade_oplock,
4369 .need_neg = smb2_need_neg,
4370 .negotiate = smb2_negotiate,
4371 .negotiate_wsize = smb3_negotiate_wsize,
4372 .negotiate_rsize = smb3_negotiate_rsize,
4373 .sess_setup = SMB2_sess_setup,
4374 .logoff = SMB2_logoff,
4375 .tree_connect = SMB2_tcon,
4376 .tree_disconnect = SMB2_tdis,
4377 .qfs_tcon = smb3_qfs_tcon,
4378 .is_path_accessible = smb2_is_path_accessible,
4379 .can_echo = smb2_can_echo,
4381 .query_path_info = smb2_query_path_info,
4382 .get_srv_inum = smb2_get_srv_inum,
4383 .query_file_info = smb2_query_file_info,
4384 .set_path_size = smb2_set_path_size,
4385 .set_file_size = smb2_set_file_size,
4386 .set_file_info = smb2_set_file_info,
4387 .set_compression = smb2_set_compression,
4388 .mkdir = smb2_mkdir,
4389 .mkdir_setinfo = smb2_mkdir_setinfo,
4390 .rmdir = smb2_rmdir,
4391 .unlink = smb2_unlink,
4392 .rename = smb2_rename_path,
4393 .create_hardlink = smb2_create_hardlink,
4394 .query_symlink = smb2_query_symlink,
4395 .query_mf_symlink = smb3_query_mf_symlink,
4396 .create_mf_symlink = smb3_create_mf_symlink,
4397 .open = smb2_open_file,
4398 .set_fid = smb2_set_fid,
4399 .close = smb2_close_file,
4400 .flush = smb2_flush_file,
4401 .async_readv = smb2_async_readv,
4402 .async_writev = smb2_async_writev,
4403 .sync_read = smb2_sync_read,
4404 .sync_write = smb2_sync_write,
4405 .query_dir_first = smb2_query_dir_first,
4406 .query_dir_next = smb2_query_dir_next,
4407 .close_dir = smb2_close_dir,
4408 .calc_smb_size = smb2_calc_size,
4409 .is_status_pending = smb2_is_status_pending,
4410 .is_session_expired = smb2_is_session_expired,
4411 .oplock_response = smb2_oplock_response,
4412 .queryfs = smb2_queryfs,
4413 .mand_lock = smb2_mand_lock,
4414 .mand_unlock_range = smb2_unlock_range,
4415 .push_mand_locks = smb2_push_mandatory_locks,
4416 .get_lease_key = smb2_get_lease_key,
4417 .set_lease_key = smb2_set_lease_key,
4418 .new_lease_key = smb2_new_lease_key,
4419 .generate_signingkey = generate_smb30signingkey,
4420 .calc_signature = smb3_calc_signature,
4421 .set_integrity = smb3_set_integrity,
4422 .is_read_op = smb21_is_read_op,
4423 .set_oplock_level = smb3_set_oplock_level,
4424 .create_lease_buf = smb3_create_lease_buf,
4425 .parse_lease_buf = smb3_parse_lease_buf,
4426 .copychunk_range = smb2_copychunk_range,
4427 .duplicate_extents = smb2_duplicate_extents,
4428 .validate_negotiate = smb3_validate_negotiate,
4429 .wp_retry_size = smb2_wp_retry_size,
4430 .dir_needs_close = smb2_dir_needs_close,
4431 .fallocate = smb3_fallocate,
4432 .enum_snapshots = smb3_enum_snapshots,
4433 .init_transform_rq = smb3_init_transform_rq,
4434 .is_transform_hdr = smb3_is_transform_hdr,
4435 .receive_transform = smb3_receive_transform,
4436 .get_dfs_refer = smb2_get_dfs_refer,
4437 .select_sectype = smb2_select_sectype,
4438 #ifdef CONFIG_CIFS_XATTR
4439 .query_all_EAs = smb2_query_eas,
4440 .set_EA = smb2_set_ea,
4441 #endif /* CIFS_XATTR */
4442 #ifdef CONFIG_CIFS_ACL
4443 .get_acl = get_smb2_acl,
4444 .get_acl_by_fid = get_smb2_acl_by_fid,
4445 .set_acl = set_smb2_acl,
4446 #endif /* CIFS_ACL */
4447 .next_header = smb2_next_header,
4448 .ioctl_query_info = smb2_ioctl_query_info,
4449 .make_node = smb2_make_node,
4450 .fiemap = smb3_fiemap,
4451 .llseek = smb3_llseek,
4454 struct smb_version_operations smb311_operations = {
4455 .compare_fids = smb2_compare_fids,
4456 .setup_request = smb2_setup_request,
4457 .setup_async_request = smb2_setup_async_request,
4458 .check_receive = smb2_check_receive,
4459 .add_credits = smb2_add_credits,
4460 .set_credits = smb2_set_credits,
4461 .get_credits_field = smb2_get_credits_field,
4462 .get_credits = smb2_get_credits,
4463 .wait_mtu_credits = smb2_wait_mtu_credits,
4464 .adjust_credits = smb2_adjust_credits,
4465 .get_next_mid = smb2_get_next_mid,
4466 .revert_current_mid = smb2_revert_current_mid,
4467 .read_data_offset = smb2_read_data_offset,
4468 .read_data_length = smb2_read_data_length,
4469 .map_error = map_smb2_to_linux_error,
4470 .find_mid = smb2_find_mid,
4471 .check_message = smb2_check_message,
4472 .dump_detail = smb2_dump_detail,
4473 .clear_stats = smb2_clear_stats,
4474 .print_stats = smb2_print_stats,
4475 .dump_share_caps = smb2_dump_share_caps,
4476 .is_oplock_break = smb2_is_valid_oplock_break,
4477 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4478 .downgrade_oplock = smb21_downgrade_oplock,
4479 .need_neg = smb2_need_neg,
4480 .negotiate = smb2_negotiate,
4481 .negotiate_wsize = smb3_negotiate_wsize,
4482 .negotiate_rsize = smb3_negotiate_rsize,
4483 .sess_setup = SMB2_sess_setup,
4484 .logoff = SMB2_logoff,
4485 .tree_connect = SMB2_tcon,
4486 .tree_disconnect = SMB2_tdis,
4487 .qfs_tcon = smb3_qfs_tcon,
4488 .is_path_accessible = smb2_is_path_accessible,
4489 .can_echo = smb2_can_echo,
4491 .query_path_info = smb2_query_path_info,
4492 .get_srv_inum = smb2_get_srv_inum,
4493 .query_file_info = smb2_query_file_info,
4494 .set_path_size = smb2_set_path_size,
4495 .set_file_size = smb2_set_file_size,
4496 .set_file_info = smb2_set_file_info,
4497 .set_compression = smb2_set_compression,
4498 .mkdir = smb2_mkdir,
4499 .mkdir_setinfo = smb2_mkdir_setinfo,
4500 .posix_mkdir = smb311_posix_mkdir,
4501 .rmdir = smb2_rmdir,
4502 .unlink = smb2_unlink,
4503 .rename = smb2_rename_path,
4504 .create_hardlink = smb2_create_hardlink,
4505 .query_symlink = smb2_query_symlink,
4506 .query_mf_symlink = smb3_query_mf_symlink,
4507 .create_mf_symlink = smb3_create_mf_symlink,
4508 .open = smb2_open_file,
4509 .set_fid = smb2_set_fid,
4510 .close = smb2_close_file,
4511 .flush = smb2_flush_file,
4512 .async_readv = smb2_async_readv,
4513 .async_writev = smb2_async_writev,
4514 .sync_read = smb2_sync_read,
4515 .sync_write = smb2_sync_write,
4516 .query_dir_first = smb2_query_dir_first,
4517 .query_dir_next = smb2_query_dir_next,
4518 .close_dir = smb2_close_dir,
4519 .calc_smb_size = smb2_calc_size,
4520 .is_status_pending = smb2_is_status_pending,
4521 .is_session_expired = smb2_is_session_expired,
4522 .oplock_response = smb2_oplock_response,
4523 .queryfs = smb311_queryfs,
4524 .mand_lock = smb2_mand_lock,
4525 .mand_unlock_range = smb2_unlock_range,
4526 .push_mand_locks = smb2_push_mandatory_locks,
4527 .get_lease_key = smb2_get_lease_key,
4528 .set_lease_key = smb2_set_lease_key,
4529 .new_lease_key = smb2_new_lease_key,
4530 .generate_signingkey = generate_smb311signingkey,
4531 .calc_signature = smb3_calc_signature,
4532 .set_integrity = smb3_set_integrity,
4533 .is_read_op = smb21_is_read_op,
4534 .set_oplock_level = smb3_set_oplock_level,
4535 .create_lease_buf = smb3_create_lease_buf,
4536 .parse_lease_buf = smb3_parse_lease_buf,
4537 .copychunk_range = smb2_copychunk_range,
4538 .duplicate_extents = smb2_duplicate_extents,
4539 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
4540 .wp_retry_size = smb2_wp_retry_size,
4541 .dir_needs_close = smb2_dir_needs_close,
4542 .fallocate = smb3_fallocate,
4543 .enum_snapshots = smb3_enum_snapshots,
4544 .init_transform_rq = smb3_init_transform_rq,
4545 .is_transform_hdr = smb3_is_transform_hdr,
4546 .receive_transform = smb3_receive_transform,
4547 .get_dfs_refer = smb2_get_dfs_refer,
4548 .select_sectype = smb2_select_sectype,
4549 #ifdef CONFIG_CIFS_XATTR
4550 .query_all_EAs = smb2_query_eas,
4551 .set_EA = smb2_set_ea,
4552 #endif /* CIFS_XATTR */
4553 #ifdef CONFIG_CIFS_ACL
4554 .get_acl = get_smb2_acl,
4555 .get_acl_by_fid = get_smb2_acl_by_fid,
4556 .set_acl = set_smb2_acl,
4557 #endif /* CIFS_ACL */
4558 .next_header = smb2_next_header,
4559 .ioctl_query_info = smb2_ioctl_query_info,
4560 .make_node = smb2_make_node,
4561 .fiemap = smb3_fiemap,
4562 .llseek = smb3_llseek,
4565 struct smb_version_values smb20_values = {
4566 .version_string = SMB20_VERSION_STRING,
4567 .protocol_id = SMB20_PROT_ID,
4568 .req_capabilities = 0, /* MBZ */
4569 .large_lock_type = 0,
4570 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4571 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4572 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4573 .header_size = sizeof(struct smb2_sync_hdr),
4574 .header_preamble_size = 0,
4575 .max_header_size = MAX_SMB2_HDR_SIZE,
4576 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4577 .lock_cmd = SMB2_LOCK,
4579 .cap_nt_find = SMB2_NT_FIND,
4580 .cap_large_files = SMB2_LARGE_FILES,
4581 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4582 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4583 .create_lease_size = sizeof(struct create_lease),
4586 struct smb_version_values smb21_values = {
4587 .version_string = SMB21_VERSION_STRING,
4588 .protocol_id = SMB21_PROT_ID,
4589 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
4590 .large_lock_type = 0,
4591 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4592 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4593 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4594 .header_size = sizeof(struct smb2_sync_hdr),
4595 .header_preamble_size = 0,
4596 .max_header_size = MAX_SMB2_HDR_SIZE,
4597 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4598 .lock_cmd = SMB2_LOCK,
4600 .cap_nt_find = SMB2_NT_FIND,
4601 .cap_large_files = SMB2_LARGE_FILES,
4602 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4603 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4604 .create_lease_size = sizeof(struct create_lease),
4607 struct smb_version_values smb3any_values = {
4608 .version_string = SMB3ANY_VERSION_STRING,
4609 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
4610 .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,
4611 .large_lock_type = 0,
4612 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4613 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4614 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4615 .header_size = sizeof(struct smb2_sync_hdr),
4616 .header_preamble_size = 0,
4617 .max_header_size = MAX_SMB2_HDR_SIZE,
4618 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4619 .lock_cmd = SMB2_LOCK,
4621 .cap_nt_find = SMB2_NT_FIND,
4622 .cap_large_files = SMB2_LARGE_FILES,
4623 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4624 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4625 .create_lease_size = sizeof(struct create_lease_v2),
4628 struct smb_version_values smbdefault_values = {
4629 .version_string = SMBDEFAULT_VERSION_STRING,
4630 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
4631 .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,
4632 .large_lock_type = 0,
4633 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4634 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4635 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4636 .header_size = sizeof(struct smb2_sync_hdr),
4637 .header_preamble_size = 0,
4638 .max_header_size = MAX_SMB2_HDR_SIZE,
4639 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4640 .lock_cmd = SMB2_LOCK,
4642 .cap_nt_find = SMB2_NT_FIND,
4643 .cap_large_files = SMB2_LARGE_FILES,
4644 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4645 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4646 .create_lease_size = sizeof(struct create_lease_v2),
4649 struct smb_version_values smb30_values = {
4650 .version_string = SMB30_VERSION_STRING,
4651 .protocol_id = SMB30_PROT_ID,
4652 .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,
4653 .large_lock_type = 0,
4654 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4655 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4656 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4657 .header_size = sizeof(struct smb2_sync_hdr),
4658 .header_preamble_size = 0,
4659 .max_header_size = MAX_SMB2_HDR_SIZE,
4660 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4661 .lock_cmd = SMB2_LOCK,
4663 .cap_nt_find = SMB2_NT_FIND,
4664 .cap_large_files = SMB2_LARGE_FILES,
4665 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4666 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4667 .create_lease_size = sizeof(struct create_lease_v2),
4670 struct smb_version_values smb302_values = {
4671 .version_string = SMB302_VERSION_STRING,
4672 .protocol_id = SMB302_PROT_ID,
4673 .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,
4674 .large_lock_type = 0,
4675 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4676 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4677 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4678 .header_size = sizeof(struct smb2_sync_hdr),
4679 .header_preamble_size = 0,
4680 .max_header_size = MAX_SMB2_HDR_SIZE,
4681 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4682 .lock_cmd = SMB2_LOCK,
4684 .cap_nt_find = SMB2_NT_FIND,
4685 .cap_large_files = SMB2_LARGE_FILES,
4686 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4687 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4688 .create_lease_size = sizeof(struct create_lease_v2),
4691 struct smb_version_values smb311_values = {
4692 .version_string = SMB311_VERSION_STRING,
4693 .protocol_id = SMB311_PROT_ID,
4694 .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,
4695 .large_lock_type = 0,
4696 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4697 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4698 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4699 .header_size = sizeof(struct smb2_sync_hdr),
4700 .header_preamble_size = 0,
4701 .max_header_size = MAX_SMB2_HDR_SIZE,
4702 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4703 .lock_cmd = SMB2_LOCK,
4705 .cap_nt_find = SMB2_NT_FIND,
4706 .cap_large_files = SMB2_LARGE_FILES,
4707 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4708 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4709 .create_lease_size = sizeof(struct create_lease_v2),