2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
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.
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.
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
20 #include "xfs_types.h"
23 #include "xfs_trans.h"
24 #include "xfs_trans_priv.h"
26 STATIC int xfs_trans_unlock_chunk(xfs_log_item_chunk_t *,
30 * This is called to add the given log item to the transaction's
31 * list of log items. It must find a free log item descriptor
32 * or allocate a new one and add the item to that descriptor.
33 * The function returns a pointer to item descriptor used to point
34 * to the new item. The log item will now point to its new descriptor
35 * with its li_desc field.
38 xfs_trans_add_item(xfs_trans_t *tp, xfs_log_item_t *lip)
40 xfs_log_item_desc_t *lidp;
41 xfs_log_item_chunk_t *licp;
45 * If there are no free descriptors, allocate a new chunk
46 * of them and put it at the front of the chunk list.
48 if (tp->t_items_free == 0) {
49 licp = (xfs_log_item_chunk_t*)
50 kmem_alloc(sizeof(xfs_log_item_chunk_t), KM_SLEEP);
53 * Initialize the chunk, and then
54 * claim the first slot in the newly allocated chunk.
57 XFS_LIC_CLAIM(licp, 0);
59 XFS_LIC_INIT_SLOT(licp, 0);
60 lidp = XFS_LIC_SLOT(licp, 0);
63 * Link in the new chunk and update the free count.
65 licp->lic_next = tp->t_items.lic_next;
66 tp->t_items.lic_next = licp;
67 tp->t_items_free = XFS_LIC_NUM_SLOTS - 1;
70 * Initialize the descriptor and the generic portion
73 * Point the new slot at this item and return it.
74 * Also point the log item at its currently active
75 * descriptor and set the item's mount pointer.
81 lip->li_mountp = tp->t_mountp;
86 * Find the free descriptor. It is somewhere in the chunklist
90 while (licp != NULL) {
91 if (XFS_LIC_VACANCY(licp)) {
92 if (licp->lic_unused <= XFS_LIC_MAX_SLOT) {
94 ASSERT(XFS_LIC_ISFREE(licp, i));
97 for (i = 0; i <= XFS_LIC_MAX_SLOT; i++) {
98 if (XFS_LIC_ISFREE(licp, i))
101 ASSERT(i <= XFS_LIC_MAX_SLOT);
104 licp = licp->lic_next;
106 ASSERT(licp != NULL);
108 * If we find a free descriptor, claim it,
109 * initialize it, and return it.
111 XFS_LIC_CLAIM(licp, i);
112 if (licp->lic_unused <= i) {
113 licp->lic_unused = i + 1;
114 XFS_LIC_INIT_SLOT(licp, i);
116 lidp = XFS_LIC_SLOT(licp, i);
118 lidp->lid_item = lip;
122 lip->li_mountp = tp->t_mountp;
127 * Free the given descriptor.
129 * This requires setting the bit in the chunk's free mask corresponding
133 xfs_trans_free_item(xfs_trans_t *tp, xfs_log_item_desc_t *lidp)
136 xfs_log_item_chunk_t *licp;
137 xfs_log_item_chunk_t **licpp;
139 slot = XFS_LIC_DESC_TO_SLOT(lidp);
140 licp = XFS_LIC_DESC_TO_CHUNK(lidp);
141 XFS_LIC_RELSE(licp, slot);
142 lidp->lid_item->li_desc = NULL;
146 * If there are no more used items in the chunk and this is not
147 * the chunk embedded in the transaction structure, then free
148 * the chunk. First pull it from the chunk list and then
149 * free it back to the heap. We didn't bother with a doubly
150 * linked list here because the lists should be very short
151 * and this is not a performance path. It's better to save
152 * the memory of the extra pointer.
154 * Also decrement the transaction structure's count of free items
155 * by the number in a chunk since we are freeing an empty chunk.
157 if (XFS_LIC_ARE_ALL_FREE(licp) && (licp != &(tp->t_items))) {
158 licpp = &(tp->t_items.lic_next);
159 while (*licpp != licp) {
160 ASSERT(*licpp != NULL);
161 licpp = &((*licpp)->lic_next);
163 *licpp = licp->lic_next;
164 kmem_free(licp, sizeof(xfs_log_item_chunk_t));
165 tp->t_items_free -= XFS_LIC_NUM_SLOTS;
170 * This is called to find the descriptor corresponding to the given
171 * log item. It returns a pointer to the descriptor.
172 * The log item MUST have a corresponding descriptor in the given
173 * transaction. This routine does not return NULL, it panics.
175 * The descriptor pointer is kept in the log item's li_desc field.
179 xfs_log_item_desc_t *
180 xfs_trans_find_item(xfs_trans_t *tp, xfs_log_item_t *lip)
182 ASSERT(lip->li_desc != NULL);
189 * Return a pointer to the first descriptor in the chunk list.
190 * This does not return NULL if there are none, it panics.
192 * The first descriptor must be in either the first or second chunk.
193 * This is because the only chunk allowed to be empty is the first.
194 * All others are freed when they become empty.
196 * At some point this and xfs_trans_next_item() should be optimized
197 * to quickly look at the mask to determine if there is anything to
200 xfs_log_item_desc_t *
201 xfs_trans_first_item(xfs_trans_t *tp)
203 xfs_log_item_chunk_t *licp;
208 * If it's not in the first chunk, skip to the second.
210 if (XFS_LIC_ARE_ALL_FREE(licp)) {
211 licp = licp->lic_next;
215 * Return the first non-free descriptor in the chunk.
217 ASSERT(!XFS_LIC_ARE_ALL_FREE(licp));
218 for (i = 0; i < licp->lic_unused; i++) {
219 if (XFS_LIC_ISFREE(licp, i)) {
223 return XFS_LIC_SLOT(licp, i);
225 cmn_err(CE_WARN, "xfs_trans_first_item() -- no first item");
231 * Given a descriptor, return the next descriptor in the chunk list.
232 * This returns NULL if there are no more used descriptors in the list.
234 * We do this by first locating the chunk in which the descriptor resides,
235 * and then scanning forward in the chunk and the list for the next
239 xfs_log_item_desc_t *
240 xfs_trans_next_item(xfs_trans_t *tp, xfs_log_item_desc_t *lidp)
242 xfs_log_item_chunk_t *licp;
245 licp = XFS_LIC_DESC_TO_CHUNK(lidp);
248 * First search the rest of the chunk. The for loop keeps us
249 * from referencing things beyond the end of the chunk.
251 for (i = (int)XFS_LIC_DESC_TO_SLOT(lidp) + 1; i < licp->lic_unused; i++) {
252 if (XFS_LIC_ISFREE(licp, i)) {
256 return XFS_LIC_SLOT(licp, i);
260 * Now search the next chunk. It must be there, because the
261 * next chunk would have been freed if it were empty.
262 * If there is no next chunk, return NULL.
264 if (licp->lic_next == NULL) {
268 licp = licp->lic_next;
269 ASSERT(!XFS_LIC_ARE_ALL_FREE(licp));
270 for (i = 0; i < licp->lic_unused; i++) {
271 if (XFS_LIC_ISFREE(licp, i)) {
275 return XFS_LIC_SLOT(licp, i);
279 return NULL; /* keep gcc quite */
283 * This is called to unlock all of the items of a transaction and to free
284 * all the descriptors of that transaction.
286 * It walks the list of descriptors and unlocks each item. It frees
287 * each chunk except that embedded in the transaction as it goes along.
290 xfs_trans_free_items(
294 xfs_log_item_chunk_t *licp;
295 xfs_log_item_chunk_t *next_licp;
298 abort = flags & XFS_TRANS_ABORT;
301 * Special case the embedded chunk so we don't free it below.
303 if (!XFS_LIC_ARE_ALL_FREE(licp)) {
304 (void) xfs_trans_unlock_chunk(licp, 1, abort, NULLCOMMITLSN);
305 XFS_LIC_ALL_FREE(licp);
306 licp->lic_unused = 0;
308 licp = licp->lic_next;
311 * Unlock each item in each chunk and free the chunks.
313 while (licp != NULL) {
314 ASSERT(!XFS_LIC_ARE_ALL_FREE(licp));
315 (void) xfs_trans_unlock_chunk(licp, 1, abort, NULLCOMMITLSN);
316 next_licp = licp->lic_next;
317 kmem_free(licp, sizeof(xfs_log_item_chunk_t));
322 * Reset the transaction structure's free item count.
324 tp->t_items_free = XFS_LIC_NUM_SLOTS;
325 tp->t_items.lic_next = NULL;
331 * This is called to unlock the items associated with a transaction.
332 * Items which were not logged should be freed.
333 * Those which were logged must still be tracked so they can be unpinned
334 * when the transaction commits.
337 xfs_trans_unlock_items(xfs_trans_t *tp, xfs_lsn_t commit_lsn)
339 xfs_log_item_chunk_t *licp;
340 xfs_log_item_chunk_t *next_licp;
341 xfs_log_item_chunk_t **licpp;
348 * Special case the embedded chunk so we don't free.
350 if (!XFS_LIC_ARE_ALL_FREE(licp)) {
351 freed = xfs_trans_unlock_chunk(licp, 0, 0, commit_lsn);
353 licpp = &(tp->t_items.lic_next);
354 licp = licp->lic_next;
357 * Unlock each item in each chunk, free non-dirty descriptors,
358 * and free empty chunks.
360 while (licp != NULL) {
361 ASSERT(!XFS_LIC_ARE_ALL_FREE(licp));
362 freed += xfs_trans_unlock_chunk(licp, 0, 0, commit_lsn);
363 next_licp = licp->lic_next;
364 if (XFS_LIC_ARE_ALL_FREE(licp)) {
366 kmem_free(licp, sizeof(xfs_log_item_chunk_t));
367 freed -= XFS_LIC_NUM_SLOTS;
369 licpp = &(licp->lic_next);
371 ASSERT(*licpp == next_licp);
376 * Fix the free descriptor count in the transaction.
378 tp->t_items_free += freed;
382 * Unlock each item pointed to by a descriptor in the given chunk.
383 * Stamp the commit lsn into each item if necessary.
384 * Free descriptors pointing to items which are not dirty if freeing_chunk
385 * is zero. If freeing_chunk is non-zero, then we need to unlock all
386 * items in the chunk.
388 * Return the number of descriptors freed.
391 xfs_trans_unlock_chunk(
392 xfs_log_item_chunk_t *licp,
395 xfs_lsn_t commit_lsn)
397 xfs_log_item_desc_t *lidp;
403 lidp = licp->lic_descs;
404 for (i = 0; i < licp->lic_unused; i++, lidp++) {
405 if (XFS_LIC_ISFREE(licp, i)) {
408 lip = lidp->lid_item;
411 if (commit_lsn != NULLCOMMITLSN)
412 IOP_COMMITTING(lip, commit_lsn);
414 lip->li_flags |= XFS_LI_ABORTED;
418 * Free the descriptor if the item is not dirty
419 * within this transaction and the caller is not
420 * going to just free the entire thing regardless.
422 if (!(freeing_chunk) &&
423 (!(lidp->lid_flags & XFS_LID_DIRTY) || abort)) {
424 XFS_LIC_RELSE(licp, i);
434 * This is called to add the given busy item to the transaction's
435 * list of busy items. It must find a free busy item descriptor
436 * or allocate a new one and add the item to that descriptor.
437 * The function returns a pointer to busy descriptor used to point
438 * to the new busy entry. The log busy entry will now point to its new
439 * descriptor with its ???? field.
441 xfs_log_busy_slot_t *
442 xfs_trans_add_busy(xfs_trans_t *tp, xfs_agnumber_t ag, xfs_extlen_t idx)
444 xfs_log_busy_chunk_t *lbcp;
445 xfs_log_busy_slot_t *lbsp;
449 * If there are no free descriptors, allocate a new chunk
450 * of them and put it at the front of the chunk list.
452 if (tp->t_busy_free == 0) {
453 lbcp = (xfs_log_busy_chunk_t*)
454 kmem_alloc(sizeof(xfs_log_busy_chunk_t), KM_SLEEP);
455 ASSERT(lbcp != NULL);
457 * Initialize the chunk, and then
458 * claim the first slot in the newly allocated chunk.
461 XFS_LBC_CLAIM(lbcp, 0);
462 lbcp->lbc_unused = 1;
463 lbsp = XFS_LBC_SLOT(lbcp, 0);
466 * Link in the new chunk and update the free count.
468 lbcp->lbc_next = tp->t_busy.lbc_next;
469 tp->t_busy.lbc_next = lbcp;
470 tp->t_busy_free = XFS_LIC_NUM_SLOTS - 1;
473 * Initialize the descriptor and the generic portion
476 * Point the new slot at this item and return it.
477 * Also point the log item at its currently active
478 * descriptor and set the item's mount pointer.
486 * Find the free descriptor. It is somewhere in the chunklist
490 while (lbcp != NULL) {
491 if (XFS_LBC_VACANCY(lbcp)) {
492 if (lbcp->lbc_unused <= XFS_LBC_MAX_SLOT) {
493 i = lbcp->lbc_unused;
496 /* out-of-order vacancy */
497 cmn_err(CE_DEBUG, "OOO vacancy lbcp 0x%p\n", lbcp);
501 lbcp = lbcp->lbc_next;
503 ASSERT(lbcp != NULL);
505 * If we find a free descriptor, claim it,
506 * initialize it, and return it.
508 XFS_LBC_CLAIM(lbcp, i);
509 if (lbcp->lbc_unused <= i) {
510 lbcp->lbc_unused = i + 1;
512 lbsp = XFS_LBC_SLOT(lbcp, i);
521 * xfs_trans_free_busy
522 * Free all of the busy lists from a transaction
525 xfs_trans_free_busy(xfs_trans_t *tp)
527 xfs_log_busy_chunk_t *lbcp;
528 xfs_log_busy_chunk_t *lbcq;
530 lbcp = tp->t_busy.lbc_next;
531 while (lbcp != NULL) {
532 lbcq = lbcp->lbc_next;
533 kmem_free(lbcp, sizeof(xfs_log_busy_chunk_t));
537 XFS_LBC_INIT(&tp->t_busy);
538 tp->t_busy.lbc_unused = 0;