1 // SPDX-License-Identifier: GPL-2.0
3 * SMB2 version specific operations
8 #include <linux/pagemap.h>
10 #include <linux/falloc.h>
11 #include <linux/scatterlist.h>
12 #include <linux/uuid.h>
13 #include <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 spin_lock(&GlobalMid_Lock);
392 cifs_ses_server(ses)->CurrentMid = 0;
393 spin_unlock(&GlobalMid_Lock);
394 rc = SMB2_negotiate(xid, ses);
395 /* BB we probably don't need to retry with modern servers */
402 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
404 struct TCP_Server_Info *server = tcon->ses->server;
407 /* start with specified wsize, or default */
408 wsize = ctx->wsize ? ctx->wsize : CIFS_DEFAULT_IOSIZE;
409 wsize = min_t(unsigned int, wsize, server->max_write);
410 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
411 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
417 smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
419 struct TCP_Server_Info *server = tcon->ses->server;
422 /* start with specified wsize, or default */
423 wsize = ctx->wsize ? ctx->wsize : SMB3_DEFAULT_IOSIZE;
424 wsize = min_t(unsigned int, wsize, server->max_write);
425 #ifdef CONFIG_CIFS_SMB_DIRECT
429 * Account for SMB2 data transfer packet header and
430 * possible encryption header
432 wsize = min_t(unsigned int,
434 server->smbd_conn->max_fragmented_send_size -
435 SMB2_READWRITE_PDU_HEADER_SIZE -
436 sizeof(struct smb2_transform_hdr));
438 wsize = min_t(unsigned int,
439 wsize, server->smbd_conn->max_readwrite_size);
442 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
443 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
449 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
451 struct TCP_Server_Info *server = tcon->ses->server;
454 /* start with specified rsize, or default */
455 rsize = ctx->rsize ? ctx->rsize : CIFS_DEFAULT_IOSIZE;
456 rsize = min_t(unsigned int, rsize, server->max_read);
458 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
459 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
465 smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
467 struct TCP_Server_Info *server = tcon->ses->server;
470 /* start with specified rsize, or default */
471 rsize = ctx->rsize ? ctx->rsize : SMB3_DEFAULT_IOSIZE;
472 rsize = min_t(unsigned int, rsize, server->max_read);
473 #ifdef CONFIG_CIFS_SMB_DIRECT
477 * Account for SMB2 data transfer packet header and
478 * possible encryption header
480 rsize = min_t(unsigned int,
482 server->smbd_conn->max_fragmented_recv_size -
483 SMB2_READWRITE_PDU_HEADER_SIZE -
484 sizeof(struct smb2_transform_hdr));
486 rsize = min_t(unsigned int,
487 rsize, server->smbd_conn->max_readwrite_size);
491 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
492 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
498 parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
500 struct cifs_server_iface **iface_list,
503 struct network_interface_info_ioctl_rsp *p;
504 struct sockaddr_in *addr4;
505 struct sockaddr_in6 *addr6;
506 struct iface_info_ipv4 *p4;
507 struct iface_info_ipv6 *p6;
508 struct cifs_server_iface *info;
518 * Fist pass: count and sanity check
521 bytes_left = buf_len;
523 while (bytes_left >= sizeof(*p)) {
525 next = le32_to_cpu(p->Next);
527 bytes_left -= sizeof(*p);
530 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
535 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
540 /* Azure rounds the buffer size up 8, to a 16 byte boundary */
541 if ((bytes_left > 8) || p->Next)
542 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
546 * Second pass: extract info to internal structure
549 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
556 bytes_left = buf_len;
558 while (bytes_left >= sizeof(*p)) {
559 info->speed = le64_to_cpu(p->LinkSpeed);
560 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE) ? 1 : 0;
561 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE) ? 1 : 0;
563 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
564 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
565 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
566 le32_to_cpu(p->Capability));
570 * The kernel and wire socket structures have the same
571 * layout and use network byte order but make the
572 * conversion explicit in case either one changes.
575 addr4 = (struct sockaddr_in *)&info->sockaddr;
576 p4 = (struct iface_info_ipv4 *)p->Buffer;
577 addr4->sin_family = AF_INET;
578 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
580 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
581 addr4->sin_port = cpu_to_be16(CIFS_PORT);
583 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
587 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
588 p6 = (struct iface_info_ipv6 *)p->Buffer;
589 addr6->sin6_family = AF_INET6;
590 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
592 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
593 addr6->sin6_flowinfo = 0;
594 addr6->sin6_scope_id = 0;
595 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
597 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
602 "%s: skipping unsupported socket family\n",
610 next = le32_to_cpu(p->Next);
613 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
631 static int compare_iface(const void *ia, const void *ib)
633 const struct cifs_server_iface *a = (struct cifs_server_iface *)ia;
634 const struct cifs_server_iface *b = (struct cifs_server_iface *)ib;
636 return a->speed == b->speed ? 0 : (a->speed > b->speed ? -1 : 1);
640 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
643 unsigned int ret_data_len = 0;
644 struct network_interface_info_ioctl_rsp *out_buf = NULL;
645 struct cifs_server_iface *iface_list;
647 struct cifs_ses *ses = tcon->ses;
649 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
650 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
651 NULL /* no data input */, 0 /* no data input */,
652 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
653 if (rc == -EOPNOTSUPP) {
655 "server does not support query network interfaces\n");
657 } else if (rc != 0) {
658 cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
662 rc = parse_server_interfaces(out_buf, ret_data_len,
663 &iface_list, &iface_count);
667 /* sort interfaces from fastest to slowest */
668 sort(iface_list, iface_count, sizeof(*iface_list), compare_iface, NULL);
670 spin_lock(&ses->iface_lock);
671 kfree(ses->iface_list);
672 ses->iface_list = iface_list;
673 ses->iface_count = iface_count;
674 ses->iface_last_update = jiffies;
675 spin_unlock(&ses->iface_lock);
683 smb2_close_cached_fid(struct kref *ref)
685 struct cached_fid *cfid = container_of(ref, struct cached_fid,
688 if (cfid->is_valid) {
689 cifs_dbg(FYI, "clear cached root file handle\n");
690 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
691 cfid->fid->volatile_fid);
695 * We only check validity above to send SMB2_close,
696 * but we still need to invalidate these entries
697 * when this function is called
699 cfid->is_valid = false;
700 cfid->file_all_info_is_valid = false;
701 cfid->has_lease = false;
708 void close_cached_dir(struct cached_fid *cfid)
710 mutex_lock(&cfid->fid_mutex);
711 kref_put(&cfid->refcount, smb2_close_cached_fid);
712 mutex_unlock(&cfid->fid_mutex);
715 void close_cached_dir_lease_locked(struct cached_fid *cfid)
717 if (cfid->has_lease) {
718 cfid->has_lease = false;
719 kref_put(&cfid->refcount, smb2_close_cached_fid);
723 void close_cached_dir_lease(struct cached_fid *cfid)
725 mutex_lock(&cfid->fid_mutex);
726 close_cached_dir_lease_locked(cfid);
727 mutex_unlock(&cfid->fid_mutex);
731 smb2_cached_lease_break(struct work_struct *work)
733 struct cached_fid *cfid = container_of(work,
734 struct cached_fid, lease_break);
736 close_cached_dir_lease(cfid);
740 * Open the and cache a directory handle.
741 * Only supported for the root handle.
743 int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
745 struct cifs_sb_info *cifs_sb,
746 struct cached_fid **cfid)
748 struct cifs_ses *ses = tcon->ses;
749 struct TCP_Server_Info *server = ses->server;
750 struct cifs_open_parms oparms;
751 struct smb2_create_rsp *o_rsp = NULL;
752 struct smb2_query_info_rsp *qi_rsp = NULL;
754 struct smb_rqst rqst[2];
755 struct kvec rsp_iov[2];
756 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
757 struct kvec qi_iov[1];
759 __le16 utf16_path = 0; /* Null - since an open of top of share */
760 u8 oplock = SMB2_OPLOCK_LEVEL_II;
761 struct cifs_fid *pfid;
762 struct dentry *dentry;
764 if (tcon->nohandlecache)
767 if (cifs_sb->root == NULL)
773 dentry = cifs_sb->root;
775 mutex_lock(&tcon->crfid.fid_mutex);
776 if (tcon->crfid.is_valid) {
777 cifs_dbg(FYI, "found a cached root file handle\n");
778 *cfid = &tcon->crfid;
779 kref_get(&tcon->crfid.refcount);
780 mutex_unlock(&tcon->crfid.fid_mutex);
785 * We do not hold the lock for the open because in case
786 * SMB2_open needs to reconnect, it will end up calling
787 * cifs_mark_open_files_invalid() which takes the lock again
788 * thus causing a deadlock
791 mutex_unlock(&tcon->crfid.fid_mutex);
793 if (smb3_encryption_required(tcon))
794 flags |= CIFS_TRANSFORM_REQ;
796 if (!server->ops->new_lease_key)
799 pfid = tcon->crfid.fid;
800 server->ops->new_lease_key(pfid);
802 memset(rqst, 0, sizeof(rqst));
803 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
804 memset(rsp_iov, 0, sizeof(rsp_iov));
807 memset(&open_iov, 0, sizeof(open_iov));
808 rqst[0].rq_iov = open_iov;
809 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
812 oparms.create_options = cifs_create_options(cifs_sb, 0);
813 oparms.desired_access = FILE_READ_ATTRIBUTES;
814 oparms.disposition = FILE_OPEN;
816 oparms.reconnect = false;
818 rc = SMB2_open_init(tcon, server,
819 &rqst[0], &oplock, &oparms, &utf16_path);
822 smb2_set_next_command(tcon, &rqst[0]);
824 memset(&qi_iov, 0, sizeof(qi_iov));
825 rqst[1].rq_iov = qi_iov;
828 rc = SMB2_query_info_init(tcon, server,
829 &rqst[1], COMPOUND_FID,
830 COMPOUND_FID, FILE_ALL_INFORMATION,
832 sizeof(struct smb2_file_all_info) +
833 PATH_MAX * 2, 0, NULL);
837 smb2_set_related(&rqst[1]);
839 rc = compound_send_recv(xid, ses, server,
841 resp_buftype, rsp_iov);
842 mutex_lock(&tcon->crfid.fid_mutex);
845 * Now we need to check again as the cached root might have
846 * been successfully re-opened from a concurrent process
849 if (tcon->crfid.is_valid) {
850 /* work was already done */
852 /* stash fids for close() later */
853 struct cifs_fid fid = {
854 .persistent_fid = pfid->persistent_fid,
855 .volatile_fid = pfid->volatile_fid,
859 * caller expects this func to set the fid in crfid to valid
860 * cached root, so increment the refcount.
862 kref_get(&tcon->crfid.refcount);
864 mutex_unlock(&tcon->crfid.fid_mutex);
867 /* close extra handle outside of crit sec */
868 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
874 /* Cached root is still invalid, continue normaly */
877 if (rc == -EREMCHG) {
878 tcon->need_reconnect = true;
879 pr_warn_once("server share %s deleted\n",
885 atomic_inc(&tcon->num_remote_opens);
887 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
888 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
889 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
890 #ifdef CONFIG_CIFS_DEBUG2
891 oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
892 #endif /* CIFS_DEBUG2 */
894 tcon->crfid.tcon = tcon;
895 tcon->crfid.is_valid = true;
896 tcon->crfid.dentry = dentry;
898 kref_init(&tcon->crfid.refcount);
900 /* BB TBD check to see if oplock level check can be removed below */
901 if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
903 * See commit 2f94a3125b87. Increment the refcount when we
904 * get a lease for root, release it if lease break occurs
906 kref_get(&tcon->crfid.refcount);
907 tcon->crfid.has_lease = true;
908 smb2_parse_contexts(server, o_rsp,
910 oparms.fid->lease_key, &oplock,
915 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
916 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
918 if (!smb2_validate_and_copy_iov(
919 le16_to_cpu(qi_rsp->OutputBufferOffset),
920 sizeof(struct smb2_file_all_info),
921 &rsp_iov[1], sizeof(struct smb2_file_all_info),
922 (char *)&tcon->crfid.file_all_info))
923 tcon->crfid.file_all_info_is_valid = true;
924 tcon->crfid.time = jiffies;
928 mutex_unlock(&tcon->crfid.fid_mutex);
930 SMB2_open_free(&rqst[0]);
931 SMB2_query_info_free(&rqst[1]);
932 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
933 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
935 *cfid = &tcon->crfid;
939 int open_cached_dir_by_dentry(struct cifs_tcon *tcon,
940 struct dentry *dentry,
941 struct cached_fid **cfid)
943 mutex_lock(&tcon->crfid.fid_mutex);
944 if (tcon->crfid.dentry == dentry) {
945 cifs_dbg(FYI, "found a cached root file handle by dentry\n");
946 *cfid = &tcon->crfid;
947 kref_get(&tcon->crfid.refcount);
948 mutex_unlock(&tcon->crfid.fid_mutex);
951 mutex_unlock(&tcon->crfid.fid_mutex);
956 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
957 struct cifs_sb_info *cifs_sb)
960 __le16 srch_path = 0; /* Null - open root of share */
961 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
962 struct cifs_open_parms oparms;
964 struct cached_fid *cfid = NULL;
967 oparms.desired_access = FILE_READ_ATTRIBUTES;
968 oparms.disposition = FILE_OPEN;
969 oparms.create_options = cifs_create_options(cifs_sb, 0);
971 oparms.reconnect = false;
973 rc = open_cached_dir(xid, tcon, "", cifs_sb, &cfid);
975 memcpy(&fid, cfid->fid, sizeof(struct cifs_fid));
977 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
982 SMB3_request_interfaces(xid, tcon);
984 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
985 FS_ATTRIBUTE_INFORMATION);
986 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
987 FS_DEVICE_INFORMATION);
988 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
989 FS_VOLUME_INFORMATION);
990 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
991 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
993 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
995 close_cached_dir(cfid);
999 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
1000 struct cifs_sb_info *cifs_sb)
1003 __le16 srch_path = 0; /* Null - open root of share */
1004 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1005 struct cifs_open_parms oparms;
1006 struct cifs_fid fid;
1009 oparms.desired_access = FILE_READ_ATTRIBUTES;
1010 oparms.disposition = FILE_OPEN;
1011 oparms.create_options = cifs_create_options(cifs_sb, 0);
1013 oparms.reconnect = false;
1015 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
1020 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1021 FS_ATTRIBUTE_INFORMATION);
1022 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1023 FS_DEVICE_INFORMATION);
1024 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1028 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
1029 struct cifs_sb_info *cifs_sb, const char *full_path)
1033 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1034 struct cifs_open_parms oparms;
1035 struct cifs_fid fid;
1037 if ((*full_path == 0) && tcon->crfid.is_valid)
1040 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1045 oparms.desired_access = FILE_READ_ATTRIBUTES;
1046 oparms.disposition = FILE_OPEN;
1047 oparms.create_options = cifs_create_options(cifs_sb, 0);
1049 oparms.reconnect = false;
1051 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
1058 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1064 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
1065 struct cifs_sb_info *cifs_sb, const char *full_path,
1066 u64 *uniqueid, FILE_ALL_INFO *data)
1068 *uniqueid = le64_to_cpu(data->IndexNumber);
1073 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
1074 struct cifs_fid *fid, FILE_ALL_INFO *data)
1077 struct smb2_file_all_info *smb2_data;
1079 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
1081 if (smb2_data == NULL)
1084 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
1087 move_smb2_info_to_cifs(data, smb2_data);
1092 #ifdef CONFIG_CIFS_XATTR
1094 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
1095 struct smb2_file_full_ea_info *src, size_t src_size,
1096 const unsigned char *ea_name)
1099 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
1101 size_t buf_size = dst_size;
1102 size_t name_len, value_len, user_name_len;
1104 while (src_size > 0) {
1105 name = &src->ea_data[0];
1106 name_len = (size_t)src->ea_name_length;
1107 value = &src->ea_data[src->ea_name_length + 1];
1108 value_len = (size_t)le16_to_cpu(src->ea_value_length);
1113 if (src_size < 8 + name_len + 1 + value_len) {
1114 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
1120 if (ea_name_len == name_len &&
1121 memcmp(ea_name, name, name_len) == 0) {
1125 if (dst_size < value_len) {
1129 memcpy(dst, value, value_len);
1133 /* 'user.' plus a terminating null */
1134 user_name_len = 5 + 1 + name_len;
1136 if (buf_size == 0) {
1137 /* skip copy - calc size only */
1138 rc += user_name_len;
1139 } else if (dst_size >= user_name_len) {
1140 dst_size -= user_name_len;
1141 memcpy(dst, "user.", 5);
1143 memcpy(dst, src->ea_data, name_len);
1147 rc += user_name_len;
1149 /* stop before overrun buffer */
1155 if (!src->next_entry_offset)
1158 if (src_size < le32_to_cpu(src->next_entry_offset)) {
1159 /* stop before overrun buffer */
1163 src_size -= le32_to_cpu(src->next_entry_offset);
1164 src = (void *)((char *)src +
1165 le32_to_cpu(src->next_entry_offset));
1168 /* didn't find the named attribute */
1177 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1178 const unsigned char *path, const unsigned char *ea_name,
1179 char *ea_data, size_t buf_size,
1180 struct cifs_sb_info *cifs_sb)
1184 struct kvec rsp_iov = {NULL, 0};
1185 int buftype = CIFS_NO_BUFFER;
1186 struct smb2_query_info_rsp *rsp;
1187 struct smb2_file_full_ea_info *info = NULL;
1189 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1193 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1195 FILE_FULL_EA_INFORMATION,
1198 MAX_SMB2_CREATE_RESPONSE_SIZE -
1199 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1200 &rsp_iov, &buftype, cifs_sb);
1203 * If ea_name is NULL (listxattr) and there are no EAs,
1204 * return 0 as it's not an error. Otherwise, the specified
1205 * ea_name was not found.
1207 if (!ea_name && rc == -ENODATA)
1212 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1213 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1214 le32_to_cpu(rsp->OutputBufferLength),
1216 sizeof(struct smb2_file_full_ea_info));
1220 info = (struct smb2_file_full_ea_info *)(
1221 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1222 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1223 le32_to_cpu(rsp->OutputBufferLength), ea_name);
1227 free_rsp_buf(buftype, rsp_iov.iov_base);
1233 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1234 const char *path, const char *ea_name, const void *ea_value,
1235 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1236 struct cifs_sb_info *cifs_sb)
1238 struct cifs_ses *ses = tcon->ses;
1239 struct TCP_Server_Info *server = cifs_pick_channel(ses);
1240 __le16 *utf16_path = NULL;
1241 int ea_name_len = strlen(ea_name);
1242 int flags = CIFS_CP_CREATE_CLOSE_OP;
1244 struct smb_rqst rqst[3];
1245 int resp_buftype[3];
1246 struct kvec rsp_iov[3];
1247 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1248 struct cifs_open_parms oparms;
1249 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1250 struct cifs_fid fid;
1251 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1252 unsigned int size[1];
1254 struct smb2_file_full_ea_info *ea = NULL;
1255 struct kvec close_iov[1];
1256 struct smb2_query_info_rsp *rsp;
1257 int rc, used_len = 0;
1259 if (smb3_encryption_required(tcon))
1260 flags |= CIFS_TRANSFORM_REQ;
1262 if (ea_name_len > 255)
1265 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1269 memset(rqst, 0, sizeof(rqst));
1270 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1271 memset(rsp_iov, 0, sizeof(rsp_iov));
1273 if (ses->server->ops->query_all_EAs) {
1275 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1281 /* If we are adding a attribute we should first check
1282 * if there will be enough space available to store
1283 * the new EA. If not we should not add it since we
1284 * would not be able to even read the EAs back.
1286 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1288 FILE_FULL_EA_INFORMATION,
1291 MAX_SMB2_CREATE_RESPONSE_SIZE -
1292 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1293 &rsp_iov[1], &resp_buftype[1], cifs_sb);
1295 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1296 used_len = le32_to_cpu(rsp->OutputBufferLength);
1298 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1299 resp_buftype[1] = CIFS_NO_BUFFER;
1300 memset(&rsp_iov[1], 0, sizeof(rsp_iov[1]));
1303 /* Use a fudge factor of 256 bytes in case we collide
1304 * with a different set_EAs command.
1306 if(CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1307 MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
1308 used_len + ea_name_len + ea_value_len + 1) {
1316 memset(&open_iov, 0, sizeof(open_iov));
1317 rqst[0].rq_iov = open_iov;
1318 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1320 memset(&oparms, 0, sizeof(oparms));
1322 oparms.desired_access = FILE_WRITE_EA;
1323 oparms.disposition = FILE_OPEN;
1324 oparms.create_options = cifs_create_options(cifs_sb, 0);
1326 oparms.reconnect = false;
1328 rc = SMB2_open_init(tcon, server,
1329 &rqst[0], &oplock, &oparms, utf16_path);
1332 smb2_set_next_command(tcon, &rqst[0]);
1336 memset(&si_iov, 0, sizeof(si_iov));
1337 rqst[1].rq_iov = si_iov;
1338 rqst[1].rq_nvec = 1;
1340 len = sizeof(*ea) + ea_name_len + ea_value_len + 1;
1341 ea = kzalloc(len, GFP_KERNEL);
1347 ea->ea_name_length = ea_name_len;
1348 ea->ea_value_length = cpu_to_le16(ea_value_len);
1349 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1350 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1355 rc = SMB2_set_info_init(tcon, server,
1356 &rqst[1], COMPOUND_FID,
1357 COMPOUND_FID, current->tgid,
1358 FILE_FULL_EA_INFORMATION,
1359 SMB2_O_INFO_FILE, 0, data, size);
1360 smb2_set_next_command(tcon, &rqst[1]);
1361 smb2_set_related(&rqst[1]);
1365 memset(&close_iov, 0, sizeof(close_iov));
1366 rqst[2].rq_iov = close_iov;
1367 rqst[2].rq_nvec = 1;
1368 rc = SMB2_close_init(tcon, server,
1369 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1370 smb2_set_related(&rqst[2]);
1372 rc = compound_send_recv(xid, ses, server,
1374 resp_buftype, rsp_iov);
1375 /* no need to bump num_remote_opens because handle immediately closed */
1380 SMB2_open_free(&rqst[0]);
1381 SMB2_set_info_free(&rqst[1]);
1382 SMB2_close_free(&rqst[2]);
1383 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1384 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1385 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1391 smb2_can_echo(struct TCP_Server_Info *server)
1393 return server->echoes;
1397 smb2_clear_stats(struct cifs_tcon *tcon)
1401 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1402 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1403 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1408 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1410 seq_puts(m, "\n\tShare Capabilities:");
1411 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1412 seq_puts(m, " DFS,");
1413 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1414 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1415 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1416 seq_puts(m, " SCALEOUT,");
1417 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1418 seq_puts(m, " CLUSTER,");
1419 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1420 seq_puts(m, " ASYMMETRIC,");
1421 if (tcon->capabilities == 0)
1422 seq_puts(m, " None");
1423 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1424 seq_puts(m, " Aligned,");
1425 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1426 seq_puts(m, " Partition Aligned,");
1427 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1428 seq_puts(m, " SSD,");
1429 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1430 seq_puts(m, " TRIM-support,");
1432 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1433 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1434 if (tcon->perf_sector_size)
1435 seq_printf(m, "\tOptimal sector size: 0x%x",
1436 tcon->perf_sector_size);
1437 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1441 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1443 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1444 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1447 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1448 * totals (requests sent) since those SMBs are per-session not per tcon
1450 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1451 (long long)(tcon->bytes_read),
1452 (long long)(tcon->bytes_written));
1453 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1454 atomic_read(&tcon->num_local_opens),
1455 atomic_read(&tcon->num_remote_opens));
1456 seq_printf(m, "\nTreeConnects: %d total %d failed",
1457 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1458 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1459 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1460 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1461 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1462 seq_printf(m, "\nCreates: %d total %d failed",
1463 atomic_read(&sent[SMB2_CREATE_HE]),
1464 atomic_read(&failed[SMB2_CREATE_HE]));
1465 seq_printf(m, "\nCloses: %d total %d failed",
1466 atomic_read(&sent[SMB2_CLOSE_HE]),
1467 atomic_read(&failed[SMB2_CLOSE_HE]));
1468 seq_printf(m, "\nFlushes: %d total %d failed",
1469 atomic_read(&sent[SMB2_FLUSH_HE]),
1470 atomic_read(&failed[SMB2_FLUSH_HE]));
1471 seq_printf(m, "\nReads: %d total %d failed",
1472 atomic_read(&sent[SMB2_READ_HE]),
1473 atomic_read(&failed[SMB2_READ_HE]));
1474 seq_printf(m, "\nWrites: %d total %d failed",
1475 atomic_read(&sent[SMB2_WRITE_HE]),
1476 atomic_read(&failed[SMB2_WRITE_HE]));
1477 seq_printf(m, "\nLocks: %d total %d failed",
1478 atomic_read(&sent[SMB2_LOCK_HE]),
1479 atomic_read(&failed[SMB2_LOCK_HE]));
1480 seq_printf(m, "\nIOCTLs: %d total %d failed",
1481 atomic_read(&sent[SMB2_IOCTL_HE]),
1482 atomic_read(&failed[SMB2_IOCTL_HE]));
1483 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1484 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1485 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1486 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1487 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1488 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1489 seq_printf(m, "\nQueryInfos: %d total %d failed",
1490 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1491 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1492 seq_printf(m, "\nSetInfos: %d total %d failed",
1493 atomic_read(&sent[SMB2_SET_INFO_HE]),
1494 atomic_read(&failed[SMB2_SET_INFO_HE]));
1495 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1496 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1497 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1501 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1503 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1504 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1506 cfile->fid.persistent_fid = fid->persistent_fid;
1507 cfile->fid.volatile_fid = fid->volatile_fid;
1508 cfile->fid.access = fid->access;
1509 #ifdef CONFIG_CIFS_DEBUG2
1510 cfile->fid.mid = fid->mid;
1511 #endif /* CIFS_DEBUG2 */
1512 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1514 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1515 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1519 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1520 struct cifs_fid *fid)
1522 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1526 smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
1527 struct cifsFileInfo *cfile)
1529 struct smb2_file_network_open_info file_inf;
1530 struct inode *inode;
1533 rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
1534 cfile->fid.volatile_fid, &file_inf);
1538 inode = d_inode(cfile->dentry);
1540 spin_lock(&inode->i_lock);
1541 CIFS_I(inode)->time = jiffies;
1543 /* Creation time should not need to be updated on close */
1544 if (file_inf.LastWriteTime)
1545 inode->i_mtime = cifs_NTtimeToUnix(file_inf.LastWriteTime);
1546 if (file_inf.ChangeTime)
1547 inode->i_ctime = cifs_NTtimeToUnix(file_inf.ChangeTime);
1548 if (file_inf.LastAccessTime)
1549 inode->i_atime = cifs_NTtimeToUnix(file_inf.LastAccessTime);
1552 * i_blocks is not related to (i_size / i_blksize),
1553 * but instead 512 byte (2**9) size is required for
1554 * calculating num blocks.
1556 if (le64_to_cpu(file_inf.AllocationSize) > 4096)
1558 (512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
1560 /* End of file and Attributes should not have to be updated on close */
1561 spin_unlock(&inode->i_lock);
1565 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1566 u64 persistent_fid, u64 volatile_fid,
1567 struct copychunk_ioctl *pcchunk)
1570 unsigned int ret_data_len;
1571 struct resume_key_req *res_key;
1573 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1574 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1575 NULL, 0 /* no input */, CIFSMaxBufSize,
1576 (char **)&res_key, &ret_data_len);
1578 if (rc == -EOPNOTSUPP) {
1579 pr_warn_once("Server share %s does not support copy range\n", tcon->treeName);
1580 goto req_res_key_exit;
1582 cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1583 goto req_res_key_exit;
1585 if (ret_data_len < sizeof(struct resume_key_req)) {
1586 cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
1588 goto req_res_key_exit;
1590 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1598 struct smb_rqst rqst[3];
1599 struct kvec rsp_iov[3];
1600 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1601 struct kvec qi_iov[1];
1602 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1603 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1604 struct kvec close_iov[1];
1608 smb2_ioctl_query_info(const unsigned int xid,
1609 struct cifs_tcon *tcon,
1610 struct cifs_sb_info *cifs_sb,
1611 __le16 *path, int is_dir,
1614 struct iqi_vars *vars;
1615 struct smb_rqst *rqst;
1616 struct kvec *rsp_iov;
1617 struct cifs_ses *ses = tcon->ses;
1618 struct TCP_Server_Info *server = cifs_pick_channel(ses);
1619 char __user *arg = (char __user *)p;
1620 struct smb_query_info qi;
1621 struct smb_query_info __user *pqi;
1623 int flags = CIFS_CP_CREATE_CLOSE_OP;
1624 struct smb2_query_info_rsp *qi_rsp = NULL;
1625 struct smb2_ioctl_rsp *io_rsp = NULL;
1626 void *buffer = NULL;
1627 int resp_buftype[3];
1628 struct cifs_open_parms oparms;
1629 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1630 struct cifs_fid fid;
1631 unsigned int size[2];
1633 int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
1635 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
1638 rqst = &vars->rqst[0];
1639 rsp_iov = &vars->rsp_iov[0];
1641 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1643 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1646 if (qi.output_buffer_length > 1024) {
1651 if (!ses || !server) {
1656 if (smb3_encryption_required(tcon))
1657 flags |= CIFS_TRANSFORM_REQ;
1659 buffer = memdup_user(arg + sizeof(struct smb_query_info),
1660 qi.output_buffer_length);
1661 if (IS_ERR(buffer)) {
1663 return PTR_ERR(buffer);
1667 rqst[0].rq_iov = &vars->open_iov[0];
1668 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1670 memset(&oparms, 0, sizeof(oparms));
1672 oparms.disposition = FILE_OPEN;
1673 oparms.create_options = cifs_create_options(cifs_sb, create_options);
1675 oparms.reconnect = false;
1677 if (qi.flags & PASSTHRU_FSCTL) {
1678 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1679 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1680 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1682 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1683 oparms.desired_access = GENERIC_ALL;
1685 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1686 oparms.desired_access = GENERIC_READ;
1688 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1689 oparms.desired_access = GENERIC_WRITE;
1692 } else if (qi.flags & PASSTHRU_SET_INFO) {
1693 oparms.desired_access = GENERIC_WRITE;
1695 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1698 rc = SMB2_open_init(tcon, server,
1699 &rqst[0], &oplock, &oparms, path);
1702 smb2_set_next_command(tcon, &rqst[0]);
1705 if (qi.flags & PASSTHRU_FSCTL) {
1706 /* Can eventually relax perm check since server enforces too */
1707 if (!capable(CAP_SYS_ADMIN))
1710 rqst[1].rq_iov = &vars->io_iov[0];
1711 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1713 rc = SMB2_ioctl_init(tcon, server,
1715 COMPOUND_FID, COMPOUND_FID,
1716 qi.info_type, true, buffer,
1717 qi.output_buffer_length,
1719 MAX_SMB2_CREATE_RESPONSE_SIZE -
1720 MAX_SMB2_CLOSE_RESPONSE_SIZE);
1722 } else if (qi.flags == PASSTHRU_SET_INFO) {
1723 /* Can eventually relax perm check since server enforces too */
1724 if (!capable(CAP_SYS_ADMIN))
1727 rqst[1].rq_iov = &vars->si_iov[0];
1728 rqst[1].rq_nvec = 1;
1733 rc = SMB2_set_info_init(tcon, server,
1735 COMPOUND_FID, COMPOUND_FID,
1737 FILE_END_OF_FILE_INFORMATION,
1738 SMB2_O_INFO_FILE, 0, data, size);
1740 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1741 rqst[1].rq_iov = &vars->qi_iov[0];
1742 rqst[1].rq_nvec = 1;
1744 rc = SMB2_query_info_init(tcon, server,
1745 &rqst[1], COMPOUND_FID,
1746 COMPOUND_FID, qi.file_info_class,
1747 qi.info_type, qi.additional_information,
1748 qi.input_buffer_length,
1749 qi.output_buffer_length, buffer);
1750 } else { /* unknown flags */
1751 cifs_tcon_dbg(VFS, "Invalid passthru query flags: 0x%x\n",
1758 smb2_set_next_command(tcon, &rqst[1]);
1759 smb2_set_related(&rqst[1]);
1762 rqst[2].rq_iov = &vars->close_iov[0];
1763 rqst[2].rq_nvec = 1;
1765 rc = SMB2_close_init(tcon, server,
1766 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1769 smb2_set_related(&rqst[2]);
1771 rc = compound_send_recv(xid, ses, server,
1773 resp_buftype, rsp_iov);
1777 /* No need to bump num_remote_opens since handle immediately closed */
1778 if (qi.flags & PASSTHRU_FSCTL) {
1779 pqi = (struct smb_query_info __user *)arg;
1780 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1781 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1782 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1783 if (qi.input_buffer_length > 0 &&
1784 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1785 > rsp_iov[1].iov_len)
1788 if (copy_to_user(&pqi->input_buffer_length,
1789 &qi.input_buffer_length,
1790 sizeof(qi.input_buffer_length)))
1793 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1794 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1795 qi.input_buffer_length))
1798 pqi = (struct smb_query_info __user *)arg;
1799 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1800 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1801 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1802 if (copy_to_user(&pqi->input_buffer_length,
1803 &qi.input_buffer_length,
1804 sizeof(qi.input_buffer_length)))
1807 if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1808 qi.input_buffer_length))
1813 cifs_small_buf_release(rqst[0].rq_iov[0].iov_base);
1814 cifs_small_buf_release(rqst[1].rq_iov[0].iov_base);
1815 cifs_small_buf_release(rqst[2].rq_iov[0].iov_base);
1816 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1817 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1818 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1829 smb2_copychunk_range(const unsigned int xid,
1830 struct cifsFileInfo *srcfile,
1831 struct cifsFileInfo *trgtfile, u64 src_off,
1832 u64 len, u64 dest_off)
1835 unsigned int ret_data_len;
1836 struct copychunk_ioctl *pcchunk;
1837 struct copychunk_ioctl_rsp *retbuf = NULL;
1838 struct cifs_tcon *tcon;
1839 int chunks_copied = 0;
1840 bool chunk_sizes_updated = false;
1841 ssize_t bytes_written, total_bytes_written = 0;
1843 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1845 if (pcchunk == NULL)
1848 cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
1849 /* Request a key from the server to identify the source of the copy */
1850 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1851 srcfile->fid.persistent_fid,
1852 srcfile->fid.volatile_fid, pcchunk);
1854 /* Note: request_res_key sets res_key null only if rc !=0 */
1858 /* For now array only one chunk long, will make more flexible later */
1859 pcchunk->ChunkCount = cpu_to_le32(1);
1860 pcchunk->Reserved = 0;
1861 pcchunk->Reserved2 = 0;
1863 tcon = tlink_tcon(trgtfile->tlink);
1866 pcchunk->SourceOffset = cpu_to_le64(src_off);
1867 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1869 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1871 /* Request server copy to target from src identified by key */
1874 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1875 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1876 true /* is_fsctl */, (char *)pcchunk,
1877 sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1878 (char **)&retbuf, &ret_data_len);
1881 sizeof(struct copychunk_ioctl_rsp)) {
1882 cifs_tcon_dbg(VFS, "Invalid cchunk response size\n");
1886 if (retbuf->TotalBytesWritten == 0) {
1887 cifs_dbg(FYI, "no bytes copied\n");
1892 * Check if server claimed to write more than we asked
1894 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1895 le32_to_cpu(pcchunk->Length)) {
1896 cifs_tcon_dbg(VFS, "Invalid copy chunk response\n");
1900 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1901 cifs_tcon_dbg(VFS, "Invalid num chunks written\n");
1907 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1908 src_off += bytes_written;
1909 dest_off += bytes_written;
1910 len -= bytes_written;
1911 total_bytes_written += bytes_written;
1913 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1914 le32_to_cpu(retbuf->ChunksWritten),
1915 le32_to_cpu(retbuf->ChunkBytesWritten),
1917 } else if (rc == -EINVAL) {
1918 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1921 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1922 le32_to_cpu(retbuf->ChunksWritten),
1923 le32_to_cpu(retbuf->ChunkBytesWritten),
1924 le32_to_cpu(retbuf->TotalBytesWritten));
1927 * Check if this is the first request using these sizes,
1928 * (ie check if copy succeed once with original sizes
1929 * and check if the server gave us different sizes after
1930 * we already updated max sizes on previous request).
1931 * if not then why is the server returning an error now
1933 if ((chunks_copied != 0) || chunk_sizes_updated)
1936 /* Check that server is not asking us to grow size */
1937 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1938 tcon->max_bytes_chunk)
1939 tcon->max_bytes_chunk =
1940 le32_to_cpu(retbuf->ChunkBytesWritten);
1942 goto cchunk_out; /* server gave us bogus size */
1944 /* No need to change MaxChunks since already set to 1 */
1945 chunk_sizes_updated = true;
1956 return total_bytes_written;
1960 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1961 struct cifs_fid *fid)
1963 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1967 smb2_read_data_offset(char *buf)
1969 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1971 return rsp->DataOffset;
1975 smb2_read_data_length(char *buf, bool in_remaining)
1977 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1980 return le32_to_cpu(rsp->DataRemaining);
1982 return le32_to_cpu(rsp->DataLength);
1987 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1988 struct cifs_io_parms *parms, unsigned int *bytes_read,
1989 char **buf, int *buf_type)
1991 parms->persistent_fid = pfid->persistent_fid;
1992 parms->volatile_fid = pfid->volatile_fid;
1993 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1997 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1998 struct cifs_io_parms *parms, unsigned int *written,
1999 struct kvec *iov, unsigned long nr_segs)
2002 parms->persistent_fid = pfid->persistent_fid;
2003 parms->volatile_fid = pfid->volatile_fid;
2004 return SMB2_write(xid, parms, written, iov, nr_segs);
2007 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
2008 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
2009 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
2011 struct cifsInodeInfo *cifsi;
2014 cifsi = CIFS_I(inode);
2016 /* if file already sparse don't bother setting sparse again */
2017 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
2018 return true; /* already sparse */
2020 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
2021 return true; /* already not sparse */
2024 * Can't check for sparse support on share the usual way via the
2025 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
2026 * since Samba server doesn't set the flag on the share, yet
2027 * supports the set sparse FSCTL and returns sparse correctly
2028 * in the file attributes. If we fail setting sparse though we
2029 * mark that server does not support sparse files for this share
2030 * to avoid repeatedly sending the unsupported fsctl to server
2031 * if the file is repeatedly extended.
2033 if (tcon->broken_sparse_sup)
2036 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2037 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
2039 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
2041 tcon->broken_sparse_sup = true;
2042 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
2047 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
2049 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
2055 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
2056 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
2058 __le64 eof = cpu_to_le64(size);
2059 struct inode *inode;
2062 * If extending file more than one page make sparse. Many Linux fs
2063 * make files sparse by default when extending via ftruncate
2065 inode = d_inode(cfile->dentry);
2067 if (!set_alloc && (size > inode->i_size + 8192)) {
2068 __u8 set_sparse = 1;
2070 /* whether set sparse succeeds or not, extend the file */
2071 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
2074 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
2075 cfile->fid.volatile_fid, cfile->pid, &eof);
2079 smb2_duplicate_extents(const unsigned int xid,
2080 struct cifsFileInfo *srcfile,
2081 struct cifsFileInfo *trgtfile, u64 src_off,
2082 u64 len, u64 dest_off)
2085 unsigned int ret_data_len;
2086 struct inode *inode;
2087 struct duplicate_extents_to_file dup_ext_buf;
2088 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
2090 /* server fileays advertise duplicate extent support with this flag */
2091 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
2092 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
2095 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
2096 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
2097 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
2098 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
2099 dup_ext_buf.ByteCount = cpu_to_le64(len);
2100 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
2101 src_off, dest_off, len);
2103 inode = d_inode(trgtfile->dentry);
2104 if (inode->i_size < dest_off + len) {
2105 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
2107 goto duplicate_extents_out;
2110 * Although also could set plausible allocation size (i_blocks)
2111 * here in addition to setting the file size, in reflink
2112 * it is likely that the target file is sparse. Its allocation
2113 * size will be queried on next revalidate, but it is important
2114 * to make sure that file's cached size is updated immediately
2116 cifs_setsize(inode, dest_off + len);
2118 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
2119 trgtfile->fid.volatile_fid,
2120 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
2121 true /* is_fsctl */,
2122 (char *)&dup_ext_buf,
2123 sizeof(struct duplicate_extents_to_file),
2124 CIFSMaxBufSize, NULL,
2127 if (ret_data_len > 0)
2128 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
2130 duplicate_extents_out:
2135 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2136 struct cifsFileInfo *cfile)
2138 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
2139 cfile->fid.volatile_fid);
2143 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
2144 struct cifsFileInfo *cfile)
2146 struct fsctl_set_integrity_information_req integr_info;
2147 unsigned int ret_data_len;
2149 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
2150 integr_info.Flags = 0;
2151 integr_info.Reserved = 0;
2153 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2154 cfile->fid.volatile_fid,
2155 FSCTL_SET_INTEGRITY_INFORMATION,
2156 true /* is_fsctl */,
2157 (char *)&integr_info,
2158 sizeof(struct fsctl_set_integrity_information_req),
2159 CIFSMaxBufSize, NULL,
2164 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
2165 #define GMT_TOKEN_SIZE 50
2167 #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
2170 * Input buffer contains (empty) struct smb_snapshot array with size filled in
2171 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
2174 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
2175 struct cifsFileInfo *cfile, void __user *ioc_buf)
2177 char *retbuf = NULL;
2178 unsigned int ret_data_len = 0;
2180 u32 max_response_size;
2181 struct smb_snapshot_array snapshot_in;
2184 * On the first query to enumerate the list of snapshots available
2185 * for this volume the buffer begins with 0 (number of snapshots
2186 * which can be returned is zero since at that point we do not know
2187 * how big the buffer needs to be). On the second query,
2188 * it (ret_data_len) is set to number of snapshots so we can
2189 * know to set the maximum response size larger (see below).
2191 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
2195 * Note that for snapshot queries that servers like Azure expect that
2196 * the first query be minimal size (and just used to get the number/size
2197 * of previous versions) so response size must be specified as EXACTLY
2198 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2201 if (ret_data_len == 0)
2202 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
2204 max_response_size = CIFSMaxBufSize;
2206 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2207 cfile->fid.volatile_fid,
2208 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
2209 true /* is_fsctl */,
2210 NULL, 0 /* no input data */, max_response_size,
2213 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2218 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2220 if (copy_from_user(&snapshot_in, ioc_buf,
2221 sizeof(struct smb_snapshot_array))) {
2228 * Check for min size, ie not large enough to fit even one GMT
2229 * token (snapshot). On the first ioctl some users may pass in
2230 * smaller size (or zero) to simply get the size of the array
2231 * so the user space caller can allocate sufficient memory
2232 * and retry the ioctl again with larger array size sufficient
2233 * to hold all of the snapshot GMT tokens on the second try.
2235 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2236 ret_data_len = sizeof(struct smb_snapshot_array);
2239 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2240 * the snapshot array (of 50 byte GMT tokens) each
2241 * representing an available previous version of the data
2243 if (ret_data_len > (snapshot_in.snapshot_array_size +
2244 sizeof(struct smb_snapshot_array)))
2245 ret_data_len = snapshot_in.snapshot_array_size +
2246 sizeof(struct smb_snapshot_array);
2248 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2259 smb3_notify(const unsigned int xid, struct file *pfile,
2260 void __user *ioc_buf)
2262 struct smb3_notify notify;
2263 struct dentry *dentry = pfile->f_path.dentry;
2264 struct inode *inode = file_inode(pfile);
2265 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2266 struct cifs_open_parms oparms;
2267 struct cifs_fid fid;
2268 struct cifs_tcon *tcon;
2269 const unsigned char *path;
2270 void *page = alloc_dentry_path();
2271 __le16 *utf16_path = NULL;
2272 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2275 path = build_path_from_dentry(dentry, page);
2281 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2282 if (utf16_path == NULL) {
2287 if (copy_from_user(¬ify, ioc_buf, sizeof(struct smb3_notify))) {
2292 tcon = cifs_sb_master_tcon(cifs_sb);
2294 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2295 oparms.disposition = FILE_OPEN;
2296 oparms.create_options = cifs_create_options(cifs_sb, 0);
2298 oparms.reconnect = false;
2300 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
2305 rc = SMB2_change_notify(xid, tcon, fid.persistent_fid, fid.volatile_fid,
2306 notify.watch_tree, notify.completion_filter);
2308 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2310 cifs_dbg(FYI, "change notify for path %s rc %d\n", path, rc);
2313 free_dentry_path(page);
2319 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2320 const char *path, struct cifs_sb_info *cifs_sb,
2321 struct cifs_fid *fid, __u16 search_flags,
2322 struct cifs_search_info *srch_inf)
2325 struct smb_rqst rqst[2];
2326 struct kvec rsp_iov[2];
2327 int resp_buftype[2];
2328 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2329 struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
2331 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2332 struct cifs_open_parms oparms;
2333 struct smb2_query_directory_rsp *qd_rsp = NULL;
2334 struct smb2_create_rsp *op_rsp = NULL;
2335 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
2336 int retry_count = 0;
2338 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2342 if (smb3_encryption_required(tcon))
2343 flags |= CIFS_TRANSFORM_REQ;
2345 memset(rqst, 0, sizeof(rqst));
2346 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2347 memset(rsp_iov, 0, sizeof(rsp_iov));
2350 memset(&open_iov, 0, sizeof(open_iov));
2351 rqst[0].rq_iov = open_iov;
2352 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2355 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2356 oparms.disposition = FILE_OPEN;
2357 oparms.create_options = cifs_create_options(cifs_sb, 0);
2359 oparms.reconnect = false;
2361 rc = SMB2_open_init(tcon, server,
2362 &rqst[0], &oplock, &oparms, utf16_path);
2365 smb2_set_next_command(tcon, &rqst[0]);
2367 /* Query directory */
2368 srch_inf->entries_in_buffer = 0;
2369 srch_inf->index_of_last_entry = 2;
2371 memset(&qd_iov, 0, sizeof(qd_iov));
2372 rqst[1].rq_iov = qd_iov;
2373 rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
2375 rc = SMB2_query_directory_init(xid, tcon, server,
2377 COMPOUND_FID, COMPOUND_FID,
2378 0, srch_inf->info_level);
2382 smb2_set_related(&rqst[1]);
2385 rc = compound_send_recv(xid, tcon->ses, server,
2387 resp_buftype, rsp_iov);
2389 if (rc == -EAGAIN && retry_count++ < 10)
2392 /* If the open failed there is nothing to do */
2393 op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
2394 if (op_rsp == NULL || op_rsp->sync_hdr.Status != STATUS_SUCCESS) {
2395 cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
2398 fid->persistent_fid = op_rsp->PersistentFileId;
2399 fid->volatile_fid = op_rsp->VolatileFileId;
2401 /* Anything else than ENODATA means a genuine error */
2402 if (rc && rc != -ENODATA) {
2403 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2404 cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
2405 trace_smb3_query_dir_err(xid, fid->persistent_fid,
2406 tcon->tid, tcon->ses->Suid, 0, 0, rc);
2410 atomic_inc(&tcon->num_remote_opens);
2412 qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
2413 if (qd_rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
2414 trace_smb3_query_dir_done(xid, fid->persistent_fid,
2415 tcon->tid, tcon->ses->Suid, 0, 0);
2416 srch_inf->endOfSearch = true;
2421 rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
2424 trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
2425 tcon->ses->Suid, 0, 0, rc);
2428 resp_buftype[1] = CIFS_NO_BUFFER;
2430 trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
2431 tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
2435 SMB2_open_free(&rqst[0]);
2436 SMB2_query_directory_free(&rqst[1]);
2437 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2438 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2443 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2444 struct cifs_fid *fid, __u16 search_flags,
2445 struct cifs_search_info *srch_inf)
2447 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2448 fid->volatile_fid, 0, srch_inf);
2452 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2453 struct cifs_fid *fid)
2455 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2459 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2460 * the number of credits and return true. Otherwise - return false.
2463 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
2465 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2466 int scredits, in_flight;
2468 if (shdr->Status != STATUS_PENDING)
2471 if (shdr->CreditRequest) {
2472 spin_lock(&server->req_lock);
2473 server->credits += le16_to_cpu(shdr->CreditRequest);
2474 scredits = server->credits;
2475 in_flight = server->in_flight;
2476 spin_unlock(&server->req_lock);
2477 wake_up(&server->request_q);
2479 trace_smb3_add_credits(server->CurrentMid,
2480 server->conn_id, server->hostname, scredits,
2481 le16_to_cpu(shdr->CreditRequest), in_flight);
2482 cifs_dbg(FYI, "%s: status pending add %u credits total=%d\n",
2483 __func__, le16_to_cpu(shdr->CreditRequest), scredits);
2490 smb2_is_session_expired(char *buf)
2492 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2494 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2495 shdr->Status != STATUS_USER_SESSION_DELETED)
2498 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2499 le16_to_cpu(shdr->Command),
2500 le64_to_cpu(shdr->MessageId));
2501 cifs_dbg(FYI, "Session expired or deleted\n");
2507 smb2_is_status_io_timeout(char *buf)
2509 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2511 if (shdr->Status == STATUS_IO_TIMEOUT)
2518 smb2_is_network_name_deleted(char *buf, struct TCP_Server_Info *server)
2520 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2521 struct list_head *tmp, *tmp1;
2522 struct cifs_ses *ses;
2523 struct cifs_tcon *tcon;
2525 if (shdr->Status != STATUS_NETWORK_NAME_DELETED)
2528 spin_lock(&cifs_tcp_ses_lock);
2529 list_for_each(tmp, &server->smb_ses_list) {
2530 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
2531 list_for_each(tmp1, &ses->tcon_list) {
2532 tcon = list_entry(tmp1, struct cifs_tcon, tcon_list);
2533 if (tcon->tid == shdr->TreeId) {
2534 tcon->need_reconnect = true;
2535 spin_unlock(&cifs_tcp_ses_lock);
2536 pr_warn_once("Server share %s deleted.\n",
2542 spin_unlock(&cifs_tcp_ses_lock);
2546 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2547 struct cifsInodeInfo *cinode)
2549 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2550 return SMB2_lease_break(0, tcon, cinode->lease_key,
2551 smb2_get_lease_state(cinode));
2553 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2555 CIFS_CACHE_READ(cinode) ? 1 : 0);
2559 smb2_set_related(struct smb_rqst *rqst)
2561 struct smb2_sync_hdr *shdr;
2563 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2565 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2568 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2571 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2574 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2576 struct smb2_sync_hdr *shdr;
2577 struct cifs_ses *ses = tcon->ses;
2578 struct TCP_Server_Info *server = ses->server;
2579 unsigned long len = smb_rqst_len(server, rqst);
2582 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2584 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2588 /* SMB headers in a compound are 8 byte aligned. */
2590 /* No padding needed */
2594 num_padding = 8 - (len & 7);
2595 if (!smb3_encryption_required(tcon)) {
2597 * If we do not have encryption then we can just add an extra
2598 * iov for the padding.
2600 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2601 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2606 * We can not add a small padding iov for the encryption case
2607 * because the encryption framework can not handle the padding
2609 * We have to flatten this into a single buffer and add
2610 * the padding to it.
2612 for (i = 1; i < rqst->rq_nvec; i++) {
2613 memcpy(rqst->rq_iov[0].iov_base +
2614 rqst->rq_iov[0].iov_len,
2615 rqst->rq_iov[i].iov_base,
2616 rqst->rq_iov[i].iov_len);
2617 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2619 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2621 rqst->rq_iov[0].iov_len += num_padding;
2627 shdr->NextCommand = cpu_to_le32(len);
2631 * Passes the query info response back to the caller on success.
2632 * Caller need to free this with free_rsp_buf().
2635 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2636 __le16 *utf16_path, u32 desired_access,
2637 u32 class, u32 type, u32 output_len,
2638 struct kvec *rsp, int *buftype,
2639 struct cifs_sb_info *cifs_sb)
2641 struct cifs_ses *ses = tcon->ses;
2642 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2643 int flags = CIFS_CP_CREATE_CLOSE_OP;
2644 struct smb_rqst rqst[3];
2645 int resp_buftype[3];
2646 struct kvec rsp_iov[3];
2647 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2648 struct kvec qi_iov[1];
2649 struct kvec close_iov[1];
2650 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2651 struct cifs_open_parms oparms;
2652 struct cifs_fid fid;
2655 if (smb3_encryption_required(tcon))
2656 flags |= CIFS_TRANSFORM_REQ;
2658 memset(rqst, 0, sizeof(rqst));
2659 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2660 memset(rsp_iov, 0, sizeof(rsp_iov));
2662 memset(&open_iov, 0, sizeof(open_iov));
2663 rqst[0].rq_iov = open_iov;
2664 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2667 oparms.desired_access = desired_access;
2668 oparms.disposition = FILE_OPEN;
2669 oparms.create_options = cifs_create_options(cifs_sb, 0);
2671 oparms.reconnect = false;
2673 rc = SMB2_open_init(tcon, server,
2674 &rqst[0], &oplock, &oparms, utf16_path);
2677 smb2_set_next_command(tcon, &rqst[0]);
2679 memset(&qi_iov, 0, sizeof(qi_iov));
2680 rqst[1].rq_iov = qi_iov;
2681 rqst[1].rq_nvec = 1;
2683 rc = SMB2_query_info_init(tcon, server,
2684 &rqst[1], COMPOUND_FID, COMPOUND_FID,
2690 smb2_set_next_command(tcon, &rqst[1]);
2691 smb2_set_related(&rqst[1]);
2693 memset(&close_iov, 0, sizeof(close_iov));
2694 rqst[2].rq_iov = close_iov;
2695 rqst[2].rq_nvec = 1;
2697 rc = SMB2_close_init(tcon, server,
2698 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2701 smb2_set_related(&rqst[2]);
2703 rc = compound_send_recv(xid, ses, server,
2705 resp_buftype, rsp_iov);
2707 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2708 if (rc == -EREMCHG) {
2709 tcon->need_reconnect = true;
2710 pr_warn_once("server share %s deleted\n",
2716 *buftype = resp_buftype[1];
2719 SMB2_open_free(&rqst[0]);
2720 SMB2_query_info_free(&rqst[1]);
2721 SMB2_close_free(&rqst[2]);
2722 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2723 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2728 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2729 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2731 struct smb2_query_info_rsp *rsp;
2732 struct smb2_fs_full_size_info *info = NULL;
2733 __le16 utf16_path = 0; /* Null - open root of share */
2734 struct kvec rsp_iov = {NULL, 0};
2735 int buftype = CIFS_NO_BUFFER;
2739 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2740 FILE_READ_ATTRIBUTES,
2741 FS_FULL_SIZE_INFORMATION,
2742 SMB2_O_INFO_FILESYSTEM,
2743 sizeof(struct smb2_fs_full_size_info),
2744 &rsp_iov, &buftype, cifs_sb);
2748 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2749 buf->f_type = SMB2_MAGIC_NUMBER;
2750 info = (struct smb2_fs_full_size_info *)(
2751 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2752 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2753 le32_to_cpu(rsp->OutputBufferLength),
2755 sizeof(struct smb2_fs_full_size_info));
2757 smb2_copy_fs_info_to_kstatfs(info, buf);
2760 free_rsp_buf(buftype, rsp_iov.iov_base);
2765 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2766 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2769 __le16 srch_path = 0; /* Null - open root of share */
2770 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2771 struct cifs_open_parms oparms;
2772 struct cifs_fid fid;
2774 if (!tcon->posix_extensions)
2775 return smb2_queryfs(xid, tcon, cifs_sb, buf);
2778 oparms.desired_access = FILE_READ_ATTRIBUTES;
2779 oparms.disposition = FILE_OPEN;
2780 oparms.create_options = cifs_create_options(cifs_sb, 0);
2782 oparms.reconnect = false;
2784 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
2789 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2790 fid.volatile_fid, buf);
2791 buf->f_type = SMB2_MAGIC_NUMBER;
2792 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2797 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2799 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2800 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2804 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2805 __u64 length, __u32 type, int lock, int unlock, bool wait)
2807 if (unlock && !lock)
2808 type = SMB2_LOCKFLAG_UNLOCK;
2809 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2810 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2811 current->tgid, length, offset, type, wait);
2815 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2817 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2821 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2823 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2827 smb2_new_lease_key(struct cifs_fid *fid)
2829 generate_random_uuid(fid->lease_key);
2833 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2834 const char *search_name,
2835 struct dfs_info3_param **target_nodes,
2836 unsigned int *num_of_nodes,
2837 const struct nls_table *nls_codepage, int remap)
2840 __le16 *utf16_path = NULL;
2841 int utf16_path_len = 0;
2842 struct cifs_tcon *tcon;
2843 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2844 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2845 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2847 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
2850 * Try to use the IPC tcon, otherwise just use any
2852 tcon = ses->tcon_ipc;
2854 spin_lock(&cifs_tcp_ses_lock);
2855 tcon = list_first_entry_or_null(&ses->tcon_list,
2860 spin_unlock(&cifs_tcp_ses_lock);
2864 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2870 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2872 nls_codepage, remap);
2878 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2879 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2885 /* Highest DFS referral version understood */
2886 dfs_req->MaxReferralLevel = DFS_VERSION;
2888 /* Path to resolve in an UTF-16 null-terminated string */
2889 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2892 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2893 FSCTL_DFS_GET_REFERRALS,
2894 true /* is_fsctl */,
2895 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
2896 (char **)&dfs_rsp, &dfs_rsp_size);
2897 } while (rc == -EAGAIN);
2900 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
2901 cifs_tcon_dbg(VFS, "ioctl error in %s rc=%d\n", __func__, rc);
2905 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2906 num_of_nodes, target_nodes,
2907 nls_codepage, remap, search_name,
2908 true /* is_unicode */);
2910 cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
2915 if (tcon && !tcon->ipc) {
2916 /* ipc tcons are not refcounted */
2917 spin_lock(&cifs_tcp_ses_lock);
2919 /* tc_count can never go negative */
2920 WARN_ON(tcon->tc_count < 0);
2921 spin_unlock(&cifs_tcp_ses_lock);
2930 parse_reparse_posix(struct reparse_posix_data *symlink_buf,
2931 u32 plen, char **target_path,
2932 struct cifs_sb_info *cifs_sb)
2936 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
2937 len = le16_to_cpu(symlink_buf->ReparseDataLength);
2939 if (le64_to_cpu(symlink_buf->InodeType) != NFS_SPECFILE_LNK) {
2940 cifs_dbg(VFS, "%lld not a supported symlink type\n",
2941 le64_to_cpu(symlink_buf->InodeType));
2945 *target_path = cifs_strndup_from_utf16(
2946 symlink_buf->PathBuffer,
2947 len, true, cifs_sb->local_nls);
2948 if (!(*target_path))
2951 convert_delimiter(*target_path, '/');
2952 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2958 parse_reparse_symlink(struct reparse_symlink_data_buffer *symlink_buf,
2959 u32 plen, char **target_path,
2960 struct cifs_sb_info *cifs_sb)
2962 unsigned int sub_len;
2963 unsigned int sub_offset;
2965 /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
2967 sub_offset = le16_to_cpu(symlink_buf->SubstituteNameOffset);
2968 sub_len = le16_to_cpu(symlink_buf->SubstituteNameLength);
2969 if (sub_offset + 20 > plen ||
2970 sub_offset + sub_len + 20 > plen) {
2971 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
2975 *target_path = cifs_strndup_from_utf16(
2976 symlink_buf->PathBuffer + sub_offset,
2977 sub_len, true, cifs_sb->local_nls);
2978 if (!(*target_path))
2981 convert_delimiter(*target_path, '/');
2982 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2988 parse_reparse_point(struct reparse_data_buffer *buf,
2989 u32 plen, char **target_path,
2990 struct cifs_sb_info *cifs_sb)
2992 if (plen < sizeof(struct reparse_data_buffer)) {
2993 cifs_dbg(VFS, "reparse buffer is too small. Must be at least 8 bytes but was %d\n",
2998 if (plen < le16_to_cpu(buf->ReparseDataLength) +
2999 sizeof(struct reparse_data_buffer)) {
3000 cifs_dbg(VFS, "srv returned invalid reparse buf length: %d\n",
3005 /* See MS-FSCC 2.1.2 */
3006 switch (le32_to_cpu(buf->ReparseTag)) {
3007 case IO_REPARSE_TAG_NFS:
3008 return parse_reparse_posix(
3009 (struct reparse_posix_data *)buf,
3010 plen, target_path, cifs_sb);
3011 case IO_REPARSE_TAG_SYMLINK:
3012 return parse_reparse_symlink(
3013 (struct reparse_symlink_data_buffer *)buf,
3014 plen, target_path, cifs_sb);
3016 cifs_dbg(VFS, "srv returned unknown symlink buffer tag:0x%08x\n",
3017 le32_to_cpu(buf->ReparseTag));
3022 #define SMB2_SYMLINK_STRUCT_SIZE \
3023 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
3026 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
3027 struct cifs_sb_info *cifs_sb, const char *full_path,
3028 char **target_path, bool is_reparse_point)
3031 __le16 *utf16_path = NULL;
3032 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3033 struct cifs_open_parms oparms;
3034 struct cifs_fid fid;
3035 struct kvec err_iov = {NULL, 0};
3036 struct smb2_err_rsp *err_buf = NULL;
3037 struct smb2_symlink_err_rsp *symlink;
3038 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
3039 unsigned int sub_len;
3040 unsigned int sub_offset;
3041 unsigned int print_len;
3042 unsigned int print_offset;
3043 int flags = CIFS_CP_CREATE_CLOSE_OP;
3044 struct smb_rqst rqst[3];
3045 int resp_buftype[3];
3046 struct kvec rsp_iov[3];
3047 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
3048 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
3049 struct kvec close_iov[1];
3050 struct smb2_create_rsp *create_rsp;
3051 struct smb2_ioctl_rsp *ioctl_rsp;
3052 struct reparse_data_buffer *reparse_buf;
3053 int create_options = is_reparse_point ? OPEN_REPARSE_POINT : 0;
3056 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
3058 *target_path = NULL;
3060 if (smb3_encryption_required(tcon))
3061 flags |= CIFS_TRANSFORM_REQ;
3063 memset(rqst, 0, sizeof(rqst));
3064 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
3065 memset(rsp_iov, 0, sizeof(rsp_iov));
3067 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
3072 memset(&open_iov, 0, sizeof(open_iov));
3073 rqst[0].rq_iov = open_iov;
3074 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
3076 memset(&oparms, 0, sizeof(oparms));
3078 oparms.desired_access = FILE_READ_ATTRIBUTES;
3079 oparms.disposition = FILE_OPEN;
3080 oparms.create_options = cifs_create_options(cifs_sb, create_options);
3082 oparms.reconnect = false;
3084 rc = SMB2_open_init(tcon, server,
3085 &rqst[0], &oplock, &oparms, utf16_path);
3088 smb2_set_next_command(tcon, &rqst[0]);
3092 memset(&io_iov, 0, sizeof(io_iov));
3093 rqst[1].rq_iov = io_iov;
3094 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
3096 rc = SMB2_ioctl_init(tcon, server,
3097 &rqst[1], fid.persistent_fid,
3098 fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
3099 true /* is_fctl */, NULL, 0,
3101 MAX_SMB2_CREATE_RESPONSE_SIZE -
3102 MAX_SMB2_CLOSE_RESPONSE_SIZE);
3106 smb2_set_next_command(tcon, &rqst[1]);
3107 smb2_set_related(&rqst[1]);
3111 memset(&close_iov, 0, sizeof(close_iov));
3112 rqst[2].rq_iov = close_iov;
3113 rqst[2].rq_nvec = 1;
3115 rc = SMB2_close_init(tcon, server,
3116 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
3120 smb2_set_related(&rqst[2]);
3122 rc = compound_send_recv(xid, tcon->ses, server,
3124 resp_buftype, rsp_iov);
3126 create_rsp = rsp_iov[0].iov_base;
3127 if (create_rsp && create_rsp->sync_hdr.Status)
3128 err_iov = rsp_iov[0];
3129 ioctl_rsp = rsp_iov[1].iov_base;
3132 * Open was successful and we got an ioctl response.
3134 if ((rc == 0) && (is_reparse_point)) {
3135 /* See MS-FSCC 2.3.23 */
3137 reparse_buf = (struct reparse_data_buffer *)
3138 ((char *)ioctl_rsp +
3139 le32_to_cpu(ioctl_rsp->OutputOffset));
3140 plen = le32_to_cpu(ioctl_rsp->OutputCount);
3142 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
3143 rsp_iov[1].iov_len) {
3144 cifs_tcon_dbg(VFS, "srv returned invalid ioctl len: %d\n",
3150 rc = parse_reparse_point(reparse_buf, plen, target_path,
3155 if (!rc || !err_iov.iov_base) {
3160 err_buf = err_iov.iov_base;
3161 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
3162 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
3167 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
3168 if (le32_to_cpu(symlink->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
3169 le32_to_cpu(symlink->ReparseTag) != IO_REPARSE_TAG_SYMLINK) {
3174 /* open must fail on symlink - reset rc */
3176 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
3177 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
3178 print_len = le16_to_cpu(symlink->PrintNameLength);
3179 print_offset = le16_to_cpu(symlink->PrintNameOffset);
3181 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
3186 if (err_iov.iov_len <
3187 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
3192 *target_path = cifs_strndup_from_utf16(
3193 (char *)symlink->PathBuffer + sub_offset,
3194 sub_len, true, cifs_sb->local_nls);
3195 if (!(*target_path)) {
3199 convert_delimiter(*target_path, '/');
3200 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
3203 cifs_dbg(FYI, "query symlink rc %d\n", rc);
3205 SMB2_open_free(&rqst[0]);
3206 SMB2_ioctl_free(&rqst[1]);
3207 SMB2_close_free(&rqst[2]);
3208 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
3209 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3210 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
3215 smb2_query_reparse_tag(const unsigned int xid, struct cifs_tcon *tcon,
3216 struct cifs_sb_info *cifs_sb, const char *full_path,
3220 __le16 *utf16_path = NULL;
3221 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3222 struct cifs_open_parms oparms;
3223 struct cifs_fid fid;
3224 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
3225 int flags = CIFS_CP_CREATE_CLOSE_OP;
3226 struct smb_rqst rqst[3];
3227 int resp_buftype[3];
3228 struct kvec rsp_iov[3];
3229 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
3230 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
3231 struct kvec close_iov[1];
3232 struct smb2_ioctl_rsp *ioctl_rsp;
3233 struct reparse_data_buffer *reparse_buf;
3236 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
3238 if (smb3_encryption_required(tcon))
3239 flags |= CIFS_TRANSFORM_REQ;
3241 memset(rqst, 0, sizeof(rqst));
3242 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
3243 memset(rsp_iov, 0, sizeof(rsp_iov));
3245 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
3250 * setup smb2open - TODO add optimization to call cifs_get_readable_path
3251 * to see if there is a handle already open that we can use
3253 memset(&open_iov, 0, sizeof(open_iov));
3254 rqst[0].rq_iov = open_iov;
3255 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
3257 memset(&oparms, 0, sizeof(oparms));
3259 oparms.desired_access = FILE_READ_ATTRIBUTES;
3260 oparms.disposition = FILE_OPEN;
3261 oparms.create_options = cifs_create_options(cifs_sb, OPEN_REPARSE_POINT);
3263 oparms.reconnect = false;
3265 rc = SMB2_open_init(tcon, server,
3266 &rqst[0], &oplock, &oparms, utf16_path);
3269 smb2_set_next_command(tcon, &rqst[0]);
3273 memset(&io_iov, 0, sizeof(io_iov));
3274 rqst[1].rq_iov = io_iov;
3275 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
3277 rc = SMB2_ioctl_init(tcon, server,
3278 &rqst[1], COMPOUND_FID,
3279 COMPOUND_FID, FSCTL_GET_REPARSE_POINT,
3280 true /* is_fctl */, NULL, 0,
3282 MAX_SMB2_CREATE_RESPONSE_SIZE -
3283 MAX_SMB2_CLOSE_RESPONSE_SIZE);
3287 smb2_set_next_command(tcon, &rqst[1]);
3288 smb2_set_related(&rqst[1]);
3292 memset(&close_iov, 0, sizeof(close_iov));
3293 rqst[2].rq_iov = close_iov;
3294 rqst[2].rq_nvec = 1;
3296 rc = SMB2_close_init(tcon, server,
3297 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
3301 smb2_set_related(&rqst[2]);
3303 rc = compound_send_recv(xid, tcon->ses, server,
3305 resp_buftype, rsp_iov);
3307 ioctl_rsp = rsp_iov[1].iov_base;
3310 * Open was successful and we got an ioctl response.
3313 /* See MS-FSCC 2.3.23 */
3315 reparse_buf = (struct reparse_data_buffer *)
3316 ((char *)ioctl_rsp +
3317 le32_to_cpu(ioctl_rsp->OutputOffset));
3318 plen = le32_to_cpu(ioctl_rsp->OutputCount);
3320 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
3321 rsp_iov[1].iov_len) {
3322 cifs_tcon_dbg(FYI, "srv returned invalid ioctl len: %d\n",
3327 *tag = le32_to_cpu(reparse_buf->ReparseTag);
3332 SMB2_open_free(&rqst[0]);
3333 SMB2_ioctl_free(&rqst[1]);
3334 SMB2_close_free(&rqst[2]);
3335 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
3336 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3337 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
3341 static struct cifs_ntsd *
3342 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
3343 const struct cifs_fid *cifsfid, u32 *pacllen, u32 info)
3345 struct cifs_ntsd *pntsd = NULL;
3347 int rc = -EOPNOTSUPP;
3348 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3351 return ERR_CAST(tlink);
3354 cifs_dbg(FYI, "trying to get acl\n");
3356 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
3357 cifsfid->volatile_fid, (void **)&pntsd, pacllen,
3361 cifs_put_tlink(tlink);
3363 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3370 static struct cifs_ntsd *
3371 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
3372 const char *path, u32 *pacllen, u32 info)
3374 struct cifs_ntsd *pntsd = NULL;
3375 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3378 struct cifs_tcon *tcon;
3379 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3380 struct cifs_fid fid;
3381 struct cifs_open_parms oparms;
3384 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
3386 return ERR_CAST(tlink);
3388 tcon = tlink_tcon(tlink);
3391 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3399 oparms.desired_access = READ_CONTROL;
3400 oparms.disposition = FILE_OPEN;
3402 * When querying an ACL, even if the file is a symlink we want to open
3403 * the source not the target, and so the protocol requires that the
3404 * client specify this flag when opening a reparse point
3406 oparms.create_options = cifs_create_options(cifs_sb, 0) | OPEN_REPARSE_POINT;
3408 oparms.reconnect = false;
3410 if (info & SACL_SECINFO)
3411 oparms.desired_access |= SYSTEM_SECURITY;
3413 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
3417 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3418 fid.volatile_fid, (void **)&pntsd, pacllen,
3420 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3423 cifs_put_tlink(tlink);
3426 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3433 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
3434 struct inode *inode, const char *path, int aclflag)
3436 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3438 int rc, access_flags = 0;
3439 struct cifs_tcon *tcon;
3440 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3441 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3442 struct cifs_fid fid;
3443 struct cifs_open_parms oparms;
3446 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
3448 return PTR_ERR(tlink);
3450 tcon = tlink_tcon(tlink);
3453 if (aclflag & CIFS_ACL_OWNER || aclflag & CIFS_ACL_GROUP)
3454 access_flags |= WRITE_OWNER;
3455 if (aclflag & CIFS_ACL_SACL)
3456 access_flags |= SYSTEM_SECURITY;
3457 if (aclflag & CIFS_ACL_DACL)
3458 access_flags |= WRITE_DAC;
3460 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3468 oparms.desired_access = access_flags;
3469 oparms.create_options = cifs_create_options(cifs_sb, 0);
3470 oparms.disposition = FILE_OPEN;
3473 oparms.reconnect = false;
3475 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
3479 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3480 fid.volatile_fid, pnntsd, acllen, aclflag);
3481 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3484 cifs_put_tlink(tlink);
3489 /* Retrieve an ACL from the server */
3490 static struct cifs_ntsd *
3491 get_smb2_acl(struct cifs_sb_info *cifs_sb,
3492 struct inode *inode, const char *path,
3493 u32 *pacllen, u32 info)
3495 struct cifs_ntsd *pntsd = NULL;
3496 struct cifsFileInfo *open_file = NULL;
3498 if (inode && !(info & SACL_SECINFO))
3499 open_file = find_readable_file(CIFS_I(inode), true);
3500 if (!open_file || (info & SACL_SECINFO))
3501 return get_smb2_acl_by_path(cifs_sb, path, pacllen, info);
3503 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen, info);
3504 cifsFileInfo_put(open_file);
3508 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3509 loff_t offset, loff_t len, bool keep_size)
3511 struct cifs_ses *ses = tcon->ses;
3512 struct inode *inode;
3513 struct cifsInodeInfo *cifsi;
3514 struct cifsFileInfo *cfile = file->private_data;
3515 struct file_zero_data_information fsctl_buf;
3522 inode = d_inode(cfile->dentry);
3523 cifsi = CIFS_I(inode);
3525 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3526 ses->Suid, offset, len);
3529 * We zero the range through ioctl, so we need remove the page caches
3530 * first, otherwise the data may be inconsistent with the server.
3532 truncate_pagecache_range(inode, offset, offset + len - 1);
3534 /* if file not oplocked can't be sure whether asking to extend size */
3535 if (!CIFS_CACHE_READ(cifsi))
3536 if (keep_size == false) {
3538 trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
3539 tcon->tid, ses->Suid, offset, len, rc);
3544 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3546 fsctl_buf.FileOffset = cpu_to_le64(offset);
3547 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3549 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3550 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
3552 sizeof(struct file_zero_data_information),
3555 goto zero_range_exit;
3558 * do we also need to change the size of the file?
3560 if (keep_size == false && i_size_read(inode) < offset + len) {
3561 eof = cpu_to_le64(offset + len);
3562 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3563 cfile->fid.volatile_fid, cfile->pid, &eof);
3569 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3570 ses->Suid, offset, len, rc);
3572 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3573 ses->Suid, offset, len);
3577 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3578 loff_t offset, loff_t len)
3580 struct inode *inode;
3581 struct cifsFileInfo *cfile = file->private_data;
3582 struct file_zero_data_information fsctl_buf;
3585 __u8 set_sparse = 1;
3589 inode = d_inode(cfile->dentry);
3591 /* Need to make file sparse, if not already, before freeing range. */
3592 /* Consider adding equivalent for compressed since it could also work */
3593 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3599 filemap_invalidate_lock(inode->i_mapping);
3601 * We implement the punch hole through ioctl, so we need remove the page
3602 * caches first, otherwise the data may be inconsistent with the server.
3604 truncate_pagecache_range(inode, offset, offset + len - 1);
3606 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3608 fsctl_buf.FileOffset = cpu_to_le64(offset);
3609 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3611 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3612 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3613 true /* is_fctl */, (char *)&fsctl_buf,
3614 sizeof(struct file_zero_data_information),
3615 CIFSMaxBufSize, NULL, NULL);
3617 filemap_invalidate_unlock(inode->i_mapping);
3621 static int smb3_simple_fallocate_write_range(unsigned int xid,
3622 struct cifs_tcon *tcon,
3623 struct cifsFileInfo *cfile,
3624 loff_t off, loff_t len,
3627 struct cifs_io_parms io_parms = {0};
3632 io_parms.netfid = cfile->fid.netfid;
3633 io_parms.pid = current->tgid;
3634 io_parms.tcon = tcon;
3635 io_parms.persistent_fid = cfile->fid.persistent_fid;
3636 io_parms.volatile_fid = cfile->fid.volatile_fid;
3639 io_parms.offset = off;
3640 io_parms.length = len;
3641 if (io_parms.length > SMB2_MAX_BUFFER_SIZE)
3642 io_parms.length = SMB2_MAX_BUFFER_SIZE;
3643 /* iov[0] is reserved for smb header */
3644 iov[1].iov_base = buf;
3645 iov[1].iov_len = io_parms.length;
3646 rc = SMB2_write(xid, &io_parms, &nbytes, iov, 1);
3658 static int smb3_simple_fallocate_range(unsigned int xid,
3659 struct cifs_tcon *tcon,
3660 struct cifsFileInfo *cfile,
3661 loff_t off, loff_t len)
3663 struct file_allocated_range_buffer in_data, *out_data = NULL, *tmp_data;
3669 in_data.file_offset = cpu_to_le64(off);
3670 in_data.length = cpu_to_le64(len);
3671 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3672 cfile->fid.volatile_fid,
3673 FSCTL_QUERY_ALLOCATED_RANGES, true,
3674 (char *)&in_data, sizeof(in_data),
3675 1024 * sizeof(struct file_allocated_range_buffer),
3676 (char **)&out_data, &out_data_len);
3680 buf = kzalloc(1024 * 1024, GFP_KERNEL);
3686 tmp_data = out_data;
3689 * The rest of the region is unmapped so write it all.
3691 if (out_data_len == 0) {
3692 rc = smb3_simple_fallocate_write_range(xid, tcon,
3693 cfile, off, len, buf);
3697 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3702 if (off < le64_to_cpu(tmp_data->file_offset)) {
3704 * We are at a hole. Write until the end of the region
3705 * or until the next allocated data,
3706 * whichever comes next.
3708 l = le64_to_cpu(tmp_data->file_offset) - off;
3711 rc = smb3_simple_fallocate_write_range(xid, tcon,
3712 cfile, off, l, buf);
3721 * We are at a section of allocated data, just skip forward
3722 * until the end of the data or the end of the region
3723 * we are supposed to fallocate, whichever comes first.
3725 l = le64_to_cpu(tmp_data->length);
3731 tmp_data = &tmp_data[1];
3732 out_data_len -= sizeof(struct file_allocated_range_buffer);
3742 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3743 loff_t off, loff_t len, bool keep_size)
3745 struct inode *inode;
3746 struct cifsInodeInfo *cifsi;
3747 struct cifsFileInfo *cfile = file->private_data;
3748 long rc = -EOPNOTSUPP;
3754 inode = d_inode(cfile->dentry);
3755 cifsi = CIFS_I(inode);
3757 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3758 tcon->ses->Suid, off, len);
3759 /* if file not oplocked can't be sure whether asking to extend size */
3760 if (!CIFS_CACHE_READ(cifsi))
3761 if (keep_size == false) {
3762 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3763 tcon->tid, tcon->ses->Suid, off, len, rc);
3769 * Extending the file
3771 if ((keep_size == false) && i_size_read(inode) < off + len) {
3772 rc = inode_newsize_ok(inode, off + len);
3776 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0)
3777 smb2_set_sparse(xid, tcon, cfile, inode, false);
3779 eof = cpu_to_le64(off + len);
3780 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3781 cfile->fid.volatile_fid, cfile->pid, &eof);
3783 cifsi->server_eof = off + len;
3784 cifs_setsize(inode, off + len);
3785 cifs_truncate_page(inode->i_mapping, inode->i_size);
3786 truncate_setsize(inode, off + len);
3792 * Files are non-sparse by default so falloc may be a no-op
3793 * Must check if file sparse. If not sparse, and since we are not
3794 * extending then no need to do anything since file already allocated
3796 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
3801 if (keep_size == true) {
3803 * We can not preallocate pages beyond the end of the file
3806 if (off >= i_size_read(inode)) {
3811 * For fallocates that are partially beyond the end of file,
3812 * clamp len so we only fallocate up to the end of file.
3814 if (off + len > i_size_read(inode)) {
3815 len = i_size_read(inode) - off;
3819 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3821 * At this point, we are trying to fallocate an internal
3822 * regions of a sparse file. Since smb2 does not have a
3823 * fallocate command we have two otions on how to emulate this.
3824 * We can either turn the entire file to become non-sparse
3825 * which we only do if the fallocate is for virtually
3826 * the whole file, or we can overwrite the region with zeroes
3827 * using SMB2_write, which could be prohibitevly expensive
3831 * We are only trying to fallocate a small region so
3832 * just write it with zero.
3834 if (len <= 1024 * 1024) {
3835 rc = smb3_simple_fallocate_range(xid, tcon, cfile,
3841 * Check if falloc starts within first few pages of file
3842 * and ends within a few pages of the end of file to
3843 * ensure that most of file is being forced to be
3844 * fallocated now. If so then setting whole file sparse
3845 * ie potentially making a few extra pages at the beginning
3846 * or end of the file non-sparse via set_sparse is harmless.
3848 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3854 smb2_set_sparse(xid, tcon, cfile, inode, false);
3859 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3860 tcon->ses->Suid, off, len, rc);
3862 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3863 tcon->ses->Suid, off, len);
3869 static long smb3_collapse_range(struct file *file, struct cifs_tcon *tcon,
3870 loff_t off, loff_t len)
3874 struct cifsFileInfo *cfile = file->private_data;
3879 if (off >= i_size_read(file->f_inode) ||
3880 off + len >= i_size_read(file->f_inode)) {
3885 rc = smb2_copychunk_range(xid, cfile, cfile, off + len,
3886 i_size_read(file->f_inode) - off - len, off);
3890 eof = cpu_to_le64(i_size_read(file->f_inode) - len);
3891 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3892 cfile->fid.volatile_fid, cfile->pid, &eof);
3902 static long smb3_insert_range(struct file *file, struct cifs_tcon *tcon,
3903 loff_t off, loff_t len)
3907 struct cifsFileInfo *cfile = file->private_data;
3913 if (off >= i_size_read(file->f_inode)) {
3918 count = i_size_read(file->f_inode) - off;
3919 eof = cpu_to_le64(i_size_read(file->f_inode) + len);
3921 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3922 cfile->fid.volatile_fid, cfile->pid, &eof);
3926 rc = smb2_copychunk_range(xid, cfile, cfile, off, count, off + len);
3930 rc = smb3_zero_range(file, tcon, off, len, 1);
3940 static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3942 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3943 struct cifsInodeInfo *cifsi;
3944 struct inode *inode;
3946 struct file_allocated_range_buffer in_data, *out_data = NULL;
3950 if (whence != SEEK_HOLE && whence != SEEK_DATA)
3951 return generic_file_llseek(file, offset, whence);
3953 inode = d_inode(cfile->dentry);
3954 cifsi = CIFS_I(inode);
3956 if (offset < 0 || offset >= i_size_read(inode))
3961 * We need to be sure that all dirty pages are written as they
3962 * might fill holes on the server.
3963 * Note that we also MUST flush any written pages since at least
3964 * some servers (Windows2016) will not reflect recent writes in
3965 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3967 wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
3969 filemap_write_and_wait(inode->i_mapping);
3970 smb2_flush_file(xid, tcon, &wrcfile->fid);
3971 cifsFileInfo_put(wrcfile);
3974 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3975 if (whence == SEEK_HOLE)
3976 offset = i_size_read(inode);
3980 in_data.file_offset = cpu_to_le64(offset);
3981 in_data.length = cpu_to_le64(i_size_read(inode));
3983 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3984 cfile->fid.volatile_fid,
3985 FSCTL_QUERY_ALLOCATED_RANGES, true,
3986 (char *)&in_data, sizeof(in_data),
3987 sizeof(struct file_allocated_range_buffer),
3988 (char **)&out_data, &out_data_len);
3994 if (whence == SEEK_HOLE && out_data_len == 0)
3997 if (whence == SEEK_DATA && out_data_len == 0) {
4002 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
4006 if (whence == SEEK_DATA) {
4007 offset = le64_to_cpu(out_data->file_offset);
4010 if (offset < le64_to_cpu(out_data->file_offset))
4013 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
4019 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
4024 static int smb3_fiemap(struct cifs_tcon *tcon,
4025 struct cifsFileInfo *cfile,
4026 struct fiemap_extent_info *fei, u64 start, u64 len)
4029 struct file_allocated_range_buffer in_data, *out_data;
4031 int i, num, rc, flags, last_blob;
4034 rc = fiemap_prep(d_inode(cfile->dentry), fei, start, &len, 0);
4040 in_data.file_offset = cpu_to_le64(start);
4041 in_data.length = cpu_to_le64(len);
4043 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
4044 cfile->fid.volatile_fid,
4045 FSCTL_QUERY_ALLOCATED_RANGES, true,
4046 (char *)&in_data, sizeof(in_data),
4047 1024 * sizeof(struct file_allocated_range_buffer),
4048 (char **)&out_data, &out_data_len);
4057 if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) {
4061 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
4066 num = out_data_len / sizeof(struct file_allocated_range_buffer);
4067 for (i = 0; i < num; i++) {
4069 if (i == num - 1 && last_blob)
4070 flags |= FIEMAP_EXTENT_LAST;
4072 rc = fiemap_fill_next_extent(fei,
4073 le64_to_cpu(out_data[i].file_offset),
4074 le64_to_cpu(out_data[i].file_offset),
4075 le64_to_cpu(out_data[i].length),
4086 next = le64_to_cpu(out_data[num - 1].file_offset) +
4087 le64_to_cpu(out_data[num - 1].length);
4088 len = len - (next - start);
4099 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
4100 loff_t off, loff_t len)
4102 /* KEEP_SIZE already checked for by do_fallocate */
4103 if (mode & FALLOC_FL_PUNCH_HOLE)
4104 return smb3_punch_hole(file, tcon, off, len);
4105 else if (mode & FALLOC_FL_ZERO_RANGE) {
4106 if (mode & FALLOC_FL_KEEP_SIZE)
4107 return smb3_zero_range(file, tcon, off, len, true);
4108 return smb3_zero_range(file, tcon, off, len, false);
4109 } else if (mode == FALLOC_FL_KEEP_SIZE)
4110 return smb3_simple_falloc(file, tcon, off, len, true);
4111 else if (mode == FALLOC_FL_COLLAPSE_RANGE)
4112 return smb3_collapse_range(file, tcon, off, len);
4113 else if (mode == FALLOC_FL_INSERT_RANGE)
4114 return smb3_insert_range(file, tcon, off, len);
4116 return smb3_simple_falloc(file, tcon, off, len, false);
4122 smb2_downgrade_oplock(struct TCP_Server_Info *server,
4123 struct cifsInodeInfo *cinode, __u32 oplock,
4124 unsigned int epoch, bool *purge_cache)
4126 server->ops->set_oplock_level(cinode, oplock, 0, NULL);
4130 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
4131 unsigned int epoch, bool *purge_cache);
4134 smb3_downgrade_oplock(struct TCP_Server_Info *server,
4135 struct cifsInodeInfo *cinode, __u32 oplock,
4136 unsigned int epoch, bool *purge_cache)
4138 unsigned int old_state = cinode->oplock;
4139 unsigned int old_epoch = cinode->epoch;
4140 unsigned int new_state;
4142 if (epoch > old_epoch) {
4143 smb21_set_oplock_level(cinode, oplock, 0, NULL);
4144 cinode->epoch = epoch;
4147 new_state = cinode->oplock;
4148 *purge_cache = false;
4150 if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
4151 (new_state & CIFS_CACHE_READ_FLG) == 0)
4152 *purge_cache = true;
4153 else if (old_state == new_state && (epoch - old_epoch > 1))
4154 *purge_cache = true;
4158 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
4159 unsigned int epoch, bool *purge_cache)
4162 cinode->lease_granted = false;
4163 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
4165 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
4166 cinode->oplock = CIFS_CACHE_RHW_FLG;
4167 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
4168 &cinode->vfs_inode);
4169 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
4170 cinode->oplock = CIFS_CACHE_RW_FLG;
4171 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
4172 &cinode->vfs_inode);
4173 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
4174 cinode->oplock = CIFS_CACHE_READ_FLG;
4175 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
4176 &cinode->vfs_inode);
4182 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
4183 unsigned int epoch, bool *purge_cache)
4185 char message[5] = {0};
4186 unsigned int new_oplock = 0;
4189 cinode->lease_granted = true;
4190 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
4193 /* Check if the server granted an oplock rather than a lease */
4194 if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
4195 return smb2_set_oplock_level(cinode, oplock, epoch,
4198 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
4199 new_oplock |= CIFS_CACHE_READ_FLG;
4200 strcat(message, "R");
4202 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
4203 new_oplock |= CIFS_CACHE_HANDLE_FLG;
4204 strcat(message, "H");
4206 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
4207 new_oplock |= CIFS_CACHE_WRITE_FLG;
4208 strcat(message, "W");
4211 strncpy(message, "None", sizeof(message));
4213 cinode->oplock = new_oplock;
4214 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
4215 &cinode->vfs_inode);
4219 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
4220 unsigned int epoch, bool *purge_cache)
4222 unsigned int old_oplock = cinode->oplock;
4224 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
4227 *purge_cache = false;
4228 if (old_oplock == CIFS_CACHE_READ_FLG) {
4229 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
4230 (epoch - cinode->epoch > 0))
4231 *purge_cache = true;
4232 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
4233 (epoch - cinode->epoch > 1))
4234 *purge_cache = true;
4235 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
4236 (epoch - cinode->epoch > 1))
4237 *purge_cache = true;
4238 else if (cinode->oplock == 0 &&
4239 (epoch - cinode->epoch > 0))
4240 *purge_cache = true;
4241 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
4242 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
4243 (epoch - cinode->epoch > 0))
4244 *purge_cache = true;
4245 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
4246 (epoch - cinode->epoch > 1))
4247 *purge_cache = true;
4249 cinode->epoch = epoch;
4254 smb2_is_read_op(__u32 oplock)
4256 return oplock == SMB2_OPLOCK_LEVEL_II;
4260 smb21_is_read_op(__u32 oplock)
4262 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
4263 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
4267 map_oplock_to_lease(u8 oplock)
4269 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
4270 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
4271 else if (oplock == SMB2_OPLOCK_LEVEL_II)
4272 return SMB2_LEASE_READ_CACHING;
4273 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
4274 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
4275 SMB2_LEASE_WRITE_CACHING;
4280 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
4282 struct create_lease *buf;
4284 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
4288 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
4289 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
4291 buf->ccontext.DataOffset = cpu_to_le16(offsetof
4292 (struct create_lease, lcontext));
4293 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
4294 buf->ccontext.NameOffset = cpu_to_le16(offsetof
4295 (struct create_lease, Name));
4296 buf->ccontext.NameLength = cpu_to_le16(4);
4297 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4306 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
4308 struct create_lease_v2 *buf;
4310 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
4314 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
4315 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
4317 buf->ccontext.DataOffset = cpu_to_le16(offsetof
4318 (struct create_lease_v2, lcontext));
4319 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
4320 buf->ccontext.NameOffset = cpu_to_le16(offsetof
4321 (struct create_lease_v2, Name));
4322 buf->ccontext.NameLength = cpu_to_le16(4);
4323 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4332 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
4334 struct create_lease *lc = (struct create_lease *)buf;
4336 *epoch = 0; /* not used */
4337 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
4338 return SMB2_OPLOCK_LEVEL_NOCHANGE;
4339 return le32_to_cpu(lc->lcontext.LeaseState);
4343 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
4345 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
4347 *epoch = le16_to_cpu(lc->lcontext.Epoch);
4348 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
4349 return SMB2_OPLOCK_LEVEL_NOCHANGE;
4351 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
4352 return le32_to_cpu(lc->lcontext.LeaseState);
4356 smb2_wp_retry_size(struct inode *inode)
4358 return min_t(unsigned int, CIFS_SB(inode->i_sb)->ctx->wsize,
4359 SMB2_MAX_BUFFER_SIZE);
4363 smb2_dir_needs_close(struct cifsFileInfo *cfile)
4365 return !cfile->invalidHandle;
4369 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
4370 struct smb_rqst *old_rq, __le16 cipher_type)
4372 struct smb2_sync_hdr *shdr =
4373 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
4375 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
4376 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
4377 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
4378 tr_hdr->Flags = cpu_to_le16(0x01);
4379 if ((cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4380 (cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4381 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
4383 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
4384 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
4387 /* We can not use the normal sg_set_buf() as we will sometimes pass a
4388 * stack object as buf.
4390 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
4391 unsigned int buflen)
4395 * VMAP_STACK (at least) puts stack into the vmalloc address space
4397 if (is_vmalloc_addr(buf))
4398 addr = vmalloc_to_page(buf);
4400 addr = virt_to_page(buf);
4401 sg_set_page(sg, addr, buflen, offset_in_page(buf));
4404 /* Assumes the first rqst has a transform header as the first iov.
4406 * rqst[0].rq_iov[0] is transform header
4407 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
4408 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
4410 static struct scatterlist *
4411 init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
4413 unsigned int sg_len;
4414 struct scatterlist *sg;
4417 unsigned int idx = 0;
4421 for (i = 0; i < num_rqst; i++)
4422 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
4424 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
4428 sg_init_table(sg, sg_len);
4429 for (i = 0; i < num_rqst; i++) {
4430 for (j = 0; j < rqst[i].rq_nvec; j++) {
4432 * The first rqst has a transform header where the
4433 * first 20 bytes are not part of the encrypted blob
4435 skip = (i == 0) && (j == 0) ? 20 : 0;
4436 smb2_sg_set_buf(&sg[idx++],
4437 rqst[i].rq_iov[j].iov_base + skip,
4438 rqst[i].rq_iov[j].iov_len - skip);
4441 for (j = 0; j < rqst[i].rq_npages; j++) {
4442 unsigned int len, offset;
4444 rqst_page_get_length(&rqst[i], j, &len, &offset);
4445 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
4448 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
4453 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
4455 struct cifs_ses *ses;
4458 spin_lock(&cifs_tcp_ses_lock);
4459 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
4460 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
4461 if (ses->Suid == ses_id) {
4462 ses_enc_key = enc ? ses->smb3encryptionkey :
4463 ses->smb3decryptionkey;
4464 memcpy(key, ses_enc_key, SMB3_ENC_DEC_KEY_SIZE);
4465 spin_unlock(&cifs_tcp_ses_lock);
4470 spin_unlock(&cifs_tcp_ses_lock);
4475 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
4476 * iov[0] - transform header (associate data),
4477 * iov[1-N] - SMB2 header and pages - data to encrypt.
4478 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
4482 crypt_message(struct TCP_Server_Info *server, int num_rqst,
4483 struct smb_rqst *rqst, int enc)
4485 struct smb2_transform_hdr *tr_hdr =
4486 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
4487 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
4489 struct scatterlist *sg;
4490 u8 sign[SMB2_SIGNATURE_SIZE] = {};
4491 u8 key[SMB3_ENC_DEC_KEY_SIZE];
4492 struct aead_request *req;
4494 unsigned int iv_len;
4495 DECLARE_CRYPTO_WAIT(wait);
4496 struct crypto_aead *tfm;
4497 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4499 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
4501 cifs_server_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
4506 rc = smb3_crypto_aead_allocate(server);
4508 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
4512 tfm = enc ? server->secmech.ccmaesencrypt :
4513 server->secmech.ccmaesdecrypt;
4515 if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
4516 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4517 rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
4519 rc = crypto_aead_setkey(tfm, key, SMB3_GCM128_CRYPTKEY_SIZE);
4522 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
4526 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
4528 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
4532 req = aead_request_alloc(tfm, GFP_KERNEL);
4534 cifs_server_dbg(VFS, "%s: Failed to alloc aead request\n", __func__);
4539 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
4540 crypt_len += SMB2_SIGNATURE_SIZE;
4543 sg = init_sg(num_rqst, rqst, sign);
4545 cifs_server_dbg(VFS, "%s: Failed to init sg\n", __func__);
4550 iv_len = crypto_aead_ivsize(tfm);
4551 iv = kzalloc(iv_len, GFP_KERNEL);
4553 cifs_server_dbg(VFS, "%s: Failed to alloc iv\n", __func__);
4558 if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4559 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4560 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
4563 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
4566 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
4567 aead_request_set_ad(req, assoc_data_len);
4569 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
4570 crypto_req_done, &wait);
4572 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
4573 : crypto_aead_decrypt(req), &wait);
4576 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
4587 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
4591 for (i = 0; i < num_rqst; i++) {
4592 if (rqst[i].rq_pages) {
4593 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
4594 put_page(rqst[i].rq_pages[j]);
4595 kfree(rqst[i].rq_pages);
4601 * This function will initialize new_rq and encrypt the content.
4602 * The first entry, new_rq[0], only contains a single iov which contains
4603 * a smb2_transform_hdr and is pre-allocated by the caller.
4604 * This function then populates new_rq[1+] with the content from olq_rq[0+].
4606 * The end result is an array of smb_rqst structures where the first structure
4607 * only contains a single iov for the transform header which we then can pass
4608 * to crypt_message().
4610 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
4611 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
4614 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
4615 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
4617 struct page **pages;
4618 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
4619 unsigned int npages;
4620 unsigned int orig_len = 0;
4624 for (i = 1; i < num_rqst; i++) {
4625 npages = old_rq[i - 1].rq_npages;
4626 pages = kmalloc_array(npages, sizeof(struct page *),
4631 new_rq[i].rq_pages = pages;
4632 new_rq[i].rq_npages = npages;
4633 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
4634 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
4635 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
4636 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
4637 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
4639 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
4641 for (j = 0; j < npages; j++) {
4642 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4647 /* copy pages form the old */
4648 for (j = 0; j < npages; j++) {
4650 unsigned int offset, len;
4652 rqst_page_get_length(&new_rq[i], j, &len, &offset);
4654 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
4655 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
4657 memcpy(dst, src, len);
4658 kunmap(new_rq[i].rq_pages[j]);
4659 kunmap(old_rq[i - 1].rq_pages[j]);
4663 /* fill the 1st iov with a transform header */
4664 fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
4666 rc = crypt_message(server, num_rqst, new_rq, 1);
4667 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
4674 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
4679 smb3_is_transform_hdr(void *buf)
4681 struct smb2_transform_hdr *trhdr = buf;
4683 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
4687 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
4688 unsigned int buf_data_size, struct page **pages,
4689 unsigned int npages, unsigned int page_data_size,
4693 struct smb_rqst rqst = {NULL};
4696 iov[0].iov_base = buf;
4697 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
4698 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
4699 iov[1].iov_len = buf_data_size;
4703 rqst.rq_pages = pages;
4704 rqst.rq_npages = npages;
4705 rqst.rq_pagesz = PAGE_SIZE;
4706 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
4708 rc = crypt_message(server, 1, &rqst, 0);
4709 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
4714 memmove(buf, iov[1].iov_base, buf_data_size);
4717 server->total_read = buf_data_size + page_data_size;
4723 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
4724 unsigned int npages, unsigned int len)
4729 for (i = 0; i < npages; i++) {
4730 struct page *page = pages[i];
4734 if (len >= PAGE_SIZE) {
4735 /* enough data to fill the page */
4739 zero_user(page, len, PAGE_SIZE - len);
4742 length = cifs_read_page_from_socket(server, page, 0, n);
4745 server->total_read += length;
4752 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
4753 unsigned int cur_off, struct bio_vec **page_vec)
4755 struct bio_vec *bvec;
4758 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
4762 for (i = 0; i < npages; i++) {
4763 bvec[i].bv_page = pages[i];
4764 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
4765 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
4766 data_size -= bvec[i].bv_len;
4769 if (data_size != 0) {
4770 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4780 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4781 char *buf, unsigned int buf_len, struct page **pages,
4782 unsigned int npages, unsigned int page_data_size,
4785 unsigned int data_offset;
4786 unsigned int data_len;
4787 unsigned int cur_off;
4788 unsigned int cur_page_idx;
4789 unsigned int pad_len;
4790 struct cifs_readdata *rdata = mid->callback_data;
4791 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
4792 struct bio_vec *bvec = NULL;
4793 struct iov_iter iter;
4796 bool use_rdma_mr = false;
4798 if (shdr->Command != SMB2_READ) {
4799 cifs_server_dbg(VFS, "only big read responses are supported\n");
4803 if (server->ops->is_session_expired &&
4804 server->ops->is_session_expired(buf)) {
4806 cifs_reconnect(server);
4810 if (server->ops->is_status_pending &&
4811 server->ops->is_status_pending(buf, server))
4814 /* set up first two iov to get credits */
4815 rdata->iov[0].iov_base = buf;
4816 rdata->iov[0].iov_len = 0;
4817 rdata->iov[1].iov_base = buf;
4818 rdata->iov[1].iov_len =
4819 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
4820 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4821 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4822 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4823 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4825 rdata->result = server->ops->map_error(buf, true);
4826 if (rdata->result != 0) {
4827 cifs_dbg(FYI, "%s: server returned error %d\n",
4828 __func__, rdata->result);
4829 /* normal error on read response */
4831 mid->mid_state = MID_RESPONSE_RECEIVED;
4833 dequeue_mid(mid, false);
4837 data_offset = server->ops->read_data_offset(buf);
4838 #ifdef CONFIG_CIFS_SMB_DIRECT
4839 use_rdma_mr = rdata->mr;
4841 data_len = server->ops->read_data_length(buf, use_rdma_mr);
4843 if (data_offset < server->vals->read_rsp_size) {
4845 * win2k8 sometimes sends an offset of 0 when the read
4846 * is beyond the EOF. Treat it as if the data starts just after
4849 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4850 __func__, data_offset);
4851 data_offset = server->vals->read_rsp_size;
4852 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4853 /* data_offset is beyond the end of smallbuf */
4854 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4855 __func__, data_offset);
4856 rdata->result = -EIO;
4858 mid->mid_state = MID_RESPONSE_MALFORMED;
4860 dequeue_mid(mid, rdata->result);
4864 pad_len = data_offset - server->vals->read_rsp_size;
4866 if (buf_len <= data_offset) {
4867 /* read response payload is in pages */
4868 cur_page_idx = pad_len / PAGE_SIZE;
4869 cur_off = pad_len % PAGE_SIZE;
4871 if (cur_page_idx != 0) {
4872 /* data offset is beyond the 1st page of response */
4873 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4874 __func__, data_offset);
4875 rdata->result = -EIO;
4877 mid->mid_state = MID_RESPONSE_MALFORMED;
4879 dequeue_mid(mid, rdata->result);
4883 if (data_len > page_data_size - pad_len) {
4884 /* data_len is corrupt -- discard frame */
4885 rdata->result = -EIO;
4887 mid->mid_state = MID_RESPONSE_MALFORMED;
4889 dequeue_mid(mid, rdata->result);
4893 rdata->result = init_read_bvec(pages, npages, page_data_size,
4895 if (rdata->result != 0) {
4897 mid->mid_state = MID_RESPONSE_MALFORMED;
4899 dequeue_mid(mid, rdata->result);
4903 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
4904 } else if (buf_len >= data_offset + data_len) {
4905 /* read response payload is in buf */
4906 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
4907 iov.iov_base = buf + data_offset;
4908 iov.iov_len = data_len;
4909 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
4911 /* read response payload cannot be in both buf and pages */
4912 WARN_ONCE(1, "buf can not contain only a part of read data");
4913 rdata->result = -EIO;
4915 mid->mid_state = MID_RESPONSE_MALFORMED;
4917 dequeue_mid(mid, rdata->result);
4921 length = rdata->copy_into_pages(server, rdata, &iter);
4929 mid->mid_state = MID_RESPONSE_RECEIVED;
4931 dequeue_mid(mid, false);
4935 struct smb2_decrypt_work {
4936 struct work_struct decrypt;
4937 struct TCP_Server_Info *server;
4938 struct page **ppages;
4940 unsigned int npages;
4945 static void smb2_decrypt_offload(struct work_struct *work)
4947 struct smb2_decrypt_work *dw = container_of(work,
4948 struct smb2_decrypt_work, decrypt);
4950 struct mid_q_entry *mid;
4952 rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
4953 dw->ppages, dw->npages, dw->len, true);
4955 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4959 dw->server->lstrp = jiffies;
4960 mid = smb2_find_dequeue_mid(dw->server, dw->buf);
4962 cifs_dbg(FYI, "mid not found\n");
4964 mid->decrypted = true;
4965 rc = handle_read_data(dw->server, mid, dw->buf,
4966 dw->server->vals->read_rsp_size,
4967 dw->ppages, dw->npages, dw->len,
4970 #ifdef CONFIG_CIFS_STATS2
4971 mid->when_received = jiffies;
4973 if (dw->server->ops->is_network_name_deleted)
4974 dw->server->ops->is_network_name_deleted(dw->buf,
4979 spin_lock(&GlobalMid_Lock);
4980 if (dw->server->tcpStatus == CifsNeedReconnect) {
4981 mid->mid_state = MID_RETRY_NEEDED;
4982 spin_unlock(&GlobalMid_Lock);
4985 mid->mid_state = MID_REQUEST_SUBMITTED;
4986 mid->mid_flags &= ~(MID_DELETED);
4987 list_add_tail(&mid->qhead,
4988 &dw->server->pending_mid_q);
4989 spin_unlock(&GlobalMid_Lock);
4992 cifs_mid_q_entry_release(mid);
4996 for (i = dw->npages-1; i >= 0; i--)
4997 put_page(dw->ppages[i]);
5000 cifs_small_buf_release(dw->buf);
5006 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
5009 char *buf = server->smallbuf;
5010 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
5011 unsigned int npages;
5012 struct page **pages;
5014 unsigned int buflen = server->pdu_size;
5017 struct smb2_decrypt_work *dw;
5020 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
5021 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
5023 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
5026 server->total_read += rc;
5028 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
5029 server->vals->read_rsp_size;
5030 npages = DIV_ROUND_UP(len, PAGE_SIZE);
5032 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
5038 for (; i < npages; i++) {
5039 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
5046 /* read read data into pages */
5047 rc = read_data_into_pages(server, pages, npages, len);
5051 rc = cifs_discard_remaining_data(server);
5056 * For large reads, offload to different thread for better performance,
5057 * use more cores decrypting which can be expensive
5060 if ((server->min_offload) && (server->in_flight > 1) &&
5061 (server->pdu_size >= server->min_offload)) {
5062 dw = kmalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
5064 goto non_offloaded_decrypt;
5066 dw->buf = server->smallbuf;
5067 server->smallbuf = (char *)cifs_small_buf_get();
5069 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
5071 dw->npages = npages;
5072 dw->server = server;
5075 queue_work(decrypt_wq, &dw->decrypt);
5076 *num_mids = 0; /* worker thread takes care of finding mid */
5080 non_offloaded_decrypt:
5081 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
5082 pages, npages, len, false);
5086 *mid = smb2_find_mid(server, buf);
5088 cifs_dbg(FYI, "mid not found\n");
5090 cifs_dbg(FYI, "mid found\n");
5091 (*mid)->decrypted = true;
5092 rc = handle_read_data(server, *mid, buf,
5093 server->vals->read_rsp_size,
5094 pages, npages, len, false);
5096 if (server->ops->is_network_name_deleted) {
5097 server->ops->is_network_name_deleted(buf,
5104 for (i = i - 1; i >= 0; i--)
5109 cifs_discard_remaining_data(server);
5114 receive_encrypted_standard(struct TCP_Server_Info *server,
5115 struct mid_q_entry **mids, char **bufs,
5119 char *buf = server->smallbuf;
5120 struct smb2_sync_hdr *shdr;
5121 unsigned int pdu_length = server->pdu_size;
5122 unsigned int buf_size;
5123 struct mid_q_entry *mid_entry;
5125 char *next_buffer = NULL;
5129 /* switch to large buffer if too big for a small one */
5130 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
5131 server->large_buf = true;
5132 memcpy(server->bigbuf, buf, server->total_read);
5133 buf = server->bigbuf;
5136 /* now read the rest */
5137 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
5138 pdu_length - HEADER_SIZE(server) + 1);
5141 server->total_read += length;
5143 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
5144 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0, false);
5148 next_is_large = server->large_buf;
5150 shdr = (struct smb2_sync_hdr *)buf;
5151 if (shdr->NextCommand) {
5153 next_buffer = (char *)cifs_buf_get();
5155 next_buffer = (char *)cifs_small_buf_get();
5157 buf + le32_to_cpu(shdr->NextCommand),
5158 pdu_length - le32_to_cpu(shdr->NextCommand));
5161 mid_entry = smb2_find_mid(server, buf);
5162 if (mid_entry == NULL)
5163 cifs_dbg(FYI, "mid not found\n");
5165 cifs_dbg(FYI, "mid found\n");
5166 mid_entry->decrypted = true;
5167 mid_entry->resp_buf_size = server->pdu_size;
5170 if (*num_mids >= MAX_COMPOUND) {
5171 cifs_server_dbg(VFS, "too many PDUs in compound\n");
5174 bufs[*num_mids] = buf;
5175 mids[(*num_mids)++] = mid_entry;
5177 if (mid_entry && mid_entry->handle)
5178 ret = mid_entry->handle(server, mid_entry);
5180 ret = cifs_handle_standard(server, mid_entry);
5182 if (ret == 0 && shdr->NextCommand) {
5183 pdu_length -= le32_to_cpu(shdr->NextCommand);
5184 server->large_buf = next_is_large;
5186 server->bigbuf = buf = next_buffer;
5188 server->smallbuf = buf = next_buffer;
5190 } else if (ret != 0) {
5192 * ret != 0 here means that we didn't get to handle_mid() thus
5193 * server->smallbuf and server->bigbuf are still valid. We need
5194 * to free next_buffer because it is not going to be used
5198 free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
5200 free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
5207 smb3_receive_transform(struct TCP_Server_Info *server,
5208 struct mid_q_entry **mids, char **bufs, int *num_mids)
5210 char *buf = server->smallbuf;
5211 unsigned int pdu_length = server->pdu_size;
5212 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
5213 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
5215 if (pdu_length < sizeof(struct smb2_transform_hdr) +
5216 sizeof(struct smb2_sync_hdr)) {
5217 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
5219 cifs_reconnect(server);
5220 return -ECONNABORTED;
5223 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
5224 cifs_server_dbg(VFS, "Transform message is broken\n");
5225 cifs_reconnect(server);
5226 return -ECONNABORTED;
5229 /* TODO: add support for compounds containing READ. */
5230 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
5231 return receive_encrypted_read(server, &mids[0], num_mids);
5234 return receive_encrypted_standard(server, mids, bufs, num_mids);
5238 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
5240 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
5242 return handle_read_data(server, mid, buf, server->pdu_size,
5247 smb2_next_header(char *buf)
5249 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
5250 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
5252 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
5253 return sizeof(struct smb2_transform_hdr) +
5254 le32_to_cpu(t_hdr->OriginalMessageSize);
5256 return le32_to_cpu(hdr->NextCommand);
5260 smb2_make_node(unsigned int xid, struct inode *inode,
5261 struct dentry *dentry, struct cifs_tcon *tcon,
5262 const char *full_path, umode_t mode, dev_t dev)
5264 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
5266 FILE_ALL_INFO *buf = NULL;
5267 struct cifs_io_parms io_parms = {0};
5269 struct cifs_fid fid;
5270 struct cifs_open_parms oparms;
5271 unsigned int bytes_written;
5272 struct win_dev *pdev;
5276 * Check if mounted with mount parm 'sfu' mount parm.
5277 * SFU emulation should work with all servers, but only
5278 * supports block and char device (no socket & fifo),
5279 * and was used by default in earlier versions of Windows
5281 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
5285 * TODO: Add ability to create instead via reparse point. Windows (e.g.
5286 * their current NFS server) uses this approach to expose special files
5287 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
5290 if (!S_ISCHR(mode) && !S_ISBLK(mode))
5293 cifs_dbg(FYI, "sfu compat create special file\n");
5295 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
5302 oparms.cifs_sb = cifs_sb;
5303 oparms.desired_access = GENERIC_WRITE;
5304 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
5305 CREATE_OPTION_SPECIAL);
5306 oparms.disposition = FILE_CREATE;
5307 oparms.path = full_path;
5309 oparms.reconnect = false;
5311 if (tcon->ses->server->oplocks)
5312 oplock = REQ_OPLOCK;
5315 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
5320 * BB Do not bother to decode buf since no local inode yet to put
5321 * timestamps in, but we can reuse it safely.
5324 pdev = (struct win_dev *)buf;
5325 io_parms.pid = current->tgid;
5326 io_parms.tcon = tcon;
5327 io_parms.offset = 0;
5328 io_parms.length = sizeof(struct win_dev);
5329 iov[1].iov_base = buf;
5330 iov[1].iov_len = sizeof(struct win_dev);
5331 if (S_ISCHR(mode)) {
5332 memcpy(pdev->type, "IntxCHR", 8);
5333 pdev->major = cpu_to_le64(MAJOR(dev));
5334 pdev->minor = cpu_to_le64(MINOR(dev));
5335 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
5336 &bytes_written, iov, 1);
5337 } else if (S_ISBLK(mode)) {
5338 memcpy(pdev->type, "IntxBLK", 8);
5339 pdev->major = cpu_to_le64(MAJOR(dev));
5340 pdev->minor = cpu_to_le64(MINOR(dev));
5341 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
5342 &bytes_written, iov, 1);
5344 tcon->ses->server->ops->close(xid, tcon, &fid);
5347 /* FIXME: add code here to set EAs */
5354 struct smb_version_operations smb20_operations = {
5355 .compare_fids = smb2_compare_fids,
5356 .setup_request = smb2_setup_request,
5357 .setup_async_request = smb2_setup_async_request,
5358 .check_receive = smb2_check_receive,
5359 .add_credits = smb2_add_credits,
5360 .set_credits = smb2_set_credits,
5361 .get_credits_field = smb2_get_credits_field,
5362 .get_credits = smb2_get_credits,
5363 .wait_mtu_credits = cifs_wait_mtu_credits,
5364 .get_next_mid = smb2_get_next_mid,
5365 .revert_current_mid = smb2_revert_current_mid,
5366 .read_data_offset = smb2_read_data_offset,
5367 .read_data_length = smb2_read_data_length,
5368 .map_error = map_smb2_to_linux_error,
5369 .find_mid = smb2_find_mid,
5370 .check_message = smb2_check_message,
5371 .dump_detail = smb2_dump_detail,
5372 .clear_stats = smb2_clear_stats,
5373 .print_stats = smb2_print_stats,
5374 .is_oplock_break = smb2_is_valid_oplock_break,
5375 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5376 .downgrade_oplock = smb2_downgrade_oplock,
5377 .need_neg = smb2_need_neg,
5378 .negotiate = smb2_negotiate,
5379 .negotiate_wsize = smb2_negotiate_wsize,
5380 .negotiate_rsize = smb2_negotiate_rsize,
5381 .sess_setup = SMB2_sess_setup,
5382 .logoff = SMB2_logoff,
5383 .tree_connect = SMB2_tcon,
5384 .tree_disconnect = SMB2_tdis,
5385 .qfs_tcon = smb2_qfs_tcon,
5386 .is_path_accessible = smb2_is_path_accessible,
5387 .can_echo = smb2_can_echo,
5389 .query_path_info = smb2_query_path_info,
5390 .get_srv_inum = smb2_get_srv_inum,
5391 .query_file_info = smb2_query_file_info,
5392 .set_path_size = smb2_set_path_size,
5393 .set_file_size = smb2_set_file_size,
5394 .set_file_info = smb2_set_file_info,
5395 .set_compression = smb2_set_compression,
5396 .mkdir = smb2_mkdir,
5397 .mkdir_setinfo = smb2_mkdir_setinfo,
5398 .rmdir = smb2_rmdir,
5399 .unlink = smb2_unlink,
5400 .rename = smb2_rename_path,
5401 .create_hardlink = smb2_create_hardlink,
5402 .query_symlink = smb2_query_symlink,
5403 .query_mf_symlink = smb3_query_mf_symlink,
5404 .create_mf_symlink = smb3_create_mf_symlink,
5405 .open = smb2_open_file,
5406 .set_fid = smb2_set_fid,
5407 .close = smb2_close_file,
5408 .flush = smb2_flush_file,
5409 .async_readv = smb2_async_readv,
5410 .async_writev = smb2_async_writev,
5411 .sync_read = smb2_sync_read,
5412 .sync_write = smb2_sync_write,
5413 .query_dir_first = smb2_query_dir_first,
5414 .query_dir_next = smb2_query_dir_next,
5415 .close_dir = smb2_close_dir,
5416 .calc_smb_size = smb2_calc_size,
5417 .is_status_pending = smb2_is_status_pending,
5418 .is_session_expired = smb2_is_session_expired,
5419 .oplock_response = smb2_oplock_response,
5420 .queryfs = smb2_queryfs,
5421 .mand_lock = smb2_mand_lock,
5422 .mand_unlock_range = smb2_unlock_range,
5423 .push_mand_locks = smb2_push_mandatory_locks,
5424 .get_lease_key = smb2_get_lease_key,
5425 .set_lease_key = smb2_set_lease_key,
5426 .new_lease_key = smb2_new_lease_key,
5427 .calc_signature = smb2_calc_signature,
5428 .is_read_op = smb2_is_read_op,
5429 .set_oplock_level = smb2_set_oplock_level,
5430 .create_lease_buf = smb2_create_lease_buf,
5431 .parse_lease_buf = smb2_parse_lease_buf,
5432 .copychunk_range = smb2_copychunk_range,
5433 .wp_retry_size = smb2_wp_retry_size,
5434 .dir_needs_close = smb2_dir_needs_close,
5435 .get_dfs_refer = smb2_get_dfs_refer,
5436 .select_sectype = smb2_select_sectype,
5437 #ifdef CONFIG_CIFS_XATTR
5438 .query_all_EAs = smb2_query_eas,
5439 .set_EA = smb2_set_ea,
5440 #endif /* CIFS_XATTR */
5441 .get_acl = get_smb2_acl,
5442 .get_acl_by_fid = get_smb2_acl_by_fid,
5443 .set_acl = set_smb2_acl,
5444 .next_header = smb2_next_header,
5445 .ioctl_query_info = smb2_ioctl_query_info,
5446 .make_node = smb2_make_node,
5447 .fiemap = smb3_fiemap,
5448 .llseek = smb3_llseek,
5449 .is_status_io_timeout = smb2_is_status_io_timeout,
5450 .is_network_name_deleted = smb2_is_network_name_deleted,
5453 struct smb_version_operations smb21_operations = {
5454 .compare_fids = smb2_compare_fids,
5455 .setup_request = smb2_setup_request,
5456 .setup_async_request = smb2_setup_async_request,
5457 .check_receive = smb2_check_receive,
5458 .add_credits = smb2_add_credits,
5459 .set_credits = smb2_set_credits,
5460 .get_credits_field = smb2_get_credits_field,
5461 .get_credits = smb2_get_credits,
5462 .wait_mtu_credits = smb2_wait_mtu_credits,
5463 .adjust_credits = smb2_adjust_credits,
5464 .get_next_mid = smb2_get_next_mid,
5465 .revert_current_mid = smb2_revert_current_mid,
5466 .read_data_offset = smb2_read_data_offset,
5467 .read_data_length = smb2_read_data_length,
5468 .map_error = map_smb2_to_linux_error,
5469 .find_mid = smb2_find_mid,
5470 .check_message = smb2_check_message,
5471 .dump_detail = smb2_dump_detail,
5472 .clear_stats = smb2_clear_stats,
5473 .print_stats = smb2_print_stats,
5474 .is_oplock_break = smb2_is_valid_oplock_break,
5475 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5476 .downgrade_oplock = smb2_downgrade_oplock,
5477 .need_neg = smb2_need_neg,
5478 .negotiate = smb2_negotiate,
5479 .negotiate_wsize = smb2_negotiate_wsize,
5480 .negotiate_rsize = smb2_negotiate_rsize,
5481 .sess_setup = SMB2_sess_setup,
5482 .logoff = SMB2_logoff,
5483 .tree_connect = SMB2_tcon,
5484 .tree_disconnect = SMB2_tdis,
5485 .qfs_tcon = smb2_qfs_tcon,
5486 .is_path_accessible = smb2_is_path_accessible,
5487 .can_echo = smb2_can_echo,
5489 .query_path_info = smb2_query_path_info,
5490 .get_srv_inum = smb2_get_srv_inum,
5491 .query_file_info = smb2_query_file_info,
5492 .set_path_size = smb2_set_path_size,
5493 .set_file_size = smb2_set_file_size,
5494 .set_file_info = smb2_set_file_info,
5495 .set_compression = smb2_set_compression,
5496 .mkdir = smb2_mkdir,
5497 .mkdir_setinfo = smb2_mkdir_setinfo,
5498 .rmdir = smb2_rmdir,
5499 .unlink = smb2_unlink,
5500 .rename = smb2_rename_path,
5501 .create_hardlink = smb2_create_hardlink,
5502 .query_symlink = smb2_query_symlink,
5503 .query_mf_symlink = smb3_query_mf_symlink,
5504 .create_mf_symlink = smb3_create_mf_symlink,
5505 .open = smb2_open_file,
5506 .set_fid = smb2_set_fid,
5507 .close = smb2_close_file,
5508 .flush = smb2_flush_file,
5509 .async_readv = smb2_async_readv,
5510 .async_writev = smb2_async_writev,
5511 .sync_read = smb2_sync_read,
5512 .sync_write = smb2_sync_write,
5513 .query_dir_first = smb2_query_dir_first,
5514 .query_dir_next = smb2_query_dir_next,
5515 .close_dir = smb2_close_dir,
5516 .calc_smb_size = smb2_calc_size,
5517 .is_status_pending = smb2_is_status_pending,
5518 .is_session_expired = smb2_is_session_expired,
5519 .oplock_response = smb2_oplock_response,
5520 .queryfs = smb2_queryfs,
5521 .mand_lock = smb2_mand_lock,
5522 .mand_unlock_range = smb2_unlock_range,
5523 .push_mand_locks = smb2_push_mandatory_locks,
5524 .get_lease_key = smb2_get_lease_key,
5525 .set_lease_key = smb2_set_lease_key,
5526 .new_lease_key = smb2_new_lease_key,
5527 .calc_signature = smb2_calc_signature,
5528 .is_read_op = smb21_is_read_op,
5529 .set_oplock_level = smb21_set_oplock_level,
5530 .create_lease_buf = smb2_create_lease_buf,
5531 .parse_lease_buf = smb2_parse_lease_buf,
5532 .copychunk_range = smb2_copychunk_range,
5533 .wp_retry_size = smb2_wp_retry_size,
5534 .dir_needs_close = smb2_dir_needs_close,
5535 .enum_snapshots = smb3_enum_snapshots,
5536 .notify = smb3_notify,
5537 .get_dfs_refer = smb2_get_dfs_refer,
5538 .select_sectype = smb2_select_sectype,
5539 #ifdef CONFIG_CIFS_XATTR
5540 .query_all_EAs = smb2_query_eas,
5541 .set_EA = smb2_set_ea,
5542 #endif /* CIFS_XATTR */
5543 .get_acl = get_smb2_acl,
5544 .get_acl_by_fid = get_smb2_acl_by_fid,
5545 .set_acl = set_smb2_acl,
5546 .next_header = smb2_next_header,
5547 .ioctl_query_info = smb2_ioctl_query_info,
5548 .make_node = smb2_make_node,
5549 .fiemap = smb3_fiemap,
5550 .llseek = smb3_llseek,
5551 .is_status_io_timeout = smb2_is_status_io_timeout,
5552 .is_network_name_deleted = smb2_is_network_name_deleted,
5555 struct smb_version_operations smb30_operations = {
5556 .compare_fids = smb2_compare_fids,
5557 .setup_request = smb2_setup_request,
5558 .setup_async_request = smb2_setup_async_request,
5559 .check_receive = smb2_check_receive,
5560 .add_credits = smb2_add_credits,
5561 .set_credits = smb2_set_credits,
5562 .get_credits_field = smb2_get_credits_field,
5563 .get_credits = smb2_get_credits,
5564 .wait_mtu_credits = smb2_wait_mtu_credits,
5565 .adjust_credits = smb2_adjust_credits,
5566 .get_next_mid = smb2_get_next_mid,
5567 .revert_current_mid = smb2_revert_current_mid,
5568 .read_data_offset = smb2_read_data_offset,
5569 .read_data_length = smb2_read_data_length,
5570 .map_error = map_smb2_to_linux_error,
5571 .find_mid = smb2_find_mid,
5572 .check_message = smb2_check_message,
5573 .dump_detail = smb2_dump_detail,
5574 .clear_stats = smb2_clear_stats,
5575 .print_stats = smb2_print_stats,
5576 .dump_share_caps = smb2_dump_share_caps,
5577 .is_oplock_break = smb2_is_valid_oplock_break,
5578 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5579 .downgrade_oplock = smb3_downgrade_oplock,
5580 .need_neg = smb2_need_neg,
5581 .negotiate = smb2_negotiate,
5582 .negotiate_wsize = smb3_negotiate_wsize,
5583 .negotiate_rsize = smb3_negotiate_rsize,
5584 .sess_setup = SMB2_sess_setup,
5585 .logoff = SMB2_logoff,
5586 .tree_connect = SMB2_tcon,
5587 .tree_disconnect = SMB2_tdis,
5588 .qfs_tcon = smb3_qfs_tcon,
5589 .is_path_accessible = smb2_is_path_accessible,
5590 .can_echo = smb2_can_echo,
5592 .query_path_info = smb2_query_path_info,
5593 /* WSL tags introduced long after smb2.1, enable for SMB3, 3.11 only */
5594 .query_reparse_tag = smb2_query_reparse_tag,
5595 .get_srv_inum = smb2_get_srv_inum,
5596 .query_file_info = smb2_query_file_info,
5597 .set_path_size = smb2_set_path_size,
5598 .set_file_size = smb2_set_file_size,
5599 .set_file_info = smb2_set_file_info,
5600 .set_compression = smb2_set_compression,
5601 .mkdir = smb2_mkdir,
5602 .mkdir_setinfo = smb2_mkdir_setinfo,
5603 .rmdir = smb2_rmdir,
5604 .unlink = smb2_unlink,
5605 .rename = smb2_rename_path,
5606 .create_hardlink = smb2_create_hardlink,
5607 .query_symlink = smb2_query_symlink,
5608 .query_mf_symlink = smb3_query_mf_symlink,
5609 .create_mf_symlink = smb3_create_mf_symlink,
5610 .open = smb2_open_file,
5611 .set_fid = smb2_set_fid,
5612 .close = smb2_close_file,
5613 .close_getattr = smb2_close_getattr,
5614 .flush = smb2_flush_file,
5615 .async_readv = smb2_async_readv,
5616 .async_writev = smb2_async_writev,
5617 .sync_read = smb2_sync_read,
5618 .sync_write = smb2_sync_write,
5619 .query_dir_first = smb2_query_dir_first,
5620 .query_dir_next = smb2_query_dir_next,
5621 .close_dir = smb2_close_dir,
5622 .calc_smb_size = smb2_calc_size,
5623 .is_status_pending = smb2_is_status_pending,
5624 .is_session_expired = smb2_is_session_expired,
5625 .oplock_response = smb2_oplock_response,
5626 .queryfs = smb2_queryfs,
5627 .mand_lock = smb2_mand_lock,
5628 .mand_unlock_range = smb2_unlock_range,
5629 .push_mand_locks = smb2_push_mandatory_locks,
5630 .get_lease_key = smb2_get_lease_key,
5631 .set_lease_key = smb2_set_lease_key,
5632 .new_lease_key = smb2_new_lease_key,
5633 .generate_signingkey = generate_smb30signingkey,
5634 .calc_signature = smb3_calc_signature,
5635 .set_integrity = smb3_set_integrity,
5636 .is_read_op = smb21_is_read_op,
5637 .set_oplock_level = smb3_set_oplock_level,
5638 .create_lease_buf = smb3_create_lease_buf,
5639 .parse_lease_buf = smb3_parse_lease_buf,
5640 .copychunk_range = smb2_copychunk_range,
5641 .duplicate_extents = smb2_duplicate_extents,
5642 .validate_negotiate = smb3_validate_negotiate,
5643 .wp_retry_size = smb2_wp_retry_size,
5644 .dir_needs_close = smb2_dir_needs_close,
5645 .fallocate = smb3_fallocate,
5646 .enum_snapshots = smb3_enum_snapshots,
5647 .notify = smb3_notify,
5648 .init_transform_rq = smb3_init_transform_rq,
5649 .is_transform_hdr = smb3_is_transform_hdr,
5650 .receive_transform = smb3_receive_transform,
5651 .get_dfs_refer = smb2_get_dfs_refer,
5652 .select_sectype = smb2_select_sectype,
5653 #ifdef CONFIG_CIFS_XATTR
5654 .query_all_EAs = smb2_query_eas,
5655 .set_EA = smb2_set_ea,
5656 #endif /* CIFS_XATTR */
5657 .get_acl = get_smb2_acl,
5658 .get_acl_by_fid = get_smb2_acl_by_fid,
5659 .set_acl = set_smb2_acl,
5660 .next_header = smb2_next_header,
5661 .ioctl_query_info = smb2_ioctl_query_info,
5662 .make_node = smb2_make_node,
5663 .fiemap = smb3_fiemap,
5664 .llseek = smb3_llseek,
5665 .is_status_io_timeout = smb2_is_status_io_timeout,
5666 .is_network_name_deleted = smb2_is_network_name_deleted,
5669 struct smb_version_operations smb311_operations = {
5670 .compare_fids = smb2_compare_fids,
5671 .setup_request = smb2_setup_request,
5672 .setup_async_request = smb2_setup_async_request,
5673 .check_receive = smb2_check_receive,
5674 .add_credits = smb2_add_credits,
5675 .set_credits = smb2_set_credits,
5676 .get_credits_field = smb2_get_credits_field,
5677 .get_credits = smb2_get_credits,
5678 .wait_mtu_credits = smb2_wait_mtu_credits,
5679 .adjust_credits = smb2_adjust_credits,
5680 .get_next_mid = smb2_get_next_mid,
5681 .revert_current_mid = smb2_revert_current_mid,
5682 .read_data_offset = smb2_read_data_offset,
5683 .read_data_length = smb2_read_data_length,
5684 .map_error = map_smb2_to_linux_error,
5685 .find_mid = smb2_find_mid,
5686 .check_message = smb2_check_message,
5687 .dump_detail = smb2_dump_detail,
5688 .clear_stats = smb2_clear_stats,
5689 .print_stats = smb2_print_stats,
5690 .dump_share_caps = smb2_dump_share_caps,
5691 .is_oplock_break = smb2_is_valid_oplock_break,
5692 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5693 .downgrade_oplock = smb3_downgrade_oplock,
5694 .need_neg = smb2_need_neg,
5695 .negotiate = smb2_negotiate,
5696 .negotiate_wsize = smb3_negotiate_wsize,
5697 .negotiate_rsize = smb3_negotiate_rsize,
5698 .sess_setup = SMB2_sess_setup,
5699 .logoff = SMB2_logoff,
5700 .tree_connect = SMB2_tcon,
5701 .tree_disconnect = SMB2_tdis,
5702 .qfs_tcon = smb3_qfs_tcon,
5703 .is_path_accessible = smb2_is_path_accessible,
5704 .can_echo = smb2_can_echo,
5706 .query_path_info = smb2_query_path_info,
5707 .query_reparse_tag = smb2_query_reparse_tag,
5708 .get_srv_inum = smb2_get_srv_inum,
5709 .query_file_info = smb2_query_file_info,
5710 .set_path_size = smb2_set_path_size,
5711 .set_file_size = smb2_set_file_size,
5712 .set_file_info = smb2_set_file_info,
5713 .set_compression = smb2_set_compression,
5714 .mkdir = smb2_mkdir,
5715 .mkdir_setinfo = smb2_mkdir_setinfo,
5716 .posix_mkdir = smb311_posix_mkdir,
5717 .rmdir = smb2_rmdir,
5718 .unlink = smb2_unlink,
5719 .rename = smb2_rename_path,
5720 .create_hardlink = smb2_create_hardlink,
5721 .query_symlink = smb2_query_symlink,
5722 .query_mf_symlink = smb3_query_mf_symlink,
5723 .create_mf_symlink = smb3_create_mf_symlink,
5724 .open = smb2_open_file,
5725 .set_fid = smb2_set_fid,
5726 .close = smb2_close_file,
5727 .close_getattr = smb2_close_getattr,
5728 .flush = smb2_flush_file,
5729 .async_readv = smb2_async_readv,
5730 .async_writev = smb2_async_writev,
5731 .sync_read = smb2_sync_read,
5732 .sync_write = smb2_sync_write,
5733 .query_dir_first = smb2_query_dir_first,
5734 .query_dir_next = smb2_query_dir_next,
5735 .close_dir = smb2_close_dir,
5736 .calc_smb_size = smb2_calc_size,
5737 .is_status_pending = smb2_is_status_pending,
5738 .is_session_expired = smb2_is_session_expired,
5739 .oplock_response = smb2_oplock_response,
5740 .queryfs = smb311_queryfs,
5741 .mand_lock = smb2_mand_lock,
5742 .mand_unlock_range = smb2_unlock_range,
5743 .push_mand_locks = smb2_push_mandatory_locks,
5744 .get_lease_key = smb2_get_lease_key,
5745 .set_lease_key = smb2_set_lease_key,
5746 .new_lease_key = smb2_new_lease_key,
5747 .generate_signingkey = generate_smb311signingkey,
5748 .calc_signature = smb3_calc_signature,
5749 .set_integrity = smb3_set_integrity,
5750 .is_read_op = smb21_is_read_op,
5751 .set_oplock_level = smb3_set_oplock_level,
5752 .create_lease_buf = smb3_create_lease_buf,
5753 .parse_lease_buf = smb3_parse_lease_buf,
5754 .copychunk_range = smb2_copychunk_range,
5755 .duplicate_extents = smb2_duplicate_extents,
5756 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5757 .wp_retry_size = smb2_wp_retry_size,
5758 .dir_needs_close = smb2_dir_needs_close,
5759 .fallocate = smb3_fallocate,
5760 .enum_snapshots = smb3_enum_snapshots,
5761 .notify = smb3_notify,
5762 .init_transform_rq = smb3_init_transform_rq,
5763 .is_transform_hdr = smb3_is_transform_hdr,
5764 .receive_transform = smb3_receive_transform,
5765 .get_dfs_refer = smb2_get_dfs_refer,
5766 .select_sectype = smb2_select_sectype,
5767 #ifdef CONFIG_CIFS_XATTR
5768 .query_all_EAs = smb2_query_eas,
5769 .set_EA = smb2_set_ea,
5770 #endif /* CIFS_XATTR */
5771 .get_acl = get_smb2_acl,
5772 .get_acl_by_fid = get_smb2_acl_by_fid,
5773 .set_acl = set_smb2_acl,
5774 .next_header = smb2_next_header,
5775 .ioctl_query_info = smb2_ioctl_query_info,
5776 .make_node = smb2_make_node,
5777 .fiemap = smb3_fiemap,
5778 .llseek = smb3_llseek,
5779 .is_status_io_timeout = smb2_is_status_io_timeout,
5780 .is_network_name_deleted = smb2_is_network_name_deleted,
5783 struct smb_version_values smb20_values = {
5784 .version_string = SMB20_VERSION_STRING,
5785 .protocol_id = SMB20_PROT_ID,
5786 .req_capabilities = 0, /* MBZ */
5787 .large_lock_type = 0,
5788 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5789 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5790 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5791 .header_size = sizeof(struct smb2_sync_hdr),
5792 .header_preamble_size = 0,
5793 .max_header_size = MAX_SMB2_HDR_SIZE,
5794 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5795 .lock_cmd = SMB2_LOCK,
5797 .cap_nt_find = SMB2_NT_FIND,
5798 .cap_large_files = SMB2_LARGE_FILES,
5799 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5800 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5801 .create_lease_size = sizeof(struct create_lease),
5804 struct smb_version_values smb21_values = {
5805 .version_string = SMB21_VERSION_STRING,
5806 .protocol_id = SMB21_PROT_ID,
5807 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5808 .large_lock_type = 0,
5809 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5810 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5811 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5812 .header_size = sizeof(struct smb2_sync_hdr),
5813 .header_preamble_size = 0,
5814 .max_header_size = MAX_SMB2_HDR_SIZE,
5815 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5816 .lock_cmd = SMB2_LOCK,
5818 .cap_nt_find = SMB2_NT_FIND,
5819 .cap_large_files = SMB2_LARGE_FILES,
5820 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5821 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5822 .create_lease_size = sizeof(struct create_lease),
5825 struct smb_version_values smb3any_values = {
5826 .version_string = SMB3ANY_VERSION_STRING,
5827 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5828 .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,
5829 .large_lock_type = 0,
5830 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5831 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5832 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5833 .header_size = sizeof(struct smb2_sync_hdr),
5834 .header_preamble_size = 0,
5835 .max_header_size = MAX_SMB2_HDR_SIZE,
5836 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5837 .lock_cmd = SMB2_LOCK,
5839 .cap_nt_find = SMB2_NT_FIND,
5840 .cap_large_files = SMB2_LARGE_FILES,
5841 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5842 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5843 .create_lease_size = sizeof(struct create_lease_v2),
5846 struct smb_version_values smbdefault_values = {
5847 .version_string = SMBDEFAULT_VERSION_STRING,
5848 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5849 .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,
5850 .large_lock_type = 0,
5851 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5852 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5853 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5854 .header_size = sizeof(struct smb2_sync_hdr),
5855 .header_preamble_size = 0,
5856 .max_header_size = MAX_SMB2_HDR_SIZE,
5857 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5858 .lock_cmd = SMB2_LOCK,
5860 .cap_nt_find = SMB2_NT_FIND,
5861 .cap_large_files = SMB2_LARGE_FILES,
5862 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5863 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5864 .create_lease_size = sizeof(struct create_lease_v2),
5867 struct smb_version_values smb30_values = {
5868 .version_string = SMB30_VERSION_STRING,
5869 .protocol_id = SMB30_PROT_ID,
5870 .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,
5871 .large_lock_type = 0,
5872 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5873 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5874 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5875 .header_size = sizeof(struct smb2_sync_hdr),
5876 .header_preamble_size = 0,
5877 .max_header_size = MAX_SMB2_HDR_SIZE,
5878 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5879 .lock_cmd = SMB2_LOCK,
5881 .cap_nt_find = SMB2_NT_FIND,
5882 .cap_large_files = SMB2_LARGE_FILES,
5883 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5884 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5885 .create_lease_size = sizeof(struct create_lease_v2),
5888 struct smb_version_values smb302_values = {
5889 .version_string = SMB302_VERSION_STRING,
5890 .protocol_id = SMB302_PROT_ID,
5891 .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,
5892 .large_lock_type = 0,
5893 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5894 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5895 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5896 .header_size = sizeof(struct smb2_sync_hdr),
5897 .header_preamble_size = 0,
5898 .max_header_size = MAX_SMB2_HDR_SIZE,
5899 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5900 .lock_cmd = SMB2_LOCK,
5902 .cap_nt_find = SMB2_NT_FIND,
5903 .cap_large_files = SMB2_LARGE_FILES,
5904 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5905 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5906 .create_lease_size = sizeof(struct create_lease_v2),
5909 struct smb_version_values smb311_values = {
5910 .version_string = SMB311_VERSION_STRING,
5911 .protocol_id = SMB311_PROT_ID,
5912 .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,
5913 .large_lock_type = 0,
5914 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5915 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5916 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5917 .header_size = sizeof(struct smb2_sync_hdr),
5918 .header_preamble_size = 0,
5919 .max_header_size = MAX_SMB2_HDR_SIZE,
5920 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5921 .lock_cmd = SMB2_LOCK,
5923 .cap_nt_find = SMB2_NT_FIND,
5924 .cap_large_files = SMB2_LARGE_FILES,
5925 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5926 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5927 .create_lease_size = sizeof(struct create_lease_v2),