cifsfs.c 40.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 *   fs/cifs/cifsfs.c
 *
4
 *   Copyright (C) International Business Machines  Corp., 2002,2008
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *   Author(s): Steve French (sfrench@us.ibm.com)
 *
 *   Common Internet FileSystem (CIFS) client
 *
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

/* Note that BB means BUGBUG (ie something to fix eventually) */

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/mount.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/seq_file.h>
#include <linux/vfs.h>
#include <linux/mempool.h>
35
#include <linux/delay.h>
36
#include <linux/kthread.h>
37
#include <linux/freezer.h>
A
Al Viro 已提交
38
#include <linux/namei.h>
P
Pavel Shilovsky 已提交
39
#include <linux/random.h>
40
#include <linux/uuid.h>
41
#include <linux/xattr.h>
42
#include <net/ipv6.h>
L
Linus Torvalds 已提交
43 44 45 46 47 48 49 50
#include "cifsfs.h"
#include "cifspdu.h"
#define DECLARE_GLOBALS_HERE
#include "cifsglob.h"
#include "cifsproto.h"
#include "cifs_debug.h"
#include "cifs_fs_sb.h"
#include <linux/mm.h>
51
#include <linux/key-type.h>
52
#include "cifs_spnego.h"
53
#include "fscache.h"
54
#include "smb2pdu.h"
L
Linus Torvalds 已提交
55 56

int cifsFYI = 0;
57
bool traceSMB;
58
bool enable_oplocks = true;
59 60
bool linuxExtEnabled = true;
bool lookupCacheEnabled = true;
61
bool disable_legacy_dialects; /* false by default */
62
unsigned int global_secflags = CIFSSEC_DEF;
63
/* unsigned int ntlmv2_support = 0; */
L
Linus Torvalds 已提交
64
unsigned int sign_CIFS_PDUs = 1;
65
static const struct super_operations cifs_super_ops;
L
Linus Torvalds 已提交
66
unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
67
module_param(CIFSMaxBufSize, uint, 0444);
68 69
MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header) "
				 "for CIFS requests. "
70
				 "Default: 16384 Range: 8192 to 130048");
L
Linus Torvalds 已提交
71
unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
72
module_param(cifs_min_rcv, uint, 0444);
73 74
MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
				"1 to 64");
L
Linus Torvalds 已提交
75
unsigned int cifs_min_small = 30;
76
module_param(cifs_min_small, uint, 0444);
77 78
MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
				 "Range: 2 to 256");
L
Linus Torvalds 已提交
79
unsigned int cifs_max_pending = CIFS_MAX_REQ;
J
Jeff Layton 已提交
80
module_param(cifs_max_pending, uint, 0444);
81 82
MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server for "
				   "CIFS/SMB1 dialect (N/A for SMB3) "
83
				   "Default: 32767 Range: 2 to 32767.");
84
module_param(enable_oplocks, bool, 0644);
J
Jeff Layton 已提交
85
MODULE_PARM_DESC(enable_oplocks, "Enable or disable oplocks. Default: y/Y/1");
86

87 88 89 90 91 92 93 94 95
module_param(disable_legacy_dialects, bool, 0644);
MODULE_PARM_DESC(disable_legacy_dialects, "To improve security it may be "
				  "helpful to restrict the ability to "
				  "override the default dialects (SMB2.1, "
				  "SMB3 and SMB3.02) on mount with old "
				  "dialects (CIFS/SMB1 and SMB2) since "
				  "vers=1.0 (CIFS/SMB1) and vers=2.0 are weaker"
				  " and less secure. Default: n/N/0");

L
Linus Torvalds 已提交
96 97 98 99
extern mempool_t *cifs_sm_req_poolp;
extern mempool_t *cifs_req_poolp;
extern mempool_t *cifs_mid_poolp;

J
Jeff Layton 已提交
100
struct workqueue_struct	*cifsiod_wq;
R
Rabin Vincent 已提交
101
struct workqueue_struct	*cifsoplockd_wq;
102
__u32 cifs_lock_secret;
J
Jeff Layton 已提交
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
/*
 * Bumps refcount for cifs super block.
 * Note that it should be only called if a referece to VFS super block is
 * already held, e.g. in open-type syscalls context. Otherwise it can race with
 * atomic_dec_and_test in deactivate_locked_super.
 */
void
cifs_sb_active(struct super_block *sb)
{
	struct cifs_sb_info *server = CIFS_SB(sb);

	if (atomic_inc_return(&server->active) == 1)
		atomic_inc(&sb->s_active);
}

void
cifs_sb_deactive(struct super_block *sb)
{
	struct cifs_sb_info *server = CIFS_SB(sb);

	if (atomic_dec_and_test(&server->active))
		deactivate_super(sb);
}

L
Linus Torvalds 已提交
128
static int
A
Al Viro 已提交
129
cifs_read_super(struct super_block *sb)
L
Linus Torvalds 已提交
130 131
{
	struct inode *inode;
132
	struct cifs_sb_info *cifs_sb;
133
	struct cifs_tcon *tcon;
L
Linus Torvalds 已提交
134
	int rc = 0;
135

136
	cifs_sb = CIFS_SB(sb);
137
	tcon = cifs_sb_master_tcon(cifs_sb);
L
Linus Torvalds 已提交
138

139
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIXACL)
140
		sb->s_flags |= SB_POSIXACL;
141

142
	if (tcon->ses->capabilities & tcon->ses->server->vals->cap_large_files)
143 144 145 146 147 148 149
		sb->s_maxbytes = MAX_LFS_FILESIZE;
	else
		sb->s_maxbytes = MAX_NON_LFS;

	/* BB FIXME fix time_gran to be larger for LANMAN sessions */
	sb->s_time_gran = 100;

L
Linus Torvalds 已提交
150 151
	sb->s_magic = CIFS_MAGIC_NUMBER;
	sb->s_op = &cifs_super_ops;
152
	sb->s_xattr = cifs_xattr_handlers;
153 154 155 156 157 158
	rc = super_setup_bdi(sb);
	if (rc)
		goto out_no_root;
	/* tune readahead according to rsize */
	sb->s_bdi->ra_pages = cifs_sb->rsize / PAGE_SIZE;

L
Linus Torvalds 已提交
159 160
	sb->s_blocksize = CIFS_MAX_MSGSIZE;
	sb->s_blocksize_bits = 14;	/* default 2**14 = CIFS_MAX_MSGSIZE */
161
	inode = cifs_root_iget(sb);
L
Linus Torvalds 已提交
162

163 164
	if (IS_ERR(inode)) {
		rc = PTR_ERR(inode);
L
Linus Torvalds 已提交
165 166 167
		goto out_no_root;
	}

168
	if (tcon->nocase)
169 170 171 172
		sb->s_d_op = &cifs_ci_dentry_ops;
	else
		sb->s_d_op = &cifs_dentry_ops;

173
	sb->s_root = d_make_root(inode);
L
Linus Torvalds 已提交
174 175 176 177
	if (!sb->s_root) {
		rc = -ENOMEM;
		goto out_no_root;
	}
178

P
Paul Bolle 已提交
179
#ifdef CONFIG_CIFS_NFSD_EXPORT
180
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
181
		cifs_dbg(FYI, "export ops supported\n");
182 183
		sb->s_export_op = &cifs_export_ops;
	}
P
Paul Bolle 已提交
184
#endif /* CONFIG_CIFS_NFSD_EXPORT */
L
Linus Torvalds 已提交
185 186 187 188

	return 0;

out_no_root:
189
	cifs_dbg(VFS, "%s: get root inode failed\n", __func__);
L
Linus Torvalds 已提交
190 191 192
	return rc;
}

A
Al Viro 已提交
193 194 195 196
static void cifs_kill_sb(struct super_block *sb)
{
	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
	kill_anon_super(sb);
197
	cifs_umount(cifs_sb);
L
Linus Torvalds 已提交
198 199 200
}

