1 /* 2 * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved. 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms of version 2 of the GNU General Public License as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it would be useful, but 9 * WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 11 * 12 * Further, this software is distributed without any warranty that it is 13 * free of the rightful claim of any third person regarding infringement 14 * or the like. Any license provided herein, whether implied or 15 * otherwise, applies only to this software file. Patent licenses, if 16 * any, provided herein do not apply to combinations of this program with 17 * other software, or any other product whatsoever. 18 * 19 * You should have received a copy of the GNU General Public License along 20 * with this program; if not, write the Free Software Foundation, Inc., 59 21 * Temple Place - Suite 330, Boston MA 02111-1307, USA. 22 * 23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, 24 * Mountain View, CA 94043, or: 25 * 26 * http://www.sgi.com 27 * 28 * For further information regarding this notice, see: 29 * 30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/ 31 */ 32 33 /* 34 * This file contains the implementation of the xfs_buf_log_item. 35 * It contains the item operations used to manipulate the buf log 36 * items as well as utility routines used by the buffer specific 37 * transaction routines. 38 */ 39 40 #include "xfs.h" 41 42 #include "xfs_macros.h" 43 #include "xfs_types.h" 44 #include "xfs_inum.h" 45 #include "xfs_log.h" 46 #include "xfs_trans.h" 47 #include "xfs_buf_item.h" 48 #include "xfs_sb.h" 49 #include "xfs_dir.h" 50 #include "xfs_dmapi.h" 51 #include "xfs_mount.h" 52 #include "xfs_trans_priv.h" 53 #include "xfs_rw.h" 54 #include "xfs_bit.h" 55 #include "xfs_error.h" 56 57 58 kmem_zone_t *xfs_buf_item_zone; 59 60 #ifdef XFS_TRANS_DEBUG 61 /* 62 * This function uses an alternate strategy for tracking the bytes 63 * that the user requests to be logged. This can then be used 64 * in conjunction with the bli_orig array in the buf log item to 65 * catch bugs in our callers' code. 66 * 67 * We also double check the bits set in xfs_buf_item_log using a 68 * simple algorithm to check that every byte is accounted for. 69 */ 70 STATIC void 71 xfs_buf_item_log_debug( 72 xfs_buf_log_item_t *bip, 73 uint first, 74 uint last) 75 { 76 uint x; 77 uint byte; 78 uint nbytes; 79 uint chunk_num; 80 uint word_num; 81 uint bit_num; 82 uint bit_set; 83 uint *wordp; 84 85 ASSERT(bip->bli_logged != NULL); 86 byte = first; 87 nbytes = last - first + 1; 88 bfset(bip->bli_logged, first, nbytes); 89 for (x = 0; x < nbytes; x++) { 90 chunk_num = byte >> XFS_BLI_SHIFT; 91 word_num = chunk_num >> BIT_TO_WORD_SHIFT; 92 bit_num = chunk_num & (NBWORD - 1); 93 wordp = &(bip->bli_format.blf_data_map[word_num]); 94 bit_set = *wordp & (1 << bit_num); 95 ASSERT(bit_set); 96 byte++; 97 } 98 } 99 100 /* 101 * This function is called when we flush something into a buffer without 102 * logging it. This happens for things like inodes which are logged 103 * separately from the buffer. 104 */ 105 void 106 xfs_buf_item_flush_log_debug( 107 xfs_buf_t *bp, 108 uint first, 109 uint last) 110 { 111 xfs_buf_log_item_t *bip; 112 uint nbytes; 113 114 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*); 115 if ((bip == NULL) || (bip->bli_item.li_type != XFS_LI_BUF)) { 116 return; 117 } 118 119 ASSERT(bip->bli_logged != NULL); 120 nbytes = last - first + 1; 121 bfset(bip->bli_logged, first, nbytes); 122 } 123 124 /* 125 * This function is called to verify that our caller's have logged 126 * all the bytes that they changed. 127 * 128 * It does this by comparing the original copy of the buffer stored in 129 * the buf log item's bli_orig array to the current copy of the buffer 130 * and ensuring that all bytes which miscompare are set in the bli_logged 131 * array of the buf log item. 132 */ 133 STATIC void 134 xfs_buf_item_log_check( 135 xfs_buf_log_item_t *bip) 136 { 137 char *orig; 138 char *buffer; 139 int x; 140 xfs_buf_t *bp; 141 142 ASSERT(bip->bli_orig != NULL); 143 ASSERT(bip->bli_logged != NULL); 144 145 bp = bip->bli_buf; 146 ASSERT(XFS_BUF_COUNT(bp) > 0); 147 ASSERT(XFS_BUF_PTR(bp) != NULL); 148 orig = bip->bli_orig; 149 buffer = XFS_BUF_PTR(bp); 150 for (x = 0; x < XFS_BUF_COUNT(bp); x++) { 151 if (orig[x] != buffer[x] && !btst(bip->bli_logged, x)) 152 cmn_err(CE_PANIC, 153 "xfs_buf_item_log_check bip %x buffer %x orig %x index %d", 154 bip, bp, orig, x); 155 } 156 } 157 #else 158 #define xfs_buf_item_log_debug(x,y,z) 159 #define xfs_buf_item_log_check(x) 160 #endif 161 162 STATIC void xfs_buf_error_relse(xfs_buf_t *bp); 163 STATIC void xfs_buf_do_callbacks(xfs_buf_t *bp, xfs_log_item_t *lip); 164 165 /* 166 * This returns the number of log iovecs needed to log the 167 * given buf log item. 168 * 169 * It calculates this as 1 iovec for the buf log format structure 170 * and 1 for each stretch of non-contiguous chunks to be logged. 171 * Contiguous chunks are logged in a single iovec. 172 * 173 * If the XFS_BLI_STALE flag has been set, then log nothing. 174 */ 175 STATIC uint 176 xfs_buf_item_size( 177 xfs_buf_log_item_t *bip) 178 { 179 uint nvecs; 180 int next_bit; 181 int last_bit; 182 xfs_buf_t *bp; 183 184 ASSERT(atomic_read(&bip->bli_refcount) > 0); 185 if (bip->bli_flags & XFS_BLI_STALE) { 186 /* 187 * The buffer is stale, so all we need to log 188 * is the buf log format structure with the 189 * cancel flag in it. 190 */ 191 xfs_buf_item_trace("SIZE STALE", bip); 192 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL); 193 return 1; 194 } 195 196 bp = bip->bli_buf; 197 ASSERT(bip->bli_flags & XFS_BLI_LOGGED); 198 nvecs = 1; 199 last_bit = xfs_next_bit(bip->bli_format.blf_data_map, 200 bip->bli_format.blf_map_size, 0); 201 ASSERT(last_bit != -1); 202 nvecs++; 203 while (last_bit != -1) { 204 /* 205 * This takes the bit number to start looking from and 206 * returns the next set bit from there. It returns -1 207 * if there are no more bits set or the start bit is 208 * beyond the end of the bitmap. 209 */ 210 next_bit = xfs_next_bit(bip->bli_format.blf_data_map, 211 bip->bli_format.blf_map_size, 212 last_bit + 1); 213 /* 214 * If we run out of bits, leave the loop, 215 * else if we find a new set of bits bump the number of vecs, 216 * else keep scanning the current set of bits. 217 */ 218 if (next_bit == -1) { 219 last_bit = -1; 220 } else if (next_bit != last_bit + 1) { 221 last_bit = next_bit; 222 nvecs++; 223 } else if (xfs_buf_offset(bp, next_bit * XFS_BLI_CHUNK) != 224 (xfs_buf_offset(bp, last_bit * XFS_BLI_CHUNK) + 225 XFS_BLI_CHUNK)) { 226 last_bit = next_bit; 227 nvecs++; 228 } else { 229 last_bit++; 230 } 231 } 232 233 xfs_buf_item_trace("SIZE NORM", bip); 234 return nvecs; 235 } 236 237 /* 238 * This is called to fill in the vector of log iovecs for the 239 * given log buf item. It fills the first entry with a buf log 240 * format structure, and the rest point to contiguous chunks 241 * within the buffer. 242 */ 243 STATIC void 244 xfs_buf_item_format( 245 xfs_buf_log_item_t *bip, 246 xfs_log_iovec_t *log_vector) 247 { 248 uint base_size; 249 uint nvecs; 250 xfs_log_iovec_t *vecp; 251 xfs_buf_t *bp; 252 int first_bit; 253 int last_bit; 254 int next_bit; 255 uint nbits; 256 uint buffer_offset; 257 258 ASSERT(atomic_read(&bip->bli_refcount) > 0); 259 ASSERT((bip->bli_flags & XFS_BLI_LOGGED) || 260 (bip->bli_flags & XFS_BLI_STALE)); 261 bp = bip->bli_buf; 262 ASSERT(XFS_BUF_BP_ISMAPPED(bp)); 263 vecp = log_vector; 264 265 /* 266 * The size of the base structure is the size of the 267 * declared structure plus the space for the extra words 268 * of the bitmap. We subtract one from the map size, because 269 * the first element of the bitmap is accounted for in the 270 * size of the base structure. 271 */ 272 base_size = 273 (uint)(sizeof(xfs_buf_log_format_t) + 274 ((bip->bli_format.blf_map_size - 1) * sizeof(uint))); 275 vecp->i_addr = (xfs_caddr_t)&bip->bli_format; 276 vecp->i_len = base_size; 277 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_BFORMAT); 278 vecp++; 279 nvecs = 1; 280 281 if (bip->bli_flags & XFS_BLI_STALE) { 282 /* 283 * The buffer is stale, so all we need to log 284 * is the buf log format structure with the 285 * cancel flag in it. 286 */ 287 xfs_buf_item_trace("FORMAT STALE", bip); 288 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL); 289 bip->bli_format.blf_size = nvecs; 290 return; 291 } 292 293 /* 294 * Fill in an iovec for each set of contiguous chunks. 295 */ 296 first_bit = xfs_next_bit(bip->bli_format.blf_data_map, 297 bip->bli_format.blf_map_size, 0); 298 ASSERT(first_bit != -1); 299 last_bit = first_bit; 300 nbits = 1; 301 for (;;) { 302 /* 303 * This takes the bit number to start looking from and 304 * returns the next set bit from there. It returns -1 305 * if there are no more bits set or the start bit is 306 * beyond the end of the bitmap. 307 */ 308 next_bit = xfs_next_bit(bip->bli_format.blf_data_map, 309 bip->bli_format.blf_map_size, 310 (uint)last_bit + 1); 311 /* 312 * If we run out of bits fill in the last iovec and get 313 * out of the loop. 314 * Else if we start a new set of bits then fill in the 315 * iovec for the series we were looking at and start 316 * counting the bits in the new one. 317 * Else we're still in the same set of bits so just 318 * keep counting and scanning. 319 */ 320 if (next_bit == -1) { 321 buffer_offset = first_bit * XFS_BLI_CHUNK; 322 vecp->i_addr = xfs_buf_offset(bp, buffer_offset); 323 vecp->i_len = nbits * XFS_BLI_CHUNK; 324 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_BCHUNK); 325 nvecs++; 326 break; 327 } else if (next_bit != last_bit + 1) { 328 buffer_offset = first_bit * XFS_BLI_CHUNK; 329 vecp->i_addr = xfs_buf_offset(bp, buffer_offset); 330 vecp->i_len = nbits * XFS_BLI_CHUNK; 331 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_BCHUNK); 332 nvecs++; 333 vecp++; 334 first_bit = next_bit; 335 last_bit = next_bit; 336 nbits = 1; 337 } else if (xfs_buf_offset(bp, next_bit << XFS_BLI_SHIFT) != 338 (xfs_buf_offset(bp, last_bit << XFS_BLI_SHIFT) + 339 XFS_BLI_CHUNK)) { 340 buffer_offset = first_bit * XFS_BLI_CHUNK; 341 vecp->i_addr = xfs_buf_offset(bp, buffer_offset); 342 vecp->i_len = nbits * XFS_BLI_CHUNK; 343 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_BCHUNK); 344 /* You would think we need to bump the nvecs here too, but we do not 345 * this number is used by recovery, and it gets confused by the boundary 346 * split here 347 * nvecs++; 348 */ 349 vecp++; 350 first_bit = next_bit; 351 last_bit = next_bit; 352 nbits = 1; 353 } else { 354 last_bit++; 355 nbits++; 356 } 357 } 358 bip->bli_format.blf_size = nvecs; 359 360 /* 361 * Check to make sure everything is consistent. 362 */ 363 xfs_buf_item_trace("FORMAT NORM", bip); 364 xfs_buf_item_log_check(bip); 365 } 366 367 /* 368 * This is called to pin the buffer associated with the buf log 369 * item in memory so it cannot be written out. Simply call bpin() 370 * on the buffer to do this. 371 */ 372 STATIC void 373 xfs_buf_item_pin( 374 xfs_buf_log_item_t *bip) 375 { 376 xfs_buf_t *bp; 377 378 bp = bip->bli_buf; 379 ASSERT(XFS_BUF_ISBUSY(bp)); 380 ASSERT(atomic_read(&bip->bli_refcount) > 0); 381 ASSERT((bip->bli_flags & XFS_BLI_LOGGED) || 382 (bip->bli_flags & XFS_BLI_STALE)); 383 xfs_buf_item_trace("PIN", bip); 384 xfs_buftrace("XFS_PIN", bp); 385 xfs_bpin(bp); 386 } 387 388 389 /* 390 * This is called to unpin the buffer associated with the buf log 391 * item which was previously pinned with a call to xfs_buf_item_pin(). 392 * Just call bunpin() on the buffer to do this. 393 * 394 * Also drop the reference to the buf item for the current transaction. 395 * If the XFS_BLI_STALE flag is set and we are the last reference, 396 * then free up the buf log item and unlock the buffer. 397 */ 398 STATIC void 399 xfs_buf_item_unpin( 400 xfs_buf_log_item_t *bip, 401 int stale) 402 { 403 xfs_mount_t *mp; 404 xfs_buf_t *bp; 405 int freed; 406 SPLDECL(s); 407 408 bp = bip->bli_buf; 409 ASSERT(bp != NULL); 410 ASSERT(XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *) == bip); 411 ASSERT(atomic_read(&bip->bli_refcount) > 0); 412 xfs_buf_item_trace("UNPIN", bip); 413 xfs_buftrace("XFS_UNPIN", bp); 414 415 freed = atomic_dec_and_test(&bip->bli_refcount); 416 mp = bip->bli_item.li_mountp; 417 xfs_bunpin(bp); 418 if (freed && stale) { 419 ASSERT(bip->bli_flags & XFS_BLI_STALE); 420 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0); 421 ASSERT(!(XFS_BUF_ISDELAYWRITE(bp))); 422 ASSERT(XFS_BUF_ISSTALE(bp)); 423 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL); 424 xfs_buf_item_trace("UNPIN STALE", bip); 425 xfs_buftrace("XFS_UNPIN STALE", bp); 426 /* 427 * If we get called here because of an IO error, we may 428 * or may not have the item on the AIL. xfs_trans_delete_ail() 429 * will take care of that situation. 430 * xfs_trans_delete_ail() drops the AIL lock. 431 */ 432 if (bip->bli_flags & XFS_BLI_STALE_INODE) { 433 xfs_buf_do_callbacks(bp, (xfs_log_item_t *)bip); 434 XFS_BUF_SET_FSPRIVATE(bp, NULL); 435 XFS_BUF_CLR_IODONE_FUNC(bp); 436 } else { 437 AIL_LOCK(mp,s); 438 xfs_trans_delete_ail(mp, (xfs_log_item_t *)bip, s); 439 xfs_buf_item_relse(bp); 440 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) == NULL); 441 } 442 xfs_buf_relse(bp); 443 } 444 } 445 446 /* 447 * this is called from uncommit in the forced-shutdown path. 448 * we need to check to see if the reference count on the log item 449 * is going to drop to zero. If so, unpin will free the log item 450 * so we need to free the item's descriptor (that points to the item) 451 * in the transaction. 452 */ 453 STATIC void 454 xfs_buf_item_unpin_remove( 455 xfs_buf_log_item_t *bip, 456 xfs_trans_t *tp) 457 { 458 xfs_buf_t *bp; 459 xfs_log_item_desc_t *lidp; 460 int stale = 0; 461 462 bp = bip->bli_buf; 463 /* 464 * will xfs_buf_item_unpin() call xfs_buf_item_relse()? 465 */ 466 if ((atomic_read(&bip->bli_refcount) == 1) && 467 (bip->bli_flags & XFS_BLI_STALE)) { 468 ASSERT(XFS_BUF_VALUSEMA(bip->bli_buf) <= 0); 469 xfs_buf_item_trace("UNPIN REMOVE", bip); 470 xfs_buftrace("XFS_UNPIN_REMOVE", bp); 471 /* 472 * yes -- clear the xaction descriptor in-use flag 473 * and free the chunk if required. We can safely 474 * do some work here and then call buf_item_unpin 475 * to do the rest because if the if is true, then 476 * we are holding the buffer locked so no one else 477 * will be able to bump up the refcount. 478 */ 479 lidp = xfs_trans_find_item(tp, (xfs_log_item_t *) bip); 480 stale = lidp->lid_flags & XFS_LID_BUF_STALE; 481 xfs_trans_free_item(tp, lidp); 482 /* 483 * Since the transaction no longer refers to the buffer, 484 * the buffer should no longer refer to the transaction. 485 */ 486 XFS_BUF_SET_FSPRIVATE2(bp, NULL); 487 } 488 489 xfs_buf_item_unpin(bip, stale); 490 491 return; 492 } 493 494 /* 495 * This is called to attempt to lock the buffer associated with this 496 * buf log item. Don't sleep on the buffer lock. If we can't get 497 * the lock right away, return 0. If we can get the lock, pull the 498 * buffer from the free list, mark it busy, and return 1. 499 */ 500 STATIC uint 501 xfs_buf_item_trylock( 502 xfs_buf_log_item_t *bip) 503 { 504 xfs_buf_t *bp; 505 506 bp = bip->bli_buf; 507 508 if (XFS_BUF_ISPINNED(bp)) { 509 return XFS_ITEM_PINNED; 510 } 511 512 if (!XFS_BUF_CPSEMA(bp)) { 513 return XFS_ITEM_LOCKED; 514 } 515 516 /* 517 * Remove the buffer from the free list. Only do this 518 * if it's on the free list. Private buffers like the 519 * superblock buffer are not. 520 */ 521 XFS_BUF_HOLD(bp); 522 523 ASSERT(!(bip->bli_flags & XFS_BLI_STALE)); 524 xfs_buf_item_trace("TRYLOCK SUCCESS", bip); 525 return XFS_ITEM_SUCCESS; 526 } 527 528 /* 529 * Release the buffer associated with the buf log item. 530 * If there is no dirty logged data associated with the 531 * buffer recorded in the buf log item, then free the 532 * buf log item and remove the reference to it in the 533 * buffer. 534 * 535 * This call ignores the recursion count. It is only called 536 * when the buffer should REALLY be unlocked, regardless 537 * of the recursion count. 538 * 539 * If the XFS_BLI_HOLD flag is set in the buf log item, then 540 * free the log item if necessary but do not unlock the buffer. 541 * This is for support of xfs_trans_bhold(). Make sure the 542 * XFS_BLI_HOLD field is cleared if we don't free the item. 543 */ 544 STATIC void 545 xfs_buf_item_unlock( 546 xfs_buf_log_item_t *bip) 547 { 548 int aborted; 549 xfs_buf_t *bp; 550 uint hold; 551 552 bp = bip->bli_buf; 553 xfs_buftrace("XFS_UNLOCK", bp); 554 555 /* 556 * Clear the buffer's association with this transaction. 557 */ 558 XFS_BUF_SET_FSPRIVATE2(bp, NULL); 559 560 /* 561 * If this is a transaction abort, don't return early. 562 * Instead, allow the brelse to happen. 563 * Normally it would be done for stale (cancelled) buffers 564 * at unpin time, but we'll never go through the pin/unpin 565 * cycle if we abort inside commit. 566 */ 567 aborted = (bip->bli_item.li_flags & XFS_LI_ABORTED) != 0; 568 569 /* 570 * If the buf item is marked stale, then don't do anything. 571 * We'll unlock the buffer and free the buf item when the 572 * buffer is unpinned for the last time. 573 */ 574 if (bip->bli_flags & XFS_BLI_STALE) { 575 bip->bli_flags &= ~XFS_BLI_LOGGED; 576 xfs_buf_item_trace("UNLOCK STALE", bip); 577 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL); 578 if (!aborted) 579 return; 580 } 581 582 /* 583 * Drop the transaction's reference to the log item if 584 * it was not logged as part of the transaction. Otherwise 585 * we'll drop the reference in xfs_buf_item_unpin() when 586 * the transaction is really through with the buffer. 587 */ 588 if (!(bip->bli_flags & XFS_BLI_LOGGED)) { 589 atomic_dec(&bip->bli_refcount); 590 } else { 591 /* 592 * Clear the logged flag since this is per 593 * transaction state. 594 */ 595 bip->bli_flags &= ~XFS_BLI_LOGGED; 596 } 597 598 /* 599 * Before possibly freeing the buf item, determine if we should 600 * release the buffer at the end of this routine. 601 */ 602 hold = bip->bli_flags & XFS_BLI_HOLD; 603 xfs_buf_item_trace("UNLOCK", bip); 604 605 /* 606 * If the buf item isn't tracking any data, free it. 607 * Otherwise, if XFS_BLI_HOLD is set clear it. 608 */ 609 if (xfs_count_bits(bip->bli_format.blf_data_map, 610 bip->bli_format.blf_map_size, 0) == 0) { 611 xfs_buf_item_relse(bp); 612 } else if (hold) { 613 bip->bli_flags &= ~XFS_BLI_HOLD; 614 } 615 616 /* 617 * Release the buffer if XFS_BLI_HOLD was not set. 618 */ 619 if (!hold) { 620 xfs_buf_relse(bp); 621 } 622 } 623 624 /* 625 * This is called to find out where the oldest active copy of the 626 * buf log item in the on disk log resides now that the last log 627 * write of it completed at the given lsn. 628 * We always re-log all the dirty data in a buffer, so usually the 629 * latest copy in the on disk log is the only one that matters. For 630 * those cases we simply return the given lsn. 631 * 632 * The one exception to this is for buffers full of newly allocated 633 * inodes. These buffers are only relogged with the XFS_BLI_INODE_BUF 634 * flag set, indicating that only the di_next_unlinked fields from the 635 * inodes in the buffers will be replayed during recovery. If the 636 * original newly allocated inode images have not yet been flushed 637 * when the buffer is so relogged, then we need to make sure that we 638 * keep the old images in the 'active' portion of the log. We do this 639 * by returning the original lsn of that transaction here rather than 640 * the current one. 641 */ 642 STATIC xfs_lsn_t 643 xfs_buf_item_committed( 644 xfs_buf_log_item_t *bip, 645 xfs_lsn_t lsn) 646 { 647 xfs_buf_item_trace("COMMITTED", bip); 648 if ((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) && 649 (bip->bli_item.li_lsn != 0)) { 650 return bip->bli_item.li_lsn; 651 } 652 return (lsn); 653 } 654 655 /* 656 * This is called when the transaction holding the buffer is aborted. 657 * Just behave as if the transaction had been cancelled. If we're shutting down 658 * and have aborted this transaction, we'll trap this buffer when it tries to 659 * get written out. 660 */ 661 STATIC void 662 xfs_buf_item_abort( 663 xfs_buf_log_item_t *bip) 664 { 665 xfs_buf_t *bp; 666 667 bp = bip->bli_buf; 668 xfs_buftrace("XFS_ABORT", bp); 669 XFS_BUF_SUPER_STALE(bp); 670 xfs_buf_item_unlock(bip); 671 return; 672 } 673 674 /* 675 * This is called to asynchronously write the buffer associated with this 676 * buf log item out to disk. The buffer will already have been locked by 677 * a successful call to xfs_buf_item_trylock(). If the buffer still has 678 * B_DELWRI set, then get it going out to disk with a call to bawrite(). 679 * If not, then just release the buffer. 680 */ 681 STATIC void 682 xfs_buf_item_push( 683 xfs_buf_log_item_t *bip) 684 { 685 xfs_buf_t *bp; 686 687 ASSERT(!(bip->bli_flags & XFS_BLI_STALE)); 688 xfs_buf_item_trace("PUSH", bip); 689 690 bp = bip->bli_buf; 691 692 if (XFS_BUF_ISDELAYWRITE(bp)) { 693 xfs_bawrite(bip->bli_item.li_mountp, bp); 694 } else { 695 xfs_buf_relse(bp); 696 } 697 } 698 699 /* ARGSUSED */ 700 STATIC void 701 xfs_buf_item_committing(xfs_buf_log_item_t *bip, xfs_lsn_t commit_lsn) 702 { 703 } 704 705 /* 706 * This is the ops vector shared by all buf log items. 707 */ 708 STATIC struct xfs_item_ops xfs_buf_item_ops = { 709 .iop_size = (uint(*)(xfs_log_item_t*))xfs_buf_item_size, 710 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*)) 711 xfs_buf_item_format, 712 .iop_pin = (void(*)(xfs_log_item_t*))xfs_buf_item_pin, 713 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_buf_item_unpin, 714 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t *)) 715 xfs_buf_item_unpin_remove, 716 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_buf_item_trylock, 717 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_buf_item_unlock, 718 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t)) 719 xfs_buf_item_committed, 720 .iop_push = (void(*)(xfs_log_item_t*))xfs_buf_item_push, 721 .iop_abort = (void(*)(xfs_log_item_t*))xfs_buf_item_abort, 722 .iop_pushbuf = NULL, 723 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t)) 724 xfs_buf_item_committing 725 }; 726 727 728 /* 729 * Allocate a new buf log item to go with the given buffer. 730 * Set the buffer's b_fsprivate field to point to the new 731 * buf log item. If there are other item's attached to the 732 * buffer (see xfs_buf_attach_iodone() below), then put the 733 * buf log item at the front. 734 */ 735 void 736 xfs_buf_item_init( 737 xfs_buf_t *bp, 738 xfs_mount_t *mp) 739 { 740 xfs_log_item_t *lip; 741 xfs_buf_log_item_t *bip; 742 int chunks; 743 int map_size; 744 745 /* 746 * Check to see if there is already a buf log item for 747 * this buffer. If there is, it is guaranteed to be 748 * the first. If we do already have one, there is 749 * nothing to do here so return. 750 */ 751 if (XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *) != mp) 752 XFS_BUF_SET_FSPRIVATE3(bp, mp); 753 XFS_BUF_SET_BDSTRAT_FUNC(bp, xfs_bdstrat_cb); 754 if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) { 755 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *); 756 if (lip->li_type == XFS_LI_BUF) { 757 return; 758 } 759 } 760 761 /* 762 * chunks is the number of XFS_BLI_CHUNK size pieces 763 * the buffer can be divided into. Make sure not to 764 * truncate any pieces. map_size is the size of the 765 * bitmap needed to describe the chunks of the buffer. 766 */ 767 chunks = (int)((XFS_BUF_COUNT(bp) + (XFS_BLI_CHUNK - 1)) >> XFS_BLI_SHIFT); 768 map_size = (int)((chunks + NBWORD) >> BIT_TO_WORD_SHIFT); 769 770 bip = (xfs_buf_log_item_t*)kmem_zone_zalloc(xfs_buf_item_zone, 771 KM_SLEEP); 772 bip->bli_item.li_type = XFS_LI_BUF; 773 bip->bli_item.li_ops = &xfs_buf_item_ops; 774 bip->bli_item.li_mountp = mp; 775 bip->bli_buf = bp; 776 bip->bli_format.blf_type = XFS_LI_BUF; 777 bip->bli_format.blf_blkno = (__int64_t)XFS_BUF_ADDR(bp); 778 bip->bli_format.blf_len = (ushort)BTOBB(XFS_BUF_COUNT(bp)); 779 bip->bli_format.blf_map_size = map_size; 780 #ifdef XFS_BLI_TRACE 781 bip->bli_trace = ktrace_alloc(XFS_BLI_TRACE_SIZE, KM_SLEEP); 782 #endif 783 784 #ifdef XFS_TRANS_DEBUG 785 /* 786 * Allocate the arrays for tracking what needs to be logged 787 * and what our callers request to be logged. bli_orig 788 * holds a copy of the original, clean buffer for comparison 789 * against, and bli_logged keeps a 1 bit flag per byte in 790 * the buffer to indicate which bytes the callers have asked 791 * to have logged. 792 */ 793 bip->bli_orig = (char *)kmem_alloc(XFS_BUF_COUNT(bp), KM_SLEEP); 794 memcpy(bip->bli_orig, XFS_BUF_PTR(bp), XFS_BUF_COUNT(bp)); 795 bip->bli_logged = (char *)kmem_zalloc(XFS_BUF_COUNT(bp) / NBBY, KM_SLEEP); 796 #endif 797 798 /* 799 * Put the buf item into the list of items attached to the 800 * buffer at the front. 801 */ 802 if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) { 803 bip->bli_item.li_bio_list = 804 XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *); 805 } 806 XFS_BUF_SET_FSPRIVATE(bp, bip); 807 } 808 809 810 /* 811 * Mark bytes first through last inclusive as dirty in the buf 812 * item's bitmap. 813 */ 814 void 815 xfs_buf_item_log( 816 xfs_buf_log_item_t *bip, 817 uint first, 818 uint last) 819 { 820 uint first_bit; 821 uint last_bit; 822 uint bits_to_set; 823 uint bits_set; 824 uint word_num; 825 uint *wordp; 826 uint bit; 827 uint end_bit; 828 uint mask; 829 830 /* 831 * Mark the item as having some dirty data for 832 * quick reference in xfs_buf_item_dirty. 833 */ 834 bip->bli_flags |= XFS_BLI_DIRTY; 835 836 /* 837 * Convert byte offsets to bit numbers. 838 */ 839 first_bit = first >> XFS_BLI_SHIFT; 840 last_bit = last >> XFS_BLI_SHIFT; 841 842 /* 843 * Calculate the total number of bits to be set. 844 */ 845 bits_to_set = last_bit - first_bit + 1; 846 847 /* 848 * Get a pointer to the first word in the bitmap 849 * to set a bit in. 850 */ 851 word_num = first_bit >> BIT_TO_WORD_SHIFT; 852 wordp = &(bip->bli_format.blf_data_map[word_num]); 853 854 /* 855 * Calculate the starting bit in the first word. 856 */ 857 bit = first_bit & (uint)(NBWORD - 1); 858 859 /* 860 * First set any bits in the first word of our range. 861 * If it starts at bit 0 of the word, it will be 862 * set below rather than here. That is what the variable 863 * bit tells us. The variable bits_set tracks the number 864 * of bits that have been set so far. End_bit is the number 865 * of the last bit to be set in this word plus one. 866 */ 867 if (bit) { 868 end_bit = MIN(bit + bits_to_set, (uint)NBWORD); 869 mask = ((1 << (end_bit - bit)) - 1) << bit; 870 *wordp |= mask; 871 wordp++; 872 bits_set = end_bit - bit; 873 } else { 874 bits_set = 0; 875 } 876 877 /* 878 * Now set bits a whole word at a time that are between 879 * first_bit and last_bit. 880 */ 881 while ((bits_to_set - bits_set) >= NBWORD) { 882 *wordp |= 0xffffffff; 883 bits_set += NBWORD; 884 wordp++; 885 } 886 887 /* 888 * Finally, set any bits left to be set in one last partial word. 889 */ 890 end_bit = bits_to_set - bits_set; 891 if (end_bit) { 892 mask = (1 << end_bit) - 1; 893 *wordp |= mask; 894 } 895 896 xfs_buf_item_log_debug(bip, first, last); 897 } 898 899 900 /* 901 * Return 1 if the buffer has some data that has been logged (at any 902 * point, not just the current transaction) and 0 if not. 903 */ 904 uint 905 xfs_buf_item_dirty( 906 xfs_buf_log_item_t *bip) 907 { 908 return (bip->bli_flags & XFS_BLI_DIRTY); 909 } 910 911 /* 912 * This is called when the buf log item is no longer needed. It should 913 * free the buf log item associated with the given buffer and clear 914 * the buffer's pointer to the buf log item. If there are no more 915 * items in the list, clear the b_iodone field of the buffer (see 916 * xfs_buf_attach_iodone() below). 917 */ 918 void 919 xfs_buf_item_relse( 920 xfs_buf_t *bp) 921 { 922 xfs_buf_log_item_t *bip; 923 924 xfs_buftrace("XFS_RELSE", bp); 925 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*); 926 XFS_BUF_SET_FSPRIVATE(bp, bip->bli_item.li_bio_list); 927 if ((XFS_BUF_FSPRIVATE(bp, void *) == NULL) && 928 (XFS_BUF_IODONE_FUNC(bp) != NULL)) { 929 ASSERT((XFS_BUF_ISUNINITIAL(bp)) == 0); 930 XFS_BUF_CLR_IODONE_FUNC(bp); 931 } 932 933 #ifdef XFS_TRANS_DEBUG 934 kmem_free(bip->bli_orig, XFS_BUF_COUNT(bp)); 935 bip->bli_orig = NULL; 936 kmem_free(bip->bli_logged, XFS_BUF_COUNT(bp) / NBBY); 937 bip->bli_logged = NULL; 938 #endif /* XFS_TRANS_DEBUG */ 939 940 #ifdef XFS_BLI_TRACE 941 ktrace_free(bip->bli_trace); 942 #endif 943 kmem_zone_free(xfs_buf_item_zone, bip); 944 } 945 946 947 /* 948 * Add the given log item with its callback to the list of callbacks 949 * to be called when the buffer's I/O completes. If it is not set 950 * already, set the buffer's b_iodone() routine to be 951 * xfs_buf_iodone_callbacks() and link the log item into the list of 952 * items rooted at b_fsprivate. Items are always added as the second 953 * entry in the list if there is a first, because the buf item code 954 * assumes that the buf log item is first. 955 */ 956 void 957 xfs_buf_attach_iodone( 958 xfs_buf_t *bp, 959 void (*cb)(xfs_buf_t *, xfs_log_item_t *), 960 xfs_log_item_t *lip) 961 { 962 xfs_log_item_t *head_lip; 963 964 ASSERT(XFS_BUF_ISBUSY(bp)); 965 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0); 966 967 lip->li_cb = cb; 968 if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) { 969 head_lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *); 970 lip->li_bio_list = head_lip->li_bio_list; 971 head_lip->li_bio_list = lip; 972 } else { 973 XFS_BUF_SET_FSPRIVATE(bp, lip); 974 } 975 976 ASSERT((XFS_BUF_IODONE_FUNC(bp) == xfs_buf_iodone_callbacks) || 977 (XFS_BUF_IODONE_FUNC(bp) == NULL)); 978 XFS_BUF_SET_IODONE_FUNC(bp, xfs_buf_iodone_callbacks); 979 } 980 981 STATIC void 982 xfs_buf_do_callbacks( 983 xfs_buf_t *bp, 984 xfs_log_item_t *lip) 985 { 986 xfs_log_item_t *nlip; 987 988 while (lip != NULL) { 989 nlip = lip->li_bio_list; 990 ASSERT(lip->li_cb != NULL); 991 /* 992 * Clear the next pointer so we don't have any 993 * confusion if the item is added to another buf. 994 * Don't touch the log item after calling its 995 * callback, because it could have freed itself. 996 */ 997 lip->li_bio_list = NULL; 998 lip->li_cb(bp, lip); 999 lip = nlip; 1000 } 1001 } 1002 1003 /* 1004 * This is the iodone() function for buffers which have had callbacks 1005 * attached to them by xfs_buf_attach_iodone(). It should remove each 1006 * log item from the buffer's list and call the callback of each in turn. 1007 * When done, the buffer's fsprivate field is set to NULL and the buffer 1008 * is unlocked with a call to iodone(). 1009 */ 1010 void 1011 xfs_buf_iodone_callbacks( 1012 xfs_buf_t *bp) 1013 { 1014 xfs_log_item_t *lip; 1015 static ulong lasttime; 1016 static xfs_buftarg_t *lasttarg; 1017 xfs_mount_t *mp; 1018 1019 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL); 1020 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *); 1021 1022 if (XFS_BUF_GETERROR(bp) != 0) { 1023 /* 1024 * If we've already decided to shutdown the filesystem 1025 * because of IO errors, there's no point in giving this 1026 * a retry. 1027 */ 1028 mp = lip->li_mountp; 1029 if (XFS_FORCED_SHUTDOWN(mp)) { 1030 ASSERT(XFS_BUF_TARGET(bp) == mp->m_ddev_targp); 1031 XFS_BUF_SUPER_STALE(bp); 1032 xfs_buftrace("BUF_IODONE_CB", bp); 1033 xfs_buf_do_callbacks(bp, lip); 1034 XFS_BUF_SET_FSPRIVATE(bp, NULL); 1035 XFS_BUF_CLR_IODONE_FUNC(bp); 1036 1037 /* 1038 * XFS_SHUT flag gets set when we go thru the 1039 * entire buffer cache and deliberately start 1040 * throwing away delayed write buffers. 1041 * Since there's no biowait done on those, 1042 * we should just brelse them. 1043 */ 1044 if (XFS_BUF_ISSHUT(bp)) { 1045 XFS_BUF_UNSHUT(bp); 1046 xfs_buf_relse(bp); 1047 } else { 1048 xfs_biodone(bp); 1049 } 1050 1051 return; 1052 } 1053 1054 if ((XFS_BUF_TARGET(bp) != lasttarg) || 1055 (time_after(jiffies, (lasttime + 5*HZ)))) { 1056 lasttime = jiffies; 1057 prdev("XFS write error in file system meta-data " 1058 "block 0x%llx in %s", 1059 XFS_BUF_TARGET(bp), 1060 (__uint64_t)XFS_BUF_ADDR(bp), mp->m_fsname); 1061 } 1062 lasttarg = XFS_BUF_TARGET(bp); 1063 1064 if (XFS_BUF_ISASYNC(bp)) { 1065 /* 1066 * If the write was asynchronous then noone will be 1067 * looking for the error. Clear the error state 1068 * and write the buffer out again delayed write. 1069 * 1070 * XXXsup This is OK, so long as we catch these 1071 * before we start the umount; we don't want these 1072 * DELWRI metadata bufs to be hanging around. 1073 */ 1074 XFS_BUF_ERROR(bp,0); /* errno of 0 unsets the flag */ 1075 1076 if (!(XFS_BUF_ISSTALE(bp))) { 1077 XFS_BUF_DELAYWRITE(bp); 1078 XFS_BUF_DONE(bp); 1079 XFS_BUF_SET_START(bp); 1080 } 1081 ASSERT(XFS_BUF_IODONE_FUNC(bp)); 1082 xfs_buftrace("BUF_IODONE ASYNC", bp); 1083 xfs_buf_relse(bp); 1084 } else { 1085 /* 1086 * If the write of the buffer was not asynchronous, 1087 * then we want to make sure to return the error 1088 * to the caller of bwrite(). Because of this we 1089 * cannot clear the B_ERROR state at this point. 1090 * Instead we install a callback function that 1091 * will be called when the buffer is released, and 1092 * that routine will clear the error state and 1093 * set the buffer to be written out again after 1094 * some delay. 1095 */ 1096 /* We actually overwrite the existing b-relse 1097 function at times, but we're gonna be shutting down 1098 anyway. */ 1099 XFS_BUF_SET_BRELSE_FUNC(bp,xfs_buf_error_relse); 1100 XFS_BUF_DONE(bp); 1101 XFS_BUF_V_IODONESEMA(bp); 1102 } 1103 return; 1104 } 1105 #ifdef XFSERRORDEBUG 1106 xfs_buftrace("XFS BUFCB NOERR", bp); 1107 #endif 1108 xfs_buf_do_callbacks(bp, lip); 1109 XFS_BUF_SET_FSPRIVATE(bp, NULL); 1110 XFS_BUF_CLR_IODONE_FUNC(bp); 1111 xfs_biodone(bp); 1112 } 1113 1114 /* 1115 * This is a callback routine attached to a buffer which gets an error 1116 * when being written out synchronously. 1117 */ 1118 STATIC void 1119 xfs_buf_error_relse( 1120 xfs_buf_t *bp) 1121 { 1122 xfs_log_item_t *lip; 1123 xfs_mount_t *mp; 1124 1125 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *); 1126 mp = (xfs_mount_t *)lip->li_mountp; 1127 ASSERT(XFS_BUF_TARGET(bp) == mp->m_ddev_targp); 1128 1129 XFS_BUF_STALE(bp); 1130 XFS_BUF_DONE(bp); 1131 XFS_BUF_UNDELAYWRITE(bp); 1132 XFS_BUF_ERROR(bp,0); 1133 xfs_buftrace("BUF_ERROR_RELSE", bp); 1134 if (! XFS_FORCED_SHUTDOWN(mp)) 1135 xfs_force_shutdown(mp, XFS_METADATA_IO_ERROR); 1136 /* 1137 * We have to unpin the pinned buffers so do the 1138 * callbacks. 1139 */ 1140 xfs_buf_do_callbacks(bp, lip); 1141 XFS_BUF_SET_FSPRIVATE(bp, NULL); 1142 XFS_BUF_CLR_IODONE_FUNC(bp); 1143 XFS_BUF_SET_BRELSE_FUNC(bp,NULL); 1144 xfs_buf_relse(bp); 1145 } 1146 1147 1148 /* 1149 * This is the iodone() function for buffers which have been 1150 * logged. It is called when they are eventually flushed out. 1151 * It should remove the buf item from the AIL, and free the buf item. 1152 * It is called by xfs_buf_iodone_callbacks() above which will take 1153 * care of cleaning up the buffer itself. 1154 */ 1155 /* ARGSUSED */ 1156 void 1157 xfs_buf_iodone( 1158 xfs_buf_t *bp, 1159 xfs_buf_log_item_t *bip) 1160 { 1161 struct xfs_mount *mp; 1162 SPLDECL(s); 1163 1164 ASSERT(bip->bli_buf == bp); 1165 1166 mp = bip->bli_item.li_mountp; 1167 1168 /* 1169 * If we are forcibly shutting down, this may well be 1170 * off the AIL already. That's because we simulate the 1171 * log-committed callbacks to unpin these buffers. Or we may never 1172 * have put this item on AIL because of the transaction was 1173 * aborted forcibly. xfs_trans_delete_ail() takes care of these. 1174 * 1175 * Either way, AIL is useless if we're forcing a shutdown. 1176 */ 1177 AIL_LOCK(mp,s); 1178 /* 1179 * xfs_trans_delete_ail() drops the AIL lock. 1180 */ 1181 xfs_trans_delete_ail(mp, (xfs_log_item_t *)bip, s); 1182 1183 #ifdef XFS_TRANS_DEBUG 1184 kmem_free(bip->bli_orig, XFS_BUF_COUNT(bp)); 1185 bip->bli_orig = NULL; 1186 kmem_free(bip->bli_logged, XFS_BUF_COUNT(bp) / NBBY); 1187 bip->bli_logged = NULL; 1188 #endif /* XFS_TRANS_DEBUG */ 1189 1190 #ifdef XFS_BLI_TRACE 1191 ktrace_free(bip->bli_trace); 1192 #endif 1193 kmem_zone_free(xfs_buf_item_zone, bip); 1194 } 1195 1196 #if defined(XFS_BLI_TRACE) 1197 void 1198 xfs_buf_item_trace( 1199 char *id, 1200 xfs_buf_log_item_t *bip) 1201 { 1202 xfs_buf_t *bp; 1203 ASSERT(bip->bli_trace != NULL); 1204 1205 bp = bip->bli_buf; 1206 ktrace_enter(bip->bli_trace, 1207 (void *)id, 1208 (void *)bip->bli_buf, 1209 (void *)((unsigned long)bip->bli_flags), 1210 (void *)((unsigned long)bip->bli_recur), 1211 (void *)((unsigned long)atomic_read(&bip->bli_refcount)), 1212 (void *)((unsigned long) 1213 (0xFFFFFFFF & XFS_BUF_ADDR(bp) >> 32)), 1214 (void *)((unsigned long)(0xFFFFFFFF & XFS_BUF_ADDR(bp))), 1215 (void *)((unsigned long)XFS_BUF_COUNT(bp)), 1216 (void *)((unsigned long)XFS_BUF_BFLAGS(bp)), 1217 XFS_BUF_FSPRIVATE(bp, void *), 1218 XFS_BUF_FSPRIVATE2(bp, void *), 1219 (void *)(unsigned long)XFS_BUF_ISPINNED(bp), 1220 (void *)XFS_BUF_IODONE_FUNC(bp), 1221 (void *)((unsigned long)(XFS_BUF_VALUSEMA(bp))), 1222 (void *)bip->bli_item.li_desc, 1223 (void *)((unsigned long)bip->bli_item.li_flags)); 1224 } 1225 #endif /* XFS_BLI_TRACE */ 1226