1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2016 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
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"
11 #include "xfs_bit.h"
12 #include "xfs_shared.h"
13 #include "xfs_mount.h"
14 #include "xfs_defer.h"
15 #include "xfs_trans.h"
16 #include "xfs_trans_priv.h"
17 #include "xfs_rmap_item.h"
18 #include "xfs_log.h"
19 #include "xfs_rmap.h"
20 #include "xfs_error.h"
21 #include "xfs_log_priv.h"
22 #include "xfs_log_recover.h"
23 #include "xfs_ag.h"
24 #include "xfs_btree.h"
25 #include "xfs_trace.h"
26 #include "xfs_rtgroup.h"
27
28 struct kmem_cache *xfs_rui_cache;
29 struct kmem_cache *xfs_rud_cache;
30
31 static const struct xfs_item_ops xfs_rui_item_ops;
32
RUI_ITEM(struct xfs_log_item * lip)33 static inline struct xfs_rui_log_item *RUI_ITEM(struct xfs_log_item *lip)
34 {
35 return container_of(lip, struct xfs_rui_log_item, rui_item);
36 }
37
38 STATIC void
xfs_rui_item_free(struct xfs_rui_log_item * ruip)39 xfs_rui_item_free(
40 struct xfs_rui_log_item *ruip)
41 {
42 kvfree(ruip->rui_item.li_lv_shadow);
43 if (ruip->rui_format.rui_nextents > XFS_RUI_MAX_FAST_EXTENTS)
44 kfree(ruip);
45 else
46 kmem_cache_free(xfs_rui_cache, ruip);
47 }
48
49 /*
50 * Freeing the RUI requires that we remove it from the AIL if it has already
51 * been placed there. However, the RUI may not yet have been placed in the AIL
52 * when called by xfs_rui_release() from RUD processing due to the ordering of
53 * committed vs unpin operations in bulk insert operations. Hence the reference
54 * count to ensure only the last caller frees the RUI.
55 */
56 STATIC void
xfs_rui_release(struct xfs_rui_log_item * ruip)57 xfs_rui_release(
58 struct xfs_rui_log_item *ruip)
59 {
60 ASSERT(atomic_read(&ruip->rui_refcount) > 0);
61 if (!atomic_dec_and_test(&ruip->rui_refcount))
62 return;
63
64 xfs_trans_ail_delete(&ruip->rui_item, 0);
65 xfs_rui_item_free(ruip);
66 }
67
68 STATIC void
xfs_rui_item_size(struct xfs_log_item * lip,int * nvecs,int * nbytes)69 xfs_rui_item_size(
70 struct xfs_log_item *lip,
71 int *nvecs,
72 int *nbytes)
73 {
74 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
75
76 *nvecs += 1;
77 *nbytes += xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents);
78 }
79
xfs_rui_log_space(unsigned int nr)80 unsigned int xfs_rui_log_space(unsigned int nr)
81 {
82 return xlog_item_space(1, xfs_rui_log_format_sizeof(nr));
83 }
84
85 /*
86 * This is called to fill in the vector of log iovecs for the
87 * given rui log item. We use only 1 iovec, and we point that
88 * at the rui_log_format structure embedded in the rui item.
89 * It is at this point that we assert that all of the extent
90 * slots in the rui item have been filled.
91 */
92 STATIC void
xfs_rui_item_format(struct xfs_log_item * lip,struct xfs_log_vec * lv)93 xfs_rui_item_format(
94 struct xfs_log_item *lip,
95 struct xfs_log_vec *lv)
96 {
97 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
98 struct xfs_log_iovec *vecp = NULL;
99
100 ASSERT(atomic_read(&ruip->rui_next_extent) ==
101 ruip->rui_format.rui_nextents);
102
103 ASSERT(lip->li_type == XFS_LI_RUI || lip->li_type == XFS_LI_RUI_RT);
104
105 ruip->rui_format.rui_type = lip->li_type;
106 ruip->rui_format.rui_size = 1;
107
108 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUI_FORMAT, &ruip->rui_format,
109 xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents));
110 }
111
112 /*
113 * The unpin operation is the last place an RUI is manipulated in the log. It is
114 * either inserted in the AIL or aborted in the event of a log I/O error. In
115 * either case, the RUI transaction has been successfully committed to make it
116 * this far. Therefore, we expect whoever committed the RUI to either construct
117 * and commit the RUD or drop the RUD's reference in the event of error. Simply
118 * drop the log's RUI reference now that the log is done with it.
119 */
120 STATIC void
xfs_rui_item_unpin(struct xfs_log_item * lip,int remove)121 xfs_rui_item_unpin(
122 struct xfs_log_item *lip,
123 int remove)
124 {
125 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
126
127 xfs_rui_release(ruip);
128 }
129
130 /*
131 * The RUI has been either committed or aborted if the transaction has been
132 * cancelled. If the transaction was cancelled, an RUD isn't going to be
133 * constructed and thus we free the RUI here directly.
134 */
135 STATIC void
xfs_rui_item_release(struct xfs_log_item * lip)136 xfs_rui_item_release(
137 struct xfs_log_item *lip)
138 {
139 xfs_rui_release(RUI_ITEM(lip));
140 }
141
142 /*
143 * Allocate and initialize an rui item with the given number of extents.
144 */
145 STATIC struct xfs_rui_log_item *
xfs_rui_init(struct xfs_mount * mp,unsigned short item_type,uint nextents)146 xfs_rui_init(
147 struct xfs_mount *mp,
148 unsigned short item_type,
149 uint nextents)
150
151 {
152 struct xfs_rui_log_item *ruip;
153
154 ASSERT(nextents > 0);
155 ASSERT(item_type == XFS_LI_RUI || item_type == XFS_LI_RUI_RT);
156
157 if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
158 ruip = kzalloc(xfs_rui_log_item_sizeof(nextents),
159 GFP_KERNEL | __GFP_NOFAIL);
160 else
161 ruip = kmem_cache_zalloc(xfs_rui_cache,
162 GFP_KERNEL | __GFP_NOFAIL);
163
164 xfs_log_item_init(mp, &ruip->rui_item, item_type, &xfs_rui_item_ops);
165 ruip->rui_format.rui_nextents = nextents;
166 ruip->rui_format.rui_id = (uintptr_t)(void *)ruip;
167 atomic_set(&ruip->rui_next_extent, 0);
168 atomic_set(&ruip->rui_refcount, 2);
169
170 return ruip;
171 }
172
RUD_ITEM(struct xfs_log_item * lip)173 static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip)
174 {
175 return container_of(lip, struct xfs_rud_log_item, rud_item);
176 }
177
178 STATIC void
xfs_rud_item_size(struct xfs_log_item * lip,int * nvecs,int * nbytes)179 xfs_rud_item_size(
180 struct xfs_log_item *lip,
181 int *nvecs,
182 int *nbytes)
183 {
184 *nvecs += 1;
185 *nbytes += sizeof(struct xfs_rud_log_format);
186 }
187
xfs_rud_log_space(void)188 unsigned int xfs_rud_log_space(void)
189 {
190 return xlog_item_space(1, sizeof(struct xfs_rud_log_format));
191 }
192
193 /*
194 * This is called to fill in the vector of log iovecs for the
195 * given rud log item. We use only 1 iovec, and we point that
196 * at the rud_log_format structure embedded in the rud item.
197 * It is at this point that we assert that all of the extent
198 * slots in the rud item have been filled.
199 */
200 STATIC void
xfs_rud_item_format(struct xfs_log_item * lip,struct xfs_log_vec * lv)201 xfs_rud_item_format(
202 struct xfs_log_item *lip,
203 struct xfs_log_vec *lv)
204 {
205 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
206 struct xfs_log_iovec *vecp = NULL;
207
208 ASSERT(lip->li_type == XFS_LI_RUD || lip->li_type == XFS_LI_RUD_RT);
209
210 rudp->rud_format.rud_type = lip->li_type;
211 rudp->rud_format.rud_size = 1;
212
213 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format,
214 sizeof(struct xfs_rud_log_format));
215 }
216
217 /*
218 * The RUD is either committed or aborted if the transaction is cancelled. If
219 * the transaction is cancelled, drop our reference to the RUI and free the
220 * RUD.
221 */
222 STATIC void
xfs_rud_item_release(struct xfs_log_item * lip)223 xfs_rud_item_release(
224 struct xfs_log_item *lip)
225 {
226 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
227
228 xfs_rui_release(rudp->rud_ruip);
229 kvfree(rudp->rud_item.li_lv_shadow);
230 kmem_cache_free(xfs_rud_cache, rudp);
231 }
232
233 static struct xfs_log_item *
xfs_rud_item_intent(struct xfs_log_item * lip)234 xfs_rud_item_intent(
235 struct xfs_log_item *lip)
236 {
237 return &RUD_ITEM(lip)->rud_ruip->rui_item;
238 }
239
240 static const struct xfs_item_ops xfs_rud_item_ops = {
241 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED |
242 XFS_ITEM_INTENT_DONE,
243 .iop_size = xfs_rud_item_size,
244 .iop_format = xfs_rud_item_format,
245 .iop_release = xfs_rud_item_release,
246 .iop_intent = xfs_rud_item_intent,
247 };
248
ri_entry(const struct list_head * e)249 static inline struct xfs_rmap_intent *ri_entry(const struct list_head *e)
250 {
251 return list_entry(e, struct xfs_rmap_intent, ri_list);
252 }
253
254 static inline bool
xfs_rui_item_isrt(const struct xfs_log_item * lip)255 xfs_rui_item_isrt(const struct xfs_log_item *lip)
256 {
257 ASSERT(lip->li_type == XFS_LI_RUI || lip->li_type == XFS_LI_RUI_RT);
258
259 return lip->li_type == XFS_LI_RUI_RT;
260 }
261
262 /* Sort rmap intents by AG. */
263 static int
xfs_rmap_update_diff_items(void * priv,const struct list_head * a,const struct list_head * b)264 xfs_rmap_update_diff_items(
265 void *priv,
266 const struct list_head *a,
267 const struct list_head *b)
268 {
269 struct xfs_rmap_intent *ra = ri_entry(a);
270 struct xfs_rmap_intent *rb = ri_entry(b);
271
272 return ra->ri_group->xg_gno - rb->ri_group->xg_gno;
273 }
274
275 /* Log rmap updates in the intent item. */
276 STATIC void
xfs_rmap_update_log_item(struct xfs_trans * tp,struct xfs_rui_log_item * ruip,struct xfs_rmap_intent * ri)277 xfs_rmap_update_log_item(
278 struct xfs_trans *tp,
279 struct xfs_rui_log_item *ruip,
280 struct xfs_rmap_intent *ri)
281 {
282 uint next_extent;
283 struct xfs_map_extent *map;
284
285 /*
286 * atomic_inc_return gives us the value after the increment;
287 * we want to use it as an array index so we need to subtract 1 from
288 * it.
289 */
290 next_extent = atomic_inc_return(&ruip->rui_next_extent) - 1;
291 ASSERT(next_extent < ruip->rui_format.rui_nextents);
292 map = &ruip->rui_format.rui_extents[next_extent];
293 map->me_owner = ri->ri_owner;
294 map->me_startblock = ri->ri_bmap.br_startblock;
295 map->me_startoff = ri->ri_bmap.br_startoff;
296 map->me_len = ri->ri_bmap.br_blockcount;
297
298 map->me_flags = 0;
299 if (ri->ri_bmap.br_state == XFS_EXT_UNWRITTEN)
300 map->me_flags |= XFS_RMAP_EXTENT_UNWRITTEN;
301 if (ri->ri_whichfork == XFS_ATTR_FORK)
302 map->me_flags |= XFS_RMAP_EXTENT_ATTR_FORK;
303 switch (ri->ri_type) {
304 case XFS_RMAP_MAP:
305 map->me_flags |= XFS_RMAP_EXTENT_MAP;
306 break;
307 case XFS_RMAP_MAP_SHARED:
308 map->me_flags |= XFS_RMAP_EXTENT_MAP_SHARED;
309 break;
310 case XFS_RMAP_UNMAP:
311 map->me_flags |= XFS_RMAP_EXTENT_UNMAP;
312 break;
313 case XFS_RMAP_UNMAP_SHARED:
314 map->me_flags |= XFS_RMAP_EXTENT_UNMAP_SHARED;
315 break;
316 case XFS_RMAP_CONVERT:
317 map->me_flags |= XFS_RMAP_EXTENT_CONVERT;
318 break;
319 case XFS_RMAP_CONVERT_SHARED:
320 map->me_flags |= XFS_RMAP_EXTENT_CONVERT_SHARED;
321 break;
322 case XFS_RMAP_ALLOC:
323 map->me_flags |= XFS_RMAP_EXTENT_ALLOC;
324 break;
325 case XFS_RMAP_FREE:
326 map->me_flags |= XFS_RMAP_EXTENT_FREE;
327 break;
328 default:
329 ASSERT(0);
330 }
331 }
332
333 static struct xfs_log_item *
__xfs_rmap_update_create_intent(struct xfs_trans * tp,struct list_head * items,unsigned int count,bool sort,unsigned short item_type)334 __xfs_rmap_update_create_intent(
335 struct xfs_trans *tp,
336 struct list_head *items,
337 unsigned int count,
338 bool sort,
339 unsigned short item_type)
340 {
341 struct xfs_mount *mp = tp->t_mountp;
342 struct xfs_rui_log_item *ruip;
343 struct xfs_rmap_intent *ri;
344
345 ASSERT(count > 0);
346
347 ruip = xfs_rui_init(mp, item_type, count);
348 if (sort)
349 list_sort(mp, items, xfs_rmap_update_diff_items);
350 list_for_each_entry(ri, items, ri_list)
351 xfs_rmap_update_log_item(tp, ruip, ri);
352 return &ruip->rui_item;
353 }
354
355 static struct xfs_log_item *
xfs_rmap_update_create_intent(struct xfs_trans * tp,struct list_head * items,unsigned int count,bool sort)356 xfs_rmap_update_create_intent(
357 struct xfs_trans *tp,
358 struct list_head *items,
359 unsigned int count,
360 bool sort)
361 {
362 return __xfs_rmap_update_create_intent(tp, items, count, sort,
363 XFS_LI_RUI);
364 }
365
366 static inline unsigned short
xfs_rud_type_from_rui(const struct xfs_rui_log_item * ruip)367 xfs_rud_type_from_rui(const struct xfs_rui_log_item *ruip)
368 {
369 return xfs_rui_item_isrt(&ruip->rui_item) ? XFS_LI_RUD_RT : XFS_LI_RUD;
370 }
371
372 /* Get an RUD so we can process all the deferred rmap updates. */
373 static struct xfs_log_item *
xfs_rmap_update_create_done(struct xfs_trans * tp,struct xfs_log_item * intent,unsigned int count)374 xfs_rmap_update_create_done(
375 struct xfs_trans *tp,
376 struct xfs_log_item *intent,
377 unsigned int count)
378 {
379 struct xfs_rui_log_item *ruip = RUI_ITEM(intent);
380 struct xfs_rud_log_item *rudp;
381
382 rudp = kmem_cache_zalloc(xfs_rud_cache, GFP_KERNEL | __GFP_NOFAIL);
383 xfs_log_item_init(tp->t_mountp, &rudp->rud_item,
384 xfs_rud_type_from_rui(ruip), &xfs_rud_item_ops);
385 rudp->rud_ruip = ruip;
386 rudp->rud_format.rud_rui_id = ruip->rui_format.rui_id;
387
388 return &rudp->rud_item;
389 }
390
391 /* Add this deferred RUI to the transaction. */
392 void
xfs_rmap_defer_add(struct xfs_trans * tp,struct xfs_rmap_intent * ri)393 xfs_rmap_defer_add(
394 struct xfs_trans *tp,
395 struct xfs_rmap_intent *ri)
396 {
397 struct xfs_mount *mp = tp->t_mountp;
398
399 /*
400 * Deferred rmap updates for the realtime and data sections must use
401 * separate transactions to finish deferred work because updates to
402 * realtime metadata files can lock AGFs to allocate btree blocks and
403 * we don't want that mixing with the AGF locks taken to finish data
404 * section updates.
405 */
406 ri->ri_group = xfs_group_intent_get(mp, ri->ri_bmap.br_startblock,
407 ri->ri_realtime ? XG_TYPE_RTG : XG_TYPE_AG);
408
409 trace_xfs_rmap_defer(mp, ri);
410 xfs_defer_add(tp, &ri->ri_list, ri->ri_realtime ?
411 &xfs_rtrmap_update_defer_type :
412 &xfs_rmap_update_defer_type);
413 }
414
415 /* Cancel a deferred rmap update. */
416 STATIC void
xfs_rmap_update_cancel_item(struct list_head * item)417 xfs_rmap_update_cancel_item(
418 struct list_head *item)
419 {
420 struct xfs_rmap_intent *ri = ri_entry(item);
421
422 xfs_group_intent_put(ri->ri_group);
423 kmem_cache_free(xfs_rmap_intent_cache, ri);
424 }
425
426 /* Process a deferred rmap update. */
427 STATIC int
xfs_rmap_update_finish_item(struct xfs_trans * tp,struct xfs_log_item * done,struct list_head * item,struct xfs_btree_cur ** state)428 xfs_rmap_update_finish_item(
429 struct xfs_trans *tp,
430 struct xfs_log_item *done,
431 struct list_head *item,
432 struct xfs_btree_cur **state)
433 {
434 struct xfs_rmap_intent *ri = ri_entry(item);
435 int error;
436
437 error = xfs_rmap_finish_one(tp, ri, state);
438
439 xfs_rmap_update_cancel_item(item);
440 return error;
441 }
442
443 /* Clean up after calling xfs_rmap_finish_one. */
444 STATIC void
xfs_rmap_finish_one_cleanup(struct xfs_trans * tp,struct xfs_btree_cur * rcur,int error)445 xfs_rmap_finish_one_cleanup(
446 struct xfs_trans *tp,
447 struct xfs_btree_cur *rcur,
448 int error)
449 {
450 struct xfs_buf *agbp = NULL;
451
452 if (rcur == NULL)
453 return;
454 agbp = rcur->bc_ag.agbp;
455 xfs_btree_del_cursor(rcur, error);
456 if (error && agbp)
457 xfs_trans_brelse(tp, agbp);
458 }
459
460 /* Abort all pending RUIs. */
461 STATIC void
xfs_rmap_update_abort_intent(struct xfs_log_item * intent)462 xfs_rmap_update_abort_intent(
463 struct xfs_log_item *intent)
464 {
465 xfs_rui_release(RUI_ITEM(intent));
466 }
467
468 /* Is this recovered RUI ok? */
469 static inline bool
xfs_rui_validate_map(struct xfs_mount * mp,bool isrt,struct xfs_map_extent * map)470 xfs_rui_validate_map(
471 struct xfs_mount *mp,
472 bool isrt,
473 struct xfs_map_extent *map)
474 {
475 if (!xfs_has_rmapbt(mp))
476 return false;
477
478 if (map->me_flags & ~XFS_RMAP_EXTENT_FLAGS)
479 return false;
480
481 switch (map->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
482 case XFS_RMAP_EXTENT_MAP:
483 case XFS_RMAP_EXTENT_MAP_SHARED:
484 case XFS_RMAP_EXTENT_UNMAP:
485 case XFS_RMAP_EXTENT_UNMAP_SHARED:
486 case XFS_RMAP_EXTENT_CONVERT:
487 case XFS_RMAP_EXTENT_CONVERT_SHARED:
488 case XFS_RMAP_EXTENT_ALLOC:
489 case XFS_RMAP_EXTENT_FREE:
490 break;
491 default:
492 return false;
493 }
494
495 if (!XFS_RMAP_NON_INODE_OWNER(map->me_owner) &&
496 !xfs_verify_ino(mp, map->me_owner))
497 return false;
498
499 if (!xfs_verify_fileext(mp, map->me_startoff, map->me_len))
500 return false;
501
502 if (isrt)
503 return xfs_verify_rtbext(mp, map->me_startblock, map->me_len);
504
505 return xfs_verify_fsbext(mp, map->me_startblock, map->me_len);
506 }
507
508 static inline void
xfs_rui_recover_work(struct xfs_mount * mp,struct xfs_defer_pending * dfp,bool isrt,const struct xfs_map_extent * map)509 xfs_rui_recover_work(
510 struct xfs_mount *mp,
511 struct xfs_defer_pending *dfp,
512 bool isrt,
513 const struct xfs_map_extent *map)
514 {
515 struct xfs_rmap_intent *ri;
516
517 ri = kmem_cache_alloc(xfs_rmap_intent_cache, GFP_KERNEL | __GFP_NOFAIL);
518
519 switch (map->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
520 case XFS_RMAP_EXTENT_MAP:
521 ri->ri_type = XFS_RMAP_MAP;
522 break;
523 case XFS_RMAP_EXTENT_MAP_SHARED:
524 ri->ri_type = XFS_RMAP_MAP_SHARED;
525 break;
526 case XFS_RMAP_EXTENT_UNMAP:
527 ri->ri_type = XFS_RMAP_UNMAP;
528 break;
529 case XFS_RMAP_EXTENT_UNMAP_SHARED:
530 ri->ri_type = XFS_RMAP_UNMAP_SHARED;
531 break;
532 case XFS_RMAP_EXTENT_CONVERT:
533 ri->ri_type = XFS_RMAP_CONVERT;
534 break;
535 case XFS_RMAP_EXTENT_CONVERT_SHARED:
536 ri->ri_type = XFS_RMAP_CONVERT_SHARED;
537 break;
538 case XFS_RMAP_EXTENT_ALLOC:
539 ri->ri_type = XFS_RMAP_ALLOC;
540 break;
541 case XFS_RMAP_EXTENT_FREE:
542 ri->ri_type = XFS_RMAP_FREE;
543 break;
544 default:
545 ASSERT(0);
546 return;
547 }
548
549 ri->ri_owner = map->me_owner;
550 ri->ri_whichfork = (map->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ?
551 XFS_ATTR_FORK : XFS_DATA_FORK;
552 ri->ri_bmap.br_startblock = map->me_startblock;
553 ri->ri_bmap.br_startoff = map->me_startoff;
554 ri->ri_bmap.br_blockcount = map->me_len;
555 ri->ri_bmap.br_state = (map->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ?
556 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
557 ri->ri_group = xfs_group_intent_get(mp, map->me_startblock,
558 isrt ? XG_TYPE_RTG : XG_TYPE_AG);
559 ri->ri_realtime = isrt;
560
561 xfs_defer_add_item(dfp, &ri->ri_list);
562 }
563
564 /*
565 * Process an rmap update intent item that was recovered from the log.
566 * We need to update the rmapbt.
567 */
568 STATIC int
xfs_rmap_recover_work(struct xfs_defer_pending * dfp,struct list_head * capture_list)569 xfs_rmap_recover_work(
570 struct xfs_defer_pending *dfp,
571 struct list_head *capture_list)
572 {
573 struct xfs_trans_res resv;
574 struct xfs_log_item *lip = dfp->dfp_intent;
575 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
576 struct xfs_trans *tp;
577 struct xfs_mount *mp = lip->li_log->l_mp;
578 bool isrt = xfs_rui_item_isrt(lip);
579 int i;
580 int error = 0;
581
582 /*
583 * First check the validity of the extents described by the
584 * RUI. If any are bad, then assume that all are bad and
585 * just toss the RUI.
586 */
587 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
588 if (!xfs_rui_validate_map(mp, isrt,
589 &ruip->rui_format.rui_extents[i])) {
590 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
591 &ruip->rui_format,
592 sizeof(ruip->rui_format));
593 return -EFSCORRUPTED;
594 }
595
596 xfs_rui_recover_work(mp, dfp, isrt,
597 &ruip->rui_format.rui_extents[i]);
598 }
599
600 resv = xlog_recover_resv(&M_RES(mp)->tr_itruncate);
601 error = xfs_trans_alloc(mp, &resv, mp->m_rmap_maxlevels, 0,
602 XFS_TRANS_RESERVE, &tp);
603 if (error)
604 return error;
605
606 error = xlog_recover_finish_intent(tp, dfp);
607 if (error == -EFSCORRUPTED)
608 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
609 &ruip->rui_format,
610 sizeof(ruip->rui_format));
611 if (error)
612 goto abort_error;
613
614 return xfs_defer_ops_capture_and_commit(tp, capture_list);
615
616 abort_error:
617 xfs_trans_cancel(tp);
618 return error;
619 }
620
621 /* Relog an intent item to push the log tail forward. */
622 static struct xfs_log_item *
xfs_rmap_relog_intent(struct xfs_trans * tp,struct xfs_log_item * intent,struct xfs_log_item * done_item)623 xfs_rmap_relog_intent(
624 struct xfs_trans *tp,
625 struct xfs_log_item *intent,
626 struct xfs_log_item *done_item)
627 {
628 struct xfs_rui_log_item *ruip;
629 struct xfs_map_extent *map;
630 unsigned int count;
631
632 ASSERT(intent->li_type == XFS_LI_RUI ||
633 intent->li_type == XFS_LI_RUI_RT);
634
635 count = RUI_ITEM(intent)->rui_format.rui_nextents;
636 map = RUI_ITEM(intent)->rui_format.rui_extents;
637
638 ruip = xfs_rui_init(tp->t_mountp, intent->li_type, count);
639 memcpy(ruip->rui_format.rui_extents, map, count * sizeof(*map));
640 atomic_set(&ruip->rui_next_extent, count);
641
642 return &ruip->rui_item;
643 }
644
645 const struct xfs_defer_op_type xfs_rmap_update_defer_type = {
646 .name = "rmap",
647 .max_items = XFS_RUI_MAX_FAST_EXTENTS,
648 .create_intent = xfs_rmap_update_create_intent,
649 .abort_intent = xfs_rmap_update_abort_intent,
650 .create_done = xfs_rmap_update_create_done,
651 .finish_item = xfs_rmap_update_finish_item,
652 .finish_cleanup = xfs_rmap_finish_one_cleanup,
653 .cancel_item = xfs_rmap_update_cancel_item,
654 .recover_work = xfs_rmap_recover_work,
655 .relog_intent = xfs_rmap_relog_intent,
656 };
657
658 #ifdef CONFIG_XFS_RT
659 static struct xfs_log_item *
xfs_rtrmap_update_create_intent(struct xfs_trans * tp,struct list_head * items,unsigned int count,bool sort)660 xfs_rtrmap_update_create_intent(
661 struct xfs_trans *tp,
662 struct list_head *items,
663 unsigned int count,
664 bool sort)
665 {
666 return __xfs_rmap_update_create_intent(tp, items, count, sort,
667 XFS_LI_RUI_RT);
668 }
669
670 /* Clean up after calling xfs_rmap_finish_one. */
671 STATIC void
xfs_rtrmap_finish_one_cleanup(struct xfs_trans * tp,struct xfs_btree_cur * rcur,int error)672 xfs_rtrmap_finish_one_cleanup(
673 struct xfs_trans *tp,
674 struct xfs_btree_cur *rcur,
675 int error)
676 {
677 if (rcur)
678 xfs_btree_del_cursor(rcur, error);
679 }
680
681 const struct xfs_defer_op_type xfs_rtrmap_update_defer_type = {
682 .name = "rtrmap",
683 .max_items = XFS_RUI_MAX_FAST_EXTENTS,
684 .create_intent = xfs_rtrmap_update_create_intent,
685 .abort_intent = xfs_rmap_update_abort_intent,
686 .create_done = xfs_rmap_update_create_done,
687 .finish_item = xfs_rmap_update_finish_item,
688 .finish_cleanup = xfs_rtrmap_finish_one_cleanup,
689 .cancel_item = xfs_rmap_update_cancel_item,
690 .recover_work = xfs_rmap_recover_work,
691 .relog_intent = xfs_rmap_relog_intent,
692 };
693 #else
694 const struct xfs_defer_op_type xfs_rtrmap_update_defer_type = {
695 .name = "rtrmap",
696 };
697 #endif
698
699 STATIC bool
xfs_rui_item_match(struct xfs_log_item * lip,uint64_t intent_id)700 xfs_rui_item_match(
701 struct xfs_log_item *lip,
702 uint64_t intent_id)
703 {
704 return RUI_ITEM(lip)->rui_format.rui_id == intent_id;
705 }
706
707 static const struct xfs_item_ops xfs_rui_item_ops = {
708 .flags = XFS_ITEM_INTENT,
709 .iop_size = xfs_rui_item_size,
710 .iop_format = xfs_rui_item_format,
711 .iop_unpin = xfs_rui_item_unpin,
712 .iop_release = xfs_rui_item_release,
713 .iop_match = xfs_rui_item_match,
714 };
715
716 static inline void
xfs_rui_copy_format(struct xfs_rui_log_format * dst,const struct xfs_rui_log_format * src)717 xfs_rui_copy_format(
718 struct xfs_rui_log_format *dst,
719 const struct xfs_rui_log_format *src)
720 {
721 unsigned int i;
722
723 memcpy(dst, src, offsetof(struct xfs_rui_log_format, rui_extents));
724
725 for (i = 0; i < src->rui_nextents; i++)
726 memcpy(&dst->rui_extents[i], &src->rui_extents[i],
727 sizeof(struct xfs_map_extent));
728 }
729
730 /*
731 * This routine is called to create an in-core extent rmap update
732 * item from the rui format structure which was logged on disk.
733 * It allocates an in-core rui, copies the extents from the format
734 * structure into it, and adds the rui to the AIL with the given
735 * LSN.
736 */
737 STATIC int
xlog_recover_rui_commit_pass2(struct xlog * log,struct list_head * buffer_list,struct xlog_recover_item * item,xfs_lsn_t lsn)738 xlog_recover_rui_commit_pass2(
739 struct xlog *log,
740 struct list_head *buffer_list,
741 struct xlog_recover_item *item,
742 xfs_lsn_t lsn)
743 {
744 struct xfs_mount *mp = log->l_mp;
745 struct xfs_rui_log_item *ruip;
746 struct xfs_rui_log_format *rui_formatp;
747 size_t len;
748
749 rui_formatp = item->ri_buf[0].iov_base;
750
751 if (item->ri_buf[0].iov_len < xfs_rui_log_format_sizeof(0)) {
752 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
753 item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
754 return -EFSCORRUPTED;
755 }
756
757 len = xfs_rui_log_format_sizeof(rui_formatp->rui_nextents);
758 if (item->ri_buf[0].iov_len != len) {
759 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
760 item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
761 return -EFSCORRUPTED;
762 }
763
764 ruip = xfs_rui_init(mp, ITEM_TYPE(item), rui_formatp->rui_nextents);
765 xfs_rui_copy_format(&ruip->rui_format, rui_formatp);
766 atomic_set(&ruip->rui_next_extent, rui_formatp->rui_nextents);
767
768 xlog_recover_intent_item(log, &ruip->rui_item, lsn,
769 &xfs_rmap_update_defer_type);
770 return 0;
771 }
772
773 const struct xlog_recover_item_ops xlog_rui_item_ops = {
774 .item_type = XFS_LI_RUI,
775 .commit_pass2 = xlog_recover_rui_commit_pass2,
776 };
777
778 #ifdef CONFIG_XFS_RT
779 STATIC int
xlog_recover_rtrui_commit_pass2(struct xlog * log,struct list_head * buffer_list,struct xlog_recover_item * item,xfs_lsn_t lsn)780 xlog_recover_rtrui_commit_pass2(
781 struct xlog *log,
782 struct list_head *buffer_list,
783 struct xlog_recover_item *item,
784 xfs_lsn_t lsn)
785 {
786 struct xfs_mount *mp = log->l_mp;
787 struct xfs_rui_log_item *ruip;
788 struct xfs_rui_log_format *rui_formatp;
789 size_t len;
790
791 rui_formatp = item->ri_buf[0].iov_base;
792
793 if (item->ri_buf[0].iov_len < xfs_rui_log_format_sizeof(0)) {
794 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
795 item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
796 return -EFSCORRUPTED;
797 }
798
799 len = xfs_rui_log_format_sizeof(rui_formatp->rui_nextents);
800 if (item->ri_buf[0].iov_len != len) {
801 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
802 item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
803 return -EFSCORRUPTED;
804 }
805
806 ruip = xfs_rui_init(mp, ITEM_TYPE(item), rui_formatp->rui_nextents);
807 xfs_rui_copy_format(&ruip->rui_format, rui_formatp);
808 atomic_set(&ruip->rui_next_extent, rui_formatp->rui_nextents);
809
810 xlog_recover_intent_item(log, &ruip->rui_item, lsn,
811 &xfs_rtrmap_update_defer_type);
812 return 0;
813 }
814 #else
815 STATIC int
xlog_recover_rtrui_commit_pass2(struct xlog * log,struct list_head * buffer_list,struct xlog_recover_item * item,xfs_lsn_t lsn)816 xlog_recover_rtrui_commit_pass2(
817 struct xlog *log,
818 struct list_head *buffer_list,
819 struct xlog_recover_item *item,
820 xfs_lsn_t lsn)
821 {
822 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
823 item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
824 return -EFSCORRUPTED;
825 }
826 #endif
827
828 const struct xlog_recover_item_ops xlog_rtrui_item_ops = {
829 .item_type = XFS_LI_RUI_RT,
830 .commit_pass2 = xlog_recover_rtrui_commit_pass2,
831 };
832
833 /*
834 * This routine is called when an RUD format structure is found in a committed
835 * transaction in the log. Its purpose is to cancel the corresponding RUI if it
836 * was still in the log. To do this it searches the AIL for the RUI with an id
837 * equal to that in the RUD format structure. If we find it we drop the RUD
838 * reference, which removes the RUI from the AIL and frees it.
839 */
840 STATIC int
xlog_recover_rud_commit_pass2(struct xlog * log,struct list_head * buffer_list,struct xlog_recover_item * item,xfs_lsn_t lsn)841 xlog_recover_rud_commit_pass2(
842 struct xlog *log,
843 struct list_head *buffer_list,
844 struct xlog_recover_item *item,
845 xfs_lsn_t lsn)
846 {
847 struct xfs_rud_log_format *rud_formatp;
848
849 rud_formatp = item->ri_buf[0].iov_base;
850 if (item->ri_buf[0].iov_len != sizeof(struct xfs_rud_log_format)) {
851 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
852 rud_formatp, item->ri_buf[0].iov_len);
853 return -EFSCORRUPTED;
854 }
855
856 xlog_recover_release_intent(log, XFS_LI_RUI, rud_formatp->rud_rui_id);
857 return 0;
858 }
859
860 const struct xlog_recover_item_ops xlog_rud_item_ops = {
861 .item_type = XFS_LI_RUD,
862 .commit_pass2 = xlog_recover_rud_commit_pass2,
863 };
864
865 #ifdef CONFIG_XFS_RT
866 STATIC int
xlog_recover_rtrud_commit_pass2(struct xlog * log,struct list_head * buffer_list,struct xlog_recover_item * item,xfs_lsn_t lsn)867 xlog_recover_rtrud_commit_pass2(
868 struct xlog *log,
869 struct list_head *buffer_list,
870 struct xlog_recover_item *item,
871 xfs_lsn_t lsn)
872 {
873 struct xfs_rud_log_format *rud_formatp;
874
875 rud_formatp = item->ri_buf[0].iov_base;
876 if (item->ri_buf[0].iov_len != sizeof(struct xfs_rud_log_format)) {
877 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
878 rud_formatp, item->ri_buf[0].iov_len);
879 return -EFSCORRUPTED;
880 }
881
882 xlog_recover_release_intent(log, XFS_LI_RUI_RT,
883 rud_formatp->rud_rui_id);
884 return 0;
885 }
886 #else
887 # define xlog_recover_rtrud_commit_pass2 xlog_recover_rtrui_commit_pass2
888 #endif
889
890 const struct xlog_recover_item_ops xlog_rtrud_item_ops = {
891 .item_type = XFS_LI_RUD_RT,
892 .commit_pass2 = xlog_recover_rtrud_commit_pass2,
893 };
894