static int
201
cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
L
Linus Torvalds 已提交
202
{
203
	struct super_block *sb = dentry->d_sb;
S
Steve French 已提交
204
	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
205
	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
206
	struct TCP_Server_Info *server = tcon->ses->server;
207
	unsigned int xid;
208
	int rc = 0;
L
Linus Torvalds 已提交
209

210
	xid = get_xid();
L
Linus Torvalds 已提交
211

S
Steve French 已提交
212 213 214 215 216 217 218 219
	/*
	 * PATH_MAX may be too long - it would presumably be total path,
	 * but note that some servers (includinng Samba 3) have a shorter
	 * maximum path.
	 *
	 * Instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO.
	 */
	buf->f_namelen = PATH_MAX;
L
Linus Torvalds 已提交
220 221 222
	buf->f_files = 0;	/* undefined */
	buf->f_ffree = 0;	/* unlimited */

223 224
	if (server->ops->queryfs)
		rc = server->ops->queryfs(xid, tcon, buf);
S
Steve French 已提交
225

226
	free_xid(xid);
S
Steve French 已提交
227
	return 0;
L
Linus Torvalds 已提交
228 229
}

230 231
static long cifs_fallocate(struct file *file, int mode, loff_t off, loff_t len)
{
232
	struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
233 234 235 236 237 238 239 240 241
	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
	struct TCP_Server_Info *server = tcon->ses->server;

	if (server->ops->fallocate)
		return server->ops->fallocate(file, tcon, mode, off, len);

	return -EOPNOTSUPP;
}

242
static int cifs_permission(struct inode *inode, int mask)
L
Linus Torvalds 已提交
243 244 245 246 247
{
	struct cifs_sb_info *cifs_sb;

	cifs_sb = CIFS_SB(inode->i_sb);

248 249 250 251 252 253
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
		if ((mask & MAY_EXEC) && !execute_ok(inode))
			return -EACCES;
		else
			return 0;
	} else /* file mode might have been restricted at mount time
254
		on the client (above and beyond ACL on servers) for
L
Linus Torvalds 已提交
255
		servers which do not support setting and viewing mode bits,
256
		so allowing client to check permissions is useful */
257
		return generic_permission(inode, mask);
L
Linus Torvalds 已提交
258 259
}

260 261 262 263
static struct kmem_cache *cifs_inode_cachep;
static struct kmem_cache *cifs_req_cachep;
static struct kmem_cache *cifs_mid_cachep;
static struct kmem_cache *cifs_sm_req_cachep;
L
Linus Torvalds 已提交
264 265 266 267 268 269 270 271
mempool_t *cifs_sm_req_poolp;
mempool_t *cifs_req_poolp;
mempool_t *cifs_mid_poolp;

static struct inode *
cifs_alloc_inode(struct super_block *sb)
{
	struct cifsInodeInfo *cifs_inode;
272
	cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
L
Linus Torvalds 已提交
273 274 275 276
	if (!cifs_inode)
		return NULL;
	cifs_inode->cifsAttrs = 0x20;	/* default */
	cifs_inode->time = 0;
P
Pavel Shilovsky 已提交
277 278 279 280
	/*
	 * Until the file is open and we have gotten oplock info back from the
	 * server, can not assume caching of file data or metadata.
	 */
281
	cifs_set_oplock_level(cifs_inode, 0);
282
	cifs_inode->flags = 0;
283 284
	spin_lock_init(&cifs_inode->writers_lock);
	cifs_inode->writers = 0;
L
Linus Torvalds 已提交
285
	cifs_inode->vfs_inode.i_blkbits = 14;  /* 2**14 = CIFS_MAX_MSGSIZE */
286
	cifs_inode->server_eof = 0;
287 288
	cifs_inode->uniqueid = 0;
	cifs_inode->createtime = 0;
289
	cifs_inode->epoch = 0;
290
	generate_random_uuid(cifs_inode->lease_key);
291

P
Pavel Shilovsky 已提交
292 293 294 295 296
	/*
	 * Can not set i_flags here - they get immediately overwritten to zero
	 * by the VFS.
	 */
	/* cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME; */
L
Linus Torvalds 已提交
297
	INIT_LIST_HEAD(&cifs_inode->openFileList);
298
	INIT_LIST_HEAD(&cifs_inode->llist);
L
Linus Torvalds 已提交
299 300 301
	return &cifs_inode->vfs_inode;
}

N
Nick Piggin 已提交
302 303 304 305 306 307
static void cifs_i_callback(struct rcu_head *head)
{
	struct inode *inode = container_of(head, struct inode, i_rcu);
	kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
}

L
Linus Torvalds 已提交
308 309 310
static void
cifs_destroy_inode(struct inode *inode)
{
N
Nick Piggin 已提交
311
	call_rcu(&inode->i_rcu, cifs_i_callback);
L
Linus Torvalds 已提交
312 313
}

314
static void
315
cifs_evict_inode(struct inode *inode)
316
{
317
	truncate_inode_pages_final(&inode->i_data);
318
	clear_inode(inode);
319 320 321
	cifs_fscache_release_inode_cookie(inode);
}

322 323 324
static void
cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
{
325 326 327
	struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
	struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;

328
	seq_puts(s, ",addr=");
329

330
	switch (server->dstaddr.ss_family) {
331
	case AF_INET:
332
		seq_printf(s, "%pI4", &sa->sin_addr.s_addr);
333 334
		break;
	case AF_INET6:
335 336 337
		seq_printf(s, "%pI6", &sa6->sin6_addr.s6_addr);
		if (sa6->sin6_scope_id)
			seq_printf(s, "%%%u", sa6->sin6_scope_id);
338 339
		break;
	default:
340
		seq_puts(s, "(unknown)");
341
	}
L
Long Li 已提交
342 343
	if (server->rdma)
		seq_puts(s, ",rdma");
344 345
}

346
static void
347
cifs_show_security(struct seq_file *s, struct cifs_ses *ses)
348
{
349 350 351
	if (ses->sectype == Unspecified) {
		if (ses->user_name == NULL)
			seq_puts(s, ",sec=none");
352
		return;
353
	}
354

355
	seq_puts(s, ",sec=");
356

357
	switch (ses->sectype) {
358
	case LANMAN:
359
		seq_puts(s, "lanman");
360 361
		break;
	case NTLMv2:
362
		seq_puts(s, "ntlmv2");
363 364
		break;
	case NTLM:
365
		seq_puts(s, "ntlm");
366 367
		break;
	case Kerberos:
368
		seq_puts(s, "krb5");
369 370
		break;
	case RawNTLMSSP:
371
		seq_puts(s, "ntlmssp");
372 373 374
		break;
	default:
		/* shouldn't ever happen */
375
		seq_puts(s, "unknown");
376 377 378
		break;
	}

379
	if (ses->sign)
380
		seq_puts(s, "i");
381 382
}

383 384 385
static void
cifs_show_cache_flavor(struct seq_file *s, struct cifs_sb_info *cifs_sb)
{
386
	seq_puts(s, ",cache=");
387 388

	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
389
		seq_puts(s, "strict");
390
	else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
391
		seq_puts(s, "none");
392
	else
393
		seq_puts(s, "loose");
394 395
}

396 397 398 399 400 401 402 403 404 405 406 407
static void
cifs_show_nls(struct seq_file *s, struct nls_table *cur)
{
	struct nls_table *def;

	/* Display iocharset= option if it's not default charset */
	def = load_nls_default();
	if (def != cur)
		seq_printf(s, ",iocharset=%s", cur->charset);
	unload_nls(def);
}

L
Linus Torvalds 已提交
408 409 410 411 412 413
/*
 * cifs_show_options() is for displaying mount options in /proc/mounts.
 * Not all settable options are displayed but most of the important
 * ones are.
 */
