xref: /linux/fs/xfs/xfs_mount.c (revision c41564b5af328ea4600b26119f6c9c8e1eb5c28b)
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_dir.h"
281da177e4SLinus Torvalds #include "xfs_dir2.h"
291da177e4SLinus Torvalds #include "xfs_dmapi.h"
301da177e4SLinus Torvalds #include "xfs_mount.h"
311da177e4SLinus Torvalds #include "xfs_bmap_btree.h"
32a844f451SNathan Scott #include "xfs_alloc_btree.h"
331da177e4SLinus Torvalds #include "xfs_ialloc_btree.h"
341da177e4SLinus Torvalds #include "xfs_dir_sf.h"
351da177e4SLinus Torvalds #include "xfs_dir2_sf.h"
36a844f451SNathan Scott #include "xfs_attr_sf.h"
371da177e4SLinus Torvalds #include "xfs_dinode.h"
381da177e4SLinus Torvalds #include "xfs_inode.h"
39a844f451SNathan Scott #include "xfs_btree.h"
40a844f451SNathan Scott #include "xfs_ialloc.h"
411da177e4SLinus Torvalds #include "xfs_alloc.h"
421da177e4SLinus Torvalds #include "xfs_rtalloc.h"
431da177e4SLinus Torvalds #include "xfs_bmap.h"
441da177e4SLinus Torvalds #include "xfs_error.h"
451da177e4SLinus Torvalds #include "xfs_rw.h"
461da177e4SLinus Torvalds #include "xfs_quota.h"
471da177e4SLinus Torvalds #include "xfs_fsops.h"
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds STATIC void	xfs_mount_log_sbunit(xfs_mount_t *, __int64_t);
501da177e4SLinus Torvalds STATIC int	xfs_uuid_mount(xfs_mount_t *);
511da177e4SLinus Torvalds STATIC void	xfs_uuid_unmount(xfs_mount_t *mp);
52ba0f32d4SChristoph Hellwig STATIC void	xfs_unmountfs_wait(xfs_mount_t *);
531da177e4SLinus Torvalds 
548d280b98SDavid Chinner 
558d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
568d280b98SDavid Chinner STATIC void	xfs_icsb_destroy_counters(xfs_mount_t *);
578d280b98SDavid Chinner STATIC void	xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t, int);
588d280b98SDavid Chinner STATIC void	xfs_icsb_sync_counters(xfs_mount_t *);
598d280b98SDavid Chinner STATIC int	xfs_icsb_modify_counters(xfs_mount_t *, xfs_sb_field_t,
608d280b98SDavid Chinner 						int, int);
618d280b98SDavid Chinner STATIC int	xfs_icsb_modify_counters_locked(xfs_mount_t *, xfs_sb_field_t,
628d280b98SDavid Chinner 						int, int);
63e8234a68SDavid Chinner STATIC int	xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
648d280b98SDavid Chinner 
658d280b98SDavid Chinner #else
668d280b98SDavid Chinner 
678d280b98SDavid Chinner #define xfs_icsb_destroy_counters(mp)			do { } while (0)
688d280b98SDavid Chinner #define xfs_icsb_balance_counter(mp, a, b)		do { } while (0)
698d280b98SDavid Chinner #define xfs_icsb_sync_counters(mp)			do { } while (0)
708d280b98SDavid Chinner #define xfs_icsb_modify_counters(mp, a, b, c)		do { } while (0)
718d280b98SDavid Chinner #define xfs_icsb_modify_counters_locked(mp, a, b, c)	do { } while (0)
728d280b98SDavid Chinner 
738d280b98SDavid Chinner #endif
748d280b98SDavid Chinner 
751df84c93SChristoph Hellwig static const struct {
761da177e4SLinus Torvalds 	short offset;
771da177e4SLinus Torvalds 	short type;	/* 0 = integer
781da177e4SLinus Torvalds 			 * 1 = binary / string (no translation)
791da177e4SLinus Torvalds 			 */
801da177e4SLinus Torvalds } xfs_sb_info[] = {
811da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_magicnum),   0 },
821da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_blocksize),  0 },
831da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_dblocks),    0 },
841da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rblocks),    0 },
851da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rextents),   0 },
861da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_uuid),       1 },
871da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logstart),   0 },
881da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rootino),    0 },
891da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rbmino),     0 },
901da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rsumino),    0 },
911da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rextsize),   0 },
921da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_agblocks),   0 },
931da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_agcount),    0 },
941da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rbmblocks),  0 },
951da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logblocks),  0 },
961da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_versionnum), 0 },
971da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_sectsize),   0 },
981da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inodesize),  0 },
991da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inopblock),  0 },
1001da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_fname[0]),   1 },
1011da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_blocklog),   0 },
1021da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_sectlog),    0 },
1031da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inodelog),   0 },
1041da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inopblog),   0 },
1051da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_agblklog),   0 },
1061da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rextslog),   0 },
1071da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inprogress), 0 },
1081da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_imax_pct),   0 },
1091da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_icount),     0 },
1101da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_ifree),      0 },
1111da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_fdblocks),   0 },
1121da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_frextents),  0 },
1131da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_uquotino),   0 },
1141da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_gquotino),   0 },
1151da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_qflags),     0 },
1161da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_flags),      0 },
1171da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_shared_vn),  0 },
1181da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
1191da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_unit),	 0 },
1201da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_width),	 0 },
1211da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_dirblklog),	 0 },
1221da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logsectlog), 0 },
1231da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logsectsize),0 },
1241da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logsunit),	 0 },
1251da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_features2),	 0 },
1261da177e4SLinus Torvalds     { sizeof(xfs_sb_t),			 0 }
1271da177e4SLinus Torvalds };
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds /*
1301da177e4SLinus Torvalds  * Return a pointer to an initialized xfs_mount structure.
1311da177e4SLinus Torvalds  */
1321da177e4SLinus Torvalds xfs_mount_t *
1331da177e4SLinus Torvalds xfs_mount_init(void)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	xfs_mount_t *mp;
1361da177e4SLinus Torvalds 
1378d280b98SDavid Chinner 	mp = kmem_zalloc(sizeof(xfs_mount_t), KM_SLEEP);
1388d280b98SDavid Chinner 
1398d280b98SDavid Chinner 	if (xfs_icsb_init_counters(mp)) {
1408d280b98SDavid Chinner 		mp->m_flags |= XFS_MOUNT_NO_PERCPU_SB;
1418d280b98SDavid Chinner 	}
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	AIL_LOCKINIT(&mp->m_ail_lock, "xfs_ail");
1441da177e4SLinus Torvalds 	spinlock_init(&mp->m_sb_lock, "xfs_sb");
145794ee1baSJes Sorensen 	mutex_init(&mp->m_ilock);
1461da177e4SLinus Torvalds 	initnsema(&mp->m_growlock, 1, "xfs_grow");
1471da177e4SLinus Torvalds 	/*
1481da177e4SLinus Torvalds 	 * Initialize the AIL.
1491da177e4SLinus Torvalds 	 */
1501da177e4SLinus Torvalds 	xfs_trans_ail_init(mp);
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	atomic_set(&mp->m_active_trans, 0);
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	return mp;
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds /*
1581da177e4SLinus Torvalds  * Free up the resources associated with a mount structure.  Assume that
1591da177e4SLinus Torvalds  * the structure was initially zeroed, so we can tell which fields got
1601da177e4SLinus Torvalds  * initialized.
1611da177e4SLinus Torvalds  */
1621da177e4SLinus Torvalds void
1631da177e4SLinus Torvalds xfs_mount_free(
1641da177e4SLinus Torvalds 	xfs_mount_t	*mp,
1651da177e4SLinus Torvalds 	int		remove_bhv)
1661da177e4SLinus Torvalds {
1671da177e4SLinus Torvalds 	if (mp->m_ihash)
1681da177e4SLinus Torvalds 		xfs_ihash_free(mp);
1691da177e4SLinus Torvalds 	if (mp->m_chash)
1701da177e4SLinus Torvalds 		xfs_chash_free(mp);
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	if (mp->m_perag) {
1731da177e4SLinus Torvalds 		int	agno;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 		for (agno = 0; agno < mp->m_maxagi; agno++)
1761da177e4SLinus Torvalds 			if (mp->m_perag[agno].pagb_list)
1771da177e4SLinus Torvalds 				kmem_free(mp->m_perag[agno].pagb_list,
1781da177e4SLinus Torvalds 						sizeof(xfs_perag_busy_t) *
1791da177e4SLinus Torvalds 							XFS_PAGB_NUM_SLOTS);
1801da177e4SLinus Torvalds 		kmem_free(mp->m_perag,
1811da177e4SLinus Torvalds 			  sizeof(xfs_perag_t) * mp->m_sb.sb_agcount);
1821da177e4SLinus Torvalds 	}
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 	AIL_LOCK_DESTROY(&mp->m_ail_lock);
1851da177e4SLinus Torvalds 	spinlock_destroy(&mp->m_sb_lock);
1861da177e4SLinus Torvalds 	mutex_destroy(&mp->m_ilock);
1871da177e4SLinus Torvalds 	freesema(&mp->m_growlock);
1881da177e4SLinus Torvalds 	if (mp->m_quotainfo)
1891da177e4SLinus Torvalds 		XFS_QM_DONE(mp);
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	if (mp->m_fsname != NULL)
1921da177e4SLinus Torvalds 		kmem_free(mp->m_fsname, mp->m_fsname_len);
193fc1f8c1cSNathan Scott 	if (mp->m_rtname != NULL)
194fc1f8c1cSNathan Scott 		kmem_free(mp->m_rtname, strlen(mp->m_rtname) + 1);
195fc1f8c1cSNathan Scott 	if (mp->m_logname != NULL)
196fc1f8c1cSNathan Scott 		kmem_free(mp->m_logname, strlen(mp->m_logname) + 1);
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	if (remove_bhv) {
1991da177e4SLinus Torvalds 		struct vfs	*vfsp = XFS_MTOVFS(mp);
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 		bhv_remove_all_vfsops(vfsp, 0);
2021da177e4SLinus Torvalds 		VFS_REMOVEBHV(vfsp, &mp->m_bhv);
2031da177e4SLinus Torvalds 	}
2041da177e4SLinus Torvalds 
2058d280b98SDavid Chinner 	xfs_icsb_destroy_counters(mp);
2061da177e4SLinus Torvalds 	kmem_free(mp, sizeof(xfs_mount_t));
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds /*
2111da177e4SLinus Torvalds  * Check the validity of the SB found.
2121da177e4SLinus Torvalds  */
2131da177e4SLinus Torvalds STATIC int
2141da177e4SLinus Torvalds xfs_mount_validate_sb(
2151da177e4SLinus Torvalds 	xfs_mount_t	*mp,
2161da177e4SLinus Torvalds 	xfs_sb_t	*sbp)
2171da177e4SLinus Torvalds {
2181da177e4SLinus Torvalds 	/*
2191da177e4SLinus Torvalds 	 * If the log device and data device have the
2201da177e4SLinus Torvalds 	 * same device number, the log is internal.
2211da177e4SLinus Torvalds 	 * Consequently, the sb_logstart should be non-zero.  If
2221da177e4SLinus Torvalds 	 * we have a zero sb_logstart in this case, we may be trying to mount
2231da177e4SLinus Torvalds 	 * a volume filesystem in a non-volume manner.
2241da177e4SLinus Torvalds 	 */
2251da177e4SLinus Torvalds 	if (sbp->sb_magicnum != XFS_SB_MAGIC) {
2261da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: bad magic number");
2271da177e4SLinus Torvalds 		return XFS_ERROR(EWRONGFS);
2281da177e4SLinus Torvalds 	}
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	if (!XFS_SB_GOOD_VERSION(sbp)) {
2311da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: bad version");
2321da177e4SLinus Torvalds 		return XFS_ERROR(EWRONGFS);
2331da177e4SLinus Torvalds 	}
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	if (unlikely(
2361da177e4SLinus Torvalds 	    sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
2371da177e4SLinus Torvalds 		cmn_err(CE_WARN,
2381da177e4SLinus Torvalds 	"XFS: filesystem is marked as having an external log; "
2391da177e4SLinus Torvalds 	"specify logdev on the\nmount command line.");
2401da177e4SLinus Torvalds 		XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(1)",
2411da177e4SLinus Torvalds 				     XFS_ERRLEVEL_HIGH, mp, sbp);
2421da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
2431da177e4SLinus Torvalds 	}
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	if (unlikely(
2461da177e4SLinus Torvalds 	    sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
2471da177e4SLinus Torvalds 		cmn_err(CE_WARN,
2481da177e4SLinus Torvalds 	"XFS: filesystem is marked as having an internal log; "
2491da177e4SLinus Torvalds 	"don't specify logdev on\nthe mount command line.");
2501da177e4SLinus Torvalds 		XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(2)",
2511da177e4SLinus Torvalds 				     XFS_ERRLEVEL_HIGH, mp, sbp);
2521da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
2531da177e4SLinus Torvalds 	}
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	/*
2561da177e4SLinus Torvalds 	 * More sanity checking. These were stolen directly from
2571da177e4SLinus Torvalds 	 * xfs_repair.
2581da177e4SLinus Torvalds 	 */
2591da177e4SLinus Torvalds 	if (unlikely(
2601da177e4SLinus Torvalds 	    sbp->sb_agcount <= 0					||
2611da177e4SLinus Torvalds 	    sbp->sb_sectsize < XFS_MIN_SECTORSIZE			||
2621da177e4SLinus Torvalds 	    sbp->sb_sectsize > XFS_MAX_SECTORSIZE			||
2631da177e4SLinus Torvalds 	    sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG			||
2641da177e4SLinus Torvalds 	    sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG			||
2651da177e4SLinus Torvalds 	    sbp->sb_blocksize < XFS_MIN_BLOCKSIZE			||
2661da177e4SLinus Torvalds 	    sbp->sb_blocksize > XFS_MAX_BLOCKSIZE			||
2671da177e4SLinus Torvalds 	    sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG			||
2681da177e4SLinus Torvalds 	    sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG			||
2691da177e4SLinus Torvalds 	    sbp->sb_inodesize < XFS_DINODE_MIN_SIZE			||
2701da177e4SLinus Torvalds 	    sbp->sb_inodesize > XFS_DINODE_MAX_SIZE			||
2719f989c94SNathan Scott 	    sbp->sb_inodelog < XFS_DINODE_MIN_LOG			||
2729f989c94SNathan Scott 	    sbp->sb_inodelog > XFS_DINODE_MAX_LOG			||
2739f989c94SNathan Scott 	    (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog)	||
2741da177e4SLinus Torvalds 	    (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE)	||
2751da177e4SLinus Torvalds 	    (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE)	||
2769f989c94SNathan Scott 	    (sbp->sb_imax_pct > 100 || sbp->sb_imax_pct < 1))) {
2771da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: SB sanity check 1 failed");
2781da177e4SLinus Torvalds 		XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(3)",
2791da177e4SLinus Torvalds 				     XFS_ERRLEVEL_LOW, mp, sbp);
2801da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
2811da177e4SLinus Torvalds 	}
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	/*
2841da177e4SLinus Torvalds 	 * Sanity check AG count, size fields against data size field
2851da177e4SLinus Torvalds 	 */
2861da177e4SLinus Torvalds 	if (unlikely(
2871da177e4SLinus Torvalds 	    sbp->sb_dblocks == 0 ||
2881da177e4SLinus Torvalds 	    sbp->sb_dblocks >
2891da177e4SLinus Torvalds 	     (xfs_drfsbno_t)sbp->sb_agcount * sbp->sb_agblocks ||
2901da177e4SLinus Torvalds 	    sbp->sb_dblocks < (xfs_drfsbno_t)(sbp->sb_agcount - 1) *
2911da177e4SLinus Torvalds 			      sbp->sb_agblocks + XFS_MIN_AG_BLOCKS)) {
2921da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: SB sanity check 2 failed");
2931da177e4SLinus Torvalds 		XFS_ERROR_REPORT("xfs_mount_validate_sb(4)",
2941da177e4SLinus Torvalds 				 XFS_ERRLEVEL_LOW, mp);
2951da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
2991da177e4SLinus Torvalds 	ASSERT(sbp->sb_blocklog >= BBSHIFT);
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds #if XFS_BIG_BLKNOS     /* Limited by ULONG_MAX of page cache index */
3021da177e4SLinus Torvalds 	if (unlikely(
3031da177e4SLinus Torvalds 	    (sbp->sb_dblocks >> (PAGE_SHIFT - sbp->sb_blocklog)) > ULONG_MAX ||
3041da177e4SLinus Torvalds 	    (sbp->sb_rblocks >> (PAGE_SHIFT - sbp->sb_blocklog)) > ULONG_MAX)) {
3051da177e4SLinus Torvalds #else                  /* Limited by UINT_MAX of sectors */
3061da177e4SLinus Torvalds 	if (unlikely(
3071da177e4SLinus Torvalds 	    (sbp->sb_dblocks << (sbp->sb_blocklog - BBSHIFT)) > UINT_MAX ||
3081da177e4SLinus Torvalds 	    (sbp->sb_rblocks << (sbp->sb_blocklog - BBSHIFT)) > UINT_MAX)) {
3091da177e4SLinus Torvalds #endif
3101da177e4SLinus Torvalds 		cmn_err(CE_WARN,
3111da177e4SLinus Torvalds 	"XFS: File system is 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)) {
3161da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: file system busy");
3171da177e4SLinus Torvalds 		XFS_ERROR_REPORT("xfs_mount_validate_sb(5)",
3181da177e4SLinus Torvalds 				 XFS_ERRLEVEL_LOW, mp);
3191da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
3201da177e4SLinus Torvalds 	}
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	/*
323de20614bSNathan Scott 	 * Version 1 directory format has never worked on Linux.
324de20614bSNathan Scott 	 */
325de20614bSNathan Scott 	if (unlikely(!XFS_SB_VERSION_HASDIRV2(sbp))) {
326de20614bSNathan Scott 		cmn_err(CE_WARN,
327de20614bSNathan Scott 	"XFS: Attempted to mount file system using version 1 directory format");
328de20614bSNathan Scott 		return XFS_ERROR(ENOSYS);
329de20614bSNathan Scott 	}
330de20614bSNathan Scott 
331de20614bSNathan Scott 	/*
3321da177e4SLinus Torvalds 	 * Until this is fixed only page-sized or smaller data blocks work.
3331da177e4SLinus Torvalds 	 */
3341da177e4SLinus Torvalds 	if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
3351da177e4SLinus Torvalds 		cmn_err(CE_WARN,
3361da177e4SLinus Torvalds 		"XFS: Attempted to mount file system with blocksize %d bytes",
3371da177e4SLinus Torvalds 			sbp->sb_blocksize);
3381da177e4SLinus Torvalds 		cmn_err(CE_WARN,
339da1650a5SChristoph Hellwig 		"XFS: Only page-sized (%ld) or less blocksizes currently work.",
3401da177e4SLinus Torvalds 			PAGE_SIZE);
3411da177e4SLinus Torvalds 		return XFS_ERROR(ENOSYS);
3421da177e4SLinus Torvalds 	}
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	return 0;
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds xfs_agnumber_t
348c11e2c36SNathan Scott xfs_initialize_perag(
349c11e2c36SNathan Scott 	struct vfs	*vfs,
350c11e2c36SNathan Scott 	xfs_mount_t	*mp,
351c11e2c36SNathan Scott 	xfs_agnumber_t	agcount)
3521da177e4SLinus Torvalds {
3531da177e4SLinus Torvalds 	xfs_agnumber_t	index, max_metadata;
3541da177e4SLinus Torvalds 	xfs_perag_t	*pag;
3551da177e4SLinus Torvalds 	xfs_agino_t	agino;
3561da177e4SLinus Torvalds 	xfs_ino_t	ino;
3571da177e4SLinus Torvalds 	xfs_sb_t	*sbp = &mp->m_sb;
3581da177e4SLinus Torvalds 	xfs_ino_t	max_inum = XFS_MAXINUMBER_32;
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	/* Check to see if the filesystem can overflow 32 bit inodes */
3611da177e4SLinus Torvalds 	agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
3621da177e4SLinus Torvalds 	ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	/* Clear the mount flag if no inode can overflow 32 bits
3651da177e4SLinus Torvalds 	 * on this filesystem, or if specifically requested..
3661da177e4SLinus Torvalds 	 */
367c11e2c36SNathan Scott 	if ((vfs->vfs_flag & VFS_32BITINODES) && ino > max_inum) {
3681da177e4SLinus Torvalds 		mp->m_flags |= XFS_MOUNT_32BITINODES;
3691da177e4SLinus Torvalds 	} else {
3701da177e4SLinus Torvalds 		mp->m_flags &= ~XFS_MOUNT_32BITINODES;
3711da177e4SLinus Torvalds 	}
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	/* If we can overflow then setup the ag headers accordingly */
3741da177e4SLinus Torvalds 	if (mp->m_flags & XFS_MOUNT_32BITINODES) {
3751da177e4SLinus Torvalds 		/* Calculate how much should be reserved for inodes to
3761da177e4SLinus Torvalds 		 * meet the max inode percentage.
3771da177e4SLinus Torvalds 		 */
3781da177e4SLinus Torvalds 		if (mp->m_maxicount) {
3791da177e4SLinus Torvalds 			__uint64_t	icount;
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 			icount = sbp->sb_dblocks * sbp->sb_imax_pct;
3821da177e4SLinus Torvalds 			do_div(icount, 100);
3831da177e4SLinus Torvalds 			icount += sbp->sb_agblocks - 1;
384a749ee86SEric Sandeen 			do_div(icount, sbp->sb_agblocks);
3851da177e4SLinus Torvalds 			max_metadata = icount;
3861da177e4SLinus Torvalds 		} else {
3871da177e4SLinus Torvalds 			max_metadata = agcount;
3881da177e4SLinus Torvalds 		}
3891da177e4SLinus Torvalds 		for (index = 0; index < agcount; index++) {
3901da177e4SLinus Torvalds 			ino = XFS_AGINO_TO_INO(mp, index, agino);
3911da177e4SLinus Torvalds 			if (ino > max_inum) {
3921da177e4SLinus Torvalds 				index++;
3931da177e4SLinus Torvalds 				break;
3941da177e4SLinus Torvalds 			}
3951da177e4SLinus Torvalds 
396*c41564b5SNathan Scott 			/* This ag is preferred for inodes */
3971da177e4SLinus Torvalds 			pag = &mp->m_perag[index];
3981da177e4SLinus Torvalds 			pag->pagi_inodeok = 1;
3991da177e4SLinus Torvalds 			if (index < max_metadata)
4001da177e4SLinus Torvalds 				pag->pagf_metadata = 1;
4011da177e4SLinus Torvalds 		}
4021da177e4SLinus Torvalds 	} else {
4031da177e4SLinus Torvalds 		/* Setup default behavior for smaller filesystems */
4041da177e4SLinus Torvalds 		for (index = 0; index < agcount; index++) {
4051da177e4SLinus Torvalds 			pag = &mp->m_perag[index];
4061da177e4SLinus Torvalds 			pag->pagi_inodeok = 1;
4071da177e4SLinus Torvalds 		}
4081da177e4SLinus Torvalds 	}
4091da177e4SLinus Torvalds 	return index;
4101da177e4SLinus Torvalds }
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds /*
4131da177e4SLinus Torvalds  * xfs_xlatesb
4141da177e4SLinus Torvalds  *
4151da177e4SLinus Torvalds  *     data       - on disk version of sb
4161da177e4SLinus Torvalds  *     sb         - a superblock
4171da177e4SLinus Torvalds  *     dir        - conversion direction: <0 - convert sb to buf
4181da177e4SLinus Torvalds  *                                        >0 - convert buf to sb
4191da177e4SLinus Torvalds  *     fields     - which fields to copy (bitmask)
4201da177e4SLinus Torvalds  */
4211da177e4SLinus Torvalds void
4221da177e4SLinus Torvalds xfs_xlatesb(
4231da177e4SLinus Torvalds 	void		*data,
4241da177e4SLinus Torvalds 	xfs_sb_t	*sb,
4251da177e4SLinus Torvalds 	int		dir,
4261da177e4SLinus Torvalds 	__int64_t	fields)
4271da177e4SLinus Torvalds {
4281da177e4SLinus Torvalds 	xfs_caddr_t	buf_ptr;
4291da177e4SLinus Torvalds 	xfs_caddr_t	mem_ptr;
4301da177e4SLinus Torvalds 	xfs_sb_field_t	f;
4311da177e4SLinus Torvalds 	int		first;
4321da177e4SLinus Torvalds 	int		size;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 	ASSERT(dir);
4351da177e4SLinus Torvalds 	ASSERT(fields);
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 	if (!fields)
4381da177e4SLinus Torvalds 		return;
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	buf_ptr = (xfs_caddr_t)data;
4411da177e4SLinus Torvalds 	mem_ptr = (xfs_caddr_t)sb;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	while (fields) {
4441da177e4SLinus Torvalds 		f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
4451da177e4SLinus Torvalds 		first = xfs_sb_info[f].offset;
4461da177e4SLinus Torvalds 		size = xfs_sb_info[f + 1].offset - first;
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 		ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 		if (size == 1 || xfs_sb_info[f].type == 1) {
4511da177e4SLinus Torvalds 			if (dir > 0) {
4521da177e4SLinus Torvalds 				memcpy(mem_ptr + first, buf_ptr + first, size);
4531da177e4SLinus Torvalds 			} else {
4541da177e4SLinus Torvalds 				memcpy(buf_ptr + first, mem_ptr + first, size);
4551da177e4SLinus Torvalds 			}
4561da177e4SLinus Torvalds 		} else {
4571da177e4SLinus Torvalds 			switch (size) {
4581da177e4SLinus Torvalds 			case 2:
4591da177e4SLinus Torvalds 				INT_XLATE(*(__uint16_t*)(buf_ptr+first),
4601da177e4SLinus Torvalds 					  *(__uint16_t*)(mem_ptr+first),
4611da177e4SLinus Torvalds 					  dir, ARCH_CONVERT);
4621da177e4SLinus Torvalds 				break;
4631da177e4SLinus Torvalds 			case 4:
4641da177e4SLinus Torvalds 				INT_XLATE(*(__uint32_t*)(buf_ptr+first),
4651da177e4SLinus Torvalds 					  *(__uint32_t*)(mem_ptr+first),
4661da177e4SLinus Torvalds 					  dir, ARCH_CONVERT);
4671da177e4SLinus Torvalds 				break;
4681da177e4SLinus Torvalds 			case 8:
4691da177e4SLinus Torvalds 				INT_XLATE(*(__uint64_t*)(buf_ptr+first),
4701da177e4SLinus Torvalds 					  *(__uint64_t*)(mem_ptr+first), dir, ARCH_CONVERT);
4711da177e4SLinus Torvalds 				break;
4721da177e4SLinus Torvalds 			default:
4731da177e4SLinus Torvalds 				ASSERT(0);
4741da177e4SLinus Torvalds 			}
4751da177e4SLinus Torvalds 		}
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 		fields &= ~(1LL << f);
4781da177e4SLinus Torvalds 	}
4791da177e4SLinus Torvalds }
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds /*
4821da177e4SLinus Torvalds  * xfs_readsb
4831da177e4SLinus Torvalds  *
4841da177e4SLinus Torvalds  * Does the initial read of the superblock.
4851da177e4SLinus Torvalds  */
4861da177e4SLinus Torvalds int
4871da177e4SLinus Torvalds xfs_readsb(xfs_mount_t *mp)
4881da177e4SLinus Torvalds {
4891da177e4SLinus Torvalds 	unsigned int	sector_size;
4901da177e4SLinus Torvalds 	unsigned int	extra_flags;
4911da177e4SLinus Torvalds 	xfs_buf_t	*bp;
4921da177e4SLinus Torvalds 	xfs_sb_t	*sbp;
4931da177e4SLinus Torvalds 	int		error;
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	ASSERT(mp->m_sb_bp == NULL);
4961da177e4SLinus Torvalds 	ASSERT(mp->m_ddev_targp != NULL);
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	/*
4991da177e4SLinus Torvalds 	 * Allocate a (locked) buffer to hold the superblock.
5001da177e4SLinus Torvalds 	 * This will be kept around at all times to optimize
5011da177e4SLinus Torvalds 	 * access to the superblock.
5021da177e4SLinus Torvalds 	 */
5031da177e4SLinus Torvalds 	sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
5041da177e4SLinus Torvalds 	extra_flags = XFS_BUF_LOCK | XFS_BUF_MANAGE | XFS_BUF_MAPPED;
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
5071da177e4SLinus Torvalds 				BTOBB(sector_size), extra_flags);
5081da177e4SLinus Torvalds 	if (!bp || XFS_BUF_ISERROR(bp)) {
5091da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: SB read failed");
5101da177e4SLinus Torvalds 		error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
5111da177e4SLinus Torvalds 		goto fail;
5121da177e4SLinus Torvalds 	}
5131da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISBUSY(bp));
5141da177e4SLinus Torvalds 	ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	/*
5171da177e4SLinus Torvalds 	 * Initialize the mount structure from the superblock.
5181da177e4SLinus Torvalds 	 * But first do some basic consistency checking.
5191da177e4SLinus Torvalds 	 */
5201da177e4SLinus Torvalds 	sbp = XFS_BUF_TO_SBP(bp);
5211da177e4SLinus Torvalds 	xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), 1, XFS_SB_ALL_BITS);
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	error = xfs_mount_validate_sb(mp, &(mp->m_sb));
5241da177e4SLinus Torvalds 	if (error) {
5251da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: SB validate failed");
5261da177e4SLinus Torvalds 		goto fail;
5271da177e4SLinus Torvalds 	}
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	/*
5301da177e4SLinus Torvalds 	 * We must be able to do sector-sized and sector-aligned IO.
5311da177e4SLinus Torvalds 	 */
5321da177e4SLinus Torvalds 	if (sector_size > mp->m_sb.sb_sectsize) {
5331da177e4SLinus Torvalds 		cmn_err(CE_WARN,
5341da177e4SLinus Torvalds 			"XFS: device supports only %u byte sectors (not %u)",
5351da177e4SLinus Torvalds 			sector_size, mp->m_sb.sb_sectsize);
5361da177e4SLinus Torvalds 		error = ENOSYS;
5371da177e4SLinus Torvalds 		goto fail;
5381da177e4SLinus Torvalds 	}
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 	/*
5411da177e4SLinus Torvalds 	 * If device sector size is smaller than the superblock size,
5421da177e4SLinus Torvalds 	 * re-read the superblock so the buffer is correctly sized.
5431da177e4SLinus Torvalds 	 */
5441da177e4SLinus Torvalds 	if (sector_size < mp->m_sb.sb_sectsize) {
5451da177e4SLinus Torvalds 		XFS_BUF_UNMANAGE(bp);
5461da177e4SLinus Torvalds 		xfs_buf_relse(bp);
5471da177e4SLinus Torvalds 		sector_size = mp->m_sb.sb_sectsize;
5481da177e4SLinus Torvalds 		bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
5491da177e4SLinus Torvalds 					BTOBB(sector_size), extra_flags);
5501da177e4SLinus Torvalds 		if (!bp || XFS_BUF_ISERROR(bp)) {
5511da177e4SLinus Torvalds 			cmn_err(CE_WARN, "XFS: SB re-read failed");
5521da177e4SLinus Torvalds 			error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
5531da177e4SLinus Torvalds 			goto fail;
5541da177e4SLinus Torvalds 		}
5551da177e4SLinus Torvalds 		ASSERT(XFS_BUF_ISBUSY(bp));
5561da177e4SLinus Torvalds 		ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
5571da177e4SLinus Torvalds 	}
5581da177e4SLinus Torvalds 
5598d280b98SDavid Chinner 	xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
5608d280b98SDavid Chinner 	xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
5618d280b98SDavid Chinner 	xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
5628d280b98SDavid Chinner 
5631da177e4SLinus Torvalds 	mp->m_sb_bp = bp;
5641da177e4SLinus Torvalds 	xfs_buf_relse(bp);
5651da177e4SLinus Torvalds 	ASSERT(XFS_BUF_VALUSEMA(bp) > 0);
5661da177e4SLinus Torvalds 	return 0;
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds  fail:
5691da177e4SLinus Torvalds 	if (bp) {
5701da177e4SLinus Torvalds 		XFS_BUF_UNMANAGE(bp);
5711da177e4SLinus Torvalds 		xfs_buf_relse(bp);
5721da177e4SLinus Torvalds 	}
5731da177e4SLinus Torvalds 	return error;
5741da177e4SLinus Torvalds }
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds /*
5781da177e4SLinus Torvalds  * xfs_mount_common
5791da177e4SLinus Torvalds  *
5801da177e4SLinus Torvalds  * Mount initialization code establishing various mount
5811da177e4SLinus Torvalds  * fields from the superblock associated with the given
5821da177e4SLinus Torvalds  * mount structure
5831da177e4SLinus Torvalds  */
584ba0f32d4SChristoph Hellwig STATIC void
5851da177e4SLinus Torvalds xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
5861da177e4SLinus Torvalds {
5871da177e4SLinus Torvalds 	int	i;
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	mp->m_agfrotor = mp->m_agirotor = 0;
5901da177e4SLinus Torvalds 	spinlock_init(&mp->m_agirotor_lock, "m_agirotor_lock");
5911da177e4SLinus Torvalds 	mp->m_maxagi = mp->m_sb.sb_agcount;
5921da177e4SLinus Torvalds 	mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
5931da177e4SLinus Torvalds 	mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
5941da177e4SLinus Torvalds 	mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
5951da177e4SLinus Torvalds 	mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
5961da177e4SLinus Torvalds 	mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
5971da177e4SLinus Torvalds 	mp->m_litino = sbp->sb_inodesize -
5981da177e4SLinus Torvalds 		((uint)sizeof(xfs_dinode_core_t) + (uint)sizeof(xfs_agino_t));
5991da177e4SLinus Torvalds 	mp->m_blockmask = sbp->sb_blocksize - 1;
6001da177e4SLinus Torvalds 	mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
6011da177e4SLinus Torvalds 	mp->m_blockwmask = mp->m_blockwsize - 1;
6021da177e4SLinus Torvalds 	INIT_LIST_HEAD(&mp->m_del_inodes);
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	/*
6051da177e4SLinus Torvalds 	 * Setup for attributes, in case they get created.
6061da177e4SLinus Torvalds 	 * This value is for inodes getting attributes for the first time,
6071da177e4SLinus Torvalds 	 * the per-inode value is for old attribute values.
6081da177e4SLinus Torvalds 	 */
6091da177e4SLinus Torvalds 	ASSERT(sbp->sb_inodesize >= 256 && sbp->sb_inodesize <= 2048);
6101da177e4SLinus Torvalds 	switch (sbp->sb_inodesize) {
6111da177e4SLinus Torvalds 	case 256:
612d8cc890dSNathan Scott 		mp->m_attroffset = XFS_LITINO(mp) -
613d8cc890dSNathan Scott 				   XFS_BMDR_SPACE_CALC(MINABTPTRS);
6141da177e4SLinus Torvalds 		break;
6151da177e4SLinus Torvalds 	case 512:
6161da177e4SLinus Torvalds 	case 1024:
6171da177e4SLinus Torvalds 	case 2048:
618d8cc890dSNathan Scott 		mp->m_attroffset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
6191da177e4SLinus Torvalds 		break;
6201da177e4SLinus Torvalds 	default:
6211da177e4SLinus Torvalds 		ASSERT(0);
6221da177e4SLinus Torvalds 	}
6231da177e4SLinus Torvalds 	ASSERT(mp->m_attroffset < XFS_LITINO(mp));
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	for (i = 0; i < 2; i++) {
6261da177e4SLinus Torvalds 		mp->m_alloc_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
6271da177e4SLinus Torvalds 			xfs_alloc, i == 0);
6281da177e4SLinus Torvalds 		mp->m_alloc_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
6291da177e4SLinus Torvalds 			xfs_alloc, i == 0);
6301da177e4SLinus Torvalds 	}
6311da177e4SLinus Torvalds 	for (i = 0; i < 2; i++) {
6321da177e4SLinus Torvalds 		mp->m_bmap_dmxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
6331da177e4SLinus Torvalds 			xfs_bmbt, i == 0);
6341da177e4SLinus Torvalds 		mp->m_bmap_dmnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
6351da177e4SLinus Torvalds 			xfs_bmbt, i == 0);
6361da177e4SLinus Torvalds 	}
6371da177e4SLinus Torvalds 	for (i = 0; i < 2; i++) {
6381da177e4SLinus Torvalds 		mp->m_inobt_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
6391da177e4SLinus Torvalds 			xfs_inobt, i == 0);
6401da177e4SLinus Torvalds 		mp->m_inobt_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
6411da177e4SLinus Torvalds 			xfs_inobt, i == 0);
6421da177e4SLinus Torvalds 	}
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
6451da177e4SLinus Torvalds 	mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
6461da177e4SLinus Torvalds 					sbp->sb_inopblock);
6471da177e4SLinus Torvalds 	mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
6481da177e4SLinus Torvalds }
6491da177e4SLinus Torvalds /*
6501da177e4SLinus Torvalds  * xfs_mountfs
6511da177e4SLinus Torvalds  *
6521da177e4SLinus Torvalds  * This function does the following on an initial mount of a file system:
6531da177e4SLinus Torvalds  *	- reads the superblock from disk and init the mount struct
6541da177e4SLinus Torvalds  *	- if we're a 32-bit kernel, do a size check on the superblock
6551da177e4SLinus Torvalds  *		so we don't mount terabyte filesystems
6561da177e4SLinus Torvalds  *	- init mount struct realtime fields
6571da177e4SLinus Torvalds  *	- allocate inode hash table for fs
6581da177e4SLinus Torvalds  *	- init directory manager
6591da177e4SLinus Torvalds  *	- perform recovery and init the log manager
6601da177e4SLinus Torvalds  */
6611da177e4SLinus Torvalds int
6621da177e4SLinus Torvalds xfs_mountfs(
6631da177e4SLinus Torvalds 	vfs_t		*vfsp,
6641da177e4SLinus Torvalds 	xfs_mount_t	*mp,
6651da177e4SLinus Torvalds 	int		mfsi_flags)
6661da177e4SLinus Torvalds {
6671da177e4SLinus Torvalds 	xfs_buf_t	*bp;
6681da177e4SLinus Torvalds 	xfs_sb_t	*sbp = &(mp->m_sb);
6691da177e4SLinus Torvalds 	xfs_inode_t	*rip;
6701da177e4SLinus Torvalds 	vnode_t		*rvp = NULL;
6711da177e4SLinus Torvalds 	int		readio_log, writeio_log;
6721da177e4SLinus Torvalds 	xfs_daddr_t	d;
6731da177e4SLinus Torvalds 	__uint64_t	ret64;
6741da177e4SLinus Torvalds 	__int64_t	update_flags;
6751da177e4SLinus Torvalds 	uint		quotamount, quotaflags;
6761da177e4SLinus Torvalds 	int		agno;
6771da177e4SLinus Torvalds 	int		uuid_mounted = 0;
6781da177e4SLinus Torvalds 	int		error = 0;
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	if (mp->m_sb_bp == NULL) {
6811da177e4SLinus Torvalds 		if ((error = xfs_readsb(mp))) {
682014c2544SJesper Juhl 			return error;
6831da177e4SLinus Torvalds 		}
6841da177e4SLinus Torvalds 	}
6851da177e4SLinus Torvalds 	xfs_mount_common(mp, sbp);
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	/*
6881da177e4SLinus Torvalds 	 * Check if sb_agblocks is aligned at stripe boundary
6891da177e4SLinus Torvalds 	 * If sb_agblocks is NOT aligned turn off m_dalign since
6901da177e4SLinus Torvalds 	 * allocator alignment is within an ag, therefore ag has
6911da177e4SLinus Torvalds 	 * to be aligned at stripe boundary.
6921da177e4SLinus Torvalds 	 */
6931da177e4SLinus Torvalds 	update_flags = 0LL;
6941da177e4SLinus Torvalds 	if (mp->m_dalign && !(mfsi_flags & XFS_MFSI_SECOND)) {
6951da177e4SLinus Torvalds 		/*
6961da177e4SLinus Torvalds 		 * If stripe unit and stripe width are not multiples
6971da177e4SLinus Torvalds 		 * of the fs blocksize turn off alignment.
6981da177e4SLinus Torvalds 		 */
6991da177e4SLinus Torvalds 		if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
7001da177e4SLinus Torvalds 		    (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
7011da177e4SLinus Torvalds 			if (mp->m_flags & XFS_MOUNT_RETERR) {
7021da177e4SLinus Torvalds 				cmn_err(CE_WARN,
7031da177e4SLinus Torvalds 					"XFS: alignment check 1 failed");
7041da177e4SLinus Torvalds 				error = XFS_ERROR(EINVAL);
7051da177e4SLinus Torvalds 				goto error1;
7061da177e4SLinus Torvalds 			}
7071da177e4SLinus Torvalds 			mp->m_dalign = mp->m_swidth = 0;
7081da177e4SLinus Torvalds 		} else {
7091da177e4SLinus Torvalds 			/*
7101da177e4SLinus Torvalds 			 * Convert the stripe unit and width to FSBs.
7111da177e4SLinus Torvalds 			 */
7121da177e4SLinus Torvalds 			mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
7131da177e4SLinus Torvalds 			if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
7141da177e4SLinus Torvalds 				if (mp->m_flags & XFS_MOUNT_RETERR) {
7151da177e4SLinus Torvalds 					error = XFS_ERROR(EINVAL);
7161da177e4SLinus Torvalds 					goto error1;
7171da177e4SLinus Torvalds 				}
7181da177e4SLinus Torvalds 				xfs_fs_cmn_err(CE_WARN, mp,
7191da177e4SLinus Torvalds "stripe alignment turned off: sunit(%d)/swidth(%d) incompatible with agsize(%d)",
7201da177e4SLinus Torvalds 					mp->m_dalign, mp->m_swidth,
7211da177e4SLinus Torvalds 					sbp->sb_agblocks);
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 				mp->m_dalign = 0;
7241da177e4SLinus Torvalds 				mp->m_swidth = 0;
7251da177e4SLinus Torvalds 			} else if (mp->m_dalign) {
7261da177e4SLinus Torvalds 				mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
7271da177e4SLinus Torvalds 			} else {
7281da177e4SLinus Torvalds 				if (mp->m_flags & XFS_MOUNT_RETERR) {
7291da177e4SLinus Torvalds 					xfs_fs_cmn_err(CE_WARN, mp,
7301da177e4SLinus Torvalds "stripe alignment turned off: sunit(%d) less than bsize(%d)",
7311da177e4SLinus Torvalds                                         	mp->m_dalign,
7321da177e4SLinus Torvalds 						mp->m_blockmask +1);
7331da177e4SLinus Torvalds 					error = XFS_ERROR(EINVAL);
7341da177e4SLinus Torvalds 					goto error1;
7351da177e4SLinus Torvalds 				}
7361da177e4SLinus Torvalds 				mp->m_swidth = 0;
7371da177e4SLinus Torvalds 			}
7381da177e4SLinus Torvalds 		}
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 		/*
7411da177e4SLinus Torvalds 		 * Update superblock with new values
7421da177e4SLinus Torvalds 		 * and log changes
7431da177e4SLinus Torvalds 		 */
7441da177e4SLinus Torvalds 		if (XFS_SB_VERSION_HASDALIGN(sbp)) {
7451da177e4SLinus Torvalds 			if (sbp->sb_unit != mp->m_dalign) {
7461da177e4SLinus Torvalds 				sbp->sb_unit = mp->m_dalign;
7471da177e4SLinus Torvalds 				update_flags |= XFS_SB_UNIT;
7481da177e4SLinus Torvalds 			}
7491da177e4SLinus Torvalds 			if (sbp->sb_width != mp->m_swidth) {
7501da177e4SLinus Torvalds 				sbp->sb_width = mp->m_swidth;
7511da177e4SLinus Torvalds 				update_flags |= XFS_SB_WIDTH;
7521da177e4SLinus Torvalds 			}
7531da177e4SLinus Torvalds 		}
7541da177e4SLinus Torvalds 	} else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
7551da177e4SLinus Torvalds 		    XFS_SB_VERSION_HASDALIGN(&mp->m_sb)) {
7561da177e4SLinus Torvalds 			mp->m_dalign = sbp->sb_unit;
7571da177e4SLinus Torvalds 			mp->m_swidth = sbp->sb_width;
7581da177e4SLinus Torvalds 	}
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	xfs_alloc_compute_maxlevels(mp);
7611da177e4SLinus Torvalds 	xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
7621da177e4SLinus Torvalds 	xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
7631da177e4SLinus Torvalds 	xfs_ialloc_compute_maxlevels(mp);
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	if (sbp->sb_imax_pct) {
7661da177e4SLinus Torvalds 		__uint64_t	icount;
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 		/* Make sure the maximum inode count is a multiple of the
7691da177e4SLinus Torvalds 		 * units we allocate inodes in.
7701da177e4SLinus Torvalds 		 */
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 		icount = sbp->sb_dblocks * sbp->sb_imax_pct;
7731da177e4SLinus Torvalds 		do_div(icount, 100);
7741da177e4SLinus Torvalds 		do_div(icount, mp->m_ialloc_blks);
7751da177e4SLinus Torvalds 		mp->m_maxicount = (icount * mp->m_ialloc_blks)  <<
7761da177e4SLinus Torvalds 				   sbp->sb_inopblog;
7771da177e4SLinus Torvalds 	} else
7781da177e4SLinus Torvalds 		mp->m_maxicount = 0;
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog);
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	/*
7831da177e4SLinus Torvalds 	 * XFS uses the uuid from the superblock as the unique
7841da177e4SLinus Torvalds 	 * identifier for fsid.  We can not use the uuid from the volume
7851da177e4SLinus Torvalds 	 * since a single partition filesystem is identical to a single
7861da177e4SLinus Torvalds 	 * partition volume/filesystem.
7871da177e4SLinus Torvalds 	 */
7881da177e4SLinus Torvalds 	if ((mfsi_flags & XFS_MFSI_SECOND) == 0 &&
7891da177e4SLinus Torvalds 	    (mp->m_flags & XFS_MOUNT_NOUUID) == 0) {
7901da177e4SLinus Torvalds 		if (xfs_uuid_mount(mp)) {
7911da177e4SLinus Torvalds 			error = XFS_ERROR(EINVAL);
7921da177e4SLinus Torvalds 			goto error1;
7931da177e4SLinus Torvalds 		}
7941da177e4SLinus Torvalds 		uuid_mounted=1;
7951da177e4SLinus Torvalds 		ret64 = uuid_hash64(&sbp->sb_uuid);
7961da177e4SLinus Torvalds 		memcpy(&vfsp->vfs_fsid, &ret64, sizeof(ret64));
7971da177e4SLinus Torvalds 	}
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	/*
8001da177e4SLinus Torvalds 	 * Set the default minimum read and write sizes unless
8011da177e4SLinus Torvalds 	 * already specified in a mount option.
8021da177e4SLinus Torvalds 	 * We use smaller I/O sizes when the file system
8031da177e4SLinus Torvalds 	 * is being used for NFS service (wsync mount option).
8041da177e4SLinus Torvalds 	 */
8051da177e4SLinus Torvalds 	if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
8061da177e4SLinus Torvalds 		if (mp->m_flags & XFS_MOUNT_WSYNC) {
8071da177e4SLinus Torvalds 			readio_log = XFS_WSYNC_READIO_LOG;
8081da177e4SLinus Torvalds 			writeio_log = XFS_WSYNC_WRITEIO_LOG;
8091da177e4SLinus Torvalds 		} else {
8101da177e4SLinus Torvalds 			readio_log = XFS_READIO_LOG_LARGE;
8111da177e4SLinus Torvalds 			writeio_log = XFS_WRITEIO_LOG_LARGE;
8121da177e4SLinus Torvalds 		}
8131da177e4SLinus Torvalds 	} else {
8141da177e4SLinus Torvalds 		readio_log = mp->m_readio_log;
8151da177e4SLinus Torvalds 		writeio_log = mp->m_writeio_log;
8161da177e4SLinus Torvalds 	}
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	/*
8191da177e4SLinus Torvalds 	 * Set the number of readahead buffers to use based on
8201da177e4SLinus Torvalds 	 * physical memory size.
8211da177e4SLinus Torvalds 	 */
8221da177e4SLinus Torvalds 	if (xfs_physmem <= 4096)		/* <= 16MB */
8231da177e4SLinus Torvalds 		mp->m_nreadaheads = XFS_RW_NREADAHEAD_16MB;
8241da177e4SLinus Torvalds 	else if (xfs_physmem <= 8192)	/* <= 32MB */
8251da177e4SLinus Torvalds 		mp->m_nreadaheads = XFS_RW_NREADAHEAD_32MB;
8261da177e4SLinus Torvalds 	else
8271da177e4SLinus Torvalds 		mp->m_nreadaheads = XFS_RW_NREADAHEAD_K32;
8281da177e4SLinus Torvalds 	if (sbp->sb_blocklog > readio_log) {
8291da177e4SLinus Torvalds 		mp->m_readio_log = sbp->sb_blocklog;
8301da177e4SLinus Torvalds 	} else {
8311da177e4SLinus Torvalds 		mp->m_readio_log = readio_log;
8321da177e4SLinus Torvalds 	}
8331da177e4SLinus Torvalds 	mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
8341da177e4SLinus Torvalds 	if (sbp->sb_blocklog > writeio_log) {
8351da177e4SLinus Torvalds 		mp->m_writeio_log = sbp->sb_blocklog;
8361da177e4SLinus Torvalds 	} else {
8371da177e4SLinus Torvalds 		mp->m_writeio_log = writeio_log;
8381da177e4SLinus Torvalds 	}
8391da177e4SLinus Torvalds 	mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	/*
8421da177e4SLinus Torvalds 	 * Set the inode cluster size based on the physical memory
8431da177e4SLinus Torvalds 	 * size.  This may still be overridden by the file system
8441da177e4SLinus Torvalds 	 * block size if it is larger than the chosen cluster size.
8451da177e4SLinus Torvalds 	 */
8461da177e4SLinus Torvalds 	if (xfs_physmem <= btoc(32 * 1024 * 1024)) { /* <= 32 MB */
8471da177e4SLinus Torvalds 		mp->m_inode_cluster_size = XFS_INODE_SMALL_CLUSTER_SIZE;
8481da177e4SLinus Torvalds 	} else {
8491da177e4SLinus Torvalds 		mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
8501da177e4SLinus Torvalds 	}
8511da177e4SLinus Torvalds 	/*
8521da177e4SLinus Torvalds 	 * Set whether we're using inode alignment.
8531da177e4SLinus Torvalds 	 */
8541da177e4SLinus Torvalds 	if (XFS_SB_VERSION_HASALIGN(&mp->m_sb) &&
8551da177e4SLinus Torvalds 	    mp->m_sb.sb_inoalignmt >=
8561da177e4SLinus Torvalds 	    XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
8571da177e4SLinus Torvalds 		mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
8581da177e4SLinus Torvalds 	else
8591da177e4SLinus Torvalds 		mp->m_inoalign_mask = 0;
8601da177e4SLinus Torvalds 	/*
8611da177e4SLinus Torvalds 	 * If we are using stripe alignment, check whether
8621da177e4SLinus Torvalds 	 * the stripe unit is a multiple of the inode alignment
8631da177e4SLinus Torvalds 	 */
8641da177e4SLinus Torvalds 	if (mp->m_dalign && mp->m_inoalign_mask &&
8651da177e4SLinus Torvalds 	    !(mp->m_dalign & mp->m_inoalign_mask))
8661da177e4SLinus Torvalds 		mp->m_sinoalign = mp->m_dalign;
8671da177e4SLinus Torvalds 	else
8681da177e4SLinus Torvalds 		mp->m_sinoalign = 0;
8691da177e4SLinus Torvalds 	/*
8701da177e4SLinus Torvalds 	 * Check that the data (and log if separate) are an ok size.
8711da177e4SLinus Torvalds 	 */
8721da177e4SLinus Torvalds 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
8731da177e4SLinus Torvalds 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
8741da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: size check 1 failed");
8751da177e4SLinus Torvalds 		error = XFS_ERROR(E2BIG);
8761da177e4SLinus Torvalds 		goto error1;
8771da177e4SLinus Torvalds 	}
8781da177e4SLinus Torvalds 	error = xfs_read_buf(mp, mp->m_ddev_targp,
8791da177e4SLinus Torvalds 			     d - XFS_FSS_TO_BB(mp, 1),
8801da177e4SLinus Torvalds 			     XFS_FSS_TO_BB(mp, 1), 0, &bp);
8811da177e4SLinus Torvalds 	if (!error) {
8821da177e4SLinus Torvalds 		xfs_buf_relse(bp);
8831da177e4SLinus Torvalds 	} else {
8841da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: size check 2 failed");
8851da177e4SLinus Torvalds 		if (error == ENOSPC) {
8861da177e4SLinus Torvalds 			error = XFS_ERROR(E2BIG);
8871da177e4SLinus Torvalds 		}
8881da177e4SLinus Torvalds 		goto error1;
8891da177e4SLinus Torvalds 	}
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	if (((mfsi_flags & XFS_MFSI_CLIENT) == 0) &&
8921da177e4SLinus Torvalds 	    mp->m_logdev_targp != mp->m_ddev_targp) {
8931da177e4SLinus Torvalds 		d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
8941da177e4SLinus Torvalds 		if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
8951da177e4SLinus Torvalds 			cmn_err(CE_WARN, "XFS: size check 3 failed");
8961da177e4SLinus Torvalds 			error = XFS_ERROR(E2BIG);
8971da177e4SLinus Torvalds 			goto error1;
8981da177e4SLinus Torvalds 		}
8991da177e4SLinus Torvalds 		error = xfs_read_buf(mp, mp->m_logdev_targp,
9001da177e4SLinus Torvalds 				     d - XFS_FSB_TO_BB(mp, 1),
9011da177e4SLinus Torvalds 				     XFS_FSB_TO_BB(mp, 1), 0, &bp);
9021da177e4SLinus Torvalds 		if (!error) {
9031da177e4SLinus Torvalds 			xfs_buf_relse(bp);
9041da177e4SLinus Torvalds 		} else {
9051da177e4SLinus Torvalds 			cmn_err(CE_WARN, "XFS: size check 3 failed");
9061da177e4SLinus Torvalds 			if (error == ENOSPC) {
9071da177e4SLinus Torvalds 				error = XFS_ERROR(E2BIG);
9081da177e4SLinus Torvalds 			}
9091da177e4SLinus Torvalds 			goto error1;
9101da177e4SLinus Torvalds 		}
9111da177e4SLinus Torvalds 	}
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 	/*
9141da177e4SLinus Torvalds 	 * Initialize realtime fields in the mount structure
9151da177e4SLinus Torvalds 	 */
9161da177e4SLinus Torvalds 	if ((error = xfs_rtmount_init(mp))) {
9171da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: RT mount failed");
9181da177e4SLinus Torvalds 		goto error1;
9191da177e4SLinus Torvalds 	}
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds 	/*
9221da177e4SLinus Torvalds 	 * For client case we are done now
9231da177e4SLinus Torvalds 	 */
9241da177e4SLinus Torvalds 	if (mfsi_flags & XFS_MFSI_CLIENT) {
925014c2544SJesper Juhl 		return 0;
9261da177e4SLinus Torvalds 	}
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	/*
9291da177e4SLinus Torvalds 	 *  Copies the low order bits of the timestamp and the randomly
9301da177e4SLinus Torvalds 	 *  set "sequence" number out of a UUID.
9311da177e4SLinus Torvalds 	 */
9321da177e4SLinus Torvalds 	uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds 	/*
9351da177e4SLinus Torvalds 	 *  The vfs structure needs to have a file system independent
9361da177e4SLinus Torvalds 	 *  way of checking for the invariant file system ID.  Since it
9371da177e4SLinus Torvalds 	 *  can't look at mount structures it has a pointer to the data
9381da177e4SLinus Torvalds 	 *  in the mount structure.
9391da177e4SLinus Torvalds 	 *
9401da177e4SLinus Torvalds 	 *  File systems that don't support user level file handles (i.e.
9411da177e4SLinus Torvalds 	 *  all of them except for XFS) will leave vfs_altfsid as NULL.
9421da177e4SLinus Torvalds 	 */
9431da177e4SLinus Torvalds 	vfsp->vfs_altfsid = (xfs_fsid_t *)mp->m_fixedfsid;
9441da177e4SLinus Torvalds 	mp->m_dmevmask = 0;	/* not persistent; set after each mount */
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	/*
9471da177e4SLinus Torvalds 	 * Select the right directory manager.
9481da177e4SLinus Torvalds 	 */
9491da177e4SLinus Torvalds 	mp->m_dirops =
9501da177e4SLinus Torvalds 		XFS_SB_VERSION_HASDIRV2(&mp->m_sb) ?
9511da177e4SLinus Torvalds 			xfsv2_dirops :
9521da177e4SLinus Torvalds 			xfsv1_dirops;
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 	/*
9551da177e4SLinus Torvalds 	 * Initialize directory manager's entries.
9561da177e4SLinus Torvalds 	 */
9571da177e4SLinus Torvalds 	XFS_DIR_MOUNT(mp);
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 	/*
9601da177e4SLinus Torvalds 	 * Initialize the attribute manager's entries.
9611da177e4SLinus Torvalds 	 */
9621da177e4SLinus Torvalds 	mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	/*
9651da177e4SLinus Torvalds 	 * Initialize the precomputed transaction reservations values.
9661da177e4SLinus Torvalds 	 */
9671da177e4SLinus Torvalds 	xfs_trans_init(mp);
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	/*
9701da177e4SLinus Torvalds 	 * Allocate and initialize the inode hash table for this
9711da177e4SLinus Torvalds 	 * file system.
9721da177e4SLinus Torvalds 	 */
9731da177e4SLinus Torvalds 	xfs_ihash_init(mp);
9741da177e4SLinus Torvalds 	xfs_chash_init(mp);
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 	/*
9771da177e4SLinus Torvalds 	 * Allocate and initialize the per-ag data.
9781da177e4SLinus Torvalds 	 */
9791da177e4SLinus Torvalds 	init_rwsem(&mp->m_peraglock);
9801da177e4SLinus Torvalds 	mp->m_perag =
9811da177e4SLinus Torvalds 		kmem_zalloc(sbp->sb_agcount * sizeof(xfs_perag_t), KM_SLEEP);
9821da177e4SLinus Torvalds 
983c11e2c36SNathan Scott 	mp->m_maxagi = xfs_initialize_perag(vfsp, mp, sbp->sb_agcount);
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 	/*
9861da177e4SLinus Torvalds 	 * log's mount-time initialization. Perform 1st part recovery if needed
9871da177e4SLinus Torvalds 	 */
9881da177e4SLinus Torvalds 	if (likely(sbp->sb_logblocks > 0)) {	/* check for volume case */
9891da177e4SLinus Torvalds 		error = xfs_log_mount(mp, mp->m_logdev_targp,
9901da177e4SLinus Torvalds 				      XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
9911da177e4SLinus Torvalds 				      XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
9921da177e4SLinus Torvalds 		if (error) {
9931da177e4SLinus Torvalds 			cmn_err(CE_WARN, "XFS: log mount failed");
9941da177e4SLinus Torvalds 			goto error2;
9951da177e4SLinus Torvalds 		}
9961da177e4SLinus Torvalds 	} else {	/* No log has been defined */
9971da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: no log defined");
9981da177e4SLinus Torvalds 		XFS_ERROR_REPORT("xfs_mountfs_int(1)", XFS_ERRLEVEL_LOW, mp);
9991da177e4SLinus Torvalds 		error = XFS_ERROR(EFSCORRUPTED);
10001da177e4SLinus Torvalds 		goto error2;
10011da177e4SLinus Torvalds 	}
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds 	/*
10041da177e4SLinus Torvalds 	 * Get and sanity-check the root inode.
10051da177e4SLinus Torvalds 	 * Save the pointer to it in the mount structure.
10061da177e4SLinus Torvalds 	 */
10071da177e4SLinus Torvalds 	error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip, 0);
10081da177e4SLinus Torvalds 	if (error) {
10091da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: failed to read root inode");
10101da177e4SLinus Torvalds 		goto error3;
10111da177e4SLinus Torvalds 	}
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds 	ASSERT(rip != NULL);
10141da177e4SLinus Torvalds 	rvp = XFS_ITOV(rip);
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 	if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) {
10171da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: corrupted root inode");
10181da177e4SLinus Torvalds 		prdev("Root inode %llu is not a directory",
10191da177e4SLinus Torvalds 		      mp->m_ddev_targp, (unsigned long long)rip->i_ino);
10201da177e4SLinus Torvalds 		xfs_iunlock(rip, XFS_ILOCK_EXCL);
10211da177e4SLinus Torvalds 		XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
10221da177e4SLinus Torvalds 				 mp);
10231da177e4SLinus Torvalds 		error = XFS_ERROR(EFSCORRUPTED);
10241da177e4SLinus Torvalds 		goto error4;
10251da177e4SLinus Torvalds 	}
10261da177e4SLinus Torvalds 	mp->m_rootip = rip;	/* save it */
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 	xfs_iunlock(rip, XFS_ILOCK_EXCL);
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 	/*
10311da177e4SLinus Torvalds 	 * Initialize realtime inode pointers in the mount structure
10321da177e4SLinus Torvalds 	 */
10331da177e4SLinus Torvalds 	if ((error = xfs_rtmount_inodes(mp))) {
10341da177e4SLinus Torvalds 		/*
10351da177e4SLinus Torvalds 		 * Free up the root inode.
10361da177e4SLinus Torvalds 		 */
10371da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: failed to read RT inodes");
10381da177e4SLinus Torvalds 		goto error4;
10391da177e4SLinus Torvalds 	}
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds 	/*
10421da177e4SLinus Torvalds 	 * If fs is not mounted readonly, then update the superblock
10431da177e4SLinus Torvalds 	 * unit and width changes.
10441da177e4SLinus Torvalds 	 */
10451da177e4SLinus Torvalds 	if (update_flags && !(vfsp->vfs_flag & VFS_RDONLY))
10461da177e4SLinus Torvalds 		xfs_mount_log_sbunit(mp, update_flags);
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds 	/*
10491da177e4SLinus Torvalds 	 * Initialise the XFS quota management subsystem for this mount
10501da177e4SLinus Torvalds 	 */
10511da177e4SLinus Torvalds 	if ((error = XFS_QM_INIT(mp, &quotamount, &quotaflags)))
10521da177e4SLinus Torvalds 		goto error4;
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds 	/*
10551da177e4SLinus Torvalds 	 * Finish recovering the file system.  This part needed to be
10561da177e4SLinus Torvalds 	 * delayed until after the root and real-time bitmap inodes
10571da177e4SLinus Torvalds 	 * were consistently read in.
10581da177e4SLinus Torvalds 	 */
10591da177e4SLinus Torvalds 	error = xfs_log_mount_finish(mp, mfsi_flags);
10601da177e4SLinus Torvalds 	if (error) {
10611da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: log mount finish failed");
10621da177e4SLinus Torvalds 		goto error4;
10631da177e4SLinus Torvalds 	}
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	/*
10661da177e4SLinus Torvalds 	 * Complete the quota initialisation, post-log-replay component.
10671da177e4SLinus Torvalds 	 */
10681da177e4SLinus Torvalds 	if ((error = XFS_QM_MOUNT(mp, quotamount, quotaflags, mfsi_flags)))
10691da177e4SLinus Torvalds 		goto error4;
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds 	return 0;
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds  error4:
10741da177e4SLinus Torvalds 	/*
10751da177e4SLinus Torvalds 	 * Free up the root inode.
10761da177e4SLinus Torvalds 	 */
10771da177e4SLinus Torvalds 	VN_RELE(rvp);
10781da177e4SLinus Torvalds  error3:
10791da177e4SLinus Torvalds 	xfs_log_unmount_dealloc(mp);
10801da177e4SLinus Torvalds  error2:
10811da177e4SLinus Torvalds 	xfs_ihash_free(mp);
10821da177e4SLinus Torvalds 	xfs_chash_free(mp);
10831da177e4SLinus Torvalds 	for (agno = 0; agno < sbp->sb_agcount; agno++)
10841da177e4SLinus Torvalds 		if (mp->m_perag[agno].pagb_list)
10851da177e4SLinus Torvalds 			kmem_free(mp->m_perag[agno].pagb_list,
10861da177e4SLinus Torvalds 			  sizeof(xfs_perag_busy_t) * XFS_PAGB_NUM_SLOTS);
10871da177e4SLinus Torvalds 	kmem_free(mp->m_perag, sbp->sb_agcount * sizeof(xfs_perag_t));
10881da177e4SLinus Torvalds 	mp->m_perag = NULL;
10891da177e4SLinus Torvalds 	/* FALLTHROUGH */
10901da177e4SLinus Torvalds  error1:
10911da177e4SLinus Torvalds 	if (uuid_mounted)
10921da177e4SLinus Torvalds 		xfs_uuid_unmount(mp);
10931da177e4SLinus Torvalds 	xfs_freesb(mp);
10941da177e4SLinus Torvalds 	return error;
10951da177e4SLinus Torvalds }
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds /*
10981da177e4SLinus Torvalds  * xfs_unmountfs
10991da177e4SLinus Torvalds  *
11001da177e4SLinus Torvalds  * This flushes out the inodes,dquots and the superblock, unmounts the
11011da177e4SLinus Torvalds  * log and makes sure that incore structures are freed.
11021da177e4SLinus Torvalds  */
11031da177e4SLinus Torvalds int
11041da177e4SLinus Torvalds xfs_unmountfs(xfs_mount_t *mp, struct cred *cr)
11051da177e4SLinus Torvalds {
11061da177e4SLinus Torvalds 	struct vfs	*vfsp = XFS_MTOVFS(mp);
11071da177e4SLinus Torvalds #if defined(DEBUG) || defined(INDUCE_IO_ERROR)
11081da177e4SLinus Torvalds 	int64_t		fsid;
11091da177e4SLinus Torvalds #endif
11101da177e4SLinus Torvalds 
1111efa80278SChristoph Hellwig 	xfs_iflush_all(mp);
11121da177e4SLinus Torvalds 
1113ee2a4f7cSNathan Scott 	XFS_QM_DQPURGEALL(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_UMOUNTING);
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 	/*
11161da177e4SLinus Torvalds 	 * Flush out the log synchronously so that we know for sure
11171da177e4SLinus Torvalds 	 * that nothing is pinned.  This is important because bflush()
11181da177e4SLinus Torvalds 	 * will skip pinned buffers.
11191da177e4SLinus Torvalds 	 */
11201da177e4SLinus Torvalds 	xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	xfs_binval(mp->m_ddev_targp);
11231da177e4SLinus Torvalds 	if (mp->m_rtdev_targp) {
11241da177e4SLinus Torvalds 		xfs_binval(mp->m_rtdev_targp);
11251da177e4SLinus Torvalds 	}
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds 	xfs_unmountfs_writesb(mp);
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 	xfs_unmountfs_wait(mp); 		/* wait for async bufs */
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 	xfs_log_unmount(mp);			/* Done! No more fs ops. */
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds 	xfs_freesb(mp);
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 	/*
11361da177e4SLinus Torvalds 	 * All inodes from this mount point should be freed.
11371da177e4SLinus Torvalds 	 */
11381da177e4SLinus Torvalds 	ASSERT(mp->m_inodes == NULL);
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 	xfs_unmountfs_close(mp, cr);
11411da177e4SLinus Torvalds 	if ((mp->m_flags & XFS_MOUNT_NOUUID) == 0)
11421da177e4SLinus Torvalds 		xfs_uuid_unmount(mp);
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds #if defined(DEBUG) || defined(INDUCE_IO_ERROR)
11451da177e4SLinus Torvalds 	/*
11461da177e4SLinus Torvalds 	 * clear all error tags on this filesystem
11471da177e4SLinus Torvalds 	 */
11481da177e4SLinus Torvalds 	memcpy(&fsid, &vfsp->vfs_fsid, sizeof(int64_t));
11491da177e4SLinus Torvalds 	xfs_errortag_clearall_umount(fsid, mp->m_fsname, 0);
11501da177e4SLinus Torvalds #endif
11511da177e4SLinus Torvalds 	XFS_IODONE(vfsp);
11521da177e4SLinus Torvalds 	xfs_mount_free(mp, 1);
11531da177e4SLinus Torvalds 	return 0;
11541da177e4SLinus Torvalds }
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds void
11571da177e4SLinus Torvalds xfs_unmountfs_close(xfs_mount_t *mp, struct cred *cr)
11581da177e4SLinus Torvalds {
11591da177e4SLinus Torvalds 	if (mp->m_logdev_targp != mp->m_ddev_targp)
11601da177e4SLinus Torvalds 		xfs_free_buftarg(mp->m_logdev_targp, 1);
11611da177e4SLinus Torvalds 	if (mp->m_rtdev_targp)
11621da177e4SLinus Torvalds 		xfs_free_buftarg(mp->m_rtdev_targp, 1);
11631da177e4SLinus Torvalds 	xfs_free_buftarg(mp->m_ddev_targp, 0);
11641da177e4SLinus Torvalds }
11651da177e4SLinus Torvalds 
1166ba0f32d4SChristoph Hellwig STATIC void
11671da177e4SLinus Torvalds xfs_unmountfs_wait(xfs_mount_t *mp)
11681da177e4SLinus Torvalds {
11691da177e4SLinus Torvalds 	if (mp->m_logdev_targp != mp->m_ddev_targp)
11701da177e4SLinus Torvalds 		xfs_wait_buftarg(mp->m_logdev_targp);
11711da177e4SLinus Torvalds 	if (mp->m_rtdev_targp)
11721da177e4SLinus Torvalds 		xfs_wait_buftarg(mp->m_rtdev_targp);
11731da177e4SLinus Torvalds 	xfs_wait_buftarg(mp->m_ddev_targp);
11741da177e4SLinus Torvalds }
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds int
11771da177e4SLinus Torvalds xfs_unmountfs_writesb(xfs_mount_t *mp)
11781da177e4SLinus Torvalds {
11791da177e4SLinus Torvalds 	xfs_buf_t	*sbp;
11801da177e4SLinus Torvalds 	xfs_sb_t	*sb;
11811da177e4SLinus Torvalds 	int		error = 0;
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 	/*
11841da177e4SLinus Torvalds 	 * skip superblock write if fs is read-only, or
11851da177e4SLinus Torvalds 	 * if we are doing a forced umount.
11861da177e4SLinus Torvalds 	 */
11871da177e4SLinus Torvalds 	sbp = xfs_getsb(mp, 0);
11881da177e4SLinus Torvalds 	if (!(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY ||
11891da177e4SLinus Torvalds 		XFS_FORCED_SHUTDOWN(mp))) {
11908d280b98SDavid Chinner 
11918d280b98SDavid Chinner 		xfs_icsb_sync_counters(mp);
11928d280b98SDavid Chinner 
11931da177e4SLinus Torvalds 		/*
11941da177e4SLinus Torvalds 		 * mark shared-readonly if desired
11951da177e4SLinus Torvalds 		 */
11961da177e4SLinus Torvalds 		sb = XFS_BUF_TO_SBP(sbp);
11971da177e4SLinus Torvalds 		if (mp->m_mk_sharedro) {
11981da177e4SLinus Torvalds 			if (!(sb->sb_flags & XFS_SBF_READONLY))
11991da177e4SLinus Torvalds 				sb->sb_flags |= XFS_SBF_READONLY;
12001da177e4SLinus Torvalds 			if (!XFS_SB_VERSION_HASSHARED(sb))
12011da177e4SLinus Torvalds 				XFS_SB_VERSION_ADDSHARED(sb);
12021da177e4SLinus Torvalds 			xfs_fs_cmn_err(CE_NOTE, mp,
12031da177e4SLinus Torvalds 				"Unmounting, marking shared read-only");
12041da177e4SLinus Torvalds 		}
12051da177e4SLinus Torvalds 		XFS_BUF_UNDONE(sbp);
12061da177e4SLinus Torvalds 		XFS_BUF_UNREAD(sbp);
12071da177e4SLinus Torvalds 		XFS_BUF_UNDELAYWRITE(sbp);
12081da177e4SLinus Torvalds 		XFS_BUF_WRITE(sbp);
12091da177e4SLinus Torvalds 		XFS_BUF_UNASYNC(sbp);
12101da177e4SLinus Torvalds 		ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp);
12111da177e4SLinus Torvalds 		xfsbdstrat(mp, sbp);
12121da177e4SLinus Torvalds 		/* Nevermind errors we might get here. */
12131da177e4SLinus Torvalds 		error = xfs_iowait(sbp);
12141da177e4SLinus Torvalds 		if (error)
12151da177e4SLinus Torvalds 			xfs_ioerror_alert("xfs_unmountfs_writesb",
12161da177e4SLinus Torvalds 					  mp, sbp, XFS_BUF_ADDR(sbp));
12171da177e4SLinus Torvalds 		if (error && mp->m_mk_sharedro)
12181da177e4SLinus Torvalds 			xfs_fs_cmn_err(CE_ALERT, mp, "Superblock write error detected while unmounting.  Filesystem may not be marked shared readonly");
12191da177e4SLinus Torvalds 	}
12201da177e4SLinus Torvalds 	xfs_buf_relse(sbp);
1221014c2544SJesper Juhl 	return error;
12221da177e4SLinus Torvalds }
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds /*
12251da177e4SLinus Torvalds  * xfs_mod_sb() can be used to copy arbitrary changes to the
12261da177e4SLinus Torvalds  * in-core superblock into the superblock buffer to be logged.
12271da177e4SLinus Torvalds  * It does not provide the higher level of locking that is
12281da177e4SLinus Torvalds  * needed to protect the in-core superblock from concurrent
12291da177e4SLinus Torvalds  * access.
12301da177e4SLinus Torvalds  */
12311da177e4SLinus Torvalds void
12321da177e4SLinus Torvalds xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
12331da177e4SLinus Torvalds {
12341da177e4SLinus Torvalds 	xfs_buf_t	*bp;
12351da177e4SLinus Torvalds 	int		first;
12361da177e4SLinus Torvalds 	int		last;
12371da177e4SLinus Torvalds 	xfs_mount_t	*mp;
12381da177e4SLinus Torvalds 	xfs_sb_t	*sbp;
12391da177e4SLinus Torvalds 	xfs_sb_field_t	f;
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 	ASSERT(fields);
12421da177e4SLinus Torvalds 	if (!fields)
12431da177e4SLinus Torvalds 		return;
12441da177e4SLinus Torvalds 	mp = tp->t_mountp;
12451da177e4SLinus Torvalds 	bp = xfs_trans_getsb(tp, mp, 0);
12461da177e4SLinus Torvalds 	sbp = XFS_BUF_TO_SBP(bp);
12471da177e4SLinus Torvalds 	first = sizeof(xfs_sb_t);
12481da177e4SLinus Torvalds 	last = 0;
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds 	/* translate/copy */
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds 	xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), -1, fields);
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds 	/* find modified range */
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 	f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
12571da177e4SLinus Torvalds 	ASSERT((1LL << f) & XFS_SB_MOD_BITS);
12581da177e4SLinus Torvalds 	first = xfs_sb_info[f].offset;
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds 	f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
12611da177e4SLinus Torvalds 	ASSERT((1LL << f) & XFS_SB_MOD_BITS);
12621da177e4SLinus Torvalds 	last = xfs_sb_info[f + 1].offset - 1;
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds 	xfs_trans_log_buf(tp, bp, first, last);
12651da177e4SLinus Torvalds }
12661da177e4SLinus Torvalds /*
12671da177e4SLinus Torvalds  * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
12681da177e4SLinus Torvalds  * a delta to a specified field in the in-core superblock.  Simply
12691da177e4SLinus Torvalds  * switch on the field indicated and apply the delta to that field.
12701da177e4SLinus Torvalds  * Fields are not allowed to dip below zero, so if the delta would
12711da177e4SLinus Torvalds  * do this do not apply it and return EINVAL.
12721da177e4SLinus Torvalds  *
12731da177e4SLinus Torvalds  * The SB_LOCK must be held when this routine is called.
12741da177e4SLinus Torvalds  */
12758d280b98SDavid Chinner int
12761da177e4SLinus Torvalds xfs_mod_incore_sb_unlocked(xfs_mount_t *mp, xfs_sb_field_t field,
12771da177e4SLinus Torvalds 			int delta, int rsvd)
12781da177e4SLinus Torvalds {
12791da177e4SLinus Torvalds 	int		scounter;	/* short counter for 32 bit fields */
12801da177e4SLinus Torvalds 	long long	lcounter;	/* long counter for 64 bit fields */
12811da177e4SLinus Torvalds 	long long	res_used, rem;
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	/*
12841da177e4SLinus Torvalds 	 * With the in-core superblock spin lock held, switch
12851da177e4SLinus Torvalds 	 * on the indicated field.  Apply the delta to the
12861da177e4SLinus Torvalds 	 * proper field.  If the fields value would dip below
12871da177e4SLinus Torvalds 	 * 0, then do not apply the delta and return EINVAL.
12881da177e4SLinus Torvalds 	 */
12891da177e4SLinus Torvalds 	switch (field) {
12901da177e4SLinus Torvalds 	case XFS_SBS_ICOUNT:
12911da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_icount;
12921da177e4SLinus Torvalds 		lcounter += delta;
12931da177e4SLinus Torvalds 		if (lcounter < 0) {
12941da177e4SLinus Torvalds 			ASSERT(0);
1295014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
12961da177e4SLinus Torvalds 		}
12971da177e4SLinus Torvalds 		mp->m_sb.sb_icount = lcounter;
1298014c2544SJesper Juhl 		return 0;
12991da177e4SLinus Torvalds 	case XFS_SBS_IFREE:
13001da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_ifree;
13011da177e4SLinus Torvalds 		lcounter += delta;
13021da177e4SLinus Torvalds 		if (lcounter < 0) {
13031da177e4SLinus Torvalds 			ASSERT(0);
1304014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
13051da177e4SLinus Torvalds 		}
13061da177e4SLinus Torvalds 		mp->m_sb.sb_ifree = lcounter;
1307014c2544SJesper Juhl 		return 0;
13081da177e4SLinus Torvalds 	case XFS_SBS_FDBLOCKS:
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_fdblocks;
13111da177e4SLinus Torvalds 		res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds 		if (delta > 0) {		/* Putting blocks back */
13141da177e4SLinus Torvalds 			if (res_used > delta) {
13151da177e4SLinus Torvalds 				mp->m_resblks_avail += delta;
13161da177e4SLinus Torvalds 			} else {
13171da177e4SLinus Torvalds 				rem = delta - res_used;
13181da177e4SLinus Torvalds 				mp->m_resblks_avail = mp->m_resblks;
13191da177e4SLinus Torvalds 				lcounter += rem;
13201da177e4SLinus Torvalds 			}
13211da177e4SLinus Torvalds 		} else {				/* Taking blocks away */
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 			lcounter += delta;
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 		/*
13261da177e4SLinus Torvalds 		 * If were out of blocks, use any available reserved blocks if
13271da177e4SLinus Torvalds 		 * were allowed to.
13281da177e4SLinus Torvalds 		 */
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds 			if (lcounter < 0) {
13311da177e4SLinus Torvalds 				if (rsvd) {
13321da177e4SLinus Torvalds 					lcounter = (long long)mp->m_resblks_avail + delta;
13331da177e4SLinus Torvalds 					if (lcounter < 0) {
1334014c2544SJesper Juhl 						return XFS_ERROR(ENOSPC);
13351da177e4SLinus Torvalds 					}
13361da177e4SLinus Torvalds 					mp->m_resblks_avail = lcounter;
1337014c2544SJesper Juhl 					return 0;
13381da177e4SLinus Torvalds 				} else {	/* not reserved */
1339014c2544SJesper Juhl 					return XFS_ERROR(ENOSPC);
13401da177e4SLinus Torvalds 				}
13411da177e4SLinus Torvalds 			}
13421da177e4SLinus Torvalds 		}
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds 		mp->m_sb.sb_fdblocks = lcounter;
1345014c2544SJesper Juhl 		return 0;
13461da177e4SLinus Torvalds 	case XFS_SBS_FREXTENTS:
13471da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_frextents;
13481da177e4SLinus Torvalds 		lcounter += delta;
13491da177e4SLinus Torvalds 		if (lcounter < 0) {
1350014c2544SJesper Juhl 			return XFS_ERROR(ENOSPC);
13511da177e4SLinus Torvalds 		}
13521da177e4SLinus Torvalds 		mp->m_sb.sb_frextents = lcounter;
1353014c2544SJesper Juhl 		return 0;
13541da177e4SLinus Torvalds 	case XFS_SBS_DBLOCKS:
13551da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_dblocks;
13561da177e4SLinus Torvalds 		lcounter += delta;
13571da177e4SLinus Torvalds 		if (lcounter < 0) {
13581da177e4SLinus Torvalds 			ASSERT(0);
1359014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
13601da177e4SLinus Torvalds 		}
13611da177e4SLinus Torvalds 		mp->m_sb.sb_dblocks = lcounter;
1362014c2544SJesper Juhl 		return 0;
13631da177e4SLinus Torvalds 	case XFS_SBS_AGCOUNT:
13641da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_agcount;
13651da177e4SLinus Torvalds 		scounter += delta;
13661da177e4SLinus Torvalds 		if (scounter < 0) {
13671da177e4SLinus Torvalds 			ASSERT(0);
1368014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
13691da177e4SLinus Torvalds 		}
13701da177e4SLinus Torvalds 		mp->m_sb.sb_agcount = scounter;
1371014c2544SJesper Juhl 		return 0;
13721da177e4SLinus Torvalds 	case XFS_SBS_IMAX_PCT:
13731da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_imax_pct;
13741da177e4SLinus Torvalds 		scounter += delta;
13751da177e4SLinus Torvalds 		if (scounter < 0) {
13761da177e4SLinus Torvalds 			ASSERT(0);
1377014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
13781da177e4SLinus Torvalds 		}
13791da177e4SLinus Torvalds 		mp->m_sb.sb_imax_pct = scounter;
1380014c2544SJesper Juhl 		return 0;
13811da177e4SLinus Torvalds 	case XFS_SBS_REXTSIZE:
13821da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rextsize;
13831da177e4SLinus Torvalds 		scounter += delta;
13841da177e4SLinus Torvalds 		if (scounter < 0) {
13851da177e4SLinus Torvalds 			ASSERT(0);
1386014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
13871da177e4SLinus Torvalds 		}
13881da177e4SLinus Torvalds 		mp->m_sb.sb_rextsize = scounter;
1389014c2544SJesper Juhl 		return 0;
13901da177e4SLinus Torvalds 	case XFS_SBS_RBMBLOCKS:
13911da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rbmblocks;
13921da177e4SLinus Torvalds 		scounter += delta;
13931da177e4SLinus Torvalds 		if (scounter < 0) {
13941da177e4SLinus Torvalds 			ASSERT(0);
1395014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
13961da177e4SLinus Torvalds 		}
13971da177e4SLinus Torvalds 		mp->m_sb.sb_rbmblocks = scounter;
1398014c2544SJesper Juhl 		return 0;
13991da177e4SLinus Torvalds 	case XFS_SBS_RBLOCKS:
14001da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_rblocks;
14011da177e4SLinus Torvalds 		lcounter += delta;
14021da177e4SLinus Torvalds 		if (lcounter < 0) {
14031da177e4SLinus Torvalds 			ASSERT(0);
1404014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
14051da177e4SLinus Torvalds 		}
14061da177e4SLinus Torvalds 		mp->m_sb.sb_rblocks = lcounter;
1407014c2544SJesper Juhl 		return 0;
14081da177e4SLinus Torvalds 	case XFS_SBS_REXTENTS:
14091da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_rextents;
14101da177e4SLinus Torvalds 		lcounter += delta;
14111da177e4SLinus Torvalds 		if (lcounter < 0) {
14121da177e4SLinus Torvalds 			ASSERT(0);
1413014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
14141da177e4SLinus Torvalds 		}
14151da177e4SLinus Torvalds 		mp->m_sb.sb_rextents = lcounter;
1416014c2544SJesper Juhl 		return 0;
14171da177e4SLinus Torvalds 	case XFS_SBS_REXTSLOG:
14181da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rextslog;
14191da177e4SLinus Torvalds 		scounter += delta;
14201da177e4SLinus Torvalds 		if (scounter < 0) {
14211da177e4SLinus Torvalds 			ASSERT(0);
1422014c2544SJesper Juhl 			return XFS_ERROR(EINVAL);
14231da177e4SLinus Torvalds 		}
14241da177e4SLinus Torvalds 		mp->m_sb.sb_rextslog = scounter;
1425014c2544SJesper Juhl 		return 0;
14261da177e4SLinus Torvalds 	default:
14271da177e4SLinus Torvalds 		ASSERT(0);
1428014c2544SJesper Juhl 		return XFS_ERROR(EINVAL);
14291da177e4SLinus Torvalds 	}
14301da177e4SLinus Torvalds }
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds /*
14331da177e4SLinus Torvalds  * xfs_mod_incore_sb() is used to change a field in the in-core
14341da177e4SLinus Torvalds  * superblock structure by the specified delta.  This modification
14351da177e4SLinus Torvalds  * is protected by the SB_LOCK.  Just use the xfs_mod_incore_sb_unlocked()
14361da177e4SLinus Torvalds  * routine to do the work.
14371da177e4SLinus Torvalds  */
14381da177e4SLinus Torvalds int
14391da177e4SLinus Torvalds xfs_mod_incore_sb(xfs_mount_t *mp, xfs_sb_field_t field, int delta, int rsvd)
14401da177e4SLinus Torvalds {
14411da177e4SLinus Torvalds 	unsigned long	s;
14421da177e4SLinus Torvalds 	int	status;
14431da177e4SLinus Torvalds 
14448d280b98SDavid Chinner 	/* check for per-cpu counters */
14458d280b98SDavid Chinner 	switch (field) {
14468d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
14478d280b98SDavid Chinner 	case XFS_SBS_ICOUNT:
14488d280b98SDavid Chinner 	case XFS_SBS_IFREE:
14498d280b98SDavid Chinner 	case XFS_SBS_FDBLOCKS:
14508d280b98SDavid Chinner 		if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
14518d280b98SDavid Chinner 			status = xfs_icsb_modify_counters(mp, field,
14528d280b98SDavid Chinner 							delta, rsvd);
14538d280b98SDavid Chinner 			break;
14548d280b98SDavid Chinner 		}
14558d280b98SDavid Chinner 		/* FALLTHROUGH */
14568d280b98SDavid Chinner #endif
14578d280b98SDavid Chinner 	default:
14581da177e4SLinus Torvalds 		s = XFS_SB_LOCK(mp);
14591da177e4SLinus Torvalds 		status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
14601da177e4SLinus Torvalds 		XFS_SB_UNLOCK(mp, s);
14618d280b98SDavid Chinner 		break;
14628d280b98SDavid Chinner 	}
14638d280b98SDavid Chinner 
1464014c2544SJesper Juhl 	return status;
14651da177e4SLinus Torvalds }
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds /*
14681da177e4SLinus Torvalds  * xfs_mod_incore_sb_batch() is used to change more than one field
14691da177e4SLinus Torvalds  * in the in-core superblock structure at a time.  This modification
14701da177e4SLinus Torvalds  * is protected by a lock internal to this module.  The fields and
14711da177e4SLinus Torvalds  * changes to those fields are specified in the array of xfs_mod_sb
14721da177e4SLinus Torvalds  * structures passed in.
14731da177e4SLinus Torvalds  *
14741da177e4SLinus Torvalds  * Either all of the specified deltas will be applied or none of
14751da177e4SLinus Torvalds  * them will.  If any modified field dips below 0, then all modifications
14761da177e4SLinus Torvalds  * will be backed out and EINVAL will be returned.
14771da177e4SLinus Torvalds  */
14781da177e4SLinus Torvalds int
14791da177e4SLinus Torvalds xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
14801da177e4SLinus Torvalds {
14811da177e4SLinus Torvalds 	unsigned long	s;
14821da177e4SLinus Torvalds 	int		status=0;
14831da177e4SLinus Torvalds 	xfs_mod_sb_t	*msbp;
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds 	/*
14861da177e4SLinus Torvalds 	 * Loop through the array of mod structures and apply each
14871da177e4SLinus Torvalds 	 * individually.  If any fail, then back out all those
14881da177e4SLinus Torvalds 	 * which have already been applied.  Do all of this within
14891da177e4SLinus Torvalds 	 * the scope of the SB_LOCK so that all of the changes will
14901da177e4SLinus Torvalds 	 * be atomic.
14911da177e4SLinus Torvalds 	 */
14921da177e4SLinus Torvalds 	s = XFS_SB_LOCK(mp);
14931da177e4SLinus Torvalds 	msbp = &msb[0];
14941da177e4SLinus Torvalds 	for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) {
14951da177e4SLinus Torvalds 		/*
14961da177e4SLinus Torvalds 		 * Apply the delta at index n.  If it fails, break
14971da177e4SLinus Torvalds 		 * from the loop so we'll fall into the undo loop
14981da177e4SLinus Torvalds 		 * below.
14991da177e4SLinus Torvalds 		 */
15008d280b98SDavid Chinner 		switch (msbp->msb_field) {
15018d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
15028d280b98SDavid Chinner 		case XFS_SBS_ICOUNT:
15038d280b98SDavid Chinner 		case XFS_SBS_IFREE:
15048d280b98SDavid Chinner 		case XFS_SBS_FDBLOCKS:
15058d280b98SDavid Chinner 			if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
15068d280b98SDavid Chinner 				status = xfs_icsb_modify_counters_locked(mp,
15078d280b98SDavid Chinner 							msbp->msb_field,
15081da177e4SLinus Torvalds 							msbp->msb_delta, rsvd);
15098d280b98SDavid Chinner 				break;
15108d280b98SDavid Chinner 			}
15118d280b98SDavid Chinner 			/* FALLTHROUGH */
15128d280b98SDavid Chinner #endif
15138d280b98SDavid Chinner 		default:
15148d280b98SDavid Chinner 			status = xfs_mod_incore_sb_unlocked(mp,
15158d280b98SDavid Chinner 						msbp->msb_field,
15168d280b98SDavid Chinner 						msbp->msb_delta, rsvd);
15178d280b98SDavid Chinner 			break;
15188d280b98SDavid Chinner 		}
15198d280b98SDavid Chinner 
15201da177e4SLinus Torvalds 		if (status != 0) {
15211da177e4SLinus Torvalds 			break;
15221da177e4SLinus Torvalds 		}
15231da177e4SLinus Torvalds 	}
15241da177e4SLinus Torvalds 
15251da177e4SLinus Torvalds 	/*
15261da177e4SLinus Torvalds 	 * If we didn't complete the loop above, then back out
15271da177e4SLinus Torvalds 	 * any changes made to the superblock.  If you add code
15281da177e4SLinus Torvalds 	 * between the loop above and here, make sure that you
15291da177e4SLinus Torvalds 	 * preserve the value of status. Loop back until
15301da177e4SLinus Torvalds 	 * we step below the beginning of the array.  Make sure
15311da177e4SLinus Torvalds 	 * we don't touch anything back there.
15321da177e4SLinus Torvalds 	 */
15331da177e4SLinus Torvalds 	if (status != 0) {
15341da177e4SLinus Torvalds 		msbp--;
15351da177e4SLinus Torvalds 		while (msbp >= msb) {
15368d280b98SDavid Chinner 			switch (msbp->msb_field) {
15378d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
15388d280b98SDavid Chinner 			case XFS_SBS_ICOUNT:
15398d280b98SDavid Chinner 			case XFS_SBS_IFREE:
15408d280b98SDavid Chinner 			case XFS_SBS_FDBLOCKS:
15418d280b98SDavid Chinner 				if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
15428d280b98SDavid Chinner 					status =
15438d280b98SDavid Chinner 					    xfs_icsb_modify_counters_locked(mp,
15448d280b98SDavid Chinner 							msbp->msb_field,
15458d280b98SDavid Chinner 							-(msbp->msb_delta),
15468d280b98SDavid Chinner 							rsvd);
15478d280b98SDavid Chinner 					break;
15488d280b98SDavid Chinner 				}
15498d280b98SDavid Chinner 				/* FALLTHROUGH */
15508d280b98SDavid Chinner #endif
15518d280b98SDavid Chinner 			default:
15521da177e4SLinus Torvalds 				status = xfs_mod_incore_sb_unlocked(mp,
15538d280b98SDavid Chinner 							msbp->msb_field,
15548d280b98SDavid Chinner 							-(msbp->msb_delta),
15558d280b98SDavid Chinner 							rsvd);
15568d280b98SDavid Chinner 				break;
15578d280b98SDavid Chinner 			}
15581da177e4SLinus Torvalds 			ASSERT(status == 0);
15591da177e4SLinus Torvalds 			msbp--;
15601da177e4SLinus Torvalds 		}
15611da177e4SLinus Torvalds 	}
15621da177e4SLinus Torvalds 	XFS_SB_UNLOCK(mp, s);
1563014c2544SJesper Juhl 	return status;
15641da177e4SLinus Torvalds }
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds /*
15671da177e4SLinus Torvalds  * xfs_getsb() is called to obtain the buffer for the superblock.
15681da177e4SLinus Torvalds  * The buffer is returned locked and read in from disk.
15691da177e4SLinus Torvalds  * The buffer should be released with a call to xfs_brelse().
15701da177e4SLinus Torvalds  *
15711da177e4SLinus Torvalds  * If the flags parameter is BUF_TRYLOCK, then we'll only return
15721da177e4SLinus Torvalds  * the superblock buffer if it can be locked without sleeping.
15731da177e4SLinus Torvalds  * If it can't then we'll return NULL.
15741da177e4SLinus Torvalds  */
15751da177e4SLinus Torvalds xfs_buf_t *
15761da177e4SLinus Torvalds xfs_getsb(
15771da177e4SLinus Torvalds 	xfs_mount_t	*mp,
15781da177e4SLinus Torvalds 	int		flags)
15791da177e4SLinus Torvalds {
15801da177e4SLinus Torvalds 	xfs_buf_t	*bp;
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 	ASSERT(mp->m_sb_bp != NULL);
15831da177e4SLinus Torvalds 	bp = mp->m_sb_bp;
15841da177e4SLinus Torvalds 	if (flags & XFS_BUF_TRYLOCK) {
15851da177e4SLinus Torvalds 		if (!XFS_BUF_CPSEMA(bp)) {
15861da177e4SLinus Torvalds 			return NULL;
15871da177e4SLinus Torvalds 		}
15881da177e4SLinus Torvalds 	} else {
15891da177e4SLinus Torvalds 		XFS_BUF_PSEMA(bp, PRIBIO);
15901da177e4SLinus Torvalds 	}
15911da177e4SLinus Torvalds 	XFS_BUF_HOLD(bp);
15921da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISDONE(bp));
1593014c2544SJesper Juhl 	return bp;
15941da177e4SLinus Torvalds }
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds /*
15971da177e4SLinus Torvalds  * Used to free the superblock along various error paths.
15981da177e4SLinus Torvalds  */
15991da177e4SLinus Torvalds void
16001da177e4SLinus Torvalds xfs_freesb(
16011da177e4SLinus Torvalds 	xfs_mount_t	*mp)
16021da177e4SLinus Torvalds {
16031da177e4SLinus Torvalds 	xfs_buf_t	*bp;
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds 	/*
16061da177e4SLinus Torvalds 	 * Use xfs_getsb() so that the buffer will be locked
16071da177e4SLinus Torvalds 	 * when we call xfs_buf_relse().
16081da177e4SLinus Torvalds 	 */
16091da177e4SLinus Torvalds 	bp = xfs_getsb(mp, 0);
16101da177e4SLinus Torvalds 	XFS_BUF_UNMANAGE(bp);
16111da177e4SLinus Torvalds 	xfs_buf_relse(bp);
16121da177e4SLinus Torvalds 	mp->m_sb_bp = NULL;
16131da177e4SLinus Torvalds }
16141da177e4SLinus Torvalds 
16151da177e4SLinus Torvalds /*
16161da177e4SLinus Torvalds  * See if the UUID is unique among mounted XFS filesystems.
16171da177e4SLinus Torvalds  * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
16181da177e4SLinus Torvalds  */
16191da177e4SLinus Torvalds STATIC int
16201da177e4SLinus Torvalds xfs_uuid_mount(
16211da177e4SLinus Torvalds 	xfs_mount_t	*mp)
16221da177e4SLinus Torvalds {
16231da177e4SLinus Torvalds 	if (uuid_is_nil(&mp->m_sb.sb_uuid)) {
16241da177e4SLinus Torvalds 		cmn_err(CE_WARN,
16251da177e4SLinus Torvalds 			"XFS: Filesystem %s has nil UUID - can't mount",
16261da177e4SLinus Torvalds 			mp->m_fsname);
16271da177e4SLinus Torvalds 		return -1;
16281da177e4SLinus Torvalds 	}
16291da177e4SLinus Torvalds 	if (!uuid_table_insert(&mp->m_sb.sb_uuid)) {
16301da177e4SLinus Torvalds 		cmn_err(CE_WARN,
16311da177e4SLinus Torvalds 			"XFS: Filesystem %s has duplicate UUID - can't mount",
16321da177e4SLinus Torvalds 			mp->m_fsname);
16331da177e4SLinus Torvalds 		return -1;
16341da177e4SLinus Torvalds 	}
16351da177e4SLinus Torvalds 	return 0;
16361da177e4SLinus Torvalds }
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds /*
16391da177e4SLinus Torvalds  * Remove filesystem from the UUID table.
16401da177e4SLinus Torvalds  */
16411da177e4SLinus Torvalds STATIC void
16421da177e4SLinus Torvalds xfs_uuid_unmount(
16431da177e4SLinus Torvalds 	xfs_mount_t	*mp)
16441da177e4SLinus Torvalds {
16451da177e4SLinus Torvalds 	uuid_table_remove(&mp->m_sb.sb_uuid);
16461da177e4SLinus Torvalds }
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds /*
16491da177e4SLinus Torvalds  * Used to log changes to the superblock unit and width fields which could
16501da177e4SLinus Torvalds  * be altered by the mount options. Only the first superblock is updated.
16511da177e4SLinus Torvalds  */
16521da177e4SLinus Torvalds STATIC void
16531da177e4SLinus Torvalds xfs_mount_log_sbunit(
16541da177e4SLinus Torvalds 	xfs_mount_t	*mp,
16551da177e4SLinus Torvalds 	__int64_t	fields)
16561da177e4SLinus Torvalds {
16571da177e4SLinus Torvalds 	xfs_trans_t	*tp;
16581da177e4SLinus Torvalds 
16591da177e4SLinus Torvalds 	ASSERT(fields & (XFS_SB_UNIT|XFS_SB_WIDTH|XFS_SB_UUID));
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds 	tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
16621da177e4SLinus Torvalds 	if (xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
16631da177e4SLinus Torvalds 				XFS_DEFAULT_LOG_COUNT)) {
16641da177e4SLinus Torvalds 		xfs_trans_cancel(tp, 0);
16651da177e4SLinus Torvalds 		return;
16661da177e4SLinus Torvalds 	}
16671da177e4SLinus Torvalds 	xfs_mod_sb(tp, fields);
16681da177e4SLinus Torvalds 	xfs_trans_commit(tp, 0, NULL);
16691da177e4SLinus Torvalds }
16708d280b98SDavid Chinner 
16718d280b98SDavid Chinner 
16728d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB
16738d280b98SDavid Chinner /*
16748d280b98SDavid Chinner  * Per-cpu incore superblock counters
16758d280b98SDavid Chinner  *
16768d280b98SDavid Chinner  * Simple concept, difficult implementation
16778d280b98SDavid Chinner  *
16788d280b98SDavid Chinner  * Basically, replace the incore superblock counters with a distributed per cpu
16798d280b98SDavid Chinner  * counter for contended fields (e.g.  free block count).
16808d280b98SDavid Chinner  *
16818d280b98SDavid Chinner  * Difficulties arise in that the incore sb is used for ENOSPC checking, and
16828d280b98SDavid Chinner  * hence needs to be accurately read when we are running low on space. Hence
16838d280b98SDavid Chinner  * there is a method to enable and disable the per-cpu counters based on how
16848d280b98SDavid Chinner  * much "stuff" is available in them.
16858d280b98SDavid Chinner  *
16868d280b98SDavid Chinner  * Basically, a counter is enabled if there is enough free resource to justify
16878d280b98SDavid Chinner  * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
16888d280b98SDavid Chinner  * ENOSPC), then we disable the counters to synchronise all callers and
16898d280b98SDavid Chinner  * re-distribute the available resources.
16908d280b98SDavid Chinner  *
16918d280b98SDavid Chinner  * If, once we redistributed the available resources, we still get a failure,
16928d280b98SDavid Chinner  * we disable the per-cpu counter and go through the slow path.
16938d280b98SDavid Chinner  *
16948d280b98SDavid Chinner  * The slow path is the current xfs_mod_incore_sb() function.  This means that
16958d280b98SDavid Chinner  * when we disable a per-cpu counter, we need to drain it's resources back to
16968d280b98SDavid Chinner  * the global superblock. We do this after disabling the counter to prevent
16978d280b98SDavid Chinner  * more threads from queueing up on the counter.
16988d280b98SDavid Chinner  *
16998d280b98SDavid Chinner  * Essentially, this means that we still need a lock in the fast path to enable
17008d280b98SDavid Chinner  * synchronisation between the global counters and the per-cpu counters. This
17018d280b98SDavid Chinner  * is not a problem because the lock will be local to a CPU almost all the time
17028d280b98SDavid Chinner  * and have little contention except when we get to ENOSPC conditions.
17038d280b98SDavid Chinner  *
17048d280b98SDavid Chinner  * Basically, this lock becomes a barrier that enables us to lock out the fast
17058d280b98SDavid Chinner  * path while we do things like enabling and disabling counters and
17068d280b98SDavid Chinner  * synchronising the counters.
17078d280b98SDavid Chinner  *
17088d280b98SDavid Chinner  * Locking rules:
17098d280b98SDavid Chinner  *
17108d280b98SDavid Chinner  * 	1. XFS_SB_LOCK() before picking up per-cpu locks
17118d280b98SDavid Chinner  * 	2. per-cpu locks always picked up via for_each_online_cpu() order
17128d280b98SDavid Chinner  * 	3. accurate counter sync requires XFS_SB_LOCK + per cpu locks
17138d280b98SDavid Chinner  * 	4. modifying per-cpu counters requires holding per-cpu lock
17148d280b98SDavid Chinner  * 	5. modifying global counters requires holding XFS_SB_LOCK
17158d280b98SDavid Chinner  *	6. enabling or disabling a counter requires holding the XFS_SB_LOCK
17168d280b98SDavid Chinner  *	   and _none_ of the per-cpu locks.
17178d280b98SDavid Chinner  *
17188d280b98SDavid Chinner  * Disabled counters are only ever re-enabled by a balance operation
17198d280b98SDavid Chinner  * that results in more free resources per CPU than a given threshold.
17208d280b98SDavid Chinner  * To ensure counters don't remain disabled, they are rebalanced when
17218d280b98SDavid Chinner  * the global resource goes above a higher threshold (i.e. some hysteresis
17228d280b98SDavid Chinner  * is present to prevent thrashing).
17238d280b98SDavid Chinner  */
1724e8234a68SDavid Chinner 
1725e8234a68SDavid Chinner /*
1726e8234a68SDavid Chinner  * hot-plug CPU notifier support.
1727e8234a68SDavid Chinner  *
1728e8234a68SDavid Chinner  * We cannot use the hotcpu_register() function because it does
1729e8234a68SDavid Chinner  * not allow notifier instances. We need a notifier per filesystem
1730e8234a68SDavid Chinner  * as we need to be able to identify the filesystem to balance
1731*c41564b5SNathan Scott  * the counters out. This is achieved by having a notifier block
1732e8234a68SDavid Chinner  * embedded in the xfs_mount_t and doing pointer magic to get the
1733e8234a68SDavid Chinner  * mount pointer from the notifier block address.
1734e8234a68SDavid Chinner  */
1735e8234a68SDavid Chinner STATIC int
1736e8234a68SDavid Chinner xfs_icsb_cpu_notify(
1737e8234a68SDavid Chinner 	struct notifier_block *nfb,
1738e8234a68SDavid Chinner 	unsigned long action,
1739e8234a68SDavid Chinner 	void *hcpu)
1740e8234a68SDavid Chinner {
1741e8234a68SDavid Chinner 	xfs_icsb_cnts_t *cntp;
1742e8234a68SDavid Chinner 	xfs_mount_t	*mp;
1743e8234a68SDavid Chinner 	int		s;
1744e8234a68SDavid Chinner 
1745e8234a68SDavid Chinner 	mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
1746e8234a68SDavid Chinner 	cntp = (xfs_icsb_cnts_t *)
1747e8234a68SDavid Chinner 			per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
1748e8234a68SDavid Chinner 	switch (action) {
1749e8234a68SDavid Chinner 	case CPU_UP_PREPARE:
1750e8234a68SDavid Chinner 		/* Easy Case - initialize the area and locks, and
1751e8234a68SDavid Chinner 		 * then rebalance when online does everything else for us. */
175201e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
1753e8234a68SDavid Chinner 		break;
1754e8234a68SDavid Chinner 	case CPU_ONLINE:
1755e8234a68SDavid Chinner 		xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
1756e8234a68SDavid Chinner 		xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
1757e8234a68SDavid Chinner 		xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
1758e8234a68SDavid Chinner 		break;
1759e8234a68SDavid Chinner 	case CPU_DEAD:
1760e8234a68SDavid Chinner 		/* Disable all the counters, then fold the dead cpu's
1761e8234a68SDavid Chinner 		 * count into the total on the global superblock and
1762e8234a68SDavid Chinner 		 * re-enable the counters. */
1763e8234a68SDavid Chinner 		s = XFS_SB_LOCK(mp);
1764e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
1765e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
1766e8234a68SDavid Chinner 		xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
1767e8234a68SDavid Chinner 
1768e8234a68SDavid Chinner 		mp->m_sb.sb_icount += cntp->icsb_icount;
1769e8234a68SDavid Chinner 		mp->m_sb.sb_ifree += cntp->icsb_ifree;
1770e8234a68SDavid Chinner 		mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
1771e8234a68SDavid Chinner 
177201e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
1773e8234a68SDavid Chinner 
1774e8234a68SDavid Chinner 		xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, XFS_ICSB_SB_LOCKED);
1775e8234a68SDavid Chinner 		xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, XFS_ICSB_SB_LOCKED);
1776e8234a68SDavid Chinner 		xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, XFS_ICSB_SB_LOCKED);
1777e8234a68SDavid Chinner 		XFS_SB_UNLOCK(mp, s);
1778e8234a68SDavid Chinner 		break;
1779e8234a68SDavid Chinner 	}
1780e8234a68SDavid Chinner 
1781e8234a68SDavid Chinner 	return NOTIFY_OK;
1782e8234a68SDavid Chinner }
1783e8234a68SDavid Chinner 
17848d280b98SDavid Chinner int
17858d280b98SDavid Chinner xfs_icsb_init_counters(
17868d280b98SDavid Chinner 	xfs_mount_t	*mp)
17878d280b98SDavid Chinner {
17888d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
17898d280b98SDavid Chinner 	int		i;
17908d280b98SDavid Chinner 
17918d280b98SDavid Chinner 	mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
17928d280b98SDavid Chinner 	if (mp->m_sb_cnts == NULL)
17938d280b98SDavid Chinner 		return -ENOMEM;
17948d280b98SDavid Chinner 
1795e8234a68SDavid Chinner 	mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
1796e8234a68SDavid Chinner 	mp->m_icsb_notifier.priority = 0;
1797e8234a68SDavid Chinner 	register_cpu_notifier(&mp->m_icsb_notifier);
1798e8234a68SDavid Chinner 
17998d280b98SDavid Chinner 	for_each_online_cpu(i) {
18008d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
180101e1b69cSDavid Chinner 		memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
18028d280b98SDavid Chinner 	}
18038d280b98SDavid Chinner 	/*
18048d280b98SDavid Chinner 	 * start with all counters disabled so that the
18058d280b98SDavid Chinner 	 * initial balance kicks us off correctly
18068d280b98SDavid Chinner 	 */
18078d280b98SDavid Chinner 	mp->m_icsb_counters = -1;
18088d280b98SDavid Chinner 	return 0;
18098d280b98SDavid Chinner }
18108d280b98SDavid Chinner 
18118d280b98SDavid Chinner STATIC void
18128d280b98SDavid Chinner xfs_icsb_destroy_counters(
18138d280b98SDavid Chinner 	xfs_mount_t	*mp)
18148d280b98SDavid Chinner {
1815e8234a68SDavid Chinner 	if (mp->m_sb_cnts) {
1816e8234a68SDavid Chinner 		unregister_cpu_notifier(&mp->m_icsb_notifier);
18178d280b98SDavid Chinner 		free_percpu(mp->m_sb_cnts);
18188d280b98SDavid Chinner 	}
1819e8234a68SDavid Chinner }
18208d280b98SDavid Chinner 
182101e1b69cSDavid Chinner STATIC inline void
182201e1b69cSDavid Chinner xfs_icsb_lock_cntr(
182301e1b69cSDavid Chinner 	xfs_icsb_cnts_t	*icsbp)
182401e1b69cSDavid Chinner {
182501e1b69cSDavid Chinner 	while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
182601e1b69cSDavid Chinner 		ndelay(1000);
182701e1b69cSDavid Chinner 	}
182801e1b69cSDavid Chinner }
182901e1b69cSDavid Chinner 
183001e1b69cSDavid Chinner STATIC inline void
183101e1b69cSDavid Chinner xfs_icsb_unlock_cntr(
183201e1b69cSDavid Chinner 	xfs_icsb_cnts_t	*icsbp)
183301e1b69cSDavid Chinner {
183401e1b69cSDavid Chinner 	clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
183501e1b69cSDavid Chinner }
183601e1b69cSDavid Chinner 
18378d280b98SDavid Chinner 
18388d280b98SDavid Chinner STATIC inline void
18398d280b98SDavid Chinner xfs_icsb_lock_all_counters(
18408d280b98SDavid Chinner 	xfs_mount_t	*mp)
18418d280b98SDavid Chinner {
18428d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
18438d280b98SDavid Chinner 	int		i;
18448d280b98SDavid Chinner 
18458d280b98SDavid Chinner 	for_each_online_cpu(i) {
18468d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
184701e1b69cSDavid Chinner 		xfs_icsb_lock_cntr(cntp);
18488d280b98SDavid Chinner 	}
18498d280b98SDavid Chinner }
18508d280b98SDavid Chinner 
18518d280b98SDavid Chinner STATIC inline void
18528d280b98SDavid Chinner xfs_icsb_unlock_all_counters(
18538d280b98SDavid Chinner 	xfs_mount_t	*mp)
18548d280b98SDavid Chinner {
18558d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
18568d280b98SDavid Chinner 	int		i;
18578d280b98SDavid Chinner 
18588d280b98SDavid Chinner 	for_each_online_cpu(i) {
18598d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
186001e1b69cSDavid Chinner 		xfs_icsb_unlock_cntr(cntp);
18618d280b98SDavid Chinner 	}
18628d280b98SDavid Chinner }
18638d280b98SDavid Chinner 
18648d280b98SDavid Chinner STATIC void
18658d280b98SDavid Chinner xfs_icsb_count(
18668d280b98SDavid Chinner 	xfs_mount_t	*mp,
18678d280b98SDavid Chinner 	xfs_icsb_cnts_t	*cnt,
18688d280b98SDavid Chinner 	int		flags)
18698d280b98SDavid Chinner {
18708d280b98SDavid Chinner 	xfs_icsb_cnts_t *cntp;
18718d280b98SDavid Chinner 	int		i;
18728d280b98SDavid Chinner 
18738d280b98SDavid Chinner 	memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
18748d280b98SDavid Chinner 
18758d280b98SDavid Chinner 	if (!(flags & XFS_ICSB_LAZY_COUNT))
18768d280b98SDavid Chinner 		xfs_icsb_lock_all_counters(mp);
18778d280b98SDavid Chinner 
18788d280b98SDavid Chinner 	for_each_online_cpu(i) {
18798d280b98SDavid Chinner 		cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
18808d280b98SDavid Chinner 		cnt->icsb_icount += cntp->icsb_icount;
18818d280b98SDavid Chinner 		cnt->icsb_ifree += cntp->icsb_ifree;
18828d280b98SDavid Chinner 		cnt->icsb_fdblocks += cntp->icsb_fdblocks;
18838d280b98SDavid Chinner 	}
18848d280b98SDavid Chinner 
18858d280b98SDavid Chinner 	if (!(flags & XFS_ICSB_LAZY_COUNT))
18868d280b98SDavid Chinner 		xfs_icsb_unlock_all_counters(mp);
18878d280b98SDavid Chinner }
18888d280b98SDavid Chinner 
18898d280b98SDavid Chinner STATIC int
18908d280b98SDavid Chinner xfs_icsb_counter_disabled(
18918d280b98SDavid Chinner 	xfs_mount_t	*mp,
18928d280b98SDavid Chinner 	xfs_sb_field_t	field)
18938d280b98SDavid Chinner {
18948d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
18958d280b98SDavid Chinner 	return test_bit(field, &mp->m_icsb_counters);
18968d280b98SDavid Chinner }
18978d280b98SDavid Chinner 
18988d280b98SDavid Chinner STATIC int
18998d280b98SDavid Chinner xfs_icsb_disable_counter(
19008d280b98SDavid Chinner 	xfs_mount_t	*mp,
19018d280b98SDavid Chinner 	xfs_sb_field_t	field)
19028d280b98SDavid Chinner {
19038d280b98SDavid Chinner 	xfs_icsb_cnts_t	cnt;
19048d280b98SDavid Chinner 
19058d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
19068d280b98SDavid Chinner 
19078d280b98SDavid Chinner 	xfs_icsb_lock_all_counters(mp);
19088d280b98SDavid Chinner 	if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
19098d280b98SDavid Chinner 		/* drain back to superblock */
19108d280b98SDavid Chinner 
19118d280b98SDavid Chinner 		xfs_icsb_count(mp, &cnt, XFS_ICSB_SB_LOCKED|XFS_ICSB_LAZY_COUNT);
19128d280b98SDavid Chinner 		switch(field) {
19138d280b98SDavid Chinner 		case XFS_SBS_ICOUNT:
19148d280b98SDavid Chinner 			mp->m_sb.sb_icount = cnt.icsb_icount;
19158d280b98SDavid Chinner 			break;
19168d280b98SDavid Chinner 		case XFS_SBS_IFREE:
19178d280b98SDavid Chinner 			mp->m_sb.sb_ifree = cnt.icsb_ifree;
19188d280b98SDavid Chinner 			break;
19198d280b98SDavid Chinner 		case XFS_SBS_FDBLOCKS:
19208d280b98SDavid Chinner 			mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
19218d280b98SDavid Chinner 			break;
19228d280b98SDavid Chinner 		default:
19238d280b98SDavid Chinner 			BUG();
19248d280b98SDavid Chinner 		}
19258d280b98SDavid Chinner 	}
19268d280b98SDavid Chinner 
19278d280b98SDavid Chinner 	xfs_icsb_unlock_all_counters(mp);
19288d280b98SDavid Chinner 
19298d280b98SDavid Chinner 	return 0;
19308d280b98SDavid Chinner }
19318d280b98SDavid Chinner 
19328d280b98SDavid Chinner STATIC void
19338d280b98SDavid Chinner xfs_icsb_enable_counter(
19348d280b98SDavid Chinner 	xfs_mount_t	*mp,
19358d280b98SDavid Chinner 	xfs_sb_field_t	field,
19368d280b98SDavid Chinner 	uint64_t	count,
19378d280b98SDavid Chinner 	uint64_t	resid)
19388d280b98SDavid Chinner {
19398d280b98SDavid Chinner 	xfs_icsb_cnts_t	*cntp;
19408d280b98SDavid Chinner 	int		i;
19418d280b98SDavid Chinner 
19428d280b98SDavid Chinner 	ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
19438d280b98SDavid Chinner 
19448d280b98SDavid Chinner 	xfs_icsb_lock_all_counters(mp);
19458d280b98SDavid Chinner 	for_each_online_cpu(i) {
19468d280b98SDavid Chinner 		cntp = per_cpu_ptr(mp->m_sb_cnts, i);
19478d280b98SDavid Chinner 		switch (field) {
19488d280b98SDavid Chinner 		case XFS_SBS_ICOUNT:
19498d280b98SDavid Chinner 			cntp->icsb_icount = count + resid;
19508d280b98SDavid Chinner 			break;
19518d280b98SDavid Chinner 		case XFS_SBS_IFREE:
19528d280b98SDavid Chinner 			cntp->icsb_ifree = count + resid;
19538d280b98SDavid Chinner 			break;
19548d280b98SDavid Chinner 		case XFS_SBS_FDBLOCKS:
19558d280b98SDavid Chinner 			cntp->icsb_fdblocks = count + resid;
19568d280b98SDavid Chinner 			break;
19578d280b98SDavid Chinner 		default:
19588d280b98SDavid Chinner 			BUG();
19598d280b98SDavid Chinner 			break;
19608d280b98SDavid Chinner 		}
19618d280b98SDavid Chinner 		resid = 0;
19628d280b98SDavid Chinner 	}
19638d280b98SDavid Chinner 	clear_bit(field, &mp->m_icsb_counters);
19648d280b98SDavid Chinner 	xfs_icsb_unlock_all_counters(mp);
19658d280b98SDavid Chinner }
19668d280b98SDavid Chinner 
19678d280b98SDavid Chinner STATIC void
19688d280b98SDavid Chinner xfs_icsb_sync_counters_int(
19698d280b98SDavid Chinner 	xfs_mount_t	*mp,
19708d280b98SDavid Chinner 	int		flags)
19718d280b98SDavid Chinner {
19728d280b98SDavid Chinner 	xfs_icsb_cnts_t	cnt;
19738d280b98SDavid Chinner 	int		s;
19748d280b98SDavid Chinner 
19758d280b98SDavid Chinner 	/* Pass 1: lock all counters */
19768d280b98SDavid Chinner 	if ((flags & XFS_ICSB_SB_LOCKED) == 0)
19778d280b98SDavid Chinner 		s = XFS_SB_LOCK(mp);
19788d280b98SDavid Chinner 
19798d280b98SDavid Chinner 	xfs_icsb_count(mp, &cnt, flags);
19808d280b98SDavid Chinner 
19818d280b98SDavid Chinner 	/* Step 3: update mp->m_sb fields */
19828d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
19838d280b98SDavid Chinner 		mp->m_sb.sb_icount = cnt.icsb_icount;
19848d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
19858d280b98SDavid Chinner 		mp->m_sb.sb_ifree = cnt.icsb_ifree;
19868d280b98SDavid Chinner 	if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
19878d280b98SDavid Chinner 		mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
19888d280b98SDavid Chinner 
19898d280b98SDavid Chinner 	if ((flags & XFS_ICSB_SB_LOCKED) == 0)
19908d280b98SDavid Chinner 		XFS_SB_UNLOCK(mp, s);
19918d280b98SDavid Chinner }
19928d280b98SDavid Chinner 
19938d280b98SDavid Chinner /*
19948d280b98SDavid Chinner  * Accurate update of per-cpu counters to incore superblock
19958d280b98SDavid Chinner  */
19968d280b98SDavid Chinner STATIC void
19978d280b98SDavid Chinner xfs_icsb_sync_counters(
19988d280b98SDavid Chinner 	xfs_mount_t	*mp)
19998d280b98SDavid Chinner {
20008d280b98SDavid Chinner 	xfs_icsb_sync_counters_int(mp, 0);
20018d280b98SDavid Chinner }
20028d280b98SDavid Chinner 
20038d280b98SDavid Chinner /*
20048d280b98SDavid Chinner  * lazy addition used for things like df, background sb syncs, etc
20058d280b98SDavid Chinner  */
20068d280b98SDavid Chinner void
20078d280b98SDavid Chinner xfs_icsb_sync_counters_lazy(
20088d280b98SDavid Chinner 	xfs_mount_t	*mp)
20098d280b98SDavid Chinner {
20108d280b98SDavid Chinner 	xfs_icsb_sync_counters_int(mp, XFS_ICSB_LAZY_COUNT);
20118d280b98SDavid Chinner }
20128d280b98SDavid Chinner 
20138d280b98SDavid Chinner /*
20148d280b98SDavid Chinner  * Balance and enable/disable counters as necessary.
20158d280b98SDavid Chinner  *
20168d280b98SDavid Chinner  * Thresholds for re-enabling counters are somewhat magic.
20178d280b98SDavid Chinner  * inode counts are chosen to be the same number as single
20188d280b98SDavid Chinner  * on disk allocation chunk per CPU, and free blocks is
20198d280b98SDavid Chinner  * something far enough zero that we aren't going thrash
20208d280b98SDavid Chinner  * when we get near ENOSPC.
20218d280b98SDavid Chinner  */
20228d280b98SDavid Chinner #define XFS_ICSB_INO_CNTR_REENABLE	64
20238d280b98SDavid Chinner #define XFS_ICSB_FDBLK_CNTR_REENABLE	512
20248d280b98SDavid Chinner STATIC void
20258d280b98SDavid Chinner xfs_icsb_balance_counter(
20268d280b98SDavid Chinner 	xfs_mount_t	*mp,
20278d280b98SDavid Chinner 	xfs_sb_field_t  field,
20288d280b98SDavid Chinner 	int		flags)
20298d280b98SDavid Chinner {
20308d280b98SDavid Chinner 	uint64_t	count, resid = 0;
20318d280b98SDavid Chinner 	int		weight = num_online_cpus();
20328d280b98SDavid Chinner 	int		s;
20338d280b98SDavid Chinner 
20348d280b98SDavid Chinner 	if (!(flags & XFS_ICSB_SB_LOCKED))
20358d280b98SDavid Chinner 		s = XFS_SB_LOCK(mp);
20368d280b98SDavid Chinner 
20378d280b98SDavid Chinner 	/* disable counter and sync counter */
20388d280b98SDavid Chinner 	xfs_icsb_disable_counter(mp, field);
20398d280b98SDavid Chinner 
20408d280b98SDavid Chinner 	/* update counters  - first CPU gets residual*/
20418d280b98SDavid Chinner 	switch (field) {
20428d280b98SDavid Chinner 	case XFS_SBS_ICOUNT:
20438d280b98SDavid Chinner 		count = mp->m_sb.sb_icount;
20448d280b98SDavid Chinner 		resid = do_div(count, weight);
20458d280b98SDavid Chinner 		if (count < XFS_ICSB_INO_CNTR_REENABLE)
20468d280b98SDavid Chinner 			goto out;
20478d280b98SDavid Chinner 		break;
20488d280b98SDavid Chinner 	case XFS_SBS_IFREE:
20498d280b98SDavid Chinner 		count = mp->m_sb.sb_ifree;
20508d280b98SDavid Chinner 		resid = do_div(count, weight);
20518d280b98SDavid Chinner 		if (count < XFS_ICSB_INO_CNTR_REENABLE)
20528d280b98SDavid Chinner 			goto out;
20538d280b98SDavid Chinner 		break;
20548d280b98SDavid Chinner 	case XFS_SBS_FDBLOCKS:
20558d280b98SDavid Chinner 		count = mp->m_sb.sb_fdblocks;
20568d280b98SDavid Chinner 		resid = do_div(count, weight);
20578d280b98SDavid Chinner 		if (count < XFS_ICSB_FDBLK_CNTR_REENABLE)
20588d280b98SDavid Chinner 			goto out;
20598d280b98SDavid Chinner 		break;
20608d280b98SDavid Chinner 	default:
20618d280b98SDavid Chinner 		BUG();
20628d280b98SDavid Chinner 		break;
20638d280b98SDavid Chinner 	}
20648d280b98SDavid Chinner 
20658d280b98SDavid Chinner 	xfs_icsb_enable_counter(mp, field, count, resid);
20668d280b98SDavid Chinner out:
20678d280b98SDavid Chinner 	if (!(flags & XFS_ICSB_SB_LOCKED))
20688d280b98SDavid Chinner 		XFS_SB_UNLOCK(mp, s);
20698d280b98SDavid Chinner }
20708d280b98SDavid Chinner 
20718d280b98SDavid Chinner STATIC int
20728d280b98SDavid Chinner xfs_icsb_modify_counters_int(
20738d280b98SDavid Chinner 	xfs_mount_t	*mp,
20748d280b98SDavid Chinner 	xfs_sb_field_t	field,
20758d280b98SDavid Chinner 	int		delta,
20768d280b98SDavid Chinner 	int		rsvd,
20778d280b98SDavid Chinner 	int		flags)
20788d280b98SDavid Chinner {
20798d280b98SDavid Chinner 	xfs_icsb_cnts_t	*icsbp;
20808d280b98SDavid Chinner 	long long	lcounter;	/* long counter for 64 bit fields */
20818d280b98SDavid Chinner 	int		cpu, s, locked = 0;
20828d280b98SDavid Chinner 	int		ret = 0, balance_done = 0;
20838d280b98SDavid Chinner 
20848d280b98SDavid Chinner again:
20858d280b98SDavid Chinner 	cpu = get_cpu();
20868d280b98SDavid Chinner 	icsbp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, cpu),
208701e1b69cSDavid Chinner 	xfs_icsb_lock_cntr(icsbp);
20888d280b98SDavid Chinner 	if (unlikely(xfs_icsb_counter_disabled(mp, field)))
20898d280b98SDavid Chinner 		goto slow_path;
20908d280b98SDavid Chinner 
20918d280b98SDavid Chinner 	switch (field) {
20928d280b98SDavid Chinner 	case XFS_SBS_ICOUNT:
20938d280b98SDavid Chinner 		lcounter = icsbp->icsb_icount;
20948d280b98SDavid Chinner 		lcounter += delta;
20958d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
20968d280b98SDavid Chinner 			goto slow_path;
20978d280b98SDavid Chinner 		icsbp->icsb_icount = lcounter;
20988d280b98SDavid Chinner 		break;
20998d280b98SDavid Chinner 
21008d280b98SDavid Chinner 	case XFS_SBS_IFREE:
21018d280b98SDavid Chinner 		lcounter = icsbp->icsb_ifree;
21028d280b98SDavid Chinner 		lcounter += delta;
21038d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
21048d280b98SDavid Chinner 			goto slow_path;
21058d280b98SDavid Chinner 		icsbp->icsb_ifree = lcounter;
21068d280b98SDavid Chinner 		break;
21078d280b98SDavid Chinner 
21088d280b98SDavid Chinner 	case XFS_SBS_FDBLOCKS:
21098d280b98SDavid Chinner 		BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
21108d280b98SDavid Chinner 
21118d280b98SDavid Chinner 		lcounter = icsbp->icsb_fdblocks;
21128d280b98SDavid Chinner 		lcounter += delta;
21138d280b98SDavid Chinner 		if (unlikely(lcounter < 0))
21148d280b98SDavid Chinner 			goto slow_path;
21158d280b98SDavid Chinner 		icsbp->icsb_fdblocks = lcounter;
21168d280b98SDavid Chinner 		break;
21178d280b98SDavid Chinner 	default:
21188d280b98SDavid Chinner 		BUG();
21198d280b98SDavid Chinner 		break;
21208d280b98SDavid Chinner 	}
212101e1b69cSDavid Chinner 	xfs_icsb_unlock_cntr(icsbp);
21228d280b98SDavid Chinner 	put_cpu();
21238d280b98SDavid Chinner 	if (locked)
21248d280b98SDavid Chinner 		XFS_SB_UNLOCK(mp, s);
21258d280b98SDavid Chinner 	return 0;
21268d280b98SDavid Chinner 
21278d280b98SDavid Chinner 	/*
21288d280b98SDavid Chinner 	 * The slow path needs to be run with the SBLOCK
21298d280b98SDavid Chinner 	 * held so that we prevent other threads from
21308d280b98SDavid Chinner 	 * attempting to run this path at the same time.
21318d280b98SDavid Chinner 	 * this provides exclusion for the balancing code,
21328d280b98SDavid Chinner 	 * and exclusive fallback if the balance does not
21338d280b98SDavid Chinner 	 * provide enough resources to continue in an unlocked
21348d280b98SDavid Chinner 	 * manner.
21358d280b98SDavid Chinner 	 */
21368d280b98SDavid Chinner slow_path:
213701e1b69cSDavid Chinner 	xfs_icsb_unlock_cntr(icsbp);
21388d280b98SDavid Chinner 	put_cpu();
21398d280b98SDavid Chinner 
21408d280b98SDavid Chinner 	/* need to hold superblock incase we need
21418d280b98SDavid Chinner 	 * to disable a counter */
21428d280b98SDavid Chinner 	if (!(flags & XFS_ICSB_SB_LOCKED)) {
21438d280b98SDavid Chinner 		s = XFS_SB_LOCK(mp);
21448d280b98SDavid Chinner 		locked = 1;
21458d280b98SDavid Chinner 		flags |= XFS_ICSB_SB_LOCKED;
21468d280b98SDavid Chinner 	}
21478d280b98SDavid Chinner 	if (!balance_done) {
21488d280b98SDavid Chinner 		xfs_icsb_balance_counter(mp, field, flags);
21498d280b98SDavid Chinner 		balance_done = 1;
21508d280b98SDavid Chinner 		goto again;
21518d280b98SDavid Chinner 	} else {
21528d280b98SDavid Chinner 		/*
21538d280b98SDavid Chinner 		 * we might not have enough on this local
21548d280b98SDavid Chinner 		 * cpu to allocate for a bulk request.
21558d280b98SDavid Chinner 		 * We need to drain this field from all CPUs
21568d280b98SDavid Chinner 		 * and disable the counter fastpath
21578d280b98SDavid Chinner 		 */
21588d280b98SDavid Chinner 		xfs_icsb_disable_counter(mp, field);
21598d280b98SDavid Chinner 	}
21608d280b98SDavid Chinner 
21618d280b98SDavid Chinner 	ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
21628d280b98SDavid Chinner 
21638d280b98SDavid Chinner 	if (locked)
21648d280b98SDavid Chinner 		XFS_SB_UNLOCK(mp, s);
21658d280b98SDavid Chinner 	return ret;
21668d280b98SDavid Chinner }
21678d280b98SDavid Chinner 
21688d280b98SDavid Chinner STATIC int
21698d280b98SDavid Chinner xfs_icsb_modify_counters(
21708d280b98SDavid Chinner 	xfs_mount_t	*mp,
21718d280b98SDavid Chinner 	xfs_sb_field_t	field,
21728d280b98SDavid Chinner 	int		delta,
21738d280b98SDavid Chinner 	int		rsvd)
21748d280b98SDavid Chinner {
21758d280b98SDavid Chinner 	return xfs_icsb_modify_counters_int(mp, field, delta, rsvd, 0);
21768d280b98SDavid Chinner }
21778d280b98SDavid Chinner 
21788d280b98SDavid Chinner /*
21798d280b98SDavid Chinner  * Called when superblock is already locked
21808d280b98SDavid Chinner  */
21818d280b98SDavid Chinner STATIC int
21828d280b98SDavid Chinner xfs_icsb_modify_counters_locked(
21838d280b98SDavid Chinner 	xfs_mount_t	*mp,
21848d280b98SDavid Chinner 	xfs_sb_field_t	field,
21858d280b98SDavid Chinner 	int		delta,
21868d280b98SDavid Chinner 	int		rsvd)
21878d280b98SDavid Chinner {
21888d280b98SDavid Chinner 	return xfs_icsb_modify_counters_int(mp, field, delta,
21898d280b98SDavid Chinner 						rsvd, XFS_ICSB_SB_LOCKED);
21908d280b98SDavid Chinner }
21918d280b98SDavid Chinner #endif
2192