xfs_rmap_item.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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_buf_item.h"
  18. #include "xfs_rmap_item.h"
  19. #include "xfs_log.h"
  20. #include "xfs_rmap.h"
  21. kmem_zone_t *xfs_rui_zone;
  22. kmem_zone_t *xfs_rud_zone;
  23. static inline struct xfs_rui_log_item *RUI_ITEM(struct xfs_log_item *lip)
  24. {
  25. return container_of(lip, struct xfs_rui_log_item, rui_item);
  26. }
  27. void
  28. xfs_rui_item_free(
  29. struct xfs_rui_log_item *ruip)
  30. {
  31. if (ruip->rui_format.rui_nextents > XFS_RUI_MAX_FAST_EXTENTS)
  32. kmem_free(ruip);
  33. else
  34. kmem_zone_free(xfs_rui_zone, ruip);
  35. }
  36. /*
  37. * Freeing the RUI requires that we remove it from the AIL if it has already
  38. * been placed there. However, the RUI may not yet have been placed in the AIL
  39. * when called by xfs_rui_release() from RUD processing due to the ordering of
  40. * committed vs unpin operations in bulk insert operations. Hence the reference
  41. * count to ensure only the last caller frees the RUI.
  42. */
  43. void
  44. xfs_rui_release(
  45. struct xfs_rui_log_item *ruip)
  46. {
  47. ASSERT(atomic_read(&ruip->rui_refcount) > 0);
  48. if (atomic_dec_and_test(&ruip->rui_refcount)) {
  49. xfs_trans_ail_remove(&ruip->rui_item, SHUTDOWN_LOG_IO_ERROR);
  50. xfs_rui_item_free(ruip);
  51. }
  52. }
  53. STATIC void
  54. xfs_rui_item_size(
  55. struct xfs_log_item *lip,
  56. int *nvecs,
  57. int *nbytes)
  58. {
  59. struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
  60. *nvecs += 1;
  61. *nbytes += xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents);
  62. }
  63. /*
  64. * This is called to fill in the vector of log iovecs for the
  65. * given rui log item. We use only 1 iovec, and we point that
  66. * at the rui_log_format structure embedded in the rui item.
  67. * It is at this point that we assert that all of the extent
  68. * slots in the rui item have been filled.
  69. */
  70. STATIC void
  71. xfs_rui_item_format(
  72. struct xfs_log_item *lip,
  73. struct xfs_log_vec *lv)
  74. {
  75. struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
  76. struct xfs_log_iovec *vecp = NULL;
  77. ASSERT(atomic_read(&ruip->rui_next_extent) ==
  78. ruip->rui_format.rui_nextents);
  79. ruip->rui_format.rui_type = XFS_LI_RUI;
  80. ruip->rui_format.rui_size = 1;
  81. xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUI_FORMAT, &ruip->rui_format,
  82. xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents));
  83. }
  84. /*
  85. * Pinning has no meaning for an rui item, so just return.
  86. */
  87. STATIC void
  88. xfs_rui_item_pin(
  89. struct xfs_log_item *lip)
  90. {
  91. }
  92. /*
  93. * The unpin operation is the last place an RUI is manipulated in the log. It is
  94. * either inserted in the AIL or aborted in the event of a log I/O error. In
  95. * either case, the RUI transaction has been successfully committed to make it
  96. * this far. Therefore, we expect whoever committed the RUI to either construct
  97. * and commit the RUD or drop the RUD's reference in the event of error. Simply
  98. * drop the log's RUI reference now that the log is done with it.
  99. */
  100. STATIC void
  101. xfs_rui_item_unpin(
  102. struct xfs_log_item *lip,
  103. int remove)
  104. {
  105. struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
  106. xfs_rui_release(ruip);
  107. }
  108. /*
  109. * RUI items have no locking or pushing. However, since RUIs are pulled from
  110. * the AIL when their corresponding RUDs are committed to disk, their situation
  111. * is very similar to being pinned. Return XFS_ITEM_PINNED so that the caller
  112. * will eventually flush the log. This should help in getting the RUI out of
  113. * the AIL.
  114. */
  115. STATIC uint
  116. xfs_rui_item_push(
  117. struct xfs_log_item *lip,
  118. struct list_head *buffer_list)
  119. {
  120. return XFS_ITEM_PINNED;
  121. }
  122. /*
  123. * The RUI has been either committed or aborted if the transaction has been
  124. * cancelled. If the transaction was cancelled, an RUD isn't going to be
  125. * constructed and thus we free the RUI here directly.
  126. */
  127. STATIC void
  128. xfs_rui_item_unlock(
  129. struct xfs_log_item *lip)
  130. {
  131. if (test_bit(XFS_LI_ABORTED, &lip->li_flags))
  132. xfs_rui_release(RUI_ITEM(lip));
  133. }
  134. /*
  135. * The RUI is logged only once and cannot be moved in the log, so simply return
  136. * the lsn at which it's been logged.
  137. */
  138. STATIC xfs_lsn_t
  139. xfs_rui_item_committed(
  140. struct xfs_log_item *lip,
  141. xfs_lsn_t lsn)
  142. {
  143. return lsn;
  144. }
  145. /*
  146. * The RUI dependency tracking op doesn't do squat. It can't because
  147. * it doesn't know where the free extent is coming from. The dependency
  148. * tracking has to be handled by the "enclosing" metadata object. For
  149. * example, for inodes, the inode is locked throughout the extent freeing
  150. * so the dependency should be recorded there.
  151. */
  152. STATIC void
  153. xfs_rui_item_committing(
  154. struct xfs_log_item *lip,
  155. xfs_lsn_t lsn)
  156. {
  157. }
  158. /*
  159. * This is the ops vector shared by all rui log items.
  160. */
  161. static const struct xfs_item_ops xfs_rui_item_ops = {
  162. .iop_size = xfs_rui_item_size,
  163. .iop_format = xfs_rui_item_format,
  164. .iop_pin = xfs_rui_item_pin,
  165. .iop_unpin = xfs_rui_item_unpin,
  166. .iop_unlock = xfs_rui_item_unlock,
  167. .iop_committed = xfs_rui_item_committed,
  168. .iop_push = xfs_rui_item_push,
  169. .iop_committing = xfs_rui_item_committing,
  170. };
  171. /*
  172. * Allocate and initialize an rui item with the given number of extents.
  173. */
  174. struct xfs_rui_log_item *
  175. xfs_rui_init(
  176. struct xfs_mount *mp,
  177. uint nextents)
  178. {
  179. struct xfs_rui_log_item *ruip;
  180. ASSERT(nextents > 0);
  181. if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
  182. ruip = kmem_zalloc(xfs_rui_log_item_sizeof(nextents), KM_SLEEP);
  183. else
  184. ruip = kmem_zone_zalloc(xfs_rui_zone, KM_SLEEP);
  185. xfs_log_item_init(mp, &ruip->rui_item, XFS_LI_RUI, &xfs_rui_item_ops);
  186. ruip->rui_format.rui_nextents = nextents;
  187. ruip->rui_format.rui_id = (uintptr_t)(void *)ruip;
  188. atomic_set(&ruip->rui_next_extent, 0);
  189. atomic_set(&ruip->rui_refcount, 2);
  190. return ruip;
  191. }
  192. /*
  193. * Copy an RUI format buffer from the given buf, and into the destination
  194. * RUI format structure. The RUI/RUD items were designed not to need any
  195. * special alignment handling.
  196. */
  197. int
  198. xfs_rui_copy_format(
  199. struct xfs_log_iovec *buf,
  200. struct xfs_rui_log_format *dst_rui_fmt)
  201. {
  202. struct xfs_rui_log_format *src_rui_fmt;
  203. uint len;
  204. src_rui_fmt = buf->i_addr;
  205. len = xfs_rui_log_format_sizeof(src_rui_fmt->rui_nextents);
  206. if (buf->i_len != len)
  207. return -EFSCORRUPTED;
  208. memcpy(dst_rui_fmt, src_rui_fmt, len);
  209. return 0;
  210. }
  211. static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip)
  212. {
  213. return container_of(lip, struct xfs_rud_log_item, rud_item);
  214. }
  215. STATIC void
  216. xfs_rud_item_size(
  217. struct xfs_log_item *lip,
  218. int *nvecs,
  219. int *nbytes)
  220. {
  221. *nvecs += 1;
  222. *nbytes += sizeof(struct xfs_rud_log_format);
  223. }
  224. /*
  225. * This is called to fill in the vector of log iovecs for the
  226. * given rud log item. We use only 1 iovec, and we point that
  227. * at the rud_log_format structure embedded in the rud item.
  228. * It is at this point that we assert that all of the extent
  229. * slots in the rud item have been filled.
  230. */
  231. STATIC void
  232. xfs_rud_item_format(
  233. struct xfs_log_item *lip,
  234. struct xfs_log_vec *lv)
  235. {
  236. struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
  237. struct xfs_log_iovec *vecp = NULL;
  238. rudp->rud_format.rud_type = XFS_LI_RUD;
  239. rudp->rud_format.rud_size = 1;
  240. xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format,
  241. sizeof(struct xfs_rud_log_format));
  242. }
  243. /*
  244. * Pinning has no meaning for an rud item, so just return.
  245. */
  246. STATIC void
  247. xfs_rud_item_pin(
  248. struct xfs_log_item *lip)
  249. {
  250. }
  251. /*
  252. * Since pinning has no meaning for an rud item, unpinning does
  253. * not either.
  254. */
  255. STATIC void
  256. xfs_rud_item_unpin(
  257. struct xfs_log_item *lip,
  258. int remove)
  259. {
  260. }
  261. /*
  262. * There isn't much you can do to push on an rud item. It is simply stuck
  263. * waiting for the log to be flushed to disk.
  264. */
  265. STATIC uint
  266. xfs_rud_item_push(
  267. struct xfs_log_item *lip,
  268. struct list_head *buffer_list)
  269. {
  270. return XFS_ITEM_PINNED;
  271. }
  272. /*
  273. * The RUD is either committed or aborted if the transaction is cancelled. If
  274. * the transaction is cancelled, drop our reference to the RUI and free the
  275. * RUD.
  276. */
  277. STATIC void
  278. xfs_rud_item_unlock(
  279. struct xfs_log_item *lip)
  280. {
  281. struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
  282. if (test_bit(XFS_LI_ABORTED, &lip->li_flags)) {
  283. xfs_rui_release(rudp->rud_ruip);
  284. kmem_zone_free(xfs_rud_zone, rudp);
  285. }
  286. }
  287. /*
  288. * When the rud item is committed to disk, all we need to do is delete our
  289. * reference to our partner rui item and then free ourselves. Since we're
  290. * freeing ourselves we must return -1 to keep the transaction code from
  291. * further referencing this item.
  292. */
  293. STATIC xfs_lsn_t
  294. xfs_rud_item_committed(
  295. struct xfs_log_item *lip,
  296. xfs_lsn_t lsn)
  297. {
  298. struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
  299. /*
  300. * Drop the RUI reference regardless of whether the RUD has been
  301. * aborted. Once the RUD transaction is constructed, it is the sole
  302. * responsibility of the RUD to release the RUI (even if the RUI is
  303. * aborted due to log I/O error).
  304. */
  305. xfs_rui_release(rudp->rud_ruip);
  306. kmem_zone_free(xfs_rud_zone, rudp);
  307. return (xfs_lsn_t)-1;
  308. }
  309. /*
  310. * The RUD dependency tracking op doesn't do squat. It can't because
  311. * it doesn't know where the free extent is coming from. The dependency
  312. * tracking has to be handled by the "enclosing" metadata object. For
  313. * example, for inodes, the inode is locked throughout the extent freeing
  314. * so the dependency should be recorded there.
  315. */
  316. STATIC void
  317. xfs_rud_item_committing(
  318. struct xfs_log_item *lip,
  319. xfs_lsn_t lsn)
  320. {
  321. }
  322. /*
  323. * This is the ops vector shared by all rud log items.
  324. */
  325. static const struct xfs_item_ops xfs_rud_item_ops = {
  326. .iop_size = xfs_rud_item_size,
  327. .iop_format = xfs_rud_item_format,
  328. .iop_pin = xfs_rud_item_pin,
  329. .iop_unpin = xfs_rud_item_unpin,
  330. .iop_unlock = xfs_rud_item_unlock,
  331. .iop_committed = xfs_rud_item_committed,
  332. .iop_push = xfs_rud_item_push,
  333. .iop_committing = xfs_rud_item_committing,
  334. };
  335. /*
  336. * Allocate and initialize an rud item with the given number of extents.
  337. */
  338. struct xfs_rud_log_item *
  339. xfs_rud_init(
  340. struct xfs_mount *mp,
  341. struct xfs_rui_log_item *ruip)
  342. {
  343. struct xfs_rud_log_item *rudp;
  344. rudp = kmem_zone_zalloc(xfs_rud_zone, KM_SLEEP);
  345. xfs_log_item_init(mp, &rudp->rud_item, XFS_LI_RUD, &xfs_rud_item_ops);
  346. rudp->rud_ruip = ruip;
  347. rudp->rud_format.rud_rui_id = ruip->rui_format.rui_id;
  348. return rudp;
  349. }
  350. /*
  351. * Process an rmap update intent item that was recovered from the log.
  352. * We need to update the rmapbt.
  353. */
  354. int
  355. xfs_rui_recover(
  356. struct xfs_mount *mp,
  357. struct xfs_rui_log_item *ruip)
  358. {
  359. int i;
  360. int error = 0;
  361. struct xfs_map_extent *rmap;
  362. xfs_fsblock_t startblock_fsb;
  363. bool op_ok;
  364. struct xfs_rud_log_item *rudp;
  365. enum xfs_rmap_intent_type type;
  366. int whichfork;
  367. xfs_exntst_t state;
  368. struct xfs_trans *tp;
  369. struct xfs_btree_cur *rcur = NULL;
  370. ASSERT(!test_bit(XFS_RUI_RECOVERED, &ruip->rui_flags));
  371. /*
  372. * First check the validity of the extents described by the
  373. * RUI. If any are bad, then assume that all are bad and
  374. * just toss the RUI.
  375. */
  376. for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
  377. rmap = &ruip->rui_format.rui_extents[i];
  378. startblock_fsb = XFS_BB_TO_FSB(mp,
  379. XFS_FSB_TO_DADDR(mp, rmap->me_startblock));
  380. switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
  381. case XFS_RMAP_EXTENT_MAP:
  382. case XFS_RMAP_EXTENT_MAP_SHARED:
  383. case XFS_RMAP_EXTENT_UNMAP:
  384. case XFS_RMAP_EXTENT_UNMAP_SHARED:
  385. case XFS_RMAP_EXTENT_CONVERT:
  386. case XFS_RMAP_EXTENT_CONVERT_SHARED:
  387. case XFS_RMAP_EXTENT_ALLOC:
  388. case XFS_RMAP_EXTENT_FREE:
  389. op_ok = true;
  390. break;
  391. default:
  392. op_ok = false;
  393. break;
  394. }
  395. if (!op_ok || startblock_fsb == 0 ||
  396. rmap->me_len == 0 ||
  397. startblock_fsb >= mp->m_sb.sb_dblocks ||
  398. rmap->me_len >= mp->m_sb.sb_agblocks ||
  399. (rmap->me_flags & ~XFS_RMAP_EXTENT_FLAGS)) {
  400. /*
  401. * This will pull the RUI from the AIL and
  402. * free the memory associated with it.
  403. */
  404. set_bit(XFS_RUI_RECOVERED, &ruip->rui_flags);
  405. xfs_rui_release(ruip);
  406. return -EIO;
  407. }
  408. }
  409. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
  410. mp->m_rmap_maxlevels, 0, XFS_TRANS_RESERVE, &tp);
  411. if (error)
  412. return error;
  413. rudp = xfs_trans_get_rud(tp, ruip);
  414. for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
  415. rmap = &ruip->rui_format.rui_extents[i];
  416. state = (rmap->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ?
  417. XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
  418. whichfork = (rmap->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ?
  419. XFS_ATTR_FORK : XFS_DATA_FORK;
  420. switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
  421. case XFS_RMAP_EXTENT_MAP:
  422. type = XFS_RMAP_MAP;
  423. break;
  424. case XFS_RMAP_EXTENT_MAP_SHARED:
  425. type = XFS_RMAP_MAP_SHARED;
  426. break;
  427. case XFS_RMAP_EXTENT_UNMAP:
  428. type = XFS_RMAP_UNMAP;
  429. break;
  430. case XFS_RMAP_EXTENT_UNMAP_SHARED:
  431. type = XFS_RMAP_UNMAP_SHARED;
  432. break;
  433. case XFS_RMAP_EXTENT_CONVERT:
  434. type = XFS_RMAP_CONVERT;
  435. break;
  436. case XFS_RMAP_EXTENT_CONVERT_SHARED:
  437. type = XFS_RMAP_CONVERT_SHARED;
  438. break;
  439. case XFS_RMAP_EXTENT_ALLOC:
  440. type = XFS_RMAP_ALLOC;
  441. break;
  442. case XFS_RMAP_EXTENT_FREE:
  443. type = XFS_RMAP_FREE;
  444. break;
  445. default:
  446. error = -EFSCORRUPTED;
  447. goto abort_error;
  448. }
  449. error = xfs_trans_log_finish_rmap_update(tp, rudp, type,
  450. rmap->me_owner, whichfork,
  451. rmap->me_startoff, rmap->me_startblock,
  452. rmap->me_len, state, &rcur);
  453. if (error)
  454. goto abort_error;
  455. }
  456. xfs_rmap_finish_one_cleanup(tp, rcur, error);
  457. set_bit(XFS_RUI_RECOVERED, &ruip->rui_flags);
  458. error = xfs_trans_commit(tp);
  459. return error;
  460. abort_error:
  461. xfs_rmap_finish_one_cleanup(tp, rcur, error);
  462. xfs_trans_cancel(tp);
  463. return error;
  464. }