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 #include "xfs_types.h"
38 #include "xfs_trans.h"
41 #include "xfs_dmapi.h"
42 #include "xfs_mount.h"
43 #include "xfs_buf_item.h"
44 #include "xfs_trans_priv.h"
45 #include "xfs_error.h"
48 kmem_zone_t
*xfs_buf_item_zone
;
50 #ifdef XFS_TRANS_DEBUG
52 * This function uses an alternate strategy for tracking the bytes
53 * that the user requests to be logged. This can then be used
54 * in conjunction with the bli_orig array in the buf log item to
55 * catch bugs in our callers' code.
57 * We also double check the bits set in xfs_buf_item_log using a
58 * simple algorithm to check that every byte is accounted for.
61 xfs_buf_item_log_debug(
62 xfs_buf_log_item_t
*bip
,
75 ASSERT(bip
->bli_logged
!= NULL
);
77 nbytes
= last
- first
+ 1;
78 bfset(bip
->bli_logged
, first
, nbytes
);
79 for (x
= 0; x
< nbytes
; x
++) {
80 chunk_num
= byte
>> XFS_BLI_SHIFT
;
81 word_num
= chunk_num
>> BIT_TO_WORD_SHIFT
;
82 bit_num
= chunk_num
& (NBWORD
- 1);
83 wordp
= &(bip
->bli_format
.blf_data_map
[word_num
]);
84 bit_set
= *wordp
& (1 << bit_num
);
91 * This function is called when we flush something into a buffer without
92 * logging it. This happens for things like inodes which are logged
93 * separately from the buffer.
96 xfs_buf_item_flush_log_debug(
101 xfs_buf_log_item_t
*bip
;
104 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
105 if ((bip
== NULL
) || (bip
->bli_item
.li_type
!= XFS_LI_BUF
)) {
109 ASSERT(bip
->bli_logged
!= NULL
);
110 nbytes
= last
- first
+ 1;
111 bfset(bip
->bli_logged
, first
, nbytes
);
115 * This function is called to verify that our caller's have logged
116 * all the bytes that they changed.
118 * It does this by comparing the original copy of the buffer stored in
119 * the buf log item's bli_orig array to the current copy of the buffer
120 * and ensuring that all bytes which miscompare are set in the bli_logged
121 * array of the buf log item.
124 xfs_buf_item_log_check(
125 xfs_buf_log_item_t
*bip
)
132 ASSERT(bip
->bli_orig
!= NULL
);
133 ASSERT(bip
->bli_logged
!= NULL
);
136 ASSERT(XFS_BUF_COUNT(bp
) > 0);
137 ASSERT(XFS_BUF_PTR(bp
) != NULL
);
138 orig
= bip
->bli_orig
;
139 buffer
= XFS_BUF_PTR(bp
);
140 for (x
= 0; x
< XFS_BUF_COUNT(bp
); x
++) {
141 if (orig
[x
] != buffer
[x
] && !btst(bip
->bli_logged
, x
))
143 "xfs_buf_item_log_check bip %x buffer %x orig %x index %d",
148 #define xfs_buf_item_log_debug(x,y,z)
149 #define xfs_buf_item_log_check(x)
152 STATIC
void xfs_buf_error_relse(xfs_buf_t
*bp
);
153 STATIC
void xfs_buf_do_callbacks(xfs_buf_t
*bp
, xfs_log_item_t
*lip
);
156 * This returns the number of log iovecs needed to log the
157 * given buf log item.
159 * It calculates this as 1 iovec for the buf log format structure
160 * and 1 for each stretch of non-contiguous chunks to be logged.
161 * Contiguous chunks are logged in a single iovec.
163 * If the XFS_BLI_STALE flag has been set, then log nothing.
167 xfs_buf_log_item_t
*bip
)
174 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
175 if (bip
->bli_flags
& XFS_BLI_STALE
) {
177 * The buffer is stale, so all we need to log
178 * is the buf log format structure with the
181 xfs_buf_item_trace("SIZE STALE", bip
);
182 ASSERT(bip
->bli_format
.blf_flags
& XFS_BLI_CANCEL
);
187 ASSERT(bip
->bli_flags
& XFS_BLI_LOGGED
);
189 last_bit
= xfs_next_bit(bip
->bli_format
.blf_data_map
,
190 bip
->bli_format
.blf_map_size
, 0);
191 ASSERT(last_bit
!= -1);
193 while (last_bit
!= -1) {
195 * This takes the bit number to start looking from and
196 * returns the next set bit from there. It returns -1
197 * if there are no more bits set or the start bit is
198 * beyond the end of the bitmap.
200 next_bit
= xfs_next_bit(bip
->bli_format
.blf_data_map
,
201 bip
->bli_format
.blf_map_size
,
204 * If we run out of bits, leave the loop,
205 * else if we find a new set of bits bump the number of vecs,
206 * else keep scanning the current set of bits.
208 if (next_bit
== -1) {
210 } else if (next_bit
!= last_bit
+ 1) {
213 } else if (xfs_buf_offset(bp
, next_bit
* XFS_BLI_CHUNK
) !=
214 (xfs_buf_offset(bp
, last_bit
* XFS_BLI_CHUNK
) +
223 xfs_buf_item_trace("SIZE NORM", bip
);
228 * This is called to fill in the vector of log iovecs for the
229 * given log buf item. It fills the first entry with a buf log
230 * format structure, and the rest point to contiguous chunks
235 xfs_buf_log_item_t
*bip
,
236 xfs_log_iovec_t
*log_vector
)
240 xfs_log_iovec_t
*vecp
;
248 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
249 ASSERT((bip
->bli_flags
& XFS_BLI_LOGGED
) ||
250 (bip
->bli_flags
& XFS_BLI_STALE
));
252 ASSERT(XFS_BUF_BP_ISMAPPED(bp
));
256 * The size of the base structure is the size of the
257 * declared structure plus the space for the extra words
258 * of the bitmap. We subtract one from the map size, because
259 * the first element of the bitmap is accounted for in the
260 * size of the base structure.
263 (uint
)(sizeof(xfs_buf_log_format_t
) +
264 ((bip
->bli_format
.blf_map_size
- 1) * sizeof(uint
)));
265 vecp
->i_addr
= (xfs_caddr_t
)&bip
->bli_format
;
266 vecp
->i_len
= base_size
;
267 XLOG_VEC_SET_TYPE(vecp
, XLOG_REG_TYPE_BFORMAT
);
271 if (bip
->bli_flags
& XFS_BLI_STALE
) {
273 * The buffer is stale, so all we need to log
274 * is the buf log format structure with the
277 xfs_buf_item_trace("FORMAT STALE", bip
);
278 ASSERT(bip
->bli_format
.blf_flags
& XFS_BLI_CANCEL
);
279 bip
->bli_format
.blf_size
= nvecs
;
284 * Fill in an iovec for each set of contiguous chunks.
286 first_bit
= xfs_next_bit(bip
->bli_format
.blf_data_map
,
287 bip
->bli_format
.blf_map_size
, 0);
288 ASSERT(first_bit
!= -1);
289 last_bit
= first_bit
;
293 * This takes the bit number to start looking from and
294 * returns the next set bit from there. It returns -1
295 * if there are no more bits set or the start bit is
296 * beyond the end of the bitmap.
298 next_bit
= xfs_next_bit(bip
->bli_format
.blf_data_map
,
299 bip
->bli_format
.blf_map_size
,
302 * If we run out of bits fill in the last iovec and get
304 * Else if we start a new set of bits then fill in the
305 * iovec for the series we were looking at and start
306 * counting the bits in the new one.
307 * Else we're still in the same set of bits so just
308 * keep counting and scanning.
310 if (next_bit
== -1) {
311 buffer_offset
= first_bit
* XFS_BLI_CHUNK
;
312 vecp
->i_addr
= xfs_buf_offset(bp
, buffer_offset
);
313 vecp
->i_len
= nbits
* XFS_BLI_CHUNK
;
314 XLOG_VEC_SET_TYPE(vecp
, XLOG_REG_TYPE_BCHUNK
);
317 } else if (next_bit
!= last_bit
+ 1) {
318 buffer_offset
= first_bit
* XFS_BLI_CHUNK
;
319 vecp
->i_addr
= xfs_buf_offset(bp
, buffer_offset
);
320 vecp
->i_len
= nbits
* XFS_BLI_CHUNK
;
321 XLOG_VEC_SET_TYPE(vecp
, XLOG_REG_TYPE_BCHUNK
);
324 first_bit
= next_bit
;
327 } else if (xfs_buf_offset(bp
, next_bit
<< XFS_BLI_SHIFT
) !=
328 (xfs_buf_offset(bp
, last_bit
<< XFS_BLI_SHIFT
) +
330 buffer_offset
= first_bit
* XFS_BLI_CHUNK
;
331 vecp
->i_addr
= xfs_buf_offset(bp
, buffer_offset
);
332 vecp
->i_len
= nbits
* XFS_BLI_CHUNK
;
333 XLOG_VEC_SET_TYPE(vecp
, XLOG_REG_TYPE_BCHUNK
);
334 /* You would think we need to bump the nvecs here too, but we do not
335 * this number is used by recovery, and it gets confused by the boundary
340 first_bit
= next_bit
;
348 bip
->bli_format
.blf_size
= nvecs
;
351 * Check to make sure everything is consistent.
353 xfs_buf_item_trace("FORMAT NORM", bip
);
354 xfs_buf_item_log_check(bip
);
358 * This is called to pin the buffer associated with the buf log
359 * item in memory so it cannot be written out. Simply call bpin()
360 * on the buffer to do this.
364 xfs_buf_log_item_t
*bip
)
369 ASSERT(XFS_BUF_ISBUSY(bp
));
370 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
371 ASSERT((bip
->bli_flags
& XFS_BLI_LOGGED
) ||
372 (bip
->bli_flags
& XFS_BLI_STALE
));
373 xfs_buf_item_trace("PIN", bip
);
374 xfs_buftrace("XFS_PIN", bp
);
380 * This is called to unpin the buffer associated with the buf log
381 * item which was previously pinned with a call to xfs_buf_item_pin().
382 * Just call bunpin() on the buffer to do this.
384 * Also drop the reference to the buf item for the current transaction.
385 * If the XFS_BLI_STALE flag is set and we are the last reference,
386 * then free up the buf log item and unlock the buffer.
390 xfs_buf_log_item_t
*bip
,
400 ASSERT(XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*) == bip
);
401 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
402 xfs_buf_item_trace("UNPIN", bip
);
403 xfs_buftrace("XFS_UNPIN", bp
);
405 freed
= atomic_dec_and_test(&bip
->bli_refcount
);
406 mp
= bip
->bli_item
.li_mountp
;
408 if (freed
&& stale
) {
409 ASSERT(bip
->bli_flags
& XFS_BLI_STALE
);
410 ASSERT(XFS_BUF_VALUSEMA(bp
) <= 0);
411 ASSERT(!(XFS_BUF_ISDELAYWRITE(bp
)));
412 ASSERT(XFS_BUF_ISSTALE(bp
));
413 ASSERT(bip
->bli_format
.blf_flags
& XFS_BLI_CANCEL
);
414 xfs_buf_item_trace("UNPIN STALE", bip
);
415 xfs_buftrace("XFS_UNPIN STALE", bp
);
417 * If we get called here because of an IO error, we may
418 * or may not have the item on the AIL. xfs_trans_delete_ail()
419 * will take care of that situation.
420 * xfs_trans_delete_ail() drops the AIL lock.
422 if (bip
->bli_flags
& XFS_BLI_STALE_INODE
) {
423 xfs_buf_do_callbacks(bp
, (xfs_log_item_t
*)bip
);
424 XFS_BUF_SET_FSPRIVATE(bp
, NULL
);
425 XFS_BUF_CLR_IODONE_FUNC(bp
);
428 xfs_trans_delete_ail(mp
, (xfs_log_item_t
*)bip
, s
);
429 xfs_buf_item_relse(bp
);
430 ASSERT(XFS_BUF_FSPRIVATE(bp
, void *) == NULL
);
437 * this is called from uncommit in the forced-shutdown path.
438 * we need to check to see if the reference count on the log item
439 * is going to drop to zero. If so, unpin will free the log item
440 * so we need to free the item's descriptor (that points to the item)
441 * in the transaction.
444 xfs_buf_item_unpin_remove(
445 xfs_buf_log_item_t
*bip
,
449 xfs_log_item_desc_t
*lidp
;
454 * will xfs_buf_item_unpin() call xfs_buf_item_relse()?
456 if ((atomic_read(&bip
->bli_refcount
) == 1) &&
457 (bip
->bli_flags
& XFS_BLI_STALE
)) {
458 ASSERT(XFS_BUF_VALUSEMA(bip
->bli_buf
) <= 0);
459 xfs_buf_item_trace("UNPIN REMOVE", bip
);
460 xfs_buftrace("XFS_UNPIN_REMOVE", bp
);
462 * yes -- clear the xaction descriptor in-use flag
463 * and free the chunk if required. We can safely
464 * do some work here and then call buf_item_unpin
465 * to do the rest because if the if is true, then
466 * we are holding the buffer locked so no one else
467 * will be able to bump up the refcount.
469 lidp
= xfs_trans_find_item(tp
, (xfs_log_item_t
*) bip
);
470 stale
= lidp
->lid_flags
& XFS_LID_BUF_STALE
;
471 xfs_trans_free_item(tp
, lidp
);
473 * Since the transaction no longer refers to the buffer,
474 * the buffer should no longer refer to the transaction.
476 XFS_BUF_SET_FSPRIVATE2(bp
, NULL
);
479 xfs_buf_item_unpin(bip
, stale
);
485 * This is called to attempt to lock the buffer associated with this
486 * buf log item. Don't sleep on the buffer lock. If we can't get
487 * the lock right away, return 0. If we can get the lock, pull the
488 * buffer from the free list, mark it busy, and return 1.
491 xfs_buf_item_trylock(
492 xfs_buf_log_item_t
*bip
)
498 if (XFS_BUF_ISPINNED(bp
)) {
499 return XFS_ITEM_PINNED
;
502 if (!XFS_BUF_CPSEMA(bp
)) {
503 return XFS_ITEM_LOCKED
;
507 * Remove the buffer from the free list. Only do this
508 * if it's on the free list. Private buffers like the
509 * superblock buffer are not.
513 ASSERT(!(bip
->bli_flags
& XFS_BLI_STALE
));
514 xfs_buf_item_trace("TRYLOCK SUCCESS", bip
);
515 return XFS_ITEM_SUCCESS
;
519 * Release the buffer associated with the buf log item.
520 * If there is no dirty logged data associated with the
521 * buffer recorded in the buf log item, then free the
522 * buf log item and remove the reference to it in the
525 * This call ignores the recursion count. It is only called
526 * when the buffer should REALLY be unlocked, regardless
527 * of the recursion count.
529 * If the XFS_BLI_HOLD flag is set in the buf log item, then
530 * free the log item if necessary but do not unlock the buffer.
531 * This is for support of xfs_trans_bhold(). Make sure the
532 * XFS_BLI_HOLD field is cleared if we don't free the item.
536 xfs_buf_log_item_t
*bip
)
543 xfs_buftrace("XFS_UNLOCK", bp
);
546 * Clear the buffer's association with this transaction.
548 XFS_BUF_SET_FSPRIVATE2(bp
, NULL
);
551 * If this is a transaction abort, don't return early.
552 * Instead, allow the brelse to happen.
553 * Normally it would be done for stale (cancelled) buffers
554 * at unpin time, but we'll never go through the pin/unpin
555 * cycle if we abort inside commit.
557 aborted
= (bip
->bli_item
.li_flags
& XFS_LI_ABORTED
) != 0;
560 * If the buf item is marked stale, then don't do anything.
561 * We'll unlock the buffer and free the buf item when the
562 * buffer is unpinned for the last time.
564 if (bip
->bli_flags
& XFS_BLI_STALE
) {
565 bip
->bli_flags
&= ~XFS_BLI_LOGGED
;
566 xfs_buf_item_trace("UNLOCK STALE", bip
);
567 ASSERT(bip
->bli_format
.blf_flags
& XFS_BLI_CANCEL
);
573 * Drop the transaction's reference to the log item if
574 * it was not logged as part of the transaction. Otherwise
575 * we'll drop the reference in xfs_buf_item_unpin() when
576 * the transaction is really through with the buffer.
578 if (!(bip
->bli_flags
& XFS_BLI_LOGGED
)) {
579 atomic_dec(&bip
->bli_refcount
);
582 * Clear the logged flag since this is per
585 bip
->bli_flags
&= ~XFS_BLI_LOGGED
;
589 * Before possibly freeing the buf item, determine if we should
590 * release the buffer at the end of this routine.
592 hold
= bip
->bli_flags
& XFS_BLI_HOLD
;
593 xfs_buf_item_trace("UNLOCK", bip
);
596 * If the buf item isn't tracking any data, free it.
597 * Otherwise, if XFS_BLI_HOLD is set clear it.
599 if (xfs_count_bits(bip
->bli_format
.blf_data_map
,
600 bip
->bli_format
.blf_map_size
, 0) == 0) {
601 xfs_buf_item_relse(bp
);
603 bip
->bli_flags
&= ~XFS_BLI_HOLD
;
607 * Release the buffer if XFS_BLI_HOLD was not set.
615 * This is called to find out where the oldest active copy of the
616 * buf log item in the on disk log resides now that the last log
617 * write of it completed at the given lsn.
618 * We always re-log all the dirty data in a buffer, so usually the
619 * latest copy in the on disk log is the only one that matters. For
620 * those cases we simply return the given lsn.
622 * The one exception to this is for buffers full of newly allocated
623 * inodes. These buffers are only relogged with the XFS_BLI_INODE_BUF
624 * flag set, indicating that only the di_next_unlinked fields from the
625 * inodes in the buffers will be replayed during recovery. If the
626 * original newly allocated inode images have not yet been flushed
627 * when the buffer is so relogged, then we need to make sure that we
628 * keep the old images in the 'active' portion of the log. We do this
629 * by returning the original lsn of that transaction here rather than
633 xfs_buf_item_committed(
634 xfs_buf_log_item_t
*bip
,
637 xfs_buf_item_trace("COMMITTED", bip
);
638 if ((bip
->bli_flags
& XFS_BLI_INODE_ALLOC_BUF
) &&
639 (bip
->bli_item
.li_lsn
!= 0)) {
640 return bip
->bli_item
.li_lsn
;
646 * This is called when the transaction holding the buffer is aborted.
647 * Just behave as if the transaction had been cancelled. If we're shutting down
648 * and have aborted this transaction, we'll trap this buffer when it tries to
653 xfs_buf_log_item_t
*bip
)
658 xfs_buftrace("XFS_ABORT", bp
);
659 XFS_BUF_SUPER_STALE(bp
);
660 xfs_buf_item_unlock(bip
);
665 * This is called to asynchronously write the buffer associated with this
666 * buf log item out to disk. The buffer will already have been locked by
667 * a successful call to xfs_buf_item_trylock(). If the buffer still has
668 * B_DELWRI set, then get it going out to disk with a call to bawrite().
669 * If not, then just release the buffer.
673 xfs_buf_log_item_t
*bip
)
677 ASSERT(!(bip
->bli_flags
& XFS_BLI_STALE
));
678 xfs_buf_item_trace("PUSH", bip
);
682 if (XFS_BUF_ISDELAYWRITE(bp
)) {
683 xfs_bawrite(bip
->bli_item
.li_mountp
, bp
);
691 xfs_buf_item_committing(xfs_buf_log_item_t
*bip
, xfs_lsn_t commit_lsn
)
696 * This is the ops vector shared by all buf log items.
698 STATIC
struct xfs_item_ops xfs_buf_item_ops
= {
699 .iop_size
= (uint(*)(xfs_log_item_t
*))xfs_buf_item_size
,
700 .iop_format
= (void(*)(xfs_log_item_t
*, xfs_log_iovec_t
*))
702 .iop_pin
= (void(*)(xfs_log_item_t
*))xfs_buf_item_pin
,
703 .iop_unpin
= (void(*)(xfs_log_item_t
*, int))xfs_buf_item_unpin
,
704 .iop_unpin_remove
= (void(*)(xfs_log_item_t
*, xfs_trans_t
*))
705 xfs_buf_item_unpin_remove
,
706 .iop_trylock
= (uint(*)(xfs_log_item_t
*))xfs_buf_item_trylock
,
707 .iop_unlock
= (void(*)(xfs_log_item_t
*))xfs_buf_item_unlock
,
708 .iop_committed
= (xfs_lsn_t(*)(xfs_log_item_t
*, xfs_lsn_t
))
709 xfs_buf_item_committed
,
710 .iop_push
= (void(*)(xfs_log_item_t
*))xfs_buf_item_push
,
711 .iop_abort
= (void(*)(xfs_log_item_t
*))xfs_buf_item_abort
,
713 .iop_committing
= (void(*)(xfs_log_item_t
*, xfs_lsn_t
))
714 xfs_buf_item_committing
719 * Allocate a new buf log item to go with the given buffer.
720 * Set the buffer's b_fsprivate field to point to the new
721 * buf log item. If there are other item's attached to the
722 * buffer (see xfs_buf_attach_iodone() below), then put the
723 * buf log item at the front.
731 xfs_buf_log_item_t
*bip
;
736 * Check to see if there is already a buf log item for
737 * this buffer. If there is, it is guaranteed to be
738 * the first. If we do already have one, there is
739 * nothing to do here so return.
741 if (XFS_BUF_FSPRIVATE3(bp
, xfs_mount_t
*) != mp
)
742 XFS_BUF_SET_FSPRIVATE3(bp
, mp
);
743 XFS_BUF_SET_BDSTRAT_FUNC(bp
, xfs_bdstrat_cb
);
744 if (XFS_BUF_FSPRIVATE(bp
, void *) != NULL
) {
745 lip
= XFS_BUF_FSPRIVATE(bp
, xfs_log_item_t
*);
746 if (lip
->li_type
== XFS_LI_BUF
) {
752 * chunks is the number of XFS_BLI_CHUNK size pieces
753 * the buffer can be divided into. Make sure not to
754 * truncate any pieces. map_size is the size of the
755 * bitmap needed to describe the chunks of the buffer.
757 chunks
= (int)((XFS_BUF_COUNT(bp
) + (XFS_BLI_CHUNK
- 1)) >> XFS_BLI_SHIFT
);
758 map_size
= (int)((chunks
+ NBWORD
) >> BIT_TO_WORD_SHIFT
);
760 bip
= (xfs_buf_log_item_t
*)kmem_zone_zalloc(xfs_buf_item_zone
,
762 bip
->bli_item
.li_type
= XFS_LI_BUF
;
763 bip
->bli_item
.li_ops
= &xfs_buf_item_ops
;
764 bip
->bli_item
.li_mountp
= mp
;
766 bip
->bli_format
.blf_type
= XFS_LI_BUF
;
767 bip
->bli_format
.blf_blkno
= (__int64_t
)XFS_BUF_ADDR(bp
);
768 bip
->bli_format
.blf_len
= (ushort
)BTOBB(XFS_BUF_COUNT(bp
));
769 bip
->bli_format
.blf_map_size
= map_size
;
771 bip
->bli_trace
= ktrace_alloc(XFS_BLI_TRACE_SIZE
, KM_SLEEP
);
774 #ifdef XFS_TRANS_DEBUG
776 * Allocate the arrays for tracking what needs to be logged
777 * and what our callers request to be logged. bli_orig
778 * holds a copy of the original, clean buffer for comparison
779 * against, and bli_logged keeps a 1 bit flag per byte in
780 * the buffer to indicate which bytes the callers have asked
783 bip
->bli_orig
= (char *)kmem_alloc(XFS_BUF_COUNT(bp
), KM_SLEEP
);
784 memcpy(bip
->bli_orig
, XFS_BUF_PTR(bp
), XFS_BUF_COUNT(bp
));
785 bip
->bli_logged
= (char *)kmem_zalloc(XFS_BUF_COUNT(bp
) / NBBY
, KM_SLEEP
);
789 * Put the buf item into the list of items attached to the
790 * buffer at the front.
792 if (XFS_BUF_FSPRIVATE(bp
, void *) != NULL
) {
793 bip
->bli_item
.li_bio_list
=
794 XFS_BUF_FSPRIVATE(bp
, xfs_log_item_t
*);
796 XFS_BUF_SET_FSPRIVATE(bp
, bip
);
801 * Mark bytes first through last inclusive as dirty in the buf
806 xfs_buf_log_item_t
*bip
,
821 * Mark the item as having some dirty data for
822 * quick reference in xfs_buf_item_dirty.
824 bip
->bli_flags
|= XFS_BLI_DIRTY
;
827 * Convert byte offsets to bit numbers.
829 first_bit
= first
>> XFS_BLI_SHIFT
;
830 last_bit
= last
>> XFS_BLI_SHIFT
;
833 * Calculate the total number of bits to be set.
835 bits_to_set
= last_bit
- first_bit
+ 1;
838 * Get a pointer to the first word in the bitmap
841 word_num
= first_bit
>> BIT_TO_WORD_SHIFT
;
842 wordp
= &(bip
->bli_format
.blf_data_map
[word_num
]);
845 * Calculate the starting bit in the first word.
847 bit
= first_bit
& (uint
)(NBWORD
- 1);
850 * First set any bits in the first word of our range.
851 * If it starts at bit 0 of the word, it will be
852 * set below rather than here. That is what the variable
853 * bit tells us. The variable bits_set tracks the number
854 * of bits that have been set so far. End_bit is the number
855 * of the last bit to be set in this word plus one.
858 end_bit
= MIN(bit
+ bits_to_set
, (uint
)NBWORD
);
859 mask
= ((1 << (end_bit
- bit
)) - 1) << bit
;
862 bits_set
= end_bit
- bit
;
868 * Now set bits a whole word at a time that are between
869 * first_bit and last_bit.
871 while ((bits_to_set
- bits_set
) >= NBWORD
) {
872 *wordp
|= 0xffffffff;
878 * Finally, set any bits left to be set in one last partial word.
880 end_bit
= bits_to_set
- bits_set
;
882 mask
= (1 << end_bit
) - 1;
886 xfs_buf_item_log_debug(bip
, first
, last
);
891 * Return 1 if the buffer has some data that has been logged (at any
892 * point, not just the current transaction) and 0 if not.
896 xfs_buf_log_item_t
*bip
)
898 return (bip
->bli_flags
& XFS_BLI_DIRTY
);
902 * This is called when the buf log item is no longer needed. It should
903 * free the buf log item associated with the given buffer and clear
904 * the buffer's pointer to the buf log item. If there are no more
905 * items in the list, clear the b_iodone field of the buffer (see
906 * xfs_buf_attach_iodone() below).
912 xfs_buf_log_item_t
*bip
;
914 xfs_buftrace("XFS_RELSE", bp
);
915 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
916 XFS_BUF_SET_FSPRIVATE(bp
, bip
->bli_item
.li_bio_list
);
917 if ((XFS_BUF_FSPRIVATE(bp
, void *) == NULL
) &&
918 (XFS_BUF_IODONE_FUNC(bp
) != NULL
)) {
919 ASSERT((XFS_BUF_ISUNINITIAL(bp
)) == 0);
920 XFS_BUF_CLR_IODONE_FUNC(bp
);
923 #ifdef XFS_TRANS_DEBUG
924 kmem_free(bip
->bli_orig
, XFS_BUF_COUNT(bp
));
925 bip
->bli_orig
= NULL
;
926 kmem_free(bip
->bli_logged
, XFS_BUF_COUNT(bp
) / NBBY
);
927 bip
->bli_logged
= NULL
;
928 #endif /* XFS_TRANS_DEBUG */
931 ktrace_free(bip
->bli_trace
);
933 kmem_zone_free(xfs_buf_item_zone
, bip
);
938 * Add the given log item with its callback to the list of callbacks
939 * to be called when the buffer's I/O completes. If it is not set
940 * already, set the buffer's b_iodone() routine to be
941 * xfs_buf_iodone_callbacks() and link the log item into the list of
942 * items rooted at b_fsprivate. Items are always added as the second
943 * entry in the list if there is a first, because the buf item code
944 * assumes that the buf log item is first.
947 xfs_buf_attach_iodone(
949 void (*cb
)(xfs_buf_t
*, xfs_log_item_t
*),
952 xfs_log_item_t
*head_lip
;
954 ASSERT(XFS_BUF_ISBUSY(bp
));
955 ASSERT(XFS_BUF_VALUSEMA(bp
) <= 0);
958 if (XFS_BUF_FSPRIVATE(bp
, void *) != NULL
) {
959 head_lip
= XFS_BUF_FSPRIVATE(bp
, xfs_log_item_t
*);
960 lip
->li_bio_list
= head_lip
->li_bio_list
;
961 head_lip
->li_bio_list
= lip
;
963 XFS_BUF_SET_FSPRIVATE(bp
, lip
);
966 ASSERT((XFS_BUF_IODONE_FUNC(bp
) == xfs_buf_iodone_callbacks
) ||
967 (XFS_BUF_IODONE_FUNC(bp
) == NULL
));
968 XFS_BUF_SET_IODONE_FUNC(bp
, xfs_buf_iodone_callbacks
);
972 xfs_buf_do_callbacks(
976 xfs_log_item_t
*nlip
;
978 while (lip
!= NULL
) {
979 nlip
= lip
->li_bio_list
;
980 ASSERT(lip
->li_cb
!= NULL
);
982 * Clear the next pointer so we don't have any
983 * confusion if the item is added to another buf.
984 * Don't touch the log item after calling its
985 * callback, because it could have freed itself.
987 lip
->li_bio_list
= NULL
;
994 * This is the iodone() function for buffers which have had callbacks
995 * attached to them by xfs_buf_attach_iodone(). It should remove each
996 * log item from the buffer's list and call the callback of each in turn.
997 * When done, the buffer's fsprivate field is set to NULL and the buffer
998 * is unlocked with a call to iodone().
1001 xfs_buf_iodone_callbacks(
1004 xfs_log_item_t
*lip
;
1005 static ulong lasttime
;
1006 static xfs_buftarg_t
*lasttarg
;
1009 ASSERT(XFS_BUF_FSPRIVATE(bp
, void *) != NULL
);
1010 lip
= XFS_BUF_FSPRIVATE(bp
, xfs_log_item_t
*);
1012 if (XFS_BUF_GETERROR(bp
) != 0) {
1014 * If we've already decided to shutdown the filesystem
1015 * because of IO errors, there's no point in giving this
1018 mp
= lip
->li_mountp
;
1019 if (XFS_FORCED_SHUTDOWN(mp
)) {
1020 ASSERT(XFS_BUF_TARGET(bp
) == mp
->m_ddev_targp
);
1021 XFS_BUF_SUPER_STALE(bp
);
1022 xfs_buftrace("BUF_IODONE_CB", bp
);
1023 xfs_buf_do_callbacks(bp
, lip
);
1024 XFS_BUF_SET_FSPRIVATE(bp
, NULL
);
1025 XFS_BUF_CLR_IODONE_FUNC(bp
);
1028 * XFS_SHUT flag gets set when we go thru the
1029 * entire buffer cache and deliberately start
1030 * throwing away delayed write buffers.
1031 * Since there's no biowait done on those,
1032 * we should just brelse them.
1034 if (XFS_BUF_ISSHUT(bp
)) {
1044 if ((XFS_BUF_TARGET(bp
) != lasttarg
) ||
1045 (time_after(jiffies
, (lasttime
+ 5*HZ
)))) {
1047 prdev("XFS write error in file system meta-data "
1048 "block 0x%llx in %s",
1050 (__uint64_t
)XFS_BUF_ADDR(bp
), mp
->m_fsname
);
1052 lasttarg
= XFS_BUF_TARGET(bp
);
1054 if (XFS_BUF_ISASYNC(bp
)) {
1056 * If the write was asynchronous then noone will be
1057 * looking for the error. Clear the error state
1058 * and write the buffer out again delayed write.
1060 * XXXsup This is OK, so long as we catch these
1061 * before we start the umount; we don't want these
1062 * DELWRI metadata bufs to be hanging around.
1064 XFS_BUF_ERROR(bp
,0); /* errno of 0 unsets the flag */
1066 if (!(XFS_BUF_ISSTALE(bp
))) {
1067 XFS_BUF_DELAYWRITE(bp
);
1069 XFS_BUF_SET_START(bp
);
1071 ASSERT(XFS_BUF_IODONE_FUNC(bp
));
1072 xfs_buftrace("BUF_IODONE ASYNC", bp
);
1076 * If the write of the buffer was not asynchronous,
1077 * then we want to make sure to return the error
1078 * to the caller of bwrite(). Because of this we
1079 * cannot clear the B_ERROR state at this point.
1080 * Instead we install a callback function that
1081 * will be called when the buffer is released, and
1082 * that routine will clear the error state and
1083 * set the buffer to be written out again after
1086 /* We actually overwrite the existing b-relse
1087 function at times, but we're gonna be shutting down
1089 XFS_BUF_SET_BRELSE_FUNC(bp
,xfs_buf_error_relse
);
1091 XFS_BUF_V_IODONESEMA(bp
);
1095 #ifdef XFSERRORDEBUG
1096 xfs_buftrace("XFS BUFCB NOERR", bp
);
1098 xfs_buf_do_callbacks(bp
, lip
);
1099 XFS_BUF_SET_FSPRIVATE(bp
, NULL
);
1100 XFS_BUF_CLR_IODONE_FUNC(bp
);
1105 * This is a callback routine attached to a buffer which gets an error
1106 * when being written out synchronously.
1109 xfs_buf_error_relse(
1112 xfs_log_item_t
*lip
;
1115 lip
= XFS_BUF_FSPRIVATE(bp
, xfs_log_item_t
*);
1116 mp
= (xfs_mount_t
*)lip
->li_mountp
;
1117 ASSERT(XFS_BUF_TARGET(bp
) == mp
->m_ddev_targp
);
1121 XFS_BUF_UNDELAYWRITE(bp
);
1122 XFS_BUF_ERROR(bp
,0);
1123 xfs_buftrace("BUF_ERROR_RELSE", bp
);
1124 if (! XFS_FORCED_SHUTDOWN(mp
))
1125 xfs_force_shutdown(mp
, XFS_METADATA_IO_ERROR
);
1127 * We have to unpin the pinned buffers so do the
1130 xfs_buf_do_callbacks(bp
, lip
);
1131 XFS_BUF_SET_FSPRIVATE(bp
, NULL
);
1132 XFS_BUF_CLR_IODONE_FUNC(bp
);
1133 XFS_BUF_SET_BRELSE_FUNC(bp
,NULL
);
1139 * This is the iodone() function for buffers which have been
1140 * logged. It is called when they are eventually flushed out.
1141 * It should remove the buf item from the AIL, and free the buf item.
1142 * It is called by xfs_buf_iodone_callbacks() above which will take
1143 * care of cleaning up the buffer itself.
1149 xfs_buf_log_item_t
*bip
)
1151 struct xfs_mount
*mp
;
1154 ASSERT(bip
->bli_buf
== bp
);
1156 mp
= bip
->bli_item
.li_mountp
;
1159 * If we are forcibly shutting down, this may well be
1160 * off the AIL already. That's because we simulate the
1161 * log-committed callbacks to unpin these buffers. Or we may never
1162 * have put this item on AIL because of the transaction was
1163 * aborted forcibly. xfs_trans_delete_ail() takes care of these.
1165 * Either way, AIL is useless if we're forcing a shutdown.
1169 * xfs_trans_delete_ail() drops the AIL lock.
1171 xfs_trans_delete_ail(mp
, (xfs_log_item_t
*)bip
, s
);
1173 #ifdef XFS_TRANS_DEBUG
1174 kmem_free(bip
->bli_orig
, XFS_BUF_COUNT(bp
));
1175 bip
->bli_orig
= NULL
;
1176 kmem_free(bip
->bli_logged
, XFS_BUF_COUNT(bp
) / NBBY
);
1177 bip
->bli_logged
= NULL
;
1178 #endif /* XFS_TRANS_DEBUG */
1180 #ifdef XFS_BLI_TRACE
1181 ktrace_free(bip
->bli_trace
);
1183 kmem_zone_free(xfs_buf_item_zone
, bip
);
1186 #if defined(XFS_BLI_TRACE)
1190 xfs_buf_log_item_t
*bip
)
1193 ASSERT(bip
->bli_trace
!= NULL
);
1196 ktrace_enter(bip
->bli_trace
,
1198 (void *)bip
->bli_buf
,
1199 (void *)((unsigned long)bip
->bli_flags
),
1200 (void *)((unsigned long)bip
->bli_recur
),
1201 (void *)((unsigned long)atomic_read(&bip
->bli_refcount
)),
1202 (void *)((unsigned long)
1203 (0xFFFFFFFF & XFS_BUF_ADDR(bp
) >> 32)),
1204 (void *)((unsigned long)(0xFFFFFFFF & XFS_BUF_ADDR(bp
))),
1205 (void *)((unsigned long)XFS_BUF_COUNT(bp
)),
1206 (void *)((unsigned long)XFS_BUF_BFLAGS(bp
)),
1207 XFS_BUF_FSPRIVATE(bp
, void *),
1208 XFS_BUF_FSPRIVATE2(bp
, void *),
1209 (void *)(unsigned long)XFS_BUF_ISPINNED(bp
),
1210 (void *)XFS_BUF_IODONE_FUNC(bp
),
1211 (void *)((unsigned long)(XFS_BUF_VALUSEMA(bp
))),
1212 (void *)bip
->bli_item
.li_desc
,
1213 (void *)((unsigned long)bip
->bli_item
.li_flags
));
1215 #endif /* XFS_BLI_TRACE */