2 * Copyright (c) 2000-2004 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 * This file contains the implementation of the xfs_buf_log_item.
35 * It contains the item operations used to manipulate the buf log
36 * items as well as utility routines used by the buffer specific
37 * transaction routines.
42 #include "xfs_macros.h"
43 #include "xfs_types.h"
46 #include "xfs_trans.h"
47 #include "xfs_buf_item.h"
50 #include "xfs_dmapi.h"
51 #include "xfs_mount.h"
52 #include "xfs_trans_priv.h"
55 #include "xfs_error.h"
58 #define ROUNDUPNBWORD(x) (((x) + (NBWORD - 1)) & ~(NBWORD - 1))
60 kmem_zone_t *xfs_buf_item_zone;
62 #ifdef XFS_TRANS_DEBUG
64 * This function uses an alternate strategy for tracking the bytes
65 * that the user requests to be logged. This can then be used
66 * in conjunction with the bli_orig array in the buf log item to
67 * catch bugs in our callers' code.
69 * We also double check the bits set in xfs_buf_item_log using a
70 * simple algorithm to check that every byte is accounted for.
73 xfs_buf_item_log_debug(
74 xfs_buf_log_item_t *bip,
87 ASSERT(bip->bli_logged != NULL);
89 nbytes = last - first + 1;
90 bfset(bip->bli_logged, first, nbytes);
91 for (x = 0; x < nbytes; x++) {
92 chunk_num = byte >> XFS_BLI_SHIFT;
93 word_num = chunk_num >> BIT_TO_WORD_SHIFT;
94 bit_num = chunk_num & (NBWORD - 1);
95 wordp = &(bip->bli_format.blf_data_map[word_num]);
96 bit_set = *wordp & (1 << bit_num);
103 * This function is called when we flush something into a buffer without
104 * logging it. This happens for things like inodes which are logged
105 * separately from the buffer.
108 xfs_buf_item_flush_log_debug(
113 xfs_buf_log_item_t *bip;
116 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
117 if ((bip == NULL) || (bip->bli_item.li_type != XFS_LI_BUF)) {
121 ASSERT(bip->bli_logged != NULL);
122 nbytes = last - first + 1;
123 bfset(bip->bli_logged, first, nbytes);
127 * This function is called to verify that our caller's have logged
128 * all the bytes that they changed.
130 * It does this by comparing the original copy of the buffer stored in
131 * the buf log item's bli_orig array to the current copy of the buffer
132 * and ensuring that all bytes which miscompare are set in the bli_logged
133 * array of the buf log item.
136 xfs_buf_item_log_check(
137 xfs_buf_log_item_t *bip)
144 ASSERT(bip->bli_orig != NULL);
145 ASSERT(bip->bli_logged != NULL);
148 ASSERT(XFS_BUF_COUNT(bp) > 0);
149 ASSERT(XFS_BUF_PTR(bp) != NULL);
150 orig = bip->bli_orig;
151 buffer = XFS_BUF_PTR(bp);
152 for (x = 0; x < XFS_BUF_COUNT(bp); x++) {
153 if (orig[x] != buffer[x] && !btst(bip->bli_logged, x))
155 "xfs_buf_item_log_check bip %x buffer %x orig %x index %d",
160 #define xfs_buf_item_log_debug(x,y,z)
161 #define xfs_buf_item_log_check(x)
164 STATIC void xfs_buf_error_relse(xfs_buf_t *bp);
165 STATIC void xfs_buf_do_callbacks(xfs_buf_t *bp, xfs_log_item_t *lip);
168 * This returns the number of log iovecs needed to log the
169 * given buf log item.
171 * It calculates this as 1 iovec for the buf log format structure
172 * and 1 for each stretch of non-contiguous chunks to be logged.
173 * Contiguous chunks are logged in a single iovec.
175 * If the XFS_BLI_STALE flag has been set, then log nothing.
179 xfs_buf_log_item_t *bip)
186 ASSERT(atomic_read(&bip->bli_refcount) > 0);
187 if (bip->bli_flags & XFS_BLI_STALE) {
189 * The buffer is stale, so all we need to log
190 * is the buf log format structure with the
193 xfs_buf_item_trace("SIZE STALE", bip);
194 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
199 ASSERT(bip->bli_flags & XFS_BLI_LOGGED);
201 last_bit = xfs_next_bit(bip->bli_format.blf_data_map,
202 bip->bli_format.blf_map_size, 0);
203 ASSERT(last_bit != -1);
205 while (last_bit != -1) {
207 * This takes the bit number to start looking from and
208 * returns the next set bit from there. It returns -1
209 * if there are no more bits set or the start bit is
210 * beyond the end of the bitmap.
212 next_bit = xfs_next_bit(bip->bli_format.blf_data_map,
213 bip->bli_format.blf_map_size,
216 * If we run out of bits, leave the loop,
217 * else if we find a new set of bits bump the number of vecs,
218 * else keep scanning the current set of bits.
220 if (next_bit == -1) {
222 } else if (next_bit != last_bit + 1) {
225 } else if (xfs_buf_offset(bp, next_bit * XFS_BLI_CHUNK) !=
226 (xfs_buf_offset(bp, last_bit * XFS_BLI_CHUNK) +
235 xfs_buf_item_trace("SIZE NORM", bip);
240 * This is called to fill in the vector of log iovecs for the
241 * given log buf item. It fills the first entry with a buf log
242 * format structure, and the rest point to contiguous chunks
247 xfs_buf_log_item_t *bip,
248 xfs_log_iovec_t *log_vector)
252 xfs_log_iovec_t *vecp;
260 ASSERT(atomic_read(&bip->bli_refcount) > 0);
261 ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
262 (bip->bli_flags & XFS_BLI_STALE));
264 ASSERT(XFS_BUF_BP_ISMAPPED(bp));
268 * The size of the base structure is the size of the
269 * declared structure plus the space for the extra words
270 * of the bitmap. We subtract one from the map size, because
271 * the first element of the bitmap is accounted for in the
272 * size of the base structure.
275 (uint)(sizeof(xfs_buf_log_format_t) +
276 ((bip->bli_format.blf_map_size - 1) * sizeof(uint)));
277 vecp->i_addr = (xfs_caddr_t)&bip->bli_format;
278 vecp->i_len = base_size;
282 if (bip->bli_flags & XFS_BLI_STALE) {
284 * The buffer is stale, so all we need to log
285 * is the buf log format structure with the
288 xfs_buf_item_trace("FORMAT STALE", bip);
289 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
290 bip->bli_format.blf_size = nvecs;
295 * Fill in an iovec for each set of contiguous chunks.
297 first_bit = xfs_next_bit(bip->bli_format.blf_data_map,
298 bip->bli_format.blf_map_size, 0);
299 ASSERT(first_bit != -1);
300 last_bit = first_bit;
304 * This takes the bit number to start looking from and
305 * returns the next set bit from there. It returns -1
306 * if there are no more bits set or the start bit is
307 * beyond the end of the bitmap.
309 next_bit = xfs_next_bit(bip->bli_format.blf_data_map,
310 bip->bli_format.blf_map_size,
313 * If we run out of bits fill in the last iovec and get
315 * Else if we start a new set of bits then fill in the
316 * iovec for the series we were looking at and start
317 * counting the bits in the new one.
318 * Else we're still in the same set of bits so just
319 * keep counting and scanning.
321 if (next_bit == -1) {
322 buffer_offset = first_bit * XFS_BLI_CHUNK;
323 vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
324 vecp->i_len = nbits * XFS_BLI_CHUNK;
327 } else if (next_bit != last_bit + 1) {
328 buffer_offset = first_bit * XFS_BLI_CHUNK;
329 vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
330 vecp->i_len = nbits * XFS_BLI_CHUNK;
333 first_bit = next_bit;
336 } else if (xfs_buf_offset(bp, next_bit << XFS_BLI_SHIFT) !=
337 (xfs_buf_offset(bp, last_bit << XFS_BLI_SHIFT) +
339 buffer_offset = first_bit * XFS_BLI_CHUNK;
340 vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
341 vecp->i_len = nbits * XFS_BLI_CHUNK;
342 /* You would think we need to bump the nvecs here too, but we do not
343 * this number is used by recovery, and it gets confused by the boundary
348 first_bit = next_bit;
356 bip->bli_format.blf_size = nvecs;
359 * Check to make sure everything is consistent.
361 xfs_buf_item_trace("FORMAT NORM", bip);
362 xfs_buf_item_log_check(bip);
366 * This is called to pin the buffer associated with the buf log
367 * item in memory so it cannot be written out. Simply call bpin()
368 * on the buffer to do this.
372 xfs_buf_log_item_t *bip)
377 ASSERT(XFS_BUF_ISBUSY(bp));
378 ASSERT(atomic_read(&bip->bli_refcount) > 0);
379 ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
380 (bip->bli_flags & XFS_BLI_STALE));
381 xfs_buf_item_trace("PIN", bip);
382 xfs_buftrace("XFS_PIN", bp);
388 * This is called to unpin the buffer associated with the buf log
389 * item which was previously pinned with a call to xfs_buf_item_pin().
390 * Just call bunpin() on the buffer to do this.
392 * Also drop the reference to the buf item for the current transaction.
393 * If the XFS_BLI_STALE flag is set and we are the last reference,
394 * then free up the buf log item and unlock the buffer.
398 xfs_buf_log_item_t *bip,
408 ASSERT(XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *) == bip);
409 ASSERT(atomic_read(&bip->bli_refcount) > 0);
410 xfs_buf_item_trace("UNPIN", bip);
411 xfs_buftrace("XFS_UNPIN", bp);
413 freed = atomic_dec_and_test(&bip->bli_refcount);
414 mp = bip->bli_item.li_mountp;
416 if (freed && stale) {
417 ASSERT(bip->bli_flags & XFS_BLI_STALE);
418 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
419 ASSERT(!(XFS_BUF_ISDELAYWRITE(bp)));
420 ASSERT(XFS_BUF_ISSTALE(bp));
421 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
422 xfs_buf_item_trace("UNPIN STALE", bip);
423 xfs_buftrace("XFS_UNPIN STALE", bp);
425 * If we get called here because of an IO error, we may
426 * or may not have the item on the AIL. xfs_trans_delete_ail()
427 * will take care of that situation.
428 * xfs_trans_delete_ail() drops the AIL lock.
430 if (bip->bli_flags & XFS_BLI_STALE_INODE) {
431 xfs_buf_do_callbacks(bp, (xfs_log_item_t *)bip);
432 XFS_BUF_SET_FSPRIVATE(bp, NULL);
433 XFS_BUF_CLR_IODONE_FUNC(bp);
436 xfs_trans_delete_ail(mp, (xfs_log_item_t *)bip, s);
437 xfs_buf_item_relse(bp);
438 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) == NULL);
445 * this is called from uncommit in the forced-shutdown path.
446 * we need to check to see if the reference count on the log item
447 * is going to drop to zero. If so, unpin will free the log item
448 * so we need to free the item's descriptor (that points to the item)
449 * in the transaction.
452 xfs_buf_item_unpin_remove(
453 xfs_buf_log_item_t *bip,
457 xfs_log_item_desc_t *lidp;
462 * will xfs_buf_item_unpin() call xfs_buf_item_relse()?
464 if ((atomic_read(&bip->bli_refcount) == 1) &&
465 (bip->bli_flags & XFS_BLI_STALE)) {
466 ASSERT(XFS_BUF_VALUSEMA(bip->bli_buf) <= 0);
467 xfs_buf_item_trace("UNPIN REMOVE", bip);
468 xfs_buftrace("XFS_UNPIN_REMOVE", bp);
470 * yes -- clear the xaction descriptor in-use flag
471 * and free the chunk if required. We can safely
472 * do some work here and then call buf_item_unpin
473 * to do the rest because if the if is true, then
474 * we are holding the buffer locked so no one else
475 * will be able to bump up the refcount.
477 lidp = xfs_trans_find_item(tp, (xfs_log_item_t *) bip);
478 stale = lidp->lid_flags & XFS_LID_BUF_STALE;
479 xfs_trans_free_item(tp, lidp);
481 * Since the transaction no longer refers to the buffer,
482 * the buffer should no longer refer to the transaction.
484 XFS_BUF_SET_FSPRIVATE2(bp, NULL);
487 xfs_buf_item_unpin(bip, stale);
493 * This is called to attempt to lock the buffer associated with this
494 * buf log item. Don't sleep on the buffer lock. If we can't get
495 * the lock right away, return 0. If we can get the lock, pull the
496 * buffer from the free list, mark it busy, and return 1.
499 xfs_buf_item_trylock(
500 xfs_buf_log_item_t *bip)
506 if (XFS_BUF_ISPINNED(bp)) {
507 return XFS_ITEM_PINNED;
510 if (!XFS_BUF_CPSEMA(bp)) {
511 return XFS_ITEM_LOCKED;
515 * Remove the buffer from the free list. Only do this
516 * if it's on the free list. Private buffers like the
517 * superblock buffer are not.
521 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
522 xfs_buf_item_trace("TRYLOCK SUCCESS", bip);
523 return XFS_ITEM_SUCCESS;
527 * Release the buffer associated with the buf log item.
528 * If there is no dirty logged data associated with the
529 * buffer recorded in the buf log item, then free the
530 * buf log item and remove the reference to it in the
533 * This call ignores the recursion count. It is only called
534 * when the buffer should REALLY be unlocked, regardless
535 * of the recursion count.
537 * If the XFS_BLI_HOLD flag is set in the buf log item, then
538 * free the log item if necessary but do not unlock the buffer.
539 * This is for support of xfs_trans_bhold(). Make sure the
540 * XFS_BLI_HOLD field is cleared if we don't free the item.
544 xfs_buf_log_item_t *bip)
551 xfs_buftrace("XFS_UNLOCK", bp);
554 * Clear the buffer's association with this transaction.
556 XFS_BUF_SET_FSPRIVATE2(bp, NULL);
559 * If this is a transaction abort, don't return early.
560 * Instead, allow the brelse to happen.
561 * Normally it would be done for stale (cancelled) buffers
562 * at unpin time, but we'll never go through the pin/unpin
563 * cycle if we abort inside commit.
565 aborted = (bip->bli_item.li_flags & XFS_LI_ABORTED) != 0;
568 * If the buf item is marked stale, then don't do anything.
569 * We'll unlock the buffer and free the buf item when the
570 * buffer is unpinned for the last time.
572 if (bip->bli_flags & XFS_BLI_STALE) {
573 bip->bli_flags &= ~XFS_BLI_LOGGED;
574 xfs_buf_item_trace("UNLOCK STALE", bip);
575 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
581 * Drop the transaction's reference to the log item if
582 * it was not logged as part of the transaction. Otherwise
583 * we'll drop the reference in xfs_buf_item_unpin() when
584 * the transaction is really through with the buffer.
586 if (!(bip->bli_flags & XFS_BLI_LOGGED)) {
587 atomic_dec(&bip->bli_refcount);
590 * Clear the logged flag since this is per
593 bip->bli_flags &= ~XFS_BLI_LOGGED;
597 * Before possibly freeing the buf item, determine if we should
598 * release the buffer at the end of this routine.
600 hold = bip->bli_flags & XFS_BLI_HOLD;
601 xfs_buf_item_trace("UNLOCK", bip);
604 * If the buf item isn't tracking any data, free it.
605 * Otherwise, if XFS_BLI_HOLD is set clear it.
607 if (xfs_count_bits(bip->bli_format.blf_data_map,
608 bip->bli_format.blf_map_size, 0) == 0) {
609 xfs_buf_item_relse(bp);
611 bip->bli_flags &= ~XFS_BLI_HOLD;
615 * Release the buffer if XFS_BLI_HOLD was not set.
623 * This is called to find out where the oldest active copy of the
624 * buf log item in the on disk log resides now that the last log
625 * write of it completed at the given lsn.
626 * We always re-log all the dirty data in a buffer, so usually the
627 * latest copy in the on disk log is the only one that matters. For
628 * those cases we simply return the given lsn.
630 * The one exception to this is for buffers full of newly allocated
631 * inodes. These buffers are only relogged with the XFS_BLI_INODE_BUF
632 * flag set, indicating that only the di_next_unlinked fields from the
633 * inodes in the buffers will be replayed during recovery. If the
634 * original newly allocated inode images have not yet been flushed
635 * when the buffer is so relogged, then we need to make sure that we
636 * keep the old images in the 'active' portion of the log. We do this
637 * by returning the original lsn of that transaction here rather than
641 xfs_buf_item_committed(
642 xfs_buf_log_item_t *bip,
645 xfs_buf_item_trace("COMMITTED", bip);
646 if ((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) &&
647 (bip->bli_item.li_lsn != 0)) {
648 return bip->bli_item.li_lsn;
654 * This is called when the transaction holding the buffer is aborted.
655 * Just behave as if the transaction had been cancelled. If we're shutting down
656 * and have aborted this transaction, we'll trap this buffer when it tries to
661 xfs_buf_log_item_t *bip)
666 xfs_buftrace("XFS_ABORT", bp);
667 XFS_BUF_SUPER_STALE(bp);
668 xfs_buf_item_unlock(bip);
673 * This is called to asynchronously write the buffer associated with this
674 * buf log item out to disk. The buffer will already have been locked by
675 * a successful call to xfs_buf_item_trylock(). If the buffer still has
676 * B_DELWRI set, then get it going out to disk with a call to bawrite().
677 * If not, then just release the buffer.
681 xfs_buf_log_item_t *bip)
685 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
686 xfs_buf_item_trace("PUSH", bip);
690 if (XFS_BUF_ISDELAYWRITE(bp)) {
691 xfs_bawrite(bip->bli_item.li_mountp, bp);
699 xfs_buf_item_committing(xfs_buf_log_item_t *bip, xfs_lsn_t commit_lsn)
704 * This is the ops vector shared by all buf log items.
706 struct xfs_item_ops xfs_buf_item_ops = {
707 .iop_size = (uint(*)(xfs_log_item_t*))xfs_buf_item_size,
708 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
710 .iop_pin = (void(*)(xfs_log_item_t*))xfs_buf_item_pin,
711 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_buf_item_unpin,
712 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t *))
713 xfs_buf_item_unpin_remove,
714 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_buf_item_trylock,
715 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_buf_item_unlock,
716 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
717 xfs_buf_item_committed,
718 .iop_push = (void(*)(xfs_log_item_t*))xfs_buf_item_push,
719 .iop_abort = (void(*)(xfs_log_item_t*))xfs_buf_item_abort,
721 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
722 xfs_buf_item_committing
727 * Allocate a new buf log item to go with the given buffer.
728 * Set the buffer's b_fsprivate field to point to the new
729 * buf log item. If there are other item's attached to the
730 * buffer (see xfs_buf_attach_iodone() below), then put the
731 * buf log item at the front.
739 xfs_buf_log_item_t *bip;
744 * Check to see if there is already a buf log item for
745 * this buffer. If there is, it is guaranteed to be
746 * the first. If we do already have one, there is
747 * nothing to do here so return.
749 if (XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *) != mp)
750 XFS_BUF_SET_FSPRIVATE3(bp, mp);
751 XFS_BUF_SET_BDSTRAT_FUNC(bp, xfs_bdstrat_cb);
752 if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
753 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
754 if (lip->li_type == XFS_LI_BUF) {
760 * chunks is the number of XFS_BLI_CHUNK size pieces
761 * the buffer can be divided into. Make sure not to
762 * truncate any pieces. map_size is the size of the
763 * bitmap needed to describe the chunks of the buffer.
765 chunks = (int)((XFS_BUF_COUNT(bp) + (XFS_BLI_CHUNK - 1)) >> XFS_BLI_SHIFT);
766 map_size = (int)((chunks + NBWORD) >> BIT_TO_WORD_SHIFT);
768 bip = (xfs_buf_log_item_t*)kmem_zone_zalloc(xfs_buf_item_zone,
770 bip->bli_item.li_type = XFS_LI_BUF;
771 bip->bli_item.li_ops = &xfs_buf_item_ops;
772 bip->bli_item.li_mountp = mp;
774 bip->bli_format.blf_type = XFS_LI_BUF;
775 bip->bli_format.blf_blkno = (__int64_t)XFS_BUF_ADDR(bp);
776 bip->bli_format.blf_len = (ushort)BTOBB(XFS_BUF_COUNT(bp));
777 bip->bli_format.blf_map_size = map_size;
779 bip->bli_trace = ktrace_alloc(XFS_BLI_TRACE_SIZE, KM_SLEEP);
782 #ifdef XFS_TRANS_DEBUG
784 * Allocate the arrays for tracking what needs to be logged
785 * and what our callers request to be logged. bli_orig
786 * holds a copy of the original, clean buffer for comparison
787 * against, and bli_logged keeps a 1 bit flag per byte in
788 * the buffer to indicate which bytes the callers have asked
791 bip->bli_orig = (char *)kmem_alloc(XFS_BUF_COUNT(bp), KM_SLEEP);
792 memcpy(bip->bli_orig, XFS_BUF_PTR(bp), XFS_BUF_COUNT(bp));
793 bip->bli_logged = (char *)kmem_zalloc(XFS_BUF_COUNT(bp) / NBBY, KM_SLEEP);
797 * Put the buf item into the list of items attached to the
798 * buffer at the front.
800 if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
801 bip->bli_item.li_bio_list =
802 XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
804 XFS_BUF_SET_FSPRIVATE(bp, bip);
809 * Mark bytes first through last inclusive as dirty in the buf
814 xfs_buf_log_item_t *bip,
829 * Mark the item as having some dirty data for
830 * quick reference in xfs_buf_item_dirty.
832 bip->bli_flags |= XFS_BLI_DIRTY;
835 * Convert byte offsets to bit numbers.
837 first_bit = first >> XFS_BLI_SHIFT;
838 last_bit = last >> XFS_BLI_SHIFT;
841 * Calculate the total number of bits to be set.
843 bits_to_set = last_bit - first_bit + 1;
846 * Get a pointer to the first word in the bitmap
849 word_num = first_bit >> BIT_TO_WORD_SHIFT;
850 wordp = &(bip->bli_format.blf_data_map[word_num]);
853 * Calculate the starting bit in the first word.
855 bit = first_bit & (uint)(NBWORD - 1);
858 * First set any bits in the first word of our range.
859 * If it starts at bit 0 of the word, it will be
860 * set below rather than here. That is what the variable
861 * bit tells us. The variable bits_set tracks the number
862 * of bits that have been set so far. End_bit is the number
863 * of the last bit to be set in this word plus one.
866 end_bit = MIN(bit + bits_to_set, (uint)NBWORD);
867 mask = ((1 << (end_bit - bit)) - 1) << bit;
870 bits_set = end_bit - bit;
876 * Now set bits a whole word at a time that are between
877 * first_bit and last_bit.
879 while ((bits_to_set - bits_set) >= NBWORD) {
880 *wordp |= 0xffffffff;
886 * Finally, set any bits left to be set in one last partial word.
888 end_bit = bits_to_set - bits_set;
890 mask = (1 << end_bit) - 1;
894 xfs_buf_item_log_debug(bip, first, last);
899 * Return 1 if the buffer has some data that has been logged (at any
900 * point, not just the current transaction) and 0 if not.
904 xfs_buf_log_item_t *bip)
906 return (bip->bli_flags & XFS_BLI_DIRTY);
910 * This is called when the buf log item is no longer needed. It should
911 * free the buf log item associated with the given buffer and clear
912 * the buffer's pointer to the buf log item. If there are no more
913 * items in the list, clear the b_iodone field of the buffer (see
914 * xfs_buf_attach_iodone() below).
920 xfs_buf_log_item_t *bip;
922 xfs_buftrace("XFS_RELSE", bp);
923 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
924 XFS_BUF_SET_FSPRIVATE(bp, bip->bli_item.li_bio_list);
925 if ((XFS_BUF_FSPRIVATE(bp, void *) == NULL) &&
926 (XFS_BUF_IODONE_FUNC(bp) != NULL)) {
927 ASSERT((XFS_BUF_ISUNINITIAL(bp)) == 0);
928 XFS_BUF_CLR_IODONE_FUNC(bp);
931 #ifdef XFS_TRANS_DEBUG
932 kmem_free(bip->bli_orig, XFS_BUF_COUNT(bp));
933 bip->bli_orig = NULL;
934 kmem_free(bip->bli_logged, XFS_BUF_COUNT(bp) / NBBY);
935 bip->bli_logged = NULL;
936 #endif /* XFS_TRANS_DEBUG */
939 ktrace_free(bip->bli_trace);
941 kmem_zone_free(xfs_buf_item_zone, bip);
946 * Add the given log item with its callback to the list of callbacks
947 * to be called when the buffer's I/O completes. If it is not set
948 * already, set the buffer's b_iodone() routine to be
949 * xfs_buf_iodone_callbacks() and link the log item into the list of
950 * items rooted at b_fsprivate. Items are always added as the second
951 * entry in the list if there is a first, because the buf item code
952 * assumes that the buf log item is first.
955 xfs_buf_attach_iodone(
957 void (*cb)(xfs_buf_t *, xfs_log_item_t *),
960 xfs_log_item_t *head_lip;
962 ASSERT(XFS_BUF_ISBUSY(bp));
963 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
966 if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
967 head_lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
968 lip->li_bio_list = head_lip->li_bio_list;
969 head_lip->li_bio_list = lip;
971 XFS_BUF_SET_FSPRIVATE(bp, lip);
974 ASSERT((XFS_BUF_IODONE_FUNC(bp) == xfs_buf_iodone_callbacks) ||
975 (XFS_BUF_IODONE_FUNC(bp) == NULL));
976 XFS_BUF_SET_IODONE_FUNC(bp, xfs_buf_iodone_callbacks);
980 xfs_buf_do_callbacks(
984 xfs_log_item_t *nlip;
986 while (lip != NULL) {
987 nlip = lip->li_bio_list;
988 ASSERT(lip->li_cb != NULL);
990 * Clear the next pointer so we don't have any
991 * confusion if the item is added to another buf.
992 * Don't touch the log item after calling its
993 * callback, because it could have freed itself.
995 lip->li_bio_list = NULL;
1002 * This is the iodone() function for buffers which have had callbacks
1003 * attached to them by xfs_buf_attach_iodone(). It should remove each
1004 * log item from the buffer's list and call the callback of each in turn.
1005 * When done, the buffer's fsprivate field is set to NULL and the buffer
1006 * is unlocked with a call to iodone().
1009 xfs_buf_iodone_callbacks(
1012 xfs_log_item_t *lip;
1013 static ulong lasttime;
1014 static xfs_buftarg_t *lasttarg;
1017 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
1018 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
1020 if (XFS_BUF_GETERROR(bp) != 0) {
1022 * If we've already decided to shutdown the filesystem
1023 * because of IO errors, there's no point in giving this
1026 mp = lip->li_mountp;
1027 if (XFS_FORCED_SHUTDOWN(mp)) {
1028 ASSERT(XFS_BUF_TARGET(bp) == mp->m_ddev_targp);
1029 XFS_BUF_SUPER_STALE(bp);
1030 xfs_buftrace("BUF_IODONE_CB", bp);
1031 xfs_buf_do_callbacks(bp, lip);
1032 XFS_BUF_SET_FSPRIVATE(bp, NULL);
1033 XFS_BUF_CLR_IODONE_FUNC(bp);
1036 * XFS_SHUT flag gets set when we go thru the
1037 * entire buffer cache and deliberately start
1038 * throwing away delayed write buffers.
1039 * Since there's no biowait done on those,
1040 * we should just brelse them.
1042 if (XFS_BUF_ISSHUT(bp)) {
1052 if ((XFS_BUF_TARGET(bp) != lasttarg) ||
1053 (time_after(jiffies, (lasttime + 5*HZ)))) {
1055 prdev("XFS write error in file system meta-data "
1056 "block 0x%llx in %s",
1058 (__uint64_t)XFS_BUF_ADDR(bp), mp->m_fsname);
1060 lasttarg = XFS_BUF_TARGET(bp);
1062 if (XFS_BUF_ISASYNC(bp)) {
1064 * If the write was asynchronous then noone will be
1065 * looking for the error. Clear the error state
1066 * and write the buffer out again delayed write.
1068 * XXXsup This is OK, so long as we catch these
1069 * before we start the umount; we don't want these
1070 * DELWRI metadata bufs to be hanging around.
1072 XFS_BUF_ERROR(bp,0); /* errno of 0 unsets the flag */
1074 if (!(XFS_BUF_ISSTALE(bp))) {
1075 XFS_BUF_DELAYWRITE(bp);
1077 XFS_BUF_SET_START(bp);
1079 ASSERT(XFS_BUF_IODONE_FUNC(bp));
1080 xfs_buftrace("BUF_IODONE ASYNC", bp);
1084 * If the write of the buffer was not asynchronous,
1085 * then we want to make sure to return the error
1086 * to the caller of bwrite(). Because of this we
1087 * cannot clear the B_ERROR state at this point.
1088 * Instead we install a callback function that
1089 * will be called when the buffer is released, and
1090 * that routine will clear the error state and
1091 * set the buffer to be written out again after
1094 /* We actually overwrite the existing b-relse
1095 function at times, but we're gonna be shutting down
1097 XFS_BUF_SET_BRELSE_FUNC(bp,xfs_buf_error_relse);
1099 XFS_BUF_V_IODONESEMA(bp);
1103 #ifdef XFSERRORDEBUG
1104 xfs_buftrace("XFS BUFCB NOERR", bp);
1106 xfs_buf_do_callbacks(bp, lip);
1107 XFS_BUF_SET_FSPRIVATE(bp, NULL);
1108 XFS_BUF_CLR_IODONE_FUNC(bp);
1113 * This is a callback routine attached to a buffer which gets an error
1114 * when being written out synchronously.
1117 xfs_buf_error_relse(
1120 xfs_log_item_t *lip;
1123 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
1124 mp = (xfs_mount_t *)lip->li_mountp;
1125 ASSERT(XFS_BUF_TARGET(bp) == mp->m_ddev_targp);
1129 XFS_BUF_UNDELAYWRITE(bp);
1130 XFS_BUF_ERROR(bp,0);
1131 xfs_buftrace("BUF_ERROR_RELSE", bp);
1132 if (! XFS_FORCED_SHUTDOWN(mp))
1133 xfs_force_shutdown(mp, XFS_METADATA_IO_ERROR);
1135 * We have to unpin the pinned buffers so do the
1138 xfs_buf_do_callbacks(bp, lip);
1139 XFS_BUF_SET_FSPRIVATE(bp, NULL);
1140 XFS_BUF_CLR_IODONE_FUNC(bp);
1141 XFS_BUF_SET_BRELSE_FUNC(bp,NULL);
1147 * This is the iodone() function for buffers which have been
1148 * logged. It is called when they are eventually flushed out.
1149 * It should remove the buf item from the AIL, and free the buf item.
1150 * It is called by xfs_buf_iodone_callbacks() above which will take
1151 * care of cleaning up the buffer itself.
1157 xfs_buf_log_item_t *bip)
1159 struct xfs_mount *mp;
1162 ASSERT(bip->bli_buf == bp);
1164 mp = bip->bli_item.li_mountp;
1167 * If we are forcibly shutting down, this may well be
1168 * off the AIL already. That's because we simulate the
1169 * log-committed callbacks to unpin these buffers. Or we may never
1170 * have put this item on AIL because of the transaction was
1171 * aborted forcibly. xfs_trans_delete_ail() takes care of these.
1173 * Either way, AIL is useless if we're forcing a shutdown.
1177 * xfs_trans_delete_ail() drops the AIL lock.
1179 xfs_trans_delete_ail(mp, (xfs_log_item_t *)bip, s);
1181 #ifdef XFS_TRANS_DEBUG
1182 kmem_free(bip->bli_orig, XFS_BUF_COUNT(bp));
1183 bip->bli_orig = NULL;
1184 kmem_free(bip->bli_logged, XFS_BUF_COUNT(bp) / NBBY);
1185 bip->bli_logged = NULL;
1186 #endif /* XFS_TRANS_DEBUG */
1188 #ifdef XFS_BLI_TRACE
1189 ktrace_free(bip->bli_trace);
1191 kmem_zone_free(xfs_buf_item_zone, bip);
1194 #if defined(XFS_BLI_TRACE)
1198 xfs_buf_log_item_t *bip)
1201 ASSERT(bip->bli_trace != NULL);
1204 ktrace_enter(bip->bli_trace,
1206 (void *)bip->bli_buf,
1207 (void *)((unsigned long)bip->bli_flags),
1208 (void *)((unsigned long)bip->bli_recur),
1209 (void *)((unsigned long)atomic_read(&bip->bli_refcount)),
1210 (void *)((unsigned long)
1211 (0xFFFFFFFF & XFS_BUF_ADDR(bp) >> 32)),
1212 (void *)((unsigned long)(0xFFFFFFFF & XFS_BUF_ADDR(bp))),
1213 (void *)((unsigned long)XFS_BUF_COUNT(bp)),
1214 (void *)((unsigned long)XFS_BUF_BFLAGS(bp)),
1215 XFS_BUF_FSPRIVATE(bp, void *),
1216 XFS_BUF_FSPRIVATE2(bp, void *),
1217 (void *)(unsigned long)XFS_BUF_ISPINNED(bp),
1218 (void *)XFS_BUF_IODONE_FUNC(bp),
1219 (void *)((unsigned long)(XFS_BUF_VALUSEMA(bp))),
1220 (void *)bip->bli_item.li_desc,
1221 (void *)((unsigned long)bip->bli_item.li_flags));
1223 #endif /* XFS_BLI_TRACE */