Viewing: super25.c

// SPDX-License-Identifier: GPL-2.0

/*
 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright (c) 2011, 2016, Intel Corporation.
 */

/*
 * This file is part of Lustre, http://www.lustre.org/
 */

#define DEBUG_SUBSYSTEM S_LLITE

#include <linux/module.h>
#include <linux/types.h>
#include <linux/version.h>
#include <lustre_ha.h>
#include <lustre_dlm.h>
#include <lustre_quota.h>
#include <linux/init.h>
#include <lustre_compat/linux/fs.h>
#include <linux/random.h>
#include <lprocfs_status.h>
#include "llite_internal.h"
#include "vvp_internal.h"

static struct kmem_cache *ll_inode_cachep;

static struct inode *ll_alloc_inode(struct super_block *sb)
{
	struct ll_inode_info *lli;
#ifdef HAVE_ALLOC_INODE_SB
	lli = alloc_inode_sb(sb, ll_inode_cachep, GFP_NOFS);
	if (!lli)
		return NULL;
	OBD_ALLOC_POST(lli, sizeof(*lli), "slab-alloced");
	memset(lli, 0, sizeof(*lli));
#else
	OBD_SLAB_ALLOC_PTR_GFP(lli, ll_inode_cachep, GFP_NOFS);
	if (!lli)
		return NULL;
#endif
	inode_init_once(&lli->lli_vfs_inode);
	lli->lli_open_thrsh_count = UINT_MAX;

	return &lli->lli_vfs_inode;
}

static void ll_inode_destroy_callback(struct rcu_head *head)
{
	struct inode *inode = container_of(head, struct inode, i_rcu);
	struct ll_inode_info *ptr = ll_i2info(inode);

	llcrypt_free_inode(inode);
	OBD_SLAB_FREE_PTR(ptr, ll_inode_cachep);
}

static void ll_destroy_inode(struct inode *inode)
{
	call_rcu(&inode->i_rcu, ll_inode_destroy_callback);
}

static int ll_drop_inode(struct inode *inode)
{
	struct ll_sb_info *sbi = ll_i2sbi(inode);
	int drop;

	if (!sbi->ll_inode_cache_enabled)
		return 1;

	drop = inode_generic_drop(inode);
	if (!drop)
		drop = llcrypt_drop_inode(inode);

	return drop;
}

static int ll_show_devname(struct seq_file *m, struct dentry *root)
{
	struct lustre_sb_info *lsi = s2lsi(root->d_sb);
	struct lustre_mount_data *lmd = lsi->lsi_lmd;

	if (lmd && lmd->lmd_mgsname) {
		struct ll_sb_info *sbi = lsi->lsi_llsbi;

		seq_printf(m, "%s:/%s", lmd->lmd_mgsname, sbi->ll_fsname);
	} else if (lmd && lmd->lmd_dev) {
		seq_puts(m, lmd->lmd_dev);
	} else {
		seq_puts(m, "<unknown>");
	}
	return 0;
}

/* exported operations */
const struct super_operations lustre_super_operations = {
	.alloc_inode   = ll_alloc_inode,
	.destroy_inode = ll_destroy_inode,
	.drop_inode    = ll_drop_inode,
	.evict_inode   = ll_delete_inode,
	.put_super     = ll_put_super,
	.statfs        = ll_statfs,
	.umount_begin  = ll_umount_begin,
	.show_options  = ll_show_options,
	.show_devname  = ll_show_devname,
};

/**
 * lustre_fill_super() - set up the superblock with lustre info
 * @sb: setup superblock struct with lustre info
 * @fc: Pointer to struct fs_context
 *
 * This is the entry point for the mount call into Lustre.
 * This is called when a client is mounted, and this is
 * where we start setting things up.
 *
 * Returns:
 * * %0 Success
 * * %negative Error
 */
