2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_buf_item.h"
38 #include "xfs_trans_priv.h"
39 #include "xfs_error.h"
41 #include "xfs_trace.h"
44 STATIC xfs_buf_t
*xfs_trans_buf_item_match(xfs_trans_t
*, xfs_buftarg_t
*,
46 STATIC xfs_buf_t
*xfs_trans_buf_item_match_all(xfs_trans_t
*, xfs_buftarg_t
*,
51 * Get and lock the buffer for the caller if it is not already
52 * locked within the given transaction. If it is already locked
53 * within the transaction, just increment its lock recursion count
54 * and return a pointer to it.
56 * Use the fast path function xfs_trans_buf_item_match() or the buffer
57 * cache routine incore_match() to find the buffer
58 * if it is already owned by this transaction.
60 * If we don't already own the buffer, use get_buf() to get it.
61 * If it doesn't yet have an associated xfs_buf_log_item structure,
62 * then allocate one and add the item to this transaction.
64 * If the transaction pointer is NULL, make this just a normal
68 xfs_trans_get_buf(xfs_trans_t
*tp
,
69 xfs_buftarg_t
*target_dev
,
75 xfs_buf_log_item_t
*bip
;
78 flags
= XBF_LOCK
| XBF_MAPPED
;
81 * Default to a normal get_buf() call if the tp is NULL.
84 return xfs_buf_get(target_dev
, blkno
, len
,
85 flags
| XBF_DONT_BLOCK
);
88 * If we find the buffer in the cache with this transaction
89 * pointer in its b_fsprivate2 field, then we know we already
90 * have it locked. In this case we just increment the lock
91 * recursion count and return the buffer to the caller.
93 if (tp
->t_items
.lic_next
== NULL
) {
94 bp
= xfs_trans_buf_item_match(tp
, target_dev
, blkno
, len
);
96 bp
= xfs_trans_buf_item_match_all(tp
, target_dev
, blkno
, len
);
99 ASSERT(XFS_BUF_VALUSEMA(bp
) <= 0);
100 if (XFS_FORCED_SHUTDOWN(tp
->t_mountp
))
101 XFS_BUF_SUPER_STALE(bp
);
104 * If the buffer is stale then it was binval'ed
105 * since last read. This doesn't matter since the
106 * caller isn't allowed to use the data anyway.
108 else if (XFS_BUF_ISSTALE(bp
))
109 ASSERT(!XFS_BUF_ISDELAYWRITE(bp
));
111 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
112 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
114 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
116 trace_xfs_trans_get_buf_recur(bip
);
121 * We always specify the XBF_DONT_BLOCK flag within a transaction
122 * so that get_buf does not try to push out a delayed write buffer
123 * which might cause another transaction to take place (if the
124 * buffer was delayed alloc). Such recursive transactions can
125 * easily deadlock with our current transaction as well as cause
126 * us to run out of stack space.
128 bp
= xfs_buf_get(target_dev
, blkno
, len
, flags
| XBF_DONT_BLOCK
);
133 ASSERT(!XFS_BUF_GETERROR(bp
));
136 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
137 * it doesn't have one yet, then allocate one and initialize it.
138 * The checks to see if one is there are in xfs_buf_item_init().
140 xfs_buf_item_init(bp
, tp
->t_mountp
);
143 * Set the recursion count for the buffer within this transaction
146 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
147 ASSERT(!(bip
->bli_flags
& XFS_BLI_STALE
));
148 ASSERT(!(bip
->bli_format
.blf_flags
& XFS_BLI_CANCEL
));
149 ASSERT(!(bip
->bli_flags
& XFS_BLI_LOGGED
));
153 * Take a reference for this transaction on the buf item.
155 atomic_inc(&bip
->bli_refcount
);
158 * Get a log_item_desc to point at the new item.
160 (void) xfs_trans_add_item(tp
, (xfs_log_item_t
*)bip
);
163 * Initialize b_fsprivate2 so we can find it with incore_match()
166 XFS_BUF_SET_FSPRIVATE2(bp
, tp
);
168 trace_xfs_trans_get_buf(bip
);
173 * Get and lock the superblock buffer of this file system for the
176 * We don't need to use incore_match() here, because the superblock
177 * buffer is a private buffer which we keep a pointer to in the
181 xfs_trans_getsb(xfs_trans_t
*tp
,
182 struct xfs_mount
*mp
,
186 xfs_buf_log_item_t
*bip
;
189 * Default to just trying to lock the superblock buffer
193 return (xfs_getsb(mp
, flags
));
197 * If the superblock buffer already has this transaction
198 * pointer in its b_fsprivate2 field, then we know we already
199 * have it locked. In this case we just increment the lock
200 * recursion count and return the buffer to the caller.
203 if (XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
) {
204 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
206 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
208 trace_xfs_trans_getsb_recur(bip
);
212 bp
= xfs_getsb(mp
, flags
);
218 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
219 * it doesn't have one yet, then allocate one and initialize it.
220 * The checks to see if one is there are in xfs_buf_item_init().
222 xfs_buf_item_init(bp
, mp
);
225 * Set the recursion count for the buffer within this transaction
228 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
229 ASSERT(!(bip
->bli_flags
& XFS_BLI_STALE
));
230 ASSERT(!(bip
->bli_format
.blf_flags
& XFS_BLI_CANCEL
));
231 ASSERT(!(bip
->bli_flags
& XFS_BLI_LOGGED
));
235 * Take a reference for this transaction on the buf item.
237 atomic_inc(&bip
->bli_refcount
);
240 * Get a log_item_desc to point at the new item.
242 (void) xfs_trans_add_item(tp
, (xfs_log_item_t
*)bip
);
245 * Initialize b_fsprivate2 so we can find it with incore_match()
248 XFS_BUF_SET_FSPRIVATE2(bp
, tp
);
250 trace_xfs_trans_getsb(bip
);
255 xfs_buftarg_t
*xfs_error_target
;
258 int xfs_error_mod
= 33;
262 * Get and lock the buffer for the caller if it is not already
263 * locked within the given transaction. If it has not yet been
264 * read in, read it from disk. If it is already locked
265 * within the transaction and already read in, just increment its
266 * lock recursion count and return a pointer to it.
268 * Use the fast path function xfs_trans_buf_item_match() or the buffer
269 * cache routine incore_match() to find the buffer
270 * if it is already owned by this transaction.
272 * If we don't already own the buffer, use read_buf() to get it.
273 * If it doesn't yet have an associated xfs_buf_log_item structure,
274 * then allocate one and add the item to this transaction.
276 * If the transaction pointer is NULL, make this just a normal
283 xfs_buftarg_t
*target
,
290 xfs_buf_log_item_t
*bip
;
294 flags
= XBF_LOCK
| XBF_MAPPED
;
297 * Default to a normal get_buf() call if the tp is NULL.
300 bp
= xfs_buf_read(target
, blkno
, len
, flags
| XBF_DONT_BLOCK
);
302 return (flags
& XBF_TRYLOCK
) ?
303 EAGAIN
: XFS_ERROR(ENOMEM
);
305 if (XFS_BUF_GETERROR(bp
) != 0) {
306 xfs_ioerror_alert("xfs_trans_read_buf", mp
,
308 error
= XFS_BUF_GETERROR(bp
);
314 if (xfs_error_target
== target
) {
315 if (((xfs_req_num
++) % xfs_error_mod
) == 0) {
317 cmn_err(CE_DEBUG
, "Returning error!\n");
318 return XFS_ERROR(EIO
);
323 if (XFS_FORCED_SHUTDOWN(mp
))
330 * If we find the buffer in the cache with this transaction
331 * pointer in its b_fsprivate2 field, then we know we already
332 * have it locked. If it is already read in we just increment
333 * the lock recursion count and return the buffer to the caller.
334 * If the buffer is not yet read in, then we read it in, increment
335 * the lock recursion count, and return it to the caller.
337 if (tp
->t_items
.lic_next
== NULL
) {
338 bp
= xfs_trans_buf_item_match(tp
, target
, blkno
, len
);
340 bp
= xfs_trans_buf_item_match_all(tp
, target
, blkno
, len
);
343 ASSERT(XFS_BUF_VALUSEMA(bp
) <= 0);
344 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
345 ASSERT(XFS_BUF_FSPRIVATE(bp
, void *) != NULL
);
346 ASSERT((XFS_BUF_ISERROR(bp
)) == 0);
347 if (!(XFS_BUF_ISDONE(bp
))) {
348 trace_xfs_trans_read_buf_io(bp
, _RET_IP_
);
349 ASSERT(!XFS_BUF_ISASYNC(bp
));
351 xfsbdstrat(tp
->t_mountp
, bp
);
352 error
= xfs_iowait(bp
);
354 xfs_ioerror_alert("xfs_trans_read_buf", mp
,
358 * We can gracefully recover from most read
359 * errors. Ones we can't are those that happen
360 * after the transaction's already dirty.
362 if (tp
->t_flags
& XFS_TRANS_DIRTY
)
363 xfs_force_shutdown(tp
->t_mountp
,
364 SHUTDOWN_META_IO_ERROR
);
369 * We never locked this buf ourselves, so we shouldn't
370 * brelse it either. Just get out.
372 if (XFS_FORCED_SHUTDOWN(mp
)) {
373 trace_xfs_trans_read_buf_shut(bp
, _RET_IP_
);
375 return XFS_ERROR(EIO
);
379 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
382 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
383 trace_xfs_trans_read_buf_recur(bip
);
389 * We always specify the XBF_DONT_BLOCK flag within a transaction
390 * so that get_buf does not try to push out a delayed write buffer
391 * which might cause another transaction to take place (if the
392 * buffer was delayed alloc). Such recursive transactions can
393 * easily deadlock with our current transaction as well as cause
394 * us to run out of stack space.
396 bp
= xfs_buf_read(target
, blkno
, len
, flags
| XBF_DONT_BLOCK
);
401 if (XFS_BUF_GETERROR(bp
) != 0) {
402 XFS_BUF_SUPER_STALE(bp
);
403 error
= XFS_BUF_GETERROR(bp
);
405 xfs_ioerror_alert("xfs_trans_read_buf", mp
,
407 if (tp
->t_flags
& XFS_TRANS_DIRTY
)
408 xfs_force_shutdown(tp
->t_mountp
, SHUTDOWN_META_IO_ERROR
);
413 if (xfs_do_error
&& !(tp
->t_flags
& XFS_TRANS_DIRTY
)) {
414 if (xfs_error_target
== target
) {
415 if (((xfs_req_num
++) % xfs_error_mod
) == 0) {
416 xfs_force_shutdown(tp
->t_mountp
,
417 SHUTDOWN_META_IO_ERROR
);
419 cmn_err(CE_DEBUG
, "Returning trans error!\n");
420 return XFS_ERROR(EIO
);
425 if (XFS_FORCED_SHUTDOWN(mp
))
429 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
430 * it doesn't have one yet, then allocate one and initialize it.
431 * The checks to see if one is there are in xfs_buf_item_init().
433 xfs_buf_item_init(bp
, tp
->t_mountp
);
436 * Set the recursion count for the buffer within this transaction
439 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
440 ASSERT(!(bip
->bli_flags
& XFS_BLI_STALE
));
441 ASSERT(!(bip
->bli_format
.blf_flags
& XFS_BLI_CANCEL
));
442 ASSERT(!(bip
->bli_flags
& XFS_BLI_LOGGED
));
446 * Take a reference for this transaction on the buf item.
448 atomic_inc(&bip
->bli_refcount
);
451 * Get a log_item_desc to point at the new item.
453 (void) xfs_trans_add_item(tp
, (xfs_log_item_t
*)bip
);
456 * Initialize b_fsprivate2 so we can find it with incore_match()
459 XFS_BUF_SET_FSPRIVATE2(bp
, tp
);
461 trace_xfs_trans_read_buf(bip
);
467 * the theory here is that buffer is good but we're
468 * bailing out because the filesystem is being forcibly
469 * shut down. So we should leave the b_flags alone since
470 * the buffer's not staled and just get out.
473 if (XFS_BUF_ISSTALE(bp
) && XFS_BUF_ISDELAYWRITE(bp
))
474 cmn_err(CE_NOTE
, "about to pop assert, bp == 0x%p", bp
);
476 ASSERT((XFS_BUF_BFLAGS(bp
) & (XBF_STALE
|XBF_DELWRI
)) !=
477 (XBF_STALE
|XBF_DELWRI
));
479 trace_xfs_trans_read_buf_shut(bp
, _RET_IP_
);
482 return XFS_ERROR(EIO
);
487 * Release the buffer bp which was previously acquired with one of the
488 * xfs_trans_... buffer allocation routines if the buffer has not
489 * been modified within this transaction. If the buffer is modified
490 * within this transaction, do decrement the recursion count but do
491 * not release the buffer even if the count goes to 0. If the buffer is not
492 * modified within the transaction, decrement the recursion count and
493 * release the buffer if the recursion count goes to 0.
495 * If the buffer is to be released and it was not modified before
496 * this transaction began, then free the buf_log_item associated with it.
498 * If the transaction pointer is NULL, make this just a normal
502 xfs_trans_brelse(xfs_trans_t
*tp
,
505 xfs_buf_log_item_t
*bip
;
507 xfs_log_item_desc_t
*lidp
;
510 * Default to a normal brelse() call if the tp is NULL.
513 ASSERT(XFS_BUF_FSPRIVATE2(bp
, void *) == NULL
);
515 * If there's a buf log item attached to the buffer,
516 * then let the AIL know that the buffer is being
519 if (XFS_BUF_FSPRIVATE(bp
, void *) != NULL
) {
520 lip
= XFS_BUF_FSPRIVATE(bp
, xfs_log_item_t
*);
521 if (lip
->li_type
== XFS_LI_BUF
) {
522 bip
= XFS_BUF_FSPRIVATE(bp
,xfs_buf_log_item_t
*);
523 xfs_trans_unlocked_item(bip
->bli_item
.li_ailp
,
531 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
532 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
533 ASSERT(bip
->bli_item
.li_type
== XFS_LI_BUF
);
534 ASSERT(!(bip
->bli_flags
& XFS_BLI_STALE
));
535 ASSERT(!(bip
->bli_format
.blf_flags
& XFS_BLI_CANCEL
));
536 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
539 * Find the item descriptor pointing to this buffer's
540 * log item. It must be there.
542 lidp
= xfs_trans_find_item(tp
, (xfs_log_item_t
*)bip
);
543 ASSERT(lidp
!= NULL
);
545 trace_xfs_trans_brelse(bip
);
548 * If the release is just for a recursive lock,
549 * then decrement the count and return.
551 if (bip
->bli_recur
> 0) {
557 * If the buffer is dirty within this transaction, we can't
558 * release it until we commit.
560 if (lidp
->lid_flags
& XFS_LID_DIRTY
)
564 * If the buffer has been invalidated, then we can't release
565 * it until the transaction commits to disk unless it is re-dirtied
566 * as part of this transaction. This prevents us from pulling
567 * the item from the AIL before we should.
569 if (bip
->bli_flags
& XFS_BLI_STALE
)
572 ASSERT(!(bip
->bli_flags
& XFS_BLI_LOGGED
));
575 * Free up the log item descriptor tracking the released item.
577 xfs_trans_free_item(tp
, lidp
);
580 * Clear the hold flag in the buf log item if it is set.
581 * We wouldn't want the next user of the buffer to
584 if (bip
->bli_flags
& XFS_BLI_HOLD
) {
585 bip
->bli_flags
&= ~XFS_BLI_HOLD
;
589 * Drop our reference to the buf log item.
591 atomic_dec(&bip
->bli_refcount
);
594 * If the buf item is not tracking data in the log, then
595 * we must free it before releasing the buffer back to the
596 * free pool. Before releasing the buffer to the free pool,
597 * clear the transaction pointer in b_fsprivate2 to dissolve
598 * its relation to this transaction.
600 if (!xfs_buf_item_dirty(bip
)) {
602 ASSERT(bp->b_pincount == 0);
604 ASSERT(atomic_read(&bip
->bli_refcount
) == 0);
605 ASSERT(!(bip
->bli_item
.li_flags
& XFS_LI_IN_AIL
));
606 ASSERT(!(bip
->bli_flags
& XFS_BLI_INODE_ALLOC_BUF
));
607 xfs_buf_item_relse(bp
);
610 XFS_BUF_SET_FSPRIVATE2(bp
, NULL
);
613 * If we've still got a buf log item on the buffer, then
614 * tell the AIL that the buffer is being unlocked.
617 xfs_trans_unlocked_item(bip
->bli_item
.li_ailp
,
618 (xfs_log_item_t
*)bip
);
626 * Add the locked buffer to the transaction.
627 * The buffer must be locked, and it cannot be associated with any
630 * If the buffer does not yet have a buf log item associated with it,
631 * then allocate one for it. Then add the buf item to the transaction.
634 xfs_trans_bjoin(xfs_trans_t
*tp
,
637 xfs_buf_log_item_t
*bip
;
639 ASSERT(XFS_BUF_ISBUSY(bp
));
640 ASSERT(XFS_BUF_FSPRIVATE2(bp
, void *) == NULL
);
643 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
644 * it doesn't have one yet, then allocate one and initialize it.
645 * The checks to see if one is there are in xfs_buf_item_init().
647 xfs_buf_item_init(bp
, tp
->t_mountp
);
648 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
649 ASSERT(!(bip
->bli_flags
& XFS_BLI_STALE
));
650 ASSERT(!(bip
->bli_format
.blf_flags
& XFS_BLI_CANCEL
));
651 ASSERT(!(bip
->bli_flags
& XFS_BLI_LOGGED
));
654 * Take a reference for this transaction on the buf item.
656 atomic_inc(&bip
->bli_refcount
);
659 * Get a log_item_desc to point at the new item.
661 (void) xfs_trans_add_item(tp
, (xfs_log_item_t
*)bip
);
664 * Initialize b_fsprivate2 so we can find it with incore_match()
665 * in xfs_trans_get_buf() and friends above.
667 XFS_BUF_SET_FSPRIVATE2(bp
, tp
);
669 trace_xfs_trans_bjoin(bip
);
673 * Mark the buffer as not needing to be unlocked when the buf item's
674 * IOP_UNLOCK() routine is called. The buffer must already be locked
675 * and associated with the given transaction.
679 xfs_trans_bhold(xfs_trans_t
*tp
,
682 xfs_buf_log_item_t
*bip
;
684 ASSERT(XFS_BUF_ISBUSY(bp
));
685 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
686 ASSERT(XFS_BUF_FSPRIVATE(bp
, void *) != NULL
);
688 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
689 ASSERT(!(bip
->bli_flags
& XFS_BLI_STALE
));
690 ASSERT(!(bip
->bli_format
.blf_flags
& XFS_BLI_CANCEL
));
691 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
692 bip
->bli_flags
|= XFS_BLI_HOLD
;
693 trace_xfs_trans_bhold(bip
);
697 * Cancel the previous buffer hold request made on this buffer
698 * for this transaction.
701 xfs_trans_bhold_release(xfs_trans_t
*tp
,
704 xfs_buf_log_item_t
*bip
;
706 ASSERT(XFS_BUF_ISBUSY(bp
));
707 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
708 ASSERT(XFS_BUF_FSPRIVATE(bp
, void *) != NULL
);
710 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
711 ASSERT(!(bip
->bli_flags
& XFS_BLI_STALE
));
712 ASSERT(!(bip
->bli_format
.blf_flags
& XFS_BLI_CANCEL
));
713 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
714 ASSERT(bip
->bli_flags
& XFS_BLI_HOLD
);
715 bip
->bli_flags
&= ~XFS_BLI_HOLD
;
717 trace_xfs_trans_bhold_release(bip
);
721 * This is called to mark bytes first through last inclusive of the given
722 * buffer as needing to be logged when the transaction is committed.
723 * The buffer must already be associated with the given transaction.
725 * First and last are numbers relative to the beginning of this buffer,
726 * so the first byte in the buffer is numbered 0 regardless of the
730 xfs_trans_log_buf(xfs_trans_t
*tp
,
735 xfs_buf_log_item_t
*bip
;
736 xfs_log_item_desc_t
*lidp
;
738 ASSERT(XFS_BUF_ISBUSY(bp
));
739 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
740 ASSERT(XFS_BUF_FSPRIVATE(bp
, void *) != NULL
);
741 ASSERT((first
<= last
) && (last
< XFS_BUF_COUNT(bp
)));
742 ASSERT((XFS_BUF_IODONE_FUNC(bp
) == NULL
) ||
743 (XFS_BUF_IODONE_FUNC(bp
) == xfs_buf_iodone_callbacks
));
746 * Mark the buffer as needing to be written out eventually,
747 * and set its iodone function to remove the buffer's buf log
748 * item from the AIL and free it when the buffer is flushed
749 * to disk. See xfs_buf_attach_iodone() for more details
750 * on li_cb and xfs_buf_iodone_callbacks().
751 * If we end up aborting this transaction, we trap this buffer
752 * inside the b_bdstrat callback so that this won't get written to
755 XFS_BUF_DELAYWRITE(bp
);
758 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
759 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
760 XFS_BUF_SET_IODONE_FUNC(bp
, xfs_buf_iodone_callbacks
);
761 bip
->bli_item
.li_cb
= (void(*)(xfs_buf_t
*,xfs_log_item_t
*))xfs_buf_iodone
;
763 trace_xfs_trans_log_buf(bip
);
766 * If we invalidated the buffer within this transaction, then
767 * cancel the invalidation now that we're dirtying the buffer
768 * again. There are no races with the code in xfs_buf_item_unpin(),
769 * because we have a reference to the buffer this entire time.
771 if (bip
->bli_flags
& XFS_BLI_STALE
) {
772 bip
->bli_flags
&= ~XFS_BLI_STALE
;
773 ASSERT(XFS_BUF_ISSTALE(bp
));
775 bip
->bli_format
.blf_flags
&= ~XFS_BLI_CANCEL
;
778 lidp
= xfs_trans_find_item(tp
, (xfs_log_item_t
*)bip
);
779 ASSERT(lidp
!= NULL
);
781 tp
->t_flags
|= XFS_TRANS_DIRTY
;
782 lidp
->lid_flags
|= XFS_LID_DIRTY
;
783 lidp
->lid_flags
&= ~XFS_LID_BUF_STALE
;
784 bip
->bli_flags
|= XFS_BLI_LOGGED
;
785 xfs_buf_item_log(bip
, first
, last
);
790 * This called to invalidate a buffer that is being used within
791 * a transaction. Typically this is because the blocks in the
792 * buffer are being freed, so we need to prevent it from being
793 * written out when we're done. Allowing it to be written again
794 * might overwrite data in the free blocks if they are reallocated
797 * We prevent the buffer from being written out by clearing the
798 * B_DELWRI flag. We can't always
799 * get rid of the buf log item at this point, though, because
800 * the buffer may still be pinned by another transaction. If that
801 * is the case, then we'll wait until the buffer is committed to
802 * disk for the last time (we can tell by the ref count) and
803 * free it in xfs_buf_item_unpin(). Until it is cleaned up we
804 * will keep the buffer locked so that the buffer and buf log item
812 xfs_log_item_desc_t
*lidp
;
813 xfs_buf_log_item_t
*bip
;
815 ASSERT(XFS_BUF_ISBUSY(bp
));
816 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
817 ASSERT(XFS_BUF_FSPRIVATE(bp
, void *) != NULL
);
819 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
820 lidp
= xfs_trans_find_item(tp
, (xfs_log_item_t
*)bip
);
821 ASSERT(lidp
!= NULL
);
822 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
824 trace_xfs_trans_binval(bip
);
826 if (bip
->bli_flags
& XFS_BLI_STALE
) {
828 * If the buffer is already invalidated, then
831 ASSERT(!(XFS_BUF_ISDELAYWRITE(bp
)));
832 ASSERT(XFS_BUF_ISSTALE(bp
));
833 ASSERT(!(bip
->bli_flags
& (XFS_BLI_LOGGED
| XFS_BLI_DIRTY
)));
834 ASSERT(!(bip
->bli_format
.blf_flags
& XFS_BLI_INODE_BUF
));
835 ASSERT(bip
->bli_format
.blf_flags
& XFS_BLI_CANCEL
);
836 ASSERT(lidp
->lid_flags
& XFS_LID_DIRTY
);
837 ASSERT(tp
->t_flags
& XFS_TRANS_DIRTY
);
842 * Clear the dirty bit in the buffer and set the STALE flag
843 * in the buf log item. The STALE flag will be used in
844 * xfs_buf_item_unpin() to determine if it should clean up
845 * when the last reference to the buf item is given up.
846 * We set the XFS_BLI_CANCEL flag in the buf log format structure
847 * and log the buf item. This will be used at recovery time
848 * to determine that copies of the buffer in the log before
849 * this should not be replayed.
850 * We mark the item descriptor and the transaction dirty so
851 * that we'll hold the buffer until after the commit.
853 * Since we're invalidating the buffer, we also clear the state
854 * about which parts of the buffer have been logged. We also
855 * clear the flag indicating that this is an inode buffer since
856 * the data in the buffer will no longer be valid.
858 * We set the stale bit in the buffer as well since we're getting
861 XFS_BUF_UNDELAYWRITE(bp
);
863 bip
->bli_flags
|= XFS_BLI_STALE
;
864 bip
->bli_flags
&= ~(XFS_BLI_LOGGED
| XFS_BLI_DIRTY
);
865 bip
->bli_format
.blf_flags
&= ~XFS_BLI_INODE_BUF
;
866 bip
->bli_format
.blf_flags
|= XFS_BLI_CANCEL
;
867 memset((char *)(bip
->bli_format
.blf_data_map
), 0,
868 (bip
->bli_format
.blf_map_size
* sizeof(uint
)));
869 lidp
->lid_flags
|= XFS_LID_DIRTY
|XFS_LID_BUF_STALE
;
870 tp
->t_flags
|= XFS_TRANS_DIRTY
;
874 * This call is used to indicate that the buffer contains on-disk
875 * inodes which must be handled specially during recovery. They
876 * require special handling because only the di_next_unlinked from
877 * the inodes in the buffer should be recovered. The rest of the
878 * data in the buffer is logged via the inodes themselves.
880 * All we do is set the XFS_BLI_INODE_BUF flag in the buffer's log
881 * format structure so that we'll know what to do at recovery time.
889 xfs_buf_log_item_t
*bip
;
891 ASSERT(XFS_BUF_ISBUSY(bp
));
892 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
893 ASSERT(XFS_BUF_FSPRIVATE(bp
, void *) != NULL
);
895 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
896 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
898 bip
->bli_format
.blf_flags
|= XFS_BLI_INODE_BUF
;
902 * This call is used to indicate that the buffer is going to
903 * be staled and was an inode buffer. This means it gets
904 * special processing during unpin - where any inodes
905 * associated with the buffer should be removed from ail.
906 * There is also special processing during recovery,
907 * any replay of the inodes in the buffer needs to be
908 * prevented as the buffer may have been reused.
911 xfs_trans_stale_inode_buf(
915 xfs_buf_log_item_t
*bip
;
917 ASSERT(XFS_BUF_ISBUSY(bp
));
918 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
919 ASSERT(XFS_BUF_FSPRIVATE(bp
, void *) != NULL
);
921 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
922 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
924 bip
->bli_flags
|= XFS_BLI_STALE_INODE
;
925 bip
->bli_item
.li_cb
= (void(*)(xfs_buf_t
*,xfs_log_item_t
*))
932 * Mark the buffer as being one which contains newly allocated
933 * inodes. We need to make sure that even if this buffer is
934 * relogged as an 'inode buf' we still recover all of the inode
935 * images in the face of a crash. This works in coordination with
936 * xfs_buf_item_committed() to ensure that the buffer remains in the
937 * AIL at its original location even after it has been relogged.
941 xfs_trans_inode_alloc_buf(
945 xfs_buf_log_item_t
*bip
;
947 ASSERT(XFS_BUF_ISBUSY(bp
));
948 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
949 ASSERT(XFS_BUF_FSPRIVATE(bp
, void *) != NULL
);
951 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
952 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
954 bip
->bli_flags
|= XFS_BLI_INODE_ALLOC_BUF
;
959 * Similar to xfs_trans_inode_buf(), this marks the buffer as a cluster of
960 * dquots. However, unlike in inode buffer recovery, dquot buffers get
961 * recovered in their entirety. (Hence, no XFS_BLI_DQUOT_ALLOC_BUF flag).
962 * The only thing that makes dquot buffers different from regular
963 * buffers is that we must not replay dquot bufs when recovering
964 * if a _corresponding_ quotaoff has happened. We also have to distinguish
965 * between usr dquot bufs and grp dquot bufs, because usr and grp quotas
966 * can be turned off independently.
975 xfs_buf_log_item_t
*bip
;
977 ASSERT(XFS_BUF_ISBUSY(bp
));
978 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
979 ASSERT(XFS_BUF_FSPRIVATE(bp
, void *) != NULL
);
980 ASSERT(type
== XFS_BLI_UDQUOT_BUF
||
981 type
== XFS_BLI_PDQUOT_BUF
||
982 type
== XFS_BLI_GDQUOT_BUF
);
984 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
985 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
987 bip
->bli_format
.blf_flags
|= type
;
991 * Check to see if a buffer matching the given parameters is already
992 * a part of the given transaction. Only check the first, embedded
993 * chunk, since we don't want to spend all day scanning large transactions.
996 xfs_trans_buf_item_match(
998 xfs_buftarg_t
*target
,
1002 xfs_log_item_chunk_t
*licp
;
1003 xfs_log_item_desc_t
*lidp
;
1004 xfs_buf_log_item_t
*blip
;
1010 licp
= &tp
->t_items
;
1011 if (!xfs_lic_are_all_free(licp
)) {
1012 for (i
= 0; i
< licp
->lic_unused
; i
++) {
1014 * Skip unoccupied slots.
1016 if (xfs_lic_isfree(licp
, i
)) {
1020 lidp
= xfs_lic_slot(licp
, i
);
1021 blip
= (xfs_buf_log_item_t
*)lidp
->lid_item
;
1022 if (blip
->bli_item
.li_type
!= XFS_LI_BUF
) {
1027 if ((XFS_BUF_TARGET(bp
) == target
) &&
1028 (XFS_BUF_ADDR(bp
) == blkno
) &&
1029 (XFS_BUF_COUNT(bp
) == len
)) {
1031 * We found it. Break out and
1032 * return the pointer to the buffer.
1044 * Check to see if a buffer matching the given parameters is already
1045 * a part of the given transaction. Check all the chunks, we
1046 * want to be thorough.
1049 xfs_trans_buf_item_match_all(
1051 xfs_buftarg_t
*target
,
1055 xfs_log_item_chunk_t
*licp
;
1056 xfs_log_item_desc_t
*lidp
;
1057 xfs_buf_log_item_t
*blip
;
1063 for (licp
= &tp
->t_items
; licp
!= NULL
; licp
= licp
->lic_next
) {
1064 if (xfs_lic_are_all_free(licp
)) {
1065 ASSERT(licp
== &tp
->t_items
);
1066 ASSERT(licp
->lic_next
== NULL
);
1069 for (i
= 0; i
< licp
->lic_unused
; i
++) {
1071 * Skip unoccupied slots.
1073 if (xfs_lic_isfree(licp
, i
)) {
1077 lidp
= xfs_lic_slot(licp
, i
);
1078 blip
= (xfs_buf_log_item_t
*)lidp
->lid_item
;
1079 if (blip
->bli_item
.li_type
!= XFS_LI_BUF
) {
1084 if ((XFS_BUF_TARGET(bp
) == target
) &&
1085 (XFS_BUF_ADDR(bp
) == blkno
) &&
1086 (XFS_BUF_COUNT(bp
) == len
)) {
1088 * We found it. Break out and
1089 * return the pointer to the buffer.