xfs_bmap_item.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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_inode.h"
  16. #include "xfs_trans.h"
  17. #include "xfs_trans_priv.h"
  18. #include "xfs_bmap_item.h"
  19. #include "xfs_log.h"
  20. #include "xfs_bmap.h"
  21. #include "xfs_icache.h"
  22. #include "xfs_bmap_btree.h"
  23. #include "xfs_trans_space.h"
  24. #include "xfs_error.h"
  25. #include "xfs_log_priv.h"
  26. #include "xfs_log_recover.h"
  27. struct kmem_cache *xfs_bui_cache;
  28. struct kmem_cache *xfs_bud_cache;
  29. static const struct xfs_item_ops xfs_bui_item_ops;
  30. static inline struct xfs_bui_log_item *BUI_ITEM(struct xfs_log_item *lip)
  31. {
  32. return container_of(lip, struct xfs_bui_log_item, bui_item);
  33. }
  34. STATIC void
  35. xfs_bui_item_free(
  36. struct xfs_bui_log_item *buip)
  37. {
  38. kmem_free(buip->bui_item.li_lv_shadow);
  39. kmem_cache_free(xfs_bui_cache, buip);
  40. }
  41. /*
  42. * Freeing the BUI requires that we remove it from the AIL if it has already
  43. * been placed there. However, the BUI may not yet have been placed in the AIL
  44. * when called by xfs_bui_release() from BUD processing due to the ordering of
  45. * committed vs unpin operations in bulk insert operations. Hence the reference
  46. * count to ensure only the last caller frees the BUI.
  47. */
  48. STATIC void
  49. xfs_bui_release(
  50. struct xfs_bui_log_item *buip)
  51. {
  52. ASSERT(atomic_read(&buip->bui_refcount) > 0);
  53. if (!atomic_dec_and_test(&buip->bui_refcount))
  54. return;
  55. xfs_trans_ail_delete(&buip->bui_item, 0);
  56. xfs_bui_item_free(buip);
  57. }
  58. STATIC void
  59. xfs_bui_item_size(
  60. struct xfs_log_item *lip,
  61. int *nvecs,
  62. int *nbytes)
  63. {
  64. struct xfs_bui_log_item *buip = BUI_ITEM(lip);
  65. *nvecs += 1;
  66. *nbytes += xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents);
  67. }
  68. /*
  69. * This is called to fill in the vector of log iovecs for the
  70. * given bui log item. We use only 1 iovec, and we point that
  71. * at the bui_log_format structure embedded in the bui item.
  72. * It is at this point that we assert that all of the extent
  73. * slots in the bui item have been filled.
  74. */
  75. STATIC void
  76. xfs_bui_item_format(
  77. struct xfs_log_item *lip,
  78. struct xfs_log_vec *lv)
  79. {
  80. struct xfs_bui_log_item *buip = BUI_ITEM(lip);
  81. struct xfs_log_iovec *vecp = NULL;
  82. ASSERT(atomic_read(&buip->bui_next_extent) ==
  83. buip->bui_format.bui_nextents);
  84. buip->bui_format.bui_type = XFS_LI_BUI;
  85. buip->bui_format.bui_size = 1;
  86. xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUI_FORMAT, &buip->bui_format,
  87. xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents));
  88. }
  89. /*
  90. * The unpin operation is the last place an BUI is manipulated in the log. It is
  91. * either inserted in the AIL or aborted in the event of a log I/O error. In
  92. * either case, the BUI transaction has been successfully committed to make it
  93. * this far. Therefore, we expect whoever committed the BUI to either construct
  94. * and commit the BUD or drop the BUD's reference in the event of error. Simply
  95. * drop the log's BUI reference now that the log is done with it.
  96. */
  97. STATIC void
  98. xfs_bui_item_unpin(
  99. struct xfs_log_item *lip,
  100. int remove)
  101. {
  102. struct xfs_bui_log_item *buip = BUI_ITEM(lip);
  103. xfs_bui_release(buip);
  104. }
  105. /*
  106. * The BUI has been either committed or aborted if the transaction has been
  107. * cancelled. If the transaction was cancelled, an BUD isn't going to be
  108. * constructed and thus we free the BUI here directly.
  109. */
  110. STATIC void
  111. xfs_bui_item_release(
  112. struct xfs_log_item *lip)
  113. {
  114. xfs_bui_release(BUI_ITEM(lip));
  115. }
  116. /*
  117. * Allocate and initialize an bui item with the given number of extents.
  118. */
  119. STATIC struct xfs_bui_log_item *
  120. xfs_bui_init(
  121. struct xfs_mount *mp)
  122. {
  123. struct xfs_bui_log_item *buip;
  124. buip = kmem_cache_zalloc(xfs_bui_cache, GFP_KERNEL | __GFP_NOFAIL);
  125. xfs_log_item_init(mp, &buip->bui_item, XFS_LI_BUI, &xfs_bui_item_ops);
  126. buip->bui_format.bui_nextents = XFS_BUI_MAX_FAST_EXTENTS;
  127. buip->bui_format.bui_id = (uintptr_t)(void *)buip;
  128. atomic_set(&buip->bui_next_extent, 0);
  129. atomic_set(&buip->bui_refcount, 2);
  130. return buip;
  131. }
  132. static inline struct xfs_bud_log_item *BUD_ITEM(struct xfs_log_item *lip)
  133. {
  134. return container_of(lip, struct xfs_bud_log_item, bud_item);
  135. }
  136. STATIC void
  137. xfs_bud_item_size(
  138. struct xfs_log_item *lip,
  139. int *nvecs,
  140. int *nbytes)
  141. {
  142. *nvecs += 1;
  143. *nbytes += sizeof(struct xfs_bud_log_format);
  144. }
  145. /*
  146. * This is called to fill in the vector of log iovecs for the
  147. * given bud log item. We use only 1 iovec, and we point that
  148. * at the bud_log_format structure embedded in the bud item.
  149. * It is at this point that we assert that all of the extent
  150. * slots in the bud item have been filled.
  151. */
  152. STATIC void
  153. xfs_bud_item_format(
  154. struct xfs_log_item *lip,
  155. struct xfs_log_vec *lv)
  156. {
  157. struct xfs_bud_log_item *budp = BUD_ITEM(lip);
  158. struct xfs_log_iovec *vecp = NULL;
  159. budp->bud_format.bud_type = XFS_LI_BUD;
  160. budp->bud_format.bud_size = 1;
  161. xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUD_FORMAT, &budp->bud_format,
  162. sizeof(struct xfs_bud_log_format));
  163. }
  164. /*
  165. * The BUD is either committed or aborted if the transaction is cancelled. If
  166. * the transaction is cancelled, drop our reference to the BUI and free the
  167. * BUD.
  168. */
  169. STATIC void
  170. xfs_bud_item_release(
  171. struct xfs_log_item *lip)
  172. {
  173. struct xfs_bud_log_item *budp = BUD_ITEM(lip);
  174. xfs_bui_release(budp->bud_buip);
  175. kmem_free(budp->bud_item.li_lv_shadow);
  176. kmem_cache_free(xfs_bud_cache, budp);
  177. }
  178. static struct xfs_log_item *
  179. xfs_bud_item_intent(
  180. struct xfs_log_item *lip)
  181. {
  182. return &BUD_ITEM(lip)->bud_buip->bui_item;
  183. }
  184. static const struct xfs_item_ops xfs_bud_item_ops = {
  185. .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED |
  186. XFS_ITEM_INTENT_DONE,
  187. .iop_size = xfs_bud_item_size,
  188. .iop_format = xfs_bud_item_format,
  189. .iop_release = xfs_bud_item_release,
  190. .iop_intent = xfs_bud_item_intent,
  191. };
  192. static struct xfs_bud_log_item *
  193. xfs_trans_get_bud(
  194. struct xfs_trans *tp,
  195. struct xfs_bui_log_item *buip)
  196. {
  197. struct xfs_bud_log_item *budp;
  198. budp = kmem_cache_zalloc(xfs_bud_cache, GFP_KERNEL | __GFP_NOFAIL);
  199. xfs_log_item_init(tp->t_mountp, &budp->bud_item, XFS_LI_BUD,
  200. &xfs_bud_item_ops);
  201. budp->bud_buip = buip;
  202. budp->bud_format.bud_bui_id = buip->bui_format.bui_id;
  203. xfs_trans_add_item(tp, &budp->bud_item);
  204. return budp;
  205. }
  206. /*
  207. * Finish an bmap update and log it to the BUD. Note that the
  208. * transaction is marked dirty regardless of whether the bmap update
  209. * succeeds or fails to support the BUI/BUD lifecycle rules.
  210. */
  211. static int
  212. xfs_trans_log_finish_bmap_update(
  213. struct xfs_trans *tp,
  214. struct xfs_bud_log_item *budp,
  215. enum xfs_bmap_intent_type type,
  216. struct xfs_inode *ip,
  217. int whichfork,
  218. xfs_fileoff_t startoff,
  219. xfs_fsblock_t startblock,
  220. xfs_filblks_t *blockcount,
  221. xfs_exntst_t state)
  222. {
  223. int error;
  224. error = xfs_bmap_finish_one(tp, ip, type, whichfork, startoff,
  225. startblock, blockcount, state);
  226. /*
  227. * Mark the transaction dirty, even on error. This ensures the
  228. * transaction is aborted, which:
  229. *
  230. * 1.) releases the BUI and frees the BUD
  231. * 2.) shuts down the filesystem
  232. */
  233. tp->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE;
  234. set_bit(XFS_LI_DIRTY, &budp->bud_item.li_flags);
  235. return error;
  236. }
  237. /* Sort bmap intents by inode. */
  238. static int
  239. xfs_bmap_update_diff_items(
  240. void *priv,
  241. const struct list_head *a,
  242. const struct list_head *b)
  243. {
  244. struct xfs_bmap_intent *ba;
  245. struct xfs_bmap_intent *bb;
  246. ba = container_of(a, struct xfs_bmap_intent, bi_list);
  247. bb = container_of(b, struct xfs_bmap_intent, bi_list);
  248. return ba->bi_owner->i_ino - bb->bi_owner->i_ino;
  249. }
  250. /* Set the map extent flags for this mapping. */
  251. static void
  252. xfs_trans_set_bmap_flags(
  253. struct xfs_map_extent *bmap,
  254. enum xfs_bmap_intent_type type,
  255. int whichfork,
  256. xfs_exntst_t state)
  257. {
  258. bmap->me_flags = 0;
  259. switch (type) {
  260. case XFS_BMAP_MAP:
  261. case XFS_BMAP_UNMAP:
  262. bmap->me_flags = type;
  263. break;
  264. default:
  265. ASSERT(0);
  266. }
  267. if (state == XFS_EXT_UNWRITTEN)
  268. bmap->me_flags |= XFS_BMAP_EXTENT_UNWRITTEN;
  269. if (whichfork == XFS_ATTR_FORK)
  270. bmap->me_flags |= XFS_BMAP_EXTENT_ATTR_FORK;
  271. }
  272. /* Log bmap updates in the intent item. */
  273. STATIC void
  274. xfs_bmap_update_log_item(
  275. struct xfs_trans *tp,
  276. struct xfs_bui_log_item *buip,
  277. struct xfs_bmap_intent *bmap)
  278. {
  279. uint next_extent;
  280. struct xfs_map_extent *map;
  281. tp->t_flags |= XFS_TRANS_DIRTY;
  282. set_bit(XFS_LI_DIRTY, &buip->bui_item.li_flags);
  283. /*
  284. * atomic_inc_return gives us the value after the increment;
  285. * we want to use it as an array index so we need to subtract 1 from
  286. * it.
  287. */
  288. next_extent = atomic_inc_return(&buip->bui_next_extent) - 1;
  289. ASSERT(next_extent < buip->bui_format.bui_nextents);
  290. map = &buip->bui_format.bui_extents[next_extent];
  291. map->me_owner = bmap->bi_owner->i_ino;
  292. map->me_startblock = bmap->bi_bmap.br_startblock;
  293. map->me_startoff = bmap->bi_bmap.br_startoff;
  294. map->me_len = bmap->bi_bmap.br_blockcount;
  295. xfs_trans_set_bmap_flags(map, bmap->bi_type, bmap->bi_whichfork,
  296. bmap->bi_bmap.br_state);
  297. }
  298. static struct xfs_log_item *
  299. xfs_bmap_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_bui_log_item *buip = xfs_bui_init(mp);
  307. struct xfs_bmap_intent *bmap;
  308. ASSERT(count == XFS_BUI_MAX_FAST_EXTENTS);
  309. xfs_trans_add_item(tp, &buip->bui_item);
  310. if (sort)
  311. list_sort(mp, items, xfs_bmap_update_diff_items);
  312. list_for_each_entry(bmap, items, bi_list)
  313. xfs_bmap_update_log_item(tp, buip, bmap);
  314. return &buip->bui_item;
  315. }
  316. /* Get an BUD so we can process all the deferred rmap updates. */
  317. static struct xfs_log_item *
  318. xfs_bmap_update_create_done(
  319. struct xfs_trans *tp,
  320. struct xfs_log_item *intent,
  321. unsigned int count)
  322. {
  323. return &xfs_trans_get_bud(tp, BUI_ITEM(intent))->bud_item;
  324. }
  325. /* Process a deferred rmap update. */
  326. STATIC int
  327. xfs_bmap_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_bmap_intent *bmap;
  334. xfs_filblks_t count;
  335. int error;
  336. bmap = container_of(item, struct xfs_bmap_intent, bi_list);
  337. count = bmap->bi_bmap.br_blockcount;
  338. error = xfs_trans_log_finish_bmap_update(tp, BUD_ITEM(done),
  339. bmap->bi_type,
  340. bmap->bi_owner, bmap->bi_whichfork,
  341. bmap->bi_bmap.br_startoff,
  342. bmap->bi_bmap.br_startblock,
  343. &count,
  344. bmap->bi_bmap.br_state);
  345. if (!error && count > 0) {
  346. ASSERT(bmap->bi_type == XFS_BMAP_UNMAP);
  347. bmap->bi_bmap.br_blockcount = count;
  348. return -EAGAIN;
  349. }
  350. kmem_cache_free(xfs_bmap_intent_cache, bmap);
  351. return error;
  352. }
  353. /* Abort all pending BUIs. */
  354. STATIC void
  355. xfs_bmap_update_abort_intent(
  356. struct xfs_log_item *intent)
  357. {
  358. xfs_bui_release(BUI_ITEM(intent));
  359. }
  360. /* Cancel a deferred rmap update. */
  361. STATIC void
  362. xfs_bmap_update_cancel_item(
  363. struct list_head *item)
  364. {
  365. struct xfs_bmap_intent *bmap;
  366. bmap = container_of(item, struct xfs_bmap_intent, bi_list);
  367. kmem_cache_free(xfs_bmap_intent_cache, bmap);
  368. }
  369. const struct xfs_defer_op_type xfs_bmap_update_defer_type = {
  370. .max_items = XFS_BUI_MAX_FAST_EXTENTS,
  371. .create_intent = xfs_bmap_update_create_intent,
  372. .abort_intent = xfs_bmap_update_abort_intent,
  373. .create_done = xfs_bmap_update_create_done,
  374. .finish_item = xfs_bmap_update_finish_item,
  375. .cancel_item = xfs_bmap_update_cancel_item,
  376. };
  377. /* Is this recovered BUI ok? */
  378. static inline bool
  379. xfs_bui_validate(
  380. struct xfs_mount *mp,
  381. struct xfs_bui_log_item *buip)
  382. {
  383. struct xfs_map_extent *bmap;
  384. /* Only one mapping operation per BUI... */
  385. if (buip->bui_format.bui_nextents != XFS_BUI_MAX_FAST_EXTENTS)
  386. return false;
  387. bmap = &buip->bui_format.bui_extents[0];
  388. if (bmap->me_flags & ~XFS_BMAP_EXTENT_FLAGS)
  389. return false;
  390. switch (bmap->me_flags & XFS_BMAP_EXTENT_TYPE_MASK) {
  391. case XFS_BMAP_MAP:
  392. case XFS_BMAP_UNMAP:
  393. break;
  394. default:
  395. return false;
  396. }
  397. if (!xfs_verify_ino(mp, bmap->me_owner))
  398. return false;
  399. if (!xfs_verify_fileext(mp, bmap->me_startoff, bmap->me_len))
  400. return false;
  401. return xfs_verify_fsbext(mp, bmap->me_startblock, bmap->me_len);
  402. }
  403. /*
  404. * Process a bmap update intent item that was recovered from the log.
  405. * We need to update some inode's bmbt.
  406. */
  407. STATIC int
  408. xfs_bui_item_recover(
  409. struct xfs_log_item *lip,
  410. struct list_head *capture_list)
  411. {
  412. struct xfs_bmbt_irec irec;
  413. struct xfs_bui_log_item *buip = BUI_ITEM(lip);
  414. struct xfs_trans *tp;
  415. struct xfs_inode *ip = NULL;
  416. struct xfs_mount *mp = lip->li_log->l_mp;
  417. struct xfs_map_extent *bmap;
  418. struct xfs_bud_log_item *budp;
  419. xfs_filblks_t count;
  420. xfs_exntst_t state;
  421. unsigned int bui_type;
  422. int whichfork;
  423. int iext_delta;
  424. int error = 0;
  425. if (!xfs_bui_validate(mp, buip)) {
  426. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  427. &buip->bui_format, sizeof(buip->bui_format));
  428. return -EFSCORRUPTED;
  429. }
  430. bmap = &buip->bui_format.bui_extents[0];
  431. state = (bmap->me_flags & XFS_BMAP_EXTENT_UNWRITTEN) ?
  432. XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
  433. whichfork = (bmap->me_flags & XFS_BMAP_EXTENT_ATTR_FORK) ?
  434. XFS_ATTR_FORK : XFS_DATA_FORK;
  435. bui_type = bmap->me_flags & XFS_BMAP_EXTENT_TYPE_MASK;
  436. error = xlog_recover_iget(mp, bmap->me_owner, &ip);
  437. if (error)
  438. return error;
  439. /* Allocate transaction and do the work. */
  440. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
  441. XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK), 0, 0, &tp);
  442. if (error)
  443. goto err_rele;
  444. budp = xfs_trans_get_bud(tp, buip);
  445. xfs_ilock(ip, XFS_ILOCK_EXCL);
  446. xfs_trans_ijoin(tp, ip, 0);
  447. if (bui_type == XFS_BMAP_MAP)
  448. iext_delta = XFS_IEXT_ADD_NOSPLIT_CNT;
  449. else
  450. iext_delta = XFS_IEXT_PUNCH_HOLE_CNT;
  451. error = xfs_iext_count_may_overflow(ip, whichfork, iext_delta);
  452. if (error == -EFBIG)
  453. error = xfs_iext_count_upgrade(tp, ip, iext_delta);
  454. if (error)
  455. goto err_cancel;
  456. count = bmap->me_len;
  457. error = xfs_trans_log_finish_bmap_update(tp, budp, bui_type, ip,
  458. whichfork, bmap->me_startoff, bmap->me_startblock,
  459. &count, state);
  460. if (error == -EFSCORRUPTED)
  461. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bmap,
  462. sizeof(*bmap));
  463. if (error)
  464. goto err_cancel;
  465. if (count > 0) {
  466. ASSERT(bui_type == XFS_BMAP_UNMAP);
  467. irec.br_startblock = bmap->me_startblock;
  468. irec.br_blockcount = count;
  469. irec.br_startoff = bmap->me_startoff;
  470. irec.br_state = state;
  471. xfs_bmap_unmap_extent(tp, ip, &irec);
  472. }
  473. /*
  474. * Commit transaction, which frees the transaction and saves the inode
  475. * for later replay activities.
  476. */
  477. error = xfs_defer_ops_capture_and_commit(tp, capture_list);
  478. if (error)
  479. goto err_unlock;
  480. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  481. xfs_irele(ip);
  482. return 0;
  483. err_cancel:
  484. xfs_trans_cancel(tp);
  485. err_unlock:
  486. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  487. err_rele:
  488. xfs_irele(ip);
  489. return error;
  490. }
  491. STATIC bool
  492. xfs_bui_item_match(
  493. struct xfs_log_item *lip,
  494. uint64_t intent_id)
  495. {
  496. return BUI_ITEM(lip)->bui_format.bui_id == intent_id;
  497. }
  498. /* Relog an intent item to push the log tail forward. */
  499. static struct xfs_log_item *
  500. xfs_bui_item_relog(
  501. struct xfs_log_item *intent,
  502. struct xfs_trans *tp)
  503. {
  504. struct xfs_bud_log_item *budp;
  505. struct xfs_bui_log_item *buip;
  506. struct xfs_map_extent *extp;
  507. unsigned int count;
  508. count = BUI_ITEM(intent)->bui_format.bui_nextents;
  509. extp = BUI_ITEM(intent)->bui_format.bui_extents;
  510. tp->t_flags |= XFS_TRANS_DIRTY;
  511. budp = xfs_trans_get_bud(tp, BUI_ITEM(intent));
  512. set_bit(XFS_LI_DIRTY, &budp->bud_item.li_flags);
  513. buip = xfs_bui_init(tp->t_mountp);
  514. memcpy(buip->bui_format.bui_extents, extp, count * sizeof(*extp));
  515. atomic_set(&buip->bui_next_extent, count);
  516. xfs_trans_add_item(tp, &buip->bui_item);
  517. set_bit(XFS_LI_DIRTY, &buip->bui_item.li_flags);
  518. return &buip->bui_item;
  519. }
  520. static const struct xfs_item_ops xfs_bui_item_ops = {
  521. .flags = XFS_ITEM_INTENT,
  522. .iop_size = xfs_bui_item_size,
  523. .iop_format = xfs_bui_item_format,
  524. .iop_unpin = xfs_bui_item_unpin,
  525. .iop_release = xfs_bui_item_release,
  526. .iop_recover = xfs_bui_item_recover,
  527. .iop_match = xfs_bui_item_match,
  528. .iop_relog = xfs_bui_item_relog,
  529. };
  530. static inline void
  531. xfs_bui_copy_format(
  532. struct xfs_bui_log_format *dst,
  533. const struct xfs_bui_log_format *src)
  534. {
  535. unsigned int i;
  536. memcpy(dst, src, offsetof(struct xfs_bui_log_format, bui_extents));
  537. for (i = 0; i < src->bui_nextents; i++)
  538. memcpy(&dst->bui_extents[i], &src->bui_extents[i],
  539. sizeof(struct xfs_map_extent));
  540. }
  541. /*
  542. * This routine is called to create an in-core extent bmap update
  543. * item from the bui format structure which was logged on disk.
  544. * It allocates an in-core bui, copies the extents from the format
  545. * structure into it, and adds the bui to the AIL with the given
  546. * LSN.
  547. */
  548. STATIC int
  549. xlog_recover_bui_commit_pass2(
  550. struct xlog *log,
  551. struct list_head *buffer_list,
  552. struct xlog_recover_item *item,
  553. xfs_lsn_t lsn)
  554. {
  555. struct xfs_mount *mp = log->l_mp;
  556. struct xfs_bui_log_item *buip;
  557. struct xfs_bui_log_format *bui_formatp;
  558. size_t len;
  559. bui_formatp = item->ri_buf[0].i_addr;
  560. if (item->ri_buf[0].i_len < xfs_bui_log_format_sizeof(0)) {
  561. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  562. item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
  563. return -EFSCORRUPTED;
  564. }
  565. if (bui_formatp->bui_nextents != XFS_BUI_MAX_FAST_EXTENTS) {
  566. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  567. item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
  568. return -EFSCORRUPTED;
  569. }
  570. len = xfs_bui_log_format_sizeof(bui_formatp->bui_nextents);
  571. if (item->ri_buf[0].i_len != len) {
  572. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  573. item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
  574. return -EFSCORRUPTED;
  575. }
  576. buip = xfs_bui_init(mp);
  577. xfs_bui_copy_format(&buip->bui_format, bui_formatp);
  578. atomic_set(&buip->bui_next_extent, bui_formatp->bui_nextents);
  579. /*
  580. * Insert the intent into the AIL directly and drop one reference so
  581. * that finishing or canceling the work will drop the other.
  582. */
  583. xfs_trans_ail_insert(log->l_ailp, &buip->bui_item, lsn);
  584. xfs_bui_release(buip);
  585. return 0;
  586. }
  587. const struct xlog_recover_item_ops xlog_bui_item_ops = {
  588. .item_type = XFS_LI_BUI,
  589. .commit_pass2 = xlog_recover_bui_commit_pass2,
  590. };
  591. /*
  592. * This routine is called when an BUD format structure is found in a committed
  593. * transaction in the log. Its purpose is to cancel the corresponding BUI if it
  594. * was still in the log. To do this it searches the AIL for the BUI with an id
  595. * equal to that in the BUD format structure. If we find it we drop the BUD
  596. * reference, which removes the BUI from the AIL and frees it.
  597. */
  598. STATIC int
  599. xlog_recover_bud_commit_pass2(
  600. struct xlog *log,
  601. struct list_head *buffer_list,
  602. struct xlog_recover_item *item,
  603. xfs_lsn_t lsn)
  604. {
  605. struct xfs_bud_log_format *bud_formatp;
  606. bud_formatp = item->ri_buf[0].i_addr;
  607. if (item->ri_buf[0].i_len != sizeof(struct xfs_bud_log_format)) {
  608. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
  609. item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
  610. return -EFSCORRUPTED;
  611. }
  612. xlog_recover_release_intent(log, XFS_LI_BUI, bud_formatp->bud_bui_id);
  613. return 0;
  614. }
  615. const struct xlog_recover_item_ops xlog_bud_item_ops = {
  616. .item_type = XFS_LI_BUD,
  617. .commit_pass2 = xlog_recover_bud_commit_pass2,
  618. };