xref: /linux/fs/xfs/xfs_mount.c (revision 34d7f603b93be0ac38c2464219f5a2795d76c8ee)
11da177e4SLinus Torvalds /*
27b718769SNathan Scott  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
37b718769SNathan Scott  * All Rights Reserved.
41da177e4SLinus Torvalds  *
57b718769SNathan Scott  * This program is free software; you can redistribute it and/or
67b718769SNathan Scott  * modify it under the terms of the GNU General Public License as
71da177e4SLinus Torvalds  * published by the Free Software Foundation.
81da177e4SLinus Torvalds  *
97b718769SNathan Scott  * This program is distributed in the hope that it would be useful,
107b718769SNathan Scott  * but WITHOUT ANY WARRANTY; without even the implied warranty of
117b718769SNathan Scott  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
127b718769SNathan Scott  * GNU General Public License for more details.
131da177e4SLinus Torvalds  *
147b718769SNathan Scott  * You should have received a copy of the GNU General Public License
157b718769SNathan Scott  * along with this program; if not, write the Free Software Foundation,
167b718769SNathan Scott  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds #include "xfs.h"
19a844f451SNathan Scott #include "xfs_fs.h"
201da177e4SLinus Torvalds #include "xfs_types.h"
21a844f451SNathan Scott #include "xfs_bit.h"
221da177e4SLinus Torvalds #include "xfs_log.h"
23a844f451SNathan Scott #include "xfs_inum.h"
241da177e4SLinus Torvalds #include "xfs_trans.h"
25211e4d43SChristoph Hellwig #include "xfs_trans_priv.h"
261da177e4SLinus Torvalds #include "xfs_sb.h"
271da177e4SLinus Torvalds #include "xfs_ag.h"
281da177e4SLinus Torvalds #include "xfs_dir2.h"
291da177e4SLinus Torvalds #include "xfs_mount.h"
301da177e4SLinus Torvalds #include "xfs_bmap_btree.h"
31a844f451SNathan Scott #include "xfs_alloc_btree.h"
321da177e4SLinus Torvalds #include "xfs_ialloc_btree.h"
331da177e4SLinus Torvalds #include "xfs_dinode.h"
341da177e4SLinus Torvalds #include "xfs_inode.h"
35a844f451SNathan Scott #include "xfs_btree.h"
36a844f451SNathan Scott #include "xfs_ialloc.h"
371da177e4SLinus Torvalds #include "xfs_alloc.h"
381da177e4SLinus Torvalds #include "xfs_rtalloc.h"
391da177e4SLinus Torvalds #include "xfs_bmap.h"
401da177e4SLinus Torvalds #include "xfs_error.h"
411da177e4SLinus Torvalds #include "xfs_quota.h"
421da177e4SLinus Torvalds #include "xfs_fsops.h"
4343355099SChristoph Hellwig #include "xfs_utils.h"
440b1b213fSChristoph Hellwig #include "xfs_trace.h"
456d8b79cfSDave Chinner #include "xfs_icache.h"
4604a1e6c5SDave Chinner #include "xfs_cksum.h"
4704a1e6c5SDave Chinner #include "xfs_buf_item.h"
480b1b213fSChristoph Hellwig 
491da177e4SLinus Torvalds 
508d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
5120f4ebf2SDavid Chinner STATIC void	xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t,
5245af6c6dSChristoph Hellwig 						int);
5345af6c6dSChristoph Hellwig STATIC void	xfs_icsb_balance_counter_locked(xfs_mount_t *, xfs_sb_field_t,
5445af6c6dSChristoph Hellwig 						int);
5536fbe6e6SDavid Chinner STATIC void	xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
568d280b98SDavid Chinner #else
578d280b98SDavid Chinner 
5845af6c6dSChristoph Hellwig #define xfs_icsb_balance_counter(mp, a, b)		do { } while (0)
5945af6c6dSChristoph Hellwig #define xfs_icsb_balance_counter_locked(mp, a, b)	do { } while (0)
608d280b98SDavid Chinner #endif
618d280b98SDavid Chinner 
621df84c93SChristoph Hellwig static const struct {
631da177e4SLinus Torvalds 	short offset;
641da177e4SLinus Torvalds 	short type;	/* 0 = integer
651da177e4SLinus Torvalds 			 * 1 = binary / string (no translation)
661da177e4SLinus Torvalds 			 */
671da177e4SLinus Torvalds } xfs_sb_info[] = {
681da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_magicnum),   0 },
691da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_blocksize),  0 },
701da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_dblocks),    0 },
711da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rblocks),    0 },
721da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rextents),   0 },
731da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_uuid),       1 },
741da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logstart),   0 },
751da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rootino),    0 },
761da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rbmino),     0 },
771da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rsumino),    0 },
781da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rextsize),   0 },
791da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_agblocks),   0 },
801da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_agcount),    0 },
811da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rbmblocks),  0 },
821da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logblocks),  0 },
831da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_versionnum), 0 },
841da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_sectsize),   0 },
851da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inodesize),  0 },
861da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inopblock),  0 },
871da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_fname[0]),   1 },
881da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_blocklog),   0 },
891da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_sectlog),    0 },
901da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inodelog),   0 },
911da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inopblog),   0 },
921da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_agblklog),   0 },
931da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rextslog),   0 },
941da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inprogress), 0 },
951da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_imax_pct),   0 },
961da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_icount),     0 },
971da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_ifree),      0 },
981da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_fdblocks),   0 },
991da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_frextents),  0 },
1001da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_uquotino),   0 },
1011da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_gquotino),   0 },
1021da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_qflags),     0 },
1031da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_flags),      0 },
1041da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_shared_vn),  0 },
1051da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
1061da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_unit),	 0 },
1071da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_width),	 0 },
1081da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_dirblklog),	 0 },
1091da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logsectlog), 0 },
1101da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logsectsize),0 },
1111da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logsunit),	 0 },
1121da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_features2),	 0 },
113ee1c0908SDavid Chinner     { offsetof(xfs_sb_t, sb_bad_features2), 0 },
11404a1e6c5SDave Chinner     { offsetof(xfs_sb_t, sb_features_compat), 0 },
11504a1e6c5SDave Chinner     { offsetof(xfs_sb_t, sb_features_ro_compat), 0 },
11604a1e6c5SDave Chinner     { offsetof(xfs_sb_t, sb_features_incompat), 0 },
117e721f504SDave Chinner     { offsetof(xfs_sb_t, sb_features_log_incompat), 0 },
11804a1e6c5SDave Chinner     { offsetof(xfs_sb_t, sb_crc),	 0 },
119e721f504SDave Chinner     { offsetof(xfs_sb_t, sb_pad),	 0 },
12004a1e6c5SDave Chinner     { offsetof(xfs_sb_t, sb_pquotino),	 0 },
12104a1e6c5SDave Chinner     { offsetof(xfs_sb_t, sb_lsn),	 0 },
1221da177e4SLinus Torvalds     { sizeof(xfs_sb_t),			 0 }
1231da177e4SLinus Torvalds };
1241da177e4SLinus Torvalds 
12527174203SChristoph Hellwig static DEFINE_MUTEX(xfs_uuid_table_mutex);
12627174203SChristoph Hellwig static int xfs_uuid_table_size;
12727174203SChristoph Hellwig static uuid_t *xfs_uuid_table;
12827174203SChristoph Hellwig 
12927174203SChristoph Hellwig /*
13027174203SChristoph Hellwig  * See if the UUID is unique among mounted XFS filesystems.
13127174203SChristoph Hellwig  * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
13227174203SChristoph Hellwig  */
13327174203SChristoph Hellwig STATIC int
13427174203SChristoph Hellwig xfs_uuid_mount(
13527174203SChristoph Hellwig 	struct xfs_mount	*mp)
13627174203SChristoph Hellwig {
13727174203SChristoph Hellwig 	uuid_t			*uuid = &mp->m_sb.sb_uuid;
13827174203SChristoph Hellwig 	int			hole, i;
13927174203SChristoph Hellwig 
14027174203SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_NOUUID)
14127174203SChristoph Hellwig 		return 0;
14227174203SChristoph Hellwig 
14327174203SChristoph Hellwig 	if (uuid_is_nil(uuid)) {
1440b932cccSDave Chinner 		xfs_warn(mp, "Filesystem has nil UUID - can't mount");
14527174203SChristoph Hellwig 		return XFS_ERROR(EINVAL);
14627174203SChristoph Hellwig 	}
14727174203SChristoph Hellwig 
14827174203SChristoph Hellwig 	mutex_lock(&xfs_uuid_table_mutex);
14927174203SChristoph Hellwig 	for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) {
15027174203SChristoph Hellwig 		if (uuid_is_nil(&xfs_uuid_table[i])) {
15127174203SChristoph Hellwig 			hole = i;
15227174203SChristoph Hellwig 			continue;
15327174203SChristoph Hellwig 		}
15427174203SChristoph Hellwig 		if (uuid_equal(uuid, &xfs_uuid_table[i]))
15527174203SChristoph Hellwig 			goto out_duplicate;
15627174203SChristoph Hellwig 	}
15727174203SChristoph Hellwig 
15827174203SChristoph Hellwig 	if (hole < 0) {
15927174203SChristoph Hellwig 		xfs_uuid_table = kmem_realloc(xfs_uuid_table,
16027174203SChristoph Hellwig 			(xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table),
16127174203SChristoph Hellwig 			xfs_uuid_table_size  * sizeof(*xfs_uuid_table),
16227174203SChristoph Hellwig 			KM_SLEEP);
16327174203SChristoph Hellwig 		hole = xfs_uuid_table_size++;
16427174203SChristoph Hellwig 	}
16527174203SChristoph Hellwig 	xfs_uuid_table[hole] = *uuid;
16627174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
16727174203SChristoph Hellwig 
16827174203SChristoph Hellwig 	return 0;
16927174203SChristoph Hellwig 
17027174203SChristoph Hellwig  out_duplicate:
17127174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
172021000e5SMitsuo Hayasaka 	xfs_warn(mp, "Filesystem has duplicate UUID %pU - can't mount", uuid);
17327174203SChristoph Hellwig 	return XFS_ERROR(EINVAL);
17427174203SChristoph Hellwig }
17527174203SChristoph Hellwig 
17627174203SChristoph Hellwig STATIC void
17727174203SChristoph Hellwig xfs_uuid_unmount(
17827174203SChristoph Hellwig 	struct xfs_mount	*mp)
17927174203SChristoph Hellwig {
18027174203SChristoph Hellwig 	uuid_t			*uuid = &mp->m_sb.sb_uuid;
18127174203SChristoph Hellwig 	int			i;
18227174203SChristoph Hellwig 
18327174203SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_NOUUID)
18427174203SChristoph Hellwig 		return;
18527174203SChristoph Hellwig 
18627174203SChristoph Hellwig 	mutex_lock(&xfs_uuid_table_mutex);
18727174203SChristoph Hellwig 	for (i = 0; i < xfs_uuid_table_size; i++) {
18827174203SChristoph Hellwig 		if (uuid_is_nil(&xfs_uuid_table[i]))
18927174203SChristoph Hellwig 			continue;
19027174203SChristoph Hellwig 		if (!uuid_equal(uuid, &xfs_uuid_table[i]))
19127174203SChristoph Hellwig 			continue;
19227174203SChristoph Hellwig 		memset(&xfs_uuid_table[i], 0, sizeof(uuid_t));
19327174203SChristoph Hellwig 		break;
19427174203SChristoph Hellwig 	}
19527174203SChristoph Hellwig 	ASSERT(i < xfs_uuid_table_size);
19627174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
19727174203SChristoph Hellwig }
19827174203SChristoph Hellwig 
19927174203SChristoph Hellwig 
2001da177e4SLinus Torvalds /*
2010fa800fbSDave Chinner  * Reference counting access wrappers to the perag structures.
202e176579eSDave Chinner  * Because we never free per-ag structures, the only thing we
203e176579eSDave Chinner  * have to protect against changes is the tree structure itself.
2040fa800fbSDave Chinner  */
2050fa800fbSDave Chinner struct xfs_perag *
2060fa800fbSDave Chinner xfs_perag_get(struct xfs_mount *mp, xfs_agnumber_t agno)
2070fa800fbSDave Chinner {
2080fa800fbSDave Chinner 	struct xfs_perag	*pag;
2090fa800fbSDave Chinner 	int			ref = 0;
2100fa800fbSDave Chinner 
211e176579eSDave Chinner 	rcu_read_lock();
2120fa800fbSDave Chinner 	pag = radix_tree_lookup(&mp->m_perag_tree, agno);
2130fa800fbSDave Chinner 	if (pag) {
2140fa800fbSDave Chinner 		ASSERT(atomic_read(&pag->pag_ref) >= 0);
2150fa800fbSDave Chinner 		ref = atomic_inc_return(&pag->pag_ref);
2160fa800fbSDave Chinner 	}
217e176579eSDave Chinner 	rcu_read_unlock();
2180fa800fbSDave Chinner 	trace_xfs_perag_get(mp, agno, ref, _RET_IP_);
2190fa800fbSDave Chinner 	return pag;
2200fa800fbSDave Chinner }
2210fa800fbSDave Chinner 
22265d0f205SDave Chinner /*
22365d0f205SDave Chinner  * search from @first to find the next perag with the given tag set.
22465d0f205SDave Chinner  */
22565d0f205SDave Chinner struct xfs_perag *
22665d0f205SDave Chinner xfs_perag_get_tag(
22765d0f205SDave Chinner 	struct xfs_mount	*mp,
22865d0f205SDave Chinner 	xfs_agnumber_t		first,
22965d0f205SDave Chinner 	int			tag)
23065d0f205SDave Chinner {
23165d0f205SDave Chinner 	struct xfs_perag	*pag;
23265d0f205SDave Chinner 	int			found;
23365d0f205SDave Chinner 	int			ref;
23465d0f205SDave Chinner 
23565d0f205SDave Chinner 	rcu_read_lock();
23665d0f205SDave Chinner 	found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
23765d0f205SDave Chinner 					(void **)&pag, first, 1, tag);
23865d0f205SDave Chinner 	if (found <= 0) {
23965d0f205SDave Chinner 		rcu_read_unlock();
24065d0f205SDave Chinner 		return NULL;
24165d0f205SDave Chinner 	}
24265d0f205SDave Chinner 	ref = atomic_inc_return(&pag->pag_ref);
24365d0f205SDave Chinner 	rcu_read_unlock();
24465d0f205SDave Chinner 	trace_xfs_perag_get_tag(mp, pag->pag_agno, ref, _RET_IP_);
24565d0f205SDave Chinner 	return pag;
24665d0f205SDave Chinner }
24765d0f205SDave Chinner 
2480fa800fbSDave Chinner void
2490fa800fbSDave Chinner xfs_perag_put(struct xfs_perag *pag)
2500fa800fbSDave Chinner {
2510fa800fbSDave Chinner 	int	ref;
2520fa800fbSDave Chinner 
2530fa800fbSDave Chinner 	ASSERT(atomic_read(&pag->pag_ref) > 0);
2540fa800fbSDave Chinner 	ref = atomic_dec_return(&pag->pag_ref);
2550fa800fbSDave Chinner 	trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_);
2560fa800fbSDave Chinner }
2570fa800fbSDave Chinner 
258e176579eSDave Chinner STATIC void
259e176579eSDave Chinner __xfs_free_perag(
260e176579eSDave Chinner 	struct rcu_head	*head)
261e176579eSDave Chinner {
262e176579eSDave Chinner 	struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head);
263e176579eSDave Chinner 
264e176579eSDave Chinner 	ASSERT(atomic_read(&pag->pag_ref) == 0);
265e176579eSDave Chinner 	kmem_free(pag);
266e176579eSDave Chinner }
267e176579eSDave Chinner 
2680fa800fbSDave Chinner /*
269e176579eSDave Chinner  * Free up the per-ag resources associated with the mount structure.
2701da177e4SLinus Torvalds  */
271c962fb79SChristoph Hellwig STATIC void
272ff4f038cSChristoph Hellwig xfs_free_perag(
273745f6919SChristoph Hellwig 	xfs_mount_t	*mp)
2741da177e4SLinus Torvalds {
2751c1c6ebcSDave Chinner 	xfs_agnumber_t	agno;
2761c1c6ebcSDave Chinner 	struct xfs_perag *pag;
2771da177e4SLinus Torvalds 
2781c1c6ebcSDave Chinner 	for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
2791c1c6ebcSDave Chinner 		spin_lock(&mp->m_perag_lock);
2801c1c6ebcSDave Chinner 		pag = radix_tree_delete(&mp->m_perag_tree, agno);
2811c1c6ebcSDave Chinner 		spin_unlock(&mp->m_perag_lock);
282e176579eSDave Chinner 		ASSERT(pag);
283f83282a8SDave Chinner 		ASSERT(atomic_read(&pag->pag_ref) == 0);
284e176579eSDave Chinner 		call_rcu(&pag->rcu_head, __xfs_free_perag);
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds 
2884cc929eeSNathan Scott /*
2894cc929eeSNathan Scott  * Check size of device based on the (data/realtime) block count.
2904cc929eeSNathan Scott  * Note: this check is used by the growfs code as well as mount.
2914cc929eeSNathan Scott  */
2924cc929eeSNathan Scott int
2934cc929eeSNathan Scott xfs_sb_validate_fsb_count(
2944cc929eeSNathan Scott 	xfs_sb_t	*sbp,
2954cc929eeSNathan Scott 	__uint64_t	nblocks)
2964cc929eeSNathan Scott {
2974cc929eeSNathan Scott 	ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
2984cc929eeSNathan Scott 	ASSERT(sbp->sb_blocklog >= BBSHIFT);
2994cc929eeSNathan Scott 
3004cc929eeSNathan Scott #if XFS_BIG_BLKNOS     /* Limited by ULONG_MAX of page cache index */
3014cc929eeSNathan Scott 	if (nblocks >> (PAGE_CACHE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
302657a4cffSEric Sandeen 		return EFBIG;
3034cc929eeSNathan Scott #else                  /* Limited by UINT_MAX of sectors */
3044cc929eeSNathan Scott 	if (nblocks << (sbp->sb_blocklog - BBSHIFT) > UINT_MAX)
305657a4cffSEric Sandeen 		return EFBIG;
3064cc929eeSNathan Scott #endif
3074cc929eeSNathan Scott 	return 0;
3084cc929eeSNathan Scott }
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds /*
3111da177e4SLinus Torvalds  * Check the validity of the SB found.
3121da177e4SLinus Torvalds  */
3131da177e4SLinus Torvalds STATIC int
3141da177e4SLinus Torvalds xfs_mount_validate_sb(
3151da177e4SLinus Torvalds 	xfs_mount_t	*mp,
316764d1f89SNathan Scott 	xfs_sb_t	*sbp,
31734510185SDave Chinner 	bool		check_inprogress,
31834510185SDave Chinner 	bool		check_version)
3191da177e4SLinus Torvalds {
320af34e09dSDave Chinner 
3211da177e4SLinus Torvalds 	/*
3221da177e4SLinus Torvalds 	 * If the log device and data device have the
3231da177e4SLinus Torvalds 	 * same device number, the log is internal.
3241da177e4SLinus Torvalds 	 * Consequently, the sb_logstart should be non-zero.  If
3251da177e4SLinus Torvalds 	 * we have a zero sb_logstart in this case, we may be trying to mount
3261da177e4SLinus Torvalds 	 * a volume filesystem in a non-volume manner.
3271da177e4SLinus Torvalds 	 */
3281da177e4SLinus Torvalds 	if (sbp->sb_magicnum != XFS_SB_MAGIC) {
329af34e09dSDave Chinner 		xfs_warn(mp, "bad magic number");
3301da177e4SLinus Torvalds 		return XFS_ERROR(EWRONGFS);
3311da177e4SLinus Torvalds 	}
3321da177e4SLinus Torvalds 
33304a1e6c5SDave Chinner 
33462118709SEric Sandeen 	if (!xfs_sb_good_version(sbp)) {
335af34e09dSDave Chinner 		xfs_warn(mp, "bad version");
3361da177e4SLinus Torvalds 		return XFS_ERROR(EWRONGFS);
3371da177e4SLinus Torvalds 	}
3381da177e4SLinus Torvalds 
33904a1e6c5SDave Chinner 	/*
340e721f504SDave Chinner 	 * Version 5 superblock feature mask validation. Reject combinations the
34134510185SDave Chinner 	 * kernel cannot support up front before checking anything else. For
34234510185SDave Chinner 	 * write validation, we don't need to check feature masks.
34304a1e6c5SDave Chinner 	 */
34434510185SDave Chinner 	if (check_version && XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) {
34504a1e6c5SDave Chinner 		xfs_alert(mp,
346e721f504SDave Chinner "Version 5 superblock detected. This kernel has EXPERIMENTAL support enabled!\n"
347e721f504SDave Chinner "Use of these features in this kernel is at your own risk!");
348e721f504SDave Chinner 
349e721f504SDave Chinner 		if (xfs_sb_has_compat_feature(sbp,
350e721f504SDave Chinner 					XFS_SB_FEAT_COMPAT_UNKNOWN)) {
351e721f504SDave Chinner 			xfs_warn(mp,
352e721f504SDave Chinner "Superblock has unknown compatible features (0x%x) enabled.\n"
353e721f504SDave Chinner "Using a more recent kernel is recommended.",
354e721f504SDave Chinner 				(sbp->sb_features_compat &
355e721f504SDave Chinner 						XFS_SB_FEAT_COMPAT_UNKNOWN));
356e721f504SDave Chinner 		}
357e721f504SDave Chinner 
358e721f504SDave Chinner 		if (xfs_sb_has_ro_compat_feature(sbp,
359e721f504SDave Chinner 					XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
360e721f504SDave Chinner 			xfs_alert(mp,
361e721f504SDave Chinner "Superblock has unknown read-only compatible features (0x%x) enabled.",
362e721f504SDave Chinner 				(sbp->sb_features_ro_compat &
363e721f504SDave Chinner 						XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
364e721f504SDave Chinner 			if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
365e721f504SDave Chinner 				xfs_warn(mp,
366e721f504SDave Chinner "Attempted to mount read-only compatible filesystem read-write.\n"
367e721f504SDave Chinner "Filesystem can only be safely mounted read only.");
36804a1e6c5SDave Chinner 				return XFS_ERROR(EINVAL);
36904a1e6c5SDave Chinner 			}
370e721f504SDave Chinner 		}
371e721f504SDave Chinner 		if (xfs_sb_has_incompat_feature(sbp,
372e721f504SDave Chinner 					XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
373e721f504SDave Chinner 			xfs_warn(mp,
374e721f504SDave Chinner "Superblock has unknown incompatible features (0x%x) enabled.\n"
375e721f504SDave Chinner "Filesystem can not be safely mounted by this kernel.",
376e721f504SDave Chinner 				(sbp->sb_features_incompat &
377e721f504SDave Chinner 						XFS_SB_FEAT_INCOMPAT_UNKNOWN));
378e721f504SDave Chinner 			return XFS_ERROR(EINVAL);
379e721f504SDave Chinner 		}
380e721f504SDave Chinner 	}
38104a1e6c5SDave Chinner 
3821da177e4SLinus Torvalds 	if (unlikely(
3831da177e4SLinus Torvalds 	    sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
384af34e09dSDave Chinner 		xfs_warn(mp,
385764d1f89SNathan Scott 		"filesystem is marked as having an external log; "
386af34e09dSDave Chinner 		"specify logdev on the mount command line.");
387764d1f89SNathan Scott 		return XFS_ERROR(EINVAL);
3881da177e4SLinus Torvalds 	}
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	if (unlikely(
3911da177e4SLinus Torvalds 	    sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
392af34e09dSDave Chinner 		xfs_warn(mp,
393764d1f89SNathan Scott 		"filesystem is marked as having an internal log; "
394af34e09dSDave Chinner 		"do not specify logdev on the mount command line.");
395764d1f89SNathan Scott 		return XFS_ERROR(EINVAL);
3961da177e4SLinus Torvalds 	}
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	/*
399c0e090ceSEric Sandeen 	 * More sanity checking.  Most of these were stolen directly from
4001da177e4SLinus Torvalds 	 * xfs_repair.
4011da177e4SLinus Torvalds 	 */
4021da177e4SLinus Torvalds 	if (unlikely(
4031da177e4SLinus Torvalds 	    sbp->sb_agcount <= 0					||
4041da177e4SLinus Torvalds 	    sbp->sb_sectsize < XFS_MIN_SECTORSIZE			||
4051da177e4SLinus Torvalds 	    sbp->sb_sectsize > XFS_MAX_SECTORSIZE			||
4061da177e4SLinus Torvalds 	    sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG			||
4071da177e4SLinus Torvalds 	    sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG			||
4082ac00af7SOlaf Weber 	    sbp->sb_sectsize != (1 << sbp->sb_sectlog)			||
4091da177e4SLinus Torvalds 	    sbp->sb_blocksize < XFS_MIN_BLOCKSIZE			||
4101da177e4SLinus Torvalds 	    sbp->sb_blocksize > XFS_MAX_BLOCKSIZE			||
4111da177e4SLinus Torvalds 	    sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG			||
4121da177e4SLinus Torvalds 	    sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG			||
4132ac00af7SOlaf Weber 	    sbp->sb_blocksize != (1 << sbp->sb_blocklog)		||
4141da177e4SLinus Torvalds 	    sbp->sb_inodesize < XFS_DINODE_MIN_SIZE			||
4151da177e4SLinus Torvalds 	    sbp->sb_inodesize > XFS_DINODE_MAX_SIZE			||
4169f989c94SNathan Scott 	    sbp->sb_inodelog < XFS_DINODE_MIN_LOG			||
4179f989c94SNathan Scott 	    sbp->sb_inodelog > XFS_DINODE_MAX_LOG			||
4182ac00af7SOlaf Weber 	    sbp->sb_inodesize != (1 << sbp->sb_inodelog)		||
4199f989c94SNathan Scott 	    (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog)	||
4201da177e4SLinus Torvalds 	    (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE)	||
4211da177e4SLinus Torvalds 	    (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE)	||
422c0e090ceSEric Sandeen 	    (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */)	||
4231da177e4SLinus Torvalds 	    sbp->sb_dblocks == 0					||
424c0e090ceSEric Sandeen 	    sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp)			||
425c0e090ceSEric Sandeen 	    sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp))) {
426c0e090ceSEric Sandeen 		XFS_CORRUPTION_ERROR("SB sanity check failed",
427c0e090ceSEric Sandeen 				XFS_ERRLEVEL_LOW, mp, sbp);
4281da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
4291da177e4SLinus Torvalds 	}
4301da177e4SLinus Torvalds 
4312edbddd5SLachlan McIlroy 	/*
4322edbddd5SLachlan McIlroy 	 * Until this is fixed only page-sized or smaller data blocks work.
4332edbddd5SLachlan McIlroy 	 */
4342edbddd5SLachlan McIlroy 	if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
435af34e09dSDave Chinner 		xfs_warn(mp,
436af34e09dSDave Chinner 		"File system with blocksize %d bytes. "
437af34e09dSDave Chinner 		"Only pagesize (%ld) or less will currently work.",
438af34e09dSDave Chinner 				sbp->sb_blocksize, PAGE_SIZE);
4392edbddd5SLachlan McIlroy 		return XFS_ERROR(ENOSYS);
4402edbddd5SLachlan McIlroy 	}
4412edbddd5SLachlan McIlroy 
4421a5902c5SChristoph Hellwig 	/*
4431a5902c5SChristoph Hellwig 	 * Currently only very few inode sizes are supported.
4441a5902c5SChristoph Hellwig 	 */
4451a5902c5SChristoph Hellwig 	switch (sbp->sb_inodesize) {
4461a5902c5SChristoph Hellwig 	case 256:
4471a5902c5SChristoph Hellwig 	case 512:
4481a5902c5SChristoph Hellwig 	case 1024:
4491a5902c5SChristoph Hellwig 	case 2048:
4501a5902c5SChristoph Hellwig 		break;
4511a5902c5SChristoph Hellwig 	default:
452af34e09dSDave Chinner 		xfs_warn(mp, "inode size of %d bytes not supported",
4531a5902c5SChristoph Hellwig 				sbp->sb_inodesize);
4541a5902c5SChristoph Hellwig 		return XFS_ERROR(ENOSYS);
4551a5902c5SChristoph Hellwig 	}
4561a5902c5SChristoph Hellwig 
4574cc929eeSNathan Scott 	if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
4584cc929eeSNathan Scott 	    xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
459af34e09dSDave Chinner 		xfs_warn(mp,
460764d1f89SNathan Scott 		"file system too large to be mounted on this system.");
461657a4cffSEric Sandeen 		return XFS_ERROR(EFBIG);
4621da177e4SLinus Torvalds 	}
4631da177e4SLinus Torvalds 
46498021821SDave Chinner 	if (check_inprogress && sbp->sb_inprogress) {
46598021821SDave Chinner 		xfs_warn(mp, "Offline file system operation in progress!");
4661da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
4671da177e4SLinus Torvalds 	}
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	/*
470de20614bSNathan Scott 	 * Version 1 directory format has never worked on Linux.
471de20614bSNathan Scott 	 */
47262118709SEric Sandeen 	if (unlikely(!xfs_sb_version_hasdirv2(sbp))) {
47398021821SDave Chinner 		xfs_warn(mp, "file system using version 1 directory format");
474de20614bSNathan Scott 		return XFS_ERROR(ENOSYS);
475de20614bSNathan Scott 	}
476de20614bSNathan Scott 
4771da177e4SLinus Torvalds 	return 0;
4781da177e4SLinus Torvalds }
4791da177e4SLinus Torvalds 
4801c1c6ebcSDave Chinner int
481c11e2c36SNathan Scott xfs_initialize_perag(
482c11e2c36SNathan Scott 	xfs_mount_t	*mp,
4831c1c6ebcSDave Chinner 	xfs_agnumber_t	agcount,
4841c1c6ebcSDave Chinner 	xfs_agnumber_t	*maxagi)
4851da177e4SLinus Torvalds {
4862d2194f6SCarlos Maiolino 	xfs_agnumber_t	index;
4878b26c582SDave Chinner 	xfs_agnumber_t	first_initialised = 0;
4881da177e4SLinus Torvalds 	xfs_perag_t	*pag;
4891da177e4SLinus Torvalds 	xfs_agino_t	agino;
4901da177e4SLinus Torvalds 	xfs_ino_t	ino;
4911da177e4SLinus Torvalds 	xfs_sb_t	*sbp = &mp->m_sb;
4928b26c582SDave Chinner 	int		error = -ENOMEM;
4931da177e4SLinus Torvalds 
4941c1c6ebcSDave Chinner 	/*
4951c1c6ebcSDave Chinner 	 * Walk the current per-ag tree so we don't try to initialise AGs
4961c1c6ebcSDave Chinner 	 * that already exist (growfs case). Allocate and insert all the
4971c1c6ebcSDave Chinner 	 * AGs we don't find ready for initialisation.
4981c1c6ebcSDave Chinner 	 */
4991c1c6ebcSDave Chinner 	for (index = 0; index < agcount; index++) {
5001c1c6ebcSDave Chinner 		pag = xfs_perag_get(mp, index);
5011c1c6ebcSDave Chinner 		if (pag) {
5021c1c6ebcSDave Chinner 			xfs_perag_put(pag);
5031c1c6ebcSDave Chinner 			continue;
5041c1c6ebcSDave Chinner 		}
5058b26c582SDave Chinner 		if (!first_initialised)
5068b26c582SDave Chinner 			first_initialised = index;
507fb3b504aSChristoph Hellwig 
5081c1c6ebcSDave Chinner 		pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
5091c1c6ebcSDave Chinner 		if (!pag)
5108b26c582SDave Chinner 			goto out_unwind;
511fb3b504aSChristoph Hellwig 		pag->pag_agno = index;
512fb3b504aSChristoph Hellwig 		pag->pag_mount = mp;
5131a427ab0SDave Chinner 		spin_lock_init(&pag->pag_ici_lock);
51469b491c2SDave Chinner 		mutex_init(&pag->pag_ici_reclaim_lock);
515fb3b504aSChristoph Hellwig 		INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
51674f75a0cSDave Chinner 		spin_lock_init(&pag->pag_buf_lock);
51774f75a0cSDave Chinner 		pag->pag_buf_tree = RB_ROOT;
518fb3b504aSChristoph Hellwig 
5191c1c6ebcSDave Chinner 		if (radix_tree_preload(GFP_NOFS))
5208b26c582SDave Chinner 			goto out_unwind;
521fb3b504aSChristoph Hellwig 
5221c1c6ebcSDave Chinner 		spin_lock(&mp->m_perag_lock);
5231c1c6ebcSDave Chinner 		if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
5241c1c6ebcSDave Chinner 			BUG();
5251c1c6ebcSDave Chinner 			spin_unlock(&mp->m_perag_lock);
5268b26c582SDave Chinner 			radix_tree_preload_end();
5278b26c582SDave Chinner 			error = -EEXIST;
5288b26c582SDave Chinner 			goto out_unwind;
5291c1c6ebcSDave Chinner 		}
5301c1c6ebcSDave Chinner 		spin_unlock(&mp->m_perag_lock);
5311c1c6ebcSDave Chinner 		radix_tree_preload_end();
5321c1c6ebcSDave Chinner 	}
5331c1c6ebcSDave Chinner 
534fb3b504aSChristoph Hellwig 	/*
535fb3b504aSChristoph Hellwig 	 * If we mount with the inode64 option, or no inode overflows
536fb3b504aSChristoph Hellwig 	 * the legacy 32-bit address space clear the inode32 option.
5371da177e4SLinus Torvalds 	 */
538fb3b504aSChristoph Hellwig 	agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
539fb3b504aSChristoph Hellwig 	ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
5401da177e4SLinus Torvalds 
541fb3b504aSChristoph Hellwig 	if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > XFS_MAXINUMBER_32)
542fb3b504aSChristoph Hellwig 		mp->m_flags |= XFS_MOUNT_32BITINODES;
543fb3b504aSChristoph Hellwig 	else
544fb3b504aSChristoph Hellwig 		mp->m_flags &= ~XFS_MOUNT_32BITINODES;
545fb3b504aSChristoph Hellwig 
5462d2194f6SCarlos Maiolino 	if (mp->m_flags & XFS_MOUNT_32BITINODES)
5472d2194f6SCarlos Maiolino 		index = xfs_set_inode32(mp);
5482d2194f6SCarlos Maiolino 	else
5492d2194f6SCarlos Maiolino 		index = xfs_set_inode64(mp);
550fb3b504aSChristoph Hellwig 
5511c1c6ebcSDave Chinner 	if (maxagi)
5521c1c6ebcSDave Chinner 		*maxagi = index;
5531c1c6ebcSDave Chinner 	return 0;
5548b26c582SDave Chinner 
5558b26c582SDave Chinner out_unwind:
5568b26c582SDave Chinner 	kmem_free(pag);
5578b26c582SDave Chinner 	for (; index > first_initialised; index--) {
5588b26c582SDave Chinner 		pag = radix_tree_delete(&mp->m_perag_tree, index);
5598b26c582SDave Chinner 		kmem_free(pag);
5608b26c582SDave Chinner 	}
5618b26c582SDave Chinner 	return error;
5621da177e4SLinus Torvalds }
5631da177e4SLinus Torvalds 
5642bdf7cd0SChristoph Hellwig void
5652bdf7cd0SChristoph Hellwig xfs_sb_from_disk(
56698021821SDave Chinner 	struct xfs_sb	*to,
5672bdf7cd0SChristoph Hellwig 	xfs_dsb_t	*from)
5682bdf7cd0SChristoph Hellwig {
5692bdf7cd0SChristoph Hellwig 	to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
5702bdf7cd0SChristoph Hellwig 	to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
5712bdf7cd0SChristoph Hellwig 	to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
5722bdf7cd0SChristoph Hellwig 	to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
5732bdf7cd0SChristoph Hellwig 	to->sb_rextents = be64_to_cpu(from->sb_rextents);
5742bdf7cd0SChristoph Hellwig 	memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
5752bdf7cd0SChristoph Hellwig 	to->sb_logstart = be64_to_cpu(from->sb_logstart);
5762bdf7cd0SChristoph Hellwig 	to->sb_rootino = be64_to_cpu(from->sb_rootino);
5772bdf7cd0SChristoph Hellwig 	to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
5782bdf7cd0SChristoph Hellwig 	to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
5792bdf7cd0SChristoph Hellwig 	to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
5802bdf7cd0SChristoph Hellwig 	to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
5812bdf7cd0SChristoph Hellwig 	to->sb_agcount = be32_to_cpu(from->sb_agcount);
5822bdf7cd0SChristoph Hellwig 	to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
5832bdf7cd0SChristoph Hellwig 	to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
5842bdf7cd0SChristoph Hellwig 	to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
5852bdf7cd0SChristoph Hellwig 	to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
5862bdf7cd0SChristoph Hellwig 	to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
5872bdf7cd0SChristoph Hellwig 	to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
5882bdf7cd0SChristoph Hellwig 	memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
5892bdf7cd0SChristoph Hellwig 	to->sb_blocklog = from->sb_blocklog;
5902bdf7cd0SChristoph Hellwig 	to->sb_sectlog = from->sb_sectlog;
5912bdf7cd0SChristoph Hellwig 	to->sb_inodelog = from->sb_inodelog;
5922bdf7cd0SChristoph Hellwig 	to->sb_inopblog = from->sb_inopblog;
5932bdf7cd0SChristoph Hellwig 	to->sb_agblklog = from->sb_agblklog;
5942bdf7cd0SChristoph Hellwig 	to->sb_rextslog = from->sb_rextslog;
5952bdf7cd0SChristoph Hellwig 	to->sb_inprogress = from->sb_inprogress;
5962bdf7cd0SChristoph Hellwig 	to->sb_imax_pct = from->sb_imax_pct;
5972bdf7cd0SChristoph Hellwig 	to->sb_icount = be64_to_cpu(from->sb_icount);
5982bdf7cd0SChristoph Hellwig 	to->sb_ifree = be64_to_cpu(from->sb_ifree);
5992bdf7cd0SChristoph Hellwig 	to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
6002bdf7cd0SChristoph Hellwig 	to->sb_frextents = be64_to_cpu(from->sb_frextents);
6012bdf7cd0SChristoph Hellwig 	to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
6022bdf7cd0SChristoph Hellwig 	to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
6032bdf7cd0SChristoph Hellwig 	to->sb_qflags = be16_to_cpu(from->sb_qflags);
6042bdf7cd0SChristoph Hellwig 	to->sb_flags = from->sb_flags;
6052bdf7cd0SChristoph Hellwig 	to->sb_shared_vn = from->sb_shared_vn;
6062bdf7cd0SChristoph Hellwig 	to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
6072bdf7cd0SChristoph Hellwig 	to->sb_unit = be32_to_cpu(from->sb_unit);
6082bdf7cd0SChristoph Hellwig 	to->sb_width = be32_to_cpu(from->sb_width);
6092bdf7cd0SChristoph Hellwig 	to->sb_dirblklog = from->sb_dirblklog;
6102bdf7cd0SChristoph Hellwig 	to->sb_logsectlog = from->sb_logsectlog;
6112bdf7cd0SChristoph Hellwig 	to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
6122bdf7cd0SChristoph Hellwig 	to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
6132bdf7cd0SChristoph Hellwig 	to->sb_features2 = be32_to_cpu(from->sb_features2);
614ee1c0908SDavid Chinner 	to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
61504a1e6c5SDave Chinner 	to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
61604a1e6c5SDave Chinner 	to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
61704a1e6c5SDave Chinner 	to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
618e721f504SDave Chinner 	to->sb_features_log_incompat =
619e721f504SDave Chinner 				be32_to_cpu(from->sb_features_log_incompat);
620e721f504SDave Chinner 	to->sb_pad = 0;
62104a1e6c5SDave Chinner 	to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
62204a1e6c5SDave Chinner 	to->sb_lsn = be64_to_cpu(from->sb_lsn);
6232bdf7cd0SChristoph Hellwig }
6242bdf7cd0SChristoph Hellwig 
6251da177e4SLinus Torvalds /*
6262bdf7cd0SChristoph Hellwig  * Copy in core superblock to ondisk one.
6271da177e4SLinus Torvalds  *
6282bdf7cd0SChristoph Hellwig  * The fields argument is mask of superblock fields to copy.
6291da177e4SLinus Torvalds  */
6301da177e4SLinus Torvalds void
6312bdf7cd0SChristoph Hellwig xfs_sb_to_disk(
6322bdf7cd0SChristoph Hellwig 	xfs_dsb_t	*to,
6332bdf7cd0SChristoph Hellwig 	xfs_sb_t	*from,
6341da177e4SLinus Torvalds 	__int64_t	fields)
6351da177e4SLinus Torvalds {
6362bdf7cd0SChristoph Hellwig 	xfs_caddr_t	to_ptr = (xfs_caddr_t)to;
6372bdf7cd0SChristoph Hellwig 	xfs_caddr_t	from_ptr = (xfs_caddr_t)from;
6381da177e4SLinus Torvalds 	xfs_sb_field_t	f;
6391da177e4SLinus Torvalds 	int		first;
6401da177e4SLinus Torvalds 	int		size;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	ASSERT(fields);
6431da177e4SLinus Torvalds 	if (!fields)
6441da177e4SLinus Torvalds 		return;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	while (fields) {
6471da177e4SLinus Torvalds 		f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
6481da177e4SLinus Torvalds 		first = xfs_sb_info[f].offset;
6491da177e4SLinus Torvalds 		size = xfs_sb_info[f + 1].offset - first;
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 		ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 		if (size == 1 || xfs_sb_info[f].type == 1) {
6542bdf7cd0SChristoph Hellwig 			memcpy(to_ptr + first, from_ptr + first, size);
6551da177e4SLinus Torvalds 		} else {
6561da177e4SLinus Torvalds 			switch (size) {
6571da177e4SLinus Torvalds 			case 2:
6582bdf7cd0SChristoph Hellwig 				*(__be16 *)(to_ptr + first) =
6592bdf7cd0SChristoph Hellwig 					cpu_to_be16(*(__u16 *)(from_ptr + first));
6601da177e4SLinus Torvalds 				break;
6611da177e4SLinus Torvalds 			case 4:
6622bdf7cd0SChristoph Hellwig 				*(__be32 *)(to_ptr + first) =
6632bdf7cd0SChristoph Hellwig 					cpu_to_be32(*(__u32 *)(from_ptr + first));
6641da177e4SLinus Torvalds 				break;
6651da177e4SLinus Torvalds 			case 8:
6662bdf7cd0SChristoph Hellwig 				*(__be64 *)(to_ptr + first) =
6672bdf7cd0SChristoph Hellwig 					cpu_to_be64(*(__u64 *)(from_ptr + first));
6681da177e4SLinus Torvalds 				break;
6691da177e4SLinus Torvalds 			default:
6701da177e4SLinus Torvalds 				ASSERT(0);
6711da177e4SLinus Torvalds 			}
6721da177e4SLinus Torvalds 		}
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 		fields &= ~(1LL << f);
6751da177e4SLinus Torvalds 	}
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds 
67804a1e6c5SDave Chinner static int
679612cfbfeSDave Chinner xfs_sb_verify(
68034510185SDave Chinner 	struct xfs_buf	*bp,
68134510185SDave Chinner 	bool		check_version)
68298021821SDave Chinner {
68398021821SDave Chinner 	struct xfs_mount *mp = bp->b_target->bt_mount;
68498021821SDave Chinner 	struct xfs_sb	sb;
68598021821SDave Chinner 
68698021821SDave Chinner 	xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp));
68798021821SDave Chinner 
68898021821SDave Chinner 	/*
68998021821SDave Chinner 	 * Only check the in progress field for the primary superblock as
69098021821SDave Chinner 	 * mkfs.xfs doesn't clear it from secondary superblocks.
69198021821SDave Chinner 	 */
69234510185SDave Chinner 	return xfs_mount_validate_sb(mp, &sb, bp->b_bn == XFS_SB_DADDR,
69334510185SDave Chinner 				     check_version);
694612cfbfeSDave Chinner }
695612cfbfeSDave Chinner 
69604a1e6c5SDave Chinner /*
69704a1e6c5SDave Chinner  * If the superblock has the CRC feature bit set or the CRC field is non-null,
69804a1e6c5SDave Chinner  * check that the CRC is valid.  We check the CRC field is non-null because a
69904a1e6c5SDave Chinner  * single bit error could clear the feature bit and unused parts of the
70004a1e6c5SDave Chinner  * superblock are supposed to be zero. Hence a non-null crc field indicates that
70104a1e6c5SDave Chinner  * we've potentially lost a feature bit and we should check it anyway.
70204a1e6c5SDave Chinner  */
7031813dd64SDave Chinner static void
704612cfbfeSDave Chinner xfs_sb_read_verify(
705612cfbfeSDave Chinner 	struct xfs_buf	*bp)
706612cfbfeSDave Chinner {
70704a1e6c5SDave Chinner 	struct xfs_mount *mp = bp->b_target->bt_mount;
70804a1e6c5SDave Chinner 	struct xfs_dsb	*dsb = XFS_BUF_TO_SBP(bp);
70904a1e6c5SDave Chinner 	int		error;
71004a1e6c5SDave Chinner 
71104a1e6c5SDave Chinner 	/*
71204a1e6c5SDave Chinner 	 * open code the version check to avoid needing to convert the entire
71304a1e6c5SDave Chinner 	 * superblock from disk order just to check the version number
71404a1e6c5SDave Chinner 	 */
71504a1e6c5SDave Chinner 	if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
71604a1e6c5SDave Chinner 	    (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
71704a1e6c5SDave Chinner 						XFS_SB_VERSION_5) ||
71804a1e6c5SDave Chinner 	     dsb->sb_crc != 0)) {
71904a1e6c5SDave Chinner 
72004a1e6c5SDave Chinner 		if (!xfs_verify_cksum(bp->b_addr, be16_to_cpu(dsb->sb_sectsize),
72104a1e6c5SDave Chinner 				      offsetof(struct xfs_sb, sb_crc))) {
72204a1e6c5SDave Chinner 			error = EFSCORRUPTED;
72304a1e6c5SDave Chinner 			goto out_error;
72404a1e6c5SDave Chinner 		}
72504a1e6c5SDave Chinner 	}
72634510185SDave Chinner 	error = xfs_sb_verify(bp, true);
72704a1e6c5SDave Chinner 
72804a1e6c5SDave Chinner out_error:
72904a1e6c5SDave Chinner 	if (error) {
73004a1e6c5SDave Chinner 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
73104a1e6c5SDave Chinner 		xfs_buf_ioerror(bp, error);
73204a1e6c5SDave Chinner 	}
73398021821SDave Chinner }
73498021821SDave Chinner 
73598021821SDave Chinner /*
73698021821SDave Chinner  * We may be probed for a filesystem match, so we may not want to emit
73798021821SDave Chinner  * messages when the superblock buffer is not actually an XFS superblock.
73898021821SDave Chinner  * If we find an XFS superblock, the run a normal, noisy mount because we are
73998021821SDave Chinner  * really going to mount it and want to know about errors.
74098021821SDave Chinner  */
7411813dd64SDave Chinner static void
74298021821SDave Chinner xfs_sb_quiet_read_verify(
74398021821SDave Chinner 	struct xfs_buf	*bp)
74498021821SDave Chinner {
74504a1e6c5SDave Chinner 	struct xfs_dsb	*dsb = XFS_BUF_TO_SBP(bp);
74698021821SDave Chinner 
74798021821SDave Chinner 
74804a1e6c5SDave Chinner 	if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
74998021821SDave Chinner 		/* XFS filesystem, verify noisily! */
75098021821SDave Chinner 		xfs_sb_read_verify(bp);
75198021821SDave Chinner 		return;
75298021821SDave Chinner 	}
75398021821SDave Chinner 	/* quietly fail */
754aeb4f20aSEric Sandeen 	xfs_buf_ioerror(bp, EWRONGFS);
75598021821SDave Chinner }
75698021821SDave Chinner 
7571813dd64SDave Chinner static void
7581813dd64SDave Chinner xfs_sb_write_verify(
7591813dd64SDave Chinner 	struct xfs_buf		*bp)
7601813dd64SDave Chinner {
76104a1e6c5SDave Chinner 	struct xfs_mount	*mp = bp->b_target->bt_mount;
76204a1e6c5SDave Chinner 	struct xfs_buf_log_item	*bip = bp->b_fspriv;
76304a1e6c5SDave Chinner 	int			error;
76404a1e6c5SDave Chinner 
76534510185SDave Chinner 	error = xfs_sb_verify(bp, false);
76604a1e6c5SDave Chinner 	if (error) {
76704a1e6c5SDave Chinner 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
76804a1e6c5SDave Chinner 		xfs_buf_ioerror(bp, error);
76904a1e6c5SDave Chinner 		return;
77004a1e6c5SDave Chinner 	}
77104a1e6c5SDave Chinner 
77204a1e6c5SDave Chinner 	if (!xfs_sb_version_hascrc(&mp->m_sb))
77304a1e6c5SDave Chinner 		return;
77404a1e6c5SDave Chinner 
77504a1e6c5SDave Chinner 	if (bip)
77604a1e6c5SDave Chinner 		XFS_BUF_TO_SBP(bp)->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
77704a1e6c5SDave Chinner 
77804a1e6c5SDave Chinner 	xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
77904a1e6c5SDave Chinner 			 offsetof(struct xfs_sb, sb_crc));
7801813dd64SDave Chinner }
7811813dd64SDave Chinner 
7821813dd64SDave Chinner const struct xfs_buf_ops xfs_sb_buf_ops = {
7831813dd64SDave Chinner 	.verify_read = xfs_sb_read_verify,
7841813dd64SDave Chinner 	.verify_write = xfs_sb_write_verify,
7851813dd64SDave Chinner };
7861813dd64SDave Chinner 
7871813dd64SDave Chinner static const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
7881813dd64SDave Chinner 	.verify_read = xfs_sb_quiet_read_verify,
7891813dd64SDave Chinner 	.verify_write = xfs_sb_write_verify,
7901813dd64SDave Chinner };
7911813dd64SDave Chinner 
7921da177e4SLinus Torvalds /*
7931da177e4SLinus Torvalds  * xfs_readsb
7941da177e4SLinus Torvalds  *
7951da177e4SLinus Torvalds  * Does the initial read of the superblock.
7961da177e4SLinus Torvalds  */
7971da177e4SLinus Torvalds int
798764d1f89SNathan Scott xfs_readsb(xfs_mount_t *mp, int flags)
7991da177e4SLinus Torvalds {
8001da177e4SLinus Torvalds 	unsigned int	sector_size;
80104a1e6c5SDave Chinner 	struct xfs_buf	*bp;
80204a1e6c5SDave Chinner 	struct xfs_sb	*sbp = &mp->m_sb;
8031da177e4SLinus Torvalds 	int		error;
804af34e09dSDave Chinner 	int		loud = !(flags & XFS_MFSI_QUIET);
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	ASSERT(mp->m_sb_bp == NULL);
8071da177e4SLinus Torvalds 	ASSERT(mp->m_ddev_targp != NULL);
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	/*
8101da177e4SLinus Torvalds 	 * Allocate a (locked) buffer to hold the superblock.
8111da177e4SLinus Torvalds 	 * This will be kept around at all times to optimize
8121da177e4SLinus Torvalds 	 * access to the superblock.
8131da177e4SLinus Torvalds 	 */
8141da177e4SLinus Torvalds 	sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
8151da177e4SLinus Torvalds 
81626af6552SDave Chinner reread:
817e70b73f8SDave Chinner 	bp = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR,
81898021821SDave Chinner 				   BTOBB(sector_size), 0,
8191813dd64SDave Chinner 				   loud ? &xfs_sb_buf_ops
8201813dd64SDave Chinner 				        : &xfs_sb_quiet_buf_ops);
82126af6552SDave Chinner 	if (!bp) {
822af34e09dSDave Chinner 		if (loud)
823af34e09dSDave Chinner 			xfs_warn(mp, "SB buffer read failed");
82426af6552SDave Chinner 		return EIO;
8251da177e4SLinus Torvalds 	}
826eab4e633SDave Chinner 	if (bp->b_error) {
827eab4e633SDave Chinner 		error = bp->b_error;
828eab4e633SDave Chinner 		if (loud)
829e721f504SDave Chinner 			xfs_warn(mp, "SB validate failed with error %d.", error);
830eab4e633SDave Chinner 		goto release_buf;
831eab4e633SDave Chinner 	}
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	/*
8341da177e4SLinus Torvalds 	 * Initialize the mount structure from the superblock.
8351da177e4SLinus Torvalds 	 */
83698021821SDave Chinner 	xfs_sb_from_disk(&mp->m_sb, XFS_BUF_TO_SBP(bp));
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	/*
8391da177e4SLinus Torvalds 	 * We must be able to do sector-sized and sector-aligned IO.
8401da177e4SLinus Torvalds 	 */
84104a1e6c5SDave Chinner 	if (sector_size > sbp->sb_sectsize) {
842af34e09dSDave Chinner 		if (loud)
843af34e09dSDave Chinner 			xfs_warn(mp, "device supports %u byte sectors (not %u)",
84404a1e6c5SDave Chinner 				sector_size, sbp->sb_sectsize);
8451da177e4SLinus Torvalds 		error = ENOSYS;
84626af6552SDave Chinner 		goto release_buf;
8471da177e4SLinus Torvalds 	}
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 	/*
8501da177e4SLinus Torvalds 	 * If device sector size is smaller than the superblock size,
8511da177e4SLinus Torvalds 	 * re-read the superblock so the buffer is correctly sized.
8521da177e4SLinus Torvalds 	 */
85304a1e6c5SDave Chinner 	if (sector_size < sbp->sb_sectsize) {
8541da177e4SLinus Torvalds 		xfs_buf_relse(bp);
85504a1e6c5SDave Chinner 		sector_size = sbp->sb_sectsize;
85626af6552SDave Chinner 		goto reread;
8571da177e4SLinus Torvalds 	}
8581da177e4SLinus Torvalds 
8595478eeadSLachlan McIlroy 	/* Initialize per-cpu counters */
8605478eeadSLachlan McIlroy 	xfs_icsb_reinit_counters(mp);
8618d280b98SDavid Chinner 
86204a1e6c5SDave Chinner 	/* no need to be quiet anymore, so reset the buf ops */
86304a1e6c5SDave Chinner 	bp->b_ops = &xfs_sb_buf_ops;
86404a1e6c5SDave Chinner 
8651da177e4SLinus Torvalds 	mp->m_sb_bp = bp;
86626af6552SDave Chinner 	xfs_buf_unlock(bp);
8671da177e4SLinus Torvalds 	return 0;
8681da177e4SLinus Torvalds 
86926af6552SDave Chinner release_buf:
8701da177e4SLinus Torvalds 	xfs_buf_relse(bp);
8711da177e4SLinus Torvalds 	return error;
8721da177e4SLinus Torvalds }
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds /*
8761da177e4SLinus Torvalds  * xfs_mount_common
8771da177e4SLinus Torvalds  *
8781da177e4SLinus Torvalds  * Mount initialization code establishing various mount
8791da177e4SLinus Torvalds  * fields from the superblock associated with the given
8801da177e4SLinus Torvalds  * mount structure
8811da177e4SLinus Torvalds  */
882ba0f32d4SChristoph Hellwig STATIC void
8831da177e4SLinus Torvalds xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
8841da177e4SLinus Torvalds {
8851da177e4SLinus Torvalds 	mp->m_agfrotor = mp->m_agirotor = 0;
886007c61c6SEric Sandeen 	spin_lock_init(&mp->m_agirotor_lock);
8871da177e4SLinus Torvalds 	mp->m_maxagi = mp->m_sb.sb_agcount;
8881da177e4SLinus Torvalds 	mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
8891da177e4SLinus Torvalds 	mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
8901da177e4SLinus Torvalds 	mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
8911da177e4SLinus Torvalds 	mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
8921da177e4SLinus Torvalds 	mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
8931da177e4SLinus Torvalds 	mp->m_blockmask = sbp->sb_blocksize - 1;
8941da177e4SLinus Torvalds 	mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
8951da177e4SLinus Torvalds 	mp->m_blockwmask = mp->m_blockwsize - 1;
8961da177e4SLinus Torvalds 
89760197e8dSChristoph Hellwig 	mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
89860197e8dSChristoph Hellwig 	mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
89960197e8dSChristoph Hellwig 	mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
90060197e8dSChristoph Hellwig 	mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
90160197e8dSChristoph Hellwig 
90260197e8dSChristoph Hellwig 	mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
90360197e8dSChristoph Hellwig 	mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
90460197e8dSChristoph Hellwig 	mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
90560197e8dSChristoph Hellwig 	mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
90660197e8dSChristoph Hellwig 
90760197e8dSChristoph Hellwig 	mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
90860197e8dSChristoph Hellwig 	mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
90960197e8dSChristoph Hellwig 	mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
91060197e8dSChristoph Hellwig 	mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 	mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
9131da177e4SLinus Torvalds 	mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
9141da177e4SLinus Torvalds 					sbp->sb_inopblock);
9151da177e4SLinus Torvalds 	mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
9161da177e4SLinus Torvalds }
91792821e2bSDavid Chinner 
91892821e2bSDavid Chinner /*
91992821e2bSDavid Chinner  * xfs_initialize_perag_data
92092821e2bSDavid Chinner  *
92192821e2bSDavid Chinner  * Read in each per-ag structure so we can count up the number of
92292821e2bSDavid Chinner  * allocated inodes, free inodes and used filesystem blocks as this
92392821e2bSDavid Chinner  * information is no longer persistent in the superblock. Once we have
92492821e2bSDavid Chinner  * this information, write it into the in-core superblock structure.
92592821e2bSDavid Chinner  */
92692821e2bSDavid Chinner STATIC int
92792821e2bSDavid Chinner xfs_initialize_perag_data(xfs_mount_t *mp, xfs_agnumber_t agcount)
92892821e2bSDavid Chinner {
92992821e2bSDavid Chinner 	xfs_agnumber_t	index;
93092821e2bSDavid Chinner 	xfs_perag_t	*pag;
93192821e2bSDavid Chinner 	xfs_sb_t	*sbp = &mp->m_sb;
93292821e2bSDavid Chinner 	uint64_t	ifree = 0;
93392821e2bSDavid Chinner 	uint64_t	ialloc = 0;
93492821e2bSDavid Chinner 	uint64_t	bfree = 0;
93592821e2bSDavid Chinner 	uint64_t	bfreelst = 0;
93692821e2bSDavid Chinner 	uint64_t	btree = 0;
93792821e2bSDavid Chinner 	int		error;
93892821e2bSDavid Chinner 
93992821e2bSDavid Chinner 	for (index = 0; index < agcount; index++) {
94092821e2bSDavid Chinner 		/*
94192821e2bSDavid Chinner 		 * read the agf, then the agi. This gets us
9429da096fdSMalcolm Parsons 		 * all the information we need and populates the
94392821e2bSDavid Chinner 		 * per-ag structures for us.
94492821e2bSDavid Chinner 		 */
94592821e2bSDavid Chinner 		error = xfs_alloc_pagf_init(mp, NULL, index, 0);
94692821e2bSDavid Chinner 		if (error)
94792821e2bSDavid Chinner 			return error;
94892821e2bSDavid Chinner 
94992821e2bSDavid Chinner 		error = xfs_ialloc_pagi_init(mp, NULL, index);
95092821e2bSDavid Chinner 		if (error)
95192821e2bSDavid Chinner 			return error;
95244b56e0aSDave Chinner 		pag = xfs_perag_get(mp, index);
95392821e2bSDavid Chinner 		ifree += pag->pagi_freecount;
95492821e2bSDavid Chinner 		ialloc += pag->pagi_count;
95592821e2bSDavid Chinner 		bfree += pag->pagf_freeblks;
95692821e2bSDavid Chinner 		bfreelst += pag->pagf_flcount;
95792821e2bSDavid Chinner 		btree += pag->pagf_btreeblks;
95844b56e0aSDave Chinner 		xfs_perag_put(pag);
95992821e2bSDavid Chinner 	}
96092821e2bSDavid Chinner 	/*
96192821e2bSDavid Chinner 	 * Overwrite incore superblock counters with just-read data
96292821e2bSDavid Chinner 	 */
9633685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
96492821e2bSDavid Chinner 	sbp->sb_ifree = ifree;
96592821e2bSDavid Chinner 	sbp->sb_icount = ialloc;
96692821e2bSDavid Chinner 	sbp->sb_fdblocks = bfree + bfreelst + btree;
9673685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
96892821e2bSDavid Chinner 
96992821e2bSDavid Chinner 	/* Fixup the per-cpu counters as well. */
97092821e2bSDavid Chinner 	xfs_icsb_reinit_counters(mp);
97192821e2bSDavid Chinner 
97292821e2bSDavid Chinner 	return 0;
97392821e2bSDavid Chinner }
97492821e2bSDavid Chinner 
9751da177e4SLinus Torvalds /*
9760771fb45SEric Sandeen  * Update alignment values based on mount options and sb values
9771da177e4SLinus Torvalds  */
9780771fb45SEric Sandeen STATIC int
9797884bc86SChristoph Hellwig xfs_update_alignment(xfs_mount_t *mp)
9801da177e4SLinus Torvalds {
9811da177e4SLinus Torvalds 	xfs_sb_t	*sbp = &(mp->m_sb);
9821da177e4SLinus Torvalds 
9834249023aSChristoph Hellwig 	if (mp->m_dalign) {
9841da177e4SLinus Torvalds 		/*
9851da177e4SLinus Torvalds 		 * If stripe unit and stripe width are not multiples
9861da177e4SLinus Torvalds 		 * of the fs blocksize turn off alignment.
9871da177e4SLinus Torvalds 		 */
9881da177e4SLinus Torvalds 		if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
9891da177e4SLinus Torvalds 		    (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
9901da177e4SLinus Torvalds 			if (mp->m_flags & XFS_MOUNT_RETERR) {
991c0e090ceSEric Sandeen 				xfs_warn(mp, "alignment check failed: "
992c0e090ceSEric Sandeen 					 "(sunit/swidth vs. blocksize)");
9930771fb45SEric Sandeen 				return XFS_ERROR(EINVAL);
9941da177e4SLinus Torvalds 			}
9951da177e4SLinus Torvalds 			mp->m_dalign = mp->m_swidth = 0;
9961da177e4SLinus Torvalds 		} else {
9971da177e4SLinus Torvalds 			/*
9981da177e4SLinus Torvalds 			 * Convert the stripe unit and width to FSBs.
9991da177e4SLinus Torvalds 			 */
10001da177e4SLinus Torvalds 			mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
10011da177e4SLinus Torvalds 			if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
10021da177e4SLinus Torvalds 				if (mp->m_flags & XFS_MOUNT_RETERR) {
1003c0e090ceSEric Sandeen 					xfs_warn(mp, "alignment check failed: "
1004c0e090ceSEric Sandeen 						 "(sunit/swidth vs. ag size)");
10050771fb45SEric Sandeen 					return XFS_ERROR(EINVAL);
10061da177e4SLinus Torvalds 				}
100753487786SDave Chinner 				xfs_warn(mp,
100853487786SDave Chinner 		"stripe alignment turned off: sunit(%d)/swidth(%d) "
100953487786SDave Chinner 		"incompatible with agsize(%d)",
10101da177e4SLinus Torvalds 					mp->m_dalign, mp->m_swidth,
10111da177e4SLinus Torvalds 					sbp->sb_agblocks);
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds 				mp->m_dalign = 0;
10141da177e4SLinus Torvalds 				mp->m_swidth = 0;
10151da177e4SLinus Torvalds 			} else if (mp->m_dalign) {
10161da177e4SLinus Torvalds 				mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
10171da177e4SLinus Torvalds 			} else {
10181da177e4SLinus Torvalds 				if (mp->m_flags & XFS_MOUNT_RETERR) {
1019c0e090ceSEric Sandeen 					xfs_warn(mp, "alignment check failed: "
1020c0e090ceSEric Sandeen 						"sunit(%d) less than bsize(%d)",
10211da177e4SLinus Torvalds 						mp->m_dalign,
10221da177e4SLinus Torvalds 						mp->m_blockmask +1);
10230771fb45SEric Sandeen 					return XFS_ERROR(EINVAL);
10241da177e4SLinus Torvalds 				}
10251da177e4SLinus Torvalds 				mp->m_swidth = 0;
10261da177e4SLinus Torvalds 			}
10271da177e4SLinus Torvalds 		}
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 		/*
10301da177e4SLinus Torvalds 		 * Update superblock with new values
10311da177e4SLinus Torvalds 		 * and log changes
10321da177e4SLinus Torvalds 		 */
103362118709SEric Sandeen 		if (xfs_sb_version_hasdalign(sbp)) {
10341da177e4SLinus Torvalds 			if (sbp->sb_unit != mp->m_dalign) {
10351da177e4SLinus Torvalds 				sbp->sb_unit = mp->m_dalign;
10367884bc86SChristoph Hellwig 				mp->m_update_flags |= XFS_SB_UNIT;
10371da177e4SLinus Torvalds 			}
10381da177e4SLinus Torvalds 			if (sbp->sb_width != mp->m_swidth) {
10391da177e4SLinus Torvalds 				sbp->sb_width = mp->m_swidth;
10407884bc86SChristoph Hellwig 				mp->m_update_flags |= XFS_SB_WIDTH;
10411da177e4SLinus Torvalds 			}
1042*34d7f603SJie Liu 		} else {
1043*34d7f603SJie Liu 			xfs_warn(mp,
1044*34d7f603SJie Liu 	"cannot change alignment: superblock does not support data alignment");
1045*34d7f603SJie Liu 			return XFS_ERROR(EINVAL);
10461da177e4SLinus Torvalds 		}
10471da177e4SLinus Torvalds 	} else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
104862118709SEric Sandeen 		    xfs_sb_version_hasdalign(&mp->m_sb)) {
10491da177e4SLinus Torvalds 			mp->m_dalign = sbp->sb_unit;
10501da177e4SLinus Torvalds 			mp->m_swidth = sbp->sb_width;
10511da177e4SLinus Torvalds 	}
10521da177e4SLinus Torvalds 
10530771fb45SEric Sandeen 	return 0;
10540771fb45SEric Sandeen }
10551da177e4SLinus Torvalds 
10560771fb45SEric Sandeen /*
10570771fb45SEric Sandeen  * Set the maximum inode count for this filesystem
10580771fb45SEric Sandeen  */
10590771fb45SEric Sandeen STATIC void
10600771fb45SEric Sandeen xfs_set_maxicount(xfs_mount_t *mp)
10610771fb45SEric Sandeen {
10620771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
10631da177e4SLinus Torvalds 	__uint64_t	icount;
10641da177e4SLinus Torvalds 
10650771fb45SEric Sandeen 	if (sbp->sb_imax_pct) {
10660771fb45SEric Sandeen 		/*
10670771fb45SEric Sandeen 		 * Make sure the maximum inode count is a multiple
10680771fb45SEric Sandeen 		 * of the units we allocate inodes in.
10691da177e4SLinus Torvalds 		 */
10701da177e4SLinus Torvalds 		icount = sbp->sb_dblocks * sbp->sb_imax_pct;
10711da177e4SLinus Torvalds 		do_div(icount, 100);
10721da177e4SLinus Torvalds 		do_div(icount, mp->m_ialloc_blks);
10731da177e4SLinus Torvalds 		mp->m_maxicount = (icount * mp->m_ialloc_blks)  <<
10741da177e4SLinus Torvalds 				   sbp->sb_inopblog;
10750771fb45SEric Sandeen 	} else {
10761da177e4SLinus Torvalds 		mp->m_maxicount = 0;
10771da177e4SLinus Torvalds 	}
10781da177e4SLinus Torvalds }
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds /*
10811da177e4SLinus Torvalds  * Set the default minimum read and write sizes unless
10821da177e4SLinus Torvalds  * already specified in a mount option.
10831da177e4SLinus Torvalds  * We use smaller I/O sizes when the file system
10841da177e4SLinus Torvalds  * is being used for NFS service (wsync mount option).
10851da177e4SLinus Torvalds  */
10860771fb45SEric Sandeen STATIC void
10870771fb45SEric Sandeen xfs_set_rw_sizes(xfs_mount_t *mp)
10880771fb45SEric Sandeen {
10890771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
10900771fb45SEric Sandeen 	int		readio_log, writeio_log;
10910771fb45SEric Sandeen 
10921da177e4SLinus Torvalds 	if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
10931da177e4SLinus Torvalds 		if (mp->m_flags & XFS_MOUNT_WSYNC) {
10941da177e4SLinus Torvalds 			readio_log = XFS_WSYNC_READIO_LOG;
10951da177e4SLinus Torvalds 			writeio_log = XFS_WSYNC_WRITEIO_LOG;
10961da177e4SLinus Torvalds 		} else {
10971da177e4SLinus Torvalds 			readio_log = XFS_READIO_LOG_LARGE;
10981da177e4SLinus Torvalds 			writeio_log = XFS_WRITEIO_LOG_LARGE;
10991da177e4SLinus Torvalds 		}
11001da177e4SLinus Torvalds 	} else {
11011da177e4SLinus Torvalds 		readio_log = mp->m_readio_log;
11021da177e4SLinus Torvalds 		writeio_log = mp->m_writeio_log;
11031da177e4SLinus Torvalds 	}
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds 	if (sbp->sb_blocklog > readio_log) {
11061da177e4SLinus Torvalds 		mp->m_readio_log = sbp->sb_blocklog;
11071da177e4SLinus Torvalds 	} else {
11081da177e4SLinus Torvalds 		mp->m_readio_log = readio_log;
11091da177e4SLinus Torvalds 	}
11101da177e4SLinus Torvalds 	mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
11111da177e4SLinus Torvalds 	if (sbp->sb_blocklog > writeio_log) {
11121da177e4SLinus Torvalds 		mp->m_writeio_log = sbp->sb_blocklog;
11131da177e4SLinus Torvalds 	} else {
11141da177e4SLinus Torvalds 		mp->m_writeio_log = writeio_log;
11151da177e4SLinus Torvalds 	}
11161da177e4SLinus Torvalds 	mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
11170771fb45SEric Sandeen }
1118425f9dddSEric Sandeen 
11191da177e4SLinus Torvalds /*
1120055388a3SDave Chinner  * precalculate the low space thresholds for dynamic speculative preallocation.
1121055388a3SDave Chinner  */
1122055388a3SDave Chinner void
1123055388a3SDave Chinner xfs_set_low_space_thresholds(
1124055388a3SDave Chinner 	struct xfs_mount	*mp)
1125055388a3SDave Chinner {
1126055388a3SDave Chinner 	int i;
1127055388a3SDave Chinner 
1128055388a3SDave Chinner 	for (i = 0; i < XFS_LOWSP_MAX; i++) {
1129055388a3SDave Chinner 		__uint64_t space = mp->m_sb.sb_dblocks;
1130055388a3SDave Chinner 
1131055388a3SDave Chinner 		do_div(space, 100);
1132055388a3SDave Chinner 		mp->m_low_space[i] = space * (i + 1);
1133055388a3SDave Chinner 	}
1134055388a3SDave Chinner }
1135055388a3SDave Chinner 
1136055388a3SDave Chinner 
1137055388a3SDave Chinner /*
11381da177e4SLinus Torvalds  * Set whether we're using inode alignment.
11391da177e4SLinus Torvalds  */
11400771fb45SEric Sandeen STATIC void
11410771fb45SEric Sandeen xfs_set_inoalignment(xfs_mount_t *mp)
11420771fb45SEric Sandeen {
114362118709SEric Sandeen 	if (xfs_sb_version_hasalign(&mp->m_sb) &&
11441da177e4SLinus Torvalds 	    mp->m_sb.sb_inoalignmt >=
11451da177e4SLinus Torvalds 	    XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
11461da177e4SLinus Torvalds 		mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
11471da177e4SLinus Torvalds 	else
11481da177e4SLinus Torvalds 		mp->m_inoalign_mask = 0;
11491da177e4SLinus Torvalds 	/*
11501da177e4SLinus Torvalds 	 * If we are using stripe alignment, check whether
11511da177e4SLinus Torvalds 	 * the stripe unit is a multiple of the inode alignment
11521da177e4SLinus Torvalds 	 */
11531da177e4SLinus Torvalds 	if (mp->m_dalign && mp->m_inoalign_mask &&
11541da177e4SLinus Torvalds 	    !(mp->m_dalign & mp->m_inoalign_mask))
11551da177e4SLinus Torvalds 		mp->m_sinoalign = mp->m_dalign;
11561da177e4SLinus Torvalds 	else
11571da177e4SLinus Torvalds 		mp->m_sinoalign = 0;
11580771fb45SEric Sandeen }
11590771fb45SEric Sandeen 
11601da177e4SLinus Torvalds /*
11611da177e4SLinus Torvalds  * Check that the data (and log if separate) are an ok size.
11621da177e4SLinus Torvalds  */
11630771fb45SEric Sandeen STATIC int
11644249023aSChristoph Hellwig xfs_check_sizes(xfs_mount_t *mp)
11650771fb45SEric Sandeen {
11660771fb45SEric Sandeen 	xfs_buf_t	*bp;
11670771fb45SEric Sandeen 	xfs_daddr_t	d;
11680771fb45SEric Sandeen 
11691da177e4SLinus Torvalds 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
11701da177e4SLinus Torvalds 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
11710b932cccSDave Chinner 		xfs_warn(mp, "filesystem size mismatch detected");
1172657a4cffSEric Sandeen 		return XFS_ERROR(EFBIG);
11731da177e4SLinus Torvalds 	}
1174e70b73f8SDave Chinner 	bp = xfs_buf_read_uncached(mp->m_ddev_targp,
11751da177e4SLinus Torvalds 					d - XFS_FSS_TO_BB(mp, 1),
1176c3f8fc73SDave Chinner 					XFS_FSS_TO_BB(mp, 1), 0, NULL);
11771922c949SDave Chinner 	if (!bp) {
11780b932cccSDave Chinner 		xfs_warn(mp, "last sector read failed");
11791922c949SDave Chinner 		return EIO;
11801da177e4SLinus Torvalds 	}
11811922c949SDave Chinner 	xfs_buf_relse(bp);
11821da177e4SLinus Torvalds 
11834249023aSChristoph Hellwig 	if (mp->m_logdev_targp != mp->m_ddev_targp) {
11841da177e4SLinus Torvalds 		d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
11851da177e4SLinus Torvalds 		if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
11860b932cccSDave Chinner 			xfs_warn(mp, "log size mismatch detected");
1187657a4cffSEric Sandeen 			return XFS_ERROR(EFBIG);
11881da177e4SLinus Torvalds 		}
1189e70b73f8SDave Chinner 		bp = xfs_buf_read_uncached(mp->m_logdev_targp,
11901da177e4SLinus Torvalds 					d - XFS_FSB_TO_BB(mp, 1),
1191c3f8fc73SDave Chinner 					XFS_FSB_TO_BB(mp, 1), 0, NULL);
11921922c949SDave Chinner 		if (!bp) {
11930b932cccSDave Chinner 			xfs_warn(mp, "log device read failed");
11941922c949SDave Chinner 			return EIO;
11951da177e4SLinus Torvalds 		}
11961922c949SDave Chinner 		xfs_buf_relse(bp);
11970771fb45SEric Sandeen 	}
11980771fb45SEric Sandeen 	return 0;
11990771fb45SEric Sandeen }
12000771fb45SEric Sandeen 
12010771fb45SEric Sandeen /*
12027d095257SChristoph Hellwig  * Clear the quotaflags in memory and in the superblock.
12037d095257SChristoph Hellwig  */
12047d095257SChristoph Hellwig int
12057d095257SChristoph Hellwig xfs_mount_reset_sbqflags(
12067d095257SChristoph Hellwig 	struct xfs_mount	*mp)
12077d095257SChristoph Hellwig {
12087d095257SChristoph Hellwig 	int			error;
12097d095257SChristoph Hellwig 	struct xfs_trans	*tp;
12107d095257SChristoph Hellwig 
12117d095257SChristoph Hellwig 	mp->m_qflags = 0;
12127d095257SChristoph Hellwig 
12137d095257SChristoph Hellwig 	/*
12147d095257SChristoph Hellwig 	 * It is OK to look at sb_qflags here in mount path,
12157d095257SChristoph Hellwig 	 * without m_sb_lock.
12167d095257SChristoph Hellwig 	 */
12177d095257SChristoph Hellwig 	if (mp->m_sb.sb_qflags == 0)
12187d095257SChristoph Hellwig 		return 0;
12197d095257SChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
12207d095257SChristoph Hellwig 	mp->m_sb.sb_qflags = 0;
12217d095257SChristoph Hellwig 	spin_unlock(&mp->m_sb_lock);
12227d095257SChristoph Hellwig 
12237d095257SChristoph Hellwig 	/*
12247d095257SChristoph Hellwig 	 * If the fs is readonly, let the incore superblock run
12257d095257SChristoph Hellwig 	 * with quotas off but don't flush the update out to disk
12267d095257SChristoph Hellwig 	 */
12277d095257SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_RDONLY)
12287d095257SChristoph Hellwig 		return 0;
12297d095257SChristoph Hellwig 
12307d095257SChristoph Hellwig 	tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
1231b0c10b98SJeff Liu 	error = xfs_trans_reserve(tp, 0, XFS_QM_SBCHANGE_LOG_RES(mp),
1232b0c10b98SJeff Liu 				  0, 0, XFS_DEFAULT_LOG_COUNT);
12337d095257SChristoph Hellwig 	if (error) {
12347d095257SChristoph Hellwig 		xfs_trans_cancel(tp, 0);
123553487786SDave Chinner 		xfs_alert(mp, "%s: Superblock update failed!", __func__);
12367d095257SChristoph Hellwig 		return error;
12377d095257SChristoph Hellwig 	}
12387d095257SChristoph Hellwig 
12397d095257SChristoph Hellwig 	xfs_mod_sb(tp, XFS_SB_QFLAGS);
12407d095257SChristoph Hellwig 	return xfs_trans_commit(tp, 0);
12417d095257SChristoph Hellwig }
12427d095257SChristoph Hellwig 
1243d5db0f97SEric Sandeen __uint64_t
1244d5db0f97SEric Sandeen xfs_default_resblks(xfs_mount_t *mp)
1245d5db0f97SEric Sandeen {
1246d5db0f97SEric Sandeen 	__uint64_t resblks;
1247d5db0f97SEric Sandeen 
1248d5db0f97SEric Sandeen 	/*
12498babd8a2SDave Chinner 	 * We default to 5% or 8192 fsbs of space reserved, whichever is
12508babd8a2SDave Chinner 	 * smaller.  This is intended to cover concurrent allocation
12518babd8a2SDave Chinner 	 * transactions when we initially hit enospc. These each require a 4
12528babd8a2SDave Chinner 	 * block reservation. Hence by default we cover roughly 2000 concurrent
12538babd8a2SDave Chinner 	 * allocation reservations.
1254d5db0f97SEric Sandeen 	 */
1255d5db0f97SEric Sandeen 	resblks = mp->m_sb.sb_dblocks;
1256d5db0f97SEric Sandeen 	do_div(resblks, 20);
12578babd8a2SDave Chinner 	resblks = min_t(__uint64_t, resblks, 8192);
1258d5db0f97SEric Sandeen 	return resblks;
1259d5db0f97SEric Sandeen }
1260d5db0f97SEric Sandeen 
12617d095257SChristoph Hellwig /*
12620771fb45SEric Sandeen  * This function does the following on an initial mount of a file system:
12630771fb45SEric Sandeen  *	- reads the superblock from disk and init the mount struct
12640771fb45SEric Sandeen  *	- if we're a 32-bit kernel, do a size check on the superblock
12650771fb45SEric Sandeen  *		so we don't mount terabyte filesystems
12660771fb45SEric Sandeen  *	- init mount struct realtime fields
12670771fb45SEric Sandeen  *	- allocate inode hash table for fs
12680771fb45SEric Sandeen  *	- init directory manager
12690771fb45SEric Sandeen  *	- perform recovery and init the log manager
12700771fb45SEric Sandeen  */
12710771fb45SEric Sandeen int
12720771fb45SEric Sandeen xfs_mountfs(
12734249023aSChristoph Hellwig 	xfs_mount_t	*mp)
12740771fb45SEric Sandeen {
12750771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
12760771fb45SEric Sandeen 	xfs_inode_t	*rip;
12770771fb45SEric Sandeen 	__uint64_t	resblks;
12787d095257SChristoph Hellwig 	uint		quotamount = 0;
12797d095257SChristoph Hellwig 	uint		quotaflags = 0;
12800771fb45SEric Sandeen 	int		error = 0;
12810771fb45SEric Sandeen 
12820771fb45SEric Sandeen 	xfs_mount_common(mp, sbp);
12830771fb45SEric Sandeen 
12840771fb45SEric Sandeen 	/*
1285e6957ea4SEric Sandeen 	 * Check for a mismatched features2 values.  Older kernels
1286e6957ea4SEric Sandeen 	 * read & wrote into the wrong sb offset for sb_features2
1287e6957ea4SEric Sandeen 	 * on some platforms due to xfs_sb_t not being 64bit size aligned
1288e6957ea4SEric Sandeen 	 * when sb_features2 was added, which made older superblock
1289e6957ea4SEric Sandeen 	 * reading/writing routines swap it as a 64-bit value.
1290ee1c0908SDavid Chinner 	 *
1291e6957ea4SEric Sandeen 	 * For backwards compatibility, we make both slots equal.
1292e6957ea4SEric Sandeen 	 *
1293e6957ea4SEric Sandeen 	 * If we detect a mismatched field, we OR the set bits into the
1294e6957ea4SEric Sandeen 	 * existing features2 field in case it has already been modified; we
1295e6957ea4SEric Sandeen 	 * don't want to lose any features.  We then update the bad location
1296e6957ea4SEric Sandeen 	 * with the ORed value so that older kernels will see any features2
1297e6957ea4SEric Sandeen 	 * flags, and mark the two fields as needing updates once the
1298e6957ea4SEric Sandeen 	 * transaction subsystem is online.
1299ee1c0908SDavid Chinner 	 */
1300e6957ea4SEric Sandeen 	if (xfs_sb_has_mismatched_features2(sbp)) {
13010b932cccSDave Chinner 		xfs_warn(mp, "correcting sb_features alignment problem");
1302ee1c0908SDavid Chinner 		sbp->sb_features2 |= sbp->sb_bad_features2;
1303e6957ea4SEric Sandeen 		sbp->sb_bad_features2 = sbp->sb_features2;
13047884bc86SChristoph Hellwig 		mp->m_update_flags |= XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2;
1305e6957ea4SEric Sandeen 
1306e6957ea4SEric Sandeen 		/*
1307e6957ea4SEric Sandeen 		 * Re-check for ATTR2 in case it was found in bad_features2
1308e6957ea4SEric Sandeen 		 * slot.
1309e6957ea4SEric Sandeen 		 */
13107c12f296STim Shimmin 		if (xfs_sb_version_hasattr2(&mp->m_sb) &&
13117c12f296STim Shimmin 		   !(mp->m_flags & XFS_MOUNT_NOATTR2))
1312e6957ea4SEric Sandeen 			mp->m_flags |= XFS_MOUNT_ATTR2;
13137c12f296STim Shimmin 	}
1314e6957ea4SEric Sandeen 
13157c12f296STim Shimmin 	if (xfs_sb_version_hasattr2(&mp->m_sb) &&
13167c12f296STim Shimmin 	   (mp->m_flags & XFS_MOUNT_NOATTR2)) {
13177c12f296STim Shimmin 		xfs_sb_version_removeattr2(&mp->m_sb);
13187884bc86SChristoph Hellwig 		mp->m_update_flags |= XFS_SB_FEATURES2;
13197c12f296STim Shimmin 
13207c12f296STim Shimmin 		/* update sb_versionnum for the clearing of the morebits */
13217c12f296STim Shimmin 		if (!sbp->sb_features2)
13227884bc86SChristoph Hellwig 			mp->m_update_flags |= XFS_SB_VERSIONNUM;
1323ee1c0908SDavid Chinner 	}
1324ee1c0908SDavid Chinner 
1325ee1c0908SDavid Chinner 	/*
13260771fb45SEric Sandeen 	 * Check if sb_agblocks is aligned at stripe boundary
13270771fb45SEric Sandeen 	 * If sb_agblocks is NOT aligned turn off m_dalign since
13280771fb45SEric Sandeen 	 * allocator alignment is within an ag, therefore ag has
13290771fb45SEric Sandeen 	 * to be aligned at stripe boundary.
13300771fb45SEric Sandeen 	 */
13317884bc86SChristoph Hellwig 	error = xfs_update_alignment(mp);
13320771fb45SEric Sandeen 	if (error)
1333f9057e3dSChristoph Hellwig 		goto out;
13340771fb45SEric Sandeen 
13350771fb45SEric Sandeen 	xfs_alloc_compute_maxlevels(mp);
13360771fb45SEric Sandeen 	xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
13370771fb45SEric Sandeen 	xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
13380771fb45SEric Sandeen 	xfs_ialloc_compute_maxlevels(mp);
13390771fb45SEric Sandeen 
13400771fb45SEric Sandeen 	xfs_set_maxicount(mp);
13410771fb45SEric Sandeen 
134227174203SChristoph Hellwig 	error = xfs_uuid_mount(mp);
134327174203SChristoph Hellwig 	if (error)
1344f9057e3dSChristoph Hellwig 		goto out;
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 	/*
13470771fb45SEric Sandeen 	 * Set the minimum read and write sizes
13480771fb45SEric Sandeen 	 */
13490771fb45SEric Sandeen 	xfs_set_rw_sizes(mp);
13500771fb45SEric Sandeen 
1351055388a3SDave Chinner 	/* set the low space thresholds for dynamic preallocation */
1352055388a3SDave Chinner 	xfs_set_low_space_thresholds(mp);
1353055388a3SDave Chinner 
13540771fb45SEric Sandeen 	/*
13550771fb45SEric Sandeen 	 * Set the inode cluster size.
13560771fb45SEric Sandeen 	 * This may still be overridden by the file system
13570771fb45SEric Sandeen 	 * block size if it is larger than the chosen cluster size.
13580771fb45SEric Sandeen 	 */
13590771fb45SEric Sandeen 	mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
13600771fb45SEric Sandeen 
13610771fb45SEric Sandeen 	/*
13620771fb45SEric Sandeen 	 * Set inode alignment fields
13630771fb45SEric Sandeen 	 */
13640771fb45SEric Sandeen 	xfs_set_inoalignment(mp);
13650771fb45SEric Sandeen 
13660771fb45SEric Sandeen 	/*
13670771fb45SEric Sandeen 	 * Check that the data (and log if separate) are an ok size.
13680771fb45SEric Sandeen 	 */
13694249023aSChristoph Hellwig 	error = xfs_check_sizes(mp);
13700771fb45SEric Sandeen 	if (error)
1371f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
13720771fb45SEric Sandeen 
13730771fb45SEric Sandeen 	/*
13741da177e4SLinus Torvalds 	 * Initialize realtime fields in the mount structure
13751da177e4SLinus Torvalds 	 */
13760771fb45SEric Sandeen 	error = xfs_rtmount_init(mp);
13770771fb45SEric Sandeen 	if (error) {
13780b932cccSDave Chinner 		xfs_warn(mp, "RT mount failed");
1379f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
13801da177e4SLinus Torvalds 	}
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 	/*
13831da177e4SLinus Torvalds 	 *  Copies the low order bits of the timestamp and the randomly
13841da177e4SLinus Torvalds 	 *  set "sequence" number out of a UUID.
13851da177e4SLinus Torvalds 	 */
13861da177e4SLinus Torvalds 	uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 	mp->m_dmevmask = 0;	/* not persistent; set after each mount */
13891da177e4SLinus Torvalds 
1390f6c2d1faSNathan Scott 	xfs_dir_mount(mp);
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds 	/*
13931da177e4SLinus Torvalds 	 * Initialize the attribute manager's entries.
13941da177e4SLinus Torvalds 	 */
13951da177e4SLinus Torvalds 	mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds 	/*
13981da177e4SLinus Torvalds 	 * Initialize the precomputed transaction reservations values.
13991da177e4SLinus Torvalds 	 */
14001da177e4SLinus Torvalds 	xfs_trans_init(mp);
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds 	/*
14031da177e4SLinus Torvalds 	 * Allocate and initialize the per-ag data.
14041da177e4SLinus Torvalds 	 */
14051c1c6ebcSDave Chinner 	spin_lock_init(&mp->m_perag_lock);
14069b98b6f3SDave Chinner 	INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
14071c1c6ebcSDave Chinner 	error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
14081c1c6ebcSDave Chinner 	if (error) {
14090b932cccSDave Chinner 		xfs_warn(mp, "Failed per-ag init: %d", error);
1410f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
14111c1c6ebcSDave Chinner 	}
14121da177e4SLinus Torvalds 
1413f9057e3dSChristoph Hellwig 	if (!sbp->sb_logblocks) {
14140b932cccSDave Chinner 		xfs_warn(mp, "no log defined");
1415f9057e3dSChristoph Hellwig 		XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp);
1416f9057e3dSChristoph Hellwig 		error = XFS_ERROR(EFSCORRUPTED);
1417f9057e3dSChristoph Hellwig 		goto out_free_perag;
1418f9057e3dSChristoph Hellwig 	}
1419f9057e3dSChristoph Hellwig 
14201da177e4SLinus Torvalds 	/*
14211da177e4SLinus Torvalds 	 * log's mount-time initialization. Perform 1st part recovery if needed
14221da177e4SLinus Torvalds 	 */
14231da177e4SLinus Torvalds 	error = xfs_log_mount(mp, mp->m_logdev_targp,
14241da177e4SLinus Torvalds 			      XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
14251da177e4SLinus Torvalds 			      XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
14261da177e4SLinus Torvalds 	if (error) {
14270b932cccSDave Chinner 		xfs_warn(mp, "log mount failed");
1428d4f3512bSDave Chinner 		goto out_fail_wait;
14291da177e4SLinus Torvalds 	}
14301da177e4SLinus Torvalds 
14311da177e4SLinus Torvalds 	/*
143292821e2bSDavid Chinner 	 * Now the log is mounted, we know if it was an unclean shutdown or
143392821e2bSDavid Chinner 	 * not. If it was, with the first phase of recovery has completed, we
143492821e2bSDavid Chinner 	 * have consistent AG blocks on disk. We have not recovered EFIs yet,
143592821e2bSDavid Chinner 	 * but they are recovered transactionally in the second recovery phase
143692821e2bSDavid Chinner 	 * later.
143792821e2bSDavid Chinner 	 *
143892821e2bSDavid Chinner 	 * Hence we can safely re-initialise incore superblock counters from
143992821e2bSDavid Chinner 	 * the per-ag data. These may not be correct if the filesystem was not
144092821e2bSDavid Chinner 	 * cleanly unmounted, so we need to wait for recovery to finish before
144192821e2bSDavid Chinner 	 * doing this.
144292821e2bSDavid Chinner 	 *
144392821e2bSDavid Chinner 	 * If the filesystem was cleanly unmounted, then we can trust the
144492821e2bSDavid Chinner 	 * values in the superblock to be correct and we don't need to do
144592821e2bSDavid Chinner 	 * anything here.
144692821e2bSDavid Chinner 	 *
144792821e2bSDavid Chinner 	 * If we are currently making the filesystem, the initialisation will
144892821e2bSDavid Chinner 	 * fail as the perag data is in an undefined state.
144992821e2bSDavid Chinner 	 */
145092821e2bSDavid Chinner 	if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
145192821e2bSDavid Chinner 	    !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
145292821e2bSDavid Chinner 	     !mp->m_sb.sb_inprogress) {
145392821e2bSDavid Chinner 		error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
1454f9057e3dSChristoph Hellwig 		if (error)
1455d4f3512bSDave Chinner 			goto out_fail_wait;
145692821e2bSDavid Chinner 	}
1457f9057e3dSChristoph Hellwig 
145892821e2bSDavid Chinner 	/*
14591da177e4SLinus Torvalds 	 * Get and sanity-check the root inode.
14601da177e4SLinus Torvalds 	 * Save the pointer to it in the mount structure.
14611da177e4SLinus Torvalds 	 */
14627b6259e7SDave Chinner 	error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip);
14631da177e4SLinus Torvalds 	if (error) {
14640b932cccSDave Chinner 		xfs_warn(mp, "failed to read root inode");
1465f9057e3dSChristoph Hellwig 		goto out_log_dealloc;
14661da177e4SLinus Torvalds 	}
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds 	ASSERT(rip != NULL);
14691da177e4SLinus Torvalds 
1470abbede1bSAl Viro 	if (unlikely(!S_ISDIR(rip->i_d.di_mode))) {
14710b932cccSDave Chinner 		xfs_warn(mp, "corrupted root inode %llu: not a directory",
1472b6574520SNathan Scott 			(unsigned long long)rip->i_ino);
14731da177e4SLinus Torvalds 		xfs_iunlock(rip, XFS_ILOCK_EXCL);
14741da177e4SLinus Torvalds 		XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
14751da177e4SLinus Torvalds 				 mp);
14761da177e4SLinus Torvalds 		error = XFS_ERROR(EFSCORRUPTED);
1477f9057e3dSChristoph Hellwig 		goto out_rele_rip;
14781da177e4SLinus Torvalds 	}
14791da177e4SLinus Torvalds 	mp->m_rootip = rip;	/* save it */
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds 	xfs_iunlock(rip, XFS_ILOCK_EXCL);
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds 	/*
14841da177e4SLinus Torvalds 	 * Initialize realtime inode pointers in the mount structure
14851da177e4SLinus Torvalds 	 */
14860771fb45SEric Sandeen 	error = xfs_rtmount_inodes(mp);
14870771fb45SEric Sandeen 	if (error) {
14881da177e4SLinus Torvalds 		/*
14891da177e4SLinus Torvalds 		 * Free up the root inode.
14901da177e4SLinus Torvalds 		 */
14910b932cccSDave Chinner 		xfs_warn(mp, "failed to read RT inodes");
1492f9057e3dSChristoph Hellwig 		goto out_rele_rip;
14931da177e4SLinus Torvalds 	}
14941da177e4SLinus Torvalds 
14951da177e4SLinus Torvalds 	/*
14967884bc86SChristoph Hellwig 	 * If this is a read-only mount defer the superblock updates until
14977884bc86SChristoph Hellwig 	 * the next remount into writeable mode.  Otherwise we would never
14987884bc86SChristoph Hellwig 	 * perform the update e.g. for the root filesystem.
14991da177e4SLinus Torvalds 	 */
15007884bc86SChristoph Hellwig 	if (mp->m_update_flags && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
15017884bc86SChristoph Hellwig 		error = xfs_mount_log_sb(mp, mp->m_update_flags);
1502e5720eecSDavid Chinner 		if (error) {
15030b932cccSDave Chinner 			xfs_warn(mp, "failed to write sb changes");
1504b93b6e43SChristoph Hellwig 			goto out_rtunmount;
1505e5720eecSDavid Chinner 		}
1506e5720eecSDavid Chinner 	}
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds 	/*
15091da177e4SLinus Torvalds 	 * Initialise the XFS quota management subsystem for this mount
15101da177e4SLinus Torvalds 	 */
15117d095257SChristoph Hellwig 	if (XFS_IS_QUOTA_RUNNING(mp)) {
15127d095257SChristoph Hellwig 		error = xfs_qm_newmount(mp, &quotamount, &quotaflags);
15130771fb45SEric Sandeen 		if (error)
1514b93b6e43SChristoph Hellwig 			goto out_rtunmount;
15157d095257SChristoph Hellwig 	} else {
15167d095257SChristoph Hellwig 		ASSERT(!XFS_IS_QUOTA_ON(mp));
15177d095257SChristoph Hellwig 
15187d095257SChristoph Hellwig 		/*
15197d095257SChristoph Hellwig 		 * If a file system had quotas running earlier, but decided to
15207d095257SChristoph Hellwig 		 * mount without -o uquota/pquota/gquota options, revoke the
15217d095257SChristoph Hellwig 		 * quotachecked license.
15227d095257SChristoph Hellwig 		 */
15237d095257SChristoph Hellwig 		if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) {
15240b932cccSDave Chinner 			xfs_notice(mp, "resetting quota flags");
15257d095257SChristoph Hellwig 			error = xfs_mount_reset_sbqflags(mp);
15267d095257SChristoph Hellwig 			if (error)
15277d095257SChristoph Hellwig 				return error;
15287d095257SChristoph Hellwig 		}
15297d095257SChristoph Hellwig 	}
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 	/*
15321da177e4SLinus Torvalds 	 * Finish recovering the file system.  This part needed to be
15331da177e4SLinus Torvalds 	 * delayed until after the root and real-time bitmap inodes
15341da177e4SLinus Torvalds 	 * were consistently read in.
15351da177e4SLinus Torvalds 	 */
15364249023aSChristoph Hellwig 	error = xfs_log_mount_finish(mp);
15371da177e4SLinus Torvalds 	if (error) {
15380b932cccSDave Chinner 		xfs_warn(mp, "log mount finish failed");
1539b93b6e43SChristoph Hellwig 		goto out_rtunmount;
15401da177e4SLinus Torvalds 	}
15411da177e4SLinus Torvalds 
15421da177e4SLinus Torvalds 	/*
15431da177e4SLinus Torvalds 	 * Complete the quota initialisation, post-log-replay component.
15441da177e4SLinus Torvalds 	 */
15457d095257SChristoph Hellwig 	if (quotamount) {
15467d095257SChristoph Hellwig 		ASSERT(mp->m_qflags == 0);
15477d095257SChristoph Hellwig 		mp->m_qflags = quotaflags;
15487d095257SChristoph Hellwig 
15497d095257SChristoph Hellwig 		xfs_qm_mount_quotas(mp);
15507d095257SChristoph Hellwig 	}
15517d095257SChristoph Hellwig 
155284e1e99fSDavid Chinner 	/*
155384e1e99fSDavid Chinner 	 * Now we are mounted, reserve a small amount of unused space for
155484e1e99fSDavid Chinner 	 * privileged transactions. This is needed so that transaction
155584e1e99fSDavid Chinner 	 * space required for critical operations can dip into this pool
155684e1e99fSDavid Chinner 	 * when at ENOSPC. This is needed for operations like create with
155784e1e99fSDavid Chinner 	 * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
155884e1e99fSDavid Chinner 	 * are not allowed to use this reserved space.
15598babd8a2SDave Chinner 	 *
15608babd8a2SDave Chinner 	 * This may drive us straight to ENOSPC on mount, but that implies
15618babd8a2SDave Chinner 	 * we were already there on the last unmount. Warn if this occurs.
156284e1e99fSDavid Chinner 	 */
1563d5db0f97SEric Sandeen 	if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
1564d5db0f97SEric Sandeen 		resblks = xfs_default_resblks(mp);
1565714082bcSDavid Chinner 		error = xfs_reserve_blocks(mp, &resblks, NULL);
1566714082bcSDavid Chinner 		if (error)
15670b932cccSDave Chinner 			xfs_warn(mp,
15680b932cccSDave Chinner 	"Unable to allocate reserve blocks. Continuing without reserve pool.");
1569d5db0f97SEric Sandeen 	}
157084e1e99fSDavid Chinner 
15711da177e4SLinus Torvalds 	return 0;
15721da177e4SLinus Torvalds 
1573b93b6e43SChristoph Hellwig  out_rtunmount:
1574b93b6e43SChristoph Hellwig 	xfs_rtunmount_inodes(mp);
1575f9057e3dSChristoph Hellwig  out_rele_rip:
157643355099SChristoph Hellwig 	IRELE(rip);
1577f9057e3dSChristoph Hellwig  out_log_dealloc:
157821b699c8SChristoph Hellwig 	xfs_log_unmount(mp);
1579d4f3512bSDave Chinner  out_fail_wait:
1580d4f3512bSDave Chinner 	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
1581d4f3512bSDave Chinner 		xfs_wait_buftarg(mp->m_logdev_targp);
1582d4f3512bSDave Chinner 	xfs_wait_buftarg(mp->m_ddev_targp);
1583f9057e3dSChristoph Hellwig  out_free_perag:
1584ff4f038cSChristoph Hellwig 	xfs_free_perag(mp);
1585f9057e3dSChristoph Hellwig  out_remove_uuid:
158627174203SChristoph Hellwig 	xfs_uuid_unmount(mp);
1587f9057e3dSChristoph Hellwig  out:
15881da177e4SLinus Torvalds 	return error;
15891da177e4SLinus Torvalds }
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds /*
15921da177e4SLinus Torvalds  * This flushes out the inodes,dquots and the superblock, unmounts the
15931da177e4SLinus Torvalds  * log and makes sure that incore structures are freed.
15941da177e4SLinus Torvalds  */
159541b5c2e7SChristoph Hellwig void
159641b5c2e7SChristoph Hellwig xfs_unmountfs(
159741b5c2e7SChristoph Hellwig 	struct xfs_mount	*mp)
15981da177e4SLinus Torvalds {
159984e1e99fSDavid Chinner 	__uint64_t		resblks;
160041b5c2e7SChristoph Hellwig 	int			error;
16011da177e4SLinus Torvalds 
1602579b62faSBrian Foster 	cancel_delayed_work_sync(&mp->m_eofblocks_work);
1603579b62faSBrian Foster 
16047d095257SChristoph Hellwig 	xfs_qm_unmount_quotas(mp);
1605b93b6e43SChristoph Hellwig 	xfs_rtunmount_inodes(mp);
160677508ec8SChristoph Hellwig 	IRELE(mp->m_rootip);
160777508ec8SChristoph Hellwig 
1608641c56fbSDavid Chinner 	/*
1609641c56fbSDavid Chinner 	 * We can potentially deadlock here if we have an inode cluster
16109da096fdSMalcolm Parsons 	 * that has been freed has its buffer still pinned in memory because
1611641c56fbSDavid Chinner 	 * the transaction is still sitting in a iclog. The stale inodes
1612641c56fbSDavid Chinner 	 * on that buffer will have their flush locks held until the
1613641c56fbSDavid Chinner 	 * transaction hits the disk and the callbacks run. the inode
1614641c56fbSDavid Chinner 	 * flush takes the flush lock unconditionally and with nothing to
1615641c56fbSDavid Chinner 	 * push out the iclog we will never get that unlocked. hence we
1616641c56fbSDavid Chinner 	 * need to force the log first.
1617641c56fbSDavid Chinner 	 */
1618a14a348bSChristoph Hellwig 	xfs_log_force(mp, XFS_LOG_SYNC);
1619c854363eSDave Chinner 
1620c854363eSDave Chinner 	/*
1621211e4d43SChristoph Hellwig 	 * Flush all pending changes from the AIL.
1622c854363eSDave Chinner 	 */
1623211e4d43SChristoph Hellwig 	xfs_ail_push_all_sync(mp->m_ail);
1624211e4d43SChristoph Hellwig 
1625211e4d43SChristoph Hellwig 	/*
1626211e4d43SChristoph Hellwig 	 * And reclaim all inodes.  At this point there should be no dirty
16277e18530bSDave Chinner 	 * inodes and none should be pinned or locked, but use synchronous
16287e18530bSDave Chinner 	 * reclaim just to be sure. We can stop background inode reclaim
16297e18530bSDave Chinner 	 * here as well if it is still running.
1630211e4d43SChristoph Hellwig 	 */
16317e18530bSDave Chinner 	cancel_delayed_work_sync(&mp->m_reclaim_work);
1632c854363eSDave Chinner 	xfs_reclaim_inodes(mp, SYNC_WAIT);
16331da177e4SLinus Torvalds 
16347d095257SChristoph Hellwig 	xfs_qm_unmount(mp);
1635a357a121SLachlan McIlroy 
16361da177e4SLinus Torvalds 	/*
163784e1e99fSDavid Chinner 	 * Unreserve any blocks we have so that when we unmount we don't account
163884e1e99fSDavid Chinner 	 * the reserved free space as used. This is really only necessary for
163984e1e99fSDavid Chinner 	 * lazy superblock counting because it trusts the incore superblock
16409da096fdSMalcolm Parsons 	 * counters to be absolutely correct on clean unmount.
164184e1e99fSDavid Chinner 	 *
164284e1e99fSDavid Chinner 	 * We don't bother correcting this elsewhere for lazy superblock
164384e1e99fSDavid Chinner 	 * counting because on mount of an unclean filesystem we reconstruct the
164484e1e99fSDavid Chinner 	 * correct counter value and this is irrelevant.
164584e1e99fSDavid Chinner 	 *
164684e1e99fSDavid Chinner 	 * For non-lazy counter filesystems, this doesn't matter at all because
164784e1e99fSDavid Chinner 	 * we only every apply deltas to the superblock and hence the incore
164884e1e99fSDavid Chinner 	 * value does not matter....
164984e1e99fSDavid Chinner 	 */
165084e1e99fSDavid Chinner 	resblks = 0;
1651714082bcSDavid Chinner 	error = xfs_reserve_blocks(mp, &resblks, NULL);
1652714082bcSDavid Chinner 	if (error)
16530b932cccSDave Chinner 		xfs_warn(mp, "Unable to free reserved block pool. "
1654714082bcSDavid Chinner 				"Freespace may not be correct on next mount.");
1655714082bcSDavid Chinner 
1656adab0f67SChandra Seetharaman 	error = xfs_log_sbcount(mp);
1657e5720eecSDavid Chinner 	if (error)
16580b932cccSDave Chinner 		xfs_warn(mp, "Unable to update superblock counters. "
1659e5720eecSDavid Chinner 				"Freespace may not be correct on next mount.");
166087c7bec7SChristoph Hellwig 
166121b699c8SChristoph Hellwig 	xfs_log_unmount(mp);
166227174203SChristoph Hellwig 	xfs_uuid_unmount(mp);
16631da177e4SLinus Torvalds 
16641550d0b0SChristoph Hellwig #if defined(DEBUG)
16650ce4cfd4SChristoph Hellwig 	xfs_errortag_clearall(mp, 0);
16661da177e4SLinus Torvalds #endif
1667ff4f038cSChristoph Hellwig 	xfs_free_perag(mp);
16681da177e4SLinus Torvalds }
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds int
167192821e2bSDavid Chinner xfs_fs_writable(xfs_mount_t *mp)
167292821e2bSDavid Chinner {
1673d9457dc0SJan Kara 	return !(mp->m_super->s_writers.frozen || XFS_FORCED_SHUTDOWN(mp) ||
1674bd186aa9SChristoph Hellwig 		(mp->m_flags & XFS_MOUNT_RDONLY));
167592821e2bSDavid Chinner }
167692821e2bSDavid Chinner 
167792821e2bSDavid Chinner /*
1678b2ce3974SAlex Elder  * xfs_log_sbcount
1679b2ce3974SAlex Elder  *
1680adab0f67SChandra Seetharaman  * Sync the superblock counters to disk.
1681b2ce3974SAlex Elder  *
1682b2ce3974SAlex Elder  * Note this code can be called during the process of freezing, so
1683adab0f67SChandra Seetharaman  * we may need to use the transaction allocator which does not
1684b2ce3974SAlex Elder  * block when the transaction subsystem is in its frozen state.
168592821e2bSDavid Chinner  */
168692821e2bSDavid Chinner int
1687adab0f67SChandra Seetharaman xfs_log_sbcount(xfs_mount_t *mp)
168892821e2bSDavid Chinner {
168992821e2bSDavid Chinner 	xfs_trans_t	*tp;
169092821e2bSDavid Chinner 	int		error;
169192821e2bSDavid Chinner 
169292821e2bSDavid Chinner 	if (!xfs_fs_writable(mp))
169392821e2bSDavid Chinner 		return 0;
169492821e2bSDavid Chinner 
1695d4d90b57SChristoph Hellwig 	xfs_icsb_sync_counters(mp, 0);
169692821e2bSDavid Chinner 
169792821e2bSDavid Chinner 	/*
169892821e2bSDavid Chinner 	 * we don't need to do this if we are updating the superblock
169992821e2bSDavid Chinner 	 * counters on every modification.
170092821e2bSDavid Chinner 	 */
170192821e2bSDavid Chinner 	if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
170292821e2bSDavid Chinner 		return 0;
170392821e2bSDavid Chinner 
1704b2ce3974SAlex Elder 	tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT, KM_SLEEP);
1705e457274bSJeff Liu 	error = xfs_trans_reserve(tp, 0, XFS_SB_LOG_RES(mp), 0, 0,
170692821e2bSDavid Chinner 				  XFS_DEFAULT_LOG_COUNT);
170792821e2bSDavid Chinner 	if (error) {
170892821e2bSDavid Chinner 		xfs_trans_cancel(tp, 0);
170992821e2bSDavid Chinner 		return error;
171092821e2bSDavid Chinner 	}
171192821e2bSDavid Chinner 
171292821e2bSDavid Chinner 	xfs_mod_sb(tp, XFS_SB_IFREE | XFS_SB_ICOUNT | XFS_SB_FDBLOCKS);
171392821e2bSDavid Chinner 	xfs_trans_set_sync(tp);
1714e5720eecSDavid Chinner 	error = xfs_trans_commit(tp, 0);
1715e5720eecSDavid Chinner 	return error;
171692821e2bSDavid Chinner }
171792821e2bSDavid Chinner 
17181da177e4SLinus Torvalds /*
17191da177e4SLinus Torvalds  * xfs_mod_sb() can be used to copy arbitrary changes to the
17201da177e4SLinus Torvalds  * in-core superblock into the superblock buffer to be logged.
17211da177e4SLinus Torvalds  * It does not provide the higher level of locking that is
17221da177e4SLinus Torvalds  * needed to protect the in-core superblock from concurrent
17231da177e4SLinus Torvalds  * access.
17241da177e4SLinus Torvalds  */
17251da177e4SLinus Torvalds void
17261da177e4SLinus Torvalds xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
17271da177e4SLinus Torvalds {
17281da177e4SLinus Torvalds 	xfs_buf_t	*bp;
17291da177e4SLinus Torvalds 	int		first;
17301da177e4SLinus Torvalds 	int		last;
17311da177e4SLinus Torvalds 	xfs_mount_t	*mp;
17321da177e4SLinus Torvalds 	xfs_sb_field_t	f;
17331da177e4SLinus Torvalds 
17341da177e4SLinus Torvalds 	ASSERT(fields);
17351da177e4SLinus Torvalds 	if (!fields)
17361da177e4SLinus Torvalds 		return;
17371da177e4SLinus Torvalds 	mp = tp->t_mountp;
17381da177e4SLinus Torvalds 	bp = xfs_trans_getsb(tp, mp, 0);
17391da177e4SLinus Torvalds 	first = sizeof(xfs_sb_t);
17401da177e4SLinus Torvalds 	last = 0;
17411da177e4SLinus Torvalds 
17421da177e4SLinus Torvalds 	/* translate/copy */
17431da177e4SLinus Torvalds 
17442bdf7cd0SChristoph Hellwig 	xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb, fields);
17451da177e4SLinus Torvalds 
17461da177e4SLinus Torvalds 	/* find modified range */
1747587aa0feSDave Chinner 	f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
1748587aa0feSDave Chinner 	ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1749587aa0feSDave Chinner 	last = xfs_sb_info[f + 1].offset - 1;
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds 	f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
17521da177e4SLinus Torvalds 	ASSERT((1LL << f) & XFS_SB_MOD_BITS);
17531da177e4SLinus Torvalds 	first = xfs_sb_info[f].offset;
17541da177e4SLinus Torvalds 
175504a1e6c5SDave Chinner 	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
17561da177e4SLinus Torvalds 	xfs_trans_log_buf(tp, bp, first, last);
17571da177e4SLinus Torvalds }
1758d210a28cSYingping Lu 
1759d210a28cSYingping Lu 
17601da177e4SLinus Torvalds /*
17611da177e4SLinus Torvalds  * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
17621da177e4SLinus Torvalds  * a delta to a specified field in the in-core superblock.  Simply
17631da177e4SLinus Torvalds  * switch on the field indicated and apply the delta to that field.
17641da177e4SLinus Torvalds  * Fields are not allowed to dip below zero, so if the delta would
17651da177e4SLinus Torvalds  * do this do not apply it and return EINVAL.
17661da177e4SLinus Torvalds  *
17673685c2a1SEric Sandeen  * The m_sb_lock must be held when this routine is called.
17681da177e4SLinus Torvalds  */
1769d96f8f89SEric Sandeen STATIC int
177020f4ebf2SDavid Chinner xfs_mod_incore_sb_unlocked(
177120f4ebf2SDavid Chinner 	xfs_mount_t	*mp,
177220f4ebf2SDavid Chinner 	xfs_sb_field_t	field,
177320f4ebf2SDavid Chinner 	int64_t		delta,
177420f4ebf2SDavid Chinner 	int		rsvd)
17751da177e4SLinus Torvalds {
17761da177e4SLinus Torvalds 	int		scounter;	/* short counter for 32 bit fields */
17771da177e4SLinus Torvalds 	long long	lcounter;	/* long counter for 64 bit fields */
17781da177e4SLinus Torvalds 	long long	res_used, rem;
17791da177e4SLinus Torvalds 
17801da177e4SLinus Torvalds 	/*
17811da177e4SLinus Torvalds 	 * With the in-core superblock spin lock held, switch
17821da177e4SLinus Torvalds 	 * on the indicated field.  Apply the delta to the
17831da177e4SLinus Torvalds 	 * proper field.  If the fields value would dip below
17841da177e4SLinus Torvalds 	 * 0, then do not apply the delta and return EINVAL.
17851da177e4SLinus Torvalds 	 */
17861da177e4SLinus Torvalds 	switch (field) {
17871da177e4SLinus Torvalds 	case XFS_SBS_ICOUNT:
17881da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_icount;
17891da177e4SLinus Torvalds 		lcounter += delta;
17901da177e4SLinus Torvalds 		if (lcounter < 0) {
17911da177e4SLinus Torvalds 			ASSERT(0);
1792014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
17931da177e4SLinus Torvalds 		}
17941da177e4SLinus Torvalds 		mp->m_sb.sb_icount = lcounter;
1795014c2544SJesper Juhl 		return 0;
17961da177e4SLinus Torvalds 	case XFS_SBS_IFREE:
17971da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_ifree;
17981da177e4SLinus Torvalds 		lcounter += delta;
17991da177e4SLinus Torvalds 		if (lcounter < 0) {
18001da177e4SLinus Torvalds 			ASSERT(0);
1801014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
18021da177e4SLinus Torvalds 		}
18031da177e4SLinus Torvalds 		mp->m_sb.sb_ifree = lcounter;
1804014c2544SJesper Juhl 		return 0;
18051da177e4SLinus Torvalds 	case XFS_SBS_FDBLOCKS:
18064be536deSDavid Chinner 		lcounter = (long long)
18074be536deSDavid Chinner 			mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
18081da177e4SLinus Torvalds 		res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
18091da177e4SLinus Torvalds 
18101da177e4SLinus Torvalds 		if (delta > 0) {		/* Putting blocks back */
18111da177e4SLinus Torvalds 			if (res_used > delta) {
18121da177e4SLinus Torvalds 				mp->m_resblks_avail += delta;
18131da177e4SLinus Torvalds 			} else {
18141da177e4SLinus Torvalds 				rem = delta - res_used;
18151da177e4SLinus Torvalds 				mp->m_resblks_avail = mp->m_resblks;
18161da177e4SLinus Torvalds 				lcounter += rem;
18171da177e4SLinus Torvalds 			}
18181da177e4SLinus Torvalds 		} else {				/* Taking blocks away */
18191da177e4SLinus Torvalds 			lcounter += delta;
18208babd8a2SDave Chinner 			if (lcounter >= 0) {
18218babd8a2SDave Chinner 				mp->m_sb.sb_fdblocks = lcounter +
18228babd8a2SDave Chinner 							XFS_ALLOC_SET_ASIDE(mp);
18238babd8a2SDave Chinner 				return 0;
18248babd8a2SDave Chinner 			}
18251da177e4SLinus Torvalds 
18261da177e4SLinus Torvalds 			/*
18278babd8a2SDave Chinner 			 * We are out of blocks, use any available reserved
18288babd8a2SDave Chinner 			 * blocks if were allowed to.
18291da177e4SLinus Torvalds 			 */
18308babd8a2SDave Chinner 			if (!rsvd)
1831014c2544SJesper Juhl 				return XFS_ERROR(ENOSPC);
18328babd8a2SDave Chinner 
18338babd8a2SDave Chinner 			lcounter = (long long)mp->m_resblks_avail + delta;
18348babd8a2SDave Chinner 			if (lcounter >= 0) {
18351da177e4SLinus Torvalds 				mp->m_resblks_avail = lcounter;
1836014c2544SJesper Juhl 				return 0;
18378babd8a2SDave Chinner 			}
18388babd8a2SDave Chinner 			printk_once(KERN_WARNING
18398babd8a2SDave Chinner 				"Filesystem \"%s\": reserve blocks depleted! "
18408babd8a2SDave Chinner 				"Consider increasing reserve pool size.",
18418babd8a2SDave Chinner 				mp->m_fsname);
1842014c2544SJesper Juhl 			return XFS_ERROR(ENOSPC);
18431da177e4SLinus Torvalds 		}
18441da177e4SLinus Torvalds 
18454be536deSDavid Chinner 		mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
1846014c2544SJesper Juhl 		return 0;
18471da177e4SLinus Torvalds 	case XFS_SBS_FREXTENTS:
18481da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_frextents;
18491da177e4SLinus Torvalds 		lcounter += delta;
18501da177e4SLinus Torvalds 		if (lcounter < 0) {
1851014c2544SJesper Juhl 			return XFS_ERROR(ENOSPC);
18521da177e4SLinus Torvalds 		}
18531da177e4SLinus Torvalds 		mp->m_sb.sb_frextents = lcounter;
1854014c2544SJesper Juhl 		return 0;
18551da177e4SLinus Torvalds 	case XFS_SBS_DBLOCKS:
18561da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_dblocks;
18571da177e4SLinus Torvalds 		lcounter += delta;
18581da177e4SLinus Torvalds 		if (lcounter < 0) {
18591da177e4SLinus Torvalds 			ASSERT(0);
1860014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
18611da177e4SLinus Torvalds 		}
18621da177e4SLinus Torvalds 		mp->m_sb.sb_dblocks = lcounter;
1863014c2544SJesper Juhl 		return 0;
18641da177e4SLinus Torvalds 	case XFS_SBS_AGCOUNT:
18651da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_agcount;
18661da177e4SLinus Torvalds 		scounter += delta;
18671da177e4SLinus Torvalds 		if (scounter < 0) {
18681da177e4SLinus Torvalds 			ASSERT(0);
1869014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
18701da177e4SLinus Torvalds 		}
18711da177e4SLinus Torvalds 		mp->m_sb.sb_agcount = scounter;
1872014c2544SJesper Juhl 		return 0;
18731da177e4SLinus Torvalds 	case XFS_SBS_IMAX_PCT:
18741da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_imax_pct;
18751da177e4SLinus Torvalds 		scounter += delta;
18761da177e4SLinus Torvalds 		if (scounter < 0) {
18771da177e4SLinus Torvalds 			ASSERT(0);
1878014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
18791da177e4SLinus Torvalds 		}
18801da177e4SLinus Torvalds 		mp->m_sb.sb_imax_pct = scounter;
1881014c2544SJesper Juhl 		return 0;
18821da177e4SLinus Torvalds 	case XFS_SBS_REXTSIZE:
18831da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rextsize;
18841da177e4SLinus Torvalds 		scounter += delta;
18851da177e4SLinus Torvalds 		if (scounter < 0) {
18861da177e4SLinus Torvalds 			ASSERT(0);
1887014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
18881da177e4SLinus Torvalds 		}
18891da177e4SLinus Torvalds 		mp->m_sb.sb_rextsize = scounter;
1890014c2544SJesper Juhl 		return 0;
18911da177e4SLinus Torvalds 	case XFS_SBS_RBMBLOCKS:
18921da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rbmblocks;
18931da177e4SLinus Torvalds 		scounter += delta;
18941da177e4SLinus Torvalds 		if (scounter < 0) {
18951da177e4SLinus Torvalds 			ASSERT(0);
1896014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
18971da177e4SLinus Torvalds 		}
18981da177e4SLinus Torvalds 		mp->m_sb.sb_rbmblocks = scounter;
1899014c2544SJesper Juhl 		return 0;
19001da177e4SLinus Torvalds 	case XFS_SBS_RBLOCKS:
19011da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_rblocks;
19021da177e4SLinus Torvalds 		lcounter += delta;
19031da177e4SLinus Torvalds 		if (lcounter < 0) {
19041da177e4SLinus Torvalds 			ASSERT(0);
1905014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
19061da177e4SLinus Torvalds 		}
19071da177e4SLinus Torvalds 		mp->m_sb.sb_rblocks = lcounter;
1908014c2544SJesper Juhl 		return 0;
19091da177e4SLinus Torvalds 	case XFS_SBS_REXTENTS:
19101da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_rextents;
19111da177e4SLinus Torvalds 		lcounter += delta;
19121da177e4SLinus Torvalds 		if (lcounter < 0) {
19131da177e4SLinus Torvalds 			ASSERT(0);
1914014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
19151da177e4SLinus Torvalds 		}
19161da177e4SLinus Torvalds 		mp->m_sb.sb_rextents = lcounter;
1917014c2544SJesper Juhl 		return 0;
19181da177e4SLinus Torvalds 	case XFS_SBS_REXTSLOG:
19191da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rextslog;
19201da177e4SLinus Torvalds 		scounter += delta;
19211da177e4SLinus Torvalds 		if (scounter < 0) {
19221da177e4SLinus Torvalds 			ASSERT(0);
1923014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
19241da177e4SLinus Torvalds 		}
19251da177e4SLinus Torvalds 		mp->m_sb.sb_rextslog = scounter;
1926014c2544SJesper Juhl 		return 0;
19271da177e4SLinus Torvalds 	default:
19281da177e4SLinus Torvalds 		ASSERT(0);
1929014c2544SJesper Juhl 		return XFS_ERROR(EINVAL);
19301da177e4SLinus Torvalds 	}
19311da177e4SLinus Torvalds }
19321da177e4SLinus Torvalds 
19331da177e4SLinus Torvalds /*
19341da177e4SLinus Torvalds  * xfs_mod_incore_sb() is used to change a field in the in-core
19351da177e4SLinus Torvalds  * superblock structure by the specified delta.  This modification
19363685c2a1SEric Sandeen  * is protected by the m_sb_lock.  Just use the xfs_mod_incore_sb_unlocked()
19371da177e4SLinus Torvalds  * routine to do the work.
19381da177e4SLinus Torvalds  */
19391da177e4SLinus Torvalds int
194020f4ebf2SDavid Chinner xfs_mod_incore_sb(
194196540c78SChristoph Hellwig 	struct xfs_mount	*mp,
194220f4ebf2SDavid Chinner 	xfs_sb_field_t		field,
194320f4ebf2SDavid Chinner 	int64_t			delta,
194420f4ebf2SDavid Chinner 	int			rsvd)
19451da177e4SLinus Torvalds {
19461da177e4SLinus Torvalds 	int			status;
19471da177e4SLinus Torvalds 
19488d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
194996540c78SChristoph Hellwig 	ASSERT(field < XFS_SBS_ICOUNT || field > XFS_SBS_FDBLOCKS);
19508d280b98SDavid Chinner #endif
19513685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
19521da177e4SLinus Torvalds 	status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
19533685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
19548d280b98SDavid Chinner 
1955014c2544SJesper Juhl 	return status;
19561da177e4SLinus Torvalds }
19571da177e4SLinus Torvalds 
19581da177e4SLinus Torvalds /*
19591b040712SChristoph Hellwig  * Change more than one field in the in-core superblock structure at a time.
19601da177e4SLinus Torvalds  *
19611b040712SChristoph Hellwig  * The fields and changes to those fields are specified in the array of
19621b040712SChristoph Hellwig  * xfs_mod_sb structures passed in.  Either all of the specified deltas
19631b040712SChristoph Hellwig  * will be applied or none of them will.  If any modified field dips below 0,
19641b040712SChristoph Hellwig  * then all modifications will be backed out and EINVAL will be returned.
19651b040712SChristoph Hellwig  *
19661b040712SChristoph Hellwig  * Note that this function may not be used for the superblock values that
19671b040712SChristoph Hellwig  * are tracked with the in-memory per-cpu counters - a direct call to
19681b040712SChristoph Hellwig  * xfs_icsb_modify_counters is required for these.
19691da177e4SLinus Torvalds  */
19701da177e4SLinus Torvalds int
19711b040712SChristoph Hellwig xfs_mod_incore_sb_batch(
19721b040712SChristoph Hellwig 	struct xfs_mount	*mp,
19731b040712SChristoph Hellwig 	xfs_mod_sb_t		*msb,
19741b040712SChristoph Hellwig 	uint			nmsb,
19751b040712SChristoph Hellwig 	int			rsvd)
19761da177e4SLinus Torvalds {
197745c51b99SDavid Sterba 	xfs_mod_sb_t		*msbp;
19781b040712SChristoph Hellwig 	int			error = 0;
19791da177e4SLinus Torvalds 
19801da177e4SLinus Torvalds 	/*
19811b040712SChristoph Hellwig 	 * Loop through the array of mod structures and apply each individually.
19821b040712SChristoph Hellwig 	 * If any fail, then back out all those which have already been applied.
19831b040712SChristoph Hellwig 	 * Do all of this within the scope of the m_sb_lock so that all of the
19841b040712SChristoph Hellwig 	 * changes will be atomic.
19851da177e4SLinus Torvalds 	 */
19863685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
198745c51b99SDavid Sterba 	for (msbp = msb; msbp < (msb + nmsb); msbp++) {
19881b040712SChristoph Hellwig 		ASSERT(msbp->msb_field < XFS_SBS_ICOUNT ||
19891b040712SChristoph Hellwig 		       msbp->msb_field > XFS_SBS_FDBLOCKS);
19908d280b98SDavid Chinner 
19911b040712SChristoph Hellwig 		error = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
19921b040712SChristoph Hellwig 						   msbp->msb_delta, rsvd);
19931b040712SChristoph Hellwig 		if (error)
19941b040712SChristoph Hellwig 			goto unwind;
19951da177e4SLinus Torvalds 	}
19961b040712SChristoph Hellwig 	spin_unlock(&mp->m_sb_lock);
19971b040712SChristoph Hellwig 	return 0;
19981da177e4SLinus Torvalds 
19991b040712SChristoph Hellwig unwind:
20001b040712SChristoph Hellwig 	while (--msbp >= msb) {
20011b040712SChristoph Hellwig 		error = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
20021b040712SChristoph Hellwig 						   -msbp->msb_delta, rsvd);
20031b040712SChristoph Hellwig 		ASSERT(error == 0);
20041da177e4SLinus Torvalds 	}
20053685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
20061b040712SChristoph Hellwig 	return error;
20071da177e4SLinus Torvalds }
20081da177e4SLinus Torvalds 
20091da177e4SLinus Torvalds /*
20101da177e4SLinus Torvalds  * xfs_getsb() is called to obtain the buffer for the superblock.
20111da177e4SLinus Torvalds  * The buffer is returned locked and read in from disk.
20121da177e4SLinus Torvalds  * The buffer should be released with a call to xfs_brelse().
20131da177e4SLinus Torvalds  *
20141da177e4SLinus Torvalds  * If the flags parameter is BUF_TRYLOCK, then we'll only return
20151da177e4SLinus Torvalds  * the superblock buffer if it can be locked without sleeping.
20161da177e4SLinus Torvalds  * If it can't then we'll return NULL.
20171da177e4SLinus Torvalds  */
20180c842ad4SChristoph Hellwig struct xfs_buf *
20191da177e4SLinus Torvalds xfs_getsb(
20200c842ad4SChristoph Hellwig 	struct xfs_mount	*mp,
20211da177e4SLinus Torvalds 	int			flags)
20221da177e4SLinus Torvalds {
20230c842ad4SChristoph Hellwig 	struct xfs_buf		*bp = mp->m_sb_bp;
20241da177e4SLinus Torvalds 
20250c842ad4SChristoph Hellwig 	if (!xfs_buf_trylock(bp)) {
20260c842ad4SChristoph Hellwig 		if (flags & XBF_TRYLOCK)
20271da177e4SLinus Torvalds 			return NULL;
20280c842ad4SChristoph Hellwig 		xfs_buf_lock(bp);
20291da177e4SLinus Torvalds 	}
20300c842ad4SChristoph Hellwig 
203172790aa1SChandra Seetharaman 	xfs_buf_hold(bp);
20321da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISDONE(bp));
2033014c2544SJesper Juhl 	return bp;
20341da177e4SLinus Torvalds }
20351da177e4SLinus Torvalds 
20361da177e4SLinus Torvalds /*
20371da177e4SLinus Torvalds  * Used to free the superblock along various error paths.
20381da177e4SLinus Torvalds  */
20391da177e4SLinus Torvalds void
20401da177e4SLinus Torvalds xfs_freesb(
204126af6552SDave Chinner 	struct xfs_mount	*mp)
20421da177e4SLinus Torvalds {
204326af6552SDave Chinner 	struct xfs_buf		*bp = mp->m_sb_bp;
20441da177e4SLinus Torvalds 
204526af6552SDave Chinner 	xfs_buf_lock(bp);
20461da177e4SLinus Torvalds 	mp->m_sb_bp = NULL;
204726af6552SDave Chinner 	xfs_buf_relse(bp);
20481da177e4SLinus Torvalds }
20491da177e4SLinus Torvalds 
20501da177e4SLinus Torvalds /*
20511da177e4SLinus Torvalds  * Used to log changes to the superblock unit and width fields which could
2052e6957ea4SEric Sandeen  * be altered by the mount options, as well as any potential sb_features2
2053e6957ea4SEric Sandeen  * fixup. Only the first superblock is updated.
20541da177e4SLinus Torvalds  */
20557884bc86SChristoph Hellwig int
2056ee1c0908SDavid Chinner xfs_mount_log_sb(
20571da177e4SLinus Torvalds 	xfs_mount_t	*mp,
20581da177e4SLinus Torvalds 	__int64_t	fields)
20591da177e4SLinus Torvalds {
20601da177e4SLinus Torvalds 	xfs_trans_t	*tp;
2061e5720eecSDavid Chinner 	int		error;
20621da177e4SLinus Torvalds 
2063ee1c0908SDavid Chinner 	ASSERT(fields & (XFS_SB_UNIT | XFS_SB_WIDTH | XFS_SB_UUID |
20644b166de0SDavid Chinner 			 XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2 |
20654b166de0SDavid Chinner 			 XFS_SB_VERSIONNUM));
20661da177e4SLinus Torvalds 
20671da177e4SLinus Torvalds 	tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
20685166ab06SJeff Liu 	error = xfs_trans_reserve(tp, 0, XFS_SB_LOG_RES(mp), 0, 0,
2069e5720eecSDavid Chinner 				  XFS_DEFAULT_LOG_COUNT);
2070e5720eecSDavid Chinner 	if (error) {
20711da177e4SLinus Torvalds 		xfs_trans_cancel(tp, 0);
2072e5720eecSDavid Chinner 		return error;
20731da177e4SLinus Torvalds 	}
20741da177e4SLinus Torvalds 	xfs_mod_sb(tp, fields);
2075e5720eecSDavid Chinner 	error = xfs_trans_commit(tp, 0);
2076e5720eecSDavid Chinner 	return error;
20771da177e4SLinus Torvalds }
20788d280b98SDavid Chinner 
2079dda35b8fSChristoph Hellwig /*
2080dda35b8fSChristoph Hellwig  * If the underlying (data/log/rt) device is readonly, there are some
2081dda35b8fSChristoph Hellwig  * operations that cannot proceed.
2082dda35b8fSChristoph Hellwig  */
2083dda35b8fSChristoph Hellwig int
2084dda35b8fSChristoph Hellwig xfs_dev_is_read_only(
2085dda35b8fSChristoph Hellwig 	struct xfs_mount	*mp,
2086dda35b8fSChristoph Hellwig 	char			*message)
2087dda35b8fSChristoph Hellwig {
2088dda35b8fSChristoph Hellwig 	if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
2089dda35b8fSChristoph Hellwig 	    xfs_readonly_buftarg(mp->m_logdev_targp) ||
2090dda35b8fSChristoph Hellwig 	    (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
20910b932cccSDave Chinner 		xfs_notice(mp, "%s required on read-only device.", message);
20920b932cccSDave Chinner 		xfs_notice(mp, "write access unavailable, cannot proceed.");
2093dda35b8fSChristoph Hellwig 		return EROFS;
2094dda35b8fSChristoph Hellwig 	}
2095dda35b8fSChristoph Hellwig 	return 0;
2096dda35b8fSChristoph Hellwig }
20978d280b98SDavid Chinner 
20988d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
20998d280b98SDavid Chinner /*
21008d280b98SDavid Chinner  * Per-cpu incore superblock counters
21018d280b98SDavid Chinner  *
21028d280b98SDavid Chinner  * Simple concept, difficult implementation
21038d280b98SDavid Chinner  *
21048d280b98SDavid Chinner  * Basically, replace the incore superblock counters with a distributed per cpu
21058d280b98SDavid Chinner  * counter for contended fields (e.g.  free block count).
21068d280b98SDavid Chinner  *
21078d280b98SDavid Chinner  * Difficulties arise in that the incore sb is used for ENOSPC checking, and
21088d280b98SDavid Chinner  * hence needs to be accurately read when we are running low on space. Hence
21098d280b98SDavid Chinner  * there is a method to enable and disable the per-cpu counters based on how
21108d280b98SDavid Chinner  * much "stuff" is available in them.
21118d280b98SDavid Chinner  *
21128d280b98SDavid Chinner  * Basically, a counter is enabled if there is enough free resource to justify
21138d280b98SDavid Chinner  * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
21148d280b98SDavid Chinner  * ENOSPC), then we disable the counters to synchronise all callers and
21158d280b98SDavid Chinner  * re-distribute the available resources.
21168d280b98SDavid Chinner  *
21178d280b98SDavid Chinner  * If, once we redistributed the available resources, we still get a failure,
21188d280b98SDavid Chinner  * we disable the per-cpu counter and go through the slow path.
21198d280b98SDavid Chinner  *
21208d280b98SDavid Chinner  * The slow path is the current xfs_mod_incore_sb() function.  This means that
21219da096fdSMalcolm Parsons  * when we disable a per-cpu counter, we need to drain its resources back to
21228d280b98SDavid Chinner  * the global superblock. We do this after disabling the counter to prevent
21238d280b98SDavid Chinner  * more threads from queueing up on the counter.
21248d280b98SDavid Chinner  *
21258d280b98SDavid Chinner  * Essentially, this means that we still need a lock in the fast path to enable
21268d280b98SDavid Chinner  * synchronisation between the global counters and the per-cpu counters. This
21278d280b98SDavid Chinner  * is not a problem because the lock will be local to a CPU almost all the time
21288d280b98SDavid Chinner  * and have little contention except when we get to ENOSPC conditions.
21298d280b98SDavid Chinner  *
21308d280b98SDavid Chinner  * Basically, this lock becomes a barrier that enables us to lock out the fast
21318d280b98SDavid Chinner  * path while we do things like enabling and disabling counters and
21328d280b98SDavid Chinner  * synchronising the counters.
21338d280b98SDavid Chinner  *
21348d280b98SDavid Chinner  * Locking rules:
21358d280b98SDavid Chinner  *
21363685c2a1SEric Sandeen  * 	1. m_sb_lock before picking up per-cpu locks
21378d280b98SDavid Chinner  * 	2. per-cpu locks always picked up via for_each_online_cpu() order
21383685c2a1SEric Sandeen  * 	3. accurate counter sync requires m_sb_lock + per cpu locks
21398d280b98SDavid Chinner  * 	4. modifying per-cpu counters requires holding per-cpu lock
21403685c2a1SEric Sandeen  * 	5. modifying global counters requires holding m_sb_lock
21413685c2a1SEric Sandeen  *	6. enabling or disabling a counter requires holding the m_sb_lock
21428d280b98SDavid Chinner  *	   and _none_ of the per-cpu locks.
21438d280b98SDavid Chinner  *
21448d280b98SDavid Chinner  * Disabled counters are only ever re-enabled by a balance operation
21458d280b98SDavid Chinner  * that results in more free resources per CPU than a given threshold.
21468d280b98SDavid Chinner  * To ensure counters don't remain disabled, they are rebalanced when
21478d280b98SDavid Chinner  * the global resource goes above a higher threshold (i.e. some hysteresis
21488d280b98SDavid Chinner  * is present to prevent thrashing).
21498d280b98SDavid Chinner  */
2150e8234a68SDavid Chinner 
21515a67e4c5SChandra Seetharaman #ifdef CONFIG_HOTPLUG_CPU
2152e8234a68SDavid Chinner /*
2153e8234a68SDavid Chinner  * hot-plug CPU notifier support.
2154e8234a68SDavid Chinner  *
21555a67e4c5SChandra Seetharaman  * We need a notifier per filesystem as we need to be able to identify
21565a67e4c5SChandra Seetharaman  * the filesystem to balance the counters out. This is achieved by
21575a67e4c5SChandra Seetharaman  * having a notifier block embedded in the xfs_mount_t and doing pointer
21585a67e4c5SChandra Seetharaman  * magic to get the mount pointer from the notifier block address.
2159e8234a68SDavid Chinner  */
2160e8234a68SDavid Chinner STATIC int
2161e8234a68SDavid Chinner xfs_icsb_cpu_notify(
2162e8234a68SDavid Chinner 	struct notifier_block *nfb,
2163e8234a68SDavid Chinner 	unsigned long action,
2164e8234a68SDavid Chinner 	void *hcpu)
2165e8234a68SDavid Chinner {
2166e8234a68SDavid Chinner 	xfs_icsb_cnts_t *cntp;
2167e8234a68SDavid Chinner 	xfs_mount_t	*mp;
2168e8234a68SDavid Chinner 
2169e8234a68SDavid Chinner 	mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
2170e8234a68SDavid Chinner 	cntp = (xfs_icsb_cnts_t *)
2171e8234a68SDavid Chinner 			per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
2172e8234a68SDavid Chinner 	switch (action) {
2173e8234a68SDavid Chinner 	case CPU_UP_PREPARE:
21748bb78442SRafael J. Wysocki 	case CPU_UP_PREPARE_FROZEN:
2175e8234a68SDavid Chinner 		/* Easy Case - initialize the area and locks, and
2176e8234a68SDavid Chinner 		 * then rebalance when online does everything else for us. */
217701e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
2178e8234a68SDavid Chinner 		break;
2179e8234a68SDavid Chinner 	case CPU_ONLINE:
21808bb78442SRafael J. Wysocki 	case CPU_ONLINE_FROZEN:
218103135cf7SDavid Chinner 		xfs_icsb_lock(mp);
218245af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
218345af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
218445af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
218503135cf7SDavid Chinner 		xfs_icsb_unlock(mp);
2186e8234a68SDavid Chinner 		break;
2187e8234a68SDavid Chinner 	case CPU_DEAD:
21888bb78442SRafael J. Wysocki 	case CPU_DEAD_FROZEN:
2189e8234a68SDavid Chinner 		/* Disable all the counters, then fold the dead cpu's
2190e8234a68SDavid Chinner 		 * count into the total on the global superblock and
2191e8234a68SDavid Chinner 		 * re-enable the counters. */
219203135cf7SDavid Chinner 		xfs_icsb_lock(mp);
21933685c2a1SEric Sandeen 		spin_lock(&mp->m_sb_lock);
2194e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
2195e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
2196e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
2197e8234a68SDavid Chinner 
2198e8234a68SDavid Chinner 		mp->m_sb.sb_icount += cntp->icsb_icount;
2199e8234a68SDavid Chinner 		mp->m_sb.sb_ifree += cntp->icsb_ifree;
2200e8234a68SDavid Chinner 		mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
2201e8234a68SDavid Chinner 
220201e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
2203e8234a68SDavid Chinner 
220445af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter_locked(mp, XFS_SBS_ICOUNT, 0);
220545af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter_locked(mp, XFS_SBS_IFREE, 0);
220645af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter_locked(mp, XFS_SBS_FDBLOCKS, 0);
22073685c2a1SEric Sandeen 		spin_unlock(&mp->m_sb_lock);
220803135cf7SDavid Chinner 		xfs_icsb_unlock(mp);
2209e8234a68SDavid Chinner 		break;
2210e8234a68SDavid Chinner 	}
2211e8234a68SDavid Chinner 
2212e8234a68SDavid Chinner 	return NOTIFY_OK;
2213e8234a68SDavid Chinner }
22145a67e4c5SChandra Seetharaman #endif /* CONFIG_HOTPLUG_CPU */
2215e8234a68SDavid Chinner 
22168d280b98SDavid Chinner int
22178d280b98SDavid Chinner xfs_icsb_init_counters(
22188d280b98SDavid Chinner 	xfs_mount_t	*mp)
22198d280b98SDavid Chinner {
22208d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
22218d280b98SDavid Chinner 	int		i;
22228d280b98SDavid Chinner 
22238d280b98SDavid Chinner 	mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
22248d280b98SDavid Chinner 	if (mp->m_sb_cnts == NULL)
22258d280b98SDavid Chinner 		return -ENOMEM;
22268d280b98SDavid Chinner 
22275a67e4c5SChandra Seetharaman #ifdef CONFIG_HOTPLUG_CPU
2228e8234a68SDavid Chinner 	mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
2229e8234a68SDavid Chinner 	mp->m_icsb_notifier.priority = 0;
22305a67e4c5SChandra Seetharaman 	register_hotcpu_notifier(&mp->m_icsb_notifier);
22315a67e4c5SChandra Seetharaman #endif /* CONFIG_HOTPLUG_CPU */
2232e8234a68SDavid Chinner 
22338d280b98SDavid Chinner 	for_each_online_cpu(i) {
22348d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
223501e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
22368d280b98SDavid Chinner 	}
223720b64285SDavid Chinner 
223820b64285SDavid Chinner 	mutex_init(&mp->m_icsb_mutex);
223920b64285SDavid Chinner 
22408d280b98SDavid Chinner 	/*
22418d280b98SDavid Chinner 	 * start with all counters disabled so that the
22428d280b98SDavid Chinner 	 * initial balance kicks us off correctly
22438d280b98SDavid Chinner 	 */
22448d280b98SDavid Chinner 	mp->m_icsb_counters = -1;
22458d280b98SDavid Chinner 	return 0;
22468d280b98SDavid Chinner }
22478d280b98SDavid Chinner 
22485478eeadSLachlan McIlroy void
22495478eeadSLachlan McIlroy xfs_icsb_reinit_counters(
22505478eeadSLachlan McIlroy 	xfs_mount_t	*mp)
22515478eeadSLachlan McIlroy {
22525478eeadSLachlan McIlroy 	xfs_icsb_lock(mp);
22535478eeadSLachlan McIlroy 	/*
22545478eeadSLachlan McIlroy 	 * start with all counters disabled so that the
22555478eeadSLachlan McIlroy 	 * initial balance kicks us off correctly
22565478eeadSLachlan McIlroy 	 */
22575478eeadSLachlan McIlroy 	mp->m_icsb_counters = -1;
225845af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
225945af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
226045af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
22615478eeadSLachlan McIlroy 	xfs_icsb_unlock(mp);
22625478eeadSLachlan McIlroy }
22635478eeadSLachlan McIlroy 
2264c962fb79SChristoph Hellwig void
22658d280b98SDavid Chinner xfs_icsb_destroy_counters(
22668d280b98SDavid Chinner 	xfs_mount_t	*mp)
22678d280b98SDavid Chinner {
2268e8234a68SDavid Chinner 	if (mp->m_sb_cnts) {
22695a67e4c5SChandra Seetharaman 		unregister_hotcpu_notifier(&mp->m_icsb_notifier);
22708d280b98SDavid Chinner 		free_percpu(mp->m_sb_cnts);
22718d280b98SDavid Chinner 	}
227203135cf7SDavid Chinner 	mutex_destroy(&mp->m_icsb_mutex);
2273e8234a68SDavid Chinner }
22748d280b98SDavid Chinner 
2275b8f82a4aSChristoph Hellwig STATIC void
227601e1b69cSDavid Chinner xfs_icsb_lock_cntr(
227701e1b69cSDavid Chinner 	xfs_icsb_cnts_t	*icsbp)
227801e1b69cSDavid Chinner {
227901e1b69cSDavid Chinner 	while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
228001e1b69cSDavid Chinner 		ndelay(1000);
228101e1b69cSDavid Chinner 	}
228201e1b69cSDavid Chinner }
228301e1b69cSDavid Chinner 
2284b8f82a4aSChristoph Hellwig STATIC void
228501e1b69cSDavid Chinner xfs_icsb_unlock_cntr(
228601e1b69cSDavid Chinner 	xfs_icsb_cnts_t	*icsbp)
228701e1b69cSDavid Chinner {
228801e1b69cSDavid Chinner 	clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
228901e1b69cSDavid Chinner }
229001e1b69cSDavid Chinner 
22918d280b98SDavid Chinner 
2292b8f82a4aSChristoph Hellwig STATIC void
22938d280b98SDavid Chinner xfs_icsb_lock_all_counters(
22948d280b98SDavid Chinner 	xfs_mount_t	*mp)
22958d280b98SDavid Chinner {
22968d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
22978d280b98SDavid Chinner 	int		i;
22988d280b98SDavid Chinner 
22998d280b98SDavid Chinner 	for_each_online_cpu(i) {
23008d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
230101e1b69cSDavid Chinner 		xfs_icsb_lock_cntr(cntp);
23028d280b98SDavid Chinner 	}
23038d280b98SDavid Chinner }
23048d280b98SDavid Chinner 
2305b8f82a4aSChristoph Hellwig STATIC void
23068d280b98SDavid Chinner xfs_icsb_unlock_all_counters(
23078d280b98SDavid Chinner 	xfs_mount_t	*mp)
23088d280b98SDavid Chinner {
23098d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
23108d280b98SDavid Chinner 	int		i;
23118d280b98SDavid Chinner 
23128d280b98SDavid Chinner 	for_each_online_cpu(i) {
23138d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
231401e1b69cSDavid Chinner 		xfs_icsb_unlock_cntr(cntp);
23158d280b98SDavid Chinner 	}
23168d280b98SDavid Chinner }
23178d280b98SDavid Chinner 
23188d280b98SDavid Chinner STATIC void
23198d280b98SDavid Chinner xfs_icsb_count(
23208d280b98SDavid Chinner 	xfs_mount_t	*mp,
23218d280b98SDavid Chinner 	xfs_icsb_cnts_t	*cnt,
23228d280b98SDavid Chinner 	int		flags)
23238d280b98SDavid Chinner {
23248d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
23258d280b98SDavid Chinner 	int		i;
23268d280b98SDavid Chinner 
23278d280b98SDavid Chinner 	memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
23288d280b98SDavid Chinner 
23298d280b98SDavid Chinner 	if (!(flags & XFS_ICSB_LAZY_COUNT))
23308d280b98SDavid Chinner 		xfs_icsb_lock_all_counters(mp);
23318d280b98SDavid Chinner 
23328d280b98SDavid Chinner 	for_each_online_cpu(i) {
23338d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
23348d280b98SDavid Chinner 		cnt->icsb_icount += cntp->icsb_icount;
23358d280b98SDavid Chinner 		cnt->icsb_ifree += cntp->icsb_ifree;
23368d280b98SDavid Chinner 		cnt->icsb_fdblocks += cntp->icsb_fdblocks;
23378d280b98SDavid Chinner 	}
23388d280b98SDavid Chinner 
23398d280b98SDavid Chinner 	if (!(flags & XFS_ICSB_LAZY_COUNT))
23408d280b98SDavid Chinner 		xfs_icsb_unlock_all_counters(mp);
23418d280b98SDavid Chinner }
23428d280b98SDavid Chinner 
23438d280b98SDavid Chinner STATIC int
23448d280b98SDavid Chinner xfs_icsb_counter_disabled(
23458d280b98SDavid Chinner 	xfs_mount_t	*mp,
23468d280b98SDavid Chinner 	xfs_sb_field_t	field)
23478d280b98SDavid Chinner {
23488d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
23498d280b98SDavid Chinner 	return test_bit(field, &mp->m_icsb_counters);
23508d280b98SDavid Chinner }
23518d280b98SDavid Chinner 
235236fbe6e6SDavid Chinner STATIC void
23538d280b98SDavid Chinner xfs_icsb_disable_counter(
23548d280b98SDavid Chinner 	xfs_mount_t	*mp,
23558d280b98SDavid Chinner 	xfs_sb_field_t	field)
23568d280b98SDavid Chinner {
23578d280b98SDavid Chinner 	xfs_icsb_cnts_t	cnt;
23588d280b98SDavid Chinner 
23598d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
23608d280b98SDavid Chinner 
236120b64285SDavid Chinner 	/*
236220b64285SDavid Chinner 	 * If we are already disabled, then there is nothing to do
236320b64285SDavid Chinner 	 * here. We check before locking all the counters to avoid
236420b64285SDavid Chinner 	 * the expensive lock operation when being called in the
236520b64285SDavid Chinner 	 * slow path and the counter is already disabled. This is
236620b64285SDavid Chinner 	 * safe because the only time we set or clear this state is under
236720b64285SDavid Chinner 	 * the m_icsb_mutex.
236820b64285SDavid Chinner 	 */
236920b64285SDavid Chinner 	if (xfs_icsb_counter_disabled(mp, field))
237036fbe6e6SDavid Chinner 		return;
237120b64285SDavid Chinner 
23728d280b98SDavid Chinner 	xfs_icsb_lock_all_counters(mp);
23738d280b98SDavid Chinner 	if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
23748d280b98SDavid Chinner 		/* drain back to superblock */
23758d280b98SDavid Chinner 
2376ce46193bSChristoph Hellwig 		xfs_icsb_count(mp, &cnt, XFS_ICSB_LAZY_COUNT);
23778d280b98SDavid Chinner 		switch(field) {
23788d280b98SDavid Chinner 		case XFS_SBS_ICOUNT:
23798d280b98SDavid Chinner 			mp->m_sb.sb_icount = cnt.icsb_icount;
23808d280b98SDavid Chinner 			break;
23818d280b98SDavid Chinner 		case XFS_SBS_IFREE:
23828d280b98SDavid Chinner 			mp->m_sb.sb_ifree = cnt.icsb_ifree;
23838d280b98SDavid Chinner 			break;
23848d280b98SDavid Chinner 		case XFS_SBS_FDBLOCKS:
23858d280b98SDavid Chinner 			mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
23868d280b98SDavid Chinner 			break;
23878d280b98SDavid Chinner 		default:
23888d280b98SDavid Chinner 			BUG();
23898d280b98SDavid Chinner 		}
23908d280b98SDavid Chinner 	}
23918d280b98SDavid Chinner 
23928d280b98SDavid Chinner 	xfs_icsb_unlock_all_counters(mp);
23938d280b98SDavid Chinner }
23948d280b98SDavid Chinner 
23958d280b98SDavid Chinner STATIC void
23968d280b98SDavid Chinner xfs_icsb_enable_counter(
23978d280b98SDavid Chinner 	xfs_mount_t	*mp,
23988d280b98SDavid Chinner 	xfs_sb_field_t	field,
23998d280b98SDavid Chinner 	uint64_t	count,
24008d280b98SDavid Chinner 	uint64_t	resid)
24018d280b98SDavid Chinner {
24028d280b98SDavid Chinner 	xfs_icsb_cnts_t	*cntp;
24038d280b98SDavid Chinner 	int		i;
24048d280b98SDavid Chinner 
24058d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
24068d280b98SDavid Chinner 
24078d280b98SDavid Chinner 	xfs_icsb_lock_all_counters(mp);
24088d280b98SDavid Chinner 	for_each_online_cpu(i) {
24098d280b98SDavid Chinner 		cntp = per_cpu_ptr(mp->m_sb_cnts, i);
24108d280b98SDavid Chinner 		switch (field) {
24118d280b98SDavid Chinner 		case XFS_SBS_ICOUNT:
24128d280b98SDavid Chinner 			cntp->icsb_icount = count + resid;
24138d280b98SDavid Chinner 			break;
24148d280b98SDavid Chinner 		case XFS_SBS_IFREE:
24158d280b98SDavid Chinner 			cntp->icsb_ifree = count + resid;
24168d280b98SDavid Chinner 			break;
24178d280b98SDavid Chinner 		case XFS_SBS_FDBLOCKS:
24188d280b98SDavid Chinner 			cntp->icsb_fdblocks = count + resid;
24198d280b98SDavid Chinner 			break;
24208d280b98SDavid Chinner 		default:
24218d280b98SDavid Chinner 			BUG();
24228d280b98SDavid Chinner 			break;
24238d280b98SDavid Chinner 		}
24248d280b98SDavid Chinner 		resid = 0;
24258d280b98SDavid Chinner 	}
24268d280b98SDavid Chinner 	clear_bit(field, &mp->m_icsb_counters);
24278d280b98SDavid Chinner 	xfs_icsb_unlock_all_counters(mp);
24288d280b98SDavid Chinner }
24298d280b98SDavid Chinner 
2430dbcabad1SDavid Chinner void
2431d4d90b57SChristoph Hellwig xfs_icsb_sync_counters_locked(
24328d280b98SDavid Chinner 	xfs_mount_t	*mp,
24338d280b98SDavid Chinner 	int		flags)
24348d280b98SDavid Chinner {
24358d280b98SDavid Chinner 	xfs_icsb_cnts_t	cnt;
24368d280b98SDavid Chinner 
24378d280b98SDavid Chinner 	xfs_icsb_count(mp, &cnt, flags);
24388d280b98SDavid Chinner 
24398d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
24408d280b98SDavid Chinner 		mp->m_sb.sb_icount = cnt.icsb_icount;
24418d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
24428d280b98SDavid Chinner 		mp->m_sb.sb_ifree = cnt.icsb_ifree;
24438d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
24448d280b98SDavid Chinner 		mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
24458d280b98SDavid Chinner }
24468d280b98SDavid Chinner 
24478d280b98SDavid Chinner /*
24488d280b98SDavid Chinner  * Accurate update of per-cpu counters to incore superblock
24498d280b98SDavid Chinner  */
2450d4d90b57SChristoph Hellwig void
24518d280b98SDavid Chinner xfs_icsb_sync_counters(
2452d4d90b57SChristoph Hellwig 	xfs_mount_t	*mp,
2453d4d90b57SChristoph Hellwig 	int		flags)
24548d280b98SDavid Chinner {
2455d4d90b57SChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
2456d4d90b57SChristoph Hellwig 	xfs_icsb_sync_counters_locked(mp, flags);
2457d4d90b57SChristoph Hellwig 	spin_unlock(&mp->m_sb_lock);
24588d280b98SDavid Chinner }
24598d280b98SDavid Chinner 
24608d280b98SDavid Chinner /*
24618d280b98SDavid Chinner  * Balance and enable/disable counters as necessary.
24628d280b98SDavid Chinner  *
246320b64285SDavid Chinner  * Thresholds for re-enabling counters are somewhat magic.  inode counts are
246420b64285SDavid Chinner  * chosen to be the same number as single on disk allocation chunk per CPU, and
246520b64285SDavid Chinner  * free blocks is something far enough zero that we aren't going thrash when we
246620b64285SDavid Chinner  * get near ENOSPC. We also need to supply a minimum we require per cpu to
246720b64285SDavid Chinner  * prevent looping endlessly when xfs_alloc_space asks for more than will
246820b64285SDavid Chinner  * be distributed to a single CPU but each CPU has enough blocks to be
246920b64285SDavid Chinner  * reenabled.
247020b64285SDavid Chinner  *
247120b64285SDavid Chinner  * Note that we can be called when counters are already disabled.
247220b64285SDavid Chinner  * xfs_icsb_disable_counter() optimises the counter locking in this case to
247320b64285SDavid Chinner  * prevent locking every per-cpu counter needlessly.
24748d280b98SDavid Chinner  */
247520b64285SDavid Chinner 
247620b64285SDavid Chinner #define XFS_ICSB_INO_CNTR_REENABLE	(uint64_t)64
24774be536deSDavid Chinner #define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \
247820b64285SDavid Chinner 		(uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp))
24798d280b98SDavid Chinner STATIC void
248045af6c6dSChristoph Hellwig xfs_icsb_balance_counter_locked(
24818d280b98SDavid Chinner 	xfs_mount_t	*mp,
24828d280b98SDavid Chinner 	xfs_sb_field_t  field,
248320b64285SDavid Chinner 	int		min_per_cpu)
24848d280b98SDavid Chinner {
24856fdf8cccSNathan Scott 	uint64_t	count, resid;
24868d280b98SDavid Chinner 	int		weight = num_online_cpus();
248720b64285SDavid Chinner 	uint64_t	min = (uint64_t)min_per_cpu;
24888d280b98SDavid Chinner 
24898d280b98SDavid Chinner 	/* disable counter and sync counter */
24908d280b98SDavid Chinner 	xfs_icsb_disable_counter(mp, field);
24918d280b98SDavid Chinner 
24928d280b98SDavid Chinner 	/* update counters  - first CPU gets residual*/
24938d280b98SDavid Chinner 	switch (field) {
24948d280b98SDavid Chinner 	case XFS_SBS_ICOUNT:
24958d280b98SDavid Chinner 		count = mp->m_sb.sb_icount;
24968d280b98SDavid Chinner 		resid = do_div(count, weight);
249720b64285SDavid Chinner 		if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
249845af6c6dSChristoph Hellwig 			return;
24998d280b98SDavid Chinner 		break;
25008d280b98SDavid Chinner 	case XFS_SBS_IFREE:
25018d280b98SDavid Chinner 		count = mp->m_sb.sb_ifree;
25028d280b98SDavid Chinner 		resid = do_div(count, weight);
250320b64285SDavid Chinner 		if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
250445af6c6dSChristoph Hellwig 			return;
25058d280b98SDavid Chinner 		break;
25068d280b98SDavid Chinner 	case XFS_SBS_FDBLOCKS:
25078d280b98SDavid Chinner 		count = mp->m_sb.sb_fdblocks;
25088d280b98SDavid Chinner 		resid = do_div(count, weight);
250920b64285SDavid Chinner 		if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp)))
251045af6c6dSChristoph Hellwig 			return;
25118d280b98SDavid Chinner 		break;
25128d280b98SDavid Chinner 	default:
25138d280b98SDavid Chinner 		BUG();
25146fdf8cccSNathan Scott 		count = resid = 0;	/* quiet, gcc */
25158d280b98SDavid Chinner 		break;
25168d280b98SDavid Chinner 	}
25178d280b98SDavid Chinner 
25188d280b98SDavid Chinner 	xfs_icsb_enable_counter(mp, field, count, resid);
251945af6c6dSChristoph Hellwig }
252045af6c6dSChristoph Hellwig 
252145af6c6dSChristoph Hellwig STATIC void
252245af6c6dSChristoph Hellwig xfs_icsb_balance_counter(
252345af6c6dSChristoph Hellwig 	xfs_mount_t	*mp,
252445af6c6dSChristoph Hellwig 	xfs_sb_field_t  fields,
252545af6c6dSChristoph Hellwig 	int		min_per_cpu)
252645af6c6dSChristoph Hellwig {
252745af6c6dSChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
252845af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter_locked(mp, fields, min_per_cpu);
25293685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
25308d280b98SDavid Chinner }
25318d280b98SDavid Chinner 
25321b040712SChristoph Hellwig int
253320b64285SDavid Chinner xfs_icsb_modify_counters(
25348d280b98SDavid Chinner 	xfs_mount_t	*mp,
25358d280b98SDavid Chinner 	xfs_sb_field_t	field,
253620f4ebf2SDavid Chinner 	int64_t		delta,
253720b64285SDavid Chinner 	int		rsvd)
25388d280b98SDavid Chinner {
25398d280b98SDavid Chinner 	xfs_icsb_cnts_t	*icsbp;
25408d280b98SDavid Chinner 	long long	lcounter;	/* long counter for 64 bit fields */
25417a9e02d6SChristoph Lameter 	int		ret = 0;
25428d280b98SDavid Chinner 
254320b64285SDavid Chinner 	might_sleep();
25448d280b98SDavid Chinner again:
25457a9e02d6SChristoph Lameter 	preempt_disable();
25467a9e02d6SChristoph Lameter 	icsbp = this_cpu_ptr(mp->m_sb_cnts);
254720b64285SDavid Chinner 
254820b64285SDavid Chinner 	/*
254920b64285SDavid Chinner 	 * if the counter is disabled, go to slow path
255020b64285SDavid Chinner 	 */
25518d280b98SDavid Chinner 	if (unlikely(xfs_icsb_counter_disabled(mp, field)))
25528d280b98SDavid Chinner 		goto slow_path;
255320b64285SDavid Chinner 	xfs_icsb_lock_cntr(icsbp);
255420b64285SDavid Chinner 	if (unlikely(xfs_icsb_counter_disabled(mp, field))) {
255520b64285SDavid Chinner 		xfs_icsb_unlock_cntr(icsbp);
255620b64285SDavid Chinner 		goto slow_path;
255720b64285SDavid Chinner 	}
25588d280b98SDavid Chinner 
25598d280b98SDavid Chinner 	switch (field) {
25608d280b98SDavid Chinner 	case XFS_SBS_ICOUNT:
25618d280b98SDavid Chinner 		lcounter = icsbp->icsb_icount;
25628d280b98SDavid Chinner 		lcounter += delta;
25638d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
256420b64285SDavid Chinner 			goto balance_counter;
25658d280b98SDavid Chinner 		icsbp->icsb_icount = lcounter;
25668d280b98SDavid Chinner 		break;
25678d280b98SDavid Chinner 
25688d280b98SDavid Chinner 	case XFS_SBS_IFREE:
25698d280b98SDavid Chinner 		lcounter = icsbp->icsb_ifree;
25708d280b98SDavid Chinner 		lcounter += delta;
25718d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
257220b64285SDavid Chinner 			goto balance_counter;
25738d280b98SDavid Chinner 		icsbp->icsb_ifree = lcounter;
25748d280b98SDavid Chinner 		break;
25758d280b98SDavid Chinner 
25768d280b98SDavid Chinner 	case XFS_SBS_FDBLOCKS:
25778d280b98SDavid Chinner 		BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
25788d280b98SDavid Chinner 
25794be536deSDavid Chinner 		lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
25808d280b98SDavid Chinner 		lcounter += delta;
25818d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
258220b64285SDavid Chinner 			goto balance_counter;
25834be536deSDavid Chinner 		icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
25848d280b98SDavid Chinner 		break;
25858d280b98SDavid Chinner 	default:
25868d280b98SDavid Chinner 		BUG();
25878d280b98SDavid Chinner 		break;
25888d280b98SDavid Chinner 	}
258901e1b69cSDavid Chinner 	xfs_icsb_unlock_cntr(icsbp);
25907a9e02d6SChristoph Lameter 	preempt_enable();
25918d280b98SDavid Chinner 	return 0;
25928d280b98SDavid Chinner 
25938d280b98SDavid Chinner slow_path:
25947a9e02d6SChristoph Lameter 	preempt_enable();
259520b64285SDavid Chinner 
259620b64285SDavid Chinner 	/*
259720b64285SDavid Chinner 	 * serialise with a mutex so we don't burn lots of cpu on
259820b64285SDavid Chinner 	 * the superblock lock. We still need to hold the superblock
259920b64285SDavid Chinner 	 * lock, however, when we modify the global structures.
260020b64285SDavid Chinner 	 */
260103135cf7SDavid Chinner 	xfs_icsb_lock(mp);
260220b64285SDavid Chinner 
260320b64285SDavid Chinner 	/*
260420b64285SDavid Chinner 	 * Now running atomically.
260520b64285SDavid Chinner 	 *
260620b64285SDavid Chinner 	 * If the counter is enabled, someone has beaten us to rebalancing.
260720b64285SDavid Chinner 	 * Drop the lock and try again in the fast path....
260820b64285SDavid Chinner 	 */
260920b64285SDavid Chinner 	if (!(xfs_icsb_counter_disabled(mp, field))) {
261003135cf7SDavid Chinner 		xfs_icsb_unlock(mp);
261120b64285SDavid Chinner 		goto again;
261220b64285SDavid Chinner 	}
261320b64285SDavid Chinner 
261420b64285SDavid Chinner 	/*
261520b64285SDavid Chinner 	 * The counter is currently disabled. Because we are
261620b64285SDavid Chinner 	 * running atomically here, we know a rebalance cannot
261720b64285SDavid Chinner 	 * be in progress. Hence we can go straight to operating
261820b64285SDavid Chinner 	 * on the global superblock. We do not call xfs_mod_incore_sb()
26193685c2a1SEric Sandeen 	 * here even though we need to get the m_sb_lock. Doing so
262020b64285SDavid Chinner 	 * will cause us to re-enter this function and deadlock.
26213685c2a1SEric Sandeen 	 * Hence we get the m_sb_lock ourselves and then call
262220b64285SDavid Chinner 	 * xfs_mod_incore_sb_unlocked() as the unlocked path operates
262320b64285SDavid Chinner 	 * directly on the global counters.
262420b64285SDavid Chinner 	 */
26253685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
262620b64285SDavid Chinner 	ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
26273685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
262820b64285SDavid Chinner 
262920b64285SDavid Chinner 	/*
263020b64285SDavid Chinner 	 * Now that we've modified the global superblock, we
263120b64285SDavid Chinner 	 * may be able to re-enable the distributed counters
263220b64285SDavid Chinner 	 * (e.g. lots of space just got freed). After that
263320b64285SDavid Chinner 	 * we are done.
263420b64285SDavid Chinner 	 */
263520b64285SDavid Chinner 	if (ret != ENOSPC)
263645af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, field, 0);
263703135cf7SDavid Chinner 	xfs_icsb_unlock(mp);
263820b64285SDavid Chinner 	return ret;
263920b64285SDavid Chinner 
264020b64285SDavid Chinner balance_counter:
264101e1b69cSDavid Chinner 	xfs_icsb_unlock_cntr(icsbp);
26427a9e02d6SChristoph Lameter 	preempt_enable();
26438d280b98SDavid Chinner 
264420b64285SDavid Chinner 	/*
264520b64285SDavid Chinner 	 * We may have multiple threads here if multiple per-cpu
264620b64285SDavid Chinner 	 * counters run dry at the same time. This will mean we can
264720b64285SDavid Chinner 	 * do more balances than strictly necessary but it is not
264820b64285SDavid Chinner 	 * the common slowpath case.
264920b64285SDavid Chinner 	 */
265003135cf7SDavid Chinner 	xfs_icsb_lock(mp);
265120b64285SDavid Chinner 
265220b64285SDavid Chinner 	/*
265320b64285SDavid Chinner 	 * running atomically.
265420b64285SDavid Chinner 	 *
265520b64285SDavid Chinner 	 * This will leave the counter in the correct state for future
265620b64285SDavid Chinner 	 * accesses. After the rebalance, we simply try again and our retry
265720b64285SDavid Chinner 	 * will either succeed through the fast path or slow path without
265820b64285SDavid Chinner 	 * another balance operation being required.
265920b64285SDavid Chinner 	 */
266045af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, field, delta);
266103135cf7SDavid Chinner 	xfs_icsb_unlock(mp);
26628d280b98SDavid Chinner 	goto again;
26638d280b98SDavid Chinner }
26648d280b98SDavid Chinner 
26658d280b98SDavid Chinner #endif
2666