static int
414
cifs_show_options(struct seq_file *s, struct dentry *root)
L
Linus Torvalds 已提交
415
{
416
	struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
417
	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
418 419
	struct sockaddr *srcaddr;
	srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
420

421
	seq_show_option(s, "vers", tcon->ses->server->vals->version_string);
422
	cifs_show_security(s, tcon->ses);
423
	cifs_show_cache_flavor(s, cifs_sb);
424

425
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)
426
		seq_puts(s, ",multiuser");
427
	else if (tcon->ses->user_name)
428
		seq_show_option(s, "username", tcon->ses->user_name);
429

430
	if (tcon->ses->domainName)
431
		seq_show_option(s, "domain", tcon->ses->domainName);
432

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
	if (srcaddr->sa_family != AF_UNSPEC) {
		struct sockaddr_in *saddr4;
		struct sockaddr_in6 *saddr6;
		saddr4 = (struct sockaddr_in *)srcaddr;
		saddr6 = (struct sockaddr_in6 *)srcaddr;
		if (srcaddr->sa_family == AF_INET6)
			seq_printf(s, ",srcaddr=%pI6c",
				   &saddr6->sin6_addr);
		else if (srcaddr->sa_family == AF_INET)
			seq_printf(s, ",srcaddr=%pI4",
				   &saddr4->sin_addr.s_addr);
		else
			seq_printf(s, ",srcaddr=BAD-AF:%i",
				   (int)(srcaddr->sa_family));
	}

449 450
	seq_printf(s, ",uid=%u",
		   from_kuid_munged(&init_user_ns, cifs_sb->mnt_uid));
451
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
452
		seq_puts(s, ",forceuid");
453
	else
454
		seq_puts(s, ",noforceuid");
455

456 457
	seq_printf(s, ",gid=%u",
		   from_kgid_munged(&init_user_ns, cifs_sb->mnt_gid));
458
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
459
		seq_puts(s, ",forcegid");
460
	else
461
		seq_puts(s, ",noforcegid");
462

463
	cifs_show_address(s, tcon->ses->server);
L
Linus Torvalds 已提交
464

465
	if (!tcon->unix_ext)
A
Al Viro 已提交
466
		seq_printf(s, ",file_mode=0%ho,dir_mode=0%ho",
467 468
					   cifs_sb->mnt_file_mode,
					   cifs_sb->mnt_dir_mode);
469 470 471

	cifs_show_nls(s, cifs_sb->local_nls);

472
	if (tcon->seal)
473
		seq_puts(s, ",seal");
474
	if (tcon->nocase)
475
		seq_puts(s, ",nocase");
476
	if (tcon->retry)
477
		seq_puts(s, ",hard");
478 479
	else
		seq_puts(s, ",soft");
480 481
	if (tcon->use_persistent)
		seq_puts(s, ",persistenthandles");
S
Steve French 已提交
482 483
	else if (tcon->use_resilient)
		seq_puts(s, ",resilienthandles");
484
	if (tcon->unix_ext)
485
		seq_puts(s, ",unix");
486
	else
487
		seq_puts(s, ",nounix");
488
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
489
		seq_puts(s, ",posixpaths");
490
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
491
		seq_puts(s, ",setuids");
492 493
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
		seq_puts(s, ",idsfromsid");
494
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
495
		seq_puts(s, ",serverino");
496
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
497
		seq_puts(s, ",rwpidforward");
498
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL)
499
		seq_puts(s, ",forcemand");
500
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
501
		seq_puts(s, ",nouser_xattr");
502
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
503
		seq_puts(s, ",mapchars");
504 505
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SFM_CHR)
		seq_puts(s, ",mapposix");
506
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
507
		seq_puts(s, ",sfu");
508
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
509
		seq_puts(s, ",nobrl");
S
Steve French 已提交
510 511
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_HANDLE_CACHE)
		seq_puts(s, ",nohandlecache");
512
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
513
		seq_puts(s, ",cifsacl");
514
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
515
		seq_puts(s, ",dynperm");
516
	if (root->d_sb->s_flags & SB_POSIXACL)
517
		seq_puts(s, ",acl");
518
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
519
		seq_puts(s, ",mfsymlinks");
520
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
521
		seq_puts(s, ",fsc");
522
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)
523
		seq_puts(s, ",nostrictsync");
524
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
525
		seq_puts(s, ",noperm");
526
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID)
527 528 529
		seq_printf(s, ",backupuid=%u",
			   from_kuid_munged(&init_user_ns,
					    cifs_sb->mnt_backupuid));
530
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID)
531 532 533
		seq_printf(s, ",backupgid=%u",
			   from_kgid_munged(&init_user_ns,
					    cifs_sb->mnt_backupgid));
534

535 536
	seq_printf(s, ",rsize=%u", cifs_sb->rsize);
	seq_printf(s, ",wsize=%u", cifs_sb->wsize);
S
Steve French 已提交
537 538
	seq_printf(s, ",echo_interval=%lu",
			tcon->ses->server->echo_interval / HZ);
539
	/* convert actimeo and display it in seconds */
540
	seq_printf(s, ",actimeo=%lu", cifs_sb->actimeo / HZ);
541

L
Linus Torvalds 已提交
542 543 544
	return 0;
}

545
static void cifs_umount_begin(struct super_block *sb)
546
{
547
	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
548
	struct cifs_tcon *tcon;
549

550
	if (cifs_sb == NULL)
551 552
		return;

553
	tcon = cifs_sb_master_tcon(cifs_sb);
554

555
	spin_lock(&cifs_tcp_ses_lock);
556 557 558 559
	if ((tcon->tc_count > 1) || (tcon->tidStatus == CifsExiting)) {
		/* we have other mounts to same share or we have
		   already tried to force umount this and woken up
		   all waiting network requests, nothing to do */
560
		spin_unlock(&cifs_tcp_ses_lock);
561 562
		return;
	} else if (tcon->tc_count == 1)
563
		tcon->tidStatus = CifsExiting;
564
	spin_unlock(&cifs_tcp_ses_lock);
565

566
	/* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
S
Steve French 已提交
567
	/* cancel_notify_requests(tcon); */
568
	if (tcon->ses && tcon->ses->server) {
569
		cifs_dbg(FYI, "wake up tasks now - umount begin not complete\n");
570
		wake_up_all(&tcon->ses->server->request_q);
571 572 573 574 575
		wake_up_all(&tcon->ses->server->response_q);
		msleep(1); /* yield */
		/* we have to kick the requests once more */
		wake_up_all(&tcon->ses->server->response_q);
		msleep(1);
576
	}
577 578 579 580

	return;
}

581
#ifdef CONFIG_CIFS_STATS2
582
static int cifs_show_stats(struct seq_file *s, struct dentry *root)
583 584 585 586 587 588
{
	/* BB FIXME */
	return 0;
}
#endif

L
Linus Torvalds 已提交
589 590
static int cifs_remount(struct super_block *sb, int *flags, char *data)
{
591
	sync_filesystem(sb);
592
	*flags |= SB_NODIRATIME;
L
Linus Torvalds 已提交
593 594 595
	return 0;
}

596
static int cifs_drop_inode(struct inode *inode)
597 598 599
{
	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);

600 601 602
	/* no serverino => unconditional eviction */
	return !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) ||
		generic_drop_inode(inode);
603 604
}

605
static const struct super_operations cifs_super_ops = {
L
Linus Torvalds 已提交
606 607 608
	.statfs = cifs_statfs,
	.alloc_inode = cifs_alloc_inode,
	.destroy_inode = cifs_destroy_inode,
609
	.drop_inode	= cifs_drop_inode,
610
	.evict_inode	= cifs_evict_inode,
611 612
/*	.delete_inode	= cifs_delete_inode,  */  /* Do not need above
	function unless later we add lazy close of inodes or unless the
613 614
	kernel forgets to call us with the same number of releases (closes)
	as opens */
L
Linus Torvalds 已提交
615
	.show_options = cifs_show_options,
S
Steve French 已提交
616
	.umount_begin   = cifs_umount_begin,
L
Linus Torvalds 已提交
617
	.remount_fs = cifs_remount,
618
#ifdef CONFIG_CIFS_STATS2
619
	.show_stats = cifs_show_stats,
620
#endif
L
Linus Torvalds 已提交
621 622
};

