xref: /linux/fs/xfs/xfs_mount.c (revision fc1f8c1ca373dc88dd6e8c0e8b7de0e6ad95d441)
11da177e4SLinus Torvalds /*
2d8cc890dSNathan Scott  * Copyright (c) 2000-2005 Silicon Graphics, Inc.  All Rights Reserved.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify it
51da177e4SLinus Torvalds  * under the terms of version 2 of the GNU General Public License as
61da177e4SLinus Torvalds  * published by the Free Software Foundation.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * This program is distributed in the hope that it would be useful, but
91da177e4SLinus Torvalds  * WITHOUT ANY WARRANTY; without even the implied warranty of
101da177e4SLinus Torvalds  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * Further, this software is distributed without any warranty that it is
131da177e4SLinus Torvalds  * free of the rightful claim of any third person regarding infringement
141da177e4SLinus Torvalds  * or the like.  Any license provided herein, whether implied or
151da177e4SLinus Torvalds  * otherwise, applies only to this software file.  Patent licenses, if
161da177e4SLinus Torvalds  * any, provided herein do not apply to combinations of this program with
171da177e4SLinus Torvalds  * other software, or any other product whatsoever.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License along
201da177e4SLinus Torvalds  * with this program; if not, write the Free Software Foundation, Inc., 59
211da177e4SLinus Torvalds  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
241da177e4SLinus Torvalds  * Mountain View, CA  94043, or:
251da177e4SLinus Torvalds  *
261da177e4SLinus Torvalds  * http://www.sgi.com
271da177e4SLinus Torvalds  *
281da177e4SLinus Torvalds  * For further information regarding this notice, see:
291da177e4SLinus Torvalds  *
301da177e4SLinus Torvalds  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
311da177e4SLinus Torvalds  */
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #include "xfs.h"
341da177e4SLinus Torvalds #include "xfs_macros.h"
351da177e4SLinus Torvalds #include "xfs_types.h"
361da177e4SLinus Torvalds #include "xfs_inum.h"
371da177e4SLinus Torvalds #include "xfs_log.h"
381da177e4SLinus Torvalds #include "xfs_trans.h"
391da177e4SLinus Torvalds #include "xfs_sb.h"
401da177e4SLinus Torvalds #include "xfs_ag.h"
411da177e4SLinus Torvalds #include "xfs_dir.h"
421da177e4SLinus Torvalds #include "xfs_dir2.h"
431da177e4SLinus Torvalds #include "xfs_dmapi.h"
441da177e4SLinus Torvalds #include "xfs_mount.h"
451da177e4SLinus Torvalds #include "xfs_alloc_btree.h"
461da177e4SLinus Torvalds #include "xfs_bmap_btree.h"
471da177e4SLinus Torvalds #include "xfs_ialloc_btree.h"
481da177e4SLinus Torvalds #include "xfs_btree.h"
491da177e4SLinus Torvalds #include "xfs_ialloc.h"
501da177e4SLinus Torvalds #include "xfs_attr_sf.h"
511da177e4SLinus Torvalds #include "xfs_dir_sf.h"
521da177e4SLinus Torvalds #include "xfs_dir2_sf.h"
531da177e4SLinus Torvalds #include "xfs_dinode.h"
541da177e4SLinus Torvalds #include "xfs_inode.h"
551da177e4SLinus Torvalds #include "xfs_alloc.h"
561da177e4SLinus Torvalds #include "xfs_rtalloc.h"
571da177e4SLinus Torvalds #include "xfs_bmap.h"
581da177e4SLinus Torvalds #include "xfs_error.h"
591da177e4SLinus Torvalds #include "xfs_bit.h"
601da177e4SLinus Torvalds #include "xfs_rw.h"
611da177e4SLinus Torvalds #include "xfs_quota.h"
621da177e4SLinus Torvalds #include "xfs_fsops.h"
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds STATIC void	xfs_mount_log_sbunit(xfs_mount_t *, __int64_t);
651da177e4SLinus Torvalds STATIC int	xfs_uuid_mount(xfs_mount_t *);
661da177e4SLinus Torvalds STATIC void	xfs_uuid_unmount(xfs_mount_t *mp);
67ba0f32d4SChristoph Hellwig STATIC void	xfs_unmountfs_wait(xfs_mount_t *);
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds static struct {
701da177e4SLinus Torvalds     short offset;
711da177e4SLinus Torvalds     short type;     /* 0 = integer
721da177e4SLinus Torvalds 		* 1 = binary / string (no translation)
731da177e4SLinus Torvalds 		*/
741da177e4SLinus Torvalds } xfs_sb_info[] = {
751da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_magicnum),   0 },
761da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_blocksize),  0 },
771da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_dblocks),    0 },
781da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rblocks),    0 },
791da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rextents),   0 },
801da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_uuid),       1 },
811da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logstart),   0 },
821da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rootino),    0 },
831da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rbmino),     0 },
841da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rsumino),    0 },
851da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rextsize),   0 },
861da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_agblocks),   0 },
871da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_agcount),    0 },
881da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rbmblocks),  0 },
891da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logblocks),  0 },
901da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_versionnum), 0 },
911da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_sectsize),   0 },
921da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inodesize),  0 },
931da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inopblock),  0 },
941da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_fname[0]),   1 },
951da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_blocklog),   0 },
961da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_sectlog),    0 },
971da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inodelog),   0 },
981da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inopblog),   0 },
991da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_agblklog),   0 },
1001da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_rextslog),   0 },
1011da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inprogress), 0 },
1021da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_imax_pct),   0 },
1031da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_icount),     0 },
1041da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_ifree),      0 },
1051da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_fdblocks),   0 },
1061da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_frextents),  0 },
1071da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_uquotino),   0 },
1081da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_gquotino),   0 },
1091da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_qflags),     0 },
1101da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_flags),      0 },
1111da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_shared_vn),  0 },
1121da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
1131da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_unit),	 0 },
1141da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_width),	 0 },
1151da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_dirblklog),	 0 },
1161da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logsectlog), 0 },
1171da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logsectsize),0 },
1181da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_logsunit),	 0 },
1191da177e4SLinus Torvalds     { offsetof(xfs_sb_t, sb_features2),	 0 },
1201da177e4SLinus Torvalds     { sizeof(xfs_sb_t),			 0 }
1211da177e4SLinus Torvalds };
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds /*
1241da177e4SLinus Torvalds  * Return a pointer to an initialized xfs_mount structure.
1251da177e4SLinus Torvalds  */
1261da177e4SLinus Torvalds xfs_mount_t *
1271da177e4SLinus Torvalds xfs_mount_init(void)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	xfs_mount_t *mp;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	mp = kmem_zalloc(sizeof(*mp), KM_SLEEP);
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	AIL_LOCKINIT(&mp->m_ail_lock, "xfs_ail");
1341da177e4SLinus Torvalds 	spinlock_init(&mp->m_sb_lock, "xfs_sb");
1351da177e4SLinus Torvalds 	mutex_init(&mp->m_ilock, MUTEX_DEFAULT, "xfs_ilock");
1361da177e4SLinus Torvalds 	initnsema(&mp->m_growlock, 1, "xfs_grow");
1371da177e4SLinus Torvalds 	/*
1381da177e4SLinus Torvalds 	 * Initialize the AIL.
1391da177e4SLinus Torvalds 	 */
1401da177e4SLinus Torvalds 	xfs_trans_ail_init(mp);
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	atomic_set(&mp->m_active_trans, 0);
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	return mp;
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds /*
1481da177e4SLinus Torvalds  * Free up the resources associated with a mount structure.  Assume that
1491da177e4SLinus Torvalds  * the structure was initially zeroed, so we can tell which fields got
1501da177e4SLinus Torvalds  * initialized.
1511da177e4SLinus Torvalds  */
1521da177e4SLinus Torvalds void
1531da177e4SLinus Torvalds xfs_mount_free(
1541da177e4SLinus Torvalds 	xfs_mount_t *mp,
1551da177e4SLinus Torvalds 	int	    remove_bhv)
1561da177e4SLinus Torvalds {
1571da177e4SLinus Torvalds 	if (mp->m_ihash)
1581da177e4SLinus Torvalds 		xfs_ihash_free(mp);
1591da177e4SLinus Torvalds 	if (mp->m_chash)
1601da177e4SLinus Torvalds 		xfs_chash_free(mp);
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	if (mp->m_perag) {
1631da177e4SLinus Torvalds 		int	agno;
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 		for (agno = 0; agno < mp->m_maxagi; agno++)
1661da177e4SLinus Torvalds 			if (mp->m_perag[agno].pagb_list)
1671da177e4SLinus Torvalds 				kmem_free(mp->m_perag[agno].pagb_list,
1681da177e4SLinus Torvalds 						sizeof(xfs_perag_busy_t) *
1691da177e4SLinus Torvalds 							XFS_PAGB_NUM_SLOTS);
1701da177e4SLinus Torvalds 		kmem_free(mp->m_perag,
1711da177e4SLinus Torvalds 			  sizeof(xfs_perag_t) * mp->m_sb.sb_agcount);
1721da177e4SLinus Torvalds 	}
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	AIL_LOCK_DESTROY(&mp->m_ail_lock);
1751da177e4SLinus Torvalds 	spinlock_destroy(&mp->m_sb_lock);
1761da177e4SLinus Torvalds 	mutex_destroy(&mp->m_ilock);
1771da177e4SLinus Torvalds 	freesema(&mp->m_growlock);
1781da177e4SLinus Torvalds 	if (mp->m_quotainfo)
1791da177e4SLinus Torvalds 		XFS_QM_DONE(mp);
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	if (mp->m_fsname != NULL)
1821da177e4SLinus Torvalds 		kmem_free(mp->m_fsname, mp->m_fsname_len);
183*fc1f8c1cSNathan Scott 	if (mp->m_rtname != NULL)
184*fc1f8c1cSNathan Scott 		kmem_free(mp->m_rtname, strlen(mp->m_rtname) + 1);
185*fc1f8c1cSNathan Scott 	if (mp->m_logname != NULL)
186*fc1f8c1cSNathan Scott 		kmem_free(mp->m_logname, strlen(mp->m_logname) + 1);
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	if (remove_bhv) {
1891da177e4SLinus Torvalds 		struct vfs	*vfsp = XFS_MTOVFS(mp);
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 		bhv_remove_all_vfsops(vfsp, 0);
1921da177e4SLinus Torvalds 		VFS_REMOVEBHV(vfsp, &mp->m_bhv);
1931da177e4SLinus Torvalds 	}
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	kmem_free(mp, sizeof(xfs_mount_t));
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds /*
2001da177e4SLinus Torvalds  * Check the validity of the SB found.
2011da177e4SLinus Torvalds  */
2021da177e4SLinus Torvalds STATIC int
2031da177e4SLinus Torvalds xfs_mount_validate_sb(
2041da177e4SLinus Torvalds 	xfs_mount_t	*mp,
2051da177e4SLinus Torvalds 	xfs_sb_t	*sbp)
2061da177e4SLinus Torvalds {
2071da177e4SLinus Torvalds 	/*
2081da177e4SLinus Torvalds 	 * If the log device and data device have the
2091da177e4SLinus Torvalds 	 * same device number, the log is internal.
2101da177e4SLinus Torvalds 	 * Consequently, the sb_logstart should be non-zero.  If
2111da177e4SLinus Torvalds 	 * we have a zero sb_logstart in this case, we may be trying to mount
2121da177e4SLinus Torvalds 	 * a volume filesystem in a non-volume manner.
2131da177e4SLinus Torvalds 	 */
2141da177e4SLinus Torvalds 	if (sbp->sb_magicnum != XFS_SB_MAGIC) {
2151da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: bad magic number");
2161da177e4SLinus Torvalds 		return XFS_ERROR(EWRONGFS);
2171da177e4SLinus Torvalds 	}
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	if (!XFS_SB_GOOD_VERSION(sbp)) {
2201da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: bad version");
2211da177e4SLinus Torvalds 		return XFS_ERROR(EWRONGFS);
2221da177e4SLinus Torvalds 	}
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	if (unlikely(
2251da177e4SLinus Torvalds 	    sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
2261da177e4SLinus Torvalds 		cmn_err(CE_WARN,
2271da177e4SLinus Torvalds 	"XFS: filesystem is marked as having an external log; "
2281da177e4SLinus Torvalds 	"specify logdev on the\nmount command line.");
2291da177e4SLinus Torvalds 		XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(1)",
2301da177e4SLinus Torvalds 				     XFS_ERRLEVEL_HIGH, mp, sbp);
2311da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
2321da177e4SLinus Torvalds 	}
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	if (unlikely(
2351da177e4SLinus Torvalds 	    sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
2361da177e4SLinus Torvalds 		cmn_err(CE_WARN,
2371da177e4SLinus Torvalds 	"XFS: filesystem is marked as having an internal log; "
2381da177e4SLinus Torvalds 	"don't specify logdev on\nthe mount command line.");
2391da177e4SLinus Torvalds 		XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(2)",
2401da177e4SLinus Torvalds 				     XFS_ERRLEVEL_HIGH, mp, sbp);
2411da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
2421da177e4SLinus Torvalds 	}
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	/*
2451da177e4SLinus Torvalds 	 * More sanity checking. These were stolen directly from
2461da177e4SLinus Torvalds 	 * xfs_repair.
2471da177e4SLinus Torvalds 	 */
2481da177e4SLinus Torvalds 	if (unlikely(
2491da177e4SLinus Torvalds 	    sbp->sb_agcount <= 0					||
2501da177e4SLinus Torvalds 	    sbp->sb_sectsize < XFS_MIN_SECTORSIZE			||
2511da177e4SLinus Torvalds 	    sbp->sb_sectsize > XFS_MAX_SECTORSIZE			||
2521da177e4SLinus Torvalds 	    sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG			||
2531da177e4SLinus Torvalds 	    sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG			||
2541da177e4SLinus Torvalds 	    sbp->sb_blocksize < XFS_MIN_BLOCKSIZE			||
2551da177e4SLinus Torvalds 	    sbp->sb_blocksize > XFS_MAX_BLOCKSIZE			||
2561da177e4SLinus Torvalds 	    sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG			||
2571da177e4SLinus Torvalds 	    sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG			||
2581da177e4SLinus Torvalds 	    sbp->sb_inodesize < XFS_DINODE_MIN_SIZE			||
2591da177e4SLinus Torvalds 	    sbp->sb_inodesize > XFS_DINODE_MAX_SIZE			||
2601da177e4SLinus Torvalds 	    (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE)	||
2611da177e4SLinus Torvalds 	    (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE)	||
2621da177e4SLinus Torvalds 	    sbp->sb_imax_pct > 100)) {
2631da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: SB sanity check 1 failed");
2641da177e4SLinus Torvalds 		XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(3)",
2651da177e4SLinus Torvalds 				     XFS_ERRLEVEL_LOW, mp, sbp);
2661da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
2671da177e4SLinus Torvalds 	}
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	/*
2701da177e4SLinus Torvalds 	 * Sanity check AG count, size fields against data size field
2711da177e4SLinus Torvalds 	 */
2721da177e4SLinus Torvalds 	if (unlikely(
2731da177e4SLinus Torvalds 	    sbp->sb_dblocks == 0 ||
2741da177e4SLinus Torvalds 	    sbp->sb_dblocks >
2751da177e4SLinus Torvalds 	     (xfs_drfsbno_t)sbp->sb_agcount * sbp->sb_agblocks ||
2761da177e4SLinus Torvalds 	    sbp->sb_dblocks < (xfs_drfsbno_t)(sbp->sb_agcount - 1) *
2771da177e4SLinus Torvalds 			      sbp->sb_agblocks + XFS_MIN_AG_BLOCKS)) {
2781da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: SB sanity check 2 failed");
2791da177e4SLinus Torvalds 		XFS_ERROR_REPORT("xfs_mount_validate_sb(4)",
2801da177e4SLinus Torvalds 				 XFS_ERRLEVEL_LOW, mp);
2811da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
2821da177e4SLinus Torvalds 	}
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
2851da177e4SLinus Torvalds 	ASSERT(sbp->sb_blocklog >= BBSHIFT);
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds #if XFS_BIG_BLKNOS     /* Limited by ULONG_MAX of page cache index */
2881da177e4SLinus Torvalds 	if (unlikely(
2891da177e4SLinus Torvalds 	    (sbp->sb_dblocks >> (PAGE_SHIFT - sbp->sb_blocklog)) > ULONG_MAX ||
2901da177e4SLinus Torvalds 	    (sbp->sb_rblocks >> (PAGE_SHIFT - sbp->sb_blocklog)) > ULONG_MAX)) {
2911da177e4SLinus Torvalds #else                  /* Limited by UINT_MAX of sectors */
2921da177e4SLinus Torvalds 	if (unlikely(
2931da177e4SLinus Torvalds 	    (sbp->sb_dblocks << (sbp->sb_blocklog - BBSHIFT)) > UINT_MAX ||
2941da177e4SLinus Torvalds 	    (sbp->sb_rblocks << (sbp->sb_blocklog - BBSHIFT)) > UINT_MAX)) {
2951da177e4SLinus Torvalds #endif
2961da177e4SLinus Torvalds 		cmn_err(CE_WARN,
2971da177e4SLinus Torvalds 	"XFS: File system is too large to be mounted on this system.");
2981da177e4SLinus Torvalds 		return XFS_ERROR(E2BIG);
2991da177e4SLinus Torvalds 	}
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	if (unlikely(sbp->sb_inprogress)) {
3021da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: file system busy");
3031da177e4SLinus Torvalds 		XFS_ERROR_REPORT("xfs_mount_validate_sb(5)",
3041da177e4SLinus Torvalds 				 XFS_ERRLEVEL_LOW, mp);
3051da177e4SLinus Torvalds 		return XFS_ERROR(EFSCORRUPTED);
3061da177e4SLinus Torvalds 	}
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	/*
309de20614bSNathan Scott 	 * Version 1 directory format has never worked on Linux.
310de20614bSNathan Scott 	 */
311de20614bSNathan Scott 	if (unlikely(!XFS_SB_VERSION_HASDIRV2(sbp))) {
312de20614bSNathan Scott 		cmn_err(CE_WARN,
313de20614bSNathan Scott 	"XFS: Attempted to mount file system using version 1 directory format");
314de20614bSNathan Scott 		return XFS_ERROR(ENOSYS);
315de20614bSNathan Scott 	}
316de20614bSNathan Scott 
317de20614bSNathan Scott 	/*
3181da177e4SLinus Torvalds 	 * Until this is fixed only page-sized or smaller data blocks work.
3191da177e4SLinus Torvalds 	 */
3201da177e4SLinus Torvalds 	if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
3211da177e4SLinus Torvalds 		cmn_err(CE_WARN,
3221da177e4SLinus Torvalds 		"XFS: Attempted to mount file system with blocksize %d bytes",
3231da177e4SLinus Torvalds 			sbp->sb_blocksize);
3241da177e4SLinus Torvalds 		cmn_err(CE_WARN,
325da1650a5SChristoph Hellwig 		"XFS: Only page-sized (%ld) or less blocksizes currently work.",
3261da177e4SLinus Torvalds 			PAGE_SIZE);
3271da177e4SLinus Torvalds 		return XFS_ERROR(ENOSYS);
3281da177e4SLinus Torvalds 	}
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	return 0;
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds xfs_agnumber_t
3341da177e4SLinus Torvalds xfs_initialize_perag(xfs_mount_t *mp, xfs_agnumber_t agcount)
3351da177e4SLinus Torvalds {
3361da177e4SLinus Torvalds 	xfs_agnumber_t	index, max_metadata;
3371da177e4SLinus Torvalds 	xfs_perag_t	*pag;
3381da177e4SLinus Torvalds 	xfs_agino_t	agino;
3391da177e4SLinus Torvalds 	xfs_ino_t	ino;
3401da177e4SLinus Torvalds 	xfs_sb_t	*sbp = &mp->m_sb;
3411da177e4SLinus Torvalds 	xfs_ino_t	max_inum = XFS_MAXINUMBER_32;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	/* Check to see if the filesystem can overflow 32 bit inodes */
3441da177e4SLinus Torvalds 	agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
3451da177e4SLinus Torvalds 	ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	/* Clear the mount flag if no inode can overflow 32 bits
3481da177e4SLinus Torvalds 	 * on this filesystem, or if specifically requested..
3491da177e4SLinus Torvalds 	 */
3501da177e4SLinus Torvalds 	if ((mp->m_flags & XFS_MOUNT_32BITINOOPT) && ino > max_inum) {
3511da177e4SLinus Torvalds 		mp->m_flags |= XFS_MOUNT_32BITINODES;
3521da177e4SLinus Torvalds 	} else {
3531da177e4SLinus Torvalds 		mp->m_flags &= ~XFS_MOUNT_32BITINODES;
3541da177e4SLinus Torvalds 	}
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	/* If we can overflow then setup the ag headers accordingly */
3571da177e4SLinus Torvalds 	if (mp->m_flags & XFS_MOUNT_32BITINODES) {
3581da177e4SLinus Torvalds 		/* Calculate how much should be reserved for inodes to
3591da177e4SLinus Torvalds 		 * meet the max inode percentage.
3601da177e4SLinus Torvalds 		 */
3611da177e4SLinus Torvalds 		if (mp->m_maxicount) {
3621da177e4SLinus Torvalds 			__uint64_t	icount;
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 			icount = sbp->sb_dblocks * sbp->sb_imax_pct;
3651da177e4SLinus Torvalds 			do_div(icount, 100);
3661da177e4SLinus Torvalds 			icount += sbp->sb_agblocks - 1;
3671da177e4SLinus Torvalds 			do_div(icount, mp->m_ialloc_blks);
3681da177e4SLinus Torvalds 			max_metadata = icount;
3691da177e4SLinus Torvalds 		} else {
3701da177e4SLinus Torvalds 			max_metadata = agcount;
3711da177e4SLinus Torvalds 		}
3721da177e4SLinus Torvalds 		for (index = 0; index < agcount; index++) {
3731da177e4SLinus Torvalds 			ino = XFS_AGINO_TO_INO(mp, index, agino);
3741da177e4SLinus Torvalds 			if (ino > max_inum) {
3751da177e4SLinus Torvalds 				index++;
3761da177e4SLinus Torvalds 				break;
3771da177e4SLinus Torvalds 			}
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 			/* This ag is prefered for inodes */
3801da177e4SLinus Torvalds 			pag = &mp->m_perag[index];
3811da177e4SLinus Torvalds 			pag->pagi_inodeok = 1;
3821da177e4SLinus Torvalds 			if (index < max_metadata)
3831da177e4SLinus Torvalds 				pag->pagf_metadata = 1;
3841da177e4SLinus Torvalds 		}
3851da177e4SLinus Torvalds 	} else {
3861da177e4SLinus Torvalds 		/* Setup default behavior for smaller filesystems */
3871da177e4SLinus Torvalds 		for (index = 0; index < agcount; index++) {
3881da177e4SLinus Torvalds 			pag = &mp->m_perag[index];
3891da177e4SLinus Torvalds 			pag->pagi_inodeok = 1;
3901da177e4SLinus Torvalds 		}
3911da177e4SLinus Torvalds 	}
3921da177e4SLinus Torvalds 	return index;
3931da177e4SLinus Torvalds }
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds /*
3961da177e4SLinus Torvalds  * xfs_xlatesb
3971da177e4SLinus Torvalds  *
3981da177e4SLinus Torvalds  *     data       - on disk version of sb
3991da177e4SLinus Torvalds  *     sb         - a superblock
4001da177e4SLinus Torvalds  *     dir        - conversion direction: <0 - convert sb to buf
4011da177e4SLinus Torvalds  *                                        >0 - convert buf to sb
4021da177e4SLinus Torvalds  *     fields     - which fields to copy (bitmask)
4031da177e4SLinus Torvalds  */
4041da177e4SLinus Torvalds void
4051da177e4SLinus Torvalds xfs_xlatesb(
4061da177e4SLinus Torvalds 	void		*data,
4071da177e4SLinus Torvalds 	xfs_sb_t	*sb,
4081da177e4SLinus Torvalds 	int		dir,
4091da177e4SLinus Torvalds 	__int64_t	fields)
4101da177e4SLinus Torvalds {
4111da177e4SLinus Torvalds 	xfs_caddr_t	buf_ptr;
4121da177e4SLinus Torvalds 	xfs_caddr_t	mem_ptr;
4131da177e4SLinus Torvalds 	xfs_sb_field_t	f;
4141da177e4SLinus Torvalds 	int		first;
4151da177e4SLinus Torvalds 	int		size;
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	ASSERT(dir);
4181da177e4SLinus Torvalds 	ASSERT(fields);
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	if (!fields)
4211da177e4SLinus Torvalds 		return;
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	buf_ptr = (xfs_caddr_t)data;
4241da177e4SLinus Torvalds 	mem_ptr = (xfs_caddr_t)sb;
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	while (fields) {
4271da177e4SLinus Torvalds 		f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
4281da177e4SLinus Torvalds 		first = xfs_sb_info[f].offset;
4291da177e4SLinus Torvalds 		size = xfs_sb_info[f + 1].offset - first;
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 		ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 		if (size == 1 || xfs_sb_info[f].type == 1) {
4341da177e4SLinus Torvalds 			if (dir > 0) {
4351da177e4SLinus Torvalds 				memcpy(mem_ptr + first, buf_ptr + first, size);
4361da177e4SLinus Torvalds 			} else {
4371da177e4SLinus Torvalds 				memcpy(buf_ptr + first, mem_ptr + first, size);
4381da177e4SLinus Torvalds 			}
4391da177e4SLinus Torvalds 		} else {
4401da177e4SLinus Torvalds 			switch (size) {
4411da177e4SLinus Torvalds 			case 2:
4421da177e4SLinus Torvalds 				INT_XLATE(*(__uint16_t*)(buf_ptr+first),
4431da177e4SLinus Torvalds 					  *(__uint16_t*)(mem_ptr+first),
4441da177e4SLinus Torvalds 					  dir, ARCH_CONVERT);
4451da177e4SLinus Torvalds 				break;
4461da177e4SLinus Torvalds 			case 4:
4471da177e4SLinus Torvalds 				INT_XLATE(*(__uint32_t*)(buf_ptr+first),
4481da177e4SLinus Torvalds 					  *(__uint32_t*)(mem_ptr+first),
4491da177e4SLinus Torvalds 					  dir, ARCH_CONVERT);
4501da177e4SLinus Torvalds 				break;
4511da177e4SLinus Torvalds 			case 8:
4521da177e4SLinus Torvalds 				INT_XLATE(*(__uint64_t*)(buf_ptr+first),
4531da177e4SLinus Torvalds 					  *(__uint64_t*)(mem_ptr+first), dir, ARCH_CONVERT);
4541da177e4SLinus Torvalds 				break;
4551da177e4SLinus Torvalds 			default:
4561da177e4SLinus Torvalds 				ASSERT(0);
4571da177e4SLinus Torvalds 			}
4581da177e4SLinus Torvalds 		}
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 		fields &= ~(1LL << f);
4611da177e4SLinus Torvalds 	}
4621da177e4SLinus Torvalds }
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds /*
4651da177e4SLinus Torvalds  * xfs_readsb
4661da177e4SLinus Torvalds  *
4671da177e4SLinus Torvalds  * Does the initial read of the superblock.
4681da177e4SLinus Torvalds  */
4691da177e4SLinus Torvalds int
4701da177e4SLinus Torvalds xfs_readsb(xfs_mount_t *mp)
4711da177e4SLinus Torvalds {
4721da177e4SLinus Torvalds 	unsigned int	sector_size;
4731da177e4SLinus Torvalds 	unsigned int	extra_flags;
4741da177e4SLinus Torvalds 	xfs_buf_t	*bp;
4751da177e4SLinus Torvalds 	xfs_sb_t	*sbp;
4761da177e4SLinus Torvalds 	int		error;
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	ASSERT(mp->m_sb_bp == NULL);
4791da177e4SLinus Torvalds 	ASSERT(mp->m_ddev_targp != NULL);
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	/*
4821da177e4SLinus Torvalds 	 * Allocate a (locked) buffer to hold the superblock.
4831da177e4SLinus Torvalds 	 * This will be kept around at all times to optimize
4841da177e4SLinus Torvalds 	 * access to the superblock.
4851da177e4SLinus Torvalds 	 */
4861da177e4SLinus Torvalds 	sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
4871da177e4SLinus Torvalds 	extra_flags = XFS_BUF_LOCK | XFS_BUF_MANAGE | XFS_BUF_MAPPED;
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
4901da177e4SLinus Torvalds 				BTOBB(sector_size), extra_flags);
4911da177e4SLinus Torvalds 	if (!bp || XFS_BUF_ISERROR(bp)) {
4921da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: SB read failed");
4931da177e4SLinus Torvalds 		error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
4941da177e4SLinus Torvalds 		goto fail;
4951da177e4SLinus Torvalds 	}
4961da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISBUSY(bp));
4971da177e4SLinus Torvalds 	ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	/*
5001da177e4SLinus Torvalds 	 * Initialize the mount structure from the superblock.
5011da177e4SLinus Torvalds 	 * But first do some basic consistency checking.
5021da177e4SLinus Torvalds 	 */
5031da177e4SLinus Torvalds 	sbp = XFS_BUF_TO_SBP(bp);
5041da177e4SLinus Torvalds 	xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), 1, XFS_SB_ALL_BITS);
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	error = xfs_mount_validate_sb(mp, &(mp->m_sb));
5071da177e4SLinus Torvalds 	if (error) {
5081da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: SB validate failed");
5091da177e4SLinus Torvalds 		goto fail;
5101da177e4SLinus Torvalds 	}
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	/*
5131da177e4SLinus Torvalds 	 * We must be able to do sector-sized and sector-aligned IO.
5141da177e4SLinus Torvalds 	 */
5151da177e4SLinus Torvalds 	if (sector_size > mp->m_sb.sb_sectsize) {
5161da177e4SLinus Torvalds 		cmn_err(CE_WARN,
5171da177e4SLinus Torvalds 			"XFS: device supports only %u byte sectors (not %u)",
5181da177e4SLinus Torvalds 			sector_size, mp->m_sb.sb_sectsize);
5191da177e4SLinus Torvalds 		error = ENOSYS;
5201da177e4SLinus Torvalds 		goto fail;
5211da177e4SLinus Torvalds 	}
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	/*
5241da177e4SLinus Torvalds 	 * If device sector size is smaller than the superblock size,
5251da177e4SLinus Torvalds 	 * re-read the superblock so the buffer is correctly sized.
5261da177e4SLinus Torvalds 	 */
5271da177e4SLinus Torvalds 	if (sector_size < mp->m_sb.sb_sectsize) {
5281da177e4SLinus Torvalds 		XFS_BUF_UNMANAGE(bp);
5291da177e4SLinus Torvalds 		xfs_buf_relse(bp);
5301da177e4SLinus Torvalds 		sector_size = mp->m_sb.sb_sectsize;
5311da177e4SLinus Torvalds 		bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
5321da177e4SLinus Torvalds 					BTOBB(sector_size), extra_flags);
5331da177e4SLinus Torvalds 		if (!bp || XFS_BUF_ISERROR(bp)) {
5341da177e4SLinus Torvalds 			cmn_err(CE_WARN, "XFS: SB re-read failed");
5351da177e4SLinus Torvalds 			error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
5361da177e4SLinus Torvalds 			goto fail;
5371da177e4SLinus Torvalds 		}
5381da177e4SLinus Torvalds 		ASSERT(XFS_BUF_ISBUSY(bp));
5391da177e4SLinus Torvalds 		ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
5401da177e4SLinus Torvalds 	}
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	mp->m_sb_bp = bp;
5431da177e4SLinus Torvalds 	xfs_buf_relse(bp);
5441da177e4SLinus Torvalds 	ASSERT(XFS_BUF_VALUSEMA(bp) > 0);
5451da177e4SLinus Torvalds 	return 0;
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds  fail:
5481da177e4SLinus Torvalds 	if (bp) {
5491da177e4SLinus Torvalds 		XFS_BUF_UNMANAGE(bp);
5501da177e4SLinus Torvalds 		xfs_buf_relse(bp);
5511da177e4SLinus Torvalds 	}
5521da177e4SLinus Torvalds 	return error;
5531da177e4SLinus Torvalds }
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds /*
5571da177e4SLinus Torvalds  * xfs_mount_common
5581da177e4SLinus Torvalds  *
5591da177e4SLinus Torvalds  * Mount initialization code establishing various mount
5601da177e4SLinus Torvalds  * fields from the superblock associated with the given
5611da177e4SLinus Torvalds  * mount structure
5621da177e4SLinus Torvalds  */
563ba0f32d4SChristoph Hellwig STATIC void
5641da177e4SLinus Torvalds xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
5651da177e4SLinus Torvalds {
5661da177e4SLinus Torvalds 	int	i;
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	mp->m_agfrotor = mp->m_agirotor = 0;
5691da177e4SLinus Torvalds 	spinlock_init(&mp->m_agirotor_lock, "m_agirotor_lock");
5701da177e4SLinus Torvalds 	mp->m_maxagi = mp->m_sb.sb_agcount;
5711da177e4SLinus Torvalds 	mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
5721da177e4SLinus Torvalds 	mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
5731da177e4SLinus Torvalds 	mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
5741da177e4SLinus Torvalds 	mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
5751da177e4SLinus Torvalds 	mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
5761da177e4SLinus Torvalds 	mp->m_litino = sbp->sb_inodesize -
5771da177e4SLinus Torvalds 		((uint)sizeof(xfs_dinode_core_t) + (uint)sizeof(xfs_agino_t));
5781da177e4SLinus Torvalds 	mp->m_blockmask = sbp->sb_blocksize - 1;
5791da177e4SLinus Torvalds 	mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
5801da177e4SLinus Torvalds 	mp->m_blockwmask = mp->m_blockwsize - 1;
5811da177e4SLinus Torvalds 	INIT_LIST_HEAD(&mp->m_del_inodes);
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	/*
5841da177e4SLinus Torvalds 	 * Setup for attributes, in case they get created.
5851da177e4SLinus Torvalds 	 * This value is for inodes getting attributes for the first time,
5861da177e4SLinus Torvalds 	 * the per-inode value is for old attribute values.
5871da177e4SLinus Torvalds 	 */
5881da177e4SLinus Torvalds 	ASSERT(sbp->sb_inodesize >= 256 && sbp->sb_inodesize <= 2048);
5891da177e4SLinus Torvalds 	switch (sbp->sb_inodesize) {
5901da177e4SLinus Torvalds 	case 256:
591d8cc890dSNathan Scott 		mp->m_attroffset = XFS_LITINO(mp) -
592d8cc890dSNathan Scott 				   XFS_BMDR_SPACE_CALC(MINABTPTRS);
5931da177e4SLinus Torvalds 		break;
5941da177e4SLinus Torvalds 	case 512:
5951da177e4SLinus Torvalds 	case 1024:
5961da177e4SLinus Torvalds 	case 2048:
597d8cc890dSNathan Scott 		mp->m_attroffset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
5981da177e4SLinus Torvalds 		break;
5991da177e4SLinus Torvalds 	default:
6001da177e4SLinus Torvalds 		ASSERT(0);
6011da177e4SLinus Torvalds 	}
6021da177e4SLinus Torvalds 	ASSERT(mp->m_attroffset < XFS_LITINO(mp));
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	for (i = 0; i < 2; i++) {
6051da177e4SLinus Torvalds 		mp->m_alloc_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
6061da177e4SLinus Torvalds 			xfs_alloc, i == 0);
6071da177e4SLinus Torvalds 		mp->m_alloc_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
6081da177e4SLinus Torvalds 			xfs_alloc, i == 0);
6091da177e4SLinus Torvalds 	}
6101da177e4SLinus Torvalds 	for (i = 0; i < 2; i++) {
6111da177e4SLinus Torvalds 		mp->m_bmap_dmxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
6121da177e4SLinus Torvalds 			xfs_bmbt, i == 0);
6131da177e4SLinus Torvalds 		mp->m_bmap_dmnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
6141da177e4SLinus Torvalds 			xfs_bmbt, i == 0);
6151da177e4SLinus Torvalds 	}
6161da177e4SLinus Torvalds 	for (i = 0; i < 2; i++) {
6171da177e4SLinus Torvalds 		mp->m_inobt_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
6181da177e4SLinus Torvalds 			xfs_inobt, i == 0);
6191da177e4SLinus Torvalds 		mp->m_inobt_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
6201da177e4SLinus Torvalds 			xfs_inobt, i == 0);
6211da177e4SLinus Torvalds 	}
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
6241da177e4SLinus Torvalds 	mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
6251da177e4SLinus Torvalds 					sbp->sb_inopblock);
6261da177e4SLinus Torvalds 	mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
6271da177e4SLinus Torvalds }
6281da177e4SLinus Torvalds /*
6291da177e4SLinus Torvalds  * xfs_mountfs
6301da177e4SLinus Torvalds  *
6311da177e4SLinus Torvalds  * This function does the following on an initial mount of a file system:
6321da177e4SLinus Torvalds  *	- reads the superblock from disk and init the mount struct
6331da177e4SLinus Torvalds  *	- if we're a 32-bit kernel, do a size check on the superblock
6341da177e4SLinus Torvalds  *		so we don't mount terabyte filesystems
6351da177e4SLinus Torvalds  *	- init mount struct realtime fields
6361da177e4SLinus Torvalds  *	- allocate inode hash table for fs
6371da177e4SLinus Torvalds  *	- init directory manager
6381da177e4SLinus Torvalds  *	- perform recovery and init the log manager
6391da177e4SLinus Torvalds  */
6401da177e4SLinus Torvalds int
6411da177e4SLinus Torvalds xfs_mountfs(
6421da177e4SLinus Torvalds 	vfs_t		*vfsp,
6431da177e4SLinus Torvalds 	xfs_mount_t	*mp,
6441da177e4SLinus Torvalds 	int		mfsi_flags)
6451da177e4SLinus Torvalds {
6461da177e4SLinus Torvalds 	xfs_buf_t	*bp;
6471da177e4SLinus Torvalds 	xfs_sb_t	*sbp = &(mp->m_sb);
6481da177e4SLinus Torvalds 	xfs_inode_t	*rip;
6491da177e4SLinus Torvalds 	vnode_t		*rvp = NULL;
6501da177e4SLinus Torvalds 	int		readio_log, writeio_log;
6511da177e4SLinus Torvalds 	xfs_daddr_t	d;
6521da177e4SLinus Torvalds 	__uint64_t	ret64;
6531da177e4SLinus Torvalds 	__int64_t	update_flags;
6541da177e4SLinus Torvalds 	uint		quotamount, quotaflags;
6551da177e4SLinus Torvalds 	int		agno;
6561da177e4SLinus Torvalds 	int		uuid_mounted = 0;
6571da177e4SLinus Torvalds 	int		error = 0;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	if (mp->m_sb_bp == NULL) {
6601da177e4SLinus Torvalds 		if ((error = xfs_readsb(mp))) {
6611da177e4SLinus Torvalds 			return (error);
6621da177e4SLinus Torvalds 		}
6631da177e4SLinus Torvalds 	}
6641da177e4SLinus Torvalds 	xfs_mount_common(mp, sbp);
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	/*
6671da177e4SLinus Torvalds 	 * Check if sb_agblocks is aligned at stripe boundary
6681da177e4SLinus Torvalds 	 * If sb_agblocks is NOT aligned turn off m_dalign since
6691da177e4SLinus Torvalds 	 * allocator alignment is within an ag, therefore ag has
6701da177e4SLinus Torvalds 	 * to be aligned at stripe boundary.
6711da177e4SLinus Torvalds 	 */
6721da177e4SLinus Torvalds 	update_flags = 0LL;
6731da177e4SLinus Torvalds 	if (mp->m_dalign && !(mfsi_flags & XFS_MFSI_SECOND)) {
6741da177e4SLinus Torvalds 		/*
6751da177e4SLinus Torvalds 		 * If stripe unit and stripe width are not multiples
6761da177e4SLinus Torvalds 		 * of the fs blocksize turn off alignment.
6771da177e4SLinus Torvalds 		 */
6781da177e4SLinus Torvalds 		if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
6791da177e4SLinus Torvalds 		    (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
6801da177e4SLinus Torvalds 			if (mp->m_flags & XFS_MOUNT_RETERR) {
6811da177e4SLinus Torvalds 				cmn_err(CE_WARN,
6821da177e4SLinus Torvalds 					"XFS: alignment check 1 failed");
6831da177e4SLinus Torvalds 				error = XFS_ERROR(EINVAL);
6841da177e4SLinus Torvalds 				goto error1;
6851da177e4SLinus Torvalds 			}
6861da177e4SLinus Torvalds 			mp->m_dalign = mp->m_swidth = 0;
6871da177e4SLinus Torvalds 		} else {
6881da177e4SLinus Torvalds 			/*
6891da177e4SLinus Torvalds 			 * Convert the stripe unit and width to FSBs.
6901da177e4SLinus Torvalds 			 */
6911da177e4SLinus Torvalds 			mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
6921da177e4SLinus Torvalds 			if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
6931da177e4SLinus Torvalds 				if (mp->m_flags & XFS_MOUNT_RETERR) {
6941da177e4SLinus Torvalds 					error = XFS_ERROR(EINVAL);
6951da177e4SLinus Torvalds 					goto error1;
6961da177e4SLinus Torvalds 				}
6971da177e4SLinus Torvalds 				xfs_fs_cmn_err(CE_WARN, mp,
6981da177e4SLinus Torvalds "stripe alignment turned off: sunit(%d)/swidth(%d) incompatible with agsize(%d)",
6991da177e4SLinus Torvalds 					mp->m_dalign, mp->m_swidth,
7001da177e4SLinus Torvalds 					sbp->sb_agblocks);
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 				mp->m_dalign = 0;
7031da177e4SLinus Torvalds 				mp->m_swidth = 0;
7041da177e4SLinus Torvalds 			} else if (mp->m_dalign) {
7051da177e4SLinus Torvalds 				mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
7061da177e4SLinus Torvalds 			} else {
7071da177e4SLinus Torvalds 				if (mp->m_flags & XFS_MOUNT_RETERR) {
7081da177e4SLinus Torvalds 					xfs_fs_cmn_err(CE_WARN, mp,
7091da177e4SLinus Torvalds "stripe alignment turned off: sunit(%d) less than bsize(%d)",
7101da177e4SLinus Torvalds                                         	mp->m_dalign,
7111da177e4SLinus Torvalds 						mp->m_blockmask +1);
7121da177e4SLinus Torvalds 					error = XFS_ERROR(EINVAL);
7131da177e4SLinus Torvalds 					goto error1;
7141da177e4SLinus Torvalds 				}
7151da177e4SLinus Torvalds 				mp->m_swidth = 0;
7161da177e4SLinus Torvalds 			}
7171da177e4SLinus Torvalds 		}
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 		/*
7201da177e4SLinus Torvalds 		 * Update superblock with new values
7211da177e4SLinus Torvalds 		 * and log changes
7221da177e4SLinus Torvalds 		 */
7231da177e4SLinus Torvalds 		if (XFS_SB_VERSION_HASDALIGN(sbp)) {
7241da177e4SLinus Torvalds 			if (sbp->sb_unit != mp->m_dalign) {
7251da177e4SLinus Torvalds 				sbp->sb_unit = mp->m_dalign;
7261da177e4SLinus Torvalds 				update_flags |= XFS_SB_UNIT;
7271da177e4SLinus Torvalds 			}
7281da177e4SLinus Torvalds 			if (sbp->sb_width != mp->m_swidth) {
7291da177e4SLinus Torvalds 				sbp->sb_width = mp->m_swidth;
7301da177e4SLinus Torvalds 				update_flags |= XFS_SB_WIDTH;
7311da177e4SLinus Torvalds 			}
7321da177e4SLinus Torvalds 		}
7331da177e4SLinus Torvalds 	} else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
7341da177e4SLinus Torvalds 		    XFS_SB_VERSION_HASDALIGN(&mp->m_sb)) {
7351da177e4SLinus Torvalds 			mp->m_dalign = sbp->sb_unit;
7361da177e4SLinus Torvalds 			mp->m_swidth = sbp->sb_width;
7371da177e4SLinus Torvalds 	}
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	xfs_alloc_compute_maxlevels(mp);
7401da177e4SLinus Torvalds 	xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
7411da177e4SLinus Torvalds 	xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
7421da177e4SLinus Torvalds 	xfs_ialloc_compute_maxlevels(mp);
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	if (sbp->sb_imax_pct) {
7451da177e4SLinus Torvalds 		__uint64_t	icount;
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 		/* Make sure the maximum inode count is a multiple of the
7481da177e4SLinus Torvalds 		 * units we allocate inodes in.
7491da177e4SLinus Torvalds 		 */
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 		icount = sbp->sb_dblocks * sbp->sb_imax_pct;
7521da177e4SLinus Torvalds 		do_div(icount, 100);
7531da177e4SLinus Torvalds 		do_div(icount, mp->m_ialloc_blks);
7541da177e4SLinus Torvalds 		mp->m_maxicount = (icount * mp->m_ialloc_blks)  <<
7551da177e4SLinus Torvalds 				   sbp->sb_inopblog;
7561da177e4SLinus Torvalds 	} else
7571da177e4SLinus Torvalds 		mp->m_maxicount = 0;
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog);
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	/*
7621da177e4SLinus Torvalds 	 * XFS uses the uuid from the superblock as the unique
7631da177e4SLinus Torvalds 	 * identifier for fsid.  We can not use the uuid from the volume
7641da177e4SLinus Torvalds 	 * since a single partition filesystem is identical to a single
7651da177e4SLinus Torvalds 	 * partition volume/filesystem.
7661da177e4SLinus Torvalds 	 */
7671da177e4SLinus Torvalds 	if ((mfsi_flags & XFS_MFSI_SECOND) == 0 &&
7681da177e4SLinus Torvalds 	    (mp->m_flags & XFS_MOUNT_NOUUID) == 0) {
7691da177e4SLinus Torvalds 		if (xfs_uuid_mount(mp)) {
7701da177e4SLinus Torvalds 			error = XFS_ERROR(EINVAL);
7711da177e4SLinus Torvalds 			goto error1;
7721da177e4SLinus Torvalds 		}
7731da177e4SLinus Torvalds 		uuid_mounted=1;
7741da177e4SLinus Torvalds 		ret64 = uuid_hash64(&sbp->sb_uuid);
7751da177e4SLinus Torvalds 		memcpy(&vfsp->vfs_fsid, &ret64, sizeof(ret64));
7761da177e4SLinus Torvalds 	}
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	/*
7791da177e4SLinus Torvalds 	 * Set the default minimum read and write sizes unless
7801da177e4SLinus Torvalds 	 * already specified in a mount option.
7811da177e4SLinus Torvalds 	 * We use smaller I/O sizes when the file system
7821da177e4SLinus Torvalds 	 * is being used for NFS service (wsync mount option).
7831da177e4SLinus Torvalds 	 */
7841da177e4SLinus Torvalds 	if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
7851da177e4SLinus Torvalds 		if (mp->m_flags & XFS_MOUNT_WSYNC) {
7861da177e4SLinus Torvalds 			readio_log = XFS_WSYNC_READIO_LOG;
7871da177e4SLinus Torvalds 			writeio_log = XFS_WSYNC_WRITEIO_LOG;
7881da177e4SLinus Torvalds 		} else {
7891da177e4SLinus Torvalds 			readio_log = XFS_READIO_LOG_LARGE;
7901da177e4SLinus Torvalds 			writeio_log = XFS_WRITEIO_LOG_LARGE;
7911da177e4SLinus Torvalds 		}
7921da177e4SLinus Torvalds 	} else {
7931da177e4SLinus Torvalds 		readio_log = mp->m_readio_log;
7941da177e4SLinus Torvalds 		writeio_log = mp->m_writeio_log;
7951da177e4SLinus Torvalds 	}
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 	/*
7981da177e4SLinus Torvalds 	 * Set the number of readahead buffers to use based on
7991da177e4SLinus Torvalds 	 * physical memory size.
8001da177e4SLinus Torvalds 	 */
8011da177e4SLinus Torvalds 	if (xfs_physmem <= 4096)		/* <= 16MB */
8021da177e4SLinus Torvalds 		mp->m_nreadaheads = XFS_RW_NREADAHEAD_16MB;
8031da177e4SLinus Torvalds 	else if (xfs_physmem <= 8192)	/* <= 32MB */
8041da177e4SLinus Torvalds 		mp->m_nreadaheads = XFS_RW_NREADAHEAD_32MB;
8051da177e4SLinus Torvalds 	else
8061da177e4SLinus Torvalds 		mp->m_nreadaheads = XFS_RW_NREADAHEAD_K32;
8071da177e4SLinus Torvalds 	if (sbp->sb_blocklog > readio_log) {
8081da177e4SLinus Torvalds 		mp->m_readio_log = sbp->sb_blocklog;
8091da177e4SLinus Torvalds 	} else {
8101da177e4SLinus Torvalds 		mp->m_readio_log = readio_log;
8111da177e4SLinus Torvalds 	}
8121da177e4SLinus Torvalds 	mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
8131da177e4SLinus Torvalds 	if (sbp->sb_blocklog > writeio_log) {
8141da177e4SLinus Torvalds 		mp->m_writeio_log = sbp->sb_blocklog;
8151da177e4SLinus Torvalds 	} else {
8161da177e4SLinus Torvalds 		mp->m_writeio_log = writeio_log;
8171da177e4SLinus Torvalds 	}
8181da177e4SLinus Torvalds 	mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 	/*
8211da177e4SLinus Torvalds 	 * Set the inode cluster size based on the physical memory
8221da177e4SLinus Torvalds 	 * size.  This may still be overridden by the file system
8231da177e4SLinus Torvalds 	 * block size if it is larger than the chosen cluster size.
8241da177e4SLinus Torvalds 	 */
8251da177e4SLinus Torvalds 	if (xfs_physmem <= btoc(32 * 1024 * 1024)) { /* <= 32 MB */
8261da177e4SLinus Torvalds 		mp->m_inode_cluster_size = XFS_INODE_SMALL_CLUSTER_SIZE;
8271da177e4SLinus Torvalds 	} else {
8281da177e4SLinus Torvalds 		mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
8291da177e4SLinus Torvalds 	}
8301da177e4SLinus Torvalds 	/*
8311da177e4SLinus Torvalds 	 * Set whether we're using inode alignment.
8321da177e4SLinus Torvalds 	 */
8331da177e4SLinus Torvalds 	if (XFS_SB_VERSION_HASALIGN(&mp->m_sb) &&
8341da177e4SLinus Torvalds 	    mp->m_sb.sb_inoalignmt >=
8351da177e4SLinus Torvalds 	    XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
8361da177e4SLinus Torvalds 		mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
8371da177e4SLinus Torvalds 	else
8381da177e4SLinus Torvalds 		mp->m_inoalign_mask = 0;
8391da177e4SLinus Torvalds 	/*
8401da177e4SLinus Torvalds 	 * If we are using stripe alignment, check whether
8411da177e4SLinus Torvalds 	 * the stripe unit is a multiple of the inode alignment
8421da177e4SLinus Torvalds 	 */
8431da177e4SLinus Torvalds 	if (mp->m_dalign && mp->m_inoalign_mask &&
8441da177e4SLinus Torvalds 	    !(mp->m_dalign & mp->m_inoalign_mask))
8451da177e4SLinus Torvalds 		mp->m_sinoalign = mp->m_dalign;
8461da177e4SLinus Torvalds 	else
8471da177e4SLinus Torvalds 		mp->m_sinoalign = 0;
8481da177e4SLinus Torvalds 	/*
8491da177e4SLinus Torvalds 	 * Check that the data (and log if separate) are an ok size.
8501da177e4SLinus Torvalds 	 */
8511da177e4SLinus Torvalds 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
8521da177e4SLinus Torvalds 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
8531da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: size check 1 failed");
8541da177e4SLinus Torvalds 		error = XFS_ERROR(E2BIG);
8551da177e4SLinus Torvalds 		goto error1;
8561da177e4SLinus Torvalds 	}
8571da177e4SLinus Torvalds 	error = xfs_read_buf(mp, mp->m_ddev_targp,
8581da177e4SLinus Torvalds 			     d - XFS_FSS_TO_BB(mp, 1),
8591da177e4SLinus Torvalds 			     XFS_FSS_TO_BB(mp, 1), 0, &bp);
8601da177e4SLinus Torvalds 	if (!error) {
8611da177e4SLinus Torvalds 		xfs_buf_relse(bp);
8621da177e4SLinus Torvalds 	} else {
8631da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: size check 2 failed");
8641da177e4SLinus Torvalds 		if (error == ENOSPC) {
8651da177e4SLinus Torvalds 			error = XFS_ERROR(E2BIG);
8661da177e4SLinus Torvalds 		}
8671da177e4SLinus Torvalds 		goto error1;
8681da177e4SLinus Torvalds 	}
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	if (((mfsi_flags & XFS_MFSI_CLIENT) == 0) &&
8711da177e4SLinus Torvalds 	    mp->m_logdev_targp != mp->m_ddev_targp) {
8721da177e4SLinus Torvalds 		d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
8731da177e4SLinus Torvalds 		if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
8741da177e4SLinus Torvalds 			cmn_err(CE_WARN, "XFS: size check 3 failed");
8751da177e4SLinus Torvalds 			error = XFS_ERROR(E2BIG);
8761da177e4SLinus Torvalds 			goto error1;
8771da177e4SLinus Torvalds 		}
8781da177e4SLinus Torvalds 		error = xfs_read_buf(mp, mp->m_logdev_targp,
8791da177e4SLinus Torvalds 				     d - XFS_FSB_TO_BB(mp, 1),
8801da177e4SLinus Torvalds 				     XFS_FSB_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 3 failed");
8851da177e4SLinus Torvalds 			if (error == ENOSPC) {
8861da177e4SLinus Torvalds 				error = XFS_ERROR(E2BIG);
8871da177e4SLinus Torvalds 			}
8881da177e4SLinus Torvalds 			goto error1;
8891da177e4SLinus Torvalds 		}
8901da177e4SLinus Torvalds 	}
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	/*
8931da177e4SLinus Torvalds 	 * Initialize realtime fields in the mount structure
8941da177e4SLinus Torvalds 	 */
8951da177e4SLinus Torvalds 	if ((error = xfs_rtmount_init(mp))) {
8961da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: RT mount failed");
8971da177e4SLinus Torvalds 		goto error1;
8981da177e4SLinus Torvalds 	}
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 	/*
9011da177e4SLinus Torvalds 	 * For client case we are done now
9021da177e4SLinus Torvalds 	 */
9031da177e4SLinus Torvalds 	if (mfsi_flags & XFS_MFSI_CLIENT) {
9041da177e4SLinus Torvalds 		return(0);
9051da177e4SLinus Torvalds 	}
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	/*
9081da177e4SLinus Torvalds 	 *  Copies the low order bits of the timestamp and the randomly
9091da177e4SLinus Torvalds 	 *  set "sequence" number out of a UUID.
9101da177e4SLinus Torvalds 	 */
9111da177e4SLinus Torvalds 	uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 	/*
9141da177e4SLinus Torvalds 	 *  The vfs structure needs to have a file system independent
9151da177e4SLinus Torvalds 	 *  way of checking for the invariant file system ID.  Since it
9161da177e4SLinus Torvalds 	 *  can't look at mount structures it has a pointer to the data
9171da177e4SLinus Torvalds 	 *  in the mount structure.
9181da177e4SLinus Torvalds 	 *
9191da177e4SLinus Torvalds 	 *  File systems that don't support user level file handles (i.e.
9201da177e4SLinus Torvalds 	 *  all of them except for XFS) will leave vfs_altfsid as NULL.
9211da177e4SLinus Torvalds 	 */
9221da177e4SLinus Torvalds 	vfsp->vfs_altfsid = (xfs_fsid_t *)mp->m_fixedfsid;
9231da177e4SLinus Torvalds 	mp->m_dmevmask = 0;	/* not persistent; set after each mount */
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	/*
9261da177e4SLinus Torvalds 	 * Select the right directory manager.
9271da177e4SLinus Torvalds 	 */
9281da177e4SLinus Torvalds 	mp->m_dirops =
9291da177e4SLinus Torvalds 		XFS_SB_VERSION_HASDIRV2(&mp->m_sb) ?
9301da177e4SLinus Torvalds 			xfsv2_dirops :
9311da177e4SLinus Torvalds 			xfsv1_dirops;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	/*
9341da177e4SLinus Torvalds 	 * Initialize directory manager's entries.
9351da177e4SLinus Torvalds 	 */
9361da177e4SLinus Torvalds 	XFS_DIR_MOUNT(mp);
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 	/*
9391da177e4SLinus Torvalds 	 * Initialize the attribute manager's entries.
9401da177e4SLinus Torvalds 	 */
9411da177e4SLinus Torvalds 	mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	/*
9441da177e4SLinus Torvalds 	 * Initialize the precomputed transaction reservations values.
9451da177e4SLinus Torvalds 	 */
9461da177e4SLinus Torvalds 	xfs_trans_init(mp);
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 	/*
9491da177e4SLinus Torvalds 	 * Allocate and initialize the inode hash table for this
9501da177e4SLinus Torvalds 	 * file system.
9511da177e4SLinus Torvalds 	 */
9521da177e4SLinus Torvalds 	xfs_ihash_init(mp);
9531da177e4SLinus Torvalds 	xfs_chash_init(mp);
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	/*
9561da177e4SLinus Torvalds 	 * Allocate and initialize the per-ag data.
9571da177e4SLinus Torvalds 	 */
9581da177e4SLinus Torvalds 	init_rwsem(&mp->m_peraglock);
9591da177e4SLinus Torvalds 	mp->m_perag =
9601da177e4SLinus Torvalds 		kmem_zalloc(sbp->sb_agcount * sizeof(xfs_perag_t), KM_SLEEP);
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	mp->m_maxagi = xfs_initialize_perag(mp, sbp->sb_agcount);
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	/*
9651da177e4SLinus Torvalds 	 * log's mount-time initialization. Perform 1st part recovery if needed
9661da177e4SLinus Torvalds 	 */
9671da177e4SLinus Torvalds 	if (likely(sbp->sb_logblocks > 0)) {	/* check for volume case */
9681da177e4SLinus Torvalds 		error = xfs_log_mount(mp, mp->m_logdev_targp,
9691da177e4SLinus Torvalds 				      XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
9701da177e4SLinus Torvalds 				      XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
9711da177e4SLinus Torvalds 		if (error) {
9721da177e4SLinus Torvalds 			cmn_err(CE_WARN, "XFS: log mount failed");
9731da177e4SLinus Torvalds 			goto error2;
9741da177e4SLinus Torvalds 		}
9751da177e4SLinus Torvalds 	} else {	/* No log has been defined */
9761da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: no log defined");
9771da177e4SLinus Torvalds 		XFS_ERROR_REPORT("xfs_mountfs_int(1)", XFS_ERRLEVEL_LOW, mp);
9781da177e4SLinus Torvalds 		error = XFS_ERROR(EFSCORRUPTED);
9791da177e4SLinus Torvalds 		goto error2;
9801da177e4SLinus Torvalds 	}
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	/*
9831da177e4SLinus Torvalds 	 * Get and sanity-check the root inode.
9841da177e4SLinus Torvalds 	 * Save the pointer to it in the mount structure.
9851da177e4SLinus Torvalds 	 */
9861da177e4SLinus Torvalds 	error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip, 0);
9871da177e4SLinus Torvalds 	if (error) {
9881da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: failed to read root inode");
9891da177e4SLinus Torvalds 		goto error3;
9901da177e4SLinus Torvalds 	}
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 	ASSERT(rip != NULL);
9931da177e4SLinus Torvalds 	rvp = XFS_ITOV(rip);
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) {
9961da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: corrupted root inode");
9971da177e4SLinus Torvalds 		prdev("Root inode %llu is not a directory",
9981da177e4SLinus Torvalds 		      mp->m_ddev_targp, (unsigned long long)rip->i_ino);
9991da177e4SLinus Torvalds 		xfs_iunlock(rip, XFS_ILOCK_EXCL);
10001da177e4SLinus Torvalds 		XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
10011da177e4SLinus Torvalds 				 mp);
10021da177e4SLinus Torvalds 		error = XFS_ERROR(EFSCORRUPTED);
10031da177e4SLinus Torvalds 		goto error4;
10041da177e4SLinus Torvalds 	}
10051da177e4SLinus Torvalds 	mp->m_rootip = rip;	/* save it */
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	xfs_iunlock(rip, XFS_ILOCK_EXCL);
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 	/*
10101da177e4SLinus Torvalds 	 * Initialize realtime inode pointers in the mount structure
10111da177e4SLinus Torvalds 	 */
10121da177e4SLinus Torvalds 	if ((error = xfs_rtmount_inodes(mp))) {
10131da177e4SLinus Torvalds 		/*
10141da177e4SLinus Torvalds 		 * Free up the root inode.
10151da177e4SLinus Torvalds 		 */
10161da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: failed to read RT inodes");
10171da177e4SLinus Torvalds 		goto error4;
10181da177e4SLinus Torvalds 	}
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds 	/*
10211da177e4SLinus Torvalds 	 * If fs is not mounted readonly, then update the superblock
10221da177e4SLinus Torvalds 	 * unit and width changes.
10231da177e4SLinus Torvalds 	 */
10241da177e4SLinus Torvalds 	if (update_flags && !(vfsp->vfs_flag & VFS_RDONLY))
10251da177e4SLinus Torvalds 		xfs_mount_log_sbunit(mp, update_flags);
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	/*
10281da177e4SLinus Torvalds 	 * Initialise the XFS quota management subsystem for this mount
10291da177e4SLinus Torvalds 	 */
10301da177e4SLinus Torvalds 	if ((error = XFS_QM_INIT(mp, &quotamount, &quotaflags)))
10311da177e4SLinus Torvalds 		goto error4;
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds 	/*
10341da177e4SLinus Torvalds 	 * Finish recovering the file system.  This part needed to be
10351da177e4SLinus Torvalds 	 * delayed until after the root and real-time bitmap inodes
10361da177e4SLinus Torvalds 	 * were consistently read in.
10371da177e4SLinus Torvalds 	 */
10381da177e4SLinus Torvalds 	error = xfs_log_mount_finish(mp, mfsi_flags);
10391da177e4SLinus Torvalds 	if (error) {
10401da177e4SLinus Torvalds 		cmn_err(CE_WARN, "XFS: log mount finish failed");
10411da177e4SLinus Torvalds 		goto error4;
10421da177e4SLinus Torvalds 	}
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	/*
10451da177e4SLinus Torvalds 	 * Complete the quota initialisation, post-log-replay component.
10461da177e4SLinus Torvalds 	 */
10471da177e4SLinus Torvalds 	if ((error = XFS_QM_MOUNT(mp, quotamount, quotaflags, mfsi_flags)))
10481da177e4SLinus Torvalds 		goto error4;
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 	return 0;
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds  error4:
10531da177e4SLinus Torvalds 	/*
10541da177e4SLinus Torvalds 	 * Free up the root inode.
10551da177e4SLinus Torvalds 	 */
10561da177e4SLinus Torvalds 	VN_RELE(rvp);
10571da177e4SLinus Torvalds  error3:
10581da177e4SLinus Torvalds 	xfs_log_unmount_dealloc(mp);
10591da177e4SLinus Torvalds  error2:
10601da177e4SLinus Torvalds 	xfs_ihash_free(mp);
10611da177e4SLinus Torvalds 	xfs_chash_free(mp);
10621da177e4SLinus Torvalds 	for (agno = 0; agno < sbp->sb_agcount; agno++)
10631da177e4SLinus Torvalds 		if (mp->m_perag[agno].pagb_list)
10641da177e4SLinus Torvalds 			kmem_free(mp->m_perag[agno].pagb_list,
10651da177e4SLinus Torvalds 			  sizeof(xfs_perag_busy_t) * XFS_PAGB_NUM_SLOTS);
10661da177e4SLinus Torvalds 	kmem_free(mp->m_perag, sbp->sb_agcount * sizeof(xfs_perag_t));
10671da177e4SLinus Torvalds 	mp->m_perag = NULL;
10681da177e4SLinus Torvalds 	/* FALLTHROUGH */
10691da177e4SLinus Torvalds  error1:
10701da177e4SLinus Torvalds 	if (uuid_mounted)
10711da177e4SLinus Torvalds 		xfs_uuid_unmount(mp);
10721da177e4SLinus Torvalds 	xfs_freesb(mp);
10731da177e4SLinus Torvalds 	return error;
10741da177e4SLinus Torvalds }
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds /*
10771da177e4SLinus Torvalds  * xfs_unmountfs
10781da177e4SLinus Torvalds  *
10791da177e4SLinus Torvalds  * This flushes out the inodes,dquots and the superblock, unmounts the
10801da177e4SLinus Torvalds  * log and makes sure that incore structures are freed.
10811da177e4SLinus Torvalds  */
10821da177e4SLinus Torvalds int
10831da177e4SLinus Torvalds xfs_unmountfs(xfs_mount_t *mp, struct cred *cr)
10841da177e4SLinus Torvalds {
10851da177e4SLinus Torvalds 	struct vfs	*vfsp = XFS_MTOVFS(mp);
10861da177e4SLinus Torvalds #if defined(DEBUG) || defined(INDUCE_IO_ERROR)
10871da177e4SLinus Torvalds 	int64_t		fsid;
10881da177e4SLinus Torvalds #endif
10891da177e4SLinus Torvalds 
1090efa80278SChristoph Hellwig 	xfs_iflush_all(mp);
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 	XFS_QM_DQPURGEALL(mp,
10931da177e4SLinus Torvalds 		XFS_QMOPT_UQUOTA | XFS_QMOPT_GQUOTA | XFS_QMOPT_UMOUNTING);
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	/*
10961da177e4SLinus Torvalds 	 * Flush out the log synchronously so that we know for sure
10971da177e4SLinus Torvalds 	 * that nothing is pinned.  This is important because bflush()
10981da177e4SLinus Torvalds 	 * will skip pinned buffers.
10991da177e4SLinus Torvalds 	 */
11001da177e4SLinus Torvalds 	xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 	xfs_binval(mp->m_ddev_targp);
11031da177e4SLinus Torvalds 	if (mp->m_rtdev_targp) {
11041da177e4SLinus Torvalds 		xfs_binval(mp->m_rtdev_targp);
11051da177e4SLinus Torvalds 	}
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 	xfs_unmountfs_writesb(mp);
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 	xfs_unmountfs_wait(mp); 		/* wait for async bufs */
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 	xfs_log_unmount(mp);			/* Done! No more fs ops. */
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 	xfs_freesb(mp);
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 	/*
11161da177e4SLinus Torvalds 	 * All inodes from this mount point should be freed.
11171da177e4SLinus Torvalds 	 */
11181da177e4SLinus Torvalds 	ASSERT(mp->m_inodes == NULL);
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 	xfs_unmountfs_close(mp, cr);
11211da177e4SLinus Torvalds 	if ((mp->m_flags & XFS_MOUNT_NOUUID) == 0)
11221da177e4SLinus Torvalds 		xfs_uuid_unmount(mp);
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds #if defined(DEBUG) || defined(INDUCE_IO_ERROR)
11251da177e4SLinus Torvalds 	/*
11261da177e4SLinus Torvalds 	 * clear all error tags on this filesystem
11271da177e4SLinus Torvalds 	 */
11281da177e4SLinus Torvalds 	memcpy(&fsid, &vfsp->vfs_fsid, sizeof(int64_t));
11291da177e4SLinus Torvalds 	xfs_errortag_clearall_umount(fsid, mp->m_fsname, 0);
11301da177e4SLinus Torvalds #endif
11311da177e4SLinus Torvalds 	XFS_IODONE(vfsp);
11321da177e4SLinus Torvalds 	xfs_mount_free(mp, 1);
11331da177e4SLinus Torvalds 	return 0;
11341da177e4SLinus Torvalds }
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds void
11371da177e4SLinus Torvalds xfs_unmountfs_close(xfs_mount_t *mp, struct cred *cr)
11381da177e4SLinus Torvalds {
11391da177e4SLinus Torvalds 	if (mp->m_logdev_targp != mp->m_ddev_targp)
11401da177e4SLinus Torvalds 		xfs_free_buftarg(mp->m_logdev_targp, 1);
11411da177e4SLinus Torvalds 	if (mp->m_rtdev_targp)
11421da177e4SLinus Torvalds 		xfs_free_buftarg(mp->m_rtdev_targp, 1);
11431da177e4SLinus Torvalds 	xfs_free_buftarg(mp->m_ddev_targp, 0);
11441da177e4SLinus Torvalds }
11451da177e4SLinus Torvalds 
1146ba0f32d4SChristoph Hellwig STATIC void
11471da177e4SLinus Torvalds xfs_unmountfs_wait(xfs_mount_t *mp)
11481da177e4SLinus Torvalds {
11491da177e4SLinus Torvalds 	if (mp->m_logdev_targp != mp->m_ddev_targp)
11501da177e4SLinus Torvalds 		xfs_wait_buftarg(mp->m_logdev_targp);
11511da177e4SLinus Torvalds 	if (mp->m_rtdev_targp)
11521da177e4SLinus Torvalds 		xfs_wait_buftarg(mp->m_rtdev_targp);
11531da177e4SLinus Torvalds 	xfs_wait_buftarg(mp->m_ddev_targp);
11541da177e4SLinus Torvalds }
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds int
11571da177e4SLinus Torvalds xfs_unmountfs_writesb(xfs_mount_t *mp)
11581da177e4SLinus Torvalds {
11591da177e4SLinus Torvalds 	xfs_buf_t	*sbp;
11601da177e4SLinus Torvalds 	xfs_sb_t	*sb;
11611da177e4SLinus Torvalds 	int		error = 0;
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 	/*
11641da177e4SLinus Torvalds 	 * skip superblock write if fs is read-only, or
11651da177e4SLinus Torvalds 	 * if we are doing a forced umount.
11661da177e4SLinus Torvalds 	 */
11671da177e4SLinus Torvalds 	sbp = xfs_getsb(mp, 0);
11681da177e4SLinus Torvalds 	if (!(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY ||
11691da177e4SLinus Torvalds 		XFS_FORCED_SHUTDOWN(mp))) {
11701da177e4SLinus Torvalds 		/*
11711da177e4SLinus Torvalds 		 * mark shared-readonly if desired
11721da177e4SLinus Torvalds 		 */
11731da177e4SLinus Torvalds 		sb = XFS_BUF_TO_SBP(sbp);
11741da177e4SLinus Torvalds 		if (mp->m_mk_sharedro) {
11751da177e4SLinus Torvalds 			if (!(sb->sb_flags & XFS_SBF_READONLY))
11761da177e4SLinus Torvalds 				sb->sb_flags |= XFS_SBF_READONLY;
11771da177e4SLinus Torvalds 			if (!XFS_SB_VERSION_HASSHARED(sb))
11781da177e4SLinus Torvalds 				XFS_SB_VERSION_ADDSHARED(sb);
11791da177e4SLinus Torvalds 			xfs_fs_cmn_err(CE_NOTE, mp,
11801da177e4SLinus Torvalds 				"Unmounting, marking shared read-only");
11811da177e4SLinus Torvalds 		}
11821da177e4SLinus Torvalds 		XFS_BUF_UNDONE(sbp);
11831da177e4SLinus Torvalds 		XFS_BUF_UNREAD(sbp);
11841da177e4SLinus Torvalds 		XFS_BUF_UNDELAYWRITE(sbp);
11851da177e4SLinus Torvalds 		XFS_BUF_WRITE(sbp);
11861da177e4SLinus Torvalds 		XFS_BUF_UNASYNC(sbp);
11871da177e4SLinus Torvalds 		ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp);
11881da177e4SLinus Torvalds 		xfsbdstrat(mp, sbp);
11891da177e4SLinus Torvalds 		/* Nevermind errors we might get here. */
11901da177e4SLinus Torvalds 		error = xfs_iowait(sbp);
11911da177e4SLinus Torvalds 		if (error)
11921da177e4SLinus Torvalds 			xfs_ioerror_alert("xfs_unmountfs_writesb",
11931da177e4SLinus Torvalds 					  mp, sbp, XFS_BUF_ADDR(sbp));
11941da177e4SLinus Torvalds 		if (error && mp->m_mk_sharedro)
11951da177e4SLinus Torvalds 			xfs_fs_cmn_err(CE_ALERT, mp, "Superblock write error detected while unmounting.  Filesystem may not be marked shared readonly");
11961da177e4SLinus Torvalds 	}
11971da177e4SLinus Torvalds 	xfs_buf_relse(sbp);
11981da177e4SLinus Torvalds 	return (error);
11991da177e4SLinus Torvalds }
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds /*
12021da177e4SLinus Torvalds  * xfs_mod_sb() can be used to copy arbitrary changes to the
12031da177e4SLinus Torvalds  * in-core superblock into the superblock buffer to be logged.
12041da177e4SLinus Torvalds  * It does not provide the higher level of locking that is
12051da177e4SLinus Torvalds  * needed to protect the in-core superblock from concurrent
12061da177e4SLinus Torvalds  * access.
12071da177e4SLinus Torvalds  */
12081da177e4SLinus Torvalds void
12091da177e4SLinus Torvalds xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
12101da177e4SLinus Torvalds {
12111da177e4SLinus Torvalds 	xfs_buf_t	*bp;
12121da177e4SLinus Torvalds 	int		first;
12131da177e4SLinus Torvalds 	int		last;
12141da177e4SLinus Torvalds 	xfs_mount_t	*mp;
12151da177e4SLinus Torvalds 	xfs_sb_t	*sbp;
12161da177e4SLinus Torvalds 	xfs_sb_field_t	f;
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 	ASSERT(fields);
12191da177e4SLinus Torvalds 	if (!fields)
12201da177e4SLinus Torvalds 		return;
12211da177e4SLinus Torvalds 	mp = tp->t_mountp;
12221da177e4SLinus Torvalds 	bp = xfs_trans_getsb(tp, mp, 0);
12231da177e4SLinus Torvalds 	sbp = XFS_BUF_TO_SBP(bp);
12241da177e4SLinus Torvalds 	first = sizeof(xfs_sb_t);
12251da177e4SLinus Torvalds 	last = 0;
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 	/* translate/copy */
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds 	xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), -1, fields);
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 	/* find modified range */
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 	f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
12341da177e4SLinus Torvalds 	ASSERT((1LL << f) & XFS_SB_MOD_BITS);
12351da177e4SLinus Torvalds 	first = xfs_sb_info[f].offset;
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
12381da177e4SLinus Torvalds 	ASSERT((1LL << f) & XFS_SB_MOD_BITS);
12391da177e4SLinus Torvalds 	last = xfs_sb_info[f + 1].offset - 1;
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 	xfs_trans_log_buf(tp, bp, first, last);
12421da177e4SLinus Torvalds }
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds /*
12451da177e4SLinus Torvalds  * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
12461da177e4SLinus Torvalds  * a delta to a specified field in the in-core superblock.  Simply
12471da177e4SLinus Torvalds  * switch on the field indicated and apply the delta to that field.
12481da177e4SLinus Torvalds  * Fields are not allowed to dip below zero, so if the delta would
12491da177e4SLinus Torvalds  * do this do not apply it and return EINVAL.
12501da177e4SLinus Torvalds  *
12511da177e4SLinus Torvalds  * The SB_LOCK must be held when this routine is called.
12521da177e4SLinus Torvalds  */
12531da177e4SLinus Torvalds STATIC int
12541da177e4SLinus Torvalds xfs_mod_incore_sb_unlocked(xfs_mount_t *mp, xfs_sb_field_t field,
12551da177e4SLinus Torvalds 			int delta, int rsvd)
12561da177e4SLinus Torvalds {
12571da177e4SLinus Torvalds 	int		scounter;	/* short counter for 32 bit fields */
12581da177e4SLinus Torvalds 	long long	lcounter;	/* long counter for 64 bit fields */
12591da177e4SLinus Torvalds 	long long	res_used, rem;
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 	/*
12621da177e4SLinus Torvalds 	 * With the in-core superblock spin lock held, switch
12631da177e4SLinus Torvalds 	 * on the indicated field.  Apply the delta to the
12641da177e4SLinus Torvalds 	 * proper field.  If the fields value would dip below
12651da177e4SLinus Torvalds 	 * 0, then do not apply the delta and return EINVAL.
12661da177e4SLinus Torvalds 	 */
12671da177e4SLinus Torvalds 	switch (field) {
12681da177e4SLinus Torvalds 	case XFS_SBS_ICOUNT:
12691da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_icount;
12701da177e4SLinus Torvalds 		lcounter += delta;
12711da177e4SLinus Torvalds 		if (lcounter < 0) {
12721da177e4SLinus Torvalds 			ASSERT(0);
12731da177e4SLinus Torvalds 			return (XFS_ERROR(EINVAL));
12741da177e4SLinus Torvalds 		}
12751da177e4SLinus Torvalds 		mp->m_sb.sb_icount = lcounter;
12761da177e4SLinus Torvalds 		return (0);
12771da177e4SLinus Torvalds 	case XFS_SBS_IFREE:
12781da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_ifree;
12791da177e4SLinus Torvalds 		lcounter += delta;
12801da177e4SLinus Torvalds 		if (lcounter < 0) {
12811da177e4SLinus Torvalds 			ASSERT(0);
12821da177e4SLinus Torvalds 			return (XFS_ERROR(EINVAL));
12831da177e4SLinus Torvalds 		}
12841da177e4SLinus Torvalds 		mp->m_sb.sb_ifree = lcounter;
12851da177e4SLinus Torvalds 		return (0);
12861da177e4SLinus Torvalds 	case XFS_SBS_FDBLOCKS:
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_fdblocks;
12891da177e4SLinus Torvalds 		res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 		if (delta > 0) {		/* Putting blocks back */
12921da177e4SLinus Torvalds 			if (res_used > delta) {
12931da177e4SLinus Torvalds 				mp->m_resblks_avail += delta;
12941da177e4SLinus Torvalds 			} else {
12951da177e4SLinus Torvalds 				rem = delta - res_used;
12961da177e4SLinus Torvalds 				mp->m_resblks_avail = mp->m_resblks;
12971da177e4SLinus Torvalds 				lcounter += rem;
12981da177e4SLinus Torvalds 			}
12991da177e4SLinus Torvalds 		} else {				/* Taking blocks away */
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 			lcounter += delta;
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds 		/*
13041da177e4SLinus Torvalds 		 * If were out of blocks, use any available reserved blocks if
13051da177e4SLinus Torvalds 		 * were allowed to.
13061da177e4SLinus Torvalds 		 */
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 			if (lcounter < 0) {
13091da177e4SLinus Torvalds 				if (rsvd) {
13101da177e4SLinus Torvalds 					lcounter = (long long)mp->m_resblks_avail + delta;
13111da177e4SLinus Torvalds 					if (lcounter < 0) {
13121da177e4SLinus Torvalds 						return (XFS_ERROR(ENOSPC));
13131da177e4SLinus Torvalds 					}
13141da177e4SLinus Torvalds 					mp->m_resblks_avail = lcounter;
13151da177e4SLinus Torvalds 					return (0);
13161da177e4SLinus Torvalds 				} else {	/* not reserved */
13171da177e4SLinus Torvalds 					return (XFS_ERROR(ENOSPC));
13181da177e4SLinus Torvalds 				}
13191da177e4SLinus Torvalds 			}
13201da177e4SLinus Torvalds 		}
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 		mp->m_sb.sb_fdblocks = lcounter;
13231da177e4SLinus Torvalds 		return (0);
13241da177e4SLinus Torvalds 	case XFS_SBS_FREXTENTS:
13251da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_frextents;
13261da177e4SLinus Torvalds 		lcounter += delta;
13271da177e4SLinus Torvalds 		if (lcounter < 0) {
13281da177e4SLinus Torvalds 			return (XFS_ERROR(ENOSPC));
13291da177e4SLinus Torvalds 		}
13301da177e4SLinus Torvalds 		mp->m_sb.sb_frextents = lcounter;
13311da177e4SLinus Torvalds 		return (0);
13321da177e4SLinus Torvalds 	case XFS_SBS_DBLOCKS:
13331da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_dblocks;
13341da177e4SLinus Torvalds 		lcounter += delta;
13351da177e4SLinus Torvalds 		if (lcounter < 0) {
13361da177e4SLinus Torvalds 			ASSERT(0);
13371da177e4SLinus Torvalds 			return (XFS_ERROR(EINVAL));
13381da177e4SLinus Torvalds 		}
13391da177e4SLinus Torvalds 		mp->m_sb.sb_dblocks = lcounter;
13401da177e4SLinus Torvalds 		return (0);
13411da177e4SLinus Torvalds 	case XFS_SBS_AGCOUNT:
13421da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_agcount;
13431da177e4SLinus Torvalds 		scounter += delta;
13441da177e4SLinus Torvalds 		if (scounter < 0) {
13451da177e4SLinus Torvalds 			ASSERT(0);
13461da177e4SLinus Torvalds 			return (XFS_ERROR(EINVAL));
13471da177e4SLinus Torvalds 		}
13481da177e4SLinus Torvalds 		mp->m_sb.sb_agcount = scounter;
13491da177e4SLinus Torvalds 		return (0);
13501da177e4SLinus Torvalds 	case XFS_SBS_IMAX_PCT:
13511da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_imax_pct;
13521da177e4SLinus Torvalds 		scounter += delta;
13531da177e4SLinus Torvalds 		if (scounter < 0) {
13541da177e4SLinus Torvalds 			ASSERT(0);
13551da177e4SLinus Torvalds 			return (XFS_ERROR(EINVAL));
13561da177e4SLinus Torvalds 		}
13571da177e4SLinus Torvalds 		mp->m_sb.sb_imax_pct = scounter;
13581da177e4SLinus Torvalds 		return (0);
13591da177e4SLinus Torvalds 	case XFS_SBS_REXTSIZE:
13601da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rextsize;
13611da177e4SLinus Torvalds 		scounter += delta;
13621da177e4SLinus Torvalds 		if (scounter < 0) {
13631da177e4SLinus Torvalds 			ASSERT(0);
13641da177e4SLinus Torvalds 			return (XFS_ERROR(EINVAL));
13651da177e4SLinus Torvalds 		}
13661da177e4SLinus Torvalds 		mp->m_sb.sb_rextsize = scounter;
13671da177e4SLinus Torvalds 		return (0);
13681da177e4SLinus Torvalds 	case XFS_SBS_RBMBLOCKS:
13691da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rbmblocks;
13701da177e4SLinus Torvalds 		scounter += delta;
13711da177e4SLinus Torvalds 		if (scounter < 0) {
13721da177e4SLinus Torvalds 			ASSERT(0);
13731da177e4SLinus Torvalds 			return (XFS_ERROR(EINVAL));
13741da177e4SLinus Torvalds 		}
13751da177e4SLinus Torvalds 		mp->m_sb.sb_rbmblocks = scounter;
13761da177e4SLinus Torvalds 		return (0);
13771da177e4SLinus Torvalds 	case XFS_SBS_RBLOCKS:
13781da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_rblocks;
13791da177e4SLinus Torvalds 		lcounter += delta;
13801da177e4SLinus Torvalds 		if (lcounter < 0) {
13811da177e4SLinus Torvalds 			ASSERT(0);
13821da177e4SLinus Torvalds 			return (XFS_ERROR(EINVAL));
13831da177e4SLinus Torvalds 		}
13841da177e4SLinus Torvalds 		mp->m_sb.sb_rblocks = lcounter;
13851da177e4SLinus Torvalds 		return (0);
13861da177e4SLinus Torvalds 	case XFS_SBS_REXTENTS:
13871da177e4SLinus Torvalds 		lcounter = (long long)mp->m_sb.sb_rextents;
13881da177e4SLinus Torvalds 		lcounter += delta;
13891da177e4SLinus Torvalds 		if (lcounter < 0) {
13901da177e4SLinus Torvalds 			ASSERT(0);
13911da177e4SLinus Torvalds 			return (XFS_ERROR(EINVAL));
13921da177e4SLinus Torvalds 		}
13931da177e4SLinus Torvalds 		mp->m_sb.sb_rextents = lcounter;
13941da177e4SLinus Torvalds 		return (0);
13951da177e4SLinus Torvalds 	case XFS_SBS_REXTSLOG:
13961da177e4SLinus Torvalds 		scounter = mp->m_sb.sb_rextslog;
13971da177e4SLinus Torvalds 		scounter += delta;
13981da177e4SLinus Torvalds 		if (scounter < 0) {
13991da177e4SLinus Torvalds 			ASSERT(0);
14001da177e4SLinus Torvalds 			return (XFS_ERROR(EINVAL));
14011da177e4SLinus Torvalds 		}
14021da177e4SLinus Torvalds 		mp->m_sb.sb_rextslog = scounter;
14031da177e4SLinus Torvalds 		return (0);
14041da177e4SLinus Torvalds 	default:
14051da177e4SLinus Torvalds 		ASSERT(0);
14061da177e4SLinus Torvalds 		return (XFS_ERROR(EINVAL));
14071da177e4SLinus Torvalds 	}
14081da177e4SLinus Torvalds }
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds /*
14111da177e4SLinus Torvalds  * xfs_mod_incore_sb() is used to change a field in the in-core
14121da177e4SLinus Torvalds  * superblock structure by the specified delta.  This modification
14131da177e4SLinus Torvalds  * is protected by the SB_LOCK.  Just use the xfs_mod_incore_sb_unlocked()
14141da177e4SLinus Torvalds  * routine to do the work.
14151da177e4SLinus Torvalds  */
14161da177e4SLinus Torvalds int
14171da177e4SLinus Torvalds xfs_mod_incore_sb(xfs_mount_t *mp, xfs_sb_field_t field, int delta, int rsvd)
14181da177e4SLinus Torvalds {
14191da177e4SLinus Torvalds 	unsigned long	s;
14201da177e4SLinus Torvalds 	int	status;
14211da177e4SLinus Torvalds 
14221da177e4SLinus Torvalds 	s = XFS_SB_LOCK(mp);
14231da177e4SLinus Torvalds 	status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
14241da177e4SLinus Torvalds 	XFS_SB_UNLOCK(mp, s);
14251da177e4SLinus Torvalds 	return (status);
14261da177e4SLinus Torvalds }
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds /*
14291da177e4SLinus Torvalds  * xfs_mod_incore_sb_batch() is used to change more than one field
14301da177e4SLinus Torvalds  * in the in-core superblock structure at a time.  This modification
14311da177e4SLinus Torvalds  * is protected by a lock internal to this module.  The fields and
14321da177e4SLinus Torvalds  * changes to those fields are specified in the array of xfs_mod_sb
14331da177e4SLinus Torvalds  * structures passed in.
14341da177e4SLinus Torvalds  *
14351da177e4SLinus Torvalds  * Either all of the specified deltas will be applied or none of
14361da177e4SLinus Torvalds  * them will.  If any modified field dips below 0, then all modifications
14371da177e4SLinus Torvalds  * will be backed out and EINVAL will be returned.
14381da177e4SLinus Torvalds  */
14391da177e4SLinus Torvalds int
14401da177e4SLinus Torvalds xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
14411da177e4SLinus Torvalds {
14421da177e4SLinus Torvalds 	unsigned long	s;
14431da177e4SLinus Torvalds 	int		status=0;
14441da177e4SLinus Torvalds 	xfs_mod_sb_t	*msbp;
14451da177e4SLinus Torvalds 
14461da177e4SLinus Torvalds 	/*
14471da177e4SLinus Torvalds 	 * Loop through the array of mod structures and apply each
14481da177e4SLinus Torvalds 	 * individually.  If any fail, then back out all those
14491da177e4SLinus Torvalds 	 * which have already been applied.  Do all of this within
14501da177e4SLinus Torvalds 	 * the scope of the SB_LOCK so that all of the changes will
14511da177e4SLinus Torvalds 	 * be atomic.
14521da177e4SLinus Torvalds 	 */
14531da177e4SLinus Torvalds 	s = XFS_SB_LOCK(mp);
14541da177e4SLinus Torvalds 	msbp = &msb[0];
14551da177e4SLinus Torvalds 	for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) {
14561da177e4SLinus Torvalds 		/*
14571da177e4SLinus Torvalds 		 * Apply the delta at index n.  If it fails, break
14581da177e4SLinus Torvalds 		 * from the loop so we'll fall into the undo loop
14591da177e4SLinus Torvalds 		 * below.
14601da177e4SLinus Torvalds 		 */
14611da177e4SLinus Torvalds 		status = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
14621da177e4SLinus Torvalds 						    msbp->msb_delta, rsvd);
14631da177e4SLinus Torvalds 		if (status != 0) {
14641da177e4SLinus Torvalds 			break;
14651da177e4SLinus Torvalds 		}
14661da177e4SLinus Torvalds 	}
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds 	/*
14691da177e4SLinus Torvalds 	 * If we didn't complete the loop above, then back out
14701da177e4SLinus Torvalds 	 * any changes made to the superblock.  If you add code
14711da177e4SLinus Torvalds 	 * between the loop above and here, make sure that you
14721da177e4SLinus Torvalds 	 * preserve the value of status. Loop back until
14731da177e4SLinus Torvalds 	 * we step below the beginning of the array.  Make sure
14741da177e4SLinus Torvalds 	 * we don't touch anything back there.
14751da177e4SLinus Torvalds 	 */
14761da177e4SLinus Torvalds 	if (status != 0) {
14771da177e4SLinus Torvalds 		msbp--;
14781da177e4SLinus Torvalds 		while (msbp >= msb) {
14791da177e4SLinus Torvalds 			status = xfs_mod_incore_sb_unlocked(mp,
14801da177e4SLinus Torvalds 				    msbp->msb_field, -(msbp->msb_delta), rsvd);
14811da177e4SLinus Torvalds 			ASSERT(status == 0);
14821da177e4SLinus Torvalds 			msbp--;
14831da177e4SLinus Torvalds 		}
14841da177e4SLinus Torvalds 	}
14851da177e4SLinus Torvalds 	XFS_SB_UNLOCK(mp, s);
14861da177e4SLinus Torvalds 	return (status);
14871da177e4SLinus Torvalds }
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds /*
14901da177e4SLinus Torvalds  * xfs_getsb() is called to obtain the buffer for the superblock.
14911da177e4SLinus Torvalds  * The buffer is returned locked and read in from disk.
14921da177e4SLinus Torvalds  * The buffer should be released with a call to xfs_brelse().
14931da177e4SLinus Torvalds  *
14941da177e4SLinus Torvalds  * If the flags parameter is BUF_TRYLOCK, then we'll only return
14951da177e4SLinus Torvalds  * the superblock buffer if it can be locked without sleeping.
14961da177e4SLinus Torvalds  * If it can't then we'll return NULL.
14971da177e4SLinus Torvalds  */
14981da177e4SLinus Torvalds xfs_buf_t *
14991da177e4SLinus Torvalds xfs_getsb(
15001da177e4SLinus Torvalds 	xfs_mount_t	*mp,
15011da177e4SLinus Torvalds 	int		flags)
15021da177e4SLinus Torvalds {
15031da177e4SLinus Torvalds 	xfs_buf_t	*bp;
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds 	ASSERT(mp->m_sb_bp != NULL);
15061da177e4SLinus Torvalds 	bp = mp->m_sb_bp;
15071da177e4SLinus Torvalds 	if (flags & XFS_BUF_TRYLOCK) {
15081da177e4SLinus Torvalds 		if (!XFS_BUF_CPSEMA(bp)) {
15091da177e4SLinus Torvalds 			return NULL;
15101da177e4SLinus Torvalds 		}
15111da177e4SLinus Torvalds 	} else {
15121da177e4SLinus Torvalds 		XFS_BUF_PSEMA(bp, PRIBIO);
15131da177e4SLinus Torvalds 	}
15141da177e4SLinus Torvalds 	XFS_BUF_HOLD(bp);
15151da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISDONE(bp));
15161da177e4SLinus Torvalds 	return (bp);
15171da177e4SLinus Torvalds }
15181da177e4SLinus Torvalds 
15191da177e4SLinus Torvalds /*
15201da177e4SLinus Torvalds  * Used to free the superblock along various error paths.
15211da177e4SLinus Torvalds  */
15221da177e4SLinus Torvalds void
15231da177e4SLinus Torvalds xfs_freesb(
15241da177e4SLinus Torvalds 	xfs_mount_t	*mp)
15251da177e4SLinus Torvalds {
15261da177e4SLinus Torvalds 	xfs_buf_t	*bp;
15271da177e4SLinus Torvalds 
15281da177e4SLinus Torvalds 	/*
15291da177e4SLinus Torvalds 	 * Use xfs_getsb() so that the buffer will be locked
15301da177e4SLinus Torvalds 	 * when we call xfs_buf_relse().
15311da177e4SLinus Torvalds 	 */
15321da177e4SLinus Torvalds 	bp = xfs_getsb(mp, 0);
15331da177e4SLinus Torvalds 	XFS_BUF_UNMANAGE(bp);
15341da177e4SLinus Torvalds 	xfs_buf_relse(bp);
15351da177e4SLinus Torvalds 	mp->m_sb_bp = NULL;
15361da177e4SLinus Torvalds }
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds /*
15391da177e4SLinus Torvalds  * See if the UUID is unique among mounted XFS filesystems.
15401da177e4SLinus Torvalds  * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
15411da177e4SLinus Torvalds  */
15421da177e4SLinus Torvalds STATIC int
15431da177e4SLinus Torvalds xfs_uuid_mount(
15441da177e4SLinus Torvalds 	xfs_mount_t	*mp)
15451da177e4SLinus Torvalds {
15461da177e4SLinus Torvalds 	if (uuid_is_nil(&mp->m_sb.sb_uuid)) {
15471da177e4SLinus Torvalds 		cmn_err(CE_WARN,
15481da177e4SLinus Torvalds 			"XFS: Filesystem %s has nil UUID - can't mount",
15491da177e4SLinus Torvalds 			mp->m_fsname);
15501da177e4SLinus Torvalds 		return -1;
15511da177e4SLinus Torvalds 	}
15521da177e4SLinus Torvalds 	if (!uuid_table_insert(&mp->m_sb.sb_uuid)) {
15531da177e4SLinus Torvalds 		cmn_err(CE_WARN,
15541da177e4SLinus Torvalds 			"XFS: Filesystem %s has duplicate UUID - can't mount",
15551da177e4SLinus Torvalds 			mp->m_fsname);
15561da177e4SLinus Torvalds 		return -1;
15571da177e4SLinus Torvalds 	}
15581da177e4SLinus Torvalds 	return 0;
15591da177e4SLinus Torvalds }
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds /*
15621da177e4SLinus Torvalds  * Remove filesystem from the UUID table.
15631da177e4SLinus Torvalds  */
15641da177e4SLinus Torvalds STATIC void
15651da177e4SLinus Torvalds xfs_uuid_unmount(
15661da177e4SLinus Torvalds 	xfs_mount_t	*mp)
15671da177e4SLinus Torvalds {
15681da177e4SLinus Torvalds 	uuid_table_remove(&mp->m_sb.sb_uuid);
15691da177e4SLinus Torvalds }
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds /*
15721da177e4SLinus Torvalds  * Used to log changes to the superblock unit and width fields which could
15731da177e4SLinus Torvalds  * be altered by the mount options. Only the first superblock is updated.
15741da177e4SLinus Torvalds  */
15751da177e4SLinus Torvalds STATIC void
15761da177e4SLinus Torvalds xfs_mount_log_sbunit(
15771da177e4SLinus Torvalds 	xfs_mount_t	*mp,
15781da177e4SLinus Torvalds 	__int64_t	fields)
15791da177e4SLinus Torvalds {
15801da177e4SLinus Torvalds 	xfs_trans_t	*tp;
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 	ASSERT(fields & (XFS_SB_UNIT|XFS_SB_WIDTH|XFS_SB_UUID));
15831da177e4SLinus Torvalds 
15841da177e4SLinus Torvalds 	tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
15851da177e4SLinus Torvalds 	if (xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
15861da177e4SLinus Torvalds 				XFS_DEFAULT_LOG_COUNT)) {
15871da177e4SLinus Torvalds 		xfs_trans_cancel(tp, 0);
15881da177e4SLinus Torvalds 		return;
15891da177e4SLinus Torvalds 	}
15901da177e4SLinus Torvalds 	xfs_mod_sb(tp, fields);
15911da177e4SLinus Torvalds 	xfs_trans_commit(tp, 0, NULL);
15921da177e4SLinus Torvalds }
1593