v2.6.22.24-op1
[linux-2.6.22.y-op.git] / fs / xfs / xfs_trans_item.c
blob2912aac07c7bff8d7133a54297c78d6707a6669c
1 /*
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
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
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_log.h"
22 #include "xfs_inum.h"
23 #include "xfs_trans.h"
25 STATIC int xfs_trans_unlock_chunk(xfs_log_item_chunk_t *,
26 int, int, xfs_lsn_t);
29 * This is called to add the given log item to the transaction's
30 * list of log items. It must find a free log item descriptor
31 * or allocate a new one and add the item to that descriptor.
32 * The function returns a pointer to item descriptor used to point
33 * to the new item. The log item will now point to its new descriptor
34 * with its li_desc field.
36 xfs_log_item_desc_t *
37 xfs_trans_add_item(xfs_trans_t *tp, xfs_log_item_t *lip)
39 xfs_log_item_desc_t *lidp;
40 xfs_log_item_chunk_t *licp;
41 int i=0;
44 * If there are no free descriptors, allocate a new chunk
45 * of them and put it at the front of the chunk list.
47 if (tp->t_items_free == 0) {
48 licp = (xfs_log_item_chunk_t*)
49 kmem_alloc(sizeof(xfs_log_item_chunk_t), KM_SLEEP);
50 ASSERT(licp != NULL);
52 * Initialize the chunk, and then
53 * claim the first slot in the newly allocated chunk.
55 XFS_LIC_INIT(licp);
56 XFS_LIC_CLAIM(licp, 0);
57 licp->lic_unused = 1;
58 XFS_LIC_INIT_SLOT(licp, 0);
59 lidp = XFS_LIC_SLOT(licp, 0);
62 * Link in the new chunk and update the free count.
64 licp->lic_next = tp->t_items.lic_next;
65 tp->t_items.lic_next = licp;
66 tp->t_items_free = XFS_LIC_NUM_SLOTS - 1;
69 * Initialize the descriptor and the generic portion
70 * of the log item.
72 * Point the new slot at this item and return it.
73 * Also point the log item at its currently active
74 * descriptor and set the item's mount pointer.
76 lidp->lid_item = lip;
77 lidp->lid_flags = 0;
78 lidp->lid_size = 0;
79 lip->li_desc = lidp;
80 lip->li_mountp = tp->t_mountp;
81 return lidp;
85 * Find the free descriptor. It is somewhere in the chunklist
86 * of descriptors.
88 licp = &tp->t_items;
89 while (licp != NULL) {
90 if (XFS_LIC_VACANCY(licp)) {
91 if (licp->lic_unused <= XFS_LIC_MAX_SLOT) {
92 i = licp->lic_unused;
93 ASSERT(XFS_LIC_ISFREE(licp, i));
94 break;
96 for (i = 0; i <= XFS_LIC_MAX_SLOT; i++) {
97 if (XFS_LIC_ISFREE(licp, i))
98 break;
100 ASSERT(i <= XFS_LIC_MAX_SLOT);
101 break;
103 licp = licp->lic_next;
105 ASSERT(licp != NULL);
107 * If we find a free descriptor, claim it,
108 * initialize it, and return it.
110 XFS_LIC_CLAIM(licp, i);
111 if (licp->lic_unused <= i) {
112 licp->lic_unused = i + 1;
113 XFS_LIC_INIT_SLOT(licp, i);
115 lidp = XFS_LIC_SLOT(licp, i);
116 tp->t_items_free--;
117 lidp->lid_item = lip;
118 lidp->lid_flags = 0;
119 lidp->lid_size = 0;
120 lip->li_desc = lidp;
121 lip->li_mountp = tp->t_mountp;
122 return lidp;
126 * Free the given descriptor.
128 * This requires setting the bit in the chunk's free mask corresponding
129 * to the given slot.
131 void
132 xfs_trans_free_item(xfs_trans_t *tp, xfs_log_item_desc_t *lidp)
134 uint slot;
135 xfs_log_item_chunk_t *licp;
136 xfs_log_item_chunk_t **licpp;
138 slot = XFS_LIC_DESC_TO_SLOT(lidp);
139 licp = XFS_LIC_DESC_TO_CHUNK(lidp);
140 XFS_LIC_RELSE(licp, slot);
141 lidp->lid_item->li_desc = NULL;
142 tp->t_items_free++;
145 * If there are no more used items in the chunk and this is not
146 * the chunk embedded in the transaction structure, then free
147 * the chunk. First pull it from the chunk list and then
148 * free it back to the heap. We didn't bother with a doubly
149 * linked list here because the lists should be very short
150 * and this is not a performance path. It's better to save
151 * the memory of the extra pointer.
153 * Also decrement the transaction structure's count of free items
154 * by the number in a chunk since we are freeing an empty chunk.
156 if (XFS_LIC_ARE_ALL_FREE(licp) && (licp != &(tp->t_items))) {
157 licpp = &(tp->t_items.lic_next);
158 while (*licpp != licp) {
159 ASSERT(*licpp != NULL);
160 licpp = &((*licpp)->lic_next);
162 *licpp = licp->lic_next;
163 kmem_free(licp, sizeof(xfs_log_item_chunk_t));
164 tp->t_items_free -= XFS_LIC_NUM_SLOTS;
169 * This is called to find the descriptor corresponding to the given
170 * log item. It returns a pointer to the descriptor.
171 * The log item MUST have a corresponding descriptor in the given
172 * transaction. This routine does not return NULL, it panics.
174 * The descriptor pointer is kept in the log item's li_desc field.
175 * Just return it.
177 /*ARGSUSED*/
178 xfs_log_item_desc_t *
179 xfs_trans_find_item(xfs_trans_t *tp, xfs_log_item_t *lip)
181 ASSERT(lip->li_desc != NULL);
183 return lip->li_desc;
188 * Return a pointer to the first descriptor in the chunk list.
189 * This does not return NULL if there are none, it panics.
191 * The first descriptor must be in either the first or second chunk.
192 * This is because the only chunk allowed to be empty is the first.
193 * All others are freed when they become empty.
195 * At some point this and xfs_trans_next_item() should be optimized
196 * to quickly look at the mask to determine if there is anything to
197 * look at.
199 xfs_log_item_desc_t *
200 xfs_trans_first_item(xfs_trans_t *tp)
202 xfs_log_item_chunk_t *licp;
203 int i;
205 licp = &tp->t_items;
207 * If it's not in the first chunk, skip to the second.
209 if (XFS_LIC_ARE_ALL_FREE(licp)) {
210 licp = licp->lic_next;
214 * Return the first non-free descriptor in the chunk.
216 ASSERT(!XFS_LIC_ARE_ALL_FREE(licp));
217 for (i = 0; i < licp->lic_unused; i++) {
218 if (XFS_LIC_ISFREE(licp, i)) {
219 continue;
222 return XFS_LIC_SLOT(licp, i);
224 cmn_err(CE_WARN, "xfs_trans_first_item() -- no first item");
225 return NULL;
230 * Given a descriptor, return the next descriptor in the chunk list.
231 * This returns NULL if there are no more used descriptors in the list.
233 * We do this by first locating the chunk in which the descriptor resides,
234 * and then scanning forward in the chunk and the list for the next
235 * used descriptor.
237 /*ARGSUSED*/
238 xfs_log_item_desc_t *
239 xfs_trans_next_item(xfs_trans_t *tp, xfs_log_item_desc_t *lidp)
241 xfs_log_item_chunk_t *licp;
242 int i;
244 licp = XFS_LIC_DESC_TO_CHUNK(lidp);
247 * First search the rest of the chunk. The for loop keeps us
248 * from referencing things beyond the end of the chunk.
250 for (i = (int)XFS_LIC_DESC_TO_SLOT(lidp) + 1; i < licp->lic_unused; i++) {
251 if (XFS_LIC_ISFREE(licp, i)) {
252 continue;
255 return XFS_LIC_SLOT(licp, i);
259 * Now search the next chunk. It must be there, because the
260 * next chunk would have been freed if it were empty.
261 * If there is no next chunk, return NULL.
263 if (licp->lic_next == NULL) {
264 return NULL;
267 licp = licp->lic_next;
268 ASSERT(!XFS_LIC_ARE_ALL_FREE(licp));
269 for (i = 0; i < licp->lic_unused; i++) {
270 if (XFS_LIC_ISFREE(licp, i)) {
271 continue;
274 return XFS_LIC_SLOT(licp, i);
276 ASSERT(0);
277 /* NOTREACHED */
278 return NULL; /* keep gcc quite */
282 * This is called to unlock all of the items of a transaction and to free
283 * all the descriptors of that transaction.
285 * It walks the list of descriptors and unlocks each item. It frees
286 * each chunk except that embedded in the transaction as it goes along.
288 void
289 xfs_trans_free_items(
290 xfs_trans_t *tp,
291 int flags)
293 xfs_log_item_chunk_t *licp;
294 xfs_log_item_chunk_t *next_licp;
295 int abort;
297 abort = flags & XFS_TRANS_ABORT;
298 licp = &tp->t_items;
300 * Special case the embedded chunk so we don't free it below.
302 if (!XFS_LIC_ARE_ALL_FREE(licp)) {
303 (void) xfs_trans_unlock_chunk(licp, 1, abort, NULLCOMMITLSN);
304 XFS_LIC_ALL_FREE(licp);
305 licp->lic_unused = 0;
307 licp = licp->lic_next;
310 * Unlock each item in each chunk and free the chunks.
312 while (licp != NULL) {
313 ASSERT(!XFS_LIC_ARE_ALL_FREE(licp));
314 (void) xfs_trans_unlock_chunk(licp, 1, abort, NULLCOMMITLSN);
315 next_licp = licp->lic_next;
316 kmem_free(licp, sizeof(xfs_log_item_chunk_t));
317 licp = next_licp;
321 * Reset the transaction structure's free item count.
323 tp->t_items_free = XFS_LIC_NUM_SLOTS;
324 tp->t_items.lic_next = NULL;
330 * This is called to unlock the items associated with a transaction.
331 * Items which were not logged should be freed.
332 * Those which were logged must still be tracked so they can be unpinned
333 * when the transaction commits.
335 void
336 xfs_trans_unlock_items(xfs_trans_t *tp, xfs_lsn_t commit_lsn)
338 xfs_log_item_chunk_t *licp;
339 xfs_log_item_chunk_t *next_licp;
340 xfs_log_item_chunk_t **licpp;
341 int freed;
343 freed = 0;
344 licp = &tp->t_items;
347 * Special case the embedded chunk so we don't free.
349 if (!XFS_LIC_ARE_ALL_FREE(licp)) {
350 freed = xfs_trans_unlock_chunk(licp, 0, 0, commit_lsn);
352 licpp = &(tp->t_items.lic_next);
353 licp = licp->lic_next;
356 * Unlock each item in each chunk, free non-dirty descriptors,
357 * and free empty chunks.
359 while (licp != NULL) {
360 ASSERT(!XFS_LIC_ARE_ALL_FREE(licp));
361 freed += xfs_trans_unlock_chunk(licp, 0, 0, commit_lsn);
362 next_licp = licp->lic_next;
363 if (XFS_LIC_ARE_ALL_FREE(licp)) {
364 *licpp = next_licp;
365 kmem_free(licp, sizeof(xfs_log_item_chunk_t));
366 freed -= XFS_LIC_NUM_SLOTS;
367 } else {
368 licpp = &(licp->lic_next);
370 ASSERT(*licpp == next_licp);
371 licp = next_licp;
375 * Fix the free descriptor count in the transaction.
377 tp->t_items_free += freed;
381 * Unlock each item pointed to by a descriptor in the given chunk.
382 * Stamp the commit lsn into each item if necessary.
383 * Free descriptors pointing to items which are not dirty if freeing_chunk
384 * is zero. If freeing_chunk is non-zero, then we need to unlock all
385 * items in the chunk.
387 * Return the number of descriptors freed.
389 STATIC int
390 xfs_trans_unlock_chunk(
391 xfs_log_item_chunk_t *licp,
392 int freeing_chunk,
393 int abort,
394 xfs_lsn_t commit_lsn)
396 xfs_log_item_desc_t *lidp;
397 xfs_log_item_t *lip;
398 int i;
399 int freed;
401 freed = 0;
402 lidp = licp->lic_descs;
403 for (i = 0; i < licp->lic_unused; i++, lidp++) {
404 if (XFS_LIC_ISFREE(licp, i)) {
405 continue;
407 lip = lidp->lid_item;
408 lip->li_desc = NULL;
410 if (commit_lsn != NULLCOMMITLSN)
411 IOP_COMMITTING(lip, commit_lsn);
412 if (abort)
413 lip->li_flags |= XFS_LI_ABORTED;
414 IOP_UNLOCK(lip);
417 * Free the descriptor if the item is not dirty
418 * within this transaction and the caller is not
419 * going to just free the entire thing regardless.
421 if (!(freeing_chunk) &&
422 (!(lidp->lid_flags & XFS_LID_DIRTY) || abort)) {
423 XFS_LIC_RELSE(licp, i);
424 freed++;
428 return freed;
433 * This is called to add the given busy item to the transaction's
434 * list of busy items. It must find a free busy item descriptor
435 * or allocate a new one and add the item to that descriptor.
436 * The function returns a pointer to busy descriptor used to point
437 * to the new busy entry. The log busy entry will now point to its new
438 * descriptor with its ???? field.
440 xfs_log_busy_slot_t *
441 xfs_trans_add_busy(xfs_trans_t *tp, xfs_agnumber_t ag, xfs_extlen_t idx)
443 xfs_log_busy_chunk_t *lbcp;
444 xfs_log_busy_slot_t *lbsp;
445 int i=0;
448 * If there are no free descriptors, allocate a new chunk
449 * of them and put it at the front of the chunk list.
451 if (tp->t_busy_free == 0) {
452 lbcp = (xfs_log_busy_chunk_t*)
453 kmem_alloc(sizeof(xfs_log_busy_chunk_t), KM_SLEEP);
454 ASSERT(lbcp != NULL);
456 * Initialize the chunk, and then
457 * claim the first slot in the newly allocated chunk.
459 XFS_LBC_INIT(lbcp);
460 XFS_LBC_CLAIM(lbcp, 0);
461 lbcp->lbc_unused = 1;
462 lbsp = XFS_LBC_SLOT(lbcp, 0);
465 * Link in the new chunk and update the free count.
467 lbcp->lbc_next = tp->t_busy.lbc_next;
468 tp->t_busy.lbc_next = lbcp;
469 tp->t_busy_free = XFS_LIC_NUM_SLOTS - 1;
472 * Initialize the descriptor and the generic portion
473 * of the log item.
475 * Point the new slot at this item and return it.
476 * Also point the log item at its currently active
477 * descriptor and set the item's mount pointer.
479 lbsp->lbc_ag = ag;
480 lbsp->lbc_idx = idx;
481 return lbsp;
485 * Find the free descriptor. It is somewhere in the chunklist
486 * of descriptors.
488 lbcp = &tp->t_busy;
489 while (lbcp != NULL) {
490 if (XFS_LBC_VACANCY(lbcp)) {
491 if (lbcp->lbc_unused <= XFS_LBC_MAX_SLOT) {
492 i = lbcp->lbc_unused;
493 break;
494 } else {
495 /* out-of-order vacancy */
496 cmn_err(CE_DEBUG, "OOO vacancy lbcp 0x%p\n", lbcp);
497 ASSERT(0);
500 lbcp = lbcp->lbc_next;
502 ASSERT(lbcp != NULL);
504 * If we find a free descriptor, claim it,
505 * initialize it, and return it.
507 XFS_LBC_CLAIM(lbcp, i);
508 if (lbcp->lbc_unused <= i) {
509 lbcp->lbc_unused = i + 1;
511 lbsp = XFS_LBC_SLOT(lbcp, i);
512 tp->t_busy_free--;
513 lbsp->lbc_ag = ag;
514 lbsp->lbc_idx = idx;
515 return lbsp;
520 * xfs_trans_free_busy
521 * Free all of the busy lists from a transaction
523 void
524 xfs_trans_free_busy(xfs_trans_t *tp)
526 xfs_log_busy_chunk_t *lbcp;
527 xfs_log_busy_chunk_t *lbcq;
529 lbcp = tp->t_busy.lbc_next;
530 while (lbcp != NULL) {
531 lbcq = lbcp->lbc_next;
532 kmem_free(lbcp, sizeof(xfs_log_busy_chunk_t));
533 lbcp = lbcq;
536 XFS_LBC_INIT(&tp->t_busy);
537 tp->t_busy.lbc_unused = 0;