623 624 625 626 627 628 629
/*
 * Get root dentry from superblock according to prefix path mount option.
 * Return dentry with refcount + 1 on success and NULL otherwise.
 */
static struct dentry *
cifs_get_root(struct smb_vol *vol, struct super_block *sb)
{
A
Al Viro 已提交
630
	struct dentry *dentry;
631
	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
A
Al Viro 已提交
632 633
	char *full_path = NULL;
	char *s, *p;
634 635
	char sep;

636 637 638
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
		return dget(sb->s_root);

639
	full_path = cifs_build_path_to_root(vol, cifs_sb,
640
				cifs_sb_master_tcon(cifs_sb), 0);
641
	if (full_path == NULL)
642
		return ERR_PTR(-ENOMEM);
643

644
	cifs_dbg(FYI, "Get root dentry for %s\n", full_path);
645 646

	sep = CIFS_DIR_SEP(cifs_sb);
A
Al Viro 已提交
647 648 649 650
	dentry = dget(sb->s_root);
	p = s = full_path;

	do {
651
		struct inode *dir = d_inode(dentry);
A
Al Viro 已提交
652 653
		struct dentry *child;

654 655 656 657 658
		if (!dir) {
			dput(dentry);
			dentry = ERR_PTR(-ENOENT);
			break;
		}
659 660 661 662 663
		if (!S_ISDIR(dir->i_mode)) {
			dput(dentry);
			dentry = ERR_PTR(-ENOTDIR);
			break;
		}
664

A
Al Viro 已提交
665 666 667 668 669 670 671 672 673 674
		/* skip separators */
		while (*s == sep)
			s++;
		if (!*s)
			break;
		p = s++;
		/* next separator */
		while (*s && *s != sep)
			s++;

675
		child = lookup_one_len_unlocked(p, dentry, s - p);
A
Al Viro 已提交
676 677 678
		dput(dentry);
		dentry = child;
	} while (!IS_ERR(dentry));
679
	kfree(full_path);
A
Al Viro 已提交
680
	return dentry;
681 682
}

A
Al Viro 已提交
683 684 685 686 687 688 689
static int cifs_set_super(struct super_block *sb, void *data)
{
	struct cifs_mnt_data *mnt_data = data;
	sb->s_fs_info = mnt_data->cifs_sb;
	return set_anon_super(sb, NULL);
}

A
Al Viro 已提交
690 691
static struct dentry *
cifs_do_mount(struct file_system_type *fs_type,
692
	      int flags, const char *dev_name, void *data)
L
Linus Torvalds 已提交
693 694
{
	int rc;
695
	struct super_block *sb;
696 697
	struct cifs_sb_info *cifs_sb;
	struct smb_vol *volume_info;
698
	struct cifs_mnt_data mnt_data;
699
	struct dentry *root;
L
Linus Torvalds 已提交
700

701
	cifs_dbg(FYI, "Devname: %s flags: %d\n", dev_name, flags);
L
Linus Torvalds 已提交
702

703 704 705
	volume_info = cifs_get_volume_info((char *)data, dev_name);
	if (IS_ERR(volume_info))
		return ERR_CAST(volume_info);
706 707 708 709

	cifs_sb = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
	if (cifs_sb == NULL) {
		root = ERR_PTR(-ENOMEM);
A
Al Viro 已提交
710
		goto out_nls;
711 712
	}

A
Al Viro 已提交
713 714 715
	cifs_sb->mountdata = kstrndup(data, PAGE_SIZE, GFP_KERNEL);
	if (cifs_sb->mountdata == NULL) {
		root = ERR_PTR(-ENOMEM);
716
		goto out_free;
A
Al Viro 已提交
717 718
	}

719 720 721 722
	rc = cifs_setup_cifs_sb(volume_info, cifs_sb);
	if (rc) {
		root = ERR_PTR(rc);
		goto out_free;
723 724
	}

A
Al Viro 已提交
725 726
	rc = cifs_mount(cifs_sb, volume_info);
	if (rc) {
727
		if (!(flags & SB_SILENT))
728 729
			cifs_dbg(VFS, "cifs_mount failed w/return code = %d\n",
				 rc);
A
Al Viro 已提交
730
		root = ERR_PTR(rc);
731
		goto out_free;
A
Al Viro 已提交
732 733
	}

734 735 736 737
	mnt_data.vol = volume_info;
	mnt_data.cifs_sb = cifs_sb;
	mnt_data.flags = flags;

D
David Howells 已提交
738
	/* BB should we make this contingent on mount parm? */
739
	flags |= SB_NODIRATIME | SB_NOATIME;
D
David Howells 已提交
740 741

	sb = sget(fs_type, cifs_match_super, cifs_set_super, flags, &mnt_data);
742 743
	if (IS_ERR(sb)) {
		root = ERR_CAST(sb);
A
Al Viro 已提交
744
		cifs_umount(cifs_sb);
745
		goto out;
746
	}
L
Linus Torvalds 已提交
747

A
Al Viro 已提交
748
	if (sb->s_root) {
749
		cifs_dbg(FYI, "Use existing superblock\n");
A
Al Viro 已提交
750
		cifs_umount(cifs_sb);
A
Al Viro 已提交
751 752 753 754 755 756
	} else {
		rc = cifs_read_super(sb);
		if (rc) {
			root = ERR_PTR(rc);
			goto out_super;
		}
757

758
		sb->s_flags |= SB_ACTIVE;
L
Linus Torvalds 已提交
759
	}
760

761
	root = cifs_get_root(volume_info, sb);
762
	if (IS_ERR(root))
763
		goto out_super;
764

765
	cifs_dbg(FYI, "dentry root is: %p\n", root);
766
	goto out;
767

768 769 770
out_super:
	deactivate_locked_super(sb);
out:
771
	cifs_cleanup_volume_info(volume_info);
772
	return root;
A
Al Viro 已提交
773

774 775
out_free:
	kfree(cifs_sb->prepath);
A
Al Viro 已提交
776 777 778 779 780
	kfree(cifs_sb->mountdata);
	kfree(cifs_sb);
out_nls:
	unload_nls(volume_info->local_nls);
	goto out;
L
Linus Torvalds 已提交
781 782
}

783 784 785 786 787 788
static ssize_t
cifs_loose_read_iter(struct kiocb *iocb, struct iov_iter *iter)
{
	ssize_t rc;
	struct inode *inode = file_inode(iocb->ki_filp);

789 790 791
	if (iocb->ki_filp->f_flags & O_DIRECT)
		return cifs_user_readv(iocb, iter);

792 793 794 795 796 797 798
	rc = cifs_revalidate_mapping(inode);
	if (rc)
		return rc;

	return generic_file_read_iter(iocb, iter);
}

A
Al Viro 已提交
799
static ssize_t cifs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
L
Linus Torvalds 已提交
800
{
A
Al Viro 已提交
801
	struct inode *inode = file_inode(iocb->ki_filp);
802
	struct cifsInodeInfo *cinode = CIFS_I(inode);
L
Linus Torvalds 已提交
803
	ssize_t written;
804
	int rc;
L
Linus Torvalds 已提交
805

806 807 808 809 810 811 812 813 814 815 816 817
	if (iocb->ki_filp->f_flags & O_DIRECT) {
		written = cifs_user_writev(iocb, from);
		if (written > 0 && CIFS_CACHE_READ(cinode)) {
			cifs_zap_mapping(inode);
			cifs_dbg(FYI,
				 "Set no oplock for inode=%p after a write operation\n",
				 inode);
			cinode->oplock = 0;
		}
		return written;
	}

818 819 820 821
	written = cifs_get_writer(cinode);
	if (written)
		return written;

A
Al Viro 已提交
822
	written = generic_file_write_iter(iocb, from);
823

824
	if (CIFS_CACHE_WRITE(CIFS_I(inode)))
825
		goto out;
826 827 828

	rc = filemap_fdatawrite(inode->i_mapping);
	if (rc)
A
Al Viro 已提交
829
		cifs_dbg(FYI, "cifs_file_write_iter: %d rc on %p inode\n",
830
			 rc, inode);
831

832 833
out:
	cifs_put_writer(cinode);
L
Linus Torvalds 已提交
834 835 836
	return written;
}

