blob: 0f3af9f0576426eba87c74461b01b2d67700306e [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0+
Darrick J. Wong5880f2d72016-08-03 12:04:45 +10002/*
3 * Copyright (C) 2016 Oracle. All Rights Reserved.
Darrick J. Wong5880f2d72016-08-03 12:04:45 +10004 * Author: Darrick J. Wong <darrick.wong@oracle.com>
Darrick J. Wong5880f2d72016-08-03 12:04:45 +10005 */
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. Wong9e88b5d2016-08-03 12:09:48 +100011#include "xfs_bit.h"
Darrick J. Wongb31c2bd2018-02-22 14:41:25 -080012#include "xfs_shared.h"
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100013#include "xfs_mount.h"
Darrick J. Wong9c194642016-08-03 12:16:05 +100014#include "xfs_defer.h"
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100015#include "xfs_trans.h"
16#include "xfs_trans_priv.h"
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100017#include "xfs_rmap_item.h"
18#include "xfs_log.h"
Darrick J. Wong9c194642016-08-03 12:16:05 +100019#include "xfs_rmap.h"
Darrick J. Wonga5155b82019-11-02 09:40:53 -070020#include "xfs_error.h"
Darrick J. Wong86ffa472020-05-01 16:00:45 -070021#include "xfs_log_recover.h"
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100022
23kmem_zone_t *xfs_rui_zone;
24kmem_zone_t *xfs_rud_zone;
25
26static inline struct xfs_rui_log_item *RUI_ITEM(struct xfs_log_item *lip)
27{
28 return container_of(lip, struct xfs_rui_log_item, rui_item);
29}
30
31void
32xfs_rui_item_free(
33 struct xfs_rui_log_item *ruip)
34{
35 if (ruip->rui_format.rui_nextents > XFS_RUI_MAX_FAST_EXTENTS)
36 kmem_free(ruip);
37 else
Carlos Maiolino377bcd52019-11-14 12:43:04 -080038 kmem_cache_free(xfs_rui_zone, ruip);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100039}
40
Dave Chinner0612d112018-04-02 20:08:27 -070041/*
42 * Freeing the RUI requires that we remove it from the AIL if it has already
43 * been placed there. However, the RUI may not yet have been placed in the AIL
44 * when called by xfs_rui_release() from RUD 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 RUI.
47 */
48void
49xfs_rui_release(
50 struct xfs_rui_log_item *ruip)
51{
52 ASSERT(atomic_read(&ruip->rui_refcount) > 0);
53 if (atomic_dec_and_test(&ruip->rui_refcount)) {
Brian Foster65587922020-05-06 13:25:23 -070054 xfs_trans_ail_delete(&ruip->rui_item, SHUTDOWN_LOG_IO_ERROR);
Dave Chinner0612d112018-04-02 20:08:27 -070055 xfs_rui_item_free(ruip);
56 }
57}
58
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100059STATIC void
60xfs_rui_item_size(
61 struct xfs_log_item *lip,
62 int *nvecs,
63 int *nbytes)
64{
Darrick J. Wongcd001582016-09-19 10:24:27 +100065 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
66
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100067 *nvecs += 1;
Darrick J. Wongcd001582016-09-19 10:24:27 +100068 *nbytes += xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100069}
70
71/*
72 * This is called to fill in the vector of log iovecs for the
73 * given rui log item. We use only 1 iovec, and we point that
74 * at the rui_log_format structure embedded in the rui item.
75 * It is at this point that we assert that all of the extent
76 * slots in the rui item have been filled.
77 */
78STATIC void
79xfs_rui_item_format(
80 struct xfs_log_item *lip,
81 struct xfs_log_vec *lv)
82{
83 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
84 struct xfs_log_iovec *vecp = NULL;
85
86 ASSERT(atomic_read(&ruip->rui_next_extent) ==
87 ruip->rui_format.rui_nextents);
88
89 ruip->rui_format.rui_type = XFS_LI_RUI;
90 ruip->rui_format.rui_size = 1;
91
92 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUI_FORMAT, &ruip->rui_format,
Darrick J. Wongcd001582016-09-19 10:24:27 +100093 xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents));
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100094}
95
96/*
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100097 * The unpin operation is the last place an RUI is manipulated in the log. It is
98 * either inserted in the AIL or aborted in the event of a log I/O error. In
99 * either case, the RUI transaction has been successfully committed to make it
100 * this far. Therefore, we expect whoever committed the RUI to either construct
101 * and commit the RUD or drop the RUD's reference in the event of error. Simply
102 * drop the log's RUI reference now that the log is done with it.
103 */
104STATIC void
105xfs_rui_item_unpin(
106 struct xfs_log_item *lip,
107 int remove)
108{
109 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
110
111 xfs_rui_release(ruip);
112}
113
114/*
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000115 * The RUI has been either committed or aborted if the transaction has been
116 * cancelled. If the transaction was cancelled, an RUD isn't going to be
117 * constructed and thus we free the RUI here directly.
118 */
119STATIC void
Christoph Hellwigddf92052019-06-28 19:27:32 -0700120xfs_rui_item_release(
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000121 struct xfs_log_item *lip)
122{
Christoph Hellwigddf92052019-06-28 19:27:32 -0700123 xfs_rui_release(RUI_ITEM(lip));
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000124}
125
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000126static const struct xfs_item_ops xfs_rui_item_ops = {
127 .iop_size = xfs_rui_item_size,
128 .iop_format = xfs_rui_item_format,
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000129 .iop_unpin = xfs_rui_item_unpin,
Christoph Hellwigddf92052019-06-28 19:27:32 -0700130 .iop_release = xfs_rui_item_release,
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000131};
132
133/*
134 * Allocate and initialize an rui item with the given number of extents.
135 */
136struct xfs_rui_log_item *
137xfs_rui_init(
138 struct xfs_mount *mp,
139 uint nextents)
140
141{
142 struct xfs_rui_log_item *ruip;
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000143
144 ASSERT(nextents > 0);
Darrick J. Wongcd001582016-09-19 10:24:27 +1000145 if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700146 ruip = kmem_zalloc(xfs_rui_log_item_sizeof(nextents), 0);
Darrick J. Wongcd001582016-09-19 10:24:27 +1000147 else
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700148 ruip = kmem_zone_zalloc(xfs_rui_zone, 0);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000149
150 xfs_log_item_init(mp, &ruip->rui_item, XFS_LI_RUI, &xfs_rui_item_ops);
151 ruip->rui_format.rui_nextents = nextents;
152 ruip->rui_format.rui_id = (uintptr_t)(void *)ruip;
153 atomic_set(&ruip->rui_next_extent, 0);
154 atomic_set(&ruip->rui_refcount, 2);
155
156 return ruip;
157}
158
159/*
160 * Copy an RUI format buffer from the given buf, and into the destination
161 * RUI format structure. The RUI/RUD items were designed not to need any
162 * special alignment handling.
163 */
164int
165xfs_rui_copy_format(
166 struct xfs_log_iovec *buf,
167 struct xfs_rui_log_format *dst_rui_fmt)
168{
169 struct xfs_rui_log_format *src_rui_fmt;
170 uint len;
171
172 src_rui_fmt = buf->i_addr;
Darrick J. Wongcd001582016-09-19 10:24:27 +1000173 len = xfs_rui_log_format_sizeof(src_rui_fmt->rui_nextents);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000174
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700175 if (buf->i_len != len) {
176 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000177 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700178 }
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000179
Darrick J. Wongcd001582016-09-19 10:24:27 +1000180 memcpy(dst_rui_fmt, src_rui_fmt, len);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000181 return 0;
182}
183
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000184static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip)
185{
186 return container_of(lip, struct xfs_rud_log_item, rud_item);
187}
188
189STATIC void
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000190xfs_rud_item_size(
191 struct xfs_log_item *lip,
192 int *nvecs,
193 int *nbytes)
194{
195 *nvecs += 1;
Darrick J. Wong722e2512016-08-03 12:28:43 +1000196 *nbytes += sizeof(struct xfs_rud_log_format);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000197}
198
199/*
200 * This is called to fill in the vector of log iovecs for the
201 * given rud log item. We use only 1 iovec, and we point that
202 * at the rud_log_format structure embedded in the rud item.
203 * It is at this point that we assert that all of the extent
204 * slots in the rud item have been filled.
205 */
206STATIC void
207xfs_rud_item_format(
208 struct xfs_log_item *lip,
209 struct xfs_log_vec *lv)
210{
211 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
212 struct xfs_log_iovec *vecp = NULL;
213
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000214 rudp->rud_format.rud_type = XFS_LI_RUD;
215 rudp->rud_format.rud_size = 1;
216
217 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format,
Darrick J. Wong722e2512016-08-03 12:28:43 +1000218 sizeof(struct xfs_rud_log_format));
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000219}
220
221/*
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000222 * The RUD is either committed or aborted if the transaction is cancelled. If
223 * the transaction is cancelled, drop our reference to the RUI and free the
224 * RUD.
225 */
226STATIC void
Christoph Hellwigddf92052019-06-28 19:27:32 -0700227xfs_rud_item_release(
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000228 struct xfs_log_item *lip)
229{
230 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
231
Christoph Hellwigddf92052019-06-28 19:27:32 -0700232 xfs_rui_release(rudp->rud_ruip);
Carlos Maiolino377bcd52019-11-14 12:43:04 -0800233 kmem_cache_free(xfs_rud_zone, rudp);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000234}
235
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000236static const struct xfs_item_ops xfs_rud_item_ops = {
Christoph Hellwig9ce632a2019-06-28 19:27:32 -0700237 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED,
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000238 .iop_size = xfs_rud_item_size,
239 .iop_format = xfs_rud_item_format,
Christoph Hellwigddf92052019-06-28 19:27:32 -0700240 .iop_release = xfs_rud_item_release,
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000241};
242
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700243static struct xfs_rud_log_item *
Christoph Hellwig608834472019-06-28 19:27:36 -0700244xfs_trans_get_rud(
245 struct xfs_trans *tp,
Darrick J. Wong722e2512016-08-03 12:28:43 +1000246 struct xfs_rui_log_item *ruip)
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000247{
Christoph Hellwig608834472019-06-28 19:27:36 -0700248 struct xfs_rud_log_item *rudp;
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000249
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700250 rudp = kmem_zone_zalloc(xfs_rud_zone, 0);
Christoph Hellwig608834472019-06-28 19:27:36 -0700251 xfs_log_item_init(tp->t_mountp, &rudp->rud_item, XFS_LI_RUD,
252 &xfs_rud_item_ops);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000253 rudp->rud_ruip = ruip;
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000254 rudp->rud_format.rud_rui_id = ruip->rui_format.rui_id;
255
Christoph Hellwig608834472019-06-28 19:27:36 -0700256 xfs_trans_add_item(tp, &rudp->rud_item);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000257 return rudp;
258}
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000259
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700260/* Set the map extent flags for this reverse mapping. */
261static void
262xfs_trans_set_rmap_flags(
263 struct xfs_map_extent *rmap,
264 enum xfs_rmap_intent_type type,
265 int whichfork,
266 xfs_exntst_t state)
267{
268 rmap->me_flags = 0;
269 if (state == XFS_EXT_UNWRITTEN)
270 rmap->me_flags |= XFS_RMAP_EXTENT_UNWRITTEN;
271 if (whichfork == XFS_ATTR_FORK)
272 rmap->me_flags |= XFS_RMAP_EXTENT_ATTR_FORK;
273 switch (type) {
274 case XFS_RMAP_MAP:
275 rmap->me_flags |= XFS_RMAP_EXTENT_MAP;
276 break;
277 case XFS_RMAP_MAP_SHARED:
278 rmap->me_flags |= XFS_RMAP_EXTENT_MAP_SHARED;
279 break;
280 case XFS_RMAP_UNMAP:
281 rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP;
282 break;
283 case XFS_RMAP_UNMAP_SHARED:
284 rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP_SHARED;
285 break;
286 case XFS_RMAP_CONVERT:
287 rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT;
288 break;
289 case XFS_RMAP_CONVERT_SHARED:
290 rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT_SHARED;
291 break;
292 case XFS_RMAP_ALLOC:
293 rmap->me_flags |= XFS_RMAP_EXTENT_ALLOC;
294 break;
295 case XFS_RMAP_FREE:
296 rmap->me_flags |= XFS_RMAP_EXTENT_FREE;
297 break;
298 default:
299 ASSERT(0);
300 }
301}
302
303/*
304 * Finish an rmap update and log it to the RUD. Note that the transaction is
305 * marked dirty regardless of whether the rmap update succeeds or fails to
306 * support the RUI/RUD lifecycle rules.
307 */
308static int
309xfs_trans_log_finish_rmap_update(
310 struct xfs_trans *tp,
311 struct xfs_rud_log_item *rudp,
312 enum xfs_rmap_intent_type type,
313 uint64_t owner,
314 int whichfork,
315 xfs_fileoff_t startoff,
316 xfs_fsblock_t startblock,
317 xfs_filblks_t blockcount,
318 xfs_exntst_t state,
319 struct xfs_btree_cur **pcur)
320{
321 int error;
322
323 error = xfs_rmap_finish_one(tp, type, owner, whichfork, startoff,
324 startblock, blockcount, state, pcur);
325
326 /*
327 * Mark the transaction dirty, even on error. This ensures the
328 * transaction is aborted, which:
329 *
330 * 1.) releases the RUI and frees the RUD
331 * 2.) shuts down the filesystem
332 */
333 tp->t_flags |= XFS_TRANS_DIRTY;
334 set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags);
335
336 return error;
337}
338
339/* Sort rmap intents by AG. */
340static int
341xfs_rmap_update_diff_items(
342 void *priv,
343 struct list_head *a,
344 struct list_head *b)
345{
346 struct xfs_mount *mp = priv;
347 struct xfs_rmap_intent *ra;
348 struct xfs_rmap_intent *rb;
349
350 ra = container_of(a, struct xfs_rmap_intent, ri_list);
351 rb = container_of(b, struct xfs_rmap_intent, ri_list);
352 return XFS_FSB_TO_AGNO(mp, ra->ri_bmap.br_startblock) -
353 XFS_FSB_TO_AGNO(mp, rb->ri_bmap.br_startblock);
354}
355
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700356/* Log rmap updates in the intent item. */
357STATIC void
358xfs_rmap_update_log_item(
359 struct xfs_trans *tp,
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700360 struct xfs_rui_log_item *ruip,
361 struct xfs_rmap_intent *rmap)
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700362{
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700363 uint next_extent;
364 struct xfs_map_extent *map;
365
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700366 tp->t_flags |= XFS_TRANS_DIRTY;
367 set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags);
368
369 /*
370 * atomic_inc_return gives us the value after the increment;
371 * we want to use it as an array index so we need to subtract 1 from
372 * it.
373 */
374 next_extent = atomic_inc_return(&ruip->rui_next_extent) - 1;
375 ASSERT(next_extent < ruip->rui_format.rui_nextents);
376 map = &ruip->rui_format.rui_extents[next_extent];
377 map->me_owner = rmap->ri_owner;
378 map->me_startblock = rmap->ri_bmap.br_startblock;
379 map->me_startoff = rmap->ri_bmap.br_startoff;
380 map->me_len = rmap->ri_bmap.br_blockcount;
381 xfs_trans_set_rmap_flags(map, rmap->ri_type, rmap->ri_whichfork,
382 rmap->ri_bmap.br_state);
383}
384
Christoph Hellwig13a83332020-04-30 12:52:21 -0700385static struct xfs_log_item *
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700386xfs_rmap_update_create_intent(
387 struct xfs_trans *tp,
388 struct list_head *items,
Christoph Hellwigd367a862020-04-30 12:52:20 -0700389 unsigned int count,
390 bool sort)
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700391{
392 struct xfs_mount *mp = tp->t_mountp;
393 struct xfs_rui_log_item *ruip = xfs_rui_init(mp, count);
394 struct xfs_rmap_intent *rmap;
395
396 ASSERT(count > 0);
397
398 xfs_trans_add_item(tp, &ruip->rui_item);
Christoph Hellwigd367a862020-04-30 12:52:20 -0700399 if (sort)
400 list_sort(mp, items, xfs_rmap_update_diff_items);
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700401 list_for_each_entry(rmap, items, ri_list)
402 xfs_rmap_update_log_item(tp, ruip, rmap);
Christoph Hellwig13a83332020-04-30 12:52:21 -0700403 return &ruip->rui_item;
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700404}
405
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700406/* Get an RUD so we can process all the deferred rmap updates. */
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700407static struct xfs_log_item *
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700408xfs_rmap_update_create_done(
409 struct xfs_trans *tp,
Christoph Hellwig13a83332020-04-30 12:52:21 -0700410 struct xfs_log_item *intent,
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700411 unsigned int count)
412{
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700413 return &xfs_trans_get_rud(tp, RUI_ITEM(intent))->rud_item;
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700414}
415
416/* Process a deferred rmap update. */
417STATIC int
418xfs_rmap_update_finish_item(
419 struct xfs_trans *tp,
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700420 struct xfs_log_item *done,
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700421 struct list_head *item,
Christoph Hellwig3ec1b262020-04-30 12:52:22 -0700422 struct xfs_btree_cur **state)
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700423{
424 struct xfs_rmap_intent *rmap;
425 int error;
426
427 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700428 error = xfs_trans_log_finish_rmap_update(tp, RUD_ITEM(done),
Christoph Hellwig3ec1b262020-04-30 12:52:22 -0700429 rmap->ri_type, rmap->ri_owner, rmap->ri_whichfork,
430 rmap->ri_bmap.br_startoff, rmap->ri_bmap.br_startblock,
431 rmap->ri_bmap.br_blockcount, rmap->ri_bmap.br_state,
432 state);
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700433 kmem_free(rmap);
434 return error;
435}
436
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700437/* Abort all pending RUIs. */
438STATIC void
439xfs_rmap_update_abort_intent(
Christoph Hellwig13a83332020-04-30 12:52:21 -0700440 struct xfs_log_item *intent)
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700441{
Christoph Hellwig13a83332020-04-30 12:52:21 -0700442 xfs_rui_release(RUI_ITEM(intent));
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700443}
444
445/* Cancel a deferred rmap update. */
446STATIC void
447xfs_rmap_update_cancel_item(
448 struct list_head *item)
449{
450 struct xfs_rmap_intent *rmap;
451
452 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
453 kmem_free(rmap);
454}
455
456const struct xfs_defer_op_type xfs_rmap_update_defer_type = {
457 .max_items = XFS_RUI_MAX_FAST_EXTENTS,
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700458 .create_intent = xfs_rmap_update_create_intent,
459 .abort_intent = xfs_rmap_update_abort_intent,
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700460 .create_done = xfs_rmap_update_create_done,
461 .finish_item = xfs_rmap_update_finish_item,
Christoph Hellwig3ec1b262020-04-30 12:52:22 -0700462 .finish_cleanup = xfs_rmap_finish_one_cleanup,
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700463 .cancel_item = xfs_rmap_update_cancel_item,
464};
465
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000466/*
467 * Process an rmap update intent item that was recovered from the log.
468 * We need to update the rmapbt.
469 */
470int
471xfs_rui_recover(
472 struct xfs_mount *mp,
473 struct xfs_rui_log_item *ruip)
474{
475 int i;
476 int error = 0;
477 struct xfs_map_extent *rmap;
478 xfs_fsblock_t startblock_fsb;
479 bool op_ok;
Darrick J. Wong9c194642016-08-03 12:16:05 +1000480 struct xfs_rud_log_item *rudp;
481 enum xfs_rmap_intent_type type;
482 int whichfork;
483 xfs_exntst_t state;
484 struct xfs_trans *tp;
485 struct xfs_btree_cur *rcur = NULL;
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000486
487 ASSERT(!test_bit(XFS_RUI_RECOVERED, &ruip->rui_flags));
488
489 /*
490 * First check the validity of the extents described by the
491 * RUI. If any are bad, then assume that all are bad and
492 * just toss the RUI.
493 */
494 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
Darrick J. Wonge127faf2016-08-03 12:29:32 +1000495 rmap = &ruip->rui_format.rui_extents[i];
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000496 startblock_fsb = XFS_BB_TO_FSB(mp,
497 XFS_FSB_TO_DADDR(mp, rmap->me_startblock));
498 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
499 case XFS_RMAP_EXTENT_MAP:
Darrick J. Wong0e07c032016-10-03 09:11:47 -0700500 case XFS_RMAP_EXTENT_MAP_SHARED:
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000501 case XFS_RMAP_EXTENT_UNMAP:
Darrick J. Wong0e07c032016-10-03 09:11:47 -0700502 case XFS_RMAP_EXTENT_UNMAP_SHARED:
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000503 case XFS_RMAP_EXTENT_CONVERT:
Darrick J. Wong0e07c032016-10-03 09:11:47 -0700504 case XFS_RMAP_EXTENT_CONVERT_SHARED:
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000505 case XFS_RMAP_EXTENT_ALLOC:
506 case XFS_RMAP_EXTENT_FREE:
507 op_ok = true;
508 break;
509 default:
510 op_ok = false;
511 break;
512 }
Darrick J. Wonge127faf2016-08-03 12:29:32 +1000513 if (!op_ok || startblock_fsb == 0 ||
514 rmap->me_len == 0 ||
515 startblock_fsb >= mp->m_sb.sb_dblocks ||
516 rmap->me_len >= mp->m_sb.sb_agblocks ||
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000517 (rmap->me_flags & ~XFS_RMAP_EXTENT_FLAGS)) {
518 /*
519 * This will pull the RUI from the AIL and
520 * free the memory associated with it.
521 */
522 set_bit(XFS_RUI_RECOVERED, &ruip->rui_flags);
523 xfs_rui_release(ruip);
Darrick J. Wong895e1962019-11-06 09:17:43 -0800524 return -EFSCORRUPTED;
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000525 }
526 }
527
Darrick J. Wongb31c2bd2018-02-22 14:41:25 -0800528 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
529 mp->m_rmap_maxlevels, 0, XFS_TRANS_RESERVE, &tp);
Darrick J. Wong9c194642016-08-03 12:16:05 +1000530 if (error)
531 return error;
Darrick J. Wong722e2512016-08-03 12:28:43 +1000532 rudp = xfs_trans_get_rud(tp, ruip);
Darrick J. Wong9c194642016-08-03 12:16:05 +1000533
534 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
Darrick J. Wonge127faf2016-08-03 12:29:32 +1000535 rmap = &ruip->rui_format.rui_extents[i];
Darrick J. Wong9c194642016-08-03 12:16:05 +1000536 state = (rmap->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ?
537 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
538 whichfork = (rmap->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ?
539 XFS_ATTR_FORK : XFS_DATA_FORK;
540 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
541 case XFS_RMAP_EXTENT_MAP:
542 type = XFS_RMAP_MAP;
543 break;
Darrick J. Wongceeb9c82016-10-03 09:11:48 -0700544 case XFS_RMAP_EXTENT_MAP_SHARED:
545 type = XFS_RMAP_MAP_SHARED;
546 break;
Darrick J. Wong9c194642016-08-03 12:16:05 +1000547 case XFS_RMAP_EXTENT_UNMAP:
548 type = XFS_RMAP_UNMAP;
549 break;
Darrick J. Wongceeb9c82016-10-03 09:11:48 -0700550 case XFS_RMAP_EXTENT_UNMAP_SHARED:
551 type = XFS_RMAP_UNMAP_SHARED;
552 break;
Darrick J. Wong9c194642016-08-03 12:16:05 +1000553 case XFS_RMAP_EXTENT_CONVERT:
554 type = XFS_RMAP_CONVERT;
555 break;
Darrick J. Wong3f165b32016-10-03 09:11:48 -0700556 case XFS_RMAP_EXTENT_CONVERT_SHARED:
557 type = XFS_RMAP_CONVERT_SHARED;
558 break;
Darrick J. Wong9c194642016-08-03 12:16:05 +1000559 case XFS_RMAP_EXTENT_ALLOC:
560 type = XFS_RMAP_ALLOC;
561 break;
562 case XFS_RMAP_EXTENT_FREE:
563 type = XFS_RMAP_FREE;
564 break;
565 default:
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700566 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
Darrick J. Wong9c194642016-08-03 12:16:05 +1000567 error = -EFSCORRUPTED;
568 goto abort_error;
569 }
570 error = xfs_trans_log_finish_rmap_update(tp, rudp, type,
571 rmap->me_owner, whichfork,
572 rmap->me_startoff, rmap->me_startblock,
573 rmap->me_len, state, &rcur);
574 if (error)
575 goto abort_error;
576
577 }
578
579 xfs_rmap_finish_one_cleanup(tp, rcur, error);
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000580 set_bit(XFS_RUI_RECOVERED, &ruip->rui_flags);
Darrick J. Wong9c194642016-08-03 12:16:05 +1000581 error = xfs_trans_commit(tp);
582 return error;
583
584abort_error:
585 xfs_rmap_finish_one_cleanup(tp, rcur, error);
586 xfs_trans_cancel(tp);
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000587 return error;
588}
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700589
590const struct xlog_recover_item_ops xlog_rui_item_ops = {
591 .item_type = XFS_LI_RUI,
592};
593
594const struct xlog_recover_item_ops xlog_rud_item_ops = {
595 .item_type = XFS_LI_RUD,
596};