1
2
3
4
5
6#include "xfs.h"
7#include "xfs_fs.h"
8#include "xfs_shared.h"
9#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
12#include "xfs_mount.h"
13#include "xfs_alloc.h"
14#include "xfs_errortag.h"
15#include "xfs_error.h"
16#include "xfs_trace.h"
17#include "xfs_trans.h"
18#include "xfs_rmap_btree.h"
19#include "xfs_btree.h"
20#include "xfs_refcount_btree.h"
21#include "xfs_ialloc_btree.h"
22#include "xfs_ag.h"
23#include "xfs_ag_resv.h"
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68bool
69xfs_ag_resv_critical(
70 struct xfs_perag *pag,
71 enum xfs_ag_resv_type type)
72{
73 xfs_extlen_t avail;
74 xfs_extlen_t orig;
75
76 switch (type) {
77 case XFS_AG_RESV_METADATA:
78 avail = pag->pagf_freeblks - pag->pag_rmapbt_resv.ar_reserved;
79 orig = pag->pag_meta_resv.ar_asked;
80 break;
81 case XFS_AG_RESV_RMAPBT:
82 avail = pag->pagf_freeblks + pag->pagf_flcount -
83 pag->pag_meta_resv.ar_reserved;
84 orig = pag->pag_rmapbt_resv.ar_asked;
85 break;
86 default:
87 ASSERT(0);
88 return false;
89 }
90
91 trace_xfs_ag_resv_critical(pag, type, avail);
92
93
94 return XFS_TEST_ERROR(avail < orig / 10 ||
95 avail < pag->pag_mount->m_agbtree_maxlevels,
96 pag->pag_mount, XFS_ERRTAG_AG_RESV_CRITICAL);
97}
98
99
100
101
102
103xfs_extlen_t
104xfs_ag_resv_needed(
105 struct xfs_perag *pag,
106 enum xfs_ag_resv_type type)
107{
108 xfs_extlen_t len;
109
110 len = pag->pag_meta_resv.ar_reserved + pag->pag_rmapbt_resv.ar_reserved;
111 switch (type) {
112 case XFS_AG_RESV_METADATA:
113 case XFS_AG_RESV_RMAPBT:
114 len -= xfs_perag_resv(pag, type)->ar_reserved;
115 break;
116 case XFS_AG_RESV_NONE:
117
118 break;
119 default:
120 ASSERT(0);
121 }
122
123 trace_xfs_ag_resv_needed(pag, type, len);
124
125 return len;
126}
127
128
129static int
130__xfs_ag_resv_free(
131 struct xfs_perag *pag,
132 enum xfs_ag_resv_type type)
133{
134 struct xfs_ag_resv *resv;
135 xfs_extlen_t oldresv;
136 int error;
137
138 trace_xfs_ag_resv_free(pag, type, 0);
139
140 resv = xfs_perag_resv(pag, type);
141 if (pag->pag_agno == 0)
142 pag->pag_mount->m_ag_max_usable += resv->ar_asked;
143
144
145
146
147
148 if (type == XFS_AG_RESV_RMAPBT)
149 oldresv = resv->ar_orig_reserved;
150 else
151 oldresv = resv->ar_reserved;
152 error = xfs_mod_fdblocks(pag->pag_mount, oldresv, true);
153 resv->ar_reserved = 0;
154 resv->ar_asked = 0;
155 resv->ar_orig_reserved = 0;
156
157 if (error)
158 trace_xfs_ag_resv_free_error(pag->pag_mount, pag->pag_agno,
159 error, _RET_IP_);
160 return error;
161}
162
163
164int
165xfs_ag_resv_free(
166 struct xfs_perag *pag)
167{
168 int error;
169 int err2;
170
171 error = __xfs_ag_resv_free(pag, XFS_AG_RESV_RMAPBT);
172 err2 = __xfs_ag_resv_free(pag, XFS_AG_RESV_METADATA);
173 if (err2 && !error)
174 error = err2;
175 return error;
176}
177
178static int
179__xfs_ag_resv_init(
180 struct xfs_perag *pag,
181 enum xfs_ag_resv_type type,
182 xfs_extlen_t ask,
183 xfs_extlen_t used)
184{
185 struct xfs_mount *mp = pag->pag_mount;
186 struct xfs_ag_resv *resv;
187 int error;
188 xfs_extlen_t hidden_space;
189
190 if (used > ask)
191 ask = used;
192
193 switch (type) {
194 case XFS_AG_RESV_RMAPBT:
195
196
197
198
199
200
201 hidden_space = ask;
202 break;
203 case XFS_AG_RESV_METADATA:
204
205
206
207
208
209 hidden_space = ask - used;
210 break;
211 default:
212 ASSERT(0);
213 return -EINVAL;
214 }
215
216 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_AG_RESV_FAIL))
217 error = -ENOSPC;
218 else
219 error = xfs_mod_fdblocks(mp, -(int64_t)hidden_space, true);
220 if (error) {
221 trace_xfs_ag_resv_init_error(pag->pag_mount, pag->pag_agno,
222 error, _RET_IP_);
223 xfs_warn(mp,
224"Per-AG reservation for AG %u failed. Filesystem may run out of space.",
225 pag->pag_agno);
226 return error;
227 }
228
229
230
231
232
233
234
235 if (pag->pag_agno == 0)
236 mp->m_ag_max_usable -= ask;
237
238 resv = xfs_perag_resv(pag, type);
239 resv->ar_asked = ask;
240 resv->ar_orig_reserved = hidden_space;
241 resv->ar_reserved = ask - used;
242
243 trace_xfs_ag_resv_init(pag, type, ask);
244 return 0;
245}
246
247
248int
249xfs_ag_resv_init(
250 struct xfs_perag *pag,
251 struct xfs_trans *tp)
252{
253 struct xfs_mount *mp = pag->pag_mount;
254 xfs_extlen_t ask;
255 xfs_extlen_t used;
256 int error = 0, error2;
257 bool has_resv = false;
258
259
260 if (pag->pag_meta_resv.ar_asked == 0) {
261 ask = used = 0;
262
263 error = xfs_refcountbt_calc_reserves(mp, tp, pag, &ask, &used);
264 if (error)
265 goto out;
266
267 error = xfs_finobt_calc_reserves(mp, tp, pag, &ask, &used);
268 if (error)
269 goto out;
270
271 error = __xfs_ag_resv_init(pag, XFS_AG_RESV_METADATA,
272 ask, used);
273 if (error) {
274
275
276
277
278
279
280
281 ask = used = 0;
282
283 mp->m_finobt_nores = true;
284
285 error = xfs_refcountbt_calc_reserves(mp, tp, pag, &ask,
286 &used);
287 if (error)
288 goto out;
289
290 error = __xfs_ag_resv_init(pag, XFS_AG_RESV_METADATA,
291 ask, used);
292 if (error)
293 goto out;
294 }
295 if (ask)
296 has_resv = true;
297 }
298
299
300 if (pag->pag_rmapbt_resv.ar_asked == 0) {
301 ask = used = 0;
302
303 error = xfs_rmapbt_calc_reserves(mp, tp, pag, &ask, &used);
304 if (error)
305 goto out;
306
307 error = __xfs_ag_resv_init(pag, XFS_AG_RESV_RMAPBT, ask, used);
308 if (error)
309 goto out;
310 if (ask)
311 has_resv = true;
312 }
313
314out:
315
316
317
318
319
320
321
322
323
324 if (has_resv) {
325 error2 = xfs_alloc_pagf_init(mp, tp, pag->pag_agno, 0);
326 if (error2)
327 return error2;
328
329
330
331
332
333
334
335
336
337 if (!error &&
338 xfs_perag_resv(pag, XFS_AG_RESV_METADATA)->ar_reserved +
339 xfs_perag_resv(pag, XFS_AG_RESV_RMAPBT)->ar_reserved >
340 pag->pagf_freeblks + pag->pagf_flcount)
341 error = -ENOSPC;
342 }
343
344 return error;
345}
346
347
348void
349xfs_ag_resv_alloc_extent(
350 struct xfs_perag *pag,
351 enum xfs_ag_resv_type type,
352 struct xfs_alloc_arg *args)
353{
354 struct xfs_ag_resv *resv;
355 xfs_extlen_t len;
356 uint field;
357
358 trace_xfs_ag_resv_alloc_extent(pag, type, args->len);
359
360 switch (type) {
361 case XFS_AG_RESV_AGFL:
362 return;
363 case XFS_AG_RESV_METADATA:
364 case XFS_AG_RESV_RMAPBT:
365 resv = xfs_perag_resv(pag, type);
366 break;
367 default:
368 ASSERT(0);
369 fallthrough;
370 case XFS_AG_RESV_NONE:
371 field = args->wasdel ? XFS_TRANS_SB_RES_FDBLOCKS :
372 XFS_TRANS_SB_FDBLOCKS;
373 xfs_trans_mod_sb(args->tp, field, -(int64_t)args->len);
374 return;
375 }
376
377 len = min_t(xfs_extlen_t, args->len, resv->ar_reserved);
378 resv->ar_reserved -= len;
379 if (type == XFS_AG_RESV_RMAPBT)
380 return;
381
382 xfs_trans_mod_sb(args->tp, XFS_TRANS_SB_RES_FDBLOCKS, -(int64_t)len);
383
384 if (args->len > len)
385 xfs_trans_mod_sb(args->tp, XFS_TRANS_SB_FDBLOCKS,
386 -((int64_t)args->len - len));
387}
388
389
390void
391xfs_ag_resv_free_extent(
392 struct xfs_perag *pag,
393 enum xfs_ag_resv_type type,
394 struct xfs_trans *tp,
395 xfs_extlen_t len)
396{
397 xfs_extlen_t leftover;
398 struct xfs_ag_resv *resv;
399
400 trace_xfs_ag_resv_free_extent(pag, type, len);
401
402 switch (type) {
403 case XFS_AG_RESV_AGFL:
404 return;
405 case XFS_AG_RESV_METADATA:
406 case XFS_AG_RESV_RMAPBT:
407 resv = xfs_perag_resv(pag, type);
408 break;
409 default:
410 ASSERT(0);
411 fallthrough;
412 case XFS_AG_RESV_NONE:
413 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (int64_t)len);
414 return;
415 }
416
417 leftover = min_t(xfs_extlen_t, len, resv->ar_asked - resv->ar_reserved);
418 resv->ar_reserved += leftover;
419 if (type == XFS_AG_RESV_RMAPBT)
420 return;
421
422 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FDBLOCKS, len);
423
424 if (len > leftover)
425 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, len - leftover);
426}
427