xfs_bmap_item.c 19 KB

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