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