blob: e6df2ce5937d3af2a189aa8c63dabf4b19fee6d7 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11007#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11008#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +11009#include "xfs_format.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110010#include "xfs_log_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110011#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "xfs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_mount.h"
Dave Chinner239880e2013-10-23 10:50:10 +110014#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "xfs_error.h"
16#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "xfs_fsops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs_trans_space.h"
Dave Chinner239880e2013-10-23 10:50:10 +110019#include "xfs_log.h"
Dave Chinnerb16817b2018-05-13 23:10:08 -070020#include "xfs_ag.h"
Darrick J. Wong84d69612016-10-03 09:11:44 -070021#include "xfs_ag_resv.h"
Darrick J. Wong7f89c832021-08-10 17:00:54 -070022#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/*
Gao Xiangc789c832021-03-23 19:05:38 -070025 * Write new AG headers to disk. Non-transactional, but need to be
26 * written and completed prior to the growfs transaction being logged.
27 * To do this, we use a delayed write buffer list and wait for
28 * submission and IO completion of the list as a whole. This allows the
29 * IO subsystem to merge all the AG headers in a single AG into a single
30 * IO and hide most of the latency of the IO from us.
31 *
32 * This also means that if we get an error whilst building the buffer
33 * list to write, we can cancel the entire list without having written
34 * anything.
35 */
36static int
37xfs_resizefs_init_new_ags(
38 struct xfs_trans *tp,
39 struct aghdr_init_data *id,
40 xfs_agnumber_t oagcount,
41 xfs_agnumber_t nagcount,
42 xfs_rfsblock_t delta,
43 bool *lastag_extended)
44{
45 struct xfs_mount *mp = tp->t_mountp;
46 xfs_rfsblock_t nb = mp->m_sb.sb_dblocks + delta;
47 int error;
48
49 *lastag_extended = false;
50
51 INIT_LIST_HEAD(&id->buffer_list);
52 for (id->agno = nagcount - 1;
53 id->agno >= oagcount;
54 id->agno--, delta -= id->agsize) {
55
56 if (id->agno == nagcount - 1)
57 id->agsize = nb - (id->agno *
58 (xfs_rfsblock_t)mp->m_sb.sb_agblocks);
59 else
60 id->agsize = mp->m_sb.sb_agblocks;
61
62 error = xfs_ag_init_headers(mp, id);
63 if (error) {
64 xfs_buf_delwri_cancel(&id->buffer_list);
65 return error;
66 }
67 }
68
69 error = xfs_buf_delwri_submit(&id->buffer_list);
70 if (error)
71 return error;
72
Gao Xiangc789c832021-03-23 19:05:38 -070073 if (delta) {
74 *lastag_extended = true;
75 error = xfs_ag_extend_space(mp, tp, id, delta);
76 }
77 return error;
78}
79
80/*
Dave Chinnerb16817b2018-05-13 23:10:08 -070081 * growfs operations
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static int
84xfs_growfs_data_private(
Gao Xiang07aabd92021-02-02 18:24:06 -080085 struct xfs_mount *mp, /* mount point for filesystem */
86 struct xfs_growfs_data *in) /* growfs data input struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Dave Chinnere8222612020-12-16 16:07:34 -080088 struct xfs_buf *bp;
Dave Chinner83a7f862018-05-13 23:10:07 -070089 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 xfs_agnumber_t nagcount;
91 xfs_agnumber_t nagimax = 0;
Gao Xiangce5e1062021-02-02 18:24:06 -080092 xfs_rfsblock_t nb, nb_div, nb_mod;
Gao Xiangfb2fc172021-03-23 19:05:39 -070093 int64_t delta;
Gao Xiangc789c832021-03-23 19:05:38 -070094 bool lastag_extended;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 xfs_agnumber_t oagcount;
Gao Xiang07aabd92021-02-02 18:24:06 -080096 struct xfs_trans *tp;
Dave Chinner0410c3b2018-05-13 23:10:06 -070097 struct aghdr_init_data id = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 nb = in->newblocks;
Gao Xiangfb2fc172021-03-23 19:05:39 -0700100 error = xfs_sb_validate_fsb_count(&mp->m_sb, nb);
Dave Chinnerba3726742014-10-02 09:05:32 +1000101 if (error)
Dave Chinnereab4e632012-11-12 22:54:02 +1100102 return error;
Gao Xiangfb2fc172021-03-23 19:05:39 -0700103
104 if (nb > mp->m_sb.sb_dblocks) {
105 error = xfs_buf_read_uncached(mp->m_ddev_targp,
106 XFS_FSB_TO_BB(mp, nb) - XFS_FSS_TO_BB(mp, 1),
107 XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL);
108 if (error)
109 return error;
110 xfs_buf_relse(bp);
111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Gao Xiangce5e1062021-02-02 18:24:06 -0800113 nb_div = nb;
114 nb_mod = do_div(nb_div, mp->m_sb.sb_agblocks);
115 nagcount = nb_div + (nb_mod != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (nb_mod && nb_mod < XFS_MIN_AG_BLOCKS) {
117 nagcount--;
Eric Sandeene6da7c92009-05-23 14:30:12 -0500118 nb = (xfs_rfsblock_t)nagcount * mp->m_sb.sb_agblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
Gao Xiangce5e1062021-02-02 18:24:06 -0800120 delta = nb - mp->m_sb.sb_dblocks;
Gao Xiangfb2fc172021-03-23 19:05:39 -0700121 /*
122 * Reject filesystems with a single AG because they are not
123 * supported, and reject a shrink operation that would cause a
124 * filesystem to become unsupported.
125 */
126 if (delta < 0 && nagcount < 2)
127 return -EINVAL;
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 oagcount = mp->m_sb.sb_agcount;
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000130
131 /* allocate the new per-ag structures */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (nagcount > oagcount) {
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000133 error = xfs_initialize_perag(mp, nagcount, &nagimax);
134 if (error)
135 return error;
Gao Xiangfb2fc172021-03-23 19:05:39 -0700136 } else if (nagcount < oagcount) {
137 /* TODO: shrinking the entire AGs hasn't yet completed */
138 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000140
Christoph Hellwig253f4912016-04-06 09:19:55 +1000141 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata,
Gao Xiangfb2fc172021-03-23 19:05:39 -0700142 (delta > 0 ? XFS_GROWFS_SPACE_RES(mp) : -delta), 0,
143 XFS_TRANS_RESERVE, &tp);
Christoph Hellwig253f4912016-04-06 09:19:55 +1000144 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Gao Xiangfb2fc172021-03-23 19:05:39 -0700147 if (delta > 0) {
148 error = xfs_resizefs_init_new_ags(tp, &id, oagcount, nagcount,
149 delta, &lastag_extended);
150 } else {
151 static struct ratelimit_state shrink_warning = \
152 RATELIMIT_STATE_INIT("shrink_warning", 86400 * HZ, 1);
153 ratelimit_set_flags(&shrink_warning, RATELIMIT_MSG_ON_RELEASE);
154
155 if (__ratelimit(&shrink_warning))
156 xfs_alert(mp,
157 "EXPERIMENTAL online shrink feature in use. Use at your own risk!");
158
159 error = xfs_ag_shrink_space(mp, &tp, nagcount - 1, -delta);
160 }
Dave Chinner9aebe802018-05-13 23:10:06 -0700161 if (error)
Dave Chinner83a7f862018-05-13 23:10:07 -0700162 goto out_trans_cancel;
Dave Chinner9aebe802018-05-13 23:10:06 -0700163
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000164 /*
165 * Update changed superblock fields transactionally. These are not
166 * seen by the rest of the world until the transaction commit applies
167 * them atomically to the superblock.
168 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (nagcount > oagcount)
170 xfs_trans_mod_sb(tp, XFS_TRANS_SB_AGCOUNT, nagcount - oagcount);
Gao Xiangc789c832021-03-23 19:05:38 -0700171 if (delta)
172 xfs_trans_mod_sb(tp, XFS_TRANS_SB_DBLOCKS, delta);
Dave Chinner0410c3b2018-05-13 23:10:06 -0700173 if (id.nfree)
174 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, id.nfree);
Gao Xiang014695c2021-03-23 19:05:37 -0700175
176 /*
177 * Sync sb counters now to reflect the updated values. This is
178 * particularly important for shrink because the write verifier
179 * will fail if sb_fdblocks is ever larger than sb_dblocks.
180 */
Dave Chinner38c26bf2021-08-18 18:46:37 -0700181 if (xfs_has_lazysbcount(mp))
Gao Xiang014695c2021-03-23 19:05:37 -0700182 xfs_log_sb(tp);
183
Christoph Hellwigf8079b82015-02-05 11:13:21 +1100184 xfs_trans_set_sync(tp);
Christoph Hellwig70393312015-06-04 13:48:08 +1000185 error = xfs_trans_commit(tp);
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000186 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return error;
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* New allocation groups fully initialized, so update mount struct */
190 if (nagimax)
191 mp->m_maxagi = nagimax;
Dave Chinner055388a2011-01-04 11:35:03 +1100192 xfs_set_low_space_thresholds(mp);
Darrick J. Wong52548852016-08-03 11:38:24 +1000193 mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000194
Gao Xiangfb2fc172021-03-23 19:05:39 -0700195 if (delta > 0) {
196 /*
197 * If we expanded the last AG, free the per-AG reservation
198 * so we can reinitialize it with the new size.
199 */
200 if (lastag_extended) {
201 struct xfs_perag *pag;
Darrick J. Wong20e73b02017-01-03 18:39:33 -0800202
Gao Xiangfb2fc172021-03-23 19:05:39 -0700203 pag = xfs_perag_get(mp, id.agno);
204 error = xfs_ag_resv_free(pag);
205 xfs_perag_put(pag);
206 if (error)
207 return error;
208 }
209 /*
210 * Reserve AG metadata blocks. ENOSPC here does not mean there
211 * was a growfs failure, just that there still isn't space for
212 * new user data after the grow has been run.
213 */
214 error = xfs_fs_reserve_ag_blocks(mp);
215 if (error == -ENOSPC)
216 error = 0;
Darrick J. Wong20e73b02017-01-03 18:39:33 -0800217 }
Dave Chinner83a7f862018-05-13 23:10:07 -0700218 return error;
219
220out_trans_cancel:
221 xfs_trans_cancel(tp);
222 return error;
223}
224
225static int
226xfs_growfs_log_private(
Gao Xiang07aabd92021-02-02 18:24:06 -0800227 struct xfs_mount *mp, /* mount point for filesystem */
228 struct xfs_growfs_log *in) /* growfs log input struct */
Dave Chinner83a7f862018-05-13 23:10:07 -0700229{
230 xfs_extlen_t nb;
231
232 nb = in->newblocks;
233 if (nb < XFS_MIN_LOG_BLOCKS || nb < XFS_B_TO_FSB(mp, XFS_MIN_LOG_BYTES))
234 return -EINVAL;
235 if (nb == mp->m_sb.sb_logblocks &&
236 in->isint == (mp->m_sb.sb_logstart != 0))
237 return -EINVAL;
238 /*
239 * Moving the log is hard, need new interfaces to sync
240 * the log first, hold off all activity while moving it.
241 * Can have shorter or longer log in the same space,
242 * or transform internal to external log or vice versa.
243 */
244 return -ENOSYS;
245}
246
247static int
248xfs_growfs_imaxpct(
249 struct xfs_mount *mp,
250 __u32 imaxpct)
251{
252 struct xfs_trans *tp;
253 int dpct;
254 int error;
255
256 if (imaxpct > 100)
257 return -EINVAL;
258
259 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata,
260 XFS_GROWFS_SPACE_RES(mp), 0, XFS_TRANS_RESERVE, &tp);
261 if (error)
262 return error;
263
264 dpct = imaxpct - mp->m_sb.sb_imax_pct;
265 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IMAXPCT, dpct);
266 xfs_trans_set_sync(tp);
267 return xfs_trans_commit(tp);
268}
269
270/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 * protected versions of growfs function acquire and release locks on the mount
272 * point - exported through ioctls: XFS_IOC_FSGROWFSDATA, XFS_IOC_FSGROWFSLOG,
273 * XFS_IOC_FSGROWFSRT
274 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275int
276xfs_growfs_data(
Dave Chinner87444b82018-05-13 23:10:07 -0700277 struct xfs_mount *mp,
278 struct xfs_growfs_data *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Dave Chinner87444b82018-05-13 23:10:07 -0700280 int error = 0;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600281
282 if (!capable(CAP_SYS_ADMIN))
Dave Chinner24513372014-06-25 14:58:08 +1000283 return -EPERM;
Christoph Hellwigcc92e7a2007-08-30 17:21:54 +1000284 if (!mutex_trylock(&mp->m_growlock))
Dave Chinner24513372014-06-25 14:58:08 +1000285 return -EWOULDBLOCK;
Dave Chinner87444b82018-05-13 23:10:07 -0700286
287 /* update imaxpct separately to the physical grow of the filesystem */
288 if (in->imaxpct != mp->m_sb.sb_imax_pct) {
289 error = xfs_growfs_imaxpct(mp, in->imaxpct);
290 if (error)
291 goto out_error;
292 }
293
294 if (in->newblocks != mp->m_sb.sb_dblocks) {
295 error = xfs_growfs_data_private(mp, in);
296 if (error)
297 goto out_error;
298 }
299
300 /* Post growfs calculations needed to reflect new state in operations */
301 if (mp->m_sb.sb_imax_pct) {
302 uint64_t icount = mp->m_sb.sb_dblocks * mp->m_sb.sb_imax_pct;
303 do_div(icount, 100);
Darrick J. Wongef325952019-06-05 11:19:34 -0700304 M_IGEO(mp)->maxicount = XFS_FSB_TO_INO(mp, icount);
Dave Chinner87444b82018-05-13 23:10:07 -0700305 } else
Darrick J. Wongef325952019-06-05 11:19:34 -0700306 M_IGEO(mp)->maxicount = 0;
Dave Chinner87444b82018-05-13 23:10:07 -0700307
Dave Chinner83a7f862018-05-13 23:10:07 -0700308 /* Update secondary superblocks now the physical grow has completed */
Dave Chinnerb16817b2018-05-13 23:10:08 -0700309 error = xfs_update_secondary_sbs(mp);
Dave Chinner83a7f862018-05-13 23:10:07 -0700310
Dave Chinner87444b82018-05-13 23:10:07 -0700311out_error:
Christoph Hellwig52785112015-02-16 11:49:23 +1100312 /*
313 * Increment the generation unconditionally, the error could be from
314 * updating the secondary superblocks, in which case the new size
315 * is live already.
316 */
317 mp->m_generation++;
Christoph Hellwigcc92e7a2007-08-30 17:21:54 +1000318 mutex_unlock(&mp->m_growlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return error;
320}
321
322int
323xfs_growfs_log(
324 xfs_mount_t *mp,
Gao Xiang07aabd92021-02-02 18:24:06 -0800325 struct xfs_growfs_log *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 int error;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600328
329 if (!capable(CAP_SYS_ADMIN))
Dave Chinner24513372014-06-25 14:58:08 +1000330 return -EPERM;
Christoph Hellwigcc92e7a2007-08-30 17:21:54 +1000331 if (!mutex_trylock(&mp->m_growlock))
Dave Chinner24513372014-06-25 14:58:08 +1000332 return -EWOULDBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 error = xfs_growfs_log_private(mp, in);
Christoph Hellwigcc92e7a2007-08-30 17:21:54 +1000334 mutex_unlock(&mp->m_growlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return error;
336}
337
338/*
339 * exported through ioctl XFS_IOC_FSCOUNTS
340 */
341
Eric Sandeen91083262019-05-01 20:26:30 -0700342void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343xfs_fs_counts(
344 xfs_mount_t *mp,
345 xfs_fsop_counts_t *cnt)
346{
Dave Chinner501ab322015-02-23 21:19:28 +1100347 cnt->allocino = percpu_counter_read_positive(&mp->m_icount);
Dave Chinnere88b64e2015-02-23 21:19:53 +1100348 cnt->freeino = percpu_counter_read_positive(&mp->m_ifree);
Dave Chinner0d485ad2015-02-23 21:22:03 +1100349 cnt->freedata = percpu_counter_read_positive(&mp->m_fdblocks) -
Darrick J. Wong52548852016-08-03 11:38:24 +1000350 mp->m_alloc_set_aside;
Dave Chinner501ab322015-02-23 21:19:28 +1100351
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000352 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 cnt->freertx = mp->m_sb.sb_frextents;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000354 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357/*
358 * exported through ioctl XFS_IOC_SET_RESBLKS & XFS_IOC_GET_RESBLKS
359 *
360 * xfs_reserve_blocks is called to set m_resblks
361 * in the in-core mount table. The number of unused reserved blocks
Nathan Scottc41564b2006-03-29 08:55:14 +1000362 * is kept in m_resblks_avail.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 *
364 * Reserve the requested number of blocks if available. Otherwise return
365 * as many as possible to satisfy the request. The actual number
366 * reserved are returned in outval
367 *
368 * A null inval pointer indicates that only the current reserved blocks
369 * available should be returned no settings are changed.
370 */
371
372int
373xfs_reserve_blocks(
374 xfs_mount_t *mp,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700375 uint64_t *inval,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 xfs_fsop_resblks_t *outval)
377{
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700378 int64_t lcounter, delta;
379 int64_t fdblks_delta = 0;
380 uint64_t request;
381 int64_t free;
Brian Foster408fd482016-06-21 11:53:28 +1000382 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 /* If inval is null, report current values and return */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700385 if (inval == (uint64_t *)NULL) {
David Chinner84e1e992007-06-18 16:50:27 +1000386 if (!outval)
Dave Chinner24513372014-06-25 14:58:08 +1000387 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 outval->resblks = mp->m_resblks;
389 outval->resblks_avail = mp->m_resblks_avail;
Jesper Juhl014c2542006-01-15 02:37:08 +0100390 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
393 request = *inval;
David Chinnerdbcabad2007-02-10 18:36:17 +1100394
395 /*
Brian Foster408fd482016-06-21 11:53:28 +1000396 * With per-cpu counters, this becomes an interesting problem. we need
397 * to work out if we are freeing or allocation blocks first, then we can
398 * do the modification as necessary.
David Chinnerdbcabad2007-02-10 18:36:17 +1100399 *
Brian Foster408fd482016-06-21 11:53:28 +1000400 * We do this under the m_sb_lock so that if we are near ENOSPC, we will
401 * hold out any changes while we work out what to do. This means that
402 * the amount of free space can change while we do this, so we need to
403 * retry if we end up trying to reserve more space than is available.
David Chinnerdbcabad2007-02-10 18:36:17 +1100404 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000405 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 /*
408 * If our previous reservation was larger than the current value,
Brian Foster408fd482016-06-21 11:53:28 +1000409 * then move any unused blocks back to the free pool. Modify the resblks
410 * counters directly since we shouldn't have any problems unreserving
411 * space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (mp->m_resblks > request) {
414 lcounter = mp->m_resblks_avail - request;
415 if (lcounter > 0) { /* release unused blocks */
David Chinnerdbcabad2007-02-10 18:36:17 +1100416 fdblks_delta = lcounter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 mp->m_resblks_avail -= lcounter;
418 }
419 mp->m_resblks = request;
Brian Foster408fd482016-06-21 11:53:28 +1000420 if (fdblks_delta) {
421 spin_unlock(&mp->m_sb_lock);
422 error = xfs_mod_fdblocks(mp, fdblks_delta, 0);
423 spin_lock(&mp->m_sb_lock);
424 }
David Chinner4be536d2006-09-07 14:26:50 +1000425
Brian Foster408fd482016-06-21 11:53:28 +1000426 goto out;
427 }
428
429 /*
430 * If the request is larger than the current reservation, reserve the
431 * blocks before we update the reserve counters. Sample m_fdblocks and
432 * perform a partial reservation if the request exceeds free space.
433 */
434 error = -ENOSPC;
435 do {
Dave Chinner0d485ad2015-02-23 21:22:03 +1100436 free = percpu_counter_sum(&mp->m_fdblocks) -
Darrick J. Wong52548852016-08-03 11:38:24 +1000437 mp->m_alloc_set_aside;
Darrick J. Wongaafe12c2018-06-21 23:26:56 -0700438 if (free <= 0)
Brian Foster408fd482016-06-21 11:53:28 +1000439 break;
David Chinnerdbcabad2007-02-10 18:36:17 +1100440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 delta = request - mp->m_resblks;
David Chinner4be536d2006-09-07 14:26:50 +1000442 lcounter = free - delta;
Brian Foster408fd482016-06-21 11:53:28 +1000443 if (lcounter < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /* We can't satisfy the request, just get what we can */
Brian Foster408fd482016-06-21 11:53:28 +1000445 fdblks_delta = free;
446 else
447 fdblks_delta = delta;
448
449 /*
450 * We'll either succeed in getting space from the free block
451 * count or we'll get an ENOSPC. If we get a ENOSPC, it means
452 * things changed while we were calculating fdblks_delta and so
453 * we should try again to see if there is anything left to
454 * reserve.
455 *
456 * Don't set the reserved flag here - we don't want to reserve
457 * the extra reserve blocks from the reserve.....
458 */
459 spin_unlock(&mp->m_sb_lock);
460 error = xfs_mod_fdblocks(mp, -fdblks_delta, 0);
461 spin_lock(&mp->m_sb_lock);
462 } while (error == -ENOSPC);
463
464 /*
465 * Update the reserve counters if blocks have been successfully
466 * allocated.
467 */
468 if (!error && fdblks_delta) {
469 mp->m_resblks += fdblks_delta;
470 mp->m_resblks_avail += fdblks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
Brian Foster408fd482016-06-21 11:53:28 +1000472
David Chinnerdbcabad2007-02-10 18:36:17 +1100473out:
David Chinner84e1e992007-06-18 16:50:27 +1000474 if (outval) {
475 outval->resblks = mp->m_resblks;
476 outval->resblks_avail = mp->m_resblks_avail;
477 }
David Chinnerdbcabad2007-02-10 18:36:17 +1100478
Brian Foster408fd482016-06-21 11:53:28 +1000479 spin_unlock(&mp->m_sb_lock);
480 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483int
484xfs_fs_goingdown(
485 xfs_mount_t *mp,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700486 uint32_t inflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 switch (inflags) {
489 case XFS_FSOP_GOING_FLAGS_DEFAULT: {
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100490 if (!freeze_bdev(mp->m_super->s_bdev)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000491 xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100492 thaw_bdev(mp->m_super->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 break;
495 }
496 case XFS_FSOP_GOING_FLAGS_LOGFLUSH:
Nathan Scott7d04a332006-06-09 14:58:38 +1000497 xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 break;
499 case XFS_FSOP_GOING_FLAGS_NOLOGFLUSH:
Nathan Scott7d04a332006-06-09 14:58:38 +1000500 xfs_force_shutdown(mp,
501 SHUTDOWN_FORCE_UMOUNT | SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 break;
503 default:
Dave Chinner24513372014-06-25 14:58:08 +1000504 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506
507 return 0;
508}
Dave Chinner2af51f32012-04-23 15:59:03 +1000509
510/*
511 * Force a shutdown of the filesystem instantly while keeping the filesystem
512 * consistent. We don't do an unmount here; just shutdown the shop, make sure
513 * that absolutely nothing persistent happens to this filesystem after this
514 * point.
Dave Chinnerb36d4652021-08-10 18:00:39 -0700515 *
516 * The shutdown state change is atomic, resulting in the first and only the
517 * first shutdown call processing the shutdown. This means we only shutdown the
518 * log once as it requires, and we don't spam the logs when multiple concurrent
519 * shutdowns race to set the shutdown flags.
Dave Chinner2af51f32012-04-23 15:59:03 +1000520 */
521void
522xfs_do_force_shutdown(
Dave Chinner56668a52018-10-18 17:20:39 +1100523 struct xfs_mount *mp,
Dave Chinner2af51f32012-04-23 15:59:03 +1000524 int flags,
525 char *fname,
526 int lnnum)
527{
Dave Chinnerb36d4652021-08-10 18:00:39 -0700528 int tag;
529 const char *why;
Dave Chinner2af51f32012-04-23 15:59:03 +1000530
Dave Chinnerb36d4652021-08-10 18:00:39 -0700531 spin_lock(&mp->m_sb_lock);
532 if (XFS_FORCED_SHUTDOWN(mp)) {
533 spin_unlock(&mp->m_sb_lock);
Dave Chinner56668a52018-10-18 17:20:39 +1100534 return;
535 }
Dave Chinnerb36d4652021-08-10 18:00:39 -0700536 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
537 if (mp->m_sb_bp)
538 mp->m_sb_bp->b_flags |= XBF_DONE;
539 spin_unlock(&mp->m_sb_lock);
Dave Chinner56668a52018-10-18 17:20:39 +1100540
Dave Chinnerb36d4652021-08-10 18:00:39 -0700541 if (flags & SHUTDOWN_FORCE_UMOUNT)
542 xfs_alert(mp, "User initiated shutdown received.");
543
544 if (xlog_force_shutdown(mp->m_log, flags)) {
545 tag = XFS_PTAG_SHUTDOWN_LOGERROR;
546 why = "Log I/O Error";
547 } else if (flags & SHUTDOWN_CORRUPT_INCORE) {
548 tag = XFS_PTAG_SHUTDOWN_CORRUPT;
549 why = "Corruption of in-memory data";
Brian Foster28d84622020-05-06 13:29:19 -0700550 } else {
Dave Chinnerb36d4652021-08-10 18:00:39 -0700551 tag = XFS_PTAG_SHUTDOWN_IOERROR;
552 why = "Metadata I/O Error";
Dave Chinner2af51f32012-04-23 15:59:03 +1000553 }
Dave Chinner56668a52018-10-18 17:20:39 +1100554
Darrick J. Wong7f89c832021-08-10 17:00:54 -0700555 trace_xfs_force_shutdown(mp, tag, flags, fname, lnnum);
556
Dave Chinnerb36d4652021-08-10 18:00:39 -0700557 xfs_alert_tag(mp, tag,
558"%s (0x%x) detected at %pS (%s:%d). Shutting down filesystem.",
559 why, flags, __return_address, fname, lnnum);
Dave Chinner56668a52018-10-18 17:20:39 +1100560 xfs_alert(mp,
561 "Please unmount the filesystem and rectify the problem(s)");
Dave Chinnerb36d4652021-08-10 18:00:39 -0700562 if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
563 xfs_stack_trace();
Dave Chinner2af51f32012-04-23 15:59:03 +1000564}
Darrick J. Wong84d69612016-10-03 09:11:44 -0700565
566/*
567 * Reserve free space for per-AG metadata.
568 */
569int
570xfs_fs_reserve_ag_blocks(
571 struct xfs_mount *mp)
572{
573 xfs_agnumber_t agno;
574 struct xfs_perag *pag;
575 int error = 0;
576 int err2;
577
Darrick J. Wong15a268d2019-02-13 11:46:16 -0800578 mp->m_finobt_nores = false;
Dave Chinnerf250eed2021-06-02 10:48:24 +1000579 for_each_perag(mp, agno, pag) {
Darrick J. Wongebcbef32018-07-29 22:37:08 -0700580 err2 = xfs_ag_resv_init(pag, NULL);
Darrick J. Wong84d69612016-10-03 09:11:44 -0700581 if (err2 && !error)
582 error = err2;
583 }
584
585 if (error && error != -ENOSPC) {
586 xfs_warn(mp,
587 "Error %d reserving per-AG metadata reserve pool.", error);
588 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
589 }
590
591 return error;
592}
593
594/*
595 * Free space reserved for per-AG metadata.
596 */
597int
598xfs_fs_unreserve_ag_blocks(
599 struct xfs_mount *mp)
600{
601 xfs_agnumber_t agno;
602 struct xfs_perag *pag;
603 int error = 0;
604 int err2;
605
Dave Chinnerf250eed2021-06-02 10:48:24 +1000606 for_each_perag(mp, agno, pag) {
Darrick J. Wong84d69612016-10-03 09:11:44 -0700607 err2 = xfs_ag_resv_free(pag);
Darrick J. Wong84d69612016-10-03 09:11:44 -0700608 if (err2 && !error)
609 error = err2;
610 }
611
612 if (error)
613 xfs_warn(mp,
614 "Error %d freeing per-AG metadata reserve pool.", error);
615
616 return error;
617}