xfs_extfree_item.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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_shared.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_ag.h"
  15. #include "xfs_defer.h"
  16. #include "xfs_trans.h"
  17. #include "xfs_trans_priv.h"
  18. #include "xfs_extfree_item.h"
  19. #include "xfs_log.h"
  20. #include "xfs_btree.h"
  21. #include "xfs_rmap.h"
  22. #include "xfs_alloc.h"
  23. #include "xfs_bmap.h"
  24. #include "xfs_trace.h"
  25. #include "xfs_error.h"
  26. #include "xfs_log_priv.h"
  27. #include "xfs_log_recover.h"
  28. struct kmem_cache *xfs_efi_cache;
  29. struct kmem_cache *xfs_efd_cache;
  30. static const struct xfs_item_ops xfs_efi_item_ops;
  31. static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip)
  32. {
  33. return container_of(lip, struct xfs_efi_log_item, efi_item);
  34. }
  35. STATIC void
  36. xfs_efi_item_free(
  37. struct xfs_efi_log_item *efip)
  38. {
  39. kmem_free(efip->efi_item.li_lv_shadow);
  40. if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
  41. kmem_free(efip);
  42. else
  43. kmem_cache_free(xfs_efi_cache, efip);
  44. }
  45. /*
  46. * Freeing the efi requires that we remove it from the AIL if it has already
  47. * been placed there. However, the EFI may not yet have been placed in the AIL
  48. * when called by xfs_efi_release() from EFD processing due to the ordering of
  49. * committed vs unpin operations in bulk insert operations. Hence the reference
  50. * count to ensure only the last caller frees the EFI.
  51. */
  52. STATIC void
  53. xfs_efi_release(
  54. struct xfs_efi_log_item *efip)
  55. {
  56. ASSERT(atomic_read(&efip->efi_refcount) > 0);
  57. if (!atomic_dec_and_test(&efip->efi_refcount))
  58. return;
  59. xfs_trans_ail_delete(&efip->efi_item, 0);
  60. xfs_efi_item_free(efip);
  61. }
  62. STATIC void
  63. xfs_efi_item_size(
  64. struct xfs_log_item *lip,
  65. int *nvecs,
  66. int *nbytes)
  67. {
  68. struct xfs_efi_log_item *efip = EFI_ITEM(lip);
  69. *nvecs += 1;
  70. *nbytes += xfs_efi_log_format_sizeof(efip->efi_format.efi_nextents);
  71. }
  72. /*
  73. * This is called to fill in the vector of log iovecs for the
  74. * given efi log item. We use only 1 iovec, and we point that
  75. * at the efi_log_format structure embedded in the efi item.
  76. * It is at this point that we assert that all of the extent
  77. * slots in the efi item have been filled.
  78. */
  79. STATIC void
  80. xfs_efi_item_format(
  81. struct xfs_log_item *lip,
  82. struct xfs_log_vec *lv)
  83. {
  84. struct xfs_efi_log_item *efip = EFI_ITEM(lip);
  85. struct xfs_log_iovec *vecp = NULL;
  86. ASSERT(atomic_read(&efip->efi_next_extent) ==
  87. efip->efi_format.efi_nextents);
  88. efip->efi_format.efi_type = XFS_LI_EFI;
  89. efip->efi_format.efi_size = 1;
  90. xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
  91. &efip->efi_format,
  92. xfs_efi_log_format_sizeof(efip->efi_format.efi_nextents));
  93. }
  94. /*
  95. * The unpin operation is the last place an EFI is manipulated in the log. It is
  96. * either inserted in the AIL or aborted in the event of a log I/O error. In
  97. * either case, the EFI transaction has been successfully committed to make it
  98. * this far. Therefore, we expect whoever committed the EFI to either construct
  99. * and commit the EFD or drop the EFD's reference in the event of error. Simply
  100. * drop the log's EFI reference now that the log is done with it.
  101. */
  102. STATIC void
  103. xfs_efi_item_unpin(
  104. struct xfs_log_item *lip,
  105. int remove)
  106. {
  107. struct xfs_efi_log_item *efip = EFI_ITEM(lip);
  108. xfs_efi_release(efip);
  109. }
  110. /*
  111. * The EFI has been either committed or aborted if the transaction has been
  112. * cancelled. If the transaction was cancelled, an EFD isn't going to be
  113. * constructed and thus we free the EFI here directly.
  114. */
  115. STATIC void
  116. xfs_efi_item_release(
  117. struct xfs_log_item *lip)
  118. {
  119. xfs_efi_release(EFI_ITEM(lip));
  120. }
  121. /*
  122. * Allocate and initialize an efi item with the given number of extents.
  123. */
  124. STATIC struct xfs_efi_log_item *
  125. xfs_efi_init(
  126. struct xfs_mount *mp,
  127. uint nextents)
  128. {
  129. struct xfs_efi_log_item *efip;
  130. ASSERT(nextents > 0);
  131. if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
  132. efip = kzalloc(xfs_efi_log_item_sizeof(nextents),
  133. GFP_KERNEL | __GFP_NOFAIL);
  134. } else {
  135. efip = kmem_cache_zalloc(xfs_efi_cache,
  136. GFP_KERNEL | __GFP_NOFAIL);
  137. }
  138. xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
  139. efip->efi_format.efi_nextents = nextents;
  140. efip->efi_format.efi_id = (uintptr_t)(void *)efip;
  141. atomic_set(&efip->efi_next_extent, 0);
  142. atomic_set(&efip->efi_refcount, 2);
  143. return efip;
  144. }
  145. /*
  146. * Copy an EFI format buffer from the given buf, and into the destination
  147. * EFI format structure.
  148. * The given buffer can be in 32 bit or 64 bit form (which has different padding),
  149. * one of which will be the native format for this kernel.
  150. * It will handle the conversion of formats if necessary.
  151. */
  152. STATIC int
  153. xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
  154. {
  155. xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
  156. uint i;
  157. uint len = xfs_efi_log_format_sizeof(src_efi_fmt->efi_nextents);
  158. uint len32 = xfs_efi_log_format32_sizeof(src_efi_fmt->efi_nextents);
  159. uint len64 = xfs_efi_log_format64_sizeof(src_efi_fmt->efi_nextents);
  160. if (buf->i_len == len) {
  161. memcpy(dst_efi_fmt, src_efi_fmt,
  162. offsetof(struct xfs_efi_log_format, efi_extents));
  163. for (i = 0; i < src_efi_fmt->efi_nextents; i++)
  164. memcpy(&dst_efi_fmt->efi_extents[i],
  165. &src_efi_fmt->efi_extents[i],
  166. sizeof(struct xfs_extent));
  167. return 0;
  168. } else if (buf->i_len == len32) {
  169. xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
  170. dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
  171. dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
  172. dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
  173. dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
  174. for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
  175. dst_efi_fmt->efi_extents[i].ext_start =
  176. src_efi_fmt_32->efi_extents[i].ext_start;
  177. dst_efi_fmt->efi_extents[i].ext_len =
  178. src_efi_fmt_32->efi_extents[i].ext_len;
  179. }
  180. return 0;
  181. } else if (buf->i_len == len64) {
  182. xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
  183. dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
  184. dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
  185. dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
  186. dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
  187. for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
  188. dst_efi_fmt->efi_extents[i].ext_start =
  189. src_efi_fmt_64->efi_extents[i].ext_start;
  190. dst_efi_fmt->efi_extents[i].ext_len =
  191. src_efi_fmt_64->efi_extents[i].ext_len;
  192. }
  193. return 0;
  194. }
  195. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, NULL, buf->i_addr,
  196. buf->i_len);
  197. return -EFSCORRUPTED;
  198. }
  199. static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
  200. {
  201. return container_of(lip, struct xfs_efd_log_item, efd_item);
  202. }
  203. STATIC void
  204. xfs_efd_item_free(struct xfs_efd_log_item *efdp)
  205. {
  206. kmem_free(efdp->efd_item.li_lv_shadow);
  207. if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
  208. kmem_free(efdp);
  209. else
  210. kmem_cache_free(xfs_efd_cache, efdp);
  211. }
  212. STATIC void
  213. xfs_efd_item_size(
  214. struct xfs_log_item *lip,
  215. int *nvecs,
  216. int *nbytes)
  217. {
  218. struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
  219. *nvecs += 1;
  220. *nbytes += xfs_efd_log_format_sizeof(efdp->efd_format.efd_nextents);
  221. }
  222. /*
  223. * This is called to fill in the vector of log iovecs for the
  224. * given efd log item. We use only 1 iovec, and we point that
  225. * at the efd_log_format structure embedded in the efd item.
  226. * It is at this point that we assert that all of the extent
  227. * slots in the efd item have been filled.
  228. */
  229. STATIC void
  230. xfs_efd_item_format(
  231. struct xfs_log_item *lip,
  232. struct xfs_log_vec *lv)
  233. {
  234. struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
  235. struct xfs_log_iovec *vecp = NULL;
  236. ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
  237. efdp->efd_format.efd_type = XFS_LI_EFD;
  238. efdp->efd_format.efd_size = 1;
  239. xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
  240. &efdp->efd_format,
  241. xfs_efd_log_format_sizeof(efdp->efd_format.efd_nextents));
  242. }
  243. /*
  244. * The EFD is either committed or aborted if the transaction is cancelled. If
  245. * the transaction is cancelled, drop our reference to the EFI and free the EFD.
  246. */
  247. STATIC void
  248. xfs_efd_item_release(
  249. struct xfs_log_item *lip)
  250. {
  251. struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
  252. xfs_efi_release(efdp->efd_efip);
  253. xfs_efd_item_free(efdp);
  254. }
  255. static struct xfs_log_item *
  256. xfs_efd_item_intent(
  257. struct xfs_log_item *lip)
  258. {
  259. return &EFD_ITEM(lip)->efd_efip->efi_item;
  260. }
  261. static const struct xfs_item_ops xfs_efd_item_ops = {
  262. .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED |
  263. XFS_ITEM_INTENT_DONE,
  264. .iop_size = xfs_efd_item_size,
  265. .iop_format = xfs_efd_item_format,
  266. .iop_release = xfs_efd_item_release,
  267. .iop_intent = xfs_efd_item_intent,
  268. };
  269. /*
  270. * Allocate an "extent free done" log item that will hold nextents worth of
  271. * extents. The caller must use all nextents extents, because we are not
  272. * flexible about this at all.
  273. */
  274. static struct xfs_efd_log_item *
  275. xfs_trans_get_efd(
  276. struct xfs_trans *tp,
  277. struct xfs_efi_log_item *efip,
  278. unsigned int nextents)
  279. {
  280. struct xfs_efd_log_item *efdp;
  281. ASSERT(nextents > 0);
  282. if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
  283. efdp = kzalloc(xfs_efd_log_item_sizeof(nextents),
  284. GFP_KERNEL | __GFP_NOFAIL);
  285. } else {
  286. efdp = kmem_cache_zalloc(xfs_efd_cache,
  287. GFP_KERNEL | __GFP_NOFAIL);
  288. }
  289. xfs_log_item_init(tp->t_mountp, &efdp->efd_item, XFS_LI_EFD,
  290. &xfs_efd_item_ops);
  291. efdp->efd_efip = efip;
  292. efdp->efd_format.efd_nextents = nextents;
  293. efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
  294. xfs_trans_add_item(tp, &efdp->efd_item);
  295. return efdp;
  296. }
  297. /*
  298. * Free an extent and log it to the EFD. Note that the transaction is marked
  299. * dirty regardless of whether the extent free succeeds or fails to support the
  300. * EFI/EFD lifecycle rules.
  301. */
  302. static int
  303. xfs_trans_free_extent(
  304. struct xfs_trans *tp,
  305. struct xfs_efd_log_item *efdp,
  306. xfs_fsblock_t start_block,
  307. xfs_extlen_t ext_len,
  308. const struct xfs_owner_info *oinfo,
  309. bool skip_discard)
  310. {
  311. struct xfs_mount *mp = tp->t_mountp;
  312. struct xfs_extent *extp;
  313. uint next_extent;
  314. xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, start_block);
  315. xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp,
  316. start_block);
  317. int error;
  318. trace_xfs_bmap_free_deferred(tp->t_mountp, agno, 0, agbno, ext_len);
  319. error = __xfs_free_extent(tp, start_block, ext_len,
  320. oinfo, XFS_AG_RESV_NONE, skip_discard);
  321. /*
  322. * Mark the transaction dirty, even on error. This ensures the
  323. * transaction is aborted, which:
  324. *
  325. * 1.) releases the EFI and frees the EFD
  326. * 2.) shuts down the filesystem
  327. */
  328. tp->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE;
  329. set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
  330. next_extent = efdp->efd_next_extent;
  331. ASSERT(next_extent < efdp->efd_format.efd_nextents);
  332. extp = &(efdp->efd_format.efd_extents[next_extent]);
  333. extp->ext_start = start_block;
  334. extp->ext_len = ext_len;
  335. efdp->efd_next_extent++;
  336. return error;
  337. }
  338. /* Sort bmap items by AG. */
  339. static int
  340. xfs_extent_free_diff_items(
  341. void *priv,
  342. const struct list_head *a,
  343. const struct list_head *b)
  344. {
  345. struct xfs_mount *mp = priv;
  346. struct xfs_extent_free_item *ra;
  347. struct xfs_extent_free_item *rb;
  348. ra = container_of(a, struct xfs_extent_free_item, xefi_list);
  349. rb = container_of(b, struct xfs_extent_free_item, xefi_list);
  350. return XFS_FSB_TO_AGNO(mp, ra->xefi_startblock) -
  351. XFS_FSB_TO_AGNO(mp, rb->xefi_startblock);
  352. }
  353. /* Log a free extent to the intent item. */
  354. STATIC void
  355. xfs_extent_free_log_item(
  356. struct xfs_trans *tp,
  357. struct xfs_efi_log_item *efip,
  358. struct xfs_extent_free_item *free)
  359. {
  360. uint next_extent;
  361. struct xfs_extent *extp;
  362. tp->t_flags |= XFS_TRANS_DIRTY;
  363. set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
  364. /*
  365. * atomic_inc_return gives us the value after the increment;
  366. * we want to use it as an array index so we need to subtract 1 from
  367. * it.
  368. */
  369. next_extent = atomic_inc_return(&efip->efi_next_extent) - 1;
  370. ASSERT(next_extent < efip->efi_format.efi_nextents);
  371. extp = &efip->efi_format.efi_extents[next_extent];
  372. extp->ext_start = free->xefi_startblock;
  373. extp->ext_len = free->xefi_blockcount;
  374. }
  375. static struct xfs_log_item *
  376. xfs_extent_free_create_intent(
  377. struct xfs_trans *tp,
  378. struct list_head *items,
  379. unsigned int count,
  380. bool sort)
  381. {
  382. struct xfs_mount *mp = tp->t_mountp;
  383. struct xfs_efi_log_item *efip = xfs_efi_init(mp, count);
  384. struct xfs_extent_free_item *free;
  385. ASSERT(count > 0);
  386. xfs_trans_add_item(tp, &efip->efi_item);
  387. if (sort)
  388. list_sort(mp, items, xfs_extent_free_diff_items);
  389. list_for_each_entry(free, items, xefi_list)
  390. xfs_extent_free_log_item(tp, efip, free);
  391. return &efip->efi_item;
  392. }
  393. /* Get an EFD so we can process all the free extents. */
  394. static struct xfs_log_item *
  395. xfs_extent_free_create_done(
  396. struct xfs_trans *tp,
  397. struct xfs_log_item *intent,
  398. unsigned int count)
  399. {
  400. return &xfs_trans_get_efd(tp, EFI_ITEM(intent), count)->efd_item;
  401. }
  402. /* Process a free extent. */
  403. STATIC int
  404. xfs_extent_free_finish_item(
  405. struct xfs_trans *tp,
  406. struct xfs_log_item *done,
  407. struct list_head *item,
  408. struct xfs_btree_cur **state)
  409. {
  410. struct xfs_owner_info oinfo = { };
  411. struct xfs_extent_free_item *free;
  412. int error;
  413. free = container_of(item, struct xfs_extent_free_item, xefi_list);
  414. oinfo.oi_owner = free->xefi_owner;
  415. if (free->xefi_flags & XFS_EFI_ATTR_FORK)
  416. oinfo.oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
  417. if (free->xefi_flags & XFS_EFI_BMBT_BLOCK)
  418. oinfo.oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK;
  419. error = xfs_trans_free_extent(tp, EFD_ITEM(done),
  420. free->xefi_startblock,
  421. free->xefi_blockcount,
  422. &oinfo, free->xefi_flags & XFS_EFI_SKIP_DISCARD);
  423. kmem_cache_free(xfs_extfree_item_cache, free);
  424. return error;
  425. }
  426. /* Abort all pending EFIs. */
  427. STATIC void
  428. xfs_extent_free_abort_intent(
  429. struct xfs_log_item *intent)
  430. {
  431. xfs_efi_release(EFI_ITEM(intent));
  432. }
  433. /* Cancel a free extent. */
  434. STATIC void
  435. xfs_extent_free_cancel_item(
  436. struct list_head *item)
  437. {
  438. struct xfs_extent_free_item *free;
  439. free = container_of(item, struct xfs_extent_free_item, xefi_list);
  440. kmem_cache_free(xfs_extfree_item_cache, free);
  441. }
  442. const struct xfs_defer_op_type xfs_extent_free_defer_type = {
  443. .max_items = XFS_EFI_MAX_FAST_EXTENTS,
  444. .create_intent = xfs_extent_free_create_intent,
  445. .abort_intent = xfs_extent_free_abort_intent,
  446. .create_done = xfs_extent_free_create_done,
  447. .finish_item = xfs_extent_free_finish_item,
  448. .cancel_item = xfs_extent_free_cancel_item,
  449. };
  450. /*
  451. * AGFL blocks are accounted differently in the reserve pools and are not
  452. * inserted into the busy extent list.
  453. */
  454. STATIC int
  455. xfs_agfl_free_finish_item(
  456. struct xfs_trans *tp,
  457. struct xfs_log_item *done,
  458. struct list_head *item,
  459. struct xfs_btree_cur **state)
  460. {
  461. struct xfs_owner_info oinfo = { };
  462. struct xfs_mount *mp = tp->t_mountp;
  463. struct xfs_efd_log_item *efdp = EFD_ITEM(done);
  464. struct xfs_extent_free_item *free;
  465. struct xfs_extent *extp;
  466. struct xfs_buf *agbp;
  467. int error;
  468. xfs_agnumber_t agno;
  469. xfs_agblock_t agbno;
  470. uint next_extent;
  471. struct xfs_perag *pag;
  472. free = container_of(item, struct xfs_extent_free_item, xefi_list);
  473. ASSERT(free->xefi_blockcount == 1);
  474. agno = XFS_FSB_TO_AGNO(mp, free->xefi_startblock);
  475. agbno = XFS_FSB_TO_AGBNO(mp, free->xefi_startblock);
  476. oinfo.oi_owner = free->xefi_owner;
  477. trace_xfs_agfl_free_deferred(mp, agno, 0, agbno, free->xefi_blockcount);
  478. pag = xfs_perag_get(mp, agno);
  479. error = xfs_alloc_read_agf(pag, tp, 0, &agbp);
  480. if (!error)
  481. error = xfs_free_agfl_block(tp, agno, agbno, agbp, &oinfo);
  482. xfs_perag_put(pag);
  483. /*
  484. * Mark the transaction dirty, even on error. This ensures the
  485. * transaction is aborted, which:
  486. *
  487. * 1.) releases the EFI and frees the EFD
  488. * 2.) shuts down the filesystem
  489. */
  490. tp->t_flags |= XFS_TRANS_DIRTY;
  491. set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
  492. next_extent = efdp->efd_next_extent;
  493. ASSERT(next_extent < efdp->efd_format.efd_nextents);
  494. extp = &(efdp->efd_format.efd_extents[next_extent]);
  495. extp->ext_start = free->xefi_startblock;
  496. extp->ext_len = free->xefi_blockcount;
  497. efdp->efd_next_extent++;
  498. kmem_cache_free(xfs_extfree_item_cache, free);
  499. return error;
  500. }
  501. /* sub-type with special handling for AGFL deferred frees */
  502. const struct xfs_defer_op_type xfs_agfl_free_defer_type = {
  503. .max_items = XFS_EFI_MAX_FAST_EXTENTS,
  504. .create_intent = xfs_extent_free_create_intent,
  505. .abort_intent = xfs_extent_free_abort_intent,
  506. .create_done = xfs_extent_free_create_done,
  507. .finish_item = xfs_agfl_free_finish_item,
  508. .cancel_item = xfs_extent_free_cancel_item,
  509. };
  510. /* Is this recovered EFI ok? */
  511. static inline bool
  512. xfs_efi_validate_ext(
  513. struct xfs_mount *mp,
  514. struct xfs_extent *extp)
  515. {
  516. return xfs_verify_fsbext(mp, extp->ext_start, extp->ext_len);
  517. }
  518. /*
  519. * Process an extent free intent item that was recovered from
  520. * the log. We need to free the extents that it describes.
  521. */
  522. STATIC int
  523. xfs_efi_item_recover(
  524. struct xfs_log_item *lip,
  525. struct list_head *capture_list)
  526. {
  527. struct xfs_efi_log_item *efip = EFI_ITEM(lip);
  528. struct xfs_mount *mp = lip->li_log->l_mp;
  529. struct xfs_efd_log_item *efdp;
  530. struct xfs_trans *tp;
  531. struct xfs_extent *extp;
  532. int i;
  533. int error = 0;
  534. /*
  535. * First check the validity of the extents described by the
  536. * EFI. If any are bad, then assume that all are bad and
  537. * just toss the EFI.
  538. */
  539. for (i = 0; i < efip->efi_format.efi_nextents; i++) {
  540. if (!xfs_efi_validate_ext(mp,
  541. &efip->efi_format.efi_extents[i])) {
  542. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  543. &efip->efi_format,
  544. sizeof(efip->efi_format));
  545. return -EFSCORRUPTED;
  546. }
  547. }
  548. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
  549. if (error)
  550. return error;
  551. efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
  552. for (i = 0; i < efip->efi_format.efi_nextents; i++) {
  553. extp = &efip->efi_format.efi_extents[i];
  554. error = xfs_trans_free_extent(tp, efdp, extp->ext_start,
  555. extp->ext_len,
  556. &XFS_RMAP_OINFO_ANY_OWNER, false);
  557. if (error == -EFSCORRUPTED)
  558. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  559. extp, sizeof(*extp));
  560. if (error)
  561. goto abort_error;
  562. }
  563. return xfs_defer_ops_capture_and_commit(tp, capture_list);
  564. abort_error:
  565. xfs_trans_cancel(tp);
  566. return error;
  567. }
  568. STATIC bool
  569. xfs_efi_item_match(
  570. struct xfs_log_item *lip,
  571. uint64_t intent_id)
  572. {
  573. return EFI_ITEM(lip)->efi_format.efi_id == intent_id;
  574. }
  575. /* Relog an intent item to push the log tail forward. */
  576. static struct xfs_log_item *
  577. xfs_efi_item_relog(
  578. struct xfs_log_item *intent,
  579. struct xfs_trans *tp)
  580. {
  581. struct xfs_efd_log_item *efdp;
  582. struct xfs_efi_log_item *efip;
  583. struct xfs_extent *extp;
  584. unsigned int count;
  585. count = EFI_ITEM(intent)->efi_format.efi_nextents;
  586. extp = EFI_ITEM(intent)->efi_format.efi_extents;
  587. tp->t_flags |= XFS_TRANS_DIRTY;
  588. efdp = xfs_trans_get_efd(tp, EFI_ITEM(intent), count);
  589. efdp->efd_next_extent = count;
  590. memcpy(efdp->efd_format.efd_extents, extp, count * sizeof(*extp));
  591. set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
  592. efip = xfs_efi_init(tp->t_mountp, count);
  593. memcpy(efip->efi_format.efi_extents, extp, count * sizeof(*extp));
  594. atomic_set(&efip->efi_next_extent, count);
  595. xfs_trans_add_item(tp, &efip->efi_item);
  596. set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
  597. return &efip->efi_item;
  598. }
  599. static const struct xfs_item_ops xfs_efi_item_ops = {
  600. .flags = XFS_ITEM_INTENT,
  601. .iop_size = xfs_efi_item_size,
  602. .iop_format = xfs_efi_item_format,
  603. .iop_unpin = xfs_efi_item_unpin,
  604. .iop_release = xfs_efi_item_release,
  605. .iop_recover = xfs_efi_item_recover,
  606. .iop_match = xfs_efi_item_match,
  607. .iop_relog = xfs_efi_item_relog,
  608. };
  609. /*
  610. * This routine is called to create an in-core extent free intent
  611. * item from the efi format structure which was logged on disk.
  612. * It allocates an in-core efi, copies the extents from the format
  613. * structure into it, and adds the efi to the AIL with the given
  614. * LSN.
  615. */
  616. STATIC int
  617. xlog_recover_efi_commit_pass2(
  618. struct xlog *log,
  619. struct list_head *buffer_list,
  620. struct xlog_recover_item *item,
  621. xfs_lsn_t lsn)
  622. {
  623. struct xfs_mount *mp = log->l_mp;
  624. struct xfs_efi_log_item *efip;
  625. struct xfs_efi_log_format *efi_formatp;
  626. int error;
  627. efi_formatp = item->ri_buf[0].i_addr;
  628. if (item->ri_buf[0].i_len < xfs_efi_log_format_sizeof(0)) {
  629. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  630. item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
  631. return -EFSCORRUPTED;
  632. }
  633. efip = xfs_efi_init(mp, efi_formatp->efi_nextents);
  634. error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
  635. if (error) {
  636. xfs_efi_item_free(efip);
  637. return error;
  638. }
  639. atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
  640. /*
  641. * Insert the intent into the AIL directly and drop one reference so
  642. * that finishing or canceling the work will drop the other.
  643. */
  644. xfs_trans_ail_insert(log->l_ailp, &efip->efi_item, lsn);
  645. xfs_efi_release(efip);
  646. return 0;
  647. }
  648. const struct xlog_recover_item_ops xlog_efi_item_ops = {
  649. .item_type = XFS_LI_EFI,
  650. .commit_pass2 = xlog_recover_efi_commit_pass2,
  651. };
  652. /*
  653. * This routine is called when an EFD format structure is found in a committed
  654. * transaction in the log. Its purpose is to cancel the corresponding EFI if it
  655. * was still in the log. To do this it searches the AIL for the EFI with an id
  656. * equal to that in the EFD format structure. If we find it we drop the EFD
  657. * reference, which removes the EFI from the AIL and frees it.
  658. */
  659. STATIC int
  660. xlog_recover_efd_commit_pass2(
  661. struct xlog *log,
  662. struct list_head *buffer_list,
  663. struct xlog_recover_item *item,
  664. xfs_lsn_t lsn)
  665. {
  666. struct xfs_efd_log_format *efd_formatp;
  667. int buflen = item->ri_buf[0].i_len;
  668. efd_formatp = item->ri_buf[0].i_addr;
  669. if (buflen < sizeof(struct xfs_efd_log_format)) {
  670. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
  671. efd_formatp, buflen);
  672. return -EFSCORRUPTED;
  673. }
  674. if (item->ri_buf[0].i_len != xfs_efd_log_format32_sizeof(
  675. efd_formatp->efd_nextents) &&
  676. item->ri_buf[0].i_len != xfs_efd_log_format64_sizeof(
  677. efd_formatp->efd_nextents)) {
  678. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
  679. efd_formatp, buflen);
  680. return -EFSCORRUPTED;
  681. }
  682. xlog_recover_release_intent(log, XFS_LI_EFI, efd_formatp->efd_efi_id);
  683. return 0;
  684. }
  685. const struct xlog_recover_item_ops xlog_efd_item_ops = {
  686. .item_type = XFS_LI_EFD,
  687. .commit_pass2 = xlog_recover_efd_commit_pass2,
  688. };