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