xfs_refcount_item.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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_trans.h"
  29. #include "xfs_trans_priv.h"
  30. #include "xfs_buf_item.h"
  31. #include "xfs_refcount_item.h"
  32. #include "xfs_log.h"
  33. #include "xfs_refcount.h"
  34. kmem_zone_t *xfs_cui_zone;
  35. kmem_zone_t *xfs_cud_zone;
  36. static inline struct xfs_cui_log_item *CUI_ITEM(struct xfs_log_item *lip)
  37. {
  38. return container_of(lip, struct xfs_cui_log_item, cui_item);
  39. }
  40. void
  41. xfs_cui_item_free(
  42. struct xfs_cui_log_item *cuip)
  43. {
  44. if (cuip->cui_format.cui_nextents > XFS_CUI_MAX_FAST_EXTENTS)
  45. kmem_free(cuip);
  46. else
  47. kmem_zone_free(xfs_cui_zone, cuip);
  48. }
  49. STATIC void
  50. xfs_cui_item_size(
  51. struct xfs_log_item *lip,
  52. int *nvecs,
  53. int *nbytes)
  54. {
  55. struct xfs_cui_log_item *cuip = CUI_ITEM(lip);
  56. *nvecs += 1;
  57. *nbytes += xfs_cui_log_format_sizeof(cuip->cui_format.cui_nextents);
  58. }
  59. /*
  60. * This is called to fill in the vector of log iovecs for the
  61. * given cui log item. We use only 1 iovec, and we point that
  62. * at the cui_log_format structure embedded in the cui item.
  63. * It is at this point that we assert that all of the extent
  64. * slots in the cui item have been filled.
  65. */
  66. STATIC void
  67. xfs_cui_item_format(
  68. struct xfs_log_item *lip,
  69. struct xfs_log_vec *lv)
  70. {
  71. struct xfs_cui_log_item *cuip = CUI_ITEM(lip);
  72. struct xfs_log_iovec *vecp = NULL;
  73. ASSERT(atomic_read(&cuip->cui_next_extent) ==
  74. cuip->cui_format.cui_nextents);
  75. cuip->cui_format.cui_type = XFS_LI_CUI;
  76. cuip->cui_format.cui_size = 1;
  77. xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_CUI_FORMAT, &cuip->cui_format,
  78. xfs_cui_log_format_sizeof(cuip->cui_format.cui_nextents));
  79. }
  80. /*
  81. * Pinning has no meaning for an cui item, so just return.
  82. */
  83. STATIC void
  84. xfs_cui_item_pin(
  85. struct xfs_log_item *lip)
  86. {
  87. }
  88. /*
  89. * The unpin operation is the last place an CUI 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 CUI transaction has been successfully committed to make it
  92. * this far. Therefore, we expect whoever committed the CUI to either construct
  93. * and commit the CUD or drop the CUD's reference in the event of error. Simply
  94. * drop the log's CUI reference now that the log is done with it.
  95. */
  96. STATIC void
  97. xfs_cui_item_unpin(
  98. struct xfs_log_item *lip,
  99. int remove)
  100. {
  101. struct xfs_cui_log_item *cuip = CUI_ITEM(lip);
  102. xfs_cui_release(cuip);
  103. }
  104. /*
  105. * CUI items have no locking or pushing. However, since CUIs are pulled from
  106. * the AIL when their corresponding CUDs are committed to disk, their situation
  107. * is very similar to being pinned. Return XFS_ITEM_PINNED so that the caller
  108. * will eventually flush the log. This should help in getting the CUI out of
  109. * the AIL.
  110. */
  111. STATIC uint
  112. xfs_cui_item_push(
  113. struct xfs_log_item *lip,
  114. struct list_head *buffer_list)
  115. {
  116. return XFS_ITEM_PINNED;
  117. }
  118. /*
  119. * The CUI has been either committed or aborted if the transaction has been
  120. * cancelled. If the transaction was cancelled, an CUD isn't going to be
  121. * constructed and thus we free the CUI here directly.
  122. */
  123. STATIC void
  124. xfs_cui_item_unlock(
  125. struct xfs_log_item *lip)
  126. {
  127. if (lip->li_flags & XFS_LI_ABORTED)
  128. xfs_cui_item_free(CUI_ITEM(lip));
  129. }
  130. /*
  131. * The CUI is logged only once and cannot be moved in the log, so simply return
  132. * the lsn at which it's been logged.
  133. */
  134. STATIC xfs_lsn_t
  135. xfs_cui_item_committed(
  136. struct xfs_log_item *lip,
  137. xfs_lsn_t lsn)
  138. {
  139. return lsn;
  140. }
  141. /*
  142. * The CUI dependency tracking op doesn't do squat. It can't because
  143. * it doesn't know where the free extent is coming from. The dependency
  144. * tracking has to be handled by the "enclosing" metadata object. For
  145. * example, for inodes, the inode is locked throughout the extent freeing
  146. * so the dependency should be recorded there.
  147. */
  148. STATIC void
  149. xfs_cui_item_committing(
  150. struct xfs_log_item *lip,
  151. xfs_lsn_t lsn)
  152. {
  153. }
  154. /*
  155. * This is the ops vector shared by all cui log items.
  156. */
  157. static const struct xfs_item_ops xfs_cui_item_ops = {
  158. .iop_size = xfs_cui_item_size,
  159. .iop_format = xfs_cui_item_format,
  160. .iop_pin = xfs_cui_item_pin,
  161. .iop_unpin = xfs_cui_item_unpin,
  162. .iop_unlock = xfs_cui_item_unlock,
  163. .iop_committed = xfs_cui_item_committed,
  164. .iop_push = xfs_cui_item_push,
  165. .iop_committing = xfs_cui_item_committing,
  166. };
  167. /*
  168. * Allocate and initialize an cui item with the given number of extents.
  169. */
  170. struct xfs_cui_log_item *
  171. xfs_cui_init(
  172. struct xfs_mount *mp,
  173. uint nextents)
  174. {
  175. struct xfs_cui_log_item *cuip;
  176. ASSERT(nextents > 0);
  177. if (nextents > XFS_CUI_MAX_FAST_EXTENTS)
  178. cuip = kmem_zalloc(xfs_cui_log_item_sizeof(nextents),
  179. KM_SLEEP);
  180. else
  181. cuip = kmem_zone_zalloc(xfs_cui_zone, KM_SLEEP);
  182. xfs_log_item_init(mp, &cuip->cui_item, XFS_LI_CUI, &xfs_cui_item_ops);
  183. cuip->cui_format.cui_nextents = nextents;
  184. cuip->cui_format.cui_id = (uintptr_t)(void *)cuip;
  185. atomic_set(&cuip->cui_next_extent, 0);
  186. atomic_set(&cuip->cui_refcount, 2);
  187. return cuip;
  188. }
  189. /*
  190. * Freeing the CUI requires that we remove it from the AIL if it has already
  191. * been placed there. However, the CUI may not yet have been placed in the AIL
  192. * when called by xfs_cui_release() from CUD processing due to the ordering of
  193. * committed vs unpin operations in bulk insert operations. Hence the reference
  194. * count to ensure only the last caller frees the CUI.
  195. */
  196. void
  197. xfs_cui_release(
  198. struct xfs_cui_log_item *cuip)
  199. {
  200. ASSERT(atomic_read(&cuip->cui_refcount) > 0);
  201. if (atomic_dec_and_test(&cuip->cui_refcount)) {
  202. xfs_trans_ail_remove(&cuip->cui_item, SHUTDOWN_LOG_IO_ERROR);
  203. xfs_cui_item_free(cuip);
  204. }
  205. }
  206. static inline struct xfs_cud_log_item *CUD_ITEM(struct xfs_log_item *lip)
  207. {
  208. return container_of(lip, struct xfs_cud_log_item, cud_item);
  209. }
  210. STATIC void
  211. xfs_cud_item_size(
  212. struct xfs_log_item *lip,
  213. int *nvecs,
  214. int *nbytes)
  215. {
  216. *nvecs += 1;
  217. *nbytes += sizeof(struct xfs_cud_log_format);
  218. }
  219. /*
  220. * This is called to fill in the vector of log iovecs for the
  221. * given cud log item. We use only 1 iovec, and we point that
  222. * at the cud_log_format structure embedded in the cud item.
  223. * It is at this point that we assert that all of the extent
  224. * slots in the cud item have been filled.
  225. */
  226. STATIC void
  227. xfs_cud_item_format(
  228. struct xfs_log_item *lip,
  229. struct xfs_log_vec *lv)
  230. {
  231. struct xfs_cud_log_item *cudp = CUD_ITEM(lip);
  232. struct xfs_log_iovec *vecp = NULL;
  233. cudp->cud_format.cud_type = XFS_LI_CUD;
  234. cudp->cud_format.cud_size = 1;
  235. xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_CUD_FORMAT, &cudp->cud_format,
  236. sizeof(struct xfs_cud_log_format));
  237. }
  238. /*
  239. * Pinning has no meaning for an cud item, so just return.
  240. */
  241. STATIC void
  242. xfs_cud_item_pin(
  243. struct xfs_log_item *lip)
  244. {
  245. }
  246. /*
  247. * Since pinning has no meaning for an cud item, unpinning does
  248. * not either.
  249. */
  250. STATIC void
  251. xfs_cud_item_unpin(
  252. struct xfs_log_item *lip,
  253. int remove)
  254. {
  255. }
  256. /*
  257. * There isn't much you can do to push on an cud item. It is simply stuck
  258. * waiting for the log to be flushed to disk.
  259. */
  260. STATIC uint
  261. xfs_cud_item_push(
  262. struct xfs_log_item *lip,
  263. struct list_head *buffer_list)
  264. {
  265. return XFS_ITEM_PINNED;
  266. }
  267. /*
  268. * The CUD is either committed or aborted if the transaction is cancelled. If
  269. * the transaction is cancelled, drop our reference to the CUI and free the
  270. * CUD.
  271. */
  272. STATIC void
  273. xfs_cud_item_unlock(
  274. struct xfs_log_item *lip)
  275. {
  276. struct xfs_cud_log_item *cudp = CUD_ITEM(lip);
  277. if (lip->li_flags & XFS_LI_ABORTED) {
  278. xfs_cui_release(cudp->cud_cuip);
  279. kmem_zone_free(xfs_cud_zone, cudp);
  280. }
  281. }
  282. /*
  283. * When the cud item is committed to disk, all we need to do is delete our
  284. * reference to our partner cui item and then free ourselves. Since we're
  285. * freeing ourselves we must return -1 to keep the transaction code from
  286. * further referencing this item.
  287. */
  288. STATIC xfs_lsn_t
  289. xfs_cud_item_committed(
  290. struct xfs_log_item *lip,
  291. xfs_lsn_t lsn)
  292. {
  293. struct xfs_cud_log_item *cudp = CUD_ITEM(lip);
  294. /*
  295. * Drop the CUI reference regardless of whether the CUD has been
  296. * aborted. Once the CUD transaction is constructed, it is the sole
  297. * responsibility of the CUD to release the CUI (even if the CUI is
  298. * aborted due to log I/O error).
  299. */
  300. xfs_cui_release(cudp->cud_cuip);
  301. kmem_zone_free(xfs_cud_zone, cudp);
  302. return (xfs_lsn_t)-1;
  303. }
  304. /*
  305. * The CUD dependency tracking op doesn't do squat. It can't because
  306. * it doesn't know where the free extent is coming from. The dependency
  307. * tracking has to be handled by the "enclosing" metadata object. For
  308. * example, for inodes, the inode is locked throughout the extent freeing
  309. * so the dependency should be recorded there.
  310. */
  311. STATIC void
  312. xfs_cud_item_committing(
  313. struct xfs_log_item *lip,
  314. xfs_lsn_t lsn)
  315. {
  316. }
  317. /*
  318. * This is the ops vector shared by all cud log items.
  319. */
  320. static const struct xfs_item_ops xfs_cud_item_ops = {
  321. .iop_size = xfs_cud_item_size,
  322. .iop_format = xfs_cud_item_format,
  323. .iop_pin = xfs_cud_item_pin,
  324. .iop_unpin = xfs_cud_item_unpin,
  325. .iop_unlock = xfs_cud_item_unlock,
  326. .iop_committed = xfs_cud_item_committed,
  327. .iop_push = xfs_cud_item_push,
  328. .iop_committing = xfs_cud_item_committing,
  329. };
  330. /*
  331. * Allocate and initialize an cud item with the given number of extents.
  332. */
  333. struct xfs_cud_log_item *
  334. xfs_cud_init(
  335. struct xfs_mount *mp,
  336. struct xfs_cui_log_item *cuip)
  337. {
  338. struct xfs_cud_log_item *cudp;
  339. cudp = kmem_zone_zalloc(xfs_cud_zone, KM_SLEEP);
  340. xfs_log_item_init(mp, &cudp->cud_item, XFS_LI_CUD, &xfs_cud_item_ops);
  341. cudp->cud_cuip = cuip;
  342. cudp->cud_format.cud_cui_id = cuip->cui_format.cui_id;
  343. return cudp;
  344. }
  345. /*
  346. * Process a refcount update intent item that was recovered from the log.
  347. * We need to update the refcountbt.
  348. */
  349. int
  350. xfs_cui_recover(
  351. struct xfs_mount *mp,
  352. struct xfs_cui_log_item *cuip)
  353. {
  354. int i;
  355. int error = 0;
  356. unsigned int refc_type;
  357. struct xfs_phys_extent *refc;
  358. xfs_fsblock_t startblock_fsb;
  359. bool op_ok;
  360. struct xfs_cud_log_item *cudp;
  361. struct xfs_trans *tp;
  362. struct xfs_btree_cur *rcur = NULL;
  363. enum xfs_refcount_intent_type type;
  364. xfs_fsblock_t firstfsb;
  365. xfs_fsblock_t new_fsb;
  366. xfs_extlen_t new_len;
  367. struct xfs_bmbt_irec irec;
  368. struct xfs_defer_ops dfops;
  369. bool requeue_only = false;
  370. ASSERT(!test_bit(XFS_CUI_RECOVERED, &cuip->cui_flags));
  371. /*
  372. * First check the validity of the extents described by the
  373. * CUI. If any are bad, then assume that all are bad and
  374. * just toss the CUI.
  375. */
  376. for (i = 0; i < cuip->cui_format.cui_nextents; i++) {
  377. refc = &cuip->cui_format.cui_extents[i];
  378. startblock_fsb = XFS_BB_TO_FSB(mp,
  379. XFS_FSB_TO_DADDR(mp, refc->pe_startblock));
  380. switch (refc->pe_flags & XFS_REFCOUNT_EXTENT_TYPE_MASK) {
  381. case XFS_REFCOUNT_INCREASE:
  382. case XFS_REFCOUNT_DECREASE:
  383. case XFS_REFCOUNT_ALLOC_COW:
  384. case XFS_REFCOUNT_FREE_COW:
  385. op_ok = true;
  386. break;
  387. default:
  388. op_ok = false;
  389. break;
  390. }
  391. if (!op_ok || startblock_fsb == 0 ||
  392. refc->pe_len == 0 ||
  393. startblock_fsb >= mp->m_sb.sb_dblocks ||
  394. refc->pe_len >= mp->m_sb.sb_agblocks ||
  395. (refc->pe_flags & ~XFS_REFCOUNT_EXTENT_FLAGS)) {
  396. /*
  397. * This will pull the CUI from the AIL and
  398. * free the memory associated with it.
  399. */
  400. set_bit(XFS_CUI_RECOVERED, &cuip->cui_flags);
  401. xfs_cui_release(cuip);
  402. return -EIO;
  403. }
  404. }
  405. /*
  406. * Under normal operation, refcount updates are deferred, so we
  407. * wouldn't be adding them directly to a transaction. All
  408. * refcount updates manage reservation usage internally and
  409. * dynamically by deferring work that won't fit in the
  410. * transaction. Normally, any work that needs to be deferred
  411. * gets attached to the same defer_ops that scheduled the
  412. * refcount update. However, we're in log recovery here, so we
  413. * we create our own defer_ops and use that to finish up any
  414. * work that doesn't fit.
  415. */
  416. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
  417. if (error)
  418. return error;
  419. cudp = xfs_trans_get_cud(tp, cuip);
  420. xfs_defer_init(&dfops, &firstfsb);
  421. for (i = 0; i < cuip->cui_format.cui_nextents; i++) {
  422. refc = &cuip->cui_format.cui_extents[i];
  423. refc_type = refc->pe_flags & XFS_REFCOUNT_EXTENT_TYPE_MASK;
  424. switch (refc_type) {
  425. case XFS_REFCOUNT_INCREASE:
  426. case XFS_REFCOUNT_DECREASE:
  427. case XFS_REFCOUNT_ALLOC_COW:
  428. case XFS_REFCOUNT_FREE_COW:
  429. type = refc_type;
  430. break;
  431. default:
  432. error = -EFSCORRUPTED;
  433. goto abort_error;
  434. }
  435. if (requeue_only) {
  436. new_fsb = refc->pe_startblock;
  437. new_len = refc->pe_len;
  438. } else
  439. error = xfs_trans_log_finish_refcount_update(tp, cudp,
  440. &dfops, type, refc->pe_startblock, refc->pe_len,
  441. &new_fsb, &new_len, &rcur);
  442. if (error)
  443. goto abort_error;
  444. /* Requeue what we didn't finish. */
  445. if (new_len > 0) {
  446. irec.br_startblock = new_fsb;
  447. irec.br_blockcount = new_len;
  448. switch (type) {
  449. case XFS_REFCOUNT_INCREASE:
  450. error = xfs_refcount_increase_extent(
  451. tp->t_mountp, &dfops, &irec);
  452. break;
  453. case XFS_REFCOUNT_DECREASE:
  454. error = xfs_refcount_decrease_extent(
  455. tp->t_mountp, &dfops, &irec);
  456. break;
  457. case XFS_REFCOUNT_ALLOC_COW:
  458. error = xfs_refcount_alloc_cow_extent(
  459. tp->t_mountp, &dfops,
  460. irec.br_startblock,
  461. irec.br_blockcount);
  462. break;
  463. case XFS_REFCOUNT_FREE_COW:
  464. error = xfs_refcount_free_cow_extent(
  465. tp->t_mountp, &dfops,
  466. irec.br_startblock,
  467. irec.br_blockcount);
  468. break;
  469. default:
  470. ASSERT(0);
  471. }
  472. if (error)
  473. goto abort_error;
  474. requeue_only = true;
  475. }
  476. }
  477. xfs_refcount_finish_one_cleanup(tp, rcur, error);
  478. error = xfs_defer_finish(&tp, &dfops);
  479. if (error)
  480. goto abort_defer;
  481. set_bit(XFS_CUI_RECOVERED, &cuip->cui_flags);
  482. error = xfs_trans_commit(tp);
  483. return error;
  484. abort_error:
  485. xfs_refcount_finish_one_cleanup(tp, rcur, error);
  486. abort_defer:
  487. xfs_defer_cancel(&dfops);
  488. xfs_trans_cancel(tp);
  489. return error;
  490. }