blob: 7768fb2b713577abe85d32a96a889a995d443567 [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. Wong6413a012016-10-03 09:11:25 -070025
26kmem_zone_t *xfs_bui_zone;
27kmem_zone_t *xfs_bud_zone;
28
29static inline struct xfs_bui_log_item *BUI_ITEM(struct xfs_log_item *lip)
30{
31 return container_of(lip, struct xfs_bui_log_item, bui_item);
32}
33
34void
35xfs_bui_item_free(
36 struct xfs_bui_log_item *buip)
37{
Carlos Maiolino377bcd52019-11-14 12:43:04 -080038 kmem_cache_free(xfs_bui_zone, buip);
Darrick J. Wong6413a012016-10-03 09:11:25 -070039}
40
Dave Chinner0612d112018-04-02 20:08:27 -070041/*
42 * Freeing the BUI requires that we remove it from the AIL if it has already
43 * been placed there. However, the BUI may not yet have been placed in the AIL
44 * when called by xfs_bui_release() from BUD processing due to the ordering of
45 * committed vs unpin operations in bulk insert operations. Hence the reference
46 * count to ensure only the last caller frees the BUI.
47 */
48void
49xfs_bui_release(
50 struct xfs_bui_log_item *buip)
51{
52 ASSERT(atomic_read(&buip->bui_refcount) > 0);
53 if (atomic_dec_and_test(&buip->bui_refcount)) {
54 xfs_trans_ail_remove(&buip->bui_item, SHUTDOWN_LOG_IO_ERROR);
55 xfs_bui_item_free(buip);
56 }
57}
58
59
Darrick J. Wong6413a012016-10-03 09:11:25 -070060STATIC void
61xfs_bui_item_size(
62 struct xfs_log_item *lip,
63 int *nvecs,
64 int *nbytes)
65{
66 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
67
68 *nvecs += 1;
69 *nbytes += xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents);
70}
71
72/*
73 * This is called to fill in the vector of log iovecs for the
74 * given bui log item. We use only 1 iovec, and we point that
75 * at the bui_log_format structure embedded in the bui item.
76 * It is at this point that we assert that all of the extent
77 * slots in the bui item have been filled.
78 */
79STATIC void
80xfs_bui_item_format(
81 struct xfs_log_item *lip,
82 struct xfs_log_vec *lv)
83{
84 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
85 struct xfs_log_iovec *vecp = NULL;
86
87 ASSERT(atomic_read(&buip->bui_next_extent) ==
88 buip->bui_format.bui_nextents);
89
90 buip->bui_format.bui_type = XFS_LI_BUI;
91 buip->bui_format.bui_size = 1;
92
93 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUI_FORMAT, &buip->bui_format,
94 xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents));
95}
96
97/*
Darrick J. Wong6413a012016-10-03 09:11:25 -070098 * The unpin operation is the last place an BUI is manipulated in the log. It is
99 * either inserted in the AIL or aborted in the event of a log I/O error. In
100 * either case, the BUI transaction has been successfully committed to make it
101 * this far. Therefore, we expect whoever committed the BUI to either construct
102 * and commit the BUD or drop the BUD's reference in the event of error. Simply
103 * drop the log's BUI reference now that the log is done with it.
104 */
105STATIC void
106xfs_bui_item_unpin(
107 struct xfs_log_item *lip,
108 int remove)
109{
110 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
111
112 xfs_bui_release(buip);
113}
114
115/*
Darrick J. Wong6413a012016-10-03 09:11:25 -0700116 * The BUI has been either committed or aborted if the transaction has been
117 * cancelled. If the transaction was cancelled, an BUD isn't going to be
118 * constructed and thus we free the BUI here directly.
119 */
120STATIC void
Christoph Hellwigddf92052019-06-28 19:27:32 -0700121xfs_bui_item_release(
Darrick J. Wong6413a012016-10-03 09:11:25 -0700122 struct xfs_log_item *lip)
123{
Christoph Hellwigddf92052019-06-28 19:27:32 -0700124 xfs_bui_release(BUI_ITEM(lip));
Darrick J. Wong6413a012016-10-03 09:11:25 -0700125}
126
Darrick J. Wong6413a012016-10-03 09:11:25 -0700127static const struct xfs_item_ops xfs_bui_item_ops = {
128 .iop_size = xfs_bui_item_size,
129 .iop_format = xfs_bui_item_format,
Darrick J. Wong6413a012016-10-03 09:11:25 -0700130 .iop_unpin = xfs_bui_item_unpin,
Christoph Hellwigddf92052019-06-28 19:27:32 -0700131 .iop_release = xfs_bui_item_release,
Darrick J. Wong6413a012016-10-03 09:11:25 -0700132};
133
134/*
135 * Allocate and initialize an bui item with the given number of extents.
136 */
137struct xfs_bui_log_item *
138xfs_bui_init(
139 struct xfs_mount *mp)
140
141{
142 struct xfs_bui_log_item *buip;
143
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700144 buip = kmem_zone_zalloc(xfs_bui_zone, 0);
Darrick J. Wong6413a012016-10-03 09:11:25 -0700145
146 xfs_log_item_init(mp, &buip->bui_item, XFS_LI_BUI, &xfs_bui_item_ops);
147 buip->bui_format.bui_nextents = XFS_BUI_MAX_FAST_EXTENTS;
148 buip->bui_format.bui_id = (uintptr_t)(void *)buip;
149 atomic_set(&buip->bui_next_extent, 0);
150 atomic_set(&buip->bui_refcount, 2);
151
152 return buip;
153}
154
Darrick J. Wong6413a012016-10-03 09:11:25 -0700155static inline struct xfs_bud_log_item *BUD_ITEM(struct xfs_log_item *lip)
156{
157 return container_of(lip, struct xfs_bud_log_item, bud_item);
158}
159
160STATIC void
161xfs_bud_item_size(
162 struct xfs_log_item *lip,
163 int *nvecs,
164 int *nbytes)
165{
166 *nvecs += 1;
167 *nbytes += sizeof(struct xfs_bud_log_format);
168}
169
170/*
171 * This is called to fill in the vector of log iovecs for the
172 * given bud log item. We use only 1 iovec, and we point that
173 * at the bud_log_format structure embedded in the bud item.
174 * It is at this point that we assert that all of the extent
175 * slots in the bud item have been filled.
176 */
177STATIC void
178xfs_bud_item_format(
179 struct xfs_log_item *lip,
180 struct xfs_log_vec *lv)
181{
182 struct xfs_bud_log_item *budp = BUD_ITEM(lip);
183 struct xfs_log_iovec *vecp = NULL;
184
185 budp->bud_format.bud_type = XFS_LI_BUD;
186 budp->bud_format.bud_size = 1;
187
188 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUD_FORMAT, &budp->bud_format,
189 sizeof(struct xfs_bud_log_format));
190}
191
192/*
Darrick J. Wong6413a012016-10-03 09:11:25 -0700193 * The BUD is either committed or aborted if the transaction is cancelled. If
194 * the transaction is cancelled, drop our reference to the BUI and free the
195 * BUD.
196 */
197STATIC void
Christoph Hellwigddf92052019-06-28 19:27:32 -0700198xfs_bud_item_release(
Darrick J. Wong6413a012016-10-03 09:11:25 -0700199 struct xfs_log_item *lip)
200{
201 struct xfs_bud_log_item *budp = BUD_ITEM(lip);
202
Christoph Hellwigddf92052019-06-28 19:27:32 -0700203 xfs_bui_release(budp->bud_buip);
Carlos Maiolino377bcd52019-11-14 12:43:04 -0800204 kmem_cache_free(xfs_bud_zone, budp);
Darrick J. Wong6413a012016-10-03 09:11:25 -0700205}
206
Darrick J. Wong6413a012016-10-03 09:11:25 -0700207static const struct xfs_item_ops xfs_bud_item_ops = {
Christoph Hellwig9ce632a2019-06-28 19:27:32 -0700208 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED,
Darrick J. Wong6413a012016-10-03 09:11:25 -0700209 .iop_size = xfs_bud_item_size,
210 .iop_format = xfs_bud_item_format,
Christoph Hellwigddf92052019-06-28 19:27:32 -0700211 .iop_release = xfs_bud_item_release,
Darrick J. Wong6413a012016-10-03 09:11:25 -0700212};
213
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700214static struct xfs_bud_log_item *
Christoph Hellwig73f0d232019-06-28 19:27:36 -0700215xfs_trans_get_bud(
216 struct xfs_trans *tp,
Darrick J. Wong6413a012016-10-03 09:11:25 -0700217 struct xfs_bui_log_item *buip)
Darrick J. Wong6413a012016-10-03 09:11:25 -0700218{
Christoph Hellwig73f0d232019-06-28 19:27:36 -0700219 struct xfs_bud_log_item *budp;
Darrick J. Wong6413a012016-10-03 09:11:25 -0700220
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700221 budp = kmem_zone_zalloc(xfs_bud_zone, 0);
Christoph Hellwig73f0d232019-06-28 19:27:36 -0700222 xfs_log_item_init(tp->t_mountp, &budp->bud_item, XFS_LI_BUD,
223 &xfs_bud_item_ops);
Darrick J. Wong6413a012016-10-03 09:11:25 -0700224 budp->bud_buip = buip;
225 budp->bud_format.bud_bui_id = buip->bui_format.bui_id;
226
Christoph Hellwig73f0d232019-06-28 19:27:36 -0700227 xfs_trans_add_item(tp, &budp->bud_item);
Darrick J. Wong6413a012016-10-03 09:11:25 -0700228 return budp;
229}
Darrick J. Wong77d61fe2016-10-03 09:11:26 -0700230
231/*
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700232 * Finish an bmap update and log it to the BUD. Note that the
233 * transaction is marked dirty regardless of whether the bmap update
234 * succeeds or fails to support the BUI/BUD lifecycle rules.
235 */
236static int
237xfs_trans_log_finish_bmap_update(
238 struct xfs_trans *tp,
239 struct xfs_bud_log_item *budp,
240 enum xfs_bmap_intent_type type,
241 struct xfs_inode *ip,
242 int whichfork,
243 xfs_fileoff_t startoff,
244 xfs_fsblock_t startblock,
245 xfs_filblks_t *blockcount,
246 xfs_exntst_t state)
247{
248 int error;
249
250 error = xfs_bmap_finish_one(tp, ip, type, whichfork, startoff,
251 startblock, blockcount, state);
252
253 /*
254 * Mark the transaction dirty, even on error. This ensures the
255 * transaction is aborted, which:
256 *
257 * 1.) releases the BUI and frees the BUD
258 * 2.) shuts down the filesystem
259 */
260 tp->t_flags |= XFS_TRANS_DIRTY;
261 set_bit(XFS_LI_DIRTY, &budp->bud_item.li_flags);
262
263 return error;
264}
265
266/* Sort bmap intents by inode. */
267static int
268xfs_bmap_update_diff_items(
269 void *priv,
270 struct list_head *a,
271 struct list_head *b)
272{
273 struct xfs_bmap_intent *ba;
274 struct xfs_bmap_intent *bb;
275
276 ba = container_of(a, struct xfs_bmap_intent, bi_list);
277 bb = container_of(b, struct xfs_bmap_intent, bi_list);
278 return ba->bi_owner->i_ino - bb->bi_owner->i_ino;
279}
280
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700281/* Set the map extent flags for this mapping. */
282static void
283xfs_trans_set_bmap_flags(
284 struct xfs_map_extent *bmap,
285 enum xfs_bmap_intent_type type,
286 int whichfork,
287 xfs_exntst_t state)
288{
289 bmap->me_flags = 0;
290 switch (type) {
291 case XFS_BMAP_MAP:
292 case XFS_BMAP_UNMAP:
293 bmap->me_flags = type;
294 break;
295 default:
296 ASSERT(0);
297 }
298 if (state == XFS_EXT_UNWRITTEN)
299 bmap->me_flags |= XFS_BMAP_EXTENT_UNWRITTEN;
300 if (whichfork == XFS_ATTR_FORK)
301 bmap->me_flags |= XFS_BMAP_EXTENT_ATTR_FORK;
302}
303
304/* Log bmap updates in the intent item. */
305STATIC void
306xfs_bmap_update_log_item(
307 struct xfs_trans *tp,
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700308 struct xfs_bui_log_item *buip,
309 struct xfs_bmap_intent *bmap)
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700310{
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700311 uint next_extent;
312 struct xfs_map_extent *map;
313
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700314 tp->t_flags |= XFS_TRANS_DIRTY;
315 set_bit(XFS_LI_DIRTY, &buip->bui_item.li_flags);
316
317 /*
318 * atomic_inc_return gives us the value after the increment;
319 * we want to use it as an array index so we need to subtract 1 from
320 * it.
321 */
322 next_extent = atomic_inc_return(&buip->bui_next_extent) - 1;
323 ASSERT(next_extent < buip->bui_format.bui_nextents);
324 map = &buip->bui_format.bui_extents[next_extent];
325 map->me_owner = bmap->bi_owner->i_ino;
326 map->me_startblock = bmap->bi_bmap.br_startblock;
327 map->me_startoff = bmap->bi_bmap.br_startoff;
328 map->me_len = bmap->bi_bmap.br_blockcount;
329 xfs_trans_set_bmap_flags(map, bmap->bi_type, bmap->bi_whichfork,
330 bmap->bi_bmap.br_state);
331}
332
Christoph Hellwig13a83332020-04-30 12:52:21 -0700333static struct xfs_log_item *
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700334xfs_bmap_update_create_intent(
335 struct xfs_trans *tp,
336 struct list_head *items,
Christoph Hellwigd367a862020-04-30 12:52:20 -0700337 unsigned int count,
338 bool sort)
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700339{
Christoph Hellwigd367a862020-04-30 12:52:20 -0700340 struct xfs_mount *mp = tp->t_mountp;
341 struct xfs_bui_log_item *buip = xfs_bui_init(mp);
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700342 struct xfs_bmap_intent *bmap;
343
344 ASSERT(count == XFS_BUI_MAX_FAST_EXTENTS);
345
346 xfs_trans_add_item(tp, &buip->bui_item);
Christoph Hellwigd367a862020-04-30 12:52:20 -0700347 if (sort)
348 list_sort(mp, items, xfs_bmap_update_diff_items);
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700349 list_for_each_entry(bmap, items, bi_list)
350 xfs_bmap_update_log_item(tp, buip, bmap);
Christoph Hellwig13a83332020-04-30 12:52:21 -0700351 return &buip->bui_item;
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700352}
353
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700354/* Get an BUD so we can process all the deferred rmap updates. */
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700355static struct xfs_log_item *
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700356xfs_bmap_update_create_done(
357 struct xfs_trans *tp,
Christoph Hellwig13a83332020-04-30 12:52:21 -0700358 struct xfs_log_item *intent,
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700359 unsigned int count)
360{
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700361 return &xfs_trans_get_bud(tp, BUI_ITEM(intent))->bud_item;
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700362}
363
364/* Process a deferred rmap update. */
365STATIC int
366xfs_bmap_update_finish_item(
367 struct xfs_trans *tp,
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700368 struct xfs_log_item *done,
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700369 struct list_head *item,
Christoph Hellwig3ec1b262020-04-30 12:52:22 -0700370 struct xfs_btree_cur **state)
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700371{
372 struct xfs_bmap_intent *bmap;
373 xfs_filblks_t count;
374 int error;
375
376 bmap = container_of(item, struct xfs_bmap_intent, bi_list);
377 count = bmap->bi_bmap.br_blockcount;
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700378 error = xfs_trans_log_finish_bmap_update(tp, BUD_ITEM(done),
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700379 bmap->bi_type,
380 bmap->bi_owner, bmap->bi_whichfork,
381 bmap->bi_bmap.br_startoff,
382 bmap->bi_bmap.br_startblock,
383 &count,
384 bmap->bi_bmap.br_state);
385 if (!error && count > 0) {
386 ASSERT(bmap->bi_type == XFS_BMAP_UNMAP);
387 bmap->bi_bmap.br_blockcount = count;
388 return -EAGAIN;
389 }
390 kmem_free(bmap);
391 return error;
392}
393
394/* Abort all pending BUIs. */
395STATIC void
396xfs_bmap_update_abort_intent(
Christoph Hellwig13a83332020-04-30 12:52:21 -0700397 struct xfs_log_item *intent)
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700398{
Christoph Hellwig13a83332020-04-30 12:52:21 -0700399 xfs_bui_release(BUI_ITEM(intent));
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700400}
401
402/* Cancel a deferred rmap update. */
403STATIC void
404xfs_bmap_update_cancel_item(
405 struct list_head *item)
406{
407 struct xfs_bmap_intent *bmap;
408
409 bmap = container_of(item, struct xfs_bmap_intent, bi_list);
410 kmem_free(bmap);
411}
412
413const struct xfs_defer_op_type xfs_bmap_update_defer_type = {
414 .max_items = XFS_BUI_MAX_FAST_EXTENTS,
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700415 .create_intent = xfs_bmap_update_create_intent,
416 .abort_intent = xfs_bmap_update_abort_intent,
Christoph Hellwigcaeaea92019-06-28 19:29:42 -0700417 .create_done = xfs_bmap_update_create_done,
418 .finish_item = xfs_bmap_update_finish_item,
419 .cancel_item = xfs_bmap_update_cancel_item,
420};
421
422/*
Darrick J. Wong77d61fe2016-10-03 09:11:26 -0700423 * Process a bmap update intent item that was recovered from the log.
424 * We need to update some inode's bmbt.
425 */
426int
427xfs_bui_recover(
Brian Fosterfbfa9772018-08-01 07:20:29 -0700428 struct xfs_trans *parent_tp,
429 struct xfs_bui_log_item *buip)
Darrick J. Wong77d61fe2016-10-03 09:11:26 -0700430{
431 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700432 unsigned int bui_type;
Darrick J. Wong77d61fe2016-10-03 09:11:26 -0700433 struct xfs_map_extent *bmap;
434 xfs_fsblock_t startblock_fsb;
435 xfs_fsblock_t inode_fsb;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -0700436 xfs_filblks_t count;
Darrick J. Wong77d61fe2016-10-03 09:11:26 -0700437 bool op_ok;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700438 struct xfs_bud_log_item *budp;
439 enum xfs_bmap_intent_type type;
440 int whichfork;
441 xfs_exntst_t state;
442 struct xfs_trans *tp;
443 struct xfs_inode *ip = NULL;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -0700444 struct xfs_bmbt_irec irec;
Brian Fosterfbfa9772018-08-01 07:20:29 -0700445 struct xfs_mount *mp = parent_tp->t_mountp;
Darrick J. Wong77d61fe2016-10-03 09:11:26 -0700446
447 ASSERT(!test_bit(XFS_BUI_RECOVERED, &buip->bui_flags));
448
449 /* Only one mapping operation per BUI... */
450 if (buip->bui_format.bui_nextents != XFS_BUI_MAX_FAST_EXTENTS) {
451 set_bit(XFS_BUI_RECOVERED, &buip->bui_flags);
452 xfs_bui_release(buip);
Darrick J. Wong895e1962019-11-06 09:17:43 -0800453 return -EFSCORRUPTED;
Darrick J. Wong77d61fe2016-10-03 09:11:26 -0700454 }
455
456 /*
457 * First check the validity of the extent described by the
458 * BUI. If anything is bad, then toss the BUI.
459 */
460 bmap = &buip->bui_format.bui_extents[0];
461 startblock_fsb = XFS_BB_TO_FSB(mp,
462 XFS_FSB_TO_DADDR(mp, bmap->me_startblock));
463 inode_fsb = XFS_BB_TO_FSB(mp, XFS_FSB_TO_DADDR(mp,
464 XFS_INO_TO_FSB(mp, bmap->me_owner)));
465 switch (bmap->me_flags & XFS_BMAP_EXTENT_TYPE_MASK) {
466 case XFS_BMAP_MAP:
467 case XFS_BMAP_UNMAP:
468 op_ok = true;
469 break;
470 default:
471 op_ok = false;
472 break;
473 }
474 if (!op_ok || startblock_fsb == 0 ||
475 bmap->me_len == 0 ||
476 inode_fsb == 0 ||
477 startblock_fsb >= mp->m_sb.sb_dblocks ||
478 bmap->me_len >= mp->m_sb.sb_agblocks ||
479 inode_fsb >= mp->m_sb.sb_dblocks ||
480 (bmap->me_flags & ~XFS_BMAP_EXTENT_FLAGS)) {
481 /*
482 * This will pull the BUI from the AIL and
483 * free the memory associated with it.
484 */
485 set_bit(XFS_BUI_RECOVERED, &buip->bui_flags);
486 xfs_bui_release(buip);
Darrick J. Wong895e1962019-11-06 09:17:43 -0800487 return -EFSCORRUPTED;
Darrick J. Wong77d61fe2016-10-03 09:11:26 -0700488 }
489
Darrick J. Wongfe0be232017-04-12 12:26:07 -0700490 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
491 XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK), 0, 0, &tp);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700492 if (error)
493 return error;
Brian Foster91ef75b2018-07-24 13:43:13 -0700494 /*
495 * Recovery stashes all deferred ops during intent processing and
496 * finishes them on completion. Transfer current dfops state to this
497 * transaction and transfer the result back before we return.
498 */
Brian Fosterce356d62018-08-01 07:20:30 -0700499 xfs_defer_move(tp, parent_tp);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700500 budp = xfs_trans_get_bud(tp, buip);
501
502 /* Grab the inode. */
503 error = xfs_iget(mp, tp, bmap->me_owner, 0, XFS_ILOCK_EXCL, &ip);
504 if (error)
505 goto err_inode;
506
Darrick J. Wong17c12bc2016-10-03 09:11:29 -0700507 if (VFS_I(ip)->i_nlink == 0)
508 xfs_iflags_set(ip, XFS_IRECOVERY);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700509
510 /* Process deferred bmap item. */
511 state = (bmap->me_flags & XFS_BMAP_EXTENT_UNWRITTEN) ?
512 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
513 whichfork = (bmap->me_flags & XFS_BMAP_EXTENT_ATTR_FORK) ?
514 XFS_ATTR_FORK : XFS_DATA_FORK;
515 bui_type = bmap->me_flags & XFS_BMAP_EXTENT_TYPE_MASK;
516 switch (bui_type) {
517 case XFS_BMAP_MAP:
518 case XFS_BMAP_UNMAP:
519 type = bui_type;
520 break;
521 default:
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700522 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700523 error = -EFSCORRUPTED;
Darrick J. Wong50995582017-11-21 20:53:02 -0800524 goto err_inode;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700525 }
526 xfs_trans_ijoin(tp, ip, 0);
527
Darrick J. Wonge1a4e372017-06-14 21:25:57 -0700528 count = bmap->me_len;
Brian Foster7dbddba2018-08-01 07:20:32 -0700529 error = xfs_trans_log_finish_bmap_update(tp, budp, type, ip, whichfork,
530 bmap->me_startoff, bmap->me_startblock, &count, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700531 if (error)
Darrick J. Wong50995582017-11-21 20:53:02 -0800532 goto err_inode;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700533
Darrick J. Wonge1a4e372017-06-14 21:25:57 -0700534 if (count > 0) {
535 ASSERT(type == XFS_BMAP_UNMAP);
536 irec.br_startblock = bmap->me_startblock;
537 irec.br_blockcount = count;
538 irec.br_startoff = bmap->me_startoff;
539 irec.br_state = state;
Darrick J. Wong3e08f422019-08-26 17:06:04 -0700540 xfs_bmap_unmap_extent(tp, ip, &irec);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -0700541 }
542
Darrick J. Wong77d61fe2016-10-03 09:11:26 -0700543 set_bit(XFS_BUI_RECOVERED, &buip->bui_flags);
Brian Fosterce356d62018-08-01 07:20:30 -0700544 xfs_defer_move(parent_tp, tp);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700545 error = xfs_trans_commit(tp);
546 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong44a87362018-07-25 12:52:32 -0700547 xfs_irele(ip);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700548
549 return error;
550
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700551err_inode:
Brian Fosterce356d62018-08-01 07:20:30 -0700552 xfs_defer_move(parent_tp, tp);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700553 xfs_trans_cancel(tp);
554 if (ip) {
555 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong44a87362018-07-25 12:52:32 -0700556 xfs_irele(ip);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700557 }
Darrick J. Wong77d61fe2016-10-03 09:11:26 -0700558 return error;
559}