xfs_trans_buf.c 21.9 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2 3
 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
 * All Rights Reserved.
L
Linus Torvalds 已提交
4
 *
5 6
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
L
Linus Torvalds 已提交
7 8
 * published by the Free Software Foundation.
 *
9 10 11 12
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
L
Linus Torvalds 已提交
13
 *
14 15 16
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
L
Linus Torvalds 已提交
17 18
 */
#include "xfs.h"
19
#include "xfs_fs.h"
20
#include "xfs_shared.h"
21
#include "xfs_format.h"
22 23
#include "xfs_log_format.h"
#include "xfs_trans_resv.h"
L
Linus Torvalds 已提交
24
#include "xfs_mount.h"
25
#include "xfs_inode.h"
26
#include "xfs_trans.h"
27
#include "xfs_buf_item.h"
L
Linus Torvalds 已提交
28 29
#include "xfs_trans_priv.h"
#include "xfs_error.h"
C
Christoph Hellwig 已提交
30
#include "xfs_trace.h"
L
Linus Torvalds 已提交
31

32 33 34 35 36 37 38 39
/*
 * Check to see if a buffer matching the given parameters is already
 * a part of the given transaction.
 */
STATIC struct xfs_buf *
xfs_trans_buf_item_match(
	struct xfs_trans	*tp,
	struct xfs_buftarg	*target,
40 41
	struct xfs_buf_map	*map,
	int			nmaps)
42
{
43 44
	struct xfs_log_item_desc *lidp;
	struct xfs_buf_log_item	*blip;
45 46 47 48 49
	int			len = 0;
	int			i;

	for (i = 0; i < nmaps; i++)
		len += map[i].bm_len;
L
Linus Torvalds 已提交
50

51 52 53
	list_for_each_entry(lidp, &tp->t_items, lid_trans) {
		blip = (struct xfs_buf_log_item *)lidp->lid_item;
		if (blip->bli_item.li_type == XFS_LI_BUF &&
54
		    blip->bli_buf->b_target == target &&
55 56 57
		    XFS_BUF_ADDR(blip->bli_buf) == map[0].bm_bn &&
		    blip->bli_buf->b_length == len) {
			ASSERT(blip->bli_buf->b_map_count == nmaps);
58
			return blip->bli_buf;
59
		}
60 61 62 63
	}

	return NULL;
}
L
Linus Torvalds 已提交
64

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
/*
 * Add the locked buffer to the transaction.
 *
 * The buffer must be locked, and it cannot be associated with any
 * transaction.
 *
 * If the buffer does not yet have a buf log item associated with it,
 * then allocate one for it.  Then add the buf item to the transaction.
 */
STATIC void
_xfs_trans_bjoin(
	struct xfs_trans	*tp,
	struct xfs_buf		*bp,
	int			reset_recur)
{
	struct xfs_buf_log_item	*bip;

82
	ASSERT(bp->b_transp == NULL);
83 84 85 86 87 88 89

	/*
	 * The xfs_buf_log_item pointer is stored in b_fsprivate.  If
	 * it doesn't have one yet, then allocate one and initialize it.
	 * The checks to see if one is there are in xfs_buf_item_init().
	 */
	xfs_buf_item_init(bp, tp->t_mountp);
90
	bip = bp->b_fspriv;
91
	ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
92
	ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
93 94 95 96 97 98 99 100 101 102 103 104
	ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
	if (reset_recur)
		bip->bli_recur = 0;

	/*
	 * Take a reference for this transaction on the buf item.
	 */
	atomic_inc(&bip->bli_refcount);

	/*
	 * Get a log_item_desc to point at the new item.
	 */
105
	xfs_trans_add_item(tp, &bip->bli_item);
106 107 108 109 110

	/*
	 * Initialize b_fsprivate2 so we can find it with incore_match()
	 * in xfs_trans_get_buf() and friends above.
	 */
111
	bp->b_transp = tp;
112 113 114 115 116 117 118 119 120 121 122

}

void
xfs_trans_bjoin(
	struct xfs_trans	*tp,
	struct xfs_buf		*bp)
{
	_xfs_trans_bjoin(tp, bp, 0);
	trace_xfs_trans_bjoin(bp->b_fspriv);
}
L
Linus Torvalds 已提交
123 124 125 126 127 128 129 130 131 132

