xfs_bmap_item.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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_mount.h"
  13. #include "xfs_defer.h"
  14. #include "xfs_inode.h"
  15. #include "xfs_trans.h"
  16. #include "xfs_trans_priv.h"
  17. #include "xfs_buf_item.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_trace.h"
  23. #include "xfs_bmap_btree.h"
  24. #include "xfs_trans_space.h"
  25. kmem_zone_t *xfs_bui_zone;
  26. kmem_zone_t *xfs_bud_zone;
  27. static inline struct xfs_bui_log_item *BUI_ITEM(struct xfs_log_item *lip)
  28. {
  29. return container_of(lip, struct xfs_bui_log_item, bui_item);
  30. }
  31. void
  32. xfs_bui_item_free(
  33. struct xfs_bui_log_item *buip)
  34. {
  35. kmem_zone_free(xfs_bui_zone, buip);
  36. }
  37. /*
  38. * Freeing the BUI requires that we remove it from the AIL if it has already
  39. * been placed there. However, the BUI may not yet have been placed in the AIL
  40. * when called by xfs_bui_release() from BUD processing due to the ordering of
  41. * committed vs unpin operations in bulk insert operations. Hence the reference
  42. * count to ensure only the last caller frees the BUI.
  43. */
  44. void
  45. xfs_bui_release(
  46. struct xfs_bui_log_item *buip)
  47. {
  48. ASSERT(atomic_read(&buip->bui_refcount) > 0);
  49. if (atomic_dec_and_test(&buip->bui_refcount)) {
  50. xfs_trans_ail_remove(&buip->bui_item, SHUTDOWN_LOG_IO_ERROR);
  51. xfs_bui_item_free(buip);
  52. }
  53. }
  54. STATIC void
  55. xfs_bui_item_size(
  56. struct xfs_log_item *lip,
  57. int *nvecs,
  58. int *nbytes)
  59. {
  60. struct xfs_bui_log_item *buip = BUI_ITEM(lip);
  61. *nvecs += 1;
  62. *nbytes += xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents);
  63. }
  64. /*
  65. * This is called to fill in the vector of log iovecs for the
  66. * given bui log item. We use only 1 iovec, and we point that
  67. * at the bui_log_format structure embedded in the bui item.
  68. * It is at this point that we assert that all of the extent
  69. * slots in the bui item have been filled.
  70. */
  71. STATIC void
  72. xfs_bui_item_format(
  73. struct xfs_log_item *lip,
  74. struct xfs_log_vec *lv)
  75. {
  76. struct xfs_bui_log_item *buip = BUI_ITEM(lip);
  77. struct xfs_log_iovec *vecp = NULL;
  78. ASSERT(atomic_read(&buip->bui_next_extent) ==
  79. buip->bui_format.bui_nextents);
  80. buip->bui_format.bui_type = XFS_LI_BUI;
  81. buip->bui_format.bui_size = 1;
  82. xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUI_FORMAT, &buip->bui_format,
  83. xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents));
  84. }
  85. /*
  86. * Pinning has no meaning for an bui item, so just return.
  87. */
  88. STATIC void
  89. xfs_bui_item_pin(
  90. struct xfs_log_item *lip)
  91. {
  92. }
  93. /*
  94. * The unpin operation is the last place an BUI is manipulated in the log. It is
  95. * either inserted in the AIL or aborted in the event of a log I/O error. In
  96. * either case, the BUI transaction has been successfully committed to make it
  97. * this far. Therefore, we expect whoever committed the BUI to either construct
  98. * and commit the BUD or drop the BUD's reference in the event of error. Simply
  99. * drop the log's BUI reference now that the log is done with it.
  100. */
  101. STATIC void
  102. xfs_bui_item_unpin(
  103. struct xfs_log_item *lip,
  104. int remove)
  105. {
  106. struct xfs_bui_log_item *buip = BUI_ITEM(lip);
  107. xfs_bui_release(buip);
  108. }
  109. /*
  110. * BUI items have no locking or pushing. However, since BUIs are pulled from
  111. * the AIL when their corresponding BUDs are committed to disk, their situation
  112. * is very similar to being pinned. Return XFS_ITEM_PINNED so that the caller
  113. * will eventually flush the log. This should help in getting the BUI out of
  114. * the AIL.
  115. */
  116. STATIC uint
  117. xfs_bui_item_push(
  118. struct xfs_log_item *lip,
  119. struct list_head *buffer_list)
  120. {
  121. return XFS_ITEM_PINNED;
  122. }
  123. /*
  124. * The BUI has been either committed or aborted if the transaction has been
  125. * cancelled. If the transaction was cancelled, an BUD isn't going to be
  126. * constructed and thus we free the BUI here directly.
  127. */
  128. STATIC void
  129. xfs_bui_item_unlock(
  130. struct xfs_log_item *lip)
  131. {
  132. if (test_bit(XFS_LI_ABORTED, &lip->li_flags))
  133. xfs_bui_release(BUI_ITEM(lip));
  134. }
  135. /*
  136. * The BUI is logged only once and cannot be moved in the log, so simply return
  137. * the lsn at which it's been logged.
  138. */
  139. STATIC xfs_lsn_t
  140. xfs_bui_item_committed(
  141. struct xfs_log_item *lip,
  142. xfs_lsn_t lsn)
  143. {
  144. return lsn;
  145. }
  146. /*
  147. * The BUI dependency tracking op doesn't do squat. It can't because
  148. * it doesn't know where the free extent is coming from. The dependency
  149. * tracking has to be handled by the "enclosing" metadata object. For
  150. * example, for inodes, the inode is locked throughout the extent freeing
  151. * so the dependency should be recorded there.
  152. */
  153. STATIC void
  154. xfs_bui_item_committing(
  155. struct xfs_log_item *lip,
  156. xfs_lsn_t lsn)
  157. {
  158. }
  159. /*
  160. * This is the ops vector shared by all bui log items.
  161. */
  162. static const struct xfs_item_ops xfs_bui_item_ops = {
  163. .iop_size = xfs_bui_item_size,
  164. .iop_format = xfs_bui_item_format,
  165. .iop_pin = xfs_bui_item_pin,
  166. .iop_unpin = xfs_bui_item_unpin,
  167. .iop_unlock = xfs_bui_item_unlock,
  168. .iop_committed = xfs_bui_item_committed,
  169. .iop_push = xfs_bui_item_push,
  170. .iop_committing = xfs_bui_item_committing,
  171. };
  172. /*
  173. * Allocate and initialize an bui item with the given number of extents.
  174. */
  175. struct xfs_bui_log_item *
  176. xfs_bui_init(
  177. struct xfs_mount *mp)
  178. {
  179. struct xfs_bui_log_item *buip;
  180. buip = kmem_zone_zalloc(xfs_bui_zone, KM_SLEEP);
  181. xfs_log_item_init(mp, &buip->bui_item, XFS_LI_BUI, &xfs_bui_item_ops);
  182. buip->bui_format.bui_nextents = XFS_BUI_MAX_FAST_EXTENTS;
  183. buip->bui_format.bui_id = (uintptr_t)(void *)buip;
  184. atomic_set(&buip->bui_next_extent, 0);
  185. atomic_set(&buip->bui_refcount, 2);
  186. return buip;
  187. }
  188. static inline struct xfs_bud_log_item *BUD_ITEM(struct xfs_log_item *lip)
  189. {
  190. return container_of(lip, struct xfs_bud_log_item, bud_item);
  191. }
  192. STATIC void
  193. xfs_bud_item_size(
  194. struct xfs_log_item *lip,
  195. int *nvecs,
  196. int *nbytes)
  197. {
  198. *nvecs += 1;
  199. *nbytes += sizeof(struct xfs_bud_log_format);
  200. }
  201. /*
  202. * This is called to fill in the vector of log iovecs for the
  203. * given bud log item. We use only 1 iovec, and we point that
  204. * at the bud_log_format structure embedded in the bud item.
  205. * It is at this point that we assert that all of the extent
  206. * slots in the bud item have been filled.
  207. */
  208. STATIC void
  209. xfs_bud_item_format(
  210. struct xfs_log_item *lip,
  211. struct xfs_log_vec *lv)
  212. {
  213. struct xfs_bud_log_item *budp = BUD_ITEM(lip);
  214. struct xfs_log_iovec *vecp = NULL;
  215. budp->bud_format.bud_type = XFS_LI_BUD;
  216. budp->bud_format.bud_size = 1;
  217. xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUD_FORMAT, &budp->bud_format,
  218. sizeof(struct xfs_bud_log_format));
  219. }
  220. /*
  221. * Pinning has no meaning for an bud item, so just return.
  222. */
  223. STATIC void
  224. xfs_bud_item_pin(
  225. struct xfs_log_item *lip)
  226. {
  227. }
  228. /*
  229. * Since pinning has no meaning for an bud item, unpinning does
  230. * not either.
  231. */
  232. STATIC void
  233. xfs_bud_item_unpin(
  234. struct xfs_log_item *lip,
  235. int remove)
  236. {
  237. }
  238. /*
  239. * There isn't much you can do to push on an bud item. It is simply stuck
  240. * waiting for the log to be flushed to disk.
  241. */
  242. STATIC uint
  243. xfs_bud_item_push(
  244. struct xfs_log_item *lip,
  245. struct list_head *buffer_list)
  246. {
  247. return XFS_ITEM_PINNED;
  248. }
  249. /*
  250. * The BUD is either committed or aborted if the transaction is cancelled. If
  251. * the transaction is cancelled, drop our reference to the BUI and free the
  252. * BUD.
  253. */
  254. STATIC void
  255. xfs_bud_item_unlock(
  256. struct xfs_log_item *lip)
  257. {
  258. struct xfs_bud_log_item *budp = BUD_ITEM(lip);
  259. if (test_bit(XFS_LI_ABORTED, &lip->li_flags)) {
  260. xfs_bui_release(budp->bud_buip);
  261. kmem_zone_free(xfs_bud_zone, budp);
  262. }
  263. }
  264. /*
  265. * When the bud item is committed to disk, all we need to do is delete our
  266. * reference to our partner bui item and then free ourselves. Since we're
  267. * freeing ourselves we must return -1 to keep the transaction code from
  268. * further referencing this item.
  269. */
  270. STATIC xfs_lsn_t
  271. xfs_bud_item_committed(
  272. struct xfs_log_item *lip,
  273. xfs_lsn_t lsn)
  274. {
  275. struct xfs_bud_log_item *budp = BUD_ITEM(lip);
  276. /*
  277. * Drop the BUI reference regardless of whether the BUD has been
  278. * aborted. Once the BUD transaction is constructed, it is the sole
  279. * responsibility of the BUD to release the BUI (even if the BUI is
  280. * aborted due to log I/O error).
  281. */
  282. xfs_bui_release(budp->bud_buip);
  283. kmem_zone_free(xfs_bud_zone, budp);
  284. return (xfs_lsn_t)-1;
  285. }
  286. /*
  287. * The BUD dependency tracking op doesn't do squat. It can't because
  288. * it doesn't know where the free extent is coming from. The dependency
  289. * tracking has to be handled by the "enclosing" metadata object. For
  290. * example, for inodes, the inode is locked throughout the extent freeing
  291. * so the dependency should be recorded there.
  292. */
  293. STATIC void
  294. xfs_bud_item_committing(
  295. struct xfs_log_item *lip,
  296. xfs_lsn_t lsn)
  297. {
  298. }
  299. /*
  300. * This is the ops vector shared by all bud log items.
  301. */
  302. static const struct xfs_item_ops xfs_bud_item_ops = {
  303. .iop_size = xfs_bud_item_size,
  304. .iop_format = xfs_bud_item_format,
  305. .iop_pin = xfs_bud_item_pin,
  306. .iop_unpin = xfs_bud_item_unpin,
  307. .iop_unlock = xfs_bud_item_unlock,
  308. .iop_committed = xfs_bud_item_committed,
  309. .iop_push = xfs_bud_item_push,
  310. .iop_committing = xfs_bud_item_committing,
  311. };
  312. /*
  313. * Allocate and initialize an bud item with the given number of extents.
  314. */
  315. struct xfs_bud_log_item *
  316. xfs_bud_init(
  317. struct xfs_mount *mp,
  318. struct xfs_bui_log_item *buip)
  319. {
  320. struct xfs_bud_log_item *budp;
  321. budp = kmem_zone_zalloc(xfs_bud_zone, KM_SLEEP);
  322. xfs_log_item_init(mp, &budp->bud_item, XFS_LI_BUD, &xfs_bud_item_ops);
  323. budp->bud_buip = buip;
  324. budp->bud_format.bud_bui_id = buip->bui_format.bui_id;
  325. return budp;
  326. }
  327. /*
  328. * Process a bmap update intent item that was recovered from the log.
  329. * We need to update some inode's bmbt.
  330. */
  331. int
  332. xfs_bui_recover(
  333. struct xfs_trans *parent_tp,
  334. struct xfs_bui_log_item *buip)
  335. {
  336. int error = 0;
  337. unsigned int bui_type;
  338. struct xfs_map_extent *bmap;
  339. xfs_fsblock_t startblock_fsb;
  340. xfs_fsblock_t inode_fsb;
  341. xfs_filblks_t count;
  342. bool op_ok;
  343. struct xfs_bud_log_item *budp;
  344. enum xfs_bmap_intent_type type;
  345. int whichfork;
  346. xfs_exntst_t state;
  347. struct xfs_trans *tp;
  348. struct xfs_inode *ip = NULL;
  349. struct xfs_bmbt_irec irec;
  350. struct xfs_mount *mp = parent_tp->t_mountp;
  351. ASSERT(!test_bit(XFS_BUI_RECOVERED, &buip->bui_flags));
  352. /* Only one mapping operation per BUI... */
  353. if (buip->bui_format.bui_nextents != XFS_BUI_MAX_FAST_EXTENTS) {
  354. set_bit(XFS_BUI_RECOVERED, &buip->bui_flags);
  355. xfs_bui_release(buip);
  356. return -EIO;
  357. }
  358. /*
  359. * First check the validity of the extent described by the
  360. * BUI. If anything is bad, then toss the BUI.
  361. */
  362. bmap = &buip->bui_format.bui_extents[0];
  363. startblock_fsb = XFS_BB_TO_FSB(mp,
  364. XFS_FSB_TO_DADDR(mp, bmap->me_startblock));
  365. inode_fsb = XFS_BB_TO_FSB(mp, XFS_FSB_TO_DADDR(mp,
  366. XFS_INO_TO_FSB(mp, bmap->me_owner)));
  367. switch (bmap->me_flags & XFS_BMAP_EXTENT_TYPE_MASK) {
  368. case XFS_BMAP_MAP:
  369. case XFS_BMAP_UNMAP:
  370. op_ok = true;
  371. break;
  372. default:
  373. op_ok = false;
  374. break;
  375. }
  376. if (!op_ok || startblock_fsb == 0 ||
  377. bmap->me_len == 0 ||
  378. inode_fsb == 0 ||
  379. startblock_fsb >= mp->m_sb.sb_dblocks ||
  380. bmap->me_len >= mp->m_sb.sb_agblocks ||
  381. inode_fsb >= mp->m_sb.sb_dblocks ||
  382. (bmap->me_flags & ~XFS_BMAP_EXTENT_FLAGS)) {
  383. /*
  384. * This will pull the BUI from the AIL and
  385. * free the memory associated with it.
  386. */
  387. set_bit(XFS_BUI_RECOVERED, &buip->bui_flags);
  388. xfs_bui_release(buip);
  389. return -EIO;
  390. }
  391. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
  392. XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK), 0, 0, &tp);
  393. if (error)
  394. return error;
  395. /*
  396. * Recovery stashes all deferred ops during intent processing and
  397. * finishes them on completion. Transfer current dfops state to this
  398. * transaction and transfer the result back before we return.
  399. */
  400. xfs_defer_move(tp, parent_tp);
  401. budp = xfs_trans_get_bud(tp, buip);
  402. /* Grab the inode. */
  403. error = xfs_iget(mp, tp, bmap->me_owner, 0, XFS_ILOCK_EXCL, &ip);
  404. if (error)
  405. goto err_inode;
  406. if (VFS_I(ip)->i_nlink == 0)
  407. xfs_iflags_set(ip, XFS_IRECOVERY);
  408. /* Process deferred bmap item. */
  409. state = (bmap->me_flags & XFS_BMAP_EXTENT_UNWRITTEN) ?
  410. XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
  411. whichfork = (bmap->me_flags & XFS_BMAP_EXTENT_ATTR_FORK) ?
  412. XFS_ATTR_FORK : XFS_DATA_FORK;
  413. bui_type = bmap->me_flags & XFS_BMAP_EXTENT_TYPE_MASK;
  414. switch (bui_type) {
  415. case XFS_BMAP_MAP:
  416. case XFS_BMAP_UNMAP:
  417. type = bui_type;
  418. break;
  419. default:
  420. error = -EFSCORRUPTED;
  421. goto err_inode;
  422. }
  423. xfs_trans_ijoin(tp, ip, 0);
  424. count = bmap->me_len;
  425. error = xfs_trans_log_finish_bmap_update(tp, budp, type, ip, whichfork,
  426. bmap->me_startoff, bmap->me_startblock, &count, state);
  427. if (error)
  428. goto err_inode;
  429. if (count > 0) {
  430. ASSERT(type == XFS_BMAP_UNMAP);
  431. irec.br_startblock = bmap->me_startblock;
  432. irec.br_blockcount = count;
  433. irec.br_startoff = bmap->me_startoff;
  434. irec.br_state = state;
  435. error = xfs_bmap_unmap_extent(tp, ip, &irec);
  436. if (error)
  437. goto err_inode;
  438. }
  439. set_bit(XFS_BUI_RECOVERED, &buip->bui_flags);
  440. xfs_defer_move(parent_tp, tp);
  441. error = xfs_trans_commit(tp);
  442. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  443. xfs_irele(ip);
  444. return error;
  445. err_inode:
  446. xfs_defer_move(parent_tp, tp);
  447. xfs_trans_cancel(tp);
  448. if (ip) {
  449. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  450. xfs_irele(ip);
  451. }
  452. return error;
  453. }