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