xfs_trans_extfree.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2000,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_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_mount.h"
  25. #include "xfs_trans.h"
  26. #include "xfs_trans_priv.h"
  27. #include "xfs_extfree_item.h"
  28. #include "xfs_alloc.h"
  29. /*
  30. * This routine is called to allocate an "extent free intention"
  31. * log item that will hold nextents worth of extents. The
  32. * caller must use all nextents extents, because we are not
  33. * flexible about this at all.
  34. */
  35. xfs_efi_log_item_t *
  36. xfs_trans_get_efi(xfs_trans_t *tp,
  37. uint nextents)
  38. {
  39. xfs_efi_log_item_t *efip;
  40. ASSERT(tp != NULL);
  41. ASSERT(nextents > 0);
  42. efip = xfs_efi_init(tp->t_mountp, nextents);
  43. ASSERT(efip != NULL);
  44. /*
  45. * Get a log_item_desc to point at the new item.
  46. */
  47. xfs_trans_add_item(tp, &efip->efi_item);
  48. return efip;
  49. }
  50. /*
  51. * This routine is called to indicate that the described
  52. * extent is to be logged as needing to be freed. It should
  53. * be called once for each extent to be freed.
  54. */
  55. void
  56. xfs_trans_log_efi_extent(xfs_trans_t *tp,
  57. xfs_efi_log_item_t *efip,
  58. xfs_fsblock_t start_block,
  59. xfs_extlen_t ext_len)
  60. {
  61. uint next_extent;
  62. xfs_extent_t *extp;
  63. tp->t_flags |= XFS_TRANS_DIRTY;
  64. efip->efi_item.li_desc->lid_flags |= XFS_LID_DIRTY;
  65. /*
  66. * atomic_inc_return gives us the value after the increment;
  67. * we want to use it as an array index so we need to subtract 1 from
  68. * it.
  69. */
  70. next_extent = atomic_inc_return(&efip->efi_next_extent) - 1;
  71. ASSERT(next_extent < efip->efi_format.efi_nextents);
  72. extp = &(efip->efi_format.efi_extents[next_extent]);
  73. extp->ext_start = start_block;
  74. extp->ext_len = ext_len;
  75. }
  76. /*
  77. * This routine is called to allocate an "extent free done"
  78. * log item that will hold nextents worth of extents. The
  79. * caller must use all nextents extents, because we are not
  80. * flexible about this at all.
  81. */
  82. xfs_efd_log_item_t *
  83. xfs_trans_get_efd(xfs_trans_t *tp,
  84. xfs_efi_log_item_t *efip,
  85. uint nextents)
  86. {
  87. xfs_efd_log_item_t *efdp;
  88. ASSERT(tp != NULL);
  89. ASSERT(nextents > 0);
  90. efdp = xfs_efd_init(tp->t_mountp, efip, nextents);
  91. ASSERT(efdp != NULL);
  92. /*
  93. * Get a log_item_desc to point at the new item.
  94. */
  95. xfs_trans_add_item(tp, &efdp->efd_item);
  96. return efdp;
  97. }
  98. /*
  99. * Free an extent and log it to the EFD. Note that the transaction is marked
  100. * dirty regardless of whether the extent free succeeds or fails to support the
  101. * EFI/EFD lifecycle rules.
  102. */
  103. int
  104. xfs_trans_free_extent(
  105. struct xfs_trans *tp,
  106. struct xfs_efd_log_item *efdp,
  107. xfs_fsblock_t start_block,
  108. xfs_extlen_t ext_len)
  109. {
  110. uint next_extent;
  111. struct xfs_extent *extp;
  112. int error;
  113. error = xfs_free_extent(tp, start_block, ext_len);
  114. /*
  115. * Mark the transaction dirty, even on error. This ensures the
  116. * transaction is aborted, which:
  117. *
  118. * 1.) releases the EFI and frees the EFD
  119. * 2.) shuts down the filesystem
  120. */
  121. tp->t_flags |= XFS_TRANS_DIRTY;
  122. efdp->efd_item.li_desc->lid_flags |= XFS_LID_DIRTY;
  123. next_extent = efdp->efd_next_extent;
  124. ASSERT(next_extent < efdp->efd_format.efd_nextents);
  125. extp = &(efdp->efd_format.efd_extents[next_extent]);
  126. extp->ext_start = start_block;
  127. extp->ext_len = ext_len;
  128. efdp->efd_next_extent++;
  129. return error;
  130. }