1 // SPDX-License-Identifier: GPL-2.0
3 * SMB2 version specific operations
5 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
8 #include <linux/pagemap.h>
10 #include <linux/falloc.h>
11 #include <linux/scatterlist.h>
12 #include <linux/uuid.h>
13 #include <linux/sort.h>
14 #include <crypto/aead.h>
15 #include <linux/fiemap.h>
16 #include <uapi/linux/magic.h>
20 #include "smb2proto.h"
21 #include "cifsproto.h"
22 #include "cifs_debug.h"
23 #include "cifs_unicode.h"
24 #include "smb2status.h"
26 #include "cifs_ioctl.h"
27 #include "smbdirect.h"
29 #include "fs_context.h"
30 #include "cached_dir.h"
33 /* Change credits for different ops and return the total number of credits */
35 change_conf(struct TCP_Server_Info *server)
37 server->credits += server->echo_credits + server->oplock_credits;
38 if (server->credits > server->max_credits)
39 server->credits = server->max_credits;
40 server->oplock_credits = server->echo_credits = 0;
41 switch (server->credits) {
45 server->echoes = false;
46 server->oplocks = false;
49 server->echoes = true;
50 server->oplocks = false;
51 server->echo_credits = 1;
54 server->echoes = true;
56 server->oplocks = true;
57 server->oplock_credits = 1;
59 server->oplocks = false;
61 server->echo_credits = 1;
63 server->credits -= server->echo_credits + server->oplock_credits;
64 return server->credits + server->echo_credits + server->oplock_credits;
68 smb2_add_credits(struct TCP_Server_Info *server,
69 const struct cifs_credits *credits, const int optype)
72 int scredits, in_flight;
73 unsigned int add = credits->value;
74 unsigned int instance = credits->instance;
75 bool reconnect_detected = false;
76 bool reconnect_with_invalid_credits = false;
78 spin_lock(&server->req_lock);
79 val = server->ops->get_credits_field(server, optype);
81 /* eg found case where write overlapping reconnect messed up credits */
82 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
83 reconnect_with_invalid_credits = true;
85 if ((instance == 0) || (instance == server->reconnect_instance))
88 reconnect_detected = true;
91 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
92 pr_warn_once("server overflowed SMB3 credits\n");
93 trace_smb3_overflow_credits(server->CurrentMid,
94 server->conn_id, server->hostname, *val,
95 add, server->in_flight);
97 WARN_ON_ONCE(server->in_flight == 0);
99 if (server->in_flight == 0 &&
100 ((optype & CIFS_OP_MASK) != CIFS_NEG_OP) &&
101 ((optype & CIFS_OP_MASK) != CIFS_SESS_OP))
102 rc = change_conf(server);
104 * Sometimes server returns 0 credits on oplock break ack - we need to
105 * rebalance credits in this case.
107 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
109 if (server->credits > 1) {
111 server->oplock_credits++;
113 } else if ((server->in_flight > 0) && (server->oplock_credits > 3) &&
114 ((optype & CIFS_OP_MASK) == CIFS_OBREAK_OP))
115 /* if now have too many oplock credits, rebalance so don't starve normal ops */
119 in_flight = server->in_flight;
120 spin_unlock(&server->req_lock);
121 wake_up(&server->request_q);
123 if (reconnect_detected) {
124 trace_smb3_reconnect_detected(server->CurrentMid,
125 server->conn_id, server->hostname, scredits, add, in_flight);
127 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
131 if (reconnect_with_invalid_credits) {
132 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
133 server->conn_id, server->hostname, scredits, add, in_flight);
134 cifs_dbg(FYI, "Negotiate operation when server credits is non-zero. Optype: %d, server credits: %d, credits added: %d\n",
135 optype, scredits, add);
138 spin_lock(&server->srv_lock);
139 if (server->tcpStatus == CifsNeedReconnect
140 || server->tcpStatus == CifsExiting) {
141 spin_unlock(&server->srv_lock);
144 spin_unlock(&server->srv_lock);
148 /* change_conf hasn't been executed */
151 cifs_server_dbg(VFS, "Possible client or server bug - zero credits\n");
154 cifs_server_dbg(VFS, "disabling echoes and oplocks\n");
157 cifs_dbg(FYI, "disabling oplocks\n");
160 /* change_conf rebalanced credits for different types */
164 trace_smb3_add_credits(server->CurrentMid,
165 server->conn_id, server->hostname, scredits, add, in_flight);
166 cifs_dbg(FYI, "%s: added %u credits total=%d\n", __func__, add, scredits);
170 smb2_set_credits(struct TCP_Server_Info *server, const int val)
172 int scredits, in_flight;
174 spin_lock(&server->req_lock);
175 server->credits = val;
177 server->reconnect_instance++;
179 * ChannelSequence updated for all channels in primary channel so that consistent
180 * across SMB3 requests sent on any channel. See MS-SMB2 3.2.4.1 and 3.2.7.1
182 if (SERVER_IS_CHAN(server))
183 server->primary_server->channel_sequence_num++;
185 server->channel_sequence_num++;
187 scredits = server->credits;
188 in_flight = server->in_flight;
189 spin_unlock(&server->req_lock);
191 trace_smb3_set_credits(server->CurrentMid,
192 server->conn_id, server->hostname, scredits, val, in_flight);
193 cifs_dbg(FYI, "%s: set %u credits\n", __func__, val);
195 /* don't log while holding the lock */
197 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
201 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
205 return &server->echo_credits;
207 return &server->oplock_credits;
209 return &server->credits;
214 smb2_get_credits(struct mid_q_entry *mid)
216 return mid->credits_received;
220 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
221 unsigned int *num, struct cifs_credits *credits)
224 unsigned int scredits, in_flight;
226 spin_lock(&server->req_lock);
228 spin_unlock(&server->req_lock);
230 spin_lock(&server->srv_lock);
231 if (server->tcpStatus == CifsExiting) {
232 spin_unlock(&server->srv_lock);
235 spin_unlock(&server->srv_lock);
237 spin_lock(&server->req_lock);
238 if (server->credits <= 0) {
239 spin_unlock(&server->req_lock);
240 cifs_num_waiters_inc(server);
241 rc = wait_event_killable(server->request_q,
242 has_credits(server, &server->credits, 1));
243 cifs_num_waiters_dec(server);
246 spin_lock(&server->req_lock);
248 scredits = server->credits;
249 /* can deadlock with reopen */
251 *num = SMB2_MAX_BUFFER_SIZE;
253 credits->instance = 0;
257 /* leave some credits for reopen and other ops */
259 *num = min_t(unsigned int, size,
260 scredits * SMB2_MAX_BUFFER_SIZE);
263 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
264 credits->instance = server->reconnect_instance;
265 server->credits -= credits->value;
267 if (server->in_flight > server->max_in_flight)
268 server->max_in_flight = server->in_flight;
272 scredits = server->credits;
273 in_flight = server->in_flight;
274 spin_unlock(&server->req_lock);
276 trace_smb3_wait_credits(server->CurrentMid,
277 server->conn_id, server->hostname, scredits, -(credits->value), in_flight);
278 cifs_dbg(FYI, "%s: removed %u credits total=%d\n",
279 __func__, credits->value, scredits);
285 smb2_adjust_credits(struct TCP_Server_Info *server,
286 struct cifs_credits *credits,
287 const unsigned int payload_size)
289 int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
290 int scredits, in_flight;
292 if (!credits->value || credits->value == new_val)
295 if (credits->value < new_val) {
296 trace_smb3_too_many_credits(server->CurrentMid,
297 server->conn_id, server->hostname, 0, credits->value - new_val, 0);
298 cifs_server_dbg(VFS, "request has less credits (%d) than required (%d)",
299 credits->value, new_val);
304 spin_lock(&server->req_lock);
306 if (server->reconnect_instance != credits->instance) {
307 scredits = server->credits;
308 in_flight = server->in_flight;
309 spin_unlock(&server->req_lock);
311 trace_smb3_reconnect_detected(server->CurrentMid,
312 server->conn_id, server->hostname, scredits,
313 credits->value - new_val, in_flight);
314 cifs_server_dbg(VFS, "trying to return %d credits to old session\n",
315 credits->value - new_val);
319 server->credits += credits->value - new_val;
320 scredits = server->credits;
321 in_flight = server->in_flight;
322 spin_unlock(&server->req_lock);
323 wake_up(&server->request_q);
325 trace_smb3_adj_credits(server->CurrentMid,
326 server->conn_id, server->hostname, scredits,
327 credits->value - new_val, in_flight);
328 cifs_dbg(FYI, "%s: adjust added %u credits total=%d\n",
329 __func__, credits->value - new_val, scredits);
331 credits->value = new_val;
337 smb2_get_next_mid(struct TCP_Server_Info *server)
340 /* for SMB2 we need the current value */
341 spin_lock(&server->mid_lock);
342 mid = server->CurrentMid++;
343 spin_unlock(&server->mid_lock);
348 smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
350 spin_lock(&server->mid_lock);
351 if (server->CurrentMid >= val)
352 server->CurrentMid -= val;
353 spin_unlock(&server->mid_lock);
356 static struct mid_q_entry *
357 __smb2_find_mid(struct TCP_Server_Info *server, char *buf, bool dequeue)
359 struct mid_q_entry *mid;
360 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
361 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
363 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
364 cifs_server_dbg(VFS, "Encrypted frame parsing not supported yet\n");
368 spin_lock(&server->mid_lock);
369 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
370 if ((mid->mid == wire_mid) &&
371 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
372 (mid->command == shdr->Command)) {
373 kref_get(&mid->refcount);
375 list_del_init(&mid->qhead);
376 mid->mid_flags |= MID_DELETED;
378 spin_unlock(&server->mid_lock);
382 spin_unlock(&server->mid_lock);
386 static struct mid_q_entry *
387 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
389 return __smb2_find_mid(server, buf, false);
392 static struct mid_q_entry *
393 smb2_find_dequeue_mid(struct TCP_Server_Info *server, char *buf)
395 return __smb2_find_mid(server, buf, true);
399 smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
401 #ifdef CONFIG_CIFS_DEBUG2
402 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
404 cifs_server_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
405 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
406 shdr->Id.SyncId.ProcessId);
407 if (!server->ops->check_message(buf, server->total_read, server)) {
408 cifs_server_dbg(VFS, "smb buf %p len %u\n", buf,
409 server->ops->calc_smb_size(buf));
415 smb2_need_neg(struct TCP_Server_Info *server)
417 return server->max_read == 0;
421 smb2_negotiate(const unsigned int xid,
422 struct cifs_ses *ses,
423 struct TCP_Server_Info *server)
427 spin_lock(&server->mid_lock);
428 server->CurrentMid = 0;
429 spin_unlock(&server->mid_lock);
430 rc = SMB2_negotiate(xid, ses, server);
431 /* BB we probably don't need to retry with modern servers */
438 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
440 struct TCP_Server_Info *server = tcon->ses->server;
443 /* start with specified wsize, or default */
444 wsize = ctx->wsize ? ctx->wsize : CIFS_DEFAULT_IOSIZE;
445 wsize = min_t(unsigned int, wsize, server->max_write);
446 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
447 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
453 smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
455 struct TCP_Server_Info *server = tcon->ses->server;
458 /* start with specified wsize, or default */
459 wsize = ctx->wsize ? ctx->wsize : SMB3_DEFAULT_IOSIZE;
460 wsize = min_t(unsigned int, wsize, server->max_write);
461 #ifdef CONFIG_CIFS_SMB_DIRECT
465 * Account for SMB2 data transfer packet header and
466 * possible encryption header
468 wsize = min_t(unsigned int,
470 server->smbd_conn->max_fragmented_send_size -
471 SMB2_READWRITE_PDU_HEADER_SIZE -
472 sizeof(struct smb2_transform_hdr));
474 wsize = min_t(unsigned int,
475 wsize, server->smbd_conn->max_readwrite_size);
478 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
479 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
485 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
487 struct TCP_Server_Info *server = tcon->ses->server;
490 /* start with specified rsize, or default */
491 rsize = ctx->rsize ? ctx->rsize : CIFS_DEFAULT_IOSIZE;
492 rsize = min_t(unsigned int, rsize, server->max_read);
494 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
495 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
501 smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
503 struct TCP_Server_Info *server = tcon->ses->server;
506 /* start with specified rsize, or default */
507 rsize = ctx->rsize ? ctx->rsize : SMB3_DEFAULT_IOSIZE;
508 rsize = min_t(unsigned int, rsize, server->max_read);
509 #ifdef CONFIG_CIFS_SMB_DIRECT
513 * Account for SMB2 data transfer packet header and
514 * possible encryption header
516 rsize = min_t(unsigned int,
518 server->smbd_conn->max_fragmented_recv_size -
519 SMB2_READWRITE_PDU_HEADER_SIZE -
520 sizeof(struct smb2_transform_hdr));
522 rsize = min_t(unsigned int,
523 rsize, server->smbd_conn->max_readwrite_size);
527 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
528 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
534 * compare two interfaces a and b
535 * return 0 if everything matches.
536 * return 1 if a is rdma capable, or rss capable, or has higher link speed
537 * return -1 otherwise.
540 iface_cmp(struct cifs_server_iface *a, struct cifs_server_iface *b)
545 if (a->rdma_capable == b->rdma_capable) {
546 if (a->rss_capable == b->rss_capable) {
547 if (a->speed == b->speed) {
548 cmp_ret = cifs_ipaddr_cmp((struct sockaddr *) &a->sockaddr,
549 (struct sockaddr *) &b->sockaddr);
552 else if (cmp_ret > 0)
556 } else if (a->speed > b->speed)
560 } else if (a->rss_capable > b->rss_capable)
564 } else if (a->rdma_capable > b->rdma_capable)
571 parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
572 size_t buf_len, struct cifs_ses *ses, bool in_mount)
574 struct network_interface_info_ioctl_rsp *p;
575 struct sockaddr_in *addr4;
576 struct sockaddr_in6 *addr6;
577 struct iface_info_ipv4 *p4;
578 struct iface_info_ipv6 *p6;
579 struct cifs_server_iface *info = NULL, *iface = NULL, *niface = NULL;
580 struct cifs_server_iface tmp_iface;
586 bytes_left = buf_len;
589 spin_lock(&ses->iface_lock);
590 /* do not query too frequently, this time with lock held */
591 if (ses->iface_last_update &&
592 time_before(jiffies, ses->iface_last_update +
593 (SMB_INTERFACE_POLL_INTERVAL * HZ))) {
594 spin_unlock(&ses->iface_lock);
599 * Go through iface_list and mark them as inactive
601 list_for_each_entry_safe(iface, niface, &ses->iface_list,
603 iface->is_active = 0;
605 spin_unlock(&ses->iface_lock);
608 * Samba server e.g. can return an empty interface list in some cases,
609 * which would only be a problem if we were requesting multichannel
611 if (bytes_left == 0) {
612 /* avoid spamming logs every 10 minutes, so log only in mount */
613 if ((ses->chan_max > 1) && in_mount)
615 "multichannel not available\n"
616 "Empty network interface list returned by server %s\n",
617 ses->server->hostname);
619 ses->iface_last_update = jiffies;
623 while (bytes_left >= (ssize_t)sizeof(*p)) {
624 memset(&tmp_iface, 0, sizeof(tmp_iface));
625 tmp_iface.speed = le64_to_cpu(p->LinkSpeed);
626 tmp_iface.rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE) ? 1 : 0;
627 tmp_iface.rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE) ? 1 : 0;
631 * The kernel and wire socket structures have the same
632 * layout and use network byte order but make the
633 * conversion explicit in case either one changes.
636 addr4 = (struct sockaddr_in *)&tmp_iface.sockaddr;
637 p4 = (struct iface_info_ipv4 *)p->Buffer;
638 addr4->sin_family = AF_INET;
639 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
641 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
642 addr4->sin_port = cpu_to_be16(CIFS_PORT);
644 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
648 addr6 = (struct sockaddr_in6 *)&tmp_iface.sockaddr;
649 p6 = (struct iface_info_ipv6 *)p->Buffer;
650 addr6->sin6_family = AF_INET6;
651 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
653 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
654 addr6->sin6_flowinfo = 0;
655 addr6->sin6_scope_id = 0;
656 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
658 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
663 "%s: skipping unsupported socket family\n",
669 * The iface_list is assumed to be sorted by speed.
670 * Check if the new interface exists in that list.
671 * NEVER change iface. it could be in use.
672 * Add a new one instead
674 spin_lock(&ses->iface_lock);
675 list_for_each_entry_safe(iface, niface, &ses->iface_list,
677 ret = iface_cmp(iface, &tmp_iface);
679 iface->is_active = 1;
680 spin_unlock(&ses->iface_lock);
682 } else if (ret < 0) {
683 /* all remaining ifaces are slower */
684 kref_get(&iface->refcount);
688 spin_unlock(&ses->iface_lock);
690 /* no match. insert the entry in the list */
691 info = kmalloc(sizeof(struct cifs_server_iface),
697 memcpy(info, &tmp_iface, sizeof(tmp_iface));
699 /* add this new entry to the list */
700 kref_init(&info->refcount);
703 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, ses->iface_count);
704 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
705 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
706 le32_to_cpu(p->Capability));
708 spin_lock(&ses->iface_lock);
709 if (!list_entry_is_head(iface, &ses->iface_list, iface_head)) {
710 list_add_tail(&info->iface_head, &iface->iface_head);
711 kref_put(&iface->refcount, release_iface);
713 list_add_tail(&info->iface_head, &ses->iface_list);
716 spin_unlock(&ses->iface_lock);
719 next = le32_to_cpu(p->Next);
721 bytes_left -= sizeof(*p);
724 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
729 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
734 /* Azure rounds the buffer size up 8, to a 16 byte boundary */
735 if ((bytes_left > 8) || p->Next)
736 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
738 ses->iface_last_update = jiffies;
742 * Go through the list again and put the inactive entries
744 spin_lock(&ses->iface_lock);
745 list_for_each_entry_safe(iface, niface, &ses->iface_list,
747 if (!iface->is_active) {
748 list_del(&iface->iface_head);
749 kref_put(&iface->refcount, release_iface);
753 spin_unlock(&ses->iface_lock);
759 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon, bool in_mount)
762 unsigned int ret_data_len = 0;
763 struct network_interface_info_ioctl_rsp *out_buf = NULL;
764 struct cifs_ses *ses = tcon->ses;
765 struct TCP_Server_Info *pserver;
767 /* do not query too frequently */
768 if (ses->iface_last_update &&
769 time_before(jiffies, ses->iface_last_update +
770 (SMB_INTERFACE_POLL_INTERVAL * HZ)))
773 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
774 FSCTL_QUERY_NETWORK_INTERFACE_INFO,
775 NULL /* no data input */, 0 /* no data input */,
776 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
777 if (rc == -EOPNOTSUPP) {
779 "server does not support query network interfaces\n");
781 } else if (rc != 0) {
782 cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
786 rc = parse_server_interfaces(out_buf, ret_data_len, ses, in_mount);
790 /* check if iface is still active */
791 spin_lock(&ses->chan_lock);
792 pserver = ses->chans[0].server;
793 if (pserver && !cifs_chan_is_iface_active(ses, pserver)) {
794 spin_unlock(&ses->chan_lock);
795 cifs_chan_update_iface(ses, pserver);
796 spin_lock(&ses->chan_lock);
798 spin_unlock(&ses->chan_lock);
806 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
807 struct cifs_sb_info *cifs_sb)
810 __le16 srch_path = 0; /* Null - open root of share */
811 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
812 struct cifs_open_parms oparms;
814 struct cached_fid *cfid = NULL;
816 oparms = (struct cifs_open_parms) {
819 .desired_access = FILE_READ_ATTRIBUTES,
820 .disposition = FILE_OPEN,
821 .create_options = cifs_create_options(cifs_sb, 0),
825 rc = open_cached_dir(xid, tcon, "", cifs_sb, false, &cfid);
827 memcpy(&fid, &cfid->fid, sizeof(struct cifs_fid));
829 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
834 SMB3_request_interfaces(xid, tcon, true /* called during mount */);
836 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
837 FS_ATTRIBUTE_INFORMATION);
838 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
839 FS_DEVICE_INFORMATION);
840 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
841 FS_VOLUME_INFORMATION);
842 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
843 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
845 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
847 close_cached_dir(cfid);
851 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
852 struct cifs_sb_info *cifs_sb)
855 __le16 srch_path = 0; /* Null - open root of share */
856 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
857 struct cifs_open_parms oparms;
860 oparms = (struct cifs_open_parms) {
863 .desired_access = FILE_READ_ATTRIBUTES,
864 .disposition = FILE_OPEN,
865 .create_options = cifs_create_options(cifs_sb, 0),
869 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
874 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
875 FS_ATTRIBUTE_INFORMATION);
876 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
877 FS_DEVICE_INFORMATION);
878 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
882 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
883 struct cifs_sb_info *cifs_sb, const char *full_path)
886 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
887 int err_buftype = CIFS_NO_BUFFER;
888 struct cifs_open_parms oparms;
889 struct kvec err_iov = {};
891 struct cached_fid *cfid;
895 rc = open_cached_dir(xid, tcon, full_path, cifs_sb, true, &cfid);
897 if (cfid->has_lease) {
898 close_cached_dir(cfid);
901 close_cached_dir(cfid);
904 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
908 oparms = (struct cifs_open_parms) {
911 .desired_access = FILE_READ_ATTRIBUTES,
912 .disposition = FILE_OPEN,
913 .create_options = cifs_create_options(cifs_sb, 0),
917 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
918 &err_iov, &err_buftype);
920 struct smb2_hdr *hdr = err_iov.iov_base;
922 if (unlikely(!hdr || err_buftype == CIFS_NO_BUFFER))
925 if (rc != -EREMOTE && hdr->Status == STATUS_OBJECT_NAME_INVALID) {
926 rc2 = cifs_inval_name_dfs_link_error(xid, tcon, cifs_sb,
935 if (rc == -EREMOTE && IS_ENABLED(CONFIG_CIFS_DFS_UPCALL) && cifs_sb &&
936 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS))
941 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
944 free_rsp_buf(err_buftype, err_iov.iov_base);
949 static int smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
950 struct cifs_sb_info *cifs_sb, const char *full_path,
951 u64 *uniqueid, struct cifs_open_info_data *data)
953 *uniqueid = le64_to_cpu(data->fi.IndexNumber);
957 static int smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
958 struct cifsFileInfo *cfile, struct cifs_open_info_data *data)
960 struct cifs_fid *fid = &cfile->fid;
962 if (cfile->symlink_target) {
963 data->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
964 if (!data->symlink_target)
967 return SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid, &data->fi);
970 #ifdef CONFIG_CIFS_XATTR
972 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
973 struct smb2_file_full_ea_info *src, size_t src_size,
974 const unsigned char *ea_name)
977 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
979 size_t buf_size = dst_size;
980 size_t name_len, value_len, user_name_len;
982 while (src_size > 0) {
983 name_len = (size_t)src->ea_name_length;
984 value_len = (size_t)le16_to_cpu(src->ea_value_length);
989 if (src_size < 8 + name_len + 1 + value_len) {
990 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
995 name = &src->ea_data[0];
996 value = &src->ea_data[src->ea_name_length + 1];
999 if (ea_name_len == name_len &&
1000 memcmp(ea_name, name, name_len) == 0) {
1004 if (dst_size < value_len) {
1008 memcpy(dst, value, value_len);
1012 /* 'user.' plus a terminating null */
1013 user_name_len = 5 + 1 + name_len;
1015 if (buf_size == 0) {
1016 /* skip copy - calc size only */
1017 rc += user_name_len;
1018 } else if (dst_size >= user_name_len) {
1019 dst_size -= user_name_len;
1020 memcpy(dst, "user.", 5);
1022 memcpy(dst, src->ea_data, name_len);
1026 rc += user_name_len;
1028 /* stop before overrun buffer */
1034 if (!src->next_entry_offset)
1037 if (src_size < le32_to_cpu(src->next_entry_offset)) {
1038 /* stop before overrun buffer */
1042 src_size -= le32_to_cpu(src->next_entry_offset);
1043 src = (void *)((char *)src +
1044 le32_to_cpu(src->next_entry_offset));
1047 /* didn't find the named attribute */
1056 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1057 const unsigned char *path, const unsigned char *ea_name,
1058 char *ea_data, size_t buf_size,
1059 struct cifs_sb_info *cifs_sb)
1062 struct kvec rsp_iov = {NULL, 0};
1063 int buftype = CIFS_NO_BUFFER;
1064 struct smb2_query_info_rsp *rsp;
1065 struct smb2_file_full_ea_info *info = NULL;
1067 rc = smb2_query_info_compound(xid, tcon, path,
1069 FILE_FULL_EA_INFORMATION,
1072 MAX_SMB2_CREATE_RESPONSE_SIZE -
1073 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1074 &rsp_iov, &buftype, cifs_sb);
1077 * If ea_name is NULL (listxattr) and there are no EAs,
1078 * return 0 as it's not an error. Otherwise, the specified
1079 * ea_name was not found.
1081 if (!ea_name && rc == -ENODATA)
1086 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1087 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1088 le32_to_cpu(rsp->OutputBufferLength),
1090 sizeof(struct smb2_file_full_ea_info));
1094 info = (struct smb2_file_full_ea_info *)(
1095 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1096 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1097 le32_to_cpu(rsp->OutputBufferLength), ea_name);
1100 free_rsp_buf(buftype, rsp_iov.iov_base);
1105 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1106 const char *path, const char *ea_name, const void *ea_value,
1107 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1108 struct cifs_sb_info *cifs_sb)
1110 struct smb2_compound_vars *vars;
1111 struct cifs_ses *ses = tcon->ses;
1112 struct TCP_Server_Info *server;
1113 struct smb_rqst *rqst;
1114 struct kvec *rsp_iov;
1115 __le16 *utf16_path = NULL;
1116 int ea_name_len = strlen(ea_name);
1117 int flags = CIFS_CP_CREATE_CLOSE_OP;
1119 int resp_buftype[3];
1120 struct cifs_open_parms oparms;
1121 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1122 struct cifs_fid fid;
1123 unsigned int size[1];
1125 struct smb2_file_full_ea_info *ea = NULL;
1126 struct smb2_query_info_rsp *rsp;
1127 int rc, used_len = 0;
1128 int retries = 0, cur_sleep = 1;
1131 /* reinitialize for possible replay */
1132 flags = CIFS_CP_CREATE_CLOSE_OP;
1133 oplock = SMB2_OPLOCK_LEVEL_NONE;
1134 server = cifs_pick_channel(ses);
1136 if (smb3_encryption_required(tcon))
1137 flags |= CIFS_TRANSFORM_REQ;
1139 if (ea_name_len > 255)
1142 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1146 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1147 vars = kzalloc(sizeof(*vars), GFP_KERNEL);
1153 rsp_iov = vars->rsp_iov;
1155 if (ses->server->ops->query_all_EAs) {
1157 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1163 /* If we are adding a attribute we should first check
1164 * if there will be enough space available to store
1165 * the new EA. If not we should not add it since we
1166 * would not be able to even read the EAs back.
1168 rc = smb2_query_info_compound(xid, tcon, path,
1170 FILE_FULL_EA_INFORMATION,
1173 MAX_SMB2_CREATE_RESPONSE_SIZE -
1174 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1175 &rsp_iov[1], &resp_buftype[1], cifs_sb);
1177 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1178 used_len = le32_to_cpu(rsp->OutputBufferLength);
1180 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1181 resp_buftype[1] = CIFS_NO_BUFFER;
1182 memset(&rsp_iov[1], 0, sizeof(rsp_iov[1]));
1185 /* Use a fudge factor of 256 bytes in case we collide
1186 * with a different set_EAs command.
1188 if (CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1189 MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
1190 used_len + ea_name_len + ea_value_len + 1) {
1198 rqst[0].rq_iov = vars->open_iov;
1199 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1201 oparms = (struct cifs_open_parms) {
1204 .desired_access = FILE_WRITE_EA,
1205 .disposition = FILE_OPEN,
1206 .create_options = cifs_create_options(cifs_sb, 0),
1208 .replay = !!(retries),
1211 rc = SMB2_open_init(tcon, server,
1212 &rqst[0], &oplock, &oparms, utf16_path);
1215 smb2_set_next_command(tcon, &rqst[0]);
1219 rqst[1].rq_iov = vars->si_iov;
1220 rqst[1].rq_nvec = 1;
1222 len = sizeof(*ea) + ea_name_len + ea_value_len + 1;
1223 ea = kzalloc(len, GFP_KERNEL);
1229 ea->ea_name_length = ea_name_len;
1230 ea->ea_value_length = cpu_to_le16(ea_value_len);
1231 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1232 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1237 rc = SMB2_set_info_init(tcon, server,
1238 &rqst[1], COMPOUND_FID,
1239 COMPOUND_FID, current->tgid,
1240 FILE_FULL_EA_INFORMATION,
1241 SMB2_O_INFO_FILE, 0, data, size);
1244 smb2_set_next_command(tcon, &rqst[1]);
1245 smb2_set_related(&rqst[1]);
1248 rqst[2].rq_iov = &vars->close_iov;
1249 rqst[2].rq_nvec = 1;
1250 rc = SMB2_close_init(tcon, server,
1251 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1254 smb2_set_related(&rqst[2]);
1257 smb2_set_replay(server, &rqst[0]);
1258 smb2_set_replay(server, &rqst[1]);
1259 smb2_set_replay(server, &rqst[2]);
1262 rc = compound_send_recv(xid, ses, server,
1264 resp_buftype, rsp_iov);
1265 /* no need to bump num_remote_opens because handle immediately closed */
1269 SMB2_open_free(&rqst[0]);
1270 SMB2_set_info_free(&rqst[1]);
1271 SMB2_close_free(&rqst[2]);
1272 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1273 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1274 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1279 if (is_replayable_error(rc) &&
1280 smb2_should_replay(tcon, &retries, &cur_sleep))
1288 smb2_can_echo(struct TCP_Server_Info *server)
1290 return server->echoes;
1294 smb2_clear_stats(struct cifs_tcon *tcon)
1298 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1299 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1300 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1305 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1307 seq_puts(m, "\n\tShare Capabilities:");
1308 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1309 seq_puts(m, " DFS,");
1310 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1311 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1312 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1313 seq_puts(m, " SCALEOUT,");
1314 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1315 seq_puts(m, " CLUSTER,");
1316 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1317 seq_puts(m, " ASYMMETRIC,");
1318 if (tcon->capabilities == 0)
1319 seq_puts(m, " None");
1320 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1321 seq_puts(m, " Aligned,");
1322 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1323 seq_puts(m, " Partition Aligned,");
1324 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1325 seq_puts(m, " SSD,");
1326 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1327 seq_puts(m, " TRIM-support,");
1329 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1330 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1331 if (tcon->perf_sector_size)
1332 seq_printf(m, "\tOptimal sector size: 0x%x",
1333 tcon->perf_sector_size);
1334 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1338 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1340 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1341 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1344 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1345 * totals (requests sent) since those SMBs are per-session not per tcon
1347 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1348 (long long)(tcon->bytes_read),
1349 (long long)(tcon->bytes_written));
1350 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1351 atomic_read(&tcon->num_local_opens),
1352 atomic_read(&tcon->num_remote_opens));
1353 seq_printf(m, "\nTreeConnects: %d total %d failed",
1354 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1355 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1356 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1357 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1358 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1359 seq_printf(m, "\nCreates: %d total %d failed",
1360 atomic_read(&sent[SMB2_CREATE_HE]),
1361 atomic_read(&failed[SMB2_CREATE_HE]));
1362 seq_printf(m, "\nCloses: %d total %d failed",
1363 atomic_read(&sent[SMB2_CLOSE_HE]),
1364 atomic_read(&failed[SMB2_CLOSE_HE]));
1365 seq_printf(m, "\nFlushes: %d total %d failed",
1366 atomic_read(&sent[SMB2_FLUSH_HE]),
1367 atomic_read(&failed[SMB2_FLUSH_HE]));
1368 seq_printf(m, "\nReads: %d total %d failed",
1369 atomic_read(&sent[SMB2_READ_HE]),
1370 atomic_read(&failed[SMB2_READ_HE]));
1371 seq_printf(m, "\nWrites: %d total %d failed",
1372 atomic_read(&sent[SMB2_WRITE_HE]),
1373 atomic_read(&failed[SMB2_WRITE_HE]));
1374 seq_printf(m, "\nLocks: %d total %d failed",
1375 atomic_read(&sent[SMB2_LOCK_HE]),
1376 atomic_read(&failed[SMB2_LOCK_HE]));
1377 seq_printf(m, "\nIOCTLs: %d total %d failed",
1378 atomic_read(&sent[SMB2_IOCTL_HE]),
1379 atomic_read(&failed[SMB2_IOCTL_HE]));
1380 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1381 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1382 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1383 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1384 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1385 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1386 seq_printf(m, "\nQueryInfos: %d total %d failed",
1387 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1388 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1389 seq_printf(m, "\nSetInfos: %d total %d failed",
1390 atomic_read(&sent[SMB2_SET_INFO_HE]),
1391 atomic_read(&failed[SMB2_SET_INFO_HE]));
1392 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1393 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1394 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1398 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1400 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1401 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1403 cfile->fid.persistent_fid = fid->persistent_fid;
1404 cfile->fid.volatile_fid = fid->volatile_fid;
1405 cfile->fid.access = fid->access;
1406 #ifdef CONFIG_CIFS_DEBUG2
1407 cfile->fid.mid = fid->mid;
1408 #endif /* CIFS_DEBUG2 */
1409 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1411 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1412 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1416 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1417 struct cifs_fid *fid)
1419 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1423 smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
1424 struct cifsFileInfo *cfile)
1426 struct smb2_file_network_open_info file_inf;
1427 struct inode *inode;
1430 rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
1431 cfile->fid.volatile_fid, &file_inf);
1435 inode = d_inode(cfile->dentry);
1437 spin_lock(&inode->i_lock);
1438 CIFS_I(inode)->time = jiffies;
1440 /* Creation time should not need to be updated on close */
1441 if (file_inf.LastWriteTime)
1442 inode_set_mtime_to_ts(inode,
1443 cifs_NTtimeToUnix(file_inf.LastWriteTime));
1444 if (file_inf.ChangeTime)
1445 inode_set_ctime_to_ts(inode,
1446 cifs_NTtimeToUnix(file_inf.ChangeTime));
1447 if (file_inf.LastAccessTime)
1448 inode_set_atime_to_ts(inode,
1449 cifs_NTtimeToUnix(file_inf.LastAccessTime));
1452 * i_blocks is not related to (i_size / i_blksize),
1453 * but instead 512 byte (2**9) size is required for
1454 * calculating num blocks.
1456 if (le64_to_cpu(file_inf.AllocationSize) > 4096)
1458 (512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
1460 /* End of file and Attributes should not have to be updated on close */
1461 spin_unlock(&inode->i_lock);
1465 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1466 u64 persistent_fid, u64 volatile_fid,
1467 struct copychunk_ioctl *pcchunk)
1470 unsigned int ret_data_len;
1471 struct resume_key_req *res_key;
1473 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1474 FSCTL_SRV_REQUEST_RESUME_KEY, NULL, 0 /* no input */,
1475 CIFSMaxBufSize, (char **)&res_key, &ret_data_len);
1477 if (rc == -EOPNOTSUPP) {
1478 pr_warn_once("Server share %s does not support copy range\n", tcon->tree_name);
1479 goto req_res_key_exit;
1481 cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1482 goto req_res_key_exit;
1484 if (ret_data_len < sizeof(struct resume_key_req)) {
1485 cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
1487 goto req_res_key_exit;
1489 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1497 smb2_ioctl_query_info(const unsigned int xid,
1498 struct cifs_tcon *tcon,
1499 struct cifs_sb_info *cifs_sb,
1500 __le16 *path, int is_dir,
1503 struct smb2_compound_vars *vars;
1504 struct smb_rqst *rqst;
1505 struct kvec *rsp_iov;
1506 struct cifs_ses *ses = tcon->ses;
1507 struct TCP_Server_Info *server;
1508 char __user *arg = (char __user *)p;
1509 struct smb_query_info qi;
1510 struct smb_query_info __user *pqi;
1512 int flags = CIFS_CP_CREATE_CLOSE_OP;
1513 struct smb2_query_info_rsp *qi_rsp = NULL;
1514 struct smb2_ioctl_rsp *io_rsp = NULL;
1515 void *buffer = NULL;
1516 int resp_buftype[3];
1517 struct cifs_open_parms oparms;
1518 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1519 struct cifs_fid fid;
1520 unsigned int size[2];
1522 int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
1523 void (*free_req1_func)(struct smb_rqst *r);
1524 int retries = 0, cur_sleep = 1;
1527 /* reinitialize for possible replay */
1528 flags = CIFS_CP_CREATE_CLOSE_OP;
1529 oplock = SMB2_OPLOCK_LEVEL_NONE;
1530 server = cifs_pick_channel(ses);
1532 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
1535 rqst = &vars->rqst[0];
1536 rsp_iov = &vars->rsp_iov[0];
1538 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1540 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info))) {
1544 if (qi.output_buffer_length > 1024) {
1549 if (!ses || !server) {
1554 if (smb3_encryption_required(tcon))
1555 flags |= CIFS_TRANSFORM_REQ;
1557 if (qi.output_buffer_length) {
1558 buffer = memdup_user(arg + sizeof(struct smb_query_info), qi.output_buffer_length);
1559 if (IS_ERR(buffer)) {
1560 rc = PTR_ERR(buffer);
1566 rqst[0].rq_iov = &vars->open_iov[0];
1567 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1569 oparms = (struct cifs_open_parms) {
1571 .disposition = FILE_OPEN,
1572 .create_options = cifs_create_options(cifs_sb, create_options),
1574 .replay = !!(retries),
1577 if (qi.flags & PASSTHRU_FSCTL) {
1578 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1579 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1580 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1582 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1583 oparms.desired_access = GENERIC_ALL;
1585 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1586 oparms.desired_access = GENERIC_READ;
1588 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1589 oparms.desired_access = GENERIC_WRITE;
1592 } else if (qi.flags & PASSTHRU_SET_INFO) {
1593 oparms.desired_access = GENERIC_WRITE;
1595 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1598 rc = SMB2_open_init(tcon, server,
1599 &rqst[0], &oplock, &oparms, path);
1601 goto free_output_buffer;
1602 smb2_set_next_command(tcon, &rqst[0]);
1605 if (qi.flags & PASSTHRU_FSCTL) {
1606 /* Can eventually relax perm check since server enforces too */
1607 if (!capable(CAP_SYS_ADMIN)) {
1611 rqst[1].rq_iov = &vars->io_iov[0];
1612 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1614 rc = SMB2_ioctl_init(tcon, server, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1615 qi.info_type, buffer, qi.output_buffer_length,
1616 CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1617 MAX_SMB2_CLOSE_RESPONSE_SIZE);
1618 free_req1_func = SMB2_ioctl_free;
1619 } else if (qi.flags == PASSTHRU_SET_INFO) {
1620 /* Can eventually relax perm check since server enforces too */
1621 if (!capable(CAP_SYS_ADMIN)) {
1625 if (qi.output_buffer_length < 8) {
1629 rqst[1].rq_iov = vars->si_iov;
1630 rqst[1].rq_nvec = 1;
1632 /* MS-FSCC 2.4.13 FileEndOfFileInformation */
1636 rc = SMB2_set_info_init(tcon, server, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1637 current->tgid, FILE_END_OF_FILE_INFORMATION,
1638 SMB2_O_INFO_FILE, 0, data, size);
1639 free_req1_func = SMB2_set_info_free;
1640 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1641 rqst[1].rq_iov = &vars->qi_iov;
1642 rqst[1].rq_nvec = 1;
1644 rc = SMB2_query_info_init(tcon, server,
1645 &rqst[1], COMPOUND_FID,
1646 COMPOUND_FID, qi.file_info_class,
1647 qi.info_type, qi.additional_information,
1648 qi.input_buffer_length,
1649 qi.output_buffer_length, buffer);
1650 free_req1_func = SMB2_query_info_free;
1651 } else { /* unknown flags */
1652 cifs_tcon_dbg(VFS, "Invalid passthru query flags: 0x%x\n",
1659 smb2_set_next_command(tcon, &rqst[1]);
1660 smb2_set_related(&rqst[1]);
1663 rqst[2].rq_iov = &vars->close_iov;
1664 rqst[2].rq_nvec = 1;
1666 rc = SMB2_close_init(tcon, server,
1667 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1670 smb2_set_related(&rqst[2]);
1673 smb2_set_replay(server, &rqst[0]);
1674 smb2_set_replay(server, &rqst[1]);
1675 smb2_set_replay(server, &rqst[2]);
1678 rc = compound_send_recv(xid, ses, server,
1680 resp_buftype, rsp_iov);
1684 /* No need to bump num_remote_opens since handle immediately closed */
1685 if (qi.flags & PASSTHRU_FSCTL) {
1686 pqi = (struct smb_query_info __user *)arg;
1687 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1688 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1689 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1690 if (qi.input_buffer_length > 0 &&
1691 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1692 > rsp_iov[1].iov_len) {
1697 if (copy_to_user(&pqi->input_buffer_length,
1698 &qi.input_buffer_length,
1699 sizeof(qi.input_buffer_length))) {
1704 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1705 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1706 qi.input_buffer_length))
1709 pqi = (struct smb_query_info __user *)arg;
1710 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1711 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1712 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1713 if (copy_to_user(&pqi->input_buffer_length,
1714 &qi.input_buffer_length,
1715 sizeof(qi.input_buffer_length))) {
1720 if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1721 qi.input_buffer_length))
1726 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1727 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1728 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1729 SMB2_close_free(&rqst[2]);
1731 free_req1_func(&rqst[1]);
1733 SMB2_open_free(&rqst[0]);
1739 if (is_replayable_error(rc) &&
1740 smb2_should_replay(tcon, &retries, &cur_sleep))
1747 smb2_copychunk_range(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 copychunk_ioctl *pcchunk;
1755 struct copychunk_ioctl_rsp *retbuf = NULL;
1756 struct cifs_tcon *tcon;
1757 int chunks_copied = 0;
1758 bool chunk_sizes_updated = false;
1759 ssize_t bytes_written, total_bytes_written = 0;
1761 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1762 if (pcchunk == NULL)
1765 cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
1766 /* Request a key from the server to identify the source of the copy */
1767 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1768 srcfile->fid.persistent_fid,
1769 srcfile->fid.volatile_fid, pcchunk);
1771 /* Note: request_res_key sets res_key null only if rc !=0 */
1775 /* For now array only one chunk long, will make more flexible later */
1776 pcchunk->ChunkCount = cpu_to_le32(1);
1777 pcchunk->Reserved = 0;
1778 pcchunk->Reserved2 = 0;
1780 tcon = tlink_tcon(trgtfile->tlink);
1783 pcchunk->SourceOffset = cpu_to_le64(src_off);
1784 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1786 cpu_to_le32(min_t(u64, len, tcon->max_bytes_chunk));
1788 /* Request server copy to target from src identified by key */
1791 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1792 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1793 (char *)pcchunk, sizeof(struct copychunk_ioctl),
1794 CIFSMaxBufSize, (char **)&retbuf, &ret_data_len);
1797 sizeof(struct copychunk_ioctl_rsp)) {
1798 cifs_tcon_dbg(VFS, "Invalid cchunk response size\n");
1802 if (retbuf->TotalBytesWritten == 0) {
1803 cifs_dbg(FYI, "no bytes copied\n");
1808 * Check if server claimed to write more than we asked
1810 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1811 le32_to_cpu(pcchunk->Length)) {
1812 cifs_tcon_dbg(VFS, "Invalid copy chunk response\n");
1816 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1817 cifs_tcon_dbg(VFS, "Invalid num chunks written\n");
1823 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1824 src_off += bytes_written;
1825 dest_off += bytes_written;
1826 len -= bytes_written;
1827 total_bytes_written += bytes_written;
1829 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1830 le32_to_cpu(retbuf->ChunksWritten),
1831 le32_to_cpu(retbuf->ChunkBytesWritten),
1833 } else if (rc == -EINVAL) {
1834 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1837 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1838 le32_to_cpu(retbuf->ChunksWritten),
1839 le32_to_cpu(retbuf->ChunkBytesWritten),
1840 le32_to_cpu(retbuf->TotalBytesWritten));
1843 * Check if this is the first request using these sizes,
1844 * (ie check if copy succeed once with original sizes
1845 * and check if the server gave us different sizes after
1846 * we already updated max sizes on previous request).
1847 * if not then why is the server returning an error now
1849 if ((chunks_copied != 0) || chunk_sizes_updated)
1852 /* Check that server is not asking us to grow size */
1853 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1854 tcon->max_bytes_chunk)
1855 tcon->max_bytes_chunk =
1856 le32_to_cpu(retbuf->ChunkBytesWritten);
1858 goto cchunk_out; /* server gave us bogus size */
1860 /* No need to change MaxChunks since already set to 1 */
1861 chunk_sizes_updated = true;
1872 return total_bytes_written;
1876 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1877 struct cifs_fid *fid)
1879 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1883 smb2_read_data_offset(char *buf)
1885 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1887 return rsp->DataOffset;
1891 smb2_read_data_length(char *buf, bool in_remaining)
1893 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1896 return le32_to_cpu(rsp->DataRemaining);
1898 return le32_to_cpu(rsp->DataLength);
1903 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1904 struct cifs_io_parms *parms, unsigned int *bytes_read,
1905 char **buf, int *buf_type)
1907 parms->persistent_fid = pfid->persistent_fid;
1908 parms->volatile_fid = pfid->volatile_fid;
1909 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1913 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1914 struct cifs_io_parms *parms, unsigned int *written,
1915 struct kvec *iov, unsigned long nr_segs)
1918 parms->persistent_fid = pfid->persistent_fid;
1919 parms->volatile_fid = pfid->volatile_fid;
1920 return SMB2_write(xid, parms, written, iov, nr_segs);
1923 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1924 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1925 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1927 struct cifsInodeInfo *cifsi;
1930 cifsi = CIFS_I(inode);
1932 /* if file already sparse don't bother setting sparse again */
1933 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1934 return true; /* already sparse */
1936 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1937 return true; /* already not sparse */
1940 * Can't check for sparse support on share the usual way via the
1941 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1942 * since Samba server doesn't set the flag on the share, yet
1943 * supports the set sparse FSCTL and returns sparse correctly
1944 * in the file attributes. If we fail setting sparse though we
1945 * mark that server does not support sparse files for this share
1946 * to avoid repeatedly sending the unsupported fsctl to server
1947 * if the file is repeatedly extended.
1949 if (tcon->broken_sparse_sup)
1952 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1953 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1954 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
1956 tcon->broken_sparse_sup = true;
1957 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1962 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1964 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1970 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1971 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1973 struct inode *inode;
1976 * If extending file more than one page make sparse. Many Linux fs
1977 * make files sparse by default when extending via ftruncate
1979 inode = d_inode(cfile->dentry);
1981 if (!set_alloc && (size > inode->i_size + 8192)) {
1982 __u8 set_sparse = 1;
1984 /* whether set sparse succeeds or not, extend the file */
1985 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1988 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1989 cfile->fid.volatile_fid, cfile->pid, size);
1993 smb2_duplicate_extents(const unsigned int xid,
1994 struct cifsFileInfo *srcfile,
1995 struct cifsFileInfo *trgtfile, u64 src_off,
1996 u64 len, u64 dest_off)
1999 unsigned int ret_data_len;
2000 struct inode *inode;
2001 struct duplicate_extents_to_file dup_ext_buf;
2002 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
2004 /* server fileays advertise duplicate extent support with this flag */
2005 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
2006 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
2009 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
2010 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
2011 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
2012 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
2013 dup_ext_buf.ByteCount = cpu_to_le64(len);
2014 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
2015 src_off, dest_off, len);
2017 inode = d_inode(trgtfile->dentry);
2018 if (inode->i_size < dest_off + len) {
2019 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
2021 goto duplicate_extents_out;
2024 * Although also could set plausible allocation size (i_blocks)
2025 * here in addition to setting the file size, in reflink
2026 * it is likely that the target file is sparse. Its allocation
2027 * size will be queried on next revalidate, but it is important
2028 * to make sure that file's cached size is updated immediately
2030 cifs_setsize(inode, dest_off + len);
2032 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
2033 trgtfile->fid.volatile_fid,
2034 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
2035 (char *)&dup_ext_buf,
2036 sizeof(struct duplicate_extents_to_file),
2037 CIFSMaxBufSize, NULL,
2040 if (ret_data_len > 0)
2041 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
2043 duplicate_extents_out:
2048 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2049 struct cifsFileInfo *cfile)
2051 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
2052 cfile->fid.volatile_fid);
2056 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
2057 struct cifsFileInfo *cfile)
2059 struct fsctl_set_integrity_information_req integr_info;
2060 unsigned int ret_data_len;
2062 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
2063 integr_info.Flags = 0;
2064 integr_info.Reserved = 0;
2066 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2067 cfile->fid.volatile_fid,
2068 FSCTL_SET_INTEGRITY_INFORMATION,
2069 (char *)&integr_info,
2070 sizeof(struct fsctl_set_integrity_information_req),
2071 CIFSMaxBufSize, NULL,
2076 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
2077 #define GMT_TOKEN_SIZE 50
2079 #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
2082 * Input buffer contains (empty) struct smb_snapshot array with size filled in
2083 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
2086 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
2087 struct cifsFileInfo *cfile, void __user *ioc_buf)
2089 char *retbuf = NULL;
2090 unsigned int ret_data_len = 0;
2092 u32 max_response_size;
2093 struct smb_snapshot_array snapshot_in;
2096 * On the first query to enumerate the list of snapshots available
2097 * for this volume the buffer begins with 0 (number of snapshots
2098 * which can be returned is zero since at that point we do not know
2099 * how big the buffer needs to be). On the second query,
2100 * it (ret_data_len) is set to number of snapshots so we can
2101 * know to set the maximum response size larger (see below).
2103 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
2107 * Note that for snapshot queries that servers like Azure expect that
2108 * the first query be minimal size (and just used to get the number/size
2109 * of previous versions) so response size must be specified as EXACTLY
2110 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2113 if (ret_data_len == 0)
2114 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
2116 max_response_size = CIFSMaxBufSize;
2118 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2119 cfile->fid.volatile_fid,
2120 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
2121 NULL, 0 /* no input data */, max_response_size,
2124 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2129 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2131 if (copy_from_user(&snapshot_in, ioc_buf,
2132 sizeof(struct smb_snapshot_array))) {
2139 * Check for min size, ie not large enough to fit even one GMT
2140 * token (snapshot). On the first ioctl some users may pass in
2141 * smaller size (or zero) to simply get the size of the array
2142 * so the user space caller can allocate sufficient memory
2143 * and retry the ioctl again with larger array size sufficient
2144 * to hold all of the snapshot GMT tokens on the second try.
2146 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2147 ret_data_len = sizeof(struct smb_snapshot_array);
2150 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2151 * the snapshot array (of 50 byte GMT tokens) each
2152 * representing an available previous version of the data
2154 if (ret_data_len > (snapshot_in.snapshot_array_size +
2155 sizeof(struct smb_snapshot_array)))
2156 ret_data_len = snapshot_in.snapshot_array_size +
2157 sizeof(struct smb_snapshot_array);
2159 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2170 smb3_notify(const unsigned int xid, struct file *pfile,
2171 void __user *ioc_buf, bool return_changes)
2173 struct smb3_notify_info notify;
2174 struct smb3_notify_info __user *pnotify_buf;
2175 struct dentry *dentry = pfile->f_path.dentry;
2176 struct inode *inode = file_inode(pfile);
2177 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2178 struct cifs_open_parms oparms;
2179 struct cifs_fid fid;
2180 struct cifs_tcon *tcon;
2181 const unsigned char *path;
2182 char *returned_ioctl_info = NULL;
2183 void *page = alloc_dentry_path();
2184 __le16 *utf16_path = NULL;
2185 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2189 path = build_path_from_dentry(dentry, page);
2195 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2196 if (utf16_path == NULL) {
2201 if (return_changes) {
2202 if (copy_from_user(¬ify, ioc_buf, sizeof(struct smb3_notify_info))) {
2207 if (copy_from_user(¬ify, ioc_buf, sizeof(struct smb3_notify))) {
2211 notify.data_len = 0;
2214 tcon = cifs_sb_master_tcon(cifs_sb);
2215 oparms = (struct cifs_open_parms) {
2218 .desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA,
2219 .disposition = FILE_OPEN,
2220 .create_options = cifs_create_options(cifs_sb, 0),
2224 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
2229 rc = SMB2_change_notify(xid, tcon, fid.persistent_fid, fid.volatile_fid,
2230 notify.watch_tree, notify.completion_filter,
2231 notify.data_len, &returned_ioctl_info, &ret_len);
2233 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2235 cifs_dbg(FYI, "change notify for path %s rc %d\n", path, rc);
2236 if (return_changes && (ret_len > 0) && (notify.data_len > 0)) {
2237 if (ret_len > notify.data_len)
2238 ret_len = notify.data_len;
2239 pnotify_buf = (struct smb3_notify_info __user *)ioc_buf;
2240 if (copy_to_user(pnotify_buf->notify_data, returned_ioctl_info, ret_len))
2242 else if (copy_to_user(&pnotify_buf->data_len, &ret_len, sizeof(ret_len)))
2245 kfree(returned_ioctl_info);
2247 free_dentry_path(page);
2253 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2254 const char *path, struct cifs_sb_info *cifs_sb,
2255 struct cifs_fid *fid, __u16 search_flags,
2256 struct cifs_search_info *srch_inf)
2259 struct smb_rqst rqst[2];
2260 struct kvec rsp_iov[2];
2261 int resp_buftype[2];
2262 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2263 struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
2265 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2266 struct cifs_open_parms oparms;
2267 struct smb2_query_directory_rsp *qd_rsp = NULL;
2268 struct smb2_create_rsp *op_rsp = NULL;
2269 struct TCP_Server_Info *server;
2270 int retries = 0, cur_sleep = 1;
2273 /* reinitialize for possible replay */
2275 oplock = SMB2_OPLOCK_LEVEL_NONE;
2276 server = cifs_pick_channel(tcon->ses);
2278 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2282 if (smb3_encryption_required(tcon))
2283 flags |= CIFS_TRANSFORM_REQ;
2285 memset(rqst, 0, sizeof(rqst));
2286 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2287 memset(rsp_iov, 0, sizeof(rsp_iov));
2290 memset(&open_iov, 0, sizeof(open_iov));
2291 rqst[0].rq_iov = open_iov;
2292 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2294 oparms = (struct cifs_open_parms) {
2297 .desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA,
2298 .disposition = FILE_OPEN,
2299 .create_options = cifs_create_options(cifs_sb, 0),
2301 .replay = !!(retries),
2304 rc = SMB2_open_init(tcon, server,
2305 &rqst[0], &oplock, &oparms, utf16_path);
2308 smb2_set_next_command(tcon, &rqst[0]);
2310 /* Query directory */
2311 srch_inf->entries_in_buffer = 0;
2312 srch_inf->index_of_last_entry = 2;
2314 memset(&qd_iov, 0, sizeof(qd_iov));
2315 rqst[1].rq_iov = qd_iov;
2316 rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
2318 rc = SMB2_query_directory_init(xid, tcon, server,
2320 COMPOUND_FID, COMPOUND_FID,
2321 0, srch_inf->info_level);
2325 smb2_set_related(&rqst[1]);
2328 smb2_set_replay(server, &rqst[0]);
2329 smb2_set_replay(server, &rqst[1]);
2332 rc = compound_send_recv(xid, tcon->ses, server,
2334 resp_buftype, rsp_iov);
2336 /* If the open failed there is nothing to do */
2337 op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
2338 if (op_rsp == NULL || op_rsp->hdr.Status != STATUS_SUCCESS) {
2339 cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
2342 fid->persistent_fid = op_rsp->PersistentFileId;
2343 fid->volatile_fid = op_rsp->VolatileFileId;
2345 /* Anything else than ENODATA means a genuine error */
2346 if (rc && rc != -ENODATA) {
2347 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2348 cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
2349 trace_smb3_query_dir_err(xid, fid->persistent_fid,
2350 tcon->tid, tcon->ses->Suid, 0, 0, rc);
2354 atomic_inc(&tcon->num_remote_opens);
2356 qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
2357 if (qd_rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2358 trace_smb3_query_dir_done(xid, fid->persistent_fid,
2359 tcon->tid, tcon->ses->Suid, 0, 0);
2360 srch_inf->endOfSearch = true;
2365 rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
2368 trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
2369 tcon->ses->Suid, 0, 0, rc);
2372 resp_buftype[1] = CIFS_NO_BUFFER;
2374 trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
2375 tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
2379 SMB2_open_free(&rqst[0]);
2380 SMB2_query_directory_free(&rqst[1]);
2381 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2382 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2384 if (is_replayable_error(rc) &&
2385 smb2_should_replay(tcon, &retries, &cur_sleep))
2392 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2393 struct cifs_fid *fid, __u16 search_flags,
2394 struct cifs_search_info *srch_inf)
2396 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2397 fid->volatile_fid, 0, srch_inf);
2401 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2402 struct cifs_fid *fid)
2404 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2408 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2409 * the number of credits and return true. Otherwise - return false.
2412 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
2414 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
2415 int scredits, in_flight;
2417 if (shdr->Status != STATUS_PENDING)
2420 if (shdr->CreditRequest) {
2421 spin_lock(&server->req_lock);
2422 server->credits += le16_to_cpu(shdr->CreditRequest);
2423 scredits = server->credits;
2424 in_flight = server->in_flight;
2425 spin_unlock(&server->req_lock);
2426 wake_up(&server->request_q);
2428 trace_smb3_pend_credits(server->CurrentMid,
2429 server->conn_id, server->hostname, scredits,
2430 le16_to_cpu(shdr->CreditRequest), in_flight);
2431 cifs_dbg(FYI, "%s: status pending add %u credits total=%d\n",
2432 __func__, le16_to_cpu(shdr->CreditRequest), scredits);
2439 smb2_is_session_expired(char *buf)
2441 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
2443 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2444 shdr->Status != STATUS_USER_SESSION_DELETED)
2447 trace_smb3_ses_expired(le32_to_cpu(shdr->Id.SyncId.TreeId),
2448 le64_to_cpu(shdr->SessionId),
2449 le16_to_cpu(shdr->Command),
2450 le64_to_cpu(shdr->MessageId));
2451 cifs_dbg(FYI, "Session expired or deleted\n");
2457 smb2_is_status_io_timeout(char *buf)
2459 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
2461 if (shdr->Status == STATUS_IO_TIMEOUT)
2468 smb2_is_network_name_deleted(char *buf, struct TCP_Server_Info *server)
2470 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
2471 struct TCP_Server_Info *pserver;
2472 struct cifs_ses *ses;
2473 struct cifs_tcon *tcon;
2475 if (shdr->Status != STATUS_NETWORK_NAME_DELETED)
2478 /* If server is a channel, select the primary channel */
2479 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
2481 spin_lock(&cifs_tcp_ses_lock);
2482 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
2483 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2484 if (tcon->tid == le32_to_cpu(shdr->Id.SyncId.TreeId)) {
2485 spin_lock(&tcon->tc_lock);
2486 tcon->need_reconnect = true;
2487 spin_unlock(&tcon->tc_lock);
2488 spin_unlock(&cifs_tcp_ses_lock);
2489 pr_warn_once("Server share %s deleted.\n",
2495 spin_unlock(&cifs_tcp_ses_lock);
2501 smb2_oplock_response(struct cifs_tcon *tcon, __u64 persistent_fid,
2502 __u64 volatile_fid, __u16 net_fid, struct cifsInodeInfo *cinode)
2504 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2505 return SMB2_lease_break(0, tcon, cinode->lease_key,
2506 smb2_get_lease_state(cinode));
2508 return SMB2_oplock_break(0, tcon, persistent_fid, volatile_fid,
2509 CIFS_CACHE_READ(cinode) ? 1 : 0);
2513 smb2_set_replay(struct TCP_Server_Info *server, struct smb_rqst *rqst)
2515 struct smb2_hdr *shdr;
2517 if (server->dialect < SMB30_PROT_ID)
2520 shdr = (struct smb2_hdr *)(rqst->rq_iov[0].iov_base);
2522 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2525 shdr->Flags |= SMB2_FLAGS_REPLAY_OPERATION;
2529 smb2_set_related(struct smb_rqst *rqst)
2531 struct smb2_hdr *shdr;
2533 shdr = (struct smb2_hdr *)(rqst->rq_iov[0].iov_base);
2535 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2538 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2541 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2544 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2546 struct smb2_hdr *shdr;
2547 struct cifs_ses *ses = tcon->ses;
2548 struct TCP_Server_Info *server = ses->server;
2549 unsigned long len = smb_rqst_len(server, rqst);
2552 shdr = (struct smb2_hdr *)(rqst->rq_iov[0].iov_base);
2554 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2558 /* SMB headers in a compound are 8 byte aligned. */
2560 /* No padding needed */
2564 num_padding = 8 - (len & 7);
2565 if (!smb3_encryption_required(tcon)) {
2567 * If we do not have encryption then we can just add an extra
2568 * iov for the padding.
2570 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2571 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2576 * We can not add a small padding iov for the encryption case
2577 * because the encryption framework can not handle the padding
2579 * We have to flatten this into a single buffer and add
2580 * the padding to it.
2582 for (i = 1; i < rqst->rq_nvec; i++) {
2583 memcpy(rqst->rq_iov[0].iov_base +
2584 rqst->rq_iov[0].iov_len,
2585 rqst->rq_iov[i].iov_base,
2586 rqst->rq_iov[i].iov_len);
2587 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2589 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2591 rqst->rq_iov[0].iov_len += num_padding;
2597 shdr->NextCommand = cpu_to_le32(len);
2601 * helper function for exponential backoff and check if replayable
2603 bool smb2_should_replay(struct cifs_tcon *tcon,
2607 if (!pretries || !pcur_sleep)
2610 if (tcon->retry || (*pretries)++ < tcon->ses->server->retrans) {
2611 msleep(*pcur_sleep);
2612 (*pcur_sleep) = ((*pcur_sleep) << 1);
2613 if ((*pcur_sleep) > CIFS_MAX_SLEEP)
2614 (*pcur_sleep) = CIFS_MAX_SLEEP;
2622 * Passes the query info response back to the caller on success.
2623 * Caller need to free this with free_rsp_buf().
2626 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2627 const char *path, u32 desired_access,
2628 u32 class, u32 type, u32 output_len,
2629 struct kvec *rsp, int *buftype,
2630 struct cifs_sb_info *cifs_sb)
2632 struct smb2_compound_vars *vars;
2633 struct cifs_ses *ses = tcon->ses;
2634 struct TCP_Server_Info *server;
2635 int flags = CIFS_CP_CREATE_CLOSE_OP;
2636 struct smb_rqst *rqst;
2637 int resp_buftype[3];
2638 struct kvec *rsp_iov;
2639 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2640 struct cifs_open_parms oparms;
2641 struct cifs_fid fid;
2644 struct cached_fid *cfid = NULL;
2645 int retries = 0, cur_sleep = 1;
2648 /* reinitialize for possible replay */
2649 flags = CIFS_CP_CREATE_CLOSE_OP;
2650 oplock = SMB2_OPLOCK_LEVEL_NONE;
2651 server = cifs_pick_channel(ses);
2655 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2659 if (smb3_encryption_required(tcon))
2660 flags |= CIFS_TRANSFORM_REQ;
2662 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2663 vars = kzalloc(sizeof(*vars), GFP_KERNEL);
2669 rsp_iov = vars->rsp_iov;
2672 * We can only call this for things we know are directories.
2674 if (!strcmp(path, ""))
2675 open_cached_dir(xid, tcon, path, cifs_sb, false,
2676 &cfid); /* cfid null if open dir failed */
2678 rqst[0].rq_iov = vars->open_iov;
2679 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2681 oparms = (struct cifs_open_parms) {
2684 .desired_access = desired_access,
2685 .disposition = FILE_OPEN,
2686 .create_options = cifs_create_options(cifs_sb, 0),
2688 .replay = !!(retries),
2691 rc = SMB2_open_init(tcon, server,
2692 &rqst[0], &oplock, &oparms, utf16_path);
2695 smb2_set_next_command(tcon, &rqst[0]);
2697 rqst[1].rq_iov = &vars->qi_iov;
2698 rqst[1].rq_nvec = 1;
2701 rc = SMB2_query_info_init(tcon, server,
2703 cfid->fid.persistent_fid,
2704 cfid->fid.volatile_fid,
2709 rc = SMB2_query_info_init(tcon, server,
2720 smb2_set_next_command(tcon, &rqst[1]);
2721 smb2_set_related(&rqst[1]);
2724 rqst[2].rq_iov = &vars->close_iov;
2725 rqst[2].rq_nvec = 1;
2727 rc = SMB2_close_init(tcon, server,
2728 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2731 smb2_set_related(&rqst[2]);
2735 smb2_set_replay(server, &rqst[0]);
2736 smb2_set_replay(server, &rqst[2]);
2738 smb2_set_replay(server, &rqst[1]);
2742 rc = compound_send_recv(xid, ses, server,
2744 &resp_buftype[1], &rsp_iov[1]);
2746 rc = compound_send_recv(xid, ses, server,
2748 resp_buftype, rsp_iov);
2751 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2752 if (rc == -EREMCHG) {
2753 tcon->need_reconnect = true;
2754 pr_warn_once("server share %s deleted\n",
2760 *buftype = resp_buftype[1];
2763 SMB2_open_free(&rqst[0]);
2764 SMB2_query_info_free(&rqst[1]);
2765 SMB2_close_free(&rqst[2]);
2766 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2767 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2769 close_cached_dir(cfid);
2774 if (is_replayable_error(rc) &&
2775 smb2_should_replay(tcon, &retries, &cur_sleep))
2782 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2783 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2785 struct smb2_query_info_rsp *rsp;
2786 struct smb2_fs_full_size_info *info = NULL;
2787 struct kvec rsp_iov = {NULL, 0};
2788 int buftype = CIFS_NO_BUFFER;
2792 rc = smb2_query_info_compound(xid, tcon, "",
2793 FILE_READ_ATTRIBUTES,
2794 FS_FULL_SIZE_INFORMATION,
2795 SMB2_O_INFO_FILESYSTEM,
2796 sizeof(struct smb2_fs_full_size_info),
2797 &rsp_iov, &buftype, cifs_sb);
2801 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2802 buf->f_type = SMB2_SUPER_MAGIC;
2803 info = (struct smb2_fs_full_size_info *)(
2804 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2805 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2806 le32_to_cpu(rsp->OutputBufferLength),
2808 sizeof(struct smb2_fs_full_size_info));
2810 smb2_copy_fs_info_to_kstatfs(info, buf);
2813 trace_smb3_qfs_done(xid, tcon->tid, tcon->ses->Suid, tcon->tree_name, rc);
2814 free_rsp_buf(buftype, rsp_iov.iov_base);
2819 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2820 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2823 __le16 srch_path = 0; /* Null - open root of share */
2824 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2825 struct cifs_open_parms oparms;
2826 struct cifs_fid fid;
2828 if (!tcon->posix_extensions)
2829 return smb2_queryfs(xid, tcon, cifs_sb, buf);
2831 oparms = (struct cifs_open_parms) {
2834 .desired_access = FILE_READ_ATTRIBUTES,
2835 .disposition = FILE_OPEN,
2836 .create_options = cifs_create_options(cifs_sb, 0),
2840 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
2845 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2846 fid.volatile_fid, buf);
2847 buf->f_type = SMB2_SUPER_MAGIC;
2848 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2853 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2855 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2856 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2860 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2861 __u64 length, __u32 type, int lock, int unlock, bool wait)
2863 if (unlock && !lock)
2864 type = SMB2_LOCKFLAG_UNLOCK;
2865 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2866 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2867 current->tgid, length, offset, type, wait);
2871 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2873 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2877 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2879 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2883 smb2_new_lease_key(struct cifs_fid *fid)
2885 generate_random_uuid(fid->lease_key);
2889 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2890 const char *search_name,
2891 struct dfs_info3_param **target_nodes,
2892 unsigned int *num_of_nodes,
2893 const struct nls_table *nls_codepage, int remap)
2896 __le16 *utf16_path = NULL;
2897 int utf16_path_len = 0;
2898 struct cifs_tcon *tcon;
2899 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2900 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2901 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2902 int retry_count = 0;
2904 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
2907 * Try to use the IPC tcon, otherwise just use any
2909 tcon = ses->tcon_ipc;
2911 spin_lock(&cifs_tcp_ses_lock);
2912 tcon = list_first_entry_or_null(&ses->tcon_list,
2917 spin_unlock(&cifs_tcp_ses_lock);
2921 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2927 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2929 nls_codepage, remap);
2935 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2936 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2942 /* Highest DFS referral version understood */
2943 dfs_req->MaxReferralLevel = DFS_VERSION;
2945 /* Path to resolve in an UTF-16 null-terminated string */
2946 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2949 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2950 FSCTL_DFS_GET_REFERRALS,
2951 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
2952 (char **)&dfs_rsp, &dfs_rsp_size);
2953 if (!is_retryable_error(rc))
2955 usleep_range(512, 2048);
2956 } while (++retry_count < 5);
2958 if (!rc && !dfs_rsp)
2961 if (!is_retryable_error(rc) && rc != -ENOENT && rc != -EOPNOTSUPP)
2962 cifs_tcon_dbg(VFS, "%s: ioctl error: rc=%d\n", __func__, rc);
2966 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2967 num_of_nodes, target_nodes,
2968 nls_codepage, remap, search_name,
2969 true /* is_unicode */);
2971 cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
2976 if (tcon && !tcon->ipc) {
2977 /* ipc tcons are not refcounted */
2978 spin_lock(&cifs_tcp_ses_lock);
2980 /* tc_count can never go negative */
2981 WARN_ON(tcon->tc_count < 0);
2982 spin_unlock(&cifs_tcp_ses_lock);
2990 static struct cifs_ntsd *
2991 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
2992 const struct cifs_fid *cifsfid, u32 *pacllen, u32 info)
2994 struct cifs_ntsd *pntsd = NULL;
2996 int rc = -EOPNOTSUPP;
2997 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3000 return ERR_CAST(tlink);
3003 cifs_dbg(FYI, "trying to get acl\n");
3005 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
3006 cifsfid->volatile_fid, (void **)&pntsd, pacllen,
3010 cifs_put_tlink(tlink);
3012 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3019 static struct cifs_ntsd *
3020 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
3021 const char *path, u32 *pacllen, u32 info)
3023 struct cifs_ntsd *pntsd = NULL;
3024 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3027 struct cifs_tcon *tcon;
3028 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3029 struct cifs_fid fid;
3030 struct cifs_open_parms oparms;
3033 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
3035 return ERR_CAST(tlink);
3037 tcon = tlink_tcon(tlink);
3040 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3047 oparms = (struct cifs_open_parms) {
3050 .desired_access = READ_CONTROL,
3051 .disposition = FILE_OPEN,
3053 * When querying an ACL, even if the file is a symlink
3054 * we want to open the source not the target, and so
3055 * the protocol requires that the client specify this
3056 * flag when opening a reparse point
3058 .create_options = cifs_create_options(cifs_sb, 0) |
3063 if (info & SACL_SECINFO)
3064 oparms.desired_access |= SYSTEM_SECURITY;
3066 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
3070 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3071 fid.volatile_fid, (void **)&pntsd, pacllen,
3073 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3076 cifs_put_tlink(tlink);
3079 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3086 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
3087 struct inode *inode, const char *path, int aclflag)
3089 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3091 int rc, access_flags = 0;
3092 struct cifs_tcon *tcon;
3093 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3094 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3095 struct cifs_fid fid;
3096 struct cifs_open_parms oparms;
3099 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
3101 return PTR_ERR(tlink);
3103 tcon = tlink_tcon(tlink);
3106 if (aclflag & CIFS_ACL_OWNER || aclflag & CIFS_ACL_GROUP)
3107 access_flags |= WRITE_OWNER;
3108 if (aclflag & CIFS_ACL_SACL)
3109 access_flags |= SYSTEM_SECURITY;
3110 if (aclflag & CIFS_ACL_DACL)
3111 access_flags |= WRITE_DAC;
3113 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3120 oparms = (struct cifs_open_parms) {
3122 .desired_access = access_flags,
3123 .create_options = cifs_create_options(cifs_sb, 0),
3124 .disposition = FILE_OPEN,
3129 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
3133 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3134 fid.volatile_fid, pnntsd, acllen, aclflag);
3135 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3138 cifs_put_tlink(tlink);
3143 /* Retrieve an ACL from the server */
3144 static struct cifs_ntsd *
3145 get_smb2_acl(struct cifs_sb_info *cifs_sb,
3146 struct inode *inode, const char *path,
3147 u32 *pacllen, u32 info)
3149 struct cifs_ntsd *pntsd = NULL;
3150 struct cifsFileInfo *open_file = NULL;
3152 if (inode && !(info & SACL_SECINFO))
3153 open_file = find_readable_file(CIFS_I(inode), true);
3154 if (!open_file || (info & SACL_SECINFO))
3155 return get_smb2_acl_by_path(cifs_sb, path, pacllen, info);
3157 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen, info);
3158 cifsFileInfo_put(open_file);
3162 static long smb3_zero_data(struct file *file, struct cifs_tcon *tcon,
3163 loff_t offset, loff_t len, unsigned int xid)
3165 struct cifsFileInfo *cfile = file->private_data;
3166 struct file_zero_data_information fsctl_buf;
3168 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3170 fsctl_buf.FileOffset = cpu_to_le64(offset);
3171 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3173 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3174 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3176 sizeof(struct file_zero_data_information),
3180 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3181 loff_t offset, loff_t len, bool keep_size)
3183 struct cifs_ses *ses = tcon->ses;
3184 struct inode *inode = file_inode(file);
3185 struct cifsInodeInfo *cifsi = CIFS_I(inode);
3186 struct cifsFileInfo *cfile = file->private_data;
3187 unsigned long long new_size;
3193 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3194 ses->Suid, offset, len);
3197 filemap_invalidate_lock(inode->i_mapping);
3200 * We zero the range through ioctl, so we need remove the page caches
3201 * first, otherwise the data may be inconsistent with the server.
3203 truncate_pagecache_range(inode, offset, offset + len - 1);
3205 /* if file not oplocked can't be sure whether asking to extend size */
3207 if (keep_size == false && !CIFS_CACHE_READ(cifsi))
3208 goto zero_range_exit;
3210 rc = smb3_zero_data(file, tcon, offset, len, xid);
3212 goto zero_range_exit;
3215 * do we also need to change the size of the file?
3217 new_size = offset + len;
3218 if (keep_size == false && (unsigned long long)i_size_read(inode) < new_size) {
3219 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3220 cfile->fid.volatile_fid, cfile->pid, new_size);
3222 truncate_setsize(inode, new_size);
3223 netfs_resize_file(&cifsi->netfs, new_size, true);
3224 if (offset < cifsi->netfs.zero_point)
3225 cifsi->netfs.zero_point = offset;
3226 fscache_resize_cookie(cifs_inode_cookie(inode), new_size);
3231 filemap_invalidate_unlock(inode->i_mapping);
3232 inode_unlock(inode);
3235 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3236 ses->Suid, offset, len, rc);
3238 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3239 ses->Suid, offset, len);
3243 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3244 loff_t offset, loff_t len)
3246 struct inode *inode = file_inode(file);
3247 struct cifsFileInfo *cfile = file->private_data;
3248 struct file_zero_data_information fsctl_buf;
3251 __u8 set_sparse = 1;
3256 /* Need to make file sparse, if not already, before freeing range. */
3257 /* Consider adding equivalent for compressed since it could also work */
3258 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3263 filemap_invalidate_lock(inode->i_mapping);
3265 * We implement the punch hole through ioctl, so we need remove the page
3266 * caches first, otherwise the data may be inconsistent with the server.
3268 truncate_pagecache_range(inode, offset, offset + len - 1);
3270 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3272 fsctl_buf.FileOffset = cpu_to_le64(offset);
3273 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3275 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3276 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3278 sizeof(struct file_zero_data_information),
3279 CIFSMaxBufSize, NULL, NULL);
3280 filemap_invalidate_unlock(inode->i_mapping);
3282 inode_unlock(inode);
3287 static int smb3_simple_fallocate_write_range(unsigned int xid,
3288 struct cifs_tcon *tcon,
3289 struct cifsFileInfo *cfile,
3290 loff_t off, loff_t len,
3293 struct cifs_io_parms io_parms = {0};
3298 io_parms.netfid = cfile->fid.netfid;
3299 io_parms.pid = current->tgid;
3300 io_parms.tcon = tcon;
3301 io_parms.persistent_fid = cfile->fid.persistent_fid;
3302 io_parms.volatile_fid = cfile->fid.volatile_fid;
3305 io_parms.offset = off;
3306 io_parms.length = len;
3307 if (io_parms.length > SMB2_MAX_BUFFER_SIZE)
3308 io_parms.length = SMB2_MAX_BUFFER_SIZE;
3309 /* iov[0] is reserved for smb header */
3310 iov[1].iov_base = buf;
3311 iov[1].iov_len = io_parms.length;
3312 rc = SMB2_write(xid, &io_parms, &nbytes, iov, 1);
3324 static int smb3_simple_fallocate_range(unsigned int xid,
3325 struct cifs_tcon *tcon,
3326 struct cifsFileInfo *cfile,
3327 loff_t off, loff_t len)
3329 struct file_allocated_range_buffer in_data, *out_data = NULL, *tmp_data;
3335 in_data.file_offset = cpu_to_le64(off);
3336 in_data.length = cpu_to_le64(len);
3337 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3338 cfile->fid.volatile_fid,
3339 FSCTL_QUERY_ALLOCATED_RANGES,
3340 (char *)&in_data, sizeof(in_data),
3341 1024 * sizeof(struct file_allocated_range_buffer),
3342 (char **)&out_data, &out_data_len);
3346 buf = kzalloc(1024 * 1024, GFP_KERNEL);
3352 tmp_data = out_data;
3355 * The rest of the region is unmapped so write it all.
3357 if (out_data_len == 0) {
3358 rc = smb3_simple_fallocate_write_range(xid, tcon,
3359 cfile, off, len, buf);
3363 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3368 if (off < le64_to_cpu(tmp_data->file_offset)) {
3370 * We are at a hole. Write until the end of the region
3371 * or until the next allocated data,
3372 * whichever comes next.
3374 l = le64_to_cpu(tmp_data->file_offset) - off;
3377 rc = smb3_simple_fallocate_write_range(xid, tcon,
3378 cfile, off, l, buf);
3387 * We are at a section of allocated data, just skip forward
3388 * until the end of the data or the end of the region
3389 * we are supposed to fallocate, whichever comes first.
3391 l = le64_to_cpu(tmp_data->length);
3397 tmp_data = &tmp_data[1];
3398 out_data_len -= sizeof(struct file_allocated_range_buffer);
3408 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3409 loff_t off, loff_t len, bool keep_size)
3411 struct inode *inode;
3412 struct cifsInodeInfo *cifsi;
3413 struct cifsFileInfo *cfile = file->private_data;
3414 long rc = -EOPNOTSUPP;
3420 inode = d_inode(cfile->dentry);
3421 cifsi = CIFS_I(inode);
3423 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3424 tcon->ses->Suid, off, len);
3425 /* if file not oplocked can't be sure whether asking to extend size */
3426 if (!CIFS_CACHE_READ(cifsi))
3427 if (keep_size == false) {
3428 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3429 tcon->tid, tcon->ses->Suid, off, len, rc);
3435 * Extending the file
3437 if ((keep_size == false) && i_size_read(inode) < off + len) {
3438 rc = inode_newsize_ok(inode, off + len);
3442 if (cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)
3443 smb2_set_sparse(xid, tcon, cfile, inode, false);
3445 new_eof = off + len;
3446 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3447 cfile->fid.volatile_fid, cfile->pid, new_eof);
3449 netfs_resize_file(&cifsi->netfs, new_eof, true);
3450 cifs_setsize(inode, new_eof);
3451 cifs_truncate_page(inode->i_mapping, inode->i_size);
3452 truncate_setsize(inode, new_eof);
3458 * Files are non-sparse by default so falloc may be a no-op
3459 * Must check if file sparse. If not sparse, and since we are not
3460 * extending then no need to do anything since file already allocated
3462 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
3467 if (keep_size == true) {
3469 * We can not preallocate pages beyond the end of the file
3472 if (off >= i_size_read(inode)) {
3477 * For fallocates that are partially beyond the end of file,
3478 * clamp len so we only fallocate up to the end of file.
3480 if (off + len > i_size_read(inode)) {
3481 len = i_size_read(inode) - off;
3485 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3487 * At this point, we are trying to fallocate an internal
3488 * regions of a sparse file. Since smb2 does not have a
3489 * fallocate command we have two otions on how to emulate this.
3490 * We can either turn the entire file to become non-sparse
3491 * which we only do if the fallocate is for virtually
3492 * the whole file, or we can overwrite the region with zeroes
3493 * using SMB2_write, which could be prohibitevly expensive
3497 * We are only trying to fallocate a small region so
3498 * just write it with zero.
3500 if (len <= 1024 * 1024) {
3501 rc = smb3_simple_fallocate_range(xid, tcon, cfile,
3507 * Check if falloc starts within first few pages of file
3508 * and ends within a few pages of the end of file to
3509 * ensure that most of file is being forced to be
3510 * fallocated now. If so then setting whole file sparse
3511 * ie potentially making a few extra pages at the beginning
3512 * or end of the file non-sparse via set_sparse is harmless.
3514 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3520 smb2_set_sparse(xid, tcon, cfile, inode, false);
3525 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3526 tcon->ses->Suid, off, len, rc);
3528 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3529 tcon->ses->Suid, off, len);
3535 static long smb3_collapse_range(struct file *file, struct cifs_tcon *tcon,
3536 loff_t off, loff_t len)
3540 struct inode *inode = file_inode(file);
3541 struct cifsInodeInfo *cifsi = CIFS_I(inode);
3542 struct cifsFileInfo *cfile = file->private_data;
3543 struct netfs_inode *ictx = &cifsi->netfs;
3544 loff_t old_eof, new_eof;
3550 old_eof = i_size_read(inode);
3551 if ((off >= old_eof) ||
3552 off + len >= old_eof) {
3557 filemap_invalidate_lock(inode->i_mapping);
3558 rc = filemap_write_and_wait_range(inode->i_mapping, off, old_eof - 1);
3562 truncate_pagecache_range(inode, off, old_eof);
3563 ictx->zero_point = old_eof;
3565 rc = smb2_copychunk_range(xid, cfile, cfile, off + len,
3566 old_eof - off - len, off);
3570 new_eof = old_eof - len;
3571 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3572 cfile->fid.volatile_fid, cfile->pid, new_eof);
3578 truncate_setsize(inode, new_eof);
3579 netfs_resize_file(&cifsi->netfs, new_eof, true);
3580 ictx->zero_point = new_eof;
3581 fscache_resize_cookie(cifs_inode_cookie(inode), new_eof);
3583 filemap_invalidate_unlock(inode->i_mapping);
3585 inode_unlock(inode);
3590 static long smb3_insert_range(struct file *file, struct cifs_tcon *tcon,
3591 loff_t off, loff_t len)
3595 struct cifsFileInfo *cfile = file->private_data;
3596 struct inode *inode = file_inode(file);
3597 struct cifsInodeInfo *cifsi = CIFS_I(inode);
3598 __u64 count, old_eof, new_eof;
3604 old_eof = i_size_read(inode);
3605 if (off >= old_eof) {
3610 count = old_eof - off;
3611 new_eof = old_eof + len;
3613 filemap_invalidate_lock(inode->i_mapping);
3614 rc = filemap_write_and_wait_range(inode->i_mapping, off, new_eof - 1);
3617 truncate_pagecache_range(inode, off, old_eof);
3619 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3620 cfile->fid.volatile_fid, cfile->pid, new_eof);
3624 truncate_setsize(inode, new_eof);
3625 netfs_resize_file(&cifsi->netfs, i_size_read(inode), true);
3626 fscache_resize_cookie(cifs_inode_cookie(inode), i_size_read(inode));
3628 rc = smb2_copychunk_range(xid, cfile, cfile, off, count, off + len);
3632 rc = smb3_zero_data(file, tcon, off, len, xid);
3638 filemap_invalidate_unlock(inode->i_mapping);
3640 inode_unlock(inode);
3645 static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3647 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3648 struct cifsInodeInfo *cifsi;
3649 struct inode *inode;
3651 struct file_allocated_range_buffer in_data, *out_data = NULL;
3655 if (whence != SEEK_HOLE && whence != SEEK_DATA)
3656 return generic_file_llseek(file, offset, whence);
3658 inode = d_inode(cfile->dentry);
3659 cifsi = CIFS_I(inode);
3661 if (offset < 0 || offset >= i_size_read(inode))
3666 * We need to be sure that all dirty pages are written as they
3667 * might fill holes on the server.
3668 * Note that we also MUST flush any written pages since at least
3669 * some servers (Windows2016) will not reflect recent writes in
3670 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3672 wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
3674 filemap_write_and_wait(inode->i_mapping);
3675 smb2_flush_file(xid, tcon, &wrcfile->fid);
3676 cifsFileInfo_put(wrcfile);
3679 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3680 if (whence == SEEK_HOLE)
3681 offset = i_size_read(inode);
3685 in_data.file_offset = cpu_to_le64(offset);
3686 in_data.length = cpu_to_le64(i_size_read(inode));
3688 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3689 cfile->fid.volatile_fid,
3690 FSCTL_QUERY_ALLOCATED_RANGES,
3691 (char *)&in_data, sizeof(in_data),
3692 sizeof(struct file_allocated_range_buffer),
3693 (char **)&out_data, &out_data_len);
3699 if (whence == SEEK_HOLE && out_data_len == 0)
3702 if (whence == SEEK_DATA && out_data_len == 0) {
3707 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3711 if (whence == SEEK_DATA) {
3712 offset = le64_to_cpu(out_data->file_offset);
3715 if (offset < le64_to_cpu(out_data->file_offset))
3718 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3724 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3729 static int smb3_fiemap(struct cifs_tcon *tcon,
3730 struct cifsFileInfo *cfile,
3731 struct fiemap_extent_info *fei, u64 start, u64 len)
3734 struct file_allocated_range_buffer in_data, *out_data;
3736 int i, num, rc, flags, last_blob;
3739 rc = fiemap_prep(d_inode(cfile->dentry), fei, start, &len, 0);
3745 in_data.file_offset = cpu_to_le64(start);
3746 in_data.length = cpu_to_le64(len);
3748 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3749 cfile->fid.volatile_fid,
3750 FSCTL_QUERY_ALLOCATED_RANGES,
3751 (char *)&in_data, sizeof(in_data),
3752 1024 * sizeof(struct file_allocated_range_buffer),
3753 (char **)&out_data, &out_data_len);
3762 if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) {
3766 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3771 num = out_data_len / sizeof(struct file_allocated_range_buffer);
3772 for (i = 0; i < num; i++) {
3774 if (i == num - 1 && last_blob)
3775 flags |= FIEMAP_EXTENT_LAST;
3777 rc = fiemap_fill_next_extent(fei,
3778 le64_to_cpu(out_data[i].file_offset),
3779 le64_to_cpu(out_data[i].file_offset),
3780 le64_to_cpu(out_data[i].length),
3791 next = le64_to_cpu(out_data[num - 1].file_offset) +
3792 le64_to_cpu(out_data[num - 1].length);
3793 len = len - (next - start);
3804 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3805 loff_t off, loff_t len)
3807 /* KEEP_SIZE already checked for by do_fallocate */
3808 if (mode & FALLOC_FL_PUNCH_HOLE)
3809 return smb3_punch_hole(file, tcon, off, len);
3810 else if (mode & FALLOC_FL_ZERO_RANGE) {
3811 if (mode & FALLOC_FL_KEEP_SIZE)
3812 return smb3_zero_range(file, tcon, off, len, true);
3813 return smb3_zero_range(file, tcon, off, len, false);
3814 } else if (mode == FALLOC_FL_KEEP_SIZE)
3815 return smb3_simple_falloc(file, tcon, off, len, true);
3816 else if (mode == FALLOC_FL_COLLAPSE_RANGE)
3817 return smb3_collapse_range(file, tcon, off, len);
3818 else if (mode == FALLOC_FL_INSERT_RANGE)
3819 return smb3_insert_range(file, tcon, off, len);
3821 return smb3_simple_falloc(file, tcon, off, len, false);
3827 smb2_downgrade_oplock(struct TCP_Server_Info *server,
3828 struct cifsInodeInfo *cinode, __u32 oplock,
3829 unsigned int epoch, bool *purge_cache)
3831 server->ops->set_oplock_level(cinode, oplock, 0, NULL);
3835 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3836 unsigned int epoch, bool *purge_cache);
3839 smb3_downgrade_oplock(struct TCP_Server_Info *server,
3840 struct cifsInodeInfo *cinode, __u32 oplock,
3841 unsigned int epoch, bool *purge_cache)
3843 unsigned int old_state = cinode->oplock;
3844 unsigned int old_epoch = cinode->epoch;
3845 unsigned int new_state;
3847 if (epoch > old_epoch) {
3848 smb21_set_oplock_level(cinode, oplock, 0, NULL);
3849 cinode->epoch = epoch;
3852 new_state = cinode->oplock;
3853 *purge_cache = false;
3855 if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
3856 (new_state & CIFS_CACHE_READ_FLG) == 0)
3857 *purge_cache = true;
3858 else if (old_state == new_state && (epoch - old_epoch > 1))
3859 *purge_cache = true;
3863 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3864 unsigned int epoch, bool *purge_cache)
3867 cinode->lease_granted = false;
3868 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3870 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3871 cinode->oplock = CIFS_CACHE_RHW_FLG;
3872 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3873 &cinode->netfs.inode);
3874 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
3875 cinode->oplock = CIFS_CACHE_RW_FLG;
3876 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3877 &cinode->netfs.inode);
3878 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3879 cinode->oplock = CIFS_CACHE_READ_FLG;
3880 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3881 &cinode->netfs.inode);
3887 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3888 unsigned int epoch, bool *purge_cache)
3890 char message[5] = {0};
3891 unsigned int new_oplock = 0;
3894 cinode->lease_granted = true;
3895 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3898 /* Check if the server granted an oplock rather than a lease */
3899 if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3900 return smb2_set_oplock_level(cinode, oplock, epoch,
3903 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
3904 new_oplock |= CIFS_CACHE_READ_FLG;
3905 strcat(message, "R");
3907 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
3908 new_oplock |= CIFS_CACHE_HANDLE_FLG;
3909 strcat(message, "H");
3911 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
3912 new_oplock |= CIFS_CACHE_WRITE_FLG;
3913 strcat(message, "W");
3916 strncpy(message, "None", sizeof(message));
3918 cinode->oplock = new_oplock;
3919 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3920 &cinode->netfs.inode);
3924 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3925 unsigned int epoch, bool *purge_cache)
3927 unsigned int old_oplock = cinode->oplock;
3929 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3932 *purge_cache = false;
3933 if (old_oplock == CIFS_CACHE_READ_FLG) {
3934 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3935 (epoch - cinode->epoch > 0))
3936 *purge_cache = true;
3937 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3938 (epoch - cinode->epoch > 1))
3939 *purge_cache = true;
3940 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3941 (epoch - cinode->epoch > 1))
3942 *purge_cache = true;
3943 else if (cinode->oplock == 0 &&
3944 (epoch - cinode->epoch > 0))
3945 *purge_cache = true;
3946 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
3947 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3948 (epoch - cinode->epoch > 0))
3949 *purge_cache = true;
3950 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3951 (epoch - cinode->epoch > 1))
3952 *purge_cache = true;
3954 cinode->epoch = epoch;
3958 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3960 smb2_is_read_op(__u32 oplock)
3962 return oplock == SMB2_OPLOCK_LEVEL_II;
3964 #endif /* CIFS_ALLOW_INSECURE_LEGACY */
3967 smb21_is_read_op(__u32 oplock)
3969 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
3970 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
3974 map_oplock_to_lease(u8 oplock)
3976 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3977 return SMB2_LEASE_WRITE_CACHING_LE | SMB2_LEASE_READ_CACHING_LE;
3978 else if (oplock == SMB2_OPLOCK_LEVEL_II)
3979 return SMB2_LEASE_READ_CACHING_LE;
3980 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3981 return SMB2_LEASE_HANDLE_CACHING_LE | SMB2_LEASE_READ_CACHING_LE |
3982 SMB2_LEASE_WRITE_CACHING_LE;
3987 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3989 struct create_lease *buf;
3991 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
3995 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3996 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3998 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3999 (struct create_lease, lcontext));
4000 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
4001 buf->ccontext.NameOffset = cpu_to_le16(offsetof
4002 (struct create_lease, Name));
4003 buf->ccontext.NameLength = cpu_to_le16(4);
4004 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4013 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
4015 struct create_lease_v2 *buf;
4017 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
4021 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
4022 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
4024 buf->ccontext.DataOffset = cpu_to_le16(offsetof
4025 (struct create_lease_v2, lcontext));
4026 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
4027 buf->ccontext.NameOffset = cpu_to_le16(offsetof
4028 (struct create_lease_v2, Name));
4029 buf->ccontext.NameLength = cpu_to_le16(4);
4030 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4039 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
4041 struct create_lease *lc = (struct create_lease *)buf;
4043 *epoch = 0; /* not used */
4044 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE)
4045 return SMB2_OPLOCK_LEVEL_NOCHANGE;
4046 return le32_to_cpu(lc->lcontext.LeaseState);
4050 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
4052 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
4054 *epoch = le16_to_cpu(lc->lcontext.Epoch);
4055 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE)
4056 return SMB2_OPLOCK_LEVEL_NOCHANGE;
4058 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
4059 return le32_to_cpu(lc->lcontext.LeaseState);
4063 smb2_wp_retry_size(struct inode *inode)
4065 return min_t(unsigned int, CIFS_SB(inode->i_sb)->ctx->wsize,
4066 SMB2_MAX_BUFFER_SIZE);
4070 smb2_dir_needs_close(struct cifsFileInfo *cfile)
4072 return !cfile->invalidHandle;
4076 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
4077 struct smb_rqst *old_rq, __le16 cipher_type)
4079 struct smb2_hdr *shdr =
4080 (struct smb2_hdr *)old_rq->rq_iov[0].iov_base;
4082 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
4083 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
4084 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
4085 tr_hdr->Flags = cpu_to_le16(0x01);
4086 if ((cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4087 (cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4088 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
4090 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
4091 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
4094 static void *smb2_aead_req_alloc(struct crypto_aead *tfm, const struct smb_rqst *rqst,
4095 int num_rqst, const u8 *sig, u8 **iv,
4096 struct aead_request **req, struct sg_table *sgt,
4097 unsigned int *num_sgs, size_t *sensitive_size)
4099 unsigned int req_size = sizeof(**req) + crypto_aead_reqsize(tfm);
4100 unsigned int iv_size = crypto_aead_ivsize(tfm);
4104 *num_sgs = cifs_get_num_sgs(rqst, num_rqst, sig);
4105 if (IS_ERR_VALUE((long)(int)*num_sgs))
4106 return ERR_PTR(*num_sgs);
4109 len += crypto_aead_alignmask(tfm) & ~(crypto_tfm_ctx_alignment() - 1);
4110 len = ALIGN(len, crypto_tfm_ctx_alignment());
4112 len = ALIGN(len, __alignof__(struct scatterlist));
4113 len += array_size(*num_sgs, sizeof(struct scatterlist));
4114 *sensitive_size = len;
4116 p = kvzalloc(len, GFP_NOFS);
4118 return ERR_PTR(-ENOMEM);
4120 *iv = (u8 *)PTR_ALIGN(p, crypto_aead_alignmask(tfm) + 1);
4121 *req = (struct aead_request *)PTR_ALIGN(*iv + iv_size,
4122 crypto_tfm_ctx_alignment());
4123 sgt->sgl = (struct scatterlist *)PTR_ALIGN((u8 *)*req + req_size,
4124 __alignof__(struct scatterlist));
4128 static void *smb2_get_aead_req(struct crypto_aead *tfm, struct smb_rqst *rqst,
4129 int num_rqst, const u8 *sig, u8 **iv,
4130 struct aead_request **req, struct scatterlist **sgl,
4131 size_t *sensitive_size)
4133 struct sg_table sgtable = {};
4134 unsigned int skip, num_sgs, i, j;
4138 p = smb2_aead_req_alloc(tfm, rqst, num_rqst, sig, iv, req, &sgtable,
4139 &num_sgs, sensitive_size);
4143 sg_init_marker(sgtable.sgl, num_sgs);
4146 * The first rqst has a transform header where the
4147 * first 20 bytes are not part of the encrypted blob.
4151 for (i = 0; i < num_rqst; i++) {
4152 struct iov_iter *iter = &rqst[i].rq_iter;
4153 size_t count = iov_iter_count(iter);
4155 for (j = 0; j < rqst[i].rq_nvec; j++) {
4156 cifs_sg_set_buf(&sgtable,
4157 rqst[i].rq_iov[j].iov_base + skip,
4158 rqst[i].rq_iov[j].iov_len - skip);
4160 /* See the above comment on the 'skip' assignment */
4163 sgtable.orig_nents = sgtable.nents;
4165 rc = extract_iter_to_sg(iter, count, &sgtable,
4166 num_sgs - sgtable.nents, 0);
4167 iov_iter_revert(iter, rc);
4168 sgtable.orig_nents = sgtable.nents;
4171 cifs_sg_set_buf(&sgtable, sig, SMB2_SIGNATURE_SIZE);
4172 sg_mark_end(&sgtable.sgl[sgtable.nents - 1]);
4178 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
4180 struct TCP_Server_Info *pserver;
4181 struct cifs_ses *ses;
4184 /* If server is a channel, select the primary channel */
4185 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
4187 spin_lock(&cifs_tcp_ses_lock);
4188 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
4189 if (ses->Suid == ses_id) {
4190 spin_lock(&ses->ses_lock);
4191 ses_enc_key = enc ? ses->smb3encryptionkey :
4192 ses->smb3decryptionkey;
4193 memcpy(key, ses_enc_key, SMB3_ENC_DEC_KEY_SIZE);
4194 spin_unlock(&ses->ses_lock);
4195 spin_unlock(&cifs_tcp_ses_lock);
4199 spin_unlock(&cifs_tcp_ses_lock);
4201 trace_smb3_ses_not_found(ses_id);
4206 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
4207 * iov[0] - transform header (associate data),
4208 * iov[1-N] - SMB2 header and pages - data to encrypt.
4209 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
4213 crypt_message(struct TCP_Server_Info *server, int num_rqst,
4214 struct smb_rqst *rqst, int enc)
4216 struct smb2_transform_hdr *tr_hdr =
4217 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
4218 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
4220 struct scatterlist *sg;
4221 u8 sign[SMB2_SIGNATURE_SIZE] = {};
4222 u8 key[SMB3_ENC_DEC_KEY_SIZE];
4223 struct aead_request *req;
4225 DECLARE_CRYPTO_WAIT(wait);
4226 struct crypto_aead *tfm;
4227 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4229 size_t sensitive_size;
4231 rc = smb2_get_enc_key(server, le64_to_cpu(tr_hdr->SessionId), enc, key);
4233 cifs_server_dbg(FYI, "%s: Could not get %scryption key. sid: 0x%llx\n", __func__,
4234 enc ? "en" : "de", le64_to_cpu(tr_hdr->SessionId));
4238 rc = smb3_crypto_aead_allocate(server);
4240 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
4244 tfm = enc ? server->secmech.enc : server->secmech.dec;
4246 if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
4247 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4248 rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
4250 rc = crypto_aead_setkey(tfm, key, SMB3_GCM128_CRYPTKEY_SIZE);
4253 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
4257 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
4259 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
4263 creq = smb2_get_aead_req(tfm, rqst, num_rqst, sign, &iv, &req, &sg,
4266 return PTR_ERR(creq);
4269 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
4270 crypt_len += SMB2_SIGNATURE_SIZE;
4273 if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4274 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4275 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
4278 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
4281 aead_request_set_tfm(req, tfm);
4282 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
4283 aead_request_set_ad(req, assoc_data_len);
4285 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
4286 crypto_req_done, &wait);
4288 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
4289 : crypto_aead_decrypt(req), &wait);
4292 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
4294 kvfree_sensitive(creq, sensitive_size);
4299 * Clear a read buffer, discarding the folios which have XA_MARK_0 set.
4301 static void cifs_clear_xarray_buffer(struct xarray *buffer)
4303 struct folio *folio;
4305 XA_STATE(xas, buffer, 0);
4308 xas_for_each_marked(&xas, folio, ULONG_MAX, XA_MARK_0) {
4316 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
4320 for (i = 0; i < num_rqst; i++)
4321 if (!xa_empty(&rqst[i].rq_buffer))
4322 cifs_clear_xarray_buffer(&rqst[i].rq_buffer);
4326 * This function will initialize new_rq and encrypt the content.
4327 * The first entry, new_rq[0], only contains a single iov which contains
4328 * a smb2_transform_hdr and is pre-allocated by the caller.
4329 * This function then populates new_rq[1+] with the content from olq_rq[0+].
4331 * The end result is an array of smb_rqst structures where the first structure
4332 * only contains a single iov for the transform header which we then can pass
4333 * to crypt_message().
4335 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
4336 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
4339 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
4340 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
4342 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
4344 unsigned int orig_len = 0;
4348 for (i = 1; i < num_rqst; i++) {
4349 struct smb_rqst *old = &old_rq[i - 1];
4350 struct smb_rqst *new = &new_rq[i];
4351 struct xarray *buffer = &new->rq_buffer;
4352 size_t size = iov_iter_count(&old->rq_iter), seg, copied = 0;
4354 orig_len += smb_rqst_len(server, old);
4355 new->rq_iov = old->rq_iov;
4356 new->rq_nvec = old->rq_nvec;
4361 unsigned int npages = DIV_ROUND_UP(size, PAGE_SIZE);
4363 for (j = 0; j < npages; j++) {
4367 page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4371 o = xa_store(buffer, j, page, GFP_KERNEL);
4378 xa_set_mark(buffer, j, XA_MARK_0);
4380 seg = min_t(size_t, size - copied, PAGE_SIZE);
4381 if (copy_page_from_iter(page, 0, seg, &old->rq_iter) != seg) {
4387 iov_iter_xarray(&new->rq_iter, ITER_SOURCE,
4389 new->rq_iter_size = size;
4393 /* fill the 1st iov with a transform header */
4394 fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
4396 rc = crypt_message(server, num_rqst, new_rq, 1);
4397 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
4404 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
4409 smb3_is_transform_hdr(void *buf)
4411 struct smb2_transform_hdr *trhdr = buf;
4413 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
4417 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
4418 unsigned int buf_data_size, struct iov_iter *iter,
4422 struct smb_rqst rqst = {NULL};
4423 size_t iter_size = 0;
4426 iov[0].iov_base = buf;
4427 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
4428 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
4429 iov[1].iov_len = buf_data_size;
4434 rqst.rq_iter = *iter;
4435 rqst.rq_iter_size = iov_iter_count(iter);
4436 iter_size = iov_iter_count(iter);
4439 rc = crypt_message(server, 1, &rqst, 0);
4440 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
4445 memmove(buf, iov[1].iov_base, buf_data_size);
4448 server->total_read = buf_data_size + iter_size;
4454 cifs_copy_pages_to_iter(struct xarray *pages, unsigned int data_size,
4455 unsigned int skip, struct iov_iter *iter)
4458 unsigned long index;
4460 xa_for_each(pages, index, page) {
4461 size_t n, len = min_t(unsigned int, PAGE_SIZE - skip, data_size);
4463 n = copy_page_to_iter(page, skip, len, iter);
4465 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4476 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4477 char *buf, unsigned int buf_len, struct xarray *pages,
4478 unsigned int pages_len, bool is_offloaded)
4480 unsigned int data_offset;
4481 unsigned int data_len;
4482 unsigned int cur_off;
4483 unsigned int cur_page_idx;
4484 unsigned int pad_len;
4485 struct cifs_readdata *rdata = mid->callback_data;
4486 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
4488 bool use_rdma_mr = false;
4490 if (shdr->Command != SMB2_READ) {
4491 cifs_server_dbg(VFS, "only big read responses are supported\n");
4495 if (server->ops->is_session_expired &&
4496 server->ops->is_session_expired(buf)) {
4498 cifs_reconnect(server, true);
4502 if (server->ops->is_status_pending &&
4503 server->ops->is_status_pending(buf, server))
4506 /* set up first two iov to get credits */
4507 rdata->iov[0].iov_base = buf;
4508 rdata->iov[0].iov_len = 0;
4509 rdata->iov[1].iov_base = buf;
4510 rdata->iov[1].iov_len =
4511 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
4512 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4513 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4514 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4515 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4517 rdata->result = server->ops->map_error(buf, true);
4518 if (rdata->result != 0) {
4519 cifs_dbg(FYI, "%s: server returned error %d\n",
4520 __func__, rdata->result);
4521 /* normal error on read response */
4523 mid->mid_state = MID_RESPONSE_RECEIVED;
4525 dequeue_mid(mid, false);
4529 data_offset = server->ops->read_data_offset(buf);
4530 #ifdef CONFIG_CIFS_SMB_DIRECT
4531 use_rdma_mr = rdata->mr;
4533 data_len = server->ops->read_data_length(buf, use_rdma_mr);
4535 if (data_offset < server->vals->read_rsp_size) {
4537 * win2k8 sometimes sends an offset of 0 when the read
4538 * is beyond the EOF. Treat it as if the data starts just after
4541 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4542 __func__, data_offset);
4543 data_offset = server->vals->read_rsp_size;
4544 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4545 /* data_offset is beyond the end of smallbuf */
4546 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4547 __func__, data_offset);
4548 rdata->result = -EIO;
4550 mid->mid_state = MID_RESPONSE_MALFORMED;
4552 dequeue_mid(mid, rdata->result);
4556 pad_len = data_offset - server->vals->read_rsp_size;
4558 if (buf_len <= data_offset) {
4559 /* read response payload is in pages */
4560 cur_page_idx = pad_len / PAGE_SIZE;
4561 cur_off = pad_len % PAGE_SIZE;
4563 if (cur_page_idx != 0) {
4564 /* data offset is beyond the 1st page of response */
4565 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4566 __func__, data_offset);
4567 rdata->result = -EIO;
4569 mid->mid_state = MID_RESPONSE_MALFORMED;
4571 dequeue_mid(mid, rdata->result);
4575 if (data_len > pages_len - pad_len) {
4576 /* data_len is corrupt -- discard frame */
4577 rdata->result = -EIO;
4579 mid->mid_state = MID_RESPONSE_MALFORMED;
4581 dequeue_mid(mid, rdata->result);
4585 /* Copy the data to the output I/O iterator. */
4586 rdata->result = cifs_copy_pages_to_iter(pages, pages_len,
4587 cur_off, &rdata->iter);
4588 if (rdata->result != 0) {
4590 mid->mid_state = MID_RESPONSE_MALFORMED;
4592 dequeue_mid(mid, rdata->result);
4595 rdata->got_bytes = pages_len;
4597 } else if (buf_len >= data_offset + data_len) {
4598 /* read response payload is in buf */
4599 WARN_ONCE(pages && !xa_empty(pages),
4600 "read data can be either in buf or in pages");
4601 length = copy_to_iter(buf + data_offset, data_len, &rdata->iter);
4604 rdata->got_bytes = data_len;
4606 /* read response payload cannot be in both buf and pages */
4607 WARN_ONCE(1, "buf can not contain only a part of read data");
4608 rdata->result = -EIO;
4610 mid->mid_state = MID_RESPONSE_MALFORMED;
4612 dequeue_mid(mid, rdata->result);
4617 mid->mid_state = MID_RESPONSE_RECEIVED;
4619 dequeue_mid(mid, false);
4623 struct smb2_decrypt_work {
4624 struct work_struct decrypt;
4625 struct TCP_Server_Info *server;
4626 struct xarray buffer;
4632 static void smb2_decrypt_offload(struct work_struct *work)
4634 struct smb2_decrypt_work *dw = container_of(work,
4635 struct smb2_decrypt_work, decrypt);
4637 struct mid_q_entry *mid;
4638 struct iov_iter iter;
4640 iov_iter_xarray(&iter, ITER_DEST, &dw->buffer, 0, dw->len);
4641 rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
4644 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4648 dw->server->lstrp = jiffies;
4649 mid = smb2_find_dequeue_mid(dw->server, dw->buf);
4651 cifs_dbg(FYI, "mid not found\n");
4653 mid->decrypted = true;
4654 rc = handle_read_data(dw->server, mid, dw->buf,
4655 dw->server->vals->read_rsp_size,
4656 &dw->buffer, dw->len,
4659 #ifdef CONFIG_CIFS_STATS2
4660 mid->when_received = jiffies;
4662 if (dw->server->ops->is_network_name_deleted)
4663 dw->server->ops->is_network_name_deleted(dw->buf,
4668 spin_lock(&dw->server->srv_lock);
4669 if (dw->server->tcpStatus == CifsNeedReconnect) {
4670 spin_lock(&dw->server->mid_lock);
4671 mid->mid_state = MID_RETRY_NEEDED;
4672 spin_unlock(&dw->server->mid_lock);
4673 spin_unlock(&dw->server->srv_lock);
4676 spin_lock(&dw->server->mid_lock);
4677 mid->mid_state = MID_REQUEST_SUBMITTED;
4678 mid->mid_flags &= ~(MID_DELETED);
4679 list_add_tail(&mid->qhead,
4680 &dw->server->pending_mid_q);
4681 spin_unlock(&dw->server->mid_lock);
4682 spin_unlock(&dw->server->srv_lock);
4689 cifs_clear_xarray_buffer(&dw->buffer);
4690 cifs_small_buf_release(dw->buf);
4696 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4700 char *buf = server->smallbuf;
4701 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4702 struct iov_iter iter;
4703 unsigned int len, npages;
4704 unsigned int buflen = server->pdu_size;
4707 struct smb2_decrypt_work *dw;
4709 dw = kzalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
4712 xa_init(&dw->buffer);
4713 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
4714 dw->server = server;
4717 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
4718 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
4720 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
4723 server->total_read += rc;
4725 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
4726 server->vals->read_rsp_size;
4728 npages = DIV_ROUND_UP(len, PAGE_SIZE);
4731 for (; i < npages; i++) {
4734 page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4738 old = xa_store(&dw->buffer, i, page, GFP_KERNEL);
4739 if (xa_is_err(old)) {
4744 xa_set_mark(&dw->buffer, i, XA_MARK_0);
4747 iov_iter_xarray(&iter, ITER_DEST, &dw->buffer, 0, npages * PAGE_SIZE);
4749 /* Read the data into the buffer and clear excess bufferage. */
4750 rc = cifs_read_iter_from_socket(server, &iter, dw->len);
4754 server->total_read += rc;
4755 if (rc < npages * PAGE_SIZE)
4756 iov_iter_zero(npages * PAGE_SIZE - rc, &iter);
4757 iov_iter_revert(&iter, npages * PAGE_SIZE);
4758 iov_iter_truncate(&iter, dw->len);
4760 rc = cifs_discard_remaining_data(server);
4765 * For large reads, offload to different thread for better performance,
4766 * use more cores decrypting which can be expensive
4769 if ((server->min_offload) && (server->in_flight > 1) &&
4770 (server->pdu_size >= server->min_offload)) {
4771 dw->buf = server->smallbuf;
4772 server->smallbuf = (char *)cifs_small_buf_get();
4774 queue_work(decrypt_wq, &dw->decrypt);
4775 *num_mids = 0; /* worker thread takes care of finding mid */
4779 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
4784 *mid = smb2_find_mid(server, buf);
4786 cifs_dbg(FYI, "mid not found\n");
4788 cifs_dbg(FYI, "mid found\n");
4789 (*mid)->decrypted = true;
4790 rc = handle_read_data(server, *mid, buf,
4791 server->vals->read_rsp_size,
4792 &dw->buffer, dw->len, false);
4794 if (server->ops->is_network_name_deleted) {
4795 server->ops->is_network_name_deleted(buf,
4802 cifs_clear_xarray_buffer(&dw->buffer);
4807 cifs_discard_remaining_data(server);
4812 receive_encrypted_standard(struct TCP_Server_Info *server,
4813 struct mid_q_entry **mids, char **bufs,
4817 char *buf = server->smallbuf;
4818 struct smb2_hdr *shdr;
4819 unsigned int pdu_length = server->pdu_size;
4820 unsigned int buf_size;
4821 unsigned int next_cmd;
4822 struct mid_q_entry *mid_entry;
4824 char *next_buffer = NULL;
4828 /* switch to large buffer if too big for a small one */
4829 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
4830 server->large_buf = true;
4831 memcpy(server->bigbuf, buf, server->total_read);
4832 buf = server->bigbuf;
4835 /* now read the rest */
4836 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
4837 pdu_length - HEADER_SIZE(server) + 1);
4840 server->total_read += length;
4842 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
4843 length = decrypt_raw_data(server, buf, buf_size, NULL, false);
4847 next_is_large = server->large_buf;
4849 shdr = (struct smb2_hdr *)buf;
4850 next_cmd = le32_to_cpu(shdr->NextCommand);
4852 if (WARN_ON_ONCE(next_cmd > pdu_length))
4855 next_buffer = (char *)cifs_buf_get();
4857 next_buffer = (char *)cifs_small_buf_get();
4858 memcpy(next_buffer, buf + next_cmd, pdu_length - next_cmd);
4861 mid_entry = smb2_find_mid(server, buf);
4862 if (mid_entry == NULL)
4863 cifs_dbg(FYI, "mid not found\n");
4865 cifs_dbg(FYI, "mid found\n");
4866 mid_entry->decrypted = true;
4867 mid_entry->resp_buf_size = server->pdu_size;
4870 if (*num_mids >= MAX_COMPOUND) {
4871 cifs_server_dbg(VFS, "too many PDUs in compound\n");
4874 bufs[*num_mids] = buf;
4875 mids[(*num_mids)++] = mid_entry;
4877 if (mid_entry && mid_entry->handle)
4878 ret = mid_entry->handle(server, mid_entry);
4880 ret = cifs_handle_standard(server, mid_entry);
4882 if (ret == 0 && next_cmd) {
4883 pdu_length -= next_cmd;
4884 server->large_buf = next_is_large;
4886 server->bigbuf = buf = next_buffer;
4888 server->smallbuf = buf = next_buffer;
4890 } else if (ret != 0) {
4892 * ret != 0 here means that we didn't get to handle_mid() thus
4893 * server->smallbuf and server->bigbuf are still valid. We need
4894 * to free next_buffer because it is not going to be used
4898 free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
4900 free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
4907 smb3_receive_transform(struct TCP_Server_Info *server,
4908 struct mid_q_entry **mids, char **bufs, int *num_mids)
4910 char *buf = server->smallbuf;
4911 unsigned int pdu_length = server->pdu_size;
4912 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4913 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4915 if (pdu_length < sizeof(struct smb2_transform_hdr) +
4916 sizeof(struct smb2_hdr)) {
4917 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
4919 cifs_reconnect(server, true);
4920 return -ECONNABORTED;
4923 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
4924 cifs_server_dbg(VFS, "Transform message is broken\n");
4925 cifs_reconnect(server, true);
4926 return -ECONNABORTED;
4929 /* TODO: add support for compounds containing READ. */
4930 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
4931 return receive_encrypted_read(server, &mids[0], num_mids);
4934 return receive_encrypted_standard(server, mids, bufs, num_mids);
4938 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
4940 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
4942 return handle_read_data(server, mid, buf, server->pdu_size,
4946 static int smb2_next_header(struct TCP_Server_Info *server, char *buf,
4949 struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
4950 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
4952 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
4953 *noff = le32_to_cpu(t_hdr->OriginalMessageSize);
4954 if (unlikely(check_add_overflow(*noff, sizeof(*t_hdr), noff)))
4957 *noff = le32_to_cpu(hdr->NextCommand);
4959 if (unlikely(*noff && *noff < MID_HEADER_SIZE(server)))
4964 int cifs_sfu_make_node(unsigned int xid, struct inode *inode,
4965 struct dentry *dentry, struct cifs_tcon *tcon,
4966 const char *full_path, umode_t mode, dev_t dev)
4968 struct cifs_open_info_data buf = {};
4969 struct TCP_Server_Info *server = tcon->ses->server;
4970 struct cifs_open_parms oparms;
4971 struct cifs_io_parms io_parms = {};
4972 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4973 struct cifs_fid fid;
4974 unsigned int bytes_written;
4975 struct win_dev *pdev;
4977 __u32 oplock = server->oplocks ? REQ_OPLOCK : 0;
4980 if (!S_ISCHR(mode) && !S_ISBLK(mode) && !S_ISFIFO(mode))
4983 oparms = (struct cifs_open_parms) {
4986 .desired_access = GENERIC_WRITE,
4987 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
4988 CREATE_OPTION_SPECIAL),
4989 .disposition = FILE_CREATE,
4994 rc = server->ops->open(xid, &oparms, &oplock, &buf);
4999 * BB Do not bother to decode buf since no local inode yet to put
5000 * timestamps in, but we can reuse it safely.
5002 pdev = (struct win_dev *)&buf.fi;
5003 io_parms.pid = current->tgid;
5004 io_parms.tcon = tcon;
5005 io_parms.length = sizeof(*pdev);
5006 iov[1].iov_base = pdev;
5007 iov[1].iov_len = sizeof(*pdev);
5008 if (S_ISCHR(mode)) {
5009 memcpy(pdev->type, "IntxCHR", 8);
5010 pdev->major = cpu_to_le64(MAJOR(dev));
5011 pdev->minor = cpu_to_le64(MINOR(dev));
5012 } else if (S_ISBLK(mode)) {
5013 memcpy(pdev->type, "IntxBLK", 8);
5014 pdev->major = cpu_to_le64(MAJOR(dev));
5015 pdev->minor = cpu_to_le64(MINOR(dev));
5016 } else if (S_ISFIFO(mode)) {
5017 memcpy(pdev->type, "LnxFIFO", 8);
5020 rc = server->ops->sync_write(xid, &fid, &io_parms,
5021 &bytes_written, iov, 1);
5022 server->ops->close(xid, tcon, &fid);
5024 /* FIXME: add code here to set EAs */
5025 cifs_free_open_info(&buf);
5029 static int smb2_make_node(unsigned int xid, struct inode *inode,
5030 struct dentry *dentry, struct cifs_tcon *tcon,
5031 const char *full_path, umode_t mode, dev_t dev)
5033 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
5037 * Check if mounted with mount parm 'sfu' mount parm.
5038 * SFU emulation should work with all servers, but only
5039 * supports block and char device (no socket & fifo),
5040 * and was used by default in earlier versions of Windows
5042 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
5043 rc = cifs_sfu_make_node(xid, inode, dentry, tcon,
5044 full_path, mode, dev);
5046 rc = smb2_mknod_reparse(xid, inode, dentry, tcon,
5047 full_path, mode, dev);
5052 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
5053 struct smb_version_operations smb20_operations = {
5054 .compare_fids = smb2_compare_fids,
5055 .setup_request = smb2_setup_request,
5056 .setup_async_request = smb2_setup_async_request,
5057 .check_receive = smb2_check_receive,
5058 .add_credits = smb2_add_credits,
5059 .set_credits = smb2_set_credits,
5060 .get_credits_field = smb2_get_credits_field,
5061 .get_credits = smb2_get_credits,
5062 .wait_mtu_credits = cifs_wait_mtu_credits,
5063 .get_next_mid = smb2_get_next_mid,
5064 .revert_current_mid = smb2_revert_current_mid,
5065 .read_data_offset = smb2_read_data_offset,
5066 .read_data_length = smb2_read_data_length,
5067 .map_error = map_smb2_to_linux_error,
5068 .find_mid = smb2_find_mid,
5069 .check_message = smb2_check_message,
5070 .dump_detail = smb2_dump_detail,
5071 .clear_stats = smb2_clear_stats,
5072 .print_stats = smb2_print_stats,
5073 .is_oplock_break = smb2_is_valid_oplock_break,
5074 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5075 .downgrade_oplock = smb2_downgrade_oplock,
5076 .need_neg = smb2_need_neg,
5077 .negotiate = smb2_negotiate,
5078 .negotiate_wsize = smb2_negotiate_wsize,
5079 .negotiate_rsize = smb2_negotiate_rsize,
5080 .sess_setup = SMB2_sess_setup,
5081 .logoff = SMB2_logoff,
5082 .tree_connect = SMB2_tcon,
5083 .tree_disconnect = SMB2_tdis,
5084 .qfs_tcon = smb2_qfs_tcon,
5085 .is_path_accessible = smb2_is_path_accessible,
5086 .can_echo = smb2_can_echo,
5088 .query_path_info = smb2_query_path_info,
5089 .query_reparse_point = smb2_query_reparse_point,
5090 .get_srv_inum = smb2_get_srv_inum,
5091 .query_file_info = smb2_query_file_info,
5092 .set_path_size = smb2_set_path_size,
5093 .set_file_size = smb2_set_file_size,
5094 .set_file_info = smb2_set_file_info,
5095 .set_compression = smb2_set_compression,
5096 .mkdir = smb2_mkdir,
5097 .mkdir_setinfo = smb2_mkdir_setinfo,
5098 .rmdir = smb2_rmdir,
5099 .unlink = smb2_unlink,
5100 .rename = smb2_rename_path,
5101 .create_hardlink = smb2_create_hardlink,
5102 .parse_reparse_point = smb2_parse_reparse_point,
5103 .query_mf_symlink = smb3_query_mf_symlink,
5104 .create_mf_symlink = smb3_create_mf_symlink,
5105 .create_reparse_symlink = smb2_create_reparse_symlink,
5106 .open = smb2_open_file,
5107 .set_fid = smb2_set_fid,
5108 .close = smb2_close_file,
5109 .flush = smb2_flush_file,
5110 .async_readv = smb2_async_readv,
5111 .async_writev = smb2_async_writev,
5112 .sync_read = smb2_sync_read,
5113 .sync_write = smb2_sync_write,
5114 .query_dir_first = smb2_query_dir_first,
5115 .query_dir_next = smb2_query_dir_next,
5116 .close_dir = smb2_close_dir,
5117 .calc_smb_size = smb2_calc_size,
5118 .is_status_pending = smb2_is_status_pending,
5119 .is_session_expired = smb2_is_session_expired,
5120 .oplock_response = smb2_oplock_response,
5121 .queryfs = smb2_queryfs,
5122 .mand_lock = smb2_mand_lock,
5123 .mand_unlock_range = smb2_unlock_range,
5124 .push_mand_locks = smb2_push_mandatory_locks,
5125 .get_lease_key = smb2_get_lease_key,
5126 .set_lease_key = smb2_set_lease_key,
5127 .new_lease_key = smb2_new_lease_key,
5128 .calc_signature = smb2_calc_signature,
5129 .is_read_op = smb2_is_read_op,
5130 .set_oplock_level = smb2_set_oplock_level,
5131 .create_lease_buf = smb2_create_lease_buf,
5132 .parse_lease_buf = smb2_parse_lease_buf,
5133 .copychunk_range = smb2_copychunk_range,
5134 .wp_retry_size = smb2_wp_retry_size,
5135 .dir_needs_close = smb2_dir_needs_close,
5136 .get_dfs_refer = smb2_get_dfs_refer,
5137 .select_sectype = smb2_select_sectype,
5138 #ifdef CONFIG_CIFS_XATTR
5139 .query_all_EAs = smb2_query_eas,
5140 .set_EA = smb2_set_ea,
5141 #endif /* CIFS_XATTR */
5142 .get_acl = get_smb2_acl,
5143 .get_acl_by_fid = get_smb2_acl_by_fid,
5144 .set_acl = set_smb2_acl,
5145 .next_header = smb2_next_header,
5146 .ioctl_query_info = smb2_ioctl_query_info,
5147 .make_node = smb2_make_node,
5148 .fiemap = smb3_fiemap,
5149 .llseek = smb3_llseek,
5150 .is_status_io_timeout = smb2_is_status_io_timeout,
5151 .is_network_name_deleted = smb2_is_network_name_deleted,
5153 #endif /* CIFS_ALLOW_INSECURE_LEGACY */
5155 struct smb_version_operations smb21_operations = {
5156 .compare_fids = smb2_compare_fids,
5157 .setup_request = smb2_setup_request,
5158 .setup_async_request = smb2_setup_async_request,
5159 .check_receive = smb2_check_receive,
5160 .add_credits = smb2_add_credits,
5161 .set_credits = smb2_set_credits,
5162 .get_credits_field = smb2_get_credits_field,
5163 .get_credits = smb2_get_credits,
5164 .wait_mtu_credits = smb2_wait_mtu_credits,
5165 .adjust_credits = smb2_adjust_credits,
5166 .get_next_mid = smb2_get_next_mid,
5167 .revert_current_mid = smb2_revert_current_mid,
5168 .read_data_offset = smb2_read_data_offset,
5169 .read_data_length = smb2_read_data_length,
5170 .map_error = map_smb2_to_linux_error,
5171 .find_mid = smb2_find_mid,
5172 .check_message = smb2_check_message,
5173 .dump_detail = smb2_dump_detail,
5174 .clear_stats = smb2_clear_stats,
5175 .print_stats = smb2_print_stats,
5176 .is_oplock_break = smb2_is_valid_oplock_break,
5177 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5178 .downgrade_oplock = smb2_downgrade_oplock,
5179 .need_neg = smb2_need_neg,
5180 .negotiate = smb2_negotiate,
5181 .negotiate_wsize = smb2_negotiate_wsize,
5182 .negotiate_rsize = smb2_negotiate_rsize,
5183 .sess_setup = SMB2_sess_setup,
5184 .logoff = SMB2_logoff,
5185 .tree_connect = SMB2_tcon,
5186 .tree_disconnect = SMB2_tdis,
5187 .qfs_tcon = smb2_qfs_tcon,
5188 .is_path_accessible = smb2_is_path_accessible,
5189 .can_echo = smb2_can_echo,
5191 .query_path_info = smb2_query_path_info,
5192 .query_reparse_point = smb2_query_reparse_point,
5193 .get_srv_inum = smb2_get_srv_inum,
5194 .query_file_info = smb2_query_file_info,
5195 .set_path_size = smb2_set_path_size,
5196 .set_file_size = smb2_set_file_size,
5197 .set_file_info = smb2_set_file_info,
5198 .set_compression = smb2_set_compression,
5199 .mkdir = smb2_mkdir,
5200 .mkdir_setinfo = smb2_mkdir_setinfo,
5201 .rmdir = smb2_rmdir,
5202 .unlink = smb2_unlink,
5203 .rename = smb2_rename_path,
5204 .create_hardlink = smb2_create_hardlink,
5205 .parse_reparse_point = smb2_parse_reparse_point,
5206 .query_mf_symlink = smb3_query_mf_symlink,
5207 .create_mf_symlink = smb3_create_mf_symlink,
5208 .create_reparse_symlink = smb2_create_reparse_symlink,
5209 .open = smb2_open_file,
5210 .set_fid = smb2_set_fid,
5211 .close = smb2_close_file,
5212 .flush = smb2_flush_file,
5213 .async_readv = smb2_async_readv,
5214 .async_writev = smb2_async_writev,
5215 .sync_read = smb2_sync_read,
5216 .sync_write = smb2_sync_write,
5217 .query_dir_first = smb2_query_dir_first,
5218 .query_dir_next = smb2_query_dir_next,
5219 .close_dir = smb2_close_dir,
5220 .calc_smb_size = smb2_calc_size,
5221 .is_status_pending = smb2_is_status_pending,
5222 .is_session_expired = smb2_is_session_expired,
5223 .oplock_response = smb2_oplock_response,
5224 .queryfs = smb2_queryfs,
5225 .mand_lock = smb2_mand_lock,
5226 .mand_unlock_range = smb2_unlock_range,
5227 .push_mand_locks = smb2_push_mandatory_locks,
5228 .get_lease_key = smb2_get_lease_key,
5229 .set_lease_key = smb2_set_lease_key,
5230 .new_lease_key = smb2_new_lease_key,
5231 .calc_signature = smb2_calc_signature,
5232 .is_read_op = smb21_is_read_op,
5233 .set_oplock_level = smb21_set_oplock_level,
5234 .create_lease_buf = smb2_create_lease_buf,
5235 .parse_lease_buf = smb2_parse_lease_buf,
5236 .copychunk_range = smb2_copychunk_range,
5237 .wp_retry_size = smb2_wp_retry_size,
5238 .dir_needs_close = smb2_dir_needs_close,
5239 .enum_snapshots = smb3_enum_snapshots,
5240 .notify = smb3_notify,
5241 .get_dfs_refer = smb2_get_dfs_refer,
5242 .select_sectype = smb2_select_sectype,
5243 #ifdef CONFIG_CIFS_XATTR
5244 .query_all_EAs = smb2_query_eas,
5245 .set_EA = smb2_set_ea,
5246 #endif /* CIFS_XATTR */
5247 .get_acl = get_smb2_acl,
5248 .get_acl_by_fid = get_smb2_acl_by_fid,
5249 .set_acl = set_smb2_acl,
5250 .next_header = smb2_next_header,
5251 .ioctl_query_info = smb2_ioctl_query_info,
5252 .make_node = smb2_make_node,
5253 .fiemap = smb3_fiemap,
5254 .llseek = smb3_llseek,
5255 .is_status_io_timeout = smb2_is_status_io_timeout,
5256 .is_network_name_deleted = smb2_is_network_name_deleted,
5259 struct smb_version_operations smb30_operations = {
5260 .compare_fids = smb2_compare_fids,
5261 .setup_request = smb2_setup_request,
5262 .setup_async_request = smb2_setup_async_request,
5263 .check_receive = smb2_check_receive,
5264 .add_credits = smb2_add_credits,
5265 .set_credits = smb2_set_credits,
5266 .get_credits_field = smb2_get_credits_field,
5267 .get_credits = smb2_get_credits,
5268 .wait_mtu_credits = smb2_wait_mtu_credits,
5269 .adjust_credits = smb2_adjust_credits,
5270 .get_next_mid = smb2_get_next_mid,
5271 .revert_current_mid = smb2_revert_current_mid,
5272 .read_data_offset = smb2_read_data_offset,
5273 .read_data_length = smb2_read_data_length,
5274 .map_error = map_smb2_to_linux_error,
5275 .find_mid = smb2_find_mid,
5276 .check_message = smb2_check_message,
5277 .dump_detail = smb2_dump_detail,
5278 .clear_stats = smb2_clear_stats,
5279 .print_stats = smb2_print_stats,
5280 .dump_share_caps = smb2_dump_share_caps,
5281 .is_oplock_break = smb2_is_valid_oplock_break,
5282 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5283 .downgrade_oplock = smb3_downgrade_oplock,
5284 .need_neg = smb2_need_neg,
5285 .negotiate = smb2_negotiate,
5286 .negotiate_wsize = smb3_negotiate_wsize,
5287 .negotiate_rsize = smb3_negotiate_rsize,
5288 .sess_setup = SMB2_sess_setup,
5289 .logoff = SMB2_logoff,
5290 .tree_connect = SMB2_tcon,
5291 .tree_disconnect = SMB2_tdis,
5292 .qfs_tcon = smb3_qfs_tcon,
5293 .is_path_accessible = smb2_is_path_accessible,
5294 .can_echo = smb2_can_echo,
5296 .query_path_info = smb2_query_path_info,
5297 /* WSL tags introduced long after smb2.1, enable for SMB3, 3.11 only */
5298 .query_reparse_point = smb2_query_reparse_point,
5299 .get_srv_inum = smb2_get_srv_inum,
5300 .query_file_info = smb2_query_file_info,
5301 .set_path_size = smb2_set_path_size,
5302 .set_file_size = smb2_set_file_size,
5303 .set_file_info = smb2_set_file_info,
5304 .set_compression = smb2_set_compression,
5305 .mkdir = smb2_mkdir,
5306 .mkdir_setinfo = smb2_mkdir_setinfo,
5307 .rmdir = smb2_rmdir,
5308 .unlink = smb2_unlink,
5309 .rename = smb2_rename_path,
5310 .create_hardlink = smb2_create_hardlink,
5311 .parse_reparse_point = smb2_parse_reparse_point,
5312 .query_mf_symlink = smb3_query_mf_symlink,
5313 .create_mf_symlink = smb3_create_mf_symlink,
5314 .create_reparse_symlink = smb2_create_reparse_symlink,
5315 .open = smb2_open_file,
5316 .set_fid = smb2_set_fid,
5317 .close = smb2_close_file,
5318 .close_getattr = smb2_close_getattr,
5319 .flush = smb2_flush_file,
5320 .async_readv = smb2_async_readv,
5321 .async_writev = smb2_async_writev,
5322 .sync_read = smb2_sync_read,
5323 .sync_write = smb2_sync_write,
5324 .query_dir_first = smb2_query_dir_first,
5325 .query_dir_next = smb2_query_dir_next,
5326 .close_dir = smb2_close_dir,
5327 .calc_smb_size = smb2_calc_size,
5328 .is_status_pending = smb2_is_status_pending,
5329 .is_session_expired = smb2_is_session_expired,
5330 .oplock_response = smb2_oplock_response,
5331 .queryfs = smb2_queryfs,
5332 .mand_lock = smb2_mand_lock,
5333 .mand_unlock_range = smb2_unlock_range,
5334 .push_mand_locks = smb2_push_mandatory_locks,
5335 .get_lease_key = smb2_get_lease_key,
5336 .set_lease_key = smb2_set_lease_key,
5337 .new_lease_key = smb2_new_lease_key,
5338 .generate_signingkey = generate_smb30signingkey,
5339 .calc_signature = smb3_calc_signature,
5340 .set_integrity = smb3_set_integrity,
5341 .is_read_op = smb21_is_read_op,
5342 .set_oplock_level = smb3_set_oplock_level,
5343 .create_lease_buf = smb3_create_lease_buf,
5344 .parse_lease_buf = smb3_parse_lease_buf,
5345 .copychunk_range = smb2_copychunk_range,
5346 .duplicate_extents = smb2_duplicate_extents,
5347 .validate_negotiate = smb3_validate_negotiate,
5348 .wp_retry_size = smb2_wp_retry_size,
5349 .dir_needs_close = smb2_dir_needs_close,
5350 .fallocate = smb3_fallocate,
5351 .enum_snapshots = smb3_enum_snapshots,
5352 .notify = smb3_notify,
5353 .init_transform_rq = smb3_init_transform_rq,
5354 .is_transform_hdr = smb3_is_transform_hdr,
5355 .receive_transform = smb3_receive_transform,
5356 .get_dfs_refer = smb2_get_dfs_refer,
5357 .select_sectype = smb2_select_sectype,
5358 #ifdef CONFIG_CIFS_XATTR
5359 .query_all_EAs = smb2_query_eas,
5360 .set_EA = smb2_set_ea,
5361 #endif /* CIFS_XATTR */
5362 .get_acl = get_smb2_acl,
5363 .get_acl_by_fid = get_smb2_acl_by_fid,
5364 .set_acl = set_smb2_acl,
5365 .next_header = smb2_next_header,
5366 .ioctl_query_info = smb2_ioctl_query_info,
5367 .make_node = smb2_make_node,
5368 .fiemap = smb3_fiemap,
5369 .llseek = smb3_llseek,
5370 .is_status_io_timeout = smb2_is_status_io_timeout,
5371 .is_network_name_deleted = smb2_is_network_name_deleted,
5374 struct smb_version_operations smb311_operations = {
5375 .compare_fids = smb2_compare_fids,
5376 .setup_request = smb2_setup_request,
5377 .setup_async_request = smb2_setup_async_request,
5378 .check_receive = smb2_check_receive,
5379 .add_credits = smb2_add_credits,
5380 .set_credits = smb2_set_credits,
5381 .get_credits_field = smb2_get_credits_field,
5382 .get_credits = smb2_get_credits,
5383 .wait_mtu_credits = smb2_wait_mtu_credits,
5384 .adjust_credits = smb2_adjust_credits,
5385 .get_next_mid = smb2_get_next_mid,
5386 .revert_current_mid = smb2_revert_current_mid,
5387 .read_data_offset = smb2_read_data_offset,
5388 .read_data_length = smb2_read_data_length,
5389 .map_error = map_smb2_to_linux_error,
5390 .find_mid = smb2_find_mid,
5391 .check_message = smb2_check_message,
5392 .dump_detail = smb2_dump_detail,
5393 .clear_stats = smb2_clear_stats,
5394 .print_stats = smb2_print_stats,
5395 .dump_share_caps = smb2_dump_share_caps,
5396 .is_oplock_break = smb2_is_valid_oplock_break,
5397 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5398 .downgrade_oplock = smb3_downgrade_oplock,
5399 .need_neg = smb2_need_neg,
5400 .negotiate = smb2_negotiate,
5401 .negotiate_wsize = smb3_negotiate_wsize,
5402 .negotiate_rsize = smb3_negotiate_rsize,
5403 .sess_setup = SMB2_sess_setup,
5404 .logoff = SMB2_logoff,
5405 .tree_connect = SMB2_tcon,
5406 .tree_disconnect = SMB2_tdis,
5407 .qfs_tcon = smb3_qfs_tcon,
5408 .is_path_accessible = smb2_is_path_accessible,
5409 .can_echo = smb2_can_echo,
5411 .query_path_info = smb2_query_path_info,
5412 .query_reparse_point = smb2_query_reparse_point,
5413 .get_srv_inum = smb2_get_srv_inum,
5414 .query_file_info = smb2_query_file_info,
5415 .set_path_size = smb2_set_path_size,
5416 .set_file_size = smb2_set_file_size,
5417 .set_file_info = smb2_set_file_info,
5418 .set_compression = smb2_set_compression,
5419 .mkdir = smb2_mkdir,
5420 .mkdir_setinfo = smb2_mkdir_setinfo,
5421 .posix_mkdir = smb311_posix_mkdir,
5422 .rmdir = smb2_rmdir,
5423 .unlink = smb2_unlink,
5424 .rename = smb2_rename_path,
5425 .create_hardlink = smb2_create_hardlink,
5426 .parse_reparse_point = smb2_parse_reparse_point,
5427 .query_mf_symlink = smb3_query_mf_symlink,
5428 .create_mf_symlink = smb3_create_mf_symlink,
5429 .create_reparse_symlink = smb2_create_reparse_symlink,
5430 .open = smb2_open_file,
5431 .set_fid = smb2_set_fid,
5432 .close = smb2_close_file,
5433 .close_getattr = smb2_close_getattr,
5434 .flush = smb2_flush_file,
5435 .async_readv = smb2_async_readv,
5436 .async_writev = smb2_async_writev,
5437 .sync_read = smb2_sync_read,
5438 .sync_write = smb2_sync_write,
5439 .query_dir_first = smb2_query_dir_first,
5440 .query_dir_next = smb2_query_dir_next,
5441 .close_dir = smb2_close_dir,
5442 .calc_smb_size = smb2_calc_size,
5443 .is_status_pending = smb2_is_status_pending,
5444 .is_session_expired = smb2_is_session_expired,
5445 .oplock_response = smb2_oplock_response,
5446 .queryfs = smb311_queryfs,
5447 .mand_lock = smb2_mand_lock,
5448 .mand_unlock_range = smb2_unlock_range,
5449 .push_mand_locks = smb2_push_mandatory_locks,
5450 .get_lease_key = smb2_get_lease_key,
5451 .set_lease_key = smb2_set_lease_key,
5452 .new_lease_key = smb2_new_lease_key,
5453 .generate_signingkey = generate_smb311signingkey,
5454 .calc_signature = smb3_calc_signature,
5455 .set_integrity = smb3_set_integrity,
5456 .is_read_op = smb21_is_read_op,
5457 .set_oplock_level = smb3_set_oplock_level,
5458 .create_lease_buf = smb3_create_lease_buf,
5459 .parse_lease_buf = smb3_parse_lease_buf,
5460 .copychunk_range = smb2_copychunk_range,
5461 .duplicate_extents = smb2_duplicate_extents,
5462 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5463 .wp_retry_size = smb2_wp_retry_size,
5464 .dir_needs_close = smb2_dir_needs_close,
5465 .fallocate = smb3_fallocate,
5466 .enum_snapshots = smb3_enum_snapshots,
5467 .notify = smb3_notify,
5468 .init_transform_rq = smb3_init_transform_rq,
5469 .is_transform_hdr = smb3_is_transform_hdr,
5470 .receive_transform = smb3_receive_transform,
5471 .get_dfs_refer = smb2_get_dfs_refer,
5472 .select_sectype = smb2_select_sectype,
5473 #ifdef CONFIG_CIFS_XATTR
5474 .query_all_EAs = smb2_query_eas,
5475 .set_EA = smb2_set_ea,
5476 #endif /* CIFS_XATTR */
5477 .get_acl = get_smb2_acl,
5478 .get_acl_by_fid = get_smb2_acl_by_fid,
5479 .set_acl = set_smb2_acl,
5480 .next_header = smb2_next_header,
5481 .ioctl_query_info = smb2_ioctl_query_info,
5482 .make_node = smb2_make_node,
5483 .fiemap = smb3_fiemap,
5484 .llseek = smb3_llseek,
5485 .is_status_io_timeout = smb2_is_status_io_timeout,
5486 .is_network_name_deleted = smb2_is_network_name_deleted,
5489 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
5490 struct smb_version_values smb20_values = {
5491 .version_string = SMB20_VERSION_STRING,
5492 .protocol_id = SMB20_PROT_ID,
5493 .req_capabilities = 0, /* MBZ */
5494 .large_lock_type = 0,
5495 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5496 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5497 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5498 .header_size = sizeof(struct smb2_hdr),
5499 .header_preamble_size = 0,
5500 .max_header_size = MAX_SMB2_HDR_SIZE,
5501 .read_rsp_size = sizeof(struct smb2_read_rsp),
5502 .lock_cmd = SMB2_LOCK,
5504 .cap_nt_find = SMB2_NT_FIND,
5505 .cap_large_files = SMB2_LARGE_FILES,
5506 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5507 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5508 .create_lease_size = sizeof(struct create_lease),
5510 #endif /* ALLOW_INSECURE_LEGACY */
5512 struct smb_version_values smb21_values = {
5513 .version_string = SMB21_VERSION_STRING,
5514 .protocol_id = SMB21_PROT_ID,
5515 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5516 .large_lock_type = 0,
5517 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5518 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5519 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5520 .header_size = sizeof(struct smb2_hdr),
5521 .header_preamble_size = 0,
5522 .max_header_size = MAX_SMB2_HDR_SIZE,
5523 .read_rsp_size = sizeof(struct smb2_read_rsp),
5524 .lock_cmd = SMB2_LOCK,
5526 .cap_nt_find = SMB2_NT_FIND,
5527 .cap_large_files = SMB2_LARGE_FILES,
5528 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5529 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5530 .create_lease_size = sizeof(struct create_lease),
5533 struct smb_version_values smb3any_values = {
5534 .version_string = SMB3ANY_VERSION_STRING,
5535 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5536 .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,
5537 .large_lock_type = 0,
5538 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5539 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5540 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5541 .header_size = sizeof(struct smb2_hdr),
5542 .header_preamble_size = 0,
5543 .max_header_size = MAX_SMB2_HDR_SIZE,
5544 .read_rsp_size = sizeof(struct smb2_read_rsp),
5545 .lock_cmd = SMB2_LOCK,
5547 .cap_nt_find = SMB2_NT_FIND,
5548 .cap_large_files = SMB2_LARGE_FILES,
5549 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5550 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5551 .create_lease_size = sizeof(struct create_lease_v2),
5554 struct smb_version_values smbdefault_values = {
5555 .version_string = SMBDEFAULT_VERSION_STRING,
5556 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5557 .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,
5558 .large_lock_type = 0,
5559 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5560 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5561 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5562 .header_size = sizeof(struct smb2_hdr),
5563 .header_preamble_size = 0,
5564 .max_header_size = MAX_SMB2_HDR_SIZE,
5565 .read_rsp_size = sizeof(struct smb2_read_rsp),
5566 .lock_cmd = SMB2_LOCK,
5568 .cap_nt_find = SMB2_NT_FIND,
5569 .cap_large_files = SMB2_LARGE_FILES,
5570 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5571 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5572 .create_lease_size = sizeof(struct create_lease_v2),
5575 struct smb_version_values smb30_values = {
5576 .version_string = SMB30_VERSION_STRING,
5577 .protocol_id = SMB30_PROT_ID,
5578 .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,
5579 .large_lock_type = 0,
5580 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5581 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5582 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5583 .header_size = sizeof(struct smb2_hdr),
5584 .header_preamble_size = 0,
5585 .max_header_size = MAX_SMB2_HDR_SIZE,
5586 .read_rsp_size = sizeof(struct smb2_read_rsp),
5587 .lock_cmd = SMB2_LOCK,
5589 .cap_nt_find = SMB2_NT_FIND,
5590 .cap_large_files = SMB2_LARGE_FILES,
5591 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5592 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5593 .create_lease_size = sizeof(struct create_lease_v2),
5596 struct smb_version_values smb302_values = {
5597 .version_string = SMB302_VERSION_STRING,
5598 .protocol_id = SMB302_PROT_ID,
5599 .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,
5600 .large_lock_type = 0,
5601 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5602 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5603 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5604 .header_size = sizeof(struct smb2_hdr),
5605 .header_preamble_size = 0,
5606 .max_header_size = MAX_SMB2_HDR_SIZE,
5607 .read_rsp_size = sizeof(struct smb2_read_rsp),
5608 .lock_cmd = SMB2_LOCK,
5610 .cap_nt_find = SMB2_NT_FIND,
5611 .cap_large_files = SMB2_LARGE_FILES,
5612 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5613 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5614 .create_lease_size = sizeof(struct create_lease_v2),
5617 struct smb_version_values smb311_values = {
5618 .version_string = SMB311_VERSION_STRING,
5619 .protocol_id = SMB311_PROT_ID,
5620 .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,
5621 .large_lock_type = 0,
5622 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5623 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5624 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5625 .header_size = sizeof(struct smb2_hdr),
5626 .header_preamble_size = 0,
5627 .max_header_size = MAX_SMB2_HDR_SIZE,
5628 .read_rsp_size = sizeof(struct smb2_read_rsp),
5629 .lock_cmd = SMB2_LOCK,
5631 .cap_nt_find = SMB2_NT_FIND,
5632 .cap_large_files = SMB2_LARGE_FILES,
5633 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5634 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5635 .create_lease_size = sizeof(struct create_lease_v2),