/*
 * Get and lock the buffer for the caller if it is not already
 * locked within the given transaction.  If it is already locked
 * within the transaction, just increment its lock recursion count
 * and return a pointer to it.
 *
 * If the transaction pointer is NULL, make this just a normal
 * get_buf() call.
 */
133 134 135 136 137 138 139
struct xfs_buf *
xfs_trans_get_buf_map(
	struct xfs_trans	*tp,
	struct xfs_buftarg	*target,
	struct xfs_buf_map	*map,
	int			nmaps,
	xfs_buf_flags_t		flags)
L
Linus Torvalds 已提交
140 141 142 143
{
	xfs_buf_t		*bp;
	xfs_buf_log_item_t	*bip;

144 145
	if (!tp)
		return xfs_buf_get_map(target, map, nmaps, flags);
L
Linus Torvalds 已提交
146 147 148 149 150 151 152

	/*
	 * If we find the buffer in the cache with this transaction
	 * pointer in its b_fsprivate2 field, then we know we already
	 * have it locked.  In this case we just increment the lock
	 * recursion count and return the buffer to the caller.
	 */
153
	bp = xfs_trans_buf_item_match(tp, target, map, nmaps);
L
Linus Torvalds 已提交
154
	if (bp != NULL) {
155
		ASSERT(xfs_buf_islocked(bp));
156 157 158 159
		if (XFS_FORCED_SHUTDOWN(tp->t_mountp)) {
			xfs_buf_stale(bp);
			XFS_BUF_DONE(bp);
		}
C
Christoph Hellwig 已提交
160

161
		ASSERT(bp->b_transp == tp);
162
		bip = bp->b_fspriv;
L
Linus Torvalds 已提交
163 164 165
		ASSERT(bip != NULL);
		ASSERT(atomic_read(&bip->bli_refcount) > 0);
		bip->bli_recur++;
C
Christoph Hellwig 已提交
166
		trace_xfs_trans_get_buf_recur(bip);
E
Eric Sandeen 已提交
167
		return bp;
L
Linus Torvalds 已提交
168 169
	}

170
	bp = xfs_buf_get_map(target, map, nmaps, flags);
L
Linus Torvalds 已提交
171 172 173 174
	if (bp == NULL) {
		return NULL;
	}

175
	ASSERT(!bp->b_error);
L
Linus Torvalds 已提交
176

177 178
	_xfs_trans_bjoin(tp, bp, 1);
	trace_xfs_trans_get_buf(bp->b_fspriv);
E
Eric Sandeen 已提交
179
	return bp;
L
Linus Torvalds 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
}

/*
 * Get and lock the superblock buffer of this file system for the
 * given transaction.
 *
 * We don't need to use incore_match() here, because the superblock
 * buffer is a private buffer which we keep a pointer to in the
 * mount structure.
 */
xfs_buf_t *
xfs_trans_getsb(xfs_trans_t	*tp,
		struct xfs_mount *mp,
		int		flags)
{
	xfs_buf_t		*bp;
	xfs_buf_log_item_t	*bip;

	/*
	 * Default to just trying to lock the superblock buffer
	 * if tp is NULL.
	 */
E
Eric Sandeen 已提交
202 203
	if (tp == NULL)
		return xfs_getsb(mp, flags);
L
Linus Torvalds 已提交
204 205 206 207 208 209 210 211

	/*
	 * If the superblock buffer already has this transaction
	 * pointer in its b_fsprivate2 field, then we know we already
	 * have it locked.  In this case we just increment the lock
	 * recursion count and return the buffer to the caller.
	 */
	bp = mp->m_sb_bp;
212
	if (bp->b_transp == tp) {
213
		bip = bp->b_fspriv;
L
Linus Torvalds 已提交
214 215 216
		ASSERT(bip != NULL);
		ASSERT(atomic_read(&bip->bli_refcount) > 0);
		bip->bli_recur++;
C
Christoph Hellwig 已提交
217
		trace_xfs_trans_getsb_recur(bip);
E
Eric Sandeen 已提交
218
		return bp;
L
Linus Torvalds 已提交
219 220 221
	}

	bp = xfs_getsb(mp, flags);
222
	if (bp == NULL)
L
Linus Torvalds 已提交
223 224
		return NULL;

225 226
	_xfs_trans_bjoin(tp, bp, 1);
	trace_xfs_trans_getsb(bp->b_fspriv);
E
Eric Sandeen 已提交
227
	return bp;
L
Linus Torvalds 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240
}

