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-2002,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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_types.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 22 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "xfs_trans.h" |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 24 | #include "xfs_trans_priv.h" |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 25 | /* XXX: from here down needed until struct xfs_trans has its own ailp */ |
David Chinner | fc1829f | 2008-10-30 17:39:46 +1100 | [diff] [blame] | 26 | #include "xfs_bit.h" |
| 27 | #include "xfs_buf_item.h" |
| 28 | #include "xfs_sb.h" |
| 29 | #include "xfs_ag.h" |
| 30 | #include "xfs_dir2.h" |
| 31 | #include "xfs_dmapi.h" |
| 32 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | STATIC int xfs_trans_unlock_chunk(xfs_log_item_chunk_t *, |
| 35 | int, int, xfs_lsn_t); |
| 36 | |
| 37 | /* |
| 38 | * This is called to add the given log item to the transaction's |
| 39 | * list of log items. It must find a free log item descriptor |
| 40 | * or allocate a new one and add the item to that descriptor. |
| 41 | * The function returns a pointer to item descriptor used to point |
| 42 | * to the new item. The log item will now point to its new descriptor |
| 43 | * with its li_desc field. |
| 44 | */ |
| 45 | xfs_log_item_desc_t * |
| 46 | xfs_trans_add_item(xfs_trans_t *tp, xfs_log_item_t *lip) |
| 47 | { |
| 48 | xfs_log_item_desc_t *lidp; |
| 49 | xfs_log_item_chunk_t *licp; |
| 50 | int i=0; |
| 51 | |
| 52 | /* |
| 53 | * If there are no free descriptors, allocate a new chunk |
| 54 | * of them and put it at the front of the chunk list. |
| 55 | */ |
| 56 | if (tp->t_items_free == 0) { |
| 57 | licp = (xfs_log_item_chunk_t*) |
| 58 | kmem_alloc(sizeof(xfs_log_item_chunk_t), KM_SLEEP); |
| 59 | ASSERT(licp != NULL); |
| 60 | /* |
| 61 | * Initialize the chunk, and then |
| 62 | * claim the first slot in the newly allocated chunk. |
| 63 | */ |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 64 | xfs_lic_init(licp); |
| 65 | xfs_lic_claim(licp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | licp->lic_unused = 1; |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 67 | xfs_lic_init_slot(licp, 0); |
| 68 | lidp = xfs_lic_slot(licp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | * Link in the new chunk and update the free count. |
| 72 | */ |
| 73 | licp->lic_next = tp->t_items.lic_next; |
| 74 | tp->t_items.lic_next = licp; |
| 75 | tp->t_items_free = XFS_LIC_NUM_SLOTS - 1; |
| 76 | |
| 77 | /* |
| 78 | * Initialize the descriptor and the generic portion |
| 79 | * of the log item. |
| 80 | * |
| 81 | * Point the new slot at this item and return it. |
| 82 | * Also point the log item at its currently active |
| 83 | * descriptor and set the item's mount pointer. |
| 84 | */ |
| 85 | lidp->lid_item = lip; |
| 86 | lidp->lid_flags = 0; |
| 87 | lidp->lid_size = 0; |
| 88 | lip->li_desc = lidp; |
| 89 | lip->li_mountp = tp->t_mountp; |
David Chinner | fc1829f | 2008-10-30 17:39:46 +1100 | [diff] [blame] | 90 | lip->li_ailp = tp->t_mountp->m_ail; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 91 | return lidp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Find the free descriptor. It is somewhere in the chunklist |
| 96 | * of descriptors. |
| 97 | */ |
| 98 | licp = &tp->t_items; |
| 99 | while (licp != NULL) { |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 100 | if (xfs_lic_vacancy(licp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | if (licp->lic_unused <= XFS_LIC_MAX_SLOT) { |
| 102 | i = licp->lic_unused; |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 103 | ASSERT(xfs_lic_isfree(licp, i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | break; |
| 105 | } |
| 106 | for (i = 0; i <= XFS_LIC_MAX_SLOT; i++) { |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 107 | if (xfs_lic_isfree(licp, i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | break; |
| 109 | } |
| 110 | ASSERT(i <= XFS_LIC_MAX_SLOT); |
| 111 | break; |
| 112 | } |
| 113 | licp = licp->lic_next; |
| 114 | } |
| 115 | ASSERT(licp != NULL); |
| 116 | /* |
| 117 | * If we find a free descriptor, claim it, |
| 118 | * initialize it, and return it. |
| 119 | */ |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 120 | xfs_lic_claim(licp, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | if (licp->lic_unused <= i) { |
| 122 | licp->lic_unused = i + 1; |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 123 | xfs_lic_init_slot(licp, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 125 | lidp = xfs_lic_slot(licp, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | tp->t_items_free--; |
| 127 | lidp->lid_item = lip; |
| 128 | lidp->lid_flags = 0; |
| 129 | lidp->lid_size = 0; |
| 130 | lip->li_desc = lidp; |
| 131 | lip->li_mountp = tp->t_mountp; |
David Chinner | fc1829f | 2008-10-30 17:39:46 +1100 | [diff] [blame] | 132 | lip->li_ailp = tp->t_mountp->m_ail; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 133 | return lidp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Free the given descriptor. |
| 138 | * |
| 139 | * This requires setting the bit in the chunk's free mask corresponding |
| 140 | * to the given slot. |
| 141 | */ |
| 142 | void |
| 143 | xfs_trans_free_item(xfs_trans_t *tp, xfs_log_item_desc_t *lidp) |
| 144 | { |
| 145 | uint slot; |
| 146 | xfs_log_item_chunk_t *licp; |
| 147 | xfs_log_item_chunk_t **licpp; |
| 148 | |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 149 | slot = xfs_lic_desc_to_slot(lidp); |
| 150 | licp = xfs_lic_desc_to_chunk(lidp); |
| 151 | xfs_lic_relse(licp, slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | lidp->lid_item->li_desc = NULL; |
| 153 | tp->t_items_free++; |
| 154 | |
| 155 | /* |
| 156 | * If there are no more used items in the chunk and this is not |
| 157 | * the chunk embedded in the transaction structure, then free |
| 158 | * the chunk. First pull it from the chunk list and then |
| 159 | * free it back to the heap. We didn't bother with a doubly |
| 160 | * linked list here because the lists should be very short |
| 161 | * and this is not a performance path. It's better to save |
| 162 | * the memory of the extra pointer. |
| 163 | * |
| 164 | * Also decrement the transaction structure's count of free items |
| 165 | * by the number in a chunk since we are freeing an empty chunk. |
| 166 | */ |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 167 | if (xfs_lic_are_all_free(licp) && (licp != &(tp->t_items))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | licpp = &(tp->t_items.lic_next); |
| 169 | while (*licpp != licp) { |
| 170 | ASSERT(*licpp != NULL); |
| 171 | licpp = &((*licpp)->lic_next); |
| 172 | } |
| 173 | *licpp = licp->lic_next; |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 174 | kmem_free(licp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | tp->t_items_free -= XFS_LIC_NUM_SLOTS; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * This is called to find the descriptor corresponding to the given |
| 181 | * log item. It returns a pointer to the descriptor. |
| 182 | * The log item MUST have a corresponding descriptor in the given |
| 183 | * transaction. This routine does not return NULL, it panics. |
| 184 | * |
| 185 | * The descriptor pointer is kept in the log item's li_desc field. |
| 186 | * Just return it. |
| 187 | */ |
| 188 | /*ARGSUSED*/ |
| 189 | xfs_log_item_desc_t * |
| 190 | xfs_trans_find_item(xfs_trans_t *tp, xfs_log_item_t *lip) |
| 191 | { |
| 192 | ASSERT(lip->li_desc != NULL); |
| 193 | |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 194 | return lip->li_desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | |
| 198 | /* |
| 199 | * Return a pointer to the first descriptor in the chunk list. |
| 200 | * This does not return NULL if there are none, it panics. |
| 201 | * |
| 202 | * The first descriptor must be in either the first or second chunk. |
| 203 | * This is because the only chunk allowed to be empty is the first. |
| 204 | * All others are freed when they become empty. |
| 205 | * |
| 206 | * At some point this and xfs_trans_next_item() should be optimized |
| 207 | * to quickly look at the mask to determine if there is anything to |
| 208 | * look at. |
| 209 | */ |
| 210 | xfs_log_item_desc_t * |
| 211 | xfs_trans_first_item(xfs_trans_t *tp) |
| 212 | { |
| 213 | xfs_log_item_chunk_t *licp; |
| 214 | int i; |
| 215 | |
| 216 | licp = &tp->t_items; |
| 217 | /* |
| 218 | * If it's not in the first chunk, skip to the second. |
| 219 | */ |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 220 | if (xfs_lic_are_all_free(licp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | licp = licp->lic_next; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Return the first non-free descriptor in the chunk. |
| 226 | */ |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 227 | ASSERT(!xfs_lic_are_all_free(licp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | for (i = 0; i < licp->lic_unused; i++) { |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 229 | if (xfs_lic_isfree(licp, i)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | continue; |
| 231 | } |
| 232 | |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 233 | return xfs_lic_slot(licp, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
| 235 | cmn_err(CE_WARN, "xfs_trans_first_item() -- no first item"); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 236 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | |
| 240 | /* |
| 241 | * Given a descriptor, return the next descriptor in the chunk list. |
| 242 | * This returns NULL if there are no more used descriptors in the list. |
| 243 | * |
| 244 | * We do this by first locating the chunk in which the descriptor resides, |
| 245 | * and then scanning forward in the chunk and the list for the next |
| 246 | * used descriptor. |
| 247 | */ |
| 248 | /*ARGSUSED*/ |
| 249 | xfs_log_item_desc_t * |
| 250 | xfs_trans_next_item(xfs_trans_t *tp, xfs_log_item_desc_t *lidp) |
| 251 | { |
| 252 | xfs_log_item_chunk_t *licp; |
| 253 | int i; |
| 254 | |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 255 | licp = xfs_lic_desc_to_chunk(lidp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | /* |
| 258 | * First search the rest of the chunk. The for loop keeps us |
| 259 | * from referencing things beyond the end of the chunk. |
| 260 | */ |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 261 | for (i = (int)xfs_lic_desc_to_slot(lidp) + 1; i < licp->lic_unused; i++) { |
| 262 | if (xfs_lic_isfree(licp, i)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | continue; |
| 264 | } |
| 265 | |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 266 | return xfs_lic_slot(licp, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /* |
| 270 | * Now search the next chunk. It must be there, because the |
| 271 | * next chunk would have been freed if it were empty. |
| 272 | * If there is no next chunk, return NULL. |
| 273 | */ |
| 274 | if (licp->lic_next == NULL) { |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 275 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | licp = licp->lic_next; |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 279 | ASSERT(!xfs_lic_are_all_free(licp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | for (i = 0; i < licp->lic_unused; i++) { |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 281 | if (xfs_lic_isfree(licp, i)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | continue; |
| 283 | } |
| 284 | |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 285 | return xfs_lic_slot(licp, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | ASSERT(0); |
| 288 | /* NOTREACHED */ |
| 289 | return NULL; /* keep gcc quite */ |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * This is called to unlock all of the items of a transaction and to free |
| 294 | * all the descriptors of that transaction. |
| 295 | * |
| 296 | * It walks the list of descriptors and unlocks each item. It frees |
| 297 | * each chunk except that embedded in the transaction as it goes along. |
| 298 | */ |
| 299 | void |
| 300 | xfs_trans_free_items( |
| 301 | xfs_trans_t *tp, |
| 302 | int flags) |
| 303 | { |
| 304 | xfs_log_item_chunk_t *licp; |
| 305 | xfs_log_item_chunk_t *next_licp; |
| 306 | int abort; |
| 307 | |
| 308 | abort = flags & XFS_TRANS_ABORT; |
| 309 | licp = &tp->t_items; |
| 310 | /* |
| 311 | * Special case the embedded chunk so we don't free it below. |
| 312 | */ |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 313 | if (!xfs_lic_are_all_free(licp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | (void) xfs_trans_unlock_chunk(licp, 1, abort, NULLCOMMITLSN); |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 315 | xfs_lic_all_free(licp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | licp->lic_unused = 0; |
| 317 | } |
| 318 | licp = licp->lic_next; |
| 319 | |
| 320 | /* |
| 321 | * Unlock each item in each chunk and free the chunks. |
| 322 | */ |
| 323 | while (licp != NULL) { |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 324 | ASSERT(!xfs_lic_are_all_free(licp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | (void) xfs_trans_unlock_chunk(licp, 1, abort, NULLCOMMITLSN); |
| 326 | next_licp = licp->lic_next; |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 327 | kmem_free(licp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | licp = next_licp; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Reset the transaction structure's free item count. |
| 333 | */ |
| 334 | tp->t_items_free = XFS_LIC_NUM_SLOTS; |
| 335 | tp->t_items.lic_next = NULL; |
| 336 | } |
| 337 | |
| 338 | |
| 339 | |
| 340 | /* |
| 341 | * This is called to unlock the items associated with a transaction. |
| 342 | * Items which were not logged should be freed. |
| 343 | * Those which were logged must still be tracked so they can be unpinned |
| 344 | * when the transaction commits. |
| 345 | */ |
| 346 | void |
| 347 | xfs_trans_unlock_items(xfs_trans_t *tp, xfs_lsn_t commit_lsn) |
| 348 | { |
| 349 | xfs_log_item_chunk_t *licp; |
| 350 | xfs_log_item_chunk_t *next_licp; |
| 351 | xfs_log_item_chunk_t **licpp; |
| 352 | int freed; |
| 353 | |
| 354 | freed = 0; |
| 355 | licp = &tp->t_items; |
| 356 | |
| 357 | /* |
| 358 | * Special case the embedded chunk so we don't free. |
| 359 | */ |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 360 | if (!xfs_lic_are_all_free(licp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | freed = xfs_trans_unlock_chunk(licp, 0, 0, commit_lsn); |
| 362 | } |
| 363 | licpp = &(tp->t_items.lic_next); |
| 364 | licp = licp->lic_next; |
| 365 | |
| 366 | /* |
| 367 | * Unlock each item in each chunk, free non-dirty descriptors, |
| 368 | * and free empty chunks. |
| 369 | */ |
| 370 | while (licp != NULL) { |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 371 | ASSERT(!xfs_lic_are_all_free(licp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | freed += xfs_trans_unlock_chunk(licp, 0, 0, commit_lsn); |
| 373 | next_licp = licp->lic_next; |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 374 | if (xfs_lic_are_all_free(licp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | *licpp = next_licp; |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 376 | kmem_free(licp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | freed -= XFS_LIC_NUM_SLOTS; |
| 378 | } else { |
| 379 | licpp = &(licp->lic_next); |
| 380 | } |
| 381 | ASSERT(*licpp == next_licp); |
| 382 | licp = next_licp; |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | * Fix the free descriptor count in the transaction. |
| 387 | */ |
| 388 | tp->t_items_free += freed; |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Unlock each item pointed to by a descriptor in the given chunk. |
| 393 | * Stamp the commit lsn into each item if necessary. |
| 394 | * Free descriptors pointing to items which are not dirty if freeing_chunk |
| 395 | * is zero. If freeing_chunk is non-zero, then we need to unlock all |
| 396 | * items in the chunk. |
| 397 | * |
| 398 | * Return the number of descriptors freed. |
| 399 | */ |
| 400 | STATIC int |
| 401 | xfs_trans_unlock_chunk( |
| 402 | xfs_log_item_chunk_t *licp, |
| 403 | int freeing_chunk, |
| 404 | int abort, |
| 405 | xfs_lsn_t commit_lsn) |
| 406 | { |
| 407 | xfs_log_item_desc_t *lidp; |
| 408 | xfs_log_item_t *lip; |
| 409 | int i; |
| 410 | int freed; |
| 411 | |
| 412 | freed = 0; |
| 413 | lidp = licp->lic_descs; |
| 414 | for (i = 0; i < licp->lic_unused; i++, lidp++) { |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 415 | if (xfs_lic_isfree(licp, i)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | continue; |
| 417 | } |
| 418 | lip = lidp->lid_item; |
| 419 | lip->li_desc = NULL; |
| 420 | |
| 421 | if (commit_lsn != NULLCOMMITLSN) |
| 422 | IOP_COMMITTING(lip, commit_lsn); |
| 423 | if (abort) |
| 424 | lip->li_flags |= XFS_LI_ABORTED; |
| 425 | IOP_UNLOCK(lip); |
| 426 | |
| 427 | /* |
| 428 | * Free the descriptor if the item is not dirty |
| 429 | * within this transaction and the caller is not |
| 430 | * going to just free the entire thing regardless. |
| 431 | */ |
| 432 | if (!(freeing_chunk) && |
| 433 | (!(lidp->lid_flags & XFS_LID_DIRTY) || abort)) { |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 434 | xfs_lic_relse(licp, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | freed++; |
| 436 | } |
| 437 | } |
| 438 | |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 439 | return freed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | |
| 443 | /* |
| 444 | * This is called to add the given busy item to the transaction's |
| 445 | * list of busy items. It must find a free busy item descriptor |
| 446 | * or allocate a new one and add the item to that descriptor. |
| 447 | * The function returns a pointer to busy descriptor used to point |
| 448 | * to the new busy entry. The log busy entry will now point to its new |
| 449 | * descriptor with its ???? field. |
| 450 | */ |
| 451 | xfs_log_busy_slot_t * |
| 452 | xfs_trans_add_busy(xfs_trans_t *tp, xfs_agnumber_t ag, xfs_extlen_t idx) |
| 453 | { |
| 454 | xfs_log_busy_chunk_t *lbcp; |
| 455 | xfs_log_busy_slot_t *lbsp; |
| 456 | int i=0; |
| 457 | |
| 458 | /* |
| 459 | * If there are no free descriptors, allocate a new chunk |
| 460 | * of them and put it at the front of the chunk list. |
| 461 | */ |
| 462 | if (tp->t_busy_free == 0) { |
| 463 | lbcp = (xfs_log_busy_chunk_t*) |
| 464 | kmem_alloc(sizeof(xfs_log_busy_chunk_t), KM_SLEEP); |
| 465 | ASSERT(lbcp != NULL); |
| 466 | /* |
| 467 | * Initialize the chunk, and then |
| 468 | * claim the first slot in the newly allocated chunk. |
| 469 | */ |
| 470 | XFS_LBC_INIT(lbcp); |
| 471 | XFS_LBC_CLAIM(lbcp, 0); |
| 472 | lbcp->lbc_unused = 1; |
| 473 | lbsp = XFS_LBC_SLOT(lbcp, 0); |
| 474 | |
| 475 | /* |
| 476 | * Link in the new chunk and update the free count. |
| 477 | */ |
| 478 | lbcp->lbc_next = tp->t_busy.lbc_next; |
| 479 | tp->t_busy.lbc_next = lbcp; |
| 480 | tp->t_busy_free = XFS_LIC_NUM_SLOTS - 1; |
| 481 | |
| 482 | /* |
| 483 | * Initialize the descriptor and the generic portion |
| 484 | * of the log item. |
| 485 | * |
| 486 | * Point the new slot at this item and return it. |
| 487 | * Also point the log item at its currently active |
| 488 | * descriptor and set the item's mount pointer. |
| 489 | */ |
| 490 | lbsp->lbc_ag = ag; |
| 491 | lbsp->lbc_idx = idx; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 492 | return lbsp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /* |
| 496 | * Find the free descriptor. It is somewhere in the chunklist |
| 497 | * of descriptors. |
| 498 | */ |
| 499 | lbcp = &tp->t_busy; |
| 500 | while (lbcp != NULL) { |
| 501 | if (XFS_LBC_VACANCY(lbcp)) { |
| 502 | if (lbcp->lbc_unused <= XFS_LBC_MAX_SLOT) { |
| 503 | i = lbcp->lbc_unused; |
| 504 | break; |
| 505 | } else { |
| 506 | /* out-of-order vacancy */ |
Nathan Scott | b657452 | 2006-06-09 15:29:40 +1000 | [diff] [blame] | 507 | cmn_err(CE_DEBUG, "OOO vacancy lbcp 0x%p\n", lbcp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | ASSERT(0); |
| 509 | } |
| 510 | } |
| 511 | lbcp = lbcp->lbc_next; |
| 512 | } |
| 513 | ASSERT(lbcp != NULL); |
| 514 | /* |
| 515 | * If we find a free descriptor, claim it, |
| 516 | * initialize it, and return it. |
| 517 | */ |
| 518 | XFS_LBC_CLAIM(lbcp, i); |
| 519 | if (lbcp->lbc_unused <= i) { |
| 520 | lbcp->lbc_unused = i + 1; |
| 521 | } |
| 522 | lbsp = XFS_LBC_SLOT(lbcp, i); |
| 523 | tp->t_busy_free--; |
| 524 | lbsp->lbc_ag = ag; |
| 525 | lbsp->lbc_idx = idx; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 526 | return lbsp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | |
| 530 | /* |
| 531 | * xfs_trans_free_busy |
| 532 | * Free all of the busy lists from a transaction |
| 533 | */ |
| 534 | void |
| 535 | xfs_trans_free_busy(xfs_trans_t *tp) |
| 536 | { |
| 537 | xfs_log_busy_chunk_t *lbcp; |
| 538 | xfs_log_busy_chunk_t *lbcq; |
| 539 | |
| 540 | lbcp = tp->t_busy.lbc_next; |
| 541 | while (lbcp != NULL) { |
| 542 | lbcq = lbcp->lbc_next; |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 543 | kmem_free(lbcp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | lbcp = lbcq; |
| 545 | } |
| 546 | |
| 547 | XFS_LBC_INIT(&tp->t_busy); |
| 548 | tp->t_busy.lbc_unused = 0; |
| 549 | } |