xref: /linux/fs/xfs/xfs_mount.c (revision 239880ef6454ccff2ba8d762c3f86e8278f0ce1c)
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"
21*239880efSDave Chinner #include "xfs_format.h"
22*239880efSDave Chinner #include "xfs_log_format.h"
23*239880efSDave 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"
30*239880efSDave Chinner #include "xfs_trans.h"
31*239880efSDave Chinner #include "xfs_trans_priv.h"
32*239880efSDave Chinner #include "xfs_log.h"
332b9ab5abSDave Chinner #include "xfs_dir2.h"
341da177e4SLinus Torvalds #include "xfs_bmap_btree.h"
35a844f451SNathan Scott #include "xfs_alloc_btree.h"
361da177e4SLinus Torvalds #include "xfs_ialloc_btree.h"
371da177e4SLinus Torvalds #include "xfs_dinode.h"
381da177e4SLinus Torvalds #include "xfs_inode.h"
39a844f451SNathan Scott #include "xfs_btree.h"
40a844f451SNathan Scott #include "xfs_ialloc.h"
411da177e4SLinus Torvalds #include "xfs_alloc.h"
421da177e4SLinus Torvalds #include "xfs_rtalloc.h"
431da177e4SLinus Torvalds #include "xfs_bmap.h"
441da177e4SLinus Torvalds #include "xfs_error.h"
451da177e4SLinus Torvalds #include "xfs_quota.h"
461da177e4SLinus Torvalds #include "xfs_fsops.h"
470b1b213fSChristoph Hellwig #include "xfs_trace.h"
486d8b79cfSDave Chinner #include "xfs_icache.h"
4904a1e6c5SDave Chinner #include "xfs_cksum.h"
5004a1e6c5SDave Chinner #include "xfs_buf_item.h"
510b1b213fSChristoph Hellwig 
521da177e4SLinus Torvalds 
538d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
5420f4ebf2SDavid Chinner STATIC void	xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t,
5545af6c6dSChristoph Hellwig 						int);
5645af6c6dSChristoph Hellwig STATIC void	xfs_icsb_balance_counter_locked(xfs_mount_t *, xfs_sb_field_t,
5745af6c6dSChristoph Hellwig 						int);
5836fbe6e6SDavid Chinner STATIC void	xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
598d280b98SDavid Chinner #else
608d280b98SDavid Chinner 
6145af6c6dSChristoph Hellwig #define xfs_icsb_balance_counter(mp, a, b)		do { } while (0)
6245af6c6dSChristoph Hellwig #define xfs_icsb_balance_counter_locked(mp, a, b)	do { } while (0)
638d280b98SDavid Chinner #endif
648d280b98SDavid Chinner 
6527174203SChristoph Hellwig static DEFINE_MUTEX(xfs_uuid_table_mutex);
6627174203SChristoph Hellwig static int xfs_uuid_table_size;
6727174203SChristoph Hellwig static uuid_t *xfs_uuid_table;
6827174203SChristoph Hellwig 
6927174203SChristoph Hellwig /*
7027174203SChristoph Hellwig  * See if the UUID is unique among mounted XFS filesystems.
7127174203SChristoph Hellwig  * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
7227174203SChristoph Hellwig  */
7327174203SChristoph Hellwig STATIC int
7427174203SChristoph Hellwig xfs_uuid_mount(
7527174203SChristoph Hellwig 	struct xfs_mount	*mp)
7627174203SChristoph Hellwig {
7727174203SChristoph Hellwig 	uuid_t			*uuid = &mp->m_sb.sb_uuid;
7827174203SChristoph Hellwig 	int			hole, i;
7927174203SChristoph Hellwig 
8027174203SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_NOUUID)
8127174203SChristoph Hellwig 		return 0;
8227174203SChristoph Hellwig 
8327174203SChristoph Hellwig 	if (uuid_is_nil(uuid)) {
840b932cccSDave Chinner 		xfs_warn(mp, "Filesystem has nil UUID - can't mount");
8527174203SChristoph Hellwig 		return XFS_ERROR(EINVAL);
8627174203SChristoph Hellwig 	}
8727174203SChristoph Hellwig 
8827174203SChristoph Hellwig 	mutex_lock(&xfs_uuid_table_mutex);
8927174203SChristoph Hellwig 	for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) {
9027174203SChristoph Hellwig 		if (uuid_is_nil(&xfs_uuid_table[i])) {
9127174203SChristoph Hellwig 			hole = i;
9227174203SChristoph Hellwig 			continue;
9327174203SChristoph Hellwig 		}
9427174203SChristoph Hellwig 		if (uuid_equal(uuid, &xfs_uuid_table[i]))
9527174203SChristoph Hellwig 			goto out_duplicate;
9627174203SChristoph Hellwig 	}
9727174203SChristoph Hellwig 
9827174203SChristoph Hellwig 	if (hole < 0) {
9927174203SChristoph Hellwig 		xfs_uuid_table = kmem_realloc(xfs_uuid_table,
10027174203SChristoph Hellwig 			(xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table),
10127174203SChristoph Hellwig 			xfs_uuid_table_size  * sizeof(*xfs_uuid_table),
10227174203SChristoph Hellwig 			KM_SLEEP);
10327174203SChristoph Hellwig 		hole = xfs_uuid_table_size++;
10427174203SChristoph Hellwig 	}
10527174203SChristoph Hellwig 	xfs_uuid_table[hole] = *uuid;
10627174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
10727174203SChristoph Hellwig 
10827174203SChristoph Hellwig 	return 0;
10927174203SChristoph Hellwig 
11027174203SChristoph Hellwig  out_duplicate:
11127174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
112021000e5SMitsuo Hayasaka 	xfs_warn(mp, "Filesystem has duplicate UUID %pU - can't mount", uuid);
11327174203SChristoph Hellwig 	return XFS_ERROR(EINVAL);
11427174203SChristoph Hellwig }
11527174203SChristoph Hellwig 
11627174203SChristoph Hellwig STATIC void
11727174203SChristoph Hellwig xfs_uuid_unmount(
11827174203SChristoph Hellwig 	struct xfs_mount	*mp)
11927174203SChristoph Hellwig {
12027174203SChristoph Hellwig 	uuid_t			*uuid = &mp->m_sb.sb_uuid;
12127174203SChristoph Hellwig 	int			i;
12227174203SChristoph Hellwig 
12327174203SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_NOUUID)
12427174203SChristoph Hellwig 		return;
12527174203SChristoph Hellwig 
12627174203SChristoph Hellwig 	mutex_lock(&xfs_uuid_table_mutex);
12727174203SChristoph Hellwig 	for (i = 0; i < xfs_uuid_table_size; i++) {
12827174203SChristoph Hellwig 		if (uuid_is_nil(&xfs_uuid_table[i]))
12927174203SChristoph Hellwig 			continue;
13027174203SChristoph Hellwig 		if (!uuid_equal(uuid, &xfs_uuid_table[i]))
13127174203SChristoph Hellwig 			continue;
13227174203SChristoph Hellwig 		memset(&xfs_uuid_table[i], 0, sizeof(uuid_t));
13327174203SChristoph Hellwig 		break;
13427174203SChristoph Hellwig 	}
13527174203SChristoph Hellwig 	ASSERT(i < xfs_uuid_table_size);
13627174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
13727174203SChristoph Hellwig }
13827174203SChristoph Hellwig 
13927174203SChristoph Hellwig 
140e176579eSDave Chinner STATIC void
141e176579eSDave Chinner __xfs_free_perag(
142e176579eSDave Chinner 	struct rcu_head	*head)
143e176579eSDave Chinner {
144e176579eSDave Chinner 	struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head);
145e176579eSDave Chinner 
146e176579eSDave Chinner 	ASSERT(atomic_read(&pag->pag_ref) == 0);
147e176579eSDave Chinner 	kmem_free(pag);
148e176579eSDave Chinner }
149e176579eSDave Chinner 
1500fa800fbSDave Chinner /*
151e176579eSDave Chinner  * Free up the per-ag resources associated with the mount structure.
1521da177e4SLinus Torvalds  */
153c962fb79SChristoph Hellwig STATIC void
154ff4f038cSChristoph Hellwig xfs_free_perag(
155745f6919SChristoph Hellwig 	xfs_mount_t	*mp)
1561da177e4SLinus Torvalds {
1571c1c6ebcSDave Chinner 	xfs_agnumber_t	agno;
1581c1c6ebcSDave Chinner 	struct xfs_perag *pag;
1591da177e4SLinus Torvalds 
1601c1c6ebcSDave Chinner 	for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
1611c1c6ebcSDave Chinner 		spin_lock(&mp->m_perag_lock);
1621c1c6ebcSDave Chinner 		pag = radix_tree_delete(&mp->m_perag_tree, agno);
1631c1c6ebcSDave Chinner 		spin_unlock(&mp->m_perag_lock);
164e176579eSDave Chinner 		ASSERT(pag);
165f83282a8SDave Chinner 		ASSERT(atomic_read(&pag->pag_ref) == 0);
166e176579eSDave Chinner 		call_rcu(&pag->rcu_head, __xfs_free_perag);
1671da177e4SLinus Torvalds 	}
1681da177e4SLinus Torvalds }
1691da177e4SLinus Torvalds 
1704cc929eeSNathan Scott /*
1714cc929eeSNathan Scott  * Check size of device based on the (data/realtime) block count.
1724cc929eeSNathan Scott  * Note: this check is used by the growfs code as well as mount.
1734cc929eeSNathan Scott  */
1744cc929eeSNathan Scott int
1754cc929eeSNathan Scott xfs_sb_validate_fsb_count(
1764cc929eeSNathan Scott 	xfs_sb_t	*sbp,
1774cc929eeSNathan Scott 	__uint64_t	nblocks)
1784cc929eeSNathan Scott {
1794cc929eeSNathan Scott 	ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
1804cc929eeSNathan Scott 	ASSERT(sbp->sb_blocklog >= BBSHIFT);
1814cc929eeSNathan Scott 
1824cc929eeSNathan Scott #if XFS_BIG_BLKNOS     /* Limited by ULONG_MAX of page cache index */
1834cc929eeSNathan Scott 	if (nblocks >> (PAGE_CACHE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
184657a4cffSEric Sandeen 		return EFBIG;
1854cc929eeSNathan Scott #else                  /* Limited by UINT_MAX of sectors */
1864cc929eeSNathan Scott 	if (nblocks << (sbp->sb_blocklog - BBSHIFT) > UINT_MAX)
187657a4cffSEric Sandeen 		return EFBIG;
1884cc929eeSNathan Scott #endif
1894cc929eeSNathan Scott 	return 0;
1904cc929eeSNathan Scott }
1911da177e4SLinus Torvalds 
1921c1c6ebcSDave Chinner int
193c11e2c36SNathan Scott xfs_initialize_perag(
194c11e2c36SNathan Scott 	xfs_mount_t	*mp,
1951c1c6ebcSDave Chinner 	xfs_agnumber_t	agcount,
1961c1c6ebcSDave Chinner 	xfs_agnumber_t	*maxagi)
1971da177e4SLinus Torvalds {
1982d2194f6SCarlos Maiolino 	xfs_agnumber_t	index;
1998b26c582SDave Chinner 	xfs_agnumber_t	first_initialised = 0;
2001da177e4SLinus Torvalds 	xfs_perag_t	*pag;
2011da177e4SLinus Torvalds 	xfs_agino_t	agino;
2021da177e4SLinus Torvalds 	xfs_ino_t	ino;
2031da177e4SLinus Torvalds 	xfs_sb_t	*sbp = &mp->m_sb;
2048b26c582SDave Chinner 	int		error = -ENOMEM;
2051da177e4SLinus Torvalds 
2061c1c6ebcSDave Chinner 	/*
2071c1c6ebcSDave Chinner 	 * Walk the current per-ag tree so we don't try to initialise AGs
2081c1c6ebcSDave Chinner 	 * that already exist (growfs case). Allocate and insert all the
2091c1c6ebcSDave Chinner 	 * AGs we don't find ready for initialisation.
2101c1c6ebcSDave Chinner 	 */
2111c1c6ebcSDave Chinner 	for (index = 0; index < agcount; index++) {
2121c1c6ebcSDave Chinner 		pag = xfs_perag_get(mp, index);
2131c1c6ebcSDave Chinner 		if (pag) {
2141c1c6ebcSDave Chinner 			xfs_perag_put(pag);
2151c1c6ebcSDave Chinner 			continue;
2161c1c6ebcSDave Chinner 		}
2178b26c582SDave Chinner 		if (!first_initialised)
2188b26c582SDave Chinner 			first_initialised = index;
219fb3b504aSChristoph Hellwig 
2201c1c6ebcSDave Chinner 		pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
2211c1c6ebcSDave Chinner 		if (!pag)
2228b26c582SDave Chinner 			goto out_unwind;
223fb3b504aSChristoph Hellwig 		pag->pag_agno = index;
224fb3b504aSChristoph Hellwig 		pag->pag_mount = mp;
2251a427ab0SDave Chinner 		spin_lock_init(&pag->pag_ici_lock);
22669b491c2SDave Chinner 		mutex_init(&pag->pag_ici_reclaim_lock);
227fb3b504aSChristoph Hellwig 		INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
22874f75a0cSDave Chinner 		spin_lock_init(&pag->pag_buf_lock);
22974f75a0cSDave Chinner 		pag->pag_buf_tree = RB_ROOT;
230fb3b504aSChristoph Hellwig 
2311c1c6ebcSDave Chinner 		if (radix_tree_preload(GFP_NOFS))
2328b26c582SDave Chinner 			goto out_unwind;
233fb3b504aSChristoph Hellwig 
2341c1c6ebcSDave Chinner 		spin_lock(&mp->m_perag_lock);
2351c1c6ebcSDave Chinner 		if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
2361c1c6ebcSDave Chinner 			BUG();
2371c1c6ebcSDave Chinner 			spin_unlock(&mp->m_perag_lock);
2388b26c582SDave Chinner 			radix_tree_preload_end();
2398b26c582SDave Chinner 			error = -EEXIST;
2408b26c582SDave Chinner 			goto out_unwind;
2411c1c6ebcSDave Chinner 		}
2421c1c6ebcSDave Chinner 		spin_unlock(&mp->m_perag_lock);
2431c1c6ebcSDave Chinner 		radix_tree_preload_end();
2441c1c6ebcSDave Chinner 	}
2451c1c6ebcSDave Chinner 
246fb3b504aSChristoph Hellwig 	/*
247fb3b504aSChristoph Hellwig 	 * If we mount with the inode64 option, or no inode overflows
248fb3b504aSChristoph Hellwig 	 * the legacy 32-bit address space clear the inode32 option.
2491da177e4SLinus Torvalds 	 */
250fb3b504aSChristoph Hellwig 	agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
251fb3b504aSChristoph Hellwig 	ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
2521da177e4SLinus Torvalds 
253fb3b504aSChristoph Hellwig 	if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > XFS_MAXINUMBER_32)
254fb3b504aSChristoph Hellwig 		mp->m_flags |= XFS_MOUNT_32BITINODES;
255fb3b504aSChristoph Hellwig 	else
256fb3b504aSChristoph Hellwig 		mp->m_flags &= ~XFS_MOUNT_32BITINODES;
257fb3b504aSChristoph Hellwig 
2582d2194f6SCarlos Maiolino 	if (mp->m_flags & XFS_MOUNT_32BITINODES)
2592d2194f6SCarlos Maiolino 		index = xfs_set_inode32(mp);
2602d2194f6SCarlos Maiolino 	else
2612d2194f6SCarlos Maiolino 		index = xfs_set_inode64(mp);
262fb3b504aSChristoph Hellwig 
2631c1c6ebcSDave Chinner 	if (maxagi)
2641c1c6ebcSDave Chinner 		*maxagi = index;
2651c1c6ebcSDave Chinner 	return 0;
2668b26c582SDave Chinner 
2678b26c582SDave Chinner out_unwind:
2688b26c582SDave Chinner 	kmem_free(pag);
2698b26c582SDave Chinner 	for (; index > first_initialised; index--) {
2708b26c582SDave Chinner 		pag = radix_tree_delete(&mp->m_perag_tree, index);
2718b26c582SDave Chinner 		kmem_free(pag);
2728b26c582SDave Chinner 	}
2738b26c582SDave Chinner 	return error;
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds /*
2771da177e4SLinus Torvalds  * xfs_readsb
2781da177e4SLinus Torvalds  *
2791da177e4SLinus Torvalds  * Does the initial read of the superblock.
2801da177e4SLinus Torvalds  */
2811da177e4SLinus Torvalds int
282ff55068cSDave Chinner xfs_readsb(
283ff55068cSDave Chinner 	struct xfs_mount *mp,
284ff55068cSDave Chinner 	int		flags)
2851da177e4SLinus Torvalds {
2861da177e4SLinus Torvalds 	unsigned int	sector_size;
28704a1e6c5SDave Chinner 	struct xfs_buf	*bp;
28804a1e6c5SDave Chinner 	struct xfs_sb	*sbp = &mp->m_sb;
2891da177e4SLinus Torvalds 	int		error;
290af34e09dSDave Chinner 	int		loud = !(flags & XFS_MFSI_QUIET);
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	ASSERT(mp->m_sb_bp == NULL);
2931da177e4SLinus Torvalds 	ASSERT(mp->m_ddev_targp != NULL);
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	/*
2961da177e4SLinus Torvalds 	 * Allocate a (locked) buffer to hold the superblock.
2971da177e4SLinus Torvalds 	 * This will be kept around at all times to optimize
2981da177e4SLinus Torvalds 	 * access to the superblock.
2991da177e4SLinus Torvalds 	 */
3001da177e4SLinus Torvalds 	sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
3011da177e4SLinus Torvalds 
30226af6552SDave Chinner reread:
303e70b73f8SDave Chinner 	bp = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR,
30498021821SDave Chinner 				   BTOBB(sector_size), 0,
3051813dd64SDave Chinner 				   loud ? &xfs_sb_buf_ops
3061813dd64SDave Chinner 				        : &xfs_sb_quiet_buf_ops);
30726af6552SDave Chinner 	if (!bp) {
308af34e09dSDave Chinner 		if (loud)
309af34e09dSDave Chinner 			xfs_warn(mp, "SB buffer read failed");
31026af6552SDave Chinner 		return EIO;
3111da177e4SLinus Torvalds 	}
312eab4e633SDave Chinner 	if (bp->b_error) {
313eab4e633SDave Chinner 		error = bp->b_error;
314eab4e633SDave Chinner 		if (loud)
315e721f504SDave Chinner 			xfs_warn(mp, "SB validate failed with error %d.", error);
316eab4e633SDave Chinner 		goto release_buf;
317eab4e633SDave Chinner 	}
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	/*
3201da177e4SLinus Torvalds 	 * Initialize the mount structure from the superblock.
3211da177e4SLinus Torvalds 	 */
32298021821SDave Chinner 	xfs_sb_from_disk(&mp->m_sb, XFS_BUF_TO_SBP(bp));
32383e782e1SChandra Seetharaman 	xfs_sb_quota_from_disk(&mp->m_sb);
324ff55068cSDave Chinner 
3251da177e4SLinus Torvalds 	/*
3261da177e4SLinus Torvalds 	 * We must be able to do sector-sized and sector-aligned IO.
3271da177e4SLinus Torvalds 	 */
32804a1e6c5SDave Chinner 	if (sector_size > sbp->sb_sectsize) {
329af34e09dSDave Chinner 		if (loud)
330af34e09dSDave Chinner 			xfs_warn(mp, "device supports %u byte sectors (not %u)",
33104a1e6c5SDave Chinner 				sector_size, sbp->sb_sectsize);
3321da177e4SLinus Torvalds 		error = ENOSYS;
33326af6552SDave Chinner 		goto release_buf;
3341da177e4SLinus Torvalds 	}
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	/*
3371da177e4SLinus Torvalds 	 * If device sector size is smaller than the superblock size,
3381da177e4SLinus Torvalds 	 * re-read the superblock so the buffer is correctly sized.
3391da177e4SLinus Torvalds 	 */
34004a1e6c5SDave Chinner 	if (sector_size < sbp->sb_sectsize) {
3411da177e4SLinus Torvalds 		xfs_buf_relse(bp);
34204a1e6c5SDave Chinner 		sector_size = sbp->sb_sectsize;
34326af6552SDave Chinner 		goto reread;
3441da177e4SLinus Torvalds 	}
3451da177e4SLinus Torvalds 
3465478eeadSLachlan McIlroy 	/* Initialize per-cpu counters */
3475478eeadSLachlan McIlroy 	xfs_icsb_reinit_counters(mp);
3488d280b98SDavid Chinner 
34904a1e6c5SDave Chinner 	/* no need to be quiet anymore, so reset the buf ops */
35004a1e6c5SDave Chinner 	bp->b_ops = &xfs_sb_buf_ops;
35104a1e6c5SDave Chinner 
3521da177e4SLinus Torvalds 	mp->m_sb_bp = bp;
35326af6552SDave Chinner 	xfs_buf_unlock(bp);
3541da177e4SLinus Torvalds 	return 0;
3551da177e4SLinus Torvalds 
35626af6552SDave Chinner release_buf:
3571da177e4SLinus Torvalds 	xfs_buf_relse(bp);
3581da177e4SLinus Torvalds 	return error;
3591da177e4SLinus Torvalds }
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds /*
3620771fb45SEric Sandeen  * Update alignment values based on mount options and sb values
3631da177e4SLinus Torvalds  */
3640771fb45SEric Sandeen STATIC int
3657884bc86SChristoph Hellwig xfs_update_alignment(xfs_mount_t *mp)
3661da177e4SLinus Torvalds {
3671da177e4SLinus Torvalds 	xfs_sb_t	*sbp = &(mp->m_sb);
3681da177e4SLinus Torvalds 
3694249023aSChristoph Hellwig 	if (mp->m_dalign) {
3701da177e4SLinus Torvalds 		/*
3711da177e4SLinus Torvalds 		 * If stripe unit and stripe width are not multiples
3721da177e4SLinus Torvalds 		 * of the fs blocksize turn off alignment.
3731da177e4SLinus Torvalds 		 */
3741da177e4SLinus Torvalds 		if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
3751da177e4SLinus Torvalds 		    (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
37639a45d84SJie Liu 			xfs_warn(mp,
37739a45d84SJie Liu 		"alignment check failed: sunit/swidth vs. blocksize(%d)",
37839a45d84SJie Liu 				sbp->sb_blocksize);
3790771fb45SEric Sandeen 			return XFS_ERROR(EINVAL);
3801da177e4SLinus Torvalds 		} else {
3811da177e4SLinus Torvalds 			/*
3821da177e4SLinus Torvalds 			 * Convert the stripe unit and width to FSBs.
3831da177e4SLinus Torvalds 			 */
3841da177e4SLinus Torvalds 			mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
3851da177e4SLinus Torvalds 			if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
38653487786SDave Chinner 				xfs_warn(mp,
38739a45d84SJie Liu 			"alignment check failed: sunit/swidth vs. agsize(%d)",
3881da177e4SLinus Torvalds 					 sbp->sb_agblocks);
38939a45d84SJie Liu 				return XFS_ERROR(EINVAL);
3901da177e4SLinus Torvalds 			} else if (mp->m_dalign) {
3911da177e4SLinus Torvalds 				mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
3921da177e4SLinus Torvalds 			} else {
39339a45d84SJie Liu 				xfs_warn(mp,
39439a45d84SJie Liu 			"alignment check failed: sunit(%d) less than bsize(%d)",
39539a45d84SJie Liu 					 mp->m_dalign, sbp->sb_blocksize);
3960771fb45SEric Sandeen 				return XFS_ERROR(EINVAL);
3971da177e4SLinus Torvalds 			}
3981da177e4SLinus Torvalds 		}
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 		/*
4011da177e4SLinus Torvalds 		 * Update superblock with new values
4021da177e4SLinus Torvalds 		 * and log changes
4031da177e4SLinus Torvalds 		 */
40462118709SEric Sandeen 		if (xfs_sb_version_hasdalign(sbp)) {
4051da177e4SLinus Torvalds 			if (sbp->sb_unit != mp->m_dalign) {
4061da177e4SLinus Torvalds 				sbp->sb_unit = mp->m_dalign;
4077884bc86SChristoph Hellwig 				mp->m_update_flags |= XFS_SB_UNIT;
4081da177e4SLinus Torvalds 			}
4091da177e4SLinus Torvalds 			if (sbp->sb_width != mp->m_swidth) {
4101da177e4SLinus Torvalds 				sbp->sb_width = mp->m_swidth;
4117884bc86SChristoph Hellwig 				mp->m_update_flags |= XFS_SB_WIDTH;
4121da177e4SLinus Torvalds 			}
41334d7f603SJie Liu 		} else {
41434d7f603SJie Liu 			xfs_warn(mp,
41534d7f603SJie Liu 	"cannot change alignment: superblock does not support data alignment");
41634d7f603SJie Liu 			return XFS_ERROR(EINVAL);
4171da177e4SLinus Torvalds 		}
4181da177e4SLinus Torvalds 	} else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
41962118709SEric Sandeen 		    xfs_sb_version_hasdalign(&mp->m_sb)) {
4201da177e4SLinus Torvalds 			mp->m_dalign = sbp->sb_unit;
4211da177e4SLinus Torvalds 			mp->m_swidth = sbp->sb_width;
4221da177e4SLinus Torvalds 	}
4231da177e4SLinus Torvalds 
4240771fb45SEric Sandeen 	return 0;
4250771fb45SEric Sandeen }
4261da177e4SLinus Torvalds 
4270771fb45SEric Sandeen /*
4280771fb45SEric Sandeen  * Set the maximum inode count for this filesystem
4290771fb45SEric Sandeen  */
4300771fb45SEric Sandeen STATIC void
4310771fb45SEric Sandeen xfs_set_maxicount(xfs_mount_t *mp)
4320771fb45SEric Sandeen {
4330771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
4341da177e4SLinus Torvalds 	__uint64_t	icount;
4351da177e4SLinus Torvalds 
4360771fb45SEric Sandeen 	if (sbp->sb_imax_pct) {
4370771fb45SEric Sandeen 		/*
4380771fb45SEric Sandeen 		 * Make sure the maximum inode count is a multiple
4390771fb45SEric Sandeen 		 * of the units we allocate inodes in.
4401da177e4SLinus Torvalds 		 */
4411da177e4SLinus Torvalds 		icount = sbp->sb_dblocks * sbp->sb_imax_pct;
4421da177e4SLinus Torvalds 		do_div(icount, 100);
4431da177e4SLinus Torvalds 		do_div(icount, mp->m_ialloc_blks);
4441da177e4SLinus Torvalds 		mp->m_maxicount = (icount * mp->m_ialloc_blks)  <<
4451da177e4SLinus Torvalds 				   sbp->sb_inopblog;
4460771fb45SEric Sandeen 	} else {
4471da177e4SLinus Torvalds 		mp->m_maxicount = 0;
4481da177e4SLinus Torvalds 	}
4491da177e4SLinus Torvalds }
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds /*
4521da177e4SLinus Torvalds  * Set the default minimum read and write sizes unless
4531da177e4SLinus Torvalds  * already specified in a mount option.
4541da177e4SLinus Torvalds  * We use smaller I/O sizes when the file system
4551da177e4SLinus Torvalds  * is being used for NFS service (wsync mount option).
4561da177e4SLinus Torvalds  */
4570771fb45SEric Sandeen STATIC void
4580771fb45SEric Sandeen xfs_set_rw_sizes(xfs_mount_t *mp)
4590771fb45SEric Sandeen {
4600771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
4610771fb45SEric Sandeen 	int		readio_log, writeio_log;
4620771fb45SEric Sandeen 
4631da177e4SLinus Torvalds 	if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
4641da177e4SLinus Torvalds 		if (mp->m_flags & XFS_MOUNT_WSYNC) {
4651da177e4SLinus Torvalds 			readio_log = XFS_WSYNC_READIO_LOG;
4661da177e4SLinus Torvalds 			writeio_log = XFS_WSYNC_WRITEIO_LOG;
4671da177e4SLinus Torvalds 		} else {
4681da177e4SLinus Torvalds 			readio_log = XFS_READIO_LOG_LARGE;
4691da177e4SLinus Torvalds 			writeio_log = XFS_WRITEIO_LOG_LARGE;
4701da177e4SLinus Torvalds 		}
4711da177e4SLinus Torvalds 	} else {
4721da177e4SLinus Torvalds 		readio_log = mp->m_readio_log;
4731da177e4SLinus Torvalds 		writeio_log = mp->m_writeio_log;
4741da177e4SLinus Torvalds 	}
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	if (sbp->sb_blocklog > readio_log) {
4771da177e4SLinus Torvalds 		mp->m_readio_log = sbp->sb_blocklog;
4781da177e4SLinus Torvalds 	} else {
4791da177e4SLinus Torvalds 		mp->m_readio_log = readio_log;
4801da177e4SLinus Torvalds 	}
4811da177e4SLinus Torvalds 	mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
4821da177e4SLinus Torvalds 	if (sbp->sb_blocklog > writeio_log) {
4831da177e4SLinus Torvalds 		mp->m_writeio_log = sbp->sb_blocklog;
4841da177e4SLinus Torvalds 	} else {
4851da177e4SLinus Torvalds 		mp->m_writeio_log = writeio_log;
4861da177e4SLinus Torvalds 	}
4871da177e4SLinus Torvalds 	mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
4880771fb45SEric Sandeen }
489425f9dddSEric Sandeen 
4901da177e4SLinus Torvalds /*
491055388a3SDave Chinner  * precalculate the low space thresholds for dynamic speculative preallocation.
492055388a3SDave Chinner  */
493055388a3SDave Chinner void
494055388a3SDave Chinner xfs_set_low_space_thresholds(
495055388a3SDave Chinner 	struct xfs_mount	*mp)
496055388a3SDave Chinner {
497055388a3SDave Chinner 	int i;
498055388a3SDave Chinner 
499055388a3SDave Chinner 	for (i = 0; i < XFS_LOWSP_MAX; i++) {
500055388a3SDave Chinner 		__uint64_t space = mp->m_sb.sb_dblocks;
501055388a3SDave Chinner 
502055388a3SDave Chinner 		do_div(space, 100);
503055388a3SDave Chinner 		mp->m_low_space[i] = space * (i + 1);
504055388a3SDave Chinner 	}
505055388a3SDave Chinner }
506055388a3SDave Chinner 
507055388a3SDave Chinner 
508055388a3SDave Chinner /*
5091da177e4SLinus Torvalds  * Set whether we're using inode alignment.
5101da177e4SLinus Torvalds  */
5110771fb45SEric Sandeen STATIC void
5120771fb45SEric Sandeen xfs_set_inoalignment(xfs_mount_t *mp)
5130771fb45SEric Sandeen {
51462118709SEric Sandeen 	if (xfs_sb_version_hasalign(&mp->m_sb) &&
5151da177e4SLinus Torvalds 	    mp->m_sb.sb_inoalignmt >=
5161da177e4SLinus Torvalds 	    XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
5171da177e4SLinus Torvalds 		mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
5181da177e4SLinus Torvalds 	else
5191da177e4SLinus Torvalds 		mp->m_inoalign_mask = 0;
5201da177e4SLinus Torvalds 	/*
5211da177e4SLinus Torvalds 	 * If we are using stripe alignment, check whether
5221da177e4SLinus Torvalds 	 * the stripe unit is a multiple of the inode alignment
5231da177e4SLinus Torvalds 	 */
5241da177e4SLinus Torvalds 	if (mp->m_dalign && mp->m_inoalign_mask &&
5251da177e4SLinus Torvalds 	    !(mp->m_dalign & mp->m_inoalign_mask))
5261da177e4SLinus Torvalds 		mp->m_sinoalign = mp->m_dalign;
5271da177e4SLinus Torvalds 	else
5281da177e4SLinus Torvalds 		mp->m_sinoalign = 0;
5290771fb45SEric Sandeen }
5300771fb45SEric Sandeen 
5311da177e4SLinus Torvalds /*
5320471f62eSZhi Yong Wu  * Check that the data (and log if separate) is an ok size.
5331da177e4SLinus Torvalds  */
5340771fb45SEric Sandeen STATIC int
5354249023aSChristoph Hellwig xfs_check_sizes(xfs_mount_t *mp)
5360771fb45SEric Sandeen {
5370771fb45SEric Sandeen 	xfs_buf_t	*bp;
5380771fb45SEric Sandeen 	xfs_daddr_t	d;
5390771fb45SEric Sandeen 
5401da177e4SLinus Torvalds 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
5411da177e4SLinus Torvalds 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
5420b932cccSDave Chinner 		xfs_warn(mp, "filesystem size mismatch detected");
543657a4cffSEric Sandeen 		return XFS_ERROR(EFBIG);
5441da177e4SLinus Torvalds 	}
545e70b73f8SDave Chinner 	bp = xfs_buf_read_uncached(mp->m_ddev_targp,
5461da177e4SLinus Torvalds 					d - XFS_FSS_TO_BB(mp, 1),
547c3f8fc73SDave Chinner 					XFS_FSS_TO_BB(mp, 1), 0, NULL);
5481922c949SDave Chinner 	if (!bp) {
5490b932cccSDave Chinner 		xfs_warn(mp, "last sector read failed");
5501922c949SDave Chinner 		return EIO;
5511da177e4SLinus Torvalds 	}
5521922c949SDave Chinner 	xfs_buf_relse(bp);
5531da177e4SLinus Torvalds 
5544249023aSChristoph Hellwig 	if (mp->m_logdev_targp != mp->m_ddev_targp) {
5551da177e4SLinus Torvalds 		d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
5561da177e4SLinus Torvalds 		if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
5570b932cccSDave Chinner 			xfs_warn(mp, "log size mismatch detected");
558657a4cffSEric Sandeen 			return XFS_ERROR(EFBIG);
5591da177e4SLinus Torvalds 		}
560e70b73f8SDave Chinner 		bp = xfs_buf_read_uncached(mp->m_logdev_targp,
5611da177e4SLinus Torvalds 					d - XFS_FSB_TO_BB(mp, 1),
562c3f8fc73SDave Chinner 					XFS_FSB_TO_BB(mp, 1), 0, NULL);
5631922c949SDave Chinner 		if (!bp) {
5640b932cccSDave Chinner 			xfs_warn(mp, "log device read failed");
5651922c949SDave Chinner 			return EIO;
5661da177e4SLinus Torvalds 		}
5671922c949SDave Chinner 		xfs_buf_relse(bp);
5680771fb45SEric Sandeen 	}
5690771fb45SEric Sandeen 	return 0;
5700771fb45SEric Sandeen }
5710771fb45SEric Sandeen 
5720771fb45SEric Sandeen /*
5737d095257SChristoph Hellwig  * Clear the quotaflags in memory and in the superblock.
5747d095257SChristoph Hellwig  */
5757d095257SChristoph Hellwig int
5767d095257SChristoph Hellwig xfs_mount_reset_sbqflags(
5777d095257SChristoph Hellwig 	struct xfs_mount	*mp)
5787d095257SChristoph Hellwig {
5797d095257SChristoph Hellwig 	int			error;
5807d095257SChristoph Hellwig 	struct xfs_trans	*tp;
5817d095257SChristoph Hellwig 
5827d095257SChristoph Hellwig 	mp->m_qflags = 0;
5837d095257SChristoph Hellwig 
5847d095257SChristoph Hellwig 	/*
5857d095257SChristoph Hellwig 	 * It is OK to look at sb_qflags here in mount path,
5867d095257SChristoph Hellwig 	 * without m_sb_lock.
5877d095257SChristoph Hellwig 	 */
5887d095257SChristoph Hellwig 	if (mp->m_sb.sb_qflags == 0)
5897d095257SChristoph Hellwig 		return 0;
5907d095257SChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
5917d095257SChristoph Hellwig 	mp->m_sb.sb_qflags = 0;
5927d095257SChristoph Hellwig 	spin_unlock(&mp->m_sb_lock);
5937d095257SChristoph Hellwig 
5947d095257SChristoph Hellwig 	/*
5957d095257SChristoph Hellwig 	 * If the fs is readonly, let the incore superblock run
5967d095257SChristoph Hellwig 	 * with quotas off but don't flush the update out to disk
5977d095257SChristoph Hellwig 	 */
5987d095257SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_RDONLY)
5997d095257SChristoph Hellwig 		return 0;
6007d095257SChristoph Hellwig 
6017d095257SChristoph Hellwig 	tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
6023d3c8b52SJie Liu 	error = xfs_trans_reserve(tp, &M_RES(mp)->tr_qm_sbchange, 0, 0);
6037d095257SChristoph Hellwig 	if (error) {
6047d095257SChristoph Hellwig 		xfs_trans_cancel(tp, 0);
60553487786SDave Chinner 		xfs_alert(mp, "%s: Superblock update failed!", __func__);
6067d095257SChristoph Hellwig 		return error;
6077d095257SChristoph Hellwig 	}
6087d095257SChristoph Hellwig 
6097d095257SChristoph Hellwig 	xfs_mod_sb(tp, XFS_SB_QFLAGS);
6107d095257SChristoph Hellwig 	return xfs_trans_commit(tp, 0);
6117d095257SChristoph Hellwig }
6127d095257SChristoph Hellwig 
613d5db0f97SEric Sandeen __uint64_t
614d5db0f97SEric Sandeen xfs_default_resblks(xfs_mount_t *mp)
615d5db0f97SEric Sandeen {
616d5db0f97SEric Sandeen 	__uint64_t resblks;
617d5db0f97SEric Sandeen 
618d5db0f97SEric Sandeen 	/*
6198babd8a2SDave Chinner 	 * We default to 5% or 8192 fsbs of space reserved, whichever is
6208babd8a2SDave Chinner 	 * smaller.  This is intended to cover concurrent allocation
6218babd8a2SDave Chinner 	 * transactions when we initially hit enospc. These each require a 4
6228babd8a2SDave Chinner 	 * block reservation. Hence by default we cover roughly 2000 concurrent
6238babd8a2SDave Chinner 	 * allocation reservations.
624d5db0f97SEric Sandeen 	 */
625d5db0f97SEric Sandeen 	resblks = mp->m_sb.sb_dblocks;
626d5db0f97SEric Sandeen 	do_div(resblks, 20);
6278babd8a2SDave Chinner 	resblks = min_t(__uint64_t, resblks, 8192);
628d5db0f97SEric Sandeen 	return resblks;
629d5db0f97SEric Sandeen }
630d5db0f97SEric Sandeen 
6317d095257SChristoph Hellwig /*
6320771fb45SEric Sandeen  * This function does the following on an initial mount of a file system:
6330771fb45SEric Sandeen  *	- reads the superblock from disk and init the mount struct
6340771fb45SEric Sandeen  *	- if we're a 32-bit kernel, do a size check on the superblock
6350771fb45SEric Sandeen  *		so we don't mount terabyte filesystems
6360771fb45SEric Sandeen  *	- init mount struct realtime fields
6370771fb45SEric Sandeen  *	- allocate inode hash table for fs
6380771fb45SEric Sandeen  *	- init directory manager
6390771fb45SEric Sandeen  *	- perform recovery and init the log manager
6400771fb45SEric Sandeen  */
6410771fb45SEric Sandeen int
6420771fb45SEric Sandeen xfs_mountfs(
6434249023aSChristoph Hellwig 	xfs_mount_t	*mp)
6440771fb45SEric Sandeen {
6450771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
6460771fb45SEric Sandeen 	xfs_inode_t	*rip;
6470771fb45SEric Sandeen 	__uint64_t	resblks;
6487d095257SChristoph Hellwig 	uint		quotamount = 0;
6497d095257SChristoph Hellwig 	uint		quotaflags = 0;
6500771fb45SEric Sandeen 	int		error = 0;
6510771fb45SEric Sandeen 
652ff55068cSDave Chinner 	xfs_sb_mount_common(mp, sbp);
6530771fb45SEric Sandeen 
6540771fb45SEric Sandeen 	/*
655e6957ea4SEric Sandeen 	 * Check for a mismatched features2 values.  Older kernels
656e6957ea4SEric Sandeen 	 * read & wrote into the wrong sb offset for sb_features2
657e6957ea4SEric Sandeen 	 * on some platforms due to xfs_sb_t not being 64bit size aligned
658e6957ea4SEric Sandeen 	 * when sb_features2 was added, which made older superblock
659e6957ea4SEric Sandeen 	 * reading/writing routines swap it as a 64-bit value.
660ee1c0908SDavid Chinner 	 *
661e6957ea4SEric Sandeen 	 * For backwards compatibility, we make both slots equal.
662e6957ea4SEric Sandeen 	 *
663e6957ea4SEric Sandeen 	 * If we detect a mismatched field, we OR the set bits into the
664e6957ea4SEric Sandeen 	 * existing features2 field in case it has already been modified; we
665e6957ea4SEric Sandeen 	 * don't want to lose any features.  We then update the bad location
666e6957ea4SEric Sandeen 	 * with the ORed value so that older kernels will see any features2
667e6957ea4SEric Sandeen 	 * flags, and mark the two fields as needing updates once the
668e6957ea4SEric Sandeen 	 * transaction subsystem is online.
669ee1c0908SDavid Chinner 	 */
670e6957ea4SEric Sandeen 	if (xfs_sb_has_mismatched_features2(sbp)) {
6710b932cccSDave Chinner 		xfs_warn(mp, "correcting sb_features alignment problem");
672ee1c0908SDavid Chinner 		sbp->sb_features2 |= sbp->sb_bad_features2;
673e6957ea4SEric Sandeen 		sbp->sb_bad_features2 = sbp->sb_features2;
6747884bc86SChristoph Hellwig 		mp->m_update_flags |= XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2;
675e6957ea4SEric Sandeen 
676e6957ea4SEric Sandeen 		/*
677e6957ea4SEric Sandeen 		 * Re-check for ATTR2 in case it was found in bad_features2
678e6957ea4SEric Sandeen 		 * slot.
679e6957ea4SEric Sandeen 		 */
6807c12f296STim Shimmin 		if (xfs_sb_version_hasattr2(&mp->m_sb) &&
6817c12f296STim Shimmin 		   !(mp->m_flags & XFS_MOUNT_NOATTR2))
682e6957ea4SEric Sandeen 			mp->m_flags |= XFS_MOUNT_ATTR2;
6837c12f296STim Shimmin 	}
684e6957ea4SEric Sandeen 
6857c12f296STim Shimmin 	if (xfs_sb_version_hasattr2(&mp->m_sb) &&
6867c12f296STim Shimmin 	   (mp->m_flags & XFS_MOUNT_NOATTR2)) {
6877c12f296STim Shimmin 		xfs_sb_version_removeattr2(&mp->m_sb);
6887884bc86SChristoph Hellwig 		mp->m_update_flags |= XFS_SB_FEATURES2;
6897c12f296STim Shimmin 
6907c12f296STim Shimmin 		/* update sb_versionnum for the clearing of the morebits */
6917c12f296STim Shimmin 		if (!sbp->sb_features2)
6927884bc86SChristoph Hellwig 			mp->m_update_flags |= XFS_SB_VERSIONNUM;
693ee1c0908SDavid Chinner 	}
694ee1c0908SDavid Chinner 
695ee1c0908SDavid Chinner 	/*
6960771fb45SEric Sandeen 	 * Check if sb_agblocks is aligned at stripe boundary
6970771fb45SEric Sandeen 	 * If sb_agblocks is NOT aligned turn off m_dalign since
6980771fb45SEric Sandeen 	 * allocator alignment is within an ag, therefore ag has
6990771fb45SEric Sandeen 	 * to be aligned at stripe boundary.
7000771fb45SEric Sandeen 	 */
7017884bc86SChristoph Hellwig 	error = xfs_update_alignment(mp);
7020771fb45SEric Sandeen 	if (error)
703f9057e3dSChristoph Hellwig 		goto out;
7040771fb45SEric Sandeen 
7050771fb45SEric Sandeen 	xfs_alloc_compute_maxlevels(mp);
7060771fb45SEric Sandeen 	xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
7070771fb45SEric Sandeen 	xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
7080771fb45SEric Sandeen 	xfs_ialloc_compute_maxlevels(mp);
7090771fb45SEric Sandeen 
7100771fb45SEric Sandeen 	xfs_set_maxicount(mp);
7110771fb45SEric Sandeen 
71227174203SChristoph Hellwig 	error = xfs_uuid_mount(mp);
71327174203SChristoph Hellwig 	if (error)
714f9057e3dSChristoph Hellwig 		goto out;
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	/*
7170771fb45SEric Sandeen 	 * Set the minimum read and write sizes
7180771fb45SEric Sandeen 	 */
7190771fb45SEric Sandeen 	xfs_set_rw_sizes(mp);
7200771fb45SEric Sandeen 
721055388a3SDave Chinner 	/* set the low space thresholds for dynamic preallocation */
722055388a3SDave Chinner 	xfs_set_low_space_thresholds(mp);
723055388a3SDave Chinner 
7240771fb45SEric Sandeen 	/*
7250771fb45SEric Sandeen 	 * Set the inode cluster size.
7260771fb45SEric Sandeen 	 * This may still be overridden by the file system
7270771fb45SEric Sandeen 	 * block size if it is larger than the chosen cluster size.
7280771fb45SEric Sandeen 	 */
7290771fb45SEric Sandeen 	mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
7300771fb45SEric Sandeen 
7310771fb45SEric Sandeen 	/*
7320771fb45SEric Sandeen 	 * Set inode alignment fields
7330771fb45SEric Sandeen 	 */
7340771fb45SEric Sandeen 	xfs_set_inoalignment(mp);
7350771fb45SEric Sandeen 
7360771fb45SEric Sandeen 	/*
737c2bfbc9bSZhi Yong Wu 	 * Check that the data (and log if separate) is an ok size.
7380771fb45SEric Sandeen 	 */
7394249023aSChristoph Hellwig 	error = xfs_check_sizes(mp);
7400771fb45SEric Sandeen 	if (error)
741f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
7420771fb45SEric Sandeen 
7430771fb45SEric Sandeen 	/*
7441da177e4SLinus Torvalds 	 * Initialize realtime fields in the mount structure
7451da177e4SLinus Torvalds 	 */
7460771fb45SEric Sandeen 	error = xfs_rtmount_init(mp);
7470771fb45SEric Sandeen 	if (error) {
7480b932cccSDave Chinner 		xfs_warn(mp, "RT mount failed");
749f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
7501da177e4SLinus Torvalds 	}
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	/*
7531da177e4SLinus Torvalds 	 *  Copies the low order bits of the timestamp and the randomly
7541da177e4SLinus Torvalds 	 *  set "sequence" number out of a UUID.
7551da177e4SLinus Torvalds 	 */
7561da177e4SLinus Torvalds 	uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 	mp->m_dmevmask = 0;	/* not persistent; set after each mount */
7591da177e4SLinus Torvalds 
760f6c2d1faSNathan Scott 	xfs_dir_mount(mp);
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	/*
7631da177e4SLinus Torvalds 	 * Initialize the attribute manager's entries.
7641da177e4SLinus Torvalds 	 */
7651da177e4SLinus Torvalds 	mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	/*
7681da177e4SLinus Torvalds 	 * Initialize the precomputed transaction reservations values.
7691da177e4SLinus Torvalds 	 */
7701da177e4SLinus Torvalds 	xfs_trans_init(mp);
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	/*
7731da177e4SLinus Torvalds 	 * Allocate and initialize the per-ag data.
7741da177e4SLinus Torvalds 	 */
7751c1c6ebcSDave Chinner 	spin_lock_init(&mp->m_perag_lock);
7769b98b6f3SDave Chinner 	INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
7771c1c6ebcSDave Chinner 	error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
7781c1c6ebcSDave Chinner 	if (error) {
7790b932cccSDave Chinner 		xfs_warn(mp, "Failed per-ag init: %d", error);
780f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
7811c1c6ebcSDave Chinner 	}
7821da177e4SLinus Torvalds 
783f9057e3dSChristoph Hellwig 	if (!sbp->sb_logblocks) {
7840b932cccSDave Chinner 		xfs_warn(mp, "no log defined");
785f9057e3dSChristoph Hellwig 		XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp);
786f9057e3dSChristoph Hellwig 		error = XFS_ERROR(EFSCORRUPTED);
787f9057e3dSChristoph Hellwig 		goto out_free_perag;
788f9057e3dSChristoph Hellwig 	}
789f9057e3dSChristoph Hellwig 
7901da177e4SLinus Torvalds 	/*
7911da177e4SLinus Torvalds 	 * log's mount-time initialization. Perform 1st part recovery if needed
7921da177e4SLinus Torvalds 	 */
7931da177e4SLinus Torvalds 	error = xfs_log_mount(mp, mp->m_logdev_targp,
7941da177e4SLinus Torvalds 			      XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
7951da177e4SLinus Torvalds 			      XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
7961da177e4SLinus Torvalds 	if (error) {
7970b932cccSDave Chinner 		xfs_warn(mp, "log mount failed");
798d4f3512bSDave Chinner 		goto out_fail_wait;
7991da177e4SLinus Torvalds 	}
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	/*
80292821e2bSDavid Chinner 	 * Now the log is mounted, we know if it was an unclean shutdown or
80392821e2bSDavid Chinner 	 * not. If it was, with the first phase of recovery has completed, we
80492821e2bSDavid Chinner 	 * have consistent AG blocks on disk. We have not recovered EFIs yet,
80592821e2bSDavid Chinner 	 * but they are recovered transactionally in the second recovery phase
80692821e2bSDavid Chinner 	 * later.
80792821e2bSDavid Chinner 	 *
80892821e2bSDavid Chinner 	 * Hence we can safely re-initialise incore superblock counters from
80992821e2bSDavid Chinner 	 * the per-ag data. These may not be correct if the filesystem was not
81092821e2bSDavid Chinner 	 * cleanly unmounted, so we need to wait for recovery to finish before
81192821e2bSDavid Chinner 	 * doing this.
81292821e2bSDavid Chinner 	 *
81392821e2bSDavid Chinner 	 * If the filesystem was cleanly unmounted, then we can trust the
81492821e2bSDavid Chinner 	 * values in the superblock to be correct and we don't need to do
81592821e2bSDavid Chinner 	 * anything here.
81692821e2bSDavid Chinner 	 *
81792821e2bSDavid Chinner 	 * If we are currently making the filesystem, the initialisation will
81892821e2bSDavid Chinner 	 * fail as the perag data is in an undefined state.
81992821e2bSDavid Chinner 	 */
82092821e2bSDavid Chinner 	if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
82192821e2bSDavid Chinner 	    !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
82292821e2bSDavid Chinner 	     !mp->m_sb.sb_inprogress) {
82392821e2bSDavid Chinner 		error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
824f9057e3dSChristoph Hellwig 		if (error)
825d4f3512bSDave Chinner 			goto out_fail_wait;
82692821e2bSDavid Chinner 	}
827f9057e3dSChristoph Hellwig 
82892821e2bSDavid Chinner 	/*
8291da177e4SLinus Torvalds 	 * Get and sanity-check the root inode.
8301da177e4SLinus Torvalds 	 * Save the pointer to it in the mount structure.
8311da177e4SLinus Torvalds 	 */
8327b6259e7SDave Chinner 	error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip);
8331da177e4SLinus Torvalds 	if (error) {
8340b932cccSDave Chinner 		xfs_warn(mp, "failed to read root inode");
835f9057e3dSChristoph Hellwig 		goto out_log_dealloc;
8361da177e4SLinus Torvalds 	}
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	ASSERT(rip != NULL);
8391da177e4SLinus Torvalds 
840abbede1bSAl Viro 	if (unlikely(!S_ISDIR(rip->i_d.di_mode))) {
8410b932cccSDave Chinner 		xfs_warn(mp, "corrupted root inode %llu: not a directory",
842b6574520SNathan Scott 			(unsigned long long)rip->i_ino);
8431da177e4SLinus Torvalds 		xfs_iunlock(rip, XFS_ILOCK_EXCL);
8441da177e4SLinus Torvalds 		XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
8451da177e4SLinus Torvalds 				 mp);
8461da177e4SLinus Torvalds 		error = XFS_ERROR(EFSCORRUPTED);
847f9057e3dSChristoph Hellwig 		goto out_rele_rip;
8481da177e4SLinus Torvalds 	}
8491da177e4SLinus Torvalds 	mp->m_rootip = rip;	/* save it */
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	xfs_iunlock(rip, XFS_ILOCK_EXCL);
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	/*
8541da177e4SLinus Torvalds 	 * Initialize realtime inode pointers in the mount structure
8551da177e4SLinus Torvalds 	 */
8560771fb45SEric Sandeen 	error = xfs_rtmount_inodes(mp);
8570771fb45SEric Sandeen 	if (error) {
8581da177e4SLinus Torvalds 		/*
8591da177e4SLinus Torvalds 		 * Free up the root inode.
8601da177e4SLinus Torvalds 		 */
8610b932cccSDave Chinner 		xfs_warn(mp, "failed to read RT inodes");
862f9057e3dSChristoph Hellwig 		goto out_rele_rip;
8631da177e4SLinus Torvalds 	}
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	/*
8667884bc86SChristoph Hellwig 	 * If this is a read-only mount defer the superblock updates until
8677884bc86SChristoph Hellwig 	 * the next remount into writeable mode.  Otherwise we would never
8687884bc86SChristoph Hellwig 	 * perform the update e.g. for the root filesystem.
8691da177e4SLinus Torvalds 	 */
8707884bc86SChristoph Hellwig 	if (mp->m_update_flags && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
8717884bc86SChristoph Hellwig 		error = xfs_mount_log_sb(mp, mp->m_update_flags);
872e5720eecSDavid Chinner 		if (error) {
8730b932cccSDave Chinner 			xfs_warn(mp, "failed to write sb changes");
874b93b6e43SChristoph Hellwig 			goto out_rtunmount;
875e5720eecSDavid Chinner 		}
876e5720eecSDavid Chinner 	}
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	/*
8791da177e4SLinus Torvalds 	 * Initialise the XFS quota management subsystem for this mount
8801da177e4SLinus Torvalds 	 */
8817d095257SChristoph Hellwig 	if (XFS_IS_QUOTA_RUNNING(mp)) {
8827d095257SChristoph Hellwig 		error = xfs_qm_newmount(mp, &quotamount, &quotaflags);
8830771fb45SEric Sandeen 		if (error)
884b93b6e43SChristoph Hellwig 			goto out_rtunmount;
8857d095257SChristoph Hellwig 	} else {
8867d095257SChristoph Hellwig 		ASSERT(!XFS_IS_QUOTA_ON(mp));
8877d095257SChristoph Hellwig 
8887d095257SChristoph Hellwig 		/*
8897d095257SChristoph Hellwig 		 * If a file system had quotas running earlier, but decided to
8907d095257SChristoph Hellwig 		 * mount without -o uquota/pquota/gquota options, revoke the
8917d095257SChristoph Hellwig 		 * quotachecked license.
8927d095257SChristoph Hellwig 		 */
8937d095257SChristoph Hellwig 		if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) {
8940b932cccSDave Chinner 			xfs_notice(mp, "resetting quota flags");
8957d095257SChristoph Hellwig 			error = xfs_mount_reset_sbqflags(mp);
8967d095257SChristoph Hellwig 			if (error)
8977d095257SChristoph Hellwig 				return error;
8987d095257SChristoph Hellwig 		}
8997d095257SChristoph Hellwig 	}
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	/*
9021da177e4SLinus Torvalds 	 * Finish recovering the file system.  This part needed to be
9031da177e4SLinus Torvalds 	 * delayed until after the root and real-time bitmap inodes
9041da177e4SLinus Torvalds 	 * were consistently read in.
9051da177e4SLinus Torvalds 	 */
9064249023aSChristoph Hellwig 	error = xfs_log_mount_finish(mp);
9071da177e4SLinus Torvalds 	if (error) {
9080b932cccSDave Chinner 		xfs_warn(mp, "log mount finish failed");
909b93b6e43SChristoph Hellwig 		goto out_rtunmount;
9101da177e4SLinus Torvalds 	}
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 	/*
9131da177e4SLinus Torvalds 	 * Complete the quota initialisation, post-log-replay component.
9141da177e4SLinus Torvalds 	 */
9157d095257SChristoph Hellwig 	if (quotamount) {
9167d095257SChristoph Hellwig 		ASSERT(mp->m_qflags == 0);
9177d095257SChristoph Hellwig 		mp->m_qflags = quotaflags;
9187d095257SChristoph Hellwig 
9197d095257SChristoph Hellwig 		xfs_qm_mount_quotas(mp);
9207d095257SChristoph Hellwig 	}
9217d095257SChristoph Hellwig 
92284e1e99fSDavid Chinner 	/*
92384e1e99fSDavid Chinner 	 * Now we are mounted, reserve a small amount of unused space for
92484e1e99fSDavid Chinner 	 * privileged transactions. This is needed so that transaction
92584e1e99fSDavid Chinner 	 * space required for critical operations can dip into this pool
92684e1e99fSDavid Chinner 	 * when at ENOSPC. This is needed for operations like create with
92784e1e99fSDavid Chinner 	 * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
92884e1e99fSDavid Chinner 	 * are not allowed to use this reserved space.
9298babd8a2SDave Chinner 	 *
9308babd8a2SDave Chinner 	 * This may drive us straight to ENOSPC on mount, but that implies
9318babd8a2SDave Chinner 	 * we were already there on the last unmount. Warn if this occurs.
93284e1e99fSDavid Chinner 	 */
933d5db0f97SEric Sandeen 	if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
934d5db0f97SEric Sandeen 		resblks = xfs_default_resblks(mp);
935714082bcSDavid Chinner 		error = xfs_reserve_blocks(mp, &resblks, NULL);
936714082bcSDavid Chinner 		if (error)
9370b932cccSDave Chinner 			xfs_warn(mp,
9380b932cccSDave Chinner 	"Unable to allocate reserve blocks. Continuing without reserve pool.");
939d5db0f97SEric Sandeen 	}
94084e1e99fSDavid Chinner 
9411da177e4SLinus Torvalds 	return 0;
9421da177e4SLinus Torvalds 
943b93b6e43SChristoph Hellwig  out_rtunmount:
944b93b6e43SChristoph Hellwig 	xfs_rtunmount_inodes(mp);
945f9057e3dSChristoph Hellwig  out_rele_rip:
94643355099SChristoph Hellwig 	IRELE(rip);
947f9057e3dSChristoph Hellwig  out_log_dealloc:
94821b699c8SChristoph Hellwig 	xfs_log_unmount(mp);
949d4f3512bSDave Chinner  out_fail_wait:
950d4f3512bSDave Chinner 	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
951d4f3512bSDave Chinner 		xfs_wait_buftarg(mp->m_logdev_targp);
952d4f3512bSDave Chinner 	xfs_wait_buftarg(mp->m_ddev_targp);
953f9057e3dSChristoph Hellwig  out_free_perag:
954ff4f038cSChristoph Hellwig 	xfs_free_perag(mp);
955f9057e3dSChristoph Hellwig  out_remove_uuid:
95627174203SChristoph Hellwig 	xfs_uuid_unmount(mp);
957f9057e3dSChristoph Hellwig  out:
9581da177e4SLinus Torvalds 	return error;
9591da177e4SLinus Torvalds }
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds /*
9621da177e4SLinus Torvalds  * This flushes out the inodes,dquots and the superblock, unmounts the
9631da177e4SLinus Torvalds  * log and makes sure that incore structures are freed.
9641da177e4SLinus Torvalds  */
96541b5c2e7SChristoph Hellwig void
96641b5c2e7SChristoph Hellwig xfs_unmountfs(
96741b5c2e7SChristoph Hellwig 	struct xfs_mount	*mp)
9681da177e4SLinus Torvalds {
96984e1e99fSDavid Chinner 	__uint64_t		resblks;
97041b5c2e7SChristoph Hellwig 	int			error;
9711da177e4SLinus Torvalds 
972579b62faSBrian Foster 	cancel_delayed_work_sync(&mp->m_eofblocks_work);
973579b62faSBrian Foster 
9747d095257SChristoph Hellwig 	xfs_qm_unmount_quotas(mp);
975b93b6e43SChristoph Hellwig 	xfs_rtunmount_inodes(mp);
97677508ec8SChristoph Hellwig 	IRELE(mp->m_rootip);
97777508ec8SChristoph Hellwig 
978641c56fbSDavid Chinner 	/*
979641c56fbSDavid Chinner 	 * We can potentially deadlock here if we have an inode cluster
9809da096fdSMalcolm Parsons 	 * that has been freed has its buffer still pinned in memory because
981641c56fbSDavid Chinner 	 * the transaction is still sitting in a iclog. The stale inodes
982641c56fbSDavid Chinner 	 * on that buffer will have their flush locks held until the
983641c56fbSDavid Chinner 	 * transaction hits the disk and the callbacks run. the inode
984641c56fbSDavid Chinner 	 * flush takes the flush lock unconditionally and with nothing to
985641c56fbSDavid Chinner 	 * push out the iclog we will never get that unlocked. hence we
986641c56fbSDavid Chinner 	 * need to force the log first.
987641c56fbSDavid Chinner 	 */
988a14a348bSChristoph Hellwig 	xfs_log_force(mp, XFS_LOG_SYNC);
989c854363eSDave Chinner 
990c854363eSDave Chinner 	/*
991211e4d43SChristoph Hellwig 	 * Flush all pending changes from the AIL.
992c854363eSDave Chinner 	 */
993211e4d43SChristoph Hellwig 	xfs_ail_push_all_sync(mp->m_ail);
994211e4d43SChristoph Hellwig 
995211e4d43SChristoph Hellwig 	/*
996211e4d43SChristoph Hellwig 	 * And reclaim all inodes.  At this point there should be no dirty
9977e18530bSDave Chinner 	 * inodes and none should be pinned or locked, but use synchronous
9987e18530bSDave Chinner 	 * reclaim just to be sure. We can stop background inode reclaim
9997e18530bSDave Chinner 	 * here as well if it is still running.
1000211e4d43SChristoph Hellwig 	 */
10017e18530bSDave Chinner 	cancel_delayed_work_sync(&mp->m_reclaim_work);
1002c854363eSDave Chinner 	xfs_reclaim_inodes(mp, SYNC_WAIT);
10031da177e4SLinus Torvalds 
10047d095257SChristoph Hellwig 	xfs_qm_unmount(mp);
1005a357a121SLachlan McIlroy 
10061da177e4SLinus Torvalds 	/*
100784e1e99fSDavid Chinner 	 * Unreserve any blocks we have so that when we unmount we don't account
100884e1e99fSDavid Chinner 	 * the reserved free space as used. This is really only necessary for
100984e1e99fSDavid Chinner 	 * lazy superblock counting because it trusts the incore superblock
10109da096fdSMalcolm Parsons 	 * counters to be absolutely correct on clean unmount.
101184e1e99fSDavid Chinner 	 *
101284e1e99fSDavid Chinner 	 * We don't bother correcting this elsewhere for lazy superblock
101384e1e99fSDavid Chinner 	 * counting because on mount of an unclean filesystem we reconstruct the
101484e1e99fSDavid Chinner 	 * correct counter value and this is irrelevant.
101584e1e99fSDavid Chinner 	 *
101684e1e99fSDavid Chinner 	 * For non-lazy counter filesystems, this doesn't matter at all because
101784e1e99fSDavid Chinner 	 * we only every apply deltas to the superblock and hence the incore
101884e1e99fSDavid Chinner 	 * value does not matter....
101984e1e99fSDavid Chinner 	 */
102084e1e99fSDavid Chinner 	resblks = 0;
1021714082bcSDavid Chinner 	error = xfs_reserve_blocks(mp, &resblks, NULL);
1022714082bcSDavid Chinner 	if (error)
10230b932cccSDave Chinner 		xfs_warn(mp, "Unable to free reserved block pool. "
1024714082bcSDavid Chinner 				"Freespace may not be correct on next mount.");
1025714082bcSDavid Chinner 
1026adab0f67SChandra Seetharaman 	error = xfs_log_sbcount(mp);
1027e5720eecSDavid Chinner 	if (error)
10280b932cccSDave Chinner 		xfs_warn(mp, "Unable to update superblock counters. "
1029e5720eecSDavid Chinner 				"Freespace may not be correct on next mount.");
103087c7bec7SChristoph Hellwig 
103121b699c8SChristoph Hellwig 	xfs_log_unmount(mp);
103227174203SChristoph Hellwig 	xfs_uuid_unmount(mp);
10331da177e4SLinus Torvalds 
10341550d0b0SChristoph Hellwig #if defined(DEBUG)
10350ce4cfd4SChristoph Hellwig 	xfs_errortag_clearall(mp, 0);
10361da177e4SLinus Torvalds #endif
1037ff4f038cSChristoph Hellwig 	xfs_free_perag(mp);
10381da177e4SLinus Torvalds }
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds int
104192821e2bSDavid Chinner xfs_fs_writable(xfs_mount_t *mp)
104292821e2bSDavid Chinner {
1043d9457dc0SJan Kara 	return !(mp->m_super->s_writers.frozen || XFS_FORCED_SHUTDOWN(mp) ||
1044bd186aa9SChristoph Hellwig 		(mp->m_flags & XFS_MOUNT_RDONLY));
104592821e2bSDavid Chinner }
104692821e2bSDavid Chinner 
104792821e2bSDavid Chinner /*
1048b2ce3974SAlex Elder  * xfs_log_sbcount
1049b2ce3974SAlex Elder  *
1050adab0f67SChandra Seetharaman  * Sync the superblock counters to disk.
1051b2ce3974SAlex Elder  *
1052b2ce3974SAlex Elder  * Note this code can be called during the process of freezing, so
1053adab0f67SChandra Seetharaman  * we may need to use the transaction allocator which does not
1054b2ce3974SAlex Elder  * block when the transaction subsystem is in its frozen state.
105592821e2bSDavid Chinner  */
105692821e2bSDavid Chinner int
1057adab0f67SChandra Seetharaman xfs_log_sbcount(xfs_mount_t *mp)
105892821e2bSDavid Chinner {
105992821e2bSDavid Chinner 	xfs_trans_t	*tp;
106092821e2bSDavid Chinner 	int		error;
106192821e2bSDavid Chinner 
106292821e2bSDavid Chinner 	if (!xfs_fs_writable(mp))
106392821e2bSDavid Chinner 		return 0;
106492821e2bSDavid Chinner 
1065d4d90b57SChristoph Hellwig 	xfs_icsb_sync_counters(mp, 0);
106692821e2bSDavid Chinner 
106792821e2bSDavid Chinner 	/*
106892821e2bSDavid Chinner 	 * we don't need to do this if we are updating the superblock
106992821e2bSDavid Chinner 	 * counters on every modification.
107092821e2bSDavid Chinner 	 */
107192821e2bSDavid Chinner 	if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
107292821e2bSDavid Chinner 		return 0;
107392821e2bSDavid Chinner 
1074b2ce3974SAlex Elder 	tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT, KM_SLEEP);
10753d3c8b52SJie Liu 	error = xfs_trans_reserve(tp, &M_RES(mp)->tr_sb, 0, 0);
107692821e2bSDavid Chinner 	if (error) {
107792821e2bSDavid Chinner 		xfs_trans_cancel(tp, 0);
107892821e2bSDavid Chinner 		return error;
107992821e2bSDavid Chinner 	}
108092821e2bSDavid Chinner 
108192821e2bSDavid Chinner 	xfs_mod_sb(tp, XFS_SB_IFREE | XFS_SB_ICOUNT | XFS_SB_FDBLOCKS);
108292821e2bSDavid Chinner 	xfs_trans_set_sync(tp);
1083e5720eecSDavid Chinner 	error = xfs_trans_commit(tp, 0);
1084e5720eecSDavid Chinner 	return error;
108592821e2bSDavid Chinner }
108692821e2bSDavid Chinner 
10871da177e4SLinus Torvalds /*
108899e738b7SZhi Yong Wu  * xfs_mod_incore_sb_unlocked() is a utility routine commonly used to apply
10891da177e4SLinus Torvalds  * a delta to a specified field in the in-core superblock.  Simply
10901da177e4SLinus Torvalds  * switch on the field indicated and apply the delta to that field.
10911da177e4SLinus Torvalds  * Fields are not allowed to dip below zero, so if the delta would
10921da177e4SLinus Torvalds  * do this do not apply it and return EINVAL.
10931da177e4SLinus Torvalds  *
10943685c2a1SEric Sandeen  * The m_sb_lock must be held when this routine is called.
10951da177e4SLinus Torvalds  */
1096d96f8f89SEric Sandeen STATIC int
109720f4ebf2SDavid Chinner xfs_mod_incore_sb_unlocked(
109820f4ebf2SDavid Chinner 	xfs_mount_t	*mp,
109920f4ebf2SDavid Chinner 	xfs_sb_field_t	field,
110020f4ebf2SDavid Chinner 	int64_t		delta,
110120f4ebf2SDavid Chinner 	int		rsvd)
11021da177e4SLinus Torvalds {
11031da177e4SLinus Torvalds 	int		scounter;	/* short counter for 32 bit fields */
11041da177e4SLinus Torvalds 	long long	lcounter;	/* long counter for 64 bit fields */
11051da177e4SLinus Torvalds 	long long	res_used, rem;
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 	/*
11081da177e4SLinus Torvalds 	 * With the in-core superblock spin lock held, switch
11091da177e4SLinus Torvalds 	 * on the indicated field.  Apply the delta to the
11101da177e4SLinus Torvalds 	 * proper field.  If the fields value would dip below
11111da177e4SLinus Torvalds 	 * 0, then do not apply the delta and return EINVAL.
11121da177e4SLinus Torvalds 	 */
11131da177e4SLinus Torvalds 	switch (field) {
11141da177e4SLinus Torvalds 	case XFS_SBS_ICOUNT:
11151da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_icount;
11161da177e4SLinus Torvalds 		lcounter += delta;
11171da177e4SLinus Torvalds 		if (lcounter < 0) {
11181da177e4SLinus Torvalds 			ASSERT(0);
1119014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
11201da177e4SLinus Torvalds 		}
11211da177e4SLinus Torvalds 		mp->m_sb.sb_icount = lcounter;
1122014c2544SJesper Juhl 		return 0;
11231da177e4SLinus Torvalds 	case XFS_SBS_IFREE:
11241da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_ifree;
11251da177e4SLinus Torvalds 		lcounter += delta;
11261da177e4SLinus Torvalds 		if (lcounter < 0) {
11271da177e4SLinus Torvalds 			ASSERT(0);
1128014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
11291da177e4SLinus Torvalds 		}
11301da177e4SLinus Torvalds 		mp->m_sb.sb_ifree = lcounter;
1131014c2544SJesper Juhl 		return 0;
11321da177e4SLinus Torvalds 	case XFS_SBS_FDBLOCKS:
11334be536deSDavid Chinner 		lcounter = (long long)
11344be536deSDavid Chinner 			mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
11351da177e4SLinus Torvalds 		res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 		if (delta > 0) {		/* Putting blocks back */
11381da177e4SLinus Torvalds 			if (res_used > delta) {
11391da177e4SLinus Torvalds 				mp->m_resblks_avail += delta;
11401da177e4SLinus Torvalds 			} else {
11411da177e4SLinus Torvalds 				rem = delta - res_used;
11421da177e4SLinus Torvalds 				mp->m_resblks_avail = mp->m_resblks;
11431da177e4SLinus Torvalds 				lcounter += rem;
11441da177e4SLinus Torvalds 			}
11451da177e4SLinus Torvalds 		} else {				/* Taking blocks away */
11461da177e4SLinus Torvalds 			lcounter += delta;
11478babd8a2SDave Chinner 			if (lcounter >= 0) {
11488babd8a2SDave Chinner 				mp->m_sb.sb_fdblocks = lcounter +
11498babd8a2SDave Chinner 							XFS_ALLOC_SET_ASIDE(mp);
11508babd8a2SDave Chinner 				return 0;
11518babd8a2SDave Chinner 			}
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds 			/*
11548babd8a2SDave Chinner 			 * We are out of blocks, use any available reserved
11558babd8a2SDave Chinner 			 * blocks if were allowed to.
11561da177e4SLinus Torvalds 			 */
11578babd8a2SDave Chinner 			if (!rsvd)
1158014c2544SJesper Juhl 				return XFS_ERROR(ENOSPC);
11598babd8a2SDave Chinner 
11608babd8a2SDave Chinner 			lcounter = (long long)mp->m_resblks_avail + delta;
11618babd8a2SDave Chinner 			if (lcounter >= 0) {
11621da177e4SLinus Torvalds 				mp->m_resblks_avail = lcounter;
1163014c2544SJesper Juhl 				return 0;
11648babd8a2SDave Chinner 			}
11658babd8a2SDave Chinner 			printk_once(KERN_WARNING
11668babd8a2SDave Chinner 				"Filesystem \"%s\": reserve blocks depleted! "
11678babd8a2SDave Chinner 				"Consider increasing reserve pool size.",
11688babd8a2SDave Chinner 				mp->m_fsname);
1169014c2544SJesper Juhl 			return XFS_ERROR(ENOSPC);
11701da177e4SLinus Torvalds 		}
11711da177e4SLinus Torvalds 
11724be536deSDavid Chinner 		mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
1173014c2544SJesper Juhl 		return 0;
11741da177e4SLinus Torvalds 	case XFS_SBS_FREXTENTS:
11751da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_frextents;
11761da177e4SLinus Torvalds 		lcounter += delta;
11771da177e4SLinus Torvalds 		if (lcounter < 0) {
1178014c2544SJesper Juhl 			return XFS_ERROR(ENOSPC);
11791da177e4SLinus Torvalds 		}
11801da177e4SLinus Torvalds 		mp->m_sb.sb_frextents = lcounter;
1181014c2544SJesper Juhl 		return 0;
11821da177e4SLinus Torvalds 	case XFS_SBS_DBLOCKS:
11831da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_dblocks;
11841da177e4SLinus Torvalds 		lcounter += delta;
11851da177e4SLinus Torvalds 		if (lcounter < 0) {
11861da177e4SLinus Torvalds 			ASSERT(0);
1187014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
11881da177e4SLinus Torvalds 		}
11891da177e4SLinus Torvalds 		mp->m_sb.sb_dblocks = lcounter;
1190014c2544SJesper Juhl 		return 0;
11911da177e4SLinus Torvalds 	case XFS_SBS_AGCOUNT:
11921da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_agcount;
11931da177e4SLinus Torvalds 		scounter += delta;
11941da177e4SLinus Torvalds 		if (scounter < 0) {
11951da177e4SLinus Torvalds 			ASSERT(0);
1196014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
11971da177e4SLinus Torvalds 		}
11981da177e4SLinus Torvalds 		mp->m_sb.sb_agcount = scounter;
1199014c2544SJesper Juhl 		return 0;
12001da177e4SLinus Torvalds 	case XFS_SBS_IMAX_PCT:
12011da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_imax_pct;
12021da177e4SLinus Torvalds 		scounter += delta;
12031da177e4SLinus Torvalds 		if (scounter < 0) {
12041da177e4SLinus Torvalds 			ASSERT(0);
1205014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
12061da177e4SLinus Torvalds 		}
12071da177e4SLinus Torvalds 		mp->m_sb.sb_imax_pct = scounter;
1208014c2544SJesper Juhl 		return 0;
12091da177e4SLinus Torvalds 	case XFS_SBS_REXTSIZE:
12101da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rextsize;
12111da177e4SLinus Torvalds 		scounter += delta;
12121da177e4SLinus Torvalds 		if (scounter < 0) {
12131da177e4SLinus Torvalds 			ASSERT(0);
1214014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
12151da177e4SLinus Torvalds 		}
12161da177e4SLinus Torvalds 		mp->m_sb.sb_rextsize = scounter;
1217014c2544SJesper Juhl 		return 0;
12181da177e4SLinus Torvalds 	case XFS_SBS_RBMBLOCKS:
12191da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rbmblocks;
12201da177e4SLinus Torvalds 		scounter += delta;
12211da177e4SLinus Torvalds 		if (scounter < 0) {
12221da177e4SLinus Torvalds 			ASSERT(0);
1223014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
12241da177e4SLinus Torvalds 		}
12251da177e4SLinus Torvalds 		mp->m_sb.sb_rbmblocks = scounter;
1226014c2544SJesper Juhl 		return 0;
12271da177e4SLinus Torvalds 	case XFS_SBS_RBLOCKS:
12281da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_rblocks;
12291da177e4SLinus Torvalds 		lcounter += delta;
12301da177e4SLinus Torvalds 		if (lcounter < 0) {
12311da177e4SLinus Torvalds 			ASSERT(0);
1232014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
12331da177e4SLinus Torvalds 		}
12341da177e4SLinus Torvalds 		mp->m_sb.sb_rblocks = lcounter;
1235014c2544SJesper Juhl 		return 0;
12361da177e4SLinus Torvalds 	case XFS_SBS_REXTENTS:
12371da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_rextents;
12381da177e4SLinus Torvalds 		lcounter += delta;
12391da177e4SLinus Torvalds 		if (lcounter < 0) {
12401da177e4SLinus Torvalds 			ASSERT(0);
1241014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
12421da177e4SLinus Torvalds 		}
12431da177e4SLinus Torvalds 		mp->m_sb.sb_rextents = lcounter;
1244014c2544SJesper Juhl 		return 0;
12451da177e4SLinus Torvalds 	case XFS_SBS_REXTSLOG:
12461da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rextslog;
12471da177e4SLinus Torvalds 		scounter += delta;
12481da177e4SLinus Torvalds 		if (scounter < 0) {
12491da177e4SLinus Torvalds 			ASSERT(0);
1250014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
12511da177e4SLinus Torvalds 		}
12521da177e4SLinus Torvalds 		mp->m_sb.sb_rextslog = scounter;
1253014c2544SJesper Juhl 		return 0;
12541da177e4SLinus Torvalds 	default:
12551da177e4SLinus Torvalds 		ASSERT(0);
1256014c2544SJesper Juhl 		return XFS_ERROR(EINVAL);
12571da177e4SLinus Torvalds 	}
12581da177e4SLinus Torvalds }
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds /*
12611da177e4SLinus Torvalds  * xfs_mod_incore_sb() is used to change a field in the in-core
12621da177e4SLinus Torvalds  * superblock structure by the specified delta.  This modification
12633685c2a1SEric Sandeen  * is protected by the m_sb_lock.  Just use the xfs_mod_incore_sb_unlocked()
12641da177e4SLinus Torvalds  * routine to do the work.
12651da177e4SLinus Torvalds  */
12661da177e4SLinus Torvalds int
126720f4ebf2SDavid Chinner xfs_mod_incore_sb(
126896540c78SChristoph Hellwig 	struct xfs_mount	*mp,
126920f4ebf2SDavid Chinner 	xfs_sb_field_t		field,
127020f4ebf2SDavid Chinner 	int64_t			delta,
127120f4ebf2SDavid Chinner 	int			rsvd)
12721da177e4SLinus Torvalds {
12731da177e4SLinus Torvalds 	int			status;
12741da177e4SLinus Torvalds 
12758d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
127696540c78SChristoph Hellwig 	ASSERT(field < XFS_SBS_ICOUNT || field > XFS_SBS_FDBLOCKS);
12778d280b98SDavid Chinner #endif
12783685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
12791da177e4SLinus Torvalds 	status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
12803685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
12818d280b98SDavid Chinner 
1282014c2544SJesper Juhl 	return status;
12831da177e4SLinus Torvalds }
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds /*
12861b040712SChristoph Hellwig  * Change more than one field in the in-core superblock structure at a time.
12871da177e4SLinus Torvalds  *
12881b040712SChristoph Hellwig  * The fields and changes to those fields are specified in the array of
12891b040712SChristoph Hellwig  * xfs_mod_sb structures passed in.  Either all of the specified deltas
12901b040712SChristoph Hellwig  * will be applied or none of them will.  If any modified field dips below 0,
12911b040712SChristoph Hellwig  * then all modifications will be backed out and EINVAL will be returned.
12921b040712SChristoph Hellwig  *
12931b040712SChristoph Hellwig  * Note that this function may not be used for the superblock values that
12941b040712SChristoph Hellwig  * are tracked with the in-memory per-cpu counters - a direct call to
12951b040712SChristoph Hellwig  * xfs_icsb_modify_counters is required for these.
12961da177e4SLinus Torvalds  */
12971da177e4SLinus Torvalds int
12981b040712SChristoph Hellwig xfs_mod_incore_sb_batch(
12991b040712SChristoph Hellwig 	struct xfs_mount	*mp,
13001b040712SChristoph Hellwig 	xfs_mod_sb_t		*msb,
13011b040712SChristoph Hellwig 	uint			nmsb,
13021b040712SChristoph Hellwig 	int			rsvd)
13031da177e4SLinus Torvalds {
130445c51b99SDavid Sterba 	xfs_mod_sb_t		*msbp;
13051b040712SChristoph Hellwig 	int			error = 0;
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 	/*
13081b040712SChristoph Hellwig 	 * Loop through the array of mod structures and apply each individually.
13091b040712SChristoph Hellwig 	 * If any fail, then back out all those which have already been applied.
13101b040712SChristoph Hellwig 	 * Do all of this within the scope of the m_sb_lock so that all of the
13111b040712SChristoph Hellwig 	 * changes will be atomic.
13121da177e4SLinus Torvalds 	 */
13133685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
131445c51b99SDavid Sterba 	for (msbp = msb; msbp < (msb + nmsb); msbp++) {
13151b040712SChristoph Hellwig 		ASSERT(msbp->msb_field < XFS_SBS_ICOUNT ||
13161b040712SChristoph Hellwig 		       msbp->msb_field > XFS_SBS_FDBLOCKS);
13178d280b98SDavid Chinner 
13181b040712SChristoph Hellwig 		error = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
13191b040712SChristoph Hellwig 						   msbp->msb_delta, rsvd);
13201b040712SChristoph Hellwig 		if (error)
13211b040712SChristoph Hellwig 			goto unwind;
13221da177e4SLinus Torvalds 	}
13231b040712SChristoph Hellwig 	spin_unlock(&mp->m_sb_lock);
13241b040712SChristoph Hellwig 	return 0;
13251da177e4SLinus Torvalds 
13261b040712SChristoph Hellwig unwind:
13271b040712SChristoph Hellwig 	while (--msbp >= msb) {
13281b040712SChristoph Hellwig 		error = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
13291b040712SChristoph Hellwig 						   -msbp->msb_delta, rsvd);
13301b040712SChristoph Hellwig 		ASSERT(error == 0);
13311da177e4SLinus Torvalds 	}
13323685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
13331b040712SChristoph Hellwig 	return error;
13341da177e4SLinus Torvalds }
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds /*
13371da177e4SLinus Torvalds  * xfs_getsb() is called to obtain the buffer for the superblock.
13381da177e4SLinus Torvalds  * The buffer is returned locked and read in from disk.
13391da177e4SLinus Torvalds  * The buffer should be released with a call to xfs_brelse().
13401da177e4SLinus Torvalds  *
13411da177e4SLinus Torvalds  * If the flags parameter is BUF_TRYLOCK, then we'll only return
13421da177e4SLinus Torvalds  * the superblock buffer if it can be locked without sleeping.
13431da177e4SLinus Torvalds  * If it can't then we'll return NULL.
13441da177e4SLinus Torvalds  */
13450c842ad4SChristoph Hellwig struct xfs_buf *
13461da177e4SLinus Torvalds xfs_getsb(
13470c842ad4SChristoph Hellwig 	struct xfs_mount	*mp,
13481da177e4SLinus Torvalds 	int			flags)
13491da177e4SLinus Torvalds {
13500c842ad4SChristoph Hellwig 	struct xfs_buf		*bp = mp->m_sb_bp;
13511da177e4SLinus Torvalds 
13520c842ad4SChristoph Hellwig 	if (!xfs_buf_trylock(bp)) {
13530c842ad4SChristoph Hellwig 		if (flags & XBF_TRYLOCK)
13541da177e4SLinus Torvalds 			return NULL;
13550c842ad4SChristoph Hellwig 		xfs_buf_lock(bp);
13561da177e4SLinus Torvalds 	}
13570c842ad4SChristoph Hellwig 
135872790aa1SChandra Seetharaman 	xfs_buf_hold(bp);
13591da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISDONE(bp));
1360014c2544SJesper Juhl 	return bp;
13611da177e4SLinus Torvalds }
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds /*
13641da177e4SLinus Torvalds  * Used to free the superblock along various error paths.
13651da177e4SLinus Torvalds  */
13661da177e4SLinus Torvalds void
13671da177e4SLinus Torvalds xfs_freesb(
136826af6552SDave Chinner 	struct xfs_mount	*mp)
13691da177e4SLinus Torvalds {
137026af6552SDave Chinner 	struct xfs_buf		*bp = mp->m_sb_bp;
13711da177e4SLinus Torvalds 
137226af6552SDave Chinner 	xfs_buf_lock(bp);
13731da177e4SLinus Torvalds 	mp->m_sb_bp = NULL;
137426af6552SDave Chinner 	xfs_buf_relse(bp);
13751da177e4SLinus Torvalds }
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds /*
13781da177e4SLinus Torvalds  * Used to log changes to the superblock unit and width fields which could
1379e6957ea4SEric Sandeen  * be altered by the mount options, as well as any potential sb_features2
1380e6957ea4SEric Sandeen  * fixup. Only the first superblock is updated.
13811da177e4SLinus Torvalds  */
13827884bc86SChristoph Hellwig int
1383ee1c0908SDavid Chinner xfs_mount_log_sb(
13841da177e4SLinus Torvalds 	xfs_mount_t	*mp,
13851da177e4SLinus Torvalds 	__int64_t	fields)
13861da177e4SLinus Torvalds {
13871da177e4SLinus Torvalds 	xfs_trans_t	*tp;
1388e5720eecSDavid Chinner 	int		error;
13891da177e4SLinus Torvalds 
1390ee1c0908SDavid Chinner 	ASSERT(fields & (XFS_SB_UNIT | XFS_SB_WIDTH | XFS_SB_UUID |
13914b166de0SDavid Chinner 			 XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2 |
13924b166de0SDavid Chinner 			 XFS_SB_VERSIONNUM));
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds 	tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
13953d3c8b52SJie Liu 	error = xfs_trans_reserve(tp, &M_RES(mp)->tr_sb, 0, 0);
1396e5720eecSDavid Chinner 	if (error) {
13971da177e4SLinus Torvalds 		xfs_trans_cancel(tp, 0);
1398e5720eecSDavid Chinner 		return error;
13991da177e4SLinus Torvalds 	}
14001da177e4SLinus Torvalds 	xfs_mod_sb(tp, fields);
1401e5720eecSDavid Chinner 	error = xfs_trans_commit(tp, 0);
1402e5720eecSDavid Chinner 	return error;
14031da177e4SLinus Torvalds }
14048d280b98SDavid Chinner 
1405dda35b8fSChristoph Hellwig /*
1406dda35b8fSChristoph Hellwig  * If the underlying (data/log/rt) device is readonly, there are some
1407dda35b8fSChristoph Hellwig  * operations that cannot proceed.
1408dda35b8fSChristoph Hellwig  */
1409dda35b8fSChristoph Hellwig int
1410dda35b8fSChristoph Hellwig xfs_dev_is_read_only(
1411dda35b8fSChristoph Hellwig 	struct xfs_mount	*mp,
1412dda35b8fSChristoph Hellwig 	char			*message)
1413dda35b8fSChristoph Hellwig {
1414dda35b8fSChristoph Hellwig 	if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
1415dda35b8fSChristoph Hellwig 	    xfs_readonly_buftarg(mp->m_logdev_targp) ||
1416dda35b8fSChristoph Hellwig 	    (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
14170b932cccSDave Chinner 		xfs_notice(mp, "%s required on read-only device.", message);
14180b932cccSDave Chinner 		xfs_notice(mp, "write access unavailable, cannot proceed.");
1419dda35b8fSChristoph Hellwig 		return EROFS;
1420dda35b8fSChristoph Hellwig 	}
1421dda35b8fSChristoph Hellwig 	return 0;
1422dda35b8fSChristoph Hellwig }
14238d280b98SDavid Chinner 
14248d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
14258d280b98SDavid Chinner /*
14268d280b98SDavid Chinner  * Per-cpu incore superblock counters
14278d280b98SDavid Chinner  *
14288d280b98SDavid Chinner  * Simple concept, difficult implementation
14298d280b98SDavid Chinner  *
14308d280b98SDavid Chinner  * Basically, replace the incore superblock counters with a distributed per cpu
14318d280b98SDavid Chinner  * counter for contended fields (e.g.  free block count).
14328d280b98SDavid Chinner  *
14338d280b98SDavid Chinner  * Difficulties arise in that the incore sb is used for ENOSPC checking, and
14348d280b98SDavid Chinner  * hence needs to be accurately read when we are running low on space. Hence
14358d280b98SDavid Chinner  * there is a method to enable and disable the per-cpu counters based on how
14368d280b98SDavid Chinner  * much "stuff" is available in them.
14378d280b98SDavid Chinner  *
14388d280b98SDavid Chinner  * Basically, a counter is enabled if there is enough free resource to justify
14398d280b98SDavid Chinner  * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
14408d280b98SDavid Chinner  * ENOSPC), then we disable the counters to synchronise all callers and
14418d280b98SDavid Chinner  * re-distribute the available resources.
14428d280b98SDavid Chinner  *
14438d280b98SDavid Chinner  * If, once we redistributed the available resources, we still get a failure,
14448d280b98SDavid Chinner  * we disable the per-cpu counter and go through the slow path.
14458d280b98SDavid Chinner  *
14468d280b98SDavid Chinner  * The slow path is the current xfs_mod_incore_sb() function.  This means that
14479da096fdSMalcolm Parsons  * when we disable a per-cpu counter, we need to drain its resources back to
14488d280b98SDavid Chinner  * the global superblock. We do this after disabling the counter to prevent
14498d280b98SDavid Chinner  * more threads from queueing up on the counter.
14508d280b98SDavid Chinner  *
14518d280b98SDavid Chinner  * Essentially, this means that we still need a lock in the fast path to enable
14528d280b98SDavid Chinner  * synchronisation between the global counters and the per-cpu counters. This
14538d280b98SDavid Chinner  * is not a problem because the lock will be local to a CPU almost all the time
14548d280b98SDavid Chinner  * and have little contention except when we get to ENOSPC conditions.
14558d280b98SDavid Chinner  *
14568d280b98SDavid Chinner  * Basically, this lock becomes a barrier that enables us to lock out the fast
14578d280b98SDavid Chinner  * path while we do things like enabling and disabling counters and
14588d280b98SDavid Chinner  * synchronising the counters.
14598d280b98SDavid Chinner  *
14608d280b98SDavid Chinner  * Locking rules:
14618d280b98SDavid Chinner  *
14623685c2a1SEric Sandeen  * 	1. m_sb_lock before picking up per-cpu locks
14638d280b98SDavid Chinner  * 	2. per-cpu locks always picked up via for_each_online_cpu() order
14643685c2a1SEric Sandeen  * 	3. accurate counter sync requires m_sb_lock + per cpu locks
14658d280b98SDavid Chinner  * 	4. modifying per-cpu counters requires holding per-cpu lock
14663685c2a1SEric Sandeen  * 	5. modifying global counters requires holding m_sb_lock
14673685c2a1SEric Sandeen  *	6. enabling or disabling a counter requires holding the m_sb_lock
14688d280b98SDavid Chinner  *	   and _none_ of the per-cpu locks.
14698d280b98SDavid Chinner  *
14708d280b98SDavid Chinner  * Disabled counters are only ever re-enabled by a balance operation
14718d280b98SDavid Chinner  * that results in more free resources per CPU than a given threshold.
14728d280b98SDavid Chinner  * To ensure counters don't remain disabled, they are rebalanced when
14738d280b98SDavid Chinner  * the global resource goes above a higher threshold (i.e. some hysteresis
14748d280b98SDavid Chinner  * is present to prevent thrashing).
14758d280b98SDavid Chinner  */
1476e8234a68SDavid Chinner 
14775a67e4c5SChandra Seetharaman #ifdef CONFIG_HOTPLUG_CPU
1478e8234a68SDavid Chinner /*
1479e8234a68SDavid Chinner  * hot-plug CPU notifier support.
1480e8234a68SDavid Chinner  *
14815a67e4c5SChandra Seetharaman  * We need a notifier per filesystem as we need to be able to identify
14825a67e4c5SChandra Seetharaman  * the filesystem to balance the counters out. This is achieved by
14835a67e4c5SChandra Seetharaman  * having a notifier block embedded in the xfs_mount_t and doing pointer
14845a67e4c5SChandra Seetharaman  * magic to get the mount pointer from the notifier block address.
1485e8234a68SDavid Chinner  */
1486e8234a68SDavid Chinner STATIC int
1487e8234a68SDavid Chinner xfs_icsb_cpu_notify(
1488e8234a68SDavid Chinner 	struct notifier_block *nfb,
1489e8234a68SDavid Chinner 	unsigned long action,
1490e8234a68SDavid Chinner 	void *hcpu)
1491e8234a68SDavid Chinner {
1492e8234a68SDavid Chinner 	xfs_icsb_cnts_t *cntp;
1493e8234a68SDavid Chinner 	xfs_mount_t	*mp;
1494e8234a68SDavid Chinner 
1495e8234a68SDavid Chinner 	mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
1496e8234a68SDavid Chinner 	cntp = (xfs_icsb_cnts_t *)
1497e8234a68SDavid Chinner 			per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
1498e8234a68SDavid Chinner 	switch (action) {
1499e8234a68SDavid Chinner 	case CPU_UP_PREPARE:
15008bb78442SRafael J. Wysocki 	case CPU_UP_PREPARE_FROZEN:
1501e8234a68SDavid Chinner 		/* Easy Case - initialize the area and locks, and
1502e8234a68SDavid Chinner 		 * then rebalance when online does everything else for us. */
150301e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
1504e8234a68SDavid Chinner 		break;
1505e8234a68SDavid Chinner 	case CPU_ONLINE:
15068bb78442SRafael J. Wysocki 	case CPU_ONLINE_FROZEN:
150703135cf7SDavid Chinner 		xfs_icsb_lock(mp);
150845af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
150945af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
151045af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
151103135cf7SDavid Chinner 		xfs_icsb_unlock(mp);
1512e8234a68SDavid Chinner 		break;
1513e8234a68SDavid Chinner 	case CPU_DEAD:
15148bb78442SRafael J. Wysocki 	case CPU_DEAD_FROZEN:
1515e8234a68SDavid Chinner 		/* Disable all the counters, then fold the dead cpu's
1516e8234a68SDavid Chinner 		 * count into the total on the global superblock and
1517e8234a68SDavid Chinner 		 * re-enable the counters. */
151803135cf7SDavid Chinner 		xfs_icsb_lock(mp);
15193685c2a1SEric Sandeen 		spin_lock(&mp->m_sb_lock);
1520e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
1521e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
1522e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
1523e8234a68SDavid Chinner 
1524e8234a68SDavid Chinner 		mp->m_sb.sb_icount += cntp->icsb_icount;
1525e8234a68SDavid Chinner 		mp->m_sb.sb_ifree += cntp->icsb_ifree;
1526e8234a68SDavid Chinner 		mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
1527e8234a68SDavid Chinner 
152801e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
1529e8234a68SDavid Chinner 
153045af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter_locked(mp, XFS_SBS_ICOUNT, 0);
153145af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter_locked(mp, XFS_SBS_IFREE, 0);
153245af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter_locked(mp, XFS_SBS_FDBLOCKS, 0);
15333685c2a1SEric Sandeen 		spin_unlock(&mp->m_sb_lock);
153403135cf7SDavid Chinner 		xfs_icsb_unlock(mp);
1535e8234a68SDavid Chinner 		break;
1536e8234a68SDavid Chinner 	}
1537e8234a68SDavid Chinner 
1538e8234a68SDavid Chinner 	return NOTIFY_OK;
1539e8234a68SDavid Chinner }
15405a67e4c5SChandra Seetharaman #endif /* CONFIG_HOTPLUG_CPU */
1541e8234a68SDavid Chinner 
15428d280b98SDavid Chinner int
15438d280b98SDavid Chinner xfs_icsb_init_counters(
15448d280b98SDavid Chinner 	xfs_mount_t	*mp)
15458d280b98SDavid Chinner {
15468d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
15478d280b98SDavid Chinner 	int		i;
15488d280b98SDavid Chinner 
15498d280b98SDavid Chinner 	mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
15508d280b98SDavid Chinner 	if (mp->m_sb_cnts == NULL)
15518d280b98SDavid Chinner 		return -ENOMEM;
15528d280b98SDavid Chinner 
15538d280b98SDavid Chinner 	for_each_online_cpu(i) {
15548d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
155501e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
15568d280b98SDavid Chinner 	}
155720b64285SDavid Chinner 
155820b64285SDavid Chinner 	mutex_init(&mp->m_icsb_mutex);
155920b64285SDavid Chinner 
15608d280b98SDavid Chinner 	/*
15618d280b98SDavid Chinner 	 * start with all counters disabled so that the
15628d280b98SDavid Chinner 	 * initial balance kicks us off correctly
15638d280b98SDavid Chinner 	 */
15648d280b98SDavid Chinner 	mp->m_icsb_counters = -1;
156546677e67SRichard Weinberger 
156646677e67SRichard Weinberger #ifdef CONFIG_HOTPLUG_CPU
156746677e67SRichard Weinberger 	mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
156846677e67SRichard Weinberger 	mp->m_icsb_notifier.priority = 0;
156946677e67SRichard Weinberger 	register_hotcpu_notifier(&mp->m_icsb_notifier);
157046677e67SRichard Weinberger #endif /* CONFIG_HOTPLUG_CPU */
157146677e67SRichard Weinberger 
15728d280b98SDavid Chinner 	return 0;
15738d280b98SDavid Chinner }
15748d280b98SDavid Chinner 
15755478eeadSLachlan McIlroy void
15765478eeadSLachlan McIlroy xfs_icsb_reinit_counters(
15775478eeadSLachlan McIlroy 	xfs_mount_t	*mp)
15785478eeadSLachlan McIlroy {
15795478eeadSLachlan McIlroy 	xfs_icsb_lock(mp);
15805478eeadSLachlan McIlroy 	/*
15815478eeadSLachlan McIlroy 	 * start with all counters disabled so that the
15825478eeadSLachlan McIlroy 	 * initial balance kicks us off correctly
15835478eeadSLachlan McIlroy 	 */
15845478eeadSLachlan McIlroy 	mp->m_icsb_counters = -1;
158545af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
158645af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
158745af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
15885478eeadSLachlan McIlroy 	xfs_icsb_unlock(mp);
15895478eeadSLachlan McIlroy }
15905478eeadSLachlan McIlroy 
1591c962fb79SChristoph Hellwig void
15928d280b98SDavid Chinner xfs_icsb_destroy_counters(
15938d280b98SDavid Chinner 	xfs_mount_t	*mp)
15948d280b98SDavid Chinner {
1595e8234a68SDavid Chinner 	if (mp->m_sb_cnts) {
15965a67e4c5SChandra Seetharaman 		unregister_hotcpu_notifier(&mp->m_icsb_notifier);
15978d280b98SDavid Chinner 		free_percpu(mp->m_sb_cnts);
15988d280b98SDavid Chinner 	}
159903135cf7SDavid Chinner 	mutex_destroy(&mp->m_icsb_mutex);
1600e8234a68SDavid Chinner }
16018d280b98SDavid Chinner 
1602b8f82a4aSChristoph Hellwig STATIC void
160301e1b69cSDavid Chinner xfs_icsb_lock_cntr(
160401e1b69cSDavid Chinner 	xfs_icsb_cnts_t	*icsbp)
160501e1b69cSDavid Chinner {
160601e1b69cSDavid Chinner 	while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
160701e1b69cSDavid Chinner 		ndelay(1000);
160801e1b69cSDavid Chinner 	}
160901e1b69cSDavid Chinner }
161001e1b69cSDavid Chinner 
1611b8f82a4aSChristoph Hellwig STATIC void
161201e1b69cSDavid Chinner xfs_icsb_unlock_cntr(
161301e1b69cSDavid Chinner 	xfs_icsb_cnts_t	*icsbp)
161401e1b69cSDavid Chinner {
161501e1b69cSDavid Chinner 	clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
161601e1b69cSDavid Chinner }
161701e1b69cSDavid Chinner 
16188d280b98SDavid Chinner 
1619b8f82a4aSChristoph Hellwig STATIC void
16208d280b98SDavid Chinner xfs_icsb_lock_all_counters(
16218d280b98SDavid Chinner 	xfs_mount_t	*mp)
16228d280b98SDavid Chinner {
16238d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
16248d280b98SDavid Chinner 	int		i;
16258d280b98SDavid Chinner 
16268d280b98SDavid Chinner 	for_each_online_cpu(i) {
16278d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
162801e1b69cSDavid Chinner 		xfs_icsb_lock_cntr(cntp);
16298d280b98SDavid Chinner 	}
16308d280b98SDavid Chinner }
16318d280b98SDavid Chinner 
1632b8f82a4aSChristoph Hellwig STATIC void
16338d280b98SDavid Chinner xfs_icsb_unlock_all_counters(
16348d280b98SDavid Chinner 	xfs_mount_t	*mp)
16358d280b98SDavid Chinner {
16368d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
16378d280b98SDavid Chinner 	int		i;
16388d280b98SDavid Chinner 
16398d280b98SDavid Chinner 	for_each_online_cpu(i) {
16408d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
164101e1b69cSDavid Chinner 		xfs_icsb_unlock_cntr(cntp);
16428d280b98SDavid Chinner 	}
16438d280b98SDavid Chinner }
16448d280b98SDavid Chinner 
16458d280b98SDavid Chinner STATIC void
16468d280b98SDavid Chinner xfs_icsb_count(
16478d280b98SDavid Chinner 	xfs_mount_t	*mp,
16488d280b98SDavid Chinner 	xfs_icsb_cnts_t	*cnt,
16498d280b98SDavid Chinner 	int		flags)
16508d280b98SDavid Chinner {
16518d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
16528d280b98SDavid Chinner 	int		i;
16538d280b98SDavid Chinner 
16548d280b98SDavid Chinner 	memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
16558d280b98SDavid Chinner 
16568d280b98SDavid Chinner 	if (!(flags & XFS_ICSB_LAZY_COUNT))
16578d280b98SDavid Chinner 		xfs_icsb_lock_all_counters(mp);
16588d280b98SDavid Chinner 
16598d280b98SDavid Chinner 	for_each_online_cpu(i) {
16608d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
16618d280b98SDavid Chinner 		cnt->icsb_icount += cntp->icsb_icount;
16628d280b98SDavid Chinner 		cnt->icsb_ifree += cntp->icsb_ifree;
16638d280b98SDavid Chinner 		cnt->icsb_fdblocks += cntp->icsb_fdblocks;
16648d280b98SDavid Chinner 	}
16658d280b98SDavid Chinner 
16668d280b98SDavid Chinner 	if (!(flags & XFS_ICSB_LAZY_COUNT))
16678d280b98SDavid Chinner 		xfs_icsb_unlock_all_counters(mp);
16688d280b98SDavid Chinner }
16698d280b98SDavid Chinner 
16708d280b98SDavid Chinner STATIC int
16718d280b98SDavid Chinner xfs_icsb_counter_disabled(
16728d280b98SDavid Chinner 	xfs_mount_t	*mp,
16738d280b98SDavid Chinner 	xfs_sb_field_t	field)
16748d280b98SDavid Chinner {
16758d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
16768d280b98SDavid Chinner 	return test_bit(field, &mp->m_icsb_counters);
16778d280b98SDavid Chinner }
16788d280b98SDavid Chinner 
167936fbe6e6SDavid Chinner STATIC void
16808d280b98SDavid Chinner xfs_icsb_disable_counter(
16818d280b98SDavid Chinner 	xfs_mount_t	*mp,
16828d280b98SDavid Chinner 	xfs_sb_field_t	field)
16838d280b98SDavid Chinner {
16848d280b98SDavid Chinner 	xfs_icsb_cnts_t	cnt;
16858d280b98SDavid Chinner 
16868d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
16878d280b98SDavid Chinner 
168820b64285SDavid Chinner 	/*
168920b64285SDavid Chinner 	 * If we are already disabled, then there is nothing to do
169020b64285SDavid Chinner 	 * here. We check before locking all the counters to avoid
169120b64285SDavid Chinner 	 * the expensive lock operation when being called in the
169220b64285SDavid Chinner 	 * slow path and the counter is already disabled. This is
169320b64285SDavid Chinner 	 * safe because the only time we set or clear this state is under
169420b64285SDavid Chinner 	 * the m_icsb_mutex.
169520b64285SDavid Chinner 	 */
169620b64285SDavid Chinner 	if (xfs_icsb_counter_disabled(mp, field))
169736fbe6e6SDavid Chinner 		return;
169820b64285SDavid Chinner 
16998d280b98SDavid Chinner 	xfs_icsb_lock_all_counters(mp);
17008d280b98SDavid Chinner 	if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
17018d280b98SDavid Chinner 		/* drain back to superblock */
17028d280b98SDavid Chinner 
1703ce46193bSChristoph Hellwig 		xfs_icsb_count(mp, &cnt, XFS_ICSB_LAZY_COUNT);
17048d280b98SDavid Chinner 		switch(field) {
17058d280b98SDavid Chinner 		case XFS_SBS_ICOUNT:
17068d280b98SDavid Chinner 			mp->m_sb.sb_icount = cnt.icsb_icount;
17078d280b98SDavid Chinner 			break;
17088d280b98SDavid Chinner 		case XFS_SBS_IFREE:
17098d280b98SDavid Chinner 			mp->m_sb.sb_ifree = cnt.icsb_ifree;
17108d280b98SDavid Chinner 			break;
17118d280b98SDavid Chinner 		case XFS_SBS_FDBLOCKS:
17128d280b98SDavid Chinner 			mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
17138d280b98SDavid Chinner 			break;
17148d280b98SDavid Chinner 		default:
17158d280b98SDavid Chinner 			BUG();
17168d280b98SDavid Chinner 		}
17178d280b98SDavid Chinner 	}
17188d280b98SDavid Chinner 
17198d280b98SDavid Chinner 	xfs_icsb_unlock_all_counters(mp);
17208d280b98SDavid Chinner }
17218d280b98SDavid Chinner 
17228d280b98SDavid Chinner STATIC void
17238d280b98SDavid Chinner xfs_icsb_enable_counter(
17248d280b98SDavid Chinner 	xfs_mount_t	*mp,
17258d280b98SDavid Chinner 	xfs_sb_field_t	field,
17268d280b98SDavid Chinner 	uint64_t	count,
17278d280b98SDavid Chinner 	uint64_t	resid)
17288d280b98SDavid Chinner {
17298d280b98SDavid Chinner 	xfs_icsb_cnts_t	*cntp;
17308d280b98SDavid Chinner 	int		i;
17318d280b98SDavid Chinner 
17328d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
17338d280b98SDavid Chinner 
17348d280b98SDavid Chinner 	xfs_icsb_lock_all_counters(mp);
17358d280b98SDavid Chinner 	for_each_online_cpu(i) {
17368d280b98SDavid Chinner 		cntp = per_cpu_ptr(mp->m_sb_cnts, i);
17378d280b98SDavid Chinner 		switch (field) {
17388d280b98SDavid Chinner 		case XFS_SBS_ICOUNT:
17398d280b98SDavid Chinner 			cntp->icsb_icount = count + resid;
17408d280b98SDavid Chinner 			break;
17418d280b98SDavid Chinner 		case XFS_SBS_IFREE:
17428d280b98SDavid Chinner 			cntp->icsb_ifree = count + resid;
17438d280b98SDavid Chinner 			break;
17448d280b98SDavid Chinner 		case XFS_SBS_FDBLOCKS:
17458d280b98SDavid Chinner 			cntp->icsb_fdblocks = count + resid;
17468d280b98SDavid Chinner 			break;
17478d280b98SDavid Chinner 		default:
17488d280b98SDavid Chinner 			BUG();
17498d280b98SDavid Chinner 			break;
17508d280b98SDavid Chinner 		}
17518d280b98SDavid Chinner 		resid = 0;
17528d280b98SDavid Chinner 	}
17538d280b98SDavid Chinner 	clear_bit(field, &mp->m_icsb_counters);
17548d280b98SDavid Chinner 	xfs_icsb_unlock_all_counters(mp);
17558d280b98SDavid Chinner }
17568d280b98SDavid Chinner 
1757dbcabad1SDavid Chinner void
1758d4d90b57SChristoph Hellwig xfs_icsb_sync_counters_locked(
17598d280b98SDavid Chinner 	xfs_mount_t	*mp,
17608d280b98SDavid Chinner 	int		flags)
17618d280b98SDavid Chinner {
17628d280b98SDavid Chinner 	xfs_icsb_cnts_t	cnt;
17638d280b98SDavid Chinner 
17648d280b98SDavid Chinner 	xfs_icsb_count(mp, &cnt, flags);
17658d280b98SDavid Chinner 
17668d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
17678d280b98SDavid Chinner 		mp->m_sb.sb_icount = cnt.icsb_icount;
17688d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
17698d280b98SDavid Chinner 		mp->m_sb.sb_ifree = cnt.icsb_ifree;
17708d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
17718d280b98SDavid Chinner 		mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
17728d280b98SDavid Chinner }
17738d280b98SDavid Chinner 
17748d280b98SDavid Chinner /*
17758d280b98SDavid Chinner  * Accurate update of per-cpu counters to incore superblock
17768d280b98SDavid Chinner  */
1777d4d90b57SChristoph Hellwig void
17788d280b98SDavid Chinner xfs_icsb_sync_counters(
1779d4d90b57SChristoph Hellwig 	xfs_mount_t	*mp,
1780d4d90b57SChristoph Hellwig 	int		flags)
17818d280b98SDavid Chinner {
1782d4d90b57SChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
1783d4d90b57SChristoph Hellwig 	xfs_icsb_sync_counters_locked(mp, flags);
1784d4d90b57SChristoph Hellwig 	spin_unlock(&mp->m_sb_lock);
17858d280b98SDavid Chinner }
17868d280b98SDavid Chinner 
17878d280b98SDavid Chinner /*
17888d280b98SDavid Chinner  * Balance and enable/disable counters as necessary.
17898d280b98SDavid Chinner  *
179020b64285SDavid Chinner  * Thresholds for re-enabling counters are somewhat magic.  inode counts are
179120b64285SDavid Chinner  * chosen to be the same number as single on disk allocation chunk per CPU, and
179220b64285SDavid Chinner  * free blocks is something far enough zero that we aren't going thrash when we
179320b64285SDavid Chinner  * get near ENOSPC. We also need to supply a minimum we require per cpu to
179420b64285SDavid Chinner  * prevent looping endlessly when xfs_alloc_space asks for more than will
179520b64285SDavid Chinner  * be distributed to a single CPU but each CPU has enough blocks to be
179620b64285SDavid Chinner  * reenabled.
179720b64285SDavid Chinner  *
179820b64285SDavid Chinner  * Note that we can be called when counters are already disabled.
179920b64285SDavid Chinner  * xfs_icsb_disable_counter() optimises the counter locking in this case to
180020b64285SDavid Chinner  * prevent locking every per-cpu counter needlessly.
18018d280b98SDavid Chinner  */
180220b64285SDavid Chinner 
180320b64285SDavid Chinner #define XFS_ICSB_INO_CNTR_REENABLE	(uint64_t)64
18044be536deSDavid Chinner #define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \
180520b64285SDavid Chinner 		(uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp))
18068d280b98SDavid Chinner STATIC void
180745af6c6dSChristoph Hellwig xfs_icsb_balance_counter_locked(
18088d280b98SDavid Chinner 	xfs_mount_t	*mp,
18098d280b98SDavid Chinner 	xfs_sb_field_t  field,
181020b64285SDavid Chinner 	int		min_per_cpu)
18118d280b98SDavid Chinner {
18126fdf8cccSNathan Scott 	uint64_t	count, resid;
18138d280b98SDavid Chinner 	int		weight = num_online_cpus();
181420b64285SDavid Chinner 	uint64_t	min = (uint64_t)min_per_cpu;
18158d280b98SDavid Chinner 
18168d280b98SDavid Chinner 	/* disable counter and sync counter */
18178d280b98SDavid Chinner 	xfs_icsb_disable_counter(mp, field);
18188d280b98SDavid Chinner 
18198d280b98SDavid Chinner 	/* update counters  - first CPU gets residual*/
18208d280b98SDavid Chinner 	switch (field) {
18218d280b98SDavid Chinner 	case XFS_SBS_ICOUNT:
18228d280b98SDavid Chinner 		count = mp->m_sb.sb_icount;
18238d280b98SDavid Chinner 		resid = do_div(count, weight);
182420b64285SDavid Chinner 		if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
182545af6c6dSChristoph Hellwig 			return;
18268d280b98SDavid Chinner 		break;
18278d280b98SDavid Chinner 	case XFS_SBS_IFREE:
18288d280b98SDavid Chinner 		count = mp->m_sb.sb_ifree;
18298d280b98SDavid Chinner 		resid = do_div(count, weight);
183020b64285SDavid Chinner 		if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
183145af6c6dSChristoph Hellwig 			return;
18328d280b98SDavid Chinner 		break;
18338d280b98SDavid Chinner 	case XFS_SBS_FDBLOCKS:
18348d280b98SDavid Chinner 		count = mp->m_sb.sb_fdblocks;
18358d280b98SDavid Chinner 		resid = do_div(count, weight);
183620b64285SDavid Chinner 		if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp)))
183745af6c6dSChristoph Hellwig 			return;
18388d280b98SDavid Chinner 		break;
18398d280b98SDavid Chinner 	default:
18408d280b98SDavid Chinner 		BUG();
18416fdf8cccSNathan Scott 		count = resid = 0;	/* quiet, gcc */
18428d280b98SDavid Chinner 		break;
18438d280b98SDavid Chinner 	}
18448d280b98SDavid Chinner 
18458d280b98SDavid Chinner 	xfs_icsb_enable_counter(mp, field, count, resid);
184645af6c6dSChristoph Hellwig }
184745af6c6dSChristoph Hellwig 
184845af6c6dSChristoph Hellwig STATIC void
184945af6c6dSChristoph Hellwig xfs_icsb_balance_counter(
185045af6c6dSChristoph Hellwig 	xfs_mount_t	*mp,
185145af6c6dSChristoph Hellwig 	xfs_sb_field_t  fields,
185245af6c6dSChristoph Hellwig 	int		min_per_cpu)
185345af6c6dSChristoph Hellwig {
185445af6c6dSChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
185545af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter_locked(mp, fields, min_per_cpu);
18563685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
18578d280b98SDavid Chinner }
18588d280b98SDavid Chinner 
18591b040712SChristoph Hellwig int
186020b64285SDavid Chinner xfs_icsb_modify_counters(
18618d280b98SDavid Chinner 	xfs_mount_t	*mp,
18628d280b98SDavid Chinner 	xfs_sb_field_t	field,
186320f4ebf2SDavid Chinner 	int64_t		delta,
186420b64285SDavid Chinner 	int		rsvd)
18658d280b98SDavid Chinner {
18668d280b98SDavid Chinner 	xfs_icsb_cnts_t	*icsbp;
18678d280b98SDavid Chinner 	long long	lcounter;	/* long counter for 64 bit fields */
18687a9e02d6SChristoph Lameter 	int		ret = 0;
18698d280b98SDavid Chinner 
187020b64285SDavid Chinner 	might_sleep();
18718d280b98SDavid Chinner again:
18727a9e02d6SChristoph Lameter 	preempt_disable();
18737a9e02d6SChristoph Lameter 	icsbp = this_cpu_ptr(mp->m_sb_cnts);
187420b64285SDavid Chinner 
187520b64285SDavid Chinner 	/*
187620b64285SDavid Chinner 	 * if the counter is disabled, go to slow path
187720b64285SDavid Chinner 	 */
18788d280b98SDavid Chinner 	if (unlikely(xfs_icsb_counter_disabled(mp, field)))
18798d280b98SDavid Chinner 		goto slow_path;
188020b64285SDavid Chinner 	xfs_icsb_lock_cntr(icsbp);
188120b64285SDavid Chinner 	if (unlikely(xfs_icsb_counter_disabled(mp, field))) {
188220b64285SDavid Chinner 		xfs_icsb_unlock_cntr(icsbp);
188320b64285SDavid Chinner 		goto slow_path;
188420b64285SDavid Chinner 	}
18858d280b98SDavid Chinner 
18868d280b98SDavid Chinner 	switch (field) {
18878d280b98SDavid Chinner 	case XFS_SBS_ICOUNT:
18888d280b98SDavid Chinner 		lcounter = icsbp->icsb_icount;
18898d280b98SDavid Chinner 		lcounter += delta;
18908d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
189120b64285SDavid Chinner 			goto balance_counter;
18928d280b98SDavid Chinner 		icsbp->icsb_icount = lcounter;
18938d280b98SDavid Chinner 		break;
18948d280b98SDavid Chinner 
18958d280b98SDavid Chinner 	case XFS_SBS_IFREE:
18968d280b98SDavid Chinner 		lcounter = icsbp->icsb_ifree;
18978d280b98SDavid Chinner 		lcounter += delta;
18988d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
189920b64285SDavid Chinner 			goto balance_counter;
19008d280b98SDavid Chinner 		icsbp->icsb_ifree = lcounter;
19018d280b98SDavid Chinner 		break;
19028d280b98SDavid Chinner 
19038d280b98SDavid Chinner 	case XFS_SBS_FDBLOCKS:
19048d280b98SDavid Chinner 		BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
19058d280b98SDavid Chinner 
19064be536deSDavid Chinner 		lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
19078d280b98SDavid Chinner 		lcounter += delta;
19088d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
190920b64285SDavid Chinner 			goto balance_counter;
19104be536deSDavid Chinner 		icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
19118d280b98SDavid Chinner 		break;
19128d280b98SDavid Chinner 	default:
19138d280b98SDavid Chinner 		BUG();
19148d280b98SDavid Chinner 		break;
19158d280b98SDavid Chinner 	}
191601e1b69cSDavid Chinner 	xfs_icsb_unlock_cntr(icsbp);
19177a9e02d6SChristoph Lameter 	preempt_enable();
19188d280b98SDavid Chinner 	return 0;
19198d280b98SDavid Chinner 
19208d280b98SDavid Chinner slow_path:
19217a9e02d6SChristoph Lameter 	preempt_enable();
192220b64285SDavid Chinner 
192320b64285SDavid Chinner 	/*
192420b64285SDavid Chinner 	 * serialise with a mutex so we don't burn lots of cpu on
192520b64285SDavid Chinner 	 * the superblock lock. We still need to hold the superblock
192620b64285SDavid Chinner 	 * lock, however, when we modify the global structures.
192720b64285SDavid Chinner 	 */
192803135cf7SDavid Chinner 	xfs_icsb_lock(mp);
192920b64285SDavid Chinner 
193020b64285SDavid Chinner 	/*
193120b64285SDavid Chinner 	 * Now running atomically.
193220b64285SDavid Chinner 	 *
193320b64285SDavid Chinner 	 * If the counter is enabled, someone has beaten us to rebalancing.
193420b64285SDavid Chinner 	 * Drop the lock and try again in the fast path....
193520b64285SDavid Chinner 	 */
193620b64285SDavid Chinner 	if (!(xfs_icsb_counter_disabled(mp, field))) {
193703135cf7SDavid Chinner 		xfs_icsb_unlock(mp);
193820b64285SDavid Chinner 		goto again;
193920b64285SDavid Chinner 	}
194020b64285SDavid Chinner 
194120b64285SDavid Chinner 	/*
194220b64285SDavid Chinner 	 * The counter is currently disabled. Because we are
194320b64285SDavid Chinner 	 * running atomically here, we know a rebalance cannot
194420b64285SDavid Chinner 	 * be in progress. Hence we can go straight to operating
194520b64285SDavid Chinner 	 * on the global superblock. We do not call xfs_mod_incore_sb()
19463685c2a1SEric Sandeen 	 * here even though we need to get the m_sb_lock. Doing so
194720b64285SDavid Chinner 	 * will cause us to re-enter this function and deadlock.
19483685c2a1SEric Sandeen 	 * Hence we get the m_sb_lock ourselves and then call
194920b64285SDavid Chinner 	 * xfs_mod_incore_sb_unlocked() as the unlocked path operates
195020b64285SDavid Chinner 	 * directly on the global counters.
195120b64285SDavid Chinner 	 */
19523685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
195320b64285SDavid Chinner 	ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
19543685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
195520b64285SDavid Chinner 
195620b64285SDavid Chinner 	/*
195720b64285SDavid Chinner 	 * Now that we've modified the global superblock, we
195820b64285SDavid Chinner 	 * may be able to re-enable the distributed counters
195920b64285SDavid Chinner 	 * (e.g. lots of space just got freed). After that
196020b64285SDavid Chinner 	 * we are done.
196120b64285SDavid Chinner 	 */
196220b64285SDavid Chinner 	if (ret != ENOSPC)
196345af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, field, 0);
196403135cf7SDavid Chinner 	xfs_icsb_unlock(mp);
196520b64285SDavid Chinner 	return ret;
196620b64285SDavid Chinner 
196720b64285SDavid Chinner balance_counter:
196801e1b69cSDavid Chinner 	xfs_icsb_unlock_cntr(icsbp);
19697a9e02d6SChristoph Lameter 	preempt_enable();
19708d280b98SDavid Chinner 
197120b64285SDavid Chinner 	/*
197220b64285SDavid Chinner 	 * We may have multiple threads here if multiple per-cpu
197320b64285SDavid Chinner 	 * counters run dry at the same time. This will mean we can
197420b64285SDavid Chinner 	 * do more balances than strictly necessary but it is not
197520b64285SDavid Chinner 	 * the common slowpath case.
197620b64285SDavid Chinner 	 */
197703135cf7SDavid Chinner 	xfs_icsb_lock(mp);
197820b64285SDavid Chinner 
197920b64285SDavid Chinner 	/*
198020b64285SDavid Chinner 	 * running atomically.
198120b64285SDavid Chinner 	 *
198220b64285SDavid Chinner 	 * This will leave the counter in the correct state for future
198320b64285SDavid Chinner 	 * accesses. After the rebalance, we simply try again and our retry
198420b64285SDavid Chinner 	 * will either succeed through the fast path or slow path without
198520b64285SDavid Chinner 	 * another balance operation being required.
198620b64285SDavid Chinner 	 */
198745af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, field, delta);
198803135cf7SDavid Chinner 	xfs_icsb_unlock(mp);
19898d280b98SDavid Chinner 	goto again;
19908d280b98SDavid Chinner }
19918d280b98SDavid Chinner 
19928d280b98SDavid Chinner #endif
1993