837
static loff_t cifs_llseek(struct file *file, loff_t offset, int whence)
838
{
839
	/*
840
	 * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
841 842
	 * the cached file length
	 */
843
	if (whence != SEEK_SET && whence != SEEK_CUR) {
844
		int rc;
A
Al Viro 已提交
845
		struct inode *inode = file_inode(file);
846 847 848 849 850

		/*
		 * We need to be sure that all dirty pages are written and the
		 * server has the newest file length.
		 */
851
		if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
852 853
		    inode->i_mapping->nrpages != 0) {
			rc = filemap_fdatawait(inode->i_mapping);
854 855 856 857
			if (rc) {
				mapping_set_error(inode->i_mapping, rc);
				return rc;
			}
858 859 860 861 862 863 864 865 866 867 868
		}
		/*
		 * Some applications poll for the file length in this strange
		 * way so we must seek to end on non-oplocked files by
		 * setting the revalidate time to zero.
		 */
		CIFS_I(inode)->time = 0;

		rc = cifs_revalidate_file_attr(file);
		if (rc < 0)
			return (loff_t)rc;
869
	}
870
	return generic_file_llseek(file, offset, whence);
871 872
}

873 874
static int
cifs_setlease(struct file *file, long arg, struct file_lock **lease, void **priv)
S
Steve French 已提交
875
{
876 877 878 879
	/*
	 * Note that this is called by vfs setlease with i_lock held to
	 * protect *lease from going away.
	 */
A
Al Viro 已提交
880
	struct inode *inode = file_inode(file);
881
	struct cifsFileInfo *cfile = file->private_data;
S
Steve French 已提交
882 883 884 885

	if (!(S_ISREG(inode->i_mode)))
		return -EINVAL;

886 887 888
	/* Check if file is oplocked if this is request for new lease */
	if (arg == F_UNLCK ||
	    ((arg == F_RDLCK) && CIFS_CACHE_READ(CIFS_I(inode))) ||
889
	    ((arg == F_WRLCK) && CIFS_CACHE_WRITE(CIFS_I(inode))))
890
		return generic_setlease(file, arg, lease, priv);
891
	else if (tlink_tcon(cfile->tlink)->local_lease &&
892 893 894 895 896 897 898 899 900
		 !CIFS_CACHE_READ(CIFS_I(inode)))
		/*
		 * If the server claims to support oplock on this file, then we
		 * still need to check oplock even if the local_lease mount
		 * option is set, but there are servers which do not support
		 * oplock for which this mount option may be useful if the user
		 * knows that the file won't be changed on the server by anyone
		 * else.
		 */
901
		return generic_setlease(file, arg, lease, priv);
902
	else
S
Steve French 已提交
903 904 905
		return -EAGAIN;
}

906
struct file_system_type cifs_fs_type = {
L
Linus Torvalds 已提交
907 908
	.owner = THIS_MODULE,
	.name = "cifs",
A
Al Viro 已提交
909
	.mount = cifs_do_mount,
A
Al Viro 已提交
910
	.kill_sb = cifs_kill_sb,
L
Linus Torvalds 已提交
911 912
	/*  .fs_flags */
};
913
MODULE_ALIAS_FS("cifs");
914 915 916 917 918 919 920 921 922 923 924

static struct file_system_type smb3_fs_type = {
	.owner = THIS_MODULE,
	.name = "smb3",
	.mount = cifs_do_mount,
	.kill_sb = cifs_kill_sb,
	/*  .fs_flags */
};
MODULE_ALIAS_FS("smb3");
MODULE_ALIAS("smb3");

925
const struct inode_operations cifs_dir_inode_ops = {
L
Linus Torvalds 已提交
926
	.create = cifs_create,
927
	.atomic_open = cifs_atomic_open,
L
Linus Torvalds 已提交
928 929 930 931 932 933
	.lookup = cifs_lookup,
	.getattr = cifs_getattr,
	.unlink = cifs_unlink,
	.link = cifs_hardlink,
	.mkdir = cifs_mkdir,
	.rmdir = cifs_rmdir,
934
	.rename = cifs_rename2,
L
Linus Torvalds 已提交
935 936 937 938 939 940 941
	.permission = cifs_permission,
	.setattr = cifs_setattr,
	.symlink = cifs_symlink,
	.mknod   = cifs_mknod,
	.listxattr = cifs_listxattr,
};

942
const struct inode_operations cifs_file_inode_ops = {
L
Linus Torvalds 已提交
943
	.setattr = cifs_setattr,
944
	.getattr = cifs_getattr,
L
Linus Torvalds 已提交
945 946 947 948
	.permission = cifs_permission,
	.listxattr = cifs_listxattr,
};

949
const struct inode_operations cifs_symlink_inode_ops = {
950
	.get_link = cifs_get_link,
L
Linus Torvalds 已提交
951 952 953 954
	.permission = cifs_permission,
	.listxattr = cifs_listxattr,
};

955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
static int cifs_clone_file_range(struct file *src_file, loff_t off,
		struct file *dst_file, loff_t destoff, u64 len)
{
	struct inode *src_inode = file_inode(src_file);
	struct inode *target_inode = file_inode(dst_file);
	struct cifsFileInfo *smb_file_src = src_file->private_data;
	struct cifsFileInfo *smb_file_target = dst_file->private_data;
	struct cifs_tcon *target_tcon = tlink_tcon(smb_file_target->tlink);
	unsigned int xid;
	int rc;

	cifs_dbg(FYI, "clone range\n");

	xid = get_xid();

	if (!src_file->private_data || !dst_file->private_data) {
		rc = -EBADF;
		cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
		goto out;
	}

	/*
	 * Note: cifs case is easier than btrfs since server responsible for
	 * checks for proper open modes and file type and if it wants
	 * server could even support copy of range where source = target
	 */
	lock_two_nondirectories(target_inode, src_inode);

	if (len == 0)
		len = src_inode->i_size - off;

	cifs_dbg(FYI, "about to flush pages\n");
	/* should we flush first and last page first */
	truncate_inode_pages_range(&target_inode->i_data, destoff,
989
				   PAGE_ALIGN(destoff + len)-1);
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007

	if (target_tcon->ses->server->ops->duplicate_extents)
		rc = target_tcon->ses->server->ops->duplicate_extents(xid,
			smb_file_src, smb_file_target, off, len, destoff);
	else
		rc = -EOPNOTSUPP;

	/* force revalidate of size and timestamps of target file now
	   that target is updated on the server */
	CIFS_I(target_inode)->time = 0;
	/* although unlocking in the reverse order from locking is not
	   strictly necessary here it is a little cleaner to be consistent */
	unlock_two_nondirectories(src_inode, target_inode);
out:
	free_xid(xid);
	return rc;
}