/*
 * Get and lock the buffer for the caller if it is not already
 * locked within the given transaction.  If it has not yet been
 * read in, read it from disk. If it is already locked
 * within the transaction and already read in, just increment its
 * lock recursion count and return a pointer to it.
 *
 * If the transaction pointer is NULL, make this just a normal
 * read_buf() call.
 */
int
241 242 243 244 245 246 247
xfs_trans_read_buf_map(
	struct xfs_mount	*mp,
	struct xfs_trans	*tp,
	struct xfs_buftarg	*target,
	struct xfs_buf_map	*map,
	int			nmaps,
	xfs_buf_flags_t		flags,
248
	struct xfs_buf		**bpp,
249
	const struct xfs_buf_ops *ops)
L
Linus Torvalds 已提交
250
{
251 252
	struct xfs_buf		*bp = NULL;
	struct xfs_buf_log_item	*bip;
L
Linus Torvalds 已提交
253 254
	int			error;

D
Dave Chinner 已提交
255
	*bpp = NULL;
L
Linus Torvalds 已提交
256 257 258 259 260 261 262 263
	/*
	 * If we find the buffer in the cache with this transaction
	 * pointer in its b_fsprivate2 field, then we know we already
	 * have it locked.  If it is already read in we just increment
	 * the lock recursion count and return the buffer to the caller.
	 * If the buffer is not yet read in, then we read it in, increment
	 * the lock recursion count, and return it to the caller.
	 */
264 265 266
	if (tp)
		bp = xfs_trans_buf_item_match(tp, target, map, nmaps);
	if (bp) {
267
		ASSERT(xfs_buf_islocked(bp));
268
		ASSERT(bp->b_transp == tp);
269
		ASSERT(bp->b_fspriv != NULL);
270
		ASSERT(!bp->b_error);
271 272
		ASSERT(bp->b_flags & XBF_DONE);

L
Linus Torvalds 已提交
273 274 275 276 277
		/*
		 * We never locked this buf ourselves, so we shouldn't
		 * brelse it either. Just get out.
		 */
		if (XFS_FORCED_SHUTDOWN(mp)) {
C
Christoph Hellwig 已提交
278
			trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
D
Dave Chinner 已提交
279
			return -EIO;
L
Linus Torvalds 已提交
280 281
		}

282
		bip = bp->b_fspriv;
L
Linus Torvalds 已提交
283 284 285
		bip->bli_recur++;

		ASSERT(atomic_read(&bip->bli_refcount) > 0);
C
Christoph Hellwig 已提交
286
		trace_xfs_trans_read_buf_recur(bip);
L
Linus Torvalds 已提交
287 288 289 290
		*bpp = bp;
		return 0;
	}

291
	bp = xfs_buf_read_map(target, map, nmaps, flags, ops);
292 293 294 295
	if (!bp) {
		if (!(flags & XBF_TRYLOCK))
			return -ENOMEM;
		return tp ? 0 : -EAGAIN;
L
Linus Torvalds 已提交
296
	}
297 298 299 300 301 302 303 304 305 306

	/*
	 * If we've had a read error, then the contents of the buffer are
	 * invalid and should not be used. To ensure that a followup read tries
	 * to pull the buffer from disk again, we clear the XBF_DONE flag and
	 * mark the buffer stale. This ensures that anyone who has a current
	 * reference to the buffer will interpret it's contents correctly and
	 * future cache lookups will also treat it as an empty, uninitialised
	 * buffer.
	 */
307 308
	if (bp->b_error) {
		error = bp->b_error;
309 310 311
		if (!XFS_FORCED_SHUTDOWN(mp))
			xfs_buf_ioerror_alert(bp, __func__);
		bp->b_flags &= ~XBF_DONE;
312
		xfs_buf_stale(bp);
313 314

		if (tp && (tp->t_flags & XFS_TRANS_DIRTY))
315
			xfs_force_shutdown(tp->t_mountp, SHUTDOWN_META_IO_ERROR);
L
Linus Torvalds 已提交
316
		xfs_buf_relse(bp);
317 318

		/* bad CRC means corrupted metadata */
D
Dave Chinner 已提交
319 320
		if (error == -EFSBADCRC)
			error = -EFSCORRUPTED;
L
Linus Torvalds 已提交
321 322
		return error;
	}
323 324 325 326 327

	if (XFS_FORCED_SHUTDOWN(mp)) {
		xfs_buf_relse(bp);
		trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
		return -EIO;
L
Linus Torvalds 已提交
328 329
	}

330
	if (tp) {
331
		_xfs_trans_bjoin(tp, bp, 1);
332 333
		trace_xfs_trans_read_buf(bp->b_fspriv);
	}
L
Linus Torvalds 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
	*bpp = bp;
	return 0;

}

