1 // SPDX-License-Identifier: LGPL-2.1
5 * Copyright (C) International Business Machines Corp., 2002,2008
6 * Author(s): Steve French (sfrench@us.ibm.com)
8 * Common Internet FileSystem (CIFS) client
12 /* Note that BB means BUGBUG (ie something to fix eventually) */
14 #include <linux/module.h>
16 #include <linux/mount.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/list.h>
20 #include <linux/seq_file.h>
21 #include <linux/vfs.h>
22 #include <linux/mempool.h>
23 #include <linux/delay.h>
24 #include <linux/kthread.h>
25 #include <linux/freezer.h>
26 #include <linux/namei.h>
27 #include <linux/random.h>
28 #include <linux/uuid.h>
29 #include <linux/xattr.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"
43 #ifdef CONFIG_CIFS_DFS_UPCALL
44 #include "dfs_cache.h"
46 #ifdef CONFIG_CIFS_SWN_UPCALL
49 #include "fs_context.h"
52 * DOS dates from 1980/1/1 through 2107/12/31
53 * Protocol specifications indicate the range should be to 119, which
54 * limits maximum year to 2099. But this range has not been checked.
56 #define SMB_DATE_MAX (127<<9 | 12<<5 | 31)
57 #define SMB_DATE_MIN (0<<9 | 1<<5 | 1)
58 #define SMB_TIME_MAX (23<<11 | 59<<5 | 29)
62 bool enable_oplocks = true;
63 bool linuxExtEnabled = true;
64 bool lookupCacheEnabled = true;
65 bool disable_legacy_dialects; /* false by default */
66 bool enable_gcm_256 = true;
67 bool require_gcm_256; /* false by default */
68 bool enable_negotiate_signing; /* false by default */
69 unsigned int global_secflags = CIFSSEC_DEF;
70 /* unsigned int ntlmv2_support = 0; */
71 unsigned int sign_CIFS_PDUs = 1;
72 static const struct super_operations cifs_super_ops;
73 unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
74 module_param(CIFSMaxBufSize, uint, 0444);
75 MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header) "
77 "Default: 16384 Range: 8192 to 130048");
78 unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
79 module_param(cifs_min_rcv, uint, 0444);
80 MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
82 unsigned int cifs_min_small = 30;
83 module_param(cifs_min_small, uint, 0444);
84 MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
86 unsigned int cifs_max_pending = CIFS_MAX_REQ;
87 module_param(cifs_max_pending, uint, 0444);
88 MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server for "
89 "CIFS/SMB1 dialect (N/A for SMB3) "
90 "Default: 32767 Range: 2 to 32767.");
91 #ifdef CONFIG_CIFS_STATS2
92 unsigned int slow_rsp_threshold = 1;
93 module_param(slow_rsp_threshold, uint, 0644);
94 MODULE_PARM_DESC(slow_rsp_threshold, "Amount of time (in seconds) to wait "
95 "before logging that a response is delayed. "
96 "Default: 1 (if set to 0 disables msg).");
99 module_param(enable_oplocks, bool, 0644);
100 MODULE_PARM_DESC(enable_oplocks, "Enable or disable oplocks. Default: y/Y/1");
102 module_param(enable_gcm_256, bool, 0644);
103 MODULE_PARM_DESC(enable_gcm_256, "Enable requesting strongest (256 bit) GCM encryption. Default: n/N/0");
105 module_param(require_gcm_256, bool, 0644);
106 MODULE_PARM_DESC(require_gcm_256, "Require strongest (256 bit) GCM encryption. Default: n/N/0");
108 module_param(enable_negotiate_signing, bool, 0644);
109 MODULE_PARM_DESC(enable_negotiate_signing, "Enable negotiating packet signing algorithm with server. Default: n/N/0");
111 module_param(disable_legacy_dialects, bool, 0644);
112 MODULE_PARM_DESC(disable_legacy_dialects, "To improve security it may be "
113 "helpful to restrict the ability to "
114 "override the default dialects (SMB2.1, "
115 "SMB3 and SMB3.02) on mount with old "
116 "dialects (CIFS/SMB1 and SMB2) since "
117 "vers=1.0 (CIFS/SMB1) and vers=2.0 are weaker"
118 " and less secure. Default: n/N/0");
120 extern mempool_t *cifs_sm_req_poolp;
121 extern mempool_t *cifs_req_poolp;
122 extern mempool_t *cifs_mid_poolp;
124 struct workqueue_struct *cifsiod_wq;
125 struct workqueue_struct *decrypt_wq;
126 struct workqueue_struct *fileinfo_put_wq;
127 struct workqueue_struct *cifsoplockd_wq;
128 struct workqueue_struct *deferredclose_wq;
129 __u32 cifs_lock_secret;
132 * Bumps refcount for cifs super block.
133 * Note that it should be only called if a referece to VFS super block is
134 * already held, e.g. in open-type syscalls context. Otherwise it can race with
135 * atomic_dec_and_test in deactivate_locked_super.
138 cifs_sb_active(struct super_block *sb)
140 struct cifs_sb_info *server = CIFS_SB(sb);
142 if (atomic_inc_return(&server->active) == 1)
143 atomic_inc(&sb->s_active);
147 cifs_sb_deactive(struct super_block *sb)
149 struct cifs_sb_info *server = CIFS_SB(sb);
151 if (atomic_dec_and_test(&server->active))
152 deactivate_super(sb);
156 cifs_read_super(struct super_block *sb)
159 struct cifs_sb_info *cifs_sb;
160 struct cifs_tcon *tcon;
161 struct timespec64 ts;
164 cifs_sb = CIFS_SB(sb);
165 tcon = cifs_sb_master_tcon(cifs_sb);
167 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIXACL)
168 sb->s_flags |= SB_POSIXACL;
170 if (tcon->snapshot_time)
171 sb->s_flags |= SB_RDONLY;
173 if (tcon->ses->capabilities & tcon->ses->server->vals->cap_large_files)
174 sb->s_maxbytes = MAX_LFS_FILESIZE;
176 sb->s_maxbytes = MAX_NON_LFS;
179 * Some very old servers like DOS and OS/2 used 2 second granularity
180 * (while all current servers use 100ns granularity - see MS-DTYP)
181 * but 1 second is the maximum allowed granularity for the VFS
182 * so for old servers set time granularity to 1 second while for
183 * everything else (current servers) set it to 100ns.
185 if ((tcon->ses->server->vals->protocol_id == SMB10_PROT_ID) &&
186 ((tcon->ses->capabilities &
187 tcon->ses->server->vals->cap_nt_find) == 0) &&
189 sb->s_time_gran = 1000000000; /* 1 second is max allowed gran */
190 ts = cnvrtDosUnixTm(cpu_to_le16(SMB_DATE_MIN), 0, 0);
191 sb->s_time_min = ts.tv_sec;
192 ts = cnvrtDosUnixTm(cpu_to_le16(SMB_DATE_MAX),
193 cpu_to_le16(SMB_TIME_MAX), 0);
194 sb->s_time_max = ts.tv_sec;
197 * Almost every server, including all SMB2+, uses DCE TIME
198 * ie 100 nanosecond units, since 1601. See MS-DTYP and MS-FSCC
200 sb->s_time_gran = 100;
201 ts = cifs_NTtimeToUnix(0);
202 sb->s_time_min = ts.tv_sec;
203 ts = cifs_NTtimeToUnix(cpu_to_le64(S64_MAX));
204 sb->s_time_max = ts.tv_sec;
207 sb->s_magic = CIFS_MAGIC_NUMBER;
208 sb->s_op = &cifs_super_ops;
209 sb->s_xattr = cifs_xattr_handlers;
210 rc = super_setup_bdi(sb);
213 /* tune readahead according to rsize if readahead size not set on mount */
214 if (cifs_sb->ctx->rasize)
215 sb->s_bdi->ra_pages = cifs_sb->ctx->rasize / PAGE_SIZE;
217 sb->s_bdi->ra_pages = cifs_sb->ctx->rsize / PAGE_SIZE;
219 sb->s_blocksize = CIFS_MAX_MSGSIZE;
220 sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
221 inode = cifs_root_iget(sb);
229 sb->s_d_op = &cifs_ci_dentry_ops;
231 sb->s_d_op = &cifs_dentry_ops;
233 sb->s_root = d_make_root(inode);
239 #ifdef CONFIG_CIFS_NFSD_EXPORT
240 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
241 cifs_dbg(FYI, "export ops supported\n");
242 sb->s_export_op = &cifs_export_ops;
244 #endif /* CONFIG_CIFS_NFSD_EXPORT */
249 cifs_dbg(VFS, "%s: get root inode failed\n", __func__);
253 static void cifs_kill_sb(struct super_block *sb)
255 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
256 struct cifs_tcon *tcon;
257 struct cached_fid *cfid;
260 * We ned to release all dentries for the cached directories
261 * before we kill the sb.
265 cifs_sb->root = NULL;
267 tcon = cifs_sb_master_tcon(cifs_sb);
270 mutex_lock(&cfid->fid_mutex);
276 mutex_unlock(&cfid->fid_mutex);
280 cifs_umount(cifs_sb);
284 cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
286 struct super_block *sb = dentry->d_sb;
287 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
288 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
289 struct TCP_Server_Info *server = tcon->ses->server;
295 if (le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength) > 0)
297 le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength);
299 buf->f_namelen = PATH_MAX;
301 buf->f_fsid.val[0] = tcon->vol_serial_number;
302 /* are using part of create time for more randomness, see man statfs */
303 buf->f_fsid.val[1] = (int)le64_to_cpu(tcon->vol_create_time);
305 buf->f_files = 0; /* undefined */
306 buf->f_ffree = 0; /* unlimited */
308 if (server->ops->queryfs)
309 rc = server->ops->queryfs(xid, tcon, cifs_sb, buf);
315 static long cifs_fallocate(struct file *file, int mode, loff_t off, loff_t len)
317 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
318 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
319 struct TCP_Server_Info *server = tcon->ses->server;
321 if (server->ops->fallocate)
322 return server->ops->fallocate(file, tcon, mode, off, len);
327 static int cifs_permission(struct user_namespace *mnt_userns,
328 struct inode *inode, int mask)
330 struct cifs_sb_info *cifs_sb;
332 cifs_sb = CIFS_SB(inode->i_sb);
334 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
335 if ((mask & MAY_EXEC) && !execute_ok(inode))
339 } else /* file mode might have been restricted at mount time
340 on the client (above and beyond ACL on servers) for
341 servers which do not support setting and viewing mode bits,
342 so allowing client to check permissions is useful */
343 return generic_permission(&init_user_ns, inode, mask);
346 static struct kmem_cache *cifs_inode_cachep;
347 static struct kmem_cache *cifs_req_cachep;
348 static struct kmem_cache *cifs_mid_cachep;
349 static struct kmem_cache *cifs_sm_req_cachep;
350 mempool_t *cifs_sm_req_poolp;
351 mempool_t *cifs_req_poolp;
352 mempool_t *cifs_mid_poolp;
354 static struct inode *
355 cifs_alloc_inode(struct super_block *sb)
357 struct cifsInodeInfo *cifs_inode;
358 cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
361 cifs_inode->cifsAttrs = 0x20; /* default */
362 cifs_inode->time = 0;
364 * Until the file is open and we have gotten oplock info back from the
365 * server, can not assume caching of file data or metadata.
367 cifs_set_oplock_level(cifs_inode, 0);
368 cifs_inode->flags = 0;
369 spin_lock_init(&cifs_inode->writers_lock);
370 cifs_inode->writers = 0;
371 cifs_inode->vfs_inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
372 cifs_inode->server_eof = 0;
373 cifs_inode->uniqueid = 0;
374 cifs_inode->createtime = 0;
375 cifs_inode->epoch = 0;
376 spin_lock_init(&cifs_inode->open_file_lock);
377 generate_random_uuid(cifs_inode->lease_key);
380 * Can not set i_flags here - they get immediately overwritten to zero
383 /* cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME; */
384 INIT_LIST_HEAD(&cifs_inode->openFileList);
385 INIT_LIST_HEAD(&cifs_inode->llist);
386 INIT_LIST_HEAD(&cifs_inode->deferred_closes);
387 spin_lock_init(&cifs_inode->deferred_lock);
388 return &cifs_inode->vfs_inode;
392 cifs_free_inode(struct inode *inode)
394 kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
398 cifs_evict_inode(struct inode *inode)
400 truncate_inode_pages_final(&inode->i_data);
405 cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
407 struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
408 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
410 seq_puts(s, ",addr=");
412 switch (server->dstaddr.ss_family) {
414 seq_printf(s, "%pI4", &sa->sin_addr.s_addr);
417 seq_printf(s, "%pI6", &sa6->sin6_addr.s6_addr);
418 if (sa6->sin6_scope_id)
419 seq_printf(s, "%%%u", sa6->sin6_scope_id);
422 seq_puts(s, "(unknown)");
425 seq_puts(s, ",rdma");
429 cifs_show_security(struct seq_file *s, struct cifs_ses *ses)
431 if (ses->sectype == Unspecified) {
432 if (ses->user_name == NULL)
433 seq_puts(s, ",sec=none");
437 seq_puts(s, ",sec=");
439 switch (ses->sectype) {
441 seq_puts(s, "ntlmv2");
447 seq_puts(s, "ntlmssp");
450 /* shouldn't ever happen */
451 seq_puts(s, "unknown");
458 if (ses->sectype == Kerberos)
459 seq_printf(s, ",cruid=%u",
460 from_kuid_munged(&init_user_ns, ses->cred_uid));
464 cifs_show_cache_flavor(struct seq_file *s, struct cifs_sb_info *cifs_sb)
466 seq_puts(s, ",cache=");
468 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
469 seq_puts(s, "strict");
470 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
472 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
473 seq_puts(s, "singleclient"); /* assume only one client access */
474 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE)
475 seq_puts(s, "ro"); /* read only caching assumed */
477 seq_puts(s, "loose");
481 * cifs_show_devname() is used so we show the mount device name with correct
482 * format (e.g. forward slashes vs. back slashes) in /proc/mounts
484 static int cifs_show_devname(struct seq_file *m, struct dentry *root)
486 struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
487 char *devname = kstrdup(cifs_sb->ctx->source, GFP_KERNEL);
492 convert_delimiter(devname, '/');
493 /* escape all spaces in share names */
494 seq_escape(m, devname, " \t");
501 * cifs_show_options() is for displaying mount options in /proc/mounts.
502 * Not all settable options are displayed but most of the important
506 cifs_show_options(struct seq_file *s, struct dentry *root)
508 struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
509 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
510 struct sockaddr *srcaddr;
511 srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
513 seq_show_option(s, "vers", tcon->ses->server->vals->version_string);
514 cifs_show_security(s, tcon->ses);
515 cifs_show_cache_flavor(s, cifs_sb);
518 seq_puts(s, ",nolease");
519 if (cifs_sb->ctx->multiuser)
520 seq_puts(s, ",multiuser");
521 else if (tcon->ses->user_name)
522 seq_show_option(s, "username", tcon->ses->user_name);
524 if (tcon->ses->domainName && tcon->ses->domainName[0] != 0)
525 seq_show_option(s, "domain", tcon->ses->domainName);
527 if (srcaddr->sa_family != AF_UNSPEC) {
528 struct sockaddr_in *saddr4;
529 struct sockaddr_in6 *saddr6;
530 saddr4 = (struct sockaddr_in *)srcaddr;
531 saddr6 = (struct sockaddr_in6 *)srcaddr;
532 if (srcaddr->sa_family == AF_INET6)
533 seq_printf(s, ",srcaddr=%pI6c",
535 else if (srcaddr->sa_family == AF_INET)
536 seq_printf(s, ",srcaddr=%pI4",
537 &saddr4->sin_addr.s_addr);
539 seq_printf(s, ",srcaddr=BAD-AF:%i",
540 (int)(srcaddr->sa_family));
543 seq_printf(s, ",uid=%u",
544 from_kuid_munged(&init_user_ns, cifs_sb->ctx->linux_uid));
545 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
546 seq_puts(s, ",forceuid");
548 seq_puts(s, ",noforceuid");
550 seq_printf(s, ",gid=%u",
551 from_kgid_munged(&init_user_ns, cifs_sb->ctx->linux_gid));
552 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
553 seq_puts(s, ",forcegid");
555 seq_puts(s, ",noforcegid");
557 cifs_show_address(s, tcon->ses->server);
560 seq_printf(s, ",file_mode=0%ho,dir_mode=0%ho",
561 cifs_sb->ctx->file_mode,
562 cifs_sb->ctx->dir_mode);
563 if (cifs_sb->ctx->iocharset)
564 seq_printf(s, ",iocharset=%s", cifs_sb->ctx->iocharset);
566 seq_puts(s, ",seal");
567 else if (tcon->ses->server->ignore_signature)
568 seq_puts(s, ",signloosely");
570 seq_puts(s, ",nocase");
572 seq_puts(s, ",nodelete");
573 if (tcon->local_lease)
574 seq_puts(s, ",locallease");
576 seq_puts(s, ",hard");
578 seq_puts(s, ",soft");
579 if (tcon->use_persistent)
580 seq_puts(s, ",persistenthandles");
581 else if (tcon->use_resilient)
582 seq_puts(s, ",resilienthandles");
583 if (tcon->posix_extensions)
584 seq_puts(s, ",posix");
585 else if (tcon->unix_ext)
586 seq_puts(s, ",unix");
588 seq_puts(s, ",nounix");
589 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
590 seq_puts(s, ",nodfs");
591 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
592 seq_puts(s, ",posixpaths");
593 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
594 seq_puts(s, ",setuids");
595 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
596 seq_puts(s, ",idsfromsid");
597 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
598 seq_puts(s, ",serverino");
599 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
600 seq_puts(s, ",rwpidforward");
601 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL)
602 seq_puts(s, ",forcemand");
603 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
604 seq_puts(s, ",nouser_xattr");
605 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
606 seq_puts(s, ",mapchars");
607 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SFM_CHR)
608 seq_puts(s, ",mapposix");
609 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
611 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
612 seq_puts(s, ",nobrl");
613 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_HANDLE_CACHE)
614 seq_puts(s, ",nohandlecache");
615 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)
616 seq_puts(s, ",modefromsid");
617 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
618 seq_puts(s, ",cifsacl");
619 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
620 seq_puts(s, ",dynperm");
621 if (root->d_sb->s_flags & SB_POSIXACL)
623 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
624 seq_puts(s, ",mfsymlinks");
625 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
627 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)
628 seq_puts(s, ",nostrictsync");
629 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
630 seq_puts(s, ",noperm");
631 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID)
632 seq_printf(s, ",backupuid=%u",
633 from_kuid_munged(&init_user_ns,
634 cifs_sb->ctx->backupuid));
635 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID)
636 seq_printf(s, ",backupgid=%u",
637 from_kgid_munged(&init_user_ns,
638 cifs_sb->ctx->backupgid));
640 seq_printf(s, ",rsize=%u", cifs_sb->ctx->rsize);
641 seq_printf(s, ",wsize=%u", cifs_sb->ctx->wsize);
642 seq_printf(s, ",bsize=%u", cifs_sb->ctx->bsize);
643 if (cifs_sb->ctx->rasize)
644 seq_printf(s, ",rasize=%u", cifs_sb->ctx->rasize);
645 if (tcon->ses->server->min_offload)
646 seq_printf(s, ",esize=%u", tcon->ses->server->min_offload);
647 seq_printf(s, ",echo_interval=%lu",
648 tcon->ses->server->echo_interval / HZ);
650 /* Only display max_credits if it was overridden on mount */
651 if (tcon->ses->server->max_credits != SMB2_MAX_CREDITS_AVAILABLE)
652 seq_printf(s, ",max_credits=%u", tcon->ses->server->max_credits);
654 if (tcon->snapshot_time)
655 seq_printf(s, ",snapshot=%llu", tcon->snapshot_time);
656 if (tcon->handle_timeout)
657 seq_printf(s, ",handletimeout=%u", tcon->handle_timeout);
660 * Display file and directory attribute timeout in seconds.
661 * If file and directory attribute timeout the same then actimeo
662 * was likely specified on mount
664 if (cifs_sb->ctx->acdirmax == cifs_sb->ctx->acregmax)
665 seq_printf(s, ",actimeo=%lu", cifs_sb->ctx->acregmax / HZ);
667 seq_printf(s, ",acdirmax=%lu", cifs_sb->ctx->acdirmax / HZ);
668 seq_printf(s, ",acregmax=%lu", cifs_sb->ctx->acregmax / HZ);
671 if (tcon->ses->chan_max > 1)
672 seq_printf(s, ",multichannel,max_channels=%zu",
673 tcon->ses->chan_max);
675 if (tcon->use_witness)
676 seq_puts(s, ",witness");
681 static void cifs_umount_begin(struct super_block *sb)
683 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
684 struct cifs_tcon *tcon;
689 tcon = cifs_sb_master_tcon(cifs_sb);
691 spin_lock(&cifs_tcp_ses_lock);
692 if ((tcon->tc_count > 1) || (tcon->tidStatus == CifsExiting)) {
693 /* we have other mounts to same share or we have
694 already tried to force umount this and woken up
695 all waiting network requests, nothing to do */
696 spin_unlock(&cifs_tcp_ses_lock);
698 } else if (tcon->tc_count == 1)
699 tcon->tidStatus = CifsExiting;
700 spin_unlock(&cifs_tcp_ses_lock);
702 /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
703 /* cancel_notify_requests(tcon); */
704 if (tcon->ses && tcon->ses->server) {
705 cifs_dbg(FYI, "wake up tasks now - umount begin not complete\n");
706 wake_up_all(&tcon->ses->server->request_q);
707 wake_up_all(&tcon->ses->server->response_q);
708 msleep(1); /* yield */
709 /* we have to kick the requests once more */
710 wake_up_all(&tcon->ses->server->response_q);
717 #ifdef CONFIG_CIFS_STATS2
718 static int cifs_show_stats(struct seq_file *s, struct dentry *root)
725 static int cifs_drop_inode(struct inode *inode)
727 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
729 /* no serverino => unconditional eviction */
730 return !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) ||
731 generic_drop_inode(inode);
734 static const struct super_operations cifs_super_ops = {
735 .statfs = cifs_statfs,
736 .alloc_inode = cifs_alloc_inode,
737 .free_inode = cifs_free_inode,
738 .drop_inode = cifs_drop_inode,
739 .evict_inode = cifs_evict_inode,
740 /* .show_path = cifs_show_path, */ /* Would we ever need show path? */
741 .show_devname = cifs_show_devname,
742 /* .delete_inode = cifs_delete_inode, */ /* Do not need above
743 function unless later we add lazy close of inodes or unless the
744 kernel forgets to call us with the same number of releases (closes)
746 .show_options = cifs_show_options,
747 .umount_begin = cifs_umount_begin,
748 #ifdef CONFIG_CIFS_STATS2
749 .show_stats = cifs_show_stats,
754 * Get root dentry from superblock according to prefix path mount option.
755 * Return dentry with refcount + 1 on success and NULL otherwise.
757 static struct dentry *
758 cifs_get_root(struct smb3_fs_context *ctx, struct super_block *sb)
760 struct dentry *dentry;
761 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
762 char *full_path = NULL;
766 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
767 return dget(sb->s_root);
769 full_path = cifs_build_path_to_root(ctx, cifs_sb,
770 cifs_sb_master_tcon(cifs_sb), 0);
771 if (full_path == NULL)
772 return ERR_PTR(-ENOMEM);
774 cifs_dbg(FYI, "Get root dentry for %s\n", full_path);
776 sep = CIFS_DIR_SEP(cifs_sb);
777 dentry = dget(sb->s_root);
781 struct inode *dir = d_inode(dentry);
782 struct dentry *child;
784 if (!S_ISDIR(dir->i_mode)) {
786 dentry = ERR_PTR(-ENOTDIR);
790 /* skip separators */
797 while (*s && *s != sep)
800 child = lookup_positive_unlocked(p, dentry, s - p);
803 } while (!IS_ERR(dentry));
808 static int cifs_set_super(struct super_block *sb, void *data)
810 struct cifs_mnt_data *mnt_data = data;
811 sb->s_fs_info = mnt_data->cifs_sb;
812 return set_anon_super(sb, NULL);
816 cifs_smb3_do_mount(struct file_system_type *fs_type,
817 int flags, struct smb3_fs_context *old_ctx)
820 struct super_block *sb;
821 struct cifs_sb_info *cifs_sb = NULL;
822 struct cifs_mnt_data mnt_data;
826 * Prints in Kernel / CIFS log the attempted mount operation
827 * If CIFS_DEBUG && cifs_FYI
830 cifs_dbg(FYI, "Devname: %s flags: %d\n", old_ctx->UNC, flags);
832 cifs_info("Attempting to mount %s\n", old_ctx->UNC);
834 cifs_sb = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
835 if (cifs_sb == NULL) {
836 root = ERR_PTR(-ENOMEM);
840 cifs_sb->ctx = kzalloc(sizeof(struct smb3_fs_context), GFP_KERNEL);
842 root = ERR_PTR(-ENOMEM);
845 rc = smb3_fs_context_dup(cifs_sb->ctx, old_ctx);
851 rc = cifs_setup_volume_info(cifs_sb->ctx, NULL, NULL);
857 rc = cifs_setup_cifs_sb(cifs_sb);
863 rc = cifs_mount(cifs_sb, cifs_sb->ctx);
865 if (!(flags & SB_SILENT))
866 cifs_dbg(VFS, "cifs_mount failed w/return code = %d\n",
872 mnt_data.ctx = cifs_sb->ctx;
873 mnt_data.cifs_sb = cifs_sb;
874 mnt_data.flags = flags;
876 /* BB should we make this contingent on mount parm? */
877 flags |= SB_NODIRATIME | SB_NOATIME;
879 sb = sget(fs_type, cifs_match_super, cifs_set_super, flags, &mnt_data);
882 cifs_umount(cifs_sb);
888 cifs_dbg(FYI, "Use existing superblock\n");
889 cifs_umount(cifs_sb);
892 rc = cifs_read_super(sb);
898 sb->s_flags |= SB_ACTIVE;
901 root = cifs_get_root(cifs_sb ? cifs_sb->ctx : old_ctx, sb);
906 cifs_sb->root = dget(root);
908 cifs_dbg(FYI, "dentry root is: %p\n", root);
912 deactivate_locked_super(sb);
915 kfree(cifs_sb->prepath);
916 smb3_cleanup_fs_context(cifs_sb->ctx);
924 cifs_loose_read_iter(struct kiocb *iocb, struct iov_iter *iter)
927 struct inode *inode = file_inode(iocb->ki_filp);
929 if (iocb->ki_filp->f_flags & O_DIRECT)
930 return cifs_user_readv(iocb, iter);
932 rc = cifs_revalidate_mapping(inode);
936 return generic_file_read_iter(iocb, iter);
939 static ssize_t cifs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
941 struct inode *inode = file_inode(iocb->ki_filp);
942 struct cifsInodeInfo *cinode = CIFS_I(inode);
946 if (iocb->ki_filp->f_flags & O_DIRECT) {
947 written = cifs_user_writev(iocb, from);
948 if (written > 0 && CIFS_CACHE_READ(cinode)) {
949 cifs_zap_mapping(inode);
951 "Set no oplock for inode=%p after a write operation\n",
958 written = cifs_get_writer(cinode);
962 written = generic_file_write_iter(iocb, from);
964 if (CIFS_CACHE_WRITE(CIFS_I(inode)))
967 rc = filemap_fdatawrite(inode->i_mapping);
969 cifs_dbg(FYI, "cifs_file_write_iter: %d rc on %p inode\n",
973 cifs_put_writer(cinode);
977 static loff_t cifs_llseek(struct file *file, loff_t offset, int whence)
979 struct cifsFileInfo *cfile = file->private_data;
980 struct cifs_tcon *tcon;
983 * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
984 * the cached file length
986 if (whence != SEEK_SET && whence != SEEK_CUR) {
988 struct inode *inode = file_inode(file);
991 * We need to be sure that all dirty pages are written and the
992 * server has the newest file length.
994 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
995 inode->i_mapping->nrpages != 0) {
996 rc = filemap_fdatawait(inode->i_mapping);
998 mapping_set_error(inode->i_mapping, rc);
1003 * Some applications poll for the file length in this strange
1004 * way so we must seek to end on non-oplocked files by
1005 * setting the revalidate time to zero.
1007 CIFS_I(inode)->time = 0;
1009 rc = cifs_revalidate_file_attr(file);
1013 if (cfile && cfile->tlink) {
1014 tcon = tlink_tcon(cfile->tlink);
1015 if (tcon->ses->server->ops->llseek)
1016 return tcon->ses->server->ops->llseek(file, tcon,
1019 return generic_file_llseek(file, offset, whence);
1023 cifs_setlease(struct file *file, long arg, struct file_lock **lease, void **priv)
1026 * Note that this is called by vfs setlease with i_lock held to
1027 * protect *lease from going away.
1029 struct inode *inode = file_inode(file);
1030 struct cifsFileInfo *cfile = file->private_data;
1032 if (!(S_ISREG(inode->i_mode)))
1035 /* Check if file is oplocked if this is request for new lease */
1036 if (arg == F_UNLCK ||
1037 ((arg == F_RDLCK) && CIFS_CACHE_READ(CIFS_I(inode))) ||
1038 ((arg == F_WRLCK) && CIFS_CACHE_WRITE(CIFS_I(inode))))
1039 return generic_setlease(file, arg, lease, priv);
1040 else if (tlink_tcon(cfile->tlink)->local_lease &&
1041 !CIFS_CACHE_READ(CIFS_I(inode)))
1043 * If the server claims to support oplock on this file, then we
1044 * still need to check oplock even if the local_lease mount
1045 * option is set, but there are servers which do not support
1046 * oplock for which this mount option may be useful if the user
1047 * knows that the file won't be changed on the server by anyone
1050 return generic_setlease(file, arg, lease, priv);
1055 struct file_system_type cifs_fs_type = {
1056 .owner = THIS_MODULE,
1058 .init_fs_context = smb3_init_fs_context,
1059 .parameters = smb3_fs_parameters,
1060 .kill_sb = cifs_kill_sb,
1061 .fs_flags = FS_RENAME_DOES_D_MOVE,
1063 MODULE_ALIAS_FS("cifs");
1065 static struct file_system_type smb3_fs_type = {
1066 .owner = THIS_MODULE,
1068 .init_fs_context = smb3_init_fs_context,
1069 .parameters = smb3_fs_parameters,
1070 .kill_sb = cifs_kill_sb,
1071 .fs_flags = FS_RENAME_DOES_D_MOVE,
1073 MODULE_ALIAS_FS("smb3");
1074 MODULE_ALIAS("smb3");
1076 const struct inode_operations cifs_dir_inode_ops = {
1077 .create = cifs_create,
1078 .atomic_open = cifs_atomic_open,
1079 .lookup = cifs_lookup,
1080 .getattr = cifs_getattr,
1081 .unlink = cifs_unlink,
1082 .link = cifs_hardlink,
1083 .mkdir = cifs_mkdir,
1084 .rmdir = cifs_rmdir,
1085 .rename = cifs_rename2,
1086 .permission = cifs_permission,
1087 .setattr = cifs_setattr,
1088 .symlink = cifs_symlink,
1089 .mknod = cifs_mknod,
1090 .listxattr = cifs_listxattr,
1093 const struct inode_operations cifs_file_inode_ops = {
1094 .setattr = cifs_setattr,
1095 .getattr = cifs_getattr,
1096 .permission = cifs_permission,
1097 .listxattr = cifs_listxattr,
1098 .fiemap = cifs_fiemap,
1101 const struct inode_operations cifs_symlink_inode_ops = {
1102 .get_link = cifs_get_link,
1103 .permission = cifs_permission,
1104 .listxattr = cifs_listxattr,
1107 static loff_t cifs_remap_file_range(struct file *src_file, loff_t off,
1108 struct file *dst_file, loff_t destoff, loff_t len,
1109 unsigned int remap_flags)
1111 struct inode *src_inode = file_inode(src_file);
1112 struct inode *target_inode = file_inode(dst_file);
1113 struct cifsFileInfo *smb_file_src = src_file->private_data;
1114 struct cifsFileInfo *smb_file_target;
1115 struct cifs_tcon *target_tcon;
1119 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
1122 cifs_dbg(FYI, "clone range\n");
1126 if (!src_file->private_data || !dst_file->private_data) {
1128 cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
1132 smb_file_target = dst_file->private_data;
1133 target_tcon = tlink_tcon(smb_file_target->tlink);
1136 * Note: cifs case is easier than btrfs since server responsible for
1137 * checks for proper open modes and file type and if it wants
1138 * server could even support copy of range where source = target
1140 lock_two_nondirectories(target_inode, src_inode);
1143 len = src_inode->i_size - off;
1145 cifs_dbg(FYI, "about to flush pages\n");
1146 /* should we flush first and last page first */
1147 truncate_inode_pages_range(&target_inode->i_data, destoff,
1148 PAGE_ALIGN(destoff + len)-1);
1150 if (target_tcon->ses->server->ops->duplicate_extents)
1151 rc = target_tcon->ses->server->ops->duplicate_extents(xid,
1152 smb_file_src, smb_file_target, off, len, destoff);
1156 /* force revalidate of size and timestamps of target file now
1157 that target is updated on the server */
1158 CIFS_I(target_inode)->time = 0;
1159 /* although unlocking in the reverse order from locking is not
1160 strictly necessary here it is a little cleaner to be consistent */
1161 unlock_two_nondirectories(src_inode, target_inode);
1164 return rc < 0 ? rc : len;
1167 ssize_t cifs_file_copychunk_range(unsigned int xid,
1168 struct file *src_file, loff_t off,
1169 struct file *dst_file, loff_t destoff,
1170 size_t len, unsigned int flags)
1172 struct inode *src_inode = file_inode(src_file);
1173 struct inode *target_inode = file_inode(dst_file);
1174 struct cifsFileInfo *smb_file_src;
1175 struct cifsFileInfo *smb_file_target;
1176 struct cifs_tcon *src_tcon;
1177 struct cifs_tcon *target_tcon;
1180 cifs_dbg(FYI, "copychunk range\n");
1182 if (!src_file->private_data || !dst_file->private_data) {
1184 cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
1189 smb_file_target = dst_file->private_data;
1190 smb_file_src = src_file->private_data;
1191 src_tcon = tlink_tcon(smb_file_src->tlink);
1192 target_tcon = tlink_tcon(smb_file_target->tlink);
1194 if (src_tcon->ses != target_tcon->ses) {
1195 cifs_dbg(VFS, "source and target of copy not on same server\n");
1200 if (!target_tcon->ses->server->ops->copychunk_range)
1204 * Note: cifs case is easier than btrfs since server responsible for
1205 * checks for proper open modes and file type and if it wants
1206 * server could even support copy of range where source = target
1208 lock_two_nondirectories(target_inode, src_inode);
1210 cifs_dbg(FYI, "about to flush pages\n");
1211 /* should we flush first and last page first */
1212 truncate_inode_pages(&target_inode->i_data, 0);
1214 rc = file_modified(dst_file);
1216 rc = target_tcon->ses->server->ops->copychunk_range(xid,
1217 smb_file_src, smb_file_target, off, len, destoff);
1219 file_accessed(src_file);
1221 /* force revalidate of size and timestamps of target file now
1222 * that target is updated on the server
1224 CIFS_I(target_inode)->time = 0;
1225 /* although unlocking in the reverse order from locking is not
1226 * strictly necessary here it is a little cleaner to be consistent
1228 unlock_two_nondirectories(src_inode, target_inode);
1235 * Directory operations under CIFS/SMB2/SMB3 are synchronous, so fsync()
1236 * is a dummy operation.
1238 static int cifs_dir_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1240 cifs_dbg(FYI, "Sync directory - name: %pD datasync: 0x%x\n",
1246 static ssize_t cifs_copy_file_range(struct file *src_file, loff_t off,
1247 struct file *dst_file, loff_t destoff,
1248 size_t len, unsigned int flags)
1250 unsigned int xid = get_xid();
1252 struct cifsFileInfo *cfile = dst_file->private_data;
1254 if (cfile->swapfile)
1257 rc = cifs_file_copychunk_range(xid, src_file, off, dst_file, destoff,
1261 if (rc == -EOPNOTSUPP || rc == -EXDEV)
1262 rc = generic_copy_file_range(src_file, off, dst_file,
1263 destoff, len, flags);
1267 const struct file_operations cifs_file_ops = {
1268 .read_iter = cifs_loose_read_iter,
1269 .write_iter = cifs_file_write_iter,
1271 .release = cifs_close,
1273 .flock = cifs_flock,
1274 .fsync = cifs_fsync,
1275 .flush = cifs_flush,
1276 .mmap = cifs_file_mmap,
1277 .splice_read = generic_file_splice_read,
1278 .splice_write = iter_file_splice_write,
1279 .llseek = cifs_llseek,
1280 .unlocked_ioctl = cifs_ioctl,
1281 .copy_file_range = cifs_copy_file_range,
1282 .remap_file_range = cifs_remap_file_range,
1283 .setlease = cifs_setlease,
1284 .fallocate = cifs_fallocate,
1287 const struct file_operations cifs_file_strict_ops = {
1288 .read_iter = cifs_strict_readv,
1289 .write_iter = cifs_strict_writev,
1291 .release = cifs_close,
1293 .flock = cifs_flock,
1294 .fsync = cifs_strict_fsync,
1295 .flush = cifs_flush,
1296 .mmap = cifs_file_strict_mmap,
1297 .splice_read = generic_file_splice_read,
1298 .splice_write = iter_file_splice_write,
1299 .llseek = cifs_llseek,
1300 .unlocked_ioctl = cifs_ioctl,
1301 .copy_file_range = cifs_copy_file_range,
1302 .remap_file_range = cifs_remap_file_range,
1303 .setlease = cifs_setlease,
1304 .fallocate = cifs_fallocate,
1307 const struct file_operations cifs_file_direct_ops = {
1308 .read_iter = cifs_direct_readv,
1309 .write_iter = cifs_direct_writev,
1311 .release = cifs_close,
1313 .flock = cifs_flock,
1314 .fsync = cifs_fsync,
1315 .flush = cifs_flush,
1316 .mmap = cifs_file_mmap,
1317 .splice_read = generic_file_splice_read,
1318 .splice_write = iter_file_splice_write,
1319 .unlocked_ioctl = cifs_ioctl,
1320 .copy_file_range = cifs_copy_file_range,
1321 .remap_file_range = cifs_remap_file_range,
1322 .llseek = cifs_llseek,
1323 .setlease = cifs_setlease,
1324 .fallocate = cifs_fallocate,
1327 const struct file_operations cifs_file_nobrl_ops = {
1328 .read_iter = cifs_loose_read_iter,
1329 .write_iter = cifs_file_write_iter,
1331 .release = cifs_close,
1332 .fsync = cifs_fsync,
1333 .flush = cifs_flush,
1334 .mmap = cifs_file_mmap,
1335 .splice_read = generic_file_splice_read,
1336 .splice_write = iter_file_splice_write,
1337 .llseek = cifs_llseek,
1338 .unlocked_ioctl = cifs_ioctl,
1339 .copy_file_range = cifs_copy_file_range,
1340 .remap_file_range = cifs_remap_file_range,
1341 .setlease = cifs_setlease,
1342 .fallocate = cifs_fallocate,
1345 const struct file_operations cifs_file_strict_nobrl_ops = {
1346 .read_iter = cifs_strict_readv,
1347 .write_iter = cifs_strict_writev,
1349 .release = cifs_close,
1350 .fsync = cifs_strict_fsync,
1351 .flush = cifs_flush,
1352 .mmap = cifs_file_strict_mmap,
1353 .splice_read = generic_file_splice_read,
1354 .splice_write = iter_file_splice_write,
1355 .llseek = cifs_llseek,
1356 .unlocked_ioctl = cifs_ioctl,
1357 .copy_file_range = cifs_copy_file_range,
1358 .remap_file_range = cifs_remap_file_range,
1359 .setlease = cifs_setlease,
1360 .fallocate = cifs_fallocate,
1363 const struct file_operations cifs_file_direct_nobrl_ops = {
1364 .read_iter = cifs_direct_readv,
1365 .write_iter = cifs_direct_writev,
1367 .release = cifs_close,
1368 .fsync = cifs_fsync,
1369 .flush = cifs_flush,
1370 .mmap = cifs_file_mmap,
1371 .splice_read = generic_file_splice_read,
1372 .splice_write = iter_file_splice_write,
1373 .unlocked_ioctl = cifs_ioctl,
1374 .copy_file_range = cifs_copy_file_range,
1375 .remap_file_range = cifs_remap_file_range,
1376 .llseek = cifs_llseek,
1377 .setlease = cifs_setlease,
1378 .fallocate = cifs_fallocate,
1381 const struct file_operations cifs_dir_ops = {
1382 .iterate_shared = cifs_readdir,
1383 .release = cifs_closedir,
1384 .read = generic_read_dir,
1385 .unlocked_ioctl = cifs_ioctl,
1386 .copy_file_range = cifs_copy_file_range,
1387 .remap_file_range = cifs_remap_file_range,
1388 .llseek = generic_file_llseek,
1389 .fsync = cifs_dir_fsync,
1393 cifs_init_once(void *inode)
1395 struct cifsInodeInfo *cifsi = inode;
1397 inode_init_once(&cifsi->vfs_inode);
1398 init_rwsem(&cifsi->lock_sem);
1402 cifs_init_inodecache(void)
1404 cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
1405 sizeof(struct cifsInodeInfo),
1406 0, (SLAB_RECLAIM_ACCOUNT|
1407 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1409 if (cifs_inode_cachep == NULL)
1416 cifs_destroy_inodecache(void)
1419 * Make sure all delayed rcu free inodes are flushed before we
1423 kmem_cache_destroy(cifs_inode_cachep);
1427 cifs_init_request_bufs(void)
1430 * SMB2 maximum header size is bigger than CIFS one - no problems to
1431 * allocate some more bytes for CIFS.
1433 size_t max_hdr_size = MAX_SMB2_HDR_SIZE;
1435 if (CIFSMaxBufSize < 8192) {
1436 /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
1437 Unicode path name has to fit in any SMB/CIFS path based frames */
1438 CIFSMaxBufSize = 8192;
1439 } else if (CIFSMaxBufSize > 1024*127) {
1440 CIFSMaxBufSize = 1024 * 127;
1442 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
1445 cifs_dbg(VFS, "CIFSMaxBufSize %d 0x%x\n",
1446 CIFSMaxBufSize, CIFSMaxBufSize);
1448 cifs_req_cachep = kmem_cache_create_usercopy("cifs_request",
1449 CIFSMaxBufSize + max_hdr_size, 0,
1450 SLAB_HWCACHE_ALIGN, 0,
1451 CIFSMaxBufSize + max_hdr_size,
1453 if (cifs_req_cachep == NULL)
1456 if (cifs_min_rcv < 1)
1458 else if (cifs_min_rcv > 64) {
1460 cifs_dbg(VFS, "cifs_min_rcv set to maximum (64)\n");
1463 cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
1466 if (cifs_req_poolp == NULL) {
1467 kmem_cache_destroy(cifs_req_cachep);
1470 /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
1471 almost all handle based requests (but not write response, nor is it
1472 sufficient for path based requests). A smaller size would have
1473 been more efficient (compacting multiple slab items on one 4k page)
1474 for the case in which debug was on, but this larger size allows
1475 more SMBs to use small buffer alloc and is still much more
1476 efficient to alloc 1 per page off the slab compared to 17K (5page)
1477 alloc of large cifs buffers even when page debugging is on */
1478 cifs_sm_req_cachep = kmem_cache_create_usercopy("cifs_small_rq",
1479 MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
1480 0, MAX_CIFS_SMALL_BUFFER_SIZE, NULL);
1481 if (cifs_sm_req_cachep == NULL) {
1482 mempool_destroy(cifs_req_poolp);
1483 kmem_cache_destroy(cifs_req_cachep);
1487 if (cifs_min_small < 2)
1489 else if (cifs_min_small > 256) {
1490 cifs_min_small = 256;
1491 cifs_dbg(FYI, "cifs_min_small set to maximum (256)\n");
1494 cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
1495 cifs_sm_req_cachep);
1497 if (cifs_sm_req_poolp == NULL) {
1498 mempool_destroy(cifs_req_poolp);
1499 kmem_cache_destroy(cifs_req_cachep);
1500 kmem_cache_destroy(cifs_sm_req_cachep);
1508 cifs_destroy_request_bufs(void)
1510 mempool_destroy(cifs_req_poolp);
1511 kmem_cache_destroy(cifs_req_cachep);
1512 mempool_destroy(cifs_sm_req_poolp);
1513 kmem_cache_destroy(cifs_sm_req_cachep);
1517 cifs_init_mids(void)
1519 cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
1520 sizeof(struct mid_q_entry), 0,
1521 SLAB_HWCACHE_ALIGN, NULL);
1522 if (cifs_mid_cachep == NULL)
1525 /* 3 is a reasonable minimum number of simultaneous operations */
1526 cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
1527 if (cifs_mid_poolp == NULL) {
1528 kmem_cache_destroy(cifs_mid_cachep);
1536 cifs_destroy_mids(void)
1538 mempool_destroy(cifs_mid_poolp);
1539 kmem_cache_destroy(cifs_mid_cachep);
1547 INIT_LIST_HEAD(&cifs_tcp_ses_list);
1549 * Initialize Global counters
1551 atomic_set(&sesInfoAllocCount, 0);
1552 atomic_set(&tconInfoAllocCount, 0);
1553 atomic_set(&tcpSesNextId, 0);
1554 atomic_set(&tcpSesAllocCount, 0);
1555 atomic_set(&tcpSesReconnectCount, 0);
1556 atomic_set(&tconInfoReconnectCount, 0);
1558 atomic_set(&bufAllocCount, 0);
1559 atomic_set(&smBufAllocCount, 0);
1560 #ifdef CONFIG_CIFS_STATS2
1561 atomic_set(&totBufAllocCount, 0);
1562 atomic_set(&totSmBufAllocCount, 0);
1563 if (slow_rsp_threshold < 1)
1564 cifs_dbg(FYI, "slow_response_threshold msgs disabled\n");
1565 else if (slow_rsp_threshold > 32767)
1567 "slow response threshold set higher than recommended (0 to 32767)\n");
1568 #endif /* CONFIG_CIFS_STATS2 */
1570 atomic_set(&midCount, 0);
1571 GlobalCurrentXid = 0;
1572 GlobalTotalActiveXid = 0;
1573 GlobalMaxActiveXid = 0;
1574 spin_lock_init(&cifs_tcp_ses_lock);
1575 spin_lock_init(&GlobalMid_Lock);
1577 cifs_lock_secret = get_random_u32();
1579 if (cifs_max_pending < 2) {
1580 cifs_max_pending = 2;
1581 cifs_dbg(FYI, "cifs_max_pending set to min of 2\n");
1582 } else if (cifs_max_pending > CIFS_MAX_REQ) {
1583 cifs_max_pending = CIFS_MAX_REQ;
1584 cifs_dbg(FYI, "cifs_max_pending set to max of %u\n",
1588 cifsiod_wq = alloc_workqueue("cifsiod", WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1591 goto out_clean_proc;
1595 * Consider in future setting limit!=0 maybe to min(num_of_cores - 1, 3)
1596 * so that we don't launch too many worker threads but
1597 * Documentation/core-api/workqueue.rst recommends setting it to 0
1600 /* WQ_UNBOUND allows decrypt tasks to run on any CPU */
1601 decrypt_wq = alloc_workqueue("smb3decryptd",
1602 WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1605 goto out_destroy_cifsiod_wq;
1608 fileinfo_put_wq = alloc_workqueue("cifsfileinfoput",
1609 WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1610 if (!fileinfo_put_wq) {
1612 goto out_destroy_decrypt_wq;
1615 cifsoplockd_wq = alloc_workqueue("cifsoplockd",
1616 WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1617 if (!cifsoplockd_wq) {
1619 goto out_destroy_fileinfo_put_wq;
1622 deferredclose_wq = alloc_workqueue("deferredclose",
1623 WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1624 if (!deferredclose_wq) {
1626 goto out_destroy_cifsoplockd_wq;
1629 rc = cifs_fscache_register();
1631 goto out_destroy_deferredclose_wq;
1633 rc = cifs_init_inodecache();
1635 goto out_unreg_fscache;
1637 rc = cifs_init_mids();
1639 goto out_destroy_inodecache;
1641 rc = cifs_init_request_bufs();
1643 goto out_destroy_mids;
1645 #ifdef CONFIG_CIFS_DFS_UPCALL
1646 rc = dfs_cache_init();
1648 goto out_destroy_request_bufs;
1649 #endif /* CONFIG_CIFS_DFS_UPCALL */
1650 #ifdef CONFIG_CIFS_UPCALL
1651 rc = init_cifs_spnego();
1653 goto out_destroy_dfs_cache;
1654 #endif /* CONFIG_CIFS_UPCALL */
1655 #ifdef CONFIG_CIFS_SWN_UPCALL
1656 rc = cifs_genl_init();
1658 goto out_register_key_type;
1659 #endif /* CONFIG_CIFS_SWN_UPCALL */
1661 rc = init_cifs_idmap();
1663 goto out_cifs_swn_init;
1665 rc = register_filesystem(&cifs_fs_type);
1667 goto out_init_cifs_idmap;
1669 rc = register_filesystem(&smb3_fs_type);
1671 unregister_filesystem(&cifs_fs_type);
1672 goto out_init_cifs_idmap;
1677 out_init_cifs_idmap:
1680 #ifdef CONFIG_CIFS_SWN_UPCALL
1682 out_register_key_type:
1684 #ifdef CONFIG_CIFS_UPCALL
1686 out_destroy_dfs_cache:
1688 #ifdef CONFIG_CIFS_DFS_UPCALL
1689 dfs_cache_destroy();
1690 out_destroy_request_bufs:
1692 cifs_destroy_request_bufs();
1694 cifs_destroy_mids();
1695 out_destroy_inodecache:
1696 cifs_destroy_inodecache();
1698 cifs_fscache_unregister();
1699 out_destroy_deferredclose_wq:
1700 destroy_workqueue(deferredclose_wq);
1701 out_destroy_cifsoplockd_wq:
1702 destroy_workqueue(cifsoplockd_wq);
1703 out_destroy_fileinfo_put_wq:
1704 destroy_workqueue(fileinfo_put_wq);
1705 out_destroy_decrypt_wq:
1706 destroy_workqueue(decrypt_wq);
1707 out_destroy_cifsiod_wq:
1708 destroy_workqueue(cifsiod_wq);
1717 cifs_dbg(NOISY, "exit_smb3\n");
1718 unregister_filesystem(&cifs_fs_type);
1719 unregister_filesystem(&smb3_fs_type);
1720 cifs_dfs_release_automount_timer();
1722 #ifdef CONFIG_CIFS_SWN_UPCALL
1725 #ifdef CONFIG_CIFS_UPCALL
1728 #ifdef CONFIG_CIFS_DFS_UPCALL
1729 dfs_cache_destroy();
1731 cifs_destroy_request_bufs();
1732 cifs_destroy_mids();
1733 cifs_destroy_inodecache();
1734 cifs_fscache_unregister();
1735 destroy_workqueue(deferredclose_wq);
1736 destroy_workqueue(cifsoplockd_wq);
1737 destroy_workqueue(decrypt_wq);
1738 destroy_workqueue(fileinfo_put_wq);
1739 destroy_workqueue(cifsiod_wq);
1743 MODULE_AUTHOR("Steve French");
1744 MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
1746 ("VFS to access SMB3 servers e.g. Samba, Macs, Azure and Windows (and "
1747 "also older servers complying with the SNIA CIFS Specification)");
1748 MODULE_VERSION(CIFS_VERSION);
1749 MODULE_SOFTDEP("ecb");
1750 MODULE_SOFTDEP("hmac");
1751 MODULE_SOFTDEP("md5");
1752 MODULE_SOFTDEP("nls");
1753 MODULE_SOFTDEP("aes");
1754 MODULE_SOFTDEP("cmac");
1755 MODULE_SOFTDEP("sha256");
1756 MODULE_SOFTDEP("sha512");
1757 MODULE_SOFTDEP("aead2");
1758 MODULE_SOFTDEP("ccm");
1759 MODULE_SOFTDEP("gcm");
1760 module_init(init_cifs)
1761 module_exit(exit_cifs)