xref: /kernel/linux/linux-5.10/fs/cifs/cifsfs.c (revision 8c2ecf20)
1/*
2 *   fs/cifs/cifsfs.c
3 *
4 *   Copyright (C) International Business Machines  Corp., 2002,2008
5 *   Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 *   Common Internet FileSystem (CIFS) client
8 *
9 *   This library is free software; you can redistribute it and/or modify
10 *   it under the terms of the GNU Lesser General Public License as published
11 *   by the Free Software Foundation; either version 2.1 of the License, or
12 *   (at your option) any later version.
13 *
14 *   This library is distributed in the hope that it will be useful,
15 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
17 *   the GNU Lesser General Public License for more details.
18 *
19 *   You should have received a copy of the GNU Lesser General Public License
20 *   along with this library; if not, write to the Free Software
21 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24/* Note that BB means BUGBUG (ie something to fix eventually) */
25
26#include <linux/module.h>
27#include <linux/fs.h>
28#include <linux/mount.h>
29#include <linux/slab.h>
30#include <linux/init.h>
31#include <linux/list.h>
32#include <linux/seq_file.h>
33#include <linux/vfs.h>
34#include <linux/mempool.h>
35#include <linux/delay.h>
36#include <linux/kthread.h>
37#include <linux/freezer.h>
38#include <linux/namei.h>
39#include <linux/random.h>
40#include <linux/uuid.h>
41#include <linux/xattr.h>
42#include <net/ipv6.h>
43#include "cifsfs.h"
44#include "cifspdu.h"
45#define DECLARE_GLOBALS_HERE
46#include "cifsglob.h"
47#include "cifsproto.h"
48#include "cifs_debug.h"
49#include "cifs_fs_sb.h"
50#include <linux/mm.h>
51#include <linux/key-type.h>
52#include "cifs_spnego.h"
53#include "fscache.h"
54#include "smb2pdu.h"
55#ifdef CONFIG_CIFS_DFS_UPCALL
56#include "dfs_cache.h"
57#endif
58
59/*
60 * DOS dates from 1980/1/1 through 2107/12/31
61 * Protocol specifications indicate the range should be to 119, which
62 * limits maximum year to 2099. But this range has not been checked.
63 */
64#define SMB_DATE_MAX (127<<9 | 12<<5 | 31)
65#define SMB_DATE_MIN (0<<9 | 1<<5 | 1)
66#define SMB_TIME_MAX (23<<11 | 59<<5 | 29)
67
68int cifsFYI = 0;
69bool traceSMB;
70bool enable_oplocks = true;
71bool linuxExtEnabled = true;
72bool lookupCacheEnabled = true;
73bool disable_legacy_dialects; /* false by default */
74bool enable_gcm_256;  /* false by default, change when more servers support it */
75bool require_gcm_256; /* false by default */
76unsigned int global_secflags = CIFSSEC_DEF;
77/* unsigned int ntlmv2_support = 0; */
78unsigned int sign_CIFS_PDUs = 1;
79static const struct super_operations cifs_super_ops;
80unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
81module_param(CIFSMaxBufSize, uint, 0444);
82MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header) "
83				 "for CIFS requests. "
84				 "Default: 16384 Range: 8192 to 130048");
85unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
86module_param(cifs_min_rcv, uint, 0444);
87MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
88				"1 to 64");
89unsigned int cifs_min_small = 30;
90module_param(cifs_min_small, uint, 0444);
91MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
92				 "Range: 2 to 256");
93unsigned int cifs_max_pending = CIFS_MAX_REQ;
94module_param(cifs_max_pending, uint, 0444);
95MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server for "
96				   "CIFS/SMB1 dialect (N/A for SMB3) "
97				   "Default: 32767 Range: 2 to 32767.");
98#ifdef CONFIG_CIFS_STATS2
99unsigned int slow_rsp_threshold = 1;
100module_param(slow_rsp_threshold, uint, 0644);
101MODULE_PARM_DESC(slow_rsp_threshold, "Amount of time (in seconds) to wait "
102				   "before logging that a response is delayed. "
103				   "Default: 1 (if set to 0 disables msg).");
104#endif /* STATS2 */
105
106module_param(enable_oplocks, bool, 0644);
107MODULE_PARM_DESC(enable_oplocks, "Enable or disable oplocks. Default: y/Y/1");
108
109module_param(enable_gcm_256, bool, 0644);
110MODULE_PARM_DESC(enable_gcm_256, "Enable requesting strongest (256 bit) GCM encryption. Default: n/N/0");
111
112module_param(require_gcm_256, bool, 0644);
113MODULE_PARM_DESC(require_gcm_256, "Require strongest (256 bit) GCM encryption. Default: n/N/0");
114
115module_param(disable_legacy_dialects, bool, 0644);
116MODULE_PARM_DESC(disable_legacy_dialects, "To improve security it may be "
117				  "helpful to restrict the ability to "
118				  "override the default dialects (SMB2.1, "
119				  "SMB3 and SMB3.02) on mount with old "
120				  "dialects (CIFS/SMB1 and SMB2) since "
121				  "vers=1.0 (CIFS/SMB1) and vers=2.0 are weaker"
122				  " and less secure. Default: n/N/0");
123
124extern mempool_t *cifs_sm_req_poolp;
125extern mempool_t *cifs_req_poolp;
126extern mempool_t *cifs_mid_poolp;
127
128struct workqueue_struct	*cifsiod_wq;
129struct workqueue_struct	*decrypt_wq;
130struct workqueue_struct	*fileinfo_put_wq;
131struct workqueue_struct	*cifsoplockd_wq;
132__u32 cifs_lock_secret;
133
134/*
135 * Bumps refcount for cifs super block.
136 * Note that it should be only called if a referece to VFS super block is
137 * already held, e.g. in open-type syscalls context. Otherwise it can race with
138 * atomic_dec_and_test in deactivate_locked_super.
139 */
140void
141cifs_sb_active(struct super_block *sb)
142{
143	struct cifs_sb_info *server = CIFS_SB(sb);
144
145	if (atomic_inc_return(&server->active) == 1)
146		atomic_inc(&sb->s_active);
147}
148
149void
150cifs_sb_deactive(struct super_block *sb)
151{
152	struct cifs_sb_info *server = CIFS_SB(sb);
153
154	if (atomic_dec_and_test(&server->active))
155		deactivate_super(sb);
156}
157
158static int
159cifs_read_super(struct super_block *sb)
160{
161	struct inode *inode;
162	struct cifs_sb_info *cifs_sb;
163	struct cifs_tcon *tcon;
164	struct timespec64 ts;
165	int rc = 0;
166
167	cifs_sb = CIFS_SB(sb);
168	tcon = cifs_sb_master_tcon(cifs_sb);
169
170	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIXACL)
171		sb->s_flags |= SB_POSIXACL;
172
173	if (tcon->snapshot_time)
174		sb->s_flags |= SB_RDONLY;
175
176	if (tcon->ses->capabilities & tcon->ses->server->vals->cap_large_files)
177		sb->s_maxbytes = MAX_LFS_FILESIZE;
178	else
179		sb->s_maxbytes = MAX_NON_LFS;
180
181	/*
182	 * Some very old servers like DOS and OS/2 used 2 second granularity
183	 * (while all current servers use 100ns granularity - see MS-DTYP)
184	 * but 1 second is the maximum allowed granularity for the VFS
185	 * so for old servers set time granularity to 1 second while for
186	 * everything else (current servers) set it to 100ns.
187	 */
188	if ((tcon->ses->server->vals->protocol_id == SMB10_PROT_ID) &&
189	    ((tcon->ses->capabilities &
190	      tcon->ses->server->vals->cap_nt_find) == 0) &&
191	    !tcon->unix_ext) {
192		sb->s_time_gran = 1000000000; /* 1 second is max allowed gran */
193		ts = cnvrtDosUnixTm(cpu_to_le16(SMB_DATE_MIN), 0, 0);
194		sb->s_time_min = ts.tv_sec;
195		ts = cnvrtDosUnixTm(cpu_to_le16(SMB_DATE_MAX),
196				    cpu_to_le16(SMB_TIME_MAX), 0);
197		sb->s_time_max = ts.tv_sec;
198	} else {
199		/*
200		 * Almost every server, including all SMB2+, uses DCE TIME
201		 * ie 100 nanosecond units, since 1601.  See MS-DTYP and MS-FSCC
202		 */
203		sb->s_time_gran = 100;
204		ts = cifs_NTtimeToUnix(0);
205		sb->s_time_min = ts.tv_sec;
206		ts = cifs_NTtimeToUnix(cpu_to_le64(S64_MAX));
207		sb->s_time_max = ts.tv_sec;
208	}
209
210	sb->s_magic = CIFS_MAGIC_NUMBER;
211	sb->s_op = &cifs_super_ops;
212	sb->s_xattr = cifs_xattr_handlers;
213	rc = super_setup_bdi(sb);
214	if (rc)
215		goto out_no_root;
216	/* tune readahead according to rsize */
217	sb->s_bdi->ra_pages = cifs_sb->rsize / PAGE_SIZE;
218
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);
222
223	if (IS_ERR(inode)) {
224		rc = PTR_ERR(inode);
225		goto out_no_root;
226	}
227
228	if (tcon->nocase)
229		sb->s_d_op = &cifs_ci_dentry_ops;
230	else
231		sb->s_d_op = &cifs_dentry_ops;
232
233	sb->s_root = d_make_root(inode);
234	if (!sb->s_root) {
235		rc = -ENOMEM;
236		goto out_no_root;
237	}
238
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;
243	}
244#endif /* CONFIG_CIFS_NFSD_EXPORT */
245
246	return 0;
247
248out_no_root:
249	cifs_dbg(VFS, "%s: get root inode failed\n", __func__);
250	return rc;
251}
252
253static void cifs_kill_sb(struct super_block *sb)
254{
255	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
256	kill_anon_super(sb);
257	cifs_umount(cifs_sb);
258}
259
260static int
261cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
262{
263	struct super_block *sb = dentry->d_sb;
264	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
265	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
266	struct TCP_Server_Info *server = tcon->ses->server;
267	unsigned int xid;
268	int rc = 0;
269
270	xid = get_xid();
271
272	if (le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength) > 0)
273		buf->f_namelen =
274		       le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength);
275	else
276		buf->f_namelen = PATH_MAX;
277
278	buf->f_fsid.val[0] = tcon->vol_serial_number;
279	/* are using part of create time for more randomness, see man statfs */
280	buf->f_fsid.val[1] =  (int)le64_to_cpu(tcon->vol_create_time);
281
282	buf->f_files = 0;	/* undefined */
283	buf->f_ffree = 0;	/* unlimited */
284
285	if (server->ops->queryfs)
286		rc = server->ops->queryfs(xid, tcon, cifs_sb, buf);
287
288	free_xid(xid);
289	return rc;
290}
291
292static long cifs_fallocate(struct file *file, int mode, loff_t off, loff_t len)
293{
294	struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
295	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
296	struct TCP_Server_Info *server = tcon->ses->server;
297
298	if (server->ops->fallocate)
299		return server->ops->fallocate(file, tcon, mode, off, len);
300
301	return -EOPNOTSUPP;
302}
303
304static int cifs_permission(struct inode *inode, int mask)
305{
306	struct cifs_sb_info *cifs_sb;
307
308	cifs_sb = CIFS_SB(inode->i_sb);
309
310	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
311		if ((mask & MAY_EXEC) && !execute_ok(inode))
312			return -EACCES;
313		else
314			return 0;
315	} else /* file mode might have been restricted at mount time
316		on the client (above and beyond ACL on servers) for
317		servers which do not support setting and viewing mode bits,
318		so allowing client to check permissions is useful */
319		return generic_permission(inode, mask);
320}
321
322static struct kmem_cache *cifs_inode_cachep;
323static struct kmem_cache *cifs_req_cachep;
324static struct kmem_cache *cifs_mid_cachep;
325static struct kmem_cache *cifs_sm_req_cachep;
326mempool_t *cifs_sm_req_poolp;
327mempool_t *cifs_req_poolp;
328mempool_t *cifs_mid_poolp;
329
330static struct inode *
331cifs_alloc_inode(struct super_block *sb)
332{
333	struct cifsInodeInfo *cifs_inode;
334	cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
335	if (!cifs_inode)
336		return NULL;
337	cifs_inode->cifsAttrs = 0x20;	/* default */
338	cifs_inode->time = 0;
339	/*
340	 * Until the file is open and we have gotten oplock info back from the
341	 * server, can not assume caching of file data or metadata.
342	 */
343	cifs_set_oplock_level(cifs_inode, 0);
344	cifs_inode->flags = 0;
345	spin_lock_init(&cifs_inode->writers_lock);
346	cifs_inode->writers = 0;
347	cifs_inode->vfs_inode.i_blkbits = 14;  /* 2**14 = CIFS_MAX_MSGSIZE */
348	cifs_inode->server_eof = 0;
349	cifs_inode->uniqueid = 0;
350	cifs_inode->createtime = 0;
351	cifs_inode->epoch = 0;
352	spin_lock_init(&cifs_inode->open_file_lock);
353	generate_random_uuid(cifs_inode->lease_key);
354
355	/*
356	 * Can not set i_flags here - they get immediately overwritten to zero
357	 * by the VFS.
358	 */
359	/* cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME; */
360	INIT_LIST_HEAD(&cifs_inode->openFileList);
361	INIT_LIST_HEAD(&cifs_inode->llist);
362	return &cifs_inode->vfs_inode;
363}
364
365static void
366cifs_free_inode(struct inode *inode)
367{
368	kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
369}
370
371static void
372cifs_evict_inode(struct inode *inode)
373{
374	truncate_inode_pages_final(&inode->i_data);
375	clear_inode(inode);
376	cifs_fscache_release_inode_cookie(inode);
377}
378
379static void
380cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
381{
382	struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
383	struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
384
385	seq_puts(s, ",addr=");
386
387	switch (server->dstaddr.ss_family) {
388	case AF_INET:
389		seq_printf(s, "%pI4", &sa->sin_addr.s_addr);
390		break;
391	case AF_INET6:
392		seq_printf(s, "%pI6", &sa6->sin6_addr.s6_addr);
393		if (sa6->sin6_scope_id)
394			seq_printf(s, "%%%u", sa6->sin6_scope_id);
395		break;
396	default:
397		seq_puts(s, "(unknown)");
398	}
399	if (server->rdma)
400		seq_puts(s, ",rdma");
401}
402
403static void
404cifs_show_security(struct seq_file *s, struct cifs_ses *ses)
405{
406	if (ses->sectype == Unspecified) {
407		if (ses->user_name == NULL)
408			seq_puts(s, ",sec=none");
409		return;
410	}
411
412	seq_puts(s, ",sec=");
413
414	switch (ses->sectype) {
415	case LANMAN:
416		seq_puts(s, "lanman");
417		break;
418	case NTLMv2:
419		seq_puts(s, "ntlmv2");
420		break;
421	case NTLM:
422		seq_puts(s, "ntlm");
423		break;
424	case Kerberos:
425		seq_puts(s, "krb5");
426		break;
427	case RawNTLMSSP:
428		seq_puts(s, "ntlmssp");
429		break;
430	default:
431		/* shouldn't ever happen */
432		seq_puts(s, "unknown");
433		break;
434	}
435
436	if (ses->sign)
437		seq_puts(s, "i");
438
439	if (ses->sectype == Kerberos)
440		seq_printf(s, ",cruid=%u",
441			   from_kuid_munged(&init_user_ns, ses->cred_uid));
442}
443
444static void
445cifs_show_cache_flavor(struct seq_file *s, struct cifs_sb_info *cifs_sb)
446{
447	seq_puts(s, ",cache=");
448
449	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
450		seq_puts(s, "strict");
451	else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
452		seq_puts(s, "none");
453	else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
454		seq_puts(s, "singleclient"); /* assume only one client access */
455	else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE)
456		seq_puts(s, "ro"); /* read only caching assumed */
457	else
458		seq_puts(s, "loose");
459}
460
461static void
462cifs_show_nls(struct seq_file *s, struct nls_table *cur)
463{
464	struct nls_table *def;
465
466	/* Display iocharset= option if it's not default charset */
467	def = load_nls_default();
468	if (def != cur)
469		seq_printf(s, ",iocharset=%s", cur->charset);
470	unload_nls(def);
471}
472
473/*
474 * cifs_show_options() is for displaying mount options in /proc/mounts.
475 * Not all settable options are displayed but most of the important
476 * ones are.
477 */
478static int
479cifs_show_options(struct seq_file *s, struct dentry *root)
480{
481	struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
482	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
483	struct sockaddr *srcaddr;
484	srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
485
486	seq_show_option(s, "vers", tcon->ses->server->vals->version_string);
487	cifs_show_security(s, tcon->ses);
488	cifs_show_cache_flavor(s, cifs_sb);
489
490	if (tcon->no_lease)
491		seq_puts(s, ",nolease");
492	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)
493		seq_puts(s, ",multiuser");
494	else if (tcon->ses->user_name)
495		seq_show_option(s, "username", tcon->ses->user_name);
496
497	if (tcon->ses->domainName && tcon->ses->domainName[0] != 0)
498		seq_show_option(s, "domain", tcon->ses->domainName);
499
500	if (srcaddr->sa_family != AF_UNSPEC) {
501		struct sockaddr_in *saddr4;
502		struct sockaddr_in6 *saddr6;
503		saddr4 = (struct sockaddr_in *)srcaddr;
504		saddr6 = (struct sockaddr_in6 *)srcaddr;
505		if (srcaddr->sa_family == AF_INET6)
506			seq_printf(s, ",srcaddr=%pI6c",
507				   &saddr6->sin6_addr);
508		else if (srcaddr->sa_family == AF_INET)
509			seq_printf(s, ",srcaddr=%pI4",
510				   &saddr4->sin_addr.s_addr);
511		else
512			seq_printf(s, ",srcaddr=BAD-AF:%i",
513				   (int)(srcaddr->sa_family));
514	}
515
516	seq_printf(s, ",uid=%u",
517		   from_kuid_munged(&init_user_ns, cifs_sb->mnt_uid));
518	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
519		seq_puts(s, ",forceuid");
520	else
521		seq_puts(s, ",noforceuid");
522
523	seq_printf(s, ",gid=%u",
524		   from_kgid_munged(&init_user_ns, cifs_sb->mnt_gid));
525	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
526		seq_puts(s, ",forcegid");
527	else
528		seq_puts(s, ",noforcegid");
529
530	cifs_show_address(s, tcon->ses->server);
531
532	if (!tcon->unix_ext)
533		seq_printf(s, ",file_mode=0%ho,dir_mode=0%ho",
534					   cifs_sb->mnt_file_mode,
535					   cifs_sb->mnt_dir_mode);
536
537	cifs_show_nls(s, cifs_sb->local_nls);
538
539	if (tcon->seal)
540		seq_puts(s, ",seal");
541	else if (tcon->ses->server->ignore_signature)
542		seq_puts(s, ",signloosely");
543	if (tcon->nocase)
544		seq_puts(s, ",nocase");
545	if (tcon->nodelete)
546		seq_puts(s, ",nodelete");
547	if (tcon->local_lease)
548		seq_puts(s, ",locallease");
549	if (tcon->retry)
550		seq_puts(s, ",hard");
551	else
552		seq_puts(s, ",soft");
553	if (tcon->use_persistent)
554		seq_puts(s, ",persistenthandles");
555	else if (tcon->use_resilient)
556		seq_puts(s, ",resilienthandles");
557	if (tcon->posix_extensions)
558		seq_puts(s, ",posix");
559	else if (tcon->unix_ext)
560		seq_puts(s, ",unix");
561	else
562		seq_puts(s, ",nounix");
563	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
564		seq_puts(s, ",nodfs");
565	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
566		seq_puts(s, ",posixpaths");
567	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
568		seq_puts(s, ",setuids");
569	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
570		seq_puts(s, ",idsfromsid");
571	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
572		seq_puts(s, ",serverino");
573	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
574		seq_puts(s, ",rwpidforward");
575	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL)
576		seq_puts(s, ",forcemand");
577	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
578		seq_puts(s, ",nouser_xattr");
579	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
580		seq_puts(s, ",mapchars");
581	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SFM_CHR)
582		seq_puts(s, ",mapposix");
583	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
584		seq_puts(s, ",sfu");
585	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
586		seq_puts(s, ",nobrl");
587	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_HANDLE_CACHE)
588		seq_puts(s, ",nohandlecache");
589	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)
590		seq_puts(s, ",modefromsid");
591	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
592		seq_puts(s, ",cifsacl");
593	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
594		seq_puts(s, ",dynperm");
595	if (root->d_sb->s_flags & SB_POSIXACL)
596		seq_puts(s, ",acl");
597	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
598		seq_puts(s, ",mfsymlinks");
599	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
600		seq_puts(s, ",fsc");
601	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)
602		seq_puts(s, ",nostrictsync");
603	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
604		seq_puts(s, ",noperm");
605	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID)
606		seq_printf(s, ",backupuid=%u",
607			   from_kuid_munged(&init_user_ns,
608					    cifs_sb->mnt_backupuid));
609	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID)
610		seq_printf(s, ",backupgid=%u",
611			   from_kgid_munged(&init_user_ns,
612					    cifs_sb->mnt_backupgid));
613
614	seq_printf(s, ",rsize=%u", cifs_sb->rsize);
615	seq_printf(s, ",wsize=%u", cifs_sb->wsize);
616	seq_printf(s, ",bsize=%u", cifs_sb->bsize);
617	if (tcon->ses->server->min_offload)
618		seq_printf(s, ",esize=%u", tcon->ses->server->min_offload);
619	seq_printf(s, ",echo_interval=%lu",
620			tcon->ses->server->echo_interval / HZ);
621
622	/* Only display the following if overridden on mount */
623	if (tcon->ses->server->max_credits != SMB2_MAX_CREDITS_AVAILABLE)
624		seq_printf(s, ",max_credits=%u", tcon->ses->server->max_credits);
625	if (tcon->ses->server->tcp_nodelay)
626		seq_puts(s, ",tcpnodelay");
627	if (tcon->ses->server->noautotune)
628		seq_puts(s, ",noautotune");
629	if (tcon->ses->server->noblocksnd)
630		seq_puts(s, ",noblocksend");
631
632	if (tcon->snapshot_time)
633		seq_printf(s, ",snapshot=%llu", tcon->snapshot_time);
634	if (tcon->handle_timeout)
635		seq_printf(s, ",handletimeout=%u", tcon->handle_timeout);
636	/* convert actimeo and display it in seconds */
637	seq_printf(s, ",actimeo=%lu", cifs_sb->actimeo / HZ);
638
639	if (tcon->ses->chan_max > 1)
640		seq_printf(s, ",multichannel,max_channels=%zu",
641			   tcon->ses->chan_max);
642
643	return 0;
644}
645
646static void cifs_umount_begin(struct super_block *sb)
647{
648	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
649	struct cifs_tcon *tcon;
650
651	if (cifs_sb == NULL)
652		return;
653
654	tcon = cifs_sb_master_tcon(cifs_sb);
655
656	spin_lock(&cifs_tcp_ses_lock);
657	if ((tcon->tc_count > 1) || (tcon->tidStatus == CifsExiting)) {
658		/* we have other mounts to same share or we have
659		   already tried to force umount this and woken up
660		   all waiting network requests, nothing to do */
661		spin_unlock(&cifs_tcp_ses_lock);
662		return;
663	} else if (tcon->tc_count == 1)
664		tcon->tidStatus = CifsExiting;
665	spin_unlock(&cifs_tcp_ses_lock);
666
667	/* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
668	/* cancel_notify_requests(tcon); */
669	if (tcon->ses && tcon->ses->server) {
670		cifs_dbg(FYI, "wake up tasks now - umount begin not complete\n");
671		wake_up_all(&tcon->ses->server->request_q);
672		wake_up_all(&tcon->ses->server->response_q);
673		msleep(1); /* yield */
674		/* we have to kick the requests once more */
675		wake_up_all(&tcon->ses->server->response_q);
676		msleep(1);
677	}
678
679	return;
680}
681
682#ifdef CONFIG_CIFS_STATS2
683static int cifs_show_stats(struct seq_file *s, struct dentry *root)
684{
685	/* BB FIXME */
686	return 0;
687}
688#endif
689
690static int cifs_remount(struct super_block *sb, int *flags, char *data)
691{
692	sync_filesystem(sb);
693	*flags |= SB_NODIRATIME;
694	return 0;
695}
696
697static int cifs_drop_inode(struct inode *inode)
698{
699	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
700
701	/* no serverino => unconditional eviction */
702	return !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) ||
703		generic_drop_inode(inode);
704}
705
706static const struct super_operations cifs_super_ops = {
707	.statfs = cifs_statfs,
708	.alloc_inode = cifs_alloc_inode,
709	.free_inode = cifs_free_inode,
710	.drop_inode	= cifs_drop_inode,
711	.evict_inode	= cifs_evict_inode,
712/*	.delete_inode	= cifs_delete_inode,  */  /* Do not need above
713	function unless later we add lazy close of inodes or unless the
714	kernel forgets to call us with the same number of releases (closes)
715	as opens */
716	.show_options = cifs_show_options,
717	.umount_begin   = cifs_umount_begin,
718	.remount_fs = cifs_remount,
719#ifdef CONFIG_CIFS_STATS2
720	.show_stats = cifs_show_stats,
721#endif
722};
723
724/*
725 * Get root dentry from superblock according to prefix path mount option.
726 * Return dentry with refcount + 1 on success and NULL otherwise.
727 */
728static struct dentry *
729cifs_get_root(struct smb_vol *vol, struct super_block *sb)
730{
731	struct dentry *dentry;
732	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
733	char *full_path = NULL;
734	char *s, *p;
735	char sep;
736
737	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
738		return dget(sb->s_root);
739
740	full_path = cifs_build_path_to_root(vol, cifs_sb,
741				cifs_sb_master_tcon(cifs_sb), 0);
742	if (full_path == NULL)
743		return ERR_PTR(-ENOMEM);
744
745	cifs_dbg(FYI, "Get root dentry for %s\n", full_path);
746
747	sep = CIFS_DIR_SEP(cifs_sb);
748	dentry = dget(sb->s_root);
749	p = s = full_path;
750
751	do {
752		struct inode *dir = d_inode(dentry);
753		struct dentry *child;
754
755		if (!S_ISDIR(dir->i_mode)) {
756			dput(dentry);
757			dentry = ERR_PTR(-ENOTDIR);
758			break;
759		}
760
761		/* skip separators */
762		while (*s == sep)
763			s++;
764		if (!*s)
765			break;
766		p = s++;
767		/* next separator */
768		while (*s && *s != sep)
769			s++;
770
771		child = lookup_positive_unlocked(p, dentry, s - p);
772		dput(dentry);
773		dentry = child;
774	} while (!IS_ERR(dentry));
775	kfree(full_path);
776	return dentry;
777}
778
779static int cifs_set_super(struct super_block *sb, void *data)
780{
781	struct cifs_mnt_data *mnt_data = data;
782	sb->s_fs_info = mnt_data->cifs_sb;
783	return set_anon_super(sb, NULL);
784}
785
786static struct dentry *
787cifs_smb3_do_mount(struct file_system_type *fs_type,
788	      int flags, const char *dev_name, void *data, bool is_smb3)
789{
790	int rc;
791	struct super_block *sb;
792	struct cifs_sb_info *cifs_sb;
793	struct smb_vol *volume_info;
794	struct cifs_mnt_data mnt_data;
795	struct dentry *root;
796
797	/*
798	 * Prints in Kernel / CIFS log the attempted mount operation
799	 *	If CIFS_DEBUG && cifs_FYI
800	 */
801	if (cifsFYI)
802		cifs_dbg(FYI, "Devname: %s flags: %d\n", dev_name, flags);
803	else
804		cifs_info("Attempting to mount %s\n", dev_name);
805
806	volume_info = cifs_get_volume_info((char *)data, dev_name, is_smb3);
807	if (IS_ERR(volume_info))
808		return ERR_CAST(volume_info);
809
810	cifs_sb = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
811	if (cifs_sb == NULL) {
812		root = ERR_PTR(-ENOMEM);
813		goto out_nls;
814	}
815
816	cifs_sb->mountdata = kstrndup(data, PAGE_SIZE, GFP_KERNEL);
817	if (cifs_sb->mountdata == NULL) {
818		root = ERR_PTR(-ENOMEM);
819		goto out_free;
820	}
821
822	rc = cifs_setup_cifs_sb(volume_info, cifs_sb);
823	if (rc) {
824		root = ERR_PTR(rc);
825		goto out_free;
826	}
827
828	rc = cifs_mount(cifs_sb, volume_info);
829	if (rc) {
830		if (!(flags & SB_SILENT))
831			cifs_dbg(VFS, "cifs_mount failed w/return code = %d\n",
832				 rc);
833		root = ERR_PTR(rc);
834		goto out_free;
835	}
836
837	mnt_data.vol = volume_info;
838	mnt_data.cifs_sb = cifs_sb;
839	mnt_data.flags = flags;
840
841	/* BB should we make this contingent on mount parm? */
842	flags |= SB_NODIRATIME | SB_NOATIME;
843
844	sb = sget(fs_type, cifs_match_super, cifs_set_super, flags, &mnt_data);
845	if (IS_ERR(sb)) {
846		root = ERR_CAST(sb);
847		cifs_umount(cifs_sb);
848		goto out;
849	}
850
851	if (sb->s_root) {
852		cifs_dbg(FYI, "Use existing superblock\n");
853		cifs_umount(cifs_sb);
854	} else {
855		rc = cifs_read_super(sb);
856		if (rc) {
857			root = ERR_PTR(rc);
858			goto out_super;
859		}
860
861		sb->s_flags |= SB_ACTIVE;
862	}
863
864	root = cifs_get_root(volume_info, sb);
865	if (IS_ERR(root))
866		goto out_super;
867
868	cifs_dbg(FYI, "dentry root is: %p\n", root);
869	goto out;
870
871out_super:
872	deactivate_locked_super(sb);
873	return root;
874out:
875	cifs_cleanup_volume_info(volume_info);
876	return root;
877
878out_free:
879	kfree(cifs_sb->prepath);
880	kfree(cifs_sb->mountdata);
881	kfree(cifs_sb);
882out_nls:
883	unload_nls(volume_info->local_nls);
884	goto out;
885}
886
887static struct dentry *
888smb3_do_mount(struct file_system_type *fs_type,
889	      int flags, const char *dev_name, void *data)
890{
891	return cifs_smb3_do_mount(fs_type, flags, dev_name, data, true);
892}
893
894static struct dentry *
895cifs_do_mount(struct file_system_type *fs_type,
896	      int flags, const char *dev_name, void *data)
897{
898	return cifs_smb3_do_mount(fs_type, flags, dev_name, data, false);
899}
900
901static ssize_t
902cifs_loose_read_iter(struct kiocb *iocb, struct iov_iter *iter)
903{
904	ssize_t rc;
905	struct inode *inode = file_inode(iocb->ki_filp);
906
907	if (iocb->ki_flags & IOCB_DIRECT)
908		return cifs_user_readv(iocb, iter);
909
910	rc = cifs_revalidate_mapping(inode);
911	if (rc)
912		return rc;
913
914	return generic_file_read_iter(iocb, iter);
915}
916
917static ssize_t cifs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
918{
919	struct inode *inode = file_inode(iocb->ki_filp);
920	struct cifsInodeInfo *cinode = CIFS_I(inode);
921	ssize_t written;
922	int rc;
923
924	if (iocb->ki_filp->f_flags & O_DIRECT) {
925		written = cifs_user_writev(iocb, from);
926		if (written > 0 && CIFS_CACHE_READ(cinode)) {
927			cifs_zap_mapping(inode);
928			cifs_dbg(FYI,
929				 "Set no oplock for inode=%p after a write operation\n",
930				 inode);
931			cinode->oplock = 0;
932		}
933		return written;
934	}
935
936	written = cifs_get_writer(cinode);
937	if (written)
938		return written;
939
940	written = generic_file_write_iter(iocb, from);
941
942	if (CIFS_CACHE_WRITE(CIFS_I(inode)))
943		goto out;
944
945	rc = filemap_fdatawrite(inode->i_mapping);
946	if (rc)
947		cifs_dbg(FYI, "cifs_file_write_iter: %d rc on %p inode\n",
948			 rc, inode);
949
950out:
951	cifs_put_writer(cinode);
952	return written;
953}
954
955static loff_t cifs_llseek(struct file *file, loff_t offset, int whence)
956{
957	struct cifsFileInfo *cfile = file->private_data;
958	struct cifs_tcon *tcon;
959
960	/*
961	 * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
962	 * the cached file length
963	 */
964	if (whence != SEEK_SET && whence != SEEK_CUR) {
965		int rc;
966		struct inode *inode = file_inode(file);
967
968		/*
969		 * We need to be sure that all dirty pages are written and the
970		 * server has the newest file length.
971		 */
972		if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
973		    inode->i_mapping->nrpages != 0) {
974			rc = filemap_fdatawait(inode->i_mapping);
975			if (rc) {
976				mapping_set_error(inode->i_mapping, rc);
977				return rc;
978			}
979		}
980		/*
981		 * Some applications poll for the file length in this strange
982		 * way so we must seek to end on non-oplocked files by
983		 * setting the revalidate time to zero.
984		 */
985		CIFS_I(inode)->time = 0;
986
987		rc = cifs_revalidate_file_attr(file);
988		if (rc < 0)
989			return (loff_t)rc;
990	}
991	if (cfile && cfile->tlink) {
992		tcon = tlink_tcon(cfile->tlink);
993		if (tcon->ses->server->ops->llseek)
994			return tcon->ses->server->ops->llseek(file, tcon,
995							      offset, whence);
996	}
997	return generic_file_llseek(file, offset, whence);
998}
999
1000static int
1001cifs_setlease(struct file *file, long arg, struct file_lock **lease, void **priv)
1002{
1003	/*
1004	 * Note that this is called by vfs setlease with i_lock held to
1005	 * protect *lease from going away.
1006	 */
1007	struct inode *inode = file_inode(file);
1008	struct cifsFileInfo *cfile = file->private_data;
1009
1010	if (!(S_ISREG(inode->i_mode)))
1011		return -EINVAL;
1012
1013	/* Check if file is oplocked if this is request for new lease */
1014	if (arg == F_UNLCK ||
1015	    ((arg == F_RDLCK) && CIFS_CACHE_READ(CIFS_I(inode))) ||
1016	    ((arg == F_WRLCK) && CIFS_CACHE_WRITE(CIFS_I(inode))))
1017		return generic_setlease(file, arg, lease, priv);
1018	else if (tlink_tcon(cfile->tlink)->local_lease &&
1019		 !CIFS_CACHE_READ(CIFS_I(inode)))
1020		/*
1021		 * If the server claims to support oplock on this file, then we
1022		 * still need to check oplock even if the local_lease mount
1023		 * option is set, but there are servers which do not support
1024		 * oplock for which this mount option may be useful if the user
1025		 * knows that the file won't be changed on the server by anyone
1026		 * else.
1027		 */
1028		return generic_setlease(file, arg, lease, priv);
1029	else
1030		return -EAGAIN;
1031}
1032
1033struct file_system_type cifs_fs_type = {
1034	.owner = THIS_MODULE,
1035	.name = "cifs",
1036	.mount = cifs_do_mount,
1037	.kill_sb = cifs_kill_sb,
1038	.fs_flags = FS_RENAME_DOES_D_MOVE,
1039};
1040MODULE_ALIAS_FS("cifs");
1041
1042struct file_system_type smb3_fs_type = {
1043	.owner = THIS_MODULE,
1044	.name = "smb3",
1045	.mount = smb3_do_mount,
1046	.kill_sb = cifs_kill_sb,
1047	.fs_flags = FS_RENAME_DOES_D_MOVE,
1048};
1049MODULE_ALIAS_FS("smb3");
1050MODULE_ALIAS("smb3");
1051
1052const struct inode_operations cifs_dir_inode_ops = {
1053	.create = cifs_create,
1054	.atomic_open = cifs_atomic_open,
1055	.lookup = cifs_lookup,
1056	.getattr = cifs_getattr,
1057	.unlink = cifs_unlink,
1058	.link = cifs_hardlink,
1059	.mkdir = cifs_mkdir,
1060	.rmdir = cifs_rmdir,
1061	.rename = cifs_rename2,
1062	.permission = cifs_permission,
1063	.setattr = cifs_setattr,
1064	.symlink = cifs_symlink,
1065	.mknod   = cifs_mknod,
1066	.listxattr = cifs_listxattr,
1067};
1068
1069const struct inode_operations cifs_file_inode_ops = {
1070	.setattr = cifs_setattr,
1071	.getattr = cifs_getattr,
1072	.permission = cifs_permission,
1073	.listxattr = cifs_listxattr,
1074	.fiemap = cifs_fiemap,
1075};
1076
1077const struct inode_operations cifs_symlink_inode_ops = {
1078	.get_link = cifs_get_link,
1079	.setattr = cifs_setattr,
1080	.permission = cifs_permission,
1081	.listxattr = cifs_listxattr,
1082};
1083
1084static loff_t cifs_remap_file_range(struct file *src_file, loff_t off,
1085		struct file *dst_file, loff_t destoff, loff_t len,
1086		unsigned int remap_flags)
1087{
1088	struct inode *src_inode = file_inode(src_file);
1089	struct inode *target_inode = file_inode(dst_file);
1090	struct cifsFileInfo *smb_file_src = src_file->private_data;
1091	struct cifsFileInfo *smb_file_target;
1092	struct cifs_tcon *target_tcon;
1093	unsigned int xid;
1094	int rc;
1095
1096	if (remap_flags & REMAP_FILE_DEDUP)
1097		return -EOPNOTSUPP;
1098	if (remap_flags & ~REMAP_FILE_ADVISORY)
1099		return -EINVAL;
1100
1101	cifs_dbg(FYI, "clone range\n");
1102
1103	xid = get_xid();
1104
1105	if (!src_file->private_data || !dst_file->private_data) {
1106		rc = -EBADF;
1107		cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
1108		goto out;
1109	}
1110
1111	smb_file_target = dst_file->private_data;
1112	target_tcon = tlink_tcon(smb_file_target->tlink);
1113
1114	/*
1115	 * Note: cifs case is easier than btrfs since server responsible for
1116	 * checks for proper open modes and file type and if it wants
1117	 * server could even support copy of range where source = target
1118	 */
1119	lock_two_nondirectories(target_inode, src_inode);
1120
1121	if (len == 0)
1122		len = src_inode->i_size - off;
1123
1124	cifs_dbg(FYI, "about to flush pages\n");
1125	/* should we flush first and last page first */
1126	truncate_inode_pages_range(&target_inode->i_data, destoff,
1127				   PAGE_ALIGN(destoff + len)-1);
1128
1129	if (target_tcon->ses->server->ops->duplicate_extents)
1130		rc = target_tcon->ses->server->ops->duplicate_extents(xid,
1131			smb_file_src, smb_file_target, off, len, destoff);
1132	else
1133		rc = -EOPNOTSUPP;
1134
1135	/* force revalidate of size and timestamps of target file now
1136	   that target is updated on the server */
1137	CIFS_I(target_inode)->time = 0;
1138	/* although unlocking in the reverse order from locking is not
1139	   strictly necessary here it is a little cleaner to be consistent */
1140	unlock_two_nondirectories(src_inode, target_inode);
1141out:
1142	free_xid(xid);
1143	return rc < 0 ? rc : len;
1144}
1145
1146ssize_t cifs_file_copychunk_range(unsigned int xid,
1147				struct file *src_file, loff_t off,
1148				struct file *dst_file, loff_t destoff,
1149				size_t len, unsigned int flags)
1150{
1151	struct inode *src_inode = file_inode(src_file);
1152	struct inode *target_inode = file_inode(dst_file);
1153	struct cifsFileInfo *smb_file_src;
1154	struct cifsFileInfo *smb_file_target;
1155	struct cifs_tcon *src_tcon;
1156	struct cifs_tcon *target_tcon;
1157	ssize_t rc;
1158
1159	cifs_dbg(FYI, "copychunk range\n");
1160
1161	if (!src_file->private_data || !dst_file->private_data) {
1162		rc = -EBADF;
1163		cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
1164		goto out;
1165	}
1166
1167	rc = -EXDEV;
1168	smb_file_target = dst_file->private_data;
1169	smb_file_src = src_file->private_data;
1170	src_tcon = tlink_tcon(smb_file_src->tlink);
1171	target_tcon = tlink_tcon(smb_file_target->tlink);
1172
1173	if (src_tcon->ses != target_tcon->ses) {
1174		cifs_dbg(VFS, "source and target of copy not on same server\n");
1175		goto out;
1176	}
1177
1178	rc = -EOPNOTSUPP;
1179	if (!target_tcon->ses->server->ops->copychunk_range)
1180		goto out;
1181
1182	/*
1183	 * Note: cifs case is easier than btrfs since server responsible for
1184	 * checks for proper open modes and file type and if it wants
1185	 * server could even support copy of range where source = target
1186	 */
1187	lock_two_nondirectories(target_inode, src_inode);
1188
1189	cifs_dbg(FYI, "about to flush pages\n");
1190	/* should we flush first and last page first */
1191	truncate_inode_pages(&target_inode->i_data, 0);
1192
1193	rc = file_modified(dst_file);
1194	if (!rc)
1195		rc = target_tcon->ses->server->ops->copychunk_range(xid,
1196			smb_file_src, smb_file_target, off, len, destoff);
1197
1198	file_accessed(src_file);
1199
1200	/* force revalidate of size and timestamps of target file now
1201	 * that target is updated on the server
1202	 */
1203	CIFS_I(target_inode)->time = 0;
1204	/* although unlocking in the reverse order from locking is not
1205	 * strictly necessary here it is a little cleaner to be consistent
1206	 */
1207	unlock_two_nondirectories(src_inode, target_inode);
1208
1209out:
1210	return rc;
1211}
1212
1213/*
1214 * Directory operations under CIFS/SMB2/SMB3 are synchronous, so fsync()
1215 * is a dummy operation.
1216 */
1217static int cifs_dir_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1218{
1219	cifs_dbg(FYI, "Sync directory - name: %pD datasync: 0x%x\n",
1220		 file, datasync);
1221
1222	return 0;
1223}
1224
1225static ssize_t cifs_copy_file_range(struct file *src_file, loff_t off,
1226				struct file *dst_file, loff_t destoff,
1227				size_t len, unsigned int flags)
1228{
1229	unsigned int xid = get_xid();
1230	ssize_t rc;
1231	struct cifsFileInfo *cfile = dst_file->private_data;
1232
1233	if (cfile->swapfile) {
1234		rc = -EOPNOTSUPP;
1235		free_xid(xid);
1236		return rc;
1237	}
1238
1239	rc = cifs_file_copychunk_range(xid, src_file, off, dst_file, destoff,
1240					len, flags);
1241	free_xid(xid);
1242
1243	if (rc == -EOPNOTSUPP || rc == -EXDEV)
1244		rc = generic_copy_file_range(src_file, off, dst_file,
1245					     destoff, len, flags);
1246	return rc;
1247}
1248
1249const struct file_operations cifs_file_ops = {
1250	.read_iter = cifs_loose_read_iter,
1251	.write_iter = cifs_file_write_iter,
1252	.open = cifs_open,
1253	.release = cifs_close,
1254	.lock = cifs_lock,
1255	.flock = cifs_flock,
1256	.fsync = cifs_fsync,
1257	.flush = cifs_flush,
1258	.mmap  = cifs_file_mmap,
1259	.splice_read = generic_file_splice_read,
1260	.splice_write = iter_file_splice_write,
1261	.llseek = cifs_llseek,
1262	.unlocked_ioctl	= cifs_ioctl,
1263	.copy_file_range = cifs_copy_file_range,
1264	.remap_file_range = cifs_remap_file_range,
1265	.setlease = cifs_setlease,
1266	.fallocate = cifs_fallocate,
1267};
1268
1269const struct file_operations cifs_file_strict_ops = {
1270	.read_iter = cifs_strict_readv,
1271	.write_iter = cifs_strict_writev,
1272	.open = cifs_open,
1273	.release = cifs_close,
1274	.lock = cifs_lock,
1275	.flock = cifs_flock,
1276	.fsync = cifs_strict_fsync,
1277	.flush = cifs_flush,
1278	.mmap = cifs_file_strict_mmap,
1279	.splice_read = generic_file_splice_read,
1280	.splice_write = iter_file_splice_write,
1281	.llseek = cifs_llseek,
1282	.unlocked_ioctl	= cifs_ioctl,
1283	.copy_file_range = cifs_copy_file_range,
1284	.remap_file_range = cifs_remap_file_range,
1285	.setlease = cifs_setlease,
1286	.fallocate = cifs_fallocate,
1287};
1288
1289const struct file_operations cifs_file_direct_ops = {
1290	.read_iter = cifs_direct_readv,
1291	.write_iter = cifs_direct_writev,
1292	.open = cifs_open,
1293	.release = cifs_close,
1294	.lock = cifs_lock,
1295	.flock = cifs_flock,
1296	.fsync = cifs_fsync,
1297	.flush = cifs_flush,
1298	.mmap = cifs_file_mmap,
1299	.splice_read = generic_file_splice_read,
1300	.splice_write = iter_file_splice_write,
1301	.unlocked_ioctl  = cifs_ioctl,
1302	.copy_file_range = cifs_copy_file_range,
1303	.remap_file_range = cifs_remap_file_range,
1304	.llseek = cifs_llseek,
1305	.setlease = cifs_setlease,
1306	.fallocate = cifs_fallocate,
1307};
1308
1309const struct file_operations cifs_file_nobrl_ops = {
1310	.read_iter = cifs_loose_read_iter,
1311	.write_iter = cifs_file_write_iter,
1312	.open = cifs_open,
1313	.release = cifs_close,
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	.llseek = cifs_llseek,
1320	.unlocked_ioctl	= cifs_ioctl,
1321	.copy_file_range = cifs_copy_file_range,
1322	.remap_file_range = cifs_remap_file_range,
1323	.setlease = cifs_setlease,
1324	.fallocate = cifs_fallocate,
1325};
1326
1327const struct file_operations cifs_file_strict_nobrl_ops = {
1328	.read_iter = cifs_strict_readv,
1329	.write_iter = cifs_strict_writev,
1330	.open = cifs_open,
1331	.release = cifs_close,
1332	.fsync = cifs_strict_fsync,
1333	.flush = cifs_flush,
1334	.mmap = cifs_file_strict_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,
1343};
1344
1345const struct file_operations cifs_file_direct_nobrl_ops = {
1346	.read_iter = cifs_direct_readv,
1347	.write_iter = cifs_direct_writev,
1348	.open = cifs_open,
1349	.release = cifs_close,
1350	.fsync = cifs_fsync,
1351	.flush = cifs_flush,
1352	.mmap = cifs_file_mmap,
1353	.splice_read = generic_file_splice_read,
1354	.splice_write = iter_file_splice_write,
1355	.unlocked_ioctl  = cifs_ioctl,
1356	.copy_file_range = cifs_copy_file_range,
1357	.remap_file_range = cifs_remap_file_range,
1358	.llseek = cifs_llseek,
1359	.setlease = cifs_setlease,
1360	.fallocate = cifs_fallocate,
1361};
1362
1363const struct file_operations cifs_dir_ops = {
1364	.iterate_shared = cifs_readdir,
1365	.release = cifs_closedir,
1366	.read    = generic_read_dir,
1367	.unlocked_ioctl  = cifs_ioctl,
1368	.copy_file_range = cifs_copy_file_range,
1369	.remap_file_range = cifs_remap_file_range,
1370	.llseek = generic_file_llseek,
1371	.fsync = cifs_dir_fsync,
1372};
1373
1374static void
1375cifs_init_once(void *inode)
1376{
1377	struct cifsInodeInfo *cifsi = inode;
1378
1379	inode_init_once(&cifsi->vfs_inode);
1380	init_rwsem(&cifsi->lock_sem);
1381}
1382
1383static int __init
1384cifs_init_inodecache(void)
1385{
1386	cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
1387					      sizeof(struct cifsInodeInfo),
1388					      0, (SLAB_RECLAIM_ACCOUNT|
1389						SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1390					      cifs_init_once);
1391	if (cifs_inode_cachep == NULL)
1392		return -ENOMEM;
1393
1394	return 0;
1395}
1396
1397static void
1398cifs_destroy_inodecache(void)
1399{
1400	/*
1401	 * Make sure all delayed rcu free inodes are flushed before we
1402	 * destroy cache.
1403	 */
1404	rcu_barrier();
1405	kmem_cache_destroy(cifs_inode_cachep);
1406}
1407
1408static int
1409cifs_init_request_bufs(void)
1410{
1411	/*
1412	 * SMB2 maximum header size is bigger than CIFS one - no problems to
1413	 * allocate some more bytes for CIFS.
1414	 */
1415	size_t max_hdr_size = MAX_SMB2_HDR_SIZE;
1416
1417	if (CIFSMaxBufSize < 8192) {
1418	/* Buffer size can not be smaller than 2 * PATH_MAX since maximum
1419	Unicode path name has to fit in any SMB/CIFS path based frames */
1420		CIFSMaxBufSize = 8192;
1421	} else if (CIFSMaxBufSize > 1024*127) {
1422		CIFSMaxBufSize = 1024 * 127;
1423	} else {
1424		CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
1425	}
1426/*
1427	cifs_dbg(VFS, "CIFSMaxBufSize %d 0x%x\n",
1428		 CIFSMaxBufSize, CIFSMaxBufSize);
1429*/
1430	cifs_req_cachep = kmem_cache_create_usercopy("cifs_request",
1431					    CIFSMaxBufSize + max_hdr_size, 0,
1432					    SLAB_HWCACHE_ALIGN, 0,
1433					    CIFSMaxBufSize + max_hdr_size,
1434					    NULL);
1435	if (cifs_req_cachep == NULL)
1436		return -ENOMEM;
1437
1438	if (cifs_min_rcv < 1)
1439		cifs_min_rcv = 1;
1440	else if (cifs_min_rcv > 64) {
1441		cifs_min_rcv = 64;
1442		cifs_dbg(VFS, "cifs_min_rcv set to maximum (64)\n");
1443	}
1444
1445	cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
1446						  cifs_req_cachep);
1447
1448	if (cifs_req_poolp == NULL) {
1449		kmem_cache_destroy(cifs_req_cachep);
1450		return -ENOMEM;
1451	}
1452	/* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
1453	almost all handle based requests (but not write response, nor is it
1454	sufficient for path based requests).  A smaller size would have
1455	been more efficient (compacting multiple slab items on one 4k page)
1456	for the case in which debug was on, but this larger size allows
1457	more SMBs to use small buffer alloc and is still much more
1458	efficient to alloc 1 per page off the slab compared to 17K (5page)
1459	alloc of large cifs buffers even when page debugging is on */
1460	cifs_sm_req_cachep = kmem_cache_create_usercopy("cifs_small_rq",
1461			MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
1462			0, MAX_CIFS_SMALL_BUFFER_SIZE, NULL);
1463	if (cifs_sm_req_cachep == NULL) {
1464		mempool_destroy(cifs_req_poolp);
1465		kmem_cache_destroy(cifs_req_cachep);
1466		return -ENOMEM;
1467	}
1468
1469	if (cifs_min_small < 2)
1470		cifs_min_small = 2;
1471	else if (cifs_min_small > 256) {
1472		cifs_min_small = 256;
1473		cifs_dbg(FYI, "cifs_min_small set to maximum (256)\n");
1474	}
1475
1476	cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
1477						     cifs_sm_req_cachep);
1478
1479	if (cifs_sm_req_poolp == NULL) {
1480		mempool_destroy(cifs_req_poolp);
1481		kmem_cache_destroy(cifs_req_cachep);
1482		kmem_cache_destroy(cifs_sm_req_cachep);
1483		return -ENOMEM;
1484	}
1485
1486	return 0;
1487}
1488
1489static void
1490cifs_destroy_request_bufs(void)
1491{
1492	mempool_destroy(cifs_req_poolp);
1493	kmem_cache_destroy(cifs_req_cachep);
1494	mempool_destroy(cifs_sm_req_poolp);
1495	kmem_cache_destroy(cifs_sm_req_cachep);
1496}
1497
1498static int
1499cifs_init_mids(void)
1500{
1501	cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
1502					    sizeof(struct mid_q_entry), 0,
1503					    SLAB_HWCACHE_ALIGN, NULL);
1504	if (cifs_mid_cachep == NULL)
1505		return -ENOMEM;
1506
1507	/* 3 is a reasonable minimum number of simultaneous operations */
1508	cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
1509	if (cifs_mid_poolp == NULL) {
1510		kmem_cache_destroy(cifs_mid_cachep);
1511		return -ENOMEM;
1512	}
1513
1514	return 0;
1515}
1516
1517static void
1518cifs_destroy_mids(void)
1519{
1520	mempool_destroy(cifs_mid_poolp);
1521	kmem_cache_destroy(cifs_mid_cachep);
1522}
1523
1524static int __init
1525init_cifs(void)
1526{
1527	int rc = 0;
1528	cifs_proc_init();
1529	INIT_LIST_HEAD(&cifs_tcp_ses_list);
1530#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* unused temporarily */
1531	INIT_LIST_HEAD(&GlobalDnotifyReqList);
1532	INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
1533#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */
1534/*
1535 *  Initialize Global counters
1536 */
1537	atomic_set(&sesInfoAllocCount, 0);
1538	atomic_set(&tconInfoAllocCount, 0);
1539	atomic_set(&tcpSesAllocCount, 0);
1540	atomic_set(&tcpSesReconnectCount, 0);
1541	atomic_set(&tconInfoReconnectCount, 0);
1542
1543	atomic_set(&bufAllocCount, 0);
1544	atomic_set(&smBufAllocCount, 0);
1545#ifdef CONFIG_CIFS_STATS2
1546	atomic_set(&totBufAllocCount, 0);
1547	atomic_set(&totSmBufAllocCount, 0);
1548	if (slow_rsp_threshold < 1)
1549		cifs_dbg(FYI, "slow_response_threshold msgs disabled\n");
1550	else if (slow_rsp_threshold > 32767)
1551		cifs_dbg(VFS,
1552		       "slow response threshold set higher than recommended (0 to 32767)\n");
1553#endif /* CONFIG_CIFS_STATS2 */
1554
1555	atomic_set(&midCount, 0);
1556	GlobalCurrentXid = 0;
1557	GlobalTotalActiveXid = 0;
1558	GlobalMaxActiveXid = 0;
1559	spin_lock_init(&cifs_tcp_ses_lock);
1560	spin_lock_init(&GlobalMid_Lock);
1561
1562	cifs_lock_secret = get_random_u32();
1563
1564	if (cifs_max_pending < 2) {
1565		cifs_max_pending = 2;
1566		cifs_dbg(FYI, "cifs_max_pending set to min of 2\n");
1567	} else if (cifs_max_pending > CIFS_MAX_REQ) {
1568		cifs_max_pending = CIFS_MAX_REQ;
1569		cifs_dbg(FYI, "cifs_max_pending set to max of %u\n",
1570			 CIFS_MAX_REQ);
1571	}
1572
1573	cifsiod_wq = alloc_workqueue("cifsiod", WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1574	if (!cifsiod_wq) {
1575		rc = -ENOMEM;
1576		goto out_clean_proc;
1577	}
1578
1579	/*
1580	 * Consider in future setting limit!=0 maybe to min(num_of_cores - 1, 3)
1581	 * so that we don't launch too many worker threads but
1582	 * Documentation/core-api/workqueue.rst recommends setting it to 0
1583	 */
1584
1585	/* WQ_UNBOUND allows decrypt tasks to run on any CPU */
1586	decrypt_wq = alloc_workqueue("smb3decryptd",
1587				     WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1588	if (!decrypt_wq) {
1589		rc = -ENOMEM;
1590		goto out_destroy_cifsiod_wq;
1591	}
1592
1593	fileinfo_put_wq = alloc_workqueue("cifsfileinfoput",
1594				     WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1595	if (!fileinfo_put_wq) {
1596		rc = -ENOMEM;
1597		goto out_destroy_decrypt_wq;
1598	}
1599
1600	cifsoplockd_wq = alloc_workqueue("cifsoplockd",
1601					 WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1602	if (!cifsoplockd_wq) {
1603		rc = -ENOMEM;
1604		goto out_destroy_fileinfo_put_wq;
1605	}
1606
1607	rc = cifs_fscache_register();
1608	if (rc)
1609		goto out_destroy_cifsoplockd_wq;
1610
1611	rc = cifs_init_inodecache();
1612	if (rc)
1613		goto out_unreg_fscache;
1614
1615	rc = cifs_init_mids();
1616	if (rc)
1617		goto out_destroy_inodecache;
1618
1619	rc = cifs_init_request_bufs();
1620	if (rc)
1621		goto out_destroy_mids;
1622
1623#ifdef CONFIG_CIFS_DFS_UPCALL
1624	rc = dfs_cache_init();
1625	if (rc)
1626		goto out_destroy_request_bufs;
1627#endif /* CONFIG_CIFS_DFS_UPCALL */
1628#ifdef CONFIG_CIFS_UPCALL
1629	rc = init_cifs_spnego();
1630	if (rc)
1631		goto out_destroy_dfs_cache;
1632#endif /* CONFIG_CIFS_UPCALL */
1633
1634	rc = init_cifs_idmap();
1635	if (rc)
1636		goto out_register_key_type;
1637
1638	rc = register_filesystem(&cifs_fs_type);
1639	if (rc)
1640		goto out_init_cifs_idmap;
1641
1642	rc = register_filesystem(&smb3_fs_type);
1643	if (rc) {
1644		unregister_filesystem(&cifs_fs_type);
1645		goto out_init_cifs_idmap;
1646	}
1647
1648	return 0;
1649
1650out_init_cifs_idmap:
1651	exit_cifs_idmap();
1652out_register_key_type:
1653#ifdef CONFIG_CIFS_UPCALL
1654	exit_cifs_spnego();
1655out_destroy_dfs_cache:
1656#endif
1657#ifdef CONFIG_CIFS_DFS_UPCALL
1658	dfs_cache_destroy();
1659out_destroy_request_bufs:
1660#endif
1661	cifs_destroy_request_bufs();
1662out_destroy_mids:
1663	cifs_destroy_mids();
1664out_destroy_inodecache:
1665	cifs_destroy_inodecache();
1666out_unreg_fscache:
1667	cifs_fscache_unregister();
1668out_destroy_cifsoplockd_wq:
1669	destroy_workqueue(cifsoplockd_wq);
1670out_destroy_fileinfo_put_wq:
1671	destroy_workqueue(fileinfo_put_wq);
1672out_destroy_decrypt_wq:
1673	destroy_workqueue(decrypt_wq);
1674out_destroy_cifsiod_wq:
1675	destroy_workqueue(cifsiod_wq);
1676out_clean_proc:
1677	cifs_proc_clean();
1678	return rc;
1679}
1680
1681static void __exit
1682exit_cifs(void)
1683{
1684	cifs_dbg(NOISY, "exit_smb3\n");
1685	unregister_filesystem(&cifs_fs_type);
1686	unregister_filesystem(&smb3_fs_type);
1687	cifs_dfs_release_automount_timer();
1688	exit_cifs_idmap();
1689#ifdef CONFIG_CIFS_UPCALL
1690	exit_cifs_spnego();
1691#endif
1692#ifdef CONFIG_CIFS_DFS_UPCALL
1693	dfs_cache_destroy();
1694#endif
1695	cifs_destroy_request_bufs();
1696	cifs_destroy_mids();
1697	cifs_destroy_inodecache();
1698	cifs_fscache_unregister();
1699	destroy_workqueue(cifsoplockd_wq);
1700	destroy_workqueue(decrypt_wq);
1701	destroy_workqueue(fileinfo_put_wq);
1702	destroy_workqueue(cifsiod_wq);
1703	cifs_proc_clean();
1704}
1705
1706MODULE_AUTHOR("Steve French");
1707MODULE_LICENSE("GPL");	/* combination of LGPL + GPL source behaves as GPL */
1708MODULE_DESCRIPTION
1709	("VFS to access SMB3 servers e.g. Samba, Macs, Azure and Windows (and "
1710	"also older servers complying with the SNIA CIFS Specification)");
1711MODULE_VERSION(CIFS_VERSION);
1712MODULE_SOFTDEP("ecb");
1713MODULE_SOFTDEP("hmac");
1714MODULE_SOFTDEP("md4");
1715MODULE_SOFTDEP("md5");
1716MODULE_SOFTDEP("nls");
1717MODULE_SOFTDEP("aes");
1718MODULE_SOFTDEP("cmac");
1719MODULE_SOFTDEP("sha256");
1720MODULE_SOFTDEP("sha512");
1721MODULE_SOFTDEP("aead2");
1722MODULE_SOFTDEP("ccm");
1723MODULE_SOFTDEP("gcm");
1724module_init(init_cifs)
1725module_exit(exit_cifs)
1726