xref: /openbmc/linux/fs/xfs/xfs_fsops.c (revision d37cf9b63113f13d742713881ce691fc615d8b3b)
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 {
90*bb305f88SChristoph Hellwig 	xfs_agnumber_t		oagcount = mp->m_sb.sb_agcount;
91e8222613SDave Chinner 	struct xfs_buf		*bp;
9283a7f86eSDave Chinner 	int			error;
931da177e4SLinus Torvalds 	xfs_agnumber_t		nagcount;
941da177e4SLinus Torvalds 	xfs_agnumber_t		nagimax = 0;
95ce5e1062SGao Xiang 	xfs_rfsblock_t		nb, nb_div, nb_mod;
96fb2fc172SGao Xiang 	int64_t			delta;
97ed04a91fSDarrick J. Wong 	bool			lastag_extended = false;
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 
141*bb305f88SChristoph Hellwig 	/* TODO: shrinking the entire AGs hasn't yet completed */
142*bb305f88SChristoph Hellwig 	if (nagcount < oagcount)
143*bb305f88SChristoph Hellwig 		return -EINVAL;
144*bb305f88SChristoph Hellwig 
1451c1c6ebcSDave Chinner 	/* allocate the new per-ag structures */
146*bb305f88SChristoph Hellwig 	error = xfs_initialize_perag(mp, oagcount, nagcount, nb, &nagimax);
1471c1c6ebcSDave Chinner 	if (error)
1481c1c6ebcSDave Chinner 		return error;
1491c1c6ebcSDave Chinner 
15006f3ef6eSDarrick J. Wong 	if (delta > 0)
151253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata,
15206f3ef6eSDarrick J. Wong 				XFS_GROWFS_SPACE_RES(mp), 0, XFS_TRANS_RESERVE,
15306f3ef6eSDarrick J. Wong 				&tp);
15406f3ef6eSDarrick J. Wong 	else
15506f3ef6eSDarrick J. Wong 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata, -delta, 0,
15606f3ef6eSDarrick J. Wong 				0, &tp);
157253f4911SChristoph Hellwig 	if (error)
1588a456679SLong Li 		goto out_free_unused_perag;
1591da177e4SLinus Torvalds 
160c6aee248SDave Chinner 	last_pag = xfs_perag_get(mp, oagcount - 1);
161fb2fc172SGao Xiang 	if (delta > 0) {
162c789c83cSGao Xiang 		error = xfs_resizefs_init_new_ags(tp, &id, oagcount, nagcount,
163c6aee248SDave Chinner 				delta, last_pag, &lastag_extended);
164fb2fc172SGao Xiang 	} else {
165df5660cfSDarrick J. Wong 		xfs_warn_mount(mp, XFS_OPSTATE_WARNED_SHRINK,
166fb2fc172SGao Xiang 	"EXPERIMENTAL online shrink feature in use. Use at your own risk!");
167fb2fc172SGao Xiang 
168c6aee248SDave Chinner 		error = xfs_ag_shrink_space(last_pag, &tp, -delta);
169fb2fc172SGao Xiang 	}
170c6aee248SDave Chinner 	xfs_perag_put(last_pag);
1719aebe805SDave Chinner 	if (error)
17283a7f86eSDave Chinner 		goto out_trans_cancel;
1739aebe805SDave Chinner 
1741c1c6ebcSDave Chinner 	/*
1751c1c6ebcSDave Chinner 	 * Update changed superblock fields transactionally. These are not
1761c1c6ebcSDave Chinner 	 * seen by the rest of the world until the transaction commit applies
1771c1c6ebcSDave Chinner 	 * them atomically to the superblock.
1781c1c6ebcSDave Chinner 	 */
1791da177e4SLinus Torvalds 	if (nagcount > oagcount)
1801da177e4SLinus Torvalds 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_AGCOUNT, nagcount - oagcount);
181c789c83cSGao Xiang 	if (delta)
182c789c83cSGao Xiang 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_DBLOCKS, delta);
1830410c3bbSDave Chinner 	if (id.nfree)
1840410c3bbSDave Chinner 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, id.nfree);
185014695c0SGao Xiang 
186014695c0SGao Xiang 	/*
187014695c0SGao Xiang 	 * Sync sb counters now to reflect the updated values. This is
188014695c0SGao Xiang 	 * particularly important for shrink because the write verifier
189014695c0SGao Xiang 	 * will fail if sb_fdblocks is ever larger than sb_dblocks.
190014695c0SGao Xiang 	 */
19138c26bfdSDave Chinner 	if (xfs_has_lazysbcount(mp))
192014695c0SGao Xiang 		xfs_log_sb(tp);
193014695c0SGao Xiang 
194f8079b85SChristoph Hellwig 	xfs_trans_set_sync(tp);
19570393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
1961c1c6ebcSDave Chinner 	if (error)
1971da177e4SLinus Torvalds 		return error;
1981c1c6ebcSDave Chinner 
1991da177e4SLinus Torvalds 	/* New allocation groups fully initialized, so update mount struct */
2001da177e4SLinus Torvalds 	if (nagimax)
2011da177e4SLinus Torvalds 		mp->m_maxagi = nagimax;
202055388a3SDave Chinner 	xfs_set_low_space_thresholds(mp);
20352548852SDarrick J. Wong 	mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
2041c1c6ebcSDave Chinner 
205fb2fc172SGao Xiang 	if (delta > 0) {
20620e73b00SDarrick J. Wong 		/*
20720e73b00SDarrick J. Wong 		 * If we expanded the last AG, free the per-AG reservation
20820e73b00SDarrick J. Wong 		 * so we can reinitialize it with the new size.
20920e73b00SDarrick J. Wong 		 */
210c789c83cSGao Xiang 		if (lastag_extended) {
21120e73b00SDarrick J. Wong 			struct xfs_perag	*pag;
21220e73b00SDarrick J. Wong 
2130410c3bbSDave Chinner 			pag = xfs_perag_get(mp, id.agno);
21420e73b00SDarrick J. Wong 			error = xfs_ag_resv_free(pag);
21520e73b00SDarrick J. Wong 			xfs_perag_put(pag);
21620e73b00SDarrick J. Wong 			if (error)
21783a7f86eSDave Chinner 				return error;
21820e73b00SDarrick J. Wong 		}
21983a7f86eSDave Chinner 		/*
220fb2fc172SGao Xiang 		 * Reserve AG metadata blocks. ENOSPC here does not mean there
221fb2fc172SGao Xiang 		 * was a growfs failure, just that there still isn't space for
222fb2fc172SGao Xiang 		 * new user data after the grow has been run.
22383a7f86eSDave Chinner 		 */
22484d69619SDarrick J. Wong 		error = xfs_fs_reserve_ag_blocks(mp);
22583a7f86eSDave Chinner 		if (error == -ENOSPC)
22683a7f86eSDave Chinner 			error = 0;
227fb2fc172SGao Xiang 	}
22883a7f86eSDave Chinner 	return error;
22983a7f86eSDave Chinner 
23083a7f86eSDave Chinner out_trans_cancel:
23183a7f86eSDave Chinner 	xfs_trans_cancel(tp);
2328a456679SLong Li out_free_unused_perag:
2338a456679SLong Li 	if (nagcount > oagcount)
2348a456679SLong Li 		xfs_free_unused_perag_range(mp, oagcount, nagcount);
23583a7f86eSDave Chinner 	return error;
23683a7f86eSDave Chinner }
23783a7f86eSDave Chinner 
23883a7f86eSDave Chinner static int
xfs_growfs_log_private(struct xfs_mount * mp,struct xfs_growfs_log * in)23983a7f86eSDave Chinner xfs_growfs_log_private(
24007aabd9cSGao Xiang 	struct xfs_mount	*mp,	/* mount point for filesystem */
24107aabd9cSGao Xiang 	struct xfs_growfs_log	*in)	/* growfs log input struct */
24283a7f86eSDave Chinner {
24383a7f86eSDave Chinner 	xfs_extlen_t		nb;
24483a7f86eSDave Chinner 
24583a7f86eSDave Chinner 	nb = in->newblocks;
24683a7f86eSDave Chinner 	if (nb < XFS_MIN_LOG_BLOCKS || nb < XFS_B_TO_FSB(mp, XFS_MIN_LOG_BYTES))
24783a7f86eSDave Chinner 		return -EINVAL;
24883a7f86eSDave Chinner 	if (nb == mp->m_sb.sb_logblocks &&
24983a7f86eSDave Chinner 	    in->isint == (mp->m_sb.sb_logstart != 0))
25083a7f86eSDave Chinner 		return -EINVAL;
25183a7f86eSDave Chinner 	/*
25283a7f86eSDave Chinner 	 * Moving the log is hard, need new interfaces to sync
25383a7f86eSDave Chinner 	 * the log first, hold off all activity while moving it.
25483a7f86eSDave Chinner 	 * Can have shorter or longer log in the same space,
25583a7f86eSDave Chinner 	 * or transform internal to external log or vice versa.
25683a7f86eSDave Chinner 	 */
25783a7f86eSDave Chinner 	return -ENOSYS;
25883a7f86eSDave Chinner }
25983a7f86eSDave Chinner 
26083a7f86eSDave Chinner static int
xfs_growfs_imaxpct(struct xfs_mount * mp,__u32 imaxpct)26183a7f86eSDave Chinner xfs_growfs_imaxpct(
26283a7f86eSDave Chinner 	struct xfs_mount	*mp,
26383a7f86eSDave Chinner 	__u32			imaxpct)
26483a7f86eSDave Chinner {
26583a7f86eSDave Chinner 	struct xfs_trans	*tp;
26683a7f86eSDave Chinner 	int			dpct;
26783a7f86eSDave Chinner 	int			error;
26883a7f86eSDave Chinner 
26983a7f86eSDave Chinner 	if (imaxpct > 100)
27083a7f86eSDave Chinner 		return -EINVAL;
27183a7f86eSDave Chinner 
27283a7f86eSDave Chinner 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata,
27383a7f86eSDave Chinner 			XFS_GROWFS_SPACE_RES(mp), 0, XFS_TRANS_RESERVE, &tp);
27483a7f86eSDave Chinner 	if (error)
27583a7f86eSDave Chinner 		return error;
27683a7f86eSDave Chinner 
27783a7f86eSDave Chinner 	dpct = imaxpct - mp->m_sb.sb_imax_pct;
27883a7f86eSDave Chinner 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_IMAXPCT, dpct);
27983a7f86eSDave Chinner 	xfs_trans_set_sync(tp);
28083a7f86eSDave Chinner 	return xfs_trans_commit(tp);
28183a7f86eSDave Chinner }
28283a7f86eSDave Chinner 
28383a7f86eSDave Chinner /*
2841da177e4SLinus Torvalds  * protected versions of growfs function acquire and release locks on the mount
2851da177e4SLinus Torvalds  * point - exported through ioctls: XFS_IOC_FSGROWFSDATA, XFS_IOC_FSGROWFSLOG,
2861da177e4SLinus Torvalds  * XFS_IOC_FSGROWFSRT
2871da177e4SLinus Torvalds  */
2881da177e4SLinus Torvalds int
xfs_growfs_data(struct xfs_mount * mp,struct xfs_growfs_data * in)2891da177e4SLinus Torvalds xfs_growfs_data(
29087444b8cSDave Chinner 	struct xfs_mount	*mp,
29187444b8cSDave Chinner 	struct xfs_growfs_data	*in)
2921da177e4SLinus Torvalds {
29387444b8cSDave Chinner 	int			error = 0;
294743bb465Ssandeen@sandeen.net 
295743bb465Ssandeen@sandeen.net 	if (!capable(CAP_SYS_ADMIN))
2962451337dSDave Chinner 		return -EPERM;
297cc92e7acSChristoph Hellwig 	if (!mutex_trylock(&mp->m_growlock))
2982451337dSDave Chinner 		return -EWOULDBLOCK;
29987444b8cSDave Chinner 
30087444b8cSDave Chinner 	/* update imaxpct separately to the physical grow of the filesystem */
30187444b8cSDave Chinner 	if (in->imaxpct != mp->m_sb.sb_imax_pct) {
30287444b8cSDave Chinner 		error = xfs_growfs_imaxpct(mp, in->imaxpct);
30387444b8cSDave Chinner 		if (error)
30487444b8cSDave Chinner 			goto out_error;
30587444b8cSDave Chinner 	}
30687444b8cSDave Chinner 
30787444b8cSDave Chinner 	if (in->newblocks != mp->m_sb.sb_dblocks) {
3081da177e4SLinus Torvalds 		error = xfs_growfs_data_private(mp, in);
30987444b8cSDave Chinner 		if (error)
31087444b8cSDave Chinner 			goto out_error;
31187444b8cSDave Chinner 	}
31287444b8cSDave Chinner 
31387444b8cSDave Chinner 	/* Post growfs calculations needed to reflect new state in operations */
31487444b8cSDave Chinner 	if (mp->m_sb.sb_imax_pct) {
31587444b8cSDave Chinner 		uint64_t icount = mp->m_sb.sb_dblocks * mp->m_sb.sb_imax_pct;
31687444b8cSDave Chinner 		do_div(icount, 100);
317ef325959SDarrick J. Wong 		M_IGEO(mp)->maxicount = XFS_FSB_TO_INO(mp, icount);
31887444b8cSDave Chinner 	} else
319ef325959SDarrick J. Wong 		M_IGEO(mp)->maxicount = 0;
32087444b8cSDave Chinner 
32183a7f86eSDave Chinner 	/* Update secondary superblocks now the physical grow has completed */
322b16817b6SDave Chinner 	error = xfs_update_secondary_sbs(mp);
32383a7f86eSDave Chinner 
32487444b8cSDave Chinner out_error:
32552785112SChristoph Hellwig 	/*
32652785112SChristoph Hellwig 	 * Increment the generation unconditionally, the error could be from
32752785112SChristoph Hellwig 	 * updating the secondary superblocks, in which case the new size
32852785112SChristoph Hellwig 	 * is live already.
32952785112SChristoph Hellwig 	 */
33052785112SChristoph Hellwig 	mp->m_generation++;
331cc92e7acSChristoph Hellwig 	mutex_unlock(&mp->m_growlock);
3321da177e4SLinus Torvalds 	return error;
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds int
xfs_growfs_log(xfs_mount_t * mp,struct xfs_growfs_log * in)3361da177e4SLinus Torvalds xfs_growfs_log(
3371da177e4SLinus Torvalds 	xfs_mount_t		*mp,
33807aabd9cSGao Xiang 	struct xfs_growfs_log	*in)
3391da177e4SLinus Torvalds {
3401da177e4SLinus Torvalds 	int error;
341743bb465Ssandeen@sandeen.net 
342743bb465Ssandeen@sandeen.net 	if (!capable(CAP_SYS_ADMIN))
3432451337dSDave Chinner 		return -EPERM;
344cc92e7acSChristoph Hellwig 	if (!mutex_trylock(&mp->m_growlock))
3452451337dSDave Chinner 		return -EWOULDBLOCK;
3461da177e4SLinus Torvalds 	error = xfs_growfs_log_private(mp, in);
347cc92e7acSChristoph Hellwig 	mutex_unlock(&mp->m_growlock);
3481da177e4SLinus Torvalds 	return error;
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds /*
3521da177e4SLinus Torvalds  * exported through ioctl XFS_IOC_FSCOUNTS
3531da177e4SLinus Torvalds  */
3541da177e4SLinus Torvalds 
35591083269SEric Sandeen void
xfs_fs_counts(xfs_mount_t * mp,xfs_fsop_counts_t * cnt)3561da177e4SLinus Torvalds xfs_fs_counts(
3571da177e4SLinus Torvalds 	xfs_mount_t		*mp,
3581da177e4SLinus Torvalds 	xfs_fsop_counts_t	*cnt)
3591da177e4SLinus Torvalds {
360501ab323SDave Chinner 	cnt->allocino = percpu_counter_read_positive(&mp->m_icount);
361e88b64eaSDave Chinner 	cnt->freeino = percpu_counter_read_positive(&mp->m_ifree);
3620d485adaSDave Chinner 	cnt->freedata = percpu_counter_read_positive(&mp->m_fdblocks) -
36385bcfa26SDarrick J. Wong 						xfs_fdblocks_unavailable(mp);
3642229276cSDarrick J. Wong 	cnt->freertx = percpu_counter_read_positive(&mp->m_frextents);
3651da177e4SLinus Torvalds }
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds /*
3681da177e4SLinus Torvalds  * exported through ioctl XFS_IOC_SET_RESBLKS & XFS_IOC_GET_RESBLKS
3691da177e4SLinus Torvalds  *
3701da177e4SLinus Torvalds  * xfs_reserve_blocks is called to set m_resblks
3711da177e4SLinus Torvalds  * in the in-core mount table. The number of unused reserved blocks
372c41564b5SNathan Scott  * is kept in m_resblks_avail.
3731da177e4SLinus Torvalds  *
3741da177e4SLinus Torvalds  * Reserve the requested number of blocks if available. Otherwise return
3751da177e4SLinus Torvalds  * as many as possible to satisfy the request. The actual number
3761da177e4SLinus Torvalds  * reserved are returned in outval
3771da177e4SLinus Torvalds  *
3781da177e4SLinus Torvalds  * A null inval pointer indicates that only the current reserved blocks
3791da177e4SLinus Torvalds  * available  should  be returned no settings are changed.
3801da177e4SLinus Torvalds  */
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds int
xfs_reserve_blocks(xfs_mount_t * mp,uint64_t * inval,xfs_fsop_resblks_t * outval)3831da177e4SLinus Torvalds xfs_reserve_blocks(
3841da177e4SLinus Torvalds 	xfs_mount_t             *mp,
385c8ce540dSDarrick J. Wong 	uint64_t              *inval,
3861da177e4SLinus Torvalds 	xfs_fsop_resblks_t      *outval)
3871da177e4SLinus Torvalds {
388c8ce540dSDarrick J. Wong 	int64_t			lcounter, delta;
389c8ce540dSDarrick J. Wong 	int64_t			fdblks_delta = 0;
390c8ce540dSDarrick J. Wong 	uint64_t		request;
391c8ce540dSDarrick J. Wong 	int64_t			free;
392408fd484SBrian Foster 	int			error = 0;
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	/* If inval is null, report current values and return */
395c8ce540dSDarrick J. Wong 	if (inval == (uint64_t *)NULL) {
39684e1e99fSDavid Chinner 		if (!outval)
3972451337dSDave Chinner 			return -EINVAL;
3981da177e4SLinus Torvalds 		outval->resblks = mp->m_resblks;
3991da177e4SLinus Torvalds 		outval->resblks_avail = mp->m_resblks_avail;
400014c2544SJesper Juhl 		return 0;
4011da177e4SLinus Torvalds 	}
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	request = *inval;
404dbcabad1SDavid Chinner 
405dbcabad1SDavid Chinner 	/*
406408fd484SBrian Foster 	 * With per-cpu counters, this becomes an interesting problem. we need
407408fd484SBrian Foster 	 * to work out if we are freeing or allocation blocks first, then we can
408408fd484SBrian Foster 	 * do the modification as necessary.
409dbcabad1SDavid Chinner 	 *
410408fd484SBrian Foster 	 * We do this under the m_sb_lock so that if we are near ENOSPC, we will
411408fd484SBrian Foster 	 * hold out any changes while we work out what to do. This means that
412408fd484SBrian Foster 	 * the amount of free space can change while we do this, so we need to
413408fd484SBrian Foster 	 * retry if we end up trying to reserve more space than is available.
414dbcabad1SDavid Chinner 	 */
4153685c2a1SEric Sandeen 	spin_lock(&mp->m_sb_lock);
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	/*
4181da177e4SLinus Torvalds 	 * If our previous reservation was larger than the current value,
419408fd484SBrian Foster 	 * then move any unused blocks back to the free pool. Modify the resblks
420408fd484SBrian Foster 	 * counters directly since we shouldn't have any problems unreserving
421408fd484SBrian Foster 	 * space.
4221da177e4SLinus Torvalds 	 */
4231da177e4SLinus Torvalds 	if (mp->m_resblks > request) {
4241da177e4SLinus Torvalds 		lcounter = mp->m_resblks_avail - request;
4251da177e4SLinus Torvalds 		if (lcounter  > 0) {		/* release unused blocks */
426dbcabad1SDavid Chinner 			fdblks_delta = lcounter;
4271da177e4SLinus Torvalds 			mp->m_resblks_avail -= lcounter;
4281da177e4SLinus Torvalds 		}
4291da177e4SLinus Torvalds 		mp->m_resblks = request;
430408fd484SBrian Foster 		if (fdblks_delta) {
431408fd484SBrian Foster 			spin_unlock(&mp->m_sb_lock);
432408fd484SBrian Foster 			error = xfs_mod_fdblocks(mp, fdblks_delta, 0);
433408fd484SBrian Foster 			spin_lock(&mp->m_sb_lock);
434408fd484SBrian Foster 		}
4354be536deSDavid Chinner 
436408fd484SBrian Foster 		goto out;
437408fd484SBrian Foster 	}
438408fd484SBrian Foster 
439408fd484SBrian Foster 	/*
440408fd484SBrian Foster 	 * If the request is larger than the current reservation, reserve the
441408fd484SBrian Foster 	 * blocks before we update the reserve counters. Sample m_fdblocks and
442408fd484SBrian Foster 	 * perform a partial reservation if the request exceeds free space.
44315f04fdcSDarrick J. Wong 	 *
44415f04fdcSDarrick J. Wong 	 * The code below estimates how many blocks it can request from
44515f04fdcSDarrick J. Wong 	 * fdblocks to stash in the reserve pool.  This is a classic TOCTOU
44615f04fdcSDarrick J. Wong 	 * race since fdblocks updates are not always coordinated via
4470baa2657SDarrick J. Wong 	 * m_sb_lock.  Set the reserve size even if there's not enough free
4480baa2657SDarrick J. Wong 	 * space to fill it because mod_fdblocks will refill an undersized
4490baa2657SDarrick J. Wong 	 * reserve when it can.
450408fd484SBrian Foster 	 */
4510d485adaSDave Chinner 	free = percpu_counter_sum(&mp->m_fdblocks) -
452c8c56825SDarrick J. Wong 						xfs_fdblocks_unavailable(mp);
4531da177e4SLinus Torvalds 	delta = request - mp->m_resblks;
4540baa2657SDarrick J. Wong 	mp->m_resblks = request;
45515f04fdcSDarrick J. Wong 	if (delta > 0 && free > 0) {
456408fd484SBrian Foster 		/*
457408fd484SBrian Foster 		 * We'll either succeed in getting space from the free block
45815f04fdcSDarrick J. Wong 		 * count or we'll get an ENOSPC.  Don't set the reserved flag
45915f04fdcSDarrick J. Wong 		 * here - we don't want to reserve the extra reserve blocks
46015f04fdcSDarrick J. Wong 		 * from the reserve.
46182be38bcSDarrick J. Wong 		 *
46282be38bcSDarrick J. Wong 		 * The desired reserve size can change after we drop the lock.
46382be38bcSDarrick J. Wong 		 * Use mod_fdblocks to put the space into the reserve or into
46482be38bcSDarrick J. Wong 		 * fdblocks as appropriate.
465408fd484SBrian Foster 		 */
46615f04fdcSDarrick J. Wong 		fdblks_delta = min(free, delta);
467408fd484SBrian Foster 		spin_unlock(&mp->m_sb_lock);
468408fd484SBrian Foster 		error = xfs_mod_fdblocks(mp, -fdblks_delta, 0);
4690baa2657SDarrick J. Wong 		if (!error)
47082be38bcSDarrick J. Wong 			xfs_mod_fdblocks(mp, fdblks_delta, 0);
47182be38bcSDarrick J. Wong 		spin_lock(&mp->m_sb_lock);
4721da177e4SLinus Torvalds 	}
473dbcabad1SDavid Chinner out:
47484e1e99fSDavid Chinner 	if (outval) {
4751da177e4SLinus Torvalds 		outval->resblks = mp->m_resblks;
4761da177e4SLinus Torvalds 		outval->resblks_avail = mp->m_resblks_avail;
47784e1e99fSDavid Chinner 	}
478dbcabad1SDavid Chinner 
479408fd484SBrian Foster 	spin_unlock(&mp->m_sb_lock);
480408fd484SBrian Foster 	return error;
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds int
xfs_fs_goingdown(xfs_mount_t * mp,uint32_t inflags)4841da177e4SLinus Torvalds xfs_fs_goingdown(
4851da177e4SLinus Torvalds 	xfs_mount_t	*mp,
486c8ce540dSDarrick J. Wong 	uint32_t	inflags)
4871da177e4SLinus Torvalds {
4881da177e4SLinus Torvalds 	switch (inflags) {
4891da177e4SLinus Torvalds 	case XFS_FSOP_GOING_FLAGS_DEFAULT: {
490040f04bdSChristoph Hellwig 		if (!freeze_bdev(mp->m_super->s_bdev)) {
4917d04a335SNathan Scott 			xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
492040f04bdSChristoph Hellwig 			thaw_bdev(mp->m_super->s_bdev);
4931da177e4SLinus Torvalds 		}
4941da177e4SLinus Torvalds 		break;
4951da177e4SLinus Torvalds 	}
4961da177e4SLinus Torvalds 	case XFS_FSOP_GOING_FLAGS_LOGFLUSH:
4977d04a335SNathan Scott 		xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
4981da177e4SLinus Torvalds 		break;
4991da177e4SLinus Torvalds 	case XFS_FSOP_GOING_FLAGS_NOLOGFLUSH:
5007d04a335SNathan Scott 		xfs_force_shutdown(mp,
5017d04a335SNathan Scott 				SHUTDOWN_FORCE_UMOUNT | SHUTDOWN_LOG_IO_ERROR);
5021da177e4SLinus Torvalds 		break;
5031da177e4SLinus Torvalds 	default:
5042451337dSDave Chinner 		return -EINVAL;
5051da177e4SLinus Torvalds 	}
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	return 0;
5081da177e4SLinus Torvalds }
5092af51f3aSDave Chinner 
5102af51f3aSDave Chinner /*
5112af51f3aSDave Chinner  * Force a shutdown of the filesystem instantly while keeping the filesystem
5122af51f3aSDave Chinner  * consistent. We don't do an unmount here; just shutdown the shop, make sure
5132af51f3aSDave Chinner  * that absolutely nothing persistent happens to this filesystem after this
5142af51f3aSDave Chinner  * point.
515b36d4651SDave Chinner  *
516b36d4651SDave Chinner  * The shutdown state change is atomic, resulting in the first and only the
517b36d4651SDave Chinner  * first shutdown call processing the shutdown. This means we only shutdown the
518b36d4651SDave Chinner  * log once as it requires, and we don't spam the logs when multiple concurrent
519b36d4651SDave Chinner  * shutdowns race to set the shutdown flags.
5202af51f3aSDave Chinner  */
5212af51f3aSDave Chinner void
xfs_do_force_shutdown(struct xfs_mount * mp,uint32_t flags,char * fname,int lnnum)5222af51f3aSDave Chinner xfs_do_force_shutdown(
52356668a5cSDave Chinner 	struct xfs_mount *mp,
5242eb7550dSDave Chinner 	uint32_t	flags,
5252af51f3aSDave Chinner 	char		*fname,
5262af51f3aSDave Chinner 	int		lnnum)
5272af51f3aSDave Chinner {
528b36d4651SDave Chinner 	int		tag;
529b36d4651SDave Chinner 	const char	*why;
5302af51f3aSDave Chinner 
53141e63621SDave Chinner 
53241e63621SDave Chinner 	if (test_and_set_bit(XFS_OPSTATE_SHUTDOWN, &mp->m_opstate)) {
53341e63621SDave Chinner 		xlog_shutdown_wait(mp->m_log);
53456668a5cSDave Chinner 		return;
53541e63621SDave Chinner 	}
536b36d4651SDave Chinner 	if (mp->m_sb_bp)
537b36d4651SDave Chinner 		mp->m_sb_bp->b_flags |= XBF_DONE;
53856668a5cSDave Chinner 
539b36d4651SDave Chinner 	if (flags & SHUTDOWN_FORCE_UMOUNT)
540b36d4651SDave Chinner 		xfs_alert(mp, "User initiated shutdown received.");
541b36d4651SDave Chinner 
542b36d4651SDave Chinner 	if (xlog_force_shutdown(mp->m_log, flags)) {
543b36d4651SDave Chinner 		tag = XFS_PTAG_SHUTDOWN_LOGERROR;
544b36d4651SDave Chinner 		why = "Log I/O Error";
545b36d4651SDave Chinner 	} else if (flags & SHUTDOWN_CORRUPT_INCORE) {
546b36d4651SDave Chinner 		tag = XFS_PTAG_SHUTDOWN_CORRUPT;
547b36d4651SDave Chinner 		why = "Corruption of in-memory data";
5486f643c57SShiyang Ruan 	} else if (flags & SHUTDOWN_CORRUPT_ONDISK) {
5496f643c57SShiyang Ruan 		tag = XFS_PTAG_SHUTDOWN_CORRUPT;
5506f643c57SShiyang Ruan 		why = "Corruption of on-disk metadata";
551e7caa877SChristoph Hellwig 	} else if (flags & SHUTDOWN_DEVICE_REMOVED) {
552e7caa877SChristoph Hellwig 		tag = XFS_PTAG_SHUTDOWN_IOERROR;
553e7caa877SChristoph Hellwig 		why = "Block device removal";
55428d84620SBrian Foster 	} else {
555b36d4651SDave Chinner 		tag = XFS_PTAG_SHUTDOWN_IOERROR;
556b36d4651SDave Chinner 		why = "Metadata I/O Error";
5572af51f3aSDave Chinner 	}
55856668a5cSDave Chinner 
5597f89c838SDarrick J. Wong 	trace_xfs_force_shutdown(mp, tag, flags, fname, lnnum);
5607f89c838SDarrick J. Wong 
561b36d4651SDave Chinner 	xfs_alert_tag(mp, tag,
562b36d4651SDave Chinner "%s (0x%x) detected at %pS (%s:%d).  Shutting down filesystem.",
563b36d4651SDave Chinner 			why, flags, __return_address, fname, lnnum);
5642af51f3aSDave Chinner 	xfs_alert(mp,
56556668a5cSDave Chinner 		"Please unmount the filesystem and rectify the problem(s)");
566b36d4651SDave Chinner 	if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
567b36d4651SDave Chinner 		xfs_stack_trace();
5682af51f3aSDave Chinner }
56984d69619SDarrick J. Wong 
57084d69619SDarrick J. Wong /*
57184d69619SDarrick J. Wong  * Reserve free space for per-AG metadata.
57284d69619SDarrick J. Wong  */
57384d69619SDarrick J. Wong int
xfs_fs_reserve_ag_blocks(struct xfs_mount * mp)57484d69619SDarrick J. Wong xfs_fs_reserve_ag_blocks(
57584d69619SDarrick J. Wong 	struct xfs_mount	*mp)
57684d69619SDarrick J. Wong {
57784d69619SDarrick J. Wong 	xfs_agnumber_t		agno;
57884d69619SDarrick J. Wong 	struct xfs_perag	*pag;
57984d69619SDarrick J. Wong 	int			error = 0;
58084d69619SDarrick J. Wong 	int			err2;
58184d69619SDarrick J. Wong 
58215a268d9SDarrick J. Wong 	mp->m_finobt_nores = false;
583f250eedcSDave Chinner 	for_each_perag(mp, agno, pag) {
584ebcbef3aSDarrick J. Wong 		err2 = xfs_ag_resv_init(pag, NULL);
58584d69619SDarrick J. Wong 		if (err2 && !error)
58684d69619SDarrick J. Wong 			error = err2;
58784d69619SDarrick J. Wong 	}
58884d69619SDarrick J. Wong 
58984d69619SDarrick J. Wong 	if (error && error != -ENOSPC) {
59084d69619SDarrick J. Wong 		xfs_warn(mp,
59184d69619SDarrick J. Wong 	"Error %d reserving per-AG metadata reserve pool.", error);
59284d69619SDarrick J. Wong 		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
59384d69619SDarrick J. Wong 	}
59484d69619SDarrick J. Wong 
59584d69619SDarrick J. Wong 	return error;
59684d69619SDarrick J. Wong }
59784d69619SDarrick J. Wong 
59884d69619SDarrick J. Wong /*
59984d69619SDarrick J. Wong  * Free space reserved for per-AG metadata.
60084d69619SDarrick J. Wong  */
60184d69619SDarrick J. Wong int
xfs_fs_unreserve_ag_blocks(struct xfs_mount * mp)60284d69619SDarrick J. Wong xfs_fs_unreserve_ag_blocks(
60384d69619SDarrick J. Wong 	struct xfs_mount	*mp)
60484d69619SDarrick J. Wong {
60584d69619SDarrick J. Wong 	xfs_agnumber_t		agno;
60684d69619SDarrick J. Wong 	struct xfs_perag	*pag;
60784d69619SDarrick J. Wong 	int			error = 0;
60884d69619SDarrick J. Wong 	int			err2;
60984d69619SDarrick J. Wong 
610f250eedcSDave Chinner 	for_each_perag(mp, agno, pag) {
61184d69619SDarrick J. Wong 		err2 = xfs_ag_resv_free(pag);
61284d69619SDarrick J. Wong 		if (err2 && !error)
61384d69619SDarrick J. Wong 			error = err2;
61484d69619SDarrick J. Wong 	}
61584d69619SDarrick J. Wong 
61684d69619SDarrick J. Wong 	if (error)
61784d69619SDarrick J. Wong 		xfs_warn(mp,
61884d69619SDarrick J. Wong 	"Error %d freeing per-AG metadata reserve pool.", error);
61984d69619SDarrick J. Wong 
62084d69619SDarrick J. Wong 	return error;
62184d69619SDarrick J. Wong }
622