xref: /linux/fs/xfs/xfs_trans_buf.c (revision a7f25dc23ff6d238ed70e8a3a8a3792cde3bcc68)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include "xfs_platform.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_trans.h"
14 #include "xfs_buf_item.h"
15 #include "xfs_trans_priv.h"
16 #include "xfs_trace.h"
17 
18 /*
19  * Check to see if a buffer matching the given parameters is already
20  * a part of the given transaction.
21  */
22 STATIC struct xfs_buf *
xfs_trans_buf_item_match(struct xfs_trans * tp,struct xfs_buftarg * target,struct xfs_buf_map * map,int nmaps)23 xfs_trans_buf_item_match(
24 	struct xfs_trans	*tp,
25 	struct xfs_buftarg	*target,
26 	struct xfs_buf_map	*map,
27 	int			nmaps)
28 {
29 	struct xfs_log_item	*lip;
30 	struct xfs_buf_log_item	*blip;
31 	int			len = 0;
32 	int			i;
33 
34 	for (i = 0; i < nmaps; i++)
35 		len += map[i].bm_len;
36 
37 	list_for_each_entry(lip, &tp->t_items, li_trans) {
38 		blip = (struct xfs_buf_log_item *)lip;
39 		if (blip->bli_item.li_type == XFS_LI_BUF &&
40 		    blip->bli_buf->b_target == target &&
41 		    xfs_buf_daddr(blip->bli_buf) == map[0].bm_bn &&
42 		    blip->bli_buf->b_length == len) {
43 			ASSERT(blip->bli_buf->b_map_count == nmaps);
44 			return blip->bli_buf;
45 		}
46 	}
47 
48 	return NULL;
49 }
50 
51 /*
52  * Add the locked buffer to the transaction.
53  *
54  * The buffer must be locked, and it cannot be associated with any
55  * transaction.
56  *
57  * If the buffer does not yet have a buf log item associated with it,
58  * then allocate one for it.  Then add the buf item to the transaction.
59  */
60 STATIC void
_xfs_trans_bjoin(struct xfs_trans * tp,struct xfs_buf * bp,int reset_recur)61 _xfs_trans_bjoin(
62 	struct xfs_trans	*tp,
63 	struct xfs_buf		*bp,
64 	int			reset_recur)
65 {
66 	struct xfs_buf_log_item	*bip;
67 
68 	ASSERT(bp->b_transp == NULL);
69 
70 	/*
71 	 * The xfs_buf_log_item pointer is stored in b_log_item.  If
72 	 * it doesn't have one yet, then allocate one and initialize it.
73 	 * The checks to see if one is there are in xfs_buf_item_init().
74 	 */
75 	xfs_buf_item_init(bp, tp->t_mountp);
76 	bip = bp->b_log_item;
77 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
78 	ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
79 	ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
80 	if (reset_recur)
81 		bip->bli_recur = 0;
82 
83 	/*
84 	 * Take a reference for this transaction on the buf item.
85 	 */
86 	atomic_inc(&bip->bli_refcount);
87 
88 	/*
89 	 * Attach the item to the transaction so we can find it in
90 	 * xfs_trans_get_buf() and friends.
91 	 */
92 	xfs_trans_add_item(tp, &bip->bli_item);
93 	bp->b_transp = tp;
94 
95 }
96 
97 void
xfs_trans_bjoin(struct xfs_trans * tp,struct xfs_buf * bp)98 xfs_trans_bjoin(
99 	struct xfs_trans	*tp,
100 	struct xfs_buf		*bp)
101 {
102 	_xfs_trans_bjoin(tp, bp, 0);
103 	trace_xfs_trans_bjoin(bp->b_log_item);
104 }
105 
106 /*
107  * Get and lock the buffer for the caller if it is not already
108  * locked within the given transaction.  If it is already locked
109  * within the transaction, just increment its lock recursion count
110  * and return a pointer to it.
111  *
112  * If the transaction pointer is NULL, make this just a normal
113  * get_buf() call.
114  */
115 int
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,struct xfs_buf ** bpp)116 xfs_trans_get_buf_map(
117 	struct xfs_trans	*tp,
118 	struct xfs_buftarg	*target,
119 	struct xfs_buf_map	*map,
120 	int			nmaps,
121 	xfs_buf_flags_t		flags,
122 	struct xfs_buf		**bpp)
123 {
124 	struct xfs_buf		*bp;
125 	struct xfs_buf_log_item	*bip;
126 	int			error;
127 
128 	*bpp = NULL;
129 	if (!tp)
130 		return xfs_buf_get_map(target, map, nmaps, flags, bpp);
131 
132 	/*
133 	 * If we find the buffer in the cache with this transaction
134 	 * pointer in its b_fsprivate2 field, then we know we already
135 	 * have it locked.  In this case we just increment the lock
136 	 * recursion count and return the buffer to the caller.
137 	 */
138 	bp = xfs_trans_buf_item_match(tp, target, map, nmaps);
139 	if (bp != NULL) {
140 		ASSERT(xfs_buf_islocked(bp));
141 		if (xfs_is_shutdown(tp->t_mountp)) {
142 			xfs_buf_stale(bp);
143 			xfs_buf_set_uptodate(bp);
144 		}
145 
146 		ASSERT(bp->b_transp == tp);
147 		bip = bp->b_log_item;
148 		ASSERT(bip != NULL);
149 		ASSERT(atomic_read(&bip->bli_refcount) > 0);
150 		bip->bli_recur++;
151 		trace_xfs_trans_get_buf_recur(bip);
152 		*bpp = bp;
153 		return 0;
154 	}
155 
156 	error = xfs_buf_get_map(target, map, nmaps, flags, &bp);
157 	if (error)
158 		return error;
159 
160 	ASSERT(!bp->b_error);
161 
162 	_xfs_trans_bjoin(tp, bp, 1);
163 	trace_xfs_trans_get_buf(bp->b_log_item);
164 	*bpp = bp;
165 	return 0;
166 }
167 
168 /*
169  * Get and lock the superblock buffer for the given transaction.
170  */
171 static struct xfs_buf *
__xfs_trans_getsb(struct xfs_trans * tp,struct xfs_buf * bp)172 __xfs_trans_getsb(
173 	struct xfs_trans	*tp,
174 	struct xfs_buf		*bp)
175 {
176 	/*
177 	 * Just increment the lock recursion count if the buffer is already
178 	 * attached to this transaction.
179 	 */
180 	if (bp->b_transp == tp) {
181 		struct xfs_buf_log_item	*bip = bp->b_log_item;
182 
183 		ASSERT(bip != NULL);
184 		ASSERT(atomic_read(&bip->bli_refcount) > 0);
185 		bip->bli_recur++;
186 
187 		trace_xfs_trans_getsb_recur(bip);
188 	} else {
189 		xfs_buf_lock(bp);
190 		xfs_buf_hold(bp);
191 		_xfs_trans_bjoin(tp, bp, 1);
192 
193 		trace_xfs_trans_getsb(bp->b_log_item);
194 	}
195 
196 	return bp;
197 }
198 
199 struct xfs_buf *
xfs_trans_getsb(struct xfs_trans * tp)200 xfs_trans_getsb(
201 	struct xfs_trans	*tp)
202 {
203 	return __xfs_trans_getsb(tp, tp->t_mountp->m_sb_bp);
204 }
205 
206 struct xfs_buf *
xfs_trans_getrtsb(struct xfs_trans * tp)207 xfs_trans_getrtsb(
208 	struct xfs_trans	*tp)
209 {
210 	if (!tp->t_mountp->m_rtsb_bp)
211 		return NULL;
212 	return __xfs_trans_getsb(tp, tp->t_mountp->m_rtsb_bp);
213 }
214 
215 /*
216  * Get and lock the buffer for the caller if it is not already
217  * locked within the given transaction.  If it has not yet been
218  * read in, read it from disk. If it is already locked
219  * within the transaction and already read in, just increment its
220  * lock recursion count and return a pointer to it.
221  *
222  * If the transaction pointer is NULL, make this just a normal
223  * read_buf() call.
224  */
225 int
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,struct xfs_buf ** bpp,const struct xfs_buf_ops * ops)226 xfs_trans_read_buf_map(
227 	struct xfs_mount	*mp,
228 	struct xfs_trans	*tp,
229 	struct xfs_buftarg	*target,
230 	struct xfs_buf_map	*map,
231 	int			nmaps,
232 	xfs_buf_flags_t		flags,
233 	struct xfs_buf		**bpp,
234 	const struct xfs_buf_ops *ops)
235 {
236 	struct xfs_buf		*bp = NULL;
237 	int			error;
238 
239 	*bpp = NULL;
240 	/*
241 	 * If we find the buffer in the cache with this transaction
242 	 * pointer in its b_fsprivate2 field, then we know we already
243 	 * have it locked.  If it is already read in we just increment
244 	 * the lock recursion count and return the buffer to the caller.
245 	 * If the buffer is not yet read in, then we read it in, increment
246 	 * the lock recursion count, and return it to the caller.
247 	 */
248 	if (tp)
249 		bp = xfs_trans_buf_item_match(tp, target, map, nmaps);
250 	if (bp) {
251 		ASSERT(xfs_buf_islocked(bp));
252 		ASSERT(bp->b_transp == tp);
253 		ASSERT(!bp->b_error);
254 		ASSERT(bp->b_flags & XBF_DONE);
255 		ASSERT(atomic_read(&bp->b_log_item->bli_refcount) > 0);
256 		ASSERT(bp->b_ops);
257 
258 		/*
259 		 * We never locked this buf ourselves, so we shouldn't
260 		 * brelse it either. Just get out.
261 		 */
262 		if (xfs_is_shutdown(mp)) {
263 			trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
264 			return -EIO;
265 		}
266 
267 		bp->b_log_item->bli_recur++;
268 		trace_xfs_trans_read_buf_recur(bp->b_log_item);
269 		*bpp = bp;
270 		return 0;
271 	}
272 
273 	error = xfs_buf_read_map(target, map, nmaps, flags, &bp, ops,
274 			__return_address);
275 	switch (error) {
276 	case 0:
277 		break;
278 	default:
279 		if (tp && (tp->t_flags & XFS_TRANS_DIRTY))
280 			xfs_force_shutdown(tp->t_mountp, SHUTDOWN_META_IO_ERROR);
281 		fallthrough;
282 	case -ENOMEM:
283 	case -EAGAIN:
284 		return error;
285 	}
286 
287 	if (xfs_is_shutdown(mp)) {
288 		xfs_buf_relse(bp);
289 		trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
290 		return -EIO;
291 	}
292 
293 	if (tp) {
294 		_xfs_trans_bjoin(tp, bp, 1);
295 		trace_xfs_trans_read_buf(bp->b_log_item);
296 	}
297 	ASSERT(bp->b_ops != NULL || ops == NULL);
298 	*bpp = bp;
299 	return 0;
300 
301 }
302 
303 /* Has this buffer been dirtied by anyone? */
304 bool
xfs_trans_buf_is_dirty(struct xfs_buf * bp)305 xfs_trans_buf_is_dirty(
306 	struct xfs_buf		*bp)
307 {
308 	struct xfs_buf_log_item	*bip = bp->b_log_item;
309 
310 	if (!bip)
311 		return false;
312 	ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
313 	return test_bit(XFS_LI_DIRTY, &bip->bli_item.li_flags);
314 }
315 
316 /*
317  * Release a buffer previously joined to the transaction. If the buffer is
318  * modified within this transaction, decrement the recursion count but do not
319  * release the buffer even if the count goes to 0. If the buffer is not modified
320  * within the transaction, decrement the recursion count and release the buffer
321  * if the recursion count goes to 0.
322  *
323  * If the buffer is to be released and it was not already dirty before this
324  * transaction began, then also free the buf_log_item associated with it.
325  *
326  * If the transaction pointer is NULL, this is a normal xfs_buf_relse() call.
327  */
328 void
xfs_trans_brelse(struct xfs_trans * tp,struct xfs_buf * bp)329 xfs_trans_brelse(
330 	struct xfs_trans	*tp,
331 	struct xfs_buf		*bp)
332 {
333 	struct xfs_buf_log_item	*bip = bp->b_log_item;
334 
335 	ASSERT(bp->b_transp == tp);
336 
337 	if (!tp) {
338 		xfs_buf_relse(bp);
339 		return;
340 	}
341 
342 	trace_xfs_trans_brelse(bip);
343 	ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
344 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
345 
346 	/*
347 	 * If the release is for a recursive lookup, then decrement the count
348 	 * and return.
349 	 */
350 	if (bip->bli_recur > 0) {
351 		bip->bli_recur--;
352 		return;
353 	}
354 
355 	/*
356 	 * If the buffer is invalidated or dirty in this transaction, we can't
357 	 * release it until we commit.
358 	 */
359 	if (test_bit(XFS_LI_DIRTY, &bip->bli_item.li_flags))
360 		return;
361 	if (bip->bli_flags & XFS_BLI_STALE)
362 		return;
363 
364 	/*
365 	 * Unlink the log item from the transaction and clear the hold flag, if
366 	 * set. We wouldn't want the next user of the buffer to get confused.
367 	 */
368 	ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
369 	xfs_trans_del_item(&bip->bli_item);
370 	bip->bli_flags &= ~XFS_BLI_HOLD;
371 
372 	/* drop the reference to the bli */
373 	xfs_buf_item_put(bip);
374 
375 	bp->b_transp = NULL;
376 	xfs_buf_relse(bp);
377 }
378 
379 /*
380  * Forcibly detach a buffer previously joined to the transaction.  The caller
381  * will retain its locked reference to the buffer after this function returns.
382  * The buffer must be completely clean and must not be held to the transaction.
383  */
384 void
xfs_trans_bdetach(struct xfs_trans * tp,struct xfs_buf * bp)385 xfs_trans_bdetach(
386 	struct xfs_trans	*tp,
387 	struct xfs_buf		*bp)
388 {
389 	struct xfs_buf_log_item	*bip = bp->b_log_item;
390 
391 	ASSERT(tp != NULL);
392 	ASSERT(bp->b_transp == tp);
393 	ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
394 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
395 
396 	trace_xfs_trans_bdetach(bip);
397 
398 	/*
399 	 * Erase all recursion count, since we're removing this buffer from the
400 	 * transaction.
401 	 */
402 	bip->bli_recur = 0;
403 
404 	/*
405 	 * The buffer must be completely clean.  Specifically, it had better
406 	 * not be dirty, stale, logged, ordered, or held to the transaction.
407 	 */
408 	ASSERT(!test_bit(XFS_LI_DIRTY, &bip->bli_item.li_flags));
409 	ASSERT(!(bip->bli_flags & XFS_BLI_DIRTY));
410 	ASSERT(!(bip->bli_flags & XFS_BLI_HOLD));
411 	ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
412 	ASSERT(!(bip->bli_flags & XFS_BLI_ORDERED));
413 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
414 
415 	/* Unlink the log item from the transaction and drop the log item. */
416 	xfs_trans_del_item(&bip->bli_item);
417 	xfs_buf_item_put(bip);
418 	bp->b_transp = NULL;
419 }
420 
421 /*
422  * Mark the buffer as not needing to be unlocked when the buf item's
423  * iop_committing() routine is called.  The buffer must already be locked
424  * and associated with the given transaction.
425  */
426 /* ARGSUSED */
427 void
xfs_trans_bhold(xfs_trans_t * tp,struct xfs_buf * bp)428 xfs_trans_bhold(
429 	xfs_trans_t		*tp,
430 	struct xfs_buf		*bp)
431 {
432 	struct xfs_buf_log_item	*bip = bp->b_log_item;
433 
434 	ASSERT(bp->b_transp == tp);
435 	ASSERT(bip != NULL);
436 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
437 	ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
438 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
439 
440 	bip->bli_flags |= XFS_BLI_HOLD;
441 	trace_xfs_trans_bhold(bip);
442 }
443 
444 /*
445  * Cancel the previous buffer hold request made on this buffer
446  * for this transaction.
447  */
448 void
xfs_trans_bhold_release(xfs_trans_t * tp,struct xfs_buf * bp)449 xfs_trans_bhold_release(
450 	xfs_trans_t		*tp,
451 	struct xfs_buf		*bp)
452 {
453 	struct xfs_buf_log_item	*bip = bp->b_log_item;
454 
455 	ASSERT(bp->b_transp == tp);
456 	ASSERT(bip != NULL);
457 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
458 	ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
459 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
460 	ASSERT(bip->bli_flags & XFS_BLI_HOLD);
461 
462 	bip->bli_flags &= ~XFS_BLI_HOLD;
463 	trace_xfs_trans_bhold_release(bip);
464 }
465 
466 /*
467  * Mark a buffer dirty in the transaction.
468  */
469 void
xfs_trans_dirty_buf(struct xfs_trans * tp,struct xfs_buf * bp)470 xfs_trans_dirty_buf(
471 	struct xfs_trans	*tp,
472 	struct xfs_buf		*bp)
473 {
474 	struct xfs_buf_log_item	*bip = bp->b_log_item;
475 
476 	ASSERT(bp->b_transp == tp);
477 	ASSERT(bip != NULL);
478 
479 	/*
480 	 * Mark the buffer as needing to be written out eventually,
481 	 * and set its iodone function to remove the buffer's buf log
482 	 * item from the AIL and free it when the buffer is flushed
483 	 * to disk.
484 	 */
485 	xfs_buf_set_uptodate(bp);
486 
487 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
488 
489 	/*
490 	 * If we invalidated the buffer within this transaction, then
491 	 * cancel the invalidation now that we're dirtying the buffer
492 	 * again.  There are no races with the code in xfs_buf_item_unpin(),
493 	 * because we have a reference to the buffer this entire time.
494 	 */
495 	if (bip->bli_flags & XFS_BLI_STALE) {
496 		bip->bli_flags &= ~XFS_BLI_STALE;
497 		xfs_buf_clear_stale(bp);
498 		bip->__bli_format.blf_flags &= ~XFS_BLF_CANCEL;
499 	}
500 	bip->bli_flags |= XFS_BLI_DIRTY | XFS_BLI_LOGGED;
501 
502 	tp->t_flags |= XFS_TRANS_DIRTY;
503 	set_bit(XFS_LI_DIRTY, &bip->bli_item.li_flags);
504 }
505 
506 /*
507  * This is called to mark bytes first through last inclusive of the given
508  * buffer as needing to be logged when the transaction is committed.
509  * The buffer must already be associated with the given transaction.
510  *
511  * First and last are numbers relative to the beginning of this buffer,
512  * so the first byte in the buffer is numbered 0 regardless of the
513  * value of b_blkno.
514  */
515 void
xfs_trans_log_buf(struct xfs_trans * tp,struct xfs_buf * bp,uint first,uint last)516 xfs_trans_log_buf(
517 	struct xfs_trans	*tp,
518 	struct xfs_buf		*bp,
519 	uint			first,
520 	uint			last)
521 {
522 	struct xfs_buf_log_item	*bip = bp->b_log_item;
523 
524 	ASSERT(first <= last);
525 	ASSERT(last < BBTOB(bp->b_length));
526 	ASSERT(!(bip->bli_flags & XFS_BLI_ORDERED));
527 
528 	xfs_trans_dirty_buf(tp, bp);
529 
530 	trace_xfs_trans_log_buf(bip);
531 	xfs_buf_item_log(bip, first, last);
532 }
533 
534 
535 /*
536  * Invalidate a buffer that is being used within a transaction.
537  *
538  * Typically this is because the blocks in the buffer are being freed, so we
539  * need to prevent it from being written out when we're done.  Allowing it
540  * to be written again might overwrite data in the free blocks if they are
541  * reallocated to a file.
542  *
543  * We prevent the buffer from being written out by marking it stale.  We can't
544  * get rid of the buf log item at this point because the buffer may still be
545  * pinned by another transaction.  If that is the case, then we'll wait until
546  * the buffer is committed to disk for the last time (we can tell by the ref
547  * count) and free it in xfs_buf_item_unpin().  Until that happens we will
548  * keep the buffer locked so that the buffer and buf log item are not reused.
549  *
550  * We also set the XFS_BLF_CANCEL flag in the buf log format structure and log
551  * the buf item.  This will be used at recovery time to determine that copies
552  * of the buffer in the log before this should not be replayed.
553  *
554  * We mark the item descriptor and the transaction dirty so that we'll hold
555  * the buffer until after the commit.
556  *
557  * Since we're invalidating the buffer, we also clear the state about which
558  * parts of the buffer have been logged.  We also clear the flag indicating
559  * that this is an inode buffer since the data in the buffer will no longer
560  * be valid.
561  *
562  * We set the stale bit in the buffer as well since we're getting rid of it.
563  */
564 void
xfs_trans_binval(xfs_trans_t * tp,struct xfs_buf * bp)565 xfs_trans_binval(
566 	xfs_trans_t		*tp,
567 	struct xfs_buf		*bp)
568 {
569 	struct xfs_buf_log_item	*bip = bp->b_log_item;
570 	int			i;
571 
572 	ASSERT(bp->b_transp == tp);
573 	ASSERT(bip != NULL);
574 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
575 
576 	trace_xfs_trans_binval(bip);
577 
578 	if (bip->bli_flags & XFS_BLI_STALE) {
579 		/*
580 		 * If the buffer is already invalidated, then
581 		 * just return.
582 		 */
583 		ASSERT(bp->b_flags & XBF_STALE);
584 		ASSERT(!(bip->bli_flags & (XFS_BLI_LOGGED | XFS_BLI_DIRTY)));
585 		ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_INODE_BUF));
586 		ASSERT(!(bip->__bli_format.blf_flags & XFS_BLFT_MASK));
587 		ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
588 		ASSERT(test_bit(XFS_LI_DIRTY, &bip->bli_item.li_flags));
589 		ASSERT(tp->t_flags & XFS_TRANS_DIRTY);
590 		return;
591 	}
592 
593 	xfs_buf_stale(bp);
594 
595 	bip->bli_flags |= XFS_BLI_STALE;
596 	bip->bli_flags &= ~(XFS_BLI_INODE_BUF | XFS_BLI_LOGGED | XFS_BLI_DIRTY);
597 	bip->__bli_format.blf_flags &= ~XFS_BLF_INODE_BUF;
598 	bip->__bli_format.blf_flags |= XFS_BLF_CANCEL;
599 	bip->__bli_format.blf_flags &= ~XFS_BLFT_MASK;
600 	for (i = 0; i < bip->bli_format_count; i++) {
601 		memset(bip->bli_formats[i].blf_data_map, 0,
602 		       (bip->bli_formats[i].blf_map_size * sizeof(uint)));
603 	}
604 	set_bit(XFS_LI_DIRTY, &bip->bli_item.li_flags);
605 	tp->t_flags |= XFS_TRANS_DIRTY;
606 }
607 
608 /*
609  * This call is used to indicate that the buffer contains on-disk inodes which
610  * must be handled specially during recovery.  They require special handling
611  * because only the di_next_unlinked from the inodes in the buffer should be
612  * recovered.  The rest of the data in the buffer is logged via the inodes
613  * themselves.
614  *
615  * All we do is set the XFS_BLI_INODE_BUF flag in the items flags so it can be
616  * transferred to the buffer's log format structure so that we'll know what to
617  * do at recovery time.
618  */
619 void
xfs_trans_inode_buf(xfs_trans_t * tp,struct xfs_buf * bp)620 xfs_trans_inode_buf(
621 	xfs_trans_t		*tp,
622 	struct xfs_buf		*bp)
623 {
624 	struct xfs_buf_log_item	*bip = bp->b_log_item;
625 
626 	ASSERT(bp->b_transp == tp);
627 	ASSERT(bip != NULL);
628 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
629 
630 	bip->bli_flags |= XFS_BLI_INODE_BUF;
631 	bp->b_iodone = xfs_buf_inode_iodone;
632 	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
633 }
634 
635 /*
636  * This call is used to indicate that the buffer is going to
637  * be staled and was an inode buffer. This means it gets
638  * special processing during unpin - where any inodes
639  * associated with the buffer should be removed from ail.
640  * There is also special processing during recovery,
641  * any replay of the inodes in the buffer needs to be
642  * prevented as the buffer may have been reused.
643  */
644 void
xfs_trans_stale_inode_buf(xfs_trans_t * tp,struct xfs_buf * bp)645 xfs_trans_stale_inode_buf(
646 	xfs_trans_t		*tp,
647 	struct xfs_buf		*bp)
648 {
649 	struct xfs_buf_log_item	*bip = bp->b_log_item;
650 
651 	ASSERT(bp->b_transp == tp);
652 	ASSERT(bip != NULL);
653 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
654 
655 	bip->bli_flags |= XFS_BLI_STALE_INODE;
656 	bp->b_iodone = xfs_buf_inode_iodone;
657 	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
658 }
659 
660 /*
661  * Mark the buffer as being one which contains newly allocated
662  * inodes.  We need to make sure that even if this buffer is
663  * relogged as an 'inode buf' we still recover all of the inode
664  * images in the face of a crash.  This works in coordination with
665  * xfs_buf_item_committed() to ensure that the buffer remains in the
666  * AIL at its original location even after it has been relogged.
667  */
668 /* ARGSUSED */
669 void
xfs_trans_inode_alloc_buf(xfs_trans_t * tp,struct xfs_buf * bp)670 xfs_trans_inode_alloc_buf(
671 	xfs_trans_t		*tp,
672 	struct xfs_buf		*bp)
673 {
674 	struct xfs_buf_log_item	*bip = bp->b_log_item;
675 
676 	ASSERT(bp->b_transp == tp);
677 	ASSERT(bip != NULL);
678 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
679 
680 	bip->bli_flags |= XFS_BLI_INODE_ALLOC_BUF;
681 	bp->b_iodone = xfs_buf_inode_iodone;
682 	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
683 }
684 
685 /*
686  * Mark the buffer as ordered for this transaction. This means that the contents
687  * of the buffer are not recorded in the transaction but it is tracked in the
688  * AIL as though it was. This allows us to record logical changes in
689  * transactions rather than the physical changes we make to the buffer without
690  * changing writeback ordering constraints of metadata buffers.
691  */
692 bool
xfs_trans_ordered_buf(struct xfs_trans * tp,struct xfs_buf * bp)693 xfs_trans_ordered_buf(
694 	struct xfs_trans	*tp,
695 	struct xfs_buf		*bp)
696 {
697 	struct xfs_buf_log_item	*bip = bp->b_log_item;
698 
699 	ASSERT(bp->b_transp == tp);
700 	ASSERT(bip != NULL);
701 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
702 
703 	if (xfs_buf_item_dirty_format(bip))
704 		return false;
705 
706 	bip->bli_flags |= XFS_BLI_ORDERED;
707 	trace_xfs_buf_item_ordered(bip);
708 
709 	/*
710 	 * We don't log a dirty range of an ordered buffer but it still needs
711 	 * to be marked dirty and that it has been logged.
712 	 */
713 	xfs_trans_dirty_buf(tp, bp);
714 	return true;
715 }
716 
717 /*
718  * Set the type of the buffer for log recovery so that it can correctly identify
719  * and hence attach the correct buffer ops to the buffer after replay.
720  */
721 void
xfs_trans_buf_set_type(struct xfs_trans * tp,struct xfs_buf * bp,enum xfs_blft type)722 xfs_trans_buf_set_type(
723 	struct xfs_trans	*tp,
724 	struct xfs_buf		*bp,
725 	enum xfs_blft		type)
726 {
727 	struct xfs_buf_log_item	*bip = bp->b_log_item;
728 
729 	if (!tp)
730 		return;
731 
732 	ASSERT(bp->b_transp == tp);
733 	ASSERT(bip != NULL);
734 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
735 
736 	xfs_blft_to_flags(&bip->__bli_format, type);
737 }
738 
739 void
xfs_trans_buf_copy_type(struct xfs_buf * dst_bp,struct xfs_buf * src_bp)740 xfs_trans_buf_copy_type(
741 	struct xfs_buf		*dst_bp,
742 	struct xfs_buf		*src_bp)
743 {
744 	struct xfs_buf_log_item	*sbip = src_bp->b_log_item;
745 	struct xfs_buf_log_item	*dbip = dst_bp->b_log_item;
746 	enum xfs_blft		type;
747 
748 	type = xfs_blft_from_flags(&sbip->__bli_format);
749 	xfs_blft_to_flags(&dbip->__bli_format, type);
750 }
751 
752 /*
753  * Similar to xfs_trans_inode_buf(), this marks the buffer as a cluster of
754  * dquots. However, unlike in inode buffer recovery, dquot buffers get
755  * recovered in their entirety. (Hence, no XFS_BLI_DQUOT_ALLOC_BUF flag).
756  * The only thing that makes dquot buffers different from regular
757  * buffers is that we must not replay dquot bufs when recovering
758  * if a _corresponding_ quotaoff has happened. We also have to distinguish
759  * between usr dquot bufs and grp dquot bufs, because usr and grp quotas
760  * can be turned off independently.
761  */
762 /* ARGSUSED */
763 void
xfs_trans_dquot_buf(xfs_trans_t * tp,struct xfs_buf * bp,uint type)764 xfs_trans_dquot_buf(
765 	xfs_trans_t		*tp,
766 	struct xfs_buf		*bp,
767 	uint			type)
768 {
769 	struct xfs_buf_log_item	*bip = bp->b_log_item;
770 
771 	ASSERT(type == XFS_BLF_UDQUOT_BUF ||
772 	       type == XFS_BLF_PDQUOT_BUF ||
773 	       type == XFS_BLF_GDQUOT_BUF);
774 
775 	bip->__bli_format.blf_flags |= type;
776 
777 	switch (type) {
778 	case XFS_BLF_UDQUOT_BUF:
779 		type = XFS_BLFT_UDQUOT_BUF;
780 		break;
781 	case XFS_BLF_PDQUOT_BUF:
782 		type = XFS_BLFT_PDQUOT_BUF;
783 		break;
784 	case XFS_BLF_GDQUOT_BUF:
785 		type = XFS_BLFT_GDQUOT_BUF;
786 		break;
787 	default:
788 		type = XFS_BLFT_UNKNOWN_BUF;
789 		break;
790 	}
791 
792 	bp->b_iodone = xfs_buf_dquot_iodone;
793 	xfs_trans_buf_set_type(tp, bp, type);
794 }
795