xref: /openbmc/linux/fs/xfs/xfs_mount.c (revision d37cf9b63113f13d742713881ce691fc615d8b3b)
10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
37b718769SNathan Scott  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
47b718769SNathan Scott  * All Rights Reserved.
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds #include "xfs.h"
7a844f451SNathan Scott #include "xfs_fs.h"
870a9883cSDave Chinner #include "xfs_shared.h"
9239880efSDave Chinner #include "xfs_format.h"
10239880efSDave Chinner #include "xfs_log_format.h"
11239880efSDave Chinner #include "xfs_trans_resv.h"
12a844f451SNathan Scott #include "xfs_bit.h"
131da177e4SLinus Torvalds #include "xfs_sb.h"
141da177e4SLinus Torvalds #include "xfs_mount.h"
151da177e4SLinus Torvalds #include "xfs_inode.h"
16a4fbe6abSDave Chinner #include "xfs_dir2.h"
17a844f451SNathan Scott #include "xfs_ialloc.h"
181da177e4SLinus Torvalds #include "xfs_alloc.h"
191da177e4SLinus Torvalds #include "xfs_rtalloc.h"
201da177e4SLinus Torvalds #include "xfs_bmap.h"
21a4fbe6abSDave Chinner #include "xfs_trans.h"
22a4fbe6abSDave Chinner #include "xfs_trans_priv.h"
23a4fbe6abSDave Chinner #include "xfs_log.h"
24b5f17becSDave Chinner #include "xfs_log_priv.h"
251da177e4SLinus Torvalds #include "xfs_error.h"
261da177e4SLinus Torvalds #include "xfs_quota.h"
271da177e4SLinus Torvalds #include "xfs_fsops.h"
286d8b79cfSDave Chinner #include "xfs_icache.h"
29a31b1d3dSBrian Foster #include "xfs_sysfs.h"
30035e00acSDarrick J. Wong #include "xfs_rmap_btree.h"
311946b91cSDarrick J. Wong #include "xfs_refcount_btree.h"
32174edb0eSDarrick J. Wong #include "xfs_reflink.h"
33ebf55872SChristoph Hellwig #include "xfs_extent_busy.h"
3439353ff6SDarrick J. Wong #include "xfs_health.h"
3513eaec4bSDarrick J. Wong #include "xfs_trace.h"
369bbafc71SDave Chinner #include "xfs_ag.h"
37d7a74cadSDarrick J. Wong #include "scrub/stats.h"
381da177e4SLinus Torvalds 
3927174203SChristoph Hellwig static DEFINE_MUTEX(xfs_uuid_table_mutex);
4027174203SChristoph Hellwig static int xfs_uuid_table_size;
4127174203SChristoph Hellwig static uuid_t *xfs_uuid_table;
4227174203SChristoph Hellwig 
43af3b6382SDarrick J. Wong void
xfs_uuid_table_free(void)44af3b6382SDarrick J. Wong xfs_uuid_table_free(void)
45af3b6382SDarrick J. Wong {
46af3b6382SDarrick J. Wong 	if (xfs_uuid_table_size == 0)
47af3b6382SDarrick J. Wong 		return;
48af3b6382SDarrick J. Wong 	kmem_free(xfs_uuid_table);
49af3b6382SDarrick J. Wong 	xfs_uuid_table = NULL;
50af3b6382SDarrick J. Wong 	xfs_uuid_table_size = 0;
51af3b6382SDarrick J. Wong }
52af3b6382SDarrick J. Wong 
5327174203SChristoph Hellwig /*
5427174203SChristoph Hellwig  * See if the UUID is unique among mounted XFS filesystems.
5527174203SChristoph Hellwig  * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
5627174203SChristoph Hellwig  */
5727174203SChristoph Hellwig STATIC int
xfs_uuid_mount(struct xfs_mount * mp)5827174203SChristoph Hellwig xfs_uuid_mount(
5927174203SChristoph Hellwig 	struct xfs_mount	*mp)
6027174203SChristoph Hellwig {
6127174203SChristoph Hellwig 	uuid_t			*uuid = &mp->m_sb.sb_uuid;
6227174203SChristoph Hellwig 	int			hole, i;
6327174203SChristoph Hellwig 
648f720d9fSAmir Goldstein 	/* Publish UUID in struct super_block */
6585787090SChristoph Hellwig 	uuid_copy(&mp->m_super->s_uuid, uuid);
668f720d9fSAmir Goldstein 
670560f31aSDave Chinner 	if (xfs_has_nouuid(mp))
6827174203SChristoph Hellwig 		return 0;
6927174203SChristoph Hellwig 
70d905fdaaSAmir Goldstein 	if (uuid_is_null(uuid)) {
71d905fdaaSAmir Goldstein 		xfs_warn(mp, "Filesystem has null UUID - can't mount");
722451337dSDave Chinner 		return -EINVAL;
7327174203SChristoph Hellwig 	}
7427174203SChristoph Hellwig 
7527174203SChristoph Hellwig 	mutex_lock(&xfs_uuid_table_mutex);
7627174203SChristoph Hellwig 	for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) {
77d905fdaaSAmir Goldstein 		if (uuid_is_null(&xfs_uuid_table[i])) {
7827174203SChristoph Hellwig 			hole = i;
7927174203SChristoph Hellwig 			continue;
8027174203SChristoph Hellwig 		}
8127174203SChristoph Hellwig 		if (uuid_equal(uuid, &xfs_uuid_table[i]))
8227174203SChristoph Hellwig 			goto out_duplicate;
8327174203SChristoph Hellwig 	}
8427174203SChristoph Hellwig 
8527174203SChristoph Hellwig 	if (hole < 0) {
86771915c4SCarlos Maiolino 		xfs_uuid_table = krealloc(xfs_uuid_table,
8727174203SChristoph Hellwig 			(xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table),
88771915c4SCarlos Maiolino 			GFP_KERNEL | __GFP_NOFAIL);
8927174203SChristoph Hellwig 		hole = xfs_uuid_table_size++;
9027174203SChristoph Hellwig 	}
9127174203SChristoph Hellwig 	xfs_uuid_table[hole] = *uuid;
9227174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
9327174203SChristoph Hellwig 
9427174203SChristoph Hellwig 	return 0;
9527174203SChristoph Hellwig 
9627174203SChristoph Hellwig  out_duplicate:
9727174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
98021000e5SMitsuo Hayasaka 	xfs_warn(mp, "Filesystem has duplicate UUID %pU - can't mount", uuid);
992451337dSDave Chinner 	return -EINVAL;
10027174203SChristoph Hellwig }
10127174203SChristoph Hellwig 
10227174203SChristoph Hellwig STATIC void
xfs_uuid_unmount(struct xfs_mount * mp)10327174203SChristoph Hellwig xfs_uuid_unmount(
10427174203SChristoph Hellwig 	struct xfs_mount	*mp)
10527174203SChristoph Hellwig {
10627174203SChristoph Hellwig 	uuid_t			*uuid = &mp->m_sb.sb_uuid;
10727174203SChristoph Hellwig 	int			i;
10827174203SChristoph Hellwig 
1090560f31aSDave Chinner 	if (xfs_has_nouuid(mp))
11027174203SChristoph Hellwig 		return;
11127174203SChristoph Hellwig 
11227174203SChristoph Hellwig 	mutex_lock(&xfs_uuid_table_mutex);
11327174203SChristoph Hellwig 	for (i = 0; i < xfs_uuid_table_size; i++) {
114d905fdaaSAmir Goldstein 		if (uuid_is_null(&xfs_uuid_table[i]))
11527174203SChristoph Hellwig 			continue;
11627174203SChristoph Hellwig 		if (!uuid_equal(uuid, &xfs_uuid_table[i]))
11727174203SChristoph Hellwig 			continue;
11827174203SChristoph Hellwig 		memset(&xfs_uuid_table[i], 0, sizeof(uuid_t));
11927174203SChristoph Hellwig 		break;
12027174203SChristoph Hellwig 	}
12127174203SChristoph Hellwig 	ASSERT(i < xfs_uuid_table_size);
12227174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
12327174203SChristoph Hellwig }
12427174203SChristoph Hellwig 
1254cc929eeSNathan Scott /*
1264cc929eeSNathan Scott  * Check size of device based on the (data/realtime) block count.
1274cc929eeSNathan Scott  * Note: this check is used by the growfs code as well as mount.
1284cc929eeSNathan Scott  */
1294cc929eeSNathan Scott int
xfs_sb_validate_fsb_count(xfs_sb_t * sbp,uint64_t nblocks)1304cc929eeSNathan Scott xfs_sb_validate_fsb_count(
1314cc929eeSNathan Scott 	xfs_sb_t	*sbp,
132c8ce540dSDarrick J. Wong 	uint64_t	nblocks)
1334cc929eeSNathan Scott {
1344cc929eeSNathan Scott 	ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
1354cc929eeSNathan Scott 	ASSERT(sbp->sb_blocklog >= BBSHIFT);
1364cc929eeSNathan Scott 
137d5cf09baSChristoph Hellwig 	/* Limited by ULONG_MAX of page cache index */
13809cbfeafSKirill A. Shutemov 	if (nblocks >> (PAGE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
1392451337dSDave Chinner 		return -EFBIG;
1404cc929eeSNathan Scott 	return 0;
1414cc929eeSNathan Scott }
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds /*
1441da177e4SLinus Torvalds  * xfs_readsb
1451da177e4SLinus Torvalds  *
1461da177e4SLinus Torvalds  * Does the initial read of the superblock.
1471da177e4SLinus Torvalds  */
1481da177e4SLinus Torvalds int
xfs_readsb(struct xfs_mount * mp,int flags)149ff55068cSDave Chinner xfs_readsb(
150ff55068cSDave Chinner 	struct xfs_mount *mp,
151ff55068cSDave Chinner 	int		flags)
1521da177e4SLinus Torvalds {
1531da177e4SLinus Torvalds 	unsigned int	sector_size;
15404a1e6c5SDave Chinner 	struct xfs_buf	*bp;
15504a1e6c5SDave Chinner 	struct xfs_sb	*sbp = &mp->m_sb;
1561da177e4SLinus Torvalds 	int		error;
157af34e09dSDave Chinner 	int		loud = !(flags & XFS_MFSI_QUIET);
158daba5427SEric Sandeen 	const struct xfs_buf_ops *buf_ops;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	ASSERT(mp->m_sb_bp == NULL);
1611da177e4SLinus Torvalds 	ASSERT(mp->m_ddev_targp != NULL);
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	/*
164daba5427SEric Sandeen 	 * For the initial read, we must guess at the sector
165daba5427SEric Sandeen 	 * size based on the block device.  It's enough to
166daba5427SEric Sandeen 	 * get the sb_sectsize out of the superblock and
167daba5427SEric Sandeen 	 * then reread with the proper length.
168daba5427SEric Sandeen 	 * We don't verify it yet, because it may not be complete.
169daba5427SEric Sandeen 	 */
170daba5427SEric Sandeen 	sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
171daba5427SEric Sandeen 	buf_ops = NULL;
172daba5427SEric Sandeen 
173daba5427SEric Sandeen 	/*
174c891c30aSBrian Foster 	 * Allocate a (locked) buffer to hold the superblock. This will be kept
175c891c30aSBrian Foster 	 * around at all times to optimize access to the superblock. Therefore,
176c891c30aSBrian Foster 	 * set XBF_NO_IOACCT to make sure it doesn't hold the buftarg count
177c891c30aSBrian Foster 	 * elevated.
1781da177e4SLinus Torvalds 	 */
17926af6552SDave Chinner reread:
180ba372674SDave Chinner 	error = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR,
181c891c30aSBrian Foster 				      BTOBB(sector_size), XBF_NO_IOACCT, &bp,
182c891c30aSBrian Foster 				      buf_ops);
183ba372674SDave Chinner 	if (error) {
184eab4e633SDave Chinner 		if (loud)
185e721f504SDave Chinner 			xfs_warn(mp, "SB validate failed with error %d.", error);
186ac75a1f7SDave Chinner 		/* bad CRC means corrupted metadata */
1872451337dSDave Chinner 		if (error == -EFSBADCRC)
1882451337dSDave Chinner 			error = -EFSCORRUPTED;
189ba372674SDave Chinner 		return error;
190eab4e633SDave Chinner 	}
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	/*
1931da177e4SLinus Torvalds 	 * Initialize the mount structure from the superblock.
1941da177e4SLinus Torvalds 	 */
1953e6e8afdSChristoph Hellwig 	xfs_sb_from_disk(sbp, bp->b_addr);
196556b8883SDave Chinner 
197556b8883SDave Chinner 	/*
198556b8883SDave Chinner 	 * If we haven't validated the superblock, do so now before we try
199556b8883SDave Chinner 	 * to check the sector size and reread the superblock appropriately.
200556b8883SDave Chinner 	 */
201556b8883SDave Chinner 	if (sbp->sb_magicnum != XFS_SB_MAGIC) {
202556b8883SDave Chinner 		if (loud)
203556b8883SDave Chinner 			xfs_warn(mp, "Invalid superblock magic number");
2042451337dSDave Chinner 		error = -EINVAL;
205556b8883SDave Chinner 		goto release_buf;
206556b8883SDave Chinner 	}
207ff55068cSDave Chinner 
2081da177e4SLinus Torvalds 	/*
2091da177e4SLinus Torvalds 	 * We must be able to do sector-sized and sector-aligned IO.
2101da177e4SLinus Torvalds 	 */
21104a1e6c5SDave Chinner 	if (sector_size > sbp->sb_sectsize) {
212af34e09dSDave Chinner 		if (loud)
213af34e09dSDave Chinner 			xfs_warn(mp, "device supports %u byte sectors (not %u)",
21404a1e6c5SDave Chinner 				sector_size, sbp->sb_sectsize);
2152451337dSDave Chinner 		error = -ENOSYS;
21626af6552SDave Chinner 		goto release_buf;
2171da177e4SLinus Torvalds 	}
2181da177e4SLinus Torvalds 
219556b8883SDave Chinner 	if (buf_ops == NULL) {
2201da177e4SLinus Torvalds 		/*
221daba5427SEric Sandeen 		 * Re-read the superblock so the buffer is correctly sized,
222daba5427SEric Sandeen 		 * and properly verified.
2231da177e4SLinus Torvalds 		 */
2241da177e4SLinus Torvalds 		xfs_buf_relse(bp);
22504a1e6c5SDave Chinner 		sector_size = sbp->sb_sectsize;
226daba5427SEric Sandeen 		buf_ops = loud ? &xfs_sb_buf_ops : &xfs_sb_quiet_buf_ops;
22726af6552SDave Chinner 		goto reread;
2281da177e4SLinus Torvalds 	}
2291da177e4SLinus Torvalds 
230a1d86e8dSDave Chinner 	mp->m_features |= xfs_sb_version_to_features(sbp);
2315681ca40SDave Chinner 	xfs_reinit_percpu_counters(mp);
2328d280b98SDavid Chinner 
23304a1e6c5SDave Chinner 	/* no need to be quiet anymore, so reset the buf ops */
23404a1e6c5SDave Chinner 	bp->b_ops = &xfs_sb_buf_ops;
23504a1e6c5SDave Chinner 
2361da177e4SLinus Torvalds 	mp->m_sb_bp = bp;
23726af6552SDave Chinner 	xfs_buf_unlock(bp);
2381da177e4SLinus Torvalds 	return 0;
2391da177e4SLinus Torvalds 
24026af6552SDave Chinner release_buf:
2411da177e4SLinus Torvalds 	xfs_buf_relse(bp);
2421da177e4SLinus Torvalds 	return error;
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds /*
24613eaec4bSDarrick J. Wong  * If the sunit/swidth change would move the precomputed root inode value, we
24713eaec4bSDarrick J. Wong  * must reject the ondisk change because repair will stumble over that.
24813eaec4bSDarrick J. Wong  * However, we allow the mount to proceed because we never rejected this
24913eaec4bSDarrick J. Wong  * combination before.  Returns true to update the sb, false otherwise.
25013eaec4bSDarrick J. Wong  */
25113eaec4bSDarrick J. Wong static inline int
xfs_check_new_dalign(struct xfs_mount * mp,int new_dalign,bool * update_sb)25213eaec4bSDarrick J. Wong xfs_check_new_dalign(
25313eaec4bSDarrick J. Wong 	struct xfs_mount	*mp,
25413eaec4bSDarrick J. Wong 	int			new_dalign,
25513eaec4bSDarrick J. Wong 	bool			*update_sb)
25613eaec4bSDarrick J. Wong {
25713eaec4bSDarrick J. Wong 	struct xfs_sb		*sbp = &mp->m_sb;
25813eaec4bSDarrick J. Wong 	xfs_ino_t		calc_ino;
25913eaec4bSDarrick J. Wong 
26013eaec4bSDarrick J. Wong 	calc_ino = xfs_ialloc_calc_rootino(mp, new_dalign);
26113eaec4bSDarrick J. Wong 	trace_xfs_check_new_dalign(mp, new_dalign, calc_ino);
26213eaec4bSDarrick J. Wong 
26313eaec4bSDarrick J. Wong 	if (sbp->sb_rootino == calc_ino) {
26413eaec4bSDarrick J. Wong 		*update_sb = true;
26513eaec4bSDarrick J. Wong 		return 0;
26613eaec4bSDarrick J. Wong 	}
26713eaec4bSDarrick J. Wong 
26813eaec4bSDarrick J. Wong 	xfs_warn(mp,
26913eaec4bSDarrick J. Wong "Cannot change stripe alignment; would require moving root inode.");
27013eaec4bSDarrick J. Wong 
27113eaec4bSDarrick J. Wong 	/*
27213eaec4bSDarrick J. Wong 	 * XXX: Next time we add a new incompat feature, this should start
27313eaec4bSDarrick J. Wong 	 * returning -EINVAL to fail the mount.  Until then, spit out a warning
27413eaec4bSDarrick J. Wong 	 * that we're ignoring the administrator's instructions.
27513eaec4bSDarrick J. Wong 	 */
27613eaec4bSDarrick J. Wong 	xfs_warn(mp, "Skipping superblock stripe alignment update.");
27713eaec4bSDarrick J. Wong 	*update_sb = false;
27813eaec4bSDarrick J. Wong 	return 0;
27913eaec4bSDarrick J. Wong }
28013eaec4bSDarrick J. Wong 
28113eaec4bSDarrick J. Wong /*
2824f5b1b3aSDarrick J. Wong  * If we were provided with new sunit/swidth values as mount options, make sure
2834f5b1b3aSDarrick J. Wong  * that they pass basic alignment and superblock feature checks, and convert
2844f5b1b3aSDarrick J. Wong  * them into the same units (FSB) that everything else expects.  This step
2854f5b1b3aSDarrick J. Wong  * /must/ be done before computing the inode geometry.
2861da177e4SLinus Torvalds  */
2870771fb45SEric Sandeen STATIC int
xfs_validate_new_dalign(struct xfs_mount * mp)2884f5b1b3aSDarrick J. Wong xfs_validate_new_dalign(
2894f5b1b3aSDarrick J. Wong 	struct xfs_mount	*mp)
2901da177e4SLinus Torvalds {
2914f5b1b3aSDarrick J. Wong 	if (mp->m_dalign == 0)
2924f5b1b3aSDarrick J. Wong 		return 0;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	/*
2951da177e4SLinus Torvalds 	 * If stripe unit and stripe width are not multiples
2961da177e4SLinus Torvalds 	 * of the fs blocksize turn off alignment.
2971da177e4SLinus Torvalds 	 */
2981da177e4SLinus Torvalds 	if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
2991da177e4SLinus Torvalds 	    (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
30039a45d84SJie Liu 		xfs_warn(mp,
30139a45d84SJie Liu 	"alignment check failed: sunit/swidth vs. blocksize(%d)",
3024f5b1b3aSDarrick J. Wong 			mp->m_sb.sb_blocksize);
3032451337dSDave Chinner 		return -EINVAL;
304de94a2e1SZeng Heng 	}
305de94a2e1SZeng Heng 
3061da177e4SLinus Torvalds 	/*
3071da177e4SLinus Torvalds 	 * Convert the stripe unit and width to FSBs.
3081da177e4SLinus Torvalds 	 */
3091da177e4SLinus Torvalds 	mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
3104f5b1b3aSDarrick J. Wong 	if (mp->m_dalign && (mp->m_sb.sb_agblocks % mp->m_dalign)) {
31153487786SDave Chinner 		xfs_warn(mp,
31239a45d84SJie Liu 	"alignment check failed: sunit/swidth vs. agsize(%d)",
3134f5b1b3aSDarrick J. Wong 			mp->m_sb.sb_agblocks);
3142451337dSDave Chinner 		return -EINVAL;
315de94a2e1SZeng Heng 	}
316de94a2e1SZeng Heng 
317de94a2e1SZeng Heng 	if (!mp->m_dalign) {
31839a45d84SJie Liu 		xfs_warn(mp,
31939a45d84SJie Liu 	"alignment check failed: sunit(%d) less than bsize(%d)",
3204f5b1b3aSDarrick J. Wong 			mp->m_dalign, mp->m_sb.sb_blocksize);
3212451337dSDave Chinner 		return -EINVAL;
3221da177e4SLinus Torvalds 	}
323de94a2e1SZeng Heng 
324de94a2e1SZeng Heng 	mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
3251da177e4SLinus Torvalds 
32638c26bfdSDave Chinner 	if (!xfs_has_dalign(mp)) {
32734d7f603SJie Liu 		xfs_warn(mp,
32834d7f603SJie Liu "cannot change alignment: superblock does not support data alignment");
3292451337dSDave Chinner 		return -EINVAL;
3301da177e4SLinus Torvalds 	}
3314f5b1b3aSDarrick J. Wong 
3324f5b1b3aSDarrick J. Wong 	return 0;
3334f5b1b3aSDarrick J. Wong }
3344f5b1b3aSDarrick J. Wong 
3354f5b1b3aSDarrick J. Wong /* Update alignment values based on mount options and sb values. */
3364f5b1b3aSDarrick J. Wong STATIC int
xfs_update_alignment(struct xfs_mount * mp)3374f5b1b3aSDarrick J. Wong xfs_update_alignment(
3384f5b1b3aSDarrick J. Wong 	struct xfs_mount	*mp)
3394f5b1b3aSDarrick J. Wong {
3404f5b1b3aSDarrick J. Wong 	struct xfs_sb		*sbp = &mp->m_sb;
3414f5b1b3aSDarrick J. Wong 
3424f5b1b3aSDarrick J. Wong 	if (mp->m_dalign) {
34313eaec4bSDarrick J. Wong 		bool		update_sb;
34413eaec4bSDarrick J. Wong 		int		error;
34513eaec4bSDarrick J. Wong 
3464f5b1b3aSDarrick J. Wong 		if (sbp->sb_unit == mp->m_dalign &&
3474f5b1b3aSDarrick J. Wong 		    sbp->sb_width == mp->m_swidth)
3484f5b1b3aSDarrick J. Wong 			return 0;
3494f5b1b3aSDarrick J. Wong 
35013eaec4bSDarrick J. Wong 		error = xfs_check_new_dalign(mp, mp->m_dalign, &update_sb);
35113eaec4bSDarrick J. Wong 		if (error || !update_sb)
35213eaec4bSDarrick J. Wong 			return error;
35313eaec4bSDarrick J. Wong 
3544f5b1b3aSDarrick J. Wong 		sbp->sb_unit = mp->m_dalign;
3554f5b1b3aSDarrick J. Wong 		sbp->sb_width = mp->m_swidth;
3564f5b1b3aSDarrick J. Wong 		mp->m_update_sb = true;
3570560f31aSDave Chinner 	} else if (!xfs_has_noalign(mp) && xfs_has_dalign(mp)) {
3581da177e4SLinus Torvalds 		mp->m_dalign = sbp->sb_unit;
3591da177e4SLinus Torvalds 		mp->m_swidth = sbp->sb_width;
3601da177e4SLinus Torvalds 	}
3611da177e4SLinus Torvalds 
3620771fb45SEric Sandeen 	return 0;
3630771fb45SEric Sandeen }
3641da177e4SLinus Torvalds 
3650771fb45SEric Sandeen /*
366055388a3SDave Chinner  * precalculate the low space thresholds for dynamic speculative preallocation.
367055388a3SDave Chinner  */
368055388a3SDave Chinner void
xfs_set_low_space_thresholds(struct xfs_mount * mp)369055388a3SDave Chinner xfs_set_low_space_thresholds(
370055388a3SDave Chinner 	struct xfs_mount	*mp)
371055388a3SDave Chinner {
37265f03d86SDarrick J. Wong 	uint64_t		dblocks = mp->m_sb.sb_dblocks;
37365f03d86SDarrick J. Wong 	uint64_t		rtexts = mp->m_sb.sb_rextents;
374055388a3SDave Chinner 	int			i;
375055388a3SDave Chinner 
37665f03d86SDarrick J. Wong 	do_div(dblocks, 100);
37765f03d86SDarrick J. Wong 	do_div(rtexts, 100);
378055388a3SDave Chinner 
37965f03d86SDarrick J. Wong 	for (i = 0; i < XFS_LOWSP_MAX; i++) {
38065f03d86SDarrick J. Wong 		mp->m_low_space[i] = dblocks * (i + 1);
38165f03d86SDarrick J. Wong 		mp->m_low_rtexts[i] = rtexts * (i + 1);
382055388a3SDave Chinner 	}
383055388a3SDave Chinner }
384055388a3SDave Chinner 
3851da177e4SLinus Torvalds /*
3860471f62eSZhi Yong Wu  * Check that the data (and log if separate) is an ok size.
3871da177e4SLinus Torvalds  */
3880771fb45SEric Sandeen STATIC int
xfs_check_sizes(struct xfs_mount * mp)389ba372674SDave Chinner xfs_check_sizes(
390ba372674SDave Chinner 	struct xfs_mount *mp)
3910771fb45SEric Sandeen {
392ba372674SDave Chinner 	struct xfs_buf	*bp;
3930771fb45SEric Sandeen 	xfs_daddr_t	d;
394ba372674SDave Chinner 	int		error;
3950771fb45SEric Sandeen 
3961da177e4SLinus Torvalds 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
3971da177e4SLinus Torvalds 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
3980b932cccSDave Chinner 		xfs_warn(mp, "filesystem size mismatch detected");
3992451337dSDave Chinner 		return -EFBIG;
4001da177e4SLinus Torvalds 	}
401ba372674SDave Chinner 	error = xfs_buf_read_uncached(mp->m_ddev_targp,
4021da177e4SLinus Torvalds 					d - XFS_FSS_TO_BB(mp, 1),
403ba372674SDave Chinner 					XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL);
404ba372674SDave Chinner 	if (error) {
4050b932cccSDave Chinner 		xfs_warn(mp, "last sector read failed");
406ba372674SDave Chinner 		return error;
4071da177e4SLinus Torvalds 	}
4081922c949SDave Chinner 	xfs_buf_relse(bp);
4091da177e4SLinus Torvalds 
410ba372674SDave Chinner 	if (mp->m_logdev_targp == mp->m_ddev_targp)
411ba372674SDave Chinner 		return 0;
412ba372674SDave Chinner 
4131da177e4SLinus Torvalds 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
4141da177e4SLinus Torvalds 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
4150b932cccSDave Chinner 		xfs_warn(mp, "log size mismatch detected");
4162451337dSDave Chinner 		return -EFBIG;
4171da177e4SLinus Torvalds 	}
418ba372674SDave Chinner 	error = xfs_buf_read_uncached(mp->m_logdev_targp,
4191da177e4SLinus Torvalds 					d - XFS_FSB_TO_BB(mp, 1),
420ba372674SDave Chinner 					XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
421ba372674SDave Chinner 	if (error) {
4220b932cccSDave Chinner 		xfs_warn(mp, "log device read failed");
423ba372674SDave Chinner 		return error;
4241da177e4SLinus Torvalds 	}
4251922c949SDave Chinner 	xfs_buf_relse(bp);
4260771fb45SEric Sandeen 	return 0;
4270771fb45SEric Sandeen }
4280771fb45SEric Sandeen 
4290771fb45SEric Sandeen /*
4307d095257SChristoph Hellwig  * Clear the quotaflags in memory and in the superblock.
4317d095257SChristoph Hellwig  */
4327d095257SChristoph Hellwig int
xfs_mount_reset_sbqflags(struct xfs_mount * mp)4337d095257SChristoph Hellwig xfs_mount_reset_sbqflags(
4347d095257SChristoph Hellwig 	struct xfs_mount	*mp)
4357d095257SChristoph Hellwig {
4367d095257SChristoph Hellwig 	mp->m_qflags = 0;
4377d095257SChristoph Hellwig 
43861e63ecbSDave Chinner 	/* It is OK to look at sb_qflags in the mount path without m_sb_lock. */
4397d095257SChristoph Hellwig 	if (mp->m_sb.sb_qflags == 0)
4407d095257SChristoph Hellwig 		return 0;
4417d095257SChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
4427d095257SChristoph Hellwig 	mp->m_sb.sb_qflags = 0;
4437d095257SChristoph Hellwig 	spin_unlock(&mp->m_sb_lock);
4447d095257SChristoph Hellwig 
44561e63ecbSDave Chinner 	if (!xfs_fs_writable(mp, SB_FREEZE_WRITE))
4467d095257SChristoph Hellwig 		return 0;
4477d095257SChristoph Hellwig 
44861e63ecbSDave Chinner 	return xfs_sync_sb(mp, false);
4497d095257SChristoph Hellwig }
4507d095257SChristoph Hellwig 
451c8ce540dSDarrick J. Wong uint64_t
xfs_default_resblks(xfs_mount_t * mp)452d5db0f97SEric Sandeen xfs_default_resblks(xfs_mount_t *mp)
453d5db0f97SEric Sandeen {
454c8ce540dSDarrick J. Wong 	uint64_t resblks;
455d5db0f97SEric Sandeen 
456d5db0f97SEric Sandeen 	/*
4578babd8a2SDave Chinner 	 * We default to 5% or 8192 fsbs of space reserved, whichever is
4588babd8a2SDave Chinner 	 * smaller.  This is intended to cover concurrent allocation
4598babd8a2SDave Chinner 	 * transactions when we initially hit enospc. These each require a 4
4608babd8a2SDave Chinner 	 * block reservation. Hence by default we cover roughly 2000 concurrent
4618babd8a2SDave Chinner 	 * allocation reservations.
462d5db0f97SEric Sandeen 	 */
463d5db0f97SEric Sandeen 	resblks = mp->m_sb.sb_dblocks;
464d5db0f97SEric Sandeen 	do_div(resblks, 20);
465c8ce540dSDarrick J. Wong 	resblks = min_t(uint64_t, resblks, 8192);
466d5db0f97SEric Sandeen 	return resblks;
467d5db0f97SEric Sandeen }
468d5db0f97SEric Sandeen 
4692e9e6481SDarrick J. Wong /* Ensure the summary counts are correct. */
4702e9e6481SDarrick J. Wong STATIC int
xfs_check_summary_counts(struct xfs_mount * mp)4712e9e6481SDarrick J. Wong xfs_check_summary_counts(
4722e9e6481SDarrick J. Wong 	struct xfs_mount	*mp)
4732e9e6481SDarrick J. Wong {
4745a605fd6SDarrick J. Wong 	int			error = 0;
4755a605fd6SDarrick J. Wong 
4762e9e6481SDarrick J. Wong 	/*
4772e9e6481SDarrick J. Wong 	 * The AG0 superblock verifier rejects in-progress filesystems,
4782e9e6481SDarrick J. Wong 	 * so we should never see the flag set this far into mounting.
4792e9e6481SDarrick J. Wong 	 */
4802e9e6481SDarrick J. Wong 	if (mp->m_sb.sb_inprogress) {
4812e9e6481SDarrick J. Wong 		xfs_err(mp, "sb_inprogress set after log recovery??");
4822e9e6481SDarrick J. Wong 		WARN_ON(1);
4832e9e6481SDarrick J. Wong 		return -EFSCORRUPTED;
4842e9e6481SDarrick J. Wong 	}
4852e9e6481SDarrick J. Wong 
4862e9e6481SDarrick J. Wong 	/*
4872e9e6481SDarrick J. Wong 	 * Now the log is mounted, we know if it was an unclean shutdown or
4882e9e6481SDarrick J. Wong 	 * not. If it was, with the first phase of recovery has completed, we
4892e9e6481SDarrick J. Wong 	 * have consistent AG blocks on disk. We have not recovered EFIs yet,
4902e9e6481SDarrick J. Wong 	 * but they are recovered transactionally in the second recovery phase
4912e9e6481SDarrick J. Wong 	 * later.
4922e9e6481SDarrick J. Wong 	 *
4932e9e6481SDarrick J. Wong 	 * If the log was clean when we mounted, we can check the summary
4942e9e6481SDarrick J. Wong 	 * counters.  If any of them are obviously incorrect, we can recompute
4952e9e6481SDarrick J. Wong 	 * them from the AGF headers in the next step.
4962e9e6481SDarrick J. Wong 	 */
4972e973b2cSDave Chinner 	if (xfs_is_clean(mp) &&
4982e9e6481SDarrick J. Wong 	    (mp->m_sb.sb_fdblocks > mp->m_sb.sb_dblocks ||
49900d22a1cSDarrick J. Wong 	     !xfs_verify_icount(mp, mp->m_sb.sb_icount) ||
5002e9e6481SDarrick J. Wong 	     mp->m_sb.sb_ifree > mp->m_sb.sb_icount))
50139353ff6SDarrick J. Wong 		xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS);
5022e9e6481SDarrick J. Wong 
5032e9e6481SDarrick J. Wong 	/*
5042e9e6481SDarrick J. Wong 	 * We can safely re-initialise incore superblock counters from the
5052e9e6481SDarrick J. Wong 	 * per-ag data. These may not be correct if the filesystem was not
5062e9e6481SDarrick J. Wong 	 * cleanly unmounted, so we waited for recovery to finish before doing
5072e9e6481SDarrick J. Wong 	 * this.
5082e9e6481SDarrick J. Wong 	 *
5092e9e6481SDarrick J. Wong 	 * If the filesystem was cleanly unmounted or the previous check did
5102e9e6481SDarrick J. Wong 	 * not flag anything weird, then we can trust the values in the
5112e9e6481SDarrick J. Wong 	 * superblock to be correct and we don't need to do anything here.
5122e9e6481SDarrick J. Wong 	 * Otherwise, recalculate the summary counters.
5132e9e6481SDarrick J. Wong 	 */
5145a605fd6SDarrick J. Wong 	if ((xfs_has_lazysbcount(mp) && !xfs_is_clean(mp)) ||
5155a605fd6SDarrick J. Wong 	    xfs_fs_has_sickness(mp, XFS_SICK_FS_COUNTERS)) {
5165a605fd6SDarrick J. Wong 		error = xfs_initialize_perag_data(mp, mp->m_sb.sb_agcount);
5175a605fd6SDarrick J. Wong 		if (error)
5185a605fd6SDarrick J. Wong 			return error;
5195a605fd6SDarrick J. Wong 	}
5202e9e6481SDarrick J. Wong 
5215a605fd6SDarrick J. Wong 	/*
5225a605fd6SDarrick J. Wong 	 * Older kernels misused sb_frextents to reflect both incore
5235a605fd6SDarrick J. Wong 	 * reservations made by running transactions and the actual count of
5245a605fd6SDarrick J. Wong 	 * free rt extents in the ondisk metadata.  Transactions committed
5255a605fd6SDarrick J. Wong 	 * during runtime can therefore contain a superblock update that
5265a605fd6SDarrick J. Wong 	 * undercounts the number of free rt extents tracked in the rt bitmap.
5275a605fd6SDarrick J. Wong 	 * A clean unmount record will have the correct frextents value since
5285a605fd6SDarrick J. Wong 	 * there can be no other transactions running at that point.
5295a605fd6SDarrick J. Wong 	 *
5305a605fd6SDarrick J. Wong 	 * If we're mounting the rt volume after recovering the log, recompute
5315a605fd6SDarrick J. Wong 	 * frextents from the rtbitmap file to fix the inconsistency.
5325a605fd6SDarrick J. Wong 	 */
5335a605fd6SDarrick J. Wong 	if (xfs_has_realtime(mp) && !xfs_is_clean(mp)) {
5345a605fd6SDarrick J. Wong 		error = xfs_rtalloc_reinit_frextents(mp);
5355a605fd6SDarrick J. Wong 		if (error)
5365a605fd6SDarrick J. Wong 			return error;
5375a605fd6SDarrick J. Wong 	}
5385a605fd6SDarrick J. Wong 
5395a605fd6SDarrick J. Wong 	return 0;
5402e9e6481SDarrick J. Wong }
5412e9e6481SDarrick J. Wong 
54259f6ab40SLong Li static void
xfs_unmount_check(struct xfs_mount * mp)54359f6ab40SLong Li xfs_unmount_check(
54459f6ab40SLong Li 	struct xfs_mount	*mp)
54559f6ab40SLong Li {
54659f6ab40SLong Li 	if (xfs_is_shutdown(mp))
54759f6ab40SLong Li 		return;
54859f6ab40SLong Li 
54959f6ab40SLong Li 	if (percpu_counter_sum(&mp->m_ifree) >
55059f6ab40SLong Li 			percpu_counter_sum(&mp->m_icount)) {
55159f6ab40SLong Li 		xfs_alert(mp, "ifree/icount mismatch at unmount");
55259f6ab40SLong Li 		xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS);
55359f6ab40SLong Li 	}
55459f6ab40SLong Li }
55559f6ab40SLong Li 
5567d095257SChristoph Hellwig /*
557d336f7ebSDarrick J. Wong  * Flush and reclaim dirty inodes in preparation for unmount. Inodes and
558d336f7ebSDarrick J. Wong  * internal inode structures can be sitting in the CIL and AIL at this point,
559d336f7ebSDarrick J. Wong  * so we need to unpin them, write them back and/or reclaim them before unmount
560ab23a776SDave Chinner  * can proceed.  In other words, callers are required to have inactivated all
561ab23a776SDave Chinner  * inodes.
562d336f7ebSDarrick J. Wong  *
563d336f7ebSDarrick J. Wong  * An inode cluster that has been freed can have its buffer still pinned in
564d336f7ebSDarrick J. Wong  * memory because the transaction is still sitting in a iclog. The stale inodes
565d336f7ebSDarrick J. Wong  * on that buffer will be pinned to the buffer until the transaction hits the
566d336f7ebSDarrick J. Wong  * disk and the callbacks run. Pushing the AIL will skip the stale inodes and
567d336f7ebSDarrick J. Wong  * may never see the pinned buffer, so nothing will push out the iclog and
568d336f7ebSDarrick J. Wong  * unpin the buffer.
569d336f7ebSDarrick J. Wong  *
570d336f7ebSDarrick J. Wong  * Hence we need to force the log to unpin everything first. However, log
571d336f7ebSDarrick J. Wong  * forces don't wait for the discards they issue to complete, so we have to
572d336f7ebSDarrick J. Wong  * explicitly wait for them to complete here as well.
573d336f7ebSDarrick J. Wong  *
574d336f7ebSDarrick J. Wong  * Then we can tell the world we are unmounting so that error handling knows
575d336f7ebSDarrick J. Wong  * that the filesystem is going away and we should error out anything that we
576d336f7ebSDarrick J. Wong  * have been retrying in the background.  This will prevent never-ending
577d336f7ebSDarrick J. Wong  * retries in AIL pushing from hanging the unmount.
578d336f7ebSDarrick J. Wong  *
579d336f7ebSDarrick J. Wong  * Finally, we can push the AIL to clean all the remaining dirty objects, then
580d336f7ebSDarrick J. Wong  * reclaim the remaining inodes that are still in memory at this point in time.
581d336f7ebSDarrick J. Wong  */
582d336f7ebSDarrick J. Wong static void
xfs_unmount_flush_inodes(struct xfs_mount * mp)583d336f7ebSDarrick J. Wong xfs_unmount_flush_inodes(
584d336f7ebSDarrick J. Wong 	struct xfs_mount	*mp)
585d336f7ebSDarrick J. Wong {
586d336f7ebSDarrick J. Wong 	xfs_log_force(mp, XFS_LOG_SYNC);
587d336f7ebSDarrick J. Wong 	xfs_extent_busy_wait_all(mp);
588d336f7ebSDarrick J. Wong 	flush_workqueue(xfs_discard_wq);
589d336f7ebSDarrick J. Wong 
5902e973b2cSDave Chinner 	set_bit(XFS_OPSTATE_UNMOUNTING, &mp->m_opstate);
591d336f7ebSDarrick J. Wong 
592d336f7ebSDarrick J. Wong 	xfs_ail_push_all_sync(mp->m_ail);
593ab23a776SDave Chinner 	xfs_inodegc_stop(mp);
594d336f7ebSDarrick J. Wong 	cancel_delayed_work_sync(&mp->m_reclaim_work);
595d336f7ebSDarrick J. Wong 	xfs_reclaim_inodes(mp);
596d336f7ebSDarrick J. Wong 	xfs_health_unmount(mp);
597d336f7ebSDarrick J. Wong }
598d336f7ebSDarrick J. Wong 
599b2941046SDave Chinner static void
xfs_mount_setup_inode_geom(struct xfs_mount * mp)600b2941046SDave Chinner xfs_mount_setup_inode_geom(
601b2941046SDave Chinner 	struct xfs_mount	*mp)
602b2941046SDave Chinner {
603b2941046SDave Chinner 	struct xfs_ino_geometry *igeo = M_IGEO(mp);
604b2941046SDave Chinner 
605b2941046SDave Chinner 	igeo->attr_fork_offset = xfs_bmap_compute_attr_offset(mp);
606b2941046SDave Chinner 	ASSERT(igeo->attr_fork_offset < XFS_LITINO(mp));
607b2941046SDave Chinner 
608b2941046SDave Chinner 	xfs_ialloc_setup_geometry(mp);
609b2941046SDave Chinner }
610b2941046SDave Chinner 
611b74e15d7SDarrick J. Wong /* Compute maximum possible height for per-AG btree types for this fs. */
612b74e15d7SDarrick J. Wong static inline void
xfs_agbtree_compute_maxlevels(struct xfs_mount * mp)613b74e15d7SDarrick J. Wong xfs_agbtree_compute_maxlevels(
614b74e15d7SDarrick J. Wong 	struct xfs_mount	*mp)
615b74e15d7SDarrick J. Wong {
616b74e15d7SDarrick J. Wong 	unsigned int		levels;
617b74e15d7SDarrick J. Wong 
618b74e15d7SDarrick J. Wong 	levels = max(mp->m_alloc_maxlevels, M_IGEO(mp)->inobt_maxlevels);
619b74e15d7SDarrick J. Wong 	levels = max(levels, mp->m_rmap_maxlevels);
620b74e15d7SDarrick J. Wong 	mp->m_agbtree_maxlevels = max(levels, mp->m_refc_maxlevels);
621b74e15d7SDarrick J. Wong }
622b74e15d7SDarrick J. Wong 
623d336f7ebSDarrick J. Wong /*
6240771fb45SEric Sandeen  * This function does the following on an initial mount of a file system:
6250771fb45SEric Sandeen  *	- reads the superblock from disk and init the mount struct
6260771fb45SEric Sandeen  *	- if we're a 32-bit kernel, do a size check on the superblock
6270771fb45SEric Sandeen  *		so we don't mount terabyte filesystems
6280771fb45SEric Sandeen  *	- init mount struct realtime fields
6290771fb45SEric Sandeen  *	- allocate inode hash table for fs
6300771fb45SEric Sandeen  *	- init directory manager
6310771fb45SEric Sandeen  *	- perform recovery and init the log manager
6320771fb45SEric Sandeen  */
6330771fb45SEric Sandeen int
xfs_mountfs(struct xfs_mount * mp)6340771fb45SEric Sandeen xfs_mountfs(
635f0b2efadSBrian Foster 	struct xfs_mount	*mp)
6360771fb45SEric Sandeen {
637f0b2efadSBrian Foster 	struct xfs_sb		*sbp = &(mp->m_sb);
638f0b2efadSBrian Foster 	struct xfs_inode	*rip;
639ef325959SDarrick J. Wong 	struct xfs_ino_geometry	*igeo = M_IGEO(mp);
640c8ce540dSDarrick J. Wong 	uint64_t		resblks;
6417d095257SChristoph Hellwig 	uint			quotamount = 0;
6427d095257SChristoph Hellwig 	uint			quotaflags = 0;
6430771fb45SEric Sandeen 	int			error = 0;
6440771fb45SEric Sandeen 
645ff55068cSDave Chinner 	xfs_sb_mount_common(mp, sbp);
6460771fb45SEric Sandeen 
6470771fb45SEric Sandeen 	/*
648074e427bSDave Chinner 	 * Check for a mismatched features2 values.  Older kernels read & wrote
649074e427bSDave Chinner 	 * into the wrong sb offset for sb_features2 on some platforms due to
650074e427bSDave Chinner 	 * xfs_sb_t not being 64bit size aligned when sb_features2 was added,
651074e427bSDave Chinner 	 * which made older superblock reading/writing routines swap it as a
652074e427bSDave Chinner 	 * 64-bit value.
653ee1c0908SDavid Chinner 	 *
654e6957ea4SEric Sandeen 	 * For backwards compatibility, we make both slots equal.
655e6957ea4SEric Sandeen 	 *
656074e427bSDave Chinner 	 * If we detect a mismatched field, we OR the set bits into the existing
657074e427bSDave Chinner 	 * features2 field in case it has already been modified; we don't want
658074e427bSDave Chinner 	 * to lose any features.  We then update the bad location with the ORed
659074e427bSDave Chinner 	 * value so that older kernels will see any features2 flags. The
660074e427bSDave Chinner 	 * superblock writeback code ensures the new sb_features2 is copied to
661074e427bSDave Chinner 	 * sb_bad_features2 before it is logged or written to disk.
662ee1c0908SDavid Chinner 	 */
663e6957ea4SEric Sandeen 	if (xfs_sb_has_mismatched_features2(sbp)) {
6640b932cccSDave Chinner 		xfs_warn(mp, "correcting sb_features alignment problem");
665ee1c0908SDavid Chinner 		sbp->sb_features2 |= sbp->sb_bad_features2;
66661e63ecbSDave Chinner 		mp->m_update_sb = true;
6677c12f296STim Shimmin 	}
668e6957ea4SEric Sandeen 
669ee1c0908SDavid Chinner 
670263997a6SDave Chinner 	/* always use v2 inodes by default now */
671263997a6SDave Chinner 	if (!(mp->m_sb.sb_versionnum & XFS_SB_VERSION_NLINKBIT)) {
672263997a6SDave Chinner 		mp->m_sb.sb_versionnum |= XFS_SB_VERSION_NLINKBIT;
67338c26bfdSDave Chinner 		mp->m_features |= XFS_FEAT_NLINK;
67461e63ecbSDave Chinner 		mp->m_update_sb = true;
675263997a6SDave Chinner 	}
676263997a6SDave Chinner 
677ee1c0908SDavid Chinner 	/*
6784f5b1b3aSDarrick J. Wong 	 * If we were given new sunit/swidth options, do some basic validation
6794f5b1b3aSDarrick J. Wong 	 * checks and convert the incore dalign and swidth values to the
6804f5b1b3aSDarrick J. Wong 	 * same units (FSB) that everything else uses.  This /must/ happen
6814f5b1b3aSDarrick J. Wong 	 * before computing the inode geometry.
6820771fb45SEric Sandeen 	 */
6834f5b1b3aSDarrick J. Wong 	error = xfs_validate_new_dalign(mp);
6840771fb45SEric Sandeen 	if (error)
685f9057e3dSChristoph Hellwig 		goto out;
6860771fb45SEric Sandeen 
6870771fb45SEric Sandeen 	xfs_alloc_compute_maxlevels(mp);
6880771fb45SEric Sandeen 	xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
6890771fb45SEric Sandeen 	xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
690b2941046SDave Chinner 	xfs_mount_setup_inode_geom(mp);
691035e00acSDarrick J. Wong 	xfs_rmapbt_compute_maxlevels(mp);
6921946b91cSDarrick J. Wong 	xfs_refcountbt_compute_maxlevels(mp);
6930771fb45SEric Sandeen 
694b74e15d7SDarrick J. Wong 	xfs_agbtree_compute_maxlevels(mp);
695b74e15d7SDarrick J. Wong 
6964f5b1b3aSDarrick J. Wong 	/*
6974f5b1b3aSDarrick J. Wong 	 * Check if sb_agblocks is aligned at stripe boundary.  If sb_agblocks
6984f5b1b3aSDarrick J. Wong 	 * is NOT aligned turn off m_dalign since allocator alignment is within
6994f5b1b3aSDarrick J. Wong 	 * an ag, therefore ag has to be aligned at stripe boundary.  Note that
7004f5b1b3aSDarrick J. Wong 	 * we must compute the free space and rmap btree geometry before doing
7014f5b1b3aSDarrick J. Wong 	 * this.
7024f5b1b3aSDarrick J. Wong 	 */
7034f5b1b3aSDarrick J. Wong 	error = xfs_update_alignment(mp);
7044f5b1b3aSDarrick J. Wong 	if (error)
7054f5b1b3aSDarrick J. Wong 		goto out;
7064f5b1b3aSDarrick J. Wong 
707e6b3bb78SCarlos Maiolino 	/* enable fail_at_unmount as default */
708749f24f3SThomas Meyer 	mp->m_fail_unmount = true;
709e6b3bb78SCarlos Maiolino 
710e1d3d218SIan Kent 	error = xfs_sysfs_init(&mp->m_kobj, &xfs_mp_ktype,
711e1d3d218SIan Kent 			       NULL, mp->m_super->s_id);
71227174203SChristoph Hellwig 	if (error)
713f9057e3dSChristoph Hellwig 		goto out;
7141da177e4SLinus Torvalds 
715225e4635SBill O'Donnell 	error = xfs_sysfs_init(&mp->m_stats.xs_kobj, &xfs_stats_ktype,
716225e4635SBill O'Donnell 			       &mp->m_kobj, "stats");
717a31b1d3dSBrian Foster 	if (error)
718a31b1d3dSBrian Foster 		goto out_remove_sysfs;
719a31b1d3dSBrian Foster 
720d7a74cadSDarrick J. Wong 	xchk_stats_register(mp->m_scrub_stats, mp->m_debugfs);
721d7a74cadSDarrick J. Wong 
722192852beSCarlos Maiolino 	error = xfs_error_sysfs_init(mp);
723225e4635SBill O'Donnell 	if (error)
724d7a74cadSDarrick J. Wong 		goto out_remove_scrub_stats;
725225e4635SBill O'Donnell 
72631965ef3SDarrick J. Wong 	error = xfs_errortag_init(mp);
72731965ef3SDarrick J. Wong 	if (error)
72831965ef3SDarrick J. Wong 		goto out_remove_error_sysfs;
729192852beSCarlos Maiolino 
730192852beSCarlos Maiolino 	error = xfs_uuid_mount(mp);
731192852beSCarlos Maiolino 	if (error)
73231965ef3SDarrick J. Wong 		goto out_remove_errortag;
733192852beSCarlos Maiolino 
7341da177e4SLinus Torvalds 	/*
7352fcddee8SChristoph Hellwig 	 * Update the preferred write size based on the information from the
7362fcddee8SChristoph Hellwig 	 * on-disk superblock.
7370771fb45SEric Sandeen 	 */
7382fcddee8SChristoph Hellwig 	mp->m_allocsize_log =
7392fcddee8SChristoph Hellwig 		max_t(uint32_t, sbp->sb_blocklog, mp->m_allocsize_log);
7402fcddee8SChristoph Hellwig 	mp->m_allocsize_blocks = 1U << (mp->m_allocsize_log - sbp->sb_blocklog);
7410771fb45SEric Sandeen 
742055388a3SDave Chinner 	/* set the low space thresholds for dynamic preallocation */
743055388a3SDave Chinner 	xfs_set_low_space_thresholds(mp);
744055388a3SDave Chinner 
7450771fb45SEric Sandeen 	/*
746e5376fc1SBrian Foster 	 * If enabled, sparse inode chunk alignment is expected to match the
747e5376fc1SBrian Foster 	 * cluster size. Full inode chunk alignment must match the chunk size,
748e5376fc1SBrian Foster 	 * but that is checked on sb read verification...
749e5376fc1SBrian Foster 	 */
75038c26bfdSDave Chinner 	if (xfs_has_sparseinodes(mp) &&
751e5376fc1SBrian Foster 	    mp->m_sb.sb_spino_align !=
752490d451fSDarrick J. Wong 			XFS_B_TO_FSBT(mp, igeo->inode_cluster_size_raw)) {
753e5376fc1SBrian Foster 		xfs_warn(mp,
754e5376fc1SBrian Foster 	"Sparse inode block alignment (%u) must match cluster size (%llu).",
755e5376fc1SBrian Foster 			 mp->m_sb.sb_spino_align,
756490d451fSDarrick J. Wong 			 XFS_B_TO_FSBT(mp, igeo->inode_cluster_size_raw));
757e5376fc1SBrian Foster 		error = -EINVAL;
758e5376fc1SBrian Foster 		goto out_remove_uuid;
759e5376fc1SBrian Foster 	}
760e5376fc1SBrian Foster 
761e5376fc1SBrian Foster 	/*
762c2bfbc9bSZhi Yong Wu 	 * Check that the data (and log if separate) is an ok size.
7630771fb45SEric Sandeen 	 */
7644249023aSChristoph Hellwig 	error = xfs_check_sizes(mp);
7650771fb45SEric Sandeen 	if (error)
766f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
7670771fb45SEric Sandeen 
7680771fb45SEric Sandeen 	/*
7691da177e4SLinus Torvalds 	 * Initialize realtime fields in the mount structure
7701da177e4SLinus Torvalds 	 */
7710771fb45SEric Sandeen 	error = xfs_rtmount_init(mp);
7720771fb45SEric Sandeen 	if (error) {
7730b932cccSDave Chinner 		xfs_warn(mp, "RT mount failed");
774f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
7751da177e4SLinus Torvalds 	}
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 	/*
7781da177e4SLinus Torvalds 	 *  Copies the low order bits of the timestamp and the randomly
7791da177e4SLinus Torvalds 	 *  set "sequence" number out of a UUID.
7801da177e4SLinus Torvalds 	 */
781cb0ba6ccSChristoph Hellwig 	mp->m_fixedfsid[0] =
782cb0ba6ccSChristoph Hellwig 		(get_unaligned_be16(&sbp->sb_uuid.b[8]) << 16) |
783cb0ba6ccSChristoph Hellwig 		 get_unaligned_be16(&sbp->sb_uuid.b[4]);
784cb0ba6ccSChristoph Hellwig 	mp->m_fixedfsid[1] = get_unaligned_be32(&sbp->sb_uuid.b[0]);
7851da177e4SLinus Torvalds 
7860650b554SDave Chinner 	error = xfs_da_mount(mp);
7870650b554SDave Chinner 	if (error) {
7880650b554SDave Chinner 		xfs_warn(mp, "Failed dir/attr init: %d", error);
7890650b554SDave Chinner 		goto out_remove_uuid;
7900650b554SDave Chinner 	}
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	/*
7931da177e4SLinus Torvalds 	 * Initialize the precomputed transaction reservations values.
7941da177e4SLinus Torvalds 	 */
7951da177e4SLinus Torvalds 	xfs_trans_init(mp);
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 	/*
7981da177e4SLinus Torvalds 	 * Allocate and initialize the per-ag data.
7991da177e4SLinus Torvalds 	 */
800*bb305f88SChristoph Hellwig 	error = xfs_initialize_perag(mp, 0, sbp->sb_agcount,
801*bb305f88SChristoph Hellwig 			mp->m_sb.sb_dblocks, &mp->m_maxagi);
8021c1c6ebcSDave Chinner 	if (error) {
8030b932cccSDave Chinner 		xfs_warn(mp, "Failed per-ag init: %d", error);
8040650b554SDave Chinner 		goto out_free_dir;
8051c1c6ebcSDave Chinner 	}
8061da177e4SLinus Torvalds 
807a71895c5SDarrick J. Wong 	if (XFS_IS_CORRUPT(mp, !sbp->sb_logblocks)) {
8080b932cccSDave Chinner 		xfs_warn(mp, "no log defined");
8092451337dSDave Chinner 		error = -EFSCORRUPTED;
810f9057e3dSChristoph Hellwig 		goto out_free_perag;
811f9057e3dSChristoph Hellwig 	}
812f9057e3dSChristoph Hellwig 
81340b1de00SDarrick J. Wong 	error = xfs_inodegc_register_shrinker(mp);
81440b1de00SDarrick J. Wong 	if (error)
81540b1de00SDarrick J. Wong 		goto out_fail_wait;
81640b1de00SDarrick J. Wong 
8171da177e4SLinus Torvalds 	/*
818f0b2efadSBrian Foster 	 * Log's mount-time initialization. The first part of recovery can place
819f0b2efadSBrian Foster 	 * some items on the AIL, to be handled when recovery is finished or
820f0b2efadSBrian Foster 	 * cancelled.
8211da177e4SLinus Torvalds 	 */
8221da177e4SLinus Torvalds 	error = xfs_log_mount(mp, mp->m_logdev_targp,
8231da177e4SLinus Torvalds 			      XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
8241da177e4SLinus Torvalds 			      XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
8251da177e4SLinus Torvalds 	if (error) {
8260b932cccSDave Chinner 		xfs_warn(mp, "log mount failed");
82740b1de00SDarrick J. Wong 		goto out_inodegc_shrinker;
8281da177e4SLinus Torvalds 	}
8291da177e4SLinus Torvalds 
830ab23a776SDave Chinner 	/* Enable background inode inactivation workers. */
831ab23a776SDave Chinner 	xfs_inodegc_start(mp);
8326f649091SDarrick J. Wong 	xfs_blockgc_start(mp);
833ab23a776SDave Chinner 
83492821e2bSDavid Chinner 	/*
835e23b55d5SDave Chinner 	 * Now that we've recovered any pending superblock feature bit
836e23b55d5SDave Chinner 	 * additions, we can finish setting up the attr2 behaviour for the
8370560f31aSDave Chinner 	 * mount. The noattr2 option overrides the superblock flag, so only
8380560f31aSDave Chinner 	 * check the superblock feature flag if the mount option is not set.
839e23b55d5SDave Chinner 	 */
8400560f31aSDave Chinner 	if (xfs_has_noattr2(mp)) {
8410560f31aSDave Chinner 		mp->m_features &= ~XFS_FEAT_ATTR2;
8420560f31aSDave Chinner 	} else if (!xfs_has_attr2(mp) &&
8430560f31aSDave Chinner 		   (mp->m_sb.sb_features2 & XFS_SB_VERSION2_ATTR2BIT)) {
8440560f31aSDave Chinner 		mp->m_features |= XFS_FEAT_ATTR2;
8450560f31aSDave Chinner 	}
846e23b55d5SDave Chinner 
847e23b55d5SDave Chinner 	/*
8481da177e4SLinus Torvalds 	 * Get and sanity-check the root inode.
8491da177e4SLinus Torvalds 	 * Save the pointer to it in the mount structure.
8501da177e4SLinus Torvalds 	 */
851541b5accSDave Chinner 	error = xfs_iget(mp, NULL, sbp->sb_rootino, XFS_IGET_UNTRUSTED,
852541b5accSDave Chinner 			 XFS_ILOCK_EXCL, &rip);
8531da177e4SLinus Torvalds 	if (error) {
854541b5accSDave Chinner 		xfs_warn(mp,
855541b5accSDave Chinner 			"Failed to read root inode 0x%llx, error %d",
856541b5accSDave Chinner 			sbp->sb_rootino, -error);
857f9057e3dSChristoph Hellwig 		goto out_log_dealloc;
8581da177e4SLinus Torvalds 	}
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 	ASSERT(rip != NULL);
8611da177e4SLinus Torvalds 
862a71895c5SDarrick J. Wong 	if (XFS_IS_CORRUPT(mp, !S_ISDIR(VFS_I(rip)->i_mode))) {
8630b932cccSDave Chinner 		xfs_warn(mp, "corrupted root inode %llu: not a directory",
864b6574520SNathan Scott 			(unsigned long long)rip->i_ino);
8651da177e4SLinus Torvalds 		xfs_iunlock(rip, XFS_ILOCK_EXCL);
8662451337dSDave Chinner 		error = -EFSCORRUPTED;
867f9057e3dSChristoph Hellwig 		goto out_rele_rip;
8681da177e4SLinus Torvalds 	}
8691da177e4SLinus Torvalds 	mp->m_rootip = rip;	/* save it */
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	xfs_iunlock(rip, XFS_ILOCK_EXCL);
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	/*
8741da177e4SLinus Torvalds 	 * Initialize realtime inode pointers in the mount structure
8751da177e4SLinus Torvalds 	 */
8760771fb45SEric Sandeen 	error = xfs_rtmount_inodes(mp);
8770771fb45SEric Sandeen 	if (error) {
8781da177e4SLinus Torvalds 		/*
8791da177e4SLinus Torvalds 		 * Free up the root inode.
8801da177e4SLinus Torvalds 		 */
8810b932cccSDave Chinner 		xfs_warn(mp, "failed to read RT inodes");
882f9057e3dSChristoph Hellwig 		goto out_rele_rip;
8831da177e4SLinus Torvalds 	}
8841da177e4SLinus Torvalds 
8855a605fd6SDarrick J. Wong 	/* Make sure the summary counts are ok. */
8865a605fd6SDarrick J. Wong 	error = xfs_check_summary_counts(mp);
8875a605fd6SDarrick J. Wong 	if (error)
8885a605fd6SDarrick J. Wong 		goto out_rtunmount;
8895a605fd6SDarrick J. Wong 
8901da177e4SLinus Torvalds 	/*
8917884bc86SChristoph Hellwig 	 * If this is a read-only mount defer the superblock updates until
8927884bc86SChristoph Hellwig 	 * the next remount into writeable mode.  Otherwise we would never
8937884bc86SChristoph Hellwig 	 * perform the update e.g. for the root filesystem.
8941da177e4SLinus Torvalds 	 */
8952e973b2cSDave Chinner 	if (mp->m_update_sb && !xfs_is_readonly(mp)) {
89661e63ecbSDave Chinner 		error = xfs_sync_sb(mp, false);
897e5720eecSDavid Chinner 		if (error) {
8980b932cccSDave Chinner 			xfs_warn(mp, "failed to write sb changes");
899b93b6e43SChristoph Hellwig 			goto out_rtunmount;
900e5720eecSDavid Chinner 		}
901e5720eecSDavid Chinner 	}
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds 	/*
9041da177e4SLinus Torvalds 	 * Initialise the XFS quota management subsystem for this mount
9051da177e4SLinus Torvalds 	 */
906149e53afSChristoph Hellwig 	if (XFS_IS_QUOTA_ON(mp)) {
9077d095257SChristoph Hellwig 		error = xfs_qm_newmount(mp, &quotamount, &quotaflags);
9080771fb45SEric Sandeen 		if (error)
909b93b6e43SChristoph Hellwig 			goto out_rtunmount;
9107d095257SChristoph Hellwig 	} else {
9117d095257SChristoph Hellwig 		/*
9127d095257SChristoph Hellwig 		 * If a file system had quotas running earlier, but decided to
9137d095257SChristoph Hellwig 		 * mount without -o uquota/pquota/gquota options, revoke the
9147d095257SChristoph Hellwig 		 * quotachecked license.
9157d095257SChristoph Hellwig 		 */
9167d095257SChristoph Hellwig 		if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) {
9170b932cccSDave Chinner 			xfs_notice(mp, "resetting quota flags");
9187d095257SChristoph Hellwig 			error = xfs_mount_reset_sbqflags(mp);
9197d095257SChristoph Hellwig 			if (error)
920a70a4fa5SBrian Foster 				goto out_rtunmount;
9217d095257SChristoph Hellwig 		}
9227d095257SChristoph Hellwig 	}
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds 	/*
925f0b2efadSBrian Foster 	 * Finish recovering the file system.  This part needed to be delayed
926f0b2efadSBrian Foster 	 * until after the root and real-time bitmap inodes were consistently
92781ed9475SDarrick J. Wong 	 * read in.  Temporarily create per-AG space reservations for metadata
92881ed9475SDarrick J. Wong 	 * btree shape changes because space freeing transactions (for inode
92981ed9475SDarrick J. Wong 	 * inactivation) require the per-AG reservation in lieu of reserving
93081ed9475SDarrick J. Wong 	 * blocks.
9311da177e4SLinus Torvalds 	 */
93281ed9475SDarrick J. Wong 	error = xfs_fs_reserve_ag_blocks(mp);
93381ed9475SDarrick J. Wong 	if (error && error == -ENOSPC)
93481ed9475SDarrick J. Wong 		xfs_warn(mp,
93581ed9475SDarrick J. Wong 	"ENOSPC reserving per-AG metadata pool, log recovery may fail.");
9364249023aSChristoph Hellwig 	error = xfs_log_mount_finish(mp);
93781ed9475SDarrick J. Wong 	xfs_fs_unreserve_ag_blocks(mp);
9381da177e4SLinus Torvalds 	if (error) {
9390b932cccSDave Chinner 		xfs_warn(mp, "log mount finish failed");
940b93b6e43SChristoph Hellwig 		goto out_rtunmount;
9411da177e4SLinus Torvalds 	}
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	/*
944ddeb14f4SDave Chinner 	 * Now the log is fully replayed, we can transition to full read-only
945ddeb14f4SDave Chinner 	 * mode for read-only mounts. This will sync all the metadata and clean
946ddeb14f4SDave Chinner 	 * the log so that the recovery we just performed does not have to be
947ddeb14f4SDave Chinner 	 * replayed again on the next mount.
948ddeb14f4SDave Chinner 	 *
949ddeb14f4SDave Chinner 	 * We use the same quiesce mechanism as the rw->ro remount, as they are
950ddeb14f4SDave Chinner 	 * semantically identical operations.
951ddeb14f4SDave Chinner 	 */
9522e973b2cSDave Chinner 	if (xfs_is_readonly(mp) && !xfs_has_norecovery(mp))
953ea2064daSBrian Foster 		xfs_log_clean(mp);
954ddeb14f4SDave Chinner 
955ddeb14f4SDave Chinner 	/*
9561da177e4SLinus Torvalds 	 * Complete the quota initialisation, post-log-replay component.
9571da177e4SLinus Torvalds 	 */
9587d095257SChristoph Hellwig 	if (quotamount) {
9597d095257SChristoph Hellwig 		ASSERT(mp->m_qflags == 0);
9607d095257SChristoph Hellwig 		mp->m_qflags = quotaflags;
9617d095257SChristoph Hellwig 
9627d095257SChristoph Hellwig 		xfs_qm_mount_quotas(mp);
9637d095257SChristoph Hellwig 	}
9647d095257SChristoph Hellwig 
96584e1e99fSDavid Chinner 	/*
96684e1e99fSDavid Chinner 	 * Now we are mounted, reserve a small amount of unused space for
96784e1e99fSDavid Chinner 	 * privileged transactions. This is needed so that transaction
96884e1e99fSDavid Chinner 	 * space required for critical operations can dip into this pool
96984e1e99fSDavid Chinner 	 * when at ENOSPC. This is needed for operations like create with
97084e1e99fSDavid Chinner 	 * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
97184e1e99fSDavid Chinner 	 * are not allowed to use this reserved space.
9728babd8a2SDave Chinner 	 *
9738babd8a2SDave Chinner 	 * This may drive us straight to ENOSPC on mount, but that implies
9748babd8a2SDave Chinner 	 * we were already there on the last unmount. Warn if this occurs.
97584e1e99fSDavid Chinner 	 */
9762e973b2cSDave Chinner 	if (!xfs_is_readonly(mp)) {
977d5db0f97SEric Sandeen 		resblks = xfs_default_resblks(mp);
978714082bcSDavid Chinner 		error = xfs_reserve_blocks(mp, &resblks, NULL);
979714082bcSDavid Chinner 		if (error)
9800b932cccSDave Chinner 			xfs_warn(mp,
9810b932cccSDave Chinner 	"Unable to allocate reserve blocks. Continuing without reserve pool.");
982174edb0eSDarrick J. Wong 
98384d69619SDarrick J. Wong 		/* Reserve AG blocks for future btree expansion. */
98484d69619SDarrick J. Wong 		error = xfs_fs_reserve_ag_blocks(mp);
98584d69619SDarrick J. Wong 		if (error && error != -ENOSPC)
98684d69619SDarrick J. Wong 			goto out_agresv;
987d5db0f97SEric Sandeen 	}
98884e1e99fSDavid Chinner 
9891da177e4SLinus Torvalds 	return 0;
9901da177e4SLinus Torvalds 
99184d69619SDarrick J. Wong  out_agresv:
99284d69619SDarrick J. Wong 	xfs_fs_unreserve_ag_blocks(mp);
993174edb0eSDarrick J. Wong 	xfs_qm_unmount_quotas(mp);
994b93b6e43SChristoph Hellwig  out_rtunmount:
995b93b6e43SChristoph Hellwig 	xfs_rtunmount_inodes(mp);
996f9057e3dSChristoph Hellwig  out_rele_rip:
99744a8736bSDarrick J. Wong 	xfs_irele(rip);
99877aff8c7SDarrick J. Wong 	/* Clean out dquots that might be in memory after quotacheck. */
99977aff8c7SDarrick J. Wong 	xfs_qm_unmount(mp);
1000ab23a776SDave Chinner 
1001ab23a776SDave Chinner 	/*
1002ab23a776SDave Chinner 	 * Inactivate all inodes that might still be in memory after a log
1003ab23a776SDave Chinner 	 * intent recovery failure so that reclaim can free them.  Metadata
1004ab23a776SDave Chinner 	 * inodes and the root directory shouldn't need inactivation, but the
1005ab23a776SDave Chinner 	 * mount failed for some reason, so pull down all the state and flee.
1006ab23a776SDave Chinner 	 */
1007ab23a776SDave Chinner 	xfs_inodegc_flush(mp);
1008ab23a776SDave Chinner 
10092d1d1da3SDarrick J. Wong 	/*
1010d336f7ebSDarrick J. Wong 	 * Flush all inode reclamation work and flush the log.
10112d1d1da3SDarrick J. Wong 	 * We have to do this /after/ rtunmount and qm_unmount because those
10122d1d1da3SDarrick J. Wong 	 * two will have scheduled delayed reclaim for the rt/quota inodes.
10132d1d1da3SDarrick J. Wong 	 *
10142d1d1da3SDarrick J. Wong 	 * This is slightly different from the unmountfs call sequence
10152d1d1da3SDarrick J. Wong 	 * because we could be tearing down a partially set up mount.  In
10162d1d1da3SDarrick J. Wong 	 * particular, if log_mount_finish fails we bail out without calling
10172d1d1da3SDarrick J. Wong 	 * qm_unmount_quotas and therefore rely on qm_unmount to release the
10182d1d1da3SDarrick J. Wong 	 * quota inodes.
10192d1d1da3SDarrick J. Wong 	 */
1020d336f7ebSDarrick J. Wong 	xfs_unmount_flush_inodes(mp);
1021f9057e3dSChristoph Hellwig  out_log_dealloc:
1022f0b2efadSBrian Foster 	xfs_log_mount_cancel(mp);
102340b1de00SDarrick J. Wong  out_inodegc_shrinker:
102440b1de00SDarrick J. Wong 	unregister_shrinker(&mp->m_inodegc_shrinker);
1025d4f3512bSDave Chinner  out_fail_wait:
1026d4f3512bSDave Chinner 	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
102710fb9ac1SBrian Foster 		xfs_buftarg_drain(mp->m_logdev_targp);
102810fb9ac1SBrian Foster 	xfs_buftarg_drain(mp->m_ddev_targp);
1029f9057e3dSChristoph Hellwig  out_free_perag:
1030ff4f038cSChristoph Hellwig 	xfs_free_perag(mp);
10310650b554SDave Chinner  out_free_dir:
10320650b554SDave Chinner 	xfs_da_unmount(mp);
1033f9057e3dSChristoph Hellwig  out_remove_uuid:
103427174203SChristoph Hellwig 	xfs_uuid_unmount(mp);
103531965ef3SDarrick J. Wong  out_remove_errortag:
103631965ef3SDarrick J. Wong 	xfs_errortag_del(mp);
1037192852beSCarlos Maiolino  out_remove_error_sysfs:
1038192852beSCarlos Maiolino 	xfs_error_sysfs_del(mp);
1039d7a74cadSDarrick J. Wong  out_remove_scrub_stats:
1040d7a74cadSDarrick J. Wong 	xchk_stats_unregister(mp->m_scrub_stats);
1041225e4635SBill O'Donnell 	xfs_sysfs_del(&mp->m_stats.xs_kobj);
1042a31b1d3dSBrian Foster  out_remove_sysfs:
1043a31b1d3dSBrian Foster 	xfs_sysfs_del(&mp->m_kobj);
1044f9057e3dSChristoph Hellwig  out:
10451da177e4SLinus Torvalds 	return error;
10461da177e4SLinus Torvalds }
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds /*
10491da177e4SLinus Torvalds  * This flushes out the inodes,dquots and the superblock, unmounts the
10501da177e4SLinus Torvalds  * log and makes sure that incore structures are freed.
10511da177e4SLinus Torvalds  */
105241b5c2e7SChristoph Hellwig void
xfs_unmountfs(struct xfs_mount * mp)105341b5c2e7SChristoph Hellwig xfs_unmountfs(
105441b5c2e7SChristoph Hellwig 	struct xfs_mount	*mp)
10551da177e4SLinus Torvalds {
1056c8ce540dSDarrick J. Wong 	uint64_t		resblks;
105741b5c2e7SChristoph Hellwig 	int			error;
10581da177e4SLinus Torvalds 
1059ab23a776SDave Chinner 	/*
1060ab23a776SDave Chinner 	 * Perform all on-disk metadata updates required to inactivate inodes
1061ab23a776SDave Chinner 	 * that the VFS evicted earlier in the unmount process.  Freeing inodes
1062ab23a776SDave Chinner 	 * and discarding CoW fork preallocations can cause shape changes to
1063ab23a776SDave Chinner 	 * the free inode and refcount btrees, respectively, so we must finish
1064ab23a776SDave Chinner 	 * this before we discard the metadata space reservations.  Metadata
1065ab23a776SDave Chinner 	 * inodes and the root directory do not require inactivation.
1066ab23a776SDave Chinner 	 */
1067ab23a776SDave Chinner 	xfs_inodegc_flush(mp);
1068ab23a776SDave Chinner 
1069c9a6526fSDarrick J. Wong 	xfs_blockgc_stop(mp);
107084d69619SDarrick J. Wong 	xfs_fs_unreserve_ag_blocks(mp);
10717d095257SChristoph Hellwig 	xfs_qm_unmount_quotas(mp);
1072b93b6e43SChristoph Hellwig 	xfs_rtunmount_inodes(mp);
107344a8736bSDarrick J. Wong 	xfs_irele(mp->m_rootip);
107477508ec8SChristoph Hellwig 
1075d336f7ebSDarrick J. Wong 	xfs_unmount_flush_inodes(mp);
10761da177e4SLinus Torvalds 
10777d095257SChristoph Hellwig 	xfs_qm_unmount(mp);
1078a357a121SLachlan McIlroy 
10791da177e4SLinus Torvalds 	/*
108084e1e99fSDavid Chinner 	 * Unreserve any blocks we have so that when we unmount we don't account
108184e1e99fSDavid Chinner 	 * the reserved free space as used. This is really only necessary for
108284e1e99fSDavid Chinner 	 * lazy superblock counting because it trusts the incore superblock
10839da096fdSMalcolm Parsons 	 * counters to be absolutely correct on clean unmount.
108484e1e99fSDavid Chinner 	 *
108584e1e99fSDavid Chinner 	 * We don't bother correcting this elsewhere for lazy superblock
108684e1e99fSDavid Chinner 	 * counting because on mount of an unclean filesystem we reconstruct the
108784e1e99fSDavid Chinner 	 * correct counter value and this is irrelevant.
108884e1e99fSDavid Chinner 	 *
108984e1e99fSDavid Chinner 	 * For non-lazy counter filesystems, this doesn't matter at all because
109084e1e99fSDavid Chinner 	 * we only every apply deltas to the superblock and hence the incore
109184e1e99fSDavid Chinner 	 * value does not matter....
109284e1e99fSDavid Chinner 	 */
109384e1e99fSDavid Chinner 	resblks = 0;
1094714082bcSDavid Chinner 	error = xfs_reserve_blocks(mp, &resblks, NULL);
1095714082bcSDavid Chinner 	if (error)
10960b932cccSDave Chinner 		xfs_warn(mp, "Unable to free reserved block pool. "
1097714082bcSDavid Chinner 				"Freespace may not be correct on next mount.");
109859f6ab40SLong Li 	xfs_unmount_check(mp);
1099714082bcSDavid Chinner 
110021b699c8SChristoph Hellwig 	xfs_log_unmount(mp);
11010650b554SDave Chinner 	xfs_da_unmount(mp);
110227174203SChristoph Hellwig 	xfs_uuid_unmount(mp);
11031da177e4SLinus Torvalds 
11041550d0b0SChristoph Hellwig #if defined(DEBUG)
110531965ef3SDarrick J. Wong 	xfs_errortag_clearall(mp);
11061da177e4SLinus Torvalds #endif
110740b1de00SDarrick J. Wong 	unregister_shrinker(&mp->m_inodegc_shrinker);
1108ff4f038cSChristoph Hellwig 	xfs_free_perag(mp);
1109a31b1d3dSBrian Foster 
111031965ef3SDarrick J. Wong 	xfs_errortag_del(mp);
1111192852beSCarlos Maiolino 	xfs_error_sysfs_del(mp);
1112d7a74cadSDarrick J. Wong 	xchk_stats_unregister(mp->m_scrub_stats);
1113225e4635SBill O'Donnell 	xfs_sysfs_del(&mp->m_stats.xs_kobj);
1114a31b1d3dSBrian Foster 	xfs_sysfs_del(&mp->m_kobj);
11151da177e4SLinus Torvalds }
11161da177e4SLinus Torvalds 
111791ee575fSBrian Foster /*
111891ee575fSBrian Foster  * Determine whether modifications can proceed. The caller specifies the minimum
111991ee575fSBrian Foster  * freeze level for which modifications should not be allowed. This allows
112091ee575fSBrian Foster  * certain operations to proceed while the freeze sequence is in progress, if
112191ee575fSBrian Foster  * necessary.
112291ee575fSBrian Foster  */
112391ee575fSBrian Foster bool
xfs_fs_writable(struct xfs_mount * mp,int level)112491ee575fSBrian Foster xfs_fs_writable(
112591ee575fSBrian Foster 	struct xfs_mount	*mp,
112691ee575fSBrian Foster 	int			level)
112792821e2bSDavid Chinner {
112891ee575fSBrian Foster 	ASSERT(level > SB_UNFROZEN);
112991ee575fSBrian Foster 	if ((mp->m_super->s_writers.frozen >= level) ||
113075c8c50fSDave Chinner 	    xfs_is_shutdown(mp) || xfs_is_readonly(mp))
113191ee575fSBrian Foster 		return false;
113291ee575fSBrian Foster 
113391ee575fSBrian Foster 	return true;
113492821e2bSDavid Chinner }
113592821e2bSDavid Chinner 
11362229276cSDarrick J. Wong /* Adjust m_fdblocks or m_frextents. */
11370d485adaSDave Chinner int
xfs_mod_freecounter(struct xfs_mount * mp,struct percpu_counter * counter,int64_t delta,bool rsvd)11382229276cSDarrick J. Wong xfs_mod_freecounter(
11390d485adaSDave Chinner 	struct xfs_mount	*mp,
11402229276cSDarrick J. Wong 	struct percpu_counter	*counter,
11410d485adaSDave Chinner 	int64_t			delta,
11420d485adaSDave Chinner 	bool			rsvd)
11430d485adaSDave Chinner {
11440d485adaSDave Chinner 	int64_t			lcounter;
11450d485adaSDave Chinner 	long long		res_used;
11462229276cSDarrick J. Wong 	uint64_t		set_aside = 0;
11470d485adaSDave Chinner 	s32			batch;
11482229276cSDarrick J. Wong 	bool			has_resv_pool;
11492229276cSDarrick J. Wong 
11502229276cSDarrick J. Wong 	ASSERT(counter == &mp->m_fdblocks || counter == &mp->m_frextents);
11512229276cSDarrick J. Wong 	has_resv_pool = (counter == &mp->m_fdblocks);
11522229276cSDarrick J. Wong 	if (rsvd)
11532229276cSDarrick J. Wong 		ASSERT(has_resv_pool);
11540d485adaSDave Chinner 
11550d485adaSDave Chinner 	if (delta > 0) {
11560d485adaSDave Chinner 		/*
11570d485adaSDave Chinner 		 * If the reserve pool is depleted, put blocks back into it
11580d485adaSDave Chinner 		 * first. Most of the time the pool is full.
11590d485adaSDave Chinner 		 */
11602229276cSDarrick J. Wong 		if (likely(!has_resv_pool ||
11612229276cSDarrick J. Wong 			   mp->m_resblks == mp->m_resblks_avail)) {
11622229276cSDarrick J. Wong 			percpu_counter_add(counter, delta);
11630d485adaSDave Chinner 			return 0;
11640d485adaSDave Chinner 		}
11650d485adaSDave Chinner 
11660d485adaSDave Chinner 		spin_lock(&mp->m_sb_lock);
11670d485adaSDave Chinner 		res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
11680d485adaSDave Chinner 
11690d485adaSDave Chinner 		if (res_used > delta) {
11700d485adaSDave Chinner 			mp->m_resblks_avail += delta;
11710d485adaSDave Chinner 		} else {
11720d485adaSDave Chinner 			delta -= res_used;
11730d485adaSDave Chinner 			mp->m_resblks_avail = mp->m_resblks;
11742229276cSDarrick J. Wong 			percpu_counter_add(counter, delta);
11750d485adaSDave Chinner 		}
11760d485adaSDave Chinner 		spin_unlock(&mp->m_sb_lock);
11770d485adaSDave Chinner 		return 0;
11780d485adaSDave Chinner 	}
11790d485adaSDave Chinner 
11800d485adaSDave Chinner 	/*
11810d485adaSDave Chinner 	 * Taking blocks away, need to be more accurate the closer we
11820d485adaSDave Chinner 	 * are to zero.
11830d485adaSDave Chinner 	 *
11840d485adaSDave Chinner 	 * If the counter has a value of less than 2 * max batch size,
11850d485adaSDave Chinner 	 * then make everything serialise as we are real close to
11860d485adaSDave Chinner 	 * ENOSPC.
11870d485adaSDave Chinner 	 */
11882229276cSDarrick J. Wong 	if (__percpu_counter_compare(counter, 2 * XFS_FDBLOCKS_BATCH,
11898c1903d3SDave Chinner 				     XFS_FDBLOCKS_BATCH) < 0)
11900d485adaSDave Chinner 		batch = 1;
11910d485adaSDave Chinner 	else
11928c1903d3SDave Chinner 		batch = XFS_FDBLOCKS_BATCH;
11930d485adaSDave Chinner 
1194fd43cf60SBrian Foster 	/*
1195fd43cf60SBrian Foster 	 * Set aside allocbt blocks because these blocks are tracked as free
1196fd43cf60SBrian Foster 	 * space but not available for allocation. Technically this means that a
1197fd43cf60SBrian Foster 	 * single reservation cannot consume all remaining free space, but the
1198fd43cf60SBrian Foster 	 * ratio of allocbt blocks to usable free blocks should be rather small.
1199fd43cf60SBrian Foster 	 * The tradeoff without this is that filesystems that maintain high
1200fd43cf60SBrian Foster 	 * perag block reservations can over reserve physical block availability
1201fd43cf60SBrian Foster 	 * and fail physical allocation, which leads to much more serious
1202fd43cf60SBrian Foster 	 * problems (i.e. transaction abort, pagecache discards, etc.) than
1203fd43cf60SBrian Foster 	 * slightly premature -ENOSPC.
1204fd43cf60SBrian Foster 	 */
12052229276cSDarrick J. Wong 	if (has_resv_pool)
1206c8c56825SDarrick J. Wong 		set_aside = xfs_fdblocks_unavailable(mp);
12072229276cSDarrick J. Wong 	percpu_counter_add_batch(counter, delta, batch);
12082229276cSDarrick J. Wong 	if (__percpu_counter_compare(counter, set_aside,
12098c1903d3SDave Chinner 				     XFS_FDBLOCKS_BATCH) >= 0) {
12100d485adaSDave Chinner 		/* we had space! */
12110d485adaSDave Chinner 		return 0;
12120d485adaSDave Chinner 	}
12130d485adaSDave Chinner 
12140d485adaSDave Chinner 	/*
12150d485adaSDave Chinner 	 * lock up the sb for dipping into reserves before releasing the space
12160d485adaSDave Chinner 	 * that took us to ENOSPC.
12170d485adaSDave Chinner 	 */
12180d485adaSDave Chinner 	spin_lock(&mp->m_sb_lock);
12192229276cSDarrick J. Wong 	percpu_counter_add(counter, -delta);
12202229276cSDarrick J. Wong 	if (!has_resv_pool || !rsvd)
12210d485adaSDave Chinner 		goto fdblocks_enospc;
12220d485adaSDave Chinner 
12230d485adaSDave Chinner 	lcounter = (long long)mp->m_resblks_avail + delta;
12240d485adaSDave Chinner 	if (lcounter >= 0) {
12250d485adaSDave Chinner 		mp->m_resblks_avail = lcounter;
12260d485adaSDave Chinner 		spin_unlock(&mp->m_sb_lock);
12270d485adaSDave Chinner 		return 0;
12280d485adaSDave Chinner 	}
1229ec43f6daSEric Sandeen 	xfs_warn_once(mp,
1230ec43f6daSEric Sandeen "Reserve blocks depleted! Consider increasing reserve pool size.");
1231ec43f6daSEric Sandeen 
12320d485adaSDave Chinner fdblocks_enospc:
12330d485adaSDave Chinner 	spin_unlock(&mp->m_sb_lock);
12340d485adaSDave Chinner 	return -ENOSPC;
12350d485adaSDave Chinner }
12360d485adaSDave Chinner 
12371da177e4SLinus Torvalds /*
12381da177e4SLinus Torvalds  * Used to free the superblock along various error paths.
12391da177e4SLinus Torvalds  */
12401da177e4SLinus Torvalds void
xfs_freesb(struct xfs_mount * mp)12411da177e4SLinus Torvalds xfs_freesb(
124226af6552SDave Chinner 	struct xfs_mount	*mp)
12431da177e4SLinus Torvalds {
124426af6552SDave Chinner 	struct xfs_buf		*bp = mp->m_sb_bp;
12451da177e4SLinus Torvalds 
124626af6552SDave Chinner 	xfs_buf_lock(bp);
12471da177e4SLinus Torvalds 	mp->m_sb_bp = NULL;
124826af6552SDave Chinner 	xfs_buf_relse(bp);
12491da177e4SLinus Torvalds }
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds /*
1252dda35b8fSChristoph Hellwig  * If the underlying (data/log/rt) device is readonly, there are some
1253dda35b8fSChristoph Hellwig  * operations that cannot proceed.
1254dda35b8fSChristoph Hellwig  */
1255dda35b8fSChristoph Hellwig int
xfs_dev_is_read_only(struct xfs_mount * mp,char * message)1256dda35b8fSChristoph Hellwig xfs_dev_is_read_only(
1257dda35b8fSChristoph Hellwig 	struct xfs_mount	*mp,
1258dda35b8fSChristoph Hellwig 	char			*message)
1259dda35b8fSChristoph Hellwig {
1260dda35b8fSChristoph Hellwig 	if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
1261dda35b8fSChristoph Hellwig 	    xfs_readonly_buftarg(mp->m_logdev_targp) ||
1262dda35b8fSChristoph Hellwig 	    (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
12630b932cccSDave Chinner 		xfs_notice(mp, "%s required on read-only device.", message);
12640b932cccSDave Chinner 		xfs_notice(mp, "write access unavailable, cannot proceed.");
12652451337dSDave Chinner 		return -EROFS;
1266dda35b8fSChristoph Hellwig 	}
1267dda35b8fSChristoph Hellwig 	return 0;
1268dda35b8fSChristoph Hellwig }
1269f467cad9SDarrick J. Wong 
1270f467cad9SDarrick J. Wong /* Force the summary counters to be recalculated at next mount. */
1271f467cad9SDarrick J. Wong void
xfs_force_summary_recalc(struct xfs_mount * mp)1272f467cad9SDarrick J. Wong xfs_force_summary_recalc(
1273f467cad9SDarrick J. Wong 	struct xfs_mount	*mp)
1274f467cad9SDarrick J. Wong {
127538c26bfdSDave Chinner 	if (!xfs_has_lazysbcount(mp))
1276f467cad9SDarrick J. Wong 		return;
1277f467cad9SDarrick J. Wong 
127839353ff6SDarrick J. Wong 	xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS);
1279f467cad9SDarrick J. Wong }
12809fe82b8cSDarrick J. Wong 
12819fe82b8cSDarrick J. Wong /*
1282908ce71eSDarrick J. Wong  * Enable a log incompat feature flag in the primary superblock.  The caller
1283908ce71eSDarrick J. Wong  * cannot have any other transactions in progress.
1284908ce71eSDarrick J. Wong  */
1285908ce71eSDarrick J. Wong int
xfs_add_incompat_log_feature(struct xfs_mount * mp,uint32_t feature)1286908ce71eSDarrick J. Wong xfs_add_incompat_log_feature(
1287908ce71eSDarrick J. Wong 	struct xfs_mount	*mp,
1288908ce71eSDarrick J. Wong 	uint32_t		feature)
1289908ce71eSDarrick J. Wong {
1290908ce71eSDarrick J. Wong 	struct xfs_dsb		*dsb;
1291908ce71eSDarrick J. Wong 	int			error;
1292908ce71eSDarrick J. Wong 
1293908ce71eSDarrick J. Wong 	ASSERT(hweight32(feature) == 1);
1294908ce71eSDarrick J. Wong 	ASSERT(!(feature & XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
1295908ce71eSDarrick J. Wong 
1296908ce71eSDarrick J. Wong 	/*
1297908ce71eSDarrick J. Wong 	 * Force the log to disk and kick the background AIL thread to reduce
1298908ce71eSDarrick J. Wong 	 * the chances that the bwrite will stall waiting for the AIL to unpin
1299908ce71eSDarrick J. Wong 	 * the primary superblock buffer.  This isn't a data integrity
1300908ce71eSDarrick J. Wong 	 * operation, so we don't need a synchronous push.
1301908ce71eSDarrick J. Wong 	 */
1302908ce71eSDarrick J. Wong 	error = xfs_log_force(mp, XFS_LOG_SYNC);
1303908ce71eSDarrick J. Wong 	if (error)
1304908ce71eSDarrick J. Wong 		return error;
1305908ce71eSDarrick J. Wong 	xfs_ail_push_all(mp->m_ail);
1306908ce71eSDarrick J. Wong 
1307908ce71eSDarrick J. Wong 	/*
1308908ce71eSDarrick J. Wong 	 * Lock the primary superblock buffer to serialize all callers that
1309908ce71eSDarrick J. Wong 	 * are trying to set feature bits.
1310908ce71eSDarrick J. Wong 	 */
1311908ce71eSDarrick J. Wong 	xfs_buf_lock(mp->m_sb_bp);
1312908ce71eSDarrick J. Wong 	xfs_buf_hold(mp->m_sb_bp);
1313908ce71eSDarrick J. Wong 
131475c8c50fSDave Chinner 	if (xfs_is_shutdown(mp)) {
1315908ce71eSDarrick J. Wong 		error = -EIO;
1316908ce71eSDarrick J. Wong 		goto rele;
1317908ce71eSDarrick J. Wong 	}
1318908ce71eSDarrick J. Wong 
1319908ce71eSDarrick J. Wong 	if (xfs_sb_has_incompat_log_feature(&mp->m_sb, feature))
1320908ce71eSDarrick J. Wong 		goto rele;
1321908ce71eSDarrick J. Wong 
1322908ce71eSDarrick J. Wong 	/*
1323908ce71eSDarrick J. Wong 	 * Write the primary superblock to disk immediately, because we need
1324908ce71eSDarrick J. Wong 	 * the log_incompat bit to be set in the primary super now to protect
1325908ce71eSDarrick J. Wong 	 * the log items that we're going to commit later.
1326908ce71eSDarrick J. Wong 	 */
1327908ce71eSDarrick J. Wong 	dsb = mp->m_sb_bp->b_addr;
1328908ce71eSDarrick J. Wong 	xfs_sb_to_disk(dsb, &mp->m_sb);
1329908ce71eSDarrick J. Wong 	dsb->sb_features_log_incompat |= cpu_to_be32(feature);
1330908ce71eSDarrick J. Wong 	error = xfs_bwrite(mp->m_sb_bp);
1331908ce71eSDarrick J. Wong 	if (error)
1332908ce71eSDarrick J. Wong 		goto shutdown;
1333908ce71eSDarrick J. Wong 
1334908ce71eSDarrick J. Wong 	/*
1335908ce71eSDarrick J. Wong 	 * Add the feature bits to the incore superblock before we unlock the
1336908ce71eSDarrick J. Wong 	 * buffer.
1337908ce71eSDarrick J. Wong 	 */
1338908ce71eSDarrick J. Wong 	xfs_sb_add_incompat_log_features(&mp->m_sb, feature);
1339908ce71eSDarrick J. Wong 	xfs_buf_relse(mp->m_sb_bp);
1340908ce71eSDarrick J. Wong 
1341908ce71eSDarrick J. Wong 	/* Log the superblock to disk. */
1342908ce71eSDarrick J. Wong 	return xfs_sync_sb(mp, false);
1343908ce71eSDarrick J. Wong shutdown:
1344908ce71eSDarrick J. Wong 	xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1345908ce71eSDarrick J. Wong rele:
1346908ce71eSDarrick J. Wong 	xfs_buf_relse(mp->m_sb_bp);
1347908ce71eSDarrick J. Wong 	return error;
1348908ce71eSDarrick J. Wong }
1349908ce71eSDarrick J. Wong 
1350908ce71eSDarrick J. Wong /*
1351908ce71eSDarrick J. Wong  * Clear all the log incompat flags from the superblock.
1352908ce71eSDarrick J. Wong  *
1353908ce71eSDarrick J. Wong  * The caller cannot be in a transaction, must ensure that the log does not
1354908ce71eSDarrick J. Wong  * contain any log items protected by any log incompat bit, and must ensure
1355908ce71eSDarrick J. Wong  * that there are no other threads that depend on the state of the log incompat
1356908ce71eSDarrick J. Wong  * feature flags in the primary super.
1357908ce71eSDarrick J. Wong  *
1358908ce71eSDarrick J. Wong  * Returns true if the superblock is dirty.
1359908ce71eSDarrick J. Wong  */
1360908ce71eSDarrick J. Wong bool
xfs_clear_incompat_log_features(struct xfs_mount * mp)1361908ce71eSDarrick J. Wong xfs_clear_incompat_log_features(
1362908ce71eSDarrick J. Wong 	struct xfs_mount	*mp)
1363908ce71eSDarrick J. Wong {
1364908ce71eSDarrick J. Wong 	bool			ret = false;
1365908ce71eSDarrick J. Wong 
1366ebd9027dSDave Chinner 	if (!xfs_has_crc(mp) ||
1367908ce71eSDarrick J. Wong 	    !xfs_sb_has_incompat_log_feature(&mp->m_sb,
1368908ce71eSDarrick J. Wong 				XFS_SB_FEAT_INCOMPAT_LOG_ALL) ||
136975c8c50fSDave Chinner 	    xfs_is_shutdown(mp))
1370908ce71eSDarrick J. Wong 		return false;
1371908ce71eSDarrick J. Wong 
1372908ce71eSDarrick J. Wong 	/*
1373908ce71eSDarrick J. Wong 	 * Update the incore superblock.  We synchronize on the primary super
1374908ce71eSDarrick J. Wong 	 * buffer lock to be consistent with the add function, though at least
1375908ce71eSDarrick J. Wong 	 * in theory this shouldn't be necessary.
1376908ce71eSDarrick J. Wong 	 */
1377908ce71eSDarrick J. Wong 	xfs_buf_lock(mp->m_sb_bp);
1378908ce71eSDarrick J. Wong 	xfs_buf_hold(mp->m_sb_bp);
1379908ce71eSDarrick J. Wong 
1380908ce71eSDarrick J. Wong 	if (xfs_sb_has_incompat_log_feature(&mp->m_sb,
1381908ce71eSDarrick J. Wong 				XFS_SB_FEAT_INCOMPAT_LOG_ALL)) {
1382908ce71eSDarrick J. Wong 		xfs_sb_remove_incompat_log_features(&mp->m_sb);
1383908ce71eSDarrick J. Wong 		ret = true;
1384908ce71eSDarrick J. Wong 	}
1385908ce71eSDarrick J. Wong 
1386908ce71eSDarrick J. Wong 	xfs_buf_relse(mp->m_sb_bp);
1387908ce71eSDarrick J. Wong 	return ret;
1388908ce71eSDarrick J. Wong }
1389908ce71eSDarrick J. Wong 
1390908ce71eSDarrick J. Wong /*
13919fe82b8cSDarrick J. Wong  * Update the in-core delayed block counter.
13929fe82b8cSDarrick J. Wong  *
13939fe82b8cSDarrick J. Wong  * We prefer to update the counter without having to take a spinlock for every
13949fe82b8cSDarrick J. Wong  * counter update (i.e. batching).  Each change to delayed allocation
13959fe82b8cSDarrick J. Wong  * reservations can change can easily exceed the default percpu counter
13969fe82b8cSDarrick J. Wong  * batching, so we use a larger batch factor here.
13979fe82b8cSDarrick J. Wong  *
13989fe82b8cSDarrick J. Wong  * Note that we don't currently have any callers requiring fast summation
13999fe82b8cSDarrick J. Wong  * (e.g. percpu_counter_read) so we can use a big batch value here.
14009fe82b8cSDarrick J. Wong  */
14019fe82b8cSDarrick J. Wong #define XFS_DELALLOC_BATCH	(4096)
14029fe82b8cSDarrick J. Wong void
xfs_mod_delalloc(struct xfs_mount * mp,int64_t delta)14039fe82b8cSDarrick J. Wong xfs_mod_delalloc(
14049fe82b8cSDarrick J. Wong 	struct xfs_mount	*mp,
14059fe82b8cSDarrick J. Wong 	int64_t			delta)
14069fe82b8cSDarrick J. Wong {
14079fe82b8cSDarrick J. Wong 	percpu_counter_add_batch(&mp->m_delalloc_blks, delta,
14089fe82b8cSDarrick J. Wong 			XFS_DELALLOC_BATCH);
14099fe82b8cSDarrick J. Wong }
1410