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