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_refcount_item.h"
18 #include "xfs_log.h"
19 #include "xfs_refcount.h"
20 #include "xfs_error.h"
21 #include "xfs_log_priv.h"
22 #include "xfs_log_recover.h"
23
24 kmem_zone_t *xfs_cui_zone;
25 kmem_zone_t *xfs_cud_zone;
26
27 static const struct xfs_item_ops xfs_cui_item_ops;
28
CUI_ITEM(struct xfs_log_item * lip)29 static inline struct xfs_cui_log_item *CUI_ITEM(struct xfs_log_item *lip)
30 {
31 return container_of(lip, struct xfs_cui_log_item, cui_item);
32 }
33
34 STATIC void
xfs_cui_item_free(struct xfs_cui_log_item * cuip)35 xfs_cui_item_free(
36 struct xfs_cui_log_item *cuip)
37 {
38 kmem_free(cuip->cui_item.li_lv_shadow);
39 if (cuip->cui_format.cui_nextents > XFS_CUI_MAX_FAST_EXTENTS)
40 kmem_free(cuip);
41 else
42 kmem_cache_free(xfs_cui_zone, cuip);
43 }
44
45 /*
46 * Freeing the CUI requires that we remove it from the AIL if it has already
47 * been placed there. However, the CUI may not yet have been placed in the AIL
48 * when called by xfs_cui_release() from CUD 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 CUI.
51 */
52 STATIC void
xfs_cui_release(struct xfs_cui_log_item * cuip)53 xfs_cui_release(
54 struct xfs_cui_log_item *cuip)
55 {
56 ASSERT(atomic_read(&cuip->cui_refcount) > 0);
57 if (atomic_dec_and_test(&cuip->cui_refcount)) {
58 xfs_trans_ail_delete(&cuip->cui_item, SHUTDOWN_LOG_IO_ERROR);
59 xfs_cui_item_free(cuip);
60 }
61 }
62
63
64 STATIC void
xfs_cui_item_size(struct xfs_log_item * lip,int * nvecs,int * nbytes)65 xfs_cui_item_size(
66 struct xfs_log_item *lip,
67 int *nvecs,
68 int *nbytes)
69 {
70 struct xfs_cui_log_item *cuip = CUI_ITEM(lip);
71
72 *nvecs += 1;
73 *nbytes += xfs_cui_log_format_sizeof(cuip->cui_format.cui_nextents);
74 }
75
76 /*
77 * This is called to fill in the vector of log iovecs for the
78 * given cui log item. We use only 1 iovec, and we point that
79 * at the cui_log_format structure embedded in the cui item.
80 * It is at this point that we assert that all of the extent
81 * slots in the cui item have been filled.
82 */
83 STATIC void
xfs_cui_item_format(struct xfs_log_item * lip,struct xfs_log_vec * lv)84 xfs_cui_item_format(
85 struct xfs_log_item *lip,
86 struct xfs_log_vec *lv)
87 {
88 struct xfs_cui_log_item *cuip = CUI_ITEM(lip);
89 struct xfs_log_iovec *vecp = NULL;
90
91 ASSERT(atomic_read(&cuip->cui_next_extent) ==
92 cuip->cui_format.cui_nextents);
93
94 cuip->cui_format.cui_type = XFS_LI_CUI;
95 cuip->cui_format.cui_size = 1;
96
97 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_CUI_FORMAT, &cuip->cui_format,
98 xfs_cui_log_format_sizeof(cuip->cui_format.cui_nextents));
99 }
100
101 /*
102 * The unpin operation is the last place an CUI 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 CUI transaction has been successfully committed to make it
105 * this far. Therefore, we expect whoever committed the CUI to either construct
106 * and commit the CUD or drop the CUD's reference in the event of error. Simply
107 * drop the log's CUI reference now that the log is done with it.
108 */
109 STATIC void
xfs_cui_item_unpin(struct xfs_log_item * lip,int remove)110 xfs_cui_item_unpin(
111 struct xfs_log_item *lip,
112 int remove)
113 {
114 struct xfs_cui_log_item *cuip = CUI_ITEM(lip);
115
116 xfs_cui_release(cuip);
117 }
118
119 /*
120 * The CUI has been either committed or aborted if the transaction has been
121 * cancelled. If the transaction was cancelled, an CUD isn't going to be
122 * constructed and thus we free the CUI here directly.
123 */
124 STATIC void
xfs_cui_item_release(struct xfs_log_item * lip)125 xfs_cui_item_release(
126 struct xfs_log_item *lip)
127 {
128 xfs_cui_release(CUI_ITEM(lip));
129 }
130
131 /*
132 * Allocate and initialize an cui item with the given number of extents.
133 */
134 STATIC struct xfs_cui_log_item *
xfs_cui_init(struct xfs_mount * mp,uint nextents)135 xfs_cui_init(
136 struct xfs_mount *mp,
137 uint nextents)
138
139 {
140 struct xfs_cui_log_item *cuip;
141
142 ASSERT(nextents > 0);
143 if (nextents > XFS_CUI_MAX_FAST_EXTENTS)
144 cuip = kmem_zalloc(xfs_cui_log_item_sizeof(nextents),
145 0);
146 else
147 cuip = kmem_cache_zalloc(xfs_cui_zone,
148 GFP_KERNEL | __GFP_NOFAIL);
149
150 xfs_log_item_init(mp, &cuip->cui_item, XFS_LI_CUI, &xfs_cui_item_ops);
151 cuip->cui_format.cui_nextents = nextents;
152 cuip->cui_format.cui_id = (uintptr_t)(void *)cuip;
153 atomic_set(&cuip->cui_next_extent, 0);
154 atomic_set(&cuip->cui_refcount, 2);
155
156 return cuip;
157 }
158
CUD_ITEM(struct xfs_log_item * lip)159 static inline struct xfs_cud_log_item *CUD_ITEM(struct xfs_log_item *lip)
160 {
161 return container_of(lip, struct xfs_cud_log_item, cud_item);
162 }
163
164 STATIC void
xfs_cud_item_size(struct xfs_log_item * lip,int * nvecs,int * nbytes)165 xfs_cud_item_size(
166 struct xfs_log_item *lip,
167 int *nvecs,
168 int *nbytes)
169 {
170 *nvecs += 1;
171 *nbytes += sizeof(struct xfs_cud_log_format);
172 }
173
174 /*
175 * This is called to fill in the vector of log iovecs for the
176 * given cud log item. We use only 1 iovec, and we point that
177 * at the cud_log_format structure embedded in the cud item.
178 * It is at this point that we assert that all of the extent
179 * slots in the cud item have been filled.
180 */
181 STATIC void
xfs_cud_item_format(struct xfs_log_item * lip,struct xfs_log_vec * lv)182 xfs_cud_item_format(
183 struct xfs_log_item *lip,
184 struct xfs_log_vec *lv)
185 {
186 struct xfs_cud_log_item *cudp = CUD_ITEM(lip);
187 struct xfs_log_iovec *vecp = NULL;
188
189 cudp->cud_format.cud_type = XFS_LI_CUD;
190 cudp->cud_format.cud_size = 1;
191
192 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_CUD_FORMAT, &cudp->cud_format,
193 sizeof(struct xfs_cud_log_format));
194 }
195
196 /*
197 * The CUD is either committed or aborted if the transaction is cancelled. If
198 * the transaction is cancelled, drop our reference to the CUI and free the
199 * CUD.
200 */
201 STATIC void
xfs_cud_item_release(struct xfs_log_item * lip)202 xfs_cud_item_release(
203 struct xfs_log_item *lip)
204 {
205 struct xfs_cud_log_item *cudp = CUD_ITEM(lip);
206
207 xfs_cui_release(cudp->cud_cuip);
208 kmem_free(cudp->cud_item.li_lv_shadow);
209 kmem_cache_free(xfs_cud_zone, cudp);
210 }
211
212 static const struct xfs_item_ops xfs_cud_item_ops = {
213 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED,
214 .iop_size = xfs_cud_item_size,
215 .iop_format = xfs_cud_item_format,
216 .iop_release = xfs_cud_item_release,
217 };
218
219 static struct xfs_cud_log_item *
xfs_trans_get_cud(struct xfs_trans * tp,struct xfs_cui_log_item * cuip)220 xfs_trans_get_cud(
221 struct xfs_trans *tp,
222 struct xfs_cui_log_item *cuip)
223 {
224 struct xfs_cud_log_item *cudp;
225
226 cudp = kmem_cache_zalloc(xfs_cud_zone, GFP_KERNEL | __GFP_NOFAIL);
227 xfs_log_item_init(tp->t_mountp, &cudp->cud_item, XFS_LI_CUD,
228 &xfs_cud_item_ops);
229 cudp->cud_cuip = cuip;
230 cudp->cud_format.cud_cui_id = cuip->cui_format.cui_id;
231
232 xfs_trans_add_item(tp, &cudp->cud_item);
233 return cudp;
234 }
235
236 /*
237 * Finish an refcount update and log it to the CUD. Note that the
238 * transaction is marked dirty regardless of whether the refcount
239 * update succeeds or fails to support the CUI/CUD lifecycle rules.
240 */
241 static int
xfs_trans_log_finish_refcount_update(struct xfs_trans * tp,struct xfs_cud_log_item * cudp,enum xfs_refcount_intent_type type,xfs_fsblock_t startblock,xfs_extlen_t blockcount,xfs_fsblock_t * new_fsb,xfs_extlen_t * new_len,struct xfs_btree_cur ** pcur)242 xfs_trans_log_finish_refcount_update(
243 struct xfs_trans *tp,
244 struct xfs_cud_log_item *cudp,
245 enum xfs_refcount_intent_type type,
246 xfs_fsblock_t startblock,
247 xfs_extlen_t blockcount,
248 xfs_fsblock_t *new_fsb,
249 xfs_extlen_t *new_len,
250 struct xfs_btree_cur **pcur)
251 {
252 int error;
253
254 error = xfs_refcount_finish_one(tp, type, startblock,
255 blockcount, new_fsb, new_len, pcur);
256
257 /*
258 * Mark the transaction dirty, even on error. This ensures the
259 * transaction is aborted, which:
260 *
261 * 1.) releases the CUI and frees the CUD
262 * 2.) shuts down the filesystem
263 */
264 tp->t_flags |= XFS_TRANS_DIRTY;
265 set_bit(XFS_LI_DIRTY, &cudp->cud_item.li_flags);
266
267 return error;
268 }
269
270 /* Sort refcount intents by AG. */
271 static int
xfs_refcount_update_diff_items(void * priv,const struct list_head * a,const struct list_head * b)272 xfs_refcount_update_diff_items(
273 void *priv,
274 const struct list_head *a,
275 const struct list_head *b)
276 {
277 struct xfs_mount *mp = priv;
278 struct xfs_refcount_intent *ra;
279 struct xfs_refcount_intent *rb;
280
281 ra = container_of(a, struct xfs_refcount_intent, ri_list);
282 rb = container_of(b, struct xfs_refcount_intent, ri_list);
283 return XFS_FSB_TO_AGNO(mp, ra->ri_startblock) -
284 XFS_FSB_TO_AGNO(mp, rb->ri_startblock);
285 }
286
287 /* Set the phys extent flags for this reverse mapping. */
288 static void
xfs_trans_set_refcount_flags(struct xfs_phys_extent * refc,enum xfs_refcount_intent_type type)289 xfs_trans_set_refcount_flags(
290 struct xfs_phys_extent *refc,
291 enum xfs_refcount_intent_type type)
292 {
293 refc->pe_flags = 0;
294 switch (type) {
295 case XFS_REFCOUNT_INCREASE:
296 case XFS_REFCOUNT_DECREASE:
297 case XFS_REFCOUNT_ALLOC_COW:
298 case XFS_REFCOUNT_FREE_COW:
299 refc->pe_flags |= type;
300 break;
301 default:
302 ASSERT(0);
303 }
304 }
305
306 /* Log refcount updates in the intent item. */
307 STATIC void
xfs_refcount_update_log_item(struct xfs_trans * tp,struct xfs_cui_log_item * cuip,struct xfs_refcount_intent * refc)308 xfs_refcount_update_log_item(
309 struct xfs_trans *tp,
310 struct xfs_cui_log_item *cuip,
311 struct xfs_refcount_intent *refc)
312 {
313 uint next_extent;
314 struct xfs_phys_extent *ext;
315
316 tp->t_flags |= XFS_TRANS_DIRTY;
317 set_bit(XFS_LI_DIRTY, &cuip->cui_item.li_flags);
318
319 /*
320 * atomic_inc_return gives us the value after the increment;
321 * we want to use it as an array index so we need to subtract 1 from
322 * it.
323 */
324 next_extent = atomic_inc_return(&cuip->cui_next_extent) - 1;
325 ASSERT(next_extent < cuip->cui_format.cui_nextents);
326 ext = &cuip->cui_format.cui_extents[next_extent];
327 ext->pe_startblock = refc->ri_startblock;
328 ext->pe_len = refc->ri_blockcount;
329 xfs_trans_set_refcount_flags(ext, refc->ri_type);
330 }
331
332 static struct xfs_log_item *
xfs_refcount_update_create_intent(struct xfs_trans * tp,struct list_head * items,unsigned int count,bool sort)333 xfs_refcount_update_create_intent(
334 struct xfs_trans *tp,
335 struct list_head *items,
336 unsigned int count,
337 bool sort)
338 {
339 struct xfs_mount *mp = tp->t_mountp;
340 struct xfs_cui_log_item *cuip = xfs_cui_init(mp, count);
341 struct xfs_refcount_intent *refc;
342
343 ASSERT(count > 0);
344
345 xfs_trans_add_item(tp, &cuip->cui_item);
346 if (sort)
347 list_sort(mp, items, xfs_refcount_update_diff_items);
348 list_for_each_entry(refc, items, ri_list)
349 xfs_refcount_update_log_item(tp, cuip, refc);
350 return &cuip->cui_item;
351 }
352
353 /* Get an CUD so we can process all the deferred refcount updates. */
354 static struct xfs_log_item *
xfs_refcount_update_create_done(struct xfs_trans * tp,struct xfs_log_item * intent,unsigned int count)355 xfs_refcount_update_create_done(
356 struct xfs_trans *tp,
357 struct xfs_log_item *intent,
358 unsigned int count)
359 {
360 return &xfs_trans_get_cud(tp, CUI_ITEM(intent))->cud_item;
361 }
362
363 /* Process a deferred refcount update. */
364 STATIC int
xfs_refcount_update_finish_item(struct xfs_trans * tp,struct xfs_log_item * done,struct list_head * item,struct xfs_btree_cur ** state)365 xfs_refcount_update_finish_item(
366 struct xfs_trans *tp,
367 struct xfs_log_item *done,
368 struct list_head *item,
369 struct xfs_btree_cur **state)
370 {
371 struct xfs_refcount_intent *refc;
372 xfs_fsblock_t new_fsb;
373 xfs_extlen_t new_aglen;
374 int error;
375
376 refc = container_of(item, struct xfs_refcount_intent, ri_list);
377 error = xfs_trans_log_finish_refcount_update(tp, CUD_ITEM(done),
378 refc->ri_type, refc->ri_startblock, refc->ri_blockcount,
379 &new_fsb, &new_aglen, state);
380
381 /* Did we run out of reservation? Requeue what we didn't finish. */
382 if (!error && new_aglen > 0) {
383 ASSERT(refc->ri_type == XFS_REFCOUNT_INCREASE ||
384 refc->ri_type == XFS_REFCOUNT_DECREASE);
385 refc->ri_startblock = new_fsb;
386 refc->ri_blockcount = new_aglen;
387 return -EAGAIN;
388 }
389 kmem_free(refc);
390 return error;
391 }
392
393 /* Abort all pending CUIs. */
394 STATIC void
xfs_refcount_update_abort_intent(struct xfs_log_item * intent)395 xfs_refcount_update_abort_intent(
396 struct xfs_log_item *intent)
397 {
398 xfs_cui_release(CUI_ITEM(intent));
399 }
400
401 /* Cancel a deferred refcount update. */
402 STATIC void
xfs_refcount_update_cancel_item(struct list_head * item)403 xfs_refcount_update_cancel_item(
404 struct list_head *item)
405 {
406 struct xfs_refcount_intent *refc;
407
408 refc = container_of(item, struct xfs_refcount_intent, ri_list);
409 kmem_free(refc);
410 }
411
412 const struct xfs_defer_op_type xfs_refcount_update_defer_type = {
413 .max_items = XFS_CUI_MAX_FAST_EXTENTS,
414 .create_intent = xfs_refcount_update_create_intent,
415 .abort_intent = xfs_refcount_update_abort_intent,
416 .create_done = xfs_refcount_update_create_done,
417 .finish_item = xfs_refcount_update_finish_item,
418 .finish_cleanup = xfs_refcount_finish_one_cleanup,
419 .cancel_item = xfs_refcount_update_cancel_item,
420 };
421
422 /* Is this recovered CUI ok? */
423 static inline bool
xfs_cui_validate_phys(struct xfs_mount * mp,struct xfs_phys_extent * refc)424 xfs_cui_validate_phys(
425 struct xfs_mount *mp,
426 struct xfs_phys_extent *refc)
427 {
428 if (!xfs_has_reflink(mp))
429 return false;
430
431 if (refc->pe_flags & ~XFS_REFCOUNT_EXTENT_FLAGS)
432 return false;
433
434 switch (refc->pe_flags & XFS_REFCOUNT_EXTENT_TYPE_MASK) {
435 case XFS_REFCOUNT_INCREASE:
436 case XFS_REFCOUNT_DECREASE:
437 case XFS_REFCOUNT_ALLOC_COW:
438 case XFS_REFCOUNT_FREE_COW:
439 break;
440 default:
441 return false;
442 }
443
444 return xfs_verify_fsbext(mp, refc->pe_startblock, refc->pe_len);
445 }
446
447 /*
448 * Process a refcount update intent item that was recovered from the log.
449 * We need to update the refcountbt.
450 */
451 STATIC int
xfs_cui_item_recover(struct xfs_log_item * lip,struct list_head * capture_list)452 xfs_cui_item_recover(
453 struct xfs_log_item *lip,
454 struct list_head *capture_list)
455 {
456 struct xfs_bmbt_irec irec;
457 struct xfs_cui_log_item *cuip = CUI_ITEM(lip);
458 struct xfs_phys_extent *refc;
459 struct xfs_cud_log_item *cudp;
460 struct xfs_trans *tp;
461 struct xfs_btree_cur *rcur = NULL;
462 struct xfs_mount *mp = lip->li_mountp;
463 xfs_fsblock_t new_fsb;
464 xfs_extlen_t new_len;
465 unsigned int refc_type;
466 bool requeue_only = false;
467 enum xfs_refcount_intent_type type;
468 int i;
469 int error = 0;
470
471 /*
472 * First check the validity of the extents described by the
473 * CUI. If any are bad, then assume that all are bad and
474 * just toss the CUI.
475 */
476 for (i = 0; i < cuip->cui_format.cui_nextents; i++) {
477 if (!xfs_cui_validate_phys(mp,
478 &cuip->cui_format.cui_extents[i])) {
479 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
480 &cuip->cui_format,
481 sizeof(cuip->cui_format));
482 return -EFSCORRUPTED;
483 }
484 }
485
486 /*
487 * Under normal operation, refcount updates are deferred, so we
488 * wouldn't be adding them directly to a transaction. All
489 * refcount updates manage reservation usage internally and
490 * dynamically by deferring work that won't fit in the
491 * transaction. Normally, any work that needs to be deferred
492 * gets attached to the same defer_ops that scheduled the
493 * refcount update. However, we're in log recovery here, so we
494 * use the passed in defer_ops and to finish up any work that
495 * doesn't fit. We need to reserve enough blocks to handle a
496 * full btree split on either end of the refcount range.
497 */
498 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
499 mp->m_refc_maxlevels * 2, 0, XFS_TRANS_RESERVE, &tp);
500 if (error)
501 return error;
502
503 cudp = xfs_trans_get_cud(tp, cuip);
504
505 for (i = 0; i < cuip->cui_format.cui_nextents; i++) {
506 refc = &cuip->cui_format.cui_extents[i];
507 refc_type = refc->pe_flags & XFS_REFCOUNT_EXTENT_TYPE_MASK;
508 switch (refc_type) {
509 case XFS_REFCOUNT_INCREASE:
510 case XFS_REFCOUNT_DECREASE:
511 case XFS_REFCOUNT_ALLOC_COW:
512 case XFS_REFCOUNT_FREE_COW:
513 type = refc_type;
514 break;
515 default:
516 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
517 error = -EFSCORRUPTED;
518 goto abort_error;
519 }
520 if (requeue_only) {
521 new_fsb = refc->pe_startblock;
522 new_len = refc->pe_len;
523 } else
524 error = xfs_trans_log_finish_refcount_update(tp, cudp,
525 type, refc->pe_startblock, refc->pe_len,
526 &new_fsb, &new_len, &rcur);
527 if (error == -EFSCORRUPTED)
528 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
529 refc, sizeof(*refc));
530 if (error)
531 goto abort_error;
532
533 /* Requeue what we didn't finish. */
534 if (new_len > 0) {
535 irec.br_startblock = new_fsb;
536 irec.br_blockcount = new_len;
537 switch (type) {
538 case XFS_REFCOUNT_INCREASE:
539 xfs_refcount_increase_extent(tp, &irec);
540 break;
541 case XFS_REFCOUNT_DECREASE:
542 xfs_refcount_decrease_extent(tp, &irec);
543 break;
544 case XFS_REFCOUNT_ALLOC_COW:
545 xfs_refcount_alloc_cow_extent(tp,
546 irec.br_startblock,
547 irec.br_blockcount);
548 break;
549 case XFS_REFCOUNT_FREE_COW:
550 xfs_refcount_free_cow_extent(tp,
551 irec.br_startblock,
552 irec.br_blockcount);
553 break;
554 default:
555 ASSERT(0);
556 }
557 requeue_only = true;
558 }
559 }
560
561 xfs_refcount_finish_one_cleanup(tp, rcur, error);
562 return xfs_defer_ops_capture_and_commit(tp, NULL, capture_list);
563
564 abort_error:
565 xfs_refcount_finish_one_cleanup(tp, rcur, error);
566 xfs_trans_cancel(tp);
567 return error;
568 }
569
570 STATIC bool
xfs_cui_item_match(struct xfs_log_item * lip,uint64_t intent_id)571 xfs_cui_item_match(
572 struct xfs_log_item *lip,
573 uint64_t intent_id)
574 {
575 return CUI_ITEM(lip)->cui_format.cui_id == intent_id;
576 }
577
578 /* Relog an intent item to push the log tail forward. */
579 static struct xfs_log_item *
xfs_cui_item_relog(struct xfs_log_item * intent,struct xfs_trans * tp)580 xfs_cui_item_relog(
581 struct xfs_log_item *intent,
582 struct xfs_trans *tp)
583 {
584 struct xfs_cud_log_item *cudp;
585 struct xfs_cui_log_item *cuip;
586 struct xfs_phys_extent *extp;
587 unsigned int count;
588
589 count = CUI_ITEM(intent)->cui_format.cui_nextents;
590 extp = CUI_ITEM(intent)->cui_format.cui_extents;
591
592 tp->t_flags |= XFS_TRANS_DIRTY;
593 cudp = xfs_trans_get_cud(tp, CUI_ITEM(intent));
594 set_bit(XFS_LI_DIRTY, &cudp->cud_item.li_flags);
595
596 cuip = xfs_cui_init(tp->t_mountp, count);
597 memcpy(cuip->cui_format.cui_extents, extp, count * sizeof(*extp));
598 atomic_set(&cuip->cui_next_extent, count);
599 xfs_trans_add_item(tp, &cuip->cui_item);
600 set_bit(XFS_LI_DIRTY, &cuip->cui_item.li_flags);
601 return &cuip->cui_item;
602 }
603
604 static const struct xfs_item_ops xfs_cui_item_ops = {
605 .iop_size = xfs_cui_item_size,
606 .iop_format = xfs_cui_item_format,
607 .iop_unpin = xfs_cui_item_unpin,
608 .iop_release = xfs_cui_item_release,
609 .iop_recover = xfs_cui_item_recover,
610 .iop_match = xfs_cui_item_match,
611 .iop_relog = xfs_cui_item_relog,
612 };
613
614 /*
615 * Copy an CUI format buffer from the given buf, and into the destination
616 * CUI format structure. The CUI/CUD items were designed not to need any
617 * special alignment handling.
618 */
619 static int
xfs_cui_copy_format(struct xfs_log_iovec * buf,struct xfs_cui_log_format * dst_cui_fmt)620 xfs_cui_copy_format(
621 struct xfs_log_iovec *buf,
622 struct xfs_cui_log_format *dst_cui_fmt)
623 {
624 struct xfs_cui_log_format *src_cui_fmt;
625 uint len;
626
627 src_cui_fmt = buf->i_addr;
628 len = xfs_cui_log_format_sizeof(src_cui_fmt->cui_nextents);
629
630 if (buf->i_len == len) {
631 memcpy(dst_cui_fmt, src_cui_fmt, len);
632 return 0;
633 }
634 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
635 return -EFSCORRUPTED;
636 }
637
638 /*
639 * This routine is called to create an in-core extent refcount update
640 * item from the cui format structure which was logged on disk.
641 * It allocates an in-core cui, copies the extents from the format
642 * structure into it, and adds the cui to the AIL with the given
643 * LSN.
644 */
645 STATIC int
xlog_recover_cui_commit_pass2(struct xlog * log,struct list_head * buffer_list,struct xlog_recover_item * item,xfs_lsn_t lsn)646 xlog_recover_cui_commit_pass2(
647 struct xlog *log,
648 struct list_head *buffer_list,
649 struct xlog_recover_item *item,
650 xfs_lsn_t lsn)
651 {
652 int error;
653 struct xfs_mount *mp = log->l_mp;
654 struct xfs_cui_log_item *cuip;
655 struct xfs_cui_log_format *cui_formatp;
656
657 cui_formatp = item->ri_buf[0].i_addr;
658
659 cuip = xfs_cui_init(mp, cui_formatp->cui_nextents);
660 error = xfs_cui_copy_format(&item->ri_buf[0], &cuip->cui_format);
661 if (error) {
662 xfs_cui_item_free(cuip);
663 return error;
664 }
665 atomic_set(&cuip->cui_next_extent, cui_formatp->cui_nextents);
666 /*
667 * Insert the intent into the AIL directly and drop one reference so
668 * that finishing or canceling the work will drop the other.
669 */
670 xfs_trans_ail_insert(log->l_ailp, &cuip->cui_item, lsn);
671 xfs_cui_release(cuip);
672 return 0;
673 }
674
675 const struct xlog_recover_item_ops xlog_cui_item_ops = {
676 .item_type = XFS_LI_CUI,
677 .commit_pass2 = xlog_recover_cui_commit_pass2,
678 };
679
680 /*
681 * This routine is called when an CUD format structure is found in a committed
682 * transaction in the log. Its purpose is to cancel the corresponding CUI if it
683 * was still in the log. To do this it searches the AIL for the CUI with an id
684 * equal to that in the CUD format structure. If we find it we drop the CUD
685 * reference, which removes the CUI from the AIL and frees it.
686 */
687 STATIC int
xlog_recover_cud_commit_pass2(struct xlog * log,struct list_head * buffer_list,struct xlog_recover_item * item,xfs_lsn_t lsn)688 xlog_recover_cud_commit_pass2(
689 struct xlog *log,
690 struct list_head *buffer_list,
691 struct xlog_recover_item *item,
692 xfs_lsn_t lsn)
693 {
694 struct xfs_cud_log_format *cud_formatp;
695
696 cud_formatp = item->ri_buf[0].i_addr;
697 if (item->ri_buf[0].i_len != sizeof(struct xfs_cud_log_format)) {
698 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, log->l_mp);
699 return -EFSCORRUPTED;
700 }
701
702 xlog_recover_release_intent(log, XFS_LI_CUI, cud_formatp->cud_cui_id);
703 return 0;
704 }
705
706 const struct xlog_recover_item_ops xlog_cud_item_ops = {
707 .item_type = XFS_LI_CUD,
708 .commit_pass2 = xlog_recover_cud_commit_pass2,
709 };
710