xfs_extfree_item.c 15 KB

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