xref: /linux/fs/xfs/xfs_mount.c (revision 77aff8c76425c8f49b50d0b9009915066739e7d2)
11da177e4SLinus Torvalds /*
27b718769SNathan Scott  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
37b718769SNathan Scott  * All Rights Reserved.
41da177e4SLinus Torvalds  *
57b718769SNathan Scott  * This program is free software; you can redistribute it and/or
67b718769SNathan Scott  * modify it under the terms of the GNU General Public License as
71da177e4SLinus Torvalds  * published by the Free Software Foundation.
81da177e4SLinus Torvalds  *
97b718769SNathan Scott  * This program is distributed in the hope that it would be useful,
107b718769SNathan Scott  * but WITHOUT ANY WARRANTY; without even the implied warranty of
117b718769SNathan Scott  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
127b718769SNathan Scott  * GNU General Public License for more details.
131da177e4SLinus Torvalds  *
147b718769SNathan Scott  * You should have received a copy of the GNU General Public License
157b718769SNathan Scott  * along with this program; if not, write the Free Software Foundation,
167b718769SNathan Scott  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds #include "xfs.h"
19a844f451SNathan Scott #include "xfs_fs.h"
2070a9883cSDave Chinner #include "xfs_shared.h"
21239880efSDave Chinner #include "xfs_format.h"
22239880efSDave Chinner #include "xfs_log_format.h"
23239880efSDave Chinner #include "xfs_trans_resv.h"
24a844f451SNathan Scott #include "xfs_bit.h"
251da177e4SLinus Torvalds #include "xfs_sb.h"
261da177e4SLinus Torvalds #include "xfs_mount.h"
273ab78df2SDarrick J. Wong #include "xfs_defer.h"
2857062787SDave Chinner #include "xfs_da_format.h"
299a2cc41cSDave Chinner #include "xfs_da_btree.h"
301da177e4SLinus Torvalds #include "xfs_inode.h"
31a4fbe6abSDave Chinner #include "xfs_dir2.h"
32a844f451SNathan Scott #include "xfs_ialloc.h"
331da177e4SLinus Torvalds #include "xfs_alloc.h"
341da177e4SLinus Torvalds #include "xfs_rtalloc.h"
351da177e4SLinus Torvalds #include "xfs_bmap.h"
36a4fbe6abSDave Chinner #include "xfs_trans.h"
37a4fbe6abSDave Chinner #include "xfs_trans_priv.h"
38a4fbe6abSDave Chinner #include "xfs_log.h"
391da177e4SLinus Torvalds #include "xfs_error.h"
401da177e4SLinus Torvalds #include "xfs_quota.h"
411da177e4SLinus Torvalds #include "xfs_fsops.h"
420b1b213fSChristoph Hellwig #include "xfs_trace.h"
436d8b79cfSDave Chinner #include "xfs_icache.h"
44a31b1d3dSBrian Foster #include "xfs_sysfs.h"
45035e00acSDarrick J. Wong #include "xfs_rmap_btree.h"
461946b91cSDarrick J. Wong #include "xfs_refcount_btree.h"
47174edb0eSDarrick J. Wong #include "xfs_reflink.h"
48ebf55872SChristoph Hellwig #include "xfs_extent_busy.h"
490b1b213fSChristoph Hellwig 
501da177e4SLinus Torvalds 
5127174203SChristoph Hellwig static DEFINE_MUTEX(xfs_uuid_table_mutex);
5227174203SChristoph Hellwig static int xfs_uuid_table_size;
5327174203SChristoph Hellwig static uuid_t *xfs_uuid_table;
5427174203SChristoph Hellwig 
55af3b6382SDarrick J. Wong void
56af3b6382SDarrick J. Wong xfs_uuid_table_free(void)
57af3b6382SDarrick J. Wong {
58af3b6382SDarrick J. Wong 	if (xfs_uuid_table_size == 0)
59af3b6382SDarrick J. Wong 		return;
60af3b6382SDarrick J. Wong 	kmem_free(xfs_uuid_table);
61af3b6382SDarrick J. Wong 	xfs_uuid_table = NULL;
62af3b6382SDarrick J. Wong 	xfs_uuid_table_size = 0;
63af3b6382SDarrick J. Wong }
64af3b6382SDarrick J. Wong 
6527174203SChristoph Hellwig /*
6627174203SChristoph Hellwig  * See if the UUID is unique among mounted XFS filesystems.
6727174203SChristoph Hellwig  * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
6827174203SChristoph Hellwig  */
6927174203SChristoph Hellwig STATIC int
7027174203SChristoph Hellwig xfs_uuid_mount(
7127174203SChristoph Hellwig 	struct xfs_mount	*mp)
7227174203SChristoph Hellwig {
7327174203SChristoph Hellwig 	uuid_t			*uuid = &mp->m_sb.sb_uuid;
7427174203SChristoph Hellwig 	int			hole, i;
7527174203SChristoph Hellwig 
768f720d9fSAmir Goldstein 	/* Publish UUID in struct super_block */
7785787090SChristoph Hellwig 	uuid_copy(&mp->m_super->s_uuid, uuid);
788f720d9fSAmir Goldstein 
7927174203SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_NOUUID)
8027174203SChristoph Hellwig 		return 0;
8127174203SChristoph Hellwig 
82d905fdaaSAmir Goldstein 	if (uuid_is_null(uuid)) {
83d905fdaaSAmir Goldstein 		xfs_warn(mp, "Filesystem has null UUID - can't mount");
842451337dSDave Chinner 		return -EINVAL;
8527174203SChristoph Hellwig 	}
8627174203SChristoph Hellwig 
8727174203SChristoph Hellwig 	mutex_lock(&xfs_uuid_table_mutex);
8827174203SChristoph Hellwig 	for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) {
89d905fdaaSAmir Goldstein 		if (uuid_is_null(&xfs_uuid_table[i])) {
9027174203SChristoph Hellwig 			hole = i;
9127174203SChristoph Hellwig 			continue;
9227174203SChristoph Hellwig 		}
9327174203SChristoph Hellwig 		if (uuid_equal(uuid, &xfs_uuid_table[i]))
9427174203SChristoph Hellwig 			goto out_duplicate;
9527174203SChristoph Hellwig 	}
9627174203SChristoph Hellwig 
9727174203SChristoph Hellwig 	if (hole < 0) {
9827174203SChristoph Hellwig 		xfs_uuid_table = kmem_realloc(xfs_uuid_table,
9927174203SChristoph Hellwig 			(xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table),
10027174203SChristoph Hellwig 			KM_SLEEP);
10127174203SChristoph Hellwig 		hole = xfs_uuid_table_size++;
10227174203SChristoph Hellwig 	}
10327174203SChristoph Hellwig 	xfs_uuid_table[hole] = *uuid;
10427174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
10527174203SChristoph Hellwig 
10627174203SChristoph Hellwig 	return 0;
10727174203SChristoph Hellwig 
10827174203SChristoph Hellwig  out_duplicate:
10927174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
110021000e5SMitsuo Hayasaka 	xfs_warn(mp, "Filesystem has duplicate UUID %pU - can't mount", uuid);
1112451337dSDave Chinner 	return -EINVAL;
11227174203SChristoph Hellwig }
11327174203SChristoph Hellwig 
11427174203SChristoph Hellwig STATIC void
11527174203SChristoph Hellwig xfs_uuid_unmount(
11627174203SChristoph Hellwig 	struct xfs_mount	*mp)
11727174203SChristoph Hellwig {
11827174203SChristoph Hellwig 	uuid_t			*uuid = &mp->m_sb.sb_uuid;
11927174203SChristoph Hellwig 	int			i;
12027174203SChristoph Hellwig 
12127174203SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_NOUUID)
12227174203SChristoph Hellwig 		return;
12327174203SChristoph Hellwig 
12427174203SChristoph Hellwig 	mutex_lock(&xfs_uuid_table_mutex);
12527174203SChristoph Hellwig 	for (i = 0; i < xfs_uuid_table_size; i++) {
126d905fdaaSAmir Goldstein 		if (uuid_is_null(&xfs_uuid_table[i]))
12727174203SChristoph Hellwig 			continue;
12827174203SChristoph Hellwig 		if (!uuid_equal(uuid, &xfs_uuid_table[i]))
12927174203SChristoph Hellwig 			continue;
13027174203SChristoph Hellwig 		memset(&xfs_uuid_table[i], 0, sizeof(uuid_t));
13127174203SChristoph Hellwig 		break;
13227174203SChristoph Hellwig 	}
13327174203SChristoph Hellwig 	ASSERT(i < xfs_uuid_table_size);
13427174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
13527174203SChristoph Hellwig }
13627174203SChristoph Hellwig 
13727174203SChristoph Hellwig 
138e176579eSDave Chinner STATIC void
139e176579eSDave Chinner __xfs_free_perag(
140e176579eSDave Chinner 	struct rcu_head	*head)
141e176579eSDave Chinner {
142e176579eSDave Chinner 	struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head);
143e176579eSDave Chinner 
144e176579eSDave Chinner 	ASSERT(atomic_read(&pag->pag_ref) == 0);
145e176579eSDave Chinner 	kmem_free(pag);
146e176579eSDave Chinner }
147e176579eSDave Chinner 
1480fa800fbSDave Chinner /*
149e176579eSDave Chinner  * Free up the per-ag resources associated with the mount structure.
1501da177e4SLinus Torvalds  */
151c962fb79SChristoph Hellwig STATIC void
152ff4f038cSChristoph Hellwig xfs_free_perag(
153745f6919SChristoph Hellwig 	xfs_mount_t	*mp)
1541da177e4SLinus Torvalds {
1551c1c6ebcSDave Chinner 	xfs_agnumber_t	agno;
1561c1c6ebcSDave Chinner 	struct xfs_perag *pag;
1571da177e4SLinus Torvalds 
1581c1c6ebcSDave Chinner 	for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
1591c1c6ebcSDave Chinner 		spin_lock(&mp->m_perag_lock);
1601c1c6ebcSDave Chinner 		pag = radix_tree_delete(&mp->m_perag_tree, agno);
1611c1c6ebcSDave Chinner 		spin_unlock(&mp->m_perag_lock);
162e176579eSDave Chinner 		ASSERT(pag);
163f83282a8SDave Chinner 		ASSERT(atomic_read(&pag->pag_ref) == 0);
1646031e73aSLucas Stach 		xfs_buf_hash_destroy(pag);
165e176579eSDave Chinner 		call_rcu(&pag->rcu_head, __xfs_free_perag);
1661da177e4SLinus Torvalds 	}
1671da177e4SLinus Torvalds }
1681da177e4SLinus Torvalds 
1694cc929eeSNathan Scott /*
1704cc929eeSNathan Scott  * Check size of device based on the (data/realtime) block count.
1714cc929eeSNathan Scott  * Note: this check is used by the growfs code as well as mount.
1724cc929eeSNathan Scott  */
1734cc929eeSNathan Scott int
1744cc929eeSNathan Scott xfs_sb_validate_fsb_count(
1754cc929eeSNathan Scott 	xfs_sb_t	*sbp,
176c8ce540dSDarrick J. Wong 	uint64_t	nblocks)
1774cc929eeSNathan Scott {
1784cc929eeSNathan Scott 	ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
1794cc929eeSNathan Scott 	ASSERT(sbp->sb_blocklog >= BBSHIFT);
1804cc929eeSNathan Scott 
181d5cf09baSChristoph Hellwig 	/* Limited by ULONG_MAX of page cache index */
18209cbfeafSKirill A. Shutemov 	if (nblocks >> (PAGE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
1832451337dSDave Chinner 		return -EFBIG;
1844cc929eeSNathan Scott 	return 0;
1854cc929eeSNathan Scott }
1861da177e4SLinus Torvalds 
1871c1c6ebcSDave Chinner int
188c11e2c36SNathan Scott xfs_initialize_perag(
189c11e2c36SNathan Scott 	xfs_mount_t	*mp,
1901c1c6ebcSDave Chinner 	xfs_agnumber_t	agcount,
1911c1c6ebcSDave Chinner 	xfs_agnumber_t	*maxagi)
1921da177e4SLinus Torvalds {
1932d2194f6SCarlos Maiolino 	xfs_agnumber_t	index;
194b20fe473SBill O'Donnell 	xfs_agnumber_t	first_initialised = NULLAGNUMBER;
1951da177e4SLinus Torvalds 	xfs_perag_t	*pag;
1968b26c582SDave Chinner 	int		error = -ENOMEM;
1971da177e4SLinus Torvalds 
1981c1c6ebcSDave Chinner 	/*
1991c1c6ebcSDave Chinner 	 * Walk the current per-ag tree so we don't try to initialise AGs
2001c1c6ebcSDave Chinner 	 * that already exist (growfs case). Allocate and insert all the
2011c1c6ebcSDave Chinner 	 * AGs we don't find ready for initialisation.
2021c1c6ebcSDave Chinner 	 */
2031c1c6ebcSDave Chinner 	for (index = 0; index < agcount; index++) {
2041c1c6ebcSDave Chinner 		pag = xfs_perag_get(mp, index);
2051c1c6ebcSDave Chinner 		if (pag) {
2061c1c6ebcSDave Chinner 			xfs_perag_put(pag);
2071c1c6ebcSDave Chinner 			continue;
2081c1c6ebcSDave Chinner 		}
209fb3b504aSChristoph Hellwig 
2101c1c6ebcSDave Chinner 		pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
2111c1c6ebcSDave Chinner 		if (!pag)
212b20fe473SBill O'Donnell 			goto out_unwind_new_pags;
213fb3b504aSChristoph Hellwig 		pag->pag_agno = index;
214fb3b504aSChristoph Hellwig 		pag->pag_mount = mp;
2151a427ab0SDave Chinner 		spin_lock_init(&pag->pag_ici_lock);
21669b491c2SDave Chinner 		mutex_init(&pag->pag_ici_reclaim_lock);
217fb3b504aSChristoph Hellwig 		INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
2186031e73aSLucas Stach 		if (xfs_buf_hash_init(pag))
219b20fe473SBill O'Donnell 			goto out_free_pag;
220ebf55872SChristoph Hellwig 		init_waitqueue_head(&pag->pagb_wait);
221fb3b504aSChristoph Hellwig 
2221c1c6ebcSDave Chinner 		if (radix_tree_preload(GFP_NOFS))
223b20fe473SBill O'Donnell 			goto out_hash_destroy;
224fb3b504aSChristoph Hellwig 
2251c1c6ebcSDave Chinner 		spin_lock(&mp->m_perag_lock);
2261c1c6ebcSDave Chinner 		if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
2271c1c6ebcSDave Chinner 			BUG();
2281c1c6ebcSDave Chinner 			spin_unlock(&mp->m_perag_lock);
2298b26c582SDave Chinner 			radix_tree_preload_end();
2308b26c582SDave Chinner 			error = -EEXIST;
231b20fe473SBill O'Donnell 			goto out_hash_destroy;
2321c1c6ebcSDave Chinner 		}
2331c1c6ebcSDave Chinner 		spin_unlock(&mp->m_perag_lock);
2341c1c6ebcSDave Chinner 		radix_tree_preload_end();
235b20fe473SBill O'Donnell 		/* first new pag is fully initialized */
236b20fe473SBill O'Donnell 		if (first_initialised == NULLAGNUMBER)
237b20fe473SBill O'Donnell 			first_initialised = index;
2381c1c6ebcSDave Chinner 	}
2391c1c6ebcSDave Chinner 
24012c3f05cSEric Sandeen 	index = xfs_set_inode_alloc(mp, agcount);
241fb3b504aSChristoph Hellwig 
2421c1c6ebcSDave Chinner 	if (maxagi)
2431c1c6ebcSDave Chinner 		*maxagi = index;
2448018026eSDarrick J. Wong 
2458018026eSDarrick J. Wong 	mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp);
2461c1c6ebcSDave Chinner 	return 0;
2478b26c582SDave Chinner 
248b20fe473SBill O'Donnell out_hash_destroy:
2496031e73aSLucas Stach 	xfs_buf_hash_destroy(pag);
250b20fe473SBill O'Donnell out_free_pag:
2518b26c582SDave Chinner 	kmem_free(pag);
252b20fe473SBill O'Donnell out_unwind_new_pags:
253b20fe473SBill O'Donnell 	/* unwind any prior newly initialized pags */
254b20fe473SBill O'Donnell 	for (index = first_initialised; index < agcount; index++) {
2558b26c582SDave Chinner 		pag = radix_tree_delete(&mp->m_perag_tree, index);
256b20fe473SBill O'Donnell 		if (!pag)
257b20fe473SBill O'Donnell 			break;
2586031e73aSLucas Stach 		xfs_buf_hash_destroy(pag);
2598b26c582SDave Chinner 		kmem_free(pag);
2608b26c582SDave Chinner 	}
2618b26c582SDave Chinner 	return error;
2621da177e4SLinus Torvalds }
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds /*
2651da177e4SLinus Torvalds  * xfs_readsb
2661da177e4SLinus Torvalds  *
2671da177e4SLinus Torvalds  * Does the initial read of the superblock.
2681da177e4SLinus Torvalds  */
2691da177e4SLinus Torvalds int
270ff55068cSDave Chinner xfs_readsb(
271ff55068cSDave Chinner 	struct xfs_mount *mp,
272ff55068cSDave Chinner 	int		flags)
2731da177e4SLinus Torvalds {
2741da177e4SLinus Torvalds 	unsigned int	sector_size;
27504a1e6c5SDave Chinner 	struct xfs_buf	*bp;
27604a1e6c5SDave Chinner 	struct xfs_sb	*sbp = &mp->m_sb;
2771da177e4SLinus Torvalds 	int		error;
278af34e09dSDave Chinner 	int		loud = !(flags & XFS_MFSI_QUIET);
279daba5427SEric Sandeen 	const struct xfs_buf_ops *buf_ops;
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	ASSERT(mp->m_sb_bp == NULL);
2821da177e4SLinus Torvalds 	ASSERT(mp->m_ddev_targp != NULL);
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	/*
285daba5427SEric Sandeen 	 * For the initial read, we must guess at the sector
286daba5427SEric Sandeen 	 * size based on the block device.  It's enough to
287daba5427SEric Sandeen 	 * get the sb_sectsize out of the superblock and
288daba5427SEric Sandeen 	 * then reread with the proper length.
289daba5427SEric Sandeen 	 * We don't verify it yet, because it may not be complete.
290daba5427SEric Sandeen 	 */
291daba5427SEric Sandeen 	sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
292daba5427SEric Sandeen 	buf_ops = NULL;
293daba5427SEric Sandeen 
294daba5427SEric Sandeen 	/*
295c891c30aSBrian Foster 	 * Allocate a (locked) buffer to hold the superblock. This will be kept
296c891c30aSBrian Foster 	 * around at all times to optimize access to the superblock. Therefore,
297c891c30aSBrian Foster 	 * set XBF_NO_IOACCT to make sure it doesn't hold the buftarg count
298c891c30aSBrian Foster 	 * elevated.
2991da177e4SLinus Torvalds 	 */
30026af6552SDave Chinner reread:
301ba372674SDave Chinner 	error = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR,
302c891c30aSBrian Foster 				      BTOBB(sector_size), XBF_NO_IOACCT, &bp,
303c891c30aSBrian Foster 				      buf_ops);
304ba372674SDave Chinner 	if (error) {
305eab4e633SDave Chinner 		if (loud)
306e721f504SDave Chinner 			xfs_warn(mp, "SB validate failed with error %d.", error);
307ac75a1f7SDave Chinner 		/* bad CRC means corrupted metadata */
3082451337dSDave Chinner 		if (error == -EFSBADCRC)
3092451337dSDave Chinner 			error = -EFSCORRUPTED;
310ba372674SDave Chinner 		return error;
311eab4e633SDave Chinner 	}
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	/*
3141da177e4SLinus Torvalds 	 * Initialize the mount structure from the superblock.
3151da177e4SLinus Torvalds 	 */
316556b8883SDave Chinner 	xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(bp));
317556b8883SDave Chinner 
318556b8883SDave Chinner 	/*
319556b8883SDave Chinner 	 * If we haven't validated the superblock, do so now before we try
320556b8883SDave Chinner 	 * to check the sector size and reread the superblock appropriately.
321556b8883SDave Chinner 	 */
322556b8883SDave Chinner 	if (sbp->sb_magicnum != XFS_SB_MAGIC) {
323556b8883SDave Chinner 		if (loud)
324556b8883SDave Chinner 			xfs_warn(mp, "Invalid superblock magic number");
3252451337dSDave Chinner 		error = -EINVAL;
326556b8883SDave Chinner 		goto release_buf;
327556b8883SDave Chinner 	}
328ff55068cSDave Chinner 
3291da177e4SLinus Torvalds 	/*
3301da177e4SLinus Torvalds 	 * We must be able to do sector-sized and sector-aligned IO.
3311da177e4SLinus Torvalds 	 */
33204a1e6c5SDave Chinner 	if (sector_size > sbp->sb_sectsize) {
333af34e09dSDave Chinner 		if (loud)
334af34e09dSDave Chinner 			xfs_warn(mp, "device supports %u byte sectors (not %u)",
33504a1e6c5SDave Chinner 				sector_size, sbp->sb_sectsize);
3362451337dSDave Chinner 		error = -ENOSYS;
33726af6552SDave Chinner 		goto release_buf;
3381da177e4SLinus Torvalds 	}
3391da177e4SLinus Torvalds 
340556b8883SDave Chinner 	if (buf_ops == NULL) {
3411da177e4SLinus Torvalds 		/*
342daba5427SEric Sandeen 		 * Re-read the superblock so the buffer is correctly sized,
343daba5427SEric Sandeen 		 * and properly verified.
3441da177e4SLinus Torvalds 		 */
3451da177e4SLinus Torvalds 		xfs_buf_relse(bp);
34604a1e6c5SDave Chinner 		sector_size = sbp->sb_sectsize;
347daba5427SEric Sandeen 		buf_ops = loud ? &xfs_sb_buf_ops : &xfs_sb_quiet_buf_ops;
34826af6552SDave Chinner 		goto reread;
3491da177e4SLinus Torvalds 	}
3501da177e4SLinus Torvalds 
3515681ca40SDave Chinner 	xfs_reinit_percpu_counters(mp);
3528d280b98SDavid Chinner 
35304a1e6c5SDave Chinner 	/* no need to be quiet anymore, so reset the buf ops */
35404a1e6c5SDave Chinner 	bp->b_ops = &xfs_sb_buf_ops;
35504a1e6c5SDave Chinner 
3561da177e4SLinus Torvalds 	mp->m_sb_bp = bp;
35726af6552SDave Chinner 	xfs_buf_unlock(bp);
3581da177e4SLinus Torvalds 	return 0;
3591da177e4SLinus Torvalds 
36026af6552SDave Chinner release_buf:
3611da177e4SLinus Torvalds 	xfs_buf_relse(bp);
3621da177e4SLinus Torvalds 	return error;
3631da177e4SLinus Torvalds }
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds /*
3660771fb45SEric Sandeen  * Update alignment values based on mount options and sb values
3671da177e4SLinus Torvalds  */
3680771fb45SEric Sandeen STATIC int
3697884bc86SChristoph Hellwig xfs_update_alignment(xfs_mount_t *mp)
3701da177e4SLinus Torvalds {
3711da177e4SLinus Torvalds 	xfs_sb_t	*sbp = &(mp->m_sb);
3721da177e4SLinus Torvalds 
3734249023aSChristoph Hellwig 	if (mp->m_dalign) {
3741da177e4SLinus Torvalds 		/*
3751da177e4SLinus Torvalds 		 * If stripe unit and stripe width are not multiples
3761da177e4SLinus Torvalds 		 * of the fs blocksize turn off alignment.
3771da177e4SLinus Torvalds 		 */
3781da177e4SLinus Torvalds 		if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
3791da177e4SLinus Torvalds 		    (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
38039a45d84SJie Liu 			xfs_warn(mp,
38139a45d84SJie Liu 		"alignment check failed: sunit/swidth vs. blocksize(%d)",
38239a45d84SJie Liu 				sbp->sb_blocksize);
3832451337dSDave Chinner 			return -EINVAL;
3841da177e4SLinus Torvalds 		} else {
3851da177e4SLinus Torvalds 			/*
3861da177e4SLinus Torvalds 			 * Convert the stripe unit and width to FSBs.
3871da177e4SLinus Torvalds 			 */
3881da177e4SLinus Torvalds 			mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
3891da177e4SLinus Torvalds 			if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
39053487786SDave Chinner 				xfs_warn(mp,
39139a45d84SJie Liu 			"alignment check failed: sunit/swidth vs. agsize(%d)",
3921da177e4SLinus Torvalds 					 sbp->sb_agblocks);
3932451337dSDave Chinner 				return -EINVAL;
3941da177e4SLinus Torvalds 			} else if (mp->m_dalign) {
3951da177e4SLinus Torvalds 				mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
3961da177e4SLinus Torvalds 			} else {
39739a45d84SJie Liu 				xfs_warn(mp,
39839a45d84SJie Liu 			"alignment check failed: sunit(%d) less than bsize(%d)",
39939a45d84SJie Liu 					 mp->m_dalign, sbp->sb_blocksize);
4002451337dSDave Chinner 				return -EINVAL;
4011da177e4SLinus Torvalds 			}
4021da177e4SLinus Torvalds 		}
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 		/*
4051da177e4SLinus Torvalds 		 * Update superblock with new values
4061da177e4SLinus Torvalds 		 * and log changes
4071da177e4SLinus Torvalds 		 */
40862118709SEric Sandeen 		if (xfs_sb_version_hasdalign(sbp)) {
4091da177e4SLinus Torvalds 			if (sbp->sb_unit != mp->m_dalign) {
4101da177e4SLinus Torvalds 				sbp->sb_unit = mp->m_dalign;
41161e63ecbSDave Chinner 				mp->m_update_sb = true;
4121da177e4SLinus Torvalds 			}
4131da177e4SLinus Torvalds 			if (sbp->sb_width != mp->m_swidth) {
4141da177e4SLinus Torvalds 				sbp->sb_width = mp->m_swidth;
41561e63ecbSDave Chinner 				mp->m_update_sb = true;
4161da177e4SLinus Torvalds 			}
41734d7f603SJie Liu 		} else {
41834d7f603SJie Liu 			xfs_warn(mp,
41934d7f603SJie Liu 	"cannot change alignment: superblock does not support data alignment");
4202451337dSDave Chinner 			return -EINVAL;
4211da177e4SLinus Torvalds 		}
4221da177e4SLinus Torvalds 	} else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
42362118709SEric Sandeen 		    xfs_sb_version_hasdalign(&mp->m_sb)) {
4241da177e4SLinus Torvalds 			mp->m_dalign = sbp->sb_unit;
4251da177e4SLinus Torvalds 			mp->m_swidth = sbp->sb_width;
4261da177e4SLinus Torvalds 	}
4271da177e4SLinus Torvalds 
4280771fb45SEric Sandeen 	return 0;
4290771fb45SEric Sandeen }
4301da177e4SLinus Torvalds 
4310771fb45SEric Sandeen /*
4320771fb45SEric Sandeen  * Set the maximum inode count for this filesystem
4330771fb45SEric Sandeen  */
4340771fb45SEric Sandeen STATIC void
4350771fb45SEric Sandeen xfs_set_maxicount(xfs_mount_t *mp)
4360771fb45SEric Sandeen {
4370771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
438c8ce540dSDarrick J. Wong 	uint64_t	icount;
4391da177e4SLinus Torvalds 
4400771fb45SEric Sandeen 	if (sbp->sb_imax_pct) {
4410771fb45SEric Sandeen 		/*
4420771fb45SEric Sandeen 		 * Make sure the maximum inode count is a multiple
4430771fb45SEric Sandeen 		 * of the units we allocate inodes in.
4441da177e4SLinus Torvalds 		 */
4451da177e4SLinus Torvalds 		icount = sbp->sb_dblocks * sbp->sb_imax_pct;
4461da177e4SLinus Torvalds 		do_div(icount, 100);
4471da177e4SLinus Torvalds 		do_div(icount, mp->m_ialloc_blks);
4481da177e4SLinus Torvalds 		mp->m_maxicount = (icount * mp->m_ialloc_blks)  <<
4491da177e4SLinus Torvalds 				   sbp->sb_inopblog;
4500771fb45SEric Sandeen 	} else {
4511da177e4SLinus Torvalds 		mp->m_maxicount = 0;
4521da177e4SLinus Torvalds 	}
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds /*
4561da177e4SLinus Torvalds  * Set the default minimum read and write sizes unless
4571da177e4SLinus Torvalds  * already specified in a mount option.
4581da177e4SLinus Torvalds  * We use smaller I/O sizes when the file system
4591da177e4SLinus Torvalds  * is being used for NFS service (wsync mount option).
4601da177e4SLinus Torvalds  */
4610771fb45SEric Sandeen STATIC void
4620771fb45SEric Sandeen xfs_set_rw_sizes(xfs_mount_t *mp)
4630771fb45SEric Sandeen {
4640771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
4650771fb45SEric Sandeen 	int		readio_log, writeio_log;
4660771fb45SEric Sandeen 
4671da177e4SLinus Torvalds 	if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
4681da177e4SLinus Torvalds 		if (mp->m_flags & XFS_MOUNT_WSYNC) {
4691da177e4SLinus Torvalds 			readio_log = XFS_WSYNC_READIO_LOG;
4701da177e4SLinus Torvalds 			writeio_log = XFS_WSYNC_WRITEIO_LOG;
4711da177e4SLinus Torvalds 		} else {
4721da177e4SLinus Torvalds 			readio_log = XFS_READIO_LOG_LARGE;
4731da177e4SLinus Torvalds 			writeio_log = XFS_WRITEIO_LOG_LARGE;
4741da177e4SLinus Torvalds 		}
4751da177e4SLinus Torvalds 	} else {
4761da177e4SLinus Torvalds 		readio_log = mp->m_readio_log;
4771da177e4SLinus Torvalds 		writeio_log = mp->m_writeio_log;
4781da177e4SLinus Torvalds 	}
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	if (sbp->sb_blocklog > readio_log) {
4811da177e4SLinus Torvalds 		mp->m_readio_log = sbp->sb_blocklog;
4821da177e4SLinus Torvalds 	} else {
4831da177e4SLinus Torvalds 		mp->m_readio_log = readio_log;
4841da177e4SLinus Torvalds 	}
4851da177e4SLinus Torvalds 	mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
4861da177e4SLinus Torvalds 	if (sbp->sb_blocklog > writeio_log) {
4871da177e4SLinus Torvalds 		mp->m_writeio_log = sbp->sb_blocklog;
4881da177e4SLinus Torvalds 	} else {
4891da177e4SLinus Torvalds 		mp->m_writeio_log = writeio_log;
4901da177e4SLinus Torvalds 	}
4911da177e4SLinus Torvalds 	mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
4920771fb45SEric Sandeen }
493425f9dddSEric Sandeen 
4941da177e4SLinus Torvalds /*
495055388a3SDave Chinner  * precalculate the low space thresholds for dynamic speculative preallocation.
496055388a3SDave Chinner  */
497055388a3SDave Chinner void
498055388a3SDave Chinner xfs_set_low_space_thresholds(
499055388a3SDave Chinner 	struct xfs_mount	*mp)
500055388a3SDave Chinner {
501055388a3SDave Chinner 	int i;
502055388a3SDave Chinner 
503055388a3SDave Chinner 	for (i = 0; i < XFS_LOWSP_MAX; i++) {
504c8ce540dSDarrick J. Wong 		uint64_t space = mp->m_sb.sb_dblocks;
505055388a3SDave Chinner 
506055388a3SDave Chinner 		do_div(space, 100);
507055388a3SDave Chinner 		mp->m_low_space[i] = space * (i + 1);
508055388a3SDave Chinner 	}
509055388a3SDave Chinner }
510055388a3SDave Chinner 
511055388a3SDave Chinner 
512055388a3SDave Chinner /*
5131da177e4SLinus Torvalds  * Set whether we're using inode alignment.
5141da177e4SLinus Torvalds  */
5150771fb45SEric Sandeen STATIC void
5160771fb45SEric Sandeen xfs_set_inoalignment(xfs_mount_t *mp)
5170771fb45SEric Sandeen {
51862118709SEric Sandeen 	if (xfs_sb_version_hasalign(&mp->m_sb) &&
519d5825712SChandan Rajendra 		mp->m_sb.sb_inoalignmt >= xfs_icluster_size_fsb(mp))
5201da177e4SLinus Torvalds 		mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
5211da177e4SLinus Torvalds 	else
5221da177e4SLinus Torvalds 		mp->m_inoalign_mask = 0;
5231da177e4SLinus Torvalds 	/*
5241da177e4SLinus Torvalds 	 * If we are using stripe alignment, check whether
5251da177e4SLinus Torvalds 	 * the stripe unit is a multiple of the inode alignment
5261da177e4SLinus Torvalds 	 */
5271da177e4SLinus Torvalds 	if (mp->m_dalign && mp->m_inoalign_mask &&
5281da177e4SLinus Torvalds 	    !(mp->m_dalign & mp->m_inoalign_mask))
5291da177e4SLinus Torvalds 		mp->m_sinoalign = mp->m_dalign;
5301da177e4SLinus Torvalds 	else
5311da177e4SLinus Torvalds 		mp->m_sinoalign = 0;
5320771fb45SEric Sandeen }
5330771fb45SEric Sandeen 
5341da177e4SLinus Torvalds /*
5350471f62eSZhi Yong Wu  * Check that the data (and log if separate) is an ok size.
5361da177e4SLinus Torvalds  */
5370771fb45SEric Sandeen STATIC int
538ba372674SDave Chinner xfs_check_sizes(
539ba372674SDave Chinner 	struct xfs_mount *mp)
5400771fb45SEric Sandeen {
541ba372674SDave Chinner 	struct xfs_buf	*bp;
5420771fb45SEric Sandeen 	xfs_daddr_t	d;
543ba372674SDave Chinner 	int		error;
5440771fb45SEric Sandeen 
5451da177e4SLinus Torvalds 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
5461da177e4SLinus Torvalds 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
5470b932cccSDave Chinner 		xfs_warn(mp, "filesystem size mismatch detected");
5482451337dSDave Chinner 		return -EFBIG;
5491da177e4SLinus Torvalds 	}
550ba372674SDave Chinner 	error = xfs_buf_read_uncached(mp->m_ddev_targp,
5511da177e4SLinus Torvalds 					d - XFS_FSS_TO_BB(mp, 1),
552ba372674SDave Chinner 					XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL);
553ba372674SDave Chinner 	if (error) {
5540b932cccSDave Chinner 		xfs_warn(mp, "last sector read failed");
555ba372674SDave Chinner 		return error;
5561da177e4SLinus Torvalds 	}
5571922c949SDave Chinner 	xfs_buf_relse(bp);
5581da177e4SLinus Torvalds 
559ba372674SDave Chinner 	if (mp->m_logdev_targp == mp->m_ddev_targp)
560ba372674SDave Chinner 		return 0;
561ba372674SDave Chinner 
5621da177e4SLinus Torvalds 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
5631da177e4SLinus Torvalds 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
5640b932cccSDave Chinner 		xfs_warn(mp, "log size mismatch detected");
5652451337dSDave Chinner 		return -EFBIG;
5661da177e4SLinus Torvalds 	}
567ba372674SDave Chinner 	error = xfs_buf_read_uncached(mp->m_logdev_targp,
5681da177e4SLinus Torvalds 					d - XFS_FSB_TO_BB(mp, 1),
569ba372674SDave Chinner 					XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
570ba372674SDave Chinner 	if (error) {
5710b932cccSDave Chinner 		xfs_warn(mp, "log device read failed");
572ba372674SDave Chinner 		return error;
5731da177e4SLinus Torvalds 	}
5741922c949SDave Chinner 	xfs_buf_relse(bp);
5750771fb45SEric Sandeen 	return 0;
5760771fb45SEric Sandeen }
5770771fb45SEric Sandeen 
5780771fb45SEric Sandeen /*
5797d095257SChristoph Hellwig  * Clear the quotaflags in memory and in the superblock.
5807d095257SChristoph Hellwig  */
5817d095257SChristoph Hellwig int
5827d095257SChristoph Hellwig xfs_mount_reset_sbqflags(
5837d095257SChristoph Hellwig 	struct xfs_mount	*mp)
5847d095257SChristoph Hellwig {
5857d095257SChristoph Hellwig 	mp->m_qflags = 0;
5867d095257SChristoph Hellwig 
58761e63ecbSDave Chinner 	/* It is OK to look at sb_qflags in the mount path without m_sb_lock. */
5887d095257SChristoph Hellwig 	if (mp->m_sb.sb_qflags == 0)
5897d095257SChristoph Hellwig 		return 0;
5907d095257SChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
5917d095257SChristoph Hellwig 	mp->m_sb.sb_qflags = 0;
5927d095257SChristoph Hellwig 	spin_unlock(&mp->m_sb_lock);
5937d095257SChristoph Hellwig 
59461e63ecbSDave Chinner 	if (!xfs_fs_writable(mp, SB_FREEZE_WRITE))
5957d095257SChristoph Hellwig 		return 0;
5967d095257SChristoph Hellwig 
59761e63ecbSDave Chinner 	return xfs_sync_sb(mp, false);
5987d095257SChristoph Hellwig }
5997d095257SChristoph Hellwig 
600c8ce540dSDarrick J. Wong uint64_t
601d5db0f97SEric Sandeen xfs_default_resblks(xfs_mount_t *mp)
602d5db0f97SEric Sandeen {
603c8ce540dSDarrick J. Wong 	uint64_t resblks;
604d5db0f97SEric Sandeen 
605d5db0f97SEric Sandeen 	/*
6068babd8a2SDave Chinner 	 * We default to 5% or 8192 fsbs of space reserved, whichever is
6078babd8a2SDave Chinner 	 * smaller.  This is intended to cover concurrent allocation
6088babd8a2SDave Chinner 	 * transactions when we initially hit enospc. These each require a 4
6098babd8a2SDave Chinner 	 * block reservation. Hence by default we cover roughly 2000 concurrent
6108babd8a2SDave Chinner 	 * allocation reservations.
611d5db0f97SEric Sandeen 	 */
612d5db0f97SEric Sandeen 	resblks = mp->m_sb.sb_dblocks;
613d5db0f97SEric Sandeen 	do_div(resblks, 20);
614c8ce540dSDarrick J. Wong 	resblks = min_t(uint64_t, resblks, 8192);
615d5db0f97SEric Sandeen 	return resblks;
616d5db0f97SEric Sandeen }
617d5db0f97SEric Sandeen 
6187d095257SChristoph Hellwig /*
6190771fb45SEric Sandeen  * This function does the following on an initial mount of a file system:
6200771fb45SEric Sandeen  *	- reads the superblock from disk and init the mount struct
6210771fb45SEric Sandeen  *	- if we're a 32-bit kernel, do a size check on the superblock
6220771fb45SEric Sandeen  *		so we don't mount terabyte filesystems
6230771fb45SEric Sandeen  *	- init mount struct realtime fields
6240771fb45SEric Sandeen  *	- allocate inode hash table for fs
6250771fb45SEric Sandeen  *	- init directory manager
6260771fb45SEric Sandeen  *	- perform recovery and init the log manager
6270771fb45SEric Sandeen  */
6280771fb45SEric Sandeen int
6290771fb45SEric Sandeen xfs_mountfs(
630f0b2efadSBrian Foster 	struct xfs_mount	*mp)
6310771fb45SEric Sandeen {
632f0b2efadSBrian Foster 	struct xfs_sb		*sbp = &(mp->m_sb);
633f0b2efadSBrian Foster 	struct xfs_inode	*rip;
634c8ce540dSDarrick J. Wong 	uint64_t		resblks;
6357d095257SChristoph Hellwig 	uint			quotamount = 0;
6367d095257SChristoph Hellwig 	uint			quotaflags = 0;
6370771fb45SEric Sandeen 	int			error = 0;
6380771fb45SEric Sandeen 
639ff55068cSDave Chinner 	xfs_sb_mount_common(mp, sbp);
6400771fb45SEric Sandeen 
6410771fb45SEric Sandeen 	/*
642074e427bSDave Chinner 	 * Check for a mismatched features2 values.  Older kernels read & wrote
643074e427bSDave Chinner 	 * into the wrong sb offset for sb_features2 on some platforms due to
644074e427bSDave Chinner 	 * xfs_sb_t not being 64bit size aligned when sb_features2 was added,
645074e427bSDave Chinner 	 * which made older superblock reading/writing routines swap it as a
646074e427bSDave Chinner 	 * 64-bit value.
647ee1c0908SDavid Chinner 	 *
648e6957ea4SEric Sandeen 	 * For backwards compatibility, we make both slots equal.
649e6957ea4SEric Sandeen 	 *
650074e427bSDave Chinner 	 * If we detect a mismatched field, we OR the set bits into the existing
651074e427bSDave Chinner 	 * features2 field in case it has already been modified; we don't want
652074e427bSDave Chinner 	 * to lose any features.  We then update the bad location with the ORed
653074e427bSDave Chinner 	 * value so that older kernels will see any features2 flags. The
654074e427bSDave Chinner 	 * superblock writeback code ensures the new sb_features2 is copied to
655074e427bSDave Chinner 	 * sb_bad_features2 before it is logged or written to disk.
656ee1c0908SDavid Chinner 	 */
657e6957ea4SEric Sandeen 	if (xfs_sb_has_mismatched_features2(sbp)) {
6580b932cccSDave Chinner 		xfs_warn(mp, "correcting sb_features alignment problem");
659ee1c0908SDavid Chinner 		sbp->sb_features2 |= sbp->sb_bad_features2;
66061e63ecbSDave Chinner 		mp->m_update_sb = true;
661e6957ea4SEric Sandeen 
662e6957ea4SEric Sandeen 		/*
663e6957ea4SEric Sandeen 		 * Re-check for ATTR2 in case it was found in bad_features2
664e6957ea4SEric Sandeen 		 * slot.
665e6957ea4SEric Sandeen 		 */
6667c12f296STim Shimmin 		if (xfs_sb_version_hasattr2(&mp->m_sb) &&
6677c12f296STim Shimmin 		   !(mp->m_flags & XFS_MOUNT_NOATTR2))
668e6957ea4SEric Sandeen 			mp->m_flags |= XFS_MOUNT_ATTR2;
6697c12f296STim Shimmin 	}
670e6957ea4SEric Sandeen 
6717c12f296STim Shimmin 	if (xfs_sb_version_hasattr2(&mp->m_sb) &&
6727c12f296STim Shimmin 	   (mp->m_flags & XFS_MOUNT_NOATTR2)) {
6737c12f296STim Shimmin 		xfs_sb_version_removeattr2(&mp->m_sb);
67461e63ecbSDave Chinner 		mp->m_update_sb = true;
6757c12f296STim Shimmin 
6767c12f296STim Shimmin 		/* update sb_versionnum for the clearing of the morebits */
6777c12f296STim Shimmin 		if (!sbp->sb_features2)
67861e63ecbSDave Chinner 			mp->m_update_sb = true;
679ee1c0908SDavid Chinner 	}
680ee1c0908SDavid Chinner 
681263997a6SDave Chinner 	/* always use v2 inodes by default now */
682263997a6SDave Chinner 	if (!(mp->m_sb.sb_versionnum & XFS_SB_VERSION_NLINKBIT)) {
683263997a6SDave Chinner 		mp->m_sb.sb_versionnum |= XFS_SB_VERSION_NLINKBIT;
68461e63ecbSDave Chinner 		mp->m_update_sb = true;
685263997a6SDave Chinner 	}
686263997a6SDave Chinner 
687ee1c0908SDavid Chinner 	/*
6880771fb45SEric Sandeen 	 * Check if sb_agblocks is aligned at stripe boundary
6890771fb45SEric Sandeen 	 * If sb_agblocks is NOT aligned turn off m_dalign since
6900771fb45SEric Sandeen 	 * allocator alignment is within an ag, therefore ag has
6910771fb45SEric Sandeen 	 * to be aligned at stripe boundary.
6920771fb45SEric Sandeen 	 */
6937884bc86SChristoph Hellwig 	error = xfs_update_alignment(mp);
6940771fb45SEric Sandeen 	if (error)
695f9057e3dSChristoph Hellwig 		goto out;
6960771fb45SEric Sandeen 
6970771fb45SEric Sandeen 	xfs_alloc_compute_maxlevels(mp);
6980771fb45SEric Sandeen 	xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
6990771fb45SEric Sandeen 	xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
7000771fb45SEric Sandeen 	xfs_ialloc_compute_maxlevels(mp);
701035e00acSDarrick J. Wong 	xfs_rmapbt_compute_maxlevels(mp);
7021946b91cSDarrick J. Wong 	xfs_refcountbt_compute_maxlevels(mp);
7030771fb45SEric Sandeen 
7040771fb45SEric Sandeen 	xfs_set_maxicount(mp);
7050771fb45SEric Sandeen 
706e6b3bb78SCarlos Maiolino 	/* enable fail_at_unmount as default */
707e6b3bb78SCarlos Maiolino 	mp->m_fail_unmount = 1;
708e6b3bb78SCarlos Maiolino 
709a31b1d3dSBrian Foster 	error = xfs_sysfs_init(&mp->m_kobj, &xfs_mp_ktype, NULL, mp->m_fsname);
71027174203SChristoph Hellwig 	if (error)
711f9057e3dSChristoph Hellwig 		goto out;
7121da177e4SLinus Torvalds 
713225e4635SBill O'Donnell 	error = xfs_sysfs_init(&mp->m_stats.xs_kobj, &xfs_stats_ktype,
714225e4635SBill O'Donnell 			       &mp->m_kobj, "stats");
715a31b1d3dSBrian Foster 	if (error)
716a31b1d3dSBrian Foster 		goto out_remove_sysfs;
717a31b1d3dSBrian Foster 
718192852beSCarlos Maiolino 	error = xfs_error_sysfs_init(mp);
719225e4635SBill O'Donnell 	if (error)
720225e4635SBill O'Donnell 		goto out_del_stats;
721225e4635SBill O'Donnell 
72231965ef3SDarrick J. Wong 	error = xfs_errortag_init(mp);
72331965ef3SDarrick J. Wong 	if (error)
72431965ef3SDarrick J. Wong 		goto out_remove_error_sysfs;
725192852beSCarlos Maiolino 
726192852beSCarlos Maiolino 	error = xfs_uuid_mount(mp);
727192852beSCarlos Maiolino 	if (error)
72831965ef3SDarrick J. Wong 		goto out_remove_errortag;
729192852beSCarlos Maiolino 
7301da177e4SLinus Torvalds 	/*
7310771fb45SEric Sandeen 	 * Set the minimum read and write sizes
7320771fb45SEric Sandeen 	 */
7330771fb45SEric Sandeen 	xfs_set_rw_sizes(mp);
7340771fb45SEric Sandeen 
735055388a3SDave Chinner 	/* set the low space thresholds for dynamic preallocation */
736055388a3SDave Chinner 	xfs_set_low_space_thresholds(mp);
737055388a3SDave Chinner 
7380771fb45SEric Sandeen 	/*
7390771fb45SEric Sandeen 	 * Set the inode cluster size.
7400771fb45SEric Sandeen 	 * This may still be overridden by the file system
7410771fb45SEric Sandeen 	 * block size if it is larger than the chosen cluster size.
7428f80587bSDave Chinner 	 *
7438f80587bSDave Chinner 	 * For v5 filesystems, scale the cluster size with the inode size to
7448f80587bSDave Chinner 	 * keep a constant ratio of inode per cluster buffer, but only if mkfs
7458f80587bSDave Chinner 	 * has set the inode alignment value appropriately for larger cluster
7468f80587bSDave Chinner 	 * sizes.
7470771fb45SEric Sandeen 	 */
7480771fb45SEric Sandeen 	mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
7498f80587bSDave Chinner 	if (xfs_sb_version_hascrc(&mp->m_sb)) {
7508f80587bSDave Chinner 		int	new_size = mp->m_inode_cluster_size;
7518f80587bSDave Chinner 
7528f80587bSDave Chinner 		new_size *= mp->m_sb.sb_inodesize / XFS_DINODE_MIN_SIZE;
7538f80587bSDave Chinner 		if (mp->m_sb.sb_inoalignmt >= XFS_B_TO_FSBT(mp, new_size))
7548f80587bSDave Chinner 			mp->m_inode_cluster_size = new_size;
7558f80587bSDave Chinner 	}
7560771fb45SEric Sandeen 
7570771fb45SEric Sandeen 	/*
758e5376fc1SBrian Foster 	 * If enabled, sparse inode chunk alignment is expected to match the
759e5376fc1SBrian Foster 	 * cluster size. Full inode chunk alignment must match the chunk size,
760e5376fc1SBrian Foster 	 * but that is checked on sb read verification...
761e5376fc1SBrian Foster 	 */
762e5376fc1SBrian Foster 	if (xfs_sb_version_hassparseinodes(&mp->m_sb) &&
763e5376fc1SBrian Foster 	    mp->m_sb.sb_spino_align !=
764e5376fc1SBrian Foster 			XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size)) {
765e5376fc1SBrian Foster 		xfs_warn(mp,
766e5376fc1SBrian Foster 	"Sparse inode block alignment (%u) must match cluster size (%llu).",
767e5376fc1SBrian Foster 			 mp->m_sb.sb_spino_align,
768e5376fc1SBrian Foster 			 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size));
769e5376fc1SBrian Foster 		error = -EINVAL;
770e5376fc1SBrian Foster 		goto out_remove_uuid;
771e5376fc1SBrian Foster 	}
772e5376fc1SBrian Foster 
773e5376fc1SBrian Foster 	/*
7740771fb45SEric Sandeen 	 * Set inode alignment fields
7750771fb45SEric Sandeen 	 */
7760771fb45SEric Sandeen 	xfs_set_inoalignment(mp);
7770771fb45SEric Sandeen 
7780771fb45SEric Sandeen 	/*
779c2bfbc9bSZhi Yong Wu 	 * Check that the data (and log if separate) is an ok size.
7800771fb45SEric Sandeen 	 */
7814249023aSChristoph Hellwig 	error = xfs_check_sizes(mp);
7820771fb45SEric Sandeen 	if (error)
783f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
7840771fb45SEric Sandeen 
7850771fb45SEric Sandeen 	/*
7861da177e4SLinus Torvalds 	 * Initialize realtime fields in the mount structure
7871da177e4SLinus Torvalds 	 */
7880771fb45SEric Sandeen 	error = xfs_rtmount_init(mp);
7890771fb45SEric Sandeen 	if (error) {
7900b932cccSDave Chinner 		xfs_warn(mp, "RT mount failed");
791f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
7921da177e4SLinus Torvalds 	}
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	/*
7951da177e4SLinus Torvalds 	 *  Copies the low order bits of the timestamp and the randomly
7961da177e4SLinus Torvalds 	 *  set "sequence" number out of a UUID.
7971da177e4SLinus Torvalds 	 */
798cb0ba6ccSChristoph Hellwig 	mp->m_fixedfsid[0] =
799cb0ba6ccSChristoph Hellwig 		(get_unaligned_be16(&sbp->sb_uuid.b[8]) << 16) |
800cb0ba6ccSChristoph Hellwig 		 get_unaligned_be16(&sbp->sb_uuid.b[4]);
801cb0ba6ccSChristoph Hellwig 	mp->m_fixedfsid[1] = get_unaligned_be32(&sbp->sb_uuid.b[0]);
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	mp->m_dmevmask = 0;	/* not persistent; set after each mount */
8041da177e4SLinus Torvalds 
8050650b554SDave Chinner 	error = xfs_da_mount(mp);
8060650b554SDave Chinner 	if (error) {
8070650b554SDave Chinner 		xfs_warn(mp, "Failed dir/attr init: %d", error);
8080650b554SDave Chinner 		goto out_remove_uuid;
8090650b554SDave Chinner 	}
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 	/*
8121da177e4SLinus Torvalds 	 * Initialize the precomputed transaction reservations values.
8131da177e4SLinus Torvalds 	 */
8141da177e4SLinus Torvalds 	xfs_trans_init(mp);
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 	/*
8171da177e4SLinus Torvalds 	 * Allocate and initialize the per-ag data.
8181da177e4SLinus Torvalds 	 */
8191c1c6ebcSDave Chinner 	spin_lock_init(&mp->m_perag_lock);
8209b98b6f3SDave Chinner 	INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
8211c1c6ebcSDave Chinner 	error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
8221c1c6ebcSDave Chinner 	if (error) {
8230b932cccSDave Chinner 		xfs_warn(mp, "Failed per-ag init: %d", error);
8240650b554SDave Chinner 		goto out_free_dir;
8251c1c6ebcSDave Chinner 	}
8261da177e4SLinus Torvalds 
827f9057e3dSChristoph Hellwig 	if (!sbp->sb_logblocks) {
8280b932cccSDave Chinner 		xfs_warn(mp, "no log defined");
829f9057e3dSChristoph Hellwig 		XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp);
8302451337dSDave Chinner 		error = -EFSCORRUPTED;
831f9057e3dSChristoph Hellwig 		goto out_free_perag;
832f9057e3dSChristoph Hellwig 	}
833f9057e3dSChristoph Hellwig 
8341da177e4SLinus Torvalds 	/*
835f0b2efadSBrian Foster 	 * Log's mount-time initialization. The first part of recovery can place
836f0b2efadSBrian Foster 	 * some items on the AIL, to be handled when recovery is finished or
837f0b2efadSBrian Foster 	 * cancelled.
8381da177e4SLinus Torvalds 	 */
8391da177e4SLinus Torvalds 	error = xfs_log_mount(mp, mp->m_logdev_targp,
8401da177e4SLinus Torvalds 			      XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
8411da177e4SLinus Torvalds 			      XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
8421da177e4SLinus Torvalds 	if (error) {
8430b932cccSDave Chinner 		xfs_warn(mp, "log mount failed");
844d4f3512bSDave Chinner 		goto out_fail_wait;
8451da177e4SLinus Torvalds 	}
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	/*
84892821e2bSDavid Chinner 	 * Now the log is mounted, we know if it was an unclean shutdown or
84992821e2bSDavid Chinner 	 * not. If it was, with the first phase of recovery has completed, we
85092821e2bSDavid Chinner 	 * have consistent AG blocks on disk. We have not recovered EFIs yet,
85192821e2bSDavid Chinner 	 * but they are recovered transactionally in the second recovery phase
85292821e2bSDavid Chinner 	 * later.
85392821e2bSDavid Chinner 	 *
85492821e2bSDavid Chinner 	 * Hence we can safely re-initialise incore superblock counters from
85592821e2bSDavid Chinner 	 * the per-ag data. These may not be correct if the filesystem was not
85692821e2bSDavid Chinner 	 * cleanly unmounted, so we need to wait for recovery to finish before
85792821e2bSDavid Chinner 	 * doing this.
85892821e2bSDavid Chinner 	 *
85992821e2bSDavid Chinner 	 * If the filesystem was cleanly unmounted, then we can trust the
86092821e2bSDavid Chinner 	 * values in the superblock to be correct and we don't need to do
86192821e2bSDavid Chinner 	 * anything here.
86292821e2bSDavid Chinner 	 *
86392821e2bSDavid Chinner 	 * If we are currently making the filesystem, the initialisation will
86492821e2bSDavid Chinner 	 * fail as the perag data is in an undefined state.
86592821e2bSDavid Chinner 	 */
86692821e2bSDavid Chinner 	if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
86792821e2bSDavid Chinner 	    !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
86892821e2bSDavid Chinner 	     !mp->m_sb.sb_inprogress) {
86992821e2bSDavid Chinner 		error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
870f9057e3dSChristoph Hellwig 		if (error)
8716eee8972Skbuild test robot 			goto out_log_dealloc;
87292821e2bSDavid Chinner 	}
873f9057e3dSChristoph Hellwig 
87492821e2bSDavid Chinner 	/*
8751da177e4SLinus Torvalds 	 * Get and sanity-check the root inode.
8761da177e4SLinus Torvalds 	 * Save the pointer to it in the mount structure.
8771da177e4SLinus Torvalds 	 */
8787b6259e7SDave Chinner 	error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip);
8791da177e4SLinus Torvalds 	if (error) {
8800b932cccSDave Chinner 		xfs_warn(mp, "failed to read root inode");
881f9057e3dSChristoph Hellwig 		goto out_log_dealloc;
8821da177e4SLinus Torvalds 	}
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	ASSERT(rip != NULL);
8851da177e4SLinus Torvalds 
886c19b3b05SDave Chinner 	if (unlikely(!S_ISDIR(VFS_I(rip)->i_mode))) {
8870b932cccSDave Chinner 		xfs_warn(mp, "corrupted root inode %llu: not a directory",
888b6574520SNathan Scott 			(unsigned long long)rip->i_ino);
8891da177e4SLinus Torvalds 		xfs_iunlock(rip, XFS_ILOCK_EXCL);
8901da177e4SLinus Torvalds 		XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
8911da177e4SLinus Torvalds 				 mp);
8922451337dSDave Chinner 		error = -EFSCORRUPTED;
893f9057e3dSChristoph Hellwig 		goto out_rele_rip;
8941da177e4SLinus Torvalds 	}
8951da177e4SLinus Torvalds 	mp->m_rootip = rip;	/* save it */
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 	xfs_iunlock(rip, XFS_ILOCK_EXCL);
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 	/*
9001da177e4SLinus Torvalds 	 * Initialize realtime inode pointers in the mount structure
9011da177e4SLinus Torvalds 	 */
9020771fb45SEric Sandeen 	error = xfs_rtmount_inodes(mp);
9030771fb45SEric Sandeen 	if (error) {
9041da177e4SLinus Torvalds 		/*
9051da177e4SLinus Torvalds 		 * Free up the root inode.
9061da177e4SLinus Torvalds 		 */
9070b932cccSDave Chinner 		xfs_warn(mp, "failed to read RT inodes");
908f9057e3dSChristoph Hellwig 		goto out_rele_rip;
9091da177e4SLinus Torvalds 	}
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	/*
9127884bc86SChristoph Hellwig 	 * If this is a read-only mount defer the superblock updates until
9137884bc86SChristoph Hellwig 	 * the next remount into writeable mode.  Otherwise we would never
9147884bc86SChristoph Hellwig 	 * perform the update e.g. for the root filesystem.
9151da177e4SLinus Torvalds 	 */
91661e63ecbSDave Chinner 	if (mp->m_update_sb && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
91761e63ecbSDave Chinner 		error = xfs_sync_sb(mp, false);
918e5720eecSDavid Chinner 		if (error) {
9190b932cccSDave Chinner 			xfs_warn(mp, "failed to write sb changes");
920b93b6e43SChristoph Hellwig 			goto out_rtunmount;
921e5720eecSDavid Chinner 		}
922e5720eecSDavid Chinner 	}
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds 	/*
9251da177e4SLinus Torvalds 	 * Initialise the XFS quota management subsystem for this mount
9261da177e4SLinus Torvalds 	 */
9277d095257SChristoph Hellwig 	if (XFS_IS_QUOTA_RUNNING(mp)) {
9287d095257SChristoph Hellwig 		error = xfs_qm_newmount(mp, &quotamount, &quotaflags);
9290771fb45SEric Sandeen 		if (error)
930b93b6e43SChristoph Hellwig 			goto out_rtunmount;
9317d095257SChristoph Hellwig 	} else {
9327d095257SChristoph Hellwig 		ASSERT(!XFS_IS_QUOTA_ON(mp));
9337d095257SChristoph Hellwig 
9347d095257SChristoph Hellwig 		/*
9357d095257SChristoph Hellwig 		 * If a file system had quotas running earlier, but decided to
9367d095257SChristoph Hellwig 		 * mount without -o uquota/pquota/gquota options, revoke the
9377d095257SChristoph Hellwig 		 * quotachecked license.
9387d095257SChristoph Hellwig 		 */
9397d095257SChristoph Hellwig 		if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) {
9400b932cccSDave Chinner 			xfs_notice(mp, "resetting quota flags");
9417d095257SChristoph Hellwig 			error = xfs_mount_reset_sbqflags(mp);
9427d095257SChristoph Hellwig 			if (error)
943a70a4fa5SBrian Foster 				goto out_rtunmount;
9447d095257SChristoph Hellwig 		}
9457d095257SChristoph Hellwig 	}
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	/*
948f0b2efadSBrian Foster 	 * Finish recovering the file system.  This part needed to be delayed
949f0b2efadSBrian Foster 	 * until after the root and real-time bitmap inodes were consistently
950f0b2efadSBrian Foster 	 * read in.
9511da177e4SLinus Torvalds 	 */
9524249023aSChristoph Hellwig 	error = xfs_log_mount_finish(mp);
9531da177e4SLinus Torvalds 	if (error) {
9540b932cccSDave Chinner 		xfs_warn(mp, "log mount finish failed");
955b93b6e43SChristoph Hellwig 		goto out_rtunmount;
9561da177e4SLinus Torvalds 	}
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	/*
959ddeb14f4SDave Chinner 	 * Now the log is fully replayed, we can transition to full read-only
960ddeb14f4SDave Chinner 	 * mode for read-only mounts. This will sync all the metadata and clean
961ddeb14f4SDave Chinner 	 * the log so that the recovery we just performed does not have to be
962ddeb14f4SDave Chinner 	 * replayed again on the next mount.
963ddeb14f4SDave Chinner 	 *
964ddeb14f4SDave Chinner 	 * We use the same quiesce mechanism as the rw->ro remount, as they are
965ddeb14f4SDave Chinner 	 * semantically identical operations.
966ddeb14f4SDave Chinner 	 */
967ddeb14f4SDave Chinner 	if ((mp->m_flags & (XFS_MOUNT_RDONLY|XFS_MOUNT_NORECOVERY)) ==
968ddeb14f4SDave Chinner 							XFS_MOUNT_RDONLY) {
969ddeb14f4SDave Chinner 		xfs_quiesce_attr(mp);
970ddeb14f4SDave Chinner 	}
971ddeb14f4SDave Chinner 
972ddeb14f4SDave Chinner 	/*
9731da177e4SLinus Torvalds 	 * Complete the quota initialisation, post-log-replay component.
9741da177e4SLinus Torvalds 	 */
9757d095257SChristoph Hellwig 	if (quotamount) {
9767d095257SChristoph Hellwig 		ASSERT(mp->m_qflags == 0);
9777d095257SChristoph Hellwig 		mp->m_qflags = quotaflags;
9787d095257SChristoph Hellwig 
9797d095257SChristoph Hellwig 		xfs_qm_mount_quotas(mp);
9807d095257SChristoph Hellwig 	}
9817d095257SChristoph Hellwig 
98284e1e99fSDavid Chinner 	/*
98384e1e99fSDavid Chinner 	 * Now we are mounted, reserve a small amount of unused space for
98484e1e99fSDavid Chinner 	 * privileged transactions. This is needed so that transaction
98584e1e99fSDavid Chinner 	 * space required for critical operations can dip into this pool
98684e1e99fSDavid Chinner 	 * when at ENOSPC. This is needed for operations like create with
98784e1e99fSDavid Chinner 	 * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
98884e1e99fSDavid Chinner 	 * are not allowed to use this reserved space.
9898babd8a2SDave Chinner 	 *
9908babd8a2SDave Chinner 	 * This may drive us straight to ENOSPC on mount, but that implies
9918babd8a2SDave Chinner 	 * we were already there on the last unmount. Warn if this occurs.
99284e1e99fSDavid Chinner 	 */
993d5db0f97SEric Sandeen 	if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
994d5db0f97SEric Sandeen 		resblks = xfs_default_resblks(mp);
995714082bcSDavid Chinner 		error = xfs_reserve_blocks(mp, &resblks, NULL);
996714082bcSDavid Chinner 		if (error)
9970b932cccSDave Chinner 			xfs_warn(mp,
9980b932cccSDave Chinner 	"Unable to allocate reserve blocks. Continuing without reserve pool.");
999174edb0eSDarrick J. Wong 
1000174edb0eSDarrick J. Wong 		/* Recover any CoW blocks that never got remapped. */
1001174edb0eSDarrick J. Wong 		error = xfs_reflink_recover_cow(mp);
1002174edb0eSDarrick J. Wong 		if (error) {
1003174edb0eSDarrick J. Wong 			xfs_err(mp,
1004174edb0eSDarrick J. Wong 	"Error %d recovering leftover CoW allocations.", error);
1005174edb0eSDarrick J. Wong 			xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1006174edb0eSDarrick J. Wong 			goto out_quota;
1007174edb0eSDarrick J. Wong 		}
100884d69619SDarrick J. Wong 
100984d69619SDarrick J. Wong 		/* Reserve AG blocks for future btree expansion. */
101084d69619SDarrick J. Wong 		error = xfs_fs_reserve_ag_blocks(mp);
101184d69619SDarrick J. Wong 		if (error && error != -ENOSPC)
101284d69619SDarrick J. Wong 			goto out_agresv;
1013d5db0f97SEric Sandeen 	}
101484e1e99fSDavid Chinner 
10151da177e4SLinus Torvalds 	return 0;
10161da177e4SLinus Torvalds 
101784d69619SDarrick J. Wong  out_agresv:
101884d69619SDarrick J. Wong 	xfs_fs_unreserve_ag_blocks(mp);
1019174edb0eSDarrick J. Wong  out_quota:
1020174edb0eSDarrick J. Wong 	xfs_qm_unmount_quotas(mp);
1021b93b6e43SChristoph Hellwig  out_rtunmount:
1022b93b6e43SChristoph Hellwig 	xfs_rtunmount_inodes(mp);
1023f9057e3dSChristoph Hellwig  out_rele_rip:
102443355099SChristoph Hellwig 	IRELE(rip);
10250ae120f8SBrian Foster 	cancel_delayed_work_sync(&mp->m_reclaim_work);
10260ae120f8SBrian Foster 	xfs_reclaim_inodes(mp, SYNC_WAIT);
1027*77aff8c7SDarrick J. Wong 	/* Clean out dquots that might be in memory after quotacheck. */
1028*77aff8c7SDarrick J. Wong 	xfs_qm_unmount(mp);
1029f9057e3dSChristoph Hellwig  out_log_dealloc:
1030e6b3bb78SCarlos Maiolino 	mp->m_flags |= XFS_MOUNT_UNMOUNTING;
1031f0b2efadSBrian Foster 	xfs_log_mount_cancel(mp);
1032d4f3512bSDave Chinner  out_fail_wait:
1033d4f3512bSDave Chinner 	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
1034d4f3512bSDave Chinner 		xfs_wait_buftarg(mp->m_logdev_targp);
1035d4f3512bSDave Chinner 	xfs_wait_buftarg(mp->m_ddev_targp);
1036f9057e3dSChristoph Hellwig  out_free_perag:
1037ff4f038cSChristoph Hellwig 	xfs_free_perag(mp);
10380650b554SDave Chinner  out_free_dir:
10390650b554SDave Chinner 	xfs_da_unmount(mp);
1040f9057e3dSChristoph Hellwig  out_remove_uuid:
104127174203SChristoph Hellwig 	xfs_uuid_unmount(mp);
104231965ef3SDarrick J. Wong  out_remove_errortag:
104331965ef3SDarrick J. Wong 	xfs_errortag_del(mp);
1044192852beSCarlos Maiolino  out_remove_error_sysfs:
1045192852beSCarlos Maiolino 	xfs_error_sysfs_del(mp);
1046225e4635SBill O'Donnell  out_del_stats:
1047225e4635SBill O'Donnell 	xfs_sysfs_del(&mp->m_stats.xs_kobj);
1048a31b1d3dSBrian Foster  out_remove_sysfs:
1049a31b1d3dSBrian Foster 	xfs_sysfs_del(&mp->m_kobj);
1050f9057e3dSChristoph Hellwig  out:
10511da177e4SLinus Torvalds 	return error;
10521da177e4SLinus Torvalds }
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds /*
10551da177e4SLinus Torvalds  * This flushes out the inodes,dquots and the superblock, unmounts the
10561da177e4SLinus Torvalds  * log and makes sure that incore structures are freed.
10571da177e4SLinus Torvalds  */
105841b5c2e7SChristoph Hellwig void
105941b5c2e7SChristoph Hellwig xfs_unmountfs(
106041b5c2e7SChristoph Hellwig 	struct xfs_mount	*mp)
10611da177e4SLinus Torvalds {
1062c8ce540dSDarrick J. Wong 	uint64_t		resblks;
106341b5c2e7SChristoph Hellwig 	int			error;
10641da177e4SLinus Torvalds 
1065579b62faSBrian Foster 	cancel_delayed_work_sync(&mp->m_eofblocks_work);
106683104d44SDarrick J. Wong 	cancel_delayed_work_sync(&mp->m_cowblocks_work);
1067579b62faSBrian Foster 
106884d69619SDarrick J. Wong 	xfs_fs_unreserve_ag_blocks(mp);
10697d095257SChristoph Hellwig 	xfs_qm_unmount_quotas(mp);
1070b93b6e43SChristoph Hellwig 	xfs_rtunmount_inodes(mp);
107177508ec8SChristoph Hellwig 	IRELE(mp->m_rootip);
107277508ec8SChristoph Hellwig 
1073641c56fbSDavid Chinner 	/*
1074641c56fbSDavid Chinner 	 * We can potentially deadlock here if we have an inode cluster
10759da096fdSMalcolm Parsons 	 * that has been freed has its buffer still pinned in memory because
1076641c56fbSDavid Chinner 	 * the transaction is still sitting in a iclog. The stale inodes
1077641c56fbSDavid Chinner 	 * on that buffer will have their flush locks held until the
1078641c56fbSDavid Chinner 	 * transaction hits the disk and the callbacks run. the inode
1079641c56fbSDavid Chinner 	 * flush takes the flush lock unconditionally and with nothing to
1080641c56fbSDavid Chinner 	 * push out the iclog we will never get that unlocked. hence we
1081641c56fbSDavid Chinner 	 * need to force the log first.
1082641c56fbSDavid Chinner 	 */
1083a14a348bSChristoph Hellwig 	xfs_log_force(mp, XFS_LOG_SYNC);
1084c854363eSDave Chinner 
1085c854363eSDave Chinner 	/*
1086ebf55872SChristoph Hellwig 	 * Wait for all busy extents to be freed, including completion of
1087ebf55872SChristoph Hellwig 	 * any discard operation.
1088ebf55872SChristoph Hellwig 	 */
1089ebf55872SChristoph Hellwig 	xfs_extent_busy_wait_all(mp);
10904560e78fSChristoph Hellwig 	flush_workqueue(xfs_discard_wq);
1091ebf55872SChristoph Hellwig 
1092ebf55872SChristoph Hellwig 	/*
1093e6b3bb78SCarlos Maiolino 	 * We now need to tell the world we are unmounting. This will allow
1094e6b3bb78SCarlos Maiolino 	 * us to detect that the filesystem is going away and we should error
1095e6b3bb78SCarlos Maiolino 	 * out anything that we have been retrying in the background. This will
1096e6b3bb78SCarlos Maiolino 	 * prevent neverending retries in AIL pushing from hanging the unmount.
1097e6b3bb78SCarlos Maiolino 	 */
1098e6b3bb78SCarlos Maiolino 	mp->m_flags |= XFS_MOUNT_UNMOUNTING;
1099e6b3bb78SCarlos Maiolino 
1100e6b3bb78SCarlos Maiolino 	/*
1101211e4d43SChristoph Hellwig 	 * Flush all pending changes from the AIL.
1102c854363eSDave Chinner 	 */
1103211e4d43SChristoph Hellwig 	xfs_ail_push_all_sync(mp->m_ail);
1104211e4d43SChristoph Hellwig 
1105211e4d43SChristoph Hellwig 	/*
1106211e4d43SChristoph Hellwig 	 * And reclaim all inodes.  At this point there should be no dirty
11077e18530bSDave Chinner 	 * inodes and none should be pinned or locked, but use synchronous
11087e18530bSDave Chinner 	 * reclaim just to be sure. We can stop background inode reclaim
11097e18530bSDave Chinner 	 * here as well if it is still running.
1110211e4d43SChristoph Hellwig 	 */
11117e18530bSDave Chinner 	cancel_delayed_work_sync(&mp->m_reclaim_work);
1112c854363eSDave Chinner 	xfs_reclaim_inodes(mp, SYNC_WAIT);
11131da177e4SLinus Torvalds 
11147d095257SChristoph Hellwig 	xfs_qm_unmount(mp);
1115a357a121SLachlan McIlroy 
11161da177e4SLinus Torvalds 	/*
111784e1e99fSDavid Chinner 	 * Unreserve any blocks we have so that when we unmount we don't account
111884e1e99fSDavid Chinner 	 * the reserved free space as used. This is really only necessary for
111984e1e99fSDavid Chinner 	 * lazy superblock counting because it trusts the incore superblock
11209da096fdSMalcolm Parsons 	 * counters to be absolutely correct on clean unmount.
112184e1e99fSDavid Chinner 	 *
112284e1e99fSDavid Chinner 	 * We don't bother correcting this elsewhere for lazy superblock
112384e1e99fSDavid Chinner 	 * counting because on mount of an unclean filesystem we reconstruct the
112484e1e99fSDavid Chinner 	 * correct counter value and this is irrelevant.
112584e1e99fSDavid Chinner 	 *
112684e1e99fSDavid Chinner 	 * For non-lazy counter filesystems, this doesn't matter at all because
112784e1e99fSDavid Chinner 	 * we only every apply deltas to the superblock and hence the incore
112884e1e99fSDavid Chinner 	 * value does not matter....
112984e1e99fSDavid Chinner 	 */
113084e1e99fSDavid Chinner 	resblks = 0;
1131714082bcSDavid Chinner 	error = xfs_reserve_blocks(mp, &resblks, NULL);
1132714082bcSDavid Chinner 	if (error)
11330b932cccSDave Chinner 		xfs_warn(mp, "Unable to free reserved block pool. "
1134714082bcSDavid Chinner 				"Freespace may not be correct on next mount.");
1135714082bcSDavid Chinner 
1136adab0f67SChandra Seetharaman 	error = xfs_log_sbcount(mp);
1137e5720eecSDavid Chinner 	if (error)
11380b932cccSDave Chinner 		xfs_warn(mp, "Unable to update superblock counters. "
1139e5720eecSDavid Chinner 				"Freespace may not be correct on next mount.");
114087c7bec7SChristoph Hellwig 
1141225e4635SBill O'Donnell 
114221b699c8SChristoph Hellwig 	xfs_log_unmount(mp);
11430650b554SDave Chinner 	xfs_da_unmount(mp);
114427174203SChristoph Hellwig 	xfs_uuid_unmount(mp);
11451da177e4SLinus Torvalds 
11461550d0b0SChristoph Hellwig #if defined(DEBUG)
114731965ef3SDarrick J. Wong 	xfs_errortag_clearall(mp);
11481da177e4SLinus Torvalds #endif
1149ff4f038cSChristoph Hellwig 	xfs_free_perag(mp);
1150a31b1d3dSBrian Foster 
115131965ef3SDarrick J. Wong 	xfs_errortag_del(mp);
1152192852beSCarlos Maiolino 	xfs_error_sysfs_del(mp);
1153225e4635SBill O'Donnell 	xfs_sysfs_del(&mp->m_stats.xs_kobj);
1154a31b1d3dSBrian Foster 	xfs_sysfs_del(&mp->m_kobj);
11551da177e4SLinus Torvalds }
11561da177e4SLinus Torvalds 
115791ee575fSBrian Foster /*
115891ee575fSBrian Foster  * Determine whether modifications can proceed. The caller specifies the minimum
115991ee575fSBrian Foster  * freeze level for which modifications should not be allowed. This allows
116091ee575fSBrian Foster  * certain operations to proceed while the freeze sequence is in progress, if
116191ee575fSBrian Foster  * necessary.
116291ee575fSBrian Foster  */
116391ee575fSBrian Foster bool
116491ee575fSBrian Foster xfs_fs_writable(
116591ee575fSBrian Foster 	struct xfs_mount	*mp,
116691ee575fSBrian Foster 	int			level)
116792821e2bSDavid Chinner {
116891ee575fSBrian Foster 	ASSERT(level > SB_UNFROZEN);
116991ee575fSBrian Foster 	if ((mp->m_super->s_writers.frozen >= level) ||
117091ee575fSBrian Foster 	    XFS_FORCED_SHUTDOWN(mp) || (mp->m_flags & XFS_MOUNT_RDONLY))
117191ee575fSBrian Foster 		return false;
117291ee575fSBrian Foster 
117391ee575fSBrian Foster 	return true;
117492821e2bSDavid Chinner }
117592821e2bSDavid Chinner 
117692821e2bSDavid Chinner /*
1177b2ce3974SAlex Elder  * xfs_log_sbcount
1178b2ce3974SAlex Elder  *
1179adab0f67SChandra Seetharaman  * Sync the superblock counters to disk.
1180b2ce3974SAlex Elder  *
118191ee575fSBrian Foster  * Note this code can be called during the process of freezing, so we use the
118291ee575fSBrian Foster  * transaction allocator that does not block when the transaction subsystem is
118391ee575fSBrian Foster  * in its frozen state.
118492821e2bSDavid Chinner  */
118592821e2bSDavid Chinner int
1186adab0f67SChandra Seetharaman xfs_log_sbcount(xfs_mount_t *mp)
118792821e2bSDavid Chinner {
118891ee575fSBrian Foster 	/* allow this to proceed during the freeze sequence... */
118991ee575fSBrian Foster 	if (!xfs_fs_writable(mp, SB_FREEZE_COMPLETE))
119092821e2bSDavid Chinner 		return 0;
119192821e2bSDavid Chinner 
119292821e2bSDavid Chinner 	/*
119392821e2bSDavid Chinner 	 * we don't need to do this if we are updating the superblock
119492821e2bSDavid Chinner 	 * counters on every modification.
119592821e2bSDavid Chinner 	 */
119692821e2bSDavid Chinner 	if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
119792821e2bSDavid Chinner 		return 0;
119892821e2bSDavid Chinner 
119961e63ecbSDave Chinner 	return xfs_sync_sb(mp, true);
120092821e2bSDavid Chinner }
120192821e2bSDavid Chinner 
12028c1903d3SDave Chinner /*
12038c1903d3SDave Chinner  * Deltas for the inode count are +/-64, hence we use a large batch size
12048c1903d3SDave Chinner  * of 128 so we don't need to take the counter lock on every update.
12058c1903d3SDave Chinner  */
12068c1903d3SDave Chinner #define XFS_ICOUNT_BATCH	128
1207501ab323SDave Chinner int
1208501ab323SDave Chinner xfs_mod_icount(
1209501ab323SDave Chinner 	struct xfs_mount	*mp,
1210501ab323SDave Chinner 	int64_t			delta)
1211501ab323SDave Chinner {
1212104b4e51SNikolay Borisov 	percpu_counter_add_batch(&mp->m_icount, delta, XFS_ICOUNT_BATCH);
12138c1903d3SDave Chinner 	if (__percpu_counter_compare(&mp->m_icount, 0, XFS_ICOUNT_BATCH) < 0) {
1214501ab323SDave Chinner 		ASSERT(0);
1215501ab323SDave Chinner 		percpu_counter_add(&mp->m_icount, -delta);
1216501ab323SDave Chinner 		return -EINVAL;
1217501ab323SDave Chinner 	}
1218501ab323SDave Chinner 	return 0;
1219501ab323SDave Chinner }
1220501ab323SDave Chinner 
1221e88b64eaSDave Chinner int
1222e88b64eaSDave Chinner xfs_mod_ifree(
1223e88b64eaSDave Chinner 	struct xfs_mount	*mp,
1224e88b64eaSDave Chinner 	int64_t			delta)
1225e88b64eaSDave Chinner {
1226e88b64eaSDave Chinner 	percpu_counter_add(&mp->m_ifree, delta);
1227e88b64eaSDave Chinner 	if (percpu_counter_compare(&mp->m_ifree, 0) < 0) {
1228e88b64eaSDave Chinner 		ASSERT(0);
1229e88b64eaSDave Chinner 		percpu_counter_add(&mp->m_ifree, -delta);
1230e88b64eaSDave Chinner 		return -EINVAL;
1231e88b64eaSDave Chinner 	}
1232e88b64eaSDave Chinner 	return 0;
1233e88b64eaSDave Chinner }
12340d485adaSDave Chinner 
12358c1903d3SDave Chinner /*
12368c1903d3SDave Chinner  * Deltas for the block count can vary from 1 to very large, but lock contention
12378c1903d3SDave Chinner  * only occurs on frequent small block count updates such as in the delayed
12388c1903d3SDave Chinner  * allocation path for buffered writes (page a time updates). Hence we set
12398c1903d3SDave Chinner  * a large batch count (1024) to minimise global counter updates except when
12408c1903d3SDave Chinner  * we get near to ENOSPC and we have to be very accurate with our updates.
12418c1903d3SDave Chinner  */
12428c1903d3SDave Chinner #define XFS_FDBLOCKS_BATCH	1024
12430d485adaSDave Chinner int
12440d485adaSDave Chinner xfs_mod_fdblocks(
12450d485adaSDave Chinner 	struct xfs_mount	*mp,
12460d485adaSDave Chinner 	int64_t			delta,
12470d485adaSDave Chinner 	bool			rsvd)
12480d485adaSDave Chinner {
12490d485adaSDave Chinner 	int64_t			lcounter;
12500d485adaSDave Chinner 	long long		res_used;
12510d485adaSDave Chinner 	s32			batch;
12520d485adaSDave Chinner 
12530d485adaSDave Chinner 	if (delta > 0) {
12540d485adaSDave Chinner 		/*
12550d485adaSDave Chinner 		 * If the reserve pool is depleted, put blocks back into it
12560d485adaSDave Chinner 		 * first. Most of the time the pool is full.
12570d485adaSDave Chinner 		 */
12580d485adaSDave Chinner 		if (likely(mp->m_resblks == mp->m_resblks_avail)) {
12590d485adaSDave Chinner 			percpu_counter_add(&mp->m_fdblocks, delta);
12600d485adaSDave Chinner 			return 0;
12610d485adaSDave Chinner 		}
12620d485adaSDave Chinner 
12630d485adaSDave Chinner 		spin_lock(&mp->m_sb_lock);
12640d485adaSDave Chinner 		res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
12650d485adaSDave Chinner 
12660d485adaSDave Chinner 		if (res_used > delta) {
12670d485adaSDave Chinner 			mp->m_resblks_avail += delta;
12680d485adaSDave Chinner 		} else {
12690d485adaSDave Chinner 			delta -= res_used;
12700d485adaSDave Chinner 			mp->m_resblks_avail = mp->m_resblks;
12710d485adaSDave Chinner 			percpu_counter_add(&mp->m_fdblocks, delta);
12720d485adaSDave Chinner 		}
12730d485adaSDave Chinner 		spin_unlock(&mp->m_sb_lock);
12740d485adaSDave Chinner 		return 0;
12750d485adaSDave Chinner 	}
12760d485adaSDave Chinner 
12770d485adaSDave Chinner 	/*
12780d485adaSDave Chinner 	 * Taking blocks away, need to be more accurate the closer we
12790d485adaSDave Chinner 	 * are to zero.
12800d485adaSDave Chinner 	 *
12810d485adaSDave Chinner 	 * If the counter has a value of less than 2 * max batch size,
12820d485adaSDave Chinner 	 * then make everything serialise as we are real close to
12830d485adaSDave Chinner 	 * ENOSPC.
12840d485adaSDave Chinner 	 */
12858c1903d3SDave Chinner 	if (__percpu_counter_compare(&mp->m_fdblocks, 2 * XFS_FDBLOCKS_BATCH,
12868c1903d3SDave Chinner 				     XFS_FDBLOCKS_BATCH) < 0)
12870d485adaSDave Chinner 		batch = 1;
12880d485adaSDave Chinner 	else
12898c1903d3SDave Chinner 		batch = XFS_FDBLOCKS_BATCH;
12900d485adaSDave Chinner 
1291104b4e51SNikolay Borisov 	percpu_counter_add_batch(&mp->m_fdblocks, delta, batch);
129252548852SDarrick J. Wong 	if (__percpu_counter_compare(&mp->m_fdblocks, mp->m_alloc_set_aside,
12938c1903d3SDave Chinner 				     XFS_FDBLOCKS_BATCH) >= 0) {
12940d485adaSDave Chinner 		/* we had space! */
12950d485adaSDave Chinner 		return 0;
12960d485adaSDave Chinner 	}
12970d485adaSDave Chinner 
12980d485adaSDave Chinner 	/*
12990d485adaSDave Chinner 	 * lock up the sb for dipping into reserves before releasing the space
13000d485adaSDave Chinner 	 * that took us to ENOSPC.
13010d485adaSDave Chinner 	 */
13020d485adaSDave Chinner 	spin_lock(&mp->m_sb_lock);
13030d485adaSDave Chinner 	percpu_counter_add(&mp->m_fdblocks, -delta);
13040d485adaSDave Chinner 	if (!rsvd)
13050d485adaSDave Chinner 		goto fdblocks_enospc;
13060d485adaSDave Chinner 
13070d485adaSDave Chinner 	lcounter = (long long)mp->m_resblks_avail + delta;
13080d485adaSDave Chinner 	if (lcounter >= 0) {
13090d485adaSDave Chinner 		mp->m_resblks_avail = lcounter;
13100d485adaSDave Chinner 		spin_unlock(&mp->m_sb_lock);
13110d485adaSDave Chinner 		return 0;
13120d485adaSDave Chinner 	}
13130d485adaSDave Chinner 	printk_once(KERN_WARNING
13140d485adaSDave Chinner 		"Filesystem \"%s\": reserve blocks depleted! "
13150d485adaSDave Chinner 		"Consider increasing reserve pool size.",
13160d485adaSDave Chinner 		mp->m_fsname);
13170d485adaSDave Chinner fdblocks_enospc:
13180d485adaSDave Chinner 	spin_unlock(&mp->m_sb_lock);
13190d485adaSDave Chinner 	return -ENOSPC;
13200d485adaSDave Chinner }
13210d485adaSDave Chinner 
1322bab98bbeSDave Chinner int
1323bab98bbeSDave Chinner xfs_mod_frextents(
1324bab98bbeSDave Chinner 	struct xfs_mount	*mp,
1325bab98bbeSDave Chinner 	int64_t			delta)
1326bab98bbeSDave Chinner {
1327bab98bbeSDave Chinner 	int64_t			lcounter;
1328bab98bbeSDave Chinner 	int			ret = 0;
1329bab98bbeSDave Chinner 
1330bab98bbeSDave Chinner 	spin_lock(&mp->m_sb_lock);
1331bab98bbeSDave Chinner 	lcounter = mp->m_sb.sb_frextents + delta;
1332bab98bbeSDave Chinner 	if (lcounter < 0)
1333bab98bbeSDave Chinner 		ret = -ENOSPC;
1334bab98bbeSDave Chinner 	else
1335bab98bbeSDave Chinner 		mp->m_sb.sb_frextents = lcounter;
1336bab98bbeSDave Chinner 	spin_unlock(&mp->m_sb_lock);
1337bab98bbeSDave Chinner 	return ret;
1338bab98bbeSDave Chinner }
1339bab98bbeSDave Chinner 
13401da177e4SLinus Torvalds /*
13411da177e4SLinus Torvalds  * xfs_getsb() is called to obtain the buffer for the superblock.
13421da177e4SLinus Torvalds  * The buffer is returned locked and read in from disk.
13431da177e4SLinus Torvalds  * The buffer should be released with a call to xfs_brelse().
13441da177e4SLinus Torvalds  *
13451da177e4SLinus Torvalds  * If the flags parameter is BUF_TRYLOCK, then we'll only return
13461da177e4SLinus Torvalds  * the superblock buffer if it can be locked without sleeping.
13471da177e4SLinus Torvalds  * If it can't then we'll return NULL.
13481da177e4SLinus Torvalds  */
13490c842ad4SChristoph Hellwig struct xfs_buf *
13501da177e4SLinus Torvalds xfs_getsb(
13510c842ad4SChristoph Hellwig 	struct xfs_mount	*mp,
13521da177e4SLinus Torvalds 	int			flags)
13531da177e4SLinus Torvalds {
13540c842ad4SChristoph Hellwig 	struct xfs_buf		*bp = mp->m_sb_bp;
13551da177e4SLinus Torvalds 
13560c842ad4SChristoph Hellwig 	if (!xfs_buf_trylock(bp)) {
13570c842ad4SChristoph Hellwig 		if (flags & XBF_TRYLOCK)
13581da177e4SLinus Torvalds 			return NULL;
13590c842ad4SChristoph Hellwig 		xfs_buf_lock(bp);
13601da177e4SLinus Torvalds 	}
13610c842ad4SChristoph Hellwig 
136272790aa1SChandra Seetharaman 	xfs_buf_hold(bp);
1363b0388bf1SDave Chinner 	ASSERT(bp->b_flags & XBF_DONE);
1364014c2544SJesper Juhl 	return bp;
13651da177e4SLinus Torvalds }
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds /*
13681da177e4SLinus Torvalds  * Used to free the superblock along various error paths.
13691da177e4SLinus Torvalds  */
13701da177e4SLinus Torvalds void
13711da177e4SLinus Torvalds xfs_freesb(
137226af6552SDave Chinner 	struct xfs_mount	*mp)
13731da177e4SLinus Torvalds {
137426af6552SDave Chinner 	struct xfs_buf		*bp = mp->m_sb_bp;
13751da177e4SLinus Torvalds 
137626af6552SDave Chinner 	xfs_buf_lock(bp);
13771da177e4SLinus Torvalds 	mp->m_sb_bp = NULL;
137826af6552SDave Chinner 	xfs_buf_relse(bp);
13791da177e4SLinus Torvalds }
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds /*
1382dda35b8fSChristoph Hellwig  * If the underlying (data/log/rt) device is readonly, there are some
1383dda35b8fSChristoph Hellwig  * operations that cannot proceed.
1384dda35b8fSChristoph Hellwig  */
1385dda35b8fSChristoph Hellwig int
1386dda35b8fSChristoph Hellwig xfs_dev_is_read_only(
1387dda35b8fSChristoph Hellwig 	struct xfs_mount	*mp,
1388dda35b8fSChristoph Hellwig 	char			*message)
1389dda35b8fSChristoph Hellwig {
1390dda35b8fSChristoph Hellwig 	if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
1391dda35b8fSChristoph Hellwig 	    xfs_readonly_buftarg(mp->m_logdev_targp) ||
1392dda35b8fSChristoph Hellwig 	    (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
13930b932cccSDave Chinner 		xfs_notice(mp, "%s required on read-only device.", message);
13940b932cccSDave Chinner 		xfs_notice(mp, "write access unavailable, cannot proceed.");
13952451337dSDave Chinner 		return -EROFS;
1396dda35b8fSChristoph Hellwig 	}
1397dda35b8fSChristoph Hellwig 	return 0;
1398dda35b8fSChristoph Hellwig }
1399