Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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_defer.h"
15#include "xfs_trans.h"
16#include "xfs_trans_priv.h"
17#include "xfs_extfree_item.h"
18#include "xfs_log.h"
19#include "xfs_btree.h"
20#include "xfs_rmap.h"
21#include "xfs_alloc.h"
22#include "xfs_bmap.h"
23#include "xfs_trace.h"
24#include "xfs_error.h"
25
26kmem_zone_t *xfs_efi_zone;
27kmem_zone_t *xfs_efd_zone;
28
29static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip)
30{
31 return container_of(lip, struct xfs_efi_log_item, efi_item);
32}
33
34void
35xfs_efi_item_free(
36 struct xfs_efi_log_item *efip)
37{
38 kmem_free(efip->efi_item.li_lv_shadow);
39 if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
40 kmem_free(efip);
41 else
42 kmem_cache_free(xfs_efi_zone, efip);
43}
44
45/*
46 * Freeing the efi requires that we remove it from the AIL if it has already
47 * been placed there. However, the EFI may not yet have been placed in the AIL
48 * when called by xfs_efi_release() from EFD processing due to the ordering of
49 * committed vs unpin operations in bulk insert operations. Hence the reference
50 * count to ensure only the last caller frees the EFI.
51 */
52void
53xfs_efi_release(
54 struct xfs_efi_log_item *efip)
55{
56 ASSERT(atomic_read(&efip->efi_refcount) > 0);
57 if (atomic_dec_and_test(&efip->efi_refcount)) {
58 xfs_trans_ail_remove(&efip->efi_item, SHUTDOWN_LOG_IO_ERROR);
59 xfs_efi_item_free(efip);
60 }
61}
62
63/*
64 * This returns the number of iovecs needed to log the given efi item.
65 * We only need 1 iovec for an efi item. It just logs the efi_log_format
66 * structure.
67 */
68static inline int
69xfs_efi_item_sizeof(
70 struct xfs_efi_log_item *efip)
71{
72 return sizeof(struct xfs_efi_log_format) +
73 (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
74}
75
76STATIC void
77xfs_efi_item_size(
78 struct xfs_log_item *lip,
79 int *nvecs,
80 int *nbytes)
81{
82 *nvecs += 1;
83 *nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip));
84}
85
86/*
87 * This is called to fill in the vector of log iovecs for the
88 * given efi log item. We use only 1 iovec, and we point that
89 * at the efi_log_format structure embedded in the efi item.
90 * It is at this point that we assert that all of the extent
91 * slots in the efi item have been filled.
92 */
93STATIC void
94xfs_efi_item_format(
95 struct xfs_log_item *lip,
96 struct xfs_log_vec *lv)
97{
98 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
99 struct xfs_log_iovec *vecp = NULL;
100
101 ASSERT(atomic_read(&efip->efi_next_extent) ==
102 efip->efi_format.efi_nextents);
103
104 efip->efi_format.efi_type = XFS_LI_EFI;
105 efip->efi_format.efi_size = 1;
106
107 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
108 &efip->efi_format,
109 xfs_efi_item_sizeof(efip));
110}
111
112
113/*
114 * The unpin operation is the last place an EFI is manipulated in the log. It is
115 * either inserted in the AIL or aborted in the event of a log I/O error. In
116 * either case, the EFI transaction has been successfully committed to make it
117 * this far. Therefore, we expect whoever committed the EFI to either construct
118 * and commit the EFD or drop the EFD's reference in the event of error. Simply
119 * drop the log's EFI reference now that the log is done with it.
120 */
121STATIC void
122xfs_efi_item_unpin(
123 struct xfs_log_item *lip,
124 int remove)
125{
126 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
127 xfs_efi_release(efip);
128}
129
130/*
131 * The EFI has been either committed or aborted if the transaction has been
132 * cancelled. If the transaction was cancelled, an EFD isn't going to be
133 * constructed and thus we free the EFI here directly.
134 */
135STATIC void
136xfs_efi_item_release(
137 struct xfs_log_item *lip)
138{
139 xfs_efi_release(EFI_ITEM(lip));
140}
141
142static const struct xfs_item_ops xfs_efi_item_ops = {
143 .iop_size = xfs_efi_item_size,
144 .iop_format = xfs_efi_item_format,
145 .iop_unpin = xfs_efi_item_unpin,
146 .iop_release = xfs_efi_item_release,
147};
148
149
150/*
151 * Allocate and initialize an efi item with the given number of extents.
152 */
153struct xfs_efi_log_item *
154xfs_efi_init(
155 struct xfs_mount *mp,
156 uint nextents)
157
158{
159 struct xfs_efi_log_item *efip;
160 uint size;
161
162 ASSERT(nextents > 0);
163 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
164 size = (uint)(sizeof(xfs_efi_log_item_t) +
165 ((nextents - 1) * sizeof(xfs_extent_t)));
166 efip = kmem_zalloc(size, 0);
167 } else {
168 efip = kmem_zone_zalloc(xfs_efi_zone, 0);
169 }
170
171 xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
172 efip->efi_format.efi_nextents = nextents;
173 efip->efi_format.efi_id = (uintptr_t)(void *)efip;
174 atomic_set(&efip->efi_next_extent, 0);
175 atomic_set(&efip->efi_refcount, 2);
176
177 return efip;
178}
179
180/*
181 * Copy an EFI format buffer from the given buf, and into the destination
182 * EFI format structure.
183 * The given buffer can be in 32 bit or 64 bit form (which has different padding),
184 * one of which will be the native format for this kernel.
185 * It will handle the conversion of formats if necessary.
186 */
187int
188xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
189{
190 xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
191 uint i;
192 uint len = sizeof(xfs_efi_log_format_t) +
193 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t);
194 uint len32 = sizeof(xfs_efi_log_format_32_t) +
195 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t);
196 uint len64 = sizeof(xfs_efi_log_format_64_t) +
197 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t);
198
199 if (buf->i_len == len) {
200 memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len);
201 return 0;
202 } else if (buf->i_len == len32) {
203 xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
204
205 dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
206 dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
207 dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
208 dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
209 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
210 dst_efi_fmt->efi_extents[i].ext_start =
211 src_efi_fmt_32->efi_extents[i].ext_start;
212 dst_efi_fmt->efi_extents[i].ext_len =
213 src_efi_fmt_32->efi_extents[i].ext_len;
214 }
215 return 0;
216 } else if (buf->i_len == len64) {
217 xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
218
219 dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
220 dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
221 dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
222 dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
223 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
224 dst_efi_fmt->efi_extents[i].ext_start =
225 src_efi_fmt_64->efi_extents[i].ext_start;
226 dst_efi_fmt->efi_extents[i].ext_len =
227 src_efi_fmt_64->efi_extents[i].ext_len;
228 }
229 return 0;
230 }
231 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
232 return -EFSCORRUPTED;
233}
234
235static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
236{
237 return container_of(lip, struct xfs_efd_log_item, efd_item);
238}
239
240STATIC void
241xfs_efd_item_free(struct xfs_efd_log_item *efdp)
242{
243 kmem_free(efdp->efd_item.li_lv_shadow);
244 if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
245 kmem_free(efdp);
246 else
247 kmem_cache_free(xfs_efd_zone, efdp);
248}
249
250/*
251 * This returns the number of iovecs needed to log the given efd item.
252 * We only need 1 iovec for an efd item. It just logs the efd_log_format
253 * structure.
254 */
255static inline int
256xfs_efd_item_sizeof(
257 struct xfs_efd_log_item *efdp)
258{
259 return sizeof(xfs_efd_log_format_t) +
260 (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
261}
262
263STATIC void
264xfs_efd_item_size(
265 struct xfs_log_item *lip,
266 int *nvecs,
267 int *nbytes)
268{
269 *nvecs += 1;
270 *nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip));
271}
272
273/*
274 * This is called to fill in the vector of log iovecs for the
275 * given efd log item. We use only 1 iovec, and we point that
276 * at the efd_log_format structure embedded in the efd item.
277 * It is at this point that we assert that all of the extent
278 * slots in the efd item have been filled.
279 */
280STATIC void
281xfs_efd_item_format(
282 struct xfs_log_item *lip,
283 struct xfs_log_vec *lv)
284{
285 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
286 struct xfs_log_iovec *vecp = NULL;
287
288 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
289
290 efdp->efd_format.efd_type = XFS_LI_EFD;
291 efdp->efd_format.efd_size = 1;
292
293 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
294 &efdp->efd_format,
295 xfs_efd_item_sizeof(efdp));
296}
297
298/*
299 * The EFD is either committed or aborted if the transaction is cancelled. If
300 * the transaction is cancelled, drop our reference to the EFI and free the EFD.
301 */
302STATIC void
303xfs_efd_item_release(
304 struct xfs_log_item *lip)
305{
306 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
307
308 xfs_efi_release(efdp->efd_efip);
309 xfs_efd_item_free(efdp);
310}
311
312static const struct xfs_item_ops xfs_efd_item_ops = {
313 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED,
314 .iop_size = xfs_efd_item_size,
315 .iop_format = xfs_efd_item_format,
316 .iop_release = xfs_efd_item_release,
317};
318
319/*
320 * Allocate an "extent free done" log item that will hold nextents worth of
321 * extents. The caller must use all nextents extents, because we are not
322 * flexible about this at all.
323 */
324static struct xfs_efd_log_item *
325xfs_trans_get_efd(
326 struct xfs_trans *tp,
327 struct xfs_efi_log_item *efip,
328 unsigned int nextents)
329{
330 struct xfs_efd_log_item *efdp;
331
332 ASSERT(nextents > 0);
333
334 if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
335 efdp = kmem_zalloc(sizeof(struct xfs_efd_log_item) +
336 (nextents - 1) * sizeof(struct xfs_extent),
337 0);
338 } else {
339 efdp = kmem_zone_zalloc(xfs_efd_zone, 0);
340 }
341
342 xfs_log_item_init(tp->t_mountp, &efdp->efd_item, XFS_LI_EFD,
343 &xfs_efd_item_ops);
344 efdp->efd_efip = efip;
345 efdp->efd_format.efd_nextents = nextents;
346 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
347
348 xfs_trans_add_item(tp, &efdp->efd_item);
349 return efdp;
350}
351
352/*
353 * Free an extent and log it to the EFD. Note that the transaction is marked
354 * dirty regardless of whether the extent free succeeds or fails to support the
355 * EFI/EFD lifecycle rules.
356 */
357static int
358xfs_trans_free_extent(
359 struct xfs_trans *tp,
360 struct xfs_efd_log_item *efdp,
361 xfs_fsblock_t start_block,
362 xfs_extlen_t ext_len,
363 const struct xfs_owner_info *oinfo,
364 bool skip_discard)
365{
366 struct xfs_mount *mp = tp->t_mountp;
367 struct xfs_extent *extp;
368 uint next_extent;
369 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, start_block);
370 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp,
371 start_block);
372 int error;
373
374 trace_xfs_bmap_free_deferred(tp->t_mountp, agno, 0, agbno, ext_len);
375
376 error = __xfs_free_extent(tp, start_block, ext_len,
377 oinfo, XFS_AG_RESV_NONE, skip_discard);
378 /*
379 * Mark the transaction dirty, even on error. This ensures the
380 * transaction is aborted, which:
381 *
382 * 1.) releases the EFI and frees the EFD
383 * 2.) shuts down the filesystem
384 */
385 tp->t_flags |= XFS_TRANS_DIRTY;
386 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
387
388 next_extent = efdp->efd_next_extent;
389 ASSERT(next_extent < efdp->efd_format.efd_nextents);
390 extp = &(efdp->efd_format.efd_extents[next_extent]);
391 extp->ext_start = start_block;
392 extp->ext_len = ext_len;
393 efdp->efd_next_extent++;
394
395 return error;
396}
397
398/* Sort bmap items by AG. */
399static int
400xfs_extent_free_diff_items(
401 void *priv,
402 struct list_head *a,
403 struct list_head *b)
404{
405 struct xfs_mount *mp = priv;
406 struct xfs_extent_free_item *ra;
407 struct xfs_extent_free_item *rb;
408
409 ra = container_of(a, struct xfs_extent_free_item, xefi_list);
410 rb = container_of(b, struct xfs_extent_free_item, xefi_list);
411 return XFS_FSB_TO_AGNO(mp, ra->xefi_startblock) -
412 XFS_FSB_TO_AGNO(mp, rb->xefi_startblock);
413}
414
415/* Get an EFI. */
416STATIC void *
417xfs_extent_free_create_intent(
418 struct xfs_trans *tp,
419 unsigned int count)
420{
421 struct xfs_efi_log_item *efip;
422
423 ASSERT(tp != NULL);
424 ASSERT(count > 0);
425
426 efip = xfs_efi_init(tp->t_mountp, count);
427 ASSERT(efip != NULL);
428
429 /*
430 * Get a log_item_desc to point at the new item.
431 */
432 xfs_trans_add_item(tp, &efip->efi_item);
433 return efip;
434}
435
436/* Log a free extent to the intent item. */
437STATIC void
438xfs_extent_free_log_item(
439 struct xfs_trans *tp,
440 void *intent,
441 struct list_head *item)
442{
443 struct xfs_efi_log_item *efip = intent;
444 struct xfs_extent_free_item *free;
445 uint next_extent;
446 struct xfs_extent *extp;
447
448 free = container_of(item, struct xfs_extent_free_item, xefi_list);
449
450 tp->t_flags |= XFS_TRANS_DIRTY;
451 set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
452
453 /*
454 * atomic_inc_return gives us the value after the increment;
455 * we want to use it as an array index so we need to subtract 1 from
456 * it.
457 */
458 next_extent = atomic_inc_return(&efip->efi_next_extent) - 1;
459 ASSERT(next_extent < efip->efi_format.efi_nextents);
460 extp = &efip->efi_format.efi_extents[next_extent];
461 extp->ext_start = free->xefi_startblock;
462 extp->ext_len = free->xefi_blockcount;
463}
464
465/* Get an EFD so we can process all the free extents. */
466STATIC void *
467xfs_extent_free_create_done(
468 struct xfs_trans *tp,
469 void *intent,
470 unsigned int count)
471{
472 return xfs_trans_get_efd(tp, intent, count);
473}
474
475/* Process a free extent. */
476STATIC int
477xfs_extent_free_finish_item(
478 struct xfs_trans *tp,
479 struct list_head *item,
480 void *done_item,
481 void **state)
482{
483 struct xfs_extent_free_item *free;
484 int error;
485
486 free = container_of(item, struct xfs_extent_free_item, xefi_list);
487 error = xfs_trans_free_extent(tp, done_item,
488 free->xefi_startblock,
489 free->xefi_blockcount,
490 &free->xefi_oinfo, free->xefi_skip_discard);
491 kmem_free(free);
492 return error;
493}
494
495/* Abort all pending EFIs. */
496STATIC void
497xfs_extent_free_abort_intent(
498 void *intent)
499{
500 xfs_efi_release(intent);
501}
502
503/* Cancel a free extent. */
504STATIC void
505xfs_extent_free_cancel_item(
506 struct list_head *item)
507{
508 struct xfs_extent_free_item *free;
509
510 free = container_of(item, struct xfs_extent_free_item, xefi_list);
511 kmem_free(free);
512}
513
514const struct xfs_defer_op_type xfs_extent_free_defer_type = {
515 .max_items = XFS_EFI_MAX_FAST_EXTENTS,
516 .diff_items = xfs_extent_free_diff_items,
517 .create_intent = xfs_extent_free_create_intent,
518 .abort_intent = xfs_extent_free_abort_intent,
519 .log_item = xfs_extent_free_log_item,
520 .create_done = xfs_extent_free_create_done,
521 .finish_item = xfs_extent_free_finish_item,
522 .cancel_item = xfs_extent_free_cancel_item,
523};
524
525/*
526 * AGFL blocks are accounted differently in the reserve pools and are not
527 * inserted into the busy extent list.
528 */
529STATIC int
530xfs_agfl_free_finish_item(
531 struct xfs_trans *tp,
532 struct list_head *item,
533 void *done_item,
534 void **state)
535{
536 struct xfs_mount *mp = tp->t_mountp;
537 struct xfs_efd_log_item *efdp = done_item;
538 struct xfs_extent_free_item *free;
539 struct xfs_extent *extp;
540 struct xfs_buf *agbp;
541 int error;
542 xfs_agnumber_t agno;
543 xfs_agblock_t agbno;
544 uint next_extent;
545
546 free = container_of(item, struct xfs_extent_free_item, xefi_list);
547 ASSERT(free->xefi_blockcount == 1);
548 agno = XFS_FSB_TO_AGNO(mp, free->xefi_startblock);
549 agbno = XFS_FSB_TO_AGBNO(mp, free->xefi_startblock);
550
551 trace_xfs_agfl_free_deferred(mp, agno, 0, agbno, free->xefi_blockcount);
552
553 error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp);
554 if (!error)
555 error = xfs_free_agfl_block(tp, agno, agbno, agbp,
556 &free->xefi_oinfo);
557
558 /*
559 * Mark the transaction dirty, even on error. This ensures the
560 * transaction is aborted, which:
561 *
562 * 1.) releases the EFI and frees the EFD
563 * 2.) shuts down the filesystem
564 */
565 tp->t_flags |= XFS_TRANS_DIRTY;
566 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
567
568 next_extent = efdp->efd_next_extent;
569 ASSERT(next_extent < efdp->efd_format.efd_nextents);
570 extp = &(efdp->efd_format.efd_extents[next_extent]);
571 extp->ext_start = free->xefi_startblock;
572 extp->ext_len = free->xefi_blockcount;
573 efdp->efd_next_extent++;
574
575 kmem_free(free);
576 return error;
577}
578
579/* sub-type with special handling for AGFL deferred frees */
580const struct xfs_defer_op_type xfs_agfl_free_defer_type = {
581 .max_items = XFS_EFI_MAX_FAST_EXTENTS,
582 .diff_items = xfs_extent_free_diff_items,
583 .create_intent = xfs_extent_free_create_intent,
584 .abort_intent = xfs_extent_free_abort_intent,
585 .log_item = xfs_extent_free_log_item,
586 .create_done = xfs_extent_free_create_done,
587 .finish_item = xfs_agfl_free_finish_item,
588 .cancel_item = xfs_extent_free_cancel_item,
589};
590
591/*
592 * Process an extent free intent item that was recovered from
593 * the log. We need to free the extents that it describes.
594 */
595int
596xfs_efi_recover(
597 struct xfs_mount *mp,
598 struct xfs_efi_log_item *efip)
599{
600 struct xfs_efd_log_item *efdp;
601 struct xfs_trans *tp;
602 int i;
603 int error = 0;
604 xfs_extent_t *extp;
605 xfs_fsblock_t startblock_fsb;
606
607 ASSERT(!test_bit(XFS_EFI_RECOVERED, &efip->efi_flags));
608
609 /*
610 * First check the validity of the extents described by the
611 * EFI. If any are bad, then assume that all are bad and
612 * just toss the EFI.
613 */
614 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
615 extp = &efip->efi_format.efi_extents[i];
616 startblock_fsb = XFS_BB_TO_FSB(mp,
617 XFS_FSB_TO_DADDR(mp, extp->ext_start));
618 if (startblock_fsb == 0 ||
619 extp->ext_len == 0 ||
620 startblock_fsb >= mp->m_sb.sb_dblocks ||
621 extp->ext_len >= mp->m_sb.sb_agblocks) {
622 /*
623 * This will pull the EFI from the AIL and
624 * free the memory associated with it.
625 */
626 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
627 xfs_efi_release(efip);
628 return -EFSCORRUPTED;
629 }
630 }
631
632 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
633 if (error)
634 return error;
635 efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
636
637 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
638 extp = &efip->efi_format.efi_extents[i];
639 error = xfs_trans_free_extent(tp, efdp, extp->ext_start,
640 extp->ext_len,
641 &XFS_RMAP_OINFO_ANY_OWNER, false);
642 if (error)
643 goto abort_error;
644
645 }
646
647 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
648 error = xfs_trans_commit(tp);
649 return error;
650
651abort_error:
652 xfs_trans_cancel(tp);
653 return error;
654}