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