xref: /OK3568_Linux_fs/kernel/fs/xfs/xfs_fsops.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4*4882a593Smuzhiyun  * All Rights Reserved.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun #include "xfs.h"
7*4882a593Smuzhiyun #include "xfs_fs.h"
8*4882a593Smuzhiyun #include "xfs_shared.h"
9*4882a593Smuzhiyun #include "xfs_format.h"
10*4882a593Smuzhiyun #include "xfs_log_format.h"
11*4882a593Smuzhiyun #include "xfs_trans_resv.h"
12*4882a593Smuzhiyun #include "xfs_sb.h"
13*4882a593Smuzhiyun #include "xfs_mount.h"
14*4882a593Smuzhiyun #include "xfs_trans.h"
15*4882a593Smuzhiyun #include "xfs_error.h"
16*4882a593Smuzhiyun #include "xfs_alloc.h"
17*4882a593Smuzhiyun #include "xfs_fsops.h"
18*4882a593Smuzhiyun #include "xfs_trans_space.h"
19*4882a593Smuzhiyun #include "xfs_log.h"
20*4882a593Smuzhiyun #include "xfs_ag.h"
21*4882a593Smuzhiyun #include "xfs_ag_resv.h"
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun /*
24*4882a593Smuzhiyun  * growfs operations
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun static int
xfs_growfs_data_private(xfs_mount_t * mp,xfs_growfs_data_t * in)27*4882a593Smuzhiyun xfs_growfs_data_private(
28*4882a593Smuzhiyun 	xfs_mount_t		*mp,		/* mount point for filesystem */
29*4882a593Smuzhiyun 	xfs_growfs_data_t	*in)		/* growfs data input struct */
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun 	xfs_buf_t		*bp;
32*4882a593Smuzhiyun 	int			error;
33*4882a593Smuzhiyun 	xfs_agnumber_t		nagcount;
34*4882a593Smuzhiyun 	xfs_agnumber_t		nagimax = 0;
35*4882a593Smuzhiyun 	xfs_rfsblock_t		nb, nb_mod;
36*4882a593Smuzhiyun 	xfs_rfsblock_t		new;
37*4882a593Smuzhiyun 	xfs_agnumber_t		oagcount;
38*4882a593Smuzhiyun 	xfs_trans_t		*tp;
39*4882a593Smuzhiyun 	struct aghdr_init_data	id = {};
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	nb = in->newblocks;
42*4882a593Smuzhiyun 	if (nb < mp->m_sb.sb_dblocks)
43*4882a593Smuzhiyun 		return -EINVAL;
44*4882a593Smuzhiyun 	if ((error = xfs_sb_validate_fsb_count(&mp->m_sb, nb)))
45*4882a593Smuzhiyun 		return error;
46*4882a593Smuzhiyun 	error = xfs_buf_read_uncached(mp->m_ddev_targp,
47*4882a593Smuzhiyun 				XFS_FSB_TO_BB(mp, nb) - XFS_FSS_TO_BB(mp, 1),
48*4882a593Smuzhiyun 				XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL);
49*4882a593Smuzhiyun 	if (error)
50*4882a593Smuzhiyun 		return error;
51*4882a593Smuzhiyun 	xfs_buf_relse(bp);
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	new = nb;	/* use new as a temporary here */
54*4882a593Smuzhiyun 	nb_mod = do_div(new, mp->m_sb.sb_agblocks);
55*4882a593Smuzhiyun 	nagcount = new + (nb_mod != 0);
56*4882a593Smuzhiyun 	if (nb_mod && nb_mod < XFS_MIN_AG_BLOCKS) {
57*4882a593Smuzhiyun 		nagcount--;
58*4882a593Smuzhiyun 		nb = (xfs_rfsblock_t)nagcount * mp->m_sb.sb_agblocks;
59*4882a593Smuzhiyun 		if (nb < mp->m_sb.sb_dblocks)
60*4882a593Smuzhiyun 			return -EINVAL;
61*4882a593Smuzhiyun 	}
62*4882a593Smuzhiyun 	new = nb - mp->m_sb.sb_dblocks;
63*4882a593Smuzhiyun 	oagcount = mp->m_sb.sb_agcount;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	/* allocate the new per-ag structures */
66*4882a593Smuzhiyun 	if (nagcount > oagcount) {
67*4882a593Smuzhiyun 		error = xfs_initialize_perag(mp, nagcount, &nagimax);
68*4882a593Smuzhiyun 		if (error)
69*4882a593Smuzhiyun 			return error;
70*4882a593Smuzhiyun 	}
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata,
73*4882a593Smuzhiyun 			XFS_GROWFS_SPACE_RES(mp), 0, XFS_TRANS_RESERVE, &tp);
74*4882a593Smuzhiyun 	if (error)
75*4882a593Smuzhiyun 		return error;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	/*
78*4882a593Smuzhiyun 	 * Write new AG headers to disk. Non-transactional, but need to be
79*4882a593Smuzhiyun 	 * written and completed prior to the growfs transaction being logged.
80*4882a593Smuzhiyun 	 * To do this, we use a delayed write buffer list and wait for
81*4882a593Smuzhiyun 	 * submission and IO completion of the list as a whole. This allows the
82*4882a593Smuzhiyun 	 * IO subsystem to merge all the AG headers in a single AG into a single
83*4882a593Smuzhiyun 	 * IO and hide most of the latency of the IO from us.
84*4882a593Smuzhiyun 	 *
85*4882a593Smuzhiyun 	 * This also means that if we get an error whilst building the buffer
86*4882a593Smuzhiyun 	 * list to write, we can cancel the entire list without having written
87*4882a593Smuzhiyun 	 * anything.
88*4882a593Smuzhiyun 	 */
89*4882a593Smuzhiyun 	INIT_LIST_HEAD(&id.buffer_list);
90*4882a593Smuzhiyun 	for (id.agno = nagcount - 1;
91*4882a593Smuzhiyun 	     id.agno >= oagcount;
92*4882a593Smuzhiyun 	     id.agno--, new -= id.agsize) {
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 		if (id.agno == nagcount - 1)
95*4882a593Smuzhiyun 			id.agsize = nb -
96*4882a593Smuzhiyun 				(id.agno * (xfs_rfsblock_t)mp->m_sb.sb_agblocks);
97*4882a593Smuzhiyun 		else
98*4882a593Smuzhiyun 			id.agsize = mp->m_sb.sb_agblocks;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 		error = xfs_ag_init_headers(mp, &id);
101*4882a593Smuzhiyun 		if (error) {
102*4882a593Smuzhiyun 			xfs_buf_delwri_cancel(&id.buffer_list);
103*4882a593Smuzhiyun 			goto out_trans_cancel;
104*4882a593Smuzhiyun 		}
105*4882a593Smuzhiyun 	}
106*4882a593Smuzhiyun 	error = xfs_buf_delwri_submit(&id.buffer_list);
107*4882a593Smuzhiyun 	if (error)
108*4882a593Smuzhiyun 		goto out_trans_cancel;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	xfs_trans_agblocks_delta(tp, id.nfree);
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	/* If there are new blocks in the old last AG, extend it. */
113*4882a593Smuzhiyun 	if (new) {
114*4882a593Smuzhiyun 		error = xfs_ag_extend_space(mp, tp, &id, new);
115*4882a593Smuzhiyun 		if (error)
116*4882a593Smuzhiyun 			goto out_trans_cancel;
117*4882a593Smuzhiyun 	}
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	/*
120*4882a593Smuzhiyun 	 * Update changed superblock fields transactionally. These are not
121*4882a593Smuzhiyun 	 * seen by the rest of the world until the transaction commit applies
122*4882a593Smuzhiyun 	 * them atomically to the superblock.
123*4882a593Smuzhiyun 	 */
124*4882a593Smuzhiyun 	if (nagcount > oagcount)
125*4882a593Smuzhiyun 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_AGCOUNT, nagcount - oagcount);
126*4882a593Smuzhiyun 	if (nb > mp->m_sb.sb_dblocks)
127*4882a593Smuzhiyun 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_DBLOCKS,
128*4882a593Smuzhiyun 				 nb - mp->m_sb.sb_dblocks);
129*4882a593Smuzhiyun 	if (id.nfree)
130*4882a593Smuzhiyun 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, id.nfree);
131*4882a593Smuzhiyun 	xfs_trans_set_sync(tp);
132*4882a593Smuzhiyun 	error = xfs_trans_commit(tp);
133*4882a593Smuzhiyun 	if (error)
134*4882a593Smuzhiyun 		return error;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	/* New allocation groups fully initialized, so update mount struct */
137*4882a593Smuzhiyun 	if (nagimax)
138*4882a593Smuzhiyun 		mp->m_maxagi = nagimax;
139*4882a593Smuzhiyun 	xfs_set_low_space_thresholds(mp);
140*4882a593Smuzhiyun 	mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	/*
143*4882a593Smuzhiyun 	 * If we expanded the last AG, free the per-AG reservation
144*4882a593Smuzhiyun 	 * so we can reinitialize it with the new size.
145*4882a593Smuzhiyun 	 */
146*4882a593Smuzhiyun 	if (new) {
147*4882a593Smuzhiyun 		struct xfs_perag	*pag;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 		pag = xfs_perag_get(mp, id.agno);
150*4882a593Smuzhiyun 		error = xfs_ag_resv_free(pag);
151*4882a593Smuzhiyun 		xfs_perag_put(pag);
152*4882a593Smuzhiyun 		if (error)
153*4882a593Smuzhiyun 			return error;
154*4882a593Smuzhiyun 	}
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	/*
157*4882a593Smuzhiyun 	 * Reserve AG metadata blocks. ENOSPC here does not mean there was a
158*4882a593Smuzhiyun 	 * growfs failure, just that there still isn't space for new user data
159*4882a593Smuzhiyun 	 * after the grow has been run.
160*4882a593Smuzhiyun 	 */
161*4882a593Smuzhiyun 	error = xfs_fs_reserve_ag_blocks(mp);
162*4882a593Smuzhiyun 	if (error == -ENOSPC)
163*4882a593Smuzhiyun 		error = 0;
164*4882a593Smuzhiyun 	return error;
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun out_trans_cancel:
167*4882a593Smuzhiyun 	xfs_trans_cancel(tp);
168*4882a593Smuzhiyun 	return error;
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun static int
xfs_growfs_log_private(xfs_mount_t * mp,xfs_growfs_log_t * in)172*4882a593Smuzhiyun xfs_growfs_log_private(
173*4882a593Smuzhiyun 	xfs_mount_t		*mp,	/* mount point for filesystem */
174*4882a593Smuzhiyun 	xfs_growfs_log_t	*in)	/* growfs log input struct */
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	xfs_extlen_t		nb;
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	nb = in->newblocks;
179*4882a593Smuzhiyun 	if (nb < XFS_MIN_LOG_BLOCKS || nb < XFS_B_TO_FSB(mp, XFS_MIN_LOG_BYTES))
180*4882a593Smuzhiyun 		return -EINVAL;
181*4882a593Smuzhiyun 	if (nb == mp->m_sb.sb_logblocks &&
182*4882a593Smuzhiyun 	    in->isint == (mp->m_sb.sb_logstart != 0))
183*4882a593Smuzhiyun 		return -EINVAL;
184*4882a593Smuzhiyun 	/*
185*4882a593Smuzhiyun 	 * Moving the log is hard, need new interfaces to sync
186*4882a593Smuzhiyun 	 * the log first, hold off all activity while moving it.
187*4882a593Smuzhiyun 	 * Can have shorter or longer log in the same space,
188*4882a593Smuzhiyun 	 * or transform internal to external log or vice versa.
189*4882a593Smuzhiyun 	 */
190*4882a593Smuzhiyun 	return -ENOSYS;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun static int
xfs_growfs_imaxpct(struct xfs_mount * mp,__u32 imaxpct)194*4882a593Smuzhiyun xfs_growfs_imaxpct(
195*4882a593Smuzhiyun 	struct xfs_mount	*mp,
196*4882a593Smuzhiyun 	__u32			imaxpct)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun 	struct xfs_trans	*tp;
199*4882a593Smuzhiyun 	int			dpct;
200*4882a593Smuzhiyun 	int			error;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	if (imaxpct > 100)
203*4882a593Smuzhiyun 		return -EINVAL;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata,
206*4882a593Smuzhiyun 			XFS_GROWFS_SPACE_RES(mp), 0, XFS_TRANS_RESERVE, &tp);
207*4882a593Smuzhiyun 	if (error)
208*4882a593Smuzhiyun 		return error;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	dpct = imaxpct - mp->m_sb.sb_imax_pct;
211*4882a593Smuzhiyun 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_IMAXPCT, dpct);
212*4882a593Smuzhiyun 	xfs_trans_set_sync(tp);
213*4882a593Smuzhiyun 	return xfs_trans_commit(tp);
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun /*
217*4882a593Smuzhiyun  * protected versions of growfs function acquire and release locks on the mount
218*4882a593Smuzhiyun  * point - exported through ioctls: XFS_IOC_FSGROWFSDATA, XFS_IOC_FSGROWFSLOG,
219*4882a593Smuzhiyun  * XFS_IOC_FSGROWFSRT
220*4882a593Smuzhiyun  */
221*4882a593Smuzhiyun int
xfs_growfs_data(struct xfs_mount * mp,struct xfs_growfs_data * in)222*4882a593Smuzhiyun xfs_growfs_data(
223*4882a593Smuzhiyun 	struct xfs_mount	*mp,
224*4882a593Smuzhiyun 	struct xfs_growfs_data	*in)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun 	int			error = 0;
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	if (!capable(CAP_SYS_ADMIN))
229*4882a593Smuzhiyun 		return -EPERM;
230*4882a593Smuzhiyun 	if (!mutex_trylock(&mp->m_growlock))
231*4882a593Smuzhiyun 		return -EWOULDBLOCK;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	/* update imaxpct separately to the physical grow of the filesystem */
234*4882a593Smuzhiyun 	if (in->imaxpct != mp->m_sb.sb_imax_pct) {
235*4882a593Smuzhiyun 		error = xfs_growfs_imaxpct(mp, in->imaxpct);
236*4882a593Smuzhiyun 		if (error)
237*4882a593Smuzhiyun 			goto out_error;
238*4882a593Smuzhiyun 	}
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	if (in->newblocks != mp->m_sb.sb_dblocks) {
241*4882a593Smuzhiyun 		error = xfs_growfs_data_private(mp, in);
242*4882a593Smuzhiyun 		if (error)
243*4882a593Smuzhiyun 			goto out_error;
244*4882a593Smuzhiyun 	}
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	/* Post growfs calculations needed to reflect new state in operations */
247*4882a593Smuzhiyun 	if (mp->m_sb.sb_imax_pct) {
248*4882a593Smuzhiyun 		uint64_t icount = mp->m_sb.sb_dblocks * mp->m_sb.sb_imax_pct;
249*4882a593Smuzhiyun 		do_div(icount, 100);
250*4882a593Smuzhiyun 		M_IGEO(mp)->maxicount = XFS_FSB_TO_INO(mp, icount);
251*4882a593Smuzhiyun 	} else
252*4882a593Smuzhiyun 		M_IGEO(mp)->maxicount = 0;
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	/* Update secondary superblocks now the physical grow has completed */
255*4882a593Smuzhiyun 	error = xfs_update_secondary_sbs(mp);
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun out_error:
258*4882a593Smuzhiyun 	/*
259*4882a593Smuzhiyun 	 * Increment the generation unconditionally, the error could be from
260*4882a593Smuzhiyun 	 * updating the secondary superblocks, in which case the new size
261*4882a593Smuzhiyun 	 * is live already.
262*4882a593Smuzhiyun 	 */
263*4882a593Smuzhiyun 	mp->m_generation++;
264*4882a593Smuzhiyun 	mutex_unlock(&mp->m_growlock);
265*4882a593Smuzhiyun 	return error;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun int
xfs_growfs_log(xfs_mount_t * mp,xfs_growfs_log_t * in)269*4882a593Smuzhiyun xfs_growfs_log(
270*4882a593Smuzhiyun 	xfs_mount_t		*mp,
271*4882a593Smuzhiyun 	xfs_growfs_log_t	*in)
272*4882a593Smuzhiyun {
273*4882a593Smuzhiyun 	int error;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	if (!capable(CAP_SYS_ADMIN))
276*4882a593Smuzhiyun 		return -EPERM;
277*4882a593Smuzhiyun 	if (!mutex_trylock(&mp->m_growlock))
278*4882a593Smuzhiyun 		return -EWOULDBLOCK;
279*4882a593Smuzhiyun 	error = xfs_growfs_log_private(mp, in);
280*4882a593Smuzhiyun 	mutex_unlock(&mp->m_growlock);
281*4882a593Smuzhiyun 	return error;
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun /*
285*4882a593Smuzhiyun  * exported through ioctl XFS_IOC_FSCOUNTS
286*4882a593Smuzhiyun  */
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun void
xfs_fs_counts(xfs_mount_t * mp,xfs_fsop_counts_t * cnt)289*4882a593Smuzhiyun xfs_fs_counts(
290*4882a593Smuzhiyun 	xfs_mount_t		*mp,
291*4882a593Smuzhiyun 	xfs_fsop_counts_t	*cnt)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	cnt->allocino = percpu_counter_read_positive(&mp->m_icount);
294*4882a593Smuzhiyun 	cnt->freeino = percpu_counter_read_positive(&mp->m_ifree);
295*4882a593Smuzhiyun 	cnt->freedata = percpu_counter_read_positive(&mp->m_fdblocks) -
296*4882a593Smuzhiyun 						mp->m_alloc_set_aside;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	spin_lock(&mp->m_sb_lock);
299*4882a593Smuzhiyun 	cnt->freertx = mp->m_sb.sb_frextents;
300*4882a593Smuzhiyun 	spin_unlock(&mp->m_sb_lock);
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun /*
304*4882a593Smuzhiyun  * exported through ioctl XFS_IOC_SET_RESBLKS & XFS_IOC_GET_RESBLKS
305*4882a593Smuzhiyun  *
306*4882a593Smuzhiyun  * xfs_reserve_blocks is called to set m_resblks
307*4882a593Smuzhiyun  * in the in-core mount table. The number of unused reserved blocks
308*4882a593Smuzhiyun  * is kept in m_resblks_avail.
309*4882a593Smuzhiyun  *
310*4882a593Smuzhiyun  * Reserve the requested number of blocks if available. Otherwise return
311*4882a593Smuzhiyun  * as many as possible to satisfy the request. The actual number
312*4882a593Smuzhiyun  * reserved are returned in outval
313*4882a593Smuzhiyun  *
314*4882a593Smuzhiyun  * A null inval pointer indicates that only the current reserved blocks
315*4882a593Smuzhiyun  * available  should  be returned no settings are changed.
316*4882a593Smuzhiyun  */
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun int
xfs_reserve_blocks(xfs_mount_t * mp,uint64_t * inval,xfs_fsop_resblks_t * outval)319*4882a593Smuzhiyun xfs_reserve_blocks(
320*4882a593Smuzhiyun 	xfs_mount_t             *mp,
321*4882a593Smuzhiyun 	uint64_t              *inval,
322*4882a593Smuzhiyun 	xfs_fsop_resblks_t      *outval)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun 	int64_t			lcounter, delta;
325*4882a593Smuzhiyun 	int64_t			fdblks_delta = 0;
326*4882a593Smuzhiyun 	uint64_t		request;
327*4882a593Smuzhiyun 	int64_t			free;
328*4882a593Smuzhiyun 	int			error = 0;
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	/* If inval is null, report current values and return */
331*4882a593Smuzhiyun 	if (inval == (uint64_t *)NULL) {
332*4882a593Smuzhiyun 		if (!outval)
333*4882a593Smuzhiyun 			return -EINVAL;
334*4882a593Smuzhiyun 		outval->resblks = mp->m_resblks;
335*4882a593Smuzhiyun 		outval->resblks_avail = mp->m_resblks_avail;
336*4882a593Smuzhiyun 		return 0;
337*4882a593Smuzhiyun 	}
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	request = *inval;
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	/*
342*4882a593Smuzhiyun 	 * With per-cpu counters, this becomes an interesting problem. we need
343*4882a593Smuzhiyun 	 * to work out if we are freeing or allocation blocks first, then we can
344*4882a593Smuzhiyun 	 * do the modification as necessary.
345*4882a593Smuzhiyun 	 *
346*4882a593Smuzhiyun 	 * We do this under the m_sb_lock so that if we are near ENOSPC, we will
347*4882a593Smuzhiyun 	 * hold out any changes while we work out what to do. This means that
348*4882a593Smuzhiyun 	 * the amount of free space can change while we do this, so we need to
349*4882a593Smuzhiyun 	 * retry if we end up trying to reserve more space than is available.
350*4882a593Smuzhiyun 	 */
351*4882a593Smuzhiyun 	spin_lock(&mp->m_sb_lock);
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	/*
354*4882a593Smuzhiyun 	 * If our previous reservation was larger than the current value,
355*4882a593Smuzhiyun 	 * then move any unused blocks back to the free pool. Modify the resblks
356*4882a593Smuzhiyun 	 * counters directly since we shouldn't have any problems unreserving
357*4882a593Smuzhiyun 	 * space.
358*4882a593Smuzhiyun 	 */
359*4882a593Smuzhiyun 	if (mp->m_resblks > request) {
360*4882a593Smuzhiyun 		lcounter = mp->m_resblks_avail - request;
361*4882a593Smuzhiyun 		if (lcounter  > 0) {		/* release unused blocks */
362*4882a593Smuzhiyun 			fdblks_delta = lcounter;
363*4882a593Smuzhiyun 			mp->m_resblks_avail -= lcounter;
364*4882a593Smuzhiyun 		}
365*4882a593Smuzhiyun 		mp->m_resblks = request;
366*4882a593Smuzhiyun 		if (fdblks_delta) {
367*4882a593Smuzhiyun 			spin_unlock(&mp->m_sb_lock);
368*4882a593Smuzhiyun 			error = xfs_mod_fdblocks(mp, fdblks_delta, 0);
369*4882a593Smuzhiyun 			spin_lock(&mp->m_sb_lock);
370*4882a593Smuzhiyun 		}
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 		goto out;
373*4882a593Smuzhiyun 	}
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	/*
376*4882a593Smuzhiyun 	 * If the request is larger than the current reservation, reserve the
377*4882a593Smuzhiyun 	 * blocks before we update the reserve counters. Sample m_fdblocks and
378*4882a593Smuzhiyun 	 * perform a partial reservation if the request exceeds free space.
379*4882a593Smuzhiyun 	 *
380*4882a593Smuzhiyun 	 * The code below estimates how many blocks it can request from
381*4882a593Smuzhiyun 	 * fdblocks to stash in the reserve pool.  This is a classic TOCTOU
382*4882a593Smuzhiyun 	 * race since fdblocks updates are not always coordinated via
383*4882a593Smuzhiyun 	 * m_sb_lock.  Set the reserve size even if there's not enough free
384*4882a593Smuzhiyun 	 * space to fill it because mod_fdblocks will refill an undersized
385*4882a593Smuzhiyun 	 * reserve when it can.
386*4882a593Smuzhiyun 	 */
387*4882a593Smuzhiyun 	free = percpu_counter_sum(&mp->m_fdblocks) -
388*4882a593Smuzhiyun 						xfs_fdblocks_unavailable(mp);
389*4882a593Smuzhiyun 	delta = request - mp->m_resblks;
390*4882a593Smuzhiyun 	mp->m_resblks = request;
391*4882a593Smuzhiyun 	if (delta > 0 && free > 0) {
392*4882a593Smuzhiyun 		/*
393*4882a593Smuzhiyun 		 * We'll either succeed in getting space from the free block
394*4882a593Smuzhiyun 		 * count or we'll get an ENOSPC.  Don't set the reserved flag
395*4882a593Smuzhiyun 		 * here - we don't want to reserve the extra reserve blocks
396*4882a593Smuzhiyun 		 * from the reserve.
397*4882a593Smuzhiyun 		 *
398*4882a593Smuzhiyun 		 * The desired reserve size can change after we drop the lock.
399*4882a593Smuzhiyun 		 * Use mod_fdblocks to put the space into the reserve or into
400*4882a593Smuzhiyun 		 * fdblocks as appropriate.
401*4882a593Smuzhiyun 		 */
402*4882a593Smuzhiyun 		fdblks_delta = min(free, delta);
403*4882a593Smuzhiyun 		spin_unlock(&mp->m_sb_lock);
404*4882a593Smuzhiyun 		error = xfs_mod_fdblocks(mp, -fdblks_delta, 0);
405*4882a593Smuzhiyun 		if (!error)
406*4882a593Smuzhiyun 			xfs_mod_fdblocks(mp, fdblks_delta, 0);
407*4882a593Smuzhiyun 		spin_lock(&mp->m_sb_lock);
408*4882a593Smuzhiyun 	}
409*4882a593Smuzhiyun out:
410*4882a593Smuzhiyun 	if (outval) {
411*4882a593Smuzhiyun 		outval->resblks = mp->m_resblks;
412*4882a593Smuzhiyun 		outval->resblks_avail = mp->m_resblks_avail;
413*4882a593Smuzhiyun 	}
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	spin_unlock(&mp->m_sb_lock);
416*4882a593Smuzhiyun 	return error;
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun int
xfs_fs_goingdown(xfs_mount_t * mp,uint32_t inflags)420*4882a593Smuzhiyun xfs_fs_goingdown(
421*4882a593Smuzhiyun 	xfs_mount_t	*mp,
422*4882a593Smuzhiyun 	uint32_t	inflags)
423*4882a593Smuzhiyun {
424*4882a593Smuzhiyun 	switch (inflags) {
425*4882a593Smuzhiyun 	case XFS_FSOP_GOING_FLAGS_DEFAULT: {
426*4882a593Smuzhiyun 		if (!freeze_bdev(mp->m_super->s_bdev)) {
427*4882a593Smuzhiyun 			xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
428*4882a593Smuzhiyun 			thaw_bdev(mp->m_super->s_bdev);
429*4882a593Smuzhiyun 		}
430*4882a593Smuzhiyun 		break;
431*4882a593Smuzhiyun 	}
432*4882a593Smuzhiyun 	case XFS_FSOP_GOING_FLAGS_LOGFLUSH:
433*4882a593Smuzhiyun 		xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
434*4882a593Smuzhiyun 		break;
435*4882a593Smuzhiyun 	case XFS_FSOP_GOING_FLAGS_NOLOGFLUSH:
436*4882a593Smuzhiyun 		xfs_force_shutdown(mp,
437*4882a593Smuzhiyun 				SHUTDOWN_FORCE_UMOUNT | SHUTDOWN_LOG_IO_ERROR);
438*4882a593Smuzhiyun 		break;
439*4882a593Smuzhiyun 	default:
440*4882a593Smuzhiyun 		return -EINVAL;
441*4882a593Smuzhiyun 	}
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 	return 0;
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun /*
447*4882a593Smuzhiyun  * Force a shutdown of the filesystem instantly while keeping the filesystem
448*4882a593Smuzhiyun  * consistent. We don't do an unmount here; just shutdown the shop, make sure
449*4882a593Smuzhiyun  * that absolutely nothing persistent happens to this filesystem after this
450*4882a593Smuzhiyun  * point.
451*4882a593Smuzhiyun  */
452*4882a593Smuzhiyun void
xfs_do_force_shutdown(struct xfs_mount * mp,int flags,char * fname,int lnnum)453*4882a593Smuzhiyun xfs_do_force_shutdown(
454*4882a593Smuzhiyun 	struct xfs_mount *mp,
455*4882a593Smuzhiyun 	int		flags,
456*4882a593Smuzhiyun 	char		*fname,
457*4882a593Smuzhiyun 	int		lnnum)
458*4882a593Smuzhiyun {
459*4882a593Smuzhiyun 	bool		logerror = flags & SHUTDOWN_LOG_IO_ERROR;
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	/*
462*4882a593Smuzhiyun 	 * No need to duplicate efforts.
463*4882a593Smuzhiyun 	 */
464*4882a593Smuzhiyun 	if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
465*4882a593Smuzhiyun 		return;
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 	/*
468*4882a593Smuzhiyun 	 * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
469*4882a593Smuzhiyun 	 * queue up anybody new on the log reservations, and wakes up
470*4882a593Smuzhiyun 	 * everybody who's sleeping on log reservations to tell them
471*4882a593Smuzhiyun 	 * the bad news.
472*4882a593Smuzhiyun 	 */
473*4882a593Smuzhiyun 	if (xfs_log_force_umount(mp, logerror))
474*4882a593Smuzhiyun 		return;
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	if (flags & SHUTDOWN_FORCE_UMOUNT) {
477*4882a593Smuzhiyun 		xfs_alert(mp,
478*4882a593Smuzhiyun "User initiated shutdown received. Shutting down filesystem");
479*4882a593Smuzhiyun 		return;
480*4882a593Smuzhiyun 	}
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	xfs_notice(mp,
483*4882a593Smuzhiyun "%s(0x%x) called from line %d of file %s. Return address = "PTR_FMT,
484*4882a593Smuzhiyun 		__func__, flags, lnnum, fname, __return_address);
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	if (flags & SHUTDOWN_CORRUPT_INCORE) {
487*4882a593Smuzhiyun 		xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_CORRUPT,
488*4882a593Smuzhiyun "Corruption of in-memory data detected.  Shutting down filesystem");
489*4882a593Smuzhiyun 		if (XFS_ERRLEVEL_HIGH <= xfs_error_level)
490*4882a593Smuzhiyun 			xfs_stack_trace();
491*4882a593Smuzhiyun 	} else if (logerror) {
492*4882a593Smuzhiyun 		xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_LOGERROR,
493*4882a593Smuzhiyun 			"Log I/O Error Detected. Shutting down filesystem");
494*4882a593Smuzhiyun 	} else {
495*4882a593Smuzhiyun 		xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_IOERROR,
496*4882a593Smuzhiyun 			"I/O Error Detected. Shutting down filesystem");
497*4882a593Smuzhiyun 	}
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun 	xfs_alert(mp,
500*4882a593Smuzhiyun 		"Please unmount the filesystem and rectify the problem(s)");
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun /*
504*4882a593Smuzhiyun  * Reserve free space for per-AG metadata.
505*4882a593Smuzhiyun  */
506*4882a593Smuzhiyun int
xfs_fs_reserve_ag_blocks(struct xfs_mount * mp)507*4882a593Smuzhiyun xfs_fs_reserve_ag_blocks(
508*4882a593Smuzhiyun 	struct xfs_mount	*mp)
509*4882a593Smuzhiyun {
510*4882a593Smuzhiyun 	xfs_agnumber_t		agno;
511*4882a593Smuzhiyun 	struct xfs_perag	*pag;
512*4882a593Smuzhiyun 	int			error = 0;
513*4882a593Smuzhiyun 	int			err2;
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 	mp->m_finobt_nores = false;
516*4882a593Smuzhiyun 	for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
517*4882a593Smuzhiyun 		pag = xfs_perag_get(mp, agno);
518*4882a593Smuzhiyun 		err2 = xfs_ag_resv_init(pag, NULL);
519*4882a593Smuzhiyun 		xfs_perag_put(pag);
520*4882a593Smuzhiyun 		if (err2 && !error)
521*4882a593Smuzhiyun 			error = err2;
522*4882a593Smuzhiyun 	}
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	if (error && error != -ENOSPC) {
525*4882a593Smuzhiyun 		xfs_warn(mp,
526*4882a593Smuzhiyun 	"Error %d reserving per-AG metadata reserve pool.", error);
527*4882a593Smuzhiyun 		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
528*4882a593Smuzhiyun 	}
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	return error;
531*4882a593Smuzhiyun }
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun /*
534*4882a593Smuzhiyun  * Free space reserved for per-AG metadata.
535*4882a593Smuzhiyun  */
536*4882a593Smuzhiyun int
xfs_fs_unreserve_ag_blocks(struct xfs_mount * mp)537*4882a593Smuzhiyun xfs_fs_unreserve_ag_blocks(
538*4882a593Smuzhiyun 	struct xfs_mount	*mp)
539*4882a593Smuzhiyun {
540*4882a593Smuzhiyun 	xfs_agnumber_t		agno;
541*4882a593Smuzhiyun 	struct xfs_perag	*pag;
542*4882a593Smuzhiyun 	int			error = 0;
543*4882a593Smuzhiyun 	int			err2;
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
546*4882a593Smuzhiyun 		pag = xfs_perag_get(mp, agno);
547*4882a593Smuzhiyun 		err2 = xfs_ag_resv_free(pag);
548*4882a593Smuzhiyun 		xfs_perag_put(pag);
549*4882a593Smuzhiyun 		if (err2 && !error)
550*4882a593Smuzhiyun 			error = err2;
551*4882a593Smuzhiyun 	}
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 	if (error)
554*4882a593Smuzhiyun 		xfs_warn(mp,
555*4882a593Smuzhiyun 	"Error %d freeing per-AG metadata reserve pool.", error);
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	return error;
558*4882a593Smuzhiyun }
559