2 * Copyright (C) 2016 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
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.
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.
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.
22 #include "xfs_shared.h"
23 #include "xfs_format.h"
24 #include "xfs_log_format.h"
25 #include "xfs_trans_resv.h"
27 #include "xfs_mount.h"
28 #include "xfs_defer.h"
29 #include "xfs_alloc.h"
30 #include "xfs_error.h"
31 #include "xfs_trace.h"
32 #include "xfs_cksum.h"
33 #include "xfs_trans.h"
36 #include "xfs_bmap_btree.h"
37 #include "xfs_ag_resv.h"
38 #include "xfs_trans_space.h"
39 #include "xfs_rmap_btree.h"
40 #include "xfs_btree.h"
41 #include "xfs_refcount_btree.h"
44 * Per-AG Block Reservations
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.
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.
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. :)
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.
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).
88 struct xfs_perag
*pag
,
89 enum xfs_ag_resv_type 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
;
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
;
109 trace_xfs_ag_resv_critical(pag
, type
, avail
);
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 XFS_RANDOM_AG_RESV_CRITICAL
);
118 * How many blocks are reserved but not used, and therefore must not be
123 struct xfs_perag
*pag
,
124 enum xfs_ag_resv_type type
)
128 len
= pag
->pag_meta_resv
.ar_reserved
+ pag
->pag_agfl_resv
.ar_reserved
;
130 case XFS_AG_RESV_METADATA
:
131 case XFS_AG_RESV_AGFL
:
132 len
-= xfs_perag_resv(pag
, type
)->ar_reserved
;
134 case XFS_AG_RESV_NONE
:
141 trace_xfs_ag_resv_needed(pag
, type
, len
);
146 /* Clean out a reservation */
149 struct xfs_perag
*pag
,
150 enum xfs_ag_resv_type type
)
152 struct xfs_ag_resv
*resv
;
153 xfs_extlen_t oldresv
;
156 trace_xfs_ag_resv_free(pag
, type
, 0);
158 resv
= xfs_perag_resv(pag
, type
);
159 pag
->pag_mount
->m_ag_max_usable
+= resv
->ar_asked
;
161 * AGFL blocks are always considered "free", so whatever
162 * was reserved at mount time must be given back at umount.
164 if (type
== XFS_AG_RESV_AGFL
)
165 oldresv
= resv
->ar_orig_reserved
;
167 oldresv
= resv
->ar_reserved
;
168 error
= xfs_mod_fdblocks(pag
->pag_mount
, oldresv
, true);
169 resv
->ar_reserved
= 0;
173 trace_xfs_ag_resv_free_error(pag
->pag_mount
, pag
->pag_agno
,
178 /* Free a per-AG reservation. */
181 struct xfs_perag
*pag
)
186 error
= __xfs_ag_resv_free(pag
, XFS_AG_RESV_AGFL
);
187 err2
= __xfs_ag_resv_free(pag
, XFS_AG_RESV_METADATA
);
195 struct xfs_perag
*pag
,
196 enum xfs_ag_resv_type type
,
200 struct xfs_mount
*mp
= pag
->pag_mount
;
201 struct xfs_ag_resv
*resv
;
204 resv
= xfs_perag_resv(pag
, type
);
207 resv
->ar_asked
= ask
;
208 resv
->ar_reserved
= resv
->ar_orig_reserved
= ask
- used
;
209 mp
->m_ag_max_usable
-= ask
;
211 trace_xfs_ag_resv_init(pag
, type
, ask
);
213 error
= xfs_mod_fdblocks(mp
, -(int64_t)resv
->ar_reserved
, true);
215 trace_xfs_ag_resv_init_error(pag
->pag_mount
, pag
->pag_agno
,
221 /* Create a per-AG block reservation. */
224 struct xfs_perag
*pag
)
230 /* Create the metadata reservation. */
231 if (pag
->pag_meta_resv
.ar_asked
== 0) {
234 error
= xfs_refcountbt_calc_reserves(pag
->pag_mount
,
235 pag
->pag_agno
, &ask
, &used
);
239 error
= __xfs_ag_resv_init(pag
, XFS_AG_RESV_METADATA
,
245 /* Create the AGFL metadata reservation */
246 if (pag
->pag_agfl_resv
.ar_asked
== 0) {
249 error
= xfs_rmapbt_calc_reserves(pag
->pag_mount
, pag
->pag_agno
,
254 error
= __xfs_ag_resv_init(pag
, XFS_AG_RESV_AGFL
, ask
, used
);
263 /* Allocate a block from the reservation. */
265 xfs_ag_resv_alloc_extent(
266 struct xfs_perag
*pag
,
267 enum xfs_ag_resv_type type
,
268 struct xfs_alloc_arg
*args
)
270 struct xfs_ag_resv
*resv
;
274 trace_xfs_ag_resv_alloc_extent(pag
, type
, args
->len
);
277 case XFS_AG_RESV_METADATA
:
278 case XFS_AG_RESV_AGFL
:
279 resv
= xfs_perag_resv(pag
, type
);
284 case XFS_AG_RESV_NONE
:
285 field
= args
->wasdel
? XFS_TRANS_SB_RES_FDBLOCKS
:
286 XFS_TRANS_SB_FDBLOCKS
;
287 xfs_trans_mod_sb(args
->tp
, field
, -(int64_t)args
->len
);
291 len
= min_t(xfs_extlen_t
, args
->len
, resv
->ar_reserved
);
292 resv
->ar_reserved
-= len
;
293 if (type
== XFS_AG_RESV_AGFL
)
295 /* Allocations of reserved blocks only need on-disk sb updates... */
296 xfs_trans_mod_sb(args
->tp
, XFS_TRANS_SB_RES_FDBLOCKS
, -(int64_t)len
);
297 /* ...but non-reserved blocks need in-core and on-disk updates. */
299 xfs_trans_mod_sb(args
->tp
, XFS_TRANS_SB_FDBLOCKS
,
300 -((int64_t)args
->len
- len
));
303 /* Free a block to the reservation. */
305 xfs_ag_resv_free_extent(
306 struct xfs_perag
*pag
,
307 enum xfs_ag_resv_type type
,
308 struct xfs_trans
*tp
,
311 xfs_extlen_t leftover
;
312 struct xfs_ag_resv
*resv
;
314 trace_xfs_ag_resv_free_extent(pag
, type
, len
);
317 case XFS_AG_RESV_METADATA
:
318 case XFS_AG_RESV_AGFL
:
319 resv
= xfs_perag_resv(pag
, type
);
324 case XFS_AG_RESV_NONE
:
325 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_FDBLOCKS
, (int64_t)len
);
329 leftover
= min_t(xfs_extlen_t
, len
, resv
->ar_asked
- resv
->ar_reserved
);
330 resv
->ar_reserved
+= leftover
;
331 if (type
== XFS_AG_RESV_AGFL
)
333 /* Freeing into the reserved pool only requires on-disk update... */
334 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_RES_FDBLOCKS
, len
);
335 /* ...but freeing beyond that requires in-core and on-disk update. */
337 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_FDBLOCKS
, len
- leftover
);