1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2000-2006 Silicon Graphics, Inc. 4 * All Rights Reserved. 5 */ 6 #include "xfs_platform.h" 7 #include <linux/backing-dev.h> 8 #include <linux/dax.h> 9 10 #include "xfs_shared.h" 11 #include "xfs_format.h" 12 #include "xfs_log_format.h" 13 #include "xfs_trans_resv.h" 14 #include "xfs_mount.h" 15 #include "xfs_trace.h" 16 #include "xfs_log.h" 17 #include "xfs_log_recover.h" 18 #include "xfs_log_priv.h" 19 #include "xfs_trans.h" 20 #include "xfs_buf_item.h" 21 #include "xfs_errortag.h" 22 #include "xfs_error.h" 23 #include "xfs_ag.h" 24 #include "xfs_buf_mem.h" 25 #include "xfs_notify_failure.h" 26 27 struct kmem_cache *xfs_buf_cache; 28 29 /* 30 * Locking orders 31 * 32 * xfs_buf_stale: 33 * b_sema (caller holds) 34 * b_lockref.lock 35 * lru_lock 36 * 37 * xfs_buf_rele: 38 * b_lockref.lock 39 * lru_lock 40 * 41 * xfs_buftarg_drain_rele 42 * lru_lock 43 * b_lockref.lock (trylock due to inversion) 44 * 45 * xfs_buftarg_isolate 46 * lru_lock 47 * b_lockref.lock (trylock due to inversion) 48 */ 49 50 static void xfs_buf_submit(struct xfs_buf *bp); 51 static int xfs_buf_iowait(struct xfs_buf *bp); 52 53 static inline bool xfs_buf_is_uncached(struct xfs_buf *bp) 54 { 55 return bp->b_rhash_key == XFS_BUF_DADDR_NULL; 56 } 57 58 static inline void 59 xfs_buf_set_flags( 60 struct xfs_buf *bp, 61 unsigned int flags) 62 { 63 WRITE_ONCE(bp->b_flags, bp->b_flags | flags); 64 } 65 66 static inline void 67 xfs_buf_clear_flags( 68 struct xfs_buf *bp, 69 unsigned int flags) 70 { 71 WRITE_ONCE(bp->b_flags, bp->b_flags & ~flags); 72 } 73 74 void 75 xfs_buf_set_uptodate( 76 struct xfs_buf *bp) 77 { 78 xfs_buf_set_flags(bp, XBF_DONE); 79 } 80 81 /* 82 * When we mark a buffer stale, we remove the buffer from the LRU and clear the 83 * b_lru_ref count so that the buffer is freed immediately when the buffer 84 * reference count falls to zero. If the buffer is already on the LRU, we need 85 * to remove the reference that LRU holds on the buffer. 86 * 87 * This prevents build-up of stale buffers on the LRU. 88 */ 89 void 90 xfs_buf_stale( 91 struct xfs_buf *bp) 92 { 93 ASSERT(xfs_buf_islocked(bp)); 94 95 xfs_buf_set_flags(bp, XBF_STALE); 96 97 /* 98 * Clear the delwri status so that a delwri queue walker will not 99 * flush this buffer to disk now that it is stale. The delwri queue has 100 * a reference to the buffer, so this is safe to do. 101 */ 102 xfs_buf_clear_flags(bp, _XBF_DELWRI_Q); 103 104 spin_lock(&bp->b_lockref.lock); 105 atomic_set(&bp->b_lru_ref, 0); 106 if (!lockref_is_dead(&bp->b_lockref)) 107 list_lru_del_obj(&bp->b_target->bt_lru, &bp->b_lru); 108 spin_unlock(&bp->b_lockref.lock); 109 } 110 111 void 112 xfs_buf_clear_stale( 113 struct xfs_buf *bp) 114 { 115 ASSERT(bp->b_flags & XBF_STALE); 116 xfs_buf_clear_flags(bp, XBF_STALE); 117 } 118 119 static void 120 xfs_buf_free_callback( 121 struct callback_head *cb) 122 { 123 struct xfs_buf *bp = container_of(cb, struct xfs_buf, b_rcu); 124 125 if (bp->b_maps != &bp->__b_map) 126 kfree(bp->b_maps); 127 kmem_cache_free(xfs_buf_cache, bp); 128 } 129 130 static void 131 xfs_buf_free( 132 struct xfs_buf *bp) 133 { 134 unsigned int size = BBTOB(bp->b_length); 135 136 might_sleep(); 137 trace_xfs_buf_free(bp, _RET_IP_); 138 139 ASSERT(list_empty(&bp->b_lru)); 140 141 if (!xfs_buftarg_is_mem(bp->b_target) && size >= PAGE_SIZE) 142 mm_account_reclaimed_pages(howmany(size, PAGE_SIZE)); 143 144 if (is_vmalloc_addr(bp->b_addr)) 145 vfree(bp->b_addr); 146 else if (bp->b_flags & _XBF_KMEM) 147 kfree(bp->b_addr); 148 else if (bp->b_addr) 149 folio_put(virt_to_folio(bp->b_addr)); 150 151 call_rcu(&bp->b_rcu, xfs_buf_free_callback); 152 } 153 154 static int 155 xfs_buf_alloc_folio( 156 struct xfs_buf *bp, 157 size_t size, 158 gfp_t gfp_mask) 159 { 160 struct folio *folio; 161 162 folio = folio_alloc(gfp_mask, get_order(size)); 163 if (!folio) 164 return -ENOMEM; 165 bp->b_addr = folio_address(folio); 166 trace_xfs_buf_backing_folio(bp, _RET_IP_); 167 return 0; 168 } 169 170 static int 171 xfs_buf_alloc_kmem( 172 struct xfs_buf *bp, 173 size_t size, 174 gfp_t gfp_mask) 175 { 176 ASSERT(is_power_of_2(size)); 177 ASSERT(size < PAGE_SIZE); 178 179 bp->b_addr = kmalloc(size, gfp_mask | __GFP_RECLAIMABLE); 180 if (!bp->b_addr) 181 return -ENOMEM; 182 183 /* 184 * Slab guarantees that we get back naturally aligned allocations for 185 * power of two sizes. Keep this check as the canary in the coal mine 186 * if anything changes in slab. 187 */ 188 if (WARN_ON_ONCE(!IS_ALIGNED((unsigned long)bp->b_addr, size))) { 189 kfree(bp->b_addr); 190 bp->b_addr = NULL; 191 return -ENOMEM; 192 } 193 xfs_buf_set_flags(bp, _XBF_KMEM); 194 trace_xfs_buf_backing_kmem(bp, _RET_IP_); 195 return 0; 196 } 197 198 static int 199 xfs_buf_alloc_vmalloc( 200 struct xfs_buf *bp, 201 size_t size, 202 gfp_t gfp_mask) 203 { 204 for (;;) { 205 bp->b_addr = __vmalloc(size, gfp_mask); 206 if (bp->b_addr) 207 break; 208 if (gfp_mask & __GFP_NORETRY) 209 return -ENOMEM; 210 XFS_STATS_INC(bp->b_mount, xb_page_retries); 211 memalloc_retry_wait(gfp_mask); 212 } 213 214 trace_xfs_buf_backing_vmalloc(bp, _RET_IP_); 215 return 0; 216 } 217 218 /* 219 * Allocate backing memory for a buffer. 220 * 221 * For tmpfs-backed buffers used by in-memory btrees this directly maps the 222 * tmpfs page cache folios. 223 * 224 * For real file system buffers there are three different kinds backing memory: 225 * 226 * The first type backs the buffer by a kmalloc allocation. This is done for 227 * less than PAGE_SIZE allocations to avoid wasting memory. 228 * 229 * The second type is a single folio buffer - this may be a high order folio or 230 * just a single page sized folio, but either way they get treated the same way 231 * by the rest of the code - the buffer memory spans a single contiguous memory 232 * region that we don't have to map and unmap to access the data directly. 233 * 234 * The third type of buffer is the vmalloc()d buffer. This provides the buffer 235 * with the required contiguous memory region but backed by discontiguous 236 * physical pages. 237 */ 238 static int 239 xfs_buf_alloc_backing_mem( 240 struct xfs_buf *bp, 241 xfs_buf_flags_t flags) 242 { 243 size_t size = BBTOB(bp->b_length); 244 gfp_t gfp_mask = GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOWARN; 245 246 if (xfs_buftarg_is_mem(bp->b_target)) 247 return xmbuf_map_backing_mem(bp); 248 249 /* Assure zeroed buffer for non-read cases. */ 250 if (!(flags & XBF_READ)) 251 gfp_mask |= __GFP_ZERO; 252 253 if (flags & XBF_READ_AHEAD) 254 gfp_mask |= __GFP_NORETRY; 255 256 /* 257 * Optimistically attempt a single high order folio allocation for 258 * larger than PAGE_SIZE buffers. 259 * 260 * Allocating a high order folio makes the assumption that buffers are a 261 * power-of-2 size, matching the power-of-2 folios sizes available. 262 * 263 * The exception here are user xattr data buffers, which can be arbitrarily 264 * sized up to 64kB plus structure metadata, skip straight to the vmalloc 265 * path for them instead of wasting memory here. 266 */ 267 if (size > PAGE_SIZE) { 268 if (is_power_of_2(size)) { 269 gfp_t folio_gfp = gfp_mask; 270 271 folio_gfp &= ~__GFP_DIRECT_RECLAIM; 272 folio_gfp |= __GFP_NORETRY; 273 if (xfs_buf_alloc_folio(bp, size, folio_gfp) == 0) 274 return 0; 275 trace_xfs_buf_backing_fallback(bp, _RET_IP_); 276 } 277 return xfs_buf_alloc_vmalloc(bp, size, gfp_mask); 278 } 279 280 /* 281 * The slab allocator now guarantees aligned allocations for all power 282 * of two sizes. This covers most smaller XFS buffers, so just use 283 * kmalloc in this case. 284 * 285 * Don't bother with the vmalloc fallback for allocations of page size 286 * or less: vmalloc won't do any better. 287 */ 288 if (!(gfp_mask & __GFP_NORETRY)) 289 gfp_mask |= __GFP_NOFAIL; 290 if (size < PAGE_SIZE && is_power_of_2(size)) 291 return xfs_buf_alloc_kmem(bp, size, gfp_mask); 292 return xfs_buf_alloc_folio(bp, size, gfp_mask); 293 } 294 295 static int 296 xfs_buf_alloc( 297 struct xfs_buftarg *target, 298 struct xfs_buf_map *map, 299 int nmaps, 300 xfs_buf_flags_t flags, 301 struct xfs_buf **bpp) 302 { 303 struct xfs_buf *bp; 304 int error; 305 int i; 306 307 *bpp = NULL; 308 bp = kmem_cache_zalloc(xfs_buf_cache, 309 GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL); 310 311 /* 312 * We don't want certain flags to appear in b_flags unless they are 313 * specifically set by later operations on the buffer. 314 */ 315 flags &= ~(XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD); 316 lockref_init(&bp->b_lockref); 317 sema_init(&bp->b_sema, 1); /* unlocked */ 318 atomic_set(&bp->b_lru_ref, 1); 319 init_completion(&bp->b_iowait); 320 INIT_LIST_HEAD(&bp->b_lru); 321 INIT_LIST_HEAD(&bp->b_list); 322 INIT_LIST_HEAD(&bp->b_li_list); 323 bp->b_target = target; 324 bp->b_mount = target->bt_mount; 325 WRITE_ONCE(bp->b_flags, flags); 326 bp->b_rhash_key = map[0].bm_bn; 327 bp->b_length = 0; 328 bp->b_map_count = nmaps; 329 if (nmaps == 1) 330 bp->b_maps = &bp->__b_map; 331 else 332 bp->b_maps = kzalloc_objs(struct xfs_buf_map, nmaps, 333 GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL); 334 for (i = 0; i < nmaps; i++) { 335 bp->b_maps[i].bm_bn = map[i].bm_bn; 336 bp->b_maps[i].bm_len = map[i].bm_len; 337 bp->b_length += map[i].bm_len; 338 } 339 340 atomic_set(&bp->b_pin_count, 0); 341 init_waitqueue_head(&bp->b_waiters); 342 343 XFS_STATS_INC(bp->b_mount, xb_create); 344 trace_xfs_buf_init(bp, _RET_IP_); 345 346 error = xfs_buf_alloc_backing_mem(bp, flags); 347 if (error) { 348 xfs_buf_free(bp); 349 return error; 350 } 351 352 *bpp = bp; 353 return 0; 354 } 355 356 /* 357 * Finding and Reading Buffers 358 */ 359 static int 360 _xfs_buf_obj_cmp( 361 struct rhashtable_compare_arg *arg, 362 const void *obj) 363 { 364 const struct xfs_buf_map *map = arg->key; 365 const struct xfs_buf *bp = obj; 366 367 /* 368 * The key hashing in the lookup path depends on the key being the 369 * first element of the compare_arg, make sure to assert this. 370 */ 371 BUILD_BUG_ON(offsetof(struct xfs_buf_map, bm_bn) != 0); 372 373 if (bp->b_rhash_key != map->bm_bn) 374 return 1; 375 376 if (unlikely(bp->b_length != map->bm_len)) { 377 /* 378 * found a block number match. If the range doesn't 379 * match, the only way this is allowed is if the buffer 380 * in the cache is stale and the transaction that made 381 * it stale has not yet committed. i.e. we are 382 * reallocating a busy extent. Skip this buffer and 383 * continue searching for an exact match. 384 * 385 * Note: If we're scanning for incore buffers to stale, don't 386 * complain if we find non-stale buffers. 387 */ 388 if (!(map->bm_flags & XBM_LIVESCAN)) 389 ASSERT(bp->b_flags & XBF_STALE); 390 return 1; 391 } 392 return 0; 393 } 394 395 static const struct rhashtable_params xfs_buf_hash_params = { 396 .min_size = 32, /* empty AGs have minimal footprint */ 397 .nelem_hint = 16, 398 .key_len = sizeof(xfs_daddr_t), 399 .key_offset = offsetof(struct xfs_buf, b_rhash_key), 400 .head_offset = offsetof(struct xfs_buf, b_rhash_head), 401 .automatic_shrinking = true, 402 .obj_cmpfn = _xfs_buf_obj_cmp, 403 }; 404 405 static int 406 xfs_buf_map_verify( 407 struct xfs_buftarg *btp, 408 struct xfs_buf_map *map) 409 { 410 /* Check for IOs smaller than the sector size / not sector aligned */ 411 ASSERT(!(BBTOB(map->bm_len) < btp->bt_meta_sectorsize)); 412 ASSERT(!(BBTOB(map->bm_bn) & (xfs_off_t)btp->bt_meta_sectormask)); 413 414 /* 415 * Corrupted block numbers can get through to here, unfortunately, so we 416 * have to check that the buffer falls within the filesystem bounds. 417 */ 418 if (map->bm_bn < 0 || map->bm_bn >= btp->bt_nr_sectors) { 419 xfs_alert(btp->bt_mount, 420 "%s: daddr 0x%llx out of range, EOFS 0x%llx", 421 __func__, map->bm_bn, btp->bt_nr_sectors); 422 WARN_ON(1); 423 return -EFSCORRUPTED; 424 } 425 return 0; 426 } 427 428 static int 429 xfs_buf_find_lock( 430 struct xfs_buf *bp, 431 xfs_buf_flags_t flags) 432 { 433 if (flags & XBF_TRYLOCK) { 434 if (!xfs_buf_trylock(bp)) { 435 XFS_STATS_INC(bp->b_mount, xb_busy_locked); 436 return -EAGAIN; 437 } 438 } else { 439 xfs_buf_lock(bp); 440 XFS_STATS_INC(bp->b_mount, xb_get_locked_waited); 441 } 442 443 /* 444 * if the buffer is stale, clear all the external state associated with 445 * it. We need to keep flags such as how we allocated the buffer memory 446 * intact here. 447 */ 448 if (bp->b_flags & XBF_STALE) { 449 if (flags & XBF_LIVESCAN) { 450 xfs_buf_unlock(bp); 451 return -ENOENT; 452 } 453 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0); 454 xfs_buf_clear_flags(bp, ~_XBF_KMEM); 455 bp->b_ops = NULL; 456 } 457 return 0; 458 } 459 460 static inline struct xfs_buf * 461 xfs_buf_lookup( 462 struct xfs_buftarg *btp, 463 struct xfs_buf_map *map) 464 { 465 struct xfs_buf *bp; 466 467 rcu_read_lock(); 468 bp = rhashtable_lookup(&btp->bt_hash, map, xfs_buf_hash_params); 469 if (!bp || !lockref_get_not_dead(&bp->b_lockref)) { 470 rcu_read_unlock(); 471 XFS_STATS_INC(btp->bt_mount, xb_miss_locked); 472 return NULL; 473 } 474 rcu_read_unlock(); 475 476 trace_xfs_buf_find(bp, _RET_IP_); 477 XFS_STATS_INC(btp->bt_mount, xb_get_locked); 478 return bp; 479 } 480 481 /* 482 * Insert the new_bp into the hash table. This consumes the perag reference 483 * taken for the lookup regardless of the result of the insert. 484 */ 485 static int 486 xfs_buf_find_insert( 487 struct xfs_buftarg *btp, 488 struct xfs_buf_map *cmap, 489 struct xfs_buf_map *map, 490 int nmaps, 491 xfs_buf_flags_t flags, 492 struct xfs_buf **bpp) 493 { 494 struct xfs_buf *new_bp; 495 struct xfs_buf *bp; 496 int error; 497 498 error = xfs_buf_alloc(btp, map, nmaps, flags, &new_bp); 499 if (error) 500 return error; 501 502 /* The new buffer keeps the perag reference until it is freed. */ 503 if (!xfs_buftarg_is_mem(btp)) { 504 new_bp->b_pag = xfs_perag_get(btp->bt_mount, 505 xfs_daddr_to_agno(btp->bt_mount, cmap->bm_bn)); 506 } 507 508 retry: 509 rcu_read_lock(); 510 bp = rhashtable_lookup_get_insert_fast(&btp->bt_hash, 511 &new_bp->b_rhash_head, xfs_buf_hash_params); 512 if (IS_ERR(bp)) { 513 rcu_read_unlock(); 514 error = PTR_ERR(bp); 515 goto out_free_buf; 516 } 517 if (bp) { 518 /* 519 * If there is an existing buffer with a dead lockref, retry 520 * until the new buffer is added, or a usable buffer is found. 521 */ 522 if (!lockref_get_not_dead(&bp->b_lockref)) { 523 rcu_read_unlock(); 524 cpu_relax(); 525 goto retry; 526 } 527 rcu_read_unlock(); 528 *bpp = bp; 529 goto out_free_buf; 530 } 531 rcu_read_unlock(); 532 533 *bpp = new_bp; 534 return 0; 535 536 out_free_buf: 537 if (new_bp->b_pag) 538 xfs_perag_put(new_bp->b_pag); 539 xfs_buf_free(new_bp); 540 return error; 541 } 542 543 /* 544 * Assembles a buffer covering the specified range. The code is optimised for 545 * cache hits, as metadata intensive workloads will see 3 orders of magnitude 546 * more hits than misses. 547 */ 548 static int 549 xfs_find_get_buf( 550 struct xfs_buftarg *btp, 551 struct xfs_buf_map *map, 552 int nmaps, 553 xfs_buf_flags_t flags, 554 struct xfs_buf **bpp) 555 { 556 struct xfs_buf *bp = NULL; 557 struct xfs_buf_map cmap = { .bm_bn = map[0].bm_bn }; 558 int error; 559 int i; 560 561 if (flags & XBF_LIVESCAN) 562 cmap.bm_flags |= XBM_LIVESCAN; 563 for (i = 0; i < nmaps; i++) 564 cmap.bm_len += map[i].bm_len; 565 566 error = xfs_buf_map_verify(btp, &cmap); 567 if (error) 568 return error; 569 570 /* cache hits always outnumber misses by at least 10:1 */ 571 bp = xfs_buf_lookup(btp, &cmap); 572 if (unlikely(!bp)) { 573 if (flags & XBF_INCORE) 574 return -ENOENT; 575 error = xfs_buf_find_insert(btp, &cmap, map, nmaps, flags, &bp); 576 if (error) 577 return error; 578 } 579 580 *bpp = bp; 581 return 0; 582 } 583 584 int 585 xfs_buf_get_map( 586 struct xfs_buftarg *btp, 587 struct xfs_buf_map *map, 588 int nmaps, 589 xfs_buf_flags_t flags, 590 struct xfs_buf **bpp) 591 { 592 int error; 593 594 ASSERT(!(flags & ~(XBF_TRYLOCK | XBF_INCORE | XBF_LIVESCAN))); 595 ASSERT(!(flags & XBF_LIVESCAN) || (flags & XBF_INCORE)); 596 597 /* 598 * Zero the buffer and clear b_error as xfs_buf_get_map callers don't 599 * expect valid data to be found in the buffer. 600 */ 601 error = xfs_find_get_buf(btp, map, nmaps, flags, bpp); 602 if (error) 603 return error; 604 605 error = xfs_buf_find_lock(*bpp, flags); 606 if (error) { 607 xfs_buf_rele(*bpp); 608 return error; 609 } 610 XFS_STATS_INC(btp->bt_mount, xb_get); 611 trace_xfs_buf_get(*bpp, flags, _RET_IP_); 612 xfs_buf_ioerror(*bpp, 0); 613 return 0; 614 } 615 616 int 617 _xfs_buf_read( 618 struct xfs_buf *bp) 619 { 620 ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL); 621 622 xfs_buf_clear_flags(bp, XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD | 623 XBF_DONE); 624 xfs_buf_set_flags(bp, XBF_READ); 625 xfs_buf_submit(bp); 626 return xfs_buf_iowait(bp); 627 } 628 629 int 630 xfs_buf_read_map( 631 struct xfs_buftarg *target, 632 struct xfs_buf_map *map, 633 int nmaps, 634 xfs_buf_flags_t flags, 635 struct xfs_buf **bpp, 636 const struct xfs_buf_ops *ops, 637 xfs_failaddr_t fa) 638 { 639 struct xfs_buf *bp; 640 int error; 641 642 ASSERT(!(flags & ~XBF_TRYLOCK)); 643 644 flags |= XBF_READ; 645 *bpp = NULL; 646 647 error = xfs_find_get_buf(target, map, nmaps, flags, &bp); 648 if (error) 649 return error; 650 error = xfs_buf_find_lock(bp, flags); 651 if (error) { 652 xfs_buf_rele(bp); 653 return error; 654 } 655 656 trace_xfs_buf_read(bp, flags, _RET_IP_); 657 658 if (bp->b_flags & XBF_DONE) { 659 ASSERT(bp->b_error == 0); 660 661 /* 662 * If the caller passed an ops structure and the buffer doesn't 663 * have ops assigned yet, set the ops and use them to verify the 664 * buffer contents. 665 * 666 * Under normal operations, every in-core buffer is verified on 667 * read I/O completion, but there are two scenarios that can 668 * lead to in-core buffers without an assigned ->b_ops: 669 * 670 * 1) During log recovery of buffers on a V4 filesystem. 671 * These buffers are purged at the end of recovery, though. 672 * 2) Oonline repair intentionally reads with a NULL buffer 673 * ops to run several verifiers across an in-core buffer in 674 * order to establish buffer type. If repair can't 675 * establish that, the buffer will be left in memory with 676 * NULL buffer ops. 677 */ 678 if (ops && !bp->b_ops) { 679 bp->b_ops = ops; 680 bp->b_ops->verify_read(bp); 681 /* 682 * If verification failed, clear XBF_DONE as we assume 683 * that buffers have no recorded errors when in XBF_DONE 684 * state. 685 */ 686 error = bp->b_error; 687 if (error) 688 xfs_buf_clear_flags(bp, XBF_DONE); 689 } 690 691 /* We do not want read in the flags */ 692 xfs_buf_clear_flags(bp, XBF_READ); 693 } else { 694 /* Initiate the buffer read and wait. */ 695 XFS_STATS_INC(target->bt_mount, xb_get_read); 696 bp->b_ops = ops; 697 error = _xfs_buf_read(bp); 698 } 699 700 if (error) 701 goto out_ioerror; 702 703 *bpp = bp; 704 return 0; 705 706 out_ioerror: 707 /* 708 * Check against log shutdown for error reporting because metadata 709 * writeback may require a read first and we need to report errors in 710 * metadata writeback until the log is shut down. High level 711 * transaction read functions already check against mount shutdown, so 712 * we only need to be concerned about low level/ IO interactions here. 713 */ 714 if (!xlog_is_shutdown(target->bt_mount->m_log)) 715 xfs_buf_ioerror_alert(bp, fa); 716 717 /* 718 * If we've had a read error, then the contents of the buffer are 719 * invalid and should not be used. To ensure that a followup read tries 720 * to pull the buffer from disk again, we clear the XBF_DONE flag and 721 * mark the buffer stale. This ensures that anyone who has a current 722 * reference to the buffer will interpret it's contents correctly and 723 * future cache lookups will also treat it as an empty, uninitialised 724 * buffer. 725 */ 726 xfs_buf_clear_flags(bp, XBF_DONE); 727 xfs_buf_stale(bp); 728 xfs_buf_relse(bp); 729 730 /* bad CRC means corrupted metadata */ 731 if (error == -EFSBADCRC) 732 return -EFSCORRUPTED; 733 return error; 734 } 735 736 /* 737 * If we are not low on memory then do the readahead in a deadlock 738 * safe manner. 739 */ 740 void 741 xfs_buf_readahead_map( 742 struct xfs_buftarg *target, 743 struct xfs_buf_map *map, 744 int nmaps, 745 const struct xfs_buf_ops *ops) 746 { 747 const xfs_buf_flags_t flags = XBF_READ | XBF_ASYNC | XBF_READ_AHEAD; 748 struct xfs_buf *bp; 749 750 /* 751 * Currently we don't have a good means or justification for performing 752 * xmbuf_map_page asynchronously, so we don't do readahead. 753 */ 754 if (xfs_buftarg_is_mem(target)) 755 return; 756 757 if (xfs_find_get_buf(target, map, nmaps, flags, &bp)) 758 return; 759 760 /* 761 * Do a lockless fast path check for a valid uptodate buffer and avoid 762 * locking entirely in this case. 763 */ 764 if ((READ_ONCE(bp->b_flags) & (XBF_DONE | XBF_STALE)) == XBF_DONE) 765 goto out_rele; 766 767 /* Otherwise lock the buffer to stabilize the state */ 768 if (!xfs_buf_trylock(bp)) 769 goto out_rele; 770 771 /* Let the actual reader deal with stale buffers. */ 772 if (bp->b_flags & (XBF_STALE | XBF_DONE)) 773 goto out_unlock; 774 775 trace_xfs_buf_readahead(bp, 0, _RET_IP_); 776 XFS_STATS_INC(target->bt_mount, xb_get_read); 777 bp->b_ops = ops; 778 xfs_buf_clear_flags(bp, XBF_WRITE | XBF_DONE); 779 xfs_buf_set_flags(bp, flags); 780 percpu_counter_inc(&target->bt_readahead_count); 781 xfs_buf_submit(bp); 782 return; 783 out_unlock: 784 xfs_buf_unlock(bp); 785 out_rele: 786 xfs_buf_rele(bp); 787 } 788 789 /* 790 * Read an uncached buffer from disk. Allocates and returns a locked 791 * buffer containing the disk contents or nothing. Uncached buffers always have 792 * a cache index of XFS_BUF_DADDR_NULL so we can easily determine if the buffer 793 * is cached or uncached during fault diagnosis. 794 */ 795 int 796 xfs_buf_read_uncached( 797 struct xfs_buftarg *target, 798 xfs_daddr_t daddr, 799 size_t numblks, 800 struct xfs_buf **bpp, 801 const struct xfs_buf_ops *ops) 802 { 803 struct xfs_buf *bp; 804 int error; 805 806 *bpp = NULL; 807 808 error = xfs_buf_get_uncached(target, numblks, &bp); 809 if (error) 810 return error; 811 812 /* set up the buffer for a read IO */ 813 ASSERT(bp->b_map_count == 1); 814 bp->b_rhash_key = XFS_BUF_DADDR_NULL; 815 bp->b_maps[0].bm_bn = daddr; 816 xfs_buf_set_flags(bp, XBF_READ); 817 bp->b_ops = ops; 818 819 xfs_buf_submit(bp); 820 error = xfs_buf_iowait(bp); 821 if (error) { 822 xfs_buf_relse(bp); 823 return error; 824 } 825 826 *bpp = bp; 827 return 0; 828 } 829 830 int 831 xfs_buf_get_uncached( 832 struct xfs_buftarg *target, 833 size_t numblks, 834 struct xfs_buf **bpp) 835 { 836 int error; 837 DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks); 838 839 error = xfs_buf_alloc(target, &map, 1, 0, bpp); 840 if (error) 841 return error; 842 xfs_buf_lock(*bpp); 843 trace_xfs_buf_get_uncached(*bpp, _RET_IP_); 844 return 0; 845 } 846 847 /* 848 * Increment reference count on buffer, to hold the buffer concurrently 849 * with another thread which may release (free) the buffer asynchronously. 850 * Must hold the buffer already to call this function. 851 */ 852 void 853 xfs_buf_hold( 854 struct xfs_buf *bp) 855 { 856 trace_xfs_buf_hold(bp, _RET_IP_); 857 858 lockref_get(&bp->b_lockref); 859 } 860 861 static void 862 xfs_buf_destroy( 863 struct xfs_buf *bp) 864 { 865 ASSERT(lockref_is_dead(&bp->b_lockref)); 866 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q)); 867 868 if (bp->b_pag) 869 xfs_perag_put(bp->b_pag); 870 xfs_buf_free(bp); 871 } 872 873 static inline void 874 xfs_buf_kill( 875 struct xfs_buf *bp) 876 { 877 lockref_mark_dead(&bp->b_lockref); 878 if (!xfs_buf_is_uncached(bp)) { 879 rhashtable_remove_fast(&bp->b_target->bt_hash, 880 &bp->b_rhash_head, xfs_buf_hash_params); 881 } 882 } 883 884 /* 885 * Release a hold on the specified buffer. 886 */ 887 void 888 xfs_buf_rele( 889 struct xfs_buf *bp) 890 { 891 trace_xfs_buf_rele(bp, _RET_IP_); 892 893 if (lockref_put_or_lock(&bp->b_lockref)) 894 return; 895 if (!--bp->b_lockref.count) { 896 if (xfs_buf_is_uncached(bp) || !atomic_read(&bp->b_lru_ref)) 897 goto kill; 898 list_lru_add_obj(&bp->b_target->bt_lru, &bp->b_lru); 899 } 900 spin_unlock(&bp->b_lockref.lock); 901 return; 902 903 kill: 904 xfs_buf_kill(bp); 905 list_lru_del_obj(&bp->b_target->bt_lru, &bp->b_lru); 906 spin_unlock(&bp->b_lockref.lock); 907 908 xfs_buf_destroy(bp); 909 } 910 911 /* 912 * Lock a buffer object, if it is not already locked. 913 * 914 * If we come across a stale, pinned, locked buffer, we know that we are 915 * being asked to lock a buffer that has been reallocated. Because it is 916 * pinned, we know that the log has not been pushed to disk and hence it 917 * will still be locked. Rather than continuing to have trylock attempts 918 * fail until someone else pushes the log, push it ourselves before 919 * returning. This means that the xfsaild will not get stuck trying 920 * to push on stale inode buffers. 921 */ 922 int 923 xfs_buf_trylock( 924 struct xfs_buf *bp) 925 { 926 int locked; 927 928 locked = down_trylock(&bp->b_sema) == 0; 929 if (locked) 930 trace_xfs_buf_trylock(bp, _RET_IP_); 931 else 932 trace_xfs_buf_trylock_fail(bp, _RET_IP_); 933 return locked; 934 } 935 936 /* 937 * Lock a buffer object. 938 * 939 * If we come across a stale, pinned, locked buffer, we know that we 940 * are being asked to lock a buffer that has been reallocated. Because 941 * it is pinned, we know that the log has not been pushed to disk and 942 * hence it will still be locked. Rather than sleeping until someone 943 * else pushes the log, push it ourselves before trying to get the lock. 944 */ 945 void 946 xfs_buf_lock( 947 struct xfs_buf *bp) 948 { 949 trace_xfs_buf_lock(bp, _RET_IP_); 950 951 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE)) 952 xfs_log_force(bp->b_mount, 0); 953 down(&bp->b_sema); 954 955 trace_xfs_buf_lock_done(bp, _RET_IP_); 956 } 957 958 void 959 xfs_buf_unlock( 960 struct xfs_buf *bp) 961 { 962 ASSERT(xfs_buf_islocked(bp)); 963 964 up(&bp->b_sema); 965 trace_xfs_buf_unlock(bp, _RET_IP_); 966 } 967 968 STATIC void 969 xfs_buf_wait_unpin( 970 struct xfs_buf *bp) 971 { 972 DECLARE_WAITQUEUE (wait, current); 973 974 if (atomic_read(&bp->b_pin_count) == 0) 975 return; 976 977 add_wait_queue(&bp->b_waiters, &wait); 978 for (;;) { 979 set_current_state(TASK_UNINTERRUPTIBLE); 980 if (atomic_read(&bp->b_pin_count) == 0) 981 break; 982 io_schedule(); 983 } 984 remove_wait_queue(&bp->b_waiters, &wait); 985 set_current_state(TASK_RUNNING); 986 } 987 988 static void 989 xfs_buf_ioerror_alert_ratelimited( 990 struct xfs_buf *bp) 991 { 992 static unsigned long lasttime; 993 static struct xfs_buftarg *lasttarg; 994 995 if (bp->b_target != lasttarg || 996 time_after(jiffies, (lasttime + 5*HZ))) { 997 lasttime = jiffies; 998 xfs_buf_ioerror_alert(bp, __this_address); 999 } 1000 lasttarg = bp->b_target; 1001 } 1002 1003 /* 1004 * Account for this latest trip around the retry handler, and decide if 1005 * we've failed enough times to constitute a permanent failure. 1006 */ 1007 static bool 1008 xfs_buf_ioerror_permanent( 1009 struct xfs_buf *bp, 1010 struct xfs_error_cfg *cfg) 1011 { 1012 struct xfs_mount *mp = bp->b_mount; 1013 1014 if (cfg->max_retries != XFS_ERR_RETRY_FOREVER && 1015 ++bp->b_retries > cfg->max_retries) 1016 return true; 1017 if (cfg->retry_timeout != XFS_ERR_RETRY_FOREVER && 1018 time_after(jiffies, cfg->retry_timeout + bp->b_first_retry_time)) 1019 return true; 1020 1021 /* At unmount we may treat errors differently */ 1022 if (xfs_is_unmounting(mp) && mp->m_fail_unmount) 1023 return true; 1024 1025 return false; 1026 } 1027 1028 /* 1029 * On a sync write or shutdown we just want to stale the buffer and let the 1030 * caller handle the error in bp->b_error appropriately. 1031 * 1032 * If the write was asynchronous then no one will be looking for the error. If 1033 * this is the first failure of this type, clear the error state and write the 1034 * buffer out again. This means we always retry an async write failure at least 1035 * once, but we also need to set the buffer up to behave correctly now for 1036 * repeated failures. 1037 * 1038 * If we get repeated async write failures, then we take action according to the 1039 * error configuration we have been set up to use. 1040 * 1041 * Returns true if this function took care of error handling and the caller must 1042 * not touch the buffer again. Return false if the caller should proceed with 1043 * normal I/O completion handling. 1044 */ 1045 static bool 1046 xfs_buf_ioend_handle_error( 1047 struct xfs_buf *bp) 1048 { 1049 struct xfs_mount *mp = bp->b_mount; 1050 struct xfs_error_cfg *cfg; 1051 struct xfs_log_item *lip; 1052 1053 /* 1054 * If we've already shutdown the journal because of I/O errors, there's 1055 * no point in giving this a retry. 1056 */ 1057 if (xlog_is_shutdown(mp->m_log)) 1058 goto out_stale; 1059 1060 xfs_buf_ioerror_alert_ratelimited(bp); 1061 1062 /* 1063 * We're not going to bother about retrying this during recovery. 1064 * One strike! 1065 */ 1066 if (mp->m_log && xlog_in_recovery(mp->m_log)) { 1067 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR); 1068 return false; 1069 } 1070 1071 /* 1072 * Synchronous writes will have callers process the error. 1073 */ 1074 if (!(bp->b_flags & XBF_ASYNC)) 1075 goto out_stale; 1076 1077 trace_xfs_buf_iodone_async(bp, _RET_IP_); 1078 1079 cfg = xfs_error_get_cfg(mp, XFS_ERR_METADATA, bp->b_error); 1080 if (bp->b_last_error != bp->b_error || 1081 !(bp->b_flags & (XBF_STALE | XBF_WRITE_FAIL))) { 1082 bp->b_last_error = bp->b_error; 1083 if (cfg->retry_timeout != XFS_ERR_RETRY_FOREVER && 1084 !bp->b_first_retry_time) 1085 bp->b_first_retry_time = jiffies; 1086 goto resubmit; 1087 } 1088 1089 /* 1090 * Permanent error - we need to trigger a shutdown if we haven't already 1091 * to indicate that inconsistency will result from this action. 1092 */ 1093 if (xfs_buf_ioerror_permanent(bp, cfg)) { 1094 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR); 1095 goto out_stale; 1096 } 1097 1098 /* Still considered a transient error. Caller will schedule retries. */ 1099 list_for_each_entry(lip, &bp->b_li_list, li_bio_list) { 1100 set_bit(XFS_LI_FAILED, &lip->li_flags); 1101 clear_bit(XFS_LI_FLUSHING, &lip->li_flags); 1102 } 1103 1104 xfs_buf_ioerror(bp, 0); 1105 xfs_buf_relse(bp); 1106 return true; 1107 1108 resubmit: 1109 xfs_buf_ioerror(bp, 0); 1110 xfs_buf_set_flags(bp, XBF_DONE | XBF_WRITE_FAIL); 1111 reinit_completion(&bp->b_iowait); 1112 xfs_buf_submit(bp); 1113 return true; 1114 out_stale: 1115 xfs_buf_stale(bp); 1116 xfs_buf_set_flags(bp, XBF_DONE); 1117 xfs_buf_clear_flags(bp, XBF_WRITE); 1118 trace_xfs_buf_error_relse(bp, _RET_IP_); 1119 return false; 1120 } 1121 1122 /* 1123 * Complete a buffer read or write. 1124 * 1125 * Releases the buffer if the I/O was asynchronous. 1126 */ 1127 static void 1128 xfs_buf_ioend( 1129 struct xfs_buf *bp) 1130 { 1131 bool async = bp->b_flags & XBF_ASYNC; 1132 1133 trace_xfs_buf_iodone(bp, _RET_IP_); 1134 1135 if (bp->b_flags & XBF_READ) { 1136 if (!bp->b_error && is_vmalloc_addr(bp->b_addr)) 1137 invalidate_kernel_vmap_range(bp->b_addr, 1138 roundup(BBTOB(bp->b_length), PAGE_SIZE)); 1139 if (!bp->b_error && bp->b_ops) 1140 bp->b_ops->verify_read(bp); 1141 if (!bp->b_error) 1142 xfs_buf_set_flags(bp, XBF_DONE); 1143 if (bp->b_flags & XBF_READ_AHEAD) 1144 percpu_counter_dec(&bp->b_target->bt_readahead_count); 1145 } else { 1146 if (unlikely(bp->b_error)) { 1147 if (xfs_buf_ioend_handle_error(bp)) { 1148 ASSERT(async); 1149 return; 1150 } 1151 } else { 1152 xfs_buf_clear_flags(bp, XBF_WRITE_FAIL); 1153 xfs_buf_set_flags(bp, XBF_DONE); 1154 } 1155 1156 /* clear the retry state */ 1157 bp->b_last_error = 0; 1158 bp->b_retries = 0; 1159 bp->b_first_retry_time = 0; 1160 1161 /* 1162 * Note that for things like remote attribute buffers, there may 1163 * not be a buffer log item here, so processing the buffer log 1164 * item must remain optional. 1165 */ 1166 if (bp->b_log_item) 1167 xfs_buf_item_done(bp); 1168 1169 if (bp->b_iodone) 1170 bp->b_iodone(bp); 1171 } 1172 1173 xfs_buf_clear_flags(bp, XBF_READ | XBF_WRITE | XBF_READ_AHEAD); 1174 if (async) 1175 xfs_buf_relse(bp); 1176 } 1177 1178 static void 1179 xfs_buf_ioend_work( 1180 struct work_struct *work) 1181 { 1182 xfs_buf_ioend(container_of(work, struct xfs_buf, b_ioend_work)); 1183 } 1184 1185 void 1186 __xfs_buf_ioerror( 1187 struct xfs_buf *bp, 1188 int error, 1189 xfs_failaddr_t failaddr) 1190 { 1191 ASSERT(error <= 0 && error >= -1000); 1192 bp->b_error = error; 1193 trace_xfs_buf_ioerror(bp, error, failaddr); 1194 } 1195 1196 void 1197 xfs_buf_ioerror_alert( 1198 struct xfs_buf *bp, 1199 xfs_failaddr_t func) 1200 { 1201 xfs_buf_alert_ratelimited(bp, "XFS: metadata IO error", 1202 "metadata I/O error in \"%pS\" at daddr 0x%llx len %d error %d", 1203 func, (uint64_t)xfs_buf_daddr(bp), 1204 bp->b_length, -bp->b_error); 1205 } 1206 1207 /* 1208 * Fail a locked and referenced buffer outside the I/O path. 1209 * 1210 * The caller transfers a reference which will be released after processing the 1211 * error. 1212 */ 1213 void 1214 xfs_buf_fail( 1215 struct xfs_buf *bp) 1216 { 1217 ASSERT(xfs_buf_islocked(bp)); 1218 1219 xfs_buf_set_flags(bp, XBF_ASYNC); 1220 xfs_buf_clear_flags(bp, XBF_DONE); 1221 xfs_buf_stale(bp); 1222 xfs_buf_ioerror(bp, -EIO); 1223 xfs_buf_ioend(bp); 1224 } 1225 1226 int 1227 xfs_bwrite( 1228 struct xfs_buf *bp) 1229 { 1230 int error; 1231 1232 ASSERT(xfs_buf_islocked(bp)); 1233 1234 xfs_buf_set_flags(bp, XBF_WRITE); 1235 xfs_buf_clear_flags(bp, XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q | 1236 XBF_DONE); 1237 1238 xfs_buf_submit(bp); 1239 error = xfs_buf_iowait(bp); 1240 if (error) 1241 xfs_force_shutdown(bp->b_mount, SHUTDOWN_META_IO_ERROR); 1242 return error; 1243 } 1244 1245 static void 1246 xfs_buf_bio_end_io( 1247 struct bio *bio) 1248 { 1249 struct xfs_buf *bp = bio->bi_private; 1250 1251 if (bio->bi_status) 1252 xfs_buf_ioerror(bp, blk_status_to_errno(bio->bi_status)); 1253 else if ((bp->b_flags & XBF_WRITE) && (bp->b_flags & XBF_ASYNC) && 1254 XFS_TEST_ERROR(bp->b_mount, XFS_ERRTAG_BUF_IOERROR)) 1255 xfs_buf_ioerror(bp, -EIO); 1256 1257 if (bp->b_flags & XBF_ASYNC) { 1258 INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work); 1259 queue_work(bp->b_mount->m_buf_workqueue, &bp->b_ioend_work); 1260 } else { 1261 complete(&bp->b_iowait); 1262 } 1263 1264 bio_put(bio); 1265 } 1266 1267 static inline blk_opf_t 1268 xfs_buf_bio_op( 1269 struct xfs_buf *bp) 1270 { 1271 blk_opf_t op; 1272 1273 if (bp->b_flags & XBF_WRITE) { 1274 op = REQ_OP_WRITE; 1275 } else { 1276 op = REQ_OP_READ; 1277 if (bp->b_flags & XBF_READ_AHEAD) 1278 op |= REQ_RAHEAD; 1279 } 1280 1281 return op | REQ_META; 1282 } 1283 1284 static void 1285 xfs_buf_submit_bio( 1286 struct xfs_buf *bp) 1287 { 1288 unsigned int len = BBTOB(bp->b_length); 1289 unsigned int nr_vecs = bio_add_max_vecs(bp->b_addr, len); 1290 unsigned int map = 0; 1291 struct blk_plug plug; 1292 struct bio *bio; 1293 1294 bio = bio_alloc(bp->b_target->bt_bdev, nr_vecs, xfs_buf_bio_op(bp), 1295 GFP_NOIO); 1296 if (is_vmalloc_addr(bp->b_addr)) 1297 bio_add_vmalloc(bio, bp->b_addr, len); 1298 else 1299 bio_add_virt_nofail(bio, bp->b_addr, len); 1300 bio->bi_private = bp; 1301 bio->bi_end_io = xfs_buf_bio_end_io; 1302 1303 /* 1304 * If there is more than one map segment, split out a new bio for each 1305 * map except of the last one. The last map is handled by the 1306 * remainder of the original bio outside the loop. 1307 */ 1308 blk_start_plug(&plug); 1309 for (map = 0; map < bp->b_map_count - 1; map++) { 1310 struct bio *split; 1311 1312 split = bio_split(bio, bp->b_maps[map].bm_len, GFP_NOFS, 1313 &fs_bio_set); 1314 split->bi_iter.bi_sector = bp->b_maps[map].bm_bn; 1315 bio_chain(split, bio); 1316 submit_bio(split); 1317 } 1318 bio->bi_iter.bi_sector = bp->b_maps[map].bm_bn; 1319 submit_bio(bio); 1320 blk_finish_plug(&plug); 1321 } 1322 1323 /* 1324 * Wait for I/O completion of a sync buffer and return the I/O error code. 1325 */ 1326 static int 1327 xfs_buf_iowait( 1328 struct xfs_buf *bp) 1329 { 1330 ASSERT(!(bp->b_flags & XBF_ASYNC)); 1331 1332 trace_xfs_buf_iowait(bp, _RET_IP_); 1333 wait_for_completion(&bp->b_iowait); 1334 trace_xfs_buf_iowait_done(bp, _RET_IP_); 1335 1336 xfs_buf_ioend(bp); 1337 return bp->b_error; 1338 } 1339 1340 /* 1341 * Run the write verifier callback function if it exists. If this fails, mark 1342 * the buffer with an error and do not dispatch the I/O. 1343 */ 1344 static bool 1345 xfs_buf_verify_write( 1346 struct xfs_buf *bp) 1347 { 1348 if (bp->b_ops) { 1349 bp->b_ops->verify_write(bp); 1350 if (bp->b_error) 1351 return false; 1352 } else if (bp->b_rhash_key != XFS_BUF_DADDR_NULL) { 1353 /* 1354 * Non-crc filesystems don't attach verifiers during log 1355 * recovery, so don't warn for such filesystems. 1356 */ 1357 if (xfs_has_crc(bp->b_mount)) { 1358 xfs_warn(bp->b_mount, 1359 "%s: no buf ops on daddr 0x%llx len %d", 1360 __func__, xfs_buf_daddr(bp), 1361 bp->b_length); 1362 xfs_hex_dump(bp->b_addr, XFS_CORRUPTION_DUMP_LEN); 1363 dump_stack(); 1364 } 1365 } 1366 1367 return true; 1368 } 1369 1370 /* 1371 * Buffer I/O submission path, read or write. Asynchronous submission transfers 1372 * the buffer lock ownership and the current reference to the IO. It is not 1373 * safe to reference the buffer after a call to this function unless the caller 1374 * holds an additional reference itself. 1375 */ 1376 static void 1377 xfs_buf_submit( 1378 struct xfs_buf *bp) 1379 { 1380 trace_xfs_buf_submit(bp, _RET_IP_); 1381 1382 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q)); 1383 1384 /* 1385 * On log shutdown we stale and complete the buffer immediately. We can 1386 * be called to read the superblock before the log has been set up, so 1387 * be careful checking the log state. 1388 * 1389 * Checking the mount shutdown state here can result in the log tail 1390 * moving inappropriately on disk as the log may not yet be shut down. 1391 * i.e. failing this buffer on mount shutdown can remove it from the AIL 1392 * and move the tail of the log forwards without having written this 1393 * buffer to disk. This corrupts the log tail state in memory, and 1394 * because the log may not be shut down yet, it can then be propagated 1395 * to disk before the log is shutdown. Hence we check log shutdown 1396 * state here rather than mount state to avoid corrupting the log tail 1397 * on shutdown. 1398 */ 1399 if (bp->b_mount->m_log && xlog_is_shutdown(bp->b_mount->m_log)) { 1400 xfs_buf_ioerror(bp, -EIO); 1401 goto ioerror; 1402 } 1403 1404 if (bp->b_flags & XBF_WRITE) 1405 xfs_buf_wait_unpin(bp); 1406 1407 /* 1408 * Make sure we capture only current IO errors rather than stale errors 1409 * left over from previous use of the buffer (e.g. failed readahead). 1410 */ 1411 bp->b_error = 0; 1412 1413 if ((bp->b_flags & XBF_WRITE) && !xfs_buf_verify_write(bp)) { 1414 /* ->verify_write should have set b_error already */ 1415 xfs_force_shutdown(bp->b_mount, SHUTDOWN_CORRUPT_INCORE); 1416 goto ioerror; 1417 } 1418 1419 /* In-memory targets are directly mapped, no I/O required. */ 1420 if (xfs_buftarg_is_mem(bp->b_target)) 1421 goto end_io; 1422 1423 xfs_buf_submit_bio(bp); 1424 return; 1425 1426 ioerror: 1427 xfs_buf_clear_flags(bp, XBF_DONE); 1428 xfs_buf_stale(bp); 1429 end_io: 1430 if (bp->b_flags & XBF_ASYNC) 1431 xfs_buf_ioend(bp); 1432 else 1433 complete(&bp->b_iowait); 1434 } 1435 1436 /* 1437 * Log a message about and stale a buffer that a caller has decided is corrupt. 1438 * 1439 * This function should be called for the kinds of metadata corruption that 1440 * cannot be detect from a verifier, such as incorrect inter-block relationship 1441 * data. Do /not/ call this function from a verifier function. 1442 * 1443 * The buffer must be XBF_DONE prior to the call. Afterwards, the buffer will 1444 * be marked stale, but b_error will not be set. The caller is responsible for 1445 * releasing the buffer or fixing it. 1446 */ 1447 void 1448 __xfs_buf_mark_corrupt( 1449 struct xfs_buf *bp, 1450 xfs_failaddr_t fa) 1451 { 1452 ASSERT(bp->b_flags & XBF_DONE); 1453 1454 xfs_buf_corruption_error(bp, fa); 1455 xfs_buf_stale(bp); 1456 } 1457 1458 /* 1459 * Handling of buffer targets (buftargs). 1460 */ 1461 1462 /* 1463 * Wait for any bufs with callbacks that have been submitted but have not yet 1464 * returned. These buffers will have an elevated hold count, so wait on those 1465 * while freeing all the buffers only held by the LRU. 1466 */ 1467 static enum lru_status 1468 xfs_buftarg_drain_rele( 1469 struct list_head *item, 1470 struct list_lru_one *lru, 1471 void *arg) 1472 1473 { 1474 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru); 1475 struct list_head *dispose = arg; 1476 1477 if (!spin_trylock(&bp->b_lockref.lock)) 1478 return LRU_SKIP; 1479 if (bp->b_lockref.count > 0) { 1480 /* need to wait, so skip it this pass */ 1481 spin_unlock(&bp->b_lockref.lock); 1482 trace_xfs_buf_drain_buftarg(bp, _RET_IP_); 1483 return LRU_SKIP; 1484 } 1485 1486 xfs_buf_kill(bp); 1487 list_lru_isolate_move(lru, item, dispose); 1488 spin_unlock(&bp->b_lockref.lock); 1489 return LRU_REMOVED; 1490 } 1491 1492 /* 1493 * Wait for outstanding I/O on the buftarg to complete. 1494 */ 1495 void 1496 xfs_buftarg_wait( 1497 struct xfs_buftarg *btp) 1498 { 1499 /* 1500 * First wait for all in-flight readahead buffers to be released. This is 1501 * critical as new buffers do not make the LRU until they are released. 1502 * 1503 * Next, flush the buffer workqueue to ensure all completion processing 1504 * has finished. Just waiting on buffer locks is not sufficient for 1505 * async IO as the reference count held over IO is not released until 1506 * after the buffer lock is dropped. Hence we need to ensure here that 1507 * all reference counts have been dropped before we start walking the 1508 * LRU list. 1509 */ 1510 while (percpu_counter_sum(&btp->bt_readahead_count)) 1511 delay(100); 1512 flush_workqueue(btp->bt_mount->m_buf_workqueue); 1513 } 1514 1515 void 1516 xfs_buftarg_drain( 1517 struct xfs_buftarg *btp) 1518 { 1519 LIST_HEAD(dispose); 1520 int loop = 0; 1521 bool write_fail = false; 1522 1523 xfs_buftarg_wait(btp); 1524 1525 /* loop until there is nothing left on the lru list. */ 1526 while (list_lru_count(&btp->bt_lru)) { 1527 list_lru_walk(&btp->bt_lru, xfs_buftarg_drain_rele, 1528 &dispose, LONG_MAX); 1529 1530 while (!list_empty(&dispose)) { 1531 struct xfs_buf *bp; 1532 bp = list_first_entry(&dispose, struct xfs_buf, b_lru); 1533 list_del_init(&bp->b_lru); 1534 if (bp->b_flags & XBF_WRITE_FAIL) { 1535 write_fail = true; 1536 xfs_buf_alert_ratelimited(bp, 1537 "XFS: Corruption Alert", 1538 "Corruption Alert: Buffer at daddr 0x%llx had permanent write failures!", 1539 (long long)xfs_buf_daddr(bp)); 1540 } 1541 xfs_buf_destroy(bp); 1542 } 1543 if (loop++ != 0) 1544 delay(100); 1545 } 1546 1547 /* 1548 * If one or more failed buffers were freed, that means dirty metadata 1549 * was thrown away. This should only ever happen after I/O completion 1550 * handling has elevated I/O error(s) to permanent failures and shuts 1551 * down the journal. 1552 */ 1553 if (write_fail) { 1554 ASSERT(xlog_is_shutdown(btp->bt_mount->m_log)); 1555 xfs_alert(btp->bt_mount, 1556 "Please run xfs_repair to determine the extent of the problem."); 1557 } 1558 } 1559 1560 static enum lru_status 1561 xfs_buftarg_isolate( 1562 struct list_head *item, 1563 struct list_lru_one *lru, 1564 void *arg) 1565 { 1566 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru); 1567 struct list_head *dispose = arg; 1568 1569 /* 1570 * We are inverting the lru lock vs bp->b_lockref.lock order here, so 1571 * use a trylock. If we fail to get the lock, just skip the buffer. 1572 */ 1573 if (!spin_trylock(&bp->b_lockref.lock)) 1574 return LRU_SKIP; 1575 1576 /* 1577 * If the buffer is in use, remove it from the LRU for now. We can't 1578 * free it while someone is using it, and we should also not count 1579 * eviction passed for it, just as if it hadn't been added to the LRU 1580 * yet. 1581 */ 1582 if (bp->b_lockref.count > 0) { 1583 list_lru_isolate(lru, &bp->b_lru); 1584 spin_unlock(&bp->b_lockref.lock); 1585 return LRU_REMOVED; 1586 } 1587 1588 /* 1589 * Decrement the b_lru_ref count unless the value is already 1590 * zero. If the value is already zero, we need to reclaim the 1591 * buffer, otherwise it gets another trip through the LRU. 1592 */ 1593 if (atomic_add_unless(&bp->b_lru_ref, -1, 0)) { 1594 spin_unlock(&bp->b_lockref.lock); 1595 return LRU_ROTATE; 1596 } 1597 1598 xfs_buf_kill(bp); 1599 list_lru_isolate_move(lru, item, dispose); 1600 spin_unlock(&bp->b_lockref.lock); 1601 return LRU_REMOVED; 1602 } 1603 1604 static unsigned long 1605 xfs_buftarg_shrink_scan( 1606 struct shrinker *shrink, 1607 struct shrink_control *sc) 1608 { 1609 struct xfs_buftarg *btp = shrink->private_data; 1610 LIST_HEAD(dispose); 1611 unsigned long freed; 1612 1613 freed = list_lru_shrink_walk(&btp->bt_lru, sc, 1614 xfs_buftarg_isolate, &dispose); 1615 1616 while (!list_empty(&dispose)) { 1617 struct xfs_buf *bp; 1618 bp = list_first_entry(&dispose, struct xfs_buf, b_lru); 1619 list_del_init(&bp->b_lru); 1620 xfs_buf_destroy(bp); 1621 } 1622 1623 return freed; 1624 } 1625 1626 static unsigned long 1627 xfs_buftarg_shrink_count( 1628 struct shrinker *shrink, 1629 struct shrink_control *sc) 1630 { 1631 struct xfs_buftarg *btp = shrink->private_data; 1632 return list_lru_shrink_count(&btp->bt_lru, sc); 1633 } 1634 1635 void 1636 xfs_destroy_buftarg( 1637 struct xfs_buftarg *btp) 1638 { 1639 shrinker_free(btp->bt_shrinker); 1640 ASSERT(percpu_counter_sum(&btp->bt_readahead_count) == 0); 1641 percpu_counter_destroy(&btp->bt_readahead_count); 1642 list_lru_destroy(&btp->bt_lru); 1643 rhashtable_destroy(&btp->bt_hash); 1644 } 1645 1646 void 1647 xfs_free_buftarg( 1648 struct xfs_buftarg *btp) 1649 { 1650 xfs_destroy_buftarg(btp); 1651 fs_put_dax(btp->bt_daxdev, btp->bt_mount); 1652 /* the main block device is closed by kill_block_super */ 1653 if (btp->bt_bdev != btp->bt_mount->m_super->s_bdev) 1654 fs_bdev_file_release(btp->bt_file, btp->bt_mount->m_super); 1655 kfree(btp); 1656 } 1657 1658 /* 1659 * Configure this buffer target for hardware-assisted atomic writes if the 1660 * underlying block device supports is congruent with the filesystem geometry. 1661 */ 1662 static inline void 1663 xfs_configure_buftarg_atomic_writes( 1664 struct xfs_buftarg *btp) 1665 { 1666 struct xfs_mount *mp = btp->bt_mount; 1667 unsigned int min_bytes, max_bytes; 1668 1669 min_bytes = bdev_atomic_write_unit_min_bytes(btp->bt_bdev); 1670 max_bytes = bdev_atomic_write_unit_max_bytes(btp->bt_bdev); 1671 1672 /* 1673 * Ignore atomic write geometry that is nonsense or doesn't even cover 1674 * a single fsblock. 1675 */ 1676 if (min_bytes > max_bytes || 1677 min_bytes > mp->m_sb.sb_blocksize || 1678 max_bytes < mp->m_sb.sb_blocksize) { 1679 min_bytes = 0; 1680 max_bytes = 0; 1681 } 1682 1683 btp->bt_awu_min = min_bytes; 1684 btp->bt_awu_max = max_bytes; 1685 } 1686 1687 /* Configure a buffer target that abstracts a block device. */ 1688 int 1689 xfs_configure_buftarg( 1690 struct xfs_buftarg *btp, 1691 unsigned int sectorsize, 1692 xfs_rfsblock_t nr_blocks) 1693 { 1694 struct xfs_mount *mp = btp->bt_mount; 1695 1696 if (btp->bt_bdev) { 1697 int error; 1698 1699 error = bdev_validate_blocksize(btp->bt_bdev, sectorsize); 1700 if (error) { 1701 xfs_warn(mp, 1702 "Cannot use blocksize %u on device %pg, err %d", 1703 sectorsize, btp->bt_bdev, error); 1704 return -EINVAL; 1705 } 1706 1707 if (bdev_can_atomic_write(btp->bt_bdev)) 1708 xfs_configure_buftarg_atomic_writes(btp); 1709 } 1710 1711 btp->bt_meta_sectorsize = sectorsize; 1712 btp->bt_meta_sectormask = sectorsize - 1; 1713 /* m_blkbb_log is not set up yet */ 1714 btp->bt_nr_sectors = nr_blocks << (mp->m_sb.sb_blocklog - BBSHIFT); 1715 return 0; 1716 } 1717 1718 int 1719 xfs_init_buftarg( 1720 struct xfs_buftarg *btp, 1721 size_t logical_sectorsize, 1722 const char *descr) 1723 { 1724 /* The maximum size of the buftarg is only known once the sb is read. */ 1725 btp->bt_nr_sectors = XFS_BUF_DADDR_MAX; 1726 1727 /* Set up device logical sector size mask */ 1728 btp->bt_logical_sectorsize = logical_sectorsize; 1729 btp->bt_logical_sectormask = logical_sectorsize - 1; 1730 1731 /* 1732 * Buffer IO error rate limiting. Limit it to no more than 10 messages 1733 * per 30 seconds so as to not spam logs too much on repeated errors. 1734 */ 1735 ratelimit_state_init(&btp->bt_ioerror_rl, 30 * HZ, 1736 DEFAULT_RATELIMIT_BURST); 1737 1738 if (rhashtable_init(&btp->bt_hash, &xfs_buf_hash_params)) 1739 return -ENOMEM; 1740 if (list_lru_init(&btp->bt_lru)) 1741 goto out_destroy_hash; 1742 if (percpu_counter_init(&btp->bt_readahead_count, 0, GFP_KERNEL)) 1743 goto out_destroy_lru; 1744 1745 btp->bt_shrinker = 1746 shrinker_alloc(SHRINKER_NUMA_AWARE, "xfs-buf:%s", descr); 1747 if (!btp->bt_shrinker) 1748 goto out_destroy_io_count; 1749 btp->bt_shrinker->count_objects = xfs_buftarg_shrink_count; 1750 btp->bt_shrinker->scan_objects = xfs_buftarg_shrink_scan; 1751 btp->bt_shrinker->private_data = btp; 1752 shrinker_register(btp->bt_shrinker); 1753 return 0; 1754 1755 out_destroy_io_count: 1756 percpu_counter_destroy(&btp->bt_readahead_count); 1757 out_destroy_lru: 1758 list_lru_destroy(&btp->bt_lru); 1759 out_destroy_hash: 1760 rhashtable_destroy(&btp->bt_hash); 1761 return -ENOMEM; 1762 } 1763 1764 struct xfs_buftarg * 1765 xfs_alloc_buftarg( 1766 struct xfs_mount *mp, 1767 struct file *bdev_file) 1768 { 1769 struct xfs_buftarg *btp; 1770 const struct dax_holder_operations *ops = NULL; 1771 int error; 1772 1773 1774 #if defined(CONFIG_FS_DAX) && defined(CONFIG_MEMORY_FAILURE) 1775 ops = &xfs_dax_holder_operations; 1776 #endif 1777 btp = kzalloc_obj(*btp, GFP_KERNEL | __GFP_NOFAIL); 1778 1779 btp->bt_mount = mp; 1780 btp->bt_file = bdev_file; 1781 btp->bt_bdev = file_bdev(bdev_file); 1782 btp->bt_dev = btp->bt_bdev->bd_dev; 1783 btp->bt_daxdev = fs_dax_get_by_bdev(btp->bt_bdev, &btp->bt_dax_part_off, 1784 mp, ops); 1785 1786 /* 1787 * Flush and invalidate all devices' pagecaches before reading any 1788 * metadata because XFS doesn't use the bdev pagecache. 1789 */ 1790 error = sync_blockdev(btp->bt_bdev); 1791 if (error) 1792 goto error_free; 1793 1794 /* 1795 * When allocating the buftargs we have not yet read the super block and 1796 * thus don't know the file system sector size yet. 1797 */ 1798 btp->bt_meta_sectorsize = bdev_logical_block_size(btp->bt_bdev); 1799 btp->bt_meta_sectormask = btp->bt_meta_sectorsize - 1; 1800 1801 error = xfs_init_buftarg(btp, btp->bt_meta_sectorsize, 1802 mp->m_super->s_id); 1803 if (error) 1804 goto error_free; 1805 1806 return btp; 1807 1808 error_free: 1809 fs_put_dax(btp->bt_daxdev, mp); 1810 kfree(btp); 1811 return ERR_PTR(error); 1812 } 1813 1814 static inline void 1815 xfs_buf_list_del( 1816 struct xfs_buf *bp) 1817 { 1818 list_del_init(&bp->b_list); 1819 wake_up_var(&bp->b_list); 1820 } 1821 1822 /* 1823 * Cancel a delayed write list. 1824 * 1825 * Remove each buffer from the list, clear the delwri queue flag and drop the 1826 * associated buffer reference. 1827 */ 1828 void 1829 xfs_buf_delwri_cancel( 1830 struct list_head *list) 1831 { 1832 struct xfs_buf *bp; 1833 1834 while (!list_empty(list)) { 1835 bp = list_first_entry(list, struct xfs_buf, b_list); 1836 1837 xfs_buf_lock(bp); 1838 xfs_buf_clear_flags(bp, _XBF_DELWRI_Q); 1839 xfs_buf_list_del(bp); 1840 xfs_buf_relse(bp); 1841 } 1842 } 1843 1844 /* 1845 * Add a buffer to the delayed write list. 1846 * 1847 * This queues a buffer for writeout if it hasn't already been. Note that 1848 * neither this routine nor the buffer list submission functions perform 1849 * any internal synchronization. It is expected that the lists are thread-local 1850 * to the callers. 1851 * 1852 * Returns true if we queued up the buffer, or false if it already had 1853 * been on the buffer list. 1854 */ 1855 bool 1856 xfs_buf_delwri_queue( 1857 struct xfs_buf *bp, 1858 struct list_head *list) 1859 { 1860 ASSERT(xfs_buf_islocked(bp)); 1861 ASSERT(!(bp->b_flags & XBF_READ)); 1862 1863 /* 1864 * If the buffer is already marked delwri it already is queued up 1865 * by someone else for imediate writeout. Just ignore it in that 1866 * case. 1867 */ 1868 if (bp->b_flags & _XBF_DELWRI_Q) { 1869 trace_xfs_buf_delwri_queued(bp, _RET_IP_); 1870 return false; 1871 } 1872 1873 trace_xfs_buf_delwri_queue(bp, _RET_IP_); 1874 1875 /* 1876 * If a buffer gets written out synchronously or marked stale while it 1877 * is on a delwri list we lazily remove it. To do this, the other party 1878 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone. 1879 * It remains referenced and on the list. In a rare corner case it 1880 * might get readded to a delwri list after the synchronous writeout, in 1881 * which case we need just need to re-add the flag here. 1882 */ 1883 xfs_buf_set_flags(bp, _XBF_DELWRI_Q); 1884 if (list_empty(&bp->b_list)) { 1885 xfs_buf_hold(bp); 1886 list_add_tail(&bp->b_list, list); 1887 } 1888 1889 return true; 1890 } 1891 1892 /* 1893 * Queue a buffer to this delwri list as part of a data integrity operation. 1894 * If the buffer is on any other delwri list, we'll wait for that to clear 1895 * so that the caller can submit the buffer for IO and wait for the result. 1896 * Callers must ensure the buffer is not already on the list. 1897 */ 1898 void 1899 xfs_buf_delwri_queue_here( 1900 struct xfs_buf *bp, 1901 struct list_head *buffer_list) 1902 { 1903 /* 1904 * We need this buffer to end up on the /caller's/ delwri list, not any 1905 * old list. This can happen if the buffer is marked stale (which 1906 * clears DELWRI_Q) after the AIL queues the buffer to its list but 1907 * before the AIL has a chance to submit the list. 1908 */ 1909 while (!list_empty(&bp->b_list)) { 1910 xfs_buf_unlock(bp); 1911 wait_var_event(&bp->b_list, list_empty(&bp->b_list)); 1912 xfs_buf_lock(bp); 1913 } 1914 1915 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q)); 1916 1917 xfs_buf_delwri_queue(bp, buffer_list); 1918 } 1919 1920 /* 1921 * Compare function is more complex than it needs to be because 1922 * the return value is only 32 bits and we are doing comparisons 1923 * on 64 bit values 1924 */ 1925 static int 1926 xfs_buf_cmp( 1927 void *priv, 1928 const struct list_head *a, 1929 const struct list_head *b) 1930 { 1931 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list); 1932 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list); 1933 xfs_daddr_t diff; 1934 1935 diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn; 1936 if (diff < 0) 1937 return -1; 1938 if (diff > 0) 1939 return 1; 1940 return 0; 1941 } 1942 1943 static bool 1944 xfs_buf_delwri_submit_prep( 1945 struct xfs_buf *bp) 1946 { 1947 /* 1948 * Someone else might have written the buffer synchronously or marked it 1949 * stale in the meantime. In that case only the _XBF_DELWRI_Q flag got 1950 * cleared, and we have to drop the reference and remove it from the 1951 * list here. 1952 */ 1953 if (!(bp->b_flags & _XBF_DELWRI_Q)) { 1954 xfs_buf_list_del(bp); 1955 xfs_buf_relse(bp); 1956 return false; 1957 } 1958 1959 trace_xfs_buf_delwri_split(bp, _RET_IP_); 1960 xfs_buf_clear_flags(bp, _XBF_DELWRI_Q); 1961 xfs_buf_set_flags(bp, XBF_WRITE); 1962 return true; 1963 } 1964 1965 /* 1966 * Write out a buffer list asynchronously. 1967 * 1968 * This will take the @buffer_list, write all non-locked and non-pinned buffers 1969 * out and not wait for I/O completion on any of the buffers. This interface 1970 * is only safely useable for callers that can track I/O completion by higher 1971 * level means, e.g. AIL pushing as the @buffer_list is consumed in this 1972 * function. 1973 * 1974 * Note: this function will skip buffers it would block on, and in doing so 1975 * leaves them on @buffer_list so they can be retried on a later pass. As such, 1976 * it is up to the caller to ensure that the buffer list is fully submitted or 1977 * cancelled appropriately when they are finished with the list. Failure to 1978 * cancel or resubmit the list until it is empty will result in leaked buffers 1979 * at unmount time. 1980 */ 1981 int 1982 xfs_buf_delwri_submit_nowait( 1983 struct list_head *buffer_list) 1984 { 1985 struct xfs_buf *bp, *n; 1986 int pinned = 0; 1987 struct blk_plug plug; 1988 1989 list_sort(NULL, buffer_list, xfs_buf_cmp); 1990 1991 blk_start_plug(&plug); 1992 list_for_each_entry_safe(bp, n, buffer_list, b_list) { 1993 if (!xfs_buf_trylock(bp)) 1994 continue; 1995 if (xfs_buf_ispinned(bp)) { 1996 xfs_buf_unlock(bp); 1997 pinned++; 1998 continue; 1999 } 2000 if (!xfs_buf_delwri_submit_prep(bp)) 2001 continue; 2002 xfs_buf_set_flags(bp, XBF_ASYNC); 2003 xfs_buf_list_del(bp); 2004 xfs_buf_submit(bp); 2005 } 2006 blk_finish_plug(&plug); 2007 2008 return pinned; 2009 } 2010 2011 /* 2012 * Write out a buffer list synchronously. 2013 * 2014 * This will take the @buffer_list, write all buffers out and wait for I/O 2015 * completion on all of the buffers. @buffer_list is consumed by the function, 2016 * so callers must have some other way of tracking buffers if they require such 2017 * functionality. 2018 */ 2019 int 2020 xfs_buf_delwri_submit( 2021 struct list_head *buffer_list) 2022 { 2023 LIST_HEAD (wait_list); 2024 int error = 0, error2; 2025 struct xfs_buf *bp, *n; 2026 struct blk_plug plug; 2027 2028 list_sort(NULL, buffer_list, xfs_buf_cmp); 2029 2030 blk_start_plug(&plug); 2031 list_for_each_entry_safe(bp, n, buffer_list, b_list) { 2032 xfs_buf_lock(bp); 2033 if (!xfs_buf_delwri_submit_prep(bp)) 2034 continue; 2035 xfs_buf_clear_flags(bp, XBF_ASYNC); 2036 list_move_tail(&bp->b_list, &wait_list); 2037 xfs_buf_submit(bp); 2038 } 2039 blk_finish_plug(&plug); 2040 2041 /* Wait for IO to complete. */ 2042 while (!list_empty(&wait_list)) { 2043 bp = list_first_entry(&wait_list, struct xfs_buf, b_list); 2044 2045 xfs_buf_list_del(bp); 2046 2047 /* 2048 * Wait on the locked buffer, check for errors and unlock and 2049 * release the delwri queue reference. 2050 */ 2051 error2 = xfs_buf_iowait(bp); 2052 xfs_buf_relse(bp); 2053 if (!error) 2054 error = error2; 2055 } 2056 2057 return error; 2058 } 2059 2060 void xfs_buf_set_ref(struct xfs_buf *bp, int lru_ref) 2061 { 2062 /* 2063 * Set the lru reference count to 0 based on the error injection tag. 2064 * This allows userspace to disrupt buffer caching for debug/testing 2065 * purposes. 2066 */ 2067 if (XFS_TEST_ERROR(bp->b_mount, XFS_ERRTAG_BUF_LRU_REF)) 2068 lru_ref = 0; 2069 2070 atomic_set(&bp->b_lru_ref, lru_ref); 2071 } 2072 2073 /* 2074 * Verify an on-disk magic value against the magic value specified in the 2075 * verifier structure. The verifier magic is in disk byte order so the caller is 2076 * expected to pass the value directly from disk. 2077 */ 2078 bool 2079 xfs_verify_magic( 2080 struct xfs_buf *bp, 2081 __be32 dmagic) 2082 { 2083 struct xfs_mount *mp = bp->b_mount; 2084 int idx; 2085 2086 idx = xfs_has_crc(mp); 2087 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic[idx])) 2088 return false; 2089 return dmagic == bp->b_ops->magic[idx]; 2090 } 2091 /* 2092 * Verify an on-disk magic value against the magic value specified in the 2093 * verifier structure. The verifier magic is in disk byte order so the caller is 2094 * expected to pass the value directly from disk. 2095 */ 2096 bool 2097 xfs_verify_magic16( 2098 struct xfs_buf *bp, 2099 __be16 dmagic) 2100 { 2101 struct xfs_mount *mp = bp->b_mount; 2102 int idx; 2103 2104 idx = xfs_has_crc(mp); 2105 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic16[idx])) 2106 return false; 2107 return dmagic == bp->b_ops->magic16[idx]; 2108 } 2109