xref: /linux/fs/xfs/xfs_mount.c (revision 49ee6c911f0ae5b3a9a04e0589e3265e52f94f53)
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"
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds STATIC void	xfs_mount_log_sbunit(xfs_mount_t *, __int64_t);
481da177e4SLinus Torvalds STATIC int	xfs_uuid_mount(xfs_mount_t *);
491da177e4SLinus Torvalds STATIC void	xfs_uuid_unmount(xfs_mount_t *mp);
50ba0f32d4SChristoph Hellwig STATIC void	xfs_unmountfs_wait(xfs_mount_t *);
511da177e4SLinus Torvalds 
528d280b98SDavid Chinner 
538d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
548d280b98SDavid Chinner STATIC void	xfs_icsb_destroy_counters(xfs_mount_t *);
5520f4ebf2SDavid Chinner STATIC void	xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t,
5620f4ebf2SDavid Chinner 						int, int);
578d280b98SDavid Chinner STATIC void	xfs_icsb_sync_counters(xfs_mount_t *);
588d280b98SDavid Chinner STATIC int	xfs_icsb_modify_counters(xfs_mount_t *, xfs_sb_field_t,
5920f4ebf2SDavid Chinner 						int64_t, int);
60e8234a68SDavid Chinner STATIC int	xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
618d280b98SDavid Chinner 
628d280b98SDavid Chinner #else
638d280b98SDavid Chinner 
648d280b98SDavid Chinner #define xfs_icsb_destroy_counters(mp)			do { } while (0)
6520b64285SDavid Chinner #define xfs_icsb_balance_counter(mp, a, b, c)		do { } while (0)
668d280b98SDavid Chinner #define xfs_icsb_sync_counters(mp)			do { } while (0)
678d280b98SDavid Chinner #define xfs_icsb_modify_counters(mp, a, b, c)		do { } while (0)
688d280b98SDavid Chinner 
698d280b98SDavid Chinner #endif
708d280b98SDavid Chinner 
711df84c93SChristoph Hellwig static const struct {
721da177e4SLinus Torvalds 	short offset;
731da177e4SLinus Torvalds 	short type;	/* 0 = integer
741da177e4SLinus Torvalds 			 * 1 = binary / string (no translation)
751da177e4SLinus Torvalds 			 */
761da177e4SLinus Torvalds } xfs_sb_info[] = {
771da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_magicnum),   0 },
781da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_blocksize),  0 },
791da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_dblocks),    0 },
801da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rblocks),    0 },
811da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rextents),   0 },
821da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_uuid),       1 },
831da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logstart),   0 },
841da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rootino),    0 },
851da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rbmino),     0 },
861da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rsumino),    0 },
871da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rextsize),   0 },
881da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_agblocks),   0 },
891da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_agcount),    0 },
901da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rbmblocks),  0 },
911da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logblocks),  0 },
921da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_versionnum), 0 },
931da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_sectsize),   0 },
941da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inodesize),  0 },
951da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inopblock),  0 },
961da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_fname[0]),   1 },
971da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_blocklog),   0 },
981da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_sectlog),    0 },
991da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inodelog),   0 },
1001da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inopblog),   0 },
1011da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_agblklog),   0 },
1021da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rextslog),   0 },
1031da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inprogress), 0 },
1041da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_imax_pct),   0 },
1051da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_icount),     0 },
1061da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_ifree),      0 },
1071da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_fdblocks),   0 },
1081da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_frextents),  0 },
1091da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_uquotino),   0 },
1101da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_gquotino),   0 },
1111da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_qflags),     0 },
1121da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_flags),      0 },
1131da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_shared_vn),  0 },
1141da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
1151da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_unit),	 0 },
1161da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_width),	 0 },
1171da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_dirblklog),	 0 },
1181da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logsectlog), 0 },
1191da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logsectsize),0 },
1201da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logsunit),	 0 },
1211da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_features2),	 0 },
1221da177e4SLinus Torvalds     { sizeof(xfs_sb_t),			 0 }
1231da177e4SLinus Torvalds };
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds /*
1261da177e4SLinus Torvalds  * Return a pointer to an initialized xfs_mount structure.
1271da177e4SLinus Torvalds  */
1281da177e4SLinus Torvalds xfs_mount_t *
1291da177e4SLinus Torvalds xfs_mount_init(void)
1301da177e4SLinus Torvalds {
1311da177e4SLinus Torvalds 	xfs_mount_t *mp;
1321da177e4SLinus Torvalds 
1338d280b98SDavid Chinner 	mp = kmem_zalloc(sizeof(xfs_mount_t), KM_SLEEP);
1348d280b98SDavid Chinner 
1358d280b98SDavid Chinner 	if (xfs_icsb_init_counters(mp)) {
1368d280b98SDavid Chinner 		mp->m_flags |= XFS_MOUNT_NO_PERCPU_SB;
1378d280b98SDavid Chinner 	}
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	AIL_LOCKINIT(&mp->m_ail_lock, "xfs_ail");
1401da177e4SLinus Torvalds 	spinlock_init(&mp->m_sb_lock, "xfs_sb");
141794ee1baSJes Sorensen 	mutex_init(&mp->m_ilock);
1421da177e4SLinus Torvalds 	initnsema(&mp->m_growlock, 1, "xfs_grow");
1431da177e4SLinus Torvalds 	/*
1441da177e4SLinus Torvalds 	 * Initialize the AIL.
1451da177e4SLinus Torvalds 	 */
1461da177e4SLinus Torvalds 	xfs_trans_ail_init(mp);
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	atomic_set(&mp->m_active_trans, 0);
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	return mp;
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds /*
1541da177e4SLinus Torvalds  * Free up the resources associated with a mount structure.  Assume that
1551da177e4SLinus Torvalds  * the structure was initially zeroed, so we can tell which fields got
1561da177e4SLinus Torvalds  * initialized.
1571da177e4SLinus Torvalds  */
1581da177e4SLinus Torvalds void
1591da177e4SLinus Torvalds xfs_mount_free(
1601da177e4SLinus Torvalds 	xfs_mount_t	*mp,
1611da177e4SLinus Torvalds 	int		remove_bhv)
1621da177e4SLinus Torvalds {
1631da177e4SLinus Torvalds 	if (mp->m_ihash)
1641da177e4SLinus Torvalds 		xfs_ihash_free(mp);
1651da177e4SLinus Torvalds 	if (mp->m_chash)
1661da177e4SLinus Torvalds 		xfs_chash_free(mp);
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	if (mp->m_perag) {
1691da177e4SLinus Torvalds 		int	agno;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 		for (agno = 0; agno < mp->m_maxagi; agno++)
1721da177e4SLinus Torvalds 			if (mp->m_perag[agno].pagb_list)
1731da177e4SLinus Torvalds 				kmem_free(mp->m_perag[agno].pagb_list,
1741da177e4SLinus Torvalds 						sizeof(xfs_perag_busy_t) *
1751da177e4SLinus Torvalds 							XFS_PAGB_NUM_SLOTS);
1761da177e4SLinus Torvalds 		kmem_free(mp->m_perag,
1771da177e4SLinus Torvalds 			  sizeof(xfs_perag_t) * mp->m_sb.sb_agcount);
1781da177e4SLinus Torvalds 	}
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	AIL_LOCK_DESTROY(&mp->m_ail_lock);
1811da177e4SLinus Torvalds 	spinlock_destroy(&mp->m_sb_lock);
1821da177e4SLinus Torvalds 	mutex_destroy(&mp->m_ilock);
1831da177e4SLinus Torvalds 	freesema(&mp->m_growlock);
1841da177e4SLinus Torvalds 	if (mp->m_quotainfo)
1851da177e4SLinus Torvalds 		XFS_QM_DONE(mp);
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	if (mp->m_fsname != NULL)
1881da177e4SLinus Torvalds 		kmem_free(mp->m_fsname, mp->m_fsname_len);
189fc1f8c1cSNathan Scott 	if (mp->m_rtname != NULL)
190fc1f8c1cSNathan Scott 		kmem_free(mp->m_rtname, strlen(mp->m_rtname) + 1);
191fc1f8c1cSNathan Scott 	if (mp->m_logname != NULL)
192fc1f8c1cSNathan Scott 		kmem_free(mp->m_logname, strlen(mp->m_logname) + 1);
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	if (remove_bhv) {
195b83bd138SNathan Scott 		struct bhv_vfs	*vfsp = XFS_MTOVFS(mp);
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 		bhv_remove_all_vfsops(vfsp, 0);
1981da177e4SLinus Torvalds 		VFS_REMOVEBHV(vfsp, &mp->m_bhv);
1991da177e4SLinus Torvalds 	}
2001da177e4SLinus Torvalds 
2018d280b98SDavid Chinner 	xfs_icsb_destroy_counters(mp);
2021da177e4SLinus Torvalds 	kmem_free(mp, sizeof(xfs_mount_t));
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds 
2054cc929eeSNathan Scott /*
2064cc929eeSNathan Scott  * Check size of device based on the (data/realtime) block count.
2074cc929eeSNathan Scott  * Note: this check is used by the growfs code as well as mount.
2084cc929eeSNathan Scott  */
2094cc929eeSNathan Scott int
2104cc929eeSNathan Scott xfs_sb_validate_fsb_count(
2114cc929eeSNathan Scott 	xfs_sb_t	*sbp,
2124cc929eeSNathan Scott 	__uint64_t	nblocks)
2134cc929eeSNathan Scott {
2144cc929eeSNathan Scott 	ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
2154cc929eeSNathan Scott 	ASSERT(sbp->sb_blocklog >= BBSHIFT);
2164cc929eeSNathan Scott 
2174cc929eeSNathan Scott #if XFS_BIG_BLKNOS     /* Limited by ULONG_MAX of page cache index */
2184cc929eeSNathan Scott 	if (nblocks >> (PAGE_CACHE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
2194cc929eeSNathan Scott 		return E2BIG;
2204cc929eeSNathan Scott #else                  /* Limited by UINT_MAX of sectors */
2214cc929eeSNathan Scott 	if (nblocks << (sbp->sb_blocklog - BBSHIFT) > UINT_MAX)
2224cc929eeSNathan Scott 		return E2BIG;
2234cc929eeSNathan Scott #endif
2244cc929eeSNathan Scott 	return 0;
2254cc929eeSNathan Scott }
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds /*
2281da177e4SLinus Torvalds  * Check the validity of the SB found.
2291da177e4SLinus Torvalds  */
2301da177e4SLinus Torvalds STATIC int
2311da177e4SLinus Torvalds xfs_mount_validate_sb(
2321da177e4SLinus Torvalds 	xfs_mount_t	*mp,
233764d1f89SNathan Scott 	xfs_sb_t	*sbp,
234764d1f89SNathan Scott 	int		flags)
2351da177e4SLinus Torvalds {
2361da177e4SLinus Torvalds 	/*
2371da177e4SLinus Torvalds 	 * If the log device and data device have the
2381da177e4SLinus Torvalds 	 * same device number, the log is internal.
2391da177e4SLinus Torvalds 	 * Consequently, the sb_logstart should be non-zero.  If
2401da177e4SLinus Torvalds 	 * we have a zero sb_logstart in this case, we may be trying to mount
2411da177e4SLinus Torvalds 	 * a volume filesystem in a non-volume manner.
2421da177e4SLinus Torvalds 	 */
2431da177e4SLinus Torvalds 	if (sbp->sb_magicnum != XFS_SB_MAGIC) {
244764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags, "bad magic number");
2451da177e4SLinus Torvalds 		return XFS_ERROR(EWRONGFS);
2461da177e4SLinus Torvalds 	}
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	if (!XFS_SB_GOOD_VERSION(sbp)) {
249764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags, "bad version");
2501da177e4SLinus Torvalds 		return XFS_ERROR(EWRONGFS);
2511da177e4SLinus Torvalds 	}
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	if (unlikely(
2541da177e4SLinus Torvalds 	    sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
255764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags,
256764d1f89SNathan Scott 			"filesystem is marked as having an external log; "
2571da177e4SLinus Torvalds 			"specify logdev on the\nmount command line.");
258764d1f89SNathan Scott 		return XFS_ERROR(EINVAL);
2591da177e4SLinus Torvalds 	}
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	if (unlikely(
2621da177e4SLinus Torvalds 	    sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
263764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags,
264764d1f89SNathan Scott 			"filesystem is marked as having an internal log; "
265764d1f89SNathan Scott 			"do not specify logdev on\nthe mount command line.");
266764d1f89SNathan Scott 		return XFS_ERROR(EINVAL);
2671da177e4SLinus Torvalds 	}
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	/*
2701da177e4SLinus Torvalds 	 * More sanity checking. These were stolen directly from
2711da177e4SLinus Torvalds 	 * xfs_repair.
2721da177e4SLinus Torvalds 	 */
2731da177e4SLinus Torvalds 	if (unlikely(
2741da177e4SLinus Torvalds 	    sbp->sb_agcount <= 0					||
2751da177e4SLinus Torvalds 	    sbp->sb_sectsize < XFS_MIN_SECTORSIZE			||
2761da177e4SLinus Torvalds 	    sbp->sb_sectsize > XFS_MAX_SECTORSIZE			||
2771da177e4SLinus Torvalds 	    sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG			||
2781da177e4SLinus Torvalds 	    sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG			||
2791da177e4SLinus Torvalds 	    sbp->sb_blocksize < XFS_MIN_BLOCKSIZE			||
2801da177e4SLinus Torvalds 	    sbp->sb_blocksize > XFS_MAX_BLOCKSIZE			||
2811da177e4SLinus Torvalds 	    sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG			||
2821da177e4SLinus Torvalds 	    sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG			||
2831da177e4SLinus Torvalds 	    sbp->sb_inodesize < XFS_DINODE_MIN_SIZE			||
2841da177e4SLinus Torvalds 	    sbp->sb_inodesize > XFS_DINODE_MAX_SIZE			||
2859f989c94SNathan Scott 	    sbp->sb_inodelog < XFS_DINODE_MIN_LOG			||
2869f989c94SNathan Scott 	    sbp->sb_inodelog > XFS_DINODE_MAX_LOG			||
2879f989c94SNathan Scott 	    (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog)	||
2881da177e4SLinus Torvalds 	    (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE)	||
2891da177e4SLinus Torvalds 	    (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE)	||
290e50bd16fSNathan Scott 	    (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */))) {
291764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags, "SB sanity check 1 failed");
2921da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
2931da177e4SLinus Torvalds 	}
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	/*
2961da177e4SLinus Torvalds 	 * Sanity check AG count, size fields against data size field
2971da177e4SLinus Torvalds 	 */
2981da177e4SLinus Torvalds 	if (unlikely(
2991da177e4SLinus Torvalds 	    sbp->sb_dblocks == 0 ||
3001da177e4SLinus Torvalds 	    sbp->sb_dblocks >
3011da177e4SLinus Torvalds 	     (xfs_drfsbno_t)sbp->sb_agcount * sbp->sb_agblocks ||
3021da177e4SLinus Torvalds 	    sbp->sb_dblocks < (xfs_drfsbno_t)(sbp->sb_agcount - 1) *
3031da177e4SLinus Torvalds 			      sbp->sb_agblocks + XFS_MIN_AG_BLOCKS)) {
304764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags, "SB sanity check 2 failed");
3051da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
3061da177e4SLinus Torvalds 	}
3071da177e4SLinus Torvalds 
3084cc929eeSNathan Scott 	if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
3094cc929eeSNathan Scott 	    xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
310764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags,
311764d1f89SNathan Scott 			"file system too large to be mounted on this system.");
3121da177e4SLinus Torvalds 		return XFS_ERROR(E2BIG);
3131da177e4SLinus Torvalds 	}
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	if (unlikely(sbp->sb_inprogress)) {
316764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags, "file system busy");
3171da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
3181da177e4SLinus Torvalds 	}
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	/*
321de20614bSNathan Scott 	 * Version 1 directory format has never worked on Linux.
322de20614bSNathan Scott 	 */
323de20614bSNathan Scott 	if (unlikely(!XFS_SB_VERSION_HASDIRV2(sbp))) {
324764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags,
325764d1f89SNathan Scott 			"file system using version 1 directory format");
326de20614bSNathan Scott 		return XFS_ERROR(ENOSYS);
327de20614bSNathan Scott 	}
328de20614bSNathan Scott 
329de20614bSNathan Scott 	/*
3301da177e4SLinus Torvalds 	 * Until this is fixed only page-sized or smaller data blocks work.
3311da177e4SLinus Torvalds 	 */
3321da177e4SLinus Torvalds 	if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
333764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags,
334764d1f89SNathan Scott 			"file system with blocksize %d bytes",
3351da177e4SLinus Torvalds 			sbp->sb_blocksize);
336764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags,
337764d1f89SNathan Scott 			"only pagesize (%ld) or less will currently work.",
3381da177e4SLinus Torvalds 			PAGE_SIZE);
3391da177e4SLinus Torvalds 		return XFS_ERROR(ENOSYS);
3401da177e4SLinus Torvalds 	}
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	return 0;
3431da177e4SLinus Torvalds }
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds xfs_agnumber_t
346c11e2c36SNathan Scott xfs_initialize_perag(
347b83bd138SNathan Scott 	bhv_vfs_t	*vfs,
348c11e2c36SNathan Scott 	xfs_mount_t	*mp,
349c11e2c36SNathan Scott 	xfs_agnumber_t	agcount)
3501da177e4SLinus Torvalds {
3511da177e4SLinus Torvalds 	xfs_agnumber_t	index, max_metadata;
3521da177e4SLinus Torvalds 	xfs_perag_t	*pag;
3531da177e4SLinus Torvalds 	xfs_agino_t	agino;
3541da177e4SLinus Torvalds 	xfs_ino_t	ino;
3551da177e4SLinus Torvalds 	xfs_sb_t	*sbp = &mp->m_sb;
3561da177e4SLinus Torvalds 	xfs_ino_t	max_inum = XFS_MAXINUMBER_32;
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	/* Check to see if the filesystem can overflow 32 bit inodes */
3591da177e4SLinus Torvalds 	agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
3601da177e4SLinus Torvalds 	ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	/* Clear the mount flag if no inode can overflow 32 bits
3631da177e4SLinus Torvalds 	 * on this filesystem, or if specifically requested..
3641da177e4SLinus Torvalds 	 */
365c11e2c36SNathan Scott 	if ((vfs->vfs_flag & VFS_32BITINODES) && ino > max_inum) {
3661da177e4SLinus Torvalds 		mp->m_flags |= XFS_MOUNT_32BITINODES;
3671da177e4SLinus Torvalds 	} else {
3681da177e4SLinus Torvalds 		mp->m_flags &= ~XFS_MOUNT_32BITINODES;
3691da177e4SLinus Torvalds 	}
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	/* If we can overflow then setup the ag headers accordingly */
3721da177e4SLinus Torvalds 	if (mp->m_flags & XFS_MOUNT_32BITINODES) {
3731da177e4SLinus Torvalds 		/* Calculate how much should be reserved for inodes to
3741da177e4SLinus Torvalds 		 * meet the max inode percentage.
3751da177e4SLinus Torvalds 		 */
3761da177e4SLinus Torvalds 		if (mp->m_maxicount) {
3771da177e4SLinus Torvalds 			__uint64_t	icount;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 			icount = sbp->sb_dblocks * sbp->sb_imax_pct;
3801da177e4SLinus Torvalds 			do_div(icount, 100);
3811da177e4SLinus Torvalds 			icount += sbp->sb_agblocks - 1;
382a749ee86SEric Sandeen 			do_div(icount, sbp->sb_agblocks);
3831da177e4SLinus Torvalds 			max_metadata = icount;
3841da177e4SLinus Torvalds 		} else {
3851da177e4SLinus Torvalds 			max_metadata = agcount;
3861da177e4SLinus Torvalds 		}
3871da177e4SLinus Torvalds 		for (index = 0; index < agcount; index++) {
3881da177e4SLinus Torvalds 			ino = XFS_AGINO_TO_INO(mp, index, agino);
3891da177e4SLinus Torvalds 			if (ino > max_inum) {
3901da177e4SLinus Torvalds 				index++;
3911da177e4SLinus Torvalds 				break;
3921da177e4SLinus Torvalds 			}
3931da177e4SLinus Torvalds 
394c41564b5SNathan Scott 			/* This ag is preferred for inodes */
3951da177e4SLinus Torvalds 			pag = &mp->m_perag[index];
3961da177e4SLinus Torvalds 			pag->pagi_inodeok = 1;
3971da177e4SLinus Torvalds 			if (index < max_metadata)
3981da177e4SLinus Torvalds 				pag->pagf_metadata = 1;
3991da177e4SLinus Torvalds 		}
4001da177e4SLinus Torvalds 	} else {
4011da177e4SLinus Torvalds 		/* Setup default behavior for smaller filesystems */
4021da177e4SLinus Torvalds 		for (index = 0; index < agcount; index++) {
4031da177e4SLinus Torvalds 			pag = &mp->m_perag[index];
4041da177e4SLinus Torvalds 			pag->pagi_inodeok = 1;
4051da177e4SLinus Torvalds 		}
4061da177e4SLinus Torvalds 	}
4071da177e4SLinus Torvalds 	return index;
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds /*
4111da177e4SLinus Torvalds  * xfs_xlatesb
4121da177e4SLinus Torvalds  *
4131da177e4SLinus Torvalds  *     data       - on disk version of sb
4141da177e4SLinus Torvalds  *     sb         - a superblock
4151da177e4SLinus Torvalds  *     dir        - conversion direction: <0 - convert sb to buf
4161da177e4SLinus Torvalds  *                                        >0 - convert buf to sb
4171da177e4SLinus Torvalds  *     fields     - which fields to copy (bitmask)
4181da177e4SLinus Torvalds  */
4191da177e4SLinus Torvalds void
4201da177e4SLinus Torvalds xfs_xlatesb(
4211da177e4SLinus Torvalds 	void		*data,
4221da177e4SLinus Torvalds 	xfs_sb_t	*sb,
4231da177e4SLinus Torvalds 	int		dir,
4241da177e4SLinus Torvalds 	__int64_t	fields)
4251da177e4SLinus Torvalds {
4261da177e4SLinus Torvalds 	xfs_caddr_t	buf_ptr;
4271da177e4SLinus Torvalds 	xfs_caddr_t	mem_ptr;
4281da177e4SLinus Torvalds 	xfs_sb_field_t	f;
4291da177e4SLinus Torvalds 	int		first;
4301da177e4SLinus Torvalds 	int		size;
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	ASSERT(dir);
4331da177e4SLinus Torvalds 	ASSERT(fields);
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	if (!fields)
4361da177e4SLinus Torvalds 		return;
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	buf_ptr = (xfs_caddr_t)data;
4391da177e4SLinus Torvalds 	mem_ptr = (xfs_caddr_t)sb;
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	while (fields) {
4421da177e4SLinus Torvalds 		f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
4431da177e4SLinus Torvalds 		first = xfs_sb_info[f].offset;
4441da177e4SLinus Torvalds 		size = xfs_sb_info[f + 1].offset - first;
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 		ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 		if (size == 1 || xfs_sb_info[f].type == 1) {
4491da177e4SLinus Torvalds 			if (dir > 0) {
4501da177e4SLinus Torvalds 				memcpy(mem_ptr + first, buf_ptr + first, size);
4511da177e4SLinus Torvalds 			} else {
4521da177e4SLinus Torvalds 				memcpy(buf_ptr + first, mem_ptr + first, size);
4531da177e4SLinus Torvalds 			}
4541da177e4SLinus Torvalds 		} else {
4551da177e4SLinus Torvalds 			switch (size) {
4561da177e4SLinus Torvalds 			case 2:
4571da177e4SLinus Torvalds 				INT_XLATE(*(__uint16_t*)(buf_ptr+first),
4581da177e4SLinus Torvalds 					  *(__uint16_t*)(mem_ptr+first),
4591da177e4SLinus Torvalds 					  dir, ARCH_CONVERT);
4601da177e4SLinus Torvalds 				break;
4611da177e4SLinus Torvalds 			case 4:
4621da177e4SLinus Torvalds 				INT_XLATE(*(__uint32_t*)(buf_ptr+first),
4631da177e4SLinus Torvalds 					  *(__uint32_t*)(mem_ptr+first),
4641da177e4SLinus Torvalds 					  dir, ARCH_CONVERT);
4651da177e4SLinus Torvalds 				break;
4661da177e4SLinus Torvalds 			case 8:
4671da177e4SLinus Torvalds 				INT_XLATE(*(__uint64_t*)(buf_ptr+first),
4681da177e4SLinus Torvalds 					  *(__uint64_t*)(mem_ptr+first), dir, ARCH_CONVERT);
4691da177e4SLinus Torvalds 				break;
4701da177e4SLinus Torvalds 			default:
4711da177e4SLinus Torvalds 				ASSERT(0);
4721da177e4SLinus Torvalds 			}
4731da177e4SLinus Torvalds 		}
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 		fields &= ~(1LL << f);
4761da177e4SLinus Torvalds 	}
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds /*
4801da177e4SLinus Torvalds  * xfs_readsb
4811da177e4SLinus Torvalds  *
4821da177e4SLinus Torvalds  * Does the initial read of the superblock.
4831da177e4SLinus Torvalds  */
4841da177e4SLinus Torvalds int
485764d1f89SNathan Scott xfs_readsb(xfs_mount_t *mp, int flags)
4861da177e4SLinus Torvalds {
4871da177e4SLinus Torvalds 	unsigned int	sector_size;
4881da177e4SLinus Torvalds 	unsigned int	extra_flags;
4891da177e4SLinus Torvalds 	xfs_buf_t	*bp;
4901da177e4SLinus Torvalds 	xfs_sb_t	*sbp;
4911da177e4SLinus Torvalds 	int		error;
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	ASSERT(mp->m_sb_bp == NULL);
4941da177e4SLinus Torvalds 	ASSERT(mp->m_ddev_targp != NULL);
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	/*
4971da177e4SLinus Torvalds 	 * Allocate a (locked) buffer to hold the superblock.
4981da177e4SLinus Torvalds 	 * This will be kept around at all times to optimize
4991da177e4SLinus Torvalds 	 * access to the superblock.
5001da177e4SLinus Torvalds 	 */
5011da177e4SLinus Torvalds 	sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
5021da177e4SLinus Torvalds 	extra_flags = XFS_BUF_LOCK | XFS_BUF_MANAGE | XFS_BUF_MAPPED;
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
5051da177e4SLinus Torvalds 				BTOBB(sector_size), extra_flags);
5061da177e4SLinus Torvalds 	if (!bp || XFS_BUF_ISERROR(bp)) {
507764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags, "SB read failed");
5081da177e4SLinus Torvalds 		error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
5091da177e4SLinus Torvalds 		goto fail;
5101da177e4SLinus Torvalds 	}
5111da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISBUSY(bp));
5121da177e4SLinus Torvalds 	ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	/*
5151da177e4SLinus Torvalds 	 * Initialize the mount structure from the superblock.
5161da177e4SLinus Torvalds 	 * But first do some basic consistency checking.
5171da177e4SLinus Torvalds 	 */
5181da177e4SLinus Torvalds 	sbp = XFS_BUF_TO_SBP(bp);
5191da177e4SLinus Torvalds 	xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), 1, XFS_SB_ALL_BITS);
5201da177e4SLinus Torvalds 
521764d1f89SNathan Scott 	error = xfs_mount_validate_sb(mp, &(mp->m_sb), flags);
5221da177e4SLinus Torvalds 	if (error) {
523764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags, "SB validate failed");
5241da177e4SLinus Torvalds 		goto fail;
5251da177e4SLinus Torvalds 	}
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 	/*
5281da177e4SLinus Torvalds 	 * We must be able to do sector-sized and sector-aligned IO.
5291da177e4SLinus Torvalds 	 */
5301da177e4SLinus Torvalds 	if (sector_size > mp->m_sb.sb_sectsize) {
531764d1f89SNathan Scott 		xfs_fs_mount_cmn_err(flags,
532764d1f89SNathan Scott 			"device supports only %u byte sectors (not %u)",
5331da177e4SLinus Torvalds 			sector_size, mp->m_sb.sb_sectsize);
5341da177e4SLinus Torvalds 		error = ENOSYS;
5351da177e4SLinus Torvalds 		goto fail;
5361da177e4SLinus Torvalds 	}
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	/*
5391da177e4SLinus Torvalds 	 * If device sector size is smaller than the superblock size,
5401da177e4SLinus Torvalds 	 * re-read the superblock so the buffer is correctly sized.
5411da177e4SLinus Torvalds 	 */
5421da177e4SLinus Torvalds 	if (sector_size < mp->m_sb.sb_sectsize) {
5431da177e4SLinus Torvalds 		XFS_BUF_UNMANAGE(bp);
5441da177e4SLinus Torvalds 		xfs_buf_relse(bp);
5451da177e4SLinus Torvalds 		sector_size = mp->m_sb.sb_sectsize;
5461da177e4SLinus Torvalds 		bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
5471da177e4SLinus Torvalds 					BTOBB(sector_size), extra_flags);
5481da177e4SLinus Torvalds 		if (!bp || XFS_BUF_ISERROR(bp)) {
549764d1f89SNathan Scott 			xfs_fs_mount_cmn_err(flags, "SB re-read failed");
5501da177e4SLinus Torvalds 			error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
5511da177e4SLinus Torvalds 			goto fail;
5521da177e4SLinus Torvalds 		}
5531da177e4SLinus Torvalds 		ASSERT(XFS_BUF_ISBUSY(bp));
5541da177e4SLinus Torvalds 		ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
5551da177e4SLinus Torvalds 	}
5561da177e4SLinus Torvalds 
5575478eeadSLachlan McIlroy 	/* Initialize per-cpu counters */
5585478eeadSLachlan McIlroy 	xfs_icsb_reinit_counters(mp);
5598d280b98SDavid Chinner 
5601da177e4SLinus Torvalds 	mp->m_sb_bp = bp;
5611da177e4SLinus Torvalds 	xfs_buf_relse(bp);
5621da177e4SLinus Torvalds 	ASSERT(XFS_BUF_VALUSEMA(bp) > 0);
5631da177e4SLinus Torvalds 	return 0;
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds  fail:
5661da177e4SLinus Torvalds 	if (bp) {
5671da177e4SLinus Torvalds 		XFS_BUF_UNMANAGE(bp);
5681da177e4SLinus Torvalds 		xfs_buf_relse(bp);
5691da177e4SLinus Torvalds 	}
5701da177e4SLinus Torvalds 	return error;
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds /*
5751da177e4SLinus Torvalds  * xfs_mount_common
5761da177e4SLinus Torvalds  *
5771da177e4SLinus Torvalds  * Mount initialization code establishing various mount
5781da177e4SLinus Torvalds  * fields from the superblock associated with the given
5791da177e4SLinus Torvalds  * mount structure
5801da177e4SLinus Torvalds  */
581ba0f32d4SChristoph Hellwig STATIC void
5821da177e4SLinus Torvalds xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
5831da177e4SLinus Torvalds {
5841da177e4SLinus Torvalds 	int	i;
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	mp->m_agfrotor = mp->m_agirotor = 0;
5871da177e4SLinus Torvalds 	spinlock_init(&mp->m_agirotor_lock, "m_agirotor_lock");
5881da177e4SLinus Torvalds 	mp->m_maxagi = mp->m_sb.sb_agcount;
5891da177e4SLinus Torvalds 	mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
5901da177e4SLinus Torvalds 	mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
5911da177e4SLinus Torvalds 	mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
5921da177e4SLinus Torvalds 	mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
5931da177e4SLinus Torvalds 	mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
5941da177e4SLinus Torvalds 	mp->m_litino = sbp->sb_inodesize -
5951da177e4SLinus Torvalds 		((uint)sizeof(xfs_dinode_core_t) + (uint)sizeof(xfs_agino_t));
5961da177e4SLinus Torvalds 	mp->m_blockmask = sbp->sb_blocksize - 1;
5971da177e4SLinus Torvalds 	mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
5981da177e4SLinus Torvalds 	mp->m_blockwmask = mp->m_blockwsize - 1;
5991da177e4SLinus Torvalds 	INIT_LIST_HEAD(&mp->m_del_inodes);
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	/*
6021da177e4SLinus Torvalds 	 * Setup for attributes, in case they get created.
6031da177e4SLinus Torvalds 	 * This value is for inodes getting attributes for the first time,
6041da177e4SLinus Torvalds 	 * the per-inode value is for old attribute values.
6051da177e4SLinus Torvalds 	 */
6061da177e4SLinus Torvalds 	ASSERT(sbp->sb_inodesize >= 256 && sbp->sb_inodesize <= 2048);
6071da177e4SLinus Torvalds 	switch (sbp->sb_inodesize) {
6081da177e4SLinus Torvalds 	case 256:
609d8cc890dSNathan Scott 		mp->m_attroffset = XFS_LITINO(mp) -
610d8cc890dSNathan Scott 				   XFS_BMDR_SPACE_CALC(MINABTPTRS);
6111da177e4SLinus Torvalds 		break;
6121da177e4SLinus Torvalds 	case 512:
6131da177e4SLinus Torvalds 	case 1024:
6141da177e4SLinus Torvalds 	case 2048:
615d8cc890dSNathan Scott 		mp->m_attroffset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
6161da177e4SLinus Torvalds 		break;
6171da177e4SLinus Torvalds 	default:
6181da177e4SLinus Torvalds 		ASSERT(0);
6191da177e4SLinus Torvalds 	}
6201da177e4SLinus Torvalds 	ASSERT(mp->m_attroffset < XFS_LITINO(mp));
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	for (i = 0; i < 2; i++) {
6231da177e4SLinus Torvalds 		mp->m_alloc_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
6241da177e4SLinus Torvalds 			xfs_alloc, i == 0);
6251da177e4SLinus Torvalds 		mp->m_alloc_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
6261da177e4SLinus Torvalds 			xfs_alloc, i == 0);
6271da177e4SLinus Torvalds 	}
6281da177e4SLinus Torvalds 	for (i = 0; i < 2; i++) {
6291da177e4SLinus Torvalds 		mp->m_bmap_dmxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
6301da177e4SLinus Torvalds 			xfs_bmbt, i == 0);
6311da177e4SLinus Torvalds 		mp->m_bmap_dmnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
6321da177e4SLinus Torvalds 			xfs_bmbt, i == 0);
6331da177e4SLinus Torvalds 	}
6341da177e4SLinus Torvalds 	for (i = 0; i < 2; i++) {
6351da177e4SLinus Torvalds 		mp->m_inobt_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
6361da177e4SLinus Torvalds 			xfs_inobt, i == 0);
6371da177e4SLinus Torvalds 		mp->m_inobt_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
6381da177e4SLinus Torvalds 			xfs_inobt, i == 0);
6391da177e4SLinus Torvalds 	}
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
6421da177e4SLinus Torvalds 	mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
6431da177e4SLinus Torvalds 					sbp->sb_inopblock);
6441da177e4SLinus Torvalds 	mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
6451da177e4SLinus Torvalds }
64692821e2bSDavid Chinner 
64792821e2bSDavid Chinner /*
64892821e2bSDavid Chinner  * xfs_initialize_perag_data
64992821e2bSDavid Chinner  *
65092821e2bSDavid Chinner  * Read in each per-ag structure so we can count up the number of
65192821e2bSDavid Chinner  * allocated inodes, free inodes and used filesystem blocks as this
65292821e2bSDavid Chinner  * information is no longer persistent in the superblock. Once we have
65392821e2bSDavid Chinner  * this information, write it into the in-core superblock structure.
65492821e2bSDavid Chinner  */
65592821e2bSDavid Chinner STATIC int
65692821e2bSDavid Chinner xfs_initialize_perag_data(xfs_mount_t *mp, xfs_agnumber_t agcount)
65792821e2bSDavid Chinner {
65892821e2bSDavid Chinner 	xfs_agnumber_t	index;
65992821e2bSDavid Chinner 	xfs_perag_t	*pag;
66092821e2bSDavid Chinner 	xfs_sb_t	*sbp = &mp->m_sb;
66192821e2bSDavid Chinner 	uint64_t	ifree = 0;
66292821e2bSDavid Chinner 	uint64_t	ialloc = 0;
66392821e2bSDavid Chinner 	uint64_t	bfree = 0;
66492821e2bSDavid Chinner 	uint64_t	bfreelst = 0;
66592821e2bSDavid Chinner 	uint64_t	btree = 0;
66692821e2bSDavid Chinner 	int		error;
66792821e2bSDavid Chinner 	int		s;
66892821e2bSDavid Chinner 
66992821e2bSDavid Chinner 	for (index = 0; index < agcount; index++) {
67092821e2bSDavid Chinner 		/*
67192821e2bSDavid Chinner 		 * read the agf, then the agi. This gets us
67292821e2bSDavid Chinner 		 * all the inforamtion we need and populates the
67392821e2bSDavid Chinner 		 * per-ag structures for us.
67492821e2bSDavid Chinner 		 */
67592821e2bSDavid Chinner 		error = xfs_alloc_pagf_init(mp, NULL, index, 0);
67692821e2bSDavid Chinner 		if (error)
67792821e2bSDavid Chinner 			return error;
67892821e2bSDavid Chinner 
67992821e2bSDavid Chinner 		error = xfs_ialloc_pagi_init(mp, NULL, index);
68092821e2bSDavid Chinner 		if (error)
68192821e2bSDavid Chinner 			return error;
68292821e2bSDavid Chinner 		pag = &mp->m_perag[index];
68392821e2bSDavid Chinner 		ifree += pag->pagi_freecount;
68492821e2bSDavid Chinner 		ialloc += pag->pagi_count;
68592821e2bSDavid Chinner 		bfree += pag->pagf_freeblks;
68692821e2bSDavid Chinner 		bfreelst += pag->pagf_flcount;
68792821e2bSDavid Chinner 		btree += pag->pagf_btreeblks;
68892821e2bSDavid Chinner 	}
68992821e2bSDavid Chinner 	/*
69092821e2bSDavid Chinner 	 * Overwrite incore superblock counters with just-read data
69192821e2bSDavid Chinner 	 */
69292821e2bSDavid Chinner 	s = XFS_SB_LOCK(mp);
69392821e2bSDavid Chinner 	sbp->sb_ifree = ifree;
69492821e2bSDavid Chinner 	sbp->sb_icount = ialloc;
69592821e2bSDavid Chinner 	sbp->sb_fdblocks = bfree + bfreelst + btree;
69692821e2bSDavid Chinner 	XFS_SB_UNLOCK(mp, s);
69792821e2bSDavid Chinner 
69892821e2bSDavid Chinner 	/* Fixup the per-cpu counters as well. */
69992821e2bSDavid Chinner 	xfs_icsb_reinit_counters(mp);
70092821e2bSDavid Chinner 
70192821e2bSDavid Chinner 	return 0;
70292821e2bSDavid Chinner }
70392821e2bSDavid Chinner 
7041da177e4SLinus Torvalds /*
7051da177e4SLinus Torvalds  * xfs_mountfs
7061da177e4SLinus Torvalds  *
7071da177e4SLinus Torvalds  * This function does the following on an initial mount of a file system:
7081da177e4SLinus Torvalds  *	- reads the superblock from disk and init the mount struct
7091da177e4SLinus Torvalds  *	- if we're a 32-bit kernel, do a size check on the superblock
7101da177e4SLinus Torvalds  *		so we don't mount terabyte filesystems
7111da177e4SLinus Torvalds  *	- init mount struct realtime fields
7121da177e4SLinus Torvalds  *	- allocate inode hash table for fs
7131da177e4SLinus Torvalds  *	- init directory manager
7141da177e4SLinus Torvalds  *	- perform recovery and init the log manager
7151da177e4SLinus Torvalds  */
7161da177e4SLinus Torvalds int
7171da177e4SLinus Torvalds xfs_mountfs(
718b83bd138SNathan Scott 	bhv_vfs_t	*vfsp,
7191da177e4SLinus Torvalds 	xfs_mount_t	*mp,
7201da177e4SLinus Torvalds 	int		mfsi_flags)
7211da177e4SLinus Torvalds {
7221da177e4SLinus Torvalds 	xfs_buf_t	*bp;
7231da177e4SLinus Torvalds 	xfs_sb_t	*sbp = &(mp->m_sb);
7241da177e4SLinus Torvalds 	xfs_inode_t	*rip;
72567fcaa73SNathan Scott 	bhv_vnode_t	*rvp = NULL;
7261da177e4SLinus Torvalds 	int		readio_log, writeio_log;
7271da177e4SLinus Torvalds 	xfs_daddr_t	d;
72884e1e99fSDavid Chinner 	__uint64_t	resblks;
7291da177e4SLinus Torvalds 	__int64_t	update_flags;
7301da177e4SLinus Torvalds 	uint		quotamount, quotaflags;
7311da177e4SLinus Torvalds 	int		agno;
7321da177e4SLinus Torvalds 	int		uuid_mounted = 0;
7331da177e4SLinus Torvalds 	int		error = 0;
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	if (mp->m_sb_bp == NULL) {
736764d1f89SNathan Scott 		if ((error = xfs_readsb(mp, mfsi_flags))) {
737014c2544SJesper Juhl 			return error;
7381da177e4SLinus Torvalds 		}
7391da177e4SLinus Torvalds 	}
7401da177e4SLinus Torvalds 	xfs_mount_common(mp, sbp);
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	/*
7431da177e4SLinus Torvalds 	 * Check if sb_agblocks is aligned at stripe boundary
7441da177e4SLinus Torvalds 	 * If sb_agblocks is NOT aligned turn off m_dalign since
7451da177e4SLinus Torvalds 	 * allocator alignment is within an ag, therefore ag has
7461da177e4SLinus Torvalds 	 * to be aligned at stripe boundary.
7471da177e4SLinus Torvalds 	 */
7481da177e4SLinus Torvalds 	update_flags = 0LL;
7491da177e4SLinus Torvalds 	if (mp->m_dalign && !(mfsi_flags & XFS_MFSI_SECOND)) {
7501da177e4SLinus Torvalds 		/*
7511da177e4SLinus Torvalds 		 * If stripe unit and stripe width are not multiples
7521da177e4SLinus Torvalds 		 * of the fs blocksize turn off alignment.
7531da177e4SLinus Torvalds 		 */
7541da177e4SLinus Torvalds 		if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
7551da177e4SLinus Torvalds 		    (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
7561da177e4SLinus Torvalds 			if (mp->m_flags & XFS_MOUNT_RETERR) {
7571da177e4SLinus Torvalds 				cmn_err(CE_WARN,
7581da177e4SLinus Torvalds 					"XFS: alignment check 1 failed");
7591da177e4SLinus Torvalds 				error = XFS_ERROR(EINVAL);
7601da177e4SLinus Torvalds 				goto error1;
7611da177e4SLinus Torvalds 			}
7621da177e4SLinus Torvalds 			mp->m_dalign = mp->m_swidth = 0;
7631da177e4SLinus Torvalds 		} else {
7641da177e4SLinus Torvalds 			/*
7651da177e4SLinus Torvalds 			 * Convert the stripe unit and width to FSBs.
7661da177e4SLinus Torvalds 			 */
7671da177e4SLinus Torvalds 			mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
7681da177e4SLinus Torvalds 			if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
7691da177e4SLinus Torvalds 				if (mp->m_flags & XFS_MOUNT_RETERR) {
7701da177e4SLinus Torvalds 					error = XFS_ERROR(EINVAL);
7711da177e4SLinus Torvalds 					goto error1;
7721da177e4SLinus Torvalds 				}
7731da177e4SLinus Torvalds 				xfs_fs_cmn_err(CE_WARN, mp,
7741da177e4SLinus Torvalds "stripe alignment turned off: sunit(%d)/swidth(%d) incompatible with agsize(%d)",
7751da177e4SLinus Torvalds 					mp->m_dalign, mp->m_swidth,
7761da177e4SLinus Torvalds 					sbp->sb_agblocks);
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 				mp->m_dalign = 0;
7791da177e4SLinus Torvalds 				mp->m_swidth = 0;
7801da177e4SLinus Torvalds 			} else if (mp->m_dalign) {
7811da177e4SLinus Torvalds 				mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
7821da177e4SLinus Torvalds 			} else {
7831da177e4SLinus Torvalds 				if (mp->m_flags & XFS_MOUNT_RETERR) {
7841da177e4SLinus Torvalds 					xfs_fs_cmn_err(CE_WARN, mp,
7851da177e4SLinus Torvalds "stripe alignment turned off: sunit(%d) less than bsize(%d)",
7861da177e4SLinus Torvalds                                         	mp->m_dalign,
7871da177e4SLinus Torvalds 						mp->m_blockmask +1);
7881da177e4SLinus Torvalds 					error = XFS_ERROR(EINVAL);
7891da177e4SLinus Torvalds 					goto error1;
7901da177e4SLinus Torvalds 				}
7911da177e4SLinus Torvalds 				mp->m_swidth = 0;
7921da177e4SLinus Torvalds 			}
7931da177e4SLinus Torvalds 		}
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 		/*
7961da177e4SLinus Torvalds 		 * Update superblock with new values
7971da177e4SLinus Torvalds 		 * and log changes
7981da177e4SLinus Torvalds 		 */
7991da177e4SLinus Torvalds 		if (XFS_SB_VERSION_HASDALIGN(sbp)) {
8001da177e4SLinus Torvalds 			if (sbp->sb_unit != mp->m_dalign) {
8011da177e4SLinus Torvalds 				sbp->sb_unit = mp->m_dalign;
8021da177e4SLinus Torvalds 				update_flags |= XFS_SB_UNIT;
8031da177e4SLinus Torvalds 			}
8041da177e4SLinus Torvalds 			if (sbp->sb_width != mp->m_swidth) {
8051da177e4SLinus Torvalds 				sbp->sb_width = mp->m_swidth;
8061da177e4SLinus Torvalds 				update_flags |= XFS_SB_WIDTH;
8071da177e4SLinus Torvalds 			}
8081da177e4SLinus Torvalds 		}
8091da177e4SLinus Torvalds 	} else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
8101da177e4SLinus Torvalds 		    XFS_SB_VERSION_HASDALIGN(&mp->m_sb)) {
8111da177e4SLinus Torvalds 			mp->m_dalign = sbp->sb_unit;
8121da177e4SLinus Torvalds 			mp->m_swidth = sbp->sb_width;
8131da177e4SLinus Torvalds 	}
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	xfs_alloc_compute_maxlevels(mp);
8161da177e4SLinus Torvalds 	xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
8171da177e4SLinus Torvalds 	xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
8181da177e4SLinus Torvalds 	xfs_ialloc_compute_maxlevels(mp);
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 	if (sbp->sb_imax_pct) {
8211da177e4SLinus Torvalds 		__uint64_t	icount;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 		/* Make sure the maximum inode count is a multiple of the
8241da177e4SLinus Torvalds 		 * units we allocate inodes in.
8251da177e4SLinus Torvalds 		 */
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 		icount = sbp->sb_dblocks * sbp->sb_imax_pct;
8281da177e4SLinus Torvalds 		do_div(icount, 100);
8291da177e4SLinus Torvalds 		do_div(icount, mp->m_ialloc_blks);
8301da177e4SLinus Torvalds 		mp->m_maxicount = (icount * mp->m_ialloc_blks)  <<
8311da177e4SLinus Torvalds 				   sbp->sb_inopblog;
8321da177e4SLinus Torvalds 	} else
8331da177e4SLinus Torvalds 		mp->m_maxicount = 0;
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog);
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 	/*
8381da177e4SLinus Torvalds 	 * XFS uses the uuid from the superblock as the unique
8391da177e4SLinus Torvalds 	 * identifier for fsid.  We can not use the uuid from the volume
8401da177e4SLinus Torvalds 	 * since a single partition filesystem is identical to a single
8411da177e4SLinus Torvalds 	 * partition volume/filesystem.
8421da177e4SLinus Torvalds 	 */
8431da177e4SLinus Torvalds 	if ((mfsi_flags & XFS_MFSI_SECOND) == 0 &&
8441da177e4SLinus Torvalds 	    (mp->m_flags & XFS_MOUNT_NOUUID) == 0) {
84584e1e99fSDavid Chinner 		__uint64_t	ret64;
8461da177e4SLinus Torvalds 		if (xfs_uuid_mount(mp)) {
8471da177e4SLinus Torvalds 			error = XFS_ERROR(EINVAL);
8481da177e4SLinus Torvalds 			goto error1;
8491da177e4SLinus Torvalds 		}
8501da177e4SLinus Torvalds 		uuid_mounted=1;
8511da177e4SLinus Torvalds 		ret64 = uuid_hash64(&sbp->sb_uuid);
8521da177e4SLinus Torvalds 		memcpy(&vfsp->vfs_fsid, &ret64, sizeof(ret64));
8531da177e4SLinus Torvalds 	}
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	/*
8561da177e4SLinus Torvalds 	 * Set the default minimum read and write sizes unless
8571da177e4SLinus Torvalds 	 * already specified in a mount option.
8581da177e4SLinus Torvalds 	 * We use smaller I/O sizes when the file system
8591da177e4SLinus Torvalds 	 * is being used for NFS service (wsync mount option).
8601da177e4SLinus Torvalds 	 */
8611da177e4SLinus Torvalds 	if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
8621da177e4SLinus Torvalds 		if (mp->m_flags & XFS_MOUNT_WSYNC) {
8631da177e4SLinus Torvalds 			readio_log = XFS_WSYNC_READIO_LOG;
8641da177e4SLinus Torvalds 			writeio_log = XFS_WSYNC_WRITEIO_LOG;
8651da177e4SLinus Torvalds 		} else {
8661da177e4SLinus Torvalds 			readio_log = XFS_READIO_LOG_LARGE;
8671da177e4SLinus Torvalds 			writeio_log = XFS_WRITEIO_LOG_LARGE;
8681da177e4SLinus Torvalds 		}
8691da177e4SLinus Torvalds 	} else {
8701da177e4SLinus Torvalds 		readio_log = mp->m_readio_log;
8711da177e4SLinus Torvalds 		writeio_log = mp->m_writeio_log;
8721da177e4SLinus Torvalds 	}
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	if (sbp->sb_blocklog > readio_log) {
8751da177e4SLinus Torvalds 		mp->m_readio_log = sbp->sb_blocklog;
8761da177e4SLinus Torvalds 	} else {
8771da177e4SLinus Torvalds 		mp->m_readio_log = readio_log;
8781da177e4SLinus Torvalds 	}
8791da177e4SLinus Torvalds 	mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
8801da177e4SLinus Torvalds 	if (sbp->sb_blocklog > writeio_log) {
8811da177e4SLinus Torvalds 		mp->m_writeio_log = sbp->sb_blocklog;
8821da177e4SLinus Torvalds 	} else {
8831da177e4SLinus Torvalds 		mp->m_writeio_log = writeio_log;
8841da177e4SLinus Torvalds 	}
8851da177e4SLinus Torvalds 	mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	/*
888425f9dddSEric Sandeen 	 * Set the inode cluster size.
889425f9dddSEric Sandeen 	 * This may still be overridden by the file system
8901da177e4SLinus Torvalds 	 * block size if it is larger than the chosen cluster size.
8911da177e4SLinus Torvalds 	 */
8921da177e4SLinus Torvalds 	mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
893425f9dddSEric Sandeen 
8941da177e4SLinus Torvalds 	/*
8951da177e4SLinus Torvalds 	 * Set whether we're using inode alignment.
8961da177e4SLinus Torvalds 	 */
8971da177e4SLinus Torvalds 	if (XFS_SB_VERSION_HASALIGN(&mp->m_sb) &&
8981da177e4SLinus Torvalds 	    mp->m_sb.sb_inoalignmt >=
8991da177e4SLinus Torvalds 	    XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
9001da177e4SLinus Torvalds 		mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
9011da177e4SLinus Torvalds 	else
9021da177e4SLinus Torvalds 		mp->m_inoalign_mask = 0;
9031da177e4SLinus Torvalds 	/*
9041da177e4SLinus Torvalds 	 * If we are using stripe alignment, check whether
9051da177e4SLinus Torvalds 	 * the stripe unit is a multiple of the inode alignment
9061da177e4SLinus Torvalds 	 */
9071da177e4SLinus Torvalds 	if (mp->m_dalign && mp->m_inoalign_mask &&
9081da177e4SLinus Torvalds 	    !(mp->m_dalign & mp->m_inoalign_mask))
9091da177e4SLinus Torvalds 		mp->m_sinoalign = mp->m_dalign;
9101da177e4SLinus Torvalds 	else
9111da177e4SLinus Torvalds 		mp->m_sinoalign = 0;
9121da177e4SLinus Torvalds 	/*
9131da177e4SLinus Torvalds 	 * Check that the data (and log if separate) are an ok size.
9141da177e4SLinus Torvalds 	 */
9151da177e4SLinus Torvalds 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
9161da177e4SLinus Torvalds 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
9171da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: size check 1 failed");
9181da177e4SLinus Torvalds 		error = XFS_ERROR(E2BIG);
9191da177e4SLinus Torvalds 		goto error1;
9201da177e4SLinus Torvalds 	}
9211da177e4SLinus Torvalds 	error = xfs_read_buf(mp, mp->m_ddev_targp,
9221da177e4SLinus Torvalds 			     d - XFS_FSS_TO_BB(mp, 1),
9231da177e4SLinus Torvalds 			     XFS_FSS_TO_BB(mp, 1), 0, &bp);
9241da177e4SLinus Torvalds 	if (!error) {
9251da177e4SLinus Torvalds 		xfs_buf_relse(bp);
9261da177e4SLinus Torvalds 	} else {
9271da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: size check 2 failed");
9281da177e4SLinus Torvalds 		if (error == ENOSPC) {
9291da177e4SLinus Torvalds 			error = XFS_ERROR(E2BIG);
9301da177e4SLinus Torvalds 		}
9311da177e4SLinus Torvalds 		goto error1;
9321da177e4SLinus Torvalds 	}
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds 	if (((mfsi_flags & XFS_MFSI_CLIENT) == 0) &&
9351da177e4SLinus Torvalds 	    mp->m_logdev_targp != mp->m_ddev_targp) {
9361da177e4SLinus Torvalds 		d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
9371da177e4SLinus Torvalds 		if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
9381da177e4SLinus Torvalds 			cmn_err(CE_WARN, "XFS: size check 3 failed");
9391da177e4SLinus Torvalds 			error = XFS_ERROR(E2BIG);
9401da177e4SLinus Torvalds 			goto error1;
9411da177e4SLinus Torvalds 		}
9421da177e4SLinus Torvalds 		error = xfs_read_buf(mp, mp->m_logdev_targp,
9431da177e4SLinus Torvalds 				     d - XFS_FSB_TO_BB(mp, 1),
9441da177e4SLinus Torvalds 				     XFS_FSB_TO_BB(mp, 1), 0, &bp);
9451da177e4SLinus Torvalds 		if (!error) {
9461da177e4SLinus Torvalds 			xfs_buf_relse(bp);
9471da177e4SLinus Torvalds 		} else {
9481da177e4SLinus Torvalds 			cmn_err(CE_WARN, "XFS: size check 3 failed");
9491da177e4SLinus Torvalds 			if (error == ENOSPC) {
9501da177e4SLinus Torvalds 				error = XFS_ERROR(E2BIG);
9511da177e4SLinus Torvalds 			}
9521da177e4SLinus Torvalds 			goto error1;
9531da177e4SLinus Torvalds 		}
9541da177e4SLinus Torvalds 	}
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	/*
9571da177e4SLinus Torvalds 	 * Initialize realtime fields in the mount structure
9581da177e4SLinus Torvalds 	 */
9591da177e4SLinus Torvalds 	if ((error = xfs_rtmount_init(mp))) {
9601da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: RT mount failed");
9611da177e4SLinus Torvalds 		goto error1;
9621da177e4SLinus Torvalds 	}
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	/*
9651da177e4SLinus Torvalds 	 * For client case we are done now
9661da177e4SLinus Torvalds 	 */
9671da177e4SLinus Torvalds 	if (mfsi_flags & XFS_MFSI_CLIENT) {
968014c2544SJesper Juhl 		return 0;
9691da177e4SLinus Torvalds 	}
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	/*
9721da177e4SLinus Torvalds 	 *  Copies the low order bits of the timestamp and the randomly
9731da177e4SLinus Torvalds 	 *  set "sequence" number out of a UUID.
9741da177e4SLinus Torvalds 	 */
9751da177e4SLinus Torvalds 	uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	/*
9781da177e4SLinus Torvalds 	 *  The vfs structure needs to have a file system independent
9791da177e4SLinus Torvalds 	 *  way of checking for the invariant file system ID.  Since it
9801da177e4SLinus Torvalds 	 *  can't look at mount structures it has a pointer to the data
9811da177e4SLinus Torvalds 	 *  in the mount structure.
9821da177e4SLinus Torvalds 	 *
9831da177e4SLinus Torvalds 	 *  File systems that don't support user level file handles (i.e.
9841da177e4SLinus Torvalds 	 *  all of them except for XFS) will leave vfs_altfsid as NULL.
9851da177e4SLinus Torvalds 	 */
9861da177e4SLinus Torvalds 	vfsp->vfs_altfsid = (xfs_fsid_t *)mp->m_fixedfsid;
9871da177e4SLinus Torvalds 	mp->m_dmevmask = 0;	/* not persistent; set after each mount */
9881da177e4SLinus Torvalds 
989f6c2d1faSNathan Scott 	xfs_dir_mount(mp);
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	/*
9921da177e4SLinus Torvalds 	 * Initialize the attribute manager's entries.
9931da177e4SLinus Torvalds 	 */
9941da177e4SLinus Torvalds 	mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	/*
9971da177e4SLinus Torvalds 	 * Initialize the precomputed transaction reservations values.
9981da177e4SLinus Torvalds 	 */
9991da177e4SLinus Torvalds 	xfs_trans_init(mp);
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 	/*
10021da177e4SLinus Torvalds 	 * Allocate and initialize the inode hash table for this
10031da177e4SLinus Torvalds 	 * file system.
10041da177e4SLinus Torvalds 	 */
10051da177e4SLinus Torvalds 	xfs_ihash_init(mp);
10061da177e4SLinus Torvalds 	xfs_chash_init(mp);
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 	/*
10091da177e4SLinus Torvalds 	 * Allocate and initialize the per-ag data.
10101da177e4SLinus Torvalds 	 */
10111da177e4SLinus Torvalds 	init_rwsem(&mp->m_peraglock);
10121da177e4SLinus Torvalds 	mp->m_perag =
10131da177e4SLinus Torvalds 		kmem_zalloc(sbp->sb_agcount * sizeof(xfs_perag_t), KM_SLEEP);
10141da177e4SLinus Torvalds 
1015c11e2c36SNathan Scott 	mp->m_maxagi = xfs_initialize_perag(vfsp, mp, sbp->sb_agcount);
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	/*
10181da177e4SLinus Torvalds 	 * log's mount-time initialization. Perform 1st part recovery if needed
10191da177e4SLinus Torvalds 	 */
10201da177e4SLinus Torvalds 	if (likely(sbp->sb_logblocks > 0)) {	/* check for volume case */
10211da177e4SLinus Torvalds 		error = xfs_log_mount(mp, mp->m_logdev_targp,
10221da177e4SLinus Torvalds 				      XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
10231da177e4SLinus Torvalds 				      XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
10241da177e4SLinus Torvalds 		if (error) {
10251da177e4SLinus Torvalds 			cmn_err(CE_WARN, "XFS: log mount failed");
10261da177e4SLinus Torvalds 			goto error2;
10271da177e4SLinus Torvalds 		}
10281da177e4SLinus Torvalds 	} else {	/* No log has been defined */
10291da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: no log defined");
10301da177e4SLinus Torvalds 		XFS_ERROR_REPORT("xfs_mountfs_int(1)", XFS_ERRLEVEL_LOW, mp);
10311da177e4SLinus Torvalds 		error = XFS_ERROR(EFSCORRUPTED);
10321da177e4SLinus Torvalds 		goto error2;
10331da177e4SLinus Torvalds 	}
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	/*
103692821e2bSDavid Chinner 	 * Now the log is mounted, we know if it was an unclean shutdown or
103792821e2bSDavid Chinner 	 * not. If it was, with the first phase of recovery has completed, we
103892821e2bSDavid Chinner 	 * have consistent AG blocks on disk. We have not recovered EFIs yet,
103992821e2bSDavid Chinner 	 * but they are recovered transactionally in the second recovery phase
104092821e2bSDavid Chinner 	 * later.
104192821e2bSDavid Chinner 	 *
104292821e2bSDavid Chinner 	 * Hence we can safely re-initialise incore superblock counters from
104392821e2bSDavid Chinner 	 * the per-ag data. These may not be correct if the filesystem was not
104492821e2bSDavid Chinner 	 * cleanly unmounted, so we need to wait for recovery to finish before
104592821e2bSDavid Chinner 	 * doing this.
104692821e2bSDavid Chinner 	 *
104792821e2bSDavid Chinner 	 * If the filesystem was cleanly unmounted, then we can trust the
104892821e2bSDavid Chinner 	 * values in the superblock to be correct and we don't need to do
104992821e2bSDavid Chinner 	 * anything here.
105092821e2bSDavid Chinner 	 *
105192821e2bSDavid Chinner 	 * If we are currently making the filesystem, the initialisation will
105292821e2bSDavid Chinner 	 * fail as the perag data is in an undefined state.
105392821e2bSDavid Chinner 	 */
105492821e2bSDavid Chinner 
105592821e2bSDavid Chinner 	if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
105692821e2bSDavid Chinner 	    !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
105792821e2bSDavid Chinner 	     !mp->m_sb.sb_inprogress) {
105892821e2bSDavid Chinner 		error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
105992821e2bSDavid Chinner 		if (error) {
106092821e2bSDavid Chinner 			goto error2;
106192821e2bSDavid Chinner 		}
106292821e2bSDavid Chinner 	}
106392821e2bSDavid Chinner 	/*
10641da177e4SLinus Torvalds 	 * Get and sanity-check the root inode.
10651da177e4SLinus Torvalds 	 * Save the pointer to it in the mount structure.
10661da177e4SLinus Torvalds 	 */
10671da177e4SLinus Torvalds 	error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip, 0);
10681da177e4SLinus Torvalds 	if (error) {
10691da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: failed to read root inode");
10701da177e4SLinus Torvalds 		goto error3;
10711da177e4SLinus Torvalds 	}
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 	ASSERT(rip != NULL);
10741da177e4SLinus Torvalds 	rvp = XFS_ITOV(rip);
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds 	if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) {
10771da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: corrupted root inode");
1078b6574520SNathan Scott 		cmn_err(CE_WARN, "Device %s - root %llu is not a directory",
1079b6574520SNathan Scott 			XFS_BUFTARG_NAME(mp->m_ddev_targp),
1080b6574520SNathan Scott 			(unsigned long long)rip->i_ino);
10811da177e4SLinus Torvalds 		xfs_iunlock(rip, XFS_ILOCK_EXCL);
10821da177e4SLinus Torvalds 		XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
10831da177e4SLinus Torvalds 				 mp);
10841da177e4SLinus Torvalds 		error = XFS_ERROR(EFSCORRUPTED);
10851da177e4SLinus Torvalds 		goto error4;
10861da177e4SLinus Torvalds 	}
10871da177e4SLinus Torvalds 	mp->m_rootip = rip;	/* save it */
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 	xfs_iunlock(rip, XFS_ILOCK_EXCL);
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 	/*
10921da177e4SLinus Torvalds 	 * Initialize realtime inode pointers in the mount structure
10931da177e4SLinus Torvalds 	 */
10941da177e4SLinus Torvalds 	if ((error = xfs_rtmount_inodes(mp))) {
10951da177e4SLinus Torvalds 		/*
10961da177e4SLinus Torvalds 		 * Free up the root inode.
10971da177e4SLinus Torvalds 		 */
10981da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: failed to read RT inodes");
10991da177e4SLinus Torvalds 		goto error4;
11001da177e4SLinus Torvalds 	}
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 	/*
11031da177e4SLinus Torvalds 	 * If fs is not mounted readonly, then update the superblock
11041da177e4SLinus Torvalds 	 * unit and width changes.
11051da177e4SLinus Torvalds 	 */
11061da177e4SLinus Torvalds 	if (update_flags && !(vfsp->vfs_flag & VFS_RDONLY))
11071da177e4SLinus Torvalds 		xfs_mount_log_sbunit(mp, update_flags);
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 	/*
11101da177e4SLinus Torvalds 	 * Initialise the XFS quota management subsystem for this mount
11111da177e4SLinus Torvalds 	 */
11121da177e4SLinus Torvalds 	if ((error = XFS_QM_INIT(mp, &quotamount, &quotaflags)))
11131da177e4SLinus Torvalds 		goto error4;
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 	/*
11161da177e4SLinus Torvalds 	 * Finish recovering the file system.  This part needed to be
11171da177e4SLinus Torvalds 	 * delayed until after the root and real-time bitmap inodes
11181da177e4SLinus Torvalds 	 * were consistently read in.
11191da177e4SLinus Torvalds 	 */
11201da177e4SLinus Torvalds 	error = xfs_log_mount_finish(mp, mfsi_flags);
11211da177e4SLinus Torvalds 	if (error) {
11221da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: log mount finish failed");
11231da177e4SLinus Torvalds 		goto error4;
11241da177e4SLinus Torvalds 	}
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 	/*
11271da177e4SLinus Torvalds 	 * Complete the quota initialisation, post-log-replay component.
11281da177e4SLinus Torvalds 	 */
11291da177e4SLinus Torvalds 	if ((error = XFS_QM_MOUNT(mp, quotamount, quotaflags, mfsi_flags)))
11301da177e4SLinus Torvalds 		goto error4;
11311da177e4SLinus Torvalds 
113284e1e99fSDavid Chinner 	/*
113384e1e99fSDavid Chinner 	 * Now we are mounted, reserve a small amount of unused space for
113484e1e99fSDavid Chinner 	 * privileged transactions. This is needed so that transaction
113584e1e99fSDavid Chinner 	 * space required for critical operations can dip into this pool
113684e1e99fSDavid Chinner 	 * when at ENOSPC. This is needed for operations like create with
113784e1e99fSDavid Chinner 	 * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
113884e1e99fSDavid Chinner 	 * are not allowed to use this reserved space.
113984e1e99fSDavid Chinner 	 *
114084e1e99fSDavid Chinner 	 * We default to 5% or 1024 fsbs of space reserved, whichever is smaller.
114184e1e99fSDavid Chinner 	 * This may drive us straight to ENOSPC on mount, but that implies
114284e1e99fSDavid Chinner 	 * we were already there on the last unmount.
114384e1e99fSDavid Chinner 	 */
114439726be2SChristoph Hellwig 	resblks = mp->m_sb.sb_dblocks;
114539726be2SChristoph Hellwig 	do_div(resblks, 20);
114639726be2SChristoph Hellwig 	resblks = min_t(__uint64_t, resblks, 1024);
114784e1e99fSDavid Chinner 	xfs_reserve_blocks(mp, &resblks, NULL);
114884e1e99fSDavid Chinner 
11491da177e4SLinus Torvalds 	return 0;
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds  error4:
11521da177e4SLinus Torvalds 	/*
11531da177e4SLinus Torvalds 	 * Free up the root inode.
11541da177e4SLinus Torvalds 	 */
11551da177e4SLinus Torvalds 	VN_RELE(rvp);
11561da177e4SLinus Torvalds  error3:
11571da177e4SLinus Torvalds 	xfs_log_unmount_dealloc(mp);
11581da177e4SLinus Torvalds  error2:
11591da177e4SLinus Torvalds 	xfs_ihash_free(mp);
11601da177e4SLinus Torvalds 	xfs_chash_free(mp);
11611da177e4SLinus Torvalds 	for (agno = 0; agno < sbp->sb_agcount; agno++)
11621da177e4SLinus Torvalds 		if (mp->m_perag[agno].pagb_list)
11631da177e4SLinus Torvalds 			kmem_free(mp->m_perag[agno].pagb_list,
11641da177e4SLinus Torvalds 			  sizeof(xfs_perag_busy_t) * XFS_PAGB_NUM_SLOTS);
11651da177e4SLinus Torvalds 	kmem_free(mp->m_perag, sbp->sb_agcount * sizeof(xfs_perag_t));
11661da177e4SLinus Torvalds 	mp->m_perag = NULL;
11671da177e4SLinus Torvalds 	/* FALLTHROUGH */
11681da177e4SLinus Torvalds  error1:
11691da177e4SLinus Torvalds 	if (uuid_mounted)
11701da177e4SLinus Torvalds 		xfs_uuid_unmount(mp);
11711da177e4SLinus Torvalds 	xfs_freesb(mp);
11721da177e4SLinus Torvalds 	return error;
11731da177e4SLinus Torvalds }
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds /*
11761da177e4SLinus Torvalds  * xfs_unmountfs
11771da177e4SLinus Torvalds  *
11781da177e4SLinus Torvalds  * This flushes out the inodes,dquots and the superblock, unmounts the
11791da177e4SLinus Torvalds  * log and makes sure that incore structures are freed.
11801da177e4SLinus Torvalds  */
11811da177e4SLinus Torvalds int
11821da177e4SLinus Torvalds xfs_unmountfs(xfs_mount_t *mp, struct cred *cr)
11831da177e4SLinus Torvalds {
1184b83bd138SNathan Scott 	struct bhv_vfs	*vfsp = XFS_MTOVFS(mp);
11851da177e4SLinus Torvalds #if defined(DEBUG) || defined(INDUCE_IO_ERROR)
11861da177e4SLinus Torvalds 	int64_t		fsid;
11871da177e4SLinus Torvalds #endif
118884e1e99fSDavid Chinner 	__uint64_t	resblks;
11891da177e4SLinus Torvalds 
1190641c56fbSDavid Chinner 	/*
1191641c56fbSDavid Chinner 	 * We can potentially deadlock here if we have an inode cluster
1192641c56fbSDavid Chinner 	 * that has been freed has it's buffer still pinned in memory because
1193641c56fbSDavid Chinner 	 * the transaction is still sitting in a iclog. The stale inodes
1194641c56fbSDavid Chinner 	 * on that buffer will have their flush locks held until the
1195641c56fbSDavid Chinner 	 * transaction hits the disk and the callbacks run. the inode
1196641c56fbSDavid Chinner 	 * flush takes the flush lock unconditionally and with nothing to
1197641c56fbSDavid Chinner 	 * push out the iclog we will never get that unlocked. hence we
1198641c56fbSDavid Chinner 	 * need to force the log first.
1199641c56fbSDavid Chinner 	 */
1200641c56fbSDavid Chinner 	xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1201efa80278SChristoph Hellwig 	xfs_iflush_all(mp);
12021da177e4SLinus Torvalds 
1203ee2a4f7cSNathan Scott 	XFS_QM_DQPURGEALL(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_UMOUNTING);
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds 	/*
12061da177e4SLinus Torvalds 	 * Flush out the log synchronously so that we know for sure
12071da177e4SLinus Torvalds 	 * that nothing is pinned.  This is important because bflush()
12081da177e4SLinus Torvalds 	 * will skip pinned buffers.
12091da177e4SLinus Torvalds 	 */
12101da177e4SLinus Torvalds 	xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 	xfs_binval(mp->m_ddev_targp);
12131da177e4SLinus Torvalds 	if (mp->m_rtdev_targp) {
12141da177e4SLinus Torvalds 		xfs_binval(mp->m_rtdev_targp);
12151da177e4SLinus Torvalds 	}
12161da177e4SLinus Torvalds 
121784e1e99fSDavid Chinner 	/*
121884e1e99fSDavid Chinner 	 * Unreserve any blocks we have so that when we unmount we don't account
121984e1e99fSDavid Chinner 	 * the reserved free space as used. This is really only necessary for
122084e1e99fSDavid Chinner 	 * lazy superblock counting because it trusts the incore superblock
122184e1e99fSDavid Chinner 	 * counters to be aboslutely correct on clean unmount.
122284e1e99fSDavid Chinner 	 *
122384e1e99fSDavid Chinner 	 * We don't bother correcting this elsewhere for lazy superblock
122484e1e99fSDavid Chinner 	 * counting because on mount of an unclean filesystem we reconstruct the
122584e1e99fSDavid Chinner 	 * correct counter value and this is irrelevant.
122684e1e99fSDavid Chinner 	 *
122784e1e99fSDavid Chinner 	 * For non-lazy counter filesystems, this doesn't matter at all because
122884e1e99fSDavid Chinner 	 * we only every apply deltas to the superblock and hence the incore
122984e1e99fSDavid Chinner 	 * value does not matter....
123084e1e99fSDavid Chinner 	 */
123184e1e99fSDavid Chinner 	resblks = 0;
123284e1e99fSDavid Chinner 	xfs_reserve_blocks(mp, &resblks, NULL);
123384e1e99fSDavid Chinner 
123492821e2bSDavid Chinner 	xfs_log_sbcount(mp, 1);
12351da177e4SLinus Torvalds 	xfs_unmountfs_writesb(mp);
12361da177e4SLinus Torvalds 	xfs_unmountfs_wait(mp); 		/* wait for async bufs */
12371da177e4SLinus Torvalds 	xfs_log_unmount(mp);			/* Done! No more fs ops. */
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 	xfs_freesb(mp);
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 	/*
12421da177e4SLinus Torvalds 	 * All inodes from this mount point should be freed.
12431da177e4SLinus Torvalds 	 */
12441da177e4SLinus Torvalds 	ASSERT(mp->m_inodes == NULL);
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 	xfs_unmountfs_close(mp, cr);
12471da177e4SLinus Torvalds 	if ((mp->m_flags & XFS_MOUNT_NOUUID) == 0)
12481da177e4SLinus Torvalds 		xfs_uuid_unmount(mp);
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds #if defined(DEBUG) || defined(INDUCE_IO_ERROR)
12511da177e4SLinus Torvalds 	/*
12521da177e4SLinus Torvalds 	 * clear all error tags on this filesystem
12531da177e4SLinus Torvalds 	 */
12541da177e4SLinus Torvalds 	memcpy(&fsid, &vfsp->vfs_fsid, sizeof(int64_t));
12551da177e4SLinus Torvalds 	xfs_errortag_clearall_umount(fsid, mp->m_fsname, 0);
12561da177e4SLinus Torvalds #endif
12571da177e4SLinus Torvalds 	XFS_IODONE(vfsp);
12581da177e4SLinus Torvalds 	xfs_mount_free(mp, 1);
12591da177e4SLinus Torvalds 	return 0;
12601da177e4SLinus Torvalds }
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds void
12631da177e4SLinus Torvalds xfs_unmountfs_close(xfs_mount_t *mp, struct cred *cr)
12641da177e4SLinus Torvalds {
1265*49ee6c91SJesper Juhl 	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
12661da177e4SLinus Torvalds 		xfs_free_buftarg(mp->m_logdev_targp, 1);
12671da177e4SLinus Torvalds 	if (mp->m_rtdev_targp)
12681da177e4SLinus Torvalds 		xfs_free_buftarg(mp->m_rtdev_targp, 1);
12691da177e4SLinus Torvalds 	xfs_free_buftarg(mp->m_ddev_targp, 0);
12701da177e4SLinus Torvalds }
12711da177e4SLinus Torvalds 
1272ba0f32d4SChristoph Hellwig STATIC void
12731da177e4SLinus Torvalds xfs_unmountfs_wait(xfs_mount_t *mp)
12741da177e4SLinus Torvalds {
12751da177e4SLinus Torvalds 	if (mp->m_logdev_targp != mp->m_ddev_targp)
12761da177e4SLinus Torvalds 		xfs_wait_buftarg(mp->m_logdev_targp);
12771da177e4SLinus Torvalds 	if (mp->m_rtdev_targp)
12781da177e4SLinus Torvalds 		xfs_wait_buftarg(mp->m_rtdev_targp);
12791da177e4SLinus Torvalds 	xfs_wait_buftarg(mp->m_ddev_targp);
12801da177e4SLinus Torvalds }
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds int
128392821e2bSDavid Chinner xfs_fs_writable(xfs_mount_t *mp)
128492821e2bSDavid Chinner {
128592821e2bSDavid Chinner 	bhv_vfs_t	*vfsp = XFS_MTOVFS(mp);
128692821e2bSDavid Chinner 
128792821e2bSDavid Chinner 	return !(vfs_test_for_freeze(vfsp) || XFS_FORCED_SHUTDOWN(mp) ||
128892821e2bSDavid Chinner 		(vfsp->vfs_flag & VFS_RDONLY));
128992821e2bSDavid Chinner }
129092821e2bSDavid Chinner 
129192821e2bSDavid Chinner /*
129292821e2bSDavid Chinner  * xfs_log_sbcount
129392821e2bSDavid Chinner  *
129492821e2bSDavid Chinner  * Called either periodically to keep the on disk superblock values
129592821e2bSDavid Chinner  * roughly up to date or from unmount to make sure the values are
129692821e2bSDavid Chinner  * correct on a clean unmount.
129792821e2bSDavid Chinner  *
129892821e2bSDavid Chinner  * Note this code can be called during the process of freezing, so
129992821e2bSDavid Chinner  * we may need to use the transaction allocator which does not not
130092821e2bSDavid Chinner  * block when the transaction subsystem is in its frozen state.
130192821e2bSDavid Chinner  */
130292821e2bSDavid Chinner int
130392821e2bSDavid Chinner xfs_log_sbcount(
130492821e2bSDavid Chinner 	xfs_mount_t	*mp,
130592821e2bSDavid Chinner 	uint		sync)
130692821e2bSDavid Chinner {
130792821e2bSDavid Chinner 	xfs_trans_t	*tp;
130892821e2bSDavid Chinner 	int		error;
130992821e2bSDavid Chinner 
131092821e2bSDavid Chinner 	if (!xfs_fs_writable(mp))
131192821e2bSDavid Chinner 		return 0;
131292821e2bSDavid Chinner 
131392821e2bSDavid Chinner 	xfs_icsb_sync_counters(mp);
131492821e2bSDavid Chinner 
131592821e2bSDavid Chinner 	/*
131692821e2bSDavid Chinner 	 * we don't need to do this if we are updating the superblock
131792821e2bSDavid Chinner 	 * counters on every modification.
131892821e2bSDavid Chinner 	 */
131992821e2bSDavid Chinner 	if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
132092821e2bSDavid Chinner 		return 0;
132192821e2bSDavid Chinner 
132292821e2bSDavid Chinner 	tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT);
132392821e2bSDavid Chinner 	error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
132492821e2bSDavid Chinner 					XFS_DEFAULT_LOG_COUNT);
132592821e2bSDavid Chinner 	if (error) {
132692821e2bSDavid Chinner 		xfs_trans_cancel(tp, 0);
132792821e2bSDavid Chinner 		return error;
132892821e2bSDavid Chinner 	}
132992821e2bSDavid Chinner 
133092821e2bSDavid Chinner 	xfs_mod_sb(tp, XFS_SB_IFREE | XFS_SB_ICOUNT | XFS_SB_FDBLOCKS);
133192821e2bSDavid Chinner 	if (sync)
133292821e2bSDavid Chinner 		xfs_trans_set_sync(tp);
133392821e2bSDavid Chinner 	xfs_trans_commit(tp, 0);
133492821e2bSDavid Chinner 
133592821e2bSDavid Chinner 	return 0;
133692821e2bSDavid Chinner }
133792821e2bSDavid Chinner 
133892821e2bSDavid Chinner int
13391da177e4SLinus Torvalds xfs_unmountfs_writesb(xfs_mount_t *mp)
13401da177e4SLinus Torvalds {
13411da177e4SLinus Torvalds 	xfs_buf_t	*sbp;
13421da177e4SLinus Torvalds 	xfs_sb_t	*sb;
13431da177e4SLinus Torvalds 	int		error = 0;
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds 	/*
13461da177e4SLinus Torvalds 	 * skip superblock write if fs is read-only, or
13471da177e4SLinus Torvalds 	 * if we are doing a forced umount.
13481da177e4SLinus Torvalds 	 */
13491da177e4SLinus Torvalds 	if (!(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY ||
13501da177e4SLinus Torvalds 		XFS_FORCED_SHUTDOWN(mp))) {
13518d280b98SDavid Chinner 
135292821e2bSDavid Chinner 		sbp = xfs_getsb(mp, 0);
135392821e2bSDavid Chinner  		sb = XFS_BUF_TO_SBP(sbp);
13548d280b98SDavid Chinner 
13551da177e4SLinus Torvalds 		/*
13561da177e4SLinus Torvalds 		 * mark shared-readonly if desired
13571da177e4SLinus Torvalds 		 */
13581da177e4SLinus Torvalds 		if (mp->m_mk_sharedro) {
13591da177e4SLinus Torvalds 			if (!(sb->sb_flags & XFS_SBF_READONLY))
13601da177e4SLinus Torvalds 				sb->sb_flags |= XFS_SBF_READONLY;
13611da177e4SLinus Torvalds 			if (!XFS_SB_VERSION_HASSHARED(sb))
13621da177e4SLinus Torvalds 				XFS_SB_VERSION_ADDSHARED(sb);
13631da177e4SLinus Torvalds 			xfs_fs_cmn_err(CE_NOTE, mp,
13641da177e4SLinus Torvalds 				"Unmounting, marking shared read-only");
13651da177e4SLinus Torvalds 		}
136692821e2bSDavid Chinner 
13671da177e4SLinus Torvalds 		XFS_BUF_UNDONE(sbp);
13681da177e4SLinus Torvalds 		XFS_BUF_UNREAD(sbp);
13691da177e4SLinus Torvalds 		XFS_BUF_UNDELAYWRITE(sbp);
13701da177e4SLinus Torvalds 		XFS_BUF_WRITE(sbp);
13711da177e4SLinus Torvalds 		XFS_BUF_UNASYNC(sbp);
13721da177e4SLinus Torvalds 		ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp);
13731da177e4SLinus Torvalds 		xfsbdstrat(mp, sbp);
13741da177e4SLinus Torvalds 		/* Nevermind errors we might get here. */
13751da177e4SLinus Torvalds 		error = xfs_iowait(sbp);
13761da177e4SLinus Torvalds 		if (error)
13771da177e4SLinus Torvalds 			xfs_ioerror_alert("xfs_unmountfs_writesb",
13781da177e4SLinus Torvalds 					  mp, sbp, XFS_BUF_ADDR(sbp));
13791da177e4SLinus Torvalds 		if (error && mp->m_mk_sharedro)
13801da177e4SLinus Torvalds 			xfs_fs_cmn_err(CE_ALERT, mp, "Superblock write error detected while unmounting.  Filesystem may not be marked shared readonly");
13811da177e4SLinus Torvalds 		xfs_buf_relse(sbp);
138292821e2bSDavid Chinner 	}
1383014c2544SJesper Juhl 	return error;
13841da177e4SLinus Torvalds }
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds /*
13871da177e4SLinus Torvalds  * xfs_mod_sb() can be used to copy arbitrary changes to the
13881da177e4SLinus Torvalds  * in-core superblock into the superblock buffer to be logged.
13891da177e4SLinus Torvalds  * It does not provide the higher level of locking that is
13901da177e4SLinus Torvalds  * needed to protect the in-core superblock from concurrent
13911da177e4SLinus Torvalds  * access.
13921da177e4SLinus Torvalds  */
13931da177e4SLinus Torvalds void
13941da177e4SLinus Torvalds xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
13951da177e4SLinus Torvalds {
13961da177e4SLinus Torvalds 	xfs_buf_t	*bp;
13971da177e4SLinus Torvalds 	int		first;
13981da177e4SLinus Torvalds 	int		last;
13991da177e4SLinus Torvalds 	xfs_mount_t	*mp;
14001da177e4SLinus Torvalds 	xfs_sb_t	*sbp;
14011da177e4SLinus Torvalds 	xfs_sb_field_t	f;
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 	ASSERT(fields);
14041da177e4SLinus Torvalds 	if (!fields)
14051da177e4SLinus Torvalds 		return;
14061da177e4SLinus Torvalds 	mp = tp->t_mountp;
14071da177e4SLinus Torvalds 	bp = xfs_trans_getsb(tp, mp, 0);
14081da177e4SLinus Torvalds 	sbp = XFS_BUF_TO_SBP(bp);
14091da177e4SLinus Torvalds 	first = sizeof(xfs_sb_t);
14101da177e4SLinus Torvalds 	last = 0;
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds 	/* translate/copy */
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds 	xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), -1, fields);
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds 	/* find modified range */
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds 	f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
14191da177e4SLinus Torvalds 	ASSERT((1LL << f) & XFS_SB_MOD_BITS);
14201da177e4SLinus Torvalds 	first = xfs_sb_info[f].offset;
14211da177e4SLinus Torvalds 
14221da177e4SLinus Torvalds 	f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
14231da177e4SLinus Torvalds 	ASSERT((1LL << f) & XFS_SB_MOD_BITS);
14241da177e4SLinus Torvalds 	last = xfs_sb_info[f + 1].offset - 1;
14251da177e4SLinus Torvalds 
14261da177e4SLinus Torvalds 	xfs_trans_log_buf(tp, bp, first, last);
14271da177e4SLinus Torvalds }
1428d210a28cSYingping Lu 
1429d210a28cSYingping Lu 
14301da177e4SLinus Torvalds /*
14311da177e4SLinus Torvalds  * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
14321da177e4SLinus Torvalds  * a delta to a specified field in the in-core superblock.  Simply
14331da177e4SLinus Torvalds  * switch on the field indicated and apply the delta to that field.
14341da177e4SLinus Torvalds  * Fields are not allowed to dip below zero, so if the delta would
14351da177e4SLinus Torvalds  * do this do not apply it and return EINVAL.
14361da177e4SLinus Torvalds  *
14371da177e4SLinus Torvalds  * The SB_LOCK must be held when this routine is called.
14381da177e4SLinus Torvalds  */
14398d280b98SDavid Chinner int
144020f4ebf2SDavid Chinner xfs_mod_incore_sb_unlocked(
144120f4ebf2SDavid Chinner 	xfs_mount_t	*mp,
144220f4ebf2SDavid Chinner 	xfs_sb_field_t	field,
144320f4ebf2SDavid Chinner 	int64_t		delta,
144420f4ebf2SDavid Chinner 	int		rsvd)
14451da177e4SLinus Torvalds {
14461da177e4SLinus Torvalds 	int		scounter;	/* short counter for 32 bit fields */
14471da177e4SLinus Torvalds 	long long	lcounter;	/* long counter for 64 bit fields */
14481da177e4SLinus Torvalds 	long long	res_used, rem;
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds 	/*
14511da177e4SLinus Torvalds 	 * With the in-core superblock spin lock held, switch
14521da177e4SLinus Torvalds 	 * on the indicated field.  Apply the delta to the
14531da177e4SLinus Torvalds 	 * proper field.  If the fields value would dip below
14541da177e4SLinus Torvalds 	 * 0, then do not apply the delta and return EINVAL.
14551da177e4SLinus Torvalds 	 */
14561da177e4SLinus Torvalds 	switch (field) {
14571da177e4SLinus Torvalds 	case XFS_SBS_ICOUNT:
14581da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_icount;
14591da177e4SLinus Torvalds 		lcounter += delta;
14601da177e4SLinus Torvalds 		if (lcounter < 0) {
14611da177e4SLinus Torvalds 			ASSERT(0);
1462014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
14631da177e4SLinus Torvalds 		}
14641da177e4SLinus Torvalds 		mp->m_sb.sb_icount = lcounter;
1465014c2544SJesper Juhl 		return 0;
14661da177e4SLinus Torvalds 	case XFS_SBS_IFREE:
14671da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_ifree;
14681da177e4SLinus Torvalds 		lcounter += delta;
14691da177e4SLinus Torvalds 		if (lcounter < 0) {
14701da177e4SLinus Torvalds 			ASSERT(0);
1471014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
14721da177e4SLinus Torvalds 		}
14731da177e4SLinus Torvalds 		mp->m_sb.sb_ifree = lcounter;
1474014c2544SJesper Juhl 		return 0;
14751da177e4SLinus Torvalds 	case XFS_SBS_FDBLOCKS:
14764be536deSDavid Chinner 		lcounter = (long long)
14774be536deSDavid Chinner 			mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
14781da177e4SLinus Torvalds 		res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds 		if (delta > 0) {		/* Putting blocks back */
14811da177e4SLinus Torvalds 			if (res_used > delta) {
14821da177e4SLinus Torvalds 				mp->m_resblks_avail += delta;
14831da177e4SLinus Torvalds 			} else {
14841da177e4SLinus Torvalds 				rem = delta - res_used;
14851da177e4SLinus Torvalds 				mp->m_resblks_avail = mp->m_resblks;
14861da177e4SLinus Torvalds 				lcounter += rem;
14871da177e4SLinus Torvalds 			}
14881da177e4SLinus Torvalds 		} else {				/* Taking blocks away */
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds 			lcounter += delta;
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds 		/*
14931da177e4SLinus Torvalds 		 * If were out of blocks, use any available reserved blocks if
14941da177e4SLinus Torvalds 		 * were allowed to.
14951da177e4SLinus Torvalds 		 */
14961da177e4SLinus Torvalds 
14971da177e4SLinus Torvalds 			if (lcounter < 0) {
14981da177e4SLinus Torvalds 				if (rsvd) {
14991da177e4SLinus Torvalds 					lcounter = (long long)mp->m_resblks_avail + delta;
15001da177e4SLinus Torvalds 					if (lcounter < 0) {
1501014c2544SJesper Juhl 						return XFS_ERROR(ENOSPC);
15021da177e4SLinus Torvalds 					}
15031da177e4SLinus Torvalds 					mp->m_resblks_avail = lcounter;
1504014c2544SJesper Juhl 					return 0;
15051da177e4SLinus Torvalds 				} else {	/* not reserved */
1506014c2544SJesper Juhl 					return XFS_ERROR(ENOSPC);
15071da177e4SLinus Torvalds 				}
15081da177e4SLinus Torvalds 			}
15091da177e4SLinus Torvalds 		}
15101da177e4SLinus Torvalds 
15114be536deSDavid Chinner 		mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
1512014c2544SJesper Juhl 		return 0;
15131da177e4SLinus Torvalds 	case XFS_SBS_FREXTENTS:
15141da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_frextents;
15151da177e4SLinus Torvalds 		lcounter += delta;
15161da177e4SLinus Torvalds 		if (lcounter < 0) {
1517014c2544SJesper Juhl 			return XFS_ERROR(ENOSPC);
15181da177e4SLinus Torvalds 		}
15191da177e4SLinus Torvalds 		mp->m_sb.sb_frextents = lcounter;
1520014c2544SJesper Juhl 		return 0;
15211da177e4SLinus Torvalds 	case XFS_SBS_DBLOCKS:
15221da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_dblocks;
15231da177e4SLinus Torvalds 		lcounter += delta;
15241da177e4SLinus Torvalds 		if (lcounter < 0) {
15251da177e4SLinus Torvalds 			ASSERT(0);
1526014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
15271da177e4SLinus Torvalds 		}
15281da177e4SLinus Torvalds 		mp->m_sb.sb_dblocks = lcounter;
1529014c2544SJesper Juhl 		return 0;
15301da177e4SLinus Torvalds 	case XFS_SBS_AGCOUNT:
15311da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_agcount;
15321da177e4SLinus Torvalds 		scounter += delta;
15331da177e4SLinus Torvalds 		if (scounter < 0) {
15341da177e4SLinus Torvalds 			ASSERT(0);
1535014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
15361da177e4SLinus Torvalds 		}
15371da177e4SLinus Torvalds 		mp->m_sb.sb_agcount = scounter;
1538014c2544SJesper Juhl 		return 0;
15391da177e4SLinus Torvalds 	case XFS_SBS_IMAX_PCT:
15401da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_imax_pct;
15411da177e4SLinus Torvalds 		scounter += delta;
15421da177e4SLinus Torvalds 		if (scounter < 0) {
15431da177e4SLinus Torvalds 			ASSERT(0);
1544014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
15451da177e4SLinus Torvalds 		}
15461da177e4SLinus Torvalds 		mp->m_sb.sb_imax_pct = scounter;
1547014c2544SJesper Juhl 		return 0;
15481da177e4SLinus Torvalds 	case XFS_SBS_REXTSIZE:
15491da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rextsize;
15501da177e4SLinus Torvalds 		scounter += delta;
15511da177e4SLinus Torvalds 		if (scounter < 0) {
15521da177e4SLinus Torvalds 			ASSERT(0);
1553014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
15541da177e4SLinus Torvalds 		}
15551da177e4SLinus Torvalds 		mp->m_sb.sb_rextsize = scounter;
1556014c2544SJesper Juhl 		return 0;
15571da177e4SLinus Torvalds 	case XFS_SBS_RBMBLOCKS:
15581da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rbmblocks;
15591da177e4SLinus Torvalds 		scounter += delta;
15601da177e4SLinus Torvalds 		if (scounter < 0) {
15611da177e4SLinus Torvalds 			ASSERT(0);
1562014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
15631da177e4SLinus Torvalds 		}
15641da177e4SLinus Torvalds 		mp->m_sb.sb_rbmblocks = scounter;
1565014c2544SJesper Juhl 		return 0;
15661da177e4SLinus Torvalds 	case XFS_SBS_RBLOCKS:
15671da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_rblocks;
15681da177e4SLinus Torvalds 		lcounter += delta;
15691da177e4SLinus Torvalds 		if (lcounter < 0) {
15701da177e4SLinus Torvalds 			ASSERT(0);
1571014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
15721da177e4SLinus Torvalds 		}
15731da177e4SLinus Torvalds 		mp->m_sb.sb_rblocks = lcounter;
1574014c2544SJesper Juhl 		return 0;
15751da177e4SLinus Torvalds 	case XFS_SBS_REXTENTS:
15761da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_rextents;
15771da177e4SLinus Torvalds 		lcounter += delta;
15781da177e4SLinus Torvalds 		if (lcounter < 0) {
15791da177e4SLinus Torvalds 			ASSERT(0);
1580014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
15811da177e4SLinus Torvalds 		}
15821da177e4SLinus Torvalds 		mp->m_sb.sb_rextents = lcounter;
1583014c2544SJesper Juhl 		return 0;
15841da177e4SLinus Torvalds 	case XFS_SBS_REXTSLOG:
15851da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rextslog;
15861da177e4SLinus Torvalds 		scounter += delta;
15871da177e4SLinus Torvalds 		if (scounter < 0) {
15881da177e4SLinus Torvalds 			ASSERT(0);
1589014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
15901da177e4SLinus Torvalds 		}
15911da177e4SLinus Torvalds 		mp->m_sb.sb_rextslog = scounter;
1592014c2544SJesper Juhl 		return 0;
15931da177e4SLinus Torvalds 	default:
15941da177e4SLinus Torvalds 		ASSERT(0);
1595014c2544SJesper Juhl 		return XFS_ERROR(EINVAL);
15961da177e4SLinus Torvalds 	}
15971da177e4SLinus Torvalds }
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds /*
16001da177e4SLinus Torvalds  * xfs_mod_incore_sb() is used to change a field in the in-core
16011da177e4SLinus Torvalds  * superblock structure by the specified delta.  This modification
16021da177e4SLinus Torvalds  * is protected by the SB_LOCK.  Just use the xfs_mod_incore_sb_unlocked()
16031da177e4SLinus Torvalds  * routine to do the work.
16041da177e4SLinus Torvalds  */
16051da177e4SLinus Torvalds int
160620f4ebf2SDavid Chinner xfs_mod_incore_sb(
160720f4ebf2SDavid Chinner 	xfs_mount_t	*mp,
160820f4ebf2SDavid Chinner 	xfs_sb_field_t	field,
160920f4ebf2SDavid Chinner 	int64_t		delta,
161020f4ebf2SDavid Chinner 	int		rsvd)
16111da177e4SLinus Torvalds {
16121da177e4SLinus Torvalds 	unsigned long	s;
16131da177e4SLinus Torvalds 	int	status;
16141da177e4SLinus Torvalds 
16158d280b98SDavid Chinner 	/* check for per-cpu counters */
16168d280b98SDavid Chinner 	switch (field) {
16178d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
16188d280b98SDavid Chinner 	case XFS_SBS_ICOUNT:
16198d280b98SDavid Chinner 	case XFS_SBS_IFREE:
16208d280b98SDavid Chinner 	case XFS_SBS_FDBLOCKS:
16218d280b98SDavid Chinner 		if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
16228d280b98SDavid Chinner 			status = xfs_icsb_modify_counters(mp, field,
16238d280b98SDavid Chinner 							delta, rsvd);
16248d280b98SDavid Chinner 			break;
16258d280b98SDavid Chinner 		}
16268d280b98SDavid Chinner 		/* FALLTHROUGH */
16278d280b98SDavid Chinner #endif
16288d280b98SDavid Chinner 	default:
16291da177e4SLinus Torvalds 		s = XFS_SB_LOCK(mp);
16301da177e4SLinus Torvalds 		status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
16311da177e4SLinus Torvalds 		XFS_SB_UNLOCK(mp, s);
16328d280b98SDavid Chinner 		break;
16338d280b98SDavid Chinner 	}
16348d280b98SDavid Chinner 
1635014c2544SJesper Juhl 	return status;
16361da177e4SLinus Torvalds }
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds /*
16391da177e4SLinus Torvalds  * xfs_mod_incore_sb_batch() is used to change more than one field
16401da177e4SLinus Torvalds  * in the in-core superblock structure at a time.  This modification
16411da177e4SLinus Torvalds  * is protected by a lock internal to this module.  The fields and
16421da177e4SLinus Torvalds  * changes to those fields are specified in the array of xfs_mod_sb
16431da177e4SLinus Torvalds  * structures passed in.
16441da177e4SLinus Torvalds  *
16451da177e4SLinus Torvalds  * Either all of the specified deltas will be applied or none of
16461da177e4SLinus Torvalds  * them will.  If any modified field dips below 0, then all modifications
16471da177e4SLinus Torvalds  * will be backed out and EINVAL will be returned.
16481da177e4SLinus Torvalds  */
16491da177e4SLinus Torvalds int
16501da177e4SLinus Torvalds xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
16511da177e4SLinus Torvalds {
16521da177e4SLinus Torvalds 	unsigned long	s;
16531da177e4SLinus Torvalds 	int		status=0;
16541da177e4SLinus Torvalds 	xfs_mod_sb_t	*msbp;
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds 	/*
16571da177e4SLinus Torvalds 	 * Loop through the array of mod structures and apply each
16581da177e4SLinus Torvalds 	 * individually.  If any fail, then back out all those
16591da177e4SLinus Torvalds 	 * which have already been applied.  Do all of this within
16601da177e4SLinus Torvalds 	 * the scope of the SB_LOCK so that all of the changes will
16611da177e4SLinus Torvalds 	 * be atomic.
16621da177e4SLinus Torvalds 	 */
16631da177e4SLinus Torvalds 	s = XFS_SB_LOCK(mp);
16641da177e4SLinus Torvalds 	msbp = &msb[0];
16651da177e4SLinus Torvalds 	for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) {
16661da177e4SLinus Torvalds 		/*
16671da177e4SLinus Torvalds 		 * Apply the delta at index n.  If it fails, break
16681da177e4SLinus Torvalds 		 * from the loop so we'll fall into the undo loop
16691da177e4SLinus Torvalds 		 * below.
16701da177e4SLinus Torvalds 		 */
16718d280b98SDavid Chinner 		switch (msbp->msb_field) {
16728d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
16738d280b98SDavid Chinner 		case XFS_SBS_ICOUNT:
16748d280b98SDavid Chinner 		case XFS_SBS_IFREE:
16758d280b98SDavid Chinner 		case XFS_SBS_FDBLOCKS:
16768d280b98SDavid Chinner 			if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
167720b64285SDavid Chinner 				XFS_SB_UNLOCK(mp, s);
167820b64285SDavid Chinner 				status = xfs_icsb_modify_counters(mp,
16798d280b98SDavid Chinner 							msbp->msb_field,
16801da177e4SLinus Torvalds 							msbp->msb_delta, rsvd);
168120b64285SDavid Chinner 				s = XFS_SB_LOCK(mp);
16828d280b98SDavid Chinner 				break;
16838d280b98SDavid Chinner 			}
16848d280b98SDavid Chinner 			/* FALLTHROUGH */
16858d280b98SDavid Chinner #endif
16868d280b98SDavid Chinner 		default:
16878d280b98SDavid Chinner 			status = xfs_mod_incore_sb_unlocked(mp,
16888d280b98SDavid Chinner 						msbp->msb_field,
16898d280b98SDavid Chinner 						msbp->msb_delta, rsvd);
16908d280b98SDavid Chinner 			break;
16918d280b98SDavid Chinner 		}
16928d280b98SDavid Chinner 
16931da177e4SLinus Torvalds 		if (status != 0) {
16941da177e4SLinus Torvalds 			break;
16951da177e4SLinus Torvalds 		}
16961da177e4SLinus Torvalds 	}
16971da177e4SLinus Torvalds 
16981da177e4SLinus Torvalds 	/*
16991da177e4SLinus Torvalds 	 * If we didn't complete the loop above, then back out
17001da177e4SLinus Torvalds 	 * any changes made to the superblock.  If you add code
17011da177e4SLinus Torvalds 	 * between the loop above and here, make sure that you
17021da177e4SLinus Torvalds 	 * preserve the value of status. Loop back until
17031da177e4SLinus Torvalds 	 * we step below the beginning of the array.  Make sure
17041da177e4SLinus Torvalds 	 * we don't touch anything back there.
17051da177e4SLinus Torvalds 	 */
17061da177e4SLinus Torvalds 	if (status != 0) {
17071da177e4SLinus Torvalds 		msbp--;
17081da177e4SLinus Torvalds 		while (msbp >= msb) {
17098d280b98SDavid Chinner 			switch (msbp->msb_field) {
17108d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
17118d280b98SDavid Chinner 			case XFS_SBS_ICOUNT:
17128d280b98SDavid Chinner 			case XFS_SBS_IFREE:
17138d280b98SDavid Chinner 			case XFS_SBS_FDBLOCKS:
17148d280b98SDavid Chinner 				if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
171520b64285SDavid Chinner 					XFS_SB_UNLOCK(mp, s);
171620b64285SDavid Chinner 					status = xfs_icsb_modify_counters(mp,
17178d280b98SDavid Chinner 							msbp->msb_field,
17188d280b98SDavid Chinner 							-(msbp->msb_delta),
17198d280b98SDavid Chinner 							rsvd);
172020b64285SDavid Chinner 					s = XFS_SB_LOCK(mp);
17218d280b98SDavid Chinner 					break;
17228d280b98SDavid Chinner 				}
17238d280b98SDavid Chinner 				/* FALLTHROUGH */
17248d280b98SDavid Chinner #endif
17258d280b98SDavid Chinner 			default:
17261da177e4SLinus Torvalds 				status = xfs_mod_incore_sb_unlocked(mp,
17278d280b98SDavid Chinner 							msbp->msb_field,
17288d280b98SDavid Chinner 							-(msbp->msb_delta),
17298d280b98SDavid Chinner 							rsvd);
17308d280b98SDavid Chinner 				break;
17318d280b98SDavid Chinner 			}
17321da177e4SLinus Torvalds 			ASSERT(status == 0);
17331da177e4SLinus Torvalds 			msbp--;
17341da177e4SLinus Torvalds 		}
17351da177e4SLinus Torvalds 	}
17361da177e4SLinus Torvalds 	XFS_SB_UNLOCK(mp, s);
1737014c2544SJesper Juhl 	return status;
17381da177e4SLinus Torvalds }
17391da177e4SLinus Torvalds 
17401da177e4SLinus Torvalds /*
17411da177e4SLinus Torvalds  * xfs_getsb() is called to obtain the buffer for the superblock.
17421da177e4SLinus Torvalds  * The buffer is returned locked and read in from disk.
17431da177e4SLinus Torvalds  * The buffer should be released with a call to xfs_brelse().
17441da177e4SLinus Torvalds  *
17451da177e4SLinus Torvalds  * If the flags parameter is BUF_TRYLOCK, then we'll only return
17461da177e4SLinus Torvalds  * the superblock buffer if it can be locked without sleeping.
17471da177e4SLinus Torvalds  * If it can't then we'll return NULL.
17481da177e4SLinus Torvalds  */
17491da177e4SLinus Torvalds xfs_buf_t *
17501da177e4SLinus Torvalds xfs_getsb(
17511da177e4SLinus Torvalds 	xfs_mount_t	*mp,
17521da177e4SLinus Torvalds 	int		flags)
17531da177e4SLinus Torvalds {
17541da177e4SLinus Torvalds 	xfs_buf_t	*bp;
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 	ASSERT(mp->m_sb_bp != NULL);
17571da177e4SLinus Torvalds 	bp = mp->m_sb_bp;
17581da177e4SLinus Torvalds 	if (flags & XFS_BUF_TRYLOCK) {
17591da177e4SLinus Torvalds 		if (!XFS_BUF_CPSEMA(bp)) {
17601da177e4SLinus Torvalds 			return NULL;
17611da177e4SLinus Torvalds 		}
17621da177e4SLinus Torvalds 	} else {
17631da177e4SLinus Torvalds 		XFS_BUF_PSEMA(bp, PRIBIO);
17641da177e4SLinus Torvalds 	}
17651da177e4SLinus Torvalds 	XFS_BUF_HOLD(bp);
17661da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISDONE(bp));
1767014c2544SJesper Juhl 	return bp;
17681da177e4SLinus Torvalds }
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds /*
17711da177e4SLinus Torvalds  * Used to free the superblock along various error paths.
17721da177e4SLinus Torvalds  */
17731da177e4SLinus Torvalds void
17741da177e4SLinus Torvalds xfs_freesb(
17751da177e4SLinus Torvalds 	xfs_mount_t	*mp)
17761da177e4SLinus Torvalds {
17771da177e4SLinus Torvalds 	xfs_buf_t	*bp;
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds 	/*
17801da177e4SLinus Torvalds 	 * Use xfs_getsb() so that the buffer will be locked
17811da177e4SLinus Torvalds 	 * when we call xfs_buf_relse().
17821da177e4SLinus Torvalds 	 */
17831da177e4SLinus Torvalds 	bp = xfs_getsb(mp, 0);
17841da177e4SLinus Torvalds 	XFS_BUF_UNMANAGE(bp);
17851da177e4SLinus Torvalds 	xfs_buf_relse(bp);
17861da177e4SLinus Torvalds 	mp->m_sb_bp = NULL;
17871da177e4SLinus Torvalds }
17881da177e4SLinus Torvalds 
17891da177e4SLinus Torvalds /*
17901da177e4SLinus Torvalds  * See if the UUID is unique among mounted XFS filesystems.
17911da177e4SLinus Torvalds  * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
17921da177e4SLinus Torvalds  */
17931da177e4SLinus Torvalds STATIC int
17941da177e4SLinus Torvalds xfs_uuid_mount(
17951da177e4SLinus Torvalds 	xfs_mount_t	*mp)
17961da177e4SLinus Torvalds {
17971da177e4SLinus Torvalds 	if (uuid_is_nil(&mp->m_sb.sb_uuid)) {
17981da177e4SLinus Torvalds 		cmn_err(CE_WARN,
17991da177e4SLinus Torvalds 			"XFS: Filesystem %s has nil UUID - can't mount",
18001da177e4SLinus Torvalds 			mp->m_fsname);
18011da177e4SLinus Torvalds 		return -1;
18021da177e4SLinus Torvalds 	}
18031da177e4SLinus Torvalds 	if (!uuid_table_insert(&mp->m_sb.sb_uuid)) {
18041da177e4SLinus Torvalds 		cmn_err(CE_WARN,
18051da177e4SLinus Torvalds 			"XFS: Filesystem %s has duplicate UUID - can't mount",
18061da177e4SLinus Torvalds 			mp->m_fsname);
18071da177e4SLinus Torvalds 		return -1;
18081da177e4SLinus Torvalds 	}
18091da177e4SLinus Torvalds 	return 0;
18101da177e4SLinus Torvalds }
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds /*
18131da177e4SLinus Torvalds  * Remove filesystem from the UUID table.
18141da177e4SLinus Torvalds  */
18151da177e4SLinus Torvalds STATIC void
18161da177e4SLinus Torvalds xfs_uuid_unmount(
18171da177e4SLinus Torvalds 	xfs_mount_t	*mp)
18181da177e4SLinus Torvalds {
18191da177e4SLinus Torvalds 	uuid_table_remove(&mp->m_sb.sb_uuid);
18201da177e4SLinus Torvalds }
18211da177e4SLinus Torvalds 
18221da177e4SLinus Torvalds /*
18231da177e4SLinus Torvalds  * Used to log changes to the superblock unit and width fields which could
18241da177e4SLinus Torvalds  * be altered by the mount options. Only the first superblock is updated.
18251da177e4SLinus Torvalds  */
18261da177e4SLinus Torvalds STATIC void
18271da177e4SLinus Torvalds xfs_mount_log_sbunit(
18281da177e4SLinus Torvalds 	xfs_mount_t	*mp,
18291da177e4SLinus Torvalds 	__int64_t	fields)
18301da177e4SLinus Torvalds {
18311da177e4SLinus Torvalds 	xfs_trans_t	*tp;
18321da177e4SLinus Torvalds 
18331da177e4SLinus Torvalds 	ASSERT(fields & (XFS_SB_UNIT|XFS_SB_WIDTH|XFS_SB_UUID));
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds 	tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
18361da177e4SLinus Torvalds 	if (xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
18371da177e4SLinus Torvalds 				XFS_DEFAULT_LOG_COUNT)) {
18381da177e4SLinus Torvalds 		xfs_trans_cancel(tp, 0);
18391da177e4SLinus Torvalds 		return;
18401da177e4SLinus Torvalds 	}
18411da177e4SLinus Torvalds 	xfs_mod_sb(tp, fields);
18421c72bf90SEric Sandeen 	xfs_trans_commit(tp, 0);
18431da177e4SLinus Torvalds }
18448d280b98SDavid Chinner 
18458d280b98SDavid Chinner 
18468d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
18478d280b98SDavid Chinner /*
18488d280b98SDavid Chinner  * Per-cpu incore superblock counters
18498d280b98SDavid Chinner  *
18508d280b98SDavid Chinner  * Simple concept, difficult implementation
18518d280b98SDavid Chinner  *
18528d280b98SDavid Chinner  * Basically, replace the incore superblock counters with a distributed per cpu
18538d280b98SDavid Chinner  * counter for contended fields (e.g.  free block count).
18548d280b98SDavid Chinner  *
18558d280b98SDavid Chinner  * Difficulties arise in that the incore sb is used for ENOSPC checking, and
18568d280b98SDavid Chinner  * hence needs to be accurately read when we are running low on space. Hence
18578d280b98SDavid Chinner  * there is a method to enable and disable the per-cpu counters based on how
18588d280b98SDavid Chinner  * much "stuff" is available in them.
18598d280b98SDavid Chinner  *
18608d280b98SDavid Chinner  * Basically, a counter is enabled if there is enough free resource to justify
18618d280b98SDavid Chinner  * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
18628d280b98SDavid Chinner  * ENOSPC), then we disable the counters to synchronise all callers and
18638d280b98SDavid Chinner  * re-distribute the available resources.
18648d280b98SDavid Chinner  *
18658d280b98SDavid Chinner  * If, once we redistributed the available resources, we still get a failure,
18668d280b98SDavid Chinner  * we disable the per-cpu counter and go through the slow path.
18678d280b98SDavid Chinner  *
18688d280b98SDavid Chinner  * The slow path is the current xfs_mod_incore_sb() function.  This means that
18698d280b98SDavid Chinner  * when we disable a per-cpu counter, we need to drain it's resources back to
18708d280b98SDavid Chinner  * the global superblock. We do this after disabling the counter to prevent
18718d280b98SDavid Chinner  * more threads from queueing up on the counter.
18728d280b98SDavid Chinner  *
18738d280b98SDavid Chinner  * Essentially, this means that we still need a lock in the fast path to enable
18748d280b98SDavid Chinner  * synchronisation between the global counters and the per-cpu counters. This
18758d280b98SDavid Chinner  * is not a problem because the lock will be local to a CPU almost all the time
18768d280b98SDavid Chinner  * and have little contention except when we get to ENOSPC conditions.
18778d280b98SDavid Chinner  *
18788d280b98SDavid Chinner  * Basically, this lock becomes a barrier that enables us to lock out the fast
18798d280b98SDavid Chinner  * path while we do things like enabling and disabling counters and
18808d280b98SDavid Chinner  * synchronising the counters.
18818d280b98SDavid Chinner  *
18828d280b98SDavid Chinner  * Locking rules:
18838d280b98SDavid Chinner  *
18848d280b98SDavid Chinner  * 	1. XFS_SB_LOCK() before picking up per-cpu locks
18858d280b98SDavid Chinner  * 	2. per-cpu locks always picked up via for_each_online_cpu() order
18868d280b98SDavid Chinner  * 	3. accurate counter sync requires XFS_SB_LOCK + per cpu locks
18878d280b98SDavid Chinner  * 	4. modifying per-cpu counters requires holding per-cpu lock
18888d280b98SDavid Chinner  * 	5. modifying global counters requires holding XFS_SB_LOCK
18898d280b98SDavid Chinner  *	6. enabling or disabling a counter requires holding the XFS_SB_LOCK
18908d280b98SDavid Chinner  *	   and _none_ of the per-cpu locks.
18918d280b98SDavid Chinner  *
18928d280b98SDavid Chinner  * Disabled counters are only ever re-enabled by a balance operation
18938d280b98SDavid Chinner  * that results in more free resources per CPU than a given threshold.
18948d280b98SDavid Chinner  * To ensure counters don't remain disabled, they are rebalanced when
18958d280b98SDavid Chinner  * the global resource goes above a higher threshold (i.e. some hysteresis
18968d280b98SDavid Chinner  * is present to prevent thrashing).
18978d280b98SDavid Chinner  */
1898e8234a68SDavid Chinner 
18995a67e4c5SChandra Seetharaman #ifdef CONFIG_HOTPLUG_CPU
1900e8234a68SDavid Chinner /*
1901e8234a68SDavid Chinner  * hot-plug CPU notifier support.
1902e8234a68SDavid Chinner  *
19035a67e4c5SChandra Seetharaman  * We need a notifier per filesystem as we need to be able to identify
19045a67e4c5SChandra Seetharaman  * the filesystem to balance the counters out. This is achieved by
19055a67e4c5SChandra Seetharaman  * having a notifier block embedded in the xfs_mount_t and doing pointer
19065a67e4c5SChandra Seetharaman  * magic to get the mount pointer from the notifier block address.
1907e8234a68SDavid Chinner  */
1908e8234a68SDavid Chinner STATIC int
1909e8234a68SDavid Chinner xfs_icsb_cpu_notify(
1910e8234a68SDavid Chinner 	struct notifier_block *nfb,
1911e8234a68SDavid Chinner 	unsigned long action,
1912e8234a68SDavid Chinner 	void *hcpu)
1913e8234a68SDavid Chinner {
1914e8234a68SDavid Chinner 	xfs_icsb_cnts_t *cntp;
1915e8234a68SDavid Chinner 	xfs_mount_t	*mp;
1916e8234a68SDavid Chinner 	int		s;
1917e8234a68SDavid Chinner 
1918e8234a68SDavid Chinner 	mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
1919e8234a68SDavid Chinner 	cntp = (xfs_icsb_cnts_t *)
1920e8234a68SDavid Chinner 			per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
1921e8234a68SDavid Chinner 	switch (action) {
1922e8234a68SDavid Chinner 	case CPU_UP_PREPARE:
19238bb78442SRafael J. Wysocki 	case CPU_UP_PREPARE_FROZEN:
1924e8234a68SDavid Chinner 		/* Easy Case - initialize the area and locks, and
1925e8234a68SDavid Chinner 		 * then rebalance when online does everything else for us. */
192601e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
1927e8234a68SDavid Chinner 		break;
1928e8234a68SDavid Chinner 	case CPU_ONLINE:
19298bb78442SRafael J. Wysocki 	case CPU_ONLINE_FROZEN:
193003135cf7SDavid Chinner 		xfs_icsb_lock(mp);
193120b64285SDavid Chinner 		xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0, 0);
193220b64285SDavid Chinner 		xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0, 0);
193320b64285SDavid Chinner 		xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0, 0);
193403135cf7SDavid Chinner 		xfs_icsb_unlock(mp);
1935e8234a68SDavid Chinner 		break;
1936e8234a68SDavid Chinner 	case CPU_DEAD:
19378bb78442SRafael J. Wysocki 	case CPU_DEAD_FROZEN:
1938e8234a68SDavid Chinner 		/* Disable all the counters, then fold the dead cpu's
1939e8234a68SDavid Chinner 		 * count into the total on the global superblock and
1940e8234a68SDavid Chinner 		 * re-enable the counters. */
194103135cf7SDavid Chinner 		xfs_icsb_lock(mp);
1942e8234a68SDavid Chinner 		s = XFS_SB_LOCK(mp);
1943e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
1944e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
1945e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
1946e8234a68SDavid Chinner 
1947e8234a68SDavid Chinner 		mp->m_sb.sb_icount += cntp->icsb_icount;
1948e8234a68SDavid Chinner 		mp->m_sb.sb_ifree += cntp->icsb_ifree;
1949e8234a68SDavid Chinner 		mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
1950e8234a68SDavid Chinner 
195101e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
1952e8234a68SDavid Chinner 
195320b64285SDavid Chinner 		xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT,
195420b64285SDavid Chinner 					 XFS_ICSB_SB_LOCKED, 0);
195520b64285SDavid Chinner 		xfs_icsb_balance_counter(mp, XFS_SBS_IFREE,
195620b64285SDavid Chinner 					 XFS_ICSB_SB_LOCKED, 0);
195720b64285SDavid Chinner 		xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS,
195820b64285SDavid Chinner 					 XFS_ICSB_SB_LOCKED, 0);
1959e8234a68SDavid Chinner 		XFS_SB_UNLOCK(mp, s);
196003135cf7SDavid Chinner 		xfs_icsb_unlock(mp);
1961e8234a68SDavid Chinner 		break;
1962e8234a68SDavid Chinner 	}
1963e8234a68SDavid Chinner 
1964e8234a68SDavid Chinner 	return NOTIFY_OK;
1965e8234a68SDavid Chinner }
19665a67e4c5SChandra Seetharaman #endif /* CONFIG_HOTPLUG_CPU */
1967e8234a68SDavid Chinner 
19688d280b98SDavid Chinner int
19698d280b98SDavid Chinner xfs_icsb_init_counters(
19708d280b98SDavid Chinner 	xfs_mount_t	*mp)
19718d280b98SDavid Chinner {
19728d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
19738d280b98SDavid Chinner 	int		i;
19748d280b98SDavid Chinner 
19758d280b98SDavid Chinner 	mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
19768d280b98SDavid Chinner 	if (mp->m_sb_cnts == NULL)
19778d280b98SDavid Chinner 		return -ENOMEM;
19788d280b98SDavid Chinner 
19795a67e4c5SChandra Seetharaman #ifdef CONFIG_HOTPLUG_CPU
1980e8234a68SDavid Chinner 	mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
1981e8234a68SDavid Chinner 	mp->m_icsb_notifier.priority = 0;
19825a67e4c5SChandra Seetharaman 	register_hotcpu_notifier(&mp->m_icsb_notifier);
19835a67e4c5SChandra Seetharaman #endif /* CONFIG_HOTPLUG_CPU */
1984e8234a68SDavid Chinner 
19858d280b98SDavid Chinner 	for_each_online_cpu(i) {
19868d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
198701e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
19888d280b98SDavid Chinner 	}
198920b64285SDavid Chinner 
199020b64285SDavid Chinner 	mutex_init(&mp->m_icsb_mutex);
199120b64285SDavid Chinner 
19928d280b98SDavid Chinner 	/*
19938d280b98SDavid Chinner 	 * start with all counters disabled so that the
19948d280b98SDavid Chinner 	 * initial balance kicks us off correctly
19958d280b98SDavid Chinner 	 */
19968d280b98SDavid Chinner 	mp->m_icsb_counters = -1;
19978d280b98SDavid Chinner 	return 0;
19988d280b98SDavid Chinner }
19998d280b98SDavid Chinner 
20005478eeadSLachlan McIlroy void
20015478eeadSLachlan McIlroy xfs_icsb_reinit_counters(
20025478eeadSLachlan McIlroy 	xfs_mount_t	*mp)
20035478eeadSLachlan McIlroy {
20045478eeadSLachlan McIlroy 	xfs_icsb_lock(mp);
20055478eeadSLachlan McIlroy 	/*
20065478eeadSLachlan McIlroy 	 * start with all counters disabled so that the
20075478eeadSLachlan McIlroy 	 * initial balance kicks us off correctly
20085478eeadSLachlan McIlroy 	 */
20095478eeadSLachlan McIlroy 	mp->m_icsb_counters = -1;
20105478eeadSLachlan McIlroy 	xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0, 0);
20115478eeadSLachlan McIlroy 	xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0, 0);
20125478eeadSLachlan McIlroy 	xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0, 0);
20135478eeadSLachlan McIlroy 	xfs_icsb_unlock(mp);
20145478eeadSLachlan McIlroy }
20155478eeadSLachlan McIlroy 
20168d280b98SDavid Chinner STATIC void
20178d280b98SDavid Chinner xfs_icsb_destroy_counters(
20188d280b98SDavid Chinner 	xfs_mount_t	*mp)
20198d280b98SDavid Chinner {
2020e8234a68SDavid Chinner 	if (mp->m_sb_cnts) {
20215a67e4c5SChandra Seetharaman 		unregister_hotcpu_notifier(&mp->m_icsb_notifier);
20228d280b98SDavid Chinner 		free_percpu(mp->m_sb_cnts);
20238d280b98SDavid Chinner 	}
202403135cf7SDavid Chinner 	mutex_destroy(&mp->m_icsb_mutex);
2025e8234a68SDavid Chinner }
20268d280b98SDavid Chinner 
20277989cb8eSDavid Chinner STATIC_INLINE void
202801e1b69cSDavid Chinner xfs_icsb_lock_cntr(
202901e1b69cSDavid Chinner 	xfs_icsb_cnts_t	*icsbp)
203001e1b69cSDavid Chinner {
203101e1b69cSDavid Chinner 	while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
203201e1b69cSDavid Chinner 		ndelay(1000);
203301e1b69cSDavid Chinner 	}
203401e1b69cSDavid Chinner }
203501e1b69cSDavid Chinner 
20367989cb8eSDavid Chinner STATIC_INLINE void
203701e1b69cSDavid Chinner xfs_icsb_unlock_cntr(
203801e1b69cSDavid Chinner 	xfs_icsb_cnts_t	*icsbp)
203901e1b69cSDavid Chinner {
204001e1b69cSDavid Chinner 	clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
204101e1b69cSDavid Chinner }
204201e1b69cSDavid Chinner 
20438d280b98SDavid Chinner 
20447989cb8eSDavid Chinner STATIC_INLINE void
20458d280b98SDavid Chinner xfs_icsb_lock_all_counters(
20468d280b98SDavid Chinner 	xfs_mount_t	*mp)
20478d280b98SDavid Chinner {
20488d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
20498d280b98SDavid Chinner 	int		i;
20508d280b98SDavid Chinner 
20518d280b98SDavid Chinner 	for_each_online_cpu(i) {
20528d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
205301e1b69cSDavid Chinner 		xfs_icsb_lock_cntr(cntp);
20548d280b98SDavid Chinner 	}
20558d280b98SDavid Chinner }
20568d280b98SDavid Chinner 
20577989cb8eSDavid Chinner STATIC_INLINE void
20588d280b98SDavid Chinner xfs_icsb_unlock_all_counters(
20598d280b98SDavid Chinner 	xfs_mount_t	*mp)
20608d280b98SDavid Chinner {
20618d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
20628d280b98SDavid Chinner 	int		i;
20638d280b98SDavid Chinner 
20648d280b98SDavid Chinner 	for_each_online_cpu(i) {
20658d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
206601e1b69cSDavid Chinner 		xfs_icsb_unlock_cntr(cntp);
20678d280b98SDavid Chinner 	}
20688d280b98SDavid Chinner }
20698d280b98SDavid Chinner 
20708d280b98SDavid Chinner STATIC void
20718d280b98SDavid Chinner xfs_icsb_count(
20728d280b98SDavid Chinner 	xfs_mount_t	*mp,
20738d280b98SDavid Chinner 	xfs_icsb_cnts_t	*cnt,
20748d280b98SDavid Chinner 	int		flags)
20758d280b98SDavid Chinner {
20768d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
20778d280b98SDavid Chinner 	int		i;
20788d280b98SDavid Chinner 
20798d280b98SDavid Chinner 	memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
20808d280b98SDavid Chinner 
20818d280b98SDavid Chinner 	if (!(flags & XFS_ICSB_LAZY_COUNT))
20828d280b98SDavid Chinner 		xfs_icsb_lock_all_counters(mp);
20838d280b98SDavid Chinner 
20848d280b98SDavid Chinner 	for_each_online_cpu(i) {
20858d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
20868d280b98SDavid Chinner 		cnt->icsb_icount += cntp->icsb_icount;
20878d280b98SDavid Chinner 		cnt->icsb_ifree += cntp->icsb_ifree;
20888d280b98SDavid Chinner 		cnt->icsb_fdblocks += cntp->icsb_fdblocks;
20898d280b98SDavid Chinner 	}
20908d280b98SDavid Chinner 
20918d280b98SDavid Chinner 	if (!(flags & XFS_ICSB_LAZY_COUNT))
20928d280b98SDavid Chinner 		xfs_icsb_unlock_all_counters(mp);
20938d280b98SDavid Chinner }
20948d280b98SDavid Chinner 
20958d280b98SDavid Chinner STATIC int
20968d280b98SDavid Chinner xfs_icsb_counter_disabled(
20978d280b98SDavid Chinner 	xfs_mount_t	*mp,
20988d280b98SDavid Chinner 	xfs_sb_field_t	field)
20998d280b98SDavid Chinner {
21008d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
21018d280b98SDavid Chinner 	return test_bit(field, &mp->m_icsb_counters);
21028d280b98SDavid Chinner }
21038d280b98SDavid Chinner 
21048d280b98SDavid Chinner STATIC int
21058d280b98SDavid Chinner xfs_icsb_disable_counter(
21068d280b98SDavid Chinner 	xfs_mount_t	*mp,
21078d280b98SDavid Chinner 	xfs_sb_field_t	field)
21088d280b98SDavid Chinner {
21098d280b98SDavid Chinner 	xfs_icsb_cnts_t	cnt;
21108d280b98SDavid Chinner 
21118d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
21128d280b98SDavid Chinner 
211320b64285SDavid Chinner 	/*
211420b64285SDavid Chinner 	 * If we are already disabled, then there is nothing to do
211520b64285SDavid Chinner 	 * here. We check before locking all the counters to avoid
211620b64285SDavid Chinner 	 * the expensive lock operation when being called in the
211720b64285SDavid Chinner 	 * slow path and the counter is already disabled. This is
211820b64285SDavid Chinner 	 * safe because the only time we set or clear this state is under
211920b64285SDavid Chinner 	 * the m_icsb_mutex.
212020b64285SDavid Chinner 	 */
212120b64285SDavid Chinner 	if (xfs_icsb_counter_disabled(mp, field))
212220b64285SDavid Chinner 		return 0;
212320b64285SDavid Chinner 
21248d280b98SDavid Chinner 	xfs_icsb_lock_all_counters(mp);
21258d280b98SDavid Chinner 	if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
21268d280b98SDavid Chinner 		/* drain back to superblock */
21278d280b98SDavid Chinner 
21288d280b98SDavid Chinner 		xfs_icsb_count(mp, &cnt, XFS_ICSB_SB_LOCKED|XFS_ICSB_LAZY_COUNT);
21298d280b98SDavid Chinner 		switch(field) {
21308d280b98SDavid Chinner 		case XFS_SBS_ICOUNT:
21318d280b98SDavid Chinner 			mp->m_sb.sb_icount = cnt.icsb_icount;
21328d280b98SDavid Chinner 			break;
21338d280b98SDavid Chinner 		case XFS_SBS_IFREE:
21348d280b98SDavid Chinner 			mp->m_sb.sb_ifree = cnt.icsb_ifree;
21358d280b98SDavid Chinner 			break;
21368d280b98SDavid Chinner 		case XFS_SBS_FDBLOCKS:
21378d280b98SDavid Chinner 			mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
21388d280b98SDavid Chinner 			break;
21398d280b98SDavid Chinner 		default:
21408d280b98SDavid Chinner 			BUG();
21418d280b98SDavid Chinner 		}
21428d280b98SDavid Chinner 	}
21438d280b98SDavid Chinner 
21448d280b98SDavid Chinner 	xfs_icsb_unlock_all_counters(mp);
21458d280b98SDavid Chinner 
21468d280b98SDavid Chinner 	return 0;
21478d280b98SDavid Chinner }
21488d280b98SDavid Chinner 
21498d280b98SDavid Chinner STATIC void
21508d280b98SDavid Chinner xfs_icsb_enable_counter(
21518d280b98SDavid Chinner 	xfs_mount_t	*mp,
21528d280b98SDavid Chinner 	xfs_sb_field_t	field,
21538d280b98SDavid Chinner 	uint64_t	count,
21548d280b98SDavid Chinner 	uint64_t	resid)
21558d280b98SDavid Chinner {
21568d280b98SDavid Chinner 	xfs_icsb_cnts_t	*cntp;
21578d280b98SDavid Chinner 	int		i;
21588d280b98SDavid Chinner 
21598d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
21608d280b98SDavid Chinner 
21618d280b98SDavid Chinner 	xfs_icsb_lock_all_counters(mp);
21628d280b98SDavid Chinner 	for_each_online_cpu(i) {
21638d280b98SDavid Chinner 		cntp = per_cpu_ptr(mp->m_sb_cnts, i);
21648d280b98SDavid Chinner 		switch (field) {
21658d280b98SDavid Chinner 		case XFS_SBS_ICOUNT:
21668d280b98SDavid Chinner 			cntp->icsb_icount = count + resid;
21678d280b98SDavid Chinner 			break;
21688d280b98SDavid Chinner 		case XFS_SBS_IFREE:
21698d280b98SDavid Chinner 			cntp->icsb_ifree = count + resid;
21708d280b98SDavid Chinner 			break;
21718d280b98SDavid Chinner 		case XFS_SBS_FDBLOCKS:
21728d280b98SDavid Chinner 			cntp->icsb_fdblocks = count + resid;
21738d280b98SDavid Chinner 			break;
21748d280b98SDavid Chinner 		default:
21758d280b98SDavid Chinner 			BUG();
21768d280b98SDavid Chinner 			break;
21778d280b98SDavid Chinner 		}
21788d280b98SDavid Chinner 		resid = 0;
21798d280b98SDavid Chinner 	}
21808d280b98SDavid Chinner 	clear_bit(field, &mp->m_icsb_counters);
21818d280b98SDavid Chinner 	xfs_icsb_unlock_all_counters(mp);
21828d280b98SDavid Chinner }
21838d280b98SDavid Chinner 
2184dbcabad1SDavid Chinner void
2185dbcabad1SDavid Chinner xfs_icsb_sync_counters_flags(
21868d280b98SDavid Chinner 	xfs_mount_t	*mp,
21878d280b98SDavid Chinner 	int		flags)
21888d280b98SDavid Chinner {
21898d280b98SDavid Chinner 	xfs_icsb_cnts_t	cnt;
21908d280b98SDavid Chinner 	int		s;
21918d280b98SDavid Chinner 
21928d280b98SDavid Chinner 	/* Pass 1: lock all counters */
21938d280b98SDavid Chinner 	if ((flags & XFS_ICSB_SB_LOCKED) == 0)
21948d280b98SDavid Chinner 		s = XFS_SB_LOCK(mp);
21958d280b98SDavid Chinner 
21968d280b98SDavid Chinner 	xfs_icsb_count(mp, &cnt, flags);
21978d280b98SDavid Chinner 
21988d280b98SDavid Chinner 	/* Step 3: update mp->m_sb fields */
21998d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
22008d280b98SDavid Chinner 		mp->m_sb.sb_icount = cnt.icsb_icount;
22018d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
22028d280b98SDavid Chinner 		mp->m_sb.sb_ifree = cnt.icsb_ifree;
22038d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
22048d280b98SDavid Chinner 		mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
22058d280b98SDavid Chinner 
22068d280b98SDavid Chinner 	if ((flags & XFS_ICSB_SB_LOCKED) == 0)
22078d280b98SDavid Chinner 		XFS_SB_UNLOCK(mp, s);
22088d280b98SDavid Chinner }
22098d280b98SDavid Chinner 
22108d280b98SDavid Chinner /*
22118d280b98SDavid Chinner  * Accurate update of per-cpu counters to incore superblock
22128d280b98SDavid Chinner  */
22138d280b98SDavid Chinner STATIC void
22148d280b98SDavid Chinner xfs_icsb_sync_counters(
22158d280b98SDavid Chinner 	xfs_mount_t	*mp)
22168d280b98SDavid Chinner {
2217dbcabad1SDavid Chinner 	xfs_icsb_sync_counters_flags(mp, 0);
22188d280b98SDavid Chinner }
22198d280b98SDavid Chinner 
22208d280b98SDavid Chinner /*
22218d280b98SDavid Chinner  * Balance and enable/disable counters as necessary.
22228d280b98SDavid Chinner  *
222320b64285SDavid Chinner  * Thresholds for re-enabling counters are somewhat magic.  inode counts are
222420b64285SDavid Chinner  * chosen to be the same number as single on disk allocation chunk per CPU, and
222520b64285SDavid Chinner  * free blocks is something far enough zero that we aren't going thrash when we
222620b64285SDavid Chinner  * get near ENOSPC. We also need to supply a minimum we require per cpu to
222720b64285SDavid Chinner  * prevent looping endlessly when xfs_alloc_space asks for more than will
222820b64285SDavid Chinner  * be distributed to a single CPU but each CPU has enough blocks to be
222920b64285SDavid Chinner  * reenabled.
223020b64285SDavid Chinner  *
223120b64285SDavid Chinner  * Note that we can be called when counters are already disabled.
223220b64285SDavid Chinner  * xfs_icsb_disable_counter() optimises the counter locking in this case to
223320b64285SDavid Chinner  * prevent locking every per-cpu counter needlessly.
22348d280b98SDavid Chinner  */
223520b64285SDavid Chinner 
223620b64285SDavid Chinner #define XFS_ICSB_INO_CNTR_REENABLE	(uint64_t)64
22374be536deSDavid Chinner #define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \
223820b64285SDavid Chinner 		(uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp))
22398d280b98SDavid Chinner STATIC void
22408d280b98SDavid Chinner xfs_icsb_balance_counter(
22418d280b98SDavid Chinner 	xfs_mount_t	*mp,
22428d280b98SDavid Chinner 	xfs_sb_field_t  field,
224320b64285SDavid Chinner 	int		flags,
224420b64285SDavid Chinner 	int		min_per_cpu)
22458d280b98SDavid Chinner {
22466fdf8cccSNathan Scott 	uint64_t	count, resid;
22478d280b98SDavid Chinner 	int		weight = num_online_cpus();
22488d280b98SDavid Chinner 	int		s;
224920b64285SDavid Chinner 	uint64_t	min = (uint64_t)min_per_cpu;
22508d280b98SDavid Chinner 
22518d280b98SDavid Chinner 	if (!(flags & XFS_ICSB_SB_LOCKED))
22528d280b98SDavid Chinner 		s = XFS_SB_LOCK(mp);
22538d280b98SDavid Chinner 
22548d280b98SDavid Chinner 	/* disable counter and sync counter */
22558d280b98SDavid Chinner 	xfs_icsb_disable_counter(mp, field);
22568d280b98SDavid Chinner 
22578d280b98SDavid Chinner 	/* update counters  - first CPU gets residual*/
22588d280b98SDavid Chinner 	switch (field) {
22598d280b98SDavid Chinner 	case XFS_SBS_ICOUNT:
22608d280b98SDavid Chinner 		count = mp->m_sb.sb_icount;
22618d280b98SDavid Chinner 		resid = do_div(count, weight);
226220b64285SDavid Chinner 		if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
22638d280b98SDavid Chinner 			goto out;
22648d280b98SDavid Chinner 		break;
22658d280b98SDavid Chinner 	case XFS_SBS_IFREE:
22668d280b98SDavid Chinner 		count = mp->m_sb.sb_ifree;
22678d280b98SDavid Chinner 		resid = do_div(count, weight);
226820b64285SDavid Chinner 		if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
22698d280b98SDavid Chinner 			goto out;
22708d280b98SDavid Chinner 		break;
22718d280b98SDavid Chinner 	case XFS_SBS_FDBLOCKS:
22728d280b98SDavid Chinner 		count = mp->m_sb.sb_fdblocks;
22738d280b98SDavid Chinner 		resid = do_div(count, weight);
227420b64285SDavid Chinner 		if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp)))
22758d280b98SDavid Chinner 			goto out;
22768d280b98SDavid Chinner 		break;
22778d280b98SDavid Chinner 	default:
22788d280b98SDavid Chinner 		BUG();
22796fdf8cccSNathan Scott 		count = resid = 0;	/* quiet, gcc */
22808d280b98SDavid Chinner 		break;
22818d280b98SDavid Chinner 	}
22828d280b98SDavid Chinner 
22838d280b98SDavid Chinner 	xfs_icsb_enable_counter(mp, field, count, resid);
22848d280b98SDavid Chinner out:
22858d280b98SDavid Chinner 	if (!(flags & XFS_ICSB_SB_LOCKED))
22868d280b98SDavid Chinner 		XFS_SB_UNLOCK(mp, s);
22878d280b98SDavid Chinner }
22888d280b98SDavid Chinner 
228920b64285SDavid Chinner int
229020b64285SDavid Chinner xfs_icsb_modify_counters(
22918d280b98SDavid Chinner 	xfs_mount_t	*mp,
22928d280b98SDavid Chinner 	xfs_sb_field_t	field,
229320f4ebf2SDavid Chinner 	int64_t		delta,
229420b64285SDavid Chinner 	int		rsvd)
22958d280b98SDavid Chinner {
22968d280b98SDavid Chinner 	xfs_icsb_cnts_t	*icsbp;
22978d280b98SDavid Chinner 	long long	lcounter;	/* long counter for 64 bit fields */
229820b64285SDavid Chinner 	int		cpu, ret = 0, s;
22998d280b98SDavid Chinner 
230020b64285SDavid Chinner 	might_sleep();
23018d280b98SDavid Chinner again:
23028d280b98SDavid Chinner 	cpu = get_cpu();
230320b64285SDavid Chinner 	icsbp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, cpu);
230420b64285SDavid Chinner 
230520b64285SDavid Chinner 	/*
230620b64285SDavid Chinner 	 * if the counter is disabled, go to slow path
230720b64285SDavid Chinner 	 */
23088d280b98SDavid Chinner 	if (unlikely(xfs_icsb_counter_disabled(mp, field)))
23098d280b98SDavid Chinner 		goto slow_path;
231020b64285SDavid Chinner 	xfs_icsb_lock_cntr(icsbp);
231120b64285SDavid Chinner 	if (unlikely(xfs_icsb_counter_disabled(mp, field))) {
231220b64285SDavid Chinner 		xfs_icsb_unlock_cntr(icsbp);
231320b64285SDavid Chinner 		goto slow_path;
231420b64285SDavid Chinner 	}
23158d280b98SDavid Chinner 
23168d280b98SDavid Chinner 	switch (field) {
23178d280b98SDavid Chinner 	case XFS_SBS_ICOUNT:
23188d280b98SDavid Chinner 		lcounter = icsbp->icsb_icount;
23198d280b98SDavid Chinner 		lcounter += delta;
23208d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
232120b64285SDavid Chinner 			goto balance_counter;
23228d280b98SDavid Chinner 		icsbp->icsb_icount = lcounter;
23238d280b98SDavid Chinner 		break;
23248d280b98SDavid Chinner 
23258d280b98SDavid Chinner 	case XFS_SBS_IFREE:
23268d280b98SDavid Chinner 		lcounter = icsbp->icsb_ifree;
23278d280b98SDavid Chinner 		lcounter += delta;
23288d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
232920b64285SDavid Chinner 			goto balance_counter;
23308d280b98SDavid Chinner 		icsbp->icsb_ifree = lcounter;
23318d280b98SDavid Chinner 		break;
23328d280b98SDavid Chinner 
23338d280b98SDavid Chinner 	case XFS_SBS_FDBLOCKS:
23348d280b98SDavid Chinner 		BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
23358d280b98SDavid Chinner 
23364be536deSDavid Chinner 		lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
23378d280b98SDavid Chinner 		lcounter += delta;
23388d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
233920b64285SDavid Chinner 			goto balance_counter;
23404be536deSDavid Chinner 		icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
23418d280b98SDavid Chinner 		break;
23428d280b98SDavid Chinner 	default:
23438d280b98SDavid Chinner 		BUG();
23448d280b98SDavid Chinner 		break;
23458d280b98SDavid Chinner 	}
234601e1b69cSDavid Chinner 	xfs_icsb_unlock_cntr(icsbp);
23478d280b98SDavid Chinner 	put_cpu();
23488d280b98SDavid Chinner 	return 0;
23498d280b98SDavid Chinner 
23508d280b98SDavid Chinner slow_path:
235120b64285SDavid Chinner 	put_cpu();
235220b64285SDavid Chinner 
235320b64285SDavid Chinner 	/*
235420b64285SDavid Chinner 	 * serialise with a mutex so we don't burn lots of cpu on
235520b64285SDavid Chinner 	 * the superblock lock. We still need to hold the superblock
235620b64285SDavid Chinner 	 * lock, however, when we modify the global structures.
235720b64285SDavid Chinner 	 */
235803135cf7SDavid Chinner 	xfs_icsb_lock(mp);
235920b64285SDavid Chinner 
236020b64285SDavid Chinner 	/*
236120b64285SDavid Chinner 	 * Now running atomically.
236220b64285SDavid Chinner 	 *
236320b64285SDavid Chinner 	 * If the counter is enabled, someone has beaten us to rebalancing.
236420b64285SDavid Chinner 	 * Drop the lock and try again in the fast path....
236520b64285SDavid Chinner 	 */
236620b64285SDavid Chinner 	if (!(xfs_icsb_counter_disabled(mp, field))) {
236703135cf7SDavid Chinner 		xfs_icsb_unlock(mp);
236820b64285SDavid Chinner 		goto again;
236920b64285SDavid Chinner 	}
237020b64285SDavid Chinner 
237120b64285SDavid Chinner 	/*
237220b64285SDavid Chinner 	 * The counter is currently disabled. Because we are
237320b64285SDavid Chinner 	 * running atomically here, we know a rebalance cannot
237420b64285SDavid Chinner 	 * be in progress. Hence we can go straight to operating
237520b64285SDavid Chinner 	 * on the global superblock. We do not call xfs_mod_incore_sb()
237620b64285SDavid Chinner 	 * here even though we need to get the SB_LOCK. Doing so
237720b64285SDavid Chinner 	 * will cause us to re-enter this function and deadlock.
237820b64285SDavid Chinner 	 * Hence we get the SB_LOCK ourselves and then call
237920b64285SDavid Chinner 	 * xfs_mod_incore_sb_unlocked() as the unlocked path operates
238020b64285SDavid Chinner 	 * directly on the global counters.
238120b64285SDavid Chinner 	 */
238220b64285SDavid Chinner 	s = XFS_SB_LOCK(mp);
238320b64285SDavid Chinner 	ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
238420b64285SDavid Chinner 	XFS_SB_UNLOCK(mp, s);
238520b64285SDavid Chinner 
238620b64285SDavid Chinner 	/*
238720b64285SDavid Chinner 	 * Now that we've modified the global superblock, we
238820b64285SDavid Chinner 	 * may be able to re-enable the distributed counters
238920b64285SDavid Chinner 	 * (e.g. lots of space just got freed). After that
239020b64285SDavid Chinner 	 * we are done.
239120b64285SDavid Chinner 	 */
239220b64285SDavid Chinner 	if (ret != ENOSPC)
239320b64285SDavid Chinner 		xfs_icsb_balance_counter(mp, field, 0, 0);
239403135cf7SDavid Chinner 	xfs_icsb_unlock(mp);
239520b64285SDavid Chinner 	return ret;
239620b64285SDavid Chinner 
239720b64285SDavid Chinner balance_counter:
239801e1b69cSDavid Chinner 	xfs_icsb_unlock_cntr(icsbp);
23998d280b98SDavid Chinner 	put_cpu();
24008d280b98SDavid Chinner 
240120b64285SDavid Chinner 	/*
240220b64285SDavid Chinner 	 * We may have multiple threads here if multiple per-cpu
240320b64285SDavid Chinner 	 * counters run dry at the same time. This will mean we can
240420b64285SDavid Chinner 	 * do more balances than strictly necessary but it is not
240520b64285SDavid Chinner 	 * the common slowpath case.
240620b64285SDavid Chinner 	 */
240703135cf7SDavid Chinner 	xfs_icsb_lock(mp);
240820b64285SDavid Chinner 
240920b64285SDavid Chinner 	/*
241020b64285SDavid Chinner 	 * running atomically.
241120b64285SDavid Chinner 	 *
241220b64285SDavid Chinner 	 * This will leave the counter in the correct state for future
241320b64285SDavid Chinner 	 * accesses. After the rebalance, we simply try again and our retry
241420b64285SDavid Chinner 	 * will either succeed through the fast path or slow path without
241520b64285SDavid Chinner 	 * another balance operation being required.
241620b64285SDavid Chinner 	 */
241720b64285SDavid Chinner 	xfs_icsb_balance_counter(mp, field, 0, delta);
241803135cf7SDavid Chinner 	xfs_icsb_unlock(mp);
24198d280b98SDavid Chinner 	goto again;
24208d280b98SDavid Chinner }
24218d280b98SDavid Chinner 
24228d280b98SDavid Chinner #endif
2423