xref: /openbmc/linux/fs/xfs/xfs_super.c (revision a0ebcdab)
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 
881c59d87c4SChristoph Hellwig 
882db07349dSChristoph Hellwig 	if ((ip->i_diflags & XFS_DIFLAG_PROJINHERIT) &&
88383e782e1SChandra Seetharaman 	    ((mp->m_qflags & (XFS_PQUOTA_ACCT|XFS_PQUOTA_ENFD))) ==
88483e782e1SChandra Seetharaman 			      (XFS_PQUOTA_ACCT|XFS_PQUOTA_ENFD))
885c59d87c4SChristoph Hellwig 		xfs_qm_statvfs(ip, statp);
886a0158315SRichard Wareing 
887a0158315SRichard Wareing 	if (XFS_IS_REALTIME_MOUNT(mp) &&
888db07349dSChristoph Hellwig 	    (ip->i_diflags & (XFS_DIFLAG_RTINHERIT | XFS_DIFLAG_REALTIME))) {
8892229276cSDarrick J. Wong 		s64	freertx;
8902229276cSDarrick J. Wong 
891a0158315SRichard Wareing 		statp->f_blocks = sbp->sb_rblocks;
8922229276cSDarrick J. Wong 		freertx = percpu_counter_sum_positive(&mp->m_frextents);
8932229276cSDarrick J. Wong 		statp->f_bavail = statp->f_bfree = freertx * sbp->sb_rextsize;
894a0158315SRichard Wareing 	}
895a0158315SRichard Wareing 
896c59d87c4SChristoph Hellwig 	return 0;
897c59d87c4SChristoph Hellwig }
898c59d87c4SChristoph Hellwig 
899c59d87c4SChristoph Hellwig STATIC void
xfs_save_resvblks(struct xfs_mount * mp)900c59d87c4SChristoph Hellwig xfs_save_resvblks(struct xfs_mount *mp)
901c59d87c4SChristoph Hellwig {
902c8ce540dSDarrick J. Wong 	uint64_t resblks = 0;
903c59d87c4SChristoph Hellwig 
904c59d87c4SChristoph Hellwig 	mp->m_resblks_save = mp->m_resblks;
905c59d87c4SChristoph Hellwig 	xfs_reserve_blocks(mp, &resblks, NULL);
906c59d87c4SChristoph Hellwig }
907c59d87c4SChristoph Hellwig 
908c59d87c4SChristoph Hellwig STATIC void
xfs_restore_resvblks(struct xfs_mount * mp)909c59d87c4SChristoph Hellwig xfs_restore_resvblks(struct xfs_mount *mp)
910c59d87c4SChristoph Hellwig {
911c8ce540dSDarrick J. Wong 	uint64_t resblks;
912c59d87c4SChristoph Hellwig 
913c59d87c4SChristoph Hellwig 	if (mp->m_resblks_save) {
914c59d87c4SChristoph Hellwig 		resblks = mp->m_resblks_save;
915c59d87c4SChristoph Hellwig 		mp->m_resblks_save = 0;
916c59d87c4SChristoph Hellwig 	} else
917c59d87c4SChristoph Hellwig 		resblks = xfs_default_resblks(mp);
918c59d87c4SChristoph Hellwig 
919c59d87c4SChristoph Hellwig 	xfs_reserve_blocks(mp, &resblks, NULL);
920c59d87c4SChristoph Hellwig }
921c59d87c4SChristoph Hellwig 
922c7eea6f7SDave Chinner /*
923c59d87c4SChristoph Hellwig  * Second stage of a freeze. The data is already frozen so we only
92461e63ecbSDave Chinner  * need to take care of the metadata. Once that's done sync the superblock
92561e63ecbSDave Chinner  * to the log to dirty it in case of a crash while frozen. This ensures that we
92661e63ecbSDave Chinner  * will recover the unlinked inode lists on the next mount.
927c59d87c4SChristoph Hellwig  */
928c59d87c4SChristoph Hellwig STATIC int
xfs_fs_freeze(struct super_block * sb)929c59d87c4SChristoph Hellwig xfs_fs_freeze(
930c59d87c4SChristoph Hellwig 	struct super_block	*sb)
931c59d87c4SChristoph Hellwig {
932c59d87c4SChristoph Hellwig 	struct xfs_mount	*mp = XFS_M(sb);
933c3f2375bSWaiman Long 	unsigned int		flags;
934c3f2375bSWaiman Long 	int			ret;
935c59d87c4SChristoph Hellwig 
936c3f2375bSWaiman Long 	/*
937c3f2375bSWaiman Long 	 * The filesystem is now frozen far enough that memory reclaim
938c3f2375bSWaiman Long 	 * cannot safely operate on the filesystem. Hence we need to
939c3f2375bSWaiman Long 	 * set a GFP_NOFS context here to avoid recursion deadlocks.
940c3f2375bSWaiman Long 	 */
941c3f2375bSWaiman Long 	flags = memalloc_nofs_save();
942c59d87c4SChristoph Hellwig 	xfs_save_resvblks(mp);
9435b0ad7c2SBrian Foster 	ret = xfs_log_quiesce(mp);
944c3f2375bSWaiman Long 	memalloc_nofs_restore(flags);
945ab23a776SDave Chinner 
946ab23a776SDave Chinner 	/*
947ab23a776SDave Chinner 	 * For read-write filesystems, we need to restart the inodegc on error
948ab23a776SDave Chinner 	 * because we stopped it at SB_FREEZE_PAGEFAULT level and a thaw is not
949ab23a776SDave Chinner 	 * going to be run to restart it now.  We are at SB_FREEZE_FS level
950ab23a776SDave Chinner 	 * here, so we can restart safely without racing with a stop in
951ab23a776SDave Chinner 	 * xfs_fs_sync_fs().
952ab23a776SDave Chinner 	 */
9532e973b2cSDave Chinner 	if (ret && !xfs_is_readonly(mp)) {
9546f649091SDarrick J. Wong 		xfs_blockgc_start(mp);
955ab23a776SDave Chinner 		xfs_inodegc_start(mp);
9566f649091SDarrick J. Wong 	}
957ab23a776SDave Chinner 
958c3f2375bSWaiman Long 	return ret;
959c59d87c4SChristoph Hellwig }
960c59d87c4SChristoph Hellwig 
961c59d87c4SChristoph Hellwig STATIC int
xfs_fs_unfreeze(struct super_block * sb)962c59d87c4SChristoph Hellwig xfs_fs_unfreeze(
963c59d87c4SChristoph Hellwig 	struct super_block	*sb)
964c59d87c4SChristoph Hellwig {
965c59d87c4SChristoph Hellwig 	struct xfs_mount	*mp = XFS_M(sb);
966c59d87c4SChristoph Hellwig 
967c59d87c4SChristoph Hellwig 	xfs_restore_resvblks(mp);
968f661f1e0SDave Chinner 	xfs_log_work_queue(mp);
969ab23a776SDave Chinner 
970ab23a776SDave Chinner 	/*
971ab23a776SDave Chinner 	 * Don't reactivate the inodegc worker on a readonly filesystem because
9726f649091SDarrick J. Wong 	 * inodes are sent directly to reclaim.  Don't reactivate the blockgc
9736f649091SDarrick J. Wong 	 * worker because there are no speculative preallocations on a readonly
9746f649091SDarrick J. Wong 	 * filesystem.
975ab23a776SDave Chinner 	 */
9762e973b2cSDave Chinner 	if (!xfs_is_readonly(mp)) {
977c9a6526fSDarrick J. Wong 		xfs_blockgc_start(mp);
978ab23a776SDave Chinner 		xfs_inodegc_start(mp);
9796f649091SDarrick J. Wong 	}
980ab23a776SDave Chinner 
981c59d87c4SChristoph Hellwig 	return 0;
982c59d87c4SChristoph Hellwig }
983c59d87c4SChristoph Hellwig 
984c59d87c4SChristoph Hellwig /*
985c59d87c4SChristoph Hellwig  * This function fills in xfs_mount_t fields based on mount args.
986c59d87c4SChristoph Hellwig  * Note: the superblock _has_ now been read in.
987c59d87c4SChristoph Hellwig  */
988c59d87c4SChristoph Hellwig STATIC int
xfs_finish_flags(struct xfs_mount * mp)989c59d87c4SChristoph Hellwig xfs_finish_flags(
990c59d87c4SChristoph Hellwig 	struct xfs_mount	*mp)
991c59d87c4SChristoph Hellwig {
992c59d87c4SChristoph Hellwig 	/* Fail a mount where the logbuf is smaller than the log stripe */
99338c26bfdSDave Chinner 	if (xfs_has_logv2(mp)) {
994c59d87c4SChristoph Hellwig 		if (mp->m_logbsize <= 0 &&
995c59d87c4SChristoph Hellwig 		    mp->m_sb.sb_logsunit > XLOG_BIG_RECORD_BSIZE) {
996c59d87c4SChristoph Hellwig 			mp->m_logbsize = mp->m_sb.sb_logsunit;
997c59d87c4SChristoph Hellwig 		} else if (mp->m_logbsize > 0 &&
998c59d87c4SChristoph Hellwig 			   mp->m_logbsize < mp->m_sb.sb_logsunit) {
999c59d87c4SChristoph Hellwig 			xfs_warn(mp,
1000c59d87c4SChristoph Hellwig 		"logbuf size must be greater than or equal to log stripe size");
10012451337dSDave Chinner 			return -EINVAL;
1002c59d87c4SChristoph Hellwig 		}
1003c59d87c4SChristoph Hellwig 	} else {
1004c59d87c4SChristoph Hellwig 		/* Fail a mount if the logbuf is larger than 32K */
1005c59d87c4SChristoph Hellwig 		if (mp->m_logbsize > XLOG_BIG_RECORD_BSIZE) {
1006c59d87c4SChristoph Hellwig 			xfs_warn(mp,
1007c59d87c4SChristoph Hellwig 		"logbuf size for version 1 logs must be 16K or 32K");
10082451337dSDave Chinner 			return -EINVAL;
1009c59d87c4SChristoph Hellwig 		}
1010c59d87c4SChristoph Hellwig 	}
1011c59d87c4SChristoph Hellwig 
1012c59d87c4SChristoph Hellwig 	/*
1013d3eaace8SDave Chinner 	 * V5 filesystems always use attr2 format for attributes.
1014d3eaace8SDave Chinner 	 */
10150560f31aSDave Chinner 	if (xfs_has_crc(mp) && xfs_has_noattr2(mp)) {
10162e74af0eSEric Sandeen 		xfs_warn(mp, "Cannot mount a V5 filesystem as noattr2. "
10172e74af0eSEric Sandeen 			     "attr2 is always enabled for V5 filesystems.");
10182451337dSDave Chinner 		return -EINVAL;
1019d3eaace8SDave Chinner 	}
1020d3eaace8SDave Chinner 
1021d3eaace8SDave Chinner 	/*
1022c59d87c4SChristoph Hellwig 	 * prohibit r/w mounts of read-only filesystems
1023c59d87c4SChristoph Hellwig 	 */
10242e973b2cSDave Chinner 	if ((mp->m_sb.sb_flags & XFS_SBF_READONLY) && !xfs_is_readonly(mp)) {
1025c59d87c4SChristoph Hellwig 		xfs_warn(mp,
1026c59d87c4SChristoph Hellwig 			"cannot mount a read-only filesystem as read-write");
10272451337dSDave Chinner 		return -EROFS;
1028c59d87c4SChristoph Hellwig 	}
1029c59d87c4SChristoph Hellwig 
1030149e53afSChristoph Hellwig 	if ((mp->m_qflags & XFS_GQUOTA_ACCT) &&
1031149e53afSChristoph Hellwig 	    (mp->m_qflags & XFS_PQUOTA_ACCT) &&
103238c26bfdSDave Chinner 	    !xfs_has_pquotino(mp)) {
1033d892d586SChandra Seetharaman 		xfs_warn(mp,
1034d892d586SChandra Seetharaman 		  "Super block does not support project and group quota together");
10352451337dSDave Chinner 		return -EINVAL;
1036d892d586SChandra Seetharaman 	}
1037d892d586SChandra Seetharaman 
1038c59d87c4SChristoph Hellwig 	return 0;
1039c59d87c4SChristoph Hellwig }
1040c59d87c4SChristoph Hellwig 
10415681ca40SDave Chinner static int
xfs_init_percpu_counters(struct xfs_mount * mp)10425681ca40SDave Chinner xfs_init_percpu_counters(
10435681ca40SDave Chinner 	struct xfs_mount	*mp)
10445681ca40SDave Chinner {
10455681ca40SDave Chinner 	int		error;
10465681ca40SDave Chinner 
10475681ca40SDave Chinner 	error = percpu_counter_init(&mp->m_icount, 0, GFP_KERNEL);
10485681ca40SDave Chinner 	if (error)
10495e9383f9SJoe Perches 		return -ENOMEM;
10505681ca40SDave Chinner 
10515681ca40SDave Chinner 	error = percpu_counter_init(&mp->m_ifree, 0, GFP_KERNEL);
10525681ca40SDave Chinner 	if (error)
10535681ca40SDave Chinner 		goto free_icount;
10545681ca40SDave Chinner 
10555681ca40SDave Chinner 	error = percpu_counter_init(&mp->m_fdblocks, 0, GFP_KERNEL);
10565681ca40SDave Chinner 	if (error)
10575681ca40SDave Chinner 		goto free_ifree;
10585681ca40SDave Chinner 
10599fe82b8cSDarrick J. Wong 	error = percpu_counter_init(&mp->m_delalloc_blks, 0, GFP_KERNEL);
10609fe82b8cSDarrick J. Wong 	if (error)
10619fe82b8cSDarrick J. Wong 		goto free_fdblocks;
10629fe82b8cSDarrick J. Wong 
10632229276cSDarrick J. Wong 	error = percpu_counter_init(&mp->m_frextents, 0, GFP_KERNEL);
10642229276cSDarrick J. Wong 	if (error)
10652229276cSDarrick J. Wong 		goto free_delalloc;
10662229276cSDarrick J. Wong 
10675681ca40SDave Chinner 	return 0;
10685681ca40SDave Chinner 
10692229276cSDarrick J. Wong free_delalloc:
10702229276cSDarrick J. Wong 	percpu_counter_destroy(&mp->m_delalloc_blks);
10719fe82b8cSDarrick J. Wong free_fdblocks:
10729fe82b8cSDarrick J. Wong 	percpu_counter_destroy(&mp->m_fdblocks);
10735681ca40SDave Chinner free_ifree:
10745681ca40SDave Chinner 	percpu_counter_destroy(&mp->m_ifree);
10755681ca40SDave Chinner free_icount:
10765681ca40SDave Chinner 	percpu_counter_destroy(&mp->m_icount);
10775681ca40SDave Chinner 	return -ENOMEM;
10785681ca40SDave Chinner }
10795681ca40SDave Chinner 
10805681ca40SDave Chinner void
xfs_reinit_percpu_counters(struct xfs_mount * mp)10815681ca40SDave Chinner xfs_reinit_percpu_counters(
10825681ca40SDave Chinner 	struct xfs_mount	*mp)
10835681ca40SDave Chinner {
10845681ca40SDave Chinner 	percpu_counter_set(&mp->m_icount, mp->m_sb.sb_icount);
10855681ca40SDave Chinner 	percpu_counter_set(&mp->m_ifree, mp->m_sb.sb_ifree);
10865681ca40SDave Chinner 	percpu_counter_set(&mp->m_fdblocks, mp->m_sb.sb_fdblocks);
10872229276cSDarrick J. Wong 	percpu_counter_set(&mp->m_frextents, mp->m_sb.sb_frextents);
10885681ca40SDave Chinner }
10895681ca40SDave Chinner 
10905681ca40SDave Chinner static void
xfs_destroy_percpu_counters(struct xfs_mount * mp)10915681ca40SDave Chinner xfs_destroy_percpu_counters(
10925681ca40SDave Chinner 	struct xfs_mount	*mp)
10935681ca40SDave Chinner {
10945681ca40SDave Chinner 	percpu_counter_destroy(&mp->m_icount);
10955681ca40SDave Chinner 	percpu_counter_destroy(&mp->m_ifree);
10965681ca40SDave Chinner 	percpu_counter_destroy(&mp->m_fdblocks);
109775c8c50fSDave Chinner 	ASSERT(xfs_is_shutdown(mp) ||
10989fe82b8cSDarrick J. Wong 	       percpu_counter_sum(&mp->m_delalloc_blks) == 0);
10999fe82b8cSDarrick J. Wong 	percpu_counter_destroy(&mp->m_delalloc_blks);
11002229276cSDarrick J. Wong 	percpu_counter_destroy(&mp->m_frextents);
11015681ca40SDave Chinner }
11025681ca40SDave Chinner 
1103ab23a776SDave Chinner static int
xfs_inodegc_init_percpu(struct xfs_mount * mp)1104ab23a776SDave Chinner xfs_inodegc_init_percpu(
1105ab23a776SDave Chinner 	struct xfs_mount	*mp)
1106ab23a776SDave Chinner {
1107ab23a776SDave Chinner 	struct xfs_inodegc	*gc;
1108ab23a776SDave Chinner 	int			cpu;
1109ab23a776SDave Chinner 
1110ab23a776SDave Chinner 	mp->m_inodegc = alloc_percpu(struct xfs_inodegc);
1111ab23a776SDave Chinner 	if (!mp->m_inodegc)
1112ab23a776SDave Chinner 		return -ENOMEM;
1113ab23a776SDave Chinner 
1114ab23a776SDave Chinner 	for_each_possible_cpu(cpu) {
1115ab23a776SDave Chinner 		gc = per_cpu_ptr(mp->m_inodegc, cpu);
1116b37c4c83SDarrick J. Wong 		gc->cpu = cpu;
111762334fabSDarrick J. Wong 		gc->mp = mp;
1118ab23a776SDave Chinner 		init_llist_head(&gc->list);
1119ab23a776SDave Chinner 		gc->items = 0;
1120d4d12c02SDave Chinner 		gc->error = 0;
11217cf2b0f9SDave Chinner 		INIT_DELAYED_WORK(&gc->work, xfs_inodegc_worker);
1122ab23a776SDave Chinner 	}
1123ab23a776SDave Chinner 	return 0;
1124ab23a776SDave Chinner }
1125ab23a776SDave Chinner 
1126ab23a776SDave Chinner static void
xfs_inodegc_free_percpu(struct xfs_mount * mp)1127ab23a776SDave Chinner xfs_inodegc_free_percpu(
1128ab23a776SDave Chinner 	struct xfs_mount	*mp)
1129ab23a776SDave Chinner {
1130ab23a776SDave Chinner 	if (!mp->m_inodegc)
1131ab23a776SDave Chinner 		return;
1132ab23a776SDave Chinner 	free_percpu(mp->m_inodegc);
1133ab23a776SDave Chinner }
1134ab23a776SDave Chinner 
11352f8d66b3SIan Kent static void
xfs_fs_put_super(struct super_block * sb)11362f8d66b3SIan Kent xfs_fs_put_super(
11372f8d66b3SIan Kent 	struct super_block	*sb)
11382f8d66b3SIan Kent {
11392f8d66b3SIan Kent 	struct xfs_mount	*mp = XFS_M(sb);
11402f8d66b3SIan Kent 
114164c80dfdSLukas Herbolt 	xfs_notice(mp, "Unmounting Filesystem %pU", &mp->m_sb.sb_uuid);
11422f8d66b3SIan Kent 	xfs_filestream_unmount(mp);
11432f8d66b3SIan Kent 	xfs_unmountfs(mp);
11442f8d66b3SIan Kent 
11452f8d66b3SIan Kent 	xfs_freesb(mp);
1146d7a74cadSDarrick J. Wong 	xchk_mount_stats_free(mp);
11472f8d66b3SIan Kent 	free_percpu(mp->m_stats.xs_stats);
1148ab23a776SDave Chinner 	xfs_inodegc_free_percpu(mp);
11492f8d66b3SIan Kent 	xfs_destroy_percpu_counters(mp);
11502f8d66b3SIan Kent 	xfs_destroy_mount_workqueues(mp);
115135a93b14SChristoph Hellwig 	xfs_shutdown_devices(mp);
11522f8d66b3SIan Kent }
11532f8d66b3SIan Kent 
11542f8d66b3SIan Kent static long
xfs_fs_nr_cached_objects(struct super_block * sb,struct shrink_control * sc)11552f8d66b3SIan Kent xfs_fs_nr_cached_objects(
11562f8d66b3SIan Kent 	struct super_block	*sb,
11572f8d66b3SIan Kent 	struct shrink_control	*sc)
11582f8d66b3SIan Kent {
11592f8d66b3SIan Kent 	/* Paranoia: catch incorrect calls during mount setup or teardown */
11602f8d66b3SIan Kent 	if (WARN_ON_ONCE(!sb->s_fs_info))
11612f8d66b3SIan Kent 		return 0;
11622f8d66b3SIan Kent 	return xfs_reclaim_inodes_count(XFS_M(sb));
11632f8d66b3SIan Kent }
11642f8d66b3SIan Kent 
11652f8d66b3SIan Kent static long
xfs_fs_free_cached_objects(struct super_block * sb,struct shrink_control * sc)11662f8d66b3SIan Kent xfs_fs_free_cached_objects(
11672f8d66b3SIan Kent 	struct super_block	*sb,
11682f8d66b3SIan Kent 	struct shrink_control	*sc)
11692f8d66b3SIan Kent {
11702f8d66b3SIan Kent 	return xfs_reclaim_inodes_nr(XFS_M(sb), sc->nr_to_scan);
11712f8d66b3SIan Kent }
11722f8d66b3SIan Kent 
1173e7caa877SChristoph Hellwig static void
xfs_fs_shutdown(struct super_block * sb)1174e7caa877SChristoph Hellwig xfs_fs_shutdown(
1175e7caa877SChristoph Hellwig 	struct super_block	*sb)
1176e7caa877SChristoph Hellwig {
1177e7caa877SChristoph Hellwig 	xfs_force_shutdown(XFS_M(sb), SHUTDOWN_DEVICE_REMOVED);
1178e7caa877SChristoph Hellwig }
1179e7caa877SChristoph Hellwig 
11802f8d66b3SIan Kent static const struct super_operations xfs_super_operations = {
11812f8d66b3SIan Kent 	.alloc_inode		= xfs_fs_alloc_inode,
11822f8d66b3SIan Kent 	.destroy_inode		= xfs_fs_destroy_inode,
11832f8d66b3SIan Kent 	.dirty_inode		= xfs_fs_dirty_inode,
11842f8d66b3SIan Kent 	.drop_inode		= xfs_fs_drop_inode,
11852f8d66b3SIan Kent 	.put_super		= xfs_fs_put_super,
11862f8d66b3SIan Kent 	.sync_fs		= xfs_fs_sync_fs,
11872f8d66b3SIan Kent 	.freeze_fs		= xfs_fs_freeze,
11882f8d66b3SIan Kent 	.unfreeze_fs		= xfs_fs_unfreeze,
11892f8d66b3SIan Kent 	.statfs			= xfs_fs_statfs,
11902f8d66b3SIan Kent 	.show_options		= xfs_fs_show_options,
11912f8d66b3SIan Kent 	.nr_cached_objects	= xfs_fs_nr_cached_objects,
11922f8d66b3SIan Kent 	.free_cached_objects	= xfs_fs_free_cached_objects,
1193e7caa877SChristoph Hellwig 	.shutdown		= xfs_fs_shutdown,
11942f8d66b3SIan Kent };
11952f8d66b3SIan Kent 
119673e5fff9SIan Kent static int
suffix_kstrtoint(const char * s,unsigned int base,int * res)11978757c38fSIan Kent suffix_kstrtoint(
11988757c38fSIan Kent 	const char	*s,
11998757c38fSIan Kent 	unsigned int	base,
12008757c38fSIan Kent 	int		*res)
12018757c38fSIan Kent {
12028757c38fSIan Kent 	int		last, shift_left_factor = 0, _res;
12038757c38fSIan Kent 	char		*value;
12048757c38fSIan Kent 	int		ret = 0;
12058757c38fSIan Kent 
12068757c38fSIan Kent 	value = kstrdup(s, GFP_KERNEL);
12078757c38fSIan Kent 	if (!value)
12088757c38fSIan Kent 		return -ENOMEM;
12098757c38fSIan Kent 
12108757c38fSIan Kent 	last = strlen(value) - 1;
12118757c38fSIan Kent 	if (value[last] == 'K' || value[last] == 'k') {
12128757c38fSIan Kent 		shift_left_factor = 10;
12138757c38fSIan Kent 		value[last] = '\0';
12148757c38fSIan Kent 	}
12158757c38fSIan Kent 	if (value[last] == 'M' || value[last] == 'm') {
12168757c38fSIan Kent 		shift_left_factor = 20;
12178757c38fSIan Kent 		value[last] = '\0';
12188757c38fSIan Kent 	}
12198757c38fSIan Kent 	if (value[last] == 'G' || value[last] == 'g') {
12208757c38fSIan Kent 		shift_left_factor = 30;
12218757c38fSIan Kent 		value[last] = '\0';
12228757c38fSIan Kent 	}
12238757c38fSIan Kent 
12248757c38fSIan Kent 	if (kstrtoint(value, base, &_res))
12258757c38fSIan Kent 		ret = -EINVAL;
12268757c38fSIan Kent 	kfree(value);
12278757c38fSIan Kent 	*res = _res << shift_left_factor;
12288757c38fSIan Kent 	return ret;
12298757c38fSIan Kent }
12308757c38fSIan Kent 
123192cf7d36SPavel Reichl static inline void
xfs_fs_warn_deprecated(struct fs_context * fc,struct fs_parameter * param,uint64_t flag,bool value)123292cf7d36SPavel Reichl xfs_fs_warn_deprecated(
123392cf7d36SPavel Reichl 	struct fs_context	*fc,
123492cf7d36SPavel Reichl 	struct fs_parameter	*param,
123592cf7d36SPavel Reichl 	uint64_t		flag,
123692cf7d36SPavel Reichl 	bool			value)
123792cf7d36SPavel Reichl {
123892cf7d36SPavel Reichl 	/* Don't print the warning if reconfiguring and current mount point
123992cf7d36SPavel Reichl 	 * already had the flag set
124092cf7d36SPavel Reichl 	 */
124192cf7d36SPavel Reichl 	if ((fc->purpose & FS_CONTEXT_FOR_RECONFIGURE) &&
12420560f31aSDave Chinner             !!(XFS_M(fc->root->d_sb)->m_features & flag) == value)
124392cf7d36SPavel Reichl 		return;
124492cf7d36SPavel Reichl 	xfs_warn(fc->s_fs_info, "%s mount option is deprecated.", param->key);
124592cf7d36SPavel Reichl }
124692cf7d36SPavel Reichl 
12478757c38fSIan Kent /*
12488757c38fSIan Kent  * Set mount state from a mount option.
12498757c38fSIan Kent  *
12508757c38fSIan Kent  * NOTE: mp->m_super is NULL here!
12518757c38fSIan Kent  */
12528757c38fSIan Kent static int
xfs_fs_parse_param(struct fs_context * fc,struct fs_parameter * param)12531e5c39dfSDarrick J. Wong xfs_fs_parse_param(
12548757c38fSIan Kent 	struct fs_context	*fc,
12558757c38fSIan Kent 	struct fs_parameter	*param)
12568757c38fSIan Kent {
12570f98b4ecSPavel Reichl 	struct xfs_mount	*parsing_mp = fc->s_fs_info;
12588757c38fSIan Kent 	struct fs_parse_result	result;
12598757c38fSIan Kent 	int			size = 0;
12608757c38fSIan Kent 	int			opt;
12618757c38fSIan Kent 
1262d7167b14SAl Viro 	opt = fs_parse(fc, xfs_fs_parameters, param, &result);
12638757c38fSIan Kent 	if (opt < 0)
12648757c38fSIan Kent 		return opt;
12658757c38fSIan Kent 
12668757c38fSIan Kent 	switch (opt) {
12678757c38fSIan Kent 	case Opt_logbufs:
12680f98b4ecSPavel Reichl 		parsing_mp->m_logbufs = result.uint_32;
12698757c38fSIan Kent 		return 0;
12708757c38fSIan Kent 	case Opt_logbsize:
12710f98b4ecSPavel Reichl 		if (suffix_kstrtoint(param->string, 10, &parsing_mp->m_logbsize))
12728757c38fSIan Kent 			return -EINVAL;
12738757c38fSIan Kent 		return 0;
12748757c38fSIan Kent 	case Opt_logdev:
12750f98b4ecSPavel Reichl 		kfree(parsing_mp->m_logname);
12760f98b4ecSPavel Reichl 		parsing_mp->m_logname = kstrdup(param->string, GFP_KERNEL);
12770f98b4ecSPavel Reichl 		if (!parsing_mp->m_logname)
12788757c38fSIan Kent 			return -ENOMEM;
12798757c38fSIan Kent 		return 0;
12808757c38fSIan Kent 	case Opt_rtdev:
12810f98b4ecSPavel Reichl 		kfree(parsing_mp->m_rtname);
12820f98b4ecSPavel Reichl 		parsing_mp->m_rtname = kstrdup(param->string, GFP_KERNEL);
12830f98b4ecSPavel Reichl 		if (!parsing_mp->m_rtname)
12848757c38fSIan Kent 			return -ENOMEM;
12858757c38fSIan Kent 		return 0;
12868757c38fSIan Kent 	case Opt_allocsize:
12878757c38fSIan Kent 		if (suffix_kstrtoint(param->string, 10, &size))
12888757c38fSIan Kent 			return -EINVAL;
12890f98b4ecSPavel Reichl 		parsing_mp->m_allocsize_log = ffs(size) - 1;
12900560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_ALLOCSIZE;
12918757c38fSIan Kent 		return 0;
12928757c38fSIan Kent 	case Opt_grpid:
12938757c38fSIan Kent 	case Opt_bsdgroups:
12940560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_GRPID;
12958757c38fSIan Kent 		return 0;
12968757c38fSIan Kent 	case Opt_nogrpid:
12978757c38fSIan Kent 	case Opt_sysvgroups:
12980560f31aSDave Chinner 		parsing_mp->m_features &= ~XFS_FEAT_GRPID;
12998757c38fSIan Kent 		return 0;
13008757c38fSIan Kent 	case Opt_wsync:
13010560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_WSYNC;
13028757c38fSIan Kent 		return 0;
13038757c38fSIan Kent 	case Opt_norecovery:
13040560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_NORECOVERY;
13058757c38fSIan Kent 		return 0;
13068757c38fSIan Kent 	case Opt_noalign:
13070560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_NOALIGN;
13088757c38fSIan Kent 		return 0;
13098757c38fSIan Kent 	case Opt_swalloc:
13100560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_SWALLOC;
13118757c38fSIan Kent 		return 0;
13128757c38fSIan Kent 	case Opt_sunit:
13130f98b4ecSPavel Reichl 		parsing_mp->m_dalign = result.uint_32;
13148757c38fSIan Kent 		return 0;
13158757c38fSIan Kent 	case Opt_swidth:
13160f98b4ecSPavel Reichl 		parsing_mp->m_swidth = result.uint_32;
13178757c38fSIan Kent 		return 0;
13188757c38fSIan Kent 	case Opt_inode32:
13190560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_SMALL_INUMS;
13208757c38fSIan Kent 		return 0;
13218757c38fSIan Kent 	case Opt_inode64:
13220560f31aSDave Chinner 		parsing_mp->m_features &= ~XFS_FEAT_SMALL_INUMS;
13238757c38fSIan Kent 		return 0;
13248757c38fSIan Kent 	case Opt_nouuid:
13250560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_NOUUID;
13268757c38fSIan Kent 		return 0;
13278757c38fSIan Kent 	case Opt_largeio:
13280560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_LARGE_IOSIZE;
13298757c38fSIan Kent 		return 0;
13308757c38fSIan Kent 	case Opt_nolargeio:
13310560f31aSDave Chinner 		parsing_mp->m_features &= ~XFS_FEAT_LARGE_IOSIZE;
13328757c38fSIan Kent 		return 0;
13338757c38fSIan Kent 	case Opt_filestreams:
13340560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_FILESTREAMS;
13358757c38fSIan Kent 		return 0;
13368757c38fSIan Kent 	case Opt_noquota:
13370f98b4ecSPavel Reichl 		parsing_mp->m_qflags &= ~XFS_ALL_QUOTA_ACCT;
13380f98b4ecSPavel Reichl 		parsing_mp->m_qflags &= ~XFS_ALL_QUOTA_ENFD;
13398757c38fSIan Kent 		return 0;
13408757c38fSIan Kent 	case Opt_quota:
13418757c38fSIan Kent 	case Opt_uquota:
13428757c38fSIan Kent 	case Opt_usrquota:
1343149e53afSChristoph Hellwig 		parsing_mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ENFD);
13448757c38fSIan Kent 		return 0;
13458757c38fSIan Kent 	case Opt_qnoenforce:
13468757c38fSIan Kent 	case Opt_uqnoenforce:
1347149e53afSChristoph Hellwig 		parsing_mp->m_qflags |= XFS_UQUOTA_ACCT;
13480f98b4ecSPavel Reichl 		parsing_mp->m_qflags &= ~XFS_UQUOTA_ENFD;
13498757c38fSIan Kent 		return 0;
13508757c38fSIan Kent 	case Opt_pquota:
13518757c38fSIan Kent 	case Opt_prjquota:
1352149e53afSChristoph Hellwig 		parsing_mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ENFD);
13538757c38fSIan Kent 		return 0;
13548757c38fSIan Kent 	case Opt_pqnoenforce:
1355149e53afSChristoph Hellwig 		parsing_mp->m_qflags |= XFS_PQUOTA_ACCT;
13560f98b4ecSPavel Reichl 		parsing_mp->m_qflags &= ~XFS_PQUOTA_ENFD;
13578757c38fSIan Kent 		return 0;
13588757c38fSIan Kent 	case Opt_gquota:
13598757c38fSIan Kent 	case Opt_grpquota:
1360149e53afSChristoph Hellwig 		parsing_mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ENFD);
13618757c38fSIan Kent 		return 0;
13628757c38fSIan Kent 	case Opt_gqnoenforce:
1363149e53afSChristoph Hellwig 		parsing_mp->m_qflags |= XFS_GQUOTA_ACCT;
13640f98b4ecSPavel Reichl 		parsing_mp->m_qflags &= ~XFS_GQUOTA_ENFD;
13658757c38fSIan Kent 		return 0;
13668757c38fSIan Kent 	case Opt_discard:
13670560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_DISCARD;
13688757c38fSIan Kent 		return 0;
13698757c38fSIan Kent 	case Opt_nodiscard:
13700560f31aSDave Chinner 		parsing_mp->m_features &= ~XFS_FEAT_DISCARD;
13718757c38fSIan Kent 		return 0;
13728757c38fSIan Kent #ifdef CONFIG_FS_DAX
13738757c38fSIan Kent 	case Opt_dax:
13740f98b4ecSPavel Reichl 		xfs_mount_set_dax_mode(parsing_mp, XFS_DAX_ALWAYS);
13758d6c3446SIra Weiny 		return 0;
13768d6c3446SIra Weiny 	case Opt_dax_enum:
13770f98b4ecSPavel Reichl 		xfs_mount_set_dax_mode(parsing_mp, result.uint_32);
13788757c38fSIan Kent 		return 0;
13798757c38fSIan Kent #endif
1380c23c393eSPavel Reichl 	/* Following mount options will be removed in September 2025 */
1381c23c393eSPavel Reichl 	case Opt_ikeep:
13820560f31aSDave Chinner 		xfs_fs_warn_deprecated(fc, param, XFS_FEAT_IKEEP, true);
13830560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_IKEEP;
1384c23c393eSPavel Reichl 		return 0;
1385c23c393eSPavel Reichl 	case Opt_noikeep:
13860560f31aSDave Chinner 		xfs_fs_warn_deprecated(fc, param, XFS_FEAT_IKEEP, false);
13870560f31aSDave Chinner 		parsing_mp->m_features &= ~XFS_FEAT_IKEEP;
1388c23c393eSPavel Reichl 		return 0;
1389c23c393eSPavel Reichl 	case Opt_attr2:
13900560f31aSDave Chinner 		xfs_fs_warn_deprecated(fc, param, XFS_FEAT_ATTR2, true);
13910560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_ATTR2;
1392c23c393eSPavel Reichl 		return 0;
1393c23c393eSPavel Reichl 	case Opt_noattr2:
13940560f31aSDave Chinner 		xfs_fs_warn_deprecated(fc, param, XFS_FEAT_NOATTR2, true);
13950560f31aSDave Chinner 		parsing_mp->m_features |= XFS_FEAT_NOATTR2;
1396c23c393eSPavel Reichl 		return 0;
13978757c38fSIan Kent 	default:
13980f98b4ecSPavel Reichl 		xfs_warn(parsing_mp, "unknown mount option [%s].", param->key);
13998757c38fSIan Kent 		return -EINVAL;
14008757c38fSIan Kent 	}
14018757c38fSIan Kent 
14028757c38fSIan Kent 	return 0;
14038757c38fSIan Kent }
14048757c38fSIan Kent 
14058757c38fSIan Kent static int
xfs_fs_validate_params(struct xfs_mount * mp)14061e5c39dfSDarrick J. Wong xfs_fs_validate_params(
14078757c38fSIan Kent 	struct xfs_mount	*mp)
14088757c38fSIan Kent {
14090560f31aSDave Chinner 	/* No recovery flag requires a read-only mount */
14102e973b2cSDave Chinner 	if (xfs_has_norecovery(mp) && !xfs_is_readonly(mp)) {
14118757c38fSIan Kent 		xfs_warn(mp, "no-recovery mounts must be read-only.");
14128757c38fSIan Kent 		return -EINVAL;
14138757c38fSIan Kent 	}
14148757c38fSIan Kent 
14150560f31aSDave Chinner 	/*
14160560f31aSDave Chinner 	 * We have not read the superblock at this point, so only the attr2
14170560f31aSDave Chinner 	 * mount option can set the attr2 feature by this stage.
14180560f31aSDave Chinner 	 */
14190560f31aSDave Chinner 	if (xfs_has_attr2(mp) && xfs_has_noattr2(mp)) {
1420e23b55d5SDave Chinner 		xfs_warn(mp, "attr2 and noattr2 cannot both be specified.");
1421e23b55d5SDave Chinner 		return -EINVAL;
1422e23b55d5SDave Chinner 	}
1423e23b55d5SDave Chinner 
1424e23b55d5SDave Chinner 
14250560f31aSDave Chinner 	if (xfs_has_noalign(mp) && (mp->m_dalign || mp->m_swidth)) {
14268757c38fSIan Kent 		xfs_warn(mp,
14278757c38fSIan Kent 	"sunit and swidth options incompatible with the noalign option");
14288757c38fSIan Kent 		return -EINVAL;
14298757c38fSIan Kent 	}
14308757c38fSIan Kent 
14318757c38fSIan Kent 	if (!IS_ENABLED(CONFIG_XFS_QUOTA) && mp->m_qflags != 0) {
14328757c38fSIan Kent 		xfs_warn(mp, "quota support not available in this kernel.");
14338757c38fSIan Kent 		return -EINVAL;
14348757c38fSIan Kent 	}
14358757c38fSIan Kent 
14368757c38fSIan Kent 	if ((mp->m_dalign && !mp->m_swidth) ||
14378757c38fSIan Kent 	    (!mp->m_dalign && mp->m_swidth)) {
14388757c38fSIan Kent 		xfs_warn(mp, "sunit and swidth must be specified together");
14398757c38fSIan Kent 		return -EINVAL;
14408757c38fSIan Kent 	}
14418757c38fSIan Kent 
14428757c38fSIan Kent 	if (mp->m_dalign && (mp->m_swidth % mp->m_dalign != 0)) {
14438757c38fSIan Kent 		xfs_warn(mp,
14448757c38fSIan Kent 	"stripe width (%d) must be a multiple of the stripe unit (%d)",
14458757c38fSIan Kent 			mp->m_swidth, mp->m_dalign);
14468757c38fSIan Kent 		return -EINVAL;
14478757c38fSIan Kent 	}
14488757c38fSIan Kent 
14498757c38fSIan Kent 	if (mp->m_logbufs != -1 &&
14508757c38fSIan Kent 	    mp->m_logbufs != 0 &&
14518757c38fSIan Kent 	    (mp->m_logbufs < XLOG_MIN_ICLOGS ||
14528757c38fSIan Kent 	     mp->m_logbufs > XLOG_MAX_ICLOGS)) {
14538757c38fSIan Kent 		xfs_warn(mp, "invalid logbufs value: %d [not %d-%d]",
14548757c38fSIan Kent 			mp->m_logbufs, XLOG_MIN_ICLOGS, XLOG_MAX_ICLOGS);
14558757c38fSIan Kent 		return -EINVAL;
14568757c38fSIan Kent 	}
14578757c38fSIan Kent 
14588757c38fSIan Kent 	if (mp->m_logbsize != -1 &&
14598757c38fSIan Kent 	    mp->m_logbsize !=  0 &&
14608757c38fSIan Kent 	    (mp->m_logbsize < XLOG_MIN_RECORD_BSIZE ||
14618757c38fSIan Kent 	     mp->m_logbsize > XLOG_MAX_RECORD_BSIZE ||
14628757c38fSIan Kent 	     !is_power_of_2(mp->m_logbsize))) {
14638757c38fSIan Kent 		xfs_warn(mp,
14648757c38fSIan Kent 			"invalid logbufsize: %d [not 16k,32k,64k,128k or 256k]",
14658757c38fSIan Kent 			mp->m_logbsize);
14668757c38fSIan Kent 		return -EINVAL;
14678757c38fSIan Kent 	}
14688757c38fSIan Kent 
14690560f31aSDave Chinner 	if (xfs_has_allocsize(mp) &&
14708757c38fSIan Kent 	    (mp->m_allocsize_log > XFS_MAX_IO_LOG ||
14718757c38fSIan Kent 	     mp->m_allocsize_log < XFS_MIN_IO_LOG)) {
14728757c38fSIan Kent 		xfs_warn(mp, "invalid log iosize: %d [not %d-%d]",
14738757c38fSIan Kent 			mp->m_allocsize_log, XFS_MIN_IO_LOG, XFS_MAX_IO_LOG);
14748757c38fSIan Kent 		return -EINVAL;
14758757c38fSIan Kent 	}
14768757c38fSIan Kent 
14778757c38fSIan Kent 	return 0;
14788757c38fSIan Kent }
14798757c38fSIan Kent 
1480a76dba3bSDarrick J. Wong struct dentry *
xfs_debugfs_mkdir(const char * name,struct dentry * parent)1481a76dba3bSDarrick J. Wong xfs_debugfs_mkdir(
1482a76dba3bSDarrick J. Wong 	const char	*name,
1483a76dba3bSDarrick J. Wong 	struct dentry	*parent)
1484a76dba3bSDarrick J. Wong {
1485a76dba3bSDarrick J. Wong 	struct dentry	*child;
1486a76dba3bSDarrick J. Wong 
1487a76dba3bSDarrick J. Wong 	/* Apparently we're expected to ignore error returns?? */
1488a76dba3bSDarrick J. Wong 	child = debugfs_create_dir(name, parent);
1489a76dba3bSDarrick J. Wong 	if (IS_ERR(child))
1490a76dba3bSDarrick J. Wong 		return NULL;
1491a76dba3bSDarrick J. Wong 
1492a76dba3bSDarrick J. Wong 	return child;
1493a76dba3bSDarrick J. Wong }
1494a76dba3bSDarrick J. Wong 
14958757c38fSIan Kent static int
xfs_fs_fill_super(struct super_block * sb,struct fs_context * fc)14961e5c39dfSDarrick J. Wong xfs_fs_fill_super(
1497c59d87c4SChristoph Hellwig 	struct super_block	*sb,
149873e5fff9SIan Kent 	struct fs_context	*fc)
1499c59d87c4SChristoph Hellwig {
150073e5fff9SIan Kent 	struct xfs_mount	*mp = sb->s_fs_info;
1501c59d87c4SChristoph Hellwig 	struct inode		*root;
15020279c71fSColin Ian King 	int			flags = 0, error;
1503c59d87c4SChristoph Hellwig 
15047c89fcb2SIan Kent 	mp->m_super = sb;
1505c59d87c4SChristoph Hellwig 
1506*a0ebcdabSDave Chinner 	/*
1507*a0ebcdabSDave Chinner 	 * Copy VFS mount flags from the context now that all parameter parsing
1508*a0ebcdabSDave Chinner 	 * is guaranteed to have been completed by either the old mount API or
1509*a0ebcdabSDave Chinner 	 * the newer fsopen/fsconfig API.
1510*a0ebcdabSDave Chinner 	 */
1511*a0ebcdabSDave Chinner 	if (fc->sb_flags & SB_RDONLY)
1512*a0ebcdabSDave Chinner 		set_bit(XFS_OPSTATE_READONLY, &mp->m_opstate);
1513*a0ebcdabSDave Chinner 	if (fc->sb_flags & SB_DIRSYNC)
1514*a0ebcdabSDave Chinner 		mp->m_features |= XFS_FEAT_DIRSYNC;
1515*a0ebcdabSDave Chinner 	if (fc->sb_flags & SB_SYNCHRONOUS)
1516*a0ebcdabSDave Chinner 		mp->m_features |= XFS_FEAT_WSYNC;
1517*a0ebcdabSDave Chinner 
15181e5c39dfSDarrick J. Wong 	error = xfs_fs_validate_params(mp);
1519c59d87c4SChristoph Hellwig 	if (error)
15202a9311adSChristoph Hellwig 		return error;
1521c59d87c4SChristoph Hellwig 
1522c59d87c4SChristoph Hellwig 	sb_min_blocksize(sb, BBSIZE);
1523c59d87c4SChristoph Hellwig 	sb->s_xattr = xfs_xattr_handlers;
1524c59d87c4SChristoph Hellwig 	sb->s_export_op = &xfs_export_operations;
1525c59d87c4SChristoph Hellwig #ifdef CONFIG_XFS_QUOTA
1526c59d87c4SChristoph Hellwig 	sb->s_qcop = &xfs_quotactl_operations;
152717ef4fddSJan Kara 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
1528c59d87c4SChristoph Hellwig #endif
1529c59d87c4SChristoph Hellwig 	sb->s_op = &xfs_super_operations;
1530c59d87c4SChristoph Hellwig 
1531dae5cd81SDave Chinner 	/*
1532dae5cd81SDave Chinner 	 * Delay mount work if the debug hook is set. This is debug
1533dae5cd81SDave Chinner 	 * instrumention to coordinate simulation of xfs mount failures with
1534dae5cd81SDave Chinner 	 * VFS superblock operations
1535dae5cd81SDave Chinner 	 */
1536dae5cd81SDave Chinner 	if (xfs_globals.mount_delay) {
1537dae5cd81SDave Chinner 		xfs_notice(mp, "Delaying mount for %d seconds.",
1538dae5cd81SDave Chinner 			xfs_globals.mount_delay);
1539dae5cd81SDave Chinner 		msleep(xfs_globals.mount_delay * 1000);
1540dae5cd81SDave Chinner 	}
1541dae5cd81SDave Chinner 
154273e5fff9SIan Kent 	if (fc->sb_flags & SB_SILENT)
1543c59d87c4SChristoph Hellwig 		flags |= XFS_MFSI_QUIET;
1544c59d87c4SChristoph Hellwig 
1545c59d87c4SChristoph Hellwig 	error = xfs_open_devices(mp);
1546c59d87c4SChristoph Hellwig 	if (error)
15472a9311adSChristoph Hellwig 		return error;
1548c59d87c4SChristoph Hellwig 
1549a76dba3bSDarrick J. Wong 	if (xfs_debugfs) {
1550a76dba3bSDarrick J. Wong 		mp->m_debugfs = xfs_debugfs_mkdir(mp->m_super->s_id,
1551a76dba3bSDarrick J. Wong 						  xfs_debugfs);
1552a76dba3bSDarrick J. Wong 	} else {
1553a76dba3bSDarrick J. Wong 		mp->m_debugfs = NULL;
1554a76dba3bSDarrick J. Wong 	}
1555a76dba3bSDarrick J. Wong 
15562451337dSDave Chinner 	error = xfs_init_mount_workqueues(mp);
1557c59d87c4SChristoph Hellwig 	if (error)
155835a93b14SChristoph Hellwig 		goto out_shutdown_devices;
1559c59d87c4SChristoph Hellwig 
15605681ca40SDave Chinner 	error = xfs_init_percpu_counters(mp);
1561aa6bf01dSChristoph Hellwig 	if (error)
1562aa6bf01dSChristoph Hellwig 		goto out_destroy_workqueues;
1563aa6bf01dSChristoph Hellwig 
1564ab23a776SDave Chinner 	error = xfs_inodegc_init_percpu(mp);
1565ab23a776SDave Chinner 	if (error)
1566ab23a776SDave Chinner 		goto out_destroy_counters;
1567ab23a776SDave Chinner 
1568225e4635SBill O'Donnell 	/* Allocate stats memory before we do operations that might use it */
1569225e4635SBill O'Donnell 	mp->m_stats.xs_stats = alloc_percpu(struct xfsstats);
1570225e4635SBill O'Donnell 	if (!mp->m_stats.xs_stats) {
1571f9d460b3SDan Carpenter 		error = -ENOMEM;
1572ab23a776SDave Chinner 		goto out_destroy_inodegc;
1573225e4635SBill O'Donnell 	}
1574225e4635SBill O'Donnell 
1575d7a74cadSDarrick J. Wong 	error = xchk_mount_stats_alloc(mp);
1576c59d87c4SChristoph Hellwig 	if (error)
1577225e4635SBill O'Donnell 		goto out_free_stats;
1578c59d87c4SChristoph Hellwig 
1579d7a74cadSDarrick J. Wong 	error = xfs_readsb(mp, flags);
1580d7a74cadSDarrick J. Wong 	if (error)
1581d7a74cadSDarrick J. Wong 		goto out_free_scrub_stats;
1582d7a74cadSDarrick J. Wong 
1583c59d87c4SChristoph Hellwig 	error = xfs_finish_flags(mp);
1584c59d87c4SChristoph Hellwig 	if (error)
1585c59d87c4SChristoph Hellwig 		goto out_free_sb;
1586c59d87c4SChristoph Hellwig 
1587c59d87c4SChristoph Hellwig 	error = xfs_setup_devices(mp);
1588c59d87c4SChristoph Hellwig 	if (error)
1589c59d87c4SChristoph Hellwig 		goto out_free_sb;
1590c59d87c4SChristoph Hellwig 
1591b96cb835SDarrick J. Wong 	/* V4 support is undergoing deprecation. */
159238c26bfdSDave Chinner 	if (!xfs_has_crc(mp)) {
1593b96cb835SDarrick J. Wong #ifdef CONFIG_XFS_SUPPORT_V4
1594b96cb835SDarrick J. Wong 		xfs_warn_once(mp,
1595b96cb835SDarrick J. Wong 	"Deprecated V4 format (crc=0) will not be supported after September 2030.");
1596b96cb835SDarrick J. Wong #else
1597b96cb835SDarrick J. Wong 		xfs_warn(mp,
1598b96cb835SDarrick J. Wong 	"Deprecated V4 format (crc=0) not supported by kernel.");
1599b96cb835SDarrick J. Wong 		error = -EINVAL;
1600b96cb835SDarrick J. Wong 		goto out_free_sb;
1601b96cb835SDarrick J. Wong #endif
1602b96cb835SDarrick J. Wong 	}
1603b96cb835SDarrick J. Wong 
16047ba83850SDarrick J. Wong 	/* ASCII case insensitivity is undergoing deprecation. */
16057ba83850SDarrick J. Wong 	if (xfs_has_asciici(mp)) {
16067ba83850SDarrick J. Wong #ifdef CONFIG_XFS_SUPPORT_ASCII_CI
16077ba83850SDarrick J. Wong 		xfs_warn_once(mp,
16087ba83850SDarrick J. Wong 	"Deprecated ASCII case-insensitivity feature (ascii-ci=1) will not be supported after September 2030.");
16097ba83850SDarrick J. Wong #else
16107ba83850SDarrick J. Wong 		xfs_warn(mp,
16117ba83850SDarrick J. Wong 	"Deprecated ASCII case-insensitivity feature (ascii-ci=1) not supported by kernel.");
16127ba83850SDarrick J. Wong 		error = -EINVAL;
16137ba83850SDarrick J. Wong 		goto out_free_sb;
16147ba83850SDarrick J. Wong #endif
16157ba83850SDarrick J. Wong 	}
16167ba83850SDarrick J. Wong 
161780c720b8SDarrick J. Wong 	/* Filesystem claims it needs repair, so refuse the mount. */
1618ebd9027dSDave Chinner 	if (xfs_has_needsrepair(mp)) {
161980c720b8SDarrick J. Wong 		xfs_warn(mp, "Filesystem needs repair.  Please run xfs_repair.");
162080c720b8SDarrick J. Wong 		error = -EFSCORRUPTED;
162180c720b8SDarrick J. Wong 		goto out_free_sb;
162280c720b8SDarrick J. Wong 	}
162380c720b8SDarrick J. Wong 
1624932befe3SDarrick J. Wong 	/*
16253945ae03SDarrick J. Wong 	 * Don't touch the filesystem if a user tool thinks it owns the primary
16263945ae03SDarrick J. Wong 	 * superblock.  mkfs doesn't clear the flag from secondary supers, so
16273945ae03SDarrick J. Wong 	 * we don't check them at all.
16283945ae03SDarrick J. Wong 	 */
16293945ae03SDarrick J. Wong 	if (mp->m_sb.sb_inprogress) {
16303945ae03SDarrick J. Wong 		xfs_warn(mp, "Offline file system operation in progress!");
16313945ae03SDarrick J. Wong 		error = -EFSCORRUPTED;
16323945ae03SDarrick J. Wong 		goto out_free_sb;
16333945ae03SDarrick J. Wong 	}
16343945ae03SDarrick J. Wong 
16353945ae03SDarrick J. Wong 	/*
16363945ae03SDarrick J. Wong 	 * Until this is fixed only page-sized or smaller data blocks work.
16373945ae03SDarrick J. Wong 	 */
16383945ae03SDarrick J. Wong 	if (mp->m_sb.sb_blocksize > PAGE_SIZE) {
16393945ae03SDarrick J. Wong 		xfs_warn(mp,
16403945ae03SDarrick J. Wong 		"File system with blocksize %d bytes. "
16413945ae03SDarrick J. Wong 		"Only pagesize (%ld) or less will currently work.",
16423945ae03SDarrick J. Wong 				mp->m_sb.sb_blocksize, PAGE_SIZE);
16433945ae03SDarrick J. Wong 		error = -ENOSYS;
16443945ae03SDarrick J. Wong 		goto out_free_sb;
16453945ae03SDarrick J. Wong 	}
16463945ae03SDarrick J. Wong 
16473945ae03SDarrick J. Wong 	/* Ensure this filesystem fits in the page cache limits */
16483945ae03SDarrick J. Wong 	if (xfs_sb_validate_fsb_count(&mp->m_sb, mp->m_sb.sb_dblocks) ||
16493945ae03SDarrick J. Wong 	    xfs_sb_validate_fsb_count(&mp->m_sb, mp->m_sb.sb_rblocks)) {
16503945ae03SDarrick J. Wong 		xfs_warn(mp,
16513945ae03SDarrick J. Wong 		"file system too large to be mounted on this system.");
16523945ae03SDarrick J. Wong 		error = -EFBIG;
16533945ae03SDarrick J. Wong 		goto out_free_sb;
16543945ae03SDarrick J. Wong 	}
16553945ae03SDarrick J. Wong 
16563945ae03SDarrick J. Wong 	/*
1657932befe3SDarrick J. Wong 	 * XFS block mappings use 54 bits to store the logical block offset.
1658932befe3SDarrick J. Wong 	 * This should suffice to handle the maximum file size that the VFS
1659932befe3SDarrick J. Wong 	 * supports (currently 2^63 bytes on 64-bit and ULONG_MAX << PAGE_SHIFT
1660932befe3SDarrick J. Wong 	 * bytes on 32-bit), but as XFS and VFS have gotten the s_maxbytes
1661932befe3SDarrick J. Wong 	 * calculation wrong on 32-bit kernels in the past, we'll add a WARN_ON
1662932befe3SDarrick J. Wong 	 * to check this assertion.
1663932befe3SDarrick J. Wong 	 *
1664932befe3SDarrick J. Wong 	 * Avoid integer overflow by comparing the maximum bmbt offset to the
1665932befe3SDarrick J. Wong 	 * maximum pagecache offset in units of fs blocks.
1666932befe3SDarrick J. Wong 	 */
166733005fd0SDarrick J. Wong 	if (!xfs_verify_fileoff(mp, XFS_B_TO_FSBT(mp, MAX_LFS_FILESIZE))) {
1668932befe3SDarrick J. Wong 		xfs_warn(mp,
1669932befe3SDarrick J. Wong "MAX_LFS_FILESIZE block offset (%llu) exceeds extent map maximum (%llu)!",
1670932befe3SDarrick J. Wong 			 XFS_B_TO_FSBT(mp, MAX_LFS_FILESIZE),
1671932befe3SDarrick J. Wong 			 XFS_MAX_FILEOFF);
1672932befe3SDarrick J. Wong 		error = -EINVAL;
1673932befe3SDarrick J. Wong 		goto out_free_sb;
1674932befe3SDarrick J. Wong 	}
1675932befe3SDarrick J. Wong 
1676c59d87c4SChristoph Hellwig 	error = xfs_filestream_mount(mp);
1677c59d87c4SChristoph Hellwig 	if (error)
1678c59d87c4SChristoph Hellwig 		goto out_free_sb;
1679c59d87c4SChristoph Hellwig 
1680c59d87c4SChristoph Hellwig 	/*
1681c59d87c4SChristoph Hellwig 	 * we must configure the block size in the superblock before we run the
1682c59d87c4SChristoph Hellwig 	 * full mount process as the mount process can lookup and cache inodes.
1683c59d87c4SChristoph Hellwig 	 */
1684dddde68bSAdam Borowski 	sb->s_magic = XFS_SUPER_MAGIC;
1685c59d87c4SChristoph Hellwig 	sb->s_blocksize = mp->m_sb.sb_blocksize;
1686c59d87c4SChristoph Hellwig 	sb->s_blocksize_bits = ffs(sb->s_blocksize) - 1;
1687932befe3SDarrick J. Wong 	sb->s_maxbytes = MAX_LFS_FILESIZE;
16888de52778SAl Viro 	sb->s_max_links = XFS_MAXLINK;
1689c59d87c4SChristoph Hellwig 	sb->s_time_gran = 1;
169038c26bfdSDave Chinner 	if (xfs_has_bigtime(mp)) {
1691f93e5436SDarrick J. Wong 		sb->s_time_min = xfs_bigtime_to_unix(XFS_BIGTIME_TIME_MIN);
1692f93e5436SDarrick J. Wong 		sb->s_time_max = xfs_bigtime_to_unix(XFS_BIGTIME_TIME_MAX);
1693f93e5436SDarrick J. Wong 	} else {
1694876fdc7cSDarrick J. Wong 		sb->s_time_min = XFS_LEGACY_TIME_MIN;
1695876fdc7cSDarrick J. Wong 		sb->s_time_max = XFS_LEGACY_TIME_MAX;
1696f93e5436SDarrick J. Wong 	}
169706dbf82bSDarrick J. Wong 	trace_xfs_inode_timestamp_range(mp, sb->s_time_min, sb->s_time_max);
1698adfb5fb4SChristoph Hellwig 	sb->s_iflags |= SB_I_CGROUPWB;
1699adfb5fb4SChristoph Hellwig 
1700c59d87c4SChristoph Hellwig 	set_posix_acl_flag(sb);
1701c59d87c4SChristoph Hellwig 
1702dc037ad7SDave Chinner 	/* version 5 superblocks support inode version counters. */
1703d6837c1aSDave Chinner 	if (xfs_has_crc(mp))
1704357fdad0SMatthew Garrett 		sb->s_flags |= SB_I_VERSION;
1705dc037ad7SDave Chinner 
17060560f31aSDave Chinner 	if (xfs_has_dax_always(mp)) {
1707679a9949SChristoph Hellwig 		error = xfs_setup_dax_always(mp);
1708679a9949SChristoph Hellwig 		if (error)
1709b6e03c10SDarrick J. Wong 			goto out_filestream_unmount;
1710b6e03c10SDarrick J. Wong 	}
1711cbe4dab1SDave Chinner 
171270200574SChristoph Hellwig 	if (xfs_has_discard(mp) && !bdev_max_discard_sectors(sb->s_bdev)) {
171370200574SChristoph Hellwig 		xfs_warn(mp,
171470200574SChristoph Hellwig 	"mounting with \"discard\" option, but the device does not support discard");
17150560f31aSDave Chinner 		mp->m_features &= ~XFS_FEAT_DISCARD;
17161e6fa688SKenjiro Nakayama 	}
17171e6fa688SKenjiro Nakayama 
171838c26bfdSDave Chinner 	if (xfs_has_reflink(mp)) {
171966ae56a5SChristoph Hellwig 		if (mp->m_sb.sb_rblocks) {
1720c14632ddSDarrick J. Wong 			xfs_alert(mp,
1721c14632ddSDarrick J. Wong 	"reflink not compatible with realtime device!");
1722c14632ddSDarrick J. Wong 			error = -EINVAL;
1723c14632ddSDarrick J. Wong 			goto out_filestream_unmount;
1724c14632ddSDarrick J. Wong 		}
1725c14632ddSDarrick J. Wong 
172666ae56a5SChristoph Hellwig 		if (xfs_globals.always_cow) {
172766ae56a5SChristoph Hellwig 			xfs_info(mp, "using DEBUG-only always_cow mode.");
172866ae56a5SChristoph Hellwig 			mp->m_always_cow = true;
172966ae56a5SChristoph Hellwig 		}
173066ae56a5SChristoph Hellwig 	}
173166ae56a5SChristoph Hellwig 
173238c26bfdSDave Chinner 	if (xfs_has_rmapbt(mp) && mp->m_sb.sb_rblocks) {
1733738f57c1SDarrick J. Wong 		xfs_alert(mp,
173476883f79SDarrick J. Wong 	"reverse mapping btree not compatible with realtime device!");
1735738f57c1SDarrick J. Wong 		error = -EINVAL;
1736738f57c1SDarrick J. Wong 		goto out_filestream_unmount;
1737738f57c1SDarrick J. Wong 	}
17381c0607acSDarrick J. Wong 
17398a00ebe4SDave Chinner 	error = xfs_mountfs(mp);
1740c59d87c4SChristoph Hellwig 	if (error)
17417e18530bSDave Chinner 		goto out_filestream_unmount;
1742c59d87c4SChristoph Hellwig 
1743c59d87c4SChristoph Hellwig 	root = igrab(VFS_I(mp->m_rootip));
1744c59d87c4SChristoph Hellwig 	if (!root) {
17452451337dSDave Chinner 		error = -ENOENT;
17468a00ebe4SDave Chinner 		goto out_unmount;
1747c59d87c4SChristoph Hellwig 	}
174848fde701SAl Viro 	sb->s_root = d_make_root(root);
1749c59d87c4SChristoph Hellwig 	if (!sb->s_root) {
17502451337dSDave Chinner 		error = -ENOMEM;
17518a00ebe4SDave Chinner 		goto out_unmount;
1752c59d87c4SChristoph Hellwig 	}
1753c59d87c4SChristoph Hellwig 
17547e18530bSDave Chinner 	return 0;
17557e18530bSDave Chinner 
17567e18530bSDave Chinner  out_filestream_unmount:
1757c59d87c4SChristoph Hellwig 	xfs_filestream_unmount(mp);
1758c59d87c4SChristoph Hellwig  out_free_sb:
1759c59d87c4SChristoph Hellwig 	xfs_freesb(mp);
1760d7a74cadSDarrick J. Wong  out_free_scrub_stats:
1761d7a74cadSDarrick J. Wong 	xchk_mount_stats_free(mp);
1762225e4635SBill O'Donnell  out_free_stats:
1763225e4635SBill O'Donnell 	free_percpu(mp->m_stats.xs_stats);
1764ab23a776SDave Chinner  out_destroy_inodegc:
1765ab23a776SDave Chinner 	xfs_inodegc_free_percpu(mp);
1766c59d87c4SChristoph Hellwig  out_destroy_counters:
17675681ca40SDave Chinner 	xfs_destroy_percpu_counters(mp);
1768aa6bf01dSChristoph Hellwig  out_destroy_workqueues:
1769aa6bf01dSChristoph Hellwig 	xfs_destroy_mount_workqueues(mp);
177035a93b14SChristoph Hellwig  out_shutdown_devices:
177135a93b14SChristoph Hellwig 	xfs_shutdown_devices(mp);
17722451337dSDave Chinner 	return error;
1773c59d87c4SChristoph Hellwig 
1774c59d87c4SChristoph Hellwig  out_unmount:
1775c59d87c4SChristoph Hellwig 	xfs_filestream_unmount(mp);
1776c59d87c4SChristoph Hellwig 	xfs_unmountfs(mp);
1777c59d87c4SChristoph Hellwig 	goto out_free_sb;
1778c59d87c4SChristoph Hellwig }
1779c59d87c4SChristoph Hellwig 
178073e5fff9SIan Kent static int
xfs_fs_get_tree(struct fs_context * fc)17811e5c39dfSDarrick J. Wong xfs_fs_get_tree(
178273e5fff9SIan Kent 	struct fs_context	*fc)
178373e5fff9SIan Kent {
17841e5c39dfSDarrick J. Wong 	return get_tree_bdev(fc, xfs_fs_fill_super);
178573e5fff9SIan Kent }
178673e5fff9SIan Kent 
178763cd1e9bSIan Kent static int
xfs_remount_rw(struct xfs_mount * mp)178863cd1e9bSIan Kent xfs_remount_rw(
178963cd1e9bSIan Kent 	struct xfs_mount	*mp)
179063cd1e9bSIan Kent {
179163cd1e9bSIan Kent 	struct xfs_sb		*sbp = &mp->m_sb;
179263cd1e9bSIan Kent 	int error;
179363cd1e9bSIan Kent 
17940560f31aSDave Chinner 	if (xfs_has_norecovery(mp)) {
179563cd1e9bSIan Kent 		xfs_warn(mp,
179663cd1e9bSIan Kent 			"ro->rw transition prohibited on norecovery mount");
179763cd1e9bSIan Kent 		return -EINVAL;
179863cd1e9bSIan Kent 	}
179963cd1e9bSIan Kent 
1800d6837c1aSDave Chinner 	if (xfs_sb_is_v5(sbp) &&
180163cd1e9bSIan Kent 	    xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
180263cd1e9bSIan Kent 		xfs_warn(mp,
180363cd1e9bSIan Kent 	"ro->rw transition prohibited on unknown (0x%x) ro-compat filesystem",
180463cd1e9bSIan Kent 			(sbp->sb_features_ro_compat &
180563cd1e9bSIan Kent 				XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
180663cd1e9bSIan Kent 		return -EINVAL;
180763cd1e9bSIan Kent 	}
180863cd1e9bSIan Kent 
18092e973b2cSDave Chinner 	clear_bit(XFS_OPSTATE_READONLY, &mp->m_opstate);
181063cd1e9bSIan Kent 
181163cd1e9bSIan Kent 	/*
181263cd1e9bSIan Kent 	 * If this is the first remount to writeable state we might have some
181363cd1e9bSIan Kent 	 * superblock changes to update.
181463cd1e9bSIan Kent 	 */
181563cd1e9bSIan Kent 	if (mp->m_update_sb) {
181663cd1e9bSIan Kent 		error = xfs_sync_sb(mp, false);
181763cd1e9bSIan Kent 		if (error) {
181863cd1e9bSIan Kent 			xfs_warn(mp, "failed to write sb changes");
181963cd1e9bSIan Kent 			return error;
182063cd1e9bSIan Kent 		}
182163cd1e9bSIan Kent 		mp->m_update_sb = false;
182263cd1e9bSIan Kent 	}
182363cd1e9bSIan Kent 
182463cd1e9bSIan Kent 	/*
182563cd1e9bSIan Kent 	 * Fill out the reserve pool if it is empty. Use the stashed value if
182663cd1e9bSIan Kent 	 * it is non-zero, otherwise go with the default.
182763cd1e9bSIan Kent 	 */
182863cd1e9bSIan Kent 	xfs_restore_resvblks(mp);
182963cd1e9bSIan Kent 	xfs_log_work_queue(mp);
1830c9a6526fSDarrick J. Wong 	xfs_blockgc_start(mp);
183163cd1e9bSIan Kent 
183263cd1e9bSIan Kent 	/* Create the per-AG metadata reservation pool .*/
183363cd1e9bSIan Kent 	error = xfs_fs_reserve_ag_blocks(mp);
183463cd1e9bSIan Kent 	if (error && error != -ENOSPC)
183563cd1e9bSIan Kent 		return error;
183663cd1e9bSIan Kent 
1837ab23a776SDave Chinner 	/* Re-enable the background inode inactivation worker. */
1838ab23a776SDave Chinner 	xfs_inodegc_start(mp);
1839ab23a776SDave Chinner 
184063cd1e9bSIan Kent 	return 0;
184163cd1e9bSIan Kent }
184263cd1e9bSIan Kent 
184363cd1e9bSIan Kent static int
xfs_remount_ro(struct xfs_mount * mp)184463cd1e9bSIan Kent xfs_remount_ro(
184563cd1e9bSIan Kent 	struct xfs_mount	*mp)
184663cd1e9bSIan Kent {
1847089558bcSDarrick J. Wong 	struct xfs_icwalk	icw = {
1848089558bcSDarrick J. Wong 		.icw_flags	= XFS_ICWALK_FLAG_SYNC,
1849089558bcSDarrick J. Wong 	};
185063cd1e9bSIan Kent 	int			error;
185163cd1e9bSIan Kent 
1852b97cca3bSDarrick J. Wong 	/* Flush all the dirty data to disk. */
1853b97cca3bSDarrick J. Wong 	error = sync_filesystem(mp->m_super);
1854b97cca3bSDarrick J. Wong 	if (error)
1855b97cca3bSDarrick J. Wong 		return error;
1856b97cca3bSDarrick J. Wong 
185763cd1e9bSIan Kent 	/*
185863cd1e9bSIan Kent 	 * Cancel background eofb scanning so it cannot race with the final
185963cd1e9bSIan Kent 	 * log force+buftarg wait and deadlock the remount.
186063cd1e9bSIan Kent 	 */
1861c9a6526fSDarrick J. Wong 	xfs_blockgc_stop(mp);
186263cd1e9bSIan Kent 
1863089558bcSDarrick J. Wong 	/*
1864089558bcSDarrick J. Wong 	 * Clear out all remaining COW staging extents and speculative post-EOF
1865089558bcSDarrick J. Wong 	 * preallocations so that we don't leave inodes requiring inactivation
1866089558bcSDarrick J. Wong 	 * cleanups during reclaim on a read-only mount.  We must process every
1867089558bcSDarrick J. Wong 	 * cached inode, so this requires a synchronous cache scan.
1868089558bcSDarrick J. Wong 	 */
1869089558bcSDarrick J. Wong 	error = xfs_blockgc_free_space(mp, &icw);
187063cd1e9bSIan Kent 	if (error) {
187163cd1e9bSIan Kent 		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
187263cd1e9bSIan Kent 		return error;
187363cd1e9bSIan Kent 	}
187463cd1e9bSIan Kent 
1875ab23a776SDave Chinner 	/*
1876ab23a776SDave Chinner 	 * Stop the inodegc background worker.  xfs_fs_reconfigure already
1877ab23a776SDave Chinner 	 * flushed all pending inodegc work when it sync'd the filesystem.
1878ab23a776SDave Chinner 	 * The VFS holds s_umount, so we know that inodes cannot enter
1879ab23a776SDave Chinner 	 * xfs_fs_destroy_inode during a remount operation.  In readonly mode
1880ab23a776SDave Chinner 	 * we send inodes straight to reclaim, so no inodes will be queued.
1881ab23a776SDave Chinner 	 */
1882ab23a776SDave Chinner 	xfs_inodegc_stop(mp);
1883ab23a776SDave Chinner 
188463cd1e9bSIan Kent 	/* Free the per-AG metadata reservation pool. */
188563cd1e9bSIan Kent 	error = xfs_fs_unreserve_ag_blocks(mp);
188663cd1e9bSIan Kent 	if (error) {
188763cd1e9bSIan Kent 		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
188863cd1e9bSIan Kent 		return error;
188963cd1e9bSIan Kent 	}
189063cd1e9bSIan Kent 
189163cd1e9bSIan Kent 	/*
189263cd1e9bSIan Kent 	 * Before we sync the metadata, we need to free up the reserve block
189363cd1e9bSIan Kent 	 * pool so that the used block count in the superblock on disk is
189463cd1e9bSIan Kent 	 * correct at the end of the remount. Stash the current* reserve pool
189563cd1e9bSIan Kent 	 * size so that if we get remounted rw, we can return it to the same
189663cd1e9bSIan Kent 	 * size.
189763cd1e9bSIan Kent 	 */
189863cd1e9bSIan Kent 	xfs_save_resvblks(mp);
189963cd1e9bSIan Kent 
1900ea2064daSBrian Foster 	xfs_log_clean(mp);
19012e973b2cSDave Chinner 	set_bit(XFS_OPSTATE_READONLY, &mp->m_opstate);
190263cd1e9bSIan Kent 
190363cd1e9bSIan Kent 	return 0;
190463cd1e9bSIan Kent }
190563cd1e9bSIan Kent 
190663cd1e9bSIan Kent /*
190763cd1e9bSIan Kent  * Logically we would return an error here to prevent users from believing
190863cd1e9bSIan Kent  * they might have changed mount options using remount which can't be changed.
190963cd1e9bSIan Kent  *
191063cd1e9bSIan Kent  * But unfortunately mount(8) adds all options from mtab and fstab to the mount
191163cd1e9bSIan Kent  * arguments in some cases so we can't blindly reject options, but have to
191263cd1e9bSIan Kent  * check for each specified option if it actually differs from the currently
191363cd1e9bSIan Kent  * set option and only reject it if that's the case.
191463cd1e9bSIan Kent  *
191563cd1e9bSIan Kent  * Until that is implemented we return success for every remount request, and
191663cd1e9bSIan Kent  * silently ignore all options that we can't actually change.
191763cd1e9bSIan Kent  */
191863cd1e9bSIan Kent static int
xfs_fs_reconfigure(struct fs_context * fc)19191e5c39dfSDarrick J. Wong xfs_fs_reconfigure(
192063cd1e9bSIan Kent 	struct fs_context *fc)
192163cd1e9bSIan Kent {
192263cd1e9bSIan Kent 	struct xfs_mount	*mp = XFS_M(fc->root->d_sb);
192363cd1e9bSIan Kent 	struct xfs_mount        *new_mp = fc->s_fs_info;
192463cd1e9bSIan Kent 	int			flags = fc->sb_flags;
192563cd1e9bSIan Kent 	int			error;
192663cd1e9bSIan Kent 
19274750a171SEric Sandeen 	/* version 5 superblocks always support version counters. */
1928d6837c1aSDave Chinner 	if (xfs_has_crc(mp))
19294750a171SEric Sandeen 		fc->sb_flags |= SB_I_VERSION;
19304750a171SEric Sandeen 
19311e5c39dfSDarrick J. Wong 	error = xfs_fs_validate_params(new_mp);
193263cd1e9bSIan Kent 	if (error)
193363cd1e9bSIan Kent 		return error;
193463cd1e9bSIan Kent 
193563cd1e9bSIan Kent 	/* inode32 -> inode64 */
19360560f31aSDave Chinner 	if (xfs_has_small_inums(mp) && !xfs_has_small_inums(new_mp)) {
19370560f31aSDave Chinner 		mp->m_features &= ~XFS_FEAT_SMALL_INUMS;
1938d6837c1aSDave Chinner 		mp->m_maxagi = xfs_set_inode_alloc(mp, mp->m_sb.sb_agcount);
193963cd1e9bSIan Kent 	}
194063cd1e9bSIan Kent 
194163cd1e9bSIan Kent 	/* inode64 -> inode32 */
19420560f31aSDave Chinner 	if (!xfs_has_small_inums(mp) && xfs_has_small_inums(new_mp)) {
19430560f31aSDave Chinner 		mp->m_features |= XFS_FEAT_SMALL_INUMS;
1944d6837c1aSDave Chinner 		mp->m_maxagi = xfs_set_inode_alloc(mp, mp->m_sb.sb_agcount);
194563cd1e9bSIan Kent 	}
194663cd1e9bSIan Kent 
194763cd1e9bSIan Kent 	/* ro -> rw */
19482e973b2cSDave Chinner 	if (xfs_is_readonly(mp) && !(flags & SB_RDONLY)) {
194963cd1e9bSIan Kent 		error = xfs_remount_rw(mp);
195063cd1e9bSIan Kent 		if (error)
195163cd1e9bSIan Kent 			return error;
195263cd1e9bSIan Kent 	}
195363cd1e9bSIan Kent 
195463cd1e9bSIan Kent 	/* rw -> ro */
19552e973b2cSDave Chinner 	if (!xfs_is_readonly(mp) && (flags & SB_RDONLY)) {
195663cd1e9bSIan Kent 		error = xfs_remount_ro(mp);
195763cd1e9bSIan Kent 		if (error)
195863cd1e9bSIan Kent 			return error;
195963cd1e9bSIan Kent 	}
196063cd1e9bSIan Kent 
196163cd1e9bSIan Kent 	return 0;
196263cd1e9bSIan Kent }
196363cd1e9bSIan Kent 
1964dbbff489SChristoph Hellwig static void
xfs_fs_free(struct fs_context * fc)1965dbbff489SChristoph Hellwig xfs_fs_free(
196673e5fff9SIan Kent 	struct fs_context	*fc)
196773e5fff9SIan Kent {
196873e5fff9SIan Kent 	struct xfs_mount	*mp = fc->s_fs_info;
196973e5fff9SIan Kent 
197073e5fff9SIan Kent 	/*
197173e5fff9SIan Kent 	 * mp is stored in the fs_context when it is initialized.
197273e5fff9SIan Kent 	 * mp is transferred to the superblock on a successful mount,
197373e5fff9SIan Kent 	 * but if an error occurs before the transfer we have to free
197473e5fff9SIan Kent 	 * it here.
197573e5fff9SIan Kent 	 */
197673e5fff9SIan Kent 	if (mp)
197773e5fff9SIan Kent 		xfs_mount_free(mp);
197873e5fff9SIan Kent }
197973e5fff9SIan Kent 
198073e5fff9SIan Kent static const struct fs_context_operations xfs_context_ops = {
19811e5c39dfSDarrick J. Wong 	.parse_param = xfs_fs_parse_param,
19821e5c39dfSDarrick J. Wong 	.get_tree    = xfs_fs_get_tree,
19831e5c39dfSDarrick J. Wong 	.reconfigure = xfs_fs_reconfigure,
19841e5c39dfSDarrick J. Wong 	.free        = xfs_fs_free,
198573e5fff9SIan Kent };
198673e5fff9SIan Kent 
1987*a0ebcdabSDave Chinner /*
1988*a0ebcdabSDave Chinner  * WARNING: do not initialise any parameters in this function that depend on
1989*a0ebcdabSDave Chinner  * mount option parsing having already been performed as this can be called from
1990*a0ebcdabSDave Chinner  * fsopen() before any parameters have been set.
1991*a0ebcdabSDave Chinner  */
xfs_init_fs_context(struct fs_context * fc)199273e5fff9SIan Kent static int xfs_init_fs_context(
199373e5fff9SIan Kent 	struct fs_context	*fc)
199473e5fff9SIan Kent {
199573e5fff9SIan Kent 	struct xfs_mount	*mp;
199673e5fff9SIan Kent 
199750f83009SIan Kent 	mp = kmem_alloc(sizeof(struct xfs_mount), KM_ZERO);
199873e5fff9SIan Kent 	if (!mp)
199973e5fff9SIan Kent 		return -ENOMEM;
200073e5fff9SIan Kent 
200150f83009SIan Kent 	spin_lock_init(&mp->m_sb_lock);
200250f83009SIan Kent 	INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
200350f83009SIan Kent 	spin_lock_init(&mp->m_perag_lock);
200450f83009SIan Kent 	mutex_init(&mp->m_growlock);
2005f0f7a674SDarrick J. Wong 	INIT_WORK(&mp->m_flush_inodes_work, xfs_flush_inodes_worker);
200650f83009SIan Kent 	INIT_DELAYED_WORK(&mp->m_reclaim_work, xfs_reclaim_worker);
200750f83009SIan Kent 	mp->m_kobj.kobject.kset = xfs_kset;
200850f83009SIan Kent 	/*
200950f83009SIan Kent 	 * We don't create the finobt per-ag space reservation until after log
201050f83009SIan Kent 	 * recovery, so we must set this to true so that an ifree transaction
201150f83009SIan Kent 	 * started during log recovery will not depend on space reservations
201250f83009SIan Kent 	 * for finobt expansion.
201350f83009SIan Kent 	 */
201450f83009SIan Kent 	mp->m_finobt_nores = true;
201550f83009SIan Kent 
201673e5fff9SIan Kent 	/*
201773e5fff9SIan Kent 	 * These can be overridden by the mount option parsing.
201873e5fff9SIan Kent 	 */
201973e5fff9SIan Kent 	mp->m_logbufs = -1;
202073e5fff9SIan Kent 	mp->m_logbsize = -1;
202173e5fff9SIan Kent 	mp->m_allocsize_log = 16; /* 64k */
202273e5fff9SIan Kent 
202373e5fff9SIan Kent 	fc->s_fs_info = mp;
202473e5fff9SIan Kent 	fc->ops = &xfs_context_ops;
202573e5fff9SIan Kent 
202673e5fff9SIan Kent 	return 0;
202773e5fff9SIan Kent }
202873e5fff9SIan Kent 
20292a9311adSChristoph Hellwig static void
xfs_kill_sb(struct super_block * sb)20302a9311adSChristoph Hellwig xfs_kill_sb(
20312a9311adSChristoph Hellwig 	struct super_block		*sb)
20322a9311adSChristoph Hellwig {
20332a9311adSChristoph Hellwig 	kill_block_super(sb);
20342a9311adSChristoph Hellwig 	xfs_mount_free(XFS_M(sb));
20352a9311adSChristoph Hellwig }
20362a9311adSChristoph Hellwig 
2037c59d87c4SChristoph Hellwig static struct file_system_type xfs_fs_type = {
2038c59d87c4SChristoph Hellwig 	.owner			= THIS_MODULE,
2039c59d87c4SChristoph Hellwig 	.name			= "xfs",
204073e5fff9SIan Kent 	.init_fs_context	= xfs_init_fs_context,
2041d7167b14SAl Viro 	.parameters		= xfs_fs_parameters,
20422a9311adSChristoph Hellwig 	.kill_sb		= xfs_kill_sb,
2043f798accdSChristian Brauner 	.fs_flags		= FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
2044c59d87c4SChristoph Hellwig };
20457f78e035SEric W. Biederman MODULE_ALIAS_FS("xfs");
2046c59d87c4SChristoph Hellwig 
2047c59d87c4SChristoph Hellwig STATIC int __init
xfs_init_caches(void)2048182696fbSDarrick J. Wong xfs_init_caches(void)
2049c59d87c4SChristoph Hellwig {
20509fa47bdcSDarrick J. Wong 	int		error;
20519fa47bdcSDarrick J. Wong 
2052231f91abSDave Chinner 	xfs_buf_cache = kmem_cache_create("xfs_buf", sizeof(struct xfs_buf), 0,
2053231f91abSDave Chinner 					 SLAB_HWCACHE_ALIGN |
2054231f91abSDave Chinner 					 SLAB_RECLAIM_ACCOUNT |
2055231f91abSDave Chinner 					 SLAB_MEM_SPREAD,
2056231f91abSDave Chinner 					 NULL);
2057231f91abSDave Chinner 	if (!xfs_buf_cache)
2058231f91abSDave Chinner 		goto out;
2059231f91abSDave Chinner 
2060182696fbSDarrick J. Wong 	xfs_log_ticket_cache = kmem_cache_create("xfs_log_ticket",
2061b1231760SCarlos Maiolino 						sizeof(struct xlog_ticket),
2062b1231760SCarlos Maiolino 						0, 0, NULL);
2063182696fbSDarrick J. Wong 	if (!xfs_log_ticket_cache)
2064231f91abSDave Chinner 		goto out_destroy_buf_cache;
2065c59d87c4SChristoph Hellwig 
20669fa47bdcSDarrick J. Wong 	error = xfs_btree_init_cur_caches();
20679fa47bdcSDarrick J. Wong 	if (error)
2068c201d9caSDarrick J. Wong 		goto out_destroy_log_ticket_cache;
2069c59d87c4SChristoph Hellwig 
2070f3c799c2SDarrick J. Wong 	error = xfs_defer_init_item_caches();
2071f3c799c2SDarrick J. Wong 	if (error)
2072f3c799c2SDarrick J. Wong 		goto out_destroy_btree_cur_cache;
2073f3c799c2SDarrick J. Wong 
2074182696fbSDarrick J. Wong 	xfs_da_state_cache = kmem_cache_create("xfs_da_state",
2075b1231760SCarlos Maiolino 					      sizeof(struct xfs_da_state),
2076b1231760SCarlos Maiolino 					      0, 0, NULL);
2077182696fbSDarrick J. Wong 	if (!xfs_da_state_cache)
2078f3c799c2SDarrick J. Wong 		goto out_destroy_defer_item_cache;
2079c59d87c4SChristoph Hellwig 
2080182696fbSDarrick J. Wong 	xfs_ifork_cache = kmem_cache_create("xfs_ifork",
2081b1231760SCarlos Maiolino 					   sizeof(struct xfs_ifork),
2082b1231760SCarlos Maiolino 					   0, 0, NULL);
2083182696fbSDarrick J. Wong 	if (!xfs_ifork_cache)
2084182696fbSDarrick J. Wong 		goto out_destroy_da_state_cache;
2085c59d87c4SChristoph Hellwig 
2086182696fbSDarrick J. Wong 	xfs_trans_cache = kmem_cache_create("xfs_trans",
2087b1231760SCarlos Maiolino 					   sizeof(struct xfs_trans),
2088b1231760SCarlos Maiolino 					   0, 0, NULL);
2089182696fbSDarrick J. Wong 	if (!xfs_trans_cache)
2090182696fbSDarrick J. Wong 		goto out_destroy_ifork_cache;
2091c59d87c4SChristoph Hellwig 
2092c59d87c4SChristoph Hellwig 
2093c59d87c4SChristoph Hellwig 	/*
2094182696fbSDarrick J. Wong 	 * The size of the cache-allocated buf log item is the maximum
2095c59d87c4SChristoph Hellwig 	 * size possible under XFS.  This wastes a little bit of memory,
2096c59d87c4SChristoph Hellwig 	 * but it is much faster.
2097c59d87c4SChristoph Hellwig 	 */
2098182696fbSDarrick J. Wong 	xfs_buf_item_cache = kmem_cache_create("xfs_buf_item",
2099b1231760SCarlos Maiolino 					      sizeof(struct xfs_buf_log_item),
2100b1231760SCarlos Maiolino 					      0, 0, NULL);
2101182696fbSDarrick J. Wong 	if (!xfs_buf_item_cache)
2102182696fbSDarrick J. Wong 		goto out_destroy_trans_cache;
2103c59d87c4SChristoph Hellwig 
2104182696fbSDarrick J. Wong 	xfs_efd_cache = kmem_cache_create("xfs_efd_item",
21053c5aaaceSDarrick J. Wong 			xfs_efd_log_item_sizeof(XFS_EFD_MAX_FAST_EXTENTS),
2106b1231760SCarlos Maiolino 			0, 0, NULL);
2107182696fbSDarrick J. Wong 	if (!xfs_efd_cache)
2108182696fbSDarrick J. Wong 		goto out_destroy_buf_item_cache;
2109c59d87c4SChristoph Hellwig 
2110182696fbSDarrick J. Wong 	xfs_efi_cache = kmem_cache_create("xfs_efi_item",
21113c5aaaceSDarrick J. Wong 			xfs_efi_log_item_sizeof(XFS_EFI_MAX_FAST_EXTENTS),
2112b1231760SCarlos Maiolino 			0, 0, NULL);
2113182696fbSDarrick J. Wong 	if (!xfs_efi_cache)
2114182696fbSDarrick J. Wong 		goto out_destroy_efd_cache;
2115c59d87c4SChristoph Hellwig 
2116182696fbSDarrick J. Wong 	xfs_inode_cache = kmem_cache_create("xfs_inode",
2117b1231760SCarlos Maiolino 					   sizeof(struct xfs_inode), 0,
2118b1231760SCarlos Maiolino 					   (SLAB_HWCACHE_ALIGN |
2119b1231760SCarlos Maiolino 					    SLAB_RECLAIM_ACCOUNT |
2120b1231760SCarlos Maiolino 					    SLAB_MEM_SPREAD | SLAB_ACCOUNT),
2121b1231760SCarlos Maiolino 					   xfs_fs_inode_init_once);
2122182696fbSDarrick J. Wong 	if (!xfs_inode_cache)
2123182696fbSDarrick J. Wong 		goto out_destroy_efi_cache;
2124c59d87c4SChristoph Hellwig 
2125182696fbSDarrick J. Wong 	xfs_ili_cache = kmem_cache_create("xfs_ili",
2126b1231760SCarlos Maiolino 					 sizeof(struct xfs_inode_log_item), 0,
2127d59eadaeSDave Chinner 					 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
2128d59eadaeSDave Chinner 					 NULL);
2129182696fbSDarrick J. Wong 	if (!xfs_ili_cache)
2130182696fbSDarrick J. Wong 		goto out_destroy_inode_cache;
2131b1231760SCarlos Maiolino 
2132182696fbSDarrick J. Wong 	xfs_icreate_cache = kmem_cache_create("xfs_icr",
2133b1231760SCarlos Maiolino 					     sizeof(struct xfs_icreate_item),
2134b1231760SCarlos Maiolino 					     0, 0, NULL);
2135182696fbSDarrick J. Wong 	if (!xfs_icreate_cache)
2136182696fbSDarrick J. Wong 		goto out_destroy_ili_cache;
2137c59d87c4SChristoph Hellwig 
2138182696fbSDarrick J. Wong 	xfs_rud_cache = kmem_cache_create("xfs_rud_item",
2139b1231760SCarlos Maiolino 					 sizeof(struct xfs_rud_log_item),
2140b1231760SCarlos Maiolino 					 0, 0, NULL);
2141182696fbSDarrick J. Wong 	if (!xfs_rud_cache)
2142182696fbSDarrick J. Wong 		goto out_destroy_icreate_cache;
21435880f2d7SDarrick J. Wong 
2144182696fbSDarrick J. Wong 	xfs_rui_cache = kmem_cache_create("xfs_rui_item",
2145cd00158cSDarrick J. Wong 			xfs_rui_log_item_sizeof(XFS_RUI_MAX_FAST_EXTENTS),
2146b1231760SCarlos Maiolino 			0, 0, NULL);
2147182696fbSDarrick J. Wong 	if (!xfs_rui_cache)
2148182696fbSDarrick J. Wong 		goto out_destroy_rud_cache;
21495880f2d7SDarrick J. Wong 
2150182696fbSDarrick J. Wong 	xfs_cud_cache = kmem_cache_create("xfs_cud_item",
2151b1231760SCarlos Maiolino 					 sizeof(struct xfs_cud_log_item),
2152b1231760SCarlos Maiolino 					 0, 0, NULL);
2153182696fbSDarrick J. Wong 	if (!xfs_cud_cache)
2154182696fbSDarrick J. Wong 		goto out_destroy_rui_cache;
2155baf4bcacSDarrick J. Wong 
2156182696fbSDarrick J. Wong 	xfs_cui_cache = kmem_cache_create("xfs_cui_item",
2157baf4bcacSDarrick J. Wong 			xfs_cui_log_item_sizeof(XFS_CUI_MAX_FAST_EXTENTS),
2158b1231760SCarlos Maiolino 			0, 0, NULL);
2159182696fbSDarrick J. Wong 	if (!xfs_cui_cache)
2160182696fbSDarrick J. Wong 		goto out_destroy_cud_cache;
2161baf4bcacSDarrick J. Wong 
2162182696fbSDarrick J. Wong 	xfs_bud_cache = kmem_cache_create("xfs_bud_item",
2163b1231760SCarlos Maiolino 					 sizeof(struct xfs_bud_log_item),
2164b1231760SCarlos Maiolino 					 0, 0, NULL);
2165182696fbSDarrick J. Wong 	if (!xfs_bud_cache)
2166182696fbSDarrick J. Wong 		goto out_destroy_cui_cache;
21676413a014SDarrick J. Wong 
2168182696fbSDarrick J. Wong 	xfs_bui_cache = kmem_cache_create("xfs_bui_item",
21696413a014SDarrick J. Wong 			xfs_bui_log_item_sizeof(XFS_BUI_MAX_FAST_EXTENTS),
2170b1231760SCarlos Maiolino 			0, 0, NULL);
2171182696fbSDarrick J. Wong 	if (!xfs_bui_cache)
2172182696fbSDarrick J. Wong 		goto out_destroy_bud_cache;
21736413a014SDarrick J. Wong 
21744136e38aSDarrick J. Wong 	xfs_attrd_cache = kmem_cache_create("xfs_attrd_item",
21754136e38aSDarrick J. Wong 					    sizeof(struct xfs_attrd_log_item),
21764136e38aSDarrick J. Wong 					    0, 0, NULL);
21774136e38aSDarrick J. Wong 	if (!xfs_attrd_cache)
21784136e38aSDarrick J. Wong 		goto out_destroy_bui_cache;
21794136e38aSDarrick J. Wong 
21804136e38aSDarrick J. Wong 	xfs_attri_cache = kmem_cache_create("xfs_attri_item",
21814136e38aSDarrick J. Wong 					    sizeof(struct xfs_attri_log_item),
21824136e38aSDarrick J. Wong 					    0, 0, NULL);
21834136e38aSDarrick J. Wong 	if (!xfs_attri_cache)
21844136e38aSDarrick J. Wong 		goto out_destroy_attrd_cache;
21854136e38aSDarrick J. Wong 
2186784eb7d8SDave Chinner 	xfs_iunlink_cache = kmem_cache_create("xfs_iul_item",
2187784eb7d8SDave Chinner 					     sizeof(struct xfs_iunlink_item),
2188784eb7d8SDave Chinner 					     0, 0, NULL);
2189784eb7d8SDave Chinner 	if (!xfs_iunlink_cache)
2190784eb7d8SDave Chinner 		goto out_destroy_attri_cache;
2191784eb7d8SDave Chinner 
2192c59d87c4SChristoph Hellwig 	return 0;
2193c59d87c4SChristoph Hellwig 
2194784eb7d8SDave Chinner  out_destroy_attri_cache:
2195784eb7d8SDave Chinner 	kmem_cache_destroy(xfs_attri_cache);
21964136e38aSDarrick J. Wong  out_destroy_attrd_cache:
21974136e38aSDarrick J. Wong 	kmem_cache_destroy(xfs_attrd_cache);
21984136e38aSDarrick J. Wong  out_destroy_bui_cache:
21994136e38aSDarrick J. Wong 	kmem_cache_destroy(xfs_bui_cache);
2200182696fbSDarrick J. Wong  out_destroy_bud_cache:
2201182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_bud_cache);
2202182696fbSDarrick J. Wong  out_destroy_cui_cache:
2203182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_cui_cache);
2204182696fbSDarrick J. Wong  out_destroy_cud_cache:
2205182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_cud_cache);
2206182696fbSDarrick J. Wong  out_destroy_rui_cache:
2207182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_rui_cache);
2208182696fbSDarrick J. Wong  out_destroy_rud_cache:
2209182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_rud_cache);
2210182696fbSDarrick J. Wong  out_destroy_icreate_cache:
2211182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_icreate_cache);
2212182696fbSDarrick J. Wong  out_destroy_ili_cache:
2213182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_ili_cache);
2214182696fbSDarrick J. Wong  out_destroy_inode_cache:
2215182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_inode_cache);
2216182696fbSDarrick J. Wong  out_destroy_efi_cache:
2217182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_efi_cache);
2218182696fbSDarrick J. Wong  out_destroy_efd_cache:
2219182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_efd_cache);
2220182696fbSDarrick J. Wong  out_destroy_buf_item_cache:
2221182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_buf_item_cache);
2222182696fbSDarrick J. Wong  out_destroy_trans_cache:
2223182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_trans_cache);
2224182696fbSDarrick J. Wong  out_destroy_ifork_cache:
2225182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_ifork_cache);
2226182696fbSDarrick J. Wong  out_destroy_da_state_cache:
2227182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_da_state_cache);
2228f3c799c2SDarrick J. Wong  out_destroy_defer_item_cache:
2229f3c799c2SDarrick J. Wong 	xfs_defer_destroy_item_caches();
2230182696fbSDarrick J. Wong  out_destroy_btree_cur_cache:
22319fa47bdcSDarrick J. Wong 	xfs_btree_destroy_cur_caches();
2232182696fbSDarrick J. Wong  out_destroy_log_ticket_cache:
2233182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_log_ticket_cache);
2234231f91abSDave Chinner  out_destroy_buf_cache:
2235231f91abSDave Chinner 	kmem_cache_destroy(xfs_buf_cache);
2236c59d87c4SChristoph Hellwig  out:
2237c59d87c4SChristoph Hellwig 	return -ENOMEM;
2238c59d87c4SChristoph Hellwig }
2239c59d87c4SChristoph Hellwig 
2240c59d87c4SChristoph Hellwig STATIC void
xfs_destroy_caches(void)2241182696fbSDarrick J. Wong xfs_destroy_caches(void)
2242c59d87c4SChristoph Hellwig {
22438c0a8537SKirill A. Shutemov 	/*
22448c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free are flushed before we
22458c0a8537SKirill A. Shutemov 	 * destroy caches.
22468c0a8537SKirill A. Shutemov 	 */
22478c0a8537SKirill A. Shutemov 	rcu_barrier();
2248784eb7d8SDave Chinner 	kmem_cache_destroy(xfs_iunlink_cache);
22494136e38aSDarrick J. Wong 	kmem_cache_destroy(xfs_attri_cache);
22504136e38aSDarrick J. Wong 	kmem_cache_destroy(xfs_attrd_cache);
2251182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_bui_cache);
2252182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_bud_cache);
2253182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_cui_cache);
2254182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_cud_cache);
2255182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_rui_cache);
2256182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_rud_cache);
2257182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_icreate_cache);
2258182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_ili_cache);
2259182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_inode_cache);
2260182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_efi_cache);
2261182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_efd_cache);
2262182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_buf_item_cache);
2263182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_trans_cache);
2264182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_ifork_cache);
2265182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_da_state_cache);
2266f3c799c2SDarrick J. Wong 	xfs_defer_destroy_item_caches();
22679fa47bdcSDarrick J. Wong 	xfs_btree_destroy_cur_caches();
2268182696fbSDarrick J. Wong 	kmem_cache_destroy(xfs_log_ticket_cache);
2269231f91abSDave Chinner 	kmem_cache_destroy(xfs_buf_cache);
2270c59d87c4SChristoph Hellwig }
2271c59d87c4SChristoph Hellwig 
2272c59d87c4SChristoph Hellwig STATIC int __init
xfs_init_workqueues(void)2273c59d87c4SChristoph Hellwig xfs_init_workqueues(void)
2274c59d87c4SChristoph Hellwig {
2275c59d87c4SChristoph Hellwig 	/*
2276c999a223SDave Chinner 	 * The allocation workqueue can be used in memory reclaim situations
2277c999a223SDave Chinner 	 * (writepage path), and parallelism is only limited by the number of
2278c999a223SDave Chinner 	 * AGs in all the filesystems mounted. Hence use the default large
2279c999a223SDave Chinner 	 * max_active value for this workqueue.
2280c999a223SDave Chinner 	 */
22818018ec08SBrian Foster 	xfs_alloc_wq = alloc_workqueue("xfsalloc",
228205a302a1SDarrick J. Wong 			XFS_WQFLAGS(WQ_MEM_RECLAIM | WQ_FREEZABLE), 0);
2283c999a223SDave Chinner 	if (!xfs_alloc_wq)
22845889608dSDave Chinner 		return -ENOMEM;
2285c999a223SDave Chinner 
228605a302a1SDarrick J. Wong 	xfs_discard_wq = alloc_workqueue("xfsdiscard", XFS_WQFLAGS(WQ_UNBOUND),
228705a302a1SDarrick J. Wong 			0);
22884560e78fSChristoph Hellwig 	if (!xfs_discard_wq)
22894560e78fSChristoph Hellwig 		goto out_free_alloc_wq;
22904560e78fSChristoph Hellwig 
22910030807cSChristoph Hellwig 	return 0;
22924560e78fSChristoph Hellwig out_free_alloc_wq:
22934560e78fSChristoph Hellwig 	destroy_workqueue(xfs_alloc_wq);
22944560e78fSChristoph Hellwig 	return -ENOMEM;
2295c59d87c4SChristoph Hellwig }
2296c59d87c4SChristoph Hellwig 
2297c59d87c4SChristoph Hellwig STATIC void
xfs_destroy_workqueues(void)2298c59d87c4SChristoph Hellwig xfs_destroy_workqueues(void)
2299c59d87c4SChristoph Hellwig {
23004560e78fSChristoph Hellwig 	destroy_workqueue(xfs_discard_wq);
2301c999a223SDave Chinner 	destroy_workqueue(xfs_alloc_wq);
2302c59d87c4SChristoph Hellwig }
2303c59d87c4SChristoph Hellwig 
2304c59d87c4SChristoph Hellwig STATIC int __init
init_xfs_fs(void)2305c59d87c4SChristoph Hellwig init_xfs_fs(void)
2306c59d87c4SChristoph Hellwig {
2307c59d87c4SChristoph Hellwig 	int			error;
2308c59d87c4SChristoph Hellwig 
230930cbc591SDarrick J. Wong 	xfs_check_ondisk_structs();
231030cbc591SDarrick J. Wong 
23113cfb9290SDarrick J. Wong 	error = xfs_dahash_test();
23123cfb9290SDarrick J. Wong 	if (error)
23133cfb9290SDarrick J. Wong 		return error;
23143cfb9290SDarrick J. Wong 
2315c59d87c4SChristoph Hellwig 	printk(KERN_INFO XFS_VERSION_STRING " with "
2316c59d87c4SChristoph Hellwig 			 XFS_BUILD_OPTIONS " enabled\n");
2317c59d87c4SChristoph Hellwig 
2318c59d87c4SChristoph Hellwig 	xfs_dir_startup();
2319c59d87c4SChristoph Hellwig 
2320182696fbSDarrick J. Wong 	error = xfs_init_caches();
2321f1653c2eSDave Chinner 	if (error)
2322ef7d9593SDarrick J. Wong 		goto out;
2323f1653c2eSDave Chinner 
2324c59d87c4SChristoph Hellwig 	error = xfs_init_workqueues();
2325c59d87c4SChristoph Hellwig 	if (error)
2326182696fbSDarrick J. Wong 		goto out_destroy_caches;
2327c59d87c4SChristoph Hellwig 
2328c59d87c4SChristoph Hellwig 	error = xfs_mru_cache_init();
2329c59d87c4SChristoph Hellwig 	if (error)
2330c59d87c4SChristoph Hellwig 		goto out_destroy_wq;
2331c59d87c4SChristoph Hellwig 
2332c59d87c4SChristoph Hellwig 	error = xfs_init_procfs();
2333c59d87c4SChristoph Hellwig 	if (error)
2334231f91abSDave Chinner 		goto out_mru_cache_uninit;
2335c59d87c4SChristoph Hellwig 
2336c59d87c4SChristoph Hellwig 	error = xfs_sysctl_register();
2337c59d87c4SChristoph Hellwig 	if (error)
2338c59d87c4SChristoph Hellwig 		goto out_cleanup_procfs;
2339c59d87c4SChristoph Hellwig 
2340a76dba3bSDarrick J. Wong 	xfs_debugfs = xfs_debugfs_mkdir("xfs", NULL);
2341a76dba3bSDarrick J. Wong 
23423d871226SBrian Foster 	xfs_kset = kset_create_and_add("xfs", NULL, fs_kobj);
23433d871226SBrian Foster 	if (!xfs_kset) {
23443d871226SBrian Foster 		error = -ENOMEM;
2345a76dba3bSDarrick J. Wong 		goto out_debugfs_unregister;
23463d871226SBrian Foster 	}
23473d871226SBrian Foster 
234880529c45SBill O'Donnell 	xfsstats.xs_kobj.kobject.kset = xfs_kset;
234980529c45SBill O'Donnell 
235080529c45SBill O'Donnell 	xfsstats.xs_stats = alloc_percpu(struct xfsstats);
235180529c45SBill O'Donnell 	if (!xfsstats.xs_stats) {
235280529c45SBill O'Donnell 		error = -ENOMEM;
235380529c45SBill O'Donnell 		goto out_kset_unregister;
235480529c45SBill O'Donnell 	}
235580529c45SBill O'Donnell 
235680529c45SBill O'Donnell 	error = xfs_sysfs_init(&xfsstats.xs_kobj, &xfs_stats_ktype, NULL,
2357bb230c12SBill O'Donnell 			       "stats");
2358bb230c12SBill O'Donnell 	if (error)
235980529c45SBill O'Donnell 		goto out_free_stats;
2360bb230c12SBill O'Donnell 
2361d7a74cadSDarrick J. Wong 	error = xchk_global_stats_setup(xfs_debugfs);
2362d7a74cadSDarrick J. Wong 	if (error)
2363d7a74cadSDarrick J. Wong 		goto out_remove_stats_kobj;
2364d7a74cadSDarrick J. Wong 
236565b65735SBrian Foster #ifdef DEBUG
236665b65735SBrian Foster 	xfs_dbg_kobj.kobject.kset = xfs_kset;
236765b65735SBrian Foster 	error = xfs_sysfs_init(&xfs_dbg_kobj, &xfs_dbg_ktype, NULL, "debug");
2368a05931ceSChristoph Hellwig 	if (error)
2369d7a74cadSDarrick J. Wong 		goto out_remove_scrub_stats;
237065b65735SBrian Foster #endif
237165b65735SBrian Foster 
237265b65735SBrian Foster 	error = xfs_qm_init();
237365b65735SBrian Foster 	if (error)
2374bb230c12SBill O'Donnell 		goto out_remove_dbg_kobj;
2375c59d87c4SChristoph Hellwig 
2376c59d87c4SChristoph Hellwig 	error = register_filesystem(&xfs_fs_type);
2377c59d87c4SChristoph Hellwig 	if (error)
2378a05931ceSChristoph Hellwig 		goto out_qm_exit;
2379c59d87c4SChristoph Hellwig 	return 0;
2380c59d87c4SChristoph Hellwig 
2381a05931ceSChristoph Hellwig  out_qm_exit:
2382a05931ceSChristoph Hellwig 	xfs_qm_exit();
2383bb230c12SBill O'Donnell  out_remove_dbg_kobj:
238465b65735SBrian Foster #ifdef DEBUG
238565b65735SBrian Foster 	xfs_sysfs_del(&xfs_dbg_kobj);
2386d7a74cadSDarrick J. Wong  out_remove_scrub_stats:
238765b65735SBrian Foster #endif
2388d7a74cadSDarrick J. Wong 	xchk_global_stats_teardown();
2389d7a74cadSDarrick J. Wong  out_remove_stats_kobj:
239080529c45SBill O'Donnell 	xfs_sysfs_del(&xfsstats.xs_kobj);
239180529c45SBill O'Donnell  out_free_stats:
239280529c45SBill O'Donnell 	free_percpu(xfsstats.xs_stats);
2393bb230c12SBill O'Donnell  out_kset_unregister:
23943d871226SBrian Foster 	kset_unregister(xfs_kset);
2395a76dba3bSDarrick J. Wong  out_debugfs_unregister:
2396a76dba3bSDarrick J. Wong 	debugfs_remove(xfs_debugfs);
2397c59d87c4SChristoph Hellwig 	xfs_sysctl_unregister();
2398c59d87c4SChristoph Hellwig  out_cleanup_procfs:
2399c59d87c4SChristoph Hellwig 	xfs_cleanup_procfs();
2400c59d87c4SChristoph Hellwig  out_mru_cache_uninit:
2401c59d87c4SChristoph Hellwig 	xfs_mru_cache_uninit();
2402c59d87c4SChristoph Hellwig  out_destroy_wq:
2403c59d87c4SChristoph Hellwig 	xfs_destroy_workqueues();
2404182696fbSDarrick J. Wong  out_destroy_caches:
2405182696fbSDarrick J. Wong 	xfs_destroy_caches();
2406c59d87c4SChristoph Hellwig  out:
2407c59d87c4SChristoph Hellwig 	return error;
2408c59d87c4SChristoph Hellwig }
2409c59d87c4SChristoph Hellwig 
2410c59d87c4SChristoph Hellwig STATIC void __exit
exit_xfs_fs(void)2411c59d87c4SChristoph Hellwig exit_xfs_fs(void)
2412c59d87c4SChristoph Hellwig {
2413a05931ceSChristoph Hellwig 	xfs_qm_exit();
2414c59d87c4SChristoph Hellwig 	unregister_filesystem(&xfs_fs_type);
241565b65735SBrian Foster #ifdef DEBUG
241665b65735SBrian Foster 	xfs_sysfs_del(&xfs_dbg_kobj);
241765b65735SBrian Foster #endif
2418d7a74cadSDarrick J. Wong 	xchk_global_stats_teardown();
241980529c45SBill O'Donnell 	xfs_sysfs_del(&xfsstats.xs_kobj);
242080529c45SBill O'Donnell 	free_percpu(xfsstats.xs_stats);
24213d871226SBrian Foster 	kset_unregister(xfs_kset);
2422a76dba3bSDarrick J. Wong 	debugfs_remove(xfs_debugfs);
2423c59d87c4SChristoph Hellwig 	xfs_sysctl_unregister();
2424c59d87c4SChristoph Hellwig 	xfs_cleanup_procfs();
2425c59d87c4SChristoph Hellwig 	xfs_mru_cache_uninit();
2426c59d87c4SChristoph Hellwig 	xfs_destroy_workqueues();
2427182696fbSDarrick J. Wong 	xfs_destroy_caches();
2428af3b6382SDarrick J. Wong 	xfs_uuid_table_free();
2429c59d87c4SChristoph Hellwig }
2430c59d87c4SChristoph Hellwig 
2431c59d87c4SChristoph Hellwig module_init(init_xfs_fs);
2432c59d87c4SChristoph Hellwig module_exit(exit_xfs_fs);
2433c59d87c4SChristoph Hellwig 
2434c59d87c4SChristoph Hellwig MODULE_AUTHOR("Silicon Graphics, Inc.");
2435c59d87c4SChristoph Hellwig MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
2436c59d87c4SChristoph Hellwig MODULE_LICENSE("GPL");
2437