xref: /linux/fs/xfs/xfs_mount.c (revision c854363e80b49dd04a4de18ebc379eb8c8806674)
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"
251da177e4SLinus Torvalds #include "xfs_sb.h"
261da177e4SLinus Torvalds #include "xfs_ag.h"
271da177e4SLinus Torvalds #include "xfs_dir2.h"
281da177e4SLinus Torvalds #include "xfs_dmapi.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_dir2_sf.h"
34a844f451SNathan Scott #include "xfs_attr_sf.h"
351da177e4SLinus Torvalds #include "xfs_dinode.h"
361da177e4SLinus Torvalds #include "xfs_inode.h"
37a844f451SNathan Scott #include "xfs_btree.h"
38a844f451SNathan Scott #include "xfs_ialloc.h"
391da177e4SLinus Torvalds #include "xfs_alloc.h"
401da177e4SLinus Torvalds #include "xfs_rtalloc.h"
411da177e4SLinus Torvalds #include "xfs_bmap.h"
421da177e4SLinus Torvalds #include "xfs_error.h"
431da177e4SLinus Torvalds #include "xfs_rw.h"
441da177e4SLinus Torvalds #include "xfs_quota.h"
451da177e4SLinus Torvalds #include "xfs_fsops.h"
4643355099SChristoph Hellwig #include "xfs_utils.h"
470b1b213fSChristoph Hellwig #include "xfs_trace.h"
480b1b213fSChristoph Hellwig 
491da177e4SLinus Torvalds 
50ba0f32d4SChristoph Hellwig STATIC void	xfs_unmountfs_wait(xfs_mount_t *);
511da177e4SLinus Torvalds 
528d280b98SDavid Chinner 
538d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
5420f4ebf2SDavid Chinner STATIC void	xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t,
5545af6c6dSChristoph Hellwig 						int);
5645af6c6dSChristoph Hellwig STATIC void	xfs_icsb_balance_counter_locked(xfs_mount_t *, xfs_sb_field_t,
5745af6c6dSChristoph Hellwig 						int);
588d280b98SDavid Chinner STATIC int	xfs_icsb_modify_counters(xfs_mount_t *, xfs_sb_field_t,
5920f4ebf2SDavid Chinner 						int64_t, int);
6036fbe6e6SDavid Chinner STATIC void	xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
618d280b98SDavid Chinner 
628d280b98SDavid Chinner #else
638d280b98SDavid Chinner 
6445af6c6dSChristoph Hellwig #define xfs_icsb_balance_counter(mp, a, b)		do { } while (0)
6545af6c6dSChristoph Hellwig #define xfs_icsb_balance_counter_locked(mp, a, b)	do { } while (0)
668d280b98SDavid Chinner #define xfs_icsb_modify_counters(mp, a, b, c)		do { } while (0)
678d280b98SDavid Chinner 
688d280b98SDavid Chinner #endif
698d280b98SDavid Chinner 
701df84c93SChristoph Hellwig static const struct {
711da177e4SLinus Torvalds 	short offset;
721da177e4SLinus Torvalds 	short type;	/* 0 = integer
731da177e4SLinus Torvalds 			 * 1 = binary / string (no translation)
741da177e4SLinus Torvalds 			 */
751da177e4SLinus Torvalds } xfs_sb_info[] = {
761da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_magicnum),   0 },
771da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_blocksize),  0 },
781da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_dblocks),    0 },
791da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rblocks),    0 },
801da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rextents),   0 },
811da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_uuid),       1 },
821da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logstart),   0 },
831da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rootino),    0 },
841da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rbmino),     0 },
851da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rsumino),    0 },
861da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rextsize),   0 },
871da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_agblocks),   0 },
881da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_agcount),    0 },
891da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rbmblocks),  0 },
901da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logblocks),  0 },
911da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_versionnum), 0 },
921da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_sectsize),   0 },
931da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inodesize),  0 },
941da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inopblock),  0 },
951da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_fname[0]),   1 },
961da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_blocklog),   0 },
971da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_sectlog),    0 },
981da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inodelog),   0 },
991da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inopblog),   0 },
1001da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_agblklog),   0 },
1011da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rextslog),   0 },
1021da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inprogress), 0 },
1031da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_imax_pct),   0 },
1041da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_icount),     0 },
1051da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_ifree),      0 },
1061da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_fdblocks),   0 },
1071da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_frextents),  0 },
1081da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_uquotino),   0 },
1091da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_gquotino),   0 },
1101da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_qflags),     0 },
1111da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_flags),      0 },
1121da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_shared_vn),  0 },
1131da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
1141da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_unit),	 0 },
1151da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_width),	 0 },
1161da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_dirblklog),	 0 },
1171da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logsectlog), 0 },
1181da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logsectsize),0 },
1191da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logsunit),	 0 },
1201da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_features2),	 0 },
121ee1c0908SDavid Chinner     { offsetof(xfs_sb_t, sb_bad_features2), 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)) {
14427174203SChristoph Hellwig 		cmn_err(CE_WARN,
14527174203SChristoph Hellwig 			"XFS: Filesystem %s has nil UUID - can't mount",
14627174203SChristoph Hellwig 			mp->m_fsname);
14727174203SChristoph Hellwig 		return XFS_ERROR(EINVAL);
14827174203SChristoph Hellwig 	}
14927174203SChristoph Hellwig 
15027174203SChristoph Hellwig 	mutex_lock(&xfs_uuid_table_mutex);
15127174203SChristoph Hellwig 	for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) {
15227174203SChristoph Hellwig 		if (uuid_is_nil(&xfs_uuid_table[i])) {
15327174203SChristoph Hellwig 			hole = i;
15427174203SChristoph Hellwig 			continue;
15527174203SChristoph Hellwig 		}
15627174203SChristoph Hellwig 		if (uuid_equal(uuid, &xfs_uuid_table[i]))
15727174203SChristoph Hellwig 			goto out_duplicate;
15827174203SChristoph Hellwig 	}
15927174203SChristoph Hellwig 
16027174203SChristoph Hellwig 	if (hole < 0) {
16127174203SChristoph Hellwig 		xfs_uuid_table = kmem_realloc(xfs_uuid_table,
16227174203SChristoph Hellwig 			(xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table),
16327174203SChristoph Hellwig 			xfs_uuid_table_size  * sizeof(*xfs_uuid_table),
16427174203SChristoph Hellwig 			KM_SLEEP);
16527174203SChristoph Hellwig 		hole = xfs_uuid_table_size++;
16627174203SChristoph Hellwig 	}
16727174203SChristoph Hellwig 	xfs_uuid_table[hole] = *uuid;
16827174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
16927174203SChristoph Hellwig 
17027174203SChristoph Hellwig 	return 0;
17127174203SChristoph Hellwig 
17227174203SChristoph Hellwig  out_duplicate:
17327174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
17427174203SChristoph Hellwig 	cmn_err(CE_WARN, "XFS: Filesystem %s has duplicate UUID - can't mount",
17527174203SChristoph Hellwig 			 mp->m_fsname);
17627174203SChristoph Hellwig 	return XFS_ERROR(EINVAL);
17727174203SChristoph Hellwig }
17827174203SChristoph Hellwig 
17927174203SChristoph Hellwig STATIC void
18027174203SChristoph Hellwig xfs_uuid_unmount(
18127174203SChristoph Hellwig 	struct xfs_mount	*mp)
18227174203SChristoph Hellwig {
18327174203SChristoph Hellwig 	uuid_t			*uuid = &mp->m_sb.sb_uuid;
18427174203SChristoph Hellwig 	int			i;
18527174203SChristoph Hellwig 
18627174203SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_NOUUID)
18727174203SChristoph Hellwig 		return;
18827174203SChristoph Hellwig 
18927174203SChristoph Hellwig 	mutex_lock(&xfs_uuid_table_mutex);
19027174203SChristoph Hellwig 	for (i = 0; i < xfs_uuid_table_size; i++) {
19127174203SChristoph Hellwig 		if (uuid_is_nil(&xfs_uuid_table[i]))
19227174203SChristoph Hellwig 			continue;
19327174203SChristoph Hellwig 		if (!uuid_equal(uuid, &xfs_uuid_table[i]))
19427174203SChristoph Hellwig 			continue;
19527174203SChristoph Hellwig 		memset(&xfs_uuid_table[i], 0, sizeof(uuid_t));
19627174203SChristoph Hellwig 		break;
19727174203SChristoph Hellwig 	}
19827174203SChristoph Hellwig 	ASSERT(i < xfs_uuid_table_size);
19927174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
20027174203SChristoph Hellwig }
20127174203SChristoph Hellwig 
20227174203SChristoph Hellwig 
2031da177e4SLinus Torvalds /*
2040fa800fbSDave Chinner  * Reference counting access wrappers to the perag structures.
2050fa800fbSDave Chinner  */
2060fa800fbSDave Chinner struct xfs_perag *
2070fa800fbSDave Chinner xfs_perag_get(struct xfs_mount *mp, xfs_agnumber_t agno)
2080fa800fbSDave Chinner {
2090fa800fbSDave Chinner 	struct xfs_perag	*pag;
2100fa800fbSDave Chinner 	int			ref = 0;
2110fa800fbSDave Chinner 
2120fa800fbSDave Chinner 	spin_lock(&mp->m_perag_lock);
2130fa800fbSDave Chinner 	pag = radix_tree_lookup(&mp->m_perag_tree, agno);
2140fa800fbSDave Chinner 	if (pag) {
2150fa800fbSDave Chinner 		ASSERT(atomic_read(&pag->pag_ref) >= 0);
2160fa800fbSDave Chinner 		/* catch leaks in the positive direction during testing */
2170fa800fbSDave Chinner 		ASSERT(atomic_read(&pag->pag_ref) < 1000);
2180fa800fbSDave Chinner 		ref = atomic_inc_return(&pag->pag_ref);
2190fa800fbSDave Chinner 	}
2200fa800fbSDave Chinner 	spin_unlock(&mp->m_perag_lock);
2210fa800fbSDave Chinner 	trace_xfs_perag_get(mp, agno, ref, _RET_IP_);
2220fa800fbSDave Chinner 	return pag;
2230fa800fbSDave Chinner }
2240fa800fbSDave Chinner 
2250fa800fbSDave Chinner void
2260fa800fbSDave Chinner xfs_perag_put(struct xfs_perag *pag)
2270fa800fbSDave Chinner {
2280fa800fbSDave Chinner 	int	ref;
2290fa800fbSDave Chinner 
2300fa800fbSDave Chinner 	ASSERT(atomic_read(&pag->pag_ref) > 0);
2310fa800fbSDave Chinner 	ref = atomic_dec_return(&pag->pag_ref);
2320fa800fbSDave Chinner 	trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_);
2330fa800fbSDave Chinner }
2340fa800fbSDave Chinner 
2350fa800fbSDave Chinner /*
2361da177e4SLinus Torvalds  * Free up the resources associated with a mount structure.  Assume that
2371da177e4SLinus Torvalds  * the structure was initially zeroed, so we can tell which fields got
2381da177e4SLinus Torvalds  * initialized.
2391da177e4SLinus Torvalds  */
240c962fb79SChristoph Hellwig STATIC void
241ff4f038cSChristoph Hellwig xfs_free_perag(
242745f6919SChristoph Hellwig 	xfs_mount_t	*mp)
2431da177e4SLinus Torvalds {
2441c1c6ebcSDave Chinner 	xfs_agnumber_t	agno;
2451c1c6ebcSDave Chinner 	struct xfs_perag *pag;
2461da177e4SLinus Torvalds 
2471c1c6ebcSDave Chinner 	for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
2481c1c6ebcSDave Chinner 		spin_lock(&mp->m_perag_lock);
2491c1c6ebcSDave Chinner 		pag = radix_tree_delete(&mp->m_perag_tree, agno);
250e57336ffSDave Chinner 		ASSERT(pag);
251aed3bb90SDave Chinner 		ASSERT(atomic_read(&pag->pag_ref) == 0);
2521c1c6ebcSDave Chinner 		spin_unlock(&mp->m_perag_lock);
2531c1c6ebcSDave Chinner 		kmem_free(pag);
2541da177e4SLinus Torvalds 	}
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
2574cc929eeSNathan Scott /*
2584cc929eeSNathan Scott  * Check size of device based on the (data/realtime) block count.
2594cc929eeSNathan Scott  * Note: this check is used by the growfs code as well as mount.
2604cc929eeSNathan Scott  */
2614cc929eeSNathan Scott int
2624cc929eeSNathan Scott xfs_sb_validate_fsb_count(
2634cc929eeSNathan Scott 	xfs_sb_t	*sbp,
2644cc929eeSNathan Scott 	__uint64_t	nblocks)
2654cc929eeSNathan Scott {
2664cc929eeSNathan Scott 	ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
2674cc929eeSNathan Scott 	ASSERT(sbp->sb_blocklog >= BBSHIFT);
2684cc929eeSNathan Scott 
2694cc929eeSNathan Scott #if XFS_BIG_BLKNOS     /* Limited by ULONG_MAX of page cache index */
2704cc929eeSNathan Scott 	if (nblocks >> (PAGE_CACHE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
2714cc929eeSNathan Scott 		return E2BIG;
2724cc929eeSNathan Scott #else                  /* Limited by UINT_MAX of sectors */
2734cc929eeSNathan Scott 	if (nblocks << (sbp->sb_blocklog - BBSHIFT) > UINT_MAX)
2744cc929eeSNathan Scott 		return E2BIG;
2754cc929eeSNathan Scott #endif
2764cc929eeSNathan Scott 	return 0;
2774cc929eeSNathan Scott }
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds /*
2801da177e4SLinus Torvalds  * Check the validity of the SB found.
2811da177e4SLinus Torvalds  */
2821da177e4SLinus Torvalds STATIC int
2831da177e4SLinus Torvalds xfs_mount_validate_sb(
2841da177e4SLinus Torvalds 	xfs_mount_t	*mp,
285764d1f89SNathan Scott 	xfs_sb_t	*sbp,
286764d1f89SNathan Scott 	int		flags)
2871da177e4SLinus Torvalds {
2881da177e4SLinus Torvalds 	/*
2891da177e4SLinus Torvalds 	 * If the log device and data device have the
2901da177e4SLinus Torvalds 	 * same device number, the log is internal.
2911da177e4SLinus Torvalds 	 * Consequently, the sb_logstart should be non-zero.  If
2921da177e4SLinus Torvalds 	 * we have a zero sb_logstart in this case, we may be trying to mount
2931da177e4SLinus Torvalds 	 * a volume filesystem in a non-volume manner.
2941da177e4SLinus Torvalds 	 */
2951da177e4SLinus Torvalds 	if (sbp->sb_magicnum != XFS_SB_MAGIC) {
296764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags, "bad magic number");
2971da177e4SLinus Torvalds 		return XFS_ERROR(EWRONGFS);
2981da177e4SLinus Torvalds 	}
2991da177e4SLinus Torvalds 
30062118709SEric Sandeen 	if (!xfs_sb_good_version(sbp)) {
301764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags, "bad version");
3021da177e4SLinus Torvalds 		return XFS_ERROR(EWRONGFS);
3031da177e4SLinus Torvalds 	}
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	if (unlikely(
3061da177e4SLinus Torvalds 	    sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
307764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags,
308764d1f89SNathan Scott 			"filesystem is marked as having an external log; "
3091da177e4SLinus Torvalds 			"specify logdev on the\nmount command line.");
310764d1f89SNathan Scott 		return XFS_ERROR(EINVAL);
3111da177e4SLinus Torvalds 	}
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	if (unlikely(
3141da177e4SLinus Torvalds 	    sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
315764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags,
316764d1f89SNathan Scott 			"filesystem is marked as having an internal log; "
317764d1f89SNathan Scott 			"do not specify logdev on\nthe mount command line.");
318764d1f89SNathan Scott 		return XFS_ERROR(EINVAL);
3191da177e4SLinus Torvalds 	}
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	/*
3221da177e4SLinus Torvalds 	 * More sanity checking. These were stolen directly from
3231da177e4SLinus Torvalds 	 * xfs_repair.
3241da177e4SLinus Torvalds 	 */
3251da177e4SLinus Torvalds 	if (unlikely(
3261da177e4SLinus Torvalds 	    sbp->sb_agcount <= 0					||
3271da177e4SLinus Torvalds 	    sbp->sb_sectsize < XFS_MIN_SECTORSIZE			||
3281da177e4SLinus Torvalds 	    sbp->sb_sectsize > XFS_MAX_SECTORSIZE			||
3291da177e4SLinus Torvalds 	    sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG			||
3301da177e4SLinus Torvalds 	    sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG			||
3312ac00af7SOlaf Weber 	    sbp->sb_sectsize != (1 << sbp->sb_sectlog)			||
3321da177e4SLinus Torvalds 	    sbp->sb_blocksize < XFS_MIN_BLOCKSIZE			||
3331da177e4SLinus Torvalds 	    sbp->sb_blocksize > XFS_MAX_BLOCKSIZE			||
3341da177e4SLinus Torvalds 	    sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG			||
3351da177e4SLinus Torvalds 	    sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG			||
3362ac00af7SOlaf Weber 	    sbp->sb_blocksize != (1 << sbp->sb_blocklog)		||
3371da177e4SLinus Torvalds 	    sbp->sb_inodesize < XFS_DINODE_MIN_SIZE			||
3381da177e4SLinus Torvalds 	    sbp->sb_inodesize > XFS_DINODE_MAX_SIZE			||
3399f989c94SNathan Scott 	    sbp->sb_inodelog < XFS_DINODE_MIN_LOG			||
3409f989c94SNathan Scott 	    sbp->sb_inodelog > XFS_DINODE_MAX_LOG			||
3412ac00af7SOlaf Weber 	    sbp->sb_inodesize != (1 << sbp->sb_inodelog)		||
3429f989c94SNathan Scott 	    (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog)	||
3431da177e4SLinus Torvalds 	    (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE)	||
3441da177e4SLinus Torvalds 	    (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE)	||
345e50bd16fSNathan Scott 	    (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */))) {
346764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags, "SB sanity check 1 failed");
3471da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
3481da177e4SLinus Torvalds 	}
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	/*
3511da177e4SLinus Torvalds 	 * Sanity check AG count, size fields against data size field
3521da177e4SLinus Torvalds 	 */
3531da177e4SLinus Torvalds 	if (unlikely(
3541da177e4SLinus Torvalds 	    sbp->sb_dblocks == 0 ||
3551da177e4SLinus Torvalds 	    sbp->sb_dblocks >
3561da177e4SLinus Torvalds 	     (xfs_drfsbno_t)sbp->sb_agcount * sbp->sb_agblocks ||
3571da177e4SLinus Torvalds 	    sbp->sb_dblocks < (xfs_drfsbno_t)(sbp->sb_agcount - 1) *
3581da177e4SLinus Torvalds 			      sbp->sb_agblocks + XFS_MIN_AG_BLOCKS)) {
359764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags, "SB sanity check 2 failed");
3601da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
3611da177e4SLinus Torvalds 	}
3621da177e4SLinus Torvalds 
3632edbddd5SLachlan McIlroy 	/*
3642edbddd5SLachlan McIlroy 	 * Until this is fixed only page-sized or smaller data blocks work.
3652edbddd5SLachlan McIlroy 	 */
3662edbddd5SLachlan McIlroy 	if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
3672edbddd5SLachlan McIlroy 		xfs_fs_mount_cmn_err(flags,
3682edbddd5SLachlan McIlroy 			"file system with blocksize %d bytes",
3692edbddd5SLachlan McIlroy 			sbp->sb_blocksize);
3702edbddd5SLachlan McIlroy 		xfs_fs_mount_cmn_err(flags,
3712edbddd5SLachlan McIlroy 			"only pagesize (%ld) or less will currently work.",
3722edbddd5SLachlan McIlroy 			PAGE_SIZE);
3732edbddd5SLachlan McIlroy 		return XFS_ERROR(ENOSYS);
3742edbddd5SLachlan McIlroy 	}
3752edbddd5SLachlan McIlroy 
3761a5902c5SChristoph Hellwig 	/*
3771a5902c5SChristoph Hellwig 	 * Currently only very few inode sizes are supported.
3781a5902c5SChristoph Hellwig 	 */
3791a5902c5SChristoph Hellwig 	switch (sbp->sb_inodesize) {
3801a5902c5SChristoph Hellwig 	case 256:
3811a5902c5SChristoph Hellwig 	case 512:
3821a5902c5SChristoph Hellwig 	case 1024:
3831a5902c5SChristoph Hellwig 	case 2048:
3841a5902c5SChristoph Hellwig 		break;
3851a5902c5SChristoph Hellwig 	default:
3861a5902c5SChristoph Hellwig 		xfs_fs_mount_cmn_err(flags,
3871a5902c5SChristoph Hellwig 			"inode size of %d bytes not supported",
3881a5902c5SChristoph Hellwig 			sbp->sb_inodesize);
3891a5902c5SChristoph Hellwig 		return XFS_ERROR(ENOSYS);
3901a5902c5SChristoph Hellwig 	}
3911a5902c5SChristoph Hellwig 
3924cc929eeSNathan Scott 	if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
3934cc929eeSNathan Scott 	    xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
394764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags,
395764d1f89SNathan Scott 			"file system too large to be mounted on this system.");
3961da177e4SLinus Torvalds 		return XFS_ERROR(E2BIG);
3971da177e4SLinus Torvalds 	}
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	if (unlikely(sbp->sb_inprogress)) {
400764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags, "file system busy");
4011da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
4021da177e4SLinus Torvalds 	}
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 	/*
405de20614bSNathan Scott 	 * Version 1 directory format has never worked on Linux.
406de20614bSNathan Scott 	 */
40762118709SEric Sandeen 	if (unlikely(!xfs_sb_version_hasdirv2(sbp))) {
408764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags,
409764d1f89SNathan Scott 			"file system using version 1 directory format");
410de20614bSNathan Scott 		return XFS_ERROR(ENOSYS);
411de20614bSNathan Scott 	}
412de20614bSNathan Scott 
4131da177e4SLinus Torvalds 	return 0;
4141da177e4SLinus Torvalds }
4151da177e4SLinus Torvalds 
416da353b0dSDavid Chinner STATIC void
417da353b0dSDavid Chinner xfs_initialize_perag_icache(
418da353b0dSDavid Chinner 	xfs_perag_t	*pag)
419da353b0dSDavid Chinner {
420da353b0dSDavid Chinner 	if (!pag->pag_ici_init) {
421da353b0dSDavid Chinner 		rwlock_init(&pag->pag_ici_lock);
422da353b0dSDavid Chinner 		INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
423da353b0dSDavid Chinner 		pag->pag_ici_init = 1;
424da353b0dSDavid Chinner 	}
425da353b0dSDavid Chinner }
426da353b0dSDavid Chinner 
4271c1c6ebcSDave Chinner int
428c11e2c36SNathan Scott xfs_initialize_perag(
429c11e2c36SNathan Scott 	xfs_mount_t	*mp,
4301c1c6ebcSDave Chinner 	xfs_agnumber_t	agcount,
4311c1c6ebcSDave Chinner 	xfs_agnumber_t	*maxagi)
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	xfs_agnumber_t	index, max_metadata;
4348b26c582SDave Chinner 	xfs_agnumber_t	first_initialised = 0;
4351da177e4SLinus Torvalds 	xfs_perag_t	*pag;
4361da177e4SLinus Torvalds 	xfs_agino_t	agino;
4371da177e4SLinus Torvalds 	xfs_ino_t	ino;
4381da177e4SLinus Torvalds 	xfs_sb_t	*sbp = &mp->m_sb;
4391da177e4SLinus Torvalds 	xfs_ino_t	max_inum = XFS_MAXINUMBER_32;
4408b26c582SDave Chinner 	int		error = -ENOMEM;
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	/* Check to see if the filesystem can overflow 32 bit inodes */
4431da177e4SLinus Torvalds 	agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
4441da177e4SLinus Torvalds 	ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
4451da177e4SLinus Torvalds 
4461c1c6ebcSDave Chinner 	/*
4471c1c6ebcSDave Chinner 	 * Walk the current per-ag tree so we don't try to initialise AGs
4481c1c6ebcSDave Chinner 	 * that already exist (growfs case). Allocate and insert all the
4491c1c6ebcSDave Chinner 	 * AGs we don't find ready for initialisation.
4501c1c6ebcSDave Chinner 	 */
4511c1c6ebcSDave Chinner 	for (index = 0; index < agcount; index++) {
4521c1c6ebcSDave Chinner 		pag = xfs_perag_get(mp, index);
4531c1c6ebcSDave Chinner 		if (pag) {
4541c1c6ebcSDave Chinner 			xfs_perag_put(pag);
4551c1c6ebcSDave Chinner 			continue;
4561c1c6ebcSDave Chinner 		}
4578b26c582SDave Chinner 		if (!first_initialised)
4588b26c582SDave Chinner 			first_initialised = index;
4591c1c6ebcSDave Chinner 		pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
4601c1c6ebcSDave Chinner 		if (!pag)
4618b26c582SDave Chinner 			goto out_unwind;
4621c1c6ebcSDave Chinner 		if (radix_tree_preload(GFP_NOFS))
4638b26c582SDave Chinner 			goto out_unwind;
4641c1c6ebcSDave Chinner 		spin_lock(&mp->m_perag_lock);
4651c1c6ebcSDave Chinner 		if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
4661c1c6ebcSDave Chinner 			BUG();
4671c1c6ebcSDave Chinner 			spin_unlock(&mp->m_perag_lock);
4688b26c582SDave Chinner 			radix_tree_preload_end();
4698b26c582SDave Chinner 			error = -EEXIST;
4708b26c582SDave Chinner 			goto out_unwind;
4711c1c6ebcSDave Chinner 		}
4720fa800fbSDave Chinner 		pag->pag_agno = index;
4730fa800fbSDave Chinner 		pag->pag_mount = mp;
4741c1c6ebcSDave Chinner 		spin_unlock(&mp->m_perag_lock);
4751c1c6ebcSDave Chinner 		radix_tree_preload_end();
4761c1c6ebcSDave Chinner 	}
4771c1c6ebcSDave Chinner 
4781da177e4SLinus Torvalds 	/* Clear the mount flag if no inode can overflow 32 bits
4791da177e4SLinus Torvalds 	 * on this filesystem, or if specifically requested..
4801da177e4SLinus Torvalds 	 */
481bd186aa9SChristoph Hellwig 	if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > max_inum) {
4821da177e4SLinus Torvalds 		mp->m_flags |= XFS_MOUNT_32BITINODES;
4831da177e4SLinus Torvalds 	} else {
4841da177e4SLinus Torvalds 		mp->m_flags &= ~XFS_MOUNT_32BITINODES;
4851da177e4SLinus Torvalds 	}
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	/* If we can overflow then setup the ag headers accordingly */
4881da177e4SLinus Torvalds 	if (mp->m_flags & XFS_MOUNT_32BITINODES) {
4891da177e4SLinus Torvalds 		/* Calculate how much should be reserved for inodes to
4901da177e4SLinus Torvalds 		 * meet the max inode percentage.
4911da177e4SLinus Torvalds 		 */
4921da177e4SLinus Torvalds 		if (mp->m_maxicount) {
4931da177e4SLinus Torvalds 			__uint64_t	icount;
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 			icount = sbp->sb_dblocks * sbp->sb_imax_pct;
4961da177e4SLinus Torvalds 			do_div(icount, 100);
4971da177e4SLinus Torvalds 			icount += sbp->sb_agblocks - 1;
498a749ee86SEric Sandeen 			do_div(icount, sbp->sb_agblocks);
4991da177e4SLinus Torvalds 			max_metadata = icount;
5001da177e4SLinus Torvalds 		} else {
5011da177e4SLinus Torvalds 			max_metadata = agcount;
5021da177e4SLinus Torvalds 		}
5031da177e4SLinus Torvalds 		for (index = 0; index < agcount; index++) {
5041da177e4SLinus Torvalds 			ino = XFS_AGINO_TO_INO(mp, index, agino);
5051da177e4SLinus Torvalds 			if (ino > max_inum) {
5061da177e4SLinus Torvalds 				index++;
5071da177e4SLinus Torvalds 				break;
5081da177e4SLinus Torvalds 			}
5091da177e4SLinus Torvalds 
510c41564b5SNathan Scott 			/* This ag is preferred for inodes */
51144b56e0aSDave Chinner 			pag = xfs_perag_get(mp, index);
5121da177e4SLinus Torvalds 			pag->pagi_inodeok = 1;
5131da177e4SLinus Torvalds 			if (index < max_metadata)
5141da177e4SLinus Torvalds 				pag->pagf_metadata = 1;
515da353b0dSDavid Chinner 			xfs_initialize_perag_icache(pag);
51644b56e0aSDave Chinner 			xfs_perag_put(pag);
5171da177e4SLinus Torvalds 		}
5181da177e4SLinus Torvalds 	} else {
5191da177e4SLinus Torvalds 		/* Setup default behavior for smaller filesystems */
5201da177e4SLinus Torvalds 		for (index = 0; index < agcount; index++) {
52144b56e0aSDave Chinner 			pag = xfs_perag_get(mp, index);
5221da177e4SLinus Torvalds 			pag->pagi_inodeok = 1;
523da353b0dSDavid Chinner 			xfs_initialize_perag_icache(pag);
52444b56e0aSDave Chinner 			xfs_perag_put(pag);
5251da177e4SLinus Torvalds 		}
5261da177e4SLinus Torvalds 	}
5271c1c6ebcSDave Chinner 	if (maxagi)
5281c1c6ebcSDave Chinner 		*maxagi = index;
5291c1c6ebcSDave Chinner 	return 0;
5308b26c582SDave Chinner 
5318b26c582SDave Chinner out_unwind:
5328b26c582SDave Chinner 	kmem_free(pag);
5338b26c582SDave Chinner 	for (; index > first_initialised; index--) {
5348b26c582SDave Chinner 		pag = radix_tree_delete(&mp->m_perag_tree, index);
5358b26c582SDave Chinner 		kmem_free(pag);
5368b26c582SDave Chinner 	}
5378b26c582SDave Chinner 	return error;
5381da177e4SLinus Torvalds }
5391da177e4SLinus Torvalds 
5402bdf7cd0SChristoph Hellwig void
5412bdf7cd0SChristoph Hellwig xfs_sb_from_disk(
5422bdf7cd0SChristoph Hellwig 	xfs_sb_t	*to,
5432bdf7cd0SChristoph Hellwig 	xfs_dsb_t	*from)
5442bdf7cd0SChristoph Hellwig {
5452bdf7cd0SChristoph Hellwig 	to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
5462bdf7cd0SChristoph Hellwig 	to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
5472bdf7cd0SChristoph Hellwig 	to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
5482bdf7cd0SChristoph Hellwig 	to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
5492bdf7cd0SChristoph Hellwig 	to->sb_rextents = be64_to_cpu(from->sb_rextents);
5502bdf7cd0SChristoph Hellwig 	memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
5512bdf7cd0SChristoph Hellwig 	to->sb_logstart = be64_to_cpu(from->sb_logstart);
5522bdf7cd0SChristoph Hellwig 	to->sb_rootino = be64_to_cpu(from->sb_rootino);
5532bdf7cd0SChristoph Hellwig 	to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
5542bdf7cd0SChristoph Hellwig 	to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
5552bdf7cd0SChristoph Hellwig 	to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
5562bdf7cd0SChristoph Hellwig 	to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
5572bdf7cd0SChristoph Hellwig 	to->sb_agcount = be32_to_cpu(from->sb_agcount);
5582bdf7cd0SChristoph Hellwig 	to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
5592bdf7cd0SChristoph Hellwig 	to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
5602bdf7cd0SChristoph Hellwig 	to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
5612bdf7cd0SChristoph Hellwig 	to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
5622bdf7cd0SChristoph Hellwig 	to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
5632bdf7cd0SChristoph Hellwig 	to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
5642bdf7cd0SChristoph Hellwig 	memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
5652bdf7cd0SChristoph Hellwig 	to->sb_blocklog = from->sb_blocklog;
5662bdf7cd0SChristoph Hellwig 	to->sb_sectlog = from->sb_sectlog;
5672bdf7cd0SChristoph Hellwig 	to->sb_inodelog = from->sb_inodelog;
5682bdf7cd0SChristoph Hellwig 	to->sb_inopblog = from->sb_inopblog;
5692bdf7cd0SChristoph Hellwig 	to->sb_agblklog = from->sb_agblklog;
5702bdf7cd0SChristoph Hellwig 	to->sb_rextslog = from->sb_rextslog;
5712bdf7cd0SChristoph Hellwig 	to->sb_inprogress = from->sb_inprogress;
5722bdf7cd0SChristoph Hellwig 	to->sb_imax_pct = from->sb_imax_pct;
5732bdf7cd0SChristoph Hellwig 	to->sb_icount = be64_to_cpu(from->sb_icount);
5742bdf7cd0SChristoph Hellwig 	to->sb_ifree = be64_to_cpu(from->sb_ifree);
5752bdf7cd0SChristoph Hellwig 	to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
5762bdf7cd0SChristoph Hellwig 	to->sb_frextents = be64_to_cpu(from->sb_frextents);
5772bdf7cd0SChristoph Hellwig 	to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
5782bdf7cd0SChristoph Hellwig 	to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
5792bdf7cd0SChristoph Hellwig 	to->sb_qflags = be16_to_cpu(from->sb_qflags);
5802bdf7cd0SChristoph Hellwig 	to->sb_flags = from->sb_flags;
5812bdf7cd0SChristoph Hellwig 	to->sb_shared_vn = from->sb_shared_vn;
5822bdf7cd0SChristoph Hellwig 	to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
5832bdf7cd0SChristoph Hellwig 	to->sb_unit = be32_to_cpu(from->sb_unit);
5842bdf7cd0SChristoph Hellwig 	to->sb_width = be32_to_cpu(from->sb_width);
5852bdf7cd0SChristoph Hellwig 	to->sb_dirblklog = from->sb_dirblklog;
5862bdf7cd0SChristoph Hellwig 	to->sb_logsectlog = from->sb_logsectlog;
5872bdf7cd0SChristoph Hellwig 	to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
5882bdf7cd0SChristoph Hellwig 	to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
5892bdf7cd0SChristoph Hellwig 	to->sb_features2 = be32_to_cpu(from->sb_features2);
590ee1c0908SDavid Chinner 	to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
5912bdf7cd0SChristoph Hellwig }
5922bdf7cd0SChristoph Hellwig 
5931da177e4SLinus Torvalds /*
5942bdf7cd0SChristoph Hellwig  * Copy in core superblock to ondisk one.
5951da177e4SLinus Torvalds  *
5962bdf7cd0SChristoph Hellwig  * The fields argument is mask of superblock fields to copy.
5971da177e4SLinus Torvalds  */
5981da177e4SLinus Torvalds void
5992bdf7cd0SChristoph Hellwig xfs_sb_to_disk(
6002bdf7cd0SChristoph Hellwig 	xfs_dsb_t	*to,
6012bdf7cd0SChristoph Hellwig 	xfs_sb_t	*from,
6021da177e4SLinus Torvalds 	__int64_t	fields)
6031da177e4SLinus Torvalds {
6042bdf7cd0SChristoph Hellwig 	xfs_caddr_t	to_ptr = (xfs_caddr_t)to;
6052bdf7cd0SChristoph Hellwig 	xfs_caddr_t	from_ptr = (xfs_caddr_t)from;
6061da177e4SLinus Torvalds 	xfs_sb_field_t	f;
6071da177e4SLinus Torvalds 	int		first;
6081da177e4SLinus Torvalds 	int		size;
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	ASSERT(fields);
6111da177e4SLinus Torvalds 	if (!fields)
6121da177e4SLinus Torvalds 		return;
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	while (fields) {
6151da177e4SLinus Torvalds 		f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
6161da177e4SLinus Torvalds 		first = xfs_sb_info[f].offset;
6171da177e4SLinus Torvalds 		size = xfs_sb_info[f + 1].offset - first;
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 		ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 		if (size == 1 || xfs_sb_info[f].type == 1) {
6222bdf7cd0SChristoph Hellwig 			memcpy(to_ptr + first, from_ptr + first, size);
6231da177e4SLinus Torvalds 		} else {
6241da177e4SLinus Torvalds 			switch (size) {
6251da177e4SLinus Torvalds 			case 2:
6262bdf7cd0SChristoph Hellwig 				*(__be16 *)(to_ptr + first) =
6272bdf7cd0SChristoph Hellwig 					cpu_to_be16(*(__u16 *)(from_ptr + first));
6281da177e4SLinus Torvalds 				break;
6291da177e4SLinus Torvalds 			case 4:
6302bdf7cd0SChristoph Hellwig 				*(__be32 *)(to_ptr + first) =
6312bdf7cd0SChristoph Hellwig 					cpu_to_be32(*(__u32 *)(from_ptr + first));
6321da177e4SLinus Torvalds 				break;
6331da177e4SLinus Torvalds 			case 8:
6342bdf7cd0SChristoph Hellwig 				*(__be64 *)(to_ptr + first) =
6352bdf7cd0SChristoph Hellwig 					cpu_to_be64(*(__u64 *)(from_ptr + first));
6361da177e4SLinus Torvalds 				break;
6371da177e4SLinus Torvalds 			default:
6381da177e4SLinus Torvalds 				ASSERT(0);
6391da177e4SLinus Torvalds 			}
6401da177e4SLinus Torvalds 		}
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 		fields &= ~(1LL << f);
6431da177e4SLinus Torvalds 	}
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds /*
6471da177e4SLinus Torvalds  * xfs_readsb
6481da177e4SLinus Torvalds  *
6491da177e4SLinus Torvalds  * Does the initial read of the superblock.
6501da177e4SLinus Torvalds  */
6511da177e4SLinus Torvalds int
652764d1f89SNathan Scott xfs_readsb(xfs_mount_t *mp, int flags)
6531da177e4SLinus Torvalds {
6541da177e4SLinus Torvalds 	unsigned int	sector_size;
6551da177e4SLinus Torvalds 	unsigned int	extra_flags;
6561da177e4SLinus Torvalds 	xfs_buf_t	*bp;
6571da177e4SLinus Torvalds 	int		error;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	ASSERT(mp->m_sb_bp == NULL);
6601da177e4SLinus Torvalds 	ASSERT(mp->m_ddev_targp != NULL);
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	/*
6631da177e4SLinus Torvalds 	 * Allocate a (locked) buffer to hold the superblock.
6641da177e4SLinus Torvalds 	 * This will be kept around at all times to optimize
6651da177e4SLinus Torvalds 	 * access to the superblock.
6661da177e4SLinus Torvalds 	 */
6671da177e4SLinus Torvalds 	sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
6680cadda1cSChristoph Hellwig 	extra_flags = XBF_LOCK | XBF_FS_MANAGED | XBF_MAPPED;
6691da177e4SLinus Torvalds 
6706ad112bfSChristoph Hellwig 	bp = xfs_buf_read(mp->m_ddev_targp, XFS_SB_DADDR, BTOBB(sector_size),
6716ad112bfSChristoph Hellwig 			  extra_flags);
6721da177e4SLinus Torvalds 	if (!bp || XFS_BUF_ISERROR(bp)) {
673764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags, "SB read failed");
6741da177e4SLinus Torvalds 		error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
6751da177e4SLinus Torvalds 		goto fail;
6761da177e4SLinus Torvalds 	}
6771da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISBUSY(bp));
6781da177e4SLinus Torvalds 	ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	/*
6811da177e4SLinus Torvalds 	 * Initialize the mount structure from the superblock.
6821da177e4SLinus Torvalds 	 * But first do some basic consistency checking.
6831da177e4SLinus Torvalds 	 */
6842bdf7cd0SChristoph Hellwig 	xfs_sb_from_disk(&mp->m_sb, XFS_BUF_TO_SBP(bp));
6851da177e4SLinus Torvalds 
686764d1f89SNathan Scott 	error = xfs_mount_validate_sb(mp, &(mp->m_sb), flags);
6871da177e4SLinus Torvalds 	if (error) {
688764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags, "SB validate failed");
6891da177e4SLinus Torvalds 		goto fail;
6901da177e4SLinus Torvalds 	}
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	/*
6931da177e4SLinus Torvalds 	 * We must be able to do sector-sized and sector-aligned IO.
6941da177e4SLinus Torvalds 	 */
6951da177e4SLinus Torvalds 	if (sector_size > mp->m_sb.sb_sectsize) {
696764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags,
697764d1f89SNathan Scott 			"device supports only %u byte sectors (not %u)",
6981da177e4SLinus Torvalds 			sector_size, mp->m_sb.sb_sectsize);
6991da177e4SLinus Torvalds 		error = ENOSYS;
7001da177e4SLinus Torvalds 		goto fail;
7011da177e4SLinus Torvalds 	}
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	/*
7041da177e4SLinus Torvalds 	 * If device sector size is smaller than the superblock size,
7051da177e4SLinus Torvalds 	 * re-read the superblock so the buffer is correctly sized.
7061da177e4SLinus Torvalds 	 */
7071da177e4SLinus Torvalds 	if (sector_size < mp->m_sb.sb_sectsize) {
7081da177e4SLinus Torvalds 		XFS_BUF_UNMANAGE(bp);
7091da177e4SLinus Torvalds 		xfs_buf_relse(bp);
7101da177e4SLinus Torvalds 		sector_size = mp->m_sb.sb_sectsize;
7116ad112bfSChristoph Hellwig 		bp = xfs_buf_read(mp->m_ddev_targp, XFS_SB_DADDR,
7121da177e4SLinus Torvalds 				  BTOBB(sector_size), extra_flags);
7131da177e4SLinus Torvalds 		if (!bp || XFS_BUF_ISERROR(bp)) {
714764d1f89SNathan Scott 			xfs_fs_mount_cmn_err(flags, "SB re-read failed");
7151da177e4SLinus Torvalds 			error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
7161da177e4SLinus Torvalds 			goto fail;
7171da177e4SLinus Torvalds 		}
7181da177e4SLinus Torvalds 		ASSERT(XFS_BUF_ISBUSY(bp));
7191da177e4SLinus Torvalds 		ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
7201da177e4SLinus Torvalds 	}
7211da177e4SLinus Torvalds 
7225478eeadSLachlan McIlroy 	/* Initialize per-cpu counters */
7235478eeadSLachlan McIlroy 	xfs_icsb_reinit_counters(mp);
7248d280b98SDavid Chinner 
7251da177e4SLinus Torvalds 	mp->m_sb_bp = bp;
7261da177e4SLinus Torvalds 	xfs_buf_relse(bp);
7271da177e4SLinus Torvalds 	ASSERT(XFS_BUF_VALUSEMA(bp) > 0);
7281da177e4SLinus Torvalds 	return 0;
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds  fail:
7311da177e4SLinus Torvalds 	if (bp) {
7321da177e4SLinus Torvalds 		XFS_BUF_UNMANAGE(bp);
7331da177e4SLinus Torvalds 		xfs_buf_relse(bp);
7341da177e4SLinus Torvalds 	}
7351da177e4SLinus Torvalds 	return error;
7361da177e4SLinus Torvalds }
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds /*
7401da177e4SLinus Torvalds  * xfs_mount_common
7411da177e4SLinus Torvalds  *
7421da177e4SLinus Torvalds  * Mount initialization code establishing various mount
7431da177e4SLinus Torvalds  * fields from the superblock associated with the given
7441da177e4SLinus Torvalds  * mount structure
7451da177e4SLinus Torvalds  */
746ba0f32d4SChristoph Hellwig STATIC void
7471da177e4SLinus Torvalds xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
7481da177e4SLinus Torvalds {
7491da177e4SLinus Torvalds 	mp->m_agfrotor = mp->m_agirotor = 0;
750007c61c6SEric Sandeen 	spin_lock_init(&mp->m_agirotor_lock);
7511da177e4SLinus Torvalds 	mp->m_maxagi = mp->m_sb.sb_agcount;
7521da177e4SLinus Torvalds 	mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
7531da177e4SLinus Torvalds 	mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
7541da177e4SLinus Torvalds 	mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
7551da177e4SLinus Torvalds 	mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
7561da177e4SLinus Torvalds 	mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
7571da177e4SLinus Torvalds 	mp->m_blockmask = sbp->sb_blocksize - 1;
7581da177e4SLinus Torvalds 	mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
7591da177e4SLinus Torvalds 	mp->m_blockwmask = mp->m_blockwsize - 1;
7601da177e4SLinus Torvalds 
76160197e8dSChristoph Hellwig 	mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
76260197e8dSChristoph Hellwig 	mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
76360197e8dSChristoph Hellwig 	mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
76460197e8dSChristoph Hellwig 	mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
76560197e8dSChristoph Hellwig 
76660197e8dSChristoph Hellwig 	mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
76760197e8dSChristoph Hellwig 	mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
76860197e8dSChristoph Hellwig 	mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
76960197e8dSChristoph Hellwig 	mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
77060197e8dSChristoph Hellwig 
77160197e8dSChristoph Hellwig 	mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
77260197e8dSChristoph Hellwig 	mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
77360197e8dSChristoph Hellwig 	mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
77460197e8dSChristoph Hellwig 	mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
7771da177e4SLinus Torvalds 	mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
7781da177e4SLinus Torvalds 					sbp->sb_inopblock);
7791da177e4SLinus Torvalds 	mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
7801da177e4SLinus Torvalds }
78192821e2bSDavid Chinner 
78292821e2bSDavid Chinner /*
78392821e2bSDavid Chinner  * xfs_initialize_perag_data
78492821e2bSDavid Chinner  *
78592821e2bSDavid Chinner  * Read in each per-ag structure so we can count up the number of
78692821e2bSDavid Chinner  * allocated inodes, free inodes and used filesystem blocks as this
78792821e2bSDavid Chinner  * information is no longer persistent in the superblock. Once we have
78892821e2bSDavid Chinner  * this information, write it into the in-core superblock structure.
78992821e2bSDavid Chinner  */
79092821e2bSDavid Chinner STATIC int
79192821e2bSDavid Chinner xfs_initialize_perag_data(xfs_mount_t *mp, xfs_agnumber_t agcount)
79292821e2bSDavid Chinner {
79392821e2bSDavid Chinner 	xfs_agnumber_t	index;
79492821e2bSDavid Chinner 	xfs_perag_t	*pag;
79592821e2bSDavid Chinner 	xfs_sb_t	*sbp = &mp->m_sb;
79692821e2bSDavid Chinner 	uint64_t	ifree = 0;
79792821e2bSDavid Chinner 	uint64_t	ialloc = 0;
79892821e2bSDavid Chinner 	uint64_t	bfree = 0;
79992821e2bSDavid Chinner 	uint64_t	bfreelst = 0;
80092821e2bSDavid Chinner 	uint64_t	btree = 0;
80192821e2bSDavid Chinner 	int		error;
80292821e2bSDavid Chinner 
80392821e2bSDavid Chinner 	for (index = 0; index < agcount; index++) {
80492821e2bSDavid Chinner 		/*
80592821e2bSDavid Chinner 		 * read the agf, then the agi. This gets us
8069da096fdSMalcolm Parsons 		 * all the information we need and populates the
80792821e2bSDavid Chinner 		 * per-ag structures for us.
80892821e2bSDavid Chinner 		 */
80992821e2bSDavid Chinner 		error = xfs_alloc_pagf_init(mp, NULL, index, 0);
81092821e2bSDavid Chinner 		if (error)
81192821e2bSDavid Chinner 			return error;
81292821e2bSDavid Chinner 
81392821e2bSDavid Chinner 		error = xfs_ialloc_pagi_init(mp, NULL, index);
81492821e2bSDavid Chinner 		if (error)
81592821e2bSDavid Chinner 			return error;
81644b56e0aSDave Chinner 		pag = xfs_perag_get(mp, index);
81792821e2bSDavid Chinner 		ifree += pag->pagi_freecount;
81892821e2bSDavid Chinner 		ialloc += pag->pagi_count;
81992821e2bSDavid Chinner 		bfree += pag->pagf_freeblks;
82092821e2bSDavid Chinner 		bfreelst += pag->pagf_flcount;
82192821e2bSDavid Chinner 		btree += pag->pagf_btreeblks;
82244b56e0aSDave Chinner 		xfs_perag_put(pag);
82392821e2bSDavid Chinner 	}
82492821e2bSDavid Chinner 	/*
82592821e2bSDavid Chinner 	 * Overwrite incore superblock counters with just-read data
82692821e2bSDavid Chinner 	 */
8273685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
82892821e2bSDavid Chinner 	sbp->sb_ifree = ifree;
82992821e2bSDavid Chinner 	sbp->sb_icount = ialloc;
83092821e2bSDavid Chinner 	sbp->sb_fdblocks = bfree + bfreelst + btree;
8313685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
83292821e2bSDavid Chinner 
83392821e2bSDavid Chinner 	/* Fixup the per-cpu counters as well. */
83492821e2bSDavid Chinner 	xfs_icsb_reinit_counters(mp);
83592821e2bSDavid Chinner 
83692821e2bSDavid Chinner 	return 0;
83792821e2bSDavid Chinner }
83892821e2bSDavid Chinner 
8391da177e4SLinus Torvalds /*
8400771fb45SEric Sandeen  * Update alignment values based on mount options and sb values
8411da177e4SLinus Torvalds  */
8420771fb45SEric Sandeen STATIC int
8437884bc86SChristoph Hellwig xfs_update_alignment(xfs_mount_t *mp)
8441da177e4SLinus Torvalds {
8451da177e4SLinus Torvalds 	xfs_sb_t	*sbp = &(mp->m_sb);
8461da177e4SLinus Torvalds 
8474249023aSChristoph Hellwig 	if (mp->m_dalign) {
8481da177e4SLinus Torvalds 		/*
8491da177e4SLinus Torvalds 		 * If stripe unit and stripe width are not multiples
8501da177e4SLinus Torvalds 		 * of the fs blocksize turn off alignment.
8511da177e4SLinus Torvalds 		 */
8521da177e4SLinus Torvalds 		if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
8531da177e4SLinus Torvalds 		    (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
8541da177e4SLinus Torvalds 			if (mp->m_flags & XFS_MOUNT_RETERR) {
8551da177e4SLinus Torvalds 				cmn_err(CE_WARN,
8561da177e4SLinus Torvalds 					"XFS: alignment check 1 failed");
8570771fb45SEric Sandeen 				return XFS_ERROR(EINVAL);
8581da177e4SLinus Torvalds 			}
8591da177e4SLinus Torvalds 			mp->m_dalign = mp->m_swidth = 0;
8601da177e4SLinus Torvalds 		} else {
8611da177e4SLinus Torvalds 			/*
8621da177e4SLinus Torvalds 			 * Convert the stripe unit and width to FSBs.
8631da177e4SLinus Torvalds 			 */
8641da177e4SLinus Torvalds 			mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
8651da177e4SLinus Torvalds 			if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
8661da177e4SLinus Torvalds 				if (mp->m_flags & XFS_MOUNT_RETERR) {
8670771fb45SEric Sandeen 					return XFS_ERROR(EINVAL);
8681da177e4SLinus Torvalds 				}
8691da177e4SLinus Torvalds 				xfs_fs_cmn_err(CE_WARN, mp,
8701da177e4SLinus Torvalds "stripe alignment turned off: sunit(%d)/swidth(%d) incompatible with agsize(%d)",
8711da177e4SLinus Torvalds 					mp->m_dalign, mp->m_swidth,
8721da177e4SLinus Torvalds 					sbp->sb_agblocks);
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 				mp->m_dalign = 0;
8751da177e4SLinus Torvalds 				mp->m_swidth = 0;
8761da177e4SLinus Torvalds 			} else if (mp->m_dalign) {
8771da177e4SLinus Torvalds 				mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
8781da177e4SLinus Torvalds 			} else {
8791da177e4SLinus Torvalds 				if (mp->m_flags & XFS_MOUNT_RETERR) {
8801da177e4SLinus Torvalds 					xfs_fs_cmn_err(CE_WARN, mp,
8811da177e4SLinus Torvalds "stripe alignment turned off: sunit(%d) less than bsize(%d)",
8821da177e4SLinus Torvalds                                         	mp->m_dalign,
8831da177e4SLinus Torvalds 						mp->m_blockmask +1);
8840771fb45SEric Sandeen 					return XFS_ERROR(EINVAL);
8851da177e4SLinus Torvalds 				}
8861da177e4SLinus Torvalds 				mp->m_swidth = 0;
8871da177e4SLinus Torvalds 			}
8881da177e4SLinus Torvalds 		}
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 		/*
8911da177e4SLinus Torvalds 		 * Update superblock with new values
8921da177e4SLinus Torvalds 		 * and log changes
8931da177e4SLinus Torvalds 		 */
89462118709SEric Sandeen 		if (xfs_sb_version_hasdalign(sbp)) {
8951da177e4SLinus Torvalds 			if (sbp->sb_unit != mp->m_dalign) {
8961da177e4SLinus Torvalds 				sbp->sb_unit = mp->m_dalign;
8977884bc86SChristoph Hellwig 				mp->m_update_flags |= XFS_SB_UNIT;
8981da177e4SLinus Torvalds 			}
8991da177e4SLinus Torvalds 			if (sbp->sb_width != mp->m_swidth) {
9001da177e4SLinus Torvalds 				sbp->sb_width = mp->m_swidth;
9017884bc86SChristoph Hellwig 				mp->m_update_flags |= XFS_SB_WIDTH;
9021da177e4SLinus Torvalds 			}
9031da177e4SLinus Torvalds 		}
9041da177e4SLinus Torvalds 	} else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
90562118709SEric Sandeen 		    xfs_sb_version_hasdalign(&mp->m_sb)) {
9061da177e4SLinus Torvalds 			mp->m_dalign = sbp->sb_unit;
9071da177e4SLinus Torvalds 			mp->m_swidth = sbp->sb_width;
9081da177e4SLinus Torvalds 	}
9091da177e4SLinus Torvalds 
9100771fb45SEric Sandeen 	return 0;
9110771fb45SEric Sandeen }
9121da177e4SLinus Torvalds 
9130771fb45SEric Sandeen /*
9140771fb45SEric Sandeen  * Set the maximum inode count for this filesystem
9150771fb45SEric Sandeen  */
9160771fb45SEric Sandeen STATIC void
9170771fb45SEric Sandeen xfs_set_maxicount(xfs_mount_t *mp)
9180771fb45SEric Sandeen {
9190771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
9201da177e4SLinus Torvalds 	__uint64_t	icount;
9211da177e4SLinus Torvalds 
9220771fb45SEric Sandeen 	if (sbp->sb_imax_pct) {
9230771fb45SEric Sandeen 		/*
9240771fb45SEric Sandeen 		 * Make sure the maximum inode count is a multiple
9250771fb45SEric Sandeen 		 * of the units we allocate inodes in.
9261da177e4SLinus Torvalds 		 */
9271da177e4SLinus Torvalds 		icount = sbp->sb_dblocks * sbp->sb_imax_pct;
9281da177e4SLinus Torvalds 		do_div(icount, 100);
9291da177e4SLinus Torvalds 		do_div(icount, mp->m_ialloc_blks);
9301da177e4SLinus Torvalds 		mp->m_maxicount = (icount * mp->m_ialloc_blks)  <<
9311da177e4SLinus Torvalds 				   sbp->sb_inopblog;
9320771fb45SEric Sandeen 	} else {
9331da177e4SLinus Torvalds 		mp->m_maxicount = 0;
9341da177e4SLinus Torvalds 	}
9351da177e4SLinus Torvalds }
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds /*
9381da177e4SLinus Torvalds  * Set the default minimum read and write sizes unless
9391da177e4SLinus Torvalds  * already specified in a mount option.
9401da177e4SLinus Torvalds  * We use smaller I/O sizes when the file system
9411da177e4SLinus Torvalds  * is being used for NFS service (wsync mount option).
9421da177e4SLinus Torvalds  */
9430771fb45SEric Sandeen STATIC void
9440771fb45SEric Sandeen xfs_set_rw_sizes(xfs_mount_t *mp)
9450771fb45SEric Sandeen {
9460771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
9470771fb45SEric Sandeen 	int		readio_log, writeio_log;
9480771fb45SEric Sandeen 
9491da177e4SLinus Torvalds 	if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
9501da177e4SLinus Torvalds 		if (mp->m_flags & XFS_MOUNT_WSYNC) {
9511da177e4SLinus Torvalds 			readio_log = XFS_WSYNC_READIO_LOG;
9521da177e4SLinus Torvalds 			writeio_log = XFS_WSYNC_WRITEIO_LOG;
9531da177e4SLinus Torvalds 		} else {
9541da177e4SLinus Torvalds 			readio_log = XFS_READIO_LOG_LARGE;
9551da177e4SLinus Torvalds 			writeio_log = XFS_WRITEIO_LOG_LARGE;
9561da177e4SLinus Torvalds 		}
9571da177e4SLinus Torvalds 	} else {
9581da177e4SLinus Torvalds 		readio_log = mp->m_readio_log;
9591da177e4SLinus Torvalds 		writeio_log = mp->m_writeio_log;
9601da177e4SLinus Torvalds 	}
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	if (sbp->sb_blocklog > readio_log) {
9631da177e4SLinus Torvalds 		mp->m_readio_log = sbp->sb_blocklog;
9641da177e4SLinus Torvalds 	} else {
9651da177e4SLinus Torvalds 		mp->m_readio_log = readio_log;
9661da177e4SLinus Torvalds 	}
9671da177e4SLinus Torvalds 	mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
9681da177e4SLinus Torvalds 	if (sbp->sb_blocklog > writeio_log) {
9691da177e4SLinus Torvalds 		mp->m_writeio_log = sbp->sb_blocklog;
9701da177e4SLinus Torvalds 	} else {
9711da177e4SLinus Torvalds 		mp->m_writeio_log = writeio_log;
9721da177e4SLinus Torvalds 	}
9731da177e4SLinus Torvalds 	mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
9740771fb45SEric Sandeen }
975425f9dddSEric Sandeen 
9761da177e4SLinus Torvalds /*
9771da177e4SLinus Torvalds  * Set whether we're using inode alignment.
9781da177e4SLinus Torvalds  */
9790771fb45SEric Sandeen STATIC void
9800771fb45SEric Sandeen xfs_set_inoalignment(xfs_mount_t *mp)
9810771fb45SEric Sandeen {
98262118709SEric Sandeen 	if (xfs_sb_version_hasalign(&mp->m_sb) &&
9831da177e4SLinus Torvalds 	    mp->m_sb.sb_inoalignmt >=
9841da177e4SLinus Torvalds 	    XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
9851da177e4SLinus Torvalds 		mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
9861da177e4SLinus Torvalds 	else
9871da177e4SLinus Torvalds 		mp->m_inoalign_mask = 0;
9881da177e4SLinus Torvalds 	/*
9891da177e4SLinus Torvalds 	 * If we are using stripe alignment, check whether
9901da177e4SLinus Torvalds 	 * the stripe unit is a multiple of the inode alignment
9911da177e4SLinus Torvalds 	 */
9921da177e4SLinus Torvalds 	if (mp->m_dalign && mp->m_inoalign_mask &&
9931da177e4SLinus Torvalds 	    !(mp->m_dalign & mp->m_inoalign_mask))
9941da177e4SLinus Torvalds 		mp->m_sinoalign = mp->m_dalign;
9951da177e4SLinus Torvalds 	else
9961da177e4SLinus Torvalds 		mp->m_sinoalign = 0;
9970771fb45SEric Sandeen }
9980771fb45SEric Sandeen 
9991da177e4SLinus Torvalds /*
10001da177e4SLinus Torvalds  * Check that the data (and log if separate) are an ok size.
10011da177e4SLinus Torvalds  */
10020771fb45SEric Sandeen STATIC int
10034249023aSChristoph Hellwig xfs_check_sizes(xfs_mount_t *mp)
10040771fb45SEric Sandeen {
10050771fb45SEric Sandeen 	xfs_buf_t	*bp;
10060771fb45SEric Sandeen 	xfs_daddr_t	d;
10070771fb45SEric Sandeen 	int		error;
10080771fb45SEric Sandeen 
10091da177e4SLinus Torvalds 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
10101da177e4SLinus Torvalds 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
10111da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: size check 1 failed");
10120771fb45SEric Sandeen 		return XFS_ERROR(E2BIG);
10131da177e4SLinus Torvalds 	}
10141da177e4SLinus Torvalds 	error = xfs_read_buf(mp, mp->m_ddev_targp,
10151da177e4SLinus Torvalds 			     d - XFS_FSS_TO_BB(mp, 1),
10161da177e4SLinus Torvalds 			     XFS_FSS_TO_BB(mp, 1), 0, &bp);
10171da177e4SLinus Torvalds 	if (!error) {
10181da177e4SLinus Torvalds 		xfs_buf_relse(bp);
10191da177e4SLinus Torvalds 	} else {
10201da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: size check 2 failed");
10210771fb45SEric Sandeen 		if (error == ENOSPC)
10221da177e4SLinus Torvalds 			error = XFS_ERROR(E2BIG);
10230771fb45SEric Sandeen 		return error;
10241da177e4SLinus Torvalds 	}
10251da177e4SLinus Torvalds 
10264249023aSChristoph Hellwig 	if (mp->m_logdev_targp != mp->m_ddev_targp) {
10271da177e4SLinus Torvalds 		d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
10281da177e4SLinus Torvalds 		if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
10291da177e4SLinus Torvalds 			cmn_err(CE_WARN, "XFS: size check 3 failed");
10300771fb45SEric Sandeen 			return XFS_ERROR(E2BIG);
10311da177e4SLinus Torvalds 		}
10321da177e4SLinus Torvalds 		error = xfs_read_buf(mp, mp->m_logdev_targp,
10331da177e4SLinus Torvalds 				     d - XFS_FSB_TO_BB(mp, 1),
10341da177e4SLinus Torvalds 				     XFS_FSB_TO_BB(mp, 1), 0, &bp);
10351da177e4SLinus Torvalds 		if (!error) {
10361da177e4SLinus Torvalds 			xfs_buf_relse(bp);
10371da177e4SLinus Torvalds 		} else {
10381da177e4SLinus Torvalds 			cmn_err(CE_WARN, "XFS: size check 3 failed");
10390771fb45SEric Sandeen 			if (error == ENOSPC)
10401da177e4SLinus Torvalds 				error = XFS_ERROR(E2BIG);
10410771fb45SEric Sandeen 			return error;
10421da177e4SLinus Torvalds 		}
10430771fb45SEric Sandeen 	}
10440771fb45SEric Sandeen 	return 0;
10450771fb45SEric Sandeen }
10460771fb45SEric Sandeen 
10470771fb45SEric Sandeen /*
10487d095257SChristoph Hellwig  * Clear the quotaflags in memory and in the superblock.
10497d095257SChristoph Hellwig  */
10507d095257SChristoph Hellwig int
10517d095257SChristoph Hellwig xfs_mount_reset_sbqflags(
10527d095257SChristoph Hellwig 	struct xfs_mount	*mp)
10537d095257SChristoph Hellwig {
10547d095257SChristoph Hellwig 	int			error;
10557d095257SChristoph Hellwig 	struct xfs_trans	*tp;
10567d095257SChristoph Hellwig 
10577d095257SChristoph Hellwig 	mp->m_qflags = 0;
10587d095257SChristoph Hellwig 
10597d095257SChristoph Hellwig 	/*
10607d095257SChristoph Hellwig 	 * It is OK to look at sb_qflags here in mount path,
10617d095257SChristoph Hellwig 	 * without m_sb_lock.
10627d095257SChristoph Hellwig 	 */
10637d095257SChristoph Hellwig 	if (mp->m_sb.sb_qflags == 0)
10647d095257SChristoph Hellwig 		return 0;
10657d095257SChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
10667d095257SChristoph Hellwig 	mp->m_sb.sb_qflags = 0;
10677d095257SChristoph Hellwig 	spin_unlock(&mp->m_sb_lock);
10687d095257SChristoph Hellwig 
10697d095257SChristoph Hellwig 	/*
10707d095257SChristoph Hellwig 	 * If the fs is readonly, let the incore superblock run
10717d095257SChristoph Hellwig 	 * with quotas off but don't flush the update out to disk
10727d095257SChristoph Hellwig 	 */
10737d095257SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_RDONLY)
10747d095257SChristoph Hellwig 		return 0;
10757d095257SChristoph Hellwig 
10767d095257SChristoph Hellwig #ifdef QUOTADEBUG
10777d095257SChristoph Hellwig 	xfs_fs_cmn_err(CE_NOTE, mp, "Writing superblock quota changes");
10787d095257SChristoph Hellwig #endif
10797d095257SChristoph Hellwig 
10807d095257SChristoph Hellwig 	tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
10817d095257SChristoph Hellwig 	error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
10827d095257SChristoph Hellwig 				      XFS_DEFAULT_LOG_COUNT);
10837d095257SChristoph Hellwig 	if (error) {
10847d095257SChristoph Hellwig 		xfs_trans_cancel(tp, 0);
10857d095257SChristoph Hellwig 		xfs_fs_cmn_err(CE_ALERT, mp,
10867d095257SChristoph Hellwig 			"xfs_mount_reset_sbqflags: Superblock update failed!");
10877d095257SChristoph Hellwig 		return error;
10887d095257SChristoph Hellwig 	}
10897d095257SChristoph Hellwig 
10907d095257SChristoph Hellwig 	xfs_mod_sb(tp, XFS_SB_QFLAGS);
10917d095257SChristoph Hellwig 	return xfs_trans_commit(tp, 0);
10927d095257SChristoph Hellwig }
10937d095257SChristoph Hellwig 
1094d5db0f97SEric Sandeen __uint64_t
1095d5db0f97SEric Sandeen xfs_default_resblks(xfs_mount_t *mp)
1096d5db0f97SEric Sandeen {
1097d5db0f97SEric Sandeen 	__uint64_t resblks;
1098d5db0f97SEric Sandeen 
1099d5db0f97SEric Sandeen 	/*
1100d5db0f97SEric Sandeen 	 * We default to 5% or 1024 fsbs of space reserved, whichever is smaller.
1101d5db0f97SEric Sandeen 	 * This may drive us straight to ENOSPC on mount, but that implies
1102d5db0f97SEric Sandeen 	 * we were already there on the last unmount. Warn if this occurs.
1103d5db0f97SEric Sandeen 	 */
1104d5db0f97SEric Sandeen 	resblks = mp->m_sb.sb_dblocks;
1105d5db0f97SEric Sandeen 	do_div(resblks, 20);
1106d5db0f97SEric Sandeen 	resblks = min_t(__uint64_t, resblks, 1024);
1107d5db0f97SEric Sandeen 	return resblks;
1108d5db0f97SEric Sandeen }
1109d5db0f97SEric Sandeen 
11107d095257SChristoph Hellwig /*
11110771fb45SEric Sandeen  * This function does the following on an initial mount of a file system:
11120771fb45SEric Sandeen  *	- reads the superblock from disk and init the mount struct
11130771fb45SEric Sandeen  *	- if we're a 32-bit kernel, do a size check on the superblock
11140771fb45SEric Sandeen  *		so we don't mount terabyte filesystems
11150771fb45SEric Sandeen  *	- init mount struct realtime fields
11160771fb45SEric Sandeen  *	- allocate inode hash table for fs
11170771fb45SEric Sandeen  *	- init directory manager
11180771fb45SEric Sandeen  *	- perform recovery and init the log manager
11190771fb45SEric Sandeen  */
11200771fb45SEric Sandeen int
11210771fb45SEric Sandeen xfs_mountfs(
11224249023aSChristoph Hellwig 	xfs_mount_t	*mp)
11230771fb45SEric Sandeen {
11240771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
11250771fb45SEric Sandeen 	xfs_inode_t	*rip;
11260771fb45SEric Sandeen 	__uint64_t	resblks;
11277d095257SChristoph Hellwig 	uint		quotamount = 0;
11287d095257SChristoph Hellwig 	uint		quotaflags = 0;
11290771fb45SEric Sandeen 	int		error = 0;
11300771fb45SEric Sandeen 
11310771fb45SEric Sandeen 	xfs_mount_common(mp, sbp);
11320771fb45SEric Sandeen 
11330771fb45SEric Sandeen 	/*
1134e6957ea4SEric Sandeen 	 * Check for a mismatched features2 values.  Older kernels
1135e6957ea4SEric Sandeen 	 * read & wrote into the wrong sb offset for sb_features2
1136e6957ea4SEric Sandeen 	 * on some platforms due to xfs_sb_t not being 64bit size aligned
1137e6957ea4SEric Sandeen 	 * when sb_features2 was added, which made older superblock
1138e6957ea4SEric Sandeen 	 * reading/writing routines swap it as a 64-bit value.
1139ee1c0908SDavid Chinner 	 *
1140e6957ea4SEric Sandeen 	 * For backwards compatibility, we make both slots equal.
1141e6957ea4SEric Sandeen 	 *
1142e6957ea4SEric Sandeen 	 * If we detect a mismatched field, we OR the set bits into the
1143e6957ea4SEric Sandeen 	 * existing features2 field in case it has already been modified; we
1144e6957ea4SEric Sandeen 	 * don't want to lose any features.  We then update the bad location
1145e6957ea4SEric Sandeen 	 * with the ORed value so that older kernels will see any features2
1146e6957ea4SEric Sandeen 	 * flags, and mark the two fields as needing updates once the
1147e6957ea4SEric Sandeen 	 * transaction subsystem is online.
1148ee1c0908SDavid Chinner 	 */
1149e6957ea4SEric Sandeen 	if (xfs_sb_has_mismatched_features2(sbp)) {
1150ee1c0908SDavid Chinner 		cmn_err(CE_WARN,
1151ee1c0908SDavid Chinner 			"XFS: correcting sb_features alignment problem");
1152ee1c0908SDavid Chinner 		sbp->sb_features2 |= sbp->sb_bad_features2;
1153e6957ea4SEric Sandeen 		sbp->sb_bad_features2 = sbp->sb_features2;
11547884bc86SChristoph Hellwig 		mp->m_update_flags |= XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2;
1155e6957ea4SEric Sandeen 
1156e6957ea4SEric Sandeen 		/*
1157e6957ea4SEric Sandeen 		 * Re-check for ATTR2 in case it was found in bad_features2
1158e6957ea4SEric Sandeen 		 * slot.
1159e6957ea4SEric Sandeen 		 */
11607c12f296STim Shimmin 		if (xfs_sb_version_hasattr2(&mp->m_sb) &&
11617c12f296STim Shimmin 		   !(mp->m_flags & XFS_MOUNT_NOATTR2))
1162e6957ea4SEric Sandeen 			mp->m_flags |= XFS_MOUNT_ATTR2;
11637c12f296STim Shimmin 	}
1164e6957ea4SEric Sandeen 
11657c12f296STim Shimmin 	if (xfs_sb_version_hasattr2(&mp->m_sb) &&
11667c12f296STim Shimmin 	   (mp->m_flags & XFS_MOUNT_NOATTR2)) {
11677c12f296STim Shimmin 		xfs_sb_version_removeattr2(&mp->m_sb);
11687884bc86SChristoph Hellwig 		mp->m_update_flags |= XFS_SB_FEATURES2;
11697c12f296STim Shimmin 
11707c12f296STim Shimmin 		/* update sb_versionnum for the clearing of the morebits */
11717c12f296STim Shimmin 		if (!sbp->sb_features2)
11727884bc86SChristoph Hellwig 			mp->m_update_flags |= XFS_SB_VERSIONNUM;
1173ee1c0908SDavid Chinner 	}
1174ee1c0908SDavid Chinner 
1175ee1c0908SDavid Chinner 	/*
11760771fb45SEric Sandeen 	 * Check if sb_agblocks is aligned at stripe boundary
11770771fb45SEric Sandeen 	 * If sb_agblocks is NOT aligned turn off m_dalign since
11780771fb45SEric Sandeen 	 * allocator alignment is within an ag, therefore ag has
11790771fb45SEric Sandeen 	 * to be aligned at stripe boundary.
11800771fb45SEric Sandeen 	 */
11817884bc86SChristoph Hellwig 	error = xfs_update_alignment(mp);
11820771fb45SEric Sandeen 	if (error)
1183f9057e3dSChristoph Hellwig 		goto out;
11840771fb45SEric Sandeen 
11850771fb45SEric Sandeen 	xfs_alloc_compute_maxlevels(mp);
11860771fb45SEric Sandeen 	xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
11870771fb45SEric Sandeen 	xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
11880771fb45SEric Sandeen 	xfs_ialloc_compute_maxlevels(mp);
11890771fb45SEric Sandeen 
11900771fb45SEric Sandeen 	xfs_set_maxicount(mp);
11910771fb45SEric Sandeen 
11920771fb45SEric Sandeen 	mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog);
11930771fb45SEric Sandeen 
119427174203SChristoph Hellwig 	error = xfs_uuid_mount(mp);
119527174203SChristoph Hellwig 	if (error)
1196f9057e3dSChristoph Hellwig 		goto out;
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 	/*
11990771fb45SEric Sandeen 	 * Set the minimum read and write sizes
12000771fb45SEric Sandeen 	 */
12010771fb45SEric Sandeen 	xfs_set_rw_sizes(mp);
12020771fb45SEric Sandeen 
12030771fb45SEric Sandeen 	/*
12040771fb45SEric Sandeen 	 * Set the inode cluster size.
12050771fb45SEric Sandeen 	 * This may still be overridden by the file system
12060771fb45SEric Sandeen 	 * block size if it is larger than the chosen cluster size.
12070771fb45SEric Sandeen 	 */
12080771fb45SEric Sandeen 	mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
12090771fb45SEric Sandeen 
12100771fb45SEric Sandeen 	/*
12110771fb45SEric Sandeen 	 * Set inode alignment fields
12120771fb45SEric Sandeen 	 */
12130771fb45SEric Sandeen 	xfs_set_inoalignment(mp);
12140771fb45SEric Sandeen 
12150771fb45SEric Sandeen 	/*
12160771fb45SEric Sandeen 	 * Check that the data (and log if separate) are an ok size.
12170771fb45SEric Sandeen 	 */
12184249023aSChristoph Hellwig 	error = xfs_check_sizes(mp);
12190771fb45SEric Sandeen 	if (error)
1220f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
12210771fb45SEric Sandeen 
12220771fb45SEric Sandeen 	/*
12231da177e4SLinus Torvalds 	 * Initialize realtime fields in the mount structure
12241da177e4SLinus Torvalds 	 */
12250771fb45SEric Sandeen 	error = xfs_rtmount_init(mp);
12260771fb45SEric Sandeen 	if (error) {
12271da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: RT mount failed");
1228f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
12291da177e4SLinus Torvalds 	}
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 	/*
12321da177e4SLinus Torvalds 	 *  Copies the low order bits of the timestamp and the randomly
12331da177e4SLinus Torvalds 	 *  set "sequence" number out of a UUID.
12341da177e4SLinus Torvalds 	 */
12351da177e4SLinus Torvalds 	uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	mp->m_dmevmask = 0;	/* not persistent; set after each mount */
12381da177e4SLinus Torvalds 
1239f6c2d1faSNathan Scott 	xfs_dir_mount(mp);
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 	/*
12421da177e4SLinus Torvalds 	 * Initialize the attribute manager's entries.
12431da177e4SLinus Torvalds 	 */
12441da177e4SLinus Torvalds 	mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 	/*
12471da177e4SLinus Torvalds 	 * Initialize the precomputed transaction reservations values.
12481da177e4SLinus Torvalds 	 */
12491da177e4SLinus Torvalds 	xfs_trans_init(mp);
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds 	/*
12521da177e4SLinus Torvalds 	 * Allocate and initialize the per-ag data.
12531da177e4SLinus Torvalds 	 */
12541c1c6ebcSDave Chinner 	spin_lock_init(&mp->m_perag_lock);
12551c1c6ebcSDave Chinner 	INIT_RADIX_TREE(&mp->m_perag_tree, GFP_NOFS);
12561c1c6ebcSDave Chinner 	error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
12571c1c6ebcSDave Chinner 	if (error) {
12581c1c6ebcSDave Chinner 		cmn_err(CE_WARN, "XFS: Failed per-ag init: %d", error);
1259f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
12601c1c6ebcSDave Chinner 	}
12611da177e4SLinus Torvalds 
1262f9057e3dSChristoph Hellwig 	if (!sbp->sb_logblocks) {
1263f9057e3dSChristoph Hellwig 		cmn_err(CE_WARN, "XFS: no log defined");
1264f9057e3dSChristoph Hellwig 		XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp);
1265f9057e3dSChristoph Hellwig 		error = XFS_ERROR(EFSCORRUPTED);
1266f9057e3dSChristoph Hellwig 		goto out_free_perag;
1267f9057e3dSChristoph Hellwig 	}
1268f9057e3dSChristoph Hellwig 
12691da177e4SLinus Torvalds 	/*
12701da177e4SLinus Torvalds 	 * log's mount-time initialization. Perform 1st part recovery if needed
12711da177e4SLinus Torvalds 	 */
12721da177e4SLinus Torvalds 	error = xfs_log_mount(mp, mp->m_logdev_targp,
12731da177e4SLinus Torvalds 			      XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
12741da177e4SLinus Torvalds 			      XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
12751da177e4SLinus Torvalds 	if (error) {
12761da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: log mount failed");
1277f9057e3dSChristoph Hellwig 		goto out_free_perag;
12781da177e4SLinus Torvalds 	}
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds 	/*
128192821e2bSDavid Chinner 	 * Now the log is mounted, we know if it was an unclean shutdown or
128292821e2bSDavid Chinner 	 * not. If it was, with the first phase of recovery has completed, we
128392821e2bSDavid Chinner 	 * have consistent AG blocks on disk. We have not recovered EFIs yet,
128492821e2bSDavid Chinner 	 * but they are recovered transactionally in the second recovery phase
128592821e2bSDavid Chinner 	 * later.
128692821e2bSDavid Chinner 	 *
128792821e2bSDavid Chinner 	 * Hence we can safely re-initialise incore superblock counters from
128892821e2bSDavid Chinner 	 * the per-ag data. These may not be correct if the filesystem was not
128992821e2bSDavid Chinner 	 * cleanly unmounted, so we need to wait for recovery to finish before
129092821e2bSDavid Chinner 	 * doing this.
129192821e2bSDavid Chinner 	 *
129292821e2bSDavid Chinner 	 * If the filesystem was cleanly unmounted, then we can trust the
129392821e2bSDavid Chinner 	 * values in the superblock to be correct and we don't need to do
129492821e2bSDavid Chinner 	 * anything here.
129592821e2bSDavid Chinner 	 *
129692821e2bSDavid Chinner 	 * If we are currently making the filesystem, the initialisation will
129792821e2bSDavid Chinner 	 * fail as the perag data is in an undefined state.
129892821e2bSDavid Chinner 	 */
129992821e2bSDavid Chinner 	if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
130092821e2bSDavid Chinner 	    !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
130192821e2bSDavid Chinner 	     !mp->m_sb.sb_inprogress) {
130292821e2bSDavid Chinner 		error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
1303f9057e3dSChristoph Hellwig 		if (error)
1304f9057e3dSChristoph Hellwig 			goto out_free_perag;
130592821e2bSDavid Chinner 	}
1306f9057e3dSChristoph Hellwig 
130792821e2bSDavid Chinner 	/*
13081da177e4SLinus Torvalds 	 * Get and sanity-check the root inode.
13091da177e4SLinus Torvalds 	 * Save the pointer to it in the mount structure.
13101da177e4SLinus Torvalds 	 */
13111da177e4SLinus Torvalds 	error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip, 0);
13121da177e4SLinus Torvalds 	if (error) {
13131da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: failed to read root inode");
1314f9057e3dSChristoph Hellwig 		goto out_log_dealloc;
13151da177e4SLinus Torvalds 	}
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 	ASSERT(rip != NULL);
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds 	if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) {
13201da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: corrupted root inode");
1321b6574520SNathan Scott 		cmn_err(CE_WARN, "Device %s - root %llu is not a directory",
1322b6574520SNathan Scott 			XFS_BUFTARG_NAME(mp->m_ddev_targp),
1323b6574520SNathan Scott 			(unsigned long long)rip->i_ino);
13241da177e4SLinus Torvalds 		xfs_iunlock(rip, XFS_ILOCK_EXCL);
13251da177e4SLinus Torvalds 		XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
13261da177e4SLinus Torvalds 				 mp);
13271da177e4SLinus Torvalds 		error = XFS_ERROR(EFSCORRUPTED);
1328f9057e3dSChristoph Hellwig 		goto out_rele_rip;
13291da177e4SLinus Torvalds 	}
13301da177e4SLinus Torvalds 	mp->m_rootip = rip;	/* save it */
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds 	xfs_iunlock(rip, XFS_ILOCK_EXCL);
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 	/*
13351da177e4SLinus Torvalds 	 * Initialize realtime inode pointers in the mount structure
13361da177e4SLinus Torvalds 	 */
13370771fb45SEric Sandeen 	error = xfs_rtmount_inodes(mp);
13380771fb45SEric Sandeen 	if (error) {
13391da177e4SLinus Torvalds 		/*
13401da177e4SLinus Torvalds 		 * Free up the root inode.
13411da177e4SLinus Torvalds 		 */
13421da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: failed to read RT inodes");
1343f9057e3dSChristoph Hellwig 		goto out_rele_rip;
13441da177e4SLinus Torvalds 	}
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 	/*
13477884bc86SChristoph Hellwig 	 * If this is a read-only mount defer the superblock updates until
13487884bc86SChristoph Hellwig 	 * the next remount into writeable mode.  Otherwise we would never
13497884bc86SChristoph Hellwig 	 * perform the update e.g. for the root filesystem.
13501da177e4SLinus Torvalds 	 */
13517884bc86SChristoph Hellwig 	if (mp->m_update_flags && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
13527884bc86SChristoph Hellwig 		error = xfs_mount_log_sb(mp, mp->m_update_flags);
1353e5720eecSDavid Chinner 		if (error) {
1354e5720eecSDavid Chinner 			cmn_err(CE_WARN, "XFS: failed to write sb changes");
1355b93b6e43SChristoph Hellwig 			goto out_rtunmount;
1356e5720eecSDavid Chinner 		}
1357e5720eecSDavid Chinner 	}
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 	/*
13601da177e4SLinus Torvalds 	 * Initialise the XFS quota management subsystem for this mount
13611da177e4SLinus Torvalds 	 */
13627d095257SChristoph Hellwig 	if (XFS_IS_QUOTA_RUNNING(mp)) {
13637d095257SChristoph Hellwig 		error = xfs_qm_newmount(mp, &quotamount, &quotaflags);
13640771fb45SEric Sandeen 		if (error)
1365b93b6e43SChristoph Hellwig 			goto out_rtunmount;
13667d095257SChristoph Hellwig 	} else {
13677d095257SChristoph Hellwig 		ASSERT(!XFS_IS_QUOTA_ON(mp));
13687d095257SChristoph Hellwig 
13697d095257SChristoph Hellwig 		/*
13707d095257SChristoph Hellwig 		 * If a file system had quotas running earlier, but decided to
13717d095257SChristoph Hellwig 		 * mount without -o uquota/pquota/gquota options, revoke the
13727d095257SChristoph Hellwig 		 * quotachecked license.
13737d095257SChristoph Hellwig 		 */
13747d095257SChristoph Hellwig 		if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) {
13757d095257SChristoph Hellwig 			cmn_err(CE_NOTE,
13767d095257SChristoph Hellwig 				"XFS: resetting qflags for filesystem %s",
13777d095257SChristoph Hellwig 				mp->m_fsname);
13787d095257SChristoph Hellwig 
13797d095257SChristoph Hellwig 			error = xfs_mount_reset_sbqflags(mp);
13807d095257SChristoph Hellwig 			if (error)
13817d095257SChristoph Hellwig 				return error;
13827d095257SChristoph Hellwig 		}
13837d095257SChristoph Hellwig 	}
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 	/*
13861da177e4SLinus Torvalds 	 * Finish recovering the file system.  This part needed to be
13871da177e4SLinus Torvalds 	 * delayed until after the root and real-time bitmap inodes
13881da177e4SLinus Torvalds 	 * were consistently read in.
13891da177e4SLinus Torvalds 	 */
13904249023aSChristoph Hellwig 	error = xfs_log_mount_finish(mp);
13911da177e4SLinus Torvalds 	if (error) {
13921da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: log mount finish failed");
1393b93b6e43SChristoph Hellwig 		goto out_rtunmount;
13941da177e4SLinus Torvalds 	}
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds 	/*
13971da177e4SLinus Torvalds 	 * Complete the quota initialisation, post-log-replay component.
13981da177e4SLinus Torvalds 	 */
13997d095257SChristoph Hellwig 	if (quotamount) {
14007d095257SChristoph Hellwig 		ASSERT(mp->m_qflags == 0);
14017d095257SChristoph Hellwig 		mp->m_qflags = quotaflags;
14027d095257SChristoph Hellwig 
14037d095257SChristoph Hellwig 		xfs_qm_mount_quotas(mp);
14047d095257SChristoph Hellwig 	}
14057d095257SChristoph Hellwig 
14067d095257SChristoph Hellwig #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
14077d095257SChristoph Hellwig 	if (XFS_IS_QUOTA_ON(mp))
14087d095257SChristoph Hellwig 		xfs_fs_cmn_err(CE_NOTE, mp, "Disk quotas turned on");
14097d095257SChristoph Hellwig 	else
14107d095257SChristoph Hellwig 		xfs_fs_cmn_err(CE_NOTE, mp, "Disk quotas not turned on");
14117d095257SChristoph Hellwig #endif
14121da177e4SLinus Torvalds 
141384e1e99fSDavid Chinner 	/*
141484e1e99fSDavid Chinner 	 * Now we are mounted, reserve a small amount of unused space for
141584e1e99fSDavid Chinner 	 * privileged transactions. This is needed so that transaction
141684e1e99fSDavid Chinner 	 * space required for critical operations can dip into this pool
141784e1e99fSDavid Chinner 	 * when at ENOSPC. This is needed for operations like create with
141884e1e99fSDavid Chinner 	 * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
141984e1e99fSDavid Chinner 	 * are not allowed to use this reserved space.
142084e1e99fSDavid Chinner 	 */
1421d5db0f97SEric Sandeen 	if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
1422d5db0f97SEric Sandeen 		resblks = xfs_default_resblks(mp);
1423714082bcSDavid Chinner 		error = xfs_reserve_blocks(mp, &resblks, NULL);
1424714082bcSDavid Chinner 		if (error)
1425d5db0f97SEric Sandeen 			cmn_err(CE_WARN, "XFS: Unable to allocate reserve "
1426d5db0f97SEric Sandeen 				"blocks. Continuing without a reserve pool.");
1427d5db0f97SEric Sandeen 	}
142884e1e99fSDavid Chinner 
14291da177e4SLinus Torvalds 	return 0;
14301da177e4SLinus Torvalds 
1431b93b6e43SChristoph Hellwig  out_rtunmount:
1432b93b6e43SChristoph Hellwig 	xfs_rtunmount_inodes(mp);
1433f9057e3dSChristoph Hellwig  out_rele_rip:
143443355099SChristoph Hellwig 	IRELE(rip);
1435f9057e3dSChristoph Hellwig  out_log_dealloc:
143621b699c8SChristoph Hellwig 	xfs_log_unmount(mp);
1437f9057e3dSChristoph Hellwig  out_free_perag:
1438ff4f038cSChristoph Hellwig 	xfs_free_perag(mp);
1439f9057e3dSChristoph Hellwig  out_remove_uuid:
144027174203SChristoph Hellwig 	xfs_uuid_unmount(mp);
1441f9057e3dSChristoph Hellwig  out:
14421da177e4SLinus Torvalds 	return error;
14431da177e4SLinus Torvalds }
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds /*
14461da177e4SLinus Torvalds  * This flushes out the inodes,dquots and the superblock, unmounts the
14471da177e4SLinus Torvalds  * log and makes sure that incore structures are freed.
14481da177e4SLinus Torvalds  */
144941b5c2e7SChristoph Hellwig void
145041b5c2e7SChristoph Hellwig xfs_unmountfs(
145141b5c2e7SChristoph Hellwig 	struct xfs_mount	*mp)
14521da177e4SLinus Torvalds {
145384e1e99fSDavid Chinner 	__uint64_t		resblks;
145441b5c2e7SChristoph Hellwig 	int			error;
14551da177e4SLinus Torvalds 
14567d095257SChristoph Hellwig 	xfs_qm_unmount_quotas(mp);
1457b93b6e43SChristoph Hellwig 	xfs_rtunmount_inodes(mp);
145877508ec8SChristoph Hellwig 	IRELE(mp->m_rootip);
145977508ec8SChristoph Hellwig 
1460641c56fbSDavid Chinner 	/*
1461641c56fbSDavid Chinner 	 * We can potentially deadlock here if we have an inode cluster
14629da096fdSMalcolm Parsons 	 * that has been freed has its buffer still pinned in memory because
1463641c56fbSDavid Chinner 	 * the transaction is still sitting in a iclog. The stale inodes
1464641c56fbSDavid Chinner 	 * on that buffer will have their flush locks held until the
1465641c56fbSDavid Chinner 	 * transaction hits the disk and the callbacks run. the inode
1466641c56fbSDavid Chinner 	 * flush takes the flush lock unconditionally and with nothing to
1467641c56fbSDavid Chinner 	 * push out the iclog we will never get that unlocked. hence we
1468641c56fbSDavid Chinner 	 * need to force the log first.
1469641c56fbSDavid Chinner 	 */
1470a14a348bSChristoph Hellwig 	xfs_log_force(mp, XFS_LOG_SYNC);
1471*c854363eSDave Chinner 
1472*c854363eSDave Chinner 	/*
1473*c854363eSDave Chinner 	 * Do a delwri reclaim pass first so that as many dirty inodes are
1474*c854363eSDave Chinner 	 * queued up for IO as possible. Then flush the buffers before making
1475*c854363eSDave Chinner 	 * a synchronous path to catch all the remaining inodes are reclaimed.
1476*c854363eSDave Chinner 	 * This makes the reclaim process as quick as possible by avoiding
1477*c854363eSDave Chinner 	 * synchronous writeout and blocking on inodes already in the delwri
1478*c854363eSDave Chinner 	 * state as much as possible.
1479*c854363eSDave Chinner 	 */
1480*c854363eSDave Chinner 	xfs_reclaim_inodes(mp, 0);
1481*c854363eSDave Chinner 	XFS_bflush(mp->m_ddev_targp);
1482*c854363eSDave Chinner 	xfs_reclaim_inodes(mp, SYNC_WAIT);
14831da177e4SLinus Torvalds 
14847d095257SChristoph Hellwig 	xfs_qm_unmount(mp);
1485a357a121SLachlan McIlroy 
14861da177e4SLinus Torvalds 	/*
14871da177e4SLinus Torvalds 	 * Flush out the log synchronously so that we know for sure
14881da177e4SLinus Torvalds 	 * that nothing is pinned.  This is important because bflush()
14891da177e4SLinus Torvalds 	 * will skip pinned buffers.
14901da177e4SLinus Torvalds 	 */
1491a14a348bSChristoph Hellwig 	xfs_log_force(mp, XFS_LOG_SYNC);
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds 	xfs_binval(mp->m_ddev_targp);
14941da177e4SLinus Torvalds 	if (mp->m_rtdev_targp) {
14951da177e4SLinus Torvalds 		xfs_binval(mp->m_rtdev_targp);
14961da177e4SLinus Torvalds 	}
14971da177e4SLinus Torvalds 
149884e1e99fSDavid Chinner 	/*
149984e1e99fSDavid Chinner 	 * Unreserve any blocks we have so that when we unmount we don't account
150084e1e99fSDavid Chinner 	 * the reserved free space as used. This is really only necessary for
150184e1e99fSDavid Chinner 	 * lazy superblock counting because it trusts the incore superblock
15029da096fdSMalcolm Parsons 	 * counters to be absolutely correct on clean unmount.
150384e1e99fSDavid Chinner 	 *
150484e1e99fSDavid Chinner 	 * We don't bother correcting this elsewhere for lazy superblock
150584e1e99fSDavid Chinner 	 * counting because on mount of an unclean filesystem we reconstruct the
150684e1e99fSDavid Chinner 	 * correct counter value and this is irrelevant.
150784e1e99fSDavid Chinner 	 *
150884e1e99fSDavid Chinner 	 * For non-lazy counter filesystems, this doesn't matter at all because
150984e1e99fSDavid Chinner 	 * we only every apply deltas to the superblock and hence the incore
151084e1e99fSDavid Chinner 	 * value does not matter....
151184e1e99fSDavid Chinner 	 */
151284e1e99fSDavid Chinner 	resblks = 0;
1513714082bcSDavid Chinner 	error = xfs_reserve_blocks(mp, &resblks, NULL);
1514714082bcSDavid Chinner 	if (error)
1515714082bcSDavid Chinner 		cmn_err(CE_WARN, "XFS: Unable to free reserved block pool. "
1516714082bcSDavid Chinner 				"Freespace may not be correct on next mount.");
1517714082bcSDavid Chinner 
1518e5720eecSDavid Chinner 	error = xfs_log_sbcount(mp, 1);
1519e5720eecSDavid Chinner 	if (error)
1520e5720eecSDavid Chinner 		cmn_err(CE_WARN, "XFS: Unable to update superblock counters. "
1521e5720eecSDavid Chinner 				"Freespace may not be correct on next mount.");
15221da177e4SLinus Torvalds 	xfs_unmountfs_writesb(mp);
15231da177e4SLinus Torvalds 	xfs_unmountfs_wait(mp); 		/* wait for async bufs */
152421b699c8SChristoph Hellwig 	xfs_log_unmount_write(mp);
152521b699c8SChristoph Hellwig 	xfs_log_unmount(mp);
152627174203SChristoph Hellwig 	xfs_uuid_unmount(mp);
15271da177e4SLinus Torvalds 
15281550d0b0SChristoph Hellwig #if defined(DEBUG)
15290ce4cfd4SChristoph Hellwig 	xfs_errortag_clearall(mp, 0);
15301da177e4SLinus Torvalds #endif
1531ff4f038cSChristoph Hellwig 	xfs_free_perag(mp);
15321da177e4SLinus Torvalds }
15331da177e4SLinus Torvalds 
1534ba0f32d4SChristoph Hellwig STATIC void
15351da177e4SLinus Torvalds xfs_unmountfs_wait(xfs_mount_t *mp)
15361da177e4SLinus Torvalds {
15371da177e4SLinus Torvalds 	if (mp->m_logdev_targp != mp->m_ddev_targp)
15381da177e4SLinus Torvalds 		xfs_wait_buftarg(mp->m_logdev_targp);
15391da177e4SLinus Torvalds 	if (mp->m_rtdev_targp)
15401da177e4SLinus Torvalds 		xfs_wait_buftarg(mp->m_rtdev_targp);
15411da177e4SLinus Torvalds 	xfs_wait_buftarg(mp->m_ddev_targp);
15421da177e4SLinus Torvalds }
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds int
154592821e2bSDavid Chinner xfs_fs_writable(xfs_mount_t *mp)
154692821e2bSDavid Chinner {
1547b267ce99SChristoph Hellwig 	return !(xfs_test_for_freeze(mp) || XFS_FORCED_SHUTDOWN(mp) ||
1548bd186aa9SChristoph Hellwig 		(mp->m_flags & XFS_MOUNT_RDONLY));
154992821e2bSDavid Chinner }
155092821e2bSDavid Chinner 
155192821e2bSDavid Chinner /*
155292821e2bSDavid Chinner  * xfs_log_sbcount
155392821e2bSDavid Chinner  *
155492821e2bSDavid Chinner  * Called either periodically to keep the on disk superblock values
155592821e2bSDavid Chinner  * roughly up to date or from unmount to make sure the values are
155692821e2bSDavid Chinner  * correct on a clean unmount.
155792821e2bSDavid Chinner  *
155892821e2bSDavid Chinner  * Note this code can be called during the process of freezing, so
155992821e2bSDavid Chinner  * we may need to use the transaction allocator which does not not
156092821e2bSDavid Chinner  * block when the transaction subsystem is in its frozen state.
156192821e2bSDavid Chinner  */
156292821e2bSDavid Chinner int
156392821e2bSDavid Chinner xfs_log_sbcount(
156492821e2bSDavid Chinner 	xfs_mount_t	*mp,
156592821e2bSDavid Chinner 	uint		sync)
156692821e2bSDavid Chinner {
156792821e2bSDavid Chinner 	xfs_trans_t	*tp;
156892821e2bSDavid Chinner 	int		error;
156992821e2bSDavid Chinner 
157092821e2bSDavid Chinner 	if (!xfs_fs_writable(mp))
157192821e2bSDavid Chinner 		return 0;
157292821e2bSDavid Chinner 
1573d4d90b57SChristoph Hellwig 	xfs_icsb_sync_counters(mp, 0);
157492821e2bSDavid Chinner 
157592821e2bSDavid Chinner 	/*
157692821e2bSDavid Chinner 	 * we don't need to do this if we are updating the superblock
157792821e2bSDavid Chinner 	 * counters on every modification.
157892821e2bSDavid Chinner 	 */
157992821e2bSDavid Chinner 	if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
158092821e2bSDavid Chinner 		return 0;
158192821e2bSDavid Chinner 
158280641dc6SChristoph Hellwig 	tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT, KM_SLEEP);
158392821e2bSDavid Chinner 	error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
158492821e2bSDavid Chinner 					XFS_DEFAULT_LOG_COUNT);
158592821e2bSDavid Chinner 	if (error) {
158692821e2bSDavid Chinner 		xfs_trans_cancel(tp, 0);
158792821e2bSDavid Chinner 		return error;
158892821e2bSDavid Chinner 	}
158992821e2bSDavid Chinner 
159092821e2bSDavid Chinner 	xfs_mod_sb(tp, XFS_SB_IFREE | XFS_SB_ICOUNT | XFS_SB_FDBLOCKS);
159192821e2bSDavid Chinner 	if (sync)
159292821e2bSDavid Chinner 		xfs_trans_set_sync(tp);
1593e5720eecSDavid Chinner 	error = xfs_trans_commit(tp, 0);
1594e5720eecSDavid Chinner 	return error;
159592821e2bSDavid Chinner }
159692821e2bSDavid Chinner 
159792821e2bSDavid Chinner int
15981da177e4SLinus Torvalds xfs_unmountfs_writesb(xfs_mount_t *mp)
15991da177e4SLinus Torvalds {
16001da177e4SLinus Torvalds 	xfs_buf_t	*sbp;
16011da177e4SLinus Torvalds 	int		error = 0;
16021da177e4SLinus Torvalds 
16031da177e4SLinus Torvalds 	/*
16041da177e4SLinus Torvalds 	 * skip superblock write if fs is read-only, or
16051da177e4SLinus Torvalds 	 * if we are doing a forced umount.
16061da177e4SLinus Torvalds 	 */
1607bd186aa9SChristoph Hellwig 	if (!((mp->m_flags & XFS_MOUNT_RDONLY) ||
16081da177e4SLinus Torvalds 		XFS_FORCED_SHUTDOWN(mp))) {
16098d280b98SDavid Chinner 
161092821e2bSDavid Chinner 		sbp = xfs_getsb(mp, 0);
16118d280b98SDavid Chinner 
16121da177e4SLinus Torvalds 		XFS_BUF_UNDONE(sbp);
16131da177e4SLinus Torvalds 		XFS_BUF_UNREAD(sbp);
16141da177e4SLinus Torvalds 		XFS_BUF_UNDELAYWRITE(sbp);
16151da177e4SLinus Torvalds 		XFS_BUF_WRITE(sbp);
16161da177e4SLinus Torvalds 		XFS_BUF_UNASYNC(sbp);
16171da177e4SLinus Torvalds 		ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp);
16181da177e4SLinus Torvalds 		xfsbdstrat(mp, sbp);
16191da177e4SLinus Torvalds 		error = xfs_iowait(sbp);
16201da177e4SLinus Torvalds 		if (error)
16211da177e4SLinus Torvalds 			xfs_ioerror_alert("xfs_unmountfs_writesb",
16221da177e4SLinus Torvalds 					  mp, sbp, XFS_BUF_ADDR(sbp));
16231da177e4SLinus Torvalds 		xfs_buf_relse(sbp);
162492821e2bSDavid Chinner 	}
1625014c2544SJesper Juhl 	return error;
16261da177e4SLinus Torvalds }
16271da177e4SLinus Torvalds 
16281da177e4SLinus Torvalds /*
16291da177e4SLinus Torvalds  * xfs_mod_sb() can be used to copy arbitrary changes to the
16301da177e4SLinus Torvalds  * in-core superblock into the superblock buffer to be logged.
16311da177e4SLinus Torvalds  * It does not provide the higher level of locking that is
16321da177e4SLinus Torvalds  * needed to protect the in-core superblock from concurrent
16331da177e4SLinus Torvalds  * access.
16341da177e4SLinus Torvalds  */
16351da177e4SLinus Torvalds void
16361da177e4SLinus Torvalds xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
16371da177e4SLinus Torvalds {
16381da177e4SLinus Torvalds 	xfs_buf_t	*bp;
16391da177e4SLinus Torvalds 	int		first;
16401da177e4SLinus Torvalds 	int		last;
16411da177e4SLinus Torvalds 	xfs_mount_t	*mp;
16421da177e4SLinus Torvalds 	xfs_sb_field_t	f;
16431da177e4SLinus Torvalds 
16441da177e4SLinus Torvalds 	ASSERT(fields);
16451da177e4SLinus Torvalds 	if (!fields)
16461da177e4SLinus Torvalds 		return;
16471da177e4SLinus Torvalds 	mp = tp->t_mountp;
16481da177e4SLinus Torvalds 	bp = xfs_trans_getsb(tp, mp, 0);
16491da177e4SLinus Torvalds 	first = sizeof(xfs_sb_t);
16501da177e4SLinus Torvalds 	last = 0;
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds 	/* translate/copy */
16531da177e4SLinus Torvalds 
16542bdf7cd0SChristoph Hellwig 	xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb, fields);
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds 	/* find modified range */
1657587aa0feSDave Chinner 	f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
1658587aa0feSDave Chinner 	ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1659587aa0feSDave Chinner 	last = xfs_sb_info[f + 1].offset - 1;
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds 	f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
16621da177e4SLinus Torvalds 	ASSERT((1LL << f) & XFS_SB_MOD_BITS);
16631da177e4SLinus Torvalds 	first = xfs_sb_info[f].offset;
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds 	xfs_trans_log_buf(tp, bp, first, last);
16661da177e4SLinus Torvalds }
1667d210a28cSYingping Lu 
1668d210a28cSYingping Lu 
16691da177e4SLinus Torvalds /*
16701da177e4SLinus Torvalds  * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
16711da177e4SLinus Torvalds  * a delta to a specified field in the in-core superblock.  Simply
16721da177e4SLinus Torvalds  * switch on the field indicated and apply the delta to that field.
16731da177e4SLinus Torvalds  * Fields are not allowed to dip below zero, so if the delta would
16741da177e4SLinus Torvalds  * do this do not apply it and return EINVAL.
16751da177e4SLinus Torvalds  *
16763685c2a1SEric Sandeen  * The m_sb_lock must be held when this routine is called.
16771da177e4SLinus Torvalds  */
1678d96f8f89SEric Sandeen STATIC int
167920f4ebf2SDavid Chinner xfs_mod_incore_sb_unlocked(
168020f4ebf2SDavid Chinner 	xfs_mount_t	*mp,
168120f4ebf2SDavid Chinner 	xfs_sb_field_t	field,
168220f4ebf2SDavid Chinner 	int64_t		delta,
168320f4ebf2SDavid Chinner 	int		rsvd)
16841da177e4SLinus Torvalds {
16851da177e4SLinus Torvalds 	int		scounter;	/* short counter for 32 bit fields */
16861da177e4SLinus Torvalds 	long long	lcounter;	/* long counter for 64 bit fields */
16871da177e4SLinus Torvalds 	long long	res_used, rem;
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 	/*
16901da177e4SLinus Torvalds 	 * With the in-core superblock spin lock held, switch
16911da177e4SLinus Torvalds 	 * on the indicated field.  Apply the delta to the
16921da177e4SLinus Torvalds 	 * proper field.  If the fields value would dip below
16931da177e4SLinus Torvalds 	 * 0, then do not apply the delta and return EINVAL.
16941da177e4SLinus Torvalds 	 */
16951da177e4SLinus Torvalds 	switch (field) {
16961da177e4SLinus Torvalds 	case XFS_SBS_ICOUNT:
16971da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_icount;
16981da177e4SLinus Torvalds 		lcounter += delta;
16991da177e4SLinus Torvalds 		if (lcounter < 0) {
17001da177e4SLinus Torvalds 			ASSERT(0);
1701014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
17021da177e4SLinus Torvalds 		}
17031da177e4SLinus Torvalds 		mp->m_sb.sb_icount = lcounter;
1704014c2544SJesper Juhl 		return 0;
17051da177e4SLinus Torvalds 	case XFS_SBS_IFREE:
17061da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_ifree;
17071da177e4SLinus Torvalds 		lcounter += delta;
17081da177e4SLinus Torvalds 		if (lcounter < 0) {
17091da177e4SLinus Torvalds 			ASSERT(0);
1710014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
17111da177e4SLinus Torvalds 		}
17121da177e4SLinus Torvalds 		mp->m_sb.sb_ifree = lcounter;
1713014c2544SJesper Juhl 		return 0;
17141da177e4SLinus Torvalds 	case XFS_SBS_FDBLOCKS:
17154be536deSDavid Chinner 		lcounter = (long long)
17164be536deSDavid Chinner 			mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
17171da177e4SLinus Torvalds 		res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
17181da177e4SLinus Torvalds 
17191da177e4SLinus Torvalds 		if (delta > 0) {		/* Putting blocks back */
17201da177e4SLinus Torvalds 			if (res_used > delta) {
17211da177e4SLinus Torvalds 				mp->m_resblks_avail += delta;
17221da177e4SLinus Torvalds 			} else {
17231da177e4SLinus Torvalds 				rem = delta - res_used;
17241da177e4SLinus Torvalds 				mp->m_resblks_avail = mp->m_resblks;
17251da177e4SLinus Torvalds 				lcounter += rem;
17261da177e4SLinus Torvalds 			}
17271da177e4SLinus Torvalds 		} else {				/* Taking blocks away */
17281da177e4SLinus Torvalds 
17291da177e4SLinus Torvalds 			lcounter += delta;
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds 		/*
17321da177e4SLinus Torvalds 		 * If were out of blocks, use any available reserved blocks if
17331da177e4SLinus Torvalds 		 * were allowed to.
17341da177e4SLinus Torvalds 		 */
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds 			if (lcounter < 0) {
17371da177e4SLinus Torvalds 				if (rsvd) {
17381da177e4SLinus Torvalds 					lcounter = (long long)mp->m_resblks_avail + delta;
17391da177e4SLinus Torvalds 					if (lcounter < 0) {
1740014c2544SJesper Juhl 						return XFS_ERROR(ENOSPC);
17411da177e4SLinus Torvalds 					}
17421da177e4SLinus Torvalds 					mp->m_resblks_avail = lcounter;
1743014c2544SJesper Juhl 					return 0;
17441da177e4SLinus Torvalds 				} else {	/* not reserved */
1745014c2544SJesper Juhl 					return XFS_ERROR(ENOSPC);
17461da177e4SLinus Torvalds 				}
17471da177e4SLinus Torvalds 			}
17481da177e4SLinus Torvalds 		}
17491da177e4SLinus Torvalds 
17504be536deSDavid Chinner 		mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
1751014c2544SJesper Juhl 		return 0;
17521da177e4SLinus Torvalds 	case XFS_SBS_FREXTENTS:
17531da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_frextents;
17541da177e4SLinus Torvalds 		lcounter += delta;
17551da177e4SLinus Torvalds 		if (lcounter < 0) {
1756014c2544SJesper Juhl 			return XFS_ERROR(ENOSPC);
17571da177e4SLinus Torvalds 		}
17581da177e4SLinus Torvalds 		mp->m_sb.sb_frextents = lcounter;
1759014c2544SJesper Juhl 		return 0;
17601da177e4SLinus Torvalds 	case XFS_SBS_DBLOCKS:
17611da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_dblocks;
17621da177e4SLinus Torvalds 		lcounter += delta;
17631da177e4SLinus Torvalds 		if (lcounter < 0) {
17641da177e4SLinus Torvalds 			ASSERT(0);
1765014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
17661da177e4SLinus Torvalds 		}
17671da177e4SLinus Torvalds 		mp->m_sb.sb_dblocks = lcounter;
1768014c2544SJesper Juhl 		return 0;
17691da177e4SLinus Torvalds 	case XFS_SBS_AGCOUNT:
17701da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_agcount;
17711da177e4SLinus Torvalds 		scounter += delta;
17721da177e4SLinus Torvalds 		if (scounter < 0) {
17731da177e4SLinus Torvalds 			ASSERT(0);
1774014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
17751da177e4SLinus Torvalds 		}
17761da177e4SLinus Torvalds 		mp->m_sb.sb_agcount = scounter;
1777014c2544SJesper Juhl 		return 0;
17781da177e4SLinus Torvalds 	case XFS_SBS_IMAX_PCT:
17791da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_imax_pct;
17801da177e4SLinus Torvalds 		scounter += delta;
17811da177e4SLinus Torvalds 		if (scounter < 0) {
17821da177e4SLinus Torvalds 			ASSERT(0);
1783014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
17841da177e4SLinus Torvalds 		}
17851da177e4SLinus Torvalds 		mp->m_sb.sb_imax_pct = scounter;
1786014c2544SJesper Juhl 		return 0;
17871da177e4SLinus Torvalds 	case XFS_SBS_REXTSIZE:
17881da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rextsize;
17891da177e4SLinus Torvalds 		scounter += delta;
17901da177e4SLinus Torvalds 		if (scounter < 0) {
17911da177e4SLinus Torvalds 			ASSERT(0);
1792014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
17931da177e4SLinus Torvalds 		}
17941da177e4SLinus Torvalds 		mp->m_sb.sb_rextsize = scounter;
1795014c2544SJesper Juhl 		return 0;
17961da177e4SLinus Torvalds 	case XFS_SBS_RBMBLOCKS:
17971da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rbmblocks;
17981da177e4SLinus Torvalds 		scounter += delta;
17991da177e4SLinus Torvalds 		if (scounter < 0) {
18001da177e4SLinus Torvalds 			ASSERT(0);
1801014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
18021da177e4SLinus Torvalds 		}
18031da177e4SLinus Torvalds 		mp->m_sb.sb_rbmblocks = scounter;
1804014c2544SJesper Juhl 		return 0;
18051da177e4SLinus Torvalds 	case XFS_SBS_RBLOCKS:
18061da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_rblocks;
18071da177e4SLinus Torvalds 		lcounter += delta;
18081da177e4SLinus Torvalds 		if (lcounter < 0) {
18091da177e4SLinus Torvalds 			ASSERT(0);
1810014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
18111da177e4SLinus Torvalds 		}
18121da177e4SLinus Torvalds 		mp->m_sb.sb_rblocks = lcounter;
1813014c2544SJesper Juhl 		return 0;
18141da177e4SLinus Torvalds 	case XFS_SBS_REXTENTS:
18151da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_rextents;
18161da177e4SLinus Torvalds 		lcounter += delta;
18171da177e4SLinus Torvalds 		if (lcounter < 0) {
18181da177e4SLinus Torvalds 			ASSERT(0);
1819014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
18201da177e4SLinus Torvalds 		}
18211da177e4SLinus Torvalds 		mp->m_sb.sb_rextents = lcounter;
1822014c2544SJesper Juhl 		return 0;
18231da177e4SLinus Torvalds 	case XFS_SBS_REXTSLOG:
18241da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rextslog;
18251da177e4SLinus Torvalds 		scounter += delta;
18261da177e4SLinus Torvalds 		if (scounter < 0) {
18271da177e4SLinus Torvalds 			ASSERT(0);
1828014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
18291da177e4SLinus Torvalds 		}
18301da177e4SLinus Torvalds 		mp->m_sb.sb_rextslog = scounter;
1831014c2544SJesper Juhl 		return 0;
18321da177e4SLinus Torvalds 	default:
18331da177e4SLinus Torvalds 		ASSERT(0);
1834014c2544SJesper Juhl 		return XFS_ERROR(EINVAL);
18351da177e4SLinus Torvalds 	}
18361da177e4SLinus Torvalds }
18371da177e4SLinus Torvalds 
18381da177e4SLinus Torvalds /*
18391da177e4SLinus Torvalds  * xfs_mod_incore_sb() is used to change a field in the in-core
18401da177e4SLinus Torvalds  * superblock structure by the specified delta.  This modification
18413685c2a1SEric Sandeen  * is protected by the m_sb_lock.  Just use the xfs_mod_incore_sb_unlocked()
18421da177e4SLinus Torvalds  * routine to do the work.
18431da177e4SLinus Torvalds  */
18441da177e4SLinus Torvalds int
184520f4ebf2SDavid Chinner xfs_mod_incore_sb(
184620f4ebf2SDavid Chinner 	xfs_mount_t	*mp,
184720f4ebf2SDavid Chinner 	xfs_sb_field_t	field,
184820f4ebf2SDavid Chinner 	int64_t		delta,
184920f4ebf2SDavid Chinner 	int		rsvd)
18501da177e4SLinus Torvalds {
18511da177e4SLinus Torvalds 	int	status;
18521da177e4SLinus Torvalds 
18538d280b98SDavid Chinner 	/* check for per-cpu counters */
18548d280b98SDavid Chinner 	switch (field) {
18558d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
18568d280b98SDavid Chinner 	case XFS_SBS_ICOUNT:
18578d280b98SDavid Chinner 	case XFS_SBS_IFREE:
18588d280b98SDavid Chinner 	case XFS_SBS_FDBLOCKS:
18598d280b98SDavid Chinner 		if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
18608d280b98SDavid Chinner 			status = xfs_icsb_modify_counters(mp, field,
18618d280b98SDavid Chinner 							delta, rsvd);
18628d280b98SDavid Chinner 			break;
18638d280b98SDavid Chinner 		}
18648d280b98SDavid Chinner 		/* FALLTHROUGH */
18658d280b98SDavid Chinner #endif
18668d280b98SDavid Chinner 	default:
18673685c2a1SEric Sandeen 		spin_lock(&mp->m_sb_lock);
18681da177e4SLinus Torvalds 		status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
18693685c2a1SEric Sandeen 		spin_unlock(&mp->m_sb_lock);
18708d280b98SDavid Chinner 		break;
18718d280b98SDavid Chinner 	}
18728d280b98SDavid Chinner 
1873014c2544SJesper Juhl 	return status;
18741da177e4SLinus Torvalds }
18751da177e4SLinus Torvalds 
18761da177e4SLinus Torvalds /*
18771da177e4SLinus Torvalds  * xfs_mod_incore_sb_batch() is used to change more than one field
18781da177e4SLinus Torvalds  * in the in-core superblock structure at a time.  This modification
18791da177e4SLinus Torvalds  * is protected by a lock internal to this module.  The fields and
18801da177e4SLinus Torvalds  * changes to those fields are specified in the array of xfs_mod_sb
18811da177e4SLinus Torvalds  * structures passed in.
18821da177e4SLinus Torvalds  *
18831da177e4SLinus Torvalds  * Either all of the specified deltas will be applied or none of
18841da177e4SLinus Torvalds  * them will.  If any modified field dips below 0, then all modifications
18851da177e4SLinus Torvalds  * will be backed out and EINVAL will be returned.
18861da177e4SLinus Torvalds  */
18871da177e4SLinus Torvalds int
18881da177e4SLinus Torvalds xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
18891da177e4SLinus Torvalds {
18901da177e4SLinus Torvalds 	int		status=0;
18911da177e4SLinus Torvalds 	xfs_mod_sb_t	*msbp;
18921da177e4SLinus Torvalds 
18931da177e4SLinus Torvalds 	/*
18941da177e4SLinus Torvalds 	 * Loop through the array of mod structures and apply each
18951da177e4SLinus Torvalds 	 * individually.  If any fail, then back out all those
18961da177e4SLinus Torvalds 	 * which have already been applied.  Do all of this within
18973685c2a1SEric Sandeen 	 * the scope of the m_sb_lock so that all of the changes will
18981da177e4SLinus Torvalds 	 * be atomic.
18991da177e4SLinus Torvalds 	 */
19003685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
19011da177e4SLinus Torvalds 	msbp = &msb[0];
19021da177e4SLinus Torvalds 	for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) {
19031da177e4SLinus Torvalds 		/*
19041da177e4SLinus Torvalds 		 * Apply the delta at index n.  If it fails, break
19051da177e4SLinus Torvalds 		 * from the loop so we'll fall into the undo loop
19061da177e4SLinus Torvalds 		 * below.
19071da177e4SLinus Torvalds 		 */
19088d280b98SDavid Chinner 		switch (msbp->msb_field) {
19098d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
19108d280b98SDavid Chinner 		case XFS_SBS_ICOUNT:
19118d280b98SDavid Chinner 		case XFS_SBS_IFREE:
19128d280b98SDavid Chinner 		case XFS_SBS_FDBLOCKS:
19138d280b98SDavid Chinner 			if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
19143685c2a1SEric Sandeen 				spin_unlock(&mp->m_sb_lock);
191520b64285SDavid Chinner 				status = xfs_icsb_modify_counters(mp,
19168d280b98SDavid Chinner 							msbp->msb_field,
19171da177e4SLinus Torvalds 							msbp->msb_delta, rsvd);
19183685c2a1SEric Sandeen 				spin_lock(&mp->m_sb_lock);
19198d280b98SDavid Chinner 				break;
19208d280b98SDavid Chinner 			}
19218d280b98SDavid Chinner 			/* FALLTHROUGH */
19228d280b98SDavid Chinner #endif
19238d280b98SDavid Chinner 		default:
19248d280b98SDavid Chinner 			status = xfs_mod_incore_sb_unlocked(mp,
19258d280b98SDavid Chinner 						msbp->msb_field,
19268d280b98SDavid Chinner 						msbp->msb_delta, rsvd);
19278d280b98SDavid Chinner 			break;
19288d280b98SDavid Chinner 		}
19298d280b98SDavid Chinner 
19301da177e4SLinus Torvalds 		if (status != 0) {
19311da177e4SLinus Torvalds 			break;
19321da177e4SLinus Torvalds 		}
19331da177e4SLinus Torvalds 	}
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds 	/*
19361da177e4SLinus Torvalds 	 * If we didn't complete the loop above, then back out
19371da177e4SLinus Torvalds 	 * any changes made to the superblock.  If you add code
19381da177e4SLinus Torvalds 	 * between the loop above and here, make sure that you
19391da177e4SLinus Torvalds 	 * preserve the value of status. Loop back until
19401da177e4SLinus Torvalds 	 * we step below the beginning of the array.  Make sure
19411da177e4SLinus Torvalds 	 * we don't touch anything back there.
19421da177e4SLinus Torvalds 	 */
19431da177e4SLinus Torvalds 	if (status != 0) {
19441da177e4SLinus Torvalds 		msbp--;
19451da177e4SLinus Torvalds 		while (msbp >= msb) {
19468d280b98SDavid Chinner 			switch (msbp->msb_field) {
19478d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
19488d280b98SDavid Chinner 			case XFS_SBS_ICOUNT:
19498d280b98SDavid Chinner 			case XFS_SBS_IFREE:
19508d280b98SDavid Chinner 			case XFS_SBS_FDBLOCKS:
19518d280b98SDavid Chinner 				if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
19523685c2a1SEric Sandeen 					spin_unlock(&mp->m_sb_lock);
195320b64285SDavid Chinner 					status = xfs_icsb_modify_counters(mp,
19548d280b98SDavid Chinner 							msbp->msb_field,
19558d280b98SDavid Chinner 							-(msbp->msb_delta),
19568d280b98SDavid Chinner 							rsvd);
19573685c2a1SEric Sandeen 					spin_lock(&mp->m_sb_lock);
19588d280b98SDavid Chinner 					break;
19598d280b98SDavid Chinner 				}
19608d280b98SDavid Chinner 				/* FALLTHROUGH */
19618d280b98SDavid Chinner #endif
19628d280b98SDavid Chinner 			default:
19631da177e4SLinus Torvalds 				status = xfs_mod_incore_sb_unlocked(mp,
19648d280b98SDavid Chinner 							msbp->msb_field,
19658d280b98SDavid Chinner 							-(msbp->msb_delta),
19668d280b98SDavid Chinner 							rsvd);
19678d280b98SDavid Chinner 				break;
19688d280b98SDavid Chinner 			}
19691da177e4SLinus Torvalds 			ASSERT(status == 0);
19701da177e4SLinus Torvalds 			msbp--;
19711da177e4SLinus Torvalds 		}
19721da177e4SLinus Torvalds 	}
19733685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
1974014c2544SJesper Juhl 	return status;
19751da177e4SLinus Torvalds }
19761da177e4SLinus Torvalds 
19771da177e4SLinus Torvalds /*
19781da177e4SLinus Torvalds  * xfs_getsb() is called to obtain the buffer for the superblock.
19791da177e4SLinus Torvalds  * The buffer is returned locked and read in from disk.
19801da177e4SLinus Torvalds  * The buffer should be released with a call to xfs_brelse().
19811da177e4SLinus Torvalds  *
19821da177e4SLinus Torvalds  * If the flags parameter is BUF_TRYLOCK, then we'll only return
19831da177e4SLinus Torvalds  * the superblock buffer if it can be locked without sleeping.
19841da177e4SLinus Torvalds  * If it can't then we'll return NULL.
19851da177e4SLinus Torvalds  */
19861da177e4SLinus Torvalds xfs_buf_t *
19871da177e4SLinus Torvalds xfs_getsb(
19881da177e4SLinus Torvalds 	xfs_mount_t	*mp,
19891da177e4SLinus Torvalds 	int		flags)
19901da177e4SLinus Torvalds {
19911da177e4SLinus Torvalds 	xfs_buf_t	*bp;
19921da177e4SLinus Torvalds 
19931da177e4SLinus Torvalds 	ASSERT(mp->m_sb_bp != NULL);
19941da177e4SLinus Torvalds 	bp = mp->m_sb_bp;
19950cadda1cSChristoph Hellwig 	if (flags & XBF_TRYLOCK) {
19961da177e4SLinus Torvalds 		if (!XFS_BUF_CPSEMA(bp)) {
19971da177e4SLinus Torvalds 			return NULL;
19981da177e4SLinus Torvalds 		}
19991da177e4SLinus Torvalds 	} else {
20001da177e4SLinus Torvalds 		XFS_BUF_PSEMA(bp, PRIBIO);
20011da177e4SLinus Torvalds 	}
20021da177e4SLinus Torvalds 	XFS_BUF_HOLD(bp);
20031da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISDONE(bp));
2004014c2544SJesper Juhl 	return bp;
20051da177e4SLinus Torvalds }
20061da177e4SLinus Torvalds 
20071da177e4SLinus Torvalds /*
20081da177e4SLinus Torvalds  * Used to free the superblock along various error paths.
20091da177e4SLinus Torvalds  */
20101da177e4SLinus Torvalds void
20111da177e4SLinus Torvalds xfs_freesb(
20121da177e4SLinus Torvalds 	xfs_mount_t	*mp)
20131da177e4SLinus Torvalds {
20141da177e4SLinus Torvalds 	xfs_buf_t	*bp;
20151da177e4SLinus Torvalds 
20161da177e4SLinus Torvalds 	/*
20171da177e4SLinus Torvalds 	 * Use xfs_getsb() so that the buffer will be locked
20181da177e4SLinus Torvalds 	 * when we call xfs_buf_relse().
20191da177e4SLinus Torvalds 	 */
20201da177e4SLinus Torvalds 	bp = xfs_getsb(mp, 0);
20211da177e4SLinus Torvalds 	XFS_BUF_UNMANAGE(bp);
20221da177e4SLinus Torvalds 	xfs_buf_relse(bp);
20231da177e4SLinus Torvalds 	mp->m_sb_bp = NULL;
20241da177e4SLinus Torvalds }
20251da177e4SLinus Torvalds 
20261da177e4SLinus Torvalds /*
20271da177e4SLinus Torvalds  * Used to log changes to the superblock unit and width fields which could
2028e6957ea4SEric Sandeen  * be altered by the mount options, as well as any potential sb_features2
2029e6957ea4SEric Sandeen  * fixup. Only the first superblock is updated.
20301da177e4SLinus Torvalds  */
20317884bc86SChristoph Hellwig int
2032ee1c0908SDavid Chinner xfs_mount_log_sb(
20331da177e4SLinus Torvalds 	xfs_mount_t	*mp,
20341da177e4SLinus Torvalds 	__int64_t	fields)
20351da177e4SLinus Torvalds {
20361da177e4SLinus Torvalds 	xfs_trans_t	*tp;
2037e5720eecSDavid Chinner 	int		error;
20381da177e4SLinus Torvalds 
2039ee1c0908SDavid Chinner 	ASSERT(fields & (XFS_SB_UNIT | XFS_SB_WIDTH | XFS_SB_UUID |
20404b166de0SDavid Chinner 			 XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2 |
20414b166de0SDavid Chinner 			 XFS_SB_VERSIONNUM));
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds 	tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
2044e5720eecSDavid Chinner 	error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
2045e5720eecSDavid Chinner 				XFS_DEFAULT_LOG_COUNT);
2046e5720eecSDavid Chinner 	if (error) {
20471da177e4SLinus Torvalds 		xfs_trans_cancel(tp, 0);
2048e5720eecSDavid Chinner 		return error;
20491da177e4SLinus Torvalds 	}
20501da177e4SLinus Torvalds 	xfs_mod_sb(tp, fields);
2051e5720eecSDavid Chinner 	error = xfs_trans_commit(tp, 0);
2052e5720eecSDavid Chinner 	return error;
20531da177e4SLinus Torvalds }
20548d280b98SDavid Chinner 
20558d280b98SDavid Chinner 
20568d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
20578d280b98SDavid Chinner /*
20588d280b98SDavid Chinner  * Per-cpu incore superblock counters
20598d280b98SDavid Chinner  *
20608d280b98SDavid Chinner  * Simple concept, difficult implementation
20618d280b98SDavid Chinner  *
20628d280b98SDavid Chinner  * Basically, replace the incore superblock counters with a distributed per cpu
20638d280b98SDavid Chinner  * counter for contended fields (e.g.  free block count).
20648d280b98SDavid Chinner  *
20658d280b98SDavid Chinner  * Difficulties arise in that the incore sb is used for ENOSPC checking, and
20668d280b98SDavid Chinner  * hence needs to be accurately read when we are running low on space. Hence
20678d280b98SDavid Chinner  * there is a method to enable and disable the per-cpu counters based on how
20688d280b98SDavid Chinner  * much "stuff" is available in them.
20698d280b98SDavid Chinner  *
20708d280b98SDavid Chinner  * Basically, a counter is enabled if there is enough free resource to justify
20718d280b98SDavid Chinner  * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
20728d280b98SDavid Chinner  * ENOSPC), then we disable the counters to synchronise all callers and
20738d280b98SDavid Chinner  * re-distribute the available resources.
20748d280b98SDavid Chinner  *
20758d280b98SDavid Chinner  * If, once we redistributed the available resources, we still get a failure,
20768d280b98SDavid Chinner  * we disable the per-cpu counter and go through the slow path.
20778d280b98SDavid Chinner  *
20788d280b98SDavid Chinner  * The slow path is the current xfs_mod_incore_sb() function.  This means that
20799da096fdSMalcolm Parsons  * when we disable a per-cpu counter, we need to drain its resources back to
20808d280b98SDavid Chinner  * the global superblock. We do this after disabling the counter to prevent
20818d280b98SDavid Chinner  * more threads from queueing up on the counter.
20828d280b98SDavid Chinner  *
20838d280b98SDavid Chinner  * Essentially, this means that we still need a lock in the fast path to enable
20848d280b98SDavid Chinner  * synchronisation between the global counters and the per-cpu counters. This
20858d280b98SDavid Chinner  * is not a problem because the lock will be local to a CPU almost all the time
20868d280b98SDavid Chinner  * and have little contention except when we get to ENOSPC conditions.
20878d280b98SDavid Chinner  *
20888d280b98SDavid Chinner  * Basically, this lock becomes a barrier that enables us to lock out the fast
20898d280b98SDavid Chinner  * path while we do things like enabling and disabling counters and
20908d280b98SDavid Chinner  * synchronising the counters.
20918d280b98SDavid Chinner  *
20928d280b98SDavid Chinner  * Locking rules:
20938d280b98SDavid Chinner  *
20943685c2a1SEric Sandeen  * 	1. m_sb_lock before picking up per-cpu locks
20958d280b98SDavid Chinner  * 	2. per-cpu locks always picked up via for_each_online_cpu() order
20963685c2a1SEric Sandeen  * 	3. accurate counter sync requires m_sb_lock + per cpu locks
20978d280b98SDavid Chinner  * 	4. modifying per-cpu counters requires holding per-cpu lock
20983685c2a1SEric Sandeen  * 	5. modifying global counters requires holding m_sb_lock
20993685c2a1SEric Sandeen  *	6. enabling or disabling a counter requires holding the m_sb_lock
21008d280b98SDavid Chinner  *	   and _none_ of the per-cpu locks.
21018d280b98SDavid Chinner  *
21028d280b98SDavid Chinner  * Disabled counters are only ever re-enabled by a balance operation
21038d280b98SDavid Chinner  * that results in more free resources per CPU than a given threshold.
21048d280b98SDavid Chinner  * To ensure counters don't remain disabled, they are rebalanced when
21058d280b98SDavid Chinner  * the global resource goes above a higher threshold (i.e. some hysteresis
21068d280b98SDavid Chinner  * is present to prevent thrashing).
21078d280b98SDavid Chinner  */
2108e8234a68SDavid Chinner 
21095a67e4c5SChandra Seetharaman #ifdef CONFIG_HOTPLUG_CPU
2110e8234a68SDavid Chinner /*
2111e8234a68SDavid Chinner  * hot-plug CPU notifier support.
2112e8234a68SDavid Chinner  *
21135a67e4c5SChandra Seetharaman  * We need a notifier per filesystem as we need to be able to identify
21145a67e4c5SChandra Seetharaman  * the filesystem to balance the counters out. This is achieved by
21155a67e4c5SChandra Seetharaman  * having a notifier block embedded in the xfs_mount_t and doing pointer
21165a67e4c5SChandra Seetharaman  * magic to get the mount pointer from the notifier block address.
2117e8234a68SDavid Chinner  */
2118e8234a68SDavid Chinner STATIC int
2119e8234a68SDavid Chinner xfs_icsb_cpu_notify(
2120e8234a68SDavid Chinner 	struct notifier_block *nfb,
2121e8234a68SDavid Chinner 	unsigned long action,
2122e8234a68SDavid Chinner 	void *hcpu)
2123e8234a68SDavid Chinner {
2124e8234a68SDavid Chinner 	xfs_icsb_cnts_t *cntp;
2125e8234a68SDavid Chinner 	xfs_mount_t	*mp;
2126e8234a68SDavid Chinner 
2127e8234a68SDavid Chinner 	mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
2128e8234a68SDavid Chinner 	cntp = (xfs_icsb_cnts_t *)
2129e8234a68SDavid Chinner 			per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
2130e8234a68SDavid Chinner 	switch (action) {
2131e8234a68SDavid Chinner 	case CPU_UP_PREPARE:
21328bb78442SRafael J. Wysocki 	case CPU_UP_PREPARE_FROZEN:
2133e8234a68SDavid Chinner 		/* Easy Case - initialize the area and locks, and
2134e8234a68SDavid Chinner 		 * then rebalance when online does everything else for us. */
213501e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
2136e8234a68SDavid Chinner 		break;
2137e8234a68SDavid Chinner 	case CPU_ONLINE:
21388bb78442SRafael J. Wysocki 	case CPU_ONLINE_FROZEN:
213903135cf7SDavid Chinner 		xfs_icsb_lock(mp);
214045af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
214145af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
214245af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
214303135cf7SDavid Chinner 		xfs_icsb_unlock(mp);
2144e8234a68SDavid Chinner 		break;
2145e8234a68SDavid Chinner 	case CPU_DEAD:
21468bb78442SRafael J. Wysocki 	case CPU_DEAD_FROZEN:
2147e8234a68SDavid Chinner 		/* Disable all the counters, then fold the dead cpu's
2148e8234a68SDavid Chinner 		 * count into the total on the global superblock and
2149e8234a68SDavid Chinner 		 * re-enable the counters. */
215003135cf7SDavid Chinner 		xfs_icsb_lock(mp);
21513685c2a1SEric Sandeen 		spin_lock(&mp->m_sb_lock);
2152e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
2153e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
2154e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
2155e8234a68SDavid Chinner 
2156e8234a68SDavid Chinner 		mp->m_sb.sb_icount += cntp->icsb_icount;
2157e8234a68SDavid Chinner 		mp->m_sb.sb_ifree += cntp->icsb_ifree;
2158e8234a68SDavid Chinner 		mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
2159e8234a68SDavid Chinner 
216001e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
2161e8234a68SDavid Chinner 
216245af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter_locked(mp, XFS_SBS_ICOUNT, 0);
216345af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter_locked(mp, XFS_SBS_IFREE, 0);
216445af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter_locked(mp, XFS_SBS_FDBLOCKS, 0);
21653685c2a1SEric Sandeen 		spin_unlock(&mp->m_sb_lock);
216603135cf7SDavid Chinner 		xfs_icsb_unlock(mp);
2167e8234a68SDavid Chinner 		break;
2168e8234a68SDavid Chinner 	}
2169e8234a68SDavid Chinner 
2170e8234a68SDavid Chinner 	return NOTIFY_OK;
2171e8234a68SDavid Chinner }
21725a67e4c5SChandra Seetharaman #endif /* CONFIG_HOTPLUG_CPU */
2173e8234a68SDavid Chinner 
21748d280b98SDavid Chinner int
21758d280b98SDavid Chinner xfs_icsb_init_counters(
21768d280b98SDavid Chinner 	xfs_mount_t	*mp)
21778d280b98SDavid Chinner {
21788d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
21798d280b98SDavid Chinner 	int		i;
21808d280b98SDavid Chinner 
21818d280b98SDavid Chinner 	mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
21828d280b98SDavid Chinner 	if (mp->m_sb_cnts == NULL)
21838d280b98SDavid Chinner 		return -ENOMEM;
21848d280b98SDavid Chinner 
21855a67e4c5SChandra Seetharaman #ifdef CONFIG_HOTPLUG_CPU
2186e8234a68SDavid Chinner 	mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
2187e8234a68SDavid Chinner 	mp->m_icsb_notifier.priority = 0;
21885a67e4c5SChandra Seetharaman 	register_hotcpu_notifier(&mp->m_icsb_notifier);
21895a67e4c5SChandra Seetharaman #endif /* CONFIG_HOTPLUG_CPU */
2190e8234a68SDavid Chinner 
21918d280b98SDavid Chinner 	for_each_online_cpu(i) {
21928d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
219301e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
21948d280b98SDavid Chinner 	}
219520b64285SDavid Chinner 
219620b64285SDavid Chinner 	mutex_init(&mp->m_icsb_mutex);
219720b64285SDavid Chinner 
21988d280b98SDavid Chinner 	/*
21998d280b98SDavid Chinner 	 * start with all counters disabled so that the
22008d280b98SDavid Chinner 	 * initial balance kicks us off correctly
22018d280b98SDavid Chinner 	 */
22028d280b98SDavid Chinner 	mp->m_icsb_counters = -1;
22038d280b98SDavid Chinner 	return 0;
22048d280b98SDavid Chinner }
22058d280b98SDavid Chinner 
22065478eeadSLachlan McIlroy void
22075478eeadSLachlan McIlroy xfs_icsb_reinit_counters(
22085478eeadSLachlan McIlroy 	xfs_mount_t	*mp)
22095478eeadSLachlan McIlroy {
22105478eeadSLachlan McIlroy 	xfs_icsb_lock(mp);
22115478eeadSLachlan McIlroy 	/*
22125478eeadSLachlan McIlroy 	 * start with all counters disabled so that the
22135478eeadSLachlan McIlroy 	 * initial balance kicks us off correctly
22145478eeadSLachlan McIlroy 	 */
22155478eeadSLachlan McIlroy 	mp->m_icsb_counters = -1;
221645af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
221745af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
221845af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
22195478eeadSLachlan McIlroy 	xfs_icsb_unlock(mp);
22205478eeadSLachlan McIlroy }
22215478eeadSLachlan McIlroy 
2222c962fb79SChristoph Hellwig void
22238d280b98SDavid Chinner xfs_icsb_destroy_counters(
22248d280b98SDavid Chinner 	xfs_mount_t	*mp)
22258d280b98SDavid Chinner {
2226e8234a68SDavid Chinner 	if (mp->m_sb_cnts) {
22275a67e4c5SChandra Seetharaman 		unregister_hotcpu_notifier(&mp->m_icsb_notifier);
22288d280b98SDavid Chinner 		free_percpu(mp->m_sb_cnts);
22298d280b98SDavid Chinner 	}
223003135cf7SDavid Chinner 	mutex_destroy(&mp->m_icsb_mutex);
2231e8234a68SDavid Chinner }
22328d280b98SDavid Chinner 
2233b8f82a4aSChristoph Hellwig STATIC void
223401e1b69cSDavid Chinner xfs_icsb_lock_cntr(
223501e1b69cSDavid Chinner 	xfs_icsb_cnts_t	*icsbp)
223601e1b69cSDavid Chinner {
223701e1b69cSDavid Chinner 	while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
223801e1b69cSDavid Chinner 		ndelay(1000);
223901e1b69cSDavid Chinner 	}
224001e1b69cSDavid Chinner }
224101e1b69cSDavid Chinner 
2242b8f82a4aSChristoph Hellwig STATIC void
224301e1b69cSDavid Chinner xfs_icsb_unlock_cntr(
224401e1b69cSDavid Chinner 	xfs_icsb_cnts_t	*icsbp)
224501e1b69cSDavid Chinner {
224601e1b69cSDavid Chinner 	clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
224701e1b69cSDavid Chinner }
224801e1b69cSDavid Chinner 
22498d280b98SDavid Chinner 
2250b8f82a4aSChristoph Hellwig STATIC void
22518d280b98SDavid Chinner xfs_icsb_lock_all_counters(
22528d280b98SDavid Chinner 	xfs_mount_t	*mp)
22538d280b98SDavid Chinner {
22548d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
22558d280b98SDavid Chinner 	int		i;
22568d280b98SDavid Chinner 
22578d280b98SDavid Chinner 	for_each_online_cpu(i) {
22588d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
225901e1b69cSDavid Chinner 		xfs_icsb_lock_cntr(cntp);
22608d280b98SDavid Chinner 	}
22618d280b98SDavid Chinner }
22628d280b98SDavid Chinner 
2263b8f82a4aSChristoph Hellwig STATIC void
22648d280b98SDavid Chinner xfs_icsb_unlock_all_counters(
22658d280b98SDavid Chinner 	xfs_mount_t	*mp)
22668d280b98SDavid Chinner {
22678d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
22688d280b98SDavid Chinner 	int		i;
22698d280b98SDavid Chinner 
22708d280b98SDavid Chinner 	for_each_online_cpu(i) {
22718d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
227201e1b69cSDavid Chinner 		xfs_icsb_unlock_cntr(cntp);
22738d280b98SDavid Chinner 	}
22748d280b98SDavid Chinner }
22758d280b98SDavid Chinner 
22768d280b98SDavid Chinner STATIC void
22778d280b98SDavid Chinner xfs_icsb_count(
22788d280b98SDavid Chinner 	xfs_mount_t	*mp,
22798d280b98SDavid Chinner 	xfs_icsb_cnts_t	*cnt,
22808d280b98SDavid Chinner 	int		flags)
22818d280b98SDavid Chinner {
22828d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
22838d280b98SDavid Chinner 	int		i;
22848d280b98SDavid Chinner 
22858d280b98SDavid Chinner 	memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
22868d280b98SDavid Chinner 
22878d280b98SDavid Chinner 	if (!(flags & XFS_ICSB_LAZY_COUNT))
22888d280b98SDavid Chinner 		xfs_icsb_lock_all_counters(mp);
22898d280b98SDavid Chinner 
22908d280b98SDavid Chinner 	for_each_online_cpu(i) {
22918d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
22928d280b98SDavid Chinner 		cnt->icsb_icount += cntp->icsb_icount;
22938d280b98SDavid Chinner 		cnt->icsb_ifree += cntp->icsb_ifree;
22948d280b98SDavid Chinner 		cnt->icsb_fdblocks += cntp->icsb_fdblocks;
22958d280b98SDavid Chinner 	}
22968d280b98SDavid Chinner 
22978d280b98SDavid Chinner 	if (!(flags & XFS_ICSB_LAZY_COUNT))
22988d280b98SDavid Chinner 		xfs_icsb_unlock_all_counters(mp);
22998d280b98SDavid Chinner }
23008d280b98SDavid Chinner 
23018d280b98SDavid Chinner STATIC int
23028d280b98SDavid Chinner xfs_icsb_counter_disabled(
23038d280b98SDavid Chinner 	xfs_mount_t	*mp,
23048d280b98SDavid Chinner 	xfs_sb_field_t	field)
23058d280b98SDavid Chinner {
23068d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
23078d280b98SDavid Chinner 	return test_bit(field, &mp->m_icsb_counters);
23088d280b98SDavid Chinner }
23098d280b98SDavid Chinner 
231036fbe6e6SDavid Chinner STATIC void
23118d280b98SDavid Chinner xfs_icsb_disable_counter(
23128d280b98SDavid Chinner 	xfs_mount_t	*mp,
23138d280b98SDavid Chinner 	xfs_sb_field_t	field)
23148d280b98SDavid Chinner {
23158d280b98SDavid Chinner 	xfs_icsb_cnts_t	cnt;
23168d280b98SDavid Chinner 
23178d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
23188d280b98SDavid Chinner 
231920b64285SDavid Chinner 	/*
232020b64285SDavid Chinner 	 * If we are already disabled, then there is nothing to do
232120b64285SDavid Chinner 	 * here. We check before locking all the counters to avoid
232220b64285SDavid Chinner 	 * the expensive lock operation when being called in the
232320b64285SDavid Chinner 	 * slow path and the counter is already disabled. This is
232420b64285SDavid Chinner 	 * safe because the only time we set or clear this state is under
232520b64285SDavid Chinner 	 * the m_icsb_mutex.
232620b64285SDavid Chinner 	 */
232720b64285SDavid Chinner 	if (xfs_icsb_counter_disabled(mp, field))
232836fbe6e6SDavid Chinner 		return;
232920b64285SDavid Chinner 
23308d280b98SDavid Chinner 	xfs_icsb_lock_all_counters(mp);
23318d280b98SDavid Chinner 	if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
23328d280b98SDavid Chinner 		/* drain back to superblock */
23338d280b98SDavid Chinner 
2334ce46193bSChristoph Hellwig 		xfs_icsb_count(mp, &cnt, XFS_ICSB_LAZY_COUNT);
23358d280b98SDavid Chinner 		switch(field) {
23368d280b98SDavid Chinner 		case XFS_SBS_ICOUNT:
23378d280b98SDavid Chinner 			mp->m_sb.sb_icount = cnt.icsb_icount;
23388d280b98SDavid Chinner 			break;
23398d280b98SDavid Chinner 		case XFS_SBS_IFREE:
23408d280b98SDavid Chinner 			mp->m_sb.sb_ifree = cnt.icsb_ifree;
23418d280b98SDavid Chinner 			break;
23428d280b98SDavid Chinner 		case XFS_SBS_FDBLOCKS:
23438d280b98SDavid Chinner 			mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
23448d280b98SDavid Chinner 			break;
23458d280b98SDavid Chinner 		default:
23468d280b98SDavid Chinner 			BUG();
23478d280b98SDavid Chinner 		}
23488d280b98SDavid Chinner 	}
23498d280b98SDavid Chinner 
23508d280b98SDavid Chinner 	xfs_icsb_unlock_all_counters(mp);
23518d280b98SDavid Chinner }
23528d280b98SDavid Chinner 
23538d280b98SDavid Chinner STATIC void
23548d280b98SDavid Chinner xfs_icsb_enable_counter(
23558d280b98SDavid Chinner 	xfs_mount_t	*mp,
23568d280b98SDavid Chinner 	xfs_sb_field_t	field,
23578d280b98SDavid Chinner 	uint64_t	count,
23588d280b98SDavid Chinner 	uint64_t	resid)
23598d280b98SDavid Chinner {
23608d280b98SDavid Chinner 	xfs_icsb_cnts_t	*cntp;
23618d280b98SDavid Chinner 	int		i;
23628d280b98SDavid Chinner 
23638d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
23648d280b98SDavid Chinner 
23658d280b98SDavid Chinner 	xfs_icsb_lock_all_counters(mp);
23668d280b98SDavid Chinner 	for_each_online_cpu(i) {
23678d280b98SDavid Chinner 		cntp = per_cpu_ptr(mp->m_sb_cnts, i);
23688d280b98SDavid Chinner 		switch (field) {
23698d280b98SDavid Chinner 		case XFS_SBS_ICOUNT:
23708d280b98SDavid Chinner 			cntp->icsb_icount = count + resid;
23718d280b98SDavid Chinner 			break;
23728d280b98SDavid Chinner 		case XFS_SBS_IFREE:
23738d280b98SDavid Chinner 			cntp->icsb_ifree = count + resid;
23748d280b98SDavid Chinner 			break;
23758d280b98SDavid Chinner 		case XFS_SBS_FDBLOCKS:
23768d280b98SDavid Chinner 			cntp->icsb_fdblocks = count + resid;
23778d280b98SDavid Chinner 			break;
23788d280b98SDavid Chinner 		default:
23798d280b98SDavid Chinner 			BUG();
23808d280b98SDavid Chinner 			break;
23818d280b98SDavid Chinner 		}
23828d280b98SDavid Chinner 		resid = 0;
23838d280b98SDavid Chinner 	}
23848d280b98SDavid Chinner 	clear_bit(field, &mp->m_icsb_counters);
23858d280b98SDavid Chinner 	xfs_icsb_unlock_all_counters(mp);
23868d280b98SDavid Chinner }
23878d280b98SDavid Chinner 
2388dbcabad1SDavid Chinner void
2389d4d90b57SChristoph Hellwig xfs_icsb_sync_counters_locked(
23908d280b98SDavid Chinner 	xfs_mount_t	*mp,
23918d280b98SDavid Chinner 	int		flags)
23928d280b98SDavid Chinner {
23938d280b98SDavid Chinner 	xfs_icsb_cnts_t	cnt;
23948d280b98SDavid Chinner 
23958d280b98SDavid Chinner 	xfs_icsb_count(mp, &cnt, flags);
23968d280b98SDavid Chinner 
23978d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
23988d280b98SDavid Chinner 		mp->m_sb.sb_icount = cnt.icsb_icount;
23998d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
24008d280b98SDavid Chinner 		mp->m_sb.sb_ifree = cnt.icsb_ifree;
24018d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
24028d280b98SDavid Chinner 		mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
24038d280b98SDavid Chinner }
24048d280b98SDavid Chinner 
24058d280b98SDavid Chinner /*
24068d280b98SDavid Chinner  * Accurate update of per-cpu counters to incore superblock
24078d280b98SDavid Chinner  */
2408d4d90b57SChristoph Hellwig void
24098d280b98SDavid Chinner xfs_icsb_sync_counters(
2410d4d90b57SChristoph Hellwig 	xfs_mount_t	*mp,
2411d4d90b57SChristoph Hellwig 	int		flags)
24128d280b98SDavid Chinner {
2413d4d90b57SChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
2414d4d90b57SChristoph Hellwig 	xfs_icsb_sync_counters_locked(mp, flags);
2415d4d90b57SChristoph Hellwig 	spin_unlock(&mp->m_sb_lock);
24168d280b98SDavid Chinner }
24178d280b98SDavid Chinner 
24188d280b98SDavid Chinner /*
24198d280b98SDavid Chinner  * Balance and enable/disable counters as necessary.
24208d280b98SDavid Chinner  *
242120b64285SDavid Chinner  * Thresholds for re-enabling counters are somewhat magic.  inode counts are
242220b64285SDavid Chinner  * chosen to be the same number as single on disk allocation chunk per CPU, and
242320b64285SDavid Chinner  * free blocks is something far enough zero that we aren't going thrash when we
242420b64285SDavid Chinner  * get near ENOSPC. We also need to supply a minimum we require per cpu to
242520b64285SDavid Chinner  * prevent looping endlessly when xfs_alloc_space asks for more than will
242620b64285SDavid Chinner  * be distributed to a single CPU but each CPU has enough blocks to be
242720b64285SDavid Chinner  * reenabled.
242820b64285SDavid Chinner  *
242920b64285SDavid Chinner  * Note that we can be called when counters are already disabled.
243020b64285SDavid Chinner  * xfs_icsb_disable_counter() optimises the counter locking in this case to
243120b64285SDavid Chinner  * prevent locking every per-cpu counter needlessly.
24328d280b98SDavid Chinner  */
243320b64285SDavid Chinner 
243420b64285SDavid Chinner #define XFS_ICSB_INO_CNTR_REENABLE	(uint64_t)64
24354be536deSDavid Chinner #define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \
243620b64285SDavid Chinner 		(uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp))
24378d280b98SDavid Chinner STATIC void
243845af6c6dSChristoph Hellwig xfs_icsb_balance_counter_locked(
24398d280b98SDavid Chinner 	xfs_mount_t	*mp,
24408d280b98SDavid Chinner 	xfs_sb_field_t  field,
244120b64285SDavid Chinner 	int		min_per_cpu)
24428d280b98SDavid Chinner {
24436fdf8cccSNathan Scott 	uint64_t	count, resid;
24448d280b98SDavid Chinner 	int		weight = num_online_cpus();
244520b64285SDavid Chinner 	uint64_t	min = (uint64_t)min_per_cpu;
24468d280b98SDavid Chinner 
24478d280b98SDavid Chinner 	/* disable counter and sync counter */
24488d280b98SDavid Chinner 	xfs_icsb_disable_counter(mp, field);
24498d280b98SDavid Chinner 
24508d280b98SDavid Chinner 	/* update counters  - first CPU gets residual*/
24518d280b98SDavid Chinner 	switch (field) {
24528d280b98SDavid Chinner 	case XFS_SBS_ICOUNT:
24538d280b98SDavid Chinner 		count = mp->m_sb.sb_icount;
24548d280b98SDavid Chinner 		resid = do_div(count, weight);
245520b64285SDavid Chinner 		if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
245645af6c6dSChristoph Hellwig 			return;
24578d280b98SDavid Chinner 		break;
24588d280b98SDavid Chinner 	case XFS_SBS_IFREE:
24598d280b98SDavid Chinner 		count = mp->m_sb.sb_ifree;
24608d280b98SDavid Chinner 		resid = do_div(count, weight);
246120b64285SDavid Chinner 		if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
246245af6c6dSChristoph Hellwig 			return;
24638d280b98SDavid Chinner 		break;
24648d280b98SDavid Chinner 	case XFS_SBS_FDBLOCKS:
24658d280b98SDavid Chinner 		count = mp->m_sb.sb_fdblocks;
24668d280b98SDavid Chinner 		resid = do_div(count, weight);
246720b64285SDavid Chinner 		if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp)))
246845af6c6dSChristoph Hellwig 			return;
24698d280b98SDavid Chinner 		break;
24708d280b98SDavid Chinner 	default:
24718d280b98SDavid Chinner 		BUG();
24726fdf8cccSNathan Scott 		count = resid = 0;	/* quiet, gcc */
24738d280b98SDavid Chinner 		break;
24748d280b98SDavid Chinner 	}
24758d280b98SDavid Chinner 
24768d280b98SDavid Chinner 	xfs_icsb_enable_counter(mp, field, count, resid);
247745af6c6dSChristoph Hellwig }
247845af6c6dSChristoph Hellwig 
247945af6c6dSChristoph Hellwig STATIC void
248045af6c6dSChristoph Hellwig xfs_icsb_balance_counter(
248145af6c6dSChristoph Hellwig 	xfs_mount_t	*mp,
248245af6c6dSChristoph Hellwig 	xfs_sb_field_t  fields,
248345af6c6dSChristoph Hellwig 	int		min_per_cpu)
248445af6c6dSChristoph Hellwig {
248545af6c6dSChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
248645af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter_locked(mp, fields, min_per_cpu);
24873685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
24888d280b98SDavid Chinner }
24898d280b98SDavid Chinner 
2490a8272ce0SDavid Chinner STATIC int
249120b64285SDavid Chinner xfs_icsb_modify_counters(
24928d280b98SDavid Chinner 	xfs_mount_t	*mp,
24938d280b98SDavid Chinner 	xfs_sb_field_t	field,
249420f4ebf2SDavid Chinner 	int64_t		delta,
249520b64285SDavid Chinner 	int		rsvd)
24968d280b98SDavid Chinner {
24978d280b98SDavid Chinner 	xfs_icsb_cnts_t	*icsbp;
24988d280b98SDavid Chinner 	long long	lcounter;	/* long counter for 64 bit fields */
24997a9e02d6SChristoph Lameter 	int		ret = 0;
25008d280b98SDavid Chinner 
250120b64285SDavid Chinner 	might_sleep();
25028d280b98SDavid Chinner again:
25037a9e02d6SChristoph Lameter 	preempt_disable();
25047a9e02d6SChristoph Lameter 	icsbp = this_cpu_ptr(mp->m_sb_cnts);
250520b64285SDavid Chinner 
250620b64285SDavid Chinner 	/*
250720b64285SDavid Chinner 	 * if the counter is disabled, go to slow path
250820b64285SDavid Chinner 	 */
25098d280b98SDavid Chinner 	if (unlikely(xfs_icsb_counter_disabled(mp, field)))
25108d280b98SDavid Chinner 		goto slow_path;
251120b64285SDavid Chinner 	xfs_icsb_lock_cntr(icsbp);
251220b64285SDavid Chinner 	if (unlikely(xfs_icsb_counter_disabled(mp, field))) {
251320b64285SDavid Chinner 		xfs_icsb_unlock_cntr(icsbp);
251420b64285SDavid Chinner 		goto slow_path;
251520b64285SDavid Chinner 	}
25168d280b98SDavid Chinner 
25178d280b98SDavid Chinner 	switch (field) {
25188d280b98SDavid Chinner 	case XFS_SBS_ICOUNT:
25198d280b98SDavid Chinner 		lcounter = icsbp->icsb_icount;
25208d280b98SDavid Chinner 		lcounter += delta;
25218d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
252220b64285SDavid Chinner 			goto balance_counter;
25238d280b98SDavid Chinner 		icsbp->icsb_icount = lcounter;
25248d280b98SDavid Chinner 		break;
25258d280b98SDavid Chinner 
25268d280b98SDavid Chinner 	case XFS_SBS_IFREE:
25278d280b98SDavid Chinner 		lcounter = icsbp->icsb_ifree;
25288d280b98SDavid Chinner 		lcounter += delta;
25298d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
253020b64285SDavid Chinner 			goto balance_counter;
25318d280b98SDavid Chinner 		icsbp->icsb_ifree = lcounter;
25328d280b98SDavid Chinner 		break;
25338d280b98SDavid Chinner 
25348d280b98SDavid Chinner 	case XFS_SBS_FDBLOCKS:
25358d280b98SDavid Chinner 		BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
25368d280b98SDavid Chinner 
25374be536deSDavid Chinner 		lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
25388d280b98SDavid Chinner 		lcounter += delta;
25398d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
254020b64285SDavid Chinner 			goto balance_counter;
25414be536deSDavid Chinner 		icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
25428d280b98SDavid Chinner 		break;
25438d280b98SDavid Chinner 	default:
25448d280b98SDavid Chinner 		BUG();
25458d280b98SDavid Chinner 		break;
25468d280b98SDavid Chinner 	}
254701e1b69cSDavid Chinner 	xfs_icsb_unlock_cntr(icsbp);
25487a9e02d6SChristoph Lameter 	preempt_enable();
25498d280b98SDavid Chinner 	return 0;
25508d280b98SDavid Chinner 
25518d280b98SDavid Chinner slow_path:
25527a9e02d6SChristoph Lameter 	preempt_enable();
255320b64285SDavid Chinner 
255420b64285SDavid Chinner 	/*
255520b64285SDavid Chinner 	 * serialise with a mutex so we don't burn lots of cpu on
255620b64285SDavid Chinner 	 * the superblock lock. We still need to hold the superblock
255720b64285SDavid Chinner 	 * lock, however, when we modify the global structures.
255820b64285SDavid Chinner 	 */
255903135cf7SDavid Chinner 	xfs_icsb_lock(mp);
256020b64285SDavid Chinner 
256120b64285SDavid Chinner 	/*
256220b64285SDavid Chinner 	 * Now running atomically.
256320b64285SDavid Chinner 	 *
256420b64285SDavid Chinner 	 * If the counter is enabled, someone has beaten us to rebalancing.
256520b64285SDavid Chinner 	 * Drop the lock and try again in the fast path....
256620b64285SDavid Chinner 	 */
256720b64285SDavid Chinner 	if (!(xfs_icsb_counter_disabled(mp, field))) {
256803135cf7SDavid Chinner 		xfs_icsb_unlock(mp);
256920b64285SDavid Chinner 		goto again;
257020b64285SDavid Chinner 	}
257120b64285SDavid Chinner 
257220b64285SDavid Chinner 	/*
257320b64285SDavid Chinner 	 * The counter is currently disabled. Because we are
257420b64285SDavid Chinner 	 * running atomically here, we know a rebalance cannot
257520b64285SDavid Chinner 	 * be in progress. Hence we can go straight to operating
257620b64285SDavid Chinner 	 * on the global superblock. We do not call xfs_mod_incore_sb()
25773685c2a1SEric Sandeen 	 * here even though we need to get the m_sb_lock. Doing so
257820b64285SDavid Chinner 	 * will cause us to re-enter this function and deadlock.
25793685c2a1SEric Sandeen 	 * Hence we get the m_sb_lock ourselves and then call
258020b64285SDavid Chinner 	 * xfs_mod_incore_sb_unlocked() as the unlocked path operates
258120b64285SDavid Chinner 	 * directly on the global counters.
258220b64285SDavid Chinner 	 */
25833685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
258420b64285SDavid Chinner 	ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
25853685c2a1SEric Sandeen 	spin_unlock(&mp->m_sb_lock);
258620b64285SDavid Chinner 
258720b64285SDavid Chinner 	/*
258820b64285SDavid Chinner 	 * Now that we've modified the global superblock, we
258920b64285SDavid Chinner 	 * may be able to re-enable the distributed counters
259020b64285SDavid Chinner 	 * (e.g. lots of space just got freed). After that
259120b64285SDavid Chinner 	 * we are done.
259220b64285SDavid Chinner 	 */
259320b64285SDavid Chinner 	if (ret != ENOSPC)
259445af6c6dSChristoph Hellwig 		xfs_icsb_balance_counter(mp, field, 0);
259503135cf7SDavid Chinner 	xfs_icsb_unlock(mp);
259620b64285SDavid Chinner 	return ret;
259720b64285SDavid Chinner 
259820b64285SDavid Chinner balance_counter:
259901e1b69cSDavid Chinner 	xfs_icsb_unlock_cntr(icsbp);
26007a9e02d6SChristoph Lameter 	preempt_enable();
26018d280b98SDavid Chinner 
260220b64285SDavid Chinner 	/*
260320b64285SDavid Chinner 	 * We may have multiple threads here if multiple per-cpu
260420b64285SDavid Chinner 	 * counters run dry at the same time. This will mean we can
260520b64285SDavid Chinner 	 * do more balances than strictly necessary but it is not
260620b64285SDavid Chinner 	 * the common slowpath case.
260720b64285SDavid Chinner 	 */
260803135cf7SDavid Chinner 	xfs_icsb_lock(mp);
260920b64285SDavid Chinner 
261020b64285SDavid Chinner 	/*
261120b64285SDavid Chinner 	 * running atomically.
261220b64285SDavid Chinner 	 *
261320b64285SDavid Chinner 	 * This will leave the counter in the correct state for future
261420b64285SDavid Chinner 	 * accesses. After the rebalance, we simply try again and our retry
261520b64285SDavid Chinner 	 * will either succeed through the fast path or slow path without
261620b64285SDavid Chinner 	 * another balance operation being required.
261720b64285SDavid Chinner 	 */
261845af6c6dSChristoph Hellwig 	xfs_icsb_balance_counter(mp, field, delta);
261903135cf7SDavid Chinner 	xfs_icsb_unlock(mp);
26208d280b98SDavid Chinner 	goto again;
26218d280b98SDavid Chinner }
26228d280b98SDavid Chinner 
26238d280b98SDavid Chinner #endif
2624