| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 |  * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. | 
 | 3 |  * All Rights Reserved. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 |  * | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 |  * published by the Free Software Foundation. | 
 | 8 |  * | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 |  * | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_types.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_bit.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_log.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_inum.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_trans.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_sb.h" | 
 | 26 | #include "xfs_ag.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 27 | #include "xfs_dir2.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_dmapi.h" | 
 | 29 | #include "xfs_mount.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 30 | #include "xfs_bmap_btree.h" | 
 | 31 | #include "xfs_alloc_btree.h" | 
 | 32 | #include "xfs_ialloc_btree.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 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" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include "xfs_trans_priv.h" | 
 | 39 | #include "xfs_error.h" | 
 | 40 | #include "xfs_rw.h" | 
 | 41 |  | 
 | 42 |  | 
 | 43 | STATIC xfs_buf_t *xfs_trans_buf_item_match(xfs_trans_t *, xfs_buftarg_t *, | 
 | 44 | 		xfs_daddr_t, int); | 
 | 45 | STATIC xfs_buf_t *xfs_trans_buf_item_match_all(xfs_trans_t *, xfs_buftarg_t *, | 
 | 46 | 		xfs_daddr_t, int); | 
 | 47 |  | 
 | 48 |  | 
 | 49 | /* | 
 | 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. | 
 | 54 |  * | 
 | 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. | 
 | 58 |  * | 
 | 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. | 
 | 62 |  * | 
 | 63 |  * If the transaction pointer is NULL, make this just a normal | 
 | 64 |  * get_buf() call. | 
 | 65 |  */ | 
 | 66 | xfs_buf_t * | 
 | 67 | xfs_trans_get_buf(xfs_trans_t	*tp, | 
 | 68 | 		  xfs_buftarg_t	*target_dev, | 
 | 69 | 		  xfs_daddr_t	blkno, | 
 | 70 | 		  int		len, | 
 | 71 | 		  uint		flags) | 
 | 72 | { | 
 | 73 | 	xfs_buf_t		*bp; | 
 | 74 | 	xfs_buf_log_item_t	*bip; | 
 | 75 |  | 
 | 76 | 	if (flags == 0) | 
 | 77 | 		flags = XFS_BUF_LOCK | XFS_BUF_MAPPED; | 
 | 78 |  | 
 | 79 | 	/* | 
 | 80 | 	 * Default to a normal get_buf() call if the tp is NULL. | 
 | 81 | 	 */ | 
 | 82 | 	if (tp == NULL) { | 
 | 83 | 		bp = xfs_buf_get_flags(target_dev, blkno, len, | 
 | 84 | 							flags | BUF_BUSY); | 
 | 85 | 		return(bp); | 
 | 86 | 	} | 
 | 87 |  | 
 | 88 | 	/* | 
 | 89 | 	 * If we find the buffer in the cache with this transaction | 
 | 90 | 	 * pointer in its b_fsprivate2 field, then we know we already | 
 | 91 | 	 * have it locked.  In this case we just increment the lock | 
 | 92 | 	 * recursion count and return the buffer to the caller. | 
 | 93 | 	 */ | 
 | 94 | 	if (tp->t_items.lic_next == NULL) { | 
 | 95 | 		bp = xfs_trans_buf_item_match(tp, target_dev, blkno, len); | 
 | 96 | 	} else { | 
 | 97 | 		bp  = xfs_trans_buf_item_match_all(tp, target_dev, blkno, len); | 
 | 98 | 	} | 
 | 99 | 	if (bp != NULL) { | 
 | 100 | 		ASSERT(XFS_BUF_VALUSEMA(bp) <= 0); | 
 | 101 | 		if (XFS_FORCED_SHUTDOWN(tp->t_mountp)) { | 
 | 102 | 			xfs_buftrace("TRANS GET RECUR SHUT", bp); | 
 | 103 | 			XFS_BUF_SUPER_STALE(bp); | 
 | 104 | 		} | 
 | 105 | 		/* | 
 | 106 | 		 * If the buffer is stale then it was binval'ed | 
 | 107 | 		 * since last read.  This doesn't matter since the | 
 | 108 | 		 * caller isn't allowed to use the data anyway. | 
 | 109 | 		 */ | 
 | 110 | 		else if (XFS_BUF_ISSTALE(bp)) { | 
 | 111 | 			xfs_buftrace("TRANS GET RECUR STALE", bp); | 
 | 112 | 			ASSERT(!XFS_BUF_ISDELAYWRITE(bp)); | 
 | 113 | 		} | 
 | 114 | 		ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); | 
 | 115 | 		bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); | 
 | 116 | 		ASSERT(bip != NULL); | 
 | 117 | 		ASSERT(atomic_read(&bip->bli_refcount) > 0); | 
 | 118 | 		bip->bli_recur++; | 
 | 119 | 		xfs_buftrace("TRANS GET RECUR", bp); | 
 | 120 | 		xfs_buf_item_trace("GET RECUR", bip); | 
 | 121 | 		return (bp); | 
 | 122 | 	} | 
 | 123 |  | 
 | 124 | 	/* | 
 | 125 | 	 * We always specify the BUF_BUSY flag within a transaction so | 
 | 126 | 	 * that get_buf does not try to push out a delayed write buffer | 
 | 127 | 	 * which might cause another transaction to take place (if the | 
 | 128 | 	 * buffer was delayed alloc).  Such recursive transactions can | 
 | 129 | 	 * easily deadlock with our current transaction as well as cause | 
 | 130 | 	 * us to run out of stack space. | 
 | 131 | 	 */ | 
 | 132 | 	bp = xfs_buf_get_flags(target_dev, blkno, len, flags | BUF_BUSY); | 
 | 133 | 	if (bp == NULL) { | 
 | 134 | 		return NULL; | 
 | 135 | 	} | 
 | 136 |  | 
 | 137 | 	ASSERT(!XFS_BUF_GETERROR(bp)); | 
 | 138 |  | 
 | 139 | 	/* | 
 | 140 | 	 * The xfs_buf_log_item pointer is stored in b_fsprivate.  If | 
 | 141 | 	 * it doesn't have one yet, then allocate one and initialize it. | 
 | 142 | 	 * The checks to see if one is there are in xfs_buf_item_init(). | 
 | 143 | 	 */ | 
 | 144 | 	xfs_buf_item_init(bp, tp->t_mountp); | 
 | 145 |  | 
 | 146 | 	/* | 
 | 147 | 	 * Set the recursion count for the buffer within this transaction | 
 | 148 | 	 * to 0. | 
 | 149 | 	 */ | 
 | 150 | 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*); | 
 | 151 | 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE)); | 
 | 152 | 	ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL)); | 
 | 153 | 	ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED)); | 
 | 154 | 	bip->bli_recur = 0; | 
 | 155 |  | 
 | 156 | 	/* | 
 | 157 | 	 * Take a reference for this transaction on the buf item. | 
 | 158 | 	 */ | 
 | 159 | 	atomic_inc(&bip->bli_refcount); | 
 | 160 |  | 
 | 161 | 	/* | 
 | 162 | 	 * Get a log_item_desc to point at the new item. | 
 | 163 | 	 */ | 
 | 164 | 	(void) xfs_trans_add_item(tp, (xfs_log_item_t*)bip); | 
 | 165 |  | 
 | 166 | 	/* | 
 | 167 | 	 * Initialize b_fsprivate2 so we can find it with incore_match() | 
 | 168 | 	 * above. | 
 | 169 | 	 */ | 
 | 170 | 	XFS_BUF_SET_FSPRIVATE2(bp, tp); | 
 | 171 |  | 
 | 172 | 	xfs_buftrace("TRANS GET", bp); | 
 | 173 | 	xfs_buf_item_trace("GET", bip); | 
 | 174 | 	return (bp); | 
 | 175 | } | 
 | 176 |  | 
 | 177 | /* | 
 | 178 |  * Get and lock the superblock buffer of this file system for the | 
 | 179 |  * given transaction. | 
 | 180 |  * | 
 | 181 |  * We don't need to use incore_match() here, because the superblock | 
 | 182 |  * buffer is a private buffer which we keep a pointer to in the | 
 | 183 |  * mount structure. | 
 | 184 |  */ | 
 | 185 | xfs_buf_t * | 
 | 186 | xfs_trans_getsb(xfs_trans_t	*tp, | 
 | 187 | 		struct xfs_mount *mp, | 
 | 188 | 		int		flags) | 
 | 189 | { | 
 | 190 | 	xfs_buf_t		*bp; | 
 | 191 | 	xfs_buf_log_item_t	*bip; | 
 | 192 |  | 
 | 193 | 	/* | 
 | 194 | 	 * Default to just trying to lock the superblock buffer | 
 | 195 | 	 * if tp is NULL. | 
 | 196 | 	 */ | 
 | 197 | 	if (tp == NULL) { | 
 | 198 | 		return (xfs_getsb(mp, flags)); | 
 | 199 | 	} | 
 | 200 |  | 
 | 201 | 	/* | 
 | 202 | 	 * If the superblock buffer already has this transaction | 
 | 203 | 	 * pointer in its b_fsprivate2 field, then we know we already | 
 | 204 | 	 * have it locked.  In this case we just increment the lock | 
 | 205 | 	 * recursion count and return the buffer to the caller. | 
 | 206 | 	 */ | 
 | 207 | 	bp = mp->m_sb_bp; | 
 | 208 | 	if (XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp) { | 
 | 209 | 		bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*); | 
 | 210 | 		ASSERT(bip != NULL); | 
 | 211 | 		ASSERT(atomic_read(&bip->bli_refcount) > 0); | 
 | 212 | 		bip->bli_recur++; | 
 | 213 | 		xfs_buf_item_trace("GETSB RECUR", bip); | 
 | 214 | 		return (bp); | 
 | 215 | 	} | 
 | 216 |  | 
 | 217 | 	bp = xfs_getsb(mp, flags); | 
 | 218 | 	if (bp == NULL) { | 
 | 219 | 		return NULL; | 
 | 220 | 	} | 
 | 221 |  | 
 | 222 | 	/* | 
 | 223 | 	 * The xfs_buf_log_item pointer is stored in b_fsprivate.  If | 
 | 224 | 	 * it doesn't have one yet, then allocate one and initialize it. | 
 | 225 | 	 * The checks to see if one is there are in xfs_buf_item_init(). | 
 | 226 | 	 */ | 
 | 227 | 	xfs_buf_item_init(bp, mp); | 
 | 228 |  | 
 | 229 | 	/* | 
 | 230 | 	 * Set the recursion count for the buffer within this transaction | 
 | 231 | 	 * to 0. | 
 | 232 | 	 */ | 
 | 233 | 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*); | 
 | 234 | 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE)); | 
 | 235 | 	ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL)); | 
 | 236 | 	ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED)); | 
 | 237 | 	bip->bli_recur = 0; | 
 | 238 |  | 
 | 239 | 	/* | 
 | 240 | 	 * Take a reference for this transaction on the buf item. | 
 | 241 | 	 */ | 
 | 242 | 	atomic_inc(&bip->bli_refcount); | 
 | 243 |  | 
 | 244 | 	/* | 
 | 245 | 	 * Get a log_item_desc to point at the new item. | 
 | 246 | 	 */ | 
 | 247 | 	(void) xfs_trans_add_item(tp, (xfs_log_item_t*)bip); | 
 | 248 |  | 
 | 249 | 	/* | 
 | 250 | 	 * Initialize b_fsprivate2 so we can find it with incore_match() | 
 | 251 | 	 * above. | 
 | 252 | 	 */ | 
 | 253 | 	XFS_BUF_SET_FSPRIVATE2(bp, tp); | 
 | 254 |  | 
 | 255 | 	xfs_buf_item_trace("GETSB", bip); | 
 | 256 | 	return (bp); | 
 | 257 | } | 
 | 258 |  | 
 | 259 | #ifdef DEBUG | 
 | 260 | xfs_buftarg_t *xfs_error_target; | 
 | 261 | int	xfs_do_error; | 
 | 262 | int	xfs_req_num; | 
 | 263 | int	xfs_error_mod = 33; | 
 | 264 | #endif | 
 | 265 |  | 
 | 266 | /* | 
 | 267 |  * Get and lock the buffer for the caller if it is not already | 
 | 268 |  * locked within the given transaction.  If it has not yet been | 
 | 269 |  * read in, read it from disk. If it is already locked | 
 | 270 |  * within the transaction and already read in, just increment its | 
 | 271 |  * lock recursion count and return a pointer to it. | 
 | 272 |  * | 
 | 273 |  * Use the fast path function xfs_trans_buf_item_match() or the buffer | 
 | 274 |  * cache routine incore_match() to find the buffer | 
 | 275 |  * if it is already owned by this transaction. | 
 | 276 |  * | 
 | 277 |  * If we don't already own the buffer, use read_buf() to get it. | 
 | 278 |  * If it doesn't yet have an associated xfs_buf_log_item structure, | 
 | 279 |  * then allocate one and add the item to this transaction. | 
 | 280 |  * | 
 | 281 |  * If the transaction pointer is NULL, make this just a normal | 
 | 282 |  * read_buf() call. | 
 | 283 |  */ | 
 | 284 | int | 
 | 285 | xfs_trans_read_buf( | 
 | 286 | 	xfs_mount_t	*mp, | 
 | 287 | 	xfs_trans_t	*tp, | 
 | 288 | 	xfs_buftarg_t	*target, | 
 | 289 | 	xfs_daddr_t	blkno, | 
 | 290 | 	int		len, | 
 | 291 | 	uint		flags, | 
 | 292 | 	xfs_buf_t	**bpp) | 
 | 293 | { | 
 | 294 | 	xfs_buf_t		*bp; | 
 | 295 | 	xfs_buf_log_item_t	*bip; | 
 | 296 | 	int			error; | 
 | 297 |  | 
 | 298 | 	if (flags == 0) | 
 | 299 | 		flags = XFS_BUF_LOCK | XFS_BUF_MAPPED; | 
 | 300 |  | 
 | 301 | 	/* | 
 | 302 | 	 * Default to a normal get_buf() call if the tp is NULL. | 
 | 303 | 	 */ | 
 | 304 | 	if (tp == NULL) { | 
 | 305 | 		bp = xfs_buf_read_flags(target, blkno, len, flags | BUF_BUSY); | 
 | 306 | 		if (!bp) | 
| David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 307 | 			return (flags & XFS_BUF_TRYLOCK) ? | 
 | 308 | 					EAGAIN : XFS_ERROR(ENOMEM); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 |  | 
 | 310 | 		if ((bp != NULL) && (XFS_BUF_GETERROR(bp) != 0)) { | 
 | 311 | 			xfs_ioerror_alert("xfs_trans_read_buf", mp, | 
 | 312 | 					  bp, blkno); | 
 | 313 | 			error = XFS_BUF_GETERROR(bp); | 
 | 314 | 			xfs_buf_relse(bp); | 
 | 315 | 			return error; | 
 | 316 | 		} | 
 | 317 | #ifdef DEBUG | 
 | 318 | 		if (xfs_do_error && (bp != NULL)) { | 
 | 319 | 			if (xfs_error_target == target) { | 
 | 320 | 				if (((xfs_req_num++) % xfs_error_mod) == 0) { | 
 | 321 | 					xfs_buf_relse(bp); | 
| Nathan Scott | b657452 | 2006-06-09 15:29:40 +1000 | [diff] [blame] | 322 | 					cmn_err(CE_DEBUG, "Returning error!\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | 					return XFS_ERROR(EIO); | 
 | 324 | 				} | 
 | 325 | 			} | 
 | 326 | 		} | 
 | 327 | #endif | 
 | 328 | 		if (XFS_FORCED_SHUTDOWN(mp)) | 
 | 329 | 			goto shutdown_abort; | 
 | 330 | 		*bpp = bp; | 
 | 331 | 		return 0; | 
 | 332 | 	} | 
 | 333 |  | 
 | 334 | 	/* | 
 | 335 | 	 * If we find the buffer in the cache with this transaction | 
 | 336 | 	 * pointer in its b_fsprivate2 field, then we know we already | 
 | 337 | 	 * have it locked.  If it is already read in we just increment | 
 | 338 | 	 * the lock recursion count and return the buffer to the caller. | 
 | 339 | 	 * If the buffer is not yet read in, then we read it in, increment | 
 | 340 | 	 * the lock recursion count, and return it to the caller. | 
 | 341 | 	 */ | 
 | 342 | 	if (tp->t_items.lic_next == NULL) { | 
 | 343 | 		bp = xfs_trans_buf_item_match(tp, target, blkno, len); | 
 | 344 | 	} else { | 
 | 345 | 		bp = xfs_trans_buf_item_match_all(tp, target, blkno, len); | 
 | 346 | 	} | 
 | 347 | 	if (bp != NULL) { | 
 | 348 | 		ASSERT(XFS_BUF_VALUSEMA(bp) <= 0); | 
 | 349 | 		ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); | 
 | 350 | 		ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL); | 
 | 351 | 		ASSERT((XFS_BUF_ISERROR(bp)) == 0); | 
 | 352 | 		if (!(XFS_BUF_ISDONE(bp))) { | 
 | 353 | 			xfs_buftrace("READ_BUF_INCORE !DONE", bp); | 
 | 354 | 			ASSERT(!XFS_BUF_ISASYNC(bp)); | 
 | 355 | 			XFS_BUF_READ(bp); | 
 | 356 | 			xfsbdstrat(tp->t_mountp, bp); | 
| David Chinner | d64e31a | 2008-04-10 12:22:17 +1000 | [diff] [blame] | 357 | 			error = xfs_iowait(bp); | 
 | 358 | 			if (error) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | 				xfs_ioerror_alert("xfs_trans_read_buf", mp, | 
 | 360 | 						  bp, blkno); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | 				xfs_buf_relse(bp); | 
 | 362 | 				/* | 
| David Chinner | d64e31a | 2008-04-10 12:22:17 +1000 | [diff] [blame] | 363 | 				 * We can gracefully recover from most read | 
 | 364 | 				 * errors. Ones we can't are those that happen | 
 | 365 | 				 * after the transaction's already dirty. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | 				 */ | 
 | 367 | 				if (tp->t_flags & XFS_TRANS_DIRTY) | 
 | 368 | 					xfs_force_shutdown(tp->t_mountp, | 
| Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 369 | 							SHUTDOWN_META_IO_ERROR); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | 				return error; | 
 | 371 | 			} | 
 | 372 | 		} | 
 | 373 | 		/* | 
 | 374 | 		 * We never locked this buf ourselves, so we shouldn't | 
 | 375 | 		 * brelse it either. Just get out. | 
 | 376 | 		 */ | 
 | 377 | 		if (XFS_FORCED_SHUTDOWN(mp)) { | 
 | 378 | 			xfs_buftrace("READ_BUF_INCORE XFSSHUTDN", bp); | 
 | 379 | 			*bpp = NULL; | 
 | 380 | 			return XFS_ERROR(EIO); | 
 | 381 | 		} | 
 | 382 |  | 
 | 383 |  | 
 | 384 | 		bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*); | 
 | 385 | 		bip->bli_recur++; | 
 | 386 |  | 
 | 387 | 		ASSERT(atomic_read(&bip->bli_refcount) > 0); | 
 | 388 | 		xfs_buf_item_trace("READ RECUR", bip); | 
 | 389 | 		*bpp = bp; | 
 | 390 | 		return 0; | 
 | 391 | 	} | 
 | 392 |  | 
 | 393 | 	/* | 
 | 394 | 	 * We always specify the BUF_BUSY flag within a transaction so | 
 | 395 | 	 * that get_buf does not try to push out a delayed write buffer | 
 | 396 | 	 * which might cause another transaction to take place (if the | 
 | 397 | 	 * buffer was delayed alloc).  Such recursive transactions can | 
 | 398 | 	 * easily deadlock with our current transaction as well as cause | 
 | 399 | 	 * us to run out of stack space. | 
 | 400 | 	 */ | 
 | 401 | 	bp = xfs_buf_read_flags(target, blkno, len, flags | BUF_BUSY); | 
 | 402 | 	if (bp == NULL) { | 
 | 403 | 		*bpp = NULL; | 
 | 404 | 		return 0; | 
 | 405 | 	} | 
 | 406 | 	if (XFS_BUF_GETERROR(bp) != 0) { | 
 | 407 | 	    XFS_BUF_SUPER_STALE(bp); | 
 | 408 | 		xfs_buftrace("READ ERROR", bp); | 
 | 409 | 		error = XFS_BUF_GETERROR(bp); | 
 | 410 |  | 
 | 411 | 		xfs_ioerror_alert("xfs_trans_read_buf", mp, | 
 | 412 | 				  bp, blkno); | 
 | 413 | 		if (tp->t_flags & XFS_TRANS_DIRTY) | 
| Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 414 | 			xfs_force_shutdown(tp->t_mountp, SHUTDOWN_META_IO_ERROR); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | 		xfs_buf_relse(bp); | 
 | 416 | 		return error; | 
 | 417 | 	} | 
 | 418 | #ifdef DEBUG | 
 | 419 | 	if (xfs_do_error && !(tp->t_flags & XFS_TRANS_DIRTY)) { | 
 | 420 | 		if (xfs_error_target == target) { | 
 | 421 | 			if (((xfs_req_num++) % xfs_error_mod) == 0) { | 
 | 422 | 				xfs_force_shutdown(tp->t_mountp, | 
| Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 423 | 						   SHUTDOWN_META_IO_ERROR); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | 				xfs_buf_relse(bp); | 
| Nathan Scott | b657452 | 2006-06-09 15:29:40 +1000 | [diff] [blame] | 425 | 				cmn_err(CE_DEBUG, "Returning trans error!\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | 				return XFS_ERROR(EIO); | 
 | 427 | 			} | 
 | 428 | 		} | 
 | 429 | 	} | 
 | 430 | #endif | 
 | 431 | 	if (XFS_FORCED_SHUTDOWN(mp)) | 
 | 432 | 		goto shutdown_abort; | 
 | 433 |  | 
 | 434 | 	/* | 
 | 435 | 	 * The xfs_buf_log_item pointer is stored in b_fsprivate.  If | 
 | 436 | 	 * it doesn't have one yet, then allocate one and initialize it. | 
 | 437 | 	 * The checks to see if one is there are in xfs_buf_item_init(). | 
 | 438 | 	 */ | 
 | 439 | 	xfs_buf_item_init(bp, tp->t_mountp); | 
 | 440 |  | 
 | 441 | 	/* | 
 | 442 | 	 * Set the recursion count for the buffer within this transaction | 
 | 443 | 	 * to 0. | 
 | 444 | 	 */ | 
 | 445 | 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*); | 
 | 446 | 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE)); | 
 | 447 | 	ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL)); | 
 | 448 | 	ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED)); | 
 | 449 | 	bip->bli_recur = 0; | 
 | 450 |  | 
 | 451 | 	/* | 
 | 452 | 	 * Take a reference for this transaction on the buf item. | 
 | 453 | 	 */ | 
 | 454 | 	atomic_inc(&bip->bli_refcount); | 
 | 455 |  | 
 | 456 | 	/* | 
 | 457 | 	 * Get a log_item_desc to point at the new item. | 
 | 458 | 	 */ | 
 | 459 | 	(void) xfs_trans_add_item(tp, (xfs_log_item_t*)bip); | 
 | 460 |  | 
 | 461 | 	/* | 
 | 462 | 	 * Initialize b_fsprivate2 so we can find it with incore_match() | 
 | 463 | 	 * above. | 
 | 464 | 	 */ | 
 | 465 | 	XFS_BUF_SET_FSPRIVATE2(bp, tp); | 
 | 466 |  | 
 | 467 | 	xfs_buftrace("TRANS READ", bp); | 
 | 468 | 	xfs_buf_item_trace("READ", bip); | 
 | 469 | 	*bpp = bp; | 
 | 470 | 	return 0; | 
 | 471 |  | 
 | 472 | shutdown_abort: | 
 | 473 | 	/* | 
 | 474 | 	 * the theory here is that buffer is good but we're | 
 | 475 | 	 * bailing out because the filesystem is being forcibly | 
 | 476 | 	 * shut down.  So we should leave the b_flags alone since | 
 | 477 | 	 * the buffer's not staled and just get out. | 
 | 478 | 	 */ | 
 | 479 | #if defined(DEBUG) | 
 | 480 | 	if (XFS_BUF_ISSTALE(bp) && XFS_BUF_ISDELAYWRITE(bp)) | 
 | 481 | 		cmn_err(CE_NOTE, "about to pop assert, bp == 0x%p", bp); | 
 | 482 | #endif | 
 | 483 | 	ASSERT((XFS_BUF_BFLAGS(bp) & (XFS_B_STALE|XFS_B_DELWRI)) != | 
 | 484 | 						(XFS_B_STALE|XFS_B_DELWRI)); | 
 | 485 |  | 
 | 486 | 	xfs_buftrace("READ_BUF XFSSHUTDN", bp); | 
 | 487 | 	xfs_buf_relse(bp); | 
 | 488 | 	*bpp = NULL; | 
 | 489 | 	return XFS_ERROR(EIO); | 
 | 490 | } | 
 | 491 |  | 
 | 492 |  | 
 | 493 | /* | 
 | 494 |  * Release the buffer bp which was previously acquired with one of the | 
 | 495 |  * xfs_trans_... buffer allocation routines if the buffer has not | 
 | 496 |  * been modified within this transaction.  If the buffer is modified | 
 | 497 |  * within this transaction, do decrement the recursion count but do | 
 | 498 |  * not release the buffer even if the count goes to 0.  If the buffer is not | 
 | 499 |  * modified within the transaction, decrement the recursion count and | 
 | 500 |  * release the buffer if the recursion count goes to 0. | 
 | 501 |  * | 
 | 502 |  * If the buffer is to be released and it was not modified before | 
 | 503 |  * this transaction began, then free the buf_log_item associated with it. | 
 | 504 |  * | 
 | 505 |  * If the transaction pointer is NULL, make this just a normal | 
 | 506 |  * brelse() call. | 
 | 507 |  */ | 
 | 508 | void | 
 | 509 | xfs_trans_brelse(xfs_trans_t	*tp, | 
 | 510 | 		 xfs_buf_t	*bp) | 
 | 511 | { | 
 | 512 | 	xfs_buf_log_item_t	*bip; | 
 | 513 | 	xfs_log_item_t		*lip; | 
 | 514 | 	xfs_log_item_desc_t	*lidp; | 
 | 515 |  | 
 | 516 | 	/* | 
 | 517 | 	 * Default to a normal brelse() call if the tp is NULL. | 
 | 518 | 	 */ | 
 | 519 | 	if (tp == NULL) { | 
 | 520 | 		ASSERT(XFS_BUF_FSPRIVATE2(bp, void *) == NULL); | 
 | 521 | 		/* | 
 | 522 | 		 * If there's a buf log item attached to the buffer, | 
 | 523 | 		 * then let the AIL know that the buffer is being | 
 | 524 | 		 * unlocked. | 
 | 525 | 		 */ | 
 | 526 | 		if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) { | 
 | 527 | 			lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *); | 
 | 528 | 			if (lip->li_type == XFS_LI_BUF) { | 
 | 529 | 				bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*); | 
 | 530 | 				xfs_trans_unlocked_item( | 
 | 531 | 						bip->bli_item.li_mountp, | 
 | 532 | 						lip); | 
 | 533 | 			} | 
 | 534 | 		} | 
 | 535 | 		xfs_buf_relse(bp); | 
 | 536 | 		return; | 
 | 537 | 	} | 
 | 538 |  | 
 | 539 | 	ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); | 
 | 540 | 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); | 
 | 541 | 	ASSERT(bip->bli_item.li_type == XFS_LI_BUF); | 
 | 542 | 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE)); | 
 | 543 | 	ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL)); | 
 | 544 | 	ASSERT(atomic_read(&bip->bli_refcount) > 0); | 
 | 545 |  | 
 | 546 | 	/* | 
 | 547 | 	 * Find the item descriptor pointing to this buffer's | 
 | 548 | 	 * log item.  It must be there. | 
 | 549 | 	 */ | 
 | 550 | 	lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip); | 
 | 551 | 	ASSERT(lidp != NULL); | 
 | 552 |  | 
 | 553 | 	/* | 
 | 554 | 	 * If the release is just for a recursive lock, | 
 | 555 | 	 * then decrement the count and return. | 
 | 556 | 	 */ | 
 | 557 | 	if (bip->bli_recur > 0) { | 
 | 558 | 		bip->bli_recur--; | 
 | 559 | 		xfs_buf_item_trace("RELSE RECUR", bip); | 
 | 560 | 		return; | 
 | 561 | 	} | 
 | 562 |  | 
 | 563 | 	/* | 
 | 564 | 	 * If the buffer is dirty within this transaction, we can't | 
 | 565 | 	 * release it until we commit. | 
 | 566 | 	 */ | 
 | 567 | 	if (lidp->lid_flags & XFS_LID_DIRTY) { | 
 | 568 | 		xfs_buf_item_trace("RELSE DIRTY", bip); | 
 | 569 | 		return; | 
 | 570 | 	} | 
 | 571 |  | 
 | 572 | 	/* | 
 | 573 | 	 * If the buffer has been invalidated, then we can't release | 
 | 574 | 	 * it until the transaction commits to disk unless it is re-dirtied | 
 | 575 | 	 * as part of this transaction.  This prevents us from pulling | 
 | 576 | 	 * the item from the AIL before we should. | 
 | 577 | 	 */ | 
 | 578 | 	if (bip->bli_flags & XFS_BLI_STALE) { | 
 | 579 | 		xfs_buf_item_trace("RELSE STALE", bip); | 
 | 580 | 		return; | 
 | 581 | 	} | 
 | 582 |  | 
 | 583 | 	ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED)); | 
 | 584 | 	xfs_buf_item_trace("RELSE", bip); | 
 | 585 |  | 
 | 586 | 	/* | 
 | 587 | 	 * Free up the log item descriptor tracking the released item. | 
 | 588 | 	 */ | 
 | 589 | 	xfs_trans_free_item(tp, lidp); | 
 | 590 |  | 
 | 591 | 	/* | 
 | 592 | 	 * Clear the hold flag in the buf log item if it is set. | 
 | 593 | 	 * We wouldn't want the next user of the buffer to | 
 | 594 | 	 * get confused. | 
 | 595 | 	 */ | 
 | 596 | 	if (bip->bli_flags & XFS_BLI_HOLD) { | 
 | 597 | 		bip->bli_flags &= ~XFS_BLI_HOLD; | 
 | 598 | 	} | 
 | 599 |  | 
 | 600 | 	/* | 
 | 601 | 	 * Drop our reference to the buf log item. | 
 | 602 | 	 */ | 
 | 603 | 	atomic_dec(&bip->bli_refcount); | 
 | 604 |  | 
 | 605 | 	/* | 
 | 606 | 	 * If the buf item is not tracking data in the log, then | 
 | 607 | 	 * we must free it before releasing the buffer back to the | 
 | 608 | 	 * free pool.  Before releasing the buffer to the free pool, | 
 | 609 | 	 * clear the transaction pointer in b_fsprivate2 to dissolve | 
 | 610 | 	 * its relation to this transaction. | 
 | 611 | 	 */ | 
 | 612 | 	if (!xfs_buf_item_dirty(bip)) { | 
 | 613 | /*** | 
 | 614 | 		ASSERT(bp->b_pincount == 0); | 
 | 615 | ***/ | 
 | 616 | 		ASSERT(atomic_read(&bip->bli_refcount) == 0); | 
 | 617 | 		ASSERT(!(bip->bli_item.li_flags & XFS_LI_IN_AIL)); | 
 | 618 | 		ASSERT(!(bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF)); | 
 | 619 | 		xfs_buf_item_relse(bp); | 
 | 620 | 		bip = NULL; | 
 | 621 | 	} | 
 | 622 | 	XFS_BUF_SET_FSPRIVATE2(bp, NULL); | 
 | 623 |  | 
 | 624 | 	/* | 
 | 625 | 	 * If we've still got a buf log item on the buffer, then | 
 | 626 | 	 * tell the AIL that the buffer is being unlocked. | 
 | 627 | 	 */ | 
 | 628 | 	if (bip != NULL) { | 
 | 629 | 		xfs_trans_unlocked_item(bip->bli_item.li_mountp, | 
 | 630 | 					(xfs_log_item_t*)bip); | 
 | 631 | 	} | 
 | 632 |  | 
 | 633 | 	xfs_buf_relse(bp); | 
 | 634 | 	return; | 
 | 635 | } | 
 | 636 |  | 
 | 637 | /* | 
 | 638 |  * Add the locked buffer to the transaction. | 
 | 639 |  * The buffer must be locked, and it cannot be associated with any | 
 | 640 |  * transaction. | 
 | 641 |  * | 
 | 642 |  * If the buffer does not yet have a buf log item associated with it, | 
 | 643 |  * then allocate one for it.  Then add the buf item to the transaction. | 
 | 644 |  */ | 
 | 645 | void | 
 | 646 | xfs_trans_bjoin(xfs_trans_t	*tp, | 
 | 647 | 		xfs_buf_t	*bp) | 
 | 648 | { | 
 | 649 | 	xfs_buf_log_item_t	*bip; | 
 | 650 |  | 
 | 651 | 	ASSERT(XFS_BUF_ISBUSY(bp)); | 
 | 652 | 	ASSERT(XFS_BUF_FSPRIVATE2(bp, void *) == NULL); | 
 | 653 |  | 
 | 654 | 	/* | 
 | 655 | 	 * The xfs_buf_log_item pointer is stored in b_fsprivate.  If | 
 | 656 | 	 * it doesn't have one yet, then allocate one and initialize it. | 
 | 657 | 	 * The checks to see if one is there are in xfs_buf_item_init(). | 
 | 658 | 	 */ | 
 | 659 | 	xfs_buf_item_init(bp, tp->t_mountp); | 
 | 660 | 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); | 
 | 661 | 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE)); | 
 | 662 | 	ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL)); | 
 | 663 | 	ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED)); | 
 | 664 |  | 
 | 665 | 	/* | 
 | 666 | 	 * Take a reference for this transaction on the buf item. | 
 | 667 | 	 */ | 
 | 668 | 	atomic_inc(&bip->bli_refcount); | 
 | 669 |  | 
 | 670 | 	/* | 
 | 671 | 	 * Get a log_item_desc to point at the new item. | 
 | 672 | 	 */ | 
 | 673 | 	(void) xfs_trans_add_item(tp, (xfs_log_item_t *)bip); | 
 | 674 |  | 
 | 675 | 	/* | 
 | 676 | 	 * Initialize b_fsprivate2 so we can find it with incore_match() | 
 | 677 | 	 * in xfs_trans_get_buf() and friends above. | 
 | 678 | 	 */ | 
 | 679 | 	XFS_BUF_SET_FSPRIVATE2(bp, tp); | 
 | 680 |  | 
 | 681 | 	xfs_buf_item_trace("BJOIN", bip); | 
 | 682 | } | 
 | 683 |  | 
 | 684 | /* | 
 | 685 |  * Mark the buffer as not needing to be unlocked when the buf item's | 
 | 686 |  * IOP_UNLOCK() routine is called.  The buffer must already be locked | 
 | 687 |  * and associated with the given transaction. | 
 | 688 |  */ | 
 | 689 | /* ARGSUSED */ | 
 | 690 | void | 
 | 691 | xfs_trans_bhold(xfs_trans_t	*tp, | 
 | 692 | 		xfs_buf_t	*bp) | 
 | 693 | { | 
 | 694 | 	xfs_buf_log_item_t	*bip; | 
 | 695 |  | 
 | 696 | 	ASSERT(XFS_BUF_ISBUSY(bp)); | 
 | 697 | 	ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); | 
 | 698 | 	ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL); | 
 | 699 |  | 
 | 700 | 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); | 
 | 701 | 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE)); | 
 | 702 | 	ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL)); | 
 | 703 | 	ASSERT(atomic_read(&bip->bli_refcount) > 0); | 
 | 704 | 	bip->bli_flags |= XFS_BLI_HOLD; | 
 | 705 | 	xfs_buf_item_trace("BHOLD", bip); | 
 | 706 | } | 
 | 707 |  | 
 | 708 | /* | 
| Tim Shimmin | efa092f | 2005-09-05 08:29:01 +1000 | [diff] [blame] | 709 |  * Cancel the previous buffer hold request made on this buffer | 
 | 710 |  * for this transaction. | 
 | 711 |  */ | 
 | 712 | void | 
 | 713 | xfs_trans_bhold_release(xfs_trans_t	*tp, | 
 | 714 | 			xfs_buf_t	*bp) | 
 | 715 | { | 
 | 716 | 	xfs_buf_log_item_t	*bip; | 
 | 717 |  | 
 | 718 | 	ASSERT(XFS_BUF_ISBUSY(bp)); | 
 | 719 | 	ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); | 
 | 720 | 	ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL); | 
 | 721 |  | 
 | 722 | 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); | 
 | 723 | 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE)); | 
 | 724 | 	ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL)); | 
 | 725 | 	ASSERT(atomic_read(&bip->bli_refcount) > 0); | 
 | 726 | 	ASSERT(bip->bli_flags & XFS_BLI_HOLD); | 
 | 727 | 	bip->bli_flags &= ~XFS_BLI_HOLD; | 
 | 728 | 	xfs_buf_item_trace("BHOLD RELEASE", bip); | 
 | 729 | } | 
 | 730 |  | 
 | 731 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 |  * This is called to mark bytes first through last inclusive of the given | 
 | 733 |  * buffer as needing to be logged when the transaction is committed. | 
 | 734 |  * The buffer must already be associated with the given transaction. | 
 | 735 |  * | 
 | 736 |  * First and last are numbers relative to the beginning of this buffer, | 
 | 737 |  * so the first byte in the buffer is numbered 0 regardless of the | 
 | 738 |  * value of b_blkno. | 
 | 739 |  */ | 
 | 740 | void | 
 | 741 | xfs_trans_log_buf(xfs_trans_t	*tp, | 
 | 742 | 		  xfs_buf_t	*bp, | 
 | 743 | 		  uint		first, | 
 | 744 | 		  uint		last) | 
 | 745 | { | 
 | 746 | 	xfs_buf_log_item_t	*bip; | 
 | 747 | 	xfs_log_item_desc_t	*lidp; | 
 | 748 |  | 
 | 749 | 	ASSERT(XFS_BUF_ISBUSY(bp)); | 
 | 750 | 	ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); | 
 | 751 | 	ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL); | 
 | 752 | 	ASSERT((first <= last) && (last < XFS_BUF_COUNT(bp))); | 
 | 753 | 	ASSERT((XFS_BUF_IODONE_FUNC(bp) == NULL) || | 
 | 754 | 	       (XFS_BUF_IODONE_FUNC(bp) == xfs_buf_iodone_callbacks)); | 
 | 755 |  | 
 | 756 | 	/* | 
 | 757 | 	 * Mark the buffer as needing to be written out eventually, | 
 | 758 | 	 * and set its iodone function to remove the buffer's buf log | 
 | 759 | 	 * item from the AIL and free it when the buffer is flushed | 
 | 760 | 	 * to disk.  See xfs_buf_attach_iodone() for more details | 
 | 761 | 	 * on li_cb and xfs_buf_iodone_callbacks(). | 
 | 762 | 	 * If we end up aborting this transaction, we trap this buffer | 
 | 763 | 	 * inside the b_bdstrat callback so that this won't get written to | 
 | 764 | 	 * disk. | 
 | 765 | 	 */ | 
 | 766 | 	XFS_BUF_DELAYWRITE(bp); | 
 | 767 | 	XFS_BUF_DONE(bp); | 
 | 768 |  | 
 | 769 | 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); | 
 | 770 | 	ASSERT(atomic_read(&bip->bli_refcount) > 0); | 
 | 771 | 	XFS_BUF_SET_IODONE_FUNC(bp, xfs_buf_iodone_callbacks); | 
 | 772 | 	bip->bli_item.li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*))xfs_buf_iodone; | 
 | 773 |  | 
 | 774 | 	/* | 
 | 775 | 	 * If we invalidated the buffer within this transaction, then | 
 | 776 | 	 * cancel the invalidation now that we're dirtying the buffer | 
 | 777 | 	 * again.  There are no races with the code in xfs_buf_item_unpin(), | 
 | 778 | 	 * because we have a reference to the buffer this entire time. | 
 | 779 | 	 */ | 
 | 780 | 	if (bip->bli_flags & XFS_BLI_STALE) { | 
 | 781 | 		xfs_buf_item_trace("BLOG UNSTALE", bip); | 
 | 782 | 		bip->bli_flags &= ~XFS_BLI_STALE; | 
 | 783 | 		ASSERT(XFS_BUF_ISSTALE(bp)); | 
 | 784 | 		XFS_BUF_UNSTALE(bp); | 
 | 785 | 		bip->bli_format.blf_flags &= ~XFS_BLI_CANCEL; | 
 | 786 | 	} | 
 | 787 |  | 
 | 788 | 	lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip); | 
 | 789 | 	ASSERT(lidp != NULL); | 
 | 790 |  | 
 | 791 | 	tp->t_flags |= XFS_TRANS_DIRTY; | 
 | 792 | 	lidp->lid_flags |= XFS_LID_DIRTY; | 
 | 793 | 	lidp->lid_flags &= ~XFS_LID_BUF_STALE; | 
 | 794 | 	bip->bli_flags |= XFS_BLI_LOGGED; | 
 | 795 | 	xfs_buf_item_log(bip, first, last); | 
 | 796 | 	xfs_buf_item_trace("BLOG", bip); | 
 | 797 | } | 
 | 798 |  | 
 | 799 |  | 
 | 800 | /* | 
 | 801 |  * This called to invalidate a buffer that is being used within | 
 | 802 |  * a transaction.  Typically this is because the blocks in the | 
 | 803 |  * buffer are being freed, so we need to prevent it from being | 
 | 804 |  * written out when we're done.  Allowing it to be written again | 
 | 805 |  * might overwrite data in the free blocks if they are reallocated | 
 | 806 |  * to a file. | 
 | 807 |  * | 
 | 808 |  * We prevent the buffer from being written out by clearing the | 
 | 809 |  * B_DELWRI flag.  We can't always | 
 | 810 |  * get rid of the buf log item at this point, though, because | 
 | 811 |  * the buffer may still be pinned by another transaction.  If that | 
 | 812 |  * is the case, then we'll wait until the buffer is committed to | 
 | 813 |  * disk for the last time (we can tell by the ref count) and | 
 | 814 |  * free it in xfs_buf_item_unpin().  Until it is cleaned up we | 
 | 815 |  * will keep the buffer locked so that the buffer and buf log item | 
 | 816 |  * are not reused. | 
 | 817 |  */ | 
 | 818 | void | 
 | 819 | xfs_trans_binval( | 
 | 820 | 	xfs_trans_t	*tp, | 
 | 821 | 	xfs_buf_t	*bp) | 
 | 822 | { | 
 | 823 | 	xfs_log_item_desc_t	*lidp; | 
 | 824 | 	xfs_buf_log_item_t	*bip; | 
 | 825 |  | 
 | 826 | 	ASSERT(XFS_BUF_ISBUSY(bp)); | 
 | 827 | 	ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); | 
 | 828 | 	ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL); | 
 | 829 |  | 
 | 830 | 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); | 
 | 831 | 	lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip); | 
 | 832 | 	ASSERT(lidp != NULL); | 
 | 833 | 	ASSERT(atomic_read(&bip->bli_refcount) > 0); | 
 | 834 |  | 
 | 835 | 	if (bip->bli_flags & XFS_BLI_STALE) { | 
 | 836 | 		/* | 
 | 837 | 		 * If the buffer is already invalidated, then | 
 | 838 | 		 * just return. | 
 | 839 | 		 */ | 
 | 840 | 		ASSERT(!(XFS_BUF_ISDELAYWRITE(bp))); | 
 | 841 | 		ASSERT(XFS_BUF_ISSTALE(bp)); | 
 | 842 | 		ASSERT(!(bip->bli_flags & (XFS_BLI_LOGGED | XFS_BLI_DIRTY))); | 
 | 843 | 		ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_INODE_BUF)); | 
 | 844 | 		ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL); | 
 | 845 | 		ASSERT(lidp->lid_flags & XFS_LID_DIRTY); | 
 | 846 | 		ASSERT(tp->t_flags & XFS_TRANS_DIRTY); | 
 | 847 | 		xfs_buftrace("XFS_BINVAL RECUR", bp); | 
 | 848 | 		xfs_buf_item_trace("BINVAL RECUR", bip); | 
 | 849 | 		return; | 
 | 850 | 	} | 
 | 851 |  | 
 | 852 | 	/* | 
 | 853 | 	 * Clear the dirty bit in the buffer and set the STALE flag | 
 | 854 | 	 * in the buf log item.  The STALE flag will be used in | 
 | 855 | 	 * xfs_buf_item_unpin() to determine if it should clean up | 
 | 856 | 	 * when the last reference to the buf item is given up. | 
 | 857 | 	 * We set the XFS_BLI_CANCEL flag in the buf log format structure | 
 | 858 | 	 * and log the buf item.  This will be used at recovery time | 
 | 859 | 	 * to determine that copies of the buffer in the log before | 
 | 860 | 	 * this should not be replayed. | 
 | 861 | 	 * We mark the item descriptor and the transaction dirty so | 
 | 862 | 	 * that we'll hold the buffer until after the commit. | 
 | 863 | 	 * | 
 | 864 | 	 * Since we're invalidating the buffer, we also clear the state | 
 | 865 | 	 * about which parts of the buffer have been logged.  We also | 
 | 866 | 	 * clear the flag indicating that this is an inode buffer since | 
 | 867 | 	 * the data in the buffer will no longer be valid. | 
 | 868 | 	 * | 
 | 869 | 	 * We set the stale bit in the buffer as well since we're getting | 
 | 870 | 	 * rid of it. | 
 | 871 | 	 */ | 
 | 872 | 	XFS_BUF_UNDELAYWRITE(bp); | 
 | 873 | 	XFS_BUF_STALE(bp); | 
 | 874 | 	bip->bli_flags |= XFS_BLI_STALE; | 
 | 875 | 	bip->bli_flags &= ~(XFS_BLI_LOGGED | XFS_BLI_DIRTY); | 
 | 876 | 	bip->bli_format.blf_flags &= ~XFS_BLI_INODE_BUF; | 
 | 877 | 	bip->bli_format.blf_flags |= XFS_BLI_CANCEL; | 
 | 878 | 	memset((char *)(bip->bli_format.blf_data_map), 0, | 
 | 879 | 	      (bip->bli_format.blf_map_size * sizeof(uint))); | 
 | 880 | 	lidp->lid_flags |= XFS_LID_DIRTY|XFS_LID_BUF_STALE; | 
 | 881 | 	tp->t_flags |= XFS_TRANS_DIRTY; | 
 | 882 | 	xfs_buftrace("XFS_BINVAL", bp); | 
 | 883 | 	xfs_buf_item_trace("BINVAL", bip); | 
 | 884 | } | 
 | 885 |  | 
 | 886 | /* | 
 | 887 |  * This call is used to indicate that the buffer contains on-disk | 
 | 888 |  * inodes which must be handled specially during recovery.  They | 
 | 889 |  * require special handling because only the di_next_unlinked from | 
 | 890 |  * the inodes in the buffer should be recovered.  The rest of the | 
 | 891 |  * data in the buffer is logged via the inodes themselves. | 
 | 892 |  * | 
 | 893 |  * All we do is set the XFS_BLI_INODE_BUF flag in the buffer's log | 
 | 894 |  * format structure so that we'll know what to do at recovery time. | 
 | 895 |  */ | 
 | 896 | /* ARGSUSED */ | 
 | 897 | void | 
 | 898 | xfs_trans_inode_buf( | 
 | 899 | 	xfs_trans_t	*tp, | 
 | 900 | 	xfs_buf_t	*bp) | 
 | 901 | { | 
 | 902 | 	xfs_buf_log_item_t	*bip; | 
 | 903 |  | 
 | 904 | 	ASSERT(XFS_BUF_ISBUSY(bp)); | 
 | 905 | 	ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); | 
 | 906 | 	ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL); | 
 | 907 |  | 
 | 908 | 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); | 
 | 909 | 	ASSERT(atomic_read(&bip->bli_refcount) > 0); | 
 | 910 |  | 
 | 911 | 	bip->bli_format.blf_flags |= XFS_BLI_INODE_BUF; | 
 | 912 | } | 
 | 913 |  | 
 | 914 | /* | 
 | 915 |  * This call is used to indicate that the buffer is going to | 
 | 916 |  * be staled and was an inode buffer. This means it gets | 
 | 917 |  * special processing during unpin - where any inodes  | 
 | 918 |  * associated with the buffer should be removed from ail. | 
 | 919 |  * There is also special processing during recovery, | 
 | 920 |  * any replay of the inodes in the buffer needs to be | 
 | 921 |  * prevented as the buffer may have been reused. | 
 | 922 |  */ | 
 | 923 | void | 
 | 924 | xfs_trans_stale_inode_buf( | 
 | 925 | 	xfs_trans_t	*tp, | 
 | 926 | 	xfs_buf_t	*bp) | 
 | 927 | { | 
 | 928 | 	xfs_buf_log_item_t	*bip; | 
 | 929 |  | 
 | 930 | 	ASSERT(XFS_BUF_ISBUSY(bp)); | 
 | 931 | 	ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); | 
 | 932 | 	ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL); | 
 | 933 |  | 
 | 934 | 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); | 
 | 935 | 	ASSERT(atomic_read(&bip->bli_refcount) > 0); | 
 | 936 |  | 
 | 937 | 	bip->bli_flags |= XFS_BLI_STALE_INODE; | 
 | 938 | 	bip->bli_item.li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*)) | 
 | 939 | 		xfs_buf_iodone; | 
 | 940 | } | 
 | 941 |  | 
 | 942 |  | 
 | 943 |  | 
 | 944 | /* | 
 | 945 |  * Mark the buffer as being one which contains newly allocated | 
 | 946 |  * inodes.  We need to make sure that even if this buffer is | 
 | 947 |  * relogged as an 'inode buf' we still recover all of the inode | 
 | 948 |  * images in the face of a crash.  This works in coordination with | 
 | 949 |  * xfs_buf_item_committed() to ensure that the buffer remains in the | 
 | 950 |  * AIL at its original location even after it has been relogged. | 
 | 951 |  */ | 
 | 952 | /* ARGSUSED */ | 
 | 953 | void | 
 | 954 | xfs_trans_inode_alloc_buf( | 
 | 955 | 	xfs_trans_t	*tp, | 
 | 956 | 	xfs_buf_t	*bp) | 
 | 957 | { | 
 | 958 | 	xfs_buf_log_item_t	*bip; | 
 | 959 |  | 
 | 960 | 	ASSERT(XFS_BUF_ISBUSY(bp)); | 
 | 961 | 	ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); | 
 | 962 | 	ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL); | 
 | 963 |  | 
 | 964 | 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); | 
 | 965 | 	ASSERT(atomic_read(&bip->bli_refcount) > 0); | 
 | 966 |  | 
 | 967 | 	bip->bli_flags |= XFS_BLI_INODE_ALLOC_BUF; | 
 | 968 | } | 
 | 969 |  | 
 | 970 |  | 
 | 971 | /* | 
 | 972 |  * Similar to xfs_trans_inode_buf(), this marks the buffer as a cluster of | 
 | 973 |  * dquots. However, unlike in inode buffer recovery, dquot buffers get | 
 | 974 |  * recovered in their entirety. (Hence, no XFS_BLI_DQUOT_ALLOC_BUF flag). | 
 | 975 |  * The only thing that makes dquot buffers different from regular | 
 | 976 |  * buffers is that we must not replay dquot bufs when recovering | 
 | 977 |  * if a _corresponding_ quotaoff has happened. We also have to distinguish | 
 | 978 |  * between usr dquot bufs and grp dquot bufs, because usr and grp quotas | 
 | 979 |  * can be turned off independently. | 
 | 980 |  */ | 
 | 981 | /* ARGSUSED */ | 
 | 982 | void | 
 | 983 | xfs_trans_dquot_buf( | 
 | 984 | 	xfs_trans_t	*tp, | 
 | 985 | 	xfs_buf_t	*bp, | 
 | 986 | 	uint		type) | 
 | 987 | { | 
 | 988 | 	xfs_buf_log_item_t	*bip; | 
 | 989 |  | 
 | 990 | 	ASSERT(XFS_BUF_ISBUSY(bp)); | 
 | 991 | 	ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); | 
 | 992 | 	ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL); | 
 | 993 | 	ASSERT(type == XFS_BLI_UDQUOT_BUF || | 
| Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 994 | 	       type == XFS_BLI_PDQUOT_BUF || | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | 	       type == XFS_BLI_GDQUOT_BUF); | 
 | 996 |  | 
 | 997 | 	bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); | 
 | 998 | 	ASSERT(atomic_read(&bip->bli_refcount) > 0); | 
 | 999 |  | 
 | 1000 | 	bip->bli_format.blf_flags |= type; | 
 | 1001 | } | 
 | 1002 |  | 
 | 1003 | /* | 
 | 1004 |  * Check to see if a buffer matching the given parameters is already | 
 | 1005 |  * a part of the given transaction.  Only check the first, embedded | 
 | 1006 |  * chunk, since we don't want to spend all day scanning large transactions. | 
 | 1007 |  */ | 
 | 1008 | STATIC xfs_buf_t * | 
 | 1009 | xfs_trans_buf_item_match( | 
 | 1010 | 	xfs_trans_t	*tp, | 
 | 1011 | 	xfs_buftarg_t	*target, | 
 | 1012 | 	xfs_daddr_t	blkno, | 
 | 1013 | 	int		len) | 
 | 1014 | { | 
 | 1015 | 	xfs_log_item_chunk_t	*licp; | 
 | 1016 | 	xfs_log_item_desc_t	*lidp; | 
 | 1017 | 	xfs_buf_log_item_t	*blip; | 
 | 1018 | 	xfs_buf_t		*bp; | 
 | 1019 | 	int			i; | 
 | 1020 |  | 
 | 1021 | 	bp = NULL; | 
 | 1022 | 	len = BBTOB(len); | 
 | 1023 | 	licp = &tp->t_items; | 
| Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 1024 | 	if (!xfs_lic_are_all_free(licp)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | 		for (i = 0; i < licp->lic_unused; i++) { | 
 | 1026 | 			/* | 
 | 1027 | 			 * Skip unoccupied slots. | 
 | 1028 | 			 */ | 
| Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 1029 | 			if (xfs_lic_isfree(licp, i)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | 				continue; | 
 | 1031 | 			} | 
 | 1032 |  | 
| Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 1033 | 			lidp = xfs_lic_slot(licp, i); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | 			blip = (xfs_buf_log_item_t *)lidp->lid_item; | 
 | 1035 | 			if (blip->bli_item.li_type != XFS_LI_BUF) { | 
 | 1036 | 				continue; | 
 | 1037 | 			} | 
 | 1038 |  | 
 | 1039 | 			bp = blip->bli_buf; | 
 | 1040 | 			if ((XFS_BUF_TARGET(bp) == target) && | 
 | 1041 | 			    (XFS_BUF_ADDR(bp) == blkno) && | 
 | 1042 | 			    (XFS_BUF_COUNT(bp) == len)) { | 
 | 1043 | 				/* | 
 | 1044 | 				 * We found it.  Break out and | 
 | 1045 | 				 * return the pointer to the buffer. | 
 | 1046 | 				 */ | 
 | 1047 | 				break; | 
 | 1048 | 			} else { | 
 | 1049 | 				bp = NULL; | 
 | 1050 | 			} | 
 | 1051 | 		} | 
 | 1052 | 	} | 
 | 1053 | 	return bp; | 
 | 1054 | } | 
 | 1055 |  | 
 | 1056 | /* | 
 | 1057 |  * Check to see if a buffer matching the given parameters is already | 
 | 1058 |  * a part of the given transaction.  Check all the chunks, we | 
 | 1059 |  * want to be thorough. | 
 | 1060 |  */ | 
 | 1061 | STATIC xfs_buf_t * | 
 | 1062 | xfs_trans_buf_item_match_all( | 
 | 1063 | 	xfs_trans_t	*tp, | 
 | 1064 | 	xfs_buftarg_t	*target, | 
 | 1065 | 	xfs_daddr_t	blkno, | 
 | 1066 | 	int		len) | 
 | 1067 | { | 
 | 1068 | 	xfs_log_item_chunk_t	*licp; | 
 | 1069 | 	xfs_log_item_desc_t	*lidp; | 
 | 1070 | 	xfs_buf_log_item_t	*blip; | 
 | 1071 | 	xfs_buf_t		*bp; | 
 | 1072 | 	int			i; | 
 | 1073 |  | 
 | 1074 | 	bp = NULL; | 
 | 1075 | 	len = BBTOB(len); | 
 | 1076 | 	for (licp = &tp->t_items; licp != NULL; licp = licp->lic_next) { | 
| Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 1077 | 		if (xfs_lic_are_all_free(licp)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | 			ASSERT(licp == &tp->t_items); | 
 | 1079 | 			ASSERT(licp->lic_next == NULL); | 
 | 1080 | 			return NULL; | 
 | 1081 | 		} | 
 | 1082 | 		for (i = 0; i < licp->lic_unused; i++) { | 
 | 1083 | 			/* | 
 | 1084 | 			 * Skip unoccupied slots. | 
 | 1085 | 			 */ | 
| Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 1086 | 			if (xfs_lic_isfree(licp, i)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | 				continue; | 
 | 1088 | 			} | 
 | 1089 |  | 
| Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 1090 | 			lidp = xfs_lic_slot(licp, i); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | 			blip = (xfs_buf_log_item_t *)lidp->lid_item; | 
 | 1092 | 			if (blip->bli_item.li_type != XFS_LI_BUF) { | 
 | 1093 | 				continue; | 
 | 1094 | 			} | 
 | 1095 |  | 
 | 1096 | 			bp = blip->bli_buf; | 
 | 1097 | 			if ((XFS_BUF_TARGET(bp) == target) && | 
 | 1098 | 			    (XFS_BUF_ADDR(bp) == blkno) && | 
 | 1099 | 			    (XFS_BUF_COUNT(bp) == len)) { | 
 | 1100 | 				/* | 
 | 1101 | 				 * We found it.  Break out and | 
 | 1102 | 				 * return the pointer to the buffer. | 
 | 1103 | 				 */ | 
 | 1104 | 				return bp; | 
 | 1105 | 			} | 
 | 1106 | 		} | 
 | 1107 | 	} | 
 | 1108 | 	return NULL; | 
 | 1109 | } |