xref: /linux/fs/xfs/xfs_trans_buf.c (revision 783a2f656f9674c31d4019708a94af93fa1d1c22)
11da177e4SLinus Torvalds /*
27b718769SNathan Scott  * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
37b718769SNathan Scott  * All Rights Reserved.
41da177e4SLinus Torvalds  *
57b718769SNathan Scott  * This program is free software; you can redistribute it and/or
67b718769SNathan Scott  * modify it under the terms of the GNU General Public License as
71da177e4SLinus Torvalds  * published by the Free Software Foundation.
81da177e4SLinus Torvalds  *
97b718769SNathan Scott  * This program is distributed in the hope that it would be useful,
107b718769SNathan Scott  * but WITHOUT ANY WARRANTY; without even the implied warranty of
117b718769SNathan Scott  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
127b718769SNathan Scott  * GNU General Public License for more details.
131da177e4SLinus Torvalds  *
147b718769SNathan Scott  * You should have received a copy of the GNU General Public License
157b718769SNathan Scott  * along with this program; if not, write the Free Software Foundation,
167b718769SNathan Scott  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds #include "xfs.h"
19a844f451SNathan Scott #include "xfs_fs.h"
201da177e4SLinus Torvalds #include "xfs_types.h"
21a844f451SNathan Scott #include "xfs_bit.h"
221da177e4SLinus Torvalds #include "xfs_log.h"
23a844f451SNathan Scott #include "xfs_inum.h"
241da177e4SLinus Torvalds #include "xfs_trans.h"
251da177e4SLinus Torvalds #include "xfs_sb.h"
261da177e4SLinus Torvalds #include "xfs_ag.h"
27a844f451SNathan Scott #include "xfs_dir2.h"
281da177e4SLinus Torvalds #include "xfs_dmapi.h"
291da177e4SLinus Torvalds #include "xfs_mount.h"
30a844f451SNathan Scott #include "xfs_bmap_btree.h"
31a844f451SNathan Scott #include "xfs_alloc_btree.h"
32a844f451SNathan Scott #include "xfs_ialloc_btree.h"
33a844f451SNathan Scott #include "xfs_dir2_sf.h"
34a844f451SNathan Scott #include "xfs_attr_sf.h"
35a844f451SNathan Scott #include "xfs_dinode.h"
36a844f451SNathan Scott #include "xfs_inode.h"
37a844f451SNathan Scott #include "xfs_buf_item.h"
381da177e4SLinus Torvalds #include "xfs_trans_priv.h"
391da177e4SLinus Torvalds #include "xfs_error.h"
401da177e4SLinus Torvalds #include "xfs_rw.h"
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds STATIC xfs_buf_t *xfs_trans_buf_item_match(xfs_trans_t *, xfs_buftarg_t *,
441da177e4SLinus Torvalds 		xfs_daddr_t, int);
451da177e4SLinus Torvalds STATIC xfs_buf_t *xfs_trans_buf_item_match_all(xfs_trans_t *, xfs_buftarg_t *,
461da177e4SLinus Torvalds 		xfs_daddr_t, int);
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds /*
501da177e4SLinus Torvalds  * Get and lock the buffer for the caller if it is not already
511da177e4SLinus Torvalds  * locked within the given transaction.  If it is already locked
521da177e4SLinus Torvalds  * within the transaction, just increment its lock recursion count
531da177e4SLinus Torvalds  * and return a pointer to it.
541da177e4SLinus Torvalds  *
551da177e4SLinus Torvalds  * Use the fast path function xfs_trans_buf_item_match() or the buffer
561da177e4SLinus Torvalds  * cache routine incore_match() to find the buffer
571da177e4SLinus Torvalds  * if it is already owned by this transaction.
581da177e4SLinus Torvalds  *
591da177e4SLinus Torvalds  * If we don't already own the buffer, use get_buf() to get it.
601da177e4SLinus Torvalds  * If it doesn't yet have an associated xfs_buf_log_item structure,
611da177e4SLinus Torvalds  * then allocate one and add the item to this transaction.
621da177e4SLinus Torvalds  *
631da177e4SLinus Torvalds  * If the transaction pointer is NULL, make this just a normal
641da177e4SLinus Torvalds  * get_buf() call.
651da177e4SLinus Torvalds  */
661da177e4SLinus Torvalds xfs_buf_t *
671da177e4SLinus Torvalds xfs_trans_get_buf(xfs_trans_t	*tp,
681da177e4SLinus Torvalds 		  xfs_buftarg_t	*target_dev,
691da177e4SLinus Torvalds 		  xfs_daddr_t	blkno,
701da177e4SLinus Torvalds 		  int		len,
711da177e4SLinus Torvalds 		  uint		flags)
721da177e4SLinus Torvalds {
731da177e4SLinus Torvalds 	xfs_buf_t		*bp;
741da177e4SLinus Torvalds 	xfs_buf_log_item_t	*bip;
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 	if (flags == 0)
771da177e4SLinus Torvalds 		flags = XFS_BUF_LOCK | XFS_BUF_MAPPED;
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	/*
801da177e4SLinus Torvalds 	 * Default to a normal get_buf() call if the tp is NULL.
811da177e4SLinus Torvalds 	 */
821da177e4SLinus Torvalds 	if (tp == NULL) {
831da177e4SLinus Torvalds 		bp = xfs_buf_get_flags(target_dev, blkno, len,
841da177e4SLinus Torvalds 							flags | BUF_BUSY);
851da177e4SLinus Torvalds 		return(bp);
861da177e4SLinus Torvalds 	}
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	/*
891da177e4SLinus Torvalds 	 * If we find the buffer in the cache with this transaction
901da177e4SLinus Torvalds 	 * pointer in its b_fsprivate2 field, then we know we already
911da177e4SLinus Torvalds 	 * have it locked.  In this case we just increment the lock
921da177e4SLinus Torvalds 	 * recursion count and return the buffer to the caller.
931da177e4SLinus Torvalds 	 */
941da177e4SLinus Torvalds 	if (tp->t_items.lic_next == NULL) {
951da177e4SLinus Torvalds 		bp = xfs_trans_buf_item_match(tp, target_dev, blkno, len);
961da177e4SLinus Torvalds 	} else {
971da177e4SLinus Torvalds 		bp  = xfs_trans_buf_item_match_all(tp, target_dev, blkno, len);
981da177e4SLinus Torvalds 	}
991da177e4SLinus Torvalds 	if (bp != NULL) {
1001da177e4SLinus Torvalds 		ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1011da177e4SLinus Torvalds 		if (XFS_FORCED_SHUTDOWN(tp->t_mountp)) {
1021da177e4SLinus Torvalds 			xfs_buftrace("TRANS GET RECUR SHUT", bp);
1031da177e4SLinus Torvalds 			XFS_BUF_SUPER_STALE(bp);
1041da177e4SLinus Torvalds 		}
1051da177e4SLinus Torvalds 		/*
1061da177e4SLinus Torvalds 		 * If the buffer is stale then it was binval'ed
1071da177e4SLinus Torvalds 		 * since last read.  This doesn't matter since the
1081da177e4SLinus Torvalds 		 * caller isn't allowed to use the data anyway.
1091da177e4SLinus Torvalds 		 */
1101da177e4SLinus Torvalds 		else if (XFS_BUF_ISSTALE(bp)) {
1111da177e4SLinus Torvalds 			xfs_buftrace("TRANS GET RECUR STALE", bp);
1121da177e4SLinus Torvalds 			ASSERT(!XFS_BUF_ISDELAYWRITE(bp));
1131da177e4SLinus Torvalds 		}
1141da177e4SLinus Torvalds 		ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
1151da177e4SLinus Torvalds 		bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
1161da177e4SLinus Torvalds 		ASSERT(bip != NULL);
1171da177e4SLinus Torvalds 		ASSERT(atomic_read(&bip->bli_refcount) > 0);
1181da177e4SLinus Torvalds 		bip->bli_recur++;
1191da177e4SLinus Torvalds 		xfs_buftrace("TRANS GET RECUR", bp);
1201da177e4SLinus Torvalds 		xfs_buf_item_trace("GET RECUR", bip);
1211da177e4SLinus Torvalds 		return (bp);
1221da177e4SLinus Torvalds 	}
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	/*
1251da177e4SLinus Torvalds 	 * We always specify the BUF_BUSY flag within a transaction so
1261da177e4SLinus Torvalds 	 * that get_buf does not try to push out a delayed write buffer
1271da177e4SLinus Torvalds 	 * which might cause another transaction to take place (if the
1281da177e4SLinus Torvalds 	 * buffer was delayed alloc).  Such recursive transactions can
1291da177e4SLinus Torvalds 	 * easily deadlock with our current transaction as well as cause
1301da177e4SLinus Torvalds 	 * us to run out of stack space.
1311da177e4SLinus Torvalds 	 */
1321da177e4SLinus Torvalds 	bp = xfs_buf_get_flags(target_dev, blkno, len, flags | BUF_BUSY);
1331da177e4SLinus Torvalds 	if (bp == NULL) {
1341da177e4SLinus Torvalds 		return NULL;
1351da177e4SLinus Torvalds 	}
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	ASSERT(!XFS_BUF_GETERROR(bp));
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	/*
1401da177e4SLinus Torvalds 	 * The xfs_buf_log_item pointer is stored in b_fsprivate.  If
1411da177e4SLinus Torvalds 	 * it doesn't have one yet, then allocate one and initialize it.
1421da177e4SLinus Torvalds 	 * The checks to see if one is there are in xfs_buf_item_init().
1431da177e4SLinus Torvalds 	 */
1441da177e4SLinus Torvalds 	xfs_buf_item_init(bp, tp->t_mountp);
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	/*
1471da177e4SLinus Torvalds 	 * Set the recursion count for the buffer within this transaction
1481da177e4SLinus Torvalds 	 * to 0.
1491da177e4SLinus Torvalds 	 */
1501da177e4SLinus Torvalds 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
1511da177e4SLinus Torvalds 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
1521da177e4SLinus Torvalds 	ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
1531da177e4SLinus Torvalds 	ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
1541da177e4SLinus Torvalds 	bip->bli_recur = 0;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	/*
1571da177e4SLinus Torvalds 	 * Take a reference for this transaction on the buf item.
1581da177e4SLinus Torvalds 	 */
1591da177e4SLinus Torvalds 	atomic_inc(&bip->bli_refcount);
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	/*
1621da177e4SLinus Torvalds 	 * Get a log_item_desc to point at the new item.
1631da177e4SLinus Torvalds 	 */
1641da177e4SLinus Torvalds 	(void) xfs_trans_add_item(tp, (xfs_log_item_t*)bip);
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	/*
1671da177e4SLinus Torvalds 	 * Initialize b_fsprivate2 so we can find it with incore_match()
1681da177e4SLinus Torvalds 	 * above.
1691da177e4SLinus Torvalds 	 */
1701da177e4SLinus Torvalds 	XFS_BUF_SET_FSPRIVATE2(bp, tp);
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	xfs_buftrace("TRANS GET", bp);
1731da177e4SLinus Torvalds 	xfs_buf_item_trace("GET", bip);
1741da177e4SLinus Torvalds 	return (bp);
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds /*
1781da177e4SLinus Torvalds  * Get and lock the superblock buffer of this file system for the
1791da177e4SLinus Torvalds  * given transaction.
1801da177e4SLinus Torvalds  *
1811da177e4SLinus Torvalds  * We don't need to use incore_match() here, because the superblock
1821da177e4SLinus Torvalds  * buffer is a private buffer which we keep a pointer to in the
1831da177e4SLinus Torvalds  * mount structure.
1841da177e4SLinus Torvalds  */
1851da177e4SLinus Torvalds xfs_buf_t *
1861da177e4SLinus Torvalds xfs_trans_getsb(xfs_trans_t	*tp,
1871da177e4SLinus Torvalds 		struct xfs_mount *mp,
1881da177e4SLinus Torvalds 		int		flags)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds 	xfs_buf_t		*bp;
1911da177e4SLinus Torvalds 	xfs_buf_log_item_t	*bip;
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	/*
1941da177e4SLinus Torvalds 	 * Default to just trying to lock the superblock buffer
1951da177e4SLinus Torvalds 	 * if tp is NULL.
1961da177e4SLinus Torvalds 	 */
1971da177e4SLinus Torvalds 	if (tp == NULL) {
1981da177e4SLinus Torvalds 		return (xfs_getsb(mp, flags));
1991da177e4SLinus Torvalds 	}
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	/*
2021da177e4SLinus Torvalds 	 * If the superblock buffer already has this transaction
2031da177e4SLinus Torvalds 	 * pointer in its b_fsprivate2 field, then we know we already
2041da177e4SLinus Torvalds 	 * have it locked.  In this case we just increment the lock
2051da177e4SLinus Torvalds 	 * recursion count and return the buffer to the caller.
2061da177e4SLinus Torvalds 	 */
2071da177e4SLinus Torvalds 	bp = mp->m_sb_bp;
2081da177e4SLinus Torvalds 	if (XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp) {
2091da177e4SLinus Torvalds 		bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
2101da177e4SLinus Torvalds 		ASSERT(bip != NULL);
2111da177e4SLinus Torvalds 		ASSERT(atomic_read(&bip->bli_refcount) > 0);
2121da177e4SLinus Torvalds 		bip->bli_recur++;
2131da177e4SLinus Torvalds 		xfs_buf_item_trace("GETSB RECUR", bip);
2141da177e4SLinus Torvalds 		return (bp);
2151da177e4SLinus Torvalds 	}
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	bp = xfs_getsb(mp, flags);
2181da177e4SLinus Torvalds 	if (bp == NULL) {
2191da177e4SLinus Torvalds 		return NULL;
2201da177e4SLinus Torvalds 	}
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	/*
2231da177e4SLinus Torvalds 	 * The xfs_buf_log_item pointer is stored in b_fsprivate.  If
2241da177e4SLinus Torvalds 	 * it doesn't have one yet, then allocate one and initialize it.
2251da177e4SLinus Torvalds 	 * The checks to see if one is there are in xfs_buf_item_init().
2261da177e4SLinus Torvalds 	 */
2271da177e4SLinus Torvalds 	xfs_buf_item_init(bp, mp);
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	/*
2301da177e4SLinus Torvalds 	 * Set the recursion count for the buffer within this transaction
2311da177e4SLinus Torvalds 	 * to 0.
2321da177e4SLinus Torvalds 	 */
2331da177e4SLinus Torvalds 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
2341da177e4SLinus Torvalds 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
2351da177e4SLinus Torvalds 	ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
2361da177e4SLinus Torvalds 	ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
2371da177e4SLinus Torvalds 	bip->bli_recur = 0;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	/*
2401da177e4SLinus Torvalds 	 * Take a reference for this transaction on the buf item.
2411da177e4SLinus Torvalds 	 */
2421da177e4SLinus Torvalds 	atomic_inc(&bip->bli_refcount);
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	/*
2451da177e4SLinus Torvalds 	 * Get a log_item_desc to point at the new item.
2461da177e4SLinus Torvalds 	 */
2471da177e4SLinus Torvalds 	(void) xfs_trans_add_item(tp, (xfs_log_item_t*)bip);
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	/*
2501da177e4SLinus Torvalds 	 * Initialize b_fsprivate2 so we can find it with incore_match()
2511da177e4SLinus Torvalds 	 * above.
2521da177e4SLinus Torvalds 	 */
2531da177e4SLinus Torvalds 	XFS_BUF_SET_FSPRIVATE2(bp, tp);
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	xfs_buf_item_trace("GETSB", bip);
2561da177e4SLinus Torvalds 	return (bp);
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds #ifdef DEBUG
2601da177e4SLinus Torvalds xfs_buftarg_t *xfs_error_target;
2611da177e4SLinus Torvalds int	xfs_do_error;
2621da177e4SLinus Torvalds int	xfs_req_num;
2631da177e4SLinus Torvalds int	xfs_error_mod = 33;
2641da177e4SLinus Torvalds #endif
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds /*
2671da177e4SLinus Torvalds  * Get and lock the buffer for the caller if it is not already
2681da177e4SLinus Torvalds  * locked within the given transaction.  If it has not yet been
2691da177e4SLinus Torvalds  * read in, read it from disk. If it is already locked
2701da177e4SLinus Torvalds  * within the transaction and already read in, just increment its
2711da177e4SLinus Torvalds  * lock recursion count and return a pointer to it.
2721da177e4SLinus Torvalds  *
2731da177e4SLinus Torvalds  * Use the fast path function xfs_trans_buf_item_match() or the buffer
2741da177e4SLinus Torvalds  * cache routine incore_match() to find the buffer
2751da177e4SLinus Torvalds  * if it is already owned by this transaction.
2761da177e4SLinus Torvalds  *
2771da177e4SLinus Torvalds  * If we don't already own the buffer, use read_buf() to get it.
2781da177e4SLinus Torvalds  * If it doesn't yet have an associated xfs_buf_log_item structure,
2791da177e4SLinus Torvalds  * then allocate one and add the item to this transaction.
2801da177e4SLinus Torvalds  *
2811da177e4SLinus Torvalds  * If the transaction pointer is NULL, make this just a normal
2821da177e4SLinus Torvalds  * read_buf() call.
2831da177e4SLinus Torvalds  */
2841da177e4SLinus Torvalds int
2851da177e4SLinus Torvalds xfs_trans_read_buf(
2861da177e4SLinus Torvalds 	xfs_mount_t	*mp,
2871da177e4SLinus Torvalds 	xfs_trans_t	*tp,
2881da177e4SLinus Torvalds 	xfs_buftarg_t	*target,
2891da177e4SLinus Torvalds 	xfs_daddr_t	blkno,
2901da177e4SLinus Torvalds 	int		len,
2911da177e4SLinus Torvalds 	uint		flags,
2921da177e4SLinus Torvalds 	xfs_buf_t	**bpp)
2931da177e4SLinus Torvalds {
2941da177e4SLinus Torvalds 	xfs_buf_t		*bp;
2951da177e4SLinus Torvalds 	xfs_buf_log_item_t	*bip;
2961da177e4SLinus Torvalds 	int			error;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	if (flags == 0)
2991da177e4SLinus Torvalds 		flags = XFS_BUF_LOCK | XFS_BUF_MAPPED;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	/*
3021da177e4SLinus Torvalds 	 * Default to a normal get_buf() call if the tp is NULL.
3031da177e4SLinus Torvalds 	 */
3041da177e4SLinus Torvalds 	if (tp == NULL) {
3051da177e4SLinus Torvalds 		bp = xfs_buf_read_flags(target, blkno, len, flags | BUF_BUSY);
3061da177e4SLinus Torvalds 		if (!bp)
307a3f74ffbSDavid Chinner 			return (flags & XFS_BUF_TRYLOCK) ?
308a3f74ffbSDavid Chinner 					EAGAIN : XFS_ERROR(ENOMEM);
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 		if ((bp != NULL) && (XFS_BUF_GETERROR(bp) != 0)) {
3111da177e4SLinus Torvalds 			xfs_ioerror_alert("xfs_trans_read_buf", mp,
3121da177e4SLinus Torvalds 					  bp, blkno);
3131da177e4SLinus Torvalds 			error = XFS_BUF_GETERROR(bp);
3141da177e4SLinus Torvalds 			xfs_buf_relse(bp);
3151da177e4SLinus Torvalds 			return error;
3161da177e4SLinus Torvalds 		}
3171da177e4SLinus Torvalds #ifdef DEBUG
3181da177e4SLinus Torvalds 		if (xfs_do_error && (bp != NULL)) {
3191da177e4SLinus Torvalds 			if (xfs_error_target == target) {
3201da177e4SLinus Torvalds 				if (((xfs_req_num++) % xfs_error_mod) == 0) {
3211da177e4SLinus Torvalds 					xfs_buf_relse(bp);
322b6574520SNathan Scott 					cmn_err(CE_DEBUG, "Returning error!\n");
3231da177e4SLinus Torvalds 					return XFS_ERROR(EIO);
3241da177e4SLinus Torvalds 				}
3251da177e4SLinus Torvalds 			}
3261da177e4SLinus Torvalds 		}
3271da177e4SLinus Torvalds #endif
3281da177e4SLinus Torvalds 		if (XFS_FORCED_SHUTDOWN(mp))
3291da177e4SLinus Torvalds 			goto shutdown_abort;
3301da177e4SLinus Torvalds 		*bpp = bp;
3311da177e4SLinus Torvalds 		return 0;
3321da177e4SLinus Torvalds 	}
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	/*
3351da177e4SLinus Torvalds 	 * If we find the buffer in the cache with this transaction
3361da177e4SLinus Torvalds 	 * pointer in its b_fsprivate2 field, then we know we already
3371da177e4SLinus Torvalds 	 * have it locked.  If it is already read in we just increment
3381da177e4SLinus Torvalds 	 * the lock recursion count and return the buffer to the caller.
3391da177e4SLinus Torvalds 	 * If the buffer is not yet read in, then we read it in, increment
3401da177e4SLinus Torvalds 	 * the lock recursion count, and return it to the caller.
3411da177e4SLinus Torvalds 	 */
3421da177e4SLinus Torvalds 	if (tp->t_items.lic_next == NULL) {
3431da177e4SLinus Torvalds 		bp = xfs_trans_buf_item_match(tp, target, blkno, len);
3441da177e4SLinus Torvalds 	} else {
3451da177e4SLinus Torvalds 		bp = xfs_trans_buf_item_match_all(tp, target, blkno, len);
3461da177e4SLinus Torvalds 	}
3471da177e4SLinus Torvalds 	if (bp != NULL) {
3481da177e4SLinus Torvalds 		ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
3491da177e4SLinus Torvalds 		ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
3501da177e4SLinus Torvalds 		ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
3511da177e4SLinus Torvalds 		ASSERT((XFS_BUF_ISERROR(bp)) == 0);
3521da177e4SLinus Torvalds 		if (!(XFS_BUF_ISDONE(bp))) {
3531da177e4SLinus Torvalds 			xfs_buftrace("READ_BUF_INCORE !DONE", bp);
3541da177e4SLinus Torvalds 			ASSERT(!XFS_BUF_ISASYNC(bp));
3551da177e4SLinus Torvalds 			XFS_BUF_READ(bp);
3561da177e4SLinus Torvalds 			xfsbdstrat(tp->t_mountp, bp);
357d64e31a2SDavid Chinner 			error = xfs_iowait(bp);
358d64e31a2SDavid Chinner 			if (error) {
3591da177e4SLinus Torvalds 				xfs_ioerror_alert("xfs_trans_read_buf", mp,
3601da177e4SLinus Torvalds 						  bp, blkno);
3611da177e4SLinus Torvalds 				xfs_buf_relse(bp);
3621da177e4SLinus Torvalds 				/*
363d64e31a2SDavid Chinner 				 * We can gracefully recover from most read
364d64e31a2SDavid Chinner 				 * errors. Ones we can't are those that happen
365d64e31a2SDavid Chinner 				 * after the transaction's already dirty.
3661da177e4SLinus Torvalds 				 */
3671da177e4SLinus Torvalds 				if (tp->t_flags & XFS_TRANS_DIRTY)
3681da177e4SLinus Torvalds 					xfs_force_shutdown(tp->t_mountp,
3697d04a335SNathan Scott 							SHUTDOWN_META_IO_ERROR);
3701da177e4SLinus Torvalds 				return error;
3711da177e4SLinus Torvalds 			}
3721da177e4SLinus Torvalds 		}
3731da177e4SLinus Torvalds 		/*
3741da177e4SLinus Torvalds 		 * We never locked this buf ourselves, so we shouldn't
3751da177e4SLinus Torvalds 		 * brelse it either. Just get out.
3761da177e4SLinus Torvalds 		 */
3771da177e4SLinus Torvalds 		if (XFS_FORCED_SHUTDOWN(mp)) {
3781da177e4SLinus Torvalds 			xfs_buftrace("READ_BUF_INCORE XFSSHUTDN", bp);
3791da177e4SLinus Torvalds 			*bpp = NULL;
3801da177e4SLinus Torvalds 			return XFS_ERROR(EIO);
3811da177e4SLinus Torvalds 		}
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 		bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
3851da177e4SLinus Torvalds 		bip->bli_recur++;
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 		ASSERT(atomic_read(&bip->bli_refcount) > 0);
3881da177e4SLinus Torvalds 		xfs_buf_item_trace("READ RECUR", bip);
3891da177e4SLinus Torvalds 		*bpp = bp;
3901da177e4SLinus Torvalds 		return 0;
3911da177e4SLinus Torvalds 	}
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	/*
3941da177e4SLinus Torvalds 	 * We always specify the BUF_BUSY flag within a transaction so
3951da177e4SLinus Torvalds 	 * that get_buf does not try to push out a delayed write buffer
3961da177e4SLinus Torvalds 	 * which might cause another transaction to take place (if the
3971da177e4SLinus Torvalds 	 * buffer was delayed alloc).  Such recursive transactions can
3981da177e4SLinus Torvalds 	 * easily deadlock with our current transaction as well as cause
3991da177e4SLinus Torvalds 	 * us to run out of stack space.
4001da177e4SLinus Torvalds 	 */
4011da177e4SLinus Torvalds 	bp = xfs_buf_read_flags(target, blkno, len, flags | BUF_BUSY);
4021da177e4SLinus Torvalds 	if (bp == NULL) {
4031da177e4SLinus Torvalds 		*bpp = NULL;
4041da177e4SLinus Torvalds 		return 0;
4051da177e4SLinus Torvalds 	}
4061da177e4SLinus Torvalds 	if (XFS_BUF_GETERROR(bp) != 0) {
4071da177e4SLinus Torvalds 	    XFS_BUF_SUPER_STALE(bp);
4081da177e4SLinus Torvalds 		xfs_buftrace("READ ERROR", bp);
4091da177e4SLinus Torvalds 		error = XFS_BUF_GETERROR(bp);
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 		xfs_ioerror_alert("xfs_trans_read_buf", mp,
4121da177e4SLinus Torvalds 				  bp, blkno);
4131da177e4SLinus Torvalds 		if (tp->t_flags & XFS_TRANS_DIRTY)
4147d04a335SNathan Scott 			xfs_force_shutdown(tp->t_mountp, SHUTDOWN_META_IO_ERROR);
4151da177e4SLinus Torvalds 		xfs_buf_relse(bp);
4161da177e4SLinus Torvalds 		return error;
4171da177e4SLinus Torvalds 	}
4181da177e4SLinus Torvalds #ifdef DEBUG
4191da177e4SLinus Torvalds 	if (xfs_do_error && !(tp->t_flags & XFS_TRANS_DIRTY)) {
4201da177e4SLinus Torvalds 		if (xfs_error_target == target) {
4211da177e4SLinus Torvalds 			if (((xfs_req_num++) % xfs_error_mod) == 0) {
4221da177e4SLinus Torvalds 				xfs_force_shutdown(tp->t_mountp,
4237d04a335SNathan Scott 						   SHUTDOWN_META_IO_ERROR);
4241da177e4SLinus Torvalds 				xfs_buf_relse(bp);
425b6574520SNathan Scott 				cmn_err(CE_DEBUG, "Returning trans error!\n");
4261da177e4SLinus Torvalds 				return XFS_ERROR(EIO);
4271da177e4SLinus Torvalds 			}
4281da177e4SLinus Torvalds 		}
4291da177e4SLinus Torvalds 	}
4301da177e4SLinus Torvalds #endif
4311da177e4SLinus Torvalds 	if (XFS_FORCED_SHUTDOWN(mp))
4321da177e4SLinus Torvalds 		goto shutdown_abort;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 	/*
4351da177e4SLinus Torvalds 	 * The xfs_buf_log_item pointer is stored in b_fsprivate.  If
4361da177e4SLinus Torvalds 	 * it doesn't have one yet, then allocate one and initialize it.
4371da177e4SLinus Torvalds 	 * The checks to see if one is there are in xfs_buf_item_init().
4381da177e4SLinus Torvalds 	 */
4391da177e4SLinus Torvalds 	xfs_buf_item_init(bp, tp->t_mountp);
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	/*
4421da177e4SLinus Torvalds 	 * Set the recursion count for the buffer within this transaction
4431da177e4SLinus Torvalds 	 * to 0.
4441da177e4SLinus Torvalds 	 */
4451da177e4SLinus Torvalds 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
4461da177e4SLinus Torvalds 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
4471da177e4SLinus Torvalds 	ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
4481da177e4SLinus Torvalds 	ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
4491da177e4SLinus Torvalds 	bip->bli_recur = 0;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	/*
4521da177e4SLinus Torvalds 	 * Take a reference for this transaction on the buf item.
4531da177e4SLinus Torvalds 	 */
4541da177e4SLinus Torvalds 	atomic_inc(&bip->bli_refcount);
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	/*
4571da177e4SLinus Torvalds 	 * Get a log_item_desc to point at the new item.
4581da177e4SLinus Torvalds 	 */
4591da177e4SLinus Torvalds 	(void) xfs_trans_add_item(tp, (xfs_log_item_t*)bip);
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	/*
4621da177e4SLinus Torvalds 	 * Initialize b_fsprivate2 so we can find it with incore_match()
4631da177e4SLinus Torvalds 	 * above.
4641da177e4SLinus Torvalds 	 */
4651da177e4SLinus Torvalds 	XFS_BUF_SET_FSPRIVATE2(bp, tp);
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 	xfs_buftrace("TRANS READ", bp);
4681da177e4SLinus Torvalds 	xfs_buf_item_trace("READ", bip);
4691da177e4SLinus Torvalds 	*bpp = bp;
4701da177e4SLinus Torvalds 	return 0;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds shutdown_abort:
4731da177e4SLinus Torvalds 	/*
4741da177e4SLinus Torvalds 	 * the theory here is that buffer is good but we're
4751da177e4SLinus Torvalds 	 * bailing out because the filesystem is being forcibly
4761da177e4SLinus Torvalds 	 * shut down.  So we should leave the b_flags alone since
4771da177e4SLinus Torvalds 	 * the buffer's not staled and just get out.
4781da177e4SLinus Torvalds 	 */
4791da177e4SLinus Torvalds #if defined(DEBUG)
4801da177e4SLinus Torvalds 	if (XFS_BUF_ISSTALE(bp) && XFS_BUF_ISDELAYWRITE(bp))
4811da177e4SLinus Torvalds 		cmn_err(CE_NOTE, "about to pop assert, bp == 0x%p", bp);
4821da177e4SLinus Torvalds #endif
4831da177e4SLinus Torvalds 	ASSERT((XFS_BUF_BFLAGS(bp) & (XFS_B_STALE|XFS_B_DELWRI)) !=
4841da177e4SLinus Torvalds 						(XFS_B_STALE|XFS_B_DELWRI));
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	xfs_buftrace("READ_BUF XFSSHUTDN", bp);
4871da177e4SLinus Torvalds 	xfs_buf_relse(bp);
4881da177e4SLinus Torvalds 	*bpp = NULL;
4891da177e4SLinus Torvalds 	return XFS_ERROR(EIO);
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds /*
4941da177e4SLinus Torvalds  * Release the buffer bp which was previously acquired with one of the
4951da177e4SLinus Torvalds  * xfs_trans_... buffer allocation routines if the buffer has not
4961da177e4SLinus Torvalds  * been modified within this transaction.  If the buffer is modified
4971da177e4SLinus Torvalds  * within this transaction, do decrement the recursion count but do
4981da177e4SLinus Torvalds  * not release the buffer even if the count goes to 0.  If the buffer is not
4991da177e4SLinus Torvalds  * modified within the transaction, decrement the recursion count and
5001da177e4SLinus Torvalds  * release the buffer if the recursion count goes to 0.
5011da177e4SLinus Torvalds  *
5021da177e4SLinus Torvalds  * If the buffer is to be released and it was not modified before
5031da177e4SLinus Torvalds  * this transaction began, then free the buf_log_item associated with it.
5041da177e4SLinus Torvalds  *
5051da177e4SLinus Torvalds  * If the transaction pointer is NULL, make this just a normal
5061da177e4SLinus Torvalds  * brelse() call.
5071da177e4SLinus Torvalds  */
5081da177e4SLinus Torvalds void
5091da177e4SLinus Torvalds xfs_trans_brelse(xfs_trans_t	*tp,
5101da177e4SLinus Torvalds 		 xfs_buf_t	*bp)
5111da177e4SLinus Torvalds {
5121da177e4SLinus Torvalds 	xfs_buf_log_item_t	*bip;
5131da177e4SLinus Torvalds 	xfs_log_item_t		*lip;
5141da177e4SLinus Torvalds 	xfs_log_item_desc_t	*lidp;
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	/*
5171da177e4SLinus Torvalds 	 * Default to a normal brelse() call if the tp is NULL.
5181da177e4SLinus Torvalds 	 */
5191da177e4SLinus Torvalds 	if (tp == NULL) {
5201da177e4SLinus Torvalds 		ASSERT(XFS_BUF_FSPRIVATE2(bp, void *) == NULL);
5211da177e4SLinus Torvalds 		/*
5221da177e4SLinus Torvalds 		 * If there's a buf log item attached to the buffer,
5231da177e4SLinus Torvalds 		 * then let the AIL know that the buffer is being
5241da177e4SLinus Torvalds 		 * unlocked.
5251da177e4SLinus Torvalds 		 */
5261da177e4SLinus Torvalds 		if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
5271da177e4SLinus Torvalds 			lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
5281da177e4SLinus Torvalds 			if (lip->li_type == XFS_LI_BUF) {
5291da177e4SLinus Torvalds 				bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*);
530*783a2f65SDavid Chinner 				xfs_trans_unlocked_item(bip->bli_item.li_ailp,
5311da177e4SLinus Torvalds 							lip);
5321da177e4SLinus Torvalds 			}
5331da177e4SLinus Torvalds 		}
5341da177e4SLinus Torvalds 		xfs_buf_relse(bp);
5351da177e4SLinus Torvalds 		return;
5361da177e4SLinus Torvalds 	}
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
5391da177e4SLinus Torvalds 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
5401da177e4SLinus Torvalds 	ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
5411da177e4SLinus Torvalds 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
5421da177e4SLinus Torvalds 	ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
5431da177e4SLinus Torvalds 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	/*
5461da177e4SLinus Torvalds 	 * Find the item descriptor pointing to this buffer's
5471da177e4SLinus Torvalds 	 * log item.  It must be there.
5481da177e4SLinus Torvalds 	 */
5491da177e4SLinus Torvalds 	lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip);
5501da177e4SLinus Torvalds 	ASSERT(lidp != NULL);
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	/*
5531da177e4SLinus Torvalds 	 * If the release is just for a recursive lock,
5541da177e4SLinus Torvalds 	 * then decrement the count and return.
5551da177e4SLinus Torvalds 	 */
5561da177e4SLinus Torvalds 	if (bip->bli_recur > 0) {
5571da177e4SLinus Torvalds 		bip->bli_recur--;
5581da177e4SLinus Torvalds 		xfs_buf_item_trace("RELSE RECUR", bip);
5591da177e4SLinus Torvalds 		return;
5601da177e4SLinus Torvalds 	}
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	/*
5631da177e4SLinus Torvalds 	 * If the buffer is dirty within this transaction, we can't
5641da177e4SLinus Torvalds 	 * release it until we commit.
5651da177e4SLinus Torvalds 	 */
5661da177e4SLinus Torvalds 	if (lidp->lid_flags & XFS_LID_DIRTY) {
5671da177e4SLinus Torvalds 		xfs_buf_item_trace("RELSE DIRTY", bip);
5681da177e4SLinus Torvalds 		return;
5691da177e4SLinus Torvalds 	}
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	/*
5721da177e4SLinus Torvalds 	 * If the buffer has been invalidated, then we can't release
5731da177e4SLinus Torvalds 	 * it until the transaction commits to disk unless it is re-dirtied
5741da177e4SLinus Torvalds 	 * as part of this transaction.  This prevents us from pulling
5751da177e4SLinus Torvalds 	 * the item from the AIL before we should.
5761da177e4SLinus Torvalds 	 */
5771da177e4SLinus Torvalds 	if (bip->bli_flags & XFS_BLI_STALE) {
5781da177e4SLinus Torvalds 		xfs_buf_item_trace("RELSE STALE", bip);
5791da177e4SLinus Torvalds 		return;
5801da177e4SLinus Torvalds 	}
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
5831da177e4SLinus Torvalds 	xfs_buf_item_trace("RELSE", bip);
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	/*
5861da177e4SLinus Torvalds 	 * Free up the log item descriptor tracking the released item.
5871da177e4SLinus Torvalds 	 */
5881da177e4SLinus Torvalds 	xfs_trans_free_item(tp, lidp);
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	/*
5911da177e4SLinus Torvalds 	 * Clear the hold flag in the buf log item if it is set.
5921da177e4SLinus Torvalds 	 * We wouldn't want the next user of the buffer to
5931da177e4SLinus Torvalds 	 * get confused.
5941da177e4SLinus Torvalds 	 */
5951da177e4SLinus Torvalds 	if (bip->bli_flags & XFS_BLI_HOLD) {
5961da177e4SLinus Torvalds 		bip->bli_flags &= ~XFS_BLI_HOLD;
5971da177e4SLinus Torvalds 	}
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	/*
6001da177e4SLinus Torvalds 	 * Drop our reference to the buf log item.
6011da177e4SLinus Torvalds 	 */
6021da177e4SLinus Torvalds 	atomic_dec(&bip->bli_refcount);
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	/*
6051da177e4SLinus Torvalds 	 * If the buf item is not tracking data in the log, then
6061da177e4SLinus Torvalds 	 * we must free it before releasing the buffer back to the
6071da177e4SLinus Torvalds 	 * free pool.  Before releasing the buffer to the free pool,
6081da177e4SLinus Torvalds 	 * clear the transaction pointer in b_fsprivate2 to dissolve
6091da177e4SLinus Torvalds 	 * its relation to this transaction.
6101da177e4SLinus Torvalds 	 */
6111da177e4SLinus Torvalds 	if (!xfs_buf_item_dirty(bip)) {
6121da177e4SLinus Torvalds /***
6131da177e4SLinus Torvalds 		ASSERT(bp->b_pincount == 0);
6141da177e4SLinus Torvalds ***/
6151da177e4SLinus Torvalds 		ASSERT(atomic_read(&bip->bli_refcount) == 0);
6161da177e4SLinus Torvalds 		ASSERT(!(bip->bli_item.li_flags & XFS_LI_IN_AIL));
6171da177e4SLinus Torvalds 		ASSERT(!(bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF));
6181da177e4SLinus Torvalds 		xfs_buf_item_relse(bp);
6191da177e4SLinus Torvalds 		bip = NULL;
6201da177e4SLinus Torvalds 	}
6211da177e4SLinus Torvalds 	XFS_BUF_SET_FSPRIVATE2(bp, NULL);
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	/*
6241da177e4SLinus Torvalds 	 * If we've still got a buf log item on the buffer, then
6251da177e4SLinus Torvalds 	 * tell the AIL that the buffer is being unlocked.
6261da177e4SLinus Torvalds 	 */
6271da177e4SLinus Torvalds 	if (bip != NULL) {
628*783a2f65SDavid Chinner 		xfs_trans_unlocked_item(bip->bli_item.li_ailp,
6291da177e4SLinus Torvalds 					(xfs_log_item_t*)bip);
6301da177e4SLinus Torvalds 	}
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 	xfs_buf_relse(bp);
6331da177e4SLinus Torvalds 	return;
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds /*
6371da177e4SLinus Torvalds  * Add the locked buffer to the transaction.
6381da177e4SLinus Torvalds  * The buffer must be locked, and it cannot be associated with any
6391da177e4SLinus Torvalds  * transaction.
6401da177e4SLinus Torvalds  *
6411da177e4SLinus Torvalds  * If the buffer does not yet have a buf log item associated with it,
6421da177e4SLinus Torvalds  * then allocate one for it.  Then add the buf item to the transaction.
6431da177e4SLinus Torvalds  */
6441da177e4SLinus Torvalds void
6451da177e4SLinus Torvalds xfs_trans_bjoin(xfs_trans_t	*tp,
6461da177e4SLinus Torvalds 		xfs_buf_t	*bp)
6471da177e4SLinus Torvalds {
6481da177e4SLinus Torvalds 	xfs_buf_log_item_t	*bip;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISBUSY(bp));
6511da177e4SLinus Torvalds 	ASSERT(XFS_BUF_FSPRIVATE2(bp, void *) == NULL);
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	/*
6541da177e4SLinus Torvalds 	 * The xfs_buf_log_item pointer is stored in b_fsprivate.  If
6551da177e4SLinus Torvalds 	 * it doesn't have one yet, then allocate one and initialize it.
6561da177e4SLinus Torvalds 	 * The checks to see if one is there are in xfs_buf_item_init().
6571da177e4SLinus Torvalds 	 */
6581da177e4SLinus Torvalds 	xfs_buf_item_init(bp, tp->t_mountp);
6591da177e4SLinus Torvalds 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
6601da177e4SLinus Torvalds 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
6611da177e4SLinus Torvalds 	ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
6621da177e4SLinus Torvalds 	ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	/*
6651da177e4SLinus Torvalds 	 * Take a reference for this transaction on the buf item.
6661da177e4SLinus Torvalds 	 */
6671da177e4SLinus Torvalds 	atomic_inc(&bip->bli_refcount);
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	/*
6701da177e4SLinus Torvalds 	 * Get a log_item_desc to point at the new item.
6711da177e4SLinus Torvalds 	 */
6721da177e4SLinus Torvalds 	(void) xfs_trans_add_item(tp, (xfs_log_item_t *)bip);
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	/*
6751da177e4SLinus Torvalds 	 * Initialize b_fsprivate2 so we can find it with incore_match()
6761da177e4SLinus Torvalds 	 * in xfs_trans_get_buf() and friends above.
6771da177e4SLinus Torvalds 	 */
6781da177e4SLinus Torvalds 	XFS_BUF_SET_FSPRIVATE2(bp, tp);
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	xfs_buf_item_trace("BJOIN", bip);
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds /*
6841da177e4SLinus Torvalds  * Mark the buffer as not needing to be unlocked when the buf item's
6851da177e4SLinus Torvalds  * IOP_UNLOCK() routine is called.  The buffer must already be locked
6861da177e4SLinus Torvalds  * and associated with the given transaction.
6871da177e4SLinus Torvalds  */
6881da177e4SLinus Torvalds /* ARGSUSED */
6891da177e4SLinus Torvalds void
6901da177e4SLinus Torvalds xfs_trans_bhold(xfs_trans_t	*tp,
6911da177e4SLinus Torvalds 		xfs_buf_t	*bp)
6921da177e4SLinus Torvalds {
6931da177e4SLinus Torvalds 	xfs_buf_log_item_t	*bip;
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISBUSY(bp));
6961da177e4SLinus Torvalds 	ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
6971da177e4SLinus Torvalds 	ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
7001da177e4SLinus Torvalds 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
7011da177e4SLinus Torvalds 	ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
7021da177e4SLinus Torvalds 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
7031da177e4SLinus Torvalds 	bip->bli_flags |= XFS_BLI_HOLD;
7041da177e4SLinus Torvalds 	xfs_buf_item_trace("BHOLD", bip);
7051da177e4SLinus Torvalds }
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds /*
708efa092f3STim Shimmin  * Cancel the previous buffer hold request made on this buffer
709efa092f3STim Shimmin  * for this transaction.
710efa092f3STim Shimmin  */
711efa092f3STim Shimmin void
712efa092f3STim Shimmin xfs_trans_bhold_release(xfs_trans_t	*tp,
713efa092f3STim Shimmin 			xfs_buf_t	*bp)
714efa092f3STim Shimmin {
715efa092f3STim Shimmin 	xfs_buf_log_item_t	*bip;
716efa092f3STim Shimmin 
717efa092f3STim Shimmin 	ASSERT(XFS_BUF_ISBUSY(bp));
718efa092f3STim Shimmin 	ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
719efa092f3STim Shimmin 	ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
720efa092f3STim Shimmin 
721efa092f3STim Shimmin 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
722efa092f3STim Shimmin 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
723efa092f3STim Shimmin 	ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
724efa092f3STim Shimmin 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
725efa092f3STim Shimmin 	ASSERT(bip->bli_flags & XFS_BLI_HOLD);
726efa092f3STim Shimmin 	bip->bli_flags &= ~XFS_BLI_HOLD;
727efa092f3STim Shimmin 	xfs_buf_item_trace("BHOLD RELEASE", bip);
728efa092f3STim Shimmin }
729efa092f3STim Shimmin 
730efa092f3STim Shimmin /*
7311da177e4SLinus Torvalds  * This is called to mark bytes first through last inclusive of the given
7321da177e4SLinus Torvalds  * buffer as needing to be logged when the transaction is committed.
7331da177e4SLinus Torvalds  * The buffer must already be associated with the given transaction.
7341da177e4SLinus Torvalds  *
7351da177e4SLinus Torvalds  * First and last are numbers relative to the beginning of this buffer,
7361da177e4SLinus Torvalds  * so the first byte in the buffer is numbered 0 regardless of the
7371da177e4SLinus Torvalds  * value of b_blkno.
7381da177e4SLinus Torvalds  */
7391da177e4SLinus Torvalds void
7401da177e4SLinus Torvalds xfs_trans_log_buf(xfs_trans_t	*tp,
7411da177e4SLinus Torvalds 		  xfs_buf_t	*bp,
7421da177e4SLinus Torvalds 		  uint		first,
7431da177e4SLinus Torvalds 		  uint		last)
7441da177e4SLinus Torvalds {
7451da177e4SLinus Torvalds 	xfs_buf_log_item_t	*bip;
7461da177e4SLinus Torvalds 	xfs_log_item_desc_t	*lidp;
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISBUSY(bp));
7491da177e4SLinus Torvalds 	ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
7501da177e4SLinus Torvalds 	ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
7511da177e4SLinus Torvalds 	ASSERT((first <= last) && (last < XFS_BUF_COUNT(bp)));
7521da177e4SLinus Torvalds 	ASSERT((XFS_BUF_IODONE_FUNC(bp) == NULL) ||
7531da177e4SLinus Torvalds 	       (XFS_BUF_IODONE_FUNC(bp) == xfs_buf_iodone_callbacks));
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	/*
7561da177e4SLinus Torvalds 	 * Mark the buffer as needing to be written out eventually,
7571da177e4SLinus Torvalds 	 * and set its iodone function to remove the buffer's buf log
7581da177e4SLinus Torvalds 	 * item from the AIL and free it when the buffer is flushed
7591da177e4SLinus Torvalds 	 * to disk.  See xfs_buf_attach_iodone() for more details
7601da177e4SLinus Torvalds 	 * on li_cb and xfs_buf_iodone_callbacks().
7611da177e4SLinus Torvalds 	 * If we end up aborting this transaction, we trap this buffer
7621da177e4SLinus Torvalds 	 * inside the b_bdstrat callback so that this won't get written to
7631da177e4SLinus Torvalds 	 * disk.
7641da177e4SLinus Torvalds 	 */
7651da177e4SLinus Torvalds 	XFS_BUF_DELAYWRITE(bp);
7661da177e4SLinus Torvalds 	XFS_BUF_DONE(bp);
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
7691da177e4SLinus Torvalds 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
7701da177e4SLinus Torvalds 	XFS_BUF_SET_IODONE_FUNC(bp, xfs_buf_iodone_callbacks);
7711da177e4SLinus Torvalds 	bip->bli_item.li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*))xfs_buf_iodone;
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	/*
7741da177e4SLinus Torvalds 	 * If we invalidated the buffer within this transaction, then
7751da177e4SLinus Torvalds 	 * cancel the invalidation now that we're dirtying the buffer
7761da177e4SLinus Torvalds 	 * again.  There are no races with the code in xfs_buf_item_unpin(),
7771da177e4SLinus Torvalds 	 * because we have a reference to the buffer this entire time.
7781da177e4SLinus Torvalds 	 */
7791da177e4SLinus Torvalds 	if (bip->bli_flags & XFS_BLI_STALE) {
7801da177e4SLinus Torvalds 		xfs_buf_item_trace("BLOG UNSTALE", bip);
7811da177e4SLinus Torvalds 		bip->bli_flags &= ~XFS_BLI_STALE;
7821da177e4SLinus Torvalds 		ASSERT(XFS_BUF_ISSTALE(bp));
7831da177e4SLinus Torvalds 		XFS_BUF_UNSTALE(bp);
7841da177e4SLinus Torvalds 		bip->bli_format.blf_flags &= ~XFS_BLI_CANCEL;
7851da177e4SLinus Torvalds 	}
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip);
7881da177e4SLinus Torvalds 	ASSERT(lidp != NULL);
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	tp->t_flags |= XFS_TRANS_DIRTY;
7911da177e4SLinus Torvalds 	lidp->lid_flags |= XFS_LID_DIRTY;
7921da177e4SLinus Torvalds 	lidp->lid_flags &= ~XFS_LID_BUF_STALE;
7931da177e4SLinus Torvalds 	bip->bli_flags |= XFS_BLI_LOGGED;
7941da177e4SLinus Torvalds 	xfs_buf_item_log(bip, first, last);
7951da177e4SLinus Torvalds 	xfs_buf_item_trace("BLOG", bip);
7961da177e4SLinus Torvalds }
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds /*
8001da177e4SLinus Torvalds  * This called to invalidate a buffer that is being used within
8011da177e4SLinus Torvalds  * a transaction.  Typically this is because the blocks in the
8021da177e4SLinus Torvalds  * buffer are being freed, so we need to prevent it from being
8031da177e4SLinus Torvalds  * written out when we're done.  Allowing it to be written again
8041da177e4SLinus Torvalds  * might overwrite data in the free blocks if they are reallocated
8051da177e4SLinus Torvalds  * to a file.
8061da177e4SLinus Torvalds  *
8071da177e4SLinus Torvalds  * We prevent the buffer from being written out by clearing the
8081da177e4SLinus Torvalds  * B_DELWRI flag.  We can't always
8091da177e4SLinus Torvalds  * get rid of the buf log item at this point, though, because
8101da177e4SLinus Torvalds  * the buffer may still be pinned by another transaction.  If that
8111da177e4SLinus Torvalds  * is the case, then we'll wait until the buffer is committed to
8121da177e4SLinus Torvalds  * disk for the last time (we can tell by the ref count) and
8131da177e4SLinus Torvalds  * free it in xfs_buf_item_unpin().  Until it is cleaned up we
8141da177e4SLinus Torvalds  * will keep the buffer locked so that the buffer and buf log item
8151da177e4SLinus Torvalds  * are not reused.
8161da177e4SLinus Torvalds  */
8171da177e4SLinus Torvalds void
8181da177e4SLinus Torvalds xfs_trans_binval(
8191da177e4SLinus Torvalds 	xfs_trans_t	*tp,
8201da177e4SLinus Torvalds 	xfs_buf_t	*bp)
8211da177e4SLinus Torvalds {
8221da177e4SLinus Torvalds 	xfs_log_item_desc_t	*lidp;
8231da177e4SLinus Torvalds 	xfs_buf_log_item_t	*bip;
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISBUSY(bp));
8261da177e4SLinus Torvalds 	ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
8271da177e4SLinus Torvalds 	ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
8301da177e4SLinus Torvalds 	lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip);
8311da177e4SLinus Torvalds 	ASSERT(lidp != NULL);
8321da177e4SLinus Torvalds 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 	if (bip->bli_flags & XFS_BLI_STALE) {
8351da177e4SLinus Torvalds 		/*
8361da177e4SLinus Torvalds 		 * If the buffer is already invalidated, then
8371da177e4SLinus Torvalds 		 * just return.
8381da177e4SLinus Torvalds 		 */
8391da177e4SLinus Torvalds 		ASSERT(!(XFS_BUF_ISDELAYWRITE(bp)));
8401da177e4SLinus Torvalds 		ASSERT(XFS_BUF_ISSTALE(bp));
8411da177e4SLinus Torvalds 		ASSERT(!(bip->bli_flags & (XFS_BLI_LOGGED | XFS_BLI_DIRTY)));
8421da177e4SLinus Torvalds 		ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_INODE_BUF));
8431da177e4SLinus Torvalds 		ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
8441da177e4SLinus Torvalds 		ASSERT(lidp->lid_flags & XFS_LID_DIRTY);
8451da177e4SLinus Torvalds 		ASSERT(tp->t_flags & XFS_TRANS_DIRTY);
8461da177e4SLinus Torvalds 		xfs_buftrace("XFS_BINVAL RECUR", bp);
8471da177e4SLinus Torvalds 		xfs_buf_item_trace("BINVAL RECUR", bip);
8481da177e4SLinus Torvalds 		return;
8491da177e4SLinus Torvalds 	}
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	/*
8521da177e4SLinus Torvalds 	 * Clear the dirty bit in the buffer and set the STALE flag
8531da177e4SLinus Torvalds 	 * in the buf log item.  The STALE flag will be used in
8541da177e4SLinus Torvalds 	 * xfs_buf_item_unpin() to determine if it should clean up
8551da177e4SLinus Torvalds 	 * when the last reference to the buf item is given up.
8561da177e4SLinus Torvalds 	 * We set the XFS_BLI_CANCEL flag in the buf log format structure
8571da177e4SLinus Torvalds 	 * and log the buf item.  This will be used at recovery time
8581da177e4SLinus Torvalds 	 * to determine that copies of the buffer in the log before
8591da177e4SLinus Torvalds 	 * this should not be replayed.
8601da177e4SLinus Torvalds 	 * We mark the item descriptor and the transaction dirty so
8611da177e4SLinus Torvalds 	 * that we'll hold the buffer until after the commit.
8621da177e4SLinus Torvalds 	 *
8631da177e4SLinus Torvalds 	 * Since we're invalidating the buffer, we also clear the state
8641da177e4SLinus Torvalds 	 * about which parts of the buffer have been logged.  We also
8651da177e4SLinus Torvalds 	 * clear the flag indicating that this is an inode buffer since
8661da177e4SLinus Torvalds 	 * the data in the buffer will no longer be valid.
8671da177e4SLinus Torvalds 	 *
8681da177e4SLinus Torvalds 	 * We set the stale bit in the buffer as well since we're getting
8691da177e4SLinus Torvalds 	 * rid of it.
8701da177e4SLinus Torvalds 	 */
8711da177e4SLinus Torvalds 	XFS_BUF_UNDELAYWRITE(bp);
8721da177e4SLinus Torvalds 	XFS_BUF_STALE(bp);
8731da177e4SLinus Torvalds 	bip->bli_flags |= XFS_BLI_STALE;
8741da177e4SLinus Torvalds 	bip->bli_flags &= ~(XFS_BLI_LOGGED | XFS_BLI_DIRTY);
8751da177e4SLinus Torvalds 	bip->bli_format.blf_flags &= ~XFS_BLI_INODE_BUF;
8761da177e4SLinus Torvalds 	bip->bli_format.blf_flags |= XFS_BLI_CANCEL;
8771da177e4SLinus Torvalds 	memset((char *)(bip->bli_format.blf_data_map), 0,
8781da177e4SLinus Torvalds 	      (bip->bli_format.blf_map_size * sizeof(uint)));
8791da177e4SLinus Torvalds 	lidp->lid_flags |= XFS_LID_DIRTY|XFS_LID_BUF_STALE;
8801da177e4SLinus Torvalds 	tp->t_flags |= XFS_TRANS_DIRTY;
8811da177e4SLinus Torvalds 	xfs_buftrace("XFS_BINVAL", bp);
8821da177e4SLinus Torvalds 	xfs_buf_item_trace("BINVAL", bip);
8831da177e4SLinus Torvalds }
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds /*
8861da177e4SLinus Torvalds  * This call is used to indicate that the buffer contains on-disk
8871da177e4SLinus Torvalds  * inodes which must be handled specially during recovery.  They
8881da177e4SLinus Torvalds  * require special handling because only the di_next_unlinked from
8891da177e4SLinus Torvalds  * the inodes in the buffer should be recovered.  The rest of the
8901da177e4SLinus Torvalds  * data in the buffer is logged via the inodes themselves.
8911da177e4SLinus Torvalds  *
8921da177e4SLinus Torvalds  * All we do is set the XFS_BLI_INODE_BUF flag in the buffer's log
8931da177e4SLinus Torvalds  * format structure so that we'll know what to do at recovery time.
8941da177e4SLinus Torvalds  */
8951da177e4SLinus Torvalds /* ARGSUSED */
8961da177e4SLinus Torvalds void
8971da177e4SLinus Torvalds xfs_trans_inode_buf(
8981da177e4SLinus Torvalds 	xfs_trans_t	*tp,
8991da177e4SLinus Torvalds 	xfs_buf_t	*bp)
9001da177e4SLinus Torvalds {
9011da177e4SLinus Torvalds 	xfs_buf_log_item_t	*bip;
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISBUSY(bp));
9041da177e4SLinus Torvalds 	ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
9051da177e4SLinus Torvalds 	ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
9081da177e4SLinus Torvalds 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 	bip->bli_format.blf_flags |= XFS_BLI_INODE_BUF;
9111da177e4SLinus Torvalds }
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds /*
9141da177e4SLinus Torvalds  * This call is used to indicate that the buffer is going to
9151da177e4SLinus Torvalds  * be staled and was an inode buffer. This means it gets
9161da177e4SLinus Torvalds  * special processing during unpin - where any inodes
9171da177e4SLinus Torvalds  * associated with the buffer should be removed from ail.
9181da177e4SLinus Torvalds  * There is also special processing during recovery,
9191da177e4SLinus Torvalds  * any replay of the inodes in the buffer needs to be
9201da177e4SLinus Torvalds  * prevented as the buffer may have been reused.
9211da177e4SLinus Torvalds  */
9221da177e4SLinus Torvalds void
9231da177e4SLinus Torvalds xfs_trans_stale_inode_buf(
9241da177e4SLinus Torvalds 	xfs_trans_t	*tp,
9251da177e4SLinus Torvalds 	xfs_buf_t	*bp)
9261da177e4SLinus Torvalds {
9271da177e4SLinus Torvalds 	xfs_buf_log_item_t	*bip;
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISBUSY(bp));
9301da177e4SLinus Torvalds 	ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
9311da177e4SLinus Torvalds 	ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
9341da177e4SLinus Torvalds 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	bip->bli_flags |= XFS_BLI_STALE_INODE;
9371da177e4SLinus Torvalds 	bip->bli_item.li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*))
9381da177e4SLinus Torvalds 		xfs_buf_iodone;
9391da177e4SLinus Torvalds }
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds /*
9441da177e4SLinus Torvalds  * Mark the buffer as being one which contains newly allocated
9451da177e4SLinus Torvalds  * inodes.  We need to make sure that even if this buffer is
9461da177e4SLinus Torvalds  * relogged as an 'inode buf' we still recover all of the inode
9471da177e4SLinus Torvalds  * images in the face of a crash.  This works in coordination with
9481da177e4SLinus Torvalds  * xfs_buf_item_committed() to ensure that the buffer remains in the
9491da177e4SLinus Torvalds  * AIL at its original location even after it has been relogged.
9501da177e4SLinus Torvalds  */
9511da177e4SLinus Torvalds /* ARGSUSED */
9521da177e4SLinus Torvalds void
9531da177e4SLinus Torvalds xfs_trans_inode_alloc_buf(
9541da177e4SLinus Torvalds 	xfs_trans_t	*tp,
9551da177e4SLinus Torvalds 	xfs_buf_t	*bp)
9561da177e4SLinus Torvalds {
9571da177e4SLinus Torvalds 	xfs_buf_log_item_t	*bip;
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISBUSY(bp));
9601da177e4SLinus Torvalds 	ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
9611da177e4SLinus Torvalds 	ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
9641da177e4SLinus Torvalds 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 	bip->bli_flags |= XFS_BLI_INODE_ALLOC_BUF;
9671da177e4SLinus Torvalds }
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds /*
9711da177e4SLinus Torvalds  * Similar to xfs_trans_inode_buf(), this marks the buffer as a cluster of
9721da177e4SLinus Torvalds  * dquots. However, unlike in inode buffer recovery, dquot buffers get
9731da177e4SLinus Torvalds  * recovered in their entirety. (Hence, no XFS_BLI_DQUOT_ALLOC_BUF flag).
9741da177e4SLinus Torvalds  * The only thing that makes dquot buffers different from regular
9751da177e4SLinus Torvalds  * buffers is that we must not replay dquot bufs when recovering
9761da177e4SLinus Torvalds  * if a _corresponding_ quotaoff has happened. We also have to distinguish
9771da177e4SLinus Torvalds  * between usr dquot bufs and grp dquot bufs, because usr and grp quotas
9781da177e4SLinus Torvalds  * can be turned off independently.
9791da177e4SLinus Torvalds  */
9801da177e4SLinus Torvalds /* ARGSUSED */
9811da177e4SLinus Torvalds void
9821da177e4SLinus Torvalds xfs_trans_dquot_buf(
9831da177e4SLinus Torvalds 	xfs_trans_t	*tp,
9841da177e4SLinus Torvalds 	xfs_buf_t	*bp,
9851da177e4SLinus Torvalds 	uint		type)
9861da177e4SLinus Torvalds {
9871da177e4SLinus Torvalds 	xfs_buf_log_item_t	*bip;
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 	ASSERT(XFS_BUF_ISBUSY(bp));
9901da177e4SLinus Torvalds 	ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
9911da177e4SLinus Torvalds 	ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
9921da177e4SLinus Torvalds 	ASSERT(type == XFS_BLI_UDQUOT_BUF ||
993c8ad20ffSNathan Scott 	       type == XFS_BLI_PDQUOT_BUF ||
9941da177e4SLinus Torvalds 	       type == XFS_BLI_GDQUOT_BUF);
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
9971da177e4SLinus Torvalds 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 	bip->bli_format.blf_flags |= type;
10001da177e4SLinus Torvalds }
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds /*
10031da177e4SLinus Torvalds  * Check to see if a buffer matching the given parameters is already
10041da177e4SLinus Torvalds  * a part of the given transaction.  Only check the first, embedded
10051da177e4SLinus Torvalds  * chunk, since we don't want to spend all day scanning large transactions.
10061da177e4SLinus Torvalds  */
10071da177e4SLinus Torvalds STATIC xfs_buf_t *
10081da177e4SLinus Torvalds xfs_trans_buf_item_match(
10091da177e4SLinus Torvalds 	xfs_trans_t	*tp,
10101da177e4SLinus Torvalds 	xfs_buftarg_t	*target,
10111da177e4SLinus Torvalds 	xfs_daddr_t	blkno,
10121da177e4SLinus Torvalds 	int		len)
10131da177e4SLinus Torvalds {
10141da177e4SLinus Torvalds 	xfs_log_item_chunk_t	*licp;
10151da177e4SLinus Torvalds 	xfs_log_item_desc_t	*lidp;
10161da177e4SLinus Torvalds 	xfs_buf_log_item_t	*blip;
10171da177e4SLinus Torvalds 	xfs_buf_t		*bp;
10181da177e4SLinus Torvalds 	int			i;
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds 	bp = NULL;
10211da177e4SLinus Torvalds 	len = BBTOB(len);
10221da177e4SLinus Torvalds 	licp = &tp->t_items;
102339dab9d7SEric Sandeen 	if (!xfs_lic_are_all_free(licp)) {
10241da177e4SLinus Torvalds 		for (i = 0; i < licp->lic_unused; i++) {
10251da177e4SLinus Torvalds 			/*
10261da177e4SLinus Torvalds 			 * Skip unoccupied slots.
10271da177e4SLinus Torvalds 			 */
102839dab9d7SEric Sandeen 			if (xfs_lic_isfree(licp, i)) {
10291da177e4SLinus Torvalds 				continue;
10301da177e4SLinus Torvalds 			}
10311da177e4SLinus Torvalds 
103239dab9d7SEric Sandeen 			lidp = xfs_lic_slot(licp, i);
10331da177e4SLinus Torvalds 			blip = (xfs_buf_log_item_t *)lidp->lid_item;
10341da177e4SLinus Torvalds 			if (blip->bli_item.li_type != XFS_LI_BUF) {
10351da177e4SLinus Torvalds 				continue;
10361da177e4SLinus Torvalds 			}
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 			bp = blip->bli_buf;
10391da177e4SLinus Torvalds 			if ((XFS_BUF_TARGET(bp) == target) &&
10401da177e4SLinus Torvalds 			    (XFS_BUF_ADDR(bp) == blkno) &&
10411da177e4SLinus Torvalds 			    (XFS_BUF_COUNT(bp) == len)) {
10421da177e4SLinus Torvalds 				/*
10431da177e4SLinus Torvalds 				 * We found it.  Break out and
10441da177e4SLinus Torvalds 				 * return the pointer to the buffer.
10451da177e4SLinus Torvalds 				 */
10461da177e4SLinus Torvalds 				break;
10471da177e4SLinus Torvalds 			} else {
10481da177e4SLinus Torvalds 				bp = NULL;
10491da177e4SLinus Torvalds 			}
10501da177e4SLinus Torvalds 		}
10511da177e4SLinus Torvalds 	}
10521da177e4SLinus Torvalds 	return bp;
10531da177e4SLinus Torvalds }
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds /*
10561da177e4SLinus Torvalds  * Check to see if a buffer matching the given parameters is already
10571da177e4SLinus Torvalds  * a part of the given transaction.  Check all the chunks, we
10581da177e4SLinus Torvalds  * want to be thorough.
10591da177e4SLinus Torvalds  */
10601da177e4SLinus Torvalds STATIC xfs_buf_t *
10611da177e4SLinus Torvalds xfs_trans_buf_item_match_all(
10621da177e4SLinus Torvalds 	xfs_trans_t	*tp,
10631da177e4SLinus Torvalds 	xfs_buftarg_t	*target,
10641da177e4SLinus Torvalds 	xfs_daddr_t	blkno,
10651da177e4SLinus Torvalds 	int		len)
10661da177e4SLinus Torvalds {
10671da177e4SLinus Torvalds 	xfs_log_item_chunk_t	*licp;
10681da177e4SLinus Torvalds 	xfs_log_item_desc_t	*lidp;
10691da177e4SLinus Torvalds 	xfs_buf_log_item_t	*blip;
10701da177e4SLinus Torvalds 	xfs_buf_t		*bp;
10711da177e4SLinus Torvalds 	int			i;
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 	bp = NULL;
10741da177e4SLinus Torvalds 	len = BBTOB(len);
10751da177e4SLinus Torvalds 	for (licp = &tp->t_items; licp != NULL; licp = licp->lic_next) {
107639dab9d7SEric Sandeen 		if (xfs_lic_are_all_free(licp)) {
10771da177e4SLinus Torvalds 			ASSERT(licp == &tp->t_items);
10781da177e4SLinus Torvalds 			ASSERT(licp->lic_next == NULL);
10791da177e4SLinus Torvalds 			return NULL;
10801da177e4SLinus Torvalds 		}
10811da177e4SLinus Torvalds 		for (i = 0; i < licp->lic_unused; i++) {
10821da177e4SLinus Torvalds 			/*
10831da177e4SLinus Torvalds 			 * Skip unoccupied slots.
10841da177e4SLinus Torvalds 			 */
108539dab9d7SEric Sandeen 			if (xfs_lic_isfree(licp, i)) {
10861da177e4SLinus Torvalds 				continue;
10871da177e4SLinus Torvalds 			}
10881da177e4SLinus Torvalds 
108939dab9d7SEric Sandeen 			lidp = xfs_lic_slot(licp, i);
10901da177e4SLinus Torvalds 			blip = (xfs_buf_log_item_t *)lidp->lid_item;
10911da177e4SLinus Torvalds 			if (blip->bli_item.li_type != XFS_LI_BUF) {
10921da177e4SLinus Torvalds 				continue;
10931da177e4SLinus Torvalds 			}
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 			bp = blip->bli_buf;
10961da177e4SLinus Torvalds 			if ((XFS_BUF_TARGET(bp) == target) &&
10971da177e4SLinus Torvalds 			    (XFS_BUF_ADDR(bp) == blkno) &&
10981da177e4SLinus Torvalds 			    (XFS_BUF_COUNT(bp) == len)) {
10991da177e4SLinus Torvalds 				/*
11001da177e4SLinus Torvalds 				 * We found it.  Break out and
11011da177e4SLinus Torvalds 				 * return the pointer to the buffer.
11021da177e4SLinus Torvalds 				 */
11031da177e4SLinus Torvalds 				return bp;
11041da177e4SLinus Torvalds 			}
11051da177e4SLinus Torvalds 		}
11061da177e4SLinus Torvalds 	}
11071da177e4SLinus Torvalds 	return NULL;
11081da177e4SLinus Torvalds }
1109