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" |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 25 | #include "xfs_defer.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 26 | #include "xfs_trans.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_trans_priv.h" |
| 28 | #include "xfs_extfree_item.h" |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 29 | #include "xfs_alloc.h" |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 30 | #include "xfs_bmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | * This routine is called to allocate an "extent free done" |
| 34 | * log item that will hold nextents worth of extents. The |
| 35 | * caller must use all nextents extents, because we are not |
| 36 | * flexible about this at all. |
| 37 | */ |
Darrick J. Wong | bba61cb | 2016-08-03 11:13:47 +1000 | [diff] [blame] | 38 | struct xfs_efd_log_item * |
| 39 | xfs_trans_get_efd(struct xfs_trans *tp, |
| 40 | struct xfs_efi_log_item *efip, |
| 41 | uint nextents) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
Darrick J. Wong | bba61cb | 2016-08-03 11:13:47 +1000 | [diff] [blame] | 43 | struct xfs_efd_log_item *efdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | ASSERT(tp != NULL); |
| 46 | ASSERT(nextents > 0); |
| 47 | |
| 48 | efdp = xfs_efd_init(tp->t_mountp, efip, nextents); |
| 49 | ASSERT(efdp != NULL); |
| 50 | |
| 51 | /* |
| 52 | * Get a log_item_desc to point at the new item. |
| 53 | */ |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 54 | xfs_trans_add_item(tp, &efdp->efd_item); |
| 55 | return efdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 59 | * Free an extent and log it to the EFD. Note that the transaction is marked |
| 60 | * dirty regardless of whether the extent free succeeds or fails to support the |
| 61 | * EFI/EFD lifecycle rules. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | */ |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 63 | int |
| 64 | xfs_trans_free_extent( |
| 65 | struct xfs_trans *tp, |
| 66 | struct xfs_efd_log_item *efdp, |
| 67 | xfs_fsblock_t start_block, |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 68 | xfs_extlen_t ext_len, |
| 69 | struct xfs_owner_info *oinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | uint next_extent; |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 72 | struct xfs_extent *extp; |
| 73 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 75 | error = xfs_free_extent(tp, start_block, ext_len, oinfo); |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 76 | |
| 77 | /* |
| 78 | * Mark the transaction dirty, even on error. This ensures the |
| 79 | * transaction is aborted, which: |
| 80 | * |
| 81 | * 1.) releases the EFI and frees the EFD |
| 82 | * 2.) shuts down the filesystem |
| 83 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | tp->t_flags |= XFS_TRANS_DIRTY; |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 85 | efdp->efd_item.li_desc->lid_flags |= XFS_LID_DIRTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | next_extent = efdp->efd_next_extent; |
| 88 | ASSERT(next_extent < efdp->efd_format.efd_nextents); |
| 89 | extp = &(efdp->efd_format.efd_extents[next_extent]); |
| 90 | extp->ext_start = start_block; |
| 91 | extp->ext_len = ext_len; |
| 92 | efdp->efd_next_extent++; |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 93 | |
| 94 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 96 | |
| 97 | /* Sort bmap items by AG. */ |
| 98 | static int |
| 99 | xfs_extent_free_diff_items( |
| 100 | void *priv, |
| 101 | struct list_head *a, |
| 102 | struct list_head *b) |
| 103 | { |
| 104 | struct xfs_mount *mp = priv; |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 105 | struct xfs_extent_free_item *ra; |
| 106 | struct xfs_extent_free_item *rb; |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 107 | |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 108 | ra = container_of(a, struct xfs_extent_free_item, xefi_list); |
| 109 | rb = container_of(b, struct xfs_extent_free_item, xefi_list); |
| 110 | return XFS_FSB_TO_AGNO(mp, ra->xefi_startblock) - |
| 111 | XFS_FSB_TO_AGNO(mp, rb->xefi_startblock); |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* Get an EFI. */ |
| 115 | STATIC void * |
| 116 | xfs_extent_free_create_intent( |
| 117 | struct xfs_trans *tp, |
| 118 | unsigned int count) |
| 119 | { |
Darrick J. Wong | 51ce9d0 | 2016-08-03 12:30:31 +1000 | [diff] [blame^] | 120 | struct xfs_efi_log_item *efip; |
| 121 | |
| 122 | ASSERT(tp != NULL); |
| 123 | ASSERT(count > 0); |
| 124 | |
| 125 | efip = xfs_efi_init(tp->t_mountp, count); |
| 126 | ASSERT(efip != NULL); |
| 127 | |
| 128 | /* |
| 129 | * Get a log_item_desc to point at the new item. |
| 130 | */ |
| 131 | xfs_trans_add_item(tp, &efip->efi_item); |
| 132 | return efip; |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* Log a free extent to the intent item. */ |
| 136 | STATIC void |
| 137 | xfs_extent_free_log_item( |
| 138 | struct xfs_trans *tp, |
| 139 | void *intent, |
| 140 | struct list_head *item) |
| 141 | { |
Darrick J. Wong | 51ce9d0 | 2016-08-03 12:30:31 +1000 | [diff] [blame^] | 142 | struct xfs_efi_log_item *efip = intent; |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 143 | struct xfs_extent_free_item *free; |
Darrick J. Wong | 51ce9d0 | 2016-08-03 12:30:31 +1000 | [diff] [blame^] | 144 | uint next_extent; |
| 145 | struct xfs_extent *extp; |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 146 | |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 147 | free = container_of(item, struct xfs_extent_free_item, xefi_list); |
Darrick J. Wong | 51ce9d0 | 2016-08-03 12:30:31 +1000 | [diff] [blame^] | 148 | |
| 149 | tp->t_flags |= XFS_TRANS_DIRTY; |
| 150 | efip->efi_item.li_desc->lid_flags |= XFS_LID_DIRTY; |
| 151 | |
| 152 | /* |
| 153 | * atomic_inc_return gives us the value after the increment; |
| 154 | * we want to use it as an array index so we need to subtract 1 from |
| 155 | * it. |
| 156 | */ |
| 157 | next_extent = atomic_inc_return(&efip->efi_next_extent) - 1; |
| 158 | ASSERT(next_extent < efip->efi_format.efi_nextents); |
| 159 | extp = &efip->efi_format.efi_extents[next_extent]; |
| 160 | extp->ext_start = free->xefi_startblock; |
| 161 | extp->ext_len = free->xefi_blockcount; |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* Get an EFD so we can process all the free extents. */ |
| 165 | STATIC void * |
| 166 | xfs_extent_free_create_done( |
| 167 | struct xfs_trans *tp, |
| 168 | void *intent, |
| 169 | unsigned int count) |
| 170 | { |
| 171 | return xfs_trans_get_efd(tp, intent, count); |
| 172 | } |
| 173 | |
| 174 | /* Process a free extent. */ |
| 175 | STATIC int |
| 176 | xfs_extent_free_finish_item( |
| 177 | struct xfs_trans *tp, |
| 178 | struct xfs_defer_ops *dop, |
| 179 | struct list_head *item, |
| 180 | void *done_item, |
| 181 | void **state) |
| 182 | { |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 183 | struct xfs_extent_free_item *free; |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 184 | int error; |
| 185 | |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 186 | free = container_of(item, struct xfs_extent_free_item, xefi_list); |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 187 | error = xfs_trans_free_extent(tp, done_item, |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 188 | free->xefi_startblock, |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 189 | free->xefi_blockcount, |
| 190 | &free->xefi_oinfo); |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 191 | kmem_free(free); |
| 192 | return error; |
| 193 | } |
| 194 | |
| 195 | /* Abort all pending EFIs. */ |
| 196 | STATIC void |
| 197 | xfs_extent_free_abort_intent( |
| 198 | void *intent) |
| 199 | { |
| 200 | xfs_efi_release(intent); |
| 201 | } |
| 202 | |
| 203 | /* Cancel a free extent. */ |
| 204 | STATIC void |
| 205 | xfs_extent_free_cancel_item( |
| 206 | struct list_head *item) |
| 207 | { |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 208 | struct xfs_extent_free_item *free; |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 209 | |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 210 | free = container_of(item, struct xfs_extent_free_item, xefi_list); |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 211 | kmem_free(free); |
| 212 | } |
| 213 | |
| 214 | static const struct xfs_defer_op_type xfs_extent_free_defer_type = { |
| 215 | .type = XFS_DEFER_OPS_TYPE_FREE, |
| 216 | .max_items = XFS_EFI_MAX_FAST_EXTENTS, |
| 217 | .diff_items = xfs_extent_free_diff_items, |
| 218 | .create_intent = xfs_extent_free_create_intent, |
| 219 | .abort_intent = xfs_extent_free_abort_intent, |
| 220 | .log_item = xfs_extent_free_log_item, |
| 221 | .create_done = xfs_extent_free_create_done, |
| 222 | .finish_item = xfs_extent_free_finish_item, |
| 223 | .cancel_item = xfs_extent_free_cancel_item, |
| 224 | }; |
| 225 | |
| 226 | /* Register the deferred op type. */ |
| 227 | void |
| 228 | xfs_extent_free_init_defer_op(void) |
| 229 | { |
| 230 | xfs_defer_init_op_type(&xfs_extent_free_defer_type); |
| 231 | } |