xref: /openbmc/linux/fs/xfs/xfs_fsops.c (revision 8a456679)
10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
37b718769SNathan Scott  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
47b718769SNathan Scott  * All Rights Reserved.
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds #include "xfs.h"
7a844f451SNathan Scott #include "xfs_fs.h"
870a9883cSDave Chinner #include "xfs_shared.h"
9239880efSDave Chinner #include "xfs_format.h"
10a4fbe6abSDave Chinner #include "xfs_log_format.h"
11239880efSDave Chinner #include "xfs_trans_resv.h"
121da177e4SLinus Torvalds #include "xfs_sb.h"
131da177e4SLinus Torvalds #include "xfs_mount.h"
14239880efSDave Chinner #include "xfs_trans.h"
151da177e4SLinus Torvalds #include "xfs_error.h"
161da177e4SLinus Torvalds #include "xfs_alloc.h"
171da177e4SLinus Torvalds #include "xfs_fsops.h"
181da177e4SLinus Torvalds #include "xfs_trans_space.h"
19239880efSDave Chinner #include "xfs_log.h"
2041e63621SDave Chinner #include "xfs_log_priv.h"
21b16817b6SDave Chinner #include "xfs_ag.h"
2284d69619SDarrick J. Wong #include "xfs_ag_resv.h"
237f89c838SDarrick J. Wong #include "xfs_trace.h"
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds /*
26c789c83cSGao Xiang  * Write new AG headers to disk. Non-transactional, but need to be
27c789c83cSGao Xiang  * written and completed prior to the growfs transaction being logged.
28c789c83cSGao Xiang  * To do this, we use a delayed write buffer list and wait for
29c789c83cSGao Xiang  * submission and IO completion of the list as a whole. This allows the
30c789c83cSGao Xiang  * IO subsystem to merge all the AG headers in a single AG into a single
31c789c83cSGao Xiang  * IO and hide most of the latency of the IO from us.
32c789c83cSGao Xiang  *
33c789c83cSGao Xiang  * This also means that if we get an error whilst building the buffer
34c789c83cSGao Xiang  * list to write, we can cancel the entire list without having written
35c789c83cSGao Xiang  * anything.
36c789c83cSGao Xiang  */
37c789c83cSGao Xiang static int
xfs_resizefs_init_new_ags(struct xfs_trans * tp,struct aghdr_init_data * id,xfs_agnumber_t oagcount,xfs_agnumber_t nagcount,xfs_rfsblock_t delta,struct xfs_perag * last_pag,bool * lastag_extended)38c789c83cSGao Xiang xfs_resizefs_init_new_ags(
39c789c83cSGao Xiang 	struct xfs_trans	*tp,
40c789c83cSGao Xiang 	struct aghdr_init_data	*id,
41c789c83cSGao Xiang 	xfs_agnumber_t		oagcount,
42c789c83cSGao Xiang 	xfs_agnumber_t		nagcount,
43c789c83cSGao Xiang 	xfs_rfsblock_t		delta,
44c6aee248SDave Chinner 	struct xfs_perag	*last_pag,
45c789c83cSGao Xiang 	bool			*lastag_extended)
46c789c83cSGao Xiang {
47c789c83cSGao Xiang 	struct xfs_mount	*mp = tp->t_mountp;
48c789c83cSGao Xiang 	xfs_rfsblock_t		nb = mp->m_sb.sb_dblocks + delta;
49c789c83cSGao Xiang 	int			error;
50c789c83cSGao Xiang 
51c789c83cSGao Xiang 	*lastag_extended = false;
52c789c83cSGao Xiang 
53c789c83cSGao Xiang 	INIT_LIST_HEAD(&id->buffer_list);
54c789c83cSGao Xiang 	for (id->agno = nagcount - 1;
55c789c83cSGao Xiang 	     id->agno >= oagcount;
56c789c83cSGao Xiang 	     id->agno--, delta -= id->agsize) {
57c789c83cSGao Xiang 
58c789c83cSGao Xiang 		if (id->agno == nagcount - 1)
59c789c83cSGao Xiang 			id->agsize = nb - (id->agno *
60c789c83cSGao Xiang 					(xfs_rfsblock_t)mp->m_sb.sb_agblocks);
61c789c83cSGao Xiang 		else
62c789c83cSGao Xiang 			id->agsize = mp->m_sb.sb_agblocks;
63c789c83cSGao Xiang 
64c789c83cSGao Xiang 		error = xfs_ag_init_headers(mp, id);
65c789c83cSGao Xiang 		if (error) {
66c789c83cSGao Xiang 			xfs_buf_delwri_cancel(&id->buffer_list);
67c789c83cSGao Xiang 			return error;
68c789c83cSGao Xiang 		}
69c789c83cSGao Xiang 	}
70c789c83cSGao Xiang 
71c789c83cSGao Xiang 	error = xfs_buf_delwri_submit(&id->buffer_list);
72c789c83cSGao Xiang 	if (error)
73c789c83cSGao Xiang 		return error;
74c789c83cSGao Xiang 
75c789c83cSGao Xiang 	if (delta) {
76c789c83cSGao Xiang 		*lastag_extended = true;
77c6aee248SDave Chinner 		error = xfs_ag_extend_space(last_pag, tp, delta);
78c789c83cSGao Xiang 	}
79c789c83cSGao Xiang 	return error;
80c789c83cSGao Xiang }
81c789c83cSGao Xiang 
82c789c83cSGao Xiang /*
83b16817b6SDave Chinner  * growfs operations
841da177e4SLinus Torvalds  */
851da177e4SLinus Torvalds static int
xfs_growfs_data_private(struct xfs_mount * mp,struct xfs_growfs_data * in)861da177e4SLinus Torvalds xfs_growfs_data_private(
8707aabd9cSGao Xiang 	struct xfs_mount	*mp,		/* mount point for filesystem */
8807aabd9cSGao Xiang 	struct xfs_growfs_data	*in)		/* growfs data input struct */
891da177e4SLinus Torvalds {
90e8222613SDave Chinner 	struct xfs_buf		*bp;
9183a7f86eSDave Chinner 	int			error;
921da177e4SLinus Torvalds 	xfs_agnumber_t		nagcount;
931da177e4SLinus Torvalds 	xfs_agnumber_t		nagimax = 0;
94ce5e1062SGao Xiang 	xfs_rfsblock_t		nb, nb_div, nb_mod;
95fb2fc172SGao Xiang 	int64_t			delta;
96ed04a91fSDarrick J. Wong 	bool			lastag_extended = false;
971da177e4SLinus Torvalds 	xfs_agnumber_t		oagcount;
9807aabd9cSGao Xiang 	struct xfs_trans	*tp;
990410c3bbSDave Chinner 	struct aghdr_init_data	id = {};
100c6aee248SDave Chinner 	struct xfs_perag	*last_pag;
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 	nb = in->newblocks;
103fb2fc172SGao Xiang 	error = xfs_sb_validate_fsb_count(&mp->m_sb, nb);
104fb2fc172SGao Xiang 	if (error)
1054cc929eeSNathan Scott 		return error;
106fb2fc172SGao Xiang 
107fb2fc172SGao Xiang 	if (nb > mp->m_sb.sb_dblocks) {
108ba372674SDave Chinner 		error = xfs_buf_read_uncached(mp->m_ddev_targp,
1091da177e4SLinus Torvalds 				XFS_FSB_TO_BB(mp, nb) - XFS_FSS_TO_BB(mp, 1),
110ba372674SDave Chinner 				XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL);
111ba372674SDave Chinner 		if (error)
112eab4e633SDave Chinner 			return error;
1131da177e4SLinus Torvalds 		xfs_buf_relse(bp);
114fb2fc172SGao Xiang 	}
1151da177e4SLinus Torvalds 
116ce5e1062SGao Xiang 	nb_div = nb;
117ce5e1062SGao Xiang 	nb_mod = do_div(nb_div, mp->m_sb.sb_agblocks);
118c3b880acSLong Li 	if (nb_mod && nb_mod >= XFS_MIN_AG_BLOCKS)
119c3b880acSLong Li 		nb_div++;
120c3b880acSLong Li 	else if (nb_mod)
121c3b880acSLong Li 		nb = nb_div * mp->m_sb.sb_agblocks;
122c3b880acSLong Li 
123c3b880acSLong Li 	if (nb_div > XFS_MAX_AGNUMBER + 1) {
124c3b880acSLong Li 		nb_div = XFS_MAX_AGNUMBER + 1;
125c3b880acSLong Li 		nb = nb_div * mp->m_sb.sb_agblocks;
1261da177e4SLinus Torvalds 	}
127c3b880acSLong Li 	nagcount = nb_div;
128ce5e1062SGao Xiang 	delta = nb - mp->m_sb.sb_dblocks;
129fb2fc172SGao Xiang 	/*
130fb2fc172SGao Xiang 	 * Reject filesystems with a single AG because they are not
131fb2fc172SGao Xiang 	 * supported, and reject a shrink operation that would cause a
132fb2fc172SGao Xiang 	 * filesystem to become unsupported.
133fb2fc172SGao Xiang 	 */
134fb2fc172SGao Xiang 	if (delta < 0 && nagcount < 2)
135fb2fc172SGao Xiang 		return -EINVAL;
136fb2fc172SGao Xiang 
137c4848932SEric Sandeen 	/* No work to do */
138c4848932SEric Sandeen 	if (delta == 0)
139c4848932SEric Sandeen 		return 0;
140c4848932SEric Sandeen 
1411da177e4SLinus Torvalds 	oagcount = mp->m_sb.sb_agcount;
1421c1c6ebcSDave Chinner 	/* allocate the new per-ag structures */
1431da177e4SLinus Torvalds 	if (nagcount > oagcount) {
1440800169eSDave Chinner 		error = xfs_initialize_perag(mp, nagcount, nb, &nagimax);
1451c1c6ebcSDave Chinner 		if (error)
1461c1c6ebcSDave Chinner 			return error;
147fb2fc172SGao Xiang 	} else if (nagcount < oagcount) {
148fb2fc172SGao Xiang 		/* TODO: shrinking the entire AGs hasn't yet completed */
149fb2fc172SGao Xiang 		return -EINVAL;
1501da177e4SLinus Torvalds 	}
1511c1c6ebcSDave Chinner 
15206f3ef6eSDarrick J. Wong 	if (delta > 0)
153253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata,
15406f3ef6eSDarrick J. Wong 				XFS_GROWFS_SPACE_RES(mp), 0, XFS_TRANS_RESERVE,
15506f3ef6eSDarrick J. Wong 				&tp);
15606f3ef6eSDarrick J. Wong 	else
15706f3ef6eSDarrick J. Wong 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata, -delta, 0,
15806f3ef6eSDarrick J. Wong 				0, &tp);
159253f4911SChristoph Hellwig 	if (error)
1608a456679SLong Li 		goto out_free_unused_perag;
1611da177e4SLinus Torvalds 
162c6aee248SDave Chinner 	last_pag = xfs_perag_get(mp, oagcount - 1);
163fb2fc172SGao Xiang 	if (delta > 0) {
164c789c83cSGao Xiang 		error = xfs_resizefs_init_new_ags(tp, &id, oagcount, nagcount,
165c6aee248SDave Chinner 				delta, last_pag, &lastag_extended);
166fb2fc172SGao Xiang 	} else {
167df5660cfSDarrick J. Wong 		xfs_warn_mount(mp, XFS_OPSTATE_WARNED_SHRINK,
168fb2fc172SGao Xiang 	"EXPERIMENTAL online shrink feature in use. Use at your own risk!");
169fb2fc172SGao Xiang 
170c6aee248SDave Chinner 		error = xfs_ag_shrink_space(last_pag, &tp, -delta);
171fb2fc172SGao Xiang 	}
172c6aee248SDave Chinner 	xfs_perag_put(last_pag);
1739aebe805SDave Chinner 	if (error)
17483a7f86eSDave Chinner 		goto out_trans_cancel;
1759aebe805SDave Chinner 
1761c1c6ebcSDave Chinner 	/*
1771c1c6ebcSDave Chinner 	 * Update changed superblock fields transactionally. These are not
1781c1c6ebcSDave Chinner 	 * seen by the rest of the world until the transaction commit applies
1791c1c6ebcSDave Chinner 	 * them atomically to the superblock.
1801c1c6ebcSDave Chinner 	 */
1811da177e4SLinus Torvalds 	if (nagcount > oagcount)
1821da177e4SLinus Torvalds 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_AGCOUNT, nagcount - oagcount);
183c789c83cSGao Xiang 	if (delta)
184c789c83cSGao Xiang 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_DBLOCKS, delta);
1850410c3bbSDave Chinner 	if (id.nfree)
1860410c3bbSDave Chinner 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, id.nfree);
187014695c0SGao Xiang 
188014695c0SGao Xiang 	/*
189014695c0SGao Xiang 	 * Sync sb counters now to reflect the updated values. This is
190014695c0SGao Xiang 	 * particularly important for shrink because the write verifier
191014695c0SGao Xiang 	 * will fail if sb_fdblocks is ever larger than sb_dblocks.
192014695c0SGao Xiang 	 */
19338c26bfdSDave Chinner 	if (xfs_has_lazysbcount(mp))
194014695c0SGao Xiang 		xfs_log_sb(tp);
195014695c0SGao Xiang 
196f8079b85SChristoph Hellwig 	xfs_trans_set_sync(tp);
19770393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
1981c1c6ebcSDave Chinner 	if (error)
1991da177e4SLinus Torvalds 		return error;
2001c1c6ebcSDave Chinner 
2011da177e4SLinus Torvalds 	/* New allocation groups fully initialized, so update mount struct */
2021da177e4SLinus Torvalds 	if (nagimax)
2031da177e4SLinus Torvalds 		mp->m_maxagi = nagimax;
204055388a3SDave Chinner 	xfs_set_low_space_thresholds(mp);
20552548852SDarrick J. Wong 	mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
2061c1c6ebcSDave Chinner 
207fb2fc172SGao Xiang 	if (delta > 0) {
20820e73b00SDarrick J. Wong 		/*
20920e73b00SDarrick J. Wong 		 * If we expanded the last AG, free the per-AG reservation
21020e73b00SDarrick J. Wong 		 * so we can reinitialize it with the new size.
21120e73b00SDarrick J. Wong 		 */
212c789c83cSGao Xiang 		if (lastag_extended) {
21320e73b00SDarrick J. Wong 			struct xfs_perag	*pag;
21420e73b00SDarrick J. Wong 
2150410c3bbSDave Chinner 			pag = xfs_perag_get(mp, id.agno);
21620e73b00SDarrick J. Wong 			error = xfs_ag_resv_free(pag);
21720e73b00SDarrick J. Wong 			xfs_perag_put(pag);
21820e73b00SDarrick J. Wong 			if (error)
21983a7f86eSDave Chinner 				return error;
22020e73b00SDarrick J. Wong 		}
22183a7f86eSDave Chinner 		/*
222fb2fc172SGao Xiang 		 * Reserve AG metadata blocks. ENOSPC here does not mean there
223fb2fc172SGao Xiang 		 * was a growfs failure, just that there still isn't space for
224fb2fc172SGao Xiang 		 * new user data after the grow has been run.
22583a7f86eSDave Chinner 		 */
22684d69619SDarrick J. Wong 		error = xfs_fs_reserve_ag_blocks(mp);
22783a7f86eSDave Chinner 		if (error == -ENOSPC)
22883a7f86eSDave Chinner 			error = 0;
229fb2fc172SGao Xiang 	}
23083a7f86eSDave Chinner 	return error;
23183a7f86eSDave Chinner 
23283a7f86eSDave Chinner out_trans_cancel:
23383a7f86eSDave Chinner 	xfs_trans_cancel(tp);
2348a456679SLong Li out_free_unused_perag:
2358a456679SLong Li 	if (nagcount > oagcount)
2368a456679SLong Li 		xfs_free_unused_perag_range(mp, oagcount, nagcount);
23783a7f86eSDave Chinner 	return error;
23883a7f86eSDave Chinner }
23983a7f86eSDave Chinner 
24083a7f86eSDave Chinner static int
xfs_growfs_log_private(struct xfs_mount * mp,struct xfs_growfs_log * in)24183a7f86eSDave Chinner xfs_growfs_log_private(
24207aabd9cSGao Xiang 	struct xfs_mount	*mp,	/* mount point for filesystem */
24307aabd9cSGao Xiang 	struct xfs_growfs_log	*in)	/* growfs log input struct */
24483a7f86eSDave Chinner {
24583a7f86eSDave Chinner 	xfs_extlen_t		nb;
24683a7f86eSDave Chinner 
24783a7f86eSDave Chinner 	nb = in->newblocks;
24883a7f86eSDave Chinner 	if (nb < XFS_MIN_LOG_BLOCKS || nb < XFS_B_TO_FSB(mp, XFS_MIN_LOG_BYTES))
24983a7f86eSDave Chinner 		return -EINVAL;
25083a7f86eSDave Chinner 	if (nb == mp->m_sb.sb_logblocks &&
25183a7f86eSDave Chinner 	    in->isint == (mp->m_sb.sb_logstart != 0))
25283a7f86eSDave Chinner 		return -EINVAL;
25383a7f86eSDave Chinner 	/*
25483a7f86eSDave Chinner 	 * Moving the log is hard, need new interfaces to sync
25583a7f86eSDave Chinner 	 * the log first, hold off all activity while moving it.
25683a7f86eSDave Chinner 	 * Can have shorter or longer log in the same space,
25783a7f86eSDave Chinner 	 * or transform internal to external log or vice versa.
25883a7f86eSDave Chinner 	 */
25983a7f86eSDave Chinner 	return -ENOSYS;
26083a7f86eSDave Chinner }
26183a7f86eSDave Chinner 
26283a7f86eSDave Chinner static int
xfs_growfs_imaxpct(struct xfs_mount * mp,__u32 imaxpct)26383a7f86eSDave Chinner xfs_growfs_imaxpct(
26483a7f86eSDave Chinner 	struct xfs_mount	*mp,
26583a7f86eSDave Chinner 	__u32			imaxpct)
26683a7f86eSDave Chinner {
26783a7f86eSDave Chinner 	struct xfs_trans	*tp;
26883a7f86eSDave Chinner 	int			dpct;
26983a7f86eSDave Chinner 	int			error;
27083a7f86eSDave Chinner 
27183a7f86eSDave Chinner 	if (imaxpct > 100)
27283a7f86eSDave Chinner 		return -EINVAL;
27383a7f86eSDave Chinner 
27483a7f86eSDave Chinner 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata,
27583a7f86eSDave Chinner 			XFS_GROWFS_SPACE_RES(mp), 0, XFS_TRANS_RESERVE, &tp);
27683a7f86eSDave Chinner 	if (error)
27783a7f86eSDave Chinner 		return error;
27883a7f86eSDave Chinner 
27983a7f86eSDave Chinner 	dpct = imaxpct - mp->m_sb.sb_imax_pct;
28083a7f86eSDave Chinner 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_IMAXPCT, dpct);
28183a7f86eSDave Chinner 	xfs_trans_set_sync(tp);
28283a7f86eSDave Chinner 	return xfs_trans_commit(tp);
28383a7f86eSDave Chinner }
28483a7f86eSDave Chinner 
28583a7f86eSDave Chinner /*
2861da177e4SLinus Torvalds  * protected versions of growfs function acquire and release locks on the mount
2871da177e4SLinus Torvalds  * point - exported through ioctls: XFS_IOC_FSGROWFSDATA, XFS_IOC_FSGROWFSLOG,
2881da177e4SLinus Torvalds  * XFS_IOC_FSGROWFSRT
2891da177e4SLinus Torvalds  */
2901da177e4SLinus Torvalds int
xfs_growfs_data(struct xfs_mount * mp,struct xfs_growfs_data * in)2911da177e4SLinus Torvalds xfs_growfs_data(
29287444b8cSDave Chinner 	struct xfs_mount	*mp,
29387444b8cSDave Chinner 	struct xfs_growfs_data	*in)
2941da177e4SLinus Torvalds {
29587444b8cSDave Chinner 	int			error = 0;
296743bb465Ssandeen@sandeen.net 
297743bb465Ssandeen@sandeen.net 	if (!capable(CAP_SYS_ADMIN))
2982451337dSDave Chinner 		return -EPERM;
299cc92e7acSChristoph Hellwig 	if (!mutex_trylock(&mp->m_growlock))
3002451337dSDave Chinner 		return -EWOULDBLOCK;
30187444b8cSDave Chinner 
30287444b8cSDave Chinner 	/* update imaxpct separately to the physical grow of the filesystem */
30387444b8cSDave Chinner 	if (in->imaxpct != mp->m_sb.sb_imax_pct) {
30487444b8cSDave Chinner 		error = xfs_growfs_imaxpct(mp, in->imaxpct);
30587444b8cSDave Chinner 		if (error)
30687444b8cSDave Chinner 			goto out_error;
30787444b8cSDave Chinner 	}
30887444b8cSDave Chinner 
30987444b8cSDave Chinner 	if (in->newblocks != mp->m_sb.sb_dblocks) {
3101da177e4SLinus Torvalds 		error = xfs_growfs_data_private(mp, in);
31187444b8cSDave Chinner 		if (error)
31287444b8cSDave Chinner 			goto out_error;
31387444b8cSDave Chinner 	}
31487444b8cSDave Chinner 
31587444b8cSDave Chinner 	/* Post growfs calculations needed to reflect new state in operations */
31687444b8cSDave Chinner 	if (mp->m_sb.sb_imax_pct) {
31787444b8cSDave Chinner 		uint64_t icount = mp->m_sb.sb_dblocks * mp->m_sb.sb_imax_pct;
31887444b8cSDave Chinner 		do_div(icount, 100);
319ef325959SDarrick J. Wong 		M_IGEO(mp)->maxicount = XFS_FSB_TO_INO(mp, icount);
32087444b8cSDave Chinner 	} else
321ef325959SDarrick J. Wong 		M_IGEO(mp)->maxicount = 0;
32287444b8cSDave Chinner 
32383a7f86eSDave Chinner 	/* Update secondary superblocks now the physical grow has completed */
324b16817b6SDave Chinner 	error = xfs_update_secondary_sbs(mp);
32583a7f86eSDave Chinner 
32687444b8cSDave Chinner out_error:
32752785112SChristoph Hellwig 	/*
32852785112SChristoph Hellwig 	 * Increment the generation unconditionally, the error could be from
32952785112SChristoph Hellwig 	 * updating the secondary superblocks, in which case the new size
33052785112SChristoph Hellwig 	 * is live already.
33152785112SChristoph Hellwig 	 */
33252785112SChristoph Hellwig 	mp->m_generation++;
333cc92e7acSChristoph Hellwig 	mutex_unlock(&mp->m_growlock);
3341da177e4SLinus Torvalds 	return error;
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds int
xfs_growfs_log(xfs_mount_t * mp,struct xfs_growfs_log * in)3381da177e4SLinus Torvalds xfs_growfs_log(
3391da177e4SLinus Torvalds 	xfs_mount_t		*mp,
34007aabd9cSGao Xiang 	struct xfs_growfs_log	*in)
3411da177e4SLinus Torvalds {
3421da177e4SLinus Torvalds 	int error;
343743bb465Ssandeen@sandeen.net 
344743bb465Ssandeen@sandeen.net 	if (!capable(CAP_SYS_ADMIN))
3452451337dSDave Chinner 		return -EPERM;
346cc92e7acSChristoph Hellwig 	if (!mutex_trylock(&mp->m_growlock))
3472451337dSDave Chinner 		return -EWOULDBLOCK;
3481da177e4SLinus Torvalds 	error = xfs_growfs_log_private(mp, in);
349cc92e7acSChristoph Hellwig 	mutex_unlock(&mp->m_growlock);
3501da177e4SLinus Torvalds 	return error;
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds /*
3541da177e4SLinus Torvalds  * exported through ioctl XFS_IOC_FSCOUNTS
3551da177e4SLinus Torvalds  */
3561da177e4SLinus Torvalds 
35791083269SEric Sandeen void
xfs_fs_counts(xfs_mount_t * mp,xfs_fsop_counts_t * cnt)3581da177e4SLinus Torvalds xfs_fs_counts(
3591da177e4SLinus Torvalds 	xfs_mount_t		*mp,
3601da177e4SLinus Torvalds 	xfs_fsop_counts_t	*cnt)
3611da177e4SLinus Torvalds {
362501ab323SDave Chinner 	cnt->allocino = percpu_counter_read_positive(&mp->m_icount);
363e88b64eaSDave Chinner 	cnt->freeino = percpu_counter_read_positive(&mp->m_ifree);
3640d485adaSDave Chinner 	cnt->freedata = percpu_counter_read_positive(&mp->m_fdblocks) -
36585bcfa26SDarrick J. Wong 						xfs_fdblocks_unavailable(mp);
3662229276cSDarrick J. Wong 	cnt->freertx = percpu_counter_read_positive(&mp->m_frextents);
3671da177e4SLinus Torvalds }
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds /*
3701da177e4SLinus Torvalds  * exported through ioctl XFS_IOC_SET_RESBLKS & XFS_IOC_GET_RESBLKS
3711da177e4SLinus Torvalds  *
3721da177e4SLinus Torvalds  * xfs_reserve_blocks is called to set m_resblks
3731da177e4SLinus Torvalds  * in the in-core mount table. The number of unused reserved blocks
374c41564b5SNathan Scott  * is kept in m_resblks_avail.
3751da177e4SLinus Torvalds  *
3761da177e4SLinus Torvalds  * Reserve the requested number of blocks if available. Otherwise return
3771da177e4SLinus Torvalds  * as many as possible to satisfy the request. The actual number
3781da177e4SLinus Torvalds  * reserved are returned in outval
3791da177e4SLinus Torvalds  *
3801da177e4SLinus Torvalds  * A null inval pointer indicates that only the current reserved blocks
3811da177e4SLinus Torvalds  * available  should  be returned no settings are changed.
3821da177e4SLinus Torvalds  */
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds int
xfs_reserve_blocks(xfs_mount_t * mp,uint64_t * inval,xfs_fsop_resblks_t * outval)3851da177e4SLinus Torvalds xfs_reserve_blocks(
3861da177e4SLinus Torvalds 	xfs_mount_t             *mp,
387c8ce540dSDarrick J. Wong 	uint64_t              *inval,
3881da177e4SLinus Torvalds 	xfs_fsop_resblks_t      *outval)
3891da177e4SLinus Torvalds {
390c8ce540dSDarrick J. Wong 	int64_t			lcounter, delta;
391c8ce540dSDarrick J. Wong 	int64_t			fdblks_delta = 0;
392c8ce540dSDarrick J. Wong 	uint64_t		request;
393c8ce540dSDarrick J. Wong 	int64_t			free;
394408fd484SBrian Foster 	int			error = 0;
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	/* If inval is null, report current values and return */
397c8ce540dSDarrick J. Wong 	if (inval == (uint64_t *)NULL) {
39884e1e99fSDavid Chinner 		if (!outval)
3992451337dSDave Chinner 			return -EINVAL;
4001da177e4SLinus Torvalds 		outval->resblks = mp->m_resblks;
4011da177e4SLinus Torvalds 		outval->resblks_avail = mp->m_resblks_avail;
402014c2544SJesper Juhl 		return 0;
4031da177e4SLinus Torvalds 	}
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	request = *inval;
406dbcabad1SDavid Chinner 
407dbcabad1SDavid Chinner 	/*
408408fd484SBrian Foster 	 * With per-cpu counters, this becomes an interesting problem. we need
409408fd484SBrian Foster 	 * to work out if we are freeing or allocation blocks first, then we can
410408fd484SBrian Foster 	 * do the modification as necessary.
411dbcabad1SDavid Chinner 	 *
412408fd484SBrian Foster 	 * We do this under the m_sb_lock so that if we are near ENOSPC, we will
413408fd484SBrian Foster 	 * hold out any changes while we work out what to do. This means that
414408fd484SBrian Foster 	 * the amount of free space can change while we do this, so we need to
415408fd484SBrian Foster 	 * retry if we end up trying to reserve more space than is available.
416dbcabad1SDavid Chinner 	 */
4173685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	/*
4201da177e4SLinus Torvalds 	 * If our previous reservation was larger than the current value,
421408fd484SBrian Foster 	 * then move any unused blocks back to the free pool. Modify the resblks
422408fd484SBrian Foster 	 * counters directly since we shouldn't have any problems unreserving
423408fd484SBrian Foster 	 * space.
4241da177e4SLinus Torvalds 	 */
4251da177e4SLinus Torvalds 	if (mp->m_resblks > request) {
4261da177e4SLinus Torvalds 		lcounter = mp->m_resblks_avail - request;
4271da177e4SLinus Torvalds 		if (lcounter  > 0) {		/* release unused blocks */
428dbcabad1SDavid Chinner 			fdblks_delta = lcounter;
4291da177e4SLinus Torvalds 			mp->m_resblks_avail -= lcounter;
4301da177e4SLinus Torvalds 		}
4311da177e4SLinus Torvalds 		mp->m_resblks = request;
432408fd484SBrian Foster 		if (fdblks_delta) {
433408fd484SBrian Foster 			spin_unlock(&mp->m_sb_lock);
434408fd484SBrian Foster 			error = xfs_mod_fdblocks(mp, fdblks_delta, 0);
435408fd484SBrian Foster 			spin_lock(&mp->m_sb_lock);
436408fd484SBrian Foster 		}
4374be536deSDavid Chinner 
438408fd484SBrian Foster 		goto out;
439408fd484SBrian Foster 	}
440408fd484SBrian Foster 
441408fd484SBrian Foster 	/*
442408fd484SBrian Foster 	 * If the request is larger than the current reservation, reserve the
443408fd484SBrian Foster 	 * blocks before we update the reserve counters. Sample m_fdblocks and
444408fd484SBrian Foster 	 * perform a partial reservation if the request exceeds free space.
44515f04fdcSDarrick J. Wong 	 *
44615f04fdcSDarrick J. Wong 	 * The code below estimates how many blocks it can request from
44715f04fdcSDarrick J. Wong 	 * fdblocks to stash in the reserve pool.  This is a classic TOCTOU
44815f04fdcSDarrick J. Wong 	 * race since fdblocks updates are not always coordinated via
4490baa2657SDarrick J. Wong 	 * m_sb_lock.  Set the reserve size even if there's not enough free
4500baa2657SDarrick J. Wong 	 * space to fill it because mod_fdblocks will refill an undersized
4510baa2657SDarrick J. Wong 	 * reserve when it can.
452408fd484SBrian Foster 	 */
4530d485adaSDave Chinner 	free = percpu_counter_sum(&mp->m_fdblocks) -
454c8c56825SDarrick J. Wong 						xfs_fdblocks_unavailable(mp);
4551da177e4SLinus Torvalds 	delta = request - mp->m_resblks;
4560baa2657SDarrick J. Wong 	mp->m_resblks = request;
45715f04fdcSDarrick J. Wong 	if (delta > 0 && free > 0) {
458408fd484SBrian Foster 		/*
459408fd484SBrian Foster 		 * We'll either succeed in getting space from the free block
46015f04fdcSDarrick J. Wong 		 * count or we'll get an ENOSPC.  Don't set the reserved flag
46115f04fdcSDarrick J. Wong 		 * here - we don't want to reserve the extra reserve blocks
46215f04fdcSDarrick J. Wong 		 * from the reserve.
46382be38bcSDarrick J. Wong 		 *
46482be38bcSDarrick J. Wong 		 * The desired reserve size can change after we drop the lock.
46582be38bcSDarrick J. Wong 		 * Use mod_fdblocks to put the space into the reserve or into
46682be38bcSDarrick J. Wong 		 * fdblocks as appropriate.
467408fd484SBrian Foster 		 */
46815f04fdcSDarrick J. Wong 		fdblks_delta = min(free, delta);
469408fd484SBrian Foster 		spin_unlock(&mp->m_sb_lock);
470408fd484SBrian Foster 		error = xfs_mod_fdblocks(mp, -fdblks_delta, 0);
4710baa2657SDarrick J. Wong 		if (!error)
47282be38bcSDarrick J. Wong 			xfs_mod_fdblocks(mp, fdblks_delta, 0);
47382be38bcSDarrick J. Wong 		spin_lock(&mp->m_sb_lock);
4741da177e4SLinus Torvalds 	}
475dbcabad1SDavid Chinner out:
47684e1e99fSDavid Chinner 	if (outval) {
4771da177e4SLinus Torvalds 		outval->resblks = mp->m_resblks;
4781da177e4SLinus Torvalds 		outval->resblks_avail = mp->m_resblks_avail;
47984e1e99fSDavid Chinner 	}
480dbcabad1SDavid Chinner 
481408fd484SBrian Foster 	spin_unlock(&mp->m_sb_lock);
482408fd484SBrian Foster 	return error;
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds int
xfs_fs_goingdown(xfs_mount_t * mp,uint32_t inflags)4861da177e4SLinus Torvalds xfs_fs_goingdown(
4871da177e4SLinus Torvalds 	xfs_mount_t	*mp,
488c8ce540dSDarrick J. Wong 	uint32_t	inflags)
4891da177e4SLinus Torvalds {
4901da177e4SLinus Torvalds 	switch (inflags) {
4911da177e4SLinus Torvalds 	case XFS_FSOP_GOING_FLAGS_DEFAULT: {
492040f04bdSChristoph Hellwig 		if (!freeze_bdev(mp->m_super->s_bdev)) {
4937d04a335SNathan Scott 			xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
494040f04bdSChristoph Hellwig 			thaw_bdev(mp->m_super->s_bdev);
4951da177e4SLinus Torvalds 		}
4961da177e4SLinus Torvalds 		break;
4971da177e4SLinus Torvalds 	}
4981da177e4SLinus Torvalds 	case XFS_FSOP_GOING_FLAGS_LOGFLUSH:
4997d04a335SNathan Scott 		xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
5001da177e4SLinus Torvalds 		break;
5011da177e4SLinus Torvalds 	case XFS_FSOP_GOING_FLAGS_NOLOGFLUSH:
5027d04a335SNathan Scott 		xfs_force_shutdown(mp,
5037d04a335SNathan Scott 				SHUTDOWN_FORCE_UMOUNT | SHUTDOWN_LOG_IO_ERROR);
5041da177e4SLinus Torvalds 		break;
5051da177e4SLinus Torvalds 	default:
5062451337dSDave Chinner 		return -EINVAL;
5071da177e4SLinus Torvalds 	}
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	return 0;
5101da177e4SLinus Torvalds }
5112af51f3aSDave Chinner 
5122af51f3aSDave Chinner /*
5132af51f3aSDave Chinner  * Force a shutdown of the filesystem instantly while keeping the filesystem
5142af51f3aSDave Chinner  * consistent. We don't do an unmount here; just shutdown the shop, make sure
5152af51f3aSDave Chinner  * that absolutely nothing persistent happens to this filesystem after this
5162af51f3aSDave Chinner  * point.
517b36d4651SDave Chinner  *
518b36d4651SDave Chinner  * The shutdown state change is atomic, resulting in the first and only the
519b36d4651SDave Chinner  * first shutdown call processing the shutdown. This means we only shutdown the
520b36d4651SDave Chinner  * log once as it requires, and we don't spam the logs when multiple concurrent
521b36d4651SDave Chinner  * shutdowns race to set the shutdown flags.
5222af51f3aSDave Chinner  */
5232af51f3aSDave Chinner void
xfs_do_force_shutdown(struct xfs_mount * mp,uint32_t flags,char * fname,int lnnum)5242af51f3aSDave Chinner xfs_do_force_shutdown(
52556668a5cSDave Chinner 	struct xfs_mount *mp,
5262eb7550dSDave Chinner 	uint32_t	flags,
5272af51f3aSDave Chinner 	char		*fname,
5282af51f3aSDave Chinner 	int		lnnum)
5292af51f3aSDave Chinner {
530b36d4651SDave Chinner 	int		tag;
531b36d4651SDave Chinner 	const char	*why;
5322af51f3aSDave Chinner 
53341e63621SDave Chinner 
53441e63621SDave Chinner 	if (test_and_set_bit(XFS_OPSTATE_SHUTDOWN, &mp->m_opstate)) {
53541e63621SDave Chinner 		xlog_shutdown_wait(mp->m_log);
53656668a5cSDave Chinner 		return;
53741e63621SDave Chinner 	}
538b36d4651SDave Chinner 	if (mp->m_sb_bp)
539b36d4651SDave Chinner 		mp->m_sb_bp->b_flags |= XBF_DONE;
54056668a5cSDave Chinner 
541b36d4651SDave Chinner 	if (flags & SHUTDOWN_FORCE_UMOUNT)
542b36d4651SDave Chinner 		xfs_alert(mp, "User initiated shutdown received.");
543b36d4651SDave Chinner 
544b36d4651SDave Chinner 	if (xlog_force_shutdown(mp->m_log, flags)) {
545b36d4651SDave Chinner 		tag = XFS_PTAG_SHUTDOWN_LOGERROR;
546b36d4651SDave Chinner 		why = "Log I/O Error";
547b36d4651SDave Chinner 	} else if (flags & SHUTDOWN_CORRUPT_INCORE) {
548b36d4651SDave Chinner 		tag = XFS_PTAG_SHUTDOWN_CORRUPT;
549b36d4651SDave Chinner 		why = "Corruption of in-memory data";
5506f643c57SShiyang Ruan 	} else if (flags & SHUTDOWN_CORRUPT_ONDISK) {
5516f643c57SShiyang Ruan 		tag = XFS_PTAG_SHUTDOWN_CORRUPT;
5526f643c57SShiyang Ruan 		why = "Corruption of on-disk metadata";
553e7caa877SChristoph Hellwig 	} else if (flags & SHUTDOWN_DEVICE_REMOVED) {
554e7caa877SChristoph Hellwig 		tag = XFS_PTAG_SHUTDOWN_IOERROR;
555e7caa877SChristoph Hellwig 		why = "Block device removal";
55628d84620SBrian Foster 	} else {
557b36d4651SDave Chinner 		tag = XFS_PTAG_SHUTDOWN_IOERROR;
558b36d4651SDave Chinner 		why = "Metadata I/O Error";
5592af51f3aSDave Chinner 	}
56056668a5cSDave Chinner 
5617f89c838SDarrick J. Wong 	trace_xfs_force_shutdown(mp, tag, flags, fname, lnnum);
5627f89c838SDarrick J. Wong 
563b36d4651SDave Chinner 	xfs_alert_tag(mp, tag,
564b36d4651SDave Chinner "%s (0x%x) detected at %pS (%s:%d).  Shutting down filesystem.",
565b36d4651SDave Chinner 			why, flags, __return_address, fname, lnnum);
5662af51f3aSDave Chinner 	xfs_alert(mp,
56756668a5cSDave Chinner 		"Please unmount the filesystem and rectify the problem(s)");
568b36d4651SDave Chinner 	if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
569b36d4651SDave Chinner 		xfs_stack_trace();
5702af51f3aSDave Chinner }
57184d69619SDarrick J. Wong 
57284d69619SDarrick J. Wong /*
57384d69619SDarrick J. Wong  * Reserve free space for per-AG metadata.
57484d69619SDarrick J. Wong  */
57584d69619SDarrick J. Wong int
xfs_fs_reserve_ag_blocks(struct xfs_mount * mp)57684d69619SDarrick J. Wong xfs_fs_reserve_ag_blocks(
57784d69619SDarrick J. Wong 	struct xfs_mount	*mp)
57884d69619SDarrick J. Wong {
57984d69619SDarrick J. Wong 	xfs_agnumber_t		agno;
58084d69619SDarrick J. Wong 	struct xfs_perag	*pag;
58184d69619SDarrick J. Wong 	int			error = 0;
58284d69619SDarrick J. Wong 	int			err2;
58384d69619SDarrick J. Wong 
58415a268d9SDarrick J. Wong 	mp->m_finobt_nores = false;
585f250eedcSDave Chinner 	for_each_perag(mp, agno, pag) {
586ebcbef3aSDarrick J. Wong 		err2 = xfs_ag_resv_init(pag, NULL);
58784d69619SDarrick J. Wong 		if (err2 && !error)
58884d69619SDarrick J. Wong 			error = err2;
58984d69619SDarrick J. Wong 	}
59084d69619SDarrick J. Wong 
59184d69619SDarrick J. Wong 	if (error && error != -ENOSPC) {
59284d69619SDarrick J. Wong 		xfs_warn(mp,
59384d69619SDarrick J. Wong 	"Error %d reserving per-AG metadata reserve pool.", error);
59484d69619SDarrick J. Wong 		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
59584d69619SDarrick J. Wong 	}
59684d69619SDarrick J. Wong 
59784d69619SDarrick J. Wong 	return error;
59884d69619SDarrick J. Wong }
59984d69619SDarrick J. Wong 
60084d69619SDarrick J. Wong /*
60184d69619SDarrick J. Wong  * Free space reserved for per-AG metadata.
60284d69619SDarrick J. Wong  */
60384d69619SDarrick J. Wong int
xfs_fs_unreserve_ag_blocks(struct xfs_mount * mp)60484d69619SDarrick J. Wong xfs_fs_unreserve_ag_blocks(
60584d69619SDarrick J. Wong 	struct xfs_mount	*mp)
60684d69619SDarrick J. Wong {
60784d69619SDarrick J. Wong 	xfs_agnumber_t		agno;
60884d69619SDarrick J. Wong 	struct xfs_perag	*pag;
60984d69619SDarrick J. Wong 	int			error = 0;
61084d69619SDarrick J. Wong 	int			err2;
61184d69619SDarrick J. Wong 
612f250eedcSDave Chinner 	for_each_perag(mp, agno, pag) {
61384d69619SDarrick J. Wong 		err2 = xfs_ag_resv_free(pag);
61484d69619SDarrick J. Wong 		if (err2 && !error)
61584d69619SDarrick J. Wong 			error = err2;
61684d69619SDarrick J. Wong 	}
61784d69619SDarrick J. Wong 
61884d69619SDarrick J. Wong 	if (error)
61984d69619SDarrick J. Wong 		xfs_warn(mp,
62084d69619SDarrick J. Wong 	"Error %d freeing per-AG metadata reserve pool.", error);
62184d69619SDarrick J. Wong 
62284d69619SDarrick J. Wong 	return error;
62384d69619SDarrick J. Wong }
624