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"
43 STATIC xfs_buf_t *xfs_trans_buf_item_match(xfs_trans_t *, xfs_buftarg_t *,
45 STATIC xfs_buf_t *xfs_trans_buf_item_match_all(xfs_trans_t *, xfs_buftarg_t *,
50 * Get and lock the buffer for the caller if it is not already
51 * locked within the given transaction. If it is already locked
52 * within the transaction, just increment its lock recursion count
53 * and return a pointer to it.
55 * Use the fast path function xfs_trans_buf_item_match() or the buffer
56 * cache routine incore_match() to find the buffer
57 * if it is already owned by this transaction.
59 * If we don't already own the buffer, use get_buf() to get it.
60 * If it doesn't yet have an associated xfs_buf_log_item structure,
61 * then allocate one and add the item to this transaction.
63 * If the transaction pointer is NULL, make this just a normal
67 xfs_trans_get_buf(xfs_trans_t *tp,
68 xfs_buftarg_t *target_dev,
74 xfs_buf_log_item_t *bip;
77 flags = XFS_BUF_LOCK | XFS_BUF_MAPPED;
80 * Default to a normal get_buf() call if the tp is NULL.
83 return xfs_buf_get(target_dev, blkno, len, flags | BUF_BUSY);
86 * If we find the buffer in the cache with this transaction
87 * pointer in its b_fsprivate2 field, then we know we already
88 * have it locked. In this case we just increment the lock
89 * recursion count and return the buffer to the caller.
91 if (tp->t_items.lic_next == NULL) {
92 bp = xfs_trans_buf_item_match(tp, target_dev, blkno, len);
94 bp = xfs_trans_buf_item_match_all(tp, target_dev, blkno, len);
97 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
98 if (XFS_FORCED_SHUTDOWN(tp->t_mountp)) {
99 xfs_buftrace("TRANS GET RECUR SHUT", bp);
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 xfs_buftrace("TRANS GET RECUR STALE", 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 xfs_buftrace("TRANS GET RECUR", bp);
117 xfs_buf_item_trace("GET RECUR", bip);
122 * We always specify the BUF_BUSY flag within a transaction so
123 * that get_buf does not try to push out a delayed write buffer
124 * which might cause another transaction to take place (if the
125 * buffer was delayed alloc). Such recursive transactions can
126 * easily deadlock with our current transaction as well as cause
127 * us to run out of stack space.
129 bp = xfs_buf_get(target_dev, blkno, len, flags | BUF_BUSY);
134 ASSERT(!XFS_BUF_GETERROR(bp));
137 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
138 * it doesn't have one yet, then allocate one and initialize it.
139 * The checks to see if one is there are in xfs_buf_item_init().
141 xfs_buf_item_init(bp, tp->t_mountp);
144 * Set the recursion count for the buffer within this transaction
147 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
148 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
149 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
150 ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
154 * Take a reference for this transaction on the buf item.
156 atomic_inc(&bip->bli_refcount);
159 * Get a log_item_desc to point at the new item.
161 (void) xfs_trans_add_item(tp, (xfs_log_item_t*)bip);
164 * Initialize b_fsprivate2 so we can find it with incore_match()
167 XFS_BUF_SET_FSPRIVATE2(bp, tp);
169 xfs_buftrace("TRANS GET", bp);
170 xfs_buf_item_trace("GET", bip);
175 * Get and lock the superblock buffer of this file system for the
178 * We don't need to use incore_match() here, because the superblock
179 * buffer is a private buffer which we keep a pointer to in the
183 xfs_trans_getsb(xfs_trans_t *tp,
184 struct xfs_mount *mp,
188 xfs_buf_log_item_t *bip;
191 * Default to just trying to lock the superblock buffer
195 return (xfs_getsb(mp, flags));
199 * If the superblock buffer already has this transaction
200 * pointer in its b_fsprivate2 field, then we know we already
201 * have it locked. In this case we just increment the lock
202 * recursion count and return the buffer to the caller.
205 if (XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp) {
206 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
208 ASSERT(atomic_read(&bip->bli_refcount) > 0);
210 xfs_buf_item_trace("GETSB RECUR", bip);
214 bp = xfs_getsb(mp, flags);
220 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
221 * it doesn't have one yet, then allocate one and initialize it.
222 * The checks to see if one is there are in xfs_buf_item_init().
224 xfs_buf_item_init(bp, mp);
227 * Set the recursion count for the buffer within this transaction
230 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
231 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
232 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
233 ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
237 * Take a reference for this transaction on the buf item.
239 atomic_inc(&bip->bli_refcount);
242 * Get a log_item_desc to point at the new item.
244 (void) xfs_trans_add_item(tp, (xfs_log_item_t*)bip);
247 * Initialize b_fsprivate2 so we can find it with incore_match()
250 XFS_BUF_SET_FSPRIVATE2(bp, tp);
252 xfs_buf_item_trace("GETSB", bip);
257 xfs_buftarg_t *xfs_error_target;
260 int xfs_error_mod = 33;
264 * Get and lock the buffer for the caller if it is not already
265 * locked within the given transaction. If it has not yet been
266 * read in, read it from disk. If it is already locked
267 * within the transaction and already read in, just increment its
268 * lock recursion count and return a pointer to it.
270 * Use the fast path function xfs_trans_buf_item_match() or the buffer
271 * cache routine incore_match() to find the buffer
272 * if it is already owned by this transaction.
274 * If we don't already own the buffer, use read_buf() to get it.
275 * If it doesn't yet have an associated xfs_buf_log_item structure,
276 * then allocate one and add the item to this transaction.
278 * If the transaction pointer is NULL, make this just a normal
285 xfs_buftarg_t *target,
292 xfs_buf_log_item_t *bip;
296 flags = XFS_BUF_LOCK | XFS_BUF_MAPPED;
299 * Default to a normal get_buf() call if the tp is NULL.
302 bp = xfs_buf_read(target, blkno, len, flags | BUF_BUSY);
304 return (flags & XFS_BUF_TRYLOCK) ?
305 EAGAIN : XFS_ERROR(ENOMEM);
307 if (XFS_BUF_GETERROR(bp) != 0) {
308 xfs_ioerror_alert("xfs_trans_read_buf", mp,
310 error = XFS_BUF_GETERROR(bp);
316 if (xfs_error_target == target) {
317 if (((xfs_req_num++) % xfs_error_mod) == 0) {
319 cmn_err(CE_DEBUG, "Returning error!\n");
320 return XFS_ERROR(EIO);
325 if (XFS_FORCED_SHUTDOWN(mp))
332 * If we find the buffer in the cache with this transaction
333 * pointer in its b_fsprivate2 field, then we know we already
334 * have it locked. If it is already read in we just increment
335 * the lock recursion count and return the buffer to the caller.
336 * If the buffer is not yet read in, then we read it in, increment
337 * the lock recursion count, and return it to the caller.
339 if (tp->t_items.lic_next == NULL) {
340 bp = xfs_trans_buf_item_match(tp, target, blkno, len);
342 bp = xfs_trans_buf_item_match_all(tp, target, blkno, len);
345 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
346 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
347 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
348 ASSERT((XFS_BUF_ISERROR(bp)) == 0);
349 if (!(XFS_BUF_ISDONE(bp))) {
350 xfs_buftrace("READ_BUF_INCORE !DONE", bp);
351 ASSERT(!XFS_BUF_ISASYNC(bp));
353 xfsbdstrat(tp->t_mountp, bp);
354 error = xfs_iowait(bp);
356 xfs_ioerror_alert("xfs_trans_read_buf", mp,
360 * We can gracefully recover from most read
361 * errors. Ones we can't are those that happen
362 * after the transaction's already dirty.
364 if (tp->t_flags & XFS_TRANS_DIRTY)
365 xfs_force_shutdown(tp->t_mountp,
366 SHUTDOWN_META_IO_ERROR);
371 * We never locked this buf ourselves, so we shouldn't
372 * brelse it either. Just get out.
374 if (XFS_FORCED_SHUTDOWN(mp)) {
375 xfs_buftrace("READ_BUF_INCORE XFSSHUTDN", bp);
377 return XFS_ERROR(EIO);
381 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
384 ASSERT(atomic_read(&bip->bli_refcount) > 0);
385 xfs_buf_item_trace("READ RECUR", bip);
391 * We always specify the BUF_BUSY flag within a transaction so
392 * that get_buf does not try to push out a delayed write buffer
393 * which might cause another transaction to take place (if the
394 * buffer was delayed alloc). Such recursive transactions can
395 * easily deadlock with our current transaction as well as cause
396 * us to run out of stack space.
398 bp = xfs_buf_read(target, blkno, len, flags | BUF_BUSY);
403 if (XFS_BUF_GETERROR(bp) != 0) {
404 XFS_BUF_SUPER_STALE(bp);
405 xfs_buftrace("READ ERROR", bp);
406 error = XFS_BUF_GETERROR(bp);
408 xfs_ioerror_alert("xfs_trans_read_buf", mp,
410 if (tp->t_flags & XFS_TRANS_DIRTY)
411 xfs_force_shutdown(tp->t_mountp, SHUTDOWN_META_IO_ERROR);
416 if (xfs_do_error && !(tp->t_flags & XFS_TRANS_DIRTY)) {
417 if (xfs_error_target == target) {
418 if (((xfs_req_num++) % xfs_error_mod) == 0) {
419 xfs_force_shutdown(tp->t_mountp,
420 SHUTDOWN_META_IO_ERROR);
422 cmn_err(CE_DEBUG, "Returning trans error!\n");
423 return XFS_ERROR(EIO);
428 if (XFS_FORCED_SHUTDOWN(mp))
432 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
433 * it doesn't have one yet, then allocate one and initialize it.
434 * The checks to see if one is there are in xfs_buf_item_init().
436 xfs_buf_item_init(bp, tp->t_mountp);
439 * Set the recursion count for the buffer within this transaction
442 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
443 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
444 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
445 ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
449 * Take a reference for this transaction on the buf item.
451 atomic_inc(&bip->bli_refcount);
454 * Get a log_item_desc to point at the new item.
456 (void) xfs_trans_add_item(tp, (xfs_log_item_t*)bip);
459 * Initialize b_fsprivate2 so we can find it with incore_match()
462 XFS_BUF_SET_FSPRIVATE2(bp, tp);
464 xfs_buftrace("TRANS READ", bp);
465 xfs_buf_item_trace("READ", bip);
471 * the theory here is that buffer is good but we're
472 * bailing out because the filesystem is being forcibly
473 * shut down. So we should leave the b_flags alone since
474 * the buffer's not staled and just get out.
477 if (XFS_BUF_ISSTALE(bp) && XFS_BUF_ISDELAYWRITE(bp))
478 cmn_err(CE_NOTE, "about to pop assert, bp == 0x%p", bp);
480 ASSERT((XFS_BUF_BFLAGS(bp) & (XFS_B_STALE|XFS_B_DELWRI)) !=
481 (XFS_B_STALE|XFS_B_DELWRI));
483 xfs_buftrace("READ_BUF XFSSHUTDN", bp);
486 return XFS_ERROR(EIO);
491 * Release the buffer bp which was previously acquired with one of the
492 * xfs_trans_... buffer allocation routines if the buffer has not
493 * been modified within this transaction. If the buffer is modified
494 * within this transaction, do decrement the recursion count but do
495 * not release the buffer even if the count goes to 0. If the buffer is not
496 * modified within the transaction, decrement the recursion count and
497 * release the buffer if the recursion count goes to 0.
499 * If the buffer is to be released and it was not modified before
500 * this transaction began, then free the buf_log_item associated with it.
502 * If the transaction pointer is NULL, make this just a normal
506 xfs_trans_brelse(xfs_trans_t *tp,
509 xfs_buf_log_item_t *bip;
511 xfs_log_item_desc_t *lidp;
514 * Default to a normal brelse() call if the tp is NULL.
517 ASSERT(XFS_BUF_FSPRIVATE2(bp, void *) == NULL);
519 * If there's a buf log item attached to the buffer,
520 * then let the AIL know that the buffer is being
523 if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
524 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
525 if (lip->li_type == XFS_LI_BUF) {
526 bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*);
527 xfs_trans_unlocked_item(bip->bli_item.li_ailp,
535 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
536 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
537 ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
538 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
539 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
540 ASSERT(atomic_read(&bip->bli_refcount) > 0);
543 * Find the item descriptor pointing to this buffer's
544 * log item. It must be there.
546 lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip);
547 ASSERT(lidp != NULL);
550 * If the release is just for a recursive lock,
551 * then decrement the count and return.
553 if (bip->bli_recur > 0) {
555 xfs_buf_item_trace("RELSE RECUR", bip);
560 * If the buffer is dirty within this transaction, we can't
561 * release it until we commit.
563 if (lidp->lid_flags & XFS_LID_DIRTY) {
564 xfs_buf_item_trace("RELSE DIRTY", bip);
569 * If the buffer has been invalidated, then we can't release
570 * it until the transaction commits to disk unless it is re-dirtied
571 * as part of this transaction. This prevents us from pulling
572 * the item from the AIL before we should.
574 if (bip->bli_flags & XFS_BLI_STALE) {
575 xfs_buf_item_trace("RELSE STALE", bip);
579 ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
580 xfs_buf_item_trace("RELSE", bip);
583 * Free up the log item descriptor tracking the released item.
585 xfs_trans_free_item(tp, lidp);
588 * Clear the hold flag in the buf log item if it is set.
589 * We wouldn't want the next user of the buffer to
592 if (bip->bli_flags & XFS_BLI_HOLD) {
593 bip->bli_flags &= ~XFS_BLI_HOLD;
597 * Drop our reference to the buf log item.
599 atomic_dec(&bip->bli_refcount);
602 * If the buf item is not tracking data in the log, then
603 * we must free it before releasing the buffer back to the
604 * free pool. Before releasing the buffer to the free pool,
605 * clear the transaction pointer in b_fsprivate2 to dissolve
606 * its relation to this transaction.
608 if (!xfs_buf_item_dirty(bip)) {
610 ASSERT(bp->b_pincount == 0);
612 ASSERT(atomic_read(&bip->bli_refcount) == 0);
613 ASSERT(!(bip->bli_item.li_flags & XFS_LI_IN_AIL));
614 ASSERT(!(bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF));
615 xfs_buf_item_relse(bp);
618 XFS_BUF_SET_FSPRIVATE2(bp, NULL);
621 * If we've still got a buf log item on the buffer, then
622 * tell the AIL that the buffer is being unlocked.
625 xfs_trans_unlocked_item(bip->bli_item.li_ailp,
626 (xfs_log_item_t*)bip);
634 * Add the locked buffer to the transaction.
635 * The buffer must be locked, and it cannot be associated with any
638 * If the buffer does not yet have a buf log item associated with it,
639 * then allocate one for it. Then add the buf item to the transaction.
642 xfs_trans_bjoin(xfs_trans_t *tp,
645 xfs_buf_log_item_t *bip;
647 ASSERT(XFS_BUF_ISBUSY(bp));
648 ASSERT(XFS_BUF_FSPRIVATE2(bp, void *) == NULL);
651 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
652 * it doesn't have one yet, then allocate one and initialize it.
653 * The checks to see if one is there are in xfs_buf_item_init().
655 xfs_buf_item_init(bp, tp->t_mountp);
656 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
657 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
658 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
659 ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
662 * Take a reference for this transaction on the buf item.
664 atomic_inc(&bip->bli_refcount);
667 * Get a log_item_desc to point at the new item.
669 (void) xfs_trans_add_item(tp, (xfs_log_item_t *)bip);
672 * Initialize b_fsprivate2 so we can find it with incore_match()
673 * in xfs_trans_get_buf() and friends above.
675 XFS_BUF_SET_FSPRIVATE2(bp, tp);
677 xfs_buf_item_trace("BJOIN", bip);
681 * Mark the buffer as not needing to be unlocked when the buf item's
682 * IOP_UNLOCK() routine is called. The buffer must already be locked
683 * and associated with the given transaction.
687 xfs_trans_bhold(xfs_trans_t *tp,
690 xfs_buf_log_item_t *bip;
692 ASSERT(XFS_BUF_ISBUSY(bp));
693 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
694 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
696 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
697 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
698 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
699 ASSERT(atomic_read(&bip->bli_refcount) > 0);
700 bip->bli_flags |= XFS_BLI_HOLD;
701 xfs_buf_item_trace("BHOLD", bip);
705 * Cancel the previous buffer hold request made on this buffer
706 * for this transaction.
709 xfs_trans_bhold_release(xfs_trans_t *tp,
712 xfs_buf_log_item_t *bip;
714 ASSERT(XFS_BUF_ISBUSY(bp));
715 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
716 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
718 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
719 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
720 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
721 ASSERT(atomic_read(&bip->bli_refcount) > 0);
722 ASSERT(bip->bli_flags & XFS_BLI_HOLD);
723 bip->bli_flags &= ~XFS_BLI_HOLD;
724 xfs_buf_item_trace("BHOLD RELEASE", bip);
728 * This is called to mark bytes first through last inclusive of the given
729 * buffer as needing to be logged when the transaction is committed.
730 * The buffer must already be associated with the given transaction.
732 * First and last are numbers relative to the beginning of this buffer,
733 * so the first byte in the buffer is numbered 0 regardless of the
737 xfs_trans_log_buf(xfs_trans_t *tp,
742 xfs_buf_log_item_t *bip;
743 xfs_log_item_desc_t *lidp;
745 ASSERT(XFS_BUF_ISBUSY(bp));
746 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
747 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
748 ASSERT((first <= last) && (last < XFS_BUF_COUNT(bp)));
749 ASSERT((XFS_BUF_IODONE_FUNC(bp) == NULL) ||
750 (XFS_BUF_IODONE_FUNC(bp) == xfs_buf_iodone_callbacks));
753 * Mark the buffer as needing to be written out eventually,
754 * and set its iodone function to remove the buffer's buf log
755 * item from the AIL and free it when the buffer is flushed
756 * to disk. See xfs_buf_attach_iodone() for more details
757 * on li_cb and xfs_buf_iodone_callbacks().
758 * If we end up aborting this transaction, we trap this buffer
759 * inside the b_bdstrat callback so that this won't get written to
762 XFS_BUF_DELAYWRITE(bp);
765 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
766 ASSERT(atomic_read(&bip->bli_refcount) > 0);
767 XFS_BUF_SET_IODONE_FUNC(bp, xfs_buf_iodone_callbacks);
768 bip->bli_item.li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*))xfs_buf_iodone;
771 * If we invalidated the buffer within this transaction, then
772 * cancel the invalidation now that we're dirtying the buffer
773 * again. There are no races with the code in xfs_buf_item_unpin(),
774 * because we have a reference to the buffer this entire time.
776 if (bip->bli_flags & XFS_BLI_STALE) {
777 xfs_buf_item_trace("BLOG UNSTALE", bip);
778 bip->bli_flags &= ~XFS_BLI_STALE;
779 ASSERT(XFS_BUF_ISSTALE(bp));
781 bip->bli_format.blf_flags &= ~XFS_BLI_CANCEL;
784 lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip);
785 ASSERT(lidp != NULL);
787 tp->t_flags |= XFS_TRANS_DIRTY;
788 lidp->lid_flags |= XFS_LID_DIRTY;
789 lidp->lid_flags &= ~XFS_LID_BUF_STALE;
790 bip->bli_flags |= XFS_BLI_LOGGED;
791 xfs_buf_item_log(bip, first, last);
792 xfs_buf_item_trace("BLOG", bip);
797 * This called to invalidate a buffer that is being used within
798 * a transaction. Typically this is because the blocks in the
799 * buffer are being freed, so we need to prevent it from being
800 * written out when we're done. Allowing it to be written again
801 * might overwrite data in the free blocks if they are reallocated
804 * We prevent the buffer from being written out by clearing the
805 * B_DELWRI flag. We can't always
806 * get rid of the buf log item at this point, though, because
807 * the buffer may still be pinned by another transaction. If that
808 * is the case, then we'll wait until the buffer is committed to
809 * disk for the last time (we can tell by the ref count) and
810 * free it in xfs_buf_item_unpin(). Until it is cleaned up we
811 * will keep the buffer locked so that the buffer and buf log item
819 xfs_log_item_desc_t *lidp;
820 xfs_buf_log_item_t *bip;
822 ASSERT(XFS_BUF_ISBUSY(bp));
823 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
824 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
826 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
827 lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip);
828 ASSERT(lidp != NULL);
829 ASSERT(atomic_read(&bip->bli_refcount) > 0);
831 if (bip->bli_flags & XFS_BLI_STALE) {
833 * If the buffer is already invalidated, then
836 ASSERT(!(XFS_BUF_ISDELAYWRITE(bp)));
837 ASSERT(XFS_BUF_ISSTALE(bp));
838 ASSERT(!(bip->bli_flags & (XFS_BLI_LOGGED | XFS_BLI_DIRTY)));
839 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_INODE_BUF));
840 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
841 ASSERT(lidp->lid_flags & XFS_LID_DIRTY);
842 ASSERT(tp->t_flags & XFS_TRANS_DIRTY);
843 xfs_buftrace("XFS_BINVAL RECUR", bp);
844 xfs_buf_item_trace("BINVAL RECUR", bip);
849 * Clear the dirty bit in the buffer and set the STALE flag
850 * in the buf log item. The STALE flag will be used in
851 * xfs_buf_item_unpin() to determine if it should clean up
852 * when the last reference to the buf item is given up.
853 * We set the XFS_BLI_CANCEL flag in the buf log format structure
854 * and log the buf item. This will be used at recovery time
855 * to determine that copies of the buffer in the log before
856 * this should not be replayed.
857 * We mark the item descriptor and the transaction dirty so
858 * that we'll hold the buffer until after the commit.
860 * Since we're invalidating the buffer, we also clear the state
861 * about which parts of the buffer have been logged. We also
862 * clear the flag indicating that this is an inode buffer since
863 * the data in the buffer will no longer be valid.
865 * We set the stale bit in the buffer as well since we're getting
868 XFS_BUF_UNDELAYWRITE(bp);
870 bip->bli_flags |= XFS_BLI_STALE;
871 bip->bli_flags &= ~(XFS_BLI_LOGGED | XFS_BLI_DIRTY);
872 bip->bli_format.blf_flags &= ~XFS_BLI_INODE_BUF;
873 bip->bli_format.blf_flags |= XFS_BLI_CANCEL;
874 memset((char *)(bip->bli_format.blf_data_map), 0,
875 (bip->bli_format.blf_map_size * sizeof(uint)));
876 lidp->lid_flags |= XFS_LID_DIRTY|XFS_LID_BUF_STALE;
877 tp->t_flags |= XFS_TRANS_DIRTY;
878 xfs_buftrace("XFS_BINVAL", bp);
879 xfs_buf_item_trace("BINVAL", bip);
883 * This call is used to indicate that the buffer contains on-disk
884 * inodes which must be handled specially during recovery. They
885 * require special handling because only the di_next_unlinked from
886 * the inodes in the buffer should be recovered. The rest of the
887 * data in the buffer is logged via the inodes themselves.
889 * All we do is set the XFS_BLI_INODE_BUF flag in the buffer's log
890 * format structure so that we'll know what to do at recovery time.
898 xfs_buf_log_item_t *bip;
900 ASSERT(XFS_BUF_ISBUSY(bp));
901 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
902 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
904 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
905 ASSERT(atomic_read(&bip->bli_refcount) > 0);
907 bip->bli_format.blf_flags |= XFS_BLI_INODE_BUF;
911 * This call is used to indicate that the buffer is going to
912 * be staled and was an inode buffer. This means it gets
913 * special processing during unpin - where any inodes
914 * associated with the buffer should be removed from ail.
915 * There is also special processing during recovery,
916 * any replay of the inodes in the buffer needs to be
917 * prevented as the buffer may have been reused.
920 xfs_trans_stale_inode_buf(
924 xfs_buf_log_item_t *bip;
926 ASSERT(XFS_BUF_ISBUSY(bp));
927 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
928 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
930 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
931 ASSERT(atomic_read(&bip->bli_refcount) > 0);
933 bip->bli_flags |= XFS_BLI_STALE_INODE;
934 bip->bli_item.li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*))
941 * Mark the buffer as being one which contains newly allocated
942 * inodes. We need to make sure that even if this buffer is
943 * relogged as an 'inode buf' we still recover all of the inode
944 * images in the face of a crash. This works in coordination with
945 * xfs_buf_item_committed() to ensure that the buffer remains in the
946 * AIL at its original location even after it has been relogged.
950 xfs_trans_inode_alloc_buf(
954 xfs_buf_log_item_t *bip;
956 ASSERT(XFS_BUF_ISBUSY(bp));
957 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
958 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
960 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
961 ASSERT(atomic_read(&bip->bli_refcount) > 0);
963 bip->bli_flags |= XFS_BLI_INODE_ALLOC_BUF;
968 * Similar to xfs_trans_inode_buf(), this marks the buffer as a cluster of
969 * dquots. However, unlike in inode buffer recovery, dquot buffers get
970 * recovered in their entirety. (Hence, no XFS_BLI_DQUOT_ALLOC_BUF flag).
971 * The only thing that makes dquot buffers different from regular
972 * buffers is that we must not replay dquot bufs when recovering
973 * if a _corresponding_ quotaoff has happened. We also have to distinguish
974 * between usr dquot bufs and grp dquot bufs, because usr and grp quotas
975 * can be turned off independently.
984 xfs_buf_log_item_t *bip;
986 ASSERT(XFS_BUF_ISBUSY(bp));
987 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
988 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
989 ASSERT(type == XFS_BLI_UDQUOT_BUF ||
990 type == XFS_BLI_PDQUOT_BUF ||
991 type == XFS_BLI_GDQUOT_BUF);
993 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
994 ASSERT(atomic_read(&bip->bli_refcount) > 0);
996 bip->bli_format.blf_flags |= type;
1000 * Check to see if a buffer matching the given parameters is already
1001 * a part of the given transaction. Only check the first, embedded
1002 * chunk, since we don't want to spend all day scanning large transactions.
1005 xfs_trans_buf_item_match(
1007 xfs_buftarg_t *target,
1011 xfs_log_item_chunk_t *licp;
1012 xfs_log_item_desc_t *lidp;
1013 xfs_buf_log_item_t *blip;
1019 licp = &tp->t_items;
1020 if (!xfs_lic_are_all_free(licp)) {
1021 for (i = 0; i < licp->lic_unused; i++) {
1023 * Skip unoccupied slots.
1025 if (xfs_lic_isfree(licp, i)) {
1029 lidp = xfs_lic_slot(licp, i);
1030 blip = (xfs_buf_log_item_t *)lidp->lid_item;
1031 if (blip->bli_item.li_type != XFS_LI_BUF) {
1036 if ((XFS_BUF_TARGET(bp) == target) &&
1037 (XFS_BUF_ADDR(bp) == blkno) &&
1038 (XFS_BUF_COUNT(bp) == len)) {
1040 * We found it. Break out and
1041 * return the pointer to the buffer.
1053 * Check to see if a buffer matching the given parameters is already
1054 * a part of the given transaction. Check all the chunks, we
1055 * want to be thorough.
1058 xfs_trans_buf_item_match_all(
1060 xfs_buftarg_t *target,
1064 xfs_log_item_chunk_t *licp;
1065 xfs_log_item_desc_t *lidp;
1066 xfs_buf_log_item_t *blip;
1072 for (licp = &tp->t_items; licp != NULL; licp = licp->lic_next) {
1073 if (xfs_lic_are_all_free(licp)) {
1074 ASSERT(licp == &tp->t_items);
1075 ASSERT(licp->lic_next == NULL);
1078 for (i = 0; i < licp->lic_unused; i++) {
1080 * Skip unoccupied slots.
1082 if (xfs_lic_isfree(licp, i)) {
1086 lidp = xfs_lic_slot(licp, i);
1087 blip = (xfs_buf_log_item_t *)lidp->lid_item;
1088 if (blip->bli_item.li_type != XFS_LI_BUF) {
1093 if ((XFS_BUF_TARGET(bp) == target) &&
1094 (XFS_BUF_ADDR(bp) == blkno) &&
1095 (XFS_BUF_COUNT(bp) == len)) {
1097 * We found it. Break out and
1098 * return the pointer to the buffer.