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"
37 change_conf(struct TCP_Server_Info *server)
39 server->credits += server->echo_credits + server->oplock_credits;
40 server->oplock_credits = server->echo_credits = 0;
41 switch (server->credits) {
45 server->echoes = false;
46 server->oplocks = false;
47 cifs_dbg(VFS, "disabling echoes and oplocks\n");
50 server->echoes = true;
51 server->oplocks = false;
52 server->echo_credits = 1;
53 cifs_dbg(FYI, "disabling oplocks\n");
56 server->echoes = true;
58 server->oplocks = true;
59 server->oplock_credits = 1;
61 server->oplocks = false;
63 server->echo_credits = 1;
65 server->credits -= server->echo_credits + server->oplock_credits;
70 smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
74 spin_lock(&server->req_lock);
75 val = server->ops->get_credits_field(server, optype);
78 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
79 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
82 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
83 rc = change_conf(server);
85 * Sometimes server returns 0 credits on oplock break ack - we need to
86 * rebalance credits in this case.
88 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
90 if (server->credits > 1) {
92 server->oplock_credits++;
95 spin_unlock(&server->req_lock);
96 wake_up(&server->request_q);
98 cifs_reconnect(server);
102 smb2_set_credits(struct TCP_Server_Info *server, const int val)
104 spin_lock(&server->req_lock);
105 server->credits = val;
106 spin_unlock(&server->req_lock);
110 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
114 return &server->echo_credits;
116 return &server->oplock_credits;
118 return &server->credits;
123 smb2_get_credits(struct mid_q_entry *mid)
125 struct smb2_sync_hdr *shdr = get_sync_hdr(mid->resp_buf);
127 return le16_to_cpu(shdr->CreditRequest);
131 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
132 unsigned int *num, unsigned int *credits)
135 unsigned int scredits;
137 spin_lock(&server->req_lock);
139 if (server->credits <= 0) {
140 spin_unlock(&server->req_lock);
141 cifs_num_waiters_inc(server);
142 rc = wait_event_killable(server->request_q,
143 has_credits(server, &server->credits));
144 cifs_num_waiters_dec(server);
147 spin_lock(&server->req_lock);
149 if (server->tcpStatus == CifsExiting) {
150 spin_unlock(&server->req_lock);
154 scredits = server->credits;
155 /* can deadlock with reopen */
157 *num = SMB2_MAX_BUFFER_SIZE;
162 /* leave some credits for reopen and other ops */
164 *num = min_t(unsigned int, size,
165 scredits * SMB2_MAX_BUFFER_SIZE);
167 *credits = DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
168 server->credits -= *credits;
173 spin_unlock(&server->req_lock);
178 smb2_get_next_mid(struct TCP_Server_Info *server)
181 /* for SMB2 we need the current value */
182 spin_lock(&GlobalMid_Lock);
183 mid = server->CurrentMid++;
184 spin_unlock(&GlobalMid_Lock);
188 static struct mid_q_entry *
189 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
191 struct mid_q_entry *mid;
192 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
193 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
195 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
196 cifs_dbg(VFS, "encrypted frame parsing not supported yet");
200 spin_lock(&GlobalMid_Lock);
201 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
202 if ((mid->mid == wire_mid) &&
203 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
204 (mid->command == shdr->Command)) {
205 kref_get(&mid->refcount);
206 spin_unlock(&GlobalMid_Lock);
210 spin_unlock(&GlobalMid_Lock);
215 smb2_dump_detail(void *buf)
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, smb2_calc_size(buf));
228 smb2_need_neg(struct TCP_Server_Info *server)
230 return server->max_read == 0;
234 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
237 ses->server->CurrentMid = 0;
238 rc = SMB2_negotiate(xid, ses);
239 /* BB we probably don't need to retry with modern servers */
246 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
248 struct TCP_Server_Info *server = tcon->ses->server;
251 /* start with specified wsize, or default */
252 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
253 wsize = min_t(unsigned int, wsize, server->max_write);
255 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
256 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
262 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
264 struct TCP_Server_Info *server = tcon->ses->server;
267 /* start with specified rsize, or default */
268 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
269 rsize = min_t(unsigned int, rsize, server->max_read);
271 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
272 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
277 #ifdef CONFIG_CIFS_STATS2
279 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
282 unsigned int ret_data_len = 0;
283 struct network_interface_info_ioctl_rsp *out_buf;
285 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
286 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
288 NULL /* no data input */, 0 /* no data input */,
289 (char **)&out_buf, &ret_data_len);
291 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
292 else if (ret_data_len < sizeof(struct network_interface_info_ioctl_rsp)) {
293 cifs_dbg(VFS, "server returned bad net interface info buf\n");
296 /* Dump info on first interface */
297 cifs_dbg(FYI, "Adapter Capability 0x%x\t",
298 le32_to_cpu(out_buf->Capability));
299 cifs_dbg(FYI, "Link Speed %lld\n",
300 le64_to_cpu(out_buf->LinkSpeed));
308 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
311 __le16 srch_path = 0; /* Null - open root of share */
312 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
313 struct cifs_open_parms oparms;
317 oparms.desired_access = FILE_READ_ATTRIBUTES;
318 oparms.disposition = FILE_OPEN;
319 oparms.create_options = 0;
321 oparms.reconnect = false;
323 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
327 #ifdef CONFIG_CIFS_STATS2
328 SMB3_request_interfaces(xid, tcon);
331 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
332 FS_ATTRIBUTE_INFORMATION);
333 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
334 FS_DEVICE_INFORMATION);
335 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
336 FS_VOLUME_INFORMATION);
337 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
338 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
339 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
344 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
347 __le16 srch_path = 0; /* Null - open root of share */
348 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
349 struct cifs_open_parms oparms;
353 oparms.desired_access = FILE_READ_ATTRIBUTES;
354 oparms.disposition = FILE_OPEN;
355 oparms.create_options = 0;
357 oparms.reconnect = false;
359 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
363 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
364 FS_ATTRIBUTE_INFORMATION);
365 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
366 FS_DEVICE_INFORMATION);
367 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
372 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
373 struct cifs_sb_info *cifs_sb, const char *full_path)
377 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
378 struct cifs_open_parms oparms;
381 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
386 oparms.desired_access = FILE_READ_ATTRIBUTES;
387 oparms.disposition = FILE_OPEN;
388 if (backup_cred(cifs_sb))
389 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
391 oparms.create_options = 0;
393 oparms.reconnect = false;
395 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
401 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
407 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
408 struct cifs_sb_info *cifs_sb, const char *full_path,
409 u64 *uniqueid, FILE_ALL_INFO *data)
411 *uniqueid = le64_to_cpu(data->IndexNumber);
416 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
417 struct cifs_fid *fid, FILE_ALL_INFO *data)
420 struct smb2_file_all_info *smb2_data;
422 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
424 if (smb2_data == NULL)
427 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
430 move_smb2_info_to_cifs(data, smb2_data);
435 #ifdef CONFIG_CIFS_XATTR
437 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
438 struct smb2_file_full_ea_info *src, size_t src_size,
439 const unsigned char *ea_name)
442 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
444 size_t buf_size = dst_size;
445 size_t name_len, value_len, user_name_len;
447 while (src_size > 0) {
448 name = &src->ea_data[0];
449 name_len = (size_t)src->ea_name_length;
450 value = &src->ea_data[src->ea_name_length + 1];
451 value_len = (size_t)le16_to_cpu(src->ea_value_length);
457 if (src_size < 8 + name_len + 1 + value_len) {
458 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
464 if (ea_name_len == name_len &&
465 memcmp(ea_name, name, name_len) == 0) {
469 if (dst_size < value_len) {
473 memcpy(dst, value, value_len);
477 /* 'user.' plus a terminating null */
478 user_name_len = 5 + 1 + name_len;
481 /* skip copy - calc size only */
483 } else if (dst_size >= user_name_len) {
484 dst_size -= user_name_len;
485 memcpy(dst, "user.", 5);
487 memcpy(dst, src->ea_data, name_len);
493 /* stop before overrun buffer */
499 if (!src->next_entry_offset)
502 if (src_size < le32_to_cpu(src->next_entry_offset)) {
503 /* stop before overrun buffer */
507 src_size -= le32_to_cpu(src->next_entry_offset);
508 src = (void *)((char *)src +
509 le32_to_cpu(src->next_entry_offset));
512 /* didn't find the named attribute */
521 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
522 const unsigned char *path, const unsigned char *ea_name,
523 char *ea_data, size_t buf_size,
524 struct cifs_sb_info *cifs_sb)
528 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
529 struct cifs_open_parms oparms;
531 struct smb2_file_full_ea_info *smb2_data;
532 int ea_buf_size = SMB2_MIN_EA_BUF;
534 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
539 oparms.desired_access = FILE_READ_EA;
540 oparms.disposition = FILE_OPEN;
541 if (backup_cred(cifs_sb))
542 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
544 oparms.create_options = 0;
546 oparms.reconnect = false;
548 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
551 cifs_dbg(FYI, "open failed rc=%d\n", rc);
556 smb2_data = kzalloc(ea_buf_size, GFP_KERNEL);
557 if (smb2_data == NULL) {
558 SMB2_close(xid, tcon, fid.persistent_fid,
563 rc = SMB2_query_eas(xid, tcon, fid.persistent_fid,
565 ea_buf_size, smb2_data);
573 if (ea_buf_size > SMB2_MAX_EA_BUF) {
574 cifs_dbg(VFS, "EA size is too large\n");
575 SMB2_close(xid, tcon, fid.persistent_fid,
581 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
584 * If ea_name is NULL (listxattr) and there are no EAs, return 0 as it's
585 * not an error. Otherwise, the specified ea_name was not found.
588 rc = move_smb2_ea_to_cifs(ea_data, buf_size, smb2_data,
589 SMB2_MAX_EA_BUF, ea_name);
590 else if (!ea_name && rc == -ENODATA)
599 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
600 const char *path, const char *ea_name, const void *ea_value,
601 const __u16 ea_value_len, const struct nls_table *nls_codepage,
602 struct cifs_sb_info *cifs_sb)
606 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
607 struct cifs_open_parms oparms;
609 struct smb2_file_full_ea_info *ea;
610 int ea_name_len = strlen(ea_name);
613 if (ea_name_len > 255)
616 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
621 oparms.desired_access = FILE_WRITE_EA;
622 oparms.disposition = FILE_OPEN;
623 if (backup_cred(cifs_sb))
624 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
626 oparms.create_options = 0;
628 oparms.reconnect = false;
630 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
633 cifs_dbg(FYI, "open failed rc=%d\n", rc);
637 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
638 ea = kzalloc(len, GFP_KERNEL);
640 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
644 ea->ea_name_length = ea_name_len;
645 ea->ea_value_length = cpu_to_le16(ea_value_len);
646 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
647 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
649 rc = SMB2_set_ea(xid, tcon, fid.persistent_fid, fid.volatile_fid, ea,
653 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
660 smb2_can_echo(struct TCP_Server_Info *server)
662 return server->echoes;
666 smb2_clear_stats(struct cifs_tcon *tcon)
668 #ifdef CONFIG_CIFS_STATS
670 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
671 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
672 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
678 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
680 seq_puts(m, "\n\tShare Capabilities:");
681 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
682 seq_puts(m, " DFS,");
683 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
684 seq_puts(m, " CONTINUOUS AVAILABILITY,");
685 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
686 seq_puts(m, " SCALEOUT,");
687 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
688 seq_puts(m, " CLUSTER,");
689 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
690 seq_puts(m, " ASYMMETRIC,");
691 if (tcon->capabilities == 0)
692 seq_puts(m, " None");
693 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
694 seq_puts(m, " Aligned,");
695 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
696 seq_puts(m, " Partition Aligned,");
697 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
698 seq_puts(m, " SSD,");
699 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
700 seq_puts(m, " TRIM-support,");
702 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
703 if (tcon->perf_sector_size)
704 seq_printf(m, "\tOptimal sector size: 0x%x",
705 tcon->perf_sector_size);
709 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
711 #ifdef CONFIG_CIFS_STATS
712 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
713 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
714 seq_printf(m, "\nNegotiates: %d sent %d failed",
715 atomic_read(&sent[SMB2_NEGOTIATE_HE]),
716 atomic_read(&failed[SMB2_NEGOTIATE_HE]));
717 seq_printf(m, "\nSessionSetups: %d sent %d failed",
718 atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
719 atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
720 seq_printf(m, "\nLogoffs: %d sent %d failed",
721 atomic_read(&sent[SMB2_LOGOFF_HE]),
722 atomic_read(&failed[SMB2_LOGOFF_HE]));
723 seq_printf(m, "\nTreeConnects: %d sent %d failed",
724 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
725 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
726 seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
727 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
728 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
729 seq_printf(m, "\nCreates: %d sent %d failed",
730 atomic_read(&sent[SMB2_CREATE_HE]),
731 atomic_read(&failed[SMB2_CREATE_HE]));
732 seq_printf(m, "\nCloses: %d sent %d failed",
733 atomic_read(&sent[SMB2_CLOSE_HE]),
734 atomic_read(&failed[SMB2_CLOSE_HE]));
735 seq_printf(m, "\nFlushes: %d sent %d failed",
736 atomic_read(&sent[SMB2_FLUSH_HE]),
737 atomic_read(&failed[SMB2_FLUSH_HE]));
738 seq_printf(m, "\nReads: %d sent %d failed",
739 atomic_read(&sent[SMB2_READ_HE]),
740 atomic_read(&failed[SMB2_READ_HE]));
741 seq_printf(m, "\nWrites: %d sent %d failed",
742 atomic_read(&sent[SMB2_WRITE_HE]),
743 atomic_read(&failed[SMB2_WRITE_HE]));
744 seq_printf(m, "\nLocks: %d sent %d failed",
745 atomic_read(&sent[SMB2_LOCK_HE]),
746 atomic_read(&failed[SMB2_LOCK_HE]));
747 seq_printf(m, "\nIOCTLs: %d sent %d failed",
748 atomic_read(&sent[SMB2_IOCTL_HE]),
749 atomic_read(&failed[SMB2_IOCTL_HE]));
750 seq_printf(m, "\nCancels: %d sent %d failed",
751 atomic_read(&sent[SMB2_CANCEL_HE]),
752 atomic_read(&failed[SMB2_CANCEL_HE]));
753 seq_printf(m, "\nEchos: %d sent %d failed",
754 atomic_read(&sent[SMB2_ECHO_HE]),
755 atomic_read(&failed[SMB2_ECHO_HE]));
756 seq_printf(m, "\nQueryDirectories: %d sent %d failed",
757 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
758 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
759 seq_printf(m, "\nChangeNotifies: %d sent %d failed",
760 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
761 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
762 seq_printf(m, "\nQueryInfos: %d sent %d failed",
763 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
764 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
765 seq_printf(m, "\nSetInfos: %d sent %d failed",
766 atomic_read(&sent[SMB2_SET_INFO_HE]),
767 atomic_read(&failed[SMB2_SET_INFO_HE]));
768 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
769 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
770 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
775 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
777 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
778 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
780 cfile->fid.persistent_fid = fid->persistent_fid;
781 cfile->fid.volatile_fid = fid->volatile_fid;
782 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
784 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
785 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
789 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
790 struct cifs_fid *fid)
792 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
796 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
797 u64 persistent_fid, u64 volatile_fid,
798 struct copychunk_ioctl *pcchunk)
801 unsigned int ret_data_len;
802 struct resume_key_req *res_key;
804 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
805 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
807 NULL, 0 /* no input */,
808 (char **)&res_key, &ret_data_len);
811 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
812 goto req_res_key_exit;
814 if (ret_data_len < sizeof(struct resume_key_req)) {
815 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
817 goto req_res_key_exit;
819 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
827 smb2_copychunk_range(const unsigned int xid,
828 struct cifsFileInfo *srcfile,
829 struct cifsFileInfo *trgtfile, u64 src_off,
830 u64 len, u64 dest_off)
833 unsigned int ret_data_len;
834 struct copychunk_ioctl *pcchunk;
835 struct copychunk_ioctl_rsp *retbuf = NULL;
836 struct cifs_tcon *tcon;
837 int chunks_copied = 0;
838 bool chunk_sizes_updated = false;
839 ssize_t bytes_written, total_bytes_written = 0;
841 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
846 cifs_dbg(FYI, "in smb2_copychunk_range - about to call request res key\n");
847 /* Request a key from the server to identify the source of the copy */
848 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
849 srcfile->fid.persistent_fid,
850 srcfile->fid.volatile_fid, pcchunk);
852 /* Note: request_res_key sets res_key null only if rc !=0 */
856 /* For now array only one chunk long, will make more flexible later */
857 pcchunk->ChunkCount = cpu_to_le32(1);
858 pcchunk->Reserved = 0;
859 pcchunk->Reserved2 = 0;
861 tcon = tlink_tcon(trgtfile->tlink);
864 pcchunk->SourceOffset = cpu_to_le64(src_off);
865 pcchunk->TargetOffset = cpu_to_le64(dest_off);
867 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
869 /* Request server copy to target from src identified by key */
870 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
871 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
872 true /* is_fsctl */, false /* use_ipc */,
874 sizeof(struct copychunk_ioctl), (char **)&retbuf,
878 sizeof(struct copychunk_ioctl_rsp)) {
879 cifs_dbg(VFS, "invalid cchunk response size\n");
883 if (retbuf->TotalBytesWritten == 0) {
884 cifs_dbg(FYI, "no bytes copied\n");
889 * Check if server claimed to write more than we asked
891 if (le32_to_cpu(retbuf->TotalBytesWritten) >
892 le32_to_cpu(pcchunk->Length)) {
893 cifs_dbg(VFS, "invalid copy chunk response\n");
897 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
898 cifs_dbg(VFS, "invalid num chunks written\n");
904 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
905 src_off += bytes_written;
906 dest_off += bytes_written;
907 len -= bytes_written;
908 total_bytes_written += bytes_written;
910 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
911 le32_to_cpu(retbuf->ChunksWritten),
912 le32_to_cpu(retbuf->ChunkBytesWritten),
914 } else if (rc == -EINVAL) {
915 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
918 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
919 le32_to_cpu(retbuf->ChunksWritten),
920 le32_to_cpu(retbuf->ChunkBytesWritten),
921 le32_to_cpu(retbuf->TotalBytesWritten));
924 * Check if this is the first request using these sizes,
925 * (ie check if copy succeed once with original sizes
926 * and check if the server gave us different sizes after
927 * we already updated max sizes on previous request).
928 * if not then why is the server returning an error now
930 if ((chunks_copied != 0) || chunk_sizes_updated)
933 /* Check that server is not asking us to grow size */
934 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
935 tcon->max_bytes_chunk)
936 tcon->max_bytes_chunk =
937 le32_to_cpu(retbuf->ChunkBytesWritten);
939 goto cchunk_out; /* server gave us bogus size */
941 /* No need to change MaxChunks since already set to 1 */
942 chunk_sizes_updated = true;
953 return total_bytes_written;
957 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
958 struct cifs_fid *fid)
960 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
964 smb2_read_data_offset(char *buf)
966 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
967 return rsp->DataOffset;
971 smb2_read_data_length(char *buf)
973 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
974 return le32_to_cpu(rsp->DataLength);
979 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
980 struct cifs_io_parms *parms, unsigned int *bytes_read,
981 char **buf, int *buf_type)
983 parms->persistent_fid = pfid->persistent_fid;
984 parms->volatile_fid = pfid->volatile_fid;
985 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
989 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
990 struct cifs_io_parms *parms, unsigned int *written,
991 struct kvec *iov, unsigned long nr_segs)
994 parms->persistent_fid = pfid->persistent_fid;
995 parms->volatile_fid = pfid->volatile_fid;
996 return SMB2_write(xid, parms, written, iov, nr_segs);
999 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1000 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1001 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1003 struct cifsInodeInfo *cifsi;
1006 cifsi = CIFS_I(inode);
1008 /* if file already sparse don't bother setting sparse again */
1009 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1010 return true; /* already sparse */
1012 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1013 return true; /* already not sparse */
1016 * Can't check for sparse support on share the usual way via the
1017 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1018 * since Samba server doesn't set the flag on the share, yet
1019 * supports the set sparse FSCTL and returns sparse correctly
1020 * in the file attributes. If we fail setting sparse though we
1021 * mark that server does not support sparse files for this share
1022 * to avoid repeatedly sending the unsupported fsctl to server
1023 * if the file is repeatedly extended.
1025 if (tcon->broken_sparse_sup)
1028 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1029 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1030 true /* is_fctl */, false /* use_ipc */,
1031 &setsparse, 1, NULL, NULL);
1033 tcon->broken_sparse_sup = true;
1034 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1039 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1041 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1047 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1048 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1050 __le64 eof = cpu_to_le64(size);
1051 struct inode *inode;
1054 * If extending file more than one page make sparse. Many Linux fs
1055 * make files sparse by default when extending via ftruncate
1057 inode = d_inode(cfile->dentry);
1059 if (!set_alloc && (size > inode->i_size + 8192)) {
1060 __u8 set_sparse = 1;
1062 /* whether set sparse succeeds or not, extend the file */
1063 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1066 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1067 cfile->fid.volatile_fid, cfile->pid, &eof, false);
1071 smb2_duplicate_extents(const unsigned int xid,
1072 struct cifsFileInfo *srcfile,
1073 struct cifsFileInfo *trgtfile, u64 src_off,
1074 u64 len, u64 dest_off)
1077 unsigned int ret_data_len;
1078 struct duplicate_extents_to_file dup_ext_buf;
1079 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1081 /* server fileays advertise duplicate extent support with this flag */
1082 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1083 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1086 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1087 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1088 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1089 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1090 dup_ext_buf.ByteCount = cpu_to_le64(len);
1091 cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
1092 src_off, dest_off, len);
1094 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1096 goto duplicate_extents_out;
1098 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1099 trgtfile->fid.volatile_fid,
1100 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
1101 true /* is_fsctl */, false /* use_ipc */,
1102 (char *)&dup_ext_buf,
1103 sizeof(struct duplicate_extents_to_file),
1107 if (ret_data_len > 0)
1108 cifs_dbg(FYI, "non-zero response length in duplicate extents");
1110 duplicate_extents_out:
1115 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1116 struct cifsFileInfo *cfile)
1118 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1119 cfile->fid.volatile_fid);
1123 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1124 struct cifsFileInfo *cfile)
1126 struct fsctl_set_integrity_information_req integr_info;
1127 unsigned int ret_data_len;
1129 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1130 integr_info.Flags = 0;
1131 integr_info.Reserved = 0;
1133 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1134 cfile->fid.volatile_fid,
1135 FSCTL_SET_INTEGRITY_INFORMATION,
1136 true /* is_fsctl */, false /* use_ipc */,
1137 (char *)&integr_info,
1138 sizeof(struct fsctl_set_integrity_information_req),
1144 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
1145 #define GMT_TOKEN_SIZE 50
1148 * Input buffer contains (empty) struct smb_snapshot array with size filled in
1149 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
1152 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1153 struct cifsFileInfo *cfile, void __user *ioc_buf)
1155 char *retbuf = NULL;
1156 unsigned int ret_data_len = 0;
1158 struct smb_snapshot_array snapshot_in;
1160 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1161 cfile->fid.volatile_fid,
1162 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
1163 true /* is_fsctl */, false /* use_ipc */,
1164 NULL, 0 /* no input data */,
1167 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1172 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1174 if (copy_from_user(&snapshot_in, ioc_buf,
1175 sizeof(struct smb_snapshot_array))) {
1182 * Check for min size, ie not large enough to fit even one GMT
1183 * token (snapshot). On the first ioctl some users may pass in
1184 * smaller size (or zero) to simply get the size of the array
1185 * so the user space caller can allocate sufficient memory
1186 * and retry the ioctl again with larger array size sufficient
1187 * to hold all of the snapshot GMT tokens on the second try.
1189 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
1190 ret_data_len = sizeof(struct smb_snapshot_array);
1193 * We return struct SRV_SNAPSHOT_ARRAY, followed by
1194 * the snapshot array (of 50 byte GMT tokens) each
1195 * representing an available previous version of the data
1197 if (ret_data_len > (snapshot_in.snapshot_array_size +
1198 sizeof(struct smb_snapshot_array)))
1199 ret_data_len = snapshot_in.snapshot_array_size +
1200 sizeof(struct smb_snapshot_array);
1202 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1211 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1212 const char *path, struct cifs_sb_info *cifs_sb,
1213 struct cifs_fid *fid, __u16 search_flags,
1214 struct cifs_search_info *srch_inf)
1218 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1219 struct cifs_open_parms oparms;
1221 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1226 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1227 oparms.disposition = FILE_OPEN;
1228 if (backup_cred(cifs_sb))
1229 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1231 oparms.create_options = 0;
1233 oparms.reconnect = false;
1235 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
1238 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
1242 srch_inf->entries_in_buffer = 0;
1243 srch_inf->index_of_last_entry = 2;
1245 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1246 fid->volatile_fid, 0, srch_inf);
1248 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
1249 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1255 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1256 struct cifs_fid *fid, __u16 search_flags,
1257 struct cifs_search_info *srch_inf)
1259 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1260 fid->volatile_fid, 0, srch_inf);
1264 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1265 struct cifs_fid *fid)
1267 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1271 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
1272 * the number of credits and return true. Otherwise - return false.
1275 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
1277 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
1279 if (shdr->Status != STATUS_PENDING)
1283 spin_lock(&server->req_lock);
1284 server->credits += le16_to_cpu(shdr->CreditRequest);
1285 spin_unlock(&server->req_lock);
1286 wake_up(&server->request_q);
1293 smb2_is_session_expired(char *buf)
1295 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
1297 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
1298 shdr->Status != STATUS_USER_SESSION_DELETED)
1301 cifs_dbg(FYI, "Session expired or deleted\n");
1306 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
1307 struct cifsInodeInfo *cinode)
1309 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
1310 return SMB2_lease_break(0, tcon, cinode->lease_key,
1311 smb2_get_lease_state(cinode));
1313 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
1315 CIFS_CACHE_READ(cinode) ? 1 : 0);
1319 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1320 struct kstatfs *buf)
1323 __le16 srch_path = 0; /* Null - open root of share */
1324 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1325 struct cifs_open_parms oparms;
1326 struct cifs_fid fid;
1329 oparms.desired_access = FILE_READ_ATTRIBUTES;
1330 oparms.disposition = FILE_OPEN;
1331 oparms.create_options = 0;
1333 oparms.reconnect = false;
1335 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
1338 buf->f_type = SMB2_MAGIC_NUMBER;
1339 rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1341 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1346 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
1348 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
1349 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
1353 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
1354 __u64 length, __u32 type, int lock, int unlock, bool wait)
1356 if (unlock && !lock)
1357 type = SMB2_LOCKFLAG_UNLOCK;
1358 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
1359 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
1360 current->tgid, length, offset, type, wait);
1364 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
1366 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
1370 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
1372 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
1376 smb2_new_lease_key(struct cifs_fid *fid)
1378 generate_random_uuid(fid->lease_key);
1382 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
1383 const char *search_name,
1384 struct dfs_info3_param **target_nodes,
1385 unsigned int *num_of_nodes,
1386 const struct nls_table *nls_codepage, int remap)
1389 __le16 *utf16_path = NULL;
1390 int utf16_path_len = 0;
1391 struct cifs_tcon *tcon;
1392 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
1393 struct get_dfs_referral_rsp *dfs_rsp = NULL;
1394 u32 dfs_req_size = 0, dfs_rsp_size = 0;
1396 cifs_dbg(FYI, "smb2_get_dfs_refer path <%s>\n", search_name);
1399 * Use any tcon from the current session. Here, the first one.
1401 spin_lock(&cifs_tcp_ses_lock);
1402 tcon = list_first_entry_or_null(&ses->tcon_list, struct cifs_tcon,
1406 spin_unlock(&cifs_tcp_ses_lock);
1409 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
1415 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
1417 nls_codepage, remap);
1423 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
1424 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
1430 /* Highest DFS referral version understood */
1431 dfs_req->MaxReferralLevel = DFS_VERSION;
1433 /* Path to resolve in an UTF-16 null-terminated string */
1434 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
1437 /* try first with IPC */
1438 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1439 FSCTL_DFS_GET_REFERRALS,
1440 true /* is_fsctl */, true /* use_ipc */,
1441 (char *)dfs_req, dfs_req_size,
1442 (char **)&dfs_rsp, &dfs_rsp_size);
1443 if (rc == -ENOTCONN) {
1444 /* try with normal tcon */
1445 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1446 FSCTL_DFS_GET_REFERRALS,
1447 true /* is_fsctl */, false /*use_ipc*/,
1448 (char *)dfs_req, dfs_req_size,
1449 (char **)&dfs_rsp, &dfs_rsp_size);
1451 } while (rc == -EAGAIN);
1454 cifs_dbg(VFS, "ioctl error in smb2_get_dfs_refer rc=%d\n", rc);
1458 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
1459 num_of_nodes, target_nodes,
1460 nls_codepage, remap, search_name,
1461 true /* is_unicode */);
1463 cifs_dbg(VFS, "parse error in smb2_get_dfs_refer rc=%d\n", rc);
1469 spin_lock(&cifs_tcp_ses_lock);
1471 spin_unlock(&cifs_tcp_ses_lock);
1478 #define SMB2_SYMLINK_STRUCT_SIZE \
1479 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
1482 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
1483 const char *full_path, char **target_path,
1484 struct cifs_sb_info *cifs_sb)
1488 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1489 struct cifs_open_parms oparms;
1490 struct cifs_fid fid;
1491 struct smb2_err_rsp *err_buf = NULL;
1492 struct smb2_symlink_err_rsp *symlink;
1493 unsigned int sub_len;
1494 unsigned int sub_offset;
1495 unsigned int print_len;
1496 unsigned int print_offset;
1498 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
1500 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1505 oparms.desired_access = FILE_READ_ATTRIBUTES;
1506 oparms.disposition = FILE_OPEN;
1507 if (backup_cred(cifs_sb))
1508 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1510 oparms.create_options = 0;
1512 oparms.reconnect = false;
1514 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_buf);
1516 if (!rc || !err_buf) {
1521 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
1522 get_rfc1002_length(err_buf) + 4 < SMB2_SYMLINK_STRUCT_SIZE) {
1527 /* open must fail on symlink - reset rc */
1529 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
1530 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
1531 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
1532 print_len = le16_to_cpu(symlink->PrintNameLength);
1533 print_offset = le16_to_cpu(symlink->PrintNameOffset);
1535 if (get_rfc1002_length(err_buf) + 4 <
1536 SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
1541 if (get_rfc1002_length(err_buf) + 4 <
1542 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
1547 *target_path = cifs_strndup_from_utf16(
1548 (char *)symlink->PathBuffer + sub_offset,
1549 sub_len, true, cifs_sb->local_nls);
1550 if (!(*target_path)) {
1554 convert_delimiter(*target_path, '/');
1555 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
1560 #ifdef CONFIG_CIFS_ACL
1561 static struct cifs_ntsd *
1562 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
1563 const struct cifs_fid *cifsfid, u32 *pacllen)
1565 struct cifs_ntsd *pntsd = NULL;
1567 int rc = -EOPNOTSUPP;
1568 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1571 return ERR_CAST(tlink);
1574 cifs_dbg(FYI, "trying to get acl\n");
1576 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
1577 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
1580 cifs_put_tlink(tlink);
1582 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
1589 static struct cifs_ntsd *
1590 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
1591 const char *path, u32 *pacllen)
1593 struct cifs_ntsd *pntsd = NULL;
1594 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1597 struct cifs_tcon *tcon;
1598 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1599 struct cifs_fid fid;
1600 struct cifs_open_parms oparms;
1603 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
1605 return ERR_CAST(tlink);
1607 tcon = tlink_tcon(tlink);
1610 if (backup_cred(cifs_sb))
1611 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1613 oparms.create_options = 0;
1615 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1623 oparms.desired_access = READ_CONTROL;
1624 oparms.disposition = FILE_OPEN;
1626 oparms.reconnect = false;
1628 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
1631 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
1632 fid.volatile_fid, (void **)&pntsd, pacllen);
1633 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1636 cifs_put_tlink(tlink);
1639 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
1645 #ifdef CONFIG_CIFS_ACL
1647 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
1648 struct inode *inode, const char *path, int aclflag)
1650 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1652 int rc, access_flags = 0;
1653 struct cifs_tcon *tcon;
1654 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1655 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1656 struct cifs_fid fid;
1657 struct cifs_open_parms oparms;
1660 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
1662 return PTR_ERR(tlink);
1664 tcon = tlink_tcon(tlink);
1667 if (backup_cred(cifs_sb))
1668 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1670 oparms.create_options = 0;
1672 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
1673 access_flags = WRITE_OWNER;
1675 access_flags = WRITE_DAC;
1677 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1685 oparms.desired_access = access_flags;
1686 oparms.disposition = FILE_OPEN;
1689 oparms.reconnect = false;
1691 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
1694 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
1695 fid.volatile_fid, pnntsd, acllen, aclflag);
1696 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1699 cifs_put_tlink(tlink);
1703 #endif /* CIFS_ACL */
1705 /* Retrieve an ACL from the server */
1706 static struct cifs_ntsd *
1707 get_smb2_acl(struct cifs_sb_info *cifs_sb,
1708 struct inode *inode, const char *path,
1711 struct cifs_ntsd *pntsd = NULL;
1712 struct cifsFileInfo *open_file = NULL;
1715 open_file = find_readable_file(CIFS_I(inode), true);
1717 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
1719 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
1720 cifsFileInfo_put(open_file);
1725 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
1726 loff_t offset, loff_t len, bool keep_size)
1728 struct inode *inode;
1729 struct cifsInodeInfo *cifsi;
1730 struct cifsFileInfo *cfile = file->private_data;
1731 struct file_zero_data_information fsctl_buf;
1737 inode = d_inode(cfile->dentry);
1738 cifsi = CIFS_I(inode);
1740 /* if file not oplocked can't be sure whether asking to extend size */
1741 if (!CIFS_CACHE_READ(cifsi))
1742 if (keep_size == false) {
1749 * Must check if file sparse since fallocate -z (zero range) assumes
1750 * non-sparse allocation
1752 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
1759 * need to make sure we are not asked to extend the file since the SMB3
1760 * fsctl does not change the file size. In the future we could change
1761 * this to zero the first part of the range then set the file size
1762 * which for a non sparse file would zero the newly extended range
1764 if (keep_size == false)
1765 if (i_size_read(inode) < offset + len) {
1771 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1773 fsctl_buf.FileOffset = cpu_to_le64(offset);
1774 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1776 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1777 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
1778 true /* is_fctl */, false /* use_ipc */,
1780 sizeof(struct file_zero_data_information), NULL, NULL);
1785 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
1786 loff_t offset, loff_t len)
1788 struct inode *inode;
1789 struct cifsInodeInfo *cifsi;
1790 struct cifsFileInfo *cfile = file->private_data;
1791 struct file_zero_data_information fsctl_buf;
1794 __u8 set_sparse = 1;
1798 inode = d_inode(cfile->dentry);
1799 cifsi = CIFS_I(inode);
1801 /* Need to make file sparse, if not already, before freeing range. */
1802 /* Consider adding equivalent for compressed since it could also work */
1803 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
1809 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1811 fsctl_buf.FileOffset = cpu_to_le64(offset);
1812 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1814 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1815 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
1816 true /* is_fctl */, false /* use_ipc */,
1818 sizeof(struct file_zero_data_information), NULL, NULL);
1823 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
1824 loff_t off, loff_t len, bool keep_size)
1826 struct inode *inode;
1827 struct cifsInodeInfo *cifsi;
1828 struct cifsFileInfo *cfile = file->private_data;
1829 long rc = -EOPNOTSUPP;
1834 inode = d_inode(cfile->dentry);
1835 cifsi = CIFS_I(inode);
1837 /* if file not oplocked can't be sure whether asking to extend size */
1838 if (!CIFS_CACHE_READ(cifsi))
1839 if (keep_size == false) {
1845 * Files are non-sparse by default so falloc may be a no-op
1846 * Must check if file sparse. If not sparse, and not extending
1847 * then no need to do anything since file already allocated
1849 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
1850 if (keep_size == true)
1852 /* check if extending file */
1853 else if (i_size_read(inode) >= off + len)
1854 /* not extending file and already not sparse */
1856 /* BB: in future add else clause to extend file */
1863 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
1865 * Check if falloc starts within first few pages of file
1866 * and ends within a few pages of the end of file to
1867 * ensure that most of file is being forced to be
1868 * fallocated now. If so then setting whole file sparse
1869 * ie potentially making a few extra pages at the beginning
1870 * or end of the file non-sparse via set_sparse is harmless.
1872 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
1878 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
1880 /* BB: else ... in future add code to extend file and set sparse */
1888 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
1889 loff_t off, loff_t len)
1891 /* KEEP_SIZE already checked for by do_fallocate */
1892 if (mode & FALLOC_FL_PUNCH_HOLE)
1893 return smb3_punch_hole(file, tcon, off, len);
1894 else if (mode & FALLOC_FL_ZERO_RANGE) {
1895 if (mode & FALLOC_FL_KEEP_SIZE)
1896 return smb3_zero_range(file, tcon, off, len, true);
1897 return smb3_zero_range(file, tcon, off, len, false);
1898 } else if (mode == FALLOC_FL_KEEP_SIZE)
1899 return smb3_simple_falloc(file, tcon, off, len, true);
1901 return smb3_simple_falloc(file, tcon, off, len, false);
1907 smb2_downgrade_oplock(struct TCP_Server_Info *server,
1908 struct cifsInodeInfo *cinode, bool set_level2)
1911 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
1914 server->ops->set_oplock_level(cinode, 0, 0, NULL);
1918 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1919 unsigned int epoch, bool *purge_cache)
1922 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1924 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1925 cinode->oplock = CIFS_CACHE_RHW_FLG;
1926 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
1927 &cinode->vfs_inode);
1928 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
1929 cinode->oplock = CIFS_CACHE_RW_FLG;
1930 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
1931 &cinode->vfs_inode);
1932 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
1933 cinode->oplock = CIFS_CACHE_READ_FLG;
1934 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
1935 &cinode->vfs_inode);
1941 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1942 unsigned int epoch, bool *purge_cache)
1944 char message[5] = {0};
1947 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1951 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
1952 cinode->oplock |= CIFS_CACHE_READ_FLG;
1953 strcat(message, "R");
1955 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
1956 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
1957 strcat(message, "H");
1959 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
1960 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
1961 strcat(message, "W");
1963 if (!cinode->oplock)
1964 strcat(message, "None");
1965 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
1966 &cinode->vfs_inode);
1970 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1971 unsigned int epoch, bool *purge_cache)
1973 unsigned int old_oplock = cinode->oplock;
1975 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
1978 *purge_cache = false;
1979 if (old_oplock == CIFS_CACHE_READ_FLG) {
1980 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
1981 (epoch - cinode->epoch > 0))
1982 *purge_cache = true;
1983 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1984 (epoch - cinode->epoch > 1))
1985 *purge_cache = true;
1986 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1987 (epoch - cinode->epoch > 1))
1988 *purge_cache = true;
1989 else if (cinode->oplock == 0 &&
1990 (epoch - cinode->epoch > 0))
1991 *purge_cache = true;
1992 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
1993 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1994 (epoch - cinode->epoch > 0))
1995 *purge_cache = true;
1996 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1997 (epoch - cinode->epoch > 1))
1998 *purge_cache = true;
2000 cinode->epoch = epoch;
2005 smb2_is_read_op(__u32 oplock)
2007 return oplock == SMB2_OPLOCK_LEVEL_II;
2011 smb21_is_read_op(__u32 oplock)
2013 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
2014 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
2018 map_oplock_to_lease(u8 oplock)
2020 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
2021 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
2022 else if (oplock == SMB2_OPLOCK_LEVEL_II)
2023 return SMB2_LEASE_READ_CACHING;
2024 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
2025 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
2026 SMB2_LEASE_WRITE_CACHING;
2031 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
2033 struct create_lease *buf;
2035 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
2039 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
2040 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
2041 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2043 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2044 (struct create_lease, lcontext));
2045 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
2046 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2047 (struct create_lease, Name));
2048 buf->ccontext.NameLength = cpu_to_le16(4);
2049 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
2058 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
2060 struct create_lease_v2 *buf;
2062 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
2066 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
2067 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
2068 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2070 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2071 (struct create_lease_v2, lcontext));
2072 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
2073 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2074 (struct create_lease_v2, Name));
2075 buf->ccontext.NameLength = cpu_to_le16(4);
2076 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
2085 smb2_parse_lease_buf(void *buf, unsigned int *epoch)
2087 struct create_lease *lc = (struct create_lease *)buf;
2089 *epoch = 0; /* not used */
2090 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2091 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2092 return le32_to_cpu(lc->lcontext.LeaseState);
2096 smb3_parse_lease_buf(void *buf, unsigned int *epoch)
2098 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
2100 *epoch = le16_to_cpu(lc->lcontext.Epoch);
2101 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2102 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2103 return le32_to_cpu(lc->lcontext.LeaseState);
2107 smb2_wp_retry_size(struct inode *inode)
2109 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
2110 SMB2_MAX_BUFFER_SIZE);
2114 smb2_dir_needs_close(struct cifsFileInfo *cfile)
2116 return !cfile->invalidHandle;
2120 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, struct smb_rqst *old_rq)
2122 struct smb2_sync_hdr *shdr =
2123 (struct smb2_sync_hdr *)old_rq->rq_iov[1].iov_base;
2124 unsigned int orig_len = get_rfc1002_length(old_rq->rq_iov[0].iov_base);
2126 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
2127 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
2128 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
2129 tr_hdr->Flags = cpu_to_le16(0x01);
2130 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2131 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
2132 inc_rfc1001_len(tr_hdr, sizeof(struct smb2_transform_hdr) - 4);
2133 inc_rfc1001_len(tr_hdr, orig_len);
2136 /* We can not use the normal sg_set_buf() as we will sometimes pass a
2137 * stack object as buf.
2139 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
2140 unsigned int buflen)
2142 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
2145 static struct scatterlist *
2146 init_sg(struct smb_rqst *rqst, u8 *sign)
2148 unsigned int sg_len = rqst->rq_nvec + rqst->rq_npages + 1;
2149 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 24;
2150 struct scatterlist *sg;
2154 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
2158 sg_init_table(sg, sg_len);
2159 smb2_sg_set_buf(&sg[0], rqst->rq_iov[0].iov_base + 24, assoc_data_len);
2160 for (i = 1; i < rqst->rq_nvec; i++)
2161 smb2_sg_set_buf(&sg[i], rqst->rq_iov[i].iov_base,
2162 rqst->rq_iov[i].iov_len);
2163 for (j = 0; i < sg_len - 1; i++, j++) {
2164 unsigned int len = (j < rqst->rq_npages - 1) ? rqst->rq_pagesz
2166 sg_set_page(&sg[i], rqst->rq_pages[j], len, 0);
2168 smb2_sg_set_buf(&sg[sg_len - 1], sign, SMB2_SIGNATURE_SIZE);
2172 struct cifs_crypt_result {
2174 struct completion completion;
2177 static void cifs_crypt_complete(struct crypto_async_request *req, int err)
2179 struct cifs_crypt_result *res = req->data;
2181 if (err == -EINPROGRESS)
2185 complete(&res->completion);
2189 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
2191 struct cifs_ses *ses;
2194 spin_lock(&cifs_tcp_ses_lock);
2195 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2196 if (ses->Suid != ses_id)
2198 ses_enc_key = enc ? ses->smb3encryptionkey :
2199 ses->smb3decryptionkey;
2200 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
2201 spin_unlock(&cifs_tcp_ses_lock);
2204 spin_unlock(&cifs_tcp_ses_lock);
2209 * Encrypt or decrypt @rqst message. @rqst has the following format:
2210 * iov[0] - transform header (associate data),
2211 * iov[1-N] and pages - data to encrypt.
2212 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
2216 crypt_message(struct TCP_Server_Info *server, struct smb_rqst *rqst, int enc)
2218 struct smb2_transform_hdr *tr_hdr =
2219 (struct smb2_transform_hdr *)rqst->rq_iov[0].iov_base;
2220 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 24;
2222 struct scatterlist *sg;
2223 u8 sign[SMB2_SIGNATURE_SIZE] = {};
2224 u8 key[SMB3_SIGN_KEY_SIZE];
2225 struct aead_request *req;
2227 unsigned int iv_len;
2228 struct cifs_crypt_result result = {0, };
2229 struct crypto_aead *tfm;
2230 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2232 init_completion(&result.completion);
2234 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
2236 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
2241 rc = smb3_crypto_aead_allocate(server);
2243 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
2247 tfm = enc ? server->secmech.ccmaesencrypt :
2248 server->secmech.ccmaesdecrypt;
2249 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
2251 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
2255 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
2257 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
2261 req = aead_request_alloc(tfm, GFP_KERNEL);
2263 cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
2268 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
2269 crypt_len += SMB2_SIGNATURE_SIZE;
2272 sg = init_sg(rqst, sign);
2274 cifs_dbg(VFS, "%s: Failed to init sg", __func__);
2279 iv_len = crypto_aead_ivsize(tfm);
2280 iv = kzalloc(iv_len, GFP_KERNEL);
2282 cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
2287 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2289 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
2290 aead_request_set_ad(req, assoc_data_len);
2292 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2293 cifs_crypt_complete, &result);
2295 rc = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
2297 if (rc == -EINPROGRESS || rc == -EBUSY) {
2298 wait_for_completion(&result.completion);
2303 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
2314 smb3_init_transform_rq(struct TCP_Server_Info *server, struct smb_rqst *new_rq,
2315 struct smb_rqst *old_rq)
2318 struct page **pages;
2319 struct smb2_transform_hdr *tr_hdr;
2320 unsigned int npages = old_rq->rq_npages;
2324 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2328 new_rq->rq_pages = pages;
2329 new_rq->rq_npages = old_rq->rq_npages;
2330 new_rq->rq_pagesz = old_rq->rq_pagesz;
2331 new_rq->rq_tailsz = old_rq->rq_tailsz;
2333 for (i = 0; i < npages; i++) {
2334 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2336 goto err_free_pages;
2339 iov = kmalloc_array(old_rq->rq_nvec, sizeof(struct kvec), GFP_KERNEL);
2341 goto err_free_pages;
2343 /* copy all iovs from the old except the 1st one (rfc1002 length) */
2344 memcpy(&iov[1], &old_rq->rq_iov[1],
2345 sizeof(struct kvec) * (old_rq->rq_nvec - 1));
2346 new_rq->rq_iov = iov;
2347 new_rq->rq_nvec = old_rq->rq_nvec;
2349 tr_hdr = kmalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL);
2353 /* fill the 1st iov with a transform header */
2354 fill_transform_hdr(tr_hdr, old_rq);
2355 new_rq->rq_iov[0].iov_base = tr_hdr;
2356 new_rq->rq_iov[0].iov_len = sizeof(struct smb2_transform_hdr);
2358 /* copy pages form the old */
2359 for (i = 0; i < npages; i++) {
2360 char *dst = kmap(new_rq->rq_pages[i]);
2361 char *src = kmap(old_rq->rq_pages[i]);
2362 unsigned int len = (i < npages - 1) ? new_rq->rq_pagesz :
2364 memcpy(dst, src, len);
2365 kunmap(new_rq->rq_pages[i]);
2366 kunmap(old_rq->rq_pages[i]);
2369 rc = crypt_message(server, new_rq, 1);
2370 cifs_dbg(FYI, "encrypt message returned %d", rc);
2372 goto err_free_tr_hdr;
2381 for (i = i - 1; i >= 0; i--)
2388 smb3_free_transform_rq(struct smb_rqst *rqst)
2390 int i = rqst->rq_npages - 1;
2393 put_page(rqst->rq_pages[i]);
2394 kfree(rqst->rq_pages);
2395 /* free transform header */
2396 kfree(rqst->rq_iov[0].iov_base);
2397 kfree(rqst->rq_iov);
2401 smb3_is_transform_hdr(void *buf)
2403 struct smb2_transform_hdr *trhdr = buf;
2405 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
2409 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
2410 unsigned int buf_data_size, struct page **pages,
2411 unsigned int npages, unsigned int page_data_size)
2414 struct smb_rqst rqst = {NULL};
2415 struct smb2_hdr *hdr;
2418 iov[0].iov_base = buf;
2419 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
2420 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
2421 iov[1].iov_len = buf_data_size;
2425 rqst.rq_pages = pages;
2426 rqst.rq_npages = npages;
2427 rqst.rq_pagesz = PAGE_SIZE;
2428 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
2430 rc = crypt_message(server, &rqst, 0);
2431 cifs_dbg(FYI, "decrypt message returned %d\n", rc);
2436 memmove(buf + 4, iov[1].iov_base, buf_data_size);
2437 hdr = (struct smb2_hdr *)buf;
2438 hdr->smb2_buf_length = cpu_to_be32(buf_data_size + page_data_size);
2439 server->total_read = buf_data_size + page_data_size + 4;
2445 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
2446 unsigned int npages, unsigned int len)
2451 for (i = 0; i < npages; i++) {
2452 struct page *page = pages[i];
2456 if (len >= PAGE_SIZE) {
2457 /* enough data to fill the page */
2461 zero_user(page, len, PAGE_SIZE - len);
2464 length = cifs_read_page_from_socket(server, page, n);
2467 server->total_read += length;
2474 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
2475 unsigned int cur_off, struct bio_vec **page_vec)
2477 struct bio_vec *bvec;
2480 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
2484 for (i = 0; i < npages; i++) {
2485 bvec[i].bv_page = pages[i];
2486 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
2487 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
2488 data_size -= bvec[i].bv_len;
2491 if (data_size != 0) {
2492 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
2502 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
2503 char *buf, unsigned int buf_len, struct page **pages,
2504 unsigned int npages, unsigned int page_data_size)
2506 unsigned int data_offset;
2507 unsigned int data_len;
2508 unsigned int cur_off;
2509 unsigned int cur_page_idx;
2510 unsigned int pad_len;
2511 struct cifs_readdata *rdata = mid->callback_data;
2512 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
2513 struct bio_vec *bvec = NULL;
2514 struct iov_iter iter;
2518 if (shdr->Command != SMB2_READ) {
2519 cifs_dbg(VFS, "only big read responses are supported\n");
2523 if (server->ops->is_session_expired &&
2524 server->ops->is_session_expired(buf)) {
2525 cifs_reconnect(server);
2526 wake_up(&server->response_q);
2530 if (server->ops->is_status_pending &&
2531 server->ops->is_status_pending(buf, server, 0))
2534 /* set up first two iov to get credits */
2535 rdata->iov[0].iov_base = buf;
2536 rdata->iov[0].iov_len = 4;
2537 rdata->iov[1].iov_base = buf + 4;
2538 rdata->iov[1].iov_len =
2539 min_t(unsigned int, buf_len, server->vals->read_rsp_size) - 4;
2540 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
2541 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
2542 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
2543 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
2545 rdata->result = server->ops->map_error(buf, true);
2546 if (rdata->result != 0) {
2547 cifs_dbg(FYI, "%s: server returned error %d\n",
2548 __func__, rdata->result);
2549 /* normal error on read response */
2550 dequeue_mid(mid, false);
2554 data_offset = server->ops->read_data_offset(buf) + 4;
2555 data_len = server->ops->read_data_length(buf);
2557 if (data_offset < server->vals->read_rsp_size) {
2559 * win2k8 sometimes sends an offset of 0 when the read
2560 * is beyond the EOF. Treat it as if the data starts just after
2563 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
2564 __func__, data_offset);
2565 data_offset = server->vals->read_rsp_size;
2566 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
2567 /* data_offset is beyond the end of smallbuf */
2568 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
2569 __func__, data_offset);
2570 rdata->result = -EIO;
2571 dequeue_mid(mid, rdata->result);
2575 pad_len = data_offset - server->vals->read_rsp_size;
2577 if (buf_len <= data_offset) {
2578 /* read response payload is in pages */
2579 cur_page_idx = pad_len / PAGE_SIZE;
2580 cur_off = pad_len % PAGE_SIZE;
2582 if (cur_page_idx != 0) {
2583 /* data offset is beyond the 1st page of response */
2584 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
2585 __func__, data_offset);
2586 rdata->result = -EIO;
2587 dequeue_mid(mid, rdata->result);
2591 if (data_len > page_data_size - pad_len) {
2592 /* data_len is corrupt -- discard frame */
2593 rdata->result = -EIO;
2594 dequeue_mid(mid, rdata->result);
2598 rdata->result = init_read_bvec(pages, npages, page_data_size,
2600 if (rdata->result != 0) {
2601 dequeue_mid(mid, rdata->result);
2605 iov_iter_bvec(&iter, WRITE | ITER_BVEC, bvec, npages, data_len);
2606 } else if (buf_len >= data_offset + data_len) {
2607 /* read response payload is in buf */
2608 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
2609 iov.iov_base = buf + data_offset;
2610 iov.iov_len = data_len;
2611 iov_iter_kvec(&iter, WRITE | ITER_KVEC, &iov, 1, data_len);
2613 /* read response payload cannot be in both buf and pages */
2614 WARN_ONCE(1, "buf can not contain only a part of read data");
2615 rdata->result = -EIO;
2616 dequeue_mid(mid, rdata->result);
2620 length = rdata->copy_into_pages(server, rdata, &iter);
2627 dequeue_mid(mid, false);
2632 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2634 char *buf = server->smallbuf;
2635 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2636 unsigned int npages;
2637 struct page **pages;
2639 unsigned int buflen = get_rfc1002_length(buf) + 4;
2643 len = min_t(unsigned int, buflen, server->vals->read_rsp_size - 4 +
2644 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
2646 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
2649 server->total_read += rc;
2651 len = le32_to_cpu(tr_hdr->OriginalMessageSize) + 4 -
2652 server->vals->read_rsp_size;
2653 npages = DIV_ROUND_UP(len, PAGE_SIZE);
2655 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2661 for (; i < npages; i++) {
2662 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2669 /* read read data into pages */
2670 rc = read_data_into_pages(server, pages, npages, len);
2674 rc = cifs_discard_remaining_data(server);
2678 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size - 4,
2679 pages, npages, len);
2683 *mid = smb2_find_mid(server, buf);
2685 cifs_dbg(FYI, "mid not found\n");
2687 cifs_dbg(FYI, "mid found\n");
2688 (*mid)->decrypted = true;
2689 rc = handle_read_data(server, *mid, buf,
2690 server->vals->read_rsp_size,
2691 pages, npages, len);
2695 for (i = i - 1; i >= 0; i--)
2700 cifs_discard_remaining_data(server);
2705 receive_encrypted_standard(struct TCP_Server_Info *server,
2706 struct mid_q_entry **mid)
2709 char *buf = server->smallbuf;
2710 unsigned int pdu_length = get_rfc1002_length(buf);
2711 unsigned int buf_size;
2712 struct mid_q_entry *mid_entry;
2714 /* switch to large buffer if too big for a small one */
2715 if (pdu_length + 4 > MAX_CIFS_SMALL_BUFFER_SIZE) {
2716 server->large_buf = true;
2717 memcpy(server->bigbuf, buf, server->total_read);
2718 buf = server->bigbuf;
2721 /* now read the rest */
2722 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
2723 pdu_length - HEADER_SIZE(server) + 1 + 4);
2726 server->total_read += length;
2728 buf_size = pdu_length + 4 - sizeof(struct smb2_transform_hdr);
2729 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
2733 mid_entry = smb2_find_mid(server, buf);
2734 if (mid_entry == NULL)
2735 cifs_dbg(FYI, "mid not found\n");
2737 cifs_dbg(FYI, "mid found\n");
2738 mid_entry->decrypted = true;
2743 if (mid_entry && mid_entry->handle)
2744 return mid_entry->handle(server, mid_entry);
2746 return cifs_handle_standard(server, mid_entry);
2750 smb3_receive_transform(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2752 char *buf = server->smallbuf;
2753 unsigned int pdu_length = get_rfc1002_length(buf);
2754 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2755 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2757 if (pdu_length + 4 < sizeof(struct smb2_transform_hdr) +
2758 sizeof(struct smb2_sync_hdr)) {
2759 cifs_dbg(VFS, "Transform message is too small (%u)\n",
2761 cifs_reconnect(server);
2762 wake_up(&server->response_q);
2763 return -ECONNABORTED;
2766 if (pdu_length + 4 < orig_len + sizeof(struct smb2_transform_hdr)) {
2767 cifs_dbg(VFS, "Transform message is broken\n");
2768 cifs_reconnect(server);
2769 wake_up(&server->response_q);
2770 return -ECONNABORTED;
2773 if (pdu_length + 4 > CIFSMaxBufSize + MAX_HEADER_SIZE(server))
2774 return receive_encrypted_read(server, mid);
2776 return receive_encrypted_standard(server, mid);
2780 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
2782 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
2784 return handle_read_data(server, mid, buf, get_rfc1002_length(buf) + 4,
2788 struct smb_version_operations smb20_operations = {
2789 .compare_fids = smb2_compare_fids,
2790 .setup_request = smb2_setup_request,
2791 .setup_async_request = smb2_setup_async_request,
2792 .check_receive = smb2_check_receive,
2793 .add_credits = smb2_add_credits,
2794 .set_credits = smb2_set_credits,
2795 .get_credits_field = smb2_get_credits_field,
2796 .get_credits = smb2_get_credits,
2797 .wait_mtu_credits = cifs_wait_mtu_credits,
2798 .get_next_mid = smb2_get_next_mid,
2799 .read_data_offset = smb2_read_data_offset,
2800 .read_data_length = smb2_read_data_length,
2801 .map_error = map_smb2_to_linux_error,
2802 .find_mid = smb2_find_mid,
2803 .check_message = smb2_check_message,
2804 .dump_detail = smb2_dump_detail,
2805 .clear_stats = smb2_clear_stats,
2806 .print_stats = smb2_print_stats,
2807 .is_oplock_break = smb2_is_valid_oplock_break,
2808 .handle_cancelled_mid = smb2_handle_cancelled_mid,
2809 .downgrade_oplock = smb2_downgrade_oplock,
2810 .need_neg = smb2_need_neg,
2811 .negotiate = smb2_negotiate,
2812 .negotiate_wsize = smb2_negotiate_wsize,
2813 .negotiate_rsize = smb2_negotiate_rsize,
2814 .sess_setup = SMB2_sess_setup,
2815 .logoff = SMB2_logoff,
2816 .tree_connect = SMB2_tcon,
2817 .tree_disconnect = SMB2_tdis,
2818 .qfs_tcon = smb2_qfs_tcon,
2819 .is_path_accessible = smb2_is_path_accessible,
2820 .can_echo = smb2_can_echo,
2822 .query_path_info = smb2_query_path_info,
2823 .get_srv_inum = smb2_get_srv_inum,
2824 .query_file_info = smb2_query_file_info,
2825 .set_path_size = smb2_set_path_size,
2826 .set_file_size = smb2_set_file_size,
2827 .set_file_info = smb2_set_file_info,
2828 .set_compression = smb2_set_compression,
2829 .mkdir = smb2_mkdir,
2830 .mkdir_setinfo = smb2_mkdir_setinfo,
2831 .rmdir = smb2_rmdir,
2832 .unlink = smb2_unlink,
2833 .rename = smb2_rename_path,
2834 .create_hardlink = smb2_create_hardlink,
2835 .query_symlink = smb2_query_symlink,
2836 .query_mf_symlink = smb3_query_mf_symlink,
2837 .create_mf_symlink = smb3_create_mf_symlink,
2838 .open = smb2_open_file,
2839 .set_fid = smb2_set_fid,
2840 .close = smb2_close_file,
2841 .flush = smb2_flush_file,
2842 .async_readv = smb2_async_readv,
2843 .async_writev = smb2_async_writev,
2844 .sync_read = smb2_sync_read,
2845 .sync_write = smb2_sync_write,
2846 .query_dir_first = smb2_query_dir_first,
2847 .query_dir_next = smb2_query_dir_next,
2848 .close_dir = smb2_close_dir,
2849 .calc_smb_size = smb2_calc_size,
2850 .is_status_pending = smb2_is_status_pending,
2851 .is_session_expired = smb2_is_session_expired,
2852 .oplock_response = smb2_oplock_response,
2853 .queryfs = smb2_queryfs,
2854 .mand_lock = smb2_mand_lock,
2855 .mand_unlock_range = smb2_unlock_range,
2856 .push_mand_locks = smb2_push_mandatory_locks,
2857 .get_lease_key = smb2_get_lease_key,
2858 .set_lease_key = smb2_set_lease_key,
2859 .new_lease_key = smb2_new_lease_key,
2860 .calc_signature = smb2_calc_signature,
2861 .is_read_op = smb2_is_read_op,
2862 .set_oplock_level = smb2_set_oplock_level,
2863 .create_lease_buf = smb2_create_lease_buf,
2864 .parse_lease_buf = smb2_parse_lease_buf,
2865 .copychunk_range = smb2_copychunk_range,
2866 .wp_retry_size = smb2_wp_retry_size,
2867 .dir_needs_close = smb2_dir_needs_close,
2868 .get_dfs_refer = smb2_get_dfs_refer,
2869 .select_sectype = smb2_select_sectype,
2870 #ifdef CONFIG_CIFS_XATTR
2871 .query_all_EAs = smb2_query_eas,
2872 .set_EA = smb2_set_ea,
2873 #endif /* CIFS_XATTR */
2874 #ifdef CONFIG_CIFS_ACL
2875 .get_acl = get_smb2_acl,
2876 .get_acl_by_fid = get_smb2_acl_by_fid,
2877 .set_acl = set_smb2_acl,
2878 #endif /* CIFS_ACL */
2881 struct smb_version_operations smb21_operations = {
2882 .compare_fids = smb2_compare_fids,
2883 .setup_request = smb2_setup_request,
2884 .setup_async_request = smb2_setup_async_request,
2885 .check_receive = smb2_check_receive,
2886 .add_credits = smb2_add_credits,
2887 .set_credits = smb2_set_credits,
2888 .get_credits_field = smb2_get_credits_field,
2889 .get_credits = smb2_get_credits,
2890 .wait_mtu_credits = smb2_wait_mtu_credits,
2891 .get_next_mid = smb2_get_next_mid,
2892 .read_data_offset = smb2_read_data_offset,
2893 .read_data_length = smb2_read_data_length,
2894 .map_error = map_smb2_to_linux_error,
2895 .find_mid = smb2_find_mid,
2896 .check_message = smb2_check_message,
2897 .dump_detail = smb2_dump_detail,
2898 .clear_stats = smb2_clear_stats,
2899 .print_stats = smb2_print_stats,
2900 .is_oplock_break = smb2_is_valid_oplock_break,
2901 .handle_cancelled_mid = smb2_handle_cancelled_mid,
2902 .downgrade_oplock = smb2_downgrade_oplock,
2903 .need_neg = smb2_need_neg,
2904 .negotiate = smb2_negotiate,
2905 .negotiate_wsize = smb2_negotiate_wsize,
2906 .negotiate_rsize = smb2_negotiate_rsize,
2907 .sess_setup = SMB2_sess_setup,
2908 .logoff = SMB2_logoff,
2909 .tree_connect = SMB2_tcon,
2910 .tree_disconnect = SMB2_tdis,
2911 .qfs_tcon = smb2_qfs_tcon,
2912 .is_path_accessible = smb2_is_path_accessible,
2913 .can_echo = smb2_can_echo,
2915 .query_path_info = smb2_query_path_info,
2916 .get_srv_inum = smb2_get_srv_inum,
2917 .query_file_info = smb2_query_file_info,
2918 .set_path_size = smb2_set_path_size,
2919 .set_file_size = smb2_set_file_size,
2920 .set_file_info = smb2_set_file_info,
2921 .set_compression = smb2_set_compression,
2922 .mkdir = smb2_mkdir,
2923 .mkdir_setinfo = smb2_mkdir_setinfo,
2924 .rmdir = smb2_rmdir,
2925 .unlink = smb2_unlink,
2926 .rename = smb2_rename_path,
2927 .create_hardlink = smb2_create_hardlink,
2928 .query_symlink = smb2_query_symlink,
2929 .query_mf_symlink = smb3_query_mf_symlink,
2930 .create_mf_symlink = smb3_create_mf_symlink,
2931 .open = smb2_open_file,
2932 .set_fid = smb2_set_fid,
2933 .close = smb2_close_file,
2934 .flush = smb2_flush_file,
2935 .async_readv = smb2_async_readv,
2936 .async_writev = smb2_async_writev,
2937 .sync_read = smb2_sync_read,
2938 .sync_write = smb2_sync_write,
2939 .query_dir_first = smb2_query_dir_first,
2940 .query_dir_next = smb2_query_dir_next,
2941 .close_dir = smb2_close_dir,
2942 .calc_smb_size = smb2_calc_size,
2943 .is_status_pending = smb2_is_status_pending,
2944 .is_session_expired = smb2_is_session_expired,
2945 .oplock_response = smb2_oplock_response,
2946 .queryfs = smb2_queryfs,
2947 .mand_lock = smb2_mand_lock,
2948 .mand_unlock_range = smb2_unlock_range,
2949 .push_mand_locks = smb2_push_mandatory_locks,
2950 .get_lease_key = smb2_get_lease_key,
2951 .set_lease_key = smb2_set_lease_key,
2952 .new_lease_key = smb2_new_lease_key,
2953 .calc_signature = smb2_calc_signature,
2954 .is_read_op = smb21_is_read_op,
2955 .set_oplock_level = smb21_set_oplock_level,
2956 .create_lease_buf = smb2_create_lease_buf,
2957 .parse_lease_buf = smb2_parse_lease_buf,
2958 .copychunk_range = smb2_copychunk_range,
2959 .wp_retry_size = smb2_wp_retry_size,
2960 .dir_needs_close = smb2_dir_needs_close,
2961 .enum_snapshots = smb3_enum_snapshots,
2962 .get_dfs_refer = smb2_get_dfs_refer,
2963 .select_sectype = smb2_select_sectype,
2964 #ifdef CONFIG_CIFS_XATTR
2965 .query_all_EAs = smb2_query_eas,
2966 .set_EA = smb2_set_ea,
2967 #endif /* CIFS_XATTR */
2968 #ifdef CONFIG_CIFS_ACL
2969 .get_acl = get_smb2_acl,
2970 .get_acl_by_fid = get_smb2_acl_by_fid,
2971 .set_acl = set_smb2_acl,
2972 #endif /* CIFS_ACL */
2975 struct smb_version_operations smb30_operations = {
2976 .compare_fids = smb2_compare_fids,
2977 .setup_request = smb2_setup_request,
2978 .setup_async_request = smb2_setup_async_request,
2979 .check_receive = smb2_check_receive,
2980 .add_credits = smb2_add_credits,
2981 .set_credits = smb2_set_credits,
2982 .get_credits_field = smb2_get_credits_field,
2983 .get_credits = smb2_get_credits,
2984 .wait_mtu_credits = smb2_wait_mtu_credits,
2985 .get_next_mid = smb2_get_next_mid,
2986 .read_data_offset = smb2_read_data_offset,
2987 .read_data_length = smb2_read_data_length,
2988 .map_error = map_smb2_to_linux_error,
2989 .find_mid = smb2_find_mid,
2990 .check_message = smb2_check_message,
2991 .dump_detail = smb2_dump_detail,
2992 .clear_stats = smb2_clear_stats,
2993 .print_stats = smb2_print_stats,
2994 .dump_share_caps = smb2_dump_share_caps,
2995 .is_oplock_break = smb2_is_valid_oplock_break,
2996 .handle_cancelled_mid = smb2_handle_cancelled_mid,
2997 .downgrade_oplock = smb2_downgrade_oplock,
2998 .need_neg = smb2_need_neg,
2999 .negotiate = smb2_negotiate,
3000 .negotiate_wsize = smb2_negotiate_wsize,
3001 .negotiate_rsize = smb2_negotiate_rsize,
3002 .sess_setup = SMB2_sess_setup,
3003 .logoff = SMB2_logoff,
3004 .tree_connect = SMB2_tcon,
3005 .tree_disconnect = SMB2_tdis,
3006 .qfs_tcon = smb3_qfs_tcon,
3007 .is_path_accessible = smb2_is_path_accessible,
3008 .can_echo = smb2_can_echo,
3010 .query_path_info = smb2_query_path_info,
3011 .get_srv_inum = smb2_get_srv_inum,
3012 .query_file_info = smb2_query_file_info,
3013 .set_path_size = smb2_set_path_size,
3014 .set_file_size = smb2_set_file_size,
3015 .set_file_info = smb2_set_file_info,
3016 .set_compression = smb2_set_compression,
3017 .mkdir = smb2_mkdir,
3018 .mkdir_setinfo = smb2_mkdir_setinfo,
3019 .rmdir = smb2_rmdir,
3020 .unlink = smb2_unlink,
3021 .rename = smb2_rename_path,
3022 .create_hardlink = smb2_create_hardlink,
3023 .query_symlink = smb2_query_symlink,
3024 .query_mf_symlink = smb3_query_mf_symlink,
3025 .create_mf_symlink = smb3_create_mf_symlink,
3026 .open = smb2_open_file,
3027 .set_fid = smb2_set_fid,
3028 .close = smb2_close_file,
3029 .flush = smb2_flush_file,
3030 .async_readv = smb2_async_readv,
3031 .async_writev = smb2_async_writev,
3032 .sync_read = smb2_sync_read,
3033 .sync_write = smb2_sync_write,
3034 .query_dir_first = smb2_query_dir_first,
3035 .query_dir_next = smb2_query_dir_next,
3036 .close_dir = smb2_close_dir,
3037 .calc_smb_size = smb2_calc_size,
3038 .is_status_pending = smb2_is_status_pending,
3039 .is_session_expired = smb2_is_session_expired,
3040 .oplock_response = smb2_oplock_response,
3041 .queryfs = smb2_queryfs,
3042 .mand_lock = smb2_mand_lock,
3043 .mand_unlock_range = smb2_unlock_range,
3044 .push_mand_locks = smb2_push_mandatory_locks,
3045 .get_lease_key = smb2_get_lease_key,
3046 .set_lease_key = smb2_set_lease_key,
3047 .new_lease_key = smb2_new_lease_key,
3048 .generate_signingkey = generate_smb30signingkey,
3049 .calc_signature = smb3_calc_signature,
3050 .set_integrity = smb3_set_integrity,
3051 .is_read_op = smb21_is_read_op,
3052 .set_oplock_level = smb3_set_oplock_level,
3053 .create_lease_buf = smb3_create_lease_buf,
3054 .parse_lease_buf = smb3_parse_lease_buf,
3055 .copychunk_range = smb2_copychunk_range,
3056 .duplicate_extents = smb2_duplicate_extents,
3057 .validate_negotiate = smb3_validate_negotiate,
3058 .wp_retry_size = smb2_wp_retry_size,
3059 .dir_needs_close = smb2_dir_needs_close,
3060 .fallocate = smb3_fallocate,
3061 .enum_snapshots = smb3_enum_snapshots,
3062 .init_transform_rq = smb3_init_transform_rq,
3063 .free_transform_rq = smb3_free_transform_rq,
3064 .is_transform_hdr = smb3_is_transform_hdr,
3065 .receive_transform = smb3_receive_transform,
3066 .get_dfs_refer = smb2_get_dfs_refer,
3067 .select_sectype = smb2_select_sectype,
3068 #ifdef CONFIG_CIFS_XATTR
3069 .query_all_EAs = smb2_query_eas,
3070 .set_EA = smb2_set_ea,
3071 #endif /* CIFS_XATTR */
3072 #ifdef CONFIG_CIFS_ACL
3073 .get_acl = get_smb2_acl,
3074 .get_acl_by_fid = get_smb2_acl_by_fid,
3075 .set_acl = set_smb2_acl,
3076 #endif /* CIFS_ACL */
3079 #ifdef CONFIG_CIFS_SMB311
3080 struct smb_version_operations smb311_operations = {
3081 .compare_fids = smb2_compare_fids,
3082 .setup_request = smb2_setup_request,
3083 .setup_async_request = smb2_setup_async_request,
3084 .check_receive = smb2_check_receive,
3085 .add_credits = smb2_add_credits,
3086 .set_credits = smb2_set_credits,
3087 .get_credits_field = smb2_get_credits_field,
3088 .get_credits = smb2_get_credits,
3089 .wait_mtu_credits = smb2_wait_mtu_credits,
3090 .get_next_mid = smb2_get_next_mid,
3091 .read_data_offset = smb2_read_data_offset,
3092 .read_data_length = smb2_read_data_length,
3093 .map_error = map_smb2_to_linux_error,
3094 .find_mid = smb2_find_mid,
3095 .check_message = smb2_check_message,
3096 .dump_detail = smb2_dump_detail,
3097 .clear_stats = smb2_clear_stats,
3098 .print_stats = smb2_print_stats,
3099 .dump_share_caps = smb2_dump_share_caps,
3100 .is_oplock_break = smb2_is_valid_oplock_break,
3101 .handle_cancelled_mid = smb2_handle_cancelled_mid,
3102 .downgrade_oplock = smb2_downgrade_oplock,
3103 .need_neg = smb2_need_neg,
3104 .negotiate = smb2_negotiate,
3105 .negotiate_wsize = smb2_negotiate_wsize,
3106 .negotiate_rsize = smb2_negotiate_rsize,
3107 .sess_setup = SMB2_sess_setup,
3108 .logoff = SMB2_logoff,
3109 .tree_connect = SMB2_tcon,
3110 .tree_disconnect = SMB2_tdis,
3111 .qfs_tcon = smb3_qfs_tcon,
3112 .is_path_accessible = smb2_is_path_accessible,
3113 .can_echo = smb2_can_echo,
3115 .query_path_info = smb2_query_path_info,
3116 .get_srv_inum = smb2_get_srv_inum,
3117 .query_file_info = smb2_query_file_info,
3118 .set_path_size = smb2_set_path_size,
3119 .set_file_size = smb2_set_file_size,
3120 .set_file_info = smb2_set_file_info,
3121 .set_compression = smb2_set_compression,
3122 .mkdir = smb2_mkdir,
3123 .mkdir_setinfo = smb2_mkdir_setinfo,
3124 .rmdir = smb2_rmdir,
3125 .unlink = smb2_unlink,
3126 .rename = smb2_rename_path,
3127 .create_hardlink = smb2_create_hardlink,
3128 .query_symlink = smb2_query_symlink,
3129 .query_mf_symlink = smb3_query_mf_symlink,
3130 .create_mf_symlink = smb3_create_mf_symlink,
3131 .open = smb2_open_file,
3132 .set_fid = smb2_set_fid,
3133 .close = smb2_close_file,
3134 .flush = smb2_flush_file,
3135 .async_readv = smb2_async_readv,
3136 .async_writev = smb2_async_writev,
3137 .sync_read = smb2_sync_read,
3138 .sync_write = smb2_sync_write,
3139 .query_dir_first = smb2_query_dir_first,
3140 .query_dir_next = smb2_query_dir_next,
3141 .close_dir = smb2_close_dir,
3142 .calc_smb_size = smb2_calc_size,
3143 .is_status_pending = smb2_is_status_pending,
3144 .is_session_expired = smb2_is_session_expired,
3145 .oplock_response = smb2_oplock_response,
3146 .queryfs = smb2_queryfs,
3147 .mand_lock = smb2_mand_lock,
3148 .mand_unlock_range = smb2_unlock_range,
3149 .push_mand_locks = smb2_push_mandatory_locks,
3150 .get_lease_key = smb2_get_lease_key,
3151 .set_lease_key = smb2_set_lease_key,
3152 .new_lease_key = smb2_new_lease_key,
3153 .generate_signingkey = generate_smb311signingkey,
3154 .calc_signature = smb3_calc_signature,
3155 .set_integrity = smb3_set_integrity,
3156 .is_read_op = smb21_is_read_op,
3157 .set_oplock_level = smb3_set_oplock_level,
3158 .create_lease_buf = smb3_create_lease_buf,
3159 .parse_lease_buf = smb3_parse_lease_buf,
3160 .copychunk_range = smb2_copychunk_range,
3161 .duplicate_extents = smb2_duplicate_extents,
3162 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
3163 .wp_retry_size = smb2_wp_retry_size,
3164 .dir_needs_close = smb2_dir_needs_close,
3165 .fallocate = smb3_fallocate,
3166 .enum_snapshots = smb3_enum_snapshots,
3167 .init_transform_rq = smb3_init_transform_rq,
3168 .free_transform_rq = smb3_free_transform_rq,
3169 .is_transform_hdr = smb3_is_transform_hdr,
3170 .receive_transform = smb3_receive_transform,
3171 .get_dfs_refer = smb2_get_dfs_refer,
3172 .select_sectype = smb2_select_sectype,
3173 #ifdef CONFIG_CIFS_XATTR
3174 .query_all_EAs = smb2_query_eas,
3175 .set_EA = smb2_set_ea,
3176 #endif /* CIFS_XATTR */
3178 #endif /* CIFS_SMB311 */
3180 struct smb_version_values smb20_values = {
3181 .version_string = SMB20_VERSION_STRING,
3182 .protocol_id = SMB20_PROT_ID,
3183 .req_capabilities = 0, /* MBZ */
3184 .large_lock_type = 0,
3185 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3186 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3187 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3188 .header_size = sizeof(struct smb2_hdr),
3189 .max_header_size = MAX_SMB2_HDR_SIZE,
3190 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3191 .lock_cmd = SMB2_LOCK,
3193 .cap_nt_find = SMB2_NT_FIND,
3194 .cap_large_files = SMB2_LARGE_FILES,
3195 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3196 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3197 .create_lease_size = sizeof(struct create_lease),
3200 struct smb_version_values smb21_values = {
3201 .version_string = SMB21_VERSION_STRING,
3202 .protocol_id = SMB21_PROT_ID,
3203 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
3204 .large_lock_type = 0,
3205 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3206 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3207 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3208 .header_size = sizeof(struct smb2_hdr),
3209 .max_header_size = MAX_SMB2_HDR_SIZE,
3210 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3211 .lock_cmd = SMB2_LOCK,
3213 .cap_nt_find = SMB2_NT_FIND,
3214 .cap_large_files = SMB2_LARGE_FILES,
3215 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3216 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3217 .create_lease_size = sizeof(struct create_lease),
3220 struct smb_version_values smb3any_values = {
3221 .version_string = SMB3ANY_VERSION_STRING,
3222 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3223 .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,
3224 .large_lock_type = 0,
3225 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3226 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3227 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3228 .header_size = sizeof(struct smb2_hdr),
3229 .max_header_size = MAX_SMB2_HDR_SIZE,
3230 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3231 .lock_cmd = SMB2_LOCK,
3233 .cap_nt_find = SMB2_NT_FIND,
3234 .cap_large_files = SMB2_LARGE_FILES,
3235 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3236 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3237 .create_lease_size = sizeof(struct create_lease_v2),
3240 struct smb_version_values smbdefault_values = {
3241 .version_string = SMBDEFAULT_VERSION_STRING,
3242 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3243 .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,
3244 .large_lock_type = 0,
3245 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3246 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3247 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3248 .header_size = sizeof(struct smb2_hdr),
3249 .max_header_size = MAX_SMB2_HDR_SIZE,
3250 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3251 .lock_cmd = SMB2_LOCK,
3253 .cap_nt_find = SMB2_NT_FIND,
3254 .cap_large_files = SMB2_LARGE_FILES,
3255 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3256 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3257 .create_lease_size = sizeof(struct create_lease_v2),
3260 struct smb_version_values smb30_values = {
3261 .version_string = SMB30_VERSION_STRING,
3262 .protocol_id = SMB30_PROT_ID,
3263 .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,
3264 .large_lock_type = 0,
3265 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3266 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3267 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3268 .header_size = sizeof(struct smb2_hdr),
3269 .max_header_size = MAX_SMB2_HDR_SIZE,
3270 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3271 .lock_cmd = SMB2_LOCK,
3273 .cap_nt_find = SMB2_NT_FIND,
3274 .cap_large_files = SMB2_LARGE_FILES,
3275 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3276 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3277 .create_lease_size = sizeof(struct create_lease_v2),
3280 struct smb_version_values smb302_values = {
3281 .version_string = SMB302_VERSION_STRING,
3282 .protocol_id = SMB302_PROT_ID,
3283 .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,
3284 .large_lock_type = 0,
3285 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3286 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3287 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3288 .header_size = sizeof(struct smb2_hdr),
3289 .max_header_size = MAX_SMB2_HDR_SIZE,
3290 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3291 .lock_cmd = SMB2_LOCK,
3293 .cap_nt_find = SMB2_NT_FIND,
3294 .cap_large_files = SMB2_LARGE_FILES,
3295 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3296 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3297 .create_lease_size = sizeof(struct create_lease_v2),
3300 #ifdef CONFIG_CIFS_SMB311
3301 struct smb_version_values smb311_values = {
3302 .version_string = SMB311_VERSION_STRING,
3303 .protocol_id = SMB311_PROT_ID,
3304 .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,
3305 .large_lock_type = 0,
3306 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3307 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3308 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3309 .header_size = sizeof(struct smb2_hdr),
3310 .max_header_size = MAX_SMB2_HDR_SIZE,
3311 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3312 .lock_cmd = SMB2_LOCK,
3314 .cap_nt_find = SMB2_NT_FIND,
3315 .cap_large_files = SMB2_LARGE_FILES,
3316 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3317 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3318 .create_lease_size = sizeof(struct create_lease_v2),