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