xfs_refcount_item.c 19 KB

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