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