xref: /linux/fs/xfs/xfs_mount.c (revision 9de67c3ba9ea961ba420573d56479d09d33a7587)
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"
25a844f451SNathan Scott #include "xfs_inum.h"
261da177e4SLinus Torvalds #include "xfs_sb.h"
271da177e4SLinus Torvalds #include "xfs_ag.h"
281da177e4SLinus Torvalds #include "xfs_mount.h"
2957062787SDave Chinner #include "xfs_da_format.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"
448f80587bSDave Chinner #include "xfs_dinode.h"
450b1b213fSChristoph Hellwig 
461da177e4SLinus Torvalds 
478d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
4820f4ebf2SDavid Chinner STATIC void	xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t,
4945af6c6dSChristoph Hellwig 						int);
5045af6c6dSChristoph Hellwig STATIC void	xfs_icsb_balance_counter_locked(xfs_mount_t *, xfs_sb_field_t,
5145af6c6dSChristoph Hellwig 						int);
5236fbe6e6SDavid Chinner STATIC void	xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
538d280b98SDavid Chinner #else
548d280b98SDavid Chinner 
5545af6c6dSChristoph Hellwig #define xfs_icsb_balance_counter(mp, a, b)		do { } while (0)
5645af6c6dSChristoph Hellwig #define xfs_icsb_balance_counter_locked(mp, a, b)	do { } while (0)
578d280b98SDavid Chinner #endif
588d280b98SDavid Chinner 
5927174203SChristoph Hellwig static DEFINE_MUTEX(xfs_uuid_table_mutex);
6027174203SChristoph Hellwig static int xfs_uuid_table_size;
6127174203SChristoph Hellwig static uuid_t *xfs_uuid_table;
6227174203SChristoph Hellwig 
6327174203SChristoph Hellwig /*
6427174203SChristoph Hellwig  * See if the UUID is unique among mounted XFS filesystems.
6527174203SChristoph Hellwig  * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
6627174203SChristoph Hellwig  */
6727174203SChristoph Hellwig STATIC int
6827174203SChristoph Hellwig xfs_uuid_mount(
6927174203SChristoph Hellwig 	struct xfs_mount	*mp)
7027174203SChristoph Hellwig {
7127174203SChristoph Hellwig 	uuid_t			*uuid = &mp->m_sb.sb_uuid;
7227174203SChristoph Hellwig 	int			hole, i;
7327174203SChristoph Hellwig 
7427174203SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_NOUUID)
7527174203SChristoph Hellwig 		return 0;
7627174203SChristoph Hellwig 
7727174203SChristoph Hellwig 	if (uuid_is_nil(uuid)) {
780b932cccSDave Chinner 		xfs_warn(mp, "Filesystem has nil UUID - can't mount");
792451337dSDave Chinner 		return -EINVAL;
8027174203SChristoph Hellwig 	}
8127174203SChristoph Hellwig 
8227174203SChristoph Hellwig 	mutex_lock(&xfs_uuid_table_mutex);
8327174203SChristoph Hellwig 	for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) {
8427174203SChristoph Hellwig 		if (uuid_is_nil(&xfs_uuid_table[i])) {
8527174203SChristoph Hellwig 			hole = i;
8627174203SChristoph Hellwig 			continue;
8727174203SChristoph Hellwig 		}
8827174203SChristoph Hellwig 		if (uuid_equal(uuid, &xfs_uuid_table[i]))
8927174203SChristoph Hellwig 			goto out_duplicate;
9027174203SChristoph Hellwig 	}
9127174203SChristoph Hellwig 
9227174203SChristoph Hellwig 	if (hole < 0) {
9327174203SChristoph Hellwig 		xfs_uuid_table = kmem_realloc(xfs_uuid_table,
9427174203SChristoph Hellwig 			(xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table),
9527174203SChristoph Hellwig 			xfs_uuid_table_size  * sizeof(*xfs_uuid_table),
9627174203SChristoph Hellwig 			KM_SLEEP);
9727174203SChristoph Hellwig 		hole = xfs_uuid_table_size++;
9827174203SChristoph Hellwig 	}
9927174203SChristoph Hellwig 	xfs_uuid_table[hole] = *uuid;
10027174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
10127174203SChristoph Hellwig 
10227174203SChristoph Hellwig 	return 0;
10327174203SChristoph Hellwig 
10427174203SChristoph Hellwig  out_duplicate:
10527174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
106021000e5SMitsuo Hayasaka 	xfs_warn(mp, "Filesystem has duplicate UUID %pU - can't mount", uuid);
1072451337dSDave Chinner 	return -EINVAL;
10827174203SChristoph Hellwig }
10927174203SChristoph Hellwig 
11027174203SChristoph Hellwig STATIC void
11127174203SChristoph Hellwig xfs_uuid_unmount(
11227174203SChristoph Hellwig 	struct xfs_mount	*mp)
11327174203SChristoph Hellwig {
11427174203SChristoph Hellwig 	uuid_t			*uuid = &mp->m_sb.sb_uuid;
11527174203SChristoph Hellwig 	int			i;
11627174203SChristoph Hellwig 
11727174203SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_NOUUID)
11827174203SChristoph Hellwig 		return;
11927174203SChristoph Hellwig 
12027174203SChristoph Hellwig 	mutex_lock(&xfs_uuid_table_mutex);
12127174203SChristoph Hellwig 	for (i = 0; i < xfs_uuid_table_size; i++) {
12227174203SChristoph Hellwig 		if (uuid_is_nil(&xfs_uuid_table[i]))
12327174203SChristoph Hellwig 			continue;
12427174203SChristoph Hellwig 		if (!uuid_equal(uuid, &xfs_uuid_table[i]))
12527174203SChristoph Hellwig 			continue;
12627174203SChristoph Hellwig 		memset(&xfs_uuid_table[i], 0, sizeof(uuid_t));
12727174203SChristoph Hellwig 		break;
12827174203SChristoph Hellwig 	}
12927174203SChristoph Hellwig 	ASSERT(i < xfs_uuid_table_size);
13027174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
13127174203SChristoph Hellwig }
13227174203SChristoph Hellwig 
13327174203SChristoph Hellwig 
134e176579eSDave Chinner STATIC void
135e176579eSDave Chinner __xfs_free_perag(
136e176579eSDave Chinner 	struct rcu_head	*head)
137e176579eSDave Chinner {
138e176579eSDave Chinner 	struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head);
139e176579eSDave Chinner 
140e176579eSDave Chinner 	ASSERT(atomic_read(&pag->pag_ref) == 0);
141e176579eSDave Chinner 	kmem_free(pag);
142e176579eSDave Chinner }
143e176579eSDave Chinner 
1440fa800fbSDave Chinner /*
145e176579eSDave Chinner  * Free up the per-ag resources associated with the mount structure.
1461da177e4SLinus Torvalds  */
147c962fb79SChristoph Hellwig STATIC void
148ff4f038cSChristoph Hellwig xfs_free_perag(
149745f6919SChristoph Hellwig 	xfs_mount_t	*mp)
1501da177e4SLinus Torvalds {
1511c1c6ebcSDave Chinner 	xfs_agnumber_t	agno;
1521c1c6ebcSDave Chinner 	struct xfs_perag *pag;
1531da177e4SLinus Torvalds 
1541c1c6ebcSDave Chinner 	for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
1551c1c6ebcSDave Chinner 		spin_lock(&mp->m_perag_lock);
1561c1c6ebcSDave Chinner 		pag = radix_tree_delete(&mp->m_perag_tree, agno);
1571c1c6ebcSDave Chinner 		spin_unlock(&mp->m_perag_lock);
158e176579eSDave Chinner 		ASSERT(pag);
159f83282a8SDave Chinner 		ASSERT(atomic_read(&pag->pag_ref) == 0);
160e176579eSDave Chinner 		call_rcu(&pag->rcu_head, __xfs_free_perag);
1611da177e4SLinus Torvalds 	}
1621da177e4SLinus Torvalds }
1631da177e4SLinus Torvalds 
1644cc929eeSNathan Scott /*
1654cc929eeSNathan Scott  * Check size of device based on the (data/realtime) block count.
1664cc929eeSNathan Scott  * Note: this check is used by the growfs code as well as mount.
1674cc929eeSNathan Scott  */
1684cc929eeSNathan Scott int
1694cc929eeSNathan Scott xfs_sb_validate_fsb_count(
1704cc929eeSNathan Scott 	xfs_sb_t	*sbp,
1714cc929eeSNathan Scott 	__uint64_t	nblocks)
1724cc929eeSNathan Scott {
1734cc929eeSNathan Scott 	ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
1744cc929eeSNathan Scott 	ASSERT(sbp->sb_blocklog >= BBSHIFT);
1754cc929eeSNathan Scott 
1764cc929eeSNathan Scott #if XFS_BIG_BLKNOS     /* Limited by ULONG_MAX of page cache index */
1774cc929eeSNathan Scott 	if (nblocks >> (PAGE_CACHE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
1782451337dSDave Chinner 		return -EFBIG;
1794cc929eeSNathan Scott #else                  /* Limited by UINT_MAX of sectors */
1804cc929eeSNathan Scott 	if (nblocks << (sbp->sb_blocklog - BBSHIFT) > UINT_MAX)
1812451337dSDave Chinner 		return -EFBIG;
1824cc929eeSNathan Scott #endif
1834cc929eeSNathan Scott 	return 0;
1844cc929eeSNathan Scott }
1851da177e4SLinus Torvalds 
1861c1c6ebcSDave Chinner int
187c11e2c36SNathan Scott xfs_initialize_perag(
188c11e2c36SNathan Scott 	xfs_mount_t	*mp,
1891c1c6ebcSDave Chinner 	xfs_agnumber_t	agcount,
1901c1c6ebcSDave Chinner 	xfs_agnumber_t	*maxagi)
1911da177e4SLinus Torvalds {
1922d2194f6SCarlos Maiolino 	xfs_agnumber_t	index;
1938b26c582SDave Chinner 	xfs_agnumber_t	first_initialised = 0;
1941da177e4SLinus Torvalds 	xfs_perag_t	*pag;
1951da177e4SLinus Torvalds 	xfs_agino_t	agino;
1961da177e4SLinus Torvalds 	xfs_ino_t	ino;
1971da177e4SLinus Torvalds 	xfs_sb_t	*sbp = &mp->m_sb;
1988b26c582SDave Chinner 	int		error = -ENOMEM;
1991da177e4SLinus Torvalds 
2001c1c6ebcSDave Chinner 	/*
2011c1c6ebcSDave Chinner 	 * Walk the current per-ag tree so we don't try to initialise AGs
2021c1c6ebcSDave Chinner 	 * that already exist (growfs case). Allocate and insert all the
2031c1c6ebcSDave Chinner 	 * AGs we don't find ready for initialisation.
2041c1c6ebcSDave Chinner 	 */
2051c1c6ebcSDave Chinner 	for (index = 0; index < agcount; index++) {
2061c1c6ebcSDave Chinner 		pag = xfs_perag_get(mp, index);
2071c1c6ebcSDave Chinner 		if (pag) {
2081c1c6ebcSDave Chinner 			xfs_perag_put(pag);
2091c1c6ebcSDave Chinner 			continue;
2101c1c6ebcSDave Chinner 		}
2118b26c582SDave Chinner 		if (!first_initialised)
2128b26c582SDave Chinner 			first_initialised = index;
213fb3b504aSChristoph Hellwig 
2141c1c6ebcSDave Chinner 		pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
2151c1c6ebcSDave Chinner 		if (!pag)
2168b26c582SDave Chinner 			goto out_unwind;
217fb3b504aSChristoph Hellwig 		pag->pag_agno = index;
218fb3b504aSChristoph Hellwig 		pag->pag_mount = mp;
2191a427ab0SDave Chinner 		spin_lock_init(&pag->pag_ici_lock);
22069b491c2SDave Chinner 		mutex_init(&pag->pag_ici_reclaim_lock);
221fb3b504aSChristoph Hellwig 		INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
22274f75a0cSDave Chinner 		spin_lock_init(&pag->pag_buf_lock);
22374f75a0cSDave Chinner 		pag->pag_buf_tree = RB_ROOT;
224fb3b504aSChristoph Hellwig 
2251c1c6ebcSDave Chinner 		if (radix_tree_preload(GFP_NOFS))
2268b26c582SDave Chinner 			goto out_unwind;
227fb3b504aSChristoph Hellwig 
2281c1c6ebcSDave Chinner 		spin_lock(&mp->m_perag_lock);
2291c1c6ebcSDave Chinner 		if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
2301c1c6ebcSDave Chinner 			BUG();
2311c1c6ebcSDave Chinner 			spin_unlock(&mp->m_perag_lock);
2328b26c582SDave Chinner 			radix_tree_preload_end();
2338b26c582SDave Chinner 			error = -EEXIST;
2348b26c582SDave Chinner 			goto out_unwind;
2351c1c6ebcSDave Chinner 		}
2361c1c6ebcSDave Chinner 		spin_unlock(&mp->m_perag_lock);
2371c1c6ebcSDave Chinner 		radix_tree_preload_end();
2381c1c6ebcSDave Chinner 	}
2391c1c6ebcSDave Chinner 
240fb3b504aSChristoph Hellwig 	/*
241fb3b504aSChristoph Hellwig 	 * If we mount with the inode64 option, or no inode overflows
242fb3b504aSChristoph Hellwig 	 * the legacy 32-bit address space clear the inode32 option.
2431da177e4SLinus Torvalds 	 */
244fb3b504aSChristoph Hellwig 	agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
245fb3b504aSChristoph Hellwig 	ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
2461da177e4SLinus Torvalds 
247fb3b504aSChristoph Hellwig 	if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > XFS_MAXINUMBER_32)
248fb3b504aSChristoph Hellwig 		mp->m_flags |= XFS_MOUNT_32BITINODES;
249fb3b504aSChristoph Hellwig 	else
250fb3b504aSChristoph Hellwig 		mp->m_flags &= ~XFS_MOUNT_32BITINODES;
251fb3b504aSChristoph Hellwig 
2522d2194f6SCarlos Maiolino 	if (mp->m_flags & XFS_MOUNT_32BITINODES)
253*9de67c3bSEric Sandeen 		index = xfs_set_inode32(mp, agcount);
2542d2194f6SCarlos Maiolino 	else
255*9de67c3bSEric Sandeen 		index = xfs_set_inode64(mp, agcount);
256fb3b504aSChristoph Hellwig 
2571c1c6ebcSDave Chinner 	if (maxagi)
2581c1c6ebcSDave Chinner 		*maxagi = index;
2591c1c6ebcSDave Chinner 	return 0;
2608b26c582SDave Chinner 
2618b26c582SDave Chinner out_unwind:
2628b26c582SDave Chinner 	kmem_free(pag);
2638b26c582SDave Chinner 	for (; index > first_initialised; index--) {
2648b26c582SDave Chinner 		pag = radix_tree_delete(&mp->m_perag_tree, index);
2658b26c582SDave Chinner 		kmem_free(pag);
2668b26c582SDave Chinner 	}
2678b26c582SDave Chinner 	return error;
2681da177e4SLinus Torvalds }
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds /*
2711da177e4SLinus Torvalds  * xfs_readsb
2721da177e4SLinus Torvalds  *
2731da177e4SLinus Torvalds  * Does the initial read of the superblock.
2741da177e4SLinus Torvalds  */
2751da177e4SLinus Torvalds int
276ff55068cSDave Chinner xfs_readsb(
277ff55068cSDave Chinner 	struct xfs_mount *mp,
278ff55068cSDave Chinner 	int		flags)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	unsigned int	sector_size;
28104a1e6c5SDave Chinner 	struct xfs_buf	*bp;
28204a1e6c5SDave Chinner 	struct xfs_sb	*sbp = &mp->m_sb;
2831da177e4SLinus Torvalds 	int		error;
284af34e09dSDave Chinner 	int		loud = !(flags & XFS_MFSI_QUIET);
285daba5427SEric Sandeen 	const struct xfs_buf_ops *buf_ops;
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	ASSERT(mp->m_sb_bp == NULL);
2881da177e4SLinus Torvalds 	ASSERT(mp->m_ddev_targp != NULL);
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	/*
291daba5427SEric Sandeen 	 * For the initial read, we must guess at the sector
292daba5427SEric Sandeen 	 * size based on the block device.  It's enough to
293daba5427SEric Sandeen 	 * get the sb_sectsize out of the superblock and
294daba5427SEric Sandeen 	 * then reread with the proper length.
295daba5427SEric Sandeen 	 * We don't verify it yet, because it may not be complete.
296daba5427SEric Sandeen 	 */
297daba5427SEric Sandeen 	sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
298daba5427SEric Sandeen 	buf_ops = NULL;
299daba5427SEric Sandeen 
300daba5427SEric Sandeen 	/*
3011da177e4SLinus Torvalds 	 * Allocate a (locked) buffer to hold the superblock.
3021da177e4SLinus Torvalds 	 * This will be kept around at all times to optimize
3031da177e4SLinus Torvalds 	 * access to the superblock.
3041da177e4SLinus Torvalds 	 */
30526af6552SDave Chinner reread:
306e70b73f8SDave Chinner 	bp = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR,
307daba5427SEric Sandeen 				   BTOBB(sector_size), 0, buf_ops);
30826af6552SDave Chinner 	if (!bp) {
309af34e09dSDave Chinner 		if (loud)
310af34e09dSDave Chinner 			xfs_warn(mp, "SB buffer read failed");
3112451337dSDave Chinner 		return -EIO;
3121da177e4SLinus Torvalds 	}
313eab4e633SDave Chinner 	if (bp->b_error) {
314eab4e633SDave Chinner 		error = bp->b_error;
315eab4e633SDave Chinner 		if (loud)
316e721f504SDave Chinner 			xfs_warn(mp, "SB validate failed with error %d.", error);
317ac75a1f7SDave Chinner 		/* bad CRC means corrupted metadata */
3182451337dSDave Chinner 		if (error == -EFSBADCRC)
3192451337dSDave Chinner 			error = -EFSCORRUPTED;
320eab4e633SDave Chinner 		goto release_buf;
321eab4e633SDave Chinner 	}
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	/*
3241da177e4SLinus Torvalds 	 * Initialize the mount structure from the superblock.
3251da177e4SLinus Torvalds 	 */
326556b8883SDave Chinner 	xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(bp));
327556b8883SDave Chinner 	xfs_sb_quota_from_disk(sbp);
328556b8883SDave Chinner 
329556b8883SDave Chinner 	/*
330556b8883SDave Chinner 	 * If we haven't validated the superblock, do so now before we try
331556b8883SDave Chinner 	 * to check the sector size and reread the superblock appropriately.
332556b8883SDave Chinner 	 */
333556b8883SDave Chinner 	if (sbp->sb_magicnum != XFS_SB_MAGIC) {
334556b8883SDave Chinner 		if (loud)
335556b8883SDave Chinner 			xfs_warn(mp, "Invalid superblock magic number");
3362451337dSDave Chinner 		error = -EINVAL;
337556b8883SDave Chinner 		goto release_buf;
338556b8883SDave Chinner 	}
339ff55068cSDave Chinner 
3401da177e4SLinus Torvalds 	/*
3411da177e4SLinus Torvalds 	 * We must be able to do sector-sized and sector-aligned IO.
3421da177e4SLinus Torvalds 	 */
34304a1e6c5SDave Chinner 	if (sector_size > sbp->sb_sectsize) {
344af34e09dSDave Chinner 		if (loud)
345af34e09dSDave Chinner 			xfs_warn(mp, "device supports %u byte sectors (not %u)",
34604a1e6c5SDave Chinner 				sector_size, sbp->sb_sectsize);
3472451337dSDave Chinner 		error = -ENOSYS;
34826af6552SDave Chinner 		goto release_buf;
3491da177e4SLinus Torvalds 	}
3501da177e4SLinus Torvalds 
351556b8883SDave Chinner 	if (buf_ops == NULL) {
3521da177e4SLinus Torvalds 		/*
353daba5427SEric Sandeen 		 * Re-read the superblock so the buffer is correctly sized,
354daba5427SEric Sandeen 		 * and properly verified.
3551da177e4SLinus Torvalds 		 */
3561da177e4SLinus Torvalds 		xfs_buf_relse(bp);
35704a1e6c5SDave Chinner 		sector_size = sbp->sb_sectsize;
358daba5427SEric Sandeen 		buf_ops = loud ? &xfs_sb_buf_ops : &xfs_sb_quiet_buf_ops;
35926af6552SDave Chinner 		goto reread;
3601da177e4SLinus Torvalds 	}
3611da177e4SLinus Torvalds 
3625478eeadSLachlan McIlroy 	/* Initialize per-cpu counters */
3635478eeadSLachlan McIlroy 	xfs_icsb_reinit_counters(mp);
3648d280b98SDavid Chinner 
36504a1e6c5SDave Chinner 	/* no need to be quiet anymore, so reset the buf ops */
36604a1e6c5SDave Chinner 	bp->b_ops = &xfs_sb_buf_ops;
36704a1e6c5SDave Chinner 
3681da177e4SLinus Torvalds 	mp->m_sb_bp = bp;
36926af6552SDave Chinner 	xfs_buf_unlock(bp);
3701da177e4SLinus Torvalds 	return 0;
3711da177e4SLinus Torvalds 
37226af6552SDave Chinner release_buf:
3731da177e4SLinus Torvalds 	xfs_buf_relse(bp);
3741da177e4SLinus Torvalds 	return error;
3751da177e4SLinus Torvalds }
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds /*
3780771fb45SEric Sandeen  * Update alignment values based on mount options and sb values
3791da177e4SLinus Torvalds  */
3800771fb45SEric Sandeen STATIC int
3817884bc86SChristoph Hellwig xfs_update_alignment(xfs_mount_t *mp)
3821da177e4SLinus Torvalds {
3831da177e4SLinus Torvalds 	xfs_sb_t	*sbp = &(mp->m_sb);
3841da177e4SLinus Torvalds 
3854249023aSChristoph Hellwig 	if (mp->m_dalign) {
3861da177e4SLinus Torvalds 		/*
3871da177e4SLinus Torvalds 		 * If stripe unit and stripe width are not multiples
3881da177e4SLinus Torvalds 		 * of the fs blocksize turn off alignment.
3891da177e4SLinus Torvalds 		 */
3901da177e4SLinus Torvalds 		if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
3911da177e4SLinus Torvalds 		    (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
39239a45d84SJie Liu 			xfs_warn(mp,
39339a45d84SJie Liu 		"alignment check failed: sunit/swidth vs. blocksize(%d)",
39439a45d84SJie Liu 				sbp->sb_blocksize);
3952451337dSDave Chinner 			return -EINVAL;
3961da177e4SLinus Torvalds 		} else {
3971da177e4SLinus Torvalds 			/*
3981da177e4SLinus Torvalds 			 * Convert the stripe unit and width to FSBs.
3991da177e4SLinus Torvalds 			 */
4001da177e4SLinus Torvalds 			mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
4011da177e4SLinus Torvalds 			if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
40253487786SDave Chinner 				xfs_warn(mp,
40339a45d84SJie Liu 			"alignment check failed: sunit/swidth vs. agsize(%d)",
4041da177e4SLinus Torvalds 					 sbp->sb_agblocks);
4052451337dSDave Chinner 				return -EINVAL;
4061da177e4SLinus Torvalds 			} else if (mp->m_dalign) {
4071da177e4SLinus Torvalds 				mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
4081da177e4SLinus Torvalds 			} else {
40939a45d84SJie Liu 				xfs_warn(mp,
41039a45d84SJie Liu 			"alignment check failed: sunit(%d) less than bsize(%d)",
41139a45d84SJie Liu 					 mp->m_dalign, sbp->sb_blocksize);
4122451337dSDave Chinner 				return -EINVAL;
4131da177e4SLinus Torvalds 			}
4141da177e4SLinus Torvalds 		}
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 		/*
4171da177e4SLinus Torvalds 		 * Update superblock with new values
4181da177e4SLinus Torvalds 		 * and log changes
4191da177e4SLinus Torvalds 		 */
42062118709SEric Sandeen 		if (xfs_sb_version_hasdalign(sbp)) {
4211da177e4SLinus Torvalds 			if (sbp->sb_unit != mp->m_dalign) {
4221da177e4SLinus Torvalds 				sbp->sb_unit = mp->m_dalign;
4237884bc86SChristoph Hellwig 				mp->m_update_flags |= XFS_SB_UNIT;
4241da177e4SLinus Torvalds 			}
4251da177e4SLinus Torvalds 			if (sbp->sb_width != mp->m_swidth) {
4261da177e4SLinus Torvalds 				sbp->sb_width = mp->m_swidth;
4277884bc86SChristoph Hellwig 				mp->m_update_flags |= XFS_SB_WIDTH;
4281da177e4SLinus Torvalds 			}
42934d7f603SJie Liu 		} else {
43034d7f603SJie Liu 			xfs_warn(mp,
43134d7f603SJie Liu 	"cannot change alignment: superblock does not support data alignment");
4322451337dSDave Chinner 			return -EINVAL;
4331da177e4SLinus Torvalds 		}
4341da177e4SLinus Torvalds 	} else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
43562118709SEric Sandeen 		    xfs_sb_version_hasdalign(&mp->m_sb)) {
4361da177e4SLinus Torvalds 			mp->m_dalign = sbp->sb_unit;
4371da177e4SLinus Torvalds 			mp->m_swidth = sbp->sb_width;
4381da177e4SLinus Torvalds 	}
4391da177e4SLinus Torvalds 
4400771fb45SEric Sandeen 	return 0;
4410771fb45SEric Sandeen }
4421da177e4SLinus Torvalds 
4430771fb45SEric Sandeen /*
4440771fb45SEric Sandeen  * Set the maximum inode count for this filesystem
4450771fb45SEric Sandeen  */
4460771fb45SEric Sandeen STATIC void
4470771fb45SEric Sandeen xfs_set_maxicount(xfs_mount_t *mp)
4480771fb45SEric Sandeen {
4490771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
4501da177e4SLinus Torvalds 	__uint64_t	icount;
4511da177e4SLinus Torvalds 
4520771fb45SEric Sandeen 	if (sbp->sb_imax_pct) {
4530771fb45SEric Sandeen 		/*
4540771fb45SEric Sandeen 		 * Make sure the maximum inode count is a multiple
4550771fb45SEric Sandeen 		 * of the units we allocate inodes in.
4561da177e4SLinus Torvalds 		 */
4571da177e4SLinus Torvalds 		icount = sbp->sb_dblocks * sbp->sb_imax_pct;
4581da177e4SLinus Torvalds 		do_div(icount, 100);
4591da177e4SLinus Torvalds 		do_div(icount, mp->m_ialloc_blks);
4601da177e4SLinus Torvalds 		mp->m_maxicount = (icount * mp->m_ialloc_blks)  <<
4611da177e4SLinus Torvalds 				   sbp->sb_inopblog;
4620771fb45SEric Sandeen 	} else {
4631da177e4SLinus Torvalds 		mp->m_maxicount = 0;
4641da177e4SLinus Torvalds 	}
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds /*
4681da177e4SLinus Torvalds  * Set the default minimum read and write sizes unless
4691da177e4SLinus Torvalds  * already specified in a mount option.
4701da177e4SLinus Torvalds  * We use smaller I/O sizes when the file system
4711da177e4SLinus Torvalds  * is being used for NFS service (wsync mount option).
4721da177e4SLinus Torvalds  */
4730771fb45SEric Sandeen STATIC void
4740771fb45SEric Sandeen xfs_set_rw_sizes(xfs_mount_t *mp)
4750771fb45SEric Sandeen {
4760771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
4770771fb45SEric Sandeen 	int		readio_log, writeio_log;
4780771fb45SEric Sandeen 
4791da177e4SLinus Torvalds 	if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
4801da177e4SLinus Torvalds 		if (mp->m_flags & XFS_MOUNT_WSYNC) {
4811da177e4SLinus Torvalds 			readio_log = XFS_WSYNC_READIO_LOG;
4821da177e4SLinus Torvalds 			writeio_log = XFS_WSYNC_WRITEIO_LOG;
4831da177e4SLinus Torvalds 		} else {
4841da177e4SLinus Torvalds 			readio_log = XFS_READIO_LOG_LARGE;
4851da177e4SLinus Torvalds 			writeio_log = XFS_WRITEIO_LOG_LARGE;
4861da177e4SLinus Torvalds 		}
4871da177e4SLinus Torvalds 	} else {
4881da177e4SLinus Torvalds 		readio_log = mp->m_readio_log;
4891da177e4SLinus Torvalds 		writeio_log = mp->m_writeio_log;
4901da177e4SLinus Torvalds 	}
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	if (sbp->sb_blocklog > readio_log) {
4931da177e4SLinus Torvalds 		mp->m_readio_log = sbp->sb_blocklog;
4941da177e4SLinus Torvalds 	} else {
4951da177e4SLinus Torvalds 		mp->m_readio_log = readio_log;
4961da177e4SLinus Torvalds 	}
4971da177e4SLinus Torvalds 	mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
4981da177e4SLinus Torvalds 	if (sbp->sb_blocklog > writeio_log) {
4991da177e4SLinus Torvalds 		mp->m_writeio_log = sbp->sb_blocklog;
5001da177e4SLinus Torvalds 	} else {
5011da177e4SLinus Torvalds 		mp->m_writeio_log = writeio_log;
5021da177e4SLinus Torvalds 	}
5031da177e4SLinus Torvalds 	mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
5040771fb45SEric Sandeen }
505425f9dddSEric Sandeen 
5061da177e4SLinus Torvalds /*
507055388a3SDave Chinner  * precalculate the low space thresholds for dynamic speculative preallocation.
508055388a3SDave Chinner  */
509055388a3SDave Chinner void
510055388a3SDave Chinner xfs_set_low_space_thresholds(
511055388a3SDave Chinner 	struct xfs_mount	*mp)
512055388a3SDave Chinner {
513055388a3SDave Chinner 	int i;
514055388a3SDave Chinner 
515055388a3SDave Chinner 	for (i = 0; i < XFS_LOWSP_MAX; i++) {
516055388a3SDave Chinner 		__uint64_t space = mp->m_sb.sb_dblocks;
517055388a3SDave Chinner 
518055388a3SDave Chinner 		do_div(space, 100);
519055388a3SDave Chinner 		mp->m_low_space[i] = space * (i + 1);
520055388a3SDave Chinner 	}
521055388a3SDave Chinner }
522055388a3SDave Chinner 
523055388a3SDave Chinner 
524055388a3SDave Chinner /*
5251da177e4SLinus Torvalds  * Set whether we're using inode alignment.
5261da177e4SLinus Torvalds  */
5270771fb45SEric Sandeen STATIC void
5280771fb45SEric Sandeen xfs_set_inoalignment(xfs_mount_t *mp)
5290771fb45SEric Sandeen {
53062118709SEric Sandeen 	if (xfs_sb_version_hasalign(&mp->m_sb) &&
5311da177e4SLinus Torvalds 	    mp->m_sb.sb_inoalignmt >=
5321da177e4SLinus Torvalds 	    XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
5331da177e4SLinus Torvalds 		mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
5341da177e4SLinus Torvalds 	else
5351da177e4SLinus Torvalds 		mp->m_inoalign_mask = 0;
5361da177e4SLinus Torvalds 	/*
5371da177e4SLinus Torvalds 	 * If we are using stripe alignment, check whether
5381da177e4SLinus Torvalds 	 * the stripe unit is a multiple of the inode alignment
5391da177e4SLinus Torvalds 	 */
5401da177e4SLinus Torvalds 	if (mp->m_dalign && mp->m_inoalign_mask &&
5411da177e4SLinus Torvalds 	    !(mp->m_dalign & mp->m_inoalign_mask))
5421da177e4SLinus Torvalds 		mp->m_sinoalign = mp->m_dalign;
5431da177e4SLinus Torvalds 	else
5441da177e4SLinus Torvalds 		mp->m_sinoalign = 0;
5450771fb45SEric Sandeen }
5460771fb45SEric Sandeen 
5471da177e4SLinus Torvalds /*
5480471f62eSZhi Yong Wu  * Check that the data (and log if separate) is an ok size.
5491da177e4SLinus Torvalds  */
5500771fb45SEric Sandeen STATIC int
5514249023aSChristoph Hellwig xfs_check_sizes(xfs_mount_t *mp)
5520771fb45SEric Sandeen {
5530771fb45SEric Sandeen 	xfs_buf_t	*bp;
5540771fb45SEric Sandeen 	xfs_daddr_t	d;
5550771fb45SEric Sandeen 
5561da177e4SLinus Torvalds 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
5571da177e4SLinus Torvalds 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
5580b932cccSDave Chinner 		xfs_warn(mp, "filesystem size mismatch detected");
5592451337dSDave Chinner 		return -EFBIG;
5601da177e4SLinus Torvalds 	}
561e70b73f8SDave Chinner 	bp = xfs_buf_read_uncached(mp->m_ddev_targp,
5621da177e4SLinus Torvalds 					d - XFS_FSS_TO_BB(mp, 1),
563c3f8fc73SDave Chinner 					XFS_FSS_TO_BB(mp, 1), 0, NULL);
5641922c949SDave Chinner 	if (!bp) {
5650b932cccSDave Chinner 		xfs_warn(mp, "last sector read failed");
5662451337dSDave Chinner 		return -EIO;
5671da177e4SLinus Torvalds 	}
5681922c949SDave Chinner 	xfs_buf_relse(bp);
5691da177e4SLinus Torvalds 
5704249023aSChristoph Hellwig 	if (mp->m_logdev_targp != mp->m_ddev_targp) {
5711da177e4SLinus Torvalds 		d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
5721da177e4SLinus Torvalds 		if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
5730b932cccSDave Chinner 			xfs_warn(mp, "log size mismatch detected");
5742451337dSDave Chinner 			return -EFBIG;
5751da177e4SLinus Torvalds 		}
576e70b73f8SDave Chinner 		bp = xfs_buf_read_uncached(mp->m_logdev_targp,
5771da177e4SLinus Torvalds 					d - XFS_FSB_TO_BB(mp, 1),
578c3f8fc73SDave Chinner 					XFS_FSB_TO_BB(mp, 1), 0, NULL);
5791922c949SDave Chinner 		if (!bp) {
5800b932cccSDave Chinner 			xfs_warn(mp, "log device read failed");
5812451337dSDave Chinner 			return -EIO;
5821da177e4SLinus Torvalds 		}
5831922c949SDave Chinner 		xfs_buf_relse(bp);
5840771fb45SEric Sandeen 	}
5850771fb45SEric Sandeen 	return 0;
5860771fb45SEric Sandeen }
5870771fb45SEric Sandeen 
5880771fb45SEric Sandeen /*
5897d095257SChristoph Hellwig  * Clear the quotaflags in memory and in the superblock.
5907d095257SChristoph Hellwig  */
5917d095257SChristoph Hellwig int
5927d095257SChristoph Hellwig xfs_mount_reset_sbqflags(
5937d095257SChristoph Hellwig 	struct xfs_mount	*mp)
5947d095257SChristoph Hellwig {
5957d095257SChristoph Hellwig 	int			error;
5967d095257SChristoph Hellwig 	struct xfs_trans	*tp;
5977d095257SChristoph Hellwig 
5987d095257SChristoph Hellwig 	mp->m_qflags = 0;
5997d095257SChristoph Hellwig 
6007d095257SChristoph Hellwig 	/*
6017d095257SChristoph Hellwig 	 * It is OK to look at sb_qflags here in mount path,
6027d095257SChristoph Hellwig 	 * without m_sb_lock.
6037d095257SChristoph Hellwig 	 */
6047d095257SChristoph Hellwig 	if (mp->m_sb.sb_qflags == 0)
6057d095257SChristoph Hellwig 		return 0;
6067d095257SChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
6077d095257SChristoph Hellwig 	mp->m_sb.sb_qflags = 0;
6087d095257SChristoph Hellwig 	spin_unlock(&mp->m_sb_lock);
6097d095257SChristoph Hellwig 
6107d095257SChristoph Hellwig 	/*
6117d095257SChristoph Hellwig 	 * If the fs is readonly, let the incore superblock run
6127d095257SChristoph Hellwig 	 * with quotas off but don't flush the update out to disk
6137d095257SChristoph Hellwig 	 */
6147d095257SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_RDONLY)
6157d095257SChristoph Hellwig 		return 0;
6167d095257SChristoph Hellwig 
6177d095257SChristoph Hellwig 	tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
6183d3c8b52SJie Liu 	error = xfs_trans_reserve(tp, &M_RES(mp)->tr_qm_sbchange, 0, 0);
6197d095257SChristoph Hellwig 	if (error) {
6207d095257SChristoph Hellwig 		xfs_trans_cancel(tp, 0);
62153487786SDave Chinner 		xfs_alert(mp, "%s: Superblock update failed!", __func__);
6227d095257SChristoph Hellwig 		return error;
6237d095257SChristoph Hellwig 	}
6247d095257SChristoph Hellwig 
6257d095257SChristoph Hellwig 	xfs_mod_sb(tp, XFS_SB_QFLAGS);
6267d095257SChristoph Hellwig 	return xfs_trans_commit(tp, 0);
6277d095257SChristoph Hellwig }
6287d095257SChristoph Hellwig 
629d5db0f97SEric Sandeen __uint64_t
630d5db0f97SEric Sandeen xfs_default_resblks(xfs_mount_t *mp)
631d5db0f97SEric Sandeen {
632d5db0f97SEric Sandeen 	__uint64_t resblks;
633d5db0f97SEric Sandeen 
634d5db0f97SEric Sandeen 	/*
6358babd8a2SDave Chinner 	 * We default to 5% or 8192 fsbs of space reserved, whichever is
6368babd8a2SDave Chinner 	 * smaller.  This is intended to cover concurrent allocation
6378babd8a2SDave Chinner 	 * transactions when we initially hit enospc. These each require a 4
6388babd8a2SDave Chinner 	 * block reservation. Hence by default we cover roughly 2000 concurrent
6398babd8a2SDave Chinner 	 * allocation reservations.
640d5db0f97SEric Sandeen 	 */
641d5db0f97SEric Sandeen 	resblks = mp->m_sb.sb_dblocks;
642d5db0f97SEric Sandeen 	do_div(resblks, 20);
6438babd8a2SDave Chinner 	resblks = min_t(__uint64_t, resblks, 8192);
644d5db0f97SEric Sandeen 	return resblks;
645d5db0f97SEric Sandeen }
646d5db0f97SEric Sandeen 
6477d095257SChristoph Hellwig /*
6480771fb45SEric Sandeen  * This function does the following on an initial mount of a file system:
6490771fb45SEric Sandeen  *	- reads the superblock from disk and init the mount struct
6500771fb45SEric Sandeen  *	- if we're a 32-bit kernel, do a size check on the superblock
6510771fb45SEric Sandeen  *		so we don't mount terabyte filesystems
6520771fb45SEric Sandeen  *	- init mount struct realtime fields
6530771fb45SEric Sandeen  *	- allocate inode hash table for fs
6540771fb45SEric Sandeen  *	- init directory manager
6550771fb45SEric Sandeen  *	- perform recovery and init the log manager
6560771fb45SEric Sandeen  */
6570771fb45SEric Sandeen int
6580771fb45SEric Sandeen xfs_mountfs(
6594249023aSChristoph Hellwig 	xfs_mount_t	*mp)
6600771fb45SEric Sandeen {
6610771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
6620771fb45SEric Sandeen 	xfs_inode_t	*rip;
6630771fb45SEric Sandeen 	__uint64_t	resblks;
6647d095257SChristoph Hellwig 	uint		quotamount = 0;
6657d095257SChristoph Hellwig 	uint		quotaflags = 0;
6660771fb45SEric Sandeen 	int		error = 0;
6670771fb45SEric Sandeen 
668ff55068cSDave Chinner 	xfs_sb_mount_common(mp, sbp);
6690771fb45SEric Sandeen 
6700771fb45SEric Sandeen 	/*
671e6957ea4SEric Sandeen 	 * Check for a mismatched features2 values.  Older kernels
672e6957ea4SEric Sandeen 	 * read & wrote into the wrong sb offset for sb_features2
673e6957ea4SEric Sandeen 	 * on some platforms due to xfs_sb_t not being 64bit size aligned
674e6957ea4SEric Sandeen 	 * when sb_features2 was added, which made older superblock
675e6957ea4SEric Sandeen 	 * reading/writing routines swap it as a 64-bit value.
676ee1c0908SDavid Chinner 	 *
677e6957ea4SEric Sandeen 	 * For backwards compatibility, we make both slots equal.
678e6957ea4SEric Sandeen 	 *
679e6957ea4SEric Sandeen 	 * If we detect a mismatched field, we OR the set bits into the
680e6957ea4SEric Sandeen 	 * existing features2 field in case it has already been modified; we
681e6957ea4SEric Sandeen 	 * don't want to lose any features.  We then update the bad location
682e6957ea4SEric Sandeen 	 * with the ORed value so that older kernels will see any features2
683e6957ea4SEric Sandeen 	 * flags, and mark the two fields as needing updates once the
684e6957ea4SEric Sandeen 	 * transaction subsystem is online.
685ee1c0908SDavid Chinner 	 */
686e6957ea4SEric Sandeen 	if (xfs_sb_has_mismatched_features2(sbp)) {
6870b932cccSDave Chinner 		xfs_warn(mp, "correcting sb_features alignment problem");
688ee1c0908SDavid Chinner 		sbp->sb_features2 |= sbp->sb_bad_features2;
689e6957ea4SEric Sandeen 		sbp->sb_bad_features2 = sbp->sb_features2;
6907884bc86SChristoph Hellwig 		mp->m_update_flags |= XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2;
691e6957ea4SEric Sandeen 
692e6957ea4SEric Sandeen 		/*
693e6957ea4SEric Sandeen 		 * Re-check for ATTR2 in case it was found in bad_features2
694e6957ea4SEric Sandeen 		 * slot.
695e6957ea4SEric Sandeen 		 */
6967c12f296STim Shimmin 		if (xfs_sb_version_hasattr2(&mp->m_sb) &&
6977c12f296STim Shimmin 		   !(mp->m_flags & XFS_MOUNT_NOATTR2))
698e6957ea4SEric Sandeen 			mp->m_flags |= XFS_MOUNT_ATTR2;
6997c12f296STim Shimmin 	}
700e6957ea4SEric Sandeen 
7017c12f296STim Shimmin 	if (xfs_sb_version_hasattr2(&mp->m_sb) &&
7027c12f296STim Shimmin 	   (mp->m_flags & XFS_MOUNT_NOATTR2)) {
7037c12f296STim Shimmin 		xfs_sb_version_removeattr2(&mp->m_sb);
7047884bc86SChristoph Hellwig 		mp->m_update_flags |= XFS_SB_FEATURES2;
7057c12f296STim Shimmin 
7067c12f296STim Shimmin 		/* update sb_versionnum for the clearing of the morebits */
7077c12f296STim Shimmin 		if (!sbp->sb_features2)
7087884bc86SChristoph Hellwig 			mp->m_update_flags |= XFS_SB_VERSIONNUM;
709ee1c0908SDavid Chinner 	}
710ee1c0908SDavid Chinner 
711263997a6SDave Chinner 	/* always use v2 inodes by default now */
712263997a6SDave Chinner 	if (!(mp->m_sb.sb_versionnum & XFS_SB_VERSION_NLINKBIT)) {
713263997a6SDave Chinner 		mp->m_sb.sb_versionnum |= XFS_SB_VERSION_NLINKBIT;
714263997a6SDave Chinner 		mp->m_update_flags |= XFS_SB_VERSIONNUM;
715263997a6SDave Chinner 	}
716263997a6SDave Chinner 
717ee1c0908SDavid Chinner 	/*
7180771fb45SEric Sandeen 	 * Check if sb_agblocks is aligned at stripe boundary
7190771fb45SEric Sandeen 	 * If sb_agblocks is NOT aligned turn off m_dalign since
7200771fb45SEric Sandeen 	 * allocator alignment is within an ag, therefore ag has
7210771fb45SEric Sandeen 	 * to be aligned at stripe boundary.
7220771fb45SEric Sandeen 	 */
7237884bc86SChristoph Hellwig 	error = xfs_update_alignment(mp);
7240771fb45SEric Sandeen 	if (error)
725f9057e3dSChristoph Hellwig 		goto out;
7260771fb45SEric Sandeen 
7270771fb45SEric Sandeen 	xfs_alloc_compute_maxlevels(mp);
7280771fb45SEric Sandeen 	xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
7290771fb45SEric Sandeen 	xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
7300771fb45SEric Sandeen 	xfs_ialloc_compute_maxlevels(mp);
7310771fb45SEric Sandeen 
7320771fb45SEric Sandeen 	xfs_set_maxicount(mp);
7330771fb45SEric Sandeen 
73427174203SChristoph Hellwig 	error = xfs_uuid_mount(mp);
73527174203SChristoph Hellwig 	if (error)
736f9057e3dSChristoph Hellwig 		goto out;
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 	/*
7390771fb45SEric Sandeen 	 * Set the minimum read and write sizes
7400771fb45SEric Sandeen 	 */
7410771fb45SEric Sandeen 	xfs_set_rw_sizes(mp);
7420771fb45SEric Sandeen 
743055388a3SDave Chinner 	/* set the low space thresholds for dynamic preallocation */
744055388a3SDave Chinner 	xfs_set_low_space_thresholds(mp);
745055388a3SDave Chinner 
7460771fb45SEric Sandeen 	/*
7470771fb45SEric Sandeen 	 * Set the inode cluster size.
7480771fb45SEric Sandeen 	 * This may still be overridden by the file system
7490771fb45SEric Sandeen 	 * block size if it is larger than the chosen cluster size.
7508f80587bSDave Chinner 	 *
7518f80587bSDave Chinner 	 * For v5 filesystems, scale the cluster size with the inode size to
7528f80587bSDave Chinner 	 * keep a constant ratio of inode per cluster buffer, but only if mkfs
7538f80587bSDave Chinner 	 * has set the inode alignment value appropriately for larger cluster
7548f80587bSDave Chinner 	 * sizes.
7550771fb45SEric Sandeen 	 */
7560771fb45SEric Sandeen 	mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
7578f80587bSDave Chinner 	if (xfs_sb_version_hascrc(&mp->m_sb)) {
7588f80587bSDave Chinner 		int	new_size = mp->m_inode_cluster_size;
7598f80587bSDave Chinner 
7608f80587bSDave Chinner 		new_size *= mp->m_sb.sb_inodesize / XFS_DINODE_MIN_SIZE;
7618f80587bSDave Chinner 		if (mp->m_sb.sb_inoalignmt >= XFS_B_TO_FSBT(mp, new_size))
7628f80587bSDave Chinner 			mp->m_inode_cluster_size = new_size;
7638f80587bSDave Chinner 	}
7640771fb45SEric Sandeen 
7650771fb45SEric Sandeen 	/*
7660771fb45SEric Sandeen 	 * Set inode alignment fields
7670771fb45SEric Sandeen 	 */
7680771fb45SEric Sandeen 	xfs_set_inoalignment(mp);
7690771fb45SEric Sandeen 
7700771fb45SEric Sandeen 	/*
771c2bfbc9bSZhi Yong Wu 	 * Check that the data (and log if separate) is an ok size.
7720771fb45SEric Sandeen 	 */
7734249023aSChristoph Hellwig 	error = xfs_check_sizes(mp);
7740771fb45SEric Sandeen 	if (error)
775f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
7760771fb45SEric Sandeen 
7770771fb45SEric Sandeen 	/*
7781da177e4SLinus Torvalds 	 * Initialize realtime fields in the mount structure
7791da177e4SLinus Torvalds 	 */
7800771fb45SEric Sandeen 	error = xfs_rtmount_init(mp);
7810771fb45SEric Sandeen 	if (error) {
7820b932cccSDave Chinner 		xfs_warn(mp, "RT mount failed");
783f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
7841da177e4SLinus Torvalds 	}
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	/*
7871da177e4SLinus Torvalds 	 *  Copies the low order bits of the timestamp and the randomly
7881da177e4SLinus Torvalds 	 *  set "sequence" number out of a UUID.
7891da177e4SLinus Torvalds 	 */
7901da177e4SLinus Torvalds 	uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	mp->m_dmevmask = 0;	/* not persistent; set after each mount */
7931da177e4SLinus Torvalds 
7940650b554SDave Chinner 	error = xfs_da_mount(mp);
7950650b554SDave Chinner 	if (error) {
7960650b554SDave Chinner 		xfs_warn(mp, "Failed dir/attr init: %d", error);
7970650b554SDave Chinner 		goto out_remove_uuid;
7980650b554SDave Chinner 	}
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 	/*
8011da177e4SLinus Torvalds 	 * Initialize the precomputed transaction reservations values.
8021da177e4SLinus Torvalds 	 */
8031da177e4SLinus Torvalds 	xfs_trans_init(mp);
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	/*
8061da177e4SLinus Torvalds 	 * Allocate and initialize the per-ag data.
8071da177e4SLinus Torvalds 	 */
8081c1c6ebcSDave Chinner 	spin_lock_init(&mp->m_perag_lock);
8099b98b6f3SDave Chinner 	INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
8101c1c6ebcSDave Chinner 	error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
8111c1c6ebcSDave Chinner 	if (error) {
8120b932cccSDave Chinner 		xfs_warn(mp, "Failed per-ag init: %d", error);
8130650b554SDave Chinner 		goto out_free_dir;
8141c1c6ebcSDave Chinner 	}
8151da177e4SLinus Torvalds 
816f9057e3dSChristoph Hellwig 	if (!sbp->sb_logblocks) {
8170b932cccSDave Chinner 		xfs_warn(mp, "no log defined");
818f9057e3dSChristoph Hellwig 		XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp);
8192451337dSDave Chinner 		error = -EFSCORRUPTED;
820f9057e3dSChristoph Hellwig 		goto out_free_perag;
821f9057e3dSChristoph Hellwig 	}
822f9057e3dSChristoph Hellwig 
8231da177e4SLinus Torvalds 	/*
8241da177e4SLinus Torvalds 	 * log's mount-time initialization. Perform 1st part recovery if needed
8251da177e4SLinus Torvalds 	 */
8261da177e4SLinus Torvalds 	error = xfs_log_mount(mp, mp->m_logdev_targp,
8271da177e4SLinus Torvalds 			      XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
8281da177e4SLinus Torvalds 			      XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
8291da177e4SLinus Torvalds 	if (error) {
8300b932cccSDave Chinner 		xfs_warn(mp, "log mount failed");
831d4f3512bSDave Chinner 		goto out_fail_wait;
8321da177e4SLinus Torvalds 	}
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 	/*
83592821e2bSDavid Chinner 	 * Now the log is mounted, we know if it was an unclean shutdown or
83692821e2bSDavid Chinner 	 * not. If it was, with the first phase of recovery has completed, we
83792821e2bSDavid Chinner 	 * have consistent AG blocks on disk. We have not recovered EFIs yet,
83892821e2bSDavid Chinner 	 * but they are recovered transactionally in the second recovery phase
83992821e2bSDavid Chinner 	 * later.
84092821e2bSDavid Chinner 	 *
84192821e2bSDavid Chinner 	 * Hence we can safely re-initialise incore superblock counters from
84292821e2bSDavid Chinner 	 * the per-ag data. These may not be correct if the filesystem was not
84392821e2bSDavid Chinner 	 * cleanly unmounted, so we need to wait for recovery to finish before
84492821e2bSDavid Chinner 	 * doing this.
84592821e2bSDavid Chinner 	 *
84692821e2bSDavid Chinner 	 * If the filesystem was cleanly unmounted, then we can trust the
84792821e2bSDavid Chinner 	 * values in the superblock to be correct and we don't need to do
84892821e2bSDavid Chinner 	 * anything here.
84992821e2bSDavid Chinner 	 *
85092821e2bSDavid Chinner 	 * If we are currently making the filesystem, the initialisation will
85192821e2bSDavid Chinner 	 * fail as the perag data is in an undefined state.
85292821e2bSDavid Chinner 	 */
85392821e2bSDavid Chinner 	if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
85492821e2bSDavid Chinner 	    !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
85592821e2bSDavid Chinner 	     !mp->m_sb.sb_inprogress) {
85692821e2bSDavid Chinner 		error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
857f9057e3dSChristoph Hellwig 		if (error)
858d4f3512bSDave Chinner 			goto out_fail_wait;
85992821e2bSDavid Chinner 	}
860f9057e3dSChristoph Hellwig 
86192821e2bSDavid Chinner 	/*
8621da177e4SLinus Torvalds 	 * Get and sanity-check the root inode.
8631da177e4SLinus Torvalds 	 * Save the pointer to it in the mount structure.
8641da177e4SLinus Torvalds 	 */
8657b6259e7SDave Chinner 	error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip);
8661da177e4SLinus Torvalds 	if (error) {
8670b932cccSDave Chinner 		xfs_warn(mp, "failed to read root inode");
868f9057e3dSChristoph Hellwig 		goto out_log_dealloc;
8691da177e4SLinus Torvalds 	}
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	ASSERT(rip != NULL);
8721da177e4SLinus Torvalds 
873abbede1bSAl Viro 	if (unlikely(!S_ISDIR(rip->i_d.di_mode))) {
8740b932cccSDave Chinner 		xfs_warn(mp, "corrupted root inode %llu: not a directory",
875b6574520SNathan Scott 			(unsigned long long)rip->i_ino);
8761da177e4SLinus Torvalds 		xfs_iunlock(rip, XFS_ILOCK_EXCL);
8771da177e4SLinus Torvalds 		XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
8781da177e4SLinus Torvalds 				 mp);
8792451337dSDave Chinner 		error = -EFSCORRUPTED;
880f9057e3dSChristoph Hellwig 		goto out_rele_rip;
8811da177e4SLinus Torvalds 	}
8821da177e4SLinus Torvalds 	mp->m_rootip = rip;	/* save it */
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	xfs_iunlock(rip, XFS_ILOCK_EXCL);
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	/*
8871da177e4SLinus Torvalds 	 * Initialize realtime inode pointers in the mount structure
8881da177e4SLinus Torvalds 	 */
8890771fb45SEric Sandeen 	error = xfs_rtmount_inodes(mp);
8900771fb45SEric Sandeen 	if (error) {
8911da177e4SLinus Torvalds 		/*
8921da177e4SLinus Torvalds 		 * Free up the root inode.
8931da177e4SLinus Torvalds 		 */
8940b932cccSDave Chinner 		xfs_warn(mp, "failed to read RT inodes");
895f9057e3dSChristoph Hellwig 		goto out_rele_rip;
8961da177e4SLinus Torvalds 	}
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	/*
8997884bc86SChristoph Hellwig 	 * If this is a read-only mount defer the superblock updates until
9007884bc86SChristoph Hellwig 	 * the next remount into writeable mode.  Otherwise we would never
9017884bc86SChristoph Hellwig 	 * perform the update e.g. for the root filesystem.
9021da177e4SLinus Torvalds 	 */
9037884bc86SChristoph Hellwig 	if (mp->m_update_flags && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
9047884bc86SChristoph Hellwig 		error = xfs_mount_log_sb(mp, mp->m_update_flags);
905e5720eecSDavid Chinner 		if (error) {
9060b932cccSDave Chinner 			xfs_warn(mp, "failed to write sb changes");
907b93b6e43SChristoph Hellwig 			goto out_rtunmount;
908e5720eecSDavid Chinner 		}
909e5720eecSDavid Chinner 	}
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	/*
9121da177e4SLinus Torvalds 	 * Initialise the XFS quota management subsystem for this mount
9131da177e4SLinus Torvalds 	 */
9147d095257SChristoph Hellwig 	if (XFS_IS_QUOTA_RUNNING(mp)) {
9157d095257SChristoph Hellwig 		error = xfs_qm_newmount(mp, &quotamount, &quotaflags);
9160771fb45SEric Sandeen 		if (error)
917b93b6e43SChristoph Hellwig 			goto out_rtunmount;
9187d095257SChristoph Hellwig 	} else {
9197d095257SChristoph Hellwig 		ASSERT(!XFS_IS_QUOTA_ON(mp));
9207d095257SChristoph Hellwig 
9217d095257SChristoph Hellwig 		/*
9227d095257SChristoph Hellwig 		 * If a file system had quotas running earlier, but decided to
9237d095257SChristoph Hellwig 		 * mount without -o uquota/pquota/gquota options, revoke the
9247d095257SChristoph Hellwig 		 * quotachecked license.
9257d095257SChristoph Hellwig 		 */
9267d095257SChristoph Hellwig 		if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) {
9270b932cccSDave Chinner 			xfs_notice(mp, "resetting quota flags");
9287d095257SChristoph Hellwig 			error = xfs_mount_reset_sbqflags(mp);
9297d095257SChristoph Hellwig 			if (error)
9307d095257SChristoph Hellwig 				return error;
9317d095257SChristoph Hellwig 		}
9327d095257SChristoph Hellwig 	}
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds 	/*
9351da177e4SLinus Torvalds 	 * Finish recovering the file system.  This part needed to be
9361da177e4SLinus Torvalds 	 * delayed until after the root and real-time bitmap inodes
9371da177e4SLinus Torvalds 	 * were consistently read in.
9381da177e4SLinus Torvalds 	 */
9394249023aSChristoph Hellwig 	error = xfs_log_mount_finish(mp);
9401da177e4SLinus Torvalds 	if (error) {
9410b932cccSDave Chinner 		xfs_warn(mp, "log mount finish failed");
942b93b6e43SChristoph Hellwig 		goto out_rtunmount;
9431da177e4SLinus Torvalds 	}
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	/*
9461da177e4SLinus Torvalds 	 * Complete the quota initialisation, post-log-replay component.
9471da177e4SLinus Torvalds 	 */
9487d095257SChristoph Hellwig 	if (quotamount) {
9497d095257SChristoph Hellwig 		ASSERT(mp->m_qflags == 0);
9507d095257SChristoph Hellwig 		mp->m_qflags = quotaflags;
9517d095257SChristoph Hellwig 
9527d095257SChristoph Hellwig 		xfs_qm_mount_quotas(mp);
9537d095257SChristoph Hellwig 	}
9547d095257SChristoph Hellwig 
95584e1e99fSDavid Chinner 	/*
95684e1e99fSDavid Chinner 	 * Now we are mounted, reserve a small amount of unused space for
95784e1e99fSDavid Chinner 	 * privileged transactions. This is needed so that transaction
95884e1e99fSDavid Chinner 	 * space required for critical operations can dip into this pool
95984e1e99fSDavid Chinner 	 * when at ENOSPC. This is needed for operations like create with
96084e1e99fSDavid Chinner 	 * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
96184e1e99fSDavid Chinner 	 * are not allowed to use this reserved space.
9628babd8a2SDave Chinner 	 *
9638babd8a2SDave Chinner 	 * This may drive us straight to ENOSPC on mount, but that implies
9648babd8a2SDave Chinner 	 * we were already there on the last unmount. Warn if this occurs.
96584e1e99fSDavid Chinner 	 */
966d5db0f97SEric Sandeen 	if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
967d5db0f97SEric Sandeen 		resblks = xfs_default_resblks(mp);
968714082bcSDavid Chinner 		error = xfs_reserve_blocks(mp, &resblks, NULL);
969714082bcSDavid Chinner 		if (error)
9700b932cccSDave Chinner 			xfs_warn(mp,
9710b932cccSDave Chinner 	"Unable to allocate reserve blocks. Continuing without reserve pool.");
972d5db0f97SEric Sandeen 	}
97384e1e99fSDavid Chinner 
9741da177e4SLinus Torvalds 	return 0;
9751da177e4SLinus Torvalds 
976b93b6e43SChristoph Hellwig  out_rtunmount:
977b93b6e43SChristoph Hellwig 	xfs_rtunmount_inodes(mp);
978f9057e3dSChristoph Hellwig  out_rele_rip:
97943355099SChristoph Hellwig 	IRELE(rip);
980f9057e3dSChristoph Hellwig  out_log_dealloc:
98121b699c8SChristoph Hellwig 	xfs_log_unmount(mp);
982d4f3512bSDave Chinner  out_fail_wait:
983d4f3512bSDave Chinner 	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
984d4f3512bSDave Chinner 		xfs_wait_buftarg(mp->m_logdev_targp);
985d4f3512bSDave Chinner 	xfs_wait_buftarg(mp->m_ddev_targp);
986f9057e3dSChristoph Hellwig  out_free_perag:
987ff4f038cSChristoph Hellwig 	xfs_free_perag(mp);
9880650b554SDave Chinner  out_free_dir:
9890650b554SDave Chinner 	xfs_da_unmount(mp);
990f9057e3dSChristoph Hellwig  out_remove_uuid:
99127174203SChristoph Hellwig 	xfs_uuid_unmount(mp);
992f9057e3dSChristoph Hellwig  out:
9931da177e4SLinus Torvalds 	return error;
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds /*
9971da177e4SLinus Torvalds  * This flushes out the inodes,dquots and the superblock, unmounts the
9981da177e4SLinus Torvalds  * log and makes sure that incore structures are freed.
9991da177e4SLinus Torvalds  */
100041b5c2e7SChristoph Hellwig void
100141b5c2e7SChristoph Hellwig xfs_unmountfs(
100241b5c2e7SChristoph Hellwig 	struct xfs_mount	*mp)
10031da177e4SLinus Torvalds {
100484e1e99fSDavid Chinner 	__uint64_t		resblks;
100541b5c2e7SChristoph Hellwig 	int			error;
10061da177e4SLinus Torvalds 
1007579b62faSBrian Foster 	cancel_delayed_work_sync(&mp->m_eofblocks_work);
1008579b62faSBrian Foster 
10097d095257SChristoph Hellwig 	xfs_qm_unmount_quotas(mp);
1010b93b6e43SChristoph Hellwig 	xfs_rtunmount_inodes(mp);
101177508ec8SChristoph Hellwig 	IRELE(mp->m_rootip);
101277508ec8SChristoph Hellwig 
1013641c56fbSDavid Chinner 	/*
1014641c56fbSDavid Chinner 	 * We can potentially deadlock here if we have an inode cluster
10159da096fdSMalcolm Parsons 	 * that has been freed has its buffer still pinned in memory because
1016641c56fbSDavid Chinner 	 * the transaction is still sitting in a iclog. The stale inodes
1017641c56fbSDavid Chinner 	 * on that buffer will have their flush locks held until the
1018641c56fbSDavid Chinner 	 * transaction hits the disk and the callbacks run. the inode
1019641c56fbSDavid Chinner 	 * flush takes the flush lock unconditionally and with nothing to
1020641c56fbSDavid Chinner 	 * push out the iclog we will never get that unlocked. hence we
1021641c56fbSDavid Chinner 	 * need to force the log first.
1022641c56fbSDavid Chinner 	 */
1023a14a348bSChristoph Hellwig 	xfs_log_force(mp, XFS_LOG_SYNC);
1024c854363eSDave Chinner 
1025c854363eSDave Chinner 	/*
1026211e4d43SChristoph Hellwig 	 * Flush all pending changes from the AIL.
1027c854363eSDave Chinner 	 */
1028211e4d43SChristoph Hellwig 	xfs_ail_push_all_sync(mp->m_ail);
1029211e4d43SChristoph Hellwig 
1030211e4d43SChristoph Hellwig 	/*
1031211e4d43SChristoph Hellwig 	 * And reclaim all inodes.  At this point there should be no dirty
10327e18530bSDave Chinner 	 * inodes and none should be pinned or locked, but use synchronous
10337e18530bSDave Chinner 	 * reclaim just to be sure. We can stop background inode reclaim
10347e18530bSDave Chinner 	 * here as well if it is still running.
1035211e4d43SChristoph Hellwig 	 */
10367e18530bSDave Chinner 	cancel_delayed_work_sync(&mp->m_reclaim_work);
1037c854363eSDave Chinner 	xfs_reclaim_inodes(mp, SYNC_WAIT);
10381da177e4SLinus Torvalds 
10397d095257SChristoph Hellwig 	xfs_qm_unmount(mp);
1040a357a121SLachlan McIlroy 
10411da177e4SLinus Torvalds 	/*
104284e1e99fSDavid Chinner 	 * Unreserve any blocks we have so that when we unmount we don't account
104384e1e99fSDavid Chinner 	 * the reserved free space as used. This is really only necessary for
104484e1e99fSDavid Chinner 	 * lazy superblock counting because it trusts the incore superblock
10459da096fdSMalcolm Parsons 	 * counters to be absolutely correct on clean unmount.
104684e1e99fSDavid Chinner 	 *
104784e1e99fSDavid Chinner 	 * We don't bother correcting this elsewhere for lazy superblock
104884e1e99fSDavid Chinner 	 * counting because on mount of an unclean filesystem we reconstruct the
104984e1e99fSDavid Chinner 	 * correct counter value and this is irrelevant.
105084e1e99fSDavid Chinner 	 *
105184e1e99fSDavid Chinner 	 * For non-lazy counter filesystems, this doesn't matter at all because
105284e1e99fSDavid Chinner 	 * we only every apply deltas to the superblock and hence the incore
105384e1e99fSDavid Chinner 	 * value does not matter....
105484e1e99fSDavid Chinner 	 */
105584e1e99fSDavid Chinner 	resblks = 0;
1056714082bcSDavid Chinner 	error = xfs_reserve_blocks(mp, &resblks, NULL);
1057714082bcSDavid Chinner 	if (error)
10580b932cccSDave Chinner 		xfs_warn(mp, "Unable to free reserved block pool. "
1059714082bcSDavid Chinner 				"Freespace may not be correct on next mount.");
1060714082bcSDavid Chinner 
1061adab0f67SChandra Seetharaman 	error = xfs_log_sbcount(mp);
1062e5720eecSDavid Chinner 	if (error)
10630b932cccSDave Chinner 		xfs_warn(mp, "Unable to update superblock counters. "
1064e5720eecSDavid Chinner 				"Freespace may not be correct on next mount.");
106587c7bec7SChristoph Hellwig 
106621b699c8SChristoph Hellwig 	xfs_log_unmount(mp);
10670650b554SDave Chinner 	xfs_da_unmount(mp);
106827174203SChristoph Hellwig 	xfs_uuid_unmount(mp);
10691da177e4SLinus Torvalds 
10701550d0b0SChristoph Hellwig #if defined(DEBUG)
10710ce4cfd4SChristoph Hellwig 	xfs_errortag_clearall(mp, 0);
10721da177e4SLinus Torvalds #endif
1073ff4f038cSChristoph Hellwig 	xfs_free_perag(mp);
10741da177e4SLinus Torvalds }
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds int
107792821e2bSDavid Chinner xfs_fs_writable(xfs_mount_t *mp)
107892821e2bSDavid Chinner {
1079d9457dc0SJan Kara 	return !(mp->m_super->s_writers.frozen || XFS_FORCED_SHUTDOWN(mp) ||
1080bd186aa9SChristoph Hellwig 		(mp->m_flags & XFS_MOUNT_RDONLY));
108192821e2bSDavid Chinner }
108292821e2bSDavid Chinner 
108392821e2bSDavid Chinner /*
1084b2ce3974SAlex Elder  * xfs_log_sbcount
1085b2ce3974SAlex Elder  *
1086adab0f67SChandra Seetharaman  * Sync the superblock counters to disk.
1087b2ce3974SAlex Elder  *
1088b2ce3974SAlex Elder  * Note this code can be called during the process of freezing, so
1089adab0f67SChandra Seetharaman  * we may need to use the transaction allocator which does not
1090b2ce3974SAlex Elder  * block when the transaction subsystem is in its frozen state.
109192821e2bSDavid Chinner  */
109292821e2bSDavid Chinner int
1093adab0f67SChandra Seetharaman xfs_log_sbcount(xfs_mount_t *mp)
109492821e2bSDavid Chinner {
109592821e2bSDavid Chinner 	xfs_trans_t	*tp;
109692821e2bSDavid Chinner 	int		error;
109792821e2bSDavid Chinner 
109892821e2bSDavid Chinner 	if (!xfs_fs_writable(mp))
109992821e2bSDavid Chinner 		return 0;
110092821e2bSDavid Chinner 
1101d4d90b57SChristoph Hellwig 	xfs_icsb_sync_counters(mp, 0);
110292821e2bSDavid Chinner 
110392821e2bSDavid Chinner 	/*
110492821e2bSDavid Chinner 	 * we don't need to do this if we are updating the superblock
110592821e2bSDavid Chinner 	 * counters on every modification.
110692821e2bSDavid Chinner 	 */
110792821e2bSDavid Chinner 	if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
110892821e2bSDavid Chinner 		return 0;
110992821e2bSDavid Chinner 
1110b2ce3974SAlex Elder 	tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT, KM_SLEEP);
11113d3c8b52SJie Liu 	error = xfs_trans_reserve(tp, &M_RES(mp)->tr_sb, 0, 0);
111292821e2bSDavid Chinner 	if (error) {
111392821e2bSDavid Chinner 		xfs_trans_cancel(tp, 0);
111492821e2bSDavid Chinner 		return error;
111592821e2bSDavid Chinner 	}
111692821e2bSDavid Chinner 
111792821e2bSDavid Chinner 	xfs_mod_sb(tp, XFS_SB_IFREE | XFS_SB_ICOUNT | XFS_SB_FDBLOCKS);
111892821e2bSDavid Chinner 	xfs_trans_set_sync(tp);
1119e5720eecSDavid Chinner 	error = xfs_trans_commit(tp, 0);
1120e5720eecSDavid Chinner 	return error;
112192821e2bSDavid Chinner }
112292821e2bSDavid Chinner 
11231da177e4SLinus Torvalds /*
112499e738b7SZhi Yong Wu  * xfs_mod_incore_sb_unlocked() is a utility routine commonly used to apply
11251da177e4SLinus Torvalds  * a delta to a specified field in the in-core superblock.  Simply
11261da177e4SLinus Torvalds  * switch on the field indicated and apply the delta to that field.
11271da177e4SLinus Torvalds  * Fields are not allowed to dip below zero, so if the delta would
11281da177e4SLinus Torvalds  * do this do not apply it and return EINVAL.
11291da177e4SLinus Torvalds  *
11303685c2a1SEric Sandeen  * The m_sb_lock must be held when this routine is called.
11311da177e4SLinus Torvalds  */
1132d96f8f89SEric Sandeen STATIC int
113320f4ebf2SDavid Chinner xfs_mod_incore_sb_unlocked(
113420f4ebf2SDavid Chinner 	xfs_mount_t	*mp,
113520f4ebf2SDavid Chinner 	xfs_sb_field_t	field,
113620f4ebf2SDavid Chinner 	int64_t		delta,
113720f4ebf2SDavid Chinner 	int		rsvd)
11381da177e4SLinus Torvalds {
11391da177e4SLinus Torvalds 	int		scounter;	/* short counter for 32 bit fields */
11401da177e4SLinus Torvalds 	long long	lcounter;	/* long counter for 64 bit fields */
11411da177e4SLinus Torvalds 	long long	res_used, rem;
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 	/*
11441da177e4SLinus Torvalds 	 * With the in-core superblock spin lock held, switch
11451da177e4SLinus Torvalds 	 * on the indicated field.  Apply the delta to the
11461da177e4SLinus Torvalds 	 * proper field.  If the fields value would dip below
11471da177e4SLinus Torvalds 	 * 0, then do not apply the delta and return EINVAL.
11481da177e4SLinus Torvalds 	 */
11491da177e4SLinus Torvalds 	switch (field) {
11501da177e4SLinus Torvalds 	case XFS_SBS_ICOUNT:
11511da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_icount;
11521da177e4SLinus Torvalds 		lcounter += delta;
11531da177e4SLinus Torvalds 		if (lcounter < 0) {
11541da177e4SLinus Torvalds 			ASSERT(0);
11552451337dSDave Chinner 			return -EINVAL;
11561da177e4SLinus Torvalds 		}
11571da177e4SLinus Torvalds 		mp->m_sb.sb_icount = lcounter;
1158014c2544SJesper Juhl 		return 0;
11591da177e4SLinus Torvalds 	case XFS_SBS_IFREE:
11601da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_ifree;
11611da177e4SLinus Torvalds 		lcounter += delta;
11621da177e4SLinus Torvalds 		if (lcounter < 0) {
11631da177e4SLinus Torvalds 			ASSERT(0);
11642451337dSDave Chinner 			return -EINVAL;
11651da177e4SLinus Torvalds 		}
11661da177e4SLinus Torvalds 		mp->m_sb.sb_ifree = lcounter;
1167014c2544SJesper Juhl 		return 0;
11681da177e4SLinus Torvalds 	case XFS_SBS_FDBLOCKS:
11694be536deSDavid Chinner 		lcounter = (long long)
11704be536deSDavid Chinner 			mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
11711da177e4SLinus Torvalds 		res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 		if (delta > 0) {		/* Putting blocks back */
11741da177e4SLinus Torvalds 			if (res_used > delta) {
11751da177e4SLinus Torvalds 				mp->m_resblks_avail += delta;
11761da177e4SLinus Torvalds 			} else {
11771da177e4SLinus Torvalds 				rem = delta - res_used;
11781da177e4SLinus Torvalds 				mp->m_resblks_avail = mp->m_resblks;
11791da177e4SLinus Torvalds 				lcounter += rem;
11801da177e4SLinus Torvalds 			}
11811da177e4SLinus Torvalds 		} else {				/* Taking blocks away */
11821da177e4SLinus Torvalds 			lcounter += delta;
11838babd8a2SDave Chinner 			if (lcounter >= 0) {
11848babd8a2SDave Chinner 				mp->m_sb.sb_fdblocks = lcounter +
11858babd8a2SDave Chinner 							XFS_ALLOC_SET_ASIDE(mp);
11868babd8a2SDave Chinner 				return 0;
11878babd8a2SDave Chinner 			}
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 			/*
11908babd8a2SDave Chinner 			 * We are out of blocks, use any available reserved
11918babd8a2SDave Chinner 			 * blocks if were allowed to.
11921da177e4SLinus Torvalds 			 */
11938babd8a2SDave Chinner 			if (!rsvd)
11942451337dSDave Chinner 				return -ENOSPC;
11958babd8a2SDave Chinner 
11968babd8a2SDave Chinner 			lcounter = (long long)mp->m_resblks_avail + delta;
11978babd8a2SDave Chinner 			if (lcounter >= 0) {
11981da177e4SLinus Torvalds 				mp->m_resblks_avail = lcounter;
1199014c2544SJesper Juhl 				return 0;
12008babd8a2SDave Chinner 			}
12018babd8a2SDave Chinner 			printk_once(KERN_WARNING
12028babd8a2SDave Chinner 				"Filesystem \"%s\": reserve blocks depleted! "
12038babd8a2SDave Chinner 				"Consider increasing reserve pool size.",
12048babd8a2SDave Chinner 				mp->m_fsname);
12052451337dSDave Chinner 			return -ENOSPC;
12061da177e4SLinus Torvalds 		}
12071da177e4SLinus Torvalds 
12084be536deSDavid Chinner 		mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
1209014c2544SJesper Juhl 		return 0;
12101da177e4SLinus Torvalds 	case XFS_SBS_FREXTENTS:
12111da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_frextents;
12121da177e4SLinus Torvalds 		lcounter += delta;
12131da177e4SLinus Torvalds 		if (lcounter < 0) {
12142451337dSDave Chinner 			return -ENOSPC;
12151da177e4SLinus Torvalds 		}
12161da177e4SLinus Torvalds 		mp->m_sb.sb_frextents = lcounter;
1217014c2544SJesper Juhl 		return 0;
12181da177e4SLinus Torvalds 	case XFS_SBS_DBLOCKS:
12191da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_dblocks;
12201da177e4SLinus Torvalds 		lcounter += delta;
12211da177e4SLinus Torvalds 		if (lcounter < 0) {
12221da177e4SLinus Torvalds 			ASSERT(0);
12232451337dSDave Chinner 			return -EINVAL;
12241da177e4SLinus Torvalds 		}
12251da177e4SLinus Torvalds 		mp->m_sb.sb_dblocks = lcounter;
1226014c2544SJesper Juhl 		return 0;
12271da177e4SLinus Torvalds 	case XFS_SBS_AGCOUNT:
12281da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_agcount;
12291da177e4SLinus Torvalds 		scounter += delta;
12301da177e4SLinus Torvalds 		if (scounter < 0) {
12311da177e4SLinus Torvalds 			ASSERT(0);
12322451337dSDave Chinner 			return -EINVAL;
12331da177e4SLinus Torvalds 		}
12341da177e4SLinus Torvalds 		mp->m_sb.sb_agcount = scounter;
1235014c2544SJesper Juhl 		return 0;
12361da177e4SLinus Torvalds 	case XFS_SBS_IMAX_PCT:
12371da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_imax_pct;
12381da177e4SLinus Torvalds 		scounter += delta;
12391da177e4SLinus Torvalds 		if (scounter < 0) {
12401da177e4SLinus Torvalds 			ASSERT(0);
12412451337dSDave Chinner 			return -EINVAL;
12421da177e4SLinus Torvalds 		}
12431da177e4SLinus Torvalds 		mp->m_sb.sb_imax_pct = scounter;
1244014c2544SJesper Juhl 		return 0;
12451da177e4SLinus Torvalds 	case XFS_SBS_REXTSIZE:
12461da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rextsize;
12471da177e4SLinus Torvalds 		scounter += delta;
12481da177e4SLinus Torvalds 		if (scounter < 0) {
12491da177e4SLinus Torvalds 			ASSERT(0);
12502451337dSDave Chinner 			return -EINVAL;
12511da177e4SLinus Torvalds 		}
12521da177e4SLinus Torvalds 		mp->m_sb.sb_rextsize = scounter;
1253014c2544SJesper Juhl 		return 0;
12541da177e4SLinus Torvalds 	case XFS_SBS_RBMBLOCKS:
12551da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rbmblocks;
12561da177e4SLinus Torvalds 		scounter += delta;
12571da177e4SLinus Torvalds 		if (scounter < 0) {
12581da177e4SLinus Torvalds 			ASSERT(0);
12592451337dSDave Chinner 			return -EINVAL;
12601da177e4SLinus Torvalds 		}
12611da177e4SLinus Torvalds 		mp->m_sb.sb_rbmblocks = scounter;
1262014c2544SJesper Juhl 		return 0;
12631da177e4SLinus Torvalds 	case XFS_SBS_RBLOCKS:
12641da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_rblocks;
12651da177e4SLinus Torvalds 		lcounter += delta;
12661da177e4SLinus Torvalds 		if (lcounter < 0) {
12671da177e4SLinus Torvalds 			ASSERT(0);
12682451337dSDave Chinner 			return -EINVAL;
12691da177e4SLinus Torvalds 		}
12701da177e4SLinus Torvalds 		mp->m_sb.sb_rblocks = lcounter;
1271014c2544SJesper Juhl 		return 0;
12721da177e4SLinus Torvalds 	case XFS_SBS_REXTENTS:
12731da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_rextents;
12741da177e4SLinus Torvalds 		lcounter += delta;
12751da177e4SLinus Torvalds 		if (lcounter < 0) {
12761da177e4SLinus Torvalds 			ASSERT(0);
12772451337dSDave Chinner 			return -EINVAL;
12781da177e4SLinus Torvalds 		}
12791da177e4SLinus Torvalds 		mp->m_sb.sb_rextents = lcounter;
1280014c2544SJesper Juhl 		return 0;
12811da177e4SLinus Torvalds 	case XFS_SBS_REXTSLOG:
12821da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rextslog;
12831da177e4SLinus Torvalds 		scounter += delta;
12841da177e4SLinus Torvalds 		if (scounter < 0) {
12851da177e4SLinus Torvalds 			ASSERT(0);
12862451337dSDave Chinner 			return -EINVAL;
12871da177e4SLinus Torvalds 		}
12881da177e4SLinus Torvalds 		mp->m_sb.sb_rextslog = scounter;
1289014c2544SJesper Juhl 		return 0;
12901da177e4SLinus Torvalds 	default:
12911da177e4SLinus Torvalds 		ASSERT(0);
12922451337dSDave Chinner 		return -EINVAL;
12931da177e4SLinus Torvalds 	}
12941da177e4SLinus Torvalds }
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds /*
12971da177e4SLinus Torvalds  * xfs_mod_incore_sb() is used to change a field in the in-core
12981da177e4SLinus Torvalds  * superblock structure by the specified delta.  This modification
12993685c2a1SEric Sandeen  * is protected by the m_sb_lock.  Just use the xfs_mod_incore_sb_unlocked()
13001da177e4SLinus Torvalds  * routine to do the work.
13011da177e4SLinus Torvalds  */
13021da177e4SLinus Torvalds int
130320f4ebf2SDavid Chinner xfs_mod_incore_sb(
130496540c78SChristoph Hellwig 	struct xfs_mount	*mp,
130520f4ebf2SDavid Chinner 	xfs_sb_field_t		field,
130620f4ebf2SDavid Chinner 	int64_t			delta,
130720f4ebf2SDavid Chinner 	int			rsvd)
13081da177e4SLinus Torvalds {
13091da177e4SLinus Torvalds 	int			status;
13101da177e4SLinus Torvalds 
13118d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
131296540c78SChristoph Hellwig 	ASSERT(field < XFS_SBS_ICOUNT || field > XFS_SBS_FDBLOCKS);
13138d280b98SDavid Chinner #endif
13143685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
13151da177e4SLinus Torvalds 	status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
13163685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
13178d280b98SDavid Chinner 
1318014c2544SJesper Juhl 	return status;
13191da177e4SLinus Torvalds }
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds /*
13221b040712SChristoph Hellwig  * Change more than one field in the in-core superblock structure at a time.
13231da177e4SLinus Torvalds  *
13241b040712SChristoph Hellwig  * The fields and changes to those fields are specified in the array of
13251b040712SChristoph Hellwig  * xfs_mod_sb structures passed in.  Either all of the specified deltas
13261b040712SChristoph Hellwig  * will be applied or none of them will.  If any modified field dips below 0,
13271b040712SChristoph Hellwig  * then all modifications will be backed out and EINVAL will be returned.
13281b040712SChristoph Hellwig  *
13291b040712SChristoph Hellwig  * Note that this function may not be used for the superblock values that
13301b040712SChristoph Hellwig  * are tracked with the in-memory per-cpu counters - a direct call to
13311b040712SChristoph Hellwig  * xfs_icsb_modify_counters is required for these.
13321da177e4SLinus Torvalds  */
13331da177e4SLinus Torvalds int
13341b040712SChristoph Hellwig xfs_mod_incore_sb_batch(
13351b040712SChristoph Hellwig 	struct xfs_mount	*mp,
13361b040712SChristoph Hellwig 	xfs_mod_sb_t		*msb,
13371b040712SChristoph Hellwig 	uint			nmsb,
13381b040712SChristoph Hellwig 	int			rsvd)
13391da177e4SLinus Torvalds {
134045c51b99SDavid Sterba 	xfs_mod_sb_t		*msbp;
13411b040712SChristoph Hellwig 	int			error = 0;
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	/*
13441b040712SChristoph Hellwig 	 * Loop through the array of mod structures and apply each individually.
13451b040712SChristoph Hellwig 	 * If any fail, then back out all those which have already been applied.
13461b040712SChristoph Hellwig 	 * Do all of this within the scope of the m_sb_lock so that all of the
13471b040712SChristoph Hellwig 	 * changes will be atomic.
13481da177e4SLinus Torvalds 	 */
13493685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
135045c51b99SDavid Sterba 	for (msbp = msb; msbp < (msb + nmsb); msbp++) {
13511b040712SChristoph Hellwig 		ASSERT(msbp->msb_field < XFS_SBS_ICOUNT ||
13521b040712SChristoph Hellwig 		       msbp->msb_field > XFS_SBS_FDBLOCKS);
13538d280b98SDavid Chinner 
13541b040712SChristoph Hellwig 		error = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
13551b040712SChristoph Hellwig 						   msbp->msb_delta, rsvd);
13561b040712SChristoph Hellwig 		if (error)
13571b040712SChristoph Hellwig 			goto unwind;
13581da177e4SLinus Torvalds 	}
13591b040712SChristoph Hellwig 	spin_unlock(&mp->m_sb_lock);
13601b040712SChristoph Hellwig 	return 0;
13611da177e4SLinus Torvalds 
13621b040712SChristoph Hellwig unwind:
13631b040712SChristoph Hellwig 	while (--msbp >= msb) {
13641b040712SChristoph Hellwig 		error = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
13651b040712SChristoph Hellwig 						   -msbp->msb_delta, rsvd);
13661b040712SChristoph Hellwig 		ASSERT(error == 0);
13671da177e4SLinus Torvalds 	}
13683685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
13691b040712SChristoph Hellwig 	return error;
13701da177e4SLinus Torvalds }
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds /*
13731da177e4SLinus Torvalds  * xfs_getsb() is called to obtain the buffer for the superblock.
13741da177e4SLinus Torvalds  * The buffer is returned locked and read in from disk.
13751da177e4SLinus Torvalds  * The buffer should be released with a call to xfs_brelse().
13761da177e4SLinus Torvalds  *
13771da177e4SLinus Torvalds  * If the flags parameter is BUF_TRYLOCK, then we'll only return
13781da177e4SLinus Torvalds  * the superblock buffer if it can be locked without sleeping.
13791da177e4SLinus Torvalds  * If it can't then we'll return NULL.
13801da177e4SLinus Torvalds  */
13810c842ad4SChristoph Hellwig struct xfs_buf *
13821da177e4SLinus Torvalds xfs_getsb(
13830c842ad4SChristoph Hellwig 	struct xfs_mount	*mp,
13841da177e4SLinus Torvalds 	int			flags)
13851da177e4SLinus Torvalds {
13860c842ad4SChristoph Hellwig 	struct xfs_buf		*bp = mp->m_sb_bp;
13871da177e4SLinus Torvalds 
13880c842ad4SChristoph Hellwig 	if (!xfs_buf_trylock(bp)) {
13890c842ad4SChristoph Hellwig 		if (flags & XBF_TRYLOCK)
13901da177e4SLinus Torvalds 			return NULL;
13910c842ad4SChristoph Hellwig 		xfs_buf_lock(bp);
13921da177e4SLinus Torvalds 	}
13930c842ad4SChristoph Hellwig 
139472790aa1SChandra Seetharaman 	xfs_buf_hold(bp);
13951da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISDONE(bp));
1396014c2544SJesper Juhl 	return bp;
13971da177e4SLinus Torvalds }
13981da177e4SLinus Torvalds 
13991da177e4SLinus Torvalds /*
14001da177e4SLinus Torvalds  * Used to free the superblock along various error paths.
14011da177e4SLinus Torvalds  */
14021da177e4SLinus Torvalds void
14031da177e4SLinus Torvalds xfs_freesb(
140426af6552SDave Chinner 	struct xfs_mount	*mp)
14051da177e4SLinus Torvalds {
140626af6552SDave Chinner 	struct xfs_buf		*bp = mp->m_sb_bp;
14071da177e4SLinus Torvalds 
140826af6552SDave Chinner 	xfs_buf_lock(bp);
14091da177e4SLinus Torvalds 	mp->m_sb_bp = NULL;
141026af6552SDave Chinner 	xfs_buf_relse(bp);
14111da177e4SLinus Torvalds }
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds /*
14141da177e4SLinus Torvalds  * Used to log changes to the superblock unit and width fields which could
1415e6957ea4SEric Sandeen  * be altered by the mount options, as well as any potential sb_features2
1416e6957ea4SEric Sandeen  * fixup. Only the first superblock is updated.
14171da177e4SLinus Torvalds  */
14187884bc86SChristoph Hellwig int
1419ee1c0908SDavid Chinner xfs_mount_log_sb(
14201da177e4SLinus Torvalds 	xfs_mount_t	*mp,
14211da177e4SLinus Torvalds 	__int64_t	fields)
14221da177e4SLinus Torvalds {
14231da177e4SLinus Torvalds 	xfs_trans_t	*tp;
1424e5720eecSDavid Chinner 	int		error;
14251da177e4SLinus Torvalds 
1426ee1c0908SDavid Chinner 	ASSERT(fields & (XFS_SB_UNIT | XFS_SB_WIDTH | XFS_SB_UUID |
14274b166de0SDavid Chinner 			 XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2 |
14284b166de0SDavid Chinner 			 XFS_SB_VERSIONNUM));
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds 	tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
14313d3c8b52SJie Liu 	error = xfs_trans_reserve(tp, &M_RES(mp)->tr_sb, 0, 0);
1432e5720eecSDavid Chinner 	if (error) {
14331da177e4SLinus Torvalds 		xfs_trans_cancel(tp, 0);
1434e5720eecSDavid Chinner 		return error;
14351da177e4SLinus Torvalds 	}
14361da177e4SLinus Torvalds 	xfs_mod_sb(tp, fields);
1437e5720eecSDavid Chinner 	error = xfs_trans_commit(tp, 0);
1438e5720eecSDavid Chinner 	return error;
14391da177e4SLinus Torvalds }
14408d280b98SDavid Chinner 
1441dda35b8fSChristoph Hellwig /*
1442dda35b8fSChristoph Hellwig  * If the underlying (data/log/rt) device is readonly, there are some
1443dda35b8fSChristoph Hellwig  * operations that cannot proceed.
1444dda35b8fSChristoph Hellwig  */
1445dda35b8fSChristoph Hellwig int
1446dda35b8fSChristoph Hellwig xfs_dev_is_read_only(
1447dda35b8fSChristoph Hellwig 	struct xfs_mount	*mp,
1448dda35b8fSChristoph Hellwig 	char			*message)
1449dda35b8fSChristoph Hellwig {
1450dda35b8fSChristoph Hellwig 	if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
1451dda35b8fSChristoph Hellwig 	    xfs_readonly_buftarg(mp->m_logdev_targp) ||
1452dda35b8fSChristoph Hellwig 	    (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
14530b932cccSDave Chinner 		xfs_notice(mp, "%s required on read-only device.", message);
14540b932cccSDave Chinner 		xfs_notice(mp, "write access unavailable, cannot proceed.");
14552451337dSDave Chinner 		return -EROFS;
1456dda35b8fSChristoph Hellwig 	}
1457dda35b8fSChristoph Hellwig 	return 0;
1458dda35b8fSChristoph Hellwig }
14598d280b98SDavid Chinner 
14608d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
14618d280b98SDavid Chinner /*
14628d280b98SDavid Chinner  * Per-cpu incore superblock counters
14638d280b98SDavid Chinner  *
14648d280b98SDavid Chinner  * Simple concept, difficult implementation
14658d280b98SDavid Chinner  *
14668d280b98SDavid Chinner  * Basically, replace the incore superblock counters with a distributed per cpu
14678d280b98SDavid Chinner  * counter for contended fields (e.g.  free block count).
14688d280b98SDavid Chinner  *
14698d280b98SDavid Chinner  * Difficulties arise in that the incore sb is used for ENOSPC checking, and
14708d280b98SDavid Chinner  * hence needs to be accurately read when we are running low on space. Hence
14718d280b98SDavid Chinner  * there is a method to enable and disable the per-cpu counters based on how
14728d280b98SDavid Chinner  * much "stuff" is available in them.
14738d280b98SDavid Chinner  *
14748d280b98SDavid Chinner  * Basically, a counter is enabled if there is enough free resource to justify
14758d280b98SDavid Chinner  * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
14768d280b98SDavid Chinner  * ENOSPC), then we disable the counters to synchronise all callers and
14778d280b98SDavid Chinner  * re-distribute the available resources.
14788d280b98SDavid Chinner  *
14798d280b98SDavid Chinner  * If, once we redistributed the available resources, we still get a failure,
14808d280b98SDavid Chinner  * we disable the per-cpu counter and go through the slow path.
14818d280b98SDavid Chinner  *
14828d280b98SDavid Chinner  * The slow path is the current xfs_mod_incore_sb() function.  This means that
14839da096fdSMalcolm Parsons  * when we disable a per-cpu counter, we need to drain its resources back to
14848d280b98SDavid Chinner  * the global superblock. We do this after disabling the counter to prevent
14858d280b98SDavid Chinner  * more threads from queueing up on the counter.
14868d280b98SDavid Chinner  *
14878d280b98SDavid Chinner  * Essentially, this means that we still need a lock in the fast path to enable
14888d280b98SDavid Chinner  * synchronisation between the global counters and the per-cpu counters. This
14898d280b98SDavid Chinner  * is not a problem because the lock will be local to a CPU almost all the time
14908d280b98SDavid Chinner  * and have little contention except when we get to ENOSPC conditions.
14918d280b98SDavid Chinner  *
14928d280b98SDavid Chinner  * Basically, this lock becomes a barrier that enables us to lock out the fast
14938d280b98SDavid Chinner  * path while we do things like enabling and disabling counters and
14948d280b98SDavid Chinner  * synchronising the counters.
14958d280b98SDavid Chinner  *
14968d280b98SDavid Chinner  * Locking rules:
14978d280b98SDavid Chinner  *
14983685c2a1SEric Sandeen  * 	1. m_sb_lock before picking up per-cpu locks
14998d280b98SDavid Chinner  * 	2. per-cpu locks always picked up via for_each_online_cpu() order
15003685c2a1SEric Sandeen  * 	3. accurate counter sync requires m_sb_lock + per cpu locks
15018d280b98SDavid Chinner  * 	4. modifying per-cpu counters requires holding per-cpu lock
15023685c2a1SEric Sandeen  * 	5. modifying global counters requires holding m_sb_lock
15033685c2a1SEric Sandeen  *	6. enabling or disabling a counter requires holding the m_sb_lock
15048d280b98SDavid Chinner  *	   and _none_ of the per-cpu locks.
15058d280b98SDavid Chinner  *
15068d280b98SDavid Chinner  * Disabled counters are only ever re-enabled by a balance operation
15078d280b98SDavid Chinner  * that results in more free resources per CPU than a given threshold.
15088d280b98SDavid Chinner  * To ensure counters don't remain disabled, they are rebalanced when
15098d280b98SDavid Chinner  * the global resource goes above a higher threshold (i.e. some hysteresis
15108d280b98SDavid Chinner  * is present to prevent thrashing).
15118d280b98SDavid Chinner  */
1512e8234a68SDavid Chinner 
15135a67e4c5SChandra Seetharaman #ifdef CONFIG_HOTPLUG_CPU
1514e8234a68SDavid Chinner /*
1515e8234a68SDavid Chinner  * hot-plug CPU notifier support.
1516e8234a68SDavid Chinner  *
15175a67e4c5SChandra Seetharaman  * We need a notifier per filesystem as we need to be able to identify
15185a67e4c5SChandra Seetharaman  * the filesystem to balance the counters out. This is achieved by
15195a67e4c5SChandra Seetharaman  * having a notifier block embedded in the xfs_mount_t and doing pointer
15205a67e4c5SChandra Seetharaman  * magic to get the mount pointer from the notifier block address.
1521e8234a68SDavid Chinner  */
1522e8234a68SDavid Chinner STATIC int
1523e8234a68SDavid Chinner xfs_icsb_cpu_notify(
1524e8234a68SDavid Chinner 	struct notifier_block *nfb,
1525e8234a68SDavid Chinner 	unsigned long action,
1526e8234a68SDavid Chinner 	void *hcpu)
1527e8234a68SDavid Chinner {
1528e8234a68SDavid Chinner 	xfs_icsb_cnts_t *cntp;
1529e8234a68SDavid Chinner 	xfs_mount_t	*mp;
1530e8234a68SDavid Chinner 
1531e8234a68SDavid Chinner 	mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
1532e8234a68SDavid Chinner 	cntp = (xfs_icsb_cnts_t *)
1533e8234a68SDavid Chinner 			per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
1534e8234a68SDavid Chinner 	switch (action) {
1535e8234a68SDavid Chinner 	case CPU_UP_PREPARE:
15368bb78442SRafael J. Wysocki 	case CPU_UP_PREPARE_FROZEN:
1537e8234a68SDavid Chinner 		/* Easy Case - initialize the area and locks, and
1538e8234a68SDavid Chinner 		 * then rebalance when online does everything else for us. */
153901e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
1540e8234a68SDavid Chinner 		break;
1541e8234a68SDavid Chinner 	case CPU_ONLINE:
15428bb78442SRafael J. Wysocki 	case CPU_ONLINE_FROZEN:
154303135cf7SDavid Chinner 		xfs_icsb_lock(mp);
154445af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
154545af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
154645af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
154703135cf7SDavid Chinner 		xfs_icsb_unlock(mp);
1548e8234a68SDavid Chinner 		break;
1549e8234a68SDavid Chinner 	case CPU_DEAD:
15508bb78442SRafael J. Wysocki 	case CPU_DEAD_FROZEN:
1551e8234a68SDavid Chinner 		/* Disable all the counters, then fold the dead cpu's
1552e8234a68SDavid Chinner 		 * count into the total on the global superblock and
1553e8234a68SDavid Chinner 		 * re-enable the counters. */
155403135cf7SDavid Chinner 		xfs_icsb_lock(mp);
15553685c2a1SEric Sandeen 		spin_lock(&mp->m_sb_lock);
1556e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
1557e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
1558e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
1559e8234a68SDavid Chinner 
1560e8234a68SDavid Chinner 		mp->m_sb.sb_icount += cntp->icsb_icount;
1561e8234a68SDavid Chinner 		mp->m_sb.sb_ifree += cntp->icsb_ifree;
1562e8234a68SDavid Chinner 		mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
1563e8234a68SDavid Chinner 
156401e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
1565e8234a68SDavid Chinner 
156645af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter_locked(mp, XFS_SBS_ICOUNT, 0);
156745af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter_locked(mp, XFS_SBS_IFREE, 0);
156845af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter_locked(mp, XFS_SBS_FDBLOCKS, 0);
15693685c2a1SEric Sandeen 		spin_unlock(&mp->m_sb_lock);
157003135cf7SDavid Chinner 		xfs_icsb_unlock(mp);
1571e8234a68SDavid Chinner 		break;
1572e8234a68SDavid Chinner 	}
1573e8234a68SDavid Chinner 
1574e8234a68SDavid Chinner 	return NOTIFY_OK;
1575e8234a68SDavid Chinner }
15765a67e4c5SChandra Seetharaman #endif /* CONFIG_HOTPLUG_CPU */
1577e8234a68SDavid Chinner 
15788d280b98SDavid Chinner int
15798d280b98SDavid Chinner xfs_icsb_init_counters(
15808d280b98SDavid Chinner 	xfs_mount_t	*mp)
15818d280b98SDavid Chinner {
15828d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
15838d280b98SDavid Chinner 	int		i;
15848d280b98SDavid Chinner 
15858d280b98SDavid Chinner 	mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
15868d280b98SDavid Chinner 	if (mp->m_sb_cnts == NULL)
15878d280b98SDavid Chinner 		return -ENOMEM;
15888d280b98SDavid Chinner 
15898d280b98SDavid Chinner 	for_each_online_cpu(i) {
15908d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
159101e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
15928d280b98SDavid Chinner 	}
159320b64285SDavid Chinner 
159420b64285SDavid Chinner 	mutex_init(&mp->m_icsb_mutex);
159520b64285SDavid Chinner 
15968d280b98SDavid Chinner 	/*
15978d280b98SDavid Chinner 	 * start with all counters disabled so that the
15988d280b98SDavid Chinner 	 * initial balance kicks us off correctly
15998d280b98SDavid Chinner 	 */
16008d280b98SDavid Chinner 	mp->m_icsb_counters = -1;
160146677e67SRichard Weinberger 
160246677e67SRichard Weinberger #ifdef CONFIG_HOTPLUG_CPU
160346677e67SRichard Weinberger 	mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
160446677e67SRichard Weinberger 	mp->m_icsb_notifier.priority = 0;
160546677e67SRichard Weinberger 	register_hotcpu_notifier(&mp->m_icsb_notifier);
160646677e67SRichard Weinberger #endif /* CONFIG_HOTPLUG_CPU */
160746677e67SRichard Weinberger 
16088d280b98SDavid Chinner 	return 0;
16098d280b98SDavid Chinner }
16108d280b98SDavid Chinner 
16115478eeadSLachlan McIlroy void
16125478eeadSLachlan McIlroy xfs_icsb_reinit_counters(
16135478eeadSLachlan McIlroy 	xfs_mount_t	*mp)
16145478eeadSLachlan McIlroy {
16155478eeadSLachlan McIlroy 	xfs_icsb_lock(mp);
16165478eeadSLachlan McIlroy 	/*
16175478eeadSLachlan McIlroy 	 * start with all counters disabled so that the
16185478eeadSLachlan McIlroy 	 * initial balance kicks us off correctly
16195478eeadSLachlan McIlroy 	 */
16205478eeadSLachlan McIlroy 	mp->m_icsb_counters = -1;
162145af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
162245af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
162345af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
16245478eeadSLachlan McIlroy 	xfs_icsb_unlock(mp);
16255478eeadSLachlan McIlroy }
16265478eeadSLachlan McIlroy 
1627c962fb79SChristoph Hellwig void
16288d280b98SDavid Chinner xfs_icsb_destroy_counters(
16298d280b98SDavid Chinner 	xfs_mount_t	*mp)
16308d280b98SDavid Chinner {
1631e8234a68SDavid Chinner 	if (mp->m_sb_cnts) {
16325a67e4c5SChandra Seetharaman 		unregister_hotcpu_notifier(&mp->m_icsb_notifier);
16338d280b98SDavid Chinner 		free_percpu(mp->m_sb_cnts);
16348d280b98SDavid Chinner 	}
163503135cf7SDavid Chinner 	mutex_destroy(&mp->m_icsb_mutex);
1636e8234a68SDavid Chinner }
16378d280b98SDavid Chinner 
1638b8f82a4aSChristoph Hellwig STATIC void
163901e1b69cSDavid Chinner xfs_icsb_lock_cntr(
164001e1b69cSDavid Chinner 	xfs_icsb_cnts_t	*icsbp)
164101e1b69cSDavid Chinner {
164201e1b69cSDavid Chinner 	while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
164301e1b69cSDavid Chinner 		ndelay(1000);
164401e1b69cSDavid Chinner 	}
164501e1b69cSDavid Chinner }
164601e1b69cSDavid Chinner 
1647b8f82a4aSChristoph Hellwig STATIC void
164801e1b69cSDavid Chinner xfs_icsb_unlock_cntr(
164901e1b69cSDavid Chinner 	xfs_icsb_cnts_t	*icsbp)
165001e1b69cSDavid Chinner {
165101e1b69cSDavid Chinner 	clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
165201e1b69cSDavid Chinner }
165301e1b69cSDavid Chinner 
16548d280b98SDavid Chinner 
1655b8f82a4aSChristoph Hellwig STATIC void
16568d280b98SDavid Chinner xfs_icsb_lock_all_counters(
16578d280b98SDavid Chinner 	xfs_mount_t	*mp)
16588d280b98SDavid Chinner {
16598d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
16608d280b98SDavid Chinner 	int		i;
16618d280b98SDavid Chinner 
16628d280b98SDavid Chinner 	for_each_online_cpu(i) {
16638d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
166401e1b69cSDavid Chinner 		xfs_icsb_lock_cntr(cntp);
16658d280b98SDavid Chinner 	}
16668d280b98SDavid Chinner }
16678d280b98SDavid Chinner 
1668b8f82a4aSChristoph Hellwig STATIC void
16698d280b98SDavid Chinner xfs_icsb_unlock_all_counters(
16708d280b98SDavid Chinner 	xfs_mount_t	*mp)
16718d280b98SDavid Chinner {
16728d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
16738d280b98SDavid Chinner 	int		i;
16748d280b98SDavid Chinner 
16758d280b98SDavid Chinner 	for_each_online_cpu(i) {
16768d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
167701e1b69cSDavid Chinner 		xfs_icsb_unlock_cntr(cntp);
16788d280b98SDavid Chinner 	}
16798d280b98SDavid Chinner }
16808d280b98SDavid Chinner 
16818d280b98SDavid Chinner STATIC void
16828d280b98SDavid Chinner xfs_icsb_count(
16838d280b98SDavid Chinner 	xfs_mount_t	*mp,
16848d280b98SDavid Chinner 	xfs_icsb_cnts_t	*cnt,
16858d280b98SDavid Chinner 	int		flags)
16868d280b98SDavid Chinner {
16878d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
16888d280b98SDavid Chinner 	int		i;
16898d280b98SDavid Chinner 
16908d280b98SDavid Chinner 	memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
16918d280b98SDavid Chinner 
16928d280b98SDavid Chinner 	if (!(flags & XFS_ICSB_LAZY_COUNT))
16938d280b98SDavid Chinner 		xfs_icsb_lock_all_counters(mp);
16948d280b98SDavid Chinner 
16958d280b98SDavid Chinner 	for_each_online_cpu(i) {
16968d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
16978d280b98SDavid Chinner 		cnt->icsb_icount += cntp->icsb_icount;
16988d280b98SDavid Chinner 		cnt->icsb_ifree += cntp->icsb_ifree;
16998d280b98SDavid Chinner 		cnt->icsb_fdblocks += cntp->icsb_fdblocks;
17008d280b98SDavid Chinner 	}
17018d280b98SDavid Chinner 
17028d280b98SDavid Chinner 	if (!(flags & XFS_ICSB_LAZY_COUNT))
17038d280b98SDavid Chinner 		xfs_icsb_unlock_all_counters(mp);
17048d280b98SDavid Chinner }
17058d280b98SDavid Chinner 
17068d280b98SDavid Chinner STATIC int
17078d280b98SDavid Chinner xfs_icsb_counter_disabled(
17088d280b98SDavid Chinner 	xfs_mount_t	*mp,
17098d280b98SDavid Chinner 	xfs_sb_field_t	field)
17108d280b98SDavid Chinner {
17118d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
17128d280b98SDavid Chinner 	return test_bit(field, &mp->m_icsb_counters);
17138d280b98SDavid Chinner }
17148d280b98SDavid Chinner 
171536fbe6e6SDavid Chinner STATIC void
17168d280b98SDavid Chinner xfs_icsb_disable_counter(
17178d280b98SDavid Chinner 	xfs_mount_t	*mp,
17188d280b98SDavid Chinner 	xfs_sb_field_t	field)
17198d280b98SDavid Chinner {
17208d280b98SDavid Chinner 	xfs_icsb_cnts_t	cnt;
17218d280b98SDavid Chinner 
17228d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
17238d280b98SDavid Chinner 
172420b64285SDavid Chinner 	/*
172520b64285SDavid Chinner 	 * If we are already disabled, then there is nothing to do
172620b64285SDavid Chinner 	 * here. We check before locking all the counters to avoid
172720b64285SDavid Chinner 	 * the expensive lock operation when being called in the
172820b64285SDavid Chinner 	 * slow path and the counter is already disabled. This is
172920b64285SDavid Chinner 	 * safe because the only time we set or clear this state is under
173020b64285SDavid Chinner 	 * the m_icsb_mutex.
173120b64285SDavid Chinner 	 */
173220b64285SDavid Chinner 	if (xfs_icsb_counter_disabled(mp, field))
173336fbe6e6SDavid Chinner 		return;
173420b64285SDavid Chinner 
17358d280b98SDavid Chinner 	xfs_icsb_lock_all_counters(mp);
17368d280b98SDavid Chinner 	if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
17378d280b98SDavid Chinner 		/* drain back to superblock */
17388d280b98SDavid Chinner 
1739ce46193bSChristoph Hellwig 		xfs_icsb_count(mp, &cnt, XFS_ICSB_LAZY_COUNT);
17408d280b98SDavid Chinner 		switch(field) {
17418d280b98SDavid Chinner 		case XFS_SBS_ICOUNT:
17428d280b98SDavid Chinner 			mp->m_sb.sb_icount = cnt.icsb_icount;
17438d280b98SDavid Chinner 			break;
17448d280b98SDavid Chinner 		case XFS_SBS_IFREE:
17458d280b98SDavid Chinner 			mp->m_sb.sb_ifree = cnt.icsb_ifree;
17468d280b98SDavid Chinner 			break;
17478d280b98SDavid Chinner 		case XFS_SBS_FDBLOCKS:
17488d280b98SDavid Chinner 			mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
17498d280b98SDavid Chinner 			break;
17508d280b98SDavid Chinner 		default:
17518d280b98SDavid Chinner 			BUG();
17528d280b98SDavid Chinner 		}
17538d280b98SDavid Chinner 	}
17548d280b98SDavid Chinner 
17558d280b98SDavid Chinner 	xfs_icsb_unlock_all_counters(mp);
17568d280b98SDavid Chinner }
17578d280b98SDavid Chinner 
17588d280b98SDavid Chinner STATIC void
17598d280b98SDavid Chinner xfs_icsb_enable_counter(
17608d280b98SDavid Chinner 	xfs_mount_t	*mp,
17618d280b98SDavid Chinner 	xfs_sb_field_t	field,
17628d280b98SDavid Chinner 	uint64_t	count,
17638d280b98SDavid Chinner 	uint64_t	resid)
17648d280b98SDavid Chinner {
17658d280b98SDavid Chinner 	xfs_icsb_cnts_t	*cntp;
17668d280b98SDavid Chinner 	int		i;
17678d280b98SDavid Chinner 
17688d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
17698d280b98SDavid Chinner 
17708d280b98SDavid Chinner 	xfs_icsb_lock_all_counters(mp);
17718d280b98SDavid Chinner 	for_each_online_cpu(i) {
17728d280b98SDavid Chinner 		cntp = per_cpu_ptr(mp->m_sb_cnts, i);
17738d280b98SDavid Chinner 		switch (field) {
17748d280b98SDavid Chinner 		case XFS_SBS_ICOUNT:
17758d280b98SDavid Chinner 			cntp->icsb_icount = count + resid;
17768d280b98SDavid Chinner 			break;
17778d280b98SDavid Chinner 		case XFS_SBS_IFREE:
17788d280b98SDavid Chinner 			cntp->icsb_ifree = count + resid;
17798d280b98SDavid Chinner 			break;
17808d280b98SDavid Chinner 		case XFS_SBS_FDBLOCKS:
17818d280b98SDavid Chinner 			cntp->icsb_fdblocks = count + resid;
17828d280b98SDavid Chinner 			break;
17838d280b98SDavid Chinner 		default:
17848d280b98SDavid Chinner 			BUG();
17858d280b98SDavid Chinner 			break;
17868d280b98SDavid Chinner 		}
17878d280b98SDavid Chinner 		resid = 0;
17888d280b98SDavid Chinner 	}
17898d280b98SDavid Chinner 	clear_bit(field, &mp->m_icsb_counters);
17908d280b98SDavid Chinner 	xfs_icsb_unlock_all_counters(mp);
17918d280b98SDavid Chinner }
17928d280b98SDavid Chinner 
1793dbcabad1SDavid Chinner void
1794d4d90b57SChristoph Hellwig xfs_icsb_sync_counters_locked(
17958d280b98SDavid Chinner 	xfs_mount_t	*mp,
17968d280b98SDavid Chinner 	int		flags)
17978d280b98SDavid Chinner {
17988d280b98SDavid Chinner 	xfs_icsb_cnts_t	cnt;
17998d280b98SDavid Chinner 
18008d280b98SDavid Chinner 	xfs_icsb_count(mp, &cnt, flags);
18018d280b98SDavid Chinner 
18028d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
18038d280b98SDavid Chinner 		mp->m_sb.sb_icount = cnt.icsb_icount;
18048d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
18058d280b98SDavid Chinner 		mp->m_sb.sb_ifree = cnt.icsb_ifree;
18068d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
18078d280b98SDavid Chinner 		mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
18088d280b98SDavid Chinner }
18098d280b98SDavid Chinner 
18108d280b98SDavid Chinner /*
18118d280b98SDavid Chinner  * Accurate update of per-cpu counters to incore superblock
18128d280b98SDavid Chinner  */
1813d4d90b57SChristoph Hellwig void
18148d280b98SDavid Chinner xfs_icsb_sync_counters(
1815d4d90b57SChristoph Hellwig 	xfs_mount_t	*mp,
1816d4d90b57SChristoph Hellwig 	int		flags)
18178d280b98SDavid Chinner {
1818d4d90b57SChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
1819d4d90b57SChristoph Hellwig 	xfs_icsb_sync_counters_locked(mp, flags);
1820d4d90b57SChristoph Hellwig 	spin_unlock(&mp->m_sb_lock);
18218d280b98SDavid Chinner }
18228d280b98SDavid Chinner 
18238d280b98SDavid Chinner /*
18248d280b98SDavid Chinner  * Balance and enable/disable counters as necessary.
18258d280b98SDavid Chinner  *
182620b64285SDavid Chinner  * Thresholds for re-enabling counters are somewhat magic.  inode counts are
182720b64285SDavid Chinner  * chosen to be the same number as single on disk allocation chunk per CPU, and
182820b64285SDavid Chinner  * free blocks is something far enough zero that we aren't going thrash when we
182920b64285SDavid Chinner  * get near ENOSPC. We also need to supply a minimum we require per cpu to
183020b64285SDavid Chinner  * prevent looping endlessly when xfs_alloc_space asks for more than will
183120b64285SDavid Chinner  * be distributed to a single CPU but each CPU has enough blocks to be
183220b64285SDavid Chinner  * reenabled.
183320b64285SDavid Chinner  *
183420b64285SDavid Chinner  * Note that we can be called when counters are already disabled.
183520b64285SDavid Chinner  * xfs_icsb_disable_counter() optimises the counter locking in this case to
183620b64285SDavid Chinner  * prevent locking every per-cpu counter needlessly.
18378d280b98SDavid Chinner  */
183820b64285SDavid Chinner 
183920b64285SDavid Chinner #define XFS_ICSB_INO_CNTR_REENABLE	(uint64_t)64
18404be536deSDavid Chinner #define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \
184120b64285SDavid Chinner 		(uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp))
18428d280b98SDavid Chinner STATIC void
184345af6c6dSChristoph Hellwig xfs_icsb_balance_counter_locked(
18448d280b98SDavid Chinner 	xfs_mount_t	*mp,
18458d280b98SDavid Chinner 	xfs_sb_field_t  field,
184620b64285SDavid Chinner 	int		min_per_cpu)
18478d280b98SDavid Chinner {
18486fdf8cccSNathan Scott 	uint64_t	count, resid;
18498d280b98SDavid Chinner 	int		weight = num_online_cpus();
185020b64285SDavid Chinner 	uint64_t	min = (uint64_t)min_per_cpu;
18518d280b98SDavid Chinner 
18528d280b98SDavid Chinner 	/* disable counter and sync counter */
18538d280b98SDavid Chinner 	xfs_icsb_disable_counter(mp, field);
18548d280b98SDavid Chinner 
18558d280b98SDavid Chinner 	/* update counters  - first CPU gets residual*/
18568d280b98SDavid Chinner 	switch (field) {
18578d280b98SDavid Chinner 	case XFS_SBS_ICOUNT:
18588d280b98SDavid Chinner 		count = mp->m_sb.sb_icount;
18598d280b98SDavid Chinner 		resid = do_div(count, weight);
186020b64285SDavid Chinner 		if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
186145af6c6dSChristoph Hellwig 			return;
18628d280b98SDavid Chinner 		break;
18638d280b98SDavid Chinner 	case XFS_SBS_IFREE:
18648d280b98SDavid Chinner 		count = mp->m_sb.sb_ifree;
18658d280b98SDavid Chinner 		resid = do_div(count, weight);
186620b64285SDavid Chinner 		if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
186745af6c6dSChristoph Hellwig 			return;
18688d280b98SDavid Chinner 		break;
18698d280b98SDavid Chinner 	case XFS_SBS_FDBLOCKS:
18708d280b98SDavid Chinner 		count = mp->m_sb.sb_fdblocks;
18718d280b98SDavid Chinner 		resid = do_div(count, weight);
187220b64285SDavid Chinner 		if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp)))
187345af6c6dSChristoph Hellwig 			return;
18748d280b98SDavid Chinner 		break;
18758d280b98SDavid Chinner 	default:
18768d280b98SDavid Chinner 		BUG();
18776fdf8cccSNathan Scott 		count = resid = 0;	/* quiet, gcc */
18788d280b98SDavid Chinner 		break;
18798d280b98SDavid Chinner 	}
18808d280b98SDavid Chinner 
18818d280b98SDavid Chinner 	xfs_icsb_enable_counter(mp, field, count, resid);
188245af6c6dSChristoph Hellwig }
188345af6c6dSChristoph Hellwig 
188445af6c6dSChristoph Hellwig STATIC void
188545af6c6dSChristoph Hellwig xfs_icsb_balance_counter(
188645af6c6dSChristoph Hellwig 	xfs_mount_t	*mp,
188745af6c6dSChristoph Hellwig 	xfs_sb_field_t  fields,
188845af6c6dSChristoph Hellwig 	int		min_per_cpu)
188945af6c6dSChristoph Hellwig {
189045af6c6dSChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
189145af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter_locked(mp, fields, min_per_cpu);
18923685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
18938d280b98SDavid Chinner }
18948d280b98SDavid Chinner 
18951b040712SChristoph Hellwig int
189620b64285SDavid Chinner xfs_icsb_modify_counters(
18978d280b98SDavid Chinner 	xfs_mount_t	*mp,
18988d280b98SDavid Chinner 	xfs_sb_field_t	field,
189920f4ebf2SDavid Chinner 	int64_t		delta,
190020b64285SDavid Chinner 	int		rsvd)
19018d280b98SDavid Chinner {
19028d280b98SDavid Chinner 	xfs_icsb_cnts_t	*icsbp;
19038d280b98SDavid Chinner 	long long	lcounter;	/* long counter for 64 bit fields */
19047a9e02d6SChristoph Lameter 	int		ret = 0;
19058d280b98SDavid Chinner 
190620b64285SDavid Chinner 	might_sleep();
19078d280b98SDavid Chinner again:
19087a9e02d6SChristoph Lameter 	preempt_disable();
19097a9e02d6SChristoph Lameter 	icsbp = this_cpu_ptr(mp->m_sb_cnts);
191020b64285SDavid Chinner 
191120b64285SDavid Chinner 	/*
191220b64285SDavid Chinner 	 * if the counter is disabled, go to slow path
191320b64285SDavid Chinner 	 */
19148d280b98SDavid Chinner 	if (unlikely(xfs_icsb_counter_disabled(mp, field)))
19158d280b98SDavid Chinner 		goto slow_path;
191620b64285SDavid Chinner 	xfs_icsb_lock_cntr(icsbp);
191720b64285SDavid Chinner 	if (unlikely(xfs_icsb_counter_disabled(mp, field))) {
191820b64285SDavid Chinner 		xfs_icsb_unlock_cntr(icsbp);
191920b64285SDavid Chinner 		goto slow_path;
192020b64285SDavid Chinner 	}
19218d280b98SDavid Chinner 
19228d280b98SDavid Chinner 	switch (field) {
19238d280b98SDavid Chinner 	case XFS_SBS_ICOUNT:
19248d280b98SDavid Chinner 		lcounter = icsbp->icsb_icount;
19258d280b98SDavid Chinner 		lcounter += delta;
19268d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
192720b64285SDavid Chinner 			goto balance_counter;
19288d280b98SDavid Chinner 		icsbp->icsb_icount = lcounter;
19298d280b98SDavid Chinner 		break;
19308d280b98SDavid Chinner 
19318d280b98SDavid Chinner 	case XFS_SBS_IFREE:
19328d280b98SDavid Chinner 		lcounter = icsbp->icsb_ifree;
19338d280b98SDavid Chinner 		lcounter += delta;
19348d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
193520b64285SDavid Chinner 			goto balance_counter;
19368d280b98SDavid Chinner 		icsbp->icsb_ifree = lcounter;
19378d280b98SDavid Chinner 		break;
19388d280b98SDavid Chinner 
19398d280b98SDavid Chinner 	case XFS_SBS_FDBLOCKS:
19408d280b98SDavid Chinner 		BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
19418d280b98SDavid Chinner 
19424be536deSDavid Chinner 		lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
19438d280b98SDavid Chinner 		lcounter += delta;
19448d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
194520b64285SDavid Chinner 			goto balance_counter;
19464be536deSDavid Chinner 		icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
19478d280b98SDavid Chinner 		break;
19488d280b98SDavid Chinner 	default:
19498d280b98SDavid Chinner 		BUG();
19508d280b98SDavid Chinner 		break;
19518d280b98SDavid Chinner 	}
195201e1b69cSDavid Chinner 	xfs_icsb_unlock_cntr(icsbp);
19537a9e02d6SChristoph Lameter 	preempt_enable();
19548d280b98SDavid Chinner 	return 0;
19558d280b98SDavid Chinner 
19568d280b98SDavid Chinner slow_path:
19577a9e02d6SChristoph Lameter 	preempt_enable();
195820b64285SDavid Chinner 
195920b64285SDavid Chinner 	/*
196020b64285SDavid Chinner 	 * serialise with a mutex so we don't burn lots of cpu on
196120b64285SDavid Chinner 	 * the superblock lock. We still need to hold the superblock
196220b64285SDavid Chinner 	 * lock, however, when we modify the global structures.
196320b64285SDavid Chinner 	 */
196403135cf7SDavid Chinner 	xfs_icsb_lock(mp);
196520b64285SDavid Chinner 
196620b64285SDavid Chinner 	/*
196720b64285SDavid Chinner 	 * Now running atomically.
196820b64285SDavid Chinner 	 *
196920b64285SDavid Chinner 	 * If the counter is enabled, someone has beaten us to rebalancing.
197020b64285SDavid Chinner 	 * Drop the lock and try again in the fast path....
197120b64285SDavid Chinner 	 */
197220b64285SDavid Chinner 	if (!(xfs_icsb_counter_disabled(mp, field))) {
197303135cf7SDavid Chinner 		xfs_icsb_unlock(mp);
197420b64285SDavid Chinner 		goto again;
197520b64285SDavid Chinner 	}
197620b64285SDavid Chinner 
197720b64285SDavid Chinner 	/*
197820b64285SDavid Chinner 	 * The counter is currently disabled. Because we are
197920b64285SDavid Chinner 	 * running atomically here, we know a rebalance cannot
198020b64285SDavid Chinner 	 * be in progress. Hence we can go straight to operating
198120b64285SDavid Chinner 	 * on the global superblock. We do not call xfs_mod_incore_sb()
19823685c2a1SEric Sandeen 	 * here even though we need to get the m_sb_lock. Doing so
198320b64285SDavid Chinner 	 * will cause us to re-enter this function and deadlock.
19843685c2a1SEric Sandeen 	 * Hence we get the m_sb_lock ourselves and then call
198520b64285SDavid Chinner 	 * xfs_mod_incore_sb_unlocked() as the unlocked path operates
198620b64285SDavid Chinner 	 * directly on the global counters.
198720b64285SDavid Chinner 	 */
19883685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
198920b64285SDavid Chinner 	ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
19903685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
199120b64285SDavid Chinner 
199220b64285SDavid Chinner 	/*
199320b64285SDavid Chinner 	 * Now that we've modified the global superblock, we
199420b64285SDavid Chinner 	 * may be able to re-enable the distributed counters
199520b64285SDavid Chinner 	 * (e.g. lots of space just got freed). After that
199620b64285SDavid Chinner 	 * we are done.
199720b64285SDavid Chinner 	 */
19982451337dSDave Chinner 	if (ret != -ENOSPC)
199945af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, field, 0);
200003135cf7SDavid Chinner 	xfs_icsb_unlock(mp);
200120b64285SDavid Chinner 	return ret;
200220b64285SDavid Chinner 
200320b64285SDavid Chinner balance_counter:
200401e1b69cSDavid Chinner 	xfs_icsb_unlock_cntr(icsbp);
20057a9e02d6SChristoph Lameter 	preempt_enable();
20068d280b98SDavid Chinner 
200720b64285SDavid Chinner 	/*
200820b64285SDavid Chinner 	 * We may have multiple threads here if multiple per-cpu
200920b64285SDavid Chinner 	 * counters run dry at the same time. This will mean we can
201020b64285SDavid Chinner 	 * do more balances than strictly necessary but it is not
201120b64285SDavid Chinner 	 * the common slowpath case.
201220b64285SDavid Chinner 	 */
201303135cf7SDavid Chinner 	xfs_icsb_lock(mp);
201420b64285SDavid Chinner 
201520b64285SDavid Chinner 	/*
201620b64285SDavid Chinner 	 * running atomically.
201720b64285SDavid Chinner 	 *
201820b64285SDavid Chinner 	 * This will leave the counter in the correct state for future
201920b64285SDavid Chinner 	 * accesses. After the rebalance, we simply try again and our retry
202020b64285SDavid Chinner 	 * will either succeed through the fast path or slow path without
202120b64285SDavid Chinner 	 * another balance operation being required.
202220b64285SDavid Chinner 	 */
202345af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, field, delta);
202403135cf7SDavid Chinner 	xfs_icsb_unlock(mp);
20258d280b98SDavid Chinner 	goto again;
20268d280b98SDavid Chinner }
20278d280b98SDavid Chinner 
20288d280b98SDavid Chinner #endif
2029