static int lustre_fill_super(struct super_block *sb, struct fs_context *fc)
{
	struct lustre_mount_data *lmd;
	struct lustre_sb_info *lsi;
	int rc;

	ENTRY;
	CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);

	lsi = lustre_init_lsi(fc, sb);
	if (!lsi)
		RETURN(-ENOMEM);
	lmd = lsi->lsi_lmd;

	/*
	 * LU-639: the OBD cleanup of last mount may not finish yet, wait here.
	 */
	obd_zombie_barrier();

	if (!lmd_is_client(lmd)) {
#ifdef CONFIG_LUSTRE_FS_SERVER
		static bool printed;

		if (!printed) {
			LCONSOLE_WARN("%s: mounting server target with '-t lustre' deprecated, use '-t lustre_tgt'\n",
				      lmd->lmd_profile);
			printed = true;
		}
		rc = server_fill_super(sb);
#else
		rc = -ENODEV;
		CERROR("%s: This is client-side-only module, cannot handle server mount: rc = %d\n",
		       lmd->lmd_profile, rc);
		lustre_put_lsi(sb);
#endif
		GOTO(out, rc);
	}

	CDEBUG(D_MOUNT, "Mounting client %s\n", lmd->lmd_profile);
	rc = lustre_start_mgc(sb);
	if (rc) {
		lustre_common_put_super(sb);
		GOTO(out, rc);
	}
	/* Connect and start */
	rc = ll_fill_super(sb);
	/* ll_file_super will call lustre_common_put_super on failure,
	 * which takes care of the module reference.
	 *
	 * If error happens in fill_super() call, @lsi will be killed there.
	 * This is why we do not put it here.
	 */
out:
	if (rc) {
		CERROR("llite: Unable to mount %s: rc = %d\n",
		       s2lsi(sb) ? lmd->lmd_dev : "<unknown>", rc);
	} else {
		CDEBUG(D_SUPER, "%s: Mount complete\n",
		       lmd->lmd_dev);
	}

	return rc;
}

/***************** FS registration ******************/
static int lustre_get_tree(struct fs_context *fc)
{
	return get_tree_nodev(fc, lustre_fill_super);
}

static int lustre_reconfigure(struct fs_context *fc)
{
	struct super_block *sb = fc->root->d_sb;
	char *profilenm = get_profile_name(sb);
	struct lustre_sb_info *lsi = s2lsi(sb);
	struct ll_sb_info *sbi = ll_s2sbi(sb);
	u32 read_only;

	sync_filesystem(sb);

	if ((fc->sb_flags & SB_RDONLY) != (sb->s_flags & SB_RDONLY)) {
		int err;

		read_only = fc->sb_flags & SB_RDONLY;
		err = obd_set_info_async(NULL, sbi->ll_md_exp,
					sizeof(KEY_READ_ONLY),
					 KEY_READ_ONLY, sizeof(read_only),
					 &read_only, NULL);
		if (err) {
			LCONSOLE_WARN("Failed to remount %s %s (%d)\n",
				      profilenm, read_only ?
				      "read-only" : "read-write", err);
			return err;
		}

		if (read_only)
			sb->s_flags |= SB_RDONLY;
		else
			sb->s_flags &= ~SB_RDONLY;

		if (test_bit(LL_SBI_VERBOSE, sbi->ll_flags))
			LCONSOLE_WARN("Remounted %s %s\n", profilenm,
				      read_only ?  "read-only" : "read-write");
	}

	/* Support a rewmount with new mount options. */
	swap(lsi->lsi_lmd, fc->fs_private);

	return 0;
}

static const struct fs_context_operations lustre_fs_context_ops = {
	.parse_monolithic	= lustre_parse_monolithic,
	.reconfigure		= lustre_reconfigure,
	.get_tree		= lustre_get_tree,
	.free			= lustre_fc_free,
};

static int lustre_init_fs_context(struct fs_context *fc)
{
	struct lustre_mount_data *lmd;

	OBD_ALLOC_PTR(lmd);
	if (!lmd)
		return -ENOMEM;

	kref_init(&lmd->lmd_ref);
	fc->fs_private = lmd;
	fc->ops = &lustre_fs_context_ops;
	return 0;
}

static void lustre_kill_super(struct super_block *sb)
{
	struct lustre_sb_info *lsi = s2lsi(sb);

	if (lsi && !IS_SERVER(lsi))
		ll_kill_super(sb);

	kill_anon_super(sb);
}

/* Register the "lustre" fs type */
static struct file_system_type lustre_fs_type = {
	.owner			= THIS_MODULE,
	.name			= "lustre",
	.init_fs_context	= lustre_init_fs_context,
	.kill_sb		= lustre_kill_super,
	.fs_flags		= FS_RENAME_DOES_D_MOVE,
};
MODULE_ALIAS_FS("lustre");

