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_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_inode.h"
27 #include "xfs_trans.h"
28 #include "xfs_buf_item.h"
29 #include "xfs_trans_priv.h"
30 #include "xfs_error.h"
31 #include "xfs_trace.h"
34 * Check to see if a buffer matching the given parameters is already
35 * a part of the given transaction.
37 STATIC struct xfs_buf *
38 xfs_trans_buf_item_match(
40 struct xfs_buftarg *target,
41 struct xfs_buf_map *map,
44 struct xfs_log_item_desc *lidp;
45 struct xfs_buf_log_item *blip;
49 for (i = 0; i < nmaps; i++)
52 list_for_each_entry(lidp, &tp->t_items, lid_trans) {
53 blip = (struct xfs_buf_log_item *)lidp->lid_item;
54 if (blip->bli_item.li_type == XFS_LI_BUF &&
55 blip->bli_buf->b_target == target &&
56 XFS_BUF_ADDR(blip->bli_buf) == map[0].bm_bn &&
57 blip->bli_buf->b_length == len) {
58 ASSERT(blip->bli_buf->b_map_count == nmaps);
67 * Add the locked buffer to the transaction.
69 * The buffer must be locked, and it cannot be associated with any
72 * If the buffer does not yet have a buf log item associated with it,
73 * then allocate one for it. Then add the buf item to the transaction.
81 struct xfs_buf_log_item *bip;
83 ASSERT(bp->b_transp == NULL);
86 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
87 * it doesn't have one yet, then allocate one and initialize it.
88 * The checks to see if one is there are in xfs_buf_item_init().
90 xfs_buf_item_init(bp, tp->t_mountp);
92 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
93 ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
94 ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
99 * Take a reference for this transaction on the buf item.
101 atomic_inc(&bip->bli_refcount);
104 * Get a log_item_desc to point at the new item.
106 xfs_trans_add_item(tp, &bip->bli_item);
109 * Initialize b_fsprivate2 so we can find it with incore_match()
110 * in xfs_trans_get_buf() and friends above.
118 struct xfs_trans *tp,
121 _xfs_trans_bjoin(tp, bp, 0);
122 trace_xfs_trans_bjoin(bp->b_fspriv);
126 * Get and lock the buffer for the caller if it is not already
127 * locked within the given transaction. If it is already locked
128 * within the transaction, just increment its lock recursion count
129 * and return a pointer to it.
131 * If the transaction pointer is NULL, make this just a normal
135 xfs_trans_get_buf_map(
136 struct xfs_trans *tp,
137 struct xfs_buftarg *target,
138 struct xfs_buf_map *map,
140 xfs_buf_flags_t flags)
143 xfs_buf_log_item_t *bip;
146 return xfs_buf_get_map(target, map, nmaps, flags);
149 * If we find the buffer in the cache with this transaction
150 * pointer in its b_fsprivate2 field, then we know we already
151 * have it locked. In this case we just increment the lock
152 * recursion count and return the buffer to the caller.
154 bp = xfs_trans_buf_item_match(tp, target, map, nmaps);
156 ASSERT(xfs_buf_islocked(bp));
157 if (XFS_FORCED_SHUTDOWN(tp->t_mountp)) {
162 ASSERT(bp->b_transp == tp);
165 ASSERT(atomic_read(&bip->bli_refcount) > 0);
167 trace_xfs_trans_get_buf_recur(bip);
171 bp = xfs_buf_get_map(target, map, nmaps, flags);
176 ASSERT(!bp->b_error);
178 _xfs_trans_bjoin(tp, bp, 1);
179 trace_xfs_trans_get_buf(bp->b_fspriv);
184 * Get and lock the superblock buffer of this file system for the
187 * We don't need to use incore_match() here, because the superblock
188 * buffer is a private buffer which we keep a pointer to in the
192 xfs_trans_getsb(xfs_trans_t *tp,
193 struct xfs_mount *mp,
197 xfs_buf_log_item_t *bip;
200 * Default to just trying to lock the superblock buffer
204 return xfs_getsb(mp, flags);
207 * If the superblock buffer already has this transaction
208 * pointer in its b_fsprivate2 field, then we know we already
209 * have it locked. In this case we just increment the lock
210 * recursion count and return the buffer to the caller.
213 if (bp->b_transp == tp) {
216 ASSERT(atomic_read(&bip->bli_refcount) > 0);
218 trace_xfs_trans_getsb_recur(bip);
222 bp = xfs_getsb(mp, flags);
226 _xfs_trans_bjoin(tp, bp, 1);
227 trace_xfs_trans_getsb(bp->b_fspriv);
232 xfs_buftarg_t *xfs_error_target;
235 int xfs_error_mod = 33;
239 * Get and lock the buffer for the caller if it is not already
240 * locked within the given transaction. If it has not yet been
241 * read in, read it from disk. If it is already locked
242 * within the transaction and already read in, just increment its
243 * lock recursion count and return a pointer to it.
245 * If the transaction pointer is NULL, make this just a normal
249 xfs_trans_read_buf_map(
250 struct xfs_mount *mp,
251 struct xfs_trans *tp,
252 struct xfs_buftarg *target,
253 struct xfs_buf_map *map,
255 xfs_buf_flags_t flags,
256 struct xfs_buf **bpp,
257 const struct xfs_buf_ops *ops)
260 xfs_buf_log_item_t *bip;
265 bp = xfs_buf_read_map(target, map, nmaps, flags, ops);
267 return (flags & XBF_TRYLOCK) ?
272 xfs_buf_ioerror_alert(bp, __func__);
277 /* bad CRC means corrupted metadata */
278 if (error == -EFSBADCRC)
279 error = -EFSCORRUPTED;
284 if (xfs_error_target == target) {
285 if (((xfs_req_num++) % xfs_error_mod) == 0) {
287 xfs_debug(mp, "Returning error!");
293 if (XFS_FORCED_SHUTDOWN(mp))
300 * If we find the buffer in the cache with this transaction
301 * pointer in its b_fsprivate2 field, then we know we already
302 * have it locked. If it is already read in we just increment
303 * the lock recursion count and return the buffer to the caller.
304 * If the buffer is not yet read in, then we read it in, increment
305 * the lock recursion count, and return it to the caller.
307 bp = xfs_trans_buf_item_match(tp, target, map, nmaps);
309 ASSERT(xfs_buf_islocked(bp));
310 ASSERT(bp->b_transp == tp);
311 ASSERT(bp->b_fspriv != NULL);
312 ASSERT(!bp->b_error);
313 if (!(XFS_BUF_ISDONE(bp))) {
314 trace_xfs_trans_read_buf_io(bp, _RET_IP_);
315 ASSERT(!XFS_BUF_ISASYNC(bp));
316 ASSERT(bp->b_iodone == NULL);
320 error = xfs_buf_submit_wait(bp);
322 if (!XFS_FORCED_SHUTDOWN(mp))
323 xfs_buf_ioerror_alert(bp, __func__);
326 * We can gracefully recover from most read
327 * errors. Ones we can't are those that happen
328 * after the transaction's already dirty.
330 if (tp->t_flags & XFS_TRANS_DIRTY)
331 xfs_force_shutdown(tp->t_mountp,
332 SHUTDOWN_META_IO_ERROR);
333 /* bad CRC means corrupted metadata */
334 if (error == -EFSBADCRC)
335 error = -EFSCORRUPTED;
340 * We never locked this buf ourselves, so we shouldn't
341 * brelse it either. Just get out.
343 if (XFS_FORCED_SHUTDOWN(mp)) {
344 trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
353 ASSERT(atomic_read(&bip->bli_refcount) > 0);
354 trace_xfs_trans_read_buf_recur(bip);
359 bp = xfs_buf_read_map(target, map, nmaps, flags, ops);
362 return (flags & XBF_TRYLOCK) ?
369 xfs_buf_ioerror_alert(bp, __func__);
370 if (tp->t_flags & XFS_TRANS_DIRTY)
371 xfs_force_shutdown(tp->t_mountp, SHUTDOWN_META_IO_ERROR);
374 /* bad CRC means corrupted metadata */
375 if (error == -EFSBADCRC)
376 error = -EFSCORRUPTED;
380 if (xfs_do_error && !(tp->t_flags & XFS_TRANS_DIRTY)) {
381 if (xfs_error_target == target) {
382 if (((xfs_req_num++) % xfs_error_mod) == 0) {
383 xfs_force_shutdown(tp->t_mountp,
384 SHUTDOWN_META_IO_ERROR);
386 xfs_debug(mp, "Returning trans error!");
392 if (XFS_FORCED_SHUTDOWN(mp))
395 _xfs_trans_bjoin(tp, bp, 1);
396 trace_xfs_trans_read_buf(bp->b_fspriv);
402 trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
409 * Release the buffer bp which was previously acquired with one of the
410 * xfs_trans_... buffer allocation routines if the buffer has not
411 * been modified within this transaction. If the buffer is modified
412 * within this transaction, do decrement the recursion count but do
413 * not release the buffer even if the count goes to 0. If the buffer is not
414 * modified within the transaction, decrement the recursion count and
415 * release the buffer if the recursion count goes to 0.
417 * If the buffer is to be released and it was not modified before
418 * this transaction began, then free the buf_log_item associated with it.
420 * If the transaction pointer is NULL, make this just a normal
424 xfs_trans_brelse(xfs_trans_t *tp,
427 xfs_buf_log_item_t *bip;
430 * Default to a normal brelse() call if the tp is NULL.
433 ASSERT(bp->b_transp == NULL);
438 ASSERT(bp->b_transp == tp);
440 ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
441 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
442 ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
443 ASSERT(atomic_read(&bip->bli_refcount) > 0);
445 trace_xfs_trans_brelse(bip);
448 * If the release is just for a recursive lock,
449 * then decrement the count and return.
451 if (bip->bli_recur > 0) {
457 * If the buffer is dirty within this transaction, we can't
458 * release it until we commit.
460 if (bip->bli_item.li_desc->lid_flags & XFS_LID_DIRTY)
464 * If the buffer has been invalidated, then we can't release
465 * it until the transaction commits to disk unless it is re-dirtied
466 * as part of this transaction. This prevents us from pulling
467 * the item from the AIL before we should.
469 if (bip->bli_flags & XFS_BLI_STALE)
472 ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
475 * Free up the log item descriptor tracking the released item.
477 xfs_trans_del_item(&bip->bli_item);
480 * Clear the hold flag in the buf log item if it is set.
481 * We wouldn't want the next user of the buffer to
484 if (bip->bli_flags & XFS_BLI_HOLD) {
485 bip->bli_flags &= ~XFS_BLI_HOLD;
489 * Drop our reference to the buf log item.
491 atomic_dec(&bip->bli_refcount);
494 * If the buf item is not tracking data in the log, then
495 * we must free it before releasing the buffer back to the
496 * free pool. Before releasing the buffer to the free pool,
497 * clear the transaction pointer in b_fsprivate2 to dissolve
498 * its relation to this transaction.
500 if (!xfs_buf_item_dirty(bip)) {
502 ASSERT(bp->b_pincount == 0);
504 ASSERT(atomic_read(&bip->bli_refcount) == 0);
505 ASSERT(!(bip->bli_item.li_flags & XFS_LI_IN_AIL));
506 ASSERT(!(bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF));
507 xfs_buf_item_relse(bp);
515 * Mark the buffer as not needing to be unlocked when the buf item's
516 * iop_unlock() routine is called. The buffer must already be locked
517 * and associated with the given transaction.
521 xfs_trans_bhold(xfs_trans_t *tp,
524 xfs_buf_log_item_t *bip = bp->b_fspriv;
526 ASSERT(bp->b_transp == tp);
528 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
529 ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
530 ASSERT(atomic_read(&bip->bli_refcount) > 0);
532 bip->bli_flags |= XFS_BLI_HOLD;
533 trace_xfs_trans_bhold(bip);
537 * Cancel the previous buffer hold request made on this buffer
538 * for this transaction.
541 xfs_trans_bhold_release(xfs_trans_t *tp,
544 xfs_buf_log_item_t *bip = bp->b_fspriv;
546 ASSERT(bp->b_transp == tp);
548 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
549 ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
550 ASSERT(atomic_read(&bip->bli_refcount) > 0);
551 ASSERT(bip->bli_flags & XFS_BLI_HOLD);
553 bip->bli_flags &= ~XFS_BLI_HOLD;
554 trace_xfs_trans_bhold_release(bip);
558 * This is called to mark bytes first through last inclusive of the given
559 * buffer as needing to be logged when the transaction is committed.
560 * The buffer must already be associated with the given transaction.
562 * First and last are numbers relative to the beginning of this buffer,
563 * so the first byte in the buffer is numbered 0 regardless of the
567 xfs_trans_log_buf(xfs_trans_t *tp,
572 xfs_buf_log_item_t *bip = bp->b_fspriv;
574 ASSERT(bp->b_transp == tp);
576 ASSERT(first <= last && last < BBTOB(bp->b_length));
577 ASSERT(bp->b_iodone == NULL ||
578 bp->b_iodone == xfs_buf_iodone_callbacks);
581 * Mark the buffer as needing to be written out eventually,
582 * and set its iodone function to remove the buffer's buf log
583 * item from the AIL and free it when the buffer is flushed
584 * to disk. See xfs_buf_attach_iodone() for more details
585 * on li_cb and xfs_buf_iodone_callbacks().
586 * If we end up aborting this transaction, we trap this buffer
587 * inside the b_bdstrat callback so that this won't get written to
592 ASSERT(atomic_read(&bip->bli_refcount) > 0);
593 bp->b_iodone = xfs_buf_iodone_callbacks;
594 bip->bli_item.li_cb = xfs_buf_iodone;
596 trace_xfs_trans_log_buf(bip);
599 * If we invalidated the buffer within this transaction, then
600 * cancel the invalidation now that we're dirtying the buffer
601 * again. There are no races with the code in xfs_buf_item_unpin(),
602 * because we have a reference to the buffer this entire time.
604 if (bip->bli_flags & XFS_BLI_STALE) {
605 bip->bli_flags &= ~XFS_BLI_STALE;
606 ASSERT(XFS_BUF_ISSTALE(bp));
608 bip->__bli_format.blf_flags &= ~XFS_BLF_CANCEL;
611 tp->t_flags |= XFS_TRANS_DIRTY;
612 bip->bli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
615 * If we have an ordered buffer we are not logging any dirty range but
616 * it still needs to be marked dirty and that it has been logged.
618 bip->bli_flags |= XFS_BLI_DIRTY | XFS_BLI_LOGGED;
619 if (!(bip->bli_flags & XFS_BLI_ORDERED))
620 xfs_buf_item_log(bip, first, last);
625 * Invalidate a buffer that is being used within a transaction.
627 * Typically this is because the blocks in the buffer are being freed, so we
628 * need to prevent it from being written out when we're done. Allowing it
629 * to be written again might overwrite data in the free blocks if they are
630 * reallocated to a file.
632 * We prevent the buffer from being written out by marking it stale. We can't
633 * get rid of the buf log item at this point because the buffer may still be
634 * pinned by another transaction. If that is the case, then we'll wait until
635 * the buffer is committed to disk for the last time (we can tell by the ref
636 * count) and free it in xfs_buf_item_unpin(). Until that happens we will
637 * keep the buffer locked so that the buffer and buf log item are not reused.
639 * We also set the XFS_BLF_CANCEL flag in the buf log format structure and log
640 * the buf item. This will be used at recovery time to determine that copies
641 * of the buffer in the log before this should not be replayed.
643 * We mark the item descriptor and the transaction dirty so that we'll hold
644 * the buffer until after the commit.
646 * Since we're invalidating the buffer, we also clear the state about which
647 * parts of the buffer have been logged. We also clear the flag indicating
648 * that this is an inode buffer since the data in the buffer will no longer
651 * We set the stale bit in the buffer as well since we're getting rid of it.
658 xfs_buf_log_item_t *bip = bp->b_fspriv;
661 ASSERT(bp->b_transp == tp);
663 ASSERT(atomic_read(&bip->bli_refcount) > 0);
665 trace_xfs_trans_binval(bip);
667 if (bip->bli_flags & XFS_BLI_STALE) {
669 * If the buffer is already invalidated, then
672 ASSERT(XFS_BUF_ISSTALE(bp));
673 ASSERT(!(bip->bli_flags & (XFS_BLI_LOGGED | XFS_BLI_DIRTY)));
674 ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_INODE_BUF));
675 ASSERT(!(bip->__bli_format.blf_flags & XFS_BLFT_MASK));
676 ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
677 ASSERT(bip->bli_item.li_desc->lid_flags & XFS_LID_DIRTY);
678 ASSERT(tp->t_flags & XFS_TRANS_DIRTY);
684 bip->bli_flags |= XFS_BLI_STALE;
685 bip->bli_flags &= ~(XFS_BLI_INODE_BUF | XFS_BLI_LOGGED | XFS_BLI_DIRTY);
686 bip->__bli_format.blf_flags &= ~XFS_BLF_INODE_BUF;
687 bip->__bli_format.blf_flags |= XFS_BLF_CANCEL;
688 bip->__bli_format.blf_flags &= ~XFS_BLFT_MASK;
689 for (i = 0; i < bip->bli_format_count; i++) {
690 memset(bip->bli_formats[i].blf_data_map, 0,
691 (bip->bli_formats[i].blf_map_size * sizeof(uint)));
693 bip->bli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
694 tp->t_flags |= XFS_TRANS_DIRTY;
698 * This call is used to indicate that the buffer contains on-disk inodes which
699 * must be handled specially during recovery. They require special handling
700 * because only the di_next_unlinked from the inodes in the buffer should be
701 * recovered. The rest of the data in the buffer is logged via the inodes
704 * All we do is set the XFS_BLI_INODE_BUF flag in the items flags so it can be
705 * transferred to the buffer's log format structure so that we'll know what to
706 * do at recovery time.
713 xfs_buf_log_item_t *bip = bp->b_fspriv;
715 ASSERT(bp->b_transp == tp);
717 ASSERT(atomic_read(&bip->bli_refcount) > 0);
719 bip->bli_flags |= XFS_BLI_INODE_BUF;
720 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
724 * This call is used to indicate that the buffer is going to
725 * be staled and was an inode buffer. This means it gets
726 * special processing during unpin - where any inodes
727 * associated with the buffer should be removed from ail.
728 * There is also special processing during recovery,
729 * any replay of the inodes in the buffer needs to be
730 * prevented as the buffer may have been reused.
733 xfs_trans_stale_inode_buf(
737 xfs_buf_log_item_t *bip = bp->b_fspriv;
739 ASSERT(bp->b_transp == tp);
741 ASSERT(atomic_read(&bip->bli_refcount) > 0);
743 bip->bli_flags |= XFS_BLI_STALE_INODE;
744 bip->bli_item.li_cb = xfs_buf_iodone;
745 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
749 * Mark the buffer as being one which contains newly allocated
750 * inodes. We need to make sure that even if this buffer is
751 * relogged as an 'inode buf' we still recover all of the inode
752 * images in the face of a crash. This works in coordination with
753 * xfs_buf_item_committed() to ensure that the buffer remains in the
754 * AIL at its original location even after it has been relogged.
758 xfs_trans_inode_alloc_buf(
762 xfs_buf_log_item_t *bip = bp->b_fspriv;
764 ASSERT(bp->b_transp == tp);
766 ASSERT(atomic_read(&bip->bli_refcount) > 0);
768 bip->bli_flags |= XFS_BLI_INODE_ALLOC_BUF;
769 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
773 * Mark the buffer as ordered for this transaction. This means
774 * that the contents of the buffer are not recorded in the transaction
775 * but it is tracked in the AIL as though it was. This allows us
776 * to record logical changes in transactions rather than the physical
777 * changes we make to the buffer without changing writeback ordering
778 * constraints of metadata buffers.
781 xfs_trans_ordered_buf(
782 struct xfs_trans *tp,
785 struct xfs_buf_log_item *bip = bp->b_fspriv;
787 ASSERT(bp->b_transp == tp);
789 ASSERT(atomic_read(&bip->bli_refcount) > 0);
791 bip->bli_flags |= XFS_BLI_ORDERED;
792 trace_xfs_buf_item_ordered(bip);
796 * Set the type of the buffer for log recovery so that it can correctly identify
797 * and hence attach the correct buffer ops to the buffer after replay.
800 xfs_trans_buf_set_type(
801 struct xfs_trans *tp,
805 struct xfs_buf_log_item *bip = bp->b_fspriv;
810 ASSERT(bp->b_transp == tp);
812 ASSERT(atomic_read(&bip->bli_refcount) > 0);
814 xfs_blft_to_flags(&bip->__bli_format, type);
818 xfs_trans_buf_copy_type(
819 struct xfs_buf *dst_bp,
820 struct xfs_buf *src_bp)
822 struct xfs_buf_log_item *sbip = src_bp->b_fspriv;
823 struct xfs_buf_log_item *dbip = dst_bp->b_fspriv;
826 type = xfs_blft_from_flags(&sbip->__bli_format);
827 xfs_blft_to_flags(&dbip->__bli_format, type);
831 * Similar to xfs_trans_inode_buf(), this marks the buffer as a cluster of
832 * dquots. However, unlike in inode buffer recovery, dquot buffers get
833 * recovered in their entirety. (Hence, no XFS_BLI_DQUOT_ALLOC_BUF flag).
834 * The only thing that makes dquot buffers different from regular
835 * buffers is that we must not replay dquot bufs when recovering
836 * if a _corresponding_ quotaoff has happened. We also have to distinguish
837 * between usr dquot bufs and grp dquot bufs, because usr and grp quotas
838 * can be turned off independently.
847 struct xfs_buf_log_item *bip = bp->b_fspriv;
849 ASSERT(type == XFS_BLF_UDQUOT_BUF ||
850 type == XFS_BLF_PDQUOT_BUF ||
851 type == XFS_BLF_GDQUOT_BUF);
853 bip->__bli_format.blf_flags |= type;
856 case XFS_BLF_UDQUOT_BUF:
857 type = XFS_BLFT_UDQUOT_BUF;
859 case XFS_BLF_PDQUOT_BUF:
860 type = XFS_BLFT_PDQUOT_BUF;
862 case XFS_BLF_GDQUOT_BUF:
863 type = XFS_BLFT_GDQUOT_BUF;
866 type = XFS_BLFT_UNKNOWN_BUF;
870 xfs_trans_buf_set_type(tp, bp, type);