S
Sachin Prabhu 已提交
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
ssize_t 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)
{
	struct inode *src_inode = file_inode(src_file);
	struct inode *target_inode = file_inode(dst_file);
	struct cifsFileInfo *smb_file_src;
	struct cifsFileInfo *smb_file_target;
	struct cifs_tcon *src_tcon;
	struct cifs_tcon *target_tcon;
	ssize_t rc;

	cifs_dbg(FYI, "copychunk range\n");

	if (src_inode == target_inode) {
		rc = -EINVAL;
		goto out;
	}

	if (!src_file->private_data || !dst_file->private_data) {
		rc = -EBADF;
		cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
		goto out;
	}

	rc = -EXDEV;
	smb_file_target = dst_file->private_data;
	smb_file_src = src_file->private_data;
	src_tcon = tlink_tcon(smb_file_src->tlink);
	target_tcon = tlink_tcon(smb_file_target->tlink);

	if (src_tcon->ses != target_tcon->ses) {
		cifs_dbg(VFS, "source and target of copy not on same server\n");
		goto out;
	}

	/*
	 * Note: cifs case is easier than btrfs since server responsible for
	 * checks for proper open modes and file type and if it wants
	 * server could even support copy of range where source = target
	 */
	lock_two_nondirectories(target_inode, src_inode);

	cifs_dbg(FYI, "about to flush pages\n");
	/* should we flush first and last page first */
	truncate_inode_pages(&target_inode->i_data, 0);

	if (target_tcon->ses->server->ops->copychunk_range)
		rc = target_tcon->ses->server->ops->copychunk_range(xid,
			smb_file_src, smb_file_target, off, len, destoff);
	else
		rc = -EOPNOTSUPP;

	/* force revalidate of size and timestamps of target file now
	 * that target is updated on the server
	 */
	CIFS_I(target_inode)->time = 0;
	/* although unlocking in the reverse order from locking is not
	 * strictly necessary here it is a little cleaner to be consistent
	 */
	unlock_two_nondirectories(src_inode, target_inode);

out:
	return rc;
}

1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
/*
 * Directory operations under CIFS/SMB2/SMB3 are synchronous, so fsync()
 * is a dummy operation.
 */
static int cifs_dir_fsync(struct file *file, loff_t start, loff_t end, int datasync)
{
	cifs_dbg(FYI, "Sync directory - name: %pD datasync: 0x%x\n",
		 file, datasync);

	return 0;
}

S
Sachin Prabhu 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
static ssize_t cifs_copy_file_range(struct file *src_file, loff_t off,
				struct file *dst_file, loff_t destoff,
				size_t len, unsigned int flags)
{
	unsigned int xid = get_xid();
	ssize_t rc;

	rc = cifs_file_copychunk_range(xid, src_file, off, dst_file, destoff,
					len, flags);
	free_xid(xid);
	return rc;
}

1100
const struct file_operations cifs_file_ops = {
1101
	.read_iter = cifs_loose_read_iter,
A
Al Viro 已提交
1102
	.write_iter = cifs_file_write_iter,
L
Linus Torvalds 已提交
1103 1104 1105 1106 1107 1108
	.open = cifs_open,
	.release = cifs_close,
	.lock = cifs_lock,
	.fsync = cifs_fsync,
	.flush = cifs_flush,
	.mmap  = cifs_file_mmap,
1109
	.splice_read = generic_file_splice_read,
A
Andrés Souto 已提交
1110
	.splice_write = iter_file_splice_write,
1111
	.llseek = cifs_llseek,
1112
	.unlocked_ioctl	= cifs_ioctl,
S
Sachin Prabhu 已提交
1113
	.copy_file_range = cifs_copy_file_range,
1114
	.clone_file_range = cifs_clone_file_range,
S
Steve French 已提交
1115
	.setlease = cifs_setlease,
1116
	.fallocate = cifs_fallocate,
L
Linus Torvalds 已提交
1117 1118
};

1119
const struct file_operations cifs_file_strict_ops = {
A
Al Viro 已提交
1120
	.read_iter = cifs_strict_readv,
A
Al Viro 已提交
1121
	.write_iter = cifs_strict_writev,
1122 1123 1124 1125 1126
	.open = cifs_open,
	.release = cifs_close,
	.lock = cifs_lock,
	.fsync = cifs_strict_fsync,
	.flush = cifs_flush,
1127
	.mmap = cifs_file_strict_mmap,
1128
	.splice_read = generic_file_splice_read,
A
Andrés Souto 已提交
1129
	.splice_write = iter_file_splice_write,
1130 1131
	.llseek = cifs_llseek,
	.unlocked_ioctl	= cifs_ioctl,
S
Sachin Prabhu 已提交
1132
	.copy_file_range = cifs_copy_file_range,
1133
	.clone_file_range = cifs_clone_file_range,
1134
	.setlease = cifs_setlease,
1135
	.fallocate = cifs_fallocate,
1136 1137
};

1138
const struct file_operations cifs_file_direct_ops = {
1139
	/* BB reevaluate whether they can be done with directio, no cache */
A
Al Viro 已提交
1140
	.read_iter = cifs_user_readv,
A
Al Viro 已提交
1141
	.write_iter = cifs_user_writev,
L
Linus Torvalds 已提交
1142 1143 1144 1145 1146
	.open = cifs_open,
	.release = cifs_close,
	.lock = cifs_lock,
	.fsync = cifs_fsync,
	.flush = cifs_flush,
1147
	.mmap = cifs_file_mmap,
1148
	.splice_read = generic_file_splice_read,
A
Andrés Souto 已提交
1149
	.splice_write = iter_file_splice_write,
1150
	.unlocked_ioctl  = cifs_ioctl,
S
Sachin Prabhu 已提交
1151
	.copy_file_range = cifs_copy_file_range,
1152
	.clone_file_range = cifs_clone_file_range,
1153
	.llseek = cifs_llseek,
S
Steve French 已提交
1154
	.setlease = cifs_setlease,
1155
	.fallocate = cifs_fallocate,
L
Linus Torvalds 已提交
1156
};
1157

1158
const struct file_operations cifs_file_nobrl_ops = {
1159
	.read_iter = cifs_loose_read_iter,
A
Al Viro 已提交
1160
	.write_iter = cifs_file_write_iter,
1161 1162 1163 1164 1165
	.open = cifs_open,
	.release = cifs_close,
	.fsync = cifs_fsync,
	.flush = cifs_flush,
	.mmap  = cifs_file_mmap,
1166
	.splice_read = generic_file_splice_read,
A
Andrés Souto 已提交
1167
	.splice_write = iter_file_splice_write,
1168
	.llseek = cifs_llseek,
1169
	.unlocked_ioctl	= cifs_ioctl,
S
Sachin Prabhu 已提交
1170
	.copy_file_range = cifs_copy_file_range,
1171
	.clone_file_range = cifs_clone_file_range,
S
Steve French 已提交
1172
	.setlease = cifs_setlease,
1173
	.fallocate = cifs_fallocate,
1174 1175
};

1176
const struct file_operations cifs_file_strict_nobrl_ops = {
A
Al Viro 已提交
1177
	.read_iter = cifs_strict_readv,
A
Al Viro 已提交
1178
	.write_iter = cifs_strict_writev,
1179 1180 1181 1182
	.open = cifs_open,
	.release = cifs_close,
	.fsync = cifs_strict_fsync,
	.flush = cifs_flush,
1183
	.mmap = cifs_file_strict_mmap,
1184
	.splice_read = generic_file_splice_read,
A
Andrés Souto 已提交
1185
	.splice_write = iter_file_splice_write,
1186 1187
	.llseek = cifs_llseek,
	.unlocked_ioctl	= cifs_ioctl,
S
Sachin Prabhu 已提交
1188
	.copy_file_range = cifs_copy_file_range,
1189
	.clone_file_range = cifs_clone_file_range,
1190
	.setlease = cifs_setlease,
1191
	.fallocate = cifs_fallocate,
1192 1193
};

