xfs_rmap_item.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2016 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <darrick.wong@oracle.com>
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_format.h"
  9. #include "xfs_log_format.h"
  10. #include "xfs_trans_resv.h"
  11. #include "xfs_bit.h"
  12. #include "xfs_shared.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_defer.h"
  15. #include "xfs_trans.h"
  16. #include "xfs_trans_priv.h"
  17. #include "xfs_rmap_item.h"
  18. #include "xfs_log.h"
  19. #include "xfs_rmap.h"
  20. #include "xfs_error.h"
  21. #include "xfs_log_priv.h"
  22. #include "xfs_log_recover.h"
  23. struct kmem_cache *xfs_rui_cache;
  24. struct kmem_cache *xfs_rud_cache;
  25. static const struct xfs_item_ops xfs_rui_item_ops;
  26. static inline struct xfs_rui_log_item *RUI_ITEM(struct xfs_log_item *lip)
  27. {
  28. return container_of(lip, struct xfs_rui_log_item, rui_item);
  29. }
  30. STATIC void
  31. xfs_rui_item_free(
  32. struct xfs_rui_log_item *ruip)
  33. {
  34. kmem_free(ruip->rui_item.li_lv_shadow);
  35. if (ruip->rui_format.rui_nextents > XFS_RUI_MAX_FAST_EXTENTS)
  36. kmem_free(ruip);
  37. else
  38. kmem_cache_free(xfs_rui_cache, ruip);
  39. }
  40. /*
  41. * Freeing the RUI requires that we remove it from the AIL if it has already
  42. * been placed there. However, the RUI may not yet have been placed in the AIL
  43. * when called by xfs_rui_release() from RUD 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 RUI.
  46. */
  47. STATIC void
  48. xfs_rui_release(
  49. struct xfs_rui_log_item *ruip)
  50. {
  51. ASSERT(atomic_read(&ruip->rui_refcount) > 0);
  52. if (!atomic_dec_and_test(&ruip->rui_refcount))
  53. return;
  54. xfs_trans_ail_delete(&ruip->rui_item, 0);
  55. xfs_rui_item_free(ruip);
  56. }
  57. STATIC void
  58. xfs_rui_item_size(
  59. struct xfs_log_item *lip,
  60. int *nvecs,
  61. int *nbytes)
  62. {
  63. struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
  64. *nvecs += 1;
  65. *nbytes += xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents);
  66. }
  67. /*
  68. * This is called to fill in the vector of log iovecs for the
  69. * given rui log item. We use only 1 iovec, and we point that
  70. * at the rui_log_format structure embedded in the rui item.
  71. * It is at this point that we assert that all of the extent
  72. * slots in the rui item have been filled.
  73. */
  74. STATIC void
  75. xfs_rui_item_format(
  76. struct xfs_log_item *lip,
  77. struct xfs_log_vec *lv)
  78. {
  79. struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
  80. struct xfs_log_iovec *vecp = NULL;
  81. ASSERT(atomic_read(&ruip->rui_next_extent) ==
  82. ruip->rui_format.rui_nextents);
  83. ruip->rui_format.rui_type = XFS_LI_RUI;
  84. ruip->rui_format.rui_size = 1;
  85. xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUI_FORMAT, &ruip->rui_format,
  86. xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents));
  87. }
  88. /*
  89. * The unpin operation is the last place an RUI 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 RUI transaction has been successfully committed to make it
  92. * this far. Therefore, we expect whoever committed the RUI to either construct
  93. * and commit the RUD or drop the RUD's reference in the event of error. Simply
  94. * drop the log's RUI reference now that the log is done with it.
  95. */
  96. STATIC void
  97. xfs_rui_item_unpin(
  98. struct xfs_log_item *lip,
  99. int remove)
  100. {
  101. struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
  102. xfs_rui_release(ruip);
  103. }
  104. /*
  105. * The RUI has been either committed or aborted if the transaction has been
  106. * cancelled. If the transaction was cancelled, an RUD isn't going to be
  107. * constructed and thus we free the RUI here directly.
  108. */
  109. STATIC void
  110. xfs_rui_item_release(
  111. struct xfs_log_item *lip)
  112. {
  113. xfs_rui_release(RUI_ITEM(lip));
  114. }
  115. /*
  116. * Allocate and initialize an rui item with the given number of extents.
  117. */
  118. STATIC struct xfs_rui_log_item *
  119. xfs_rui_init(
  120. struct xfs_mount *mp,
  121. uint nextents)
  122. {
  123. struct xfs_rui_log_item *ruip;
  124. ASSERT(nextents > 0);
  125. if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
  126. ruip = kmem_zalloc(xfs_rui_log_item_sizeof(nextents), 0);
  127. else
  128. ruip = kmem_cache_zalloc(xfs_rui_cache,
  129. GFP_KERNEL | __GFP_NOFAIL);
  130. xfs_log_item_init(mp, &ruip->rui_item, XFS_LI_RUI, &xfs_rui_item_ops);
  131. ruip->rui_format.rui_nextents = nextents;
  132. ruip->rui_format.rui_id = (uintptr_t)(void *)ruip;
  133. atomic_set(&ruip->rui_next_extent, 0);
  134. atomic_set(&ruip->rui_refcount, 2);
  135. return ruip;
  136. }
  137. static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip)
  138. {
  139. return container_of(lip, struct xfs_rud_log_item, rud_item);
  140. }
  141. STATIC void
  142. xfs_rud_item_size(
  143. struct xfs_log_item *lip,
  144. int *nvecs,
  145. int *nbytes)
  146. {
  147. *nvecs += 1;
  148. *nbytes += sizeof(struct xfs_rud_log_format);
  149. }
  150. /*
  151. * This is called to fill in the vector of log iovecs for the
  152. * given rud log item. We use only 1 iovec, and we point that
  153. * at the rud_log_format structure embedded in the rud item.
  154. * It is at this point that we assert that all of the extent
  155. * slots in the rud item have been filled.
  156. */
  157. STATIC void
  158. xfs_rud_item_format(
  159. struct xfs_log_item *lip,
  160. struct xfs_log_vec *lv)
  161. {
  162. struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
  163. struct xfs_log_iovec *vecp = NULL;
  164. rudp->rud_format.rud_type = XFS_LI_RUD;
  165. rudp->rud_format.rud_size = 1;
  166. xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format,
  167. sizeof(struct xfs_rud_log_format));
  168. }
  169. /*
  170. * The RUD is either committed or aborted if the transaction is cancelled. If
  171. * the transaction is cancelled, drop our reference to the RUI and free the
  172. * RUD.
  173. */
  174. STATIC void
  175. xfs_rud_item_release(
  176. struct xfs_log_item *lip)
  177. {
  178. struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
  179. xfs_rui_release(rudp->rud_ruip);
  180. kmem_free(rudp->rud_item.li_lv_shadow);
  181. kmem_cache_free(xfs_rud_cache, rudp);
  182. }
  183. static struct xfs_log_item *
  184. xfs_rud_item_intent(
  185. struct xfs_log_item *lip)
  186. {
  187. return &RUD_ITEM(lip)->rud_ruip->rui_item;
  188. }
  189. static const struct xfs_item_ops xfs_rud_item_ops = {
  190. .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED |
  191. XFS_ITEM_INTENT_DONE,
  192. .iop_size = xfs_rud_item_size,
  193. .iop_format = xfs_rud_item_format,
  194. .iop_release = xfs_rud_item_release,
  195. .iop_intent = xfs_rud_item_intent,
  196. };
  197. static struct xfs_rud_log_item *
  198. xfs_trans_get_rud(
  199. struct xfs_trans *tp,
  200. struct xfs_rui_log_item *ruip)
  201. {
  202. struct xfs_rud_log_item *rudp;
  203. rudp = kmem_cache_zalloc(xfs_rud_cache, GFP_KERNEL | __GFP_NOFAIL);
  204. xfs_log_item_init(tp->t_mountp, &rudp->rud_item, XFS_LI_RUD,
  205. &xfs_rud_item_ops);
  206. rudp->rud_ruip = ruip;
  207. rudp->rud_format.rud_rui_id = ruip->rui_format.rui_id;
  208. xfs_trans_add_item(tp, &rudp->rud_item);
  209. return rudp;
  210. }
  211. /* Set the map extent flags for this reverse mapping. */
  212. static void
  213. xfs_trans_set_rmap_flags(
  214. struct xfs_map_extent *rmap,
  215. enum xfs_rmap_intent_type type,
  216. int whichfork,
  217. xfs_exntst_t state)
  218. {
  219. rmap->me_flags = 0;
  220. if (state == XFS_EXT_UNWRITTEN)
  221. rmap->me_flags |= XFS_RMAP_EXTENT_UNWRITTEN;
  222. if (whichfork == XFS_ATTR_FORK)
  223. rmap->me_flags |= XFS_RMAP_EXTENT_ATTR_FORK;
  224. switch (type) {
  225. case XFS_RMAP_MAP:
  226. rmap->me_flags |= XFS_RMAP_EXTENT_MAP;
  227. break;
  228. case XFS_RMAP_MAP_SHARED:
  229. rmap->me_flags |= XFS_RMAP_EXTENT_MAP_SHARED;
  230. break;
  231. case XFS_RMAP_UNMAP:
  232. rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP;
  233. break;
  234. case XFS_RMAP_UNMAP_SHARED:
  235. rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP_SHARED;
  236. break;
  237. case XFS_RMAP_CONVERT:
  238. rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT;
  239. break;
  240. case XFS_RMAP_CONVERT_SHARED:
  241. rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT_SHARED;
  242. break;
  243. case XFS_RMAP_ALLOC:
  244. rmap->me_flags |= XFS_RMAP_EXTENT_ALLOC;
  245. break;
  246. case XFS_RMAP_FREE:
  247. rmap->me_flags |= XFS_RMAP_EXTENT_FREE;
  248. break;
  249. default:
  250. ASSERT(0);
  251. }
  252. }
  253. /*
  254. * Finish an rmap update and log it to the RUD. Note that the transaction is
  255. * marked dirty regardless of whether the rmap update succeeds or fails to
  256. * support the RUI/RUD lifecycle rules.
  257. */
  258. static int
  259. xfs_trans_log_finish_rmap_update(
  260. struct xfs_trans *tp,
  261. struct xfs_rud_log_item *rudp,
  262. enum xfs_rmap_intent_type type,
  263. uint64_t owner,
  264. int whichfork,
  265. xfs_fileoff_t startoff,
  266. xfs_fsblock_t startblock,
  267. xfs_filblks_t blockcount,
  268. xfs_exntst_t state,
  269. struct xfs_btree_cur **pcur)
  270. {
  271. int error;
  272. error = xfs_rmap_finish_one(tp, type, owner, whichfork, startoff,
  273. startblock, blockcount, state, pcur);
  274. /*
  275. * Mark the transaction dirty, even on error. This ensures the
  276. * transaction is aborted, which:
  277. *
  278. * 1.) releases the RUI and frees the RUD
  279. * 2.) shuts down the filesystem
  280. */
  281. tp->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE;
  282. set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags);
  283. return error;
  284. }
  285. /* Sort rmap intents by AG. */
  286. static int
  287. xfs_rmap_update_diff_items(
  288. void *priv,
  289. const struct list_head *a,
  290. const struct list_head *b)
  291. {
  292. struct xfs_mount *mp = priv;
  293. struct xfs_rmap_intent *ra;
  294. struct xfs_rmap_intent *rb;
  295. ra = container_of(a, struct xfs_rmap_intent, ri_list);
  296. rb = container_of(b, struct xfs_rmap_intent, ri_list);
  297. return XFS_FSB_TO_AGNO(mp, ra->ri_bmap.br_startblock) -
  298. XFS_FSB_TO_AGNO(mp, rb->ri_bmap.br_startblock);
  299. }
  300. /* Log rmap updates in the intent item. */
  301. STATIC void
  302. xfs_rmap_update_log_item(
  303. struct xfs_trans *tp,
  304. struct xfs_rui_log_item *ruip,
  305. struct xfs_rmap_intent *rmap)
  306. {
  307. uint next_extent;
  308. struct xfs_map_extent *map;
  309. tp->t_flags |= XFS_TRANS_DIRTY;
  310. set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags);
  311. /*
  312. * atomic_inc_return gives us the value after the increment;
  313. * we want to use it as an array index so we need to subtract 1 from
  314. * it.
  315. */
  316. next_extent = atomic_inc_return(&ruip->rui_next_extent) - 1;
  317. ASSERT(next_extent < ruip->rui_format.rui_nextents);
  318. map = &ruip->rui_format.rui_extents[next_extent];
  319. map->me_owner = rmap->ri_owner;
  320. map->me_startblock = rmap->ri_bmap.br_startblock;
  321. map->me_startoff = rmap->ri_bmap.br_startoff;
  322. map->me_len = rmap->ri_bmap.br_blockcount;
  323. xfs_trans_set_rmap_flags(map, rmap->ri_type, rmap->ri_whichfork,
  324. rmap->ri_bmap.br_state);
  325. }
  326. static struct xfs_log_item *
  327. xfs_rmap_update_create_intent(
  328. struct xfs_trans *tp,
  329. struct list_head *items,
  330. unsigned int count,
  331. bool sort)
  332. {
  333. struct xfs_mount *mp = tp->t_mountp;
  334. struct xfs_rui_log_item *ruip = xfs_rui_init(mp, count);
  335. struct xfs_rmap_intent *rmap;
  336. ASSERT(count > 0);
  337. xfs_trans_add_item(tp, &ruip->rui_item);
  338. if (sort)
  339. list_sort(mp, items, xfs_rmap_update_diff_items);
  340. list_for_each_entry(rmap, items, ri_list)
  341. xfs_rmap_update_log_item(tp, ruip, rmap);
  342. return &ruip->rui_item;
  343. }
  344. /* Get an RUD so we can process all the deferred rmap updates. */
  345. static struct xfs_log_item *
  346. xfs_rmap_update_create_done(
  347. struct xfs_trans *tp,
  348. struct xfs_log_item *intent,
  349. unsigned int count)
  350. {
  351. return &xfs_trans_get_rud(tp, RUI_ITEM(intent))->rud_item;
  352. }
  353. /* Process a deferred rmap update. */
  354. STATIC int
  355. xfs_rmap_update_finish_item(
  356. struct xfs_trans *tp,
  357. struct xfs_log_item *done,
  358. struct list_head *item,
  359. struct xfs_btree_cur **state)
  360. {
  361. struct xfs_rmap_intent *rmap;
  362. int error;
  363. rmap = container_of(item, struct xfs_rmap_intent, ri_list);
  364. error = xfs_trans_log_finish_rmap_update(tp, RUD_ITEM(done),
  365. rmap->ri_type, rmap->ri_owner, rmap->ri_whichfork,
  366. rmap->ri_bmap.br_startoff, rmap->ri_bmap.br_startblock,
  367. rmap->ri_bmap.br_blockcount, rmap->ri_bmap.br_state,
  368. state);
  369. kmem_cache_free(xfs_rmap_intent_cache, rmap);
  370. return error;
  371. }
  372. /* Abort all pending RUIs. */
  373. STATIC void
  374. xfs_rmap_update_abort_intent(
  375. struct xfs_log_item *intent)
  376. {
  377. xfs_rui_release(RUI_ITEM(intent));
  378. }
  379. /* Cancel a deferred rmap update. */
  380. STATIC void
  381. xfs_rmap_update_cancel_item(
  382. struct list_head *item)
  383. {
  384. struct xfs_rmap_intent *rmap;
  385. rmap = container_of(item, struct xfs_rmap_intent, ri_list);
  386. kmem_cache_free(xfs_rmap_intent_cache, rmap);
  387. }
  388. const struct xfs_defer_op_type xfs_rmap_update_defer_type = {
  389. .max_items = XFS_RUI_MAX_FAST_EXTENTS,
  390. .create_intent = xfs_rmap_update_create_intent,
  391. .abort_intent = xfs_rmap_update_abort_intent,
  392. .create_done = xfs_rmap_update_create_done,
  393. .finish_item = xfs_rmap_update_finish_item,
  394. .finish_cleanup = xfs_rmap_finish_one_cleanup,
  395. .cancel_item = xfs_rmap_update_cancel_item,
  396. };
  397. /* Is this recovered RUI ok? */
  398. static inline bool
  399. xfs_rui_validate_map(
  400. struct xfs_mount *mp,
  401. struct xfs_map_extent *rmap)
  402. {
  403. if (!xfs_has_rmapbt(mp))
  404. return false;
  405. if (rmap->me_flags & ~XFS_RMAP_EXTENT_FLAGS)
  406. return false;
  407. switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
  408. case XFS_RMAP_EXTENT_MAP:
  409. case XFS_RMAP_EXTENT_MAP_SHARED:
  410. case XFS_RMAP_EXTENT_UNMAP:
  411. case XFS_RMAP_EXTENT_UNMAP_SHARED:
  412. case XFS_RMAP_EXTENT_CONVERT:
  413. case XFS_RMAP_EXTENT_CONVERT_SHARED:
  414. case XFS_RMAP_EXTENT_ALLOC:
  415. case XFS_RMAP_EXTENT_FREE:
  416. break;
  417. default:
  418. return false;
  419. }
  420. if (!XFS_RMAP_NON_INODE_OWNER(rmap->me_owner) &&
  421. !xfs_verify_ino(mp, rmap->me_owner))
  422. return false;
  423. if (!xfs_verify_fileext(mp, rmap->me_startoff, rmap->me_len))
  424. return false;
  425. return xfs_verify_fsbext(mp, rmap->me_startblock, rmap->me_len);
  426. }
  427. /*
  428. * Process an rmap update intent item that was recovered from the log.
  429. * We need to update the rmapbt.
  430. */
  431. STATIC int
  432. xfs_rui_item_recover(
  433. struct xfs_log_item *lip,
  434. struct list_head *capture_list)
  435. {
  436. struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
  437. struct xfs_map_extent *rmap;
  438. struct xfs_rud_log_item *rudp;
  439. struct xfs_trans *tp;
  440. struct xfs_btree_cur *rcur = NULL;
  441. struct xfs_mount *mp = lip->li_log->l_mp;
  442. enum xfs_rmap_intent_type type;
  443. xfs_exntst_t state;
  444. int i;
  445. int whichfork;
  446. int error = 0;
  447. /*
  448. * First check the validity of the extents described by the
  449. * RUI. If any are bad, then assume that all are bad and
  450. * just toss the RUI.
  451. */
  452. for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
  453. if (!xfs_rui_validate_map(mp,
  454. &ruip->rui_format.rui_extents[i])) {
  455. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  456. &ruip->rui_format,
  457. sizeof(ruip->rui_format));
  458. return -EFSCORRUPTED;
  459. }
  460. }
  461. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
  462. mp->m_rmap_maxlevels, 0, XFS_TRANS_RESERVE, &tp);
  463. if (error)
  464. return error;
  465. rudp = xfs_trans_get_rud(tp, ruip);
  466. for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
  467. rmap = &ruip->rui_format.rui_extents[i];
  468. state = (rmap->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ?
  469. XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
  470. whichfork = (rmap->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ?
  471. XFS_ATTR_FORK : XFS_DATA_FORK;
  472. switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
  473. case XFS_RMAP_EXTENT_MAP:
  474. type = XFS_RMAP_MAP;
  475. break;
  476. case XFS_RMAP_EXTENT_MAP_SHARED:
  477. type = XFS_RMAP_MAP_SHARED;
  478. break;
  479. case XFS_RMAP_EXTENT_UNMAP:
  480. type = XFS_RMAP_UNMAP;
  481. break;
  482. case XFS_RMAP_EXTENT_UNMAP_SHARED:
  483. type = XFS_RMAP_UNMAP_SHARED;
  484. break;
  485. case XFS_RMAP_EXTENT_CONVERT:
  486. type = XFS_RMAP_CONVERT;
  487. break;
  488. case XFS_RMAP_EXTENT_CONVERT_SHARED:
  489. type = XFS_RMAP_CONVERT_SHARED;
  490. break;
  491. case XFS_RMAP_EXTENT_ALLOC:
  492. type = XFS_RMAP_ALLOC;
  493. break;
  494. case XFS_RMAP_EXTENT_FREE:
  495. type = XFS_RMAP_FREE;
  496. break;
  497. default:
  498. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  499. &ruip->rui_format,
  500. sizeof(ruip->rui_format));
  501. error = -EFSCORRUPTED;
  502. goto abort_error;
  503. }
  504. error = xfs_trans_log_finish_rmap_update(tp, rudp, type,
  505. rmap->me_owner, whichfork,
  506. rmap->me_startoff, rmap->me_startblock,
  507. rmap->me_len, state, &rcur);
  508. if (error == -EFSCORRUPTED)
  509. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  510. rmap, sizeof(*rmap));
  511. if (error)
  512. goto abort_error;
  513. }
  514. xfs_rmap_finish_one_cleanup(tp, rcur, error);
  515. return xfs_defer_ops_capture_and_commit(tp, capture_list);
  516. abort_error:
  517. xfs_rmap_finish_one_cleanup(tp, rcur, error);
  518. xfs_trans_cancel(tp);
  519. return error;
  520. }
  521. STATIC bool
  522. xfs_rui_item_match(
  523. struct xfs_log_item *lip,
  524. uint64_t intent_id)
  525. {
  526. return RUI_ITEM(lip)->rui_format.rui_id == intent_id;
  527. }
  528. /* Relog an intent item to push the log tail forward. */
  529. static struct xfs_log_item *
  530. xfs_rui_item_relog(
  531. struct xfs_log_item *intent,
  532. struct xfs_trans *tp)
  533. {
  534. struct xfs_rud_log_item *rudp;
  535. struct xfs_rui_log_item *ruip;
  536. struct xfs_map_extent *extp;
  537. unsigned int count;
  538. count = RUI_ITEM(intent)->rui_format.rui_nextents;
  539. extp = RUI_ITEM(intent)->rui_format.rui_extents;
  540. tp->t_flags |= XFS_TRANS_DIRTY;
  541. rudp = xfs_trans_get_rud(tp, RUI_ITEM(intent));
  542. set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags);
  543. ruip = xfs_rui_init(tp->t_mountp, count);
  544. memcpy(ruip->rui_format.rui_extents, extp, count * sizeof(*extp));
  545. atomic_set(&ruip->rui_next_extent, count);
  546. xfs_trans_add_item(tp, &ruip->rui_item);
  547. set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags);
  548. return &ruip->rui_item;
  549. }
  550. static const struct xfs_item_ops xfs_rui_item_ops = {
  551. .flags = XFS_ITEM_INTENT,
  552. .iop_size = xfs_rui_item_size,
  553. .iop_format = xfs_rui_item_format,
  554. .iop_unpin = xfs_rui_item_unpin,
  555. .iop_release = xfs_rui_item_release,
  556. .iop_recover = xfs_rui_item_recover,
  557. .iop_match = xfs_rui_item_match,
  558. .iop_relog = xfs_rui_item_relog,
  559. };
  560. static inline void
  561. xfs_rui_copy_format(
  562. struct xfs_rui_log_format *dst,
  563. const struct xfs_rui_log_format *src)
  564. {
  565. unsigned int i;
  566. memcpy(dst, src, offsetof(struct xfs_rui_log_format, rui_extents));
  567. for (i = 0; i < src->rui_nextents; i++)
  568. memcpy(&dst->rui_extents[i], &src->rui_extents[i],
  569. sizeof(struct xfs_map_extent));
  570. }
  571. /*
  572. * This routine is called to create an in-core extent rmap update
  573. * item from the rui format structure which was logged on disk.
  574. * It allocates an in-core rui, copies the extents from the format
  575. * structure into it, and adds the rui to the AIL with the given
  576. * LSN.
  577. */
  578. STATIC int
  579. xlog_recover_rui_commit_pass2(
  580. struct xlog *log,
  581. struct list_head *buffer_list,
  582. struct xlog_recover_item *item,
  583. xfs_lsn_t lsn)
  584. {
  585. struct xfs_mount *mp = log->l_mp;
  586. struct xfs_rui_log_item *ruip;
  587. struct xfs_rui_log_format *rui_formatp;
  588. size_t len;
  589. rui_formatp = item->ri_buf[0].i_addr;
  590. if (item->ri_buf[0].i_len < xfs_rui_log_format_sizeof(0)) {
  591. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  592. item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
  593. return -EFSCORRUPTED;
  594. }
  595. len = xfs_rui_log_format_sizeof(rui_formatp->rui_nextents);
  596. if (item->ri_buf[0].i_len != len) {
  597. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  598. item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
  599. return -EFSCORRUPTED;
  600. }
  601. ruip = xfs_rui_init(mp, rui_formatp->rui_nextents);
  602. xfs_rui_copy_format(&ruip->rui_format, rui_formatp);
  603. atomic_set(&ruip->rui_next_extent, rui_formatp->rui_nextents);
  604. /*
  605. * Insert the intent into the AIL directly and drop one reference so
  606. * that finishing or canceling the work will drop the other.
  607. */
  608. xfs_trans_ail_insert(log->l_ailp, &ruip->rui_item, lsn);
  609. xfs_rui_release(ruip);
  610. return 0;
  611. }
  612. const struct xlog_recover_item_ops xlog_rui_item_ops = {
  613. .item_type = XFS_LI_RUI,
  614. .commit_pass2 = xlog_recover_rui_commit_pass2,
  615. };
  616. /*
  617. * This routine is called when an RUD format structure is found in a committed
  618. * transaction in the log. Its purpose is to cancel the corresponding RUI if it
  619. * was still in the log. To do this it searches the AIL for the RUI with an id
  620. * equal to that in the RUD format structure. If we find it we drop the RUD
  621. * reference, which removes the RUI from the AIL and frees it.
  622. */
  623. STATIC int
  624. xlog_recover_rud_commit_pass2(
  625. struct xlog *log,
  626. struct list_head *buffer_list,
  627. struct xlog_recover_item *item,
  628. xfs_lsn_t lsn)
  629. {
  630. struct xfs_rud_log_format *rud_formatp;
  631. rud_formatp = item->ri_buf[0].i_addr;
  632. if (item->ri_buf[0].i_len != sizeof(struct xfs_rud_log_format)) {
  633. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
  634. rud_formatp, item->ri_buf[0].i_len);
  635. return -EFSCORRUPTED;
  636. }
  637. xlog_recover_release_intent(log, XFS_LI_RUI, rud_formatp->rud_rui_id);
  638. return 0;
  639. }
  640. const struct xlog_recover_item_ops xlog_rud_item_ops = {
  641. .item_type = XFS_LI_RUD,
  642. .commit_pass2 = xlog_recover_rud_commit_pass2,
  643. };