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