blob: 534504ede1a338cc2bcf59418c292970979c98bc [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. Wong07590a92020-05-01 16:00:49 -070021#include "xfs_log_priv.h"
Darrick J. Wong86ffa472020-05-01 16:00:45 -070022#include "xfs_log_recover.h"
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100023
Darrick J. Wong182696f2021-10-12 11:09:23 -070024struct kmem_cache *xfs_rui_cache;
25struct kmem_cache *xfs_rud_cache;
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100026
Darrick J. Wongcba0cca2020-05-01 16:00:51 -070027static const struct xfs_item_ops xfs_rui_item_ops;
28
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100029static inline struct xfs_rui_log_item *RUI_ITEM(struct xfs_log_item *lip)
30{
31 return container_of(lip, struct xfs_rui_log_item, rui_item);
32}
33
Darrick J. Wong07590a92020-05-01 16:00:49 -070034STATIC void
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100035xfs_rui_item_free(
36 struct xfs_rui_log_item *ruip)
37{
Dave Chinnerc230a4a2022-05-04 11:45:11 +100038 kmem_free(ruip->rui_item.li_lv_shadow);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100039 if (ruip->rui_format.rui_nextents > XFS_RUI_MAX_FAST_EXTENTS)
40 kmem_free(ruip);
41 else
Darrick J. Wong182696f2021-10-12 11:09:23 -070042 kmem_cache_free(xfs_rui_cache, ruip);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100043}
44
Dave Chinner0612d112018-04-02 20:08:27 -070045/*
46 * Freeing the RUI requires that we remove it from the AIL if it has already
47 * been placed there. However, the RUI may not yet have been placed in the AIL
48 * when called by xfs_rui_release() from RUD 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 RUI.
51 */
Darrick J. Wongcba0cca2020-05-01 16:00:51 -070052STATIC void
Dave Chinner0612d112018-04-02 20:08:27 -070053xfs_rui_release(
54 struct xfs_rui_log_item *ruip)
55{
56 ASSERT(atomic_read(&ruip->rui_refcount) > 0);
Dave Chinner3512fc12022-05-04 11:46:47 +100057 if (!atomic_dec_and_test(&ruip->rui_refcount))
58 return;
59
60 xfs_trans_ail_delete(&ruip->rui_item, 0);
61 xfs_rui_item_free(ruip);
Dave Chinner0612d112018-04-02 20:08:27 -070062}
63
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100064STATIC void
65xfs_rui_item_size(
66 struct xfs_log_item *lip,
67 int *nvecs,
68 int *nbytes)
69{
Darrick J. Wongcd001582016-09-19 10:24:27 +100070 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
71
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100072 *nvecs += 1;
Darrick J. Wongcd001582016-09-19 10:24:27 +100073 *nbytes += xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100074}
75
76/*
77 * This is called to fill in the vector of log iovecs for the
78 * given rui log item. We use only 1 iovec, and we point that
79 * at the rui_log_format structure embedded in the rui item.
80 * It is at this point that we assert that all of the extent
81 * slots in the rui item have been filled.
82 */
83STATIC void
84xfs_rui_item_format(
85 struct xfs_log_item *lip,
86 struct xfs_log_vec *lv)
87{
88 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
89 struct xfs_log_iovec *vecp = NULL;
90
91 ASSERT(atomic_read(&ruip->rui_next_extent) ==
92 ruip->rui_format.rui_nextents);
93
94 ruip->rui_format.rui_type = XFS_LI_RUI;
95 ruip->rui_format.rui_size = 1;
96
97 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUI_FORMAT, &ruip->rui_format,
Darrick J. Wongcd001582016-09-19 10:24:27 +100098 xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents));
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100099}
100
101/*
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000102 * The unpin operation is the last place an RUI is manipulated in the log. It is
103 * either inserted in the AIL or aborted in the event of a log I/O error. In
104 * either case, the RUI transaction has been successfully committed to make it
105 * this far. Therefore, we expect whoever committed the RUI to either construct
106 * and commit the RUD or drop the RUD's reference in the event of error. Simply
107 * drop the log's RUI reference now that the log is done with it.
108 */
109STATIC void
110xfs_rui_item_unpin(
111 struct xfs_log_item *lip,
112 int remove)
113{
114 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
115
116 xfs_rui_release(ruip);
117}
118
119/*
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000120 * The RUI has been either committed or aborted if the transaction has been
121 * cancelled. If the transaction was cancelled, an RUD isn't going to be
122 * constructed and thus we free the RUI here directly.
123 */
124STATIC void
Christoph Hellwigddf92052019-06-28 19:27:32 -0700125xfs_rui_item_release(
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000126 struct xfs_log_item *lip)
127{
Christoph Hellwigddf92052019-06-28 19:27:32 -0700128 xfs_rui_release(RUI_ITEM(lip));
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000129}
130
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000131/*
132 * Allocate and initialize an rui item with the given number of extents.
133 */
Darrick J. Wong07590a92020-05-01 16:00:49 -0700134STATIC struct xfs_rui_log_item *
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000135xfs_rui_init(
136 struct xfs_mount *mp,
137 uint nextents)
138
139{
140 struct xfs_rui_log_item *ruip;
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000141
142 ASSERT(nextents > 0);
Darrick J. Wongcd001582016-09-19 10:24:27 +1000143 if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700144 ruip = kmem_zalloc(xfs_rui_log_item_sizeof(nextents), 0);
Darrick J. Wongcd001582016-09-19 10:24:27 +1000145 else
Darrick J. Wong182696f2021-10-12 11:09:23 -0700146 ruip = kmem_cache_zalloc(xfs_rui_cache,
Carlos Maiolino32a2b112020-07-22 09:23:10 -0700147 GFP_KERNEL | __GFP_NOFAIL);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000148
149 xfs_log_item_init(mp, &ruip->rui_item, XFS_LI_RUI, &xfs_rui_item_ops);
150 ruip->rui_format.rui_nextents = nextents;
151 ruip->rui_format.rui_id = (uintptr_t)(void *)ruip;
152 atomic_set(&ruip->rui_next_extent, 0);
153 atomic_set(&ruip->rui_refcount, 2);
154
155 return ruip;
156}
157
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000158static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip)
159{
160 return container_of(lip, struct xfs_rud_log_item, rud_item);
161}
162
163STATIC void
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000164xfs_rud_item_size(
165 struct xfs_log_item *lip,
166 int *nvecs,
167 int *nbytes)
168{
169 *nvecs += 1;
Darrick J. Wong722e2512016-08-03 12:28:43 +1000170 *nbytes += sizeof(struct xfs_rud_log_format);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000171}
172
173/*
174 * This is called to fill in the vector of log iovecs for the
175 * given rud log item. We use only 1 iovec, and we point that
176 * at the rud_log_format structure embedded in the rud item.
177 * It is at this point that we assert that all of the extent
178 * slots in the rud item have been filled.
179 */
180STATIC void
181xfs_rud_item_format(
182 struct xfs_log_item *lip,
183 struct xfs_log_vec *lv)
184{
185 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
186 struct xfs_log_iovec *vecp = NULL;
187
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000188 rudp->rud_format.rud_type = XFS_LI_RUD;
189 rudp->rud_format.rud_size = 1;
190
191 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format,
Darrick J. Wong722e2512016-08-03 12:28:43 +1000192 sizeof(struct xfs_rud_log_format));
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000193}
194
195/*
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000196 * The RUD is either committed or aborted if the transaction is cancelled. If
197 * the transaction is cancelled, drop our reference to the RUI and free the
198 * RUD.
199 */
200STATIC void
Christoph Hellwigddf92052019-06-28 19:27:32 -0700201xfs_rud_item_release(
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000202 struct xfs_log_item *lip)
203{
204 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
205
Christoph Hellwigddf92052019-06-28 19:27:32 -0700206 xfs_rui_release(rudp->rud_ruip);
Dave Chinnerc230a4a2022-05-04 11:45:11 +1000207 kmem_free(rudp->rud_item.li_lv_shadow);
Darrick J. Wong182696f2021-10-12 11:09:23 -0700208 kmem_cache_free(xfs_rud_cache, rudp);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000209}
210
Dave Chinnerc23ab602022-05-04 11:46:39 +1000211static struct xfs_log_item *
212xfs_rud_item_intent(
213 struct xfs_log_item *lip)
214{
215 return &RUD_ITEM(lip)->rud_ruip->rui_item;
216}
217
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000218static const struct xfs_item_ops xfs_rud_item_ops = {
Dave Chinnerf5b81202022-05-04 11:46:09 +1000219 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED |
220 XFS_ITEM_INTENT_DONE,
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000221 .iop_size = xfs_rud_item_size,
222 .iop_format = xfs_rud_item_format,
Christoph Hellwigddf92052019-06-28 19:27:32 -0700223 .iop_release = xfs_rud_item_release,
Dave Chinnerc23ab602022-05-04 11:46:39 +1000224 .iop_intent = xfs_rud_item_intent,
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000225};
226
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700227static struct xfs_rud_log_item *
Christoph Hellwig608834472019-06-28 19:27:36 -0700228xfs_trans_get_rud(
229 struct xfs_trans *tp,
Darrick J. Wong722e2512016-08-03 12:28:43 +1000230 struct xfs_rui_log_item *ruip)
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000231{
Christoph Hellwig608834472019-06-28 19:27:36 -0700232 struct xfs_rud_log_item *rudp;
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000233
Darrick J. Wong182696f2021-10-12 11:09:23 -0700234 rudp = kmem_cache_zalloc(xfs_rud_cache, GFP_KERNEL | __GFP_NOFAIL);
Christoph Hellwig608834472019-06-28 19:27:36 -0700235 xfs_log_item_init(tp->t_mountp, &rudp->rud_item, XFS_LI_RUD,
236 &xfs_rud_item_ops);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000237 rudp->rud_ruip = ruip;
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000238 rudp->rud_format.rud_rui_id = ruip->rui_format.rui_id;
239
Christoph Hellwig608834472019-06-28 19:27:36 -0700240 xfs_trans_add_item(tp, &rudp->rud_item);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000241 return rudp;
242}
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000243
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700244/* Set the map extent flags for this reverse mapping. */
245static void
246xfs_trans_set_rmap_flags(
247 struct xfs_map_extent *rmap,
248 enum xfs_rmap_intent_type type,
249 int whichfork,
250 xfs_exntst_t state)
251{
252 rmap->me_flags = 0;
253 if (state == XFS_EXT_UNWRITTEN)
254 rmap->me_flags |= XFS_RMAP_EXTENT_UNWRITTEN;
255 if (whichfork == XFS_ATTR_FORK)
256 rmap->me_flags |= XFS_RMAP_EXTENT_ATTR_FORK;
257 switch (type) {
258 case XFS_RMAP_MAP:
259 rmap->me_flags |= XFS_RMAP_EXTENT_MAP;
260 break;
261 case XFS_RMAP_MAP_SHARED:
262 rmap->me_flags |= XFS_RMAP_EXTENT_MAP_SHARED;
263 break;
264 case XFS_RMAP_UNMAP:
265 rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP;
266 break;
267 case XFS_RMAP_UNMAP_SHARED:
268 rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP_SHARED;
269 break;
270 case XFS_RMAP_CONVERT:
271 rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT;
272 break;
273 case XFS_RMAP_CONVERT_SHARED:
274 rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT_SHARED;
275 break;
276 case XFS_RMAP_ALLOC:
277 rmap->me_flags |= XFS_RMAP_EXTENT_ALLOC;
278 break;
279 case XFS_RMAP_FREE:
280 rmap->me_flags |= XFS_RMAP_EXTENT_FREE;
281 break;
282 default:
283 ASSERT(0);
284 }
285}
286
287/*
288 * Finish an rmap update and log it to the RUD. Note that the transaction is
289 * marked dirty regardless of whether the rmap update succeeds or fails to
290 * support the RUI/RUD lifecycle rules.
291 */
292static int
293xfs_trans_log_finish_rmap_update(
294 struct xfs_trans *tp,
295 struct xfs_rud_log_item *rudp,
296 enum xfs_rmap_intent_type type,
297 uint64_t owner,
298 int whichfork,
299 xfs_fileoff_t startoff,
300 xfs_fsblock_t startblock,
301 xfs_filblks_t blockcount,
302 xfs_exntst_t state,
303 struct xfs_btree_cur **pcur)
304{
305 int error;
306
307 error = xfs_rmap_finish_one(tp, type, owner, whichfork, startoff,
308 startblock, blockcount, state, pcur);
309
310 /*
311 * Mark the transaction dirty, even on error. This ensures the
312 * transaction is aborted, which:
313 *
314 * 1.) releases the RUI and frees the RUD
315 * 2.) shuts down the filesystem
316 */
Dave Chinnerbb7b1c92022-05-04 11:46:21 +1000317 tp->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE;
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700318 set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags);
319
320 return error;
321}
322
323/* Sort rmap intents by AG. */
324static int
325xfs_rmap_update_diff_items(
326 void *priv,
Sami Tolvanen4f0f5862021-04-08 11:28:34 -0700327 const struct list_head *a,
328 const struct list_head *b)
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700329{
330 struct xfs_mount *mp = priv;
331 struct xfs_rmap_intent *ra;
332 struct xfs_rmap_intent *rb;
333
334 ra = container_of(a, struct xfs_rmap_intent, ri_list);
335 rb = container_of(b, struct xfs_rmap_intent, ri_list);
336 return XFS_FSB_TO_AGNO(mp, ra->ri_bmap.br_startblock) -
337 XFS_FSB_TO_AGNO(mp, rb->ri_bmap.br_startblock);
338}
339
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700340/* Log rmap updates in the intent item. */
341STATIC void
342xfs_rmap_update_log_item(
343 struct xfs_trans *tp,
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700344 struct xfs_rui_log_item *ruip,
345 struct xfs_rmap_intent *rmap)
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700346{
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700347 uint next_extent;
348 struct xfs_map_extent *map;
349
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700350 tp->t_flags |= XFS_TRANS_DIRTY;
351 set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags);
352
353 /*
354 * atomic_inc_return gives us the value after the increment;
355 * we want to use it as an array index so we need to subtract 1 from
356 * it.
357 */
358 next_extent = atomic_inc_return(&ruip->rui_next_extent) - 1;
359 ASSERT(next_extent < ruip->rui_format.rui_nextents);
360 map = &ruip->rui_format.rui_extents[next_extent];
361 map->me_owner = rmap->ri_owner;
362 map->me_startblock = rmap->ri_bmap.br_startblock;
363 map->me_startoff = rmap->ri_bmap.br_startoff;
364 map->me_len = rmap->ri_bmap.br_blockcount;
365 xfs_trans_set_rmap_flags(map, rmap->ri_type, rmap->ri_whichfork,
366 rmap->ri_bmap.br_state);
367}
368
Christoph Hellwig13a83332020-04-30 12:52:21 -0700369static struct xfs_log_item *
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700370xfs_rmap_update_create_intent(
371 struct xfs_trans *tp,
372 struct list_head *items,
Christoph Hellwigd367a862020-04-30 12:52:20 -0700373 unsigned int count,
374 bool sort)
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700375{
376 struct xfs_mount *mp = tp->t_mountp;
377 struct xfs_rui_log_item *ruip = xfs_rui_init(mp, count);
378 struct xfs_rmap_intent *rmap;
379
380 ASSERT(count > 0);
381
382 xfs_trans_add_item(tp, &ruip->rui_item);
Christoph Hellwigd367a862020-04-30 12:52:20 -0700383 if (sort)
384 list_sort(mp, items, xfs_rmap_update_diff_items);
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700385 list_for_each_entry(rmap, items, ri_list)
386 xfs_rmap_update_log_item(tp, ruip, rmap);
Christoph Hellwig13a83332020-04-30 12:52:21 -0700387 return &ruip->rui_item;
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700388}
389
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700390/* Get an RUD so we can process all the deferred rmap updates. */
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700391static struct xfs_log_item *
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700392xfs_rmap_update_create_done(
393 struct xfs_trans *tp,
Christoph Hellwig13a83332020-04-30 12:52:21 -0700394 struct xfs_log_item *intent,
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700395 unsigned int count)
396{
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700397 return &xfs_trans_get_rud(tp, RUI_ITEM(intent))->rud_item;
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700398}
399
400/* Process a deferred rmap update. */
401STATIC int
402xfs_rmap_update_finish_item(
403 struct xfs_trans *tp,
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700404 struct xfs_log_item *done,
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700405 struct list_head *item,
Christoph Hellwig3ec1b262020-04-30 12:52:22 -0700406 struct xfs_btree_cur **state)
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700407{
408 struct xfs_rmap_intent *rmap;
409 int error;
410
411 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700412 error = xfs_trans_log_finish_rmap_update(tp, RUD_ITEM(done),
Christoph Hellwig3ec1b262020-04-30 12:52:22 -0700413 rmap->ri_type, rmap->ri_owner, rmap->ri_whichfork,
414 rmap->ri_bmap.br_startoff, rmap->ri_bmap.br_startblock,
415 rmap->ri_bmap.br_blockcount, rmap->ri_bmap.br_state,
416 state);
Darrick J. Wongf3c799c2021-10-12 14:11:01 -0700417 kmem_cache_free(xfs_rmap_intent_cache, rmap);
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700418 return error;
419}
420
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700421/* Abort all pending RUIs. */
422STATIC void
423xfs_rmap_update_abort_intent(
Christoph Hellwig13a83332020-04-30 12:52:21 -0700424 struct xfs_log_item *intent)
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700425{
Christoph Hellwig13a83332020-04-30 12:52:21 -0700426 xfs_rui_release(RUI_ITEM(intent));
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700427}
428
429/* Cancel a deferred rmap update. */
430STATIC void
431xfs_rmap_update_cancel_item(
432 struct list_head *item)
433{
434 struct xfs_rmap_intent *rmap;
435
436 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
Darrick J. Wongf3c799c2021-10-12 14:11:01 -0700437 kmem_cache_free(xfs_rmap_intent_cache, rmap);
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700438}
439
440const struct xfs_defer_op_type xfs_rmap_update_defer_type = {
441 .max_items = XFS_RUI_MAX_FAST_EXTENTS,
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700442 .create_intent = xfs_rmap_update_create_intent,
443 .abort_intent = xfs_rmap_update_abort_intent,
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700444 .create_done = xfs_rmap_update_create_done,
445 .finish_item = xfs_rmap_update_finish_item,
Christoph Hellwig3ec1b262020-04-30 12:52:22 -0700446 .finish_cleanup = xfs_rmap_finish_one_cleanup,
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700447 .cancel_item = xfs_rmap_update_cancel_item,
448};
449
Darrick J. Wongdda7ba62020-11-29 16:33:36 -0800450/* Is this recovered RUI ok? */
451static inline bool
452xfs_rui_validate_map(
453 struct xfs_mount *mp,
454 struct xfs_map_extent *rmap)
455{
Dave Chinner38c26bf2021-08-18 18:46:37 -0700456 if (!xfs_has_rmapbt(mp))
Darrick J. Wongda5de112020-11-29 16:33:39 -0800457 return false;
458
Darrick J. Wongc447ad62020-11-29 16:33:37 -0800459 if (rmap->me_flags & ~XFS_RMAP_EXTENT_FLAGS)
460 return false;
Darrick J. Wongdda7ba62020-11-29 16:33:36 -0800461
Darrick J. Wongdda7ba62020-11-29 16:33:36 -0800462 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
463 case XFS_RMAP_EXTENT_MAP:
464 case XFS_RMAP_EXTENT_MAP_SHARED:
465 case XFS_RMAP_EXTENT_UNMAP:
466 case XFS_RMAP_EXTENT_UNMAP_SHARED:
467 case XFS_RMAP_EXTENT_CONVERT:
468 case XFS_RMAP_EXTENT_CONVERT_SHARED:
469 case XFS_RMAP_EXTENT_ALLOC:
470 case XFS_RMAP_EXTENT_FREE:
Darrick J. Wongdda7ba62020-11-29 16:33:36 -0800471 break;
472 default:
Darrick J. Wongc447ad62020-11-29 16:33:37 -0800473 return false;
Darrick J. Wongdda7ba62020-11-29 16:33:36 -0800474 }
Darrick J. Wongc447ad62020-11-29 16:33:37 -0800475
476 if (!XFS_RMAP_NON_INODE_OWNER(rmap->me_owner) &&
477 !xfs_verify_ino(mp, rmap->me_owner))
478 return false;
479
Darrick J. Wong33005fd2020-12-04 13:28:35 -0800480 if (!xfs_verify_fileext(mp, rmap->me_startoff, rmap->me_len))
Darrick J. Wongc447ad62020-11-29 16:33:37 -0800481 return false;
482
Darrick J. Wong67457eb2020-12-04 13:20:00 -0800483 return xfs_verify_fsbext(mp, rmap->me_startblock, rmap->me_len);
Darrick J. Wongdda7ba62020-11-29 16:33:36 -0800484}
485
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000486/*
487 * Process an rmap update intent item that was recovered from the log.
488 * We need to update the rmapbt.
489 */
Darrick J. Wongcba0cca2020-05-01 16:00:51 -0700490STATIC int
Darrick J. Wong96b60f82020-05-01 16:00:55 -0700491xfs_rui_item_recover(
492 struct xfs_log_item *lip,
Darrick J. Wonge6fff812020-09-25 17:39:37 -0700493 struct list_head *capture_list)
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000494{
Darrick J. Wong96b60f82020-05-01 16:00:55 -0700495 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000496 struct xfs_map_extent *rmap;
Darrick J. Wong9c194642016-08-03 12:16:05 +1000497 struct xfs_rud_log_item *rudp;
Darrick J. Wong9c194642016-08-03 12:16:05 +1000498 struct xfs_trans *tp;
499 struct xfs_btree_cur *rcur = NULL;
Dave Chinnerd86142d2022-03-17 09:09:12 -0700500 struct xfs_mount *mp = lip->li_log->l_mp;
Darrick J. Wong96b60f82020-05-01 16:00:55 -0700501 enum xfs_rmap_intent_type type;
502 xfs_exntst_t state;
Darrick J. Wong96b60f82020-05-01 16:00:55 -0700503 int i;
504 int whichfork;
505 int error = 0;
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000506
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000507 /*
508 * First check the validity of the extents described by the
509 * RUI. If any are bad, then assume that all are bad and
510 * just toss the RUI.
511 */
512 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
Darrick J. Wongdda7ba62020-11-29 16:33:36 -0800513 if (!xfs_rui_validate_map(mp,
514 &ruip->rui_format.rui_extents[i])) {
515 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
516 &ruip->rui_format,
517 sizeof(ruip->rui_format));
Darrick J. Wong895e1962019-11-06 09:17:43 -0800518 return -EFSCORRUPTED;
Darrick J. Wongdda7ba62020-11-29 16:33:36 -0800519 }
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000520 }
521
Darrick J. Wongb31c2bd2018-02-22 14:41:25 -0800522 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
523 mp->m_rmap_maxlevels, 0, XFS_TRANS_RESERVE, &tp);
Darrick J. Wong9c194642016-08-03 12:16:05 +1000524 if (error)
525 return error;
Darrick J. Wong722e2512016-08-03 12:28:43 +1000526 rudp = xfs_trans_get_rud(tp, ruip);
Darrick J. Wong9c194642016-08-03 12:16:05 +1000527
528 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
Darrick J. Wonge127faf2016-08-03 12:29:32 +1000529 rmap = &ruip->rui_format.rui_extents[i];
Darrick J. Wong9c194642016-08-03 12:16:05 +1000530 state = (rmap->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ?
531 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
532 whichfork = (rmap->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ?
533 XFS_ATTR_FORK : XFS_DATA_FORK;
534 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
535 case XFS_RMAP_EXTENT_MAP:
536 type = XFS_RMAP_MAP;
537 break;
Darrick J. Wongceeb9c82016-10-03 09:11:48 -0700538 case XFS_RMAP_EXTENT_MAP_SHARED:
539 type = XFS_RMAP_MAP_SHARED;
540 break;
Darrick J. Wong9c194642016-08-03 12:16:05 +1000541 case XFS_RMAP_EXTENT_UNMAP:
542 type = XFS_RMAP_UNMAP;
543 break;
Darrick J. Wongceeb9c82016-10-03 09:11:48 -0700544 case XFS_RMAP_EXTENT_UNMAP_SHARED:
545 type = XFS_RMAP_UNMAP_SHARED;
546 break;
Darrick J. Wong9c194642016-08-03 12:16:05 +1000547 case XFS_RMAP_EXTENT_CONVERT:
548 type = XFS_RMAP_CONVERT;
549 break;
Darrick J. Wong3f165b32016-10-03 09:11:48 -0700550 case XFS_RMAP_EXTENT_CONVERT_SHARED:
551 type = XFS_RMAP_CONVERT_SHARED;
552 break;
Darrick J. Wong9c194642016-08-03 12:16:05 +1000553 case XFS_RMAP_EXTENT_ALLOC:
554 type = XFS_RMAP_ALLOC;
555 break;
556 case XFS_RMAP_EXTENT_FREE:
557 type = XFS_RMAP_FREE;
558 break;
559 default:
Darrick J. Wong950f0d52022-10-25 15:07:14 -0700560 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
561 &ruip->rui_format,
562 sizeof(ruip->rui_format));
Darrick J. Wong9c194642016-08-03 12:16:05 +1000563 error = -EFSCORRUPTED;
564 goto abort_error;
565 }
566 error = xfs_trans_log_finish_rmap_update(tp, rudp, type,
567 rmap->me_owner, whichfork,
568 rmap->me_startoff, rmap->me_startblock,
569 rmap->me_len, state, &rcur);
Darrick J. Wong43059d52021-08-06 11:06:35 -0700570 if (error == -EFSCORRUPTED)
571 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
572 rmap, sizeof(*rmap));
Darrick J. Wong9c194642016-08-03 12:16:05 +1000573 if (error)
574 goto abort_error;
575
576 }
577
578 xfs_rmap_finish_one_cleanup(tp, rcur, error);
Darrick J. Wong512edfa2021-09-16 17:28:07 -0700579 return xfs_defer_ops_capture_and_commit(tp, capture_list);
Darrick J. Wong9c194642016-08-03 12:16:05 +1000580
581abort_error:
582 xfs_rmap_finish_one_cleanup(tp, rcur, error);
583 xfs_trans_cancel(tp);
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000584 return error;
585}
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700586
Darrick J. Wong154c7332020-05-01 16:00:54 -0700587STATIC bool
588xfs_rui_item_match(
589 struct xfs_log_item *lip,
590 uint64_t intent_id)
591{
592 return RUI_ITEM(lip)->rui_format.rui_id == intent_id;
593}
594
Darrick J. Wong4e919af2020-09-27 16:18:13 -0700595/* Relog an intent item to push the log tail forward. */
596static struct xfs_log_item *
597xfs_rui_item_relog(
598 struct xfs_log_item *intent,
599 struct xfs_trans *tp)
600{
601 struct xfs_rud_log_item *rudp;
602 struct xfs_rui_log_item *ruip;
603 struct xfs_map_extent *extp;
604 unsigned int count;
605
606 count = RUI_ITEM(intent)->rui_format.rui_nextents;
607 extp = RUI_ITEM(intent)->rui_format.rui_extents;
608
609 tp->t_flags |= XFS_TRANS_DIRTY;
610 rudp = xfs_trans_get_rud(tp, RUI_ITEM(intent));
611 set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags);
612
613 ruip = xfs_rui_init(tp->t_mountp, count);
614 memcpy(ruip->rui_format.rui_extents, extp, count * sizeof(*extp));
615 atomic_set(&ruip->rui_next_extent, count);
616 xfs_trans_add_item(tp, &ruip->rui_item);
617 set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags);
618 return &ruip->rui_item;
619}
620
Darrick J. Wongcba0cca2020-05-01 16:00:51 -0700621static const struct xfs_item_ops xfs_rui_item_ops = {
Dave Chinnerf5b81202022-05-04 11:46:09 +1000622 .flags = XFS_ITEM_INTENT,
Darrick J. Wongcba0cca2020-05-01 16:00:51 -0700623 .iop_size = xfs_rui_item_size,
624 .iop_format = xfs_rui_item_format,
625 .iop_unpin = xfs_rui_item_unpin,
626 .iop_release = xfs_rui_item_release,
627 .iop_recover = xfs_rui_item_recover,
Darrick J. Wong154c7332020-05-01 16:00:54 -0700628 .iop_match = xfs_rui_item_match,
Darrick J. Wong4e919af2020-09-27 16:18:13 -0700629 .iop_relog = xfs_rui_item_relog,
Darrick J. Wongcba0cca2020-05-01 16:00:51 -0700630};
631
Darrick J. Wongb45ca962022-10-20 16:26:36 -0700632static inline void
633xfs_rui_copy_format(
634 struct xfs_rui_log_format *dst,
635 const struct xfs_rui_log_format *src)
636{
637 unsigned int i;
638
639 memcpy(dst, src, offsetof(struct xfs_rui_log_format, rui_extents));
640
641 for (i = 0; i < src->rui_nextents; i++)
642 memcpy(&dst->rui_extents[i], &src->rui_extents[i],
643 sizeof(struct xfs_map_extent));
644}
645
Darrick J. Wong07590a92020-05-01 16:00:49 -0700646/*
647 * This routine is called to create an in-core extent rmap update
648 * item from the rui format structure which was logged on disk.
649 * It allocates an in-core rui, copies the extents from the format
650 * structure into it, and adds the rui to the AIL with the given
651 * LSN.
652 */
653STATIC int
654xlog_recover_rui_commit_pass2(
655 struct xlog *log,
656 struct list_head *buffer_list,
657 struct xlog_recover_item *item,
658 xfs_lsn_t lsn)
659{
Darrick J. Wong07590a92020-05-01 16:00:49 -0700660 struct xfs_mount *mp = log->l_mp;
661 struct xfs_rui_log_item *ruip;
662 struct xfs_rui_log_format *rui_formatp;
Darrick J. Wongb45ca962022-10-20 16:26:36 -0700663 size_t len;
Darrick J. Wong07590a92020-05-01 16:00:49 -0700664
665 rui_formatp = item->ri_buf[0].i_addr;
666
Darrick J. Wongb45ca962022-10-20 16:26:36 -0700667 if (item->ri_buf[0].i_len < xfs_rui_log_format_sizeof(0)) {
Darrick J. Wong950f0d52022-10-25 15:07:14 -0700668 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
669 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
Darrick J. Wongb45ca962022-10-20 16:26:36 -0700670 return -EFSCORRUPTED;
Darrick J. Wong07590a92020-05-01 16:00:49 -0700671 }
Darrick J. Wongb45ca962022-10-20 16:26:36 -0700672
673 len = xfs_rui_log_format_sizeof(rui_formatp->rui_nextents);
674 if (item->ri_buf[0].i_len != len) {
Darrick J. Wong950f0d52022-10-25 15:07:14 -0700675 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
676 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
Darrick J. Wongb45ca962022-10-20 16:26:36 -0700677 return -EFSCORRUPTED;
678 }
679
680 ruip = xfs_rui_init(mp, rui_formatp->rui_nextents);
681 xfs_rui_copy_format(&ruip->rui_format, rui_formatp);
Darrick J. Wong07590a92020-05-01 16:00:49 -0700682 atomic_set(&ruip->rui_next_extent, rui_formatp->rui_nextents);
Darrick J. Wong07590a92020-05-01 16:00:49 -0700683 /*
Darrick J. Wong86a37172020-05-01 16:00:54 -0700684 * Insert the intent into the AIL directly and drop one reference so
685 * that finishing or canceling the work will drop the other.
Darrick J. Wong07590a92020-05-01 16:00:49 -0700686 */
Darrick J. Wong86a37172020-05-01 16:00:54 -0700687 xfs_trans_ail_insert(log->l_ailp, &ruip->rui_item, lsn);
Darrick J. Wong07590a92020-05-01 16:00:49 -0700688 xfs_rui_release(ruip);
689 return 0;
690}
691
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700692const struct xlog_recover_item_ops xlog_rui_item_ops = {
693 .item_type = XFS_LI_RUI,
Darrick J. Wong07590a92020-05-01 16:00:49 -0700694 .commit_pass2 = xlog_recover_rui_commit_pass2,
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700695};
696
Darrick J. Wong07590a92020-05-01 16:00:49 -0700697/*
698 * This routine is called when an RUD format structure is found in a committed
699 * transaction in the log. Its purpose is to cancel the corresponding RUI if it
700 * was still in the log. To do this it searches the AIL for the RUI with an id
701 * equal to that in the RUD format structure. If we find it we drop the RUD
702 * reference, which removes the RUI from the AIL and frees it.
703 */
704STATIC int
705xlog_recover_rud_commit_pass2(
706 struct xlog *log,
707 struct list_head *buffer_list,
708 struct xlog_recover_item *item,
709 xfs_lsn_t lsn)
710{
711 struct xfs_rud_log_format *rud_formatp;
Darrick J. Wong07590a92020-05-01 16:00:49 -0700712
713 rud_formatp = item->ri_buf[0].i_addr;
Darrick J. Wong921ed962022-10-25 15:14:06 -0700714 if (item->ri_buf[0].i_len != sizeof(struct xfs_rud_log_format)) {
715 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
716 rud_formatp, item->ri_buf[0].i_len);
717 return -EFSCORRUPTED;
718 }
Darrick J. Wong07590a92020-05-01 16:00:49 -0700719
Darrick J. Wong154c7332020-05-01 16:00:54 -0700720 xlog_recover_release_intent(log, XFS_LI_RUI, rud_formatp->rud_rui_id);
Darrick J. Wong07590a92020-05-01 16:00:49 -0700721 return 0;
722}
723
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700724const struct xlog_recover_item_ops xlog_rud_item_ops = {
725 .item_type = XFS_LI_RUD,
Darrick J. Wong07590a92020-05-01 16:00:49 -0700726 .commit_pass2 = xlog_recover_rud_commit_pass2,
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700727};