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>
19 #include "smb2proto.h"
20 #include "cifsproto.h"
21 #include "cifs_debug.h"
22 #include "cifs_unicode.h"
23 #include "smb2status.h"
25 #include "cifs_ioctl.h"
26 #include "smbdirect.h"
27 #include "fs_context.h"
29 /* Change credits for different ops and return the total number of credits */
31 change_conf(struct TCP_Server_Info *server)
33 server->credits += server->echo_credits + server->oplock_credits;
34 server->oplock_credits = server->echo_credits = 0;
35 switch (server->credits) {
39 server->echoes = false;
40 server->oplocks = false;
43 server->echoes = true;
44 server->oplocks = false;
45 server->echo_credits = 1;
48 server->echoes = true;
50 server->oplocks = true;
51 server->oplock_credits = 1;
53 server->oplocks = false;
55 server->echo_credits = 1;
57 server->credits -= server->echo_credits + server->oplock_credits;
58 return server->credits + server->echo_credits + server->oplock_credits;
62 smb2_add_credits(struct TCP_Server_Info *server,
63 const struct cifs_credits *credits, const int optype)
66 int scredits, in_flight;
67 unsigned int add = credits->value;
68 unsigned int instance = credits->instance;
69 bool reconnect_detected = false;
70 bool reconnect_with_invalid_credits = false;
72 spin_lock(&server->req_lock);
73 val = server->ops->get_credits_field(server, optype);
75 /* eg found case where write overlapping reconnect messed up credits */
76 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
77 reconnect_with_invalid_credits = true;
79 if ((instance == 0) || (instance == server->reconnect_instance))
82 reconnect_detected = true;
85 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
86 pr_warn_once("server overflowed SMB3 credits\n");
89 if (server->in_flight == 0 &&
90 ((optype & CIFS_OP_MASK) != CIFS_NEG_OP) &&
91 ((optype & CIFS_OP_MASK) != CIFS_SESS_OP))
92 rc = change_conf(server);
94 * Sometimes server returns 0 credits on oplock break ack - we need to
95 * rebalance credits in this case.
97 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
99 if (server->credits > 1) {
101 server->oplock_credits++;
105 in_flight = server->in_flight;
106 spin_unlock(&server->req_lock);
107 wake_up(&server->request_q);
109 if (reconnect_detected) {
110 trace_smb3_reconnect_detected(server->CurrentMid,
111 server->conn_id, server->hostname, scredits, add, in_flight);
113 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
117 if (reconnect_with_invalid_credits) {
118 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
119 server->conn_id, server->hostname, scredits, add, in_flight);
120 cifs_dbg(FYI, "Negotiate operation when server credits is non-zero. Optype: %d, server credits: %d, credits added: %d\n",
121 optype, scredits, add);
124 if (server->tcpStatus == CifsNeedReconnect
125 || server->tcpStatus == CifsExiting)
130 /* change_conf hasn't been executed */
133 cifs_server_dbg(VFS, "Possible client or server bug - zero credits\n");
136 cifs_server_dbg(VFS, "disabling echoes and oplocks\n");
139 cifs_dbg(FYI, "disabling oplocks\n");
142 /* change_conf rebalanced credits for different types */
146 trace_smb3_add_credits(server->CurrentMid,
147 server->conn_id, server->hostname, scredits, add, in_flight);
148 cifs_dbg(FYI, "%s: added %u credits total=%d\n", __func__, add, scredits);
152 smb2_set_credits(struct TCP_Server_Info *server, const int val)
154 int scredits, in_flight;
156 spin_lock(&server->req_lock);
157 server->credits = val;
159 server->reconnect_instance++;
160 scredits = server->credits;
161 in_flight = server->in_flight;
162 spin_unlock(&server->req_lock);
164 trace_smb3_set_credits(server->CurrentMid,
165 server->conn_id, server->hostname, scredits, val, in_flight);
166 cifs_dbg(FYI, "%s: set %u credits\n", __func__, val);
168 /* don't log while holding the lock */
170 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
174 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
178 return &server->echo_credits;
180 return &server->oplock_credits;
182 return &server->credits;
187 smb2_get_credits(struct mid_q_entry *mid)
189 return mid->credits_received;
193 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
194 unsigned int *num, struct cifs_credits *credits)
197 unsigned int scredits, in_flight;
199 spin_lock(&server->req_lock);
201 if (server->credits <= 0) {
202 spin_unlock(&server->req_lock);
203 cifs_num_waiters_inc(server);
204 rc = wait_event_killable(server->request_q,
205 has_credits(server, &server->credits, 1));
206 cifs_num_waiters_dec(server);
209 spin_lock(&server->req_lock);
211 if (server->tcpStatus == CifsExiting) {
212 spin_unlock(&server->req_lock);
216 scredits = server->credits;
217 /* can deadlock with reopen */
219 *num = SMB2_MAX_BUFFER_SIZE;
221 credits->instance = 0;
225 /* leave some credits for reopen and other ops */
227 *num = min_t(unsigned int, size,
228 scredits * SMB2_MAX_BUFFER_SIZE);
231 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
232 credits->instance = server->reconnect_instance;
233 server->credits -= credits->value;
235 if (server->in_flight > server->max_in_flight)
236 server->max_in_flight = server->in_flight;
240 scredits = server->credits;
241 in_flight = server->in_flight;
242 spin_unlock(&server->req_lock);
244 trace_smb3_add_credits(server->CurrentMid,
245 server->conn_id, server->hostname, scredits, -(credits->value), in_flight);
246 cifs_dbg(FYI, "%s: removed %u credits total=%d\n",
247 __func__, credits->value, scredits);
253 smb2_adjust_credits(struct TCP_Server_Info *server,
254 struct cifs_credits *credits,
255 const unsigned int payload_size)
257 int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
258 int scredits, in_flight;
260 if (!credits->value || credits->value == new_val)
263 if (credits->value < new_val) {
264 trace_smb3_too_many_credits(server->CurrentMid,
265 server->conn_id, server->hostname, 0, credits->value - new_val, 0);
266 cifs_server_dbg(VFS, "request has less credits (%d) than required (%d)",
267 credits->value, new_val);
272 spin_lock(&server->req_lock);
274 if (server->reconnect_instance != credits->instance) {
275 scredits = server->credits;
276 in_flight = server->in_flight;
277 spin_unlock(&server->req_lock);
279 trace_smb3_reconnect_detected(server->CurrentMid,
280 server->conn_id, server->hostname, scredits,
281 credits->value - new_val, in_flight);
282 cifs_server_dbg(VFS, "trying to return %d credits to old session\n",
283 credits->value - new_val);
287 server->credits += credits->value - new_val;
288 scredits = server->credits;
289 in_flight = server->in_flight;
290 spin_unlock(&server->req_lock);
291 wake_up(&server->request_q);
293 trace_smb3_add_credits(server->CurrentMid,
294 server->conn_id, server->hostname, scredits,
295 credits->value - new_val, in_flight);
296 cifs_dbg(FYI, "%s: adjust added %u credits total=%d\n",
297 __func__, credits->value - new_val, scredits);
299 credits->value = new_val;
305 smb2_get_next_mid(struct TCP_Server_Info *server)
308 /* for SMB2 we need the current value */
309 spin_lock(&GlobalMid_Lock);
310 mid = server->CurrentMid++;
311 spin_unlock(&GlobalMid_Lock);
316 smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
318 spin_lock(&GlobalMid_Lock);
319 if (server->CurrentMid >= val)
320 server->CurrentMid -= val;
321 spin_unlock(&GlobalMid_Lock);
324 static struct mid_q_entry *
325 __smb2_find_mid(struct TCP_Server_Info *server, char *buf, bool dequeue)
327 struct mid_q_entry *mid;
328 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
329 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
331 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
332 cifs_server_dbg(VFS, "Encrypted frame parsing not supported yet\n");
336 spin_lock(&GlobalMid_Lock);
337 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
338 if ((mid->mid == wire_mid) &&
339 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
340 (mid->command == shdr->Command)) {
341 kref_get(&mid->refcount);
343 list_del_init(&mid->qhead);
344 mid->mid_flags |= MID_DELETED;
346 spin_unlock(&GlobalMid_Lock);
350 spin_unlock(&GlobalMid_Lock);
354 static struct mid_q_entry *
355 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
357 return __smb2_find_mid(server, buf, false);
360 static struct mid_q_entry *
361 smb2_find_dequeue_mid(struct TCP_Server_Info *server, char *buf)
363 return __smb2_find_mid(server, buf, true);
367 smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
369 #ifdef CONFIG_CIFS_DEBUG2
370 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
372 cifs_server_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
373 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
375 cifs_server_dbg(VFS, "smb buf %p len %u\n", buf,
376 server->ops->calc_smb_size(buf, server));
381 smb2_need_neg(struct TCP_Server_Info *server)
383 return server->max_read == 0;
387 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
391 cifs_ses_server(ses)->CurrentMid = 0;
392 rc = SMB2_negotiate(xid, ses);
393 /* BB we probably don't need to retry with modern servers */
400 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
402 struct TCP_Server_Info *server = tcon->ses->server;
405 /* start with specified wsize, or default */
406 wsize = ctx->wsize ? ctx->wsize : CIFS_DEFAULT_IOSIZE;
407 wsize = min_t(unsigned int, wsize, server->max_write);
408 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
409 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
415 smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
417 struct TCP_Server_Info *server = tcon->ses->server;
420 /* start with specified wsize, or default */
421 wsize = ctx->wsize ? ctx->wsize : SMB3_DEFAULT_IOSIZE;
422 wsize = min_t(unsigned int, wsize, server->max_write);
423 #ifdef CONFIG_CIFS_SMB_DIRECT
427 * Account for SMB2 data transfer packet header and
428 * possible encryption header
430 wsize = min_t(unsigned int,
432 server->smbd_conn->max_fragmented_send_size -
433 SMB2_READWRITE_PDU_HEADER_SIZE -
434 sizeof(struct smb2_transform_hdr));
436 wsize = min_t(unsigned int,
437 wsize, server->smbd_conn->max_readwrite_size);
440 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
441 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
447 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
449 struct TCP_Server_Info *server = tcon->ses->server;
452 /* start with specified rsize, or default */
453 rsize = ctx->rsize ? ctx->rsize : CIFS_DEFAULT_IOSIZE;
454 rsize = min_t(unsigned int, rsize, server->max_read);
456 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
457 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
463 smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
465 struct TCP_Server_Info *server = tcon->ses->server;
468 /* start with specified rsize, or default */
469 rsize = ctx->rsize ? ctx->rsize : SMB3_DEFAULT_IOSIZE;
470 rsize = min_t(unsigned int, rsize, server->max_read);
471 #ifdef CONFIG_CIFS_SMB_DIRECT
475 * Account for SMB2 data transfer packet header and
476 * possible encryption header
478 rsize = min_t(unsigned int,
480 server->smbd_conn->max_fragmented_recv_size -
481 SMB2_READWRITE_PDU_HEADER_SIZE -
482 sizeof(struct smb2_transform_hdr));
484 rsize = min_t(unsigned int,
485 rsize, server->smbd_conn->max_readwrite_size);
489 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
490 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
496 parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
498 struct cifs_server_iface **iface_list,
501 struct network_interface_info_ioctl_rsp *p;
502 struct sockaddr_in *addr4;
503 struct sockaddr_in6 *addr6;
504 struct iface_info_ipv4 *p4;
505 struct iface_info_ipv6 *p6;
506 struct cifs_server_iface *info;
516 * Fist pass: count and sanity check
519 bytes_left = buf_len;
521 while (bytes_left >= sizeof(*p)) {
523 next = le32_to_cpu(p->Next);
525 bytes_left -= sizeof(*p);
528 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
533 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
538 /* Azure rounds the buffer size up 8, to a 16 byte boundary */
539 if ((bytes_left > 8) || p->Next)
540 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
544 * Second pass: extract info to internal structure
547 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
554 bytes_left = buf_len;
556 while (bytes_left >= sizeof(*p)) {
557 info->speed = le64_to_cpu(p->LinkSpeed);
558 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE) ? 1 : 0;
559 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE) ? 1 : 0;
561 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
562 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
563 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
564 le32_to_cpu(p->Capability));
568 * The kernel and wire socket structures have the same
569 * layout and use network byte order but make the
570 * conversion explicit in case either one changes.
573 addr4 = (struct sockaddr_in *)&info->sockaddr;
574 p4 = (struct iface_info_ipv4 *)p->Buffer;
575 addr4->sin_family = AF_INET;
576 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
578 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
579 addr4->sin_port = cpu_to_be16(CIFS_PORT);
581 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
585 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
586 p6 = (struct iface_info_ipv6 *)p->Buffer;
587 addr6->sin6_family = AF_INET6;
588 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
590 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
591 addr6->sin6_flowinfo = 0;
592 addr6->sin6_scope_id = 0;
593 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
595 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
600 "%s: skipping unsupported socket family\n",
608 next = le32_to_cpu(p->Next);
611 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
629 static int compare_iface(const void *ia, const void *ib)
631 const struct cifs_server_iface *a = (struct cifs_server_iface *)ia;
632 const struct cifs_server_iface *b = (struct cifs_server_iface *)ib;
634 return a->speed == b->speed ? 0 : (a->speed > b->speed ? -1 : 1);
638 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
641 unsigned int ret_data_len = 0;
642 struct network_interface_info_ioctl_rsp *out_buf = NULL;
643 struct cifs_server_iface *iface_list;
645 struct cifs_ses *ses = tcon->ses;
647 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
648 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
649 NULL /* no data input */, 0 /* no data input */,
650 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
651 if (rc == -EOPNOTSUPP) {
653 "server does not support query network interfaces\n");
655 } else if (rc != 0) {
656 cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
660 rc = parse_server_interfaces(out_buf, ret_data_len,
661 &iface_list, &iface_count);
665 /* sort interfaces from fastest to slowest */
666 sort(iface_list, iface_count, sizeof(*iface_list), compare_iface, NULL);
668 spin_lock(&ses->iface_lock);
669 kfree(ses->iface_list);
670 ses->iface_list = iface_list;
671 ses->iface_count = iface_count;
672 ses->iface_last_update = jiffies;
673 spin_unlock(&ses->iface_lock);
681 smb2_close_cached_fid(struct kref *ref)
683 struct cached_fid *cfid = container_of(ref, struct cached_fid,
686 if (cfid->is_valid) {
687 cifs_dbg(FYI, "clear cached root file handle\n");
688 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
689 cfid->fid->volatile_fid);
690 cfid->is_valid = false;
691 cfid->file_all_info_is_valid = false;
692 cfid->has_lease = false;
700 void close_cached_dir(struct cached_fid *cfid)
702 mutex_lock(&cfid->fid_mutex);
703 kref_put(&cfid->refcount, smb2_close_cached_fid);
704 mutex_unlock(&cfid->fid_mutex);
707 void close_cached_dir_lease_locked(struct cached_fid *cfid)
709 if (cfid->has_lease) {
710 cfid->has_lease = false;
711 kref_put(&cfid->refcount, smb2_close_cached_fid);
715 void close_cached_dir_lease(struct cached_fid *cfid)
717 mutex_lock(&cfid->fid_mutex);
718 close_cached_dir_lease_locked(cfid);
719 mutex_unlock(&cfid->fid_mutex);
723 smb2_cached_lease_break(struct work_struct *work)
725 struct cached_fid *cfid = container_of(work,
726 struct cached_fid, lease_break);
728 close_cached_dir_lease(cfid);
732 * Open the and cache a directory handle.
733 * Only supported for the root handle.
735 int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
737 struct cifs_sb_info *cifs_sb,
738 struct cached_fid **cfid)
740 struct cifs_ses *ses = tcon->ses;
741 struct TCP_Server_Info *server = ses->server;
742 struct cifs_open_parms oparms;
743 struct smb2_create_rsp *o_rsp = NULL;
744 struct smb2_query_info_rsp *qi_rsp = NULL;
746 struct smb_rqst rqst[2];
747 struct kvec rsp_iov[2];
748 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
749 struct kvec qi_iov[1];
751 __le16 utf16_path = 0; /* Null - since an open of top of share */
752 u8 oplock = SMB2_OPLOCK_LEVEL_II;
753 struct cifs_fid *pfid;
754 struct dentry *dentry;
756 if (tcon->nohandlecache)
759 if (cifs_sb->root == NULL)
765 dentry = cifs_sb->root;
767 mutex_lock(&tcon->crfid.fid_mutex);
768 if (tcon->crfid.is_valid) {
769 cifs_dbg(FYI, "found a cached root file handle\n");
770 *cfid = &tcon->crfid;
771 kref_get(&tcon->crfid.refcount);
772 mutex_unlock(&tcon->crfid.fid_mutex);
777 * We do not hold the lock for the open because in case
778 * SMB2_open needs to reconnect, it will end up calling
779 * cifs_mark_open_files_invalid() which takes the lock again
780 * thus causing a deadlock
783 mutex_unlock(&tcon->crfid.fid_mutex);
785 if (smb3_encryption_required(tcon))
786 flags |= CIFS_TRANSFORM_REQ;
788 if (!server->ops->new_lease_key)
791 pfid = tcon->crfid.fid;
792 server->ops->new_lease_key(pfid);
794 memset(rqst, 0, sizeof(rqst));
795 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
796 memset(rsp_iov, 0, sizeof(rsp_iov));
799 memset(&open_iov, 0, sizeof(open_iov));
800 rqst[0].rq_iov = open_iov;
801 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
804 oparms.create_options = cifs_create_options(cifs_sb, 0);
805 oparms.desired_access = FILE_READ_ATTRIBUTES;
806 oparms.disposition = FILE_OPEN;
808 oparms.reconnect = false;
810 rc = SMB2_open_init(tcon, server,
811 &rqst[0], &oplock, &oparms, &utf16_path);
814 smb2_set_next_command(tcon, &rqst[0]);
816 memset(&qi_iov, 0, sizeof(qi_iov));
817 rqst[1].rq_iov = qi_iov;
820 rc = SMB2_query_info_init(tcon, server,
821 &rqst[1], COMPOUND_FID,
822 COMPOUND_FID, FILE_ALL_INFORMATION,
824 sizeof(struct smb2_file_all_info) +
825 PATH_MAX * 2, 0, NULL);
829 smb2_set_related(&rqst[1]);
831 rc = compound_send_recv(xid, ses, server,
833 resp_buftype, rsp_iov);
834 mutex_lock(&tcon->crfid.fid_mutex);
837 * Now we need to check again as the cached root might have
838 * been successfully re-opened from a concurrent process
841 if (tcon->crfid.is_valid) {
842 /* work was already done */
844 /* stash fids for close() later */
845 struct cifs_fid fid = {
846 .persistent_fid = pfid->persistent_fid,
847 .volatile_fid = pfid->volatile_fid,
851 * caller expects this func to set the fid in crfid to valid
852 * cached root, so increment the refcount.
854 kref_get(&tcon->crfid.refcount);
856 mutex_unlock(&tcon->crfid.fid_mutex);
859 /* close extra handle outside of crit sec */
860 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
866 /* Cached root is still invalid, continue normaly */
869 if (rc == -EREMCHG) {
870 tcon->need_reconnect = true;
871 pr_warn_once("server share %s deleted\n",
877 atomic_inc(&tcon->num_remote_opens);
879 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
880 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
881 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
882 #ifdef CONFIG_CIFS_DEBUG2
883 oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
884 #endif /* CIFS_DEBUG2 */
886 tcon->crfid.tcon = tcon;
887 tcon->crfid.is_valid = true;
888 tcon->crfid.dentry = dentry;
890 kref_init(&tcon->crfid.refcount);
892 /* BB TBD check to see if oplock level check can be removed below */
893 if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
895 * See commit 2f94a3125b87. Increment the refcount when we
896 * get a lease for root, release it if lease break occurs
898 kref_get(&tcon->crfid.refcount);
899 tcon->crfid.has_lease = true;
900 smb2_parse_contexts(server, o_rsp,
902 oparms.fid->lease_key, &oplock,
907 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
908 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
910 if (!smb2_validate_and_copy_iov(
911 le16_to_cpu(qi_rsp->OutputBufferOffset),
912 sizeof(struct smb2_file_all_info),
913 &rsp_iov[1], sizeof(struct smb2_file_all_info),
914 (char *)&tcon->crfid.file_all_info))
915 tcon->crfid.file_all_info_is_valid = true;
916 tcon->crfid.time = jiffies;
920 mutex_unlock(&tcon->crfid.fid_mutex);
922 SMB2_open_free(&rqst[0]);
923 SMB2_query_info_free(&rqst[1]);
924 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
925 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
927 *cfid = &tcon->crfid;
931 int open_cached_dir_by_dentry(struct cifs_tcon *tcon,
932 struct dentry *dentry,
933 struct cached_fid **cfid)
935 mutex_lock(&tcon->crfid.fid_mutex);
936 if (tcon->crfid.dentry == dentry) {
937 cifs_dbg(FYI, "found a cached root file handle by dentry\n");
938 *cfid = &tcon->crfid;
939 kref_get(&tcon->crfid.refcount);
940 mutex_unlock(&tcon->crfid.fid_mutex);
943 mutex_unlock(&tcon->crfid.fid_mutex);
948 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
949 struct cifs_sb_info *cifs_sb)
952 __le16 srch_path = 0; /* Null - open root of share */
953 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
954 struct cifs_open_parms oparms;
956 struct cached_fid *cfid = NULL;
959 oparms.desired_access = FILE_READ_ATTRIBUTES;
960 oparms.disposition = FILE_OPEN;
961 oparms.create_options = cifs_create_options(cifs_sb, 0);
963 oparms.reconnect = false;
965 rc = open_cached_dir(xid, tcon, "", cifs_sb, &cfid);
967 memcpy(&fid, cfid->fid, sizeof(struct cifs_fid));
969 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
974 SMB3_request_interfaces(xid, tcon);
976 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
977 FS_ATTRIBUTE_INFORMATION);
978 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
979 FS_DEVICE_INFORMATION);
980 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
981 FS_VOLUME_INFORMATION);
982 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
983 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
985 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
987 close_cached_dir(cfid);
991 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
992 struct cifs_sb_info *cifs_sb)
995 __le16 srch_path = 0; /* Null - open root of share */
996 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
997 struct cifs_open_parms oparms;
1001 oparms.desired_access = FILE_READ_ATTRIBUTES;
1002 oparms.disposition = FILE_OPEN;
1003 oparms.create_options = cifs_create_options(cifs_sb, 0);
1005 oparms.reconnect = false;
1007 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
1012 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1013 FS_ATTRIBUTE_INFORMATION);
1014 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1015 FS_DEVICE_INFORMATION);
1016 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1020 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
1021 struct cifs_sb_info *cifs_sb, const char *full_path)
1025 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1026 struct cifs_open_parms oparms;
1027 struct cifs_fid fid;
1029 if ((*full_path == 0) && tcon->crfid.is_valid)
1032 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1037 oparms.desired_access = FILE_READ_ATTRIBUTES;
1038 oparms.disposition = FILE_OPEN;
1039 oparms.create_options = cifs_create_options(cifs_sb, 0);
1041 oparms.reconnect = false;
1043 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
1050 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1056 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
1057 struct cifs_sb_info *cifs_sb, const char *full_path,
1058 u64 *uniqueid, FILE_ALL_INFO *data)
1060 *uniqueid = le64_to_cpu(data->IndexNumber);
1065 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
1066 struct cifs_fid *fid, FILE_ALL_INFO *data)
1069 struct smb2_file_all_info *smb2_data;
1071 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
1073 if (smb2_data == NULL)
1076 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
1079 move_smb2_info_to_cifs(data, smb2_data);
1084 #ifdef CONFIG_CIFS_XATTR
1086 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
1087 struct smb2_file_full_ea_info *src, size_t src_size,
1088 const unsigned char *ea_name)
1091 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
1093 size_t buf_size = dst_size;
1094 size_t name_len, value_len, user_name_len;
1096 while (src_size > 0) {
1097 name = &src->ea_data[0];
1098 name_len = (size_t)src->ea_name_length;
1099 value = &src->ea_data[src->ea_name_length + 1];
1100 value_len = (size_t)le16_to_cpu(src->ea_value_length);
1105 if (src_size < 8 + name_len + 1 + value_len) {
1106 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
1112 if (ea_name_len == name_len &&
1113 memcmp(ea_name, name, name_len) == 0) {
1117 if (dst_size < value_len) {
1121 memcpy(dst, value, value_len);
1125 /* 'user.' plus a terminating null */
1126 user_name_len = 5 + 1 + name_len;
1128 if (buf_size == 0) {
1129 /* skip copy - calc size only */
1130 rc += user_name_len;
1131 } else if (dst_size >= user_name_len) {
1132 dst_size -= user_name_len;
1133 memcpy(dst, "user.", 5);
1135 memcpy(dst, src->ea_data, name_len);
1139 rc += user_name_len;
1141 /* stop before overrun buffer */
1147 if (!src->next_entry_offset)
1150 if (src_size < le32_to_cpu(src->next_entry_offset)) {
1151 /* stop before overrun buffer */
1155 src_size -= le32_to_cpu(src->next_entry_offset);
1156 src = (void *)((char *)src +
1157 le32_to_cpu(src->next_entry_offset));
1160 /* didn't find the named attribute */
1169 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1170 const unsigned char *path, const unsigned char *ea_name,
1171 char *ea_data, size_t buf_size,
1172 struct cifs_sb_info *cifs_sb)
1176 struct kvec rsp_iov = {NULL, 0};
1177 int buftype = CIFS_NO_BUFFER;
1178 struct smb2_query_info_rsp *rsp;
1179 struct smb2_file_full_ea_info *info = NULL;
1181 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1185 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1187 FILE_FULL_EA_INFORMATION,
1190 MAX_SMB2_CREATE_RESPONSE_SIZE -
1191 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1192 &rsp_iov, &buftype, cifs_sb);
1195 * If ea_name is NULL (listxattr) and there are no EAs,
1196 * return 0 as it's not an error. Otherwise, the specified
1197 * ea_name was not found.
1199 if (!ea_name && rc == -ENODATA)
1204 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1205 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1206 le32_to_cpu(rsp->OutputBufferLength),
1208 sizeof(struct smb2_file_full_ea_info));
1212 info = (struct smb2_file_full_ea_info *)(
1213 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1214 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1215 le32_to_cpu(rsp->OutputBufferLength), ea_name);
1219 free_rsp_buf(buftype, rsp_iov.iov_base);
1225 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1226 const char *path, const char *ea_name, const void *ea_value,
1227 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1228 struct cifs_sb_info *cifs_sb)
1230 struct cifs_ses *ses = tcon->ses;
1231 struct TCP_Server_Info *server = cifs_pick_channel(ses);
1232 __le16 *utf16_path = NULL;
1233 int ea_name_len = strlen(ea_name);
1234 int flags = CIFS_CP_CREATE_CLOSE_OP;
1236 struct smb_rqst rqst[3];
1237 int resp_buftype[3];
1238 struct kvec rsp_iov[3];
1239 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1240 struct cifs_open_parms oparms;
1241 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1242 struct cifs_fid fid;
1243 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1244 unsigned int size[1];
1246 struct smb2_file_full_ea_info *ea = NULL;
1247 struct kvec close_iov[1];
1248 struct smb2_query_info_rsp *rsp;
1249 int rc, used_len = 0;
1251 if (smb3_encryption_required(tcon))
1252 flags |= CIFS_TRANSFORM_REQ;
1254 if (ea_name_len > 255)
1257 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1261 memset(rqst, 0, sizeof(rqst));
1262 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1263 memset(rsp_iov, 0, sizeof(rsp_iov));
1265 if (ses->server->ops->query_all_EAs) {
1267 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1273 /* If we are adding a attribute we should first check
1274 * if there will be enough space available to store
1275 * the new EA. If not we should not add it since we
1276 * would not be able to even read the EAs back.
1278 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1280 FILE_FULL_EA_INFORMATION,
1283 MAX_SMB2_CREATE_RESPONSE_SIZE -
1284 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1285 &rsp_iov[1], &resp_buftype[1], cifs_sb);
1287 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1288 used_len = le32_to_cpu(rsp->OutputBufferLength);
1290 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1291 resp_buftype[1] = CIFS_NO_BUFFER;
1292 memset(&rsp_iov[1], 0, sizeof(rsp_iov[1]));
1295 /* Use a fudge factor of 256 bytes in case we collide
1296 * with a different set_EAs command.
1298 if(CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1299 MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
1300 used_len + ea_name_len + ea_value_len + 1) {
1308 memset(&open_iov, 0, sizeof(open_iov));
1309 rqst[0].rq_iov = open_iov;
1310 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1312 memset(&oparms, 0, sizeof(oparms));
1314 oparms.desired_access = FILE_WRITE_EA;
1315 oparms.disposition = FILE_OPEN;
1316 oparms.create_options = cifs_create_options(cifs_sb, 0);
1318 oparms.reconnect = false;
1320 rc = SMB2_open_init(tcon, server,
1321 &rqst[0], &oplock, &oparms, utf16_path);
1324 smb2_set_next_command(tcon, &rqst[0]);
1328 memset(&si_iov, 0, sizeof(si_iov));
1329 rqst[1].rq_iov = si_iov;
1330 rqst[1].rq_nvec = 1;
1332 len = sizeof(*ea) + ea_name_len + ea_value_len + 1;
1333 ea = kzalloc(len, GFP_KERNEL);
1339 ea->ea_name_length = ea_name_len;
1340 ea->ea_value_length = cpu_to_le16(ea_value_len);
1341 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1342 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1347 rc = SMB2_set_info_init(tcon, server,
1348 &rqst[1], COMPOUND_FID,
1349 COMPOUND_FID, current->tgid,
1350 FILE_FULL_EA_INFORMATION,
1351 SMB2_O_INFO_FILE, 0, data, size);
1352 smb2_set_next_command(tcon, &rqst[1]);
1353 smb2_set_related(&rqst[1]);
1357 memset(&close_iov, 0, sizeof(close_iov));
1358 rqst[2].rq_iov = close_iov;
1359 rqst[2].rq_nvec = 1;
1360 rc = SMB2_close_init(tcon, server,
1361 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1362 smb2_set_related(&rqst[2]);
1364 rc = compound_send_recv(xid, ses, server,
1366 resp_buftype, rsp_iov);
1367 /* no need to bump num_remote_opens because handle immediately closed */
1372 SMB2_open_free(&rqst[0]);
1373 SMB2_set_info_free(&rqst[1]);
1374 SMB2_close_free(&rqst[2]);
1375 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1376 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1377 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1383 smb2_can_echo(struct TCP_Server_Info *server)
1385 return server->echoes;
1389 smb2_clear_stats(struct cifs_tcon *tcon)
1393 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1394 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1395 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1400 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1402 seq_puts(m, "\n\tShare Capabilities:");
1403 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1404 seq_puts(m, " DFS,");
1405 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1406 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1407 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1408 seq_puts(m, " SCALEOUT,");
1409 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1410 seq_puts(m, " CLUSTER,");
1411 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1412 seq_puts(m, " ASYMMETRIC,");
1413 if (tcon->capabilities == 0)
1414 seq_puts(m, " None");
1415 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1416 seq_puts(m, " Aligned,");
1417 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1418 seq_puts(m, " Partition Aligned,");
1419 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1420 seq_puts(m, " SSD,");
1421 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1422 seq_puts(m, " TRIM-support,");
1424 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1425 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1426 if (tcon->perf_sector_size)
1427 seq_printf(m, "\tOptimal sector size: 0x%x",
1428 tcon->perf_sector_size);
1429 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1433 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1435 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1436 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1439 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1440 * totals (requests sent) since those SMBs are per-session not per tcon
1442 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1443 (long long)(tcon->bytes_read),
1444 (long long)(tcon->bytes_written));
1445 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1446 atomic_read(&tcon->num_local_opens),
1447 atomic_read(&tcon->num_remote_opens));
1448 seq_printf(m, "\nTreeConnects: %d total %d failed",
1449 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1450 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1451 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1452 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1453 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1454 seq_printf(m, "\nCreates: %d total %d failed",
1455 atomic_read(&sent[SMB2_CREATE_HE]),
1456 atomic_read(&failed[SMB2_CREATE_HE]));
1457 seq_printf(m, "\nCloses: %d total %d failed",
1458 atomic_read(&sent[SMB2_CLOSE_HE]),
1459 atomic_read(&failed[SMB2_CLOSE_HE]));
1460 seq_printf(m, "\nFlushes: %d total %d failed",
1461 atomic_read(&sent[SMB2_FLUSH_HE]),
1462 atomic_read(&failed[SMB2_FLUSH_HE]));
1463 seq_printf(m, "\nReads: %d total %d failed",
1464 atomic_read(&sent[SMB2_READ_HE]),
1465 atomic_read(&failed[SMB2_READ_HE]));
1466 seq_printf(m, "\nWrites: %d total %d failed",
1467 atomic_read(&sent[SMB2_WRITE_HE]),
1468 atomic_read(&failed[SMB2_WRITE_HE]));
1469 seq_printf(m, "\nLocks: %d total %d failed",
1470 atomic_read(&sent[SMB2_LOCK_HE]),
1471 atomic_read(&failed[SMB2_LOCK_HE]));
1472 seq_printf(m, "\nIOCTLs: %d total %d failed",
1473 atomic_read(&sent[SMB2_IOCTL_HE]),
1474 atomic_read(&failed[SMB2_IOCTL_HE]));
1475 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1476 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1477 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1478 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1479 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1480 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1481 seq_printf(m, "\nQueryInfos: %d total %d failed",
1482 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1483 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1484 seq_printf(m, "\nSetInfos: %d total %d failed",
1485 atomic_read(&sent[SMB2_SET_INFO_HE]),
1486 atomic_read(&failed[SMB2_SET_INFO_HE]));
1487 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1488 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1489 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1493 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1495 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1496 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1498 cfile->fid.persistent_fid = fid->persistent_fid;
1499 cfile->fid.volatile_fid = fid->volatile_fid;
1500 cfile->fid.access = fid->access;
1501 #ifdef CONFIG_CIFS_DEBUG2
1502 cfile->fid.mid = fid->mid;
1503 #endif /* CIFS_DEBUG2 */
1504 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1506 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1507 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1511 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1512 struct cifs_fid *fid)
1514 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1518 smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
1519 struct cifsFileInfo *cfile)
1521 struct smb2_file_network_open_info file_inf;
1522 struct inode *inode;
1525 rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
1526 cfile->fid.volatile_fid, &file_inf);
1530 inode = d_inode(cfile->dentry);
1532 spin_lock(&inode->i_lock);
1533 CIFS_I(inode)->time = jiffies;
1535 /* Creation time should not need to be updated on close */
1536 if (file_inf.LastWriteTime)
1537 inode->i_mtime = cifs_NTtimeToUnix(file_inf.LastWriteTime);
1538 if (file_inf.ChangeTime)
1539 inode->i_ctime = cifs_NTtimeToUnix(file_inf.ChangeTime);
1540 if (file_inf.LastAccessTime)
1541 inode->i_atime = cifs_NTtimeToUnix(file_inf.LastAccessTime);
1544 * i_blocks is not related to (i_size / i_blksize),
1545 * but instead 512 byte (2**9) size is required for
1546 * calculating num blocks.
1548 if (le64_to_cpu(file_inf.AllocationSize) > 4096)
1550 (512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
1552 /* End of file and Attributes should not have to be updated on close */
1553 spin_unlock(&inode->i_lock);
1557 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1558 u64 persistent_fid, u64 volatile_fid,
1559 struct copychunk_ioctl *pcchunk)
1562 unsigned int ret_data_len;
1563 struct resume_key_req *res_key;
1565 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1566 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1567 NULL, 0 /* no input */, CIFSMaxBufSize,
1568 (char **)&res_key, &ret_data_len);
1570 if (rc == -EOPNOTSUPP) {
1571 pr_warn_once("Server share %s does not support copy range\n", tcon->treeName);
1572 goto req_res_key_exit;
1574 cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1575 goto req_res_key_exit;
1577 if (ret_data_len < sizeof(struct resume_key_req)) {
1578 cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
1580 goto req_res_key_exit;
1582 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1590 struct smb_rqst rqst[3];
1591 struct kvec rsp_iov[3];
1592 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1593 struct kvec qi_iov[1];
1594 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1595 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1596 struct kvec close_iov[1];
1600 smb2_ioctl_query_info(const unsigned int xid,
1601 struct cifs_tcon *tcon,
1602 struct cifs_sb_info *cifs_sb,
1603 __le16 *path, int is_dir,
1606 struct iqi_vars *vars;
1607 struct smb_rqst *rqst;
1608 struct kvec *rsp_iov;
1609 struct cifs_ses *ses = tcon->ses;
1610 struct TCP_Server_Info *server = cifs_pick_channel(ses);
1611 char __user *arg = (char __user *)p;
1612 struct smb_query_info qi;
1613 struct smb_query_info __user *pqi;
1615 int flags = CIFS_CP_CREATE_CLOSE_OP;
1616 struct smb2_query_info_rsp *qi_rsp = NULL;
1617 struct smb2_ioctl_rsp *io_rsp = NULL;
1618 void *buffer = NULL;
1619 int resp_buftype[3];
1620 struct cifs_open_parms oparms;
1621 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1622 struct cifs_fid fid;
1623 unsigned int size[2];
1625 int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
1627 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
1630 rqst = &vars->rqst[0];
1631 rsp_iov = &vars->rsp_iov[0];
1633 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1635 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1638 if (qi.output_buffer_length > 1024) {
1643 if (!ses || !server) {
1648 if (smb3_encryption_required(tcon))
1649 flags |= CIFS_TRANSFORM_REQ;
1651 buffer = memdup_user(arg + sizeof(struct smb_query_info),
1652 qi.output_buffer_length);
1653 if (IS_ERR(buffer)) {
1655 return PTR_ERR(buffer);
1659 rqst[0].rq_iov = &vars->open_iov[0];
1660 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1662 memset(&oparms, 0, sizeof(oparms));
1664 oparms.disposition = FILE_OPEN;
1665 oparms.create_options = cifs_create_options(cifs_sb, create_options);
1667 oparms.reconnect = false;
1669 if (qi.flags & PASSTHRU_FSCTL) {
1670 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1671 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1672 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1674 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1675 oparms.desired_access = GENERIC_ALL;
1677 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1678 oparms.desired_access = GENERIC_READ;
1680 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1681 oparms.desired_access = GENERIC_WRITE;
1684 } else if (qi.flags & PASSTHRU_SET_INFO) {
1685 oparms.desired_access = GENERIC_WRITE;
1687 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1690 rc = SMB2_open_init(tcon, server,
1691 &rqst[0], &oplock, &oparms, path);
1694 smb2_set_next_command(tcon, &rqst[0]);
1697 if (qi.flags & PASSTHRU_FSCTL) {
1698 /* Can eventually relax perm check since server enforces too */
1699 if (!capable(CAP_SYS_ADMIN))
1702 rqst[1].rq_iov = &vars->io_iov[0];
1703 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1705 rc = SMB2_ioctl_init(tcon, server,
1707 COMPOUND_FID, COMPOUND_FID,
1708 qi.info_type, true, buffer,
1709 qi.output_buffer_length,
1711 MAX_SMB2_CREATE_RESPONSE_SIZE -
1712 MAX_SMB2_CLOSE_RESPONSE_SIZE);
1714 } else if (qi.flags == PASSTHRU_SET_INFO) {
1715 /* Can eventually relax perm check since server enforces too */
1716 if (!capable(CAP_SYS_ADMIN))
1719 rqst[1].rq_iov = &vars->si_iov[0];
1720 rqst[1].rq_nvec = 1;
1725 rc = SMB2_set_info_init(tcon, server,
1727 COMPOUND_FID, COMPOUND_FID,
1729 FILE_END_OF_FILE_INFORMATION,
1730 SMB2_O_INFO_FILE, 0, data, size);
1732 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1733 rqst[1].rq_iov = &vars->qi_iov[0];
1734 rqst[1].rq_nvec = 1;
1736 rc = SMB2_query_info_init(tcon, server,
1737 &rqst[1], COMPOUND_FID,
1738 COMPOUND_FID, qi.file_info_class,
1739 qi.info_type, qi.additional_information,
1740 qi.input_buffer_length,
1741 qi.output_buffer_length, buffer);
1742 } else { /* unknown flags */
1743 cifs_tcon_dbg(VFS, "Invalid passthru query flags: 0x%x\n",
1750 smb2_set_next_command(tcon, &rqst[1]);
1751 smb2_set_related(&rqst[1]);
1754 rqst[2].rq_iov = &vars->close_iov[0];
1755 rqst[2].rq_nvec = 1;
1757 rc = SMB2_close_init(tcon, server,
1758 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1761 smb2_set_related(&rqst[2]);
1763 rc = compound_send_recv(xid, ses, server,
1765 resp_buftype, rsp_iov);
1769 /* No need to bump num_remote_opens since handle immediately closed */
1770 if (qi.flags & PASSTHRU_FSCTL) {
1771 pqi = (struct smb_query_info __user *)arg;
1772 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1773 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1774 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1775 if (qi.input_buffer_length > 0 &&
1776 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1777 > rsp_iov[1].iov_len)
1780 if (copy_to_user(&pqi->input_buffer_length,
1781 &qi.input_buffer_length,
1782 sizeof(qi.input_buffer_length)))
1785 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1786 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1787 qi.input_buffer_length))
1790 pqi = (struct smb_query_info __user *)arg;
1791 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1792 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1793 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1794 if (copy_to_user(&pqi->input_buffer_length,
1795 &qi.input_buffer_length,
1796 sizeof(qi.input_buffer_length)))
1799 if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1800 qi.input_buffer_length))
1805 cifs_small_buf_release(rqst[0].rq_iov[0].iov_base);
1806 cifs_small_buf_release(rqst[1].rq_iov[0].iov_base);
1807 cifs_small_buf_release(rqst[2].rq_iov[0].iov_base);
1808 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1809 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1810 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1821 smb2_copychunk_range(const unsigned int xid,
1822 struct cifsFileInfo *srcfile,
1823 struct cifsFileInfo *trgtfile, u64 src_off,
1824 u64 len, u64 dest_off)
1827 unsigned int ret_data_len;
1828 struct copychunk_ioctl *pcchunk;
1829 struct copychunk_ioctl_rsp *retbuf = NULL;
1830 struct cifs_tcon *tcon;
1831 int chunks_copied = 0;
1832 bool chunk_sizes_updated = false;
1833 ssize_t bytes_written, total_bytes_written = 0;
1835 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1837 if (pcchunk == NULL)
1840 cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
1841 /* Request a key from the server to identify the source of the copy */
1842 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1843 srcfile->fid.persistent_fid,
1844 srcfile->fid.volatile_fid, pcchunk);
1846 /* Note: request_res_key sets res_key null only if rc !=0 */
1850 /* For now array only one chunk long, will make more flexible later */
1851 pcchunk->ChunkCount = cpu_to_le32(1);
1852 pcchunk->Reserved = 0;
1853 pcchunk->Reserved2 = 0;
1855 tcon = tlink_tcon(trgtfile->tlink);
1858 pcchunk->SourceOffset = cpu_to_le64(src_off);
1859 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1861 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1863 /* Request server copy to target from src identified by key */
1866 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1867 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1868 true /* is_fsctl */, (char *)pcchunk,
1869 sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1870 (char **)&retbuf, &ret_data_len);
1873 sizeof(struct copychunk_ioctl_rsp)) {
1874 cifs_tcon_dbg(VFS, "Invalid cchunk response size\n");
1878 if (retbuf->TotalBytesWritten == 0) {
1879 cifs_dbg(FYI, "no bytes copied\n");
1884 * Check if server claimed to write more than we asked
1886 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1887 le32_to_cpu(pcchunk->Length)) {
1888 cifs_tcon_dbg(VFS, "Invalid copy chunk response\n");
1892 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1893 cifs_tcon_dbg(VFS, "Invalid num chunks written\n");
1899 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1900 src_off += bytes_written;
1901 dest_off += bytes_written;
1902 len -= bytes_written;
1903 total_bytes_written += bytes_written;
1905 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1906 le32_to_cpu(retbuf->ChunksWritten),
1907 le32_to_cpu(retbuf->ChunkBytesWritten),
1909 } else if (rc == -EINVAL) {
1910 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1913 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1914 le32_to_cpu(retbuf->ChunksWritten),
1915 le32_to_cpu(retbuf->ChunkBytesWritten),
1916 le32_to_cpu(retbuf->TotalBytesWritten));
1919 * Check if this is the first request using these sizes,
1920 * (ie check if copy succeed once with original sizes
1921 * and check if the server gave us different sizes after
1922 * we already updated max sizes on previous request).
1923 * if not then why is the server returning an error now
1925 if ((chunks_copied != 0) || chunk_sizes_updated)
1928 /* Check that server is not asking us to grow size */
1929 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1930 tcon->max_bytes_chunk)
1931 tcon->max_bytes_chunk =
1932 le32_to_cpu(retbuf->ChunkBytesWritten);
1934 goto cchunk_out; /* server gave us bogus size */
1936 /* No need to change MaxChunks since already set to 1 */
1937 chunk_sizes_updated = true;
1948 return total_bytes_written;
1952 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1953 struct cifs_fid *fid)
1955 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1959 smb2_read_data_offset(char *buf)
1961 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1963 return rsp->DataOffset;
1967 smb2_read_data_length(char *buf, bool in_remaining)
1969 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1972 return le32_to_cpu(rsp->DataRemaining);
1974 return le32_to_cpu(rsp->DataLength);
1979 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1980 struct cifs_io_parms *parms, unsigned int *bytes_read,
1981 char **buf, int *buf_type)
1983 parms->persistent_fid = pfid->persistent_fid;
1984 parms->volatile_fid = pfid->volatile_fid;
1985 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1989 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1990 struct cifs_io_parms *parms, unsigned int *written,
1991 struct kvec *iov, unsigned long nr_segs)
1994 parms->persistent_fid = pfid->persistent_fid;
1995 parms->volatile_fid = pfid->volatile_fid;
1996 return SMB2_write(xid, parms, written, iov, nr_segs);
1999 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
2000 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
2001 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
2003 struct cifsInodeInfo *cifsi;
2006 cifsi = CIFS_I(inode);
2008 /* if file already sparse don't bother setting sparse again */
2009 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
2010 return true; /* already sparse */
2012 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
2013 return true; /* already not sparse */
2016 * Can't check for sparse support on share the usual way via the
2017 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
2018 * since Samba server doesn't set the flag on the share, yet
2019 * supports the set sparse FSCTL and returns sparse correctly
2020 * in the file attributes. If we fail setting sparse though we
2021 * mark that server does not support sparse files for this share
2022 * to avoid repeatedly sending the unsupported fsctl to server
2023 * if the file is repeatedly extended.
2025 if (tcon->broken_sparse_sup)
2028 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2029 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
2031 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
2033 tcon->broken_sparse_sup = true;
2034 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
2039 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
2041 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
2047 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
2048 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
2050 __le64 eof = cpu_to_le64(size);
2051 struct inode *inode;
2054 * If extending file more than one page make sparse. Many Linux fs
2055 * make files sparse by default when extending via ftruncate
2057 inode = d_inode(cfile->dentry);
2059 if (!set_alloc && (size > inode->i_size + 8192)) {
2060 __u8 set_sparse = 1;
2062 /* whether set sparse succeeds or not, extend the file */
2063 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
2066 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
2067 cfile->fid.volatile_fid, cfile->pid, &eof);
2071 smb2_duplicate_extents(const unsigned int xid,
2072 struct cifsFileInfo *srcfile,
2073 struct cifsFileInfo *trgtfile, u64 src_off,
2074 u64 len, u64 dest_off)
2077 unsigned int ret_data_len;
2078 struct inode *inode;
2079 struct duplicate_extents_to_file dup_ext_buf;
2080 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
2082 /* server fileays advertise duplicate extent support with this flag */
2083 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
2084 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
2087 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
2088 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
2089 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
2090 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
2091 dup_ext_buf.ByteCount = cpu_to_le64(len);
2092 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
2093 src_off, dest_off, len);
2095 inode = d_inode(trgtfile->dentry);
2096 if (inode->i_size < dest_off + len) {
2097 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
2099 goto duplicate_extents_out;
2102 * Although also could set plausible allocation size (i_blocks)
2103 * here in addition to setting the file size, in reflink
2104 * it is likely that the target file is sparse. Its allocation
2105 * size will be queried on next revalidate, but it is important
2106 * to make sure that file's cached size is updated immediately
2108 cifs_setsize(inode, dest_off + len);
2110 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
2111 trgtfile->fid.volatile_fid,
2112 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
2113 true /* is_fsctl */,
2114 (char *)&dup_ext_buf,
2115 sizeof(struct duplicate_extents_to_file),
2116 CIFSMaxBufSize, NULL,
2119 if (ret_data_len > 0)
2120 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
2122 duplicate_extents_out:
2127 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2128 struct cifsFileInfo *cfile)
2130 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
2131 cfile->fid.volatile_fid);
2135 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
2136 struct cifsFileInfo *cfile)
2138 struct fsctl_set_integrity_information_req integr_info;
2139 unsigned int ret_data_len;
2141 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
2142 integr_info.Flags = 0;
2143 integr_info.Reserved = 0;
2145 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2146 cfile->fid.volatile_fid,
2147 FSCTL_SET_INTEGRITY_INFORMATION,
2148 true /* is_fsctl */,
2149 (char *)&integr_info,
2150 sizeof(struct fsctl_set_integrity_information_req),
2151 CIFSMaxBufSize, NULL,
2156 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
2157 #define GMT_TOKEN_SIZE 50
2159 #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
2162 * Input buffer contains (empty) struct smb_snapshot array with size filled in
2163 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
2166 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
2167 struct cifsFileInfo *cfile, void __user *ioc_buf)
2169 char *retbuf = NULL;
2170 unsigned int ret_data_len = 0;
2172 u32 max_response_size;
2173 struct smb_snapshot_array snapshot_in;
2176 * On the first query to enumerate the list of snapshots available
2177 * for this volume the buffer begins with 0 (number of snapshots
2178 * which can be returned is zero since at that point we do not know
2179 * how big the buffer needs to be). On the second query,
2180 * it (ret_data_len) is set to number of snapshots so we can
2181 * know to set the maximum response size larger (see below).
2183 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
2187 * Note that for snapshot queries that servers like Azure expect that
2188 * the first query be minimal size (and just used to get the number/size
2189 * of previous versions) so response size must be specified as EXACTLY
2190 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2193 if (ret_data_len == 0)
2194 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
2196 max_response_size = CIFSMaxBufSize;
2198 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2199 cfile->fid.volatile_fid,
2200 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
2201 true /* is_fsctl */,
2202 NULL, 0 /* no input data */, max_response_size,
2205 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2210 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2212 if (copy_from_user(&snapshot_in, ioc_buf,
2213 sizeof(struct smb_snapshot_array))) {
2220 * Check for min size, ie not large enough to fit even one GMT
2221 * token (snapshot). On the first ioctl some users may pass in
2222 * smaller size (or zero) to simply get the size of the array
2223 * so the user space caller can allocate sufficient memory
2224 * and retry the ioctl again with larger array size sufficient
2225 * to hold all of the snapshot GMT tokens on the second try.
2227 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2228 ret_data_len = sizeof(struct smb_snapshot_array);
2231 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2232 * the snapshot array (of 50 byte GMT tokens) each
2233 * representing an available previous version of the data
2235 if (ret_data_len > (snapshot_in.snapshot_array_size +
2236 sizeof(struct smb_snapshot_array)))
2237 ret_data_len = snapshot_in.snapshot_array_size +
2238 sizeof(struct smb_snapshot_array);
2240 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2251 smb3_notify(const unsigned int xid, struct file *pfile,
2252 void __user *ioc_buf)
2254 struct smb3_notify notify;
2255 struct dentry *dentry = pfile->f_path.dentry;
2256 struct inode *inode = file_inode(pfile);
2257 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2258 struct cifs_open_parms oparms;
2259 struct cifs_fid fid;
2260 struct cifs_tcon *tcon;
2261 const unsigned char *path;
2262 void *page = alloc_dentry_path();
2263 __le16 *utf16_path = NULL;
2264 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2267 path = build_path_from_dentry(dentry, page);
2273 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2274 if (utf16_path == NULL) {
2279 if (copy_from_user(¬ify, ioc_buf, sizeof(struct smb3_notify))) {
2284 tcon = cifs_sb_master_tcon(cifs_sb);
2286 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2287 oparms.disposition = FILE_OPEN;
2288 oparms.create_options = cifs_create_options(cifs_sb, 0);
2290 oparms.reconnect = false;
2292 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
2297 rc = SMB2_change_notify(xid, tcon, fid.persistent_fid, fid.volatile_fid,
2298 notify.watch_tree, notify.completion_filter);
2300 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2302 cifs_dbg(FYI, "change notify for path %s rc %d\n", path, rc);
2305 free_dentry_path(page);
2311 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2312 const char *path, struct cifs_sb_info *cifs_sb,
2313 struct cifs_fid *fid, __u16 search_flags,
2314 struct cifs_search_info *srch_inf)
2317 struct smb_rqst rqst[2];
2318 struct kvec rsp_iov[2];
2319 int resp_buftype[2];
2320 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2321 struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
2323 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2324 struct cifs_open_parms oparms;
2325 struct smb2_query_directory_rsp *qd_rsp = NULL;
2326 struct smb2_create_rsp *op_rsp = NULL;
2327 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
2328 int retry_count = 0;
2330 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2334 if (smb3_encryption_required(tcon))
2335 flags |= CIFS_TRANSFORM_REQ;
2337 memset(rqst, 0, sizeof(rqst));
2338 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2339 memset(rsp_iov, 0, sizeof(rsp_iov));
2342 memset(&open_iov, 0, sizeof(open_iov));
2343 rqst[0].rq_iov = open_iov;
2344 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2347 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2348 oparms.disposition = FILE_OPEN;
2349 oparms.create_options = cifs_create_options(cifs_sb, 0);
2351 oparms.reconnect = false;
2353 rc = SMB2_open_init(tcon, server,
2354 &rqst[0], &oplock, &oparms, utf16_path);
2357 smb2_set_next_command(tcon, &rqst[0]);
2359 /* Query directory */
2360 srch_inf->entries_in_buffer = 0;
2361 srch_inf->index_of_last_entry = 2;
2363 memset(&qd_iov, 0, sizeof(qd_iov));
2364 rqst[1].rq_iov = qd_iov;
2365 rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
2367 rc = SMB2_query_directory_init(xid, tcon, server,
2369 COMPOUND_FID, COMPOUND_FID,
2370 0, srch_inf->info_level);
2374 smb2_set_related(&rqst[1]);
2377 rc = compound_send_recv(xid, tcon->ses, server,
2379 resp_buftype, rsp_iov);
2381 if (rc == -EAGAIN && retry_count++ < 10)
2384 /* If the open failed there is nothing to do */
2385 op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
2386 if (op_rsp == NULL || op_rsp->sync_hdr.Status != STATUS_SUCCESS) {
2387 cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
2390 fid->persistent_fid = op_rsp->PersistentFileId;
2391 fid->volatile_fid = op_rsp->VolatileFileId;
2393 /* Anything else than ENODATA means a genuine error */
2394 if (rc && rc != -ENODATA) {
2395 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2396 cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
2397 trace_smb3_query_dir_err(xid, fid->persistent_fid,
2398 tcon->tid, tcon->ses->Suid, 0, 0, rc);
2402 atomic_inc(&tcon->num_remote_opens);
2404 qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
2405 if (qd_rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
2406 trace_smb3_query_dir_done(xid, fid->persistent_fid,
2407 tcon->tid, tcon->ses->Suid, 0, 0);
2408 srch_inf->endOfSearch = true;
2413 rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
2416 trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
2417 tcon->ses->Suid, 0, 0, rc);
2420 resp_buftype[1] = CIFS_NO_BUFFER;
2422 trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
2423 tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
2427 SMB2_open_free(&rqst[0]);
2428 SMB2_query_directory_free(&rqst[1]);
2429 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2430 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2435 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2436 struct cifs_fid *fid, __u16 search_flags,
2437 struct cifs_search_info *srch_inf)
2439 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2440 fid->volatile_fid, 0, srch_inf);
2444 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2445 struct cifs_fid *fid)
2447 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2451 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2452 * the number of credits and return true. Otherwise - return false.
2455 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
2457 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2458 int scredits, in_flight;
2460 if (shdr->Status != STATUS_PENDING)
2463 if (shdr->CreditRequest) {
2464 spin_lock(&server->req_lock);
2465 server->credits += le16_to_cpu(shdr->CreditRequest);
2466 scredits = server->credits;
2467 in_flight = server->in_flight;
2468 spin_unlock(&server->req_lock);
2469 wake_up(&server->request_q);
2471 trace_smb3_add_credits(server->CurrentMid,
2472 server->conn_id, server->hostname, scredits,
2473 le16_to_cpu(shdr->CreditRequest), in_flight);
2474 cifs_dbg(FYI, "%s: status pending add %u credits total=%d\n",
2475 __func__, le16_to_cpu(shdr->CreditRequest), scredits);
2482 smb2_is_session_expired(char *buf)
2484 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2486 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2487 shdr->Status != STATUS_USER_SESSION_DELETED)
2490 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2491 le16_to_cpu(shdr->Command),
2492 le64_to_cpu(shdr->MessageId));
2493 cifs_dbg(FYI, "Session expired or deleted\n");
2499 smb2_is_status_io_timeout(char *buf)
2501 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2503 if (shdr->Status == STATUS_IO_TIMEOUT)
2510 smb2_is_network_name_deleted(char *buf, struct TCP_Server_Info *server)
2512 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2513 struct list_head *tmp, *tmp1;
2514 struct cifs_ses *ses;
2515 struct cifs_tcon *tcon;
2517 if (shdr->Status != STATUS_NETWORK_NAME_DELETED)
2520 spin_lock(&cifs_tcp_ses_lock);
2521 list_for_each(tmp, &server->smb_ses_list) {
2522 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
2523 list_for_each(tmp1, &ses->tcon_list) {
2524 tcon = list_entry(tmp1, struct cifs_tcon, tcon_list);
2525 if (tcon->tid == shdr->TreeId) {
2526 tcon->need_reconnect = true;
2527 spin_unlock(&cifs_tcp_ses_lock);
2528 pr_warn_once("Server share %s deleted.\n",
2534 spin_unlock(&cifs_tcp_ses_lock);
2538 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2539 struct cifsInodeInfo *cinode)
2541 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2542 return SMB2_lease_break(0, tcon, cinode->lease_key,
2543 smb2_get_lease_state(cinode));
2545 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2547 CIFS_CACHE_READ(cinode) ? 1 : 0);
2551 smb2_set_related(struct smb_rqst *rqst)
2553 struct smb2_sync_hdr *shdr;
2555 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2557 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2560 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2563 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2566 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2568 struct smb2_sync_hdr *shdr;
2569 struct cifs_ses *ses = tcon->ses;
2570 struct TCP_Server_Info *server = ses->server;
2571 unsigned long len = smb_rqst_len(server, rqst);
2574 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2576 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2580 /* SMB headers in a compound are 8 byte aligned. */
2582 /* No padding needed */
2586 num_padding = 8 - (len & 7);
2587 if (!smb3_encryption_required(tcon)) {
2589 * If we do not have encryption then we can just add an extra
2590 * iov for the padding.
2592 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2593 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2598 * We can not add a small padding iov for the encryption case
2599 * because the encryption framework can not handle the padding
2601 * We have to flatten this into a single buffer and add
2602 * the padding to it.
2604 for (i = 1; i < rqst->rq_nvec; i++) {
2605 memcpy(rqst->rq_iov[0].iov_base +
2606 rqst->rq_iov[0].iov_len,
2607 rqst->rq_iov[i].iov_base,
2608 rqst->rq_iov[i].iov_len);
2609 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2611 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2613 rqst->rq_iov[0].iov_len += num_padding;
2619 shdr->NextCommand = cpu_to_le32(len);
2623 * Passes the query info response back to the caller on success.
2624 * Caller need to free this with free_rsp_buf().
2627 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2628 __le16 *utf16_path, u32 desired_access,
2629 u32 class, u32 type, u32 output_len,
2630 struct kvec *rsp, int *buftype,
2631 struct cifs_sb_info *cifs_sb)
2633 struct cifs_ses *ses = tcon->ses;
2634 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2635 int flags = CIFS_CP_CREATE_CLOSE_OP;
2636 struct smb_rqst rqst[3];
2637 int resp_buftype[3];
2638 struct kvec rsp_iov[3];
2639 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2640 struct kvec qi_iov[1];
2641 struct kvec close_iov[1];
2642 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2643 struct cifs_open_parms oparms;
2644 struct cifs_fid fid;
2647 if (smb3_encryption_required(tcon))
2648 flags |= CIFS_TRANSFORM_REQ;
2650 memset(rqst, 0, sizeof(rqst));
2651 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2652 memset(rsp_iov, 0, sizeof(rsp_iov));
2654 memset(&open_iov, 0, sizeof(open_iov));
2655 rqst[0].rq_iov = open_iov;
2656 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2659 oparms.desired_access = desired_access;
2660 oparms.disposition = FILE_OPEN;
2661 oparms.create_options = cifs_create_options(cifs_sb, 0);
2663 oparms.reconnect = false;
2665 rc = SMB2_open_init(tcon, server,
2666 &rqst[0], &oplock, &oparms, utf16_path);
2669 smb2_set_next_command(tcon, &rqst[0]);
2671 memset(&qi_iov, 0, sizeof(qi_iov));
2672 rqst[1].rq_iov = qi_iov;
2673 rqst[1].rq_nvec = 1;
2675 rc = SMB2_query_info_init(tcon, server,
2676 &rqst[1], COMPOUND_FID, COMPOUND_FID,
2682 smb2_set_next_command(tcon, &rqst[1]);
2683 smb2_set_related(&rqst[1]);
2685 memset(&close_iov, 0, sizeof(close_iov));
2686 rqst[2].rq_iov = close_iov;
2687 rqst[2].rq_nvec = 1;
2689 rc = SMB2_close_init(tcon, server,
2690 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2693 smb2_set_related(&rqst[2]);
2695 rc = compound_send_recv(xid, ses, server,
2697 resp_buftype, rsp_iov);
2699 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2700 if (rc == -EREMCHG) {
2701 tcon->need_reconnect = true;
2702 pr_warn_once("server share %s deleted\n",
2708 *buftype = resp_buftype[1];
2711 SMB2_open_free(&rqst[0]);
2712 SMB2_query_info_free(&rqst[1]);
2713 SMB2_close_free(&rqst[2]);
2714 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2715 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2720 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2721 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2723 struct smb2_query_info_rsp *rsp;
2724 struct smb2_fs_full_size_info *info = NULL;
2725 __le16 utf16_path = 0; /* Null - open root of share */
2726 struct kvec rsp_iov = {NULL, 0};
2727 int buftype = CIFS_NO_BUFFER;
2731 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2732 FILE_READ_ATTRIBUTES,
2733 FS_FULL_SIZE_INFORMATION,
2734 SMB2_O_INFO_FILESYSTEM,
2735 sizeof(struct smb2_fs_full_size_info),
2736 &rsp_iov, &buftype, cifs_sb);
2740 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2741 buf->f_type = SMB2_MAGIC_NUMBER;
2742 info = (struct smb2_fs_full_size_info *)(
2743 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2744 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2745 le32_to_cpu(rsp->OutputBufferLength),
2747 sizeof(struct smb2_fs_full_size_info));
2749 smb2_copy_fs_info_to_kstatfs(info, buf);
2752 free_rsp_buf(buftype, rsp_iov.iov_base);
2757 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2758 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2761 __le16 srch_path = 0; /* Null - open root of share */
2762 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2763 struct cifs_open_parms oparms;
2764 struct cifs_fid fid;
2766 if (!tcon->posix_extensions)
2767 return smb2_queryfs(xid, tcon, cifs_sb, buf);
2770 oparms.desired_access = FILE_READ_ATTRIBUTES;
2771 oparms.disposition = FILE_OPEN;
2772 oparms.create_options = cifs_create_options(cifs_sb, 0);
2774 oparms.reconnect = false;
2776 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
2781 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2782 fid.volatile_fid, buf);
2783 buf->f_type = SMB2_MAGIC_NUMBER;
2784 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2789 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2791 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2792 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2796 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2797 __u64 length, __u32 type, int lock, int unlock, bool wait)
2799 if (unlock && !lock)
2800 type = SMB2_LOCKFLAG_UNLOCK;
2801 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2802 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2803 current->tgid, length, offset, type, wait);
2807 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2809 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2813 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2815 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2819 smb2_new_lease_key(struct cifs_fid *fid)
2821 generate_random_uuid(fid->lease_key);
2825 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2826 const char *search_name,
2827 struct dfs_info3_param **target_nodes,
2828 unsigned int *num_of_nodes,
2829 const struct nls_table *nls_codepage, int remap)
2832 __le16 *utf16_path = NULL;
2833 int utf16_path_len = 0;
2834 struct cifs_tcon *tcon;
2835 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2836 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2837 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2839 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
2842 * Try to use the IPC tcon, otherwise just use any
2844 tcon = ses->tcon_ipc;
2846 spin_lock(&cifs_tcp_ses_lock);
2847 tcon = list_first_entry_or_null(&ses->tcon_list,
2852 spin_unlock(&cifs_tcp_ses_lock);
2856 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2862 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2864 nls_codepage, remap);
2870 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2871 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2877 /* Highest DFS referral version understood */
2878 dfs_req->MaxReferralLevel = DFS_VERSION;
2880 /* Path to resolve in an UTF-16 null-terminated string */
2881 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2884 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2885 FSCTL_DFS_GET_REFERRALS,
2886 true /* is_fsctl */,
2887 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
2888 (char **)&dfs_rsp, &dfs_rsp_size);
2889 } while (rc == -EAGAIN);
2892 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
2893 cifs_tcon_dbg(VFS, "ioctl error in %s rc=%d\n", __func__, rc);
2897 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2898 num_of_nodes, target_nodes,
2899 nls_codepage, remap, search_name,
2900 true /* is_unicode */);
2902 cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
2907 if (tcon && !tcon->ipc) {
2908 /* ipc tcons are not refcounted */
2909 spin_lock(&cifs_tcp_ses_lock);
2911 spin_unlock(&cifs_tcp_ses_lock);
2920 parse_reparse_posix(struct reparse_posix_data *symlink_buf,
2921 u32 plen, char **target_path,
2922 struct cifs_sb_info *cifs_sb)
2926 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
2927 len = le16_to_cpu(symlink_buf->ReparseDataLength);
2929 if (le64_to_cpu(symlink_buf->InodeType) != NFS_SPECFILE_LNK) {
2930 cifs_dbg(VFS, "%lld not a supported symlink type\n",
2931 le64_to_cpu(symlink_buf->InodeType));
2935 *target_path = cifs_strndup_from_utf16(
2936 symlink_buf->PathBuffer,
2937 len, true, cifs_sb->local_nls);
2938 if (!(*target_path))
2941 convert_delimiter(*target_path, '/');
2942 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2948 parse_reparse_symlink(struct reparse_symlink_data_buffer *symlink_buf,
2949 u32 plen, char **target_path,
2950 struct cifs_sb_info *cifs_sb)
2952 unsigned int sub_len;
2953 unsigned int sub_offset;
2955 /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
2957 sub_offset = le16_to_cpu(symlink_buf->SubstituteNameOffset);
2958 sub_len = le16_to_cpu(symlink_buf->SubstituteNameLength);
2959 if (sub_offset + 20 > plen ||
2960 sub_offset + sub_len + 20 > plen) {
2961 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
2965 *target_path = cifs_strndup_from_utf16(
2966 symlink_buf->PathBuffer + sub_offset,
2967 sub_len, true, cifs_sb->local_nls);
2968 if (!(*target_path))
2971 convert_delimiter(*target_path, '/');
2972 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2978 parse_reparse_point(struct reparse_data_buffer *buf,
2979 u32 plen, char **target_path,
2980 struct cifs_sb_info *cifs_sb)
2982 if (plen < sizeof(struct reparse_data_buffer)) {
2983 cifs_dbg(VFS, "reparse buffer is too small. Must be at least 8 bytes but was %d\n",
2988 if (plen < le16_to_cpu(buf->ReparseDataLength) +
2989 sizeof(struct reparse_data_buffer)) {
2990 cifs_dbg(VFS, "srv returned invalid reparse buf length: %d\n",
2995 /* See MS-FSCC 2.1.2 */
2996 switch (le32_to_cpu(buf->ReparseTag)) {
2997 case IO_REPARSE_TAG_NFS:
2998 return parse_reparse_posix(
2999 (struct reparse_posix_data *)buf,
3000 plen, target_path, cifs_sb);
3001 case IO_REPARSE_TAG_SYMLINK:
3002 return parse_reparse_symlink(
3003 (struct reparse_symlink_data_buffer *)buf,
3004 plen, target_path, cifs_sb);
3006 cifs_dbg(VFS, "srv returned unknown symlink buffer tag:0x%08x\n",
3007 le32_to_cpu(buf->ReparseTag));
3012 #define SMB2_SYMLINK_STRUCT_SIZE \
3013 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
3016 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
3017 struct cifs_sb_info *cifs_sb, const char *full_path,
3018 char **target_path, bool is_reparse_point)
3021 __le16 *utf16_path = NULL;
3022 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3023 struct cifs_open_parms oparms;
3024 struct cifs_fid fid;
3025 struct kvec err_iov = {NULL, 0};
3026 struct smb2_err_rsp *err_buf = NULL;
3027 struct smb2_symlink_err_rsp *symlink;
3028 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
3029 unsigned int sub_len;
3030 unsigned int sub_offset;
3031 unsigned int print_len;
3032 unsigned int print_offset;
3033 int flags = CIFS_CP_CREATE_CLOSE_OP;
3034 struct smb_rqst rqst[3];
3035 int resp_buftype[3];
3036 struct kvec rsp_iov[3];
3037 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
3038 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
3039 struct kvec close_iov[1];
3040 struct smb2_create_rsp *create_rsp;
3041 struct smb2_ioctl_rsp *ioctl_rsp;
3042 struct reparse_data_buffer *reparse_buf;
3043 int create_options = is_reparse_point ? OPEN_REPARSE_POINT : 0;
3046 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
3048 *target_path = NULL;
3050 if (smb3_encryption_required(tcon))
3051 flags |= CIFS_TRANSFORM_REQ;
3053 memset(rqst, 0, sizeof(rqst));
3054 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
3055 memset(rsp_iov, 0, sizeof(rsp_iov));
3057 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
3062 memset(&open_iov, 0, sizeof(open_iov));
3063 rqst[0].rq_iov = open_iov;
3064 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
3066 memset(&oparms, 0, sizeof(oparms));
3068 oparms.desired_access = FILE_READ_ATTRIBUTES;
3069 oparms.disposition = FILE_OPEN;
3070 oparms.create_options = cifs_create_options(cifs_sb, create_options);
3072 oparms.reconnect = false;
3074 rc = SMB2_open_init(tcon, server,
3075 &rqst[0], &oplock, &oparms, utf16_path);
3078 smb2_set_next_command(tcon, &rqst[0]);
3082 memset(&io_iov, 0, sizeof(io_iov));
3083 rqst[1].rq_iov = io_iov;
3084 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
3086 rc = SMB2_ioctl_init(tcon, server,
3087 &rqst[1], fid.persistent_fid,
3088 fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
3089 true /* is_fctl */, NULL, 0,
3091 MAX_SMB2_CREATE_RESPONSE_SIZE -
3092 MAX_SMB2_CLOSE_RESPONSE_SIZE);
3096 smb2_set_next_command(tcon, &rqst[1]);
3097 smb2_set_related(&rqst[1]);
3101 memset(&close_iov, 0, sizeof(close_iov));
3102 rqst[2].rq_iov = close_iov;
3103 rqst[2].rq_nvec = 1;
3105 rc = SMB2_close_init(tcon, server,
3106 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
3110 smb2_set_related(&rqst[2]);
3112 rc = compound_send_recv(xid, tcon->ses, server,
3114 resp_buftype, rsp_iov);
3116 create_rsp = rsp_iov[0].iov_base;
3117 if (create_rsp && create_rsp->sync_hdr.Status)
3118 err_iov = rsp_iov[0];
3119 ioctl_rsp = rsp_iov[1].iov_base;
3122 * Open was successful and we got an ioctl response.
3124 if ((rc == 0) && (is_reparse_point)) {
3125 /* See MS-FSCC 2.3.23 */
3127 reparse_buf = (struct reparse_data_buffer *)
3128 ((char *)ioctl_rsp +
3129 le32_to_cpu(ioctl_rsp->OutputOffset));
3130 plen = le32_to_cpu(ioctl_rsp->OutputCount);
3132 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
3133 rsp_iov[1].iov_len) {
3134 cifs_tcon_dbg(VFS, "srv returned invalid ioctl len: %d\n",
3140 rc = parse_reparse_point(reparse_buf, plen, target_path,
3145 if (!rc || !err_iov.iov_base) {
3150 err_buf = err_iov.iov_base;
3151 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
3152 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
3157 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
3158 if (le32_to_cpu(symlink->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
3159 le32_to_cpu(symlink->ReparseTag) != IO_REPARSE_TAG_SYMLINK) {
3164 /* open must fail on symlink - reset rc */
3166 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
3167 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
3168 print_len = le16_to_cpu(symlink->PrintNameLength);
3169 print_offset = le16_to_cpu(symlink->PrintNameOffset);
3171 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
3176 if (err_iov.iov_len <
3177 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
3182 *target_path = cifs_strndup_from_utf16(
3183 (char *)symlink->PathBuffer + sub_offset,
3184 sub_len, true, cifs_sb->local_nls);
3185 if (!(*target_path)) {
3189 convert_delimiter(*target_path, '/');
3190 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
3193 cifs_dbg(FYI, "query symlink rc %d\n", rc);
3195 SMB2_open_free(&rqst[0]);
3196 SMB2_ioctl_free(&rqst[1]);
3197 SMB2_close_free(&rqst[2]);
3198 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
3199 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3200 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
3205 smb2_query_reparse_tag(const unsigned int xid, struct cifs_tcon *tcon,
3206 struct cifs_sb_info *cifs_sb, const char *full_path,
3210 __le16 *utf16_path = NULL;
3211 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3212 struct cifs_open_parms oparms;
3213 struct cifs_fid fid;
3214 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
3215 int flags = CIFS_CP_CREATE_CLOSE_OP;
3216 struct smb_rqst rqst[3];
3217 int resp_buftype[3];
3218 struct kvec rsp_iov[3];
3219 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
3220 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
3221 struct kvec close_iov[1];
3222 struct smb2_ioctl_rsp *ioctl_rsp;
3223 struct reparse_data_buffer *reparse_buf;
3226 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
3228 if (smb3_encryption_required(tcon))
3229 flags |= CIFS_TRANSFORM_REQ;
3231 memset(rqst, 0, sizeof(rqst));
3232 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
3233 memset(rsp_iov, 0, sizeof(rsp_iov));
3235 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
3240 * setup smb2open - TODO add optimization to call cifs_get_readable_path
3241 * to see if there is a handle already open that we can use
3243 memset(&open_iov, 0, sizeof(open_iov));
3244 rqst[0].rq_iov = open_iov;
3245 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
3247 memset(&oparms, 0, sizeof(oparms));
3249 oparms.desired_access = FILE_READ_ATTRIBUTES;
3250 oparms.disposition = FILE_OPEN;
3251 oparms.create_options = cifs_create_options(cifs_sb, OPEN_REPARSE_POINT);
3253 oparms.reconnect = false;
3255 rc = SMB2_open_init(tcon, server,
3256 &rqst[0], &oplock, &oparms, utf16_path);
3259 smb2_set_next_command(tcon, &rqst[0]);
3263 memset(&io_iov, 0, sizeof(io_iov));
3264 rqst[1].rq_iov = io_iov;
3265 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
3267 rc = SMB2_ioctl_init(tcon, server,
3268 &rqst[1], COMPOUND_FID,
3269 COMPOUND_FID, FSCTL_GET_REPARSE_POINT,
3270 true /* is_fctl */, NULL, 0,
3272 MAX_SMB2_CREATE_RESPONSE_SIZE -
3273 MAX_SMB2_CLOSE_RESPONSE_SIZE);
3277 smb2_set_next_command(tcon, &rqst[1]);
3278 smb2_set_related(&rqst[1]);
3282 memset(&close_iov, 0, sizeof(close_iov));
3283 rqst[2].rq_iov = close_iov;
3284 rqst[2].rq_nvec = 1;
3286 rc = SMB2_close_init(tcon, server,
3287 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
3291 smb2_set_related(&rqst[2]);
3293 rc = compound_send_recv(xid, tcon->ses, server,
3295 resp_buftype, rsp_iov);
3297 ioctl_rsp = rsp_iov[1].iov_base;
3300 * Open was successful and we got an ioctl response.
3303 /* See MS-FSCC 2.3.23 */
3305 reparse_buf = (struct reparse_data_buffer *)
3306 ((char *)ioctl_rsp +
3307 le32_to_cpu(ioctl_rsp->OutputOffset));
3308 plen = le32_to_cpu(ioctl_rsp->OutputCount);
3310 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
3311 rsp_iov[1].iov_len) {
3312 cifs_tcon_dbg(FYI, "srv returned invalid ioctl len: %d\n",
3317 *tag = le32_to_cpu(reparse_buf->ReparseTag);
3322 SMB2_open_free(&rqst[0]);
3323 SMB2_ioctl_free(&rqst[1]);
3324 SMB2_close_free(&rqst[2]);
3325 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
3326 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3327 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
3331 static struct cifs_ntsd *
3332 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
3333 const struct cifs_fid *cifsfid, u32 *pacllen, u32 info)
3335 struct cifs_ntsd *pntsd = NULL;
3337 int rc = -EOPNOTSUPP;
3338 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3341 return ERR_CAST(tlink);
3344 cifs_dbg(FYI, "trying to get acl\n");
3346 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
3347 cifsfid->volatile_fid, (void **)&pntsd, pacllen,
3351 cifs_put_tlink(tlink);
3353 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3360 static struct cifs_ntsd *
3361 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
3362 const char *path, u32 *pacllen, u32 info)
3364 struct cifs_ntsd *pntsd = NULL;
3365 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3368 struct cifs_tcon *tcon;
3369 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3370 struct cifs_fid fid;
3371 struct cifs_open_parms oparms;
3374 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
3376 return ERR_CAST(tlink);
3378 tcon = tlink_tcon(tlink);
3381 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3389 oparms.desired_access = READ_CONTROL;
3390 oparms.disposition = FILE_OPEN;
3392 * When querying an ACL, even if the file is a symlink we want to open
3393 * the source not the target, and so the protocol requires that the
3394 * client specify this flag when opening a reparse point
3396 oparms.create_options = cifs_create_options(cifs_sb, 0) | OPEN_REPARSE_POINT;
3398 oparms.reconnect = false;
3400 if (info & SACL_SECINFO)
3401 oparms.desired_access |= SYSTEM_SECURITY;
3403 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
3407 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3408 fid.volatile_fid, (void **)&pntsd, pacllen,
3410 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3413 cifs_put_tlink(tlink);
3416 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3423 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
3424 struct inode *inode, const char *path, int aclflag)
3426 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3428 int rc, access_flags = 0;
3429 struct cifs_tcon *tcon;
3430 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3431 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3432 struct cifs_fid fid;
3433 struct cifs_open_parms oparms;
3436 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
3438 return PTR_ERR(tlink);
3440 tcon = tlink_tcon(tlink);
3443 if (aclflag & CIFS_ACL_OWNER || aclflag & CIFS_ACL_GROUP)
3444 access_flags |= WRITE_OWNER;
3445 if (aclflag & CIFS_ACL_SACL)
3446 access_flags |= SYSTEM_SECURITY;
3447 if (aclflag & CIFS_ACL_DACL)
3448 access_flags |= WRITE_DAC;
3450 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3458 oparms.desired_access = access_flags;
3459 oparms.create_options = cifs_create_options(cifs_sb, 0);
3460 oparms.disposition = FILE_OPEN;
3463 oparms.reconnect = false;
3465 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
3469 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3470 fid.volatile_fid, pnntsd, acllen, aclflag);
3471 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3474 cifs_put_tlink(tlink);
3479 /* Retrieve an ACL from the server */
3480 static struct cifs_ntsd *
3481 get_smb2_acl(struct cifs_sb_info *cifs_sb,
3482 struct inode *inode, const char *path,
3483 u32 *pacllen, u32 info)
3485 struct cifs_ntsd *pntsd = NULL;
3486 struct cifsFileInfo *open_file = NULL;
3488 if (inode && !(info & SACL_SECINFO))
3489 open_file = find_readable_file(CIFS_I(inode), true);
3490 if (!open_file || (info & SACL_SECINFO))
3491 return get_smb2_acl_by_path(cifs_sb, path, pacllen, info);
3493 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen, info);
3494 cifsFileInfo_put(open_file);
3498 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3499 loff_t offset, loff_t len, bool keep_size)
3501 struct cifs_ses *ses = tcon->ses;
3502 struct inode *inode;
3503 struct cifsInodeInfo *cifsi;
3504 struct cifsFileInfo *cfile = file->private_data;
3505 struct file_zero_data_information fsctl_buf;
3512 inode = d_inode(cfile->dentry);
3513 cifsi = CIFS_I(inode);
3515 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3516 ses->Suid, offset, len);
3519 * We zero the range through ioctl, so we need remove the page caches
3520 * first, otherwise the data may be inconsistent with the server.
3522 truncate_pagecache_range(inode, offset, offset + len - 1);
3524 /* if file not oplocked can't be sure whether asking to extend size */
3525 if (!CIFS_CACHE_READ(cifsi))
3526 if (keep_size == false) {
3528 trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
3529 tcon->tid, ses->Suid, offset, len, rc);
3534 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3536 fsctl_buf.FileOffset = cpu_to_le64(offset);
3537 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3539 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3540 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
3542 sizeof(struct file_zero_data_information),
3545 goto zero_range_exit;
3548 * do we also need to change the size of the file?
3550 if (keep_size == false && i_size_read(inode) < offset + len) {
3551 eof = cpu_to_le64(offset + len);
3552 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3553 cfile->fid.volatile_fid, cfile->pid, &eof);
3559 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3560 ses->Suid, offset, len, rc);
3562 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3563 ses->Suid, offset, len);
3567 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3568 loff_t offset, loff_t len)
3570 struct inode *inode;
3571 struct cifsFileInfo *cfile = file->private_data;
3572 struct file_zero_data_information fsctl_buf;
3575 __u8 set_sparse = 1;
3579 inode = d_inode(cfile->dentry);
3581 /* Need to make file sparse, if not already, before freeing range. */
3582 /* Consider adding equivalent for compressed since it could also work */
3583 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3590 * We implement the punch hole through ioctl, so we need remove the page
3591 * caches first, otherwise the data may be inconsistent with the server.
3593 truncate_pagecache_range(inode, offset, offset + len - 1);
3595 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3597 fsctl_buf.FileOffset = cpu_to_le64(offset);
3598 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3600 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3601 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3602 true /* is_fctl */, (char *)&fsctl_buf,
3603 sizeof(struct file_zero_data_information),
3604 CIFSMaxBufSize, NULL, NULL);
3609 static int smb3_simple_fallocate_write_range(unsigned int xid,
3610 struct cifs_tcon *tcon,
3611 struct cifsFileInfo *cfile,
3612 loff_t off, loff_t len,
3615 struct cifs_io_parms io_parms = {0};
3620 io_parms.netfid = cfile->fid.netfid;
3621 io_parms.pid = current->tgid;
3622 io_parms.tcon = tcon;
3623 io_parms.persistent_fid = cfile->fid.persistent_fid;
3624 io_parms.volatile_fid = cfile->fid.volatile_fid;
3627 io_parms.offset = off;
3628 io_parms.length = len;
3629 if (io_parms.length > SMB2_MAX_BUFFER_SIZE)
3630 io_parms.length = SMB2_MAX_BUFFER_SIZE;
3631 /* iov[0] is reserved for smb header */
3632 iov[1].iov_base = buf;
3633 iov[1].iov_len = io_parms.length;
3634 rc = SMB2_write(xid, &io_parms, &nbytes, iov, 1);
3646 static int smb3_simple_fallocate_range(unsigned int xid,
3647 struct cifs_tcon *tcon,
3648 struct cifsFileInfo *cfile,
3649 loff_t off, loff_t len)
3651 struct file_allocated_range_buffer in_data, *out_data = NULL, *tmp_data;
3657 in_data.file_offset = cpu_to_le64(off);
3658 in_data.length = cpu_to_le64(len);
3659 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3660 cfile->fid.volatile_fid,
3661 FSCTL_QUERY_ALLOCATED_RANGES, true,
3662 (char *)&in_data, sizeof(in_data),
3663 1024 * sizeof(struct file_allocated_range_buffer),
3664 (char **)&out_data, &out_data_len);
3668 buf = kzalloc(1024 * 1024, GFP_KERNEL);
3674 tmp_data = out_data;
3677 * The rest of the region is unmapped so write it all.
3679 if (out_data_len == 0) {
3680 rc = smb3_simple_fallocate_write_range(xid, tcon,
3681 cfile, off, len, buf);
3685 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3690 if (off < le64_to_cpu(tmp_data->file_offset)) {
3692 * We are at a hole. Write until the end of the region
3693 * or until the next allocated data,
3694 * whichever comes next.
3696 l = le64_to_cpu(tmp_data->file_offset) - off;
3699 rc = smb3_simple_fallocate_write_range(xid, tcon,
3700 cfile, off, l, buf);
3709 * We are at a section of allocated data, just skip forward
3710 * until the end of the data or the end of the region
3711 * we are supposed to fallocate, whichever comes first.
3713 l = le64_to_cpu(tmp_data->length);
3719 tmp_data = &tmp_data[1];
3720 out_data_len -= sizeof(struct file_allocated_range_buffer);
3730 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3731 loff_t off, loff_t len, bool keep_size)
3733 struct inode *inode;
3734 struct cifsInodeInfo *cifsi;
3735 struct cifsFileInfo *cfile = file->private_data;
3736 long rc = -EOPNOTSUPP;
3742 inode = d_inode(cfile->dentry);
3743 cifsi = CIFS_I(inode);
3745 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3746 tcon->ses->Suid, off, len);
3747 /* if file not oplocked can't be sure whether asking to extend size */
3748 if (!CIFS_CACHE_READ(cifsi))
3749 if (keep_size == false) {
3750 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3751 tcon->tid, tcon->ses->Suid, off, len, rc);
3757 * Extending the file
3759 if ((keep_size == false) && i_size_read(inode) < off + len) {
3760 rc = inode_newsize_ok(inode, off + len);
3764 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0)
3765 smb2_set_sparse(xid, tcon, cfile, inode, false);
3767 eof = cpu_to_le64(off + len);
3768 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3769 cfile->fid.volatile_fid, cfile->pid, &eof);
3771 cifsi->server_eof = off + len;
3772 cifs_setsize(inode, off + len);
3773 cifs_truncate_page(inode->i_mapping, inode->i_size);
3774 truncate_setsize(inode, off + len);
3780 * Files are non-sparse by default so falloc may be a no-op
3781 * Must check if file sparse. If not sparse, and since we are not
3782 * extending then no need to do anything since file already allocated
3784 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
3789 if (keep_size == true) {
3791 * We can not preallocate pages beyond the end of the file
3794 if (off >= i_size_read(inode)) {
3799 * For fallocates that are partially beyond the end of file,
3800 * clamp len so we only fallocate up to the end of file.
3802 if (off + len > i_size_read(inode)) {
3803 len = i_size_read(inode) - off;
3807 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3809 * At this point, we are trying to fallocate an internal
3810 * regions of a sparse file. Since smb2 does not have a
3811 * fallocate command we have two otions on how to emulate this.
3812 * We can either turn the entire file to become non-sparse
3813 * which we only do if the fallocate is for virtually
3814 * the whole file, or we can overwrite the region with zeroes
3815 * using SMB2_write, which could be prohibitevly expensive
3819 * We are only trying to fallocate a small region so
3820 * just write it with zero.
3822 if (len <= 1024 * 1024) {
3823 rc = smb3_simple_fallocate_range(xid, tcon, cfile,
3829 * Check if falloc starts within first few pages of file
3830 * and ends within a few pages of the end of file to
3831 * ensure that most of file is being forced to be
3832 * fallocated now. If so then setting whole file sparse
3833 * ie potentially making a few extra pages at the beginning
3834 * or end of the file non-sparse via set_sparse is harmless.
3836 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3842 smb2_set_sparse(xid, tcon, cfile, inode, false);
3847 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3848 tcon->ses->Suid, off, len, rc);
3850 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3851 tcon->ses->Suid, off, len);
3857 static long smb3_collapse_range(struct file *file, struct cifs_tcon *tcon,
3858 loff_t off, loff_t len)
3862 struct cifsFileInfo *cfile = file->private_data;
3867 if (off >= i_size_read(file->f_inode) ||
3868 off + len >= i_size_read(file->f_inode)) {
3873 rc = smb2_copychunk_range(xid, cfile, cfile, off + len,
3874 i_size_read(file->f_inode) - off - len, off);
3878 eof = cpu_to_le64(i_size_read(file->f_inode) - len);
3879 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3880 cfile->fid.volatile_fid, cfile->pid, &eof);
3890 static long smb3_insert_range(struct file *file, struct cifs_tcon *tcon,
3891 loff_t off, loff_t len)
3895 struct cifsFileInfo *cfile = file->private_data;
3901 if (off >= i_size_read(file->f_inode)) {
3906 count = i_size_read(file->f_inode) - off;
3907 eof = cpu_to_le64(i_size_read(file->f_inode) + len);
3909 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3910 cfile->fid.volatile_fid, cfile->pid, &eof);
3914 rc = smb2_copychunk_range(xid, cfile, cfile, off, count, off + len);
3918 rc = smb3_zero_range(file, tcon, off, len, 1);
3928 static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3930 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3931 struct cifsInodeInfo *cifsi;
3932 struct inode *inode;
3934 struct file_allocated_range_buffer in_data, *out_data = NULL;
3938 if (whence != SEEK_HOLE && whence != SEEK_DATA)
3939 return generic_file_llseek(file, offset, whence);
3941 inode = d_inode(cfile->dentry);
3942 cifsi = CIFS_I(inode);
3944 if (offset < 0 || offset >= i_size_read(inode))
3949 * We need to be sure that all dirty pages are written as they
3950 * might fill holes on the server.
3951 * Note that we also MUST flush any written pages since at least
3952 * some servers (Windows2016) will not reflect recent writes in
3953 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3955 wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
3957 filemap_write_and_wait(inode->i_mapping);
3958 smb2_flush_file(xid, tcon, &wrcfile->fid);
3959 cifsFileInfo_put(wrcfile);
3962 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3963 if (whence == SEEK_HOLE)
3964 offset = i_size_read(inode);
3968 in_data.file_offset = cpu_to_le64(offset);
3969 in_data.length = cpu_to_le64(i_size_read(inode));
3971 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3972 cfile->fid.volatile_fid,
3973 FSCTL_QUERY_ALLOCATED_RANGES, true,
3974 (char *)&in_data, sizeof(in_data),
3975 sizeof(struct file_allocated_range_buffer),
3976 (char **)&out_data, &out_data_len);
3982 if (whence == SEEK_HOLE && out_data_len == 0)
3985 if (whence == SEEK_DATA && out_data_len == 0) {
3990 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3994 if (whence == SEEK_DATA) {
3995 offset = le64_to_cpu(out_data->file_offset);
3998 if (offset < le64_to_cpu(out_data->file_offset))
4001 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
4007 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
4012 static int smb3_fiemap(struct cifs_tcon *tcon,
4013 struct cifsFileInfo *cfile,
4014 struct fiemap_extent_info *fei, u64 start, u64 len)
4017 struct file_allocated_range_buffer in_data, *out_data;
4019 int i, num, rc, flags, last_blob;
4022 rc = fiemap_prep(d_inode(cfile->dentry), fei, start, &len, 0);
4028 in_data.file_offset = cpu_to_le64(start);
4029 in_data.length = cpu_to_le64(len);
4031 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
4032 cfile->fid.volatile_fid,
4033 FSCTL_QUERY_ALLOCATED_RANGES, true,
4034 (char *)&in_data, sizeof(in_data),
4035 1024 * sizeof(struct file_allocated_range_buffer),
4036 (char **)&out_data, &out_data_len);
4045 if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) {
4049 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
4054 num = out_data_len / sizeof(struct file_allocated_range_buffer);
4055 for (i = 0; i < num; i++) {
4057 if (i == num - 1 && last_blob)
4058 flags |= FIEMAP_EXTENT_LAST;
4060 rc = fiemap_fill_next_extent(fei,
4061 le64_to_cpu(out_data[i].file_offset),
4062 le64_to_cpu(out_data[i].file_offset),
4063 le64_to_cpu(out_data[i].length),
4074 next = le64_to_cpu(out_data[num - 1].file_offset) +
4075 le64_to_cpu(out_data[num - 1].length);
4076 len = len - (next - start);
4087 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
4088 loff_t off, loff_t len)
4090 /* KEEP_SIZE already checked for by do_fallocate */
4091 if (mode & FALLOC_FL_PUNCH_HOLE)
4092 return smb3_punch_hole(file, tcon, off, len);
4093 else if (mode & FALLOC_FL_ZERO_RANGE) {
4094 if (mode & FALLOC_FL_KEEP_SIZE)
4095 return smb3_zero_range(file, tcon, off, len, true);
4096 return smb3_zero_range(file, tcon, off, len, false);
4097 } else if (mode == FALLOC_FL_KEEP_SIZE)
4098 return smb3_simple_falloc(file, tcon, off, len, true);
4099 else if (mode == FALLOC_FL_COLLAPSE_RANGE)
4100 return smb3_collapse_range(file, tcon, off, len);
4101 else if (mode == FALLOC_FL_INSERT_RANGE)
4102 return smb3_insert_range(file, tcon, off, len);
4104 return smb3_simple_falloc(file, tcon, off, len, false);
4110 smb2_downgrade_oplock(struct TCP_Server_Info *server,
4111 struct cifsInodeInfo *cinode, __u32 oplock,
4112 unsigned int epoch, bool *purge_cache)
4114 server->ops->set_oplock_level(cinode, oplock, 0, NULL);
4118 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
4119 unsigned int epoch, bool *purge_cache);
4122 smb3_downgrade_oplock(struct TCP_Server_Info *server,
4123 struct cifsInodeInfo *cinode, __u32 oplock,
4124 unsigned int epoch, bool *purge_cache)
4126 unsigned int old_state = cinode->oplock;
4127 unsigned int old_epoch = cinode->epoch;
4128 unsigned int new_state;
4130 if (epoch > old_epoch) {
4131 smb21_set_oplock_level(cinode, oplock, 0, NULL);
4132 cinode->epoch = epoch;
4135 new_state = cinode->oplock;
4136 *purge_cache = false;
4138 if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
4139 (new_state & CIFS_CACHE_READ_FLG) == 0)
4140 *purge_cache = true;
4141 else if (old_state == new_state && (epoch - old_epoch > 1))
4142 *purge_cache = true;
4146 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
4147 unsigned int epoch, bool *purge_cache)
4150 cinode->lease_granted = false;
4151 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
4153 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
4154 cinode->oplock = CIFS_CACHE_RHW_FLG;
4155 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
4156 &cinode->vfs_inode);
4157 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
4158 cinode->oplock = CIFS_CACHE_RW_FLG;
4159 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
4160 &cinode->vfs_inode);
4161 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
4162 cinode->oplock = CIFS_CACHE_READ_FLG;
4163 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
4164 &cinode->vfs_inode);
4170 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
4171 unsigned int epoch, bool *purge_cache)
4173 char message[5] = {0};
4174 unsigned int new_oplock = 0;
4177 cinode->lease_granted = true;
4178 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
4181 /* Check if the server granted an oplock rather than a lease */
4182 if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
4183 return smb2_set_oplock_level(cinode, oplock, epoch,
4186 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
4187 new_oplock |= CIFS_CACHE_READ_FLG;
4188 strcat(message, "R");
4190 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
4191 new_oplock |= CIFS_CACHE_HANDLE_FLG;
4192 strcat(message, "H");
4194 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
4195 new_oplock |= CIFS_CACHE_WRITE_FLG;
4196 strcat(message, "W");
4199 strncpy(message, "None", sizeof(message));
4201 cinode->oplock = new_oplock;
4202 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
4203 &cinode->vfs_inode);
4207 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
4208 unsigned int epoch, bool *purge_cache)
4210 unsigned int old_oplock = cinode->oplock;
4212 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
4215 *purge_cache = false;
4216 if (old_oplock == CIFS_CACHE_READ_FLG) {
4217 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
4218 (epoch - cinode->epoch > 0))
4219 *purge_cache = true;
4220 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
4221 (epoch - cinode->epoch > 1))
4222 *purge_cache = true;
4223 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
4224 (epoch - cinode->epoch > 1))
4225 *purge_cache = true;
4226 else if (cinode->oplock == 0 &&
4227 (epoch - cinode->epoch > 0))
4228 *purge_cache = true;
4229 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
4230 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
4231 (epoch - cinode->epoch > 0))
4232 *purge_cache = true;
4233 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
4234 (epoch - cinode->epoch > 1))
4235 *purge_cache = true;
4237 cinode->epoch = epoch;
4242 smb2_is_read_op(__u32 oplock)
4244 return oplock == SMB2_OPLOCK_LEVEL_II;
4248 smb21_is_read_op(__u32 oplock)
4250 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
4251 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
4255 map_oplock_to_lease(u8 oplock)
4257 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
4258 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
4259 else if (oplock == SMB2_OPLOCK_LEVEL_II)
4260 return SMB2_LEASE_READ_CACHING;
4261 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
4262 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
4263 SMB2_LEASE_WRITE_CACHING;
4268 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
4270 struct create_lease *buf;
4272 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
4276 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
4277 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
4279 buf->ccontext.DataOffset = cpu_to_le16(offsetof
4280 (struct create_lease, lcontext));
4281 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
4282 buf->ccontext.NameOffset = cpu_to_le16(offsetof
4283 (struct create_lease, Name));
4284 buf->ccontext.NameLength = cpu_to_le16(4);
4285 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4294 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
4296 struct create_lease_v2 *buf;
4298 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
4302 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
4303 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
4305 buf->ccontext.DataOffset = cpu_to_le16(offsetof
4306 (struct create_lease_v2, lcontext));
4307 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
4308 buf->ccontext.NameOffset = cpu_to_le16(offsetof
4309 (struct create_lease_v2, Name));
4310 buf->ccontext.NameLength = cpu_to_le16(4);
4311 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4320 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
4322 struct create_lease *lc = (struct create_lease *)buf;
4324 *epoch = 0; /* not used */
4325 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
4326 return SMB2_OPLOCK_LEVEL_NOCHANGE;
4327 return le32_to_cpu(lc->lcontext.LeaseState);
4331 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
4333 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
4335 *epoch = le16_to_cpu(lc->lcontext.Epoch);
4336 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
4337 return SMB2_OPLOCK_LEVEL_NOCHANGE;
4339 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
4340 return le32_to_cpu(lc->lcontext.LeaseState);
4344 smb2_wp_retry_size(struct inode *inode)
4346 return min_t(unsigned int, CIFS_SB(inode->i_sb)->ctx->wsize,
4347 SMB2_MAX_BUFFER_SIZE);
4351 smb2_dir_needs_close(struct cifsFileInfo *cfile)
4353 return !cfile->invalidHandle;
4357 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
4358 struct smb_rqst *old_rq, __le16 cipher_type)
4360 struct smb2_sync_hdr *shdr =
4361 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
4363 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
4364 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
4365 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
4366 tr_hdr->Flags = cpu_to_le16(0x01);
4367 if ((cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4368 (cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4369 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
4371 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
4372 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
4375 /* We can not use the normal sg_set_buf() as we will sometimes pass a
4376 * stack object as buf.
4378 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
4379 unsigned int buflen)
4383 * VMAP_STACK (at least) puts stack into the vmalloc address space
4385 if (is_vmalloc_addr(buf))
4386 addr = vmalloc_to_page(buf);
4388 addr = virt_to_page(buf);
4389 sg_set_page(sg, addr, buflen, offset_in_page(buf));
4392 /* Assumes the first rqst has a transform header as the first iov.
4394 * rqst[0].rq_iov[0] is transform header
4395 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
4396 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
4398 static struct scatterlist *
4399 init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
4401 unsigned int sg_len;
4402 struct scatterlist *sg;
4405 unsigned int idx = 0;
4409 for (i = 0; i < num_rqst; i++)
4410 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
4412 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
4416 sg_init_table(sg, sg_len);
4417 for (i = 0; i < num_rqst; i++) {
4418 for (j = 0; j < rqst[i].rq_nvec; j++) {
4420 * The first rqst has a transform header where the
4421 * first 20 bytes are not part of the encrypted blob
4423 skip = (i == 0) && (j == 0) ? 20 : 0;
4424 smb2_sg_set_buf(&sg[idx++],
4425 rqst[i].rq_iov[j].iov_base + skip,
4426 rqst[i].rq_iov[j].iov_len - skip);
4429 for (j = 0; j < rqst[i].rq_npages; j++) {
4430 unsigned int len, offset;
4432 rqst_page_get_length(&rqst[i], j, &len, &offset);
4433 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
4436 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
4441 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
4443 struct cifs_ses *ses;
4446 spin_lock(&cifs_tcp_ses_lock);
4447 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
4448 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
4449 if (ses->Suid == ses_id) {
4450 ses_enc_key = enc ? ses->smb3encryptionkey :
4451 ses->smb3decryptionkey;
4452 memcpy(key, ses_enc_key, SMB3_ENC_DEC_KEY_SIZE);
4453 spin_unlock(&cifs_tcp_ses_lock);
4458 spin_unlock(&cifs_tcp_ses_lock);
4463 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
4464 * iov[0] - transform header (associate data),
4465 * iov[1-N] - SMB2 header and pages - data to encrypt.
4466 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
4470 crypt_message(struct TCP_Server_Info *server, int num_rqst,
4471 struct smb_rqst *rqst, int enc)
4473 struct smb2_transform_hdr *tr_hdr =
4474 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
4475 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
4477 struct scatterlist *sg;
4478 u8 sign[SMB2_SIGNATURE_SIZE] = {};
4479 u8 key[SMB3_ENC_DEC_KEY_SIZE];
4480 struct aead_request *req;
4482 unsigned int iv_len;
4483 DECLARE_CRYPTO_WAIT(wait);
4484 struct crypto_aead *tfm;
4485 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4487 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
4489 cifs_server_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
4494 rc = smb3_crypto_aead_allocate(server);
4496 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
4500 tfm = enc ? server->secmech.ccmaesencrypt :
4501 server->secmech.ccmaesdecrypt;
4503 if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
4504 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4505 rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
4507 rc = crypto_aead_setkey(tfm, key, SMB3_GCM128_CRYPTKEY_SIZE);
4510 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
4514 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
4516 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
4520 req = aead_request_alloc(tfm, GFP_KERNEL);
4522 cifs_server_dbg(VFS, "%s: Failed to alloc aead request\n", __func__);
4527 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
4528 crypt_len += SMB2_SIGNATURE_SIZE;
4531 sg = init_sg(num_rqst, rqst, sign);
4533 cifs_server_dbg(VFS, "%s: Failed to init sg\n", __func__);
4538 iv_len = crypto_aead_ivsize(tfm);
4539 iv = kzalloc(iv_len, GFP_KERNEL);
4541 cifs_server_dbg(VFS, "%s: Failed to alloc iv\n", __func__);
4546 if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4547 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4548 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
4551 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
4554 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
4555 aead_request_set_ad(req, assoc_data_len);
4557 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
4558 crypto_req_done, &wait);
4560 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
4561 : crypto_aead_decrypt(req), &wait);
4564 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
4575 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
4579 for (i = 0; i < num_rqst; i++) {
4580 if (rqst[i].rq_pages) {
4581 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
4582 put_page(rqst[i].rq_pages[j]);
4583 kfree(rqst[i].rq_pages);
4589 * This function will initialize new_rq and encrypt the content.
4590 * The first entry, new_rq[0], only contains a single iov which contains
4591 * a smb2_transform_hdr and is pre-allocated by the caller.
4592 * This function then populates new_rq[1+] with the content from olq_rq[0+].
4594 * The end result is an array of smb_rqst structures where the first structure
4595 * only contains a single iov for the transform header which we then can pass
4596 * to crypt_message().
4598 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
4599 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
4602 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
4603 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
4605 struct page **pages;
4606 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
4607 unsigned int npages;
4608 unsigned int orig_len = 0;
4612 for (i = 1; i < num_rqst; i++) {
4613 npages = old_rq[i - 1].rq_npages;
4614 pages = kmalloc_array(npages, sizeof(struct page *),
4619 new_rq[i].rq_pages = pages;
4620 new_rq[i].rq_npages = npages;
4621 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
4622 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
4623 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
4624 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
4625 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
4627 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
4629 for (j = 0; j < npages; j++) {
4630 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4635 /* copy pages form the old */
4636 for (j = 0; j < npages; j++) {
4638 unsigned int offset, len;
4640 rqst_page_get_length(&new_rq[i], j, &len, &offset);
4642 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
4643 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
4645 memcpy(dst, src, len);
4646 kunmap(new_rq[i].rq_pages[j]);
4647 kunmap(old_rq[i - 1].rq_pages[j]);
4651 /* fill the 1st iov with a transform header */
4652 fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
4654 rc = crypt_message(server, num_rqst, new_rq, 1);
4655 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
4662 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
4667 smb3_is_transform_hdr(void *buf)
4669 struct smb2_transform_hdr *trhdr = buf;
4671 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
4675 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
4676 unsigned int buf_data_size, struct page **pages,
4677 unsigned int npages, unsigned int page_data_size,
4681 struct smb_rqst rqst = {NULL};
4684 iov[0].iov_base = buf;
4685 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
4686 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
4687 iov[1].iov_len = buf_data_size;
4691 rqst.rq_pages = pages;
4692 rqst.rq_npages = npages;
4693 rqst.rq_pagesz = PAGE_SIZE;
4694 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
4696 rc = crypt_message(server, 1, &rqst, 0);
4697 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
4702 memmove(buf, iov[1].iov_base, buf_data_size);
4705 server->total_read = buf_data_size + page_data_size;
4711 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
4712 unsigned int npages, unsigned int len)
4717 for (i = 0; i < npages; i++) {
4718 struct page *page = pages[i];
4722 if (len >= PAGE_SIZE) {
4723 /* enough data to fill the page */
4727 zero_user(page, len, PAGE_SIZE - len);
4730 length = cifs_read_page_from_socket(server, page, 0, n);
4733 server->total_read += length;
4740 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
4741 unsigned int cur_off, struct bio_vec **page_vec)
4743 struct bio_vec *bvec;
4746 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
4750 for (i = 0; i < npages; i++) {
4751 bvec[i].bv_page = pages[i];
4752 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
4753 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
4754 data_size -= bvec[i].bv_len;
4757 if (data_size != 0) {
4758 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4768 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4769 char *buf, unsigned int buf_len, struct page **pages,
4770 unsigned int npages, unsigned int page_data_size,
4773 unsigned int data_offset;
4774 unsigned int data_len;
4775 unsigned int cur_off;
4776 unsigned int cur_page_idx;
4777 unsigned int pad_len;
4778 struct cifs_readdata *rdata = mid->callback_data;
4779 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
4780 struct bio_vec *bvec = NULL;
4781 struct iov_iter iter;
4784 bool use_rdma_mr = false;
4786 if (shdr->Command != SMB2_READ) {
4787 cifs_server_dbg(VFS, "only big read responses are supported\n");
4791 if (server->ops->is_session_expired &&
4792 server->ops->is_session_expired(buf)) {
4794 cifs_reconnect(server);
4798 if (server->ops->is_status_pending &&
4799 server->ops->is_status_pending(buf, server))
4802 /* set up first two iov to get credits */
4803 rdata->iov[0].iov_base = buf;
4804 rdata->iov[0].iov_len = 0;
4805 rdata->iov[1].iov_base = buf;
4806 rdata->iov[1].iov_len =
4807 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
4808 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4809 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4810 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4811 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4813 rdata->result = server->ops->map_error(buf, true);
4814 if (rdata->result != 0) {
4815 cifs_dbg(FYI, "%s: server returned error %d\n",
4816 __func__, rdata->result);
4817 /* normal error on read response */
4819 mid->mid_state = MID_RESPONSE_RECEIVED;
4821 dequeue_mid(mid, false);
4825 data_offset = server->ops->read_data_offset(buf);
4826 #ifdef CONFIG_CIFS_SMB_DIRECT
4827 use_rdma_mr = rdata->mr;
4829 data_len = server->ops->read_data_length(buf, use_rdma_mr);
4831 if (data_offset < server->vals->read_rsp_size) {
4833 * win2k8 sometimes sends an offset of 0 when the read
4834 * is beyond the EOF. Treat it as if the data starts just after
4837 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4838 __func__, data_offset);
4839 data_offset = server->vals->read_rsp_size;
4840 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4841 /* data_offset is beyond the end of smallbuf */
4842 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4843 __func__, data_offset);
4844 rdata->result = -EIO;
4846 mid->mid_state = MID_RESPONSE_MALFORMED;
4848 dequeue_mid(mid, rdata->result);
4852 pad_len = data_offset - server->vals->read_rsp_size;
4854 if (buf_len <= data_offset) {
4855 /* read response payload is in pages */
4856 cur_page_idx = pad_len / PAGE_SIZE;
4857 cur_off = pad_len % PAGE_SIZE;
4859 if (cur_page_idx != 0) {
4860 /* data offset is beyond the 1st page of response */
4861 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4862 __func__, data_offset);
4863 rdata->result = -EIO;
4865 mid->mid_state = MID_RESPONSE_MALFORMED;
4867 dequeue_mid(mid, rdata->result);
4871 if (data_len > page_data_size - pad_len) {
4872 /* data_len is corrupt -- discard frame */
4873 rdata->result = -EIO;
4875 mid->mid_state = MID_RESPONSE_MALFORMED;
4877 dequeue_mid(mid, rdata->result);
4881 rdata->result = init_read_bvec(pages, npages, page_data_size,
4883 if (rdata->result != 0) {
4885 mid->mid_state = MID_RESPONSE_MALFORMED;
4887 dequeue_mid(mid, rdata->result);
4891 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
4892 } else if (buf_len >= data_offset + data_len) {
4893 /* read response payload is in buf */
4894 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
4895 iov.iov_base = buf + data_offset;
4896 iov.iov_len = data_len;
4897 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
4899 /* read response payload cannot be in both buf and pages */
4900 WARN_ONCE(1, "buf can not contain only a part of read data");
4901 rdata->result = -EIO;
4903 mid->mid_state = MID_RESPONSE_MALFORMED;
4905 dequeue_mid(mid, rdata->result);
4909 length = rdata->copy_into_pages(server, rdata, &iter);
4917 mid->mid_state = MID_RESPONSE_RECEIVED;
4919 dequeue_mid(mid, false);
4923 struct smb2_decrypt_work {
4924 struct work_struct decrypt;
4925 struct TCP_Server_Info *server;
4926 struct page **ppages;
4928 unsigned int npages;
4933 static void smb2_decrypt_offload(struct work_struct *work)
4935 struct smb2_decrypt_work *dw = container_of(work,
4936 struct smb2_decrypt_work, decrypt);
4938 struct mid_q_entry *mid;
4940 rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
4941 dw->ppages, dw->npages, dw->len, true);
4943 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4947 dw->server->lstrp = jiffies;
4948 mid = smb2_find_dequeue_mid(dw->server, dw->buf);
4950 cifs_dbg(FYI, "mid not found\n");
4952 mid->decrypted = true;
4953 rc = handle_read_data(dw->server, mid, dw->buf,
4954 dw->server->vals->read_rsp_size,
4955 dw->ppages, dw->npages, dw->len,
4958 #ifdef CONFIG_CIFS_STATS2
4959 mid->when_received = jiffies;
4961 if (dw->server->ops->is_network_name_deleted)
4962 dw->server->ops->is_network_name_deleted(dw->buf,
4967 spin_lock(&GlobalMid_Lock);
4968 if (dw->server->tcpStatus == CifsNeedReconnect) {
4969 mid->mid_state = MID_RETRY_NEEDED;
4970 spin_unlock(&GlobalMid_Lock);
4973 mid->mid_state = MID_REQUEST_SUBMITTED;
4974 mid->mid_flags &= ~(MID_DELETED);
4975 list_add_tail(&mid->qhead,
4976 &dw->server->pending_mid_q);
4977 spin_unlock(&GlobalMid_Lock);
4980 cifs_mid_q_entry_release(mid);
4984 for (i = dw->npages-1; i >= 0; i--)
4985 put_page(dw->ppages[i]);
4988 cifs_small_buf_release(dw->buf);
4994 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4997 char *buf = server->smallbuf;
4998 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4999 unsigned int npages;
5000 struct page **pages;
5002 unsigned int buflen = server->pdu_size;
5005 struct smb2_decrypt_work *dw;
5008 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
5009 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
5011 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
5014 server->total_read += rc;
5016 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
5017 server->vals->read_rsp_size;
5018 npages = DIV_ROUND_UP(len, PAGE_SIZE);
5020 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
5026 for (; i < npages; i++) {
5027 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
5034 /* read read data into pages */
5035 rc = read_data_into_pages(server, pages, npages, len);
5039 rc = cifs_discard_remaining_data(server);
5044 * For large reads, offload to different thread for better performance,
5045 * use more cores decrypting which can be expensive
5048 if ((server->min_offload) && (server->in_flight > 1) &&
5049 (server->pdu_size >= server->min_offload)) {
5050 dw = kmalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
5052 goto non_offloaded_decrypt;
5054 dw->buf = server->smallbuf;
5055 server->smallbuf = (char *)cifs_small_buf_get();
5057 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
5059 dw->npages = npages;
5060 dw->server = server;
5063 queue_work(decrypt_wq, &dw->decrypt);
5064 *num_mids = 0; /* worker thread takes care of finding mid */
5068 non_offloaded_decrypt:
5069 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
5070 pages, npages, len, false);
5074 *mid = smb2_find_mid(server, buf);
5076 cifs_dbg(FYI, "mid not found\n");
5078 cifs_dbg(FYI, "mid found\n");
5079 (*mid)->decrypted = true;
5080 rc = handle_read_data(server, *mid, buf,
5081 server->vals->read_rsp_size,
5082 pages, npages, len, false);
5084 if (server->ops->is_network_name_deleted) {
5085 server->ops->is_network_name_deleted(buf,
5092 for (i = i - 1; i >= 0; i--)
5097 cifs_discard_remaining_data(server);
5102 receive_encrypted_standard(struct TCP_Server_Info *server,
5103 struct mid_q_entry **mids, char **bufs,
5107 char *buf = server->smallbuf;
5108 struct smb2_sync_hdr *shdr;
5109 unsigned int pdu_length = server->pdu_size;
5110 unsigned int buf_size;
5111 struct mid_q_entry *mid_entry;
5113 char *next_buffer = NULL;
5117 /* switch to large buffer if too big for a small one */
5118 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
5119 server->large_buf = true;
5120 memcpy(server->bigbuf, buf, server->total_read);
5121 buf = server->bigbuf;
5124 /* now read the rest */
5125 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
5126 pdu_length - HEADER_SIZE(server) + 1);
5129 server->total_read += length;
5131 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
5132 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0, false);
5136 next_is_large = server->large_buf;
5138 shdr = (struct smb2_sync_hdr *)buf;
5139 if (shdr->NextCommand) {
5141 next_buffer = (char *)cifs_buf_get();
5143 next_buffer = (char *)cifs_small_buf_get();
5145 buf + le32_to_cpu(shdr->NextCommand),
5146 pdu_length - le32_to_cpu(shdr->NextCommand));
5149 mid_entry = smb2_find_mid(server, buf);
5150 if (mid_entry == NULL)
5151 cifs_dbg(FYI, "mid not found\n");
5153 cifs_dbg(FYI, "mid found\n");
5154 mid_entry->decrypted = true;
5155 mid_entry->resp_buf_size = server->pdu_size;
5158 if (*num_mids >= MAX_COMPOUND) {
5159 cifs_server_dbg(VFS, "too many PDUs in compound\n");
5162 bufs[*num_mids] = buf;
5163 mids[(*num_mids)++] = mid_entry;
5165 if (mid_entry && mid_entry->handle)
5166 ret = mid_entry->handle(server, mid_entry);
5168 ret = cifs_handle_standard(server, mid_entry);
5170 if (ret == 0 && shdr->NextCommand) {
5171 pdu_length -= le32_to_cpu(shdr->NextCommand);
5172 server->large_buf = next_is_large;
5174 server->bigbuf = buf = next_buffer;
5176 server->smallbuf = buf = next_buffer;
5178 } else if (ret != 0) {
5180 * ret != 0 here means that we didn't get to handle_mid() thus
5181 * server->smallbuf and server->bigbuf are still valid. We need
5182 * to free next_buffer because it is not going to be used
5186 free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
5188 free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
5195 smb3_receive_transform(struct TCP_Server_Info *server,
5196 struct mid_q_entry **mids, char **bufs, int *num_mids)
5198 char *buf = server->smallbuf;
5199 unsigned int pdu_length = server->pdu_size;
5200 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
5201 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
5203 if (pdu_length < sizeof(struct smb2_transform_hdr) +
5204 sizeof(struct smb2_sync_hdr)) {
5205 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
5207 cifs_reconnect(server);
5208 return -ECONNABORTED;
5211 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
5212 cifs_server_dbg(VFS, "Transform message is broken\n");
5213 cifs_reconnect(server);
5214 return -ECONNABORTED;
5217 /* TODO: add support for compounds containing READ. */
5218 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
5219 return receive_encrypted_read(server, &mids[0], num_mids);
5222 return receive_encrypted_standard(server, mids, bufs, num_mids);
5226 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
5228 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
5230 return handle_read_data(server, mid, buf, server->pdu_size,
5235 smb2_next_header(char *buf)
5237 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
5238 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
5240 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
5241 return sizeof(struct smb2_transform_hdr) +
5242 le32_to_cpu(t_hdr->OriginalMessageSize);
5244 return le32_to_cpu(hdr->NextCommand);
5248 smb2_make_node(unsigned int xid, struct inode *inode,
5249 struct dentry *dentry, struct cifs_tcon *tcon,
5250 const char *full_path, umode_t mode, dev_t dev)
5252 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
5254 FILE_ALL_INFO *buf = NULL;
5255 struct cifs_io_parms io_parms = {0};
5257 struct cifs_fid fid;
5258 struct cifs_open_parms oparms;
5259 unsigned int bytes_written;
5260 struct win_dev *pdev;
5264 * Check if mounted with mount parm 'sfu' mount parm.
5265 * SFU emulation should work with all servers, but only
5266 * supports block and char device (no socket & fifo),
5267 * and was used by default in earlier versions of Windows
5269 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
5273 * TODO: Add ability to create instead via reparse point. Windows (e.g.
5274 * their current NFS server) uses this approach to expose special files
5275 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
5278 if (!S_ISCHR(mode) && !S_ISBLK(mode))
5281 cifs_dbg(FYI, "sfu compat create special file\n");
5283 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
5290 oparms.cifs_sb = cifs_sb;
5291 oparms.desired_access = GENERIC_WRITE;
5292 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
5293 CREATE_OPTION_SPECIAL);
5294 oparms.disposition = FILE_CREATE;
5295 oparms.path = full_path;
5297 oparms.reconnect = false;
5299 if (tcon->ses->server->oplocks)
5300 oplock = REQ_OPLOCK;
5303 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
5308 * BB Do not bother to decode buf since no local inode yet to put
5309 * timestamps in, but we can reuse it safely.
5312 pdev = (struct win_dev *)buf;
5313 io_parms.pid = current->tgid;
5314 io_parms.tcon = tcon;
5315 io_parms.offset = 0;
5316 io_parms.length = sizeof(struct win_dev);
5317 iov[1].iov_base = buf;
5318 iov[1].iov_len = sizeof(struct win_dev);
5319 if (S_ISCHR(mode)) {
5320 memcpy(pdev->type, "IntxCHR", 8);
5321 pdev->major = cpu_to_le64(MAJOR(dev));
5322 pdev->minor = cpu_to_le64(MINOR(dev));
5323 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
5324 &bytes_written, iov, 1);
5325 } else if (S_ISBLK(mode)) {
5326 memcpy(pdev->type, "IntxBLK", 8);
5327 pdev->major = cpu_to_le64(MAJOR(dev));
5328 pdev->minor = cpu_to_le64(MINOR(dev));
5329 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
5330 &bytes_written, iov, 1);
5332 tcon->ses->server->ops->close(xid, tcon, &fid);
5335 /* FIXME: add code here to set EAs */
5342 struct smb_version_operations smb20_operations = {
5343 .compare_fids = smb2_compare_fids,
5344 .setup_request = smb2_setup_request,
5345 .setup_async_request = smb2_setup_async_request,
5346 .check_receive = smb2_check_receive,
5347 .add_credits = smb2_add_credits,
5348 .set_credits = smb2_set_credits,
5349 .get_credits_field = smb2_get_credits_field,
5350 .get_credits = smb2_get_credits,
5351 .wait_mtu_credits = cifs_wait_mtu_credits,
5352 .get_next_mid = smb2_get_next_mid,
5353 .revert_current_mid = smb2_revert_current_mid,
5354 .read_data_offset = smb2_read_data_offset,
5355 .read_data_length = smb2_read_data_length,
5356 .map_error = map_smb2_to_linux_error,
5357 .find_mid = smb2_find_mid,
5358 .check_message = smb2_check_message,
5359 .dump_detail = smb2_dump_detail,
5360 .clear_stats = smb2_clear_stats,
5361 .print_stats = smb2_print_stats,
5362 .is_oplock_break = smb2_is_valid_oplock_break,
5363 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5364 .downgrade_oplock = smb2_downgrade_oplock,
5365 .need_neg = smb2_need_neg,
5366 .negotiate = smb2_negotiate,
5367 .negotiate_wsize = smb2_negotiate_wsize,
5368 .negotiate_rsize = smb2_negotiate_rsize,
5369 .sess_setup = SMB2_sess_setup,
5370 .logoff = SMB2_logoff,
5371 .tree_connect = SMB2_tcon,
5372 .tree_disconnect = SMB2_tdis,
5373 .qfs_tcon = smb2_qfs_tcon,
5374 .is_path_accessible = smb2_is_path_accessible,
5375 .can_echo = smb2_can_echo,
5377 .query_path_info = smb2_query_path_info,
5378 .get_srv_inum = smb2_get_srv_inum,
5379 .query_file_info = smb2_query_file_info,
5380 .set_path_size = smb2_set_path_size,
5381 .set_file_size = smb2_set_file_size,
5382 .set_file_info = smb2_set_file_info,
5383 .set_compression = smb2_set_compression,
5384 .mkdir = smb2_mkdir,
5385 .mkdir_setinfo = smb2_mkdir_setinfo,
5386 .rmdir = smb2_rmdir,
5387 .unlink = smb2_unlink,
5388 .rename = smb2_rename_path,
5389 .create_hardlink = smb2_create_hardlink,
5390 .query_symlink = smb2_query_symlink,
5391 .query_mf_symlink = smb3_query_mf_symlink,
5392 .create_mf_symlink = smb3_create_mf_symlink,
5393 .open = smb2_open_file,
5394 .set_fid = smb2_set_fid,
5395 .close = smb2_close_file,
5396 .flush = smb2_flush_file,
5397 .async_readv = smb2_async_readv,
5398 .async_writev = smb2_async_writev,
5399 .sync_read = smb2_sync_read,
5400 .sync_write = smb2_sync_write,
5401 .query_dir_first = smb2_query_dir_first,
5402 .query_dir_next = smb2_query_dir_next,
5403 .close_dir = smb2_close_dir,
5404 .calc_smb_size = smb2_calc_size,
5405 .is_status_pending = smb2_is_status_pending,
5406 .is_session_expired = smb2_is_session_expired,
5407 .oplock_response = smb2_oplock_response,
5408 .queryfs = smb2_queryfs,
5409 .mand_lock = smb2_mand_lock,
5410 .mand_unlock_range = smb2_unlock_range,
5411 .push_mand_locks = smb2_push_mandatory_locks,
5412 .get_lease_key = smb2_get_lease_key,
5413 .set_lease_key = smb2_set_lease_key,
5414 .new_lease_key = smb2_new_lease_key,
5415 .calc_signature = smb2_calc_signature,
5416 .is_read_op = smb2_is_read_op,
5417 .set_oplock_level = smb2_set_oplock_level,
5418 .create_lease_buf = smb2_create_lease_buf,
5419 .parse_lease_buf = smb2_parse_lease_buf,
5420 .copychunk_range = smb2_copychunk_range,
5421 .wp_retry_size = smb2_wp_retry_size,
5422 .dir_needs_close = smb2_dir_needs_close,
5423 .get_dfs_refer = smb2_get_dfs_refer,
5424 .select_sectype = smb2_select_sectype,
5425 #ifdef CONFIG_CIFS_XATTR
5426 .query_all_EAs = smb2_query_eas,
5427 .set_EA = smb2_set_ea,
5428 #endif /* CIFS_XATTR */
5429 .get_acl = get_smb2_acl,
5430 .get_acl_by_fid = get_smb2_acl_by_fid,
5431 .set_acl = set_smb2_acl,
5432 .next_header = smb2_next_header,
5433 .ioctl_query_info = smb2_ioctl_query_info,
5434 .make_node = smb2_make_node,
5435 .fiemap = smb3_fiemap,
5436 .llseek = smb3_llseek,
5437 .is_status_io_timeout = smb2_is_status_io_timeout,
5438 .is_network_name_deleted = smb2_is_network_name_deleted,
5441 struct smb_version_operations smb21_operations = {
5442 .compare_fids = smb2_compare_fids,
5443 .setup_request = smb2_setup_request,
5444 .setup_async_request = smb2_setup_async_request,
5445 .check_receive = smb2_check_receive,
5446 .add_credits = smb2_add_credits,
5447 .set_credits = smb2_set_credits,
5448 .get_credits_field = smb2_get_credits_field,
5449 .get_credits = smb2_get_credits,
5450 .wait_mtu_credits = smb2_wait_mtu_credits,
5451 .adjust_credits = smb2_adjust_credits,
5452 .get_next_mid = smb2_get_next_mid,
5453 .revert_current_mid = smb2_revert_current_mid,
5454 .read_data_offset = smb2_read_data_offset,
5455 .read_data_length = smb2_read_data_length,
5456 .map_error = map_smb2_to_linux_error,
5457 .find_mid = smb2_find_mid,
5458 .check_message = smb2_check_message,
5459 .dump_detail = smb2_dump_detail,
5460 .clear_stats = smb2_clear_stats,
5461 .print_stats = smb2_print_stats,
5462 .is_oplock_break = smb2_is_valid_oplock_break,
5463 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5464 .downgrade_oplock = smb2_downgrade_oplock,
5465 .need_neg = smb2_need_neg,
5466 .negotiate = smb2_negotiate,
5467 .negotiate_wsize = smb2_negotiate_wsize,
5468 .negotiate_rsize = smb2_negotiate_rsize,
5469 .sess_setup = SMB2_sess_setup,
5470 .logoff = SMB2_logoff,
5471 .tree_connect = SMB2_tcon,
5472 .tree_disconnect = SMB2_tdis,
5473 .qfs_tcon = smb2_qfs_tcon,
5474 .is_path_accessible = smb2_is_path_accessible,
5475 .can_echo = smb2_can_echo,
5477 .query_path_info = smb2_query_path_info,
5478 .get_srv_inum = smb2_get_srv_inum,
5479 .query_file_info = smb2_query_file_info,
5480 .set_path_size = smb2_set_path_size,
5481 .set_file_size = smb2_set_file_size,
5482 .set_file_info = smb2_set_file_info,
5483 .set_compression = smb2_set_compression,
5484 .mkdir = smb2_mkdir,
5485 .mkdir_setinfo = smb2_mkdir_setinfo,
5486 .rmdir = smb2_rmdir,
5487 .unlink = smb2_unlink,
5488 .rename = smb2_rename_path,
5489 .create_hardlink = smb2_create_hardlink,
5490 .query_symlink = smb2_query_symlink,
5491 .query_mf_symlink = smb3_query_mf_symlink,
5492 .create_mf_symlink = smb3_create_mf_symlink,
5493 .open = smb2_open_file,
5494 .set_fid = smb2_set_fid,
5495 .close = smb2_close_file,
5496 .flush = smb2_flush_file,
5497 .async_readv = smb2_async_readv,
5498 .async_writev = smb2_async_writev,
5499 .sync_read = smb2_sync_read,
5500 .sync_write = smb2_sync_write,
5501 .query_dir_first = smb2_query_dir_first,
5502 .query_dir_next = smb2_query_dir_next,
5503 .close_dir = smb2_close_dir,
5504 .calc_smb_size = smb2_calc_size,
5505 .is_status_pending = smb2_is_status_pending,
5506 .is_session_expired = smb2_is_session_expired,
5507 .oplock_response = smb2_oplock_response,
5508 .queryfs = smb2_queryfs,
5509 .mand_lock = smb2_mand_lock,
5510 .mand_unlock_range = smb2_unlock_range,
5511 .push_mand_locks = smb2_push_mandatory_locks,
5512 .get_lease_key = smb2_get_lease_key,
5513 .set_lease_key = smb2_set_lease_key,
5514 .new_lease_key = smb2_new_lease_key,
5515 .calc_signature = smb2_calc_signature,
5516 .is_read_op = smb21_is_read_op,
5517 .set_oplock_level = smb21_set_oplock_level,
5518 .create_lease_buf = smb2_create_lease_buf,
5519 .parse_lease_buf = smb2_parse_lease_buf,
5520 .copychunk_range = smb2_copychunk_range,
5521 .wp_retry_size = smb2_wp_retry_size,
5522 .dir_needs_close = smb2_dir_needs_close,
5523 .enum_snapshots = smb3_enum_snapshots,
5524 .notify = smb3_notify,
5525 .get_dfs_refer = smb2_get_dfs_refer,
5526 .select_sectype = smb2_select_sectype,
5527 #ifdef CONFIG_CIFS_XATTR
5528 .query_all_EAs = smb2_query_eas,
5529 .set_EA = smb2_set_ea,
5530 #endif /* CIFS_XATTR */
5531 .get_acl = get_smb2_acl,
5532 .get_acl_by_fid = get_smb2_acl_by_fid,
5533 .set_acl = set_smb2_acl,
5534 .next_header = smb2_next_header,
5535 .ioctl_query_info = smb2_ioctl_query_info,
5536 .make_node = smb2_make_node,
5537 .fiemap = smb3_fiemap,
5538 .llseek = smb3_llseek,
5539 .is_status_io_timeout = smb2_is_status_io_timeout,
5540 .is_network_name_deleted = smb2_is_network_name_deleted,
5543 struct smb_version_operations smb30_operations = {
5544 .compare_fids = smb2_compare_fids,
5545 .setup_request = smb2_setup_request,
5546 .setup_async_request = smb2_setup_async_request,
5547 .check_receive = smb2_check_receive,
5548 .add_credits = smb2_add_credits,
5549 .set_credits = smb2_set_credits,
5550 .get_credits_field = smb2_get_credits_field,
5551 .get_credits = smb2_get_credits,
5552 .wait_mtu_credits = smb2_wait_mtu_credits,
5553 .adjust_credits = smb2_adjust_credits,
5554 .get_next_mid = smb2_get_next_mid,
5555 .revert_current_mid = smb2_revert_current_mid,
5556 .read_data_offset = smb2_read_data_offset,
5557 .read_data_length = smb2_read_data_length,
5558 .map_error = map_smb2_to_linux_error,
5559 .find_mid = smb2_find_mid,
5560 .check_message = smb2_check_message,
5561 .dump_detail = smb2_dump_detail,
5562 .clear_stats = smb2_clear_stats,
5563 .print_stats = smb2_print_stats,
5564 .dump_share_caps = smb2_dump_share_caps,
5565 .is_oplock_break = smb2_is_valid_oplock_break,
5566 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5567 .downgrade_oplock = smb3_downgrade_oplock,
5568 .need_neg = smb2_need_neg,
5569 .negotiate = smb2_negotiate,
5570 .negotiate_wsize = smb3_negotiate_wsize,
5571 .negotiate_rsize = smb3_negotiate_rsize,
5572 .sess_setup = SMB2_sess_setup,
5573 .logoff = SMB2_logoff,
5574 .tree_connect = SMB2_tcon,
5575 .tree_disconnect = SMB2_tdis,
5576 .qfs_tcon = smb3_qfs_tcon,
5577 .is_path_accessible = smb2_is_path_accessible,
5578 .can_echo = smb2_can_echo,
5580 .query_path_info = smb2_query_path_info,
5581 /* WSL tags introduced long after smb2.1, enable for SMB3, 3.11 only */
5582 .query_reparse_tag = smb2_query_reparse_tag,
5583 .get_srv_inum = smb2_get_srv_inum,
5584 .query_file_info = smb2_query_file_info,
5585 .set_path_size = smb2_set_path_size,
5586 .set_file_size = smb2_set_file_size,
5587 .set_file_info = smb2_set_file_info,
5588 .set_compression = smb2_set_compression,
5589 .mkdir = smb2_mkdir,
5590 .mkdir_setinfo = smb2_mkdir_setinfo,
5591 .rmdir = smb2_rmdir,
5592 .unlink = smb2_unlink,
5593 .rename = smb2_rename_path,
5594 .create_hardlink = smb2_create_hardlink,
5595 .query_symlink = smb2_query_symlink,
5596 .query_mf_symlink = smb3_query_mf_symlink,
5597 .create_mf_symlink = smb3_create_mf_symlink,
5598 .open = smb2_open_file,
5599 .set_fid = smb2_set_fid,
5600 .close = smb2_close_file,
5601 .close_getattr = smb2_close_getattr,
5602 .flush = smb2_flush_file,
5603 .async_readv = smb2_async_readv,
5604 .async_writev = smb2_async_writev,
5605 .sync_read = smb2_sync_read,
5606 .sync_write = smb2_sync_write,
5607 .query_dir_first = smb2_query_dir_first,
5608 .query_dir_next = smb2_query_dir_next,
5609 .close_dir = smb2_close_dir,
5610 .calc_smb_size = smb2_calc_size,
5611 .is_status_pending = smb2_is_status_pending,
5612 .is_session_expired = smb2_is_session_expired,
5613 .oplock_response = smb2_oplock_response,
5614 .queryfs = smb2_queryfs,
5615 .mand_lock = smb2_mand_lock,
5616 .mand_unlock_range = smb2_unlock_range,
5617 .push_mand_locks = smb2_push_mandatory_locks,
5618 .get_lease_key = smb2_get_lease_key,
5619 .set_lease_key = smb2_set_lease_key,
5620 .new_lease_key = smb2_new_lease_key,
5621 .generate_signingkey = generate_smb30signingkey,
5622 .calc_signature = smb3_calc_signature,
5623 .set_integrity = smb3_set_integrity,
5624 .is_read_op = smb21_is_read_op,
5625 .set_oplock_level = smb3_set_oplock_level,
5626 .create_lease_buf = smb3_create_lease_buf,
5627 .parse_lease_buf = smb3_parse_lease_buf,
5628 .copychunk_range = smb2_copychunk_range,
5629 .duplicate_extents = smb2_duplicate_extents,
5630 .validate_negotiate = smb3_validate_negotiate,
5631 .wp_retry_size = smb2_wp_retry_size,
5632 .dir_needs_close = smb2_dir_needs_close,
5633 .fallocate = smb3_fallocate,
5634 .enum_snapshots = smb3_enum_snapshots,
5635 .notify = smb3_notify,
5636 .init_transform_rq = smb3_init_transform_rq,
5637 .is_transform_hdr = smb3_is_transform_hdr,
5638 .receive_transform = smb3_receive_transform,
5639 .get_dfs_refer = smb2_get_dfs_refer,
5640 .select_sectype = smb2_select_sectype,
5641 #ifdef CONFIG_CIFS_XATTR
5642 .query_all_EAs = smb2_query_eas,
5643 .set_EA = smb2_set_ea,
5644 #endif /* CIFS_XATTR */
5645 .get_acl = get_smb2_acl,
5646 .get_acl_by_fid = get_smb2_acl_by_fid,
5647 .set_acl = set_smb2_acl,
5648 .next_header = smb2_next_header,
5649 .ioctl_query_info = smb2_ioctl_query_info,
5650 .make_node = smb2_make_node,
5651 .fiemap = smb3_fiemap,
5652 .llseek = smb3_llseek,
5653 .is_status_io_timeout = smb2_is_status_io_timeout,
5654 .is_network_name_deleted = smb2_is_network_name_deleted,
5657 struct smb_version_operations smb311_operations = {
5658 .compare_fids = smb2_compare_fids,
5659 .setup_request = smb2_setup_request,
5660 .setup_async_request = smb2_setup_async_request,
5661 .check_receive = smb2_check_receive,
5662 .add_credits = smb2_add_credits,
5663 .set_credits = smb2_set_credits,
5664 .get_credits_field = smb2_get_credits_field,
5665 .get_credits = smb2_get_credits,
5666 .wait_mtu_credits = smb2_wait_mtu_credits,
5667 .adjust_credits = smb2_adjust_credits,
5668 .get_next_mid = smb2_get_next_mid,
5669 .revert_current_mid = smb2_revert_current_mid,
5670 .read_data_offset = smb2_read_data_offset,
5671 .read_data_length = smb2_read_data_length,
5672 .map_error = map_smb2_to_linux_error,
5673 .find_mid = smb2_find_mid,
5674 .check_message = smb2_check_message,
5675 .dump_detail = smb2_dump_detail,
5676 .clear_stats = smb2_clear_stats,
5677 .print_stats = smb2_print_stats,
5678 .dump_share_caps = smb2_dump_share_caps,
5679 .is_oplock_break = smb2_is_valid_oplock_break,
5680 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5681 .downgrade_oplock = smb3_downgrade_oplock,
5682 .need_neg = smb2_need_neg,
5683 .negotiate = smb2_negotiate,
5684 .negotiate_wsize = smb3_negotiate_wsize,
5685 .negotiate_rsize = smb3_negotiate_rsize,
5686 .sess_setup = SMB2_sess_setup,
5687 .logoff = SMB2_logoff,
5688 .tree_connect = SMB2_tcon,
5689 .tree_disconnect = SMB2_tdis,
5690 .qfs_tcon = smb3_qfs_tcon,
5691 .is_path_accessible = smb2_is_path_accessible,
5692 .can_echo = smb2_can_echo,
5694 .query_path_info = smb2_query_path_info,
5695 .query_reparse_tag = smb2_query_reparse_tag,
5696 .get_srv_inum = smb2_get_srv_inum,
5697 .query_file_info = smb2_query_file_info,
5698 .set_path_size = smb2_set_path_size,
5699 .set_file_size = smb2_set_file_size,
5700 .set_file_info = smb2_set_file_info,
5701 .set_compression = smb2_set_compression,
5702 .mkdir = smb2_mkdir,
5703 .mkdir_setinfo = smb2_mkdir_setinfo,
5704 .posix_mkdir = smb311_posix_mkdir,
5705 .rmdir = smb2_rmdir,
5706 .unlink = smb2_unlink,
5707 .rename = smb2_rename_path,
5708 .create_hardlink = smb2_create_hardlink,
5709 .query_symlink = smb2_query_symlink,
5710 .query_mf_symlink = smb3_query_mf_symlink,
5711 .create_mf_symlink = smb3_create_mf_symlink,
5712 .open = smb2_open_file,
5713 .set_fid = smb2_set_fid,
5714 .close = smb2_close_file,
5715 .close_getattr = smb2_close_getattr,
5716 .flush = smb2_flush_file,
5717 .async_readv = smb2_async_readv,
5718 .async_writev = smb2_async_writev,
5719 .sync_read = smb2_sync_read,
5720 .sync_write = smb2_sync_write,
5721 .query_dir_first = smb2_query_dir_first,
5722 .query_dir_next = smb2_query_dir_next,
5723 .close_dir = smb2_close_dir,
5724 .calc_smb_size = smb2_calc_size,
5725 .is_status_pending = smb2_is_status_pending,
5726 .is_session_expired = smb2_is_session_expired,
5727 .oplock_response = smb2_oplock_response,
5728 .queryfs = smb311_queryfs,
5729 .mand_lock = smb2_mand_lock,
5730 .mand_unlock_range = smb2_unlock_range,
5731 .push_mand_locks = smb2_push_mandatory_locks,
5732 .get_lease_key = smb2_get_lease_key,
5733 .set_lease_key = smb2_set_lease_key,
5734 .new_lease_key = smb2_new_lease_key,
5735 .generate_signingkey = generate_smb311signingkey,
5736 .calc_signature = smb3_calc_signature,
5737 .set_integrity = smb3_set_integrity,
5738 .is_read_op = smb21_is_read_op,
5739 .set_oplock_level = smb3_set_oplock_level,
5740 .create_lease_buf = smb3_create_lease_buf,
5741 .parse_lease_buf = smb3_parse_lease_buf,
5742 .copychunk_range = smb2_copychunk_range,
5743 .duplicate_extents = smb2_duplicate_extents,
5744 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5745 .wp_retry_size = smb2_wp_retry_size,
5746 .dir_needs_close = smb2_dir_needs_close,
5747 .fallocate = smb3_fallocate,
5748 .enum_snapshots = smb3_enum_snapshots,
5749 .notify = smb3_notify,
5750 .init_transform_rq = smb3_init_transform_rq,
5751 .is_transform_hdr = smb3_is_transform_hdr,
5752 .receive_transform = smb3_receive_transform,
5753 .get_dfs_refer = smb2_get_dfs_refer,
5754 .select_sectype = smb2_select_sectype,
5755 #ifdef CONFIG_CIFS_XATTR
5756 .query_all_EAs = smb2_query_eas,
5757 .set_EA = smb2_set_ea,
5758 #endif /* CIFS_XATTR */
5759 .get_acl = get_smb2_acl,
5760 .get_acl_by_fid = get_smb2_acl_by_fid,
5761 .set_acl = set_smb2_acl,
5762 .next_header = smb2_next_header,
5763 .ioctl_query_info = smb2_ioctl_query_info,
5764 .make_node = smb2_make_node,
5765 .fiemap = smb3_fiemap,
5766 .llseek = smb3_llseek,
5767 .is_status_io_timeout = smb2_is_status_io_timeout,
5768 .is_network_name_deleted = smb2_is_network_name_deleted,
5771 struct smb_version_values smb20_values = {
5772 .version_string = SMB20_VERSION_STRING,
5773 .protocol_id = SMB20_PROT_ID,
5774 .req_capabilities = 0, /* MBZ */
5775 .large_lock_type = 0,
5776 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5777 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5778 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5779 .header_size = sizeof(struct smb2_sync_hdr),
5780 .header_preamble_size = 0,
5781 .max_header_size = MAX_SMB2_HDR_SIZE,
5782 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5783 .lock_cmd = SMB2_LOCK,
5785 .cap_nt_find = SMB2_NT_FIND,
5786 .cap_large_files = SMB2_LARGE_FILES,
5787 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5788 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5789 .create_lease_size = sizeof(struct create_lease),
5792 struct smb_version_values smb21_values = {
5793 .version_string = SMB21_VERSION_STRING,
5794 .protocol_id = SMB21_PROT_ID,
5795 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5796 .large_lock_type = 0,
5797 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5798 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5799 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5800 .header_size = sizeof(struct smb2_sync_hdr),
5801 .header_preamble_size = 0,
5802 .max_header_size = MAX_SMB2_HDR_SIZE,
5803 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5804 .lock_cmd = SMB2_LOCK,
5806 .cap_nt_find = SMB2_NT_FIND,
5807 .cap_large_files = SMB2_LARGE_FILES,
5808 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5809 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5810 .create_lease_size = sizeof(struct create_lease),
5813 struct smb_version_values smb3any_values = {
5814 .version_string = SMB3ANY_VERSION_STRING,
5815 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5816 .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,
5817 .large_lock_type = 0,
5818 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5819 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5820 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5821 .header_size = sizeof(struct smb2_sync_hdr),
5822 .header_preamble_size = 0,
5823 .max_header_size = MAX_SMB2_HDR_SIZE,
5824 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5825 .lock_cmd = SMB2_LOCK,
5827 .cap_nt_find = SMB2_NT_FIND,
5828 .cap_large_files = SMB2_LARGE_FILES,
5829 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5830 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5831 .create_lease_size = sizeof(struct create_lease_v2),
5834 struct smb_version_values smbdefault_values = {
5835 .version_string = SMBDEFAULT_VERSION_STRING,
5836 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5837 .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,
5838 .large_lock_type = 0,
5839 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5840 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5841 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5842 .header_size = sizeof(struct smb2_sync_hdr),
5843 .header_preamble_size = 0,
5844 .max_header_size = MAX_SMB2_HDR_SIZE,
5845 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5846 .lock_cmd = SMB2_LOCK,
5848 .cap_nt_find = SMB2_NT_FIND,
5849 .cap_large_files = SMB2_LARGE_FILES,
5850 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5851 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5852 .create_lease_size = sizeof(struct create_lease_v2),
5855 struct smb_version_values smb30_values = {
5856 .version_string = SMB30_VERSION_STRING,
5857 .protocol_id = SMB30_PROT_ID,
5858 .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,
5859 .large_lock_type = 0,
5860 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5861 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5862 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5863 .header_size = sizeof(struct smb2_sync_hdr),
5864 .header_preamble_size = 0,
5865 .max_header_size = MAX_SMB2_HDR_SIZE,
5866 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5867 .lock_cmd = SMB2_LOCK,
5869 .cap_nt_find = SMB2_NT_FIND,
5870 .cap_large_files = SMB2_LARGE_FILES,
5871 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5872 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5873 .create_lease_size = sizeof(struct create_lease_v2),
5876 struct smb_version_values smb302_values = {
5877 .version_string = SMB302_VERSION_STRING,
5878 .protocol_id = SMB302_PROT_ID,
5879 .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,
5880 .large_lock_type = 0,
5881 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5882 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5883 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5884 .header_size = sizeof(struct smb2_sync_hdr),
5885 .header_preamble_size = 0,
5886 .max_header_size = MAX_SMB2_HDR_SIZE,
5887 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5888 .lock_cmd = SMB2_LOCK,
5890 .cap_nt_find = SMB2_NT_FIND,
5891 .cap_large_files = SMB2_LARGE_FILES,
5892 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5893 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5894 .create_lease_size = sizeof(struct create_lease_v2),
5897 struct smb_version_values smb311_values = {
5898 .version_string = SMB311_VERSION_STRING,
5899 .protocol_id = SMB311_PROT_ID,
5900 .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,
5901 .large_lock_type = 0,
5902 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5903 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5904 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5905 .header_size = sizeof(struct smb2_sync_hdr),
5906 .header_preamble_size = 0,
5907 .max_header_size = MAX_SMB2_HDR_SIZE,
5908 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5909 .lock_cmd = SMB2_LOCK,
5911 .cap_nt_find = SMB2_NT_FIND,
5912 .cap_large_files = SMB2_LARGE_FILES,
5913 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5914 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5915 .create_lease_size = sizeof(struct create_lease_v2),