Merge tag 'timers_urgent_for_v6.4_rc2' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / fs / xfs / xfs_rmap_item.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0+
5880f2d7
DW
2/*
3 * Copyright (C) 2016 Oracle. All Rights Reserved.
5880f2d7 4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5880f2d7
DW
5 */
6#include "xfs.h"
7#include "xfs_fs.h"
8#include "xfs_format.h"
9#include "xfs_log_format.h"
10#include "xfs_trans_resv.h"
9e88b5d8 11#include "xfs_bit.h"
b31c2bdc 12#include "xfs_shared.h"
5880f2d7 13#include "xfs_mount.h"
9c194644 14#include "xfs_defer.h"
5880f2d7
DW
15#include "xfs_trans.h"
16#include "xfs_trans_priv.h"
5880f2d7
DW
17#include "xfs_rmap_item.h"
18#include "xfs_log.h"
9c194644 19#include "xfs_rmap.h"
a5155b87 20#include "xfs_error.h"
07590a9d 21#include "xfs_log_priv.h"
86ffa471 22#include "xfs_log_recover.h"
c13418e8 23#include "xfs_ag.h"
5880f2d7 24
182696fb
DW
25struct kmem_cache *xfs_rui_cache;
26struct kmem_cache *xfs_rud_cache;
5880f2d7 27
cba0ccac
DW
28static const struct xfs_item_ops xfs_rui_item_ops;
29
5880f2d7
DW
30static inline struct xfs_rui_log_item *RUI_ITEM(struct xfs_log_item *lip)
31{
32 return container_of(lip, struct xfs_rui_log_item, rui_item);
33}
34
07590a9d 35STATIC void
5880f2d7
DW
36xfs_rui_item_free(
37 struct xfs_rui_log_item *ruip)
38{
c230a4a8 39 kmem_free(ruip->rui_item.li_lv_shadow);
5880f2d7
DW
40 if (ruip->rui_format.rui_nextents > XFS_RUI_MAX_FAST_EXTENTS)
41 kmem_free(ruip);
42 else
182696fb 43 kmem_cache_free(xfs_rui_cache, ruip);
5880f2d7
DW
44}
45
0612d116
DC
46/*
47 * Freeing the RUI requires that we remove it from the AIL if it has already
48 * been placed there. However, the RUI may not yet have been placed in the AIL
49 * when called by xfs_rui_release() from RUD processing due to the ordering of
50 * committed vs unpin operations in bulk insert operations. Hence the reference
51 * count to ensure only the last caller frees the RUI.
52 */
cba0ccac 53STATIC void
0612d116
DC
54xfs_rui_release(
55 struct xfs_rui_log_item *ruip)
56{
57 ASSERT(atomic_read(&ruip->rui_refcount) > 0);
3512fc1e
DC
58 if (!atomic_dec_and_test(&ruip->rui_refcount))
59 return;
60
61 xfs_trans_ail_delete(&ruip->rui_item, 0);
62 xfs_rui_item_free(ruip);
0612d116
DC
63}
64
5880f2d7
DW
65STATIC void
66xfs_rui_item_size(
67 struct xfs_log_item *lip,
68 int *nvecs,
69 int *nbytes)
70{
cd00158c
DW
71 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
72
5880f2d7 73 *nvecs += 1;
cd00158c 74 *nbytes += xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents);
5880f2d7
DW
75}
76
77/*
78 * This is called to fill in the vector of log iovecs for the
79 * given rui log item. We use only 1 iovec, and we point that
80 * at the rui_log_format structure embedded in the rui item.
81 * It is at this point that we assert that all of the extent
82 * slots in the rui item have been filled.
83 */
84STATIC void
85xfs_rui_item_format(
86 struct xfs_log_item *lip,
87 struct xfs_log_vec *lv)
88{
89 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
90 struct xfs_log_iovec *vecp = NULL;
91
92 ASSERT(atomic_read(&ruip->rui_next_extent) ==
93 ruip->rui_format.rui_nextents);
94
95 ruip->rui_format.rui_type = XFS_LI_RUI;
96 ruip->rui_format.rui_size = 1;
97
98 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUI_FORMAT, &ruip->rui_format,
cd00158c 99 xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents));
5880f2d7
DW
100}
101
5880f2d7
DW
102/*
103 * The unpin operation is the last place an RUI is manipulated in the log. It is
104 * either inserted in the AIL or aborted in the event of a log I/O error. In
105 * either case, the RUI transaction has been successfully committed to make it
106 * this far. Therefore, we expect whoever committed the RUI to either construct
107 * and commit the RUD or drop the RUD's reference in the event of error. Simply
108 * drop the log's RUI reference now that the log is done with it.
109 */
110STATIC void
111xfs_rui_item_unpin(
112 struct xfs_log_item *lip,
113 int remove)
114{
115 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
116
117 xfs_rui_release(ruip);
118}
119
5880f2d7
DW
120/*
121 * The RUI has been either committed or aborted if the transaction has been
122 * cancelled. If the transaction was cancelled, an RUD isn't going to be
123 * constructed and thus we free the RUI here directly.
124 */
125STATIC void
ddf92053 126xfs_rui_item_release(
5880f2d7
DW
127 struct xfs_log_item *lip)
128{
ddf92053 129 xfs_rui_release(RUI_ITEM(lip));
5880f2d7
DW
130}
131
5880f2d7
DW
132/*
133 * Allocate and initialize an rui item with the given number of extents.
134 */
07590a9d 135STATIC struct xfs_rui_log_item *
5880f2d7
DW
136xfs_rui_init(
137 struct xfs_mount *mp,
138 uint nextents)
139
140{
141 struct xfs_rui_log_item *ruip;
5880f2d7
DW
142
143 ASSERT(nextents > 0);
cd00158c 144 if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
707e0dda 145 ruip = kmem_zalloc(xfs_rui_log_item_sizeof(nextents), 0);
cd00158c 146 else
182696fb 147 ruip = kmem_cache_zalloc(xfs_rui_cache,
32a2b11f 148 GFP_KERNEL | __GFP_NOFAIL);
5880f2d7
DW
149
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
5880f2d7
DW
159static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip)
160{
161 return container_of(lip, struct xfs_rud_log_item, rud_item);
162}
163
5880f2d7
DW
164STATIC void
165xfs_rud_item_size(
166 struct xfs_log_item *lip,
167 int *nvecs,
168 int *nbytes)
169{
170 *nvecs += 1;
722e2517 171 *nbytes += sizeof(struct xfs_rud_log_format);
5880f2d7
DW
172}
173
174/*
175 * This is called to fill in the vector of log iovecs for the
176 * given rud log item. We use only 1 iovec, and we point that
177 * at the rud_log_format structure embedded in the rud item.
178 * It is at this point that we assert that all of the extent
179 * slots in the rud item have been filled.
180 */
181STATIC void
182xfs_rud_item_format(
183 struct xfs_log_item *lip,
184 struct xfs_log_vec *lv)
185{
186 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
187 struct xfs_log_iovec *vecp = NULL;
188
5880f2d7
DW
189 rudp->rud_format.rud_type = XFS_LI_RUD;
190 rudp->rud_format.rud_size = 1;
191
192 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format,
722e2517 193 sizeof(struct xfs_rud_log_format));
5880f2d7
DW
194}
195
5880f2d7
DW
196/*
197 * The RUD is either committed or aborted if the transaction is cancelled. If
198 * the transaction is cancelled, drop our reference to the RUI and free the
199 * RUD.
200 */
201STATIC void
ddf92053 202xfs_rud_item_release(
5880f2d7
DW
203 struct xfs_log_item *lip)
204{
205 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
206
ddf92053 207 xfs_rui_release(rudp->rud_ruip);
c230a4a8 208 kmem_free(rudp->rud_item.li_lv_shadow);
182696fb 209 kmem_cache_free(xfs_rud_cache, rudp);
5880f2d7
DW
210}
211
c23ab603
DC
212static struct xfs_log_item *
213xfs_rud_item_intent(
214 struct xfs_log_item *lip)
215{
216 return &RUD_ITEM(lip)->rud_ruip->rui_item;
217}
218
5880f2d7 219static const struct xfs_item_ops xfs_rud_item_ops = {
f5b81200
DC
220 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED |
221 XFS_ITEM_INTENT_DONE,
5880f2d7
DW
222 .iop_size = xfs_rud_item_size,
223 .iop_format = xfs_rud_item_format,
ddf92053 224 .iop_release = xfs_rud_item_release,
c23ab603 225 .iop_intent = xfs_rud_item_intent,
5880f2d7
DW
226};
227
3cfce1e3 228static struct xfs_rud_log_item *
60883447
CH
229xfs_trans_get_rud(
230 struct xfs_trans *tp,
722e2517 231 struct xfs_rui_log_item *ruip)
5880f2d7 232{
60883447 233 struct xfs_rud_log_item *rudp;
5880f2d7 234
182696fb 235 rudp = kmem_cache_zalloc(xfs_rud_cache, GFP_KERNEL | __GFP_NOFAIL);
60883447
CH
236 xfs_log_item_init(tp->t_mountp, &rudp->rud_item, XFS_LI_RUD,
237 &xfs_rud_item_ops);
5880f2d7 238 rudp->rud_ruip = ruip;
5880f2d7
DW
239 rudp->rud_format.rud_rui_id = ruip->rui_format.rui_id;
240
60883447 241 xfs_trans_add_item(tp, &rudp->rud_item);
5880f2d7
DW
242 return rudp;
243}
9e88b5d8 244
3cfce1e3
CH
245/* Set the map extent flags for this reverse mapping. */
246static void
247xfs_trans_set_rmap_flags(
ffaa196f 248 struct xfs_map_extent *map,
3cfce1e3
CH
249 enum xfs_rmap_intent_type type,
250 int whichfork,
251 xfs_exntst_t state)
252{
ffaa196f 253 map->me_flags = 0;
3cfce1e3 254 if (state == XFS_EXT_UNWRITTEN)
ffaa196f 255 map->me_flags |= XFS_RMAP_EXTENT_UNWRITTEN;
3cfce1e3 256 if (whichfork == XFS_ATTR_FORK)
ffaa196f 257 map->me_flags |= XFS_RMAP_EXTENT_ATTR_FORK;
3cfce1e3
CH
258 switch (type) {
259 case XFS_RMAP_MAP:
ffaa196f 260 map->me_flags |= XFS_RMAP_EXTENT_MAP;
3cfce1e3
CH
261 break;
262 case XFS_RMAP_MAP_SHARED:
ffaa196f 263 map->me_flags |= XFS_RMAP_EXTENT_MAP_SHARED;
3cfce1e3
CH
264 break;
265 case XFS_RMAP_UNMAP:
ffaa196f 266 map->me_flags |= XFS_RMAP_EXTENT_UNMAP;
3cfce1e3
CH
267 break;
268 case XFS_RMAP_UNMAP_SHARED:
ffaa196f 269 map->me_flags |= XFS_RMAP_EXTENT_UNMAP_SHARED;
3cfce1e3
CH
270 break;
271 case XFS_RMAP_CONVERT:
ffaa196f 272 map->me_flags |= XFS_RMAP_EXTENT_CONVERT;
3cfce1e3
CH
273 break;
274 case XFS_RMAP_CONVERT_SHARED:
ffaa196f 275 map->me_flags |= XFS_RMAP_EXTENT_CONVERT_SHARED;
3cfce1e3
CH
276 break;
277 case XFS_RMAP_ALLOC:
ffaa196f 278 map->me_flags |= XFS_RMAP_EXTENT_ALLOC;
3cfce1e3
CH
279 break;
280 case XFS_RMAP_FREE:
ffaa196f 281 map->me_flags |= XFS_RMAP_EXTENT_FREE;
3cfce1e3
CH
282 break;
283 default:
284 ASSERT(0);
285 }
286}
287
288/*
289 * Finish an rmap update and log it to the RUD. Note that the transaction is
290 * marked dirty regardless of whether the rmap update succeeds or fails to
291 * support the RUI/RUD lifecycle rules.
292 */
293static int
294xfs_trans_log_finish_rmap_update(
295 struct xfs_trans *tp,
296 struct xfs_rud_log_item *rudp,
1534328b 297 struct xfs_rmap_intent *ri,
3cfce1e3
CH
298 struct xfs_btree_cur **pcur)
299{
300 int error;
301
1534328b 302 error = xfs_rmap_finish_one(tp, ri, pcur);
3cfce1e3
CH
303
304 /*
305 * Mark the transaction dirty, even on error. This ensures the
306 * transaction is aborted, which:
307 *
308 * 1.) releases the RUI and frees the RUD
309 * 2.) shuts down the filesystem
310 */
bb7b1c9c 311 tp->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE;
3cfce1e3
CH
312 set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags);
313
314 return error;
315}
316
317/* Sort rmap intents by AG. */
318static int
319xfs_rmap_update_diff_items(
320 void *priv,
4f0f586b
ST
321 const struct list_head *a,
322 const struct list_head *b)
3cfce1e3 323{
3cfce1e3
CH
324 struct xfs_rmap_intent *ra;
325 struct xfs_rmap_intent *rb;
326
327 ra = container_of(a, struct xfs_rmap_intent, ri_list);
328 rb = container_of(b, struct xfs_rmap_intent, ri_list);
c13418e8
DW
329
330 return ra->ri_pag->pag_agno - rb->ri_pag->pag_agno;
3cfce1e3
CH
331}
332
3cfce1e3
CH
333/* Log rmap updates in the intent item. */
334STATIC void
335xfs_rmap_update_log_item(
336 struct xfs_trans *tp,
c1f09188 337 struct xfs_rui_log_item *ruip,
ffaa196f 338 struct xfs_rmap_intent *ri)
3cfce1e3 339{
3cfce1e3
CH
340 uint next_extent;
341 struct xfs_map_extent *map;
342
3cfce1e3
CH
343 tp->t_flags |= XFS_TRANS_DIRTY;
344 set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags);
345
346 /*
347 * atomic_inc_return gives us the value after the increment;
348 * we want to use it as an array index so we need to subtract 1 from
349 * it.
350 */
351 next_extent = atomic_inc_return(&ruip->rui_next_extent) - 1;
352 ASSERT(next_extent < ruip->rui_format.rui_nextents);
353 map = &ruip->rui_format.rui_extents[next_extent];
ffaa196f
DW
354 map->me_owner = ri->ri_owner;
355 map->me_startblock = ri->ri_bmap.br_startblock;
356 map->me_startoff = ri->ri_bmap.br_startoff;
357 map->me_len = ri->ri_bmap.br_blockcount;
358 xfs_trans_set_rmap_flags(map, ri->ri_type, ri->ri_whichfork,
359 ri->ri_bmap.br_state);
3cfce1e3
CH
360}
361
13a83333 362static struct xfs_log_item *
c1f09188
CH
363xfs_rmap_update_create_intent(
364 struct xfs_trans *tp,
365 struct list_head *items,
d367a868
CH
366 unsigned int count,
367 bool sort)
c1f09188
CH
368{
369 struct xfs_mount *mp = tp->t_mountp;
370 struct xfs_rui_log_item *ruip = xfs_rui_init(mp, count);
ffaa196f 371 struct xfs_rmap_intent *ri;
c1f09188
CH
372
373 ASSERT(count > 0);
374
375 xfs_trans_add_item(tp, &ruip->rui_item);
d367a868
CH
376 if (sort)
377 list_sort(mp, items, xfs_rmap_update_diff_items);
ffaa196f
DW
378 list_for_each_entry(ri, items, ri_list)
379 xfs_rmap_update_log_item(tp, ruip, ri);
13a83333 380 return &ruip->rui_item;
c1f09188
CH
381}
382
3cfce1e3 383/* Get an RUD so we can process all the deferred rmap updates. */
f09d167c 384static struct xfs_log_item *
3cfce1e3
CH
385xfs_rmap_update_create_done(
386 struct xfs_trans *tp,
13a83333 387 struct xfs_log_item *intent,
3cfce1e3
CH
388 unsigned int count)
389{
f09d167c 390 return &xfs_trans_get_rud(tp, RUI_ITEM(intent))->rud_item;
3cfce1e3
CH
391}
392
c13418e8
DW
393/* Take a passive ref to the AG containing the space we're rmapping. */
394void
395xfs_rmap_update_get_group(
396 struct xfs_mount *mp,
397 struct xfs_rmap_intent *ri)
398{
399 xfs_agnumber_t agno;
400
401 agno = XFS_FSB_TO_AGNO(mp, ri->ri_bmap.br_startblock);
d5c88131 402 ri->ri_pag = xfs_perag_intent_get(mp, agno);
c13418e8
DW
403}
404
405/* Release a passive AG ref after finishing rmapping work. */
406static inline void
407xfs_rmap_update_put_group(
408 struct xfs_rmap_intent *ri)
409{
d5c88131 410 xfs_perag_intent_put(ri->ri_pag);
c13418e8
DW
411}
412
3cfce1e3
CH
413/* Process a deferred rmap update. */
414STATIC int
415xfs_rmap_update_finish_item(
416 struct xfs_trans *tp,
f09d167c 417 struct xfs_log_item *done,
3cfce1e3 418 struct list_head *item,
3ec1b26c 419 struct xfs_btree_cur **state)
3cfce1e3 420{
ffaa196f 421 struct xfs_rmap_intent *ri;
3cfce1e3
CH
422 int error;
423
ffaa196f
DW
424 ri = container_of(item, struct xfs_rmap_intent, ri_list);
425
426 error = xfs_trans_log_finish_rmap_update(tp, RUD_ITEM(done), ri,
3ec1b26c 427 state);
c13418e8
DW
428
429 xfs_rmap_update_put_group(ri);
ffaa196f 430 kmem_cache_free(xfs_rmap_intent_cache, ri);
3cfce1e3
CH
431 return error;
432}
433
3cfce1e3
CH
434/* Abort all pending RUIs. */
435STATIC void
436xfs_rmap_update_abort_intent(
13a83333 437 struct xfs_log_item *intent)
3cfce1e3 438{
13a83333 439 xfs_rui_release(RUI_ITEM(intent));
3cfce1e3
CH
440}
441
442/* Cancel a deferred rmap update. */
443STATIC void
444xfs_rmap_update_cancel_item(
445 struct list_head *item)
446{
ffaa196f 447 struct xfs_rmap_intent *ri;
3cfce1e3 448
ffaa196f 449 ri = container_of(item, struct xfs_rmap_intent, ri_list);
c13418e8
DW
450
451 xfs_rmap_update_put_group(ri);
ffaa196f 452 kmem_cache_free(xfs_rmap_intent_cache, ri);
3cfce1e3
CH
453}
454
455const struct xfs_defer_op_type xfs_rmap_update_defer_type = {
456 .max_items = XFS_RUI_MAX_FAST_EXTENTS,
3cfce1e3
CH
457 .create_intent = xfs_rmap_update_create_intent,
458 .abort_intent = xfs_rmap_update_abort_intent,
3cfce1e3
CH
459 .create_done = xfs_rmap_update_create_done,
460 .finish_item = xfs_rmap_update_finish_item,
3ec1b26c 461 .finish_cleanup = xfs_rmap_finish_one_cleanup,
3cfce1e3
CH
462 .cancel_item = xfs_rmap_update_cancel_item,
463};
464
dda7ba65
DW
465/* Is this recovered RUI ok? */
466static inline bool
467xfs_rui_validate_map(
468 struct xfs_mount *mp,
ffaa196f 469 struct xfs_map_extent *map)
dda7ba65 470{
38c26bfd 471 if (!xfs_has_rmapbt(mp))
da5de110
DW
472 return false;
473
ffaa196f 474 if (map->me_flags & ~XFS_RMAP_EXTENT_FLAGS)
c447ad62 475 return false;
dda7ba65 476
ffaa196f 477 switch (map->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
dda7ba65
DW
478 case XFS_RMAP_EXTENT_MAP:
479 case XFS_RMAP_EXTENT_MAP_SHARED:
480 case XFS_RMAP_EXTENT_UNMAP:
481 case XFS_RMAP_EXTENT_UNMAP_SHARED:
482 case XFS_RMAP_EXTENT_CONVERT:
483 case XFS_RMAP_EXTENT_CONVERT_SHARED:
484 case XFS_RMAP_EXTENT_ALLOC:
485 case XFS_RMAP_EXTENT_FREE:
dda7ba65
DW
486 break;
487 default:
c447ad62 488 return false;
dda7ba65 489 }
c447ad62 490
ffaa196f
DW
491 if (!XFS_RMAP_NON_INODE_OWNER(map->me_owner) &&
492 !xfs_verify_ino(mp, map->me_owner))
c447ad62
DW
493 return false;
494
ffaa196f 495 if (!xfs_verify_fileext(mp, map->me_startoff, map->me_len))
c447ad62
DW
496 return false;
497
ffaa196f 498 return xfs_verify_fsbext(mp, map->me_startblock, map->me_len);
dda7ba65
DW
499}
500
9e88b5d8
DW
501/*
502 * Process an rmap update intent item that was recovered from the log.
503 * We need to update the rmapbt.
504 */
cba0ccac 505STATIC int
96b60f82
DW
506xfs_rui_item_recover(
507 struct xfs_log_item *lip,
e6fff81e 508 struct list_head *capture_list)
9e88b5d8 509{
96b60f82 510 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
9c194644 511 struct xfs_rud_log_item *rudp;
9c194644
DW
512 struct xfs_trans *tp;
513 struct xfs_btree_cur *rcur = NULL;
d86142dd 514 struct xfs_mount *mp = lip->li_log->l_mp;
96b60f82 515 int i;
96b60f82 516 int error = 0;
9e88b5d8 517
9e88b5d8
DW
518 /*
519 * First check the validity of the extents described by the
520 * RUI. If any are bad, then assume that all are bad and
521 * just toss the RUI.
522 */
523 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
dda7ba65
DW
524 if (!xfs_rui_validate_map(mp,
525 &ruip->rui_format.rui_extents[i])) {
526 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
527 &ruip->rui_format,
528 sizeof(ruip->rui_format));
895e196f 529 return -EFSCORRUPTED;
dda7ba65 530 }
9e88b5d8
DW
531 }
532
b31c2bdc
DW
533 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
534 mp->m_rmap_maxlevels, 0, XFS_TRANS_RESERVE, &tp);
9c194644
DW
535 if (error)
536 return error;
722e2517 537 rudp = xfs_trans_get_rud(tp, ruip);
9c194644
DW
538
539 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
1534328b
DW
540 struct xfs_rmap_intent fake = { };
541 struct xfs_map_extent *map;
542
543 map = &ruip->rui_format.rui_extents[i];
544 switch (map->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
9c194644 545 case XFS_RMAP_EXTENT_MAP:
1534328b 546 fake.ri_type = XFS_RMAP_MAP;
9c194644 547 break;
ceeb9c83 548 case XFS_RMAP_EXTENT_MAP_SHARED:
1534328b 549 fake.ri_type = XFS_RMAP_MAP_SHARED;
ceeb9c83 550 break;
9c194644 551 case XFS_RMAP_EXTENT_UNMAP:
1534328b 552 fake.ri_type = XFS_RMAP_UNMAP;
9c194644 553 break;
ceeb9c83 554 case XFS_RMAP_EXTENT_UNMAP_SHARED:
1534328b 555 fake.ri_type = XFS_RMAP_UNMAP_SHARED;
ceeb9c83 556 break;
9c194644 557 case XFS_RMAP_EXTENT_CONVERT:
1534328b 558 fake.ri_type = XFS_RMAP_CONVERT;
9c194644 559 break;
3f165b33 560 case XFS_RMAP_EXTENT_CONVERT_SHARED:
1534328b 561 fake.ri_type = XFS_RMAP_CONVERT_SHARED;
3f165b33 562 break;
9c194644 563 case XFS_RMAP_EXTENT_ALLOC:
1534328b 564 fake.ri_type = XFS_RMAP_ALLOC;
9c194644
DW
565 break;
566 case XFS_RMAP_EXTENT_FREE:
1534328b 567 fake.ri_type = XFS_RMAP_FREE;
9c194644
DW
568 break;
569 default:
950f0d50
DW
570 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
571 &ruip->rui_format,
572 sizeof(ruip->rui_format));
9c194644
DW
573 error = -EFSCORRUPTED;
574 goto abort_error;
575 }
1534328b
DW
576
577 fake.ri_owner = map->me_owner;
578 fake.ri_whichfork = (map->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ?
579 XFS_ATTR_FORK : XFS_DATA_FORK;
580 fake.ri_bmap.br_startblock = map->me_startblock;
581 fake.ri_bmap.br_startoff = map->me_startoff;
582 fake.ri_bmap.br_blockcount = map->me_len;
583 fake.ri_bmap.br_state = (map->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ?
584 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
585
c13418e8 586 xfs_rmap_update_get_group(mp, &fake);
1534328b
DW
587 error = xfs_trans_log_finish_rmap_update(tp, rudp, &fake,
588 &rcur);
43059d54
DW
589 if (error == -EFSCORRUPTED)
590 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
1534328b 591 map, sizeof(*map));
c13418e8 592 xfs_rmap_update_put_group(&fake);
9c194644
DW
593 if (error)
594 goto abort_error;
595
596 }
597
598 xfs_rmap_finish_one_cleanup(tp, rcur, error);
512edfac 599 return xfs_defer_ops_capture_and_commit(tp, capture_list);
9c194644
DW
600
601abort_error:
602 xfs_rmap_finish_one_cleanup(tp, rcur, error);
603 xfs_trans_cancel(tp);
9e88b5d8
DW
604 return error;
605}
86ffa471 606
154c733a
DW
607STATIC bool
608xfs_rui_item_match(
609 struct xfs_log_item *lip,
610 uint64_t intent_id)
611{
612 return RUI_ITEM(lip)->rui_format.rui_id == intent_id;
613}
614
4e919af7
DW
615/* Relog an intent item to push the log tail forward. */
616static struct xfs_log_item *
617xfs_rui_item_relog(
618 struct xfs_log_item *intent,
619 struct xfs_trans *tp)
620{
621 struct xfs_rud_log_item *rudp;
622 struct xfs_rui_log_item *ruip;
ffaa196f 623 struct xfs_map_extent *map;
4e919af7
DW
624 unsigned int count;
625
626 count = RUI_ITEM(intent)->rui_format.rui_nextents;
ffaa196f 627 map = RUI_ITEM(intent)->rui_format.rui_extents;
4e919af7
DW
628
629 tp->t_flags |= XFS_TRANS_DIRTY;
630 rudp = xfs_trans_get_rud(tp, RUI_ITEM(intent));
631 set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags);
632
633 ruip = xfs_rui_init(tp->t_mountp, count);
ffaa196f 634 memcpy(ruip->rui_format.rui_extents, map, count * sizeof(*map));
4e919af7
DW
635 atomic_set(&ruip->rui_next_extent, count);
636 xfs_trans_add_item(tp, &ruip->rui_item);
637 set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags);
638 return &ruip->rui_item;
639}
640
cba0ccac 641static const struct xfs_item_ops xfs_rui_item_ops = {
f5b81200 642 .flags = XFS_ITEM_INTENT,
cba0ccac
DW
643 .iop_size = xfs_rui_item_size,
644 .iop_format = xfs_rui_item_format,
645 .iop_unpin = xfs_rui_item_unpin,
646 .iop_release = xfs_rui_item_release,
647 .iop_recover = xfs_rui_item_recover,
154c733a 648 .iop_match = xfs_rui_item_match,
4e919af7 649 .iop_relog = xfs_rui_item_relog,
cba0ccac
DW
650};
651
b45ca961
DW
652static inline void
653xfs_rui_copy_format(
654 struct xfs_rui_log_format *dst,
655 const struct xfs_rui_log_format *src)
656{
657 unsigned int i;
658
659 memcpy(dst, src, offsetof(struct xfs_rui_log_format, rui_extents));
660
661 for (i = 0; i < src->rui_nextents; i++)
662 memcpy(&dst->rui_extents[i], &src->rui_extents[i],
663 sizeof(struct xfs_map_extent));
664}
665
07590a9d
DW
666/*
667 * This routine is called to create an in-core extent rmap update
668 * item from the rui format structure which was logged on disk.
669 * It allocates an in-core rui, copies the extents from the format
670 * structure into it, and adds the rui to the AIL with the given
671 * LSN.
672 */
673STATIC int
674xlog_recover_rui_commit_pass2(
675 struct xlog *log,
676 struct list_head *buffer_list,
677 struct xlog_recover_item *item,
678 xfs_lsn_t lsn)
679{
07590a9d
DW
680 struct xfs_mount *mp = log->l_mp;
681 struct xfs_rui_log_item *ruip;
682 struct xfs_rui_log_format *rui_formatp;
b45ca961 683 size_t len;
07590a9d
DW
684
685 rui_formatp = item->ri_buf[0].i_addr;
686
b45ca961 687 if (item->ri_buf[0].i_len < xfs_rui_log_format_sizeof(0)) {
950f0d50
DW
688 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
689 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
b45ca961 690 return -EFSCORRUPTED;
07590a9d 691 }
b45ca961
DW
692
693 len = xfs_rui_log_format_sizeof(rui_formatp->rui_nextents);
694 if (item->ri_buf[0].i_len != len) {
950f0d50
DW
695 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
696 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
b45ca961
DW
697 return -EFSCORRUPTED;
698 }
699
700 ruip = xfs_rui_init(mp, rui_formatp->rui_nextents);
701 xfs_rui_copy_format(&ruip->rui_format, rui_formatp);
07590a9d 702 atomic_set(&ruip->rui_next_extent, rui_formatp->rui_nextents);
07590a9d 703 /*
86a37174
DW
704 * Insert the intent into the AIL directly and drop one reference so
705 * that finishing or canceling the work will drop the other.
07590a9d 706 */
86a37174 707 xfs_trans_ail_insert(log->l_ailp, &ruip->rui_item, lsn);
07590a9d
DW
708 xfs_rui_release(ruip);
709 return 0;
710}
711
86ffa471
DW
712const struct xlog_recover_item_ops xlog_rui_item_ops = {
713 .item_type = XFS_LI_RUI,
07590a9d 714 .commit_pass2 = xlog_recover_rui_commit_pass2,
86ffa471
DW
715};
716
07590a9d
DW
717/*
718 * This routine is called when an RUD format structure is found in a committed
719 * transaction in the log. Its purpose is to cancel the corresponding RUI if it
720 * was still in the log. To do this it searches the AIL for the RUI with an id
721 * equal to that in the RUD format structure. If we find it we drop the RUD
722 * reference, which removes the RUI from the AIL and frees it.
723 */
724STATIC int
725xlog_recover_rud_commit_pass2(
726 struct xlog *log,
727 struct list_head *buffer_list,
728 struct xlog_recover_item *item,
729 xfs_lsn_t lsn)
730{
731 struct xfs_rud_log_format *rud_formatp;
07590a9d
DW
732
733 rud_formatp = item->ri_buf[0].i_addr;
921ed96b
DW
734 if (item->ri_buf[0].i_len != sizeof(struct xfs_rud_log_format)) {
735 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
736 rud_formatp, item->ri_buf[0].i_len);
737 return -EFSCORRUPTED;
738 }
07590a9d 739
154c733a 740 xlog_recover_release_intent(log, XFS_LI_RUI, rud_formatp->rud_rui_id);
07590a9d
DW
741 return 0;
742}
743
86ffa471
DW
744const struct xlog_recover_item_ops xlog_rud_item_ops = {
745 .item_type = XFS_LI_RUD,
07590a9d 746 .commit_pass2 = xlog_recover_rud_commit_pass2,
86ffa471 747};