2 * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 #include "xfs_macros.h"
35 #include "xfs_types.h"
38 #include "xfs_trans.h"
40 STATIC
int xfs_trans_unlock_chunk(xfs_log_item_chunk_t
*,
44 * This is called to add the given log item to the transaction's
45 * list of log items. It must find a free log item descriptor
46 * or allocate a new one and add the item to that descriptor.
47 * The function returns a pointer to item descriptor used to point
48 * to the new item. The log item will now point to its new descriptor
49 * with its li_desc field.
52 xfs_trans_add_item(xfs_trans_t
*tp
, xfs_log_item_t
*lip
)
54 xfs_log_item_desc_t
*lidp
;
55 xfs_log_item_chunk_t
*licp
;
59 * If there are no free descriptors, allocate a new chunk
60 * of them and put it at the front of the chunk list.
62 if (tp
->t_items_free
== 0) {
63 licp
= (xfs_log_item_chunk_t
*)
64 kmem_alloc(sizeof(xfs_log_item_chunk_t
), KM_SLEEP
);
67 * Initialize the chunk, and then
68 * claim the first slot in the newly allocated chunk.
71 XFS_LIC_CLAIM(licp
, 0);
73 XFS_LIC_INIT_SLOT(licp
, 0);
74 lidp
= XFS_LIC_SLOT(licp
, 0);
77 * Link in the new chunk and update the free count.
79 licp
->lic_next
= tp
->t_items
.lic_next
;
80 tp
->t_items
.lic_next
= licp
;
81 tp
->t_items_free
= XFS_LIC_NUM_SLOTS
- 1;
84 * Initialize the descriptor and the generic portion
87 * Point the new slot at this item and return it.
88 * Also point the log item at its currently active
89 * descriptor and set the item's mount pointer.
95 lip
->li_mountp
= tp
->t_mountp
;
100 * Find the free descriptor. It is somewhere in the chunklist
104 while (licp
!= NULL
) {
105 if (XFS_LIC_VACANCY(licp
)) {
106 if (licp
->lic_unused
<= XFS_LIC_MAX_SLOT
) {
107 i
= licp
->lic_unused
;
108 ASSERT(XFS_LIC_ISFREE(licp
, i
));
111 for (i
= 0; i
<= XFS_LIC_MAX_SLOT
; i
++) {
112 if (XFS_LIC_ISFREE(licp
, i
))
115 ASSERT(i
<= XFS_LIC_MAX_SLOT
);
118 licp
= licp
->lic_next
;
120 ASSERT(licp
!= NULL
);
122 * If we find a free descriptor, claim it,
123 * initialize it, and return it.
125 XFS_LIC_CLAIM(licp
, i
);
126 if (licp
->lic_unused
<= i
) {
127 licp
->lic_unused
= i
+ 1;
128 XFS_LIC_INIT_SLOT(licp
, i
);
130 lidp
= XFS_LIC_SLOT(licp
, i
);
132 lidp
->lid_item
= lip
;
136 lip
->li_mountp
= tp
->t_mountp
;
141 * Free the given descriptor.
143 * This requires setting the bit in the chunk's free mask corresponding
147 xfs_trans_free_item(xfs_trans_t
*tp
, xfs_log_item_desc_t
*lidp
)
150 xfs_log_item_chunk_t
*licp
;
151 xfs_log_item_chunk_t
**licpp
;
153 slot
= XFS_LIC_DESC_TO_SLOT(lidp
);
154 licp
= XFS_LIC_DESC_TO_CHUNK(lidp
);
155 XFS_LIC_RELSE(licp
, slot
);
156 lidp
->lid_item
->li_desc
= NULL
;
160 * If there are no more used items in the chunk and this is not
161 * the chunk embedded in the transaction structure, then free
162 * the chunk. First pull it from the chunk list and then
163 * free it back to the heap. We didn't bother with a doubly
164 * linked list here because the lists should be very short
165 * and this is not a performance path. It's better to save
166 * the memory of the extra pointer.
168 * Also decrement the transaction structure's count of free items
169 * by the number in a chunk since we are freeing an empty chunk.
171 if (XFS_LIC_ARE_ALL_FREE(licp
) && (licp
!= &(tp
->t_items
))) {
172 licpp
= &(tp
->t_items
.lic_next
);
173 while (*licpp
!= licp
) {
174 ASSERT(*licpp
!= NULL
);
175 licpp
= &((*licpp
)->lic_next
);
177 *licpp
= licp
->lic_next
;
178 kmem_free(licp
, sizeof(xfs_log_item_chunk_t
));
179 tp
->t_items_free
-= XFS_LIC_NUM_SLOTS
;
184 * This is called to find the descriptor corresponding to the given
185 * log item. It returns a pointer to the descriptor.
186 * The log item MUST have a corresponding descriptor in the given
187 * transaction. This routine does not return NULL, it panics.
189 * The descriptor pointer is kept in the log item's li_desc field.
193 xfs_log_item_desc_t
*
194 xfs_trans_find_item(xfs_trans_t
*tp
, xfs_log_item_t
*lip
)
196 ASSERT(lip
->li_desc
!= NULL
);
198 return (lip
->li_desc
);
203 * Return a pointer to the first descriptor in the chunk list.
204 * This does not return NULL if there are none, it panics.
206 * The first descriptor must be in either the first or second chunk.
207 * This is because the only chunk allowed to be empty is the first.
208 * All others are freed when they become empty.
210 * At some point this and xfs_trans_next_item() should be optimized
211 * to quickly look at the mask to determine if there is anything to
214 xfs_log_item_desc_t
*
215 xfs_trans_first_item(xfs_trans_t
*tp
)
217 xfs_log_item_chunk_t
*licp
;
222 * If it's not in the first chunk, skip to the second.
224 if (XFS_LIC_ARE_ALL_FREE(licp
)) {
225 licp
= licp
->lic_next
;
229 * Return the first non-free descriptor in the chunk.
231 ASSERT(!XFS_LIC_ARE_ALL_FREE(licp
));
232 for (i
= 0; i
< licp
->lic_unused
; i
++) {
233 if (XFS_LIC_ISFREE(licp
, i
)) {
237 return (XFS_LIC_SLOT(licp
, i
));
239 cmn_err(CE_WARN
, "xfs_trans_first_item() -- no first item");
245 * Given a descriptor, return the next descriptor in the chunk list.
246 * This returns NULL if there are no more used descriptors in the list.
248 * We do this by first locating the chunk in which the descriptor resides,
249 * and then scanning forward in the chunk and the list for the next
253 xfs_log_item_desc_t
*
254 xfs_trans_next_item(xfs_trans_t
*tp
, xfs_log_item_desc_t
*lidp
)
256 xfs_log_item_chunk_t
*licp
;
259 licp
= XFS_LIC_DESC_TO_CHUNK(lidp
);
262 * First search the rest of the chunk. The for loop keeps us
263 * from referencing things beyond the end of the chunk.
265 for (i
= (int)XFS_LIC_DESC_TO_SLOT(lidp
) + 1; i
< licp
->lic_unused
; i
++) {
266 if (XFS_LIC_ISFREE(licp
, i
)) {
270 return (XFS_LIC_SLOT(licp
, i
));
274 * Now search the next chunk. It must be there, because the
275 * next chunk would have been freed if it were empty.
276 * If there is no next chunk, return NULL.
278 if (licp
->lic_next
== NULL
) {
282 licp
= licp
->lic_next
;
283 ASSERT(!XFS_LIC_ARE_ALL_FREE(licp
));
284 for (i
= 0; i
< licp
->lic_unused
; i
++) {
285 if (XFS_LIC_ISFREE(licp
, i
)) {
289 return (XFS_LIC_SLOT(licp
, i
));
293 return NULL
; /* keep gcc quite */
297 * This is called to unlock all of the items of a transaction and to free
298 * all the descriptors of that transaction.
300 * It walks the list of descriptors and unlocks each item. It frees
301 * each chunk except that embedded in the transaction as it goes along.
304 xfs_trans_free_items(
308 xfs_log_item_chunk_t
*licp
;
309 xfs_log_item_chunk_t
*next_licp
;
312 abort
= flags
& XFS_TRANS_ABORT
;
315 * Special case the embedded chunk so we don't free it below.
317 if (!XFS_LIC_ARE_ALL_FREE(licp
)) {
318 (void) xfs_trans_unlock_chunk(licp
, 1, abort
, NULLCOMMITLSN
);
319 XFS_LIC_ALL_FREE(licp
);
320 licp
->lic_unused
= 0;
322 licp
= licp
->lic_next
;
325 * Unlock each item in each chunk and free the chunks.
327 while (licp
!= NULL
) {
328 ASSERT(!XFS_LIC_ARE_ALL_FREE(licp
));
329 (void) xfs_trans_unlock_chunk(licp
, 1, abort
, NULLCOMMITLSN
);
330 next_licp
= licp
->lic_next
;
331 kmem_free(licp
, sizeof(xfs_log_item_chunk_t
));
336 * Reset the transaction structure's free item count.
338 tp
->t_items_free
= XFS_LIC_NUM_SLOTS
;
339 tp
->t_items
.lic_next
= NULL
;
345 * This is called to unlock the items associated with a transaction.
346 * Items which were not logged should be freed.
347 * Those which were logged must still be tracked so they can be unpinned
348 * when the transaction commits.
351 xfs_trans_unlock_items(xfs_trans_t
*tp
, xfs_lsn_t commit_lsn
)
353 xfs_log_item_chunk_t
*licp
;
354 xfs_log_item_chunk_t
*next_licp
;
355 xfs_log_item_chunk_t
**licpp
;
362 * Special case the embedded chunk so we don't free.
364 if (!XFS_LIC_ARE_ALL_FREE(licp
)) {
365 freed
= xfs_trans_unlock_chunk(licp
, 0, 0, commit_lsn
);
367 licpp
= &(tp
->t_items
.lic_next
);
368 licp
= licp
->lic_next
;
371 * Unlock each item in each chunk, free non-dirty descriptors,
372 * and free empty chunks.
374 while (licp
!= NULL
) {
375 ASSERT(!XFS_LIC_ARE_ALL_FREE(licp
));
376 freed
+= xfs_trans_unlock_chunk(licp
, 0, 0, commit_lsn
);
377 next_licp
= licp
->lic_next
;
378 if (XFS_LIC_ARE_ALL_FREE(licp
)) {
380 kmem_free(licp
, sizeof(xfs_log_item_chunk_t
));
381 freed
-= XFS_LIC_NUM_SLOTS
;
383 licpp
= &(licp
->lic_next
);
385 ASSERT(*licpp
== next_licp
);
390 * Fix the free descriptor count in the transaction.
392 tp
->t_items_free
+= freed
;
396 * Unlock each item pointed to by a descriptor in the given chunk.
397 * Stamp the commit lsn into each item if necessary.
398 * Free descriptors pointing to items which are not dirty if freeing_chunk
399 * is zero. If freeing_chunk is non-zero, then we need to unlock all
400 * items in the chunk.
402 * Return the number of descriptors freed.
405 xfs_trans_unlock_chunk(
406 xfs_log_item_chunk_t
*licp
,
409 xfs_lsn_t commit_lsn
)
411 xfs_log_item_desc_t
*lidp
;
417 lidp
= licp
->lic_descs
;
418 for (i
= 0; i
< licp
->lic_unused
; i
++, lidp
++) {
419 if (XFS_LIC_ISFREE(licp
, i
)) {
422 lip
= lidp
->lid_item
;
425 if (commit_lsn
!= NULLCOMMITLSN
)
426 IOP_COMMITTING(lip
, commit_lsn
);
428 lip
->li_flags
|= XFS_LI_ABORTED
;
432 * Free the descriptor if the item is not dirty
433 * within this transaction and the caller is not
434 * going to just free the entire thing regardless.
436 if (!(freeing_chunk
) &&
437 (!(lidp
->lid_flags
& XFS_LID_DIRTY
) || abort
)) {
438 XFS_LIC_RELSE(licp
, i
);
448 * This is called to add the given busy item to the transaction's
449 * list of busy items. It must find a free busy item descriptor
450 * or allocate a new one and add the item to that descriptor.
451 * The function returns a pointer to busy descriptor used to point
452 * to the new busy entry. The log busy entry will now point to its new
453 * descriptor with its ???? field.
455 xfs_log_busy_slot_t
*
456 xfs_trans_add_busy(xfs_trans_t
*tp
, xfs_agnumber_t ag
, xfs_extlen_t idx
)
458 xfs_log_busy_chunk_t
*lbcp
;
459 xfs_log_busy_slot_t
*lbsp
;
463 * If there are no free descriptors, allocate a new chunk
464 * of them and put it at the front of the chunk list.
466 if (tp
->t_busy_free
== 0) {
467 lbcp
= (xfs_log_busy_chunk_t
*)
468 kmem_alloc(sizeof(xfs_log_busy_chunk_t
), KM_SLEEP
);
469 ASSERT(lbcp
!= NULL
);
471 * Initialize the chunk, and then
472 * claim the first slot in the newly allocated chunk.
475 XFS_LBC_CLAIM(lbcp
, 0);
476 lbcp
->lbc_unused
= 1;
477 lbsp
= XFS_LBC_SLOT(lbcp
, 0);
480 * Link in the new chunk and update the free count.
482 lbcp
->lbc_next
= tp
->t_busy
.lbc_next
;
483 tp
->t_busy
.lbc_next
= lbcp
;
484 tp
->t_busy_free
= XFS_LIC_NUM_SLOTS
- 1;
487 * Initialize the descriptor and the generic portion
490 * Point the new slot at this item and return it.
491 * Also point the log item at its currently active
492 * descriptor and set the item's mount pointer.
500 * Find the free descriptor. It is somewhere in the chunklist
504 while (lbcp
!= NULL
) {
505 if (XFS_LBC_VACANCY(lbcp
)) {
506 if (lbcp
->lbc_unused
<= XFS_LBC_MAX_SLOT
) {
507 i
= lbcp
->lbc_unused
;
510 /* out-of-order vacancy */
511 printk("OOO vacancy lbcp 0x%p\n", lbcp
);
515 lbcp
= lbcp
->lbc_next
;
517 ASSERT(lbcp
!= NULL
);
519 * If we find a free descriptor, claim it,
520 * initialize it, and return it.
522 XFS_LBC_CLAIM(lbcp
, i
);
523 if (lbcp
->lbc_unused
<= i
) {
524 lbcp
->lbc_unused
= i
+ 1;
526 lbsp
= XFS_LBC_SLOT(lbcp
, i
);
535 * xfs_trans_free_busy
536 * Free all of the busy lists from a transaction
539 xfs_trans_free_busy(xfs_trans_t
*tp
)
541 xfs_log_busy_chunk_t
*lbcp
;
542 xfs_log_busy_chunk_t
*lbcq
;
544 lbcp
= tp
->t_busy
.lbc_next
;
545 while (lbcp
!= NULL
) {
546 lbcq
= lbcp
->lbc_next
;
547 kmem_free(lbcp
, sizeof(xfs_log_busy_chunk_t
));
551 XFS_LBC_INIT(&tp
->t_busy
);
552 tp
->t_busy
.lbc_unused
= 0;