blob: e224b49b7cff6d6274c9c4611f6d4166185bc831 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0+
Darrick J. Wong6413a012016-10-03 09:11:25 -07002/*
3 * Copyright (C) 2016 Oracle. All Rights Reserved.
Darrick J. Wong6413a012016-10-03 09:11:25 -07004 * Author: Darrick J. Wong <darrick.wong@oracle.com>
Darrick J. Wong6413a012016-10-03 09:11:25 -07005 */
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"
Darrick J. Wong77d61fe2016-10-03 09:11:26 -070011#include "xfs_bit.h"
Darrick J. Wong5467b342019-06-28 19:25:35 -070012#include "xfs_shared.h"
Darrick J. Wong6413a012016-10-03 09:11:25 -070013#include "xfs_mount.h"
Darrick J. Wong77d61fe2016-10-03 09:11:26 -070014#include "xfs_defer.h"
15#include "xfs_inode.h"
Darrick J. Wong6413a012016-10-03 09:11:25 -070016#include "xfs_trans.h"
17#include "xfs_trans_priv.h"
Darrick J. Wong6413a012016-10-03 09:11:25 -070018#include "xfs_bmap_item.h"
19#include "xfs_log.h"
Darrick J. Wong77d61fe2016-10-03 09:11:26 -070020#include "xfs_bmap.h"
21#include "xfs_icache.h"
Darrick J. Wongfe0be232017-04-12 12:26:07 -070022#include "xfs_bmap_btree.h"
23#include "xfs_trans_space.h"
Darrick J. Wonga5155b82019-11-02 09:40:53 -070024#include "xfs_error.h"
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -070025#include "xfs_log_priv.h"
Darrick J. Wong86ffa472020-05-01 16:00:45 -070026#include "xfs_log_recover.h"
Darrick J. Wong774a99b2023-04-11 18:59:53 -070027#include "xfs_ag.h"
Darrick J. Wong80284112024-02-22 12:44:21 -080028#include "xfs_trace.h"
Darrick J. Wong6413a012016-10-03 09:11:25 -070029
Darrick J. Wong182696f2021-10-12 11:09:23 -070030struct kmem_cache *xfs_bui_cache;
31struct kmem_cache *xfs_bud_cache;
Darrick J. Wong6413a012016-10-03 09:11:25 -070032
Darrick J. Wong9329ba82020-05-01 16:00:52 -070033static const struct xfs_item_ops xfs_bui_item_ops;
34
Darrick J. Wong6413a012016-10-03 09:11:25 -070035static inline struct xfs_bui_log_item *BUI_ITEM(struct xfs_log_item *lip)
36{
37 return container_of(lip, struct xfs_bui_log_item, bui_item);
38}
39
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -070040STATIC void
Darrick J. Wong6413a012016-10-03 09:11:25 -070041xfs_bui_item_free(
42 struct xfs_bui_log_item *buip)
43{
Dave Chinner49292572024-01-16 09:59:42 +110044 kvfree(buip->bui_item.li_lv_shadow);
Darrick J. Wong182696f2021-10-12 11:09:23 -070045 kmem_cache_free(xfs_bui_cache, buip);
Darrick J. Wong6413a012016-10-03 09:11:25 -070046}
47
Dave Chinner0612d112018-04-02 20:08:27 -070048/*
49 * Freeing the BUI requires that we remove it from the AIL if it has already
50 * been placed there. However, the BUI may not yet have been placed in the AIL
51 * when called by xfs_bui_release() from BUD processing due to the ordering of
52 * committed vs unpin operations in bulk insert operations. Hence the reference
53 * count to ensure only the last caller frees the BUI.
54 */
Darrick J. Wong9329ba82020-05-01 16:00:52 -070055STATIC void
Dave Chinner0612d112018-04-02 20:08:27 -070056xfs_bui_release(
57 struct xfs_bui_log_item *buip)
58{
59 ASSERT(atomic_read(&buip->bui_refcount) > 0);
Dave Chinner3512fc12022-05-04 11:46:47 +100060 if (!atomic_dec_and_test(&buip->bui_refcount))
61 return;
62
63 xfs_trans_ail_delete(&buip->bui_item, 0);
64 xfs_bui_item_free(buip);
Dave Chinner0612d112018-04-02 20:08:27 -070065}
66
67
Darrick J. Wong6413a012016-10-03 09:11:25 -070068STATIC void
69xfs_bui_item_size(
70 struct xfs_log_item *lip,
71 int *nvecs,
72 int *nbytes)
73{
74 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
75
76 *nvecs += 1;
77 *nbytes += xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents);
78}
79
80/*
81 * This is called to fill in the vector of log iovecs for the
82 * given bui log item. We use only 1 iovec, and we point that
83 * at the bui_log_format structure embedded in the bui item.
84 * It is at this point that we assert that all of the extent
85 * slots in the bui item have been filled.
86 */
87STATIC void
88xfs_bui_item_format(
89 struct xfs_log_item *lip,
90 struct xfs_log_vec *lv)
91{
92 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
93 struct xfs_log_iovec *vecp = NULL;
94
95 ASSERT(atomic_read(&buip->bui_next_extent) ==
96 buip->bui_format.bui_nextents);
97
98 buip->bui_format.bui_type = XFS_LI_BUI;
99 buip->bui_format.bui_size = 1;
100
101 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUI_FORMAT, &buip->bui_format,
102 xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents));
103}
104
105/*
Darrick J. Wong6413a012016-10-03 09:11:25 -0700106 * The unpin operation is the last place an BUI is manipulated in the log. It is
107 * either inserted in the AIL or aborted in the event of a log I/O error. In
108 * either case, the BUI transaction has been successfully committed to make it
109 * this far. Therefore, we expect whoever committed the BUI to either construct
110 * and commit the BUD or drop the BUD's reference in the event of error. Simply
111 * drop the log's BUI reference now that the log is done with it.
112 */
113STATIC void
114xfs_bui_item_unpin(
115 struct xfs_log_item *lip,
116 int remove)
117{
118 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
119
120 xfs_bui_release(buip);
121}
122
123/*
Darrick J. Wong6413a012016-10-03 09:11:25 -0700124 * The BUI has been either committed or aborted if the transaction has been
125 * cancelled. If the transaction was cancelled, an BUD isn't going to be
126 * constructed and thus we free the BUI here directly.
127 */
128STATIC void
Christoph Hellwigddf92052019-06-28 19:27:32 -0700129xfs_bui_item_release(
Darrick J. Wong6413a012016-10-03 09:11:25 -0700130 struct xfs_log_item *lip)
131{
Christoph Hellwigddf92052019-06-28 19:27:32 -0700132 xfs_bui_release(BUI_ITEM(lip));
Darrick J. Wong6413a012016-10-03 09:11:25 -0700133}
134
Darrick J. Wong6413a012016-10-03 09:11:25 -0700135/*
136 * Allocate and initialize an bui item with the given number of extents.
137 */
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -0700138STATIC struct xfs_bui_log_item *
Darrick J. Wong6413a012016-10-03 09:11:25 -0700139xfs_bui_init(
140 struct xfs_mount *mp)
141
142{
143 struct xfs_bui_log_item *buip;
144
Darrick J. Wong182696f2021-10-12 11:09:23 -0700145 buip = kmem_cache_zalloc(xfs_bui_cache, GFP_KERNEL | __GFP_NOFAIL);
Darrick J. Wong6413a012016-10-03 09:11:25 -0700146
147 xfs_log_item_init(mp, &buip->bui_item, XFS_LI_BUI, &xfs_bui_item_ops);
148 buip->bui_format.bui_nextents = XFS_BUI_MAX_FAST_EXTENTS;
149 buip->bui_format.bui_id = (uintptr_t)(void *)buip;
150 atomic_set(&buip->bui_next_extent, 0);
151 atomic_set(&buip->bui_refcount, 2);
152
153 return buip;
154}
155
Darrick J. Wong6413a012016-10-03 09:11:25 -0700156static inline struct xfs_bud_log_item *BUD_ITEM(struct xfs_log_item *lip)
157{
158 return container_of(lip, struct xfs_bud_log_item, bud_item);
159}
160
161STATIC void
162xfs_bud_item_size(
163 struct xfs_log_item *lip,
164 int *nvecs,
165 int *nbytes)
166{
167 *nvecs += 1;
168 *nbytes += sizeof(struct xfs_bud_log_format);
169}
170
171/*
172 * This is called to fill in the vector of log iovecs for the
173 * given bud log item. We use only 1 iovec, and we point that
174 * at the bud_log_format structure embedded in the bud item.
175 * It is at this point that we assert that all of the extent
176 * slots in the bud item have been filled.
177 */
178STATIC void
179xfs_bud_item_format(
180 struct xfs_log_item *lip,
181 struct xfs_log_vec *lv)
182{
183 struct xfs_bud_log_item *budp = BUD_ITEM(lip);
184 struct xfs_log_iovec *vecp = NULL;
185
186 budp->bud_format.bud_type = XFS_LI_BUD;
187 budp->bud_format.bud_size = 1;
188
189 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUD_FORMAT, &budp->bud_format,
190 sizeof(struct xfs_bud_log_format));
191}
192
193/*
Darrick J. Wong6413a012016-10-03 09:11:25 -0700194 * The BUD is either committed or aborted if the transaction is cancelled. If
195 * the transaction is cancelled, drop our reference to the BUI and free the
196 * BUD.
197 */
198STATIC void
Christoph Hellwigddf92052019-06-28 19:27:32 -0700199xfs_bud_item_release(
Darrick J. Wong6413a012016-10-03 09:11:25 -0700200 struct xfs_log_item *lip)
201{
202 struct xfs_bud_log_item *budp = BUD_ITEM(lip);
203
Christoph Hellwigddf92052019-06-28 19:27:32 -0700204 xfs_bui_release(budp->bud_buip);
Dave Chinner49292572024-01-16 09:59:42 +1100205 kvfree(budp->bud_item.li_lv_shadow);
Darrick J. Wong182696f2021-10-12 11:09:23 -0700206 kmem_cache_free(xfs_bud_cache, budp);
Darrick J. Wong6413a012016-10-03 09:11:25 -0700207}
208
Dave Chinnerc23ab602022-05-04 11:46:39 +1000209static struct xfs_log_item *
210xfs_bud_item_intent(
211 struct xfs_log_item *lip)
212{
213 return &BUD_ITEM(lip)->bud_buip->bui_item;
214}
215
Darrick J. Wong6413a012016-10-03 09:11:25 -0700216static const struct xfs_item_ops xfs_bud_item_ops = {
Dave Chinnerf5b81202022-05-04 11:46:09 +1000217 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED |
218 XFS_ITEM_INTENT_DONE,
Darrick J. Wong6413a012016-10-03 09:11:25 -0700219 .iop_size = xfs_bud_item_size,
220 .iop_format = xfs_bud_item_format,
Christoph Hellwigddf92052019-06-28 19:27:32 -0700221 .iop_release = xfs_bud_item_release,
Dave Chinnerc23ab602022-05-04 11:46:39 +1000222 .iop_intent = xfs_bud_item_intent,
Darrick J. Wong6413a012016-10-03 09:11:25 -0700223};
224
Darrick J. Wongde47e4c2024-02-22 12:44:19 -0800225static inline struct xfs_bmap_intent *bi_entry(const struct list_head *e)
226{
227 return list_entry(e, struct xfs_bmap_intent, bi_list);
228}
229
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700230/* Sort bmap intents by inode. */
231static int
232xfs_bmap_update_diff_items(
233 void *priv,
Sami Tolvanen4f0f5862021-04-08 11:28:34 -0700234 const struct list_head *a,
235 const struct list_head *b)
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700236{
Darrick J. Wongde47e4c2024-02-22 12:44:19 -0800237 struct xfs_bmap_intent *ba = bi_entry(a);
238 struct xfs_bmap_intent *bb = bi_entry(b);
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700239
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700240 return ba->bi_owner->i_ino - bb->bi_owner->i_ino;
241}
242
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700243/* Log bmap updates in the intent item. */
244STATIC void
245xfs_bmap_update_log_item(
246 struct xfs_trans *tp,
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700247 struct xfs_bui_log_item *buip,
Darrick J. Wongf3ebac42023-02-01 10:16:01 -0800248 struct xfs_bmap_intent *bi)
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700249{
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700250 uint next_extent;
251 struct xfs_map_extent *map;
252
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700253 /*
254 * atomic_inc_return gives us the value after the increment;
255 * we want to use it as an array index so we need to subtract 1 from
256 * it.
257 */
258 next_extent = atomic_inc_return(&buip->bui_next_extent) - 1;
259 ASSERT(next_extent < buip->bui_format.bui_nextents);
260 map = &buip->bui_format.bui_extents[next_extent];
Darrick J. Wongf3ebac42023-02-01 10:16:01 -0800261 map->me_owner = bi->bi_owner->i_ino;
262 map->me_startblock = bi->bi_bmap.br_startblock;
263 map->me_startoff = bi->bi_bmap.br_startoff;
264 map->me_len = bi->bi_bmap.br_blockcount;
Darrick J. Wong372fe0b2024-02-22 12:44:19 -0800265
266 switch (bi->bi_type) {
267 case XFS_BMAP_MAP:
268 case XFS_BMAP_UNMAP:
269 map->me_flags = bi->bi_type;
270 break;
271 default:
272 ASSERT(0);
273 }
274 if (bi->bi_bmap.br_state == XFS_EXT_UNWRITTEN)
275 map->me_flags |= XFS_BMAP_EXTENT_UNWRITTEN;
276 if (bi->bi_whichfork == XFS_ATTR_FORK)
277 map->me_flags |= XFS_BMAP_EXTENT_ATTR_FORK;
Darrick J. Wong7302cda2024-02-22 12:44:23 -0800278 if (xfs_ifork_is_realtime(bi->bi_owner, bi->bi_whichfork))
279 map->me_flags |= XFS_BMAP_EXTENT_REALTIME;
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700280}
281
Christoph Hellwig13a83332020-04-30 12:52:21 -0700282static struct xfs_log_item *
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700283xfs_bmap_update_create_intent(
284 struct xfs_trans *tp,
285 struct list_head *items,
Christoph Hellwigd367a862020-04-30 12:52:20 -0700286 unsigned int count,
287 bool sort)
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700288{
Christoph Hellwigd367a862020-04-30 12:52:20 -0700289 struct xfs_mount *mp = tp->t_mountp;
290 struct xfs_bui_log_item *buip = xfs_bui_init(mp);
Darrick J. Wongf3ebac42023-02-01 10:16:01 -0800291 struct xfs_bmap_intent *bi;
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700292
293 ASSERT(count == XFS_BUI_MAX_FAST_EXTENTS);
294
Christoph Hellwigd367a862020-04-30 12:52:20 -0700295 if (sort)
296 list_sort(mp, items, xfs_bmap_update_diff_items);
Darrick J. Wongf3ebac42023-02-01 10:16:01 -0800297 list_for_each_entry(bi, items, bi_list)
298 xfs_bmap_update_log_item(tp, buip, bi);
Christoph Hellwig13a83332020-04-30 12:52:21 -0700299 return &buip->bui_item;
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700300}
301
Darrick J. Wong8a9aa762023-11-30 12:17:37 -0800302/* Get an BUD so we can process all the deferred bmap updates. */
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700303static struct xfs_log_item *
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700304xfs_bmap_update_create_done(
305 struct xfs_trans *tp,
Christoph Hellwig13a83332020-04-30 12:52:21 -0700306 struct xfs_log_item *intent,
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700307 unsigned int count)
308{
Darrick J. Wong8a9aa762023-11-30 12:17:37 -0800309 struct xfs_bui_log_item *buip = BUI_ITEM(intent);
310 struct xfs_bud_log_item *budp;
311
312 budp = kmem_cache_zalloc(xfs_bud_cache, GFP_KERNEL | __GFP_NOFAIL);
313 xfs_log_item_init(tp->t_mountp, &budp->bud_item, XFS_LI_BUD,
314 &xfs_bud_item_ops);
315 budp->bud_buip = buip;
316 budp->bud_format.bud_bui_id = buip->bui_format.bui_id;
317
318 return &budp->bud_item;
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700319}
320
Darrick J. Wong774a99b2023-04-11 18:59:53 -0700321/* Take a passive ref to the AG containing the space we're mapping. */
Darrick J. Wong80284112024-02-22 12:44:21 -0800322static inline void
Darrick J. Wong774a99b2023-04-11 18:59:53 -0700323xfs_bmap_update_get_group(
324 struct xfs_mount *mp,
325 struct xfs_bmap_intent *bi)
326{
Darrick J. Wong7302cda2024-02-22 12:44:23 -0800327 if (xfs_ifork_is_realtime(bi->bi_owner, bi->bi_whichfork))
328 return;
329
Darrick J. Wongd5c88132023-04-11 18:59:58 -0700330 /*
331 * Bump the intent count on behalf of the deferred rmap and refcount
332 * intent items that that we can queue when we finish this bmap work.
333 * This new intent item will bump the intent count before the bmap
334 * intent drops the intent count, ensuring that the intent count
335 * remains nonzero across the transaction roll.
336 */
Christoph Hellwig62d597a2024-07-02 11:22:51 -0700337 bi->bi_pag = xfs_perag_intent_get(mp, bi->bi_bmap.br_startblock);
Darrick J. Wong774a99b2023-04-11 18:59:53 -0700338}
339
Darrick J. Wong80284112024-02-22 12:44:21 -0800340/* Add this deferred BUI to the transaction. */
341void
342xfs_bmap_defer_add(
343 struct xfs_trans *tp,
344 struct xfs_bmap_intent *bi)
345{
346 trace_xfs_bmap_defer(bi);
347
348 xfs_bmap_update_get_group(tp->t_mountp, bi);
349 xfs_defer_add(tp, &bi->bi_list, &xfs_bmap_update_defer_type);
350}
351
Darrick J. Wong774a99b2023-04-11 18:59:53 -0700352/* Release a passive AG ref after finishing mapping work. */
353static inline void
354xfs_bmap_update_put_group(
355 struct xfs_bmap_intent *bi)
356{
Darrick J. Wong7302cda2024-02-22 12:44:23 -0800357 if (xfs_ifork_is_realtime(bi->bi_owner, bi->bi_whichfork))
358 return;
359
Darrick J. Wongd5c88132023-04-11 18:59:58 -0700360 xfs_perag_intent_put(bi->bi_pag);
Darrick J. Wong774a99b2023-04-11 18:59:53 -0700361}
362
Darrick J. Wong5d3d0a62024-02-22 12:44:20 -0800363/* Cancel a deferred bmap update. */
364STATIC void
365xfs_bmap_update_cancel_item(
366 struct list_head *item)
367{
368 struct xfs_bmap_intent *bi = bi_entry(item);
369
370 xfs_bmap_update_put_group(bi);
371 kmem_cache_free(xfs_bmap_intent_cache, bi);
372}
373
Darrick J. Wonge6e5299f2023-11-30 10:23:19 -0800374/* Process a deferred bmap update. */
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700375STATIC int
376xfs_bmap_update_finish_item(
377 struct xfs_trans *tp,
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700378 struct xfs_log_item *done,
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700379 struct list_head *item,
Christoph Hellwig3ec1b262020-04-30 12:52:22 -0700380 struct xfs_btree_cur **state)
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700381{
Darrick J. Wongde47e4c2024-02-22 12:44:19 -0800382 struct xfs_bmap_intent *bi = bi_entry(item);
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700383 int error;
384
Darrick J. Wonge6e5299f2023-11-30 10:23:19 -0800385 error = xfs_bmap_finish_one(tp, bi);
Darrick J. Wongddccb812023-02-01 09:50:53 -0800386 if (!error && bi->bi_bmap.br_blockcount > 0) {
387 ASSERT(bi->bi_type == XFS_BMAP_UNMAP);
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700388 return -EAGAIN;
389 }
Darrick J. Wong774a99b2023-04-11 18:59:53 -0700390
Darrick J. Wong5d3d0a62024-02-22 12:44:20 -0800391 xfs_bmap_update_cancel_item(item);
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700392 return error;
393}
394
395/* Abort all pending BUIs. */
396STATIC void
397xfs_bmap_update_abort_intent(
Christoph Hellwig13a83332020-04-30 12:52:21 -0700398 struct xfs_log_item *intent)
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700399{
Christoph Hellwig13a83332020-04-30 12:52:21 -0700400 xfs_bui_release(BUI_ITEM(intent));
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700401}
402
Darrick J. Wongbc525cf2020-11-29 16:33:35 -0800403/* Is this recovered BUI ok? */
404static inline bool
405xfs_bui_validate(
406 struct xfs_mount *mp,
407 struct xfs_bui_log_item *buip)
408{
Darrick J. Wongf3ebac42023-02-01 10:16:01 -0800409 struct xfs_map_extent *map;
Darrick J. Wongbc525cf2020-11-29 16:33:35 -0800410
411 /* Only one mapping operation per BUI... */
412 if (buip->bui_format.bui_nextents != XFS_BUI_MAX_FAST_EXTENTS)
413 return false;
414
Darrick J. Wongf3ebac42023-02-01 10:16:01 -0800415 map = &buip->bui_format.bui_extents[0];
Darrick J. Wongbc525cf2020-11-29 16:33:35 -0800416
Darrick J. Wongf3ebac42023-02-01 10:16:01 -0800417 if (map->me_flags & ~XFS_BMAP_EXTENT_FLAGS)
Darrick J. Wongbc525cf2020-11-29 16:33:35 -0800418 return false;
419
Darrick J. Wongf3ebac42023-02-01 10:16:01 -0800420 switch (map->me_flags & XFS_BMAP_EXTENT_TYPE_MASK) {
Darrick J. Wongbc525cf2020-11-29 16:33:35 -0800421 case XFS_BMAP_MAP:
422 case XFS_BMAP_UNMAP:
423 break;
424 default:
425 return false;
426 }
427
Darrick J. Wongf3ebac42023-02-01 10:16:01 -0800428 if (!xfs_verify_ino(mp, map->me_owner))
Darrick J. Wong67d86792020-11-29 16:33:36 -0800429 return false;
430
Darrick J. Wongf3ebac42023-02-01 10:16:01 -0800431 if (!xfs_verify_fileext(mp, map->me_startoff, map->me_len))
Darrick J. Wong67d86792020-11-29 16:33:36 -0800432 return false;
433
Darrick J. Wong1b5453b2024-02-22 12:44:24 -0800434 if (map->me_flags & XFS_BMAP_EXTENT_REALTIME)
435 return xfs_verify_rtbext(mp, map->me_startblock, map->me_len);
436
Darrick J. Wongf3ebac42023-02-01 10:16:01 -0800437 return xfs_verify_fsbext(mp, map->me_startblock, map->me_len);
Darrick J. Wongbc525cf2020-11-29 16:33:35 -0800438}
439
Darrick J. Wonge70fb322023-11-22 11:13:03 -0800440static inline struct xfs_bmap_intent *
441xfs_bui_recover_work(
442 struct xfs_mount *mp,
443 struct xfs_defer_pending *dfp,
Darrick J. Wongdb7ccc02023-11-22 13:39:25 -0800444 struct xfs_inode **ipp,
Darrick J. Wonge70fb322023-11-22 11:13:03 -0800445 struct xfs_map_extent *map)
446{
447 struct xfs_bmap_intent *bi;
Darrick J. Wongdb7ccc02023-11-22 13:39:25 -0800448 int error;
449
450 error = xlog_recover_iget(mp, map->me_owner, ipp);
451 if (error)
452 return ERR_PTR(error);
Darrick J. Wonge70fb322023-11-22 11:13:03 -0800453
Dave Chinner2c1e31e2024-01-16 09:59:47 +1100454 bi = kmem_cache_zalloc(xfs_bmap_intent_cache,
455 GFP_KERNEL | __GFP_NOFAIL);
Darrick J. Wonge70fb322023-11-22 11:13:03 -0800456 bi->bi_whichfork = (map->me_flags & XFS_BMAP_EXTENT_ATTR_FORK) ?
457 XFS_ATTR_FORK : XFS_DATA_FORK;
458 bi->bi_type = map->me_flags & XFS_BMAP_EXTENT_TYPE_MASK;
459 bi->bi_bmap.br_startblock = map->me_startblock;
460 bi->bi_bmap.br_startoff = map->me_startoff;
461 bi->bi_bmap.br_blockcount = map->me_len;
462 bi->bi_bmap.br_state = (map->me_flags & XFS_BMAP_EXTENT_UNWRITTEN) ?
463 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
Darrick J. Wongdb7ccc02023-11-22 13:39:25 -0800464 bi->bi_owner = *ipp;
Darrick J. Wonge5f1a512023-11-22 11:25:45 -0800465 xfs_bmap_update_get_group(mp, bi);
Darrick J. Wonge70fb322023-11-22 11:13:03 -0800466
467 xfs_defer_add_item(dfp, &bi->bi_list);
468 return bi;
469}
470
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700471/*
Darrick J. Wong77d61fe2016-10-03 09:11:26 -0700472 * Process a bmap update intent item that was recovered from the log.
473 * We need to update some inode's bmbt.
474 */
Darrick J. Wong9329ba82020-05-01 16:00:52 -0700475STATIC int
Darrick J. Wongdb7ccc02023-11-22 13:39:25 -0800476xfs_bmap_recover_work(
Darrick J. Wonga050acd2023-11-22 10:38:10 -0800477 struct xfs_defer_pending *dfp,
Darrick J. Wonge6fff812020-09-25 17:39:37 -0700478 struct list_head *capture_list)
Darrick J. Wong77d61fe2016-10-03 09:11:26 -0700479{
Darrick J. Wong3c919b02023-09-11 08:39:05 -0700480 struct xfs_trans_res resv;
Darrick J. Wonga050acd2023-11-22 10:38:10 -0800481 struct xfs_log_item *lip = dfp->dfp_intent;
Darrick J. Wong96b60f82020-05-01 16:00:55 -0700482 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
483 struct xfs_trans *tp;
484 struct xfs_inode *ip = NULL;
Dave Chinnerd86142d2022-03-17 09:09:12 -0700485 struct xfs_mount *mp = lip->li_log->l_mp;
Darrick J. Wongddccb812023-02-01 09:50:53 -0800486 struct xfs_map_extent *map;
Darrick J. Wonge5f1a512023-11-22 11:25:45 -0800487 struct xfs_bmap_intent *work;
Chandan Babu R85ef08b2021-01-22 16:48:11 -0800488 int iext_delta;
Darrick J. Wong96b60f82020-05-01 16:00:55 -0700489 int error = 0;
Darrick J. Wong77d61fe2016-10-03 09:11:26 -0700490
Darrick J. Wongbc525cf2020-11-29 16:33:35 -0800491 if (!xfs_bui_validate(mp, buip)) {
492 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
493 &buip->bui_format, sizeof(buip->bui_format));
Darrick J. Wong895e1962019-11-06 09:17:43 -0800494 return -EFSCORRUPTED;
Darrick J. Wongbc525cf2020-11-29 16:33:35 -0800495 }
Darrick J. Wong77d61fe2016-10-03 09:11:26 -0700496
Darrick J. Wongddccb812023-02-01 09:50:53 -0800497 map = &buip->bui_format.bui_extents[0];
Darrick J. Wongdb7ccc02023-11-22 13:39:25 -0800498 work = xfs_bui_recover_work(mp, dfp, &ip, map);
499 if (IS_ERR(work))
500 return PTR_ERR(work);
Darrick J. Wonge6fff812020-09-25 17:39:37 -0700501
Darrick J. Wong64a3f332020-09-25 17:39:50 -0700502 /* Allocate transaction and do the work. */
Darrick J. Wong3c919b02023-09-11 08:39:05 -0700503 resv = xlog_recover_resv(&M_RES(mp)->tr_itruncate);
504 error = xfs_trans_alloc(mp, &resv,
Darrick J. Wong64a3f332020-09-25 17:39:50 -0700505 XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK), 0, 0, &tp);
506 if (error)
507 goto err_rele;
508
Darrick J. Wong64a3f332020-09-25 17:39:50 -0700509 xfs_ilock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700510 xfs_trans_ijoin(tp, ip, 0);
511
Darrick J. Wong1b5453b2024-02-22 12:44:24 -0800512 if (!!(map->me_flags & XFS_BMAP_EXTENT_REALTIME) !=
513 xfs_ifork_is_realtime(ip, work->bi_whichfork)) {
514 error = -EFSCORRUPTED;
515 goto err_cancel;
516 }
517
Darrick J. Wonge5f1a512023-11-22 11:25:45 -0800518 if (work->bi_type == XFS_BMAP_MAP)
Chandan Babu R85ef08b2021-01-22 16:48:11 -0800519 iext_delta = XFS_IEXT_ADD_NOSPLIT_CNT;
520 else
521 iext_delta = XFS_IEXT_PUNCH_HOLE_CNT;
522
Christoph Hellwig25576c52024-05-02 09:33:55 +0200523 error = xfs_iext_count_extend(tp, ip, work->bi_whichfork, iext_delta);
Chandan Babu R85ef08b2021-01-22 16:48:11 -0800524 if (error)
525 goto err_cancel;
Chandan Babu R727e1ac2021-01-22 16:48:11 -0800526
Darrick J. Wonge5f1a512023-11-22 11:25:45 -0800527 error = xlog_recover_finish_intent(tp, dfp);
Darrick J. Wong43059d52021-08-06 11:06:35 -0700528 if (error == -EFSCORRUPTED)
Darrick J. Wonge70fb322023-11-22 11:13:03 -0800529 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
530 &buip->bui_format, sizeof(buip->bui_format));
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700531 if (error)
Darrick J. Wong64a3f332020-09-25 17:39:50 -0700532 goto err_cancel;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700533
Darrick J. Wongff4ab5e2020-09-25 17:39:51 -0700534 /*
535 * Commit transaction, which frees the transaction and saves the inode
536 * for later replay activities.
537 */
Darrick J. Wong512edfa2021-09-16 17:28:07 -0700538 error = xfs_defer_ops_capture_and_commit(tp, capture_list);
Darrick J. Wong64a3f332020-09-25 17:39:50 -0700539 if (error)
540 goto err_unlock;
541
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700542 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong44a87362018-07-25 12:52:32 -0700543 xfs_irele(ip);
Darrick J. Wong64a3f332020-09-25 17:39:50 -0700544 return 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700545
Darrick J. Wong64a3f332020-09-25 17:39:50 -0700546err_cancel:
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700547 xfs_trans_cancel(tp);
Darrick J. Wong64a3f332020-09-25 17:39:50 -0700548err_unlock:
549 xfs_iunlock(ip, XFS_ILOCK_EXCL);
550err_rele:
551 xfs_irele(ip);
Darrick J. Wong77d61fe2016-10-03 09:11:26 -0700552 return error;
553}
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700554
Darrick J. Wong4e919af2020-09-27 16:18:13 -0700555/* Relog an intent item to push the log tail forward. */
556static struct xfs_log_item *
Darrick J. Wonga49c7082023-11-30 12:31:30 -0800557xfs_bmap_relog_intent(
558 struct xfs_trans *tp,
Darrick J. Wong4e919af2020-09-27 16:18:13 -0700559 struct xfs_log_item *intent,
Darrick J. Wonga49c7082023-11-30 12:31:30 -0800560 struct xfs_log_item *done_item)
Darrick J. Wong4e919af2020-09-27 16:18:13 -0700561{
Darrick J. Wong4e919af2020-09-27 16:18:13 -0700562 struct xfs_bui_log_item *buip;
Darrick J. Wongf3ebac42023-02-01 10:16:01 -0800563 struct xfs_map_extent *map;
Darrick J. Wong4e919af2020-09-27 16:18:13 -0700564 unsigned int count;
565
566 count = BUI_ITEM(intent)->bui_format.bui_nextents;
Darrick J. Wongf3ebac42023-02-01 10:16:01 -0800567 map = BUI_ITEM(intent)->bui_format.bui_extents;
Darrick J. Wong4e919af2020-09-27 16:18:13 -0700568
Darrick J. Wong4e919af2020-09-27 16:18:13 -0700569 buip = xfs_bui_init(tp->t_mountp);
Darrick J. Wongf3ebac42023-02-01 10:16:01 -0800570 memcpy(buip->bui_format.bui_extents, map, count * sizeof(*map));
Darrick J. Wong4e919af2020-09-27 16:18:13 -0700571 atomic_set(&buip->bui_next_extent, count);
Darrick J. Wong3e0958b2023-11-30 11:47:45 -0800572
Darrick J. Wong4e919af2020-09-27 16:18:13 -0700573 return &buip->bui_item;
574}
575
Darrick J. Wonga49c7082023-11-30 12:31:30 -0800576const struct xfs_defer_op_type xfs_bmap_update_defer_type = {
Christoph Hellwig7f2f7532023-12-13 10:06:31 +0100577 .name = "bmap",
Darrick J. Wonga49c7082023-11-30 12:31:30 -0800578 .max_items = XFS_BUI_MAX_FAST_EXTENTS,
579 .create_intent = xfs_bmap_update_create_intent,
580 .abort_intent = xfs_bmap_update_abort_intent,
581 .create_done = xfs_bmap_update_create_done,
582 .finish_item = xfs_bmap_update_finish_item,
583 .cancel_item = xfs_bmap_update_cancel_item,
584 .recover_work = xfs_bmap_recover_work,
585 .relog_intent = xfs_bmap_relog_intent,
586};
587
588STATIC bool
589xfs_bui_item_match(
590 struct xfs_log_item *lip,
591 uint64_t intent_id)
592{
593 return BUI_ITEM(lip)->bui_format.bui_id == intent_id;
594}
595
Darrick J. Wong9329ba82020-05-01 16:00:52 -0700596static const struct xfs_item_ops xfs_bui_item_ops = {
Dave Chinnerf5b81202022-05-04 11:46:09 +1000597 .flags = XFS_ITEM_INTENT,
Darrick J. Wong9329ba82020-05-01 16:00:52 -0700598 .iop_size = xfs_bui_item_size,
599 .iop_format = xfs_bui_item_format,
600 .iop_unpin = xfs_bui_item_unpin,
601 .iop_release = xfs_bui_item_release,
Darrick J. Wong154c7332020-05-01 16:00:54 -0700602 .iop_match = xfs_bui_item_match,
Darrick J. Wong9329ba82020-05-01 16:00:52 -0700603};
604
Darrick J. Wonga38ebce2022-10-20 16:17:41 -0700605static inline void
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -0700606xfs_bui_copy_format(
Darrick J. Wonga38ebce2022-10-20 16:17:41 -0700607 struct xfs_bui_log_format *dst,
608 const struct xfs_bui_log_format *src)
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -0700609{
Darrick J. Wonga38ebce2022-10-20 16:17:41 -0700610 unsigned int i;
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -0700611
Darrick J. Wonga38ebce2022-10-20 16:17:41 -0700612 memcpy(dst, src, offsetof(struct xfs_bui_log_format, bui_extents));
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -0700613
Darrick J. Wonga38ebce2022-10-20 16:17:41 -0700614 for (i = 0; i < src->bui_nextents; i++)
615 memcpy(&dst->bui_extents[i], &src->bui_extents[i],
616 sizeof(struct xfs_map_extent));
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -0700617}
618
619/*
620 * This routine is called to create an in-core extent bmap update
621 * item from the bui format structure which was logged on disk.
622 * It allocates an in-core bui, copies the extents from the format
623 * structure into it, and adds the bui to the AIL with the given
624 * LSN.
625 */
626STATIC int
627xlog_recover_bui_commit_pass2(
628 struct xlog *log,
629 struct list_head *buffer_list,
630 struct xlog_recover_item *item,
631 xfs_lsn_t lsn)
632{
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -0700633 struct xfs_mount *mp = log->l_mp;
634 struct xfs_bui_log_item *buip;
635 struct xfs_bui_log_format *bui_formatp;
Darrick J. Wonga38ebce2022-10-20 16:17:41 -0700636 size_t len;
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -0700637
638 bui_formatp = item->ri_buf[0].i_addr;
639
Darrick J. Wonga38ebce2022-10-20 16:17:41 -0700640 if (item->ri_buf[0].i_len < xfs_bui_log_format_sizeof(0)) {
Darrick J. Wong950f0d52022-10-25 15:07:14 -0700641 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
642 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
Darrick J. Wonga38ebce2022-10-20 16:17:41 -0700643 return -EFSCORRUPTED;
644 }
645
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -0700646 if (bui_formatp->bui_nextents != XFS_BUI_MAX_FAST_EXTENTS) {
Darrick J. Wong950f0d52022-10-25 15:07:14 -0700647 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
648 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -0700649 return -EFSCORRUPTED;
650 }
Darrick J. Wonga38ebce2022-10-20 16:17:41 -0700651
652 len = xfs_bui_log_format_sizeof(bui_formatp->bui_nextents);
653 if (item->ri_buf[0].i_len != len) {
Darrick J. Wong950f0d52022-10-25 15:07:14 -0700654 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
655 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
Darrick J. Wonga38ebce2022-10-20 16:17:41 -0700656 return -EFSCORRUPTED;
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -0700657 }
Darrick J. Wonga38ebce2022-10-20 16:17:41 -0700658
659 buip = xfs_bui_init(mp);
660 xfs_bui_copy_format(&buip->bui_format, bui_formatp);
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -0700661 atomic_set(&buip->bui_next_extent, bui_formatp->bui_nextents);
Darrick J. Wong03f77672023-11-22 10:23:23 -0800662
663 xlog_recover_intent_item(log, &buip->bui_item, lsn,
Christoph Hellwigdc22af62023-12-14 06:16:32 +0100664 &xfs_bmap_update_defer_type);
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -0700665 return 0;
666}
667
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700668const struct xlog_recover_item_ops xlog_bui_item_ops = {
669 .item_type = XFS_LI_BUI,
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -0700670 .commit_pass2 = xlog_recover_bui_commit_pass2,
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700671};
672
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -0700673/*
674 * This routine is called when an BUD format structure is found in a committed
675 * transaction in the log. Its purpose is to cancel the corresponding BUI if it
676 * was still in the log. To do this it searches the AIL for the BUI with an id
677 * equal to that in the BUD format structure. If we find it we drop the BUD
678 * reference, which removes the BUI from the AIL and frees it.
679 */
680STATIC int
681xlog_recover_bud_commit_pass2(
682 struct xlog *log,
683 struct list_head *buffer_list,
684 struct xlog_recover_item *item,
685 xfs_lsn_t lsn)
686{
687 struct xfs_bud_log_format *bud_formatp;
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -0700688
689 bud_formatp = item->ri_buf[0].i_addr;
690 if (item->ri_buf[0].i_len != sizeof(struct xfs_bud_log_format)) {
Darrick J. Wong950f0d52022-10-25 15:07:14 -0700691 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
692 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -0700693 return -EFSCORRUPTED;
694 }
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -0700695
Darrick J. Wong154c7332020-05-01 16:00:54 -0700696 xlog_recover_release_intent(log, XFS_LI_BUI, bud_formatp->bud_bui_id);
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -0700697 return 0;
698}
699
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700700const struct xlog_recover_item_ops xlog_bud_item_ops = {
701 .item_type = XFS_LI_BUD,
Darrick J. Wong3c6ba3c2020-05-01 16:00:50 -0700702 .commit_pass2 = xlog_recover_bud_commit_pass2,
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700703};