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