Staging/vt66*: kconfig, depends on WLAN
[linux-2.6/linux-2.6-openrd.git] / fs / xfs / xfs_trans_buf.c
blob49130628d5efdbcc961dbe0e66e64fd5397846de
1 /*
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
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.
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.
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
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_dir2.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
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"
38 #include "xfs_trans_priv.h"
39 #include "xfs_error.h"
40 #include "xfs_rw.h"
41 #include "xfs_trace.h"
44 STATIC xfs_buf_t *xfs_trans_buf_item_match(xfs_trans_t *, xfs_buftarg_t *,
45 xfs_daddr_t, int);
46 STATIC xfs_buf_t *xfs_trans_buf_item_match_all(xfs_trans_t *, xfs_buftarg_t *,
47 xfs_daddr_t, int);
51 * Get and lock the buffer for the caller if it is not already
52 * locked within the given transaction. If it is already locked
53 * within the transaction, just increment its lock recursion count
54 * and return a pointer to it.
56 * Use the fast path function xfs_trans_buf_item_match() or the buffer
57 * cache routine incore_match() to find the buffer
58 * if it is already owned by this transaction.
60 * If we don't already own the buffer, use get_buf() to get it.
61 * If it doesn't yet have an associated xfs_buf_log_item structure,
62 * then allocate one and add the item to this transaction.
64 * If the transaction pointer is NULL, make this just a normal
65 * get_buf() call.
67 xfs_buf_t *
68 xfs_trans_get_buf(xfs_trans_t *tp,
69 xfs_buftarg_t *target_dev,
70 xfs_daddr_t blkno,
71 int len,
72 uint flags)
74 xfs_buf_t *bp;
75 xfs_buf_log_item_t *bip;
77 if (flags == 0)
78 flags = XFS_BUF_LOCK | XFS_BUF_MAPPED;
81 * Default to a normal get_buf() call if the tp is NULL.
83 if (tp == NULL)
84 return xfs_buf_get(target_dev, blkno, len, flags | BUF_BUSY);
87 * If we find the buffer in the cache with this transaction
88 * pointer in its b_fsprivate2 field, then we know we already
89 * have it locked. In this case we just increment the lock
90 * recursion count and return the buffer to the caller.
92 if (tp->t_items.lic_next == NULL) {
93 bp = xfs_trans_buf_item_match(tp, target_dev, blkno, len);
94 } else {
95 bp = xfs_trans_buf_item_match_all(tp, target_dev, blkno, len);
97 if (bp != NULL) {
98 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
99 if (XFS_FORCED_SHUTDOWN(tp->t_mountp))
100 XFS_BUF_SUPER_STALE(bp);
103 * If the buffer is stale then it was binval'ed
104 * since last read. This doesn't matter since the
105 * caller isn't allowed to use the data anyway.
107 else if (XFS_BUF_ISSTALE(bp))
108 ASSERT(!XFS_BUF_ISDELAYWRITE(bp));
110 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
111 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
112 ASSERT(bip != NULL);
113 ASSERT(atomic_read(&bip->bli_refcount) > 0);
114 bip->bli_recur++;
115 trace_xfs_trans_get_buf_recur(bip);
116 return (bp);
120 * We always specify the BUF_BUSY flag within a transaction so
121 * that get_buf does not try to push out a delayed write buffer
122 * which might cause another transaction to take place (if the
123 * buffer was delayed alloc). Such recursive transactions can
124 * easily deadlock with our current transaction as well as cause
125 * us to run out of stack space.
127 bp = xfs_buf_get(target_dev, blkno, len, flags | BUF_BUSY);
128 if (bp == NULL) {
129 return NULL;
132 ASSERT(!XFS_BUF_GETERROR(bp));
135 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
136 * it doesn't have one yet, then allocate one and initialize it.
137 * The checks to see if one is there are in xfs_buf_item_init().
139 xfs_buf_item_init(bp, tp->t_mountp);
142 * Set the recursion count for the buffer within this transaction
143 * to 0.
145 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
146 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
147 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
148 ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
149 bip->bli_recur = 0;
152 * Take a reference for this transaction on the buf item.
154 atomic_inc(&bip->bli_refcount);
157 * Get a log_item_desc to point at the new item.
159 (void) xfs_trans_add_item(tp, (xfs_log_item_t*)bip);
162 * Initialize b_fsprivate2 so we can find it with incore_match()
163 * above.
165 XFS_BUF_SET_FSPRIVATE2(bp, tp);
167 trace_xfs_trans_get_buf(bip);
168 return (bp);
172 * Get and lock the superblock buffer of this file system for the
173 * given transaction.
175 * We don't need to use incore_match() here, because the superblock
176 * buffer is a private buffer which we keep a pointer to in the
177 * mount structure.
179 xfs_buf_t *
180 xfs_trans_getsb(xfs_trans_t *tp,
181 struct xfs_mount *mp,
182 int flags)
184 xfs_buf_t *bp;
185 xfs_buf_log_item_t *bip;
188 * Default to just trying to lock the superblock buffer
189 * if tp is NULL.
191 if (tp == NULL) {
192 return (xfs_getsb(mp, flags));
196 * If the superblock buffer already has this transaction
197 * pointer in its b_fsprivate2 field, then we know we already
198 * have it locked. In this case we just increment the lock
199 * recursion count and return the buffer to the caller.
201 bp = mp->m_sb_bp;
202 if (XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp) {
203 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
204 ASSERT(bip != NULL);
205 ASSERT(atomic_read(&bip->bli_refcount) > 0);
206 bip->bli_recur++;
207 trace_xfs_trans_getsb_recur(bip);
208 return (bp);
211 bp = xfs_getsb(mp, flags);
212 if (bp == NULL) {
213 return NULL;
217 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
218 * it doesn't have one yet, then allocate one and initialize it.
219 * The checks to see if one is there are in xfs_buf_item_init().
221 xfs_buf_item_init(bp, mp);
224 * Set the recursion count for the buffer within this transaction
225 * to 0.
227 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
228 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
229 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
230 ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
231 bip->bli_recur = 0;
234 * Take a reference for this transaction on the buf item.
236 atomic_inc(&bip->bli_refcount);
239 * Get a log_item_desc to point at the new item.
241 (void) xfs_trans_add_item(tp, (xfs_log_item_t*)bip);
244 * Initialize b_fsprivate2 so we can find it with incore_match()
245 * above.
247 XFS_BUF_SET_FSPRIVATE2(bp, tp);
249 trace_xfs_trans_getsb(bip);
250 return (bp);
253 #ifdef DEBUG
254 xfs_buftarg_t *xfs_error_target;
255 int xfs_do_error;
256 int xfs_req_num;
257 int xfs_error_mod = 33;
258 #endif
261 * Get and lock the buffer for the caller if it is not already
262 * locked within the given transaction. If it has not yet been
263 * read in, read it from disk. If it is already locked
264 * within the transaction and already read in, just increment its
265 * lock recursion count and return a pointer to it.
267 * Use the fast path function xfs_trans_buf_item_match() or the buffer
268 * cache routine incore_match() to find the buffer
269 * if it is already owned by this transaction.
271 * If we don't already own the buffer, use read_buf() to get it.
272 * If it doesn't yet have an associated xfs_buf_log_item structure,
273 * then allocate one and add the item to this transaction.
275 * If the transaction pointer is NULL, make this just a normal
276 * read_buf() call.
279 xfs_trans_read_buf(
280 xfs_mount_t *mp,
281 xfs_trans_t *tp,
282 xfs_buftarg_t *target,
283 xfs_daddr_t blkno,
284 int len,
285 uint flags,
286 xfs_buf_t **bpp)
288 xfs_buf_t *bp;
289 xfs_buf_log_item_t *bip;
290 int error;
292 if (flags == 0)
293 flags = XFS_BUF_LOCK | XFS_BUF_MAPPED;
296 * Default to a normal get_buf() call if the tp is NULL.
298 if (tp == NULL) {
299 bp = xfs_buf_read(target, blkno, len, flags | BUF_BUSY);
300 if (!bp)
301 return (flags & XFS_BUF_TRYLOCK) ?
302 EAGAIN : XFS_ERROR(ENOMEM);
304 if (XFS_BUF_GETERROR(bp) != 0) {
305 xfs_ioerror_alert("xfs_trans_read_buf", mp,
306 bp, blkno);
307 error = XFS_BUF_GETERROR(bp);
308 xfs_buf_relse(bp);
309 return error;
311 #ifdef DEBUG
312 if (xfs_do_error) {
313 if (xfs_error_target == target) {
314 if (((xfs_req_num++) % xfs_error_mod) == 0) {
315 xfs_buf_relse(bp);
316 cmn_err(CE_DEBUG, "Returning error!\n");
317 return XFS_ERROR(EIO);
321 #endif
322 if (XFS_FORCED_SHUTDOWN(mp))
323 goto shutdown_abort;
324 *bpp = bp;
325 return 0;
329 * If we find the buffer in the cache with this transaction
330 * pointer in its b_fsprivate2 field, then we know we already
331 * have it locked. If it is already read in we just increment
332 * the lock recursion count and return the buffer to the caller.
333 * If the buffer is not yet read in, then we read it in, increment
334 * the lock recursion count, and return it to the caller.
336 if (tp->t_items.lic_next == NULL) {
337 bp = xfs_trans_buf_item_match(tp, target, blkno, len);
338 } else {
339 bp = xfs_trans_buf_item_match_all(tp, target, blkno, len);
341 if (bp != NULL) {
342 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
343 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
344 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
345 ASSERT((XFS_BUF_ISERROR(bp)) == 0);
346 if (!(XFS_BUF_ISDONE(bp))) {
347 trace_xfs_trans_read_buf_io(bp, _RET_IP_);
348 ASSERT(!XFS_BUF_ISASYNC(bp));
349 XFS_BUF_READ(bp);
350 xfsbdstrat(tp->t_mountp, bp);
351 error = xfs_iowait(bp);
352 if (error) {
353 xfs_ioerror_alert("xfs_trans_read_buf", mp,
354 bp, blkno);
355 xfs_buf_relse(bp);
357 * We can gracefully recover from most read
358 * errors. Ones we can't are those that happen
359 * after the transaction's already dirty.
361 if (tp->t_flags & XFS_TRANS_DIRTY)
362 xfs_force_shutdown(tp->t_mountp,
363 SHUTDOWN_META_IO_ERROR);
364 return error;
368 * We never locked this buf ourselves, so we shouldn't
369 * brelse it either. Just get out.
371 if (XFS_FORCED_SHUTDOWN(mp)) {
372 trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
373 *bpp = NULL;
374 return XFS_ERROR(EIO);
378 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
379 bip->bli_recur++;
381 ASSERT(atomic_read(&bip->bli_refcount) > 0);
382 trace_xfs_trans_read_buf_recur(bip);
383 *bpp = bp;
384 return 0;
388 * We always specify the BUF_BUSY flag within a transaction so
389 * that get_buf does not try to push out a delayed write buffer
390 * which might cause another transaction to take place (if the
391 * buffer was delayed alloc). Such recursive transactions can
392 * easily deadlock with our current transaction as well as cause
393 * us to run out of stack space.
395 bp = xfs_buf_read(target, blkno, len, flags | BUF_BUSY);
396 if (bp == NULL) {
397 *bpp = NULL;
398 return 0;
400 if (XFS_BUF_GETERROR(bp) != 0) {
401 XFS_BUF_SUPER_STALE(bp);
402 error = XFS_BUF_GETERROR(bp);
404 xfs_ioerror_alert("xfs_trans_read_buf", mp,
405 bp, blkno);
406 if (tp->t_flags & XFS_TRANS_DIRTY)
407 xfs_force_shutdown(tp->t_mountp, SHUTDOWN_META_IO_ERROR);
408 xfs_buf_relse(bp);
409 return error;
411 #ifdef DEBUG
412 if (xfs_do_error && !(tp->t_flags & XFS_TRANS_DIRTY)) {
413 if (xfs_error_target == target) {
414 if (((xfs_req_num++) % xfs_error_mod) == 0) {
415 xfs_force_shutdown(tp->t_mountp,
416 SHUTDOWN_META_IO_ERROR);
417 xfs_buf_relse(bp);
418 cmn_err(CE_DEBUG, "Returning trans error!\n");
419 return XFS_ERROR(EIO);
423 #endif
424 if (XFS_FORCED_SHUTDOWN(mp))
425 goto shutdown_abort;
428 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
429 * it doesn't have one yet, then allocate one and initialize it.
430 * The checks to see if one is there are in xfs_buf_item_init().
432 xfs_buf_item_init(bp, tp->t_mountp);
435 * Set the recursion count for the buffer within this transaction
436 * to 0.
438 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
439 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
440 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
441 ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
442 bip->bli_recur = 0;
445 * Take a reference for this transaction on the buf item.
447 atomic_inc(&bip->bli_refcount);
450 * Get a log_item_desc to point at the new item.
452 (void) xfs_trans_add_item(tp, (xfs_log_item_t*)bip);
455 * Initialize b_fsprivate2 so we can find it with incore_match()
456 * above.
458 XFS_BUF_SET_FSPRIVATE2(bp, tp);
460 trace_xfs_trans_read_buf(bip);
461 *bpp = bp;
462 return 0;
464 shutdown_abort:
466 * the theory here is that buffer is good but we're
467 * bailing out because the filesystem is being forcibly
468 * shut down. So we should leave the b_flags alone since
469 * the buffer's not staled and just get out.
471 #if defined(DEBUG)
472 if (XFS_BUF_ISSTALE(bp) && XFS_BUF_ISDELAYWRITE(bp))
473 cmn_err(CE_NOTE, "about to pop assert, bp == 0x%p", bp);
474 #endif
475 ASSERT((XFS_BUF_BFLAGS(bp) & (XFS_B_STALE|XFS_B_DELWRI)) !=
476 (XFS_B_STALE|XFS_B_DELWRI));
478 trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
479 xfs_buf_relse(bp);
480 *bpp = NULL;
481 return XFS_ERROR(EIO);
486 * Release the buffer bp which was previously acquired with one of the
487 * xfs_trans_... buffer allocation routines if the buffer has not
488 * been modified within this transaction. If the buffer is modified
489 * within this transaction, do decrement the recursion count but do
490 * not release the buffer even if the count goes to 0. If the buffer is not
491 * modified within the transaction, decrement the recursion count and
492 * release the buffer if the recursion count goes to 0.
494 * If the buffer is to be released and it was not modified before
495 * this transaction began, then free the buf_log_item associated with it.
497 * If the transaction pointer is NULL, make this just a normal
498 * brelse() call.
500 void
501 xfs_trans_brelse(xfs_trans_t *tp,
502 xfs_buf_t *bp)
504 xfs_buf_log_item_t *bip;
505 xfs_log_item_t *lip;
506 xfs_log_item_desc_t *lidp;
509 * Default to a normal brelse() call if the tp is NULL.
511 if (tp == NULL) {
512 ASSERT(XFS_BUF_FSPRIVATE2(bp, void *) == NULL);
514 * If there's a buf log item attached to the buffer,
515 * then let the AIL know that the buffer is being
516 * unlocked.
518 if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
519 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
520 if (lip->li_type == XFS_LI_BUF) {
521 bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*);
522 xfs_trans_unlocked_item(bip->bli_item.li_ailp,
523 lip);
526 xfs_buf_relse(bp);
527 return;
530 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
531 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
532 ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
533 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
534 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
535 ASSERT(atomic_read(&bip->bli_refcount) > 0);
538 * Find the item descriptor pointing to this buffer's
539 * log item. It must be there.
541 lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip);
542 ASSERT(lidp != NULL);
544 trace_xfs_trans_brelse(bip);
547 * If the release is just for a recursive lock,
548 * then decrement the count and return.
550 if (bip->bli_recur > 0) {
551 bip->bli_recur--;
552 return;
556 * If the buffer is dirty within this transaction, we can't
557 * release it until we commit.
559 if (lidp->lid_flags & XFS_LID_DIRTY)
560 return;
563 * If the buffer has been invalidated, then we can't release
564 * it until the transaction commits to disk unless it is re-dirtied
565 * as part of this transaction. This prevents us from pulling
566 * the item from the AIL before we should.
568 if (bip->bli_flags & XFS_BLI_STALE)
569 return;
571 ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
574 * Free up the log item descriptor tracking the released item.
576 xfs_trans_free_item(tp, lidp);
579 * Clear the hold flag in the buf log item if it is set.
580 * We wouldn't want the next user of the buffer to
581 * get confused.
583 if (bip->bli_flags & XFS_BLI_HOLD) {
584 bip->bli_flags &= ~XFS_BLI_HOLD;
588 * Drop our reference to the buf log item.
590 atomic_dec(&bip->bli_refcount);
593 * If the buf item is not tracking data in the log, then
594 * we must free it before releasing the buffer back to the
595 * free pool. Before releasing the buffer to the free pool,
596 * clear the transaction pointer in b_fsprivate2 to dissolve
597 * its relation to this transaction.
599 if (!xfs_buf_item_dirty(bip)) {
600 /***
601 ASSERT(bp->b_pincount == 0);
602 ***/
603 ASSERT(atomic_read(&bip->bli_refcount) == 0);
604 ASSERT(!(bip->bli_item.li_flags & XFS_LI_IN_AIL));
605 ASSERT(!(bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF));
606 xfs_buf_item_relse(bp);
607 bip = NULL;
609 XFS_BUF_SET_FSPRIVATE2(bp, NULL);
612 * If we've still got a buf log item on the buffer, then
613 * tell the AIL that the buffer is being unlocked.
615 if (bip != NULL) {
616 xfs_trans_unlocked_item(bip->bli_item.li_ailp,
617 (xfs_log_item_t*)bip);
620 xfs_buf_relse(bp);
621 return;
625 * Add the locked buffer to the transaction.
626 * The buffer must be locked, and it cannot be associated with any
627 * transaction.
629 * If the buffer does not yet have a buf log item associated with it,
630 * then allocate one for it. Then add the buf item to the transaction.
632 void
633 xfs_trans_bjoin(xfs_trans_t *tp,
634 xfs_buf_t *bp)
636 xfs_buf_log_item_t *bip;
638 ASSERT(XFS_BUF_ISBUSY(bp));
639 ASSERT(XFS_BUF_FSPRIVATE2(bp, void *) == NULL);
642 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
643 * it doesn't have one yet, then allocate one and initialize it.
644 * The checks to see if one is there are in xfs_buf_item_init().
646 xfs_buf_item_init(bp, tp->t_mountp);
647 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
648 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
649 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
650 ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
653 * Take a reference for this transaction on the buf item.
655 atomic_inc(&bip->bli_refcount);
658 * Get a log_item_desc to point at the new item.
660 (void) xfs_trans_add_item(tp, (xfs_log_item_t *)bip);
663 * Initialize b_fsprivate2 so we can find it with incore_match()
664 * in xfs_trans_get_buf() and friends above.
666 XFS_BUF_SET_FSPRIVATE2(bp, tp);
668 trace_xfs_trans_bjoin(bip);
672 * Mark the buffer as not needing to be unlocked when the buf item's
673 * IOP_UNLOCK() routine is called. The buffer must already be locked
674 * and associated with the given transaction.
676 /* ARGSUSED */
677 void
678 xfs_trans_bhold(xfs_trans_t *tp,
679 xfs_buf_t *bp)
681 xfs_buf_log_item_t *bip;
683 ASSERT(XFS_BUF_ISBUSY(bp));
684 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
685 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
687 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
688 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
689 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
690 ASSERT(atomic_read(&bip->bli_refcount) > 0);
691 bip->bli_flags |= XFS_BLI_HOLD;
692 trace_xfs_trans_bhold(bip);
696 * Cancel the previous buffer hold request made on this buffer
697 * for this transaction.
699 void
700 xfs_trans_bhold_release(xfs_trans_t *tp,
701 xfs_buf_t *bp)
703 xfs_buf_log_item_t *bip;
705 ASSERT(XFS_BUF_ISBUSY(bp));
706 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
707 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
709 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
710 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
711 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
712 ASSERT(atomic_read(&bip->bli_refcount) > 0);
713 ASSERT(bip->bli_flags & XFS_BLI_HOLD);
714 bip->bli_flags &= ~XFS_BLI_HOLD;
716 trace_xfs_trans_bhold_release(bip);
720 * This is called to mark bytes first through last inclusive of the given
721 * buffer as needing to be logged when the transaction is committed.
722 * The buffer must already be associated with the given transaction.
724 * First and last are numbers relative to the beginning of this buffer,
725 * so the first byte in the buffer is numbered 0 regardless of the
726 * value of b_blkno.
728 void
729 xfs_trans_log_buf(xfs_trans_t *tp,
730 xfs_buf_t *bp,
731 uint first,
732 uint last)
734 xfs_buf_log_item_t *bip;
735 xfs_log_item_desc_t *lidp;
737 ASSERT(XFS_BUF_ISBUSY(bp));
738 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
739 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
740 ASSERT((first <= last) && (last < XFS_BUF_COUNT(bp)));
741 ASSERT((XFS_BUF_IODONE_FUNC(bp) == NULL) ||
742 (XFS_BUF_IODONE_FUNC(bp) == xfs_buf_iodone_callbacks));
745 * Mark the buffer as needing to be written out eventually,
746 * and set its iodone function to remove the buffer's buf log
747 * item from the AIL and free it when the buffer is flushed
748 * to disk. See xfs_buf_attach_iodone() for more details
749 * on li_cb and xfs_buf_iodone_callbacks().
750 * If we end up aborting this transaction, we trap this buffer
751 * inside the b_bdstrat callback so that this won't get written to
752 * disk.
754 XFS_BUF_DELAYWRITE(bp);
755 XFS_BUF_DONE(bp);
757 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
758 ASSERT(atomic_read(&bip->bli_refcount) > 0);
759 XFS_BUF_SET_IODONE_FUNC(bp, xfs_buf_iodone_callbacks);
760 bip->bli_item.li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*))xfs_buf_iodone;
762 trace_xfs_trans_log_buf(bip);
765 * If we invalidated the buffer within this transaction, then
766 * cancel the invalidation now that we're dirtying the buffer
767 * again. There are no races with the code in xfs_buf_item_unpin(),
768 * because we have a reference to the buffer this entire time.
770 if (bip->bli_flags & XFS_BLI_STALE) {
771 bip->bli_flags &= ~XFS_BLI_STALE;
772 ASSERT(XFS_BUF_ISSTALE(bp));
773 XFS_BUF_UNSTALE(bp);
774 bip->bli_format.blf_flags &= ~XFS_BLI_CANCEL;
777 lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip);
778 ASSERT(lidp != NULL);
780 tp->t_flags |= XFS_TRANS_DIRTY;
781 lidp->lid_flags |= XFS_LID_DIRTY;
782 lidp->lid_flags &= ~XFS_LID_BUF_STALE;
783 bip->bli_flags |= XFS_BLI_LOGGED;
784 xfs_buf_item_log(bip, first, last);
789 * This called to invalidate a buffer that is being used within
790 * a transaction. Typically this is because the blocks in the
791 * buffer are being freed, so we need to prevent it from being
792 * written out when we're done. Allowing it to be written again
793 * might overwrite data in the free blocks if they are reallocated
794 * to a file.
796 * We prevent the buffer from being written out by clearing the
797 * B_DELWRI flag. We can't always
798 * get rid of the buf log item at this point, though, because
799 * the buffer may still be pinned by another transaction. If that
800 * is the case, then we'll wait until the buffer is committed to
801 * disk for the last time (we can tell by the ref count) and
802 * free it in xfs_buf_item_unpin(). Until it is cleaned up we
803 * will keep the buffer locked so that the buffer and buf log item
804 * are not reused.
806 void
807 xfs_trans_binval(
808 xfs_trans_t *tp,
809 xfs_buf_t *bp)
811 xfs_log_item_desc_t *lidp;
812 xfs_buf_log_item_t *bip;
814 ASSERT(XFS_BUF_ISBUSY(bp));
815 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
816 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
818 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
819 lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip);
820 ASSERT(lidp != NULL);
821 ASSERT(atomic_read(&bip->bli_refcount) > 0);
823 trace_xfs_trans_binval(bip);
825 if (bip->bli_flags & XFS_BLI_STALE) {
827 * If the buffer is already invalidated, then
828 * just return.
830 ASSERT(!(XFS_BUF_ISDELAYWRITE(bp)));
831 ASSERT(XFS_BUF_ISSTALE(bp));
832 ASSERT(!(bip->bli_flags & (XFS_BLI_LOGGED | XFS_BLI_DIRTY)));
833 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_INODE_BUF));
834 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
835 ASSERT(lidp->lid_flags & XFS_LID_DIRTY);
836 ASSERT(tp->t_flags & XFS_TRANS_DIRTY);
837 return;
841 * Clear the dirty bit in the buffer and set the STALE flag
842 * in the buf log item. The STALE flag will be used in
843 * xfs_buf_item_unpin() to determine if it should clean up
844 * when the last reference to the buf item is given up.
845 * We set the XFS_BLI_CANCEL flag in the buf log format structure
846 * and log the buf item. This will be used at recovery time
847 * to determine that copies of the buffer in the log before
848 * this should not be replayed.
849 * We mark the item descriptor and the transaction dirty so
850 * that we'll hold the buffer until after the commit.
852 * Since we're invalidating the buffer, we also clear the state
853 * about which parts of the buffer have been logged. We also
854 * clear the flag indicating that this is an inode buffer since
855 * the data in the buffer will no longer be valid.
857 * We set the stale bit in the buffer as well since we're getting
858 * rid of it.
860 XFS_BUF_UNDELAYWRITE(bp);
861 XFS_BUF_STALE(bp);
862 bip->bli_flags |= XFS_BLI_STALE;
863 bip->bli_flags &= ~(XFS_BLI_LOGGED | XFS_BLI_DIRTY);
864 bip->bli_format.blf_flags &= ~XFS_BLI_INODE_BUF;
865 bip->bli_format.blf_flags |= XFS_BLI_CANCEL;
866 memset((char *)(bip->bli_format.blf_data_map), 0,
867 (bip->bli_format.blf_map_size * sizeof(uint)));
868 lidp->lid_flags |= XFS_LID_DIRTY|XFS_LID_BUF_STALE;
869 tp->t_flags |= XFS_TRANS_DIRTY;
873 * This call is used to indicate that the buffer contains on-disk
874 * inodes which must be handled specially during recovery. They
875 * require special handling because only the di_next_unlinked from
876 * the inodes in the buffer should be recovered. The rest of the
877 * data in the buffer is logged via the inodes themselves.
879 * All we do is set the XFS_BLI_INODE_BUF flag in the buffer's log
880 * format structure so that we'll know what to do at recovery time.
882 /* ARGSUSED */
883 void
884 xfs_trans_inode_buf(
885 xfs_trans_t *tp,
886 xfs_buf_t *bp)
888 xfs_buf_log_item_t *bip;
890 ASSERT(XFS_BUF_ISBUSY(bp));
891 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
892 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
894 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
895 ASSERT(atomic_read(&bip->bli_refcount) > 0);
897 bip->bli_format.blf_flags |= XFS_BLI_INODE_BUF;
901 * This call is used to indicate that the buffer is going to
902 * be staled and was an inode buffer. This means it gets
903 * special processing during unpin - where any inodes
904 * associated with the buffer should be removed from ail.
905 * There is also special processing during recovery,
906 * any replay of the inodes in the buffer needs to be
907 * prevented as the buffer may have been reused.
909 void
910 xfs_trans_stale_inode_buf(
911 xfs_trans_t *tp,
912 xfs_buf_t *bp)
914 xfs_buf_log_item_t *bip;
916 ASSERT(XFS_BUF_ISBUSY(bp));
917 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
918 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
920 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
921 ASSERT(atomic_read(&bip->bli_refcount) > 0);
923 bip->bli_flags |= XFS_BLI_STALE_INODE;
924 bip->bli_item.li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*))
925 xfs_buf_iodone;
931 * Mark the buffer as being one which contains newly allocated
932 * inodes. We need to make sure that even if this buffer is
933 * relogged as an 'inode buf' we still recover all of the inode
934 * images in the face of a crash. This works in coordination with
935 * xfs_buf_item_committed() to ensure that the buffer remains in the
936 * AIL at its original location even after it has been relogged.
938 /* ARGSUSED */
939 void
940 xfs_trans_inode_alloc_buf(
941 xfs_trans_t *tp,
942 xfs_buf_t *bp)
944 xfs_buf_log_item_t *bip;
946 ASSERT(XFS_BUF_ISBUSY(bp));
947 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
948 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
950 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
951 ASSERT(atomic_read(&bip->bli_refcount) > 0);
953 bip->bli_flags |= XFS_BLI_INODE_ALLOC_BUF;
958 * Similar to xfs_trans_inode_buf(), this marks the buffer as a cluster of
959 * dquots. However, unlike in inode buffer recovery, dquot buffers get
960 * recovered in their entirety. (Hence, no XFS_BLI_DQUOT_ALLOC_BUF flag).
961 * The only thing that makes dquot buffers different from regular
962 * buffers is that we must not replay dquot bufs when recovering
963 * if a _corresponding_ quotaoff has happened. We also have to distinguish
964 * between usr dquot bufs and grp dquot bufs, because usr and grp quotas
965 * can be turned off independently.
967 /* ARGSUSED */
968 void
969 xfs_trans_dquot_buf(
970 xfs_trans_t *tp,
971 xfs_buf_t *bp,
972 uint type)
974 xfs_buf_log_item_t *bip;
976 ASSERT(XFS_BUF_ISBUSY(bp));
977 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
978 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
979 ASSERT(type == XFS_BLI_UDQUOT_BUF ||
980 type == XFS_BLI_PDQUOT_BUF ||
981 type == XFS_BLI_GDQUOT_BUF);
983 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
984 ASSERT(atomic_read(&bip->bli_refcount) > 0);
986 bip->bli_format.blf_flags |= type;
990 * Check to see if a buffer matching the given parameters is already
991 * a part of the given transaction. Only check the first, embedded
992 * chunk, since we don't want to spend all day scanning large transactions.
994 STATIC xfs_buf_t *
995 xfs_trans_buf_item_match(
996 xfs_trans_t *tp,
997 xfs_buftarg_t *target,
998 xfs_daddr_t blkno,
999 int len)
1001 xfs_log_item_chunk_t *licp;
1002 xfs_log_item_desc_t *lidp;
1003 xfs_buf_log_item_t *blip;
1004 xfs_buf_t *bp;
1005 int i;
1007 bp = NULL;
1008 len = BBTOB(len);
1009 licp = &tp->t_items;
1010 if (!xfs_lic_are_all_free(licp)) {
1011 for (i = 0; i < licp->lic_unused; i++) {
1013 * Skip unoccupied slots.
1015 if (xfs_lic_isfree(licp, i)) {
1016 continue;
1019 lidp = xfs_lic_slot(licp, i);
1020 blip = (xfs_buf_log_item_t *)lidp->lid_item;
1021 if (blip->bli_item.li_type != XFS_LI_BUF) {
1022 continue;
1025 bp = blip->bli_buf;
1026 if ((XFS_BUF_TARGET(bp) == target) &&
1027 (XFS_BUF_ADDR(bp) == blkno) &&
1028 (XFS_BUF_COUNT(bp) == len)) {
1030 * We found it. Break out and
1031 * return the pointer to the buffer.
1033 break;
1034 } else {
1035 bp = NULL;
1039 return bp;
1043 * Check to see if a buffer matching the given parameters is already
1044 * a part of the given transaction. Check all the chunks, we
1045 * want to be thorough.
1047 STATIC xfs_buf_t *
1048 xfs_trans_buf_item_match_all(
1049 xfs_trans_t *tp,
1050 xfs_buftarg_t *target,
1051 xfs_daddr_t blkno,
1052 int len)
1054 xfs_log_item_chunk_t *licp;
1055 xfs_log_item_desc_t *lidp;
1056 xfs_buf_log_item_t *blip;
1057 xfs_buf_t *bp;
1058 int i;
1060 bp = NULL;
1061 len = BBTOB(len);
1062 for (licp = &tp->t_items; licp != NULL; licp = licp->lic_next) {
1063 if (xfs_lic_are_all_free(licp)) {
1064 ASSERT(licp == &tp->t_items);
1065 ASSERT(licp->lic_next == NULL);
1066 return NULL;
1068 for (i = 0; i < licp->lic_unused; i++) {
1070 * Skip unoccupied slots.
1072 if (xfs_lic_isfree(licp, i)) {
1073 continue;
1076 lidp = xfs_lic_slot(licp, i);
1077 blip = (xfs_buf_log_item_t *)lidp->lid_item;
1078 if (blip->bli_item.li_type != XFS_LI_BUF) {
1079 continue;
1082 bp = blip->bli_buf;
1083 if ((XFS_BUF_TARGET(bp) == target) &&
1084 (XFS_BUF_ADDR(bp) == blkno) &&
1085 (XFS_BUF_COUNT(bp) == len)) {
1087 * We found it. Break out and
1088 * return the pointer to the buffer.
1090 return bp;
1094 return NULL;