static int __init lustre_init(void)
{
	int rc;

	BUILD_BUG_ON(sizeof(LUSTRE_VOLATILE_HDR) !=
		     LUSTRE_VOLATILE_HDR_LEN + 1);

	rc = libcfs_setup();
	if (rc)
		return rc;

	/* print an address of _any_ initialized kernel symbol from this
	 * module, to allow debugging with gdb that doesn't support data
	 * symbols from modules.
	 */
	CDEBUG(D_INFO, "Lustre client module (%p).\n",
	       &lustre_super_operations);

	ll_inode_cachep = kmem_cache_create("lustre_inode_cache",
					    sizeof(struct ll_inode_info), 0,
					    SLAB_HWCACHE_ALIGN |
					    SLAB_RECLAIM_ACCOUNT |
					    SLAB_MEM_SPREAD |
					    SLAB_ACCOUNT,
					    NULL);
	if (ll_inode_cachep == NULL)
		GOTO(out_cache, rc = -ENOMEM);

	ll_file_data_slab = kmem_cache_create("ll_file_data",
						 sizeof(struct ll_file_data), 0,
						 SLAB_HWCACHE_ALIGN, NULL);
	if (ll_file_data_slab == NULL)
		GOTO(out_cache, rc = -ENOMEM);

	pcc_inode_slab = kmem_cache_create("ll_pcc_inode",
					   sizeof(struct pcc_inode), 0,
					   SLAB_HWCACHE_ALIGN, NULL);
	if (pcc_inode_slab == NULL)
		GOTO(out_cache, rc = -ENOMEM);

	quota_iter_slab = kmem_cache_create("ll_quota_iter",
					   sizeof(struct if_quotactl_iter), 0,
					   SLAB_HWCACHE_ALIGN, NULL);
	if (quota_iter_slab == NULL)
		GOTO(out_cache, rc = -ENOMEM);

	rc = llite_tunables_register();
	if (rc)
		GOTO(out_cache, rc);

	rc = vvp_global_init();
	if (rc != 0)
		GOTO(out_tunables, rc);

	cl_inode_fini_env = cl_env_alloc(&cl_inode_fini_refcheck,
					 LCT_REMEMBER | LCT_NOREF);
	if (IS_ERR(cl_inode_fini_env))
		GOTO(out_vvp, rc = PTR_ERR(cl_inode_fini_env));

	cl_inode_fini_env->le_ctx.lc_cookie = 0x4;

	rc = ll_xattr_init();
	if (rc != 0)
		GOTO(out_inode_fini_env, rc);

	rc = register_filesystem(&lustre_fs_type);
	if (rc)
		GOTO(out_xattr, rc);

	RETURN(0);

out_xattr:
	ll_xattr_fini();
out_inode_fini_env:
	cl_env_put(cl_inode_fini_env, &cl_inode_fini_refcheck);
out_vvp:
	vvp_global_fini();
out_tunables:
	llite_tunables_unregister();
out_cache:
	kmem_cache_destroy(ll_inode_cachep);
	kmem_cache_destroy(ll_file_data_slab);
	kmem_cache_destroy(pcc_inode_slab);
	kmem_cache_destroy(quota_iter_slab);
	return rc;
}

static void __exit lustre_exit(void)
{
	unregister_filesystem(&lustre_fs_type);

	llite_tunables_unregister();

	ll_xattr_fini();
	cl_env_put(cl_inode_fini_env, &cl_inode_fini_refcheck);
	vvp_global_fini();

	/*
	 * Make sure all delayed rcu free inodes are flushed before we
	 * destroy cache.
	 */
	rcu_barrier();

	kmem_cache_destroy(ll_inode_cachep);
	kmem_cache_destroy(ll_file_data_slab);
	kmem_cache_destroy(pcc_inode_slab);
	kmem_cache_destroy(quota_iter_slab);
}

unsigned int llite_enable_flr_ec;
module_param(llite_enable_flr_ec, uint, 0644);
MODULE_PARM_DESC(llite_enable_flr_ec,
		 "enable FLR EC connect flag, off by default");

MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
MODULE_DESCRIPTION("Lustre Client File System");
MODULE_VERSION(LUSTRE_VERSION_STRING);
MODULE_LICENSE("GPL");

late_initcall_sync(lustre_init);
module_exit(lustre_exit);