xfs_bmap_item.c 13 KB

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