1194
const struct file_operations cifs_file_direct_nobrl_ops = {
1195
	/* BB reevaluate whether they can be done with directio, no cache */
A
Al Viro 已提交
1196
	.read_iter = cifs_user_readv,
A
Al Viro 已提交
1197
	.write_iter = cifs_user_writev,
1198 1199 1200 1201
	.open = cifs_open,
	.release = cifs_close,
	.fsync = cifs_fsync,
	.flush = cifs_flush,
1202
	.mmap = cifs_file_mmap,
1203
	.splice_read = generic_file_splice_read,
A
Andrés Souto 已提交
1204
	.splice_write = iter_file_splice_write,
1205
	.unlocked_ioctl  = cifs_ioctl,
S
Sachin Prabhu 已提交
1206
	.copy_file_range = cifs_copy_file_range,
1207
	.clone_file_range = cifs_clone_file_range,
1208
	.llseek = cifs_llseek,
S
Steve French 已提交
1209
	.setlease = cifs_setlease,
1210
	.fallocate = cifs_fallocate,
1211
};
L
Linus Torvalds 已提交
1212

1213
const struct file_operations cifs_dir_ops = {
A
Al Viro 已提交
1214
	.iterate_shared = cifs_readdir,
L
Linus Torvalds 已提交
1215 1216
	.release = cifs_closedir,
	.read    = generic_read_dir,
1217
	.unlocked_ioctl  = cifs_ioctl,
S
Sachin Prabhu 已提交
1218
	.copy_file_range = cifs_copy_file_range,
1219
	.clone_file_range = cifs_clone_file_range,
1220
	.llseek = generic_file_llseek,
1221
	.fsync = cifs_dir_fsync,
L
Linus Torvalds 已提交
1222 1223 1224
};

static void
1225
cifs_init_once(void *inode)
L
Linus Torvalds 已提交
1226 1227 1228
{
	struct cifsInodeInfo *cifsi = inode;

C
Christoph Lameter 已提交
1229
	inode_init_once(&cifsi->vfs_inode);
1230
	init_rwsem(&cifsi->lock_sem);
L
Linus Torvalds 已提交
1231 1232
}

1233
static int __init
L
Linus Torvalds 已提交
1234 1235 1236
cifs_init_inodecache(void)
{
	cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
1237
					      sizeof(struct cifsInodeInfo),
1238
					      0, (SLAB_RECLAIM_ACCOUNT|
1239
						SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1240
					      cifs_init_once);
L
Linus Torvalds 已提交
1241 1242 1243 1244 1245 1246 1247 1248 1249
	if (cifs_inode_cachep == NULL)
		return -ENOMEM;

	return 0;
}

static void
cifs_destroy_inodecache(void)
{
1250 1251 1252 1253 1254
	/*
	 * Make sure all delayed rcu free inodes are flushed before we
	 * destroy cache.
	 */
	rcu_barrier();
1255
	kmem_cache_destroy(cifs_inode_cachep);
L
Linus Torvalds 已提交
1256 1257 1258 1259 1260
}

static int
cifs_init_request_bufs(void)
{
1261 1262 1263 1264
	/*
	 * SMB2 maximum header size is bigger than CIFS one - no problems to
	 * allocate some more bytes for CIFS.
	 */
1265 1266
	size_t max_hdr_size = MAX_SMB2_HDR_SIZE;

1267
	if (CIFSMaxBufSize < 8192) {
L
Linus Torvalds 已提交
1268 1269 1270 1271 1272 1273 1274 1275
	/* Buffer size can not be smaller than 2 * PATH_MAX since maximum
	Unicode path name has to fit in any SMB/CIFS path based frames */
		CIFSMaxBufSize = 8192;
	} else if (CIFSMaxBufSize > 1024*127) {
		CIFSMaxBufSize = 1024 * 127;
	} else {
		CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
	}
1276 1277 1278 1279
/*
	cifs_dbg(VFS, "CIFSMaxBufSize %d 0x%x\n",
		 CIFSMaxBufSize, CIFSMaxBufSize);
*/
1280
	cifs_req_cachep = kmem_cache_create_usercopy("cifs_request",
1281
					    CIFSMaxBufSize + max_hdr_size, 0,
1282 1283 1284
					    SLAB_HWCACHE_ALIGN, 0,
					    CIFSMaxBufSize + max_hdr_size,
					    NULL);
L
Linus Torvalds 已提交
1285 1286 1287
	if (cifs_req_cachep == NULL)
		return -ENOMEM;

1288
	if (cifs_min_rcv < 1)
L
Linus Torvalds 已提交
1289 1290 1291
		cifs_min_rcv = 1;
	else if (cifs_min_rcv > 64) {
		cifs_min_rcv = 64;
1292
		cifs_dbg(VFS, "cifs_min_rcv set to maximum (64)\n");
L
Linus Torvalds 已提交
1293 1294
	}

1295 1296
	cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
						  cifs_req_cachep);
L
Linus Torvalds 已提交
1297

1298
	if (cifs_req_poolp == NULL) {
L
Linus Torvalds 已提交
1299 1300 1301
		kmem_cache_destroy(cifs_req_cachep);
		return -ENOMEM;
	}
1302
	/* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
L
Linus Torvalds 已提交
1303 1304
	almost all handle based requests (but not write response, nor is it
	sufficient for path based requests).  A smaller size would have
1305
	been more efficient (compacting multiple slab items on one 4k page)
L
Linus Torvalds 已提交
1306 1307
	for the case in which debug was on, but this larger size allows
	more SMBs to use small buffer alloc and is still much more
S
Steve French 已提交
1308
	efficient to alloc 1 per page off the slab compared to 17K (5page)
L
Linus Torvalds 已提交
1309
	alloc of large cifs buffers even when page debugging is on */
1310
	cifs_sm_req_cachep = kmem_cache_create_usercopy("cifs_small_rq",
S
Steve French 已提交
1311
			MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
1312
			0, MAX_CIFS_SMALL_BUFFER_SIZE, NULL);
L
Linus Torvalds 已提交
1313 1314 1315
	if (cifs_sm_req_cachep == NULL) {
		mempool_destroy(cifs_req_poolp);
		kmem_cache_destroy(cifs_req_cachep);
S
Steve French 已提交
1316
		return -ENOMEM;
L
Linus Torvalds 已提交
1317 1318
	}

1319
	if (cifs_min_small < 2)
L
Linus Torvalds 已提交
1320 1321 1322
		cifs_min_small = 2;
	else if (cifs_min_small > 256) {
		cifs_min_small = 256;
1323
		cifs_dbg(FYI, "cifs_min_small set to maximum (256)\n");
L
Linus Torvalds 已提交
1324 1325
	}

1326 1327
	cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
						     cifs_sm_req_cachep);
L
Linus Torvalds 已提交
1328

1329
	if (cifs_sm_req_poolp == NULL) {
L
Linus Torvalds 已提交
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
		mempool_destroy(cifs_req_poolp);
		kmem_cache_destroy(cifs_req_cachep);
		kmem_cache_destroy(cifs_sm_req_cachep);
		return -ENOMEM;
	}

	return 0;
}

static void
cifs_destroy_request_bufs(void)
{
	mempool_destroy(cifs_req_poolp);
1343
	kmem_cache_destroy(cifs_req_cachep);
L
Linus Torvalds 已提交
1344
	mempool_destroy(cifs_sm_req_poolp);
1345
	kmem_cache_destroy(cifs_sm_req_cachep);
L
Linus Torvalds 已提交
1346 1347 1348 1349 1350 1351
}

static int
cifs_init_mids(void)
{
	cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
1352 1353
					    sizeof(struct mid_q_entry), 0,
					    SLAB_HWCACHE_ALIGN, NULL);
L
Linus Torvalds 已提交
1354 1355 1356
	if (cifs_mid_cachep == NULL)
		return -ENOMEM;

1357 1358
	/* 3 is a reasonable minimum number of simultaneous operations */
	cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
1359
	if (cifs_mid_poolp == NULL) {
L
Linus Torvalds 已提交
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
		kmem_cache_destroy(cifs_mid_cachep);
		return -ENOMEM;
	}

	return 0;
}