/*
 * Release the buffer bp which was previously acquired with one of the
 * xfs_trans_... buffer allocation routines if the buffer has not
 * been modified within this transaction.  If the buffer is modified
 * within this transaction, do decrement the recursion count but do
 * not release the buffer even if the count goes to 0.  If the buffer is not
 * modified within the transaction, decrement the recursion count and
 * release the buffer if the recursion count goes to 0.
 *
 * If the buffer is to be released and it was not modified before
 * this transaction began, then free the buf_log_item associated with it.
 *
 * If the transaction pointer is NULL, make this just a normal
 * brelse() call.
 */
void
xfs_trans_brelse(xfs_trans_t	*tp,
		 xfs_buf_t	*bp)
{
	xfs_buf_log_item_t	*bip;

	/*
	 * Default to a normal brelse() call if the tp is NULL.
	 */
	if (tp == NULL) {
364
		ASSERT(bp->b_transp == NULL);
L
Linus Torvalds 已提交
365 366 367 368
		xfs_buf_relse(bp);
		return;
	}

369
	ASSERT(bp->b_transp == tp);
370
	bip = bp->b_fspriv;
L
Linus Torvalds 已提交
371 372
	ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
	ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
373
	ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
L
Linus Torvalds 已提交
374 375
	ASSERT(atomic_read(&bip->bli_refcount) > 0);

C
Christoph Hellwig 已提交
376 377
	trace_xfs_trans_brelse(bip);

L
Linus Torvalds 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390
	/*
	 * If the release is just for a recursive lock,
	 * then decrement the count and return.
	 */
	if (bip->bli_recur > 0) {
		bip->bli_recur--;
		return;
	}

	/*
	 * If the buffer is dirty within this transaction, we can't
	 * release it until we commit.
	 */
391
	if (bip->bli_item.li_desc->lid_flags & XFS_LID_DIRTY)
L
Linus Torvalds 已提交
392 393 394 395 396 397 398 399
		return;

	/*
	 * If the buffer has been invalidated, then we can't release
	 * it until the transaction commits to disk unless it is re-dirtied
	 * as part of this transaction.  This prevents us from pulling
	 * the item from the AIL before we should.
	 */
C
Christoph Hellwig 已提交
400
	if (bip->bli_flags & XFS_BLI_STALE)
L
Linus Torvalds 已提交
401 402 403 404 405 406 407
		return;

	ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));

	/*
	 * Free up the log item descriptor tracking the released item.
	 */
408
	xfs_trans_del_item(&bip->bli_item);
L
Linus Torvalds 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440

	/*
	 * Clear the hold flag in the buf log item if it is set.
	 * We wouldn't want the next user of the buffer to
	 * get confused.
	 */
	if (bip->bli_flags & XFS_BLI_HOLD) {
		bip->bli_flags &= ~XFS_BLI_HOLD;
	}

	/*
	 * Drop our reference to the buf log item.
	 */
	atomic_dec(&bip->bli_refcount);

	/*
	 * If the buf item is not tracking data in the log, then
	 * we must free it before releasing the buffer back to the
	 * free pool.  Before releasing the buffer to the free pool,
	 * clear the transaction pointer in b_fsprivate2 to dissolve
	 * its relation to this transaction.
	 */
	if (!xfs_buf_item_dirty(bip)) {
/***
		ASSERT(bp->b_pincount == 0);
***/
		ASSERT(atomic_read(&bip->bli_refcount) == 0);
		ASSERT(!(bip->bli_item.li_flags & XFS_LI_IN_AIL));
		ASSERT(!(bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF));
		xfs_buf_item_relse(bp);
	}

