xref: /linux/fs/xfs/xfs_mount.c (revision 595189c25c28a55523354336bf24453242c81c15)
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"
241da177e4SLinus Torvalds #include "xfs_error.h"
251da177e4SLinus Torvalds #include "xfs_quota.h"
261da177e4SLinus Torvalds #include "xfs_fsops.h"
276d8b79cfSDave Chinner #include "xfs_icache.h"
28a31b1d3dSBrian Foster #include "xfs_sysfs.h"
29035e00acSDarrick J. Wong #include "xfs_rmap_btree.h"
301946b91cSDarrick J. Wong #include "xfs_refcount_btree.h"
31174edb0eSDarrick J. Wong #include "xfs_reflink.h"
32ebf55872SChristoph Hellwig #include "xfs_extent_busy.h"
3339353ff6SDarrick J. Wong #include "xfs_health.h"
3413eaec4bSDarrick J. Wong #include "xfs_trace.h"
351da177e4SLinus Torvalds 
3627174203SChristoph Hellwig static DEFINE_MUTEX(xfs_uuid_table_mutex);
3727174203SChristoph Hellwig static int xfs_uuid_table_size;
3827174203SChristoph Hellwig static uuid_t *xfs_uuid_table;
3927174203SChristoph Hellwig 
40af3b6382SDarrick J. Wong void
41af3b6382SDarrick J. Wong xfs_uuid_table_free(void)
42af3b6382SDarrick J. Wong {
43af3b6382SDarrick J. Wong 	if (xfs_uuid_table_size == 0)
44af3b6382SDarrick J. Wong 		return;
45af3b6382SDarrick J. Wong 	kmem_free(xfs_uuid_table);
46af3b6382SDarrick J. Wong 	xfs_uuid_table = NULL;
47af3b6382SDarrick J. Wong 	xfs_uuid_table_size = 0;
48af3b6382SDarrick J. Wong }
49af3b6382SDarrick J. Wong 
5027174203SChristoph Hellwig /*
5127174203SChristoph Hellwig  * See if the UUID is unique among mounted XFS filesystems.
5227174203SChristoph Hellwig  * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
5327174203SChristoph Hellwig  */
5427174203SChristoph Hellwig STATIC int
5527174203SChristoph Hellwig xfs_uuid_mount(
5627174203SChristoph Hellwig 	struct xfs_mount	*mp)
5727174203SChristoph Hellwig {
5827174203SChristoph Hellwig 	uuid_t			*uuid = &mp->m_sb.sb_uuid;
5927174203SChristoph Hellwig 	int			hole, i;
6027174203SChristoph Hellwig 
618f720d9fSAmir Goldstein 	/* Publish UUID in struct super_block */
6285787090SChristoph Hellwig 	uuid_copy(&mp->m_super->s_uuid, uuid);
638f720d9fSAmir Goldstein 
6427174203SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_NOUUID)
6527174203SChristoph Hellwig 		return 0;
6627174203SChristoph Hellwig 
67d905fdaaSAmir Goldstein 	if (uuid_is_null(uuid)) {
68d905fdaaSAmir Goldstein 		xfs_warn(mp, "Filesystem has null UUID - can't mount");
692451337dSDave Chinner 		return -EINVAL;
7027174203SChristoph Hellwig 	}
7127174203SChristoph Hellwig 
7227174203SChristoph Hellwig 	mutex_lock(&xfs_uuid_table_mutex);
7327174203SChristoph Hellwig 	for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) {
74d905fdaaSAmir Goldstein 		if (uuid_is_null(&xfs_uuid_table[i])) {
7527174203SChristoph Hellwig 			hole = i;
7627174203SChristoph Hellwig 			continue;
7727174203SChristoph Hellwig 		}
7827174203SChristoph Hellwig 		if (uuid_equal(uuid, &xfs_uuid_table[i]))
7927174203SChristoph Hellwig 			goto out_duplicate;
8027174203SChristoph Hellwig 	}
8127174203SChristoph Hellwig 
8227174203SChristoph Hellwig 	if (hole < 0) {
83771915c4SCarlos Maiolino 		xfs_uuid_table = krealloc(xfs_uuid_table,
8427174203SChristoph Hellwig 			(xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table),
85771915c4SCarlos Maiolino 			GFP_KERNEL | __GFP_NOFAIL);
8627174203SChristoph Hellwig 		hole = xfs_uuid_table_size++;
8727174203SChristoph Hellwig 	}
8827174203SChristoph Hellwig 	xfs_uuid_table[hole] = *uuid;
8927174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
9027174203SChristoph Hellwig 
9127174203SChristoph Hellwig 	return 0;
9227174203SChristoph Hellwig 
9327174203SChristoph Hellwig  out_duplicate:
9427174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
95021000e5SMitsuo Hayasaka 	xfs_warn(mp, "Filesystem has duplicate UUID %pU - can't mount", uuid);
962451337dSDave Chinner 	return -EINVAL;
9727174203SChristoph Hellwig }
9827174203SChristoph Hellwig 
9927174203SChristoph Hellwig STATIC void
10027174203SChristoph Hellwig xfs_uuid_unmount(
10127174203SChristoph Hellwig 	struct xfs_mount	*mp)
10227174203SChristoph Hellwig {
10327174203SChristoph Hellwig 	uuid_t			*uuid = &mp->m_sb.sb_uuid;
10427174203SChristoph Hellwig 	int			i;
10527174203SChristoph Hellwig 
10627174203SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_NOUUID)
10727174203SChristoph Hellwig 		return;
10827174203SChristoph Hellwig 
10927174203SChristoph Hellwig 	mutex_lock(&xfs_uuid_table_mutex);
11027174203SChristoph Hellwig 	for (i = 0; i < xfs_uuid_table_size; i++) {
111d905fdaaSAmir Goldstein 		if (uuid_is_null(&xfs_uuid_table[i]))
11227174203SChristoph Hellwig 			continue;
11327174203SChristoph Hellwig 		if (!uuid_equal(uuid, &xfs_uuid_table[i]))
11427174203SChristoph Hellwig 			continue;
11527174203SChristoph Hellwig 		memset(&xfs_uuid_table[i], 0, sizeof(uuid_t));
11627174203SChristoph Hellwig 		break;
11727174203SChristoph Hellwig 	}
11827174203SChristoph Hellwig 	ASSERT(i < xfs_uuid_table_size);
11927174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
12027174203SChristoph Hellwig }
12127174203SChristoph Hellwig 
12227174203SChristoph Hellwig 
123e176579eSDave Chinner STATIC void
124e176579eSDave Chinner __xfs_free_perag(
125e176579eSDave Chinner 	struct rcu_head	*head)
126e176579eSDave Chinner {
127e176579eSDave Chinner 	struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head);
128e176579eSDave Chinner 
129e176579eSDave Chinner 	ASSERT(atomic_read(&pag->pag_ref) == 0);
130e176579eSDave Chinner 	kmem_free(pag);
131e176579eSDave Chinner }
132e176579eSDave Chinner 
1330fa800fbSDave Chinner /*
134e176579eSDave Chinner  * Free up the per-ag resources associated with the mount structure.
1351da177e4SLinus Torvalds  */
136c962fb79SChristoph Hellwig STATIC void
137ff4f038cSChristoph Hellwig xfs_free_perag(
138745f6919SChristoph Hellwig 	xfs_mount_t	*mp)
1391da177e4SLinus Torvalds {
1401c1c6ebcSDave Chinner 	xfs_agnumber_t	agno;
1411c1c6ebcSDave Chinner 	struct xfs_perag *pag;
1421da177e4SLinus Torvalds 
1431c1c6ebcSDave Chinner 	for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
1441c1c6ebcSDave Chinner 		spin_lock(&mp->m_perag_lock);
1451c1c6ebcSDave Chinner 		pag = radix_tree_delete(&mp->m_perag_tree, agno);
1461c1c6ebcSDave Chinner 		spin_unlock(&mp->m_perag_lock);
147e176579eSDave Chinner 		ASSERT(pag);
148f83282a8SDave Chinner 		ASSERT(atomic_read(&pag->pag_ref) == 0);
1499b247179SDarrick J. Wong 		xfs_iunlink_destroy(pag);
1506031e73aSLucas Stach 		xfs_buf_hash_destroy(pag);
151e176579eSDave Chinner 		call_rcu(&pag->rcu_head, __xfs_free_perag);
1521da177e4SLinus Torvalds 	}
1531da177e4SLinus Torvalds }
1541da177e4SLinus Torvalds 
1554cc929eeSNathan Scott /*
1564cc929eeSNathan Scott  * Check size of device based on the (data/realtime) block count.
1574cc929eeSNathan Scott  * Note: this check is used by the growfs code as well as mount.
1584cc929eeSNathan Scott  */
1594cc929eeSNathan Scott int
1604cc929eeSNathan Scott xfs_sb_validate_fsb_count(
1614cc929eeSNathan Scott 	xfs_sb_t	*sbp,
162c8ce540dSDarrick J. Wong 	uint64_t	nblocks)
1634cc929eeSNathan Scott {
1644cc929eeSNathan Scott 	ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
1654cc929eeSNathan Scott 	ASSERT(sbp->sb_blocklog >= BBSHIFT);
1664cc929eeSNathan Scott 
167d5cf09baSChristoph Hellwig 	/* Limited by ULONG_MAX of page cache index */
16809cbfeafSKirill A. Shutemov 	if (nblocks >> (PAGE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
1692451337dSDave Chinner 		return -EFBIG;
1704cc929eeSNathan Scott 	return 0;
1714cc929eeSNathan Scott }
1721da177e4SLinus Torvalds 
1731c1c6ebcSDave Chinner int
174c11e2c36SNathan Scott xfs_initialize_perag(
175c11e2c36SNathan Scott 	xfs_mount_t	*mp,
1761c1c6ebcSDave Chinner 	xfs_agnumber_t	agcount,
1771c1c6ebcSDave Chinner 	xfs_agnumber_t	*maxagi)
1781da177e4SLinus Torvalds {
1792d2194f6SCarlos Maiolino 	xfs_agnumber_t	index;
180b20fe473SBill O'Donnell 	xfs_agnumber_t	first_initialised = NULLAGNUMBER;
1811da177e4SLinus Torvalds 	xfs_perag_t	*pag;
1828b26c582SDave Chinner 	int		error = -ENOMEM;
1831da177e4SLinus Torvalds 
1841c1c6ebcSDave Chinner 	/*
1851c1c6ebcSDave Chinner 	 * Walk the current per-ag tree so we don't try to initialise AGs
1861c1c6ebcSDave Chinner 	 * that already exist (growfs case). Allocate and insert all the
1871c1c6ebcSDave Chinner 	 * AGs we don't find ready for initialisation.
1881c1c6ebcSDave Chinner 	 */
1891c1c6ebcSDave Chinner 	for (index = 0; index < agcount; index++) {
1901c1c6ebcSDave Chinner 		pag = xfs_perag_get(mp, index);
1911c1c6ebcSDave Chinner 		if (pag) {
1921c1c6ebcSDave Chinner 			xfs_perag_put(pag);
1931c1c6ebcSDave Chinner 			continue;
1941c1c6ebcSDave Chinner 		}
195fb3b504aSChristoph Hellwig 
1961c1c6ebcSDave Chinner 		pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
197*595189c2SYu Kuai 		if (!pag) {
198*595189c2SYu Kuai 			error = -ENOMEM;
199b20fe473SBill O'Donnell 			goto out_unwind_new_pags;
200*595189c2SYu Kuai 		}
201fb3b504aSChristoph Hellwig 		pag->pag_agno = index;
202fb3b504aSChristoph Hellwig 		pag->pag_mount = mp;
2031a427ab0SDave Chinner 		spin_lock_init(&pag->pag_ici_lock);
204fb3b504aSChristoph Hellwig 		INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
205*595189c2SYu Kuai 
206*595189c2SYu Kuai 		error = xfs_buf_hash_init(pag);
207*595189c2SYu Kuai 		if (error)
208b20fe473SBill O'Donnell 			goto out_free_pag;
209ebf55872SChristoph Hellwig 		init_waitqueue_head(&pag->pagb_wait);
210ff23f4afSDarrick J. Wong 		spin_lock_init(&pag->pagb_lock);
211ff23f4afSDarrick J. Wong 		pag->pagb_count = 0;
212ff23f4afSDarrick J. Wong 		pag->pagb_tree = RB_ROOT;
213fb3b504aSChristoph Hellwig 
214*595189c2SYu Kuai 		error = radix_tree_preload(GFP_NOFS);
215*595189c2SYu Kuai 		if (error)
216b20fe473SBill O'Donnell 			goto out_hash_destroy;
217fb3b504aSChristoph Hellwig 
2181c1c6ebcSDave Chinner 		spin_lock(&mp->m_perag_lock);
2191c1c6ebcSDave Chinner 		if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
220eb2e9994SAustin Kim 			WARN_ON_ONCE(1);
2211c1c6ebcSDave Chinner 			spin_unlock(&mp->m_perag_lock);
2228b26c582SDave Chinner 			radix_tree_preload_end();
2238b26c582SDave Chinner 			error = -EEXIST;
224b20fe473SBill O'Donnell 			goto out_hash_destroy;
2251c1c6ebcSDave Chinner 		}
2261c1c6ebcSDave Chinner 		spin_unlock(&mp->m_perag_lock);
2271c1c6ebcSDave Chinner 		radix_tree_preload_end();
228b20fe473SBill O'Donnell 		/* first new pag is fully initialized */
229b20fe473SBill O'Donnell 		if (first_initialised == NULLAGNUMBER)
230b20fe473SBill O'Donnell 			first_initialised = index;
2319b247179SDarrick J. Wong 		error = xfs_iunlink_init(pag);
2329b247179SDarrick J. Wong 		if (error)
2339b247179SDarrick J. Wong 			goto out_hash_destroy;
2346772c1f1SDarrick J. Wong 		spin_lock_init(&pag->pag_state_lock);
2351c1c6ebcSDave Chinner 	}
2361c1c6ebcSDave Chinner 
23712c3f05cSEric Sandeen 	index = xfs_set_inode_alloc(mp, agcount);
238fb3b504aSChristoph Hellwig 
2391c1c6ebcSDave Chinner 	if (maxagi)
2401c1c6ebcSDave Chinner 		*maxagi = index;
2418018026eSDarrick J. Wong 
2428018026eSDarrick J. Wong 	mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp);
2431c1c6ebcSDave Chinner 	return 0;
2448b26c582SDave Chinner 
245b20fe473SBill O'Donnell out_hash_destroy:
2466031e73aSLucas Stach 	xfs_buf_hash_destroy(pag);
247b20fe473SBill O'Donnell out_free_pag:
2488b26c582SDave Chinner 	kmem_free(pag);
249b20fe473SBill O'Donnell out_unwind_new_pags:
250b20fe473SBill O'Donnell 	/* unwind any prior newly initialized pags */
251b20fe473SBill O'Donnell 	for (index = first_initialised; index < agcount; index++) {
2528b26c582SDave Chinner 		pag = radix_tree_delete(&mp->m_perag_tree, index);
253b20fe473SBill O'Donnell 		if (!pag)
254b20fe473SBill O'Donnell 			break;
2556031e73aSLucas Stach 		xfs_buf_hash_destroy(pag);
2569b247179SDarrick J. Wong 		xfs_iunlink_destroy(pag);
2578b26c582SDave Chinner 		kmem_free(pag);
2588b26c582SDave Chinner 	}
2598b26c582SDave Chinner 	return error;
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds /*
2631da177e4SLinus Torvalds  * xfs_readsb
2641da177e4SLinus Torvalds  *
2651da177e4SLinus Torvalds  * Does the initial read of the superblock.
2661da177e4SLinus Torvalds  */
2671da177e4SLinus Torvalds int
268ff55068cSDave Chinner xfs_readsb(
269ff55068cSDave Chinner 	struct xfs_mount *mp,
270ff55068cSDave Chinner 	int		flags)
2711da177e4SLinus Torvalds {
2721da177e4SLinus Torvalds 	unsigned int	sector_size;
27304a1e6c5SDave Chinner 	struct xfs_buf	*bp;
27404a1e6c5SDave Chinner 	struct xfs_sb	*sbp = &mp->m_sb;
2751da177e4SLinus Torvalds 	int		error;
276af34e09dSDave Chinner 	int		loud = !(flags & XFS_MFSI_QUIET);
277daba5427SEric Sandeen 	const struct xfs_buf_ops *buf_ops;
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	ASSERT(mp->m_sb_bp == NULL);
2801da177e4SLinus Torvalds 	ASSERT(mp->m_ddev_targp != NULL);
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	/*
283daba5427SEric Sandeen 	 * For the initial read, we must guess at the sector
284daba5427SEric Sandeen 	 * size based on the block device.  It's enough to
285daba5427SEric Sandeen 	 * get the sb_sectsize out of the superblock and
286daba5427SEric Sandeen 	 * then reread with the proper length.
287daba5427SEric Sandeen 	 * We don't verify it yet, because it may not be complete.
288daba5427SEric Sandeen 	 */
289daba5427SEric Sandeen 	sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
290daba5427SEric Sandeen 	buf_ops = NULL;
291daba5427SEric Sandeen 
292daba5427SEric Sandeen 	/*
293c891c30aSBrian Foster 	 * Allocate a (locked) buffer to hold the superblock. This will be kept
294c891c30aSBrian Foster 	 * around at all times to optimize access to the superblock. Therefore,
295c891c30aSBrian Foster 	 * set XBF_NO_IOACCT to make sure it doesn't hold the buftarg count
296c891c30aSBrian Foster 	 * elevated.
2971da177e4SLinus Torvalds 	 */
29826af6552SDave Chinner reread:
299ba372674SDave Chinner 	error = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR,
300c891c30aSBrian Foster 				      BTOBB(sector_size), XBF_NO_IOACCT, &bp,
301c891c30aSBrian Foster 				      buf_ops);
302ba372674SDave Chinner 	if (error) {
303eab4e633SDave Chinner 		if (loud)
304e721f504SDave Chinner 			xfs_warn(mp, "SB validate failed with error %d.", error);
305ac75a1f7SDave Chinner 		/* bad CRC means corrupted metadata */
3062451337dSDave Chinner 		if (error == -EFSBADCRC)
3072451337dSDave Chinner 			error = -EFSCORRUPTED;
308ba372674SDave Chinner 		return error;
309eab4e633SDave Chinner 	}
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	/*
3121da177e4SLinus Torvalds 	 * Initialize the mount structure from the superblock.
3131da177e4SLinus Torvalds 	 */
3143e6e8afdSChristoph Hellwig 	xfs_sb_from_disk(sbp, bp->b_addr);
315556b8883SDave Chinner 
316556b8883SDave Chinner 	/*
317556b8883SDave Chinner 	 * If we haven't validated the superblock, do so now before we try
318556b8883SDave Chinner 	 * to check the sector size and reread the superblock appropriately.
319556b8883SDave Chinner 	 */
320556b8883SDave Chinner 	if (sbp->sb_magicnum != XFS_SB_MAGIC) {
321556b8883SDave Chinner 		if (loud)
322556b8883SDave Chinner 			xfs_warn(mp, "Invalid superblock magic number");
3232451337dSDave Chinner 		error = -EINVAL;
324556b8883SDave Chinner 		goto release_buf;
325556b8883SDave Chinner 	}
326ff55068cSDave Chinner 
3271da177e4SLinus Torvalds 	/*
3281da177e4SLinus Torvalds 	 * We must be able to do sector-sized and sector-aligned IO.
3291da177e4SLinus Torvalds 	 */
33004a1e6c5SDave Chinner 	if (sector_size > sbp->sb_sectsize) {
331af34e09dSDave Chinner 		if (loud)
332af34e09dSDave Chinner 			xfs_warn(mp, "device supports %u byte sectors (not %u)",
33304a1e6c5SDave Chinner 				sector_size, sbp->sb_sectsize);
3342451337dSDave Chinner 		error = -ENOSYS;
33526af6552SDave Chinner 		goto release_buf;
3361da177e4SLinus Torvalds 	}
3371da177e4SLinus Torvalds 
338556b8883SDave Chinner 	if (buf_ops == NULL) {
3391da177e4SLinus Torvalds 		/*
340daba5427SEric Sandeen 		 * Re-read the superblock so the buffer is correctly sized,
341daba5427SEric Sandeen 		 * and properly verified.
3421da177e4SLinus Torvalds 		 */
3431da177e4SLinus Torvalds 		xfs_buf_relse(bp);
34404a1e6c5SDave Chinner 		sector_size = sbp->sb_sectsize;
345daba5427SEric Sandeen 		buf_ops = loud ? &xfs_sb_buf_ops : &xfs_sb_quiet_buf_ops;
34626af6552SDave Chinner 		goto reread;
3471da177e4SLinus Torvalds 	}
3481da177e4SLinus Torvalds 
3495681ca40SDave Chinner 	xfs_reinit_percpu_counters(mp);
3508d280b98SDavid Chinner 
35104a1e6c5SDave Chinner 	/* no need to be quiet anymore, so reset the buf ops */
35204a1e6c5SDave Chinner 	bp->b_ops = &xfs_sb_buf_ops;
35304a1e6c5SDave Chinner 
3541da177e4SLinus Torvalds 	mp->m_sb_bp = bp;
35526af6552SDave Chinner 	xfs_buf_unlock(bp);
3561da177e4SLinus Torvalds 	return 0;
3571da177e4SLinus Torvalds 
35826af6552SDave Chinner release_buf:
3591da177e4SLinus Torvalds 	xfs_buf_relse(bp);
3601da177e4SLinus Torvalds 	return error;
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds /*
36413eaec4bSDarrick J. Wong  * If the sunit/swidth change would move the precomputed root inode value, we
36513eaec4bSDarrick J. Wong  * must reject the ondisk change because repair will stumble over that.
36613eaec4bSDarrick J. Wong  * However, we allow the mount to proceed because we never rejected this
36713eaec4bSDarrick J. Wong  * combination before.  Returns true to update the sb, false otherwise.
36813eaec4bSDarrick J. Wong  */
36913eaec4bSDarrick J. Wong static inline int
37013eaec4bSDarrick J. Wong xfs_check_new_dalign(
37113eaec4bSDarrick J. Wong 	struct xfs_mount	*mp,
37213eaec4bSDarrick J. Wong 	int			new_dalign,
37313eaec4bSDarrick J. Wong 	bool			*update_sb)
37413eaec4bSDarrick J. Wong {
37513eaec4bSDarrick J. Wong 	struct xfs_sb		*sbp = &mp->m_sb;
37613eaec4bSDarrick J. Wong 	xfs_ino_t		calc_ino;
37713eaec4bSDarrick J. Wong 
37813eaec4bSDarrick J. Wong 	calc_ino = xfs_ialloc_calc_rootino(mp, new_dalign);
37913eaec4bSDarrick J. Wong 	trace_xfs_check_new_dalign(mp, new_dalign, calc_ino);
38013eaec4bSDarrick J. Wong 
38113eaec4bSDarrick J. Wong 	if (sbp->sb_rootino == calc_ino) {
38213eaec4bSDarrick J. Wong 		*update_sb = true;
38313eaec4bSDarrick J. Wong 		return 0;
38413eaec4bSDarrick J. Wong 	}
38513eaec4bSDarrick J. Wong 
38613eaec4bSDarrick J. Wong 	xfs_warn(mp,
38713eaec4bSDarrick J. Wong "Cannot change stripe alignment; would require moving root inode.");
38813eaec4bSDarrick J. Wong 
38913eaec4bSDarrick J. Wong 	/*
39013eaec4bSDarrick J. Wong 	 * XXX: Next time we add a new incompat feature, this should start
39113eaec4bSDarrick J. Wong 	 * returning -EINVAL to fail the mount.  Until then, spit out a warning
39213eaec4bSDarrick J. Wong 	 * that we're ignoring the administrator's instructions.
39313eaec4bSDarrick J. Wong 	 */
39413eaec4bSDarrick J. Wong 	xfs_warn(mp, "Skipping superblock stripe alignment update.");
39513eaec4bSDarrick J. Wong 	*update_sb = false;
39613eaec4bSDarrick J. Wong 	return 0;
39713eaec4bSDarrick J. Wong }
39813eaec4bSDarrick J. Wong 
39913eaec4bSDarrick J. Wong /*
4004f5b1b3aSDarrick J. Wong  * If we were provided with new sunit/swidth values as mount options, make sure
4014f5b1b3aSDarrick J. Wong  * that they pass basic alignment and superblock feature checks, and convert
4024f5b1b3aSDarrick J. Wong  * them into the same units (FSB) that everything else expects.  This step
4034f5b1b3aSDarrick J. Wong  * /must/ be done before computing the inode geometry.
4041da177e4SLinus Torvalds  */
4050771fb45SEric Sandeen STATIC int
4064f5b1b3aSDarrick J. Wong xfs_validate_new_dalign(
4074f5b1b3aSDarrick J. Wong 	struct xfs_mount	*mp)
4081da177e4SLinus Torvalds {
4094f5b1b3aSDarrick J. Wong 	if (mp->m_dalign == 0)
4104f5b1b3aSDarrick J. Wong 		return 0;
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	/*
4131da177e4SLinus Torvalds 	 * If stripe unit and stripe width are not multiples
4141da177e4SLinus Torvalds 	 * of the fs blocksize turn off alignment.
4151da177e4SLinus Torvalds 	 */
4161da177e4SLinus Torvalds 	if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
4171da177e4SLinus Torvalds 	    (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
41839a45d84SJie Liu 		xfs_warn(mp,
41939a45d84SJie Liu 	"alignment check failed: sunit/swidth vs. blocksize(%d)",
4204f5b1b3aSDarrick J. Wong 			mp->m_sb.sb_blocksize);
4212451337dSDave Chinner 		return -EINVAL;
4221da177e4SLinus Torvalds 	} else {
4231da177e4SLinus Torvalds 		/*
4241da177e4SLinus Torvalds 		 * Convert the stripe unit and width to FSBs.
4251da177e4SLinus Torvalds 		 */
4261da177e4SLinus Torvalds 		mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
4274f5b1b3aSDarrick J. Wong 		if (mp->m_dalign && (mp->m_sb.sb_agblocks % mp->m_dalign)) {
42853487786SDave Chinner 			xfs_warn(mp,
42939a45d84SJie Liu 		"alignment check failed: sunit/swidth vs. agsize(%d)",
4304f5b1b3aSDarrick J. Wong 				 mp->m_sb.sb_agblocks);
4312451337dSDave Chinner 			return -EINVAL;
4321da177e4SLinus Torvalds 		} else if (mp->m_dalign) {
4331da177e4SLinus Torvalds 			mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
4341da177e4SLinus Torvalds 		} else {
43539a45d84SJie Liu 			xfs_warn(mp,
43639a45d84SJie Liu 		"alignment check failed: sunit(%d) less than bsize(%d)",
4374f5b1b3aSDarrick J. Wong 				 mp->m_dalign, mp->m_sb.sb_blocksize);
4382451337dSDave Chinner 			return -EINVAL;
4391da177e4SLinus Torvalds 		}
4401da177e4SLinus Torvalds 	}
4411da177e4SLinus Torvalds 
4424f5b1b3aSDarrick J. Wong 	if (!xfs_sb_version_hasdalign(&mp->m_sb)) {
44334d7f603SJie Liu 		xfs_warn(mp,
44434d7f603SJie Liu "cannot change alignment: superblock does not support data alignment");
4452451337dSDave Chinner 		return -EINVAL;
4461da177e4SLinus Torvalds 	}
4474f5b1b3aSDarrick J. Wong 
4484f5b1b3aSDarrick J. Wong 	return 0;
4494f5b1b3aSDarrick J. Wong }
4504f5b1b3aSDarrick J. Wong 
4514f5b1b3aSDarrick J. Wong /* Update alignment values based on mount options and sb values. */
4524f5b1b3aSDarrick J. Wong STATIC int
4534f5b1b3aSDarrick J. Wong xfs_update_alignment(
4544f5b1b3aSDarrick J. Wong 	struct xfs_mount	*mp)
4554f5b1b3aSDarrick J. Wong {
4564f5b1b3aSDarrick J. Wong 	struct xfs_sb		*sbp = &mp->m_sb;
4574f5b1b3aSDarrick J. Wong 
4584f5b1b3aSDarrick J. Wong 	if (mp->m_dalign) {
45913eaec4bSDarrick J. Wong 		bool		update_sb;
46013eaec4bSDarrick J. Wong 		int		error;
46113eaec4bSDarrick J. Wong 
4624f5b1b3aSDarrick J. Wong 		if (sbp->sb_unit == mp->m_dalign &&
4634f5b1b3aSDarrick J. Wong 		    sbp->sb_width == mp->m_swidth)
4644f5b1b3aSDarrick J. Wong 			return 0;
4654f5b1b3aSDarrick J. Wong 
46613eaec4bSDarrick J. Wong 		error = xfs_check_new_dalign(mp, mp->m_dalign, &update_sb);
46713eaec4bSDarrick J. Wong 		if (error || !update_sb)
46813eaec4bSDarrick J. Wong 			return error;
46913eaec4bSDarrick J. Wong 
4704f5b1b3aSDarrick J. Wong 		sbp->sb_unit = mp->m_dalign;
4714f5b1b3aSDarrick J. Wong 		sbp->sb_width = mp->m_swidth;
4724f5b1b3aSDarrick J. Wong 		mp->m_update_sb = true;
4731da177e4SLinus Torvalds 	} else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
47462118709SEric Sandeen 		    xfs_sb_version_hasdalign(&mp->m_sb)) {
4751da177e4SLinus Torvalds 		mp->m_dalign = sbp->sb_unit;
4761da177e4SLinus Torvalds 		mp->m_swidth = sbp->sb_width;
4771da177e4SLinus Torvalds 	}
4781da177e4SLinus Torvalds 
4790771fb45SEric Sandeen 	return 0;
4800771fb45SEric Sandeen }
4811da177e4SLinus Torvalds 
4820771fb45SEric Sandeen /*
483055388a3SDave Chinner  * precalculate the low space thresholds for dynamic speculative preallocation.
484055388a3SDave Chinner  */
485055388a3SDave Chinner void
486055388a3SDave Chinner xfs_set_low_space_thresholds(
487055388a3SDave Chinner 	struct xfs_mount	*mp)
488055388a3SDave Chinner {
489055388a3SDave Chinner 	int i;
490055388a3SDave Chinner 
491055388a3SDave Chinner 	for (i = 0; i < XFS_LOWSP_MAX; i++) {
492c8ce540dSDarrick J. Wong 		uint64_t space = mp->m_sb.sb_dblocks;
493055388a3SDave Chinner 
494055388a3SDave Chinner 		do_div(space, 100);
495055388a3SDave Chinner 		mp->m_low_space[i] = space * (i + 1);
496055388a3SDave Chinner 	}
497055388a3SDave Chinner }
498055388a3SDave Chinner 
4991da177e4SLinus Torvalds /*
5000471f62eSZhi Yong Wu  * Check that the data (and log if separate) is an ok size.
5011da177e4SLinus Torvalds  */
5020771fb45SEric Sandeen STATIC int
503ba372674SDave Chinner xfs_check_sizes(
504ba372674SDave Chinner 	struct xfs_mount *mp)
5050771fb45SEric Sandeen {
506ba372674SDave Chinner 	struct xfs_buf	*bp;
5070771fb45SEric Sandeen 	xfs_daddr_t	d;
508ba372674SDave Chinner 	int		error;
5090771fb45SEric Sandeen 
5101da177e4SLinus Torvalds 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
5111da177e4SLinus Torvalds 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
5120b932cccSDave Chinner 		xfs_warn(mp, "filesystem size mismatch detected");
5132451337dSDave Chinner 		return -EFBIG;
5141da177e4SLinus Torvalds 	}
515ba372674SDave Chinner 	error = xfs_buf_read_uncached(mp->m_ddev_targp,
5161da177e4SLinus Torvalds 					d - XFS_FSS_TO_BB(mp, 1),
517ba372674SDave Chinner 					XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL);
518ba372674SDave Chinner 	if (error) {
5190b932cccSDave Chinner 		xfs_warn(mp, "last sector read failed");
520ba372674SDave Chinner 		return error;
5211da177e4SLinus Torvalds 	}
5221922c949SDave Chinner 	xfs_buf_relse(bp);
5231da177e4SLinus Torvalds 
524ba372674SDave Chinner 	if (mp->m_logdev_targp == mp->m_ddev_targp)
525ba372674SDave Chinner 		return 0;
526ba372674SDave Chinner 
5271da177e4SLinus Torvalds 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
5281da177e4SLinus Torvalds 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
5290b932cccSDave Chinner 		xfs_warn(mp, "log size mismatch detected");
5302451337dSDave Chinner 		return -EFBIG;
5311da177e4SLinus Torvalds 	}
532ba372674SDave Chinner 	error = xfs_buf_read_uncached(mp->m_logdev_targp,
5331da177e4SLinus Torvalds 					d - XFS_FSB_TO_BB(mp, 1),
534ba372674SDave Chinner 					XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
535ba372674SDave Chinner 	if (error) {
5360b932cccSDave Chinner 		xfs_warn(mp, "log device read failed");
537ba372674SDave Chinner 		return error;
5381da177e4SLinus Torvalds 	}
5391922c949SDave Chinner 	xfs_buf_relse(bp);
5400771fb45SEric Sandeen 	return 0;
5410771fb45SEric Sandeen }
5420771fb45SEric Sandeen 
5430771fb45SEric Sandeen /*
5447d095257SChristoph Hellwig  * Clear the quotaflags in memory and in the superblock.
5457d095257SChristoph Hellwig  */
5467d095257SChristoph Hellwig int
5477d095257SChristoph Hellwig xfs_mount_reset_sbqflags(
5487d095257SChristoph Hellwig 	struct xfs_mount	*mp)
5497d095257SChristoph Hellwig {
5507d095257SChristoph Hellwig 	mp->m_qflags = 0;
5517d095257SChristoph Hellwig 
55261e63ecbSDave Chinner 	/* It is OK to look at sb_qflags in the mount path without m_sb_lock. */
5537d095257SChristoph Hellwig 	if (mp->m_sb.sb_qflags == 0)
5547d095257SChristoph Hellwig 		return 0;
5557d095257SChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
5567d095257SChristoph Hellwig 	mp->m_sb.sb_qflags = 0;
5577d095257SChristoph Hellwig 	spin_unlock(&mp->m_sb_lock);
5587d095257SChristoph Hellwig 
55961e63ecbSDave Chinner 	if (!xfs_fs_writable(mp, SB_FREEZE_WRITE))
5607d095257SChristoph Hellwig 		return 0;
5617d095257SChristoph Hellwig 
56261e63ecbSDave Chinner 	return xfs_sync_sb(mp, false);
5637d095257SChristoph Hellwig }
5647d095257SChristoph Hellwig 
565c8ce540dSDarrick J. Wong uint64_t
566d5db0f97SEric Sandeen xfs_default_resblks(xfs_mount_t *mp)
567d5db0f97SEric Sandeen {
568c8ce540dSDarrick J. Wong 	uint64_t resblks;
569d5db0f97SEric Sandeen 
570d5db0f97SEric Sandeen 	/*
5718babd8a2SDave Chinner 	 * We default to 5% or 8192 fsbs of space reserved, whichever is
5728babd8a2SDave Chinner 	 * smaller.  This is intended to cover concurrent allocation
5738babd8a2SDave Chinner 	 * transactions when we initially hit enospc. These each require a 4
5748babd8a2SDave Chinner 	 * block reservation. Hence by default we cover roughly 2000 concurrent
5758babd8a2SDave Chinner 	 * allocation reservations.
576d5db0f97SEric Sandeen 	 */
577d5db0f97SEric Sandeen 	resblks = mp->m_sb.sb_dblocks;
578d5db0f97SEric Sandeen 	do_div(resblks, 20);
579c8ce540dSDarrick J. Wong 	resblks = min_t(uint64_t, resblks, 8192);
580d5db0f97SEric Sandeen 	return resblks;
581d5db0f97SEric Sandeen }
582d5db0f97SEric Sandeen 
5832e9e6481SDarrick J. Wong /* Ensure the summary counts are correct. */
5842e9e6481SDarrick J. Wong STATIC int
5852e9e6481SDarrick J. Wong xfs_check_summary_counts(
5862e9e6481SDarrick J. Wong 	struct xfs_mount	*mp)
5872e9e6481SDarrick J. Wong {
5882e9e6481SDarrick J. Wong 	/*
5892e9e6481SDarrick J. Wong 	 * The AG0 superblock verifier rejects in-progress filesystems,
5902e9e6481SDarrick J. Wong 	 * so we should never see the flag set this far into mounting.
5912e9e6481SDarrick J. Wong 	 */
5922e9e6481SDarrick J. Wong 	if (mp->m_sb.sb_inprogress) {
5932e9e6481SDarrick J. Wong 		xfs_err(mp, "sb_inprogress set after log recovery??");
5942e9e6481SDarrick J. Wong 		WARN_ON(1);
5952e9e6481SDarrick J. Wong 		return -EFSCORRUPTED;
5962e9e6481SDarrick J. Wong 	}
5972e9e6481SDarrick J. Wong 
5982e9e6481SDarrick J. Wong 	/*
5992e9e6481SDarrick J. Wong 	 * Now the log is mounted, we know if it was an unclean shutdown or
6002e9e6481SDarrick J. Wong 	 * not. If it was, with the first phase of recovery has completed, we
6012e9e6481SDarrick J. Wong 	 * have consistent AG blocks on disk. We have not recovered EFIs yet,
6022e9e6481SDarrick J. Wong 	 * but they are recovered transactionally in the second recovery phase
6032e9e6481SDarrick J. Wong 	 * later.
6042e9e6481SDarrick J. Wong 	 *
6052e9e6481SDarrick J. Wong 	 * If the log was clean when we mounted, we can check the summary
6062e9e6481SDarrick J. Wong 	 * counters.  If any of them are obviously incorrect, we can recompute
6072e9e6481SDarrick J. Wong 	 * them from the AGF headers in the next step.
6082e9e6481SDarrick J. Wong 	 */
6092e9e6481SDarrick J. Wong 	if (XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
6102e9e6481SDarrick J. Wong 	    (mp->m_sb.sb_fdblocks > mp->m_sb.sb_dblocks ||
61100d22a1cSDarrick J. Wong 	     !xfs_verify_icount(mp, mp->m_sb.sb_icount) ||
6122e9e6481SDarrick J. Wong 	     mp->m_sb.sb_ifree > mp->m_sb.sb_icount))
61339353ff6SDarrick J. Wong 		xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS);
6142e9e6481SDarrick J. Wong 
6152e9e6481SDarrick J. Wong 	/*
6162e9e6481SDarrick J. Wong 	 * We can safely re-initialise incore superblock counters from the
6172e9e6481SDarrick J. Wong 	 * per-ag data. These may not be correct if the filesystem was not
6182e9e6481SDarrick J. Wong 	 * cleanly unmounted, so we waited for recovery to finish before doing
6192e9e6481SDarrick J. Wong 	 * this.
6202e9e6481SDarrick J. Wong 	 *
6212e9e6481SDarrick J. Wong 	 * If the filesystem was cleanly unmounted or the previous check did
6222e9e6481SDarrick J. Wong 	 * not flag anything weird, then we can trust the values in the
6232e9e6481SDarrick J. Wong 	 * superblock to be correct and we don't need to do anything here.
6242e9e6481SDarrick J. Wong 	 * Otherwise, recalculate the summary counters.
6252e9e6481SDarrick J. Wong 	 */
6262e9e6481SDarrick J. Wong 	if ((!xfs_sb_version_haslazysbcount(&mp->m_sb) ||
6272e9e6481SDarrick J. Wong 	     XFS_LAST_UNMOUNT_WAS_CLEAN(mp)) &&
62839353ff6SDarrick J. Wong 	    !xfs_fs_has_sickness(mp, XFS_SICK_FS_COUNTERS))
6292e9e6481SDarrick J. Wong 		return 0;
6302e9e6481SDarrick J. Wong 
6312e9e6481SDarrick J. Wong 	return xfs_initialize_perag_data(mp, mp->m_sb.sb_agcount);
6322e9e6481SDarrick J. Wong }
6332e9e6481SDarrick J. Wong 
6347d095257SChristoph Hellwig /*
6350771fb45SEric Sandeen  * This function does the following on an initial mount of a file system:
6360771fb45SEric Sandeen  *	- reads the superblock from disk and init the mount struct
6370771fb45SEric Sandeen  *	- if we're a 32-bit kernel, do a size check on the superblock
6380771fb45SEric Sandeen  *		so we don't mount terabyte filesystems
6390771fb45SEric Sandeen  *	- init mount struct realtime fields
6400771fb45SEric Sandeen  *	- allocate inode hash table for fs
6410771fb45SEric Sandeen  *	- init directory manager
6420771fb45SEric Sandeen  *	- perform recovery and init the log manager
6430771fb45SEric Sandeen  */
6440771fb45SEric Sandeen int
6450771fb45SEric Sandeen xfs_mountfs(
646f0b2efadSBrian Foster 	struct xfs_mount	*mp)
6470771fb45SEric Sandeen {
648f0b2efadSBrian Foster 	struct xfs_sb		*sbp = &(mp->m_sb);
649f0b2efadSBrian Foster 	struct xfs_inode	*rip;
650ef325959SDarrick J. Wong 	struct xfs_ino_geometry	*igeo = M_IGEO(mp);
651c8ce540dSDarrick J. Wong 	uint64_t		resblks;
6527d095257SChristoph Hellwig 	uint			quotamount = 0;
6537d095257SChristoph Hellwig 	uint			quotaflags = 0;
6540771fb45SEric Sandeen 	int			error = 0;
6550771fb45SEric Sandeen 
656ff55068cSDave Chinner 	xfs_sb_mount_common(mp, sbp);
6570771fb45SEric Sandeen 
6580771fb45SEric Sandeen 	/*
659074e427bSDave Chinner 	 * Check for a mismatched features2 values.  Older kernels read & wrote
660074e427bSDave Chinner 	 * into the wrong sb offset for sb_features2 on some platforms due to
661074e427bSDave Chinner 	 * xfs_sb_t not being 64bit size aligned when sb_features2 was added,
662074e427bSDave Chinner 	 * which made older superblock reading/writing routines swap it as a
663074e427bSDave Chinner 	 * 64-bit value.
664ee1c0908SDavid Chinner 	 *
665e6957ea4SEric Sandeen 	 * For backwards compatibility, we make both slots equal.
666e6957ea4SEric Sandeen 	 *
667074e427bSDave Chinner 	 * If we detect a mismatched field, we OR the set bits into the existing
668074e427bSDave Chinner 	 * features2 field in case it has already been modified; we don't want
669074e427bSDave Chinner 	 * to lose any features.  We then update the bad location with the ORed
670074e427bSDave Chinner 	 * value so that older kernels will see any features2 flags. The
671074e427bSDave Chinner 	 * superblock writeback code ensures the new sb_features2 is copied to
672074e427bSDave Chinner 	 * sb_bad_features2 before it is logged or written to disk.
673ee1c0908SDavid Chinner 	 */
674e6957ea4SEric Sandeen 	if (xfs_sb_has_mismatched_features2(sbp)) {
6750b932cccSDave Chinner 		xfs_warn(mp, "correcting sb_features alignment problem");
676ee1c0908SDavid Chinner 		sbp->sb_features2 |= sbp->sb_bad_features2;
67761e63ecbSDave Chinner 		mp->m_update_sb = true;
678e6957ea4SEric Sandeen 
679e6957ea4SEric Sandeen 		/*
680e6957ea4SEric Sandeen 		 * Re-check for ATTR2 in case it was found in bad_features2
681e6957ea4SEric Sandeen 		 * slot.
682e6957ea4SEric Sandeen 		 */
6837c12f296STim Shimmin 		if (xfs_sb_version_hasattr2(&mp->m_sb) &&
6847c12f296STim Shimmin 		   !(mp->m_flags & XFS_MOUNT_NOATTR2))
685e6957ea4SEric Sandeen 			mp->m_flags |= XFS_MOUNT_ATTR2;
6867c12f296STim Shimmin 	}
687e6957ea4SEric Sandeen 
6887c12f296STim Shimmin 	if (xfs_sb_version_hasattr2(&mp->m_sb) &&
6897c12f296STim Shimmin 	   (mp->m_flags & XFS_MOUNT_NOATTR2)) {
6907c12f296STim Shimmin 		xfs_sb_version_removeattr2(&mp->m_sb);
69161e63ecbSDave Chinner 		mp->m_update_sb = true;
6927c12f296STim Shimmin 
6937c12f296STim Shimmin 		/* update sb_versionnum for the clearing of the morebits */
6947c12f296STim Shimmin 		if (!sbp->sb_features2)
69561e63ecbSDave Chinner 			mp->m_update_sb = true;
696ee1c0908SDavid Chinner 	}
697ee1c0908SDavid Chinner 
698263997a6SDave Chinner 	/* always use v2 inodes by default now */
699263997a6SDave Chinner 	if (!(mp->m_sb.sb_versionnum & XFS_SB_VERSION_NLINKBIT)) {
700263997a6SDave Chinner 		mp->m_sb.sb_versionnum |= XFS_SB_VERSION_NLINKBIT;
70161e63ecbSDave Chinner 		mp->m_update_sb = true;
702263997a6SDave Chinner 	}
703263997a6SDave Chinner 
704ee1c0908SDavid Chinner 	/*
7054f5b1b3aSDarrick J. Wong 	 * If we were given new sunit/swidth options, do some basic validation
7064f5b1b3aSDarrick J. Wong 	 * checks and convert the incore dalign and swidth values to the
7074f5b1b3aSDarrick J. Wong 	 * same units (FSB) that everything else uses.  This /must/ happen
7084f5b1b3aSDarrick J. Wong 	 * before computing the inode geometry.
7090771fb45SEric Sandeen 	 */
7104f5b1b3aSDarrick J. Wong 	error = xfs_validate_new_dalign(mp);
7110771fb45SEric Sandeen 	if (error)
712f9057e3dSChristoph Hellwig 		goto out;
7130771fb45SEric Sandeen 
7140771fb45SEric Sandeen 	xfs_alloc_compute_maxlevels(mp);
7150771fb45SEric Sandeen 	xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
7160771fb45SEric Sandeen 	xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
717494dba7bSDarrick J. Wong 	xfs_ialloc_setup_geometry(mp);
718035e00acSDarrick J. Wong 	xfs_rmapbt_compute_maxlevels(mp);
7191946b91cSDarrick J. Wong 	xfs_refcountbt_compute_maxlevels(mp);
7200771fb45SEric Sandeen 
7214f5b1b3aSDarrick J. Wong 	/*
7224f5b1b3aSDarrick J. Wong 	 * Check if sb_agblocks is aligned at stripe boundary.  If sb_agblocks
7234f5b1b3aSDarrick J. Wong 	 * is NOT aligned turn off m_dalign since allocator alignment is within
7244f5b1b3aSDarrick J. Wong 	 * an ag, therefore ag has to be aligned at stripe boundary.  Note that
7254f5b1b3aSDarrick J. Wong 	 * we must compute the free space and rmap btree geometry before doing
7264f5b1b3aSDarrick J. Wong 	 * this.
7274f5b1b3aSDarrick J. Wong 	 */
7284f5b1b3aSDarrick J. Wong 	error = xfs_update_alignment(mp);
7294f5b1b3aSDarrick J. Wong 	if (error)
7304f5b1b3aSDarrick J. Wong 		goto out;
7314f5b1b3aSDarrick J. Wong 
732e6b3bb78SCarlos Maiolino 	/* enable fail_at_unmount as default */
733749f24f3SThomas Meyer 	mp->m_fail_unmount = true;
734e6b3bb78SCarlos Maiolino 
735e1d3d218SIan Kent 	error = xfs_sysfs_init(&mp->m_kobj, &xfs_mp_ktype,
736e1d3d218SIan Kent 			       NULL, mp->m_super->s_id);
73727174203SChristoph Hellwig 	if (error)
738f9057e3dSChristoph Hellwig 		goto out;
7391da177e4SLinus Torvalds 
740225e4635SBill O'Donnell 	error = xfs_sysfs_init(&mp->m_stats.xs_kobj, &xfs_stats_ktype,
741225e4635SBill O'Donnell 			       &mp->m_kobj, "stats");
742a31b1d3dSBrian Foster 	if (error)
743a31b1d3dSBrian Foster 		goto out_remove_sysfs;
744a31b1d3dSBrian Foster 
745192852beSCarlos Maiolino 	error = xfs_error_sysfs_init(mp);
746225e4635SBill O'Donnell 	if (error)
747225e4635SBill O'Donnell 		goto out_del_stats;
748225e4635SBill O'Donnell 
74931965ef3SDarrick J. Wong 	error = xfs_errortag_init(mp);
75031965ef3SDarrick J. Wong 	if (error)
75131965ef3SDarrick J. Wong 		goto out_remove_error_sysfs;
752192852beSCarlos Maiolino 
753192852beSCarlos Maiolino 	error = xfs_uuid_mount(mp);
754192852beSCarlos Maiolino 	if (error)
75531965ef3SDarrick J. Wong 		goto out_remove_errortag;
756192852beSCarlos Maiolino 
7571da177e4SLinus Torvalds 	/*
7582fcddee8SChristoph Hellwig 	 * Update the preferred write size based on the information from the
7592fcddee8SChristoph Hellwig 	 * on-disk superblock.
7600771fb45SEric Sandeen 	 */
7612fcddee8SChristoph Hellwig 	mp->m_allocsize_log =
7622fcddee8SChristoph Hellwig 		max_t(uint32_t, sbp->sb_blocklog, mp->m_allocsize_log);
7632fcddee8SChristoph Hellwig 	mp->m_allocsize_blocks = 1U << (mp->m_allocsize_log - sbp->sb_blocklog);
7640771fb45SEric Sandeen 
765055388a3SDave Chinner 	/* set the low space thresholds for dynamic preallocation */
766055388a3SDave Chinner 	xfs_set_low_space_thresholds(mp);
767055388a3SDave Chinner 
7680771fb45SEric Sandeen 	/*
769e5376fc1SBrian Foster 	 * If enabled, sparse inode chunk alignment is expected to match the
770e5376fc1SBrian Foster 	 * cluster size. Full inode chunk alignment must match the chunk size,
771e5376fc1SBrian Foster 	 * but that is checked on sb read verification...
772e5376fc1SBrian Foster 	 */
773e5376fc1SBrian Foster 	if (xfs_sb_version_hassparseinodes(&mp->m_sb) &&
774e5376fc1SBrian Foster 	    mp->m_sb.sb_spino_align !=
775490d451fSDarrick J. Wong 			XFS_B_TO_FSBT(mp, igeo->inode_cluster_size_raw)) {
776e5376fc1SBrian Foster 		xfs_warn(mp,
777e5376fc1SBrian Foster 	"Sparse inode block alignment (%u) must match cluster size (%llu).",
778e5376fc1SBrian Foster 			 mp->m_sb.sb_spino_align,
779490d451fSDarrick J. Wong 			 XFS_B_TO_FSBT(mp, igeo->inode_cluster_size_raw));
780e5376fc1SBrian Foster 		error = -EINVAL;
781e5376fc1SBrian Foster 		goto out_remove_uuid;
782e5376fc1SBrian Foster 	}
783e5376fc1SBrian Foster 
784e5376fc1SBrian Foster 	/*
785c2bfbc9bSZhi Yong Wu 	 * Check that the data (and log if separate) is an ok size.
7860771fb45SEric Sandeen 	 */
7874249023aSChristoph Hellwig 	error = xfs_check_sizes(mp);
7880771fb45SEric Sandeen 	if (error)
789f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
7900771fb45SEric Sandeen 
7910771fb45SEric Sandeen 	/*
7921da177e4SLinus Torvalds 	 * Initialize realtime fields in the mount structure
7931da177e4SLinus Torvalds 	 */
7940771fb45SEric Sandeen 	error = xfs_rtmount_init(mp);
7950771fb45SEric Sandeen 	if (error) {
7960b932cccSDave Chinner 		xfs_warn(mp, "RT mount failed");
797f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
7981da177e4SLinus Torvalds 	}
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 	/*
8011da177e4SLinus Torvalds 	 *  Copies the low order bits of the timestamp and the randomly
8021da177e4SLinus Torvalds 	 *  set "sequence" number out of a UUID.
8031da177e4SLinus Torvalds 	 */
804cb0ba6ccSChristoph Hellwig 	mp->m_fixedfsid[0] =
805cb0ba6ccSChristoph Hellwig 		(get_unaligned_be16(&sbp->sb_uuid.b[8]) << 16) |
806cb0ba6ccSChristoph Hellwig 		 get_unaligned_be16(&sbp->sb_uuid.b[4]);
807cb0ba6ccSChristoph Hellwig 	mp->m_fixedfsid[1] = get_unaligned_be32(&sbp->sb_uuid.b[0]);
8081da177e4SLinus Torvalds 
8090650b554SDave Chinner 	error = xfs_da_mount(mp);
8100650b554SDave Chinner 	if (error) {
8110650b554SDave Chinner 		xfs_warn(mp, "Failed dir/attr init: %d", error);
8120650b554SDave Chinner 		goto out_remove_uuid;
8130650b554SDave Chinner 	}
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	/*
8161da177e4SLinus Torvalds 	 * Initialize the precomputed transaction reservations values.
8171da177e4SLinus Torvalds 	 */
8181da177e4SLinus Torvalds 	xfs_trans_init(mp);
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 	/*
8211da177e4SLinus Torvalds 	 * Allocate and initialize the per-ag data.
8221da177e4SLinus Torvalds 	 */
8231c1c6ebcSDave Chinner 	error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
8241c1c6ebcSDave Chinner 	if (error) {
8250b932cccSDave Chinner 		xfs_warn(mp, "Failed per-ag init: %d", error);
8260650b554SDave Chinner 		goto out_free_dir;
8271c1c6ebcSDave Chinner 	}
8281da177e4SLinus Torvalds 
829a71895c5SDarrick J. Wong 	if (XFS_IS_CORRUPT(mp, !sbp->sb_logblocks)) {
8300b932cccSDave Chinner 		xfs_warn(mp, "no log defined");
8312451337dSDave Chinner 		error = -EFSCORRUPTED;
832f9057e3dSChristoph Hellwig 		goto out_free_perag;
833f9057e3dSChristoph Hellwig 	}
834f9057e3dSChristoph Hellwig 
8351da177e4SLinus Torvalds 	/*
836f0b2efadSBrian Foster 	 * Log's mount-time initialization. The first part of recovery can place
837f0b2efadSBrian Foster 	 * some items on the AIL, to be handled when recovery is finished or
838f0b2efadSBrian Foster 	 * cancelled.
8391da177e4SLinus Torvalds 	 */
8401da177e4SLinus Torvalds 	error = xfs_log_mount(mp, mp->m_logdev_targp,
8411da177e4SLinus Torvalds 			      XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
8421da177e4SLinus Torvalds 			      XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
8431da177e4SLinus Torvalds 	if (error) {
8440b932cccSDave Chinner 		xfs_warn(mp, "log mount failed");
845d4f3512bSDave Chinner 		goto out_fail_wait;
8461da177e4SLinus Torvalds 	}
8471da177e4SLinus Torvalds 
8482e9e6481SDarrick J. Wong 	/* Make sure the summary counts are ok. */
8492e9e6481SDarrick J. Wong 	error = xfs_check_summary_counts(mp);
850f9057e3dSChristoph Hellwig 	if (error)
8516eee8972Skbuild test robot 		goto out_log_dealloc;
852f9057e3dSChristoph Hellwig 
85392821e2bSDavid Chinner 	/*
8541da177e4SLinus Torvalds 	 * Get and sanity-check the root inode.
8551da177e4SLinus Torvalds 	 * Save the pointer to it in the mount structure.
8561da177e4SLinus Torvalds 	 */
857541b5accSDave Chinner 	error = xfs_iget(mp, NULL, sbp->sb_rootino, XFS_IGET_UNTRUSTED,
858541b5accSDave Chinner 			 XFS_ILOCK_EXCL, &rip);
8591da177e4SLinus Torvalds 	if (error) {
860541b5accSDave Chinner 		xfs_warn(mp,
861541b5accSDave Chinner 			"Failed to read root inode 0x%llx, error %d",
862541b5accSDave Chinner 			sbp->sb_rootino, -error);
863f9057e3dSChristoph Hellwig 		goto out_log_dealloc;
8641da177e4SLinus Torvalds 	}
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	ASSERT(rip != NULL);
8671da177e4SLinus Torvalds 
868a71895c5SDarrick J. Wong 	if (XFS_IS_CORRUPT(mp, !S_ISDIR(VFS_I(rip)->i_mode))) {
8690b932cccSDave Chinner 		xfs_warn(mp, "corrupted root inode %llu: not a directory",
870b6574520SNathan Scott 			(unsigned long long)rip->i_ino);
8711da177e4SLinus Torvalds 		xfs_iunlock(rip, XFS_ILOCK_EXCL);
8722451337dSDave Chinner 		error = -EFSCORRUPTED;
873f9057e3dSChristoph Hellwig 		goto out_rele_rip;
8741da177e4SLinus Torvalds 	}
8751da177e4SLinus Torvalds 	mp->m_rootip = rip;	/* save it */
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	xfs_iunlock(rip, XFS_ILOCK_EXCL);
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	/*
8801da177e4SLinus Torvalds 	 * Initialize realtime inode pointers in the mount structure
8811da177e4SLinus Torvalds 	 */
8820771fb45SEric Sandeen 	error = xfs_rtmount_inodes(mp);
8830771fb45SEric Sandeen 	if (error) {
8841da177e4SLinus Torvalds 		/*
8851da177e4SLinus Torvalds 		 * Free up the root inode.
8861da177e4SLinus Torvalds 		 */
8870b932cccSDave Chinner 		xfs_warn(mp, "failed to read RT inodes");
888f9057e3dSChristoph Hellwig 		goto out_rele_rip;
8891da177e4SLinus Torvalds 	}
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	/*
8927884bc86SChristoph Hellwig 	 * If this is a read-only mount defer the superblock updates until
8937884bc86SChristoph Hellwig 	 * the next remount into writeable mode.  Otherwise we would never
8947884bc86SChristoph Hellwig 	 * perform the update e.g. for the root filesystem.
8951da177e4SLinus Torvalds 	 */
89661e63ecbSDave Chinner 	if (mp->m_update_sb && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
89761e63ecbSDave Chinner 		error = xfs_sync_sb(mp, false);
898e5720eecSDavid Chinner 		if (error) {
8990b932cccSDave Chinner 			xfs_warn(mp, "failed to write sb changes");
900b93b6e43SChristoph Hellwig 			goto out_rtunmount;
901e5720eecSDavid Chinner 		}
902e5720eecSDavid Chinner 	}
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 	/*
9051da177e4SLinus Torvalds 	 * Initialise the XFS quota management subsystem for this mount
9061da177e4SLinus Torvalds 	 */
9077d095257SChristoph Hellwig 	if (XFS_IS_QUOTA_RUNNING(mp)) {
9087d095257SChristoph Hellwig 		error = xfs_qm_newmount(mp, &quotamount, &quotaflags);
9090771fb45SEric Sandeen 		if (error)
910b93b6e43SChristoph Hellwig 			goto out_rtunmount;
9117d095257SChristoph Hellwig 	} else {
9127d095257SChristoph Hellwig 		ASSERT(!XFS_IS_QUOTA_ON(mp));
9137d095257SChristoph Hellwig 
9147d095257SChristoph Hellwig 		/*
9157d095257SChristoph Hellwig 		 * If a file system had quotas running earlier, but decided to
9167d095257SChristoph Hellwig 		 * mount without -o uquota/pquota/gquota options, revoke the
9177d095257SChristoph Hellwig 		 * quotachecked license.
9187d095257SChristoph Hellwig 		 */
9197d095257SChristoph Hellwig 		if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) {
9200b932cccSDave Chinner 			xfs_notice(mp, "resetting quota flags");
9217d095257SChristoph Hellwig 			error = xfs_mount_reset_sbqflags(mp);
9227d095257SChristoph Hellwig 			if (error)
923a70a4fa5SBrian Foster 				goto out_rtunmount;
9247d095257SChristoph Hellwig 		}
9257d095257SChristoph Hellwig 	}
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	/*
928f0b2efadSBrian Foster 	 * Finish recovering the file system.  This part needed to be delayed
929f0b2efadSBrian Foster 	 * until after the root and real-time bitmap inodes were consistently
930f0b2efadSBrian Foster 	 * read in.
9311da177e4SLinus Torvalds 	 */
9324249023aSChristoph Hellwig 	error = xfs_log_mount_finish(mp);
9331da177e4SLinus Torvalds 	if (error) {
9340b932cccSDave Chinner 		xfs_warn(mp, "log mount finish failed");
935b93b6e43SChristoph Hellwig 		goto out_rtunmount;
9361da177e4SLinus Torvalds 	}
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 	/*
939ddeb14f4SDave Chinner 	 * Now the log is fully replayed, we can transition to full read-only
940ddeb14f4SDave Chinner 	 * mode for read-only mounts. This will sync all the metadata and clean
941ddeb14f4SDave Chinner 	 * the log so that the recovery we just performed does not have to be
942ddeb14f4SDave Chinner 	 * replayed again on the next mount.
943ddeb14f4SDave Chinner 	 *
944ddeb14f4SDave Chinner 	 * We use the same quiesce mechanism as the rw->ro remount, as they are
945ddeb14f4SDave Chinner 	 * semantically identical operations.
946ddeb14f4SDave Chinner 	 */
947ddeb14f4SDave Chinner 	if ((mp->m_flags & (XFS_MOUNT_RDONLY|XFS_MOUNT_NORECOVERY)) ==
948ddeb14f4SDave Chinner 							XFS_MOUNT_RDONLY) {
949ddeb14f4SDave Chinner 		xfs_quiesce_attr(mp);
950ddeb14f4SDave Chinner 	}
951ddeb14f4SDave Chinner 
952ddeb14f4SDave Chinner 	/*
9531da177e4SLinus Torvalds 	 * Complete the quota initialisation, post-log-replay component.
9541da177e4SLinus Torvalds 	 */
9557d095257SChristoph Hellwig 	if (quotamount) {
9567d095257SChristoph Hellwig 		ASSERT(mp->m_qflags == 0);
9577d095257SChristoph Hellwig 		mp->m_qflags = quotaflags;
9587d095257SChristoph Hellwig 
9597d095257SChristoph Hellwig 		xfs_qm_mount_quotas(mp);
9607d095257SChristoph Hellwig 	}
9617d095257SChristoph Hellwig 
96284e1e99fSDavid Chinner 	/*
96384e1e99fSDavid Chinner 	 * Now we are mounted, reserve a small amount of unused space for
96484e1e99fSDavid Chinner 	 * privileged transactions. This is needed so that transaction
96584e1e99fSDavid Chinner 	 * space required for critical operations can dip into this pool
96684e1e99fSDavid Chinner 	 * when at ENOSPC. This is needed for operations like create with
96784e1e99fSDavid Chinner 	 * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
96884e1e99fSDavid Chinner 	 * are not allowed to use this reserved space.
9698babd8a2SDave Chinner 	 *
9708babd8a2SDave Chinner 	 * This may drive us straight to ENOSPC on mount, but that implies
9718babd8a2SDave Chinner 	 * we were already there on the last unmount. Warn if this occurs.
97284e1e99fSDavid Chinner 	 */
973d5db0f97SEric Sandeen 	if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
974d5db0f97SEric Sandeen 		resblks = xfs_default_resblks(mp);
975714082bcSDavid Chinner 		error = xfs_reserve_blocks(mp, &resblks, NULL);
976714082bcSDavid Chinner 		if (error)
9770b932cccSDave Chinner 			xfs_warn(mp,
9780b932cccSDave Chinner 	"Unable to allocate reserve blocks. Continuing without reserve pool.");
979174edb0eSDarrick J. Wong 
980174edb0eSDarrick J. Wong 		/* Recover any CoW blocks that never got remapped. */
981174edb0eSDarrick J. Wong 		error = xfs_reflink_recover_cow(mp);
982174edb0eSDarrick J. Wong 		if (error) {
983174edb0eSDarrick J. Wong 			xfs_err(mp,
984174edb0eSDarrick J. Wong 	"Error %d recovering leftover CoW allocations.", error);
985174edb0eSDarrick J. Wong 			xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
986174edb0eSDarrick J. Wong 			goto out_quota;
987174edb0eSDarrick J. Wong 		}
98884d69619SDarrick J. Wong 
98984d69619SDarrick J. Wong 		/* Reserve AG blocks for future btree expansion. */
99084d69619SDarrick J. Wong 		error = xfs_fs_reserve_ag_blocks(mp);
99184d69619SDarrick J. Wong 		if (error && error != -ENOSPC)
99284d69619SDarrick J. Wong 			goto out_agresv;
993d5db0f97SEric Sandeen 	}
99484e1e99fSDavid Chinner 
9951da177e4SLinus Torvalds 	return 0;
9961da177e4SLinus Torvalds 
99784d69619SDarrick J. Wong  out_agresv:
99884d69619SDarrick J. Wong 	xfs_fs_unreserve_ag_blocks(mp);
999174edb0eSDarrick J. Wong  out_quota:
1000174edb0eSDarrick J. Wong 	xfs_qm_unmount_quotas(mp);
1001b93b6e43SChristoph Hellwig  out_rtunmount:
1002b93b6e43SChristoph Hellwig 	xfs_rtunmount_inodes(mp);
1003f9057e3dSChristoph Hellwig  out_rele_rip:
100444a8736bSDarrick J. Wong 	xfs_irele(rip);
100577aff8c7SDarrick J. Wong 	/* Clean out dquots that might be in memory after quotacheck. */
100677aff8c7SDarrick J. Wong 	xfs_qm_unmount(mp);
10072d1d1da3SDarrick J. Wong 	/*
10082d1d1da3SDarrick J. Wong 	 * Cancel all delayed reclaim work and reclaim the inodes directly.
10092d1d1da3SDarrick J. Wong 	 * We have to do this /after/ rtunmount and qm_unmount because those
10102d1d1da3SDarrick J. Wong 	 * two will have scheduled delayed reclaim for the rt/quota inodes.
10112d1d1da3SDarrick J. Wong 	 *
10122d1d1da3SDarrick J. Wong 	 * This is slightly different from the unmountfs call sequence
10132d1d1da3SDarrick J. Wong 	 * because we could be tearing down a partially set up mount.  In
10142d1d1da3SDarrick J. Wong 	 * particular, if log_mount_finish fails we bail out without calling
10152d1d1da3SDarrick J. Wong 	 * qm_unmount_quotas and therefore rely on qm_unmount to release the
10162d1d1da3SDarrick J. Wong 	 * quota inodes.
10172d1d1da3SDarrick J. Wong 	 */
10182d1d1da3SDarrick J. Wong 	cancel_delayed_work_sync(&mp->m_reclaim_work);
10194d0bab3aSDave Chinner 	xfs_reclaim_inodes(mp);
1020519841c2SDarrick J. Wong 	xfs_health_unmount(mp);
1021f9057e3dSChristoph Hellwig  out_log_dealloc:
1022e6b3bb78SCarlos Maiolino 	mp->m_flags |= XFS_MOUNT_UNMOUNTING;
1023f0b2efadSBrian Foster 	xfs_log_mount_cancel(mp);
1024d4f3512bSDave Chinner  out_fail_wait:
1025d4f3512bSDave Chinner 	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
1026d4f3512bSDave Chinner 		xfs_wait_buftarg(mp->m_logdev_targp);
1027d4f3512bSDave Chinner 	xfs_wait_buftarg(mp->m_ddev_targp);
1028f9057e3dSChristoph Hellwig  out_free_perag:
1029ff4f038cSChristoph Hellwig 	xfs_free_perag(mp);
10300650b554SDave Chinner  out_free_dir:
10310650b554SDave Chinner 	xfs_da_unmount(mp);
1032f9057e3dSChristoph Hellwig  out_remove_uuid:
103327174203SChristoph Hellwig 	xfs_uuid_unmount(mp);
103431965ef3SDarrick J. Wong  out_remove_errortag:
103531965ef3SDarrick J. Wong 	xfs_errortag_del(mp);
1036192852beSCarlos Maiolino  out_remove_error_sysfs:
1037192852beSCarlos Maiolino 	xfs_error_sysfs_del(mp);
1038225e4635SBill O'Donnell  out_del_stats:
1039225e4635SBill O'Donnell 	xfs_sysfs_del(&mp->m_stats.xs_kobj);
1040a31b1d3dSBrian Foster  out_remove_sysfs:
1041a31b1d3dSBrian Foster 	xfs_sysfs_del(&mp->m_kobj);
1042f9057e3dSChristoph Hellwig  out:
10431da177e4SLinus Torvalds 	return error;
10441da177e4SLinus Torvalds }
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds /*
10471da177e4SLinus Torvalds  * This flushes out the inodes,dquots and the superblock, unmounts the
10481da177e4SLinus Torvalds  * log and makes sure that incore structures are freed.
10491da177e4SLinus Torvalds  */
105041b5c2e7SChristoph Hellwig void
105141b5c2e7SChristoph Hellwig xfs_unmountfs(
105241b5c2e7SChristoph Hellwig 	struct xfs_mount	*mp)
10531da177e4SLinus Torvalds {
1054c8ce540dSDarrick J. Wong 	uint64_t		resblks;
105541b5c2e7SChristoph Hellwig 	int			error;
10561da177e4SLinus Torvalds 
1057ed30dcbdSDarrick J. Wong 	xfs_stop_block_reaping(mp);
105884d69619SDarrick J. Wong 	xfs_fs_unreserve_ag_blocks(mp);
10597d095257SChristoph Hellwig 	xfs_qm_unmount_quotas(mp);
1060b93b6e43SChristoph Hellwig 	xfs_rtunmount_inodes(mp);
106144a8736bSDarrick J. Wong 	xfs_irele(mp->m_rootip);
106277508ec8SChristoph Hellwig 
1063641c56fbSDavid Chinner 	/*
1064641c56fbSDavid Chinner 	 * We can potentially deadlock here if we have an inode cluster
10659da096fdSMalcolm Parsons 	 * that has been freed has its buffer still pinned in memory because
1066641c56fbSDavid Chinner 	 * the transaction is still sitting in a iclog. The stale inodes
1067718ecc50SDave Chinner 	 * on that buffer will be pinned to the buffer until the
1068718ecc50SDave Chinner 	 * transaction hits the disk and the callbacks run. Pushing the AIL will
1069718ecc50SDave Chinner 	 * skip the stale inodes and may never see the pinned buffer, so
1070718ecc50SDave Chinner 	 * nothing will push out the iclog and unpin the buffer. Hence we
1071718ecc50SDave Chinner 	 * need to force the log here to ensure all items are flushed into the
1072718ecc50SDave Chinner 	 * AIL before we go any further.
1073641c56fbSDavid Chinner 	 */
1074a14a348bSChristoph Hellwig 	xfs_log_force(mp, XFS_LOG_SYNC);
1075c854363eSDave Chinner 
1076c854363eSDave Chinner 	/*
1077ebf55872SChristoph Hellwig 	 * Wait for all busy extents to be freed, including completion of
1078ebf55872SChristoph Hellwig 	 * any discard operation.
1079ebf55872SChristoph Hellwig 	 */
1080ebf55872SChristoph Hellwig 	xfs_extent_busy_wait_all(mp);
10814560e78fSChristoph Hellwig 	flush_workqueue(xfs_discard_wq);
1082ebf55872SChristoph Hellwig 
1083ebf55872SChristoph Hellwig 	/*
1084e6b3bb78SCarlos Maiolino 	 * We now need to tell the world we are unmounting. This will allow
1085e6b3bb78SCarlos Maiolino 	 * us to detect that the filesystem is going away and we should error
1086e6b3bb78SCarlos Maiolino 	 * out anything that we have been retrying in the background. This will
1087e6b3bb78SCarlos Maiolino 	 * prevent neverending retries in AIL pushing from hanging the unmount.
1088e6b3bb78SCarlos Maiolino 	 */
1089e6b3bb78SCarlos Maiolino 	mp->m_flags |= XFS_MOUNT_UNMOUNTING;
1090e6b3bb78SCarlos Maiolino 
1091e6b3bb78SCarlos Maiolino 	/*
1092211e4d43SChristoph Hellwig 	 * Flush all pending changes from the AIL.
1093c854363eSDave Chinner 	 */
1094211e4d43SChristoph Hellwig 	xfs_ail_push_all_sync(mp->m_ail);
1095211e4d43SChristoph Hellwig 
1096211e4d43SChristoph Hellwig 	/*
10974d0bab3aSDave Chinner 	 * Reclaim all inodes. At this point there should be no dirty inodes and
10984d0bab3aSDave Chinner 	 * none should be pinned or locked. Stop background inode reclaim here
10994d0bab3aSDave Chinner 	 * if it is still running.
1100211e4d43SChristoph Hellwig 	 */
11017e18530bSDave Chinner 	cancel_delayed_work_sync(&mp->m_reclaim_work);
11024d0bab3aSDave Chinner 	xfs_reclaim_inodes(mp);
1103519841c2SDarrick J. Wong 	xfs_health_unmount(mp);
11041da177e4SLinus Torvalds 
11057d095257SChristoph Hellwig 	xfs_qm_unmount(mp);
1106a357a121SLachlan McIlroy 
11071da177e4SLinus Torvalds 	/*
110884e1e99fSDavid Chinner 	 * Unreserve any blocks we have so that when we unmount we don't account
110984e1e99fSDavid Chinner 	 * the reserved free space as used. This is really only necessary for
111084e1e99fSDavid Chinner 	 * lazy superblock counting because it trusts the incore superblock
11119da096fdSMalcolm Parsons 	 * counters to be absolutely correct on clean unmount.
111284e1e99fSDavid Chinner 	 *
111384e1e99fSDavid Chinner 	 * We don't bother correcting this elsewhere for lazy superblock
111484e1e99fSDavid Chinner 	 * counting because on mount of an unclean filesystem we reconstruct the
111584e1e99fSDavid Chinner 	 * correct counter value and this is irrelevant.
111684e1e99fSDavid Chinner 	 *
111784e1e99fSDavid Chinner 	 * For non-lazy counter filesystems, this doesn't matter at all because
111884e1e99fSDavid Chinner 	 * we only every apply deltas to the superblock and hence the incore
111984e1e99fSDavid Chinner 	 * value does not matter....
112084e1e99fSDavid Chinner 	 */
112184e1e99fSDavid Chinner 	resblks = 0;
1122714082bcSDavid Chinner 	error = xfs_reserve_blocks(mp, &resblks, NULL);
1123714082bcSDavid Chinner 	if (error)
11240b932cccSDave Chinner 		xfs_warn(mp, "Unable to free reserved block pool. "
1125714082bcSDavid Chinner 				"Freespace may not be correct on next mount.");
1126714082bcSDavid Chinner 
1127adab0f67SChandra Seetharaman 	error = xfs_log_sbcount(mp);
1128e5720eecSDavid Chinner 	if (error)
11290b932cccSDave Chinner 		xfs_warn(mp, "Unable to update superblock counters. "
1130e5720eecSDavid Chinner 				"Freespace may not be correct on next mount.");
113187c7bec7SChristoph Hellwig 
1132225e4635SBill O'Donnell 
113321b699c8SChristoph Hellwig 	xfs_log_unmount(mp);
11340650b554SDave Chinner 	xfs_da_unmount(mp);
113527174203SChristoph Hellwig 	xfs_uuid_unmount(mp);
11361da177e4SLinus Torvalds 
11371550d0b0SChristoph Hellwig #if defined(DEBUG)
113831965ef3SDarrick J. Wong 	xfs_errortag_clearall(mp);
11391da177e4SLinus Torvalds #endif
1140ff4f038cSChristoph Hellwig 	xfs_free_perag(mp);
1141a31b1d3dSBrian Foster 
114231965ef3SDarrick J. Wong 	xfs_errortag_del(mp);
1143192852beSCarlos Maiolino 	xfs_error_sysfs_del(mp);
1144225e4635SBill O'Donnell 	xfs_sysfs_del(&mp->m_stats.xs_kobj);
1145a31b1d3dSBrian Foster 	xfs_sysfs_del(&mp->m_kobj);
11461da177e4SLinus Torvalds }
11471da177e4SLinus Torvalds 
114891ee575fSBrian Foster /*
114991ee575fSBrian Foster  * Determine whether modifications can proceed. The caller specifies the minimum
115091ee575fSBrian Foster  * freeze level for which modifications should not be allowed. This allows
115191ee575fSBrian Foster  * certain operations to proceed while the freeze sequence is in progress, if
115291ee575fSBrian Foster  * necessary.
115391ee575fSBrian Foster  */
115491ee575fSBrian Foster bool
115591ee575fSBrian Foster xfs_fs_writable(
115691ee575fSBrian Foster 	struct xfs_mount	*mp,
115791ee575fSBrian Foster 	int			level)
115892821e2bSDavid Chinner {
115991ee575fSBrian Foster 	ASSERT(level > SB_UNFROZEN);
116091ee575fSBrian Foster 	if ((mp->m_super->s_writers.frozen >= level) ||
116191ee575fSBrian Foster 	    XFS_FORCED_SHUTDOWN(mp) || (mp->m_flags & XFS_MOUNT_RDONLY))
116291ee575fSBrian Foster 		return false;
116391ee575fSBrian Foster 
116491ee575fSBrian Foster 	return true;
116592821e2bSDavid Chinner }
116692821e2bSDavid Chinner 
116792821e2bSDavid Chinner /*
1168b2ce3974SAlex Elder  * xfs_log_sbcount
1169b2ce3974SAlex Elder  *
1170adab0f67SChandra Seetharaman  * Sync the superblock counters to disk.
1171b2ce3974SAlex Elder  *
117291ee575fSBrian Foster  * Note this code can be called during the process of freezing, so we use the
117391ee575fSBrian Foster  * transaction allocator that does not block when the transaction subsystem is
117491ee575fSBrian Foster  * in its frozen state.
117592821e2bSDavid Chinner  */
117692821e2bSDavid Chinner int
1177adab0f67SChandra Seetharaman xfs_log_sbcount(xfs_mount_t *mp)
117892821e2bSDavid Chinner {
117991ee575fSBrian Foster 	/* allow this to proceed during the freeze sequence... */
118091ee575fSBrian Foster 	if (!xfs_fs_writable(mp, SB_FREEZE_COMPLETE))
118192821e2bSDavid Chinner 		return 0;
118292821e2bSDavid Chinner 
118392821e2bSDavid Chinner 	/*
118492821e2bSDavid Chinner 	 * we don't need to do this if we are updating the superblock
118592821e2bSDavid Chinner 	 * counters on every modification.
118692821e2bSDavid Chinner 	 */
118792821e2bSDavid Chinner 	if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
118892821e2bSDavid Chinner 		return 0;
118992821e2bSDavid Chinner 
119061e63ecbSDave Chinner 	return xfs_sync_sb(mp, true);
119192821e2bSDavid Chinner }
119292821e2bSDavid Chinner 
11938c1903d3SDave Chinner /*
11948c1903d3SDave Chinner  * Deltas for the block count can vary from 1 to very large, but lock contention
11958c1903d3SDave Chinner  * only occurs on frequent small block count updates such as in the delayed
11968c1903d3SDave Chinner  * allocation path for buffered writes (page a time updates). Hence we set
11978c1903d3SDave Chinner  * a large batch count (1024) to minimise global counter updates except when
11988c1903d3SDave Chinner  * we get near to ENOSPC and we have to be very accurate with our updates.
11998c1903d3SDave Chinner  */
12008c1903d3SDave Chinner #define XFS_FDBLOCKS_BATCH	1024
12010d485adaSDave Chinner int
12020d485adaSDave Chinner xfs_mod_fdblocks(
12030d485adaSDave Chinner 	struct xfs_mount	*mp,
12040d485adaSDave Chinner 	int64_t			delta,
12050d485adaSDave Chinner 	bool			rsvd)
12060d485adaSDave Chinner {
12070d485adaSDave Chinner 	int64_t			lcounter;
12080d485adaSDave Chinner 	long long		res_used;
12090d485adaSDave Chinner 	s32			batch;
12100d485adaSDave Chinner 
12110d485adaSDave Chinner 	if (delta > 0) {
12120d485adaSDave Chinner 		/*
12130d485adaSDave Chinner 		 * If the reserve pool is depleted, put blocks back into it
12140d485adaSDave Chinner 		 * first. Most of the time the pool is full.
12150d485adaSDave Chinner 		 */
12160d485adaSDave Chinner 		if (likely(mp->m_resblks == mp->m_resblks_avail)) {
12170d485adaSDave Chinner 			percpu_counter_add(&mp->m_fdblocks, delta);
12180d485adaSDave Chinner 			return 0;
12190d485adaSDave Chinner 		}
12200d485adaSDave Chinner 
12210d485adaSDave Chinner 		spin_lock(&mp->m_sb_lock);
12220d485adaSDave Chinner 		res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
12230d485adaSDave Chinner 
12240d485adaSDave Chinner 		if (res_used > delta) {
12250d485adaSDave Chinner 			mp->m_resblks_avail += delta;
12260d485adaSDave Chinner 		} else {
12270d485adaSDave Chinner 			delta -= res_used;
12280d485adaSDave Chinner 			mp->m_resblks_avail = mp->m_resblks;
12290d485adaSDave Chinner 			percpu_counter_add(&mp->m_fdblocks, delta);
12300d485adaSDave Chinner 		}
12310d485adaSDave Chinner 		spin_unlock(&mp->m_sb_lock);
12320d485adaSDave Chinner 		return 0;
12330d485adaSDave Chinner 	}
12340d485adaSDave Chinner 
12350d485adaSDave Chinner 	/*
12360d485adaSDave Chinner 	 * Taking blocks away, need to be more accurate the closer we
12370d485adaSDave Chinner 	 * are to zero.
12380d485adaSDave Chinner 	 *
12390d485adaSDave Chinner 	 * If the counter has a value of less than 2 * max batch size,
12400d485adaSDave Chinner 	 * then make everything serialise as we are real close to
12410d485adaSDave Chinner 	 * ENOSPC.
12420d485adaSDave Chinner 	 */
12438c1903d3SDave Chinner 	if (__percpu_counter_compare(&mp->m_fdblocks, 2 * XFS_FDBLOCKS_BATCH,
12448c1903d3SDave Chinner 				     XFS_FDBLOCKS_BATCH) < 0)
12450d485adaSDave Chinner 		batch = 1;
12460d485adaSDave Chinner 	else
12478c1903d3SDave Chinner 		batch = XFS_FDBLOCKS_BATCH;
12480d485adaSDave Chinner 
1249104b4e51SNikolay Borisov 	percpu_counter_add_batch(&mp->m_fdblocks, delta, batch);
125052548852SDarrick J. Wong 	if (__percpu_counter_compare(&mp->m_fdblocks, mp->m_alloc_set_aside,
12518c1903d3SDave Chinner 				     XFS_FDBLOCKS_BATCH) >= 0) {
12520d485adaSDave Chinner 		/* we had space! */
12530d485adaSDave Chinner 		return 0;
12540d485adaSDave Chinner 	}
12550d485adaSDave Chinner 
12560d485adaSDave Chinner 	/*
12570d485adaSDave Chinner 	 * lock up the sb for dipping into reserves before releasing the space
12580d485adaSDave Chinner 	 * that took us to ENOSPC.
12590d485adaSDave Chinner 	 */
12600d485adaSDave Chinner 	spin_lock(&mp->m_sb_lock);
12610d485adaSDave Chinner 	percpu_counter_add(&mp->m_fdblocks, -delta);
12620d485adaSDave Chinner 	if (!rsvd)
12630d485adaSDave Chinner 		goto fdblocks_enospc;
12640d485adaSDave Chinner 
12650d485adaSDave Chinner 	lcounter = (long long)mp->m_resblks_avail + delta;
12660d485adaSDave Chinner 	if (lcounter >= 0) {
12670d485adaSDave Chinner 		mp->m_resblks_avail = lcounter;
12680d485adaSDave Chinner 		spin_unlock(&mp->m_sb_lock);
12690d485adaSDave Chinner 		return 0;
12700d485adaSDave Chinner 	}
1271ec43f6daSEric Sandeen 	xfs_warn_once(mp,
1272ec43f6daSEric Sandeen "Reserve blocks depleted! Consider increasing reserve pool size.");
1273ec43f6daSEric Sandeen 
12740d485adaSDave Chinner fdblocks_enospc:
12750d485adaSDave Chinner 	spin_unlock(&mp->m_sb_lock);
12760d485adaSDave Chinner 	return -ENOSPC;
12770d485adaSDave Chinner }
12780d485adaSDave Chinner 
1279bab98bbeSDave Chinner int
1280bab98bbeSDave Chinner xfs_mod_frextents(
1281bab98bbeSDave Chinner 	struct xfs_mount	*mp,
1282bab98bbeSDave Chinner 	int64_t			delta)
1283bab98bbeSDave Chinner {
1284bab98bbeSDave Chinner 	int64_t			lcounter;
1285bab98bbeSDave Chinner 	int			ret = 0;
1286bab98bbeSDave Chinner 
1287bab98bbeSDave Chinner 	spin_lock(&mp->m_sb_lock);
1288bab98bbeSDave Chinner 	lcounter = mp->m_sb.sb_frextents + delta;
1289bab98bbeSDave Chinner 	if (lcounter < 0)
1290bab98bbeSDave Chinner 		ret = -ENOSPC;
1291bab98bbeSDave Chinner 	else
1292bab98bbeSDave Chinner 		mp->m_sb.sb_frextents = lcounter;
1293bab98bbeSDave Chinner 	spin_unlock(&mp->m_sb_lock);
1294bab98bbeSDave Chinner 	return ret;
1295bab98bbeSDave Chinner }
1296bab98bbeSDave Chinner 
12971da177e4SLinus Torvalds /*
12981da177e4SLinus Torvalds  * Used to free the superblock along various error paths.
12991da177e4SLinus Torvalds  */
13001da177e4SLinus Torvalds void
13011da177e4SLinus Torvalds xfs_freesb(
130226af6552SDave Chinner 	struct xfs_mount	*mp)
13031da177e4SLinus Torvalds {
130426af6552SDave Chinner 	struct xfs_buf		*bp = mp->m_sb_bp;
13051da177e4SLinus Torvalds 
130626af6552SDave Chinner 	xfs_buf_lock(bp);
13071da177e4SLinus Torvalds 	mp->m_sb_bp = NULL;
130826af6552SDave Chinner 	xfs_buf_relse(bp);
13091da177e4SLinus Torvalds }
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds /*
1312dda35b8fSChristoph Hellwig  * If the underlying (data/log/rt) device is readonly, there are some
1313dda35b8fSChristoph Hellwig  * operations that cannot proceed.
1314dda35b8fSChristoph Hellwig  */
1315dda35b8fSChristoph Hellwig int
1316dda35b8fSChristoph Hellwig xfs_dev_is_read_only(
1317dda35b8fSChristoph Hellwig 	struct xfs_mount	*mp,
1318dda35b8fSChristoph Hellwig 	char			*message)
1319dda35b8fSChristoph Hellwig {
1320dda35b8fSChristoph Hellwig 	if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
1321dda35b8fSChristoph Hellwig 	    xfs_readonly_buftarg(mp->m_logdev_targp) ||
1322dda35b8fSChristoph Hellwig 	    (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
13230b932cccSDave Chinner 		xfs_notice(mp, "%s required on read-only device.", message);
13240b932cccSDave Chinner 		xfs_notice(mp, "write access unavailable, cannot proceed.");
13252451337dSDave Chinner 		return -EROFS;
1326dda35b8fSChristoph Hellwig 	}
1327dda35b8fSChristoph Hellwig 	return 0;
1328dda35b8fSChristoph Hellwig }
1329f467cad9SDarrick J. Wong 
1330f467cad9SDarrick J. Wong /* Force the summary counters to be recalculated at next mount. */
1331f467cad9SDarrick J. Wong void
1332f467cad9SDarrick J. Wong xfs_force_summary_recalc(
1333f467cad9SDarrick J. Wong 	struct xfs_mount	*mp)
1334f467cad9SDarrick J. Wong {
1335f467cad9SDarrick J. Wong 	if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
1336f467cad9SDarrick J. Wong 		return;
1337f467cad9SDarrick J. Wong 
133839353ff6SDarrick J. Wong 	xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS);
1339f467cad9SDarrick J. Wong }
13409fe82b8cSDarrick J. Wong 
13419fe82b8cSDarrick J. Wong /*
13429fe82b8cSDarrick J. Wong  * Update the in-core delayed block counter.
13439fe82b8cSDarrick J. Wong  *
13449fe82b8cSDarrick J. Wong  * We prefer to update the counter without having to take a spinlock for every
13459fe82b8cSDarrick J. Wong  * counter update (i.e. batching).  Each change to delayed allocation
13469fe82b8cSDarrick J. Wong  * reservations can change can easily exceed the default percpu counter
13479fe82b8cSDarrick J. Wong  * batching, so we use a larger batch factor here.
13489fe82b8cSDarrick J. Wong  *
13499fe82b8cSDarrick J. Wong  * Note that we don't currently have any callers requiring fast summation
13509fe82b8cSDarrick J. Wong  * (e.g. percpu_counter_read) so we can use a big batch value here.
13519fe82b8cSDarrick J. Wong  */
13529fe82b8cSDarrick J. Wong #define XFS_DELALLOC_BATCH	(4096)
13539fe82b8cSDarrick J. Wong void
13549fe82b8cSDarrick J. Wong xfs_mod_delalloc(
13559fe82b8cSDarrick J. Wong 	struct xfs_mount	*mp,
13569fe82b8cSDarrick J. Wong 	int64_t			delta)
13579fe82b8cSDarrick J. Wong {
13589fe82b8cSDarrick J. Wong 	percpu_counter_add_batch(&mp->m_delalloc_blks, delta,
13599fe82b8cSDarrick J. Wong 			XFS_DELALLOC_BATCH);
13609fe82b8cSDarrick J. Wong }
1361