static void
cifs_destroy_mids(void)
{
	mempool_destroy(cifs_mid_poolp);
1371
	kmem_cache_destroy(cifs_mid_cachep);
L
Linus Torvalds 已提交
1372 1373 1374 1375 1376 1377 1378
}

static int __init
init_cifs(void)
{
	int rc = 0;
	cifs_proc_init();
1379
	INIT_LIST_HEAD(&cifs_tcp_ses_list);
1380
#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* unused temporarily */
1381 1382
	INIT_LIST_HEAD(&GlobalDnotifyReqList);
	INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
1383
#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */
L
Linus Torvalds 已提交
1384 1385 1386 1387 1388
/*
 *  Initialize Global counters
 */
	atomic_set(&sesInfoAllocCount, 0);
	atomic_set(&tconInfoAllocCount, 0);
S
Steve French 已提交
1389
	atomic_set(&tcpSesAllocCount, 0);
L
Linus Torvalds 已提交
1390 1391 1392 1393
	atomic_set(&tcpSesReconnectCount, 0);
	atomic_set(&tconInfoReconnectCount, 0);

	atomic_set(&bufAllocCount, 0);
1394 1395 1396 1397 1398 1399
	atomic_set(&smBufAllocCount, 0);
#ifdef CONFIG_CIFS_STATS2
	atomic_set(&totBufAllocCount, 0);
	atomic_set(&totSmBufAllocCount, 0);
#endif /* CONFIG_CIFS_STATS2 */

L
Linus Torvalds 已提交
1400 1401 1402 1403
	atomic_set(&midCount, 0);
	GlobalCurrentXid = 0;
	GlobalTotalActiveXid = 0;
	GlobalMaxActiveXid = 0;
1404
	spin_lock_init(&cifs_tcp_ses_lock);
L
Linus Torvalds 已提交
1405 1406
	spin_lock_init(&GlobalMid_Lock);

1407
	cifs_lock_secret = get_random_u32();
1408

1409
	if (cifs_max_pending < 2) {
L
Linus Torvalds 已提交
1410
		cifs_max_pending = 2;
1411
		cifs_dbg(FYI, "cifs_max_pending set to min of 2\n");
1412 1413
	} else if (cifs_max_pending > CIFS_MAX_REQ) {
		cifs_max_pending = CIFS_MAX_REQ;
1414 1415
		cifs_dbg(FYI, "cifs_max_pending set to max of %u\n",
			 CIFS_MAX_REQ);
L
Linus Torvalds 已提交
1416 1417
	}

J
Jeff Layton 已提交
1418 1419 1420 1421 1422 1423
	cifsiod_wq = alloc_workqueue("cifsiod", WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
	if (!cifsiod_wq) {
		rc = -ENOMEM;
		goto out_clean_proc;
	}

R
Rabin Vincent 已提交
1424 1425 1426 1427 1428 1429 1430
	cifsoplockd_wq = alloc_workqueue("cifsoplockd",
					 WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
	if (!cifsoplockd_wq) {
		rc = -ENOMEM;
		goto out_destroy_cifsiod_wq;
	}

1431 1432
	rc = cifs_fscache_register();
	if (rc)
R
Rabin Vincent 已提交
1433
		goto out_destroy_cifsoplockd_wq;
1434

L
Linus Torvalds 已提交
1435
	rc = cifs_init_inodecache();
1436
	if (rc)
1437
		goto out_unreg_fscache;
1438 1439 1440 1441 1442 1443 1444 1445 1446

	rc = cifs_init_mids();
	if (rc)
		goto out_destroy_inodecache;

	rc = cifs_init_request_bufs();
	if (rc)
		goto out_destroy_mids;

1447
#ifdef CONFIG_CIFS_UPCALL
1448
	rc = init_cifs_spnego();
1449
	if (rc)
1450 1451 1452 1453 1454 1455
		goto out_destroy_request_bufs;
#endif /* CONFIG_CIFS_UPCALL */

#ifdef CONFIG_CIFS_ACL
	rc = init_cifs_idmap();
	if (rc)
1456
		goto out_register_key_type;
1457 1458 1459 1460
#endif /* CONFIG_CIFS_ACL */

	rc = register_filesystem(&cifs_fs_type);
	if (rc)
1461
		goto out_init_cifs_idmap;
1462

1463 1464 1465 1466 1467 1468
	rc = register_filesystem(&smb3_fs_type);
	if (rc) {
		unregister_filesystem(&cifs_fs_type);
		goto out_init_cifs_idmap;
	}

1469 1470
	return 0;

1471
out_init_cifs_idmap:
1472 1473
#ifdef CONFIG_CIFS_ACL
	exit_cifs_idmap();
1474
out_register_key_type:
1475
#endif
1476
#ifdef CONFIG_CIFS_UPCALL
1477
	exit_cifs_spnego();
1478
out_destroy_request_bufs:
1479
#endif
1480
	cifs_destroy_request_bufs();
1481
out_destroy_mids:
1482
	cifs_destroy_mids();
1483
out_destroy_inodecache:
1484
	cifs_destroy_inodecache();
1485
out_unreg_fscache:
1486
	cifs_fscache_unregister();
R
Rabin Vincent 已提交
1487 1488 1489
out_destroy_cifsoplockd_wq:
	destroy_workqueue(cifsoplockd_wq);
out_destroy_cifsiod_wq:
J
Jeff Layton 已提交
1490
	destroy_workqueue(cifsiod_wq);
1491 1492
out_clean_proc:
	cifs_proc_clean();
L
Linus Torvalds 已提交
1493 1494 1495 1496 1497 1498
	return rc;
}

static void __exit
exit_cifs(void)
{
1499
	cifs_dbg(NOISY, "exit_smb3\n");
1500
	unregister_filesystem(&cifs_fs_type);
1501
	unregister_filesystem(&smb3_fs_type);
1502
	cifs_dfs_release_automount_timer();
1503 1504 1505
#ifdef CONFIG_CIFS_ACL
	exit_cifs_idmap();
#endif
1506
#ifdef CONFIG_CIFS_UPCALL
1507
	exit_cifs_spnego();
L
Linus Torvalds 已提交
1508 1509
#endif
	cifs_destroy_request_bufs();
1510 1511 1512
	cifs_destroy_mids();
	cifs_destroy_inodecache();
	cifs_fscache_unregister();
R
Rabin Vincent 已提交
1513
	destroy_workqueue(cifsoplockd_wq);
J
Jeff Layton 已提交
1514
	destroy_workqueue(cifsiod_wq);
1515
	cifs_proc_clean();
L
Linus Torvalds 已提交
1516 1517 1518
}

MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
S
Steve French 已提交
1519
MODULE_LICENSE("GPL");	/* combination of LGPL + GPL source behaves as GPL */
L
Linus Torvalds 已提交
1520
MODULE_DESCRIPTION
1521 1522
    ("VFS to access servers complying with the SNIA CIFS Specification "
     "e.g. Samba and Windows");
L
Linus Torvalds 已提交
1523
MODULE_VERSION(CIFS_VERSION);
J
Jean Delvare 已提交
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
MODULE_SOFTDEP("pre: arc4");
MODULE_SOFTDEP("pre: des");
MODULE_SOFTDEP("pre: ecb");
MODULE_SOFTDEP("pre: hmac");
MODULE_SOFTDEP("pre: md4");
MODULE_SOFTDEP("pre: md5");
MODULE_SOFTDEP("pre: nls");
MODULE_SOFTDEP("pre: aes");
MODULE_SOFTDEP("pre: cmac");
MODULE_SOFTDEP("pre: sha256");
A
Aurelien Aptel 已提交
1534
MODULE_SOFTDEP("pre: sha512");
1535 1536
MODULE_SOFTDEP("pre: aead2");
MODULE_SOFTDEP("pre: ccm");
L
Linus Torvalds 已提交
1537 1538
module_init(init_cifs)
module_exit(exit_cifs)