File Index
Files and directories
Sign in
FILE

cifsfs.c

File preview with syntax highlighting.

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