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