441
	bp->b_transp = NULL;
L
Linus Torvalds 已提交
442 443 444 445 446
	xfs_buf_relse(bp);
}

/*
 * Mark the buffer as not needing to be unlocked when the buf item's
447
 * iop_unlock() routine is called.  The buffer must already be locked
L
Linus Torvalds 已提交
448 449 450 451 452 453 454
 * and associated with the given transaction.
 */
/* ARGSUSED */
void
xfs_trans_bhold(xfs_trans_t	*tp,
		xfs_buf_t	*bp)
{
455
	xfs_buf_log_item_t	*bip = bp->b_fspriv;
L
Linus Torvalds 已提交
456

457
	ASSERT(bp->b_transp == tp);
458
	ASSERT(bip != NULL);
L
Linus Torvalds 已提交
459
	ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
460
	ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
L
Linus Torvalds 已提交
461
	ASSERT(atomic_read(&bip->bli_refcount) > 0);
462

L
Linus Torvalds 已提交
463
	bip->bli_flags |= XFS_BLI_HOLD;
C
Christoph Hellwig 已提交
464
	trace_xfs_trans_bhold(bip);
L
Linus Torvalds 已提交
465 466
}

467 468 469 470 471 472 473 474
/*
 * Cancel the previous buffer hold request made on this buffer
 * for this transaction.
 */
void
xfs_trans_bhold_release(xfs_trans_t	*tp,
			xfs_buf_t	*bp)
{
475
	xfs_buf_log_item_t	*bip = bp->b_fspriv;
476

477
	ASSERT(bp->b_transp == tp);
478
	ASSERT(bip != NULL);
479
	ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
480
	ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
481 482
	ASSERT(atomic_read(&bip->bli_refcount) > 0);
	ASSERT(bip->bli_flags & XFS_BLI_HOLD);
C
Christoph Hellwig 已提交
483

484
	bip->bli_flags &= ~XFS_BLI_HOLD;
C
Christoph Hellwig 已提交
485
	trace_xfs_trans_bhold_release(bip);
486 487
}

L
Linus Torvalds 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
/*
 * This is called to mark bytes first through last inclusive of the given
 * buffer as needing to be logged when the transaction is committed.
 * The buffer must already be associated with the given transaction.
 *
 * First and last are numbers relative to the beginning of this buffer,
 * so the first byte in the buffer is numbered 0 regardless of the
 * value of b_blkno.
 */
