2 * SMB2 version specific operations
4 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License v2 as published
8 * by the Free Software Foundation.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/pagemap.h>
21 #include <linux/vfs.h>
22 #include <linux/falloc.h>
23 #include <linux/scatterlist.h>
24 #include <linux/uuid.h>
25 #include <crypto/aead.h>
28 #include "smb2proto.h"
29 #include "cifsproto.h"
30 #include "cifs_debug.h"
31 #include "cifs_unicode.h"
32 #include "smb2status.h"
34 #include "cifs_ioctl.h"
35 #include "smbdirect.h"
38 change_conf(struct TCP_Server_Info *server)
40 server->credits += server->echo_credits + server->oplock_credits;
41 server->oplock_credits = server->echo_credits = 0;
42 switch (server->credits) {
46 server->echoes = false;
47 server->oplocks = false;
48 cifs_dbg(VFS, "disabling echoes and oplocks\n");
51 server->echoes = true;
52 server->oplocks = false;
53 server->echo_credits = 1;
54 cifs_dbg(FYI, "disabling oplocks\n");
57 server->echoes = true;
59 server->oplocks = true;
60 server->oplock_credits = 1;
62 server->oplocks = false;
64 server->echo_credits = 1;
66 server->credits -= server->echo_credits + server->oplock_credits;
71 smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
75 spin_lock(&server->req_lock);
76 val = server->ops->get_credits_field(server, optype);
79 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
80 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
83 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
84 rc = change_conf(server);
86 * Sometimes server returns 0 credits on oplock break ack - we need to
87 * rebalance credits in this case.
89 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
91 if (server->credits > 1) {
93 server->oplock_credits++;
96 spin_unlock(&server->req_lock);
97 wake_up(&server->request_q);
99 cifs_reconnect(server);
103 smb2_set_credits(struct TCP_Server_Info *server, const int val)
105 spin_lock(&server->req_lock);
106 server->credits = val;
107 spin_unlock(&server->req_lock);
111 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
115 return &server->echo_credits;
117 return &server->oplock_credits;
119 return &server->credits;
124 smb2_get_credits(struct mid_q_entry *mid)
126 struct smb2_sync_hdr *shdr = get_sync_hdr(mid->resp_buf);
128 return le16_to_cpu(shdr->CreditRequest);
132 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
133 unsigned int *num, unsigned int *credits)
136 unsigned int scredits;
138 spin_lock(&server->req_lock);
140 if (server->credits <= 0) {
141 spin_unlock(&server->req_lock);
142 cifs_num_waiters_inc(server);
143 rc = wait_event_killable(server->request_q,
144 has_credits(server, &server->credits));
145 cifs_num_waiters_dec(server);
148 spin_lock(&server->req_lock);
150 if (server->tcpStatus == CifsExiting) {
151 spin_unlock(&server->req_lock);
155 scredits = server->credits;
156 /* can deadlock with reopen */
158 *num = SMB2_MAX_BUFFER_SIZE;
163 /* leave one credit for a possible reopen */
165 *num = min_t(unsigned int, size,
166 scredits * SMB2_MAX_BUFFER_SIZE);
168 *credits = DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
169 server->credits -= *credits;
174 spin_unlock(&server->req_lock);
179 smb2_get_next_mid(struct TCP_Server_Info *server)
182 /* for SMB2 we need the current value */
183 spin_lock(&GlobalMid_Lock);
184 mid = server->CurrentMid++;
185 spin_unlock(&GlobalMid_Lock);
189 static struct mid_q_entry *
190 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
192 struct mid_q_entry *mid;
193 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
194 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
196 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
197 cifs_dbg(VFS, "encrypted frame parsing not supported yet");
201 spin_lock(&GlobalMid_Lock);
202 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
203 if ((mid->mid == wire_mid) &&
204 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
205 (mid->command == shdr->Command)) {
206 spin_unlock(&GlobalMid_Lock);
210 spin_unlock(&GlobalMid_Lock);
215 smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
217 #ifdef CONFIG_CIFS_DEBUG2
218 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
220 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
221 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
223 cifs_dbg(VFS, "smb buf %p len %u\n", buf,
224 server->ops->calc_smb_size(buf, server));
229 smb2_need_neg(struct TCP_Server_Info *server)
231 return server->max_read == 0;
235 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
238 ses->server->CurrentMid = 0;
239 rc = SMB2_negotiate(xid, ses);
240 /* BB we probably don't need to retry with modern servers */
247 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
249 struct TCP_Server_Info *server = tcon->ses->server;
252 /* start with specified wsize, or default */
253 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
254 wsize = min_t(unsigned int, wsize, server->max_write);
255 #ifdef CONFIG_CIFS_SMB_DIRECT
258 wsize = min_t(unsigned int,
259 wsize, server->smbd_conn->max_fragmented_send_size);
261 wsize = min_t(unsigned int,
262 wsize, server->smbd_conn->max_readwrite_size);
265 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
266 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
272 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
274 struct TCP_Server_Info *server = tcon->ses->server;
277 /* start with specified rsize, or default */
278 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
279 rsize = min_t(unsigned int, rsize, server->max_read);
280 #ifdef CONFIG_CIFS_SMB_DIRECT
283 rsize = min_t(unsigned int,
284 rsize, server->smbd_conn->max_fragmented_recv_size);
286 rsize = min_t(unsigned int,
287 rsize, server->smbd_conn->max_readwrite_size);
291 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
292 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
297 #ifdef CONFIG_CIFS_STATS2
299 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
302 unsigned int ret_data_len = 0;
303 struct network_interface_info_ioctl_rsp *out_buf;
305 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
306 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
307 NULL /* no data input */, 0 /* no data input */,
308 (char **)&out_buf, &ret_data_len);
310 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
311 else if (ret_data_len < sizeof(struct network_interface_info_ioctl_rsp)) {
312 cifs_dbg(VFS, "server returned bad net interface info buf\n");
315 /* Dump info on first interface */
316 cifs_dbg(FYI, "Adapter Capability 0x%x\t",
317 le32_to_cpu(out_buf->Capability));
318 cifs_dbg(FYI, "Link Speed %lld\n",
319 le64_to_cpu(out_buf->LinkSpeed));
327 * Open the directory at the root of a share
329 int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
331 struct cifs_open_parms oparams;
333 __le16 srch_path = 0; /* Null - since an open of top of share */
334 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
336 mutex_lock(&tcon->prfid_mutex);
337 if (tcon->valid_root_fid) {
338 cifs_dbg(FYI, "found a cached root file handle\n");
339 memcpy(pfid, tcon->prfid, sizeof(struct cifs_fid));
340 mutex_unlock(&tcon->prfid_mutex);
345 oparams.create_options = 0;
346 oparams.desired_access = FILE_READ_ATTRIBUTES;
347 oparams.disposition = FILE_OPEN;
349 oparams.reconnect = false;
351 rc = SMB2_open(xid, &oparams, &srch_path, &oplock, NULL, NULL);
353 memcpy(tcon->prfid, pfid, sizeof(struct cifs_fid));
354 tcon->valid_root_fid = true;
356 mutex_unlock(&tcon->prfid_mutex);
361 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
364 __le16 srch_path = 0; /* Null - open root of share */
365 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
366 struct cifs_open_parms oparms;
368 bool no_cached_open = tcon->nohandlecache;
371 oparms.desired_access = FILE_READ_ATTRIBUTES;
372 oparms.disposition = FILE_OPEN;
373 oparms.create_options = 0;
375 oparms.reconnect = false;
378 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
380 rc = open_shroot(xid, tcon, &fid);
385 #ifdef CONFIG_CIFS_STATS2
386 SMB3_request_interfaces(xid, tcon);
389 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
390 FS_ATTRIBUTE_INFORMATION);
391 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
392 FS_DEVICE_INFORMATION);
393 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
394 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
396 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
401 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
404 __le16 srch_path = 0; /* Null - open root of share */
405 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
406 struct cifs_open_parms oparms;
410 oparms.desired_access = FILE_READ_ATTRIBUTES;
411 oparms.disposition = FILE_OPEN;
412 oparms.create_options = 0;
414 oparms.reconnect = false;
416 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
420 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
421 FS_ATTRIBUTE_INFORMATION);
422 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
423 FS_DEVICE_INFORMATION);
424 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
429 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
430 struct cifs_sb_info *cifs_sb, const char *full_path)
434 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
435 struct cifs_open_parms oparms;
438 if ((*full_path == 0) && tcon->valid_root_fid)
441 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
446 oparms.desired_access = FILE_READ_ATTRIBUTES;
447 oparms.disposition = FILE_OPEN;
448 oparms.create_options = 0;
450 oparms.reconnect = false;
452 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
458 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
464 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
465 struct cifs_sb_info *cifs_sb, const char *full_path,
466 u64 *uniqueid, FILE_ALL_INFO *data)
468 *uniqueid = le64_to_cpu(data->IndexNumber);
473 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
474 struct cifs_fid *fid, FILE_ALL_INFO *data)
477 struct smb2_file_all_info *smb2_data;
479 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
481 if (smb2_data == NULL)
484 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
487 move_smb2_info_to_cifs(data, smb2_data);
492 #ifdef CONFIG_CIFS_XATTR
494 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
495 struct smb2_file_full_ea_info *src, size_t src_size,
496 const unsigned char *ea_name)
499 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
501 size_t name_len, value_len, user_name_len;
503 while (src_size > 0) {
504 name = &src->ea_data[0];
505 name_len = (size_t)src->ea_name_length;
506 value = &src->ea_data[src->ea_name_length + 1];
507 value_len = (size_t)le16_to_cpu(src->ea_value_length);
513 if (src_size < 8 + name_len + 1 + value_len) {
514 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
520 if (ea_name_len == name_len &&
521 memcmp(ea_name, name, name_len) == 0) {
525 if (dst_size < value_len) {
529 memcpy(dst, value, value_len);
533 /* 'user.' plus a terminating null */
534 user_name_len = 5 + 1 + name_len;
538 if (dst_size >= user_name_len) {
539 dst_size -= user_name_len;
540 memcpy(dst, "user.", 5);
542 memcpy(dst, src->ea_data, name_len);
546 } else if (dst_size == 0) {
547 /* skip copy - calc size only */
549 /* stop before overrun buffer */
555 if (!src->next_entry_offset)
558 if (src_size < le32_to_cpu(src->next_entry_offset)) {
559 /* stop before overrun buffer */
563 src_size -= le32_to_cpu(src->next_entry_offset);
564 src = (void *)((char *)src +
565 le32_to_cpu(src->next_entry_offset));
568 /* didn't find the named attribute */
577 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
578 const unsigned char *path, const unsigned char *ea_name,
579 char *ea_data, size_t buf_size,
580 struct cifs_sb_info *cifs_sb)
584 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
585 struct cifs_open_parms oparms;
587 struct smb2_file_full_ea_info *smb2_data;
588 int ea_buf_size = SMB2_MIN_EA_BUF;
590 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
595 oparms.desired_access = FILE_READ_EA;
596 oparms.disposition = FILE_OPEN;
597 oparms.create_options = 0;
599 oparms.reconnect = false;
601 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
604 cifs_dbg(FYI, "open failed rc=%d\n", rc);
609 smb2_data = kzalloc(ea_buf_size, GFP_KERNEL);
610 if (smb2_data == NULL) {
611 SMB2_close(xid, tcon, fid.persistent_fid,
616 rc = SMB2_query_eas(xid, tcon, fid.persistent_fid,
618 ea_buf_size, smb2_data);
626 if (ea_buf_size > SMB2_MAX_EA_BUF) {
627 cifs_dbg(VFS, "EA size is too large\n");
628 SMB2_close(xid, tcon, fid.persistent_fid,
634 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
637 * If ea_name is NULL (listxattr) and there are no EAs, return 0 as it's
638 * not an error. Otherwise, the specified ea_name was not found.
641 rc = move_smb2_ea_to_cifs(ea_data, buf_size, smb2_data,
642 SMB2_MAX_EA_BUF, ea_name);
643 else if (!ea_name && rc == -ENODATA)
652 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
653 const char *path, const char *ea_name, const void *ea_value,
654 const __u16 ea_value_len, const struct nls_table *nls_codepage,
655 struct cifs_sb_info *cifs_sb)
659 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
660 struct cifs_open_parms oparms;
662 struct smb2_file_full_ea_info *ea;
663 int ea_name_len = strlen(ea_name);
666 if (ea_name_len > 255)
669 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
674 oparms.desired_access = FILE_WRITE_EA;
675 oparms.disposition = FILE_OPEN;
676 oparms.create_options = 0;
678 oparms.reconnect = false;
680 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
683 cifs_dbg(FYI, "open failed rc=%d\n", rc);
687 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
688 ea = kzalloc(len, GFP_KERNEL);
690 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
694 ea->ea_name_length = ea_name_len;
695 ea->ea_value_length = cpu_to_le16(ea_value_len);
696 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
697 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
699 rc = SMB2_set_ea(xid, tcon, fid.persistent_fid, fid.volatile_fid, ea,
701 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
708 smb2_can_echo(struct TCP_Server_Info *server)
710 return server->echoes;
714 smb2_clear_stats(struct cifs_tcon *tcon)
716 #ifdef CONFIG_CIFS_STATS
718 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
719 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
720 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
726 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
728 seq_puts(m, "\n\tShare Capabilities:");
729 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
730 seq_puts(m, " DFS,");
731 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
732 seq_puts(m, " CONTINUOUS AVAILABILITY,");
733 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
734 seq_puts(m, " SCALEOUT,");
735 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
736 seq_puts(m, " CLUSTER,");
737 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
738 seq_puts(m, " ASYMMETRIC,");
739 if (tcon->capabilities == 0)
740 seq_puts(m, " None");
741 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
742 seq_puts(m, " Aligned,");
743 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
744 seq_puts(m, " Partition Aligned,");
745 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
746 seq_puts(m, " SSD,");
747 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
748 seq_puts(m, " TRIM-support,");
750 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
751 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
752 if (tcon->perf_sector_size)
753 seq_printf(m, "\tOptimal sector size: 0x%x",
754 tcon->perf_sector_size);
755 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
759 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
761 #ifdef CONFIG_CIFS_STATS
762 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
763 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
764 seq_printf(m, "\nNegotiates: %d sent %d failed",
765 atomic_read(&sent[SMB2_NEGOTIATE_HE]),
766 atomic_read(&failed[SMB2_NEGOTIATE_HE]));
767 seq_printf(m, "\nSessionSetups: %d sent %d failed",
768 atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
769 atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
770 seq_printf(m, "\nLogoffs: %d sent %d failed",
771 atomic_read(&sent[SMB2_LOGOFF_HE]),
772 atomic_read(&failed[SMB2_LOGOFF_HE]));
773 seq_printf(m, "\nTreeConnects: %d sent %d failed",
774 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
775 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
776 seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
777 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
778 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
779 seq_printf(m, "\nCreates: %d sent %d failed",
780 atomic_read(&sent[SMB2_CREATE_HE]),
781 atomic_read(&failed[SMB2_CREATE_HE]));
782 seq_printf(m, "\nCloses: %d sent %d failed",
783 atomic_read(&sent[SMB2_CLOSE_HE]),
784 atomic_read(&failed[SMB2_CLOSE_HE]));
785 seq_printf(m, "\nFlushes: %d sent %d failed",
786 atomic_read(&sent[SMB2_FLUSH_HE]),
787 atomic_read(&failed[SMB2_FLUSH_HE]));
788 seq_printf(m, "\nReads: %d sent %d failed",
789 atomic_read(&sent[SMB2_READ_HE]),
790 atomic_read(&failed[SMB2_READ_HE]));
791 seq_printf(m, "\nWrites: %d sent %d failed",
792 atomic_read(&sent[SMB2_WRITE_HE]),
793 atomic_read(&failed[SMB2_WRITE_HE]));
794 seq_printf(m, "\nLocks: %d sent %d failed",
795 atomic_read(&sent[SMB2_LOCK_HE]),
796 atomic_read(&failed[SMB2_LOCK_HE]));
797 seq_printf(m, "\nIOCTLs: %d sent %d failed",
798 atomic_read(&sent[SMB2_IOCTL_HE]),
799 atomic_read(&failed[SMB2_IOCTL_HE]));
800 seq_printf(m, "\nCancels: %d sent %d failed",
801 atomic_read(&sent[SMB2_CANCEL_HE]),
802 atomic_read(&failed[SMB2_CANCEL_HE]));
803 seq_printf(m, "\nEchos: %d sent %d failed",
804 atomic_read(&sent[SMB2_ECHO_HE]),
805 atomic_read(&failed[SMB2_ECHO_HE]));
806 seq_printf(m, "\nQueryDirectories: %d sent %d failed",
807 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
808 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
809 seq_printf(m, "\nChangeNotifies: %d sent %d failed",
810 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
811 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
812 seq_printf(m, "\nQueryInfos: %d sent %d failed",
813 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
814 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
815 seq_printf(m, "\nSetInfos: %d sent %d failed",
816 atomic_read(&sent[SMB2_SET_INFO_HE]),
817 atomic_read(&failed[SMB2_SET_INFO_HE]));
818 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
819 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
820 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
825 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
827 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
828 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
830 cfile->fid.persistent_fid = fid->persistent_fid;
831 cfile->fid.volatile_fid = fid->volatile_fid;
832 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
834 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
835 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
839 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
840 struct cifs_fid *fid)
842 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
846 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
847 u64 persistent_fid, u64 volatile_fid,
848 struct copychunk_ioctl *pcchunk)
851 unsigned int ret_data_len;
852 struct resume_key_req *res_key;
854 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
855 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
856 NULL, 0 /* no input */,
857 (char **)&res_key, &ret_data_len);
860 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
861 goto req_res_key_exit;
863 if (ret_data_len < sizeof(struct resume_key_req)) {
864 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
866 goto req_res_key_exit;
868 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
876 smb2_copychunk_range(const unsigned int xid,
877 struct cifsFileInfo *srcfile,
878 struct cifsFileInfo *trgtfile, u64 src_off,
879 u64 len, u64 dest_off)
882 unsigned int ret_data_len;
883 struct copychunk_ioctl *pcchunk;
884 struct copychunk_ioctl_rsp *retbuf = NULL;
885 struct cifs_tcon *tcon;
886 int chunks_copied = 0;
887 bool chunk_sizes_updated = false;
888 ssize_t bytes_written, total_bytes_written = 0;
890 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
895 cifs_dbg(FYI, "in smb2_copychunk_range - about to call request res key\n");
896 /* Request a key from the server to identify the source of the copy */
897 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
898 srcfile->fid.persistent_fid,
899 srcfile->fid.volatile_fid, pcchunk);
901 /* Note: request_res_key sets res_key null only if rc !=0 */
905 /* For now array only one chunk long, will make more flexible later */
906 pcchunk->ChunkCount = cpu_to_le32(1);
907 pcchunk->Reserved = 0;
908 pcchunk->Reserved2 = 0;
910 tcon = tlink_tcon(trgtfile->tlink);
913 pcchunk->SourceOffset = cpu_to_le64(src_off);
914 pcchunk->TargetOffset = cpu_to_le64(dest_off);
916 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
918 /* Request server copy to target from src identified by key */
919 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
920 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
921 true /* is_fsctl */, (char *)pcchunk,
922 sizeof(struct copychunk_ioctl), (char **)&retbuf,
926 sizeof(struct copychunk_ioctl_rsp)) {
927 cifs_dbg(VFS, "invalid cchunk response size\n");
931 if (retbuf->TotalBytesWritten == 0) {
932 cifs_dbg(FYI, "no bytes copied\n");
937 * Check if server claimed to write more than we asked
939 if (le32_to_cpu(retbuf->TotalBytesWritten) >
940 le32_to_cpu(pcchunk->Length)) {
941 cifs_dbg(VFS, "invalid copy chunk response\n");
945 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
946 cifs_dbg(VFS, "invalid num chunks written\n");
952 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
953 src_off += bytes_written;
954 dest_off += bytes_written;
955 len -= bytes_written;
956 total_bytes_written += bytes_written;
958 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
959 le32_to_cpu(retbuf->ChunksWritten),
960 le32_to_cpu(retbuf->ChunkBytesWritten),
962 } else if (rc == -EINVAL) {
963 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
966 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
967 le32_to_cpu(retbuf->ChunksWritten),
968 le32_to_cpu(retbuf->ChunkBytesWritten),
969 le32_to_cpu(retbuf->TotalBytesWritten));
972 * Check if this is the first request using these sizes,
973 * (ie check if copy succeed once with original sizes
974 * and check if the server gave us different sizes after
975 * we already updated max sizes on previous request).
976 * if not then why is the server returning an error now
978 if ((chunks_copied != 0) || chunk_sizes_updated)
981 /* Check that server is not asking us to grow size */
982 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
983 tcon->max_bytes_chunk)
984 tcon->max_bytes_chunk =
985 le32_to_cpu(retbuf->ChunkBytesWritten);
987 goto cchunk_out; /* server gave us bogus size */
989 /* No need to change MaxChunks since already set to 1 */
990 chunk_sizes_updated = true;
1001 return total_bytes_written;
1005 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1006 struct cifs_fid *fid)
1008 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1012 smb2_read_data_offset(char *buf)
1014 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1015 return rsp->DataOffset;
1019 smb2_read_data_length(char *buf, bool in_remaining)
1021 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1024 return le32_to_cpu(rsp->DataRemaining);
1026 return le32_to_cpu(rsp->DataLength);
1031 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1032 struct cifs_io_parms *parms, unsigned int *bytes_read,
1033 char **buf, int *buf_type)
1035 parms->persistent_fid = pfid->persistent_fid;
1036 parms->volatile_fid = pfid->volatile_fid;
1037 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1041 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1042 struct cifs_io_parms *parms, unsigned int *written,
1043 struct kvec *iov, unsigned long nr_segs)
1046 parms->persistent_fid = pfid->persistent_fid;
1047 parms->volatile_fid = pfid->volatile_fid;
1048 return SMB2_write(xid, parms, written, iov, nr_segs);
1051 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1052 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1053 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1055 struct cifsInodeInfo *cifsi;
1058 cifsi = CIFS_I(inode);
1060 /* if file already sparse don't bother setting sparse again */
1061 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1062 return true; /* already sparse */
1064 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1065 return true; /* already not sparse */
1068 * Can't check for sparse support on share the usual way via the
1069 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1070 * since Samba server doesn't set the flag on the share, yet
1071 * supports the set sparse FSCTL and returns sparse correctly
1072 * in the file attributes. If we fail setting sparse though we
1073 * mark that server does not support sparse files for this share
1074 * to avoid repeatedly sending the unsupported fsctl to server
1075 * if the file is repeatedly extended.
1077 if (tcon->broken_sparse_sup)
1080 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1081 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1083 &setsparse, 1, NULL, NULL);
1085 tcon->broken_sparse_sup = true;
1086 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1091 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1093 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1099 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1100 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1102 __le64 eof = cpu_to_le64(size);
1103 struct inode *inode;
1106 * If extending file more than one page make sparse. Many Linux fs
1107 * make files sparse by default when extending via ftruncate
1109 inode = d_inode(cfile->dentry);
1111 if (!set_alloc && (size > inode->i_size + 8192)) {
1112 __u8 set_sparse = 1;
1114 /* whether set sparse succeeds or not, extend the file */
1115 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1118 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1119 cfile->fid.volatile_fid, cfile->pid, &eof, false);
1123 smb2_duplicate_extents(const unsigned int xid,
1124 struct cifsFileInfo *srcfile,
1125 struct cifsFileInfo *trgtfile, u64 src_off,
1126 u64 len, u64 dest_off)
1129 unsigned int ret_data_len;
1130 struct duplicate_extents_to_file dup_ext_buf;
1131 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1133 /* server fileays advertise duplicate extent support with this flag */
1134 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1135 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1138 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1139 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1140 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1141 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1142 dup_ext_buf.ByteCount = cpu_to_le64(len);
1143 cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
1144 src_off, dest_off, len);
1146 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1148 goto duplicate_extents_out;
1150 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1151 trgtfile->fid.volatile_fid,
1152 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
1153 true /* is_fsctl */,
1154 (char *)&dup_ext_buf,
1155 sizeof(struct duplicate_extents_to_file),
1159 if (ret_data_len > 0)
1160 cifs_dbg(FYI, "non-zero response length in duplicate extents");
1162 duplicate_extents_out:
1167 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1168 struct cifsFileInfo *cfile)
1170 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1171 cfile->fid.volatile_fid);
1175 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1176 struct cifsFileInfo *cfile)
1178 struct fsctl_set_integrity_information_req integr_info;
1179 unsigned int ret_data_len;
1181 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1182 integr_info.Flags = 0;
1183 integr_info.Reserved = 0;
1185 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1186 cfile->fid.volatile_fid,
1187 FSCTL_SET_INTEGRITY_INFORMATION,
1188 true /* is_fsctl */,
1189 (char *)&integr_info,
1190 sizeof(struct fsctl_set_integrity_information_req),
1197 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1198 struct cifsFileInfo *cfile, void __user *ioc_buf)
1200 char *retbuf = NULL;
1201 unsigned int ret_data_len = 0;
1203 struct smb_snapshot_array snapshot_in;
1205 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1206 cfile->fid.volatile_fid,
1207 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
1208 true /* is_fsctl */,
1209 NULL, 0 /* no input data */,
1212 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1217 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1219 if (copy_from_user(&snapshot_in, ioc_buf,
1220 sizeof(struct smb_snapshot_array))) {
1225 if (snapshot_in.snapshot_array_size < sizeof(struct smb_snapshot_array)) {
1231 if (ret_data_len > snapshot_in.snapshot_array_size)
1232 ret_data_len = snapshot_in.snapshot_array_size;
1234 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1243 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1244 const char *path, struct cifs_sb_info *cifs_sb,
1245 struct cifs_fid *fid, __u16 search_flags,
1246 struct cifs_search_info *srch_inf)
1250 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1251 struct cifs_open_parms oparms;
1253 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1258 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1259 oparms.disposition = FILE_OPEN;
1260 oparms.create_options = 0;
1262 oparms.reconnect = false;
1264 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
1267 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
1271 srch_inf->entries_in_buffer = 0;
1272 srch_inf->index_of_last_entry = 0;
1274 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1275 fid->volatile_fid, 0, srch_inf);
1277 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
1278 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1284 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1285 struct cifs_fid *fid, __u16 search_flags,
1286 struct cifs_search_info *srch_inf)
1288 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1289 fid->volatile_fid, 0, srch_inf);
1293 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1294 struct cifs_fid *fid)
1296 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1300 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
1301 * the number of credits and return true. Otherwise - return false.
1304 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
1306 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
1308 if (shdr->Status != STATUS_PENDING)
1312 spin_lock(&server->req_lock);
1313 server->credits += le16_to_cpu(shdr->CreditRequest);
1314 spin_unlock(&server->req_lock);
1315 wake_up(&server->request_q);
1322 smb2_is_session_expired(char *buf)
1324 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
1326 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED)
1329 cifs_dbg(FYI, "Session expired\n");
1334 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
1335 struct cifsInodeInfo *cinode)
1337 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
1338 return SMB2_lease_break(0, tcon, cinode->lease_key,
1339 smb2_get_lease_state(cinode));
1341 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
1343 CIFS_CACHE_READ(cinode) ? 1 : 0);
1347 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1348 struct kstatfs *buf)
1351 __le16 srch_path = 0; /* Null - open root of share */
1352 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1353 struct cifs_open_parms oparms;
1354 struct cifs_fid fid;
1357 oparms.desired_access = FILE_READ_ATTRIBUTES;
1358 oparms.disposition = FILE_OPEN;
1359 oparms.create_options = 0;
1361 oparms.reconnect = false;
1363 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
1366 buf->f_type = SMB2_MAGIC_NUMBER;
1367 rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1369 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1374 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
1376 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
1377 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
1381 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
1382 __u64 length, __u32 type, int lock, int unlock, bool wait)
1384 if (unlock && !lock)
1385 type = SMB2_LOCKFLAG_UNLOCK;
1386 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
1387 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
1388 current->tgid, length, offset, type, wait);
1392 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
1394 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
1398 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
1400 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
1404 smb2_new_lease_key(struct cifs_fid *fid)
1406 generate_random_uuid(fid->lease_key);
1410 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
1411 const char *search_name,
1412 struct dfs_info3_param **target_nodes,
1413 unsigned int *num_of_nodes,
1414 const struct nls_table *nls_codepage, int remap)
1417 __le16 *utf16_path = NULL;
1418 int utf16_path_len = 0;
1419 struct cifs_tcon *tcon;
1420 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
1421 struct get_dfs_referral_rsp *dfs_rsp = NULL;
1422 u32 dfs_req_size = 0, dfs_rsp_size = 0;
1424 cifs_dbg(FYI, "smb2_get_dfs_refer path <%s>\n", search_name);
1427 * Try to use the IPC tcon, otherwise just use any
1429 tcon = ses->tcon_ipc;
1431 spin_lock(&cifs_tcp_ses_lock);
1432 tcon = list_first_entry_or_null(&ses->tcon_list,
1437 spin_unlock(&cifs_tcp_ses_lock);
1441 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
1447 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
1449 nls_codepage, remap);
1455 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
1456 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
1462 /* Highest DFS referral version understood */
1463 dfs_req->MaxReferralLevel = DFS_VERSION;
1465 /* Path to resolve in an UTF-16 null-terminated string */
1466 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
1469 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1470 FSCTL_DFS_GET_REFERRALS,
1471 true /* is_fsctl */,
1472 (char *)dfs_req, dfs_req_size,
1473 (char **)&dfs_rsp, &dfs_rsp_size);
1474 } while (rc == -EAGAIN);
1477 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
1478 cifs_dbg(VFS, "ioctl error in smb2_get_dfs_refer rc=%d\n", rc);
1482 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
1483 num_of_nodes, target_nodes,
1484 nls_codepage, remap, search_name,
1485 true /* is_unicode */);
1487 cifs_dbg(VFS, "parse error in smb2_get_dfs_refer rc=%d\n", rc);
1492 if (tcon && !tcon->ipc) {
1493 /* ipc tcons are not refcounted */
1494 spin_lock(&cifs_tcp_ses_lock);
1496 spin_unlock(&cifs_tcp_ses_lock);
1503 #define SMB2_SYMLINK_STRUCT_SIZE \
1504 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
1507 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
1508 const char *full_path, char **target_path,
1509 struct cifs_sb_info *cifs_sb)
1513 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1514 struct cifs_open_parms oparms;
1515 struct cifs_fid fid;
1516 struct kvec err_iov = {NULL, 0};
1517 struct smb2_err_rsp *err_buf;
1518 struct smb2_symlink_err_rsp *symlink;
1519 unsigned int sub_len;
1520 unsigned int sub_offset;
1521 unsigned int print_len;
1522 unsigned int print_offset;
1523 struct cifs_ses *ses = tcon->ses;
1524 struct TCP_Server_Info *server = ses->server;
1526 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
1528 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1533 oparms.desired_access = FILE_READ_ATTRIBUTES;
1534 oparms.disposition = FILE_OPEN;
1535 oparms.create_options = 0;
1537 oparms.reconnect = false;
1539 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_iov);
1541 if (!rc || !err_iov.iov_base) {
1546 err_buf = err_iov.iov_base;
1547 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
1548 err_iov.iov_len + server->vals->header_preamble_size < SMB2_SYMLINK_STRUCT_SIZE) {
1553 /* open must fail on symlink - reset rc */
1555 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
1556 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
1557 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
1558 print_len = le16_to_cpu(symlink->PrintNameLength);
1559 print_offset = le16_to_cpu(symlink->PrintNameOffset);
1561 if (err_iov.iov_len + server->vals->header_preamble_size <
1562 SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
1567 if (err_iov.iov_len + server->vals->header_preamble_size <
1568 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
1573 *target_path = cifs_strndup_from_utf16(
1574 (char *)symlink->PathBuffer + sub_offset,
1575 sub_len, true, cifs_sb->local_nls);
1576 if (!(*target_path)) {
1580 convert_delimiter(*target_path, '/');
1581 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
1586 #ifdef CONFIG_CIFS_ACL
1587 static struct cifs_ntsd *
1588 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
1589 const struct cifs_fid *cifsfid, u32 *pacllen)
1591 struct cifs_ntsd *pntsd = NULL;
1593 int rc = -EOPNOTSUPP;
1594 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1597 return ERR_CAST(tlink);
1600 cifs_dbg(FYI, "trying to get acl\n");
1602 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
1603 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
1606 cifs_put_tlink(tlink);
1608 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
1615 static struct cifs_ntsd *
1616 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
1617 const char *path, u32 *pacllen)
1619 struct cifs_ntsd *pntsd = NULL;
1620 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1623 struct cifs_tcon *tcon;
1624 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1625 struct cifs_fid fid;
1626 struct cifs_open_parms oparms;
1629 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
1631 return ERR_CAST(tlink);
1633 tcon = tlink_tcon(tlink);
1636 if (backup_cred(cifs_sb))
1637 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1639 oparms.create_options = 0;
1641 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1649 oparms.desired_access = READ_CONTROL;
1650 oparms.disposition = FILE_OPEN;
1652 oparms.reconnect = false;
1654 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
1657 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
1658 fid.volatile_fid, (void **)&pntsd, pacllen);
1659 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1662 cifs_put_tlink(tlink);
1665 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
1671 #ifdef CONFIG_CIFS_ACL
1673 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
1674 struct inode *inode, const char *path, int aclflag)
1676 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1678 int rc, access_flags = 0;
1679 struct cifs_tcon *tcon;
1680 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1681 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1682 struct cifs_fid fid;
1683 struct cifs_open_parms oparms;
1686 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
1688 return PTR_ERR(tlink);
1690 tcon = tlink_tcon(tlink);
1693 if (backup_cred(cifs_sb))
1694 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1696 oparms.create_options = 0;
1698 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
1699 access_flags = WRITE_OWNER;
1701 access_flags = WRITE_DAC;
1703 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1711 oparms.desired_access = access_flags;
1712 oparms.disposition = FILE_OPEN;
1715 oparms.reconnect = false;
1717 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
1720 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
1721 fid.volatile_fid, pnntsd, acllen, aclflag);
1722 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1725 cifs_put_tlink(tlink);
1729 #endif /* CIFS_ACL */
1731 /* Retrieve an ACL from the server */
1732 static struct cifs_ntsd *
1733 get_smb2_acl(struct cifs_sb_info *cifs_sb,
1734 struct inode *inode, const char *path,
1737 struct cifs_ntsd *pntsd = NULL;
1738 struct cifsFileInfo *open_file = NULL;
1741 open_file = find_readable_file(CIFS_I(inode), true);
1743 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
1745 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
1746 cifsFileInfo_put(open_file);
1751 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
1752 loff_t offset, loff_t len, bool keep_size)
1754 struct inode *inode;
1755 struct cifsInodeInfo *cifsi;
1756 struct cifsFileInfo *cfile = file->private_data;
1757 struct file_zero_data_information fsctl_buf;
1763 inode = d_inode(cfile->dentry);
1764 cifsi = CIFS_I(inode);
1766 /* if file not oplocked can't be sure whether asking to extend size */
1767 if (!CIFS_CACHE_READ(cifsi))
1768 if (keep_size == false) {
1775 * Must check if file sparse since fallocate -z (zero range) assumes
1776 * non-sparse allocation
1778 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
1785 * need to make sure we are not asked to extend the file since the SMB3
1786 * fsctl does not change the file size. In the future we could change
1787 * this to zero the first part of the range then set the file size
1788 * which for a non sparse file would zero the newly extended range
1790 if (keep_size == false)
1791 if (i_size_read(inode) < offset + len) {
1797 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1799 fsctl_buf.FileOffset = cpu_to_le64(offset);
1800 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1802 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1803 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
1804 true /* is_fctl */, (char *)&fsctl_buf,
1805 sizeof(struct file_zero_data_information), NULL, NULL);
1810 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
1811 loff_t offset, loff_t len)
1813 struct inode *inode;
1814 struct cifsInodeInfo *cifsi;
1815 struct cifsFileInfo *cfile = file->private_data;
1816 struct file_zero_data_information fsctl_buf;
1819 __u8 set_sparse = 1;
1823 inode = d_inode(cfile->dentry);
1824 cifsi = CIFS_I(inode);
1826 /* Need to make file sparse, if not already, before freeing range. */
1827 /* Consider adding equivalent for compressed since it could also work */
1828 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
1834 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1836 fsctl_buf.FileOffset = cpu_to_le64(offset);
1837 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1839 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1840 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
1841 true /* is_fctl */, (char *)&fsctl_buf,
1842 sizeof(struct file_zero_data_information), NULL, NULL);
1847 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
1848 loff_t off, loff_t len, bool keep_size)
1850 struct inode *inode;
1851 struct cifsInodeInfo *cifsi;
1852 struct cifsFileInfo *cfile = file->private_data;
1853 long rc = -EOPNOTSUPP;
1858 inode = d_inode(cfile->dentry);
1859 cifsi = CIFS_I(inode);
1861 /* if file not oplocked can't be sure whether asking to extend size */
1862 if (!CIFS_CACHE_READ(cifsi))
1863 if (keep_size == false) {
1869 * Files are non-sparse by default so falloc may be a no-op
1870 * Must check if file sparse. If not sparse, and not extending
1871 * then no need to do anything since file already allocated
1873 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
1874 if (keep_size == true)
1876 /* check if extending file */
1877 else if (i_size_read(inode) >= off + len)
1878 /* not extending file and already not sparse */
1880 /* BB: in future add else clause to extend file */
1887 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
1889 * Check if falloc starts within first few pages of file
1890 * and ends within a few pages of the end of file to
1891 * ensure that most of file is being forced to be
1892 * fallocated now. If so then setting whole file sparse
1893 * ie potentially making a few extra pages at the beginning
1894 * or end of the file non-sparse via set_sparse is harmless.
1896 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
1902 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
1904 /* BB: else ... in future add code to extend file and set sparse */
1912 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
1913 loff_t off, loff_t len)
1915 /* KEEP_SIZE already checked for by do_fallocate */
1916 if (mode & FALLOC_FL_PUNCH_HOLE)
1917 return smb3_punch_hole(file, tcon, off, len);
1918 else if (mode & FALLOC_FL_ZERO_RANGE) {
1919 if (mode & FALLOC_FL_KEEP_SIZE)
1920 return smb3_zero_range(file, tcon, off, len, true);
1921 return smb3_zero_range(file, tcon, off, len, false);
1922 } else if (mode == FALLOC_FL_KEEP_SIZE)
1923 return smb3_simple_falloc(file, tcon, off, len, true);
1925 return smb3_simple_falloc(file, tcon, off, len, false);
1931 smb2_downgrade_oplock(struct TCP_Server_Info *server,
1932 struct cifsInodeInfo *cinode, bool set_level2)
1935 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
1938 server->ops->set_oplock_level(cinode, 0, 0, NULL);
1942 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1943 unsigned int epoch, bool *purge_cache)
1946 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1948 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1949 cinode->oplock = CIFS_CACHE_RHW_FLG;
1950 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
1951 &cinode->vfs_inode);
1952 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
1953 cinode->oplock = CIFS_CACHE_RW_FLG;
1954 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
1955 &cinode->vfs_inode);
1956 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
1957 cinode->oplock = CIFS_CACHE_READ_FLG;
1958 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
1959 &cinode->vfs_inode);
1965 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1966 unsigned int epoch, bool *purge_cache)
1968 char message[5] = {0};
1971 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1975 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
1976 cinode->oplock |= CIFS_CACHE_READ_FLG;
1977 strcat(message, "R");
1979 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
1980 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
1981 strcat(message, "H");
1983 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
1984 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
1985 strcat(message, "W");
1987 if (!cinode->oplock)
1988 strcat(message, "None");
1989 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
1990 &cinode->vfs_inode);
1994 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1995 unsigned int epoch, bool *purge_cache)
1997 unsigned int old_oplock = cinode->oplock;
1999 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
2002 *purge_cache = false;
2003 if (old_oplock == CIFS_CACHE_READ_FLG) {
2004 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
2005 (epoch - cinode->epoch > 0))
2006 *purge_cache = true;
2007 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2008 (epoch - cinode->epoch > 1))
2009 *purge_cache = true;
2010 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2011 (epoch - cinode->epoch > 1))
2012 *purge_cache = true;
2013 else if (cinode->oplock == 0 &&
2014 (epoch - cinode->epoch > 0))
2015 *purge_cache = true;
2016 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
2017 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2018 (epoch - cinode->epoch > 0))
2019 *purge_cache = true;
2020 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2021 (epoch - cinode->epoch > 1))
2022 *purge_cache = true;
2024 cinode->epoch = epoch;
2029 smb2_is_read_op(__u32 oplock)
2031 return oplock == SMB2_OPLOCK_LEVEL_II;
2035 smb21_is_read_op(__u32 oplock)
2037 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
2038 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
2042 map_oplock_to_lease(u8 oplock)
2044 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
2045 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
2046 else if (oplock == SMB2_OPLOCK_LEVEL_II)
2047 return SMB2_LEASE_READ_CACHING;
2048 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
2049 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
2050 SMB2_LEASE_WRITE_CACHING;
2055 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
2057 struct create_lease *buf;
2059 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
2063 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
2064 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
2065 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2067 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2068 (struct create_lease, lcontext));
2069 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
2070 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2071 (struct create_lease, Name));
2072 buf->ccontext.NameLength = cpu_to_le16(4);
2073 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
2082 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
2084 struct create_lease_v2 *buf;
2086 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
2090 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
2091 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
2092 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2094 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2095 (struct create_lease_v2, lcontext));
2096 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
2097 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2098 (struct create_lease_v2, Name));
2099 buf->ccontext.NameLength = cpu_to_le16(4);
2100 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
2109 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
2111 struct create_lease *lc = (struct create_lease *)buf;
2113 *epoch = 0; /* not used */
2114 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2115 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2116 return le32_to_cpu(lc->lcontext.LeaseState);
2120 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
2122 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
2124 *epoch = le16_to_cpu(lc->lcontext.Epoch);
2125 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2126 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2128 memcpy(lease_key, &lc->lcontext.LeaseKeyLow,
2129 SMB2_LEASE_KEY_SIZE);
2130 return le32_to_cpu(lc->lcontext.LeaseState);
2134 smb2_wp_retry_size(struct inode *inode)
2136 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
2137 SMB2_MAX_BUFFER_SIZE);
2141 smb2_dir_needs_close(struct cifsFileInfo *cfile)
2143 return !cfile->invalidHandle;
2147 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
2148 struct smb_rqst *old_rq)
2150 struct smb2_sync_hdr *shdr =
2151 (struct smb2_sync_hdr *)old_rq->rq_iov[1].iov_base;
2153 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
2154 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
2155 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
2156 tr_hdr->Flags = cpu_to_le16(0x01);
2157 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2158 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
2161 /* We can not use the normal sg_set_buf() as we will sometimes pass a
2162 * stack object as buf.
2164 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
2165 unsigned int buflen)
2167 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
2171 * rqst->rq_iov[0] is rfc1002 length
2172 * rqst->rq_iov[1] is tranform header
2173 * rqst->rq_iov[2+] data to be encrypted/decrypted
2175 static struct scatterlist *
2176 init_sg(struct smb_rqst *rqst, u8 *sign)
2178 unsigned int sg_len = rqst->rq_nvec + rqst->rq_npages;
2179 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
2180 struct scatterlist *sg;
2184 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
2188 sg_init_table(sg, sg_len);
2189 smb2_sg_set_buf(&sg[0], rqst->rq_iov[1].iov_base + 20, assoc_data_len);
2190 for (i = 1; i < rqst->rq_nvec - 1; i++)
2191 smb2_sg_set_buf(&sg[i], rqst->rq_iov[i+1].iov_base,
2192 rqst->rq_iov[i+1].iov_len);
2193 for (j = 0; i < sg_len - 1; i++, j++) {
2194 unsigned int len = (j < rqst->rq_npages - 1) ? rqst->rq_pagesz
2196 sg_set_page(&sg[i], rqst->rq_pages[j], len, 0);
2198 smb2_sg_set_buf(&sg[sg_len - 1], sign, SMB2_SIGNATURE_SIZE);
2203 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
2205 struct cifs_ses *ses;
2208 spin_lock(&cifs_tcp_ses_lock);
2209 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2210 if (ses->Suid != ses_id)
2212 ses_enc_key = enc ? ses->smb3encryptionkey :
2213 ses->smb3decryptionkey;
2214 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
2215 spin_unlock(&cifs_tcp_ses_lock);
2218 spin_unlock(&cifs_tcp_ses_lock);
2223 * Encrypt or decrypt @rqst message. @rqst has the following format:
2224 * iov[0] - rfc1002 length
2225 * iov[1] - transform header (associate data),
2226 * iov[2-N] and pages - data to encrypt.
2227 * On success return encrypted data in iov[2-N] and pages, leave iov[0-1]
2231 crypt_message(struct TCP_Server_Info *server, struct smb_rqst *rqst, int enc)
2233 struct smb2_transform_hdr *tr_hdr =
2234 (struct smb2_transform_hdr *)rqst->rq_iov[0].iov_base;
2235 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20 - server->vals->header_preamble_size;
2237 struct scatterlist *sg;
2238 u8 sign[SMB2_SIGNATURE_SIZE] = {};
2239 u8 key[SMB3_SIGN_KEY_SIZE];
2240 struct aead_request *req;
2242 unsigned int iv_len;
2243 DECLARE_CRYPTO_WAIT(wait);
2244 struct crypto_aead *tfm;
2245 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2247 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
2249 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
2254 rc = smb3_crypto_aead_allocate(server);
2256 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
2260 tfm = enc ? server->secmech.ccmaesencrypt :
2261 server->secmech.ccmaesdecrypt;
2262 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
2264 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
2268 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
2270 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
2274 req = aead_request_alloc(tfm, GFP_KERNEL);
2276 cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
2281 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
2282 crypt_len += SMB2_SIGNATURE_SIZE;
2285 sg = init_sg(rqst, sign);
2287 cifs_dbg(VFS, "%s: Failed to init sg", __func__);
2292 iv_len = crypto_aead_ivsize(tfm);
2293 iv = kzalloc(iv_len, GFP_KERNEL);
2295 cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
2300 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2302 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
2303 aead_request_set_ad(req, assoc_data_len);
2305 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2306 crypto_req_done, &wait);
2308 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
2309 : crypto_aead_decrypt(req), &wait);
2312 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
2323 * This is called from smb_send_rqst. At this point we have the rfc1002
2324 * header as the first element in the vector.
2327 smb3_init_transform_rq(struct TCP_Server_Info *server, struct smb_rqst *new_rq,
2328 struct smb_rqst *old_rq)
2331 struct page **pages;
2332 struct smb2_transform_hdr *tr_hdr;
2333 unsigned int npages = old_rq->rq_npages;
2334 unsigned int orig_len = get_rfc1002_length(old_rq->rq_iov[0].iov_base);
2338 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2342 new_rq->rq_pages = pages;
2343 new_rq->rq_npages = old_rq->rq_npages;
2344 new_rq->rq_pagesz = old_rq->rq_pagesz;
2345 new_rq->rq_tailsz = old_rq->rq_tailsz;
2347 for (i = 0; i < npages; i++) {
2348 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2350 goto err_free_pages;
2353 /* Make space for one extra iov to hold the transform header */
2354 iov = kmalloc_array(old_rq->rq_nvec + 1, sizeof(struct kvec),
2357 goto err_free_pages;
2359 /* copy all iovs from the old except the 1st one (rfc1002 length) */
2360 memcpy(&iov[2], &old_rq->rq_iov[1],
2361 sizeof(struct kvec) * (old_rq->rq_nvec - 1));
2362 /* copy the rfc1002 iov */
2363 iov[0].iov_base = old_rq->rq_iov[0].iov_base;
2364 iov[0].iov_len = old_rq->rq_iov[0].iov_len;
2366 new_rq->rq_iov = iov;
2367 new_rq->rq_nvec = old_rq->rq_nvec + 1;
2369 tr_hdr = kmalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL);
2373 /* fill the 2nd iov with a transform header */
2374 fill_transform_hdr(tr_hdr, orig_len, old_rq);
2375 new_rq->rq_iov[1].iov_base = tr_hdr;
2376 new_rq->rq_iov[1].iov_len = sizeof(struct smb2_transform_hdr);
2378 /* Update rfc1002 header */
2379 inc_rfc1001_len(new_rq->rq_iov[0].iov_base,
2380 sizeof(struct smb2_transform_hdr));
2382 /* copy pages form the old */
2383 for (i = 0; i < npages; i++) {
2384 char *dst = kmap(new_rq->rq_pages[i]);
2385 char *src = kmap(old_rq->rq_pages[i]);
2386 unsigned int len = (i < npages - 1) ? new_rq->rq_pagesz :
2388 memcpy(dst, src, len);
2389 kunmap(new_rq->rq_pages[i]);
2390 kunmap(old_rq->rq_pages[i]);
2393 rc = crypt_message(server, new_rq, 1);
2394 cifs_dbg(FYI, "encrypt message returned %d", rc);
2396 goto err_free_tr_hdr;
2405 for (i = i - 1; i >= 0; i--)
2412 smb3_free_transform_rq(struct smb_rqst *rqst)
2414 int i = rqst->rq_npages - 1;
2417 put_page(rqst->rq_pages[i]);
2418 kfree(rqst->rq_pages);
2419 /* free transform header */
2420 kfree(rqst->rq_iov[1].iov_base);
2421 kfree(rqst->rq_iov);
2425 smb3_is_transform_hdr(void *buf)
2427 struct smb2_transform_hdr *trhdr = buf;
2429 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
2433 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
2434 unsigned int buf_data_size, struct page **pages,
2435 unsigned int npages, unsigned int page_data_size)
2438 struct smb_rqst rqst = {NULL};
2441 iov[0].iov_base = NULL;
2443 iov[1].iov_base = buf;
2444 iov[1].iov_len = sizeof(struct smb2_transform_hdr);
2445 iov[2].iov_base = buf + sizeof(struct smb2_transform_hdr);
2446 iov[2].iov_len = buf_data_size;
2450 rqst.rq_pages = pages;
2451 rqst.rq_npages = npages;
2452 rqst.rq_pagesz = PAGE_SIZE;
2453 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
2455 rc = crypt_message(server, &rqst, 0);
2456 cifs_dbg(FYI, "decrypt message returned %d\n", rc);
2461 memmove(buf + server->vals->header_preamble_size, iov[2].iov_base, buf_data_size);
2463 server->total_read = buf_data_size + page_data_size;
2469 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
2470 unsigned int npages, unsigned int len)
2475 for (i = 0; i < npages; i++) {
2476 struct page *page = pages[i];
2480 if (len >= PAGE_SIZE) {
2481 /* enough data to fill the page */
2485 zero_user(page, len, PAGE_SIZE - len);
2488 length = cifs_read_page_from_socket(server, page, n);
2491 server->total_read += length;
2498 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
2499 unsigned int cur_off, struct bio_vec **page_vec)
2501 struct bio_vec *bvec;
2504 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
2508 for (i = 0; i < npages; i++) {
2509 bvec[i].bv_page = pages[i];
2510 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
2511 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
2512 data_size -= bvec[i].bv_len;
2515 if (data_size != 0) {
2516 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
2526 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
2527 char *buf, unsigned int buf_len, struct page **pages,
2528 unsigned int npages, unsigned int page_data_size)
2530 unsigned int data_offset;
2531 unsigned int data_len;
2532 unsigned int cur_off;
2533 unsigned int cur_page_idx;
2534 unsigned int pad_len;
2535 struct cifs_readdata *rdata = mid->callback_data;
2536 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
2537 struct bio_vec *bvec = NULL;
2538 struct iov_iter iter;
2541 bool use_rdma_mr = false;
2543 if (shdr->Command != SMB2_READ) {
2544 cifs_dbg(VFS, "only big read responses are supported\n");
2548 if (server->ops->is_session_expired &&
2549 server->ops->is_session_expired(buf)) {
2550 cifs_reconnect(server);
2551 wake_up(&server->response_q);
2555 if (server->ops->is_status_pending &&
2556 server->ops->is_status_pending(buf, server, 0))
2559 rdata->result = server->ops->map_error(buf, false);
2560 if (rdata->result != 0) {
2561 cifs_dbg(FYI, "%s: server returned error %d\n",
2562 __func__, rdata->result);
2563 dequeue_mid(mid, rdata->result);
2567 data_offset = server->ops->read_data_offset(buf) + server->vals->header_preamble_size;
2568 #ifdef CONFIG_CIFS_SMB_DIRECT
2569 use_rdma_mr = rdata->mr;
2571 data_len = server->ops->read_data_length(buf, use_rdma_mr);
2573 if (data_offset < server->vals->read_rsp_size) {
2575 * win2k8 sometimes sends an offset of 0 when the read
2576 * is beyond the EOF. Treat it as if the data starts just after
2579 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
2580 __func__, data_offset);
2581 data_offset = server->vals->read_rsp_size;
2582 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
2583 /* data_offset is beyond the end of smallbuf */
2584 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
2585 __func__, data_offset);
2586 rdata->result = -EIO;
2587 dequeue_mid(mid, rdata->result);
2591 pad_len = data_offset - server->vals->read_rsp_size;
2593 if (buf_len <= data_offset) {
2594 /* read response payload is in pages */
2595 cur_page_idx = pad_len / PAGE_SIZE;
2596 cur_off = pad_len % PAGE_SIZE;
2598 if (cur_page_idx != 0) {
2599 /* data offset is beyond the 1st page of response */
2600 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
2601 __func__, data_offset);
2602 rdata->result = -EIO;
2603 dequeue_mid(mid, rdata->result);
2607 if (data_len > page_data_size - pad_len) {
2608 /* data_len is corrupt -- discard frame */
2609 rdata->result = -EIO;
2610 dequeue_mid(mid, rdata->result);
2614 rdata->result = init_read_bvec(pages, npages, page_data_size,
2616 if (rdata->result != 0) {
2617 dequeue_mid(mid, rdata->result);
2621 iov_iter_bvec(&iter, WRITE | ITER_BVEC, bvec, npages, data_len);
2622 } else if (buf_len >= data_offset + data_len) {
2623 /* read response payload is in buf */
2624 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
2625 iov.iov_base = buf + data_offset;
2626 iov.iov_len = data_len;
2627 iov_iter_kvec(&iter, WRITE | ITER_KVEC, &iov, 1, data_len);
2629 /* read response payload cannot be in both buf and pages */
2630 WARN_ONCE(1, "buf can not contain only a part of read data");
2631 rdata->result = -EIO;
2632 dequeue_mid(mid, rdata->result);
2636 /* set up first iov for signature check */
2637 rdata->iov[0].iov_base = buf;
2638 rdata->iov[0].iov_len = 4;
2639 rdata->iov[1].iov_base = buf + 4;
2640 rdata->iov[1].iov_len = server->vals->read_rsp_size - 4;
2641 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
2642 rdata->iov[0].iov_base, server->vals->read_rsp_size);
2644 length = rdata->copy_into_pages(server, rdata, &iter);
2651 dequeue_mid(mid, false);
2656 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2658 char *buf = server->smallbuf;
2659 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2660 unsigned int npages;
2661 struct page **pages;
2663 unsigned int buflen = server->pdu_size + server->vals->header_preamble_size;
2667 len = min_t(unsigned int, buflen, server->vals->read_rsp_size -
2668 server->vals->header_preamble_size +
2669 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
2671 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
2674 server->total_read += rc;
2676 len = le32_to_cpu(tr_hdr->OriginalMessageSize) +
2677 server->vals->header_preamble_size -
2678 server->vals->read_rsp_size;
2679 npages = DIV_ROUND_UP(len, PAGE_SIZE);
2681 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2687 for (; i < npages; i++) {
2688 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2695 /* read read data into pages */
2696 rc = read_data_into_pages(server, pages, npages, len);
2700 rc = cifs_discard_remaining_data(server);
2704 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size -
2705 server->vals->header_preamble_size,
2706 pages, npages, len);
2710 *mid = smb2_find_mid(server, buf);
2712 cifs_dbg(FYI, "mid not found\n");
2714 cifs_dbg(FYI, "mid found\n");
2715 (*mid)->decrypted = true;
2716 rc = handle_read_data(server, *mid, buf,
2717 server->vals->read_rsp_size,
2718 pages, npages, len);
2722 for (i = i - 1; i >= 0; i--)
2727 cifs_discard_remaining_data(server);
2732 receive_encrypted_standard(struct TCP_Server_Info *server,
2733 struct mid_q_entry **mid)
2736 char *buf = server->smallbuf;
2737 unsigned int pdu_length = server->pdu_size;
2738 unsigned int buf_size;
2739 struct mid_q_entry *mid_entry;
2741 /* switch to large buffer if too big for a small one */
2742 if (pdu_length + server->vals->header_preamble_size > MAX_CIFS_SMALL_BUFFER_SIZE) {
2743 server->large_buf = true;
2744 memcpy(server->bigbuf, buf, server->total_read);
2745 buf = server->bigbuf;
2748 /* now read the rest */
2749 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
2750 pdu_length - HEADER_SIZE(server) + 1 +
2751 server->vals->header_preamble_size);
2754 server->total_read += length;
2756 buf_size = pdu_length + server->vals->header_preamble_size - sizeof(struct smb2_transform_hdr);
2757 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
2761 mid_entry = smb2_find_mid(server, buf);
2762 if (mid_entry == NULL)
2763 cifs_dbg(FYI, "mid not found\n");
2765 cifs_dbg(FYI, "mid found\n");
2766 mid_entry->decrypted = true;
2771 if (mid_entry && mid_entry->handle)
2772 return mid_entry->handle(server, mid_entry);
2774 return cifs_handle_standard(server, mid_entry);
2778 smb3_receive_transform(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2780 char *buf = server->smallbuf;
2781 unsigned int pdu_length = server->pdu_size;
2782 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2783 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2785 if (pdu_length + server->vals->header_preamble_size < sizeof(struct smb2_transform_hdr) +
2786 sizeof(struct smb2_sync_hdr)) {
2787 cifs_dbg(VFS, "Transform message is too small (%u)\n",
2789 cifs_reconnect(server);
2790 wake_up(&server->response_q);
2791 return -ECONNABORTED;
2794 if (pdu_length + server->vals->header_preamble_size < orig_len + sizeof(struct smb2_transform_hdr)) {
2795 cifs_dbg(VFS, "Transform message is broken\n");
2796 cifs_reconnect(server);
2797 wake_up(&server->response_q);
2798 return -ECONNABORTED;
2801 if (pdu_length + server->vals->header_preamble_size > CIFSMaxBufSize + MAX_HEADER_SIZE(server))
2802 return receive_encrypted_read(server, mid);
2804 return receive_encrypted_standard(server, mid);
2808 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
2810 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
2812 return handle_read_data(server, mid, buf, server->pdu_size +
2813 server->vals->header_preamble_size,
2817 struct smb_version_operations smb20_operations = {
2818 .compare_fids = smb2_compare_fids,
2819 .setup_request = smb2_setup_request,
2820 .setup_async_request = smb2_setup_async_request,
2821 .check_receive = smb2_check_receive,
2822 .add_credits = smb2_add_credits,
2823 .set_credits = smb2_set_credits,
2824 .get_credits_field = smb2_get_credits_field,
2825 .get_credits = smb2_get_credits,
2826 .wait_mtu_credits = cifs_wait_mtu_credits,
2827 .get_next_mid = smb2_get_next_mid,
2828 .read_data_offset = smb2_read_data_offset,
2829 .read_data_length = smb2_read_data_length,
2830 .map_error = map_smb2_to_linux_error,
2831 .find_mid = smb2_find_mid,
2832 .check_message = smb2_check_message,
2833 .dump_detail = smb2_dump_detail,
2834 .clear_stats = smb2_clear_stats,
2835 .print_stats = smb2_print_stats,
2836 .is_oplock_break = smb2_is_valid_oplock_break,
2837 .handle_cancelled_mid = smb2_handle_cancelled_mid,
2838 .downgrade_oplock = smb2_downgrade_oplock,
2839 .need_neg = smb2_need_neg,
2840 .negotiate = smb2_negotiate,
2841 .negotiate_wsize = smb2_negotiate_wsize,
2842 .negotiate_rsize = smb2_negotiate_rsize,
2843 .sess_setup = SMB2_sess_setup,
2844 .logoff = SMB2_logoff,
2845 .tree_connect = SMB2_tcon,
2846 .tree_disconnect = SMB2_tdis,
2847 .qfs_tcon = smb2_qfs_tcon,
2848 .is_path_accessible = smb2_is_path_accessible,
2849 .can_echo = smb2_can_echo,
2851 .query_path_info = smb2_query_path_info,
2852 .get_srv_inum = smb2_get_srv_inum,
2853 .query_file_info = smb2_query_file_info,
2854 .set_path_size = smb2_set_path_size,
2855 .set_file_size = smb2_set_file_size,
2856 .set_file_info = smb2_set_file_info,
2857 .set_compression = smb2_set_compression,
2858 .mkdir = smb2_mkdir,
2859 .mkdir_setinfo = smb2_mkdir_setinfo,
2860 .rmdir = smb2_rmdir,
2861 .unlink = smb2_unlink,
2862 .rename = smb2_rename_path,
2863 .create_hardlink = smb2_create_hardlink,
2864 .query_symlink = smb2_query_symlink,
2865 .query_mf_symlink = smb3_query_mf_symlink,
2866 .create_mf_symlink = smb3_create_mf_symlink,
2867 .open = smb2_open_file,
2868 .set_fid = smb2_set_fid,
2869 .close = smb2_close_file,
2870 .flush = smb2_flush_file,
2871 .async_readv = smb2_async_readv,
2872 .async_writev = smb2_async_writev,
2873 .sync_read = smb2_sync_read,
2874 .sync_write = smb2_sync_write,
2875 .query_dir_first = smb2_query_dir_first,
2876 .query_dir_next = smb2_query_dir_next,
2877 .close_dir = smb2_close_dir,
2878 .calc_smb_size = smb2_calc_size,
2879 .is_status_pending = smb2_is_status_pending,
2880 .is_session_expired = smb2_is_session_expired,
2881 .oplock_response = smb2_oplock_response,
2882 .queryfs = smb2_queryfs,
2883 .mand_lock = smb2_mand_lock,
2884 .mand_unlock_range = smb2_unlock_range,
2885 .push_mand_locks = smb2_push_mandatory_locks,
2886 .get_lease_key = smb2_get_lease_key,
2887 .set_lease_key = smb2_set_lease_key,
2888 .new_lease_key = smb2_new_lease_key,
2889 .calc_signature = smb2_calc_signature,
2890 .is_read_op = smb2_is_read_op,
2891 .set_oplock_level = smb2_set_oplock_level,
2892 .create_lease_buf = smb2_create_lease_buf,
2893 .parse_lease_buf = smb2_parse_lease_buf,
2894 .copychunk_range = smb2_copychunk_range,
2895 .wp_retry_size = smb2_wp_retry_size,
2896 .dir_needs_close = smb2_dir_needs_close,
2897 .get_dfs_refer = smb2_get_dfs_refer,
2898 .select_sectype = smb2_select_sectype,
2899 #ifdef CONFIG_CIFS_XATTR
2900 .query_all_EAs = smb2_query_eas,
2901 .set_EA = smb2_set_ea,
2902 #endif /* CIFS_XATTR */
2903 #ifdef CONFIG_CIFS_ACL
2904 .get_acl = get_smb2_acl,
2905 .get_acl_by_fid = get_smb2_acl_by_fid,
2906 .set_acl = set_smb2_acl,
2907 #endif /* CIFS_ACL */
2910 struct smb_version_operations smb21_operations = {
2911 .compare_fids = smb2_compare_fids,
2912 .setup_request = smb2_setup_request,
2913 .setup_async_request = smb2_setup_async_request,
2914 .check_receive = smb2_check_receive,
2915 .add_credits = smb2_add_credits,
2916 .set_credits = smb2_set_credits,
2917 .get_credits_field = smb2_get_credits_field,
2918 .get_credits = smb2_get_credits,
2919 .wait_mtu_credits = smb2_wait_mtu_credits,
2920 .get_next_mid = smb2_get_next_mid,
2921 .read_data_offset = smb2_read_data_offset,
2922 .read_data_length = smb2_read_data_length,
2923 .map_error = map_smb2_to_linux_error,
2924 .find_mid = smb2_find_mid,
2925 .check_message = smb2_check_message,
2926 .dump_detail = smb2_dump_detail,
2927 .clear_stats = smb2_clear_stats,
2928 .print_stats = smb2_print_stats,
2929 .is_oplock_break = smb2_is_valid_oplock_break,
2930 .handle_cancelled_mid = smb2_handle_cancelled_mid,
2931 .downgrade_oplock = smb2_downgrade_oplock,
2932 .need_neg = smb2_need_neg,
2933 .negotiate = smb2_negotiate,
2934 .negotiate_wsize = smb2_negotiate_wsize,
2935 .negotiate_rsize = smb2_negotiate_rsize,
2936 .sess_setup = SMB2_sess_setup,
2937 .logoff = SMB2_logoff,
2938 .tree_connect = SMB2_tcon,
2939 .tree_disconnect = SMB2_tdis,
2940 .qfs_tcon = smb2_qfs_tcon,
2941 .is_path_accessible = smb2_is_path_accessible,
2942 .can_echo = smb2_can_echo,
2944 .query_path_info = smb2_query_path_info,
2945 .get_srv_inum = smb2_get_srv_inum,
2946 .query_file_info = smb2_query_file_info,
2947 .set_path_size = smb2_set_path_size,
2948 .set_file_size = smb2_set_file_size,
2949 .set_file_info = smb2_set_file_info,
2950 .set_compression = smb2_set_compression,
2951 .mkdir = smb2_mkdir,
2952 .mkdir_setinfo = smb2_mkdir_setinfo,
2953 .rmdir = smb2_rmdir,
2954 .unlink = smb2_unlink,
2955 .rename = smb2_rename_path,
2956 .create_hardlink = smb2_create_hardlink,
2957 .query_symlink = smb2_query_symlink,
2958 .query_mf_symlink = smb3_query_mf_symlink,
2959 .create_mf_symlink = smb3_create_mf_symlink,
2960 .open = smb2_open_file,
2961 .set_fid = smb2_set_fid,
2962 .close = smb2_close_file,
2963 .flush = smb2_flush_file,
2964 .async_readv = smb2_async_readv,
2965 .async_writev = smb2_async_writev,
2966 .sync_read = smb2_sync_read,
2967 .sync_write = smb2_sync_write,
2968 .query_dir_first = smb2_query_dir_first,
2969 .query_dir_next = smb2_query_dir_next,
2970 .close_dir = smb2_close_dir,
2971 .calc_smb_size = smb2_calc_size,
2972 .is_status_pending = smb2_is_status_pending,
2973 .is_session_expired = smb2_is_session_expired,
2974 .oplock_response = smb2_oplock_response,
2975 .queryfs = smb2_queryfs,
2976 .mand_lock = smb2_mand_lock,
2977 .mand_unlock_range = smb2_unlock_range,
2978 .push_mand_locks = smb2_push_mandatory_locks,
2979 .get_lease_key = smb2_get_lease_key,
2980 .set_lease_key = smb2_set_lease_key,
2981 .new_lease_key = smb2_new_lease_key,
2982 .calc_signature = smb2_calc_signature,
2983 .is_read_op = smb21_is_read_op,
2984 .set_oplock_level = smb21_set_oplock_level,
2985 .create_lease_buf = smb2_create_lease_buf,
2986 .parse_lease_buf = smb2_parse_lease_buf,
2987 .copychunk_range = smb2_copychunk_range,
2988 .wp_retry_size = smb2_wp_retry_size,
2989 .dir_needs_close = smb2_dir_needs_close,
2990 .enum_snapshots = smb3_enum_snapshots,
2991 .get_dfs_refer = smb2_get_dfs_refer,
2992 .select_sectype = smb2_select_sectype,
2993 #ifdef CONFIG_CIFS_XATTR
2994 .query_all_EAs = smb2_query_eas,
2995 .set_EA = smb2_set_ea,
2996 #endif /* CIFS_XATTR */
2997 #ifdef CONFIG_CIFS_ACL
2998 .get_acl = get_smb2_acl,
2999 .get_acl_by_fid = get_smb2_acl_by_fid,
3000 .set_acl = set_smb2_acl,
3001 #endif /* CIFS_ACL */
3004 struct smb_version_operations smb30_operations = {
3005 .compare_fids = smb2_compare_fids,
3006 .setup_request = smb2_setup_request,
3007 .setup_async_request = smb2_setup_async_request,
3008 .check_receive = smb2_check_receive,
3009 .add_credits = smb2_add_credits,
3010 .set_credits = smb2_set_credits,
3011 .get_credits_field = smb2_get_credits_field,
3012 .get_credits = smb2_get_credits,
3013 .wait_mtu_credits = smb2_wait_mtu_credits,
3014 .get_next_mid = smb2_get_next_mid,
3015 .read_data_offset = smb2_read_data_offset,
3016 .read_data_length = smb2_read_data_length,
3017 .map_error = map_smb2_to_linux_error,
3018 .find_mid = smb2_find_mid,
3019 .check_message = smb2_check_message,
3020 .dump_detail = smb2_dump_detail,
3021 .clear_stats = smb2_clear_stats,
3022 .print_stats = smb2_print_stats,
3023 .dump_share_caps = smb2_dump_share_caps,
3024 .is_oplock_break = smb2_is_valid_oplock_break,
3025 .handle_cancelled_mid = smb2_handle_cancelled_mid,
3026 .downgrade_oplock = smb2_downgrade_oplock,
3027 .need_neg = smb2_need_neg,
3028 .negotiate = smb2_negotiate,
3029 .negotiate_wsize = smb2_negotiate_wsize,
3030 .negotiate_rsize = smb2_negotiate_rsize,
3031 .sess_setup = SMB2_sess_setup,
3032 .logoff = SMB2_logoff,
3033 .tree_connect = SMB2_tcon,
3034 .tree_disconnect = SMB2_tdis,
3035 .qfs_tcon = smb3_qfs_tcon,
3036 .is_path_accessible = smb2_is_path_accessible,
3037 .can_echo = smb2_can_echo,
3039 .query_path_info = smb2_query_path_info,
3040 .get_srv_inum = smb2_get_srv_inum,
3041 .query_file_info = smb2_query_file_info,
3042 .set_path_size = smb2_set_path_size,
3043 .set_file_size = smb2_set_file_size,
3044 .set_file_info = smb2_set_file_info,
3045 .set_compression = smb2_set_compression,
3046 .mkdir = smb2_mkdir,
3047 .mkdir_setinfo = smb2_mkdir_setinfo,
3048 .rmdir = smb2_rmdir,
3049 .unlink = smb2_unlink,
3050 .rename = smb2_rename_path,
3051 .create_hardlink = smb2_create_hardlink,
3052 .query_symlink = smb2_query_symlink,
3053 .query_mf_symlink = smb3_query_mf_symlink,
3054 .create_mf_symlink = smb3_create_mf_symlink,
3055 .open = smb2_open_file,
3056 .set_fid = smb2_set_fid,
3057 .close = smb2_close_file,
3058 .flush = smb2_flush_file,
3059 .async_readv = smb2_async_readv,
3060 .async_writev = smb2_async_writev,
3061 .sync_read = smb2_sync_read,
3062 .sync_write = smb2_sync_write,
3063 .query_dir_first = smb2_query_dir_first,
3064 .query_dir_next = smb2_query_dir_next,
3065 .close_dir = smb2_close_dir,
3066 .calc_smb_size = smb2_calc_size,
3067 .is_status_pending = smb2_is_status_pending,
3068 .is_session_expired = smb2_is_session_expired,
3069 .oplock_response = smb2_oplock_response,
3070 .queryfs = smb2_queryfs,
3071 .mand_lock = smb2_mand_lock,
3072 .mand_unlock_range = smb2_unlock_range,
3073 .push_mand_locks = smb2_push_mandatory_locks,
3074 .get_lease_key = smb2_get_lease_key,
3075 .set_lease_key = smb2_set_lease_key,
3076 .new_lease_key = smb2_new_lease_key,
3077 .generate_signingkey = generate_smb30signingkey,
3078 .calc_signature = smb3_calc_signature,
3079 .set_integrity = smb3_set_integrity,
3080 .is_read_op = smb21_is_read_op,
3081 .set_oplock_level = smb3_set_oplock_level,
3082 .create_lease_buf = smb3_create_lease_buf,
3083 .parse_lease_buf = smb3_parse_lease_buf,
3084 .copychunk_range = smb2_copychunk_range,
3085 .duplicate_extents = smb2_duplicate_extents,
3086 .validate_negotiate = smb3_validate_negotiate,
3087 .wp_retry_size = smb2_wp_retry_size,
3088 .dir_needs_close = smb2_dir_needs_close,
3089 .fallocate = smb3_fallocate,
3090 .enum_snapshots = smb3_enum_snapshots,
3091 .init_transform_rq = smb3_init_transform_rq,
3092 .free_transform_rq = smb3_free_transform_rq,
3093 .is_transform_hdr = smb3_is_transform_hdr,
3094 .receive_transform = smb3_receive_transform,
3095 .get_dfs_refer = smb2_get_dfs_refer,
3096 .select_sectype = smb2_select_sectype,
3097 #ifdef CONFIG_CIFS_XATTR
3098 .query_all_EAs = smb2_query_eas,
3099 .set_EA = smb2_set_ea,
3100 #endif /* CIFS_XATTR */
3101 #ifdef CONFIG_CIFS_ACL
3102 .get_acl = get_smb2_acl,
3103 .get_acl_by_fid = get_smb2_acl_by_fid,
3104 .set_acl = set_smb2_acl,
3105 #endif /* CIFS_ACL */
3108 #ifdef CONFIG_CIFS_SMB311
3109 struct smb_version_operations smb311_operations = {
3110 .compare_fids = smb2_compare_fids,
3111 .setup_request = smb2_setup_request,
3112 .setup_async_request = smb2_setup_async_request,
3113 .check_receive = smb2_check_receive,
3114 .add_credits = smb2_add_credits,
3115 .set_credits = smb2_set_credits,
3116 .get_credits_field = smb2_get_credits_field,
3117 .get_credits = smb2_get_credits,
3118 .wait_mtu_credits = smb2_wait_mtu_credits,
3119 .get_next_mid = smb2_get_next_mid,
3120 .read_data_offset = smb2_read_data_offset,
3121 .read_data_length = smb2_read_data_length,
3122 .map_error = map_smb2_to_linux_error,
3123 .find_mid = smb2_find_mid,
3124 .check_message = smb2_check_message,
3125 .dump_detail = smb2_dump_detail,
3126 .clear_stats = smb2_clear_stats,
3127 .print_stats = smb2_print_stats,
3128 .dump_share_caps = smb2_dump_share_caps,
3129 .is_oplock_break = smb2_is_valid_oplock_break,
3130 .handle_cancelled_mid = smb2_handle_cancelled_mid,
3131 .downgrade_oplock = smb2_downgrade_oplock,
3132 .need_neg = smb2_need_neg,
3133 .negotiate = smb2_negotiate,
3134 .negotiate_wsize = smb2_negotiate_wsize,
3135 .negotiate_rsize = smb2_negotiate_rsize,
3136 .sess_setup = SMB2_sess_setup,
3137 .logoff = SMB2_logoff,
3138 .tree_connect = SMB2_tcon,
3139 .tree_disconnect = SMB2_tdis,
3140 .qfs_tcon = smb3_qfs_tcon,
3141 .is_path_accessible = smb2_is_path_accessible,
3142 .can_echo = smb2_can_echo,
3144 .query_path_info = smb2_query_path_info,
3145 .get_srv_inum = smb2_get_srv_inum,
3146 .query_file_info = smb2_query_file_info,
3147 .set_path_size = smb2_set_path_size,
3148 .set_file_size = smb2_set_file_size,
3149 .set_file_info = smb2_set_file_info,
3150 .set_compression = smb2_set_compression,
3151 .mkdir = smb2_mkdir,
3152 .mkdir_setinfo = smb2_mkdir_setinfo,
3153 .rmdir = smb2_rmdir,
3154 .unlink = smb2_unlink,
3155 .rename = smb2_rename_path,
3156 .create_hardlink = smb2_create_hardlink,
3157 .query_symlink = smb2_query_symlink,
3158 .query_mf_symlink = smb3_query_mf_symlink,
3159 .create_mf_symlink = smb3_create_mf_symlink,
3160 .open = smb2_open_file,
3161 .set_fid = smb2_set_fid,
3162 .close = smb2_close_file,
3163 .flush = smb2_flush_file,
3164 .async_readv = smb2_async_readv,
3165 .async_writev = smb2_async_writev,
3166 .sync_read = smb2_sync_read,
3167 .sync_write = smb2_sync_write,
3168 .query_dir_first = smb2_query_dir_first,
3169 .query_dir_next = smb2_query_dir_next,
3170 .close_dir = smb2_close_dir,
3171 .calc_smb_size = smb2_calc_size,
3172 .is_status_pending = smb2_is_status_pending,
3173 .is_session_expired = smb2_is_session_expired,
3174 .oplock_response = smb2_oplock_response,
3175 .queryfs = smb2_queryfs,
3176 .mand_lock = smb2_mand_lock,
3177 .mand_unlock_range = smb2_unlock_range,
3178 .push_mand_locks = smb2_push_mandatory_locks,
3179 .get_lease_key = smb2_get_lease_key,
3180 .set_lease_key = smb2_set_lease_key,
3181 .new_lease_key = smb2_new_lease_key,
3182 .generate_signingkey = generate_smb311signingkey,
3183 .calc_signature = smb3_calc_signature,
3184 .set_integrity = smb3_set_integrity,
3185 .is_read_op = smb21_is_read_op,
3186 .set_oplock_level = smb3_set_oplock_level,
3187 .create_lease_buf = smb3_create_lease_buf,
3188 .parse_lease_buf = smb3_parse_lease_buf,
3189 .copychunk_range = smb2_copychunk_range,
3190 .duplicate_extents = smb2_duplicate_extents,
3191 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
3192 .wp_retry_size = smb2_wp_retry_size,
3193 .dir_needs_close = smb2_dir_needs_close,
3194 .fallocate = smb3_fallocate,
3195 .enum_snapshots = smb3_enum_snapshots,
3196 .init_transform_rq = smb3_init_transform_rq,
3197 .free_transform_rq = smb3_free_transform_rq,
3198 .is_transform_hdr = smb3_is_transform_hdr,
3199 .receive_transform = smb3_receive_transform,
3200 .get_dfs_refer = smb2_get_dfs_refer,
3201 .select_sectype = smb2_select_sectype,
3202 #ifdef CONFIG_CIFS_XATTR
3203 .query_all_EAs = smb2_query_eas,
3204 .set_EA = smb2_set_ea,
3205 #endif /* CIFS_XATTR */
3207 #endif /* CIFS_SMB311 */
3209 struct smb_version_values smb20_values = {
3210 .version_string = SMB20_VERSION_STRING,
3211 .protocol_id = SMB20_PROT_ID,
3212 .req_capabilities = 0, /* MBZ */
3213 .large_lock_type = 0,
3214 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3215 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3216 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3217 .header_size = sizeof(struct smb2_sync_hdr),
3218 .header_preamble_size = 0,
3219 .max_header_size = MAX_SMB2_HDR_SIZE,
3220 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3221 .lock_cmd = SMB2_LOCK,
3223 .cap_nt_find = SMB2_NT_FIND,
3224 .cap_large_files = SMB2_LARGE_FILES,
3225 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3226 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3227 .create_lease_size = sizeof(struct create_lease),
3230 struct smb_version_values smb21_values = {
3231 .version_string = SMB21_VERSION_STRING,
3232 .protocol_id = SMB21_PROT_ID,
3233 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
3234 .large_lock_type = 0,
3235 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3236 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3237 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3238 .header_size = sizeof(struct smb2_sync_hdr),
3239 .header_preamble_size = 0,
3240 .max_header_size = MAX_SMB2_HDR_SIZE,
3241 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3242 .lock_cmd = SMB2_LOCK,
3244 .cap_nt_find = SMB2_NT_FIND,
3245 .cap_large_files = SMB2_LARGE_FILES,
3246 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3247 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3248 .create_lease_size = sizeof(struct create_lease),
3251 struct smb_version_values smb3any_values = {
3252 .version_string = SMB3ANY_VERSION_STRING,
3253 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3254 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
3255 .large_lock_type = 0,
3256 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3257 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3258 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3259 .header_size = sizeof(struct smb2_sync_hdr),
3260 .header_preamble_size = 0,
3261 .max_header_size = MAX_SMB2_HDR_SIZE,
3262 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3263 .lock_cmd = SMB2_LOCK,
3265 .cap_nt_find = SMB2_NT_FIND,
3266 .cap_large_files = SMB2_LARGE_FILES,
3267 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3268 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3269 .create_lease_size = sizeof(struct create_lease_v2),
3272 struct smb_version_values smbdefault_values = {
3273 .version_string = SMBDEFAULT_VERSION_STRING,
3274 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3275 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
3276 .large_lock_type = 0,
3277 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3278 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3279 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3280 .header_size = sizeof(struct smb2_sync_hdr),
3281 .header_preamble_size = 0,
3282 .max_header_size = MAX_SMB2_HDR_SIZE,
3283 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3284 .lock_cmd = SMB2_LOCK,
3286 .cap_nt_find = SMB2_NT_FIND,
3287 .cap_large_files = SMB2_LARGE_FILES,
3288 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3289 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3290 .create_lease_size = sizeof(struct create_lease_v2),
3293 struct smb_version_values smb30_values = {
3294 .version_string = SMB30_VERSION_STRING,
3295 .protocol_id = SMB30_PROT_ID,
3296 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
3297 .large_lock_type = 0,
3298 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3299 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3300 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3301 .header_size = sizeof(struct smb2_sync_hdr),
3302 .header_preamble_size = 0,
3303 .max_header_size = MAX_SMB2_HDR_SIZE,
3304 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3305 .lock_cmd = SMB2_LOCK,
3307 .cap_nt_find = SMB2_NT_FIND,
3308 .cap_large_files = SMB2_LARGE_FILES,
3309 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3310 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3311 .create_lease_size = sizeof(struct create_lease_v2),
3314 struct smb_version_values smb302_values = {
3315 .version_string = SMB302_VERSION_STRING,
3316 .protocol_id = SMB302_PROT_ID,
3317 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
3318 .large_lock_type = 0,
3319 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3320 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3321 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3322 .header_size = sizeof(struct smb2_sync_hdr),
3323 .header_preamble_size = 0,
3324 .max_header_size = MAX_SMB2_HDR_SIZE,
3325 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3326 .lock_cmd = SMB2_LOCK,
3328 .cap_nt_find = SMB2_NT_FIND,
3329 .cap_large_files = SMB2_LARGE_FILES,
3330 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3331 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3332 .create_lease_size = sizeof(struct create_lease_v2),
3335 #ifdef CONFIG_CIFS_SMB311
3336 struct smb_version_values smb311_values = {
3337 .version_string = SMB311_VERSION_STRING,
3338 .protocol_id = SMB311_PROT_ID,
3339 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
3340 .large_lock_type = 0,
3341 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3342 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3343 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3344 .header_size = sizeof(struct smb2_sync_hdr),
3345 .header_preamble_size = 0,
3346 .max_header_size = MAX_SMB2_HDR_SIZE,
3347 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3348 .lock_cmd = SMB2_LOCK,
3350 .cap_nt_find = SMB2_NT_FIND,
3351 .cap_large_files = SMB2_LARGE_FILES,
3352 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3353 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3354 .create_lease_size = sizeof(struct create_lease_v2),