blob: 1b8a756d80ed009d93a8fab688eb67c1e2b6a010 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
3 *
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.
7 *
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.
11 *
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.
18 *
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.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33#include "xfs.h"
34#include "xfs_macros.h"
35#include "xfs_types.h"
36#include "xfs_inum.h"
37#include "xfs_log.h"
38#include "xfs_trans.h"
39
40STATIC int xfs_trans_unlock_chunk(xfs_log_item_chunk_t *,
41 int, int, xfs_lsn_t);
42
43/*
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.
50 */
51xfs_log_item_desc_t *
52xfs_trans_add_item(xfs_trans_t *tp, xfs_log_item_t *lip)
53{
54 xfs_log_item_desc_t *lidp;
55 xfs_log_item_chunk_t *licp;
56 int i=0;
57
58 /*
59 * If there are no free descriptors, allocate a new chunk
60 * of them and put it at the front of the chunk list.
61 */
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);
65 ASSERT(licp != NULL);
66 /*
67 * Initialize the chunk, and then
68 * claim the first slot in the newly allocated chunk.
69 */
70 XFS_LIC_INIT(licp);
71 XFS_LIC_CLAIM(licp, 0);
72 licp->lic_unused = 1;
73 XFS_LIC_INIT_SLOT(licp, 0);
74 lidp = XFS_LIC_SLOT(licp, 0);
75
76 /*
77 * Link in the new chunk and update the free count.
78 */
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;
82
83 /*
84 * Initialize the descriptor and the generic portion
85 * of the log item.
86 *
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.
90 */
91 lidp->lid_item = lip;
92 lidp->lid_flags = 0;
93 lidp->lid_size = 0;
94 lip->li_desc = lidp;
95 lip->li_mountp = tp->t_mountp;
96 return (lidp);
97 }
98
99 /*
100 * Find the free descriptor. It is somewhere in the chunklist
101 * of descriptors.
102 */
103 licp = &tp->t_items;
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));
109 break;
110 }
111 for (i = 0; i <= XFS_LIC_MAX_SLOT; i++) {
112 if (XFS_LIC_ISFREE(licp, i))
113 break;
114 }
115 ASSERT(i <= XFS_LIC_MAX_SLOT);
116 break;
117 }
118 licp = licp->lic_next;
119 }
120 ASSERT(licp != NULL);
121 /*
122 * If we find a free descriptor, claim it,
123 * initialize it, and return it.
124 */
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);
129 }
130 lidp = XFS_LIC_SLOT(licp, i);
131 tp->t_items_free--;
132 lidp->lid_item = lip;
133 lidp->lid_flags = 0;
134 lidp->lid_size = 0;
135 lip->li_desc = lidp;
136 lip->li_mountp = tp->t_mountp;
137 return (lidp);
138}
139
140/*
141 * Free the given descriptor.
142 *
143 * This requires setting the bit in the chunk's free mask corresponding
144 * to the given slot.
145 */
146void
147xfs_trans_free_item(xfs_trans_t *tp, xfs_log_item_desc_t *lidp)
148{
149 uint slot;
150 xfs_log_item_chunk_t *licp;
151 xfs_log_item_chunk_t **licpp;
152
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;
157 tp->t_items_free++;
158
159 /*
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.
167 *
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.
170 */
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);
176 }
177 *licpp = licp->lic_next;
178 kmem_free(licp, sizeof(xfs_log_item_chunk_t));
179 tp->t_items_free -= XFS_LIC_NUM_SLOTS;
180 }
181}
182
183/*
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.
188 *
189 * The descriptor pointer is kept in the log item's li_desc field.
190 * Just return it.
191 */
192/*ARGSUSED*/
193xfs_log_item_desc_t *
194xfs_trans_find_item(xfs_trans_t *tp, xfs_log_item_t *lip)
195{
196 ASSERT(lip->li_desc != NULL);
197
198 return (lip->li_desc);
199}
200
201
202/*
203 * Return a pointer to the first descriptor in the chunk list.
204 * This does not return NULL if there are none, it panics.
205 *
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.
209 *
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
212 * look at.
213 */
214xfs_log_item_desc_t *
215xfs_trans_first_item(xfs_trans_t *tp)
216{
217 xfs_log_item_chunk_t *licp;
218 int i;
219
220 licp = &tp->t_items;
221 /*
222 * If it's not in the first chunk, skip to the second.
223 */
224 if (XFS_LIC_ARE_ALL_FREE(licp)) {
225 licp = licp->lic_next;
226 }
227
228 /*
229 * Return the first non-free descriptor in the chunk.
230 */
231 ASSERT(!XFS_LIC_ARE_ALL_FREE(licp));
232 for (i = 0; i < licp->lic_unused; i++) {
233 if (XFS_LIC_ISFREE(licp, i)) {
234 continue;
235 }
236
237 return (XFS_LIC_SLOT(licp, i));
238 }
239 cmn_err(CE_WARN, "xfs_trans_first_item() -- no first item");
240 return(NULL);
241}
242
243
244/*
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.
247 *
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
250 * used descriptor.
251 */
252/*ARGSUSED*/
253xfs_log_item_desc_t *
254xfs_trans_next_item(xfs_trans_t *tp, xfs_log_item_desc_t *lidp)
255{
256 xfs_log_item_chunk_t *licp;
257 int i;
258
259 licp = XFS_LIC_DESC_TO_CHUNK(lidp);
260
261 /*
262 * First search the rest of the chunk. The for loop keeps us
263 * from referencing things beyond the end of the chunk.
264 */
265 for (i = (int)XFS_LIC_DESC_TO_SLOT(lidp) + 1; i < licp->lic_unused; i++) {
266 if (XFS_LIC_ISFREE(licp, i)) {
267 continue;
268 }
269
270 return (XFS_LIC_SLOT(licp, i));
271 }
272
273 /*
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.
277 */
278 if (licp->lic_next == NULL) {
279 return (NULL);
280 }
281
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)) {
286 continue;
287 }
288
289 return (XFS_LIC_SLOT(licp, i));
290 }
291 ASSERT(0);
292 /* NOTREACHED */
293 return NULL; /* keep gcc quite */
294}
295
296/*
297 * This is called to unlock all of the items of a transaction and to free
298 * all the descriptors of that transaction.
299 *
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.
302 */
303void
304xfs_trans_free_items(
305 xfs_trans_t *tp,
306 int flags)
307{
308 xfs_log_item_chunk_t *licp;
309 xfs_log_item_chunk_t *next_licp;
310 int abort;
311
312 abort = flags & XFS_TRANS_ABORT;
313 licp = &tp->t_items;
314 /*
315 * Special case the embedded chunk so we don't free it below.
316 */
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;
321 }
322 licp = licp->lic_next;
323
324 /*
325 * Unlock each item in each chunk and free the chunks.
326 */
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));
332 licp = next_licp;
333 }
334
335 /*
336 * Reset the transaction structure's free item count.
337 */
338 tp->t_items_free = XFS_LIC_NUM_SLOTS;
339 tp->t_items.lic_next = NULL;
340}
341
342
343
344/*
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.
349 */
350void
351xfs_trans_unlock_items(xfs_trans_t *tp, xfs_lsn_t commit_lsn)
352{
353 xfs_log_item_chunk_t *licp;
354 xfs_log_item_chunk_t *next_licp;
355 xfs_log_item_chunk_t **licpp;
356 int freed;
357
358 freed = 0;
359 licp = &tp->t_items;
360
361 /*
362 * Special case the embedded chunk so we don't free.
363 */
364 if (!XFS_LIC_ARE_ALL_FREE(licp)) {
365 freed = xfs_trans_unlock_chunk(licp, 0, 0, commit_lsn);
366 }
367 licpp = &(tp->t_items.lic_next);
368 licp = licp->lic_next;
369
370 /*
371 * Unlock each item in each chunk, free non-dirty descriptors,
372 * and free empty chunks.
373 */
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)) {
379 *licpp = next_licp;
380 kmem_free(licp, sizeof(xfs_log_item_chunk_t));
381 freed -= XFS_LIC_NUM_SLOTS;
382 } else {
383 licpp = &(licp->lic_next);
384 }
385 ASSERT(*licpp == next_licp);
386 licp = next_licp;
387 }
388
389 /*
390 * Fix the free descriptor count in the transaction.
391 */
392 tp->t_items_free += freed;
393}
394
395/*
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.
401 *
402 * Return the number of descriptors freed.
403 */
404STATIC int
405xfs_trans_unlock_chunk(
406 xfs_log_item_chunk_t *licp,
407 int freeing_chunk,
408 int abort,
409 xfs_lsn_t commit_lsn)
410{
411 xfs_log_item_desc_t *lidp;
412 xfs_log_item_t *lip;
413 int i;
414 int freed;
415
416 freed = 0;
417 lidp = licp->lic_descs;
418 for (i = 0; i < licp->lic_unused; i++, lidp++) {
419 if (XFS_LIC_ISFREE(licp, i)) {
420 continue;
421 }
422 lip = lidp->lid_item;
423 lip->li_desc = NULL;
424
425 if (commit_lsn != NULLCOMMITLSN)
426 IOP_COMMITTING(lip, commit_lsn);
427 if (abort)
428 lip->li_flags |= XFS_LI_ABORTED;
429 IOP_UNLOCK(lip);
430
431 /*
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.
435 */
436 if (!(freeing_chunk) &&
437 (!(lidp->lid_flags & XFS_LID_DIRTY) || abort)) {
438 XFS_LIC_RELSE(licp, i);
439 freed++;
440 }
441 }
442
443 return (freed);
444}
445
446
447/*
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.
454 */
455xfs_log_busy_slot_t *
456xfs_trans_add_busy(xfs_trans_t *tp, xfs_agnumber_t ag, xfs_extlen_t idx)
457{
458 xfs_log_busy_chunk_t *lbcp;
459 xfs_log_busy_slot_t *lbsp;
460 int i=0;
461
462 /*
463 * If there are no free descriptors, allocate a new chunk
464 * of them and put it at the front of the chunk list.
465 */
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);
470 /*
471 * Initialize the chunk, and then
472 * claim the first slot in the newly allocated chunk.
473 */
474 XFS_LBC_INIT(lbcp);
475 XFS_LBC_CLAIM(lbcp, 0);
476 lbcp->lbc_unused = 1;
477 lbsp = XFS_LBC_SLOT(lbcp, 0);
478
479 /*
480 * Link in the new chunk and update the free count.
481 */
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;
485
486 /*
487 * Initialize the descriptor and the generic portion
488 * of the log item.
489 *
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.
493 */
494 lbsp->lbc_ag = ag;
495 lbsp->lbc_idx = idx;
496 return (lbsp);
497 }
498
499 /*
500 * Find the free descriptor. It is somewhere in the chunklist
501 * of descriptors.
502 */
503 lbcp = &tp->t_busy;
504 while (lbcp != NULL) {
505 if (XFS_LBC_VACANCY(lbcp)) {
506 if (lbcp->lbc_unused <= XFS_LBC_MAX_SLOT) {
507 i = lbcp->lbc_unused;
508 break;
509 } else {
510 /* out-of-order vacancy */
511 printk("OOO vacancy lbcp 0x%p\n", lbcp);
512 ASSERT(0);
513 }
514 }
515 lbcp = lbcp->lbc_next;
516 }
517 ASSERT(lbcp != NULL);
518 /*
519 * If we find a free descriptor, claim it,
520 * initialize it, and return it.
521 */
522 XFS_LBC_CLAIM(lbcp, i);
523 if (lbcp->lbc_unused <= i) {
524 lbcp->lbc_unused = i + 1;
525 }
526 lbsp = XFS_LBC_SLOT(lbcp, i);
527 tp->t_busy_free--;
528 lbsp->lbc_ag = ag;
529 lbsp->lbc_idx = idx;
530 return (lbsp);
531}
532
533
534/*
535 * xfs_trans_free_busy
536 * Free all of the busy lists from a transaction
537 */
538void
539xfs_trans_free_busy(xfs_trans_t *tp)
540{
541 xfs_log_busy_chunk_t *lbcp;
542 xfs_log_busy_chunk_t *lbcq;
543
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));
548 lbcp = lbcq;
549 }
550
551 XFS_LBC_INIT(&tp->t_busy);
552 tp->t_busy.lbc_unused = 0;
553}