xref: /linux/fs/xfs/xfs_trans_buf.c (revision f2ee442115c9b6219083c019939a9cc0c9abb2f8)
1 /*
2  * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
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.
13  *
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
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_mount.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_alloc_btree.h"
30 #include "xfs_ialloc_btree.h"
31 #include "xfs_dinode.h"
32 #include "xfs_inode.h"
33 #include "xfs_buf_item.h"
34 #include "xfs_trans_priv.h"
35 #include "xfs_error.h"
36 #include "xfs_rw.h"
37 #include "xfs_trace.h"
38 
39 /*
40  * Check to see if a buffer matching the given parameters is already
41  * a part of the given transaction.
42  */
43 STATIC struct xfs_buf *
44 xfs_trans_buf_item_match(
45 	struct xfs_trans	*tp,
46 	struct xfs_buftarg	*target,
47 	xfs_daddr_t		blkno,
48 	int			len)
49 {
50 	struct xfs_log_item_desc *lidp;
51 	struct xfs_buf_log_item	*blip;
52 
53 	len = BBTOB(len);
54 	list_for_each_entry(lidp, &tp->t_items, lid_trans) {
55 		blip = (struct xfs_buf_log_item *)lidp->lid_item;
56 		if (blip->bli_item.li_type == XFS_LI_BUF &&
57 		    blip->bli_buf->b_target == target &&
58 		    XFS_BUF_ADDR(blip->bli_buf) == blkno &&
59 		    XFS_BUF_COUNT(blip->bli_buf) == len)
60 			return blip->bli_buf;
61 	}
62 
63 	return NULL;
64 }
65 
66 /*
67  * Add the locked buffer to the transaction.
68  *
69  * The buffer must be locked, and it cannot be associated with any
70  * transaction.
71  *
72  * If the buffer does not yet have a buf log item associated with it,
73  * then allocate one for it.  Then add the buf item to the transaction.
74  */
75 STATIC void
76 _xfs_trans_bjoin(
77 	struct xfs_trans	*tp,
78 	struct xfs_buf		*bp,
79 	int			reset_recur)
80 {
81 	struct xfs_buf_log_item	*bip;
82 
83 	ASSERT(bp->b_transp == NULL);
84 
85 	/*
86 	 * The xfs_buf_log_item pointer is stored in b_fsprivate.  If
87 	 * it doesn't have one yet, then allocate one and initialize it.
88 	 * The checks to see if one is there are in xfs_buf_item_init().
89 	 */
90 	xfs_buf_item_init(bp, tp->t_mountp);
91 	bip = bp->b_fspriv;
92 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
93 	ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_CANCEL));
94 	ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
95 	if (reset_recur)
96 		bip->bli_recur = 0;
97 
98 	/*
99 	 * Take a reference for this transaction on the buf item.
100 	 */
101 	atomic_inc(&bip->bli_refcount);
102 
103 	/*
104 	 * Get a log_item_desc to point at the new item.
105 	 */
106 	xfs_trans_add_item(tp, &bip->bli_item);
107 
108 	/*
109 	 * Initialize b_fsprivate2 so we can find it with incore_match()
110 	 * in xfs_trans_get_buf() and friends above.
111 	 */
112 	bp->b_transp = tp;
113 
114 }
115 
116 void
117 xfs_trans_bjoin(
118 	struct xfs_trans	*tp,
119 	struct xfs_buf		*bp)
120 {
121 	_xfs_trans_bjoin(tp, bp, 0);
122 	trace_xfs_trans_bjoin(bp->b_fspriv);
123 }
124 
125 /*
126  * Get and lock the buffer for the caller if it is not already
127  * locked within the given transaction.  If it is already locked
128  * within the transaction, just increment its lock recursion count
129  * and return a pointer to it.
130  *
131  * If the transaction pointer is NULL, make this just a normal
132  * get_buf() call.
133  */
134 xfs_buf_t *
135 xfs_trans_get_buf(xfs_trans_t	*tp,
136 		  xfs_buftarg_t	*target_dev,
137 		  xfs_daddr_t	blkno,
138 		  int		len,
139 		  uint		flags)
140 {
141 	xfs_buf_t		*bp;
142 	xfs_buf_log_item_t	*bip;
143 
144 	if (flags == 0)
145 		flags = XBF_LOCK | XBF_MAPPED;
146 
147 	/*
148 	 * Default to a normal get_buf() call if the tp is NULL.
149 	 */
150 	if (tp == NULL)
151 		return xfs_buf_get(target_dev, blkno, len,
152 				   flags | XBF_DONT_BLOCK);
153 
154 	/*
155 	 * If we find the buffer in the cache with this transaction
156 	 * pointer in its b_fsprivate2 field, then we know we already
157 	 * have it locked.  In this case we just increment the lock
158 	 * recursion count and return the buffer to the caller.
159 	 */
160 	bp = xfs_trans_buf_item_match(tp, target_dev, blkno, len);
161 	if (bp != NULL) {
162 		ASSERT(xfs_buf_islocked(bp));
163 		if (XFS_FORCED_SHUTDOWN(tp->t_mountp)) {
164 			xfs_buf_stale(bp);
165 			XFS_BUF_DONE(bp);
166 		}
167 
168 		/*
169 		 * If the buffer is stale then it was binval'ed
170 		 * since last read.  This doesn't matter since the
171 		 * caller isn't allowed to use the data anyway.
172 		 */
173 		else if (XFS_BUF_ISSTALE(bp))
174 			ASSERT(!XFS_BUF_ISDELAYWRITE(bp));
175 
176 		ASSERT(bp->b_transp == tp);
177 		bip = bp->b_fspriv;
178 		ASSERT(bip != NULL);
179 		ASSERT(atomic_read(&bip->bli_refcount) > 0);
180 		bip->bli_recur++;
181 		trace_xfs_trans_get_buf_recur(bip);
182 		return (bp);
183 	}
184 
185 	/*
186 	 * We always specify the XBF_DONT_BLOCK flag within a transaction
187 	 * so that get_buf does not try to push out a delayed write buffer
188 	 * which might cause another transaction to take place (if the
189 	 * buffer was delayed alloc).  Such recursive transactions can
190 	 * easily deadlock with our current transaction as well as cause
191 	 * us to run out of stack space.
192 	 */
193 	bp = xfs_buf_get(target_dev, blkno, len, flags | XBF_DONT_BLOCK);
194 	if (bp == NULL) {
195 		return NULL;
196 	}
197 
198 	ASSERT(!bp->b_error);
199 
200 	_xfs_trans_bjoin(tp, bp, 1);
201 	trace_xfs_trans_get_buf(bp->b_fspriv);
202 	return (bp);
203 }
204 
205 /*
206  * Get and lock the superblock buffer of this file system for the
207  * given transaction.
208  *
209  * We don't need to use incore_match() here, because the superblock
210  * buffer is a private buffer which we keep a pointer to in the
211  * mount structure.
212  */
213 xfs_buf_t *
214 xfs_trans_getsb(xfs_trans_t	*tp,
215 		struct xfs_mount *mp,
216 		int		flags)
217 {
218 	xfs_buf_t		*bp;
219 	xfs_buf_log_item_t	*bip;
220 
221 	/*
222 	 * Default to just trying to lock the superblock buffer
223 	 * if tp is NULL.
224 	 */
225 	if (tp == NULL) {
226 		return (xfs_getsb(mp, flags));
227 	}
228 
229 	/*
230 	 * If the superblock buffer already has this transaction
231 	 * pointer in its b_fsprivate2 field, then we know we already
232 	 * have it locked.  In this case we just increment the lock
233 	 * recursion count and return the buffer to the caller.
234 	 */
235 	bp = mp->m_sb_bp;
236 	if (bp->b_transp == tp) {
237 		bip = bp->b_fspriv;
238 		ASSERT(bip != NULL);
239 		ASSERT(atomic_read(&bip->bli_refcount) > 0);
240 		bip->bli_recur++;
241 		trace_xfs_trans_getsb_recur(bip);
242 		return (bp);
243 	}
244 
245 	bp = xfs_getsb(mp, flags);
246 	if (bp == NULL)
247 		return NULL;
248 
249 	_xfs_trans_bjoin(tp, bp, 1);
250 	trace_xfs_trans_getsb(bp->b_fspriv);
251 	return (bp);
252 }
253 
254 #ifdef DEBUG
255 xfs_buftarg_t *xfs_error_target;
256 int	xfs_do_error;
257 int	xfs_req_num;
258 int	xfs_error_mod = 33;
259 #endif
260 
261 /*
262  * Get and lock the buffer for the caller if it is not already
263  * locked within the given transaction.  If it has not yet been
264  * read in, read it from disk. If it is already locked
265  * within the transaction and already read in, just increment its
266  * lock recursion count and return a pointer to it.
267  *
268  * If the transaction pointer is NULL, make this just a normal
269  * read_buf() call.
270  */
271 int
272 xfs_trans_read_buf(
273 	xfs_mount_t	*mp,
274 	xfs_trans_t	*tp,
275 	xfs_buftarg_t	*target,
276 	xfs_daddr_t	blkno,
277 	int		len,
278 	uint		flags,
279 	xfs_buf_t	**bpp)
280 {
281 	xfs_buf_t		*bp;
282 	xfs_buf_log_item_t	*bip;
283 	int			error;
284 
285 	if (flags == 0)
286 		flags = XBF_LOCK | XBF_MAPPED;
287 
288 	/*
289 	 * Default to a normal get_buf() call if the tp is NULL.
290 	 */
291 	if (tp == NULL) {
292 		bp = xfs_buf_read(target, blkno, len, flags | XBF_DONT_BLOCK);
293 		if (!bp)
294 			return (flags & XBF_TRYLOCK) ?
295 					EAGAIN : XFS_ERROR(ENOMEM);
296 
297 		if (bp->b_error) {
298 			error = bp->b_error;
299 			xfs_buf_ioerror_alert(bp, __func__);
300 			xfs_buf_relse(bp);
301 			return error;
302 		}
303 #ifdef DEBUG
304 		if (xfs_do_error) {
305 			if (xfs_error_target == target) {
306 				if (((xfs_req_num++) % xfs_error_mod) == 0) {
307 					xfs_buf_relse(bp);
308 					xfs_debug(mp, "Returning error!");
309 					return XFS_ERROR(EIO);
310 				}
311 			}
312 		}
313 #endif
314 		if (XFS_FORCED_SHUTDOWN(mp))
315 			goto shutdown_abort;
316 		*bpp = bp;
317 		return 0;
318 	}
319 
320 	/*
321 	 * If we find the buffer in the cache with this transaction
322 	 * pointer in its b_fsprivate2 field, then we know we already
323 	 * have it locked.  If it is already read in we just increment
324 	 * the lock recursion count and return the buffer to the caller.
325 	 * If the buffer is not yet read in, then we read it in, increment
326 	 * the lock recursion count, and return it to the caller.
327 	 */
328 	bp = xfs_trans_buf_item_match(tp, target, blkno, len);
329 	if (bp != NULL) {
330 		ASSERT(xfs_buf_islocked(bp));
331 		ASSERT(bp->b_transp == tp);
332 		ASSERT(bp->b_fspriv != NULL);
333 		ASSERT(!bp->b_error);
334 		if (!(XFS_BUF_ISDONE(bp))) {
335 			trace_xfs_trans_read_buf_io(bp, _RET_IP_);
336 			ASSERT(!XFS_BUF_ISASYNC(bp));
337 			XFS_BUF_READ(bp);
338 			xfsbdstrat(tp->t_mountp, bp);
339 			error = xfs_buf_iowait(bp);
340 			if (error) {
341 				xfs_buf_ioerror_alert(bp, __func__);
342 				xfs_buf_relse(bp);
343 				/*
344 				 * We can gracefully recover from most read
345 				 * errors. Ones we can't are those that happen
346 				 * after the transaction's already dirty.
347 				 */
348 				if (tp->t_flags & XFS_TRANS_DIRTY)
349 					xfs_force_shutdown(tp->t_mountp,
350 							SHUTDOWN_META_IO_ERROR);
351 				return error;
352 			}
353 		}
354 		/*
355 		 * We never locked this buf ourselves, so we shouldn't
356 		 * brelse it either. Just get out.
357 		 */
358 		if (XFS_FORCED_SHUTDOWN(mp)) {
359 			trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
360 			*bpp = NULL;
361 			return XFS_ERROR(EIO);
362 		}
363 
364 
365 		bip = bp->b_fspriv;
366 		bip->bli_recur++;
367 
368 		ASSERT(atomic_read(&bip->bli_refcount) > 0);
369 		trace_xfs_trans_read_buf_recur(bip);
370 		*bpp = bp;
371 		return 0;
372 	}
373 
374 	/*
375 	 * We always specify the XBF_DONT_BLOCK flag within a transaction
376 	 * so that get_buf does not try to push out a delayed write buffer
377 	 * which might cause another transaction to take place (if the
378 	 * buffer was delayed alloc).  Such recursive transactions can
379 	 * easily deadlock with our current transaction as well as cause
380 	 * us to run out of stack space.
381 	 */
382 	bp = xfs_buf_read(target, blkno, len, flags | XBF_DONT_BLOCK);
383 	if (bp == NULL) {
384 		*bpp = NULL;
385 		return (flags & XBF_TRYLOCK) ?
386 					0 : XFS_ERROR(ENOMEM);
387 	}
388 	if (bp->b_error) {
389 		error = bp->b_error;
390 		xfs_buf_stale(bp);
391 		XFS_BUF_DONE(bp);
392 		xfs_buf_ioerror_alert(bp, __func__);
393 		if (tp->t_flags & XFS_TRANS_DIRTY)
394 			xfs_force_shutdown(tp->t_mountp, SHUTDOWN_META_IO_ERROR);
395 		xfs_buf_relse(bp);
396 		return error;
397 	}
398 #ifdef DEBUG
399 	if (xfs_do_error && !(tp->t_flags & XFS_TRANS_DIRTY)) {
400 		if (xfs_error_target == target) {
401 			if (((xfs_req_num++) % xfs_error_mod) == 0) {
402 				xfs_force_shutdown(tp->t_mountp,
403 						   SHUTDOWN_META_IO_ERROR);
404 				xfs_buf_relse(bp);
405 				xfs_debug(mp, "Returning trans error!");
406 				return XFS_ERROR(EIO);
407 			}
408 		}
409 	}
410 #endif
411 	if (XFS_FORCED_SHUTDOWN(mp))
412 		goto shutdown_abort;
413 
414 	_xfs_trans_bjoin(tp, bp, 1);
415 	trace_xfs_trans_read_buf(bp->b_fspriv);
416 
417 	*bpp = bp;
418 	return 0;
419 
420 shutdown_abort:
421 	/*
422 	 * the theory here is that buffer is good but we're
423 	 * bailing out because the filesystem is being forcibly
424 	 * shut down.  So we should leave the b_flags alone since
425 	 * the buffer's not staled and just get out.
426 	 */
427 #if defined(DEBUG)
428 	if (XFS_BUF_ISSTALE(bp) && XFS_BUF_ISDELAYWRITE(bp))
429 		xfs_notice(mp, "about to pop assert, bp == 0x%p", bp);
430 #endif
431 	ASSERT((bp->b_flags & (XBF_STALE|XBF_DELWRI)) !=
432 				     (XBF_STALE|XBF_DELWRI));
433 
434 	trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
435 	xfs_buf_relse(bp);
436 	*bpp = NULL;
437 	return XFS_ERROR(EIO);
438 }
439 
440 
441 /*
442  * Release the buffer bp which was previously acquired with one of the
443  * xfs_trans_... buffer allocation routines if the buffer has not
444  * been modified within this transaction.  If the buffer is modified
445  * within this transaction, do decrement the recursion count but do
446  * not release the buffer even if the count goes to 0.  If the buffer is not
447  * modified within the transaction, decrement the recursion count and
448  * release the buffer if the recursion count goes to 0.
449  *
450  * If the buffer is to be released and it was not modified before
451  * this transaction began, then free the buf_log_item associated with it.
452  *
453  * If the transaction pointer is NULL, make this just a normal
454  * brelse() call.
455  */
456 void
457 xfs_trans_brelse(xfs_trans_t	*tp,
458 		 xfs_buf_t	*bp)
459 {
460 	xfs_buf_log_item_t	*bip;
461 
462 	/*
463 	 * Default to a normal brelse() call if the tp is NULL.
464 	 */
465 	if (tp == NULL) {
466 		struct xfs_log_item	*lip = bp->b_fspriv;
467 
468 		ASSERT(bp->b_transp == NULL);
469 
470 		/*
471 		 * If there's a buf log item attached to the buffer,
472 		 * then let the AIL know that the buffer is being
473 		 * unlocked.
474 		 */
475 		if (lip != NULL && lip->li_type == XFS_LI_BUF) {
476 			bip = bp->b_fspriv;
477 			xfs_trans_unlocked_item(bip->bli_item.li_ailp, lip);
478 		}
479 		xfs_buf_relse(bp);
480 		return;
481 	}
482 
483 	ASSERT(bp->b_transp == tp);
484 	bip = bp->b_fspriv;
485 	ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
486 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
487 	ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_CANCEL));
488 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
489 
490 	trace_xfs_trans_brelse(bip);
491 
492 	/*
493 	 * If the release is just for a recursive lock,
494 	 * then decrement the count and return.
495 	 */
496 	if (bip->bli_recur > 0) {
497 		bip->bli_recur--;
498 		return;
499 	}
500 
501 	/*
502 	 * If the buffer is dirty within this transaction, we can't
503 	 * release it until we commit.
504 	 */
505 	if (bip->bli_item.li_desc->lid_flags & XFS_LID_DIRTY)
506 		return;
507 
508 	/*
509 	 * If the buffer has been invalidated, then we can't release
510 	 * it until the transaction commits to disk unless it is re-dirtied
511 	 * as part of this transaction.  This prevents us from pulling
512 	 * the item from the AIL before we should.
513 	 */
514 	if (bip->bli_flags & XFS_BLI_STALE)
515 		return;
516 
517 	ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
518 
519 	/*
520 	 * Free up the log item descriptor tracking the released item.
521 	 */
522 	xfs_trans_del_item(&bip->bli_item);
523 
524 	/*
525 	 * Clear the hold flag in the buf log item if it is set.
526 	 * We wouldn't want the next user of the buffer to
527 	 * get confused.
528 	 */
529 	if (bip->bli_flags & XFS_BLI_HOLD) {
530 		bip->bli_flags &= ~XFS_BLI_HOLD;
531 	}
532 
533 	/*
534 	 * Drop our reference to the buf log item.
535 	 */
536 	atomic_dec(&bip->bli_refcount);
537 
538 	/*
539 	 * If the buf item is not tracking data in the log, then
540 	 * we must free it before releasing the buffer back to the
541 	 * free pool.  Before releasing the buffer to the free pool,
542 	 * clear the transaction pointer in b_fsprivate2 to dissolve
543 	 * its relation to this transaction.
544 	 */
545 	if (!xfs_buf_item_dirty(bip)) {
546 /***
547 		ASSERT(bp->b_pincount == 0);
548 ***/
549 		ASSERT(atomic_read(&bip->bli_refcount) == 0);
550 		ASSERT(!(bip->bli_item.li_flags & XFS_LI_IN_AIL));
551 		ASSERT(!(bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF));
552 		xfs_buf_item_relse(bp);
553 		bip = NULL;
554 	}
555 	bp->b_transp = NULL;
556 
557 	/*
558 	 * If we've still got a buf log item on the buffer, then
559 	 * tell the AIL that the buffer is being unlocked.
560 	 */
561 	if (bip != NULL) {
562 		xfs_trans_unlocked_item(bip->bli_item.li_ailp,
563 					(xfs_log_item_t*)bip);
564 	}
565 
566 	xfs_buf_relse(bp);
567 	return;
568 }
569 
570 /*
571  * Mark the buffer as not needing to be unlocked when the buf item's
572  * IOP_UNLOCK() routine is called.  The buffer must already be locked
573  * and associated with the given transaction.
574  */
575 /* ARGSUSED */
576 void
577 xfs_trans_bhold(xfs_trans_t	*tp,
578 		xfs_buf_t	*bp)
579 {
580 	xfs_buf_log_item_t	*bip = bp->b_fspriv;
581 
582 	ASSERT(bp->b_transp == tp);
583 	ASSERT(bip != NULL);
584 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
585 	ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_CANCEL));
586 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
587 
588 	bip->bli_flags |= XFS_BLI_HOLD;
589 	trace_xfs_trans_bhold(bip);
590 }
591 
592 /*
593  * Cancel the previous buffer hold request made on this buffer
594  * for this transaction.
595  */
596 void
597 xfs_trans_bhold_release(xfs_trans_t	*tp,
598 			xfs_buf_t	*bp)
599 {
600 	xfs_buf_log_item_t	*bip = bp->b_fspriv;
601 
602 	ASSERT(bp->b_transp == tp);
603 	ASSERT(bip != NULL);
604 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
605 	ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_CANCEL));
606 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
607 	ASSERT(bip->bli_flags & XFS_BLI_HOLD);
608 
609 	bip->bli_flags &= ~XFS_BLI_HOLD;
610 	trace_xfs_trans_bhold_release(bip);
611 }
612 
613 /*
614  * This is called to mark bytes first through last inclusive of the given
615  * buffer as needing to be logged when the transaction is committed.
616  * The buffer must already be associated with the given transaction.
617  *
618  * First and last are numbers relative to the beginning of this buffer,
619  * so the first byte in the buffer is numbered 0 regardless of the
620  * value of b_blkno.
621  */
622 void
623 xfs_trans_log_buf(xfs_trans_t	*tp,
624 		  xfs_buf_t	*bp,
625 		  uint		first,
626 		  uint		last)
627 {
628 	xfs_buf_log_item_t	*bip = bp->b_fspriv;
629 
630 	ASSERT(bp->b_transp == tp);
631 	ASSERT(bip != NULL);
632 	ASSERT((first <= last) && (last < XFS_BUF_COUNT(bp)));
633 	ASSERT(bp->b_iodone == NULL ||
634 	       bp->b_iodone == xfs_buf_iodone_callbacks);
635 
636 	/*
637 	 * Mark the buffer as needing to be written out eventually,
638 	 * and set its iodone function to remove the buffer's buf log
639 	 * item from the AIL and free it when the buffer is flushed
640 	 * to disk.  See xfs_buf_attach_iodone() for more details
641 	 * on li_cb and xfs_buf_iodone_callbacks().
642 	 * If we end up aborting this transaction, we trap this buffer
643 	 * inside the b_bdstrat callback so that this won't get written to
644 	 * disk.
645 	 */
646 	XFS_BUF_DONE(bp);
647 
648 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
649 	bp->b_iodone = xfs_buf_iodone_callbacks;
650 	bip->bli_item.li_cb = xfs_buf_iodone;
651 
652 	xfs_buf_delwri_queue(bp);
653 
654 	trace_xfs_trans_log_buf(bip);
655 
656 	/*
657 	 * If we invalidated the buffer within this transaction, then
658 	 * cancel the invalidation now that we're dirtying the buffer
659 	 * again.  There are no races with the code in xfs_buf_item_unpin(),
660 	 * because we have a reference to the buffer this entire time.
661 	 */
662 	if (bip->bli_flags & XFS_BLI_STALE) {
663 		bip->bli_flags &= ~XFS_BLI_STALE;
664 		ASSERT(XFS_BUF_ISSTALE(bp));
665 		XFS_BUF_UNSTALE(bp);
666 		bip->bli_format.blf_flags &= ~XFS_BLF_CANCEL;
667 	}
668 
669 	tp->t_flags |= XFS_TRANS_DIRTY;
670 	bip->bli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
671 	bip->bli_flags |= XFS_BLI_LOGGED;
672 	xfs_buf_item_log(bip, first, last);
673 }
674 
675 
676 /*
677  * This called to invalidate a buffer that is being used within
678  * a transaction.  Typically this is because the blocks in the
679  * buffer are being freed, so we need to prevent it from being
680  * written out when we're done.  Allowing it to be written again
681  * might overwrite data in the free blocks if they are reallocated
682  * to a file.
683  *
684  * We prevent the buffer from being written out by clearing the
685  * B_DELWRI flag.  We can't always
686  * get rid of the buf log item at this point, though, because
687  * the buffer may still be pinned by another transaction.  If that
688  * is the case, then we'll wait until the buffer is committed to
689  * disk for the last time (we can tell by the ref count) and
690  * free it in xfs_buf_item_unpin().  Until it is cleaned up we
691  * will keep the buffer locked so that the buffer and buf log item
692  * are not reused.
693  */
694 void
695 xfs_trans_binval(
696 	xfs_trans_t	*tp,
697 	xfs_buf_t	*bp)
698 {
699 	xfs_buf_log_item_t	*bip = bp->b_fspriv;
700 
701 	ASSERT(bp->b_transp == tp);
702 	ASSERT(bip != NULL);
703 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
704 
705 	trace_xfs_trans_binval(bip);
706 
707 	if (bip->bli_flags & XFS_BLI_STALE) {
708 		/*
709 		 * If the buffer is already invalidated, then
710 		 * just return.
711 		 */
712 		ASSERT(!(XFS_BUF_ISDELAYWRITE(bp)));
713 		ASSERT(XFS_BUF_ISSTALE(bp));
714 		ASSERT(!(bip->bli_flags & (XFS_BLI_LOGGED | XFS_BLI_DIRTY)));
715 		ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_INODE_BUF));
716 		ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL);
717 		ASSERT(bip->bli_item.li_desc->lid_flags & XFS_LID_DIRTY);
718 		ASSERT(tp->t_flags & XFS_TRANS_DIRTY);
719 		return;
720 	}
721 
722 	/*
723 	 * Clear the dirty bit in the buffer and set the STALE flag
724 	 * in the buf log item.  The STALE flag will be used in
725 	 * xfs_buf_item_unpin() to determine if it should clean up
726 	 * when the last reference to the buf item is given up.
727 	 * We set the XFS_BLF_CANCEL flag in the buf log format structure
728 	 * and log the buf item.  This will be used at recovery time
729 	 * to determine that copies of the buffer in the log before
730 	 * this should not be replayed.
731 	 * We mark the item descriptor and the transaction dirty so
732 	 * that we'll hold the buffer until after the commit.
733 	 *
734 	 * Since we're invalidating the buffer, we also clear the state
735 	 * about which parts of the buffer have been logged.  We also
736 	 * clear the flag indicating that this is an inode buffer since
737 	 * the data in the buffer will no longer be valid.
738 	 *
739 	 * We set the stale bit in the buffer as well since we're getting
740 	 * rid of it.
741 	 */
742 	xfs_buf_stale(bp);
743 	bip->bli_flags |= XFS_BLI_STALE;
744 	bip->bli_flags &= ~(XFS_BLI_INODE_BUF | XFS_BLI_LOGGED | XFS_BLI_DIRTY);
745 	bip->bli_format.blf_flags &= ~XFS_BLF_INODE_BUF;
746 	bip->bli_format.blf_flags |= XFS_BLF_CANCEL;
747 	memset((char *)(bip->bli_format.blf_data_map), 0,
748 	      (bip->bli_format.blf_map_size * sizeof(uint)));
749 	bip->bli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
750 	tp->t_flags |= XFS_TRANS_DIRTY;
751 }
752 
753 /*
754  * This call is used to indicate that the buffer contains on-disk inodes which
755  * must be handled specially during recovery.  They require special handling
756  * because only the di_next_unlinked from the inodes in the buffer should be
757  * recovered.  The rest of the data in the buffer is logged via the inodes
758  * themselves.
759  *
760  * All we do is set the XFS_BLI_INODE_BUF flag in the items flags so it can be
761  * transferred to the buffer's log format structure so that we'll know what to
762  * do at recovery time.
763  */
764 void
765 xfs_trans_inode_buf(
766 	xfs_trans_t	*tp,
767 	xfs_buf_t	*bp)
768 {
769 	xfs_buf_log_item_t	*bip = bp->b_fspriv;
770 
771 	ASSERT(bp->b_transp == tp);
772 	ASSERT(bip != NULL);
773 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
774 
775 	bip->bli_flags |= XFS_BLI_INODE_BUF;
776 }
777 
778 /*
779  * This call is used to indicate that the buffer is going to
780  * be staled and was an inode buffer. This means it gets
781  * special processing during unpin - where any inodes
782  * associated with the buffer should be removed from ail.
783  * There is also special processing during recovery,
784  * any replay of the inodes in the buffer needs to be
785  * prevented as the buffer may have been reused.
786  */
787 void
788 xfs_trans_stale_inode_buf(
789 	xfs_trans_t	*tp,
790 	xfs_buf_t	*bp)
791 {
792 	xfs_buf_log_item_t	*bip = bp->b_fspriv;
793 
794 	ASSERT(bp->b_transp == tp);
795 	ASSERT(bip != NULL);
796 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
797 
798 	bip->bli_flags |= XFS_BLI_STALE_INODE;
799 	bip->bli_item.li_cb = xfs_buf_iodone;
800 }
801 
802 /*
803  * Mark the buffer as being one which contains newly allocated
804  * inodes.  We need to make sure that even if this buffer is
805  * relogged as an 'inode buf' we still recover all of the inode
806  * images in the face of a crash.  This works in coordination with
807  * xfs_buf_item_committed() to ensure that the buffer remains in the
808  * AIL at its original location even after it has been relogged.
809  */
810 /* ARGSUSED */
811 void
812 xfs_trans_inode_alloc_buf(
813 	xfs_trans_t	*tp,
814 	xfs_buf_t	*bp)
815 {
816 	xfs_buf_log_item_t	*bip = bp->b_fspriv;
817 
818 	ASSERT(bp->b_transp == tp);
819 	ASSERT(bip != NULL);
820 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
821 
822 	bip->bli_flags |= XFS_BLI_INODE_ALLOC_BUF;
823 }
824 
825 
826 /*
827  * Similar to xfs_trans_inode_buf(), this marks the buffer as a cluster of
828  * dquots. However, unlike in inode buffer recovery, dquot buffers get
829  * recovered in their entirety. (Hence, no XFS_BLI_DQUOT_ALLOC_BUF flag).
830  * The only thing that makes dquot buffers different from regular
831  * buffers is that we must not replay dquot bufs when recovering
832  * if a _corresponding_ quotaoff has happened. We also have to distinguish
833  * between usr dquot bufs and grp dquot bufs, because usr and grp quotas
834  * can be turned off independently.
835  */
836 /* ARGSUSED */
837 void
838 xfs_trans_dquot_buf(
839 	xfs_trans_t	*tp,
840 	xfs_buf_t	*bp,
841 	uint		type)
842 {
843 	xfs_buf_log_item_t	*bip = bp->b_fspriv;
844 
845 	ASSERT(bp->b_transp == tp);
846 	ASSERT(bip != NULL);
847 	ASSERT(type == XFS_BLF_UDQUOT_BUF ||
848 	       type == XFS_BLF_PDQUOT_BUF ||
849 	       type == XFS_BLF_GDQUOT_BUF);
850 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
851 
852 	bip->bli_format.blf_flags |= type;
853 }
854