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 141791927 : xfs_efi_item_free(
45 : struct xfs_efi_log_item *efip)
46 : {
47 141791927 : kmem_free(efip->efi_item.li_lv_shadow);
48 141792037 : if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
49 0 : kmem_free(efip);
50 : else
51 141792037 : kmem_cache_free(xfs_efi_cache, efip);
52 141791697 : }
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 283576160 : xfs_efi_release(
63 : struct xfs_efi_log_item *efip)
64 : {
65 283576160 : ASSERT(atomic_read(&efip->efi_refcount) > 0);
66 283576160 : if (!atomic_dec_and_test(&efip->efi_refcount))
67 : return;
68 :
69 141791605 : xfs_trans_ail_delete(&efip->efi_item, 0);
70 141792100 : xfs_efi_item_free(efip);
71 : }
72 :
73 : STATIC void
74 141714583 : xfs_efi_item_size(
75 : struct xfs_log_item *lip,
76 : int *nvecs,
77 : int *nbytes)
78 : {
79 141714583 : struct xfs_efi_log_item *efip = EFI_ITEM(lip);
80 :
81 141714583 : *nvecs += 1;
82 141714583 : *nbytes += xfs_efi_log_format_sizeof(efip->efi_format.efi_nextents);
83 141714583 : }
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 141711466 : xfs_efi_item_format(
94 : struct xfs_log_item *lip,
95 : struct xfs_log_vec *lv)
96 : {
97 141711466 : struct xfs_efi_log_item *efip = EFI_ITEM(lip);
98 141711466 : struct xfs_log_iovec *vecp = NULL;
99 :
100 141711466 : ASSERT(atomic_read(&efip->efi_next_extent) ==
101 : efip->efi_format.efi_nextents);
102 :
103 141711466 : efip->efi_format.efi_type = XFS_LI_EFI;
104 141711466 : efip->efi_format.efi_size = 1;
105 :
106 141711466 : xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
107 141711466 : &efip->efi_format,
108 141711466 : xfs_efi_log_format_sizeof(efip->efi_format.efi_nextents));
109 141701436 : }
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 141716705 : xfs_efi_item_unpin(
122 : struct xfs_log_item *lip,
123 : int remove)
124 : {
125 141716705 : struct xfs_efi_log_item *efip = EFI_ITEM(lip);
126 141716705 : xfs_efi_release(efip);
127 141718817 : }
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 71667 : xfs_efi_item_release(
136 : struct xfs_log_item *lip)
137 : {
138 71667 : xfs_efi_release(EFI_ITEM(lip));
139 71667 : }
140 :
141 : /*
142 : * Allocate and initialize an efi item with the given number of extents.
143 : */
144 : STATIC struct xfs_efi_log_item *
145 141679713 : xfs_efi_init(
146 : struct xfs_mount *mp,
147 : uint nextents)
148 :
149 : {
150 141679713 : struct xfs_efi_log_item *efip;
151 :
152 141679713 : ASSERT(nextents > 0);
153 141679713 : 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 141679713 : efip = kmem_cache_zalloc(xfs_efi_cache,
158 : GFP_KERNEL | __GFP_NOFAIL);
159 : }
160 :
161 141764727 : xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
162 141736934 : efip->efi_format.efi_nextents = nextents;
163 141736934 : efip->efi_format.efi_id = (uintptr_t)(void *)efip;
164 141736934 : atomic_set(&efip->efi_next_extent, 0);
165 141736934 : atomic_set(&efip->efi_refcount, 2);
166 :
167 141736934 : 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 72335 : xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
179 : {
180 72335 : xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
181 72335 : uint i;
182 72335 : uint len = xfs_efi_log_format_sizeof(src_efi_fmt->efi_nextents);
183 72335 : uint len32 = xfs_efi_log_format32_sizeof(src_efi_fmt->efi_nextents);
184 72335 : uint len64 = xfs_efi_log_format64_sizeof(src_efi_fmt->efi_nextents);
185 :
186 72335 : if (buf->i_len == len) {
187 144670 : memcpy(dst_efi_fmt, src_efi_fmt,
188 : offsetof(struct xfs_efi_log_format, efi_extents));
189 154495 : for (i = 0; i < src_efi_fmt->efi_nextents; i++)
190 164320 : 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 141719184 : xfs_efd_item_free(struct xfs_efd_log_item *efdp)
235 : {
236 141719184 : kmem_free(efdp->efd_item.li_lv_shadow);
237 141718579 : if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
238 0 : kmem_free(efdp);
239 : else
240 141718579 : kmem_cache_free(xfs_efd_cache, efdp);
241 141717108 : }
242 :
243 : STATIC void
244 141712592 : xfs_efd_item_size(
245 : struct xfs_log_item *lip,
246 : int *nvecs,
247 : int *nbytes)
248 : {
249 141712592 : struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
250 :
251 141712592 : *nvecs += 1;
252 141712592 : *nbytes += xfs_efd_log_format_sizeof(efdp->efd_format.efd_nextents);
253 141712592 : }
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 1034323 : xfs_efd_item_format(
264 : struct xfs_log_item *lip,
265 : struct xfs_log_vec *lv)
266 : {
267 1034323 : struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
268 1034323 : struct xfs_log_iovec *vecp = NULL;
269 :
270 1034323 : ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
271 :
272 1034323 : efdp->efd_format.efd_type = XFS_LI_EFD;
273 1034323 : efdp->efd_format.efd_size = 1;
274 :
275 1034323 : xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
276 1034323 : &efdp->efd_format,
277 1034323 : xfs_efd_log_format_sizeof(efdp->efd_format.efd_nextents));
278 1033746 : }
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 141718847 : xfs_efd_item_release(
286 : struct xfs_log_item *lip)
287 : {
288 141718847 : struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
289 :
290 141718847 : xfs_efi_release(efdp->efd_efip);
291 141719422 : xfs_efd_item_free(efdp);
292 141717166 : }
293 :
294 : static struct xfs_log_item *
295 140885981 : xfs_efd_item_intent(
296 : struct xfs_log_item *lip)
297 : {
298 140885981 : 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 141713721 : xfs_trans_get_efd(
317 : struct xfs_trans *tp,
318 : struct xfs_efi_log_item *efip,
319 : unsigned int nextents)
320 : {
321 141713721 : struct xfs_efd_log_item *efdp;
322 :
323 141713721 : ASSERT(nextents > 0);
324 :
325 141713721 : 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 141713721 : efdp = kmem_cache_zalloc(xfs_efd_cache,
330 : GFP_KERNEL | __GFP_NOFAIL);
331 : }
332 :
333 141717808 : xfs_log_item_init(tp->t_mountp, &efdp->efd_item, XFS_LI_EFD,
334 : &xfs_efd_item_ops);
335 141711355 : efdp->efd_efip = efip;
336 141711355 : efdp->efd_format.efd_nextents = nextents;
337 141711355 : efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
338 :
339 141711355 : xfs_trans_add_item(tp, &efdp->efd_item);
340 141714493 : 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 149471438 : 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 149471438 : struct xfs_owner_info oinfo = { };
383 149471438 : struct xfs_mount *mp = tp->t_mountp;
384 149471438 : struct xfs_extent *extp;
385 149471438 : uint next_extent;
386 149471438 : xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp,
387 : xefi->xefi_startblock);
388 149466074 : int error;
389 :
390 149466074 : oinfo.oi_owner = xefi->xefi_owner;
391 149466074 : if (xefi->xefi_flags & XFS_EFI_ATTR_FORK)
392 268 : oinfo.oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
393 149466074 : if (xefi->xefi_flags & XFS_EFI_BMBT_BLOCK)
394 2838212 : oinfo.oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK;
395 :
396 149466074 : trace_xfs_extent_free_deferred(mp, xefi);
397 :
398 149466002 : if (xfs_efi_is_realtime(xefi)) {
399 43898236 : ASSERT(xefi->xefi_owner == XFS_RMAP_OWN_NULL ||
400 : xefi->xefi_owner == XFS_RMAP_OWN_UNKNOWN);
401 :
402 43898236 : error = xfs_rtfree_blocks(tp, xefi->xefi_startblock,
403 43898236 : xefi->xefi_blockcount);
404 : } else {
405 105567766 : 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 149465220 : tp->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE;
419 149465220 : 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 149470030 : if (error == -EAGAIN) {
428 0 : xfs_efd_from_efi(efdp);
429 0 : return error;
430 : }
431 :
432 149470030 : next_extent = efdp->efd_next_extent;
433 149470030 : ASSERT(next_extent < efdp->efd_format.efd_nextents);
434 149470030 : extp = &(efdp->efd_format.efd_extents[next_extent]);
435 149470030 : extp->ext_start = xefi->xefi_startblock;
436 149470030 : extp->ext_len = xefi->xefi_blockcount;
437 149470030 : efdp->efd_next_extent++;
438 :
439 149470030 : return error;
440 : }
441 :
442 : /* Sort bmap items by AG. */
443 : static int
444 18085577 : xfs_extent_free_diff_items(
445 : void *priv,
446 : const struct list_head *a,
447 : const struct list_head *b)
448 : {
449 18085577 : struct xfs_extent_free_item *ra;
450 18085577 : struct xfs_extent_free_item *rb;
451 :
452 18085577 : ra = container_of(a, struct xfs_extent_free_item, xefi_list);
453 18085577 : rb = container_of(b, struct xfs_extent_free_item, xefi_list);
454 :
455 18085577 : ASSERT(xfs_efi_is_realtime(ra) == xfs_efi_is_realtime(rb));
456 :
457 18085577 : if (xfs_efi_is_realtime(ra))
458 4664212 : return ra->xefi_rtg->rtg_rgno - rb->xefi_rtg->rtg_rgno;
459 :
460 13421365 : 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 150325251 : 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 150325251 : uint next_extent;
471 150325251 : struct xfs_extent *extp;
472 :
473 150325251 : tp->t_flags |= XFS_TRANS_DIRTY;
474 150325251 : 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 150370021 : next_extent = atomic_inc_return(&efip->efi_next_extent) - 1;
482 150332823 : ASSERT(next_extent < efip->efi_format.efi_nextents);
483 150332823 : extp = &efip->efi_format.efi_extents[next_extent];
484 150332823 : extp->ext_start = xefi->xefi_startblock;
485 150332823 : extp->ext_len = xefi->xefi_blockcount;
486 150332823 : if (xfs_efi_is_realtime(xefi))
487 43897919 : extp->ext_len |= XFS_EFI_EXTLEN_REALTIME_EXT;
488 150332823 : }
489 :
490 : static struct xfs_log_item *
491 141616877 : xfs_extent_free_create_intent(
492 : struct xfs_trans *tp,
493 : struct list_head *items,
494 : unsigned int count,
495 : bool sort)
496 : {
497 141616877 : struct xfs_mount *mp = tp->t_mountp;
498 141616877 : struct xfs_efi_log_item *efip = xfs_efi_init(mp, count);
499 141675692 : struct xfs_extent_free_item *xefi;
500 :
501 141675692 : ASSERT(count > 0);
502 :
503 141675692 : xfs_trans_add_item(tp, &efip->efi_item);
504 141664571 : if (sort)
505 141083433 : list_sort(mp, items, xfs_extent_free_diff_items);
506 291996935 : list_for_each_entry(xefi, items, xefi_list)
507 150341604 : xfs_extent_free_log_item(tp, efip, xefi);
508 141655331 : 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 141706691 : xfs_extent_free_create_done(
514 : struct xfs_trans *tp,
515 : struct xfs_log_item *intent,
516 : unsigned int count)
517 : {
518 141706691 : 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 149644075 : xfs_extent_free_get_group(
524 : struct xfs_mount *mp,
525 : struct xfs_extent_free_item *xefi)
526 : {
527 149644075 : xfs_agnumber_t agno;
528 :
529 149644075 : if (xfs_efi_is_realtime(xefi)) {
530 43897794 : xfs_rgnumber_t rgno;
531 :
532 43897794 : rgno = xfs_rtb_to_rgno(mp, xefi->xefi_startblock);
533 43897563 : xefi->xefi_rtg = xfs_rtgroup_intent_get(mp, rgno);
534 43898075 : return;
535 : }
536 :
537 105746281 : agno = XFS_FSB_TO_AGNO(mp, xefi->xefi_startblock);
538 105746281 : 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 149790572 : xfs_extent_free_put_group(
544 : struct xfs_extent_free_item *xefi)
545 : {
546 149790572 : if (xfs_efi_is_realtime(xefi)) {
547 43898236 : xfs_rtgroup_intent_put(xefi->xefi_rtg);
548 43898236 : return;
549 : }
550 :
551 105892336 : xfs_perag_intent_put(xefi->xefi_pag);
552 : }
553 :
554 : /* Process a free extent. */
555 : STATIC int
556 149470277 : 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 149470277 : struct xfs_extent_free_item *xefi;
563 149470277 : int error;
564 :
565 149470277 : 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 149470277 : if (*state == NULL && xfs_efi_is_realtime(xefi)) {
572 41697100 : xfs_rtbitmap_lock(tp, tp->t_mountp);
573 41697742 : *state = (struct xfs_btree_cur *)1;
574 : }
575 :
576 149470919 : 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 149466657 : if (error == -EAGAIN)
583 : return error;
584 :
585 149466657 : xfs_extent_free_put_group(xefi);
586 149466043 : kmem_cache_free(xfs_extfree_item_cache, xefi);
587 149466043 : return error;
588 : }
589 :
590 : /* Abort all pending EFIs. */
591 : STATIC void
592 204 : xfs_extent_free_abort_intent(
593 : struct xfs_log_item *intent)
594 : {
595 204 : xfs_efi_release(EFI_ITEM(intent));
596 204 : }
597 :
598 : /* Cancel a free extent. */
599 : STATIC void
600 215 : xfs_extent_free_cancel_item(
601 : struct list_head *item)
602 : {
603 215 : struct xfs_extent_free_item *xefi;
604 :
605 215 : xefi = container_of(item, struct xfs_extent_free_item, xefi_list);
606 :
607 215 : xfs_extent_free_put_group(xefi);
608 215 : kmem_cache_free(xfs_extfree_item_cache, xefi);
609 215 : }
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 328623 : 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 328623 : struct xfs_owner_info oinfo = { };
632 328623 : struct xfs_mount *mp = tp->t_mountp;
633 328623 : struct xfs_efd_log_item *efdp = EFD_ITEM(done);
634 328623 : struct xfs_extent_free_item *xefi;
635 328623 : struct xfs_extent *extp;
636 328623 : struct xfs_buf *agbp;
637 328623 : int error;
638 328623 : xfs_agblock_t agbno;
639 328623 : uint next_extent;
640 :
641 328623 : xefi = container_of(item, struct xfs_extent_free_item, xefi_list);
642 328623 : ASSERT(xefi->xefi_blockcount == 1);
643 328623 : ASSERT(!xfs_efi_is_realtime(xefi));
644 328623 : agbno = XFS_FSB_TO_AGBNO(mp, xefi->xefi_startblock);
645 328622 : oinfo.oi_owner = xefi->xefi_owner;
646 :
647 328622 : trace_xfs_agfl_free_deferred(mp, xefi);
648 :
649 328623 : error = xfs_alloc_read_agf(xefi->xefi_pag, tp, 0, &agbp);
650 328623 : if (!error)
651 328619 : 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 328622 : tp->t_flags |= XFS_TRANS_DIRTY;
662 328622 : set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
663 :
664 328623 : next_extent = efdp->efd_next_extent;
665 328623 : ASSERT(next_extent < efdp->efd_format.efd_nextents);
666 328623 : extp = &(efdp->efd_format.efd_extents[next_extent]);
667 328623 : extp->ext_start = xefi->xefi_startblock;
668 328623 : extp->ext_len = xefi->xefi_blockcount;
669 328623 : efdp->efd_next_extent++;
670 :
671 328623 : xfs_extent_free_put_group(xefi);
672 328623 : kmem_cache_free(xfs_extfree_item_cache, xefi);
673 328623 : 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 700 : xfs_efi_validate_ext(
689 : struct xfs_mount *mp,
690 : struct xfs_extent *extp)
691 : {
692 700 : if (extp->ext_len & XFS_EFI_EXTLEN_REALTIME_EXT)
693 10 : return xfs_verify_rtbext(mp, extp->ext_start,
694 : extp->ext_len & ~XFS_EFI_EXTLEN_REALTIME_EXT);
695 :
696 690 : 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 670 : xfs_efi_item_recover(
705 : struct xfs_log_item *lip,
706 : struct list_head *capture_list)
707 : {
708 670 : struct xfs_efi_log_item *efip = EFI_ITEM(lip);
709 670 : struct xfs_mount *mp = lip->li_log->l_mp;
710 670 : struct xfs_efd_log_item *efdp;
711 670 : struct xfs_trans *tp;
712 670 : int i;
713 670 : int error = 0;
714 670 : 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 1370 : for (i = 0; i < efip->efi_format.efi_nextents; i++) {
722 700 : if (!xfs_efi_validate_ext(mp,
723 700 : &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 670 : error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
732 670 : if (error)
733 : return error;
734 670 : 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 2030 : for (i = 0; i < efip->efi_format.efi_nextents; i++) {
738 700 : struct xfs_extent *extp;
739 :
740 700 : extp = &efip->efi_format.efi_extents[i];
741 700 : if (extp->ext_len & XFS_EFI_EXTLEN_REALTIME_EXT) {
742 10 : xfs_rtbitmap_lock(tp, mp);
743 10 : break;
744 : }
745 : }
746 :
747 1370 : for (i = 0; i < efip->efi_format.efi_nextents; i++) {
748 700 : struct xfs_extent_free_item fake = {
749 : .xefi_owner = XFS_RMAP_OWN_UNKNOWN,
750 : .xefi_agresv = XFS_AG_RESV_NONE,
751 : };
752 700 : struct xfs_extent *extp;
753 700 : unsigned int len;
754 :
755 700 : extp = &efip->efi_format.efi_extents[i];
756 :
757 700 : fake.xefi_startblock = extp->ext_start;
758 700 : len = extp->ext_len;
759 700 : if (len & XFS_EFI_EXTLEN_REALTIME_EXT) {
760 10 : len &= ~XFS_EFI_EXTLEN_REALTIME_EXT;
761 10 : fake.xefi_flags |= XFS_EFI_REALTIME;
762 : }
763 700 : fake.xefi_blockcount = len;
764 :
765 700 : if (!requeue_only) {
766 700 : xfs_extent_free_get_group(mp, &fake);
767 700 : error = xfs_trans_free_extent(tp, efdp, &fake);
768 700 : 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 700 : 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 700 : if (error == -EFSCORRUPTED)
788 0 : XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
789 : extp, sizeof(*extp));
790 700 : if (error)
791 0 : goto abort_error;
792 :
793 : }
794 :
795 670 : 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 72032 : xfs_efi_item_match(
804 : struct xfs_log_item *lip,
805 : uint64_t intent_id)
806 : {
807 72032 : 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 8823 : xfs_efi_item_relog(
813 : struct xfs_log_item *intent,
814 : struct xfs_trans *tp)
815 : {
816 8823 : struct xfs_efd_log_item *efdp;
817 8823 : struct xfs_efi_log_item *efip;
818 8823 : struct xfs_extent *extp;
819 8823 : unsigned int count;
820 :
821 8823 : count = EFI_ITEM(intent)->efi_format.efi_nextents;
822 8823 : extp = EFI_ITEM(intent)->efi_format.efi_extents;
823 :
824 8823 : tp->t_flags |= XFS_TRANS_DIRTY;
825 8823 : efdp = xfs_trans_get_efd(tp, EFI_ITEM(intent), count);
826 8824 : efdp->efd_next_extent = count;
827 17648 : memcpy(efdp->efd_format.efd_extents, extp, count * sizeof(*extp));
828 8824 : set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
829 :
830 8830 : efip = xfs_efi_init(tp->t_mountp, count);
831 17650 : memcpy(efip->efi_format.efi_extents, extp, count * sizeof(*extp));
832 8825 : atomic_set(&efip->efi_next_extent, count);
833 8825 : xfs_trans_add_item(tp, &efip->efi_item);
834 8822 : set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
835 8830 : 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 72335 : 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 72335 : struct xfs_mount *mp = log->l_mp;
864 72335 : struct xfs_efi_log_item *efip;
865 72335 : struct xfs_efi_log_format *efi_formatp;
866 72335 : int error;
867 :
868 72335 : efi_formatp = item->ri_buf[0].i_addr;
869 :
870 72335 : 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 72335 : efip = xfs_efi_init(mp, efi_formatp->efi_nextents);
877 72335 : error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
878 72335 : if (error) {
879 0 : xfs_efi_item_free(efip);
880 0 : return error;
881 : }
882 72335 : 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 72335 : xfs_trans_ail_insert(log->l_ailp, &efip->efi_item, lsn);
888 72335 : xfs_efi_release(efip);
889 72335 : 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 71727 : 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 71727 : struct xfs_efd_log_format *efd_formatp;
912 71727 : int buflen = item->ri_buf[0].i_len;
913 :
914 71727 : efd_formatp = item->ri_buf[0].i_addr;
915 :
916 71727 : 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 71727 : if (item->ri_buf[0].i_len != xfs_efd_log_format32_sizeof(
923 71727 : 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 71727 : xlog_recover_release_intent(log, XFS_LI_EFI, efd_formatp->efd_efi_id);
932 71727 : 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 : };
|