Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 : /*
3 : * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
4 : * All Rights Reserved.
5 : */
6 : #include "xfs.h"
7 : #include "xfs_fs.h"
8 : #include "xfs_format.h"
9 : #include "xfs_log_format.h"
10 : #include "xfs_trans_resv.h"
11 : #include "xfs_bit.h"
12 : #include "xfs_shared.h"
13 : #include "xfs_mount.h"
14 : #include "xfs_ag.h"
15 : #include "xfs_defer.h"
16 : #include "xfs_trans.h"
17 : #include "xfs_trans_priv.h"
18 : #include "xfs_extfree_item.h"
19 : #include "xfs_log.h"
20 : #include "xfs_btree.h"
21 : #include "xfs_rmap.h"
22 : #include "xfs_alloc.h"
23 : #include "xfs_bmap.h"
24 : #include "xfs_trace.h"
25 : #include "xfs_error.h"
26 : #include "xfs_log_priv.h"
27 : #include "xfs_log_recover.h"
28 : #include "xfs_rtalloc.h"
29 : #include "xfs_inode.h"
30 : #include "xfs_rtbitmap.h"
31 : #include "xfs_rtgroup.h"
32 :
33 : struct kmem_cache *xfs_efi_cache;
34 : struct kmem_cache *xfs_efd_cache;
35 :
36 : static const struct xfs_item_ops xfs_efi_item_ops;
37 :
38 : static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip)
39 : {
40 : return container_of(lip, struct xfs_efi_log_item, efi_item);
41 : }
42 :
43 : STATIC void
44 67057984 : xfs_efi_item_free(
45 : struct xfs_efi_log_item *efip)
46 : {
47 67057984 : kmem_free(efip->efi_item.li_lv_shadow);
48 67057985 : if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
49 0 : kmem_free(efip);
50 : else
51 67057985 : kmem_cache_free(xfs_efi_cache, efip);
52 67057947 : }
53 :
54 : /*
55 : * Freeing the efi requires that we remove it from the AIL if it has already
56 : * been placed there. However, the EFI may not yet have been placed in the AIL
57 : * when called by xfs_efi_release() from EFD processing due to the ordering of
58 : * committed vs unpin operations in bulk insert operations. Hence the reference
59 : * count to ensure only the last caller frees the EFI.
60 : */
61 : STATIC void
62 134107863 : xfs_efi_release(
63 : struct xfs_efi_log_item *efip)
64 : {
65 134107863 : ASSERT(atomic_read(&efip->efi_refcount) > 0);
66 268222357 : if (!atomic_dec_and_test(&efip->efi_refcount))
67 : return;
68 :
69 67057932 : xfs_trans_ail_delete(&efip->efi_item, 0);
70 67057975 : xfs_efi_item_free(efip);
71 : }
72 :
73 : STATIC void
74 66986802 : xfs_efi_item_size(
75 : struct xfs_log_item *lip,
76 : int *nvecs,
77 : int *nbytes)
78 : {
79 66986802 : struct xfs_efi_log_item *efip = EFI_ITEM(lip);
80 :
81 66986802 : *nvecs += 1;
82 66986802 : *nbytes += xfs_efi_log_format_sizeof(efip->efi_format.efi_nextents);
83 66986802 : }
84 :
85 : /*
86 : * This is called to fill in the vector of log iovecs for the
87 : * given efi log item. We use only 1 iovec, and we point that
88 : * at the efi_log_format structure embedded in the efi item.
89 : * It is at this point that we assert that all of the extent
90 : * slots in the efi item have been filled.
91 : */
92 : STATIC void
93 66986730 : xfs_efi_item_format(
94 : struct xfs_log_item *lip,
95 : struct xfs_log_vec *lv)
96 : {
97 66986730 : struct xfs_efi_log_item *efip = EFI_ITEM(lip);
98 66986730 : struct xfs_log_iovec *vecp = NULL;
99 :
100 66986730 : ASSERT(atomic_read(&efip->efi_next_extent) ==
101 : efip->efi_format.efi_nextents);
102 :
103 66986730 : efip->efi_format.efi_type = XFS_LI_EFI;
104 66986730 : efip->efi_format.efi_size = 1;
105 :
106 66986730 : xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
107 66986730 : &efip->efi_format,
108 66986730 : xfs_efi_log_format_sizeof(efip->efi_format.efi_nextents));
109 66986805 : }
110 :
111 :
112 : /*
113 : * The unpin operation is the last place an EFI is manipulated in the log. It is
114 : * either inserted in the AIL or aborted in the event of a log I/O error. In
115 : * either case, the EFI transaction has been successfully committed to make it
116 : * this far. Therefore, we expect whoever committed the EFI to either construct
117 : * and commit the EFD or drop the EFD's reference in the event of error. Simply
118 : * drop the log's EFI reference now that the log is done with it.
119 : */
120 : STATIC void
121 66986690 : xfs_efi_item_unpin(
122 : struct xfs_log_item *lip,
123 : int remove)
124 : {
125 66986690 : struct xfs_efi_log_item *efip = EFI_ITEM(lip);
126 66986690 : xfs_efi_release(efip);
127 66986836 : }
128 :
129 : /*
130 : * The EFI has been either committed or aborted if the transaction has been
131 : * cancelled. If the transaction was cancelled, an EFD isn't going to be
132 : * constructed and thus we free the EFI here directly.
133 : */
134 : STATIC void
135 70589 : xfs_efi_item_release(
136 : struct xfs_log_item *lip)
137 : {
138 70589 : xfs_efi_release(EFI_ITEM(lip));
139 70589 : }
140 :
141 : /*
142 : * Allocate and initialize an efi item with the given number of extents.
143 : */
144 : STATIC struct xfs_efi_log_item *
145 67053623 : xfs_efi_init(
146 : struct xfs_mount *mp,
147 : uint nextents)
148 :
149 : {
150 67053623 : struct xfs_efi_log_item *efip;
151 :
152 67053623 : ASSERT(nextents > 0);
153 67053623 : if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
154 0 : efip = kzalloc(xfs_efi_log_item_sizeof(nextents),
155 : GFP_KERNEL | __GFP_NOFAIL);
156 : } else {
157 67053623 : efip = kmem_cache_zalloc(xfs_efi_cache,
158 : GFP_KERNEL | __GFP_NOFAIL);
159 : }
160 :
161 67052927 : xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
162 67053161 : efip->efi_format.efi_nextents = nextents;
163 67053161 : efip->efi_format.efi_id = (uintptr_t)(void *)efip;
164 67053161 : atomic_set(&efip->efi_next_extent, 0);
165 67053161 : atomic_set(&efip->efi_refcount, 2);
166 :
167 67053161 : return efip;
168 : }
169 :
170 : /*
171 : * Copy an EFI format buffer from the given buf, and into the destination
172 : * EFI format structure.
173 : * The given buffer can be in 32 bit or 64 bit form (which has different padding),
174 : * one of which will be the native format for this kernel.
175 : * It will handle the conversion of formats if necessary.
176 : */
177 : STATIC int
178 71115 : xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
179 : {
180 71115 : xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
181 71115 : uint i;
182 71115 : uint len = xfs_efi_log_format_sizeof(src_efi_fmt->efi_nextents);
183 71115 : uint len32 = xfs_efi_log_format32_sizeof(src_efi_fmt->efi_nextents);
184 71115 : uint len64 = xfs_efi_log_format64_sizeof(src_efi_fmt->efi_nextents);
185 :
186 71115 : if (buf->i_len == len) {
187 142230 : memcpy(dst_efi_fmt, src_efi_fmt,
188 : offsetof(struct xfs_efi_log_format, efi_extents));
189 145976 : for (i = 0; i < src_efi_fmt->efi_nextents; i++)
190 149722 : memcpy(&dst_efi_fmt->efi_extents[i],
191 : &src_efi_fmt->efi_extents[i],
192 : sizeof(struct xfs_extent));
193 : return 0;
194 0 : } else if (buf->i_len == len32) {
195 0 : xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
196 :
197 0 : dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
198 0 : dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
199 0 : dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
200 0 : dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
201 0 : for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
202 0 : dst_efi_fmt->efi_extents[i].ext_start =
203 0 : src_efi_fmt_32->efi_extents[i].ext_start;
204 0 : dst_efi_fmt->efi_extents[i].ext_len =
205 0 : src_efi_fmt_32->efi_extents[i].ext_len;
206 : }
207 : return 0;
208 0 : } else if (buf->i_len == len64) {
209 : xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
210 :
211 : dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
212 : dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
213 : dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
214 : dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
215 : for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
216 : dst_efi_fmt->efi_extents[i].ext_start =
217 : src_efi_fmt_64->efi_extents[i].ext_start;
218 : dst_efi_fmt->efi_extents[i].ext_len =
219 : src_efi_fmt_64->efi_extents[i].ext_len;
220 : }
221 : return 0;
222 : }
223 0 : XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, NULL, buf->i_addr,
224 : buf->i_len);
225 0 : return -EFSCORRUPTED;
226 : }
227 :
228 : static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
229 : {
230 : return container_of(lip, struct xfs_efd_log_item, efd_item);
231 : }
232 :
233 : STATIC void
234 66987196 : xfs_efd_item_free(struct xfs_efd_log_item *efdp)
235 : {
236 66987196 : kmem_free(efdp->efd_item.li_lv_shadow);
237 66987174 : if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
238 0 : kmem_free(efdp);
239 : else
240 66987174 : kmem_cache_free(xfs_efd_cache, efdp);
241 66987202 : }
242 :
243 : STATIC void
244 66986546 : xfs_efd_item_size(
245 : struct xfs_log_item *lip,
246 : int *nvecs,
247 : int *nbytes)
248 : {
249 66986546 : struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
250 :
251 66986546 : *nvecs += 1;
252 66986546 : *nbytes += xfs_efd_log_format_sizeof(efdp->efd_format.efd_nextents);
253 66986546 : }
254 :
255 : /*
256 : * This is called to fill in the vector of log iovecs for the
257 : * given efd log item. We use only 1 iovec, and we point that
258 : * at the efd_log_format structure embedded in the efd item.
259 : * It is at this point that we assert that all of the extent
260 : * slots in the efd item have been filled.
261 : */
262 : STATIC void
263 523562 : xfs_efd_item_format(
264 : struct xfs_log_item *lip,
265 : struct xfs_log_vec *lv)
266 : {
267 523562 : struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
268 523562 : struct xfs_log_iovec *vecp = NULL;
269 :
270 523562 : ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
271 :
272 523562 : efdp->efd_format.efd_type = XFS_LI_EFD;
273 523562 : efdp->efd_format.efd_size = 1;
274 :
275 523562 : xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
276 523562 : &efdp->efd_format,
277 523562 : xfs_efd_log_format_sizeof(efdp->efd_format.efd_nextents));
278 523567 : }
279 :
280 : /*
281 : * The EFD is either committed or aborted if the transaction is cancelled. If
282 : * the transaction is cancelled, drop our reference to the EFI and free the EFD.
283 : */
284 : STATIC void
285 66987187 : xfs_efd_item_release(
286 : struct xfs_log_item *lip)
287 : {
288 66987187 : struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
289 :
290 66987187 : xfs_efi_release(efdp->efd_efip);
291 66987203 : xfs_efd_item_free(efdp);
292 66987193 : }
293 :
294 : static struct xfs_log_item *
295 66576370 : xfs_efd_item_intent(
296 : struct xfs_log_item *lip)
297 : {
298 66576370 : return &EFD_ITEM(lip)->efd_efip->efi_item;
299 : }
300 :
301 : static const struct xfs_item_ops xfs_efd_item_ops = {
302 : .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED |
303 : XFS_ITEM_INTENT_DONE,
304 : .iop_size = xfs_efd_item_size,
305 : .iop_format = xfs_efd_item_format,
306 : .iop_release = xfs_efd_item_release,
307 : .iop_intent = xfs_efd_item_intent,
308 : };
309 :
310 : /*
311 : * Allocate an "extent free done" log item that will hold nextents worth of
312 : * extents. The caller must use all nextents extents, because we are not
313 : * flexible about this at all.
314 : */
315 : static struct xfs_efd_log_item *
316 66986834 : xfs_trans_get_efd(
317 : struct xfs_trans *tp,
318 : struct xfs_efi_log_item *efip,
319 : unsigned int nextents)
320 : {
321 66986834 : struct xfs_efd_log_item *efdp;
322 :
323 66986834 : ASSERT(nextents > 0);
324 :
325 66986834 : if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
326 0 : efdp = kzalloc(xfs_efd_log_item_sizeof(nextents),
327 : GFP_KERNEL | __GFP_NOFAIL);
328 : } else {
329 66986834 : efdp = kmem_cache_zalloc(xfs_efd_cache,
330 : GFP_KERNEL | __GFP_NOFAIL);
331 : }
332 :
333 66986901 : xfs_log_item_init(tp->t_mountp, &efdp->efd_item, XFS_LI_EFD,
334 : &xfs_efd_item_ops);
335 66987141 : efdp->efd_efip = efip;
336 66987141 : efdp->efd_format.efd_nextents = nextents;
337 66987141 : efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
338 :
339 66987141 : xfs_trans_add_item(tp, &efdp->efd_item);
340 66987098 : return efdp;
341 : }
342 :
343 : /*
344 : * Fill the EFD with all extents from the EFI when we need to roll the
345 : * transaction and continue with a new EFI.
346 : *
347 : * This simply copies all the extents in the EFI to the EFD rather than make
348 : * assumptions about which extents in the EFI have already been processed. We
349 : * currently keep the xefi list in the same order as the EFI extent list, but
350 : * that may not always be the case. Copying everything avoids leaving a landmine
351 : * were we fail to cancel all the extents in an EFI if the xefi list is
352 : * processed in a different order to the extents in the EFI.
353 : */
354 : static void
355 0 : xfs_efd_from_efi(
356 : struct xfs_efd_log_item *efdp)
357 : {
358 0 : struct xfs_efi_log_item *efip = efdp->efd_efip;
359 0 : uint i;
360 :
361 0 : ASSERT(efip->efi_format.efi_nextents > 0);
362 0 : ASSERT(efdp->efd_next_extent < efip->efi_format.efi_nextents);
363 :
364 0 : for (i = 0; i < efip->efi_format.efi_nextents; i++) {
365 0 : efdp->efd_format.efd_extents[i] =
366 : efip->efi_format.efi_extents[i];
367 : }
368 0 : efdp->efd_next_extent = efip->efi_format.efi_nextents;
369 0 : }
370 :
371 : /*
372 : * Free an extent and log it to the EFD. Note that the transaction is marked
373 : * dirty regardless of whether the extent free succeeds or fails to support the
374 : * EFI/EFD lifecycle rules.
375 : */
376 : static int
377 68646580 : xfs_trans_free_extent(
378 : struct xfs_trans *tp,
379 : struct xfs_efd_log_item *efdp,
380 : struct xfs_extent_free_item *xefi)
381 : {
382 68646580 : struct xfs_owner_info oinfo = { };
383 68646580 : struct xfs_mount *mp = tp->t_mountp;
384 68646580 : struct xfs_extent *extp;
385 68646580 : uint next_extent;
386 68646580 : xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp,
387 : xefi->xefi_startblock);
388 68646580 : int error;
389 :
390 68646580 : oinfo.oi_owner = xefi->xefi_owner;
391 68646580 : if (xefi->xefi_flags & XFS_EFI_ATTR_FORK)
392 43 : oinfo.oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
393 68646580 : if (xefi->xefi_flags & XFS_EFI_BMBT_BLOCK)
394 1010427 : oinfo.oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK;
395 :
396 68646580 : trace_xfs_extent_free_deferred(mp, xefi);
397 :
398 68646532 : if (xfs_efi_is_realtime(xefi)) {
399 15822390 : ASSERT(xefi->xefi_owner == XFS_RMAP_OWN_NULL ||
400 : xefi->xefi_owner == XFS_RMAP_OWN_UNKNOWN);
401 :
402 15822390 : error = xfs_rtfree_blocks(tp, xefi->xefi_startblock,
403 15822390 : xefi->xefi_blockcount);
404 : } else {
405 52824142 : error = __xfs_free_extent(tp, xefi->xefi_pag, agbno,
406 : xefi->xefi_blockcount, &oinfo,
407 : xefi->xefi_agresv,
408 : xefi->xefi_flags & XFS_EFI_SKIP_DISCARD);
409 : }
410 :
411 : /*
412 : * Mark the transaction dirty, even on error. This ensures the
413 : * transaction is aborted, which:
414 : *
415 : * 1.) releases the EFI and frees the EFD
416 : * 2.) shuts down the filesystem
417 : */
418 68646183 : tp->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE;
419 68646183 : set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
420 :
421 : /*
422 : * If we need a new transaction to make progress, the caller will log a
423 : * new EFI with the current contents. It will also log an EFD to cancel
424 : * the existing EFI, and so we need to copy all the unprocessed extents
425 : * in this EFI to the EFD so this works correctly.
426 : */
427 68646252 : if (error == -EAGAIN) {
428 0 : xfs_efd_from_efi(efdp);
429 0 : return error;
430 : }
431 :
432 68646252 : next_extent = efdp->efd_next_extent;
433 68646252 : ASSERT(next_extent < efdp->efd_format.efd_nextents);
434 68646252 : extp = &(efdp->efd_format.efd_extents[next_extent]);
435 68646252 : extp->ext_start = xefi->xefi_startblock;
436 68646252 : extp->ext_len = xefi->xefi_blockcount;
437 68646252 : efdp->efd_next_extent++;
438 :
439 68646252 : return error;
440 : }
441 :
442 : /* Sort bmap items by AG. */
443 : static int
444 4357593 : xfs_extent_free_diff_items(
445 : void *priv,
446 : const struct list_head *a,
447 : const struct list_head *b)
448 : {
449 4357593 : struct xfs_extent_free_item *ra;
450 4357593 : struct xfs_extent_free_item *rb;
451 :
452 4357593 : ra = container_of(a, struct xfs_extent_free_item, xefi_list);
453 4357593 : rb = container_of(b, struct xfs_extent_free_item, xefi_list);
454 :
455 4357593 : ASSERT(xfs_efi_is_realtime(ra) == xfs_efi_is_realtime(rb));
456 :
457 4357593 : if (xfs_efi_is_realtime(ra))
458 17662 : return ra->xefi_rtg->rtg_rgno - rb->xefi_rtg->rtg_rgno;
459 :
460 4339931 : return ra->xefi_pag->pag_agno - rb->xefi_pag->pag_agno;
461 : }
462 :
463 : /* Log a free extent to the intent item. */
464 : STATIC void
465 69077469 : xfs_extent_free_log_item(
466 : struct xfs_trans *tp,
467 : struct xfs_efi_log_item *efip,
468 : struct xfs_extent_free_item *xefi)
469 : {
470 69077469 : uint next_extent;
471 69077469 : struct xfs_extent *extp;
472 :
473 69077469 : tp->t_flags |= XFS_TRANS_DIRTY;
474 69077469 : set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
475 :
476 : /*
477 : * atomic_inc_return gives us the value after the increment;
478 : * we want to use it as an array index so we need to subtract 1 from
479 : * it.
480 : */
481 69077443 : next_extent = atomic_inc_return(&efip->efi_next_extent) - 1;
482 69078414 : ASSERT(next_extent < efip->efi_format.efi_nextents);
483 69078414 : extp = &efip->efi_format.efi_extents[next_extent];
484 69078414 : extp->ext_start = xefi->xefi_startblock;
485 69078414 : extp->ext_len = xefi->xefi_blockcount;
486 69078414 : if (xfs_efi_is_realtime(xefi))
487 15822368 : extp->ext_len |= XFS_EFI_EXTLEN_REALTIME_EXT;
488 69078414 : }
489 :
490 : static struct xfs_log_item *
491 66977952 : xfs_extent_free_create_intent(
492 : struct xfs_trans *tp,
493 : struct list_head *items,
494 : unsigned int count,
495 : bool sort)
496 : {
497 66977952 : struct xfs_mount *mp = tp->t_mountp;
498 66977952 : struct xfs_efi_log_item *efip = xfs_efi_init(mp, count);
499 66974560 : struct xfs_extent_free_item *xefi;
500 :
501 66974560 : ASSERT(count > 0);
502 :
503 66974560 : xfs_trans_add_item(tp, &efip->efi_item);
504 66980678 : if (sort)
505 66740997 : list_sort(mp, items, xfs_extent_free_diff_items);
506 136058792 : list_for_each_entry(xefi, items, xefi_list)
507 69078282 : xfs_extent_free_log_item(tp, efip, xefi);
508 66980510 : return &efip->efi_item;
509 : }
510 :
511 : /* Get an EFD so we can process all the free extents. */
512 : static struct xfs_log_item *
513 66981676 : xfs_extent_free_create_done(
514 : struct xfs_trans *tp,
515 : struct xfs_log_item *intent,
516 : unsigned int count)
517 : {
518 66981676 : return &xfs_trans_get_efd(tp, EFI_ITEM(intent), count)->efd_item;
519 : }
520 :
521 : /* Take a passive ref to the AG containing the space we're freeing. */
522 : void
523 68839492 : xfs_extent_free_get_group(
524 : struct xfs_mount *mp,
525 : struct xfs_extent_free_item *xefi)
526 : {
527 68839492 : xfs_agnumber_t agno;
528 :
529 68839492 : if (xfs_efi_is_realtime(xefi)) {
530 15822337 : xfs_rgnumber_t rgno;
531 :
532 15822337 : rgno = xfs_rtb_to_rgno(mp, xefi->xefi_startblock);
533 15822344 : xefi->xefi_rtg = xfs_rtgroup_intent_get(mp, rgno);
534 15822326 : return;
535 : }
536 :
537 53017155 : agno = XFS_FSB_TO_AGNO(mp, xefi->xefi_startblock);
538 53017155 : xefi->xefi_pag = xfs_perag_intent_get(mp, agno);
539 : }
540 :
541 : /* Release a passive AG ref after some freeing work. */
542 : static inline void
543 68840547 : xfs_extent_free_put_group(
544 : struct xfs_extent_free_item *xefi)
545 : {
546 68840547 : if (xfs_efi_is_realtime(xefi)) {
547 15822390 : xfs_rtgroup_intent_put(xefi->xefi_rtg);
548 15822390 : return;
549 : }
550 :
551 53018157 : xfs_perag_intent_put(xefi->xefi_pag);
552 : }
553 :
554 : /* Process a free extent. */
555 : STATIC int
556 68645617 : xfs_extent_free_finish_item(
557 : struct xfs_trans *tp,
558 : struct xfs_log_item *done,
559 : struct list_head *item,
560 : struct xfs_btree_cur **state)
561 : {
562 68645617 : struct xfs_extent_free_item *xefi;
563 68645617 : int error;
564 :
565 68645617 : xefi = container_of(item, struct xfs_extent_free_item, xefi_list);
566 :
567 : /*
568 : * Lock the rt bitmap if we've any realtime extents to free and we
569 : * haven't locked the rt inodes yet.
570 : */
571 68645617 : if (*state == NULL && xfs_efi_is_realtime(xefi)) {
572 15804868 : xfs_rtbitmap_lock(tp, tp->t_mountp);
573 15804872 : *state = (struct xfs_btree_cur *)1;
574 : }
575 :
576 68645621 : error = xfs_trans_free_extent(tp, EFD_ITEM(done), xefi);
577 :
578 : /*
579 : * Don't free the XEFI if we need a new transaction to complete
580 : * processing of it.
581 : */
582 68645699 : if (error == -EAGAIN)
583 : return error;
584 :
585 68645699 : xfs_extent_free_put_group(xefi);
586 68645854 : kmem_cache_free(xfs_extfree_item_cache, xefi);
587 68645854 : return error;
588 : }
589 :
590 : /* Abort all pending EFIs. */
591 : STATIC void
592 176 : xfs_extent_free_abort_intent(
593 : struct xfs_log_item *intent)
594 : {
595 176 : xfs_efi_release(EFI_ITEM(intent));
596 176 : }
597 :
598 : /* Cancel a free extent. */
599 : STATIC void
600 177 : xfs_extent_free_cancel_item(
601 : struct list_head *item)
602 : {
603 177 : struct xfs_extent_free_item *xefi;
604 :
605 177 : xefi = container_of(item, struct xfs_extent_free_item, xefi_list);
606 :
607 177 : xfs_extent_free_put_group(xefi);
608 177 : kmem_cache_free(xfs_extfree_item_cache, xefi);
609 177 : }
610 :
611 : const struct xfs_defer_op_type xfs_extent_free_defer_type = {
612 : .max_items = XFS_EFI_MAX_FAST_EXTENTS,
613 : .create_intent = xfs_extent_free_create_intent,
614 : .abort_intent = xfs_extent_free_abort_intent,
615 : .create_done = xfs_extent_free_create_done,
616 : .finish_item = xfs_extent_free_finish_item,
617 : .cancel_item = xfs_extent_free_cancel_item,
618 : };
619 :
620 : /*
621 : * AGFL blocks are accounted differently in the reserve pools and are not
622 : * inserted into the busy extent list.
623 : */
624 : STATIC int
625 194204 : xfs_agfl_free_finish_item(
626 : struct xfs_trans *tp,
627 : struct xfs_log_item *done,
628 : struct list_head *item,
629 : struct xfs_btree_cur **state)
630 : {
631 194204 : struct xfs_owner_info oinfo = { };
632 194204 : struct xfs_mount *mp = tp->t_mountp;
633 194204 : struct xfs_efd_log_item *efdp = EFD_ITEM(done);
634 194204 : struct xfs_extent_free_item *xefi;
635 194204 : struct xfs_extent *extp;
636 194204 : struct xfs_buf *agbp;
637 194204 : int error;
638 194204 : xfs_agblock_t agbno;
639 194204 : uint next_extent;
640 :
641 194204 : xefi = container_of(item, struct xfs_extent_free_item, xefi_list);
642 194204 : ASSERT(xefi->xefi_blockcount == 1);
643 194204 : ASSERT(!xfs_efi_is_realtime(xefi));
644 194204 : agbno = XFS_FSB_TO_AGBNO(mp, xefi->xefi_startblock);
645 194204 : oinfo.oi_owner = xefi->xefi_owner;
646 :
647 194204 : trace_xfs_agfl_free_deferred(mp, xefi);
648 :
649 194204 : error = xfs_alloc_read_agf(xefi->xefi_pag, tp, 0, &agbp);
650 194204 : if (!error)
651 194204 : error = xfs_free_agfl_block(tp, xefi->xefi_pag->pag_agno,
652 : agbno, agbp, &oinfo);
653 :
654 : /*
655 : * Mark the transaction dirty, even on error. This ensures the
656 : * transaction is aborted, which:
657 : *
658 : * 1.) releases the EFI and frees the EFD
659 : * 2.) shuts down the filesystem
660 : */
661 194204 : tp->t_flags |= XFS_TRANS_DIRTY;
662 194204 : set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
663 :
664 194204 : next_extent = efdp->efd_next_extent;
665 194204 : ASSERT(next_extent < efdp->efd_format.efd_nextents);
666 194204 : extp = &(efdp->efd_format.efd_extents[next_extent]);
667 194204 : extp->ext_start = xefi->xefi_startblock;
668 194204 : extp->ext_len = xefi->xefi_blockcount;
669 194204 : efdp->efd_next_extent++;
670 :
671 194204 : xfs_extent_free_put_group(xefi);
672 194204 : kmem_cache_free(xfs_extfree_item_cache, xefi);
673 194204 : return error;
674 : }
675 :
676 : /* sub-type with special handling for AGFL deferred frees */
677 : const struct xfs_defer_op_type xfs_agfl_free_defer_type = {
678 : .max_items = XFS_EFI_MAX_FAST_EXTENTS,
679 : .create_intent = xfs_extent_free_create_intent,
680 : .abort_intent = xfs_extent_free_abort_intent,
681 : .create_done = xfs_extent_free_create_done,
682 : .finish_item = xfs_agfl_free_finish_item,
683 : .cancel_item = xfs_extent_free_cancel_item,
684 : };
685 :
686 : /* Is this recovered EFI ok? */
687 : static inline bool
688 545 : xfs_efi_validate_ext(
689 : struct xfs_mount *mp,
690 : struct xfs_extent *extp)
691 : {
692 545 : if (extp->ext_len & XFS_EFI_EXTLEN_REALTIME_EXT)
693 0 : return xfs_verify_rtbext(mp, extp->ext_start,
694 : extp->ext_len & ~XFS_EFI_EXTLEN_REALTIME_EXT);
695 :
696 545 : return xfs_verify_fsbext(mp, extp->ext_start, extp->ext_len);
697 : }
698 :
699 : /*
700 : * Process an extent free intent item that was recovered from
701 : * the log. We need to free the extents that it describes.
702 : */
703 : STATIC int
704 531 : xfs_efi_item_recover(
705 : struct xfs_log_item *lip,
706 : struct list_head *capture_list)
707 : {
708 531 : struct xfs_efi_log_item *efip = EFI_ITEM(lip);
709 531 : struct xfs_mount *mp = lip->li_log->l_mp;
710 531 : struct xfs_efd_log_item *efdp;
711 531 : struct xfs_trans *tp;
712 531 : int i;
713 531 : int error = 0;
714 531 : bool requeue_only = false;
715 :
716 : /*
717 : * First check the validity of the extents described by the
718 : * EFI. If any are bad, then assume that all are bad and
719 : * just toss the EFI.
720 : */
721 1076 : for (i = 0; i < efip->efi_format.efi_nextents; i++) {
722 545 : if (!xfs_efi_validate_ext(mp,
723 545 : &efip->efi_format.efi_extents[i])) {
724 0 : XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
725 : &efip->efi_format,
726 : sizeof(efip->efi_format));
727 0 : return -EFSCORRUPTED;
728 : }
729 : }
730 :
731 531 : error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
732 531 : if (error)
733 : return error;
734 531 : efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
735 :
736 : /* Lock the rt bitmap if we've any realtime extents to free. */
737 1607 : for (i = 0; i < efip->efi_format.efi_nextents; i++) {
738 545 : struct xfs_extent *extp;
739 :
740 545 : extp = &efip->efi_format.efi_extents[i];
741 545 : if (extp->ext_len & XFS_EFI_EXTLEN_REALTIME_EXT) {
742 0 : xfs_rtbitmap_lock(tp, mp);
743 0 : break;
744 : }
745 : }
746 :
747 1076 : for (i = 0; i < efip->efi_format.efi_nextents; i++) {
748 545 : struct xfs_extent_free_item fake = {
749 : .xefi_owner = XFS_RMAP_OWN_UNKNOWN,
750 : .xefi_agresv = XFS_AG_RESV_NONE,
751 : };
752 545 : struct xfs_extent *extp;
753 545 : unsigned int len;
754 :
755 545 : extp = &efip->efi_format.efi_extents[i];
756 :
757 545 : fake.xefi_startblock = extp->ext_start;
758 545 : len = extp->ext_len;
759 545 : if (len & XFS_EFI_EXTLEN_REALTIME_EXT) {
760 0 : len &= ~XFS_EFI_EXTLEN_REALTIME_EXT;
761 0 : fake.xefi_flags |= XFS_EFI_REALTIME;
762 : }
763 545 : fake.xefi_blockcount = len;
764 :
765 545 : if (!requeue_only) {
766 545 : xfs_extent_free_get_group(mp, &fake);
767 545 : error = xfs_trans_free_extent(tp, efdp, &fake);
768 545 : xfs_extent_free_put_group(&fake);
769 : }
770 :
771 : /*
772 : * If we can't free the extent without potentially deadlocking,
773 : * requeue the rest of the extents to a new so that they get
774 : * run again later with a new transaction context.
775 : */
776 545 : if (error == -EAGAIN || requeue_only) {
777 0 : error = xfs_free_extent_later(tp, fake.xefi_startblock,
778 0 : fake.xefi_blockcount,
779 : &XFS_RMAP_OINFO_ANY_OWNER,
780 : fake.xefi_agresv, 0);
781 0 : if (!error) {
782 0 : requeue_only = true;
783 0 : continue;
784 : }
785 : }
786 :
787 545 : if (error == -EFSCORRUPTED)
788 0 : XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
789 : extp, sizeof(*extp));
790 545 : if (error)
791 0 : goto abort_error;
792 :
793 : }
794 :
795 531 : return xfs_defer_ops_capture_and_commit(tp, capture_list);
796 :
797 : abort_error:
798 0 : xfs_trans_cancel(tp);
799 0 : return error;
800 : }
801 :
802 : STATIC bool
803 70802 : xfs_efi_item_match(
804 : struct xfs_log_item *lip,
805 : uint64_t intent_id)
806 : {
807 70802 : return EFI_ITEM(lip)->efi_format.efi_id == intent_id;
808 : }
809 :
810 : /* Relog an intent item to push the log tail forward. */
811 : static struct xfs_log_item *
812 4591 : xfs_efi_item_relog(
813 : struct xfs_log_item *intent,
814 : struct xfs_trans *tp)
815 : {
816 4591 : struct xfs_efd_log_item *efdp;
817 4591 : struct xfs_efi_log_item *efip;
818 4591 : struct xfs_extent *extp;
819 4591 : unsigned int count;
820 :
821 4591 : count = EFI_ITEM(intent)->efi_format.efi_nextents;
822 4591 : extp = EFI_ITEM(intent)->efi_format.efi_extents;
823 :
824 4591 : tp->t_flags |= XFS_TRANS_DIRTY;
825 4591 : efdp = xfs_trans_get_efd(tp, EFI_ITEM(intent), count);
826 4591 : efdp->efd_next_extent = count;
827 9182 : memcpy(efdp->efd_format.efd_extents, extp, count * sizeof(*extp));
828 4591 : set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
829 :
830 4591 : efip = xfs_efi_init(tp->t_mountp, count);
831 9182 : memcpy(efip->efi_format.efi_extents, extp, count * sizeof(*extp));
832 4591 : atomic_set(&efip->efi_next_extent, count);
833 4591 : xfs_trans_add_item(tp, &efip->efi_item);
834 4591 : set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
835 4591 : return &efip->efi_item;
836 : }
837 :
838 : static const struct xfs_item_ops xfs_efi_item_ops = {
839 : .flags = XFS_ITEM_INTENT,
840 : .iop_size = xfs_efi_item_size,
841 : .iop_format = xfs_efi_item_format,
842 : .iop_unpin = xfs_efi_item_unpin,
843 : .iop_release = xfs_efi_item_release,
844 : .iop_recover = xfs_efi_item_recover,
845 : .iop_match = xfs_efi_item_match,
846 : .iop_relog = xfs_efi_item_relog,
847 : };
848 :
849 : /*
850 : * This routine is called to create an in-core extent free intent
851 : * item from the efi format structure which was logged on disk.
852 : * It allocates an in-core efi, copies the extents from the format
853 : * structure into it, and adds the efi to the AIL with the given
854 : * LSN.
855 : */
856 : STATIC int
857 71115 : xlog_recover_efi_commit_pass2(
858 : struct xlog *log,
859 : struct list_head *buffer_list,
860 : struct xlog_recover_item *item,
861 : xfs_lsn_t lsn)
862 : {
863 71115 : struct xfs_mount *mp = log->l_mp;
864 71115 : struct xfs_efi_log_item *efip;
865 71115 : struct xfs_efi_log_format *efi_formatp;
866 71115 : int error;
867 :
868 71115 : efi_formatp = item->ri_buf[0].i_addr;
869 :
870 71115 : if (item->ri_buf[0].i_len < xfs_efi_log_format_sizeof(0)) {
871 0 : XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
872 : item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
873 0 : return -EFSCORRUPTED;
874 : }
875 :
876 71115 : efip = xfs_efi_init(mp, efi_formatp->efi_nextents);
877 71115 : error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
878 71115 : if (error) {
879 0 : xfs_efi_item_free(efip);
880 0 : return error;
881 : }
882 71115 : atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
883 : /*
884 : * Insert the intent into the AIL directly and drop one reference so
885 : * that finishing or canceling the work will drop the other.
886 : */
887 71115 : xfs_trans_ail_insert(log->l_ailp, &efip->efi_item, lsn);
888 71115 : xfs_efi_release(efip);
889 71115 : return 0;
890 : }
891 :
892 : const struct xlog_recover_item_ops xlog_efi_item_ops = {
893 : .item_type = XFS_LI_EFI,
894 : .commit_pass2 = xlog_recover_efi_commit_pass2,
895 : };
896 :
897 : /*
898 : * This routine is called when an EFD format structure is found in a committed
899 : * transaction in the log. Its purpose is to cancel the corresponding EFI if it
900 : * was still in the log. To do this it searches the AIL for the EFI with an id
901 : * equal to that in the EFD format structure. If we find it we drop the EFD
902 : * reference, which removes the EFI from the AIL and frees it.
903 : */
904 : STATIC int
905 70623 : xlog_recover_efd_commit_pass2(
906 : struct xlog *log,
907 : struct list_head *buffer_list,
908 : struct xlog_recover_item *item,
909 : xfs_lsn_t lsn)
910 : {
911 70623 : struct xfs_efd_log_format *efd_formatp;
912 70623 : int buflen = item->ri_buf[0].i_len;
913 :
914 70623 : efd_formatp = item->ri_buf[0].i_addr;
915 :
916 70623 : if (buflen < sizeof(struct xfs_efd_log_format)) {
917 0 : XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
918 : efd_formatp, buflen);
919 0 : return -EFSCORRUPTED;
920 : }
921 :
922 70623 : if (item->ri_buf[0].i_len != xfs_efd_log_format32_sizeof(
923 70623 : efd_formatp->efd_nextents) &&
924 : item->ri_buf[0].i_len != xfs_efd_log_format64_sizeof(
925 : efd_formatp->efd_nextents)) {
926 0 : XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
927 : efd_formatp, buflen);
928 0 : return -EFSCORRUPTED;
929 : }
930 :
931 70623 : xlog_recover_release_intent(log, XFS_LI_EFI, efd_formatp->efd_efi_id);
932 70623 : return 0;
933 : }
934 :
935 : const struct xlog_recover_item_ops xlog_efd_item_ops = {
936 : .item_type = XFS_LI_EFD,
937 : .commit_pass2 = xlog_recover_efd_commit_pass2,
938 : };
|