xfs_extfree_item.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. kvfree(efip->efi_item.li_lv_shadow);
  40. if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
  41. kfree(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. kvfree(efdp->efd_item.li_lv_shadow);
  207. if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
  208. kfree(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. static inline struct xfs_extent_free_item *xefi_entry(const struct list_head *e)
  270. {
  271. return list_entry(e, struct xfs_extent_free_item, xefi_list);
  272. }
  273. /*
  274. * Fill the EFD with all extents from the EFI when we need to roll the
  275. * transaction and continue with a new EFI.
  276. *
  277. * This simply copies all the extents in the EFI to the EFD rather than make
  278. * assumptions about which extents in the EFI have already been processed. We
  279. * currently keep the xefi list in the same order as the EFI extent list, but
  280. * that may not always be the case. Copying everything avoids leaving a landmine
  281. * were we fail to cancel all the extents in an EFI if the xefi list is
  282. * processed in a different order to the extents in the EFI.
  283. */
  284. static void
  285. xfs_efd_from_efi(
  286. struct xfs_efd_log_item *efdp)
  287. {
  288. struct xfs_efi_log_item *efip = efdp->efd_efip;
  289. uint i;
  290. ASSERT(efip->efi_format.efi_nextents > 0);
  291. ASSERT(efdp->efd_next_extent < efip->efi_format.efi_nextents);
  292. for (i = 0; i < efip->efi_format.efi_nextents; i++) {
  293. efdp->efd_format.efd_extents[i] =
  294. efip->efi_format.efi_extents[i];
  295. }
  296. efdp->efd_next_extent = efip->efi_format.efi_nextents;
  297. }
  298. static void
  299. xfs_efd_add_extent(
  300. struct xfs_efd_log_item *efdp,
  301. struct xfs_extent_free_item *xefi)
  302. {
  303. struct xfs_extent *extp;
  304. ASSERT(efdp->efd_next_extent < efdp->efd_format.efd_nextents);
  305. extp = &efdp->efd_format.efd_extents[efdp->efd_next_extent];
  306. extp->ext_start = xefi->xefi_startblock;
  307. extp->ext_len = xefi->xefi_blockcount;
  308. efdp->efd_next_extent++;
  309. }
  310. /* Sort bmap items by AG. */
  311. static int
  312. xfs_extent_free_diff_items(
  313. void *priv,
  314. const struct list_head *a,
  315. const struct list_head *b)
  316. {
  317. struct xfs_extent_free_item *ra = xefi_entry(a);
  318. struct xfs_extent_free_item *rb = xefi_entry(b);
  319. return ra->xefi_pag->pag_agno - rb->xefi_pag->pag_agno;
  320. }
  321. /* Log a free extent to the intent item. */
  322. STATIC void
  323. xfs_extent_free_log_item(
  324. struct xfs_trans *tp,
  325. struct xfs_efi_log_item *efip,
  326. struct xfs_extent_free_item *xefi)
  327. {
  328. uint next_extent;
  329. struct xfs_extent *extp;
  330. /*
  331. * atomic_inc_return gives us the value after the increment;
  332. * we want to use it as an array index so we need to subtract 1 from
  333. * it.
  334. */
  335. next_extent = atomic_inc_return(&efip->efi_next_extent) - 1;
  336. ASSERT(next_extent < efip->efi_format.efi_nextents);
  337. extp = &efip->efi_format.efi_extents[next_extent];
  338. extp->ext_start = xefi->xefi_startblock;
  339. extp->ext_len = xefi->xefi_blockcount;
  340. }
  341. static struct xfs_log_item *
  342. xfs_extent_free_create_intent(
  343. struct xfs_trans *tp,
  344. struct list_head *items,
  345. unsigned int count,
  346. bool sort)
  347. {
  348. struct xfs_mount *mp = tp->t_mountp;
  349. struct xfs_efi_log_item *efip = xfs_efi_init(mp, count);
  350. struct xfs_extent_free_item *xefi;
  351. ASSERT(count > 0);
  352. if (sort)
  353. list_sort(mp, items, xfs_extent_free_diff_items);
  354. list_for_each_entry(xefi, items, xefi_list)
  355. xfs_extent_free_log_item(tp, efip, xefi);
  356. return &efip->efi_item;
  357. }
  358. /* Get an EFD so we can process all the free extents. */
  359. static struct xfs_log_item *
  360. xfs_extent_free_create_done(
  361. struct xfs_trans *tp,
  362. struct xfs_log_item *intent,
  363. unsigned int count)
  364. {
  365. struct xfs_efi_log_item *efip = EFI_ITEM(intent);
  366. struct xfs_efd_log_item *efdp;
  367. ASSERT(count > 0);
  368. if (count > XFS_EFD_MAX_FAST_EXTENTS) {
  369. efdp = kzalloc(xfs_efd_log_item_sizeof(count),
  370. GFP_KERNEL | __GFP_NOFAIL);
  371. } else {
  372. efdp = kmem_cache_zalloc(xfs_efd_cache,
  373. GFP_KERNEL | __GFP_NOFAIL);
  374. }
  375. xfs_log_item_init(tp->t_mountp, &efdp->efd_item, XFS_LI_EFD,
  376. &xfs_efd_item_ops);
  377. efdp->efd_efip = efip;
  378. efdp->efd_format.efd_nextents = count;
  379. efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
  380. return &efdp->efd_item;
  381. }
  382. /* Add this deferred EFI to the transaction. */
  383. void
  384. xfs_extent_free_defer_add(
  385. struct xfs_trans *tp,
  386. struct xfs_extent_free_item *xefi,
  387. struct xfs_defer_pending **dfpp)
  388. {
  389. struct xfs_mount *mp = tp->t_mountp;
  390. trace_xfs_extent_free_defer(mp, xefi);
  391. xefi->xefi_pag = xfs_perag_intent_get(mp, xefi->xefi_startblock);
  392. if (xefi->xefi_agresv == XFS_AG_RESV_AGFL)
  393. *dfpp = xfs_defer_add(tp, &xefi->xefi_list,
  394. &xfs_agfl_free_defer_type);
  395. else
  396. *dfpp = xfs_defer_add(tp, &xefi->xefi_list,
  397. &xfs_extent_free_defer_type);
  398. }
  399. /* Cancel a free extent. */
  400. STATIC void
  401. xfs_extent_free_cancel_item(
  402. struct list_head *item)
  403. {
  404. struct xfs_extent_free_item *xefi = xefi_entry(item);
  405. xfs_perag_intent_put(xefi->xefi_pag);
  406. kmem_cache_free(xfs_extfree_item_cache, xefi);
  407. }
  408. /* Process a free extent. */
  409. STATIC int
  410. xfs_extent_free_finish_item(
  411. struct xfs_trans *tp,
  412. struct xfs_log_item *done,
  413. struct list_head *item,
  414. struct xfs_btree_cur **state)
  415. {
  416. struct xfs_owner_info oinfo = { };
  417. struct xfs_extent_free_item *xefi = xefi_entry(item);
  418. struct xfs_efd_log_item *efdp = EFD_ITEM(done);
  419. struct xfs_mount *mp = tp->t_mountp;
  420. xfs_agblock_t agbno;
  421. int error = 0;
  422. agbno = XFS_FSB_TO_AGBNO(mp, xefi->xefi_startblock);
  423. oinfo.oi_owner = xefi->xefi_owner;
  424. if (xefi->xefi_flags & XFS_EFI_ATTR_FORK)
  425. oinfo.oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
  426. if (xefi->xefi_flags & XFS_EFI_BMBT_BLOCK)
  427. oinfo.oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK;
  428. trace_xfs_extent_free_deferred(mp, xefi);
  429. /*
  430. * If we need a new transaction to make progress, the caller will log a
  431. * new EFI with the current contents. It will also log an EFD to cancel
  432. * the existing EFI, and so we need to copy all the unprocessed extents
  433. * in this EFI to the EFD so this works correctly.
  434. */
  435. if (!(xefi->xefi_flags & XFS_EFI_CANCELLED))
  436. error = __xfs_free_extent(tp, xefi->xefi_pag, agbno,
  437. xefi->xefi_blockcount, &oinfo, xefi->xefi_agresv,
  438. xefi->xefi_flags & XFS_EFI_SKIP_DISCARD);
  439. if (error == -EAGAIN) {
  440. xfs_efd_from_efi(efdp);
  441. return error;
  442. }
  443. xfs_efd_add_extent(efdp, xefi);
  444. xfs_extent_free_cancel_item(item);
  445. return error;
  446. }
  447. /* Abort all pending EFIs. */
  448. STATIC void
  449. xfs_extent_free_abort_intent(
  450. struct xfs_log_item *intent)
  451. {
  452. xfs_efi_release(EFI_ITEM(intent));
  453. }
  454. /*
  455. * AGFL blocks are accounted differently in the reserve pools and are not
  456. * inserted into the busy extent list.
  457. */
  458. STATIC int
  459. xfs_agfl_free_finish_item(
  460. struct xfs_trans *tp,
  461. struct xfs_log_item *done,
  462. struct list_head *item,
  463. struct xfs_btree_cur **state)
  464. {
  465. struct xfs_owner_info oinfo = { };
  466. struct xfs_mount *mp = tp->t_mountp;
  467. struct xfs_efd_log_item *efdp = EFD_ITEM(done);
  468. struct xfs_extent_free_item *xefi = xefi_entry(item);
  469. struct xfs_buf *agbp;
  470. int error;
  471. xfs_agblock_t agbno;
  472. ASSERT(xefi->xefi_blockcount == 1);
  473. agbno = XFS_FSB_TO_AGBNO(mp, xefi->xefi_startblock);
  474. oinfo.oi_owner = xefi->xefi_owner;
  475. trace_xfs_agfl_free_deferred(mp, xefi);
  476. error = xfs_alloc_read_agf(xefi->xefi_pag, tp, 0, &agbp);
  477. if (!error)
  478. error = xfs_free_ag_extent(tp, agbp, xefi->xefi_pag->pag_agno,
  479. agbno, 1, &oinfo, XFS_AG_RESV_AGFL);
  480. xfs_efd_add_extent(efdp, xefi);
  481. xfs_extent_free_cancel_item(&xefi->xefi_list);
  482. return error;
  483. }
  484. /* Is this recovered EFI ok? */
  485. static inline bool
  486. xfs_efi_validate_ext(
  487. struct xfs_mount *mp,
  488. struct xfs_extent *extp)
  489. {
  490. return xfs_verify_fsbext(mp, extp->ext_start, extp->ext_len);
  491. }
  492. static inline void
  493. xfs_efi_recover_work(
  494. struct xfs_mount *mp,
  495. struct xfs_defer_pending *dfp,
  496. struct xfs_extent *extp)
  497. {
  498. struct xfs_extent_free_item *xefi;
  499. xefi = kmem_cache_zalloc(xfs_extfree_item_cache,
  500. GFP_KERNEL | __GFP_NOFAIL);
  501. xefi->xefi_startblock = extp->ext_start;
  502. xefi->xefi_blockcount = extp->ext_len;
  503. xefi->xefi_agresv = XFS_AG_RESV_NONE;
  504. xefi->xefi_owner = XFS_RMAP_OWN_UNKNOWN;
  505. xefi->xefi_pag = xfs_perag_intent_get(mp, extp->ext_start);
  506. xfs_defer_add_item(dfp, &xefi->xefi_list);
  507. }
  508. /*
  509. * Process an extent free intent item that was recovered from
  510. * the log. We need to free the extents that it describes.
  511. */
  512. STATIC int
  513. xfs_extent_free_recover_work(
  514. struct xfs_defer_pending *dfp,
  515. struct list_head *capture_list)
  516. {
  517. struct xfs_trans_res resv;
  518. struct xfs_log_item *lip = dfp->dfp_intent;
  519. struct xfs_efi_log_item *efip = EFI_ITEM(lip);
  520. struct xfs_mount *mp = lip->li_log->l_mp;
  521. struct xfs_trans *tp;
  522. int i;
  523. int error = 0;
  524. /*
  525. * First check the validity of the extents described by the
  526. * EFI. If any are bad, then assume that all are bad and
  527. * just toss the EFI.
  528. */
  529. for (i = 0; i < efip->efi_format.efi_nextents; i++) {
  530. if (!xfs_efi_validate_ext(mp,
  531. &efip->efi_format.efi_extents[i])) {
  532. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  533. &efip->efi_format,
  534. sizeof(efip->efi_format));
  535. return -EFSCORRUPTED;
  536. }
  537. xfs_efi_recover_work(mp, dfp, &efip->efi_format.efi_extents[i]);
  538. }
  539. resv = xlog_recover_resv(&M_RES(mp)->tr_itruncate);
  540. error = xfs_trans_alloc(mp, &resv, 0, 0, 0, &tp);
  541. if (error)
  542. return error;
  543. error = xlog_recover_finish_intent(tp, dfp);
  544. if (error == -EFSCORRUPTED)
  545. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  546. &efip->efi_format,
  547. sizeof(efip->efi_format));
  548. if (error)
  549. goto abort_error;
  550. return xfs_defer_ops_capture_and_commit(tp, capture_list);
  551. abort_error:
  552. xfs_trans_cancel(tp);
  553. return error;
  554. }
  555. /* Relog an intent item to push the log tail forward. */
  556. static struct xfs_log_item *
  557. xfs_extent_free_relog_intent(
  558. struct xfs_trans *tp,
  559. struct xfs_log_item *intent,
  560. struct xfs_log_item *done_item)
  561. {
  562. struct xfs_efd_log_item *efdp = EFD_ITEM(done_item);
  563. struct xfs_efi_log_item *efip;
  564. struct xfs_extent *extp;
  565. unsigned int count;
  566. count = EFI_ITEM(intent)->efi_format.efi_nextents;
  567. extp = EFI_ITEM(intent)->efi_format.efi_extents;
  568. efdp->efd_next_extent = count;
  569. memcpy(efdp->efd_format.efd_extents, extp, count * sizeof(*extp));
  570. efip = xfs_efi_init(tp->t_mountp, count);
  571. memcpy(efip->efi_format.efi_extents, extp, count * sizeof(*extp));
  572. atomic_set(&efip->efi_next_extent, count);
  573. return &efip->efi_item;
  574. }
  575. const struct xfs_defer_op_type xfs_extent_free_defer_type = {
  576. .name = "extent_free",
  577. .max_items = XFS_EFI_MAX_FAST_EXTENTS,
  578. .create_intent = xfs_extent_free_create_intent,
  579. .abort_intent = xfs_extent_free_abort_intent,
  580. .create_done = xfs_extent_free_create_done,
  581. .finish_item = xfs_extent_free_finish_item,
  582. .cancel_item = xfs_extent_free_cancel_item,
  583. .recover_work = xfs_extent_free_recover_work,
  584. .relog_intent = xfs_extent_free_relog_intent,
  585. };
  586. /* sub-type with special handling for AGFL deferred frees */
  587. const struct xfs_defer_op_type xfs_agfl_free_defer_type = {
  588. .name = "agfl_free",
  589. .max_items = XFS_EFI_MAX_FAST_EXTENTS,
  590. .create_intent = xfs_extent_free_create_intent,
  591. .abort_intent = xfs_extent_free_abort_intent,
  592. .create_done = xfs_extent_free_create_done,
  593. .finish_item = xfs_agfl_free_finish_item,
  594. .cancel_item = xfs_extent_free_cancel_item,
  595. .recover_work = xfs_extent_free_recover_work,
  596. .relog_intent = xfs_extent_free_relog_intent,
  597. };
  598. STATIC bool
  599. xfs_efi_item_match(
  600. struct xfs_log_item *lip,
  601. uint64_t intent_id)
  602. {
  603. return EFI_ITEM(lip)->efi_format.efi_id == intent_id;
  604. }
  605. static const struct xfs_item_ops xfs_efi_item_ops = {
  606. .flags = XFS_ITEM_INTENT,
  607. .iop_size = xfs_efi_item_size,
  608. .iop_format = xfs_efi_item_format,
  609. .iop_unpin = xfs_efi_item_unpin,
  610. .iop_release = xfs_efi_item_release,
  611. .iop_match = xfs_efi_item_match,
  612. };
  613. /*
  614. * This routine is called to create an in-core extent free intent
  615. * item from the efi format structure which was logged on disk.
  616. * It allocates an in-core efi, copies the extents from the format
  617. * structure into it, and adds the efi to the AIL with the given
  618. * LSN.
  619. */
  620. STATIC int
  621. xlog_recover_efi_commit_pass2(
  622. struct xlog *log,
  623. struct list_head *buffer_list,
  624. struct xlog_recover_item *item,
  625. xfs_lsn_t lsn)
  626. {
  627. struct xfs_mount *mp = log->l_mp;
  628. struct xfs_efi_log_item *efip;
  629. struct xfs_efi_log_format *efi_formatp;
  630. int error;
  631. efi_formatp = item->ri_buf[0].i_addr;
  632. if (item->ri_buf[0].i_len < xfs_efi_log_format_sizeof(0)) {
  633. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  634. item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
  635. return -EFSCORRUPTED;
  636. }
  637. efip = xfs_efi_init(mp, efi_formatp->efi_nextents);
  638. error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
  639. if (error) {
  640. xfs_efi_item_free(efip);
  641. return error;
  642. }
  643. atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
  644. xlog_recover_intent_item(log, &efip->efi_item, lsn,
  645. &xfs_extent_free_defer_type);
  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. };