xref: /linux/fs/smb/client/cifsfs.c (revision 558c3eca715ea0110fe6fce38b063a6f9936188c)
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
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  */
10 
11 /* Note that BB means BUGBUG (ie something to fix eventually) */
12 
13 #include <linux/module.h>
14 #include <linux/fs.h>
15 #include <linux/filelock.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/splice.h>
29 #include <linux/uuid.h>
30 #include <linux/xattr.h>
31 #include <linux/mm.h>
32 #include <linux/key-type.h>
33 #include <uapi/linux/magic.h>
34 #include <net/ipv6.h>
35 #include "cifsfs.h"
36 #define DECLARE_GLOBALS_HERE
37 #include "cifsglob.h"
38 #include "cifsproto.h"
39 #include "smb2proto.h"
40 #include "cifs_debug.h"
41 #include "cifs_fs_sb.h"
42 #include "cifs_spnego.h"
43 #include "fscache.h"
44 #ifdef CONFIG_CIFS_DFS_UPCALL
45 #include "dfs_cache.h"
46 #endif
47 #ifdef CONFIG_CIFS_SWN_UPCALL
48 #include "netlink.h"
49 #endif
50 #include "fs_context.h"
51 #include "cached_dir.h"
52 
53 /*
54  * DOS dates from 1980/1/1 through 2107/12/31
55  * Protocol specifications indicate the range should be to 119, which
56  * limits maximum year to 2099. But this range has not been checked.
57  */
58 #define SMB_DATE_MAX (127<<9 | 12<<5 | 31)
59 #define SMB_DATE_MIN (0<<9 | 1<<5 | 1)
60 #define SMB_TIME_MAX (23<<11 | 59<<5 | 29)
61 
62 int cifsFYI = 0;
63 bool traceSMB;
64 bool enable_oplocks = true;
65 bool linuxExtEnabled = true;
66 bool lookupCacheEnabled = true;
67 bool disable_legacy_dialects; /* false by default */
68 bool enable_gcm_256 = true;
69 bool require_gcm_256; /* false by default */
70 bool enable_negotiate_signing; /* false by default */
71 unsigned int global_secflags = CIFSSEC_DEF;
72 /* unsigned int ntlmv2_support = 0; */
73 
74 /*
75  * Global transaction id (XID) information
76  */
77 unsigned int GlobalCurrentXid;	/* protected by GlobalMid_Lock */
78 unsigned int GlobalTotalActiveXid; /* prot by GlobalMid_Lock */
79 unsigned int GlobalMaxActiveXid;	/* prot by GlobalMid_Lock */
80 DEFINE_SPINLOCK(GlobalMid_Lock); /* protects above & list operations on midQ entries */
81 
82 /*
83  *  Global counters, updated atomically
84  */
85 atomic_t sesInfoAllocCount;
86 atomic_t tconInfoAllocCount;
87 atomic_t tcpSesNextId;
88 atomic_t tcpSesAllocCount;
89 atomic_t tcpSesReconnectCount;
90 atomic_t tconInfoReconnectCount;
91 
92 atomic_t mid_count;
93 atomic_t buf_alloc_count;
94 atomic_t small_buf_alloc_count;
95 #ifdef CONFIG_CIFS_STATS2
96 atomic_t total_buf_alloc_count;
97 atomic_t total_small_buf_alloc_count;
98 #endif/* STATS2 */
99 struct list_head	cifs_tcp_ses_list;
100 DEFINE_SPINLOCK(cifs_tcp_ses_lock);
101 static const struct super_operations cifs_super_ops;
102 unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
103 module_param(CIFSMaxBufSize, uint, 0444);
104 MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header) "
105 				 "for CIFS requests. "
106 				 "Default: 16384 Range: 8192 to 130048");
107 unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
108 module_param(cifs_min_rcv, uint, 0444);
109 MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
110 				"1 to 64");
111 unsigned int cifs_min_small = 30;
112 module_param(cifs_min_small, uint, 0444);
113 MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
114 				 "Range: 2 to 256");
115 unsigned int cifs_max_pending = CIFS_MAX_REQ;
116 module_param(cifs_max_pending, uint, 0444);
117 MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server for "
118 				   "CIFS/SMB1 dialect (N/A for SMB3) "
119 				   "Default: 32767 Range: 2 to 32767.");
120 unsigned int dir_cache_timeout = 30;
121 module_param(dir_cache_timeout, uint, 0644);
122 MODULE_PARM_DESC(dir_cache_timeout, "Number of seconds to cache directory contents for which we have a lease. Default: 30 "
123 				 "Range: 1 to 65000 seconds, 0 to disable caching dir contents");
124 /* Module-wide total cached dirents (in bytes) across all tcons */
125 atomic64_t cifs_dircache_bytes_used = ATOMIC64_INIT(0);
126 
127 atomic_t cifs_sillycounter;
128 atomic_t cifs_tmpcounter;
129 
130 #ifdef CONFIG_CIFS_STATS2
131 unsigned int slow_rsp_threshold = 1;
132 module_param(slow_rsp_threshold, uint, 0644);
133 MODULE_PARM_DESC(slow_rsp_threshold, "Amount of time (in seconds) to wait "
134 				   "before logging that a response is delayed. "
135 				   "Default: 1 (if set to 0 disables msg).");
136 #endif /* STATS2 */
137 
138 module_param(enable_oplocks, bool, 0644);
139 MODULE_PARM_DESC(enable_oplocks, "Enable or disable oplocks. Default: y/Y/1");
140 
141 module_param(enable_gcm_256, bool, 0644);
142 MODULE_PARM_DESC(enable_gcm_256, "Enable requesting strongest (256 bit) GCM encryption. Default: y/Y/1");
143 
144 module_param(require_gcm_256, bool, 0644);
145 MODULE_PARM_DESC(require_gcm_256, "Require strongest (256 bit) GCM encryption. Default: n/N/0");
146 
147 module_param(enable_negotiate_signing, bool, 0644);
148 MODULE_PARM_DESC(enable_negotiate_signing, "Enable negotiating packet signing algorithm with server. Default: n/N/0");
149 
150 module_param(disable_legacy_dialects, bool, 0644);
151 MODULE_PARM_DESC(disable_legacy_dialects, "To improve security it may be "
152 				  "helpful to restrict the ability to "
153 				  "override the default dialects (SMB2.1, "
154 				  "SMB3 and SMB3.02) on mount with old "
155 				  "dialects (CIFS/SMB1 and SMB2) since "
156 				  "vers=1.0 (CIFS/SMB1) and vers=2.0 are weaker"
157 				  " and less secure. Default: n/N/0");
158 
159 struct workqueue_struct	*cifsiod_wq;
160 struct workqueue_struct	*decrypt_wq;
161 struct workqueue_struct	*fileinfo_put_wq;
162 struct workqueue_struct	*cifsoplockd_wq;
163 struct workqueue_struct	*deferredclose_wq;
164 struct workqueue_struct	*serverclose_wq;
165 struct workqueue_struct	*cfid_put_wq;
166 __u32 cifs_lock_secret;
167 
168 /*
169  * Bumps refcount for cifs super block.
170  * Note that it should be only called if a reference to VFS super block is
171  * already held, e.g. in open-type syscalls context. Otherwise it can race with
172  * atomic_dec_and_test in deactivate_locked_super.
173  */
174 void
175 cifs_sb_active(struct super_block *sb)
176 {
177 	struct cifs_sb_info *server = CIFS_SB(sb);
178 
179 	if (atomic_inc_return(&server->active) == 1)
180 		atomic_inc(&sb->s_active);
181 }
182 
183 void
184 cifs_sb_deactive(struct super_block *sb)
185 {
186 	struct cifs_sb_info *server = CIFS_SB(sb);
187 
188 	if (atomic_dec_and_test(&server->active))
189 		deactivate_super(sb);
190 }
191 
192 static int
193 cifs_read_super(struct super_block *sb)
194 {
195 	struct cifs_sb_info *cifs_sb;
196 	struct cifs_tcon *tcon;
197 	unsigned int sbflags;
198 	struct timespec64 ts;
199 	struct inode *inode;
200 	int rc = 0;
201 
202 	cifs_sb = CIFS_SB(sb);
203 	tcon = cifs_sb_master_tcon(cifs_sb);
204 	sbflags = cifs_sb_flags(cifs_sb);
205 
206 	if (sbflags & CIFS_MOUNT_POSIXACL)
207 		sb->s_flags |= SB_POSIXACL;
208 
209 	if (tcon->snapshot_time)
210 		sb->s_flags |= SB_RDONLY;
211 
212 	if (tcon->ses->capabilities & tcon->ses->server->vals->cap_large_files)
213 		sb->s_maxbytes = MAX_LFS_FILESIZE;
214 	else
215 		sb->s_maxbytes = MAX_NON_LFS;
216 
217 	/*
218 	 * Some very old servers like DOS and OS/2 used 2 second granularity
219 	 * (while all current servers use 100ns granularity - see MS-DTYP)
220 	 * but 1 second is the maximum allowed granularity for the VFS
221 	 * so for old servers set time granularity to 1 second while for
222 	 * everything else (current servers) set it to 100ns.
223 	 */
224 	if ((tcon->ses->server->vals->protocol_id == SMB10_PROT_ID) &&
225 	    ((tcon->ses->capabilities &
226 	      tcon->ses->server->vals->cap_nt_find) == 0) &&
227 	    !tcon->unix_ext) {
228 		sb->s_time_gran = 1000000000; /* 1 second is max allowed gran */
229 		ts = cnvrtDosUnixTm(cpu_to_le16(SMB_DATE_MIN), 0, 0);
230 		sb->s_time_min = ts.tv_sec;
231 		ts = cnvrtDosUnixTm(cpu_to_le16(SMB_DATE_MAX),
232 				    cpu_to_le16(SMB_TIME_MAX), 0);
233 		sb->s_time_max = ts.tv_sec;
234 	} else {
235 		/*
236 		 * Almost every server, including all SMB2+, uses DCE TIME
237 		 * ie 100 nanosecond units, since 1601.  See MS-DTYP and MS-FSCC
238 		 */
239 		sb->s_time_gran = 100;
240 		ts = cifs_NTtimeToUnix(0);
241 		sb->s_time_min = ts.tv_sec;
242 		ts = cifs_NTtimeToUnix(cpu_to_le64(S64_MAX));
243 		sb->s_time_max = ts.tv_sec;
244 	}
245 
246 	sb->s_magic = CIFS_SUPER_MAGIC;
247 	sb->s_op = &cifs_super_ops;
248 	sb->s_xattr = cifs_xattr_handlers;
249 	rc = super_setup_bdi(sb);
250 	if (rc)
251 		goto out_no_root;
252 	/* tune readahead according to rsize if readahead size not set on mount */
253 	if (cifs_sb->ctx->rsize == 0)
254 		cifs_sb->ctx->rsize =
255 			tcon->ses->server->ops->negotiate_rsize(tcon, cifs_sb->ctx);
256 	if (cifs_sb->ctx->rasize)
257 		sb->s_bdi->ra_pages = cifs_sb->ctx->rasize / PAGE_SIZE;
258 	else
259 		sb->s_bdi->ra_pages = 2 * (cifs_sb->ctx->rsize / PAGE_SIZE);
260 
261 	sb->s_blocksize = CIFS_MAX_MSGSIZE;
262 	sb->s_blocksize_bits = 14;	/* default 2**14 = CIFS_MAX_MSGSIZE */
263 	inode = cifs_root_iget(sb);
264 
265 	if (IS_ERR(inode)) {
266 		rc = PTR_ERR(inode);
267 		goto out_no_root;
268 	}
269 
270 	if (tcon->nocase)
271 		set_default_d_op(sb, &cifs_ci_dentry_ops);
272 	else
273 		set_default_d_op(sb, &cifs_dentry_ops);
274 
275 	sb->s_root = d_make_root(inode);
276 	if (!sb->s_root) {
277 		rc = -ENOMEM;
278 		goto out_no_root;
279 	}
280 
281 #ifdef CONFIG_CIFS_NFSD_EXPORT
282 	if (sbflags & CIFS_MOUNT_SERVER_INUM) {
283 		cifs_dbg(FYI, "export ops supported\n");
284 		sb->s_export_op = &cifs_export_ops;
285 	}
286 #endif /* CONFIG_CIFS_NFSD_EXPORT */
287 
288 	return 0;
289 
290 out_no_root:
291 	cifs_dbg(VFS, "%s: get root inode failed\n", __func__);
292 	return rc;
293 }
294 
295 static void cifs_kill_sb(struct super_block *sb)
296 {
297 	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
298 
299 	/*
300 	 * We need to release all dentries for the cached directories
301 	 * and close all deferred file handles before we kill the sb.
302 	 */
303 	if (cifs_sb->root) {
304 		close_all_cached_dirs(cifs_sb);
305 		cifs_close_all_deferred_files_sb(cifs_sb);
306 
307 		/* Wait for all pending oplock breaks to complete */
308 		flush_workqueue(cifsoplockd_wq);
309 		/* Wait for all opened files to release */
310 		flush_workqueue(deferredclose_wq);
311 
312 		/* finally release root dentry */
313 		dput(cifs_sb->root);
314 		cifs_sb->root = NULL;
315 	}
316 
317 	kill_anon_super(sb);
318 	cifs_umount(cifs_sb);
319 }
320 
321 static int
322 cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
323 {
324 	struct super_block *sb = dentry->d_sb;
325 	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
326 	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
327 	struct TCP_Server_Info *server = tcon->ses->server;
328 	unsigned int xid;
329 	int rc = 0;
330 	const char *full_path;
331 	void *page;
332 
333 	xid = get_xid();
334 	page = alloc_dentry_path();
335 
336 	full_path = build_path_from_dentry(dentry, page);
337 	if (IS_ERR(full_path)) {
338 		rc = PTR_ERR(full_path);
339 		goto statfs_out;
340 	}
341 
342 	if (le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength) > 0)
343 		buf->f_namelen =
344 		       le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength);
345 	else
346 		buf->f_namelen = PATH_MAX;
347 
348 	buf->f_fsid.val[0] = tcon->vol_serial_number;
349 	/* are using part of create time for more randomness, see man statfs */
350 	buf->f_fsid.val[1] =  (int)le64_to_cpu(tcon->vol_create_time);
351 
352 	buf->f_files = 0;	/* undefined */
353 	buf->f_ffree = 0;	/* unlimited */
354 
355 	if (server->ops->queryfs)
356 		rc = server->ops->queryfs(xid, tcon, full_path, cifs_sb, buf);
357 
358 statfs_out:
359 	free_dentry_path(page);
360 	free_xid(xid);
361 	return rc;
362 }
363 
364 static long cifs_fallocate(struct file *file, int mode, loff_t off, loff_t len)
365 {
366 	struct cifs_tcon *tcon = cifs_sb_master_tcon(CIFS_SB(file));
367 	struct TCP_Server_Info *server = tcon->ses->server;
368 	struct inode *inode = file_inode(file);
369 	int rc;
370 
371 	if (!server->ops->fallocate)
372 		return -EOPNOTSUPP;
373 
374 	rc = inode_lock_killable(inode);
375 	if (rc)
376 		return rc;
377 
378 	netfs_wait_for_outstanding_io(inode);
379 
380 	rc = file_modified(file);
381 	if (rc)
382 		goto out_unlock;
383 
384 	rc = server->ops->fallocate(file, tcon, mode, off, len);
385 
386 out_unlock:
387 	inode_unlock(inode);
388 	return rc;
389 }
390 
391 static int cifs_permission(struct mnt_idmap *idmap,
392 			   struct inode *inode, int mask)
393 {
394 	unsigned int sbflags = cifs_sb_flags(CIFS_SB(inode));
395 
396 	if (sbflags & CIFS_MOUNT_NO_PERM) {
397 		if ((mask & MAY_EXEC) && !execute_ok(inode))
398 			return -EACCES;
399 		else
400 			return 0;
401 	} else /* file mode might have been restricted at mount time
402 		on the client (above and beyond ACL on servers) for
403 		servers which do not support setting and viewing mode bits,
404 		so allowing client to check permissions is useful */
405 		return generic_permission(&nop_mnt_idmap, inode, mask);
406 }
407 
408 static struct kmem_cache *cifs_inode_cachep;
409 static struct kmem_cache *cifs_req_cachep;
410 static struct kmem_cache *cifs_mid_cachep;
411 static struct kmem_cache *cifs_sm_req_cachep;
412 static struct kmem_cache *cifs_io_request_cachep;
413 static struct kmem_cache *cifs_io_subrequest_cachep;
414 mempool_t *cifs_sm_req_poolp;
415 mempool_t *cifs_req_poolp;
416 mempool_t cifs_mid_pool;
417 mempool_t cifs_io_request_pool;
418 mempool_t cifs_io_subrequest_pool;
419 
420 static struct inode *
421 cifs_alloc_inode(struct super_block *sb)
422 {
423 	struct cifsInodeInfo *cifs_inode;
424 	cifs_inode = alloc_inode_sb(sb, cifs_inode_cachep, GFP_KERNEL);
425 	if (!cifs_inode)
426 		return NULL;
427 	cifs_inode->cifsAttrs = ATTR_ARCHIVE;	/* default */
428 	cifs_inode->time = 0;
429 	/*
430 	 * Until the file is open and we have gotten oplock info back from the
431 	 * server, can not assume caching of file data or metadata.
432 	 */
433 	cifs_set_oplock_level(cifs_inode, 0);
434 	cifs_inode->lease_granted = false;
435 	cifs_inode->flags = 0;
436 	spin_lock_init(&cifs_inode->writers_lock);
437 	cifs_inode->writers = 0;
438 	cifs_inode->netfs.inode.i_blkbits = 14;  /* 2**14 = CIFS_MAX_MSGSIZE */
439 	cifs_inode->netfs._remote_i_size = 0;
440 	cifs_inode->netfs._zero_point = 0;
441 	cifs_inode->uniqueid = 0;
442 	cifs_inode->createtime = 0;
443 	cifs_inode->epoch = 0;
444 	spin_lock_init(&cifs_inode->open_file_lock);
445 	generate_random_uuid(cifs_inode->lease_key);
446 	cifs_inode->symlink_target = NULL;
447 
448 	/*
449 	 * Can not set i_flags here - they get immediately overwritten to zero
450 	 * by the VFS.
451 	 */
452 	/* cifs_inode->netfs.inode.i_flags = S_NOATIME | S_NOCMTIME; */
453 	INIT_LIST_HEAD(&cifs_inode->openFileList);
454 	INIT_LIST_HEAD(&cifs_inode->llist);
455 	INIT_LIST_HEAD(&cifs_inode->deferred_closes);
456 	spin_lock_init(&cifs_inode->deferred_lock);
457 	return &cifs_inode->netfs.inode;
458 }
459 
460 static void
461 cifs_free_inode(struct inode *inode)
462 {
463 	struct cifsInodeInfo *cinode = CIFS_I(inode);
464 
465 	if (S_ISLNK(inode->i_mode))
466 		kfree(cinode->symlink_target);
467 	kmem_cache_free(cifs_inode_cachep, cinode);
468 }
469 
470 static void
471 cifs_evict_inode(struct inode *inode)
472 {
473 	netfs_wait_for_outstanding_io(inode);
474 	truncate_inode_pages_final(&inode->i_data);
475 	if (inode_state_read_once(inode) & I_PINNING_NETFS_WB)
476 		cifs_fscache_unuse_inode_cookie(inode, true);
477 	cifs_fscache_release_inode_cookie(inode);
478 	clear_inode(inode);
479 }
480 
481 static void
482 cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
483 {
484 	struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
485 	struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
486 
487 	seq_puts(s, ",addr=");
488 
489 	switch (server->dstaddr.ss_family) {
490 	case AF_INET:
491 		seq_printf(s, "%pI4", &sa->sin_addr.s_addr);
492 		break;
493 	case AF_INET6:
494 		seq_printf(s, "%pI6", &sa6->sin6_addr.s6_addr);
495 		if (sa6->sin6_scope_id)
496 			seq_printf(s, "%%%u", sa6->sin6_scope_id);
497 		break;
498 	default:
499 		seq_puts(s, "(unknown)");
500 	}
501 	if (server->rdma)
502 		seq_puts(s, ",rdma");
503 }
504 
505 static void
506 cifs_show_security(struct seq_file *s, struct cifs_ses *ses)
507 {
508 	if (ses->sectype == Unspecified) {
509 		if (ses->user_name == NULL)
510 			seq_puts(s, ",sec=none");
511 		return;
512 	}
513 
514 	seq_puts(s, ",sec=");
515 
516 	switch (ses->sectype) {
517 	case NTLMv2:
518 		seq_puts(s, "ntlmv2");
519 		break;
520 	case Kerberos:
521 		seq_puts(s, "krb5");
522 		break;
523 	case RawNTLMSSP:
524 		seq_puts(s, "ntlmssp");
525 		break;
526 	default:
527 		/* shouldn't ever happen */
528 		seq_puts(s, "unknown");
529 		break;
530 	}
531 
532 	if (ses->sign)
533 		seq_puts(s, "i");
534 
535 	if (ses->sectype == Kerberos)
536 		seq_printf(s, ",cruid=%u",
537 			   from_kuid_munged(&init_user_ns, ses->cred_uid));
538 }
539 
540 static void
541 cifs_show_cache_flavor(struct seq_file *s, struct cifs_sb_info *cifs_sb)
542 {
543 	unsigned int sbflags = cifs_sb_flags(cifs_sb);
544 
545 	seq_puts(s, ",cache=");
546 
547 	if (sbflags & CIFS_MOUNT_STRICT_IO)
548 		seq_puts(s, "strict");
549 	else if (sbflags & CIFS_MOUNT_DIRECT_IO)
550 		seq_puts(s, "none");
551 	else if (sbflags & CIFS_MOUNT_RW_CACHE)
552 		seq_puts(s, "singleclient"); /* assume only one client access */
553 	else if (sbflags & CIFS_MOUNT_RO_CACHE)
554 		seq_puts(s, "ro"); /* read only caching assumed */
555 	else
556 		seq_puts(s, "loose");
557 }
558 
559 /*
560  * cifs_show_devname() is used so we show the mount device name with correct
561  * format (e.g. forward slashes vs. back slashes) in /proc/mounts
562  */
563 static int cifs_show_devname(struct seq_file *m, struct dentry *root)
564 {
565 	struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
566 	char *devname = kstrdup(cifs_sb->ctx->source, GFP_KERNEL);
567 
568 	if (devname == NULL)
569 		seq_puts(m, "none");
570 	else {
571 		convert_delimiter(devname, '/');
572 		/* escape all spaces in share names */
573 		seq_escape(m, devname, " \t");
574 		kfree(devname);
575 	}
576 	return 0;
577 }
578 
579 static void
580 cifs_show_upcall_target(struct seq_file *s, struct cifs_sb_info *cifs_sb)
581 {
582 	if (cifs_sb->ctx->upcall_target == UPTARGET_UNSPECIFIED) {
583 		seq_puts(s, ",upcall_target=app");
584 		return;
585 	}
586 
587 	seq_puts(s, ",upcall_target=");
588 
589 	switch (cifs_sb->ctx->upcall_target) {
590 	case UPTARGET_APP:
591 		seq_puts(s, "app");
592 		break;
593 	case UPTARGET_MOUNT:
594 		seq_puts(s, "mount");
595 		break;
596 	default:
597 		/* shouldn't ever happen */
598 		seq_puts(s, "unknown");
599 		break;
600 	}
601 }
602 
603 /*
604  * cifs_show_options() is for displaying mount options in /proc/mounts.
605  * Not all settable options are displayed but most of the important
606  * ones are.
607  */
608 static int
609 cifs_show_options(struct seq_file *s, struct dentry *root)
610 {
611 	struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
612 	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
613 	struct sockaddr *srcaddr;
614 	unsigned int sbflags;
615 
616 	srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
617 
618 	seq_show_option(s, "vers", tcon->ses->server->vals->version_string);
619 	cifs_show_security(s, tcon->ses);
620 	cifs_show_cache_flavor(s, cifs_sb);
621 	cifs_show_upcall_target(s, cifs_sb);
622 
623 	if (tcon->no_lease)
624 		seq_puts(s, ",nolease");
625 	if (cifs_sb->ctx->multiuser)
626 		seq_puts(s, ",multiuser");
627 	else if (tcon->ses->user_name)
628 		seq_show_option(s, "username", tcon->ses->user_name);
629 
630 	if (tcon->ses->domainName && tcon->ses->domainName[0] != 0)
631 		seq_show_option(s, "domain", tcon->ses->domainName);
632 
633 	if (srcaddr->sa_family != AF_UNSPEC) {
634 		struct sockaddr_in *saddr4;
635 		struct sockaddr_in6 *saddr6;
636 		saddr4 = (struct sockaddr_in *)srcaddr;
637 		saddr6 = (struct sockaddr_in6 *)srcaddr;
638 		if (srcaddr->sa_family == AF_INET6)
639 			seq_printf(s, ",srcaddr=%pI6c",
640 				   &saddr6->sin6_addr);
641 		else if (srcaddr->sa_family == AF_INET)
642 			seq_printf(s, ",srcaddr=%pI4",
643 				   &saddr4->sin_addr.s_addr);
644 		else
645 			seq_printf(s, ",srcaddr=BAD-AF:%i",
646 				   (int)(srcaddr->sa_family));
647 	}
648 
649 	sbflags = cifs_sb_flags(cifs_sb);
650 	seq_printf(s, ",uid=%u",
651 		   from_kuid_munged(&init_user_ns, cifs_sb->ctx->linux_uid));
652 	if (sbflags & CIFS_MOUNT_OVERR_UID)
653 		seq_puts(s, ",forceuid");
654 	else
655 		seq_puts(s, ",noforceuid");
656 
657 	seq_printf(s, ",gid=%u",
658 		   from_kgid_munged(&init_user_ns, cifs_sb->ctx->linux_gid));
659 	if (sbflags & CIFS_MOUNT_OVERR_GID)
660 		seq_puts(s, ",forcegid");
661 	else
662 		seq_puts(s, ",noforcegid");
663 
664 	cifs_show_address(s, tcon->ses->server);
665 
666 	if (!tcon->unix_ext)
667 		seq_printf(s, ",file_mode=0%ho,dir_mode=0%ho",
668 					   cifs_sb->ctx->file_mode,
669 					   cifs_sb->ctx->dir_mode);
670 	if (cifs_sb->ctx->iocharset)
671 		seq_printf(s, ",iocharset=%s", cifs_sb->ctx->iocharset);
672 	if (tcon->ses->unicode == 0)
673 		seq_puts(s, ",nounicode");
674 	else if (tcon->ses->unicode == 1)
675 		seq_puts(s, ",unicode");
676 	if (tcon->seal)
677 		seq_puts(s, ",seal");
678 	else if (tcon->ses->server->ignore_signature)
679 		seq_puts(s, ",signloosely");
680 	if (tcon->nocase)
681 		seq_puts(s, ",nocase");
682 	if (tcon->nodelete)
683 		seq_puts(s, ",nodelete");
684 	if (cifs_sb->ctx->no_sparse)
685 		seq_puts(s, ",nosparse");
686 	if (tcon->local_lease)
687 		seq_puts(s, ",locallease");
688 	if (tcon->retry)
689 		seq_puts(s, ",hard");
690 	else
691 		seq_puts(s, ",soft");
692 	if (tcon->use_persistent)
693 		seq_puts(s, ",persistenthandles");
694 	else if (tcon->use_resilient)
695 		seq_puts(s, ",resilienthandles");
696 	if (tcon->posix_extensions)
697 		seq_puts(s, ",posix");
698 	else if (tcon->unix_ext)
699 		seq_puts(s, ",unix");
700 	else
701 		seq_puts(s, ",nounix");
702 	if (sbflags & CIFS_MOUNT_NO_DFS)
703 		seq_puts(s, ",nodfs");
704 	if (sbflags & CIFS_MOUNT_POSIX_PATHS)
705 		seq_puts(s, ",posixpaths");
706 	if (sbflags & CIFS_MOUNT_SET_UID)
707 		seq_puts(s, ",setuids");
708 	if (sbflags & CIFS_MOUNT_UID_FROM_ACL)
709 		seq_puts(s, ",idsfromsid");
710 	if (sbflags & CIFS_MOUNT_SERVER_INUM)
711 		seq_puts(s, ",serverino");
712 	if (sbflags & CIFS_MOUNT_RWPIDFORWARD)
713 		seq_puts(s, ",rwpidforward");
714 	if (sbflags & CIFS_MOUNT_NOPOSIXBRL)
715 		seq_puts(s, ",forcemand");
716 	if (sbflags & CIFS_MOUNT_NO_XATTR)
717 		seq_puts(s, ",nouser_xattr");
718 	if (sbflags & CIFS_MOUNT_MAP_SPECIAL_CHR)
719 		seq_puts(s, ",mapchars");
720 	if (sbflags & CIFS_MOUNT_MAP_SFM_CHR)
721 		seq_puts(s, ",mapposix");
722 	if (sbflags & CIFS_MOUNT_UNX_EMUL)
723 		seq_puts(s, ",sfu");
724 	if (sbflags & CIFS_MOUNT_NO_BRL)
725 		seq_puts(s, ",nobrl");
726 	if (sbflags & CIFS_MOUNT_NO_HANDLE_CACHE)
727 		seq_puts(s, ",nohandlecache");
728 	if (sbflags & CIFS_MOUNT_MODE_FROM_SID)
729 		seq_puts(s, ",modefromsid");
730 	if (sbflags & CIFS_MOUNT_CIFS_ACL)
731 		seq_puts(s, ",cifsacl");
732 	if (sbflags & CIFS_MOUNT_DYNPERM)
733 		seq_puts(s, ",dynperm");
734 	if (root->d_sb->s_flags & SB_POSIXACL)
735 		seq_puts(s, ",acl");
736 	if (sbflags & CIFS_MOUNT_MF_SYMLINKS)
737 		seq_puts(s, ",mfsymlinks");
738 	if (sbflags & CIFS_MOUNT_FSCACHE)
739 		seq_puts(s, ",fsc");
740 	if (sbflags & CIFS_MOUNT_NOSSYNC)
741 		seq_puts(s, ",nostrictsync");
742 	if (sbflags & CIFS_MOUNT_NO_PERM)
743 		seq_puts(s, ",noperm");
744 	if (sbflags & CIFS_MOUNT_CIFS_BACKUPUID)
745 		seq_printf(s, ",backupuid=%u",
746 			   from_kuid_munged(&init_user_ns,
747 					    cifs_sb->ctx->backupuid));
748 	if (sbflags & CIFS_MOUNT_CIFS_BACKUPGID)
749 		seq_printf(s, ",backupgid=%u",
750 			   from_kgid_munged(&init_user_ns,
751 					    cifs_sb->ctx->backupgid));
752 	seq_show_option(s, "reparse",
753 			cifs_reparse_type_str(cifs_sb->ctx->reparse_type));
754 	if (cifs_sb->ctx->nonativesocket)
755 		seq_puts(s, ",nonativesocket");
756 	else
757 		seq_puts(s, ",nativesocket");
758 	seq_show_option(s, "symlink",
759 			cifs_symlink_type_str(cifs_symlink_type(cifs_sb)));
760 
761 	seq_printf(s, ",rsize=%u", cifs_sb->ctx->rsize);
762 	seq_printf(s, ",wsize=%u", cifs_sb->ctx->wsize);
763 	seq_printf(s, ",bsize=%u", cifs_sb->ctx->bsize);
764 	if (cifs_sb->ctx->rasize)
765 		seq_printf(s, ",rasize=%u", cifs_sb->ctx->rasize);
766 	if (tcon->ses->server->min_offload)
767 		seq_printf(s, ",esize=%u", tcon->ses->server->min_offload);
768 	if (tcon->ses->server->retrans)
769 		seq_printf(s, ",retrans=%u", tcon->ses->server->retrans);
770 	seq_printf(s, ",echo_interval=%lu",
771 			tcon->ses->server->echo_interval / HZ);
772 
773 	/* Only display the following if overridden on mount */
774 	if (tcon->ses->server->max_credits != SMB2_MAX_CREDITS_AVAILABLE)
775 		seq_printf(s, ",max_credits=%u", tcon->ses->server->max_credits);
776 	if (tcon->ses->server->tcp_nodelay)
777 		seq_puts(s, ",tcpnodelay");
778 	if (tcon->ses->server->noautotune)
779 		seq_puts(s, ",noautotune");
780 	if (tcon->ses->server->noblocksnd)
781 		seq_puts(s, ",noblocksend");
782 	if (tcon->ses->server->nosharesock)
783 		seq_puts(s, ",nosharesock");
784 
785 	if (tcon->snapshot_time)
786 		seq_printf(s, ",snapshot=%llu", tcon->snapshot_time);
787 	if (tcon->handle_timeout)
788 		seq_printf(s, ",handletimeout=%u", tcon->handle_timeout);
789 	if (tcon->max_cached_dirs != MAX_CACHED_FIDS)
790 		seq_printf(s, ",max_cached_dirs=%u", tcon->max_cached_dirs);
791 
792 	/*
793 	 * Display file and directory attribute timeout in seconds.
794 	 * If file and directory attribute timeout the same then actimeo
795 	 * was likely specified on mount
796 	 */
797 	if (cifs_sb->ctx->acdirmax == cifs_sb->ctx->acregmax)
798 		seq_printf(s, ",actimeo=%lu", cifs_sb->ctx->acregmax / HZ);
799 	else {
800 		seq_printf(s, ",acdirmax=%lu", cifs_sb->ctx->acdirmax / HZ);
801 		seq_printf(s, ",acregmax=%lu", cifs_sb->ctx->acregmax / HZ);
802 	}
803 	seq_printf(s, ",closetimeo=%lu", cifs_sb->ctx->closetimeo / HZ);
804 
805 	if (tcon->ses->chan_max > 1)
806 		seq_printf(s, ",multichannel,max_channels=%zu",
807 			   tcon->ses->chan_max);
808 
809 	if (tcon->use_witness)
810 		seq_puts(s, ",witness");
811 
812 	return 0;
813 }
814 
815 static void cifs_umount_begin(struct super_block *sb)
816 {
817 	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
818 	struct cifs_tcon *tcon;
819 
820 	if (cifs_sb == NULL)
821 		return;
822 
823 	tcon = cifs_sb_master_tcon(cifs_sb);
824 
825 	spin_lock(&cifs_tcp_ses_lock);
826 	spin_lock(&tcon->tc_lock);
827 	trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
828 			    netfs_trace_tcon_ref_see_umount);
829 	if ((tcon->tc_count > 1) || (tcon->status == TID_EXITING)) {
830 		/* we have other mounts to same share or we have
831 		   already tried to umount this and woken up
832 		   all waiting network requests, nothing to do */
833 		spin_unlock(&tcon->tc_lock);
834 		spin_unlock(&cifs_tcp_ses_lock);
835 		return;
836 	}
837 	/*
838 	 * can not set tcon->status to TID_EXITING yet since we don't know if umount -f will
839 	 * fail later (e.g. due to open files).  TID_EXITING will be set just before tdis req sent
840 	 */
841 	spin_unlock(&tcon->tc_lock);
842 	spin_unlock(&cifs_tcp_ses_lock);
843 
844 	/* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
845 	/* cancel_notify_requests(tcon); */
846 	if (tcon->ses && tcon->ses->server) {
847 		cifs_dbg(FYI, "wake up tasks now - umount begin not complete\n");
848 		wake_up_all(&tcon->ses->server->request_q);
849 		wake_up_all(&tcon->ses->server->response_q);
850 		msleep(1); /* yield */
851 		/* we have to kick the requests once more */
852 		wake_up_all(&tcon->ses->server->response_q);
853 		msleep(1);
854 	}
855 
856 	return;
857 }
858 
859 static int cifs_freeze(struct super_block *sb)
860 {
861 	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
862 	struct cifs_tcon *tcon;
863 
864 	if (cifs_sb == NULL)
865 		return 0;
866 
867 	tcon = cifs_sb_master_tcon(cifs_sb);
868 
869 	cifs_close_all_deferred_files(tcon);
870 	return 0;
871 }
872 
873 #ifdef CONFIG_CIFS_STATS2
874 static int cifs_show_stats(struct seq_file *s, struct dentry *root)
875 {
876 	/* BB FIXME */
877 	return 0;
878 }
879 #endif
880 
881 static int cifs_write_inode(struct inode *inode, struct writeback_control *wbc)
882 {
883 	return netfs_unpin_writeback(inode, wbc);
884 }
885 
886 static int cifs_drop_inode(struct inode *inode)
887 {
888 	unsigned int sbflags = cifs_sb_flags(CIFS_SB(inode));
889 
890 	/* no serverino => unconditional eviction */
891 	return !(sbflags & CIFS_MOUNT_SERVER_INUM) ||
892 		inode_generic_drop(inode);
893 }
894 
895 static const struct super_operations cifs_super_ops = {
896 	.statfs = cifs_statfs,
897 	.alloc_inode = cifs_alloc_inode,
898 	.write_inode	= cifs_write_inode,
899 	.free_inode = cifs_free_inode,
900 	.drop_inode	= cifs_drop_inode,
901 	.evict_inode	= cifs_evict_inode,
902 /*	.show_path	= cifs_show_path, */ /* Would we ever need show path? */
903 	.show_devname   = cifs_show_devname,
904 /*	.delete_inode	= cifs_delete_inode,  */  /* Do not need above
905 	function unless later we add lazy close of inodes or unless the
906 	kernel forgets to call us with the same number of releases (closes)
907 	as opens */
908 	.show_options = cifs_show_options,
909 	.umount_begin   = cifs_umount_begin,
910 	.freeze_fs      = cifs_freeze,
911 #ifdef CONFIG_CIFS_STATS2
912 	.show_stats = cifs_show_stats,
913 #endif
914 };
915 
916 /*
917  * Get root dentry from superblock according to prefix path mount option.
918  * Return dentry with refcount + 1 on success and NULL otherwise.
919  */
920 static struct dentry *
921 cifs_get_root(struct smb3_fs_context *ctx, struct super_block *sb)
922 {
923 	struct dentry *dentry;
924 	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
925 	char *full_path = NULL;
926 	char *s, *p;
927 	char sep;
928 
929 	if (cifs_sb_flags(cifs_sb) & CIFS_MOUNT_USE_PREFIX_PATH)
930 		return dget(sb->s_root);
931 
932 	full_path = cifs_build_path_to_root(ctx, cifs_sb,
933 				cifs_sb_master_tcon(cifs_sb), 0);
934 	if (full_path == NULL)
935 		return ERR_PTR(-ENOMEM);
936 
937 	cifs_dbg(FYI, "Get root dentry for %s\n", full_path);
938 
939 	sep = CIFS_DIR_SEP(cifs_sb);
940 	dentry = dget(sb->s_root);
941 	s = full_path;
942 
943 	do {
944 		struct inode *dir = d_inode(dentry);
945 		struct dentry *child;
946 
947 		if (!S_ISDIR(dir->i_mode)) {
948 			dput(dentry);
949 			dentry = ERR_PTR(-ENOTDIR);
950 			break;
951 		}
952 
953 		/* skip separators */
954 		while (*s == sep)
955 			s++;
956 		if (!*s)
957 			break;
958 		p = s++;
959 		/* next separator */
960 		while (*s && *s != sep)
961 			s++;
962 
963 		child = lookup_noperm_positive_unlocked(&QSTR_LEN(p, s - p),
964 							dentry);
965 		dput(dentry);
966 		dentry = child;
967 	} while (!IS_ERR(dentry));
968 	kfree(full_path);
969 	return dentry;
970 }
971 
972 static int cifs_set_super(struct super_block *sb, void *data)
973 {
974 	struct cifs_mnt_data *mnt_data = data;
975 	sb->s_fs_info = mnt_data->cifs_sb;
976 	return set_anon_super(sb, NULL);
977 }
978 
979 struct dentry *
980 cifs_smb3_do_mount(struct file_system_type *fs_type,
981 	      int flags, struct smb3_fs_context *old_ctx)
982 {
983 	struct cifs_mnt_data mnt_data;
984 	struct cifs_sb_info *cifs_sb;
985 	struct super_block *sb;
986 	struct dentry *root;
987 	int rc;
988 
989 	if (cifsFYI) {
990 		cifs_dbg(FYI, "%s: devname=%s flags=0x%x\n", __func__,
991 			 old_ctx->source, flags);
992 	} else {
993 		cifs_info("Attempting to mount %s\n", old_ctx->source);
994 	}
995 	cifs_sb = kzalloc_obj(*cifs_sb);
996 	if (!cifs_sb)
997 		return ERR_PTR(-ENOMEM);
998 
999 	cifs_sb->ctx = kzalloc_obj(struct smb3_fs_context);
1000 	if (!cifs_sb->ctx) {
1001 		root = ERR_PTR(-ENOMEM);
1002 		goto out;
1003 	}
1004 	rc = smb3_fs_context_dup(cifs_sb->ctx, old_ctx);
1005 	if (rc) {
1006 		root = ERR_PTR(rc);
1007 		goto out;
1008 	}
1009 
1010 	rc = cifs_setup_cifs_sb(cifs_sb);
1011 	if (rc) {
1012 		root = ERR_PTR(rc);
1013 		goto out;
1014 	}
1015 
1016 	rc = cifs_mount(cifs_sb, cifs_sb->ctx);
1017 	if (rc) {
1018 		if (!(flags & SB_SILENT))
1019 			cifs_dbg(VFS, "cifs_mount failed w/return code = %d\n",
1020 				 rc);
1021 		root = ERR_PTR(rc);
1022 		goto out;
1023 	}
1024 
1025 	mnt_data.ctx = cifs_sb->ctx;
1026 	mnt_data.cifs_sb = cifs_sb;
1027 	mnt_data.flags = flags;
1028 
1029 	/* BB should we make this contingent on mount parm? */
1030 	flags |= SB_NODIRATIME | SB_NOATIME;
1031 
1032 	sb = sget(fs_type, cifs_match_super, cifs_set_super, flags, &mnt_data);
1033 	if (IS_ERR(sb)) {
1034 		cifs_umount(cifs_sb);
1035 		return ERR_CAST(sb);
1036 	}
1037 
1038 	if (sb->s_root) {
1039 		cifs_dbg(FYI, "Use existing superblock\n");
1040 		cifs_umount(cifs_sb);
1041 		cifs_sb = NULL;
1042 	} else {
1043 		rc = cifs_read_super(sb);
1044 		if (rc) {
1045 			root = ERR_PTR(rc);
1046 			goto out_super;
1047 		}
1048 
1049 		sb->s_flags |= SB_ACTIVE;
1050 	}
1051 
1052 	root = cifs_get_root(cifs_sb ? cifs_sb->ctx : old_ctx, sb);
1053 	if (IS_ERR(root))
1054 		goto out_super;
1055 
1056 	if (cifs_sb)
1057 		cifs_sb->root = dget(root);
1058 
1059 	cifs_dbg(FYI, "dentry root is: %p\n", root);
1060 	return root;
1061 
1062 out_super:
1063 	deactivate_locked_super(sb);
1064 	return root;
1065 out:
1066 	kfree(cifs_sb->prepath);
1067 	smb3_cleanup_fs_context(cifs_sb->ctx);
1068 	kfree(cifs_sb);
1069 	return root;
1070 }
1071 
1072 static loff_t cifs_llseek(struct file *file, loff_t offset, int whence)
1073 {
1074 	struct cifsFileInfo *cfile = file->private_data;
1075 	struct cifs_tcon *tcon;
1076 
1077 	/*
1078 	 * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
1079 	 * the cached file length
1080 	 */
1081 	if (whence != SEEK_SET && whence != SEEK_CUR) {
1082 		int rc;
1083 		struct inode *inode = file_inode(file);
1084 
1085 		/*
1086 		 * We need to be sure that all dirty pages are written and the
1087 		 * server has the newest file length.
1088 		 */
1089 		if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
1090 		    inode->i_mapping->nrpages != 0) {
1091 			rc = filemap_fdatawait(inode->i_mapping);
1092 			if (rc) {
1093 				mapping_set_error(inode->i_mapping, rc);
1094 				return rc;
1095 			}
1096 		}
1097 		/*
1098 		 * Some applications poll for the file length in this strange
1099 		 * way so we must seek to end on non-oplocked files by
1100 		 * setting the revalidate time to zero.
1101 		 */
1102 		CIFS_I(inode)->time = 0;
1103 
1104 		rc = cifs_revalidate_file_attr(file);
1105 		if (rc < 0)
1106 			return (loff_t)rc;
1107 	}
1108 	if (cfile && cfile->tlink) {
1109 		tcon = tlink_tcon(cfile->tlink);
1110 		if (tcon->ses->server->ops->llseek)
1111 			return tcon->ses->server->ops->llseek(file, tcon,
1112 							      offset, whence);
1113 	}
1114 	return generic_file_llseek(file, offset, whence);
1115 }
1116 
1117 static int
1118 cifs_setlease(struct file *file, int arg, struct file_lease **lease, void **priv)
1119 {
1120 	/*
1121 	 * Note that this is called by vfs setlease with i_lock held to
1122 	 * protect *lease from going away.
1123 	 */
1124 	struct inode *inode = file_inode(file);
1125 	struct cifsFileInfo *cfile = file->private_data;
1126 
1127 	/* Check if file is oplocked if this is request for new lease */
1128 	if (arg == F_UNLCK ||
1129 	    ((arg == F_RDLCK) && CIFS_CACHE_READ(CIFS_I(inode))) ||
1130 	    ((arg == F_WRLCK) && CIFS_CACHE_WRITE(CIFS_I(inode))))
1131 		return generic_setlease(file, arg, lease, priv);
1132 	else if (tlink_tcon(cfile->tlink)->local_lease &&
1133 		 !CIFS_CACHE_READ(CIFS_I(inode)))
1134 		/*
1135 		 * If the server claims to support oplock on this file, then we
1136 		 * still need to check oplock even if the local_lease mount
1137 		 * option is set, but there are servers which do not support
1138 		 * oplock for which this mount option may be useful if the user
1139 		 * knows that the file won't be changed on the server by anyone
1140 		 * else.
1141 		 */
1142 		return generic_setlease(file, arg, lease, priv);
1143 	else
1144 		return -EAGAIN;
1145 }
1146 
1147 struct file_system_type cifs_fs_type = {
1148 	.owner = THIS_MODULE,
1149 	.name = "cifs",
1150 	.init_fs_context = smb3_init_fs_context,
1151 	.parameters = smb3_fs_parameters,
1152 	.kill_sb = cifs_kill_sb,
1153 	.fs_flags = FS_RENAME_DOES_D_MOVE,
1154 };
1155 MODULE_ALIAS_FS("cifs");
1156 
1157 struct file_system_type smb3_fs_type = {
1158 	.owner = THIS_MODULE,
1159 	.name = "smb3",
1160 	.init_fs_context = smb3_init_fs_context,
1161 	.parameters = smb3_fs_parameters,
1162 	.kill_sb = cifs_kill_sb,
1163 	.fs_flags = FS_RENAME_DOES_D_MOVE,
1164 };
1165 MODULE_ALIAS_FS("smb3");
1166 MODULE_ALIAS("smb3");
1167 
1168 const struct inode_operations cifs_dir_inode_ops = {
1169 	.create = cifs_create,
1170 	.atomic_open = cifs_atomic_open,
1171 	.tmpfile = cifs_tmpfile,
1172 	.lookup = cifs_lookup,
1173 	.getattr = cifs_getattr,
1174 	.unlink = cifs_unlink,
1175 	.link = cifs_hardlink,
1176 	.mkdir = cifs_mkdir,
1177 	.rmdir = cifs_rmdir,
1178 	.rename = cifs_rename2,
1179 	.permission = cifs_permission,
1180 	.setattr = cifs_setattr,
1181 	.symlink = cifs_symlink,
1182 	.mknod   = cifs_mknod,
1183 	.listxattr = cifs_listxattr,
1184 	.get_acl = cifs_get_acl,
1185 	.set_acl = cifs_set_acl,
1186 };
1187 
1188 const struct inode_operations cifs_file_inode_ops = {
1189 	.setattr = cifs_setattr,
1190 	.getattr = cifs_getattr,
1191 	.permission = cifs_permission,
1192 	.listxattr = cifs_listxattr,
1193 	.fiemap = cifs_fiemap,
1194 	.get_acl = cifs_get_acl,
1195 	.set_acl = cifs_set_acl,
1196 };
1197 
1198 const char *cifs_get_link(struct dentry *dentry, struct inode *inode,
1199 			    struct delayed_call *done)
1200 {
1201 	char *target_path;
1202 
1203 	if (!dentry)
1204 		return ERR_PTR(-ECHILD);
1205 
1206 	target_path = kmalloc(PATH_MAX, GFP_KERNEL);
1207 	if (!target_path)
1208 		return ERR_PTR(-ENOMEM);
1209 
1210 	spin_lock(&inode->i_lock);
1211 	if (likely(CIFS_I(inode)->symlink_target)) {
1212 		strscpy(target_path, CIFS_I(inode)->symlink_target, PATH_MAX);
1213 	} else {
1214 		kfree(target_path);
1215 		target_path = ERR_PTR(-EOPNOTSUPP);
1216 	}
1217 	spin_unlock(&inode->i_lock);
1218 
1219 	if (!IS_ERR(target_path))
1220 		set_delayed_call(done, kfree_link, target_path);
1221 
1222 	return target_path;
1223 }
1224 
1225 const struct inode_operations cifs_symlink_inode_ops = {
1226 	.get_link = cifs_get_link,
1227 	.setattr = cifs_setattr,
1228 	.permission = cifs_permission,
1229 	.listxattr = cifs_listxattr,
1230 };
1231 
1232 /*
1233  * Advance the EOF marker to after the source range.
1234  */
1235 static int cifs_precopy_set_eof(struct inode *src_inode, struct cifsInodeInfo *src_cifsi,
1236 				struct cifs_tcon *src_tcon,
1237 				unsigned int xid, loff_t src_end)
1238 {
1239 	struct cifsFileInfo *writeable_srcfile;
1240 	int rc = -EINVAL;
1241 
1242 	writeable_srcfile = find_writable_file(src_cifsi, FIND_FSUID_ONLY);
1243 	if (writeable_srcfile) {
1244 		if (src_tcon->ses->server->ops->set_file_size)
1245 			rc = src_tcon->ses->server->ops->set_file_size(
1246 				xid, src_tcon, writeable_srcfile,
1247 				src_inode->i_size, true /* no need to set sparse */);
1248 		else
1249 			rc = -ENOSYS;
1250 		cifsFileInfo_put(writeable_srcfile);
1251 		cifs_dbg(FYI, "SetFSize for copychunk rc = %d\n", rc);
1252 	}
1253 
1254 	if (rc < 0)
1255 		goto set_failed;
1256 
1257 	netfs_resize_file(&src_cifsi->netfs, src_end, true);
1258 	fscache_resize_cookie(cifs_inode_cookie(src_inode), src_end);
1259 	return 0;
1260 
1261 set_failed:
1262 	return filemap_write_and_wait(src_inode->i_mapping);
1263 }
1264 
1265 /*
1266  * Flush out either the folio that overlaps the beginning of a range in which
1267  * pos resides or the folio that overlaps the end of a range unless that folio
1268  * is entirely within the range we're going to invalidate.  We extend the flush
1269  * bounds to encompass the folio.
1270  */
1271 static int cifs_flush_folio(struct inode *inode, loff_t pos, loff_t *_fstart, loff_t *_fend,
1272 			    bool first)
1273 {
1274 	struct folio *folio;
1275 	unsigned long long fpos, fend;
1276 	pgoff_t index = pos / PAGE_SIZE;
1277 	size_t size;
1278 	int rc = 0;
1279 
1280 	folio = filemap_get_folio(inode->i_mapping, index);
1281 	if (IS_ERR(folio))
1282 		return 0;
1283 
1284 	size = folio_size(folio);
1285 	fpos = folio_pos(folio);
1286 	fend = fpos + size - 1;
1287 	*_fstart = min_t(unsigned long long, *_fstart, fpos);
1288 	*_fend   = max_t(unsigned long long, *_fend, fend);
1289 	if ((first && pos == fpos) || (!first && pos == fend))
1290 		goto out;
1291 
1292 	rc = filemap_write_and_wait_range(inode->i_mapping, fpos, fend);
1293 out:
1294 	folio_put(folio);
1295 	return rc;
1296 }
1297 
1298 static loff_t cifs_remap_file_range(struct file *src_file, loff_t off,
1299 		struct file *dst_file, loff_t destoff, loff_t len,
1300 		unsigned int remap_flags)
1301 {
1302 	struct inode *src_inode = file_inode(src_file);
1303 	struct inode *target_inode = file_inode(dst_file);
1304 	struct cifsInodeInfo *src_cifsi = CIFS_I(src_inode);
1305 	struct cifsInodeInfo *target_cifsi = CIFS_I(target_inode);
1306 	struct cifsFileInfo *smb_file_src = src_file->private_data;
1307 	struct cifsFileInfo *smb_file_target = dst_file->private_data;
1308 	struct cifs_tcon *target_tcon, *src_tcon;
1309 	unsigned long long i_size, new_size;
1310 	unsigned long long destend, fstart, fend;
1311 	unsigned int xid;
1312 	int rc;
1313 
1314 	if (remap_flags & REMAP_FILE_DEDUP)
1315 		return -EOPNOTSUPP;
1316 	if (remap_flags & ~REMAP_FILE_ADVISORY)
1317 		return -EINVAL;
1318 
1319 	cifs_dbg(FYI, "clone range\n");
1320 
1321 	xid = get_xid();
1322 
1323 	if (!smb_file_src || !smb_file_target) {
1324 		rc = -EBADF;
1325 		cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
1326 		goto out;
1327 	}
1328 
1329 	src_tcon = tlink_tcon(smb_file_src->tlink);
1330 	target_tcon = tlink_tcon(smb_file_target->tlink);
1331 
1332 	/*
1333 	 * Note: cifs case is easier than btrfs since server responsible for
1334 	 * checks for proper open modes and file type and if it wants
1335 	 * server could even support copy of range where source = target
1336 	 */
1337 	lock_two_nondirectories(target_inode, src_inode);
1338 
1339 	if (len == 0)
1340 		len = src_inode->i_size - off;
1341 
1342 	cifs_dbg(FYI, "clone range\n");
1343 
1344 	/* Flush the source buffer */
1345 	rc = filemap_write_and_wait_range(src_inode->i_mapping, off,
1346 					  off + len - 1);
1347 	if (rc)
1348 		goto unlock;
1349 
1350 	/* The server-side copy will fail if the source crosses the EOF marker.
1351 	 * Advance the EOF marker after the flush above to the end of the range
1352 	 * if it's short of that.
1353 	 */
1354 	if (netfs_read_remote_i_size(src_inode) < off + len) {
1355 		rc = cifs_precopy_set_eof(src_inode, src_cifsi, src_tcon, xid, off + len);
1356 		if (rc < 0)
1357 			goto unlock;
1358 	}
1359 
1360 	new_size = destoff + len;
1361 	destend = destoff + len - 1;
1362 
1363 	/* Flush the folios at either end of the destination range to prevent
1364 	 * accidental loss of dirty data outside of the range.
1365 	 */
1366 	fstart = destoff;
1367 	fend = destend;
1368 
1369 	rc = cifs_flush_folio(target_inode, destoff, &fstart, &fend, true);
1370 	if (rc)
1371 		goto unlock;
1372 	rc = cifs_flush_folio(target_inode, destend, &fstart, &fend, false);
1373 	if (rc)
1374 		goto unlock;
1375 
1376 	spin_lock(&target_inode->i_lock);
1377 	if (fend > target_cifsi->netfs._zero_point)
1378 		netfs_write_zero_point(target_inode, fend + 1);
1379 	i_size = target_inode->i_size;
1380 	spin_unlock(&target_inode->i_lock);
1381 
1382 	/* Discard all the folios that overlap the destination region. */
1383 	cifs_dbg(FYI, "about to discard pages %llx-%llx\n", fstart, fend);
1384 	truncate_inode_pages_range(&target_inode->i_data, fstart, fend);
1385 
1386 	fscache_invalidate(cifs_inode_cookie(target_inode), NULL, i_size, 0);
1387 
1388 	rc = -EOPNOTSUPP;
1389 	if (target_tcon->ses->server->ops->duplicate_extents) {
1390 		rc = target_tcon->ses->server->ops->duplicate_extents(xid,
1391 			smb_file_src, smb_file_target, off, len, destoff);
1392 		if (rc == 0 && new_size > i_size) {
1393 			truncate_setsize(target_inode, new_size);
1394 			fscache_resize_cookie(cifs_inode_cookie(target_inode),
1395 					      new_size);
1396 		} else if (rc == -EOPNOTSUPP) {
1397 			/*
1398 			 * copy_file_range syscall man page indicates EINVAL
1399 			 * is returned e.g when "fd_in and fd_out refer to the
1400 			 * same file and the source and target ranges overlap."
1401 			 * Test generic/157 was what showed these cases where
1402 			 * we need to remap EOPNOTSUPP to EINVAL
1403 			 */
1404 			if (off >= src_inode->i_size) {
1405 				rc = -EINVAL;
1406 			} else if (src_inode == target_inode) {
1407 				if (off + len > destoff)
1408 					rc = -EINVAL;
1409 			}
1410 		}
1411 		if (rc == 0) {
1412 			spin_lock(&target_inode->i_lock);
1413 			if (new_size > target_cifsi->netfs._zero_point)
1414 				netfs_write_zero_point(target_inode, new_size);
1415 			spin_unlock(&target_inode->i_lock);
1416 		}
1417 	}
1418 
1419 	/* force revalidate of size and timestamps of target file now
1420 	   that target is updated on the server */
1421 	CIFS_I(target_inode)->time = 0;
1422 unlock:
1423 	/* although unlocking in the reverse order from locking is not
1424 	   strictly necessary here it is a little cleaner to be consistent */
1425 	unlock_two_nondirectories(src_inode, target_inode);
1426 out:
1427 	free_xid(xid);
1428 	return rc < 0 ? rc : len;
1429 }
1430 
1431 ssize_t cifs_file_copychunk_range(unsigned int xid,
1432 				struct file *src_file, loff_t off,
1433 				struct file *dst_file, loff_t destoff,
1434 				size_t len, unsigned int flags)
1435 {
1436 	struct inode *src_inode = file_inode(src_file);
1437 	struct inode *target_inode = file_inode(dst_file);
1438 	struct cifsInodeInfo *src_cifsi = CIFS_I(src_inode);
1439 	struct cifsInodeInfo *target_cifsi = CIFS_I(target_inode);
1440 	struct cifsFileInfo *smb_file_src;
1441 	struct cifsFileInfo *smb_file_target;
1442 	struct cifs_tcon *src_tcon;
1443 	struct cifs_tcon *target_tcon;
1444 	ssize_t rc;
1445 
1446 	cifs_dbg(FYI, "copychunk range\n");
1447 
1448 	if (!src_file->private_data || !dst_file->private_data) {
1449 		rc = -EBADF;
1450 		cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
1451 		goto out;
1452 	}
1453 
1454 	rc = -EXDEV;
1455 	smb_file_target = dst_file->private_data;
1456 	smb_file_src = src_file->private_data;
1457 	src_tcon = tlink_tcon(smb_file_src->tlink);
1458 	target_tcon = tlink_tcon(smb_file_target->tlink);
1459 
1460 	if (src_tcon->ses != target_tcon->ses) {
1461 		cifs_dbg(FYI, "source and target of copy not on same server\n");
1462 		goto out;
1463 	}
1464 
1465 	rc = -EOPNOTSUPP;
1466 	if (!target_tcon->ses->server->ops->copychunk_range)
1467 		goto out;
1468 
1469 	/*
1470 	 * Note: cifs case is easier than btrfs since server responsible for
1471 	 * checks for proper open modes and file type and if it wants
1472 	 * server could even support copy of range where source = target
1473 	 */
1474 	lock_two_nondirectories(target_inode, src_inode);
1475 
1476 	cifs_dbg(FYI, "about to flush pages\n");
1477 
1478 	rc = filemap_write_and_wait_range(src_inode->i_mapping, off,
1479 					  off + len - 1);
1480 	if (rc)
1481 		goto unlock;
1482 
1483 	/* The server-side copy will fail if the source crosses the EOF marker.
1484 	 * Advance the EOF marker after the flush above to the end of the range
1485 	 * if it's short of that.
1486 	 */
1487 	if (netfs_read_remote_i_size(src_inode) < off + len) {
1488 		rc = cifs_precopy_set_eof(src_inode, src_cifsi, src_tcon, xid, off + len);
1489 		if (rc < 0)
1490 			goto unlock;
1491 	}
1492 
1493 	/* Flush and invalidate all the folios in the destination region.  If
1494 	 * the copy was successful, then some of the flush is extra overhead,
1495 	 * but we need to allow for the copy failing in some way (eg. ENOSPC).
1496 	 */
1497 	rc = filemap_invalidate_inode(target_inode, true, destoff, destoff + len - 1);
1498 	if (rc)
1499 		goto unlock;
1500 
1501 	fscache_invalidate(cifs_inode_cookie(target_inode), NULL,
1502 			   i_size_read(target_inode), 0);
1503 
1504 	rc = file_modified(dst_file);
1505 	if (!rc) {
1506 		rc = target_tcon->ses->server->ops->copychunk_range(xid,
1507 			smb_file_src, smb_file_target, off, len, destoff);
1508 		if (rc > 0 && destoff + rc > i_size_read(target_inode)) {
1509 			truncate_setsize(target_inode, destoff + rc);
1510 			netfs_resize_file(&target_cifsi->netfs,
1511 					  i_size_read(target_inode), true);
1512 			fscache_resize_cookie(cifs_inode_cookie(target_inode),
1513 					      i_size_read(target_inode));
1514 		}
1515 		if (rc > 0) {
1516 			spin_lock(&target_inode->i_lock);
1517 			if (destoff + rc > target_cifsi->netfs._zero_point)
1518 				netfs_write_zero_point(target_inode, destoff + rc);
1519 			spin_unlock(&target_inode->i_lock);
1520 		}
1521 	}
1522 
1523 	file_accessed(src_file);
1524 
1525 	/* force revalidate of size and timestamps of target file now
1526 	 * that target is updated on the server
1527 	 */
1528 	CIFS_I(target_inode)->time = 0;
1529 
1530 unlock:
1531 	/* although unlocking in the reverse order from locking is not
1532 	 * strictly necessary here it is a little cleaner to be consistent
1533 	 */
1534 	unlock_two_nondirectories(src_inode, target_inode);
1535 
1536 out:
1537 	return rc;
1538 }
1539 
1540 /*
1541  * Directory operations under CIFS/SMB2/SMB3 are synchronous, so fsync()
1542  * is a dummy operation.
1543  */
1544 static int cifs_dir_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1545 {
1546 	cifs_dbg(FYI, "Sync directory - name: %pD datasync: 0x%x\n",
1547 		 file, datasync);
1548 
1549 	return 0;
1550 }
1551 
1552 static ssize_t cifs_copy_file_range(struct file *src_file, loff_t off,
1553 				struct file *dst_file, loff_t destoff,
1554 				size_t len, unsigned int flags)
1555 {
1556 	unsigned int xid = get_xid();
1557 	ssize_t rc;
1558 	struct cifsFileInfo *cfile = dst_file->private_data;
1559 
1560 	if (cfile->swapfile) {
1561 		rc = -EOPNOTSUPP;
1562 		free_xid(xid);
1563 		return rc;
1564 	}
1565 
1566 	rc = cifs_file_copychunk_range(xid, src_file, off, dst_file, destoff,
1567 					len, flags);
1568 	free_xid(xid);
1569 
1570 	if (rc == -EOPNOTSUPP || rc == -EXDEV)
1571 		rc = splice_copy_file_range(src_file, off, dst_file,
1572 					    destoff, len);
1573 	return rc;
1574 }
1575 
1576 const struct file_operations cifs_file_ops = {
1577 	.read_iter = cifs_loose_read_iter,
1578 	.write_iter = cifs_file_write_iter,
1579 	.open = cifs_open,
1580 	.release = cifs_close,
1581 	.lock = cifs_lock,
1582 	.flock = cifs_flock,
1583 	.fsync = cifs_fsync,
1584 	.flush = cifs_flush,
1585 	.mmap_prepare = cifs_file_mmap_prepare,
1586 	.splice_read = filemap_splice_read,
1587 	.splice_write = iter_file_splice_write,
1588 	.llseek = cifs_llseek,
1589 	.unlocked_ioctl	= cifs_ioctl,
1590 	.copy_file_range = cifs_copy_file_range,
1591 	.remap_file_range = cifs_remap_file_range,
1592 	.setlease = cifs_setlease,
1593 	.fallocate = cifs_fallocate,
1594 };
1595 
1596 const struct file_operations cifs_file_strict_ops = {
1597 	.read_iter = cifs_strict_readv,
1598 	.write_iter = cifs_strict_writev,
1599 	.open = cifs_open,
1600 	.release = cifs_close,
1601 	.lock = cifs_lock,
1602 	.flock = cifs_flock,
1603 	.fsync = cifs_strict_fsync,
1604 	.flush = cifs_flush,
1605 	.mmap_prepare = cifs_file_strict_mmap_prepare,
1606 	.splice_read = filemap_splice_read,
1607 	.splice_write = iter_file_splice_write,
1608 	.llseek = cifs_llseek,
1609 	.unlocked_ioctl	= cifs_ioctl,
1610 	.copy_file_range = cifs_copy_file_range,
1611 	.remap_file_range = cifs_remap_file_range,
1612 	.setlease = cifs_setlease,
1613 	.fallocate = cifs_fallocate,
1614 };
1615 
1616 const struct file_operations cifs_file_direct_ops = {
1617 	.read_iter = netfs_unbuffered_read_iter,
1618 	.write_iter = netfs_file_write_iter,
1619 	.open = cifs_open,
1620 	.release = cifs_close,
1621 	.lock = cifs_lock,
1622 	.flock = cifs_flock,
1623 	.fsync = cifs_fsync,
1624 	.flush = cifs_flush,
1625 	.mmap_prepare = cifs_file_mmap_prepare,
1626 	.splice_read = copy_splice_read,
1627 	.splice_write = iter_file_splice_write,
1628 	.unlocked_ioctl  = cifs_ioctl,
1629 	.copy_file_range = cifs_copy_file_range,
1630 	.remap_file_range = cifs_remap_file_range,
1631 	.llseek = cifs_llseek,
1632 	.setlease = cifs_setlease,
1633 	.fallocate = cifs_fallocate,
1634 };
1635 
1636 const struct file_operations cifs_file_nobrl_ops = {
1637 	.read_iter = cifs_loose_read_iter,
1638 	.write_iter = cifs_file_write_iter,
1639 	.open = cifs_open,
1640 	.release = cifs_close,
1641 	.fsync = cifs_fsync,
1642 	.flush = cifs_flush,
1643 	.mmap_prepare = cifs_file_mmap_prepare,
1644 	.splice_read = filemap_splice_read,
1645 	.splice_write = iter_file_splice_write,
1646 	.llseek = cifs_llseek,
1647 	.unlocked_ioctl	= cifs_ioctl,
1648 	.copy_file_range = cifs_copy_file_range,
1649 	.remap_file_range = cifs_remap_file_range,
1650 	.setlease = cifs_setlease,
1651 	.fallocate = cifs_fallocate,
1652 };
1653 
1654 const struct file_operations cifs_file_strict_nobrl_ops = {
1655 	.read_iter = cifs_strict_readv,
1656 	.write_iter = cifs_strict_writev,
1657 	.open = cifs_open,
1658 	.release = cifs_close,
1659 	.fsync = cifs_strict_fsync,
1660 	.flush = cifs_flush,
1661 	.mmap_prepare = cifs_file_strict_mmap_prepare,
1662 	.splice_read = filemap_splice_read,
1663 	.splice_write = iter_file_splice_write,
1664 	.llseek = cifs_llseek,
1665 	.unlocked_ioctl	= cifs_ioctl,
1666 	.copy_file_range = cifs_copy_file_range,
1667 	.remap_file_range = cifs_remap_file_range,
1668 	.setlease = cifs_setlease,
1669 	.fallocate = cifs_fallocate,
1670 };
1671 
1672 const struct file_operations cifs_file_direct_nobrl_ops = {
1673 	.read_iter = netfs_unbuffered_read_iter,
1674 	.write_iter = netfs_file_write_iter,
1675 	.open = cifs_open,
1676 	.release = cifs_close,
1677 	.fsync = cifs_fsync,
1678 	.flush = cifs_flush,
1679 	.mmap_prepare = cifs_file_mmap_prepare,
1680 	.splice_read = copy_splice_read,
1681 	.splice_write = iter_file_splice_write,
1682 	.unlocked_ioctl  = cifs_ioctl,
1683 	.copy_file_range = cifs_copy_file_range,
1684 	.remap_file_range = cifs_remap_file_range,
1685 	.llseek = cifs_llseek,
1686 	.setlease = cifs_setlease,
1687 	.fallocate = cifs_fallocate,
1688 };
1689 
1690 const struct file_operations cifs_dir_ops = {
1691 	.iterate_shared = cifs_readdir,
1692 	.release = cifs_closedir,
1693 	.read    = generic_read_dir,
1694 	.unlocked_ioctl  = cifs_ioctl,
1695 	.copy_file_range = cifs_copy_file_range,
1696 	.remap_file_range = cifs_remap_file_range,
1697 	.llseek = generic_file_llseek,
1698 	.fsync = cifs_dir_fsync,
1699 };
1700 
1701 static void
1702 cifs_init_once(void *inode)
1703 {
1704 	struct cifsInodeInfo *cifsi = inode;
1705 
1706 	inode_init_once(&cifsi->netfs.inode);
1707 	init_rwsem(&cifsi->lock_sem);
1708 }
1709 
1710 static int __init
1711 cifs_init_inodecache(void)
1712 {
1713 	cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
1714 					      sizeof(struct cifsInodeInfo),
1715 					      0, (SLAB_RECLAIM_ACCOUNT|
1716 						SLAB_ACCOUNT),
1717 					      cifs_init_once);
1718 	if (cifs_inode_cachep == NULL)
1719 		return -ENOMEM;
1720 
1721 	return 0;
1722 }
1723 
1724 static void
1725 cifs_destroy_inodecache(void)
1726 {
1727 	/*
1728 	 * Make sure all delayed rcu free inodes are flushed before we
1729 	 * destroy cache.
1730 	 */
1731 	rcu_barrier();
1732 	kmem_cache_destroy(cifs_inode_cachep);
1733 }
1734 
1735 static int
1736 cifs_init_request_bufs(void)
1737 {
1738 	/*
1739 	 * SMB2 maximum header size is bigger than CIFS one - no problems to
1740 	 * allocate some more bytes for CIFS.
1741 	 */
1742 	size_t max_hdr_size = MAX_SMB2_HDR_SIZE;
1743 
1744 	if (CIFSMaxBufSize < 8192) {
1745 	/* Buffer size can not be smaller than 2 * PATH_MAX since maximum
1746 	Unicode path name has to fit in any SMB/CIFS path based frames */
1747 		CIFSMaxBufSize = 8192;
1748 	} else if (CIFSMaxBufSize > 1024*127) {
1749 		CIFSMaxBufSize = 1024 * 127;
1750 	} else {
1751 		CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
1752 	}
1753 /*
1754 	cifs_dbg(VFS, "CIFSMaxBufSize %d 0x%x\n",
1755 		 CIFSMaxBufSize, CIFSMaxBufSize);
1756 */
1757 	cifs_req_cachep = kmem_cache_create_usercopy("cifs_request",
1758 					    CIFSMaxBufSize + max_hdr_size, 0,
1759 					    SLAB_HWCACHE_ALIGN, 0,
1760 					    CIFSMaxBufSize + max_hdr_size,
1761 					    NULL);
1762 	if (cifs_req_cachep == NULL)
1763 		return -ENOMEM;
1764 
1765 	if (cifs_min_rcv < 1)
1766 		cifs_min_rcv = 1;
1767 	else if (cifs_min_rcv > 64) {
1768 		cifs_min_rcv = 64;
1769 		cifs_dbg(VFS, "cifs_min_rcv set to maximum (64)\n");
1770 	}
1771 
1772 	cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
1773 						  cifs_req_cachep);
1774 
1775 	if (cifs_req_poolp == NULL) {
1776 		kmem_cache_destroy(cifs_req_cachep);
1777 		return -ENOMEM;
1778 	}
1779 	/* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
1780 	almost all handle based requests (but not write response, nor is it
1781 	sufficient for path based requests).  A smaller size would have
1782 	been more efficient (compacting multiple slab items on one 4k page)
1783 	for the case in which debug was on, but this larger size allows
1784 	more SMBs to use small buffer alloc and is still much more
1785 	efficient to alloc 1 per page off the slab compared to 17K (5page)
1786 	alloc of large cifs buffers even when page debugging is on */
1787 	cifs_sm_req_cachep = kmem_cache_create_usercopy("cifs_small_rq",
1788 			MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
1789 			0, MAX_CIFS_SMALL_BUFFER_SIZE, NULL);
1790 	if (cifs_sm_req_cachep == NULL) {
1791 		mempool_destroy(cifs_req_poolp);
1792 		kmem_cache_destroy(cifs_req_cachep);
1793 		return -ENOMEM;
1794 	}
1795 
1796 	if (cifs_min_small < 2)
1797 		cifs_min_small = 2;
1798 	else if (cifs_min_small > 256) {
1799 		cifs_min_small = 256;
1800 		cifs_dbg(FYI, "cifs_min_small set to maximum (256)\n");
1801 	}
1802 
1803 	cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
1804 						     cifs_sm_req_cachep);
1805 
1806 	if (cifs_sm_req_poolp == NULL) {
1807 		mempool_destroy(cifs_req_poolp);
1808 		kmem_cache_destroy(cifs_req_cachep);
1809 		kmem_cache_destroy(cifs_sm_req_cachep);
1810 		return -ENOMEM;
1811 	}
1812 
1813 	return 0;
1814 }
1815 
1816 static void
1817 cifs_destroy_request_bufs(void)
1818 {
1819 	mempool_destroy(cifs_req_poolp);
1820 	kmem_cache_destroy(cifs_req_cachep);
1821 	mempool_destroy(cifs_sm_req_poolp);
1822 	kmem_cache_destroy(cifs_sm_req_cachep);
1823 }
1824 
1825 static int init_mids(void)
1826 {
1827 	cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
1828 					    sizeof(struct mid_q_entry), 0,
1829 					    SLAB_HWCACHE_ALIGN, NULL);
1830 	if (cifs_mid_cachep == NULL)
1831 		return -ENOMEM;
1832 
1833 	/* 3 is a reasonable minimum number of simultaneous operations */
1834 	if (mempool_init_slab_pool(&cifs_mid_pool, 3, cifs_mid_cachep) < 0) {
1835 		kmem_cache_destroy(cifs_mid_cachep);
1836 		return -ENOMEM;
1837 	}
1838 
1839 	return 0;
1840 }
1841 
1842 static void destroy_mids(void)
1843 {
1844 	mempool_exit(&cifs_mid_pool);
1845 	kmem_cache_destroy(cifs_mid_cachep);
1846 }
1847 
1848 static int cifs_init_netfs(void)
1849 {
1850 	cifs_io_request_cachep =
1851 		kmem_cache_create("cifs_io_request",
1852 				  sizeof(struct cifs_io_request), 0,
1853 				  SLAB_HWCACHE_ALIGN, NULL);
1854 	if (!cifs_io_request_cachep)
1855 		goto nomem_req;
1856 
1857 	if (mempool_init_slab_pool(&cifs_io_request_pool, 100, cifs_io_request_cachep) < 0)
1858 		goto nomem_reqpool;
1859 
1860 	cifs_io_subrequest_cachep =
1861 		kmem_cache_create("cifs_io_subrequest",
1862 				  sizeof(struct cifs_io_subrequest), 0,
1863 				  SLAB_HWCACHE_ALIGN, NULL);
1864 	if (!cifs_io_subrequest_cachep)
1865 		goto nomem_subreq;
1866 
1867 	if (mempool_init_slab_pool(&cifs_io_subrequest_pool, 100, cifs_io_subrequest_cachep) < 0)
1868 		goto nomem_subreqpool;
1869 
1870 	return 0;
1871 
1872 nomem_subreqpool:
1873 	kmem_cache_destroy(cifs_io_subrequest_cachep);
1874 nomem_subreq:
1875 	mempool_exit(&cifs_io_request_pool);
1876 nomem_reqpool:
1877 	kmem_cache_destroy(cifs_io_request_cachep);
1878 nomem_req:
1879 	return -ENOMEM;
1880 }
1881 
1882 static void cifs_destroy_netfs(void)
1883 {
1884 	mempool_exit(&cifs_io_subrequest_pool);
1885 	kmem_cache_destroy(cifs_io_subrequest_cachep);
1886 	mempool_exit(&cifs_io_request_pool);
1887 	kmem_cache_destroy(cifs_io_request_cachep);
1888 }
1889 
1890 static int __init
1891 init_cifs(void)
1892 {
1893 	int rc = 0;
1894 
1895 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1896 	rc = smb1_init_maperror();
1897 	if (rc)
1898 		return rc;
1899 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1900 
1901 	rc = smb2_init_maperror();
1902 	if (rc)
1903 		return rc;
1904 
1905 	cifs_proc_init();
1906 	INIT_LIST_HEAD(&cifs_tcp_ses_list);
1907 /*
1908  *  Initialize Global counters
1909  */
1910 	atomic_set(&sesInfoAllocCount, 0);
1911 	atomic_set(&tconInfoAllocCount, 0);
1912 	atomic_set(&tcpSesNextId, 0);
1913 	atomic_set(&tcpSesAllocCount, 0);
1914 	atomic_set(&tcpSesReconnectCount, 0);
1915 	atomic_set(&tconInfoReconnectCount, 0);
1916 
1917 	atomic_set(&buf_alloc_count, 0);
1918 	atomic_set(&small_buf_alloc_count, 0);
1919 #ifdef CONFIG_CIFS_STATS2
1920 	atomic_set(&total_buf_alloc_count, 0);
1921 	atomic_set(&total_small_buf_alloc_count, 0);
1922 	if (slow_rsp_threshold < 1)
1923 		cifs_dbg(FYI, "slow_response_threshold msgs disabled\n");
1924 	else if (slow_rsp_threshold > 32767)
1925 		cifs_dbg(VFS,
1926 		       "slow response threshold set higher than recommended (0 to 32767)\n");
1927 #endif /* CONFIG_CIFS_STATS2 */
1928 
1929 	atomic_set(&mid_count, 0);
1930 	GlobalCurrentXid = 0;
1931 	GlobalTotalActiveXid = 0;
1932 	GlobalMaxActiveXid = 0;
1933 
1934 	cifs_lock_secret = get_random_u32();
1935 
1936 	if (cifs_max_pending < 2) {
1937 		cifs_max_pending = 2;
1938 		cifs_dbg(FYI, "cifs_max_pending set to min of 2\n");
1939 	} else if (cifs_max_pending > CIFS_MAX_REQ) {
1940 		cifs_max_pending = CIFS_MAX_REQ;
1941 		cifs_dbg(FYI, "cifs_max_pending set to max of %u\n",
1942 			 CIFS_MAX_REQ);
1943 	}
1944 
1945 	/* Limit max to about 18 hours, and setting to zero disables directory entry caching */
1946 	if (dir_cache_timeout > 65000) {
1947 		dir_cache_timeout = 65000;
1948 		cifs_dbg(VFS, "dir_cache_timeout set to max of 65000 seconds\n");
1949 	}
1950 
1951 	cifsiod_wq = alloc_workqueue("cifsiod",
1952 				     WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_PERCPU,
1953 				     0);
1954 	if (!cifsiod_wq) {
1955 		rc = -ENOMEM;
1956 		goto out_clean_proc;
1957 	}
1958 
1959 	/*
1960 	 * Consider in future setting limit!=0 maybe to min(num_of_cores - 1, 3)
1961 	 * so that we don't launch too many worker threads but
1962 	 * Documentation/core-api/workqueue.rst recommends setting it to 0
1963 	 */
1964 
1965 	/* WQ_UNBOUND allows decrypt tasks to run on any CPU */
1966 	decrypt_wq = alloc_workqueue("smb3decryptd",
1967 				     WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1968 	if (!decrypt_wq) {
1969 		rc = -ENOMEM;
1970 		goto out_destroy_cifsiod_wq;
1971 	}
1972 
1973 	fileinfo_put_wq = alloc_workqueue("cifsfileinfoput",
1974 				     WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1975 	if (!fileinfo_put_wq) {
1976 		rc = -ENOMEM;
1977 		goto out_destroy_decrypt_wq;
1978 	}
1979 
1980 	cifsoplockd_wq = alloc_workqueue("cifsoplockd",
1981 					 WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_PERCPU,
1982 					 0);
1983 	if (!cifsoplockd_wq) {
1984 		rc = -ENOMEM;
1985 		goto out_destroy_fileinfo_put_wq;
1986 	}
1987 
1988 	deferredclose_wq = alloc_workqueue("deferredclose",
1989 					   WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_PERCPU,
1990 					   0);
1991 	if (!deferredclose_wq) {
1992 		rc = -ENOMEM;
1993 		goto out_destroy_cifsoplockd_wq;
1994 	}
1995 
1996 	serverclose_wq = alloc_workqueue("serverclose",
1997 					   WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_PERCPU,
1998 					   0);
1999 	if (!serverclose_wq) {
2000 		rc = -ENOMEM;
2001 		goto out_destroy_deferredclose_wq;
2002 	}
2003 
2004 	cfid_put_wq = alloc_workqueue("cfid_put_wq",
2005 				      WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_PERCPU,
2006 				      0);
2007 	if (!cfid_put_wq) {
2008 		rc = -ENOMEM;
2009 		goto out_destroy_serverclose_wq;
2010 	}
2011 
2012 	rc = cifs_init_inodecache();
2013 	if (rc)
2014 		goto out_destroy_cfid_put_wq;
2015 
2016 	rc = cifs_init_netfs();
2017 	if (rc)
2018 		goto out_destroy_inodecache;
2019 
2020 	rc = init_mids();
2021 	if (rc)
2022 		goto out_destroy_netfs;
2023 
2024 	rc = cifs_init_request_bufs();
2025 	if (rc)
2026 		goto out_destroy_mids;
2027 
2028 #ifdef CONFIG_CIFS_DFS_UPCALL
2029 	rc = dfs_cache_init();
2030 	if (rc)
2031 		goto out_destroy_request_bufs;
2032 #endif /* CONFIG_CIFS_DFS_UPCALL */
2033 #ifdef CONFIG_CIFS_UPCALL
2034 	rc = init_cifs_spnego();
2035 	if (rc)
2036 		goto out_destroy_dfs_cache;
2037 #endif /* CONFIG_CIFS_UPCALL */
2038 #ifdef CONFIG_CIFS_SWN_UPCALL
2039 	rc = cifs_genl_init();
2040 	if (rc)
2041 		goto out_register_key_type;
2042 #endif /* CONFIG_CIFS_SWN_UPCALL */
2043 
2044 	rc = init_cifs_idmap();
2045 	if (rc)
2046 		goto out_cifs_swn_init;
2047 
2048 	rc = register_filesystem(&cifs_fs_type);
2049 	if (rc)
2050 		goto out_init_cifs_idmap;
2051 
2052 	rc = register_filesystem(&smb3_fs_type);
2053 	if (rc) {
2054 		unregister_filesystem(&cifs_fs_type);
2055 		goto out_init_cifs_idmap;
2056 	}
2057 
2058 	return 0;
2059 
2060 out_init_cifs_idmap:
2061 	exit_cifs_idmap();
2062 out_cifs_swn_init:
2063 #ifdef CONFIG_CIFS_SWN_UPCALL
2064 	cifs_genl_exit();
2065 out_register_key_type:
2066 #endif
2067 #ifdef CONFIG_CIFS_UPCALL
2068 	exit_cifs_spnego();
2069 out_destroy_dfs_cache:
2070 #endif
2071 #ifdef CONFIG_CIFS_DFS_UPCALL
2072 	dfs_cache_destroy();
2073 out_destroy_request_bufs:
2074 #endif
2075 	cifs_destroy_request_bufs();
2076 out_destroy_mids:
2077 	destroy_mids();
2078 out_destroy_netfs:
2079 	cifs_destroy_netfs();
2080 out_destroy_inodecache:
2081 	cifs_destroy_inodecache();
2082 out_destroy_cfid_put_wq:
2083 	destroy_workqueue(cfid_put_wq);
2084 out_destroy_serverclose_wq:
2085 	destroy_workqueue(serverclose_wq);
2086 out_destroy_deferredclose_wq:
2087 	destroy_workqueue(deferredclose_wq);
2088 out_destroy_cifsoplockd_wq:
2089 	destroy_workqueue(cifsoplockd_wq);
2090 out_destroy_fileinfo_put_wq:
2091 	destroy_workqueue(fileinfo_put_wq);
2092 out_destroy_decrypt_wq:
2093 	destroy_workqueue(decrypt_wq);
2094 out_destroy_cifsiod_wq:
2095 	destroy_workqueue(cifsiod_wq);
2096 out_clean_proc:
2097 	cifs_proc_clean();
2098 	return rc;
2099 }
2100 
2101 static void __exit
2102 exit_cifs(void)
2103 {
2104 	cifs_dbg(NOISY, "exit_smb3\n");
2105 	unregister_filesystem(&cifs_fs_type);
2106 	unregister_filesystem(&smb3_fs_type);
2107 	cifs_release_automount_timer();
2108 	exit_cifs_idmap();
2109 #ifdef CONFIG_CIFS_SWN_UPCALL
2110 	cifs_genl_exit();
2111 #endif
2112 #ifdef CONFIG_CIFS_UPCALL
2113 	exit_cifs_spnego();
2114 #endif
2115 #ifdef CONFIG_CIFS_DFS_UPCALL
2116 	dfs_cache_destroy();
2117 #endif
2118 	cifs_destroy_request_bufs();
2119 	destroy_mids();
2120 	cifs_destroy_netfs();
2121 	cifs_destroy_inodecache();
2122 	destroy_workqueue(deferredclose_wq);
2123 	destroy_workqueue(cifsoplockd_wq);
2124 	destroy_workqueue(decrypt_wq);
2125 	destroy_workqueue(fileinfo_put_wq);
2126 	destroy_workqueue(serverclose_wq);
2127 	destroy_workqueue(cfid_put_wq);
2128 	destroy_workqueue(cifsiod_wq);
2129 	cifs_proc_clean();
2130 }
2131 
2132 MODULE_AUTHOR("Steve French");
2133 MODULE_LICENSE("GPL");	/* combination of LGPL + GPL source behaves as GPL */
2134 MODULE_DESCRIPTION
2135 	("VFS to access SMB3 servers e.g. Samba, Macs, Azure and Windows (and "
2136 	"also older servers complying with the SNIA CIFS Specification)");
2137 MODULE_VERSION(CIFS_VERSION);
2138 MODULE_SOFTDEP("nls");
2139 MODULE_SOFTDEP("aes");
2140 MODULE_SOFTDEP("aead2");
2141 MODULE_SOFTDEP("ccm");
2142 MODULE_SOFTDEP("gcm");
2143 module_init(init_cifs)
2144 module_exit(exit_cifs)
2145