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