void
xfs_trans_log_buf(xfs_trans_t	*tp,
		  xfs_buf_t	*bp,
		  uint		first,
		  uint		last)
{
503
	xfs_buf_log_item_t	*bip = bp->b_fspriv;
L
Linus Torvalds 已提交
504

505
	ASSERT(bp->b_transp == tp);
506
	ASSERT(bip != NULL);
507
	ASSERT(first <= last && last < BBTOB(bp->b_length));
508 509
	ASSERT(bp->b_iodone == NULL ||
	       bp->b_iodone == xfs_buf_iodone_callbacks);
L
Linus Torvalds 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522 523

	/*
	 * Mark the buffer as needing to be written out eventually,
	 * and set its iodone function to remove the buffer's buf log
	 * item from the AIL and free it when the buffer is flushed
	 * to disk.  See xfs_buf_attach_iodone() for more details
	 * on li_cb and xfs_buf_iodone_callbacks().
	 * If we end up aborting this transaction, we trap this buffer
	 * inside the b_bdstrat callback so that this won't get written to
	 * disk.
	 */
	XFS_BUF_DONE(bp);

	ASSERT(atomic_read(&bip->bli_refcount) > 0);
524
	bp->b_iodone = xfs_buf_iodone_callbacks;
525
	bip->bli_item.li_cb = xfs_buf_iodone;
L
Linus Torvalds 已提交
526

C
Christoph Hellwig 已提交
527 528
	trace_xfs_trans_log_buf(bip);

L
Linus Torvalds 已提交
529 530 531 532 533 534 535 536 537 538
	/*
	 * If we invalidated the buffer within this transaction, then
	 * cancel the invalidation now that we're dirtying the buffer
	 * again.  There are no races with the code in xfs_buf_item_unpin(),
	 * because we have a reference to the buffer this entire time.
	 */
	if (bip->bli_flags & XFS_BLI_STALE) {
		bip->bli_flags &= ~XFS_BLI_STALE;
		ASSERT(XFS_BUF_ISSTALE(bp));
		XFS_BUF_UNSTALE(bp);
539
		bip->__bli_format.blf_flags &= ~XFS_BLF_CANCEL;
L
Linus Torvalds 已提交
540 541 542
	}

	tp->t_flags |= XFS_TRANS_DIRTY;
543
	bip->bli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
544 545 546 547 548 549 550 551

	/*
	 * If we have an ordered buffer we are not logging any dirty range but
	 * it still needs to be marked dirty and that it has been logged.
	 */
	bip->bli_flags |= XFS_BLI_DIRTY | XFS_BLI_LOGGED;
	if (!(bip->bli_flags & XFS_BLI_ORDERED))
		xfs_buf_item_log(bip, first, last);
L
Linus Torvalds 已提交
552 553 554 555
}


/*
556 557 558 559 560 561
 * Invalidate a buffer that is being used within a transaction.
 *
 * Typically this is because the blocks in the buffer are being freed, so we
 * need to prevent it from being written out when we're done.  Allowing it
 * to be written again might overwrite data in the free blocks if they are
 * reallocated to a file.
L
Linus Torvalds 已提交
562
 *
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
 * We prevent the buffer from being written out by marking it stale.  We can't
 * get rid of the buf log item at this point because the buffer may still be
 * pinned by another transaction.  If that is the case, then we'll wait until
 * the buffer is committed to disk for the last time (we can tell by the ref
 * count) and free it in xfs_buf_item_unpin().  Until that happens we will
 * keep the buffer locked so that the buffer and buf log item are not reused.
 *
 * We also set the XFS_BLF_CANCEL flag in the buf log format structure and log
 * the buf item.  This will be used at recovery time to determine that copies
 * of the buffer in the log before this should not be replayed.
 *
 * We mark the item descriptor and the transaction dirty so that we'll hold
 * the buffer until after the commit.
 *
 * Since we're invalidating the buffer, we also clear the state about which
 * parts of the buffer have been logged.  We also clear the flag indicating
 * that this is an inode buffer since the data in the buffer will no longer
 * be valid.
 *
 * We set the stale bit in the buffer as well since we're getting rid of it.
L
Linus Torvalds 已提交
583 584 585 586 587 588
 */
void
xfs_trans_binval(
	xfs_trans_t	*tp,
	xfs_buf_t	*bp)
{
589
	xfs_buf_log_item_t	*bip = bp->b_fspriv;
590
	int			i;
L
Linus Torvalds 已提交
591

592
	ASSERT(bp->b_transp == tp);
593
	ASSERT(bip != NULL);
L
Linus Torvalds 已提交
594 595
	ASSERT(atomic_read(&bip->bli_refcount) > 0);

C
Christoph Hellwig 已提交
596 597
	trace_xfs_trans_binval(bip);

L
Linus Torvalds 已提交
598 599 600 601 602 603 604
	if (bip->bli_flags & XFS_BLI_STALE) {
		/*
		 * If the buffer is already invalidated, then
		 * just return.
		 */
		ASSERT(XFS_BUF_ISSTALE(bp));
		ASSERT(!(bip->bli_flags & (XFS_BLI_LOGGED | XFS_BLI_DIRTY)));
605
		ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_INODE_BUF));
606
		ASSERT(!(bip->__bli_format.blf_flags & XFS_BLFT_MASK));
607
		ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
608
		ASSERT(bip->bli_item.li_desc->lid_flags & XFS_LID_DIRTY);
