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 kmem_zone_t *xfs_buf_item_zone;
60 #ifdef XFS_TRANS_DEBUG
62 * This function uses an alternate strategy for tracking the bytes
63 * that the user requests to be logged. This can then be used
64 * in conjunction with the bli_orig array in the buf log item to
65 * catch bugs in our callers' code.
67 * We also double check the bits set in xfs_buf_item_log using a
68 * simple algorithm to check that every byte is accounted for.
71 xfs_buf_item_log_debug(
72 xfs_buf_log_item_t *bip,
85 ASSERT(bip->bli_logged != NULL);
87 nbytes = last - first + 1;
88 bfset(bip->bli_logged, first, nbytes);
89 for (x = 0; x < nbytes; x++) {
90 chunk_num = byte >> XFS_BLI_SHIFT;
91 word_num = chunk_num >> BIT_TO_WORD_SHIFT;
92 bit_num = chunk_num & (NBWORD - 1);
93 wordp = &(bip->bli_format.blf_data_map[word_num]);
94 bit_set = *wordp & (1 << bit_num);
101 * This function is called when we flush something into a buffer without
102 * logging it. This happens for things like inodes which are logged
103 * separately from the buffer.
106 xfs_buf_item_flush_log_debug(
111 xfs_buf_log_item_t *bip;
114 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
115 if ((bip == NULL) || (bip->bli_item.li_type != XFS_LI_BUF)) {
119 ASSERT(bip->bli_logged != NULL);
120 nbytes = last - first + 1;
121 bfset(bip->bli_logged, first, nbytes);
125 * This function is called to verify that our caller's have logged
126 * all the bytes that they changed.
128 * It does this by comparing the original copy of the buffer stored in
129 * the buf log item's bli_orig array to the current copy of the buffer
130 * and ensuring that all bytes which miscompare are set in the bli_logged
131 * array of the buf log item.
134 xfs_buf_item_log_check(
135 xfs_buf_log_item_t *bip)
142 ASSERT(bip->bli_orig != NULL);
143 ASSERT(bip->bli_logged != NULL);
146 ASSERT(XFS_BUF_COUNT(bp) > 0);
147 ASSERT(XFS_BUF_PTR(bp) != NULL);
148 orig = bip->bli_orig;
149 buffer = XFS_BUF_PTR(bp);
150 for (x = 0; x < XFS_BUF_COUNT(bp); x++) {
151 if (orig[x] != buffer[x] && !btst(bip->bli_logged, x))
153 "xfs_buf_item_log_check bip %x buffer %x orig %x index %d",
158 #define xfs_buf_item_log_debug(x,y,z)
159 #define xfs_buf_item_log_check(x)
162 STATIC void xfs_buf_error_relse(xfs_buf_t *bp);
163 STATIC void xfs_buf_do_callbacks(xfs_buf_t *bp, xfs_log_item_t *lip);
166 * This returns the number of log iovecs needed to log the
167 * given buf log item.
169 * It calculates this as 1 iovec for the buf log format structure
170 * and 1 for each stretch of non-contiguous chunks to be logged.
171 * Contiguous chunks are logged in a single iovec.
173 * If the XFS_BLI_STALE flag has been set, then log nothing.
177 xfs_buf_log_item_t *bip)
184 ASSERT(atomic_read(&bip->bli_refcount) > 0);
185 if (bip->bli_flags & XFS_BLI_STALE) {
187 * The buffer is stale, so all we need to log
188 * is the buf log format structure with the
191 xfs_buf_item_trace("SIZE STALE", bip);
192 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
197 ASSERT(bip->bli_flags & XFS_BLI_LOGGED);
199 last_bit = xfs_next_bit(bip->bli_format.blf_data_map,
200 bip->bli_format.blf_map_size, 0);
201 ASSERT(last_bit != -1);
203 while (last_bit != -1) {
205 * This takes the bit number to start looking from and
206 * returns the next set bit from there. It returns -1
207 * if there are no more bits set or the start bit is
208 * beyond the end of the bitmap.
210 next_bit = xfs_next_bit(bip->bli_format.blf_data_map,
211 bip->bli_format.blf_map_size,
214 * If we run out of bits, leave the loop,
215 * else if we find a new set of bits bump the number of vecs,
216 * else keep scanning the current set of bits.
218 if (next_bit == -1) {
220 } else if (next_bit != last_bit + 1) {
223 } else if (xfs_buf_offset(bp, next_bit * XFS_BLI_CHUNK) !=
224 (xfs_buf_offset(bp, last_bit * XFS_BLI_CHUNK) +
233 xfs_buf_item_trace("SIZE NORM", bip);
238 * This is called to fill in the vector of log iovecs for the
239 * given log buf item. It fills the first entry with a buf log
240 * format structure, and the rest point to contiguous chunks
245 xfs_buf_log_item_t *bip,
246 xfs_log_iovec_t *log_vector)
250 xfs_log_iovec_t *vecp;
258 ASSERT(atomic_read(&bip->bli_refcount) > 0);
259 ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
260 (bip->bli_flags & XFS_BLI_STALE));
262 ASSERT(XFS_BUF_BP_ISMAPPED(bp));
266 * The size of the base structure is the size of the
267 * declared structure plus the space for the extra words
268 * of the bitmap. We subtract one from the map size, because
269 * the first element of the bitmap is accounted for in the
270 * size of the base structure.
273 (uint)(sizeof(xfs_buf_log_format_t) +
274 ((bip->bli_format.blf_map_size - 1) * sizeof(uint)));
275 vecp->i_addr = (xfs_caddr_t)&bip->bli_format;
276 vecp->i_len = base_size;
277 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_BFORMAT);
281 if (bip->bli_flags & XFS_BLI_STALE) {
283 * The buffer is stale, so all we need to log
284 * is the buf log format structure with the
287 xfs_buf_item_trace("FORMAT STALE", bip);
288 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
289 bip->bli_format.blf_size = nvecs;
294 * Fill in an iovec for each set of contiguous chunks.
296 first_bit = xfs_next_bit(bip->bli_format.blf_data_map,
297 bip->bli_format.blf_map_size, 0);
298 ASSERT(first_bit != -1);
299 last_bit = first_bit;
303 * This takes the bit number to start looking from and
304 * returns the next set bit from there. It returns -1
305 * if there are no more bits set or the start bit is
306 * beyond the end of the bitmap.
308 next_bit = xfs_next_bit(bip->bli_format.blf_data_map,
309 bip->bli_format.blf_map_size,
312 * If we run out of bits fill in the last iovec and get
314 * Else if we start a new set of bits then fill in the
315 * iovec for the series we were looking at and start
316 * counting the bits in the new one.
317 * Else we're still in the same set of bits so just
318 * keep counting and scanning.
320 if (next_bit == -1) {
321 buffer_offset = first_bit * XFS_BLI_CHUNK;
322 vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
323 vecp->i_len = nbits * XFS_BLI_CHUNK;
324 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_BCHUNK);
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;
331 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_BCHUNK);
334 first_bit = next_bit;
337 } else if (xfs_buf_offset(bp, next_bit << XFS_BLI_SHIFT) !=
338 (xfs_buf_offset(bp, last_bit << XFS_BLI_SHIFT) +
340 buffer_offset = first_bit * XFS_BLI_CHUNK;
341 vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
342 vecp->i_len = nbits * XFS_BLI_CHUNK;
343 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_BCHUNK);
344 /* You would think we need to bump the nvecs here too, but we do not
345 * this number is used by recovery, and it gets confused by the boundary
350 first_bit = next_bit;
358 bip->bli_format.blf_size = nvecs;
361 * Check to make sure everything is consistent.
363 xfs_buf_item_trace("FORMAT NORM", bip);
364 xfs_buf_item_log_check(bip);
368 * This is called to pin the buffer associated with the buf log
369 * item in memory so it cannot be written out. Simply call bpin()
370 * on the buffer to do this.
374 xfs_buf_log_item_t *bip)
379 ASSERT(XFS_BUF_ISBUSY(bp));
380 ASSERT(atomic_read(&bip->bli_refcount) > 0);
381 ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
382 (bip->bli_flags & XFS_BLI_STALE));
383 xfs_buf_item_trace("PIN", bip);
384 xfs_buftrace("XFS_PIN", bp);
390 * This is called to unpin the buffer associated with the buf log
391 * item which was previously pinned with a call to xfs_buf_item_pin().
392 * Just call bunpin() on the buffer to do this.
394 * Also drop the reference to the buf item for the current transaction.
395 * If the XFS_BLI_STALE flag is set and we are the last reference,
396 * then free up the buf log item and unlock the buffer.
400 xfs_buf_log_item_t *bip,
410 ASSERT(XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *) == bip);
411 ASSERT(atomic_read(&bip->bli_refcount) > 0);
412 xfs_buf_item_trace("UNPIN", bip);
413 xfs_buftrace("XFS_UNPIN", bp);
415 freed = atomic_dec_and_test(&bip->bli_refcount);
416 mp = bip->bli_item.li_mountp;
418 if (freed && stale) {
419 ASSERT(bip->bli_flags & XFS_BLI_STALE);
420 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
421 ASSERT(!(XFS_BUF_ISDELAYWRITE(bp)));
422 ASSERT(XFS_BUF_ISSTALE(bp));
423 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
424 xfs_buf_item_trace("UNPIN STALE", bip);
425 xfs_buftrace("XFS_UNPIN STALE", bp);
427 * If we get called here because of an IO error, we may
428 * or may not have the item on the AIL. xfs_trans_delete_ail()
429 * will take care of that situation.
430 * xfs_trans_delete_ail() drops the AIL lock.
432 if (bip->bli_flags & XFS_BLI_STALE_INODE) {
433 xfs_buf_do_callbacks(bp, (xfs_log_item_t *)bip);
434 XFS_BUF_SET_FSPRIVATE(bp, NULL);
435 XFS_BUF_CLR_IODONE_FUNC(bp);
438 xfs_trans_delete_ail(mp, (xfs_log_item_t *)bip, s);
439 xfs_buf_item_relse(bp);
440 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) == NULL);
447 * this is called from uncommit in the forced-shutdown path.
448 * we need to check to see if the reference count on the log item
449 * is going to drop to zero. If so, unpin will free the log item
450 * so we need to free the item's descriptor (that points to the item)
451 * in the transaction.
454 xfs_buf_item_unpin_remove(
455 xfs_buf_log_item_t *bip,
459 xfs_log_item_desc_t *lidp;
464 * will xfs_buf_item_unpin() call xfs_buf_item_relse()?
466 if ((atomic_read(&bip->bli_refcount) == 1) &&
467 (bip->bli_flags & XFS_BLI_STALE)) {
468 ASSERT(XFS_BUF_VALUSEMA(bip->bli_buf) <= 0);
469 xfs_buf_item_trace("UNPIN REMOVE", bip);
470 xfs_buftrace("XFS_UNPIN_REMOVE", bp);
472 * yes -- clear the xaction descriptor in-use flag
473 * and free the chunk if required. We can safely
474 * do some work here and then call buf_item_unpin
475 * to do the rest because if the if is true, then
476 * we are holding the buffer locked so no one else
477 * will be able to bump up the refcount.
479 lidp = xfs_trans_find_item(tp, (xfs_log_item_t *) bip);
480 stale = lidp->lid_flags & XFS_LID_BUF_STALE;
481 xfs_trans_free_item(tp, lidp);
483 * Since the transaction no longer refers to the buffer,
484 * the buffer should no longer refer to the transaction.
486 XFS_BUF_SET_FSPRIVATE2(bp, NULL);
489 xfs_buf_item_unpin(bip, stale);
495 * This is called to attempt to lock the buffer associated with this
496 * buf log item. Don't sleep on the buffer lock. If we can't get
497 * the lock right away, return 0. If we can get the lock, pull the
498 * buffer from the free list, mark it busy, and return 1.
501 xfs_buf_item_trylock(
502 xfs_buf_log_item_t *bip)
508 if (XFS_BUF_ISPINNED(bp)) {
509 return XFS_ITEM_PINNED;
512 if (!XFS_BUF_CPSEMA(bp)) {
513 return XFS_ITEM_LOCKED;
517 * Remove the buffer from the free list. Only do this
518 * if it's on the free list. Private buffers like the
519 * superblock buffer are not.
523 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
524 xfs_buf_item_trace("TRYLOCK SUCCESS", bip);
525 return XFS_ITEM_SUCCESS;
529 * Release the buffer associated with the buf log item.
530 * If there is no dirty logged data associated with the
531 * buffer recorded in the buf log item, then free the
532 * buf log item and remove the reference to it in the
535 * This call ignores the recursion count. It is only called
536 * when the buffer should REALLY be unlocked, regardless
537 * of the recursion count.
539 * If the XFS_BLI_HOLD flag is set in the buf log item, then
540 * free the log item if necessary but do not unlock the buffer.
541 * This is for support of xfs_trans_bhold(). Make sure the
542 * XFS_BLI_HOLD field is cleared if we don't free the item.
546 xfs_buf_log_item_t *bip)
553 xfs_buftrace("XFS_UNLOCK", bp);
556 * Clear the buffer's association with this transaction.
558 XFS_BUF_SET_FSPRIVATE2(bp, NULL);
561 * If this is a transaction abort, don't return early.
562 * Instead, allow the brelse to happen.
563 * Normally it would be done for stale (cancelled) buffers
564 * at unpin time, but we'll never go through the pin/unpin
565 * cycle if we abort inside commit.
567 aborted = (bip->bli_item.li_flags & XFS_LI_ABORTED) != 0;
570 * If the buf item is marked stale, then don't do anything.
571 * We'll unlock the buffer and free the buf item when the
572 * buffer is unpinned for the last time.
574 if (bip->bli_flags & XFS_BLI_STALE) {
575 bip->bli_flags &= ~XFS_BLI_LOGGED;
576 xfs_buf_item_trace("UNLOCK STALE", bip);
577 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
583 * Drop the transaction's reference to the log item if
584 * it was not logged as part of the transaction. Otherwise
585 * we'll drop the reference in xfs_buf_item_unpin() when
586 * the transaction is really through with the buffer.
588 if (!(bip->bli_flags & XFS_BLI_LOGGED)) {
589 atomic_dec(&bip->bli_refcount);
592 * Clear the logged flag since this is per
595 bip->bli_flags &= ~XFS_BLI_LOGGED;
599 * Before possibly freeing the buf item, determine if we should
600 * release the buffer at the end of this routine.
602 hold = bip->bli_flags & XFS_BLI_HOLD;
603 xfs_buf_item_trace("UNLOCK", bip);
606 * If the buf item isn't tracking any data, free it.
607 * Otherwise, if XFS_BLI_HOLD is set clear it.
609 if (xfs_count_bits(bip->bli_format.blf_data_map,
610 bip->bli_format.blf_map_size, 0) == 0) {
611 xfs_buf_item_relse(bp);
613 bip->bli_flags &= ~XFS_BLI_HOLD;
617 * Release the buffer if XFS_BLI_HOLD was not set.
625 * This is called to find out where the oldest active copy of the
626 * buf log item in the on disk log resides now that the last log
627 * write of it completed at the given lsn.
628 * We always re-log all the dirty data in a buffer, so usually the
629 * latest copy in the on disk log is the only one that matters. For
630 * those cases we simply return the given lsn.
632 * The one exception to this is for buffers full of newly allocated
633 * inodes. These buffers are only relogged with the XFS_BLI_INODE_BUF
634 * flag set, indicating that only the di_next_unlinked fields from the
635 * inodes in the buffers will be replayed during recovery. If the
636 * original newly allocated inode images have not yet been flushed
637 * when the buffer is so relogged, then we need to make sure that we
638 * keep the old images in the 'active' portion of the log. We do this
639 * by returning the original lsn of that transaction here rather than
643 xfs_buf_item_committed(
644 xfs_buf_log_item_t *bip,
647 xfs_buf_item_trace("COMMITTED", bip);
648 if ((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) &&
649 (bip->bli_item.li_lsn != 0)) {
650 return bip->bli_item.li_lsn;
656 * This is called when the transaction holding the buffer is aborted.
657 * Just behave as if the transaction had been cancelled. If we're shutting down
658 * and have aborted this transaction, we'll trap this buffer when it tries to
663 xfs_buf_log_item_t *bip)
668 xfs_buftrace("XFS_ABORT", bp);
669 XFS_BUF_SUPER_STALE(bp);
670 xfs_buf_item_unlock(bip);
675 * This is called to asynchronously write the buffer associated with this
676 * buf log item out to disk. The buffer will already have been locked by
677 * a successful call to xfs_buf_item_trylock(). If the buffer still has
678 * B_DELWRI set, then get it going out to disk with a call to bawrite().
679 * If not, then just release the buffer.
683 xfs_buf_log_item_t *bip)
687 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
688 xfs_buf_item_trace("PUSH", bip);
692 if (XFS_BUF_ISDELAYWRITE(bp)) {
693 xfs_bawrite(bip->bli_item.li_mountp, bp);
701 xfs_buf_item_committing(xfs_buf_log_item_t *bip, xfs_lsn_t commit_lsn)
706 * This is the ops vector shared by all buf log items.
708 STATIC struct xfs_item_ops xfs_buf_item_ops = {
709 .iop_size = (uint(*)(xfs_log_item_t*))xfs_buf_item_size,
710 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
712 .iop_pin = (void(*)(xfs_log_item_t*))xfs_buf_item_pin,
713 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_buf_item_unpin,
714 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t *))
715 xfs_buf_item_unpin_remove,
716 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_buf_item_trylock,
717 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_buf_item_unlock,
718 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
719 xfs_buf_item_committed,
720 .iop_push = (void(*)(xfs_log_item_t*))xfs_buf_item_push,
721 .iop_abort = (void(*)(xfs_log_item_t*))xfs_buf_item_abort,
723 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
724 xfs_buf_item_committing
729 * Allocate a new buf log item to go with the given buffer.
730 * Set the buffer's b_fsprivate field to point to the new
731 * buf log item. If there are other item's attached to the
732 * buffer (see xfs_buf_attach_iodone() below), then put the
733 * buf log item at the front.
741 xfs_buf_log_item_t *bip;
746 * Check to see if there is already a buf log item for
747 * this buffer. If there is, it is guaranteed to be
748 * the first. If we do already have one, there is
749 * nothing to do here so return.
751 if (XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *) != mp)
752 XFS_BUF_SET_FSPRIVATE3(bp, mp);
753 XFS_BUF_SET_BDSTRAT_FUNC(bp, xfs_bdstrat_cb);
754 if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
755 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
756 if (lip->li_type == XFS_LI_BUF) {
762 * chunks is the number of XFS_BLI_CHUNK size pieces
763 * the buffer can be divided into. Make sure not to
764 * truncate any pieces. map_size is the size of the
765 * bitmap needed to describe the chunks of the buffer.
767 chunks = (int)((XFS_BUF_COUNT(bp) + (XFS_BLI_CHUNK - 1)) >> XFS_BLI_SHIFT);
768 map_size = (int)((chunks + NBWORD) >> BIT_TO_WORD_SHIFT);
770 bip = (xfs_buf_log_item_t*)kmem_zone_zalloc(xfs_buf_item_zone,
772 bip->bli_item.li_type = XFS_LI_BUF;
773 bip->bli_item.li_ops = &xfs_buf_item_ops;
774 bip->bli_item.li_mountp = mp;
776 bip->bli_format.blf_type = XFS_LI_BUF;
777 bip->bli_format.blf_blkno = (__int64_t)XFS_BUF_ADDR(bp);
778 bip->bli_format.blf_len = (ushort)BTOBB(XFS_BUF_COUNT(bp));
779 bip->bli_format.blf_map_size = map_size;
781 bip->bli_trace = ktrace_alloc(XFS_BLI_TRACE_SIZE, KM_SLEEP);
784 #ifdef XFS_TRANS_DEBUG
786 * Allocate the arrays for tracking what needs to be logged
787 * and what our callers request to be logged. bli_orig
788 * holds a copy of the original, clean buffer for comparison
789 * against, and bli_logged keeps a 1 bit flag per byte in
790 * the buffer to indicate which bytes the callers have asked
793 bip->bli_orig = (char *)kmem_alloc(XFS_BUF_COUNT(bp), KM_SLEEP);
794 memcpy(bip->bli_orig, XFS_BUF_PTR(bp), XFS_BUF_COUNT(bp));
795 bip->bli_logged = (char *)kmem_zalloc(XFS_BUF_COUNT(bp) / NBBY, KM_SLEEP);
799 * Put the buf item into the list of items attached to the
800 * buffer at the front.
802 if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
803 bip->bli_item.li_bio_list =
804 XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
806 XFS_BUF_SET_FSPRIVATE(bp, bip);
811 * Mark bytes first through last inclusive as dirty in the buf
816 xfs_buf_log_item_t *bip,
831 * Mark the item as having some dirty data for
832 * quick reference in xfs_buf_item_dirty.
834 bip->bli_flags |= XFS_BLI_DIRTY;
837 * Convert byte offsets to bit numbers.
839 first_bit = first >> XFS_BLI_SHIFT;
840 last_bit = last >> XFS_BLI_SHIFT;
843 * Calculate the total number of bits to be set.
845 bits_to_set = last_bit - first_bit + 1;
848 * Get a pointer to the first word in the bitmap
851 word_num = first_bit >> BIT_TO_WORD_SHIFT;
852 wordp = &(bip->bli_format.blf_data_map[word_num]);
855 * Calculate the starting bit in the first word.
857 bit = first_bit & (uint)(NBWORD - 1);
860 * First set any bits in the first word of our range.
861 * If it starts at bit 0 of the word, it will be
862 * set below rather than here. That is what the variable
863 * bit tells us. The variable bits_set tracks the number
864 * of bits that have been set so far. End_bit is the number
865 * of the last bit to be set in this word plus one.
868 end_bit = MIN(bit + bits_to_set, (uint)NBWORD);
869 mask = ((1 << (end_bit - bit)) - 1) << bit;
872 bits_set = end_bit - bit;
878 * Now set bits a whole word at a time that are between
879 * first_bit and last_bit.
881 while ((bits_to_set - bits_set) >= NBWORD) {
882 *wordp |= 0xffffffff;
888 * Finally, set any bits left to be set in one last partial word.
890 end_bit = bits_to_set - bits_set;
892 mask = (1 << end_bit) - 1;
896 xfs_buf_item_log_debug(bip, first, last);
901 * Return 1 if the buffer has some data that has been logged (at any
902 * point, not just the current transaction) and 0 if not.
906 xfs_buf_log_item_t *bip)
908 return (bip->bli_flags & XFS_BLI_DIRTY);
912 * This is called when the buf log item is no longer needed. It should
913 * free the buf log item associated with the given buffer and clear
914 * the buffer's pointer to the buf log item. If there are no more
915 * items in the list, clear the b_iodone field of the buffer (see
916 * xfs_buf_attach_iodone() below).
922 xfs_buf_log_item_t *bip;
924 xfs_buftrace("XFS_RELSE", bp);
925 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
926 XFS_BUF_SET_FSPRIVATE(bp, bip->bli_item.li_bio_list);
927 if ((XFS_BUF_FSPRIVATE(bp, void *) == NULL) &&
928 (XFS_BUF_IODONE_FUNC(bp) != NULL)) {
929 ASSERT((XFS_BUF_ISUNINITIAL(bp)) == 0);
930 XFS_BUF_CLR_IODONE_FUNC(bp);
933 #ifdef XFS_TRANS_DEBUG
934 kmem_free(bip->bli_orig, XFS_BUF_COUNT(bp));
935 bip->bli_orig = NULL;
936 kmem_free(bip->bli_logged, XFS_BUF_COUNT(bp) / NBBY);
937 bip->bli_logged = NULL;
938 #endif /* XFS_TRANS_DEBUG */
941 ktrace_free(bip->bli_trace);
943 kmem_zone_free(xfs_buf_item_zone, bip);
948 * Add the given log item with its callback to the list of callbacks
949 * to be called when the buffer's I/O completes. If it is not set
950 * already, set the buffer's b_iodone() routine to be
951 * xfs_buf_iodone_callbacks() and link the log item into the list of
952 * items rooted at b_fsprivate. Items are always added as the second
953 * entry in the list if there is a first, because the buf item code
954 * assumes that the buf log item is first.
957 xfs_buf_attach_iodone(
959 void (*cb)(xfs_buf_t *, xfs_log_item_t *),
962 xfs_log_item_t *head_lip;
964 ASSERT(XFS_BUF_ISBUSY(bp));
965 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
968 if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
969 head_lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
970 lip->li_bio_list = head_lip->li_bio_list;
971 head_lip->li_bio_list = lip;
973 XFS_BUF_SET_FSPRIVATE(bp, lip);
976 ASSERT((XFS_BUF_IODONE_FUNC(bp) == xfs_buf_iodone_callbacks) ||
977 (XFS_BUF_IODONE_FUNC(bp) == NULL));
978 XFS_BUF_SET_IODONE_FUNC(bp, xfs_buf_iodone_callbacks);
982 xfs_buf_do_callbacks(
986 xfs_log_item_t *nlip;
988 while (lip != NULL) {
989 nlip = lip->li_bio_list;
990 ASSERT(lip->li_cb != NULL);
992 * Clear the next pointer so we don't have any
993 * confusion if the item is added to another buf.
994 * Don't touch the log item after calling its
995 * callback, because it could have freed itself.
997 lip->li_bio_list = NULL;
1004 * This is the iodone() function for buffers which have had callbacks
1005 * attached to them by xfs_buf_attach_iodone(). It should remove each
1006 * log item from the buffer's list and call the callback of each in turn.
1007 * When done, the buffer's fsprivate field is set to NULL and the buffer
1008 * is unlocked with a call to iodone().
1011 xfs_buf_iodone_callbacks(
1014 xfs_log_item_t *lip;
1015 static ulong lasttime;
1016 static xfs_buftarg_t *lasttarg;
1019 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
1020 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
1022 if (XFS_BUF_GETERROR(bp) != 0) {
1024 * If we've already decided to shutdown the filesystem
1025 * because of IO errors, there's no point in giving this
1028 mp = lip->li_mountp;
1029 if (XFS_FORCED_SHUTDOWN(mp)) {
1030 ASSERT(XFS_BUF_TARGET(bp) == mp->m_ddev_targp);
1031 XFS_BUF_SUPER_STALE(bp);
1032 xfs_buftrace("BUF_IODONE_CB", bp);
1033 xfs_buf_do_callbacks(bp, lip);
1034 XFS_BUF_SET_FSPRIVATE(bp, NULL);
1035 XFS_BUF_CLR_IODONE_FUNC(bp);
1038 * XFS_SHUT flag gets set when we go thru the
1039 * entire buffer cache and deliberately start
1040 * throwing away delayed write buffers.
1041 * Since there's no biowait done on those,
1042 * we should just brelse them.
1044 if (XFS_BUF_ISSHUT(bp)) {
1054 if ((XFS_BUF_TARGET(bp) != lasttarg) ||
1055 (time_after(jiffies, (lasttime + 5*HZ)))) {
1057 prdev("XFS write error in file system meta-data "
1058 "block 0x%llx in %s",
1060 (__uint64_t)XFS_BUF_ADDR(bp), mp->m_fsname);
1062 lasttarg = XFS_BUF_TARGET(bp);
1064 if (XFS_BUF_ISASYNC(bp)) {
1066 * If the write was asynchronous then noone will be
1067 * looking for the error. Clear the error state
1068 * and write the buffer out again delayed write.
1070 * XXXsup This is OK, so long as we catch these
1071 * before we start the umount; we don't want these
1072 * DELWRI metadata bufs to be hanging around.
1074 XFS_BUF_ERROR(bp,0); /* errno of 0 unsets the flag */
1076 if (!(XFS_BUF_ISSTALE(bp))) {
1077 XFS_BUF_DELAYWRITE(bp);
1079 XFS_BUF_SET_START(bp);
1081 ASSERT(XFS_BUF_IODONE_FUNC(bp));
1082 xfs_buftrace("BUF_IODONE ASYNC", bp);
1086 * If the write of the buffer was not asynchronous,
1087 * then we want to make sure to return the error
1088 * to the caller of bwrite(). Because of this we
1089 * cannot clear the B_ERROR state at this point.
1090 * Instead we install a callback function that
1091 * will be called when the buffer is released, and
1092 * that routine will clear the error state and
1093 * set the buffer to be written out again after
1096 /* We actually overwrite the existing b-relse
1097 function at times, but we're gonna be shutting down
1099 XFS_BUF_SET_BRELSE_FUNC(bp,xfs_buf_error_relse);
1101 XFS_BUF_V_IODONESEMA(bp);
1105 #ifdef XFSERRORDEBUG
1106 xfs_buftrace("XFS BUFCB NOERR", bp);
1108 xfs_buf_do_callbacks(bp, lip);
1109 XFS_BUF_SET_FSPRIVATE(bp, NULL);
1110 XFS_BUF_CLR_IODONE_FUNC(bp);
1115 * This is a callback routine attached to a buffer which gets an error
1116 * when being written out synchronously.
1119 xfs_buf_error_relse(
1122 xfs_log_item_t *lip;
1125 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
1126 mp = (xfs_mount_t *)lip->li_mountp;
1127 ASSERT(XFS_BUF_TARGET(bp) == mp->m_ddev_targp);
1131 XFS_BUF_UNDELAYWRITE(bp);
1132 XFS_BUF_ERROR(bp,0);
1133 xfs_buftrace("BUF_ERROR_RELSE", bp);
1134 if (! XFS_FORCED_SHUTDOWN(mp))
1135 xfs_force_shutdown(mp, XFS_METADATA_IO_ERROR);
1137 * We have to unpin the pinned buffers so do the
1140 xfs_buf_do_callbacks(bp, lip);
1141 XFS_BUF_SET_FSPRIVATE(bp, NULL);
1142 XFS_BUF_CLR_IODONE_FUNC(bp);
1143 XFS_BUF_SET_BRELSE_FUNC(bp,NULL);
1149 * This is the iodone() function for buffers which have been
1150 * logged. It is called when they are eventually flushed out.
1151 * It should remove the buf item from the AIL, and free the buf item.
1152 * It is called by xfs_buf_iodone_callbacks() above which will take
1153 * care of cleaning up the buffer itself.
1159 xfs_buf_log_item_t *bip)
1161 struct xfs_mount *mp;
1164 ASSERT(bip->bli_buf == bp);
1166 mp = bip->bli_item.li_mountp;
1169 * If we are forcibly shutting down, this may well be
1170 * off the AIL already. That's because we simulate the
1171 * log-committed callbacks to unpin these buffers. Or we may never
1172 * have put this item on AIL because of the transaction was
1173 * aborted forcibly. xfs_trans_delete_ail() takes care of these.
1175 * Either way, AIL is useless if we're forcing a shutdown.
1179 * xfs_trans_delete_ail() drops the AIL lock.
1181 xfs_trans_delete_ail(mp, (xfs_log_item_t *)bip, s);
1183 #ifdef XFS_TRANS_DEBUG
1184 kmem_free(bip->bli_orig, XFS_BUF_COUNT(bp));
1185 bip->bli_orig = NULL;
1186 kmem_free(bip->bli_logged, XFS_BUF_COUNT(bp) / NBBY);
1187 bip->bli_logged = NULL;
1188 #endif /* XFS_TRANS_DEBUG */
1190 #ifdef XFS_BLI_TRACE
1191 ktrace_free(bip->bli_trace);
1193 kmem_zone_free(xfs_buf_item_zone, bip);
1196 #if defined(XFS_BLI_TRACE)
1200 xfs_buf_log_item_t *bip)
1203 ASSERT(bip->bli_trace != NULL);
1206 ktrace_enter(bip->bli_trace,
1208 (void *)bip->bli_buf,
1209 (void *)((unsigned long)bip->bli_flags),
1210 (void *)((unsigned long)bip->bli_recur),
1211 (void *)((unsigned long)atomic_read(&bip->bli_refcount)),
1212 (void *)((unsigned long)
1213 (0xFFFFFFFF & XFS_BUF_ADDR(bp) >> 32)),
1214 (void *)((unsigned long)(0xFFFFFFFF & XFS_BUF_ADDR(bp))),
1215 (void *)((unsigned long)XFS_BUF_COUNT(bp)),
1216 (void *)((unsigned long)XFS_BUF_BFLAGS(bp)),
1217 XFS_BUF_FSPRIVATE(bp, void *),
1218 XFS_BUF_FSPRIVATE2(bp, void *),
1219 (void *)(unsigned long)XFS_BUF_ISPINNED(bp),
1220 (void *)XFS_BUF_IODONE_FUNC(bp),
1221 (void *)((unsigned long)(XFS_BUF_VALUSEMA(bp))),
1222 (void *)bip->bli_item.li_desc,
1223 (void *)((unsigned long)bip->bli_item.li_flags));
1225 #endif /* XFS_BLI_TRACE */