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