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"
31 /* Change credits for different ops and return the total number of credits */
33 change_conf(struct TCP_Server_Info *server)
35 server->credits += server->echo_credits + server->oplock_credits;
36 server->oplock_credits = server->echo_credits = 0;
37 switch (server->credits) {
41 server->echoes = false;
42 server->oplocks = false;
45 server->echoes = true;
46 server->oplocks = false;
47 server->echo_credits = 1;
50 server->echoes = true;
52 server->oplocks = true;
53 server->oplock_credits = 1;
55 server->oplocks = false;
57 server->echo_credits = 1;
59 server->credits -= server->echo_credits + server->oplock_credits;
60 return server->credits + server->echo_credits + server->oplock_credits;
64 smb2_add_credits(struct TCP_Server_Info *server,
65 const struct cifs_credits *credits, const int optype)
68 int scredits, in_flight;
69 unsigned int add = credits->value;
70 unsigned int instance = credits->instance;
71 bool reconnect_detected = false;
72 bool reconnect_with_invalid_credits = false;
74 spin_lock(&server->req_lock);
75 val = server->ops->get_credits_field(server, optype);
77 /* eg found case where write overlapping reconnect messed up credits */
78 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
79 reconnect_with_invalid_credits = true;
81 if ((instance == 0) || (instance == server->reconnect_instance))
84 reconnect_detected = true;
87 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
88 pr_warn_once("server overflowed SMB3 credits\n");
89 trace_smb3_overflow_credits(server->CurrentMid,
90 server->conn_id, server->hostname, *val,
91 add, server->in_flight);
94 if (server->in_flight == 0 &&
95 ((optype & CIFS_OP_MASK) != CIFS_NEG_OP) &&
96 ((optype & CIFS_OP_MASK) != CIFS_SESS_OP))
97 rc = change_conf(server);
99 * Sometimes server returns 0 credits on oplock break ack - we need to
100 * rebalance credits in this case.
102 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
104 if (server->credits > 1) {
106 server->oplock_credits++;
110 in_flight = server->in_flight;
111 spin_unlock(&server->req_lock);
112 wake_up(&server->request_q);
114 if (reconnect_detected) {
115 trace_smb3_reconnect_detected(server->CurrentMid,
116 server->conn_id, server->hostname, scredits, add, in_flight);
118 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
122 if (reconnect_with_invalid_credits) {
123 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
124 server->conn_id, server->hostname, scredits, add, in_flight);
125 cifs_dbg(FYI, "Negotiate operation when server credits is non-zero. Optype: %d, server credits: %d, credits added: %d\n",
126 optype, scredits, add);
129 spin_lock(&cifs_tcp_ses_lock);
130 if (server->tcpStatus == CifsNeedReconnect
131 || server->tcpStatus == CifsExiting) {
132 spin_unlock(&cifs_tcp_ses_lock);
135 spin_unlock(&cifs_tcp_ses_lock);
139 /* change_conf hasn't been executed */
142 cifs_server_dbg(VFS, "Possible client or server bug - zero credits\n");
145 cifs_server_dbg(VFS, "disabling echoes and oplocks\n");
148 cifs_dbg(FYI, "disabling oplocks\n");
151 /* change_conf rebalanced credits for different types */
155 trace_smb3_add_credits(server->CurrentMid,
156 server->conn_id, server->hostname, scredits, add, in_flight);
157 cifs_dbg(FYI, "%s: added %u credits total=%d\n", __func__, add, scredits);
161 smb2_set_credits(struct TCP_Server_Info *server, const int val)
163 int scredits, in_flight;
165 spin_lock(&server->req_lock);
166 server->credits = val;
168 server->reconnect_instance++;
169 scredits = server->credits;
170 in_flight = server->in_flight;
171 spin_unlock(&server->req_lock);
173 trace_smb3_set_credits(server->CurrentMid,
174 server->conn_id, server->hostname, scredits, val, in_flight);
175 cifs_dbg(FYI, "%s: set %u credits\n", __func__, val);
177 /* don't log while holding the lock */
179 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
183 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
187 return &server->echo_credits;
189 return &server->oplock_credits;
191 return &server->credits;
196 smb2_get_credits(struct mid_q_entry *mid)
198 return mid->credits_received;
202 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
203 unsigned int *num, struct cifs_credits *credits)
206 unsigned int scredits, in_flight;
208 spin_lock(&server->req_lock);
210 if (server->credits <= 0) {
211 spin_unlock(&server->req_lock);
212 cifs_num_waiters_inc(server);
213 rc = wait_event_killable(server->request_q,
214 has_credits(server, &server->credits, 1));
215 cifs_num_waiters_dec(server);
218 spin_lock(&server->req_lock);
220 spin_unlock(&server->req_lock);
221 spin_lock(&cifs_tcp_ses_lock);
222 if (server->tcpStatus == CifsExiting) {
223 spin_unlock(&cifs_tcp_ses_lock);
226 spin_unlock(&cifs_tcp_ses_lock);
228 spin_lock(&server->req_lock);
229 scredits = server->credits;
230 /* can deadlock with reopen */
232 *num = SMB2_MAX_BUFFER_SIZE;
234 credits->instance = 0;
238 /* leave some credits for reopen and other ops */
240 *num = min_t(unsigned int, size,
241 scredits * SMB2_MAX_BUFFER_SIZE);
244 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
245 credits->instance = server->reconnect_instance;
246 server->credits -= credits->value;
248 if (server->in_flight > server->max_in_flight)
249 server->max_in_flight = server->in_flight;
253 scredits = server->credits;
254 in_flight = server->in_flight;
255 spin_unlock(&server->req_lock);
257 trace_smb3_wait_credits(server->CurrentMid,
258 server->conn_id, server->hostname, scredits, -(credits->value), in_flight);
259 cifs_dbg(FYI, "%s: removed %u credits total=%d\n",
260 __func__, credits->value, scredits);
266 smb2_adjust_credits(struct TCP_Server_Info *server,
267 struct cifs_credits *credits,
268 const unsigned int payload_size)
270 int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
271 int scredits, in_flight;
273 if (!credits->value || credits->value == new_val)
276 if (credits->value < new_val) {
277 trace_smb3_too_many_credits(server->CurrentMid,
278 server->conn_id, server->hostname, 0, credits->value - new_val, 0);
279 cifs_server_dbg(VFS, "request has less credits (%d) than required (%d)",
280 credits->value, new_val);
285 spin_lock(&server->req_lock);
287 if (server->reconnect_instance != credits->instance) {
288 scredits = server->credits;
289 in_flight = server->in_flight;
290 spin_unlock(&server->req_lock);
292 trace_smb3_reconnect_detected(server->CurrentMid,
293 server->conn_id, server->hostname, scredits,
294 credits->value - new_val, in_flight);
295 cifs_server_dbg(VFS, "trying to return %d credits to old session\n",
296 credits->value - new_val);
300 server->credits += credits->value - new_val;
301 scredits = server->credits;
302 in_flight = server->in_flight;
303 spin_unlock(&server->req_lock);
304 wake_up(&server->request_q);
306 trace_smb3_adj_credits(server->CurrentMid,
307 server->conn_id, server->hostname, scredits,
308 credits->value - new_val, in_flight);
309 cifs_dbg(FYI, "%s: adjust added %u credits total=%d\n",
310 __func__, credits->value - new_val, scredits);
312 credits->value = new_val;
318 smb2_get_next_mid(struct TCP_Server_Info *server)
321 /* for SMB2 we need the current value */
322 spin_lock(&GlobalMid_Lock);
323 mid = server->CurrentMid++;
324 spin_unlock(&GlobalMid_Lock);
329 smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
331 spin_lock(&GlobalMid_Lock);
332 if (server->CurrentMid >= val)
333 server->CurrentMid -= val;
334 spin_unlock(&GlobalMid_Lock);
337 static struct mid_q_entry *
338 __smb2_find_mid(struct TCP_Server_Info *server, char *buf, bool dequeue)
340 struct mid_q_entry *mid;
341 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
342 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
344 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
345 cifs_server_dbg(VFS, "Encrypted frame parsing not supported yet\n");
349 spin_lock(&GlobalMid_Lock);
350 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
351 if ((mid->mid == wire_mid) &&
352 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
353 (mid->command == shdr->Command)) {
354 kref_get(&mid->refcount);
356 list_del_init(&mid->qhead);
357 mid->mid_flags |= MID_DELETED;
359 spin_unlock(&GlobalMid_Lock);
363 spin_unlock(&GlobalMid_Lock);
367 static struct mid_q_entry *
368 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
370 return __smb2_find_mid(server, buf, false);
373 static struct mid_q_entry *
374 smb2_find_dequeue_mid(struct TCP_Server_Info *server, char *buf)
376 return __smb2_find_mid(server, buf, true);
380 smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
382 #ifdef CONFIG_CIFS_DEBUG2
383 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
385 cifs_server_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
386 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
387 shdr->Id.SyncId.ProcessId);
388 cifs_server_dbg(VFS, "smb buf %p len %u\n", buf,
389 server->ops->calc_smb_size(buf, server));
394 smb2_need_neg(struct TCP_Server_Info *server)
396 return server->max_read == 0;
400 smb2_negotiate(const unsigned int xid,
401 struct cifs_ses *ses,
402 struct TCP_Server_Info *server)
406 spin_lock(&GlobalMid_Lock);
407 server->CurrentMid = 0;
408 spin_unlock(&GlobalMid_Lock);
409 rc = SMB2_negotiate(xid, ses, server);
410 /* BB we probably don't need to retry with modern servers */
417 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
419 struct TCP_Server_Info *server = tcon->ses->server;
422 /* start with specified wsize, or default */
423 wsize = ctx->wsize ? ctx->wsize : CIFS_DEFAULT_IOSIZE;
424 wsize = min_t(unsigned int, wsize, server->max_write);
425 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
426 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
432 smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
434 struct TCP_Server_Info *server = tcon->ses->server;
437 /* start with specified wsize, or default */
438 wsize = ctx->wsize ? ctx->wsize : SMB3_DEFAULT_IOSIZE;
439 wsize = min_t(unsigned int, wsize, server->max_write);
440 #ifdef CONFIG_CIFS_SMB_DIRECT
444 * Account for SMB2 data transfer packet header and
445 * possible encryption header
447 wsize = min_t(unsigned int,
449 server->smbd_conn->max_fragmented_send_size -
450 SMB2_READWRITE_PDU_HEADER_SIZE -
451 sizeof(struct smb2_transform_hdr));
453 wsize = min_t(unsigned int,
454 wsize, server->smbd_conn->max_readwrite_size);
457 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
458 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
464 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
466 struct TCP_Server_Info *server = tcon->ses->server;
469 /* start with specified rsize, or default */
470 rsize = ctx->rsize ? ctx->rsize : CIFS_DEFAULT_IOSIZE;
471 rsize = min_t(unsigned int, rsize, server->max_read);
473 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
474 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
480 smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
482 struct TCP_Server_Info *server = tcon->ses->server;
485 /* start with specified rsize, or default */
486 rsize = ctx->rsize ? ctx->rsize : SMB3_DEFAULT_IOSIZE;
487 rsize = min_t(unsigned int, rsize, server->max_read);
488 #ifdef CONFIG_CIFS_SMB_DIRECT
492 * Account for SMB2 data transfer packet header and
493 * possible encryption header
495 rsize = min_t(unsigned int,
497 server->smbd_conn->max_fragmented_recv_size -
498 SMB2_READWRITE_PDU_HEADER_SIZE -
499 sizeof(struct smb2_transform_hdr));
501 rsize = min_t(unsigned int,
502 rsize, server->smbd_conn->max_readwrite_size);
506 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
507 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
513 parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
515 struct cifs_ses *ses)
517 struct network_interface_info_ioctl_rsp *p;
518 struct sockaddr_in *addr4;
519 struct sockaddr_in6 *addr6;
520 struct iface_info_ipv4 *p4;
521 struct iface_info_ipv6 *p6;
522 struct cifs_server_iface *info = NULL, *iface = NULL, *niface = NULL;
523 struct cifs_server_iface tmp_iface;
529 bytes_left = buf_len;
532 spin_lock(&ses->iface_lock);
534 * Go through iface_list and do kref_put to remove
535 * any unused ifaces. ifaces in use will be removed
536 * when the last user calls a kref_put on it
538 list_for_each_entry_safe(iface, niface, &ses->iface_list,
540 iface->is_active = 0;
541 kref_put(&iface->refcount, release_iface);
543 spin_unlock(&ses->iface_lock);
545 while (bytes_left >= sizeof(*p)) {
546 memset(&tmp_iface, 0, sizeof(tmp_iface));
547 tmp_iface.speed = le64_to_cpu(p->LinkSpeed);
548 tmp_iface.rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE) ? 1 : 0;
549 tmp_iface.rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE) ? 1 : 0;
553 * The kernel and wire socket structures have the same
554 * layout and use network byte order but make the
555 * conversion explicit in case either one changes.
558 addr4 = (struct sockaddr_in *)&tmp_iface.sockaddr;
559 p4 = (struct iface_info_ipv4 *)p->Buffer;
560 addr4->sin_family = AF_INET;
561 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
563 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
564 addr4->sin_port = cpu_to_be16(CIFS_PORT);
566 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
570 addr6 = (struct sockaddr_in6 *)&tmp_iface.sockaddr;
571 p6 = (struct iface_info_ipv6 *)p->Buffer;
572 addr6->sin6_family = AF_INET6;
573 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
575 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
576 addr6->sin6_flowinfo = 0;
577 addr6->sin6_scope_id = 0;
578 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
580 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
585 "%s: skipping unsupported socket family\n",
591 * The iface_list is assumed to be sorted by speed.
592 * Check if the new interface exists in that list.
593 * NEVER change iface. it could be in use.
594 * Add a new one instead
596 spin_lock(&ses->iface_lock);
597 iface = niface = NULL;
598 list_for_each_entry_safe(iface, niface, &ses->iface_list,
600 ret = iface_cmp(iface, &tmp_iface);
602 /* just get a ref so that it doesn't get picked/freed */
603 iface->is_active = 1;
604 kref_get(&iface->refcount);
605 spin_unlock(&ses->iface_lock);
607 } else if (ret < 0) {
608 /* all remaining ifaces are slower */
609 kref_get(&iface->refcount);
613 spin_unlock(&ses->iface_lock);
615 /* no match. insert the entry in the list */
616 info = kmalloc(sizeof(struct cifs_server_iface),
622 memcpy(info, &tmp_iface, sizeof(tmp_iface));
624 /* add this new entry to the list */
625 kref_init(&info->refcount);
628 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, ses->iface_count);
629 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
630 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
631 le32_to_cpu(p->Capability));
633 spin_lock(&ses->iface_lock);
634 if (!list_entry_is_head(iface, &ses->iface_list, iface_head)) {
635 list_add_tail(&info->iface_head, &iface->iface_head);
636 kref_put(&iface->refcount, release_iface);
638 list_add_tail(&info->iface_head, &ses->iface_list);
639 spin_unlock(&ses->iface_lock);
642 ses->iface_last_update = jiffies;
645 next = le32_to_cpu(p->Next);
647 bytes_left -= sizeof(*p);
650 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
655 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
660 /* Azure rounds the buffer size up 8, to a 16 byte boundary */
661 if ((bytes_left > 8) || p->Next)
662 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
665 if (!ses->iface_count) {
675 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
678 unsigned int ret_data_len = 0;
679 struct network_interface_info_ioctl_rsp *out_buf = NULL;
680 struct cifs_ses *ses = tcon->ses;
682 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
683 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
684 NULL /* no data input */, 0 /* no data input */,
685 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
686 if (rc == -EOPNOTSUPP) {
688 "server does not support query network interfaces\n");
690 } else if (rc != 0) {
691 cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
695 rc = parse_server_interfaces(out_buf, ret_data_len, ses);
705 smb2_close_cached_fid(struct kref *ref)
707 struct cached_fid *cfid = container_of(ref, struct cached_fid,
709 struct cached_dirent *dirent, *q;
711 if (cfid->is_valid) {
712 cifs_dbg(FYI, "clear cached root file handle\n");
713 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
714 cfid->fid->volatile_fid);
718 * We only check validity above to send SMB2_close,
719 * but we still need to invalidate these entries
720 * when this function is called
722 cfid->is_valid = false;
723 cfid->file_all_info_is_valid = false;
724 cfid->has_lease = false;
730 * Delete all cached dirent names
732 mutex_lock(&cfid->dirents.de_mutex);
733 list_for_each_entry_safe(dirent, q, &cfid->dirents.entries, entry) {
734 list_del(&dirent->entry);
738 cfid->dirents.is_valid = 0;
739 cfid->dirents.is_failed = 0;
740 cfid->dirents.ctx = NULL;
741 cfid->dirents.pos = 0;
742 mutex_unlock(&cfid->dirents.de_mutex);
746 void close_cached_dir(struct cached_fid *cfid)
748 mutex_lock(&cfid->fid_mutex);
749 kref_put(&cfid->refcount, smb2_close_cached_fid);
750 mutex_unlock(&cfid->fid_mutex);
753 void close_cached_dir_lease_locked(struct cached_fid *cfid)
755 if (cfid->has_lease) {
756 cfid->has_lease = false;
757 kref_put(&cfid->refcount, smb2_close_cached_fid);
761 void close_cached_dir_lease(struct cached_fid *cfid)
763 mutex_lock(&cfid->fid_mutex);
764 close_cached_dir_lease_locked(cfid);
765 mutex_unlock(&cfid->fid_mutex);
769 smb2_cached_lease_break(struct work_struct *work)
771 struct cached_fid *cfid = container_of(work,
772 struct cached_fid, lease_break);
774 close_cached_dir_lease(cfid);
778 * Open the and cache a directory handle.
779 * Only supported for the root handle.
780 * If error then *cfid is not initialized.
782 int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
784 struct cifs_sb_info *cifs_sb,
785 struct cached_fid **cfid)
787 struct cifs_ses *ses;
788 struct TCP_Server_Info *server;
789 struct cifs_open_parms oparms;
790 struct smb2_create_rsp *o_rsp = NULL;
791 struct smb2_query_info_rsp *qi_rsp = NULL;
793 struct smb_rqst rqst[2];
794 struct kvec rsp_iov[2];
795 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
796 struct kvec qi_iov[1];
798 __le16 utf16_path = 0; /* Null - since an open of top of share */
799 u8 oplock = SMB2_OPLOCK_LEVEL_II;
800 struct cifs_fid *pfid;
801 struct dentry *dentry;
803 if (tcon == NULL || tcon->nohandlecache ||
804 is_smb1_server(tcon->ses->server))
808 server = ses->server;
810 if (cifs_sb->root == NULL)
816 dentry = cifs_sb->root;
818 mutex_lock(&tcon->crfid.fid_mutex);
819 if (tcon->crfid.is_valid) {
820 cifs_dbg(FYI, "found a cached root file handle\n");
821 *cfid = &tcon->crfid;
822 kref_get(&tcon->crfid.refcount);
823 mutex_unlock(&tcon->crfid.fid_mutex);
828 * We do not hold the lock for the open because in case
829 * SMB2_open needs to reconnect, it will end up calling
830 * cifs_mark_open_files_invalid() which takes the lock again
831 * thus causing a deadlock
834 mutex_unlock(&tcon->crfid.fid_mutex);
836 if (smb3_encryption_required(tcon))
837 flags |= CIFS_TRANSFORM_REQ;
839 if (!server->ops->new_lease_key)
842 pfid = tcon->crfid.fid;
843 server->ops->new_lease_key(pfid);
845 memset(rqst, 0, sizeof(rqst));
846 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
847 memset(rsp_iov, 0, sizeof(rsp_iov));
850 memset(&open_iov, 0, sizeof(open_iov));
851 rqst[0].rq_iov = open_iov;
852 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
855 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_FILE);
856 oparms.desired_access = FILE_READ_ATTRIBUTES;
857 oparms.disposition = FILE_OPEN;
859 oparms.reconnect = false;
861 rc = SMB2_open_init(tcon, server,
862 &rqst[0], &oplock, &oparms, &utf16_path);
865 smb2_set_next_command(tcon, &rqst[0]);
867 memset(&qi_iov, 0, sizeof(qi_iov));
868 rqst[1].rq_iov = qi_iov;
871 rc = SMB2_query_info_init(tcon, server,
872 &rqst[1], COMPOUND_FID,
873 COMPOUND_FID, FILE_ALL_INFORMATION,
875 sizeof(struct smb2_file_all_info) +
876 PATH_MAX * 2, 0, NULL);
880 smb2_set_related(&rqst[1]);
882 rc = compound_send_recv(xid, ses, server,
884 resp_buftype, rsp_iov);
885 mutex_lock(&tcon->crfid.fid_mutex);
888 * Now we need to check again as the cached root might have
889 * been successfully re-opened from a concurrent process
892 if (tcon->crfid.is_valid) {
893 /* work was already done */
895 /* stash fids for close() later */
896 struct cifs_fid fid = {
897 .persistent_fid = pfid->persistent_fid,
898 .volatile_fid = pfid->volatile_fid,
902 * caller expects this func to set the fid in crfid to valid
903 * cached root, so increment the refcount.
905 kref_get(&tcon->crfid.refcount);
907 mutex_unlock(&tcon->crfid.fid_mutex);
910 /* close extra handle outside of crit sec */
911 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
917 /* Cached root is still invalid, continue normaly */
920 if (rc == -EREMCHG) {
921 tcon->need_reconnect = true;
922 pr_warn_once("server share %s deleted\n",
928 atomic_inc(&tcon->num_remote_opens);
930 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
931 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
932 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
933 #ifdef CONFIG_CIFS_DEBUG2
934 oparms.fid->mid = le64_to_cpu(o_rsp->hdr.MessageId);
935 #endif /* CIFS_DEBUG2 */
937 tcon->crfid.tcon = tcon;
938 tcon->crfid.is_valid = true;
939 tcon->crfid.dentry = dentry;
941 kref_init(&tcon->crfid.refcount);
943 /* BB TBD check to see if oplock level check can be removed below */
944 if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
946 * See commit 2f94a3125b87. Increment the refcount when we
947 * get a lease for root, release it if lease break occurs
949 kref_get(&tcon->crfid.refcount);
950 tcon->crfid.has_lease = true;
951 smb2_parse_contexts(server, o_rsp,
953 oparms.fid->lease_key, &oplock,
958 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
959 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
961 if (!smb2_validate_and_copy_iov(
962 le16_to_cpu(qi_rsp->OutputBufferOffset),
963 sizeof(struct smb2_file_all_info),
964 &rsp_iov[1], sizeof(struct smb2_file_all_info),
965 (char *)&tcon->crfid.file_all_info))
966 tcon->crfid.file_all_info_is_valid = true;
967 tcon->crfid.time = jiffies;
971 mutex_unlock(&tcon->crfid.fid_mutex);
973 SMB2_open_free(&rqst[0]);
974 SMB2_query_info_free(&rqst[1]);
975 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
976 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
978 *cfid = &tcon->crfid;
982 int open_cached_dir_by_dentry(struct cifs_tcon *tcon,
983 struct dentry *dentry,
984 struct cached_fid **cfid)
986 mutex_lock(&tcon->crfid.fid_mutex);
987 if (tcon->crfid.dentry == dentry) {
988 cifs_dbg(FYI, "found a cached root file handle by dentry\n");
989 *cfid = &tcon->crfid;
990 kref_get(&tcon->crfid.refcount);
991 mutex_unlock(&tcon->crfid.fid_mutex);
994 mutex_unlock(&tcon->crfid.fid_mutex);
999 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
1000 struct cifs_sb_info *cifs_sb)
1003 __le16 srch_path = 0; /* Null - open root of share */
1004 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1005 struct cifs_open_parms oparms;
1006 struct cifs_fid fid;
1007 struct cached_fid *cfid = NULL;
1010 oparms.desired_access = FILE_READ_ATTRIBUTES;
1011 oparms.disposition = FILE_OPEN;
1012 oparms.create_options = cifs_create_options(cifs_sb, 0);
1014 oparms.reconnect = false;
1016 rc = open_cached_dir(xid, tcon, "", cifs_sb, &cfid);
1018 memcpy(&fid, cfid->fid, sizeof(struct cifs_fid));
1020 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
1025 SMB3_request_interfaces(xid, tcon);
1027 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1028 FS_ATTRIBUTE_INFORMATION);
1029 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1030 FS_DEVICE_INFORMATION);
1031 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1032 FS_VOLUME_INFORMATION);
1033 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1034 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
1036 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1038 close_cached_dir(cfid);
1042 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
1043 struct cifs_sb_info *cifs_sb)
1046 __le16 srch_path = 0; /* Null - open root of share */
1047 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1048 struct cifs_open_parms oparms;
1049 struct cifs_fid fid;
1052 oparms.desired_access = FILE_READ_ATTRIBUTES;
1053 oparms.disposition = FILE_OPEN;
1054 oparms.create_options = cifs_create_options(cifs_sb, 0);
1056 oparms.reconnect = false;
1058 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
1063 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1064 FS_ATTRIBUTE_INFORMATION);
1065 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1066 FS_DEVICE_INFORMATION);
1067 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1071 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
1072 struct cifs_sb_info *cifs_sb, const char *full_path)
1076 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1077 struct cifs_open_parms oparms;
1078 struct cifs_fid fid;
1080 if ((*full_path == 0) && tcon->crfid.is_valid)
1083 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1088 oparms.desired_access = FILE_READ_ATTRIBUTES;
1089 oparms.disposition = FILE_OPEN;
1090 oparms.create_options = cifs_create_options(cifs_sb, 0);
1092 oparms.reconnect = false;
1094 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
1101 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1107 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
1108 struct cifs_sb_info *cifs_sb, const char *full_path,
1109 u64 *uniqueid, FILE_ALL_INFO *data)
1111 *uniqueid = le64_to_cpu(data->IndexNumber);
1116 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
1117 struct cifs_fid *fid, FILE_ALL_INFO *data)
1120 struct smb2_file_all_info *smb2_data;
1122 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
1124 if (smb2_data == NULL)
1127 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
1130 move_smb2_info_to_cifs(data, smb2_data);
1135 #ifdef CONFIG_CIFS_XATTR
1137 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
1138 struct smb2_file_full_ea_info *src, size_t src_size,
1139 const unsigned char *ea_name)
1142 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
1144 size_t buf_size = dst_size;
1145 size_t name_len, value_len, user_name_len;
1147 while (src_size > 0) {
1148 name = &src->ea_data[0];
1149 name_len = (size_t)src->ea_name_length;
1150 value = &src->ea_data[src->ea_name_length + 1];
1151 value_len = (size_t)le16_to_cpu(src->ea_value_length);
1156 if (src_size < 8 + name_len + 1 + value_len) {
1157 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
1163 if (ea_name_len == name_len &&
1164 memcmp(ea_name, name, name_len) == 0) {
1168 if (dst_size < value_len) {
1172 memcpy(dst, value, value_len);
1176 /* 'user.' plus a terminating null */
1177 user_name_len = 5 + 1 + name_len;
1179 if (buf_size == 0) {
1180 /* skip copy - calc size only */
1181 rc += user_name_len;
1182 } else if (dst_size >= user_name_len) {
1183 dst_size -= user_name_len;
1184 memcpy(dst, "user.", 5);
1186 memcpy(dst, src->ea_data, name_len);
1190 rc += user_name_len;
1192 /* stop before overrun buffer */
1198 if (!src->next_entry_offset)
1201 if (src_size < le32_to_cpu(src->next_entry_offset)) {
1202 /* stop before overrun buffer */
1206 src_size -= le32_to_cpu(src->next_entry_offset);
1207 src = (void *)((char *)src +
1208 le32_to_cpu(src->next_entry_offset));
1211 /* didn't find the named attribute */
1220 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1221 const unsigned char *path, const unsigned char *ea_name,
1222 char *ea_data, size_t buf_size,
1223 struct cifs_sb_info *cifs_sb)
1226 struct kvec rsp_iov = {NULL, 0};
1227 int buftype = CIFS_NO_BUFFER;
1228 struct smb2_query_info_rsp *rsp;
1229 struct smb2_file_full_ea_info *info = NULL;
1231 rc = smb2_query_info_compound(xid, tcon, path,
1233 FILE_FULL_EA_INFORMATION,
1236 MAX_SMB2_CREATE_RESPONSE_SIZE -
1237 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1238 &rsp_iov, &buftype, cifs_sb);
1241 * If ea_name is NULL (listxattr) and there are no EAs,
1242 * return 0 as it's not an error. Otherwise, the specified
1243 * ea_name was not found.
1245 if (!ea_name && rc == -ENODATA)
1250 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1251 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1252 le32_to_cpu(rsp->OutputBufferLength),
1254 sizeof(struct smb2_file_full_ea_info));
1258 info = (struct smb2_file_full_ea_info *)(
1259 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1260 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1261 le32_to_cpu(rsp->OutputBufferLength), ea_name);
1264 free_rsp_buf(buftype, rsp_iov.iov_base);
1270 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1271 const char *path, const char *ea_name, const void *ea_value,
1272 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1273 struct cifs_sb_info *cifs_sb)
1275 struct cifs_ses *ses = tcon->ses;
1276 struct TCP_Server_Info *server = cifs_pick_channel(ses);
1277 __le16 *utf16_path = NULL;
1278 int ea_name_len = strlen(ea_name);
1279 int flags = CIFS_CP_CREATE_CLOSE_OP;
1281 struct smb_rqst rqst[3];
1282 int resp_buftype[3];
1283 struct kvec rsp_iov[3];
1284 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1285 struct cifs_open_parms oparms;
1286 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1287 struct cifs_fid fid;
1288 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1289 unsigned int size[1];
1291 struct smb2_file_full_ea_info *ea = NULL;
1292 struct kvec close_iov[1];
1293 struct smb2_query_info_rsp *rsp;
1294 int rc, used_len = 0;
1296 if (smb3_encryption_required(tcon))
1297 flags |= CIFS_TRANSFORM_REQ;
1299 if (ea_name_len > 255)
1302 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1306 memset(rqst, 0, sizeof(rqst));
1307 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1308 memset(rsp_iov, 0, sizeof(rsp_iov));
1310 if (ses->server->ops->query_all_EAs) {
1312 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1318 /* If we are adding a attribute we should first check
1319 * if there will be enough space available to store
1320 * the new EA. If not we should not add it since we
1321 * would not be able to even read the EAs back.
1323 rc = smb2_query_info_compound(xid, tcon, path,
1325 FILE_FULL_EA_INFORMATION,
1328 MAX_SMB2_CREATE_RESPONSE_SIZE -
1329 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1330 &rsp_iov[1], &resp_buftype[1], cifs_sb);
1332 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1333 used_len = le32_to_cpu(rsp->OutputBufferLength);
1335 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1336 resp_buftype[1] = CIFS_NO_BUFFER;
1337 memset(&rsp_iov[1], 0, sizeof(rsp_iov[1]));
1340 /* Use a fudge factor of 256 bytes in case we collide
1341 * with a different set_EAs command.
1343 if(CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1344 MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
1345 used_len + ea_name_len + ea_value_len + 1) {
1353 memset(&open_iov, 0, sizeof(open_iov));
1354 rqst[0].rq_iov = open_iov;
1355 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1357 memset(&oparms, 0, sizeof(oparms));
1359 oparms.desired_access = FILE_WRITE_EA;
1360 oparms.disposition = FILE_OPEN;
1361 oparms.create_options = cifs_create_options(cifs_sb, 0);
1363 oparms.reconnect = false;
1365 rc = SMB2_open_init(tcon, server,
1366 &rqst[0], &oplock, &oparms, utf16_path);
1369 smb2_set_next_command(tcon, &rqst[0]);
1373 memset(&si_iov, 0, sizeof(si_iov));
1374 rqst[1].rq_iov = si_iov;
1375 rqst[1].rq_nvec = 1;
1377 len = sizeof(*ea) + ea_name_len + ea_value_len + 1;
1378 ea = kzalloc(len, GFP_KERNEL);
1384 ea->ea_name_length = ea_name_len;
1385 ea->ea_value_length = cpu_to_le16(ea_value_len);
1386 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1387 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1392 rc = SMB2_set_info_init(tcon, server,
1393 &rqst[1], COMPOUND_FID,
1394 COMPOUND_FID, current->tgid,
1395 FILE_FULL_EA_INFORMATION,
1396 SMB2_O_INFO_FILE, 0, data, size);
1397 smb2_set_next_command(tcon, &rqst[1]);
1398 smb2_set_related(&rqst[1]);
1402 memset(&close_iov, 0, sizeof(close_iov));
1403 rqst[2].rq_iov = close_iov;
1404 rqst[2].rq_nvec = 1;
1405 rc = SMB2_close_init(tcon, server,
1406 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1407 smb2_set_related(&rqst[2]);
1409 rc = compound_send_recv(xid, ses, server,
1411 resp_buftype, rsp_iov);
1412 /* no need to bump num_remote_opens because handle immediately closed */
1417 SMB2_open_free(&rqst[0]);
1418 SMB2_set_info_free(&rqst[1]);
1419 SMB2_close_free(&rqst[2]);
1420 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1421 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1422 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1428 smb2_can_echo(struct TCP_Server_Info *server)
1430 return server->echoes;
1434 smb2_clear_stats(struct cifs_tcon *tcon)
1438 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1439 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1440 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1445 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1447 seq_puts(m, "\n\tShare Capabilities:");
1448 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1449 seq_puts(m, " DFS,");
1450 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1451 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1452 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1453 seq_puts(m, " SCALEOUT,");
1454 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1455 seq_puts(m, " CLUSTER,");
1456 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1457 seq_puts(m, " ASYMMETRIC,");
1458 if (tcon->capabilities == 0)
1459 seq_puts(m, " None");
1460 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1461 seq_puts(m, " Aligned,");
1462 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1463 seq_puts(m, " Partition Aligned,");
1464 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1465 seq_puts(m, " SSD,");
1466 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1467 seq_puts(m, " TRIM-support,");
1469 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1470 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1471 if (tcon->perf_sector_size)
1472 seq_printf(m, "\tOptimal sector size: 0x%x",
1473 tcon->perf_sector_size);
1474 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1478 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1480 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1481 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1484 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1485 * totals (requests sent) since those SMBs are per-session not per tcon
1487 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1488 (long long)(tcon->bytes_read),
1489 (long long)(tcon->bytes_written));
1490 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1491 atomic_read(&tcon->num_local_opens),
1492 atomic_read(&tcon->num_remote_opens));
1493 seq_printf(m, "\nTreeConnects: %d total %d failed",
1494 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1495 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1496 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1497 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1498 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1499 seq_printf(m, "\nCreates: %d total %d failed",
1500 atomic_read(&sent[SMB2_CREATE_HE]),
1501 atomic_read(&failed[SMB2_CREATE_HE]));
1502 seq_printf(m, "\nCloses: %d total %d failed",
1503 atomic_read(&sent[SMB2_CLOSE_HE]),
1504 atomic_read(&failed[SMB2_CLOSE_HE]));
1505 seq_printf(m, "\nFlushes: %d total %d failed",
1506 atomic_read(&sent[SMB2_FLUSH_HE]),
1507 atomic_read(&failed[SMB2_FLUSH_HE]));
1508 seq_printf(m, "\nReads: %d total %d failed",
1509 atomic_read(&sent[SMB2_READ_HE]),
1510 atomic_read(&failed[SMB2_READ_HE]));
1511 seq_printf(m, "\nWrites: %d total %d failed",
1512 atomic_read(&sent[SMB2_WRITE_HE]),
1513 atomic_read(&failed[SMB2_WRITE_HE]));
1514 seq_printf(m, "\nLocks: %d total %d failed",
1515 atomic_read(&sent[SMB2_LOCK_HE]),
1516 atomic_read(&failed[SMB2_LOCK_HE]));
1517 seq_printf(m, "\nIOCTLs: %d total %d failed",
1518 atomic_read(&sent[SMB2_IOCTL_HE]),
1519 atomic_read(&failed[SMB2_IOCTL_HE]));
1520 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1521 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1522 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1523 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1524 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1525 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1526 seq_printf(m, "\nQueryInfos: %d total %d failed",
1527 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1528 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1529 seq_printf(m, "\nSetInfos: %d total %d failed",
1530 atomic_read(&sent[SMB2_SET_INFO_HE]),
1531 atomic_read(&failed[SMB2_SET_INFO_HE]));
1532 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1533 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1534 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1538 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1540 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1541 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1543 cfile->fid.persistent_fid = fid->persistent_fid;
1544 cfile->fid.volatile_fid = fid->volatile_fid;
1545 cfile->fid.access = fid->access;
1546 #ifdef CONFIG_CIFS_DEBUG2
1547 cfile->fid.mid = fid->mid;
1548 #endif /* CIFS_DEBUG2 */
1549 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1551 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1552 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1556 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1557 struct cifs_fid *fid)
1559 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1563 smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
1564 struct cifsFileInfo *cfile)
1566 struct smb2_file_network_open_info file_inf;
1567 struct inode *inode;
1570 rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
1571 cfile->fid.volatile_fid, &file_inf);
1575 inode = d_inode(cfile->dentry);
1577 spin_lock(&inode->i_lock);
1578 CIFS_I(inode)->time = jiffies;
1580 /* Creation time should not need to be updated on close */
1581 if (file_inf.LastWriteTime)
1582 inode->i_mtime = cifs_NTtimeToUnix(file_inf.LastWriteTime);
1583 if (file_inf.ChangeTime)
1584 inode->i_ctime = cifs_NTtimeToUnix(file_inf.ChangeTime);
1585 if (file_inf.LastAccessTime)
1586 inode->i_atime = cifs_NTtimeToUnix(file_inf.LastAccessTime);
1589 * i_blocks is not related to (i_size / i_blksize),
1590 * but instead 512 byte (2**9) size is required for
1591 * calculating num blocks.
1593 if (le64_to_cpu(file_inf.AllocationSize) > 4096)
1595 (512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
1597 /* End of file and Attributes should not have to be updated on close */
1598 spin_unlock(&inode->i_lock);
1602 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1603 u64 persistent_fid, u64 volatile_fid,
1604 struct copychunk_ioctl *pcchunk)
1607 unsigned int ret_data_len;
1608 struct resume_key_req *res_key;
1610 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1611 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1612 NULL, 0 /* no input */, CIFSMaxBufSize,
1613 (char **)&res_key, &ret_data_len);
1615 if (rc == -EOPNOTSUPP) {
1616 pr_warn_once("Server share %s does not support copy range\n", tcon->treeName);
1617 goto req_res_key_exit;
1619 cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1620 goto req_res_key_exit;
1622 if (ret_data_len < sizeof(struct resume_key_req)) {
1623 cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
1625 goto req_res_key_exit;
1627 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1635 struct smb_rqst rqst[3];
1636 struct kvec rsp_iov[3];
1637 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1638 struct kvec qi_iov[1];
1639 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1640 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1641 struct kvec close_iov[1];
1645 smb2_ioctl_query_info(const unsigned int xid,
1646 struct cifs_tcon *tcon,
1647 struct cifs_sb_info *cifs_sb,
1648 __le16 *path, int is_dir,
1651 struct iqi_vars *vars;
1652 struct smb_rqst *rqst;
1653 struct kvec *rsp_iov;
1654 struct cifs_ses *ses = tcon->ses;
1655 struct TCP_Server_Info *server = cifs_pick_channel(ses);
1656 char __user *arg = (char __user *)p;
1657 struct smb_query_info qi;
1658 struct smb_query_info __user *pqi;
1660 int flags = CIFS_CP_CREATE_CLOSE_OP;
1661 struct smb2_query_info_rsp *qi_rsp = NULL;
1662 struct smb2_ioctl_rsp *io_rsp = NULL;
1663 void *buffer = NULL;
1664 int resp_buftype[3];
1665 struct cifs_open_parms oparms;
1666 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1667 struct cifs_fid fid;
1668 unsigned int size[2];
1670 int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
1671 void (*free_req1_func)(struct smb_rqst *r);
1673 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
1676 rqst = &vars->rqst[0];
1677 rsp_iov = &vars->rsp_iov[0];
1679 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1681 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info))) {
1685 if (qi.output_buffer_length > 1024) {
1690 if (!ses || !server) {
1695 if (smb3_encryption_required(tcon))
1696 flags |= CIFS_TRANSFORM_REQ;
1698 if (qi.output_buffer_length) {
1699 buffer = memdup_user(arg + sizeof(struct smb_query_info), qi.output_buffer_length);
1700 if (IS_ERR(buffer)) {
1701 rc = PTR_ERR(buffer);
1707 rqst[0].rq_iov = &vars->open_iov[0];
1708 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1710 memset(&oparms, 0, sizeof(oparms));
1712 oparms.disposition = FILE_OPEN;
1713 oparms.create_options = cifs_create_options(cifs_sb, create_options);
1715 oparms.reconnect = false;
1717 if (qi.flags & PASSTHRU_FSCTL) {
1718 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1719 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1720 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1722 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1723 oparms.desired_access = GENERIC_ALL;
1725 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1726 oparms.desired_access = GENERIC_READ;
1728 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1729 oparms.desired_access = GENERIC_WRITE;
1732 } else if (qi.flags & PASSTHRU_SET_INFO) {
1733 oparms.desired_access = GENERIC_WRITE;
1735 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1738 rc = SMB2_open_init(tcon, server,
1739 &rqst[0], &oplock, &oparms, path);
1741 goto free_output_buffer;
1742 smb2_set_next_command(tcon, &rqst[0]);
1745 if (qi.flags & PASSTHRU_FSCTL) {
1746 /* Can eventually relax perm check since server enforces too */
1747 if (!capable(CAP_SYS_ADMIN)) {
1751 rqst[1].rq_iov = &vars->io_iov[0];
1752 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1754 rc = SMB2_ioctl_init(tcon, server, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1755 qi.info_type, true, buffer, qi.output_buffer_length,
1756 CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1757 MAX_SMB2_CLOSE_RESPONSE_SIZE);
1758 free_req1_func = SMB2_ioctl_free;
1759 } else if (qi.flags == PASSTHRU_SET_INFO) {
1760 /* Can eventually relax perm check since server enforces too */
1761 if (!capable(CAP_SYS_ADMIN)) {
1765 if (qi.output_buffer_length < 8) {
1769 rqst[1].rq_iov = &vars->si_iov[0];
1770 rqst[1].rq_nvec = 1;
1772 /* MS-FSCC 2.4.13 FileEndOfFileInformation */
1776 rc = SMB2_set_info_init(tcon, server, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1777 current->tgid, FILE_END_OF_FILE_INFORMATION,
1778 SMB2_O_INFO_FILE, 0, data, size);
1779 free_req1_func = SMB2_set_info_free;
1780 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1781 rqst[1].rq_iov = &vars->qi_iov[0];
1782 rqst[1].rq_nvec = 1;
1784 rc = SMB2_query_info_init(tcon, server,
1785 &rqst[1], COMPOUND_FID,
1786 COMPOUND_FID, qi.file_info_class,
1787 qi.info_type, qi.additional_information,
1788 qi.input_buffer_length,
1789 qi.output_buffer_length, buffer);
1790 free_req1_func = SMB2_query_info_free;
1791 } else { /* unknown flags */
1792 cifs_tcon_dbg(VFS, "Invalid passthru query flags: 0x%x\n",
1799 smb2_set_next_command(tcon, &rqst[1]);
1800 smb2_set_related(&rqst[1]);
1803 rqst[2].rq_iov = &vars->close_iov[0];
1804 rqst[2].rq_nvec = 1;
1806 rc = SMB2_close_init(tcon, server,
1807 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1810 smb2_set_related(&rqst[2]);
1812 rc = compound_send_recv(xid, ses, server,
1814 resp_buftype, rsp_iov);
1818 /* No need to bump num_remote_opens since handle immediately closed */
1819 if (qi.flags & PASSTHRU_FSCTL) {
1820 pqi = (struct smb_query_info __user *)arg;
1821 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1822 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1823 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1824 if (qi.input_buffer_length > 0 &&
1825 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1826 > rsp_iov[1].iov_len) {
1831 if (copy_to_user(&pqi->input_buffer_length,
1832 &qi.input_buffer_length,
1833 sizeof(qi.input_buffer_length))) {
1838 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1839 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1840 qi.input_buffer_length))
1843 pqi = (struct smb_query_info __user *)arg;
1844 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1845 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1846 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1847 if (copy_to_user(&pqi->input_buffer_length,
1848 &qi.input_buffer_length,
1849 sizeof(qi.input_buffer_length))) {
1854 if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1855 qi.input_buffer_length))
1860 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1861 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1862 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1863 SMB2_close_free(&rqst[2]);
1865 free_req1_func(&rqst[1]);
1867 SMB2_open_free(&rqst[0]);
1876 smb2_copychunk_range(const unsigned int xid,
1877 struct cifsFileInfo *srcfile,
1878 struct cifsFileInfo *trgtfile, u64 src_off,
1879 u64 len, u64 dest_off)
1882 unsigned int ret_data_len;
1883 struct copychunk_ioctl *pcchunk;
1884 struct copychunk_ioctl_rsp *retbuf = NULL;
1885 struct cifs_tcon *tcon;
1886 int chunks_copied = 0;
1887 bool chunk_sizes_updated = false;
1888 ssize_t bytes_written, total_bytes_written = 0;
1889 struct inode *inode;
1891 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1894 * We need to flush all unwritten data before we can send the
1895 * copychunk ioctl to the server.
1897 inode = d_inode(trgtfile->dentry);
1898 filemap_write_and_wait(inode->i_mapping);
1900 if (pcchunk == NULL)
1903 cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
1904 /* Request a key from the server to identify the source of the copy */
1905 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1906 srcfile->fid.persistent_fid,
1907 srcfile->fid.volatile_fid, pcchunk);
1909 /* Note: request_res_key sets res_key null only if rc !=0 */
1913 /* For now array only one chunk long, will make more flexible later */
1914 pcchunk->ChunkCount = cpu_to_le32(1);
1915 pcchunk->Reserved = 0;
1916 pcchunk->Reserved2 = 0;
1918 tcon = tlink_tcon(trgtfile->tlink);
1921 pcchunk->SourceOffset = cpu_to_le64(src_off);
1922 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1924 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1926 /* Request server copy to target from src identified by key */
1929 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1930 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1931 true /* is_fsctl */, (char *)pcchunk,
1932 sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1933 (char **)&retbuf, &ret_data_len);
1936 sizeof(struct copychunk_ioctl_rsp)) {
1937 cifs_tcon_dbg(VFS, "Invalid cchunk response size\n");
1941 if (retbuf->TotalBytesWritten == 0) {
1942 cifs_dbg(FYI, "no bytes copied\n");
1947 * Check if server claimed to write more than we asked
1949 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1950 le32_to_cpu(pcchunk->Length)) {
1951 cifs_tcon_dbg(VFS, "Invalid copy chunk response\n");
1955 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1956 cifs_tcon_dbg(VFS, "Invalid num chunks written\n");
1962 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1963 src_off += bytes_written;
1964 dest_off += bytes_written;
1965 len -= bytes_written;
1966 total_bytes_written += bytes_written;
1968 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1969 le32_to_cpu(retbuf->ChunksWritten),
1970 le32_to_cpu(retbuf->ChunkBytesWritten),
1972 } else if (rc == -EINVAL) {
1973 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1976 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1977 le32_to_cpu(retbuf->ChunksWritten),
1978 le32_to_cpu(retbuf->ChunkBytesWritten),
1979 le32_to_cpu(retbuf->TotalBytesWritten));
1982 * Check if this is the first request using these sizes,
1983 * (ie check if copy succeed once with original sizes
1984 * and check if the server gave us different sizes after
1985 * we already updated max sizes on previous request).
1986 * if not then why is the server returning an error now
1988 if ((chunks_copied != 0) || chunk_sizes_updated)
1991 /* Check that server is not asking us to grow size */
1992 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1993 tcon->max_bytes_chunk)
1994 tcon->max_bytes_chunk =
1995 le32_to_cpu(retbuf->ChunkBytesWritten);
1997 goto cchunk_out; /* server gave us bogus size */
1999 /* No need to change MaxChunks since already set to 1 */
2000 chunk_sizes_updated = true;
2011 return total_bytes_written;
2015 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
2016 struct cifs_fid *fid)
2018 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2022 smb2_read_data_offset(char *buf)
2024 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
2026 return rsp->DataOffset;
2030 smb2_read_data_length(char *buf, bool in_remaining)
2032 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
2035 return le32_to_cpu(rsp->DataRemaining);
2037 return le32_to_cpu(rsp->DataLength);
2042 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
2043 struct cifs_io_parms *parms, unsigned int *bytes_read,
2044 char **buf, int *buf_type)
2046 parms->persistent_fid = pfid->persistent_fid;
2047 parms->volatile_fid = pfid->volatile_fid;
2048 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
2052 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
2053 struct cifs_io_parms *parms, unsigned int *written,
2054 struct kvec *iov, unsigned long nr_segs)
2057 parms->persistent_fid = pfid->persistent_fid;
2058 parms->volatile_fid = pfid->volatile_fid;
2059 return SMB2_write(xid, parms, written, iov, nr_segs);
2062 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
2063 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
2064 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
2066 struct cifsInodeInfo *cifsi;
2069 cifsi = CIFS_I(inode);
2071 /* if file already sparse don't bother setting sparse again */
2072 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
2073 return true; /* already sparse */
2075 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
2076 return true; /* already not sparse */
2079 * Can't check for sparse support on share the usual way via the
2080 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
2081 * since Samba server doesn't set the flag on the share, yet
2082 * supports the set sparse FSCTL and returns sparse correctly
2083 * in the file attributes. If we fail setting sparse though we
2084 * mark that server does not support sparse files for this share
2085 * to avoid repeatedly sending the unsupported fsctl to server
2086 * if the file is repeatedly extended.
2088 if (tcon->broken_sparse_sup)
2091 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2092 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
2094 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
2096 tcon->broken_sparse_sup = true;
2097 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
2102 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
2104 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
2110 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
2111 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
2113 __le64 eof = cpu_to_le64(size);
2114 struct inode *inode;
2117 * If extending file more than one page make sparse. Many Linux fs
2118 * make files sparse by default when extending via ftruncate
2120 inode = d_inode(cfile->dentry);
2122 if (!set_alloc && (size > inode->i_size + 8192)) {
2123 __u8 set_sparse = 1;
2125 /* whether set sparse succeeds or not, extend the file */
2126 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
2129 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
2130 cfile->fid.volatile_fid, cfile->pid, &eof);
2134 smb2_duplicate_extents(const unsigned int xid,
2135 struct cifsFileInfo *srcfile,
2136 struct cifsFileInfo *trgtfile, u64 src_off,
2137 u64 len, u64 dest_off)
2140 unsigned int ret_data_len;
2141 struct inode *inode;
2142 struct duplicate_extents_to_file dup_ext_buf;
2143 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
2145 /* server fileays advertise duplicate extent support with this flag */
2146 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
2147 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
2150 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
2151 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
2152 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
2153 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
2154 dup_ext_buf.ByteCount = cpu_to_le64(len);
2155 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
2156 src_off, dest_off, len);
2158 inode = d_inode(trgtfile->dentry);
2159 if (inode->i_size < dest_off + len) {
2160 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
2162 goto duplicate_extents_out;
2165 * Although also could set plausible allocation size (i_blocks)
2166 * here in addition to setting the file size, in reflink
2167 * it is likely that the target file is sparse. Its allocation
2168 * size will be queried on next revalidate, but it is important
2169 * to make sure that file's cached size is updated immediately
2171 cifs_setsize(inode, dest_off + len);
2173 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
2174 trgtfile->fid.volatile_fid,
2175 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
2176 true /* is_fsctl */,
2177 (char *)&dup_ext_buf,
2178 sizeof(struct duplicate_extents_to_file),
2179 CIFSMaxBufSize, NULL,
2182 if (ret_data_len > 0)
2183 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
2185 duplicate_extents_out:
2190 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2191 struct cifsFileInfo *cfile)
2193 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
2194 cfile->fid.volatile_fid);
2198 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
2199 struct cifsFileInfo *cfile)
2201 struct fsctl_set_integrity_information_req integr_info;
2202 unsigned int ret_data_len;
2204 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
2205 integr_info.Flags = 0;
2206 integr_info.Reserved = 0;
2208 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2209 cfile->fid.volatile_fid,
2210 FSCTL_SET_INTEGRITY_INFORMATION,
2211 true /* is_fsctl */,
2212 (char *)&integr_info,
2213 sizeof(struct fsctl_set_integrity_information_req),
2214 CIFSMaxBufSize, NULL,
2219 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
2220 #define GMT_TOKEN_SIZE 50
2222 #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
2225 * Input buffer contains (empty) struct smb_snapshot array with size filled in
2226 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
2229 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
2230 struct cifsFileInfo *cfile, void __user *ioc_buf)
2232 char *retbuf = NULL;
2233 unsigned int ret_data_len = 0;
2235 u32 max_response_size;
2236 struct smb_snapshot_array snapshot_in;
2239 * On the first query to enumerate the list of snapshots available
2240 * for this volume the buffer begins with 0 (number of snapshots
2241 * which can be returned is zero since at that point we do not know
2242 * how big the buffer needs to be). On the second query,
2243 * it (ret_data_len) is set to number of snapshots so we can
2244 * know to set the maximum response size larger (see below).
2246 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
2250 * Note that for snapshot queries that servers like Azure expect that
2251 * the first query be minimal size (and just used to get the number/size
2252 * of previous versions) so response size must be specified as EXACTLY
2253 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2256 if (ret_data_len == 0)
2257 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
2259 max_response_size = CIFSMaxBufSize;
2261 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2262 cfile->fid.volatile_fid,
2263 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
2264 true /* is_fsctl */,
2265 NULL, 0 /* no input data */, max_response_size,
2268 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2273 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2275 if (copy_from_user(&snapshot_in, ioc_buf,
2276 sizeof(struct smb_snapshot_array))) {
2283 * Check for min size, ie not large enough to fit even one GMT
2284 * token (snapshot). On the first ioctl some users may pass in
2285 * smaller size (or zero) to simply get the size of the array
2286 * so the user space caller can allocate sufficient memory
2287 * and retry the ioctl again with larger array size sufficient
2288 * to hold all of the snapshot GMT tokens on the second try.
2290 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2291 ret_data_len = sizeof(struct smb_snapshot_array);
2294 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2295 * the snapshot array (of 50 byte GMT tokens) each
2296 * representing an available previous version of the data
2298 if (ret_data_len > (snapshot_in.snapshot_array_size +
2299 sizeof(struct smb_snapshot_array)))
2300 ret_data_len = snapshot_in.snapshot_array_size +
2301 sizeof(struct smb_snapshot_array);
2303 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2314 smb3_notify(const unsigned int xid, struct file *pfile,
2315 void __user *ioc_buf)
2317 struct smb3_notify notify;
2318 struct dentry *dentry = pfile->f_path.dentry;
2319 struct inode *inode = file_inode(pfile);
2320 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2321 struct cifs_open_parms oparms;
2322 struct cifs_fid fid;
2323 struct cifs_tcon *tcon;
2324 const unsigned char *path;
2325 void *page = alloc_dentry_path();
2326 __le16 *utf16_path = NULL;
2327 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2330 path = build_path_from_dentry(dentry, page);
2336 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2337 if (utf16_path == NULL) {
2342 if (copy_from_user(¬ify, ioc_buf, sizeof(struct smb3_notify))) {
2347 tcon = cifs_sb_master_tcon(cifs_sb);
2349 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2350 oparms.disposition = FILE_OPEN;
2351 oparms.create_options = cifs_create_options(cifs_sb, 0);
2353 oparms.reconnect = false;
2355 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
2360 rc = SMB2_change_notify(xid, tcon, fid.persistent_fid, fid.volatile_fid,
2361 notify.watch_tree, notify.completion_filter);
2363 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2365 cifs_dbg(FYI, "change notify for path %s rc %d\n", path, rc);
2368 free_dentry_path(page);
2374 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2375 const char *path, struct cifs_sb_info *cifs_sb,
2376 struct cifs_fid *fid, __u16 search_flags,
2377 struct cifs_search_info *srch_inf)
2380 struct smb_rqst rqst[2];
2381 struct kvec rsp_iov[2];
2382 int resp_buftype[2];
2383 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2384 struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
2386 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2387 struct cifs_open_parms oparms;
2388 struct smb2_query_directory_rsp *qd_rsp = NULL;
2389 struct smb2_create_rsp *op_rsp = NULL;
2390 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
2391 int retry_count = 0;
2393 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2397 if (smb3_encryption_required(tcon))
2398 flags |= CIFS_TRANSFORM_REQ;
2400 memset(rqst, 0, sizeof(rqst));
2401 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2402 memset(rsp_iov, 0, sizeof(rsp_iov));
2405 memset(&open_iov, 0, sizeof(open_iov));
2406 rqst[0].rq_iov = open_iov;
2407 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2410 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2411 oparms.disposition = FILE_OPEN;
2412 oparms.create_options = cifs_create_options(cifs_sb, 0);
2414 oparms.reconnect = false;
2416 rc = SMB2_open_init(tcon, server,
2417 &rqst[0], &oplock, &oparms, utf16_path);
2420 smb2_set_next_command(tcon, &rqst[0]);
2422 /* Query directory */
2423 srch_inf->entries_in_buffer = 0;
2424 srch_inf->index_of_last_entry = 2;
2426 memset(&qd_iov, 0, sizeof(qd_iov));
2427 rqst[1].rq_iov = qd_iov;
2428 rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
2430 rc = SMB2_query_directory_init(xid, tcon, server,
2432 COMPOUND_FID, COMPOUND_FID,
2433 0, srch_inf->info_level);
2437 smb2_set_related(&rqst[1]);
2440 rc = compound_send_recv(xid, tcon->ses, server,
2442 resp_buftype, rsp_iov);
2444 if (rc == -EAGAIN && retry_count++ < 10)
2447 /* If the open failed there is nothing to do */
2448 op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
2449 if (op_rsp == NULL || op_rsp->hdr.Status != STATUS_SUCCESS) {
2450 cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
2453 fid->persistent_fid = op_rsp->PersistentFileId;
2454 fid->volatile_fid = op_rsp->VolatileFileId;
2456 /* Anything else than ENODATA means a genuine error */
2457 if (rc && rc != -ENODATA) {
2458 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2459 cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
2460 trace_smb3_query_dir_err(xid, fid->persistent_fid,
2461 tcon->tid, tcon->ses->Suid, 0, 0, rc);
2465 atomic_inc(&tcon->num_remote_opens);
2467 qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
2468 if (qd_rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2469 trace_smb3_query_dir_done(xid, fid->persistent_fid,
2470 tcon->tid, tcon->ses->Suid, 0, 0);
2471 srch_inf->endOfSearch = true;
2476 rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
2479 trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
2480 tcon->ses->Suid, 0, 0, rc);
2483 resp_buftype[1] = CIFS_NO_BUFFER;
2485 trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
2486 tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
2490 SMB2_open_free(&rqst[0]);
2491 SMB2_query_directory_free(&rqst[1]);
2492 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2493 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2498 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2499 struct cifs_fid *fid, __u16 search_flags,
2500 struct cifs_search_info *srch_inf)
2502 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2503 fid->volatile_fid, 0, srch_inf);
2507 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2508 struct cifs_fid *fid)
2510 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2514 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2515 * the number of credits and return true. Otherwise - return false.
2518 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
2520 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
2521 int scredits, in_flight;
2523 if (shdr->Status != STATUS_PENDING)
2526 if (shdr->CreditRequest) {
2527 spin_lock(&server->req_lock);
2528 server->credits += le16_to_cpu(shdr->CreditRequest);
2529 scredits = server->credits;
2530 in_flight = server->in_flight;
2531 spin_unlock(&server->req_lock);
2532 wake_up(&server->request_q);
2534 trace_smb3_pend_credits(server->CurrentMid,
2535 server->conn_id, server->hostname, scredits,
2536 le16_to_cpu(shdr->CreditRequest), in_flight);
2537 cifs_dbg(FYI, "%s: status pending add %u credits total=%d\n",
2538 __func__, le16_to_cpu(shdr->CreditRequest), scredits);
2545 smb2_is_session_expired(char *buf)
2547 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
2549 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2550 shdr->Status != STATUS_USER_SESSION_DELETED)
2553 trace_smb3_ses_expired(le32_to_cpu(shdr->Id.SyncId.TreeId),
2554 le64_to_cpu(shdr->SessionId),
2555 le16_to_cpu(shdr->Command),
2556 le64_to_cpu(shdr->MessageId));
2557 cifs_dbg(FYI, "Session expired or deleted\n");
2563 smb2_is_status_io_timeout(char *buf)
2565 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
2567 if (shdr->Status == STATUS_IO_TIMEOUT)
2574 smb2_is_network_name_deleted(char *buf, struct TCP_Server_Info *server)
2576 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
2577 struct list_head *tmp, *tmp1;
2578 struct cifs_ses *ses;
2579 struct cifs_tcon *tcon;
2581 if (shdr->Status != STATUS_NETWORK_NAME_DELETED)
2584 spin_lock(&cifs_tcp_ses_lock);
2585 list_for_each(tmp, &server->smb_ses_list) {
2586 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
2587 list_for_each(tmp1, &ses->tcon_list) {
2588 tcon = list_entry(tmp1, struct cifs_tcon, tcon_list);
2589 if (tcon->tid == le32_to_cpu(shdr->Id.SyncId.TreeId)) {
2590 tcon->need_reconnect = true;
2591 spin_unlock(&cifs_tcp_ses_lock);
2592 pr_warn_once("Server share %s deleted.\n",
2598 spin_unlock(&cifs_tcp_ses_lock);
2602 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2603 struct cifsInodeInfo *cinode)
2605 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2606 return SMB2_lease_break(0, tcon, cinode->lease_key,
2607 smb2_get_lease_state(cinode));
2609 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2611 CIFS_CACHE_READ(cinode) ? 1 : 0);
2615 smb2_set_related(struct smb_rqst *rqst)
2617 struct smb2_hdr *shdr;
2619 shdr = (struct smb2_hdr *)(rqst->rq_iov[0].iov_base);
2621 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2624 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2627 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2630 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2632 struct smb2_hdr *shdr;
2633 struct cifs_ses *ses = tcon->ses;
2634 struct TCP_Server_Info *server = ses->server;
2635 unsigned long len = smb_rqst_len(server, rqst);
2638 shdr = (struct smb2_hdr *)(rqst->rq_iov[0].iov_base);
2640 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2644 /* SMB headers in a compound are 8 byte aligned. */
2646 /* No padding needed */
2650 num_padding = 8 - (len & 7);
2651 if (!smb3_encryption_required(tcon)) {
2653 * If we do not have encryption then we can just add an extra
2654 * iov for the padding.
2656 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2657 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2662 * We can not add a small padding iov for the encryption case
2663 * because the encryption framework can not handle the padding
2665 * We have to flatten this into a single buffer and add
2666 * the padding to it.
2668 for (i = 1; i < rqst->rq_nvec; i++) {
2669 memcpy(rqst->rq_iov[0].iov_base +
2670 rqst->rq_iov[0].iov_len,
2671 rqst->rq_iov[i].iov_base,
2672 rqst->rq_iov[i].iov_len);
2673 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2675 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2677 rqst->rq_iov[0].iov_len += num_padding;
2683 shdr->NextCommand = cpu_to_le32(len);
2687 * Passes the query info response back to the caller on success.
2688 * Caller need to free this with free_rsp_buf().
2691 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2692 const char *path, u32 desired_access,
2693 u32 class, u32 type, u32 output_len,
2694 struct kvec *rsp, int *buftype,
2695 struct cifs_sb_info *cifs_sb)
2697 struct cifs_ses *ses = tcon->ses;
2698 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2699 int flags = CIFS_CP_CREATE_CLOSE_OP;
2700 struct smb_rqst rqst[3];
2701 int resp_buftype[3];
2702 struct kvec rsp_iov[3];
2703 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2704 struct kvec qi_iov[1];
2705 struct kvec close_iov[1];
2706 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2707 struct cifs_open_parms oparms;
2708 struct cifs_fid fid;
2711 struct cached_fid *cfid = NULL;
2715 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2719 if (smb3_encryption_required(tcon))
2720 flags |= CIFS_TRANSFORM_REQ;
2722 memset(rqst, 0, sizeof(rqst));
2723 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2724 memset(rsp_iov, 0, sizeof(rsp_iov));
2726 if (!strcmp(path, ""))
2727 open_cached_dir(xid, tcon, path, cifs_sb, &cfid); /* cfid null if open dir failed */
2729 memset(&open_iov, 0, sizeof(open_iov));
2730 rqst[0].rq_iov = open_iov;
2731 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2734 oparms.desired_access = desired_access;
2735 oparms.disposition = FILE_OPEN;
2736 oparms.create_options = cifs_create_options(cifs_sb, 0);
2738 oparms.reconnect = false;
2740 rc = SMB2_open_init(tcon, server,
2741 &rqst[0], &oplock, &oparms, utf16_path);
2744 smb2_set_next_command(tcon, &rqst[0]);
2746 memset(&qi_iov, 0, sizeof(qi_iov));
2747 rqst[1].rq_iov = qi_iov;
2748 rqst[1].rq_nvec = 1;
2751 rc = SMB2_query_info_init(tcon, server,
2753 cfid->fid->persistent_fid,
2754 cfid->fid->volatile_fid,
2759 rc = SMB2_query_info_init(tcon, server,
2770 smb2_set_next_command(tcon, &rqst[1]);
2771 smb2_set_related(&rqst[1]);
2774 memset(&close_iov, 0, sizeof(close_iov));
2775 rqst[2].rq_iov = close_iov;
2776 rqst[2].rq_nvec = 1;
2778 rc = SMB2_close_init(tcon, server,
2779 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2782 smb2_set_related(&rqst[2]);
2785 rc = compound_send_recv(xid, ses, server,
2787 &resp_buftype[1], &rsp_iov[1]);
2789 rc = compound_send_recv(xid, ses, server,
2791 resp_buftype, rsp_iov);
2794 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2795 if (rc == -EREMCHG) {
2796 tcon->need_reconnect = true;
2797 pr_warn_once("server share %s deleted\n",
2803 *buftype = resp_buftype[1];
2807 SMB2_open_free(&rqst[0]);
2808 SMB2_query_info_free(&rqst[1]);
2809 SMB2_close_free(&rqst[2]);
2810 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2811 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2813 close_cached_dir(cfid);
2818 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2819 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2821 struct smb2_query_info_rsp *rsp;
2822 struct smb2_fs_full_size_info *info = NULL;
2823 struct kvec rsp_iov = {NULL, 0};
2824 int buftype = CIFS_NO_BUFFER;
2828 rc = smb2_query_info_compound(xid, tcon, "",
2829 FILE_READ_ATTRIBUTES,
2830 FS_FULL_SIZE_INFORMATION,
2831 SMB2_O_INFO_FILESYSTEM,
2832 sizeof(struct smb2_fs_full_size_info),
2833 &rsp_iov, &buftype, cifs_sb);
2837 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2838 buf->f_type = SMB2_SUPER_MAGIC;
2839 info = (struct smb2_fs_full_size_info *)(
2840 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2841 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2842 le32_to_cpu(rsp->OutputBufferLength),
2844 sizeof(struct smb2_fs_full_size_info));
2846 smb2_copy_fs_info_to_kstatfs(info, buf);
2849 free_rsp_buf(buftype, rsp_iov.iov_base);
2854 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2855 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2858 __le16 srch_path = 0; /* Null - open root of share */
2859 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2860 struct cifs_open_parms oparms;
2861 struct cifs_fid fid;
2863 if (!tcon->posix_extensions)
2864 return smb2_queryfs(xid, tcon, cifs_sb, buf);
2867 oparms.desired_access = FILE_READ_ATTRIBUTES;
2868 oparms.disposition = FILE_OPEN;
2869 oparms.create_options = cifs_create_options(cifs_sb, 0);
2871 oparms.reconnect = false;
2873 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
2878 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2879 fid.volatile_fid, buf);
2880 buf->f_type = SMB2_SUPER_MAGIC;
2881 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2886 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2888 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2889 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2893 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2894 __u64 length, __u32 type, int lock, int unlock, bool wait)
2896 if (unlock && !lock)
2897 type = SMB2_LOCKFLAG_UNLOCK;
2898 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2899 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2900 current->tgid, length, offset, type, wait);
2904 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2906 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2910 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2912 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2916 smb2_new_lease_key(struct cifs_fid *fid)
2918 generate_random_uuid(fid->lease_key);
2922 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2923 const char *search_name,
2924 struct dfs_info3_param **target_nodes,
2925 unsigned int *num_of_nodes,
2926 const struct nls_table *nls_codepage, int remap)
2929 __le16 *utf16_path = NULL;
2930 int utf16_path_len = 0;
2931 struct cifs_tcon *tcon;
2932 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2933 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2934 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2935 int retry_count = 0;
2937 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
2940 * Try to use the IPC tcon, otherwise just use any
2942 tcon = ses->tcon_ipc;
2944 spin_lock(&cifs_tcp_ses_lock);
2945 tcon = list_first_entry_or_null(&ses->tcon_list,
2950 spin_unlock(&cifs_tcp_ses_lock);
2954 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2960 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2962 nls_codepage, remap);
2968 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2969 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2975 /* Highest DFS referral version understood */
2976 dfs_req->MaxReferralLevel = DFS_VERSION;
2978 /* Path to resolve in an UTF-16 null-terminated string */
2979 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2982 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2983 FSCTL_DFS_GET_REFERRALS,
2984 true /* is_fsctl */,
2985 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
2986 (char **)&dfs_rsp, &dfs_rsp_size);
2987 if (!is_retryable_error(rc))
2989 usleep_range(512, 2048);
2990 } while (++retry_count < 5);
2993 if (!is_retryable_error(rc) && rc != -ENOENT && rc != -EOPNOTSUPP)
2994 cifs_tcon_dbg(VFS, "%s: ioctl error: rc=%d\n", __func__, rc);
2998 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2999 num_of_nodes, target_nodes,
3000 nls_codepage, remap, search_name,
3001 true /* is_unicode */);
3003 cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
3008 if (tcon && !tcon->ipc) {
3009 /* ipc tcons are not refcounted */
3010 spin_lock(&cifs_tcp_ses_lock);
3012 /* tc_count can never go negative */
3013 WARN_ON(tcon->tc_count < 0);
3014 spin_unlock(&cifs_tcp_ses_lock);
3023 parse_reparse_posix(struct reparse_posix_data *symlink_buf,
3024 u32 plen, char **target_path,
3025 struct cifs_sb_info *cifs_sb)
3029 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
3030 len = le16_to_cpu(symlink_buf->ReparseDataLength);
3032 if (le64_to_cpu(symlink_buf->InodeType) != NFS_SPECFILE_LNK) {
3033 cifs_dbg(VFS, "%lld not a supported symlink type\n",
3034 le64_to_cpu(symlink_buf->InodeType));
3038 *target_path = cifs_strndup_from_utf16(
3039 symlink_buf->PathBuffer,
3040 len, true, cifs_sb->local_nls);
3041 if (!(*target_path))
3044 convert_delimiter(*target_path, '/');
3045 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
3051 parse_reparse_symlink(struct reparse_symlink_data_buffer *symlink_buf,
3052 u32 plen, char **target_path,
3053 struct cifs_sb_info *cifs_sb)
3055 unsigned int sub_len;
3056 unsigned int sub_offset;
3058 /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
3060 sub_offset = le16_to_cpu(symlink_buf->SubstituteNameOffset);
3061 sub_len = le16_to_cpu(symlink_buf->SubstituteNameLength);
3062 if (sub_offset + 20 > plen ||
3063 sub_offset + sub_len + 20 > plen) {
3064 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
3068 *target_path = cifs_strndup_from_utf16(
3069 symlink_buf->PathBuffer + sub_offset,
3070 sub_len, true, cifs_sb->local_nls);
3071 if (!(*target_path))
3074 convert_delimiter(*target_path, '/');
3075 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
3081 parse_reparse_point(struct reparse_data_buffer *buf,
3082 u32 plen, char **target_path,
3083 struct cifs_sb_info *cifs_sb)
3085 if (plen < sizeof(struct reparse_data_buffer)) {
3086 cifs_dbg(VFS, "reparse buffer is too small. Must be at least 8 bytes but was %d\n",
3091 if (plen < le16_to_cpu(buf->ReparseDataLength) +
3092 sizeof(struct reparse_data_buffer)) {
3093 cifs_dbg(VFS, "srv returned invalid reparse buf length: %d\n",
3098 /* See MS-FSCC 2.1.2 */
3099 switch (le32_to_cpu(buf->ReparseTag)) {
3100 case IO_REPARSE_TAG_NFS:
3101 return parse_reparse_posix(
3102 (struct reparse_posix_data *)buf,
3103 plen, target_path, cifs_sb);
3104 case IO_REPARSE_TAG_SYMLINK:
3105 return parse_reparse_symlink(
3106 (struct reparse_symlink_data_buffer *)buf,
3107 plen, target_path, cifs_sb);
3109 cifs_dbg(VFS, "srv returned unknown symlink buffer tag:0x%08x\n",
3110 le32_to_cpu(buf->ReparseTag));
3115 #define SMB2_SYMLINK_STRUCT_SIZE \
3116 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
3119 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
3120 struct cifs_sb_info *cifs_sb, const char *full_path,
3121 char **target_path, bool is_reparse_point)
3124 __le16 *utf16_path = NULL;
3125 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3126 struct cifs_open_parms oparms;
3127 struct cifs_fid fid;
3128 struct kvec err_iov = {NULL, 0};
3129 struct smb2_err_rsp *err_buf = NULL;
3130 struct smb2_symlink_err_rsp *symlink;
3131 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
3132 unsigned int sub_len;
3133 unsigned int sub_offset;
3134 unsigned int print_len;
3135 unsigned int print_offset;
3136 int flags = CIFS_CP_CREATE_CLOSE_OP;
3137 struct smb_rqst rqst[3];
3138 int resp_buftype[3];
3139 struct kvec rsp_iov[3];
3140 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
3141 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
3142 struct kvec close_iov[1];
3143 struct smb2_create_rsp *create_rsp;
3144 struct smb2_ioctl_rsp *ioctl_rsp;
3145 struct reparse_data_buffer *reparse_buf;
3146 int create_options = is_reparse_point ? OPEN_REPARSE_POINT : 0;
3149 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
3151 *target_path = NULL;
3153 if (smb3_encryption_required(tcon))
3154 flags |= CIFS_TRANSFORM_REQ;
3156 memset(rqst, 0, sizeof(rqst));
3157 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
3158 memset(rsp_iov, 0, sizeof(rsp_iov));
3160 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
3165 memset(&open_iov, 0, sizeof(open_iov));
3166 rqst[0].rq_iov = open_iov;
3167 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
3169 memset(&oparms, 0, sizeof(oparms));
3171 oparms.desired_access = FILE_READ_ATTRIBUTES;
3172 oparms.disposition = FILE_OPEN;
3173 oparms.create_options = cifs_create_options(cifs_sb, create_options);
3175 oparms.reconnect = false;
3177 rc = SMB2_open_init(tcon, server,
3178 &rqst[0], &oplock, &oparms, utf16_path);
3181 smb2_set_next_command(tcon, &rqst[0]);
3185 memset(&io_iov, 0, sizeof(io_iov));
3186 rqst[1].rq_iov = io_iov;
3187 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
3189 rc = SMB2_ioctl_init(tcon, server,
3190 &rqst[1], fid.persistent_fid,
3191 fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
3192 true /* is_fctl */, NULL, 0,
3194 MAX_SMB2_CREATE_RESPONSE_SIZE -
3195 MAX_SMB2_CLOSE_RESPONSE_SIZE);
3199 smb2_set_next_command(tcon, &rqst[1]);
3200 smb2_set_related(&rqst[1]);
3204 memset(&close_iov, 0, sizeof(close_iov));
3205 rqst[2].rq_iov = close_iov;
3206 rqst[2].rq_nvec = 1;
3208 rc = SMB2_close_init(tcon, server,
3209 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
3213 smb2_set_related(&rqst[2]);
3215 rc = compound_send_recv(xid, tcon->ses, server,
3217 resp_buftype, rsp_iov);
3219 create_rsp = rsp_iov[0].iov_base;
3220 if (create_rsp && create_rsp->hdr.Status)
3221 err_iov = rsp_iov[0];
3222 ioctl_rsp = rsp_iov[1].iov_base;
3225 * Open was successful and we got an ioctl response.
3227 if ((rc == 0) && (is_reparse_point)) {
3228 /* See MS-FSCC 2.3.23 */
3230 reparse_buf = (struct reparse_data_buffer *)
3231 ((char *)ioctl_rsp +
3232 le32_to_cpu(ioctl_rsp->OutputOffset));
3233 plen = le32_to_cpu(ioctl_rsp->OutputCount);
3235 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
3236 rsp_iov[1].iov_len) {
3237 cifs_tcon_dbg(VFS, "srv returned invalid ioctl len: %d\n",
3243 rc = parse_reparse_point(reparse_buf, plen, target_path,
3248 if (!rc || !err_iov.iov_base) {
3253 err_buf = err_iov.iov_base;
3254 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
3255 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
3260 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
3261 if (le32_to_cpu(symlink->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
3262 le32_to_cpu(symlink->ReparseTag) != IO_REPARSE_TAG_SYMLINK) {
3267 /* open must fail on symlink - reset rc */
3269 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
3270 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
3271 print_len = le16_to_cpu(symlink->PrintNameLength);
3272 print_offset = le16_to_cpu(symlink->PrintNameOffset);
3274 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
3279 if (err_iov.iov_len <
3280 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
3285 *target_path = cifs_strndup_from_utf16(
3286 (char *)symlink->PathBuffer + sub_offset,
3287 sub_len, true, cifs_sb->local_nls);
3288 if (!(*target_path)) {
3292 convert_delimiter(*target_path, '/');
3293 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
3296 cifs_dbg(FYI, "query symlink rc %d\n", rc);
3298 SMB2_open_free(&rqst[0]);
3299 SMB2_ioctl_free(&rqst[1]);
3300 SMB2_close_free(&rqst[2]);
3301 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
3302 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3303 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
3308 smb2_query_reparse_tag(const unsigned int xid, struct cifs_tcon *tcon,
3309 struct cifs_sb_info *cifs_sb, const char *full_path,
3313 __le16 *utf16_path = NULL;
3314 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3315 struct cifs_open_parms oparms;
3316 struct cifs_fid fid;
3317 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
3318 int flags = CIFS_CP_CREATE_CLOSE_OP;
3319 struct smb_rqst rqst[3];
3320 int resp_buftype[3];
3321 struct kvec rsp_iov[3];
3322 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
3323 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
3324 struct kvec close_iov[1];
3325 struct smb2_ioctl_rsp *ioctl_rsp;
3326 struct reparse_data_buffer *reparse_buf;
3329 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
3331 if (smb3_encryption_required(tcon))
3332 flags |= CIFS_TRANSFORM_REQ;
3334 memset(rqst, 0, sizeof(rqst));
3335 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
3336 memset(rsp_iov, 0, sizeof(rsp_iov));
3338 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
3343 * setup smb2open - TODO add optimization to call cifs_get_readable_path
3344 * to see if there is a handle already open that we can use
3346 memset(&open_iov, 0, sizeof(open_iov));
3347 rqst[0].rq_iov = open_iov;
3348 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
3350 memset(&oparms, 0, sizeof(oparms));
3352 oparms.desired_access = FILE_READ_ATTRIBUTES;
3353 oparms.disposition = FILE_OPEN;
3354 oparms.create_options = cifs_create_options(cifs_sb, OPEN_REPARSE_POINT);
3356 oparms.reconnect = false;
3358 rc = SMB2_open_init(tcon, server,
3359 &rqst[0], &oplock, &oparms, utf16_path);
3362 smb2_set_next_command(tcon, &rqst[0]);
3366 memset(&io_iov, 0, sizeof(io_iov));
3367 rqst[1].rq_iov = io_iov;
3368 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
3370 rc = SMB2_ioctl_init(tcon, server,
3371 &rqst[1], COMPOUND_FID,
3372 COMPOUND_FID, FSCTL_GET_REPARSE_POINT,
3373 true /* is_fctl */, NULL, 0,
3375 MAX_SMB2_CREATE_RESPONSE_SIZE -
3376 MAX_SMB2_CLOSE_RESPONSE_SIZE);
3380 smb2_set_next_command(tcon, &rqst[1]);
3381 smb2_set_related(&rqst[1]);
3385 memset(&close_iov, 0, sizeof(close_iov));
3386 rqst[2].rq_iov = close_iov;
3387 rqst[2].rq_nvec = 1;
3389 rc = SMB2_close_init(tcon, server,
3390 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
3394 smb2_set_related(&rqst[2]);
3396 rc = compound_send_recv(xid, tcon->ses, server,
3398 resp_buftype, rsp_iov);
3400 ioctl_rsp = rsp_iov[1].iov_base;
3403 * Open was successful and we got an ioctl response.
3406 /* See MS-FSCC 2.3.23 */
3408 reparse_buf = (struct reparse_data_buffer *)
3409 ((char *)ioctl_rsp +
3410 le32_to_cpu(ioctl_rsp->OutputOffset));
3411 plen = le32_to_cpu(ioctl_rsp->OutputCount);
3413 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
3414 rsp_iov[1].iov_len) {
3415 cifs_tcon_dbg(FYI, "srv returned invalid ioctl len: %d\n",
3420 *tag = le32_to_cpu(reparse_buf->ReparseTag);
3425 SMB2_open_free(&rqst[0]);
3426 SMB2_ioctl_free(&rqst[1]);
3427 SMB2_close_free(&rqst[2]);
3428 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
3429 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3430 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
3434 static struct cifs_ntsd *
3435 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
3436 const struct cifs_fid *cifsfid, u32 *pacllen, u32 info)
3438 struct cifs_ntsd *pntsd = NULL;
3440 int rc = -EOPNOTSUPP;
3441 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3444 return ERR_CAST(tlink);
3447 cifs_dbg(FYI, "trying to get acl\n");
3449 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
3450 cifsfid->volatile_fid, (void **)&pntsd, pacllen,
3454 cifs_put_tlink(tlink);
3456 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3463 static struct cifs_ntsd *
3464 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
3465 const char *path, u32 *pacllen, u32 info)
3467 struct cifs_ntsd *pntsd = NULL;
3468 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3471 struct cifs_tcon *tcon;
3472 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3473 struct cifs_fid fid;
3474 struct cifs_open_parms oparms;
3477 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
3479 return ERR_CAST(tlink);
3481 tcon = tlink_tcon(tlink);
3484 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3492 oparms.desired_access = READ_CONTROL;
3493 oparms.disposition = FILE_OPEN;
3495 * When querying an ACL, even if the file is a symlink we want to open
3496 * the source not the target, and so the protocol requires that the
3497 * client specify this flag when opening a reparse point
3499 oparms.create_options = cifs_create_options(cifs_sb, 0) | OPEN_REPARSE_POINT;
3501 oparms.reconnect = false;
3503 if (info & SACL_SECINFO)
3504 oparms.desired_access |= SYSTEM_SECURITY;
3506 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
3510 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3511 fid.volatile_fid, (void **)&pntsd, pacllen,
3513 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3516 cifs_put_tlink(tlink);
3519 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3526 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
3527 struct inode *inode, const char *path, int aclflag)
3529 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3531 int rc, access_flags = 0;
3532 struct cifs_tcon *tcon;
3533 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3534 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3535 struct cifs_fid fid;
3536 struct cifs_open_parms oparms;
3539 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
3541 return PTR_ERR(tlink);
3543 tcon = tlink_tcon(tlink);
3546 if (aclflag & CIFS_ACL_OWNER || aclflag & CIFS_ACL_GROUP)
3547 access_flags |= WRITE_OWNER;
3548 if (aclflag & CIFS_ACL_SACL)
3549 access_flags |= SYSTEM_SECURITY;
3550 if (aclflag & CIFS_ACL_DACL)
3551 access_flags |= WRITE_DAC;
3553 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3561 oparms.desired_access = access_flags;
3562 oparms.create_options = cifs_create_options(cifs_sb, 0);
3563 oparms.disposition = FILE_OPEN;
3566 oparms.reconnect = false;
3568 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
3572 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3573 fid.volatile_fid, pnntsd, acllen, aclflag);
3574 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3577 cifs_put_tlink(tlink);
3582 /* Retrieve an ACL from the server */
3583 static struct cifs_ntsd *
3584 get_smb2_acl(struct cifs_sb_info *cifs_sb,
3585 struct inode *inode, const char *path,
3586 u32 *pacllen, u32 info)
3588 struct cifs_ntsd *pntsd = NULL;
3589 struct cifsFileInfo *open_file = NULL;
3591 if (inode && !(info & SACL_SECINFO))
3592 open_file = find_readable_file(CIFS_I(inode), true);
3593 if (!open_file || (info & SACL_SECINFO))
3594 return get_smb2_acl_by_path(cifs_sb, path, pacllen, info);
3596 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen, info);
3597 cifsFileInfo_put(open_file);
3601 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3602 loff_t offset, loff_t len, bool keep_size)
3604 struct cifs_ses *ses = tcon->ses;
3605 struct inode *inode;
3606 struct cifsInodeInfo *cifsi;
3607 struct cifsFileInfo *cfile = file->private_data;
3608 struct file_zero_data_information fsctl_buf;
3615 inode = d_inode(cfile->dentry);
3616 cifsi = CIFS_I(inode);
3618 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3619 ses->Suid, offset, len);
3622 * We zero the range through ioctl, so we need remove the page caches
3623 * first, otherwise the data may be inconsistent with the server.
3625 truncate_pagecache_range(inode, offset, offset + len - 1);
3627 /* if file not oplocked can't be sure whether asking to extend size */
3628 if (!CIFS_CACHE_READ(cifsi))
3629 if (keep_size == false) {
3631 trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
3632 tcon->tid, ses->Suid, offset, len, rc);
3637 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3639 fsctl_buf.FileOffset = cpu_to_le64(offset);
3640 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3642 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3643 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
3645 sizeof(struct file_zero_data_information),
3648 goto zero_range_exit;
3651 * do we also need to change the size of the file?
3653 if (keep_size == false && i_size_read(inode) < offset + len) {
3654 eof = cpu_to_le64(offset + len);
3655 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3656 cfile->fid.volatile_fid, cfile->pid, &eof);
3662 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3663 ses->Suid, offset, len, rc);
3665 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3666 ses->Suid, offset, len);
3670 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3671 loff_t offset, loff_t len)
3673 struct inode *inode;
3674 struct cifsFileInfo *cfile = file->private_data;
3675 struct file_zero_data_information fsctl_buf;
3678 __u8 set_sparse = 1;
3682 inode = d_inode(cfile->dentry);
3684 /* Need to make file sparse, if not already, before freeing range. */
3685 /* Consider adding equivalent for compressed since it could also work */
3686 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3692 filemap_invalidate_lock(inode->i_mapping);
3694 * We implement the punch hole through ioctl, so we need remove the page
3695 * caches first, otherwise the data may be inconsistent with the server.
3697 truncate_pagecache_range(inode, offset, offset + len - 1);
3699 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3701 fsctl_buf.FileOffset = cpu_to_le64(offset);
3702 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3704 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3705 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3706 true /* is_fctl */, (char *)&fsctl_buf,
3707 sizeof(struct file_zero_data_information),
3708 CIFSMaxBufSize, NULL, NULL);
3710 filemap_invalidate_unlock(inode->i_mapping);
3714 static int smb3_simple_fallocate_write_range(unsigned int xid,
3715 struct cifs_tcon *tcon,
3716 struct cifsFileInfo *cfile,
3717 loff_t off, loff_t len,
3720 struct cifs_io_parms io_parms = {0};
3725 io_parms.netfid = cfile->fid.netfid;
3726 io_parms.pid = current->tgid;
3727 io_parms.tcon = tcon;
3728 io_parms.persistent_fid = cfile->fid.persistent_fid;
3729 io_parms.volatile_fid = cfile->fid.volatile_fid;
3732 io_parms.offset = off;
3733 io_parms.length = len;
3734 if (io_parms.length > SMB2_MAX_BUFFER_SIZE)
3735 io_parms.length = SMB2_MAX_BUFFER_SIZE;
3736 /* iov[0] is reserved for smb header */
3737 iov[1].iov_base = buf;
3738 iov[1].iov_len = io_parms.length;
3739 rc = SMB2_write(xid, &io_parms, &nbytes, iov, 1);
3751 static int smb3_simple_fallocate_range(unsigned int xid,
3752 struct cifs_tcon *tcon,
3753 struct cifsFileInfo *cfile,
3754 loff_t off, loff_t len)
3756 struct file_allocated_range_buffer in_data, *out_data = NULL, *tmp_data;
3762 in_data.file_offset = cpu_to_le64(off);
3763 in_data.length = cpu_to_le64(len);
3764 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3765 cfile->fid.volatile_fid,
3766 FSCTL_QUERY_ALLOCATED_RANGES, true,
3767 (char *)&in_data, sizeof(in_data),
3768 1024 * sizeof(struct file_allocated_range_buffer),
3769 (char **)&out_data, &out_data_len);
3773 buf = kzalloc(1024 * 1024, GFP_KERNEL);
3779 tmp_data = out_data;
3782 * The rest of the region is unmapped so write it all.
3784 if (out_data_len == 0) {
3785 rc = smb3_simple_fallocate_write_range(xid, tcon,
3786 cfile, off, len, buf);
3790 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3795 if (off < le64_to_cpu(tmp_data->file_offset)) {
3797 * We are at a hole. Write until the end of the region
3798 * or until the next allocated data,
3799 * whichever comes next.
3801 l = le64_to_cpu(tmp_data->file_offset) - off;
3804 rc = smb3_simple_fallocate_write_range(xid, tcon,
3805 cfile, off, l, buf);
3814 * We are at a section of allocated data, just skip forward
3815 * until the end of the data or the end of the region
3816 * we are supposed to fallocate, whichever comes first.
3818 l = le64_to_cpu(tmp_data->length);
3824 tmp_data = &tmp_data[1];
3825 out_data_len -= sizeof(struct file_allocated_range_buffer);
3835 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3836 loff_t off, loff_t len, bool keep_size)
3838 struct inode *inode;
3839 struct cifsInodeInfo *cifsi;
3840 struct cifsFileInfo *cfile = file->private_data;
3841 long rc = -EOPNOTSUPP;
3847 inode = d_inode(cfile->dentry);
3848 cifsi = CIFS_I(inode);
3850 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3851 tcon->ses->Suid, off, len);
3852 /* if file not oplocked can't be sure whether asking to extend size */
3853 if (!CIFS_CACHE_READ(cifsi))
3854 if (keep_size == false) {
3855 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3856 tcon->tid, tcon->ses->Suid, off, len, rc);
3862 * Extending the file
3864 if ((keep_size == false) && i_size_read(inode) < off + len) {
3865 rc = inode_newsize_ok(inode, off + len);
3869 if (cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)
3870 smb2_set_sparse(xid, tcon, cfile, inode, false);
3872 eof = cpu_to_le64(off + len);
3873 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3874 cfile->fid.volatile_fid, cfile->pid, &eof);
3876 cifsi->server_eof = off + len;
3877 cifs_setsize(inode, off + len);
3878 cifs_truncate_page(inode->i_mapping, inode->i_size);
3879 truncate_setsize(inode, off + len);
3885 * Files are non-sparse by default so falloc may be a no-op
3886 * Must check if file sparse. If not sparse, and since we are not
3887 * extending then no need to do anything since file already allocated
3889 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
3894 if (keep_size == true) {
3896 * We can not preallocate pages beyond the end of the file
3899 if (off >= i_size_read(inode)) {
3904 * For fallocates that are partially beyond the end of file,
3905 * clamp len so we only fallocate up to the end of file.
3907 if (off + len > i_size_read(inode)) {
3908 len = i_size_read(inode) - off;
3912 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3914 * At this point, we are trying to fallocate an internal
3915 * regions of a sparse file. Since smb2 does not have a
3916 * fallocate command we have two otions on how to emulate this.
3917 * We can either turn the entire file to become non-sparse
3918 * which we only do if the fallocate is for virtually
3919 * the whole file, or we can overwrite the region with zeroes
3920 * using SMB2_write, which could be prohibitevly expensive
3924 * We are only trying to fallocate a small region so
3925 * just write it with zero.
3927 if (len <= 1024 * 1024) {
3928 rc = smb3_simple_fallocate_range(xid, tcon, cfile,
3934 * Check if falloc starts within first few pages of file
3935 * and ends within a few pages of the end of file to
3936 * ensure that most of file is being forced to be
3937 * fallocated now. If so then setting whole file sparse
3938 * ie potentially making a few extra pages at the beginning
3939 * or end of the file non-sparse via set_sparse is harmless.
3941 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3947 smb2_set_sparse(xid, tcon, cfile, inode, false);
3952 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3953 tcon->ses->Suid, off, len, rc);
3955 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3956 tcon->ses->Suid, off, len);
3962 static long smb3_collapse_range(struct file *file, struct cifs_tcon *tcon,
3963 loff_t off, loff_t len)
3967 struct inode *inode;
3968 struct cifsFileInfo *cfile = file->private_data;
3969 struct cifsInodeInfo *cifsi;
3974 inode = d_inode(cfile->dentry);
3975 cifsi = CIFS_I(inode);
3977 if (off >= i_size_read(inode) ||
3978 off + len >= i_size_read(inode)) {
3983 rc = smb2_copychunk_range(xid, cfile, cfile, off + len,
3984 i_size_read(inode) - off - len, off);
3988 eof = cpu_to_le64(i_size_read(inode) - len);
3989 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3990 cfile->fid.volatile_fid, cfile->pid, &eof);
3996 cifsi->server_eof = i_size_read(inode) - len;
3997 truncate_setsize(inode, cifsi->server_eof);
3998 fscache_resize_cookie(cifs_inode_cookie(inode), cifsi->server_eof);
4004 static long smb3_insert_range(struct file *file, struct cifs_tcon *tcon,
4005 loff_t off, loff_t len)
4009 struct cifsFileInfo *cfile = file->private_data;
4015 if (off >= i_size_read(file->f_inode)) {
4020 count = i_size_read(file->f_inode) - off;
4021 eof = cpu_to_le64(i_size_read(file->f_inode) + len);
4023 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
4024 cfile->fid.volatile_fid, cfile->pid, &eof);
4028 rc = smb2_copychunk_range(xid, cfile, cfile, off, count, off + len);
4032 rc = smb3_zero_range(file, tcon, off, len, 1);
4042 static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
4044 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
4045 struct cifsInodeInfo *cifsi;
4046 struct inode *inode;
4048 struct file_allocated_range_buffer in_data, *out_data = NULL;
4052 if (whence != SEEK_HOLE && whence != SEEK_DATA)
4053 return generic_file_llseek(file, offset, whence);
4055 inode = d_inode(cfile->dentry);
4056 cifsi = CIFS_I(inode);
4058 if (offset < 0 || offset >= i_size_read(inode))
4063 * We need to be sure that all dirty pages are written as they
4064 * might fill holes on the server.
4065 * Note that we also MUST flush any written pages since at least
4066 * some servers (Windows2016) will not reflect recent writes in
4067 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
4069 wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
4071 filemap_write_and_wait(inode->i_mapping);
4072 smb2_flush_file(xid, tcon, &wrcfile->fid);
4073 cifsFileInfo_put(wrcfile);
4076 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
4077 if (whence == SEEK_HOLE)
4078 offset = i_size_read(inode);
4082 in_data.file_offset = cpu_to_le64(offset);
4083 in_data.length = cpu_to_le64(i_size_read(inode));
4085 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
4086 cfile->fid.volatile_fid,
4087 FSCTL_QUERY_ALLOCATED_RANGES, true,
4088 (char *)&in_data, sizeof(in_data),
4089 sizeof(struct file_allocated_range_buffer),
4090 (char **)&out_data, &out_data_len);
4096 if (whence == SEEK_HOLE && out_data_len == 0)
4099 if (whence == SEEK_DATA && out_data_len == 0) {
4104 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
4108 if (whence == SEEK_DATA) {
4109 offset = le64_to_cpu(out_data->file_offset);
4112 if (offset < le64_to_cpu(out_data->file_offset))
4115 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
4121 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
4126 static int smb3_fiemap(struct cifs_tcon *tcon,
4127 struct cifsFileInfo *cfile,
4128 struct fiemap_extent_info *fei, u64 start, u64 len)
4131 struct file_allocated_range_buffer in_data, *out_data;
4133 int i, num, rc, flags, last_blob;
4136 rc = fiemap_prep(d_inode(cfile->dentry), fei, start, &len, 0);
4142 in_data.file_offset = cpu_to_le64(start);
4143 in_data.length = cpu_to_le64(len);
4145 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
4146 cfile->fid.volatile_fid,
4147 FSCTL_QUERY_ALLOCATED_RANGES, true,
4148 (char *)&in_data, sizeof(in_data),
4149 1024 * sizeof(struct file_allocated_range_buffer),
4150 (char **)&out_data, &out_data_len);
4159 if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) {
4163 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
4168 num = out_data_len / sizeof(struct file_allocated_range_buffer);
4169 for (i = 0; i < num; i++) {
4171 if (i == num - 1 && last_blob)
4172 flags |= FIEMAP_EXTENT_LAST;
4174 rc = fiemap_fill_next_extent(fei,
4175 le64_to_cpu(out_data[i].file_offset),
4176 le64_to_cpu(out_data[i].file_offset),
4177 le64_to_cpu(out_data[i].length),
4188 next = le64_to_cpu(out_data[num - 1].file_offset) +
4189 le64_to_cpu(out_data[num - 1].length);
4190 len = len - (next - start);
4201 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
4202 loff_t off, loff_t len)
4204 /* KEEP_SIZE already checked for by do_fallocate */
4205 if (mode & FALLOC_FL_PUNCH_HOLE)
4206 return smb3_punch_hole(file, tcon, off, len);
4207 else if (mode & FALLOC_FL_ZERO_RANGE) {
4208 if (mode & FALLOC_FL_KEEP_SIZE)
4209 return smb3_zero_range(file, tcon, off, len, true);
4210 return smb3_zero_range(file, tcon, off, len, false);
4211 } else if (mode == FALLOC_FL_KEEP_SIZE)
4212 return smb3_simple_falloc(file, tcon, off, len, true);
4213 else if (mode == FALLOC_FL_COLLAPSE_RANGE)
4214 return smb3_collapse_range(file, tcon, off, len);
4215 else if (mode == FALLOC_FL_INSERT_RANGE)
4216 return smb3_insert_range(file, tcon, off, len);
4218 return smb3_simple_falloc(file, tcon, off, len, false);
4224 smb2_downgrade_oplock(struct TCP_Server_Info *server,
4225 struct cifsInodeInfo *cinode, __u32 oplock,
4226 unsigned int epoch, bool *purge_cache)
4228 server->ops->set_oplock_level(cinode, oplock, 0, NULL);
4232 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
4233 unsigned int epoch, bool *purge_cache);
4236 smb3_downgrade_oplock(struct TCP_Server_Info *server,
4237 struct cifsInodeInfo *cinode, __u32 oplock,
4238 unsigned int epoch, bool *purge_cache)
4240 unsigned int old_state = cinode->oplock;
4241 unsigned int old_epoch = cinode->epoch;
4242 unsigned int new_state;
4244 if (epoch > old_epoch) {
4245 smb21_set_oplock_level(cinode, oplock, 0, NULL);
4246 cinode->epoch = epoch;
4249 new_state = cinode->oplock;
4250 *purge_cache = false;
4252 if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
4253 (new_state & CIFS_CACHE_READ_FLG) == 0)
4254 *purge_cache = true;
4255 else if (old_state == new_state && (epoch - old_epoch > 1))
4256 *purge_cache = true;
4260 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
4261 unsigned int epoch, bool *purge_cache)
4264 cinode->lease_granted = false;
4265 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
4267 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
4268 cinode->oplock = CIFS_CACHE_RHW_FLG;
4269 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
4270 &cinode->netfs.inode);
4271 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
4272 cinode->oplock = CIFS_CACHE_RW_FLG;
4273 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
4274 &cinode->netfs.inode);
4275 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
4276 cinode->oplock = CIFS_CACHE_READ_FLG;
4277 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
4278 &cinode->netfs.inode);
4284 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
4285 unsigned int epoch, bool *purge_cache)
4287 char message[5] = {0};
4288 unsigned int new_oplock = 0;
4291 cinode->lease_granted = true;
4292 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
4295 /* Check if the server granted an oplock rather than a lease */
4296 if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
4297 return smb2_set_oplock_level(cinode, oplock, epoch,
4300 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
4301 new_oplock |= CIFS_CACHE_READ_FLG;
4302 strcat(message, "R");
4304 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
4305 new_oplock |= CIFS_CACHE_HANDLE_FLG;
4306 strcat(message, "H");
4308 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
4309 new_oplock |= CIFS_CACHE_WRITE_FLG;
4310 strcat(message, "W");
4313 strncpy(message, "None", sizeof(message));
4315 cinode->oplock = new_oplock;
4316 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
4317 &cinode->netfs.inode);
4321 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
4322 unsigned int epoch, bool *purge_cache)
4324 unsigned int old_oplock = cinode->oplock;
4326 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
4329 *purge_cache = false;
4330 if (old_oplock == CIFS_CACHE_READ_FLG) {
4331 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
4332 (epoch - cinode->epoch > 0))
4333 *purge_cache = true;
4334 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
4335 (epoch - cinode->epoch > 1))
4336 *purge_cache = true;
4337 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
4338 (epoch - cinode->epoch > 1))
4339 *purge_cache = true;
4340 else if (cinode->oplock == 0 &&
4341 (epoch - cinode->epoch > 0))
4342 *purge_cache = true;
4343 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
4344 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
4345 (epoch - cinode->epoch > 0))
4346 *purge_cache = true;
4347 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
4348 (epoch - cinode->epoch > 1))
4349 *purge_cache = true;
4351 cinode->epoch = epoch;
4355 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
4357 smb2_is_read_op(__u32 oplock)
4359 return oplock == SMB2_OPLOCK_LEVEL_II;
4361 #endif /* CIFS_ALLOW_INSECURE_LEGACY */
4364 smb21_is_read_op(__u32 oplock)
4366 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
4367 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
4371 map_oplock_to_lease(u8 oplock)
4373 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
4374 return SMB2_LEASE_WRITE_CACHING_LE | SMB2_LEASE_READ_CACHING_LE;
4375 else if (oplock == SMB2_OPLOCK_LEVEL_II)
4376 return SMB2_LEASE_READ_CACHING_LE;
4377 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
4378 return SMB2_LEASE_HANDLE_CACHING_LE | SMB2_LEASE_READ_CACHING_LE |
4379 SMB2_LEASE_WRITE_CACHING_LE;
4384 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
4386 struct create_lease *buf;
4388 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
4392 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
4393 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
4395 buf->ccontext.DataOffset = cpu_to_le16(offsetof
4396 (struct create_lease, lcontext));
4397 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
4398 buf->ccontext.NameOffset = cpu_to_le16(offsetof
4399 (struct create_lease, Name));
4400 buf->ccontext.NameLength = cpu_to_le16(4);
4401 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4410 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
4412 struct create_lease_v2 *buf;
4414 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
4418 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
4419 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
4421 buf->ccontext.DataOffset = cpu_to_le16(offsetof
4422 (struct create_lease_v2, lcontext));
4423 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
4424 buf->ccontext.NameOffset = cpu_to_le16(offsetof
4425 (struct create_lease_v2, Name));
4426 buf->ccontext.NameLength = cpu_to_le16(4);
4427 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4436 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
4438 struct create_lease *lc = (struct create_lease *)buf;
4440 *epoch = 0; /* not used */
4441 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE)
4442 return SMB2_OPLOCK_LEVEL_NOCHANGE;
4443 return le32_to_cpu(lc->lcontext.LeaseState);
4447 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
4449 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
4451 *epoch = le16_to_cpu(lc->lcontext.Epoch);
4452 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE)
4453 return SMB2_OPLOCK_LEVEL_NOCHANGE;
4455 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
4456 return le32_to_cpu(lc->lcontext.LeaseState);
4460 smb2_wp_retry_size(struct inode *inode)
4462 return min_t(unsigned int, CIFS_SB(inode->i_sb)->ctx->wsize,
4463 SMB2_MAX_BUFFER_SIZE);
4467 smb2_dir_needs_close(struct cifsFileInfo *cfile)
4469 return !cfile->invalidHandle;
4473 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
4474 struct smb_rqst *old_rq, __le16 cipher_type)
4476 struct smb2_hdr *shdr =
4477 (struct smb2_hdr *)old_rq->rq_iov[0].iov_base;
4479 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
4480 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
4481 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
4482 tr_hdr->Flags = cpu_to_le16(0x01);
4483 if ((cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4484 (cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4485 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
4487 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
4488 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
4491 /* We can not use the normal sg_set_buf() as we will sometimes pass a
4492 * stack object as buf.
4494 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
4495 unsigned int buflen)
4499 * VMAP_STACK (at least) puts stack into the vmalloc address space
4501 if (is_vmalloc_addr(buf))
4502 addr = vmalloc_to_page(buf);
4504 addr = virt_to_page(buf);
4505 sg_set_page(sg, addr, buflen, offset_in_page(buf));
4508 /* Assumes the first rqst has a transform header as the first iov.
4510 * rqst[0].rq_iov[0] is transform header
4511 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
4512 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
4514 static struct scatterlist *
4515 init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
4517 unsigned int sg_len;
4518 struct scatterlist *sg;
4521 unsigned int idx = 0;
4525 for (i = 0; i < num_rqst; i++)
4526 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
4528 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
4532 sg_init_table(sg, sg_len);
4533 for (i = 0; i < num_rqst; i++) {
4534 for (j = 0; j < rqst[i].rq_nvec; j++) {
4536 * The first rqst has a transform header where the
4537 * first 20 bytes are not part of the encrypted blob
4539 skip = (i == 0) && (j == 0) ? 20 : 0;
4540 smb2_sg_set_buf(&sg[idx++],
4541 rqst[i].rq_iov[j].iov_base + skip,
4542 rqst[i].rq_iov[j].iov_len - skip);
4545 for (j = 0; j < rqst[i].rq_npages; j++) {
4546 unsigned int len, offset;
4548 rqst_page_get_length(&rqst[i], j, &len, &offset);
4549 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
4552 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
4557 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
4559 struct cifs_ses *ses;
4562 spin_lock(&cifs_tcp_ses_lock);
4563 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
4564 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
4565 if (ses->Suid == ses_id) {
4566 ses_enc_key = enc ? ses->smb3encryptionkey :
4567 ses->smb3decryptionkey;
4568 memcpy(key, ses_enc_key, SMB3_ENC_DEC_KEY_SIZE);
4569 spin_unlock(&cifs_tcp_ses_lock);
4574 spin_unlock(&cifs_tcp_ses_lock);
4579 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
4580 * iov[0] - transform header (associate data),
4581 * iov[1-N] - SMB2 header and pages - data to encrypt.
4582 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
4586 crypt_message(struct TCP_Server_Info *server, int num_rqst,
4587 struct smb_rqst *rqst, int enc)
4589 struct smb2_transform_hdr *tr_hdr =
4590 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
4591 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
4593 struct scatterlist *sg;
4594 u8 sign[SMB2_SIGNATURE_SIZE] = {};
4595 u8 key[SMB3_ENC_DEC_KEY_SIZE];
4596 struct aead_request *req;
4598 unsigned int iv_len;
4599 DECLARE_CRYPTO_WAIT(wait);
4600 struct crypto_aead *tfm;
4601 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4603 rc = smb2_get_enc_key(server, le64_to_cpu(tr_hdr->SessionId), enc, key);
4605 cifs_server_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
4610 rc = smb3_crypto_aead_allocate(server);
4612 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
4616 tfm = enc ? server->secmech.ccmaesencrypt :
4617 server->secmech.ccmaesdecrypt;
4619 if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
4620 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4621 rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
4623 rc = crypto_aead_setkey(tfm, key, SMB3_GCM128_CRYPTKEY_SIZE);
4626 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
4630 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
4632 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
4636 req = aead_request_alloc(tfm, GFP_KERNEL);
4638 cifs_server_dbg(VFS, "%s: Failed to alloc aead request\n", __func__);
4643 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
4644 crypt_len += SMB2_SIGNATURE_SIZE;
4647 sg = init_sg(num_rqst, rqst, sign);
4649 cifs_server_dbg(VFS, "%s: Failed to init sg\n", __func__);
4654 iv_len = crypto_aead_ivsize(tfm);
4655 iv = kzalloc(iv_len, GFP_KERNEL);
4657 cifs_server_dbg(VFS, "%s: Failed to alloc iv\n", __func__);
4662 if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4663 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4664 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
4667 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
4670 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
4671 aead_request_set_ad(req, assoc_data_len);
4673 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
4674 crypto_req_done, &wait);
4676 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
4677 : crypto_aead_decrypt(req), &wait);
4680 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
4691 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
4695 for (i = 0; i < num_rqst; i++) {
4696 if (rqst[i].rq_pages) {
4697 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
4698 put_page(rqst[i].rq_pages[j]);
4699 kfree(rqst[i].rq_pages);
4705 * This function will initialize new_rq and encrypt the content.
4706 * The first entry, new_rq[0], only contains a single iov which contains
4707 * a smb2_transform_hdr and is pre-allocated by the caller.
4708 * This function then populates new_rq[1+] with the content from olq_rq[0+].
4710 * The end result is an array of smb_rqst structures where the first structure
4711 * only contains a single iov for the transform header which we then can pass
4712 * to crypt_message().
4714 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
4715 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
4718 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
4719 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
4721 struct page **pages;
4722 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
4723 unsigned int npages;
4724 unsigned int orig_len = 0;
4728 for (i = 1; i < num_rqst; i++) {
4729 npages = old_rq[i - 1].rq_npages;
4730 pages = kmalloc_array(npages, sizeof(struct page *),
4735 new_rq[i].rq_pages = pages;
4736 new_rq[i].rq_npages = npages;
4737 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
4738 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
4739 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
4740 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
4741 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
4743 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
4745 for (j = 0; j < npages; j++) {
4746 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4751 /* copy pages form the old */
4752 for (j = 0; j < npages; j++) {
4754 unsigned int offset, len;
4756 rqst_page_get_length(&new_rq[i], j, &len, &offset);
4758 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
4759 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
4761 memcpy(dst, src, len);
4762 kunmap(new_rq[i].rq_pages[j]);
4763 kunmap(old_rq[i - 1].rq_pages[j]);
4767 /* fill the 1st iov with a transform header */
4768 fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
4770 rc = crypt_message(server, num_rqst, new_rq, 1);
4771 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
4778 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
4783 smb3_is_transform_hdr(void *buf)
4785 struct smb2_transform_hdr *trhdr = buf;
4787 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
4791 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
4792 unsigned int buf_data_size, struct page **pages,
4793 unsigned int npages, unsigned int page_data_size,
4797 struct smb_rqst rqst = {NULL};
4800 iov[0].iov_base = buf;
4801 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
4802 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
4803 iov[1].iov_len = buf_data_size;
4807 rqst.rq_pages = pages;
4808 rqst.rq_npages = npages;
4809 rqst.rq_pagesz = PAGE_SIZE;
4810 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
4812 rc = crypt_message(server, 1, &rqst, 0);
4813 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
4818 memmove(buf, iov[1].iov_base, buf_data_size);
4821 server->total_read = buf_data_size + page_data_size;
4827 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
4828 unsigned int npages, unsigned int len)
4833 for (i = 0; i < npages; i++) {
4834 struct page *page = pages[i];
4838 if (len >= PAGE_SIZE) {
4839 /* enough data to fill the page */
4843 zero_user(page, len, PAGE_SIZE - len);
4846 length = cifs_read_page_from_socket(server, page, 0, n);
4849 server->total_read += length;
4856 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
4857 unsigned int cur_off, struct bio_vec **page_vec)
4859 struct bio_vec *bvec;
4862 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
4866 for (i = 0; i < npages; i++) {
4867 bvec[i].bv_page = pages[i];
4868 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
4869 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
4870 data_size -= bvec[i].bv_len;
4873 if (data_size != 0) {
4874 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4884 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4885 char *buf, unsigned int buf_len, struct page **pages,
4886 unsigned int npages, unsigned int page_data_size,
4889 unsigned int data_offset;
4890 unsigned int data_len;
4891 unsigned int cur_off;
4892 unsigned int cur_page_idx;
4893 unsigned int pad_len;
4894 struct cifs_readdata *rdata = mid->callback_data;
4895 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
4896 struct bio_vec *bvec = NULL;
4897 struct iov_iter iter;
4900 bool use_rdma_mr = false;
4902 if (shdr->Command != SMB2_READ) {
4903 cifs_server_dbg(VFS, "only big read responses are supported\n");
4907 if (server->ops->is_session_expired &&
4908 server->ops->is_session_expired(buf)) {
4910 cifs_reconnect(server, true);
4914 if (server->ops->is_status_pending &&
4915 server->ops->is_status_pending(buf, server))
4918 /* set up first two iov to get credits */
4919 rdata->iov[0].iov_base = buf;
4920 rdata->iov[0].iov_len = 0;
4921 rdata->iov[1].iov_base = buf;
4922 rdata->iov[1].iov_len =
4923 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
4924 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4925 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4926 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4927 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4929 rdata->result = server->ops->map_error(buf, true);
4930 if (rdata->result != 0) {
4931 cifs_dbg(FYI, "%s: server returned error %d\n",
4932 __func__, rdata->result);
4933 /* normal error on read response */
4935 mid->mid_state = MID_RESPONSE_RECEIVED;
4937 dequeue_mid(mid, false);
4941 data_offset = server->ops->read_data_offset(buf);
4942 #ifdef CONFIG_CIFS_SMB_DIRECT
4943 use_rdma_mr = rdata->mr;
4945 data_len = server->ops->read_data_length(buf, use_rdma_mr);
4947 if (data_offset < server->vals->read_rsp_size) {
4949 * win2k8 sometimes sends an offset of 0 when the read
4950 * is beyond the EOF. Treat it as if the data starts just after
4953 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4954 __func__, data_offset);
4955 data_offset = server->vals->read_rsp_size;
4956 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4957 /* data_offset is beyond the end of smallbuf */
4958 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4959 __func__, data_offset);
4960 rdata->result = -EIO;
4962 mid->mid_state = MID_RESPONSE_MALFORMED;
4964 dequeue_mid(mid, rdata->result);
4968 pad_len = data_offset - server->vals->read_rsp_size;
4970 if (buf_len <= data_offset) {
4971 /* read response payload is in pages */
4972 cur_page_idx = pad_len / PAGE_SIZE;
4973 cur_off = pad_len % PAGE_SIZE;
4975 if (cur_page_idx != 0) {
4976 /* data offset is beyond the 1st page of response */
4977 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4978 __func__, data_offset);
4979 rdata->result = -EIO;
4981 mid->mid_state = MID_RESPONSE_MALFORMED;
4983 dequeue_mid(mid, rdata->result);
4987 if (data_len > page_data_size - pad_len) {
4988 /* data_len is corrupt -- discard frame */
4989 rdata->result = -EIO;
4991 mid->mid_state = MID_RESPONSE_MALFORMED;
4993 dequeue_mid(mid, rdata->result);
4997 rdata->result = init_read_bvec(pages, npages, page_data_size,
4999 if (rdata->result != 0) {
5001 mid->mid_state = MID_RESPONSE_MALFORMED;
5003 dequeue_mid(mid, rdata->result);
5007 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
5008 } else if (buf_len >= data_offset + data_len) {
5009 /* read response payload is in buf */
5010 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
5011 iov.iov_base = buf + data_offset;
5012 iov.iov_len = data_len;
5013 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
5015 /* read response payload cannot be in both buf and pages */
5016 WARN_ONCE(1, "buf can not contain only a part of read data");
5017 rdata->result = -EIO;
5019 mid->mid_state = MID_RESPONSE_MALFORMED;
5021 dequeue_mid(mid, rdata->result);
5025 length = rdata->copy_into_pages(server, rdata, &iter);
5033 mid->mid_state = MID_RESPONSE_RECEIVED;
5035 dequeue_mid(mid, false);
5039 struct smb2_decrypt_work {
5040 struct work_struct decrypt;
5041 struct TCP_Server_Info *server;
5042 struct page **ppages;
5044 unsigned int npages;
5049 static void smb2_decrypt_offload(struct work_struct *work)
5051 struct smb2_decrypt_work *dw = container_of(work,
5052 struct smb2_decrypt_work, decrypt);
5054 struct mid_q_entry *mid;
5056 rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
5057 dw->ppages, dw->npages, dw->len, true);
5059 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
5063 dw->server->lstrp = jiffies;
5064 mid = smb2_find_dequeue_mid(dw->server, dw->buf);
5066 cifs_dbg(FYI, "mid not found\n");
5068 mid->decrypted = true;
5069 rc = handle_read_data(dw->server, mid, dw->buf,
5070 dw->server->vals->read_rsp_size,
5071 dw->ppages, dw->npages, dw->len,
5074 #ifdef CONFIG_CIFS_STATS2
5075 mid->when_received = jiffies;
5077 if (dw->server->ops->is_network_name_deleted)
5078 dw->server->ops->is_network_name_deleted(dw->buf,
5083 spin_lock(&cifs_tcp_ses_lock);
5084 spin_lock(&GlobalMid_Lock);
5085 if (dw->server->tcpStatus == CifsNeedReconnect) {
5086 mid->mid_state = MID_RETRY_NEEDED;
5087 spin_unlock(&GlobalMid_Lock);
5088 spin_unlock(&cifs_tcp_ses_lock);
5091 mid->mid_state = MID_REQUEST_SUBMITTED;
5092 mid->mid_flags &= ~(MID_DELETED);
5093 list_add_tail(&mid->qhead,
5094 &dw->server->pending_mid_q);
5095 spin_unlock(&GlobalMid_Lock);
5096 spin_unlock(&cifs_tcp_ses_lock);
5099 cifs_mid_q_entry_release(mid);
5103 for (i = dw->npages-1; i >= 0; i--)
5104 put_page(dw->ppages[i]);
5107 cifs_small_buf_release(dw->buf);
5113 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
5116 char *buf = server->smallbuf;
5117 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
5118 unsigned int npages;
5119 struct page **pages;
5121 unsigned int buflen = server->pdu_size;
5124 struct smb2_decrypt_work *dw;
5127 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
5128 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
5130 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
5133 server->total_read += rc;
5135 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
5136 server->vals->read_rsp_size;
5137 npages = DIV_ROUND_UP(len, PAGE_SIZE);
5139 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
5145 for (; i < npages; i++) {
5146 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
5153 /* read read data into pages */
5154 rc = read_data_into_pages(server, pages, npages, len);
5158 rc = cifs_discard_remaining_data(server);
5163 * For large reads, offload to different thread for better performance,
5164 * use more cores decrypting which can be expensive
5167 if ((server->min_offload) && (server->in_flight > 1) &&
5168 (server->pdu_size >= server->min_offload)) {
5169 dw = kmalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
5171 goto non_offloaded_decrypt;
5173 dw->buf = server->smallbuf;
5174 server->smallbuf = (char *)cifs_small_buf_get();
5176 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
5178 dw->npages = npages;
5179 dw->server = server;
5182 queue_work(decrypt_wq, &dw->decrypt);
5183 *num_mids = 0; /* worker thread takes care of finding mid */
5187 non_offloaded_decrypt:
5188 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
5189 pages, npages, len, false);
5193 *mid = smb2_find_mid(server, buf);
5195 cifs_dbg(FYI, "mid not found\n");
5197 cifs_dbg(FYI, "mid found\n");
5198 (*mid)->decrypted = true;
5199 rc = handle_read_data(server, *mid, buf,
5200 server->vals->read_rsp_size,
5201 pages, npages, len, false);
5203 if (server->ops->is_network_name_deleted) {
5204 server->ops->is_network_name_deleted(buf,
5211 for (i = i - 1; i >= 0; i--)
5216 cifs_discard_remaining_data(server);
5221 receive_encrypted_standard(struct TCP_Server_Info *server,
5222 struct mid_q_entry **mids, char **bufs,
5226 char *buf = server->smallbuf;
5227 struct smb2_hdr *shdr;
5228 unsigned int pdu_length = server->pdu_size;
5229 unsigned int buf_size;
5230 struct mid_q_entry *mid_entry;
5232 char *next_buffer = NULL;
5236 /* switch to large buffer if too big for a small one */
5237 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
5238 server->large_buf = true;
5239 memcpy(server->bigbuf, buf, server->total_read);
5240 buf = server->bigbuf;
5243 /* now read the rest */
5244 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
5245 pdu_length - HEADER_SIZE(server) + 1);
5248 server->total_read += length;
5250 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
5251 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0, false);
5255 next_is_large = server->large_buf;
5257 shdr = (struct smb2_hdr *)buf;
5258 if (shdr->NextCommand) {
5260 next_buffer = (char *)cifs_buf_get();
5262 next_buffer = (char *)cifs_small_buf_get();
5264 buf + le32_to_cpu(shdr->NextCommand),
5265 pdu_length - le32_to_cpu(shdr->NextCommand));
5268 mid_entry = smb2_find_mid(server, buf);
5269 if (mid_entry == NULL)
5270 cifs_dbg(FYI, "mid not found\n");
5272 cifs_dbg(FYI, "mid found\n");
5273 mid_entry->decrypted = true;
5274 mid_entry->resp_buf_size = server->pdu_size;
5277 if (*num_mids >= MAX_COMPOUND) {
5278 cifs_server_dbg(VFS, "too many PDUs in compound\n");
5281 bufs[*num_mids] = buf;
5282 mids[(*num_mids)++] = mid_entry;
5284 if (mid_entry && mid_entry->handle)
5285 ret = mid_entry->handle(server, mid_entry);
5287 ret = cifs_handle_standard(server, mid_entry);
5289 if (ret == 0 && shdr->NextCommand) {
5290 pdu_length -= le32_to_cpu(shdr->NextCommand);
5291 server->large_buf = next_is_large;
5293 server->bigbuf = buf = next_buffer;
5295 server->smallbuf = buf = next_buffer;
5297 } else if (ret != 0) {
5299 * ret != 0 here means that we didn't get to handle_mid() thus
5300 * server->smallbuf and server->bigbuf are still valid. We need
5301 * to free next_buffer because it is not going to be used
5305 free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
5307 free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
5314 smb3_receive_transform(struct TCP_Server_Info *server,
5315 struct mid_q_entry **mids, char **bufs, int *num_mids)
5317 char *buf = server->smallbuf;
5318 unsigned int pdu_length = server->pdu_size;
5319 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
5320 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
5322 if (pdu_length < sizeof(struct smb2_transform_hdr) +
5323 sizeof(struct smb2_hdr)) {
5324 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
5326 cifs_reconnect(server, true);
5327 return -ECONNABORTED;
5330 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
5331 cifs_server_dbg(VFS, "Transform message is broken\n");
5332 cifs_reconnect(server, true);
5333 return -ECONNABORTED;
5336 /* TODO: add support for compounds containing READ. */
5337 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
5338 return receive_encrypted_read(server, &mids[0], num_mids);
5341 return receive_encrypted_standard(server, mids, bufs, num_mids);
5345 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
5347 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
5349 return handle_read_data(server, mid, buf, server->pdu_size,
5354 smb2_next_header(char *buf)
5356 struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
5357 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
5359 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
5360 return sizeof(struct smb2_transform_hdr) +
5361 le32_to_cpu(t_hdr->OriginalMessageSize);
5363 return le32_to_cpu(hdr->NextCommand);
5367 smb2_make_node(unsigned int xid, struct inode *inode,
5368 struct dentry *dentry, struct cifs_tcon *tcon,
5369 const char *full_path, umode_t mode, dev_t dev)
5371 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
5373 FILE_ALL_INFO *buf = NULL;
5374 struct cifs_io_parms io_parms = {0};
5376 struct cifs_fid fid;
5377 struct cifs_open_parms oparms;
5378 unsigned int bytes_written;
5379 struct win_dev *pdev;
5383 * Check if mounted with mount parm 'sfu' mount parm.
5384 * SFU emulation should work with all servers, but only
5385 * supports block and char device (no socket & fifo),
5386 * and was used by default in earlier versions of Windows
5388 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
5392 * TODO: Add ability to create instead via reparse point. Windows (e.g.
5393 * their current NFS server) uses this approach to expose special files
5394 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
5397 if (!S_ISCHR(mode) && !S_ISBLK(mode))
5400 cifs_dbg(FYI, "sfu compat create special file\n");
5402 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
5409 oparms.cifs_sb = cifs_sb;
5410 oparms.desired_access = GENERIC_WRITE;
5411 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
5412 CREATE_OPTION_SPECIAL);
5413 oparms.disposition = FILE_CREATE;
5414 oparms.path = full_path;
5416 oparms.reconnect = false;
5418 if (tcon->ses->server->oplocks)
5419 oplock = REQ_OPLOCK;
5422 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
5427 * BB Do not bother to decode buf since no local inode yet to put
5428 * timestamps in, but we can reuse it safely.
5431 pdev = (struct win_dev *)buf;
5432 io_parms.pid = current->tgid;
5433 io_parms.tcon = tcon;
5434 io_parms.offset = 0;
5435 io_parms.length = sizeof(struct win_dev);
5436 iov[1].iov_base = buf;
5437 iov[1].iov_len = sizeof(struct win_dev);
5438 if (S_ISCHR(mode)) {
5439 memcpy(pdev->type, "IntxCHR", 8);
5440 pdev->major = cpu_to_le64(MAJOR(dev));
5441 pdev->minor = cpu_to_le64(MINOR(dev));
5442 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
5443 &bytes_written, iov, 1);
5444 } else if (S_ISBLK(mode)) {
5445 memcpy(pdev->type, "IntxBLK", 8);
5446 pdev->major = cpu_to_le64(MAJOR(dev));
5447 pdev->minor = cpu_to_le64(MINOR(dev));
5448 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
5449 &bytes_written, iov, 1);
5451 tcon->ses->server->ops->close(xid, tcon, &fid);
5454 /* FIXME: add code here to set EAs */
5460 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
5461 struct smb_version_operations smb20_operations = {
5462 .compare_fids = smb2_compare_fids,
5463 .setup_request = smb2_setup_request,
5464 .setup_async_request = smb2_setup_async_request,
5465 .check_receive = smb2_check_receive,
5466 .add_credits = smb2_add_credits,
5467 .set_credits = smb2_set_credits,
5468 .get_credits_field = smb2_get_credits_field,
5469 .get_credits = smb2_get_credits,
5470 .wait_mtu_credits = cifs_wait_mtu_credits,
5471 .get_next_mid = smb2_get_next_mid,
5472 .revert_current_mid = smb2_revert_current_mid,
5473 .read_data_offset = smb2_read_data_offset,
5474 .read_data_length = smb2_read_data_length,
5475 .map_error = map_smb2_to_linux_error,
5476 .find_mid = smb2_find_mid,
5477 .check_message = smb2_check_message,
5478 .dump_detail = smb2_dump_detail,
5479 .clear_stats = smb2_clear_stats,
5480 .print_stats = smb2_print_stats,
5481 .is_oplock_break = smb2_is_valid_oplock_break,
5482 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5483 .downgrade_oplock = smb2_downgrade_oplock,
5484 .need_neg = smb2_need_neg,
5485 .negotiate = smb2_negotiate,
5486 .negotiate_wsize = smb2_negotiate_wsize,
5487 .negotiate_rsize = smb2_negotiate_rsize,
5488 .sess_setup = SMB2_sess_setup,
5489 .logoff = SMB2_logoff,
5490 .tree_connect = SMB2_tcon,
5491 .tree_disconnect = SMB2_tdis,
5492 .qfs_tcon = smb2_qfs_tcon,
5493 .is_path_accessible = smb2_is_path_accessible,
5494 .can_echo = smb2_can_echo,
5496 .query_path_info = smb2_query_path_info,
5497 .get_srv_inum = smb2_get_srv_inum,
5498 .query_file_info = smb2_query_file_info,
5499 .set_path_size = smb2_set_path_size,
5500 .set_file_size = smb2_set_file_size,
5501 .set_file_info = smb2_set_file_info,
5502 .set_compression = smb2_set_compression,
5503 .mkdir = smb2_mkdir,
5504 .mkdir_setinfo = smb2_mkdir_setinfo,
5505 .rmdir = smb2_rmdir,
5506 .unlink = smb2_unlink,
5507 .rename = smb2_rename_path,
5508 .create_hardlink = smb2_create_hardlink,
5509 .query_symlink = smb2_query_symlink,
5510 .query_mf_symlink = smb3_query_mf_symlink,
5511 .create_mf_symlink = smb3_create_mf_symlink,
5512 .open = smb2_open_file,
5513 .set_fid = smb2_set_fid,
5514 .close = smb2_close_file,
5515 .flush = smb2_flush_file,
5516 .async_readv = smb2_async_readv,
5517 .async_writev = smb2_async_writev,
5518 .sync_read = smb2_sync_read,
5519 .sync_write = smb2_sync_write,
5520 .query_dir_first = smb2_query_dir_first,
5521 .query_dir_next = smb2_query_dir_next,
5522 .close_dir = smb2_close_dir,
5523 .calc_smb_size = smb2_calc_size,
5524 .is_status_pending = smb2_is_status_pending,
5525 .is_session_expired = smb2_is_session_expired,
5526 .oplock_response = smb2_oplock_response,
5527 .queryfs = smb2_queryfs,
5528 .mand_lock = smb2_mand_lock,
5529 .mand_unlock_range = smb2_unlock_range,
5530 .push_mand_locks = smb2_push_mandatory_locks,
5531 .get_lease_key = smb2_get_lease_key,
5532 .set_lease_key = smb2_set_lease_key,
5533 .new_lease_key = smb2_new_lease_key,
5534 .calc_signature = smb2_calc_signature,
5535 .is_read_op = smb2_is_read_op,
5536 .set_oplock_level = smb2_set_oplock_level,
5537 .create_lease_buf = smb2_create_lease_buf,
5538 .parse_lease_buf = smb2_parse_lease_buf,
5539 .copychunk_range = smb2_copychunk_range,
5540 .wp_retry_size = smb2_wp_retry_size,
5541 .dir_needs_close = smb2_dir_needs_close,
5542 .get_dfs_refer = smb2_get_dfs_refer,
5543 .select_sectype = smb2_select_sectype,
5544 #ifdef CONFIG_CIFS_XATTR
5545 .query_all_EAs = smb2_query_eas,
5546 .set_EA = smb2_set_ea,
5547 #endif /* CIFS_XATTR */
5548 .get_acl = get_smb2_acl,
5549 .get_acl_by_fid = get_smb2_acl_by_fid,
5550 .set_acl = set_smb2_acl,
5551 .next_header = smb2_next_header,
5552 .ioctl_query_info = smb2_ioctl_query_info,
5553 .make_node = smb2_make_node,
5554 .fiemap = smb3_fiemap,
5555 .llseek = smb3_llseek,
5556 .is_status_io_timeout = smb2_is_status_io_timeout,
5557 .is_network_name_deleted = smb2_is_network_name_deleted,
5559 #endif /* CIFS_ALLOW_INSECURE_LEGACY */
5561 struct smb_version_operations smb21_operations = {
5562 .compare_fids = smb2_compare_fids,
5563 .setup_request = smb2_setup_request,
5564 .setup_async_request = smb2_setup_async_request,
5565 .check_receive = smb2_check_receive,
5566 .add_credits = smb2_add_credits,
5567 .set_credits = smb2_set_credits,
5568 .get_credits_field = smb2_get_credits_field,
5569 .get_credits = smb2_get_credits,
5570 .wait_mtu_credits = smb2_wait_mtu_credits,
5571 .adjust_credits = smb2_adjust_credits,
5572 .get_next_mid = smb2_get_next_mid,
5573 .revert_current_mid = smb2_revert_current_mid,
5574 .read_data_offset = smb2_read_data_offset,
5575 .read_data_length = smb2_read_data_length,
5576 .map_error = map_smb2_to_linux_error,
5577 .find_mid = smb2_find_mid,
5578 .check_message = smb2_check_message,
5579 .dump_detail = smb2_dump_detail,
5580 .clear_stats = smb2_clear_stats,
5581 .print_stats = smb2_print_stats,
5582 .is_oplock_break = smb2_is_valid_oplock_break,
5583 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5584 .downgrade_oplock = smb2_downgrade_oplock,
5585 .need_neg = smb2_need_neg,
5586 .negotiate = smb2_negotiate,
5587 .negotiate_wsize = smb2_negotiate_wsize,
5588 .negotiate_rsize = smb2_negotiate_rsize,
5589 .sess_setup = SMB2_sess_setup,
5590 .logoff = SMB2_logoff,
5591 .tree_connect = SMB2_tcon,
5592 .tree_disconnect = SMB2_tdis,
5593 .qfs_tcon = smb2_qfs_tcon,
5594 .is_path_accessible = smb2_is_path_accessible,
5595 .can_echo = smb2_can_echo,
5597 .query_path_info = smb2_query_path_info,
5598 .get_srv_inum = smb2_get_srv_inum,
5599 .query_file_info = smb2_query_file_info,
5600 .set_path_size = smb2_set_path_size,
5601 .set_file_size = smb2_set_file_size,
5602 .set_file_info = smb2_set_file_info,
5603 .set_compression = smb2_set_compression,
5604 .mkdir = smb2_mkdir,
5605 .mkdir_setinfo = smb2_mkdir_setinfo,
5606 .rmdir = smb2_rmdir,
5607 .unlink = smb2_unlink,
5608 .rename = smb2_rename_path,
5609 .create_hardlink = smb2_create_hardlink,
5610 .query_symlink = smb2_query_symlink,
5611 .query_mf_symlink = smb3_query_mf_symlink,
5612 .create_mf_symlink = smb3_create_mf_symlink,
5613 .open = smb2_open_file,
5614 .set_fid = smb2_set_fid,
5615 .close = smb2_close_file,
5616 .flush = smb2_flush_file,
5617 .async_readv = smb2_async_readv,
5618 .async_writev = smb2_async_writev,
5619 .sync_read = smb2_sync_read,
5620 .sync_write = smb2_sync_write,
5621 .query_dir_first = smb2_query_dir_first,
5622 .query_dir_next = smb2_query_dir_next,
5623 .close_dir = smb2_close_dir,
5624 .calc_smb_size = smb2_calc_size,
5625 .is_status_pending = smb2_is_status_pending,
5626 .is_session_expired = smb2_is_session_expired,
5627 .oplock_response = smb2_oplock_response,
5628 .queryfs = smb2_queryfs,
5629 .mand_lock = smb2_mand_lock,
5630 .mand_unlock_range = smb2_unlock_range,
5631 .push_mand_locks = smb2_push_mandatory_locks,
5632 .get_lease_key = smb2_get_lease_key,
5633 .set_lease_key = smb2_set_lease_key,
5634 .new_lease_key = smb2_new_lease_key,
5635 .calc_signature = smb2_calc_signature,
5636 .is_read_op = smb21_is_read_op,
5637 .set_oplock_level = smb21_set_oplock_level,
5638 .create_lease_buf = smb2_create_lease_buf,
5639 .parse_lease_buf = smb2_parse_lease_buf,
5640 .copychunk_range = smb2_copychunk_range,
5641 .wp_retry_size = smb2_wp_retry_size,
5642 .dir_needs_close = smb2_dir_needs_close,
5643 .enum_snapshots = smb3_enum_snapshots,
5644 .notify = smb3_notify,
5645 .get_dfs_refer = smb2_get_dfs_refer,
5646 .select_sectype = smb2_select_sectype,
5647 #ifdef CONFIG_CIFS_XATTR
5648 .query_all_EAs = smb2_query_eas,
5649 .set_EA = smb2_set_ea,
5650 #endif /* CIFS_XATTR */
5651 .get_acl = get_smb2_acl,
5652 .get_acl_by_fid = get_smb2_acl_by_fid,
5653 .set_acl = set_smb2_acl,
5654 .next_header = smb2_next_header,
5655 .ioctl_query_info = smb2_ioctl_query_info,
5656 .make_node = smb2_make_node,
5657 .fiemap = smb3_fiemap,
5658 .llseek = smb3_llseek,
5659 .is_status_io_timeout = smb2_is_status_io_timeout,
5660 .is_network_name_deleted = smb2_is_network_name_deleted,
5663 struct smb_version_operations smb30_operations = {
5664 .compare_fids = smb2_compare_fids,
5665 .setup_request = smb2_setup_request,
5666 .setup_async_request = smb2_setup_async_request,
5667 .check_receive = smb2_check_receive,
5668 .add_credits = smb2_add_credits,
5669 .set_credits = smb2_set_credits,
5670 .get_credits_field = smb2_get_credits_field,
5671 .get_credits = smb2_get_credits,
5672 .wait_mtu_credits = smb2_wait_mtu_credits,
5673 .adjust_credits = smb2_adjust_credits,
5674 .get_next_mid = smb2_get_next_mid,
5675 .revert_current_mid = smb2_revert_current_mid,
5676 .read_data_offset = smb2_read_data_offset,
5677 .read_data_length = smb2_read_data_length,
5678 .map_error = map_smb2_to_linux_error,
5679 .find_mid = smb2_find_mid,
5680 .check_message = smb2_check_message,
5681 .dump_detail = smb2_dump_detail,
5682 .clear_stats = smb2_clear_stats,
5683 .print_stats = smb2_print_stats,
5684 .dump_share_caps = smb2_dump_share_caps,
5685 .is_oplock_break = smb2_is_valid_oplock_break,
5686 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5687 .downgrade_oplock = smb3_downgrade_oplock,
5688 .need_neg = smb2_need_neg,
5689 .negotiate = smb2_negotiate,
5690 .negotiate_wsize = smb3_negotiate_wsize,
5691 .negotiate_rsize = smb3_negotiate_rsize,
5692 .sess_setup = SMB2_sess_setup,
5693 .logoff = SMB2_logoff,
5694 .tree_connect = SMB2_tcon,
5695 .tree_disconnect = SMB2_tdis,
5696 .qfs_tcon = smb3_qfs_tcon,
5697 .is_path_accessible = smb2_is_path_accessible,
5698 .can_echo = smb2_can_echo,
5700 .query_path_info = smb2_query_path_info,
5701 /* WSL tags introduced long after smb2.1, enable for SMB3, 3.11 only */
5702 .query_reparse_tag = smb2_query_reparse_tag,
5703 .get_srv_inum = smb2_get_srv_inum,
5704 .query_file_info = smb2_query_file_info,
5705 .set_path_size = smb2_set_path_size,
5706 .set_file_size = smb2_set_file_size,
5707 .set_file_info = smb2_set_file_info,
5708 .set_compression = smb2_set_compression,
5709 .mkdir = smb2_mkdir,
5710 .mkdir_setinfo = smb2_mkdir_setinfo,
5711 .rmdir = smb2_rmdir,
5712 .unlink = smb2_unlink,
5713 .rename = smb2_rename_path,
5714 .create_hardlink = smb2_create_hardlink,
5715 .query_symlink = smb2_query_symlink,
5716 .query_mf_symlink = smb3_query_mf_symlink,
5717 .create_mf_symlink = smb3_create_mf_symlink,
5718 .open = smb2_open_file,
5719 .set_fid = smb2_set_fid,
5720 .close = smb2_close_file,
5721 .close_getattr = smb2_close_getattr,
5722 .flush = smb2_flush_file,
5723 .async_readv = smb2_async_readv,
5724 .async_writev = smb2_async_writev,
5725 .sync_read = smb2_sync_read,
5726 .sync_write = smb2_sync_write,
5727 .query_dir_first = smb2_query_dir_first,
5728 .query_dir_next = smb2_query_dir_next,
5729 .close_dir = smb2_close_dir,
5730 .calc_smb_size = smb2_calc_size,
5731 .is_status_pending = smb2_is_status_pending,
5732 .is_session_expired = smb2_is_session_expired,
5733 .oplock_response = smb2_oplock_response,
5734 .queryfs = smb2_queryfs,
5735 .mand_lock = smb2_mand_lock,
5736 .mand_unlock_range = smb2_unlock_range,
5737 .push_mand_locks = smb2_push_mandatory_locks,
5738 .get_lease_key = smb2_get_lease_key,
5739 .set_lease_key = smb2_set_lease_key,
5740 .new_lease_key = smb2_new_lease_key,
5741 .generate_signingkey = generate_smb30signingkey,
5742 .calc_signature = smb3_calc_signature,
5743 .set_integrity = smb3_set_integrity,
5744 .is_read_op = smb21_is_read_op,
5745 .set_oplock_level = smb3_set_oplock_level,
5746 .create_lease_buf = smb3_create_lease_buf,
5747 .parse_lease_buf = smb3_parse_lease_buf,
5748 .copychunk_range = smb2_copychunk_range,
5749 .duplicate_extents = smb2_duplicate_extents,
5750 .validate_negotiate = smb3_validate_negotiate,
5751 .wp_retry_size = smb2_wp_retry_size,
5752 .dir_needs_close = smb2_dir_needs_close,
5753 .fallocate = smb3_fallocate,
5754 .enum_snapshots = smb3_enum_snapshots,
5755 .notify = smb3_notify,
5756 .init_transform_rq = smb3_init_transform_rq,
5757 .is_transform_hdr = smb3_is_transform_hdr,
5758 .receive_transform = smb3_receive_transform,
5759 .get_dfs_refer = smb2_get_dfs_refer,
5760 .select_sectype = smb2_select_sectype,
5761 #ifdef CONFIG_CIFS_XATTR
5762 .query_all_EAs = smb2_query_eas,
5763 .set_EA = smb2_set_ea,
5764 #endif /* CIFS_XATTR */
5765 .get_acl = get_smb2_acl,
5766 .get_acl_by_fid = get_smb2_acl_by_fid,
5767 .set_acl = set_smb2_acl,
5768 .next_header = smb2_next_header,
5769 .ioctl_query_info = smb2_ioctl_query_info,
5770 .make_node = smb2_make_node,
5771 .fiemap = smb3_fiemap,
5772 .llseek = smb3_llseek,
5773 .is_status_io_timeout = smb2_is_status_io_timeout,
5774 .is_network_name_deleted = smb2_is_network_name_deleted,
5777 struct smb_version_operations smb311_operations = {
5778 .compare_fids = smb2_compare_fids,
5779 .setup_request = smb2_setup_request,
5780 .setup_async_request = smb2_setup_async_request,
5781 .check_receive = smb2_check_receive,
5782 .add_credits = smb2_add_credits,
5783 .set_credits = smb2_set_credits,
5784 .get_credits_field = smb2_get_credits_field,
5785 .get_credits = smb2_get_credits,
5786 .wait_mtu_credits = smb2_wait_mtu_credits,
5787 .adjust_credits = smb2_adjust_credits,
5788 .get_next_mid = smb2_get_next_mid,
5789 .revert_current_mid = smb2_revert_current_mid,
5790 .read_data_offset = smb2_read_data_offset,
5791 .read_data_length = smb2_read_data_length,
5792 .map_error = map_smb2_to_linux_error,
5793 .find_mid = smb2_find_mid,
5794 .check_message = smb2_check_message,
5795 .dump_detail = smb2_dump_detail,
5796 .clear_stats = smb2_clear_stats,
5797 .print_stats = smb2_print_stats,
5798 .dump_share_caps = smb2_dump_share_caps,
5799 .is_oplock_break = smb2_is_valid_oplock_break,
5800 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5801 .downgrade_oplock = smb3_downgrade_oplock,
5802 .need_neg = smb2_need_neg,
5803 .negotiate = smb2_negotiate,
5804 .negotiate_wsize = smb3_negotiate_wsize,
5805 .negotiate_rsize = smb3_negotiate_rsize,
5806 .sess_setup = SMB2_sess_setup,
5807 .logoff = SMB2_logoff,
5808 .tree_connect = SMB2_tcon,
5809 .tree_disconnect = SMB2_tdis,
5810 .qfs_tcon = smb3_qfs_tcon,
5811 .is_path_accessible = smb2_is_path_accessible,
5812 .can_echo = smb2_can_echo,
5814 .query_path_info = smb2_query_path_info,
5815 .query_reparse_tag = smb2_query_reparse_tag,
5816 .get_srv_inum = smb2_get_srv_inum,
5817 .query_file_info = smb2_query_file_info,
5818 .set_path_size = smb2_set_path_size,
5819 .set_file_size = smb2_set_file_size,
5820 .set_file_info = smb2_set_file_info,
5821 .set_compression = smb2_set_compression,
5822 .mkdir = smb2_mkdir,
5823 .mkdir_setinfo = smb2_mkdir_setinfo,
5824 .posix_mkdir = smb311_posix_mkdir,
5825 .rmdir = smb2_rmdir,
5826 .unlink = smb2_unlink,
5827 .rename = smb2_rename_path,
5828 .create_hardlink = smb2_create_hardlink,
5829 .query_symlink = smb2_query_symlink,
5830 .query_mf_symlink = smb3_query_mf_symlink,
5831 .create_mf_symlink = smb3_create_mf_symlink,
5832 .open = smb2_open_file,
5833 .set_fid = smb2_set_fid,
5834 .close = smb2_close_file,
5835 .close_getattr = smb2_close_getattr,
5836 .flush = smb2_flush_file,
5837 .async_readv = smb2_async_readv,
5838 .async_writev = smb2_async_writev,
5839 .sync_read = smb2_sync_read,
5840 .sync_write = smb2_sync_write,
5841 .query_dir_first = smb2_query_dir_first,
5842 .query_dir_next = smb2_query_dir_next,
5843 .close_dir = smb2_close_dir,
5844 .calc_smb_size = smb2_calc_size,
5845 .is_status_pending = smb2_is_status_pending,
5846 .is_session_expired = smb2_is_session_expired,
5847 .oplock_response = smb2_oplock_response,
5848 .queryfs = smb311_queryfs,
5849 .mand_lock = smb2_mand_lock,
5850 .mand_unlock_range = smb2_unlock_range,
5851 .push_mand_locks = smb2_push_mandatory_locks,
5852 .get_lease_key = smb2_get_lease_key,
5853 .set_lease_key = smb2_set_lease_key,
5854 .new_lease_key = smb2_new_lease_key,
5855 .generate_signingkey = generate_smb311signingkey,
5856 .calc_signature = smb3_calc_signature,
5857 .set_integrity = smb3_set_integrity,
5858 .is_read_op = smb21_is_read_op,
5859 .set_oplock_level = smb3_set_oplock_level,
5860 .create_lease_buf = smb3_create_lease_buf,
5861 .parse_lease_buf = smb3_parse_lease_buf,
5862 .copychunk_range = smb2_copychunk_range,
5863 .duplicate_extents = smb2_duplicate_extents,
5864 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5865 .wp_retry_size = smb2_wp_retry_size,
5866 .dir_needs_close = smb2_dir_needs_close,
5867 .fallocate = smb3_fallocate,
5868 .enum_snapshots = smb3_enum_snapshots,
5869 .notify = smb3_notify,
5870 .init_transform_rq = smb3_init_transform_rq,
5871 .is_transform_hdr = smb3_is_transform_hdr,
5872 .receive_transform = smb3_receive_transform,
5873 .get_dfs_refer = smb2_get_dfs_refer,
5874 .select_sectype = smb2_select_sectype,
5875 #ifdef CONFIG_CIFS_XATTR
5876 .query_all_EAs = smb2_query_eas,
5877 .set_EA = smb2_set_ea,
5878 #endif /* CIFS_XATTR */
5879 .get_acl = get_smb2_acl,
5880 .get_acl_by_fid = get_smb2_acl_by_fid,
5881 .set_acl = set_smb2_acl,
5882 .next_header = smb2_next_header,
5883 .ioctl_query_info = smb2_ioctl_query_info,
5884 .make_node = smb2_make_node,
5885 .fiemap = smb3_fiemap,
5886 .llseek = smb3_llseek,
5887 .is_status_io_timeout = smb2_is_status_io_timeout,
5888 .is_network_name_deleted = smb2_is_network_name_deleted,
5891 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
5892 struct smb_version_values smb20_values = {
5893 .version_string = SMB20_VERSION_STRING,
5894 .protocol_id = SMB20_PROT_ID,
5895 .req_capabilities = 0, /* MBZ */
5896 .large_lock_type = 0,
5897 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5898 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5899 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5900 .header_size = sizeof(struct smb2_hdr),
5901 .header_preamble_size = 0,
5902 .max_header_size = MAX_SMB2_HDR_SIZE,
5903 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5904 .lock_cmd = SMB2_LOCK,
5906 .cap_nt_find = SMB2_NT_FIND,
5907 .cap_large_files = SMB2_LARGE_FILES,
5908 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5909 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5910 .create_lease_size = sizeof(struct create_lease),
5912 #endif /* ALLOW_INSECURE_LEGACY */
5914 struct smb_version_values smb21_values = {
5915 .version_string = SMB21_VERSION_STRING,
5916 .protocol_id = SMB21_PROT_ID,
5917 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5918 .large_lock_type = 0,
5919 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5920 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5921 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5922 .header_size = sizeof(struct smb2_hdr),
5923 .header_preamble_size = 0,
5924 .max_header_size = MAX_SMB2_HDR_SIZE,
5925 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5926 .lock_cmd = SMB2_LOCK,
5928 .cap_nt_find = SMB2_NT_FIND,
5929 .cap_large_files = SMB2_LARGE_FILES,
5930 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5931 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5932 .create_lease_size = sizeof(struct create_lease),
5935 struct smb_version_values smb3any_values = {
5936 .version_string = SMB3ANY_VERSION_STRING,
5937 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5938 .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,
5939 .large_lock_type = 0,
5940 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5941 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5942 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5943 .header_size = sizeof(struct smb2_hdr),
5944 .header_preamble_size = 0,
5945 .max_header_size = MAX_SMB2_HDR_SIZE,
5946 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5947 .lock_cmd = SMB2_LOCK,
5949 .cap_nt_find = SMB2_NT_FIND,
5950 .cap_large_files = SMB2_LARGE_FILES,
5951 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5952 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5953 .create_lease_size = sizeof(struct create_lease_v2),
5956 struct smb_version_values smbdefault_values = {
5957 .version_string = SMBDEFAULT_VERSION_STRING,
5958 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5959 .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,
5960 .large_lock_type = 0,
5961 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5962 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5963 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5964 .header_size = sizeof(struct smb2_hdr),
5965 .header_preamble_size = 0,
5966 .max_header_size = MAX_SMB2_HDR_SIZE,
5967 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5968 .lock_cmd = SMB2_LOCK,
5970 .cap_nt_find = SMB2_NT_FIND,
5971 .cap_large_files = SMB2_LARGE_FILES,
5972 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5973 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5974 .create_lease_size = sizeof(struct create_lease_v2),
5977 struct smb_version_values smb30_values = {
5978 .version_string = SMB30_VERSION_STRING,
5979 .protocol_id = SMB30_PROT_ID,
5980 .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,
5981 .large_lock_type = 0,
5982 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5983 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5984 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5985 .header_size = sizeof(struct smb2_hdr),
5986 .header_preamble_size = 0,
5987 .max_header_size = MAX_SMB2_HDR_SIZE,
5988 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5989 .lock_cmd = SMB2_LOCK,
5991 .cap_nt_find = SMB2_NT_FIND,
5992 .cap_large_files = SMB2_LARGE_FILES,
5993 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5994 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5995 .create_lease_size = sizeof(struct create_lease_v2),
5998 struct smb_version_values smb302_values = {
5999 .version_string = SMB302_VERSION_STRING,
6000 .protocol_id = SMB302_PROT_ID,
6001 .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,
6002 .large_lock_type = 0,
6003 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
6004 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
6005 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
6006 .header_size = sizeof(struct smb2_hdr),
6007 .header_preamble_size = 0,
6008 .max_header_size = MAX_SMB2_HDR_SIZE,
6009 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
6010 .lock_cmd = SMB2_LOCK,
6012 .cap_nt_find = SMB2_NT_FIND,
6013 .cap_large_files = SMB2_LARGE_FILES,
6014 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
6015 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
6016 .create_lease_size = sizeof(struct create_lease_v2),
6019 struct smb_version_values smb311_values = {
6020 .version_string = SMB311_VERSION_STRING,
6021 .protocol_id = SMB311_PROT_ID,
6022 .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,
6023 .large_lock_type = 0,
6024 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
6025 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
6026 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
6027 .header_size = sizeof(struct smb2_hdr),
6028 .header_preamble_size = 0,
6029 .max_header_size = MAX_SMB2_HDR_SIZE,
6030 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
6031 .lock_cmd = SMB2_LOCK,
6033 .cap_nt_find = SMB2_NT_FIND,
6034 .cap_large_files = SMB2_LARGE_FILES,
6035 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
6036 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
6037 .create_lease_size = sizeof(struct create_lease_v2),