xref: /linux/fs/xfs/xfs_mount.c (revision 83e782e1a1cc0159888e58e14dfc8f3289663338)
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"
201da177e4SLinus Torvalds #include "xfs_types.h"
21a844f451SNathan Scott #include "xfs_bit.h"
221da177e4SLinus Torvalds #include "xfs_log.h"
23a844f451SNathan Scott #include "xfs_inum.h"
241da177e4SLinus Torvalds #include "xfs_trans.h"
25211e4d43SChristoph Hellwig #include "xfs_trans_priv.h"
261da177e4SLinus Torvalds #include "xfs_sb.h"
271da177e4SLinus Torvalds #include "xfs_ag.h"
281da177e4SLinus Torvalds #include "xfs_dir2.h"
291da177e4SLinus Torvalds #include "xfs_mount.h"
301da177e4SLinus Torvalds #include "xfs_bmap_btree.h"
31a844f451SNathan Scott #include "xfs_alloc_btree.h"
321da177e4SLinus Torvalds #include "xfs_ialloc_btree.h"
331da177e4SLinus Torvalds #include "xfs_dinode.h"
341da177e4SLinus Torvalds #include "xfs_inode.h"
35a844f451SNathan Scott #include "xfs_btree.h"
36a844f451SNathan Scott #include "xfs_ialloc.h"
371da177e4SLinus Torvalds #include "xfs_alloc.h"
381da177e4SLinus Torvalds #include "xfs_rtalloc.h"
391da177e4SLinus Torvalds #include "xfs_bmap.h"
401da177e4SLinus Torvalds #include "xfs_error.h"
411da177e4SLinus Torvalds #include "xfs_quota.h"
421da177e4SLinus Torvalds #include "xfs_fsops.h"
4343355099SChristoph Hellwig #include "xfs_utils.h"
440b1b213fSChristoph Hellwig #include "xfs_trace.h"
456d8b79cfSDave Chinner #include "xfs_icache.h"
4604a1e6c5SDave Chinner #include "xfs_cksum.h"
4704a1e6c5SDave Chinner #include "xfs_buf_item.h"
480b1b213fSChristoph Hellwig 
491da177e4SLinus Torvalds 
508d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
5120f4ebf2SDavid Chinner STATIC void	xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t,
5245af6c6dSChristoph Hellwig 						int);
5345af6c6dSChristoph Hellwig STATIC void	xfs_icsb_balance_counter_locked(xfs_mount_t *, xfs_sb_field_t,
5445af6c6dSChristoph Hellwig 						int);
5536fbe6e6SDavid Chinner STATIC void	xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
568d280b98SDavid Chinner #else
578d280b98SDavid Chinner 
5845af6c6dSChristoph Hellwig #define xfs_icsb_balance_counter(mp, a, b)		do { } while (0)
5945af6c6dSChristoph Hellwig #define xfs_icsb_balance_counter_locked(mp, a, b)	do { } while (0)
608d280b98SDavid Chinner #endif
618d280b98SDavid Chinner 
621df84c93SChristoph Hellwig static const struct {
631da177e4SLinus Torvalds 	short offset;
641da177e4SLinus Torvalds 	short type;	/* 0 = integer
651da177e4SLinus Torvalds 			 * 1 = binary / string (no translation)
661da177e4SLinus Torvalds 			 */
671da177e4SLinus Torvalds } xfs_sb_info[] = {
681da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_magicnum),   0 },
691da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_blocksize),  0 },
701da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_dblocks),    0 },
711da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rblocks),    0 },
721da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rextents),   0 },
731da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_uuid),       1 },
741da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logstart),   0 },
751da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rootino),    0 },
761da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rbmino),     0 },
771da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rsumino),    0 },
781da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rextsize),   0 },
791da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_agblocks),   0 },
801da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_agcount),    0 },
811da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rbmblocks),  0 },
821da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logblocks),  0 },
831da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_versionnum), 0 },
841da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_sectsize),   0 },
851da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inodesize),  0 },
861da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inopblock),  0 },
871da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_fname[0]),   1 },
881da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_blocklog),   0 },
891da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_sectlog),    0 },
901da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inodelog),   0 },
911da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inopblog),   0 },
921da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_agblklog),   0 },
931da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rextslog),   0 },
941da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inprogress), 0 },
951da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_imax_pct),   0 },
961da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_icount),     0 },
971da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_ifree),      0 },
981da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_fdblocks),   0 },
991da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_frextents),  0 },
1001da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_uquotino),   0 },
1011da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_gquotino),   0 },
1021da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_qflags),     0 },
1031da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_flags),      0 },
1041da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_shared_vn),  0 },
1051da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
1061da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_unit),	 0 },
1071da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_width),	 0 },
1081da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_dirblklog),	 0 },
1091da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logsectlog), 0 },
1101da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logsectsize),0 },
1111da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logsunit),	 0 },
1121da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_features2),	 0 },
113ee1c0908SDavid Chinner     { offsetof(xfs_sb_t, sb_bad_features2), 0 },
11404a1e6c5SDave Chinner     { offsetof(xfs_sb_t, sb_features_compat), 0 },
11504a1e6c5SDave Chinner     { offsetof(xfs_sb_t, sb_features_ro_compat), 0 },
11604a1e6c5SDave Chinner     { offsetof(xfs_sb_t, sb_features_incompat), 0 },
117e721f504SDave Chinner     { offsetof(xfs_sb_t, sb_features_log_incompat), 0 },
11804a1e6c5SDave Chinner     { offsetof(xfs_sb_t, sb_crc),	 0 },
119e721f504SDave Chinner     { offsetof(xfs_sb_t, sb_pad),	 0 },
12004a1e6c5SDave Chinner     { offsetof(xfs_sb_t, sb_pquotino),	 0 },
12104a1e6c5SDave Chinner     { offsetof(xfs_sb_t, sb_lsn),	 0 },
1221da177e4SLinus Torvalds     { sizeof(xfs_sb_t),			 0 }
1231da177e4SLinus Torvalds };
1241da177e4SLinus Torvalds 
12527174203SChristoph Hellwig static DEFINE_MUTEX(xfs_uuid_table_mutex);
12627174203SChristoph Hellwig static int xfs_uuid_table_size;
12727174203SChristoph Hellwig static uuid_t *xfs_uuid_table;
12827174203SChristoph Hellwig 
12927174203SChristoph Hellwig /*
13027174203SChristoph Hellwig  * See if the UUID is unique among mounted XFS filesystems.
13127174203SChristoph Hellwig  * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
13227174203SChristoph Hellwig  */
13327174203SChristoph Hellwig STATIC int
13427174203SChristoph Hellwig xfs_uuid_mount(
13527174203SChristoph Hellwig 	struct xfs_mount	*mp)
13627174203SChristoph Hellwig {
13727174203SChristoph Hellwig 	uuid_t			*uuid = &mp->m_sb.sb_uuid;
13827174203SChristoph Hellwig 	int			hole, i;
13927174203SChristoph Hellwig 
14027174203SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_NOUUID)
14127174203SChristoph Hellwig 		return 0;
14227174203SChristoph Hellwig 
14327174203SChristoph Hellwig 	if (uuid_is_nil(uuid)) {
1440b932cccSDave Chinner 		xfs_warn(mp, "Filesystem has nil UUID - can't mount");
14527174203SChristoph Hellwig 		return XFS_ERROR(EINVAL);
14627174203SChristoph Hellwig 	}
14727174203SChristoph Hellwig 
14827174203SChristoph Hellwig 	mutex_lock(&xfs_uuid_table_mutex);
14927174203SChristoph Hellwig 	for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) {
15027174203SChristoph Hellwig 		if (uuid_is_nil(&xfs_uuid_table[i])) {
15127174203SChristoph Hellwig 			hole = i;
15227174203SChristoph Hellwig 			continue;
15327174203SChristoph Hellwig 		}
15427174203SChristoph Hellwig 		if (uuid_equal(uuid, &xfs_uuid_table[i]))
15527174203SChristoph Hellwig 			goto out_duplicate;
15627174203SChristoph Hellwig 	}
15727174203SChristoph Hellwig 
15827174203SChristoph Hellwig 	if (hole < 0) {
15927174203SChristoph Hellwig 		xfs_uuid_table = kmem_realloc(xfs_uuid_table,
16027174203SChristoph Hellwig 			(xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table),
16127174203SChristoph Hellwig 			xfs_uuid_table_size  * sizeof(*xfs_uuid_table),
16227174203SChristoph Hellwig 			KM_SLEEP);
16327174203SChristoph Hellwig 		hole = xfs_uuid_table_size++;
16427174203SChristoph Hellwig 	}
16527174203SChristoph Hellwig 	xfs_uuid_table[hole] = *uuid;
16627174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
16727174203SChristoph Hellwig 
16827174203SChristoph Hellwig 	return 0;
16927174203SChristoph Hellwig 
17027174203SChristoph Hellwig  out_duplicate:
17127174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
172021000e5SMitsuo Hayasaka 	xfs_warn(mp, "Filesystem has duplicate UUID %pU - can't mount", uuid);
17327174203SChristoph Hellwig 	return XFS_ERROR(EINVAL);
17427174203SChristoph Hellwig }
17527174203SChristoph Hellwig 
17627174203SChristoph Hellwig STATIC void
17727174203SChristoph Hellwig xfs_uuid_unmount(
17827174203SChristoph Hellwig 	struct xfs_mount	*mp)
17927174203SChristoph Hellwig {
18027174203SChristoph Hellwig 	uuid_t			*uuid = &mp->m_sb.sb_uuid;
18127174203SChristoph Hellwig 	int			i;
18227174203SChristoph Hellwig 
18327174203SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_NOUUID)
18427174203SChristoph Hellwig 		return;
18527174203SChristoph Hellwig 
18627174203SChristoph Hellwig 	mutex_lock(&xfs_uuid_table_mutex);
18727174203SChristoph Hellwig 	for (i = 0; i < xfs_uuid_table_size; i++) {
18827174203SChristoph Hellwig 		if (uuid_is_nil(&xfs_uuid_table[i]))
18927174203SChristoph Hellwig 			continue;
19027174203SChristoph Hellwig 		if (!uuid_equal(uuid, &xfs_uuid_table[i]))
19127174203SChristoph Hellwig 			continue;
19227174203SChristoph Hellwig 		memset(&xfs_uuid_table[i], 0, sizeof(uuid_t));
19327174203SChristoph Hellwig 		break;
19427174203SChristoph Hellwig 	}
19527174203SChristoph Hellwig 	ASSERT(i < xfs_uuid_table_size);
19627174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
19727174203SChristoph Hellwig }
19827174203SChristoph Hellwig 
19927174203SChristoph Hellwig 
2001da177e4SLinus Torvalds /*
2010fa800fbSDave Chinner  * Reference counting access wrappers to the perag structures.
202e176579eSDave Chinner  * Because we never free per-ag structures, the only thing we
203e176579eSDave Chinner  * have to protect against changes is the tree structure itself.
2040fa800fbSDave Chinner  */
2050fa800fbSDave Chinner struct xfs_perag *
2060fa800fbSDave Chinner xfs_perag_get(struct xfs_mount *mp, xfs_agnumber_t agno)
2070fa800fbSDave Chinner {
2080fa800fbSDave Chinner 	struct xfs_perag	*pag;
2090fa800fbSDave Chinner 	int			ref = 0;
2100fa800fbSDave Chinner 
211e176579eSDave Chinner 	rcu_read_lock();
2120fa800fbSDave Chinner 	pag = radix_tree_lookup(&mp->m_perag_tree, agno);
2130fa800fbSDave Chinner 	if (pag) {
2140fa800fbSDave Chinner 		ASSERT(atomic_read(&pag->pag_ref) >= 0);
2150fa800fbSDave Chinner 		ref = atomic_inc_return(&pag->pag_ref);
2160fa800fbSDave Chinner 	}
217e176579eSDave Chinner 	rcu_read_unlock();
2180fa800fbSDave Chinner 	trace_xfs_perag_get(mp, agno, ref, _RET_IP_);
2190fa800fbSDave Chinner 	return pag;
2200fa800fbSDave Chinner }
2210fa800fbSDave Chinner 
22265d0f205SDave Chinner /*
22365d0f205SDave Chinner  * search from @first to find the next perag with the given tag set.
22465d0f205SDave Chinner  */
22565d0f205SDave Chinner struct xfs_perag *
22665d0f205SDave Chinner xfs_perag_get_tag(
22765d0f205SDave Chinner 	struct xfs_mount	*mp,
22865d0f205SDave Chinner 	xfs_agnumber_t		first,
22965d0f205SDave Chinner 	int			tag)
23065d0f205SDave Chinner {
23165d0f205SDave Chinner 	struct xfs_perag	*pag;
23265d0f205SDave Chinner 	int			found;
23365d0f205SDave Chinner 	int			ref;
23465d0f205SDave Chinner 
23565d0f205SDave Chinner 	rcu_read_lock();
23665d0f205SDave Chinner 	found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
23765d0f205SDave Chinner 					(void **)&pag, first, 1, tag);
23865d0f205SDave Chinner 	if (found <= 0) {
23965d0f205SDave Chinner 		rcu_read_unlock();
24065d0f205SDave Chinner 		return NULL;
24165d0f205SDave Chinner 	}
24265d0f205SDave Chinner 	ref = atomic_inc_return(&pag->pag_ref);
24365d0f205SDave Chinner 	rcu_read_unlock();
24465d0f205SDave Chinner 	trace_xfs_perag_get_tag(mp, pag->pag_agno, ref, _RET_IP_);
24565d0f205SDave Chinner 	return pag;
24665d0f205SDave Chinner }
24765d0f205SDave Chinner 
2480fa800fbSDave Chinner void
2490fa800fbSDave Chinner xfs_perag_put(struct xfs_perag *pag)
2500fa800fbSDave Chinner {
2510fa800fbSDave Chinner 	int	ref;
2520fa800fbSDave Chinner 
2530fa800fbSDave Chinner 	ASSERT(atomic_read(&pag->pag_ref) > 0);
2540fa800fbSDave Chinner 	ref = atomic_dec_return(&pag->pag_ref);
2550fa800fbSDave Chinner 	trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_);
2560fa800fbSDave Chinner }
2570fa800fbSDave Chinner 
258e176579eSDave Chinner STATIC void
259e176579eSDave Chinner __xfs_free_perag(
260e176579eSDave Chinner 	struct rcu_head	*head)
261e176579eSDave Chinner {
262e176579eSDave Chinner 	struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head);
263e176579eSDave Chinner 
264e176579eSDave Chinner 	ASSERT(atomic_read(&pag->pag_ref) == 0);
265e176579eSDave Chinner 	kmem_free(pag);
266e176579eSDave Chinner }
267e176579eSDave Chinner 
2680fa800fbSDave Chinner /*
269e176579eSDave Chinner  * Free up the per-ag resources associated with the mount structure.
2701da177e4SLinus Torvalds  */
271c962fb79SChristoph Hellwig STATIC void
272ff4f038cSChristoph Hellwig xfs_free_perag(
273745f6919SChristoph Hellwig 	xfs_mount_t	*mp)
2741da177e4SLinus Torvalds {
2751c1c6ebcSDave Chinner 	xfs_agnumber_t	agno;
2761c1c6ebcSDave Chinner 	struct xfs_perag *pag;
2771da177e4SLinus Torvalds 
2781c1c6ebcSDave Chinner 	for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
2791c1c6ebcSDave Chinner 		spin_lock(&mp->m_perag_lock);
2801c1c6ebcSDave Chinner 		pag = radix_tree_delete(&mp->m_perag_tree, agno);
2811c1c6ebcSDave Chinner 		spin_unlock(&mp->m_perag_lock);
282e176579eSDave Chinner 		ASSERT(pag);
283f83282a8SDave Chinner 		ASSERT(atomic_read(&pag->pag_ref) == 0);
284e176579eSDave Chinner 		call_rcu(&pag->rcu_head, __xfs_free_perag);
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds 
2884cc929eeSNathan Scott /*
2894cc929eeSNathan Scott  * Check size of device based on the (data/realtime) block count.
2904cc929eeSNathan Scott  * Note: this check is used by the growfs code as well as mount.
2914cc929eeSNathan Scott  */
2924cc929eeSNathan Scott int
2934cc929eeSNathan Scott xfs_sb_validate_fsb_count(
2944cc929eeSNathan Scott 	xfs_sb_t	*sbp,
2954cc929eeSNathan Scott 	__uint64_t	nblocks)
2964cc929eeSNathan Scott {
2974cc929eeSNathan Scott 	ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
2984cc929eeSNathan Scott 	ASSERT(sbp->sb_blocklog >= BBSHIFT);
2994cc929eeSNathan Scott 
3004cc929eeSNathan Scott #if XFS_BIG_BLKNOS     /* Limited by ULONG_MAX of page cache index */
3014cc929eeSNathan Scott 	if (nblocks >> (PAGE_CACHE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
302657a4cffSEric Sandeen 		return EFBIG;
3034cc929eeSNathan Scott #else                  /* Limited by UINT_MAX of sectors */
3044cc929eeSNathan Scott 	if (nblocks << (sbp->sb_blocklog - BBSHIFT) > UINT_MAX)
305657a4cffSEric Sandeen 		return EFBIG;
3064cc929eeSNathan Scott #endif
3074cc929eeSNathan Scott 	return 0;
3084cc929eeSNathan Scott }
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds /*
3111da177e4SLinus Torvalds  * Check the validity of the SB found.
3121da177e4SLinus Torvalds  */
3131da177e4SLinus Torvalds STATIC int
3141da177e4SLinus Torvalds xfs_mount_validate_sb(
3151da177e4SLinus Torvalds 	xfs_mount_t	*mp,
316764d1f89SNathan Scott 	xfs_sb_t	*sbp,
31734510185SDave Chinner 	bool		check_inprogress,
31834510185SDave Chinner 	bool		check_version)
3191da177e4SLinus Torvalds {
320af34e09dSDave Chinner 
3211da177e4SLinus Torvalds 	/*
3221da177e4SLinus Torvalds 	 * If the log device and data device have the
3231da177e4SLinus Torvalds 	 * same device number, the log is internal.
3241da177e4SLinus Torvalds 	 * Consequently, the sb_logstart should be non-zero.  If
3251da177e4SLinus Torvalds 	 * we have a zero sb_logstart in this case, we may be trying to mount
3261da177e4SLinus Torvalds 	 * a volume filesystem in a non-volume manner.
3271da177e4SLinus Torvalds 	 */
3281da177e4SLinus Torvalds 	if (sbp->sb_magicnum != XFS_SB_MAGIC) {
329af34e09dSDave Chinner 		xfs_warn(mp, "bad magic number");
3301da177e4SLinus Torvalds 		return XFS_ERROR(EWRONGFS);
3311da177e4SLinus Torvalds 	}
3321da177e4SLinus Torvalds 
33304a1e6c5SDave Chinner 
33462118709SEric Sandeen 	if (!xfs_sb_good_version(sbp)) {
335af34e09dSDave Chinner 		xfs_warn(mp, "bad version");
3361da177e4SLinus Torvalds 		return XFS_ERROR(EWRONGFS);
3371da177e4SLinus Torvalds 	}
3381da177e4SLinus Torvalds 
339*83e782e1SChandra Seetharaman 	if ((sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) &&
340*83e782e1SChandra Seetharaman 			(sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
341*83e782e1SChandra Seetharaman 				XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))) {
342*83e782e1SChandra Seetharaman 		xfs_notice(mp,
343*83e782e1SChandra Seetharaman "Super block has XFS_OQUOTA bits along with XFS_PQUOTA and/or XFS_GQUOTA bits.\n");
344*83e782e1SChandra Seetharaman 		return XFS_ERROR(EFSCORRUPTED);
345*83e782e1SChandra Seetharaman 	}
346*83e782e1SChandra Seetharaman 
34704a1e6c5SDave Chinner 	/*
348e721f504SDave Chinner 	 * Version 5 superblock feature mask validation. Reject combinations the
34934510185SDave Chinner 	 * kernel cannot support up front before checking anything else. For
35034510185SDave Chinner 	 * write validation, we don't need to check feature masks.
35104a1e6c5SDave Chinner 	 */
35234510185SDave Chinner 	if (check_version && XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) {
35304a1e6c5SDave Chinner 		xfs_alert(mp,
354e721f504SDave Chinner "Version 5 superblock detected. This kernel has EXPERIMENTAL support enabled!\n"
355e721f504SDave Chinner "Use of these features in this kernel is at your own risk!");
356e721f504SDave Chinner 
357e721f504SDave Chinner 		if (xfs_sb_has_compat_feature(sbp,
358e721f504SDave Chinner 					XFS_SB_FEAT_COMPAT_UNKNOWN)) {
359e721f504SDave Chinner 			xfs_warn(mp,
360e721f504SDave Chinner "Superblock has unknown compatible features (0x%x) enabled.\n"
361e721f504SDave Chinner "Using a more recent kernel is recommended.",
362e721f504SDave Chinner 				(sbp->sb_features_compat &
363e721f504SDave Chinner 						XFS_SB_FEAT_COMPAT_UNKNOWN));
364e721f504SDave Chinner 		}
365e721f504SDave Chinner 
366e721f504SDave Chinner 		if (xfs_sb_has_ro_compat_feature(sbp,
367e721f504SDave Chinner 					XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
368e721f504SDave Chinner 			xfs_alert(mp,
369e721f504SDave Chinner "Superblock has unknown read-only compatible features (0x%x) enabled.",
370e721f504SDave Chinner 				(sbp->sb_features_ro_compat &
371e721f504SDave Chinner 						XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
372e721f504SDave Chinner 			if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
373e721f504SDave Chinner 				xfs_warn(mp,
374e721f504SDave Chinner "Attempted to mount read-only compatible filesystem read-write.\n"
375e721f504SDave Chinner "Filesystem can only be safely mounted read only.");
37604a1e6c5SDave Chinner 				return XFS_ERROR(EINVAL);
37704a1e6c5SDave Chinner 			}
378e721f504SDave Chinner 		}
379e721f504SDave Chinner 		if (xfs_sb_has_incompat_feature(sbp,
380e721f504SDave Chinner 					XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
381e721f504SDave Chinner 			xfs_warn(mp,
382e721f504SDave Chinner "Superblock has unknown incompatible features (0x%x) enabled.\n"
383e721f504SDave Chinner "Filesystem can not be safely mounted by this kernel.",
384e721f504SDave Chinner 				(sbp->sb_features_incompat &
385e721f504SDave Chinner 						XFS_SB_FEAT_INCOMPAT_UNKNOWN));
386e721f504SDave Chinner 			return XFS_ERROR(EINVAL);
387e721f504SDave Chinner 		}
388e721f504SDave Chinner 	}
38904a1e6c5SDave Chinner 
3901da177e4SLinus Torvalds 	if (unlikely(
3911da177e4SLinus Torvalds 	    sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
392af34e09dSDave Chinner 		xfs_warn(mp,
393764d1f89SNathan Scott 		"filesystem is marked as having an external log; "
394af34e09dSDave Chinner 		"specify logdev on the mount command line.");
395764d1f89SNathan Scott 		return XFS_ERROR(EINVAL);
3961da177e4SLinus Torvalds 	}
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	if (unlikely(
3991da177e4SLinus Torvalds 	    sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
400af34e09dSDave Chinner 		xfs_warn(mp,
401764d1f89SNathan Scott 		"filesystem is marked as having an internal log; "
402af34e09dSDave Chinner 		"do not specify logdev on the mount command line.");
403764d1f89SNathan Scott 		return XFS_ERROR(EINVAL);
4041da177e4SLinus Torvalds 	}
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	/*
407c0e090ceSEric Sandeen 	 * More sanity checking.  Most of these were stolen directly from
4081da177e4SLinus Torvalds 	 * xfs_repair.
4091da177e4SLinus Torvalds 	 */
4101da177e4SLinus Torvalds 	if (unlikely(
4111da177e4SLinus Torvalds 	    sbp->sb_agcount <= 0					||
4121da177e4SLinus Torvalds 	    sbp->sb_sectsize < XFS_MIN_SECTORSIZE			||
4131da177e4SLinus Torvalds 	    sbp->sb_sectsize > XFS_MAX_SECTORSIZE			||
4141da177e4SLinus Torvalds 	    sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG			||
4151da177e4SLinus Torvalds 	    sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG			||
4162ac00af7SOlaf Weber 	    sbp->sb_sectsize != (1 << sbp->sb_sectlog)			||
4171da177e4SLinus Torvalds 	    sbp->sb_blocksize < XFS_MIN_BLOCKSIZE			||
4181da177e4SLinus Torvalds 	    sbp->sb_blocksize > XFS_MAX_BLOCKSIZE			||
4191da177e4SLinus Torvalds 	    sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG			||
4201da177e4SLinus Torvalds 	    sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG			||
4212ac00af7SOlaf Weber 	    sbp->sb_blocksize != (1 << sbp->sb_blocklog)		||
4221da177e4SLinus Torvalds 	    sbp->sb_inodesize < XFS_DINODE_MIN_SIZE			||
4231da177e4SLinus Torvalds 	    sbp->sb_inodesize > XFS_DINODE_MAX_SIZE			||
4249f989c94SNathan Scott 	    sbp->sb_inodelog < XFS_DINODE_MIN_LOG			||
4259f989c94SNathan Scott 	    sbp->sb_inodelog > XFS_DINODE_MAX_LOG			||
4262ac00af7SOlaf Weber 	    sbp->sb_inodesize != (1 << sbp->sb_inodelog)		||
4279f989c94SNathan Scott 	    (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog)	||
4281da177e4SLinus Torvalds 	    (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE)	||
4291da177e4SLinus Torvalds 	    (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE)	||
430c0e090ceSEric Sandeen 	    (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */)	||
4311da177e4SLinus Torvalds 	    sbp->sb_dblocks == 0					||
432c0e090ceSEric Sandeen 	    sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp)			||
433c0e090ceSEric Sandeen 	    sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp))) {
434c0e090ceSEric Sandeen 		XFS_CORRUPTION_ERROR("SB sanity check failed",
435c0e090ceSEric Sandeen 				XFS_ERRLEVEL_LOW, mp, sbp);
4361da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
4371da177e4SLinus Torvalds 	}
4381da177e4SLinus Torvalds 
4392edbddd5SLachlan McIlroy 	/*
4402edbddd5SLachlan McIlroy 	 * Until this is fixed only page-sized or smaller data blocks work.
4412edbddd5SLachlan McIlroy 	 */
4422edbddd5SLachlan McIlroy 	if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
443af34e09dSDave Chinner 		xfs_warn(mp,
444af34e09dSDave Chinner 		"File system with blocksize %d bytes. "
445af34e09dSDave Chinner 		"Only pagesize (%ld) or less will currently work.",
446af34e09dSDave Chinner 				sbp->sb_blocksize, PAGE_SIZE);
4472edbddd5SLachlan McIlroy 		return XFS_ERROR(ENOSYS);
4482edbddd5SLachlan McIlroy 	}
4492edbddd5SLachlan McIlroy 
4501a5902c5SChristoph Hellwig 	/*
4511a5902c5SChristoph Hellwig 	 * Currently only very few inode sizes are supported.
4521a5902c5SChristoph Hellwig 	 */
4531a5902c5SChristoph Hellwig 	switch (sbp->sb_inodesize) {
4541a5902c5SChristoph Hellwig 	case 256:
4551a5902c5SChristoph Hellwig 	case 512:
4561a5902c5SChristoph Hellwig 	case 1024:
4571a5902c5SChristoph Hellwig 	case 2048:
4581a5902c5SChristoph Hellwig 		break;
4591a5902c5SChristoph Hellwig 	default:
460af34e09dSDave Chinner 		xfs_warn(mp, "inode size of %d bytes not supported",
4611a5902c5SChristoph Hellwig 				sbp->sb_inodesize);
4621a5902c5SChristoph Hellwig 		return XFS_ERROR(ENOSYS);
4631a5902c5SChristoph Hellwig 	}
4641a5902c5SChristoph Hellwig 
4654cc929eeSNathan Scott 	if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
4664cc929eeSNathan Scott 	    xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
467af34e09dSDave Chinner 		xfs_warn(mp,
468764d1f89SNathan Scott 		"file system too large to be mounted on this system.");
469657a4cffSEric Sandeen 		return XFS_ERROR(EFBIG);
4701da177e4SLinus Torvalds 	}
4711da177e4SLinus Torvalds 
47298021821SDave Chinner 	if (check_inprogress && sbp->sb_inprogress) {
47398021821SDave Chinner 		xfs_warn(mp, "Offline file system operation in progress!");
4741da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
4751da177e4SLinus Torvalds 	}
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	/*
478de20614bSNathan Scott 	 * Version 1 directory format has never worked on Linux.
479de20614bSNathan Scott 	 */
48062118709SEric Sandeen 	if (unlikely(!xfs_sb_version_hasdirv2(sbp))) {
48198021821SDave Chinner 		xfs_warn(mp, "file system using version 1 directory format");
482de20614bSNathan Scott 		return XFS_ERROR(ENOSYS);
483de20614bSNathan Scott 	}
484de20614bSNathan Scott 
4851da177e4SLinus Torvalds 	return 0;
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds 
4881c1c6ebcSDave Chinner int
489c11e2c36SNathan Scott xfs_initialize_perag(
490c11e2c36SNathan Scott 	xfs_mount_t	*mp,
4911c1c6ebcSDave Chinner 	xfs_agnumber_t	agcount,
4921c1c6ebcSDave Chinner 	xfs_agnumber_t	*maxagi)
4931da177e4SLinus Torvalds {
4942d2194f6SCarlos Maiolino 	xfs_agnumber_t	index;
4958b26c582SDave Chinner 	xfs_agnumber_t	first_initialised = 0;
4961da177e4SLinus Torvalds 	xfs_perag_t	*pag;
4971da177e4SLinus Torvalds 	xfs_agino_t	agino;
4981da177e4SLinus Torvalds 	xfs_ino_t	ino;
4991da177e4SLinus Torvalds 	xfs_sb_t	*sbp = &mp->m_sb;
5008b26c582SDave Chinner 	int		error = -ENOMEM;
5011da177e4SLinus Torvalds 
5021c1c6ebcSDave Chinner 	/*
5031c1c6ebcSDave Chinner 	 * Walk the current per-ag tree so we don't try to initialise AGs
5041c1c6ebcSDave Chinner 	 * that already exist (growfs case). Allocate and insert all the
5051c1c6ebcSDave Chinner 	 * AGs we don't find ready for initialisation.
5061c1c6ebcSDave Chinner 	 */
5071c1c6ebcSDave Chinner 	for (index = 0; index < agcount; index++) {
5081c1c6ebcSDave Chinner 		pag = xfs_perag_get(mp, index);
5091c1c6ebcSDave Chinner 		if (pag) {
5101c1c6ebcSDave Chinner 			xfs_perag_put(pag);
5111c1c6ebcSDave Chinner 			continue;
5121c1c6ebcSDave Chinner 		}
5138b26c582SDave Chinner 		if (!first_initialised)
5148b26c582SDave Chinner 			first_initialised = index;
515fb3b504aSChristoph Hellwig 
5161c1c6ebcSDave Chinner 		pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
5171c1c6ebcSDave Chinner 		if (!pag)
5188b26c582SDave Chinner 			goto out_unwind;
519fb3b504aSChristoph Hellwig 		pag->pag_agno = index;
520fb3b504aSChristoph Hellwig 		pag->pag_mount = mp;
5211a427ab0SDave Chinner 		spin_lock_init(&pag->pag_ici_lock);
52269b491c2SDave Chinner 		mutex_init(&pag->pag_ici_reclaim_lock);
523fb3b504aSChristoph Hellwig 		INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
52474f75a0cSDave Chinner 		spin_lock_init(&pag->pag_buf_lock);
52574f75a0cSDave Chinner 		pag->pag_buf_tree = RB_ROOT;
526fb3b504aSChristoph Hellwig 
5271c1c6ebcSDave Chinner 		if (radix_tree_preload(GFP_NOFS))
5288b26c582SDave Chinner 			goto out_unwind;
529fb3b504aSChristoph Hellwig 
5301c1c6ebcSDave Chinner 		spin_lock(&mp->m_perag_lock);
5311c1c6ebcSDave Chinner 		if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
5321c1c6ebcSDave Chinner 			BUG();
5331c1c6ebcSDave Chinner 			spin_unlock(&mp->m_perag_lock);
5348b26c582SDave Chinner 			radix_tree_preload_end();
5358b26c582SDave Chinner 			error = -EEXIST;
5368b26c582SDave Chinner 			goto out_unwind;
5371c1c6ebcSDave Chinner 		}
5381c1c6ebcSDave Chinner 		spin_unlock(&mp->m_perag_lock);
5391c1c6ebcSDave Chinner 		radix_tree_preload_end();
5401c1c6ebcSDave Chinner 	}
5411c1c6ebcSDave Chinner 
542fb3b504aSChristoph Hellwig 	/*
543fb3b504aSChristoph Hellwig 	 * If we mount with the inode64 option, or no inode overflows
544fb3b504aSChristoph Hellwig 	 * the legacy 32-bit address space clear the inode32 option.
5451da177e4SLinus Torvalds 	 */
546fb3b504aSChristoph Hellwig 	agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
547fb3b504aSChristoph Hellwig 	ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
5481da177e4SLinus Torvalds 
549fb3b504aSChristoph Hellwig 	if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > XFS_MAXINUMBER_32)
550fb3b504aSChristoph Hellwig 		mp->m_flags |= XFS_MOUNT_32BITINODES;
551fb3b504aSChristoph Hellwig 	else
552fb3b504aSChristoph Hellwig 		mp->m_flags &= ~XFS_MOUNT_32BITINODES;
553fb3b504aSChristoph Hellwig 
5542d2194f6SCarlos Maiolino 	if (mp->m_flags & XFS_MOUNT_32BITINODES)
5552d2194f6SCarlos Maiolino 		index = xfs_set_inode32(mp);
5562d2194f6SCarlos Maiolino 	else
5572d2194f6SCarlos Maiolino 		index = xfs_set_inode64(mp);
558fb3b504aSChristoph Hellwig 
5591c1c6ebcSDave Chinner 	if (maxagi)
5601c1c6ebcSDave Chinner 		*maxagi = index;
5611c1c6ebcSDave Chinner 	return 0;
5628b26c582SDave Chinner 
5638b26c582SDave Chinner out_unwind:
5648b26c582SDave Chinner 	kmem_free(pag);
5658b26c582SDave Chinner 	for (; index > first_initialised; index--) {
5668b26c582SDave Chinner 		pag = radix_tree_delete(&mp->m_perag_tree, index);
5678b26c582SDave Chinner 		kmem_free(pag);
5688b26c582SDave Chinner 	}
5698b26c582SDave Chinner 	return error;
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds 
572*83e782e1SChandra Seetharaman static void
573*83e782e1SChandra Seetharaman xfs_sb_quota_from_disk(struct xfs_sb *sbp)
574*83e782e1SChandra Seetharaman {
575*83e782e1SChandra Seetharaman 	if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
576*83e782e1SChandra Seetharaman 		sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
577*83e782e1SChandra Seetharaman 					XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
578*83e782e1SChandra Seetharaman 	if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
579*83e782e1SChandra Seetharaman 		sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
580*83e782e1SChandra Seetharaman 					XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
581*83e782e1SChandra Seetharaman 	sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
582*83e782e1SChandra Seetharaman }
583*83e782e1SChandra Seetharaman 
5842bdf7cd0SChristoph Hellwig void
5852bdf7cd0SChristoph Hellwig xfs_sb_from_disk(
58698021821SDave Chinner 	struct xfs_sb	*to,
5872bdf7cd0SChristoph Hellwig 	xfs_dsb_t	*from)
5882bdf7cd0SChristoph Hellwig {
5892bdf7cd0SChristoph Hellwig 	to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
5902bdf7cd0SChristoph Hellwig 	to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
5912bdf7cd0SChristoph Hellwig 	to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
5922bdf7cd0SChristoph Hellwig 	to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
5932bdf7cd0SChristoph Hellwig 	to->sb_rextents = be64_to_cpu(from->sb_rextents);
5942bdf7cd0SChristoph Hellwig 	memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
5952bdf7cd0SChristoph Hellwig 	to->sb_logstart = be64_to_cpu(from->sb_logstart);
5962bdf7cd0SChristoph Hellwig 	to->sb_rootino = be64_to_cpu(from->sb_rootino);
5972bdf7cd0SChristoph Hellwig 	to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
5982bdf7cd0SChristoph Hellwig 	to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
5992bdf7cd0SChristoph Hellwig 	to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
6002bdf7cd0SChristoph Hellwig 	to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
6012bdf7cd0SChristoph Hellwig 	to->sb_agcount = be32_to_cpu(from->sb_agcount);
6022bdf7cd0SChristoph Hellwig 	to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
6032bdf7cd0SChristoph Hellwig 	to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
6042bdf7cd0SChristoph Hellwig 	to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
6052bdf7cd0SChristoph Hellwig 	to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
6062bdf7cd0SChristoph Hellwig 	to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
6072bdf7cd0SChristoph Hellwig 	to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
6082bdf7cd0SChristoph Hellwig 	memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
6092bdf7cd0SChristoph Hellwig 	to->sb_blocklog = from->sb_blocklog;
6102bdf7cd0SChristoph Hellwig 	to->sb_sectlog = from->sb_sectlog;
6112bdf7cd0SChristoph Hellwig 	to->sb_inodelog = from->sb_inodelog;
6122bdf7cd0SChristoph Hellwig 	to->sb_inopblog = from->sb_inopblog;
6132bdf7cd0SChristoph Hellwig 	to->sb_agblklog = from->sb_agblklog;
6142bdf7cd0SChristoph Hellwig 	to->sb_rextslog = from->sb_rextslog;
6152bdf7cd0SChristoph Hellwig 	to->sb_inprogress = from->sb_inprogress;
6162bdf7cd0SChristoph Hellwig 	to->sb_imax_pct = from->sb_imax_pct;
6172bdf7cd0SChristoph Hellwig 	to->sb_icount = be64_to_cpu(from->sb_icount);
6182bdf7cd0SChristoph Hellwig 	to->sb_ifree = be64_to_cpu(from->sb_ifree);
6192bdf7cd0SChristoph Hellwig 	to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
6202bdf7cd0SChristoph Hellwig 	to->sb_frextents = be64_to_cpu(from->sb_frextents);
6212bdf7cd0SChristoph Hellwig 	to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
6222bdf7cd0SChristoph Hellwig 	to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
6232bdf7cd0SChristoph Hellwig 	to->sb_qflags = be16_to_cpu(from->sb_qflags);
6242bdf7cd0SChristoph Hellwig 	to->sb_flags = from->sb_flags;
6252bdf7cd0SChristoph Hellwig 	to->sb_shared_vn = from->sb_shared_vn;
6262bdf7cd0SChristoph Hellwig 	to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
6272bdf7cd0SChristoph Hellwig 	to->sb_unit = be32_to_cpu(from->sb_unit);
6282bdf7cd0SChristoph Hellwig 	to->sb_width = be32_to_cpu(from->sb_width);
6292bdf7cd0SChristoph Hellwig 	to->sb_dirblklog = from->sb_dirblklog;
6302bdf7cd0SChristoph Hellwig 	to->sb_logsectlog = from->sb_logsectlog;
6312bdf7cd0SChristoph Hellwig 	to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
6322bdf7cd0SChristoph Hellwig 	to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
6332bdf7cd0SChristoph Hellwig 	to->sb_features2 = be32_to_cpu(from->sb_features2);
634ee1c0908SDavid Chinner 	to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
63504a1e6c5SDave Chinner 	to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
63604a1e6c5SDave Chinner 	to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
63704a1e6c5SDave Chinner 	to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
638e721f504SDave Chinner 	to->sb_features_log_incompat =
639e721f504SDave Chinner 				be32_to_cpu(from->sb_features_log_incompat);
640e721f504SDave Chinner 	to->sb_pad = 0;
64104a1e6c5SDave Chinner 	to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
64204a1e6c5SDave Chinner 	to->sb_lsn = be64_to_cpu(from->sb_lsn);
6432bdf7cd0SChristoph Hellwig }
6442bdf7cd0SChristoph Hellwig 
645*83e782e1SChandra Seetharaman static inline void
646*83e782e1SChandra Seetharaman xfs_sb_quota_to_disk(
647*83e782e1SChandra Seetharaman 	xfs_dsb_t	*to,
648*83e782e1SChandra Seetharaman 	xfs_sb_t	*from,
649*83e782e1SChandra Seetharaman 	__int64_t	*fields)
650*83e782e1SChandra Seetharaman {
651*83e782e1SChandra Seetharaman 	__uint16_t	qflags = from->sb_qflags;
652*83e782e1SChandra Seetharaman 
653*83e782e1SChandra Seetharaman 	if (*fields & XFS_SB_QFLAGS) {
654*83e782e1SChandra Seetharaman 		/*
655*83e782e1SChandra Seetharaman 		 * The in-core version of sb_qflags do not have
656*83e782e1SChandra Seetharaman 		 * XFS_OQUOTA_* flags, whereas the on-disk version
657*83e782e1SChandra Seetharaman 		 * does.  So, convert incore XFS_{PG}QUOTA_* flags
658*83e782e1SChandra Seetharaman 		 * to on-disk XFS_OQUOTA_* flags.
659*83e782e1SChandra Seetharaman 		 */
660*83e782e1SChandra Seetharaman 		qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
661*83e782e1SChandra Seetharaman 				XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
662*83e782e1SChandra Seetharaman 
663*83e782e1SChandra Seetharaman 		if (from->sb_qflags &
664*83e782e1SChandra Seetharaman 				(XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
665*83e782e1SChandra Seetharaman 			qflags |= XFS_OQUOTA_ENFD;
666*83e782e1SChandra Seetharaman 		if (from->sb_qflags &
667*83e782e1SChandra Seetharaman 				(XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
668*83e782e1SChandra Seetharaman 			qflags |= XFS_OQUOTA_CHKD;
669*83e782e1SChandra Seetharaman 		to->sb_qflags = cpu_to_be16(qflags);
670*83e782e1SChandra Seetharaman 		*fields &= ~XFS_SB_QFLAGS;
671*83e782e1SChandra Seetharaman 	}
672*83e782e1SChandra Seetharaman }
673*83e782e1SChandra Seetharaman 
6741da177e4SLinus Torvalds /*
6752bdf7cd0SChristoph Hellwig  * Copy in core superblock to ondisk one.
6761da177e4SLinus Torvalds  *
6772bdf7cd0SChristoph Hellwig  * The fields argument is mask of superblock fields to copy.
6781da177e4SLinus Torvalds  */
6791da177e4SLinus Torvalds void
6802bdf7cd0SChristoph Hellwig xfs_sb_to_disk(
6812bdf7cd0SChristoph Hellwig 	xfs_dsb_t	*to,
6822bdf7cd0SChristoph Hellwig 	xfs_sb_t	*from,
6831da177e4SLinus Torvalds 	__int64_t	fields)
6841da177e4SLinus Torvalds {
6852bdf7cd0SChristoph Hellwig 	xfs_caddr_t	to_ptr = (xfs_caddr_t)to;
6862bdf7cd0SChristoph Hellwig 	xfs_caddr_t	from_ptr = (xfs_caddr_t)from;
6871da177e4SLinus Torvalds 	xfs_sb_field_t	f;
6881da177e4SLinus Torvalds 	int		first;
6891da177e4SLinus Torvalds 	int		size;
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	ASSERT(fields);
6921da177e4SLinus Torvalds 	if (!fields)
6931da177e4SLinus Torvalds 		return;
6941da177e4SLinus Torvalds 
695*83e782e1SChandra Seetharaman 	xfs_sb_quota_to_disk(to, from, &fields);
6961da177e4SLinus Torvalds 	while (fields) {
6971da177e4SLinus Torvalds 		f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
6981da177e4SLinus Torvalds 		first = xfs_sb_info[f].offset;
6991da177e4SLinus Torvalds 		size = xfs_sb_info[f + 1].offset - first;
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 		ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 		if (size == 1 || xfs_sb_info[f].type == 1) {
7042bdf7cd0SChristoph Hellwig 			memcpy(to_ptr + first, from_ptr + first, size);
7051da177e4SLinus Torvalds 		} else {
7061da177e4SLinus Torvalds 			switch (size) {
7071da177e4SLinus Torvalds 			case 2:
7082bdf7cd0SChristoph Hellwig 				*(__be16 *)(to_ptr + first) =
7092bdf7cd0SChristoph Hellwig 					cpu_to_be16(*(__u16 *)(from_ptr + first));
7101da177e4SLinus Torvalds 				break;
7111da177e4SLinus Torvalds 			case 4:
7122bdf7cd0SChristoph Hellwig 				*(__be32 *)(to_ptr + first) =
7132bdf7cd0SChristoph Hellwig 					cpu_to_be32(*(__u32 *)(from_ptr + first));
7141da177e4SLinus Torvalds 				break;
7151da177e4SLinus Torvalds 			case 8:
7162bdf7cd0SChristoph Hellwig 				*(__be64 *)(to_ptr + first) =
7172bdf7cd0SChristoph Hellwig 					cpu_to_be64(*(__u64 *)(from_ptr + first));
7181da177e4SLinus Torvalds 				break;
7191da177e4SLinus Torvalds 			default:
7201da177e4SLinus Torvalds 				ASSERT(0);
7211da177e4SLinus Torvalds 			}
7221da177e4SLinus Torvalds 		}
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 		fields &= ~(1LL << f);
7251da177e4SLinus Torvalds 	}
7261da177e4SLinus Torvalds }
7271da177e4SLinus Torvalds 
72804a1e6c5SDave Chinner static int
729612cfbfeSDave Chinner xfs_sb_verify(
73034510185SDave Chinner 	struct xfs_buf	*bp,
73134510185SDave Chinner 	bool		check_version)
73298021821SDave Chinner {
73398021821SDave Chinner 	struct xfs_mount *mp = bp->b_target->bt_mount;
73498021821SDave Chinner 	struct xfs_sb	sb;
73598021821SDave Chinner 
73698021821SDave Chinner 	xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp));
73798021821SDave Chinner 
73898021821SDave Chinner 	/*
73998021821SDave Chinner 	 * Only check the in progress field for the primary superblock as
74098021821SDave Chinner 	 * mkfs.xfs doesn't clear it from secondary superblocks.
74198021821SDave Chinner 	 */
74234510185SDave Chinner 	return xfs_mount_validate_sb(mp, &sb, bp->b_bn == XFS_SB_DADDR,
74334510185SDave Chinner 				     check_version);
744612cfbfeSDave Chinner }
745612cfbfeSDave Chinner 
74604a1e6c5SDave Chinner /*
74704a1e6c5SDave Chinner  * If the superblock has the CRC feature bit set or the CRC field is non-null,
74804a1e6c5SDave Chinner  * check that the CRC is valid.  We check the CRC field is non-null because a
74904a1e6c5SDave Chinner  * single bit error could clear the feature bit and unused parts of the
75004a1e6c5SDave Chinner  * superblock are supposed to be zero. Hence a non-null crc field indicates that
75104a1e6c5SDave Chinner  * we've potentially lost a feature bit and we should check it anyway.
75204a1e6c5SDave Chinner  */
7531813dd64SDave Chinner static void
754612cfbfeSDave Chinner xfs_sb_read_verify(
755612cfbfeSDave Chinner 	struct xfs_buf	*bp)
756612cfbfeSDave Chinner {
75704a1e6c5SDave Chinner 	struct xfs_mount *mp = bp->b_target->bt_mount;
75804a1e6c5SDave Chinner 	struct xfs_dsb	*dsb = XFS_BUF_TO_SBP(bp);
75904a1e6c5SDave Chinner 	int		error;
76004a1e6c5SDave Chinner 
76104a1e6c5SDave Chinner 	/*
76204a1e6c5SDave Chinner 	 * open code the version check to avoid needing to convert the entire
76304a1e6c5SDave Chinner 	 * superblock from disk order just to check the version number
76404a1e6c5SDave Chinner 	 */
76504a1e6c5SDave Chinner 	if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
76604a1e6c5SDave Chinner 	    (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
76704a1e6c5SDave Chinner 						XFS_SB_VERSION_5) ||
76804a1e6c5SDave Chinner 	     dsb->sb_crc != 0)) {
76904a1e6c5SDave Chinner 
77004a1e6c5SDave Chinner 		if (!xfs_verify_cksum(bp->b_addr, be16_to_cpu(dsb->sb_sectsize),
77104a1e6c5SDave Chinner 				      offsetof(struct xfs_sb, sb_crc))) {
77204a1e6c5SDave Chinner 			error = EFSCORRUPTED;
77304a1e6c5SDave Chinner 			goto out_error;
77404a1e6c5SDave Chinner 		}
77504a1e6c5SDave Chinner 	}
77634510185SDave Chinner 	error = xfs_sb_verify(bp, true);
77704a1e6c5SDave Chinner 
77804a1e6c5SDave Chinner out_error:
77904a1e6c5SDave Chinner 	if (error) {
78004a1e6c5SDave Chinner 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
78104a1e6c5SDave Chinner 		xfs_buf_ioerror(bp, error);
78204a1e6c5SDave Chinner 	}
78398021821SDave Chinner }
78498021821SDave Chinner 
78598021821SDave Chinner /*
78698021821SDave Chinner  * We may be probed for a filesystem match, so we may not want to emit
78798021821SDave Chinner  * messages when the superblock buffer is not actually an XFS superblock.
78898021821SDave Chinner  * If we find an XFS superblock, the run a normal, noisy mount because we are
78998021821SDave Chinner  * really going to mount it and want to know about errors.
79098021821SDave Chinner  */
7911813dd64SDave Chinner static void
79298021821SDave Chinner xfs_sb_quiet_read_verify(
79398021821SDave Chinner 	struct xfs_buf	*bp)
79498021821SDave Chinner {
79504a1e6c5SDave Chinner 	struct xfs_dsb	*dsb = XFS_BUF_TO_SBP(bp);
79698021821SDave Chinner 
79798021821SDave Chinner 
79804a1e6c5SDave Chinner 	if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
79998021821SDave Chinner 		/* XFS filesystem, verify noisily! */
80098021821SDave Chinner 		xfs_sb_read_verify(bp);
80198021821SDave Chinner 		return;
80298021821SDave Chinner 	}
80398021821SDave Chinner 	/* quietly fail */
804aeb4f20aSEric Sandeen 	xfs_buf_ioerror(bp, EWRONGFS);
80598021821SDave Chinner }
80698021821SDave Chinner 
8071813dd64SDave Chinner static void
8081813dd64SDave Chinner xfs_sb_write_verify(
8091813dd64SDave Chinner 	struct xfs_buf		*bp)
8101813dd64SDave Chinner {
81104a1e6c5SDave Chinner 	struct xfs_mount	*mp = bp->b_target->bt_mount;
81204a1e6c5SDave Chinner 	struct xfs_buf_log_item	*bip = bp->b_fspriv;
81304a1e6c5SDave Chinner 	int			error;
81404a1e6c5SDave Chinner 
81534510185SDave Chinner 	error = xfs_sb_verify(bp, false);
81604a1e6c5SDave Chinner 	if (error) {
81704a1e6c5SDave Chinner 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
81804a1e6c5SDave Chinner 		xfs_buf_ioerror(bp, error);
81904a1e6c5SDave Chinner 		return;
82004a1e6c5SDave Chinner 	}
82104a1e6c5SDave Chinner 
82204a1e6c5SDave Chinner 	if (!xfs_sb_version_hascrc(&mp->m_sb))
82304a1e6c5SDave Chinner 		return;
82404a1e6c5SDave Chinner 
82504a1e6c5SDave Chinner 	if (bip)
82604a1e6c5SDave Chinner 		XFS_BUF_TO_SBP(bp)->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
82704a1e6c5SDave Chinner 
82804a1e6c5SDave Chinner 	xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
82904a1e6c5SDave Chinner 			 offsetof(struct xfs_sb, sb_crc));
8301813dd64SDave Chinner }
8311813dd64SDave Chinner 
8321813dd64SDave Chinner const struct xfs_buf_ops xfs_sb_buf_ops = {
8331813dd64SDave Chinner 	.verify_read = xfs_sb_read_verify,
8341813dd64SDave Chinner 	.verify_write = xfs_sb_write_verify,
8351813dd64SDave Chinner };
8361813dd64SDave Chinner 
8371813dd64SDave Chinner static const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
8381813dd64SDave Chinner 	.verify_read = xfs_sb_quiet_read_verify,
8391813dd64SDave Chinner 	.verify_write = xfs_sb_write_verify,
8401813dd64SDave Chinner };
8411813dd64SDave Chinner 
8421da177e4SLinus Torvalds /*
8431da177e4SLinus Torvalds  * xfs_readsb
8441da177e4SLinus Torvalds  *
8451da177e4SLinus Torvalds  * Does the initial read of the superblock.
8461da177e4SLinus Torvalds  */
8471da177e4SLinus Torvalds int
848764d1f89SNathan Scott xfs_readsb(xfs_mount_t *mp, int flags)
8491da177e4SLinus Torvalds {
8501da177e4SLinus Torvalds 	unsigned int	sector_size;
85104a1e6c5SDave Chinner 	struct xfs_buf	*bp;
85204a1e6c5SDave Chinner 	struct xfs_sb	*sbp = &mp->m_sb;
8531da177e4SLinus Torvalds 	int		error;
854af34e09dSDave Chinner 	int		loud = !(flags & XFS_MFSI_QUIET);
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	ASSERT(mp->m_sb_bp == NULL);
8571da177e4SLinus Torvalds 	ASSERT(mp->m_ddev_targp != NULL);
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	/*
8601da177e4SLinus Torvalds 	 * Allocate a (locked) buffer to hold the superblock.
8611da177e4SLinus Torvalds 	 * This will be kept around at all times to optimize
8621da177e4SLinus Torvalds 	 * access to the superblock.
8631da177e4SLinus Torvalds 	 */
8641da177e4SLinus Torvalds 	sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
8651da177e4SLinus Torvalds 
86626af6552SDave Chinner reread:
867e70b73f8SDave Chinner 	bp = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR,
86898021821SDave Chinner 				   BTOBB(sector_size), 0,
8691813dd64SDave Chinner 				   loud ? &xfs_sb_buf_ops
8701813dd64SDave Chinner 				        : &xfs_sb_quiet_buf_ops);
87126af6552SDave Chinner 	if (!bp) {
872af34e09dSDave Chinner 		if (loud)
873af34e09dSDave Chinner 			xfs_warn(mp, "SB buffer read failed");
87426af6552SDave Chinner 		return EIO;
8751da177e4SLinus Torvalds 	}
876eab4e633SDave Chinner 	if (bp->b_error) {
877eab4e633SDave Chinner 		error = bp->b_error;
878eab4e633SDave Chinner 		if (loud)
879e721f504SDave Chinner 			xfs_warn(mp, "SB validate failed with error %d.", error);
880eab4e633SDave Chinner 		goto release_buf;
881eab4e633SDave Chinner 	}
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	/*
8841da177e4SLinus Torvalds 	 * Initialize the mount structure from the superblock.
8851da177e4SLinus Torvalds 	 */
88698021821SDave Chinner 	xfs_sb_from_disk(&mp->m_sb, XFS_BUF_TO_SBP(bp));
8871da177e4SLinus Torvalds 
888*83e782e1SChandra Seetharaman 	xfs_sb_quota_from_disk(&mp->m_sb);
8891da177e4SLinus Torvalds 	/*
8901da177e4SLinus Torvalds 	 * We must be able to do sector-sized and sector-aligned IO.
8911da177e4SLinus Torvalds 	 */
89204a1e6c5SDave Chinner 	if (sector_size > sbp->sb_sectsize) {
893af34e09dSDave Chinner 		if (loud)
894af34e09dSDave Chinner 			xfs_warn(mp, "device supports %u byte sectors (not %u)",
89504a1e6c5SDave Chinner 				sector_size, sbp->sb_sectsize);
8961da177e4SLinus Torvalds 		error = ENOSYS;
89726af6552SDave Chinner 		goto release_buf;
8981da177e4SLinus Torvalds 	}
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 	/*
9011da177e4SLinus Torvalds 	 * If device sector size is smaller than the superblock size,
9021da177e4SLinus Torvalds 	 * re-read the superblock so the buffer is correctly sized.
9031da177e4SLinus Torvalds 	 */
90404a1e6c5SDave Chinner 	if (sector_size < sbp->sb_sectsize) {
9051da177e4SLinus Torvalds 		xfs_buf_relse(bp);
90604a1e6c5SDave Chinner 		sector_size = sbp->sb_sectsize;
90726af6552SDave Chinner 		goto reread;
9081da177e4SLinus Torvalds 	}
9091da177e4SLinus Torvalds 
9105478eeadSLachlan McIlroy 	/* Initialize per-cpu counters */
9115478eeadSLachlan McIlroy 	xfs_icsb_reinit_counters(mp);
9128d280b98SDavid Chinner 
91304a1e6c5SDave Chinner 	/* no need to be quiet anymore, so reset the buf ops */
91404a1e6c5SDave Chinner 	bp->b_ops = &xfs_sb_buf_ops;
91504a1e6c5SDave Chinner 
9161da177e4SLinus Torvalds 	mp->m_sb_bp = bp;
91726af6552SDave Chinner 	xfs_buf_unlock(bp);
9181da177e4SLinus Torvalds 	return 0;
9191da177e4SLinus Torvalds 
92026af6552SDave Chinner release_buf:
9211da177e4SLinus Torvalds 	xfs_buf_relse(bp);
9221da177e4SLinus Torvalds 	return error;
9231da177e4SLinus Torvalds }
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds /*
9271da177e4SLinus Torvalds  * xfs_mount_common
9281da177e4SLinus Torvalds  *
9291da177e4SLinus Torvalds  * Mount initialization code establishing various mount
9301da177e4SLinus Torvalds  * fields from the superblock associated with the given
9311da177e4SLinus Torvalds  * mount structure
9321da177e4SLinus Torvalds  */
933ba0f32d4SChristoph Hellwig STATIC void
9341da177e4SLinus Torvalds xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
9351da177e4SLinus Torvalds {
9361da177e4SLinus Torvalds 	mp->m_agfrotor = mp->m_agirotor = 0;
937007c61c6SEric Sandeen 	spin_lock_init(&mp->m_agirotor_lock);
9381da177e4SLinus Torvalds 	mp->m_maxagi = mp->m_sb.sb_agcount;
9391da177e4SLinus Torvalds 	mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
9401da177e4SLinus Torvalds 	mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
9411da177e4SLinus Torvalds 	mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
9421da177e4SLinus Torvalds 	mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
9431da177e4SLinus Torvalds 	mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
9441da177e4SLinus Torvalds 	mp->m_blockmask = sbp->sb_blocksize - 1;
9451da177e4SLinus Torvalds 	mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
9461da177e4SLinus Torvalds 	mp->m_blockwmask = mp->m_blockwsize - 1;
9471da177e4SLinus Torvalds 
94860197e8dSChristoph Hellwig 	mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
94960197e8dSChristoph Hellwig 	mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
95060197e8dSChristoph Hellwig 	mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
95160197e8dSChristoph Hellwig 	mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
95260197e8dSChristoph Hellwig 
95360197e8dSChristoph Hellwig 	mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
95460197e8dSChristoph Hellwig 	mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
95560197e8dSChristoph Hellwig 	mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
95660197e8dSChristoph Hellwig 	mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
95760197e8dSChristoph Hellwig 
95860197e8dSChristoph Hellwig 	mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
95960197e8dSChristoph Hellwig 	mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
96060197e8dSChristoph Hellwig 	mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
96160197e8dSChristoph Hellwig 	mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
9641da177e4SLinus Torvalds 	mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
9651da177e4SLinus Torvalds 					sbp->sb_inopblock);
9661da177e4SLinus Torvalds 	mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
9671da177e4SLinus Torvalds }
96892821e2bSDavid Chinner 
96992821e2bSDavid Chinner /*
97092821e2bSDavid Chinner  * xfs_initialize_perag_data
97192821e2bSDavid Chinner  *
97292821e2bSDavid Chinner  * Read in each per-ag structure so we can count up the number of
97392821e2bSDavid Chinner  * allocated inodes, free inodes and used filesystem blocks as this
97492821e2bSDavid Chinner  * information is no longer persistent in the superblock. Once we have
97592821e2bSDavid Chinner  * this information, write it into the in-core superblock structure.
97692821e2bSDavid Chinner  */
97792821e2bSDavid Chinner STATIC int
97892821e2bSDavid Chinner xfs_initialize_perag_data(xfs_mount_t *mp, xfs_agnumber_t agcount)
97992821e2bSDavid Chinner {
98092821e2bSDavid Chinner 	xfs_agnumber_t	index;
98192821e2bSDavid Chinner 	xfs_perag_t	*pag;
98292821e2bSDavid Chinner 	xfs_sb_t	*sbp = &mp->m_sb;
98392821e2bSDavid Chinner 	uint64_t	ifree = 0;
98492821e2bSDavid Chinner 	uint64_t	ialloc = 0;
98592821e2bSDavid Chinner 	uint64_t	bfree = 0;
98692821e2bSDavid Chinner 	uint64_t	bfreelst = 0;
98792821e2bSDavid Chinner 	uint64_t	btree = 0;
98892821e2bSDavid Chinner 	int		error;
98992821e2bSDavid Chinner 
99092821e2bSDavid Chinner 	for (index = 0; index < agcount; index++) {
99192821e2bSDavid Chinner 		/*
99292821e2bSDavid Chinner 		 * read the agf, then the agi. This gets us
9939da096fdSMalcolm Parsons 		 * all the information we need and populates the
99492821e2bSDavid Chinner 		 * per-ag structures for us.
99592821e2bSDavid Chinner 		 */
99692821e2bSDavid Chinner 		error = xfs_alloc_pagf_init(mp, NULL, index, 0);
99792821e2bSDavid Chinner 		if (error)
99892821e2bSDavid Chinner 			return error;
99992821e2bSDavid Chinner 
100092821e2bSDavid Chinner 		error = xfs_ialloc_pagi_init(mp, NULL, index);
100192821e2bSDavid Chinner 		if (error)
100292821e2bSDavid Chinner 			return error;
100344b56e0aSDave Chinner 		pag = xfs_perag_get(mp, index);
100492821e2bSDavid Chinner 		ifree += pag->pagi_freecount;
100592821e2bSDavid Chinner 		ialloc += pag->pagi_count;
100692821e2bSDavid Chinner 		bfree += pag->pagf_freeblks;
100792821e2bSDavid Chinner 		bfreelst += pag->pagf_flcount;
100892821e2bSDavid Chinner 		btree += pag->pagf_btreeblks;
100944b56e0aSDave Chinner 		xfs_perag_put(pag);
101092821e2bSDavid Chinner 	}
101192821e2bSDavid Chinner 	/*
101292821e2bSDavid Chinner 	 * Overwrite incore superblock counters with just-read data
101392821e2bSDavid Chinner 	 */
10143685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
101592821e2bSDavid Chinner 	sbp->sb_ifree = ifree;
101692821e2bSDavid Chinner 	sbp->sb_icount = ialloc;
101792821e2bSDavid Chinner 	sbp->sb_fdblocks = bfree + bfreelst + btree;
10183685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
101992821e2bSDavid Chinner 
102092821e2bSDavid Chinner 	/* Fixup the per-cpu counters as well. */
102192821e2bSDavid Chinner 	xfs_icsb_reinit_counters(mp);
102292821e2bSDavid Chinner 
102392821e2bSDavid Chinner 	return 0;
102492821e2bSDavid Chinner }
102592821e2bSDavid Chinner 
10261da177e4SLinus Torvalds /*
10270771fb45SEric Sandeen  * Update alignment values based on mount options and sb values
10281da177e4SLinus Torvalds  */
10290771fb45SEric Sandeen STATIC int
10307884bc86SChristoph Hellwig xfs_update_alignment(xfs_mount_t *mp)
10311da177e4SLinus Torvalds {
10321da177e4SLinus Torvalds 	xfs_sb_t	*sbp = &(mp->m_sb);
10331da177e4SLinus Torvalds 
10344249023aSChristoph Hellwig 	if (mp->m_dalign) {
10351da177e4SLinus Torvalds 		/*
10361da177e4SLinus Torvalds 		 * If stripe unit and stripe width are not multiples
10371da177e4SLinus Torvalds 		 * of the fs blocksize turn off alignment.
10381da177e4SLinus Torvalds 		 */
10391da177e4SLinus Torvalds 		if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
10401da177e4SLinus Torvalds 		    (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
104139a45d84SJie Liu 			xfs_warn(mp,
104239a45d84SJie Liu 		"alignment check failed: sunit/swidth vs. blocksize(%d)",
104339a45d84SJie Liu 				sbp->sb_blocksize);
10440771fb45SEric Sandeen 			return XFS_ERROR(EINVAL);
10451da177e4SLinus Torvalds 		} else {
10461da177e4SLinus Torvalds 			/*
10471da177e4SLinus Torvalds 			 * Convert the stripe unit and width to FSBs.
10481da177e4SLinus Torvalds 			 */
10491da177e4SLinus Torvalds 			mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
10501da177e4SLinus Torvalds 			if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
105153487786SDave Chinner 				xfs_warn(mp,
105239a45d84SJie Liu 			"alignment check failed: sunit/swidth vs. agsize(%d)",
10531da177e4SLinus Torvalds 					 sbp->sb_agblocks);
105439a45d84SJie Liu 				return XFS_ERROR(EINVAL);
10551da177e4SLinus Torvalds 			} else if (mp->m_dalign) {
10561da177e4SLinus Torvalds 				mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
10571da177e4SLinus Torvalds 			} else {
105839a45d84SJie Liu 				xfs_warn(mp,
105939a45d84SJie Liu 			"alignment check failed: sunit(%d) less than bsize(%d)",
106039a45d84SJie Liu 					 mp->m_dalign, sbp->sb_blocksize);
10610771fb45SEric Sandeen 				return XFS_ERROR(EINVAL);
10621da177e4SLinus Torvalds 			}
10631da177e4SLinus Torvalds 		}
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 		/*
10661da177e4SLinus Torvalds 		 * Update superblock with new values
10671da177e4SLinus Torvalds 		 * and log changes
10681da177e4SLinus Torvalds 		 */
106962118709SEric Sandeen 		if (xfs_sb_version_hasdalign(sbp)) {
10701da177e4SLinus Torvalds 			if (sbp->sb_unit != mp->m_dalign) {
10711da177e4SLinus Torvalds 				sbp->sb_unit = mp->m_dalign;
10727884bc86SChristoph Hellwig 				mp->m_update_flags |= XFS_SB_UNIT;
10731da177e4SLinus Torvalds 			}
10741da177e4SLinus Torvalds 			if (sbp->sb_width != mp->m_swidth) {
10751da177e4SLinus Torvalds 				sbp->sb_width = mp->m_swidth;
10767884bc86SChristoph Hellwig 				mp->m_update_flags |= XFS_SB_WIDTH;
10771da177e4SLinus Torvalds 			}
107834d7f603SJie Liu 		} else {
107934d7f603SJie Liu 			xfs_warn(mp,
108034d7f603SJie Liu 	"cannot change alignment: superblock does not support data alignment");
108134d7f603SJie Liu 			return XFS_ERROR(EINVAL);
10821da177e4SLinus Torvalds 		}
10831da177e4SLinus Torvalds 	} else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
108462118709SEric Sandeen 		    xfs_sb_version_hasdalign(&mp->m_sb)) {
10851da177e4SLinus Torvalds 			mp->m_dalign = sbp->sb_unit;
10861da177e4SLinus Torvalds 			mp->m_swidth = sbp->sb_width;
10871da177e4SLinus Torvalds 	}
10881da177e4SLinus Torvalds 
10890771fb45SEric Sandeen 	return 0;
10900771fb45SEric Sandeen }
10911da177e4SLinus Torvalds 
10920771fb45SEric Sandeen /*
10930771fb45SEric Sandeen  * Set the maximum inode count for this filesystem
10940771fb45SEric Sandeen  */
10950771fb45SEric Sandeen STATIC void
10960771fb45SEric Sandeen xfs_set_maxicount(xfs_mount_t *mp)
10970771fb45SEric Sandeen {
10980771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
10991da177e4SLinus Torvalds 	__uint64_t	icount;
11001da177e4SLinus Torvalds 
11010771fb45SEric Sandeen 	if (sbp->sb_imax_pct) {
11020771fb45SEric Sandeen 		/*
11030771fb45SEric Sandeen 		 * Make sure the maximum inode count is a multiple
11040771fb45SEric Sandeen 		 * of the units we allocate inodes in.
11051da177e4SLinus Torvalds 		 */
11061da177e4SLinus Torvalds 		icount = sbp->sb_dblocks * sbp->sb_imax_pct;
11071da177e4SLinus Torvalds 		do_div(icount, 100);
11081da177e4SLinus Torvalds 		do_div(icount, mp->m_ialloc_blks);
11091da177e4SLinus Torvalds 		mp->m_maxicount = (icount * mp->m_ialloc_blks)  <<
11101da177e4SLinus Torvalds 				   sbp->sb_inopblog;
11110771fb45SEric Sandeen 	} else {
11121da177e4SLinus Torvalds 		mp->m_maxicount = 0;
11131da177e4SLinus Torvalds 	}
11141da177e4SLinus Torvalds }
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds /*
11171da177e4SLinus Torvalds  * Set the default minimum read and write sizes unless
11181da177e4SLinus Torvalds  * already specified in a mount option.
11191da177e4SLinus Torvalds  * We use smaller I/O sizes when the file system
11201da177e4SLinus Torvalds  * is being used for NFS service (wsync mount option).
11211da177e4SLinus Torvalds  */
11220771fb45SEric Sandeen STATIC void
11230771fb45SEric Sandeen xfs_set_rw_sizes(xfs_mount_t *mp)
11240771fb45SEric Sandeen {
11250771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
11260771fb45SEric Sandeen 	int		readio_log, writeio_log;
11270771fb45SEric Sandeen 
11281da177e4SLinus Torvalds 	if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
11291da177e4SLinus Torvalds 		if (mp->m_flags & XFS_MOUNT_WSYNC) {
11301da177e4SLinus Torvalds 			readio_log = XFS_WSYNC_READIO_LOG;
11311da177e4SLinus Torvalds 			writeio_log = XFS_WSYNC_WRITEIO_LOG;
11321da177e4SLinus Torvalds 		} else {
11331da177e4SLinus Torvalds 			readio_log = XFS_READIO_LOG_LARGE;
11341da177e4SLinus Torvalds 			writeio_log = XFS_WRITEIO_LOG_LARGE;
11351da177e4SLinus Torvalds 		}
11361da177e4SLinus Torvalds 	} else {
11371da177e4SLinus Torvalds 		readio_log = mp->m_readio_log;
11381da177e4SLinus Torvalds 		writeio_log = mp->m_writeio_log;
11391da177e4SLinus Torvalds 	}
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 	if (sbp->sb_blocklog > readio_log) {
11421da177e4SLinus Torvalds 		mp->m_readio_log = sbp->sb_blocklog;
11431da177e4SLinus Torvalds 	} else {
11441da177e4SLinus Torvalds 		mp->m_readio_log = readio_log;
11451da177e4SLinus Torvalds 	}
11461da177e4SLinus Torvalds 	mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
11471da177e4SLinus Torvalds 	if (sbp->sb_blocklog > writeio_log) {
11481da177e4SLinus Torvalds 		mp->m_writeio_log = sbp->sb_blocklog;
11491da177e4SLinus Torvalds 	} else {
11501da177e4SLinus Torvalds 		mp->m_writeio_log = writeio_log;
11511da177e4SLinus Torvalds 	}
11521da177e4SLinus Torvalds 	mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
11530771fb45SEric Sandeen }
1154425f9dddSEric Sandeen 
11551da177e4SLinus Torvalds /*
1156055388a3SDave Chinner  * precalculate the low space thresholds for dynamic speculative preallocation.
1157055388a3SDave Chinner  */
1158055388a3SDave Chinner void
1159055388a3SDave Chinner xfs_set_low_space_thresholds(
1160055388a3SDave Chinner 	struct xfs_mount	*mp)
1161055388a3SDave Chinner {
1162055388a3SDave Chinner 	int i;
1163055388a3SDave Chinner 
1164055388a3SDave Chinner 	for (i = 0; i < XFS_LOWSP_MAX; i++) {
1165055388a3SDave Chinner 		__uint64_t space = mp->m_sb.sb_dblocks;
1166055388a3SDave Chinner 
1167055388a3SDave Chinner 		do_div(space, 100);
1168055388a3SDave Chinner 		mp->m_low_space[i] = space * (i + 1);
1169055388a3SDave Chinner 	}
1170055388a3SDave Chinner }
1171055388a3SDave Chinner 
1172055388a3SDave Chinner 
1173055388a3SDave Chinner /*
11741da177e4SLinus Torvalds  * Set whether we're using inode alignment.
11751da177e4SLinus Torvalds  */
11760771fb45SEric Sandeen STATIC void
11770771fb45SEric Sandeen xfs_set_inoalignment(xfs_mount_t *mp)
11780771fb45SEric Sandeen {
117962118709SEric Sandeen 	if (xfs_sb_version_hasalign(&mp->m_sb) &&
11801da177e4SLinus Torvalds 	    mp->m_sb.sb_inoalignmt >=
11811da177e4SLinus Torvalds 	    XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
11821da177e4SLinus Torvalds 		mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
11831da177e4SLinus Torvalds 	else
11841da177e4SLinus Torvalds 		mp->m_inoalign_mask = 0;
11851da177e4SLinus Torvalds 	/*
11861da177e4SLinus Torvalds 	 * If we are using stripe alignment, check whether
11871da177e4SLinus Torvalds 	 * the stripe unit is a multiple of the inode alignment
11881da177e4SLinus Torvalds 	 */
11891da177e4SLinus Torvalds 	if (mp->m_dalign && mp->m_inoalign_mask &&
11901da177e4SLinus Torvalds 	    !(mp->m_dalign & mp->m_inoalign_mask))
11911da177e4SLinus Torvalds 		mp->m_sinoalign = mp->m_dalign;
11921da177e4SLinus Torvalds 	else
11931da177e4SLinus Torvalds 		mp->m_sinoalign = 0;
11940771fb45SEric Sandeen }
11950771fb45SEric Sandeen 
11961da177e4SLinus Torvalds /*
11971da177e4SLinus Torvalds  * Check that the data (and log if separate) are an ok size.
11981da177e4SLinus Torvalds  */
11990771fb45SEric Sandeen STATIC int
12004249023aSChristoph Hellwig xfs_check_sizes(xfs_mount_t *mp)
12010771fb45SEric Sandeen {
12020771fb45SEric Sandeen 	xfs_buf_t	*bp;
12030771fb45SEric Sandeen 	xfs_daddr_t	d;
12040771fb45SEric Sandeen 
12051da177e4SLinus Torvalds 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
12061da177e4SLinus Torvalds 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
12070b932cccSDave Chinner 		xfs_warn(mp, "filesystem size mismatch detected");
1208657a4cffSEric Sandeen 		return XFS_ERROR(EFBIG);
12091da177e4SLinus Torvalds 	}
1210e70b73f8SDave Chinner 	bp = xfs_buf_read_uncached(mp->m_ddev_targp,
12111da177e4SLinus Torvalds 					d - XFS_FSS_TO_BB(mp, 1),
1212c3f8fc73SDave Chinner 					XFS_FSS_TO_BB(mp, 1), 0, NULL);
12131922c949SDave Chinner 	if (!bp) {
12140b932cccSDave Chinner 		xfs_warn(mp, "last sector read failed");
12151922c949SDave Chinner 		return EIO;
12161da177e4SLinus Torvalds 	}
12171922c949SDave Chinner 	xfs_buf_relse(bp);
12181da177e4SLinus Torvalds 
12194249023aSChristoph Hellwig 	if (mp->m_logdev_targp != mp->m_ddev_targp) {
12201da177e4SLinus Torvalds 		d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
12211da177e4SLinus Torvalds 		if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
12220b932cccSDave Chinner 			xfs_warn(mp, "log size mismatch detected");
1223657a4cffSEric Sandeen 			return XFS_ERROR(EFBIG);
12241da177e4SLinus Torvalds 		}
1225e70b73f8SDave Chinner 		bp = xfs_buf_read_uncached(mp->m_logdev_targp,
12261da177e4SLinus Torvalds 					d - XFS_FSB_TO_BB(mp, 1),
1227c3f8fc73SDave Chinner 					XFS_FSB_TO_BB(mp, 1), 0, NULL);
12281922c949SDave Chinner 		if (!bp) {
12290b932cccSDave Chinner 			xfs_warn(mp, "log device read failed");
12301922c949SDave Chinner 			return EIO;
12311da177e4SLinus Torvalds 		}
12321922c949SDave Chinner 		xfs_buf_relse(bp);
12330771fb45SEric Sandeen 	}
12340771fb45SEric Sandeen 	return 0;
12350771fb45SEric Sandeen }
12360771fb45SEric Sandeen 
12370771fb45SEric Sandeen /*
12387d095257SChristoph Hellwig  * Clear the quotaflags in memory and in the superblock.
12397d095257SChristoph Hellwig  */
12407d095257SChristoph Hellwig int
12417d095257SChristoph Hellwig xfs_mount_reset_sbqflags(
12427d095257SChristoph Hellwig 	struct xfs_mount	*mp)
12437d095257SChristoph Hellwig {
12447d095257SChristoph Hellwig 	int			error;
12457d095257SChristoph Hellwig 	struct xfs_trans	*tp;
12467d095257SChristoph Hellwig 
12477d095257SChristoph Hellwig 	mp->m_qflags = 0;
12487d095257SChristoph Hellwig 
12497d095257SChristoph Hellwig 	/*
12507d095257SChristoph Hellwig 	 * It is OK to look at sb_qflags here in mount path,
12517d095257SChristoph Hellwig 	 * without m_sb_lock.
12527d095257SChristoph Hellwig 	 */
12537d095257SChristoph Hellwig 	if (mp->m_sb.sb_qflags == 0)
12547d095257SChristoph Hellwig 		return 0;
12557d095257SChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
12567d095257SChristoph Hellwig 	mp->m_sb.sb_qflags = 0;
12577d095257SChristoph Hellwig 	spin_unlock(&mp->m_sb_lock);
12587d095257SChristoph Hellwig 
12597d095257SChristoph Hellwig 	/*
12607d095257SChristoph Hellwig 	 * If the fs is readonly, let the incore superblock run
12617d095257SChristoph Hellwig 	 * with quotas off but don't flush the update out to disk
12627d095257SChristoph Hellwig 	 */
12637d095257SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_RDONLY)
12647d095257SChristoph Hellwig 		return 0;
12657d095257SChristoph Hellwig 
12667d095257SChristoph Hellwig 	tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
1267b0c10b98SJeff Liu 	error = xfs_trans_reserve(tp, 0, XFS_QM_SBCHANGE_LOG_RES(mp),
1268b0c10b98SJeff Liu 				  0, 0, XFS_DEFAULT_LOG_COUNT);
12697d095257SChristoph Hellwig 	if (error) {
12707d095257SChristoph Hellwig 		xfs_trans_cancel(tp, 0);
127153487786SDave Chinner 		xfs_alert(mp, "%s: Superblock update failed!", __func__);
12727d095257SChristoph Hellwig 		return error;
12737d095257SChristoph Hellwig 	}
12747d095257SChristoph Hellwig 
12757d095257SChristoph Hellwig 	xfs_mod_sb(tp, XFS_SB_QFLAGS);
12767d095257SChristoph Hellwig 	return xfs_trans_commit(tp, 0);
12777d095257SChristoph Hellwig }
12787d095257SChristoph Hellwig 
1279d5db0f97SEric Sandeen __uint64_t
1280d5db0f97SEric Sandeen xfs_default_resblks(xfs_mount_t *mp)
1281d5db0f97SEric Sandeen {
1282d5db0f97SEric Sandeen 	__uint64_t resblks;
1283d5db0f97SEric Sandeen 
1284d5db0f97SEric Sandeen 	/*
12858babd8a2SDave Chinner 	 * We default to 5% or 8192 fsbs of space reserved, whichever is
12868babd8a2SDave Chinner 	 * smaller.  This is intended to cover concurrent allocation
12878babd8a2SDave Chinner 	 * transactions when we initially hit enospc. These each require a 4
12888babd8a2SDave Chinner 	 * block reservation. Hence by default we cover roughly 2000 concurrent
12898babd8a2SDave Chinner 	 * allocation reservations.
1290d5db0f97SEric Sandeen 	 */
1291d5db0f97SEric Sandeen 	resblks = mp->m_sb.sb_dblocks;
1292d5db0f97SEric Sandeen 	do_div(resblks, 20);
12938babd8a2SDave Chinner 	resblks = min_t(__uint64_t, resblks, 8192);
1294d5db0f97SEric Sandeen 	return resblks;
1295d5db0f97SEric Sandeen }
1296d5db0f97SEric Sandeen 
12977d095257SChristoph Hellwig /*
12980771fb45SEric Sandeen  * This function does the following on an initial mount of a file system:
12990771fb45SEric Sandeen  *	- reads the superblock from disk and init the mount struct
13000771fb45SEric Sandeen  *	- if we're a 32-bit kernel, do a size check on the superblock
13010771fb45SEric Sandeen  *		so we don't mount terabyte filesystems
13020771fb45SEric Sandeen  *	- init mount struct realtime fields
13030771fb45SEric Sandeen  *	- allocate inode hash table for fs
13040771fb45SEric Sandeen  *	- init directory manager
13050771fb45SEric Sandeen  *	- perform recovery and init the log manager
13060771fb45SEric Sandeen  */
13070771fb45SEric Sandeen int
13080771fb45SEric Sandeen xfs_mountfs(
13094249023aSChristoph Hellwig 	xfs_mount_t	*mp)
13100771fb45SEric Sandeen {
13110771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
13120771fb45SEric Sandeen 	xfs_inode_t	*rip;
13130771fb45SEric Sandeen 	__uint64_t	resblks;
13147d095257SChristoph Hellwig 	uint		quotamount = 0;
13157d095257SChristoph Hellwig 	uint		quotaflags = 0;
13160771fb45SEric Sandeen 	int		error = 0;
13170771fb45SEric Sandeen 
13180771fb45SEric Sandeen 	xfs_mount_common(mp, sbp);
13190771fb45SEric Sandeen 
13200771fb45SEric Sandeen 	/*
1321e6957ea4SEric Sandeen 	 * Check for a mismatched features2 values.  Older kernels
1322e6957ea4SEric Sandeen 	 * read & wrote into the wrong sb offset for sb_features2
1323e6957ea4SEric Sandeen 	 * on some platforms due to xfs_sb_t not being 64bit size aligned
1324e6957ea4SEric Sandeen 	 * when sb_features2 was added, which made older superblock
1325e6957ea4SEric Sandeen 	 * reading/writing routines swap it as a 64-bit value.
1326ee1c0908SDavid Chinner 	 *
1327e6957ea4SEric Sandeen 	 * For backwards compatibility, we make both slots equal.
1328e6957ea4SEric Sandeen 	 *
1329e6957ea4SEric Sandeen 	 * If we detect a mismatched field, we OR the set bits into the
1330e6957ea4SEric Sandeen 	 * existing features2 field in case it has already been modified; we
1331e6957ea4SEric Sandeen 	 * don't want to lose any features.  We then update the bad location
1332e6957ea4SEric Sandeen 	 * with the ORed value so that older kernels will see any features2
1333e6957ea4SEric Sandeen 	 * flags, and mark the two fields as needing updates once the
1334e6957ea4SEric Sandeen 	 * transaction subsystem is online.
1335ee1c0908SDavid Chinner 	 */
1336e6957ea4SEric Sandeen 	if (xfs_sb_has_mismatched_features2(sbp)) {
13370b932cccSDave Chinner 		xfs_warn(mp, "correcting sb_features alignment problem");
1338ee1c0908SDavid Chinner 		sbp->sb_features2 |= sbp->sb_bad_features2;
1339e6957ea4SEric Sandeen 		sbp->sb_bad_features2 = sbp->sb_features2;
13407884bc86SChristoph Hellwig 		mp->m_update_flags |= XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2;
1341e6957ea4SEric Sandeen 
1342e6957ea4SEric Sandeen 		/*
1343e6957ea4SEric Sandeen 		 * Re-check for ATTR2 in case it was found in bad_features2
1344e6957ea4SEric Sandeen 		 * slot.
1345e6957ea4SEric Sandeen 		 */
13467c12f296STim Shimmin 		if (xfs_sb_version_hasattr2(&mp->m_sb) &&
13477c12f296STim Shimmin 		   !(mp->m_flags & XFS_MOUNT_NOATTR2))
1348e6957ea4SEric Sandeen 			mp->m_flags |= XFS_MOUNT_ATTR2;
13497c12f296STim Shimmin 	}
1350e6957ea4SEric Sandeen 
13517c12f296STim Shimmin 	if (xfs_sb_version_hasattr2(&mp->m_sb) &&
13527c12f296STim Shimmin 	   (mp->m_flags & XFS_MOUNT_NOATTR2)) {
13537c12f296STim Shimmin 		xfs_sb_version_removeattr2(&mp->m_sb);
13547884bc86SChristoph Hellwig 		mp->m_update_flags |= XFS_SB_FEATURES2;
13557c12f296STim Shimmin 
13567c12f296STim Shimmin 		/* update sb_versionnum for the clearing of the morebits */
13577c12f296STim Shimmin 		if (!sbp->sb_features2)
13587884bc86SChristoph Hellwig 			mp->m_update_flags |= XFS_SB_VERSIONNUM;
1359ee1c0908SDavid Chinner 	}
1360ee1c0908SDavid Chinner 
1361ee1c0908SDavid Chinner 	/*
13620771fb45SEric Sandeen 	 * Check if sb_agblocks is aligned at stripe boundary
13630771fb45SEric Sandeen 	 * If sb_agblocks is NOT aligned turn off m_dalign since
13640771fb45SEric Sandeen 	 * allocator alignment is within an ag, therefore ag has
13650771fb45SEric Sandeen 	 * to be aligned at stripe boundary.
13660771fb45SEric Sandeen 	 */
13677884bc86SChristoph Hellwig 	error = xfs_update_alignment(mp);
13680771fb45SEric Sandeen 	if (error)
1369f9057e3dSChristoph Hellwig 		goto out;
13700771fb45SEric Sandeen 
13710771fb45SEric Sandeen 	xfs_alloc_compute_maxlevels(mp);
13720771fb45SEric Sandeen 	xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
13730771fb45SEric Sandeen 	xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
13740771fb45SEric Sandeen 	xfs_ialloc_compute_maxlevels(mp);
13750771fb45SEric Sandeen 
13760771fb45SEric Sandeen 	xfs_set_maxicount(mp);
13770771fb45SEric Sandeen 
137827174203SChristoph Hellwig 	error = xfs_uuid_mount(mp);
137927174203SChristoph Hellwig 	if (error)
1380f9057e3dSChristoph Hellwig 		goto out;
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 	/*
13830771fb45SEric Sandeen 	 * Set the minimum read and write sizes
13840771fb45SEric Sandeen 	 */
13850771fb45SEric Sandeen 	xfs_set_rw_sizes(mp);
13860771fb45SEric Sandeen 
1387055388a3SDave Chinner 	/* set the low space thresholds for dynamic preallocation */
1388055388a3SDave Chinner 	xfs_set_low_space_thresholds(mp);
1389055388a3SDave Chinner 
13900771fb45SEric Sandeen 	/*
13910771fb45SEric Sandeen 	 * Set the inode cluster size.
13920771fb45SEric Sandeen 	 * This may still be overridden by the file system
13930771fb45SEric Sandeen 	 * block size if it is larger than the chosen cluster size.
13940771fb45SEric Sandeen 	 */
13950771fb45SEric Sandeen 	mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
13960771fb45SEric Sandeen 
13970771fb45SEric Sandeen 	/*
13980771fb45SEric Sandeen 	 * Set inode alignment fields
13990771fb45SEric Sandeen 	 */
14000771fb45SEric Sandeen 	xfs_set_inoalignment(mp);
14010771fb45SEric Sandeen 
14020771fb45SEric Sandeen 	/*
14030771fb45SEric Sandeen 	 * Check that the data (and log if separate) are an ok size.
14040771fb45SEric Sandeen 	 */
14054249023aSChristoph Hellwig 	error = xfs_check_sizes(mp);
14060771fb45SEric Sandeen 	if (error)
1407f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
14080771fb45SEric Sandeen 
14090771fb45SEric Sandeen 	/*
14101da177e4SLinus Torvalds 	 * Initialize realtime fields in the mount structure
14111da177e4SLinus Torvalds 	 */
14120771fb45SEric Sandeen 	error = xfs_rtmount_init(mp);
14130771fb45SEric Sandeen 	if (error) {
14140b932cccSDave Chinner 		xfs_warn(mp, "RT mount failed");
1415f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
14161da177e4SLinus Torvalds 	}
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds 	/*
14191da177e4SLinus Torvalds 	 *  Copies the low order bits of the timestamp and the randomly
14201da177e4SLinus Torvalds 	 *  set "sequence" number out of a UUID.
14211da177e4SLinus Torvalds 	 */
14221da177e4SLinus Torvalds 	uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
14231da177e4SLinus Torvalds 
14241da177e4SLinus Torvalds 	mp->m_dmevmask = 0;	/* not persistent; set after each mount */
14251da177e4SLinus Torvalds 
1426f6c2d1faSNathan Scott 	xfs_dir_mount(mp);
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 	/*
14291da177e4SLinus Torvalds 	 * Initialize the attribute manager's entries.
14301da177e4SLinus Torvalds 	 */
14311da177e4SLinus Torvalds 	mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 	/*
14341da177e4SLinus Torvalds 	 * Initialize the precomputed transaction reservations values.
14351da177e4SLinus Torvalds 	 */
14361da177e4SLinus Torvalds 	xfs_trans_init(mp);
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 	/*
14391da177e4SLinus Torvalds 	 * Allocate and initialize the per-ag data.
14401da177e4SLinus Torvalds 	 */
14411c1c6ebcSDave Chinner 	spin_lock_init(&mp->m_perag_lock);
14429b98b6f3SDave Chinner 	INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
14431c1c6ebcSDave Chinner 	error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
14441c1c6ebcSDave Chinner 	if (error) {
14450b932cccSDave Chinner 		xfs_warn(mp, "Failed per-ag init: %d", error);
1446f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
14471c1c6ebcSDave Chinner 	}
14481da177e4SLinus Torvalds 
1449f9057e3dSChristoph Hellwig 	if (!sbp->sb_logblocks) {
14500b932cccSDave Chinner 		xfs_warn(mp, "no log defined");
1451f9057e3dSChristoph Hellwig 		XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp);
1452f9057e3dSChristoph Hellwig 		error = XFS_ERROR(EFSCORRUPTED);
1453f9057e3dSChristoph Hellwig 		goto out_free_perag;
1454f9057e3dSChristoph Hellwig 	}
1455f9057e3dSChristoph Hellwig 
14561da177e4SLinus Torvalds 	/*
14571da177e4SLinus Torvalds 	 * log's mount-time initialization. Perform 1st part recovery if needed
14581da177e4SLinus Torvalds 	 */
14591da177e4SLinus Torvalds 	error = xfs_log_mount(mp, mp->m_logdev_targp,
14601da177e4SLinus Torvalds 			      XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
14611da177e4SLinus Torvalds 			      XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
14621da177e4SLinus Torvalds 	if (error) {
14630b932cccSDave Chinner 		xfs_warn(mp, "log mount failed");
1464d4f3512bSDave Chinner 		goto out_fail_wait;
14651da177e4SLinus Torvalds 	}
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds 	/*
146892821e2bSDavid Chinner 	 * Now the log is mounted, we know if it was an unclean shutdown or
146992821e2bSDavid Chinner 	 * not. If it was, with the first phase of recovery has completed, we
147092821e2bSDavid Chinner 	 * have consistent AG blocks on disk. We have not recovered EFIs yet,
147192821e2bSDavid Chinner 	 * but they are recovered transactionally in the second recovery phase
147292821e2bSDavid Chinner 	 * later.
147392821e2bSDavid Chinner 	 *
147492821e2bSDavid Chinner 	 * Hence we can safely re-initialise incore superblock counters from
147592821e2bSDavid Chinner 	 * the per-ag data. These may not be correct if the filesystem was not
147692821e2bSDavid Chinner 	 * cleanly unmounted, so we need to wait for recovery to finish before
147792821e2bSDavid Chinner 	 * doing this.
147892821e2bSDavid Chinner 	 *
147992821e2bSDavid Chinner 	 * If the filesystem was cleanly unmounted, then we can trust the
148092821e2bSDavid Chinner 	 * values in the superblock to be correct and we don't need to do
148192821e2bSDavid Chinner 	 * anything here.
148292821e2bSDavid Chinner 	 *
148392821e2bSDavid Chinner 	 * If we are currently making the filesystem, the initialisation will
148492821e2bSDavid Chinner 	 * fail as the perag data is in an undefined state.
148592821e2bSDavid Chinner 	 */
148692821e2bSDavid Chinner 	if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
148792821e2bSDavid Chinner 	    !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
148892821e2bSDavid Chinner 	     !mp->m_sb.sb_inprogress) {
148992821e2bSDavid Chinner 		error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
1490f9057e3dSChristoph Hellwig 		if (error)
1491d4f3512bSDave Chinner 			goto out_fail_wait;
149292821e2bSDavid Chinner 	}
1493f9057e3dSChristoph Hellwig 
149492821e2bSDavid Chinner 	/*
14951da177e4SLinus Torvalds 	 * Get and sanity-check the root inode.
14961da177e4SLinus Torvalds 	 * Save the pointer to it in the mount structure.
14971da177e4SLinus Torvalds 	 */
14987b6259e7SDave Chinner 	error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip);
14991da177e4SLinus Torvalds 	if (error) {
15000b932cccSDave Chinner 		xfs_warn(mp, "failed to read root inode");
1501f9057e3dSChristoph Hellwig 		goto out_log_dealloc;
15021da177e4SLinus Torvalds 	}
15031da177e4SLinus Torvalds 
15041da177e4SLinus Torvalds 	ASSERT(rip != NULL);
15051da177e4SLinus Torvalds 
1506abbede1bSAl Viro 	if (unlikely(!S_ISDIR(rip->i_d.di_mode))) {
15070b932cccSDave Chinner 		xfs_warn(mp, "corrupted root inode %llu: not a directory",
1508b6574520SNathan Scott 			(unsigned long long)rip->i_ino);
15091da177e4SLinus Torvalds 		xfs_iunlock(rip, XFS_ILOCK_EXCL);
15101da177e4SLinus Torvalds 		XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
15111da177e4SLinus Torvalds 				 mp);
15121da177e4SLinus Torvalds 		error = XFS_ERROR(EFSCORRUPTED);
1513f9057e3dSChristoph Hellwig 		goto out_rele_rip;
15141da177e4SLinus Torvalds 	}
15151da177e4SLinus Torvalds 	mp->m_rootip = rip;	/* save it */
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds 	xfs_iunlock(rip, XFS_ILOCK_EXCL);
15181da177e4SLinus Torvalds 
15191da177e4SLinus Torvalds 	/*
15201da177e4SLinus Torvalds 	 * Initialize realtime inode pointers in the mount structure
15211da177e4SLinus Torvalds 	 */
15220771fb45SEric Sandeen 	error = xfs_rtmount_inodes(mp);
15230771fb45SEric Sandeen 	if (error) {
15241da177e4SLinus Torvalds 		/*
15251da177e4SLinus Torvalds 		 * Free up the root inode.
15261da177e4SLinus Torvalds 		 */
15270b932cccSDave Chinner 		xfs_warn(mp, "failed to read RT inodes");
1528f9057e3dSChristoph Hellwig 		goto out_rele_rip;
15291da177e4SLinus Torvalds 	}
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 	/*
15327884bc86SChristoph Hellwig 	 * If this is a read-only mount defer the superblock updates until
15337884bc86SChristoph Hellwig 	 * the next remount into writeable mode.  Otherwise we would never
15347884bc86SChristoph Hellwig 	 * perform the update e.g. for the root filesystem.
15351da177e4SLinus Torvalds 	 */
15367884bc86SChristoph Hellwig 	if (mp->m_update_flags && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
15377884bc86SChristoph Hellwig 		error = xfs_mount_log_sb(mp, mp->m_update_flags);
1538e5720eecSDavid Chinner 		if (error) {
15390b932cccSDave Chinner 			xfs_warn(mp, "failed to write sb changes");
1540b93b6e43SChristoph Hellwig 			goto out_rtunmount;
1541e5720eecSDavid Chinner 		}
1542e5720eecSDavid Chinner 	}
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds 	/*
15451da177e4SLinus Torvalds 	 * Initialise the XFS quota management subsystem for this mount
15461da177e4SLinus Torvalds 	 */
15477d095257SChristoph Hellwig 	if (XFS_IS_QUOTA_RUNNING(mp)) {
15487d095257SChristoph Hellwig 		error = xfs_qm_newmount(mp, &quotamount, &quotaflags);
15490771fb45SEric Sandeen 		if (error)
1550b93b6e43SChristoph Hellwig 			goto out_rtunmount;
15517d095257SChristoph Hellwig 	} else {
15527d095257SChristoph Hellwig 		ASSERT(!XFS_IS_QUOTA_ON(mp));
15537d095257SChristoph Hellwig 
15547d095257SChristoph Hellwig 		/*
15557d095257SChristoph Hellwig 		 * If a file system had quotas running earlier, but decided to
15567d095257SChristoph Hellwig 		 * mount without -o uquota/pquota/gquota options, revoke the
15577d095257SChristoph Hellwig 		 * quotachecked license.
15587d095257SChristoph Hellwig 		 */
15597d095257SChristoph Hellwig 		if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) {
15600b932cccSDave Chinner 			xfs_notice(mp, "resetting quota flags");
15617d095257SChristoph Hellwig 			error = xfs_mount_reset_sbqflags(mp);
15627d095257SChristoph Hellwig 			if (error)
15637d095257SChristoph Hellwig 				return error;
15647d095257SChristoph Hellwig 		}
15657d095257SChristoph Hellwig 	}
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds 	/*
15681da177e4SLinus Torvalds 	 * Finish recovering the file system.  This part needed to be
15691da177e4SLinus Torvalds 	 * delayed until after the root and real-time bitmap inodes
15701da177e4SLinus Torvalds 	 * were consistently read in.
15711da177e4SLinus Torvalds 	 */
15724249023aSChristoph Hellwig 	error = xfs_log_mount_finish(mp);
15731da177e4SLinus Torvalds 	if (error) {
15740b932cccSDave Chinner 		xfs_warn(mp, "log mount finish failed");
1575b93b6e43SChristoph Hellwig 		goto out_rtunmount;
15761da177e4SLinus Torvalds 	}
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds 	/*
15791da177e4SLinus Torvalds 	 * Complete the quota initialisation, post-log-replay component.
15801da177e4SLinus Torvalds 	 */
15817d095257SChristoph Hellwig 	if (quotamount) {
15827d095257SChristoph Hellwig 		ASSERT(mp->m_qflags == 0);
15837d095257SChristoph Hellwig 		mp->m_qflags = quotaflags;
15847d095257SChristoph Hellwig 
15857d095257SChristoph Hellwig 		xfs_qm_mount_quotas(mp);
15867d095257SChristoph Hellwig 	}
15877d095257SChristoph Hellwig 
158884e1e99fSDavid Chinner 	/*
158984e1e99fSDavid Chinner 	 * Now we are mounted, reserve a small amount of unused space for
159084e1e99fSDavid Chinner 	 * privileged transactions. This is needed so that transaction
159184e1e99fSDavid Chinner 	 * space required for critical operations can dip into this pool
159284e1e99fSDavid Chinner 	 * when at ENOSPC. This is needed for operations like create with
159384e1e99fSDavid Chinner 	 * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
159484e1e99fSDavid Chinner 	 * are not allowed to use this reserved space.
15958babd8a2SDave Chinner 	 *
15968babd8a2SDave Chinner 	 * This may drive us straight to ENOSPC on mount, but that implies
15978babd8a2SDave Chinner 	 * we were already there on the last unmount. Warn if this occurs.
159884e1e99fSDavid Chinner 	 */
1599d5db0f97SEric Sandeen 	if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
1600d5db0f97SEric Sandeen 		resblks = xfs_default_resblks(mp);
1601714082bcSDavid Chinner 		error = xfs_reserve_blocks(mp, &resblks, NULL);
1602714082bcSDavid Chinner 		if (error)
16030b932cccSDave Chinner 			xfs_warn(mp,
16040b932cccSDave Chinner 	"Unable to allocate reserve blocks. Continuing without reserve pool.");
1605d5db0f97SEric Sandeen 	}
160684e1e99fSDavid Chinner 
16071da177e4SLinus Torvalds 	return 0;
16081da177e4SLinus Torvalds 
1609b93b6e43SChristoph Hellwig  out_rtunmount:
1610b93b6e43SChristoph Hellwig 	xfs_rtunmount_inodes(mp);
1611f9057e3dSChristoph Hellwig  out_rele_rip:
161243355099SChristoph Hellwig 	IRELE(rip);
1613f9057e3dSChristoph Hellwig  out_log_dealloc:
161421b699c8SChristoph Hellwig 	xfs_log_unmount(mp);
1615d4f3512bSDave Chinner  out_fail_wait:
1616d4f3512bSDave Chinner 	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
1617d4f3512bSDave Chinner 		xfs_wait_buftarg(mp->m_logdev_targp);
1618d4f3512bSDave Chinner 	xfs_wait_buftarg(mp->m_ddev_targp);
1619f9057e3dSChristoph Hellwig  out_free_perag:
1620ff4f038cSChristoph Hellwig 	xfs_free_perag(mp);
1621f9057e3dSChristoph Hellwig  out_remove_uuid:
162227174203SChristoph Hellwig 	xfs_uuid_unmount(mp);
1623f9057e3dSChristoph Hellwig  out:
16241da177e4SLinus Torvalds 	return error;
16251da177e4SLinus Torvalds }
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds /*
16281da177e4SLinus Torvalds  * This flushes out the inodes,dquots and the superblock, unmounts the
16291da177e4SLinus Torvalds  * log and makes sure that incore structures are freed.
16301da177e4SLinus Torvalds  */
163141b5c2e7SChristoph Hellwig void
163241b5c2e7SChristoph Hellwig xfs_unmountfs(
163341b5c2e7SChristoph Hellwig 	struct xfs_mount	*mp)
16341da177e4SLinus Torvalds {
163584e1e99fSDavid Chinner 	__uint64_t		resblks;
163641b5c2e7SChristoph Hellwig 	int			error;
16371da177e4SLinus Torvalds 
1638579b62faSBrian Foster 	cancel_delayed_work_sync(&mp->m_eofblocks_work);
1639579b62faSBrian Foster 
16407d095257SChristoph Hellwig 	xfs_qm_unmount_quotas(mp);
1641b93b6e43SChristoph Hellwig 	xfs_rtunmount_inodes(mp);
164277508ec8SChristoph Hellwig 	IRELE(mp->m_rootip);
164377508ec8SChristoph Hellwig 
1644641c56fbSDavid Chinner 	/*
1645641c56fbSDavid Chinner 	 * We can potentially deadlock here if we have an inode cluster
16469da096fdSMalcolm Parsons 	 * that has been freed has its buffer still pinned in memory because
1647641c56fbSDavid Chinner 	 * the transaction is still sitting in a iclog. The stale inodes
1648641c56fbSDavid Chinner 	 * on that buffer will have their flush locks held until the
1649641c56fbSDavid Chinner 	 * transaction hits the disk and the callbacks run. the inode
1650641c56fbSDavid Chinner 	 * flush takes the flush lock unconditionally and with nothing to
1651641c56fbSDavid Chinner 	 * push out the iclog we will never get that unlocked. hence we
1652641c56fbSDavid Chinner 	 * need to force the log first.
1653641c56fbSDavid Chinner 	 */
1654a14a348bSChristoph Hellwig 	xfs_log_force(mp, XFS_LOG_SYNC);
1655c854363eSDave Chinner 
1656c854363eSDave Chinner 	/*
1657211e4d43SChristoph Hellwig 	 * Flush all pending changes from the AIL.
1658c854363eSDave Chinner 	 */
1659211e4d43SChristoph Hellwig 	xfs_ail_push_all_sync(mp->m_ail);
1660211e4d43SChristoph Hellwig 
1661211e4d43SChristoph Hellwig 	/*
1662211e4d43SChristoph Hellwig 	 * And reclaim all inodes.  At this point there should be no dirty
16637e18530bSDave Chinner 	 * inodes and none should be pinned or locked, but use synchronous
16647e18530bSDave Chinner 	 * reclaim just to be sure. We can stop background inode reclaim
16657e18530bSDave Chinner 	 * here as well if it is still running.
1666211e4d43SChristoph Hellwig 	 */
16677e18530bSDave Chinner 	cancel_delayed_work_sync(&mp->m_reclaim_work);
1668c854363eSDave Chinner 	xfs_reclaim_inodes(mp, SYNC_WAIT);
16691da177e4SLinus Torvalds 
16707d095257SChristoph Hellwig 	xfs_qm_unmount(mp);
1671a357a121SLachlan McIlroy 
16721da177e4SLinus Torvalds 	/*
167384e1e99fSDavid Chinner 	 * Unreserve any blocks we have so that when we unmount we don't account
167484e1e99fSDavid Chinner 	 * the reserved free space as used. This is really only necessary for
167584e1e99fSDavid Chinner 	 * lazy superblock counting because it trusts the incore superblock
16769da096fdSMalcolm Parsons 	 * counters to be absolutely correct on clean unmount.
167784e1e99fSDavid Chinner 	 *
167884e1e99fSDavid Chinner 	 * We don't bother correcting this elsewhere for lazy superblock
167984e1e99fSDavid Chinner 	 * counting because on mount of an unclean filesystem we reconstruct the
168084e1e99fSDavid Chinner 	 * correct counter value and this is irrelevant.
168184e1e99fSDavid Chinner 	 *
168284e1e99fSDavid Chinner 	 * For non-lazy counter filesystems, this doesn't matter at all because
168384e1e99fSDavid Chinner 	 * we only every apply deltas to the superblock and hence the incore
168484e1e99fSDavid Chinner 	 * value does not matter....
168584e1e99fSDavid Chinner 	 */
168684e1e99fSDavid Chinner 	resblks = 0;
1687714082bcSDavid Chinner 	error = xfs_reserve_blocks(mp, &resblks, NULL);
1688714082bcSDavid Chinner 	if (error)
16890b932cccSDave Chinner 		xfs_warn(mp, "Unable to free reserved block pool. "
1690714082bcSDavid Chinner 				"Freespace may not be correct on next mount.");
1691714082bcSDavid Chinner 
1692adab0f67SChandra Seetharaman 	error = xfs_log_sbcount(mp);
1693e5720eecSDavid Chinner 	if (error)
16940b932cccSDave Chinner 		xfs_warn(mp, "Unable to update superblock counters. "
1695e5720eecSDavid Chinner 				"Freespace may not be correct on next mount.");
169687c7bec7SChristoph Hellwig 
169721b699c8SChristoph Hellwig 	xfs_log_unmount(mp);
169827174203SChristoph Hellwig 	xfs_uuid_unmount(mp);
16991da177e4SLinus Torvalds 
17001550d0b0SChristoph Hellwig #if defined(DEBUG)
17010ce4cfd4SChristoph Hellwig 	xfs_errortag_clearall(mp, 0);
17021da177e4SLinus Torvalds #endif
1703ff4f038cSChristoph Hellwig 	xfs_free_perag(mp);
17041da177e4SLinus Torvalds }
17051da177e4SLinus Torvalds 
17061da177e4SLinus Torvalds int
170792821e2bSDavid Chinner xfs_fs_writable(xfs_mount_t *mp)
170892821e2bSDavid Chinner {
1709d9457dc0SJan Kara 	return !(mp->m_super->s_writers.frozen || XFS_FORCED_SHUTDOWN(mp) ||
1710bd186aa9SChristoph Hellwig 		(mp->m_flags & XFS_MOUNT_RDONLY));
171192821e2bSDavid Chinner }
171292821e2bSDavid Chinner 
171392821e2bSDavid Chinner /*
1714b2ce3974SAlex Elder  * xfs_log_sbcount
1715b2ce3974SAlex Elder  *
1716adab0f67SChandra Seetharaman  * Sync the superblock counters to disk.
1717b2ce3974SAlex Elder  *
1718b2ce3974SAlex Elder  * Note this code can be called during the process of freezing, so
1719adab0f67SChandra Seetharaman  * we may need to use the transaction allocator which does not
1720b2ce3974SAlex Elder  * block when the transaction subsystem is in its frozen state.
172192821e2bSDavid Chinner  */
172292821e2bSDavid Chinner int
1723adab0f67SChandra Seetharaman xfs_log_sbcount(xfs_mount_t *mp)
172492821e2bSDavid Chinner {
172592821e2bSDavid Chinner 	xfs_trans_t	*tp;
172692821e2bSDavid Chinner 	int		error;
172792821e2bSDavid Chinner 
172892821e2bSDavid Chinner 	if (!xfs_fs_writable(mp))
172992821e2bSDavid Chinner 		return 0;
173092821e2bSDavid Chinner 
1731d4d90b57SChristoph Hellwig 	xfs_icsb_sync_counters(mp, 0);
173292821e2bSDavid Chinner 
173392821e2bSDavid Chinner 	/*
173492821e2bSDavid Chinner 	 * we don't need to do this if we are updating the superblock
173592821e2bSDavid Chinner 	 * counters on every modification.
173692821e2bSDavid Chinner 	 */
173792821e2bSDavid Chinner 	if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
173892821e2bSDavid Chinner 		return 0;
173992821e2bSDavid Chinner 
1740b2ce3974SAlex Elder 	tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT, KM_SLEEP);
1741e457274bSJeff Liu 	error = xfs_trans_reserve(tp, 0, XFS_SB_LOG_RES(mp), 0, 0,
174292821e2bSDavid Chinner 				  XFS_DEFAULT_LOG_COUNT);
174392821e2bSDavid Chinner 	if (error) {
174492821e2bSDavid Chinner 		xfs_trans_cancel(tp, 0);
174592821e2bSDavid Chinner 		return error;
174692821e2bSDavid Chinner 	}
174792821e2bSDavid Chinner 
174892821e2bSDavid Chinner 	xfs_mod_sb(tp, XFS_SB_IFREE | XFS_SB_ICOUNT | XFS_SB_FDBLOCKS);
174992821e2bSDavid Chinner 	xfs_trans_set_sync(tp);
1750e5720eecSDavid Chinner 	error = xfs_trans_commit(tp, 0);
1751e5720eecSDavid Chinner 	return error;
175292821e2bSDavid Chinner }
175392821e2bSDavid Chinner 
17541da177e4SLinus Torvalds /*
17551da177e4SLinus Torvalds  * xfs_mod_sb() can be used to copy arbitrary changes to the
17561da177e4SLinus Torvalds  * in-core superblock into the superblock buffer to be logged.
17571da177e4SLinus Torvalds  * It does not provide the higher level of locking that is
17581da177e4SLinus Torvalds  * needed to protect the in-core superblock from concurrent
17591da177e4SLinus Torvalds  * access.
17601da177e4SLinus Torvalds  */
17611da177e4SLinus Torvalds void
17621da177e4SLinus Torvalds xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
17631da177e4SLinus Torvalds {
17641da177e4SLinus Torvalds 	xfs_buf_t	*bp;
17651da177e4SLinus Torvalds 	int		first;
17661da177e4SLinus Torvalds 	int		last;
17671da177e4SLinus Torvalds 	xfs_mount_t	*mp;
17681da177e4SLinus Torvalds 	xfs_sb_field_t	f;
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds 	ASSERT(fields);
17711da177e4SLinus Torvalds 	if (!fields)
17721da177e4SLinus Torvalds 		return;
17731da177e4SLinus Torvalds 	mp = tp->t_mountp;
17741da177e4SLinus Torvalds 	bp = xfs_trans_getsb(tp, mp, 0);
17751da177e4SLinus Torvalds 	first = sizeof(xfs_sb_t);
17761da177e4SLinus Torvalds 	last = 0;
17771da177e4SLinus Torvalds 
17781da177e4SLinus Torvalds 	/* translate/copy */
17791da177e4SLinus Torvalds 
17802bdf7cd0SChristoph Hellwig 	xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb, fields);
17811da177e4SLinus Torvalds 
17821da177e4SLinus Torvalds 	/* find modified range */
1783587aa0feSDave Chinner 	f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
1784587aa0feSDave Chinner 	ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1785587aa0feSDave Chinner 	last = xfs_sb_info[f + 1].offset - 1;
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds 	f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
17881da177e4SLinus Torvalds 	ASSERT((1LL << f) & XFS_SB_MOD_BITS);
17891da177e4SLinus Torvalds 	first = xfs_sb_info[f].offset;
17901da177e4SLinus Torvalds 
179104a1e6c5SDave Chinner 	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
17921da177e4SLinus Torvalds 	xfs_trans_log_buf(tp, bp, first, last);
17931da177e4SLinus Torvalds }
1794d210a28cSYingping Lu 
1795d210a28cSYingping Lu 
17961da177e4SLinus Torvalds /*
17971da177e4SLinus Torvalds  * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
17981da177e4SLinus Torvalds  * a delta to a specified field in the in-core superblock.  Simply
17991da177e4SLinus Torvalds  * switch on the field indicated and apply the delta to that field.
18001da177e4SLinus Torvalds  * Fields are not allowed to dip below zero, so if the delta would
18011da177e4SLinus Torvalds  * do this do not apply it and return EINVAL.
18021da177e4SLinus Torvalds  *
18033685c2a1SEric Sandeen  * The m_sb_lock must be held when this routine is called.
18041da177e4SLinus Torvalds  */
1805d96f8f89SEric Sandeen STATIC int
180620f4ebf2SDavid Chinner xfs_mod_incore_sb_unlocked(
180720f4ebf2SDavid Chinner 	xfs_mount_t	*mp,
180820f4ebf2SDavid Chinner 	xfs_sb_field_t	field,
180920f4ebf2SDavid Chinner 	int64_t		delta,
181020f4ebf2SDavid Chinner 	int		rsvd)
18111da177e4SLinus Torvalds {
18121da177e4SLinus Torvalds 	int		scounter;	/* short counter for 32 bit fields */
18131da177e4SLinus Torvalds 	long long	lcounter;	/* long counter for 64 bit fields */
18141da177e4SLinus Torvalds 	long long	res_used, rem;
18151da177e4SLinus Torvalds 
18161da177e4SLinus Torvalds 	/*
18171da177e4SLinus Torvalds 	 * With the in-core superblock spin lock held, switch
18181da177e4SLinus Torvalds 	 * on the indicated field.  Apply the delta to the
18191da177e4SLinus Torvalds 	 * proper field.  If the fields value would dip below
18201da177e4SLinus Torvalds 	 * 0, then do not apply the delta and return EINVAL.
18211da177e4SLinus Torvalds 	 */
18221da177e4SLinus Torvalds 	switch (field) {
18231da177e4SLinus Torvalds 	case XFS_SBS_ICOUNT:
18241da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_icount;
18251da177e4SLinus Torvalds 		lcounter += delta;
18261da177e4SLinus Torvalds 		if (lcounter < 0) {
18271da177e4SLinus Torvalds 			ASSERT(0);
1828014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
18291da177e4SLinus Torvalds 		}
18301da177e4SLinus Torvalds 		mp->m_sb.sb_icount = lcounter;
1831014c2544SJesper Juhl 		return 0;
18321da177e4SLinus Torvalds 	case XFS_SBS_IFREE:
18331da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_ifree;
18341da177e4SLinus Torvalds 		lcounter += delta;
18351da177e4SLinus Torvalds 		if (lcounter < 0) {
18361da177e4SLinus Torvalds 			ASSERT(0);
1837014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
18381da177e4SLinus Torvalds 		}
18391da177e4SLinus Torvalds 		mp->m_sb.sb_ifree = lcounter;
1840014c2544SJesper Juhl 		return 0;
18411da177e4SLinus Torvalds 	case XFS_SBS_FDBLOCKS:
18424be536deSDavid Chinner 		lcounter = (long long)
18434be536deSDavid Chinner 			mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
18441da177e4SLinus Torvalds 		res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
18451da177e4SLinus Torvalds 
18461da177e4SLinus Torvalds 		if (delta > 0) {		/* Putting blocks back */
18471da177e4SLinus Torvalds 			if (res_used > delta) {
18481da177e4SLinus Torvalds 				mp->m_resblks_avail += delta;
18491da177e4SLinus Torvalds 			} else {
18501da177e4SLinus Torvalds 				rem = delta - res_used;
18511da177e4SLinus Torvalds 				mp->m_resblks_avail = mp->m_resblks;
18521da177e4SLinus Torvalds 				lcounter += rem;
18531da177e4SLinus Torvalds 			}
18541da177e4SLinus Torvalds 		} else {				/* Taking blocks away */
18551da177e4SLinus Torvalds 			lcounter += delta;
18568babd8a2SDave Chinner 			if (lcounter >= 0) {
18578babd8a2SDave Chinner 				mp->m_sb.sb_fdblocks = lcounter +
18588babd8a2SDave Chinner 							XFS_ALLOC_SET_ASIDE(mp);
18598babd8a2SDave Chinner 				return 0;
18608babd8a2SDave Chinner 			}
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds 			/*
18638babd8a2SDave Chinner 			 * We are out of blocks, use any available reserved
18648babd8a2SDave Chinner 			 * blocks if were allowed to.
18651da177e4SLinus Torvalds 			 */
18668babd8a2SDave Chinner 			if (!rsvd)
1867014c2544SJesper Juhl 				return XFS_ERROR(ENOSPC);
18688babd8a2SDave Chinner 
18698babd8a2SDave Chinner 			lcounter = (long long)mp->m_resblks_avail + delta;
18708babd8a2SDave Chinner 			if (lcounter >= 0) {
18711da177e4SLinus Torvalds 				mp->m_resblks_avail = lcounter;
1872014c2544SJesper Juhl 				return 0;
18738babd8a2SDave Chinner 			}
18748babd8a2SDave Chinner 			printk_once(KERN_WARNING
18758babd8a2SDave Chinner 				"Filesystem \"%s\": reserve blocks depleted! "
18768babd8a2SDave Chinner 				"Consider increasing reserve pool size.",
18778babd8a2SDave Chinner 				mp->m_fsname);
1878014c2544SJesper Juhl 			return XFS_ERROR(ENOSPC);
18791da177e4SLinus Torvalds 		}
18801da177e4SLinus Torvalds 
18814be536deSDavid Chinner 		mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
1882014c2544SJesper Juhl 		return 0;
18831da177e4SLinus Torvalds 	case XFS_SBS_FREXTENTS:
18841da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_frextents;
18851da177e4SLinus Torvalds 		lcounter += delta;
18861da177e4SLinus Torvalds 		if (lcounter < 0) {
1887014c2544SJesper Juhl 			return XFS_ERROR(ENOSPC);
18881da177e4SLinus Torvalds 		}
18891da177e4SLinus Torvalds 		mp->m_sb.sb_frextents = lcounter;
1890014c2544SJesper Juhl 		return 0;
18911da177e4SLinus Torvalds 	case XFS_SBS_DBLOCKS:
18921da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_dblocks;
18931da177e4SLinus Torvalds 		lcounter += delta;
18941da177e4SLinus Torvalds 		if (lcounter < 0) {
18951da177e4SLinus Torvalds 			ASSERT(0);
1896014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
18971da177e4SLinus Torvalds 		}
18981da177e4SLinus Torvalds 		mp->m_sb.sb_dblocks = lcounter;
1899014c2544SJesper Juhl 		return 0;
19001da177e4SLinus Torvalds 	case XFS_SBS_AGCOUNT:
19011da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_agcount;
19021da177e4SLinus Torvalds 		scounter += delta;
19031da177e4SLinus Torvalds 		if (scounter < 0) {
19041da177e4SLinus Torvalds 			ASSERT(0);
1905014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
19061da177e4SLinus Torvalds 		}
19071da177e4SLinus Torvalds 		mp->m_sb.sb_agcount = scounter;
1908014c2544SJesper Juhl 		return 0;
19091da177e4SLinus Torvalds 	case XFS_SBS_IMAX_PCT:
19101da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_imax_pct;
19111da177e4SLinus Torvalds 		scounter += delta;
19121da177e4SLinus Torvalds 		if (scounter < 0) {
19131da177e4SLinus Torvalds 			ASSERT(0);
1914014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
19151da177e4SLinus Torvalds 		}
19161da177e4SLinus Torvalds 		mp->m_sb.sb_imax_pct = scounter;
1917014c2544SJesper Juhl 		return 0;
19181da177e4SLinus Torvalds 	case XFS_SBS_REXTSIZE:
19191da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rextsize;
19201da177e4SLinus Torvalds 		scounter += delta;
19211da177e4SLinus Torvalds 		if (scounter < 0) {
19221da177e4SLinus Torvalds 			ASSERT(0);
1923014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
19241da177e4SLinus Torvalds 		}
19251da177e4SLinus Torvalds 		mp->m_sb.sb_rextsize = scounter;
1926014c2544SJesper Juhl 		return 0;
19271da177e4SLinus Torvalds 	case XFS_SBS_RBMBLOCKS:
19281da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rbmblocks;
19291da177e4SLinus Torvalds 		scounter += delta;
19301da177e4SLinus Torvalds 		if (scounter < 0) {
19311da177e4SLinus Torvalds 			ASSERT(0);
1932014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
19331da177e4SLinus Torvalds 		}
19341da177e4SLinus Torvalds 		mp->m_sb.sb_rbmblocks = scounter;
1935014c2544SJesper Juhl 		return 0;
19361da177e4SLinus Torvalds 	case XFS_SBS_RBLOCKS:
19371da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_rblocks;
19381da177e4SLinus Torvalds 		lcounter += delta;
19391da177e4SLinus Torvalds 		if (lcounter < 0) {
19401da177e4SLinus Torvalds 			ASSERT(0);
1941014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
19421da177e4SLinus Torvalds 		}
19431da177e4SLinus Torvalds 		mp->m_sb.sb_rblocks = lcounter;
1944014c2544SJesper Juhl 		return 0;
19451da177e4SLinus Torvalds 	case XFS_SBS_REXTENTS:
19461da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_rextents;
19471da177e4SLinus Torvalds 		lcounter += delta;
19481da177e4SLinus Torvalds 		if (lcounter < 0) {
19491da177e4SLinus Torvalds 			ASSERT(0);
1950014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
19511da177e4SLinus Torvalds 		}
19521da177e4SLinus Torvalds 		mp->m_sb.sb_rextents = lcounter;
1953014c2544SJesper Juhl 		return 0;
19541da177e4SLinus Torvalds 	case XFS_SBS_REXTSLOG:
19551da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rextslog;
19561da177e4SLinus Torvalds 		scounter += delta;
19571da177e4SLinus Torvalds 		if (scounter < 0) {
19581da177e4SLinus Torvalds 			ASSERT(0);
1959014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
19601da177e4SLinus Torvalds 		}
19611da177e4SLinus Torvalds 		mp->m_sb.sb_rextslog = scounter;
1962014c2544SJesper Juhl 		return 0;
19631da177e4SLinus Torvalds 	default:
19641da177e4SLinus Torvalds 		ASSERT(0);
1965014c2544SJesper Juhl 		return XFS_ERROR(EINVAL);
19661da177e4SLinus Torvalds 	}
19671da177e4SLinus Torvalds }
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds /*
19701da177e4SLinus Torvalds  * xfs_mod_incore_sb() is used to change a field in the in-core
19711da177e4SLinus Torvalds  * superblock structure by the specified delta.  This modification
19723685c2a1SEric Sandeen  * is protected by the m_sb_lock.  Just use the xfs_mod_incore_sb_unlocked()
19731da177e4SLinus Torvalds  * routine to do the work.
19741da177e4SLinus Torvalds  */
19751da177e4SLinus Torvalds int
197620f4ebf2SDavid Chinner xfs_mod_incore_sb(
197796540c78SChristoph Hellwig 	struct xfs_mount	*mp,
197820f4ebf2SDavid Chinner 	xfs_sb_field_t		field,
197920f4ebf2SDavid Chinner 	int64_t			delta,
198020f4ebf2SDavid Chinner 	int			rsvd)
19811da177e4SLinus Torvalds {
19821da177e4SLinus Torvalds 	int			status;
19831da177e4SLinus Torvalds 
19848d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
198596540c78SChristoph Hellwig 	ASSERT(field < XFS_SBS_ICOUNT || field > XFS_SBS_FDBLOCKS);
19868d280b98SDavid Chinner #endif
19873685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
19881da177e4SLinus Torvalds 	status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
19893685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
19908d280b98SDavid Chinner 
1991014c2544SJesper Juhl 	return status;
19921da177e4SLinus Torvalds }
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds /*
19951b040712SChristoph Hellwig  * Change more than one field in the in-core superblock structure at a time.
19961da177e4SLinus Torvalds  *
19971b040712SChristoph Hellwig  * The fields and changes to those fields are specified in the array of
19981b040712SChristoph Hellwig  * xfs_mod_sb structures passed in.  Either all of the specified deltas
19991b040712SChristoph Hellwig  * will be applied or none of them will.  If any modified field dips below 0,
20001b040712SChristoph Hellwig  * then all modifications will be backed out and EINVAL will be returned.
20011b040712SChristoph Hellwig  *
20021b040712SChristoph Hellwig  * Note that this function may not be used for the superblock values that
20031b040712SChristoph Hellwig  * are tracked with the in-memory per-cpu counters - a direct call to
20041b040712SChristoph Hellwig  * xfs_icsb_modify_counters is required for these.
20051da177e4SLinus Torvalds  */
20061da177e4SLinus Torvalds int
20071b040712SChristoph Hellwig xfs_mod_incore_sb_batch(
20081b040712SChristoph Hellwig 	struct xfs_mount	*mp,
20091b040712SChristoph Hellwig 	xfs_mod_sb_t		*msb,
20101b040712SChristoph Hellwig 	uint			nmsb,
20111b040712SChristoph Hellwig 	int			rsvd)
20121da177e4SLinus Torvalds {
201345c51b99SDavid Sterba 	xfs_mod_sb_t		*msbp;
20141b040712SChristoph Hellwig 	int			error = 0;
20151da177e4SLinus Torvalds 
20161da177e4SLinus Torvalds 	/*
20171b040712SChristoph Hellwig 	 * Loop through the array of mod structures and apply each individually.
20181b040712SChristoph Hellwig 	 * If any fail, then back out all those which have already been applied.
20191b040712SChristoph Hellwig 	 * Do all of this within the scope of the m_sb_lock so that all of the
20201b040712SChristoph Hellwig 	 * changes will be atomic.
20211da177e4SLinus Torvalds 	 */
20223685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
202345c51b99SDavid Sterba 	for (msbp = msb; msbp < (msb + nmsb); msbp++) {
20241b040712SChristoph Hellwig 		ASSERT(msbp->msb_field < XFS_SBS_ICOUNT ||
20251b040712SChristoph Hellwig 		       msbp->msb_field > XFS_SBS_FDBLOCKS);
20268d280b98SDavid Chinner 
20271b040712SChristoph Hellwig 		error = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
20281b040712SChristoph Hellwig 						   msbp->msb_delta, rsvd);
20291b040712SChristoph Hellwig 		if (error)
20301b040712SChristoph Hellwig 			goto unwind;
20311da177e4SLinus Torvalds 	}
20321b040712SChristoph Hellwig 	spin_unlock(&mp->m_sb_lock);
20331b040712SChristoph Hellwig 	return 0;
20341da177e4SLinus Torvalds 
20351b040712SChristoph Hellwig unwind:
20361b040712SChristoph Hellwig 	while (--msbp >= msb) {
20371b040712SChristoph Hellwig 		error = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
20381b040712SChristoph Hellwig 						   -msbp->msb_delta, rsvd);
20391b040712SChristoph Hellwig 		ASSERT(error == 0);
20401da177e4SLinus Torvalds 	}
20413685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
20421b040712SChristoph Hellwig 	return error;
20431da177e4SLinus Torvalds }
20441da177e4SLinus Torvalds 
20451da177e4SLinus Torvalds /*
20461da177e4SLinus Torvalds  * xfs_getsb() is called to obtain the buffer for the superblock.
20471da177e4SLinus Torvalds  * The buffer is returned locked and read in from disk.
20481da177e4SLinus Torvalds  * The buffer should be released with a call to xfs_brelse().
20491da177e4SLinus Torvalds  *
20501da177e4SLinus Torvalds  * If the flags parameter is BUF_TRYLOCK, then we'll only return
20511da177e4SLinus Torvalds  * the superblock buffer if it can be locked without sleeping.
20521da177e4SLinus Torvalds  * If it can't then we'll return NULL.
20531da177e4SLinus Torvalds  */
20540c842ad4SChristoph Hellwig struct xfs_buf *
20551da177e4SLinus Torvalds xfs_getsb(
20560c842ad4SChristoph Hellwig 	struct xfs_mount	*mp,
20571da177e4SLinus Torvalds 	int			flags)
20581da177e4SLinus Torvalds {
20590c842ad4SChristoph Hellwig 	struct xfs_buf		*bp = mp->m_sb_bp;
20601da177e4SLinus Torvalds 
20610c842ad4SChristoph Hellwig 	if (!xfs_buf_trylock(bp)) {
20620c842ad4SChristoph Hellwig 		if (flags & XBF_TRYLOCK)
20631da177e4SLinus Torvalds 			return NULL;
20640c842ad4SChristoph Hellwig 		xfs_buf_lock(bp);
20651da177e4SLinus Torvalds 	}
20660c842ad4SChristoph Hellwig 
206772790aa1SChandra Seetharaman 	xfs_buf_hold(bp);
20681da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISDONE(bp));
2069014c2544SJesper Juhl 	return bp;
20701da177e4SLinus Torvalds }
20711da177e4SLinus Torvalds 
20721da177e4SLinus Torvalds /*
20731da177e4SLinus Torvalds  * Used to free the superblock along various error paths.
20741da177e4SLinus Torvalds  */
20751da177e4SLinus Torvalds void
20761da177e4SLinus Torvalds xfs_freesb(
207726af6552SDave Chinner 	struct xfs_mount	*mp)
20781da177e4SLinus Torvalds {
207926af6552SDave Chinner 	struct xfs_buf		*bp = mp->m_sb_bp;
20801da177e4SLinus Torvalds 
208126af6552SDave Chinner 	xfs_buf_lock(bp);
20821da177e4SLinus Torvalds 	mp->m_sb_bp = NULL;
208326af6552SDave Chinner 	xfs_buf_relse(bp);
20841da177e4SLinus Torvalds }
20851da177e4SLinus Torvalds 
20861da177e4SLinus Torvalds /*
20871da177e4SLinus Torvalds  * Used to log changes to the superblock unit and width fields which could
2088e6957ea4SEric Sandeen  * be altered by the mount options, as well as any potential sb_features2
2089e6957ea4SEric Sandeen  * fixup. Only the first superblock is updated.
20901da177e4SLinus Torvalds  */
20917884bc86SChristoph Hellwig int
2092ee1c0908SDavid Chinner xfs_mount_log_sb(
20931da177e4SLinus Torvalds 	xfs_mount_t	*mp,
20941da177e4SLinus Torvalds 	__int64_t	fields)
20951da177e4SLinus Torvalds {
20961da177e4SLinus Torvalds 	xfs_trans_t	*tp;
2097e5720eecSDavid Chinner 	int		error;
20981da177e4SLinus Torvalds 
2099ee1c0908SDavid Chinner 	ASSERT(fields & (XFS_SB_UNIT | XFS_SB_WIDTH | XFS_SB_UUID |
21004b166de0SDavid Chinner 			 XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2 |
21014b166de0SDavid Chinner 			 XFS_SB_VERSIONNUM));
21021da177e4SLinus Torvalds 
21031da177e4SLinus Torvalds 	tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
21045166ab06SJeff Liu 	error = xfs_trans_reserve(tp, 0, XFS_SB_LOG_RES(mp), 0, 0,
2105e5720eecSDavid Chinner 				  XFS_DEFAULT_LOG_COUNT);
2106e5720eecSDavid Chinner 	if (error) {
21071da177e4SLinus Torvalds 		xfs_trans_cancel(tp, 0);
2108e5720eecSDavid Chinner 		return error;
21091da177e4SLinus Torvalds 	}
21101da177e4SLinus Torvalds 	xfs_mod_sb(tp, fields);
2111e5720eecSDavid Chinner 	error = xfs_trans_commit(tp, 0);
2112e5720eecSDavid Chinner 	return error;
21131da177e4SLinus Torvalds }
21148d280b98SDavid Chinner 
2115dda35b8fSChristoph Hellwig /*
2116dda35b8fSChristoph Hellwig  * If the underlying (data/log/rt) device is readonly, there are some
2117dda35b8fSChristoph Hellwig  * operations that cannot proceed.
2118dda35b8fSChristoph Hellwig  */
2119dda35b8fSChristoph Hellwig int
2120dda35b8fSChristoph Hellwig xfs_dev_is_read_only(
2121dda35b8fSChristoph Hellwig 	struct xfs_mount	*mp,
2122dda35b8fSChristoph Hellwig 	char			*message)
2123dda35b8fSChristoph Hellwig {
2124dda35b8fSChristoph Hellwig 	if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
2125dda35b8fSChristoph Hellwig 	    xfs_readonly_buftarg(mp->m_logdev_targp) ||
2126dda35b8fSChristoph Hellwig 	    (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
21270b932cccSDave Chinner 		xfs_notice(mp, "%s required on read-only device.", message);
21280b932cccSDave Chinner 		xfs_notice(mp, "write access unavailable, cannot proceed.");
2129dda35b8fSChristoph Hellwig 		return EROFS;
2130dda35b8fSChristoph Hellwig 	}
2131dda35b8fSChristoph Hellwig 	return 0;
2132dda35b8fSChristoph Hellwig }
21338d280b98SDavid Chinner 
21348d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
21358d280b98SDavid Chinner /*
21368d280b98SDavid Chinner  * Per-cpu incore superblock counters
21378d280b98SDavid Chinner  *
21388d280b98SDavid Chinner  * Simple concept, difficult implementation
21398d280b98SDavid Chinner  *
21408d280b98SDavid Chinner  * Basically, replace the incore superblock counters with a distributed per cpu
21418d280b98SDavid Chinner  * counter for contended fields (e.g.  free block count).
21428d280b98SDavid Chinner  *
21438d280b98SDavid Chinner  * Difficulties arise in that the incore sb is used for ENOSPC checking, and
21448d280b98SDavid Chinner  * hence needs to be accurately read when we are running low on space. Hence
21458d280b98SDavid Chinner  * there is a method to enable and disable the per-cpu counters based on how
21468d280b98SDavid Chinner  * much "stuff" is available in them.
21478d280b98SDavid Chinner  *
21488d280b98SDavid Chinner  * Basically, a counter is enabled if there is enough free resource to justify
21498d280b98SDavid Chinner  * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
21508d280b98SDavid Chinner  * ENOSPC), then we disable the counters to synchronise all callers and
21518d280b98SDavid Chinner  * re-distribute the available resources.
21528d280b98SDavid Chinner  *
21538d280b98SDavid Chinner  * If, once we redistributed the available resources, we still get a failure,
21548d280b98SDavid Chinner  * we disable the per-cpu counter and go through the slow path.
21558d280b98SDavid Chinner  *
21568d280b98SDavid Chinner  * The slow path is the current xfs_mod_incore_sb() function.  This means that
21579da096fdSMalcolm Parsons  * when we disable a per-cpu counter, we need to drain its resources back to
21588d280b98SDavid Chinner  * the global superblock. We do this after disabling the counter to prevent
21598d280b98SDavid Chinner  * more threads from queueing up on the counter.
21608d280b98SDavid Chinner  *
21618d280b98SDavid Chinner  * Essentially, this means that we still need a lock in the fast path to enable
21628d280b98SDavid Chinner  * synchronisation between the global counters and the per-cpu counters. This
21638d280b98SDavid Chinner  * is not a problem because the lock will be local to a CPU almost all the time
21648d280b98SDavid Chinner  * and have little contention except when we get to ENOSPC conditions.
21658d280b98SDavid Chinner  *
21668d280b98SDavid Chinner  * Basically, this lock becomes a barrier that enables us to lock out the fast
21678d280b98SDavid Chinner  * path while we do things like enabling and disabling counters and
21688d280b98SDavid Chinner  * synchronising the counters.
21698d280b98SDavid Chinner  *
21708d280b98SDavid Chinner  * Locking rules:
21718d280b98SDavid Chinner  *
21723685c2a1SEric Sandeen  * 	1. m_sb_lock before picking up per-cpu locks
21738d280b98SDavid Chinner  * 	2. per-cpu locks always picked up via for_each_online_cpu() order
21743685c2a1SEric Sandeen  * 	3. accurate counter sync requires m_sb_lock + per cpu locks
21758d280b98SDavid Chinner  * 	4. modifying per-cpu counters requires holding per-cpu lock
21763685c2a1SEric Sandeen  * 	5. modifying global counters requires holding m_sb_lock
21773685c2a1SEric Sandeen  *	6. enabling or disabling a counter requires holding the m_sb_lock
21788d280b98SDavid Chinner  *	   and _none_ of the per-cpu locks.
21798d280b98SDavid Chinner  *
21808d280b98SDavid Chinner  * Disabled counters are only ever re-enabled by a balance operation
21818d280b98SDavid Chinner  * that results in more free resources per CPU than a given threshold.
21828d280b98SDavid Chinner  * To ensure counters don't remain disabled, they are rebalanced when
21838d280b98SDavid Chinner  * the global resource goes above a higher threshold (i.e. some hysteresis
21848d280b98SDavid Chinner  * is present to prevent thrashing).
21858d280b98SDavid Chinner  */
2186e8234a68SDavid Chinner 
21875a67e4c5SChandra Seetharaman #ifdef CONFIG_HOTPLUG_CPU
2188e8234a68SDavid Chinner /*
2189e8234a68SDavid Chinner  * hot-plug CPU notifier support.
2190e8234a68SDavid Chinner  *
21915a67e4c5SChandra Seetharaman  * We need a notifier per filesystem as we need to be able to identify
21925a67e4c5SChandra Seetharaman  * the filesystem to balance the counters out. This is achieved by
21935a67e4c5SChandra Seetharaman  * having a notifier block embedded in the xfs_mount_t and doing pointer
21945a67e4c5SChandra Seetharaman  * magic to get the mount pointer from the notifier block address.
2195e8234a68SDavid Chinner  */
2196e8234a68SDavid Chinner STATIC int
2197e8234a68SDavid Chinner xfs_icsb_cpu_notify(
2198e8234a68SDavid Chinner 	struct notifier_block *nfb,
2199e8234a68SDavid Chinner 	unsigned long action,
2200e8234a68SDavid Chinner 	void *hcpu)
2201e8234a68SDavid Chinner {
2202e8234a68SDavid Chinner 	xfs_icsb_cnts_t *cntp;
2203e8234a68SDavid Chinner 	xfs_mount_t	*mp;
2204e8234a68SDavid Chinner 
2205e8234a68SDavid Chinner 	mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
2206e8234a68SDavid Chinner 	cntp = (xfs_icsb_cnts_t *)
2207e8234a68SDavid Chinner 			per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
2208e8234a68SDavid Chinner 	switch (action) {
2209e8234a68SDavid Chinner 	case CPU_UP_PREPARE:
22108bb78442SRafael J. Wysocki 	case CPU_UP_PREPARE_FROZEN:
2211e8234a68SDavid Chinner 		/* Easy Case - initialize the area and locks, and
2212e8234a68SDavid Chinner 		 * then rebalance when online does everything else for us. */
221301e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
2214e8234a68SDavid Chinner 		break;
2215e8234a68SDavid Chinner 	case CPU_ONLINE:
22168bb78442SRafael J. Wysocki 	case CPU_ONLINE_FROZEN:
221703135cf7SDavid Chinner 		xfs_icsb_lock(mp);
221845af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
221945af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
222045af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
222103135cf7SDavid Chinner 		xfs_icsb_unlock(mp);
2222e8234a68SDavid Chinner 		break;
2223e8234a68SDavid Chinner 	case CPU_DEAD:
22248bb78442SRafael J. Wysocki 	case CPU_DEAD_FROZEN:
2225e8234a68SDavid Chinner 		/* Disable all the counters, then fold the dead cpu's
2226e8234a68SDavid Chinner 		 * count into the total on the global superblock and
2227e8234a68SDavid Chinner 		 * re-enable the counters. */
222803135cf7SDavid Chinner 		xfs_icsb_lock(mp);
22293685c2a1SEric Sandeen 		spin_lock(&mp->m_sb_lock);
2230e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
2231e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
2232e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
2233e8234a68SDavid Chinner 
2234e8234a68SDavid Chinner 		mp->m_sb.sb_icount += cntp->icsb_icount;
2235e8234a68SDavid Chinner 		mp->m_sb.sb_ifree += cntp->icsb_ifree;
2236e8234a68SDavid Chinner 		mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
2237e8234a68SDavid Chinner 
223801e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
2239e8234a68SDavid Chinner 
224045af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter_locked(mp, XFS_SBS_ICOUNT, 0);
224145af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter_locked(mp, XFS_SBS_IFREE, 0);
224245af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter_locked(mp, XFS_SBS_FDBLOCKS, 0);
22433685c2a1SEric Sandeen 		spin_unlock(&mp->m_sb_lock);
224403135cf7SDavid Chinner 		xfs_icsb_unlock(mp);
2245e8234a68SDavid Chinner 		break;
2246e8234a68SDavid Chinner 	}
2247e8234a68SDavid Chinner 
2248e8234a68SDavid Chinner 	return NOTIFY_OK;
2249e8234a68SDavid Chinner }
22505a67e4c5SChandra Seetharaman #endif /* CONFIG_HOTPLUG_CPU */
2251e8234a68SDavid Chinner 
22528d280b98SDavid Chinner int
22538d280b98SDavid Chinner xfs_icsb_init_counters(
22548d280b98SDavid Chinner 	xfs_mount_t	*mp)
22558d280b98SDavid Chinner {
22568d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
22578d280b98SDavid Chinner 	int		i;
22588d280b98SDavid Chinner 
22598d280b98SDavid Chinner 	mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
22608d280b98SDavid Chinner 	if (mp->m_sb_cnts == NULL)
22618d280b98SDavid Chinner 		return -ENOMEM;
22628d280b98SDavid Chinner 
22635a67e4c5SChandra Seetharaman #ifdef CONFIG_HOTPLUG_CPU
2264e8234a68SDavid Chinner 	mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
2265e8234a68SDavid Chinner 	mp->m_icsb_notifier.priority = 0;
22665a67e4c5SChandra Seetharaman 	register_hotcpu_notifier(&mp->m_icsb_notifier);
22675a67e4c5SChandra Seetharaman #endif /* CONFIG_HOTPLUG_CPU */
2268e8234a68SDavid Chinner 
22698d280b98SDavid Chinner 	for_each_online_cpu(i) {
22708d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
227101e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
22728d280b98SDavid Chinner 	}
227320b64285SDavid Chinner 
227420b64285SDavid Chinner 	mutex_init(&mp->m_icsb_mutex);
227520b64285SDavid Chinner 
22768d280b98SDavid Chinner 	/*
22778d280b98SDavid Chinner 	 * start with all counters disabled so that the
22788d280b98SDavid Chinner 	 * initial balance kicks us off correctly
22798d280b98SDavid Chinner 	 */
22808d280b98SDavid Chinner 	mp->m_icsb_counters = -1;
22818d280b98SDavid Chinner 	return 0;
22828d280b98SDavid Chinner }
22838d280b98SDavid Chinner 
22845478eeadSLachlan McIlroy void
22855478eeadSLachlan McIlroy xfs_icsb_reinit_counters(
22865478eeadSLachlan McIlroy 	xfs_mount_t	*mp)
22875478eeadSLachlan McIlroy {
22885478eeadSLachlan McIlroy 	xfs_icsb_lock(mp);
22895478eeadSLachlan McIlroy 	/*
22905478eeadSLachlan McIlroy 	 * start with all counters disabled so that the
22915478eeadSLachlan McIlroy 	 * initial balance kicks us off correctly
22925478eeadSLachlan McIlroy 	 */
22935478eeadSLachlan McIlroy 	mp->m_icsb_counters = -1;
229445af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
229545af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
229645af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
22975478eeadSLachlan McIlroy 	xfs_icsb_unlock(mp);
22985478eeadSLachlan McIlroy }
22995478eeadSLachlan McIlroy 
2300c962fb79SChristoph Hellwig void
23018d280b98SDavid Chinner xfs_icsb_destroy_counters(
23028d280b98SDavid Chinner 	xfs_mount_t	*mp)
23038d280b98SDavid Chinner {
2304e8234a68SDavid Chinner 	if (mp->m_sb_cnts) {
23055a67e4c5SChandra Seetharaman 		unregister_hotcpu_notifier(&mp->m_icsb_notifier);
23068d280b98SDavid Chinner 		free_percpu(mp->m_sb_cnts);
23078d280b98SDavid Chinner 	}
230803135cf7SDavid Chinner 	mutex_destroy(&mp->m_icsb_mutex);
2309e8234a68SDavid Chinner }
23108d280b98SDavid Chinner 
2311b8f82a4aSChristoph Hellwig STATIC void
231201e1b69cSDavid Chinner xfs_icsb_lock_cntr(
231301e1b69cSDavid Chinner 	xfs_icsb_cnts_t	*icsbp)
231401e1b69cSDavid Chinner {
231501e1b69cSDavid Chinner 	while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
231601e1b69cSDavid Chinner 		ndelay(1000);
231701e1b69cSDavid Chinner 	}
231801e1b69cSDavid Chinner }
231901e1b69cSDavid Chinner 
2320b8f82a4aSChristoph Hellwig STATIC void
232101e1b69cSDavid Chinner xfs_icsb_unlock_cntr(
232201e1b69cSDavid Chinner 	xfs_icsb_cnts_t	*icsbp)
232301e1b69cSDavid Chinner {
232401e1b69cSDavid Chinner 	clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
232501e1b69cSDavid Chinner }
232601e1b69cSDavid Chinner 
23278d280b98SDavid Chinner 
2328b8f82a4aSChristoph Hellwig STATIC void
23298d280b98SDavid Chinner xfs_icsb_lock_all_counters(
23308d280b98SDavid Chinner 	xfs_mount_t	*mp)
23318d280b98SDavid Chinner {
23328d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
23338d280b98SDavid Chinner 	int		i;
23348d280b98SDavid Chinner 
23358d280b98SDavid Chinner 	for_each_online_cpu(i) {
23368d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
233701e1b69cSDavid Chinner 		xfs_icsb_lock_cntr(cntp);
23388d280b98SDavid Chinner 	}
23398d280b98SDavid Chinner }
23408d280b98SDavid Chinner 
2341b8f82a4aSChristoph Hellwig STATIC void
23428d280b98SDavid Chinner xfs_icsb_unlock_all_counters(
23438d280b98SDavid Chinner 	xfs_mount_t	*mp)
23448d280b98SDavid Chinner {
23458d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
23468d280b98SDavid Chinner 	int		i;
23478d280b98SDavid Chinner 
23488d280b98SDavid Chinner 	for_each_online_cpu(i) {
23498d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
235001e1b69cSDavid Chinner 		xfs_icsb_unlock_cntr(cntp);
23518d280b98SDavid Chinner 	}
23528d280b98SDavid Chinner }
23538d280b98SDavid Chinner 
23548d280b98SDavid Chinner STATIC void
23558d280b98SDavid Chinner xfs_icsb_count(
23568d280b98SDavid Chinner 	xfs_mount_t	*mp,
23578d280b98SDavid Chinner 	xfs_icsb_cnts_t	*cnt,
23588d280b98SDavid Chinner 	int		flags)
23598d280b98SDavid Chinner {
23608d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
23618d280b98SDavid Chinner 	int		i;
23628d280b98SDavid Chinner 
23638d280b98SDavid Chinner 	memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
23648d280b98SDavid Chinner 
23658d280b98SDavid Chinner 	if (!(flags & XFS_ICSB_LAZY_COUNT))
23668d280b98SDavid Chinner 		xfs_icsb_lock_all_counters(mp);
23678d280b98SDavid Chinner 
23688d280b98SDavid Chinner 	for_each_online_cpu(i) {
23698d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
23708d280b98SDavid Chinner 		cnt->icsb_icount += cntp->icsb_icount;
23718d280b98SDavid Chinner 		cnt->icsb_ifree += cntp->icsb_ifree;
23728d280b98SDavid Chinner 		cnt->icsb_fdblocks += cntp->icsb_fdblocks;
23738d280b98SDavid Chinner 	}
23748d280b98SDavid Chinner 
23758d280b98SDavid Chinner 	if (!(flags & XFS_ICSB_LAZY_COUNT))
23768d280b98SDavid Chinner 		xfs_icsb_unlock_all_counters(mp);
23778d280b98SDavid Chinner }
23788d280b98SDavid Chinner 
23798d280b98SDavid Chinner STATIC int
23808d280b98SDavid Chinner xfs_icsb_counter_disabled(
23818d280b98SDavid Chinner 	xfs_mount_t	*mp,
23828d280b98SDavid Chinner 	xfs_sb_field_t	field)
23838d280b98SDavid Chinner {
23848d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
23858d280b98SDavid Chinner 	return test_bit(field, &mp->m_icsb_counters);
23868d280b98SDavid Chinner }
23878d280b98SDavid Chinner 
238836fbe6e6SDavid Chinner STATIC void
23898d280b98SDavid Chinner xfs_icsb_disable_counter(
23908d280b98SDavid Chinner 	xfs_mount_t	*mp,
23918d280b98SDavid Chinner 	xfs_sb_field_t	field)
23928d280b98SDavid Chinner {
23938d280b98SDavid Chinner 	xfs_icsb_cnts_t	cnt;
23948d280b98SDavid Chinner 
23958d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
23968d280b98SDavid Chinner 
239720b64285SDavid Chinner 	/*
239820b64285SDavid Chinner 	 * If we are already disabled, then there is nothing to do
239920b64285SDavid Chinner 	 * here. We check before locking all the counters to avoid
240020b64285SDavid Chinner 	 * the expensive lock operation when being called in the
240120b64285SDavid Chinner 	 * slow path and the counter is already disabled. This is
240220b64285SDavid Chinner 	 * safe because the only time we set or clear this state is under
240320b64285SDavid Chinner 	 * the m_icsb_mutex.
240420b64285SDavid Chinner 	 */
240520b64285SDavid Chinner 	if (xfs_icsb_counter_disabled(mp, field))
240636fbe6e6SDavid Chinner 		return;
240720b64285SDavid Chinner 
24088d280b98SDavid Chinner 	xfs_icsb_lock_all_counters(mp);
24098d280b98SDavid Chinner 	if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
24108d280b98SDavid Chinner 		/* drain back to superblock */
24118d280b98SDavid Chinner 
2412ce46193bSChristoph Hellwig 		xfs_icsb_count(mp, &cnt, XFS_ICSB_LAZY_COUNT);
24138d280b98SDavid Chinner 		switch(field) {
24148d280b98SDavid Chinner 		case XFS_SBS_ICOUNT:
24158d280b98SDavid Chinner 			mp->m_sb.sb_icount = cnt.icsb_icount;
24168d280b98SDavid Chinner 			break;
24178d280b98SDavid Chinner 		case XFS_SBS_IFREE:
24188d280b98SDavid Chinner 			mp->m_sb.sb_ifree = cnt.icsb_ifree;
24198d280b98SDavid Chinner 			break;
24208d280b98SDavid Chinner 		case XFS_SBS_FDBLOCKS:
24218d280b98SDavid Chinner 			mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
24228d280b98SDavid Chinner 			break;
24238d280b98SDavid Chinner 		default:
24248d280b98SDavid Chinner 			BUG();
24258d280b98SDavid Chinner 		}
24268d280b98SDavid Chinner 	}
24278d280b98SDavid Chinner 
24288d280b98SDavid Chinner 	xfs_icsb_unlock_all_counters(mp);
24298d280b98SDavid Chinner }
24308d280b98SDavid Chinner 
24318d280b98SDavid Chinner STATIC void
24328d280b98SDavid Chinner xfs_icsb_enable_counter(
24338d280b98SDavid Chinner 	xfs_mount_t	*mp,
24348d280b98SDavid Chinner 	xfs_sb_field_t	field,
24358d280b98SDavid Chinner 	uint64_t	count,
24368d280b98SDavid Chinner 	uint64_t	resid)
24378d280b98SDavid Chinner {
24388d280b98SDavid Chinner 	xfs_icsb_cnts_t	*cntp;
24398d280b98SDavid Chinner 	int		i;
24408d280b98SDavid Chinner 
24418d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
24428d280b98SDavid Chinner 
24438d280b98SDavid Chinner 	xfs_icsb_lock_all_counters(mp);
24448d280b98SDavid Chinner 	for_each_online_cpu(i) {
24458d280b98SDavid Chinner 		cntp = per_cpu_ptr(mp->m_sb_cnts, i);
24468d280b98SDavid Chinner 		switch (field) {
24478d280b98SDavid Chinner 		case XFS_SBS_ICOUNT:
24488d280b98SDavid Chinner 			cntp->icsb_icount = count + resid;
24498d280b98SDavid Chinner 			break;
24508d280b98SDavid Chinner 		case XFS_SBS_IFREE:
24518d280b98SDavid Chinner 			cntp->icsb_ifree = count + resid;
24528d280b98SDavid Chinner 			break;
24538d280b98SDavid Chinner 		case XFS_SBS_FDBLOCKS:
24548d280b98SDavid Chinner 			cntp->icsb_fdblocks = count + resid;
24558d280b98SDavid Chinner 			break;
24568d280b98SDavid Chinner 		default:
24578d280b98SDavid Chinner 			BUG();
24588d280b98SDavid Chinner 			break;
24598d280b98SDavid Chinner 		}
24608d280b98SDavid Chinner 		resid = 0;
24618d280b98SDavid Chinner 	}
24628d280b98SDavid Chinner 	clear_bit(field, &mp->m_icsb_counters);
24638d280b98SDavid Chinner 	xfs_icsb_unlock_all_counters(mp);
24648d280b98SDavid Chinner }
24658d280b98SDavid Chinner 
2466dbcabad1SDavid Chinner void
2467d4d90b57SChristoph Hellwig xfs_icsb_sync_counters_locked(
24688d280b98SDavid Chinner 	xfs_mount_t	*mp,
24698d280b98SDavid Chinner 	int		flags)
24708d280b98SDavid Chinner {
24718d280b98SDavid Chinner 	xfs_icsb_cnts_t	cnt;
24728d280b98SDavid Chinner 
24738d280b98SDavid Chinner 	xfs_icsb_count(mp, &cnt, flags);
24748d280b98SDavid Chinner 
24758d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
24768d280b98SDavid Chinner 		mp->m_sb.sb_icount = cnt.icsb_icount;
24778d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
24788d280b98SDavid Chinner 		mp->m_sb.sb_ifree = cnt.icsb_ifree;
24798d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
24808d280b98SDavid Chinner 		mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
24818d280b98SDavid Chinner }
24828d280b98SDavid Chinner 
24838d280b98SDavid Chinner /*
24848d280b98SDavid Chinner  * Accurate update of per-cpu counters to incore superblock
24858d280b98SDavid Chinner  */
2486d4d90b57SChristoph Hellwig void
24878d280b98SDavid Chinner xfs_icsb_sync_counters(
2488d4d90b57SChristoph Hellwig 	xfs_mount_t	*mp,
2489d4d90b57SChristoph Hellwig 	int		flags)
24908d280b98SDavid Chinner {
2491d4d90b57SChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
2492d4d90b57SChristoph Hellwig 	xfs_icsb_sync_counters_locked(mp, flags);
2493d4d90b57SChristoph Hellwig 	spin_unlock(&mp->m_sb_lock);
24948d280b98SDavid Chinner }
24958d280b98SDavid Chinner 
24968d280b98SDavid Chinner /*
24978d280b98SDavid Chinner  * Balance and enable/disable counters as necessary.
24988d280b98SDavid Chinner  *
249920b64285SDavid Chinner  * Thresholds for re-enabling counters are somewhat magic.  inode counts are
250020b64285SDavid Chinner  * chosen to be the same number as single on disk allocation chunk per CPU, and
250120b64285SDavid Chinner  * free blocks is something far enough zero that we aren't going thrash when we
250220b64285SDavid Chinner  * get near ENOSPC. We also need to supply a minimum we require per cpu to
250320b64285SDavid Chinner  * prevent looping endlessly when xfs_alloc_space asks for more than will
250420b64285SDavid Chinner  * be distributed to a single CPU but each CPU has enough blocks to be
250520b64285SDavid Chinner  * reenabled.
250620b64285SDavid Chinner  *
250720b64285SDavid Chinner  * Note that we can be called when counters are already disabled.
250820b64285SDavid Chinner  * xfs_icsb_disable_counter() optimises the counter locking in this case to
250920b64285SDavid Chinner  * prevent locking every per-cpu counter needlessly.
25108d280b98SDavid Chinner  */
251120b64285SDavid Chinner 
251220b64285SDavid Chinner #define XFS_ICSB_INO_CNTR_REENABLE	(uint64_t)64
25134be536deSDavid Chinner #define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \
251420b64285SDavid Chinner 		(uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp))
25158d280b98SDavid Chinner STATIC void
251645af6c6dSChristoph Hellwig xfs_icsb_balance_counter_locked(
25178d280b98SDavid Chinner 	xfs_mount_t	*mp,
25188d280b98SDavid Chinner 	xfs_sb_field_t  field,
251920b64285SDavid Chinner 	int		min_per_cpu)
25208d280b98SDavid Chinner {
25216fdf8cccSNathan Scott 	uint64_t	count, resid;
25228d280b98SDavid Chinner 	int		weight = num_online_cpus();
252320b64285SDavid Chinner 	uint64_t	min = (uint64_t)min_per_cpu;
25248d280b98SDavid Chinner 
25258d280b98SDavid Chinner 	/* disable counter and sync counter */
25268d280b98SDavid Chinner 	xfs_icsb_disable_counter(mp, field);
25278d280b98SDavid Chinner 
25288d280b98SDavid Chinner 	/* update counters  - first CPU gets residual*/
25298d280b98SDavid Chinner 	switch (field) {
25308d280b98SDavid Chinner 	case XFS_SBS_ICOUNT:
25318d280b98SDavid Chinner 		count = mp->m_sb.sb_icount;
25328d280b98SDavid Chinner 		resid = do_div(count, weight);
253320b64285SDavid Chinner 		if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
253445af6c6dSChristoph Hellwig 			return;
25358d280b98SDavid Chinner 		break;
25368d280b98SDavid Chinner 	case XFS_SBS_IFREE:
25378d280b98SDavid Chinner 		count = mp->m_sb.sb_ifree;
25388d280b98SDavid Chinner 		resid = do_div(count, weight);
253920b64285SDavid Chinner 		if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
254045af6c6dSChristoph Hellwig 			return;
25418d280b98SDavid Chinner 		break;
25428d280b98SDavid Chinner 	case XFS_SBS_FDBLOCKS:
25438d280b98SDavid Chinner 		count = mp->m_sb.sb_fdblocks;
25448d280b98SDavid Chinner 		resid = do_div(count, weight);
254520b64285SDavid Chinner 		if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp)))
254645af6c6dSChristoph Hellwig 			return;
25478d280b98SDavid Chinner 		break;
25488d280b98SDavid Chinner 	default:
25498d280b98SDavid Chinner 		BUG();
25506fdf8cccSNathan Scott 		count = resid = 0;	/* quiet, gcc */
25518d280b98SDavid Chinner 		break;
25528d280b98SDavid Chinner 	}
25538d280b98SDavid Chinner 
25548d280b98SDavid Chinner 	xfs_icsb_enable_counter(mp, field, count, resid);
255545af6c6dSChristoph Hellwig }
255645af6c6dSChristoph Hellwig 
255745af6c6dSChristoph Hellwig STATIC void
255845af6c6dSChristoph Hellwig xfs_icsb_balance_counter(
255945af6c6dSChristoph Hellwig 	xfs_mount_t	*mp,
256045af6c6dSChristoph Hellwig 	xfs_sb_field_t  fields,
256145af6c6dSChristoph Hellwig 	int		min_per_cpu)
256245af6c6dSChristoph Hellwig {
256345af6c6dSChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
256445af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter_locked(mp, fields, min_per_cpu);
25653685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
25668d280b98SDavid Chinner }
25678d280b98SDavid Chinner 
25681b040712SChristoph Hellwig int
256920b64285SDavid Chinner xfs_icsb_modify_counters(
25708d280b98SDavid Chinner 	xfs_mount_t	*mp,
25718d280b98SDavid Chinner 	xfs_sb_field_t	field,
257220f4ebf2SDavid Chinner 	int64_t		delta,
257320b64285SDavid Chinner 	int		rsvd)
25748d280b98SDavid Chinner {
25758d280b98SDavid Chinner 	xfs_icsb_cnts_t	*icsbp;
25768d280b98SDavid Chinner 	long long	lcounter;	/* long counter for 64 bit fields */
25777a9e02d6SChristoph Lameter 	int		ret = 0;
25788d280b98SDavid Chinner 
257920b64285SDavid Chinner 	might_sleep();
25808d280b98SDavid Chinner again:
25817a9e02d6SChristoph Lameter 	preempt_disable();
25827a9e02d6SChristoph Lameter 	icsbp = this_cpu_ptr(mp->m_sb_cnts);
258320b64285SDavid Chinner 
258420b64285SDavid Chinner 	/*
258520b64285SDavid Chinner 	 * if the counter is disabled, go to slow path
258620b64285SDavid Chinner 	 */
25878d280b98SDavid Chinner 	if (unlikely(xfs_icsb_counter_disabled(mp, field)))
25888d280b98SDavid Chinner 		goto slow_path;
258920b64285SDavid Chinner 	xfs_icsb_lock_cntr(icsbp);
259020b64285SDavid Chinner 	if (unlikely(xfs_icsb_counter_disabled(mp, field))) {
259120b64285SDavid Chinner 		xfs_icsb_unlock_cntr(icsbp);
259220b64285SDavid Chinner 		goto slow_path;
259320b64285SDavid Chinner 	}
25948d280b98SDavid Chinner 
25958d280b98SDavid Chinner 	switch (field) {
25968d280b98SDavid Chinner 	case XFS_SBS_ICOUNT:
25978d280b98SDavid Chinner 		lcounter = icsbp->icsb_icount;
25988d280b98SDavid Chinner 		lcounter += delta;
25998d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
260020b64285SDavid Chinner 			goto balance_counter;
26018d280b98SDavid Chinner 		icsbp->icsb_icount = lcounter;
26028d280b98SDavid Chinner 		break;
26038d280b98SDavid Chinner 
26048d280b98SDavid Chinner 	case XFS_SBS_IFREE:
26058d280b98SDavid Chinner 		lcounter = icsbp->icsb_ifree;
26068d280b98SDavid Chinner 		lcounter += delta;
26078d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
260820b64285SDavid Chinner 			goto balance_counter;
26098d280b98SDavid Chinner 		icsbp->icsb_ifree = lcounter;
26108d280b98SDavid Chinner 		break;
26118d280b98SDavid Chinner 
26128d280b98SDavid Chinner 	case XFS_SBS_FDBLOCKS:
26138d280b98SDavid Chinner 		BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
26148d280b98SDavid Chinner 
26154be536deSDavid Chinner 		lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
26168d280b98SDavid Chinner 		lcounter += delta;
26178d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
261820b64285SDavid Chinner 			goto balance_counter;
26194be536deSDavid Chinner 		icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
26208d280b98SDavid Chinner 		break;
26218d280b98SDavid Chinner 	default:
26228d280b98SDavid Chinner 		BUG();
26238d280b98SDavid Chinner 		break;
26248d280b98SDavid Chinner 	}
262501e1b69cSDavid Chinner 	xfs_icsb_unlock_cntr(icsbp);
26267a9e02d6SChristoph Lameter 	preempt_enable();
26278d280b98SDavid Chinner 	return 0;
26288d280b98SDavid Chinner 
26298d280b98SDavid Chinner slow_path:
26307a9e02d6SChristoph Lameter 	preempt_enable();
263120b64285SDavid Chinner 
263220b64285SDavid Chinner 	/*
263320b64285SDavid Chinner 	 * serialise with a mutex so we don't burn lots of cpu on
263420b64285SDavid Chinner 	 * the superblock lock. We still need to hold the superblock
263520b64285SDavid Chinner 	 * lock, however, when we modify the global structures.
263620b64285SDavid Chinner 	 */
263703135cf7SDavid Chinner 	xfs_icsb_lock(mp);
263820b64285SDavid Chinner 
263920b64285SDavid Chinner 	/*
264020b64285SDavid Chinner 	 * Now running atomically.
264120b64285SDavid Chinner 	 *
264220b64285SDavid Chinner 	 * If the counter is enabled, someone has beaten us to rebalancing.
264320b64285SDavid Chinner 	 * Drop the lock and try again in the fast path....
264420b64285SDavid Chinner 	 */
264520b64285SDavid Chinner 	if (!(xfs_icsb_counter_disabled(mp, field))) {
264603135cf7SDavid Chinner 		xfs_icsb_unlock(mp);
264720b64285SDavid Chinner 		goto again;
264820b64285SDavid Chinner 	}
264920b64285SDavid Chinner 
265020b64285SDavid Chinner 	/*
265120b64285SDavid Chinner 	 * The counter is currently disabled. Because we are
265220b64285SDavid Chinner 	 * running atomically here, we know a rebalance cannot
265320b64285SDavid Chinner 	 * be in progress. Hence we can go straight to operating
265420b64285SDavid Chinner 	 * on the global superblock. We do not call xfs_mod_incore_sb()
26553685c2a1SEric Sandeen 	 * here even though we need to get the m_sb_lock. Doing so
265620b64285SDavid Chinner 	 * will cause us to re-enter this function and deadlock.
26573685c2a1SEric Sandeen 	 * Hence we get the m_sb_lock ourselves and then call
265820b64285SDavid Chinner 	 * xfs_mod_incore_sb_unlocked() as the unlocked path operates
265920b64285SDavid Chinner 	 * directly on the global counters.
266020b64285SDavid Chinner 	 */
26613685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
266220b64285SDavid Chinner 	ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
26633685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
266420b64285SDavid Chinner 
266520b64285SDavid Chinner 	/*
266620b64285SDavid Chinner 	 * Now that we've modified the global superblock, we
266720b64285SDavid Chinner 	 * may be able to re-enable the distributed counters
266820b64285SDavid Chinner 	 * (e.g. lots of space just got freed). After that
266920b64285SDavid Chinner 	 * we are done.
267020b64285SDavid Chinner 	 */
267120b64285SDavid Chinner 	if (ret != ENOSPC)
267245af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, field, 0);
267303135cf7SDavid Chinner 	xfs_icsb_unlock(mp);
267420b64285SDavid Chinner 	return ret;
267520b64285SDavid Chinner 
267620b64285SDavid Chinner balance_counter:
267701e1b69cSDavid Chinner 	xfs_icsb_unlock_cntr(icsbp);
26787a9e02d6SChristoph Lameter 	preempt_enable();
26798d280b98SDavid Chinner 
268020b64285SDavid Chinner 	/*
268120b64285SDavid Chinner 	 * We may have multiple threads here if multiple per-cpu
268220b64285SDavid Chinner 	 * counters run dry at the same time. This will mean we can
268320b64285SDavid Chinner 	 * do more balances than strictly necessary but it is not
268420b64285SDavid Chinner 	 * the common slowpath case.
268520b64285SDavid Chinner 	 */
268603135cf7SDavid Chinner 	xfs_icsb_lock(mp);
268720b64285SDavid Chinner 
268820b64285SDavid Chinner 	/*
268920b64285SDavid Chinner 	 * running atomically.
269020b64285SDavid Chinner 	 *
269120b64285SDavid Chinner 	 * This will leave the counter in the correct state for future
269220b64285SDavid Chinner 	 * accesses. After the rebalance, we simply try again and our retry
269320b64285SDavid Chinner 	 * will either succeed through the fast path or slow path without
269420b64285SDavid Chinner 	 * another balance operation being required.
269520b64285SDavid Chinner 	 */
269645af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, field, delta);
269703135cf7SDavid Chinner 	xfs_icsb_unlock(mp);
26988d280b98SDavid Chinner 	goto again;
26998d280b98SDavid Chinner }
27008d280b98SDavid Chinner 
27018d280b98SDavid Chinner #endif
2702