xfs_extfree_item.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  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_trans.h"
  14. #include "xfs_trans_priv.h"
  15. #include "xfs_buf_item.h"
  16. #include "xfs_extfree_item.h"
  17. #include "xfs_log.h"
  18. #include "xfs_btree.h"
  19. #include "xfs_rmap.h"
  20. kmem_zone_t *xfs_efi_zone;
  21. kmem_zone_t *xfs_efd_zone;
  22. static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip)
  23. {
  24. return container_of(lip, struct xfs_efi_log_item, efi_item);
  25. }
  26. void
  27. xfs_efi_item_free(
  28. struct xfs_efi_log_item *efip)
  29. {
  30. kmem_free(efip->efi_item.li_lv_shadow);
  31. if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
  32. kmem_free(efip);
  33. else
  34. kmem_zone_free(xfs_efi_zone, efip);
  35. }
  36. /*
  37. * Freeing the efi requires that we remove it from the AIL if it has already
  38. * been placed there. However, the EFI may not yet have been placed in the AIL
  39. * when called by xfs_efi_release() from EFD 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 EFI.
  42. */
  43. void
  44. xfs_efi_release(
  45. struct xfs_efi_log_item *efip)
  46. {
  47. ASSERT(atomic_read(&efip->efi_refcount) > 0);
  48. if (atomic_dec_and_test(&efip->efi_refcount)) {
  49. xfs_trans_ail_remove(&efip->efi_item, SHUTDOWN_LOG_IO_ERROR);
  50. xfs_efi_item_free(efip);
  51. }
  52. }
  53. /*
  54. * This returns the number of iovecs needed to log the given efi item.
  55. * We only need 1 iovec for an efi item. It just logs the efi_log_format
  56. * structure.
  57. */
  58. static inline int
  59. xfs_efi_item_sizeof(
  60. struct xfs_efi_log_item *efip)
  61. {
  62. return sizeof(struct xfs_efi_log_format) +
  63. (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
  64. }
  65. STATIC void
  66. xfs_efi_item_size(
  67. struct xfs_log_item *lip,
  68. int *nvecs,
  69. int *nbytes)
  70. {
  71. *nvecs += 1;
  72. *nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip));
  73. }
  74. /*
  75. * This is called to fill in the vector of log iovecs for the
  76. * given efi log item. We use only 1 iovec, and we point that
  77. * at the efi_log_format structure embedded in the efi item.
  78. * It is at this point that we assert that all of the extent
  79. * slots in the efi item have been filled.
  80. */
  81. STATIC void
  82. xfs_efi_item_format(
  83. struct xfs_log_item *lip,
  84. struct xfs_log_vec *lv)
  85. {
  86. struct xfs_efi_log_item *efip = EFI_ITEM(lip);
  87. struct xfs_log_iovec *vecp = NULL;
  88. ASSERT(atomic_read(&efip->efi_next_extent) ==
  89. efip->efi_format.efi_nextents);
  90. efip->efi_format.efi_type = XFS_LI_EFI;
  91. efip->efi_format.efi_size = 1;
  92. xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
  93. &efip->efi_format,
  94. xfs_efi_item_sizeof(efip));
  95. }
  96. /*
  97. * Pinning has no meaning for an efi item, so just return.
  98. */
  99. STATIC void
  100. xfs_efi_item_pin(
  101. struct xfs_log_item *lip)
  102. {
  103. }
  104. /*
  105. * The unpin operation is the last place an EFI is manipulated in the log. It is
  106. * either inserted in the AIL or aborted in the event of a log I/O error. In
  107. * either case, the EFI transaction has been successfully committed to make it
  108. * this far. Therefore, we expect whoever committed the EFI to either construct
  109. * and commit the EFD or drop the EFD's reference in the event of error. Simply
  110. * drop the log's EFI reference now that the log is done with it.
  111. */
  112. STATIC void
  113. xfs_efi_item_unpin(
  114. struct xfs_log_item *lip,
  115. int remove)
  116. {
  117. struct xfs_efi_log_item *efip = EFI_ITEM(lip);
  118. xfs_efi_release(efip);
  119. }
  120. /*
  121. * Efi items have no locking or pushing. However, since EFIs are pulled from
  122. * the AIL when their corresponding EFDs are committed to disk, their situation
  123. * is very similar to being pinned. Return XFS_ITEM_PINNED so that the caller
  124. * will eventually flush the log. This should help in getting the EFI out of
  125. * the AIL.
  126. */
  127. STATIC uint
  128. xfs_efi_item_push(
  129. struct xfs_log_item *lip,
  130. struct list_head *buffer_list)
  131. {
  132. return XFS_ITEM_PINNED;
  133. }
  134. /*
  135. * The EFI has been either committed or aborted if the transaction has been
  136. * cancelled. If the transaction was cancelled, an EFD isn't going to be
  137. * constructed and thus we free the EFI here directly.
  138. */
  139. STATIC void
  140. xfs_efi_item_unlock(
  141. struct xfs_log_item *lip)
  142. {
  143. if (test_bit(XFS_LI_ABORTED, &lip->li_flags))
  144. xfs_efi_release(EFI_ITEM(lip));
  145. }
  146. /*
  147. * The EFI is logged only once and cannot be moved in the log, so simply return
  148. * the lsn at which it's been logged.
  149. */
  150. STATIC xfs_lsn_t
  151. xfs_efi_item_committed(
  152. struct xfs_log_item *lip,
  153. xfs_lsn_t lsn)
  154. {
  155. return lsn;
  156. }
  157. /*
  158. * The EFI dependency tracking op doesn't do squat. It can't because
  159. * it doesn't know where the free extent is coming from. The dependency
  160. * tracking has to be handled by the "enclosing" metadata object. For
  161. * example, for inodes, the inode is locked throughout the extent freeing
  162. * so the dependency should be recorded there.
  163. */
  164. STATIC void
  165. xfs_efi_item_committing(
  166. struct xfs_log_item *lip,
  167. xfs_lsn_t lsn)
  168. {
  169. }
  170. /*
  171. * This is the ops vector shared by all efi log items.
  172. */
  173. static const struct xfs_item_ops xfs_efi_item_ops = {
  174. .iop_size = xfs_efi_item_size,
  175. .iop_format = xfs_efi_item_format,
  176. .iop_pin = xfs_efi_item_pin,
  177. .iop_unpin = xfs_efi_item_unpin,
  178. .iop_unlock = xfs_efi_item_unlock,
  179. .iop_committed = xfs_efi_item_committed,
  180. .iop_push = xfs_efi_item_push,
  181. .iop_committing = xfs_efi_item_committing
  182. };
  183. /*
  184. * Allocate and initialize an efi item with the given number of extents.
  185. */
  186. struct xfs_efi_log_item *
  187. xfs_efi_init(
  188. struct xfs_mount *mp,
  189. uint nextents)
  190. {
  191. struct xfs_efi_log_item *efip;
  192. uint size;
  193. ASSERT(nextents > 0);
  194. if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
  195. size = (uint)(sizeof(xfs_efi_log_item_t) +
  196. ((nextents - 1) * sizeof(xfs_extent_t)));
  197. efip = kmem_zalloc(size, KM_SLEEP);
  198. } else {
  199. efip = kmem_zone_zalloc(xfs_efi_zone, KM_SLEEP);
  200. }
  201. xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
  202. efip->efi_format.efi_nextents = nextents;
  203. efip->efi_format.efi_id = (uintptr_t)(void *)efip;
  204. atomic_set(&efip->efi_next_extent, 0);
  205. atomic_set(&efip->efi_refcount, 2);
  206. return efip;
  207. }
  208. /*
  209. * Copy an EFI format buffer from the given buf, and into the destination
  210. * EFI format structure.
  211. * The given buffer can be in 32 bit or 64 bit form (which has different padding),
  212. * one of which will be the native format for this kernel.
  213. * It will handle the conversion of formats if necessary.
  214. */
  215. int
  216. xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
  217. {
  218. xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
  219. uint i;
  220. uint len = sizeof(xfs_efi_log_format_t) +
  221. (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t);
  222. uint len32 = sizeof(xfs_efi_log_format_32_t) +
  223. (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t);
  224. uint len64 = sizeof(xfs_efi_log_format_64_t) +
  225. (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t);
  226. if (buf->i_len == len) {
  227. memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len);
  228. return 0;
  229. } else if (buf->i_len == len32) {
  230. xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
  231. dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
  232. dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
  233. dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
  234. dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
  235. for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
  236. dst_efi_fmt->efi_extents[i].ext_start =
  237. src_efi_fmt_32->efi_extents[i].ext_start;
  238. dst_efi_fmt->efi_extents[i].ext_len =
  239. src_efi_fmt_32->efi_extents[i].ext_len;
  240. }
  241. return 0;
  242. } else if (buf->i_len == len64) {
  243. xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
  244. dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
  245. dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
  246. dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
  247. dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
  248. for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
  249. dst_efi_fmt->efi_extents[i].ext_start =
  250. src_efi_fmt_64->efi_extents[i].ext_start;
  251. dst_efi_fmt->efi_extents[i].ext_len =
  252. src_efi_fmt_64->efi_extents[i].ext_len;
  253. }
  254. return 0;
  255. }
  256. return -EFSCORRUPTED;
  257. }
  258. static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
  259. {
  260. return container_of(lip, struct xfs_efd_log_item, efd_item);
  261. }
  262. STATIC void
  263. xfs_efd_item_free(struct xfs_efd_log_item *efdp)
  264. {
  265. kmem_free(efdp->efd_item.li_lv_shadow);
  266. if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
  267. kmem_free(efdp);
  268. else
  269. kmem_zone_free(xfs_efd_zone, efdp);
  270. }
  271. /*
  272. * This returns the number of iovecs needed to log the given efd item.
  273. * We only need 1 iovec for an efd item. It just logs the efd_log_format
  274. * structure.
  275. */
  276. static inline int
  277. xfs_efd_item_sizeof(
  278. struct xfs_efd_log_item *efdp)
  279. {
  280. return sizeof(xfs_efd_log_format_t) +
  281. (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
  282. }
  283. STATIC void
  284. xfs_efd_item_size(
  285. struct xfs_log_item *lip,
  286. int *nvecs,
  287. int *nbytes)
  288. {
  289. *nvecs += 1;
  290. *nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip));
  291. }
  292. /*
  293. * This is called to fill in the vector of log iovecs for the
  294. * given efd log item. We use only 1 iovec, and we point that
  295. * at the efd_log_format structure embedded in the efd item.
  296. * It is at this point that we assert that all of the extent
  297. * slots in the efd item have been filled.
  298. */
  299. STATIC void
  300. xfs_efd_item_format(
  301. struct xfs_log_item *lip,
  302. struct xfs_log_vec *lv)
  303. {
  304. struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
  305. struct xfs_log_iovec *vecp = NULL;
  306. ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
  307. efdp->efd_format.efd_type = XFS_LI_EFD;
  308. efdp->efd_format.efd_size = 1;
  309. xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
  310. &efdp->efd_format,
  311. xfs_efd_item_sizeof(efdp));
  312. }
  313. /*
  314. * Pinning has no meaning for an efd item, so just return.
  315. */
  316. STATIC void
  317. xfs_efd_item_pin(
  318. struct xfs_log_item *lip)
  319. {
  320. }
  321. /*
  322. * Since pinning has no meaning for an efd item, unpinning does
  323. * not either.
  324. */
  325. STATIC void
  326. xfs_efd_item_unpin(
  327. struct xfs_log_item *lip,
  328. int remove)
  329. {
  330. }
  331. /*
  332. * There isn't much you can do to push on an efd item. It is simply stuck
  333. * waiting for the log to be flushed to disk.
  334. */
  335. STATIC uint
  336. xfs_efd_item_push(
  337. struct xfs_log_item *lip,
  338. struct list_head *buffer_list)
  339. {
  340. return XFS_ITEM_PINNED;
  341. }
  342. /*
  343. * The EFD is either committed or aborted if the transaction is cancelled. If
  344. * the transaction is cancelled, drop our reference to the EFI and free the EFD.
  345. */
  346. STATIC void
  347. xfs_efd_item_unlock(
  348. struct xfs_log_item *lip)
  349. {
  350. struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
  351. if (test_bit(XFS_LI_ABORTED, &lip->li_flags)) {
  352. xfs_efi_release(efdp->efd_efip);
  353. xfs_efd_item_free(efdp);
  354. }
  355. }
  356. /*
  357. * When the efd item is committed to disk, all we need to do is delete our
  358. * reference to our partner efi item and then free ourselves. Since we're
  359. * freeing ourselves we must return -1 to keep the transaction code from further
  360. * referencing this item.
  361. */
  362. STATIC xfs_lsn_t
  363. xfs_efd_item_committed(
  364. struct xfs_log_item *lip,
  365. xfs_lsn_t lsn)
  366. {
  367. struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
  368. /*
  369. * Drop the EFI reference regardless of whether the EFD has been
  370. * aborted. Once the EFD transaction is constructed, it is the sole
  371. * responsibility of the EFD to release the EFI (even if the EFI is
  372. * aborted due to log I/O error).
  373. */
  374. xfs_efi_release(efdp->efd_efip);
  375. xfs_efd_item_free(efdp);
  376. return (xfs_lsn_t)-1;
  377. }
  378. /*
  379. * The EFD dependency tracking op doesn't do squat. It can't because
  380. * it doesn't know where the free extent is coming from. The dependency
  381. * tracking has to be handled by the "enclosing" metadata object. For
  382. * example, for inodes, the inode is locked throughout the extent freeing
  383. * so the dependency should be recorded there.
  384. */
  385. STATIC void
  386. xfs_efd_item_committing(
  387. struct xfs_log_item *lip,
  388. xfs_lsn_t lsn)
  389. {
  390. }
  391. /*
  392. * This is the ops vector shared by all efd log items.
  393. */
  394. static const struct xfs_item_ops xfs_efd_item_ops = {
  395. .iop_size = xfs_efd_item_size,
  396. .iop_format = xfs_efd_item_format,
  397. .iop_pin = xfs_efd_item_pin,
  398. .iop_unpin = xfs_efd_item_unpin,
  399. .iop_unlock = xfs_efd_item_unlock,
  400. .iop_committed = xfs_efd_item_committed,
  401. .iop_push = xfs_efd_item_push,
  402. .iop_committing = xfs_efd_item_committing
  403. };
  404. /*
  405. * Allocate and initialize an efd item with the given number of extents.
  406. */
  407. struct xfs_efd_log_item *
  408. xfs_efd_init(
  409. struct xfs_mount *mp,
  410. struct xfs_efi_log_item *efip,
  411. uint nextents)
  412. {
  413. struct xfs_efd_log_item *efdp;
  414. uint size;
  415. ASSERT(nextents > 0);
  416. if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
  417. size = (uint)(sizeof(xfs_efd_log_item_t) +
  418. ((nextents - 1) * sizeof(xfs_extent_t)));
  419. efdp = kmem_zalloc(size, KM_SLEEP);
  420. } else {
  421. efdp = kmem_zone_zalloc(xfs_efd_zone, KM_SLEEP);
  422. }
  423. xfs_log_item_init(mp, &efdp->efd_item, XFS_LI_EFD, &xfs_efd_item_ops);
  424. efdp->efd_efip = efip;
  425. efdp->efd_format.efd_nextents = nextents;
  426. efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
  427. return efdp;
  428. }
  429. /*
  430. * Process an extent free intent item that was recovered from
  431. * the log. We need to free the extents that it describes.
  432. */
  433. int
  434. xfs_efi_recover(
  435. struct xfs_mount *mp,
  436. struct xfs_efi_log_item *efip)
  437. {
  438. struct xfs_efd_log_item *efdp;
  439. struct xfs_trans *tp;
  440. int i;
  441. int error = 0;
  442. xfs_extent_t *extp;
  443. xfs_fsblock_t startblock_fsb;
  444. struct xfs_owner_info oinfo;
  445. ASSERT(!test_bit(XFS_EFI_RECOVERED, &efip->efi_flags));
  446. /*
  447. * First check the validity of the extents described by the
  448. * EFI. If any are bad, then assume that all are bad and
  449. * just toss the EFI.
  450. */
  451. for (i = 0; i < efip->efi_format.efi_nextents; i++) {
  452. extp = &efip->efi_format.efi_extents[i];
  453. startblock_fsb = XFS_BB_TO_FSB(mp,
  454. XFS_FSB_TO_DADDR(mp, extp->ext_start));
  455. if (startblock_fsb == 0 ||
  456. extp->ext_len == 0 ||
  457. startblock_fsb >= mp->m_sb.sb_dblocks ||
  458. extp->ext_len >= mp->m_sb.sb_agblocks) {
  459. /*
  460. * This will pull the EFI from the AIL and
  461. * free the memory associated with it.
  462. */
  463. set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
  464. xfs_efi_release(efip);
  465. return -EIO;
  466. }
  467. }
  468. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
  469. if (error)
  470. return error;
  471. efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
  472. xfs_rmap_any_owner_update(&oinfo);
  473. for (i = 0; i < efip->efi_format.efi_nextents; i++) {
  474. extp = &efip->efi_format.efi_extents[i];
  475. error = xfs_trans_free_extent(tp, efdp, extp->ext_start,
  476. extp->ext_len, &oinfo, false);
  477. if (error)
  478. goto abort_error;
  479. }
  480. set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
  481. error = xfs_trans_commit(tp);
  482. return error;
  483. abort_error:
  484. xfs_trans_cancel(tp);
  485. return error;
  486. }