• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	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 (!dir) {
667 			dput(dentry);
668 			dentry = ERR_PTR(-ENOENT);
669 			break;
670 		}
671 		if (!S_ISDIR(dir->i_mode)) {
672 			dput(dentry);
673 			dentry = ERR_PTR(-ENOTDIR);
674 			break;
675 		}
676 
677 		/* skip separators */
678 		while (*s == sep)
679 			s++;
680 		if (!*s)
681 			break;
682 		p = s++;
683 		/* next separator */
684 		while (*s && *s != sep)
685 			s++;
686 
687 		child = lookup_one_len_unlocked(p, dentry, s - p);
688 		dput(dentry);
689 		dentry = child;
690 	} while (!IS_ERR(dentry));
691 	kfree(full_path);
692 	return dentry;
693 }
694 
cifs_set_super(struct super_block * sb,void * data)695 static int cifs_set_super(struct super_block *sb, void *data)
696 {
697 	struct cifs_mnt_data *mnt_data = data;
698 	sb->s_fs_info = mnt_data->cifs_sb;
699 	return set_anon_super(sb, NULL);
700 }
701 
702 static struct dentry *
cifs_smb3_do_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data,bool is_smb3)703 cifs_smb3_do_mount(struct file_system_type *fs_type,
704 	      int flags, const char *dev_name, void *data, bool is_smb3)
705 {
706 	int rc;
707 	struct super_block *sb;
708 	struct cifs_sb_info *cifs_sb;
709 	struct smb_vol *volume_info;
710 	struct cifs_mnt_data mnt_data;
711 	struct dentry *root;
712 
713 	cifs_dbg(FYI, "Devname: %s flags: %d\n", dev_name, flags);
714 
715 	volume_info = cifs_get_volume_info((char *)data, dev_name, is_smb3);
716 	if (IS_ERR(volume_info))
717 		return ERR_CAST(volume_info);
718 
719 	cifs_sb = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
720 	if (cifs_sb == NULL) {
721 		root = ERR_PTR(-ENOMEM);
722 		goto out_nls;
723 	}
724 
725 	cifs_sb->mountdata = kstrndup(data, PAGE_SIZE, GFP_KERNEL);
726 	if (cifs_sb->mountdata == NULL) {
727 		root = ERR_PTR(-ENOMEM);
728 		goto out_free;
729 	}
730 
731 	rc = cifs_setup_cifs_sb(volume_info, cifs_sb);
732 	if (rc) {
733 		root = ERR_PTR(rc);
734 		goto out_free;
735 	}
736 
737 	rc = cifs_mount(cifs_sb, volume_info);
738 	if (rc) {
739 		if (!(flags & SB_SILENT))
740 			cifs_dbg(VFS, "cifs_mount failed w/return code = %d\n",
741 				 rc);
742 		root = ERR_PTR(rc);
743 		goto out_free;
744 	}
745 
746 	mnt_data.vol = volume_info;
747 	mnt_data.cifs_sb = cifs_sb;
748 	mnt_data.flags = flags;
749 
750 	/* BB should we make this contingent on mount parm? */
751 	flags |= SB_NODIRATIME | SB_NOATIME;
752 
753 	sb = sget(fs_type, cifs_match_super, cifs_set_super, flags, &mnt_data);
754 	if (IS_ERR(sb)) {
755 		root = ERR_CAST(sb);
756 		cifs_umount(cifs_sb);
757 		goto out;
758 	}
759 
760 	if (sb->s_root) {
761 		cifs_dbg(FYI, "Use existing superblock\n");
762 		cifs_umount(cifs_sb);
763 	} else {
764 		rc = cifs_read_super(sb);
765 		if (rc) {
766 			root = ERR_PTR(rc);
767 			goto out_super;
768 		}
769 
770 		sb->s_flags |= SB_ACTIVE;
771 	}
772 
773 	root = cifs_get_root(volume_info, sb);
774 	if (IS_ERR(root))
775 		goto out_super;
776 
777 	cifs_dbg(FYI, "dentry root is: %p\n", root);
778 	goto out;
779 
780 out_super:
781 	deactivate_locked_super(sb);
782 out:
783 	cifs_cleanup_volume_info(volume_info);
784 	return root;
785 
786 out_free:
787 	kfree(cifs_sb->prepath);
788 	kfree(cifs_sb->mountdata);
789 	kfree(cifs_sb);
790 out_nls:
791 	unload_nls(volume_info->local_nls);
792 	goto out;
793 }
794 
795 static struct dentry *
smb3_do_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)796 smb3_do_mount(struct file_system_type *fs_type,
797 	      int flags, const char *dev_name, void *data)
798 {
799 	return cifs_smb3_do_mount(fs_type, flags, dev_name, data, true);
800 }
801 
802 static struct dentry *
cifs_do_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)803 cifs_do_mount(struct file_system_type *fs_type,
804 	      int flags, const char *dev_name, void *data)
805 {
806 	return cifs_smb3_do_mount(fs_type, flags, dev_name, data, false);
807 }
808 
809 static ssize_t
cifs_loose_read_iter(struct kiocb * iocb,struct iov_iter * iter)810 cifs_loose_read_iter(struct kiocb *iocb, struct iov_iter *iter)
811 {
812 	ssize_t rc;
813 	struct inode *inode = file_inode(iocb->ki_filp);
814 
815 	if (iocb->ki_filp->f_flags & O_DIRECT)
816 		return cifs_user_readv(iocb, iter);
817 
818 	rc = cifs_revalidate_mapping(inode);
819 	if (rc)
820 		return rc;
821 
822 	return generic_file_read_iter(iocb, iter);
823 }
824 
cifs_file_write_iter(struct kiocb * iocb,struct iov_iter * from)825 static ssize_t cifs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
826 {
827 	struct inode *inode = file_inode(iocb->ki_filp);
828 	struct cifsInodeInfo *cinode = CIFS_I(inode);
829 	ssize_t written;
830 	int rc;
831 
832 	if (iocb->ki_filp->f_flags & O_DIRECT) {
833 		written = cifs_user_writev(iocb, from);
834 		if (written > 0 && CIFS_CACHE_READ(cinode)) {
835 			cifs_zap_mapping(inode);
836 			cifs_dbg(FYI,
837 				 "Set no oplock for inode=%p after a write operation\n",
838 				 inode);
839 			cinode->oplock = 0;
840 		}
841 		return written;
842 	}
843 
844 	written = cifs_get_writer(cinode);
845 	if (written)
846 		return written;
847 
848 	written = generic_file_write_iter(iocb, from);
849 
850 	if (CIFS_CACHE_WRITE(CIFS_I(inode)))
851 		goto out;
852 
853 	rc = filemap_fdatawrite(inode->i_mapping);
854 	if (rc)
855 		cifs_dbg(FYI, "cifs_file_write_iter: %d rc on %p inode\n",
856 			 rc, inode);
857 
858 out:
859 	cifs_put_writer(cinode);
860 	return written;
861 }
862 
cifs_llseek(struct file * file,loff_t offset,int whence)863 static loff_t cifs_llseek(struct file *file, loff_t offset, int whence)
864 {
865 	/*
866 	 * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
867 	 * the cached file length
868 	 */
869 	if (whence != SEEK_SET && whence != SEEK_CUR) {
870 		int rc;
871 		struct inode *inode = file_inode(file);
872 
873 		/*
874 		 * We need to be sure that all dirty pages are written and the
875 		 * server has the newest file length.
876 		 */
877 		if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
878 		    inode->i_mapping->nrpages != 0) {
879 			rc = filemap_fdatawait(inode->i_mapping);
880 			if (rc) {
881 				mapping_set_error(inode->i_mapping, rc);
882 				return rc;
883 			}
884 		}
885 		/*
886 		 * Some applications poll for the file length in this strange
887 		 * way so we must seek to end on non-oplocked files by
888 		 * setting the revalidate time to zero.
889 		 */
890 		CIFS_I(inode)->time = 0;
891 
892 		rc = cifs_revalidate_file_attr(file);
893 		if (rc < 0)
894 			return (loff_t)rc;
895 	}
896 	return generic_file_llseek(file, offset, whence);
897 }
898 
899 static int
cifs_setlease(struct file * file,long arg,struct file_lock ** lease,void ** priv)900 cifs_setlease(struct file *file, long arg, struct file_lock **lease, void **priv)
901 {
902 	/*
903 	 * Note that this is called by vfs setlease with i_lock held to
904 	 * protect *lease from going away.
905 	 */
906 	struct inode *inode = file_inode(file);
907 	struct cifsFileInfo *cfile = file->private_data;
908 
909 	if (!(S_ISREG(inode->i_mode)))
910 		return -EINVAL;
911 
912 	/* Check if file is oplocked if this is request for new lease */
913 	if (arg == F_UNLCK ||
914 	    ((arg == F_RDLCK) && CIFS_CACHE_READ(CIFS_I(inode))) ||
915 	    ((arg == F_WRLCK) && CIFS_CACHE_WRITE(CIFS_I(inode))))
916 		return generic_setlease(file, arg, lease, priv);
917 	else if (tlink_tcon(cfile->tlink)->local_lease &&
918 		 !CIFS_CACHE_READ(CIFS_I(inode)))
919 		/*
920 		 * If the server claims to support oplock on this file, then we
921 		 * still need to check oplock even if the local_lease mount
922 		 * option is set, but there are servers which do not support
923 		 * oplock for which this mount option may be useful if the user
924 		 * knows that the file won't be changed on the server by anyone
925 		 * else.
926 		 */
927 		return generic_setlease(file, arg, lease, priv);
928 	else
929 		return -EAGAIN;
930 }
931 
932 struct file_system_type cifs_fs_type = {
933 	.owner = THIS_MODULE,
934 	.name = "cifs",
935 	.mount = cifs_do_mount,
936 	.kill_sb = cifs_kill_sb,
937 	/*  .fs_flags */
938 };
939 MODULE_ALIAS_FS("cifs");
940 
941 static struct file_system_type smb3_fs_type = {
942 	.owner = THIS_MODULE,
943 	.name = "smb3",
944 	.mount = smb3_do_mount,
945 	.kill_sb = cifs_kill_sb,
946 	/*  .fs_flags */
947 };
948 MODULE_ALIAS_FS("smb3");
949 MODULE_ALIAS("smb3");
950 
951 const struct inode_operations cifs_dir_inode_ops = {
952 	.create = cifs_create,
953 	.atomic_open = cifs_atomic_open,
954 	.lookup = cifs_lookup,
955 	.getattr = cifs_getattr,
956 	.unlink = cifs_unlink,
957 	.link = cifs_hardlink,
958 	.mkdir = cifs_mkdir,
959 	.rmdir = cifs_rmdir,
960 	.rename = cifs_rename2,
961 	.permission = cifs_permission,
962 	.setattr = cifs_setattr,
963 	.symlink = cifs_symlink,
964 	.mknod   = cifs_mknod,
965 	.listxattr = cifs_listxattr,
966 };
967 
968 const struct inode_operations cifs_file_inode_ops = {
969 	.setattr = cifs_setattr,
970 	.getattr = cifs_getattr,
971 	.permission = cifs_permission,
972 	.listxattr = cifs_listxattr,
973 };
974 
975 const struct inode_operations cifs_symlink_inode_ops = {
976 	.get_link = cifs_get_link,
977 	.permission = cifs_permission,
978 	.listxattr = cifs_listxattr,
979 };
980 
cifs_clone_file_range(struct file * src_file,loff_t off,struct file * dst_file,loff_t destoff,u64 len)981 static int cifs_clone_file_range(struct file *src_file, loff_t off,
982 		struct file *dst_file, loff_t destoff, u64 len)
983 {
984 	struct inode *src_inode = file_inode(src_file);
985 	struct inode *target_inode = file_inode(dst_file);
986 	struct cifsFileInfo *smb_file_src = src_file->private_data;
987 	struct cifsFileInfo *smb_file_target;
988 	struct cifs_tcon *target_tcon;
989 	unsigned int xid;
990 	int rc;
991 
992 	cifs_dbg(FYI, "clone range\n");
993 
994 	xid = get_xid();
995 
996 	if (!src_file->private_data || !dst_file->private_data) {
997 		rc = -EBADF;
998 		cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
999 		goto out;
1000 	}
1001 
1002 	smb_file_target = dst_file->private_data;
1003 	target_tcon = tlink_tcon(smb_file_target->tlink);
1004 
1005 	/*
1006 	 * Note: cifs case is easier than btrfs since server responsible for
1007 	 * checks for proper open modes and file type and if it wants
1008 	 * server could even support copy of range where source = target
1009 	 */
1010 	lock_two_nondirectories(target_inode, src_inode);
1011 
1012 	if (len == 0)
1013 		len = src_inode->i_size - off;
1014 
1015 	cifs_dbg(FYI, "about to flush pages\n");
1016 	/* should we flush first and last page first */
1017 	truncate_inode_pages_range(&target_inode->i_data, destoff,
1018 				   PAGE_ALIGN(destoff + len)-1);
1019 
1020 	if (target_tcon->ses->server->ops->duplicate_extents)
1021 		rc = target_tcon->ses->server->ops->duplicate_extents(xid,
1022 			smb_file_src, smb_file_target, off, len, destoff);
1023 	else
1024 		rc = -EOPNOTSUPP;
1025 
1026 	/* force revalidate of size and timestamps of target file now
1027 	   that target is updated on the server */
1028 	CIFS_I(target_inode)->time = 0;
1029 	/* although unlocking in the reverse order from locking is not
1030 	   strictly necessary here it is a little cleaner to be consistent */
1031 	unlock_two_nondirectories(src_inode, target_inode);
1032 out:
1033 	free_xid(xid);
1034 	return rc;
1035 }
1036 
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)1037 ssize_t cifs_file_copychunk_range(unsigned int xid,
1038 				struct file *src_file, loff_t off,
1039 				struct file *dst_file, loff_t destoff,
1040 				size_t len, unsigned int flags)
1041 {
1042 	struct inode *src_inode = file_inode(src_file);
1043 	struct inode *target_inode = file_inode(dst_file);
1044 	struct cifsFileInfo *smb_file_src;
1045 	struct cifsFileInfo *smb_file_target;
1046 	struct cifs_tcon *src_tcon;
1047 	struct cifs_tcon *target_tcon;
1048 	ssize_t rc;
1049 
1050 	cifs_dbg(FYI, "copychunk range\n");
1051 
1052 	if (src_inode == target_inode) {
1053 		rc = -EINVAL;
1054 		goto out;
1055 	}
1056 
1057 	if (!src_file->private_data || !dst_file->private_data) {
1058 		rc = -EBADF;
1059 		cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
1060 		goto out;
1061 	}
1062 
1063 	rc = -EXDEV;
1064 	smb_file_target = dst_file->private_data;
1065 	smb_file_src = src_file->private_data;
1066 	src_tcon = tlink_tcon(smb_file_src->tlink);
1067 	target_tcon = tlink_tcon(smb_file_target->tlink);
1068 
1069 	if (src_tcon->ses != target_tcon->ses) {
1070 		cifs_dbg(VFS, "source and target of copy not on same server\n");
1071 		goto out;
1072 	}
1073 
1074 	/*
1075 	 * Note: cifs case is easier than btrfs since server responsible for
1076 	 * checks for proper open modes and file type and if it wants
1077 	 * server could even support copy of range where source = target
1078 	 */
1079 	lock_two_nondirectories(target_inode, src_inode);
1080 
1081 	cifs_dbg(FYI, "about to flush pages\n");
1082 	/* should we flush first and last page first */
1083 	truncate_inode_pages(&target_inode->i_data, 0);
1084 
1085 	if (target_tcon->ses->server->ops->copychunk_range)
1086 		rc = target_tcon->ses->server->ops->copychunk_range(xid,
1087 			smb_file_src, smb_file_target, off, len, destoff);
1088 	else
1089 		rc = -EOPNOTSUPP;
1090 
1091 	/* force revalidate of size and timestamps of target file now
1092 	 * that target is updated on the server
1093 	 */
1094 	CIFS_I(target_inode)->time = 0;
1095 	/* although unlocking in the reverse order from locking is not
1096 	 * strictly necessary here it is a little cleaner to be consistent
1097 	 */
1098 	unlock_two_nondirectories(src_inode, target_inode);
1099 
1100 out:
1101 	return rc;
1102 }
1103 
1104 /*
1105  * Directory operations under CIFS/SMB2/SMB3 are synchronous, so fsync()
1106  * is a dummy operation.
1107  */
cifs_dir_fsync(struct file * file,loff_t start,loff_t end,int datasync)1108 static int cifs_dir_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1109 {
1110 	cifs_dbg(FYI, "Sync directory - name: %pD datasync: 0x%x\n",
1111 		 file, datasync);
1112 
1113 	return 0;
1114 }
1115 
cifs_copy_file_range(struct file * src_file,loff_t off,struct file * dst_file,loff_t destoff,size_t len,unsigned int flags)1116 static ssize_t cifs_copy_file_range(struct file *src_file, loff_t off,
1117 				struct file *dst_file, loff_t destoff,
1118 				size_t len, unsigned int flags)
1119 {
1120 	unsigned int xid = get_xid();
1121 	ssize_t rc;
1122 
1123 	rc = cifs_file_copychunk_range(xid, src_file, off, dst_file, destoff,
1124 					len, flags);
1125 	free_xid(xid);
1126 	return rc;
1127 }
1128 
1129 const struct file_operations cifs_file_ops = {
1130 	.read_iter = cifs_loose_read_iter,
1131 	.write_iter = cifs_file_write_iter,
1132 	.open = cifs_open,
1133 	.release = cifs_close,
1134 	.lock = cifs_lock,
1135 	.fsync = cifs_fsync,
1136 	.flush = cifs_flush,
1137 	.mmap  = cifs_file_mmap,
1138 	.splice_read = generic_file_splice_read,
1139 	.splice_write = iter_file_splice_write,
1140 	.llseek = cifs_llseek,
1141 	.unlocked_ioctl	= cifs_ioctl,
1142 	.copy_file_range = cifs_copy_file_range,
1143 	.clone_file_range = cifs_clone_file_range,
1144 	.setlease = cifs_setlease,
1145 	.fallocate = cifs_fallocate,
1146 };
1147 
1148 const struct file_operations cifs_file_strict_ops = {
1149 	.read_iter = cifs_strict_readv,
1150 	.write_iter = cifs_strict_writev,
1151 	.open = cifs_open,
1152 	.release = cifs_close,
1153 	.lock = cifs_lock,
1154 	.fsync = cifs_strict_fsync,
1155 	.flush = cifs_flush,
1156 	.mmap = cifs_file_strict_mmap,
1157 	.splice_read = generic_file_splice_read,
1158 	.splice_write = iter_file_splice_write,
1159 	.llseek = cifs_llseek,
1160 	.unlocked_ioctl	= cifs_ioctl,
1161 	.copy_file_range = cifs_copy_file_range,
1162 	.clone_file_range = cifs_clone_file_range,
1163 	.setlease = cifs_setlease,
1164 	.fallocate = cifs_fallocate,
1165 };
1166 
1167 const struct file_operations cifs_file_direct_ops = {
1168 	/* BB reevaluate whether they can be done with directio, no cache */
1169 	.read_iter = cifs_user_readv,
1170 	.write_iter = cifs_user_writev,
1171 	.open = cifs_open,
1172 	.release = cifs_close,
1173 	.lock = cifs_lock,
1174 	.fsync = cifs_fsync,
1175 	.flush = cifs_flush,
1176 	.mmap = cifs_file_mmap,
1177 	.splice_read = generic_file_splice_read,
1178 	.splice_write = iter_file_splice_write,
1179 	.unlocked_ioctl  = cifs_ioctl,
1180 	.copy_file_range = cifs_copy_file_range,
1181 	.clone_file_range = cifs_clone_file_range,
1182 	.llseek = cifs_llseek,
1183 	.setlease = cifs_setlease,
1184 	.fallocate = cifs_fallocate,
1185 };
1186 
1187 const struct file_operations cifs_file_nobrl_ops = {
1188 	.read_iter = cifs_loose_read_iter,
1189 	.write_iter = cifs_file_write_iter,
1190 	.open = cifs_open,
1191 	.release = cifs_close,
1192 	.fsync = cifs_fsync,
1193 	.flush = cifs_flush,
1194 	.mmap  = cifs_file_mmap,
1195 	.splice_read = generic_file_splice_read,
1196 	.splice_write = iter_file_splice_write,
1197 	.llseek = cifs_llseek,
1198 	.unlocked_ioctl	= cifs_ioctl,
1199 	.copy_file_range = cifs_copy_file_range,
1200 	.clone_file_range = cifs_clone_file_range,
1201 	.setlease = cifs_setlease,
1202 	.fallocate = cifs_fallocate,
1203 };
1204 
1205 const struct file_operations cifs_file_strict_nobrl_ops = {
1206 	.read_iter = cifs_strict_readv,
1207 	.write_iter = cifs_strict_writev,
1208 	.open = cifs_open,
1209 	.release = cifs_close,
1210 	.fsync = cifs_strict_fsync,
1211 	.flush = cifs_flush,
1212 	.mmap = cifs_file_strict_mmap,
1213 	.splice_read = generic_file_splice_read,
1214 	.splice_write = iter_file_splice_write,
1215 	.llseek = cifs_llseek,
1216 	.unlocked_ioctl	= cifs_ioctl,
1217 	.copy_file_range = cifs_copy_file_range,
1218 	.clone_file_range = cifs_clone_file_range,
1219 	.setlease = cifs_setlease,
1220 	.fallocate = cifs_fallocate,
1221 };
1222 
1223 const struct file_operations cifs_file_direct_nobrl_ops = {
1224 	/* BB reevaluate whether they can be done with directio, no cache */
1225 	.read_iter = cifs_user_readv,
1226 	.write_iter = cifs_user_writev,
1227 	.open = cifs_open,
1228 	.release = cifs_close,
1229 	.fsync = cifs_fsync,
1230 	.flush = cifs_flush,
1231 	.mmap = cifs_file_mmap,
1232 	.splice_read = generic_file_splice_read,
1233 	.splice_write = iter_file_splice_write,
1234 	.unlocked_ioctl  = cifs_ioctl,
1235 	.copy_file_range = cifs_copy_file_range,
1236 	.clone_file_range = cifs_clone_file_range,
1237 	.llseek = cifs_llseek,
1238 	.setlease = cifs_setlease,
1239 	.fallocate = cifs_fallocate,
1240 };
1241 
1242 const struct file_operations cifs_dir_ops = {
1243 	.iterate_shared = cifs_readdir,
1244 	.release = cifs_closedir,
1245 	.read    = generic_read_dir,
1246 	.unlocked_ioctl  = cifs_ioctl,
1247 	.copy_file_range = cifs_copy_file_range,
1248 	.clone_file_range = cifs_clone_file_range,
1249 	.llseek = generic_file_llseek,
1250 	.fsync = cifs_dir_fsync,
1251 };
1252 
1253 static void
cifs_init_once(void * inode)1254 cifs_init_once(void *inode)
1255 {
1256 	struct cifsInodeInfo *cifsi = inode;
1257 
1258 	inode_init_once(&cifsi->vfs_inode);
1259 	init_rwsem(&cifsi->lock_sem);
1260 }
1261 
1262 static int __init
cifs_init_inodecache(void)1263 cifs_init_inodecache(void)
1264 {
1265 	cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
1266 					      sizeof(struct cifsInodeInfo),
1267 					      0, (SLAB_RECLAIM_ACCOUNT|
1268 						SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1269 					      cifs_init_once);
1270 	if (cifs_inode_cachep == NULL)
1271 		return -ENOMEM;
1272 
1273 	return 0;
1274 }
1275 
1276 static void
cifs_destroy_inodecache(void)1277 cifs_destroy_inodecache(void)
1278 {
1279 	/*
1280 	 * Make sure all delayed rcu free inodes are flushed before we
1281 	 * destroy cache.
1282 	 */
1283 	rcu_barrier();
1284 	kmem_cache_destroy(cifs_inode_cachep);
1285 }
1286 
1287 static int
cifs_init_request_bufs(void)1288 cifs_init_request_bufs(void)
1289 {
1290 	/*
1291 	 * SMB2 maximum header size is bigger than CIFS one - no problems to
1292 	 * allocate some more bytes for CIFS.
1293 	 */
1294 	size_t max_hdr_size = MAX_SMB2_HDR_SIZE;
1295 
1296 	if (CIFSMaxBufSize < 8192) {
1297 	/* Buffer size can not be smaller than 2 * PATH_MAX since maximum
1298 	Unicode path name has to fit in any SMB/CIFS path based frames */
1299 		CIFSMaxBufSize = 8192;
1300 	} else if (CIFSMaxBufSize > 1024*127) {
1301 		CIFSMaxBufSize = 1024 * 127;
1302 	} else {
1303 		CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
1304 	}
1305 /*
1306 	cifs_dbg(VFS, "CIFSMaxBufSize %d 0x%x\n",
1307 		 CIFSMaxBufSize, CIFSMaxBufSize);
1308 */
1309 	cifs_req_cachep = kmem_cache_create_usercopy("cifs_request",
1310 					    CIFSMaxBufSize + max_hdr_size, 0,
1311 					    SLAB_HWCACHE_ALIGN, 0,
1312 					    CIFSMaxBufSize + max_hdr_size,
1313 					    NULL);
1314 	if (cifs_req_cachep == NULL)
1315 		return -ENOMEM;
1316 
1317 	if (cifs_min_rcv < 1)
1318 		cifs_min_rcv = 1;
1319 	else if (cifs_min_rcv > 64) {
1320 		cifs_min_rcv = 64;
1321 		cifs_dbg(VFS, "cifs_min_rcv set to maximum (64)\n");
1322 	}
1323 
1324 	cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
1325 						  cifs_req_cachep);
1326 
1327 	if (cifs_req_poolp == NULL) {
1328 		kmem_cache_destroy(cifs_req_cachep);
1329 		return -ENOMEM;
1330 	}
1331 	/* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
1332 	almost all handle based requests (but not write response, nor is it
1333 	sufficient for path based requests).  A smaller size would have
1334 	been more efficient (compacting multiple slab items on one 4k page)
1335 	for the case in which debug was on, but this larger size allows
1336 	more SMBs to use small buffer alloc and is still much more
1337 	efficient to alloc 1 per page off the slab compared to 17K (5page)
1338 	alloc of large cifs buffers even when page debugging is on */
1339 	cifs_sm_req_cachep = kmem_cache_create_usercopy("cifs_small_rq",
1340 			MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
1341 			0, MAX_CIFS_SMALL_BUFFER_SIZE, NULL);
1342 	if (cifs_sm_req_cachep == NULL) {
1343 		mempool_destroy(cifs_req_poolp);
1344 		kmem_cache_destroy(cifs_req_cachep);
1345 		return -ENOMEM;
1346 	}
1347 
1348 	if (cifs_min_small < 2)
1349 		cifs_min_small = 2;
1350 	else if (cifs_min_small > 256) {
1351 		cifs_min_small = 256;
1352 		cifs_dbg(FYI, "cifs_min_small set to maximum (256)\n");
1353 	}
1354 
1355 	cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
1356 						     cifs_sm_req_cachep);
1357 
1358 	if (cifs_sm_req_poolp == NULL) {
1359 		mempool_destroy(cifs_req_poolp);
1360 		kmem_cache_destroy(cifs_req_cachep);
1361 		kmem_cache_destroy(cifs_sm_req_cachep);
1362 		return -ENOMEM;
1363 	}
1364 
1365 	return 0;
1366 }
1367 
1368 static void
cifs_destroy_request_bufs(void)1369 cifs_destroy_request_bufs(void)
1370 {
1371 	mempool_destroy(cifs_req_poolp);
1372 	kmem_cache_destroy(cifs_req_cachep);
1373 	mempool_destroy(cifs_sm_req_poolp);
1374 	kmem_cache_destroy(cifs_sm_req_cachep);
1375 }
1376 
1377 static int
cifs_init_mids(void)1378 cifs_init_mids(void)
1379 {
1380 	cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
1381 					    sizeof(struct mid_q_entry), 0,
1382 					    SLAB_HWCACHE_ALIGN, NULL);
1383 	if (cifs_mid_cachep == NULL)
1384 		return -ENOMEM;
1385 
1386 	/* 3 is a reasonable minimum number of simultaneous operations */
1387 	cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
1388 	if (cifs_mid_poolp == NULL) {
1389 		kmem_cache_destroy(cifs_mid_cachep);
1390 		return -ENOMEM;
1391 	}
1392 
1393 	return 0;
1394 }
1395 
1396 static void
cifs_destroy_mids(void)1397 cifs_destroy_mids(void)
1398 {
1399 	mempool_destroy(cifs_mid_poolp);
1400 	kmem_cache_destroy(cifs_mid_cachep);
1401 }
1402 
1403 static int __init
init_cifs(void)1404 init_cifs(void)
1405 {
1406 	int rc = 0;
1407 	cifs_proc_init();
1408 	INIT_LIST_HEAD(&cifs_tcp_ses_list);
1409 #ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* unused temporarily */
1410 	INIT_LIST_HEAD(&GlobalDnotifyReqList);
1411 	INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
1412 #endif /* was needed for dnotify, and will be needed for inotify when VFS fix */
1413 /*
1414  *  Initialize Global counters
1415  */
1416 	atomic_set(&sesInfoAllocCount, 0);
1417 	atomic_set(&tconInfoAllocCount, 0);
1418 	atomic_set(&tcpSesAllocCount, 0);
1419 	atomic_set(&tcpSesReconnectCount, 0);
1420 	atomic_set(&tconInfoReconnectCount, 0);
1421 
1422 	atomic_set(&bufAllocCount, 0);
1423 	atomic_set(&smBufAllocCount, 0);
1424 #ifdef CONFIG_CIFS_STATS2
1425 	atomic_set(&totBufAllocCount, 0);
1426 	atomic_set(&totSmBufAllocCount, 0);
1427 #endif /* CONFIG_CIFS_STATS2 */
1428 
1429 	atomic_set(&midCount, 0);
1430 	GlobalCurrentXid = 0;
1431 	GlobalTotalActiveXid = 0;
1432 	GlobalMaxActiveXid = 0;
1433 	spin_lock_init(&cifs_tcp_ses_lock);
1434 	spin_lock_init(&GlobalMid_Lock);
1435 
1436 	cifs_lock_secret = get_random_u32();
1437 
1438 	if (cifs_max_pending < 2) {
1439 		cifs_max_pending = 2;
1440 		cifs_dbg(FYI, "cifs_max_pending set to min of 2\n");
1441 	} else if (cifs_max_pending > CIFS_MAX_REQ) {
1442 		cifs_max_pending = CIFS_MAX_REQ;
1443 		cifs_dbg(FYI, "cifs_max_pending set to max of %u\n",
1444 			 CIFS_MAX_REQ);
1445 	}
1446 
1447 	cifsiod_wq = alloc_workqueue("cifsiod", WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1448 	if (!cifsiod_wq) {
1449 		rc = -ENOMEM;
1450 		goto out_clean_proc;
1451 	}
1452 
1453 	cifsoplockd_wq = alloc_workqueue("cifsoplockd",
1454 					 WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1455 	if (!cifsoplockd_wq) {
1456 		rc = -ENOMEM;
1457 		goto out_destroy_cifsiod_wq;
1458 	}
1459 
1460 	rc = cifs_fscache_register();
1461 	if (rc)
1462 		goto out_destroy_cifsoplockd_wq;
1463 
1464 	rc = cifs_init_inodecache();
1465 	if (rc)
1466 		goto out_unreg_fscache;
1467 
1468 	rc = cifs_init_mids();
1469 	if (rc)
1470 		goto out_destroy_inodecache;
1471 
1472 	rc = cifs_init_request_bufs();
1473 	if (rc)
1474 		goto out_destroy_mids;
1475 
1476 #ifdef CONFIG_CIFS_UPCALL
1477 	rc = init_cifs_spnego();
1478 	if (rc)
1479 		goto out_destroy_request_bufs;
1480 #endif /* CONFIG_CIFS_UPCALL */
1481 
1482 #ifdef CONFIG_CIFS_ACL
1483 	rc = init_cifs_idmap();
1484 	if (rc)
1485 		goto out_register_key_type;
1486 #endif /* CONFIG_CIFS_ACL */
1487 
1488 	rc = register_filesystem(&cifs_fs_type);
1489 	if (rc)
1490 		goto out_init_cifs_idmap;
1491 
1492 	rc = register_filesystem(&smb3_fs_type);
1493 	if (rc) {
1494 		unregister_filesystem(&cifs_fs_type);
1495 		goto out_init_cifs_idmap;
1496 	}
1497 
1498 	return 0;
1499 
1500 out_init_cifs_idmap:
1501 #ifdef CONFIG_CIFS_ACL
1502 	exit_cifs_idmap();
1503 out_register_key_type:
1504 #endif
1505 #ifdef CONFIG_CIFS_UPCALL
1506 	exit_cifs_spnego();
1507 out_destroy_request_bufs:
1508 #endif
1509 	cifs_destroy_request_bufs();
1510 out_destroy_mids:
1511 	cifs_destroy_mids();
1512 out_destroy_inodecache:
1513 	cifs_destroy_inodecache();
1514 out_unreg_fscache:
1515 	cifs_fscache_unregister();
1516 out_destroy_cifsoplockd_wq:
1517 	destroy_workqueue(cifsoplockd_wq);
1518 out_destroy_cifsiod_wq:
1519 	destroy_workqueue(cifsiod_wq);
1520 out_clean_proc:
1521 	cifs_proc_clean();
1522 	return rc;
1523 }
1524 
1525 static void __exit
exit_cifs(void)1526 exit_cifs(void)
1527 {
1528 	cifs_dbg(NOISY, "exit_smb3\n");
1529 	unregister_filesystem(&cifs_fs_type);
1530 	unregister_filesystem(&smb3_fs_type);
1531 	cifs_dfs_release_automount_timer();
1532 #ifdef CONFIG_CIFS_ACL
1533 	exit_cifs_idmap();
1534 #endif
1535 #ifdef CONFIG_CIFS_UPCALL
1536 	exit_cifs_spnego();
1537 #endif
1538 	cifs_destroy_request_bufs();
1539 	cifs_destroy_mids();
1540 	cifs_destroy_inodecache();
1541 	cifs_fscache_unregister();
1542 	destroy_workqueue(cifsoplockd_wq);
1543 	destroy_workqueue(cifsiod_wq);
1544 	cifs_proc_clean();
1545 }
1546 
1547 MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
1548 MODULE_LICENSE("GPL");	/* combination of LGPL + GPL source behaves as GPL */
1549 MODULE_DESCRIPTION
1550     ("VFS to access servers complying with the SNIA CIFS Specification "
1551      "e.g. Samba and Windows");
1552 MODULE_VERSION(CIFS_VERSION);
1553 MODULE_SOFTDEP("pre: arc4");
1554 MODULE_SOFTDEP("pre: des");
1555 MODULE_SOFTDEP("pre: ecb");
1556 MODULE_SOFTDEP("pre: hmac");
1557 MODULE_SOFTDEP("pre: md4");
1558 MODULE_SOFTDEP("pre: md5");
1559 MODULE_SOFTDEP("pre: nls");
1560 MODULE_SOFTDEP("pre: aes");
1561 MODULE_SOFTDEP("pre: cmac");
1562 MODULE_SOFTDEP("pre: sha256");
1563 MODULE_SOFTDEP("pre: sha512");
1564 MODULE_SOFTDEP("pre: aead2");
1565 MODULE_SOFTDEP("pre: ccm");
1566 module_init(init_cifs)
1567 module_exit(exit_cifs)
1568