xref: /linux/fs/xfs/xfs_mount.c (revision 2d2194f61fddab3a9731b6e7a7ae3a4a19dd810c)
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"
450b1b213fSChristoph Hellwig 
461da177e4SLinus Torvalds 
478d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
4820f4ebf2SDavid Chinner STATIC void	xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t,
4945af6c6dSChristoph Hellwig 						int);
5045af6c6dSChristoph Hellwig STATIC void	xfs_icsb_balance_counter_locked(xfs_mount_t *, xfs_sb_field_t,
5145af6c6dSChristoph Hellwig 						int);
5236fbe6e6SDavid Chinner STATIC void	xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
538d280b98SDavid Chinner #else
548d280b98SDavid Chinner 
5545af6c6dSChristoph Hellwig #define xfs_icsb_balance_counter(mp, a, b)		do { } while (0)
5645af6c6dSChristoph Hellwig #define xfs_icsb_balance_counter_locked(mp, a, b)	do { } while (0)
578d280b98SDavid Chinner #endif
588d280b98SDavid Chinner 
591df84c93SChristoph Hellwig static const struct {
601da177e4SLinus Torvalds 	short offset;
611da177e4SLinus Torvalds 	short type;	/* 0 = integer
621da177e4SLinus Torvalds 			 * 1 = binary / string (no translation)
631da177e4SLinus Torvalds 			 */
641da177e4SLinus Torvalds } xfs_sb_info[] = {
651da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_magicnum),   0 },
661da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_blocksize),  0 },
671da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_dblocks),    0 },
681da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rblocks),    0 },
691da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rextents),   0 },
701da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_uuid),       1 },
711da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logstart),   0 },
721da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rootino),    0 },
731da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rbmino),     0 },
741da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rsumino),    0 },
751da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rextsize),   0 },
761da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_agblocks),   0 },
771da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_agcount),    0 },
781da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rbmblocks),  0 },
791da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logblocks),  0 },
801da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_versionnum), 0 },
811da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_sectsize),   0 },
821da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inodesize),  0 },
831da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inopblock),  0 },
841da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_fname[0]),   1 },
851da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_blocklog),   0 },
861da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_sectlog),    0 },
871da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inodelog),   0 },
881da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inopblog),   0 },
891da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_agblklog),   0 },
901da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rextslog),   0 },
911da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inprogress), 0 },
921da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_imax_pct),   0 },
931da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_icount),     0 },
941da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_ifree),      0 },
951da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_fdblocks),   0 },
961da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_frextents),  0 },
971da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_uquotino),   0 },
981da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_gquotino),   0 },
991da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_qflags),     0 },
1001da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_flags),      0 },
1011da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_shared_vn),  0 },
1021da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
1031da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_unit),	 0 },
1041da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_width),	 0 },
1051da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_dirblklog),	 0 },
1061da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logsectlog), 0 },
1071da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logsectsize),0 },
1081da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logsunit),	 0 },
1091da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_features2),	 0 },
110ee1c0908SDavid Chinner     { offsetof(xfs_sb_t, sb_bad_features2), 0 },
1111da177e4SLinus Torvalds     { sizeof(xfs_sb_t),			 0 }
1121da177e4SLinus Torvalds };
1131da177e4SLinus Torvalds 
11427174203SChristoph Hellwig static DEFINE_MUTEX(xfs_uuid_table_mutex);
11527174203SChristoph Hellwig static int xfs_uuid_table_size;
11627174203SChristoph Hellwig static uuid_t *xfs_uuid_table;
11727174203SChristoph Hellwig 
11827174203SChristoph Hellwig /*
11927174203SChristoph Hellwig  * See if the UUID is unique among mounted XFS filesystems.
12027174203SChristoph Hellwig  * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
12127174203SChristoph Hellwig  */
12227174203SChristoph Hellwig STATIC int
12327174203SChristoph Hellwig xfs_uuid_mount(
12427174203SChristoph Hellwig 	struct xfs_mount	*mp)
12527174203SChristoph Hellwig {
12627174203SChristoph Hellwig 	uuid_t			*uuid = &mp->m_sb.sb_uuid;
12727174203SChristoph Hellwig 	int			hole, i;
12827174203SChristoph Hellwig 
12927174203SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_NOUUID)
13027174203SChristoph Hellwig 		return 0;
13127174203SChristoph Hellwig 
13227174203SChristoph Hellwig 	if (uuid_is_nil(uuid)) {
1330b932cccSDave Chinner 		xfs_warn(mp, "Filesystem has nil UUID - can't mount");
13427174203SChristoph Hellwig 		return XFS_ERROR(EINVAL);
13527174203SChristoph Hellwig 	}
13627174203SChristoph Hellwig 
13727174203SChristoph Hellwig 	mutex_lock(&xfs_uuid_table_mutex);
13827174203SChristoph Hellwig 	for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) {
13927174203SChristoph Hellwig 		if (uuid_is_nil(&xfs_uuid_table[i])) {
14027174203SChristoph Hellwig 			hole = i;
14127174203SChristoph Hellwig 			continue;
14227174203SChristoph Hellwig 		}
14327174203SChristoph Hellwig 		if (uuid_equal(uuid, &xfs_uuid_table[i]))
14427174203SChristoph Hellwig 			goto out_duplicate;
14527174203SChristoph Hellwig 	}
14627174203SChristoph Hellwig 
14727174203SChristoph Hellwig 	if (hole < 0) {
14827174203SChristoph Hellwig 		xfs_uuid_table = kmem_realloc(xfs_uuid_table,
14927174203SChristoph Hellwig 			(xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table),
15027174203SChristoph Hellwig 			xfs_uuid_table_size  * sizeof(*xfs_uuid_table),
15127174203SChristoph Hellwig 			KM_SLEEP);
15227174203SChristoph Hellwig 		hole = xfs_uuid_table_size++;
15327174203SChristoph Hellwig 	}
15427174203SChristoph Hellwig 	xfs_uuid_table[hole] = *uuid;
15527174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
15627174203SChristoph Hellwig 
15727174203SChristoph Hellwig 	return 0;
15827174203SChristoph Hellwig 
15927174203SChristoph Hellwig  out_duplicate:
16027174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
161021000e5SMitsuo Hayasaka 	xfs_warn(mp, "Filesystem has duplicate UUID %pU - can't mount", uuid);
16227174203SChristoph Hellwig 	return XFS_ERROR(EINVAL);
16327174203SChristoph Hellwig }
16427174203SChristoph Hellwig 
16527174203SChristoph Hellwig STATIC void
16627174203SChristoph Hellwig xfs_uuid_unmount(
16727174203SChristoph Hellwig 	struct xfs_mount	*mp)
16827174203SChristoph Hellwig {
16927174203SChristoph Hellwig 	uuid_t			*uuid = &mp->m_sb.sb_uuid;
17027174203SChristoph Hellwig 	int			i;
17127174203SChristoph Hellwig 
17227174203SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_NOUUID)
17327174203SChristoph Hellwig 		return;
17427174203SChristoph Hellwig 
17527174203SChristoph Hellwig 	mutex_lock(&xfs_uuid_table_mutex);
17627174203SChristoph Hellwig 	for (i = 0; i < xfs_uuid_table_size; i++) {
17727174203SChristoph Hellwig 		if (uuid_is_nil(&xfs_uuid_table[i]))
17827174203SChristoph Hellwig 			continue;
17927174203SChristoph Hellwig 		if (!uuid_equal(uuid, &xfs_uuid_table[i]))
18027174203SChristoph Hellwig 			continue;
18127174203SChristoph Hellwig 		memset(&xfs_uuid_table[i], 0, sizeof(uuid_t));
18227174203SChristoph Hellwig 		break;
18327174203SChristoph Hellwig 	}
18427174203SChristoph Hellwig 	ASSERT(i < xfs_uuid_table_size);
18527174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
18627174203SChristoph Hellwig }
18727174203SChristoph Hellwig 
18827174203SChristoph Hellwig 
1891da177e4SLinus Torvalds /*
1900fa800fbSDave Chinner  * Reference counting access wrappers to the perag structures.
191e176579eSDave Chinner  * Because we never free per-ag structures, the only thing we
192e176579eSDave Chinner  * have to protect against changes is the tree structure itself.
1930fa800fbSDave Chinner  */
1940fa800fbSDave Chinner struct xfs_perag *
1950fa800fbSDave Chinner xfs_perag_get(struct xfs_mount *mp, xfs_agnumber_t agno)
1960fa800fbSDave Chinner {
1970fa800fbSDave Chinner 	struct xfs_perag	*pag;
1980fa800fbSDave Chinner 	int			ref = 0;
1990fa800fbSDave Chinner 
200e176579eSDave Chinner 	rcu_read_lock();
2010fa800fbSDave Chinner 	pag = radix_tree_lookup(&mp->m_perag_tree, agno);
2020fa800fbSDave Chinner 	if (pag) {
2030fa800fbSDave Chinner 		ASSERT(atomic_read(&pag->pag_ref) >= 0);
2040fa800fbSDave Chinner 		ref = atomic_inc_return(&pag->pag_ref);
2050fa800fbSDave Chinner 	}
206e176579eSDave Chinner 	rcu_read_unlock();
2070fa800fbSDave Chinner 	trace_xfs_perag_get(mp, agno, ref, _RET_IP_);
2080fa800fbSDave Chinner 	return pag;
2090fa800fbSDave Chinner }
2100fa800fbSDave Chinner 
21165d0f205SDave Chinner /*
21265d0f205SDave Chinner  * search from @first to find the next perag with the given tag set.
21365d0f205SDave Chinner  */
21465d0f205SDave Chinner struct xfs_perag *
21565d0f205SDave Chinner xfs_perag_get_tag(
21665d0f205SDave Chinner 	struct xfs_mount	*mp,
21765d0f205SDave Chinner 	xfs_agnumber_t		first,
21865d0f205SDave Chinner 	int			tag)
21965d0f205SDave Chinner {
22065d0f205SDave Chinner 	struct xfs_perag	*pag;
22165d0f205SDave Chinner 	int			found;
22265d0f205SDave Chinner 	int			ref;
22365d0f205SDave Chinner 
22465d0f205SDave Chinner 	rcu_read_lock();
22565d0f205SDave Chinner 	found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
22665d0f205SDave Chinner 					(void **)&pag, first, 1, tag);
22765d0f205SDave Chinner 	if (found <= 0) {
22865d0f205SDave Chinner 		rcu_read_unlock();
22965d0f205SDave Chinner 		return NULL;
23065d0f205SDave Chinner 	}
23165d0f205SDave Chinner 	ref = atomic_inc_return(&pag->pag_ref);
23265d0f205SDave Chinner 	rcu_read_unlock();
23365d0f205SDave Chinner 	trace_xfs_perag_get_tag(mp, pag->pag_agno, ref, _RET_IP_);
23465d0f205SDave Chinner 	return pag;
23565d0f205SDave Chinner }
23665d0f205SDave Chinner 
2370fa800fbSDave Chinner void
2380fa800fbSDave Chinner xfs_perag_put(struct xfs_perag *pag)
2390fa800fbSDave Chinner {
2400fa800fbSDave Chinner 	int	ref;
2410fa800fbSDave Chinner 
2420fa800fbSDave Chinner 	ASSERT(atomic_read(&pag->pag_ref) > 0);
2430fa800fbSDave Chinner 	ref = atomic_dec_return(&pag->pag_ref);
2440fa800fbSDave Chinner 	trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_);
2450fa800fbSDave Chinner }
2460fa800fbSDave Chinner 
247e176579eSDave Chinner STATIC void
248e176579eSDave Chinner __xfs_free_perag(
249e176579eSDave Chinner 	struct rcu_head	*head)
250e176579eSDave Chinner {
251e176579eSDave Chinner 	struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head);
252e176579eSDave Chinner 
253e176579eSDave Chinner 	ASSERT(atomic_read(&pag->pag_ref) == 0);
254e176579eSDave Chinner 	kmem_free(pag);
255e176579eSDave Chinner }
256e176579eSDave Chinner 
2570fa800fbSDave Chinner /*
258e176579eSDave Chinner  * Free up the per-ag resources associated with the mount structure.
2591da177e4SLinus Torvalds  */
260c962fb79SChristoph Hellwig STATIC void
261ff4f038cSChristoph Hellwig xfs_free_perag(
262745f6919SChristoph Hellwig 	xfs_mount_t	*mp)
2631da177e4SLinus Torvalds {
2641c1c6ebcSDave Chinner 	xfs_agnumber_t	agno;
2651c1c6ebcSDave Chinner 	struct xfs_perag *pag;
2661da177e4SLinus Torvalds 
2671c1c6ebcSDave Chinner 	for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
2681c1c6ebcSDave Chinner 		spin_lock(&mp->m_perag_lock);
2691c1c6ebcSDave Chinner 		pag = radix_tree_delete(&mp->m_perag_tree, agno);
2701c1c6ebcSDave Chinner 		spin_unlock(&mp->m_perag_lock);
271e176579eSDave Chinner 		ASSERT(pag);
272f83282a8SDave Chinner 		ASSERT(atomic_read(&pag->pag_ref) == 0);
273e176579eSDave Chinner 		call_rcu(&pag->rcu_head, __xfs_free_perag);
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds }
2761da177e4SLinus Torvalds 
2774cc929eeSNathan Scott /*
2784cc929eeSNathan Scott  * Check size of device based on the (data/realtime) block count.
2794cc929eeSNathan Scott  * Note: this check is used by the growfs code as well as mount.
2804cc929eeSNathan Scott  */
2814cc929eeSNathan Scott int
2824cc929eeSNathan Scott xfs_sb_validate_fsb_count(
2834cc929eeSNathan Scott 	xfs_sb_t	*sbp,
2844cc929eeSNathan Scott 	__uint64_t	nblocks)
2854cc929eeSNathan Scott {
2864cc929eeSNathan Scott 	ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
2874cc929eeSNathan Scott 	ASSERT(sbp->sb_blocklog >= BBSHIFT);
2884cc929eeSNathan Scott 
2894cc929eeSNathan Scott #if XFS_BIG_BLKNOS     /* Limited by ULONG_MAX of page cache index */
2904cc929eeSNathan Scott 	if (nblocks >> (PAGE_CACHE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
291657a4cffSEric Sandeen 		return EFBIG;
2924cc929eeSNathan Scott #else                  /* Limited by UINT_MAX of sectors */
2934cc929eeSNathan Scott 	if (nblocks << (sbp->sb_blocklog - BBSHIFT) > UINT_MAX)
294657a4cffSEric Sandeen 		return EFBIG;
2954cc929eeSNathan Scott #endif
2964cc929eeSNathan Scott 	return 0;
2974cc929eeSNathan Scott }
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds /*
3001da177e4SLinus Torvalds  * Check the validity of the SB found.
3011da177e4SLinus Torvalds  */
3021da177e4SLinus Torvalds STATIC int
3031da177e4SLinus Torvalds xfs_mount_validate_sb(
3041da177e4SLinus Torvalds 	xfs_mount_t	*mp,
305764d1f89SNathan Scott 	xfs_sb_t	*sbp,
306764d1f89SNathan Scott 	int		flags)
3071da177e4SLinus Torvalds {
308af34e09dSDave Chinner 	int		loud = !(flags & XFS_MFSI_QUIET);
309af34e09dSDave Chinner 
3101da177e4SLinus Torvalds 	/*
3111da177e4SLinus Torvalds 	 * If the log device and data device have the
3121da177e4SLinus Torvalds 	 * same device number, the log is internal.
3131da177e4SLinus Torvalds 	 * Consequently, the sb_logstart should be non-zero.  If
3141da177e4SLinus Torvalds 	 * we have a zero sb_logstart in this case, we may be trying to mount
3151da177e4SLinus Torvalds 	 * a volume filesystem in a non-volume manner.
3161da177e4SLinus Torvalds 	 */
3171da177e4SLinus Torvalds 	if (sbp->sb_magicnum != XFS_SB_MAGIC) {
318af34e09dSDave Chinner 		if (loud)
319af34e09dSDave Chinner 			xfs_warn(mp, "bad magic number");
3201da177e4SLinus Torvalds 		return XFS_ERROR(EWRONGFS);
3211da177e4SLinus Torvalds 	}
3221da177e4SLinus Torvalds 
32362118709SEric Sandeen 	if (!xfs_sb_good_version(sbp)) {
324af34e09dSDave Chinner 		if (loud)
325af34e09dSDave Chinner 			xfs_warn(mp, "bad version");
3261da177e4SLinus Torvalds 		return XFS_ERROR(EWRONGFS);
3271da177e4SLinus Torvalds 	}
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	if (unlikely(
3301da177e4SLinus Torvalds 	    sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
331af34e09dSDave Chinner 		if (loud)
332af34e09dSDave Chinner 			xfs_warn(mp,
333764d1f89SNathan Scott 		"filesystem is marked as having an external log; "
334af34e09dSDave Chinner 		"specify logdev on the mount command line.");
335764d1f89SNathan Scott 		return XFS_ERROR(EINVAL);
3361da177e4SLinus Torvalds 	}
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	if (unlikely(
3391da177e4SLinus Torvalds 	    sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
340af34e09dSDave Chinner 		if (loud)
341af34e09dSDave Chinner 			xfs_warn(mp,
342764d1f89SNathan Scott 		"filesystem is marked as having an internal log; "
343af34e09dSDave Chinner 		"do not specify logdev on the mount command line.");
344764d1f89SNathan Scott 		return XFS_ERROR(EINVAL);
3451da177e4SLinus Torvalds 	}
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	/*
348c0e090ceSEric Sandeen 	 * More sanity checking.  Most of these were stolen directly from
3491da177e4SLinus Torvalds 	 * xfs_repair.
3501da177e4SLinus Torvalds 	 */
3511da177e4SLinus Torvalds 	if (unlikely(
3521da177e4SLinus Torvalds 	    sbp->sb_agcount <= 0					||
3531da177e4SLinus Torvalds 	    sbp->sb_sectsize < XFS_MIN_SECTORSIZE			||
3541da177e4SLinus Torvalds 	    sbp->sb_sectsize > XFS_MAX_SECTORSIZE			||
3551da177e4SLinus Torvalds 	    sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG			||
3561da177e4SLinus Torvalds 	    sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG			||
3572ac00af7SOlaf Weber 	    sbp->sb_sectsize != (1 << sbp->sb_sectlog)			||
3581da177e4SLinus Torvalds 	    sbp->sb_blocksize < XFS_MIN_BLOCKSIZE			||
3591da177e4SLinus Torvalds 	    sbp->sb_blocksize > XFS_MAX_BLOCKSIZE			||
3601da177e4SLinus Torvalds 	    sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG			||
3611da177e4SLinus Torvalds 	    sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG			||
3622ac00af7SOlaf Weber 	    sbp->sb_blocksize != (1 << sbp->sb_blocklog)		||
3631da177e4SLinus Torvalds 	    sbp->sb_inodesize < XFS_DINODE_MIN_SIZE			||
3641da177e4SLinus Torvalds 	    sbp->sb_inodesize > XFS_DINODE_MAX_SIZE			||
3659f989c94SNathan Scott 	    sbp->sb_inodelog < XFS_DINODE_MIN_LOG			||
3669f989c94SNathan Scott 	    sbp->sb_inodelog > XFS_DINODE_MAX_LOG			||
3672ac00af7SOlaf Weber 	    sbp->sb_inodesize != (1 << sbp->sb_inodelog)		||
3689f989c94SNathan Scott 	    (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog)	||
3691da177e4SLinus Torvalds 	    (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE)	||
3701da177e4SLinus Torvalds 	    (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE)	||
371c0e090ceSEric Sandeen 	    (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */)	||
3721da177e4SLinus Torvalds 	    sbp->sb_dblocks == 0					||
373c0e090ceSEric Sandeen 	    sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp)			||
374c0e090ceSEric Sandeen 	    sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp))) {
375af34e09dSDave Chinner 		if (loud)
376c0e090ceSEric Sandeen 			XFS_CORRUPTION_ERROR("SB sanity check failed",
377c0e090ceSEric Sandeen 				XFS_ERRLEVEL_LOW, mp, sbp);
3781da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
3791da177e4SLinus Torvalds 	}
3801da177e4SLinus Torvalds 
3812edbddd5SLachlan McIlroy 	/*
3822edbddd5SLachlan McIlroy 	 * Until this is fixed only page-sized or smaller data blocks work.
3832edbddd5SLachlan McIlroy 	 */
3842edbddd5SLachlan McIlroy 	if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
385af34e09dSDave Chinner 		if (loud) {
386af34e09dSDave Chinner 			xfs_warn(mp,
387af34e09dSDave Chinner 		"File system with blocksize %d bytes. "
388af34e09dSDave Chinner 		"Only pagesize (%ld) or less will currently work.",
389af34e09dSDave Chinner 				sbp->sb_blocksize, PAGE_SIZE);
390af34e09dSDave Chinner 		}
3912edbddd5SLachlan McIlroy 		return XFS_ERROR(ENOSYS);
3922edbddd5SLachlan McIlroy 	}
3932edbddd5SLachlan McIlroy 
3941a5902c5SChristoph Hellwig 	/*
3951a5902c5SChristoph Hellwig 	 * Currently only very few inode sizes are supported.
3961a5902c5SChristoph Hellwig 	 */
3971a5902c5SChristoph Hellwig 	switch (sbp->sb_inodesize) {
3981a5902c5SChristoph Hellwig 	case 256:
3991a5902c5SChristoph Hellwig 	case 512:
4001a5902c5SChristoph Hellwig 	case 1024:
4011a5902c5SChristoph Hellwig 	case 2048:
4021a5902c5SChristoph Hellwig 		break;
4031a5902c5SChristoph Hellwig 	default:
404af34e09dSDave Chinner 		if (loud)
405af34e09dSDave Chinner 			xfs_warn(mp, "inode size of %d bytes not supported",
4061a5902c5SChristoph Hellwig 				sbp->sb_inodesize);
4071a5902c5SChristoph Hellwig 		return XFS_ERROR(ENOSYS);
4081a5902c5SChristoph Hellwig 	}
4091a5902c5SChristoph Hellwig 
4104cc929eeSNathan Scott 	if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
4114cc929eeSNathan Scott 	    xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
412af34e09dSDave Chinner 		if (loud)
413af34e09dSDave Chinner 			xfs_warn(mp,
414764d1f89SNathan Scott 		"file system too large to be mounted on this system.");
415657a4cffSEric Sandeen 		return XFS_ERROR(EFBIG);
4161da177e4SLinus Torvalds 	}
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	if (unlikely(sbp->sb_inprogress)) {
419af34e09dSDave Chinner 		if (loud)
420af34e09dSDave Chinner 			xfs_warn(mp, "file system busy");
4211da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
4221da177e4SLinus Torvalds 	}
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	/*
425de20614bSNathan Scott 	 * Version 1 directory format has never worked on Linux.
426de20614bSNathan Scott 	 */
42762118709SEric Sandeen 	if (unlikely(!xfs_sb_version_hasdirv2(sbp))) {
428af34e09dSDave Chinner 		if (loud)
429af34e09dSDave Chinner 			xfs_warn(mp,
430764d1f89SNathan Scott 				"file system using version 1 directory format");
431de20614bSNathan Scott 		return XFS_ERROR(ENOSYS);
432de20614bSNathan Scott 	}
433de20614bSNathan Scott 
4341da177e4SLinus Torvalds 	return 0;
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
4371c1c6ebcSDave Chinner int
438c11e2c36SNathan Scott xfs_initialize_perag(
439c11e2c36SNathan Scott 	xfs_mount_t	*mp,
4401c1c6ebcSDave Chinner 	xfs_agnumber_t	agcount,
4411c1c6ebcSDave Chinner 	xfs_agnumber_t	*maxagi)
4421da177e4SLinus Torvalds {
443*2d2194f6SCarlos Maiolino 	xfs_agnumber_t	index;
4448b26c582SDave Chinner 	xfs_agnumber_t	first_initialised = 0;
4451da177e4SLinus Torvalds 	xfs_perag_t	*pag;
4461da177e4SLinus Torvalds 	xfs_agino_t	agino;
4471da177e4SLinus Torvalds 	xfs_ino_t	ino;
4481da177e4SLinus Torvalds 	xfs_sb_t	*sbp = &mp->m_sb;
4498b26c582SDave Chinner 	int		error = -ENOMEM;
4501da177e4SLinus Torvalds 
4511c1c6ebcSDave Chinner 	/*
4521c1c6ebcSDave Chinner 	 * Walk the current per-ag tree so we don't try to initialise AGs
4531c1c6ebcSDave Chinner 	 * that already exist (growfs case). Allocate and insert all the
4541c1c6ebcSDave Chinner 	 * AGs we don't find ready for initialisation.
4551c1c6ebcSDave Chinner 	 */
4561c1c6ebcSDave Chinner 	for (index = 0; index < agcount; index++) {
4571c1c6ebcSDave Chinner 		pag = xfs_perag_get(mp, index);
4581c1c6ebcSDave Chinner 		if (pag) {
4591c1c6ebcSDave Chinner 			xfs_perag_put(pag);
4601c1c6ebcSDave Chinner 			continue;
4611c1c6ebcSDave Chinner 		}
4628b26c582SDave Chinner 		if (!first_initialised)
4638b26c582SDave Chinner 			first_initialised = index;
464fb3b504aSChristoph Hellwig 
4651c1c6ebcSDave Chinner 		pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
4661c1c6ebcSDave Chinner 		if (!pag)
4678b26c582SDave Chinner 			goto out_unwind;
468fb3b504aSChristoph Hellwig 		pag->pag_agno = index;
469fb3b504aSChristoph Hellwig 		pag->pag_mount = mp;
4701a427ab0SDave Chinner 		spin_lock_init(&pag->pag_ici_lock);
47169b491c2SDave Chinner 		mutex_init(&pag->pag_ici_reclaim_lock);
472fb3b504aSChristoph Hellwig 		INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
47374f75a0cSDave Chinner 		spin_lock_init(&pag->pag_buf_lock);
47474f75a0cSDave Chinner 		pag->pag_buf_tree = RB_ROOT;
475fb3b504aSChristoph Hellwig 
4761c1c6ebcSDave Chinner 		if (radix_tree_preload(GFP_NOFS))
4778b26c582SDave Chinner 			goto out_unwind;
478fb3b504aSChristoph Hellwig 
4791c1c6ebcSDave Chinner 		spin_lock(&mp->m_perag_lock);
4801c1c6ebcSDave Chinner 		if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
4811c1c6ebcSDave Chinner 			BUG();
4821c1c6ebcSDave Chinner 			spin_unlock(&mp->m_perag_lock);
4838b26c582SDave Chinner 			radix_tree_preload_end();
4848b26c582SDave Chinner 			error = -EEXIST;
4858b26c582SDave Chinner 			goto out_unwind;
4861c1c6ebcSDave Chinner 		}
4871c1c6ebcSDave Chinner 		spin_unlock(&mp->m_perag_lock);
4881c1c6ebcSDave Chinner 		radix_tree_preload_end();
4891c1c6ebcSDave Chinner 	}
4901c1c6ebcSDave Chinner 
491fb3b504aSChristoph Hellwig 	/*
492fb3b504aSChristoph Hellwig 	 * If we mount with the inode64 option, or no inode overflows
493fb3b504aSChristoph Hellwig 	 * the legacy 32-bit address space clear the inode32 option.
4941da177e4SLinus Torvalds 	 */
495fb3b504aSChristoph Hellwig 	agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
496fb3b504aSChristoph Hellwig 	ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
4971da177e4SLinus Torvalds 
498fb3b504aSChristoph Hellwig 	if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > XFS_MAXINUMBER_32)
499fb3b504aSChristoph Hellwig 		mp->m_flags |= XFS_MOUNT_32BITINODES;
500fb3b504aSChristoph Hellwig 	else
501fb3b504aSChristoph Hellwig 		mp->m_flags &= ~XFS_MOUNT_32BITINODES;
502fb3b504aSChristoph Hellwig 
503*2d2194f6SCarlos Maiolino 	if (mp->m_flags & XFS_MOUNT_32BITINODES)
504*2d2194f6SCarlos Maiolino 		index = xfs_set_inode32(mp);
505*2d2194f6SCarlos Maiolino 	else
506*2d2194f6SCarlos Maiolino 		index = xfs_set_inode64(mp);
507fb3b504aSChristoph Hellwig 
5081c1c6ebcSDave Chinner 	if (maxagi)
5091c1c6ebcSDave Chinner 		*maxagi = index;
5101c1c6ebcSDave Chinner 	return 0;
5118b26c582SDave Chinner 
5128b26c582SDave Chinner out_unwind:
5138b26c582SDave Chinner 	kmem_free(pag);
5148b26c582SDave Chinner 	for (; index > first_initialised; index--) {
5158b26c582SDave Chinner 		pag = radix_tree_delete(&mp->m_perag_tree, index);
5168b26c582SDave Chinner 		kmem_free(pag);
5178b26c582SDave Chinner 	}
5188b26c582SDave Chinner 	return error;
5191da177e4SLinus Torvalds }
5201da177e4SLinus Torvalds 
5212bdf7cd0SChristoph Hellwig void
5222bdf7cd0SChristoph Hellwig xfs_sb_from_disk(
5236bd92a23SChandra Seetharaman 	struct xfs_mount	*mp,
5242bdf7cd0SChristoph Hellwig 	xfs_dsb_t	*from)
5252bdf7cd0SChristoph Hellwig {
5266bd92a23SChandra Seetharaman 	struct xfs_sb *to = &mp->m_sb;
5276bd92a23SChandra Seetharaman 
5282bdf7cd0SChristoph Hellwig 	to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
5292bdf7cd0SChristoph Hellwig 	to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
5302bdf7cd0SChristoph Hellwig 	to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
5312bdf7cd0SChristoph Hellwig 	to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
5322bdf7cd0SChristoph Hellwig 	to->sb_rextents = be64_to_cpu(from->sb_rextents);
5332bdf7cd0SChristoph Hellwig 	memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
5342bdf7cd0SChristoph Hellwig 	to->sb_logstart = be64_to_cpu(from->sb_logstart);
5352bdf7cd0SChristoph Hellwig 	to->sb_rootino = be64_to_cpu(from->sb_rootino);
5362bdf7cd0SChristoph Hellwig 	to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
5372bdf7cd0SChristoph Hellwig 	to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
5382bdf7cd0SChristoph Hellwig 	to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
5392bdf7cd0SChristoph Hellwig 	to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
5402bdf7cd0SChristoph Hellwig 	to->sb_agcount = be32_to_cpu(from->sb_agcount);
5412bdf7cd0SChristoph Hellwig 	to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
5422bdf7cd0SChristoph Hellwig 	to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
5432bdf7cd0SChristoph Hellwig 	to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
5442bdf7cd0SChristoph Hellwig 	to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
5452bdf7cd0SChristoph Hellwig 	to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
5462bdf7cd0SChristoph Hellwig 	to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
5472bdf7cd0SChristoph Hellwig 	memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
5482bdf7cd0SChristoph Hellwig 	to->sb_blocklog = from->sb_blocklog;
5492bdf7cd0SChristoph Hellwig 	to->sb_sectlog = from->sb_sectlog;
5502bdf7cd0SChristoph Hellwig 	to->sb_inodelog = from->sb_inodelog;
5512bdf7cd0SChristoph Hellwig 	to->sb_inopblog = from->sb_inopblog;
5522bdf7cd0SChristoph Hellwig 	to->sb_agblklog = from->sb_agblklog;
5532bdf7cd0SChristoph Hellwig 	to->sb_rextslog = from->sb_rextslog;
5542bdf7cd0SChristoph Hellwig 	to->sb_inprogress = from->sb_inprogress;
5552bdf7cd0SChristoph Hellwig 	to->sb_imax_pct = from->sb_imax_pct;
5562bdf7cd0SChristoph Hellwig 	to->sb_icount = be64_to_cpu(from->sb_icount);
5572bdf7cd0SChristoph Hellwig 	to->sb_ifree = be64_to_cpu(from->sb_ifree);
5582bdf7cd0SChristoph Hellwig 	to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
5592bdf7cd0SChristoph Hellwig 	to->sb_frextents = be64_to_cpu(from->sb_frextents);
5602bdf7cd0SChristoph Hellwig 	to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
5612bdf7cd0SChristoph Hellwig 	to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
5622bdf7cd0SChristoph Hellwig 	to->sb_qflags = be16_to_cpu(from->sb_qflags);
5632bdf7cd0SChristoph Hellwig 	to->sb_flags = from->sb_flags;
5642bdf7cd0SChristoph Hellwig 	to->sb_shared_vn = from->sb_shared_vn;
5652bdf7cd0SChristoph Hellwig 	to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
5662bdf7cd0SChristoph Hellwig 	to->sb_unit = be32_to_cpu(from->sb_unit);
5672bdf7cd0SChristoph Hellwig 	to->sb_width = be32_to_cpu(from->sb_width);
5682bdf7cd0SChristoph Hellwig 	to->sb_dirblklog = from->sb_dirblklog;
5692bdf7cd0SChristoph Hellwig 	to->sb_logsectlog = from->sb_logsectlog;
5702bdf7cd0SChristoph Hellwig 	to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
5712bdf7cd0SChristoph Hellwig 	to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
5722bdf7cd0SChristoph Hellwig 	to->sb_features2 = be32_to_cpu(from->sb_features2);
573ee1c0908SDavid Chinner 	to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
5742bdf7cd0SChristoph Hellwig }
5752bdf7cd0SChristoph Hellwig 
5761da177e4SLinus Torvalds /*
5772bdf7cd0SChristoph Hellwig  * Copy in core superblock to ondisk one.
5781da177e4SLinus Torvalds  *
5792bdf7cd0SChristoph Hellwig  * The fields argument is mask of superblock fields to copy.
5801da177e4SLinus Torvalds  */
5811da177e4SLinus Torvalds void
5822bdf7cd0SChristoph Hellwig xfs_sb_to_disk(
5832bdf7cd0SChristoph Hellwig 	xfs_dsb_t	*to,
5842bdf7cd0SChristoph Hellwig 	xfs_sb_t	*from,
5851da177e4SLinus Torvalds 	__int64_t	fields)
5861da177e4SLinus Torvalds {
5872bdf7cd0SChristoph Hellwig 	xfs_caddr_t	to_ptr = (xfs_caddr_t)to;
5882bdf7cd0SChristoph Hellwig 	xfs_caddr_t	from_ptr = (xfs_caddr_t)from;
5891da177e4SLinus Torvalds 	xfs_sb_field_t	f;
5901da177e4SLinus Torvalds 	int		first;
5911da177e4SLinus Torvalds 	int		size;
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 	ASSERT(fields);
5941da177e4SLinus Torvalds 	if (!fields)
5951da177e4SLinus Torvalds 		return;
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	while (fields) {
5981da177e4SLinus Torvalds 		f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
5991da177e4SLinus Torvalds 		first = xfs_sb_info[f].offset;
6001da177e4SLinus Torvalds 		size = xfs_sb_info[f + 1].offset - first;
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 		ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 		if (size == 1 || xfs_sb_info[f].type == 1) {
6052bdf7cd0SChristoph Hellwig 			memcpy(to_ptr + first, from_ptr + first, size);
6061da177e4SLinus Torvalds 		} else {
6071da177e4SLinus Torvalds 			switch (size) {
6081da177e4SLinus Torvalds 			case 2:
6092bdf7cd0SChristoph Hellwig 				*(__be16 *)(to_ptr + first) =
6102bdf7cd0SChristoph Hellwig 					cpu_to_be16(*(__u16 *)(from_ptr + first));
6111da177e4SLinus Torvalds 				break;
6121da177e4SLinus Torvalds 			case 4:
6132bdf7cd0SChristoph Hellwig 				*(__be32 *)(to_ptr + first) =
6142bdf7cd0SChristoph Hellwig 					cpu_to_be32(*(__u32 *)(from_ptr + first));
6151da177e4SLinus Torvalds 				break;
6161da177e4SLinus Torvalds 			case 8:
6172bdf7cd0SChristoph Hellwig 				*(__be64 *)(to_ptr + first) =
6182bdf7cd0SChristoph Hellwig 					cpu_to_be64(*(__u64 *)(from_ptr + first));
6191da177e4SLinus Torvalds 				break;
6201da177e4SLinus Torvalds 			default:
6211da177e4SLinus Torvalds 				ASSERT(0);
6221da177e4SLinus Torvalds 			}
6231da177e4SLinus Torvalds 		}
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 		fields &= ~(1LL << f);
6261da177e4SLinus Torvalds 	}
6271da177e4SLinus Torvalds }
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds /*
6301da177e4SLinus Torvalds  * xfs_readsb
6311da177e4SLinus Torvalds  *
6321da177e4SLinus Torvalds  * Does the initial read of the superblock.
6331da177e4SLinus Torvalds  */
6341da177e4SLinus Torvalds int
635764d1f89SNathan Scott xfs_readsb(xfs_mount_t *mp, int flags)
6361da177e4SLinus Torvalds {
6371da177e4SLinus Torvalds 	unsigned int	sector_size;
6381da177e4SLinus Torvalds 	xfs_buf_t	*bp;
6391da177e4SLinus Torvalds 	int		error;
640af34e09dSDave Chinner 	int		loud = !(flags & XFS_MFSI_QUIET);
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	ASSERT(mp->m_sb_bp == NULL);
6431da177e4SLinus Torvalds 	ASSERT(mp->m_ddev_targp != NULL);
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	/*
6461da177e4SLinus Torvalds 	 * Allocate a (locked) buffer to hold the superblock.
6471da177e4SLinus Torvalds 	 * This will be kept around at all times to optimize
6481da177e4SLinus Torvalds 	 * access to the superblock.
6491da177e4SLinus Torvalds 	 */
6501da177e4SLinus Torvalds 	sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
6511da177e4SLinus Torvalds 
65226af6552SDave Chinner reread:
653e70b73f8SDave Chinner 	bp = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR,
654e70b73f8SDave Chinner 					BTOBB(sector_size), 0);
65526af6552SDave Chinner 	if (!bp) {
656af34e09dSDave Chinner 		if (loud)
657af34e09dSDave Chinner 			xfs_warn(mp, "SB buffer read failed");
65826af6552SDave Chinner 		return EIO;
6591da177e4SLinus Torvalds 	}
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	/*
6621da177e4SLinus Torvalds 	 * Initialize the mount structure from the superblock.
6631da177e4SLinus Torvalds 	 * But first do some basic consistency checking.
6641da177e4SLinus Torvalds 	 */
6656bd92a23SChandra Seetharaman 	xfs_sb_from_disk(mp, XFS_BUF_TO_SBP(bp));
666764d1f89SNathan Scott 	error = xfs_mount_validate_sb(mp, &(mp->m_sb), flags);
6671da177e4SLinus Torvalds 	if (error) {
668af34e09dSDave Chinner 		if (loud)
669af34e09dSDave Chinner 			xfs_warn(mp, "SB validate failed");
67026af6552SDave Chinner 		goto release_buf;
6711da177e4SLinus Torvalds 	}
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	/*
6741da177e4SLinus Torvalds 	 * We must be able to do sector-sized and sector-aligned IO.
6751da177e4SLinus Torvalds 	 */
6761da177e4SLinus Torvalds 	if (sector_size > mp->m_sb.sb_sectsize) {
677af34e09dSDave Chinner 		if (loud)
678af34e09dSDave Chinner 			xfs_warn(mp, "device supports %u byte sectors (not %u)",
6791da177e4SLinus Torvalds 				sector_size, mp->m_sb.sb_sectsize);
6801da177e4SLinus Torvalds 		error = ENOSYS;
68126af6552SDave Chinner 		goto release_buf;
6821da177e4SLinus Torvalds 	}
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 	/*
6851da177e4SLinus Torvalds 	 * If device sector size is smaller than the superblock size,
6861da177e4SLinus Torvalds 	 * re-read the superblock so the buffer is correctly sized.
6871da177e4SLinus Torvalds 	 */
6881da177e4SLinus Torvalds 	if (sector_size < mp->m_sb.sb_sectsize) {
6891da177e4SLinus Torvalds 		xfs_buf_relse(bp);
6901da177e4SLinus Torvalds 		sector_size = mp->m_sb.sb_sectsize;
69126af6552SDave Chinner 		goto reread;
6921da177e4SLinus Torvalds 	}
6931da177e4SLinus Torvalds 
6945478eeadSLachlan McIlroy 	/* Initialize per-cpu counters */
6955478eeadSLachlan McIlroy 	xfs_icsb_reinit_counters(mp);
6968d280b98SDavid Chinner 
6971da177e4SLinus Torvalds 	mp->m_sb_bp = bp;
69826af6552SDave Chinner 	xfs_buf_unlock(bp);
6991da177e4SLinus Torvalds 	return 0;
7001da177e4SLinus Torvalds 
70126af6552SDave Chinner release_buf:
7021da177e4SLinus Torvalds 	xfs_buf_relse(bp);
7031da177e4SLinus Torvalds 	return error;
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds /*
7081da177e4SLinus Torvalds  * xfs_mount_common
7091da177e4SLinus Torvalds  *
7101da177e4SLinus Torvalds  * Mount initialization code establishing various mount
7111da177e4SLinus Torvalds  * fields from the superblock associated with the given
7121da177e4SLinus Torvalds  * mount structure
7131da177e4SLinus Torvalds  */
714ba0f32d4SChristoph Hellwig STATIC void
7151da177e4SLinus Torvalds xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
7161da177e4SLinus Torvalds {
7171da177e4SLinus Torvalds 	mp->m_agfrotor = mp->m_agirotor = 0;
718007c61c6SEric Sandeen 	spin_lock_init(&mp->m_agirotor_lock);
7191da177e4SLinus Torvalds 	mp->m_maxagi = mp->m_sb.sb_agcount;
7201da177e4SLinus Torvalds 	mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
7211da177e4SLinus Torvalds 	mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
7221da177e4SLinus Torvalds 	mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
7231da177e4SLinus Torvalds 	mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
7241da177e4SLinus Torvalds 	mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
7251da177e4SLinus Torvalds 	mp->m_blockmask = sbp->sb_blocksize - 1;
7261da177e4SLinus Torvalds 	mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
7271da177e4SLinus Torvalds 	mp->m_blockwmask = mp->m_blockwsize - 1;
7281da177e4SLinus Torvalds 
72960197e8dSChristoph Hellwig 	mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
73060197e8dSChristoph Hellwig 	mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
73160197e8dSChristoph Hellwig 	mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
73260197e8dSChristoph Hellwig 	mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
73360197e8dSChristoph Hellwig 
73460197e8dSChristoph Hellwig 	mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
73560197e8dSChristoph Hellwig 	mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
73660197e8dSChristoph Hellwig 	mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
73760197e8dSChristoph Hellwig 	mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
73860197e8dSChristoph Hellwig 
73960197e8dSChristoph Hellwig 	mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
74060197e8dSChristoph Hellwig 	mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
74160197e8dSChristoph Hellwig 	mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
74260197e8dSChristoph Hellwig 	mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
7451da177e4SLinus Torvalds 	mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
7461da177e4SLinus Torvalds 					sbp->sb_inopblock);
7471da177e4SLinus Torvalds 	mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
7481da177e4SLinus Torvalds }
74992821e2bSDavid Chinner 
75092821e2bSDavid Chinner /*
75192821e2bSDavid Chinner  * xfs_initialize_perag_data
75292821e2bSDavid Chinner  *
75392821e2bSDavid Chinner  * Read in each per-ag structure so we can count up the number of
75492821e2bSDavid Chinner  * allocated inodes, free inodes and used filesystem blocks as this
75592821e2bSDavid Chinner  * information is no longer persistent in the superblock. Once we have
75692821e2bSDavid Chinner  * this information, write it into the in-core superblock structure.
75792821e2bSDavid Chinner  */
75892821e2bSDavid Chinner STATIC int
75992821e2bSDavid Chinner xfs_initialize_perag_data(xfs_mount_t *mp, xfs_agnumber_t agcount)
76092821e2bSDavid Chinner {
76192821e2bSDavid Chinner 	xfs_agnumber_t	index;
76292821e2bSDavid Chinner 	xfs_perag_t	*pag;
76392821e2bSDavid Chinner 	xfs_sb_t	*sbp = &mp->m_sb;
76492821e2bSDavid Chinner 	uint64_t	ifree = 0;
76592821e2bSDavid Chinner 	uint64_t	ialloc = 0;
76692821e2bSDavid Chinner 	uint64_t	bfree = 0;
76792821e2bSDavid Chinner 	uint64_t	bfreelst = 0;
76892821e2bSDavid Chinner 	uint64_t	btree = 0;
76992821e2bSDavid Chinner 	int		error;
77092821e2bSDavid Chinner 
77192821e2bSDavid Chinner 	for (index = 0; index < agcount; index++) {
77292821e2bSDavid Chinner 		/*
77392821e2bSDavid Chinner 		 * read the agf, then the agi. This gets us
7749da096fdSMalcolm Parsons 		 * all the information we need and populates the
77592821e2bSDavid Chinner 		 * per-ag structures for us.
77692821e2bSDavid Chinner 		 */
77792821e2bSDavid Chinner 		error = xfs_alloc_pagf_init(mp, NULL, index, 0);
77892821e2bSDavid Chinner 		if (error)
77992821e2bSDavid Chinner 			return error;
78092821e2bSDavid Chinner 
78192821e2bSDavid Chinner 		error = xfs_ialloc_pagi_init(mp, NULL, index);
78292821e2bSDavid Chinner 		if (error)
78392821e2bSDavid Chinner 			return error;
78444b56e0aSDave Chinner 		pag = xfs_perag_get(mp, index);
78592821e2bSDavid Chinner 		ifree += pag->pagi_freecount;
78692821e2bSDavid Chinner 		ialloc += pag->pagi_count;
78792821e2bSDavid Chinner 		bfree += pag->pagf_freeblks;
78892821e2bSDavid Chinner 		bfreelst += pag->pagf_flcount;
78992821e2bSDavid Chinner 		btree += pag->pagf_btreeblks;
79044b56e0aSDave Chinner 		xfs_perag_put(pag);
79192821e2bSDavid Chinner 	}
79292821e2bSDavid Chinner 	/*
79392821e2bSDavid Chinner 	 * Overwrite incore superblock counters with just-read data
79492821e2bSDavid Chinner 	 */
7953685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
79692821e2bSDavid Chinner 	sbp->sb_ifree = ifree;
79792821e2bSDavid Chinner 	sbp->sb_icount = ialloc;
79892821e2bSDavid Chinner 	sbp->sb_fdblocks = bfree + bfreelst + btree;
7993685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
80092821e2bSDavid Chinner 
80192821e2bSDavid Chinner 	/* Fixup the per-cpu counters as well. */
80292821e2bSDavid Chinner 	xfs_icsb_reinit_counters(mp);
80392821e2bSDavid Chinner 
80492821e2bSDavid Chinner 	return 0;
80592821e2bSDavid Chinner }
80692821e2bSDavid Chinner 
8071da177e4SLinus Torvalds /*
8080771fb45SEric Sandeen  * Update alignment values based on mount options and sb values
8091da177e4SLinus Torvalds  */
8100771fb45SEric Sandeen STATIC int
8117884bc86SChristoph Hellwig xfs_update_alignment(xfs_mount_t *mp)
8121da177e4SLinus Torvalds {
8131da177e4SLinus Torvalds 	xfs_sb_t	*sbp = &(mp->m_sb);
8141da177e4SLinus Torvalds 
8154249023aSChristoph Hellwig 	if (mp->m_dalign) {
8161da177e4SLinus Torvalds 		/*
8171da177e4SLinus Torvalds 		 * If stripe unit and stripe width are not multiples
8181da177e4SLinus Torvalds 		 * of the fs blocksize turn off alignment.
8191da177e4SLinus Torvalds 		 */
8201da177e4SLinus Torvalds 		if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
8211da177e4SLinus Torvalds 		    (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
8221da177e4SLinus Torvalds 			if (mp->m_flags & XFS_MOUNT_RETERR) {
823c0e090ceSEric Sandeen 				xfs_warn(mp, "alignment check failed: "
824c0e090ceSEric Sandeen 					 "(sunit/swidth vs. blocksize)");
8250771fb45SEric Sandeen 				return XFS_ERROR(EINVAL);
8261da177e4SLinus Torvalds 			}
8271da177e4SLinus Torvalds 			mp->m_dalign = mp->m_swidth = 0;
8281da177e4SLinus Torvalds 		} else {
8291da177e4SLinus Torvalds 			/*
8301da177e4SLinus Torvalds 			 * Convert the stripe unit and width to FSBs.
8311da177e4SLinus Torvalds 			 */
8321da177e4SLinus Torvalds 			mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
8331da177e4SLinus Torvalds 			if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
8341da177e4SLinus Torvalds 				if (mp->m_flags & XFS_MOUNT_RETERR) {
835c0e090ceSEric Sandeen 					xfs_warn(mp, "alignment check failed: "
836c0e090ceSEric Sandeen 						 "(sunit/swidth vs. ag size)");
8370771fb45SEric Sandeen 					return XFS_ERROR(EINVAL);
8381da177e4SLinus Torvalds 				}
83953487786SDave Chinner 				xfs_warn(mp,
84053487786SDave Chinner 		"stripe alignment turned off: sunit(%d)/swidth(%d) "
84153487786SDave Chinner 		"incompatible with agsize(%d)",
8421da177e4SLinus Torvalds 					mp->m_dalign, mp->m_swidth,
8431da177e4SLinus Torvalds 					sbp->sb_agblocks);
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 				mp->m_dalign = 0;
8461da177e4SLinus Torvalds 				mp->m_swidth = 0;
8471da177e4SLinus Torvalds 			} else if (mp->m_dalign) {
8481da177e4SLinus Torvalds 				mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
8491da177e4SLinus Torvalds 			} else {
8501da177e4SLinus Torvalds 				if (mp->m_flags & XFS_MOUNT_RETERR) {
851c0e090ceSEric Sandeen 					xfs_warn(mp, "alignment check failed: "
852c0e090ceSEric Sandeen 						"sunit(%d) less than bsize(%d)",
8531da177e4SLinus Torvalds 						mp->m_dalign,
8541da177e4SLinus Torvalds 						mp->m_blockmask +1);
8550771fb45SEric Sandeen 					return XFS_ERROR(EINVAL);
8561da177e4SLinus Torvalds 				}
8571da177e4SLinus Torvalds 				mp->m_swidth = 0;
8581da177e4SLinus Torvalds 			}
8591da177e4SLinus Torvalds 		}
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 		/*
8621da177e4SLinus Torvalds 		 * Update superblock with new values
8631da177e4SLinus Torvalds 		 * and log changes
8641da177e4SLinus Torvalds 		 */
86562118709SEric Sandeen 		if (xfs_sb_version_hasdalign(sbp)) {
8661da177e4SLinus Torvalds 			if (sbp->sb_unit != mp->m_dalign) {
8671da177e4SLinus Torvalds 				sbp->sb_unit = mp->m_dalign;
8687884bc86SChristoph Hellwig 				mp->m_update_flags |= XFS_SB_UNIT;
8691da177e4SLinus Torvalds 			}
8701da177e4SLinus Torvalds 			if (sbp->sb_width != mp->m_swidth) {
8711da177e4SLinus Torvalds 				sbp->sb_width = mp->m_swidth;
8727884bc86SChristoph Hellwig 				mp->m_update_flags |= XFS_SB_WIDTH;
8731da177e4SLinus Torvalds 			}
8741da177e4SLinus Torvalds 		}
8751da177e4SLinus Torvalds 	} else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
87662118709SEric Sandeen 		    xfs_sb_version_hasdalign(&mp->m_sb)) {
8771da177e4SLinus Torvalds 			mp->m_dalign = sbp->sb_unit;
8781da177e4SLinus Torvalds 			mp->m_swidth = sbp->sb_width;
8791da177e4SLinus Torvalds 	}
8801da177e4SLinus Torvalds 
8810771fb45SEric Sandeen 	return 0;
8820771fb45SEric Sandeen }
8831da177e4SLinus Torvalds 
8840771fb45SEric Sandeen /*
8850771fb45SEric Sandeen  * Set the maximum inode count for this filesystem
8860771fb45SEric Sandeen  */
8870771fb45SEric Sandeen STATIC void
8880771fb45SEric Sandeen xfs_set_maxicount(xfs_mount_t *mp)
8890771fb45SEric Sandeen {
8900771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
8911da177e4SLinus Torvalds 	__uint64_t	icount;
8921da177e4SLinus Torvalds 
8930771fb45SEric Sandeen 	if (sbp->sb_imax_pct) {
8940771fb45SEric Sandeen 		/*
8950771fb45SEric Sandeen 		 * Make sure the maximum inode count is a multiple
8960771fb45SEric Sandeen 		 * of the units we allocate inodes in.
8971da177e4SLinus Torvalds 		 */
8981da177e4SLinus Torvalds 		icount = sbp->sb_dblocks * sbp->sb_imax_pct;
8991da177e4SLinus Torvalds 		do_div(icount, 100);
9001da177e4SLinus Torvalds 		do_div(icount, mp->m_ialloc_blks);
9011da177e4SLinus Torvalds 		mp->m_maxicount = (icount * mp->m_ialloc_blks)  <<
9021da177e4SLinus Torvalds 				   sbp->sb_inopblog;
9030771fb45SEric Sandeen 	} else {
9041da177e4SLinus Torvalds 		mp->m_maxicount = 0;
9051da177e4SLinus Torvalds 	}
9061da177e4SLinus Torvalds }
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds /*
9091da177e4SLinus Torvalds  * Set the default minimum read and write sizes unless
9101da177e4SLinus Torvalds  * already specified in a mount option.
9111da177e4SLinus Torvalds  * We use smaller I/O sizes when the file system
9121da177e4SLinus Torvalds  * is being used for NFS service (wsync mount option).
9131da177e4SLinus Torvalds  */
9140771fb45SEric Sandeen STATIC void
9150771fb45SEric Sandeen xfs_set_rw_sizes(xfs_mount_t *mp)
9160771fb45SEric Sandeen {
9170771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
9180771fb45SEric Sandeen 	int		readio_log, writeio_log;
9190771fb45SEric Sandeen 
9201da177e4SLinus Torvalds 	if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
9211da177e4SLinus Torvalds 		if (mp->m_flags & XFS_MOUNT_WSYNC) {
9221da177e4SLinus Torvalds 			readio_log = XFS_WSYNC_READIO_LOG;
9231da177e4SLinus Torvalds 			writeio_log = XFS_WSYNC_WRITEIO_LOG;
9241da177e4SLinus Torvalds 		} else {
9251da177e4SLinus Torvalds 			readio_log = XFS_READIO_LOG_LARGE;
9261da177e4SLinus Torvalds 			writeio_log = XFS_WRITEIO_LOG_LARGE;
9271da177e4SLinus Torvalds 		}
9281da177e4SLinus Torvalds 	} else {
9291da177e4SLinus Torvalds 		readio_log = mp->m_readio_log;
9301da177e4SLinus Torvalds 		writeio_log = mp->m_writeio_log;
9311da177e4SLinus Torvalds 	}
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	if (sbp->sb_blocklog > readio_log) {
9341da177e4SLinus Torvalds 		mp->m_readio_log = sbp->sb_blocklog;
9351da177e4SLinus Torvalds 	} else {
9361da177e4SLinus Torvalds 		mp->m_readio_log = readio_log;
9371da177e4SLinus Torvalds 	}
9381da177e4SLinus Torvalds 	mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
9391da177e4SLinus Torvalds 	if (sbp->sb_blocklog > writeio_log) {
9401da177e4SLinus Torvalds 		mp->m_writeio_log = sbp->sb_blocklog;
9411da177e4SLinus Torvalds 	} else {
9421da177e4SLinus Torvalds 		mp->m_writeio_log = writeio_log;
9431da177e4SLinus Torvalds 	}
9441da177e4SLinus Torvalds 	mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
9450771fb45SEric Sandeen }
946425f9dddSEric Sandeen 
9471da177e4SLinus Torvalds /*
948055388a3SDave Chinner  * precalculate the low space thresholds for dynamic speculative preallocation.
949055388a3SDave Chinner  */
950055388a3SDave Chinner void
951055388a3SDave Chinner xfs_set_low_space_thresholds(
952055388a3SDave Chinner 	struct xfs_mount	*mp)
953055388a3SDave Chinner {
954055388a3SDave Chinner 	int i;
955055388a3SDave Chinner 
956055388a3SDave Chinner 	for (i = 0; i < XFS_LOWSP_MAX; i++) {
957055388a3SDave Chinner 		__uint64_t space = mp->m_sb.sb_dblocks;
958055388a3SDave Chinner 
959055388a3SDave Chinner 		do_div(space, 100);
960055388a3SDave Chinner 		mp->m_low_space[i] = space * (i + 1);
961055388a3SDave Chinner 	}
962055388a3SDave Chinner }
963055388a3SDave Chinner 
964055388a3SDave Chinner 
965055388a3SDave Chinner /*
9661da177e4SLinus Torvalds  * Set whether we're using inode alignment.
9671da177e4SLinus Torvalds  */
9680771fb45SEric Sandeen STATIC void
9690771fb45SEric Sandeen xfs_set_inoalignment(xfs_mount_t *mp)
9700771fb45SEric Sandeen {
97162118709SEric Sandeen 	if (xfs_sb_version_hasalign(&mp->m_sb) &&
9721da177e4SLinus Torvalds 	    mp->m_sb.sb_inoalignmt >=
9731da177e4SLinus Torvalds 	    XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
9741da177e4SLinus Torvalds 		mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
9751da177e4SLinus Torvalds 	else
9761da177e4SLinus Torvalds 		mp->m_inoalign_mask = 0;
9771da177e4SLinus Torvalds 	/*
9781da177e4SLinus Torvalds 	 * If we are using stripe alignment, check whether
9791da177e4SLinus Torvalds 	 * the stripe unit is a multiple of the inode alignment
9801da177e4SLinus Torvalds 	 */
9811da177e4SLinus Torvalds 	if (mp->m_dalign && mp->m_inoalign_mask &&
9821da177e4SLinus Torvalds 	    !(mp->m_dalign & mp->m_inoalign_mask))
9831da177e4SLinus Torvalds 		mp->m_sinoalign = mp->m_dalign;
9841da177e4SLinus Torvalds 	else
9851da177e4SLinus Torvalds 		mp->m_sinoalign = 0;
9860771fb45SEric Sandeen }
9870771fb45SEric Sandeen 
9881da177e4SLinus Torvalds /*
9891da177e4SLinus Torvalds  * Check that the data (and log if separate) are an ok size.
9901da177e4SLinus Torvalds  */
9910771fb45SEric Sandeen STATIC int
9924249023aSChristoph Hellwig xfs_check_sizes(xfs_mount_t *mp)
9930771fb45SEric Sandeen {
9940771fb45SEric Sandeen 	xfs_buf_t	*bp;
9950771fb45SEric Sandeen 	xfs_daddr_t	d;
9960771fb45SEric Sandeen 
9971da177e4SLinus Torvalds 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
9981da177e4SLinus Torvalds 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
9990b932cccSDave Chinner 		xfs_warn(mp, "filesystem size mismatch detected");
1000657a4cffSEric Sandeen 		return XFS_ERROR(EFBIG);
10011da177e4SLinus Torvalds 	}
1002e70b73f8SDave Chinner 	bp = xfs_buf_read_uncached(mp->m_ddev_targp,
10031da177e4SLinus Torvalds 					d - XFS_FSS_TO_BB(mp, 1),
1004e70b73f8SDave Chinner 					XFS_FSS_TO_BB(mp, 1), 0);
10051922c949SDave Chinner 	if (!bp) {
10060b932cccSDave Chinner 		xfs_warn(mp, "last sector read failed");
10071922c949SDave Chinner 		return EIO;
10081da177e4SLinus Torvalds 	}
10091922c949SDave Chinner 	xfs_buf_relse(bp);
10101da177e4SLinus Torvalds 
10114249023aSChristoph Hellwig 	if (mp->m_logdev_targp != mp->m_ddev_targp) {
10121da177e4SLinus Torvalds 		d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
10131da177e4SLinus Torvalds 		if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
10140b932cccSDave Chinner 			xfs_warn(mp, "log size mismatch detected");
1015657a4cffSEric Sandeen 			return XFS_ERROR(EFBIG);
10161da177e4SLinus Torvalds 		}
1017e70b73f8SDave Chinner 		bp = xfs_buf_read_uncached(mp->m_logdev_targp,
10181da177e4SLinus Torvalds 					d - XFS_FSB_TO_BB(mp, 1),
1019e70b73f8SDave Chinner 					XFS_FSB_TO_BB(mp, 1), 0);
10201922c949SDave Chinner 		if (!bp) {
10210b932cccSDave Chinner 			xfs_warn(mp, "log device read failed");
10221922c949SDave Chinner 			return EIO;
10231da177e4SLinus Torvalds 		}
10241922c949SDave Chinner 		xfs_buf_relse(bp);
10250771fb45SEric Sandeen 	}
10260771fb45SEric Sandeen 	return 0;
10270771fb45SEric Sandeen }
10280771fb45SEric Sandeen 
10290771fb45SEric Sandeen /*
10307d095257SChristoph Hellwig  * Clear the quotaflags in memory and in the superblock.
10317d095257SChristoph Hellwig  */
10327d095257SChristoph Hellwig int
10337d095257SChristoph Hellwig xfs_mount_reset_sbqflags(
10347d095257SChristoph Hellwig 	struct xfs_mount	*mp)
10357d095257SChristoph Hellwig {
10367d095257SChristoph Hellwig 	int			error;
10377d095257SChristoph Hellwig 	struct xfs_trans	*tp;
10387d095257SChristoph Hellwig 
10397d095257SChristoph Hellwig 	mp->m_qflags = 0;
10407d095257SChristoph Hellwig 
10417d095257SChristoph Hellwig 	/*
10427d095257SChristoph Hellwig 	 * It is OK to look at sb_qflags here in mount path,
10437d095257SChristoph Hellwig 	 * without m_sb_lock.
10447d095257SChristoph Hellwig 	 */
10457d095257SChristoph Hellwig 	if (mp->m_sb.sb_qflags == 0)
10467d095257SChristoph Hellwig 		return 0;
10477d095257SChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
10487d095257SChristoph Hellwig 	mp->m_sb.sb_qflags = 0;
10497d095257SChristoph Hellwig 	spin_unlock(&mp->m_sb_lock);
10507d095257SChristoph Hellwig 
10517d095257SChristoph Hellwig 	/*
10527d095257SChristoph Hellwig 	 * If the fs is readonly, let the incore superblock run
10537d095257SChristoph Hellwig 	 * with quotas off but don't flush the update out to disk
10547d095257SChristoph Hellwig 	 */
10557d095257SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_RDONLY)
10567d095257SChristoph Hellwig 		return 0;
10577d095257SChristoph Hellwig 
10587d095257SChristoph Hellwig 	tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
10597d095257SChristoph Hellwig 	error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
10607d095257SChristoph Hellwig 				      XFS_DEFAULT_LOG_COUNT);
10617d095257SChristoph Hellwig 	if (error) {
10627d095257SChristoph Hellwig 		xfs_trans_cancel(tp, 0);
106353487786SDave Chinner 		xfs_alert(mp, "%s: Superblock update failed!", __func__);
10647d095257SChristoph Hellwig 		return error;
10657d095257SChristoph Hellwig 	}
10667d095257SChristoph Hellwig 
10677d095257SChristoph Hellwig 	xfs_mod_sb(tp, XFS_SB_QFLAGS);
10687d095257SChristoph Hellwig 	return xfs_trans_commit(tp, 0);
10697d095257SChristoph Hellwig }
10707d095257SChristoph Hellwig 
1071d5db0f97SEric Sandeen __uint64_t
1072d5db0f97SEric Sandeen xfs_default_resblks(xfs_mount_t *mp)
1073d5db0f97SEric Sandeen {
1074d5db0f97SEric Sandeen 	__uint64_t resblks;
1075d5db0f97SEric Sandeen 
1076d5db0f97SEric Sandeen 	/*
10778babd8a2SDave Chinner 	 * We default to 5% or 8192 fsbs of space reserved, whichever is
10788babd8a2SDave Chinner 	 * smaller.  This is intended to cover concurrent allocation
10798babd8a2SDave Chinner 	 * transactions when we initially hit enospc. These each require a 4
10808babd8a2SDave Chinner 	 * block reservation. Hence by default we cover roughly 2000 concurrent
10818babd8a2SDave Chinner 	 * allocation reservations.
1082d5db0f97SEric Sandeen 	 */
1083d5db0f97SEric Sandeen 	resblks = mp->m_sb.sb_dblocks;
1084d5db0f97SEric Sandeen 	do_div(resblks, 20);
10858babd8a2SDave Chinner 	resblks = min_t(__uint64_t, resblks, 8192);
1086d5db0f97SEric Sandeen 	return resblks;
1087d5db0f97SEric Sandeen }
1088d5db0f97SEric Sandeen 
10897d095257SChristoph Hellwig /*
10900771fb45SEric Sandeen  * This function does the following on an initial mount of a file system:
10910771fb45SEric Sandeen  *	- reads the superblock from disk and init the mount struct
10920771fb45SEric Sandeen  *	- if we're a 32-bit kernel, do a size check on the superblock
10930771fb45SEric Sandeen  *		so we don't mount terabyte filesystems
10940771fb45SEric Sandeen  *	- init mount struct realtime fields
10950771fb45SEric Sandeen  *	- allocate inode hash table for fs
10960771fb45SEric Sandeen  *	- init directory manager
10970771fb45SEric Sandeen  *	- perform recovery and init the log manager
10980771fb45SEric Sandeen  */
10990771fb45SEric Sandeen int
11000771fb45SEric Sandeen xfs_mountfs(
11014249023aSChristoph Hellwig 	xfs_mount_t	*mp)
11020771fb45SEric Sandeen {
11030771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
11040771fb45SEric Sandeen 	xfs_inode_t	*rip;
11050771fb45SEric Sandeen 	__uint64_t	resblks;
11067d095257SChristoph Hellwig 	uint		quotamount = 0;
11077d095257SChristoph Hellwig 	uint		quotaflags = 0;
11080771fb45SEric Sandeen 	int		error = 0;
11090771fb45SEric Sandeen 
11100771fb45SEric Sandeen 	xfs_mount_common(mp, sbp);
11110771fb45SEric Sandeen 
11120771fb45SEric Sandeen 	/*
1113e6957ea4SEric Sandeen 	 * Check for a mismatched features2 values.  Older kernels
1114e6957ea4SEric Sandeen 	 * read & wrote into the wrong sb offset for sb_features2
1115e6957ea4SEric Sandeen 	 * on some platforms due to xfs_sb_t not being 64bit size aligned
1116e6957ea4SEric Sandeen 	 * when sb_features2 was added, which made older superblock
1117e6957ea4SEric Sandeen 	 * reading/writing routines swap it as a 64-bit value.
1118ee1c0908SDavid Chinner 	 *
1119e6957ea4SEric Sandeen 	 * For backwards compatibility, we make both slots equal.
1120e6957ea4SEric Sandeen 	 *
1121e6957ea4SEric Sandeen 	 * If we detect a mismatched field, we OR the set bits into the
1122e6957ea4SEric Sandeen 	 * existing features2 field in case it has already been modified; we
1123e6957ea4SEric Sandeen 	 * don't want to lose any features.  We then update the bad location
1124e6957ea4SEric Sandeen 	 * with the ORed value so that older kernels will see any features2
1125e6957ea4SEric Sandeen 	 * flags, and mark the two fields as needing updates once the
1126e6957ea4SEric Sandeen 	 * transaction subsystem is online.
1127ee1c0908SDavid Chinner 	 */
1128e6957ea4SEric Sandeen 	if (xfs_sb_has_mismatched_features2(sbp)) {
11290b932cccSDave Chinner 		xfs_warn(mp, "correcting sb_features alignment problem");
1130ee1c0908SDavid Chinner 		sbp->sb_features2 |= sbp->sb_bad_features2;
1131e6957ea4SEric Sandeen 		sbp->sb_bad_features2 = sbp->sb_features2;
11327884bc86SChristoph Hellwig 		mp->m_update_flags |= XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2;
1133e6957ea4SEric Sandeen 
1134e6957ea4SEric Sandeen 		/*
1135e6957ea4SEric Sandeen 		 * Re-check for ATTR2 in case it was found in bad_features2
1136e6957ea4SEric Sandeen 		 * slot.
1137e6957ea4SEric Sandeen 		 */
11387c12f296STim Shimmin 		if (xfs_sb_version_hasattr2(&mp->m_sb) &&
11397c12f296STim Shimmin 		   !(mp->m_flags & XFS_MOUNT_NOATTR2))
1140e6957ea4SEric Sandeen 			mp->m_flags |= XFS_MOUNT_ATTR2;
11417c12f296STim Shimmin 	}
1142e6957ea4SEric Sandeen 
11437c12f296STim Shimmin 	if (xfs_sb_version_hasattr2(&mp->m_sb) &&
11447c12f296STim Shimmin 	   (mp->m_flags & XFS_MOUNT_NOATTR2)) {
11457c12f296STim Shimmin 		xfs_sb_version_removeattr2(&mp->m_sb);
11467884bc86SChristoph Hellwig 		mp->m_update_flags |= XFS_SB_FEATURES2;
11477c12f296STim Shimmin 
11487c12f296STim Shimmin 		/* update sb_versionnum for the clearing of the morebits */
11497c12f296STim Shimmin 		if (!sbp->sb_features2)
11507884bc86SChristoph Hellwig 			mp->m_update_flags |= XFS_SB_VERSIONNUM;
1151ee1c0908SDavid Chinner 	}
1152ee1c0908SDavid Chinner 
1153ee1c0908SDavid Chinner 	/*
11540771fb45SEric Sandeen 	 * Check if sb_agblocks is aligned at stripe boundary
11550771fb45SEric Sandeen 	 * If sb_agblocks is NOT aligned turn off m_dalign since
11560771fb45SEric Sandeen 	 * allocator alignment is within an ag, therefore ag has
11570771fb45SEric Sandeen 	 * to be aligned at stripe boundary.
11580771fb45SEric Sandeen 	 */
11597884bc86SChristoph Hellwig 	error = xfs_update_alignment(mp);
11600771fb45SEric Sandeen 	if (error)
1161f9057e3dSChristoph Hellwig 		goto out;
11620771fb45SEric Sandeen 
11630771fb45SEric Sandeen 	xfs_alloc_compute_maxlevels(mp);
11640771fb45SEric Sandeen 	xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
11650771fb45SEric Sandeen 	xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
11660771fb45SEric Sandeen 	xfs_ialloc_compute_maxlevels(mp);
11670771fb45SEric Sandeen 
11680771fb45SEric Sandeen 	xfs_set_maxicount(mp);
11690771fb45SEric Sandeen 
117027174203SChristoph Hellwig 	error = xfs_uuid_mount(mp);
117127174203SChristoph Hellwig 	if (error)
1172f9057e3dSChristoph Hellwig 		goto out;
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	/*
11750771fb45SEric Sandeen 	 * Set the minimum read and write sizes
11760771fb45SEric Sandeen 	 */
11770771fb45SEric Sandeen 	xfs_set_rw_sizes(mp);
11780771fb45SEric Sandeen 
1179055388a3SDave Chinner 	/* set the low space thresholds for dynamic preallocation */
1180055388a3SDave Chinner 	xfs_set_low_space_thresholds(mp);
1181055388a3SDave Chinner 
11820771fb45SEric Sandeen 	/*
11830771fb45SEric Sandeen 	 * Set the inode cluster size.
11840771fb45SEric Sandeen 	 * This may still be overridden by the file system
11850771fb45SEric Sandeen 	 * block size if it is larger than the chosen cluster size.
11860771fb45SEric Sandeen 	 */
11870771fb45SEric Sandeen 	mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
11880771fb45SEric Sandeen 
11890771fb45SEric Sandeen 	/*
11900771fb45SEric Sandeen 	 * Set inode alignment fields
11910771fb45SEric Sandeen 	 */
11920771fb45SEric Sandeen 	xfs_set_inoalignment(mp);
11930771fb45SEric Sandeen 
11940771fb45SEric Sandeen 	/*
11950771fb45SEric Sandeen 	 * Check that the data (and log if separate) are an ok size.
11960771fb45SEric Sandeen 	 */
11974249023aSChristoph Hellwig 	error = xfs_check_sizes(mp);
11980771fb45SEric Sandeen 	if (error)
1199f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
12000771fb45SEric Sandeen 
12010771fb45SEric Sandeen 	/*
12021da177e4SLinus Torvalds 	 * Initialize realtime fields in the mount structure
12031da177e4SLinus Torvalds 	 */
12040771fb45SEric Sandeen 	error = xfs_rtmount_init(mp);
12050771fb45SEric Sandeen 	if (error) {
12060b932cccSDave Chinner 		xfs_warn(mp, "RT mount failed");
1207f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
12081da177e4SLinus Torvalds 	}
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	/*
12111da177e4SLinus Torvalds 	 *  Copies the low order bits of the timestamp and the randomly
12121da177e4SLinus Torvalds 	 *  set "sequence" number out of a UUID.
12131da177e4SLinus Torvalds 	 */
12141da177e4SLinus Torvalds 	uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds 	mp->m_dmevmask = 0;	/* not persistent; set after each mount */
12171da177e4SLinus Torvalds 
1218f6c2d1faSNathan Scott 	xfs_dir_mount(mp);
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds 	/*
12211da177e4SLinus Torvalds 	 * Initialize the attribute manager's entries.
12221da177e4SLinus Torvalds 	 */
12231da177e4SLinus Torvalds 	mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 	/*
12261da177e4SLinus Torvalds 	 * Initialize the precomputed transaction reservations values.
12271da177e4SLinus Torvalds 	 */
12281da177e4SLinus Torvalds 	xfs_trans_init(mp);
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 	/*
12311da177e4SLinus Torvalds 	 * Allocate and initialize the per-ag data.
12321da177e4SLinus Torvalds 	 */
12331c1c6ebcSDave Chinner 	spin_lock_init(&mp->m_perag_lock);
12349b98b6f3SDave Chinner 	INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
12351c1c6ebcSDave Chinner 	error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
12361c1c6ebcSDave Chinner 	if (error) {
12370b932cccSDave Chinner 		xfs_warn(mp, "Failed per-ag init: %d", error);
1238f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
12391c1c6ebcSDave Chinner 	}
12401da177e4SLinus Torvalds 
1241f9057e3dSChristoph Hellwig 	if (!sbp->sb_logblocks) {
12420b932cccSDave Chinner 		xfs_warn(mp, "no log defined");
1243f9057e3dSChristoph Hellwig 		XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp);
1244f9057e3dSChristoph Hellwig 		error = XFS_ERROR(EFSCORRUPTED);
1245f9057e3dSChristoph Hellwig 		goto out_free_perag;
1246f9057e3dSChristoph Hellwig 	}
1247f9057e3dSChristoph Hellwig 
12481da177e4SLinus Torvalds 	/*
12491da177e4SLinus Torvalds 	 * log's mount-time initialization. Perform 1st part recovery if needed
12501da177e4SLinus Torvalds 	 */
12511da177e4SLinus Torvalds 	error = xfs_log_mount(mp, mp->m_logdev_targp,
12521da177e4SLinus Torvalds 			      XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
12531da177e4SLinus Torvalds 			      XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
12541da177e4SLinus Torvalds 	if (error) {
12550b932cccSDave Chinner 		xfs_warn(mp, "log mount failed");
1256d4f3512bSDave Chinner 		goto out_fail_wait;
12571da177e4SLinus Torvalds 	}
12581da177e4SLinus Torvalds 
12591da177e4SLinus Torvalds 	/*
126092821e2bSDavid Chinner 	 * Now the log is mounted, we know if it was an unclean shutdown or
126192821e2bSDavid Chinner 	 * not. If it was, with the first phase of recovery has completed, we
126292821e2bSDavid Chinner 	 * have consistent AG blocks on disk. We have not recovered EFIs yet,
126392821e2bSDavid Chinner 	 * but they are recovered transactionally in the second recovery phase
126492821e2bSDavid Chinner 	 * later.
126592821e2bSDavid Chinner 	 *
126692821e2bSDavid Chinner 	 * Hence we can safely re-initialise incore superblock counters from
126792821e2bSDavid Chinner 	 * the per-ag data. These may not be correct if the filesystem was not
126892821e2bSDavid Chinner 	 * cleanly unmounted, so we need to wait for recovery to finish before
126992821e2bSDavid Chinner 	 * doing this.
127092821e2bSDavid Chinner 	 *
127192821e2bSDavid Chinner 	 * If the filesystem was cleanly unmounted, then we can trust the
127292821e2bSDavid Chinner 	 * values in the superblock to be correct and we don't need to do
127392821e2bSDavid Chinner 	 * anything here.
127492821e2bSDavid Chinner 	 *
127592821e2bSDavid Chinner 	 * If we are currently making the filesystem, the initialisation will
127692821e2bSDavid Chinner 	 * fail as the perag data is in an undefined state.
127792821e2bSDavid Chinner 	 */
127892821e2bSDavid Chinner 	if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
127992821e2bSDavid Chinner 	    !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
128092821e2bSDavid Chinner 	     !mp->m_sb.sb_inprogress) {
128192821e2bSDavid Chinner 		error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
1282f9057e3dSChristoph Hellwig 		if (error)
1283d4f3512bSDave Chinner 			goto out_fail_wait;
128492821e2bSDavid Chinner 	}
1285f9057e3dSChristoph Hellwig 
128692821e2bSDavid Chinner 	/*
12871da177e4SLinus Torvalds 	 * Get and sanity-check the root inode.
12881da177e4SLinus Torvalds 	 * Save the pointer to it in the mount structure.
12891da177e4SLinus Torvalds 	 */
12907b6259e7SDave Chinner 	error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip);
12911da177e4SLinus Torvalds 	if (error) {
12920b932cccSDave Chinner 		xfs_warn(mp, "failed to read root inode");
1293f9057e3dSChristoph Hellwig 		goto out_log_dealloc;
12941da177e4SLinus Torvalds 	}
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 	ASSERT(rip != NULL);
12971da177e4SLinus Torvalds 
1298abbede1bSAl Viro 	if (unlikely(!S_ISDIR(rip->i_d.di_mode))) {
12990b932cccSDave Chinner 		xfs_warn(mp, "corrupted root inode %llu: not a directory",
1300b6574520SNathan Scott 			(unsigned long long)rip->i_ino);
13011da177e4SLinus Torvalds 		xfs_iunlock(rip, XFS_ILOCK_EXCL);
13021da177e4SLinus Torvalds 		XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
13031da177e4SLinus Torvalds 				 mp);
13041da177e4SLinus Torvalds 		error = XFS_ERROR(EFSCORRUPTED);
1305f9057e3dSChristoph Hellwig 		goto out_rele_rip;
13061da177e4SLinus Torvalds 	}
13071da177e4SLinus Torvalds 	mp->m_rootip = rip;	/* save it */
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 	xfs_iunlock(rip, XFS_ILOCK_EXCL);
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 	/*
13121da177e4SLinus Torvalds 	 * Initialize realtime inode pointers in the mount structure
13131da177e4SLinus Torvalds 	 */
13140771fb45SEric Sandeen 	error = xfs_rtmount_inodes(mp);
13150771fb45SEric Sandeen 	if (error) {
13161da177e4SLinus Torvalds 		/*
13171da177e4SLinus Torvalds 		 * Free up the root inode.
13181da177e4SLinus Torvalds 		 */
13190b932cccSDave Chinner 		xfs_warn(mp, "failed to read RT inodes");
1320f9057e3dSChristoph Hellwig 		goto out_rele_rip;
13211da177e4SLinus Torvalds 	}
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 	/*
13247884bc86SChristoph Hellwig 	 * If this is a read-only mount defer the superblock updates until
13257884bc86SChristoph Hellwig 	 * the next remount into writeable mode.  Otherwise we would never
13267884bc86SChristoph Hellwig 	 * perform the update e.g. for the root filesystem.
13271da177e4SLinus Torvalds 	 */
13287884bc86SChristoph Hellwig 	if (mp->m_update_flags && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
13297884bc86SChristoph Hellwig 		error = xfs_mount_log_sb(mp, mp->m_update_flags);
1330e5720eecSDavid Chinner 		if (error) {
13310b932cccSDave Chinner 			xfs_warn(mp, "failed to write sb changes");
1332b93b6e43SChristoph Hellwig 			goto out_rtunmount;
1333e5720eecSDavid Chinner 		}
1334e5720eecSDavid Chinner 	}
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds 	/*
13371da177e4SLinus Torvalds 	 * Initialise the XFS quota management subsystem for this mount
13381da177e4SLinus Torvalds 	 */
13397d095257SChristoph Hellwig 	if (XFS_IS_QUOTA_RUNNING(mp)) {
13407d095257SChristoph Hellwig 		error = xfs_qm_newmount(mp, &quotamount, &quotaflags);
13410771fb45SEric Sandeen 		if (error)
1342b93b6e43SChristoph Hellwig 			goto out_rtunmount;
13437d095257SChristoph Hellwig 	} else {
13447d095257SChristoph Hellwig 		ASSERT(!XFS_IS_QUOTA_ON(mp));
13457d095257SChristoph Hellwig 
13467d095257SChristoph Hellwig 		/*
13477d095257SChristoph Hellwig 		 * If a file system had quotas running earlier, but decided to
13487d095257SChristoph Hellwig 		 * mount without -o uquota/pquota/gquota options, revoke the
13497d095257SChristoph Hellwig 		 * quotachecked license.
13507d095257SChristoph Hellwig 		 */
13517d095257SChristoph Hellwig 		if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) {
13520b932cccSDave Chinner 			xfs_notice(mp, "resetting quota flags");
13537d095257SChristoph Hellwig 			error = xfs_mount_reset_sbqflags(mp);
13547d095257SChristoph Hellwig 			if (error)
13557d095257SChristoph Hellwig 				return error;
13567d095257SChristoph Hellwig 		}
13577d095257SChristoph Hellwig 	}
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 	/*
13601da177e4SLinus Torvalds 	 * Finish recovering the file system.  This part needed to be
13611da177e4SLinus Torvalds 	 * delayed until after the root and real-time bitmap inodes
13621da177e4SLinus Torvalds 	 * were consistently read in.
13631da177e4SLinus Torvalds 	 */
13644249023aSChristoph Hellwig 	error = xfs_log_mount_finish(mp);
13651da177e4SLinus Torvalds 	if (error) {
13660b932cccSDave Chinner 		xfs_warn(mp, "log mount finish failed");
1367b93b6e43SChristoph Hellwig 		goto out_rtunmount;
13681da177e4SLinus Torvalds 	}
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 	/*
13711da177e4SLinus Torvalds 	 * Complete the quota initialisation, post-log-replay component.
13721da177e4SLinus Torvalds 	 */
13737d095257SChristoph Hellwig 	if (quotamount) {
13747d095257SChristoph Hellwig 		ASSERT(mp->m_qflags == 0);
13757d095257SChristoph Hellwig 		mp->m_qflags = quotaflags;
13767d095257SChristoph Hellwig 
13777d095257SChristoph Hellwig 		xfs_qm_mount_quotas(mp);
13787d095257SChristoph Hellwig 	}
13797d095257SChristoph Hellwig 
138084e1e99fSDavid Chinner 	/*
138184e1e99fSDavid Chinner 	 * Now we are mounted, reserve a small amount of unused space for
138284e1e99fSDavid Chinner 	 * privileged transactions. This is needed so that transaction
138384e1e99fSDavid Chinner 	 * space required for critical operations can dip into this pool
138484e1e99fSDavid Chinner 	 * when at ENOSPC. This is needed for operations like create with
138584e1e99fSDavid Chinner 	 * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
138684e1e99fSDavid Chinner 	 * are not allowed to use this reserved space.
13878babd8a2SDave Chinner 	 *
13888babd8a2SDave Chinner 	 * This may drive us straight to ENOSPC on mount, but that implies
13898babd8a2SDave Chinner 	 * we were already there on the last unmount. Warn if this occurs.
139084e1e99fSDavid Chinner 	 */
1391d5db0f97SEric Sandeen 	if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
1392d5db0f97SEric Sandeen 		resblks = xfs_default_resblks(mp);
1393714082bcSDavid Chinner 		error = xfs_reserve_blocks(mp, &resblks, NULL);
1394714082bcSDavid Chinner 		if (error)
13950b932cccSDave Chinner 			xfs_warn(mp,
13960b932cccSDave Chinner 	"Unable to allocate reserve blocks. Continuing without reserve pool.");
1397d5db0f97SEric Sandeen 	}
139884e1e99fSDavid Chinner 
13991da177e4SLinus Torvalds 	return 0;
14001da177e4SLinus Torvalds 
1401b93b6e43SChristoph Hellwig  out_rtunmount:
1402b93b6e43SChristoph Hellwig 	xfs_rtunmount_inodes(mp);
1403f9057e3dSChristoph Hellwig  out_rele_rip:
140443355099SChristoph Hellwig 	IRELE(rip);
1405f9057e3dSChristoph Hellwig  out_log_dealloc:
140621b699c8SChristoph Hellwig 	xfs_log_unmount(mp);
1407d4f3512bSDave Chinner  out_fail_wait:
1408d4f3512bSDave Chinner 	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
1409d4f3512bSDave Chinner 		xfs_wait_buftarg(mp->m_logdev_targp);
1410d4f3512bSDave Chinner 	xfs_wait_buftarg(mp->m_ddev_targp);
1411f9057e3dSChristoph Hellwig  out_free_perag:
1412ff4f038cSChristoph Hellwig 	xfs_free_perag(mp);
1413f9057e3dSChristoph Hellwig  out_remove_uuid:
141427174203SChristoph Hellwig 	xfs_uuid_unmount(mp);
1415f9057e3dSChristoph Hellwig  out:
14161da177e4SLinus Torvalds 	return error;
14171da177e4SLinus Torvalds }
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds /*
14201da177e4SLinus Torvalds  * This flushes out the inodes,dquots and the superblock, unmounts the
14211da177e4SLinus Torvalds  * log and makes sure that incore structures are freed.
14221da177e4SLinus Torvalds  */
142341b5c2e7SChristoph Hellwig void
142441b5c2e7SChristoph Hellwig xfs_unmountfs(
142541b5c2e7SChristoph Hellwig 	struct xfs_mount	*mp)
14261da177e4SLinus Torvalds {
142784e1e99fSDavid Chinner 	__uint64_t		resblks;
142841b5c2e7SChristoph Hellwig 	int			error;
14291da177e4SLinus Torvalds 
14307d095257SChristoph Hellwig 	xfs_qm_unmount_quotas(mp);
1431b93b6e43SChristoph Hellwig 	xfs_rtunmount_inodes(mp);
143277508ec8SChristoph Hellwig 	IRELE(mp->m_rootip);
143377508ec8SChristoph Hellwig 
1434641c56fbSDavid Chinner 	/*
1435641c56fbSDavid Chinner 	 * We can potentially deadlock here if we have an inode cluster
14369da096fdSMalcolm Parsons 	 * that has been freed has its buffer still pinned in memory because
1437641c56fbSDavid Chinner 	 * the transaction is still sitting in a iclog. The stale inodes
1438641c56fbSDavid Chinner 	 * on that buffer will have their flush locks held until the
1439641c56fbSDavid Chinner 	 * transaction hits the disk and the callbacks run. the inode
1440641c56fbSDavid Chinner 	 * flush takes the flush lock unconditionally and with nothing to
1441641c56fbSDavid Chinner 	 * push out the iclog we will never get that unlocked. hence we
1442641c56fbSDavid Chinner 	 * need to force the log first.
1443641c56fbSDavid Chinner 	 */
1444a14a348bSChristoph Hellwig 	xfs_log_force(mp, XFS_LOG_SYNC);
1445c854363eSDave Chinner 
1446c854363eSDave Chinner 	/*
1447211e4d43SChristoph Hellwig 	 * Flush all pending changes from the AIL.
1448c854363eSDave Chinner 	 */
1449211e4d43SChristoph Hellwig 	xfs_ail_push_all_sync(mp->m_ail);
1450211e4d43SChristoph Hellwig 
1451211e4d43SChristoph Hellwig 	/*
1452211e4d43SChristoph Hellwig 	 * And reclaim all inodes.  At this point there should be no dirty
1453211e4d43SChristoph Hellwig 	 * inode, and none should be pinned or locked, but use synchronous
1454211e4d43SChristoph Hellwig 	 * reclaim just to be sure.
1455211e4d43SChristoph Hellwig 	 */
1456c854363eSDave Chinner 	xfs_reclaim_inodes(mp, SYNC_WAIT);
14571da177e4SLinus Torvalds 
14587d095257SChristoph Hellwig 	xfs_qm_unmount(mp);
1459a357a121SLachlan McIlroy 
14601da177e4SLinus Torvalds 	/*
14611da177e4SLinus Torvalds 	 * Flush out the log synchronously so that we know for sure
14621da177e4SLinus Torvalds 	 * that nothing is pinned.  This is important because bflush()
14631da177e4SLinus Torvalds 	 * will skip pinned buffers.
14641da177e4SLinus Torvalds 	 */
1465a14a348bSChristoph Hellwig 	xfs_log_force(mp, XFS_LOG_SYNC);
14661da177e4SLinus Torvalds 
146784e1e99fSDavid Chinner 	/*
146884e1e99fSDavid Chinner 	 * Unreserve any blocks we have so that when we unmount we don't account
146984e1e99fSDavid Chinner 	 * the reserved free space as used. This is really only necessary for
147084e1e99fSDavid Chinner 	 * lazy superblock counting because it trusts the incore superblock
14719da096fdSMalcolm Parsons 	 * counters to be absolutely correct on clean unmount.
147284e1e99fSDavid Chinner 	 *
147384e1e99fSDavid Chinner 	 * We don't bother correcting this elsewhere for lazy superblock
147484e1e99fSDavid Chinner 	 * counting because on mount of an unclean filesystem we reconstruct the
147584e1e99fSDavid Chinner 	 * correct counter value and this is irrelevant.
147684e1e99fSDavid Chinner 	 *
147784e1e99fSDavid Chinner 	 * For non-lazy counter filesystems, this doesn't matter at all because
147884e1e99fSDavid Chinner 	 * we only every apply deltas to the superblock and hence the incore
147984e1e99fSDavid Chinner 	 * value does not matter....
148084e1e99fSDavid Chinner 	 */
148184e1e99fSDavid Chinner 	resblks = 0;
1482714082bcSDavid Chinner 	error = xfs_reserve_blocks(mp, &resblks, NULL);
1483714082bcSDavid Chinner 	if (error)
14840b932cccSDave Chinner 		xfs_warn(mp, "Unable to free reserved block pool. "
1485714082bcSDavid Chinner 				"Freespace may not be correct on next mount.");
1486714082bcSDavid Chinner 
1487adab0f67SChandra Seetharaman 	error = xfs_log_sbcount(mp);
1488e5720eecSDavid Chinner 	if (error)
14890b932cccSDave Chinner 		xfs_warn(mp, "Unable to update superblock counters. "
1490e5720eecSDavid Chinner 				"Freespace may not be correct on next mount.");
149187c7bec7SChristoph Hellwig 
149287c7bec7SChristoph Hellwig 	/*
1493211e4d43SChristoph Hellwig 	 * At this point we might have modified the superblock again and thus
1494211e4d43SChristoph Hellwig 	 * added an item to the AIL, thus flush it again.
149587c7bec7SChristoph Hellwig 	 */
1496211e4d43SChristoph Hellwig 	xfs_ail_push_all_sync(mp->m_ail);
149787c7bec7SChristoph Hellwig 	xfs_wait_buftarg(mp->m_ddev_targp);
149887c7bec7SChristoph Hellwig 
14999a57fa8eSMark Tinguely 	/*
15009a57fa8eSMark Tinguely 	 * The superblock buffer is uncached and xfsaild_push() will lock and
15019a57fa8eSMark Tinguely 	 * set the XBF_ASYNC flag on the buffer. We cannot do xfs_buf_iowait()
15029a57fa8eSMark Tinguely 	 * here but a lock on the superblock buffer will block until iodone()
15039a57fa8eSMark Tinguely 	 * has completed.
15049a57fa8eSMark Tinguely 	 */
15059a57fa8eSMark Tinguely 	xfs_buf_lock(mp->m_sb_bp);
15069a57fa8eSMark Tinguely 	xfs_buf_unlock(mp->m_sb_bp);
15079a57fa8eSMark Tinguely 
150821b699c8SChristoph Hellwig 	xfs_log_unmount_write(mp);
150921b699c8SChristoph Hellwig 	xfs_log_unmount(mp);
151027174203SChristoph Hellwig 	xfs_uuid_unmount(mp);
15111da177e4SLinus Torvalds 
15121550d0b0SChristoph Hellwig #if defined(DEBUG)
15130ce4cfd4SChristoph Hellwig 	xfs_errortag_clearall(mp, 0);
15141da177e4SLinus Torvalds #endif
1515ff4f038cSChristoph Hellwig 	xfs_free_perag(mp);
15161da177e4SLinus Torvalds }
15171da177e4SLinus Torvalds 
15181da177e4SLinus Torvalds int
151992821e2bSDavid Chinner xfs_fs_writable(xfs_mount_t *mp)
152092821e2bSDavid Chinner {
1521d9457dc0SJan Kara 	return !(mp->m_super->s_writers.frozen || XFS_FORCED_SHUTDOWN(mp) ||
1522bd186aa9SChristoph Hellwig 		(mp->m_flags & XFS_MOUNT_RDONLY));
152392821e2bSDavid Chinner }
152492821e2bSDavid Chinner 
152592821e2bSDavid Chinner /*
1526b2ce3974SAlex Elder  * xfs_log_sbcount
1527b2ce3974SAlex Elder  *
1528adab0f67SChandra Seetharaman  * Sync the superblock counters to disk.
1529b2ce3974SAlex Elder  *
1530b2ce3974SAlex Elder  * Note this code can be called during the process of freezing, so
1531adab0f67SChandra Seetharaman  * we may need to use the transaction allocator which does not
1532b2ce3974SAlex Elder  * block when the transaction subsystem is in its frozen state.
153392821e2bSDavid Chinner  */
153492821e2bSDavid Chinner int
1535adab0f67SChandra Seetharaman xfs_log_sbcount(xfs_mount_t *mp)
153692821e2bSDavid Chinner {
153792821e2bSDavid Chinner 	xfs_trans_t	*tp;
153892821e2bSDavid Chinner 	int		error;
153992821e2bSDavid Chinner 
154092821e2bSDavid Chinner 	if (!xfs_fs_writable(mp))
154192821e2bSDavid Chinner 		return 0;
154292821e2bSDavid Chinner 
1543d4d90b57SChristoph Hellwig 	xfs_icsb_sync_counters(mp, 0);
154492821e2bSDavid Chinner 
154592821e2bSDavid Chinner 	/*
154692821e2bSDavid Chinner 	 * we don't need to do this if we are updating the superblock
154792821e2bSDavid Chinner 	 * counters on every modification.
154892821e2bSDavid Chinner 	 */
154992821e2bSDavid Chinner 	if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
155092821e2bSDavid Chinner 		return 0;
155192821e2bSDavid Chinner 
1552b2ce3974SAlex Elder 	tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT, KM_SLEEP);
155392821e2bSDavid Chinner 	error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
155492821e2bSDavid Chinner 					XFS_DEFAULT_LOG_COUNT);
155592821e2bSDavid Chinner 	if (error) {
155692821e2bSDavid Chinner 		xfs_trans_cancel(tp, 0);
155792821e2bSDavid Chinner 		return error;
155892821e2bSDavid Chinner 	}
155992821e2bSDavid Chinner 
156092821e2bSDavid Chinner 	xfs_mod_sb(tp, XFS_SB_IFREE | XFS_SB_ICOUNT | XFS_SB_FDBLOCKS);
156192821e2bSDavid Chinner 	xfs_trans_set_sync(tp);
1562e5720eecSDavid Chinner 	error = xfs_trans_commit(tp, 0);
1563e5720eecSDavid Chinner 	return error;
156492821e2bSDavid Chinner }
156592821e2bSDavid Chinner 
15661da177e4SLinus Torvalds /*
15671da177e4SLinus Torvalds  * xfs_mod_sb() can be used to copy arbitrary changes to the
15681da177e4SLinus Torvalds  * in-core superblock into the superblock buffer to be logged.
15691da177e4SLinus Torvalds  * It does not provide the higher level of locking that is
15701da177e4SLinus Torvalds  * needed to protect the in-core superblock from concurrent
15711da177e4SLinus Torvalds  * access.
15721da177e4SLinus Torvalds  */
15731da177e4SLinus Torvalds void
15741da177e4SLinus Torvalds xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
15751da177e4SLinus Torvalds {
15761da177e4SLinus Torvalds 	xfs_buf_t	*bp;
15771da177e4SLinus Torvalds 	int		first;
15781da177e4SLinus Torvalds 	int		last;
15791da177e4SLinus Torvalds 	xfs_mount_t	*mp;
15801da177e4SLinus Torvalds 	xfs_sb_field_t	f;
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 	ASSERT(fields);
15831da177e4SLinus Torvalds 	if (!fields)
15841da177e4SLinus Torvalds 		return;
15851da177e4SLinus Torvalds 	mp = tp->t_mountp;
15861da177e4SLinus Torvalds 	bp = xfs_trans_getsb(tp, mp, 0);
15871da177e4SLinus Torvalds 	first = sizeof(xfs_sb_t);
15881da177e4SLinus Torvalds 	last = 0;
15891da177e4SLinus Torvalds 
15901da177e4SLinus Torvalds 	/* translate/copy */
15911da177e4SLinus Torvalds 
15922bdf7cd0SChristoph Hellwig 	xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb, fields);
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds 	/* find modified range */
1595587aa0feSDave Chinner 	f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
1596587aa0feSDave Chinner 	ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1597587aa0feSDave Chinner 	last = xfs_sb_info[f + 1].offset - 1;
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds 	f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
16001da177e4SLinus Torvalds 	ASSERT((1LL << f) & XFS_SB_MOD_BITS);
16011da177e4SLinus Torvalds 	first = xfs_sb_info[f].offset;
16021da177e4SLinus Torvalds 
16031da177e4SLinus Torvalds 	xfs_trans_log_buf(tp, bp, first, last);
16041da177e4SLinus Torvalds }
1605d210a28cSYingping Lu 
1606d210a28cSYingping Lu 
16071da177e4SLinus Torvalds /*
16081da177e4SLinus Torvalds  * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
16091da177e4SLinus Torvalds  * a delta to a specified field in the in-core superblock.  Simply
16101da177e4SLinus Torvalds  * switch on the field indicated and apply the delta to that field.
16111da177e4SLinus Torvalds  * Fields are not allowed to dip below zero, so if the delta would
16121da177e4SLinus Torvalds  * do this do not apply it and return EINVAL.
16131da177e4SLinus Torvalds  *
16143685c2a1SEric Sandeen  * The m_sb_lock must be held when this routine is called.
16151da177e4SLinus Torvalds  */
1616d96f8f89SEric Sandeen STATIC int
161720f4ebf2SDavid Chinner xfs_mod_incore_sb_unlocked(
161820f4ebf2SDavid Chinner 	xfs_mount_t	*mp,
161920f4ebf2SDavid Chinner 	xfs_sb_field_t	field,
162020f4ebf2SDavid Chinner 	int64_t		delta,
162120f4ebf2SDavid Chinner 	int		rsvd)
16221da177e4SLinus Torvalds {
16231da177e4SLinus Torvalds 	int		scounter;	/* short counter for 32 bit fields */
16241da177e4SLinus Torvalds 	long long	lcounter;	/* long counter for 64 bit fields */
16251da177e4SLinus Torvalds 	long long	res_used, rem;
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds 	/*
16281da177e4SLinus Torvalds 	 * With the in-core superblock spin lock held, switch
16291da177e4SLinus Torvalds 	 * on the indicated field.  Apply the delta to the
16301da177e4SLinus Torvalds 	 * proper field.  If the fields value would dip below
16311da177e4SLinus Torvalds 	 * 0, then do not apply the delta and return EINVAL.
16321da177e4SLinus Torvalds 	 */
16331da177e4SLinus Torvalds 	switch (field) {
16341da177e4SLinus Torvalds 	case XFS_SBS_ICOUNT:
16351da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_icount;
16361da177e4SLinus Torvalds 		lcounter += delta;
16371da177e4SLinus Torvalds 		if (lcounter < 0) {
16381da177e4SLinus Torvalds 			ASSERT(0);
1639014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
16401da177e4SLinus Torvalds 		}
16411da177e4SLinus Torvalds 		mp->m_sb.sb_icount = lcounter;
1642014c2544SJesper Juhl 		return 0;
16431da177e4SLinus Torvalds 	case XFS_SBS_IFREE:
16441da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_ifree;
16451da177e4SLinus Torvalds 		lcounter += delta;
16461da177e4SLinus Torvalds 		if (lcounter < 0) {
16471da177e4SLinus Torvalds 			ASSERT(0);
1648014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
16491da177e4SLinus Torvalds 		}
16501da177e4SLinus Torvalds 		mp->m_sb.sb_ifree = lcounter;
1651014c2544SJesper Juhl 		return 0;
16521da177e4SLinus Torvalds 	case XFS_SBS_FDBLOCKS:
16534be536deSDavid Chinner 		lcounter = (long long)
16544be536deSDavid Chinner 			mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
16551da177e4SLinus Torvalds 		res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds 		if (delta > 0) {		/* Putting blocks back */
16581da177e4SLinus Torvalds 			if (res_used > delta) {
16591da177e4SLinus Torvalds 				mp->m_resblks_avail += delta;
16601da177e4SLinus Torvalds 			} else {
16611da177e4SLinus Torvalds 				rem = delta - res_used;
16621da177e4SLinus Torvalds 				mp->m_resblks_avail = mp->m_resblks;
16631da177e4SLinus Torvalds 				lcounter += rem;
16641da177e4SLinus Torvalds 			}
16651da177e4SLinus Torvalds 		} else {				/* Taking blocks away */
16661da177e4SLinus Torvalds 			lcounter += delta;
16678babd8a2SDave Chinner 			if (lcounter >= 0) {
16688babd8a2SDave Chinner 				mp->m_sb.sb_fdblocks = lcounter +
16698babd8a2SDave Chinner 							XFS_ALLOC_SET_ASIDE(mp);
16708babd8a2SDave Chinner 				return 0;
16718babd8a2SDave Chinner 			}
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 			/*
16748babd8a2SDave Chinner 			 * We are out of blocks, use any available reserved
16758babd8a2SDave Chinner 			 * blocks if were allowed to.
16761da177e4SLinus Torvalds 			 */
16778babd8a2SDave Chinner 			if (!rsvd)
1678014c2544SJesper Juhl 				return XFS_ERROR(ENOSPC);
16798babd8a2SDave Chinner 
16808babd8a2SDave Chinner 			lcounter = (long long)mp->m_resblks_avail + delta;
16818babd8a2SDave Chinner 			if (lcounter >= 0) {
16821da177e4SLinus Torvalds 				mp->m_resblks_avail = lcounter;
1683014c2544SJesper Juhl 				return 0;
16848babd8a2SDave Chinner 			}
16858babd8a2SDave Chinner 			printk_once(KERN_WARNING
16868babd8a2SDave Chinner 				"Filesystem \"%s\": reserve blocks depleted! "
16878babd8a2SDave Chinner 				"Consider increasing reserve pool size.",
16888babd8a2SDave Chinner 				mp->m_fsname);
1689014c2544SJesper Juhl 			return XFS_ERROR(ENOSPC);
16901da177e4SLinus Torvalds 		}
16911da177e4SLinus Torvalds 
16924be536deSDavid Chinner 		mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
1693014c2544SJesper Juhl 		return 0;
16941da177e4SLinus Torvalds 	case XFS_SBS_FREXTENTS:
16951da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_frextents;
16961da177e4SLinus Torvalds 		lcounter += delta;
16971da177e4SLinus Torvalds 		if (lcounter < 0) {
1698014c2544SJesper Juhl 			return XFS_ERROR(ENOSPC);
16991da177e4SLinus Torvalds 		}
17001da177e4SLinus Torvalds 		mp->m_sb.sb_frextents = lcounter;
1701014c2544SJesper Juhl 		return 0;
17021da177e4SLinus Torvalds 	case XFS_SBS_DBLOCKS:
17031da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_dblocks;
17041da177e4SLinus Torvalds 		lcounter += delta;
17051da177e4SLinus Torvalds 		if (lcounter < 0) {
17061da177e4SLinus Torvalds 			ASSERT(0);
1707014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
17081da177e4SLinus Torvalds 		}
17091da177e4SLinus Torvalds 		mp->m_sb.sb_dblocks = lcounter;
1710014c2544SJesper Juhl 		return 0;
17111da177e4SLinus Torvalds 	case XFS_SBS_AGCOUNT:
17121da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_agcount;
17131da177e4SLinus Torvalds 		scounter += delta;
17141da177e4SLinus Torvalds 		if (scounter < 0) {
17151da177e4SLinus Torvalds 			ASSERT(0);
1716014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
17171da177e4SLinus Torvalds 		}
17181da177e4SLinus Torvalds 		mp->m_sb.sb_agcount = scounter;
1719014c2544SJesper Juhl 		return 0;
17201da177e4SLinus Torvalds 	case XFS_SBS_IMAX_PCT:
17211da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_imax_pct;
17221da177e4SLinus Torvalds 		scounter += delta;
17231da177e4SLinus Torvalds 		if (scounter < 0) {
17241da177e4SLinus Torvalds 			ASSERT(0);
1725014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
17261da177e4SLinus Torvalds 		}
17271da177e4SLinus Torvalds 		mp->m_sb.sb_imax_pct = scounter;
1728014c2544SJesper Juhl 		return 0;
17291da177e4SLinus Torvalds 	case XFS_SBS_REXTSIZE:
17301da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rextsize;
17311da177e4SLinus Torvalds 		scounter += delta;
17321da177e4SLinus Torvalds 		if (scounter < 0) {
17331da177e4SLinus Torvalds 			ASSERT(0);
1734014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
17351da177e4SLinus Torvalds 		}
17361da177e4SLinus Torvalds 		mp->m_sb.sb_rextsize = scounter;
1737014c2544SJesper Juhl 		return 0;
17381da177e4SLinus Torvalds 	case XFS_SBS_RBMBLOCKS:
17391da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rbmblocks;
17401da177e4SLinus Torvalds 		scounter += delta;
17411da177e4SLinus Torvalds 		if (scounter < 0) {
17421da177e4SLinus Torvalds 			ASSERT(0);
1743014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
17441da177e4SLinus Torvalds 		}
17451da177e4SLinus Torvalds 		mp->m_sb.sb_rbmblocks = scounter;
1746014c2544SJesper Juhl 		return 0;
17471da177e4SLinus Torvalds 	case XFS_SBS_RBLOCKS:
17481da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_rblocks;
17491da177e4SLinus Torvalds 		lcounter += delta;
17501da177e4SLinus Torvalds 		if (lcounter < 0) {
17511da177e4SLinus Torvalds 			ASSERT(0);
1752014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
17531da177e4SLinus Torvalds 		}
17541da177e4SLinus Torvalds 		mp->m_sb.sb_rblocks = lcounter;
1755014c2544SJesper Juhl 		return 0;
17561da177e4SLinus Torvalds 	case XFS_SBS_REXTENTS:
17571da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_rextents;
17581da177e4SLinus Torvalds 		lcounter += delta;
17591da177e4SLinus Torvalds 		if (lcounter < 0) {
17601da177e4SLinus Torvalds 			ASSERT(0);
1761014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
17621da177e4SLinus Torvalds 		}
17631da177e4SLinus Torvalds 		mp->m_sb.sb_rextents = lcounter;
1764014c2544SJesper Juhl 		return 0;
17651da177e4SLinus Torvalds 	case XFS_SBS_REXTSLOG:
17661da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rextslog;
17671da177e4SLinus Torvalds 		scounter += delta;
17681da177e4SLinus Torvalds 		if (scounter < 0) {
17691da177e4SLinus Torvalds 			ASSERT(0);
1770014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
17711da177e4SLinus Torvalds 		}
17721da177e4SLinus Torvalds 		mp->m_sb.sb_rextslog = scounter;
1773014c2544SJesper Juhl 		return 0;
17741da177e4SLinus Torvalds 	default:
17751da177e4SLinus Torvalds 		ASSERT(0);
1776014c2544SJesper Juhl 		return XFS_ERROR(EINVAL);
17771da177e4SLinus Torvalds 	}
17781da177e4SLinus Torvalds }
17791da177e4SLinus Torvalds 
17801da177e4SLinus Torvalds /*
17811da177e4SLinus Torvalds  * xfs_mod_incore_sb() is used to change a field in the in-core
17821da177e4SLinus Torvalds  * superblock structure by the specified delta.  This modification
17833685c2a1SEric Sandeen  * is protected by the m_sb_lock.  Just use the xfs_mod_incore_sb_unlocked()
17841da177e4SLinus Torvalds  * routine to do the work.
17851da177e4SLinus Torvalds  */
17861da177e4SLinus Torvalds int
178720f4ebf2SDavid Chinner xfs_mod_incore_sb(
178896540c78SChristoph Hellwig 	struct xfs_mount	*mp,
178920f4ebf2SDavid Chinner 	xfs_sb_field_t		field,
179020f4ebf2SDavid Chinner 	int64_t			delta,
179120f4ebf2SDavid Chinner 	int			rsvd)
17921da177e4SLinus Torvalds {
17931da177e4SLinus Torvalds 	int			status;
17941da177e4SLinus Torvalds 
17958d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
179696540c78SChristoph Hellwig 	ASSERT(field < XFS_SBS_ICOUNT || field > XFS_SBS_FDBLOCKS);
17978d280b98SDavid Chinner #endif
17983685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
17991da177e4SLinus Torvalds 	status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
18003685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
18018d280b98SDavid Chinner 
1802014c2544SJesper Juhl 	return status;
18031da177e4SLinus Torvalds }
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds /*
18061b040712SChristoph Hellwig  * Change more than one field in the in-core superblock structure at a time.
18071da177e4SLinus Torvalds  *
18081b040712SChristoph Hellwig  * The fields and changes to those fields are specified in the array of
18091b040712SChristoph Hellwig  * xfs_mod_sb structures passed in.  Either all of the specified deltas
18101b040712SChristoph Hellwig  * will be applied or none of them will.  If any modified field dips below 0,
18111b040712SChristoph Hellwig  * then all modifications will be backed out and EINVAL will be returned.
18121b040712SChristoph Hellwig  *
18131b040712SChristoph Hellwig  * Note that this function may not be used for the superblock values that
18141b040712SChristoph Hellwig  * are tracked with the in-memory per-cpu counters - a direct call to
18151b040712SChristoph Hellwig  * xfs_icsb_modify_counters is required for these.
18161da177e4SLinus Torvalds  */
18171da177e4SLinus Torvalds int
18181b040712SChristoph Hellwig xfs_mod_incore_sb_batch(
18191b040712SChristoph Hellwig 	struct xfs_mount	*mp,
18201b040712SChristoph Hellwig 	xfs_mod_sb_t		*msb,
18211b040712SChristoph Hellwig 	uint			nmsb,
18221b040712SChristoph Hellwig 	int			rsvd)
18231da177e4SLinus Torvalds {
182445c51b99SDavid Sterba 	xfs_mod_sb_t		*msbp;
18251b040712SChristoph Hellwig 	int			error = 0;
18261da177e4SLinus Torvalds 
18271da177e4SLinus Torvalds 	/*
18281b040712SChristoph Hellwig 	 * Loop through the array of mod structures and apply each individually.
18291b040712SChristoph Hellwig 	 * If any fail, then back out all those which have already been applied.
18301b040712SChristoph Hellwig 	 * Do all of this within the scope of the m_sb_lock so that all of the
18311b040712SChristoph Hellwig 	 * changes will be atomic.
18321da177e4SLinus Torvalds 	 */
18333685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
183445c51b99SDavid Sterba 	for (msbp = msb; msbp < (msb + nmsb); msbp++) {
18351b040712SChristoph Hellwig 		ASSERT(msbp->msb_field < XFS_SBS_ICOUNT ||
18361b040712SChristoph Hellwig 		       msbp->msb_field > XFS_SBS_FDBLOCKS);
18378d280b98SDavid Chinner 
18381b040712SChristoph Hellwig 		error = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
18391b040712SChristoph Hellwig 						   msbp->msb_delta, rsvd);
18401b040712SChristoph Hellwig 		if (error)
18411b040712SChristoph Hellwig 			goto unwind;
18421da177e4SLinus Torvalds 	}
18431b040712SChristoph Hellwig 	spin_unlock(&mp->m_sb_lock);
18441b040712SChristoph Hellwig 	return 0;
18451da177e4SLinus Torvalds 
18461b040712SChristoph Hellwig unwind:
18471b040712SChristoph Hellwig 	while (--msbp >= msb) {
18481b040712SChristoph Hellwig 		error = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
18491b040712SChristoph Hellwig 						   -msbp->msb_delta, rsvd);
18501b040712SChristoph Hellwig 		ASSERT(error == 0);
18511da177e4SLinus Torvalds 	}
18523685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
18531b040712SChristoph Hellwig 	return error;
18541da177e4SLinus Torvalds }
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds /*
18571da177e4SLinus Torvalds  * xfs_getsb() is called to obtain the buffer for the superblock.
18581da177e4SLinus Torvalds  * The buffer is returned locked and read in from disk.
18591da177e4SLinus Torvalds  * The buffer should be released with a call to xfs_brelse().
18601da177e4SLinus Torvalds  *
18611da177e4SLinus Torvalds  * If the flags parameter is BUF_TRYLOCK, then we'll only return
18621da177e4SLinus Torvalds  * the superblock buffer if it can be locked without sleeping.
18631da177e4SLinus Torvalds  * If it can't then we'll return NULL.
18641da177e4SLinus Torvalds  */
18650c842ad4SChristoph Hellwig struct xfs_buf *
18661da177e4SLinus Torvalds xfs_getsb(
18670c842ad4SChristoph Hellwig 	struct xfs_mount	*mp,
18681da177e4SLinus Torvalds 	int			flags)
18691da177e4SLinus Torvalds {
18700c842ad4SChristoph Hellwig 	struct xfs_buf		*bp = mp->m_sb_bp;
18711da177e4SLinus Torvalds 
18720c842ad4SChristoph Hellwig 	if (!xfs_buf_trylock(bp)) {
18730c842ad4SChristoph Hellwig 		if (flags & XBF_TRYLOCK)
18741da177e4SLinus Torvalds 			return NULL;
18750c842ad4SChristoph Hellwig 		xfs_buf_lock(bp);
18761da177e4SLinus Torvalds 	}
18770c842ad4SChristoph Hellwig 
187872790aa1SChandra Seetharaman 	xfs_buf_hold(bp);
18791da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISDONE(bp));
1880014c2544SJesper Juhl 	return bp;
18811da177e4SLinus Torvalds }
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds /*
18841da177e4SLinus Torvalds  * Used to free the superblock along various error paths.
18851da177e4SLinus Torvalds  */
18861da177e4SLinus Torvalds void
18871da177e4SLinus Torvalds xfs_freesb(
188826af6552SDave Chinner 	struct xfs_mount	*mp)
18891da177e4SLinus Torvalds {
189026af6552SDave Chinner 	struct xfs_buf		*bp = mp->m_sb_bp;
18911da177e4SLinus Torvalds 
189226af6552SDave Chinner 	xfs_buf_lock(bp);
18931da177e4SLinus Torvalds 	mp->m_sb_bp = NULL;
189426af6552SDave Chinner 	xfs_buf_relse(bp);
18951da177e4SLinus Torvalds }
18961da177e4SLinus Torvalds 
18971da177e4SLinus Torvalds /*
18981da177e4SLinus Torvalds  * Used to log changes to the superblock unit and width fields which could
1899e6957ea4SEric Sandeen  * be altered by the mount options, as well as any potential sb_features2
1900e6957ea4SEric Sandeen  * fixup. Only the first superblock is updated.
19011da177e4SLinus Torvalds  */
19027884bc86SChristoph Hellwig int
1903ee1c0908SDavid Chinner xfs_mount_log_sb(
19041da177e4SLinus Torvalds 	xfs_mount_t	*mp,
19051da177e4SLinus Torvalds 	__int64_t	fields)
19061da177e4SLinus Torvalds {
19071da177e4SLinus Torvalds 	xfs_trans_t	*tp;
1908e5720eecSDavid Chinner 	int		error;
19091da177e4SLinus Torvalds 
1910ee1c0908SDavid Chinner 	ASSERT(fields & (XFS_SB_UNIT | XFS_SB_WIDTH | XFS_SB_UUID |
19114b166de0SDavid Chinner 			 XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2 |
19124b166de0SDavid Chinner 			 XFS_SB_VERSIONNUM));
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds 	tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
1915e5720eecSDavid Chinner 	error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
1916e5720eecSDavid Chinner 				XFS_DEFAULT_LOG_COUNT);
1917e5720eecSDavid Chinner 	if (error) {
19181da177e4SLinus Torvalds 		xfs_trans_cancel(tp, 0);
1919e5720eecSDavid Chinner 		return error;
19201da177e4SLinus Torvalds 	}
19211da177e4SLinus Torvalds 	xfs_mod_sb(tp, fields);
1922e5720eecSDavid Chinner 	error = xfs_trans_commit(tp, 0);
1923e5720eecSDavid Chinner 	return error;
19241da177e4SLinus Torvalds }
19258d280b98SDavid Chinner 
1926dda35b8fSChristoph Hellwig /*
1927dda35b8fSChristoph Hellwig  * If the underlying (data/log/rt) device is readonly, there are some
1928dda35b8fSChristoph Hellwig  * operations that cannot proceed.
1929dda35b8fSChristoph Hellwig  */
1930dda35b8fSChristoph Hellwig int
1931dda35b8fSChristoph Hellwig xfs_dev_is_read_only(
1932dda35b8fSChristoph Hellwig 	struct xfs_mount	*mp,
1933dda35b8fSChristoph Hellwig 	char			*message)
1934dda35b8fSChristoph Hellwig {
1935dda35b8fSChristoph Hellwig 	if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
1936dda35b8fSChristoph Hellwig 	    xfs_readonly_buftarg(mp->m_logdev_targp) ||
1937dda35b8fSChristoph Hellwig 	    (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
19380b932cccSDave Chinner 		xfs_notice(mp, "%s required on read-only device.", message);
19390b932cccSDave Chinner 		xfs_notice(mp, "write access unavailable, cannot proceed.");
1940dda35b8fSChristoph Hellwig 		return EROFS;
1941dda35b8fSChristoph Hellwig 	}
1942dda35b8fSChristoph Hellwig 	return 0;
1943dda35b8fSChristoph Hellwig }
19448d280b98SDavid Chinner 
19458d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
19468d280b98SDavid Chinner /*
19478d280b98SDavid Chinner  * Per-cpu incore superblock counters
19488d280b98SDavid Chinner  *
19498d280b98SDavid Chinner  * Simple concept, difficult implementation
19508d280b98SDavid Chinner  *
19518d280b98SDavid Chinner  * Basically, replace the incore superblock counters with a distributed per cpu
19528d280b98SDavid Chinner  * counter for contended fields (e.g.  free block count).
19538d280b98SDavid Chinner  *
19548d280b98SDavid Chinner  * Difficulties arise in that the incore sb is used for ENOSPC checking, and
19558d280b98SDavid Chinner  * hence needs to be accurately read when we are running low on space. Hence
19568d280b98SDavid Chinner  * there is a method to enable and disable the per-cpu counters based on how
19578d280b98SDavid Chinner  * much "stuff" is available in them.
19588d280b98SDavid Chinner  *
19598d280b98SDavid Chinner  * Basically, a counter is enabled if there is enough free resource to justify
19608d280b98SDavid Chinner  * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
19618d280b98SDavid Chinner  * ENOSPC), then we disable the counters to synchronise all callers and
19628d280b98SDavid Chinner  * re-distribute the available resources.
19638d280b98SDavid Chinner  *
19648d280b98SDavid Chinner  * If, once we redistributed the available resources, we still get a failure,
19658d280b98SDavid Chinner  * we disable the per-cpu counter and go through the slow path.
19668d280b98SDavid Chinner  *
19678d280b98SDavid Chinner  * The slow path is the current xfs_mod_incore_sb() function.  This means that
19689da096fdSMalcolm Parsons  * when we disable a per-cpu counter, we need to drain its resources back to
19698d280b98SDavid Chinner  * the global superblock. We do this after disabling the counter to prevent
19708d280b98SDavid Chinner  * more threads from queueing up on the counter.
19718d280b98SDavid Chinner  *
19728d280b98SDavid Chinner  * Essentially, this means that we still need a lock in the fast path to enable
19738d280b98SDavid Chinner  * synchronisation between the global counters and the per-cpu counters. This
19748d280b98SDavid Chinner  * is not a problem because the lock will be local to a CPU almost all the time
19758d280b98SDavid Chinner  * and have little contention except when we get to ENOSPC conditions.
19768d280b98SDavid Chinner  *
19778d280b98SDavid Chinner  * Basically, this lock becomes a barrier that enables us to lock out the fast
19788d280b98SDavid Chinner  * path while we do things like enabling and disabling counters and
19798d280b98SDavid Chinner  * synchronising the counters.
19808d280b98SDavid Chinner  *
19818d280b98SDavid Chinner  * Locking rules:
19828d280b98SDavid Chinner  *
19833685c2a1SEric Sandeen  * 	1. m_sb_lock before picking up per-cpu locks
19848d280b98SDavid Chinner  * 	2. per-cpu locks always picked up via for_each_online_cpu() order
19853685c2a1SEric Sandeen  * 	3. accurate counter sync requires m_sb_lock + per cpu locks
19868d280b98SDavid Chinner  * 	4. modifying per-cpu counters requires holding per-cpu lock
19873685c2a1SEric Sandeen  * 	5. modifying global counters requires holding m_sb_lock
19883685c2a1SEric Sandeen  *	6. enabling or disabling a counter requires holding the m_sb_lock
19898d280b98SDavid Chinner  *	   and _none_ of the per-cpu locks.
19908d280b98SDavid Chinner  *
19918d280b98SDavid Chinner  * Disabled counters are only ever re-enabled by a balance operation
19928d280b98SDavid Chinner  * that results in more free resources per CPU than a given threshold.
19938d280b98SDavid Chinner  * To ensure counters don't remain disabled, they are rebalanced when
19948d280b98SDavid Chinner  * the global resource goes above a higher threshold (i.e. some hysteresis
19958d280b98SDavid Chinner  * is present to prevent thrashing).
19968d280b98SDavid Chinner  */
1997e8234a68SDavid Chinner 
19985a67e4c5SChandra Seetharaman #ifdef CONFIG_HOTPLUG_CPU
1999e8234a68SDavid Chinner /*
2000e8234a68SDavid Chinner  * hot-plug CPU notifier support.
2001e8234a68SDavid Chinner  *
20025a67e4c5SChandra Seetharaman  * We need a notifier per filesystem as we need to be able to identify
20035a67e4c5SChandra Seetharaman  * the filesystem to balance the counters out. This is achieved by
20045a67e4c5SChandra Seetharaman  * having a notifier block embedded in the xfs_mount_t and doing pointer
20055a67e4c5SChandra Seetharaman  * magic to get the mount pointer from the notifier block address.
2006e8234a68SDavid Chinner  */
2007e8234a68SDavid Chinner STATIC int
2008e8234a68SDavid Chinner xfs_icsb_cpu_notify(
2009e8234a68SDavid Chinner 	struct notifier_block *nfb,
2010e8234a68SDavid Chinner 	unsigned long action,
2011e8234a68SDavid Chinner 	void *hcpu)
2012e8234a68SDavid Chinner {
2013e8234a68SDavid Chinner 	xfs_icsb_cnts_t *cntp;
2014e8234a68SDavid Chinner 	xfs_mount_t	*mp;
2015e8234a68SDavid Chinner 
2016e8234a68SDavid Chinner 	mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
2017e8234a68SDavid Chinner 	cntp = (xfs_icsb_cnts_t *)
2018e8234a68SDavid Chinner 			per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
2019e8234a68SDavid Chinner 	switch (action) {
2020e8234a68SDavid Chinner 	case CPU_UP_PREPARE:
20218bb78442SRafael J. Wysocki 	case CPU_UP_PREPARE_FROZEN:
2022e8234a68SDavid Chinner 		/* Easy Case - initialize the area and locks, and
2023e8234a68SDavid Chinner 		 * then rebalance when online does everything else for us. */
202401e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
2025e8234a68SDavid Chinner 		break;
2026e8234a68SDavid Chinner 	case CPU_ONLINE:
20278bb78442SRafael J. Wysocki 	case CPU_ONLINE_FROZEN:
202803135cf7SDavid Chinner 		xfs_icsb_lock(mp);
202945af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
203045af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
203145af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
203203135cf7SDavid Chinner 		xfs_icsb_unlock(mp);
2033e8234a68SDavid Chinner 		break;
2034e8234a68SDavid Chinner 	case CPU_DEAD:
20358bb78442SRafael J. Wysocki 	case CPU_DEAD_FROZEN:
2036e8234a68SDavid Chinner 		/* Disable all the counters, then fold the dead cpu's
2037e8234a68SDavid Chinner 		 * count into the total on the global superblock and
2038e8234a68SDavid Chinner 		 * re-enable the counters. */
203903135cf7SDavid Chinner 		xfs_icsb_lock(mp);
20403685c2a1SEric Sandeen 		spin_lock(&mp->m_sb_lock);
2041e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
2042e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
2043e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
2044e8234a68SDavid Chinner 
2045e8234a68SDavid Chinner 		mp->m_sb.sb_icount += cntp->icsb_icount;
2046e8234a68SDavid Chinner 		mp->m_sb.sb_ifree += cntp->icsb_ifree;
2047e8234a68SDavid Chinner 		mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
2048e8234a68SDavid Chinner 
204901e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
2050e8234a68SDavid Chinner 
205145af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter_locked(mp, XFS_SBS_ICOUNT, 0);
205245af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter_locked(mp, XFS_SBS_IFREE, 0);
205345af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter_locked(mp, XFS_SBS_FDBLOCKS, 0);
20543685c2a1SEric Sandeen 		spin_unlock(&mp->m_sb_lock);
205503135cf7SDavid Chinner 		xfs_icsb_unlock(mp);
2056e8234a68SDavid Chinner 		break;
2057e8234a68SDavid Chinner 	}
2058e8234a68SDavid Chinner 
2059e8234a68SDavid Chinner 	return NOTIFY_OK;
2060e8234a68SDavid Chinner }
20615a67e4c5SChandra Seetharaman #endif /* CONFIG_HOTPLUG_CPU */
2062e8234a68SDavid Chinner 
20638d280b98SDavid Chinner int
20648d280b98SDavid Chinner xfs_icsb_init_counters(
20658d280b98SDavid Chinner 	xfs_mount_t	*mp)
20668d280b98SDavid Chinner {
20678d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
20688d280b98SDavid Chinner 	int		i;
20698d280b98SDavid Chinner 
20708d280b98SDavid Chinner 	mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
20718d280b98SDavid Chinner 	if (mp->m_sb_cnts == NULL)
20728d280b98SDavid Chinner 		return -ENOMEM;
20738d280b98SDavid Chinner 
20745a67e4c5SChandra Seetharaman #ifdef CONFIG_HOTPLUG_CPU
2075e8234a68SDavid Chinner 	mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
2076e8234a68SDavid Chinner 	mp->m_icsb_notifier.priority = 0;
20775a67e4c5SChandra Seetharaman 	register_hotcpu_notifier(&mp->m_icsb_notifier);
20785a67e4c5SChandra Seetharaman #endif /* CONFIG_HOTPLUG_CPU */
2079e8234a68SDavid Chinner 
20808d280b98SDavid Chinner 	for_each_online_cpu(i) {
20818d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
208201e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
20838d280b98SDavid Chinner 	}
208420b64285SDavid Chinner 
208520b64285SDavid Chinner 	mutex_init(&mp->m_icsb_mutex);
208620b64285SDavid Chinner 
20878d280b98SDavid Chinner 	/*
20888d280b98SDavid Chinner 	 * start with all counters disabled so that the
20898d280b98SDavid Chinner 	 * initial balance kicks us off correctly
20908d280b98SDavid Chinner 	 */
20918d280b98SDavid Chinner 	mp->m_icsb_counters = -1;
20928d280b98SDavid Chinner 	return 0;
20938d280b98SDavid Chinner }
20948d280b98SDavid Chinner 
20955478eeadSLachlan McIlroy void
20965478eeadSLachlan McIlroy xfs_icsb_reinit_counters(
20975478eeadSLachlan McIlroy 	xfs_mount_t	*mp)
20985478eeadSLachlan McIlroy {
20995478eeadSLachlan McIlroy 	xfs_icsb_lock(mp);
21005478eeadSLachlan McIlroy 	/*
21015478eeadSLachlan McIlroy 	 * start with all counters disabled so that the
21025478eeadSLachlan McIlroy 	 * initial balance kicks us off correctly
21035478eeadSLachlan McIlroy 	 */
21045478eeadSLachlan McIlroy 	mp->m_icsb_counters = -1;
210545af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
210645af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
210745af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
21085478eeadSLachlan McIlroy 	xfs_icsb_unlock(mp);
21095478eeadSLachlan McIlroy }
21105478eeadSLachlan McIlroy 
2111c962fb79SChristoph Hellwig void
21128d280b98SDavid Chinner xfs_icsb_destroy_counters(
21138d280b98SDavid Chinner 	xfs_mount_t	*mp)
21148d280b98SDavid Chinner {
2115e8234a68SDavid Chinner 	if (mp->m_sb_cnts) {
21165a67e4c5SChandra Seetharaman 		unregister_hotcpu_notifier(&mp->m_icsb_notifier);
21178d280b98SDavid Chinner 		free_percpu(mp->m_sb_cnts);
21188d280b98SDavid Chinner 	}
211903135cf7SDavid Chinner 	mutex_destroy(&mp->m_icsb_mutex);
2120e8234a68SDavid Chinner }
21218d280b98SDavid Chinner 
2122b8f82a4aSChristoph Hellwig STATIC void
212301e1b69cSDavid Chinner xfs_icsb_lock_cntr(
212401e1b69cSDavid Chinner 	xfs_icsb_cnts_t	*icsbp)
212501e1b69cSDavid Chinner {
212601e1b69cSDavid Chinner 	while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
212701e1b69cSDavid Chinner 		ndelay(1000);
212801e1b69cSDavid Chinner 	}
212901e1b69cSDavid Chinner }
213001e1b69cSDavid Chinner 
2131b8f82a4aSChristoph Hellwig STATIC void
213201e1b69cSDavid Chinner xfs_icsb_unlock_cntr(
213301e1b69cSDavid Chinner 	xfs_icsb_cnts_t	*icsbp)
213401e1b69cSDavid Chinner {
213501e1b69cSDavid Chinner 	clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
213601e1b69cSDavid Chinner }
213701e1b69cSDavid Chinner 
21388d280b98SDavid Chinner 
2139b8f82a4aSChristoph Hellwig STATIC void
21408d280b98SDavid Chinner xfs_icsb_lock_all_counters(
21418d280b98SDavid Chinner 	xfs_mount_t	*mp)
21428d280b98SDavid Chinner {
21438d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
21448d280b98SDavid Chinner 	int		i;
21458d280b98SDavid Chinner 
21468d280b98SDavid Chinner 	for_each_online_cpu(i) {
21478d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
214801e1b69cSDavid Chinner 		xfs_icsb_lock_cntr(cntp);
21498d280b98SDavid Chinner 	}
21508d280b98SDavid Chinner }
21518d280b98SDavid Chinner 
2152b8f82a4aSChristoph Hellwig STATIC void
21538d280b98SDavid Chinner xfs_icsb_unlock_all_counters(
21548d280b98SDavid Chinner 	xfs_mount_t	*mp)
21558d280b98SDavid Chinner {
21568d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
21578d280b98SDavid Chinner 	int		i;
21588d280b98SDavid Chinner 
21598d280b98SDavid Chinner 	for_each_online_cpu(i) {
21608d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
216101e1b69cSDavid Chinner 		xfs_icsb_unlock_cntr(cntp);
21628d280b98SDavid Chinner 	}
21638d280b98SDavid Chinner }
21648d280b98SDavid Chinner 
21658d280b98SDavid Chinner STATIC void
21668d280b98SDavid Chinner xfs_icsb_count(
21678d280b98SDavid Chinner 	xfs_mount_t	*mp,
21688d280b98SDavid Chinner 	xfs_icsb_cnts_t	*cnt,
21698d280b98SDavid Chinner 	int		flags)
21708d280b98SDavid Chinner {
21718d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
21728d280b98SDavid Chinner 	int		i;
21738d280b98SDavid Chinner 
21748d280b98SDavid Chinner 	memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
21758d280b98SDavid Chinner 
21768d280b98SDavid Chinner 	if (!(flags & XFS_ICSB_LAZY_COUNT))
21778d280b98SDavid Chinner 		xfs_icsb_lock_all_counters(mp);
21788d280b98SDavid Chinner 
21798d280b98SDavid Chinner 	for_each_online_cpu(i) {
21808d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
21818d280b98SDavid Chinner 		cnt->icsb_icount += cntp->icsb_icount;
21828d280b98SDavid Chinner 		cnt->icsb_ifree += cntp->icsb_ifree;
21838d280b98SDavid Chinner 		cnt->icsb_fdblocks += cntp->icsb_fdblocks;
21848d280b98SDavid Chinner 	}
21858d280b98SDavid Chinner 
21868d280b98SDavid Chinner 	if (!(flags & XFS_ICSB_LAZY_COUNT))
21878d280b98SDavid Chinner 		xfs_icsb_unlock_all_counters(mp);
21888d280b98SDavid Chinner }
21898d280b98SDavid Chinner 
21908d280b98SDavid Chinner STATIC int
21918d280b98SDavid Chinner xfs_icsb_counter_disabled(
21928d280b98SDavid Chinner 	xfs_mount_t	*mp,
21938d280b98SDavid Chinner 	xfs_sb_field_t	field)
21948d280b98SDavid Chinner {
21958d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
21968d280b98SDavid Chinner 	return test_bit(field, &mp->m_icsb_counters);
21978d280b98SDavid Chinner }
21988d280b98SDavid Chinner 
219936fbe6e6SDavid Chinner STATIC void
22008d280b98SDavid Chinner xfs_icsb_disable_counter(
22018d280b98SDavid Chinner 	xfs_mount_t	*mp,
22028d280b98SDavid Chinner 	xfs_sb_field_t	field)
22038d280b98SDavid Chinner {
22048d280b98SDavid Chinner 	xfs_icsb_cnts_t	cnt;
22058d280b98SDavid Chinner 
22068d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
22078d280b98SDavid Chinner 
220820b64285SDavid Chinner 	/*
220920b64285SDavid Chinner 	 * If we are already disabled, then there is nothing to do
221020b64285SDavid Chinner 	 * here. We check before locking all the counters to avoid
221120b64285SDavid Chinner 	 * the expensive lock operation when being called in the
221220b64285SDavid Chinner 	 * slow path and the counter is already disabled. This is
221320b64285SDavid Chinner 	 * safe because the only time we set or clear this state is under
221420b64285SDavid Chinner 	 * the m_icsb_mutex.
221520b64285SDavid Chinner 	 */
221620b64285SDavid Chinner 	if (xfs_icsb_counter_disabled(mp, field))
221736fbe6e6SDavid Chinner 		return;
221820b64285SDavid Chinner 
22198d280b98SDavid Chinner 	xfs_icsb_lock_all_counters(mp);
22208d280b98SDavid Chinner 	if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
22218d280b98SDavid Chinner 		/* drain back to superblock */
22228d280b98SDavid Chinner 
2223ce46193bSChristoph Hellwig 		xfs_icsb_count(mp, &cnt, XFS_ICSB_LAZY_COUNT);
22248d280b98SDavid Chinner 		switch(field) {
22258d280b98SDavid Chinner 		case XFS_SBS_ICOUNT:
22268d280b98SDavid Chinner 			mp->m_sb.sb_icount = cnt.icsb_icount;
22278d280b98SDavid Chinner 			break;
22288d280b98SDavid Chinner 		case XFS_SBS_IFREE:
22298d280b98SDavid Chinner 			mp->m_sb.sb_ifree = cnt.icsb_ifree;
22308d280b98SDavid Chinner 			break;
22318d280b98SDavid Chinner 		case XFS_SBS_FDBLOCKS:
22328d280b98SDavid Chinner 			mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
22338d280b98SDavid Chinner 			break;
22348d280b98SDavid Chinner 		default:
22358d280b98SDavid Chinner 			BUG();
22368d280b98SDavid Chinner 		}
22378d280b98SDavid Chinner 	}
22388d280b98SDavid Chinner 
22398d280b98SDavid Chinner 	xfs_icsb_unlock_all_counters(mp);
22408d280b98SDavid Chinner }
22418d280b98SDavid Chinner 
22428d280b98SDavid Chinner STATIC void
22438d280b98SDavid Chinner xfs_icsb_enable_counter(
22448d280b98SDavid Chinner 	xfs_mount_t	*mp,
22458d280b98SDavid Chinner 	xfs_sb_field_t	field,
22468d280b98SDavid Chinner 	uint64_t	count,
22478d280b98SDavid Chinner 	uint64_t	resid)
22488d280b98SDavid Chinner {
22498d280b98SDavid Chinner 	xfs_icsb_cnts_t	*cntp;
22508d280b98SDavid Chinner 	int		i;
22518d280b98SDavid Chinner 
22528d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
22538d280b98SDavid Chinner 
22548d280b98SDavid Chinner 	xfs_icsb_lock_all_counters(mp);
22558d280b98SDavid Chinner 	for_each_online_cpu(i) {
22568d280b98SDavid Chinner 		cntp = per_cpu_ptr(mp->m_sb_cnts, i);
22578d280b98SDavid Chinner 		switch (field) {
22588d280b98SDavid Chinner 		case XFS_SBS_ICOUNT:
22598d280b98SDavid Chinner 			cntp->icsb_icount = count + resid;
22608d280b98SDavid Chinner 			break;
22618d280b98SDavid Chinner 		case XFS_SBS_IFREE:
22628d280b98SDavid Chinner 			cntp->icsb_ifree = count + resid;
22638d280b98SDavid Chinner 			break;
22648d280b98SDavid Chinner 		case XFS_SBS_FDBLOCKS:
22658d280b98SDavid Chinner 			cntp->icsb_fdblocks = count + resid;
22668d280b98SDavid Chinner 			break;
22678d280b98SDavid Chinner 		default:
22688d280b98SDavid Chinner 			BUG();
22698d280b98SDavid Chinner 			break;
22708d280b98SDavid Chinner 		}
22718d280b98SDavid Chinner 		resid = 0;
22728d280b98SDavid Chinner 	}
22738d280b98SDavid Chinner 	clear_bit(field, &mp->m_icsb_counters);
22748d280b98SDavid Chinner 	xfs_icsb_unlock_all_counters(mp);
22758d280b98SDavid Chinner }
22768d280b98SDavid Chinner 
2277dbcabad1SDavid Chinner void
2278d4d90b57SChristoph Hellwig xfs_icsb_sync_counters_locked(
22798d280b98SDavid Chinner 	xfs_mount_t	*mp,
22808d280b98SDavid Chinner 	int		flags)
22818d280b98SDavid Chinner {
22828d280b98SDavid Chinner 	xfs_icsb_cnts_t	cnt;
22838d280b98SDavid Chinner 
22848d280b98SDavid Chinner 	xfs_icsb_count(mp, &cnt, flags);
22858d280b98SDavid Chinner 
22868d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
22878d280b98SDavid Chinner 		mp->m_sb.sb_icount = cnt.icsb_icount;
22888d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
22898d280b98SDavid Chinner 		mp->m_sb.sb_ifree = cnt.icsb_ifree;
22908d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
22918d280b98SDavid Chinner 		mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
22928d280b98SDavid Chinner }
22938d280b98SDavid Chinner 
22948d280b98SDavid Chinner /*
22958d280b98SDavid Chinner  * Accurate update of per-cpu counters to incore superblock
22968d280b98SDavid Chinner  */
2297d4d90b57SChristoph Hellwig void
22988d280b98SDavid Chinner xfs_icsb_sync_counters(
2299d4d90b57SChristoph Hellwig 	xfs_mount_t	*mp,
2300d4d90b57SChristoph Hellwig 	int		flags)
23018d280b98SDavid Chinner {
2302d4d90b57SChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
2303d4d90b57SChristoph Hellwig 	xfs_icsb_sync_counters_locked(mp, flags);
2304d4d90b57SChristoph Hellwig 	spin_unlock(&mp->m_sb_lock);
23058d280b98SDavid Chinner }
23068d280b98SDavid Chinner 
23078d280b98SDavid Chinner /*
23088d280b98SDavid Chinner  * Balance and enable/disable counters as necessary.
23098d280b98SDavid Chinner  *
231020b64285SDavid Chinner  * Thresholds for re-enabling counters are somewhat magic.  inode counts are
231120b64285SDavid Chinner  * chosen to be the same number as single on disk allocation chunk per CPU, and
231220b64285SDavid Chinner  * free blocks is something far enough zero that we aren't going thrash when we
231320b64285SDavid Chinner  * get near ENOSPC. We also need to supply a minimum we require per cpu to
231420b64285SDavid Chinner  * prevent looping endlessly when xfs_alloc_space asks for more than will
231520b64285SDavid Chinner  * be distributed to a single CPU but each CPU has enough blocks to be
231620b64285SDavid Chinner  * reenabled.
231720b64285SDavid Chinner  *
231820b64285SDavid Chinner  * Note that we can be called when counters are already disabled.
231920b64285SDavid Chinner  * xfs_icsb_disable_counter() optimises the counter locking in this case to
232020b64285SDavid Chinner  * prevent locking every per-cpu counter needlessly.
23218d280b98SDavid Chinner  */
232220b64285SDavid Chinner 
232320b64285SDavid Chinner #define XFS_ICSB_INO_CNTR_REENABLE	(uint64_t)64
23244be536deSDavid Chinner #define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \
232520b64285SDavid Chinner 		(uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp))
23268d280b98SDavid Chinner STATIC void
232745af6c6dSChristoph Hellwig xfs_icsb_balance_counter_locked(
23288d280b98SDavid Chinner 	xfs_mount_t	*mp,
23298d280b98SDavid Chinner 	xfs_sb_field_t  field,
233020b64285SDavid Chinner 	int		min_per_cpu)
23318d280b98SDavid Chinner {
23326fdf8cccSNathan Scott 	uint64_t	count, resid;
23338d280b98SDavid Chinner 	int		weight = num_online_cpus();
233420b64285SDavid Chinner 	uint64_t	min = (uint64_t)min_per_cpu;
23358d280b98SDavid Chinner 
23368d280b98SDavid Chinner 	/* disable counter and sync counter */
23378d280b98SDavid Chinner 	xfs_icsb_disable_counter(mp, field);
23388d280b98SDavid Chinner 
23398d280b98SDavid Chinner 	/* update counters  - first CPU gets residual*/
23408d280b98SDavid Chinner 	switch (field) {
23418d280b98SDavid Chinner 	case XFS_SBS_ICOUNT:
23428d280b98SDavid Chinner 		count = mp->m_sb.sb_icount;
23438d280b98SDavid Chinner 		resid = do_div(count, weight);
234420b64285SDavid Chinner 		if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
234545af6c6dSChristoph Hellwig 			return;
23468d280b98SDavid Chinner 		break;
23478d280b98SDavid Chinner 	case XFS_SBS_IFREE:
23488d280b98SDavid Chinner 		count = mp->m_sb.sb_ifree;
23498d280b98SDavid Chinner 		resid = do_div(count, weight);
235020b64285SDavid Chinner 		if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
235145af6c6dSChristoph Hellwig 			return;
23528d280b98SDavid Chinner 		break;
23538d280b98SDavid Chinner 	case XFS_SBS_FDBLOCKS:
23548d280b98SDavid Chinner 		count = mp->m_sb.sb_fdblocks;
23558d280b98SDavid Chinner 		resid = do_div(count, weight);
235620b64285SDavid Chinner 		if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp)))
235745af6c6dSChristoph Hellwig 			return;
23588d280b98SDavid Chinner 		break;
23598d280b98SDavid Chinner 	default:
23608d280b98SDavid Chinner 		BUG();
23616fdf8cccSNathan Scott 		count = resid = 0;	/* quiet, gcc */
23628d280b98SDavid Chinner 		break;
23638d280b98SDavid Chinner 	}
23648d280b98SDavid Chinner 
23658d280b98SDavid Chinner 	xfs_icsb_enable_counter(mp, field, count, resid);
236645af6c6dSChristoph Hellwig }
236745af6c6dSChristoph Hellwig 
236845af6c6dSChristoph Hellwig STATIC void
236945af6c6dSChristoph Hellwig xfs_icsb_balance_counter(
237045af6c6dSChristoph Hellwig 	xfs_mount_t	*mp,
237145af6c6dSChristoph Hellwig 	xfs_sb_field_t  fields,
237245af6c6dSChristoph Hellwig 	int		min_per_cpu)
237345af6c6dSChristoph Hellwig {
237445af6c6dSChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
237545af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter_locked(mp, fields, min_per_cpu);
23763685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
23778d280b98SDavid Chinner }
23788d280b98SDavid Chinner 
23791b040712SChristoph Hellwig int
238020b64285SDavid Chinner xfs_icsb_modify_counters(
23818d280b98SDavid Chinner 	xfs_mount_t	*mp,
23828d280b98SDavid Chinner 	xfs_sb_field_t	field,
238320f4ebf2SDavid Chinner 	int64_t		delta,
238420b64285SDavid Chinner 	int		rsvd)
23858d280b98SDavid Chinner {
23868d280b98SDavid Chinner 	xfs_icsb_cnts_t	*icsbp;
23878d280b98SDavid Chinner 	long long	lcounter;	/* long counter for 64 bit fields */
23887a9e02d6SChristoph Lameter 	int		ret = 0;
23898d280b98SDavid Chinner 
239020b64285SDavid Chinner 	might_sleep();
23918d280b98SDavid Chinner again:
23927a9e02d6SChristoph Lameter 	preempt_disable();
23937a9e02d6SChristoph Lameter 	icsbp = this_cpu_ptr(mp->m_sb_cnts);
239420b64285SDavid Chinner 
239520b64285SDavid Chinner 	/*
239620b64285SDavid Chinner 	 * if the counter is disabled, go to slow path
239720b64285SDavid Chinner 	 */
23988d280b98SDavid Chinner 	if (unlikely(xfs_icsb_counter_disabled(mp, field)))
23998d280b98SDavid Chinner 		goto slow_path;
240020b64285SDavid Chinner 	xfs_icsb_lock_cntr(icsbp);
240120b64285SDavid Chinner 	if (unlikely(xfs_icsb_counter_disabled(mp, field))) {
240220b64285SDavid Chinner 		xfs_icsb_unlock_cntr(icsbp);
240320b64285SDavid Chinner 		goto slow_path;
240420b64285SDavid Chinner 	}
24058d280b98SDavid Chinner 
24068d280b98SDavid Chinner 	switch (field) {
24078d280b98SDavid Chinner 	case XFS_SBS_ICOUNT:
24088d280b98SDavid Chinner 		lcounter = icsbp->icsb_icount;
24098d280b98SDavid Chinner 		lcounter += delta;
24108d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
241120b64285SDavid Chinner 			goto balance_counter;
24128d280b98SDavid Chinner 		icsbp->icsb_icount = lcounter;
24138d280b98SDavid Chinner 		break;
24148d280b98SDavid Chinner 
24158d280b98SDavid Chinner 	case XFS_SBS_IFREE:
24168d280b98SDavid Chinner 		lcounter = icsbp->icsb_ifree;
24178d280b98SDavid Chinner 		lcounter += delta;
24188d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
241920b64285SDavid Chinner 			goto balance_counter;
24208d280b98SDavid Chinner 		icsbp->icsb_ifree = lcounter;
24218d280b98SDavid Chinner 		break;
24228d280b98SDavid Chinner 
24238d280b98SDavid Chinner 	case XFS_SBS_FDBLOCKS:
24248d280b98SDavid Chinner 		BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
24258d280b98SDavid Chinner 
24264be536deSDavid Chinner 		lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
24278d280b98SDavid Chinner 		lcounter += delta;
24288d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
242920b64285SDavid Chinner 			goto balance_counter;
24304be536deSDavid Chinner 		icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
24318d280b98SDavid Chinner 		break;
24328d280b98SDavid Chinner 	default:
24338d280b98SDavid Chinner 		BUG();
24348d280b98SDavid Chinner 		break;
24358d280b98SDavid Chinner 	}
243601e1b69cSDavid Chinner 	xfs_icsb_unlock_cntr(icsbp);
24377a9e02d6SChristoph Lameter 	preempt_enable();
24388d280b98SDavid Chinner 	return 0;
24398d280b98SDavid Chinner 
24408d280b98SDavid Chinner slow_path:
24417a9e02d6SChristoph Lameter 	preempt_enable();
244220b64285SDavid Chinner 
244320b64285SDavid Chinner 	/*
244420b64285SDavid Chinner 	 * serialise with a mutex so we don't burn lots of cpu on
244520b64285SDavid Chinner 	 * the superblock lock. We still need to hold the superblock
244620b64285SDavid Chinner 	 * lock, however, when we modify the global structures.
244720b64285SDavid Chinner 	 */
244803135cf7SDavid Chinner 	xfs_icsb_lock(mp);
244920b64285SDavid Chinner 
245020b64285SDavid Chinner 	/*
245120b64285SDavid Chinner 	 * Now running atomically.
245220b64285SDavid Chinner 	 *
245320b64285SDavid Chinner 	 * If the counter is enabled, someone has beaten us to rebalancing.
245420b64285SDavid Chinner 	 * Drop the lock and try again in the fast path....
245520b64285SDavid Chinner 	 */
245620b64285SDavid Chinner 	if (!(xfs_icsb_counter_disabled(mp, field))) {
245703135cf7SDavid Chinner 		xfs_icsb_unlock(mp);
245820b64285SDavid Chinner 		goto again;
245920b64285SDavid Chinner 	}
246020b64285SDavid Chinner 
246120b64285SDavid Chinner 	/*
246220b64285SDavid Chinner 	 * The counter is currently disabled. Because we are
246320b64285SDavid Chinner 	 * running atomically here, we know a rebalance cannot
246420b64285SDavid Chinner 	 * be in progress. Hence we can go straight to operating
246520b64285SDavid Chinner 	 * on the global superblock. We do not call xfs_mod_incore_sb()
24663685c2a1SEric Sandeen 	 * here even though we need to get the m_sb_lock. Doing so
246720b64285SDavid Chinner 	 * will cause us to re-enter this function and deadlock.
24683685c2a1SEric Sandeen 	 * Hence we get the m_sb_lock ourselves and then call
246920b64285SDavid Chinner 	 * xfs_mod_incore_sb_unlocked() as the unlocked path operates
247020b64285SDavid Chinner 	 * directly on the global counters.
247120b64285SDavid Chinner 	 */
24723685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
247320b64285SDavid Chinner 	ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
24743685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
247520b64285SDavid Chinner 
247620b64285SDavid Chinner 	/*
247720b64285SDavid Chinner 	 * Now that we've modified the global superblock, we
247820b64285SDavid Chinner 	 * may be able to re-enable the distributed counters
247920b64285SDavid Chinner 	 * (e.g. lots of space just got freed). After that
248020b64285SDavid Chinner 	 * we are done.
248120b64285SDavid Chinner 	 */
248220b64285SDavid Chinner 	if (ret != ENOSPC)
248345af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, field, 0);
248403135cf7SDavid Chinner 	xfs_icsb_unlock(mp);
248520b64285SDavid Chinner 	return ret;
248620b64285SDavid Chinner 
248720b64285SDavid Chinner balance_counter:
248801e1b69cSDavid Chinner 	xfs_icsb_unlock_cntr(icsbp);
24897a9e02d6SChristoph Lameter 	preempt_enable();
24908d280b98SDavid Chinner 
249120b64285SDavid Chinner 	/*
249220b64285SDavid Chinner 	 * We may have multiple threads here if multiple per-cpu
249320b64285SDavid Chinner 	 * counters run dry at the same time. This will mean we can
249420b64285SDavid Chinner 	 * do more balances than strictly necessary but it is not
249520b64285SDavid Chinner 	 * the common slowpath case.
249620b64285SDavid Chinner 	 */
249703135cf7SDavid Chinner 	xfs_icsb_lock(mp);
249820b64285SDavid Chinner 
249920b64285SDavid Chinner 	/*
250020b64285SDavid Chinner 	 * running atomically.
250120b64285SDavid Chinner 	 *
250220b64285SDavid Chinner 	 * This will leave the counter in the correct state for future
250320b64285SDavid Chinner 	 * accesses. After the rebalance, we simply try again and our retry
250420b64285SDavid Chinner 	 * will either succeed through the fast path or slow path without
250520b64285SDavid Chinner 	 * another balance operation being required.
250620b64285SDavid Chinner 	 */
250745af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, field, delta);
250803135cf7SDavid Chinner 	xfs_icsb_unlock(mp);
25098d280b98SDavid Chinner 	goto again;
25108d280b98SDavid Chinner }
25118d280b98SDavid Chinner 
25128d280b98SDavid Chinner #endif
2513