xref: /linux/fs/xfs/xfs_mount.c (revision c19b3b05ae440de50fffe2ac2a9b27392a7448e9)
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"
2070a9883cSDave Chinner #include "xfs_shared.h"
21239880efSDave Chinner #include "xfs_format.h"
22239880efSDave Chinner #include "xfs_log_format.h"
23239880efSDave Chinner #include "xfs_trans_resv.h"
24a844f451SNathan Scott #include "xfs_bit.h"
251da177e4SLinus Torvalds #include "xfs_sb.h"
261da177e4SLinus Torvalds #include "xfs_mount.h"
2757062787SDave Chinner #include "xfs_da_format.h"
289a2cc41cSDave Chinner #include "xfs_da_btree.h"
291da177e4SLinus Torvalds #include "xfs_inode.h"
30a4fbe6abSDave Chinner #include "xfs_dir2.h"
31a844f451SNathan Scott #include "xfs_ialloc.h"
321da177e4SLinus Torvalds #include "xfs_alloc.h"
331da177e4SLinus Torvalds #include "xfs_rtalloc.h"
341da177e4SLinus Torvalds #include "xfs_bmap.h"
35a4fbe6abSDave Chinner #include "xfs_trans.h"
36a4fbe6abSDave Chinner #include "xfs_trans_priv.h"
37a4fbe6abSDave Chinner #include "xfs_log.h"
381da177e4SLinus Torvalds #include "xfs_error.h"
391da177e4SLinus Torvalds #include "xfs_quota.h"
401da177e4SLinus Torvalds #include "xfs_fsops.h"
410b1b213fSChristoph Hellwig #include "xfs_trace.h"
426d8b79cfSDave Chinner #include "xfs_icache.h"
43a31b1d3dSBrian Foster #include "xfs_sysfs.h"
440b1b213fSChristoph Hellwig 
451da177e4SLinus Torvalds 
4627174203SChristoph Hellwig static DEFINE_MUTEX(xfs_uuid_table_mutex);
4727174203SChristoph Hellwig static int xfs_uuid_table_size;
4827174203SChristoph Hellwig static uuid_t *xfs_uuid_table;
4927174203SChristoph Hellwig 
50af3b6382SDarrick J. Wong void
51af3b6382SDarrick J. Wong xfs_uuid_table_free(void)
52af3b6382SDarrick J. Wong {
53af3b6382SDarrick J. Wong 	if (xfs_uuid_table_size == 0)
54af3b6382SDarrick J. Wong 		return;
55af3b6382SDarrick J. Wong 	kmem_free(xfs_uuid_table);
56af3b6382SDarrick J. Wong 	xfs_uuid_table = NULL;
57af3b6382SDarrick J. Wong 	xfs_uuid_table_size = 0;
58af3b6382SDarrick J. Wong }
59af3b6382SDarrick J. Wong 
6027174203SChristoph Hellwig /*
6127174203SChristoph Hellwig  * See if the UUID is unique among mounted XFS filesystems.
6227174203SChristoph Hellwig  * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
6327174203SChristoph Hellwig  */
6427174203SChristoph Hellwig STATIC int
6527174203SChristoph Hellwig xfs_uuid_mount(
6627174203SChristoph Hellwig 	struct xfs_mount	*mp)
6727174203SChristoph Hellwig {
6827174203SChristoph Hellwig 	uuid_t			*uuid = &mp->m_sb.sb_uuid;
6927174203SChristoph Hellwig 	int			hole, i;
7027174203SChristoph Hellwig 
7127174203SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_NOUUID)
7227174203SChristoph Hellwig 		return 0;
7327174203SChristoph Hellwig 
7427174203SChristoph Hellwig 	if (uuid_is_nil(uuid)) {
750b932cccSDave Chinner 		xfs_warn(mp, "Filesystem has nil UUID - can't mount");
762451337dSDave Chinner 		return -EINVAL;
7727174203SChristoph Hellwig 	}
7827174203SChristoph Hellwig 
7927174203SChristoph Hellwig 	mutex_lock(&xfs_uuid_table_mutex);
8027174203SChristoph Hellwig 	for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) {
8127174203SChristoph Hellwig 		if (uuid_is_nil(&xfs_uuid_table[i])) {
8227174203SChristoph Hellwig 			hole = i;
8327174203SChristoph Hellwig 			continue;
8427174203SChristoph Hellwig 		}
8527174203SChristoph Hellwig 		if (uuid_equal(uuid, &xfs_uuid_table[i]))
8627174203SChristoph Hellwig 			goto out_duplicate;
8727174203SChristoph Hellwig 	}
8827174203SChristoph Hellwig 
8927174203SChristoph Hellwig 	if (hole < 0) {
9027174203SChristoph Hellwig 		xfs_uuid_table = kmem_realloc(xfs_uuid_table,
9127174203SChristoph Hellwig 			(xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table),
9227174203SChristoph Hellwig 			xfs_uuid_table_size  * sizeof(*xfs_uuid_table),
9327174203SChristoph Hellwig 			KM_SLEEP);
9427174203SChristoph Hellwig 		hole = xfs_uuid_table_size++;
9527174203SChristoph Hellwig 	}
9627174203SChristoph Hellwig 	xfs_uuid_table[hole] = *uuid;
9727174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
9827174203SChristoph Hellwig 
9927174203SChristoph Hellwig 	return 0;
10027174203SChristoph Hellwig 
10127174203SChristoph Hellwig  out_duplicate:
10227174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
103021000e5SMitsuo Hayasaka 	xfs_warn(mp, "Filesystem has duplicate UUID %pU - can't mount", uuid);
1042451337dSDave Chinner 	return -EINVAL;
10527174203SChristoph Hellwig }
10627174203SChristoph Hellwig 
10727174203SChristoph Hellwig STATIC void
10827174203SChristoph Hellwig xfs_uuid_unmount(
10927174203SChristoph Hellwig 	struct xfs_mount	*mp)
11027174203SChristoph Hellwig {
11127174203SChristoph Hellwig 	uuid_t			*uuid = &mp->m_sb.sb_uuid;
11227174203SChristoph Hellwig 	int			i;
11327174203SChristoph Hellwig 
11427174203SChristoph Hellwig 	if (mp->m_flags & XFS_MOUNT_NOUUID)
11527174203SChristoph Hellwig 		return;
11627174203SChristoph Hellwig 
11727174203SChristoph Hellwig 	mutex_lock(&xfs_uuid_table_mutex);
11827174203SChristoph Hellwig 	for (i = 0; i < xfs_uuid_table_size; i++) {
11927174203SChristoph Hellwig 		if (uuid_is_nil(&xfs_uuid_table[i]))
12027174203SChristoph Hellwig 			continue;
12127174203SChristoph Hellwig 		if (!uuid_equal(uuid, &xfs_uuid_table[i]))
12227174203SChristoph Hellwig 			continue;
12327174203SChristoph Hellwig 		memset(&xfs_uuid_table[i], 0, sizeof(uuid_t));
12427174203SChristoph Hellwig 		break;
12527174203SChristoph Hellwig 	}
12627174203SChristoph Hellwig 	ASSERT(i < xfs_uuid_table_size);
12727174203SChristoph Hellwig 	mutex_unlock(&xfs_uuid_table_mutex);
12827174203SChristoph Hellwig }
12927174203SChristoph Hellwig 
13027174203SChristoph Hellwig 
131e176579eSDave Chinner STATIC void
132e176579eSDave Chinner __xfs_free_perag(
133e176579eSDave Chinner 	struct rcu_head	*head)
134e176579eSDave Chinner {
135e176579eSDave Chinner 	struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head);
136e176579eSDave Chinner 
137e176579eSDave Chinner 	ASSERT(atomic_read(&pag->pag_ref) == 0);
138e176579eSDave Chinner 	kmem_free(pag);
139e176579eSDave Chinner }
140e176579eSDave Chinner 
1410fa800fbSDave Chinner /*
142e176579eSDave Chinner  * Free up the per-ag resources associated with the mount structure.
1431da177e4SLinus Torvalds  */
144c962fb79SChristoph Hellwig STATIC void
145ff4f038cSChristoph Hellwig xfs_free_perag(
146745f6919SChristoph Hellwig 	xfs_mount_t	*mp)
1471da177e4SLinus Torvalds {
1481c1c6ebcSDave Chinner 	xfs_agnumber_t	agno;
1491c1c6ebcSDave Chinner 	struct xfs_perag *pag;
1501da177e4SLinus Torvalds 
1511c1c6ebcSDave Chinner 	for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
1521c1c6ebcSDave Chinner 		spin_lock(&mp->m_perag_lock);
1531c1c6ebcSDave Chinner 		pag = radix_tree_delete(&mp->m_perag_tree, agno);
1541c1c6ebcSDave Chinner 		spin_unlock(&mp->m_perag_lock);
155e176579eSDave Chinner 		ASSERT(pag);
156f83282a8SDave Chinner 		ASSERT(atomic_read(&pag->pag_ref) == 0);
157e176579eSDave Chinner 		call_rcu(&pag->rcu_head, __xfs_free_perag);
1581da177e4SLinus Torvalds 	}
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
1614cc929eeSNathan Scott /*
1624cc929eeSNathan Scott  * Check size of device based on the (data/realtime) block count.
1634cc929eeSNathan Scott  * Note: this check is used by the growfs code as well as mount.
1644cc929eeSNathan Scott  */
1654cc929eeSNathan Scott int
1664cc929eeSNathan Scott xfs_sb_validate_fsb_count(
1674cc929eeSNathan Scott 	xfs_sb_t	*sbp,
1684cc929eeSNathan Scott 	__uint64_t	nblocks)
1694cc929eeSNathan Scott {
1704cc929eeSNathan Scott 	ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
1714cc929eeSNathan Scott 	ASSERT(sbp->sb_blocklog >= BBSHIFT);
1724cc929eeSNathan Scott 
173d5cf09baSChristoph Hellwig 	/* Limited by ULONG_MAX of page cache index */
1744cc929eeSNathan Scott 	if (nblocks >> (PAGE_CACHE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
1752451337dSDave Chinner 		return -EFBIG;
1764cc929eeSNathan Scott 	return 0;
1774cc929eeSNathan Scott }
1781da177e4SLinus Torvalds 
1791c1c6ebcSDave Chinner int
180c11e2c36SNathan Scott xfs_initialize_perag(
181c11e2c36SNathan Scott 	xfs_mount_t	*mp,
1821c1c6ebcSDave Chinner 	xfs_agnumber_t	agcount,
1831c1c6ebcSDave Chinner 	xfs_agnumber_t	*maxagi)
1841da177e4SLinus Torvalds {
1852d2194f6SCarlos Maiolino 	xfs_agnumber_t	index;
1868b26c582SDave Chinner 	xfs_agnumber_t	first_initialised = 0;
1871da177e4SLinus Torvalds 	xfs_perag_t	*pag;
1881da177e4SLinus Torvalds 	xfs_agino_t	agino;
1891da177e4SLinus Torvalds 	xfs_ino_t	ino;
1901da177e4SLinus Torvalds 	xfs_sb_t	*sbp = &mp->m_sb;
1918b26c582SDave Chinner 	int		error = -ENOMEM;
1921da177e4SLinus Torvalds 
1931c1c6ebcSDave Chinner 	/*
1941c1c6ebcSDave Chinner 	 * Walk the current per-ag tree so we don't try to initialise AGs
1951c1c6ebcSDave Chinner 	 * that already exist (growfs case). Allocate and insert all the
1961c1c6ebcSDave Chinner 	 * AGs we don't find ready for initialisation.
1971c1c6ebcSDave Chinner 	 */
1981c1c6ebcSDave Chinner 	for (index = 0; index < agcount; index++) {
1991c1c6ebcSDave Chinner 		pag = xfs_perag_get(mp, index);
2001c1c6ebcSDave Chinner 		if (pag) {
2011c1c6ebcSDave Chinner 			xfs_perag_put(pag);
2021c1c6ebcSDave Chinner 			continue;
2031c1c6ebcSDave Chinner 		}
2048b26c582SDave Chinner 		if (!first_initialised)
2058b26c582SDave Chinner 			first_initialised = index;
206fb3b504aSChristoph Hellwig 
2071c1c6ebcSDave Chinner 		pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
2081c1c6ebcSDave Chinner 		if (!pag)
2098b26c582SDave Chinner 			goto out_unwind;
210fb3b504aSChristoph Hellwig 		pag->pag_agno = index;
211fb3b504aSChristoph Hellwig 		pag->pag_mount = mp;
2121a427ab0SDave Chinner 		spin_lock_init(&pag->pag_ici_lock);
21369b491c2SDave Chinner 		mutex_init(&pag->pag_ici_reclaim_lock);
214fb3b504aSChristoph Hellwig 		INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
21574f75a0cSDave Chinner 		spin_lock_init(&pag->pag_buf_lock);
21674f75a0cSDave Chinner 		pag->pag_buf_tree = RB_ROOT;
217fb3b504aSChristoph Hellwig 
2181c1c6ebcSDave Chinner 		if (radix_tree_preload(GFP_NOFS))
2198b26c582SDave Chinner 			goto out_unwind;
220fb3b504aSChristoph Hellwig 
2211c1c6ebcSDave Chinner 		spin_lock(&mp->m_perag_lock);
2221c1c6ebcSDave Chinner 		if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
2231c1c6ebcSDave Chinner 			BUG();
2241c1c6ebcSDave Chinner 			spin_unlock(&mp->m_perag_lock);
2258b26c582SDave Chinner 			radix_tree_preload_end();
2268b26c582SDave Chinner 			error = -EEXIST;
2278b26c582SDave Chinner 			goto out_unwind;
2281c1c6ebcSDave Chinner 		}
2291c1c6ebcSDave Chinner 		spin_unlock(&mp->m_perag_lock);
2301c1c6ebcSDave Chinner 		radix_tree_preload_end();
2311c1c6ebcSDave Chinner 	}
2321c1c6ebcSDave Chinner 
233fb3b504aSChristoph Hellwig 	/*
234fb3b504aSChristoph Hellwig 	 * If we mount with the inode64 option, or no inode overflows
235fb3b504aSChristoph Hellwig 	 * the legacy 32-bit address space clear the inode32 option.
2361da177e4SLinus Torvalds 	 */
237fb3b504aSChristoph Hellwig 	agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
238fb3b504aSChristoph Hellwig 	ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
2391da177e4SLinus Torvalds 
240fb3b504aSChristoph Hellwig 	if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > XFS_MAXINUMBER_32)
241fb3b504aSChristoph Hellwig 		mp->m_flags |= XFS_MOUNT_32BITINODES;
242fb3b504aSChristoph Hellwig 	else
243fb3b504aSChristoph Hellwig 		mp->m_flags &= ~XFS_MOUNT_32BITINODES;
244fb3b504aSChristoph Hellwig 
2452d2194f6SCarlos Maiolino 	if (mp->m_flags & XFS_MOUNT_32BITINODES)
2469de67c3bSEric Sandeen 		index = xfs_set_inode32(mp, agcount);
2472d2194f6SCarlos Maiolino 	else
2489de67c3bSEric Sandeen 		index = xfs_set_inode64(mp, agcount);
249fb3b504aSChristoph Hellwig 
2501c1c6ebcSDave Chinner 	if (maxagi)
2511c1c6ebcSDave Chinner 		*maxagi = index;
2521c1c6ebcSDave Chinner 	return 0;
2538b26c582SDave Chinner 
2548b26c582SDave Chinner out_unwind:
2558b26c582SDave Chinner 	kmem_free(pag);
2568b26c582SDave Chinner 	for (; index > first_initialised; index--) {
2578b26c582SDave Chinner 		pag = radix_tree_delete(&mp->m_perag_tree, index);
2588b26c582SDave Chinner 		kmem_free(pag);
2598b26c582SDave Chinner 	}
2608b26c582SDave Chinner 	return error;
2611da177e4SLinus Torvalds }
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds /*
2641da177e4SLinus Torvalds  * xfs_readsb
2651da177e4SLinus Torvalds  *
2661da177e4SLinus Torvalds  * Does the initial read of the superblock.
2671da177e4SLinus Torvalds  */
2681da177e4SLinus Torvalds int
269ff55068cSDave Chinner xfs_readsb(
270ff55068cSDave Chinner 	struct xfs_mount *mp,
271ff55068cSDave Chinner 	int		flags)
2721da177e4SLinus Torvalds {
2731da177e4SLinus Torvalds 	unsigned int	sector_size;
27404a1e6c5SDave Chinner 	struct xfs_buf	*bp;
27504a1e6c5SDave Chinner 	struct xfs_sb	*sbp = &mp->m_sb;
2761da177e4SLinus Torvalds 	int		error;
277af34e09dSDave Chinner 	int		loud = !(flags & XFS_MFSI_QUIET);
278daba5427SEric Sandeen 	const struct xfs_buf_ops *buf_ops;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	ASSERT(mp->m_sb_bp == NULL);
2811da177e4SLinus Torvalds 	ASSERT(mp->m_ddev_targp != NULL);
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	/*
284daba5427SEric Sandeen 	 * For the initial read, we must guess at the sector
285daba5427SEric Sandeen 	 * size based on the block device.  It's enough to
286daba5427SEric Sandeen 	 * get the sb_sectsize out of the superblock and
287daba5427SEric Sandeen 	 * then reread with the proper length.
288daba5427SEric Sandeen 	 * We don't verify it yet, because it may not be complete.
289daba5427SEric Sandeen 	 */
290daba5427SEric Sandeen 	sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
291daba5427SEric Sandeen 	buf_ops = NULL;
292daba5427SEric Sandeen 
293daba5427SEric Sandeen 	/*
2941da177e4SLinus Torvalds 	 * Allocate a (locked) buffer to hold the superblock.
2951da177e4SLinus Torvalds 	 * This will be kept around at all times to optimize
2961da177e4SLinus Torvalds 	 * access to the superblock.
2971da177e4SLinus Torvalds 	 */
29826af6552SDave Chinner reread:
299ba372674SDave Chinner 	error = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR,
300ba372674SDave Chinner 				   BTOBB(sector_size), 0, &bp, buf_ops);
301ba372674SDave Chinner 	if (error) {
302eab4e633SDave Chinner 		if (loud)
303e721f504SDave Chinner 			xfs_warn(mp, "SB validate failed with error %d.", error);
304ac75a1f7SDave Chinner 		/* bad CRC means corrupted metadata */
3052451337dSDave Chinner 		if (error == -EFSBADCRC)
3062451337dSDave Chinner 			error = -EFSCORRUPTED;
307ba372674SDave Chinner 		return error;
308eab4e633SDave Chinner 	}
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	/*
3111da177e4SLinus Torvalds 	 * Initialize the mount structure from the superblock.
3121da177e4SLinus Torvalds 	 */
313556b8883SDave Chinner 	xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(bp));
314556b8883SDave Chinner 
315556b8883SDave Chinner 	/*
316556b8883SDave Chinner 	 * If we haven't validated the superblock, do so now before we try
317556b8883SDave Chinner 	 * to check the sector size and reread the superblock appropriately.
318556b8883SDave Chinner 	 */
319556b8883SDave Chinner 	if (sbp->sb_magicnum != XFS_SB_MAGIC) {
320556b8883SDave Chinner 		if (loud)
321556b8883SDave Chinner 			xfs_warn(mp, "Invalid superblock magic number");
3222451337dSDave Chinner 		error = -EINVAL;
323556b8883SDave Chinner 		goto release_buf;
324556b8883SDave Chinner 	}
325ff55068cSDave Chinner 
3261da177e4SLinus Torvalds 	/*
3271da177e4SLinus Torvalds 	 * We must be able to do sector-sized and sector-aligned IO.
3281da177e4SLinus Torvalds 	 */
32904a1e6c5SDave Chinner 	if (sector_size > sbp->sb_sectsize) {
330af34e09dSDave Chinner 		if (loud)
331af34e09dSDave Chinner 			xfs_warn(mp, "device supports %u byte sectors (not %u)",
33204a1e6c5SDave Chinner 				sector_size, sbp->sb_sectsize);
3332451337dSDave Chinner 		error = -ENOSYS;
33426af6552SDave Chinner 		goto release_buf;
3351da177e4SLinus Torvalds 	}
3361da177e4SLinus Torvalds 
337556b8883SDave Chinner 	if (buf_ops == NULL) {
3381da177e4SLinus Torvalds 		/*
339daba5427SEric Sandeen 		 * Re-read the superblock so the buffer is correctly sized,
340daba5427SEric Sandeen 		 * and properly verified.
3411da177e4SLinus Torvalds 		 */
3421da177e4SLinus Torvalds 		xfs_buf_relse(bp);
34304a1e6c5SDave Chinner 		sector_size = sbp->sb_sectsize;
344daba5427SEric Sandeen 		buf_ops = loud ? &xfs_sb_buf_ops : &xfs_sb_quiet_buf_ops;
34526af6552SDave Chinner 		goto reread;
3461da177e4SLinus Torvalds 	}
3471da177e4SLinus Torvalds 
3485681ca40SDave Chinner 	xfs_reinit_percpu_counters(mp);
3498d280b98SDavid Chinner 
35004a1e6c5SDave Chinner 	/* no need to be quiet anymore, so reset the buf ops */
35104a1e6c5SDave Chinner 	bp->b_ops = &xfs_sb_buf_ops;
35204a1e6c5SDave Chinner 
3531da177e4SLinus Torvalds 	mp->m_sb_bp = bp;
35426af6552SDave Chinner 	xfs_buf_unlock(bp);
3551da177e4SLinus Torvalds 	return 0;
3561da177e4SLinus Torvalds 
35726af6552SDave Chinner release_buf:
3581da177e4SLinus Torvalds 	xfs_buf_relse(bp);
3591da177e4SLinus Torvalds 	return error;
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds /*
3630771fb45SEric Sandeen  * Update alignment values based on mount options and sb values
3641da177e4SLinus Torvalds  */
3650771fb45SEric Sandeen STATIC int
3667884bc86SChristoph Hellwig xfs_update_alignment(xfs_mount_t *mp)
3671da177e4SLinus Torvalds {
3681da177e4SLinus Torvalds 	xfs_sb_t	*sbp = &(mp->m_sb);
3691da177e4SLinus Torvalds 
3704249023aSChristoph Hellwig 	if (mp->m_dalign) {
3711da177e4SLinus Torvalds 		/*
3721da177e4SLinus Torvalds 		 * If stripe unit and stripe width are not multiples
3731da177e4SLinus Torvalds 		 * of the fs blocksize turn off alignment.
3741da177e4SLinus Torvalds 		 */
3751da177e4SLinus Torvalds 		if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
3761da177e4SLinus Torvalds 		    (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
37739a45d84SJie Liu 			xfs_warn(mp,
37839a45d84SJie Liu 		"alignment check failed: sunit/swidth vs. blocksize(%d)",
37939a45d84SJie Liu 				sbp->sb_blocksize);
3802451337dSDave Chinner 			return -EINVAL;
3811da177e4SLinus Torvalds 		} else {
3821da177e4SLinus Torvalds 			/*
3831da177e4SLinus Torvalds 			 * Convert the stripe unit and width to FSBs.
3841da177e4SLinus Torvalds 			 */
3851da177e4SLinus Torvalds 			mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
3861da177e4SLinus Torvalds 			if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
38753487786SDave Chinner 				xfs_warn(mp,
38839a45d84SJie Liu 			"alignment check failed: sunit/swidth vs. agsize(%d)",
3891da177e4SLinus Torvalds 					 sbp->sb_agblocks);
3902451337dSDave Chinner 				return -EINVAL;
3911da177e4SLinus Torvalds 			} else if (mp->m_dalign) {
3921da177e4SLinus Torvalds 				mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
3931da177e4SLinus Torvalds 			} else {
39439a45d84SJie Liu 				xfs_warn(mp,
39539a45d84SJie Liu 			"alignment check failed: sunit(%d) less than bsize(%d)",
39639a45d84SJie Liu 					 mp->m_dalign, sbp->sb_blocksize);
3972451337dSDave Chinner 				return -EINVAL;
3981da177e4SLinus Torvalds 			}
3991da177e4SLinus Torvalds 		}
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 		/*
4021da177e4SLinus Torvalds 		 * Update superblock with new values
4031da177e4SLinus Torvalds 		 * and log changes
4041da177e4SLinus Torvalds 		 */
40562118709SEric Sandeen 		if (xfs_sb_version_hasdalign(sbp)) {
4061da177e4SLinus Torvalds 			if (sbp->sb_unit != mp->m_dalign) {
4071da177e4SLinus Torvalds 				sbp->sb_unit = mp->m_dalign;
40861e63ecbSDave Chinner 				mp->m_update_sb = true;
4091da177e4SLinus Torvalds 			}
4101da177e4SLinus Torvalds 			if (sbp->sb_width != mp->m_swidth) {
4111da177e4SLinus Torvalds 				sbp->sb_width = mp->m_swidth;
41261e63ecbSDave Chinner 				mp->m_update_sb = true;
4131da177e4SLinus Torvalds 			}
41434d7f603SJie Liu 		} else {
41534d7f603SJie Liu 			xfs_warn(mp,
41634d7f603SJie Liu 	"cannot change alignment: superblock does not support data alignment");
4172451337dSDave Chinner 			return -EINVAL;
4181da177e4SLinus Torvalds 		}
4191da177e4SLinus Torvalds 	} else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
42062118709SEric Sandeen 		    xfs_sb_version_hasdalign(&mp->m_sb)) {
4211da177e4SLinus Torvalds 			mp->m_dalign = sbp->sb_unit;
4221da177e4SLinus Torvalds 			mp->m_swidth = sbp->sb_width;
4231da177e4SLinus Torvalds 	}
4241da177e4SLinus Torvalds 
4250771fb45SEric Sandeen 	return 0;
4260771fb45SEric Sandeen }
4271da177e4SLinus Torvalds 
4280771fb45SEric Sandeen /*
4290771fb45SEric Sandeen  * Set the maximum inode count for this filesystem
4300771fb45SEric Sandeen  */
4310771fb45SEric Sandeen STATIC void
4320771fb45SEric Sandeen xfs_set_maxicount(xfs_mount_t *mp)
4330771fb45SEric Sandeen {
4340771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
4351da177e4SLinus Torvalds 	__uint64_t	icount;
4361da177e4SLinus Torvalds 
4370771fb45SEric Sandeen 	if (sbp->sb_imax_pct) {
4380771fb45SEric Sandeen 		/*
4390771fb45SEric Sandeen 		 * Make sure the maximum inode count is a multiple
4400771fb45SEric Sandeen 		 * of the units we allocate inodes in.
4411da177e4SLinus Torvalds 		 */
4421da177e4SLinus Torvalds 		icount = sbp->sb_dblocks * sbp->sb_imax_pct;
4431da177e4SLinus Torvalds 		do_div(icount, 100);
4441da177e4SLinus Torvalds 		do_div(icount, mp->m_ialloc_blks);
4451da177e4SLinus Torvalds 		mp->m_maxicount = (icount * mp->m_ialloc_blks)  <<
4461da177e4SLinus Torvalds 				   sbp->sb_inopblog;
4470771fb45SEric Sandeen 	} else {
4481da177e4SLinus Torvalds 		mp->m_maxicount = 0;
4491da177e4SLinus Torvalds 	}
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds /*
4531da177e4SLinus Torvalds  * Set the default minimum read and write sizes unless
4541da177e4SLinus Torvalds  * already specified in a mount option.
4551da177e4SLinus Torvalds  * We use smaller I/O sizes when the file system
4561da177e4SLinus Torvalds  * is being used for NFS service (wsync mount option).
4571da177e4SLinus Torvalds  */
4580771fb45SEric Sandeen STATIC void
4590771fb45SEric Sandeen xfs_set_rw_sizes(xfs_mount_t *mp)
4600771fb45SEric Sandeen {
4610771fb45SEric Sandeen 	xfs_sb_t	*sbp = &(mp->m_sb);
4620771fb45SEric Sandeen 	int		readio_log, writeio_log;
4630771fb45SEric Sandeen 
4641da177e4SLinus Torvalds 	if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
4651da177e4SLinus Torvalds 		if (mp->m_flags & XFS_MOUNT_WSYNC) {
4661da177e4SLinus Torvalds 			readio_log = XFS_WSYNC_READIO_LOG;
4671da177e4SLinus Torvalds 			writeio_log = XFS_WSYNC_WRITEIO_LOG;
4681da177e4SLinus Torvalds 		} else {
4691da177e4SLinus Torvalds 			readio_log = XFS_READIO_LOG_LARGE;
4701da177e4SLinus Torvalds 			writeio_log = XFS_WRITEIO_LOG_LARGE;
4711da177e4SLinus Torvalds 		}
4721da177e4SLinus Torvalds 	} else {
4731da177e4SLinus Torvalds 		readio_log = mp->m_readio_log;
4741da177e4SLinus Torvalds 		writeio_log = mp->m_writeio_log;
4751da177e4SLinus Torvalds 	}
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	if (sbp->sb_blocklog > readio_log) {
4781da177e4SLinus Torvalds 		mp->m_readio_log = sbp->sb_blocklog;
4791da177e4SLinus Torvalds 	} else {
4801da177e4SLinus Torvalds 		mp->m_readio_log = readio_log;
4811da177e4SLinus Torvalds 	}
4821da177e4SLinus Torvalds 	mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
4831da177e4SLinus Torvalds 	if (sbp->sb_blocklog > writeio_log) {
4841da177e4SLinus Torvalds 		mp->m_writeio_log = sbp->sb_blocklog;
4851da177e4SLinus Torvalds 	} else {
4861da177e4SLinus Torvalds 		mp->m_writeio_log = writeio_log;
4871da177e4SLinus Torvalds 	}
4881da177e4SLinus Torvalds 	mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
4890771fb45SEric Sandeen }
490425f9dddSEric Sandeen 
4911da177e4SLinus Torvalds /*
492055388a3SDave Chinner  * precalculate the low space thresholds for dynamic speculative preallocation.
493055388a3SDave Chinner  */
494055388a3SDave Chinner void
495055388a3SDave Chinner xfs_set_low_space_thresholds(
496055388a3SDave Chinner 	struct xfs_mount	*mp)
497055388a3SDave Chinner {
498055388a3SDave Chinner 	int i;
499055388a3SDave Chinner 
500055388a3SDave Chinner 	for (i = 0; i < XFS_LOWSP_MAX; i++) {
501055388a3SDave Chinner 		__uint64_t space = mp->m_sb.sb_dblocks;
502055388a3SDave Chinner 
503055388a3SDave Chinner 		do_div(space, 100);
504055388a3SDave Chinner 		mp->m_low_space[i] = space * (i + 1);
505055388a3SDave Chinner 	}
506055388a3SDave Chinner }
507055388a3SDave Chinner 
508055388a3SDave Chinner 
509055388a3SDave Chinner /*
5101da177e4SLinus Torvalds  * Set whether we're using inode alignment.
5111da177e4SLinus Torvalds  */
5120771fb45SEric Sandeen STATIC void
5130771fb45SEric Sandeen xfs_set_inoalignment(xfs_mount_t *mp)
5140771fb45SEric Sandeen {
51562118709SEric Sandeen 	if (xfs_sb_version_hasalign(&mp->m_sb) &&
5161da177e4SLinus Torvalds 	    mp->m_sb.sb_inoalignmt >=
5171da177e4SLinus Torvalds 	    XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
5181da177e4SLinus Torvalds 		mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
5191da177e4SLinus Torvalds 	else
5201da177e4SLinus Torvalds 		mp->m_inoalign_mask = 0;
5211da177e4SLinus Torvalds 	/*
5221da177e4SLinus Torvalds 	 * If we are using stripe alignment, check whether
5231da177e4SLinus Torvalds 	 * the stripe unit is a multiple of the inode alignment
5241da177e4SLinus Torvalds 	 */
5251da177e4SLinus Torvalds 	if (mp->m_dalign && mp->m_inoalign_mask &&
5261da177e4SLinus Torvalds 	    !(mp->m_dalign & mp->m_inoalign_mask))
5271da177e4SLinus Torvalds 		mp->m_sinoalign = mp->m_dalign;
5281da177e4SLinus Torvalds 	else
5291da177e4SLinus Torvalds 		mp->m_sinoalign = 0;
5300771fb45SEric Sandeen }
5310771fb45SEric Sandeen 
5321da177e4SLinus Torvalds /*
5330471f62eSZhi Yong Wu  * Check that the data (and log if separate) is an ok size.
5341da177e4SLinus Torvalds  */
5350771fb45SEric Sandeen STATIC int
536ba372674SDave Chinner xfs_check_sizes(
537ba372674SDave Chinner 	struct xfs_mount *mp)
5380771fb45SEric Sandeen {
539ba372674SDave Chinner 	struct xfs_buf	*bp;
5400771fb45SEric Sandeen 	xfs_daddr_t	d;
541ba372674SDave Chinner 	int		error;
5420771fb45SEric Sandeen 
5431da177e4SLinus Torvalds 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
5441da177e4SLinus Torvalds 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
5450b932cccSDave Chinner 		xfs_warn(mp, "filesystem size mismatch detected");
5462451337dSDave Chinner 		return -EFBIG;
5471da177e4SLinus Torvalds 	}
548ba372674SDave Chinner 	error = xfs_buf_read_uncached(mp->m_ddev_targp,
5491da177e4SLinus Torvalds 					d - XFS_FSS_TO_BB(mp, 1),
550ba372674SDave Chinner 					XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL);
551ba372674SDave Chinner 	if (error) {
5520b932cccSDave Chinner 		xfs_warn(mp, "last sector read failed");
553ba372674SDave Chinner 		return error;
5541da177e4SLinus Torvalds 	}
5551922c949SDave Chinner 	xfs_buf_relse(bp);
5561da177e4SLinus Torvalds 
557ba372674SDave Chinner 	if (mp->m_logdev_targp == mp->m_ddev_targp)
558ba372674SDave Chinner 		return 0;
559ba372674SDave Chinner 
5601da177e4SLinus Torvalds 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
5611da177e4SLinus Torvalds 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
5620b932cccSDave Chinner 		xfs_warn(mp, "log size mismatch detected");
5632451337dSDave Chinner 		return -EFBIG;
5641da177e4SLinus Torvalds 	}
565ba372674SDave Chinner 	error = xfs_buf_read_uncached(mp->m_logdev_targp,
5661da177e4SLinus Torvalds 					d - XFS_FSB_TO_BB(mp, 1),
567ba372674SDave Chinner 					XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
568ba372674SDave Chinner 	if (error) {
5690b932cccSDave Chinner 		xfs_warn(mp, "log device read failed");
570ba372674SDave Chinner 		return error;
5711da177e4SLinus Torvalds 	}
5721922c949SDave Chinner 	xfs_buf_relse(bp);
5730771fb45SEric Sandeen 	return 0;
5740771fb45SEric Sandeen }
5750771fb45SEric Sandeen 
5760771fb45SEric Sandeen /*
5777d095257SChristoph Hellwig  * Clear the quotaflags in memory and in the superblock.
5787d095257SChristoph Hellwig  */
5797d095257SChristoph Hellwig int
5807d095257SChristoph Hellwig xfs_mount_reset_sbqflags(
5817d095257SChristoph Hellwig 	struct xfs_mount	*mp)
5827d095257SChristoph Hellwig {
5837d095257SChristoph Hellwig 	mp->m_qflags = 0;
5847d095257SChristoph Hellwig 
58561e63ecbSDave Chinner 	/* It is OK to look at sb_qflags in the mount path without m_sb_lock. */
5867d095257SChristoph Hellwig 	if (mp->m_sb.sb_qflags == 0)
5877d095257SChristoph Hellwig 		return 0;
5887d095257SChristoph Hellwig 	spin_lock(&mp->m_sb_lock);
5897d095257SChristoph Hellwig 	mp->m_sb.sb_qflags = 0;
5907d095257SChristoph Hellwig 	spin_unlock(&mp->m_sb_lock);
5917d095257SChristoph Hellwig 
59261e63ecbSDave Chinner 	if (!xfs_fs_writable(mp, SB_FREEZE_WRITE))
5937d095257SChristoph Hellwig 		return 0;
5947d095257SChristoph Hellwig 
59561e63ecbSDave Chinner 	return xfs_sync_sb(mp, false);
5967d095257SChristoph Hellwig }
5977d095257SChristoph Hellwig 
598d5db0f97SEric Sandeen __uint64_t
599d5db0f97SEric Sandeen xfs_default_resblks(xfs_mount_t *mp)
600d5db0f97SEric Sandeen {
601d5db0f97SEric Sandeen 	__uint64_t resblks;
602d5db0f97SEric Sandeen 
603d5db0f97SEric Sandeen 	/*
6048babd8a2SDave Chinner 	 * We default to 5% or 8192 fsbs of space reserved, whichever is
6058babd8a2SDave Chinner 	 * smaller.  This is intended to cover concurrent allocation
6068babd8a2SDave Chinner 	 * transactions when we initially hit enospc. These each require a 4
6078babd8a2SDave Chinner 	 * block reservation. Hence by default we cover roughly 2000 concurrent
6088babd8a2SDave Chinner 	 * allocation reservations.
609d5db0f97SEric Sandeen 	 */
610d5db0f97SEric Sandeen 	resblks = mp->m_sb.sb_dblocks;
611d5db0f97SEric Sandeen 	do_div(resblks, 20);
6128babd8a2SDave Chinner 	resblks = min_t(__uint64_t, resblks, 8192);
613d5db0f97SEric Sandeen 	return resblks;
614d5db0f97SEric Sandeen }
615d5db0f97SEric Sandeen 
6167d095257SChristoph Hellwig /*
6170771fb45SEric Sandeen  * This function does the following on an initial mount of a file system:
6180771fb45SEric Sandeen  *	- reads the superblock from disk and init the mount struct
6190771fb45SEric Sandeen  *	- if we're a 32-bit kernel, do a size check on the superblock
6200771fb45SEric Sandeen  *		so we don't mount terabyte filesystems
6210771fb45SEric Sandeen  *	- init mount struct realtime fields
6220771fb45SEric Sandeen  *	- allocate inode hash table for fs
6230771fb45SEric Sandeen  *	- init directory manager
6240771fb45SEric Sandeen  *	- perform recovery and init the log manager
6250771fb45SEric Sandeen  */
6260771fb45SEric Sandeen int
6270771fb45SEric Sandeen xfs_mountfs(
628f0b2efadSBrian Foster 	struct xfs_mount	*mp)
6290771fb45SEric Sandeen {
630f0b2efadSBrian Foster 	struct xfs_sb		*sbp = &(mp->m_sb);
631f0b2efadSBrian Foster 	struct xfs_inode	*rip;
6320771fb45SEric Sandeen 	__uint64_t		resblks;
6337d095257SChristoph Hellwig 	uint			quotamount = 0;
6347d095257SChristoph Hellwig 	uint			quotaflags = 0;
6350771fb45SEric Sandeen 	int			error = 0;
6360771fb45SEric Sandeen 
637ff55068cSDave Chinner 	xfs_sb_mount_common(mp, sbp);
6380771fb45SEric Sandeen 
6390771fb45SEric Sandeen 	/*
640074e427bSDave Chinner 	 * Check for a mismatched features2 values.  Older kernels read & wrote
641074e427bSDave Chinner 	 * into the wrong sb offset for sb_features2 on some platforms due to
642074e427bSDave Chinner 	 * xfs_sb_t not being 64bit size aligned when sb_features2 was added,
643074e427bSDave Chinner 	 * which made older superblock reading/writing routines swap it as a
644074e427bSDave Chinner 	 * 64-bit value.
645ee1c0908SDavid Chinner 	 *
646e6957ea4SEric Sandeen 	 * For backwards compatibility, we make both slots equal.
647e6957ea4SEric Sandeen 	 *
648074e427bSDave Chinner 	 * If we detect a mismatched field, we OR the set bits into the existing
649074e427bSDave Chinner 	 * features2 field in case it has already been modified; we don't want
650074e427bSDave Chinner 	 * to lose any features.  We then update the bad location with the ORed
651074e427bSDave Chinner 	 * value so that older kernels will see any features2 flags. The
652074e427bSDave Chinner 	 * superblock writeback code ensures the new sb_features2 is copied to
653074e427bSDave Chinner 	 * sb_bad_features2 before it is logged or written to disk.
654ee1c0908SDavid Chinner 	 */
655e6957ea4SEric Sandeen 	if (xfs_sb_has_mismatched_features2(sbp)) {
6560b932cccSDave Chinner 		xfs_warn(mp, "correcting sb_features alignment problem");
657ee1c0908SDavid Chinner 		sbp->sb_features2 |= sbp->sb_bad_features2;
65861e63ecbSDave Chinner 		mp->m_update_sb = true;
659e6957ea4SEric Sandeen 
660e6957ea4SEric Sandeen 		/*
661e6957ea4SEric Sandeen 		 * Re-check for ATTR2 in case it was found in bad_features2
662e6957ea4SEric Sandeen 		 * slot.
663e6957ea4SEric Sandeen 		 */
6647c12f296STim Shimmin 		if (xfs_sb_version_hasattr2(&mp->m_sb) &&
6657c12f296STim Shimmin 		   !(mp->m_flags & XFS_MOUNT_NOATTR2))
666e6957ea4SEric Sandeen 			mp->m_flags |= XFS_MOUNT_ATTR2;
6677c12f296STim Shimmin 	}
668e6957ea4SEric Sandeen 
6697c12f296STim Shimmin 	if (xfs_sb_version_hasattr2(&mp->m_sb) &&
6707c12f296STim Shimmin 	   (mp->m_flags & XFS_MOUNT_NOATTR2)) {
6717c12f296STim Shimmin 		xfs_sb_version_removeattr2(&mp->m_sb);
67261e63ecbSDave Chinner 		mp->m_update_sb = true;
6737c12f296STim Shimmin 
6747c12f296STim Shimmin 		/* update sb_versionnum for the clearing of the morebits */
6757c12f296STim Shimmin 		if (!sbp->sb_features2)
67661e63ecbSDave Chinner 			mp->m_update_sb = true;
677ee1c0908SDavid Chinner 	}
678ee1c0908SDavid Chinner 
679263997a6SDave Chinner 	/* always use v2 inodes by default now */
680263997a6SDave Chinner 	if (!(mp->m_sb.sb_versionnum & XFS_SB_VERSION_NLINKBIT)) {
681263997a6SDave Chinner 		mp->m_sb.sb_versionnum |= XFS_SB_VERSION_NLINKBIT;
68261e63ecbSDave Chinner 		mp->m_update_sb = true;
683263997a6SDave Chinner 	}
684263997a6SDave Chinner 
685ee1c0908SDavid Chinner 	/*
6860771fb45SEric Sandeen 	 * Check if sb_agblocks is aligned at stripe boundary
6870771fb45SEric Sandeen 	 * If sb_agblocks is NOT aligned turn off m_dalign since
6880771fb45SEric Sandeen 	 * allocator alignment is within an ag, therefore ag has
6890771fb45SEric Sandeen 	 * to be aligned at stripe boundary.
6900771fb45SEric Sandeen 	 */
6917884bc86SChristoph Hellwig 	error = xfs_update_alignment(mp);
6920771fb45SEric Sandeen 	if (error)
693f9057e3dSChristoph Hellwig 		goto out;
6940771fb45SEric Sandeen 
6950771fb45SEric Sandeen 	xfs_alloc_compute_maxlevels(mp);
6960771fb45SEric Sandeen 	xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
6970771fb45SEric Sandeen 	xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
6980771fb45SEric Sandeen 	xfs_ialloc_compute_maxlevels(mp);
6990771fb45SEric Sandeen 
7000771fb45SEric Sandeen 	xfs_set_maxicount(mp);
7010771fb45SEric Sandeen 
702a31b1d3dSBrian Foster 	error = xfs_sysfs_init(&mp->m_kobj, &xfs_mp_ktype, NULL, mp->m_fsname);
70327174203SChristoph Hellwig 	if (error)
704f9057e3dSChristoph Hellwig 		goto out;
7051da177e4SLinus Torvalds 
706225e4635SBill O'Donnell 	error = xfs_sysfs_init(&mp->m_stats.xs_kobj, &xfs_stats_ktype,
707225e4635SBill O'Donnell 			       &mp->m_kobj, "stats");
708a31b1d3dSBrian Foster 	if (error)
709a31b1d3dSBrian Foster 		goto out_remove_sysfs;
710a31b1d3dSBrian Foster 
711225e4635SBill O'Donnell 	error = xfs_uuid_mount(mp);
712225e4635SBill O'Donnell 	if (error)
713225e4635SBill O'Donnell 		goto out_del_stats;
714225e4635SBill O'Donnell 
7151da177e4SLinus Torvalds 	/*
7160771fb45SEric Sandeen 	 * Set the minimum read and write sizes
7170771fb45SEric Sandeen 	 */
7180771fb45SEric Sandeen 	xfs_set_rw_sizes(mp);
7190771fb45SEric Sandeen 
720055388a3SDave Chinner 	/* set the low space thresholds for dynamic preallocation */
721055388a3SDave Chinner 	xfs_set_low_space_thresholds(mp);
722055388a3SDave Chinner 
7230771fb45SEric Sandeen 	/*
7240771fb45SEric Sandeen 	 * Set the inode cluster size.
7250771fb45SEric Sandeen 	 * This may still be overridden by the file system
7260771fb45SEric Sandeen 	 * block size if it is larger than the chosen cluster size.
7278f80587bSDave Chinner 	 *
7288f80587bSDave Chinner 	 * For v5 filesystems, scale the cluster size with the inode size to
7298f80587bSDave Chinner 	 * keep a constant ratio of inode per cluster buffer, but only if mkfs
7308f80587bSDave Chinner 	 * has set the inode alignment value appropriately for larger cluster
7318f80587bSDave Chinner 	 * sizes.
7320771fb45SEric Sandeen 	 */
7330771fb45SEric Sandeen 	mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
7348f80587bSDave Chinner 	if (xfs_sb_version_hascrc(&mp->m_sb)) {
7358f80587bSDave Chinner 		int	new_size = mp->m_inode_cluster_size;
7368f80587bSDave Chinner 
7378f80587bSDave Chinner 		new_size *= mp->m_sb.sb_inodesize / XFS_DINODE_MIN_SIZE;
7388f80587bSDave Chinner 		if (mp->m_sb.sb_inoalignmt >= XFS_B_TO_FSBT(mp, new_size))
7398f80587bSDave Chinner 			mp->m_inode_cluster_size = new_size;
7408f80587bSDave Chinner 	}
7410771fb45SEric Sandeen 
7420771fb45SEric Sandeen 	/*
743e5376fc1SBrian Foster 	 * If enabled, sparse inode chunk alignment is expected to match the
744e5376fc1SBrian Foster 	 * cluster size. Full inode chunk alignment must match the chunk size,
745e5376fc1SBrian Foster 	 * but that is checked on sb read verification...
746e5376fc1SBrian Foster 	 */
747e5376fc1SBrian Foster 	if (xfs_sb_version_hassparseinodes(&mp->m_sb) &&
748e5376fc1SBrian Foster 	    mp->m_sb.sb_spino_align !=
749e5376fc1SBrian Foster 			XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size)) {
750e5376fc1SBrian Foster 		xfs_warn(mp,
751e5376fc1SBrian Foster 	"Sparse inode block alignment (%u) must match cluster size (%llu).",
752e5376fc1SBrian Foster 			 mp->m_sb.sb_spino_align,
753e5376fc1SBrian Foster 			 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size));
754e5376fc1SBrian Foster 		error = -EINVAL;
755e5376fc1SBrian Foster 		goto out_remove_uuid;
756e5376fc1SBrian Foster 	}
757e5376fc1SBrian Foster 
758e5376fc1SBrian Foster 	/*
7590771fb45SEric Sandeen 	 * Set inode alignment fields
7600771fb45SEric Sandeen 	 */
7610771fb45SEric Sandeen 	xfs_set_inoalignment(mp);
7620771fb45SEric Sandeen 
7630771fb45SEric Sandeen 	/*
764c2bfbc9bSZhi Yong Wu 	 * Check that the data (and log if separate) is an ok size.
7650771fb45SEric Sandeen 	 */
7664249023aSChristoph Hellwig 	error = xfs_check_sizes(mp);
7670771fb45SEric Sandeen 	if (error)
768f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
7690771fb45SEric Sandeen 
7700771fb45SEric Sandeen 	/*
7711da177e4SLinus Torvalds 	 * Initialize realtime fields in the mount structure
7721da177e4SLinus Torvalds 	 */
7730771fb45SEric Sandeen 	error = xfs_rtmount_init(mp);
7740771fb45SEric Sandeen 	if (error) {
7750b932cccSDave Chinner 		xfs_warn(mp, "RT mount failed");
776f9057e3dSChristoph Hellwig 		goto out_remove_uuid;
7771da177e4SLinus Torvalds 	}
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	/*
7801da177e4SLinus Torvalds 	 *  Copies the low order bits of the timestamp and the randomly
7811da177e4SLinus Torvalds 	 *  set "sequence" number out of a UUID.
7821da177e4SLinus Torvalds 	 */
7831da177e4SLinus Torvalds 	uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds 	mp->m_dmevmask = 0;	/* not persistent; set after each mount */
7861da177e4SLinus Torvalds 
7870650b554SDave Chinner 	error = xfs_da_mount(mp);
7880650b554SDave Chinner 	if (error) {
7890650b554SDave Chinner 		xfs_warn(mp, "Failed dir/attr init: %d", error);
7900650b554SDave Chinner 		goto out_remove_uuid;
7910650b554SDave Chinner 	}
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 	/*
7941da177e4SLinus Torvalds 	 * Initialize the precomputed transaction reservations values.
7951da177e4SLinus Torvalds 	 */
7961da177e4SLinus Torvalds 	xfs_trans_init(mp);
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	/*
7991da177e4SLinus Torvalds 	 * Allocate and initialize the per-ag data.
8001da177e4SLinus Torvalds 	 */
8011c1c6ebcSDave Chinner 	spin_lock_init(&mp->m_perag_lock);
8029b98b6f3SDave Chinner 	INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
8031c1c6ebcSDave Chinner 	error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
8041c1c6ebcSDave Chinner 	if (error) {
8050b932cccSDave Chinner 		xfs_warn(mp, "Failed per-ag init: %d", error);
8060650b554SDave Chinner 		goto out_free_dir;
8071c1c6ebcSDave Chinner 	}
8081da177e4SLinus Torvalds 
809f9057e3dSChristoph Hellwig 	if (!sbp->sb_logblocks) {
8100b932cccSDave Chinner 		xfs_warn(mp, "no log defined");
811f9057e3dSChristoph Hellwig 		XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp);
8122451337dSDave Chinner 		error = -EFSCORRUPTED;
813f9057e3dSChristoph Hellwig 		goto out_free_perag;
814f9057e3dSChristoph Hellwig 	}
815f9057e3dSChristoph Hellwig 
8161da177e4SLinus Torvalds 	/*
817f0b2efadSBrian Foster 	 * Log's mount-time initialization. The first part of recovery can place
818f0b2efadSBrian Foster 	 * some items on the AIL, to be handled when recovery is finished or
819f0b2efadSBrian Foster 	 * cancelled.
8201da177e4SLinus Torvalds 	 */
8211da177e4SLinus Torvalds 	error = xfs_log_mount(mp, mp->m_logdev_targp,
8221da177e4SLinus Torvalds 			      XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
8231da177e4SLinus Torvalds 			      XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
8241da177e4SLinus Torvalds 	if (error) {
8250b932cccSDave Chinner 		xfs_warn(mp, "log mount failed");
826d4f3512bSDave Chinner 		goto out_fail_wait;
8271da177e4SLinus Torvalds 	}
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 	/*
83092821e2bSDavid Chinner 	 * Now the log is mounted, we know if it was an unclean shutdown or
83192821e2bSDavid Chinner 	 * not. If it was, with the first phase of recovery has completed, we
83292821e2bSDavid Chinner 	 * have consistent AG blocks on disk. We have not recovered EFIs yet,
83392821e2bSDavid Chinner 	 * but they are recovered transactionally in the second recovery phase
83492821e2bSDavid Chinner 	 * later.
83592821e2bSDavid Chinner 	 *
83692821e2bSDavid Chinner 	 * Hence we can safely re-initialise incore superblock counters from
83792821e2bSDavid Chinner 	 * the per-ag data. These may not be correct if the filesystem was not
83892821e2bSDavid Chinner 	 * cleanly unmounted, so we need to wait for recovery to finish before
83992821e2bSDavid Chinner 	 * doing this.
84092821e2bSDavid Chinner 	 *
84192821e2bSDavid Chinner 	 * If the filesystem was cleanly unmounted, then we can trust the
84292821e2bSDavid Chinner 	 * values in the superblock to be correct and we don't need to do
84392821e2bSDavid Chinner 	 * anything here.
84492821e2bSDavid Chinner 	 *
84592821e2bSDavid Chinner 	 * If we are currently making the filesystem, the initialisation will
84692821e2bSDavid Chinner 	 * fail as the perag data is in an undefined state.
84792821e2bSDavid Chinner 	 */
84892821e2bSDavid Chinner 	if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
84992821e2bSDavid Chinner 	    !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
85092821e2bSDavid Chinner 	     !mp->m_sb.sb_inprogress) {
85192821e2bSDavid Chinner 		error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
852f9057e3dSChristoph Hellwig 		if (error)
8536eee8972Skbuild test robot 			goto out_log_dealloc;
85492821e2bSDavid Chinner 	}
855f9057e3dSChristoph Hellwig 
85692821e2bSDavid Chinner 	/*
8571da177e4SLinus Torvalds 	 * Get and sanity-check the root inode.
8581da177e4SLinus Torvalds 	 * Save the pointer to it in the mount structure.
8591da177e4SLinus Torvalds 	 */
8607b6259e7SDave Chinner 	error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip);
8611da177e4SLinus Torvalds 	if (error) {
8620b932cccSDave Chinner 		xfs_warn(mp, "failed to read root inode");
863f9057e3dSChristoph Hellwig 		goto out_log_dealloc;
8641da177e4SLinus Torvalds 	}
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	ASSERT(rip != NULL);
8671da177e4SLinus Torvalds 
868*c19b3b05SDave Chinner 	if (unlikely(!S_ISDIR(VFS_I(rip)->i_mode))) {
8690b932cccSDave Chinner 		xfs_warn(mp, "corrupted root inode %llu: not a directory",
870b6574520SNathan Scott 			(unsigned long long)rip->i_ino);
8711da177e4SLinus Torvalds 		xfs_iunlock(rip, XFS_ILOCK_EXCL);
8721da177e4SLinus Torvalds 		XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
8731da177e4SLinus Torvalds 				 mp);
8742451337dSDave Chinner 		error = -EFSCORRUPTED;
875f9057e3dSChristoph Hellwig 		goto out_rele_rip;
8761da177e4SLinus Torvalds 	}
8771da177e4SLinus Torvalds 	mp->m_rootip = rip;	/* save it */
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	xfs_iunlock(rip, XFS_ILOCK_EXCL);
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 	/*
8821da177e4SLinus Torvalds 	 * Initialize realtime inode pointers in the mount structure
8831da177e4SLinus Torvalds 	 */
8840771fb45SEric Sandeen 	error = xfs_rtmount_inodes(mp);
8850771fb45SEric Sandeen 	if (error) {
8861da177e4SLinus Torvalds 		/*
8871da177e4SLinus Torvalds 		 * Free up the root inode.
8881da177e4SLinus Torvalds 		 */
8890b932cccSDave Chinner 		xfs_warn(mp, "failed to read RT inodes");
890f9057e3dSChristoph Hellwig 		goto out_rele_rip;
8911da177e4SLinus Torvalds 	}
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 	/*
8947884bc86SChristoph Hellwig 	 * If this is a read-only mount defer the superblock updates until
8957884bc86SChristoph Hellwig 	 * the next remount into writeable mode.  Otherwise we would never
8967884bc86SChristoph Hellwig 	 * perform the update e.g. for the root filesystem.
8971da177e4SLinus Torvalds 	 */
89861e63ecbSDave Chinner 	if (mp->m_update_sb && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
89961e63ecbSDave Chinner 		error = xfs_sync_sb(mp, false);
900e5720eecSDavid Chinner 		if (error) {
9010b932cccSDave Chinner 			xfs_warn(mp, "failed to write sb changes");
902b93b6e43SChristoph Hellwig 			goto out_rtunmount;
903e5720eecSDavid Chinner 		}
904e5720eecSDavid Chinner 	}
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 	/*
9071da177e4SLinus Torvalds 	 * Initialise the XFS quota management subsystem for this mount
9081da177e4SLinus Torvalds 	 */
9097d095257SChristoph Hellwig 	if (XFS_IS_QUOTA_RUNNING(mp)) {
9107d095257SChristoph Hellwig 		error = xfs_qm_newmount(mp, &quotamount, &quotaflags);
9110771fb45SEric Sandeen 		if (error)
912b93b6e43SChristoph Hellwig 			goto out_rtunmount;
9137d095257SChristoph Hellwig 	} else {
9147d095257SChristoph Hellwig 		ASSERT(!XFS_IS_QUOTA_ON(mp));
9157d095257SChristoph Hellwig 
9167d095257SChristoph Hellwig 		/*
9177d095257SChristoph Hellwig 		 * If a file system had quotas running earlier, but decided to
9187d095257SChristoph Hellwig 		 * mount without -o uquota/pquota/gquota options, revoke the
9197d095257SChristoph Hellwig 		 * quotachecked license.
9207d095257SChristoph Hellwig 		 */
9217d095257SChristoph Hellwig 		if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) {
9220b932cccSDave Chinner 			xfs_notice(mp, "resetting quota flags");
9237d095257SChristoph Hellwig 			error = xfs_mount_reset_sbqflags(mp);
9247d095257SChristoph Hellwig 			if (error)
925a70a4fa5SBrian Foster 				goto out_rtunmount;
9267d095257SChristoph Hellwig 		}
9277d095257SChristoph Hellwig 	}
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	/*
930f0b2efadSBrian Foster 	 * Finish recovering the file system.  This part needed to be delayed
931f0b2efadSBrian Foster 	 * until after the root and real-time bitmap inodes were consistently
932f0b2efadSBrian Foster 	 * read in.
9331da177e4SLinus Torvalds 	 */
9344249023aSChristoph Hellwig 	error = xfs_log_mount_finish(mp);
9351da177e4SLinus Torvalds 	if (error) {
9360b932cccSDave Chinner 		xfs_warn(mp, "log mount finish failed");
937b93b6e43SChristoph Hellwig 		goto out_rtunmount;
9381da177e4SLinus Torvalds 	}
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 	/*
9411da177e4SLinus Torvalds 	 * Complete the quota initialisation, post-log-replay component.
9421da177e4SLinus Torvalds 	 */
9437d095257SChristoph Hellwig 	if (quotamount) {
9447d095257SChristoph Hellwig 		ASSERT(mp->m_qflags == 0);
9457d095257SChristoph Hellwig 		mp->m_qflags = quotaflags;
9467d095257SChristoph Hellwig 
9477d095257SChristoph Hellwig 		xfs_qm_mount_quotas(mp);
9487d095257SChristoph Hellwig 	}
9497d095257SChristoph Hellwig 
95084e1e99fSDavid Chinner 	/*
95184e1e99fSDavid Chinner 	 * Now we are mounted, reserve a small amount of unused space for
95284e1e99fSDavid Chinner 	 * privileged transactions. This is needed so that transaction
95384e1e99fSDavid Chinner 	 * space required for critical operations can dip into this pool
95484e1e99fSDavid Chinner 	 * when at ENOSPC. This is needed for operations like create with
95584e1e99fSDavid Chinner 	 * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
95684e1e99fSDavid Chinner 	 * are not allowed to use this reserved space.
9578babd8a2SDave Chinner 	 *
9588babd8a2SDave Chinner 	 * This may drive us straight to ENOSPC on mount, but that implies
9598babd8a2SDave Chinner 	 * we were already there on the last unmount. Warn if this occurs.
96084e1e99fSDavid Chinner 	 */
961d5db0f97SEric Sandeen 	if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
962d5db0f97SEric Sandeen 		resblks = xfs_default_resblks(mp);
963714082bcSDavid Chinner 		error = xfs_reserve_blocks(mp, &resblks, NULL);
964714082bcSDavid Chinner 		if (error)
9650b932cccSDave Chinner 			xfs_warn(mp,
9660b932cccSDave Chinner 	"Unable to allocate reserve blocks. Continuing without reserve pool.");
967d5db0f97SEric Sandeen 	}
96884e1e99fSDavid Chinner 
9691da177e4SLinus Torvalds 	return 0;
9701da177e4SLinus Torvalds 
971b93b6e43SChristoph Hellwig  out_rtunmount:
972b93b6e43SChristoph Hellwig 	xfs_rtunmount_inodes(mp);
973f9057e3dSChristoph Hellwig  out_rele_rip:
97443355099SChristoph Hellwig 	IRELE(rip);
9750ae120f8SBrian Foster 	cancel_delayed_work_sync(&mp->m_reclaim_work);
9760ae120f8SBrian Foster 	xfs_reclaim_inodes(mp, SYNC_WAIT);
977f9057e3dSChristoph Hellwig  out_log_dealloc:
978f0b2efadSBrian Foster 	xfs_log_mount_cancel(mp);
979d4f3512bSDave Chinner  out_fail_wait:
980d4f3512bSDave Chinner 	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
981d4f3512bSDave Chinner 		xfs_wait_buftarg(mp->m_logdev_targp);
982d4f3512bSDave Chinner 	xfs_wait_buftarg(mp->m_ddev_targp);
983f9057e3dSChristoph Hellwig  out_free_perag:
984ff4f038cSChristoph Hellwig 	xfs_free_perag(mp);
9850650b554SDave Chinner  out_free_dir:
9860650b554SDave Chinner 	xfs_da_unmount(mp);
987f9057e3dSChristoph Hellwig  out_remove_uuid:
98827174203SChristoph Hellwig 	xfs_uuid_unmount(mp);
989225e4635SBill O'Donnell  out_del_stats:
990225e4635SBill O'Donnell 	xfs_sysfs_del(&mp->m_stats.xs_kobj);
991a31b1d3dSBrian Foster  out_remove_sysfs:
992a31b1d3dSBrian Foster 	xfs_sysfs_del(&mp->m_kobj);
993f9057e3dSChristoph Hellwig  out:
9941da177e4SLinus Torvalds 	return error;
9951da177e4SLinus Torvalds }
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds /*
9981da177e4SLinus Torvalds  * This flushes out the inodes,dquots and the superblock, unmounts the
9991da177e4SLinus Torvalds  * log and makes sure that incore structures are freed.
10001da177e4SLinus Torvalds  */
100141b5c2e7SChristoph Hellwig void
100241b5c2e7SChristoph Hellwig xfs_unmountfs(
100341b5c2e7SChristoph Hellwig 	struct xfs_mount	*mp)
10041da177e4SLinus Torvalds {
100584e1e99fSDavid Chinner 	__uint64_t		resblks;
100641b5c2e7SChristoph Hellwig 	int			error;
10071da177e4SLinus Torvalds 
1008579b62faSBrian Foster 	cancel_delayed_work_sync(&mp->m_eofblocks_work);
1009579b62faSBrian Foster 
10107d095257SChristoph Hellwig 	xfs_qm_unmount_quotas(mp);
1011b93b6e43SChristoph Hellwig 	xfs_rtunmount_inodes(mp);
101277508ec8SChristoph Hellwig 	IRELE(mp->m_rootip);
101377508ec8SChristoph Hellwig 
1014641c56fbSDavid Chinner 	/*
1015641c56fbSDavid Chinner 	 * We can potentially deadlock here if we have an inode cluster
10169da096fdSMalcolm Parsons 	 * that has been freed has its buffer still pinned in memory because
1017641c56fbSDavid Chinner 	 * the transaction is still sitting in a iclog. The stale inodes
1018641c56fbSDavid Chinner 	 * on that buffer will have their flush locks held until the
1019641c56fbSDavid Chinner 	 * transaction hits the disk and the callbacks run. the inode
1020641c56fbSDavid Chinner 	 * flush takes the flush lock unconditionally and with nothing to
1021641c56fbSDavid Chinner 	 * push out the iclog we will never get that unlocked. hence we
1022641c56fbSDavid Chinner 	 * need to force the log first.
1023641c56fbSDavid Chinner 	 */
1024a14a348bSChristoph Hellwig 	xfs_log_force(mp, XFS_LOG_SYNC);
1025c854363eSDave Chinner 
1026c854363eSDave Chinner 	/*
1027211e4d43SChristoph Hellwig 	 * Flush all pending changes from the AIL.
1028c854363eSDave Chinner 	 */
1029211e4d43SChristoph Hellwig 	xfs_ail_push_all_sync(mp->m_ail);
1030211e4d43SChristoph Hellwig 
1031211e4d43SChristoph Hellwig 	/*
1032211e4d43SChristoph Hellwig 	 * And reclaim all inodes.  At this point there should be no dirty
10337e18530bSDave Chinner 	 * inodes and none should be pinned or locked, but use synchronous
10347e18530bSDave Chinner 	 * reclaim just to be sure. We can stop background inode reclaim
10357e18530bSDave Chinner 	 * here as well if it is still running.
1036211e4d43SChristoph Hellwig 	 */
10377e18530bSDave Chinner 	cancel_delayed_work_sync(&mp->m_reclaim_work);
1038c854363eSDave Chinner 	xfs_reclaim_inodes(mp, SYNC_WAIT);
10391da177e4SLinus Torvalds 
10407d095257SChristoph Hellwig 	xfs_qm_unmount(mp);
1041a357a121SLachlan McIlroy 
10421da177e4SLinus Torvalds 	/*
104384e1e99fSDavid Chinner 	 * Unreserve any blocks we have so that when we unmount we don't account
104484e1e99fSDavid Chinner 	 * the reserved free space as used. This is really only necessary for
104584e1e99fSDavid Chinner 	 * lazy superblock counting because it trusts the incore superblock
10469da096fdSMalcolm Parsons 	 * counters to be absolutely correct on clean unmount.
104784e1e99fSDavid Chinner 	 *
104884e1e99fSDavid Chinner 	 * We don't bother correcting this elsewhere for lazy superblock
104984e1e99fSDavid Chinner 	 * counting because on mount of an unclean filesystem we reconstruct the
105084e1e99fSDavid Chinner 	 * correct counter value and this is irrelevant.
105184e1e99fSDavid Chinner 	 *
105284e1e99fSDavid Chinner 	 * For non-lazy counter filesystems, this doesn't matter at all because
105384e1e99fSDavid Chinner 	 * we only every apply deltas to the superblock and hence the incore
105484e1e99fSDavid Chinner 	 * value does not matter....
105584e1e99fSDavid Chinner 	 */
105684e1e99fSDavid Chinner 	resblks = 0;
1057714082bcSDavid Chinner 	error = xfs_reserve_blocks(mp, &resblks, NULL);
1058714082bcSDavid Chinner 	if (error)
10590b932cccSDave Chinner 		xfs_warn(mp, "Unable to free reserved block pool. "
1060714082bcSDavid Chinner 				"Freespace may not be correct on next mount.");
1061714082bcSDavid Chinner 
1062adab0f67SChandra Seetharaman 	error = xfs_log_sbcount(mp);
1063e5720eecSDavid Chinner 	if (error)
10640b932cccSDave Chinner 		xfs_warn(mp, "Unable to update superblock counters. "
1065e5720eecSDavid Chinner 				"Freespace may not be correct on next mount.");
106687c7bec7SChristoph Hellwig 
1067225e4635SBill O'Donnell 
106821b699c8SChristoph Hellwig 	xfs_log_unmount(mp);
10690650b554SDave Chinner 	xfs_da_unmount(mp);
107027174203SChristoph Hellwig 	xfs_uuid_unmount(mp);
10711da177e4SLinus Torvalds 
10721550d0b0SChristoph Hellwig #if defined(DEBUG)
10730ce4cfd4SChristoph Hellwig 	xfs_errortag_clearall(mp, 0);
10741da177e4SLinus Torvalds #endif
1075ff4f038cSChristoph Hellwig 	xfs_free_perag(mp);
1076a31b1d3dSBrian Foster 
1077225e4635SBill O'Donnell 	xfs_sysfs_del(&mp->m_stats.xs_kobj);
1078a31b1d3dSBrian Foster 	xfs_sysfs_del(&mp->m_kobj);
10791da177e4SLinus Torvalds }
10801da177e4SLinus Torvalds 
108191ee575fSBrian Foster /*
108291ee575fSBrian Foster  * Determine whether modifications can proceed. The caller specifies the minimum
108391ee575fSBrian Foster  * freeze level for which modifications should not be allowed. This allows
108491ee575fSBrian Foster  * certain operations to proceed while the freeze sequence is in progress, if
108591ee575fSBrian Foster  * necessary.
108691ee575fSBrian Foster  */
108791ee575fSBrian Foster bool
108891ee575fSBrian Foster xfs_fs_writable(
108991ee575fSBrian Foster 	struct xfs_mount	*mp,
109091ee575fSBrian Foster 	int			level)
109192821e2bSDavid Chinner {
109291ee575fSBrian Foster 	ASSERT(level > SB_UNFROZEN);
109391ee575fSBrian Foster 	if ((mp->m_super->s_writers.frozen >= level) ||
109491ee575fSBrian Foster 	    XFS_FORCED_SHUTDOWN(mp) || (mp->m_flags & XFS_MOUNT_RDONLY))
109591ee575fSBrian Foster 		return false;
109691ee575fSBrian Foster 
109791ee575fSBrian Foster 	return true;
109892821e2bSDavid Chinner }
109992821e2bSDavid Chinner 
110092821e2bSDavid Chinner /*
1101b2ce3974SAlex Elder  * xfs_log_sbcount
1102b2ce3974SAlex Elder  *
1103adab0f67SChandra Seetharaman  * Sync the superblock counters to disk.
1104b2ce3974SAlex Elder  *
110591ee575fSBrian Foster  * Note this code can be called during the process of freezing, so we use the
110691ee575fSBrian Foster  * transaction allocator that does not block when the transaction subsystem is
110791ee575fSBrian Foster  * in its frozen state.
110892821e2bSDavid Chinner  */
110992821e2bSDavid Chinner int
1110adab0f67SChandra Seetharaman xfs_log_sbcount(xfs_mount_t *mp)
111192821e2bSDavid Chinner {
111291ee575fSBrian Foster 	/* allow this to proceed during the freeze sequence... */
111391ee575fSBrian Foster 	if (!xfs_fs_writable(mp, SB_FREEZE_COMPLETE))
111492821e2bSDavid Chinner 		return 0;
111592821e2bSDavid Chinner 
111692821e2bSDavid Chinner 	/*
111792821e2bSDavid Chinner 	 * we don't need to do this if we are updating the superblock
111892821e2bSDavid Chinner 	 * counters on every modification.
111992821e2bSDavid Chinner 	 */
112092821e2bSDavid Chinner 	if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
112192821e2bSDavid Chinner 		return 0;
112292821e2bSDavid Chinner 
112361e63ecbSDave Chinner 	return xfs_sync_sb(mp, true);
112492821e2bSDavid Chinner }
112592821e2bSDavid Chinner 
11268c1903d3SDave Chinner /*
11278c1903d3SDave Chinner  * Deltas for the inode count are +/-64, hence we use a large batch size
11288c1903d3SDave Chinner  * of 128 so we don't need to take the counter lock on every update.
11298c1903d3SDave Chinner  */
11308c1903d3SDave Chinner #define XFS_ICOUNT_BATCH	128
1131501ab323SDave Chinner int
1132501ab323SDave Chinner xfs_mod_icount(
1133501ab323SDave Chinner 	struct xfs_mount	*mp,
1134501ab323SDave Chinner 	int64_t			delta)
1135501ab323SDave Chinner {
11368c1903d3SDave Chinner 	__percpu_counter_add(&mp->m_icount, delta, XFS_ICOUNT_BATCH);
11378c1903d3SDave Chinner 	if (__percpu_counter_compare(&mp->m_icount, 0, XFS_ICOUNT_BATCH) < 0) {
1138501ab323SDave Chinner 		ASSERT(0);
1139501ab323SDave Chinner 		percpu_counter_add(&mp->m_icount, -delta);
1140501ab323SDave Chinner 		return -EINVAL;
1141501ab323SDave Chinner 	}
1142501ab323SDave Chinner 	return 0;
1143501ab323SDave Chinner }
1144501ab323SDave Chinner 
1145e88b64eaSDave Chinner int
1146e88b64eaSDave Chinner xfs_mod_ifree(
1147e88b64eaSDave Chinner 	struct xfs_mount	*mp,
1148e88b64eaSDave Chinner 	int64_t			delta)
1149e88b64eaSDave Chinner {
1150e88b64eaSDave Chinner 	percpu_counter_add(&mp->m_ifree, delta);
1151e88b64eaSDave Chinner 	if (percpu_counter_compare(&mp->m_ifree, 0) < 0) {
1152e88b64eaSDave Chinner 		ASSERT(0);
1153e88b64eaSDave Chinner 		percpu_counter_add(&mp->m_ifree, -delta);
1154e88b64eaSDave Chinner 		return -EINVAL;
1155e88b64eaSDave Chinner 	}
1156e88b64eaSDave Chinner 	return 0;
1157e88b64eaSDave Chinner }
11580d485adaSDave Chinner 
11598c1903d3SDave Chinner /*
11608c1903d3SDave Chinner  * Deltas for the block count can vary from 1 to very large, but lock contention
11618c1903d3SDave Chinner  * only occurs on frequent small block count updates such as in the delayed
11628c1903d3SDave Chinner  * allocation path for buffered writes (page a time updates). Hence we set
11638c1903d3SDave Chinner  * a large batch count (1024) to minimise global counter updates except when
11648c1903d3SDave Chinner  * we get near to ENOSPC and we have to be very accurate with our updates.
11658c1903d3SDave Chinner  */
11668c1903d3SDave Chinner #define XFS_FDBLOCKS_BATCH	1024
11670d485adaSDave Chinner int
11680d485adaSDave Chinner xfs_mod_fdblocks(
11690d485adaSDave Chinner 	struct xfs_mount	*mp,
11700d485adaSDave Chinner 	int64_t			delta,
11710d485adaSDave Chinner 	bool			rsvd)
11720d485adaSDave Chinner {
11730d485adaSDave Chinner 	int64_t			lcounter;
11740d485adaSDave Chinner 	long long		res_used;
11750d485adaSDave Chinner 	s32			batch;
11760d485adaSDave Chinner 
11770d485adaSDave Chinner 	if (delta > 0) {
11780d485adaSDave Chinner 		/*
11790d485adaSDave Chinner 		 * If the reserve pool is depleted, put blocks back into it
11800d485adaSDave Chinner 		 * first. Most of the time the pool is full.
11810d485adaSDave Chinner 		 */
11820d485adaSDave Chinner 		if (likely(mp->m_resblks == mp->m_resblks_avail)) {
11830d485adaSDave Chinner 			percpu_counter_add(&mp->m_fdblocks, delta);
11840d485adaSDave Chinner 			return 0;
11850d485adaSDave Chinner 		}
11860d485adaSDave Chinner 
11870d485adaSDave Chinner 		spin_lock(&mp->m_sb_lock);
11880d485adaSDave Chinner 		res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
11890d485adaSDave Chinner 
11900d485adaSDave Chinner 		if (res_used > delta) {
11910d485adaSDave Chinner 			mp->m_resblks_avail += delta;
11920d485adaSDave Chinner 		} else {
11930d485adaSDave Chinner 			delta -= res_used;
11940d485adaSDave Chinner 			mp->m_resblks_avail = mp->m_resblks;
11950d485adaSDave Chinner 			percpu_counter_add(&mp->m_fdblocks, delta);
11960d485adaSDave Chinner 		}
11970d485adaSDave Chinner 		spin_unlock(&mp->m_sb_lock);
11980d485adaSDave Chinner 		return 0;
11990d485adaSDave Chinner 	}
12000d485adaSDave Chinner 
12010d485adaSDave Chinner 	/*
12020d485adaSDave Chinner 	 * Taking blocks away, need to be more accurate the closer we
12030d485adaSDave Chinner 	 * are to zero.
12040d485adaSDave Chinner 	 *
12050d485adaSDave Chinner 	 * If the counter has a value of less than 2 * max batch size,
12060d485adaSDave Chinner 	 * then make everything serialise as we are real close to
12070d485adaSDave Chinner 	 * ENOSPC.
12080d485adaSDave Chinner 	 */
12098c1903d3SDave Chinner 	if (__percpu_counter_compare(&mp->m_fdblocks, 2 * XFS_FDBLOCKS_BATCH,
12108c1903d3SDave Chinner 				     XFS_FDBLOCKS_BATCH) < 0)
12110d485adaSDave Chinner 		batch = 1;
12120d485adaSDave Chinner 	else
12138c1903d3SDave Chinner 		batch = XFS_FDBLOCKS_BATCH;
12140d485adaSDave Chinner 
12150d485adaSDave Chinner 	__percpu_counter_add(&mp->m_fdblocks, delta, batch);
12168c1903d3SDave Chinner 	if (__percpu_counter_compare(&mp->m_fdblocks, XFS_ALLOC_SET_ASIDE(mp),
12178c1903d3SDave Chinner 				     XFS_FDBLOCKS_BATCH) >= 0) {
12180d485adaSDave Chinner 		/* we had space! */
12190d485adaSDave Chinner 		return 0;
12200d485adaSDave Chinner 	}
12210d485adaSDave Chinner 
12220d485adaSDave Chinner 	/*
12230d485adaSDave Chinner 	 * lock up the sb for dipping into reserves before releasing the space
12240d485adaSDave Chinner 	 * that took us to ENOSPC.
12250d485adaSDave Chinner 	 */
12260d485adaSDave Chinner 	spin_lock(&mp->m_sb_lock);
12270d485adaSDave Chinner 	percpu_counter_add(&mp->m_fdblocks, -delta);
12280d485adaSDave Chinner 	if (!rsvd)
12290d485adaSDave Chinner 		goto fdblocks_enospc;
12300d485adaSDave Chinner 
12310d485adaSDave Chinner 	lcounter = (long long)mp->m_resblks_avail + delta;
12320d485adaSDave Chinner 	if (lcounter >= 0) {
12330d485adaSDave Chinner 		mp->m_resblks_avail = lcounter;
12340d485adaSDave Chinner 		spin_unlock(&mp->m_sb_lock);
12350d485adaSDave Chinner 		return 0;
12360d485adaSDave Chinner 	}
12370d485adaSDave Chinner 	printk_once(KERN_WARNING
12380d485adaSDave Chinner 		"Filesystem \"%s\": reserve blocks depleted! "
12390d485adaSDave Chinner 		"Consider increasing reserve pool size.",
12400d485adaSDave Chinner 		mp->m_fsname);
12410d485adaSDave Chinner fdblocks_enospc:
12420d485adaSDave Chinner 	spin_unlock(&mp->m_sb_lock);
12430d485adaSDave Chinner 	return -ENOSPC;
12440d485adaSDave Chinner }
12450d485adaSDave Chinner 
1246bab98bbeSDave Chinner int
1247bab98bbeSDave Chinner xfs_mod_frextents(
1248bab98bbeSDave Chinner 	struct xfs_mount	*mp,
1249bab98bbeSDave Chinner 	int64_t			delta)
1250bab98bbeSDave Chinner {
1251bab98bbeSDave Chinner 	int64_t			lcounter;
1252bab98bbeSDave Chinner 	int			ret = 0;
1253bab98bbeSDave Chinner 
1254bab98bbeSDave Chinner 	spin_lock(&mp->m_sb_lock);
1255bab98bbeSDave Chinner 	lcounter = mp->m_sb.sb_frextents + delta;
1256bab98bbeSDave Chinner 	if (lcounter < 0)
1257bab98bbeSDave Chinner 		ret = -ENOSPC;
1258bab98bbeSDave Chinner 	else
1259bab98bbeSDave Chinner 		mp->m_sb.sb_frextents = lcounter;
1260bab98bbeSDave Chinner 	spin_unlock(&mp->m_sb_lock);
1261bab98bbeSDave Chinner 	return ret;
1262bab98bbeSDave Chinner }
1263bab98bbeSDave Chinner 
12641da177e4SLinus Torvalds /*
12651da177e4SLinus Torvalds  * xfs_getsb() is called to obtain the buffer for the superblock.
12661da177e4SLinus Torvalds  * The buffer is returned locked and read in from disk.
12671da177e4SLinus Torvalds  * The buffer should be released with a call to xfs_brelse().
12681da177e4SLinus Torvalds  *
12691da177e4SLinus Torvalds  * If the flags parameter is BUF_TRYLOCK, then we'll only return
12701da177e4SLinus Torvalds  * the superblock buffer if it can be locked without sleeping.
12711da177e4SLinus Torvalds  * If it can't then we'll return NULL.
12721da177e4SLinus Torvalds  */
12730c842ad4SChristoph Hellwig struct xfs_buf *
12741da177e4SLinus Torvalds xfs_getsb(
12750c842ad4SChristoph Hellwig 	struct xfs_mount	*mp,
12761da177e4SLinus Torvalds 	int			flags)
12771da177e4SLinus Torvalds {
12780c842ad4SChristoph Hellwig 	struct xfs_buf		*bp = mp->m_sb_bp;
12791da177e4SLinus Torvalds 
12800c842ad4SChristoph Hellwig 	if (!xfs_buf_trylock(bp)) {
12810c842ad4SChristoph Hellwig 		if (flags & XBF_TRYLOCK)
12821da177e4SLinus Torvalds 			return NULL;
12830c842ad4SChristoph Hellwig 		xfs_buf_lock(bp);
12841da177e4SLinus Torvalds 	}
12850c842ad4SChristoph Hellwig 
128672790aa1SChandra Seetharaman 	xfs_buf_hold(bp);
12871da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISDONE(bp));
1288014c2544SJesper Juhl 	return bp;
12891da177e4SLinus Torvalds }
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds /*
12921da177e4SLinus Torvalds  * Used to free the superblock along various error paths.
12931da177e4SLinus Torvalds  */
12941da177e4SLinus Torvalds void
12951da177e4SLinus Torvalds xfs_freesb(
129626af6552SDave Chinner 	struct xfs_mount	*mp)
12971da177e4SLinus Torvalds {
129826af6552SDave Chinner 	struct xfs_buf		*bp = mp->m_sb_bp;
12991da177e4SLinus Torvalds 
130026af6552SDave Chinner 	xfs_buf_lock(bp);
13011da177e4SLinus Torvalds 	mp->m_sb_bp = NULL;
130226af6552SDave Chinner 	xfs_buf_relse(bp);
13031da177e4SLinus Torvalds }
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds /*
1306dda35b8fSChristoph Hellwig  * If the underlying (data/log/rt) device is readonly, there are some
1307dda35b8fSChristoph Hellwig  * operations that cannot proceed.
1308dda35b8fSChristoph Hellwig  */
1309dda35b8fSChristoph Hellwig int
1310dda35b8fSChristoph Hellwig xfs_dev_is_read_only(
1311dda35b8fSChristoph Hellwig 	struct xfs_mount	*mp,
1312dda35b8fSChristoph Hellwig 	char			*message)
1313dda35b8fSChristoph Hellwig {
1314dda35b8fSChristoph Hellwig 	if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
1315dda35b8fSChristoph Hellwig 	    xfs_readonly_buftarg(mp->m_logdev_targp) ||
1316dda35b8fSChristoph Hellwig 	    (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
13170b932cccSDave Chinner 		xfs_notice(mp, "%s required on read-only device.", message);
13180b932cccSDave Chinner 		xfs_notice(mp, "write access unavailable, cannot proceed.");
13192451337dSDave Chinner 		return -EROFS;
1320dda35b8fSChristoph Hellwig 	}
1321dda35b8fSChristoph Hellwig 	return 0;
1322dda35b8fSChristoph Hellwig }
1323