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
= XFS_BUF_LOCK
| XFS_BUF_MAPPED
;
81 * Default to a normal get_buf() call if the tp is NULL.
84 return xfs_buf_get(target_dev
, blkno
, len
, flags
| BUF_BUSY
);
87 * If we find the buffer in the cache with this transaction
88 * pointer in its b_fsprivate2 field, then we know we already
89 * have it locked. In this case we just increment the lock
90 * recursion count and return the buffer to the caller.
92 if (tp
->t_items
.lic_next
== NULL
) {
93 bp
= xfs_trans_buf_item_match(tp
, target_dev
, blkno
, len
);
95 bp
= xfs_trans_buf_item_match_all(tp
, target_dev
, blkno
, len
);
98 ASSERT(XFS_BUF_VALUSEMA(bp
) <= 0);
99 if (XFS_FORCED_SHUTDOWN(tp
->t_mountp
))
100 XFS_BUF_SUPER_STALE(bp
);
103 * If the buffer is stale then it was binval'ed
104 * since last read. This doesn't matter since the
105 * caller isn't allowed to use the data anyway.
107 else if (XFS_BUF_ISSTALE(bp
))
108 ASSERT(!XFS_BUF_ISDELAYWRITE(bp
));
110 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
111 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
113 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
115 trace_xfs_trans_get_buf_recur(bip
);
120 * We always specify the BUF_BUSY flag within a transaction so
121 * that get_buf does not try to push out a delayed write buffer
122 * which might cause another transaction to take place (if the
123 * buffer was delayed alloc). Such recursive transactions can
124 * easily deadlock with our current transaction as well as cause
125 * us to run out of stack space.
127 bp
= xfs_buf_get(target_dev
, blkno
, len
, flags
| BUF_BUSY
);
132 ASSERT(!XFS_BUF_GETERROR(bp
));
135 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
136 * it doesn't have one yet, then allocate one and initialize it.
137 * The checks to see if one is there are in xfs_buf_item_init().
139 xfs_buf_item_init(bp
, tp
->t_mountp
);
142 * Set the recursion count for the buffer within this transaction
145 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
146 ASSERT(!(bip
->bli_flags
& XFS_BLI_STALE
));
147 ASSERT(!(bip
->bli_format
.blf_flags
& XFS_BLI_CANCEL
));
148 ASSERT(!(bip
->bli_flags
& XFS_BLI_LOGGED
));
152 * Take a reference for this transaction on the buf item.
154 atomic_inc(&bip
->bli_refcount
);
157 * Get a log_item_desc to point at the new item.
159 (void) xfs_trans_add_item(tp
, (xfs_log_item_t
*)bip
);
162 * Initialize b_fsprivate2 so we can find it with incore_match()
165 XFS_BUF_SET_FSPRIVATE2(bp
, tp
);
167 trace_xfs_trans_get_buf(bip
);
172 * Get and lock the superblock buffer of this file system for the
175 * We don't need to use incore_match() here, because the superblock
176 * buffer is a private buffer which we keep a pointer to in the
180 xfs_trans_getsb(xfs_trans_t
*tp
,
181 struct xfs_mount
*mp
,
185 xfs_buf_log_item_t
*bip
;
188 * Default to just trying to lock the superblock buffer
192 return (xfs_getsb(mp
, flags
));
196 * If the superblock buffer already has this transaction
197 * pointer in its b_fsprivate2 field, then we know we already
198 * have it locked. In this case we just increment the lock
199 * recursion count and return the buffer to the caller.
202 if (XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
) {
203 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
205 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
207 trace_xfs_trans_getsb_recur(bip
);
211 bp
= xfs_getsb(mp
, flags
);
217 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
218 * it doesn't have one yet, then allocate one and initialize it.
219 * The checks to see if one is there are in xfs_buf_item_init().
221 xfs_buf_item_init(bp
, mp
);
224 * Set the recursion count for the buffer within this transaction
227 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
228 ASSERT(!(bip
->bli_flags
& XFS_BLI_STALE
));
229 ASSERT(!(bip
->bli_format
.blf_flags
& XFS_BLI_CANCEL
));
230 ASSERT(!(bip
->bli_flags
& XFS_BLI_LOGGED
));
234 * Take a reference for this transaction on the buf item.
236 atomic_inc(&bip
->bli_refcount
);
239 * Get a log_item_desc to point at the new item.
241 (void) xfs_trans_add_item(tp
, (xfs_log_item_t
*)bip
);
244 * Initialize b_fsprivate2 so we can find it with incore_match()
247 XFS_BUF_SET_FSPRIVATE2(bp
, tp
);
249 trace_xfs_trans_getsb(bip
);
254 xfs_buftarg_t
*xfs_error_target
;
257 int xfs_error_mod
= 33;
261 * Get and lock the buffer for the caller if it is not already
262 * locked within the given transaction. If it has not yet been
263 * read in, read it from disk. If it is already locked
264 * within the transaction and already read in, just increment its
265 * lock recursion count and return a pointer to it.
267 * Use the fast path function xfs_trans_buf_item_match() or the buffer
268 * cache routine incore_match() to find the buffer
269 * if it is already owned by this transaction.
271 * If we don't already own the buffer, use read_buf() to get it.
272 * If it doesn't yet have an associated xfs_buf_log_item structure,
273 * then allocate one and add the item to this transaction.
275 * If the transaction pointer is NULL, make this just a normal
282 xfs_buftarg_t
*target
,
289 xfs_buf_log_item_t
*bip
;
293 flags
= XFS_BUF_LOCK
| XFS_BUF_MAPPED
;
296 * Default to a normal get_buf() call if the tp is NULL.
299 bp
= xfs_buf_read(target
, blkno
, len
, flags
| BUF_BUSY
);
301 return (flags
& XFS_BUF_TRYLOCK
) ?
302 EAGAIN
: XFS_ERROR(ENOMEM
);
304 if (XFS_BUF_GETERROR(bp
) != 0) {
305 xfs_ioerror_alert("xfs_trans_read_buf", mp
,
307 error
= XFS_BUF_GETERROR(bp
);
313 if (xfs_error_target
== target
) {
314 if (((xfs_req_num
++) % xfs_error_mod
) == 0) {
316 cmn_err(CE_DEBUG
, "Returning error!\n");
317 return XFS_ERROR(EIO
);
322 if (XFS_FORCED_SHUTDOWN(mp
))
329 * If we find the buffer in the cache with this transaction
330 * pointer in its b_fsprivate2 field, then we know we already
331 * have it locked. If it is already read in we just increment
332 * the lock recursion count and return the buffer to the caller.
333 * If the buffer is not yet read in, then we read it in, increment
334 * the lock recursion count, and return it to the caller.
336 if (tp
->t_items
.lic_next
== NULL
) {
337 bp
= xfs_trans_buf_item_match(tp
, target
, blkno
, len
);
339 bp
= xfs_trans_buf_item_match_all(tp
, target
, blkno
, len
);
342 ASSERT(XFS_BUF_VALUSEMA(bp
) <= 0);
343 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
344 ASSERT(XFS_BUF_FSPRIVATE(bp
, void *) != NULL
);
345 ASSERT((XFS_BUF_ISERROR(bp
)) == 0);
346 if (!(XFS_BUF_ISDONE(bp
))) {
347 trace_xfs_trans_read_buf_io(bp
, _RET_IP_
);
348 ASSERT(!XFS_BUF_ISASYNC(bp
));
350 xfsbdstrat(tp
->t_mountp
, bp
);
351 error
= xfs_iowait(bp
);
353 xfs_ioerror_alert("xfs_trans_read_buf", mp
,
357 * We can gracefully recover from most read
358 * errors. Ones we can't are those that happen
359 * after the transaction's already dirty.
361 if (tp
->t_flags
& XFS_TRANS_DIRTY
)
362 xfs_force_shutdown(tp
->t_mountp
,
363 SHUTDOWN_META_IO_ERROR
);
368 * We never locked this buf ourselves, so we shouldn't
369 * brelse it either. Just get out.
371 if (XFS_FORCED_SHUTDOWN(mp
)) {
372 trace_xfs_trans_read_buf_shut(bp
, _RET_IP_
);
374 return XFS_ERROR(EIO
);
378 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
381 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
382 trace_xfs_trans_read_buf_recur(bip
);
388 * We always specify the BUF_BUSY flag within a transaction so
389 * that get_buf does not try to push out a delayed write buffer
390 * which might cause another transaction to take place (if the
391 * buffer was delayed alloc). Such recursive transactions can
392 * easily deadlock with our current transaction as well as cause
393 * us to run out of stack space.
395 bp
= xfs_buf_read(target
, blkno
, len
, flags
| BUF_BUSY
);
400 if (XFS_BUF_GETERROR(bp
) != 0) {
401 XFS_BUF_SUPER_STALE(bp
);
402 error
= XFS_BUF_GETERROR(bp
);
404 xfs_ioerror_alert("xfs_trans_read_buf", mp
,
406 if (tp
->t_flags
& XFS_TRANS_DIRTY
)
407 xfs_force_shutdown(tp
->t_mountp
, SHUTDOWN_META_IO_ERROR
);
412 if (xfs_do_error
&& !(tp
->t_flags
& XFS_TRANS_DIRTY
)) {
413 if (xfs_error_target
== target
) {
414 if (((xfs_req_num
++) % xfs_error_mod
) == 0) {
415 xfs_force_shutdown(tp
->t_mountp
,
416 SHUTDOWN_META_IO_ERROR
);
418 cmn_err(CE_DEBUG
, "Returning trans error!\n");
419 return XFS_ERROR(EIO
);
424 if (XFS_FORCED_SHUTDOWN(mp
))
428 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
429 * it doesn't have one yet, then allocate one and initialize it.
430 * The checks to see if one is there are in xfs_buf_item_init().
432 xfs_buf_item_init(bp
, tp
->t_mountp
);
435 * Set the recursion count for the buffer within this transaction
438 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
439 ASSERT(!(bip
->bli_flags
& XFS_BLI_STALE
));
440 ASSERT(!(bip
->bli_format
.blf_flags
& XFS_BLI_CANCEL
));
441 ASSERT(!(bip
->bli_flags
& XFS_BLI_LOGGED
));
445 * Take a reference for this transaction on the buf item.
447 atomic_inc(&bip
->bli_refcount
);
450 * Get a log_item_desc to point at the new item.
452 (void) xfs_trans_add_item(tp
, (xfs_log_item_t
*)bip
);
455 * Initialize b_fsprivate2 so we can find it with incore_match()
458 XFS_BUF_SET_FSPRIVATE2(bp
, tp
);
460 trace_xfs_trans_read_buf(bip
);
466 * the theory here is that buffer is good but we're
467 * bailing out because the filesystem is being forcibly
468 * shut down. So we should leave the b_flags alone since
469 * the buffer's not staled and just get out.
472 if (XFS_BUF_ISSTALE(bp
) && XFS_BUF_ISDELAYWRITE(bp
))
473 cmn_err(CE_NOTE
, "about to pop assert, bp == 0x%p", bp
);
475 ASSERT((XFS_BUF_BFLAGS(bp
) & (XFS_B_STALE
|XFS_B_DELWRI
)) !=
476 (XFS_B_STALE
|XFS_B_DELWRI
));
478 trace_xfs_trans_read_buf_shut(bp
, _RET_IP_
);
481 return XFS_ERROR(EIO
);
486 * Release the buffer bp which was previously acquired with one of the
487 * xfs_trans_... buffer allocation routines if the buffer has not
488 * been modified within this transaction. If the buffer is modified
489 * within this transaction, do decrement the recursion count but do
490 * not release the buffer even if the count goes to 0. If the buffer is not
491 * modified within the transaction, decrement the recursion count and
492 * release the buffer if the recursion count goes to 0.
494 * If the buffer is to be released and it was not modified before
495 * this transaction began, then free the buf_log_item associated with it.
497 * If the transaction pointer is NULL, make this just a normal
501 xfs_trans_brelse(xfs_trans_t
*tp
,
504 xfs_buf_log_item_t
*bip
;
506 xfs_log_item_desc_t
*lidp
;
509 * Default to a normal brelse() call if the tp is NULL.
512 ASSERT(XFS_BUF_FSPRIVATE2(bp
, void *) == NULL
);
514 * If there's a buf log item attached to the buffer,
515 * then let the AIL know that the buffer is being
518 if (XFS_BUF_FSPRIVATE(bp
, void *) != NULL
) {
519 lip
= XFS_BUF_FSPRIVATE(bp
, xfs_log_item_t
*);
520 if (lip
->li_type
== XFS_LI_BUF
) {
521 bip
= XFS_BUF_FSPRIVATE(bp
,xfs_buf_log_item_t
*);
522 xfs_trans_unlocked_item(bip
->bli_item
.li_ailp
,
530 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
531 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
532 ASSERT(bip
->bli_item
.li_type
== XFS_LI_BUF
);
533 ASSERT(!(bip
->bli_flags
& XFS_BLI_STALE
));
534 ASSERT(!(bip
->bli_format
.blf_flags
& XFS_BLI_CANCEL
));
535 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
538 * Find the item descriptor pointing to this buffer's
539 * log item. It must be there.
541 lidp
= xfs_trans_find_item(tp
, (xfs_log_item_t
*)bip
);
542 ASSERT(lidp
!= NULL
);
544 trace_xfs_trans_brelse(bip
);
547 * If the release is just for a recursive lock,
548 * then decrement the count and return.
550 if (bip
->bli_recur
> 0) {
556 * If the buffer is dirty within this transaction, we can't
557 * release it until we commit.
559 if (lidp
->lid_flags
& XFS_LID_DIRTY
)
563 * If the buffer has been invalidated, then we can't release
564 * it until the transaction commits to disk unless it is re-dirtied
565 * as part of this transaction. This prevents us from pulling
566 * the item from the AIL before we should.
568 if (bip
->bli_flags
& XFS_BLI_STALE
)
571 ASSERT(!(bip
->bli_flags
& XFS_BLI_LOGGED
));
574 * Free up the log item descriptor tracking the released item.
576 xfs_trans_free_item(tp
, lidp
);
579 * Clear the hold flag in the buf log item if it is set.
580 * We wouldn't want the next user of the buffer to
583 if (bip
->bli_flags
& XFS_BLI_HOLD
) {
584 bip
->bli_flags
&= ~XFS_BLI_HOLD
;
588 * Drop our reference to the buf log item.
590 atomic_dec(&bip
->bli_refcount
);
593 * If the buf item is not tracking data in the log, then
594 * we must free it before releasing the buffer back to the
595 * free pool. Before releasing the buffer to the free pool,
596 * clear the transaction pointer in b_fsprivate2 to dissolve
597 * its relation to this transaction.
599 if (!xfs_buf_item_dirty(bip
)) {
601 ASSERT(bp->b_pincount == 0);
603 ASSERT(atomic_read(&bip
->bli_refcount
) == 0);
604 ASSERT(!(bip
->bli_item
.li_flags
& XFS_LI_IN_AIL
));
605 ASSERT(!(bip
->bli_flags
& XFS_BLI_INODE_ALLOC_BUF
));
606 xfs_buf_item_relse(bp
);
609 XFS_BUF_SET_FSPRIVATE2(bp
, NULL
);
612 * If we've still got a buf log item on the buffer, then
613 * tell the AIL that the buffer is being unlocked.
616 xfs_trans_unlocked_item(bip
->bli_item
.li_ailp
,
617 (xfs_log_item_t
*)bip
);
625 * Add the locked buffer to the transaction.
626 * The buffer must be locked, and it cannot be associated with any
629 * If the buffer does not yet have a buf log item associated with it,
630 * then allocate one for it. Then add the buf item to the transaction.
633 xfs_trans_bjoin(xfs_trans_t
*tp
,
636 xfs_buf_log_item_t
*bip
;
638 ASSERT(XFS_BUF_ISBUSY(bp
));
639 ASSERT(XFS_BUF_FSPRIVATE2(bp
, void *) == NULL
);
642 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
643 * it doesn't have one yet, then allocate one and initialize it.
644 * The checks to see if one is there are in xfs_buf_item_init().
646 xfs_buf_item_init(bp
, tp
->t_mountp
);
647 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
648 ASSERT(!(bip
->bli_flags
& XFS_BLI_STALE
));
649 ASSERT(!(bip
->bli_format
.blf_flags
& XFS_BLI_CANCEL
));
650 ASSERT(!(bip
->bli_flags
& XFS_BLI_LOGGED
));
653 * Take a reference for this transaction on the buf item.
655 atomic_inc(&bip
->bli_refcount
);
658 * Get a log_item_desc to point at the new item.
660 (void) xfs_trans_add_item(tp
, (xfs_log_item_t
*)bip
);
663 * Initialize b_fsprivate2 so we can find it with incore_match()
664 * in xfs_trans_get_buf() and friends above.
666 XFS_BUF_SET_FSPRIVATE2(bp
, tp
);
668 trace_xfs_trans_bjoin(bip
);
672 * Mark the buffer as not needing to be unlocked when the buf item's
673 * IOP_UNLOCK() routine is called. The buffer must already be locked
674 * and associated with the given transaction.
678 xfs_trans_bhold(xfs_trans_t
*tp
,
681 xfs_buf_log_item_t
*bip
;
683 ASSERT(XFS_BUF_ISBUSY(bp
));
684 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
685 ASSERT(XFS_BUF_FSPRIVATE(bp
, void *) != NULL
);
687 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
688 ASSERT(!(bip
->bli_flags
& XFS_BLI_STALE
));
689 ASSERT(!(bip
->bli_format
.blf_flags
& XFS_BLI_CANCEL
));
690 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
691 bip
->bli_flags
|= XFS_BLI_HOLD
;
692 trace_xfs_trans_bhold(bip
);
696 * Cancel the previous buffer hold request made on this buffer
697 * for this transaction.
700 xfs_trans_bhold_release(xfs_trans_t
*tp
,
703 xfs_buf_log_item_t
*bip
;
705 ASSERT(XFS_BUF_ISBUSY(bp
));
706 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
707 ASSERT(XFS_BUF_FSPRIVATE(bp
, void *) != NULL
);
709 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
710 ASSERT(!(bip
->bli_flags
& XFS_BLI_STALE
));
711 ASSERT(!(bip
->bli_format
.blf_flags
& XFS_BLI_CANCEL
));
712 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
713 ASSERT(bip
->bli_flags
& XFS_BLI_HOLD
);
714 bip
->bli_flags
&= ~XFS_BLI_HOLD
;
716 trace_xfs_trans_bhold_release(bip
);
720 * This is called to mark bytes first through last inclusive of the given
721 * buffer as needing to be logged when the transaction is committed.
722 * The buffer must already be associated with the given transaction.
724 * First and last are numbers relative to the beginning of this buffer,
725 * so the first byte in the buffer is numbered 0 regardless of the
729 xfs_trans_log_buf(xfs_trans_t
*tp
,
734 xfs_buf_log_item_t
*bip
;
735 xfs_log_item_desc_t
*lidp
;
737 ASSERT(XFS_BUF_ISBUSY(bp
));
738 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
739 ASSERT(XFS_BUF_FSPRIVATE(bp
, void *) != NULL
);
740 ASSERT((first
<= last
) && (last
< XFS_BUF_COUNT(bp
)));
741 ASSERT((XFS_BUF_IODONE_FUNC(bp
) == NULL
) ||
742 (XFS_BUF_IODONE_FUNC(bp
) == xfs_buf_iodone_callbacks
));
745 * Mark the buffer as needing to be written out eventually,
746 * and set its iodone function to remove the buffer's buf log
747 * item from the AIL and free it when the buffer is flushed
748 * to disk. See xfs_buf_attach_iodone() for more details
749 * on li_cb and xfs_buf_iodone_callbacks().
750 * If we end up aborting this transaction, we trap this buffer
751 * inside the b_bdstrat callback so that this won't get written to
754 XFS_BUF_DELAYWRITE(bp
);
757 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
758 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
759 XFS_BUF_SET_IODONE_FUNC(bp
, xfs_buf_iodone_callbacks
);
760 bip
->bli_item
.li_cb
= (void(*)(xfs_buf_t
*,xfs_log_item_t
*))xfs_buf_iodone
;
762 trace_xfs_trans_log_buf(bip
);
765 * If we invalidated the buffer within this transaction, then
766 * cancel the invalidation now that we're dirtying the buffer
767 * again. There are no races with the code in xfs_buf_item_unpin(),
768 * because we have a reference to the buffer this entire time.
770 if (bip
->bli_flags
& XFS_BLI_STALE
) {
771 bip
->bli_flags
&= ~XFS_BLI_STALE
;
772 ASSERT(XFS_BUF_ISSTALE(bp
));
774 bip
->bli_format
.blf_flags
&= ~XFS_BLI_CANCEL
;
777 lidp
= xfs_trans_find_item(tp
, (xfs_log_item_t
*)bip
);
778 ASSERT(lidp
!= NULL
);
780 tp
->t_flags
|= XFS_TRANS_DIRTY
;
781 lidp
->lid_flags
|= XFS_LID_DIRTY
;
782 lidp
->lid_flags
&= ~XFS_LID_BUF_STALE
;
783 bip
->bli_flags
|= XFS_BLI_LOGGED
;
784 xfs_buf_item_log(bip
, first
, last
);
789 * This called to invalidate a buffer that is being used within
790 * a transaction. Typically this is because the blocks in the
791 * buffer are being freed, so we need to prevent it from being
792 * written out when we're done. Allowing it to be written again
793 * might overwrite data in the free blocks if they are reallocated
796 * We prevent the buffer from being written out by clearing the
797 * B_DELWRI flag. We can't always
798 * get rid of the buf log item at this point, though, because
799 * the buffer may still be pinned by another transaction. If that
800 * is the case, then we'll wait until the buffer is committed to
801 * disk for the last time (we can tell by the ref count) and
802 * free it in xfs_buf_item_unpin(). Until it is cleaned up we
803 * will keep the buffer locked so that the buffer and buf log item
811 xfs_log_item_desc_t
*lidp
;
812 xfs_buf_log_item_t
*bip
;
814 ASSERT(XFS_BUF_ISBUSY(bp
));
815 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
816 ASSERT(XFS_BUF_FSPRIVATE(bp
, void *) != NULL
);
818 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
819 lidp
= xfs_trans_find_item(tp
, (xfs_log_item_t
*)bip
);
820 ASSERT(lidp
!= NULL
);
821 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
823 trace_xfs_trans_binval(bip
);
825 if (bip
->bli_flags
& XFS_BLI_STALE
) {
827 * If the buffer is already invalidated, then
830 ASSERT(!(XFS_BUF_ISDELAYWRITE(bp
)));
831 ASSERT(XFS_BUF_ISSTALE(bp
));
832 ASSERT(!(bip
->bli_flags
& (XFS_BLI_LOGGED
| XFS_BLI_DIRTY
)));
833 ASSERT(!(bip
->bli_format
.blf_flags
& XFS_BLI_INODE_BUF
));
834 ASSERT(bip
->bli_format
.blf_flags
& XFS_BLI_CANCEL
);
835 ASSERT(lidp
->lid_flags
& XFS_LID_DIRTY
);
836 ASSERT(tp
->t_flags
& XFS_TRANS_DIRTY
);
841 * Clear the dirty bit in the buffer and set the STALE flag
842 * in the buf log item. The STALE flag will be used in
843 * xfs_buf_item_unpin() to determine if it should clean up
844 * when the last reference to the buf item is given up.
845 * We set the XFS_BLI_CANCEL flag in the buf log format structure
846 * and log the buf item. This will be used at recovery time
847 * to determine that copies of the buffer in the log before
848 * this should not be replayed.
849 * We mark the item descriptor and the transaction dirty so
850 * that we'll hold the buffer until after the commit.
852 * Since we're invalidating the buffer, we also clear the state
853 * about which parts of the buffer have been logged. We also
854 * clear the flag indicating that this is an inode buffer since
855 * the data in the buffer will no longer be valid.
857 * We set the stale bit in the buffer as well since we're getting
860 XFS_BUF_UNDELAYWRITE(bp
);
862 bip
->bli_flags
|= XFS_BLI_STALE
;
863 bip
->bli_flags
&= ~(XFS_BLI_LOGGED
| XFS_BLI_DIRTY
);
864 bip
->bli_format
.blf_flags
&= ~XFS_BLI_INODE_BUF
;
865 bip
->bli_format
.blf_flags
|= XFS_BLI_CANCEL
;
866 memset((char *)(bip
->bli_format
.blf_data_map
), 0,
867 (bip
->bli_format
.blf_map_size
* sizeof(uint
)));
868 lidp
->lid_flags
|= XFS_LID_DIRTY
|XFS_LID_BUF_STALE
;
869 tp
->t_flags
|= XFS_TRANS_DIRTY
;
873 * This call is used to indicate that the buffer contains on-disk
874 * inodes which must be handled specially during recovery. They
875 * require special handling because only the di_next_unlinked from
876 * the inodes in the buffer should be recovered. The rest of the
877 * data in the buffer is logged via the inodes themselves.
879 * All we do is set the XFS_BLI_INODE_BUF flag in the buffer's log
880 * format structure so that we'll know what to do at recovery time.
888 xfs_buf_log_item_t
*bip
;
890 ASSERT(XFS_BUF_ISBUSY(bp
));
891 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
892 ASSERT(XFS_BUF_FSPRIVATE(bp
, void *) != NULL
);
894 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
895 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
897 bip
->bli_format
.blf_flags
|= XFS_BLI_INODE_BUF
;
901 * This call is used to indicate that the buffer is going to
902 * be staled and was an inode buffer. This means it gets
903 * special processing during unpin - where any inodes
904 * associated with the buffer should be removed from ail.
905 * There is also special processing during recovery,
906 * any replay of the inodes in the buffer needs to be
907 * prevented as the buffer may have been reused.
910 xfs_trans_stale_inode_buf(
914 xfs_buf_log_item_t
*bip
;
916 ASSERT(XFS_BUF_ISBUSY(bp
));
917 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
918 ASSERT(XFS_BUF_FSPRIVATE(bp
, void *) != NULL
);
920 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
921 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
923 bip
->bli_flags
|= XFS_BLI_STALE_INODE
;
924 bip
->bli_item
.li_cb
= (void(*)(xfs_buf_t
*,xfs_log_item_t
*))
931 * Mark the buffer as being one which contains newly allocated
932 * inodes. We need to make sure that even if this buffer is
933 * relogged as an 'inode buf' we still recover all of the inode
934 * images in the face of a crash. This works in coordination with
935 * xfs_buf_item_committed() to ensure that the buffer remains in the
936 * AIL at its original location even after it has been relogged.
940 xfs_trans_inode_alloc_buf(
944 xfs_buf_log_item_t
*bip
;
946 ASSERT(XFS_BUF_ISBUSY(bp
));
947 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
948 ASSERT(XFS_BUF_FSPRIVATE(bp
, void *) != NULL
);
950 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
951 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
953 bip
->bli_flags
|= XFS_BLI_INODE_ALLOC_BUF
;
958 * Similar to xfs_trans_inode_buf(), this marks the buffer as a cluster of
959 * dquots. However, unlike in inode buffer recovery, dquot buffers get
960 * recovered in their entirety. (Hence, no XFS_BLI_DQUOT_ALLOC_BUF flag).
961 * The only thing that makes dquot buffers different from regular
962 * buffers is that we must not replay dquot bufs when recovering
963 * if a _corresponding_ quotaoff has happened. We also have to distinguish
964 * between usr dquot bufs and grp dquot bufs, because usr and grp quotas
965 * can be turned off independently.
974 xfs_buf_log_item_t
*bip
;
976 ASSERT(XFS_BUF_ISBUSY(bp
));
977 ASSERT(XFS_BUF_FSPRIVATE2(bp
, xfs_trans_t
*) == tp
);
978 ASSERT(XFS_BUF_FSPRIVATE(bp
, void *) != NULL
);
979 ASSERT(type
== XFS_BLI_UDQUOT_BUF
||
980 type
== XFS_BLI_PDQUOT_BUF
||
981 type
== XFS_BLI_GDQUOT_BUF
);
983 bip
= XFS_BUF_FSPRIVATE(bp
, xfs_buf_log_item_t
*);
984 ASSERT(atomic_read(&bip
->bli_refcount
) > 0);
986 bip
->bli_format
.blf_flags
|= type
;
990 * Check to see if a buffer matching the given parameters is already
991 * a part of the given transaction. Only check the first, embedded
992 * chunk, since we don't want to spend all day scanning large transactions.
995 xfs_trans_buf_item_match(
997 xfs_buftarg_t
*target
,
1001 xfs_log_item_chunk_t
*licp
;
1002 xfs_log_item_desc_t
*lidp
;
1003 xfs_buf_log_item_t
*blip
;
1009 licp
= &tp
->t_items
;
1010 if (!xfs_lic_are_all_free(licp
)) {
1011 for (i
= 0; i
< licp
->lic_unused
; i
++) {
1013 * Skip unoccupied slots.
1015 if (xfs_lic_isfree(licp
, i
)) {
1019 lidp
= xfs_lic_slot(licp
, i
);
1020 blip
= (xfs_buf_log_item_t
*)lidp
->lid_item
;
1021 if (blip
->bli_item
.li_type
!= XFS_LI_BUF
) {
1026 if ((XFS_BUF_TARGET(bp
) == target
) &&
1027 (XFS_BUF_ADDR(bp
) == blkno
) &&
1028 (XFS_BUF_COUNT(bp
) == len
)) {
1030 * We found it. Break out and
1031 * return the pointer to the buffer.
1043 * Check to see if a buffer matching the given parameters is already
1044 * a part of the given transaction. Check all the chunks, we
1045 * want to be thorough.
1048 xfs_trans_buf_item_match_all(
1050 xfs_buftarg_t
*target
,
1054 xfs_log_item_chunk_t
*licp
;
1055 xfs_log_item_desc_t
*lidp
;
1056 xfs_buf_log_item_t
*blip
;
1062 for (licp
= &tp
->t_items
; licp
!= NULL
; licp
= licp
->lic_next
) {
1063 if (xfs_lic_are_all_free(licp
)) {
1064 ASSERT(licp
== &tp
->t_items
);
1065 ASSERT(licp
->lic_next
== NULL
);
1068 for (i
= 0; i
< licp
->lic_unused
; i
++) {
1070 * Skip unoccupied slots.
1072 if (xfs_lic_isfree(licp
, i
)) {
1076 lidp
= xfs_lic_slot(licp
, i
);
1077 blip
= (xfs_buf_log_item_t
*)lidp
->lid_item
;
1078 if (blip
->bli_item
.li_type
!= XFS_LI_BUF
) {
1083 if ((XFS_BUF_TARGET(bp
) == target
) &&
1084 (XFS_BUF_ADDR(bp
) == blkno
) &&
1085 (XFS_BUF_COUNT(bp
) == len
)) {
1087 * We found it. Break out and
1088 * return the pointer to the buffer.