1 // SPDX-License-Identifier: LGPL-2.1
4 * Copyright (C) International Business Machines Corp., 2002,2008
5 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Common Internet FileSystem (CIFS) client
11 /* Note that BB means BUGBUG (ie something to fix eventually) */
13 #include <linux/module.h>
15 #include <linux/mount.h>
16 #include <linux/slab.h>
17 #include <linux/init.h>
18 #include <linux/list.h>
19 #include <linux/seq_file.h>
20 #include <linux/vfs.h>
21 #include <linux/mempool.h>
22 #include <linux/delay.h>
23 #include <linux/kthread.h>
24 #include <linux/freezer.h>
25 #include <linux/namei.h>
26 #include <linux/random.h>
27 #include <linux/uuid.h>
28 #include <linux/xattr.h>
29 #include <uapi/linux/magic.h>
33 #define DECLARE_GLOBALS_HERE
35 #include "cifsproto.h"
36 #include "cifs_debug.h"
37 #include "cifs_fs_sb.h"
39 #include <linux/key-type.h>
40 #include "cifs_spnego.h"
42 #ifdef CONFIG_CIFS_DFS_UPCALL
43 #include "dfs_cache.h"
45 #ifdef CONFIG_CIFS_SWN_UPCALL
48 #include "fs_context.h"
51 * DOS dates from 1980/1/1 through 2107/12/31
52 * Protocol specifications indicate the range should be to 119, which
53 * limits maximum year to 2099. But this range has not been checked.
55 #define SMB_DATE_MAX (127<<9 | 12<<5 | 31)
56 #define SMB_DATE_MIN (0<<9 | 1<<5 | 1)
57 #define SMB_TIME_MAX (23<<11 | 59<<5 | 29)
61 bool enable_oplocks = true;
62 bool linuxExtEnabled = true;
63 bool lookupCacheEnabled = true;
64 bool disable_legacy_dialects; /* false by default */
65 bool enable_gcm_256 = true;
66 bool require_gcm_256; /* false by default */
67 bool enable_negotiate_signing; /* false by default */
68 unsigned int global_secflags = CIFSSEC_DEF;
69 /* unsigned int ntlmv2_support = 0; */
70 unsigned int sign_CIFS_PDUs = 1;
73 * Global transaction id (XID) information
75 unsigned int GlobalCurrentXid; /* protected by GlobalMid_Sem */
76 unsigned int GlobalTotalActiveXid; /* prot by GlobalMid_Sem */
77 unsigned int GlobalMaxActiveXid; /* prot by GlobalMid_Sem */
78 spinlock_t GlobalMid_Lock; /* protects above & list operations on midQ entries */
81 * Global counters, updated atomically
83 atomic_t sesInfoAllocCount;
84 atomic_t tconInfoAllocCount;
85 atomic_t tcpSesNextId;
86 atomic_t tcpSesAllocCount;
87 atomic_t tcpSesReconnectCount;
88 atomic_t tconInfoReconnectCount;
91 atomic_t buf_alloc_count;
92 atomic_t small_buf_alloc_count;
93 #ifdef CONFIG_CIFS_STATS2
94 atomic_t total_buf_alloc_count;
95 atomic_t total_small_buf_alloc_count;
97 struct list_head cifs_tcp_ses_list;
98 spinlock_t cifs_tcp_ses_lock;
99 static const struct super_operations cifs_super_ops;
100 unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
101 module_param(CIFSMaxBufSize, uint, 0444);
102 MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header) "
103 "for CIFS requests. "
104 "Default: 16384 Range: 8192 to 130048");
105 unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
106 module_param(cifs_min_rcv, uint, 0444);
107 MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
109 unsigned int cifs_min_small = 30;
110 module_param(cifs_min_small, uint, 0444);
111 MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
113 unsigned int cifs_max_pending = CIFS_MAX_REQ;
114 module_param(cifs_max_pending, uint, 0444);
115 MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server for "
116 "CIFS/SMB1 dialect (N/A for SMB3) "
117 "Default: 32767 Range: 2 to 32767.");
118 #ifdef CONFIG_CIFS_STATS2
119 unsigned int slow_rsp_threshold = 1;
120 module_param(slow_rsp_threshold, uint, 0644);
121 MODULE_PARM_DESC(slow_rsp_threshold, "Amount of time (in seconds) to wait "
122 "before logging that a response is delayed. "
123 "Default: 1 (if set to 0 disables msg).");
126 module_param(enable_oplocks, bool, 0644);
127 MODULE_PARM_DESC(enable_oplocks, "Enable or disable oplocks. Default: y/Y/1");
129 module_param(enable_gcm_256, bool, 0644);
130 MODULE_PARM_DESC(enable_gcm_256, "Enable requesting strongest (256 bit) GCM encryption. Default: n/N/0");
132 module_param(require_gcm_256, bool, 0644);
133 MODULE_PARM_DESC(require_gcm_256, "Require strongest (256 bit) GCM encryption. Default: n/N/0");
135 module_param(enable_negotiate_signing, bool, 0644);
136 MODULE_PARM_DESC(enable_negotiate_signing, "Enable negotiating packet signing algorithm with server. Default: n/N/0");
138 module_param(disable_legacy_dialects, bool, 0644);
139 MODULE_PARM_DESC(disable_legacy_dialects, "To improve security it may be "
140 "helpful to restrict the ability to "
141 "override the default dialects (SMB2.1, "
142 "SMB3 and SMB3.02) on mount with old "
143 "dialects (CIFS/SMB1 and SMB2) since "
144 "vers=1.0 (CIFS/SMB1) and vers=2.0 are weaker"
145 " and less secure. Default: n/N/0");
147 extern mempool_t *cifs_sm_req_poolp;
148 extern mempool_t *cifs_req_poolp;
149 extern mempool_t *cifs_mid_poolp;
151 struct workqueue_struct *cifsiod_wq;
152 struct workqueue_struct *decrypt_wq;
153 struct workqueue_struct *fileinfo_put_wq;
154 struct workqueue_struct *cifsoplockd_wq;
155 struct workqueue_struct *deferredclose_wq;
156 __u32 cifs_lock_secret;
159 * Bumps refcount for cifs super block.
160 * Note that it should be only called if a referece to VFS super block is
161 * already held, e.g. in open-type syscalls context. Otherwise it can race with
162 * atomic_dec_and_test in deactivate_locked_super.
165 cifs_sb_active(struct super_block *sb)
167 struct cifs_sb_info *server = CIFS_SB(sb);
169 if (atomic_inc_return(&server->active) == 1)
170 atomic_inc(&sb->s_active);
174 cifs_sb_deactive(struct super_block *sb)
176 struct cifs_sb_info *server = CIFS_SB(sb);
178 if (atomic_dec_and_test(&server->active))
179 deactivate_super(sb);
183 cifs_read_super(struct super_block *sb)
186 struct cifs_sb_info *cifs_sb;
187 struct cifs_tcon *tcon;
188 struct timespec64 ts;
191 cifs_sb = CIFS_SB(sb);
192 tcon = cifs_sb_master_tcon(cifs_sb);
194 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIXACL)
195 sb->s_flags |= SB_POSIXACL;
197 if (tcon->snapshot_time)
198 sb->s_flags |= SB_RDONLY;
200 if (tcon->ses->capabilities & tcon->ses->server->vals->cap_large_files)
201 sb->s_maxbytes = MAX_LFS_FILESIZE;
203 sb->s_maxbytes = MAX_NON_LFS;
206 * Some very old servers like DOS and OS/2 used 2 second granularity
207 * (while all current servers use 100ns granularity - see MS-DTYP)
208 * but 1 second is the maximum allowed granularity for the VFS
209 * so for old servers set time granularity to 1 second while for
210 * everything else (current servers) set it to 100ns.
212 if ((tcon->ses->server->vals->protocol_id == SMB10_PROT_ID) &&
213 ((tcon->ses->capabilities &
214 tcon->ses->server->vals->cap_nt_find) == 0) &&
216 sb->s_time_gran = 1000000000; /* 1 second is max allowed gran */
217 ts = cnvrtDosUnixTm(cpu_to_le16(SMB_DATE_MIN), 0, 0);
218 sb->s_time_min = ts.tv_sec;
219 ts = cnvrtDosUnixTm(cpu_to_le16(SMB_DATE_MAX),
220 cpu_to_le16(SMB_TIME_MAX), 0);
221 sb->s_time_max = ts.tv_sec;
224 * Almost every server, including all SMB2+, uses DCE TIME
225 * ie 100 nanosecond units, since 1601. See MS-DTYP and MS-FSCC
227 sb->s_time_gran = 100;
228 ts = cifs_NTtimeToUnix(0);
229 sb->s_time_min = ts.tv_sec;
230 ts = cifs_NTtimeToUnix(cpu_to_le64(S64_MAX));
231 sb->s_time_max = ts.tv_sec;
234 sb->s_magic = CIFS_SUPER_MAGIC;
235 sb->s_op = &cifs_super_ops;
236 sb->s_xattr = cifs_xattr_handlers;
237 rc = super_setup_bdi(sb);
240 /* tune readahead according to rsize if readahead size not set on mount */
241 if (cifs_sb->ctx->rsize == 0)
242 cifs_sb->ctx->rsize =
243 tcon->ses->server->ops->negotiate_rsize(tcon, cifs_sb->ctx);
244 if (cifs_sb->ctx->rasize)
245 sb->s_bdi->ra_pages = cifs_sb->ctx->rasize / PAGE_SIZE;
247 sb->s_bdi->ra_pages = cifs_sb->ctx->rsize / PAGE_SIZE;
249 sb->s_blocksize = CIFS_MAX_MSGSIZE;
250 sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
251 inode = cifs_root_iget(sb);
259 sb->s_d_op = &cifs_ci_dentry_ops;
261 sb->s_d_op = &cifs_dentry_ops;
263 sb->s_root = d_make_root(inode);
269 #ifdef CONFIG_CIFS_NFSD_EXPORT
270 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
271 cifs_dbg(FYI, "export ops supported\n");
272 sb->s_export_op = &cifs_export_ops;
274 #endif /* CONFIG_CIFS_NFSD_EXPORT */
279 cifs_dbg(VFS, "%s: get root inode failed\n", __func__);
283 static void cifs_kill_sb(struct super_block *sb)
285 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
286 struct cifs_tcon *tcon;
287 struct cached_fid *cfid;
288 struct rb_root *root = &cifs_sb->tlink_tree;
289 struct rb_node *node;
290 struct tcon_link *tlink;
293 * We ned to release all dentries for the cached directories
294 * before we kill the sb.
297 for (node = rb_first(root); node; node = rb_next(node)) {
298 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
299 tcon = tlink_tcon(tlink);
303 mutex_lock(&cfid->fid_mutex);
308 mutex_unlock(&cfid->fid_mutex);
311 /* finally release root dentry */
313 cifs_sb->root = NULL;
317 cifs_umount(cifs_sb);
321 cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
323 struct super_block *sb = dentry->d_sb;
324 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
325 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
326 struct TCP_Server_Info *server = tcon->ses->server;
332 if (le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength) > 0)
334 le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength);
336 buf->f_namelen = PATH_MAX;
338 buf->f_fsid.val[0] = tcon->vol_serial_number;
339 /* are using part of create time for more randomness, see man statfs */
340 buf->f_fsid.val[1] = (int)le64_to_cpu(tcon->vol_create_time);
342 buf->f_files = 0; /* undefined */
343 buf->f_ffree = 0; /* unlimited */
345 if (server->ops->queryfs)
346 rc = server->ops->queryfs(xid, tcon, cifs_sb, buf);
352 static long cifs_fallocate(struct file *file, int mode, loff_t off, loff_t len)
354 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
355 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
356 struct TCP_Server_Info *server = tcon->ses->server;
358 if (server->ops->fallocate)
359 return server->ops->fallocate(file, tcon, mode, off, len);
364 static int cifs_permission(struct user_namespace *mnt_userns,
365 struct inode *inode, int mask)
367 struct cifs_sb_info *cifs_sb;
369 cifs_sb = CIFS_SB(inode->i_sb);
371 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
372 if ((mask & MAY_EXEC) && !execute_ok(inode))
376 } else /* file mode might have been restricted at mount time
377 on the client (above and beyond ACL on servers) for
378 servers which do not support setting and viewing mode bits,
379 so allowing client to check permissions is useful */
380 return generic_permission(&init_user_ns, inode, mask);
383 static struct kmem_cache *cifs_inode_cachep;
384 static struct kmem_cache *cifs_req_cachep;
385 static struct kmem_cache *cifs_mid_cachep;
386 static struct kmem_cache *cifs_sm_req_cachep;
387 mempool_t *cifs_sm_req_poolp;
388 mempool_t *cifs_req_poolp;
389 mempool_t *cifs_mid_poolp;
391 static struct inode *
392 cifs_alloc_inode(struct super_block *sb)
394 struct cifsInodeInfo *cifs_inode;
395 cifs_inode = alloc_inode_sb(sb, cifs_inode_cachep, GFP_KERNEL);
398 cifs_inode->cifsAttrs = 0x20; /* default */
399 cifs_inode->time = 0;
401 * Until the file is open and we have gotten oplock info back from the
402 * server, can not assume caching of file data or metadata.
404 cifs_set_oplock_level(cifs_inode, 0);
405 cifs_inode->flags = 0;
406 spin_lock_init(&cifs_inode->writers_lock);
407 cifs_inode->writers = 0;
408 cifs_inode->netfs.inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
409 cifs_inode->server_eof = 0;
410 cifs_inode->uniqueid = 0;
411 cifs_inode->createtime = 0;
412 cifs_inode->epoch = 0;
413 spin_lock_init(&cifs_inode->open_file_lock);
414 generate_random_uuid(cifs_inode->lease_key);
417 * Can not set i_flags here - they get immediately overwritten to zero
420 /* cifs_inode->netfs.inode.i_flags = S_NOATIME | S_NOCMTIME; */
421 INIT_LIST_HEAD(&cifs_inode->openFileList);
422 INIT_LIST_HEAD(&cifs_inode->llist);
423 INIT_LIST_HEAD(&cifs_inode->deferred_closes);
424 spin_lock_init(&cifs_inode->deferred_lock);
425 return &cifs_inode->netfs.inode;
429 cifs_free_inode(struct inode *inode)
431 kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
435 cifs_evict_inode(struct inode *inode)
437 truncate_inode_pages_final(&inode->i_data);
438 if (inode->i_state & I_PINNING_FSCACHE_WB)
439 cifs_fscache_unuse_inode_cookie(inode, true);
440 cifs_fscache_release_inode_cookie(inode);
445 cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
447 struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
448 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
450 seq_puts(s, ",addr=");
452 switch (server->dstaddr.ss_family) {
454 seq_printf(s, "%pI4", &sa->sin_addr.s_addr);
457 seq_printf(s, "%pI6", &sa6->sin6_addr.s6_addr);
458 if (sa6->sin6_scope_id)
459 seq_printf(s, "%%%u", sa6->sin6_scope_id);
462 seq_puts(s, "(unknown)");
465 seq_puts(s, ",rdma");
469 cifs_show_security(struct seq_file *s, struct cifs_ses *ses)
471 if (ses->sectype == Unspecified) {
472 if (ses->user_name == NULL)
473 seq_puts(s, ",sec=none");
477 seq_puts(s, ",sec=");
479 switch (ses->sectype) {
481 seq_puts(s, "ntlmv2");
487 seq_puts(s, "ntlmssp");
490 /* shouldn't ever happen */
491 seq_puts(s, "unknown");
498 if (ses->sectype == Kerberos)
499 seq_printf(s, ",cruid=%u",
500 from_kuid_munged(&init_user_ns, ses->cred_uid));
504 cifs_show_cache_flavor(struct seq_file *s, struct cifs_sb_info *cifs_sb)
506 seq_puts(s, ",cache=");
508 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
509 seq_puts(s, "strict");
510 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
512 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
513 seq_puts(s, "singleclient"); /* assume only one client access */
514 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE)
515 seq_puts(s, "ro"); /* read only caching assumed */
517 seq_puts(s, "loose");
521 * cifs_show_devname() is used so we show the mount device name with correct
522 * format (e.g. forward slashes vs. back slashes) in /proc/mounts
524 static int cifs_show_devname(struct seq_file *m, struct dentry *root)
526 struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
527 char *devname = kstrdup(cifs_sb->ctx->source, GFP_KERNEL);
532 convert_delimiter(devname, '/');
533 /* escape all spaces in share names */
534 seq_escape(m, devname, " \t");
541 * cifs_show_options() is for displaying mount options in /proc/mounts.
542 * Not all settable options are displayed but most of the important
546 cifs_show_options(struct seq_file *s, struct dentry *root)
548 struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
549 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
550 struct sockaddr *srcaddr;
551 srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
553 seq_show_option(s, "vers", tcon->ses->server->vals->version_string);
554 cifs_show_security(s, tcon->ses);
555 cifs_show_cache_flavor(s, cifs_sb);
558 seq_puts(s, ",nolease");
559 if (cifs_sb->ctx->multiuser)
560 seq_puts(s, ",multiuser");
561 else if (tcon->ses->user_name)
562 seq_show_option(s, "username", tcon->ses->user_name);
564 if (tcon->ses->domainName && tcon->ses->domainName[0] != 0)
565 seq_show_option(s, "domain", tcon->ses->domainName);
567 if (srcaddr->sa_family != AF_UNSPEC) {
568 struct sockaddr_in *saddr4;
569 struct sockaddr_in6 *saddr6;
570 saddr4 = (struct sockaddr_in *)srcaddr;
571 saddr6 = (struct sockaddr_in6 *)srcaddr;
572 if (srcaddr->sa_family == AF_INET6)
573 seq_printf(s, ",srcaddr=%pI6c",
575 else if (srcaddr->sa_family == AF_INET)
576 seq_printf(s, ",srcaddr=%pI4",
577 &saddr4->sin_addr.s_addr);
579 seq_printf(s, ",srcaddr=BAD-AF:%i",
580 (int)(srcaddr->sa_family));
583 seq_printf(s, ",uid=%u",
584 from_kuid_munged(&init_user_ns, cifs_sb->ctx->linux_uid));
585 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
586 seq_puts(s, ",forceuid");
588 seq_puts(s, ",noforceuid");
590 seq_printf(s, ",gid=%u",
591 from_kgid_munged(&init_user_ns, cifs_sb->ctx->linux_gid));
592 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
593 seq_puts(s, ",forcegid");
595 seq_puts(s, ",noforcegid");
597 cifs_show_address(s, tcon->ses->server);
600 seq_printf(s, ",file_mode=0%ho,dir_mode=0%ho",
601 cifs_sb->ctx->file_mode,
602 cifs_sb->ctx->dir_mode);
603 if (cifs_sb->ctx->iocharset)
604 seq_printf(s, ",iocharset=%s", cifs_sb->ctx->iocharset);
606 seq_puts(s, ",seal");
607 else if (tcon->ses->server->ignore_signature)
608 seq_puts(s, ",signloosely");
610 seq_puts(s, ",nocase");
612 seq_puts(s, ",nodelete");
613 if (cifs_sb->ctx->no_sparse)
614 seq_puts(s, ",nosparse");
615 if (tcon->local_lease)
616 seq_puts(s, ",locallease");
618 seq_puts(s, ",hard");
620 seq_puts(s, ",soft");
621 if (tcon->use_persistent)
622 seq_puts(s, ",persistenthandles");
623 else if (tcon->use_resilient)
624 seq_puts(s, ",resilienthandles");
625 if (tcon->posix_extensions)
626 seq_puts(s, ",posix");
627 else if (tcon->unix_ext)
628 seq_puts(s, ",unix");
630 seq_puts(s, ",nounix");
631 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
632 seq_puts(s, ",nodfs");
633 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
634 seq_puts(s, ",posixpaths");
635 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
636 seq_puts(s, ",setuids");
637 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
638 seq_puts(s, ",idsfromsid");
639 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
640 seq_puts(s, ",serverino");
641 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
642 seq_puts(s, ",rwpidforward");
643 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL)
644 seq_puts(s, ",forcemand");
645 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
646 seq_puts(s, ",nouser_xattr");
647 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
648 seq_puts(s, ",mapchars");
649 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SFM_CHR)
650 seq_puts(s, ",mapposix");
651 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
653 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
654 seq_puts(s, ",nobrl");
655 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_HANDLE_CACHE)
656 seq_puts(s, ",nohandlecache");
657 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)
658 seq_puts(s, ",modefromsid");
659 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
660 seq_puts(s, ",cifsacl");
661 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
662 seq_puts(s, ",dynperm");
663 if (root->d_sb->s_flags & SB_POSIXACL)
665 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
666 seq_puts(s, ",mfsymlinks");
667 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
669 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)
670 seq_puts(s, ",nostrictsync");
671 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
672 seq_puts(s, ",noperm");
673 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID)
674 seq_printf(s, ",backupuid=%u",
675 from_kuid_munged(&init_user_ns,
676 cifs_sb->ctx->backupuid));
677 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID)
678 seq_printf(s, ",backupgid=%u",
679 from_kgid_munged(&init_user_ns,
680 cifs_sb->ctx->backupgid));
682 seq_printf(s, ",rsize=%u", cifs_sb->ctx->rsize);
683 seq_printf(s, ",wsize=%u", cifs_sb->ctx->wsize);
684 seq_printf(s, ",bsize=%u", cifs_sb->ctx->bsize);
685 if (cifs_sb->ctx->rasize)
686 seq_printf(s, ",rasize=%u", cifs_sb->ctx->rasize);
687 if (tcon->ses->server->min_offload)
688 seq_printf(s, ",esize=%u", tcon->ses->server->min_offload);
689 seq_printf(s, ",echo_interval=%lu",
690 tcon->ses->server->echo_interval / HZ);
692 /* Only display max_credits if it was overridden on mount */
693 if (tcon->ses->server->max_credits != SMB2_MAX_CREDITS_AVAILABLE)
694 seq_printf(s, ",max_credits=%u", tcon->ses->server->max_credits);
696 if (tcon->snapshot_time)
697 seq_printf(s, ",snapshot=%llu", tcon->snapshot_time);
698 if (tcon->handle_timeout)
699 seq_printf(s, ",handletimeout=%u", tcon->handle_timeout);
702 * Display file and directory attribute timeout in seconds.
703 * If file and directory attribute timeout the same then actimeo
704 * was likely specified on mount
706 if (cifs_sb->ctx->acdirmax == cifs_sb->ctx->acregmax)
707 seq_printf(s, ",actimeo=%lu", cifs_sb->ctx->acregmax / HZ);
709 seq_printf(s, ",acdirmax=%lu", cifs_sb->ctx->acdirmax / HZ);
710 seq_printf(s, ",acregmax=%lu", cifs_sb->ctx->acregmax / HZ);
713 if (tcon->ses->chan_max > 1)
714 seq_printf(s, ",multichannel,max_channels=%zu",
715 tcon->ses->chan_max);
717 if (tcon->use_witness)
718 seq_puts(s, ",witness");
723 static void cifs_umount_begin(struct super_block *sb)
725 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
726 struct cifs_tcon *tcon;
731 tcon = cifs_sb_master_tcon(cifs_sb);
733 spin_lock(&cifs_tcp_ses_lock);
734 spin_lock(&tcon->tc_lock);
735 if ((tcon->tc_count > 1) || (tcon->status == TID_EXITING)) {
736 /* we have other mounts to same share or we have
737 already tried to force umount this and woken up
738 all waiting network requests, nothing to do */
739 spin_unlock(&tcon->tc_lock);
740 spin_unlock(&cifs_tcp_ses_lock);
742 } else if (tcon->tc_count == 1)
743 tcon->status = TID_EXITING;
744 spin_unlock(&tcon->tc_lock);
745 spin_unlock(&cifs_tcp_ses_lock);
747 /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
748 /* cancel_notify_requests(tcon); */
749 if (tcon->ses && tcon->ses->server) {
750 cifs_dbg(FYI, "wake up tasks now - umount begin not complete\n");
751 wake_up_all(&tcon->ses->server->request_q);
752 wake_up_all(&tcon->ses->server->response_q);
753 msleep(1); /* yield */
754 /* we have to kick the requests once more */
755 wake_up_all(&tcon->ses->server->response_q);
762 #ifdef CONFIG_CIFS_STATS2
763 static int cifs_show_stats(struct seq_file *s, struct dentry *root)
770 static int cifs_write_inode(struct inode *inode, struct writeback_control *wbc)
772 fscache_unpin_writeback(wbc, cifs_inode_cookie(inode));
776 static int cifs_drop_inode(struct inode *inode)
778 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
780 /* no serverino => unconditional eviction */
781 return !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) ||
782 generic_drop_inode(inode);
785 static const struct super_operations cifs_super_ops = {
786 .statfs = cifs_statfs,
787 .alloc_inode = cifs_alloc_inode,
788 .write_inode = cifs_write_inode,
789 .free_inode = cifs_free_inode,
790 .drop_inode = cifs_drop_inode,
791 .evict_inode = cifs_evict_inode,
792 /* .show_path = cifs_show_path, */ /* Would we ever need show path? */
793 .show_devname = cifs_show_devname,
794 /* .delete_inode = cifs_delete_inode, */ /* Do not need above
795 function unless later we add lazy close of inodes or unless the
796 kernel forgets to call us with the same number of releases (closes)
798 .show_options = cifs_show_options,
799 .umount_begin = cifs_umount_begin,
800 #ifdef CONFIG_CIFS_STATS2
801 .show_stats = cifs_show_stats,
806 * Get root dentry from superblock according to prefix path mount option.
807 * Return dentry with refcount + 1 on success and NULL otherwise.
809 static struct dentry *
810 cifs_get_root(struct smb3_fs_context *ctx, struct super_block *sb)
812 struct dentry *dentry;
813 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
814 char *full_path = NULL;
818 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
819 return dget(sb->s_root);
821 full_path = cifs_build_path_to_root(ctx, cifs_sb,
822 cifs_sb_master_tcon(cifs_sb), 0);
823 if (full_path == NULL)
824 return ERR_PTR(-ENOMEM);
826 cifs_dbg(FYI, "Get root dentry for %s\n", full_path);
828 sep = CIFS_DIR_SEP(cifs_sb);
829 dentry = dget(sb->s_root);
833 struct inode *dir = d_inode(dentry);
834 struct dentry *child;
836 if (!S_ISDIR(dir->i_mode)) {
838 dentry = ERR_PTR(-ENOTDIR);
842 /* skip separators */
849 while (*s && *s != sep)
852 child = lookup_positive_unlocked(p, dentry, s - p);
855 } while (!IS_ERR(dentry));
860 static int cifs_set_super(struct super_block *sb, void *data)
862 struct cifs_mnt_data *mnt_data = data;
863 sb->s_fs_info = mnt_data->cifs_sb;
864 return set_anon_super(sb, NULL);
868 cifs_smb3_do_mount(struct file_system_type *fs_type,
869 int flags, struct smb3_fs_context *old_ctx)
872 struct super_block *sb = NULL;
873 struct cifs_sb_info *cifs_sb = NULL;
874 struct cifs_mnt_data mnt_data;
878 * Prints in Kernel / CIFS log the attempted mount operation
879 * If CIFS_DEBUG && cifs_FYI
882 cifs_dbg(FYI, "Devname: %s flags: %d\n", old_ctx->UNC, flags);
884 cifs_info("Attempting to mount %s\n", old_ctx->UNC);
886 cifs_sb = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
887 if (cifs_sb == NULL) {
888 root = ERR_PTR(-ENOMEM);
892 cifs_sb->ctx = kzalloc(sizeof(struct smb3_fs_context), GFP_KERNEL);
894 root = ERR_PTR(-ENOMEM);
897 rc = smb3_fs_context_dup(cifs_sb->ctx, old_ctx);
903 rc = cifs_setup_volume_info(cifs_sb->ctx, NULL, NULL);
909 rc = cifs_setup_cifs_sb(cifs_sb);
915 rc = cifs_mount(cifs_sb, cifs_sb->ctx);
917 if (!(flags & SB_SILENT))
918 cifs_dbg(VFS, "cifs_mount failed w/return code = %d\n",
924 mnt_data.ctx = cifs_sb->ctx;
925 mnt_data.cifs_sb = cifs_sb;
926 mnt_data.flags = flags;
928 /* BB should we make this contingent on mount parm? */
929 flags |= SB_NODIRATIME | SB_NOATIME;
931 sb = sget(fs_type, cifs_match_super, cifs_set_super, flags, &mnt_data);
934 cifs_umount(cifs_sb);
940 cifs_dbg(FYI, "Use existing superblock\n");
941 cifs_umount(cifs_sb);
944 rc = cifs_read_super(sb);
950 sb->s_flags |= SB_ACTIVE;
953 root = cifs_get_root(cifs_sb ? cifs_sb->ctx : old_ctx, sb);
958 cifs_sb->root = dget(root);
960 cifs_dbg(FYI, "dentry root is: %p\n", root);
964 deactivate_locked_super(sb);
968 if (!sb || IS_ERR(sb)) { /* otherwise kill_sb will handle */
969 kfree(cifs_sb->prepath);
970 smb3_cleanup_fs_context(cifs_sb->ctx);
979 cifs_loose_read_iter(struct kiocb *iocb, struct iov_iter *iter)
982 struct inode *inode = file_inode(iocb->ki_filp);
984 if (iocb->ki_flags & IOCB_DIRECT)
985 return cifs_user_readv(iocb, iter);
987 rc = cifs_revalidate_mapping(inode);
991 return generic_file_read_iter(iocb, iter);
994 static ssize_t cifs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
996 struct inode *inode = file_inode(iocb->ki_filp);
997 struct cifsInodeInfo *cinode = CIFS_I(inode);
1001 if (iocb->ki_filp->f_flags & O_DIRECT) {
1002 written = cifs_user_writev(iocb, from);
1003 if (written > 0 && CIFS_CACHE_READ(cinode)) {
1004 cifs_zap_mapping(inode);
1006 "Set no oplock for inode=%p after a write operation\n",
1013 written = cifs_get_writer(cinode);
1017 written = generic_file_write_iter(iocb, from);
1019 if (CIFS_CACHE_WRITE(CIFS_I(inode)))
1022 rc = filemap_fdatawrite(inode->i_mapping);
1024 cifs_dbg(FYI, "cifs_file_write_iter: %d rc on %p inode\n",
1028 cifs_put_writer(cinode);
1032 static loff_t cifs_llseek(struct file *file, loff_t offset, int whence)
1034 struct cifsFileInfo *cfile = file->private_data;
1035 struct cifs_tcon *tcon;
1038 * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
1039 * the cached file length
1041 if (whence != SEEK_SET && whence != SEEK_CUR) {
1043 struct inode *inode = file_inode(file);
1046 * We need to be sure that all dirty pages are written and the
1047 * server has the newest file length.
1049 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
1050 inode->i_mapping->nrpages != 0) {
1051 rc = filemap_fdatawait(inode->i_mapping);
1053 mapping_set_error(inode->i_mapping, rc);
1058 * Some applications poll for the file length in this strange
1059 * way so we must seek to end on non-oplocked files by
1060 * setting the revalidate time to zero.
1062 CIFS_I(inode)->time = 0;
1064 rc = cifs_revalidate_file_attr(file);
1068 if (cfile && cfile->tlink) {
1069 tcon = tlink_tcon(cfile->tlink);
1070 if (tcon->ses->server->ops->llseek)
1071 return tcon->ses->server->ops->llseek(file, tcon,
1074 return generic_file_llseek(file, offset, whence);
1078 cifs_setlease(struct file *file, long arg, struct file_lock **lease, void **priv)
1081 * Note that this is called by vfs setlease with i_lock held to
1082 * protect *lease from going away.
1084 struct inode *inode = file_inode(file);
1085 struct cifsFileInfo *cfile = file->private_data;
1087 if (!(S_ISREG(inode->i_mode)))
1090 /* Check if file is oplocked if this is request for new lease */
1091 if (arg == F_UNLCK ||
1092 ((arg == F_RDLCK) && CIFS_CACHE_READ(CIFS_I(inode))) ||
1093 ((arg == F_WRLCK) && CIFS_CACHE_WRITE(CIFS_I(inode))))
1094 return generic_setlease(file, arg, lease, priv);
1095 else if (tlink_tcon(cfile->tlink)->local_lease &&
1096 !CIFS_CACHE_READ(CIFS_I(inode)))
1098 * If the server claims to support oplock on this file, then we
1099 * still need to check oplock even if the local_lease mount
1100 * option is set, but there are servers which do not support
1101 * oplock for which this mount option may be useful if the user
1102 * knows that the file won't be changed on the server by anyone
1105 return generic_setlease(file, arg, lease, priv);
1110 struct file_system_type cifs_fs_type = {
1111 .owner = THIS_MODULE,
1113 .init_fs_context = smb3_init_fs_context,
1114 .parameters = smb3_fs_parameters,
1115 .kill_sb = cifs_kill_sb,
1116 .fs_flags = FS_RENAME_DOES_D_MOVE,
1118 MODULE_ALIAS_FS("cifs");
1120 struct file_system_type smb3_fs_type = {
1121 .owner = THIS_MODULE,
1123 .init_fs_context = smb3_init_fs_context,
1124 .parameters = smb3_fs_parameters,
1125 .kill_sb = cifs_kill_sb,
1126 .fs_flags = FS_RENAME_DOES_D_MOVE,
1128 MODULE_ALIAS_FS("smb3");
1129 MODULE_ALIAS("smb3");
1131 const struct inode_operations cifs_dir_inode_ops = {
1132 .create = cifs_create,
1133 .atomic_open = cifs_atomic_open,
1134 .lookup = cifs_lookup,
1135 .getattr = cifs_getattr,
1136 .unlink = cifs_unlink,
1137 .link = cifs_hardlink,
1138 .mkdir = cifs_mkdir,
1139 .rmdir = cifs_rmdir,
1140 .rename = cifs_rename2,
1141 .permission = cifs_permission,
1142 .setattr = cifs_setattr,
1143 .symlink = cifs_symlink,
1144 .mknod = cifs_mknod,
1145 .listxattr = cifs_listxattr,
1148 const struct inode_operations cifs_file_inode_ops = {
1149 .setattr = cifs_setattr,
1150 .getattr = cifs_getattr,
1151 .permission = cifs_permission,
1152 .listxattr = cifs_listxattr,
1153 .fiemap = cifs_fiemap,
1156 const struct inode_operations cifs_symlink_inode_ops = {
1157 .get_link = cifs_get_link,
1158 .permission = cifs_permission,
1159 .listxattr = cifs_listxattr,
1162 static loff_t cifs_remap_file_range(struct file *src_file, loff_t off,
1163 struct file *dst_file, loff_t destoff, loff_t len,
1164 unsigned int remap_flags)
1166 struct inode *src_inode = file_inode(src_file);
1167 struct inode *target_inode = file_inode(dst_file);
1168 struct cifsFileInfo *smb_file_src = src_file->private_data;
1169 struct cifsFileInfo *smb_file_target;
1170 struct cifs_tcon *target_tcon;
1174 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
1177 cifs_dbg(FYI, "clone range\n");
1181 if (!src_file->private_data || !dst_file->private_data) {
1183 cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
1187 smb_file_target = dst_file->private_data;
1188 target_tcon = tlink_tcon(smb_file_target->tlink);
1191 * Note: cifs case is easier than btrfs since server responsible for
1192 * checks for proper open modes and file type and if it wants
1193 * server could even support copy of range where source = target
1195 lock_two_nondirectories(target_inode, src_inode);
1198 len = src_inode->i_size - off;
1200 cifs_dbg(FYI, "about to flush pages\n");
1201 /* should we flush first and last page first */
1202 truncate_inode_pages_range(&target_inode->i_data, destoff,
1203 PAGE_ALIGN(destoff + len)-1);
1205 if (target_tcon->ses->server->ops->duplicate_extents)
1206 rc = target_tcon->ses->server->ops->duplicate_extents(xid,
1207 smb_file_src, smb_file_target, off, len, destoff);
1211 /* force revalidate of size and timestamps of target file now
1212 that target is updated on the server */
1213 CIFS_I(target_inode)->time = 0;
1214 /* although unlocking in the reverse order from locking is not
1215 strictly necessary here it is a little cleaner to be consistent */
1216 unlock_two_nondirectories(src_inode, target_inode);
1219 return rc < 0 ? rc : len;
1222 ssize_t cifs_file_copychunk_range(unsigned int xid,
1223 struct file *src_file, loff_t off,
1224 struct file *dst_file, loff_t destoff,
1225 size_t len, unsigned int flags)
1227 struct inode *src_inode = file_inode(src_file);
1228 struct inode *target_inode = file_inode(dst_file);
1229 struct cifsFileInfo *smb_file_src;
1230 struct cifsFileInfo *smb_file_target;
1231 struct cifs_tcon *src_tcon;
1232 struct cifs_tcon *target_tcon;
1235 cifs_dbg(FYI, "copychunk range\n");
1237 if (!src_file->private_data || !dst_file->private_data) {
1239 cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
1244 smb_file_target = dst_file->private_data;
1245 smb_file_src = src_file->private_data;
1246 src_tcon = tlink_tcon(smb_file_src->tlink);
1247 target_tcon = tlink_tcon(smb_file_target->tlink);
1249 if (src_tcon->ses != target_tcon->ses) {
1250 cifs_dbg(VFS, "source and target of copy not on same server\n");
1255 if (!target_tcon->ses->server->ops->copychunk_range)
1259 * Note: cifs case is easier than btrfs since server responsible for
1260 * checks for proper open modes and file type and if it wants
1261 * server could even support copy of range where source = target
1263 lock_two_nondirectories(target_inode, src_inode);
1265 cifs_dbg(FYI, "about to flush pages\n");
1266 /* should we flush first and last page first */
1267 truncate_inode_pages(&target_inode->i_data, 0);
1269 rc = file_modified(dst_file);
1271 rc = target_tcon->ses->server->ops->copychunk_range(xid,
1272 smb_file_src, smb_file_target, off, len, destoff);
1274 file_accessed(src_file);
1276 /* force revalidate of size and timestamps of target file now
1277 * that target is updated on the server
1279 CIFS_I(target_inode)->time = 0;
1280 /* although unlocking in the reverse order from locking is not
1281 * strictly necessary here it is a little cleaner to be consistent
1283 unlock_two_nondirectories(src_inode, target_inode);
1290 * Directory operations under CIFS/SMB2/SMB3 are synchronous, so fsync()
1291 * is a dummy operation.
1293 static int cifs_dir_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1295 cifs_dbg(FYI, "Sync directory - name: %pD datasync: 0x%x\n",
1301 static ssize_t cifs_copy_file_range(struct file *src_file, loff_t off,
1302 struct file *dst_file, loff_t destoff,
1303 size_t len, unsigned int flags)
1305 unsigned int xid = get_xid();
1307 struct cifsFileInfo *cfile = dst_file->private_data;
1309 if (cfile->swapfile)
1312 rc = cifs_file_copychunk_range(xid, src_file, off, dst_file, destoff,
1316 if (rc == -EOPNOTSUPP || rc == -EXDEV)
1317 rc = generic_copy_file_range(src_file, off, dst_file,
1318 destoff, len, flags);
1322 const struct file_operations cifs_file_ops = {
1323 .read_iter = cifs_loose_read_iter,
1324 .write_iter = cifs_file_write_iter,
1326 .release = cifs_close,
1328 .flock = cifs_flock,
1329 .fsync = cifs_fsync,
1330 .flush = cifs_flush,
1331 .mmap = cifs_file_mmap,
1332 .splice_read = generic_file_splice_read,
1333 .splice_write = iter_file_splice_write,
1334 .llseek = cifs_llseek,
1335 .unlocked_ioctl = cifs_ioctl,
1336 .copy_file_range = cifs_copy_file_range,
1337 .remap_file_range = cifs_remap_file_range,
1338 .setlease = cifs_setlease,
1339 .fallocate = cifs_fallocate,
1342 const struct file_operations cifs_file_strict_ops = {
1343 .read_iter = cifs_strict_readv,
1344 .write_iter = cifs_strict_writev,
1346 .release = cifs_close,
1348 .flock = cifs_flock,
1349 .fsync = cifs_strict_fsync,
1350 .flush = cifs_flush,
1351 .mmap = cifs_file_strict_mmap,
1352 .splice_read = generic_file_splice_read,
1353 .splice_write = iter_file_splice_write,
1354 .llseek = cifs_llseek,
1355 .unlocked_ioctl = cifs_ioctl,
1356 .copy_file_range = cifs_copy_file_range,
1357 .remap_file_range = cifs_remap_file_range,
1358 .setlease = cifs_setlease,
1359 .fallocate = cifs_fallocate,
1362 const struct file_operations cifs_file_direct_ops = {
1363 .read_iter = cifs_direct_readv,
1364 .write_iter = cifs_direct_writev,
1366 .release = cifs_close,
1368 .flock = cifs_flock,
1369 .fsync = cifs_fsync,
1370 .flush = cifs_flush,
1371 .mmap = cifs_file_mmap,
1372 .splice_read = generic_file_splice_read,
1373 .splice_write = iter_file_splice_write,
1374 .unlocked_ioctl = cifs_ioctl,
1375 .copy_file_range = cifs_copy_file_range,
1376 .remap_file_range = cifs_remap_file_range,
1377 .llseek = cifs_llseek,
1378 .setlease = cifs_setlease,
1379 .fallocate = cifs_fallocate,
1382 const struct file_operations cifs_file_nobrl_ops = {
1383 .read_iter = cifs_loose_read_iter,
1384 .write_iter = cifs_file_write_iter,
1386 .release = cifs_close,
1387 .fsync = cifs_fsync,
1388 .flush = cifs_flush,
1389 .mmap = cifs_file_mmap,
1390 .splice_read = generic_file_splice_read,
1391 .splice_write = iter_file_splice_write,
1392 .llseek = cifs_llseek,
1393 .unlocked_ioctl = cifs_ioctl,
1394 .copy_file_range = cifs_copy_file_range,
1395 .remap_file_range = cifs_remap_file_range,
1396 .setlease = cifs_setlease,
1397 .fallocate = cifs_fallocate,
1400 const struct file_operations cifs_file_strict_nobrl_ops = {
1401 .read_iter = cifs_strict_readv,
1402 .write_iter = cifs_strict_writev,
1404 .release = cifs_close,
1405 .fsync = cifs_strict_fsync,
1406 .flush = cifs_flush,
1407 .mmap = cifs_file_strict_mmap,
1408 .splice_read = generic_file_splice_read,
1409 .splice_write = iter_file_splice_write,
1410 .llseek = cifs_llseek,
1411 .unlocked_ioctl = cifs_ioctl,
1412 .copy_file_range = cifs_copy_file_range,
1413 .remap_file_range = cifs_remap_file_range,
1414 .setlease = cifs_setlease,
1415 .fallocate = cifs_fallocate,
1418 const struct file_operations cifs_file_direct_nobrl_ops = {
1419 .read_iter = cifs_direct_readv,
1420 .write_iter = cifs_direct_writev,
1422 .release = cifs_close,
1423 .fsync = cifs_fsync,
1424 .flush = cifs_flush,
1425 .mmap = cifs_file_mmap,
1426 .splice_read = generic_file_splice_read,
1427 .splice_write = iter_file_splice_write,
1428 .unlocked_ioctl = cifs_ioctl,
1429 .copy_file_range = cifs_copy_file_range,
1430 .remap_file_range = cifs_remap_file_range,
1431 .llseek = cifs_llseek,
1432 .setlease = cifs_setlease,
1433 .fallocate = cifs_fallocate,
1436 const struct file_operations cifs_dir_ops = {
1437 .iterate_shared = cifs_readdir,
1438 .release = cifs_closedir,
1439 .read = generic_read_dir,
1440 .unlocked_ioctl = cifs_ioctl,
1441 .copy_file_range = cifs_copy_file_range,
1442 .remap_file_range = cifs_remap_file_range,
1443 .llseek = generic_file_llseek,
1444 .fsync = cifs_dir_fsync,
1448 cifs_init_once(void *inode)
1450 struct cifsInodeInfo *cifsi = inode;
1452 inode_init_once(&cifsi->netfs.inode);
1453 init_rwsem(&cifsi->lock_sem);
1457 cifs_init_inodecache(void)
1459 cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
1460 sizeof(struct cifsInodeInfo),
1461 0, (SLAB_RECLAIM_ACCOUNT|
1462 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1464 if (cifs_inode_cachep == NULL)
1471 cifs_destroy_inodecache(void)
1474 * Make sure all delayed rcu free inodes are flushed before we
1478 kmem_cache_destroy(cifs_inode_cachep);
1482 cifs_init_request_bufs(void)
1485 * SMB2 maximum header size is bigger than CIFS one - no problems to
1486 * allocate some more bytes for CIFS.
1488 size_t max_hdr_size = MAX_SMB2_HDR_SIZE;
1490 if (CIFSMaxBufSize < 8192) {
1491 /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
1492 Unicode path name has to fit in any SMB/CIFS path based frames */
1493 CIFSMaxBufSize = 8192;
1494 } else if (CIFSMaxBufSize > 1024*127) {
1495 CIFSMaxBufSize = 1024 * 127;
1497 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
1500 cifs_dbg(VFS, "CIFSMaxBufSize %d 0x%x\n",
1501 CIFSMaxBufSize, CIFSMaxBufSize);
1503 cifs_req_cachep = kmem_cache_create_usercopy("cifs_request",
1504 CIFSMaxBufSize + max_hdr_size, 0,
1505 SLAB_HWCACHE_ALIGN, 0,
1506 CIFSMaxBufSize + max_hdr_size,
1508 if (cifs_req_cachep == NULL)
1511 if (cifs_min_rcv < 1)
1513 else if (cifs_min_rcv > 64) {
1515 cifs_dbg(VFS, "cifs_min_rcv set to maximum (64)\n");
1518 cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
1521 if (cifs_req_poolp == NULL) {
1522 kmem_cache_destroy(cifs_req_cachep);
1525 /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
1526 almost all handle based requests (but not write response, nor is it
1527 sufficient for path based requests). A smaller size would have
1528 been more efficient (compacting multiple slab items on one 4k page)
1529 for the case in which debug was on, but this larger size allows
1530 more SMBs to use small buffer alloc and is still much more
1531 efficient to alloc 1 per page off the slab compared to 17K (5page)
1532 alloc of large cifs buffers even when page debugging is on */
1533 cifs_sm_req_cachep = kmem_cache_create_usercopy("cifs_small_rq",
1534 MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
1535 0, MAX_CIFS_SMALL_BUFFER_SIZE, NULL);
1536 if (cifs_sm_req_cachep == NULL) {
1537 mempool_destroy(cifs_req_poolp);
1538 kmem_cache_destroy(cifs_req_cachep);
1542 if (cifs_min_small < 2)
1544 else if (cifs_min_small > 256) {
1545 cifs_min_small = 256;
1546 cifs_dbg(FYI, "cifs_min_small set to maximum (256)\n");
1549 cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
1550 cifs_sm_req_cachep);
1552 if (cifs_sm_req_poolp == NULL) {
1553 mempool_destroy(cifs_req_poolp);
1554 kmem_cache_destroy(cifs_req_cachep);
1555 kmem_cache_destroy(cifs_sm_req_cachep);
1563 cifs_destroy_request_bufs(void)
1565 mempool_destroy(cifs_req_poolp);
1566 kmem_cache_destroy(cifs_req_cachep);
1567 mempool_destroy(cifs_sm_req_poolp);
1568 kmem_cache_destroy(cifs_sm_req_cachep);
1571 static int init_mids(void)
1573 cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
1574 sizeof(struct mid_q_entry), 0,
1575 SLAB_HWCACHE_ALIGN, NULL);
1576 if (cifs_mid_cachep == NULL)
1579 /* 3 is a reasonable minimum number of simultaneous operations */
1580 cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
1581 if (cifs_mid_poolp == NULL) {
1582 kmem_cache_destroy(cifs_mid_cachep);
1589 static void destroy_mids(void)
1591 mempool_destroy(cifs_mid_poolp);
1592 kmem_cache_destroy(cifs_mid_cachep);
1600 INIT_LIST_HEAD(&cifs_tcp_ses_list);
1602 * Initialize Global counters
1604 atomic_set(&sesInfoAllocCount, 0);
1605 atomic_set(&tconInfoAllocCount, 0);
1606 atomic_set(&tcpSesNextId, 0);
1607 atomic_set(&tcpSesAllocCount, 0);
1608 atomic_set(&tcpSesReconnectCount, 0);
1609 atomic_set(&tconInfoReconnectCount, 0);
1611 atomic_set(&buf_alloc_count, 0);
1612 atomic_set(&small_buf_alloc_count, 0);
1613 #ifdef CONFIG_CIFS_STATS2
1614 atomic_set(&total_buf_alloc_count, 0);
1615 atomic_set(&total_small_buf_alloc_count, 0);
1616 if (slow_rsp_threshold < 1)
1617 cifs_dbg(FYI, "slow_response_threshold msgs disabled\n");
1618 else if (slow_rsp_threshold > 32767)
1620 "slow response threshold set higher than recommended (0 to 32767)\n");
1621 #endif /* CONFIG_CIFS_STATS2 */
1623 atomic_set(&mid_count, 0);
1624 GlobalCurrentXid = 0;
1625 GlobalTotalActiveXid = 0;
1626 GlobalMaxActiveXid = 0;
1627 spin_lock_init(&cifs_tcp_ses_lock);
1628 spin_lock_init(&GlobalMid_Lock);
1630 cifs_lock_secret = get_random_u32();
1632 if (cifs_max_pending < 2) {
1633 cifs_max_pending = 2;
1634 cifs_dbg(FYI, "cifs_max_pending set to min of 2\n");
1635 } else if (cifs_max_pending > CIFS_MAX_REQ) {
1636 cifs_max_pending = CIFS_MAX_REQ;
1637 cifs_dbg(FYI, "cifs_max_pending set to max of %u\n",
1641 cifsiod_wq = alloc_workqueue("cifsiod", WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1644 goto out_clean_proc;
1648 * Consider in future setting limit!=0 maybe to min(num_of_cores - 1, 3)
1649 * so that we don't launch too many worker threads but
1650 * Documentation/core-api/workqueue.rst recommends setting it to 0
1653 /* WQ_UNBOUND allows decrypt tasks to run on any CPU */
1654 decrypt_wq = alloc_workqueue("smb3decryptd",
1655 WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1658 goto out_destroy_cifsiod_wq;
1661 fileinfo_put_wq = alloc_workqueue("cifsfileinfoput",
1662 WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1663 if (!fileinfo_put_wq) {
1665 goto out_destroy_decrypt_wq;
1668 cifsoplockd_wq = alloc_workqueue("cifsoplockd",
1669 WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1670 if (!cifsoplockd_wq) {
1672 goto out_destroy_fileinfo_put_wq;
1675 deferredclose_wq = alloc_workqueue("deferredclose",
1676 WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1677 if (!deferredclose_wq) {
1679 goto out_destroy_cifsoplockd_wq;
1682 rc = cifs_init_inodecache();
1684 goto out_destroy_deferredclose_wq;
1688 goto out_destroy_inodecache;
1690 rc = cifs_init_request_bufs();
1692 goto out_destroy_mids;
1694 #ifdef CONFIG_CIFS_DFS_UPCALL
1695 rc = dfs_cache_init();
1697 goto out_destroy_request_bufs;
1698 #endif /* CONFIG_CIFS_DFS_UPCALL */
1699 #ifdef CONFIG_CIFS_UPCALL
1700 rc = init_cifs_spnego();
1702 goto out_destroy_dfs_cache;
1703 #endif /* CONFIG_CIFS_UPCALL */
1704 #ifdef CONFIG_CIFS_SWN_UPCALL
1705 rc = cifs_genl_init();
1707 goto out_register_key_type;
1708 #endif /* CONFIG_CIFS_SWN_UPCALL */
1710 rc = init_cifs_idmap();
1712 goto out_cifs_swn_init;
1714 rc = register_filesystem(&cifs_fs_type);
1716 goto out_init_cifs_idmap;
1718 rc = register_filesystem(&smb3_fs_type);
1720 unregister_filesystem(&cifs_fs_type);
1721 goto out_init_cifs_idmap;
1726 out_init_cifs_idmap:
1729 #ifdef CONFIG_CIFS_SWN_UPCALL
1731 out_register_key_type:
1733 #ifdef CONFIG_CIFS_UPCALL
1735 out_destroy_dfs_cache:
1737 #ifdef CONFIG_CIFS_DFS_UPCALL
1738 dfs_cache_destroy();
1739 out_destroy_request_bufs:
1741 cifs_destroy_request_bufs();
1744 out_destroy_inodecache:
1745 cifs_destroy_inodecache();
1746 out_destroy_deferredclose_wq:
1747 destroy_workqueue(deferredclose_wq);
1748 out_destroy_cifsoplockd_wq:
1749 destroy_workqueue(cifsoplockd_wq);
1750 out_destroy_fileinfo_put_wq:
1751 destroy_workqueue(fileinfo_put_wq);
1752 out_destroy_decrypt_wq:
1753 destroy_workqueue(decrypt_wq);
1754 out_destroy_cifsiod_wq:
1755 destroy_workqueue(cifsiod_wq);
1764 cifs_dbg(NOISY, "exit_smb3\n");
1765 unregister_filesystem(&cifs_fs_type);
1766 unregister_filesystem(&smb3_fs_type);
1767 cifs_dfs_release_automount_timer();
1769 #ifdef CONFIG_CIFS_SWN_UPCALL
1772 #ifdef CONFIG_CIFS_UPCALL
1775 #ifdef CONFIG_CIFS_DFS_UPCALL
1776 dfs_cache_destroy();
1778 cifs_destroy_request_bufs();
1780 cifs_destroy_inodecache();
1781 destroy_workqueue(deferredclose_wq);
1782 destroy_workqueue(cifsoplockd_wq);
1783 destroy_workqueue(decrypt_wq);
1784 destroy_workqueue(fileinfo_put_wq);
1785 destroy_workqueue(cifsiod_wq);
1789 MODULE_AUTHOR("Steve French");
1790 MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
1792 ("VFS to access SMB3 servers e.g. Samba, Macs, Azure and Windows (and "
1793 "also older servers complying with the SNIA CIFS Specification)");
1794 MODULE_VERSION(CIFS_VERSION);
1795 MODULE_SOFTDEP("ecb");
1796 MODULE_SOFTDEP("hmac");
1797 MODULE_SOFTDEP("md5");
1798 MODULE_SOFTDEP("nls");
1799 MODULE_SOFTDEP("aes");
1800 MODULE_SOFTDEP("cmac");
1801 MODULE_SOFTDEP("sha256");
1802 MODULE_SOFTDEP("sha512");
1803 MODULE_SOFTDEP("aead2");
1804 MODULE_SOFTDEP("ccm");
1805 MODULE_SOFTDEP("gcm");
1806 module_init(init_cifs)
1807 module_exit(exit_cifs)