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