blob: 773cb02e73129e874af07e250e59abef120de8f3 [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"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100014#include "xfs_defer.h"
Dave Chinner239880e2013-10-23 10:50:10 +110015#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "xfs_error.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110017#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs_fsops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_trans_space.h"
21#include "xfs_rtalloc.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000022#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110023#include "xfs_log.h"
Dave Chinnerb16817b2018-05-13 23:10:08 -070024#include "xfs_ag.h"
Darrick J. Wong84d69612016-10-03 09:11:44 -070025#include "xfs_ag_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27/*
Dave Chinnerb16817b2018-05-13 23:10:08 -070028 * growfs operations
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static int
31xfs_growfs_data_private(
32 xfs_mount_t *mp, /* mount point for filesystem */
33 xfs_growfs_data_t *in) /* growfs data input struct */
34{
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 xfs_buf_t *bp;
Dave Chinner83a7f862018-05-13 23:10:07 -070036 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 xfs_agnumber_t nagcount;
38 xfs_agnumber_t nagimax = 0;
39 xfs_rfsblock_t nb, nb_mod;
40 xfs_rfsblock_t new;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 xfs_agnumber_t oagcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 xfs_trans_t *tp;
Dave Chinner0410c3b2018-05-13 23:10:06 -070043 struct aghdr_init_data id = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 nb = in->newblocks;
Dave Chinner87444b82018-05-13 23:10:07 -070046 if (nb < mp->m_sb.sb_dblocks)
Dave Chinner24513372014-06-25 14:58:08 +100047 return -EINVAL;
Nathan Scott4cc929e2007-05-14 18:24:02 +100048 if ((error = xfs_sb_validate_fsb_count(&mp->m_sb, nb)))
49 return error;
Dave Chinnerba372672014-10-02 09:05:32 +100050 error = xfs_buf_read_uncached(mp->m_ddev_targp,
Dave Chinner1922c942010-09-22 10:47:20 +100051 XFS_FSB_TO_BB(mp, nb) - XFS_FSS_TO_BB(mp, 1),
Dave Chinnerba372672014-10-02 09:05:32 +100052 XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL);
53 if (error)
Dave Chinnereab4e632012-11-12 22:54:02 +110054 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 xfs_buf_relse(bp);
56
57 new = nb; /* use new as a temporary here */
58 nb_mod = do_div(new, mp->m_sb.sb_agblocks);
59 nagcount = new + (nb_mod != 0);
60 if (nb_mod && nb_mod < XFS_MIN_AG_BLOCKS) {
61 nagcount--;
Eric Sandeene6da7c92009-05-23 14:30:12 -050062 nb = (xfs_rfsblock_t)nagcount * mp->m_sb.sb_agblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (nb < mp->m_sb.sb_dblocks)
Dave Chinner24513372014-06-25 14:58:08 +100064 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 }
66 new = nb - mp->m_sb.sb_dblocks;
67 oagcount = mp->m_sb.sb_agcount;
Dave Chinner1c1c6eb2010-01-11 11:47:44 +000068
69 /* allocate the new per-ag structures */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 if (nagcount > oagcount) {
Dave Chinner1c1c6eb2010-01-11 11:47:44 +000071 error = xfs_initialize_perag(mp, nagcount, &nagimax);
72 if (error)
73 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
Dave Chinner1c1c6eb2010-01-11 11:47:44 +000075
Christoph Hellwig253f4912016-04-06 09:19:55 +100076 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata,
77 XFS_GROWFS_SPACE_RES(mp), 0, XFS_TRANS_RESERVE, &tp);
78 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Dave Chinner1c1c6eb2010-01-11 11:47:44 +000081 /*
Dave Chinner9aebe802018-05-13 23:10:06 -070082 * Write new AG headers to disk. Non-transactional, but need to be
83 * written and completed prior to the growfs transaction being logged.
84 * To do this, we use a delayed write buffer list and wait for
85 * submission and IO completion of the list as a whole. This allows the
86 * IO subsystem to merge all the AG headers in a single AG into a single
87 * IO and hide most of the latency of the IO from us.
88 *
89 * This also means that if we get an error whilst building the buffer
90 * list to write, we can cancel the entire list without having written
91 * anything.
Dave Chinner1c1c6eb2010-01-11 11:47:44 +000092 */
Dave Chinner0410c3b2018-05-13 23:10:06 -070093 INIT_LIST_HEAD(&id.buffer_list);
94 for (id.agno = nagcount - 1;
95 id.agno >= oagcount;
96 id.agno--, new -= id.agsize) {
Dave Chinnerf94c4452013-11-21 15:41:06 +110097
Dave Chinner0410c3b2018-05-13 23:10:06 -070098 if (id.agno == nagcount - 1)
99 id.agsize = nb -
100 (id.agno * (xfs_rfsblock_t)mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 else
Dave Chinner0410c3b2018-05-13 23:10:06 -0700102 id.agsize = mp->m_sb.sb_agblocks;
Darrick J. Wonge70d8292016-08-03 11:36:08 +1000103
Dave Chinnerb16817b2018-05-13 23:10:08 -0700104 error = xfs_ag_init_headers(mp, &id);
Dave Chinner9aebe802018-05-13 23:10:06 -0700105 if (error) {
Dave Chinner0410c3b2018-05-13 23:10:06 -0700106 xfs_buf_delwri_cancel(&id.buffer_list);
Dave Chinner83a7f862018-05-13 23:10:07 -0700107 goto out_trans_cancel;
Dave Chinner9aebe802018-05-13 23:10:06 -0700108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
Dave Chinner0410c3b2018-05-13 23:10:06 -0700110 error = xfs_buf_delwri_submit(&id.buffer_list);
Dave Chinner9aebe802018-05-13 23:10:06 -0700111 if (error)
Dave Chinner83a7f862018-05-13 23:10:07 -0700112 goto out_trans_cancel;
Dave Chinner9aebe802018-05-13 23:10:06 -0700113
Dave Chinner0410c3b2018-05-13 23:10:06 -0700114 xfs_trans_agblocks_delta(tp, id.nfree);
Dave Chinnercce77bc2018-05-13 23:10:05 -0700115
Dave Chinner49dd56f2018-05-13 23:10:08 -0700116 /* If there are new blocks in the old last AG, extend it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (new) {
Dave Chinner49dd56f2018-05-13 23:10:08 -0700118 error = xfs_ag_extend_space(mp, tp, &id, new);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000119 if (error)
Dave Chinner83a7f862018-05-13 23:10:07 -0700120 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000122
123 /*
124 * Update changed superblock fields transactionally. These are not
125 * seen by the rest of the world until the transaction commit applies
126 * them atomically to the superblock.
127 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (nagcount > oagcount)
129 xfs_trans_mod_sb(tp, XFS_TRANS_SB_AGCOUNT, nagcount - oagcount);
130 if (nb > mp->m_sb.sb_dblocks)
131 xfs_trans_mod_sb(tp, XFS_TRANS_SB_DBLOCKS,
132 nb - mp->m_sb.sb_dblocks);
Dave Chinner0410c3b2018-05-13 23:10:06 -0700133 if (id.nfree)
134 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, id.nfree);
Christoph Hellwigf8079b82015-02-05 11:13:21 +1100135 xfs_trans_set_sync(tp);
Christoph Hellwig70393312015-06-04 13:48:08 +1000136 error = xfs_trans_commit(tp);
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000137 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return error;
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 /* New allocation groups fully initialized, so update mount struct */
141 if (nagimax)
142 mp->m_maxagi = nagimax;
Dave Chinner055388a2011-01-04 11:35:03 +1100143 xfs_set_low_space_thresholds(mp);
Darrick J. Wong52548852016-08-03 11:38:24 +1000144 mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000145
Darrick J. Wong20e73b02017-01-03 18:39:33 -0800146 /*
147 * If we expanded the last AG, free the per-AG reservation
148 * so we can reinitialize it with the new size.
149 */
150 if (new) {
151 struct xfs_perag *pag;
152
Dave Chinner0410c3b2018-05-13 23:10:06 -0700153 pag = xfs_perag_get(mp, id.agno);
Darrick J. Wong20e73b02017-01-03 18:39:33 -0800154 error = xfs_ag_resv_free(pag);
155 xfs_perag_put(pag);
156 if (error)
Dave Chinner83a7f862018-05-13 23:10:07 -0700157 return error;
Darrick J. Wong20e73b02017-01-03 18:39:33 -0800158 }
159
Dave Chinner83a7f862018-05-13 23:10:07 -0700160 /*
161 * Reserve AG metadata blocks. ENOSPC here does not mean there was a
162 * growfs failure, just that there still isn't space for new user data
163 * after the grow has been run.
164 */
Darrick J. Wong84d69612016-10-03 09:11:44 -0700165 error = xfs_fs_reserve_ag_blocks(mp);
Dave Chinner83a7f862018-05-13 23:10:07 -0700166 if (error == -ENOSPC)
167 error = 0;
168 return error;
169
170out_trans_cancel:
171 xfs_trans_cancel(tp);
172 return error;
173}
174
175static int
176xfs_growfs_log_private(
177 xfs_mount_t *mp, /* mount point for filesystem */
178 xfs_growfs_log_t *in) /* growfs log input struct */
179{
180 xfs_extlen_t nb;
181
182 nb = in->newblocks;
183 if (nb < XFS_MIN_LOG_BLOCKS || nb < XFS_B_TO_FSB(mp, XFS_MIN_LOG_BYTES))
184 return -EINVAL;
185 if (nb == mp->m_sb.sb_logblocks &&
186 in->isint == (mp->m_sb.sb_logstart != 0))
187 return -EINVAL;
188 /*
189 * Moving the log is hard, need new interfaces to sync
190 * the log first, hold off all activity while moving it.
191 * Can have shorter or longer log in the same space,
192 * or transform internal to external log or vice versa.
193 */
194 return -ENOSYS;
195}
196
197static int
198xfs_growfs_imaxpct(
199 struct xfs_mount *mp,
200 __u32 imaxpct)
201{
202 struct xfs_trans *tp;
203 int dpct;
204 int error;
205
206 if (imaxpct > 100)
207 return -EINVAL;
208
209 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata,
210 XFS_GROWFS_SPACE_RES(mp), 0, XFS_TRANS_RESERVE, &tp);
211 if (error)
212 return error;
213
214 dpct = imaxpct - mp->m_sb.sb_imax_pct;
215 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IMAXPCT, dpct);
216 xfs_trans_set_sync(tp);
217 return xfs_trans_commit(tp);
218}
219
220/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * protected versions of growfs function acquire and release locks on the mount
222 * point - exported through ioctls: XFS_IOC_FSGROWFSDATA, XFS_IOC_FSGROWFSLOG,
223 * XFS_IOC_FSGROWFSRT
224 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225int
226xfs_growfs_data(
Dave Chinner87444b82018-05-13 23:10:07 -0700227 struct xfs_mount *mp,
228 struct xfs_growfs_data *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Dave Chinner87444b82018-05-13 23:10:07 -0700230 int error = 0;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600231
232 if (!capable(CAP_SYS_ADMIN))
Dave Chinner24513372014-06-25 14:58:08 +1000233 return -EPERM;
Christoph Hellwigcc92e7a2007-08-30 17:21:54 +1000234 if (!mutex_trylock(&mp->m_growlock))
Dave Chinner24513372014-06-25 14:58:08 +1000235 return -EWOULDBLOCK;
Dave Chinner87444b82018-05-13 23:10:07 -0700236
237 /* update imaxpct separately to the physical grow of the filesystem */
238 if (in->imaxpct != mp->m_sb.sb_imax_pct) {
239 error = xfs_growfs_imaxpct(mp, in->imaxpct);
240 if (error)
241 goto out_error;
242 }
243
244 if (in->newblocks != mp->m_sb.sb_dblocks) {
245 error = xfs_growfs_data_private(mp, in);
246 if (error)
247 goto out_error;
248 }
249
250 /* Post growfs calculations needed to reflect new state in operations */
251 if (mp->m_sb.sb_imax_pct) {
252 uint64_t icount = mp->m_sb.sb_dblocks * mp->m_sb.sb_imax_pct;
253 do_div(icount, 100);
Darrick J. Wongef325952019-06-05 11:19:34 -0700254 M_IGEO(mp)->maxicount = XFS_FSB_TO_INO(mp, icount);
Dave Chinner87444b82018-05-13 23:10:07 -0700255 } else
Darrick J. Wongef325952019-06-05 11:19:34 -0700256 M_IGEO(mp)->maxicount = 0;
Dave Chinner87444b82018-05-13 23:10:07 -0700257
Dave Chinner83a7f862018-05-13 23:10:07 -0700258 /* Update secondary superblocks now the physical grow has completed */
Dave Chinnerb16817b2018-05-13 23:10:08 -0700259 error = xfs_update_secondary_sbs(mp);
Dave Chinner83a7f862018-05-13 23:10:07 -0700260
Dave Chinner87444b82018-05-13 23:10:07 -0700261out_error:
Christoph Hellwig52785112015-02-16 11:49:23 +1100262 /*
263 * Increment the generation unconditionally, the error could be from
264 * updating the secondary superblocks, in which case the new size
265 * is live already.
266 */
267 mp->m_generation++;
Christoph Hellwigcc92e7a2007-08-30 17:21:54 +1000268 mutex_unlock(&mp->m_growlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return error;
270}
271
272int
273xfs_growfs_log(
274 xfs_mount_t *mp,
275 xfs_growfs_log_t *in)
276{
277 int error;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600278
279 if (!capable(CAP_SYS_ADMIN))
Dave Chinner24513372014-06-25 14:58:08 +1000280 return -EPERM;
Christoph Hellwigcc92e7a2007-08-30 17:21:54 +1000281 if (!mutex_trylock(&mp->m_growlock))
Dave Chinner24513372014-06-25 14:58:08 +1000282 return -EWOULDBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 error = xfs_growfs_log_private(mp, in);
Christoph Hellwigcc92e7a2007-08-30 17:21:54 +1000284 mutex_unlock(&mp->m_growlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return error;
286}
287
288/*
289 * exported through ioctl XFS_IOC_FSCOUNTS
290 */
291
Eric Sandeen91083262019-05-01 20:26:30 -0700292void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293xfs_fs_counts(
294 xfs_mount_t *mp,
295 xfs_fsop_counts_t *cnt)
296{
Dave Chinner501ab322015-02-23 21:19:28 +1100297 cnt->allocino = percpu_counter_read_positive(&mp->m_icount);
Dave Chinnere88b64e2015-02-23 21:19:53 +1100298 cnt->freeino = percpu_counter_read_positive(&mp->m_ifree);
Dave Chinner0d485ad2015-02-23 21:22:03 +1100299 cnt->freedata = percpu_counter_read_positive(&mp->m_fdblocks) -
Darrick J. Wong52548852016-08-03 11:38:24 +1000300 mp->m_alloc_set_aside;
Dave Chinner501ab322015-02-23 21:19:28 +1100301
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000302 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 cnt->freertx = mp->m_sb.sb_frextents;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000304 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307/*
308 * exported through ioctl XFS_IOC_SET_RESBLKS & XFS_IOC_GET_RESBLKS
309 *
310 * xfs_reserve_blocks is called to set m_resblks
311 * in the in-core mount table. The number of unused reserved blocks
Nathan Scottc41564b2006-03-29 08:55:14 +1000312 * is kept in m_resblks_avail.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 *
314 * Reserve the requested number of blocks if available. Otherwise return
315 * as many as possible to satisfy the request. The actual number
316 * reserved are returned in outval
317 *
318 * A null inval pointer indicates that only the current reserved blocks
319 * available should be returned no settings are changed.
320 */
321
322int
323xfs_reserve_blocks(
324 xfs_mount_t *mp,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700325 uint64_t *inval,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 xfs_fsop_resblks_t *outval)
327{
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700328 int64_t lcounter, delta;
329 int64_t fdblks_delta = 0;
330 uint64_t request;
331 int64_t free;
Brian Foster408fd482016-06-21 11:53:28 +1000332 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* If inval is null, report current values and return */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700335 if (inval == (uint64_t *)NULL) {
David Chinner84e1e992007-06-18 16:50:27 +1000336 if (!outval)
Dave Chinner24513372014-06-25 14:58:08 +1000337 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 outval->resblks = mp->m_resblks;
339 outval->resblks_avail = mp->m_resblks_avail;
Jesper Juhl014c2542006-01-15 02:37:08 +0100340 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
343 request = *inval;
David Chinnerdbcabad2007-02-10 18:36:17 +1100344
345 /*
Brian Foster408fd482016-06-21 11:53:28 +1000346 * With per-cpu counters, this becomes an interesting problem. we need
347 * to work out if we are freeing or allocation blocks first, then we can
348 * do the modification as necessary.
David Chinnerdbcabad2007-02-10 18:36:17 +1100349 *
Brian Foster408fd482016-06-21 11:53:28 +1000350 * We do this under the m_sb_lock so that if we are near ENOSPC, we will
351 * hold out any changes while we work out what to do. This means that
352 * the amount of free space can change while we do this, so we need to
353 * retry if we end up trying to reserve more space than is available.
David Chinnerdbcabad2007-02-10 18:36:17 +1100354 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000355 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 /*
358 * If our previous reservation was larger than the current value,
Brian Foster408fd482016-06-21 11:53:28 +1000359 * then move any unused blocks back to the free pool. Modify the resblks
360 * counters directly since we shouldn't have any problems unreserving
361 * space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (mp->m_resblks > request) {
364 lcounter = mp->m_resblks_avail - request;
365 if (lcounter > 0) { /* release unused blocks */
David Chinnerdbcabad2007-02-10 18:36:17 +1100366 fdblks_delta = lcounter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 mp->m_resblks_avail -= lcounter;
368 }
369 mp->m_resblks = request;
Brian Foster408fd482016-06-21 11:53:28 +1000370 if (fdblks_delta) {
371 spin_unlock(&mp->m_sb_lock);
372 error = xfs_mod_fdblocks(mp, fdblks_delta, 0);
373 spin_lock(&mp->m_sb_lock);
374 }
David Chinner4be536d2006-09-07 14:26:50 +1000375
Brian Foster408fd482016-06-21 11:53:28 +1000376 goto out;
377 }
378
379 /*
380 * If the request is larger than the current reservation, reserve the
381 * blocks before we update the reserve counters. Sample m_fdblocks and
382 * perform a partial reservation if the request exceeds free space.
383 */
384 error = -ENOSPC;
385 do {
Dave Chinner0d485ad2015-02-23 21:22:03 +1100386 free = percpu_counter_sum(&mp->m_fdblocks) -
Darrick J. Wong52548852016-08-03 11:38:24 +1000387 mp->m_alloc_set_aside;
Darrick J. Wongaafe12c2018-06-21 23:26:56 -0700388 if (free <= 0)
Brian Foster408fd482016-06-21 11:53:28 +1000389 break;
David Chinnerdbcabad2007-02-10 18:36:17 +1100390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 delta = request - mp->m_resblks;
David Chinner4be536d2006-09-07 14:26:50 +1000392 lcounter = free - delta;
Brian Foster408fd482016-06-21 11:53:28 +1000393 if (lcounter < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /* We can't satisfy the request, just get what we can */
Brian Foster408fd482016-06-21 11:53:28 +1000395 fdblks_delta = free;
396 else
397 fdblks_delta = delta;
398
399 /*
400 * We'll either succeed in getting space from the free block
401 * count or we'll get an ENOSPC. If we get a ENOSPC, it means
402 * things changed while we were calculating fdblks_delta and so
403 * we should try again to see if there is anything left to
404 * reserve.
405 *
406 * Don't set the reserved flag here - we don't want to reserve
407 * the extra reserve blocks from the reserve.....
408 */
409 spin_unlock(&mp->m_sb_lock);
410 error = xfs_mod_fdblocks(mp, -fdblks_delta, 0);
411 spin_lock(&mp->m_sb_lock);
412 } while (error == -ENOSPC);
413
414 /*
415 * Update the reserve counters if blocks have been successfully
416 * allocated.
417 */
418 if (!error && fdblks_delta) {
419 mp->m_resblks += fdblks_delta;
420 mp->m_resblks_avail += fdblks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
Brian Foster408fd482016-06-21 11:53:28 +1000422
David Chinnerdbcabad2007-02-10 18:36:17 +1100423out:
David Chinner84e1e992007-06-18 16:50:27 +1000424 if (outval) {
425 outval->resblks = mp->m_resblks;
426 outval->resblks_avail = mp->m_resblks_avail;
427 }
David Chinnerdbcabad2007-02-10 18:36:17 +1100428
Brian Foster408fd482016-06-21 11:53:28 +1000429 spin_unlock(&mp->m_sb_lock);
430 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433int
434xfs_fs_goingdown(
435 xfs_mount_t *mp,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700436 uint32_t inflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 switch (inflags) {
439 case XFS_FSOP_GOING_FLAGS_DEFAULT: {
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000440 struct super_block *sb = freeze_bdev(mp->m_super->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Christoph Hellwigf33c6792005-11-25 16:41:47 +1100442 if (sb && !IS_ERR(sb)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000443 xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 thaw_bdev(sb->s_bdev, sb);
445 }
Barry Naujok189f4bf2008-05-21 16:58:55 +1000446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 break;
448 }
449 case XFS_FSOP_GOING_FLAGS_LOGFLUSH:
Nathan Scott7d04a332006-06-09 14:58:38 +1000450 xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 break;
452 case XFS_FSOP_GOING_FLAGS_NOLOGFLUSH:
Nathan Scott7d04a332006-06-09 14:58:38 +1000453 xfs_force_shutdown(mp,
454 SHUTDOWN_FORCE_UMOUNT | SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 break;
456 default:
Dave Chinner24513372014-06-25 14:58:08 +1000457 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
460 return 0;
461}
Dave Chinner2af51f32012-04-23 15:59:03 +1000462
463/*
464 * Force a shutdown of the filesystem instantly while keeping the filesystem
465 * consistent. We don't do an unmount here; just shutdown the shop, make sure
466 * that absolutely nothing persistent happens to this filesystem after this
467 * point.
468 */
469void
470xfs_do_force_shutdown(
Dave Chinner56668a52018-10-18 17:20:39 +1100471 struct xfs_mount *mp,
Dave Chinner2af51f32012-04-23 15:59:03 +1000472 int flags,
473 char *fname,
474 int lnnum)
475{
Dave Chinner56668a52018-10-18 17:20:39 +1100476 bool logerror = flags & SHUTDOWN_LOG_IO_ERROR;
Dave Chinner2af51f32012-04-23 15:59:03 +1000477
Dave Chinner2af51f32012-04-23 15:59:03 +1000478 /*
479 * No need to duplicate efforts.
480 */
481 if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
482 return;
483
484 /*
485 * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
486 * queue up anybody new on the log reservations, and wakes up
487 * everybody who's sleeping on log reservations to tell them
488 * the bad news.
489 */
490 if (xfs_log_force_umount(mp, logerror))
491 return;
492
Dave Chinner56668a52018-10-18 17:20:39 +1100493 if (flags & SHUTDOWN_FORCE_UMOUNT) {
494 xfs_alert(mp,
495"User initiated shutdown received. Shutting down filesystem");
496 return;
497 }
498
499 xfs_notice(mp,
500"%s(0x%x) called from line %d of file %s. Return address = "PTR_FMT,
501 __func__, flags, lnnum, fname, __return_address);
502
Dave Chinner2af51f32012-04-23 15:59:03 +1000503 if (flags & SHUTDOWN_CORRUPT_INCORE) {
504 xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_CORRUPT,
Dave Chinner56668a52018-10-18 17:20:39 +1100505"Corruption of in-memory data detected. Shutting down filesystem");
Dave Chinner2af51f32012-04-23 15:59:03 +1000506 if (XFS_ERRLEVEL_HIGH <= xfs_error_level)
507 xfs_stack_trace();
Dave Chinner56668a52018-10-18 17:20:39 +1100508 } else if (logerror) {
509 xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_LOGERROR,
510 "Log I/O Error Detected. Shutting down filesystem");
511 } else if (flags & SHUTDOWN_DEVICE_REQ) {
512 xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_IOERROR,
513 "All device paths lost. Shutting down filesystem");
514 } else if (!(flags & SHUTDOWN_REMOTE_REQ)) {
515 xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_IOERROR,
516 "I/O Error Detected. Shutting down filesystem");
Dave Chinner2af51f32012-04-23 15:59:03 +1000517 }
Dave Chinner56668a52018-10-18 17:20:39 +1100518
519 xfs_alert(mp,
520 "Please unmount the filesystem and rectify the problem(s)");
Dave Chinner2af51f32012-04-23 15:59:03 +1000521}
Darrick J. Wong84d69612016-10-03 09:11:44 -0700522
523/*
524 * Reserve free space for per-AG metadata.
525 */
526int
527xfs_fs_reserve_ag_blocks(
528 struct xfs_mount *mp)
529{
530 xfs_agnumber_t agno;
531 struct xfs_perag *pag;
532 int error = 0;
533 int err2;
534
Darrick J. Wong15a268d2019-02-13 11:46:16 -0800535 mp->m_finobt_nores = false;
Darrick J. Wong84d69612016-10-03 09:11:44 -0700536 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
537 pag = xfs_perag_get(mp, agno);
Darrick J. Wongebcbef32018-07-29 22:37:08 -0700538 err2 = xfs_ag_resv_init(pag, NULL);
Darrick J. Wong84d69612016-10-03 09:11:44 -0700539 xfs_perag_put(pag);
540 if (err2 && !error)
541 error = err2;
542 }
543
544 if (error && error != -ENOSPC) {
545 xfs_warn(mp,
546 "Error %d reserving per-AG metadata reserve pool.", error);
547 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
548 }
549
550 return error;
551}
552
553/*
554 * Free space reserved for per-AG metadata.
555 */
556int
557xfs_fs_unreserve_ag_blocks(
558 struct xfs_mount *mp)
559{
560 xfs_agnumber_t agno;
561 struct xfs_perag *pag;
562 int error = 0;
563 int err2;
564
565 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
566 pag = xfs_perag_get(mp, agno);
567 err2 = xfs_ag_resv_free(pag);
568 xfs_perag_put(pag);
569 if (err2 && !error)
570 error = err2;
571 }
572
573 if (error)
574 xfs_warn(mp,
575 "Error %d freeing per-AG metadata reserve pool.", error);
576
577 return error;
578}