Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000,2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 20 | #include "xfs_shared.h" |
Christoph Hellwig | 4fb6e8a | 2014-11-28 14:25:04 +1100 | [diff] [blame] | 21 | #include "xfs_format.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 22 | #include "xfs_log_format.h" |
| 23 | #include "xfs_trans_resv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_mount.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 25 | #include "xfs_trans.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "xfs_trans_priv.h" |
| 27 | #include "xfs_extfree_item.h" |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 28 | #include "xfs_alloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * This routine is called to allocate an "extent free intention" |
| 32 | * log item that will hold nextents worth of extents. The |
| 33 | * caller must use all nextents extents, because we are not |
| 34 | * flexible about this at all. |
| 35 | */ |
| 36 | xfs_efi_log_item_t * |
| 37 | xfs_trans_get_efi(xfs_trans_t *tp, |
| 38 | uint nextents) |
| 39 | { |
| 40 | xfs_efi_log_item_t *efip; |
| 41 | |
| 42 | ASSERT(tp != NULL); |
| 43 | ASSERT(nextents > 0); |
| 44 | |
| 45 | efip = xfs_efi_init(tp->t_mountp, nextents); |
| 46 | ASSERT(efip != NULL); |
| 47 | |
| 48 | /* |
| 49 | * Get a log_item_desc to point at the new item. |
| 50 | */ |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 51 | xfs_trans_add_item(tp, &efip->efi_item); |
| 52 | return efip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* |
| 56 | * This routine is called to indicate that the described |
| 57 | * extent is to be logged as needing to be freed. It should |
| 58 | * be called once for each extent to be freed. |
| 59 | */ |
| 60 | void |
| 61 | xfs_trans_log_efi_extent(xfs_trans_t *tp, |
| 62 | xfs_efi_log_item_t *efip, |
| 63 | xfs_fsblock_t start_block, |
| 64 | xfs_extlen_t ext_len) |
| 65 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | uint next_extent; |
| 67 | xfs_extent_t *extp; |
| 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | tp->t_flags |= XFS_TRANS_DIRTY; |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 70 | efip->efi_item.li_desc->lid_flags |= XFS_LID_DIRTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Dave Chinner | b199c8a | 2010-12-20 11:59:49 +1100 | [diff] [blame] | 72 | /* |
| 73 | * atomic_inc_return gives us the value after the increment; |
| 74 | * we want to use it as an array index so we need to subtract 1 from |
| 75 | * it. |
| 76 | */ |
| 77 | next_extent = atomic_inc_return(&efip->efi_next_extent) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | ASSERT(next_extent < efip->efi_format.efi_nextents); |
| 79 | extp = &(efip->efi_format.efi_extents[next_extent]); |
| 80 | extp->ext_start = start_block; |
| 81 | extp->ext_len = ext_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | |
| 85 | /* |
| 86 | * This routine is called to allocate an "extent free done" |
| 87 | * log item that will hold nextents worth of extents. The |
| 88 | * caller must use all nextents extents, because we are not |
| 89 | * flexible about this at all. |
| 90 | */ |
| 91 | xfs_efd_log_item_t * |
| 92 | xfs_trans_get_efd(xfs_trans_t *tp, |
| 93 | xfs_efi_log_item_t *efip, |
| 94 | uint nextents) |
| 95 | { |
| 96 | xfs_efd_log_item_t *efdp; |
| 97 | |
| 98 | ASSERT(tp != NULL); |
| 99 | ASSERT(nextents > 0); |
| 100 | |
| 101 | efdp = xfs_efd_init(tp->t_mountp, efip, nextents); |
| 102 | ASSERT(efdp != NULL); |
| 103 | |
| 104 | /* |
| 105 | * Get a log_item_desc to point at the new item. |
| 106 | */ |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 107 | xfs_trans_add_item(tp, &efdp->efd_item); |
| 108 | return efdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 112 | * Free an extent and log it to the EFD. Note that the transaction is marked |
| 113 | * dirty regardless of whether the extent free succeeds or fails to support the |
| 114 | * EFI/EFD lifecycle rules. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | */ |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 116 | int |
| 117 | xfs_trans_free_extent( |
| 118 | struct xfs_trans *tp, |
| 119 | struct xfs_efd_log_item *efdp, |
| 120 | xfs_fsblock_t start_block, |
| 121 | xfs_extlen_t ext_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | uint next_extent; |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 124 | struct xfs_extent *extp; |
| 125 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 127 | error = xfs_free_extent(tp, start_block, ext_len); |
| 128 | |
| 129 | /* |
| 130 | * Mark the transaction dirty, even on error. This ensures the |
| 131 | * transaction is aborted, which: |
| 132 | * |
| 133 | * 1.) releases the EFI and frees the EFD |
| 134 | * 2.) shuts down the filesystem |
| 135 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | tp->t_flags |= XFS_TRANS_DIRTY; |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 137 | efdp->efd_item.li_desc->lid_flags |= XFS_LID_DIRTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | next_extent = efdp->efd_next_extent; |
| 140 | ASSERT(next_extent < efdp->efd_format.efd_nextents); |
| 141 | extp = &(efdp->efd_format.efd_extents[next_extent]); |
| 142 | extp->ext_start = start_block; |
| 143 | extp->ext_len = ext_len; |
| 144 | efdp->efd_next_extent++; |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 145 | |
| 146 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |