]>
Commit | Line | Data |
---|---|---|
37b3b4d6 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
bc859611 DW |
2 | /* |
3 | * Copyright (C) 2016 Oracle. All Rights Reserved. | |
bc859611 | 4 | * Author: Darrick J. Wong <darrick.wong@oracle.com> |
bc859611 DW |
5 | */ |
6 | #include "libxfs_priv.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_sb.h" | |
13 | #include "xfs_mount.h" | |
14 | #include "xfs_defer.h" | |
15 | #include "xfs_btree.h" | |
16 | #include "xfs_bmap.h" | |
17 | #include "xfs_refcount_btree.h" | |
18 | #include "xfs_alloc.h" | |
56d3fc2b | 19 | #include "xfs_errortag.h" |
bc859611 DW |
20 | #include "xfs_trace.h" |
21 | #include "xfs_cksum.h" | |
22 | #include "xfs_trans.h" | |
23 | #include "xfs_bit.h" | |
24 | #include "xfs_refcount.h" | |
10e65503 | 25 | #include "xfs_rmap.h" |
bc859611 | 26 | |
56ef8c33 DW |
27 | /* Allowable refcount adjustment amounts. */ |
28 | enum xfs_refc_adjust_op { | |
29 | XFS_REFCOUNT_ADJUST_INCREASE = 1, | |
30 | XFS_REFCOUNT_ADJUST_DECREASE = -1, | |
10e65503 DW |
31 | XFS_REFCOUNT_ADJUST_COW_ALLOC = 0, |
32 | XFS_REFCOUNT_ADJUST_COW_FREE = -1, | |
56ef8c33 DW |
33 | }; |
34 | ||
10e65503 | 35 | STATIC int __xfs_refcount_cow_alloc(struct xfs_btree_cur *rcur, |
21375e5d | 36 | xfs_agblock_t agbno, xfs_extlen_t aglen); |
10e65503 | 37 | STATIC int __xfs_refcount_cow_free(struct xfs_btree_cur *rcur, |
21375e5d | 38 | xfs_agblock_t agbno, xfs_extlen_t aglen); |
10e65503 | 39 | |
bc859611 DW |
40 | /* |
41 | * Look up the first record less than or equal to [bno, len] in the btree | |
42 | * given by cur. | |
43 | */ | |
44 | int | |
45 | xfs_refcount_lookup_le( | |
46 | struct xfs_btree_cur *cur, | |
47 | xfs_agblock_t bno, | |
48 | int *stat) | |
49 | { | |
50 | trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_private.a.agno, bno, | |
51 | XFS_LOOKUP_LE); | |
52 | cur->bc_rec.rc.rc_startblock = bno; | |
53 | cur->bc_rec.rc.rc_blockcount = 0; | |
54 | return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat); | |
55 | } | |
56 | ||
57 | /* | |
58 | * Look up the first record greater than or equal to [bno, len] in the btree | |
59 | * given by cur. | |
60 | */ | |
61 | int | |
62 | xfs_refcount_lookup_ge( | |
63 | struct xfs_btree_cur *cur, | |
64 | xfs_agblock_t bno, | |
65 | int *stat) | |
66 | { | |
67 | trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_private.a.agno, bno, | |
68 | XFS_LOOKUP_GE); | |
69 | cur->bc_rec.rc.rc_startblock = bno; | |
70 | cur->bc_rec.rc.rc_blockcount = 0; | |
71 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); | |
72 | } | |
73 | ||
43e73fb0 DW |
74 | /* |
75 | * Look up the first record equal to [bno, len] in the btree | |
76 | * given by cur. | |
77 | */ | |
78 | int | |
79 | xfs_refcount_lookup_eq( | |
80 | struct xfs_btree_cur *cur, | |
81 | xfs_agblock_t bno, | |
82 | int *stat) | |
83 | { | |
84 | trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_private.a.agno, bno, | |
85 | XFS_LOOKUP_LE); | |
86 | cur->bc_rec.rc.rc_startblock = bno; | |
87 | cur->bc_rec.rc.rc_blockcount = 0; | |
88 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); | |
89 | } | |
90 | ||
10e65503 | 91 | /* Convert on-disk record to in-core format. */ |
aa465192 | 92 | void |
10e65503 DW |
93 | xfs_refcount_btrec_to_irec( |
94 | union xfs_btree_rec *rec, | |
95 | struct xfs_refcount_irec *irec) | |
96 | { | |
97 | irec->rc_startblock = be32_to_cpu(rec->refc.rc_startblock); | |
98 | irec->rc_blockcount = be32_to_cpu(rec->refc.rc_blockcount); | |
99 | irec->rc_refcount = be32_to_cpu(rec->refc.rc_refcount); | |
100 | } | |
101 | ||
bc859611 DW |
102 | /* |
103 | * Get the data from the pointed-to record. | |
104 | */ | |
105 | int | |
106 | xfs_refcount_get_rec( | |
107 | struct xfs_btree_cur *cur, | |
108 | struct xfs_refcount_irec *irec, | |
109 | int *stat) | |
110 | { | |
ec291989 DC |
111 | struct xfs_mount *mp = cur->bc_mp; |
112 | xfs_agnumber_t agno = cur->bc_private.a.agno; | |
10e65503 DW |
113 | union xfs_btree_rec *rec; |
114 | int error; | |
ec291989 | 115 | xfs_agblock_t realstart; |
bc859611 DW |
116 | |
117 | error = xfs_btree_get_rec(cur, &rec, stat); | |
ec291989 DC |
118 | if (error || !*stat) |
119 | return error; | |
120 | ||
121 | xfs_refcount_btrec_to_irec(rec, irec); | |
122 | ||
123 | agno = cur->bc_private.a.agno; | |
124 | if (irec->rc_blockcount == 0 || irec->rc_blockcount > MAXREFCEXTLEN) | |
125 | goto out_bad_rec; | |
126 | ||
127 | /* handle special COW-staging state */ | |
128 | realstart = irec->rc_startblock; | |
129 | if (realstart & XFS_REFC_COW_START) { | |
130 | if (irec->rc_refcount != 1) | |
131 | goto out_bad_rec; | |
132 | realstart &= ~XFS_REFC_COW_START; | |
133 | } else if (irec->rc_refcount < 2) { | |
134 | goto out_bad_rec; | |
bc859611 | 135 | } |
ec291989 DC |
136 | |
137 | /* check for valid extent range, including overflow */ | |
138 | if (!xfs_verify_agbno(mp, agno, realstart)) | |
139 | goto out_bad_rec; | |
140 | if (realstart > realstart + irec->rc_blockcount) | |
141 | goto out_bad_rec; | |
142 | if (!xfs_verify_agbno(mp, agno, realstart + irec->rc_blockcount - 1)) | |
143 | goto out_bad_rec; | |
144 | ||
145 | if (irec->rc_refcount == 0 || irec->rc_refcount > MAXREFCOUNT) | |
146 | goto out_bad_rec; | |
147 | ||
148 | trace_xfs_refcount_get(cur->bc_mp, cur->bc_private.a.agno, irec); | |
149 | return 0; | |
150 | ||
151 | out_bad_rec: | |
152 | xfs_warn(mp, | |
153 | "Refcount BTree record corruption in AG %d detected!", agno); | |
154 | xfs_warn(mp, | |
155 | "Start block 0x%x, block count 0x%x, references 0x%x", | |
156 | irec->rc_startblock, irec->rc_blockcount, irec->rc_refcount); | |
157 | return -EFSCORRUPTED; | |
bc859611 DW |
158 | } |
159 | ||
160 | /* | |
161 | * Update the record referred to by cur to the value given | |
162 | * by [bno, len, refcount]. | |
163 | * This either works (return 0) or gets an EFSCORRUPTED error. | |
164 | */ | |
165 | STATIC int | |
166 | xfs_refcount_update( | |
167 | struct xfs_btree_cur *cur, | |
168 | struct xfs_refcount_irec *irec) | |
169 | { | |
170 | union xfs_btree_rec rec; | |
171 | int error; | |
172 | ||
173 | trace_xfs_refcount_update(cur->bc_mp, cur->bc_private.a.agno, irec); | |
174 | rec.refc.rc_startblock = cpu_to_be32(irec->rc_startblock); | |
175 | rec.refc.rc_blockcount = cpu_to_be32(irec->rc_blockcount); | |
176 | rec.refc.rc_refcount = cpu_to_be32(irec->rc_refcount); | |
177 | error = xfs_btree_update(cur, &rec); | |
178 | if (error) | |
179 | trace_xfs_refcount_update_error(cur->bc_mp, | |
180 | cur->bc_private.a.agno, error, _RET_IP_); | |
181 | return error; | |
182 | } | |
183 | ||
184 | /* | |
185 | * Insert the record referred to by cur to the value given | |
186 | * by [bno, len, refcount]. | |
187 | * This either works (return 0) or gets an EFSCORRUPTED error. | |
188 | */ | |
aa465192 | 189 | int |
bc859611 DW |
190 | xfs_refcount_insert( |
191 | struct xfs_btree_cur *cur, | |
192 | struct xfs_refcount_irec *irec, | |
193 | int *i) | |
194 | { | |
195 | int error; | |
196 | ||
197 | trace_xfs_refcount_insert(cur->bc_mp, cur->bc_private.a.agno, irec); | |
198 | cur->bc_rec.rc.rc_startblock = irec->rc_startblock; | |
199 | cur->bc_rec.rc.rc_blockcount = irec->rc_blockcount; | |
200 | cur->bc_rec.rc.rc_refcount = irec->rc_refcount; | |
201 | error = xfs_btree_insert(cur, i); | |
02afaae0 DC |
202 | if (error) |
203 | goto out_error; | |
bc859611 | 204 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, *i == 1, out_error); |
02afaae0 | 205 | |
bc859611 DW |
206 | out_error: |
207 | if (error) | |
208 | trace_xfs_refcount_insert_error(cur->bc_mp, | |
209 | cur->bc_private.a.agno, error, _RET_IP_); | |
210 | return error; | |
211 | } | |
212 | ||
213 | /* | |
214 | * Remove the record referred to by cur, then set the pointer to the spot | |
215 | * where the record could be re-inserted, in case we want to increment or | |
216 | * decrement the cursor. | |
217 | * This either works (return 0) or gets an EFSCORRUPTED error. | |
218 | */ | |
219 | STATIC int | |
220 | xfs_refcount_delete( | |
221 | struct xfs_btree_cur *cur, | |
222 | int *i) | |
223 | { | |
224 | struct xfs_refcount_irec irec; | |
225 | int found_rec; | |
226 | int error; | |
227 | ||
228 | error = xfs_refcount_get_rec(cur, &irec, &found_rec); | |
229 | if (error) | |
230 | goto out_error; | |
231 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error); | |
232 | trace_xfs_refcount_delete(cur->bc_mp, cur->bc_private.a.agno, &irec); | |
233 | error = xfs_btree_delete(cur, i); | |
234 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, *i == 1, out_error); | |
235 | if (error) | |
236 | goto out_error; | |
237 | error = xfs_refcount_lookup_ge(cur, irec.rc_startblock, &found_rec); | |
238 | out_error: | |
239 | if (error) | |
240 | trace_xfs_refcount_delete_error(cur->bc_mp, | |
241 | cur->bc_private.a.agno, error, _RET_IP_); | |
242 | return error; | |
243 | } | |
56ef8c33 DW |
244 | |
245 | /* | |
246 | * Adjusting the Reference Count | |
247 | * | |
248 | * As stated elsewhere, the reference count btree (refcbt) stores | |
249 | * >1 reference counts for extents of physical blocks. In this | |
250 | * operation, we're either raising or lowering the reference count of | |
251 | * some subrange stored in the tree: | |
252 | * | |
253 | * <------ adjustment range ------> | |
254 | * ----+ +---+-----+ +--+--------+--------- | |
255 | * 2 | | 3 | 4 | |17| 55 | 10 | |
256 | * ----+ +---+-----+ +--+--------+--------- | |
257 | * X axis is physical blocks number; | |
258 | * reference counts are the numbers inside the rectangles | |
259 | * | |
260 | * The first thing we need to do is to ensure that there are no | |
261 | * refcount extents crossing either boundary of the range to be | |
262 | * adjusted. For any extent that does cross a boundary, split it into | |
263 | * two extents so that we can increment the refcount of one of the | |
264 | * pieces later: | |
265 | * | |
266 | * <------ adjustment range ------> | |
267 | * ----+ +---+-----+ +--+--------+----+---- | |
268 | * 2 | | 3 | 2 | |17| 55 | 10 | 10 | |
269 | * ----+ +---+-----+ +--+--------+----+---- | |
270 | * | |
271 | * For this next step, let's assume that all the physical blocks in | |
272 | * the adjustment range are mapped to a file and are therefore in use | |
273 | * at least once. Therefore, we can infer that any gap in the | |
274 | * refcount tree within the adjustment range represents a physical | |
275 | * extent with refcount == 1: | |
276 | * | |
277 | * <------ adjustment range ------> | |
278 | * ----+---+---+-----+-+--+--------+----+---- | |
279 | * 2 |"1"| 3 | 2 |1|17| 55 | 10 | 10 | |
280 | * ----+---+---+-----+-+--+--------+----+---- | |
281 | * ^ | |
282 | * | |
283 | * For each extent that falls within the interval range, figure out | |
284 | * which extent is to the left or the right of that extent. Now we | |
285 | * have a left, current, and right extent. If the new reference count | |
286 | * of the center extent enables us to merge left, center, and right | |
287 | * into one record covering all three, do so. If the center extent is | |
288 | * at the left end of the range, abuts the left extent, and its new | |
289 | * reference count matches the left extent's record, then merge them. | |
290 | * If the center extent is at the right end of the range, abuts the | |
291 | * right extent, and the reference counts match, merge those. In the | |
292 | * example, we can left merge (assuming an increment operation): | |
293 | * | |
294 | * <------ adjustment range ------> | |
295 | * --------+---+-----+-+--+--------+----+---- | |
296 | * 2 | 3 | 2 |1|17| 55 | 10 | 10 | |
297 | * --------+---+-----+-+--+--------+----+---- | |
298 | * ^ | |
299 | * | |
300 | * For all other extents within the range, adjust the reference count | |
301 | * or delete it if the refcount falls below 2. If we were | |
302 | * incrementing, the end result looks like this: | |
303 | * | |
304 | * <------ adjustment range ------> | |
305 | * --------+---+-----+-+--+--------+----+---- | |
306 | * 2 | 4 | 3 |2|18| 56 | 11 | 10 | |
307 | * --------+---+-----+-+--+--------+----+---- | |
308 | * | |
309 | * The result of a decrement operation looks as such: | |
310 | * | |
311 | * <------ adjustment range ------> | |
312 | * ----+ +---+ +--+--------+----+---- | |
313 | * 2 | | 2 | |16| 54 | 9 | 10 | |
314 | * ----+ +---+ +--+--------+----+---- | |
315 | * DDDD 111111DD | |
316 | * | |
317 | * The blocks marked "D" are freed; the blocks marked "1" are only | |
318 | * referenced once and therefore the record is removed from the | |
319 | * refcount btree. | |
320 | */ | |
321 | ||
322 | /* Next block after this extent. */ | |
323 | static inline xfs_agblock_t | |
324 | xfs_refc_next( | |
325 | struct xfs_refcount_irec *rc) | |
326 | { | |
327 | return rc->rc_startblock + rc->rc_blockcount; | |
328 | } | |
329 | ||
330 | /* | |
331 | * Split a refcount extent that crosses agbno. | |
332 | */ | |
333 | STATIC int | |
334 | xfs_refcount_split_extent( | |
335 | struct xfs_btree_cur *cur, | |
336 | xfs_agblock_t agbno, | |
337 | bool *shape_changed) | |
338 | { | |
339 | struct xfs_refcount_irec rcext, tmp; | |
340 | int found_rec; | |
341 | int error; | |
342 | ||
343 | *shape_changed = false; | |
344 | error = xfs_refcount_lookup_le(cur, agbno, &found_rec); | |
345 | if (error) | |
346 | goto out_error; | |
347 | if (!found_rec) | |
348 | return 0; | |
349 | ||
350 | error = xfs_refcount_get_rec(cur, &rcext, &found_rec); | |
351 | if (error) | |
352 | goto out_error; | |
353 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error); | |
354 | if (rcext.rc_startblock == agbno || xfs_refc_next(&rcext) <= agbno) | |
355 | return 0; | |
356 | ||
357 | *shape_changed = true; | |
358 | trace_xfs_refcount_split_extent(cur->bc_mp, cur->bc_private.a.agno, | |
359 | &rcext, agbno); | |
360 | ||
361 | /* Establish the right extent. */ | |
362 | tmp = rcext; | |
363 | tmp.rc_startblock = agbno; | |
364 | tmp.rc_blockcount -= (agbno - rcext.rc_startblock); | |
365 | error = xfs_refcount_update(cur, &tmp); | |
366 | if (error) | |
367 | goto out_error; | |
368 | ||
369 | /* Insert the left extent. */ | |
370 | tmp = rcext; | |
371 | tmp.rc_blockcount = agbno - rcext.rc_startblock; | |
372 | error = xfs_refcount_insert(cur, &tmp, &found_rec); | |
373 | if (error) | |
374 | goto out_error; | |
375 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error); | |
376 | return error; | |
377 | ||
378 | out_error: | |
379 | trace_xfs_refcount_split_extent_error(cur->bc_mp, | |
380 | cur->bc_private.a.agno, error, _RET_IP_); | |
381 | return error; | |
382 | } | |
383 | ||
384 | /* | |
385 | * Merge the left, center, and right extents. | |
386 | */ | |
387 | STATIC int | |
388 | xfs_refcount_merge_center_extents( | |
389 | struct xfs_btree_cur *cur, | |
390 | struct xfs_refcount_irec *left, | |
391 | struct xfs_refcount_irec *center, | |
392 | struct xfs_refcount_irec *right, | |
393 | unsigned long long extlen, | |
56ef8c33 DW |
394 | xfs_extlen_t *aglen) |
395 | { | |
396 | int error; | |
397 | int found_rec; | |
398 | ||
399 | trace_xfs_refcount_merge_center_extents(cur->bc_mp, | |
400 | cur->bc_private.a.agno, left, center, right); | |
401 | ||
402 | /* | |
403 | * Make sure the center and right extents are not in the btree. | |
404 | * If the center extent was synthesized, the first delete call | |
405 | * removes the right extent and we skip the second deletion. | |
406 | * If center and right were in the btree, then the first delete | |
407 | * call removes the center and the second one removes the right | |
408 | * extent. | |
409 | */ | |
410 | error = xfs_refcount_lookup_ge(cur, center->rc_startblock, | |
411 | &found_rec); | |
412 | if (error) | |
413 | goto out_error; | |
414 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error); | |
415 | ||
416 | error = xfs_refcount_delete(cur, &found_rec); | |
417 | if (error) | |
418 | goto out_error; | |
419 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error); | |
420 | ||
421 | if (center->rc_refcount > 1) { | |
422 | error = xfs_refcount_delete(cur, &found_rec); | |
423 | if (error) | |
424 | goto out_error; | |
425 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, | |
426 | out_error); | |
427 | } | |
428 | ||
429 | /* Enlarge the left extent. */ | |
430 | error = xfs_refcount_lookup_le(cur, left->rc_startblock, | |
431 | &found_rec); | |
432 | if (error) | |
433 | goto out_error; | |
434 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error); | |
435 | ||
436 | left->rc_blockcount = extlen; | |
437 | error = xfs_refcount_update(cur, left); | |
438 | if (error) | |
439 | goto out_error; | |
440 | ||
441 | *aglen = 0; | |
442 | return error; | |
443 | ||
444 | out_error: | |
445 | trace_xfs_refcount_merge_center_extents_error(cur->bc_mp, | |
446 | cur->bc_private.a.agno, error, _RET_IP_); | |
447 | return error; | |
448 | } | |
449 | ||
450 | /* | |
451 | * Merge with the left extent. | |
452 | */ | |
453 | STATIC int | |
454 | xfs_refcount_merge_left_extent( | |
455 | struct xfs_btree_cur *cur, | |
456 | struct xfs_refcount_irec *left, | |
457 | struct xfs_refcount_irec *cleft, | |
458 | xfs_agblock_t *agbno, | |
459 | xfs_extlen_t *aglen) | |
460 | { | |
461 | int error; | |
462 | int found_rec; | |
463 | ||
464 | trace_xfs_refcount_merge_left_extent(cur->bc_mp, | |
465 | cur->bc_private.a.agno, left, cleft); | |
466 | ||
467 | /* If the extent at agbno (cleft) wasn't synthesized, remove it. */ | |
468 | if (cleft->rc_refcount > 1) { | |
469 | error = xfs_refcount_lookup_le(cur, cleft->rc_startblock, | |
470 | &found_rec); | |
471 | if (error) | |
472 | goto out_error; | |
473 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, | |
474 | out_error); | |
475 | ||
476 | error = xfs_refcount_delete(cur, &found_rec); | |
477 | if (error) | |
478 | goto out_error; | |
479 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, | |
480 | out_error); | |
481 | } | |
482 | ||
483 | /* Enlarge the left extent. */ | |
484 | error = xfs_refcount_lookup_le(cur, left->rc_startblock, | |
485 | &found_rec); | |
486 | if (error) | |
487 | goto out_error; | |
488 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error); | |
489 | ||
490 | left->rc_blockcount += cleft->rc_blockcount; | |
491 | error = xfs_refcount_update(cur, left); | |
492 | if (error) | |
493 | goto out_error; | |
494 | ||
495 | *agbno += cleft->rc_blockcount; | |
496 | *aglen -= cleft->rc_blockcount; | |
497 | return error; | |
498 | ||
499 | out_error: | |
500 | trace_xfs_refcount_merge_left_extent_error(cur->bc_mp, | |
501 | cur->bc_private.a.agno, error, _RET_IP_); | |
502 | return error; | |
503 | } | |
504 | ||
505 | /* | |
506 | * Merge with the right extent. | |
507 | */ | |
508 | STATIC int | |
509 | xfs_refcount_merge_right_extent( | |
510 | struct xfs_btree_cur *cur, | |
511 | struct xfs_refcount_irec *right, | |
512 | struct xfs_refcount_irec *cright, | |
56ef8c33 DW |
513 | xfs_extlen_t *aglen) |
514 | { | |
515 | int error; | |
516 | int found_rec; | |
517 | ||
518 | trace_xfs_refcount_merge_right_extent(cur->bc_mp, | |
519 | cur->bc_private.a.agno, cright, right); | |
520 | ||
521 | /* | |
522 | * If the extent ending at agbno+aglen (cright) wasn't synthesized, | |
523 | * remove it. | |
524 | */ | |
525 | if (cright->rc_refcount > 1) { | |
526 | error = xfs_refcount_lookup_le(cur, cright->rc_startblock, | |
527 | &found_rec); | |
528 | if (error) | |
529 | goto out_error; | |
530 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, | |
531 | out_error); | |
532 | ||
533 | error = xfs_refcount_delete(cur, &found_rec); | |
534 | if (error) | |
535 | goto out_error; | |
536 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, | |
537 | out_error); | |
538 | } | |
539 | ||
540 | /* Enlarge the right extent. */ | |
541 | error = xfs_refcount_lookup_le(cur, right->rc_startblock, | |
542 | &found_rec); | |
543 | if (error) | |
544 | goto out_error; | |
545 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error); | |
546 | ||
547 | right->rc_startblock -= cright->rc_blockcount; | |
548 | right->rc_blockcount += cright->rc_blockcount; | |
549 | error = xfs_refcount_update(cur, right); | |
550 | if (error) | |
551 | goto out_error; | |
552 | ||
553 | *aglen -= cright->rc_blockcount; | |
554 | return error; | |
555 | ||
556 | out_error: | |
557 | trace_xfs_refcount_merge_right_extent_error(cur->bc_mp, | |
558 | cur->bc_private.a.agno, error, _RET_IP_); | |
559 | return error; | |
560 | } | |
561 | ||
10e65503 DW |
562 | #define XFS_FIND_RCEXT_SHARED 1 |
563 | #define XFS_FIND_RCEXT_COW 2 | |
56ef8c33 DW |
564 | /* |
565 | * Find the left extent and the one after it (cleft). This function assumes | |
566 | * that we've already split any extent crossing agbno. | |
567 | */ | |
568 | STATIC int | |
569 | xfs_refcount_find_left_extents( | |
570 | struct xfs_btree_cur *cur, | |
571 | struct xfs_refcount_irec *left, | |
572 | struct xfs_refcount_irec *cleft, | |
573 | xfs_agblock_t agbno, | |
10e65503 DW |
574 | xfs_extlen_t aglen, |
575 | int flags) | |
56ef8c33 DW |
576 | { |
577 | struct xfs_refcount_irec tmp; | |
578 | int error; | |
579 | int found_rec; | |
580 | ||
581 | left->rc_startblock = cleft->rc_startblock = NULLAGBLOCK; | |
582 | error = xfs_refcount_lookup_le(cur, agbno - 1, &found_rec); | |
583 | if (error) | |
584 | goto out_error; | |
585 | if (!found_rec) | |
586 | return 0; | |
587 | ||
588 | error = xfs_refcount_get_rec(cur, &tmp, &found_rec); | |
589 | if (error) | |
590 | goto out_error; | |
591 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error); | |
592 | ||
593 | if (xfs_refc_next(&tmp) != agbno) | |
594 | return 0; | |
10e65503 DW |
595 | if ((flags & XFS_FIND_RCEXT_SHARED) && tmp.rc_refcount < 2) |
596 | return 0; | |
597 | if ((flags & XFS_FIND_RCEXT_COW) && tmp.rc_refcount > 1) | |
598 | return 0; | |
56ef8c33 DW |
599 | /* We have a left extent; retrieve (or invent) the next right one */ |
600 | *left = tmp; | |
601 | ||
602 | error = xfs_btree_increment(cur, 0, &found_rec); | |
603 | if (error) | |
604 | goto out_error; | |
605 | if (found_rec) { | |
606 | error = xfs_refcount_get_rec(cur, &tmp, &found_rec); | |
607 | if (error) | |
608 | goto out_error; | |
609 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, | |
610 | out_error); | |
611 | ||
612 | /* if tmp starts at the end of our range, just use that */ | |
613 | if (tmp.rc_startblock == agbno) | |
614 | *cleft = tmp; | |
615 | else { | |
616 | /* | |
617 | * There's a gap in the refcntbt at the start of the | |
618 | * range we're interested in (refcount == 1) so | |
619 | * synthesize the implied extent and pass it back. | |
620 | * We assume here that the agbno/aglen range was | |
621 | * passed in from a data fork extent mapping and | |
622 | * therefore is allocated to exactly one owner. | |
623 | */ | |
624 | cleft->rc_startblock = agbno; | |
625 | cleft->rc_blockcount = min(aglen, | |
626 | tmp.rc_startblock - agbno); | |
627 | cleft->rc_refcount = 1; | |
628 | } | |
629 | } else { | |
630 | /* | |
631 | * No extents, so pretend that there's one covering the whole | |
632 | * range. | |
633 | */ | |
634 | cleft->rc_startblock = agbno; | |
635 | cleft->rc_blockcount = aglen; | |
636 | cleft->rc_refcount = 1; | |
637 | } | |
638 | trace_xfs_refcount_find_left_extent(cur->bc_mp, cur->bc_private.a.agno, | |
639 | left, cleft, agbno); | |
640 | return error; | |
641 | ||
642 | out_error: | |
643 | trace_xfs_refcount_find_left_extent_error(cur->bc_mp, | |
644 | cur->bc_private.a.agno, error, _RET_IP_); | |
645 | return error; | |
646 | } | |
647 | ||
648 | /* | |
649 | * Find the right extent and the one before it (cright). This function | |
650 | * assumes that we've already split any extents crossing agbno + aglen. | |
651 | */ | |
652 | STATIC int | |
653 | xfs_refcount_find_right_extents( | |
654 | struct xfs_btree_cur *cur, | |
655 | struct xfs_refcount_irec *right, | |
656 | struct xfs_refcount_irec *cright, | |
657 | xfs_agblock_t agbno, | |
10e65503 DW |
658 | xfs_extlen_t aglen, |
659 | int flags) | |
56ef8c33 DW |
660 | { |
661 | struct xfs_refcount_irec tmp; | |
662 | int error; | |
663 | int found_rec; | |
664 | ||
665 | right->rc_startblock = cright->rc_startblock = NULLAGBLOCK; | |
666 | error = xfs_refcount_lookup_ge(cur, agbno + aglen, &found_rec); | |
667 | if (error) | |
668 | goto out_error; | |
669 | if (!found_rec) | |
670 | return 0; | |
671 | ||
672 | error = xfs_refcount_get_rec(cur, &tmp, &found_rec); | |
673 | if (error) | |
674 | goto out_error; | |
675 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error); | |
676 | ||
677 | if (tmp.rc_startblock != agbno + aglen) | |
678 | return 0; | |
10e65503 DW |
679 | if ((flags & XFS_FIND_RCEXT_SHARED) && tmp.rc_refcount < 2) |
680 | return 0; | |
681 | if ((flags & XFS_FIND_RCEXT_COW) && tmp.rc_refcount > 1) | |
682 | return 0; | |
56ef8c33 DW |
683 | /* We have a right extent; retrieve (or invent) the next left one */ |
684 | *right = tmp; | |
685 | ||
686 | error = xfs_btree_decrement(cur, 0, &found_rec); | |
687 | if (error) | |
688 | goto out_error; | |
689 | if (found_rec) { | |
690 | error = xfs_refcount_get_rec(cur, &tmp, &found_rec); | |
691 | if (error) | |
692 | goto out_error; | |
693 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, | |
694 | out_error); | |
695 | ||
696 | /* if tmp ends at the end of our range, just use that */ | |
697 | if (xfs_refc_next(&tmp) == agbno + aglen) | |
698 | *cright = tmp; | |
699 | else { | |
700 | /* | |
701 | * There's a gap in the refcntbt at the end of the | |
702 | * range we're interested in (refcount == 1) so | |
703 | * create the implied extent and pass it back. | |
704 | * We assume here that the agbno/aglen range was | |
705 | * passed in from a data fork extent mapping and | |
706 | * therefore is allocated to exactly one owner. | |
707 | */ | |
708 | cright->rc_startblock = max(agbno, xfs_refc_next(&tmp)); | |
709 | cright->rc_blockcount = right->rc_startblock - | |
710 | cright->rc_startblock; | |
711 | cright->rc_refcount = 1; | |
712 | } | |
713 | } else { | |
714 | /* | |
715 | * No extents, so pretend that there's one covering the whole | |
716 | * range. | |
717 | */ | |
718 | cright->rc_startblock = agbno; | |
719 | cright->rc_blockcount = aglen; | |
720 | cright->rc_refcount = 1; | |
721 | } | |
722 | trace_xfs_refcount_find_right_extent(cur->bc_mp, cur->bc_private.a.agno, | |
723 | cright, right, agbno + aglen); | |
724 | return error; | |
725 | ||
726 | out_error: | |
727 | trace_xfs_refcount_find_right_extent_error(cur->bc_mp, | |
728 | cur->bc_private.a.agno, error, _RET_IP_); | |
729 | return error; | |
730 | } | |
731 | ||
732 | /* Is this extent valid? */ | |
733 | static inline bool | |
734 | xfs_refc_valid( | |
735 | struct xfs_refcount_irec *rc) | |
736 | { | |
737 | return rc->rc_startblock != NULLAGBLOCK; | |
738 | } | |
739 | ||
740 | /* | |
741 | * Try to merge with any extents on the boundaries of the adjustment range. | |
742 | */ | |
743 | STATIC int | |
744 | xfs_refcount_merge_extents( | |
745 | struct xfs_btree_cur *cur, | |
746 | xfs_agblock_t *agbno, | |
747 | xfs_extlen_t *aglen, | |
748 | enum xfs_refc_adjust_op adjust, | |
10e65503 | 749 | int flags, |
56ef8c33 DW |
750 | bool *shape_changed) |
751 | { | |
752 | struct xfs_refcount_irec left = {0}, cleft = {0}; | |
753 | struct xfs_refcount_irec cright = {0}, right = {0}; | |
754 | int error; | |
755 | unsigned long long ulen; | |
756 | bool cequal; | |
757 | ||
758 | *shape_changed = false; | |
759 | /* | |
760 | * Find the extent just below agbno [left], just above agbno [cleft], | |
761 | * just below (agbno + aglen) [cright], and just above (agbno + aglen) | |
762 | * [right]. | |
763 | */ | |
764 | error = xfs_refcount_find_left_extents(cur, &left, &cleft, *agbno, | |
10e65503 | 765 | *aglen, flags); |
56ef8c33 DW |
766 | if (error) |
767 | return error; | |
768 | error = xfs_refcount_find_right_extents(cur, &right, &cright, *agbno, | |
10e65503 | 769 | *aglen, flags); |
56ef8c33 DW |
770 | if (error) |
771 | return error; | |
772 | ||
773 | /* No left or right extent to merge; exit. */ | |
774 | if (!xfs_refc_valid(&left) && !xfs_refc_valid(&right)) | |
775 | return 0; | |
776 | ||
777 | cequal = (cleft.rc_startblock == cright.rc_startblock) && | |
778 | (cleft.rc_blockcount == cright.rc_blockcount); | |
779 | ||
780 | /* Try to merge left, cleft, and right. cleft must == cright. */ | |
781 | ulen = (unsigned long long)left.rc_blockcount + cleft.rc_blockcount + | |
782 | right.rc_blockcount; | |
783 | if (xfs_refc_valid(&left) && xfs_refc_valid(&right) && | |
784 | xfs_refc_valid(&cleft) && xfs_refc_valid(&cright) && cequal && | |
785 | left.rc_refcount == cleft.rc_refcount + adjust && | |
786 | right.rc_refcount == cleft.rc_refcount + adjust && | |
787 | ulen < MAXREFCEXTLEN) { | |
788 | *shape_changed = true; | |
789 | return xfs_refcount_merge_center_extents(cur, &left, &cleft, | |
1421de38 | 790 | &right, ulen, aglen); |
56ef8c33 DW |
791 | } |
792 | ||
793 | /* Try to merge left and cleft. */ | |
794 | ulen = (unsigned long long)left.rc_blockcount + cleft.rc_blockcount; | |
795 | if (xfs_refc_valid(&left) && xfs_refc_valid(&cleft) && | |
796 | left.rc_refcount == cleft.rc_refcount + adjust && | |
797 | ulen < MAXREFCEXTLEN) { | |
798 | *shape_changed = true; | |
799 | error = xfs_refcount_merge_left_extent(cur, &left, &cleft, | |
800 | agbno, aglen); | |
801 | if (error) | |
802 | return error; | |
803 | ||
804 | /* | |
805 | * If we just merged left + cleft and cleft == cright, | |
806 | * we no longer have a cright to merge with right. We're done. | |
807 | */ | |
808 | if (cequal) | |
809 | return 0; | |
810 | } | |
811 | ||
812 | /* Try to merge cright and right. */ | |
813 | ulen = (unsigned long long)right.rc_blockcount + cright.rc_blockcount; | |
814 | if (xfs_refc_valid(&right) && xfs_refc_valid(&cright) && | |
815 | right.rc_refcount == cright.rc_refcount + adjust && | |
816 | ulen < MAXREFCEXTLEN) { | |
817 | *shape_changed = true; | |
818 | return xfs_refcount_merge_right_extent(cur, &right, &cright, | |
1421de38 | 819 | aglen); |
56ef8c33 DW |
820 | } |
821 | ||
822 | return error; | |
823 | } | |
824 | ||
825 | /* | |
56ef8c33 DW |
826 | * XXX: This is a pretty hand-wavy estimate. The penalty for guessing |
827 | * true incorrectly is a shutdown FS; the penalty for guessing false | |
828 | * incorrectly is more transaction rolls than might be necessary. | |
829 | * Be conservative here. | |
830 | */ | |
831 | static bool | |
832 | xfs_refcount_still_have_space( | |
833 | struct xfs_btree_cur *cur) | |
834 | { | |
835 | unsigned long overhead; | |
836 | ||
837 | overhead = cur->bc_private.a.priv.refc.shape_changes * | |
838 | xfs_allocfree_log_count(cur->bc_mp, 1); | |
839 | overhead *= cur->bc_mp->m_sb.sb_blocksize; | |
840 | ||
841 | /* | |
842 | * Only allow 2 refcount extent updates per transaction if the | |
843 | * refcount continue update "error" has been injected. | |
844 | */ | |
845 | if (cur->bc_private.a.priv.refc.nr_ops > 2 && | |
846 | XFS_TEST_ERROR(false, cur->bc_mp, | |
e2a190dd | 847 | XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE)) |
56ef8c33 DW |
848 | return false; |
849 | ||
850 | if (cur->bc_private.a.priv.refc.nr_ops == 0) | |
851 | return true; | |
852 | else if (overhead > cur->bc_tp->t_log_res) | |
853 | return false; | |
854 | return cur->bc_tp->t_log_res - overhead > | |
594956fa | 855 | cur->bc_private.a.priv.refc.nr_ops * XFS_REFCOUNT_ITEM_OVERHEAD; |
56ef8c33 DW |
856 | } |
857 | ||
858 | /* | |
859 | * Adjust the refcounts of middle extents. At this point we should have | |
860 | * split extents that crossed the adjustment range; merged with adjacent | |
861 | * extents; and updated agbno/aglen to reflect the merges. Therefore, | |
862 | * all we have to do is update the extents inside [agbno, agbno + aglen]. | |
863 | */ | |
864 | STATIC int | |
865 | xfs_refcount_adjust_extents( | |
866 | struct xfs_btree_cur *cur, | |
867 | xfs_agblock_t *agbno, | |
868 | xfs_extlen_t *aglen, | |
869 | enum xfs_refc_adjust_op adj, | |
56ef8c33 DW |
870 | struct xfs_owner_info *oinfo) |
871 | { | |
872 | struct xfs_refcount_irec ext, tmp; | |
873 | int error; | |
874 | int found_rec, found_tmp; | |
875 | xfs_fsblock_t fsbno; | |
876 | ||
877 | /* Merging did all the work already. */ | |
878 | if (*aglen == 0) | |
879 | return 0; | |
880 | ||
881 | error = xfs_refcount_lookup_ge(cur, *agbno, &found_rec); | |
882 | if (error) | |
883 | goto out_error; | |
884 | ||
885 | while (*aglen > 0 && xfs_refcount_still_have_space(cur)) { | |
886 | error = xfs_refcount_get_rec(cur, &ext, &found_rec); | |
887 | if (error) | |
888 | goto out_error; | |
889 | if (!found_rec) { | |
890 | ext.rc_startblock = cur->bc_mp->m_sb.sb_agblocks; | |
891 | ext.rc_blockcount = 0; | |
892 | ext.rc_refcount = 0; | |
893 | } | |
894 | ||
895 | /* | |
896 | * Deal with a hole in the refcount tree; if a file maps to | |
897 | * these blocks and there's no refcountbt record, pretend that | |
898 | * there is one with refcount == 1. | |
899 | */ | |
900 | if (ext.rc_startblock != *agbno) { | |
901 | tmp.rc_startblock = *agbno; | |
902 | tmp.rc_blockcount = min(*aglen, | |
903 | ext.rc_startblock - *agbno); | |
904 | tmp.rc_refcount = 1 + adj; | |
905 | trace_xfs_refcount_modify_extent(cur->bc_mp, | |
906 | cur->bc_private.a.agno, &tmp); | |
907 | ||
908 | /* | |
909 | * Either cover the hole (increment) or | |
910 | * delete the range (decrement). | |
911 | */ | |
912 | if (tmp.rc_refcount) { | |
913 | error = xfs_refcount_insert(cur, &tmp, | |
914 | &found_tmp); | |
915 | if (error) | |
916 | goto out_error; | |
917 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, | |
918 | found_tmp == 1, out_error); | |
919 | cur->bc_private.a.priv.refc.nr_ops++; | |
920 | } else { | |
921 | fsbno = XFS_AGB_TO_FSB(cur->bc_mp, | |
922 | cur->bc_private.a.agno, | |
923 | tmp.rc_startblock); | |
21375e5d BF |
924 | xfs_bmap_add_free(cur->bc_tp, fsbno, |
925 | tmp.rc_blockcount, oinfo); | |
56ef8c33 DW |
926 | } |
927 | ||
928 | (*agbno) += tmp.rc_blockcount; | |
929 | (*aglen) -= tmp.rc_blockcount; | |
930 | ||
931 | error = xfs_refcount_lookup_ge(cur, *agbno, | |
932 | &found_rec); | |
933 | if (error) | |
934 | goto out_error; | |
935 | } | |
936 | ||
937 | /* Stop if there's nothing left to modify */ | |
938 | if (*aglen == 0 || !xfs_refcount_still_have_space(cur)) | |
939 | break; | |
940 | ||
941 | /* | |
942 | * Adjust the reference count and either update the tree | |
943 | * (incr) or free the blocks (decr). | |
944 | */ | |
945 | if (ext.rc_refcount == MAXREFCOUNT) | |
946 | goto skip; | |
947 | ext.rc_refcount += adj; | |
948 | trace_xfs_refcount_modify_extent(cur->bc_mp, | |
949 | cur->bc_private.a.agno, &ext); | |
950 | if (ext.rc_refcount > 1) { | |
951 | error = xfs_refcount_update(cur, &ext); | |
952 | if (error) | |
953 | goto out_error; | |
954 | cur->bc_private.a.priv.refc.nr_ops++; | |
955 | } else if (ext.rc_refcount == 1) { | |
956 | error = xfs_refcount_delete(cur, &found_rec); | |
957 | if (error) | |
958 | goto out_error; | |
959 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, | |
960 | found_rec == 1, out_error); | |
961 | cur->bc_private.a.priv.refc.nr_ops++; | |
962 | goto advloop; | |
963 | } else { | |
964 | fsbno = XFS_AGB_TO_FSB(cur->bc_mp, | |
965 | cur->bc_private.a.agno, | |
966 | ext.rc_startblock); | |
21375e5d BF |
967 | xfs_bmap_add_free(cur->bc_tp, fsbno, ext.rc_blockcount, |
968 | oinfo); | |
56ef8c33 DW |
969 | } |
970 | ||
971 | skip: | |
972 | error = xfs_btree_increment(cur, 0, &found_rec); | |
973 | if (error) | |
974 | goto out_error; | |
975 | ||
976 | advloop: | |
977 | (*agbno) += ext.rc_blockcount; | |
978 | (*aglen) -= ext.rc_blockcount; | |
979 | } | |
980 | ||
981 | return error; | |
982 | out_error: | |
983 | trace_xfs_refcount_modify_extent_error(cur->bc_mp, | |
984 | cur->bc_private.a.agno, error, _RET_IP_); | |
985 | return error; | |
986 | } | |
987 | ||
988 | /* Adjust the reference count of a range of AG blocks. */ | |
989 | STATIC int | |
990 | xfs_refcount_adjust( | |
991 | struct xfs_btree_cur *cur, | |
992 | xfs_agblock_t agbno, | |
993 | xfs_extlen_t aglen, | |
994 | xfs_agblock_t *new_agbno, | |
995 | xfs_extlen_t *new_aglen, | |
996 | enum xfs_refc_adjust_op adj, | |
56ef8c33 DW |
997 | struct xfs_owner_info *oinfo) |
998 | { | |
999 | bool shape_changed; | |
1000 | int shape_changes = 0; | |
1001 | int error; | |
1002 | ||
1003 | *new_agbno = agbno; | |
1004 | *new_aglen = aglen; | |
1005 | if (adj == XFS_REFCOUNT_ADJUST_INCREASE) | |
1006 | trace_xfs_refcount_increase(cur->bc_mp, cur->bc_private.a.agno, | |
1007 | agbno, aglen); | |
1008 | else | |
1009 | trace_xfs_refcount_decrease(cur->bc_mp, cur->bc_private.a.agno, | |
1010 | agbno, aglen); | |
1011 | ||
1012 | /* | |
1013 | * Ensure that no rcextents cross the boundary of the adjustment range. | |
1014 | */ | |
1015 | error = xfs_refcount_split_extent(cur, agbno, &shape_changed); | |
1016 | if (error) | |
1017 | goto out_error; | |
1018 | if (shape_changed) | |
1019 | shape_changes++; | |
1020 | ||
1021 | error = xfs_refcount_split_extent(cur, agbno + aglen, &shape_changed); | |
1022 | if (error) | |
1023 | goto out_error; | |
1024 | if (shape_changed) | |
1025 | shape_changes++; | |
1026 | ||
1027 | /* | |
1028 | * Try to merge with the left or right extents of the range. | |
1029 | */ | |
1030 | error = xfs_refcount_merge_extents(cur, new_agbno, new_aglen, adj, | |
10e65503 | 1031 | XFS_FIND_RCEXT_SHARED, &shape_changed); |
56ef8c33 DW |
1032 | if (error) |
1033 | goto out_error; | |
1034 | if (shape_changed) | |
1035 | shape_changes++; | |
1036 | if (shape_changes) | |
1037 | cur->bc_private.a.priv.refc.shape_changes++; | |
1038 | ||
1039 | /* Now that we've taken care of the ends, adjust the middle extents */ | |
1040 | error = xfs_refcount_adjust_extents(cur, new_agbno, new_aglen, | |
21375e5d | 1041 | adj, oinfo); |
56ef8c33 DW |
1042 | if (error) |
1043 | goto out_error; | |
1044 | ||
1045 | return 0; | |
1046 | ||
1047 | out_error: | |
1048 | trace_xfs_refcount_adjust_error(cur->bc_mp, cur->bc_private.a.agno, | |
1049 | error, _RET_IP_); | |
1050 | return error; | |
1051 | } | |
23a15a6c DW |
1052 | |
1053 | /* Clean up after calling xfs_refcount_finish_one. */ | |
1054 | void | |
1055 | xfs_refcount_finish_one_cleanup( | |
1056 | struct xfs_trans *tp, | |
1057 | struct xfs_btree_cur *rcur, | |
1058 | int error) | |
1059 | { | |
1060 | struct xfs_buf *agbp; | |
1061 | ||
1062 | if (rcur == NULL) | |
1063 | return; | |
1064 | agbp = rcur->bc_private.a.agbp; | |
660265b7 | 1065 | xfs_btree_del_cursor(rcur, error); |
23a15a6c DW |
1066 | if (error) |
1067 | xfs_trans_brelse(tp, agbp); | |
1068 | } | |
1069 | ||
1070 | /* | |
1071 | * Process one of the deferred refcount operations. We pass back the | |
1072 | * btree cursor to maintain our lock on the btree between calls. | |
1073 | * This saves time and eliminates a buffer deadlock between the | |
1074 | * superblock and the AGF because we'll always grab them in the same | |
1075 | * order. | |
1076 | */ | |
1077 | int | |
1078 | xfs_refcount_finish_one( | |
1079 | struct xfs_trans *tp, | |
23a15a6c DW |
1080 | enum xfs_refcount_intent_type type, |
1081 | xfs_fsblock_t startblock, | |
1082 | xfs_extlen_t blockcount, | |
1083 | xfs_fsblock_t *new_fsb, | |
1084 | xfs_extlen_t *new_len, | |
1085 | struct xfs_btree_cur **pcur) | |
1086 | { | |
1087 | struct xfs_mount *mp = tp->t_mountp; | |
1088 | struct xfs_btree_cur *rcur; | |
1089 | struct xfs_buf *agbp = NULL; | |
1090 | int error = 0; | |
1091 | xfs_agnumber_t agno; | |
1092 | xfs_agblock_t bno; | |
1093 | xfs_agblock_t new_agbno; | |
1094 | unsigned long nr_ops = 0; | |
1095 | int shape_changes = 0; | |
1096 | ||
1097 | agno = XFS_FSB_TO_AGNO(mp, startblock); | |
1098 | ASSERT(agno != NULLAGNUMBER); | |
1099 | bno = XFS_FSB_TO_AGBNO(mp, startblock); | |
1100 | ||
1101 | trace_xfs_refcount_deferred(mp, XFS_FSB_TO_AGNO(mp, startblock), | |
1102 | type, XFS_FSB_TO_AGBNO(mp, startblock), | |
1103 | blockcount); | |
1104 | ||
1105 | if (XFS_TEST_ERROR(false, mp, | |
e2a190dd | 1106 | XFS_ERRTAG_REFCOUNT_FINISH_ONE)) |
23a15a6c DW |
1107 | return -EIO; |
1108 | ||
1109 | /* | |
1110 | * If we haven't gotten a cursor or the cursor AG doesn't match | |
1111 | * the startblock, get one now. | |
1112 | */ | |
1113 | rcur = *pcur; | |
1114 | if (rcur != NULL && rcur->bc_private.a.agno != agno) { | |
1115 | nr_ops = rcur->bc_private.a.priv.refc.nr_ops; | |
1116 | shape_changes = rcur->bc_private.a.priv.refc.shape_changes; | |
1117 | xfs_refcount_finish_one_cleanup(tp, rcur, 0); | |
1118 | rcur = NULL; | |
1119 | *pcur = NULL; | |
1120 | } | |
1121 | if (rcur == NULL) { | |
1122 | error = xfs_alloc_read_agf(tp->t_mountp, tp, agno, | |
1123 | XFS_ALLOC_FLAG_FREEING, &agbp); | |
1124 | if (error) | |
1125 | return error; | |
1126 | if (!agbp) | |
1127 | return -EFSCORRUPTED; | |
1128 | ||
5ff5ced0 | 1129 | rcur = xfs_refcountbt_init_cursor(mp, tp, agbp, agno); |
23a15a6c DW |
1130 | if (!rcur) { |
1131 | error = -ENOMEM; | |
1132 | goto out_cur; | |
1133 | } | |
1134 | rcur->bc_private.a.priv.refc.nr_ops = nr_ops; | |
1135 | rcur->bc_private.a.priv.refc.shape_changes = shape_changes; | |
1136 | } | |
1137 | *pcur = rcur; | |
1138 | ||
1139 | switch (type) { | |
1140 | case XFS_REFCOUNT_INCREASE: | |
1141 | error = xfs_refcount_adjust(rcur, bno, blockcount, &new_agbno, | |
21375e5d | 1142 | new_len, XFS_REFCOUNT_ADJUST_INCREASE, NULL); |
23a15a6c DW |
1143 | *new_fsb = XFS_AGB_TO_FSB(mp, agno, new_agbno); |
1144 | break; | |
1145 | case XFS_REFCOUNT_DECREASE: | |
1146 | error = xfs_refcount_adjust(rcur, bno, blockcount, &new_agbno, | |
21375e5d | 1147 | new_len, XFS_REFCOUNT_ADJUST_DECREASE, NULL); |
23a15a6c DW |
1148 | *new_fsb = XFS_AGB_TO_FSB(mp, agno, new_agbno); |
1149 | break; | |
10e65503 DW |
1150 | case XFS_REFCOUNT_ALLOC_COW: |
1151 | *new_fsb = startblock + blockcount; | |
1152 | *new_len = 0; | |
21375e5d | 1153 | error = __xfs_refcount_cow_alloc(rcur, bno, blockcount); |
10e65503 DW |
1154 | break; |
1155 | case XFS_REFCOUNT_FREE_COW: | |
1156 | *new_fsb = startblock + blockcount; | |
1157 | *new_len = 0; | |
21375e5d | 1158 | error = __xfs_refcount_cow_free(rcur, bno, blockcount); |
10e65503 | 1159 | break; |
23a15a6c DW |
1160 | default: |
1161 | ASSERT(0); | |
1162 | error = -EFSCORRUPTED; | |
1163 | } | |
1164 | if (!error && *new_len > 0) | |
1165 | trace_xfs_refcount_finish_one_leftover(mp, agno, type, | |
1166 | bno, blockcount, new_agbno, *new_len); | |
1167 | return error; | |
1168 | ||
1169 | out_cur: | |
1170 | xfs_trans_brelse(tp, agbp); | |
1171 | ||
1172 | return error; | |
1173 | } | |
1174 | ||
1175 | /* | |
1176 | * Record a refcount intent for later processing. | |
1177 | */ | |
1178 | static int | |
1179 | __xfs_refcount_add( | |
21375e5d | 1180 | struct xfs_trans *tp, |
23a15a6c DW |
1181 | enum xfs_refcount_intent_type type, |
1182 | xfs_fsblock_t startblock, | |
1183 | xfs_extlen_t blockcount) | |
1184 | { | |
1185 | struct xfs_refcount_intent *ri; | |
1186 | ||
21375e5d BF |
1187 | trace_xfs_refcount_defer(tp->t_mountp, |
1188 | XFS_FSB_TO_AGNO(tp->t_mountp, startblock), | |
1189 | type, XFS_FSB_TO_AGBNO(tp->t_mountp, startblock), | |
23a15a6c DW |
1190 | blockcount); |
1191 | ||
1192 | ri = kmem_alloc(sizeof(struct xfs_refcount_intent), | |
1193 | KM_SLEEP | KM_NOFS); | |
1194 | INIT_LIST_HEAD(&ri->ri_list); | |
1195 | ri->ri_type = type; | |
1196 | ri->ri_startblock = startblock; | |
1197 | ri->ri_blockcount = blockcount; | |
1198 | ||
21375e5d | 1199 | xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_REFCOUNT, &ri->ri_list); |
23a15a6c DW |
1200 | return 0; |
1201 | } | |
1202 | ||
1203 | /* | |
1204 | * Increase the reference count of the blocks backing a file's extent. | |
1205 | */ | |
1206 | int | |
1207 | xfs_refcount_increase_extent( | |
21375e5d | 1208 | struct xfs_trans *tp, |
23a15a6c DW |
1209 | struct xfs_bmbt_irec *PREV) |
1210 | { | |
21375e5d | 1211 | if (!xfs_sb_version_hasreflink(&tp->t_mountp->m_sb)) |
23a15a6c DW |
1212 | return 0; |
1213 | ||
21375e5d | 1214 | return __xfs_refcount_add(tp, XFS_REFCOUNT_INCREASE, |
23a15a6c DW |
1215 | PREV->br_startblock, PREV->br_blockcount); |
1216 | } | |
1217 | ||
1218 | /* | |
1219 | * Decrease the reference count of the blocks backing a file's extent. | |
1220 | */ | |
1221 | int | |
1222 | xfs_refcount_decrease_extent( | |
21375e5d | 1223 | struct xfs_trans *tp, |
23a15a6c DW |
1224 | struct xfs_bmbt_irec *PREV) |
1225 | { | |
21375e5d | 1226 | if (!xfs_sb_version_hasreflink(&tp->t_mountp->m_sb)) |
23a15a6c DW |
1227 | return 0; |
1228 | ||
21375e5d | 1229 | return __xfs_refcount_add(tp, XFS_REFCOUNT_DECREASE, |
23a15a6c DW |
1230 | PREV->br_startblock, PREV->br_blockcount); |
1231 | } | |
94097330 DW |
1232 | |
1233 | /* | |
1234 | * Given an AG extent, find the lowest-numbered run of shared blocks | |
1235 | * within that range and return the range in fbno/flen. If | |
1236 | * find_end_of_shared is set, return the longest contiguous extent of | |
1237 | * shared blocks; if not, just return the first extent we find. If no | |
1238 | * shared blocks are found, fbno and flen will be set to NULLAGBLOCK | |
1239 | * and 0, respectively. | |
1240 | */ | |
1241 | int | |
1242 | xfs_refcount_find_shared( | |
1243 | struct xfs_btree_cur *cur, | |
1244 | xfs_agblock_t agbno, | |
1245 | xfs_extlen_t aglen, | |
1246 | xfs_agblock_t *fbno, | |
1247 | xfs_extlen_t *flen, | |
1248 | bool find_end_of_shared) | |
1249 | { | |
1250 | struct xfs_refcount_irec tmp; | |
1251 | int i; | |
1252 | int have; | |
1253 | int error; | |
1254 | ||
1255 | trace_xfs_refcount_find_shared(cur->bc_mp, cur->bc_private.a.agno, | |
1256 | agbno, aglen); | |
1257 | ||
1258 | /* By default, skip the whole range */ | |
1259 | *fbno = NULLAGBLOCK; | |
1260 | *flen = 0; | |
1261 | ||
1262 | /* Try to find a refcount extent that crosses the start */ | |
1263 | error = xfs_refcount_lookup_le(cur, agbno, &have); | |
1264 | if (error) | |
1265 | goto out_error; | |
1266 | if (!have) { | |
1267 | /* No left extent, look at the next one */ | |
1268 | error = xfs_btree_increment(cur, 0, &have); | |
1269 | if (error) | |
1270 | goto out_error; | |
1271 | if (!have) | |
1272 | goto done; | |
1273 | } | |
1274 | error = xfs_refcount_get_rec(cur, &tmp, &i); | |
1275 | if (error) | |
1276 | goto out_error; | |
1277 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, out_error); | |
1278 | ||
1279 | /* If the extent ends before the start, look at the next one */ | |
1280 | if (tmp.rc_startblock + tmp.rc_blockcount <= agbno) { | |
1281 | error = xfs_btree_increment(cur, 0, &have); | |
1282 | if (error) | |
1283 | goto out_error; | |
1284 | if (!have) | |
1285 | goto done; | |
1286 | error = xfs_refcount_get_rec(cur, &tmp, &i); | |
1287 | if (error) | |
1288 | goto out_error; | |
1289 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, out_error); | |
1290 | } | |
1291 | ||
1292 | /* If the extent starts after the range we want, bail out */ | |
1293 | if (tmp.rc_startblock >= agbno + aglen) | |
1294 | goto done; | |
1295 | ||
1296 | /* We found the start of a shared extent! */ | |
1297 | if (tmp.rc_startblock < agbno) { | |
1298 | tmp.rc_blockcount -= (agbno - tmp.rc_startblock); | |
1299 | tmp.rc_startblock = agbno; | |
1300 | } | |
1301 | ||
1302 | *fbno = tmp.rc_startblock; | |
1303 | *flen = min(tmp.rc_blockcount, agbno + aglen - *fbno); | |
1304 | if (!find_end_of_shared) | |
1305 | goto done; | |
1306 | ||
1307 | /* Otherwise, find the end of this shared extent */ | |
1308 | while (*fbno + *flen < agbno + aglen) { | |
1309 | error = xfs_btree_increment(cur, 0, &have); | |
1310 | if (error) | |
1311 | goto out_error; | |
1312 | if (!have) | |
1313 | break; | |
1314 | error = xfs_refcount_get_rec(cur, &tmp, &i); | |
1315 | if (error) | |
1316 | goto out_error; | |
1317 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, out_error); | |
1318 | if (tmp.rc_startblock >= agbno + aglen || | |
1319 | tmp.rc_startblock != *fbno + *flen) | |
1320 | break; | |
1321 | *flen = min(*flen + tmp.rc_blockcount, agbno + aglen - *fbno); | |
1322 | } | |
1323 | ||
1324 | done: | |
1325 | trace_xfs_refcount_find_shared_result(cur->bc_mp, | |
1326 | cur->bc_private.a.agno, *fbno, *flen); | |
1327 | ||
1328 | out_error: | |
1329 | if (error) | |
1330 | trace_xfs_refcount_find_shared_error(cur->bc_mp, | |
1331 | cur->bc_private.a.agno, error, _RET_IP_); | |
1332 | return error; | |
1333 | } | |
10e65503 DW |
1334 | |
1335 | /* | |
1336 | * Recovering CoW Blocks After a Crash | |
1337 | * | |
1338 | * Due to the way that the copy on write mechanism works, there's a window of | |
1339 | * opportunity in which we can lose track of allocated blocks during a crash. | |
1340 | * Because CoW uses delayed allocation in the in-core CoW fork, writeback | |
1341 | * causes blocks to be allocated and stored in the CoW fork. The blocks are | |
1342 | * no longer in the free space btree but are not otherwise recorded anywhere | |
1343 | * until the write completes and the blocks are mapped into the file. A crash | |
1344 | * in between allocation and remapping results in the replacement blocks being | |
1345 | * lost. This situation is exacerbated by the CoW extent size hint because | |
1346 | * allocations can hang around for long time. | |
1347 | * | |
1348 | * However, there is a place where we can record these allocations before they | |
1349 | * become mappings -- the reference count btree. The btree does not record | |
1350 | * extents with refcount == 1, so we can record allocations with a refcount of | |
1351 | * 1. Blocks being used for CoW writeout cannot be shared, so there should be | |
1352 | * no conflict with shared block records. These mappings should be created | |
1353 | * when we allocate blocks to the CoW fork and deleted when they're removed | |
1354 | * from the CoW fork. | |
1355 | * | |
1356 | * Minor nit: records for in-progress CoW allocations and records for shared | |
1357 | * extents must never be merged, to preserve the property that (except for CoW | |
1358 | * allocations) there are no refcount btree entries with refcount == 1. The | |
1359 | * only time this could potentially happen is when unsharing a block that's | |
1360 | * adjacent to CoW allocations, so we must be careful to avoid this. | |
1361 | * | |
1362 | * At mount time we recover lost CoW allocations by searching the refcount | |
1363 | * btree for these refcount == 1 mappings. These represent CoW allocations | |
1364 | * that were in progress at the time the filesystem went down, so we can free | |
1365 | * them to get the space back. | |
1366 | * | |
1367 | * This mechanism is superior to creating EFIs for unmapped CoW extents for | |
1368 | * several reasons -- first, EFIs pin the tail of the log and would have to be | |
1369 | * periodically relogged to avoid filling up the log. Second, CoW completions | |
1370 | * will have to file an EFD and create new EFIs for whatever remains in the | |
1371 | * CoW fork; this partially takes care of (1) but extent-size reservations | |
1372 | * will have to periodically relog even if there's no writeout in progress. | |
1373 | * This can happen if the CoW extent size hint is set, which you really want. | |
1374 | * Third, EFIs cannot currently be automatically relogged into newer | |
1375 | * transactions to advance the log tail. Fourth, stuffing the log full of | |
1376 | * EFIs places an upper bound on the number of CoW allocations that can be | |
1377 | * held filesystem-wide at any given time. Recording them in the refcount | |
1378 | * btree doesn't require us to maintain any state in memory and doesn't pin | |
1379 | * the log. | |
1380 | */ | |
1381 | /* | |
1382 | * Adjust the refcounts of CoW allocations. These allocations are "magic" | |
1383 | * in that they're not referenced anywhere else in the filesystem, so we | |
1384 | * stash them in the refcount btree with a refcount of 1 until either file | |
1385 | * remapping (or CoW cancellation) happens. | |
1386 | */ | |
1387 | STATIC int | |
1388 | xfs_refcount_adjust_cow_extents( | |
1389 | struct xfs_btree_cur *cur, | |
1390 | xfs_agblock_t agbno, | |
1391 | xfs_extlen_t aglen, | |
1421de38 | 1392 | enum xfs_refc_adjust_op adj) |
10e65503 DW |
1393 | { |
1394 | struct xfs_refcount_irec ext, tmp; | |
1395 | int error; | |
1396 | int found_rec, found_tmp; | |
1397 | ||
1398 | if (aglen == 0) | |
1399 | return 0; | |
1400 | ||
1401 | /* Find any overlapping refcount records */ | |
1402 | error = xfs_refcount_lookup_ge(cur, agbno, &found_rec); | |
1403 | if (error) | |
1404 | goto out_error; | |
1405 | error = xfs_refcount_get_rec(cur, &ext, &found_rec); | |
1406 | if (error) | |
1407 | goto out_error; | |
1408 | if (!found_rec) { | |
1409 | ext.rc_startblock = cur->bc_mp->m_sb.sb_agblocks + | |
1410 | XFS_REFC_COW_START; | |
1411 | ext.rc_blockcount = 0; | |
1412 | ext.rc_refcount = 0; | |
1413 | } | |
1414 | ||
1415 | switch (adj) { | |
1416 | case XFS_REFCOUNT_ADJUST_COW_ALLOC: | |
1417 | /* Adding a CoW reservation, there should be nothing here. */ | |
1418 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, | |
1419 | ext.rc_startblock >= agbno + aglen, out_error); | |
1420 | ||
1421 | tmp.rc_startblock = agbno; | |
1422 | tmp.rc_blockcount = aglen; | |
1423 | tmp.rc_refcount = 1; | |
1424 | trace_xfs_refcount_modify_extent(cur->bc_mp, | |
1425 | cur->bc_private.a.agno, &tmp); | |
1426 | ||
1427 | error = xfs_refcount_insert(cur, &tmp, | |
1428 | &found_tmp); | |
1429 | if (error) | |
1430 | goto out_error; | |
1431 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, | |
1432 | found_tmp == 1, out_error); | |
1433 | break; | |
1434 | case XFS_REFCOUNT_ADJUST_COW_FREE: | |
1435 | /* Removing a CoW reservation, there should be one extent. */ | |
1436 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, | |
1437 | ext.rc_startblock == agbno, out_error); | |
1438 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, | |
1439 | ext.rc_blockcount == aglen, out_error); | |
1440 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, | |
1441 | ext.rc_refcount == 1, out_error); | |
1442 | ||
1443 | ext.rc_refcount = 0; | |
1444 | trace_xfs_refcount_modify_extent(cur->bc_mp, | |
1445 | cur->bc_private.a.agno, &ext); | |
1446 | error = xfs_refcount_delete(cur, &found_rec); | |
1447 | if (error) | |
1448 | goto out_error; | |
1449 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, | |
1450 | found_rec == 1, out_error); | |
1451 | break; | |
1452 | default: | |
1453 | ASSERT(0); | |
1454 | } | |
1455 | ||
1456 | return error; | |
1457 | out_error: | |
1458 | trace_xfs_refcount_modify_extent_error(cur->bc_mp, | |
1459 | cur->bc_private.a.agno, error, _RET_IP_); | |
1460 | return error; | |
1461 | } | |
1462 | ||
1463 | /* | |
1464 | * Add or remove refcount btree entries for CoW reservations. | |
1465 | */ | |
1466 | STATIC int | |
1467 | xfs_refcount_adjust_cow( | |
1468 | struct xfs_btree_cur *cur, | |
1469 | xfs_agblock_t agbno, | |
1470 | xfs_extlen_t aglen, | |
1421de38 | 1471 | enum xfs_refc_adjust_op adj) |
10e65503 DW |
1472 | { |
1473 | bool shape_changed; | |
1474 | int error; | |
1475 | ||
1476 | agbno += XFS_REFC_COW_START; | |
1477 | ||
1478 | /* | |
1479 | * Ensure that no rcextents cross the boundary of the adjustment range. | |
1480 | */ | |
1481 | error = xfs_refcount_split_extent(cur, agbno, &shape_changed); | |
1482 | if (error) | |
1483 | goto out_error; | |
1484 | ||
1485 | error = xfs_refcount_split_extent(cur, agbno + aglen, &shape_changed); | |
1486 | if (error) | |
1487 | goto out_error; | |
1488 | ||
1489 | /* | |
1490 | * Try to merge with the left or right extents of the range. | |
1491 | */ | |
1492 | error = xfs_refcount_merge_extents(cur, &agbno, &aglen, adj, | |
1493 | XFS_FIND_RCEXT_COW, &shape_changed); | |
1494 | if (error) | |
1495 | goto out_error; | |
1496 | ||
1497 | /* Now that we've taken care of the ends, adjust the middle extents */ | |
1421de38 | 1498 | error = xfs_refcount_adjust_cow_extents(cur, agbno, aglen, adj); |
10e65503 DW |
1499 | if (error) |
1500 | goto out_error; | |
1501 | ||
1502 | return 0; | |
1503 | ||
1504 | out_error: | |
1505 | trace_xfs_refcount_adjust_cow_error(cur->bc_mp, cur->bc_private.a.agno, | |
1506 | error, _RET_IP_); | |
1507 | return error; | |
1508 | } | |
1509 | ||
1510 | /* | |
1511 | * Record a CoW allocation in the refcount btree. | |
1512 | */ | |
1513 | STATIC int | |
1514 | __xfs_refcount_cow_alloc( | |
1515 | struct xfs_btree_cur *rcur, | |
1516 | xfs_agblock_t agbno, | |
21375e5d | 1517 | xfs_extlen_t aglen) |
10e65503 | 1518 | { |
10e65503 DW |
1519 | trace_xfs_refcount_cow_increase(rcur->bc_mp, rcur->bc_private.a.agno, |
1520 | agbno, aglen); | |
1521 | ||
1522 | /* Add refcount btree reservation */ | |
a44b23a0 | 1523 | return xfs_refcount_adjust_cow(rcur, agbno, aglen, |
1421de38 | 1524 | XFS_REFCOUNT_ADJUST_COW_ALLOC); |
10e65503 DW |
1525 | } |
1526 | ||
1527 | /* | |
1528 | * Remove a CoW allocation from the refcount btree. | |
1529 | */ | |
1530 | STATIC int | |
1531 | __xfs_refcount_cow_free( | |
1532 | struct xfs_btree_cur *rcur, | |
1533 | xfs_agblock_t agbno, | |
21375e5d | 1534 | xfs_extlen_t aglen) |
10e65503 | 1535 | { |
10e65503 DW |
1536 | trace_xfs_refcount_cow_decrease(rcur->bc_mp, rcur->bc_private.a.agno, |
1537 | agbno, aglen); | |
1538 | ||
1539 | /* Remove refcount btree reservation */ | |
a44b23a0 | 1540 | return xfs_refcount_adjust_cow(rcur, agbno, aglen, |
1421de38 | 1541 | XFS_REFCOUNT_ADJUST_COW_FREE); |
10e65503 DW |
1542 | } |
1543 | ||
1544 | /* Record a CoW staging extent in the refcount btree. */ | |
1545 | int | |
1546 | xfs_refcount_alloc_cow_extent( | |
21375e5d | 1547 | struct xfs_trans *tp, |
10e65503 DW |
1548 | xfs_fsblock_t fsb, |
1549 | xfs_extlen_t len) | |
1550 | { | |
21375e5d | 1551 | struct xfs_mount *mp = tp->t_mountp; |
a44b23a0 DW |
1552 | int error; |
1553 | ||
10e65503 DW |
1554 | if (!xfs_sb_version_hasreflink(&mp->m_sb)) |
1555 | return 0; | |
1556 | ||
21375e5d | 1557 | error = __xfs_refcount_add(tp, XFS_REFCOUNT_ALLOC_COW, fsb, len); |
a44b23a0 DW |
1558 | if (error) |
1559 | return error; | |
1560 | ||
1561 | /* Add rmap entry */ | |
21375e5d | 1562 | return xfs_rmap_alloc_extent(tp, XFS_FSB_TO_AGNO(mp, fsb), |
a44b23a0 | 1563 | XFS_FSB_TO_AGBNO(mp, fsb), len, XFS_RMAP_OWN_COW); |
10e65503 DW |
1564 | } |
1565 | ||
1566 | /* Forget a CoW staging event in the refcount btree. */ | |
1567 | int | |
1568 | xfs_refcount_free_cow_extent( | |
21375e5d | 1569 | struct xfs_trans *tp, |
10e65503 DW |
1570 | xfs_fsblock_t fsb, |
1571 | xfs_extlen_t len) | |
1572 | { | |
21375e5d | 1573 | struct xfs_mount *mp = tp->t_mountp; |
a44b23a0 DW |
1574 | int error; |
1575 | ||
10e65503 DW |
1576 | if (!xfs_sb_version_hasreflink(&mp->m_sb)) |
1577 | return 0; | |
1578 | ||
a44b23a0 | 1579 | /* Remove rmap entry */ |
21375e5d | 1580 | error = xfs_rmap_free_extent(tp, XFS_FSB_TO_AGNO(mp, fsb), |
a44b23a0 DW |
1581 | XFS_FSB_TO_AGBNO(mp, fsb), len, XFS_RMAP_OWN_COW); |
1582 | if (error) | |
1583 | return error; | |
1584 | ||
21375e5d | 1585 | return __xfs_refcount_add(tp, XFS_REFCOUNT_FREE_COW, fsb, len); |
10e65503 DW |
1586 | } |
1587 | ||
1588 | struct xfs_refcount_recovery { | |
1589 | struct list_head rr_list; | |
1590 | struct xfs_refcount_irec rr_rrec; | |
1591 | }; | |
1592 | ||
1593 | /* Stuff an extent on the recovery list. */ | |
1594 | STATIC int | |
1595 | xfs_refcount_recover_extent( | |
1421de38 | 1596 | struct xfs_btree_cur *cur, |
10e65503 DW |
1597 | union xfs_btree_rec *rec, |
1598 | void *priv) | |
1599 | { | |
1600 | struct list_head *debris = priv; | |
1601 | struct xfs_refcount_recovery *rr; | |
1602 | ||
1603 | if (be32_to_cpu(rec->refc.rc_refcount) != 1) | |
1604 | return -EFSCORRUPTED; | |
1605 | ||
1606 | rr = kmem_alloc(sizeof(struct xfs_refcount_recovery), KM_SLEEP); | |
1607 | xfs_refcount_btrec_to_irec(rec, &rr->rr_rrec); | |
1608 | list_add_tail(&rr->rr_list, debris); | |
1609 | ||
1610 | return 0; | |
1611 | } | |
1612 | ||
1613 | /* Find and remove leftover CoW reservations. */ | |
1614 | int | |
1615 | xfs_refcount_recover_cow_leftovers( | |
1616 | struct xfs_mount *mp, | |
1617 | xfs_agnumber_t agno) | |
1618 | { | |
1619 | struct xfs_trans *tp; | |
1620 | struct xfs_btree_cur *cur; | |
1621 | struct xfs_buf *agbp; | |
1622 | struct xfs_refcount_recovery *rr, *n; | |
1623 | struct list_head debris; | |
1624 | union xfs_btree_irec low; | |
1625 | union xfs_btree_irec high; | |
10e65503 DW |
1626 | xfs_fsblock_t fsb; |
1627 | xfs_agblock_t agbno; | |
1628 | int error; | |
1629 | ||
1630 | if (mp->m_sb.sb_agblocks >= XFS_REFC_COW_START) | |
1631 | return -EOPNOTSUPP; | |
1632 | ||
d4e8eb2e DW |
1633 | INIT_LIST_HEAD(&debris); |
1634 | ||
1635 | /* | |
1636 | * In this first part, we use an empty transaction to gather up | |
1637 | * all the leftover CoW extents so that we can subsequently | |
1638 | * delete them. The empty transaction is used to avoid | |
1639 | * a buffer lock deadlock if there happens to be a loop in the | |
1640 | * refcountbt because we're allowed to re-grab a buffer that is | |
1641 | * already attached to our transaction. When we're done | |
1642 | * recording the CoW debris we cancel the (empty) transaction | |
1643 | * and everything goes away cleanly. | |
1644 | */ | |
1645 | error = xfs_trans_alloc_empty(mp, &tp); | |
10e65503 DW |
1646 | if (error) |
1647 | return error; | |
d4e8eb2e DW |
1648 | |
1649 | error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp); | |
1650 | if (error) | |
1651 | goto out_trans; | |
d705962a DW |
1652 | if (!agbp) { |
1653 | error = -ENOMEM; | |
1654 | goto out_trans; | |
1655 | } | |
5ff5ced0 | 1656 | cur = xfs_refcountbt_init_cursor(mp, tp, agbp, agno); |
10e65503 DW |
1657 | |
1658 | /* Find all the leftover CoW staging extents. */ | |
10e65503 DW |
1659 | memset(&low, 0, sizeof(low)); |
1660 | memset(&high, 0, sizeof(high)); | |
1661 | low.rc.rc_startblock = XFS_REFC_COW_START; | |
1662 | high.rc.rc_startblock = -1U; | |
1663 | error = xfs_btree_query_range(cur, &low, &high, | |
1664 | xfs_refcount_recover_extent, &debris); | |
a8a538b5 | 1665 | xfs_btree_del_cursor(cur, error); |
d4e8eb2e DW |
1666 | xfs_trans_brelse(tp, agbp); |
1667 | xfs_trans_cancel(tp); | |
a8a538b5 DW |
1668 | if (error) |
1669 | goto out_free; | |
10e65503 DW |
1670 | |
1671 | /* Now iterate the list to free the leftovers */ | |
d4e8eb2e | 1672 | list_for_each_entry_safe(rr, n, &debris, rr_list) { |
10e65503 DW |
1673 | /* Set up transaction. */ |
1674 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp); | |
1675 | if (error) | |
1676 | goto out_free; | |
1677 | ||
1678 | trace_xfs_refcount_recover_extent(mp, agno, &rr->rr_rrec); | |
1679 | ||
1680 | /* Free the orphan record */ | |
10e65503 DW |
1681 | agbno = rr->rr_rrec.rc_startblock - XFS_REFC_COW_START; |
1682 | fsb = XFS_AGB_TO_FSB(mp, agno, agbno); | |
21375e5d | 1683 | error = xfs_refcount_free_cow_extent(tp, fsb, |
10e65503 DW |
1684 | rr->rr_rrec.rc_blockcount); |
1685 | if (error) | |
29ae66eb | 1686 | goto out_trans; |
10e65503 DW |
1687 | |
1688 | /* Free the block. */ | |
21375e5d | 1689 | xfs_bmap_add_free(tp, fsb, rr->rr_rrec.rc_blockcount, NULL); |
10e65503 | 1690 | |
10e65503 DW |
1691 | error = xfs_trans_commit(tp); |
1692 | if (error) | |
22a69322 | 1693 | goto out_free; |
d4e8eb2e DW |
1694 | |
1695 | list_del(&rr->rr_list); | |
1696 | kmem_free(rr); | |
10e65503 | 1697 | } |
10e65503 | 1698 | |
d4e8eb2e | 1699 | return error; |
d4e8eb2e DW |
1700 | out_trans: |
1701 | xfs_trans_cancel(tp); | |
10e65503 DW |
1702 | out_free: |
1703 | /* Free the leftover list */ | |
1704 | list_for_each_entry_safe(rr, n, &debris, rr_list) { | |
1705 | list_del(&rr->rr_list); | |
1706 | kmem_free(rr); | |
1707 | } | |
10e65503 | 1708 | return error; |
10e65503 | 1709 | } |
1048d0e2 DW |
1710 | |
1711 | /* Is there a record covering a given extent? */ | |
1712 | int | |
1713 | xfs_refcount_has_record( | |
1714 | struct xfs_btree_cur *cur, | |
1715 | xfs_agblock_t bno, | |
1716 | xfs_extlen_t len, | |
1717 | bool *exists) | |
1718 | { | |
1719 | union xfs_btree_irec low; | |
1720 | union xfs_btree_irec high; | |
1721 | ||
1722 | memset(&low, 0, sizeof(low)); | |
1723 | low.rc.rc_startblock = bno; | |
1724 | memset(&high, 0xFF, sizeof(high)); | |
1725 | high.rc.rc_startblock = bno + len - 1; | |
1726 | ||
1727 | return xfs_btree_has_record(cur, &low, &high, exists); | |
1728 | } |