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