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