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