L
Linus Torvalds 已提交
609 610 611 612
		ASSERT(tp->t_flags & XFS_TRANS_DIRTY);
		return;
	}

613
	xfs_buf_stale(bp);
614

L
Linus Torvalds 已提交
615
	bip->bli_flags |= XFS_BLI_STALE;
616
	bip->bli_flags &= ~(XFS_BLI_INODE_BUF | XFS_BLI_LOGGED | XFS_BLI_DIRTY);
617 618
	bip->__bli_format.blf_flags &= ~XFS_BLF_INODE_BUF;
	bip->__bli_format.blf_flags |= XFS_BLF_CANCEL;
619
	bip->__bli_format.blf_flags &= ~XFS_BLFT_MASK;
620 621 622 623
	for (i = 0; i < bip->bli_format_count; i++) {
		memset(bip->bli_formats[i].blf_data_map, 0,
		       (bip->bli_formats[i].blf_map_size * sizeof(uint)));
	}
624
	bip->bli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
L
Linus Torvalds 已提交
625 626 627 628
	tp->t_flags |= XFS_TRANS_DIRTY;
}

/*
629 630 631 632 633
 * This call is used to indicate that the buffer contains on-disk inodes which
 * must be handled specially during recovery.  They require special handling
 * because only the di_next_unlinked from the inodes in the buffer should be
 * recovered.  The rest of the data in the buffer is logged via the inodes
 * themselves.
L
Linus Torvalds 已提交
634
 *
635 636 637
 * All we do is set the XFS_BLI_INODE_BUF flag in the items flags so it can be
 * transferred to the buffer's log format structure so that we'll know what to
 * do at recovery time.
L
Linus Torvalds 已提交
638 639 640 641 642 643
 */
void
xfs_trans_inode_buf(
	xfs_trans_t	*tp,
	xfs_buf_t	*bp)
{
644
	xfs_buf_log_item_t	*bip = bp->b_fspriv;
L
Linus Torvalds 已提交
645

646
	ASSERT(bp->b_transp == tp);
647
	ASSERT(bip != NULL);
L
Linus Torvalds 已提交
648 649
	ASSERT(atomic_read(&bip->bli_refcount) > 0);

650
	bip->bli_flags |= XFS_BLI_INODE_BUF;
651
	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
L
Linus Torvalds 已提交
652 653 654 655 656
}

/*
 * This call is used to indicate that the buffer is going to
 * be staled and was an inode buffer. This means it gets
657
 * special processing during unpin - where any inodes
L
Linus Torvalds 已提交
658 659 660 661 662 663 664 665 666 667
 * associated with the buffer should be removed from ail.
 * There is also special processing during recovery,
 * any replay of the inodes in the buffer needs to be
 * prevented as the buffer may have been reused.
 */
void
xfs_trans_stale_inode_buf(
	xfs_trans_t	*tp,
	xfs_buf_t	*bp)
{
668
	xfs_buf_log_item_t	*bip = bp->b_fspriv;
L
Linus Torvalds 已提交
669

670
	ASSERT(bp->b_transp == tp);
671
	ASSERT(bip != NULL);
L
Linus Torvalds 已提交
672 673 674
	ASSERT(atomic_read(&bip->bli_refcount) > 0);

	bip->bli_flags |= XFS_BLI_STALE_INODE;
675
	bip->bli_item.li_cb = xfs_buf_iodone;
676
	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
L
Linus Torvalds 已提交
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
}

/*
 * Mark the buffer as being one which contains newly allocated
 * inodes.  We need to make sure that even if this buffer is
 * relogged as an 'inode buf' we still recover all of the inode
 * images in the face of a crash.  This works in coordination with
 * xfs_buf_item_committed() to ensure that the buffer remains in the
 * AIL at its original location even after it has been relogged.
 */
/* ARGSUSED */
void
xfs_trans_inode_alloc_buf(
	xfs_trans_t	*tp,
	xfs_buf_t	*bp)
{
693
	xfs_buf_log_item_t	*bip = bp->b_fspriv;
L
Linus Torvalds 已提交
694

695
	ASSERT(bp->b_transp == tp);
696
	ASSERT(bip != NULL);
L
Linus Torvalds 已提交
697 698 699
	ASSERT(atomic_read(&bip->bli_refcount) > 0);

	bip->bli_flags |= XFS_BLI_INODE_ALLOC_BUF;
700
	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
L
Linus Torvalds 已提交
701 702
}

