1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86bool
87xfs_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
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);
115}
116
117
118
119
120
121xfs_extlen_t
122xfs_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
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
147static 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 pag->pag_mount->m_ag_max_usable += resv->ar_asked;
160
161
162
163
164 if (type == XFS_AG_RESV_AGFL)
165 oldresv = resv->ar_orig_reserved;
166 else
167 oldresv = resv->ar_reserved;
168 error = xfs_mod_fdblocks(pag->pag_mount, oldresv, true);
169 resv->ar_reserved = 0;
170 resv->ar_asked = 0;
171
172 if (error)
173 trace_xfs_ag_resv_free_error(pag->pag_mount, pag->pag_agno,
174 error, _RET_IP_);
175 return error;
176}
177
178
179int
180xfs_ag_resv_free(
181 struct xfs_perag *pag)
182{
183 int error;
184 int err2;
185
186 error = __xfs_ag_resv_free(pag, XFS_AG_RESV_AGFL);
187 err2 = __xfs_ag_resv_free(pag, XFS_AG_RESV_METADATA);
188 if (err2 && !error)
189 error = err2;
190 return error;
191}
192
193static int
194__xfs_ag_resv_init(
195 struct xfs_perag *pag,
196 enum xfs_ag_resv_type type,
197 xfs_extlen_t ask,
198 xfs_extlen_t used)
199{
200 struct xfs_mount *mp = pag->pag_mount;
201 struct xfs_ag_resv *resv;
202 int error;
203
204 resv = xfs_perag_resv(pag, type);
205 if (used > ask)
206 ask = used;
207 resv->ar_asked = ask;
208 resv->ar_reserved = resv->ar_orig_reserved = ask - used;
209 mp->m_ag_max_usable -= ask;
210
211 trace_xfs_ag_resv_init(pag, type, ask);
212
213 error = xfs_mod_fdblocks(mp, -(int64_t)resv->ar_reserved, true);
214 if (error)
215 trace_xfs_ag_resv_init_error(pag->pag_mount, pag->pag_agno,
216 error, _RET_IP_);
217
218 return error;
219}
220
221
222int
223xfs_ag_resv_init(
224 struct xfs_perag *pag)
225{
226 xfs_extlen_t ask;
227 xfs_extlen_t used;
228 int error = 0;
229
230
231 if (pag->pag_meta_resv.ar_asked == 0) {
232 ask = used = 0;
233
234 error = xfs_refcountbt_calc_reserves(pag->pag_mount,
235 pag->pag_agno, &ask, &used);
236 if (error)
237 goto out;
238
239 error = __xfs_ag_resv_init(pag, XFS_AG_RESV_METADATA,
240 ask, used);
241 if (error)
242 goto out;
243 }
244
245
246 if (pag->pag_agfl_resv.ar_asked == 0) {
247 ask = used = 0;
248
249 error = xfs_rmapbt_calc_reserves(pag->pag_mount, pag->pag_agno,
250 &ask, &used);
251 if (error)
252 goto out;
253
254 error = __xfs_ag_resv_init(pag, XFS_AG_RESV_AGFL, ask, used);
255 if (error)
256 goto out;
257 }
258
259out:
260 return error;
261}
262
263
264void
265xfs_ag_resv_alloc_extent(
266 struct xfs_perag *pag,
267 enum xfs_ag_resv_type type,
268 struct xfs_alloc_arg *args)
269{
270 struct xfs_ag_resv *resv;
271 xfs_extlen_t len;
272 uint field;
273
274 trace_xfs_ag_resv_alloc_extent(pag, type, args->len);
275
276 switch (type) {
277 case XFS_AG_RESV_METADATA:
278 case XFS_AG_RESV_AGFL:
279 resv = xfs_perag_resv(pag, type);
280 break;
281 default:
282 ASSERT(0);
283
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);
288 return;
289 }
290
291 len = min_t(xfs_extlen_t, args->len, resv->ar_reserved);
292 resv->ar_reserved -= len;
293 if (type == XFS_AG_RESV_AGFL)
294 return;
295
296 xfs_trans_mod_sb(args->tp, XFS_TRANS_SB_RES_FDBLOCKS, -(int64_t)len);
297
298 if (args->len > len)
299 xfs_trans_mod_sb(args->tp, XFS_TRANS_SB_FDBLOCKS,
300 -((int64_t)args->len - len));
301}
302
303
304void
305xfs_ag_resv_free_extent(
306 struct xfs_perag *pag,
307 enum xfs_ag_resv_type type,
308 struct xfs_trans *tp,
309 xfs_extlen_t len)
310{
311 xfs_extlen_t leftover;
312 struct xfs_ag_resv *resv;
313
314 trace_xfs_ag_resv_free_extent(pag, type, len);
315
316 switch (type) {
317 case XFS_AG_RESV_METADATA:
318 case XFS_AG_RESV_AGFL:
319 resv = xfs_perag_resv(pag, type);
320 break;
321 default:
322 ASSERT(0);
323
324 case XFS_AG_RESV_NONE:
325 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (int64_t)len);
326 return;
327 }
328
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)
332 return;
333
334 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FDBLOCKS, len);
335
336 if (len > leftover)
337 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, len - leftover);
338}
339