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