]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_ag_resv.c
xfs: remove unneeded parameter from XFS_TEST_ERROR
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_ag_resv.c
1 /*
2 * Copyright (C) 2016 Oracle. All Rights Reserved.
3 *
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 #include "libxfs_priv.h"
21 #include "xfs_fs.h"
22 #include "xfs_shared.h"
23 #include "xfs_format.h"
24 #include "xfs_log_format.h"
25 #include "xfs_trans_resv.h"
26 #include "xfs_sb.h"
27 #include "xfs_mount.h"
28 #include "xfs_defer.h"
29 #include "xfs_alloc.h"
30 #include "xfs_trace.h"
31 #include "xfs_cksum.h"
32 #include "xfs_trans.h"
33 #include "xfs_bit.h"
34 #include "xfs_bmap.h"
35 #include "xfs_bmap_btree.h"
36 #include "xfs_ag_resv.h"
37 #include "xfs_trans_space.h"
38 #include "xfs_rmap_btree.h"
39 #include "xfs_btree.h"
40 #include "xfs_refcount_btree.h"
41 #include "xfs_ialloc_btree.h"
42
43 /*
44 * Per-AG Block Reservations
45 *
46 * For some kinds of allocation group metadata structures, it is advantageous
47 * to reserve a small number of blocks in each AG so that future expansions of
48 * that data structure do not encounter ENOSPC because errors during a btree
49 * split cause the filesystem to go offline.
50 *
51 * Prior to the introduction of reflink, this wasn't an issue because the free
52 * space btrees maintain a reserve of space (the AGFL) to handle any expansion
53 * that may be necessary; and allocations of other metadata (inodes, BMBT,
54 * dir/attr) aren't restricted to a single AG. However, with reflink it is
55 * possible to allocate all the space in an AG, have subsequent reflink/CoW
56 * activity expand the refcount btree, and discover that there's no space left
57 * to handle that expansion. Since we can calculate the maximum size of the
58 * refcount btree, we can reserve space for it and avoid ENOSPC.
59 *
60 * Handling per-AG reservations consists of three changes to the allocator's
61 * behavior: First, because these reservations are always needed, we decrease
62 * the ag_max_usable counter to reflect the size of the AG after the reserved
63 * blocks are taken. Second, the reservations must be reflected in the
64 * fdblocks count to maintain proper accounting. Third, each AG must maintain
65 * its own reserved block counter so that we can calculate the amount of space
66 * that must remain free to maintain the reservations. Fourth, the "remaining
67 * reserved blocks" count must be used when calculating the length of the
68 * longest free extent in an AG and to clamp maxlen in the per-AG allocation
69 * functions. In other words, we maintain a virtual allocation via in-core
70 * accounting tricks so that we don't have to clean up after a crash. :)
71 *
72 * Reserved blocks can be managed by passing one of the enum xfs_ag_resv_type
73 * values via struct xfs_alloc_arg or directly to the xfs_free_extent
74 * function. It might seem a little funny to maintain a reservoir of blocks
75 * to feed another reservoir, but the AGFL only holds enough blocks to get
76 * through the next transaction. The per-AG reservation is to ensure (we
77 * hope) that each AG never runs out of blocks. Each data structure wanting
78 * to use the reservation system should update ask/used in xfs_ag_resv_init.
79 */
80
81 /*
82 * Are we critically low on blocks? For now we'll define that as the number
83 * of blocks we can get our hands on being less than 10% of what we reserved
84 * or less than some arbitrary number (maximum btree height).
85 */
86 bool
87 xfs_ag_resv_critical(
88 struct xfs_perag *pag,
89 enum xfs_ag_resv_type type)
90 {
91 xfs_extlen_t avail;
92 xfs_extlen_t orig;
93
94 switch (type) {
95 case XFS_AG_RESV_METADATA:
96 avail = pag->pagf_freeblks - pag->pag_agfl_resv.ar_reserved;
97 orig = pag->pag_meta_resv.ar_asked;
98 break;
99 case XFS_AG_RESV_AGFL:
100 avail = pag->pagf_freeblks + pag->pagf_flcount -
101 pag->pag_meta_resv.ar_reserved;
102 orig = pag->pag_agfl_resv.ar_asked;
103 break;
104 default:
105 ASSERT(0);
106 return false;
107 }
108
109 trace_xfs_ag_resv_critical(pag, type, avail);
110
111 /* Critically low if less than 10% or max btree height remains. */
112 return XFS_TEST_ERROR(avail < orig / 10 || avail < XFS_BTREE_MAXLEVELS,
113 pag->pag_mount, XFS_ERRTAG_AG_RESV_CRITICAL);
114 }
115
116 /*
117 * How many blocks are reserved but not used, and therefore must not be
118 * allocated away?
119 */
120 xfs_extlen_t
121 xfs_ag_resv_needed(
122 struct xfs_perag *pag,
123 enum xfs_ag_resv_type type)
124 {
125 xfs_extlen_t len;
126
127 len = pag->pag_meta_resv.ar_reserved + pag->pag_agfl_resv.ar_reserved;
128 switch (type) {
129 case XFS_AG_RESV_METADATA:
130 case XFS_AG_RESV_AGFL:
131 len -= xfs_perag_resv(pag, type)->ar_reserved;
132 break;
133 case XFS_AG_RESV_NONE:
134 /* empty */
135 break;
136 default:
137 ASSERT(0);
138 }
139
140 trace_xfs_ag_resv_needed(pag, type, len);
141
142 return len;
143 }
144
145 /* Clean out a reservation */
146 static int
147 __xfs_ag_resv_free(
148 struct xfs_perag *pag,
149 enum xfs_ag_resv_type type)
150 {
151 struct xfs_ag_resv *resv;
152 xfs_extlen_t oldresv;
153 int error;
154
155 trace_xfs_ag_resv_free(pag, type, 0);
156
157 resv = xfs_perag_resv(pag, type);
158 pag->pag_mount->m_ag_max_usable += resv->ar_asked;
159 /*
160 * AGFL blocks are always considered "free", so whatever
161 * was reserved at mount time must be given back at umount.
162 */
163 if (type == XFS_AG_RESV_AGFL)
164 oldresv = resv->ar_orig_reserved;
165 else
166 oldresv = resv->ar_reserved;
167 error = xfs_mod_fdblocks(pag->pag_mount, oldresv, true);
168 resv->ar_reserved = 0;
169 resv->ar_asked = 0;
170
171 if (error)
172 trace_xfs_ag_resv_free_error(pag->pag_mount, pag->pag_agno,
173 error, _RET_IP_);
174 return error;
175 }
176
177 /* Free a per-AG reservation. */
178 int
179 xfs_ag_resv_free(
180 struct xfs_perag *pag)
181 {
182 int error;
183 int err2;
184
185 error = __xfs_ag_resv_free(pag, XFS_AG_RESV_AGFL);
186 err2 = __xfs_ag_resv_free(pag, XFS_AG_RESV_METADATA);
187 if (err2 && !error)
188 error = err2;
189 return error;
190 }
191
192 static int
193 __xfs_ag_resv_init(
194 struct xfs_perag *pag,
195 enum xfs_ag_resv_type type,
196 xfs_extlen_t ask,
197 xfs_extlen_t used)
198 {
199 struct xfs_mount *mp = pag->pag_mount;
200 struct xfs_ag_resv *resv;
201 int error;
202 xfs_extlen_t reserved;
203
204 if (used > ask)
205 ask = used;
206 reserved = ask - used;
207
208 error = xfs_mod_fdblocks(mp, -(int64_t)reserved, true);
209 if (error) {
210 trace_xfs_ag_resv_init_error(pag->pag_mount, pag->pag_agno,
211 error, _RET_IP_);
212 xfs_warn(mp,
213 "Per-AG reservation for AG %u failed. Filesystem may run out of space.",
214 pag->pag_agno);
215 return error;
216 }
217
218 mp->m_ag_max_usable -= ask;
219
220 resv = xfs_perag_resv(pag, type);
221 resv->ar_asked = ask;
222 resv->ar_reserved = resv->ar_orig_reserved = reserved;
223
224 trace_xfs_ag_resv_init(pag, type, ask);
225 return 0;
226 }
227
228 /* Create a per-AG block reservation. */
229 int
230 xfs_ag_resv_init(
231 struct xfs_perag *pag)
232 {
233 struct xfs_mount *mp = pag->pag_mount;
234 xfs_agnumber_t agno = pag->pag_agno;
235 xfs_extlen_t ask;
236 xfs_extlen_t used;
237 int error = 0;
238
239 /* Create the metadata reservation. */
240 if (pag->pag_meta_resv.ar_asked == 0) {
241 ask = used = 0;
242
243 error = xfs_refcountbt_calc_reserves(mp, agno, &ask, &used);
244 if (error)
245 goto out;
246
247 error = xfs_finobt_calc_reserves(mp, agno, &ask, &used);
248 if (error)
249 goto out;
250
251 error = __xfs_ag_resv_init(pag, XFS_AG_RESV_METADATA,
252 ask, used);
253 if (error) {
254 /*
255 * Because we didn't have per-AG reservations when the
256 * finobt feature was added we might not be able to
257 * reserve all needed blocks. Warn and fall back to the
258 * old and potentially buggy code in that case, but
259 * ensure we do have the reservation for the refcountbt.
260 */
261 ask = used = 0;
262
263 mp->m_inotbt_nores = true;
264
265 error = xfs_refcountbt_calc_reserves(mp, agno, &ask,
266 &used);
267 if (error)
268 goto out;
269
270 error = __xfs_ag_resv_init(pag, XFS_AG_RESV_METADATA,
271 ask, used);
272 if (error)
273 goto out;
274 }
275 }
276
277 /* Create the AGFL metadata reservation */
278 if (pag->pag_agfl_resv.ar_asked == 0) {
279 ask = used = 0;
280
281 error = xfs_rmapbt_calc_reserves(mp, agno, &ask, &used);
282 if (error)
283 goto out;
284
285 error = __xfs_ag_resv_init(pag, XFS_AG_RESV_AGFL, ask, used);
286 if (error)
287 goto out;
288 }
289
290 #ifdef DEBUG
291 /* need to read in the AGF for the ASSERT below to work */
292 error = xfs_alloc_pagf_init(pag->pag_mount, NULL, pag->pag_agno, 0);
293 if (error)
294 return error;
295
296 ASSERT(xfs_perag_resv(pag, XFS_AG_RESV_METADATA)->ar_reserved +
297 xfs_perag_resv(pag, XFS_AG_RESV_AGFL)->ar_reserved <=
298 pag->pagf_freeblks + pag->pagf_flcount);
299 #endif
300 out:
301 return error;
302 }
303
304 /* Allocate a block from the reservation. */
305 void
306 xfs_ag_resv_alloc_extent(
307 struct xfs_perag *pag,
308 enum xfs_ag_resv_type type,
309 struct xfs_alloc_arg *args)
310 {
311 struct xfs_ag_resv *resv;
312 xfs_extlen_t len;
313 uint field;
314
315 trace_xfs_ag_resv_alloc_extent(pag, type, args->len);
316
317 switch (type) {
318 case XFS_AG_RESV_METADATA:
319 case XFS_AG_RESV_AGFL:
320 resv = xfs_perag_resv(pag, type);
321 break;
322 default:
323 ASSERT(0);
324 /* fall through */
325 case XFS_AG_RESV_NONE:
326 field = args->wasdel ? XFS_TRANS_SB_RES_FDBLOCKS :
327 XFS_TRANS_SB_FDBLOCKS;
328 xfs_trans_mod_sb(args->tp, field, -(int64_t)args->len);
329 return;
330 }
331
332 len = min_t(xfs_extlen_t, args->len, resv->ar_reserved);
333 resv->ar_reserved -= len;
334 if (type == XFS_AG_RESV_AGFL)
335 return;
336 /* Allocations of reserved blocks only need on-disk sb updates... */
337 xfs_trans_mod_sb(args->tp, XFS_TRANS_SB_RES_FDBLOCKS, -(int64_t)len);
338 /* ...but non-reserved blocks need in-core and on-disk updates. */
339 if (args->len > len)
340 xfs_trans_mod_sb(args->tp, XFS_TRANS_SB_FDBLOCKS,
341 -((int64_t)args->len - len));
342 }
343
344 /* Free a block to the reservation. */
345 void
346 xfs_ag_resv_free_extent(
347 struct xfs_perag *pag,
348 enum xfs_ag_resv_type type,
349 struct xfs_trans *tp,
350 xfs_extlen_t len)
351 {
352 xfs_extlen_t leftover;
353 struct xfs_ag_resv *resv;
354
355 trace_xfs_ag_resv_free_extent(pag, type, len);
356
357 switch (type) {
358 case XFS_AG_RESV_METADATA:
359 case XFS_AG_RESV_AGFL:
360 resv = xfs_perag_resv(pag, type);
361 break;
362 default:
363 ASSERT(0);
364 /* fall through */
365 case XFS_AG_RESV_NONE:
366 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (int64_t)len);
367 return;
368 }
369
370 leftover = min_t(xfs_extlen_t, len, resv->ar_asked - resv->ar_reserved);
371 resv->ar_reserved += leftover;
372 if (type == XFS_AG_RESV_AGFL)
373 return;
374 /* Freeing into the reserved pool only requires on-disk update... */
375 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FDBLOCKS, len);
376 /* ...but freeing beyond that requires in-core and on-disk update. */
377 if (len > leftover)
378 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, len - leftover);
379 }