xref: /openbmc/linux/fs/xfs/xfs_super.c (revision d37cf9b63113f13d742713881ce691fc615d8b3b)
10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
2c59d87c4SChristoph Hellwig /*
3c59d87c4SChristoph Hellwig  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4c59d87c4SChristoph Hellwig  * All Rights Reserved.
5c59d87c4SChristoph Hellwig  */
6c59d87c4SChristoph Hellwig 
7c59d87c4SChristoph Hellwig #include "xfs.h"
870a9883cSDave Chinner #include "xfs_shared.h"
96ca1c906SDave Chinner #include "xfs_format.h"
10239880efSDave Chinner #include "xfs_log_format.h"
11239880efSDave Chinner #include "xfs_trans_resv.h"
12c59d87c4SChristoph Hellwig #include "xfs_sb.h"
13c59d87c4SChristoph Hellwig #include "xfs_mount.h"
14c59d87c4SChristoph Hellwig #include "xfs_inode.h"
15c59d87c4SChristoph Hellwig #include "xfs_btree.h"
16c59d87c4SChristoph Hellwig #include "xfs_bmap.h"
17a4fbe6abSDave Chinner #include "xfs_alloc.h"
18c59d87c4SChristoph Hellwig #include "xfs_fsops.h"
19239880efSDave Chinner #include "xfs_trans.h"
20c59d87c4SChristoph Hellwig #include "xfs_buf_item.h"
21239880efSDave Chinner #include "xfs_log.h"
22c59d87c4SChristoph Hellwig #include "xfs_log_priv.h"
232b9ab5abSDave Chinner #include "xfs_dir2.h"
24c59d87c4SChristoph Hellwig #include "xfs_extfree_item.h"
25c59d87c4SChristoph Hellwig #include "xfs_mru_cache.h"
26c59d87c4SChristoph Hellwig #include "xfs_inode_item.h"
276d8b79cfSDave Chinner #include "xfs_icache.h"
28c59d87c4SChristoph Hellwig #include "xfs_trace.h"
293ebe7d2dSDave Chinner #include "xfs_icreate_item.h"
30a4fbe6abSDave Chinner #include "xfs_filestream.h"
31a4fbe6abSDave Chinner #include "xfs_quota.h"
3265b65735SBrian Foster #include "xfs_sysfs.h"
3330cbc591SDarrick J. Wong #include "xfs_ondisk.h"
345880f2d7SDarrick J. Wong #include "xfs_rmap_item.h"
35baf4bcacSDarrick J. Wong #include "xfs_refcount_item.h"
366413a014SDarrick J. Wong #include "xfs_bmap_item.h"
375e7e605cSDarrick J. Wong #include "xfs_reflink.h"
38894ecacfSDarrick J. Wong #include "xfs_pwork.h"
399bbafc71SDave Chinner #include "xfs_ag.h"
40f3c799c2SDarrick J. Wong #include "xfs_defer.h"
414136e38aSDarrick J. Wong #include "xfs_attr_item.h"
42d9c61ccbSDarrick J. Wong #include "xfs_xattr.h"
43784eb7d8SDave Chinner #include "xfs_iunlink_item.h"
443cfb9290SDarrick J. Wong #include "xfs_dahash_test.h"
45d7a74cadSDarrick J. Wong #include "scrub/stats.h"
46c59d87c4SChristoph Hellwig 
47dddde68bSAdam Borowski #include <linux/magic.h>
4873e5fff9SIan Kent #include <linux/fs_context.h>
4973e5fff9SIan Kent #include <linux/fs_parser.h>
50c59d87c4SChristoph Hellwig 
51c59d87c4SChristoph Hellwig static const struct super_operations xfs_super_operations;
5265b65735SBrian Foster 
53a76dba3bSDarrick J. Wong static struct dentry *xfs_debugfs;	/* top-level xfs debugfs dir */
54e3aed1a0SDave Chinner static struct kset *xfs_kset;		/* top-level xfs sysfs dir */
5565b65735SBrian Foster #ifdef DEBUG
5665b65735SBrian Foster static struct xfs_kobj xfs_dbg_kobj;	/* global debug sysfs attrs */
5765b65735SBrian Foster #endif
58c59d87c4SChristoph Hellwig 
598d6c3446SIra Weiny enum xfs_dax_mode {
608d6c3446SIra Weiny 	XFS_DAX_INODE = 0,
618d6c3446SIra Weiny 	XFS_DAX_ALWAYS = 1,
628d6c3446SIra Weiny 	XFS_DAX_NEVER = 2,
638d6c3446SIra Weiny };
648d6c3446SIra Weiny 
658d6c3446SIra Weiny static void
xfs_mount_set_dax_mode(struct xfs_mount * mp,enum xfs_dax_mode mode)668d6c3446SIra Weiny xfs_mount_set_dax_mode(
678d6c3446SIra Weiny 	struct xfs_mount	*mp,
688d6c3446SIra Weiny 	enum xfs_dax_mode	mode)
698d6c3446SIra Weiny {
708d6c3446SIra Weiny 	switch (mode) {
718d6c3446SIra Weiny 	case XFS_DAX_INODE:
720560f31aSDave Chinner 		mp->m_features &= ~(XFS_FEAT_DAX_ALWAYS | XFS_FEAT_DAX_NEVER);
738d6c3446SIra Weiny 		break;
748d6c3446SIra Weiny 	case XFS_DAX_ALWAYS:
750560f31aSDave Chinner 		mp->m_features |= XFS_FEAT_DAX_ALWAYS;
760560f31aSDave Chinner 		mp->m_features &= ~XFS_FEAT_DAX_NEVER;
778d6c3446SIra Weiny 		break;
788d6c3446SIra Weiny 	case XFS_DAX_NEVER:
790560f31aSDave Chinner 		mp->m_features |= XFS_FEAT_DAX_NEVER;
800560f31aSDave Chinner 		mp->m_features &= ~XFS_FEAT_DAX_ALWAYS;
818d6c3446SIra Weiny 		break;
828d6c3446SIra Weiny 	}
838d6c3446SIra Weiny }
848d6c3446SIra Weiny 
858d6c3446SIra Weiny static const struct constant_table dax_param_enums[] = {
868d6c3446SIra Weiny 	{"inode",	XFS_DAX_INODE },
878d6c3446SIra Weiny 	{"always",	XFS_DAX_ALWAYS },
888d6c3446SIra Weiny 	{"never",	XFS_DAX_NEVER },
898d6c3446SIra Weiny 	{}
908d6c3446SIra Weiny };
918d6c3446SIra Weiny 
92c59d87c4SChristoph Hellwig /*
93c59d87c4SChristoph Hellwig  * Table driven mount option parser.
94c59d87c4SChristoph Hellwig  */
95c59d87c4SChristoph Hellwig enum {
968da57c5cSIan Kent 	Opt_logbufs, Opt_logbsize, Opt_logdev, Opt_rtdev,
972e74af0eSEric Sandeen 	Opt_wsync, Opt_noalign, Opt_swalloc, Opt_sunit, Opt_swidth, Opt_nouuid,
9894079285SChristoph Hellwig 	Opt_grpid, Opt_nogrpid, Opt_bsdgroups, Opt_sysvgroups,
991c02d502SEric Sandeen 	Opt_allocsize, Opt_norecovery, Opt_inode64, Opt_inode32, Opt_ikeep,
1001c02d502SEric Sandeen 	Opt_noikeep, Opt_largeio, Opt_nolargeio, Opt_attr2, Opt_noattr2,
1011c02d502SEric Sandeen 	Opt_filestreams, Opt_quota, Opt_noquota, Opt_usrquota, Opt_grpquota,
1021c02d502SEric Sandeen 	Opt_prjquota, Opt_uquota, Opt_gquota, Opt_pquota,
1032e74af0eSEric Sandeen 	Opt_uqnoenforce, Opt_gqnoenforce, Opt_pqnoenforce, Opt_qnoenforce,
1048d6c3446SIra Weiny 	Opt_discard, Opt_nodiscard, Opt_dax, Opt_dax_enum,
105c59d87c4SChristoph Hellwig };
106c59d87c4SChristoph Hellwig 
107d7167b14SAl Viro static const struct fs_parameter_spec xfs_fs_parameters[] = {
10873e5fff9SIan Kent 	fsparam_u32("logbufs",		Opt_logbufs),
10973e5fff9SIan Kent 	fsparam_string("logbsize",	Opt_logbsize),
11073e5fff9SIan Kent 	fsparam_string("logdev",	Opt_logdev),
11173e5fff9SIan Kent 	fsparam_string("rtdev",		Opt_rtdev),
11273e5fff9SIan Kent 	fsparam_flag("wsync",		Opt_wsync),
11373e5fff9SIan Kent 	fsparam_flag("noalign",		Opt_noalign),
11473e5fff9SIan Kent 	fsparam_flag("swalloc",		Opt_swalloc),
11573e5fff9SIan Kent 	fsparam_u32("sunit",		Opt_sunit),
11673e5fff9SIan Kent 	fsparam_u32("swidth",		Opt_swidth),
11773e5fff9SIan Kent 	fsparam_flag("nouuid",		Opt_nouuid),
11873e5fff9SIan Kent 	fsparam_flag("grpid",		Opt_grpid),
11973e5fff9SIan Kent 	fsparam_flag("nogrpid",		Opt_nogrpid),
12073e5fff9SIan Kent 	fsparam_flag("bsdgroups",	Opt_bsdgroups),
12173e5fff9SIan Kent 	fsparam_flag("sysvgroups",	Opt_sysvgroups),
12273e5fff9SIan Kent 	fsparam_string("allocsize",	Opt_allocsize),
12373e5fff9SIan Kent 	fsparam_flag("norecovery",	Opt_norecovery),
12473e5fff9SIan Kent 	fsparam_flag("inode64",		Opt_inode64),
12573e5fff9SIan Kent 	fsparam_flag("inode32",		Opt_inode32),
12673e5fff9SIan Kent 	fsparam_flag("ikeep",		Opt_ikeep),
12773e5fff9SIan Kent 	fsparam_flag("noikeep",		Opt_noikeep),
12873e5fff9SIan Kent 	fsparam_flag("largeio",		Opt_largeio),
12973e5fff9SIan Kent 	fsparam_flag("nolargeio",	Opt_nolargeio),
13073e5fff9SIan Kent 	fsparam_flag("attr2",		Opt_attr2),
13173e5fff9SIan Kent 	fsparam_flag("noattr2",		Opt_noattr2),
13273e5fff9SIan Kent 	fsparam_flag("filestreams",	Opt_filestreams),
13373e5fff9SIan Kent 	fsparam_flag("quota",		Opt_quota),
13473e5fff9SIan Kent 	fsparam_flag("noquota",		Opt_noquota),
13573e5fff9SIan Kent 	fsparam_flag("usrquota",	Opt_usrquota),
13673e5fff9SIan Kent 	fsparam_flag("grpquota",	Opt_grpquota),
13773e5fff9SIan Kent 	fsparam_flag("prjquota",	Opt_prjquota),
13873e5fff9SIan Kent 	fsparam_flag("uquota",		Opt_uquota),
13973e5fff9SIan Kent 	fsparam_flag("gquota",		Opt_gquota),
14073e5fff9SIan Kent 	fsparam_flag("pquota",		Opt_pquota),
14173e5fff9SIan Kent 	fsparam_flag("uqnoenforce",	Opt_uqnoenforce),
14273e5fff9SIan Kent 	fsparam_flag("gqnoenforce",	Opt_gqnoenforce),
14373e5fff9SIan Kent 	fsparam_flag("pqnoenforce",	Opt_pqnoenforce),
14473e5fff9SIan Kent 	fsparam_flag("qnoenforce",	Opt_qnoenforce),
14573e5fff9SIan Kent 	fsparam_flag("discard",		Opt_discard),
14673e5fff9SIan Kent 	fsparam_flag("nodiscard",	Opt_nodiscard),
14773e5fff9SIan Kent 	fsparam_flag("dax",		Opt_dax),
1488d6c3446SIra Weiny 	fsparam_enum("dax",		Opt_dax_enum, dax_param_enums),
14973e5fff9SIan Kent 	{}
150c59d87c4SChristoph Hellwig };
151c59d87c4SChristoph Hellwig 
152c59d87c4SChristoph Hellwig struct proc_xfs_info {
153cbe4dab1SDave Chinner 	uint64_t	flag;
154c59d87c4SChristoph Hellwig 	char		*str;
155c59d87c4SChristoph Hellwig };
156c59d87c4SChristoph Hellwig 
15721f55993SChristoph Hellwig static int
xfs_fs_show_options(struct seq_file * m,struct dentry * root)15821f55993SChristoph Hellwig xfs_fs_show_options(
15921f55993SChristoph Hellwig 	struct seq_file		*m,
16021f55993SChristoph Hellwig 	struct dentry		*root)
161c59d87c4SChristoph Hellwig {
162c59d87c4SChristoph Hellwig 	static struct proc_xfs_info xfs_info_set[] = {
163c59d87c4SChristoph Hellwig 		/* the few simple ones we can get from the mount struct */
1640560f31aSDave Chinner 		{ XFS_FEAT_IKEEP,		",ikeep" },
1650560f31aSDave Chinner 		{ XFS_FEAT_WSYNC,		",wsync" },
1660560f31aSDave Chinner 		{ XFS_FEAT_NOALIGN,		",noalign" },
1670560f31aSDave Chinner 		{ XFS_FEAT_SWALLOC,		",swalloc" },
1680560f31aSDave Chinner 		{ XFS_FEAT_NOUUID,		",nouuid" },
1690560f31aSDave Chinner 		{ XFS_FEAT_NORECOVERY,		",norecovery" },
1700560f31aSDave Chinner 		{ XFS_FEAT_ATTR2,		",attr2" },
1710560f31aSDave Chinner 		{ XFS_FEAT_FILESTREAMS,		",filestreams" },
1720560f31aSDave Chinner 		{ XFS_FEAT_GRPID,		",grpid" },
1730560f31aSDave Chinner 		{ XFS_FEAT_DISCARD,		",discard" },
1740560f31aSDave Chinner 		{ XFS_FEAT_LARGE_IOSIZE,	",largeio" },
1750560f31aSDave Chinner 		{ XFS_FEAT_DAX_ALWAYS,		",dax=always" },
1760560f31aSDave Chinner 		{ XFS_FEAT_DAX_NEVER,		",dax=never" },
177c59d87c4SChristoph Hellwig 		{ 0, NULL }
178c59d87c4SChristoph Hellwig 	};
17921f55993SChristoph Hellwig 	struct xfs_mount	*mp = XFS_M(root->d_sb);
180c59d87c4SChristoph Hellwig 	struct proc_xfs_info	*xfs_infop;
181c59d87c4SChristoph Hellwig 
182c59d87c4SChristoph Hellwig 	for (xfs_infop = xfs_info_set; xfs_infop->flag; xfs_infop++) {
1830560f31aSDave Chinner 		if (mp->m_features & xfs_infop->flag)
184c59d87c4SChristoph Hellwig 			seq_puts(m, xfs_infop->str);
185c59d87c4SChristoph Hellwig 	}
1861775c506SChristoph Hellwig 
1870560f31aSDave Chinner 	seq_printf(m, ",inode%d", xfs_has_small_inums(mp) ? 32 : 64);
188c59d87c4SChristoph Hellwig 
1890560f31aSDave Chinner 	if (xfs_has_allocsize(mp))
1902e74af0eSEric Sandeen 		seq_printf(m, ",allocsize=%dk",
191aa58d445SChristoph Hellwig 			   (1 << mp->m_allocsize_log) >> 10);
192c59d87c4SChristoph Hellwig 
193c59d87c4SChristoph Hellwig 	if (mp->m_logbufs > 0)
1942e74af0eSEric Sandeen 		seq_printf(m, ",logbufs=%d", mp->m_logbufs);
195c59d87c4SChristoph Hellwig 	if (mp->m_logbsize > 0)
1962e74af0eSEric Sandeen 		seq_printf(m, ",logbsize=%dk", mp->m_logbsize >> 10);
197c59d87c4SChristoph Hellwig 
198c59d87c4SChristoph Hellwig 	if (mp->m_logname)
1992e74af0eSEric Sandeen 		seq_show_option(m, "logdev", mp->m_logname);
200c59d87c4SChristoph Hellwig 	if (mp->m_rtname)
2012e74af0eSEric Sandeen 		seq_show_option(m, "rtdev", mp->m_rtname);
202c59d87c4SChristoph Hellwig 
203c59d87c4SChristoph Hellwig 	if (mp->m_dalign > 0)
2042e74af0eSEric Sandeen 		seq_printf(m, ",sunit=%d",
205c59d87c4SChristoph Hellwig 				(int)XFS_FSB_TO_BB(mp, mp->m_dalign));
206c59d87c4SChristoph Hellwig 	if (mp->m_swidth > 0)
2072e74af0eSEric Sandeen 		seq_printf(m, ",swidth=%d",
208c59d87c4SChristoph Hellwig 				(int)XFS_FSB_TO_BB(mp, mp->m_swidth));
209c59d87c4SChristoph Hellwig 
210237d7887SKaixu Xia 	if (mp->m_qflags & XFS_UQUOTA_ENFD)
2112e74af0eSEric Sandeen 		seq_puts(m, ",usrquota");
212149e53afSChristoph Hellwig 	else if (mp->m_qflags & XFS_UQUOTA_ACCT)
2132e74af0eSEric Sandeen 		seq_puts(m, ",uqnoenforce");
214c59d87c4SChristoph Hellwig 
21583e782e1SChandra Seetharaman 	if (mp->m_qflags & XFS_PQUOTA_ENFD)
2162e74af0eSEric Sandeen 		seq_puts(m, ",prjquota");
217149e53afSChristoph Hellwig 	else if (mp->m_qflags & XFS_PQUOTA_ACCT)
2182e74af0eSEric Sandeen 		seq_puts(m, ",pqnoenforce");
219149e53afSChristoph Hellwig 
22083e782e1SChandra Seetharaman 	if (mp->m_qflags & XFS_GQUOTA_ENFD)
2212e74af0eSEric Sandeen 		seq_puts(m, ",grpquota");
222149e53afSChristoph Hellwig 	else if (mp->m_qflags & XFS_GQUOTA_ACCT)
2232e74af0eSEric Sandeen 		seq_puts(m, ",gqnoenforce");
224c59d87c4SChristoph Hellwig 
225c59d87c4SChristoph Hellwig 	if (!(mp->m_qflags & XFS_ALL_QUOTA_ACCT))
2262e74af0eSEric Sandeen 		seq_puts(m, ",noquota");
22721f55993SChristoph Hellwig 
22821f55993SChristoph Hellwig 	return 0;
229c59d87c4SChristoph Hellwig }
23091083269SEric Sandeen 
2317ac2ff8bSDave Chinner static bool
xfs_set_inode_alloc_perag(struct xfs_perag * pag,xfs_ino_t ino,xfs_agnumber_t max_metadata)2327ac2ff8bSDave Chinner xfs_set_inode_alloc_perag(
2337ac2ff8bSDave Chinner 	struct xfs_perag	*pag,
2347ac2ff8bSDave Chinner 	xfs_ino_t		ino,
2357ac2ff8bSDave Chinner 	xfs_agnumber_t		max_metadata)
2367ac2ff8bSDave Chinner {
2377ac2ff8bSDave Chinner 	if (!xfs_is_inode32(pag->pag_mount)) {
2387ac2ff8bSDave Chinner 		set_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
2397ac2ff8bSDave Chinner 		clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
2407ac2ff8bSDave Chinner 		return false;
2417ac2ff8bSDave Chinner 	}
2427ac2ff8bSDave Chinner 
2437ac2ff8bSDave Chinner 	if (ino > XFS_MAXINUMBER_32) {
2447ac2ff8bSDave Chinner 		clear_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
2457ac2ff8bSDave Chinner 		clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
2467ac2ff8bSDave Chinner 		return false;
2477ac2ff8bSDave Chinner 	}
2487ac2ff8bSDave Chinner 
2497ac2ff8bSDave Chinner 	set_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
2507ac2ff8bSDave Chinner 	if (pag->pag_agno < max_metadata)
2517ac2ff8bSDave Chinner 		set_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
2527ac2ff8bSDave Chinner 	else
2537ac2ff8bSDave Chinner 		clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
2547ac2ff8bSDave Chinner 	return true;
2557ac2ff8bSDave Chinner }
2567ac2ff8bSDave Chinner 
2579de67c3bSEric Sandeen /*
25812c3f05cSEric Sandeen  * Set parameters for inode allocation heuristics, taking into account
25912c3f05cSEric Sandeen  * filesystem size and inode32/inode64 mount options; i.e. specifically
2600560f31aSDave Chinner  * whether or not XFS_FEAT_SMALL_INUMS is set.
26112c3f05cSEric Sandeen  *
26212c3f05cSEric Sandeen  * Inode allocation patterns are altered only if inode32 is requested
2630560f31aSDave Chinner  * (XFS_FEAT_SMALL_INUMS), and the filesystem is sufficiently large.
2642e973b2cSDave Chinner  * If altered, XFS_OPSTATE_INODE32 is set as well.
26512c3f05cSEric Sandeen  *
26612c3f05cSEric Sandeen  * An agcount independent of that in the mount structure is provided
26712c3f05cSEric Sandeen  * because in the growfs case, mp->m_sb.sb_agcount is not yet updated
26812c3f05cSEric Sandeen  * to the potentially higher ag count.
26912c3f05cSEric Sandeen  *
27012c3f05cSEric Sandeen  * Returns the maximum AG index which may contain inodes.
2719de67c3bSEric Sandeen  */
2722d2194f6SCarlos Maiolino xfs_agnumber_t
xfs_set_inode_alloc(struct xfs_mount * mp,xfs_agnumber_t agcount)27312c3f05cSEric Sandeen xfs_set_inode_alloc(
27412c3f05cSEric Sandeen 	struct xfs_mount *mp,
27512c3f05cSEric Sandeen 	xfs_agnumber_t	agcount)
2762d2194f6SCarlos Maiolino {
27712c3f05cSEric Sandeen 	xfs_agnumber_t	index;
2784056c1d0SCarlos Maiolino 	xfs_agnumber_t	maxagi = 0;
2792d2194f6SCarlos Maiolino 	xfs_sb_t	*sbp = &mp->m_sb;
2802d2194f6SCarlos Maiolino 	xfs_agnumber_t	max_metadata;
28154aa61f8SEric Sandeen 	xfs_agino_t	agino;
28254aa61f8SEric Sandeen 	xfs_ino_t	ino;
2832d2194f6SCarlos Maiolino 
28412c3f05cSEric Sandeen 	/*
28512c3f05cSEric Sandeen 	 * Calculate how much should be reserved for inodes to meet
28612c3f05cSEric Sandeen 	 * the max inode percentage.  Used only for inode32.
2872d2194f6SCarlos Maiolino 	 */
288ef325959SDarrick J. Wong 	if (M_IGEO(mp)->maxicount) {
289c8ce540dSDarrick J. Wong 		uint64_t	icount;
2902d2194f6SCarlos Maiolino 
2912d2194f6SCarlos Maiolino 		icount = sbp->sb_dblocks * sbp->sb_imax_pct;
2922d2194f6SCarlos Maiolino 		do_div(icount, 100);
2932d2194f6SCarlos Maiolino 		icount += sbp->sb_agblocks - 1;
2942d2194f6SCarlos Maiolino 		do_div(icount, sbp->sb_agblocks);
2952d2194f6SCarlos Maiolino 		max_metadata = icount;
2962d2194f6SCarlos Maiolino 	} else {
2979de67c3bSEric Sandeen 		max_metadata = agcount;
2982d2194f6SCarlos Maiolino 	}
2992d2194f6SCarlos Maiolino 
30012c3f05cSEric Sandeen 	/* Get the last possible inode in the filesystem */
30143004b2aSDarrick J. Wong 	agino =	XFS_AGB_TO_AGINO(mp, sbp->sb_agblocks - 1);
30212c3f05cSEric Sandeen 	ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
30354aa61f8SEric Sandeen 
30412c3f05cSEric Sandeen 	/*
30512c3f05cSEric Sandeen 	 * If user asked for no more than 32-bit inodes, and the fs is
3062e973b2cSDave Chinner 	 * sufficiently large, set XFS_OPSTATE_INODE32 if we must alter
30712c3f05cSEric Sandeen 	 * the allocator to accommodate the request.
30812c3f05cSEric Sandeen 	 */
3090560f31aSDave Chinner 	if (xfs_has_small_inums(mp) && ino > XFS_MAXINUMBER_32)
3102e973b2cSDave Chinner 		set_bit(XFS_OPSTATE_INODE32, &mp->m_opstate);
31112c3f05cSEric Sandeen 	else
3122e973b2cSDave Chinner 		clear_bit(XFS_OPSTATE_INODE32, &mp->m_opstate);
3132d2194f6SCarlos Maiolino 
3149de67c3bSEric Sandeen 	for (index = 0; index < agcount; index++) {
3152d2194f6SCarlos Maiolino 		struct xfs_perag	*pag;
3162d2194f6SCarlos Maiolino 
31712c3f05cSEric Sandeen 		ino = XFS_AGINO_TO_INO(mp, index, agino);
31812c3f05cSEric Sandeen 
3192d2194f6SCarlos Maiolino 		pag = xfs_perag_get(mp, index);
3207ac2ff8bSDave Chinner 		if (xfs_set_inode_alloc_perag(pag, ino, max_metadata))
32112c3f05cSEric Sandeen 			maxagi++;
3222d2194f6SCarlos Maiolino 		xfs_perag_put(pag);
3232d2194f6SCarlos Maiolino 	}
3242d2194f6SCarlos Maiolino 
3252e973b2cSDave Chinner 	return xfs_is_inode32(mp) ? maxagi : agcount;
3262d2194f6SCarlos Maiolino }
3272d2194f6SCarlos Maiolino 
328679a9949SChristoph Hellwig static int
xfs_setup_dax_always(struct xfs_mount * mp)329679a9949SChristoph Hellwig xfs_setup_dax_always(
330679a9949SChristoph Hellwig 	struct xfs_mount	*mp)
331a384f088SChristoph Hellwig {
3327b0800d0SChristoph Hellwig 	if (!mp->m_ddev_targp->bt_daxdev &&
3337b0800d0SChristoph Hellwig 	    (!mp->m_rtdev_targp || !mp->m_rtdev_targp->bt_daxdev)) {
334679a9949SChristoph Hellwig 		xfs_alert(mp,
335679a9949SChristoph Hellwig 			"DAX unsupported by block device. Turning off DAX.");
336679a9949SChristoph Hellwig 		goto disable_dax;
337679a9949SChristoph Hellwig 	}
338679a9949SChristoph Hellwig 
3397b0800d0SChristoph Hellwig 	if (mp->m_super->s_blocksize != PAGE_SIZE) {
3407b0800d0SChristoph Hellwig 		xfs_alert(mp,
3417b0800d0SChristoph Hellwig 			"DAX not supported for blocksize. Turning off DAX.");
3427b0800d0SChristoph Hellwig 		goto disable_dax;
3437b0800d0SChristoph Hellwig 	}
3447b0800d0SChristoph Hellwig 
34535fcd75aSShiyang Ruan 	if (xfs_has_reflink(mp) &&
34635fcd75aSShiyang Ruan 	    bdev_is_partition(mp->m_ddev_targp->bt_bdev)) {
34735fcd75aSShiyang Ruan 		xfs_alert(mp,
34835fcd75aSShiyang Ruan 			"DAX and reflink cannot work with multi-partitions!");
349679a9949SChristoph Hellwig 		return -EINVAL;
350679a9949SChristoph Hellwig 	}
351679a9949SChristoph Hellwig 
352679a9949SChristoph Hellwig 	xfs_warn(mp, "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
353679a9949SChristoph Hellwig 	return 0;
354679a9949SChristoph Hellwig 
355679a9949SChristoph Hellwig disable_dax:
356679a9949SChristoph Hellwig 	xfs_mount_set_dax_mode(mp, XFS_DAX_NEVER);
357679a9949SChristoph Hellwig 	return 0;
358a384f088SChristoph Hellwig }
359a384f088SChristoph Hellwig 
360c59d87c4SChristoph Hellwig STATIC int
xfs_blkdev_get(xfs_mount_t * mp,const char * name,struct block_device ** bdevp)361c59d87c4SChristoph Hellwig xfs_blkdev_get(
362c59d87c4SChristoph Hellwig 	xfs_mount_t		*mp,
363c59d87c4SChristoph Hellwig 	const char		*name,
364c59d87c4SChristoph Hellwig 	struct block_device	**bdevp)
365c59d87c4SChristoph Hellwig {
366c59d87c4SChristoph Hellwig 	int			error = 0;
367c59d87c4SChristoph Hellwig 
3688ffa54e3SChristoph Hellwig 	*bdevp = blkdev_get_by_path(name, BLK_OPEN_READ | BLK_OPEN_WRITE,
3698ffa54e3SChristoph Hellwig 				    mp->m_super, &fs_holder_ops);
370c59d87c4SChristoph Hellwig 	if (IS_ERR(*bdevp)) {
371c59d87c4SChristoph Hellwig 		error = PTR_ERR(*bdevp);
37277af574eSEric Sandeen 		xfs_warn(mp, "Invalid device [%s], error=%d", name, error);
373c59d87c4SChristoph Hellwig 	}
374c59d87c4SChristoph Hellwig 
3752451337dSDave Chinner 	return error;
376c59d87c4SChristoph Hellwig }
377c59d87c4SChristoph Hellwig 
378c59d87c4SChristoph Hellwig STATIC void
xfs_shutdown_devices(struct xfs_mount * mp)37935a93b14SChristoph Hellwig xfs_shutdown_devices(
380c59d87c4SChristoph Hellwig 	struct xfs_mount	*mp)
381c59d87c4SChristoph Hellwig {
3821a0a5dadSChristoph Hellwig 	/*
3831a0a5dadSChristoph Hellwig 	 * Udev is triggered whenever anyone closes a block device or unmounts
3841a0a5dadSChristoph Hellwig 	 * a file systemm on a block device.
3851a0a5dadSChristoph Hellwig 	 * The default udev rules invoke blkid to read the fs super and create
3861a0a5dadSChristoph Hellwig 	 * symlinks to the bdev under /dev/disk.  For this, it uses buffered
3871a0a5dadSChristoph Hellwig 	 * reads through the page cache.
3881a0a5dadSChristoph Hellwig 	 *
3891a0a5dadSChristoph Hellwig 	 * xfs_db also uses buffered reads to examine metadata.  There is no
3901a0a5dadSChristoph Hellwig 	 * coordination between xfs_db and udev, which means that they can run
3911a0a5dadSChristoph Hellwig 	 * concurrently.  Note there is no coordination between the kernel and
3921a0a5dadSChristoph Hellwig 	 * blkid either.
3931a0a5dadSChristoph Hellwig 	 *
3941a0a5dadSChristoph Hellwig 	 * On a system with 64k pages, the page cache can cache the superblock
3951a0a5dadSChristoph Hellwig 	 * and the root inode (and hence the root directory) with the same 64k
3961a0a5dadSChristoph Hellwig 	 * page.  If udev spawns blkid after the mkfs and the system is busy
3971a0a5dadSChristoph Hellwig 	 * enough that it is still running when xfs_db starts up, they'll both
3981a0a5dadSChristoph Hellwig 	 * read from the same page in the pagecache.
3991a0a5dadSChristoph Hellwig 	 *
4001a0a5dadSChristoph Hellwig 	 * The unmount writes updated inode metadata to disk directly.  The XFS
4011a0a5dadSChristoph Hellwig 	 * buffer cache does not use the bdev pagecache, so it needs to
4021a0a5dadSChristoph Hellwig 	 * invalidate that pagecache on unmount.  If the above scenario occurs,
4031a0a5dadSChristoph Hellwig 	 * the pagecache no longer reflects what's on disk, xfs_db reads the
4041a0a5dadSChristoph Hellwig 	 * stale metadata, and fails to find /a.  Most of the time this succeeds
4051a0a5dadSChristoph Hellwig 	 * because closing a bdev invalidates the page cache, but when processes
4061a0a5dadSChristoph Hellwig 	 * race, everyone loses.
4071a0a5dadSChristoph Hellwig 	 */
408c59d87c4SChristoph Hellwig 	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
40935a93b14SChristoph Hellwig 		blkdev_issue_flush(mp->m_logdev_targp->bt_bdev);
41035a93b14SChristoph Hellwig 		invalidate_bdev(mp->m_logdev_targp->bt_bdev);
411c59d87c4SChristoph Hellwig 	}
412c59d87c4SChristoph Hellwig 	if (mp->m_rtdev_targp) {
41335a93b14SChristoph Hellwig 		blkdev_issue_flush(mp->m_rtdev_targp->bt_bdev);
41435a93b14SChristoph Hellwig 		invalidate_bdev(mp->m_rtdev_targp->bt_bdev);
415c59d87c4SChristoph Hellwig 	}
41635a93b14SChristoph Hellwig 	blkdev_issue_flush(mp->m_ddev_targp->bt_bdev);
41735a93b14SChristoph Hellwig 	invalidate_bdev(mp->m_ddev_targp->bt_bdev);
418c59d87c4SChristoph Hellwig }
419c59d87c4SChristoph Hellwig 
420c59d87c4SChristoph Hellwig /*
421c59d87c4SChristoph Hellwig  * The file system configurations are:
422c59d87c4SChristoph Hellwig  *	(1) device (partition) with data and internal log
423c59d87c4SChristoph Hellwig  *	(2) logical volume with data and log subvolumes.
424c59d87c4SChristoph Hellwig  *	(3) logical volume with data, log, and realtime subvolumes.
425c59d87c4SChristoph Hellwig  *
426c59d87c4SChristoph Hellwig  * We only have to handle opening the log and realtime volumes here if
427c59d87c4SChristoph Hellwig  * they are present.  The data subvolume has already been opened by
428c59d87c4SChristoph Hellwig  * get_sb_bdev() and is stored in sb->s_bdev.
429c59d87c4SChristoph Hellwig  */
430c59d87c4SChristoph Hellwig STATIC int
xfs_open_devices(struct xfs_mount * mp)431c59d87c4SChristoph Hellwig xfs_open_devices(
432c59d87c4SChristoph Hellwig 	struct xfs_mount	*mp)
433c59d87c4SChristoph Hellwig {
4348d945b59SChristoph Hellwig 	struct super_block	*sb = mp->m_super;
4358d945b59SChristoph Hellwig 	struct block_device	*ddev = sb->s_bdev;
436c59d87c4SChristoph Hellwig 	struct block_device	*logdev = NULL, *rtdev = NULL;
437c59d87c4SChristoph Hellwig 	int			error;
438c59d87c4SChristoph Hellwig 
439c59d87c4SChristoph Hellwig 	/*
4408d945b59SChristoph Hellwig 	 * blkdev_put() can't be called under s_umount, see the comment
4418d945b59SChristoph Hellwig 	 * in get_tree_bdev() for more details
4428d945b59SChristoph Hellwig 	 */
4438d945b59SChristoph Hellwig 	up_write(&sb->s_umount);
4448d945b59SChristoph Hellwig 
4458d945b59SChristoph Hellwig 	/*
446c59d87c4SChristoph Hellwig 	 * Open real time and log devices - order is important.
447c59d87c4SChristoph Hellwig 	 */
448c59d87c4SChristoph Hellwig 	if (mp->m_logname) {
449c59d87c4SChristoph Hellwig 		error = xfs_blkdev_get(mp, mp->m_logname, &logdev);
450c59d87c4SChristoph Hellwig 		if (error)
4518d945b59SChristoph Hellwig 			goto out_relock;
452c59d87c4SChristoph Hellwig 	}
453c59d87c4SChristoph Hellwig 
454c59d87c4SChristoph Hellwig 	if (mp->m_rtname) {
455c59d87c4SChristoph Hellwig 		error = xfs_blkdev_get(mp, mp->m_rtname, &rtdev);
456c59d87c4SChristoph Hellwig 		if (error)
457c59d87c4SChristoph Hellwig 			goto out_close_logdev;
458c59d87c4SChristoph Hellwig 
459c59d87c4SChristoph Hellwig 		if (rtdev == ddev || rtdev == logdev) {
460c59d87c4SChristoph Hellwig 			xfs_warn(mp,
461c59d87c4SChristoph Hellwig 	"Cannot mount filesystem with identical rtdev and ddev/logdev.");
4622451337dSDave Chinner 			error = -EINVAL;
463c59d87c4SChristoph Hellwig 			goto out_close_rtdev;
464c59d87c4SChristoph Hellwig 		}
465c59d87c4SChristoph Hellwig 	}
466c59d87c4SChristoph Hellwig 
467c59d87c4SChristoph Hellwig 	/*
468c59d87c4SChristoph Hellwig 	 * Setup xfs_mount buffer target pointers
469c59d87c4SChristoph Hellwig 	 */
4702451337dSDave Chinner 	error = -ENOMEM;
4715b5abbefSChristoph Hellwig 	mp->m_ddev_targp = xfs_alloc_buftarg(mp, ddev);
472c59d87c4SChristoph Hellwig 	if (!mp->m_ddev_targp)
473c59d87c4SChristoph Hellwig 		goto out_close_rtdev;
474c59d87c4SChristoph Hellwig 
475c59d87c4SChristoph Hellwig 	if (rtdev) {
4765b5abbefSChristoph Hellwig 		mp->m_rtdev_targp = xfs_alloc_buftarg(mp, rtdev);
477c59d87c4SChristoph Hellwig 		if (!mp->m_rtdev_targp)
478c59d87c4SChristoph Hellwig 			goto out_free_ddev_targ;
479c59d87c4SChristoph Hellwig 	}
480c59d87c4SChristoph Hellwig 
481c59d87c4SChristoph Hellwig 	if (logdev && logdev != ddev) {
4825b5abbefSChristoph Hellwig 		mp->m_logdev_targp = xfs_alloc_buftarg(mp, logdev);
483c59d87c4SChristoph Hellwig 		if (!mp->m_logdev_targp)
484c59d87c4SChristoph Hellwig 			goto out_free_rtdev_targ;
485c59d87c4SChristoph Hellwig 	} else {
486c59d87c4SChristoph Hellwig 		mp->m_logdev_targp = mp->m_ddev_targp;
487c59d87c4SChristoph Hellwig 	}
488c59d87c4SChristoph Hellwig 
4898d945b59SChristoph Hellwig 	error = 0;
4908d945b59SChristoph Hellwig out_relock:
4918d945b59SChristoph Hellwig 	down_write(&sb->s_umount);
4928d945b59SChristoph Hellwig 	return error;
493c59d87c4SChristoph Hellwig 
494c59d87c4SChristoph Hellwig  out_free_rtdev_targ:
495c59d87c4SChristoph Hellwig 	if (mp->m_rtdev_targp)
496a1f69417SEric Sandeen 		xfs_free_buftarg(mp->m_rtdev_targp);
497c59d87c4SChristoph Hellwig  out_free_ddev_targ:
498a1f69417SEric Sandeen 	xfs_free_buftarg(mp->m_ddev_targp);
499c59d87c4SChristoph Hellwig  out_close_rtdev:
500d3ef7e94SChristoph Hellwig 	 if (rtdev)
5018ffa54e3SChristoph Hellwig 		 blkdev_put(rtdev, sb);
502c59d87c4SChristoph Hellwig  out_close_logdev:
5035b5abbefSChristoph Hellwig 	if (logdev && logdev != ddev)
5048ffa54e3SChristoph Hellwig 		blkdev_put(logdev, sb);
5058d945b59SChristoph Hellwig 	goto out_relock;
506c59d87c4SChristoph Hellwig }
507c59d87c4SChristoph Hellwig 
508c59d87c4SChristoph Hellwig /*
509c59d87c4SChristoph Hellwig  * Setup xfs_mount buffer target pointers based on superblock
510c59d87c4SChristoph Hellwig  */
511c59d87c4SChristoph Hellwig STATIC int
xfs_setup_devices(struct xfs_mount * mp)512c59d87c4SChristoph Hellwig xfs_setup_devices(
513c59d87c4SChristoph Hellwig 	struct xfs_mount	*mp)
514c59d87c4SChristoph Hellwig {
515c59d87c4SChristoph Hellwig 	int			error;
516c59d87c4SChristoph Hellwig 
517a96c4151SEric Sandeen 	error = xfs_setsize_buftarg(mp->m_ddev_targp, mp->m_sb.sb_sectsize);
518c59d87c4SChristoph Hellwig 	if (error)
519c59d87c4SChristoph Hellwig 		return error;
520c59d87c4SChristoph Hellwig 
521c59d87c4SChristoph Hellwig 	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
522c59d87c4SChristoph Hellwig 		unsigned int	log_sector_size = BBSIZE;
523c59d87c4SChristoph Hellwig 
52438c26bfdSDave Chinner 		if (xfs_has_sector(mp))
525c59d87c4SChristoph Hellwig 			log_sector_size = mp->m_sb.sb_logsectsize;
526c59d87c4SChristoph Hellwig 		error = xfs_setsize_buftarg(mp->m_logdev_targp,
527c59d87c4SChristoph Hellwig 					    log_sector_size);
528c59d87c4SChristoph Hellwig 		if (error)
529c59d87c4SChristoph Hellwig 			return error;
530c59d87c4SChristoph Hellwig 	}
531c59d87c4SChristoph Hellwig 	if (mp->m_rtdev_targp) {
532c59d87c4SChristoph Hellwig 		error = xfs_setsize_buftarg(mp->m_rtdev_targp,
533c59d87c4SChristoph Hellwig 					    mp->m_sb.sb_sectsize);
534c59d87c4SChristoph Hellwig 		if (error)
535c59d87c4SChristoph Hellwig 			return error;
536c59d87c4SChristoph Hellwig 	}
537c59d87c4SChristoph Hellwig 
538c59d87c4SChristoph Hellwig 	return 0;
539c59d87c4SChristoph Hellwig }
540c59d87c4SChristoph Hellwig 
541aa6bf01dSChristoph Hellwig STATIC int
xfs_init_mount_workqueues(struct xfs_mount * mp)542aa6bf01dSChristoph Hellwig xfs_init_mount_workqueues(
543aa6bf01dSChristoph Hellwig 	struct xfs_mount	*mp)
544aa6bf01dSChristoph Hellwig {
54578c931b8SBrian Foster 	mp->m_buf_workqueue = alloc_workqueue("xfs-buf/%s",
54605a302a1SDarrick J. Wong 			XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM),
54705a302a1SDarrick J. Wong 			1, mp->m_super->s_id);
54878c931b8SBrian Foster 	if (!mp->m_buf_workqueue)
54978c931b8SBrian Foster 		goto out;
55078c931b8SBrian Foster 
551aa6bf01dSChristoph Hellwig 	mp->m_unwritten_workqueue = alloc_workqueue("xfs-conv/%s",
55205a302a1SDarrick J. Wong 			XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM),
55305a302a1SDarrick J. Wong 			0, mp->m_super->s_id);
554aa6bf01dSChristoph Hellwig 	if (!mp->m_unwritten_workqueue)
55528408243SDarrick J. Wong 		goto out_destroy_buf;
556aa6bf01dSChristoph Hellwig 
5575889608dSDave Chinner 	mp->m_reclaim_workqueue = alloc_workqueue("xfs-reclaim/%s",
55805a302a1SDarrick J. Wong 			XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM),
55905a302a1SDarrick J. Wong 			0, mp->m_super->s_id);
5605889608dSDave Chinner 	if (!mp->m_reclaim_workqueue)
56133c0dd78SDave Chinner 		goto out_destroy_unwritten;
5625889608dSDave Chinner 
563ab23a776SDave Chinner 	mp->m_blockgc_wq = alloc_workqueue("xfs-blockgc/%s",
564ab23a776SDave Chinner 			XFS_WQFLAGS(WQ_UNBOUND | WQ_FREEZABLE | WQ_MEM_RECLAIM),
56505a302a1SDarrick J. Wong 			0, mp->m_super->s_id);
566ab23a776SDave Chinner 	if (!mp->m_blockgc_wq)
5671058d0f5SChristoph Hellwig 		goto out_destroy_reclaim;
568579b62faSBrian Foster 
569ab23a776SDave Chinner 	mp->m_inodegc_wq = alloc_workqueue("xfs-inodegc/%s",
570ab23a776SDave Chinner 			XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM),
571ab23a776SDave Chinner 			1, mp->m_super->s_id);
572ab23a776SDave Chinner 	if (!mp->m_inodegc_wq)
573ab23a776SDave Chinner 		goto out_destroy_blockgc;
574ab23a776SDave Chinner 
57505a302a1SDarrick J. Wong 	mp->m_sync_workqueue = alloc_workqueue("xfs-sync/%s",
57605a302a1SDarrick J. Wong 			XFS_WQFLAGS(WQ_FREEZABLE), 0, mp->m_super->s_id);
577696a5620SBrian Foster 	if (!mp->m_sync_workqueue)
578ab23a776SDave Chinner 		goto out_destroy_inodegc;
579696a5620SBrian Foster 
580aa6bf01dSChristoph Hellwig 	return 0;
581aa6bf01dSChristoph Hellwig 
582ab23a776SDave Chinner out_destroy_inodegc:
583ab23a776SDave Chinner 	destroy_workqueue(mp->m_inodegc_wq);
584ab23a776SDave Chinner out_destroy_blockgc:
585ab23a776SDave Chinner 	destroy_workqueue(mp->m_blockgc_wq);
5865889608dSDave Chinner out_destroy_reclaim:
5875889608dSDave Chinner 	destroy_workqueue(mp->m_reclaim_workqueue);
5884c2d542fSDave Chinner out_destroy_unwritten:
5894c2d542fSDave Chinner 	destroy_workqueue(mp->m_unwritten_workqueue);
59078c931b8SBrian Foster out_destroy_buf:
59178c931b8SBrian Foster 	destroy_workqueue(mp->m_buf_workqueue);
592aa6bf01dSChristoph Hellwig out:
593aa6bf01dSChristoph Hellwig 	return -ENOMEM;
594aa6bf01dSChristoph Hellwig }
595aa6bf01dSChristoph Hellwig 
596aa6bf01dSChristoph Hellwig STATIC void
xfs_destroy_mount_workqueues(struct xfs_mount * mp)597aa6bf01dSChristoph Hellwig xfs_destroy_mount_workqueues(
598aa6bf01dSChristoph Hellwig 	struct xfs_mount	*mp)
599aa6bf01dSChristoph Hellwig {
600696a5620SBrian Foster 	destroy_workqueue(mp->m_sync_workqueue);
601ab23a776SDave Chinner 	destroy_workqueue(mp->m_blockgc_wq);
602ab23a776SDave Chinner 	destroy_workqueue(mp->m_inodegc_wq);
6035889608dSDave Chinner 	destroy_workqueue(mp->m_reclaim_workqueue);
604aa6bf01dSChristoph Hellwig 	destroy_workqueue(mp->m_unwritten_workqueue);
60578c931b8SBrian Foster 	destroy_workqueue(mp->m_buf_workqueue);
606aa6bf01dSChristoph Hellwig }
607aa6bf01dSChristoph Hellwig 
608f0f7a674SDarrick J. Wong static void
xfs_flush_inodes_worker(struct work_struct * work)609f0f7a674SDarrick J. Wong xfs_flush_inodes_worker(
610f0f7a674SDarrick J. Wong 	struct work_struct	*work)
611f0f7a674SDarrick J. Wong {
612f0f7a674SDarrick J. Wong 	struct xfs_mount	*mp = container_of(work, struct xfs_mount,
613f0f7a674SDarrick J. Wong 						   m_flush_inodes_work);
614f0f7a674SDarrick J. Wong 	struct super_block	*sb = mp->m_super;
615f0f7a674SDarrick J. Wong 
616f0f7a674SDarrick J. Wong 	if (down_read_trylock(&sb->s_umount)) {
617f0f7a674SDarrick J. Wong 		sync_inodes_sb(sb);
618f0f7a674SDarrick J. Wong 		up_read(&sb->s_umount);
619f0f7a674SDarrick J. Wong 	}
620f0f7a674SDarrick J. Wong }
621f0f7a674SDarrick J. Wong 
6229aa05000SDave Chinner /*
6239aa05000SDave Chinner  * Flush all dirty data to disk. Must not be called while holding an XFS_ILOCK
6249aa05000SDave Chinner  * or a page lock. We use sync_inodes_sb() here to ensure we block while waiting
6259aa05000SDave Chinner  * for IO to complete so that we effectively throttle multiple callers to the
6269aa05000SDave Chinner  * rate at which IO is completing.
6279aa05000SDave Chinner  */
6289aa05000SDave Chinner void
xfs_flush_inodes(struct xfs_mount * mp)6299aa05000SDave Chinner xfs_flush_inodes(
6309aa05000SDave Chinner 	struct xfs_mount	*mp)
6319aa05000SDave Chinner {
632f0f7a674SDarrick J. Wong 	/*
633f0f7a674SDarrick J. Wong 	 * If flush_work() returns true then that means we waited for a flush
634f0f7a674SDarrick J. Wong 	 * which was already in progress.  Don't bother running another scan.
635f0f7a674SDarrick J. Wong 	 */
636f0f7a674SDarrick J. Wong 	if (flush_work(&mp->m_flush_inodes_work))
637c6425702SDarrick J. Wong 		return;
638c6425702SDarrick J. Wong 
639f0f7a674SDarrick J. Wong 	queue_work(mp->m_sync_workqueue, &mp->m_flush_inodes_work);
640f0f7a674SDarrick J. Wong 	flush_work(&mp->m_flush_inodes_work);
6419aa05000SDave Chinner }
6429aa05000SDave Chinner 
643c59d87c4SChristoph Hellwig /* Catch misguided souls that try to use this interface on XFS */
644c59d87c4SChristoph Hellwig STATIC struct inode *
xfs_fs_alloc_inode(struct super_block * sb)645c59d87c4SChristoph Hellwig xfs_fs_alloc_inode(
646c59d87c4SChristoph Hellwig 	struct super_block	*sb)
647c59d87c4SChristoph Hellwig {
648c59d87c4SChristoph Hellwig 	BUG();
649c59d87c4SChristoph Hellwig 	return NULL;
650c59d87c4SChristoph Hellwig }
651c59d87c4SChristoph Hellwig 
652c59d87c4SChristoph Hellwig /*
653c59d87c4SChristoph Hellwig  * Now that the generic code is guaranteed not to be accessing
6548179c036SDave Chinner  * the linux inode, we can inactivate and reclaim the inode.
655c59d87c4SChristoph Hellwig  */
656c59d87c4SChristoph Hellwig STATIC void
xfs_fs_destroy_inode(struct inode * inode)657c59d87c4SChristoph Hellwig xfs_fs_destroy_inode(
658c59d87c4SChristoph Hellwig 	struct inode		*inode)
659c59d87c4SChristoph Hellwig {
660c59d87c4SChristoph Hellwig 	struct xfs_inode	*ip = XFS_I(inode);
661c59d87c4SChristoph Hellwig 
662c59d87c4SChristoph Hellwig 	trace_xfs_destroy_inode(ip);
663c59d87c4SChristoph Hellwig 
66465523218SChristoph Hellwig 	ASSERT(!rwsem_is_locked(&inode->i_rwsem));
6658179c036SDave Chinner 	XFS_STATS_INC(ip->i_mount, vn_rele);
6668179c036SDave Chinner 	XFS_STATS_INC(ip->i_mount, vn_remove);
667c076ae7aSDarrick J. Wong 	xfs_inode_mark_reclaimable(ip);
668c59d87c4SChristoph Hellwig }
669c59d87c4SChristoph Hellwig 
670c3b1b131SChristoph Hellwig static void
xfs_fs_dirty_inode(struct inode * inode,int flags)671c3b1b131SChristoph Hellwig xfs_fs_dirty_inode(
672c3b1b131SChristoph Hellwig 	struct inode			*inode,
673cbfecb92SLukas Czerner 	int				flags)
674c3b1b131SChristoph Hellwig {
675c3b1b131SChristoph Hellwig 	struct xfs_inode		*ip = XFS_I(inode);
676c3b1b131SChristoph Hellwig 	struct xfs_mount		*mp = ip->i_mount;
677c3b1b131SChristoph Hellwig 	struct xfs_trans		*tp;
678c3b1b131SChristoph Hellwig 
679c3b1b131SChristoph Hellwig 	if (!(inode->i_sb->s_flags & SB_LAZYTIME))
680c3b1b131SChristoph Hellwig 		return;
681cbfecb92SLukas Czerner 
682cbfecb92SLukas Czerner 	/*
683cbfecb92SLukas Czerner 	 * Only do the timestamp update if the inode is dirty (I_DIRTY_SYNC)
684cbfecb92SLukas Czerner 	 * and has dirty timestamp (I_DIRTY_TIME). I_DIRTY_TIME can be passed
685cbfecb92SLukas Czerner 	 * in flags possibly together with I_DIRTY_SYNC.
686cbfecb92SLukas Czerner 	 */
687cbfecb92SLukas Czerner 	if ((flags & ~I_DIRTY_TIME) != I_DIRTY_SYNC || !(flags & I_DIRTY_TIME))
688c3b1b131SChristoph Hellwig 		return;
689c3b1b131SChristoph Hellwig 
690c3b1b131SChristoph Hellwig 	if (xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp))
691c3b1b131SChristoph Hellwig 		return;
692c3b1b131SChristoph Hellwig 	xfs_ilock(ip, XFS_ILOCK_EXCL);
693c3b1b131SChristoph Hellwig 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
694c3b1b131SChristoph Hellwig 	xfs_trans_log_inode(tp, ip, XFS_ILOG_TIMESTAMP);
695c3b1b131SChristoph Hellwig 	xfs_trans_commit(tp);
696c3b1b131SChristoph Hellwig }
697c3b1b131SChristoph Hellwig 
698c59d87c4SChristoph Hellwig /*
699c59d87c4SChristoph Hellwig  * Slab object creation initialisation for the XFS inode.
700c59d87c4SChristoph Hellwig  * This covers only the idempotent fields in the XFS inode;
701c59d87c4SChristoph Hellwig  * all other fields need to be initialised on allocation
702c59d87c4SChristoph Hellwig  * from the slab. This avoids the need to repeatedly initialise
703c59d87c4SChristoph Hellwig  * fields in the xfs inode that left in the initialise state
704c59d87c4SChristoph Hellwig  * when freeing the inode.
705c59d87c4SChristoph Hellwig  */
706c59d87c4SChristoph Hellwig STATIC void
xfs_fs_inode_init_once(void * inode)707c59d87c4SChristoph Hellwig xfs_fs_inode_init_once(
708c59d87c4SChristoph Hellwig 	void			*inode)
709c59d87c4SChristoph Hellwig {
710c59d87c4SChristoph Hellwig 	struct xfs_inode	*ip = inode;
711c59d87c4SChristoph Hellwig 
712c59d87c4SChristoph Hellwig 	memset(ip, 0, sizeof(struct xfs_inode));
713c59d87c4SChristoph Hellwig 
714c59d87c4SChristoph Hellwig 	/* vfs inode */
715c59d87c4SChristoph Hellwig 	inode_init_once(VFS_I(ip));
716c59d87c4SChristoph Hellwig 
717c59d87c4SChristoph Hellwig 	/* xfs inode */
718c59d87c4SChristoph Hellwig 	atomic_set(&ip->i_pincount, 0);
719c59d87c4SChristoph Hellwig 	spin_lock_init(&ip->i_flags_lock);
720c59d87c4SChristoph Hellwig 
721c59d87c4SChristoph Hellwig 	mrlock_init(&ip->i_lock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
722c59d87c4SChristoph Hellwig 		     "xfsino", ip->i_ino);
723c59d87c4SChristoph Hellwig }
724c59d87c4SChristoph Hellwig 
7255132ba8fSDave Chinner /*
7265132ba8fSDave Chinner  * We do an unlocked check for XFS_IDONTCACHE here because we are already
7275132ba8fSDave Chinner  * serialised against cache hits here via the inode->i_lock and igrab() in
7285132ba8fSDave Chinner  * xfs_iget_cache_hit(). Hence a lookup that might clear this flag will not be
7295132ba8fSDave Chinner  * racing with us, and it avoids needing to grab a spinlock here for every inode
7305132ba8fSDave Chinner  * we drop the final reference on.
7315132ba8fSDave Chinner  */
7325132ba8fSDave Chinner STATIC int
xfs_fs_drop_inode(struct inode * inode)7335132ba8fSDave Chinner xfs_fs_drop_inode(
7345132ba8fSDave Chinner 	struct inode		*inode)
7355132ba8fSDave Chinner {
7365132ba8fSDave Chinner 	struct xfs_inode	*ip = XFS_I(inode);
7375132ba8fSDave Chinner 
73817c12bcdSDarrick J. Wong 	/*
73917c12bcdSDarrick J. Wong 	 * If this unlinked inode is in the middle of recovery, don't
74017c12bcdSDarrick J. Wong 	 * drop the inode just yet; log recovery will take care of
74117c12bcdSDarrick J. Wong 	 * that.  See the comment for this inode flag.
74217c12bcdSDarrick J. Wong 	 */
74317c12bcdSDarrick J. Wong 	if (ip->i_flags & XFS_IRECOVERY) {
744e1d06e5fSDave Chinner 		ASSERT(xlog_recovery_needed(ip->i_mount->m_log));
74517c12bcdSDarrick J. Wong 		return 0;
74617c12bcdSDarrick J. Wong 	}
74717c12bcdSDarrick J. Wong 
748dae2f8edSIra Weiny 	return generic_drop_inode(inode);
7495132ba8fSDave Chinner }
7505132ba8fSDave Chinner 
751a943f372SIan Kent static void
xfs_mount_free(struct xfs_mount * mp)752a943f372SIan Kent xfs_mount_free(
753c59d87c4SChristoph Hellwig 	struct xfs_mount	*mp)
754c59d87c4SChristoph Hellwig {
75535a93b14SChristoph Hellwig 	/*
75635a93b14SChristoph Hellwig 	 * Free the buftargs here because blkdev_put needs to be called outside
75735a93b14SChristoph Hellwig 	 * of sb->s_umount, which is held around the call to ->put_super.
75835a93b14SChristoph Hellwig 	 */
75935a93b14SChristoph Hellwig 	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
76035a93b14SChristoph Hellwig 		xfs_free_buftarg(mp->m_logdev_targp);
76135a93b14SChristoph Hellwig 	if (mp->m_rtdev_targp)
76235a93b14SChristoph Hellwig 		xfs_free_buftarg(mp->m_rtdev_targp);
76335a93b14SChristoph Hellwig 	if (mp->m_ddev_targp)
76435a93b14SChristoph Hellwig 		xfs_free_buftarg(mp->m_ddev_targp);
76535a93b14SChristoph Hellwig 
766a76dba3bSDarrick J. Wong 	debugfs_remove(mp->m_debugfs);
767c59d87c4SChristoph Hellwig 	kfree(mp->m_rtname);
768c59d87c4SChristoph Hellwig 	kfree(mp->m_logname);
769a943f372SIan Kent 	kmem_free(mp);
770c59d87c4SChristoph Hellwig }
771c59d87c4SChristoph Hellwig 
772c59d87c4SChristoph Hellwig STATIC int
xfs_fs_sync_fs(struct super_block * sb,int wait)773c59d87c4SChristoph Hellwig xfs_fs_sync_fs(
774c59d87c4SChristoph Hellwig 	struct super_block	*sb,
775c59d87c4SChristoph Hellwig 	int			wait)
776c59d87c4SChristoph Hellwig {
777c59d87c4SChristoph Hellwig 	struct xfs_mount	*mp = XFS_M(sb);
7782d86293cSDarrick J. Wong 	int			error;
779c59d87c4SChristoph Hellwig 
780ab23a776SDave Chinner 	trace_xfs_fs_sync_fs(mp, __return_address);
781ab23a776SDave Chinner 
782c59d87c4SChristoph Hellwig 	/*
78334625c66SChristoph Hellwig 	 * Doing anything during the async pass would be counterproductive.
784c59d87c4SChristoph Hellwig 	 */
78534625c66SChristoph Hellwig 	if (!wait)
786c59d87c4SChristoph Hellwig 		return 0;
787c59d87c4SChristoph Hellwig 
7882d86293cSDarrick J. Wong 	error = xfs_log_force(mp, XFS_LOG_SYNC);
7892d86293cSDarrick J. Wong 	if (error)
7902d86293cSDarrick J. Wong 		return error;
7912d86293cSDarrick J. Wong 
792c59d87c4SChristoph Hellwig 	if (laptop_mode) {
793c59d87c4SChristoph Hellwig 		/*
794c59d87c4SChristoph Hellwig 		 * The disk must be active because we're syncing.
795f661f1e0SDave Chinner 		 * We schedule log work now (now that the disk is
796c59d87c4SChristoph Hellwig 		 * active) instead of later (when it might not be).
797c59d87c4SChristoph Hellwig 		 */
798f661f1e0SDave Chinner 		flush_delayed_work(&mp->m_log->l_work);
799c59d87c4SChristoph Hellwig 	}
800c59d87c4SChristoph Hellwig 
801ab23a776SDave Chinner 	/*
802ab23a776SDave Chinner 	 * If we are called with page faults frozen out, it means we are about
803ab23a776SDave Chinner 	 * to freeze the transaction subsystem. Take the opportunity to shut
804ab23a776SDave Chinner 	 * down inodegc because once SB_FREEZE_FS is set it's too late to
805ab23a776SDave Chinner 	 * prevent inactivation races with freeze. The fs doesn't get called
806ab23a776SDave Chinner 	 * again by the freezing process until after SB_FREEZE_FS has been set,
8076f649091SDarrick J. Wong 	 * so it's now or never.  Same logic applies to speculative allocation
8086f649091SDarrick J. Wong 	 * garbage collection.
809ab23a776SDave Chinner 	 *
810ab23a776SDave Chinner 	 * We don't care if this is a normal syncfs call that does this or
811ab23a776SDave Chinner 	 * freeze that does this - we can run this multiple times without issue
812ab23a776SDave Chinner 	 * and we won't race with a restart because a restart can only occur
813ab23a776SDave Chinner 	 * when the state is either SB_FREEZE_FS or SB_FREEZE_COMPLETE.
814ab23a776SDave Chinner 	 */
8156f649091SDarrick J. Wong 	if (sb->s_writers.frozen == SB_FREEZE_PAGEFAULT) {
816ab23a776SDave Chinner 		xfs_inodegc_stop(mp);
8176f649091SDarrick J. Wong 		xfs_blockgc_stop(mp);
8186f649091SDarrick J. Wong 	}
819ab23a776SDave Chinner 
820c59d87c4SChristoph Hellwig 	return 0;
821c59d87c4SChristoph Hellwig }
822c59d87c4SChristoph Hellwig 
823c59d87c4SChristoph Hellwig STATIC int
xfs_fs_statfs(struct dentry * dentry,struct kstatfs * statp)824c59d87c4SChristoph Hellwig xfs_fs_statfs(
825c59d87c4SChristoph Hellwig 	struct dentry		*dentry,
826c59d87c4SChristoph Hellwig 	struct kstatfs		*statp)
827c59d87c4SChristoph Hellwig {
828c59d87c4SChristoph Hellwig 	struct xfs_mount	*mp = XFS_M(dentry->d_sb);
829c59d87c4SChristoph Hellwig 	xfs_sb_t		*sbp = &mp->m_sb;
8302b0143b5SDavid Howells 	struct xfs_inode	*ip = XFS_I(d_inode(dentry));
831c8ce540dSDarrick J. Wong 	uint64_t		fakeinos, id;
832c8ce540dSDarrick J. Wong 	uint64_t		icount;
833c8ce540dSDarrick J. Wong 	uint64_t		ifree;
834c8ce540dSDarrick J. Wong 	uint64_t		fdblocks;
835c59d87c4SChristoph Hellwig 	xfs_extlen_t		lsize;
836c8ce540dSDarrick J. Wong 	int64_t			ffree;
837c59d87c4SChristoph Hellwig 
8385e672cd6SDave Chinner 	/*
8395e672cd6SDave Chinner 	 * Expedite background inodegc but don't wait. We do not want to block
8405e672cd6SDave Chinner 	 * here waiting hours for a billion extent file to be truncated.
8415e672cd6SDave Chinner 	 */
8425e672cd6SDave Chinner 	xfs_inodegc_push(mp);
84301e8f379SDarrick J. Wong 
844dddde68bSAdam Borowski 	statp->f_type = XFS_SUPER_MAGIC;
845c59d87c4SChristoph Hellwig 	statp->f_namelen = MAXNAMELEN - 1;
846c59d87c4SChristoph Hellwig 
847c59d87c4SChristoph Hellwig 	id = huge_encode_dev(mp->m_ddev_targp->bt_dev);
8486d1349c7SAl Viro 	statp->f_fsid = u64_to_fsid(id);
849c59d87c4SChristoph Hellwig 
850501ab323SDave Chinner 	icount = percpu_counter_sum(&mp->m_icount);
851e88b64eaSDave Chinner 	ifree = percpu_counter_sum(&mp->m_ifree);
8520d485adaSDave Chinner 	fdblocks = percpu_counter_sum(&mp->m_fdblocks);
853c59d87c4SChristoph Hellwig 
854c59d87c4SChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
855c59d87c4SChristoph Hellwig 	statp->f_bsize = sbp->sb_blocksize;
856c59d87c4SChristoph Hellwig 	lsize = sbp->sb_logstart ? sbp->sb_logblocks : 0;
857c59d87c4SChristoph Hellwig 	statp->f_blocks = sbp->sb_dblocks - lsize;
8580d485adaSDave Chinner 	spin_unlock(&mp->m_sb_lock);
8590d485adaSDave Chinner 
860237aac46SZheng Bin 	/* make sure statp->f_bfree does not underflow */
86185bcfa26SDarrick J. Wong 	statp->f_bfree = max_t(int64_t, 0,
86285bcfa26SDarrick J. Wong 				fdblocks - xfs_fdblocks_unavailable(mp));
8630d485adaSDave Chinner 	statp->f_bavail = statp->f_bfree;
8640d485adaSDave Chinner 
86543004b2aSDarrick J. Wong 	fakeinos = XFS_FSB_TO_INO(mp, statp->f_bfree);
8669bb54cb5SDave Chinner 	statp->f_files = min(icount + fakeinos, (uint64_t)XFS_MAXINUMBER);
867ef325959SDarrick J. Wong 	if (M_IGEO(mp)->maxicount)
868c59d87c4SChristoph Hellwig 		statp->f_files = min_t(typeof(statp->f_files),
869c59d87c4SChristoph Hellwig 					statp->f_files,
870ef325959SDarrick J. Wong 					M_IGEO(mp)->maxicount);
871c59d87c4SChristoph Hellwig 
87201f9882eSEric Sandeen 	/* If sb_icount overshot maxicount, report actual allocation */
87301f9882eSEric Sandeen 	statp->f_files = max_t(typeof(statp->f_files),
87401f9882eSEric Sandeen 					statp->f_files,
87501f9882eSEric Sandeen 					sbp->sb_icount);
87601f9882eSEric Sandeen 
877c59d87c4SChristoph Hellwig 	/* make sure statp->f_ffree does not underflow */
878e88b64eaSDave Chinner 	ffree = statp->f_files - (icount - ifree);
879c8ce540dSDarrick J. Wong 	statp->f_ffree = max_t(int64_t, ffree, 0);
880c59d87c4SChristoph Hellwig 
881a0158315SRichard Wareing 	if (XFS_IS_REALTIME_MOUNT(mp) &&
882db07349dSChristoph Hellwig 	    (ip->i_diflags & (XFS_DIFLAG_RTINHERIT | XFS_DIFLAG_REALTIME))) {
8832229276cSDarrick J. Wong 		s64	freertx;
8842229276cSDarrick J. Wong 
885a0158315SRichard Wareing 		statp->f_blocks = sbp->sb_rblocks;
8862229276cSDarrick J. Wong 		freertx = percpu_counter_sum_positive(&mp->m_frextents);
8872229276cSDarrick J. Wong 		statp->f_bavail = statp->f_bfree = freertx * sbp->sb_rextsize;
888a0158315SRichard Wareing 	}
889a0158315SRichard Wareing 
890*1603b0b6SDarrick J. Wong 	if ((ip->i_diflags & XFS_DIFLAG_PROJINHERIT) &&
891*1603b0b6SDarrick J. Wong 	    ((mp->m_qflags & (XFS_PQUOTA_ACCT|XFS_PQUOTA_ENFD))) ==
892*1603b0b6SDarrick J. Wong 			      (XFS_PQUOTA_ACCT|XFS_PQUOTA_ENFD))
893*1603b0b6SDarrick J. Wong 		xfs_qm_statvfs(ip, statp);
894*1603b0b6SDarrick J. Wong 
895c59d87c4SChristoph Hellwig 	return 0;
896c59d87c4SChristoph Hellwig }
897c59d87c4SChristoph Hellwig 
898c59d87c4SChristoph Hellwig STATIC void
xfs_save_resvblks(struct xfs_mount * mp)899c59d87c4SChristoph Hellwig xfs_save_resvblks(struct xfs_mount *mp)
900c59d87c4SChristoph Hellwig {
901c8ce540dSDarrick J. Wong 	uint64_t resblks = 0;
902c59d87c4SChristoph Hellwig 
903c59d87c4SChristoph Hellwig 	mp->m_resblks_save = mp->m_resblks;
904c59d87c4SChristoph Hellwig 	xfs_reserve_blocks(mp, &resblks, NULL);
905c59d87c4SChristoph Hellwig }
906c59d87c4SChristoph Hellwig 
907c59d87c4SChristoph Hellwig STATIC void
xfs_restore_resvblks(struct xfs_mount * mp)908c59d87c4SChristoph Hellwig xfs_restore_resvblks(struct xfs_mount *mp)
909c59d87c4SChristoph Hellwig {
910c8ce540dSDarrick J. Wong 	uint64_t resblks;
911c59d87c4SChristoph Hellwig 
912c59d87c4SChristoph Hellwig 	if (mp->m_resblks_save) {
913c59d87c4SChristoph Hellwig 		resblks = mp->m_resblks_save;
914c59d87c4SChristoph Hellwig 		mp->m_resblks_save = 0;
915c59d87c4SChristoph Hellwig 	} else
916c59d87c4SChristoph Hellwig 		resblks = xfs_default_resblks(mp);
917c59d87c4SChristoph Hellwig 
918c59d87c4SChristoph Hellwig 	xfs_reserve_blocks(mp, &resblks, NULL);
919c59d87c4SChristoph Hellwig }
920c59d87c4SChristoph Hellwig 
921c7eea6f7SDave Chinner /*
922c59d87c4SChristoph Hellwig  * Second stage of a freeze. The data is already frozen so we only
92361e63ecbSDave Chinner  * need to take care of the metadata. Once that's done sync the superblock
92461e63ecbSDave Chinner  * to the log to dirty it in case of a crash while frozen. This ensures that we
92561e63ecbSDave Chinner  * will recover the unlinked inode lists on the next mount.
926c59d87c4SChristoph Hellwig  */
927c59d87c4SChristoph Hellwig STATIC int
xfs_fs_freeze(struct super_block * sb)928c59d87c4SChristoph Hellwig xfs_fs_freeze(
929c59d87c4SChristoph Hellwig 	struct super_block	*sb)
930c59d87c4SChristoph Hellwig {
931c59d87c4SChristoph Hellwig 	struct xfs_mount	*mp = XFS_M(sb);
932c3f2375bSWaiman Long 	unsigned int		flags;
933c3f2375bSWaiman Long 	int			ret;
934c59d87c4SChristoph Hellwig 
935c3f2375bSWaiman Long 	/*
936c3f2375bSWaiman Long 	 * The filesystem is now frozen far enough that memory reclaim
937c3f2375bSWaiman Long 	 * cannot safely operate on the filesystem. Hence we need to
938c3f2375bSWaiman Long 	 * set a GFP_NOFS context here to avoid recursion deadlocks.
939c3f2375bSWaiman Long 	 */
940c3f2375bSWaiman Long 	flags = memalloc_nofs_save();
941c59d87c4SChristoph Hellwig 	xfs_save_resvblks(mp);
9425b0ad7c2SBrian Foster 	ret = xfs_log_quiesce(mp);
943c3f2375bSWaiman Long 	memalloc_nofs_restore(flags);
944ab23a776SDave Chinner 
945ab23a776SDave Chinner 	/*
946ab23a776SDave Chinner 	 * For read-write filesystems, we need to restart the inodegc on error
947ab23a776SDave Chinner 	 * because we stopped it at SB_FREEZE_PAGEFAULT level and a thaw is not
948ab23a776SDave Chinner 	 * going to be run to restart it now.  We are at SB_FREEZE_FS level
949ab23a776SDave Chinner 	 * here, so we can restart safely without racing with a stop in
950ab23a776SDave Chinner 	 * xfs_fs_sync_fs().
951ab23a776SDave Chinner 	 */
9522e973b2cSDave Chinner 	if (ret && !xfs_is_readonly(mp)) {
9536f649091SDarrick J. Wong 		xfs_blockgc_start(mp);
954ab23a776SDave Chinner 		xfs_inodegc_start(mp);
9556f649091SDarrick J. Wong 	}
956ab23a776SDave Chinner 
957c3f2375bSWaiman Long 	return ret;
958c59d87c4SChristoph Hellwig }
959c59d87c4SChristoph Hellwig 
960c59d87c4SChristoph Hellwig STATIC int
xfs_fs_unfreeze(struct super_block * sb)961c59d87c4SChristoph Hellwig xfs_fs_unfreeze(
962c59d87c4SChristoph Hellwig 	struct super_block	*sb)
963c59d87c4SChristoph Hellwig {
964c59d87c4SChristoph Hellwig 	struct xfs_mount	*mp = XFS_M(sb);
965c59d87c4SChristoph Hellwig 
966c59d87c4SChristoph Hellwig 	xfs_restore_resvblks(mp);
967f661f1e0SDave Chinner 	xfs_log_work_queue(mp);
968ab23a776SDave Chinner 
969ab23a776SDave Chinner 	/*
970ab23a776SDave Chinner 	 * Don't reactivate the inodegc worker on a readonly filesystem because
9716f649091SDarrick J. Wong 	 * inodes are sent directly to reclaim.  Don't reactivate the blockgc
9726f649091SDarrick J. Wong 	 * worker because there are no speculative preallocations on a readonly
9736f649091SDarrick J. Wong 	 * filesystem.
974ab23a776SDave Chinner 	 */
9752e973b2cSDave Chinner 	if (!xfs_is_readonly(mp)) {
976c9a6526fSDarrick J. Wong 		xfs_blockgc_start(mp);
977ab23a776SDave Chinner 		xfs_inodegc_start(mp);
9786f649091SDarrick J. Wong 	}
979ab23a776SDave Chinner 
980c59d87c4SChristoph Hellwig 	return 0;
981c59d87c4SChristoph Hellwig }
982c59d87c4SChristoph Hellwig 
983c59d87c4SChristoph Hellwig /*
984c59d87c4SChristoph Hellwig  * This function fills in xfs_mount_t fields based on mount args.
985c59d87c4SChristoph Hellwig  * Note: the superblock _has_ now been read in.
986c59d87c4SChristoph Hellwig  */
987c59d87c4SChristoph Hellwig STATIC int
xfs_finish_flags(struct xfs_mount * mp)988c59d87c4SChristoph Hellwig xfs_finish_flags(
989c59d87c4SChristoph Hellwig 	struct xfs_mount	*mp)
990c59d87c4SChristoph Hellwig {
991c59d87c4SChristoph Hellwig 	/* Fail a mount where the logbuf is smaller than the log stripe */
99238c26bfdSDave Chinner 	if (xfs_has_logv2(mp)) {
993c59d87c4SChristoph Hellwig 		if (mp->m_logbsize <= 0 &&
994c59d87c4SChristoph Hellwig 		    mp->m_sb.sb_logsunit > XLOG_BIG_RECORD_BSIZE) {
995c59d87c4SChristoph Hellwig 			mp->m_logbsize = mp->m_sb.sb_logsunit;
996c59d87c4SChristoph Hellwig 		} else if (mp->m_logbsize > 0 &&
997c59d87c4SChristoph Hellwig 			   mp->m_logbsize < mp->m_sb.sb_logsunit) {
998c59d87c4SChristoph Hellwig 			xfs_warn(mp,
999c59d87c4SChristoph Hellwig 		"logbuf size must be greater than or equal to log stripe size");
10002451337dSDave Chinner 			return -EINVAL;
1001c59d87c4SChristoph Hellwig 		}
1002c59d87c4SChristoph Hellwig 	} else {
1003c59d87c4SChristoph Hellwig 		/* Fail a mount if the logbuf is larger than 32K */
1004c59d87c4SChristoph Hellwig 		if (mp->m_logbsize > XLOG_BIG_RECORD_BSIZE) {
1005c59d87c4SChristoph Hellwig 			xfs_warn(mp,
1006c59d87c4SChristoph Hellwig 		"logbuf size for version 1 logs must be 16K or 32K");
10072451337dSDave Chinner 			return -EINVAL;
1008c59d87c4SChristoph Hellwig 		}
1009c59d87c4SChristoph Hellwig 	}
1010c59d87c4SChristoph Hellwig 
1011c59d87c4SChristoph Hellwig 	/*
1012d3eaace8SDave Chinner 	 * V5 filesystems always use attr2 format for attributes.
1013d3eaace8SDave Chinner 	 */
10140560f31aSDave Chinner 	if (xfs_has_crc(mp) && xfs_has_noattr2(mp)) {
10152e74af0eSEric Sandeen 		xfs_warn(mp, "Cannot mount a V5 filesystem as noattr2. "
10162e74af0eSEric Sandeen 			     "attr2 is always enabled for V5 filesystems.");
10172451337dSDave Chinner 		return -EINVAL;
1018d3eaace8SDave Chinner 	}
1019d3eaace8SDave Chinner 
1020d3eaace8SDave Chinner 	/*
1021c59d87c4SChristoph Hellwig 	 * prohibit r/w mounts of read-only filesystems
1022c59d87c4SChristoph Hellwig 	 */
10232e973b2cSDave Chinner 	if ((mp->m_sb.sb_flags & XFS_SBF_READONLY) && !xfs_is_readonly(mp)) {
1024c59d87c4SChristoph Hellwig 		xfs_warn(mp,
1025c59d87c4SChristoph Hellwig 			"cannot mount a read-only filesystem as read-write");
10262451337dSDave Chinner 		return -EROFS;
1027c59d87c4SChristoph Hellwig 	}
1028c59d87c4SChristoph Hellwig 
1029149e53afSChristoph Hellwig 	if ((mp->m_qflags & XFS_GQUOTA_ACCT) &&
1030149e53afSChristoph Hellwig 	    (mp->m_qflags & XFS_PQUOTA_ACCT) &&
103138c26bfdSDave Chinner 	    !xfs_has_pquotino(mp)) {
1032d892d586SChandra Seetharaman 		xfs_warn(mp,
1033d892d586SChandra Seetharaman 		  "Super block does not support project and group quota together");
10342451337dSDave Chinner 		return -EINVAL;
1035d892d586SChandra Seetharaman 	}
1036d892d586SChandra Seetharaman 
1037c59d87c4SChristoph Hellwig 	return 0;
1038c59d87c4SChristoph Hellwig }
1039c59d87c4SChristoph Hellwig 
10405681ca40SDave Chinner static int
xfs_init_percpu_counters(struct xfs_mount * mp)10415681ca40SDave Chinner xfs_init_percpu_counters(
10425681ca40SDave Chinner 	struct xfs_mount	*mp)
10435681ca40SDave Chinner {
10445681ca40SDave Chinner 	int		error;
10455681ca40SDave Chinner 
10465681ca40SDave Chinner 	error = percpu_counter_init(&mp->m_icount, 0, GFP_KERNEL);
10475681ca40SDave Chinner 	if (error)
10485e9383f9SJoe Perches 		return -ENOMEM;
10495681ca40SDave Chinner 
10505681ca40SDave Chinner 	error = percpu_counter_init(&mp->m_ifree, 0, GFP_KERNEL);
10515681ca40SDave Chinner 	if (error)
10525681ca40SDave Chinner 		goto free_icount;
10535681ca40SDave Chinner 
10545681ca40SDave Chinner 	error = percpu_counter_init(&mp->m_fdblocks, 0, GFP_KERNEL);
10555681ca40SDave Chinner 	if (error)
10565681ca40SDave Chinner 		goto free_ifree;
10575681ca40SDave Chinner 
10589fe82b8cSDarrick J. Wong 	error = percpu_counter_init(&mp->m_delalloc_blks, 0, GFP_KERNEL);
10599fe82b8cSDarrick J. Wong 	if (error)
10609fe82b8cSDarrick J. Wong 		goto free_fdblocks;
10619fe82b8cSDarrick J. Wong 
10622229276cSDarrick J. Wong 	error = percpu_counter_init(&mp->m_frextents, 0, GFP_KERNEL);
10632229276cSDarrick J. Wong 	if (error)
10642229276cSDarrick J. Wong 		goto free_delalloc;
10652229276cSDarrick J. Wong 
10665681ca40SDave Chinner 	return 0;
10675681ca40SDave Chinner 
10682229276cSDarrick J. Wong free_delalloc:
10692229276cSDarrick J. Wong 	percpu_counter_destroy(&mp->m_delalloc_blks);
10709fe82b8cSDarrick J. Wong free_fdblocks:
10719fe82b8cSDarrick J. Wong 	percpu_counter_destroy(&mp->m_fdblocks);
10725681ca40SDave Chinner free_ifree:
10735681ca40SDave Chinner 	percpu_counter_destroy(&mp->m_ifree);
10745681ca40SDave Chinner free_icount:
10755681ca40SDave Chinner 	percpu_counter_destroy(&mp->m_icount);
10765681ca40SDave Chinner 	return -ENOMEM;
10775681ca40SDave Chinner }
10785681ca40SDave Chinner 
10795681ca40SDave Chinner void
xfs_reinit_percpu_counters(struct xfs_mount * mp)10805681ca40SDave Chinner xfs_reinit_percpu_counters(
10815681ca40SDave Chinner 	struct xfs_mount	*mp)
10825681ca40SDave Chinner {
10835681ca40SDave Chinner 	percpu_counter_set(&mp->m_icount, mp->m_sb.sb_icount);
10845681ca40SDave Chinner 	percpu_counter_set(&mp->m_ifree, mp->m_sb.sb_ifree);
10855681ca40SDave Chinner 	percpu_counter_set(&mp->m_fdblocks, mp->m_sb.sb_fdblocks);
10862229276cSDarrick J. Wong 	percpu_counter_set(&mp->m_frextents, mp->m_sb.sb_frextents);
10875681ca40SDave Chinner }
10885681ca40SDave Chinner 
10895681ca40SDave Chinner static void
xfs_destroy_percpu_counters(struct xfs_mount * mp)10905681ca40SDave Chinner xfs_destroy_percpu_counters(
10915681ca40SDave Chinner 	struct xfs_mount	*mp)
10925681ca40SDave Chinner {
10935681ca40SDave Chinner 	percpu_counter_destroy(&mp->m_icount);
10945681ca40SDave Chinner 	percpu_counter_destroy(&mp->m_ifree);
10955681ca40SDave Chinner 	percpu_counter_destroy(&mp->m_fdblocks);
109675c8c50fSDave Chinner 	ASSERT(xfs_is_shutdown(mp) ||
10979fe82b8cSDarrick J. Wong 	       percpu_counter_sum(&mp->m_delalloc_blks) == 0);
10989fe82b8cSDarrick J. Wong 	percpu_counter_destroy(&mp->m_delalloc_blks);
10992229276cSDarrick J. Wong 	percpu_counter_destroy(&mp->m_frextents);
11005681ca40SDave Chinner }
11015681ca40SDave Chinner 
1102ab23a776SDave Chinner static int
xfs_inodegc_init_percpu(struct xfs_mount * mp)1103ab23a776SDave Chinner xfs_inodegc_init_percpu(
1104ab23a776SDave Chinner 	struct xfs_mount	*mp)
1105ab23a776SDave Chinner {
1106ab23a776SDave Chinner 	struct xfs_inodegc	*gc;
1107ab23a776SDave Chinner 	int			cpu;
1108ab23a776SDave Chinner 
1109ab23a776SDave Chinner 	mp->m_inodegc = alloc_percpu(struct xfs_inodegc);
1110ab23a776SDave Chinner 	if (!mp->m_inodegc)
1111ab23a776SDave Chinner 		return -ENOMEM;
1112ab23a776SDave Chinner 
1113ab23a776SDave Chinner 	for_each_possible_cpu(cpu) {
1114ab23a776SDave Chinner 		gc = per_cpu_ptr(mp->m_inodegc, cpu);
1115b37c4c83SDarrick J. Wong 		gc->cpu = cpu;
111662334fabSDarrick J. Wong 		gc->mp = mp;
1117ab23a776SDave Chinner 		init_llist_head(&gc->list);
1118ab23a776SDave Chinner 		gc->items = 0;
1119d4d12c02SDave Chinner 		gc->error = 0;
11207cf2b0f9SDave Chinner 		INIT_DELAYED_WORK(&gc->work, xfs_inodegc_worker);
1121ab23a776SDave Chinner 	}
1122ab23a776SDave Chinner 	return 0;
1123ab23a776SDave Chinner }
1124ab23a776SDave Chinner 
1125ab23a776SDave Chinner static void
xfs_inodegc_free_percpu(struct xfs_mount * mp)1126ab23a776SDave Chinner xfs_inodegc_free_percpu(
1127ab23a776SDave Chinner 	struct xfs_mount	*mp)
1128ab23a776SDave Chinner {
1129ab23a776SDave Chinner 	if (!mp->m_inodegc)
1130ab23a776SDave Chinner 		return;
1131ab23a776SDave Chinner 	free_percpu(mp->m_inodegc);
1132ab23a776SDave Chinner }
1133ab23a776SDave Chinner 
11342f8d66b3SIan Kent static void
xfs_fs_put_super(struct super_block * sb)11352f8d66b3SIan Kent xfs_fs_put_super(
11362f8d66b3SIan Kent 	struct super_block	*sb)
11372f8d66b3SIan Kent {
11382f8d66b3SIan Kent 	struct xfs_mount	*mp = XFS_M(sb);
11392f8d66b3SIan Kent 
114064c80dfdSLukas Herbolt 	xfs_notice(mp, "Unmounting Filesystem %pU", &mp->m_sb.sb_uuid);
11412f8d66b3SIan Kent 	xfs_filestream_unmount(mp);
11422f8d66b3SIan Kent 	xfs_unmountfs(mp);
11432f8d66b3SIan Kent 
11442f8d66b3SIan Kent 	xfs_freesb(mp);
1145d7a74cadSDarrick J. Wong 	xchk_mount_stats_free(mp);
11462f8d66b3SIan Kent 	free_percpu(mp->m_stats.xs_stats);
1147ab23a776SDave Chinner 	xfs_inodegc_free_percpu(mp);
11482f8d66b3SIan Kent 	xfs_destroy_percpu_counters(mp);
11492f8d66b3SIan Kent 	xfs_destroy_mount_workqueues(mp);
115035a93b14SChristoph Hellwig 	xfs_shutdown_devices(mp);
11512f8d66b3SIan Kent }
11522f8d66b3SIan Kent 
11532f8d66b3SIan Kent static long
xfs_fs_nr_cached_objects(struct super_block * sb,struct shrink_control * sc)11542f8d66b3SIan Kent xfs_fs_nr_cached_objects(
11552f8d66b3SIan Kent 	struct super_block	*sb,
11562f8d66b3SIan Kent 	struct shrink_control	*sc)
11572f8d66b3SIan Kent {
11582f8d66b3SIan Kent 	/* Paranoia: catch incorrect calls during mount setup or teardown */
11592f8d66b3SIan Kent 	if (WARN_ON_ONCE(!sb->s_fs_info))
11602f8d66b3SIan Kent 		return 0;
11612f8d66b3SIan Kent 	return xfs_reclaim_inodes_count(XFS_M(sb));
11622f8d66b3SIan Kent }
11632f8d66b3SIan Kent 
11642f8d66b3SIan Kent static long
xfs_fs_free_cached_objects(struct super_block * sb,struct shrink_control * sc)11652f8d66b3SIan Kent xfs_fs_free_cached_objects(
11662f8d66b3SIan Kent 	struct super_block	*sb,
11672f8d66b3SIan Kent 	struct shrink_control	*sc)
11682f8d66b3SIan Kent {
11692f8d66b3SIan Kent 	return xfs_reclaim_inodes_nr(XFS_M(sb), sc->nr_to_scan);
11702f8d66b3SIan Kent }
11712f8d66b3SIan Kent 
1172e7caa877SChristoph Hellwig static void
xfs_fs_shutdown(struct super_block * sb)1173e7caa877SChristoph Hellwig xfs_fs_shutdown(
1174e7caa877SChristoph Hellwig 	struct super_block	*sb)
1175e7caa877SChristoph Hellwig {
1176e7caa877SChristoph Hellwig 	xfs_force_shutdown(XFS_M(sb), SHUTDOWN_DEVICE_REMOVED);
1177e7caa877SChristoph Hellwig }
1178e7caa877SChristoph Hellwig 
11792f8d66b3SIan Kent static const struct super_operations xfs_super_operations = {
11802f8d66b3SIan Kent 	.alloc_inode		= xfs_fs_alloc_inode,
11812f8d66b3SIan Kent 	.destroy_inode		= xfs_fs_destroy_inode,
11822f8d66b3SIan Kent 	.dirty_inode		= xfs_fs_dirty_inode,
11832f8d66b3SIan Kent 	.drop_inode		= xfs_fs_drop_inode,
11842f8d66b3SIan Kent 	.put_super		= xfs_fs_put_super,
11852f8d66b3SIan Kent 	.sync_fs		= xfs_fs_sync_fs,
11862f8d66b3SIan Kent 	.freeze_fs		= xfs_fs_freeze,
11872f8d66b3SIan Kent 	.unfreeze_fs		= xfs_fs_unfreeze,
11882f8d66b3SIan Kent 	.statfs			= xfs_fs_statfs,
11892f8d66b3SIan Kent 	.show_options		= xfs_fs_show_options,
11902f8d66b3SIan Kent 	.nr_cached_objects	= xfs_fs_nr_cached_objects,
11912f8d66b3SIan Kent 	.free_cached_objects	= xfs_fs_free_cached_objects,
1192e7caa877SChristoph Hellwig 	.shutdown		= xfs_fs_shutdown,
11932f8d66b3SIan Kent };
11942f8d66b3SIan Kent 
119573e5fff9SIan Kent static int
suffix_kstrtoint(const char * s,unsigned int base,int * res)11968757c38fSIan Kent suffix_kstrtoint(
11978757c38fSIan Kent 	const char	*s,
11988757c38fSIan Kent 	unsigned int	base,
11998757c38fSIan Kent 	int		*res)
12008757c38fSIan Kent {
12018757c38fSIan Kent 	int		last, shift_left_factor = 0, _res;
12028757c38fSIan Kent 	char		*value;
12038757c38fSIan Kent 	int		ret = 0;
12048757c38fSIan Kent 
12058757c38fSIan Kent 	value = kstrdup(s, GFP_KERNEL);
12068757c38fSIan Kent 	if (!value)
12078757c38fSIan Kent 		return -ENOMEM;
12088757c38fSIan Kent 
12098757c38fSIan Kent 	last = strlen(value) - 1;
12108757c38fSIan Kent 	if (value[last] == 'K' || value[last] == 'k') {
12118757c38fSIan Kent 		shift_left_factor = 10;
12128757c38fSIan Kent 		value[last] = '\0';
12138757c38fSIan Kent 	}
12148757c38fSIan Kent 	if (value[last] == 'M' || value[last] == 'm') {
12158757c38fSIan Kent 		shift_left_factor = 20;
12168757c38fSIan Kent 		value[last] = '\0';
12178757c38fSIan Kent 	}
12188757c38fSIan Kent 	if (value[last] == 'G' || value[last] == 'g') {
12198757c38fSIan Kent 		shift_left_factor = 30;
12208757c38fSIan Kent 		value[last] = '\0';
12218757c38fSIan Kent 	}
12228757c38fSIan Kent 
12238757c38fSIan Kent 	if (kstrtoint(value, base, &_res))
12248757c38fSIan Kent 		ret = -EINVAL;
12258757c38fSIan Kent 	kfree(value);
12268757c38fSIan Kent 	*res = _res << shift_left_factor;
12278757c38fSIan Kent 	return ret;
12288757c38fSIan Kent }
12298757c38fSIan Kent 
123092cf7d36SPavel Reichl static inline void
xfs_fs_warn_deprecated(struct fs_context * fc,struct fs_parameter * param,uint64_t flag,bool value)123192cf7d36SPavel Reichl xfs_fs_warn_deprecated(
123292cf7d36SPavel Reichl 	struct fs_context	*fc,
123392cf7d36SPavel Reichl 	struct fs_parameter	*param,
123492cf7d36SPavel Reichl 	uint64_t		flag,
123592cf7d36SPavel Reichl 	bool			value)
123692cf7d36SPavel Reichl {
123792cf7d36SPavel Reichl 	/* Don't print the warning if reconfiguring and current mount point
123892cf7d36SPavel Reichl 	 * already had the flag set
123992cf7d36SPavel Reichl 	 */
124092cf7d36SPavel Reichl 	if ((fc->purpose & FS_CONTEXT_FOR_RECONFIGURE) &&
12410560f31aSDave Chinner             !!(XFS_M(fc->root->d_sb)->m_features & flag) == value)
124292cf7d36SPavel Reichl 		return;
124392cf7d36SPavel Reichl 	xfs_warn(fc->s_fs_info, "%s mount option is deprecated.", param->key);
124492cf7d36SPavel Reichl }
124592cf7d36SPavel Reichl 
12468757c38fSIan Kent /*
12478757c38fSIan Kent  * Set mount state from a mount option.
12488757c38fSIan Kent  *
12498757c38fSIan Kent  * NOTE: mp->m_super is NULL here!
12508757c38fSIan Kent  */
12518757c38fSIan Kent static int
xfs_fs_parse_param(struct fs_context * fc,struct fs_parameter * param)12521e5c39dfSDarrick J. Wong xfs_fs_parse_param(
12538757c38fSIan Kent 	struct fs_context	*fc,
12548757c38fSIan Kent 	struct fs_parameter	*param)
12558757c38fSIan Kent {
12560f98b4ecSPavel Reichl 	struct xfs_mount	*parsing_mp = fc->s_fs_info;
12578757c38fSIan Kent 	struct fs_parse_result	result;
12588757c38fSIan Kent 	int			size = 0;
12598757c38fSIan Kent 	int			opt;
12608757c38fSIan Kent 
1261d7167b14SAl Viro 	opt = fs_parse(fc, xfs_fs_parameters, param, &result);
12628757c38fSIan Kent 	if (opt < 0)
12638757c38fSIan Kent 		return opt;
12648757c38fSIan Kent 
12658757c38fSIan Kent 	switch (opt) {
12668757c38fSIan Kent 	case Opt_logbufs:
12670f98b4ecSPavel Reichl 		parsing_mp->m_logbufs = result.uint_32;
12688757c38fSIan Kent 		return 0;
12698757c38fSIan Kent 	case Opt_logbsize:
12700f98b4ecSPavel Reichl 		if (suffix_kstrtoint(param->string, 10, &parsing_mp->m_logbsize))
12718757c38fSIan Kent 			return -EINVAL;
12728757c38fSIan Kent 		return 0;
12738757c38fSIan Kent 	case Opt_logdev:
12740f98b4ecSPavel Reichl 		kfree(parsing_mp->m_logname);
12750f98b4ecSPavel Reichl 		parsing_mp->m_logname = kstrdup(param->string, GFP_KERNEL);
12760f98b4ecSPavel Reichl 		if (!parsing_mp->m_logname)
12778757c38fSIan Kent 			return -ENOMEM;
12788757c38fSIan Kent 		return 0;
12798757c38fSIan Kent 	case Opt_rtdev:
12800f98b4ecSPavel Reichl 		kfree(parsing_mp->m_rtname);
12810f98b4ecSPavel Reichl 		parsing_mp->m_rtname = kstrdup(param->string, GFP_KERNEL);
12820f98b4ecSPavel Reichl 		if (!parsing_mp->m_rtname)
12838757c38fSIan Kent 			return -ENOMEM;
12848757c38fSIan Kent 		return 0;
12858757c38fSIan Kent 	case Opt_allocsize:
12868757c38fSIan Kent 		if (suffix_kstrtoint(param->string, 10, &size))
12878757c38fSIan Kent 			return -EINVAL;
12880f98b4ecSPavel Reichl 		parsing_mp->m_allocsize_log = ffs(size) - 1;
12890560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_ALLOCSIZE;
12908757c38fSIan Kent 		return 0;
12918757c38fSIan Kent 	case Opt_grpid:
12928757c38fSIan Kent 	case Opt_bsdgroups:
12930560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_GRPID;
12948757c38fSIan Kent 		return 0;
12958757c38fSIan Kent 	case Opt_nogrpid:
12968757c38fSIan Kent 	case Opt_sysvgroups:
12970560f31aSDave Chinner 		parsing_mp->m_features &= ~XFS_FEAT_GRPID;
12988757c38fSIan Kent 		return 0;
12998757c38fSIan Kent 	case Opt_wsync:
13000560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_WSYNC;
13018757c38fSIan Kent 		return 0;
13028757c38fSIan Kent 	case Opt_norecovery:
13030560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_NORECOVERY;
13048757c38fSIan Kent 		return 0;
13058757c38fSIan Kent 	case Opt_noalign:
13060560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_NOALIGN;
13078757c38fSIan Kent 		return 0;
13088757c38fSIan Kent 	case Opt_swalloc:
13090560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_SWALLOC;
13108757c38fSIan Kent 		return 0;
13118757c38fSIan Kent 	case Opt_sunit:
13120f98b4ecSPavel Reichl 		parsing_mp->m_dalign = result.uint_32;
13138757c38fSIan Kent 		return 0;
13148757c38fSIan Kent 	case Opt_swidth:
13150f98b4ecSPavel Reichl 		parsing_mp->m_swidth = result.uint_32;
13168757c38fSIan Kent 		return 0;
13178757c38fSIan Kent 	case Opt_inode32:
13180560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_SMALL_INUMS;
13198757c38fSIan Kent 		return 0;
13208757c38fSIan Kent 	case Opt_inode64:
13210560f31aSDave Chinner 		parsing_mp->m_features &= ~XFS_FEAT_SMALL_INUMS;
13228757c38fSIan Kent 		return 0;
13238757c38fSIan Kent 	case Opt_nouuid:
13240560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_NOUUID;
13258757c38fSIan Kent 		return 0;
13268757c38fSIan Kent 	case Opt_largeio:
13270560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_LARGE_IOSIZE;
13288757c38fSIan Kent 		return 0;
13298757c38fSIan Kent 	case Opt_nolargeio:
13300560f31aSDave Chinner 		parsing_mp->m_features &= ~XFS_FEAT_LARGE_IOSIZE;
13318757c38fSIan Kent 		return 0;
13328757c38fSIan Kent 	case Opt_filestreams:
13330560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_FILESTREAMS;
13348757c38fSIan Kent 		return 0;
13358757c38fSIan Kent 	case Opt_noquota:
13360f98b4ecSPavel Reichl 		parsing_mp->m_qflags &= ~XFS_ALL_QUOTA_ACCT;
13370f98b4ecSPavel Reichl 		parsing_mp->m_qflags &= ~XFS_ALL_QUOTA_ENFD;
13388757c38fSIan Kent 		return 0;
13398757c38fSIan Kent 	case Opt_quota:
13408757c38fSIan Kent 	case Opt_uquota:
13418757c38fSIan Kent 	case Opt_usrquota:
1342149e53afSChristoph Hellwig 		parsing_mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ENFD);
13438757c38fSIan Kent 		return 0;
13448757c38fSIan Kent 	case Opt_qnoenforce:
13458757c38fSIan Kent 	case Opt_uqnoenforce:
1346149e53afSChristoph Hellwig 		parsing_mp->m_qflags |= XFS_UQUOTA_ACCT;
13470f98b4ecSPavel Reichl 		parsing_mp->m_qflags &= ~XFS_UQUOTA_ENFD;
13488757c38fSIan Kent 		return 0;
13498757c38fSIan Kent 	case Opt_pquota:
13508757c38fSIan Kent 	case Opt_prjquota:
1351149e53afSChristoph Hellwig 		parsing_mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ENFD);
13528757c38fSIan Kent 		return 0;
13538757c38fSIan Kent 	case Opt_pqnoenforce:
1354149e53afSChristoph Hellwig 		parsing_mp->m_qflags |= XFS_PQUOTA_ACCT;
13550f98b4ecSPavel Reichl 		parsing_mp->m_qflags &= ~XFS_PQUOTA_ENFD;
13568757c38fSIan Kent 		return 0;
13578757c38fSIan Kent 	case Opt_gquota:
13588757c38fSIan Kent 	case Opt_grpquota:
1359149e53afSChristoph Hellwig 		parsing_mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ENFD);
13608757c38fSIan Kent 		return 0;
13618757c38fSIan Kent 	case Opt_gqnoenforce:
1362149e53afSChristoph Hellwig 		parsing_mp->m_qflags |= XFS_GQUOTA_ACCT;
13630f98b4ecSPavel Reichl 		parsing_mp->m_qflags &= ~XFS_GQUOTA_ENFD;
13648757c38fSIan Kent 		return 0;
13658757c38fSIan Kent 	case Opt_discard:
13660560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_DISCARD;
13678757c38fSIan Kent 		return 0;
13688757c38fSIan Kent 	case Opt_nodiscard:
13690560f31aSDave Chinner 		parsing_mp->m_features &= ~XFS_FEAT_DISCARD;
13708757c38fSIan Kent 		return 0;
13718757c38fSIan Kent #ifdef CONFIG_FS_DAX
13728757c38fSIan Kent 	case Opt_dax:
13730f98b4ecSPavel Reichl 		xfs_mount_set_dax_mode(parsing_mp, XFS_DAX_ALWAYS);
13748d6c3446SIra Weiny 		return 0;
13758d6c3446SIra Weiny 	case Opt_dax_enum:
13760f98b4ecSPavel Reichl 		xfs_mount_set_dax_mode(parsing_mp, result.uint_32);
13778757c38fSIan Kent 		return 0;
13788757c38fSIan Kent #endif
1379c23c393eSPavel Reichl 	/* Following mount options will be removed in September 2025 */
1380c23c393eSPavel Reichl 	case Opt_ikeep:
13810560f31aSDave Chinner 		xfs_fs_warn_deprecated(fc, param, XFS_FEAT_IKEEP, true);
13820560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_IKEEP;
1383c23c393eSPavel Reichl 		return 0;
1384c23c393eSPavel Reichl 	case Opt_noikeep:
13850560f31aSDave Chinner 		xfs_fs_warn_deprecated(fc, param, XFS_FEAT_IKEEP, false);
13860560f31aSDave Chinner 		parsing_mp->m_features &= ~XFS_FEAT_IKEEP;
1387c23c393eSPavel Reichl 		return 0;
1388c23c393eSPavel Reichl 	case Opt_attr2:
13890560f31aSDave Chinner 		xfs_fs_warn_deprecated(fc, param, XFS_FEAT_ATTR2, true);
13900560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_ATTR2;
1391c23c393eSPavel Reichl 		return 0;
1392c23c393eSPavel Reichl 	case Opt_noattr2:
13930560f31aSDave Chinner 		xfs_fs_warn_deprecated(fc, param, XFS_FEAT_NOATTR2, true);
13940560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_NOATTR2;
1395c23c393eSPavel Reichl 		return 0;
13968757c38fSIan Kent 	default:
13970f98b4ecSPavel Reichl 		xfs_warn(parsing_mp, "unknown mount option [%s].", param->key);
13988757c38fSIan Kent 		return -EINVAL;
13998757c38fSIan Kent 	}
14008757c38fSIan Kent 
14018757c38fSIan Kent 	return 0;
14028757c38fSIan Kent }
14038757c38fSIan Kent 
14048757c38fSIan Kent static int
xfs_fs_validate_params(struct xfs_mount * mp)14051e5c39dfSDarrick J. Wong xfs_fs_validate_params(
14068757c38fSIan Kent 	struct xfs_mount	*mp)
14078757c38fSIan Kent {
14080560f31aSDave Chinner 	/* No recovery flag requires a read-only mount */
14092e973b2cSDave Chinner 	if (xfs_has_norecovery(mp) && !xfs_is_readonly(mp)) {
14108757c38fSIan Kent 		xfs_warn(mp, "no-recovery mounts must be read-only.");
14118757c38fSIan Kent 		return -EINVAL;
14128757c38fSIan Kent 	}
14138757c38fSIan Kent 
14140560f31aSDave Chinner 	/*
14150560f31aSDave Chinner 	 * We have not read the superblock at this point, so only the attr2
14160560f31aSDave Chinner 	 * mount option can set the attr2 feature by this stage.
14170560f31aSDave Chinner 	 */
14180560f31aSDave Chinner 	if (xfs_has_attr2(mp) && xfs_has_noattr2(mp)) {
1419e23b55d5SDave Chinner 		xfs_warn(mp, "attr2 and noattr2 cannot both be specified.");
1420e23b55d5SDave Chinner 		return -EINVAL;
1421e23b55d5SDave Chinner 	}
1422e23b55d5SDave Chinner 
1423e23b55d5SDave Chinner 
14240560f31aSDave Chinner 	if (xfs_has_noalign(mp) && (mp->m_dalign || mp->m_swidth)) {
14258757c38fSIan Kent 		xfs_warn(mp,
14268757c38fSIan Kent 	"sunit and swidth options incompatible with the noalign option");
14278757c38fSIan Kent 		return -EINVAL;
14288757c38fSIan Kent 	}
14298757c38fSIan Kent 
14308757c38fSIan Kent 	if (!IS_ENABLED(CONFIG_XFS_QUOTA) && mp->m_qflags != 0) {
14318757c38fSIan Kent 		xfs_warn(mp, "quota support not available in this kernel.");
14328757c38fSIan Kent 		return -EINVAL;
14338757c38fSIan Kent 	}
14348757c38fSIan Kent 
14358757c38fSIan Kent 	if ((mp->m_dalign && !mp->m_swidth) ||
14368757c38fSIan Kent 	    (!mp->m_dalign && mp->m_swidth)) {
14378757c38fSIan Kent 		xfs_warn(mp, "sunit and swidth must be specified together");
14388757c38fSIan Kent 		return -EINVAL;
14398757c38fSIan Kent 	}
14408757c38fSIan Kent 
14418757c38fSIan Kent 	if (mp->m_dalign && (mp->m_swidth % mp->m_dalign != 0)) {
14428757c38fSIan Kent 		xfs_warn(mp,
14438757c38fSIan Kent 	"stripe width (%d) must be a multiple of the stripe unit (%d)",
14448757c38fSIan Kent 			mp->m_swidth, mp->m_dalign);
14458757c38fSIan Kent 		return -EINVAL;
14468757c38fSIan Kent 	}
14478757c38fSIan Kent 
14488757c38fSIan Kent 	if (mp->m_logbufs != -1 &&
14498757c38fSIan Kent 	    mp->m_logbufs != 0 &&
14508757c38fSIan Kent 	    (mp->m_logbufs < XLOG_MIN_ICLOGS ||
14518757c38fSIan Kent 	     mp->m_logbufs > XLOG_MAX_ICLOGS)) {
14528757c38fSIan Kent 		xfs_warn(mp, "invalid logbufs value: %d [not %d-%d]",
14538757c38fSIan Kent 			mp->m_logbufs, XLOG_MIN_ICLOGS, XLOG_MAX_ICLOGS);
14548757c38fSIan Kent 		return -EINVAL;
14558757c38fSIan Kent 	}
14568757c38fSIan Kent 
14578757c38fSIan Kent 	if (mp->m_logbsize != -1 &&
14588757c38fSIan Kent 	    mp->m_logbsize !=  0 &&
14598757c38fSIan Kent 	    (mp->m_logbsize < XLOG_MIN_RECORD_BSIZE ||
14608757c38fSIan Kent 	     mp->m_logbsize > XLOG_MAX_RECORD_BSIZE ||
14618757c38fSIan Kent 	     !is_power_of_2(mp->m_logbsize))) {
14628757c38fSIan Kent 		xfs_warn(mp,
14638757c38fSIan Kent 			"invalid logbufsize: %d [not 16k,32k,64k,128k or 256k]",
14648757c38fSIan Kent 			mp->m_logbsize);
14658757c38fSIan Kent 		return -EINVAL;
14668757c38fSIan Kent 	}
14678757c38fSIan Kent 
14680560f31aSDave Chinner 	if (xfs_has_allocsize(mp) &&
14698757c38fSIan Kent 	    (mp->m_allocsize_log > XFS_MAX_IO_LOG ||
14708757c38fSIan Kent 	     mp->m_allocsize_log < XFS_MIN_IO_LOG)) {
14718757c38fSIan Kent 		xfs_warn(mp, "invalid log iosize: %d [not %d-%d]",
14728757c38fSIan Kent 			mp->m_allocsize_log, XFS_MIN_IO_LOG, XFS_MAX_IO_LOG);
14738757c38fSIan Kent 		return -EINVAL;
14748757c38fSIan Kent 	}
14758757c38fSIan Kent 
14768757c38fSIan Kent 	return 0;
14778757c38fSIan Kent }
14788757c38fSIan Kent 
1479a76dba3bSDarrick J. Wong struct dentry *
xfs_debugfs_mkdir(const char * name,struct dentry * parent)1480a76dba3bSDarrick J. Wong xfs_debugfs_mkdir(
1481a76dba3bSDarrick J. Wong 	const char	*name,
1482a76dba3bSDarrick J. Wong 	struct dentry	*parent)
1483a76dba3bSDarrick J. Wong {
1484a76dba3bSDarrick J. Wong 	struct dentry	*child;
1485a76dba3bSDarrick J. Wong 
1486a76dba3bSDarrick J. Wong 	/* Apparently we're expected to ignore error returns?? */
1487a76dba3bSDarrick J. Wong 	child = debugfs_create_dir(name, parent);
1488a76dba3bSDarrick J. Wong 	if (IS_ERR(child))
1489a76dba3bSDarrick J. Wong 		return NULL;
1490a76dba3bSDarrick J. Wong 
1491a76dba3bSDarrick J. Wong 	return child;
1492a76dba3bSDarrick J. Wong }
1493a76dba3bSDarrick J. Wong 
14948757c38fSIan Kent static int
xfs_fs_fill_super(struct super_block * sb,struct fs_context * fc)14951e5c39dfSDarrick J. Wong xfs_fs_fill_super(
1496c59d87c4SChristoph Hellwig 	struct super_block	*sb,
149773e5fff9SIan Kent 	struct fs_context	*fc)
1498c59d87c4SChristoph Hellwig {
149973e5fff9SIan Kent 	struct xfs_mount	*mp = sb->s_fs_info;
1500c59d87c4SChristoph Hellwig 	struct inode		*root;
15010279c71fSColin Ian King 	int			flags = 0, error;
1502c59d87c4SChristoph Hellwig 
15037c89fcb2SIan Kent 	mp->m_super = sb;
1504c59d87c4SChristoph Hellwig 
1505a0ebcdabSDave Chinner 	/*
1506a0ebcdabSDave Chinner 	 * Copy VFS mount flags from the context now that all parameter parsing
1507a0ebcdabSDave Chinner 	 * is guaranteed to have been completed by either the old mount API or
1508a0ebcdabSDave Chinner 	 * the newer fsopen/fsconfig API.
1509a0ebcdabSDave Chinner 	 */
1510a0ebcdabSDave Chinner 	if (fc->sb_flags & SB_RDONLY)
1511a0ebcdabSDave Chinner 		set_bit(XFS_OPSTATE_READONLY, &mp->m_opstate);
1512a0ebcdabSDave Chinner 	if (fc->sb_flags & SB_DIRSYNC)
1513a0ebcdabSDave Chinner 		mp->m_features |= XFS_FEAT_DIRSYNC;
1514a0ebcdabSDave Chinner 	if (fc->sb_flags & SB_SYNCHRONOUS)
1515a0ebcdabSDave Chinner 		mp->m_features |= XFS_FEAT_WSYNC;
1516a0ebcdabSDave Chinner 
15171e5c39dfSDarrick J. Wong 	error = xfs_fs_validate_params(mp);
1518c59d87c4SChristoph Hellwig 	if (error)
15192a9311adSChristoph Hellwig 		return error;
1520c59d87c4SChristoph Hellwig 
1521c59d87c4SChristoph Hellwig 	sb_min_blocksize(sb, BBSIZE);
1522c59d87c4SChristoph Hellwig 	sb->s_xattr = xfs_xattr_handlers;
1523c59d87c4SChristoph Hellwig 	sb->s_export_op = &xfs_export_operations;
1524c59d87c4SChristoph Hellwig #ifdef CONFIG_XFS_QUOTA
1525c59d87c4SChristoph Hellwig 	sb->s_qcop = &xfs_quotactl_operations;
152617ef4fddSJan Kara 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
1527c59d87c4SChristoph Hellwig #endif
1528c59d87c4SChristoph Hellwig 	sb->s_op = &xfs_super_operations;
1529c59d87c4SChristoph Hellwig 
1530dae5cd81SDave Chinner 	/*
1531dae5cd81SDave Chinner 	 * Delay mount work if the debug hook is set. This is debug
1532dae5cd81SDave Chinner 	 * instrumention to coordinate simulation of xfs mount failures with
1533dae5cd81SDave Chinner 	 * VFS superblock operations
1534dae5cd81SDave Chinner 	 */
1535dae5cd81SDave Chinner 	if (xfs_globals.mount_delay) {
1536dae5cd81SDave Chinner 		xfs_notice(mp, "Delaying mount for %d seconds.",
1537dae5cd81SDave Chinner 			xfs_globals.mount_delay);
1538dae5cd81SDave Chinner 		msleep(xfs_globals.mount_delay * 1000);
1539dae5cd81SDave Chinner 	}
1540dae5cd81SDave Chinner 
154173e5fff9SIan Kent 	if (fc->sb_flags & SB_SILENT)
1542c59d87c4SChristoph Hellwig 		flags |= XFS_MFSI_QUIET;
1543c59d87c4SChristoph Hellwig 
1544c59d87c4SChristoph Hellwig 	error = xfs_open_devices(mp);
1545c59d87c4SChristoph Hellwig 	if (error)
15462a9311adSChristoph Hellwig 		return error;
1547c59d87c4SChristoph Hellwig 
1548a76dba3bSDarrick J. Wong 	if (xfs_debugfs) {
1549a76dba3bSDarrick J. Wong 		mp->m_debugfs = xfs_debugfs_mkdir(mp->m_super->s_id,
1550a76dba3bSDarrick J. Wong 						  xfs_debugfs);
1551a76dba3bSDarrick J. Wong 	} else {
1552a76dba3bSDarrick J. Wong 		mp->m_debugfs = NULL;
1553a76dba3bSDarrick J. Wong 	}
1554a76dba3bSDarrick J. Wong 
15552451337dSDave Chinner 	error = xfs_init_mount_workqueues(mp);
1556c59d87c4SChristoph Hellwig 	if (error)
155735a93b14SChristoph Hellwig 		goto out_shutdown_devices;
1558c59d87c4SChristoph Hellwig 
15595681ca40SDave Chinner 	error = xfs_init_percpu_counters(mp);
1560aa6bf01dSChristoph Hellwig 	if (error)
1561aa6bf01dSChristoph Hellwig 		goto out_destroy_workqueues;
1562aa6bf01dSChristoph Hellwig 
1563ab23a776SDave Chinner 	error = xfs_inodegc_init_percpu(mp);
1564ab23a776SDave Chinner 	if (error)
1565ab23a776SDave Chinner 		goto out_destroy_counters;
1566ab23a776SDave Chinner 
1567225e4635SBill O'Donnell 	/* Allocate stats memory before we do operations that might use it */
1568225e4635SBill O'Donnell 	mp->m_stats.xs_stats = alloc_percpu(struct xfsstats);
1569225e4635SBill O'Donnell 	if (!mp->m_stats.xs_stats) {
1570f9d460b3SDan Carpenter 		error = -ENOMEM;
1571ab23a776SDave Chinner 		goto out_destroy_inodegc;
1572225e4635SBill O'Donnell 	}
1573225e4635SBill O'Donnell 
1574d7a74cadSDarrick J. Wong 	error = xchk_mount_stats_alloc(mp);
1575c59d87c4SChristoph Hellwig 	if (error)
1576225e4635SBill O'Donnell 		goto out_free_stats;
1577c59d87c4SChristoph Hellwig 
1578d7a74cadSDarrick J. Wong 	error = xfs_readsb(mp, flags);
1579d7a74cadSDarrick J. Wong 	if (error)
1580d7a74cadSDarrick J. Wong 		goto out_free_scrub_stats;
1581d7a74cadSDarrick J. Wong 
1582c59d87c4SChristoph Hellwig 	error = xfs_finish_flags(mp);
1583c59d87c4SChristoph Hellwig 	if (error)
1584c59d87c4SChristoph Hellwig 		goto out_free_sb;
1585c59d87c4SChristoph Hellwig 
1586c59d87c4SChristoph Hellwig 	error = xfs_setup_devices(mp);
1587c59d87c4SChristoph Hellwig 	if (error)
1588c59d87c4SChristoph Hellwig 		goto out_free_sb;
1589c59d87c4SChristoph Hellwig 
1590b96cb835SDarrick J. Wong 	/* V4 support is undergoing deprecation. */
159138c26bfdSDave Chinner 	if (!xfs_has_crc(mp)) {
1592b96cb835SDarrick J. Wong #ifdef CONFIG_XFS_SUPPORT_V4
1593b96cb835SDarrick J. Wong 		xfs_warn_once(mp,
1594b96cb835SDarrick J. Wong 	"Deprecated V4 format (crc=0) will not be supported after September 2030.");
1595b96cb835SDarrick J. Wong #else
1596b96cb835SDarrick J. Wong 		xfs_warn(mp,
1597b96cb835SDarrick J. Wong 	"Deprecated V4 format (crc=0) not supported by kernel.");
1598b96cb835SDarrick J. Wong 		error = -EINVAL;
1599b96cb835SDarrick J. Wong 		goto out_free_sb;
1600b96cb835SDarrick J. Wong #endif
1601b96cb835SDarrick J. Wong 	}
1602b96cb835SDarrick J. Wong 
16037ba83850SDarrick J. Wong 	/* ASCII case insensitivity is undergoing deprecation. */
16047ba83850SDarrick J. Wong 	if (xfs_has_asciici(mp)) {
16057ba83850SDarrick J. Wong #ifdef CONFIG_XFS_SUPPORT_ASCII_CI
16067ba83850SDarrick J. Wong 		xfs_warn_once(mp,
16077ba83850SDarrick J. Wong 	"Deprecated ASCII case-insensitivity feature (ascii-ci=1) will not be supported after September 2030.");
16087ba83850SDarrick J. Wong #else
16097ba83850SDarrick J. Wong 		xfs_warn(mp,
16107ba83850SDarrick J. Wong 	"Deprecated ASCII case-insensitivity feature (ascii-ci=1) not supported by kernel.");
16117ba83850SDarrick J. Wong 		error = -EINVAL;
16127ba83850SDarrick J. Wong 		goto out_free_sb;
16137ba83850SDarrick J. Wong #endif
16147ba83850SDarrick J. Wong 	}
16157ba83850SDarrick J. Wong 
161680c720b8SDarrick J. Wong 	/* Filesystem claims it needs repair, so refuse the mount. */
1617ebd9027dSDave Chinner 	if (xfs_has_needsrepair(mp)) {
161880c720b8SDarrick J. Wong 		xfs_warn(mp, "Filesystem needs repair.  Please run xfs_repair.");
161980c720b8SDarrick J. Wong 		error = -EFSCORRUPTED;
162080c720b8SDarrick J. Wong 		goto out_free_sb;
162180c720b8SDarrick J. Wong 	}
162280c720b8SDarrick J. Wong 
1623932befe3SDarrick J. Wong 	/*
16243945ae03SDarrick J. Wong 	 * Don't touch the filesystem if a user tool thinks it owns the primary
16253945ae03SDarrick J. Wong 	 * superblock.  mkfs doesn't clear the flag from secondary supers, so
16263945ae03SDarrick J. Wong 	 * we don't check them at all.
16273945ae03SDarrick J. Wong 	 */
16283945ae03SDarrick J. Wong 	if (mp->m_sb.sb_inprogress) {
16293945ae03SDarrick J. Wong 		xfs_warn(mp, "Offline file system operation in progress!");
16303945ae03SDarrick J. Wong 		error = -EFSCORRUPTED;
16313945ae03SDarrick J. Wong 		goto out_free_sb;
16323945ae03SDarrick J. Wong 	}
16333945ae03SDarrick J. Wong 
16343945ae03SDarrick J. Wong 	/*
16353945ae03SDarrick J. Wong 	 * Until this is fixed only page-sized or smaller data blocks work.
16363945ae03SDarrick J. Wong 	 */
16373945ae03SDarrick J. Wong 	if (mp->m_sb.sb_blocksize > PAGE_SIZE) {
16383945ae03SDarrick J. Wong 		xfs_warn(mp,
16393945ae03SDarrick J. Wong 		"File system with blocksize %d bytes. "
16403945ae03SDarrick J. Wong 		"Only pagesize (%ld) or less will currently work.",
16413945ae03SDarrick J. Wong 				mp->m_sb.sb_blocksize, PAGE_SIZE);
16423945ae03SDarrick J. Wong 		error = -ENOSYS;
16433945ae03SDarrick J. Wong 		goto out_free_sb;
16443945ae03SDarrick J. Wong 	}
16453945ae03SDarrick J. Wong 
16463945ae03SDarrick J. Wong 	/* Ensure this filesystem fits in the page cache limits */
16473945ae03SDarrick J. Wong 	if (xfs_sb_validate_fsb_count(&mp->m_sb, mp->m_sb.sb_dblocks) ||
16483945ae03SDarrick J. Wong 	    xfs_sb_validate_fsb_count(&mp->m_sb, mp->m_sb.sb_rblocks)) {
16493945ae03SDarrick J. Wong 		xfs_warn(mp,
16503945ae03SDarrick J. Wong 		"file system too large to be mounted on this system.");
16513945ae03SDarrick J. Wong 		error = -EFBIG;
16523945ae03SDarrick J. Wong 		goto out_free_sb;
16533945ae03SDarrick J. Wong 	}
16543945ae03SDarrick J. Wong 
16553945ae03SDarrick J. Wong 	/*
1656932befe3SDarrick J. Wong 	 * XFS block mappings use 54 bits to store the logical block offset.
1657932befe3SDarrick J. Wong 	 * This should suffice to handle the maximum file size that the VFS
1658932befe3SDarrick J. Wong 	 * supports (currently 2^63 bytes on 64-bit and ULONG_MAX << PAGE_SHIFT
1659932befe3SDarrick J. Wong 	 * bytes on 32-bit), but as XFS and VFS have gotten the s_maxbytes
1660932befe3SDarrick J. Wong 	 * calculation wrong on 32-bit kernels in the past, we'll add a WARN_ON
1661932befe3SDarrick J. Wong 	 * to check this assertion.
1662932befe3SDarrick J. Wong 	 *
1663932befe3SDarrick J. Wong 	 * Avoid integer overflow by comparing the maximum bmbt offset to the
1664932befe3SDarrick J. Wong 	 * maximum pagecache offset in units of fs blocks.
1665932befe3SDarrick J. Wong 	 */
166633005fd0SDarrick J. Wong 	if (!xfs_verify_fileoff(mp, XFS_B_TO_FSBT(mp, MAX_LFS_FILESIZE))) {
1667932befe3SDarrick J. Wong 		xfs_warn(mp,
1668932befe3SDarrick J. Wong "MAX_LFS_FILESIZE block offset (%llu) exceeds extent map maximum (%llu)!",
1669932befe3SDarrick J. Wong 			 XFS_B_TO_FSBT(mp, MAX_LFS_FILESIZE),
1670932befe3SDarrick J. Wong 			 XFS_MAX_FILEOFF);
1671932befe3SDarrick J. Wong 		error = -EINVAL;
1672932befe3SDarrick J. Wong 		goto out_free_sb;
1673932befe3SDarrick J. Wong 	}
1674932befe3SDarrick J. Wong 
1675c59d87c4SChristoph Hellwig 	error = xfs_filestream_mount(mp);
1676c59d87c4SChristoph Hellwig 	if (error)
1677c59d87c4SChristoph Hellwig 		goto out_free_sb;
1678c59d87c4SChristoph Hellwig 
1679c59d87c4SChristoph Hellwig 	/*
1680c59d87c4SChristoph Hellwig 	 * we must configure the block size in the superblock before we run the
1681c59d87c4SChristoph Hellwig 	 * full mount process as the mount process can lookup and cache inodes.
1682c59d87c4SChristoph Hellwig 	 */
1683dddde68bSAdam Borowski 	sb->s_magic = XFS_SUPER_MAGIC;
1684c59d87c4SChristoph Hellwig 	sb->s_blocksize = mp->m_sb.sb_blocksize;
1685c59d87c4SChristoph Hellwig 	sb->s_blocksize_bits = ffs(sb->s_blocksize) - 1;
1686932befe3SDarrick J. Wong 	sb->s_maxbytes = MAX_LFS_FILESIZE;
16878de52778SAl Viro 	sb->s_max_links = XFS_MAXLINK;
1688c59d87c4SChristoph Hellwig 	sb->s_time_gran = 1;
168938c26bfdSDave Chinner 	if (xfs_has_bigtime(mp)) {
1690f93e5436SDarrick J. Wong 		sb->s_time_min = xfs_bigtime_to_unix(XFS_BIGTIME_TIME_MIN);
1691f93e5436SDarrick J. Wong 		sb->s_time_max = xfs_bigtime_to_unix(XFS_BIGTIME_TIME_MAX);
1692f93e5436SDarrick J. Wong 	} else {
1693876fdc7cSDarrick J. Wong 		sb->s_time_min = XFS_LEGACY_TIME_MIN;
1694876fdc7cSDarrick J. Wong 		sb->s_time_max = XFS_LEGACY_TIME_MAX;
1695f93e5436SDarrick J. Wong 	}
169606dbf82bSDarrick J. Wong 	trace_xfs_inode_timestamp_range(mp, sb->s_time_min, sb->s_time_max);
1697adfb5fb4SChristoph Hellwig 	sb->s_iflags |= SB_I_CGROUPWB;
1698adfb5fb4SChristoph Hellwig 
1699c59d87c4SChristoph Hellwig 	set_posix_acl_flag(sb);
1700c59d87c4SChristoph Hellwig 
1701dc037ad7SDave Chinner 	/* version 5 superblocks support inode version counters. */
1702d6837c1aSDave Chinner 	if (xfs_has_crc(mp))
1703357fdad0SMatthew Garrett 		sb->s_flags |= SB_I_VERSION;
1704dc037ad7SDave Chinner 
17050560f31aSDave Chinner 	if (xfs_has_dax_always(mp)) {
1706679a9949SChristoph Hellwig 		error = xfs_setup_dax_always(mp);
1707679a9949SChristoph Hellwig 		if (error)
1708b6e03c10SDarrick J. Wong 			goto out_filestream_unmount;
1709b6e03c10SDarrick J. Wong 	}
1710cbe4dab1SDave Chinner 
171170200574SChristoph Hellwig 	if (xfs_has_discard(mp) && !bdev_max_discard_sectors(sb->s_bdev)) {
171270200574SChristoph Hellwig 		xfs_warn(mp,
171370200574SChristoph Hellwig 	"mounting with \"discard\" option, but the device does not support discard");
17140560f31aSDave Chinner 		mp->m_features &= ~XFS_FEAT_DISCARD;
17151e6fa688SKenjiro Nakayama 	}
17161e6fa688SKenjiro Nakayama 
171738c26bfdSDave Chinner 	if (xfs_has_reflink(mp)) {
171866ae56a5SChristoph Hellwig 		if (mp->m_sb.sb_rblocks) {
1719c14632ddSDarrick J. Wong 			xfs_alert(mp,
1720c14632ddSDarrick J. Wong 	"reflink not compatible with realtime device!");
1721c14632ddSDarrick J. Wong 			error = -EINVAL;
1722c14632ddSDarrick J. Wong 			goto out_filestream_unmount;
1723c14632ddSDarrick J. Wong 		}
1724c14632ddSDarrick J. Wong 
172566ae56a5SChristoph Hellwig 		if (xfs_globals.always_cow) {
172666ae56a5SChristoph Hellwig 			xfs_info(mp, "using DEBUG-only always_cow mode.");
172766ae56a5SChristoph Hellwig 			mp->m_always_cow = true;
172866ae56a5SChristoph Hellwig 		}
172966ae56a5SChristoph Hellwig 	}
173066ae56a5SChristoph Hellwig 
173138c26bfdSDave Chinner 	if (xfs_has_rmapbt(mp) && mp->m_sb.sb_rblocks) {
1732738f57c1SDarrick J. Wong 		xfs_alert(mp,
173376883f79SDarrick J. Wong 	"reverse mapping btree not compatible with realtime device!");
1734738f57c1SDarrick J. Wong 		error = -EINVAL;
1735738f57c1SDarrick J. Wong 		goto out_filestream_unmount;
1736738f57c1SDarrick J. Wong 	}
17371c0607acSDarrick J. Wong 
17388a00ebe4SDave Chinner 	error = xfs_mountfs(mp);
1739c59d87c4SChristoph Hellwig 	if (error)
17407e18530bSDave Chinner 		goto out_filestream_unmount;
1741c59d87c4SChristoph Hellwig 
1742c59d87c4SChristoph Hellwig 	root = igrab(VFS_I(mp->m_rootip));
1743c59d87c4SChristoph Hellwig 	if (!root) {
17442451337dSDave Chinner 		error = -ENOENT;
17458a00ebe4SDave Chinner 		goto out_unmount;
1746c59d87c4SChristoph Hellwig 	}
174748fde701SAl Viro 	sb->s_root = d_make_root(root);
1748c59d87c4SChristoph Hellwig 	if (!sb->s_root) {
17492451337dSDave Chinner 		error = -ENOMEM;
17508a00ebe4SDave Chinner 		goto out_unmount;
1751c59d87c4SChristoph Hellwig 	}
1752c59d87c4SChristoph Hellwig 
17537e18530bSDave Chinner 	return 0;
17547e18530bSDave Chinner 
17557e18530bSDave Chinner  out_filestream_unmount:
1756c59d87c4SChristoph Hellwig 	xfs_filestream_unmount(mp);
1757c59d87c4SChristoph Hellwig  out_free_sb:
1758c59d87c4SChristoph Hellwig 	xfs_freesb(mp);
1759d7a74cadSDarrick J. Wong  out_free_scrub_stats:
1760d7a74cadSDarrick J. Wong 	xchk_mount_stats_free(mp);
1761225e4635SBill O'Donnell  out_free_stats:
1762225e4635SBill O'Donnell 	free_percpu(mp->m_stats.xs_stats);
1763ab23a776SDave Chinner  out_destroy_inodegc:
1764ab23a776SDave Chinner 	xfs_inodegc_free_percpu(mp);
1765c59d87c4SChristoph Hellwig  out_destroy_counters:
17665681ca40SDave Chinner 	xfs_destroy_percpu_counters(mp);
1767aa6bf01dSChristoph Hellwig  out_destroy_workqueues:
1768aa6bf01dSChristoph Hellwig 	xfs_destroy_mount_workqueues(mp);
176935a93b14SChristoph Hellwig  out_shutdown_devices:
177035a93b14SChristoph Hellwig 	xfs_shutdown_devices(mp);
17712451337dSDave Chinner 	return error;
1772c59d87c4SChristoph Hellwig 
1773c59d87c4SChristoph Hellwig  out_unmount:
1774c59d87c4SChristoph Hellwig 	xfs_filestream_unmount(mp);
1775c59d87c4SChristoph Hellwig 	xfs_unmountfs(mp);
1776c59d87c4SChristoph Hellwig 	goto out_free_sb;
1777c59d87c4SChristoph Hellwig }
1778c59d87c4SChristoph Hellwig 
177973e5fff9SIan Kent static int
xfs_fs_get_tree(struct fs_context * fc)17801e5c39dfSDarrick J. Wong xfs_fs_get_tree(
178173e5fff9SIan Kent 	struct fs_context	*fc)
178273e5fff9SIan Kent {
17831e5c39dfSDarrick J. Wong 	return get_tree_bdev(fc, xfs_fs_fill_super);
178473e5fff9SIan Kent }
178573e5fff9SIan Kent 
178663cd1e9bSIan Kent static int
xfs_remount_rw(struct xfs_mount * mp)178763cd1e9bSIan Kent xfs_remount_rw(
178863cd1e9bSIan Kent 	struct xfs_mount	*mp)
178963cd1e9bSIan Kent {
179063cd1e9bSIan Kent 	struct xfs_sb		*sbp = &mp->m_sb;
179163cd1e9bSIan Kent 	int error;
179263cd1e9bSIan Kent 
17930560f31aSDave Chinner 	if (xfs_has_norecovery(mp)) {
179463cd1e9bSIan Kent 		xfs_warn(mp,
179563cd1e9bSIan Kent 			"ro->rw transition prohibited on norecovery mount");
179663cd1e9bSIan Kent 		return -EINVAL;
179763cd1e9bSIan Kent 	}
179863cd1e9bSIan Kent 
1799d6837c1aSDave Chinner 	if (xfs_sb_is_v5(sbp) &&
180063cd1e9bSIan Kent 	    xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
180163cd1e9bSIan Kent 		xfs_warn(mp,
180263cd1e9bSIan Kent 	"ro->rw transition prohibited on unknown (0x%x) ro-compat filesystem",
180363cd1e9bSIan Kent 			(sbp->sb_features_ro_compat &
180463cd1e9bSIan Kent 				XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
180563cd1e9bSIan Kent 		return -EINVAL;
180663cd1e9bSIan Kent 	}
180763cd1e9bSIan Kent 
18082e973b2cSDave Chinner 	clear_bit(XFS_OPSTATE_READONLY, &mp->m_opstate);
180963cd1e9bSIan Kent 
181063cd1e9bSIan Kent 	/*
181163cd1e9bSIan Kent 	 * If this is the first remount to writeable state we might have some
181263cd1e9bSIan Kent 	 * superblock changes to update.
181363cd1e9bSIan Kent 	 */
181463cd1e9bSIan Kent 	if (mp->m_update_sb) {
181563cd1e9bSIan Kent 		error = xfs_sync_sb(mp, false);
181663cd1e9bSIan Kent 		if (error) {
181763cd1e9bSIan Kent 			xfs_warn(mp, "failed to write sb changes");
181863cd1e9bSIan Kent 			return error;
181963cd1e9bSIan Kent 		}
182063cd1e9bSIan Kent 		mp->m_update_sb = false;
182163cd1e9bSIan Kent 	}
182263cd1e9bSIan Kent 
182363cd1e9bSIan Kent 	/*
182463cd1e9bSIan Kent 	 * Fill out the reserve pool if it is empty. Use the stashed value if
182563cd1e9bSIan Kent 	 * it is non-zero, otherwise go with the default.
182663cd1e9bSIan Kent 	 */
182763cd1e9bSIan Kent 	xfs_restore_resvblks(mp);
182863cd1e9bSIan Kent 	xfs_log_work_queue(mp);
1829c9a6526fSDarrick J. Wong 	xfs_blockgc_start(mp);
183063cd1e9bSIan Kent 
183163cd1e9bSIan Kent 	/* Create the per-AG metadata reservation pool .*/
183263cd1e9bSIan Kent 	error = xfs_fs_reserve_ag_blocks(mp);
183363cd1e9bSIan Kent 	if (error && error != -ENOSPC)
183463cd1e9bSIan Kent 		return error;
183563cd1e9bSIan Kent 
1836ab23a776SDave Chinner 	/* Re-enable the background inode inactivation worker. */
1837ab23a776SDave Chinner 	xfs_inodegc_start(mp);
1838ab23a776SDave Chinner 
183963cd1e9bSIan Kent 	return 0;
184063cd1e9bSIan Kent }
184163cd1e9bSIan Kent 
184263cd1e9bSIan Kent static int
xfs_remount_ro(struct xfs_mount * mp)184363cd1e9bSIan Kent xfs_remount_ro(
184463cd1e9bSIan Kent 	struct xfs_mount	*mp)
184563cd1e9bSIan Kent {
1846089558bcSDarrick J. Wong 	struct xfs_icwalk	icw = {
1847089558bcSDarrick J. Wong 		.icw_flags	= XFS_ICWALK_FLAG_SYNC,
1848089558bcSDarrick J. Wong 	};
184963cd1e9bSIan Kent 	int			error;
185063cd1e9bSIan Kent 
1851b97cca3bSDarrick J. Wong 	/* Flush all the dirty data to disk. */
1852b97cca3bSDarrick J. Wong 	error = sync_filesystem(mp->m_super);
1853b97cca3bSDarrick J. Wong 	if (error)
1854b97cca3bSDarrick J. Wong 		return error;
1855b97cca3bSDarrick J. Wong 
185663cd1e9bSIan Kent 	/*
185763cd1e9bSIan Kent 	 * Cancel background eofb scanning so it cannot race with the final
185863cd1e9bSIan Kent 	 * log force+buftarg wait and deadlock the remount.
185963cd1e9bSIan Kent 	 */
1860c9a6526fSDarrick J. Wong 	xfs_blockgc_stop(mp);
186163cd1e9bSIan Kent 
1862089558bcSDarrick J. Wong 	/*
1863089558bcSDarrick J. Wong 	 * Clear out all remaining COW staging extents and speculative post-EOF
1864089558bcSDarrick J. Wong 	 * preallocations so that we don't leave inodes requiring inactivation
1865089558bcSDarrick J. Wong 	 * cleanups during reclaim on a read-only mount.  We must process every
1866089558bcSDarrick J. Wong 	 * cached inode, so this requires a synchronous cache scan.
1867089558bcSDarrick J. Wong 	 */
1868089558bcSDarrick J. Wong 	error = xfs_blockgc_free_space(mp, &icw);
186963cd1e9bSIan Kent 	if (error) {
187063cd1e9bSIan Kent 		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
187163cd1e9bSIan Kent 		return error;
187263cd1e9bSIan Kent 	}
187363cd1e9bSIan Kent 
1874ab23a776SDave Chinner 	/*
1875ab23a776SDave Chinner 	 * Stop the inodegc background worker.  xfs_fs_reconfigure already
1876ab23a776SDave Chinner 	 * flushed all pending inodegc work when it sync'd the filesystem.
1877ab23a776SDave Chinner 	 * The VFS holds s_umount, so we know that inodes cannot enter
1878ab23a776SDave Chinner 	 * xfs_fs_destroy_inode during a remount operation.  In readonly mode
1879ab23a776SDave Chinner 	 * we send inodes straight to reclaim, so no inodes will be queued.
1880ab23a776SDave Chinner 	 */
1881ab23a776SDave Chinner 	xfs_inodegc_stop(mp);
1882ab23a776SDave Chinner 
188363cd1e9bSIan Kent 	/* Free the per-AG metadata reservation pool. */
188463cd1e9bSIan Kent 	error = xfs_fs_unreserve_ag_blocks(mp);
188563cd1e9bSIan Kent 	if (error) {
188663cd1e9bSIan Kent 		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
188763cd1e9bSIan Kent 		return error;
188863cd1e9bSIan Kent 	}
188963cd1e9bSIan Kent 
189063cd1e9bSIan Kent 	/*
189163cd1e9bSIan Kent 	 * Before we sync the metadata, we need to free up the reserve block
189263cd1e9bSIan Kent 	 * pool so that the used block count in the superblock on disk is
189363cd1e9bSIan Kent 	 * correct at the end of the remount. Stash the current* reserve pool
189463cd1e9bSIan Kent 	 * size so that if we get remounted rw, we can return it to the same
189563cd1e9bSIan Kent 	 * size.
189663cd1e9bSIan Kent 	 */
189763cd1e9bSIan Kent 	xfs_save_resvblks(mp);
189863cd1e9bSIan Kent 
1899ea2064daSBrian Foster 	xfs_log_clean(mp);
19002e973b2cSDave Chinner 	set_bit(XFS_OPSTATE_READONLY, &mp->m_opstate);
190163cd1e9bSIan Kent 
190263cd1e9bSIan Kent 	return 0;
190363cd1e9bSIan Kent }
190463cd1e9bSIan Kent 
190563cd1e9bSIan Kent /*
190663cd1e9bSIan Kent  * Logically we would return an error here to prevent users from believing
190763cd1e9bSIan Kent  * they might have changed mount options using remount which can't be changed.
190863cd1e9bSIan Kent  *
190963cd1e9bSIan Kent  * But unfortunately mount(8) adds all options from mtab and fstab to the mount
191063cd1e9bSIan Kent  * arguments in some cases so we can't blindly reject options, but have to
191163cd1e9bSIan Kent  * check for each specified option if it actually differs from the currently
191263cd1e9bSIan Kent  * set option and only reject it if that's the case.
191363cd1e9bSIan Kent  *
191463cd1e9bSIan Kent  * Until that is implemented we return success for every remount request, and
191563cd1e9bSIan Kent  * silently ignore all options that we can't actually change.
191663cd1e9bSIan Kent  */
191763cd1e9bSIan Kent static int
xfs_fs_reconfigure(struct fs_context * fc)19181e5c39dfSDarrick J. Wong xfs_fs_reconfigure(
191963cd1e9bSIan Kent 	struct fs_context *fc)
192063cd1e9bSIan Kent {
192163cd1e9bSIan Kent 	struct xfs_mount	*mp = XFS_M(fc->root->d_sb);
192263cd1e9bSIan Kent 	struct xfs_mount        *new_mp = fc->s_fs_info;
192363cd1e9bSIan Kent 	int			flags = fc->sb_flags;
192463cd1e9bSIan Kent 	int			error;
192563cd1e9bSIan Kent 
19264750a171SEric Sandeen 	/* version 5 superblocks always support version counters. */
1927d6837c1aSDave Chinner 	if (xfs_has_crc(mp))
19284750a171SEric Sandeen 		fc->sb_flags |= SB_I_VERSION;
19294750a171SEric Sandeen 
19301e5c39dfSDarrick J. Wong 	error = xfs_fs_validate_params(new_mp);
193163cd1e9bSIan Kent 	if (error)
193263cd1e9bSIan Kent 		return error;
193363cd1e9bSIan Kent 
193463cd1e9bSIan Kent 	/* inode32 -> inode64 */
19350560f31aSDave Chinner 	if (xfs_has_small_inums(mp) && !xfs_has_small_inums(new_mp)) {
19360560f31aSDave Chinner 		mp->m_features &= ~XFS_FEAT_SMALL_INUMS;
1937d6837c1aSDave Chinner 		mp->m_maxagi = xfs_set_inode_alloc(mp, mp->m_sb.sb_agcount);
193863cd1e9bSIan Kent 	}
193963cd1e9bSIan Kent 
194063cd1e9bSIan Kent 	/* inode64 -> inode32 */
19410560f31aSDave Chinner 	if (!xfs_has_small_inums(mp) && xfs_has_small_inums(new_mp)) {
19420560f31aSDave Chinner 		mp->m_features |= XFS_FEAT_SMALL_INUMS;
1943d6837c1aSDave Chinner 		mp->m_maxagi = xfs_set_inode_alloc(mp, mp->m_sb.sb_agcount);
194463cd1e9bSIan Kent 	}
194563cd1e9bSIan Kent 
194663cd1e9bSIan Kent 	/* ro -> rw */
19472e973b2cSDave Chinner 	if (xfs_is_readonly(mp) && !(flags & SB_RDONLY)) {
194863cd1e9bSIan Kent 		error = xfs_remount_rw(mp);
194963cd1e9bSIan Kent 		if (error)
195063cd1e9bSIan Kent 			return error;
195163cd1e9bSIan Kent 	}
195263cd1e9bSIan Kent 
195363cd1e9bSIan Kent 	/* rw -> ro */
19542e973b2cSDave Chinner 	if (!xfs_is_readonly(mp) && (flags & SB_RDONLY)) {
195563cd1e9bSIan Kent 		error = xfs_remount_ro(mp);
195663cd1e9bSIan Kent 		if (error)
195763cd1e9bSIan Kent 			return error;
195863cd1e9bSIan Kent 	}
195963cd1e9bSIan Kent 
196063cd1e9bSIan Kent 	return 0;
196163cd1e9bSIan Kent }
196263cd1e9bSIan Kent 
1963dbbff489SChristoph Hellwig static void
xfs_fs_free(struct fs_context * fc)1964dbbff489SChristoph Hellwig xfs_fs_free(
196573e5fff9SIan Kent 	struct fs_context	*fc)
196673e5fff9SIan Kent {
196773e5fff9SIan Kent 	struct xfs_mount	*mp = fc->s_fs_info;
196873e5fff9SIan Kent 
196973e5fff9SIan Kent 	/*
197073e5fff9SIan Kent 	 * mp is stored in the fs_context when it is initialized.
197173e5fff9SIan Kent 	 * mp is transferred to the superblock on a successful mount,
197273e5fff9SIan Kent 	 * but if an error occurs before the transfer we have to free
197373e5fff9SIan Kent 	 * it here.
197473e5fff9SIan Kent 	 */
197573e5fff9SIan Kent 	if (mp)
197673e5fff9SIan Kent 		xfs_mount_free(mp);
197773e5fff9SIan Kent }
197873e5fff9SIan Kent 
197973e5fff9SIan Kent static const struct fs_context_operations xfs_context_ops = {
19801e5c39dfSDarrick J. Wong 	.parse_param = xfs_fs_parse_param,
19811e5c39dfSDarrick J. Wong 	.get_tree    = xfs_fs_get_tree,
19821e5c39dfSDarrick J. Wong 	.reconfigure = xfs_fs_reconfigure,
19831e5c39dfSDarrick J. Wong 	.free        = xfs_fs_free,
198473e5fff9SIan Kent };
198573e5fff9SIan Kent 
1986a0ebcdabSDave Chinner /*
1987a0ebcdabSDave Chinner  * WARNING: do not initialise any parameters in this function that depend on
1988a0ebcdabSDave Chinner  * mount option parsing having already been performed as this can be called from
1989a0ebcdabSDave Chinner  * fsopen() before any parameters have been set.
1990a0ebcdabSDave Chinner  */
xfs_init_fs_context(struct fs_context * fc)199173e5fff9SIan Kent static int xfs_init_fs_context(
199273e5fff9SIan Kent 	struct fs_context	*fc)
199373e5fff9SIan Kent {
199473e5fff9SIan Kent 	struct xfs_mount	*mp;
199573e5fff9SIan Kent 
199650f83009SIan Kent 	mp = kmem_alloc(sizeof(struct xfs_mount), KM_ZERO);
199773e5fff9SIan Kent 	if (!mp)
199873e5fff9SIan Kent 		return -ENOMEM;
199973e5fff9SIan Kent 
200050f83009SIan Kent 	spin_lock_init(&mp->m_sb_lock);
200150f83009SIan Kent 	INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
200250f83009SIan Kent 	spin_lock_init(&mp->m_perag_lock);
200350f83009SIan Kent 	mutex_init(&mp->m_growlock);
2004f0f7a674SDarrick J. Wong 	INIT_WORK(&mp->m_flush_inodes_work, xfs_flush_inodes_worker);
200550f83009SIan Kent 	INIT_DELAYED_WORK(&mp->m_reclaim_work, xfs_reclaim_worker);
200650f83009SIan Kent 	mp->m_kobj.kobject.kset = xfs_kset;
200750f83009SIan Kent 	/*
200850f83009SIan Kent 	 * We don't create the finobt per-ag space reservation until after log
200950f83009SIan Kent 	 * recovery, so we must set this to true so that an ifree transaction
201050f83009SIan Kent 	 * started during log recovery will not depend on space reservations
201150f83009SIan Kent 	 * for finobt expansion.
201250f83009SIan Kent 	 */
201350f83009SIan Kent 	mp->m_finobt_nores = true;
201450f83009SIan Kent 
201573e5fff9SIan Kent 	/*
201673e5fff9SIan Kent 	 * These can be overridden by the mount option parsing.
201773e5fff9SIan Kent 	 */
201873e5fff9SIan Kent 	mp->m_logbufs = -1;
201973e5fff9SIan Kent 	mp->m_logbsize = -1;
202073e5fff9SIan Kent 	mp->m_allocsize_log = 16; /* 64k */
202173e5fff9SIan Kent 
202273e5fff9SIan Kent 	fc->s_fs_info = mp;
202373e5fff9SIan Kent 	fc->ops = &xfs_context_ops;
202473e5fff9SIan Kent 
202573e5fff9SIan Kent 	return 0;
202673e5fff9SIan Kent }
202773e5fff9SIan Kent 
20282a9311adSChristoph Hellwig static void
xfs_kill_sb(struct super_block * sb)20292a9311adSChristoph Hellwig xfs_kill_sb(
20302a9311adSChristoph Hellwig 	struct super_block		*sb)
20312a9311adSChristoph Hellwig {
20322a9311adSChristoph Hellwig 	kill_block_super(sb);
20332a9311adSChristoph Hellwig 	xfs_mount_free(XFS_M(sb));
20342a9311adSChristoph Hellwig }
20352a9311adSChristoph Hellwig 
2036c59d87c4SChristoph Hellwig static struct file_system_type xfs_fs_type = {
2037c59d87c4SChristoph Hellwig 	.owner			= THIS_MODULE,
2038c59d87c4SChristoph Hellwig 	.name			= "xfs",
203973e5fff9SIan Kent 	.init_fs_context	= xfs_init_fs_context,
2040d7167b14SAl Viro 	.parameters		= xfs_fs_parameters,
20412a9311adSChristoph Hellwig 	.kill_sb		= xfs_kill_sb,
2042f798accdSChristian Brauner 	.fs_flags		= FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
2043c59d87c4SChristoph Hellwig };
20447f78e035SEric W. Biederman MODULE_ALIAS_FS("xfs");
2045c59d87c4SChristoph Hellwig 
2046c59d87c4SChristoph Hellwig STATIC int __init
xfs_init_caches(void)2047182696fbSDarrick J. Wong xfs_init_caches(void)
2048c59d87c4SChristoph Hellwig {
20499fa47bdcSDarrick J. Wong 	int		error;
20509fa47bdcSDarrick J. Wong 
2051231f91abSDave Chinner 	xfs_buf_cache = kmem_cache_create("xfs_buf", sizeof(struct xfs_buf), 0,
2052231f91abSDave Chinner 					 SLAB_HWCACHE_ALIGN |
2053231f91abSDave Chinner 					 SLAB_RECLAIM_ACCOUNT |
2054231f91abSDave Chinner 					 SLAB_MEM_SPREAD,
2055231f91abSDave Chinner 					 NULL);
2056231f91abSDave Chinner 	if (!xfs_buf_cache)
2057231f91abSDave Chinner 		goto out;
2058231f91abSDave Chinner 
2059182696fbSDarrick J. Wong 	xfs_log_ticket_cache = kmem_cache_create("xfs_log_ticket",
2060b1231760SCarlos Maiolino 						sizeof(struct xlog_ticket),
2061b1231760SCarlos Maiolino 						0, 0, NULL);
2062182696fbSDarrick J. Wong 	if (!xfs_log_ticket_cache)
2063231f91abSDave Chinner 		goto out_destroy_buf_cache;
2064c59d87c4SChristoph Hellwig 
20659fa47bdcSDarrick J. Wong 	error = xfs_btree_init_cur_caches();
20669fa47bdcSDarrick J. Wong 	if (error)
2067c201d9caSDarrick J. Wong 		goto out_destroy_log_ticket_cache;
2068c59d87c4SChristoph Hellwig 
2069f3c799c2SDarrick J. Wong 	error = xfs_defer_init_item_caches();
2070f3c799c2SDarrick J. Wong 	if (error)
2071f3c799c2SDarrick J. Wong 		goto out_destroy_btree_cur_cache;
2072f3c799c2SDarrick J. Wong 
2073182696fbSDarrick J. Wong 	xfs_da_state_cache = kmem_cache_create("xfs_da_state",
2074b1231760SCarlos Maiolino 					      sizeof(struct xfs_da_state),
2075b1231760SCarlos Maiolino 					      0, 0, NULL);
2076182696fbSDarrick J. Wong 	if (!xfs_da_state_cache)
2077f3c799c2SDarrick J. Wong 		goto out_destroy_defer_item_cache;
2078c59d87c4SChristoph Hellwig 
2079182696fbSDarrick J. Wong 	xfs_ifork_cache = kmem_cache_create("xfs_ifork",
2080b1231760SCarlos Maiolino 					   sizeof(struct xfs_ifork),
2081b1231760SCarlos Maiolino 					   0, 0, NULL);
2082182696fbSDarrick J. Wong 	if (!xfs_ifork_cache)
2083182696fbSDarrick J. Wong 		goto out_destroy_da_state_cache;
2084c59d87c4SChristoph Hellwig 
2085182696fbSDarrick J. Wong 	xfs_trans_cache = kmem_cache_create("xfs_trans",
2086b1231760SCarlos Maiolino 					   sizeof(struct xfs_trans),
2087b1231760SCarlos Maiolino 					   0, 0, NULL);
2088182696fbSDarrick J. Wong 	if (!xfs_trans_cache)
2089182696fbSDarrick J. Wong 		goto out_destroy_ifork_cache;
2090c59d87c4SChristoph Hellwig 
2091c59d87c4SChristoph Hellwig 
2092c59d87c4SChristoph Hellwig 	/*
2093182696fbSDarrick J. Wong 	 * The size of the cache-allocated buf log item is the maximum
2094c59d87c4SChristoph Hellwig 	 * size possible under XFS.  This wastes a little bit of memory,
2095c59d87c4SChristoph Hellwig 	 * but it is much faster.
2096c59d87c4SChristoph Hellwig 	 */
2097182696fbSDarrick J. Wong 	xfs_buf_item_cache = kmem_cache_create("xfs_buf_item",
2098b1231760SCarlos Maiolino 					      sizeof(struct xfs_buf_log_item),
2099b1231760SCarlos Maiolino 					      0, 0, NULL);
2100182696fbSDarrick J. Wong 	if (!xfs_buf_item_cache)
2101182696fbSDarrick J. Wong 		goto out_destroy_trans_cache;
2102c59d87c4SChristoph Hellwig 
2103182696fbSDarrick J. Wong 	xfs_efd_cache = kmem_cache_create("xfs_efd_item",
21043c5aaaceSDarrick J. Wong 			xfs_efd_log_item_sizeof(XFS_EFD_MAX_FAST_EXTENTS),
2105b1231760SCarlos Maiolino 			0, 0, NULL);
2106182696fbSDarrick J. Wong 	if (!xfs_efd_cache)
2107182696fbSDarrick J. Wong 		goto out_destroy_buf_item_cache;
2108c59d87c4SChristoph Hellwig 
2109182696fbSDarrick J. Wong 	xfs_efi_cache = kmem_cache_create("xfs_efi_item",
21103c5aaaceSDarrick J. Wong 			xfs_efi_log_item_sizeof(XFS_EFI_MAX_FAST_EXTENTS),
2111b1231760SCarlos Maiolino 			0, 0, NULL);
2112182696fbSDarrick J. Wong 	if (!xfs_efi_cache)
2113182696fbSDarrick J. Wong 		goto out_destroy_efd_cache;
2114c59d87c4SChristoph Hellwig 
2115182696fbSDarrick J. Wong 	xfs_inode_cache = kmem_cache_create("xfs_inode",
2116b1231760SCarlos Maiolino 					   sizeof(struct xfs_inode), 0,
2117b1231760SCarlos Maiolino 					   (SLAB_HWCACHE_ALIGN |
2118b1231760SCarlos Maiolino 					    SLAB_RECLAIM_ACCOUNT |
2119b1231760SCarlos Maiolino 					    SLAB_MEM_SPREAD | SLAB_ACCOUNT),
2120b1231760SCarlos Maiolino 					   xfs_fs_inode_init_once);
2121182696fbSDarrick J. Wong 	if (!xfs_inode_cache)
2122182696fbSDarrick J. Wong 		goto out_destroy_efi_cache;
2123c59d87c4SChristoph Hellwig 
2124182696fbSDarrick J. Wong 	xfs_ili_cache = kmem_cache_create("xfs_ili",
2125b1231760SCarlos Maiolino 					 sizeof(struct xfs_inode_log_item), 0,
2126d59eadaeSDave Chinner 					 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
2127d59eadaeSDave Chinner 					 NULL);
2128182696fbSDarrick J. Wong 	if (!xfs_ili_cache)
2129182696fbSDarrick J. Wong 		goto out_destroy_inode_cache;
2130b1231760SCarlos Maiolino 
2131182696fbSDarrick J. Wong 	xfs_icreate_cache = kmem_cache_create("xfs_icr",
2132b1231760SCarlos Maiolino 					     sizeof(struct xfs_icreate_item),
2133b1231760SCarlos Maiolino 					     0, 0, NULL);
2134182696fbSDarrick J. Wong 	if (!xfs_icreate_cache)
2135182696fbSDarrick J. Wong 		goto out_destroy_ili_cache;
2136c59d87c4SChristoph Hellwig 
2137182696fbSDarrick J. Wong 	xfs_rud_cache = kmem_cache_create("xfs_rud_item",
2138b1231760SCarlos Maiolino 					 sizeof(struct xfs_rud_log_item),
2139b1231760SCarlos Maiolino 					 0, 0, NULL);
2140182696fbSDarrick J. Wong 	if (!xfs_rud_cache)
2141182696fbSDarrick J. Wong 		goto out_destroy_icreate_cache;
21425880f2d7SDarrick J. Wong 
2143182696fbSDarrick J. Wong 	xfs_rui_cache = kmem_cache_create("xfs_rui_item",
2144cd00158cSDarrick J. Wong 			xfs_rui_log_item_sizeof(XFS_RUI_MAX_FAST_EXTENTS),
2145b1231760SCarlos Maiolino 			0, 0, NULL);
2146182696fbSDarrick J. Wong 	if (!xfs_rui_cache)
2147182696fbSDarrick J. Wong 		goto out_destroy_rud_cache;
21485880f2d7SDarrick J. Wong 
2149182696fbSDarrick J. Wong 	xfs_cud_cache = kmem_cache_create("xfs_cud_item",
2150b1231760SCarlos Maiolino 					 sizeof(struct xfs_cud_log_item),
2151b1231760SCarlos Maiolino 					 0, 0, NULL);
2152182696fbSDarrick J. Wong 	if (!xfs_cud_cache)
2153182696fbSDarrick J. Wong 		goto out_destroy_rui_cache;
2154baf4bcacSDarrick J. Wong 
2155182696fbSDarrick J. Wong 	xfs_cui_cache = kmem_cache_create("xfs_cui_item",
2156baf4bcacSDarrick J. Wong 			xfs_cui_log_item_sizeof(XFS_CUI_MAX_FAST_EXTENTS),
2157b1231760SCarlos Maiolino 			0, 0, NULL);
2158182696fbSDarrick J. Wong 	if (!xfs_cui_cache)
2159182696fbSDarrick J. Wong 		goto out_destroy_cud_cache;
2160baf4bcacSDarrick J. Wong 
2161182696fbSDarrick J. Wong 	xfs_bud_cache = kmem_cache_create("xfs_bud_item",
2162b1231760SCarlos Maiolino 					 sizeof(struct xfs_bud_log_item),
2163b1231760SCarlos Maiolino 					 0, 0, NULL);
2164182696fbSDarrick J. Wong 	if (!xfs_bud_cache)
2165182696fbSDarrick J. Wong 		goto out_destroy_cui_cache;
21666413a014SDarrick J. Wong 
2167182696fbSDarrick J. Wong 	xfs_bui_cache = kmem_cache_create("xfs_bui_item",
21686413a014SDarrick J. Wong 			xfs_bui_log_item_sizeof(XFS_BUI_MAX_FAST_EXTENTS),
2169b1231760SCarlos Maiolino 			0, 0, NULL);
2170182696fbSDarrick J. Wong 	if (!xfs_bui_cache)
2171182696fbSDarrick J. Wong 		goto out_destroy_bud_cache;
21726413a014SDarrick J. Wong 
21734136e38aSDarrick J. Wong 	xfs_attrd_cache = kmem_cache_create("xfs_attrd_item",
21744136e38aSDarrick J. Wong 					    sizeof(struct xfs_attrd_log_item),
21754136e38aSDarrick J. Wong 					    0, 0, NULL);
21764136e38aSDarrick J. Wong 	if (!xfs_attrd_cache)
21774136e38aSDarrick J. Wong 		goto out_destroy_bui_cache;
21784136e38aSDarrick J. Wong 
21794136e38aSDarrick J. Wong 	xfs_attri_cache = kmem_cache_create("xfs_attri_item",
21804136e38aSDarrick J. Wong 					    sizeof(struct xfs_attri_log_item),
21814136e38aSDarrick J. Wong 					    0, 0, NULL);
21824136e38aSDarrick J. Wong 	if (!xfs_attri_cache)
21834136e38aSDarrick J. Wong 		goto out_destroy_attrd_cache;
21844136e38aSDarrick J. Wong 
2185784eb7d8SDave Chinner 	xfs_iunlink_cache = kmem_cache_create("xfs_iul_item",
2186784eb7d8SDave Chinner 					     sizeof(struct xfs_iunlink_item),
2187784eb7d8SDave Chinner 					     0, 0, NULL);
2188784eb7d8SDave Chinner 	if (!xfs_iunlink_cache)
2189784eb7d8SDave Chinner 		goto out_destroy_attri_cache;
2190784eb7d8SDave Chinner 
2191c59d87c4SChristoph Hellwig 	return 0;
2192c59d87c4SChristoph Hellwig 
2193784eb7d8SDave Chinner  out_destroy_attri_cache:
2194784eb7d8SDave Chinner 	kmem_cache_destroy(xfs_attri_cache);
21954136e38aSDarrick J. Wong  out_destroy_attrd_cache:
21964136e38aSDarrick J. Wong 	kmem_cache_destroy(xfs_attrd_cache);
21974136e38aSDarrick J. Wong  out_destroy_bui_cache:
21984136e38aSDarrick J. Wong 	kmem_cache_destroy(xfs_bui_cache);
2199182696fbSDarrick J. Wong  out_destroy_bud_cache:
2200182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_bud_cache);
2201182696fbSDarrick J. Wong  out_destroy_cui_cache:
2202182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_cui_cache);
2203182696fbSDarrick J. Wong  out_destroy_cud_cache:
2204182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_cud_cache);
2205182696fbSDarrick J. Wong  out_destroy_rui_cache:
2206182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_rui_cache);
2207182696fbSDarrick J. Wong  out_destroy_rud_cache:
2208182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_rud_cache);
2209182696fbSDarrick J. Wong  out_destroy_icreate_cache:
2210182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_icreate_cache);
2211182696fbSDarrick J. Wong  out_destroy_ili_cache:
2212182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_ili_cache);
2213182696fbSDarrick J. Wong  out_destroy_inode_cache:
2214182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_inode_cache);
2215182696fbSDarrick J. Wong  out_destroy_efi_cache:
2216182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_efi_cache);
2217182696fbSDarrick J. Wong  out_destroy_efd_cache:
2218182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_efd_cache);
2219182696fbSDarrick J. Wong  out_destroy_buf_item_cache:
2220182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_buf_item_cache);
2221182696fbSDarrick J. Wong  out_destroy_trans_cache:
2222182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_trans_cache);
2223182696fbSDarrick J. Wong  out_destroy_ifork_cache:
2224182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_ifork_cache);
2225182696fbSDarrick J. Wong  out_destroy_da_state_cache:
2226182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_da_state_cache);
2227f3c799c2SDarrick J. Wong  out_destroy_defer_item_cache:
2228f3c799c2SDarrick J. Wong 	xfs_defer_destroy_item_caches();
2229182696fbSDarrick J. Wong  out_destroy_btree_cur_cache:
22309fa47bdcSDarrick J. Wong 	xfs_btree_destroy_cur_caches();
2231182696fbSDarrick J. Wong  out_destroy_log_ticket_cache:
2232182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_log_ticket_cache);
2233231f91abSDave Chinner  out_destroy_buf_cache:
2234231f91abSDave Chinner 	kmem_cache_destroy(xfs_buf_cache);
2235c59d87c4SChristoph Hellwig  out:
2236c59d87c4SChristoph Hellwig 	return -ENOMEM;
2237c59d87c4SChristoph Hellwig }
2238c59d87c4SChristoph Hellwig 
2239c59d87c4SChristoph Hellwig STATIC void
xfs_destroy_caches(void)2240182696fbSDarrick J. Wong xfs_destroy_caches(void)
2241c59d87c4SChristoph Hellwig {
22428c0a8537SKirill A. Shutemov 	/*
22438c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free are flushed before we
22448c0a8537SKirill A. Shutemov 	 * destroy caches.
22458c0a8537SKirill A. Shutemov 	 */
22468c0a8537SKirill A. Shutemov 	rcu_barrier();
2247784eb7d8SDave Chinner 	kmem_cache_destroy(xfs_iunlink_cache);
22484136e38aSDarrick J. Wong 	kmem_cache_destroy(xfs_attri_cache);
22494136e38aSDarrick J. Wong 	kmem_cache_destroy(xfs_attrd_cache);
2250182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_bui_cache);
2251182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_bud_cache);
2252182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_cui_cache);
2253182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_cud_cache);
2254182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_rui_cache);
2255182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_rud_cache);
2256182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_icreate_cache);
2257182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_ili_cache);
2258182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_inode_cache);
2259182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_efi_cache);
2260182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_efd_cache);
2261182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_buf_item_cache);
2262182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_trans_cache);
2263182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_ifork_cache);
2264182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_da_state_cache);
2265f3c799c2SDarrick J. Wong 	xfs_defer_destroy_item_caches();
22669fa47bdcSDarrick J. Wong 	xfs_btree_destroy_cur_caches();
2267182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_log_ticket_cache);
2268231f91abSDave Chinner 	kmem_cache_destroy(xfs_buf_cache);
2269c59d87c4SChristoph Hellwig }
2270c59d87c4SChristoph Hellwig 
2271c59d87c4SChristoph Hellwig STATIC int __init
xfs_init_workqueues(void)2272c59d87c4SChristoph Hellwig xfs_init_workqueues(void)
2273c59d87c4SChristoph Hellwig {
2274c59d87c4SChristoph Hellwig 	/*
2275c999a223SDave Chinner 	 * The allocation workqueue can be used in memory reclaim situations
2276c999a223SDave Chinner 	 * (writepage path), and parallelism is only limited by the number of
2277c999a223SDave Chinner 	 * AGs in all the filesystems mounted. Hence use the default large
2278c999a223SDave Chinner 	 * max_active value for this workqueue.
2279c999a223SDave Chinner 	 */
22808018ec08SBrian Foster 	xfs_alloc_wq = alloc_workqueue("xfsalloc",
228105a302a1SDarrick J. Wong 			XFS_WQFLAGS(WQ_MEM_RECLAIM | WQ_FREEZABLE), 0);
2282c999a223SDave Chinner 	if (!xfs_alloc_wq)
22835889608dSDave Chinner 		return -ENOMEM;
2284c999a223SDave Chinner 
228505a302a1SDarrick J. Wong 	xfs_discard_wq = alloc_workqueue("xfsdiscard", XFS_WQFLAGS(WQ_UNBOUND),
228605a302a1SDarrick J. Wong 			0);
22874560e78fSChristoph Hellwig 	if (!xfs_discard_wq)
22884560e78fSChristoph Hellwig 		goto out_free_alloc_wq;
22894560e78fSChristoph Hellwig 
22900030807cSChristoph Hellwig 	return 0;
22914560e78fSChristoph Hellwig out_free_alloc_wq:
22924560e78fSChristoph Hellwig 	destroy_workqueue(xfs_alloc_wq);
22934560e78fSChristoph Hellwig 	return -ENOMEM;
2294c59d87c4SChristoph Hellwig }
2295c59d87c4SChristoph Hellwig 
2296c59d87c4SChristoph Hellwig STATIC void
xfs_destroy_workqueues(void)2297c59d87c4SChristoph Hellwig xfs_destroy_workqueues(void)
2298c59d87c4SChristoph Hellwig {
22994560e78fSChristoph Hellwig 	destroy_workqueue(xfs_discard_wq);
2300c999a223SDave Chinner 	destroy_workqueue(xfs_alloc_wq);
2301c59d87c4SChristoph Hellwig }
2302c59d87c4SChristoph Hellwig 
2303c59d87c4SChristoph Hellwig STATIC int __init
init_xfs_fs(void)2304c59d87c4SChristoph Hellwig init_xfs_fs(void)
2305c59d87c4SChristoph Hellwig {
2306c59d87c4SChristoph Hellwig 	int			error;
2307c59d87c4SChristoph Hellwig 
230830cbc591SDarrick J. Wong 	xfs_check_ondisk_structs();
230930cbc591SDarrick J. Wong 
23103cfb9290SDarrick J. Wong 	error = xfs_dahash_test();
23113cfb9290SDarrick J. Wong 	if (error)
23123cfb9290SDarrick J. Wong 		return error;
23133cfb9290SDarrick J. Wong 
2314c59d87c4SChristoph Hellwig 	printk(KERN_INFO XFS_VERSION_STRING " with "
2315c59d87c4SChristoph Hellwig 			 XFS_BUILD_OPTIONS " enabled\n");
2316c59d87c4SChristoph Hellwig 
2317c59d87c4SChristoph Hellwig 	xfs_dir_startup();
2318c59d87c4SChristoph Hellwig 
2319182696fbSDarrick J. Wong 	error = xfs_init_caches();
2320f1653c2eSDave Chinner 	if (error)
2321ef7d9593SDarrick J. Wong 		goto out;
2322f1653c2eSDave Chinner 
2323c59d87c4SChristoph Hellwig 	error = xfs_init_workqueues();
2324c59d87c4SChristoph Hellwig 	if (error)
2325182696fbSDarrick J. Wong 		goto out_destroy_caches;
2326c59d87c4SChristoph Hellwig 
2327c59d87c4SChristoph Hellwig 	error = xfs_mru_cache_init();
2328c59d87c4SChristoph Hellwig 	if (error)
2329c59d87c4SChristoph Hellwig 		goto out_destroy_wq;
2330c59d87c4SChristoph Hellwig 
2331c59d87c4SChristoph Hellwig 	error = xfs_init_procfs();
2332c59d87c4SChristoph Hellwig 	if (error)
2333231f91abSDave Chinner 		goto out_mru_cache_uninit;
2334c59d87c4SChristoph Hellwig 
2335c59d87c4SChristoph Hellwig 	error = xfs_sysctl_register();
2336c59d87c4SChristoph Hellwig 	if (error)
2337c59d87c4SChristoph Hellwig 		goto out_cleanup_procfs;
2338c59d87c4SChristoph Hellwig 
2339a76dba3bSDarrick J. Wong 	xfs_debugfs = xfs_debugfs_mkdir("xfs", NULL);
2340a76dba3bSDarrick J. Wong 
23413d871226SBrian Foster 	xfs_kset = kset_create_and_add("xfs", NULL, fs_kobj);
23423d871226SBrian Foster 	if (!xfs_kset) {
23433d871226SBrian Foster 		error = -ENOMEM;
2344a76dba3bSDarrick J. Wong 		goto out_debugfs_unregister;
23453d871226SBrian Foster 	}
23463d871226SBrian Foster 
234780529c45SBill O'Donnell 	xfsstats.xs_kobj.kobject.kset = xfs_kset;
234880529c45SBill O'Donnell 
234980529c45SBill O'Donnell 	xfsstats.xs_stats = alloc_percpu(struct xfsstats);
235080529c45SBill O'Donnell 	if (!xfsstats.xs_stats) {
235180529c45SBill O'Donnell 		error = -ENOMEM;
235280529c45SBill O'Donnell 		goto out_kset_unregister;
235380529c45SBill O'Donnell 	}
235480529c45SBill O'Donnell 
235580529c45SBill O'Donnell 	error = xfs_sysfs_init(&xfsstats.xs_kobj, &xfs_stats_ktype, NULL,
2356bb230c12SBill O'Donnell 			       "stats");
2357bb230c12SBill O'Donnell 	if (error)
235880529c45SBill O'Donnell 		goto out_free_stats;
2359bb230c12SBill O'Donnell 
2360d7a74cadSDarrick J. Wong 	error = xchk_global_stats_setup(xfs_debugfs);
2361d7a74cadSDarrick J. Wong 	if (error)
2362d7a74cadSDarrick J. Wong 		goto out_remove_stats_kobj;
2363d7a74cadSDarrick J. Wong 
236465b65735SBrian Foster #ifdef DEBUG
236565b65735SBrian Foster 	xfs_dbg_kobj.kobject.kset = xfs_kset;
236665b65735SBrian Foster 	error = xfs_sysfs_init(&xfs_dbg_kobj, &xfs_dbg_ktype, NULL, "debug");
2367a05931ceSChristoph Hellwig 	if (error)
2368d7a74cadSDarrick J. Wong 		goto out_remove_scrub_stats;
236965b65735SBrian Foster #endif
237065b65735SBrian Foster 
237165b65735SBrian Foster 	error = xfs_qm_init();
237265b65735SBrian Foster 	if (error)
2373bb230c12SBill O'Donnell 		goto out_remove_dbg_kobj;
2374c59d87c4SChristoph Hellwig 
2375c59d87c4SChristoph Hellwig 	error = register_filesystem(&xfs_fs_type);
2376c59d87c4SChristoph Hellwig 	if (error)
2377a05931ceSChristoph Hellwig 		goto out_qm_exit;
2378c59d87c4SChristoph Hellwig 	return 0;
2379c59d87c4SChristoph Hellwig 
2380a05931ceSChristoph Hellwig  out_qm_exit:
2381a05931ceSChristoph Hellwig 	xfs_qm_exit();
2382bb230c12SBill O'Donnell  out_remove_dbg_kobj:
238365b65735SBrian Foster #ifdef DEBUG
238465b65735SBrian Foster 	xfs_sysfs_del(&xfs_dbg_kobj);
2385d7a74cadSDarrick J. Wong  out_remove_scrub_stats:
238665b65735SBrian Foster #endif
2387d7a74cadSDarrick J. Wong 	xchk_global_stats_teardown();
2388d7a74cadSDarrick J. Wong  out_remove_stats_kobj:
238980529c45SBill O'Donnell 	xfs_sysfs_del(&xfsstats.xs_kobj);
239080529c45SBill O'Donnell  out_free_stats:
239180529c45SBill O'Donnell 	free_percpu(xfsstats.xs_stats);
2392bb230c12SBill O'Donnell  out_kset_unregister:
23933d871226SBrian Foster 	kset_unregister(xfs_kset);
2394a76dba3bSDarrick J. Wong  out_debugfs_unregister:
2395a76dba3bSDarrick J. Wong 	debugfs_remove(xfs_debugfs);
2396c59d87c4SChristoph Hellwig 	xfs_sysctl_unregister();
2397c59d87c4SChristoph Hellwig  out_cleanup_procfs:
2398c59d87c4SChristoph Hellwig 	xfs_cleanup_procfs();
2399c59d87c4SChristoph Hellwig  out_mru_cache_uninit:
2400c59d87c4SChristoph Hellwig 	xfs_mru_cache_uninit();
2401c59d87c4SChristoph Hellwig  out_destroy_wq:
2402c59d87c4SChristoph Hellwig 	xfs_destroy_workqueues();
2403182696fbSDarrick J. Wong  out_destroy_caches:
2404182696fbSDarrick J. Wong 	xfs_destroy_caches();
2405c59d87c4SChristoph Hellwig  out:
2406c59d87c4SChristoph Hellwig 	return error;
2407c59d87c4SChristoph Hellwig }
2408c59d87c4SChristoph Hellwig 
2409c59d87c4SChristoph Hellwig STATIC void __exit
exit_xfs_fs(void)2410c59d87c4SChristoph Hellwig exit_xfs_fs(void)
2411c59d87c4SChristoph Hellwig {
2412a05931ceSChristoph Hellwig 	xfs_qm_exit();
2413c59d87c4SChristoph Hellwig 	unregister_filesystem(&xfs_fs_type);
241465b65735SBrian Foster #ifdef DEBUG
241565b65735SBrian Foster 	xfs_sysfs_del(&xfs_dbg_kobj);
241665b65735SBrian Foster #endif
2417d7a74cadSDarrick J. Wong 	xchk_global_stats_teardown();
241880529c45SBill O'Donnell 	xfs_sysfs_del(&xfsstats.xs_kobj);
241980529c45SBill O'Donnell 	free_percpu(xfsstats.xs_stats);
24203d871226SBrian Foster 	kset_unregister(xfs_kset);
2421a76dba3bSDarrick J. Wong 	debugfs_remove(xfs_debugfs);
2422c59d87c4SChristoph Hellwig 	xfs_sysctl_unregister();
2423c59d87c4SChristoph Hellwig 	xfs_cleanup_procfs();
2424c59d87c4SChristoph Hellwig 	xfs_mru_cache_uninit();
2425c59d87c4SChristoph Hellwig 	xfs_destroy_workqueues();
2426182696fbSDarrick J. Wong 	xfs_destroy_caches();
2427af3b6382SDarrick J. Wong 	xfs_uuid_table_free();
2428c59d87c4SChristoph Hellwig }
2429c59d87c4SChristoph Hellwig 
2430c59d87c4SChristoph Hellwig module_init(init_xfs_fs);
2431c59d87c4SChristoph Hellwig module_exit(exit_xfs_fs);
2432c59d87c4SChristoph Hellwig 
2433c59d87c4SChristoph Hellwig MODULE_AUTHOR("Silicon Graphics, Inc.");
2434c59d87c4SChristoph Hellwig MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
2435c59d87c4SChristoph Hellwig MODULE_LICENSE("GPL");
2436