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