703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
/*
 * Mark the buffer as ordered for this transaction. This means
 * that the contents of the buffer are not recorded in the transaction
 * but it is tracked in the AIL as though it was. This allows us
 * to record logical changes in transactions rather than the physical
 * changes we make to the buffer without changing writeback ordering
 * constraints of metadata buffers.
 */
void
xfs_trans_ordered_buf(
	struct xfs_trans	*tp,
	struct xfs_buf		*bp)
{
	struct xfs_buf_log_item	*bip = bp->b_fspriv;

	ASSERT(bp->b_transp == tp);
	ASSERT(bip != NULL);
	ASSERT(atomic_read(&bip->bli_refcount) > 0);

	bip->bli_flags |= XFS_BLI_ORDERED;
	trace_xfs_buf_item_ordered(bip);
}

726 727 728 729 730 731 732 733
/*
 * Set the type of the buffer for log recovery so that it can correctly identify
 * and hence attach the correct buffer ops to the buffer after replay.
 */
void
xfs_trans_buf_set_type(
	struct xfs_trans	*tp,
	struct xfs_buf		*bp,
734
	enum xfs_blft		type)
735 736 737
{
	struct xfs_buf_log_item	*bip = bp->b_fspriv;

738 739 740
	if (!tp)
		return;

741 742 743 744
	ASSERT(bp->b_transp == tp);
	ASSERT(bip != NULL);
	ASSERT(atomic_read(&bip->bli_refcount) > 0);

745
	xfs_blft_to_flags(&bip->__bli_format, type);
746
}
L
Linus Torvalds 已提交
747

748 749 750 751 752 753 754
void
xfs_trans_buf_copy_type(
	struct xfs_buf		*dst_bp,
	struct xfs_buf		*src_bp)
{
	struct xfs_buf_log_item	*sbip = src_bp->b_fspriv;
	struct xfs_buf_log_item	*dbip = dst_bp->b_fspriv;
755
	enum xfs_blft		type;
756

757 758
	type = xfs_blft_from_flags(&sbip->__bli_format);
	xfs_blft_to_flags(&dbip->__bli_format, type);
759 760
}

L
Linus Torvalds 已提交
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
/*
 * Similar to xfs_trans_inode_buf(), this marks the buffer as a cluster of
 * dquots. However, unlike in inode buffer recovery, dquot buffers get
 * recovered in their entirety. (Hence, no XFS_BLI_DQUOT_ALLOC_BUF flag).
 * The only thing that makes dquot buffers different from regular
 * buffers is that we must not replay dquot bufs when recovering
 * if a _corresponding_ quotaoff has happened. We also have to distinguish
 * between usr dquot bufs and grp dquot bufs, because usr and grp quotas
 * can be turned off independently.
 */
/* ARGSUSED */
void
xfs_trans_dquot_buf(
	xfs_trans_t	*tp,
	xfs_buf_t	*bp,
	uint		type)
{
778 779
	struct xfs_buf_log_item	*bip = bp->b_fspriv;

780 781 782
	ASSERT(type == XFS_BLF_UDQUOT_BUF ||
	       type == XFS_BLF_PDQUOT_BUF ||
	       type == XFS_BLF_GDQUOT_BUF);
L
Linus Torvalds 已提交
783

784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
	bip->__bli_format.blf_flags |= type;

	switch (type) {
	case XFS_BLF_UDQUOT_BUF:
		type = XFS_BLFT_UDQUOT_BUF;
		break;
	case XFS_BLF_PDQUOT_BUF:
		type = XFS_BLFT_PDQUOT_BUF;
		break;
	case XFS_BLF_GDQUOT_BUF:
		type = XFS_BLFT_GDQUOT_BUF;
		break;
	default:
		type = XFS_BLFT_UNKNOWN_BUF;
		break;
	}

801
	xfs_trans_buf_set_type(tp, bp, type);
L
Linus Torvalds 已提交
802
}