1 /*
2  *   fs/cifs/cifsfs.c
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  *   This library is free software; you can redistribute it and/or modify
10  *   it under the terms of the GNU Lesser General Public License as published
11  *   by the Free Software Foundation; either version 2.1 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This library is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
17  *   the GNU Lesser General Public License for more details.
18  *
19  *   You should have received a copy of the GNU Lesser General Public License
20  *   along with this library; if not, write to the Free Software
21  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 /* Note that BB means BUGBUG (ie something to fix eventually) */
25 
26 #include <linux/module.h>
27 #include <linux/fs.h>
28 #include <linux/mount.h>
29 #include <linux/slab.h>
30 #include <linux/init.h>
31 #include <linux/list.h>
32 #include <linux/seq_file.h>
33 #include <linux/vfs.h>
34 #include <linux/mempool.h>
35 #include <linux/delay.h>
36 #include <linux/kthread.h>
37 #include <linux/freezer.h>
38 #include <linux/namei.h>
39 #include <linux/random.h>
40 #include <linux/uuid.h>
41 #include <linux/xattr.h>
42 #include <net/ipv6.h>
43 #include "cifsfs.h"
44 #include "cifspdu.h"
45 #define DECLARE_GLOBALS_HERE
46 #include "cifsglob.h"
47 #include "cifsproto.h"
48 #include "cifs_debug.h"
49 #include "cifs_fs_sb.h"
50 #include <linux/mm.h>
51 #include <linux/key-type.h>
52 #include "cifs_spnego.h"
53 #include "fscache.h"
54 #include "smb2pdu.h"
55 
56 int cifsFYI = 0;
57 bool traceSMB;
58 bool enable_oplocks = true;
59 bool linuxExtEnabled = true;
60 bool lookupCacheEnabled = true;
61 bool disable_legacy_dialects; /* false by default */
62 unsigned int global_secflags = CIFSSEC_DEF;
63 /* unsigned int ntlmv2_support = 0; */
64 unsigned int sign_CIFS_PDUs = 1;
65 static const struct super_operations cifs_super_ops;
66 unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
67 module_param(CIFSMaxBufSize, uint, 0444);
68 MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header) "
69 				 "for CIFS requests. "
70 				 "Default: 16384 Range: 8192 to 130048");
71 unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
72 module_param(cifs_min_rcv, uint, 0444);
73 MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
74 				"1 to 64");
75 unsigned int cifs_min_small = 30;
76 module_param(cifs_min_small, uint, 0444);
77 MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
78 				 "Range: 2 to 256");
79 unsigned int cifs_max_pending = CIFS_MAX_REQ;
80 module_param(cifs_max_pending, uint, 0444);
81 MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server for "
82 				   "CIFS/SMB1 dialect (N/A for SMB3) "
83 				   "Default: 32767 Range: 2 to 32767.");
84 module_param(enable_oplocks, bool, 0644);
85 MODULE_PARM_DESC(enable_oplocks, "Enable or disable oplocks. Default: y/Y/1");
86 
87 module_param(disable_legacy_dialects, bool, 0644);
88 MODULE_PARM_DESC(disable_legacy_dialects, "To improve security it may be "
89 				  "helpful to restrict the ability to "
90 				  "override the default dialects (SMB2.1, "
91 				  "SMB3 and SMB3.02) on mount with old "
92 				  "dialects (CIFS/SMB1 and SMB2) since "
93 				  "vers=1.0 (CIFS/SMB1) and vers=2.0 are weaker"
94 				  " and less secure. Default: n/N/0");
95 
96 extern mempool_t *cifs_sm_req_poolp;
97 extern mempool_t *cifs_req_poolp;
98 extern mempool_t *cifs_mid_poolp;
99 
100 struct workqueue_struct	*cifsiod_wq;
101 struct workqueue_struct	*cifsoplockd_wq;
102 __u32 cifs_lock_secret;
103 
104 /*
105  * Bumps refcount for cifs super block.
106  * Note that it should be only called if a referece to VFS super block is
107  * already held, e.g. in open-type syscalls context. Otherwise it can race with
108  * atomic_dec_and_test in deactivate_locked_super.
109  */
110 void
cifs_sb_active(struct super_block * sb)111 cifs_sb_active(struct super_block *sb)
112 {
113 	struct cifs_sb_info *server = CIFS_SB(sb);
114 
115 	if (atomic_inc_return(&server->active) == 1)
116 		atomic_inc(&sb->s_active);
117 }
118 
119 void
cifs_sb_deactive(struct super_block * sb)120 cifs_sb_deactive(struct super_block *sb)
121 {
122 	struct cifs_sb_info *server = CIFS_SB(sb);
123 
124 	if (atomic_dec_and_test(&server->active))
125 		deactivate_super(sb);
126 }
127 
128 static int
cifs_read_super(struct super_block * sb)129 cifs_read_super(struct super_block *sb)
130 {
131 	struct inode *inode;
132 	struct cifs_sb_info *cifs_sb;
133 	struct cifs_tcon *tcon;
134 	int rc = 0;
135 
136 	cifs_sb = CIFS_SB(sb);
137 	tcon = cifs_sb_master_tcon(cifs_sb);
138 
139 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIXACL)
140 		sb->s_flags |= SB_POSIXACL;
141 
142 	if (tcon->snapshot_time)
143 		sb->s_flags |= SB_RDONLY;
144 
145 	if (tcon->ses->capabilities & tcon->ses->server->vals->cap_large_files)
146 		sb->s_maxbytes = MAX_LFS_FILESIZE;
147 	else
148 		sb->s_maxbytes = MAX_NON_LFS;
149 
150 	/* BB FIXME fix time_gran to be larger for LANMAN sessions */
151 	sb->s_time_gran = 100;
152 
153 	sb->s_magic = CIFS_MAGIC_NUMBER;
154 	sb->s_op = &cifs_super_ops;
155 	sb->s_xattr = cifs_xattr_handlers;
156 	rc = super_setup_bdi(sb);
157 	if (rc)
158 		goto out_no_root;
159 	/* tune readahead according to rsize */
160 	sb->s_bdi->ra_pages = cifs_sb->rsize / PAGE_SIZE;
161 
162 	sb->s_blocksize = CIFS_MAX_MSGSIZE;
163 	sb->s_blocksize_bits = 14;	/* default 2**14 = CIFS_MAX_MSGSIZE */
164 	inode = cifs_root_iget(sb);
165 
166 	if (IS_ERR(inode)) {
167 		rc = PTR_ERR(inode);
168 		goto out_no_root;
169 	}
170 
171 	if (tcon->nocase)
172 		sb->s_d_op = &cifs_ci_dentry_ops;
173 	else
174 		sb->s_d_op = &cifs_dentry_ops;
175 
176 	sb->s_root = d_make_root(inode);
177 	if (!sb->s_root) {
178 		rc = -ENOMEM;
179 		goto out_no_root;
180 	}
181 
182 #ifdef CONFIG_CIFS_NFSD_EXPORT
183 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
184 		cifs_dbg(FYI, "export ops supported\n");
185 		sb->s_export_op = &cifs_export_ops;
186 	}
187 #endif /* CONFIG_CIFS_NFSD_EXPORT */
188 
189 	return 0;
190 
191 out_no_root:
192 	cifs_dbg(VFS, "%s: get root inode failed\n", __func__);
193 	return rc;
194 }
195 
cifs_kill_sb(struct super_block * sb)196 static void cifs_kill_sb(struct super_block *sb)
197 {
198 	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
199 	kill_anon_super(sb);
200 	cifs_umount(cifs_sb);
201 }
202 
203 static int
cifs_statfs(struct dentry * dentry,struct kstatfs * buf)204 cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
205 {
206 	struct super_block *sb = dentry->d_sb;
207 	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
208 	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
209 	struct TCP_Server_Info *server = tcon->ses->server;
210 	unsigned int xid;
211 	int rc = 0;
212 
213 	xid = get_xid();
214 
215 	if (le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength) > 0)
216 		buf->f_namelen =
217 		       le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength);
218 	else
219 		buf->f_namelen = PATH_MAX;
220 
221 	buf->f_fsid.val[0] = tcon->vol_serial_number;
222 	/* are using part of create time for more randomness, see man statfs */
223 	buf->f_fsid.val[1] =  (int)le64_to_cpu(tcon->vol_create_time);
224 
225 	buf->f_files = 0;	/* undefined */
226 	buf->f_ffree = 0;	/* unlimited */
227 
228 	if (server->ops->queryfs)
229 		rc = server->ops->queryfs(xid, tcon, buf);
230 
231 	free_xid(xid);
232 	return rc;
233 }
234 
cifs_fallocate(struct file * file,int mode,loff_t off,loff_t len)235 static long cifs_fallocate(struct file *file, int mode, loff_t off, loff_t len)
236 {
237 	struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
238 	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
239 	struct TCP_Server_Info *server = tcon->ses->server;
240 
241 	if (server->ops->fallocate)
242 		return server->ops->fallocate(file, tcon, mode, off, len);
243 
244 	return -EOPNOTSUPP;
245 }
246 
cifs_permission(struct inode * inode,int mask)247 static int cifs_permission(struct inode *inode, int mask)
248 {
249 	struct cifs_sb_info *cifs_sb;
250 
251 	cifs_sb = CIFS_SB(inode->i_sb);
252 
253 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
254 		if ((mask & MAY_EXEC) && !execute_ok(inode))
255 			return -EACCES;
256 		else
257 			return 0;
258 	} else /* file mode might have been restricted at mount time
259 		on the client (above and beyond ACL on servers) for
260 		servers which do not support setting and viewing mode bits,
261 		so allowing client to check permissions is useful */
262 		return generic_permission(inode, mask);
263 }
264 
265 static struct kmem_cache *cifs_inode_cachep;
266 static struct kmem_cache *cifs_req_cachep;
267 static struct kmem_cache *cifs_mid_cachep;
268 static struct kmem_cache *cifs_sm_req_cachep;
269 mempool_t *cifs_sm_req_poolp;
270 mempool_t *cifs_req_poolp;
271 mempool_t *cifs_mid_poolp;
272 
273 static struct inode *
cifs_alloc_inode(struct super_block * sb)274 cifs_alloc_inode(struct super_block *sb)
275 {
276 	struct cifsInodeInfo *cifs_inode;
277 	cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
278 	if (!cifs_inode)
279 		return NULL;
280 	cifs_inode->cifsAttrs = 0x20;	/* default */
281 	cifs_inode->time = 0;
282 	/*
283 	 * Until the file is open and we have gotten oplock info back from the
284 	 * server, can not assume caching of file data or metadata.
285 	 */
286 	cifs_set_oplock_level(cifs_inode, 0);
287 	cifs_inode->flags = 0;
288 	spin_lock_init(&cifs_inode->writers_lock);
289 	cifs_inode->writers = 0;
290 	cifs_inode->vfs_inode.i_blkbits = 14;  /* 2**14 = CIFS_MAX_MSGSIZE */
291 	cifs_inode->server_eof = 0;
292 	cifs_inode->uniqueid = 0;
293 	cifs_inode->createtime = 0;
294 	cifs_inode->epoch = 0;
295 	spin_lock_init(&cifs_inode->open_file_lock);
296 	generate_random_uuid(cifs_inode->lease_key);
297 
298 	/*
299 	 * Can not set i_flags here - they get immediately overwritten to zero
300 	 * by the VFS.
301 	 */
302 	/* cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME; */
303 	INIT_LIST_HEAD(&cifs_inode->openFileList);
304 	INIT_LIST_HEAD(&cifs_inode->llist);
305 	return &cifs_inode->vfs_inode;
306 }
307 
cifs_i_callback(struct rcu_head * head)308 static void cifs_i_callback(struct rcu_head *head)
309 {
310 	struct inode *inode = container_of(head, struct inode, i_rcu);
311 	kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
312 }
313 
314 static void
cifs_destroy_inode(struct inode * inode)315 cifs_destroy_inode(struct inode *inode)
316 {
317 	call_rcu(&inode->i_rcu, cifs_i_callback);
318 }
319 
320 static void
cifs_evict_inode(struct inode * inode)321 cifs_evict_inode(struct inode *inode)
322 {
323 	truncate_inode_pages_final(&inode->i_data);
324 	clear_inode(inode);
325 	cifs_fscache_release_inode_cookie(inode);
326 }
327 
328 static void
cifs_show_address(struct seq_file * s,struct TCP_Server_Info * server)329 cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
330 {
331 	struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
332 	struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
333 
334 	seq_puts(s, ",addr=");
335 
336 	switch (server->dstaddr.ss_family) {
337 	case AF_INET:
338 		seq_printf(s, "%pI4", &sa->sin_addr.s_addr);
339 		break;
340 	case AF_INET6:
341 		seq_printf(s, "%pI6", &sa6->sin6_addr.s6_addr);
342 		if (sa6->sin6_scope_id)
343 			seq_printf(s, "%%%u", sa6->sin6_scope_id);
344 		break;
345 	default:
346 		seq_puts(s, "(unknown)");
347 	}
348 	if (server->rdma)
349 		seq_puts(s, ",rdma");
350 }
351 
352 static void
cifs_show_security(struct seq_file * s,struct cifs_ses * ses)353 cifs_show_security(struct seq_file *s, struct cifs_ses *ses)
354 {
355 	if (ses->sectype == Unspecified) {
356 		if (ses->user_name == NULL)
357 			seq_puts(s, ",sec=none");
358 		return;
359 	}
360 
361 	seq_puts(s, ",sec=");
362 
363 	switch (ses->sectype) {
364 	case LANMAN:
365 		seq_puts(s, "lanman");
366 		break;
367 	case NTLMv2:
368 		seq_puts(s, "ntlmv2");
369 		break;
370 	case NTLM:
371 		seq_puts(s, "ntlm");
372 		break;
373 	case Kerberos:
374 		seq_puts(s, "krb5");
375 		break;
376 	case RawNTLMSSP:
377 		seq_puts(s, "ntlmssp");
378 		break;
379 	default:
380 		/* shouldn't ever happen */
381 		seq_puts(s, "unknown");
382 		break;
383 	}
384 
385 	if (ses->sign)
386 		seq_puts(s, "i");
387 }
388 
389 static void
cifs_show_cache_flavor(struct seq_file * s,struct cifs_sb_info * cifs_sb)390 cifs_show_cache_flavor(struct seq_file *s, struct cifs_sb_info *cifs_sb)
391 {
392 	seq_puts(s, ",cache=");
393 
394 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
395 		seq_puts(s, "strict");
396 	else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
397 		seq_puts(s, "none");
398 	else
399 		seq_puts(s, "loose");
400 }
401 
402 static void
cifs_show_nls(struct seq_file * s,struct nls_table * cur)403 cifs_show_nls(struct seq_file *s, struct nls_table *cur)
404 {
405 	struct nls_table *def;
406 
407 	/* Display iocharset= option if it's not default charset */
408 	def = load_nls_default();
409 	if (def != cur)
410 		seq_printf(s, ",iocharset=%s", cur->charset);
411 	unload_nls(def);
412 }
413 
414 /*
415  * cifs_show_options() is for displaying mount options in /proc/mounts.
416  * Not all settable options are displayed but most of the important
417  * ones are.
418  */
419 static int
cifs_show_options(struct seq_file * s,struct dentry * root)420 cifs_show_options(struct seq_file *s, struct dentry *root)
421 {
422 	struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
423 	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
424 	struct sockaddr *srcaddr;
425 	srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
426 
427 	seq_show_option(s, "vers", tcon->ses->server->vals->version_string);
428 	cifs_show_security(s, tcon->ses);
429 	cifs_show_cache_flavor(s, cifs_sb);
430 
431 	if (tcon->no_lease)
432 		seq_puts(s, ",nolease");
433 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)
434 		seq_puts(s, ",multiuser");
435 	else if (tcon->ses->user_name)
436 		seq_show_option(s, "username", tcon->ses->user_name);
437 
438 	if (tcon->ses->domainName && tcon->ses->domainName[0] != 0)
439 		seq_show_option(s, "domain", tcon->ses->domainName);
440 
441 	if (srcaddr->sa_family != AF_UNSPEC) {
442 		struct sockaddr_in *saddr4;
443 		struct sockaddr_in6 *saddr6;
444 		saddr4 = (struct sockaddr_in *)srcaddr;
445 		saddr6 = (struct sockaddr_in6 *)srcaddr;
446 		if (srcaddr->sa_family == AF_INET6)
447 			seq_printf(s, ",srcaddr=%pI6c",
448 				   &saddr6->sin6_addr);
449 		else if (srcaddr->sa_family == AF_INET)
450 			seq_printf(s, ",srcaddr=%pI4",
451 				   &saddr4->sin_addr.s_addr);
452 		else
453 			seq_printf(s, ",srcaddr=BAD-AF:%i",
454 				   (int)(srcaddr->sa_family));
455 	}
456 
457 	seq_printf(s, ",uid=%u",
458 		   from_kuid_munged(&init_user_ns, cifs_sb->mnt_uid));
459 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
460 		seq_puts(s, ",forceuid");
461 	else
462 		seq_puts(s, ",noforceuid");
463 
464 	seq_printf(s, ",gid=%u",
465 		   from_kgid_munged(&init_user_ns, cifs_sb->mnt_gid));
466 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
467 		seq_puts(s, ",forcegid");
468 	else
469 		seq_puts(s, ",noforcegid");
470 
471 	cifs_show_address(s, tcon->ses->server);
472 
473 	if (!tcon->unix_ext)
474 		seq_printf(s, ",file_mode=0%ho,dir_mode=0%ho",
475 					   cifs_sb->mnt_file_mode,
476 					   cifs_sb->mnt_dir_mode);
477 
478 	cifs_show_nls(s, cifs_sb->local_nls);
479 
480 	if (tcon->seal)
481 		seq_puts(s, ",seal");
482 	if (tcon->nocase)
483 		seq_puts(s, ",nocase");
484 	if (tcon->retry)
485 		seq_puts(s, ",hard");
486 	else
487 		seq_puts(s, ",soft");
488 	if (tcon->use_persistent)
489 		seq_puts(s, ",persistenthandles");
490 	else if (tcon->use_resilient)
491 		seq_puts(s, ",resilienthandles");
492 	if (tcon->posix_extensions)
493 		seq_puts(s, ",posix");
494 	else if (tcon->unix_ext)
495 		seq_puts(s, ",unix");
496 	else
497 		seq_puts(s, ",nounix");
498 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
499 		seq_puts(s, ",posixpaths");
500 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
501 		seq_puts(s, ",setuids");
502 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
503 		seq_puts(s, ",idsfromsid");
504 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
505 		seq_puts(s, ",serverino");
506 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
507 		seq_puts(s, ",rwpidforward");
508 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL)
509 		seq_puts(s, ",forcemand");
510 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
511 		seq_puts(s, ",nouser_xattr");
512 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
513 		seq_puts(s, ",mapchars");
514 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SFM_CHR)
515 		seq_puts(s, ",mapposix");
516 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
517 		seq_puts(s, ",sfu");
518 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
519 		seq_puts(s, ",nobrl");
520 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_HANDLE_CACHE)
521 		seq_puts(s, ",nohandlecache");
522 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
523 		seq_puts(s, ",cifsacl");
524 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
525 		seq_puts(s, ",dynperm");
526 	if (root->d_sb->s_flags & SB_POSIXACL)
527 		seq_puts(s, ",acl");
528 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
529 		seq_puts(s, ",mfsymlinks");
530 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
531 		seq_puts(s, ",fsc");
532 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)
533 		seq_puts(s, ",nostrictsync");
534 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
535 		seq_puts(s, ",noperm");
536 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID)
537 		seq_printf(s, ",backupuid=%u",
538 			   from_kuid_munged(&init_user_ns,
539 					    cifs_sb->mnt_backupuid));
540 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID)
541 		seq_printf(s, ",backupgid=%u",
542 			   from_kgid_munged(&init_user_ns,
543 					    cifs_sb->mnt_backupgid));
544 
545 	seq_printf(s, ",rsize=%u", cifs_sb->rsize);
546 	seq_printf(s, ",wsize=%u", cifs_sb->wsize);
547 	seq_printf(s, ",echo_interval=%lu",
548 			tcon->ses->server->echo_interval / HZ);
549 	if (tcon->snapshot_time)
550 		seq_printf(s, ",snapshot=%llu", tcon->snapshot_time);
551 	/* convert actimeo and display it in seconds */
552 	seq_printf(s, ",actimeo=%lu", cifs_sb->actimeo / HZ);
553 
554 	return 0;
555 }
556 
cifs_umount_begin(struct super_block * sb)557 static void cifs_umount_begin(struct super_block *sb)
558 {
559 	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
560 	struct cifs_tcon *tcon;
561 
562 	if (cifs_sb == NULL)
563 		return;
564 
565 	tcon = cifs_sb_master_tcon(cifs_sb);
566 
567 	spin_lock(&cifs_tcp_ses_lock);
568 	if ((tcon->tc_count > 1) || (tcon->tidStatus == CifsExiting)) {
569 		/* we have other mounts to same share or we have
570 		   already tried to force umount this and woken up
571 		   all waiting network requests, nothing to do */
572 		spin_unlock(&cifs_tcp_ses_lock);
573 		return;
574 	} else if (tcon->tc_count == 1)
575 		tcon->tidStatus = CifsExiting;
576 	spin_unlock(&cifs_tcp_ses_lock);
577 
578 	/* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
579 	/* cancel_notify_requests(tcon); */
580 	if (tcon->ses && tcon->ses->server) {
581 		cifs_dbg(FYI, "wake up tasks now - umount begin not complete\n");
582 		wake_up_all(&tcon->ses->server->request_q);
583 		wake_up_all(&tcon->ses->server->response_q);
584 		msleep(1); /* yield */
585 		/* we have to kick the requests once more */
586 		wake_up_all(&tcon->ses->server->response_q);
587 		msleep(1);
588 	}
589 
590 	return;
591 }
592 
593 #ifdef CONFIG_CIFS_STATS2
cifs_show_stats(struct seq_file * s,struct dentry * root)594 static int cifs_show_stats(struct seq_file *s, struct dentry *root)
595 {
596 	/* BB FIXME */
597 	return 0;
598 }
599 #endif
600 
cifs_remount(struct super_block * sb,int * flags,char * data)601 static int cifs_remount(struct super_block *sb, int *flags, char *data)
602 {
603 	sync_filesystem(sb);
604 	*flags |= SB_NODIRATIME;
605 	return 0;
606 }
607 
cifs_drop_inode(struct inode * inode)608 static int cifs_drop_inode(struct inode *inode)
609 {
610 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
611 
612 	/* no serverino => unconditional eviction */
613 	return !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) ||
614 		generic_drop_inode(inode);
615 }
616 
617 static const struct super_operations cifs_super_ops = {
618 	.statfs = cifs_statfs,
619 	.alloc_inode = cifs_alloc_inode,
620 	.destroy_inode = cifs_destroy_inode,
621 	.drop_inode	= cifs_drop_inode,
622 	.evict_inode	= cifs_evict_inode,
623 /*	.delete_inode	= cifs_delete_inode,  */  /* Do not need above
624 	function unless later we add lazy close of inodes or unless the
625 	kernel forgets to call us with the same number of releases (closes)
626 	as opens */
627 	.show_options = cifs_show_options,
628 	.umount_begin   = cifs_umount_begin,
629 	.remount_fs = cifs_remount,
630 #ifdef CONFIG_CIFS_STATS2
631 	.show_stats = cifs_show_stats,
632 #endif
633 };
634 
635 /*
636  * Get root dentry from superblock according to prefix path mount option.
637  * Return dentry with refcount + 1 on success and NULL otherwise.
638  */
639 static struct dentry *
cifs_get_root(struct smb_vol * vol,struct super_block * sb)640 cifs_get_root(struct smb_vol *vol, struct super_block *sb)
641 {
642 	struct dentry *dentry;
643 	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
644 	char *full_path = NULL;
645 	char *s, *p;
646 	char sep;
647 
648 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
649 		return dget(sb->s_root);
650 
651 	full_path = cifs_build_path_to_root(vol, cifs_sb,
652 				cifs_sb_master_tcon(cifs_sb), 0);
653 	if (full_path == NULL)
654 		return ERR_PTR(-ENOMEM);
655 
656 	cifs_dbg(FYI, "Get root dentry for %s\n", full_path);
657 
658 	sep = CIFS_DIR_SEP(cifs_sb);
659 	dentry = dget(sb->s_root);
660 	p = s = full_path;
661 
662 	do {
663 		struct inode *dir = d_inode(dentry);
664 		struct dentry *child;
665 
666 		if (!S_ISDIR(dir->i_mode)) {
667 			dput(dentry);
668 			dentry = ERR_PTR(-ENOTDIR);
669 			break;
670 		}
671 
672 		/* skip separators */
673 		while (*s == sep)
674 			s++;
675 		if (!*s)
676 			break;
677 		p = s++;
678 		/* next separator */
679 		while (*s && *s != sep)
680 			s++;
681 
682 		child = lookup_positive_unlocked(p, dentry, s - p);
683 		dput(dentry);
684 		dentry = child;
685 	} while (!IS_ERR(dentry));
686 	kfree(full_path);
687 	return dentry;
688 }
689 
cifs_set_super(struct super_block * sb,void * data)690 static int cifs_set_super(struct super_block *sb, void *data)
691 {
692 	struct cifs_mnt_data *mnt_data = data;
693 	sb->s_fs_info = mnt_data->cifs_sb;
694 	return set_anon_super(sb, NULL);
695 }
696 
697 static struct dentry *
cifs_smb3_do_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data,bool is_smb3)698 cifs_smb3_do_mount(struct file_system_type *fs_type,
699 	      int flags, const char *dev_name, void *data, bool is_smb3)
700 {
701 	int rc;
702 	struct super_block *sb;
703 	struct cifs_sb_info *cifs_sb;
704 	struct smb_vol *volume_info;
705 	struct cifs_mnt_data mnt_data;
706 	struct dentry *root;
707 
708 	cifs_dbg(FYI, "Devname: %s flags: %d\n", dev_name, flags);
709 
710 	volume_info = cifs_get_volume_info((char *)data, dev_name, is_smb3);
711 	if (IS_ERR(volume_info))
712 		return ERR_CAST(volume_info);
713 
714 	cifs_sb = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
715 	if (cifs_sb == NULL) {
716 		root = ERR_PTR(-ENOMEM);
717 		goto out_nls;
718 	}
719 
720 	cifs_sb->mountdata = kstrndup(data, PAGE_SIZE, GFP_KERNEL);
721 	if (cifs_sb->mountdata == NULL) {
722 		root = ERR_PTR(-ENOMEM);
723 		goto out_free;
724 	}
725 
726 	rc = cifs_setup_cifs_sb(volume_info, cifs_sb);
727 	if (rc) {
728 		root = ERR_PTR(rc);
729 		goto out_free;
730 	}
731 
732 	rc = cifs_mount(cifs_sb, volume_info);
733 	if (rc) {
734 		if (!(flags & SB_SILENT))
735 			cifs_dbg(VFS, "cifs_mount failed w/return code = %d\n",
736 				 rc);
737 		root = ERR_PTR(rc);
738 		goto out_free;
739 	}
740 
741 	mnt_data.vol = volume_info;
742 	mnt_data.cifs_sb = cifs_sb;
743 	mnt_data.flags = flags;
744 
745 	/* BB should we make this contingent on mount parm? */
746 	flags |= SB_NODIRATIME | SB_NOATIME;
747 
748 	sb = sget(fs_type, cifs_match_super, cifs_set_super, flags, &mnt_data);
749 	if (IS_ERR(sb)) {
750 		root = ERR_CAST(sb);
751 		cifs_umount(cifs_sb);
752 		goto out;
753 	}
754 
755 	if (sb->s_root) {
756 		cifs_dbg(FYI, "Use existing superblock\n");
757 		cifs_umount(cifs_sb);
758 	} else {
759 		rc = cifs_read_super(sb);
760 		if (rc) {
761 			root = ERR_PTR(rc);
762 			goto out_super;
763 		}
764 
765 		sb->s_flags |= SB_ACTIVE;
766 	}
767 
768 	root = cifs_get_root(volume_info, sb);
769 	if (IS_ERR(root))
770 		goto out_super;
771 
772 	cifs_dbg(FYI, "dentry root is: %p\n", root);
773 	goto out;
774 
775 out_super:
776 	deactivate_locked_super(sb);
777 	return root;
778 out:
779 	cifs_cleanup_volume_info(volume_info);
780 	return root;
781 
782 out_free:
783 	kfree(cifs_sb->prepath);
784 	kfree(cifs_sb->mountdata);
785 	kfree(cifs_sb);
786 out_nls:
787 	unload_nls(volume_info->local_nls);
788 	goto out;
789 }
790 
791 static struct dentry *
smb3_do_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)792 smb3_do_mount(struct file_system_type *fs_type,
793 	      int flags, const char *dev_name, void *data)
794 {
795 	return cifs_smb3_do_mount(fs_type, flags, dev_name, data, true);
796 }
797 
798 static struct dentry *
cifs_do_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)799 cifs_do_mount(struct file_system_type *fs_type,
800 	      int flags, const char *dev_name, void *data)
801 {
802 	return cifs_smb3_do_mount(fs_type, flags, dev_name, data, false);
803 }
804 
805 static ssize_t
cifs_loose_read_iter(struct kiocb * iocb,struct iov_iter * iter)806 cifs_loose_read_iter(struct kiocb *iocb, struct iov_iter *iter)
807 {
808 	ssize_t rc;
809 	struct inode *inode = file_inode(iocb->ki_filp);
810 
811 	if (iocb->ki_flags & IOCB_DIRECT)
812 		return cifs_user_readv(iocb, iter);
813 
814 	rc = cifs_revalidate_mapping(inode);
815 	if (rc)
816 		return rc;
817 
818 	return generic_file_read_iter(iocb, iter);
819 }
820 
cifs_file_write_iter(struct kiocb * iocb,struct iov_iter * from)821 static ssize_t cifs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
822 {
823 	struct inode *inode = file_inode(iocb->ki_filp);
824 	struct cifsInodeInfo *cinode = CIFS_I(inode);
825 	ssize_t written;
826 	int rc;
827 
828 	if (iocb->ki_filp->f_flags & O_DIRECT) {
829 		written = cifs_user_writev(iocb, from);
830 		if (written > 0 && CIFS_CACHE_READ(cinode)) {
831 			cifs_zap_mapping(inode);
832 			cifs_dbg(FYI,
833 				 "Set no oplock for inode=%p after a write operation\n",
834 				 inode);
835 			cinode->oplock = 0;
836 		}
837 		return written;
838 	}
839 
840 	written = cifs_get_writer(cinode);
841 	if (written)
842 		return written;
843 
844 	written = generic_file_write_iter(iocb, from);
845 
846 	if (CIFS_CACHE_WRITE(CIFS_I(inode)))
847 		goto out;
848 
849 	rc = filemap_fdatawrite(inode->i_mapping);
850 	if (rc)
851 		cifs_dbg(FYI, "cifs_file_write_iter: %d rc on %p inode\n",
852 			 rc, inode);
853 
854 out:
855 	cifs_put_writer(cinode);
856 	return written;
857 }
858 
cifs_llseek(struct file * file,loff_t offset,int whence)859 static loff_t cifs_llseek(struct file *file, loff_t offset, int whence)
860 {
861 	/*
862 	 * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
863 	 * the cached file length
864 	 */
865 	if (whence != SEEK_SET && whence != SEEK_CUR) {
866 		int rc;
867 		struct inode *inode = file_inode(file);
868 
869 		/*
870 		 * We need to be sure that all dirty pages are written and the
871 		 * server has the newest file length.
872 		 */
873 		if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
874 		    inode->i_mapping->nrpages != 0) {
875 			rc = filemap_fdatawait(inode->i_mapping);
876 			if (rc) {
877 				mapping_set_error(inode->i_mapping, rc);
878 				return rc;
879 			}
880 		}
881 		/*
882 		 * Some applications poll for the file length in this strange
883 		 * way so we must seek to end on non-oplocked files by
884 		 * setting the revalidate time to zero.
885 		 */
886 		CIFS_I(inode)->time = 0;
887 
888 		rc = cifs_revalidate_file_attr(file);
889 		if (rc < 0)
890 			return (loff_t)rc;
891 	}
892 	return generic_file_llseek(file, offset, whence);
893 }
894 
895 static int
cifs_setlease(struct file * file,long arg,struct file_lock ** lease,void ** priv)896 cifs_setlease(struct file *file, long arg, struct file_lock **lease, void **priv)
897 {
898 	/*
899 	 * Note that this is called by vfs setlease with i_lock held to
900 	 * protect *lease from going away.
901 	 */
902 	struct inode *inode = file_inode(file);
903 	struct cifsFileInfo *cfile = file->private_data;
904 
905 	if (!(S_ISREG(inode->i_mode)))
906 		return -EINVAL;
907 
908 	/* Check if file is oplocked if this is request for new lease */
909 	if (arg == F_UNLCK ||
910 	    ((arg == F_RDLCK) && CIFS_CACHE_READ(CIFS_I(inode))) ||
911 	    ((arg == F_WRLCK) && CIFS_CACHE_WRITE(CIFS_I(inode))))
912 		return generic_setlease(file, arg, lease, priv);
913 	else if (tlink_tcon(cfile->tlink)->local_lease &&
914 		 !CIFS_CACHE_READ(CIFS_I(inode)))
915 		/*
916 		 * If the server claims to support oplock on this file, then we
917 		 * still need to check oplock even if the local_lease mount
918 		 * option is set, but there are servers which do not support
919 		 * oplock for which this mount option may be useful if the user
920 		 * knows that the file won't be changed on the server by anyone
921 		 * else.
922 		 */
923 		return generic_setlease(file, arg, lease, priv);
924 	else
925 		return -EAGAIN;
926 }
927 
928 struct file_system_type cifs_fs_type = {
929 	.owner = THIS_MODULE,
930 	.name = "cifs",
931 	.mount = cifs_do_mount,
932 	.kill_sb = cifs_kill_sb,
933 	/*  .fs_flags */
934 };
935 MODULE_ALIAS_FS("cifs");
936 
937 static struct file_system_type smb3_fs_type = {
938 	.owner = THIS_MODULE,
939 	.name = "smb3",
940 	.mount = smb3_do_mount,
941 	.kill_sb = cifs_kill_sb,
942 	/*  .fs_flags */
943 };
944 MODULE_ALIAS_FS("smb3");
945 MODULE_ALIAS("smb3");
946 
947 const struct inode_operations cifs_dir_inode_ops = {
948 	.create = cifs_create,
949 	.atomic_open = cifs_atomic_open,
950 	.lookup = cifs_lookup,
951 	.getattr = cifs_getattr,
952 	.unlink = cifs_unlink,
953 	.link = cifs_hardlink,
954 	.mkdir = cifs_mkdir,
955 	.rmdir = cifs_rmdir,
956 	.rename = cifs_rename2,
957 	.permission = cifs_permission,
958 	.setattr = cifs_setattr,
959 	.symlink = cifs_symlink,
960 	.mknod   = cifs_mknod,
961 	.listxattr = cifs_listxattr,
962 };
963 
964 const struct inode_operations cifs_file_inode_ops = {
965 	.setattr = cifs_setattr,
966 	.getattr = cifs_getattr,
967 	.permission = cifs_permission,
968 	.listxattr = cifs_listxattr,
969 };
970 
971 const struct inode_operations cifs_symlink_inode_ops = {
972 	.get_link = cifs_get_link,
973 	.permission = cifs_permission,
974 	.listxattr = cifs_listxattr,
975 };
976 
cifs_clone_file_range(struct file * src_file,loff_t off,struct file * dst_file,loff_t destoff,u64 len)977 static int cifs_clone_file_range(struct file *src_file, loff_t off,
978 		struct file *dst_file, loff_t destoff, u64 len)
979 {
980 	struct inode *src_inode = file_inode(src_file);
981 	struct inode *target_inode = file_inode(dst_file);
982 	struct cifsFileInfo *smb_file_src = src_file->private_data;
983 	struct cifsFileInfo *smb_file_target;
984 	struct cifs_tcon *target_tcon;
985 	unsigned int xid;
986 	int rc;
987 
988 	cifs_dbg(FYI, "clone range\n");
989 
990 	xid = get_xid();
991 
992 	if (!src_file->private_data || !dst_file->private_data) {
993 		rc = -EBADF;
994 		cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
995 		goto out;
996 	}
997 
998 	smb_file_target = dst_file->private_data;
999 	target_tcon = tlink_tcon(smb_file_target->tlink);
1000 
1001 	/*
1002 	 * Note: cifs case is easier than btrfs since server responsible for
1003 	 * checks for proper open modes and file type and if it wants
1004 	 * server could even support copy of range where source = target
1005 	 */
1006 	lock_two_nondirectories(target_inode, src_inode);
1007 
1008 	if (len == 0)
1009 		len = src_inode->i_size - off;
1010 
1011 	cifs_dbg(FYI, "about to flush pages\n");
1012 	/* should we flush first and last page first */
1013 	truncate_inode_pages_range(&target_inode->i_data, destoff,
1014 				   PAGE_ALIGN(destoff + len)-1);
1015 
1016 	if (target_tcon->ses->server->ops->duplicate_extents)
1017 		rc = target_tcon->ses->server->ops->duplicate_extents(xid,
1018 			smb_file_src, smb_file_target, off, len, destoff);
1019 	else
1020 		rc = -EOPNOTSUPP;
1021 
1022 	/* force revalidate of size and timestamps of target file now
1023 	   that target is updated on the server */
1024 	CIFS_I(target_inode)->time = 0;
1025 	/* although unlocking in the reverse order from locking is not
1026 	   strictly necessary here it is a little cleaner to be consistent */
1027 	unlock_two_nondirectories(src_inode, target_inode);
1028 out:
1029 	free_xid(xid);
1030 	return rc;
1031 }
1032 
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)1033 ssize_t cifs_file_copychunk_range(unsigned int xid,
1034 				struct file *src_file, loff_t off,
1035 				struct file *dst_file, loff_t destoff,
1036 				size_t len, unsigned int flags)
1037 {
1038 	struct inode *src_inode = file_inode(src_file);
1039 	struct inode *target_inode = file_inode(dst_file);
1040 	struct cifsFileInfo *smb_file_src;
1041 	struct cifsFileInfo *smb_file_target;
1042 	struct cifs_tcon *src_tcon;
1043 	struct cifs_tcon *target_tcon;
1044 	ssize_t rc;
1045 
1046 	cifs_dbg(FYI, "copychunk range\n");
1047 
1048 	if (src_inode == target_inode) {
1049 		rc = -EINVAL;
1050 		goto out;
1051 	}
1052 
1053 	if (!src_file->private_data || !dst_file->private_data) {
1054 		rc = -EBADF;
1055 		cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
1056 		goto out;
1057 	}
1058 
1059 	rc = -EXDEV;
1060 	smb_file_target = dst_file->private_data;
1061 	smb_file_src = src_file->private_data;
1062 	src_tcon = tlink_tcon(smb_file_src->tlink);
1063 	target_tcon = tlink_tcon(smb_file_target->tlink);
1064 
1065 	if (src_tcon->ses != target_tcon->ses) {
1066 		cifs_dbg(VFS, "source and target of copy not on same server\n");
1067 		goto out;
1068 	}
1069 
1070 	/*
1071 	 * Note: cifs case is easier than btrfs since server responsible for
1072 	 * checks for proper open modes and file type and if it wants
1073 	 * server could even support copy of range where source = target
1074 	 */
1075 	lock_two_nondirectories(target_inode, src_inode);
1076 
1077 	cifs_dbg(FYI, "about to flush pages\n");
1078 	/* should we flush first and last page first */
1079 	truncate_inode_pages(&target_inode->i_data, 0);
1080 
1081 	if (target_tcon->ses->server->ops->copychunk_range)
1082 		rc = target_tcon->ses->server->ops->copychunk_range(xid,
1083 			smb_file_src, smb_file_target, off, len, destoff);
1084 	else
1085 		rc = -EOPNOTSUPP;
1086 
1087 	/* force revalidate of size and timestamps of target file now
1088 	 * that target is updated on the server
1089 	 */
1090 	CIFS_I(target_inode)->time = 0;
1091 	/* although unlocking in the reverse order from locking is not
1092 	 * strictly necessary here it is a little cleaner to be consistent
1093 	 */
1094 	unlock_two_nondirectories(src_inode, target_inode);
1095 
1096 out:
1097 	return rc;
1098 }
1099 
1100 /*
1101  * Directory operations under CIFS/SMB2/SMB3 are synchronous, so fsync()
1102  * is a dummy operation.
1103  */
cifs_dir_fsync(struct file * file,loff_t start,loff_t end,int datasync)1104 static int cifs_dir_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1105 {
1106 	cifs_dbg(FYI, "Sync directory - name: %pD datasync: 0x%x\n",
1107 		 file, datasync);
1108 
1109 	return 0;
1110 }
1111 
cifs_copy_file_range(struct file * src_file,loff_t off,struct file * dst_file,loff_t destoff,size_t len,unsigned int flags)1112 static ssize_t cifs_copy_file_range(struct file *src_file, loff_t off,
1113 				struct file *dst_file, loff_t destoff,
1114 				size_t len, unsigned int flags)
1115 {
1116 	unsigned int xid = get_xid();
1117 	ssize_t rc;
1118 
1119 	rc = cifs_file_copychunk_range(xid, src_file, off, dst_file, destoff,
1120 					len, flags);
1121 	free_xid(xid);
1122 	return rc;
1123 }
1124 
1125 const struct file_operations cifs_file_ops = {
1126 	.read_iter = cifs_loose_read_iter,
1127 	.write_iter = cifs_file_write_iter,
1128 	.open = cifs_open,
1129 	.release = cifs_close,
1130 	.lock = cifs_lock,
1131 	.fsync = cifs_fsync,
1132 	.flush = cifs_flush,
1133 	.mmap  = cifs_file_mmap,
1134 	.splice_read = generic_file_splice_read,
1135 	.splice_write = iter_file_splice_write,
1136 	.llseek = cifs_llseek,
1137 	.unlocked_ioctl	= cifs_ioctl,
1138 	.copy_file_range = cifs_copy_file_range,
1139 	.clone_file_range = cifs_clone_file_range,
1140 	.setlease = cifs_setlease,
1141 	.fallocate = cifs_fallocate,
1142 };
1143 
1144 const struct file_operations cifs_file_strict_ops = {
1145 	.read_iter = cifs_strict_readv,
1146 	.write_iter = cifs_strict_writev,
1147 	.open = cifs_open,
1148 	.release = cifs_close,
1149 	.lock = cifs_lock,
1150 	.fsync = cifs_strict_fsync,
1151 	.flush = cifs_flush,
1152 	.mmap = cifs_file_strict_mmap,
1153 	.splice_read = generic_file_splice_read,
1154 	.splice_write = iter_file_splice_write,
1155 	.llseek = cifs_llseek,
1156 	.unlocked_ioctl	= cifs_ioctl,
1157 	.copy_file_range = cifs_copy_file_range,
1158 	.clone_file_range = cifs_clone_file_range,
1159 	.setlease = cifs_setlease,
1160 	.fallocate = cifs_fallocate,
1161 };
1162 
1163 const struct file_operations cifs_file_direct_ops = {
1164 	/* BB reevaluate whether they can be done with directio, no cache */
1165 	.read_iter = cifs_user_readv,
1166 	.write_iter = cifs_user_writev,
1167 	.open = cifs_open,
1168 	.release = cifs_close,
1169 	.lock = cifs_lock,
1170 	.fsync = cifs_fsync,
1171 	.flush = cifs_flush,
1172 	.mmap = cifs_file_mmap,
1173 	.splice_read = generic_file_splice_read,
1174 	.splice_write = iter_file_splice_write,
1175 	.unlocked_ioctl  = cifs_ioctl,
1176 	.copy_file_range = cifs_copy_file_range,
1177 	.clone_file_range = cifs_clone_file_range,
1178 	.llseek = cifs_llseek,
1179 	.setlease = cifs_setlease,
1180 	.fallocate = cifs_fallocate,
1181 };
1182 
1183 const struct file_operations cifs_file_nobrl_ops = {
1184 	.read_iter = cifs_loose_read_iter,
1185 	.write_iter = cifs_file_write_iter,
1186 	.open = cifs_open,
1187 	.release = cifs_close,
1188 	.fsync = cifs_fsync,
1189 	.flush = cifs_flush,
1190 	.mmap  = cifs_file_mmap,
1191 	.splice_read = generic_file_splice_read,
1192 	.splice_write = iter_file_splice_write,
1193 	.llseek = cifs_llseek,
1194 	.unlocked_ioctl	= cifs_ioctl,
1195 	.copy_file_range = cifs_copy_file_range,
1196 	.clone_file_range = cifs_clone_file_range,
1197 	.setlease = cifs_setlease,
1198 	.fallocate = cifs_fallocate,
1199 };
1200 
1201 const struct file_operations cifs_file_strict_nobrl_ops = {
1202 	.read_iter = cifs_strict_readv,
1203 	.write_iter = cifs_strict_writev,
1204 	.open = cifs_open,
1205 	.release = cifs_close,
1206 	.fsync = cifs_strict_fsync,
1207 	.flush = cifs_flush,
1208 	.mmap = cifs_file_strict_mmap,
1209 	.splice_read = generic_file_splice_read,
1210 	.splice_write = iter_file_splice_write,
1211 	.llseek = cifs_llseek,
1212 	.unlocked_ioctl	= cifs_ioctl,
1213 	.copy_file_range = cifs_copy_file_range,
1214 	.clone_file_range = cifs_clone_file_range,
1215 	.setlease = cifs_setlease,
1216 	.fallocate = cifs_fallocate,
1217 };
1218 
1219 const struct file_operations cifs_file_direct_nobrl_ops = {
1220 	/* BB reevaluate whether they can be done with directio, no cache */
1221 	.read_iter = cifs_user_readv,
1222 	.write_iter = cifs_user_writev,
1223 	.open = cifs_open,
1224 	.release = cifs_close,
1225 	.fsync = cifs_fsync,
1226 	.flush = cifs_flush,
1227 	.mmap = cifs_file_mmap,
1228 	.splice_read = generic_file_splice_read,
1229 	.splice_write = iter_file_splice_write,
1230 	.unlocked_ioctl  = cifs_ioctl,
1231 	.copy_file_range = cifs_copy_file_range,
1232 	.clone_file_range = cifs_clone_file_range,
1233 	.llseek = cifs_llseek,
1234 	.setlease = cifs_setlease,
1235 	.fallocate = cifs_fallocate,
1236 };
1237 
1238 const struct file_operations cifs_dir_ops = {
1239 	.iterate_shared = cifs_readdir,
1240 	.release = cifs_closedir,
1241 	.read    = generic_read_dir,
1242 	.unlocked_ioctl  = cifs_ioctl,
1243 	.copy_file_range = cifs_copy_file_range,
1244 	.clone_file_range = cifs_clone_file_range,
1245 	.llseek = generic_file_llseek,
1246 	.fsync = cifs_dir_fsync,
1247 };
1248 
1249 static void
cifs_init_once(void * inode)1250 cifs_init_once(void *inode)
1251 {
1252 	struct cifsInodeInfo *cifsi = inode;
1253 
1254 	inode_init_once(&cifsi->vfs_inode);
1255 	init_rwsem(&cifsi->lock_sem);
1256 }
1257 
1258 static int __init
cifs_init_inodecache(void)1259 cifs_init_inodecache(void)
1260 {
1261 	cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
1262 					      sizeof(struct cifsInodeInfo),
1263 					      0, (SLAB_RECLAIM_ACCOUNT|
1264 						SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1265 					      cifs_init_once);
1266 	if (cifs_inode_cachep == NULL)
1267 		return -ENOMEM;
1268 
1269 	return 0;
1270 }
1271 
1272 static void
cifs_destroy_inodecache(void)1273 cifs_destroy_inodecache(void)
1274 {
1275 	/*
1276 	 * Make sure all delayed rcu free inodes are flushed before we
1277 	 * destroy cache.
1278 	 */
1279 	rcu_barrier();
1280 	kmem_cache_destroy(cifs_inode_cachep);
1281 }
1282 
1283 static int
cifs_init_request_bufs(void)1284 cifs_init_request_bufs(void)
1285 {
1286 	/*
1287 	 * SMB2 maximum header size is bigger than CIFS one - no problems to
1288 	 * allocate some more bytes for CIFS.
1289 	 */
1290 	size_t max_hdr_size = MAX_SMB2_HDR_SIZE;
1291 
1292 	if (CIFSMaxBufSize < 8192) {
1293 	/* Buffer size can not be smaller than 2 * PATH_MAX since maximum
1294 	Unicode path name has to fit in any SMB/CIFS path based frames */
1295 		CIFSMaxBufSize = 8192;
1296 	} else if (CIFSMaxBufSize > 1024*127) {
1297 		CIFSMaxBufSize = 1024 * 127;
1298 	} else {
1299 		CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
1300 	}
1301 /*
1302 	cifs_dbg(VFS, "CIFSMaxBufSize %d 0x%x\n",
1303 		 CIFSMaxBufSize, CIFSMaxBufSize);
1304 */
1305 	cifs_req_cachep = kmem_cache_create_usercopy("cifs_request",
1306 					    CIFSMaxBufSize + max_hdr_size, 0,
1307 					    SLAB_HWCACHE_ALIGN, 0,
1308 					    CIFSMaxBufSize + max_hdr_size,
1309 					    NULL);
1310 	if (cifs_req_cachep == NULL)
1311 		return -ENOMEM;
1312 
1313 	if (cifs_min_rcv < 1)
1314 		cifs_min_rcv = 1;
1315 	else if (cifs_min_rcv > 64) {
1316 		cifs_min_rcv = 64;
1317 		cifs_dbg(VFS, "cifs_min_rcv set to maximum (64)\n");
1318 	}
1319 
1320 	cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
1321 						  cifs_req_cachep);
1322 
1323 	if (cifs_req_poolp == NULL) {
1324 		kmem_cache_destroy(cifs_req_cachep);
1325 		return -ENOMEM;
1326 	}
1327 	/* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
1328 	almost all handle based requests (but not write response, nor is it
1329 	sufficient for path based requests).  A smaller size would have
1330 	been more efficient (compacting multiple slab items on one 4k page)
1331 	for the case in which debug was on, but this larger size allows
1332 	more SMBs to use small buffer alloc and is still much more
1333 	efficient to alloc 1 per page off the slab compared to 17K (5page)
1334 	alloc of large cifs buffers even when page debugging is on */
1335 	cifs_sm_req_cachep = kmem_cache_create_usercopy("cifs_small_rq",
1336 			MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
1337 			0, MAX_CIFS_SMALL_BUFFER_SIZE, NULL);
1338 	if (cifs_sm_req_cachep == NULL) {
1339 		mempool_destroy(cifs_req_poolp);
1340 		kmem_cache_destroy(cifs_req_cachep);
1341 		return -ENOMEM;
1342 	}
1343 
1344 	if (cifs_min_small < 2)
1345 		cifs_min_small = 2;
1346 	else if (cifs_min_small > 256) {
1347 		cifs_min_small = 256;
1348 		cifs_dbg(FYI, "cifs_min_small set to maximum (256)\n");
1349 	}
1350 
1351 	cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
1352 						     cifs_sm_req_cachep);
1353 
1354 	if (cifs_sm_req_poolp == NULL) {
1355 		mempool_destroy(cifs_req_poolp);
1356 		kmem_cache_destroy(cifs_req_cachep);
1357 		kmem_cache_destroy(cifs_sm_req_cachep);
1358 		return -ENOMEM;
1359 	}
1360 
1361 	return 0;
1362 }
1363 
1364 static void
cifs_destroy_request_bufs(void)1365 cifs_destroy_request_bufs(void)
1366 {
1367 	mempool_destroy(cifs_req_poolp);
1368 	kmem_cache_destroy(cifs_req_cachep);
1369 	mempool_destroy(cifs_sm_req_poolp);
1370 	kmem_cache_destroy(cifs_sm_req_cachep);
1371 }
1372 
1373 static int
cifs_init_mids(void)1374 cifs_init_mids(void)
1375 {
1376 	cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
1377 					    sizeof(struct mid_q_entry), 0,
1378 					    SLAB_HWCACHE_ALIGN, NULL);
1379 	if (cifs_mid_cachep == NULL)
1380 		return -ENOMEM;
1381 
1382 	/* 3 is a reasonable minimum number of simultaneous operations */
1383 	cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
1384 	if (cifs_mid_poolp == NULL) {
1385 		kmem_cache_destroy(cifs_mid_cachep);
1386 		return -ENOMEM;
1387 	}
1388 
1389 	return 0;
1390 }
1391 
1392 static void
cifs_destroy_mids(void)1393 cifs_destroy_mids(void)
1394 {
1395 	mempool_destroy(cifs_mid_poolp);
1396 	kmem_cache_destroy(cifs_mid_cachep);
1397 }
1398 
1399 static int __init
init_cifs(void)1400 init_cifs(void)
1401 {
1402 	int rc = 0;
1403 	cifs_proc_init();
1404 	INIT_LIST_HEAD(&cifs_tcp_ses_list);
1405 #ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* unused temporarily */
1406 	INIT_LIST_HEAD(&GlobalDnotifyReqList);
1407 	INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
1408 #endif /* was needed for dnotify, and will be needed for inotify when VFS fix */
1409 /*
1410  *  Initialize Global counters
1411  */
1412 	atomic_set(&sesInfoAllocCount, 0);
1413 	atomic_set(&tconInfoAllocCount, 0);
1414 	atomic_set(&tcpSesAllocCount, 0);
1415 	atomic_set(&tcpSesReconnectCount, 0);
1416 	atomic_set(&tconInfoReconnectCount, 0);
1417 
1418 	atomic_set(&bufAllocCount, 0);
1419 	atomic_set(&smBufAllocCount, 0);
1420 #ifdef CONFIG_CIFS_STATS2
1421 	atomic_set(&totBufAllocCount, 0);
1422 	atomic_set(&totSmBufAllocCount, 0);
1423 #endif /* CONFIG_CIFS_STATS2 */
1424 
1425 	atomic_set(&midCount, 0);
1426 	GlobalCurrentXid = 0;
1427 	GlobalTotalActiveXid = 0;
1428 	GlobalMaxActiveXid = 0;
1429 	spin_lock_init(&cifs_tcp_ses_lock);
1430 	spin_lock_init(&GlobalMid_Lock);
1431 
1432 	cifs_lock_secret = get_random_u32();
1433 
1434 	if (cifs_max_pending < 2) {
1435 		cifs_max_pending = 2;
1436 		cifs_dbg(FYI, "cifs_max_pending set to min of 2\n");
1437 	} else if (cifs_max_pending > CIFS_MAX_REQ) {
1438 		cifs_max_pending = CIFS_MAX_REQ;
1439 		cifs_dbg(FYI, "cifs_max_pending set to max of %u\n",
1440 			 CIFS_MAX_REQ);
1441 	}
1442 
1443 	cifsiod_wq = alloc_workqueue("cifsiod", WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1444 	if (!cifsiod_wq) {
1445 		rc = -ENOMEM;
1446 		goto out_clean_proc;
1447 	}
1448 
1449 	cifsoplockd_wq = alloc_workqueue("cifsoplockd",
1450 					 WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1451 	if (!cifsoplockd_wq) {
1452 		rc = -ENOMEM;
1453 		goto out_destroy_cifsiod_wq;
1454 	}
1455 
1456 	rc = cifs_fscache_register();
1457 	if (rc)
1458 		goto out_destroy_cifsoplockd_wq;
1459 
1460 	rc = cifs_init_inodecache();
1461 	if (rc)
1462 		goto out_unreg_fscache;
1463 
1464 	rc = cifs_init_mids();
1465 	if (rc)
1466 		goto out_destroy_inodecache;
1467 
1468 	rc = cifs_init_request_bufs();
1469 	if (rc)
1470 		goto out_destroy_mids;
1471 
1472 #ifdef CONFIG_CIFS_UPCALL
1473 	rc = init_cifs_spnego();
1474 	if (rc)
1475 		goto out_destroy_request_bufs;
1476 #endif /* CONFIG_CIFS_UPCALL */
1477 
1478 #ifdef CONFIG_CIFS_ACL
1479 	rc = init_cifs_idmap();
1480 	if (rc)
1481 		goto out_register_key_type;
1482 #endif /* CONFIG_CIFS_ACL */
1483 
1484 	rc = register_filesystem(&cifs_fs_type);
1485 	if (rc)
1486 		goto out_init_cifs_idmap;
1487 
1488 	rc = register_filesystem(&smb3_fs_type);
1489 	if (rc) {
1490 		unregister_filesystem(&cifs_fs_type);
1491 		goto out_init_cifs_idmap;
1492 	}
1493 
1494 	return 0;
1495 
1496 out_init_cifs_idmap:
1497 #ifdef CONFIG_CIFS_ACL
1498 	exit_cifs_idmap();
1499 out_register_key_type:
1500 #endif
1501 #ifdef CONFIG_CIFS_UPCALL
1502 	exit_cifs_spnego();
1503 out_destroy_request_bufs:
1504 #endif
1505 	cifs_destroy_request_bufs();
1506 out_destroy_mids:
1507 	cifs_destroy_mids();
1508 out_destroy_inodecache:
1509 	cifs_destroy_inodecache();
1510 out_unreg_fscache:
1511 	cifs_fscache_unregister();
1512 out_destroy_cifsoplockd_wq:
1513 	destroy_workqueue(cifsoplockd_wq);
1514 out_destroy_cifsiod_wq:
1515 	destroy_workqueue(cifsiod_wq);
1516 out_clean_proc:
1517 	cifs_proc_clean();
1518 	return rc;
1519 }
1520 
1521 static void __exit
exit_cifs(void)1522 exit_cifs(void)
1523 {
1524 	cifs_dbg(NOISY, "exit_smb3\n");
1525 	unregister_filesystem(&cifs_fs_type);
1526 	unregister_filesystem(&smb3_fs_type);
1527 	cifs_dfs_release_automount_timer();
1528 #ifdef CONFIG_CIFS_ACL
1529 	exit_cifs_idmap();
1530 #endif
1531 #ifdef CONFIG_CIFS_UPCALL
1532 	exit_cifs_spnego();
1533 #endif
1534 	cifs_destroy_request_bufs();
1535 	cifs_destroy_mids();
1536 	cifs_destroy_inodecache();
1537 	cifs_fscache_unregister();
1538 	destroy_workqueue(cifsoplockd_wq);
1539 	destroy_workqueue(cifsiod_wq);
1540 	cifs_proc_clean();
1541 }
1542 
1543 MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
1544 MODULE_LICENSE("GPL");	/* combination of LGPL + GPL source behaves as GPL */
1545 MODULE_DESCRIPTION
1546     ("VFS to access servers complying with the SNIA CIFS Specification "
1547      "e.g. Samba and Windows");
1548 MODULE_VERSION(CIFS_VERSION);
1549 MODULE_SOFTDEP("pre: arc4");
1550 MODULE_SOFTDEP("pre: des");
1551 MODULE_SOFTDEP("pre: ecb");
1552 MODULE_SOFTDEP("pre: hmac");
1553 MODULE_SOFTDEP("pre: md4");
1554 MODULE_SOFTDEP("pre: md5");
1555 MODULE_SOFTDEP("pre: nls");
1556 MODULE_SOFTDEP("pre: aes");
1557 MODULE_SOFTDEP("pre: cmac");
1558 MODULE_SOFTDEP("pre: sha256");
1559 MODULE_SOFTDEP("pre: sha512");
1560 MODULE_SOFTDEP("pre: aead2");
1561 MODULE_SOFTDEP("pre: ccm");
1562 module_init(init_cifs)
1563 module_exit(exit_cifs)
1564