1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2023-2025 Christoph Hellwig. 4 * Copyright (c) 2024-2025, Western Digital Corporation or its affiliates. 5 */ 6 #include "xfs_platform.h" 7 #include "xfs_shared.h" 8 #include "xfs_format.h" 9 #include "xfs_log_format.h" 10 #include "xfs_error.h" 11 #include "xfs_trans_resv.h" 12 #include "xfs_mount.h" 13 #include "xfs_inode.h" 14 #include "xfs_iomap.h" 15 #include "xfs_trans.h" 16 #include "xfs_alloc.h" 17 #include "xfs_bmap.h" 18 #include "xfs_bmap_btree.h" 19 #include "xfs_trans_space.h" 20 #include "xfs_refcount.h" 21 #include "xfs_rtbitmap.h" 22 #include "xfs_rtrmap_btree.h" 23 #include "xfs_zone_alloc.h" 24 #include "xfs_sysfs.h" 25 #include "xfs_zone_priv.h" 26 #include "xfs_zones.h" 27 #include "xfs_trace.h" 28 #include "xfs_mru_cache.h" 29 #include <linux/bio-integrity.h> 30 31 static void 32 xfs_open_zone_free_rcu( 33 struct callback_head *cb) 34 { 35 struct xfs_open_zone *oz = container_of(cb, typeof(*oz), oz_rcu); 36 37 xfs_rtgroup_rele(oz->oz_rtg); 38 kfree(oz); 39 } 40 41 void 42 xfs_open_zone_put( 43 struct xfs_open_zone *oz) 44 { 45 if (atomic_dec_and_test(&oz->oz_ref)) 46 call_rcu(&oz->oz_rcu, xfs_open_zone_free_rcu); 47 } 48 49 static inline uint32_t 50 xfs_zone_bucket( 51 struct xfs_mount *mp, 52 uint32_t used_blocks) 53 { 54 return XFS_ZONE_USED_BUCKETS * used_blocks / 55 mp->m_groups[XG_TYPE_RTG].blocks; 56 } 57 58 static inline void 59 xfs_zone_add_to_bucket( 60 struct xfs_zone_info *zi, 61 xfs_rgnumber_t rgno, 62 uint32_t to_bucket) 63 { 64 __set_bit(rgno, zi->zi_used_bucket_bitmap[to_bucket]); 65 zi->zi_used_bucket_entries[to_bucket]++; 66 } 67 68 static inline void 69 xfs_zone_remove_from_bucket( 70 struct xfs_zone_info *zi, 71 xfs_rgnumber_t rgno, 72 uint32_t from_bucket) 73 { 74 __clear_bit(rgno, zi->zi_used_bucket_bitmap[from_bucket]); 75 zi->zi_used_bucket_entries[from_bucket]--; 76 } 77 78 static void 79 xfs_zone_account_reclaimable( 80 struct xfs_rtgroup *rtg, 81 uint32_t freed) 82 { 83 struct xfs_group *xg = rtg_group(rtg); 84 struct xfs_mount *mp = rtg_mount(rtg); 85 struct xfs_zone_info *zi = mp->m_zone_info; 86 uint32_t used = rtg_rmap(rtg)->i_used_blocks; 87 xfs_rgnumber_t rgno = rtg_rgno(rtg); 88 uint32_t from_bucket = xfs_zone_bucket(mp, used + freed); 89 uint32_t to_bucket = xfs_zone_bucket(mp, used); 90 bool was_full = (used + freed == rtg_blocks(rtg)); 91 92 /* 93 * This can be called from log recovery, where the zone_info structure 94 * hasn't been allocated yet. Skip all work as xfs_mount_zones will 95 * add the zones to the right buckets before the file systems becomes 96 * active. 97 */ 98 if (!zi) 99 return; 100 101 if (!used) { 102 /* 103 * The zone is now empty, remove it from the bottom bucket and 104 * trigger a reset. 105 */ 106 trace_xfs_zone_emptied(rtg); 107 108 spin_lock(&zi->zi_used_buckets_lock); 109 if (!was_full) 110 xfs_zone_remove_from_bucket(zi, rgno, from_bucket); 111 spin_unlock(&zi->zi_used_buckets_lock); 112 113 spin_lock(&zi->zi_reset_list_lock); 114 xg->xg_next_reset = zi->zi_reset_list; 115 zi->zi_reset_list = xg; 116 spin_unlock(&zi->zi_reset_list_lock); 117 118 if (zi->zi_gc_thread) 119 wake_up_process(zi->zi_gc_thread); 120 } else if (was_full) { 121 /* 122 * The zone transitioned from full, mark it up as reclaimable 123 * and wake up GC which might be waiting for zones to reclaim. 124 */ 125 spin_lock(&zi->zi_used_buckets_lock); 126 xfs_zone_add_to_bucket(zi, rgno, to_bucket); 127 spin_unlock(&zi->zi_used_buckets_lock); 128 129 if (zi->zi_gc_thread && xfs_zoned_need_gc(mp)) 130 wake_up_process(zi->zi_gc_thread); 131 } else if (to_bucket != from_bucket) { 132 /* 133 * Move the zone to a new bucket if it dropped below the 134 * threshold. 135 */ 136 spin_lock(&zi->zi_used_buckets_lock); 137 xfs_zone_add_to_bucket(zi, rgno, to_bucket); 138 xfs_zone_remove_from_bucket(zi, rgno, from_bucket); 139 spin_unlock(&zi->zi_used_buckets_lock); 140 } 141 } 142 143 /* 144 * Check if we have any zones that can be reclaimed by looking at the entry 145 * counters for the zone buckets. 146 */ 147 bool 148 xfs_zoned_have_reclaimable( 149 struct xfs_zone_info *zi) 150 { 151 int i; 152 153 spin_lock(&zi->zi_used_buckets_lock); 154 for (i = 0; i < XFS_ZONE_USED_BUCKETS; i++) { 155 if (zi->zi_used_bucket_entries[i]) { 156 spin_unlock(&zi->zi_used_buckets_lock); 157 return true; 158 } 159 } 160 spin_unlock(&zi->zi_used_buckets_lock); 161 162 return false; 163 } 164 165 static void 166 xfs_open_zone_mark_full( 167 struct xfs_open_zone *oz) 168 { 169 struct xfs_rtgroup *rtg = oz->oz_rtg; 170 struct xfs_mount *mp = rtg_mount(rtg); 171 struct xfs_zone_info *zi = mp->m_zone_info; 172 uint32_t used = rtg_rmap(rtg)->i_used_blocks; 173 174 trace_xfs_zone_full(rtg); 175 176 WRITE_ONCE(rtg->rtg_open_zone, NULL); 177 178 spin_lock(&zi->zi_open_zones_lock); 179 if (oz->oz_is_gc) 180 zi->zi_nr_open_gc_zones--; 181 else 182 zi->zi_nr_open_zones--; 183 list_del_init(&oz->oz_entry); 184 spin_unlock(&zi->zi_open_zones_lock); 185 186 if (oz->oz_is_gc) 187 wake_up_process(zi->zi_gc_thread); 188 else 189 wake_up_all(&zi->zi_zone_wait); 190 191 if (used < rtg_blocks(rtg)) 192 xfs_zone_account_reclaimable(rtg, rtg_blocks(rtg) - used); 193 xfs_open_zone_put(oz); 194 } 195 196 static inline void 197 xfs_zone_inc_written( 198 struct xfs_open_zone *oz, 199 xfs_filblks_t len) 200 { 201 xfs_assert_ilocked(rtg_rmap(oz->oz_rtg), XFS_ILOCK_EXCL); 202 203 oz->oz_written += len; 204 if (oz->oz_written == rtg_blocks(oz->oz_rtg)) 205 xfs_open_zone_mark_full(oz); 206 } 207 208 /* 209 * Called for blocks that have been written to disk, but not actually linked to 210 * an inode, which can happen when garbage collection races with user data 211 * writes to a file. 212 */ 213 static void 214 xfs_zone_skip_blocks( 215 struct xfs_open_zone *oz, 216 xfs_filblks_t len) 217 { 218 struct xfs_rtgroup *rtg = oz->oz_rtg; 219 220 trace_xfs_zone_skip_blocks(oz, 0, len); 221 222 xfs_rtgroup_lock(rtg, XFS_RTGLOCK_RMAP); 223 xfs_zone_inc_written(oz, len); 224 xfs_rtgroup_unlock(rtg, XFS_RTGLOCK_RMAP); 225 226 xfs_add_frextents(rtg_mount(rtg), len); 227 } 228 229 static int 230 xfs_zoned_map_extent( 231 struct xfs_trans *tp, 232 struct xfs_inode *ip, 233 struct xfs_bmbt_irec *new, 234 struct xfs_open_zone *oz, 235 xfs_fsblock_t old_startblock) 236 { 237 struct xfs_bmbt_irec data; 238 struct xfs_rtgroup *rtg = oz->oz_rtg; 239 struct xfs_inode *rmapip = rtg_rmap(rtg); 240 int nmaps = 1; 241 int error; 242 243 /* Grab the corresponding mapping in the data fork. */ 244 error = xfs_bmapi_read(ip, new->br_startoff, new->br_blockcount, &data, 245 &nmaps, 0); 246 if (error) 247 return error; 248 249 /* 250 * Cap the update to the existing extent in the data fork because we can 251 * only overwrite one extent at a time. 252 */ 253 ASSERT(new->br_blockcount >= data.br_blockcount); 254 new->br_blockcount = data.br_blockcount; 255 256 /* 257 * If a data write raced with this GC write, keep the existing data in 258 * the data fork, mark our newly written GC extent as reclaimable, then 259 * move on to the next extent. 260 * 261 * Note that this can also happen when racing with operations that do 262 * not actually invalidate the data, but just move it to a different 263 * inode (XFS_IOC_EXCHANGE_RANGE), or to a different offset inside the 264 * inode (FALLOC_FL_COLLAPSE_RANGE / FALLOC_FL_INSERT_RANGE). If the 265 * data was just moved around, GC fails to free the zone, but the zone 266 * becomes a GC candidate again as soon as all previous GC I/O has 267 * finished and these blocks will be moved out eventually. 268 */ 269 if (old_startblock != NULLFSBLOCK && 270 old_startblock != data.br_startblock) 271 goto skip; 272 273 trace_xfs_reflink_cow_remap_from(ip, new); 274 trace_xfs_reflink_cow_remap_to(ip, &data); 275 276 error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK, 277 XFS_IEXT_REFLINK_END_COW_CNT); 278 if (error) 279 return error; 280 281 if (data.br_startblock != HOLESTARTBLOCK) { 282 ASSERT(data.br_startblock != DELAYSTARTBLOCK); 283 ASSERT(!isnullstartblock(data.br_startblock)); 284 285 xfs_bmap_unmap_extent(tp, ip, XFS_DATA_FORK, &data); 286 if (xfs_is_reflink_inode(ip)) { 287 xfs_refcount_decrease_extent(tp, true, &data); 288 } else { 289 error = xfs_free_extent_later(tp, data.br_startblock, 290 data.br_blockcount, NULL, 291 XFS_AG_RESV_NONE, 292 XFS_FREE_EXTENT_REALTIME); 293 if (error) 294 return error; 295 } 296 } 297 298 trace_xfs_zone_record_blocks(oz, 299 xfs_rtb_to_rgbno(tp->t_mountp, new->br_startblock), 300 new->br_blockcount); 301 xfs_rtgroup_lock(rtg, XFS_RTGLOCK_RMAP); 302 xfs_rtgroup_trans_join(tp, rtg, XFS_RTGLOCK_RMAP); 303 rmapip->i_used_blocks += new->br_blockcount; 304 ASSERT(rmapip->i_used_blocks <= rtg_blocks(rtg)); 305 xfs_zone_inc_written(oz, new->br_blockcount); 306 xfs_trans_log_inode(tp, rmapip, XFS_ILOG_CORE); 307 308 /* Map the new blocks into the data fork. */ 309 xfs_bmap_map_extent(tp, ip, XFS_DATA_FORK, new); 310 return 0; 311 312 skip: 313 trace_xfs_reflink_cow_remap_skip(ip, new); 314 xfs_zone_skip_blocks(oz, new->br_blockcount); 315 return 0; 316 } 317 318 int 319 xfs_zoned_end_io( 320 struct xfs_inode *ip, 321 xfs_off_t offset, 322 xfs_off_t count, 323 xfs_daddr_t daddr, 324 struct xfs_open_zone *oz, 325 xfs_fsblock_t old_startblock) 326 { 327 struct xfs_mount *mp = ip->i_mount; 328 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count); 329 struct xfs_bmbt_irec new = { 330 .br_startoff = XFS_B_TO_FSBT(mp, offset), 331 .br_startblock = xfs_daddr_to_rtb(mp, daddr), 332 .br_state = XFS_EXT_NORM, 333 }; 334 unsigned int resblks = 335 XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK); 336 struct xfs_trans *tp; 337 int error; 338 339 if (xfs_is_shutdown(mp)) 340 return -EIO; 341 342 while (new.br_startoff < end_fsb) { 343 new.br_blockcount = end_fsb - new.br_startoff; 344 345 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 346 XFS_TRANS_RESERVE | XFS_TRANS_RES_FDBLKS, &tp); 347 if (error) 348 return error; 349 xfs_ilock(ip, XFS_ILOCK_EXCL); 350 xfs_trans_ijoin(tp, ip, 0); 351 352 error = xfs_zoned_map_extent(tp, ip, &new, oz, old_startblock); 353 if (error) 354 xfs_trans_cancel(tp); 355 else 356 error = xfs_trans_commit(tp); 357 xfs_iunlock(ip, XFS_ILOCK_EXCL); 358 if (error) 359 return error; 360 361 new.br_startoff += new.br_blockcount; 362 new.br_startblock += new.br_blockcount; 363 if (old_startblock != NULLFSBLOCK) 364 old_startblock += new.br_blockcount; 365 } 366 367 return 0; 368 } 369 370 /* 371 * "Free" blocks allocated in a zone. 372 * 373 * Just decrement the used blocks counter and report the space as freed. 374 */ 375 int 376 xfs_zone_free_blocks( 377 struct xfs_trans *tp, 378 struct xfs_rtgroup *rtg, 379 xfs_fsblock_t fsbno, 380 xfs_filblks_t len) 381 { 382 struct xfs_mount *mp = tp->t_mountp; 383 struct xfs_inode *rmapip = rtg_rmap(rtg); 384 385 xfs_assert_ilocked(rmapip, XFS_ILOCK_EXCL); 386 387 if (len > rmapip->i_used_blocks) { 388 xfs_err(mp, 389 "trying to free more blocks (%lld) than used counter (%u).", 390 len, rmapip->i_used_blocks); 391 ASSERT(len <= rmapip->i_used_blocks); 392 xfs_rtginode_mark_sick(rtg, XFS_RTGI_RMAP); 393 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 394 return -EFSCORRUPTED; 395 } 396 397 trace_xfs_zone_free_blocks(rtg, xfs_rtb_to_rgbno(mp, fsbno), len); 398 399 rmapip->i_used_blocks -= len; 400 /* 401 * Don't add open zones to the reclaimable buckets. The I/O completion 402 * for writing the last block will take care of accounting for already 403 * unused blocks instead. 404 */ 405 if (!READ_ONCE(rtg->rtg_open_zone)) 406 xfs_zone_account_reclaimable(rtg, len); 407 xfs_add_frextents(mp, len); 408 xfs_trans_log_inode(tp, rmapip, XFS_ILOG_CORE); 409 return 0; 410 } 411 412 static struct xfs_open_zone * 413 xfs_init_open_zone( 414 struct xfs_rtgroup *rtg, 415 xfs_rgblock_t write_pointer, 416 enum rw_hint write_hint, 417 bool is_gc) 418 { 419 struct xfs_open_zone *oz; 420 421 oz = kzalloc_obj(*oz, GFP_NOFS | __GFP_NOFAIL); 422 spin_lock_init(&oz->oz_alloc_lock); 423 atomic_set(&oz->oz_ref, 1); 424 oz->oz_rtg = rtg; 425 oz->oz_allocated = write_pointer; 426 oz->oz_written = write_pointer; 427 oz->oz_write_hint = write_hint; 428 oz->oz_is_gc = is_gc; 429 430 /* 431 * All dereferences of rtg->rtg_open_zone hold the ILOCK for the rmap 432 * inode, but we don't really want to take that here because we are 433 * under the zone_list_lock. Ensure the pointer is only set for a fully 434 * initialized open zone structure so that a racy lookup finding it is 435 * fine. 436 */ 437 WRITE_ONCE(rtg->rtg_open_zone, oz); 438 return oz; 439 } 440 441 /* 442 * Claim one of the free zones. User allocations may not dip into the pool 443 * reserved for guaranteeing GC forward progress. 444 */ 445 static bool 446 xfs_claim_free_zone( 447 struct xfs_zone_info *zi, 448 bool is_gc) 449 { 450 int min_free = is_gc ? 1 : XFS_MIN_FREE_GC_ZONES; 451 int free = atomic_read(&zi->zi_nr_free_zones); 452 453 do { 454 if (free < min_free) 455 return false; 456 } while (!atomic_try_cmpxchg(&zi->zi_nr_free_zones, &free, free - 1)); 457 458 return true; 459 } 460 461 /* 462 * Find a completely free zone, open it, and return a reference. 463 */ 464 struct xfs_open_zone * 465 xfs_open_zone( 466 struct xfs_mount *mp, 467 enum rw_hint write_hint, 468 bool is_gc) 469 { 470 struct xfs_zone_info *zi = mp->m_zone_info; 471 XA_STATE (xas, &mp->m_groups[XG_TYPE_RTG].xa, 0); 472 struct xfs_group *xg; 473 474 if (!xfs_claim_free_zone(zi, is_gc)) 475 return NULL; 476 477 /* 478 * Pick the free zone with lowest index. Zones in the beginning of the 479 * address space typically provides higher bandwidth than those at the 480 * end of the address space on HDDs. 481 */ 482 xas_lock(&xas); 483 xas_for_each_marked(&xas, xg, ULONG_MAX, XFS_RTG_FREE) 484 if (atomic_inc_not_zero(&xg->xg_active_ref)) 485 goto found; 486 xas_unlock(&xas); 487 488 /* 489 * We should not end up here as we claimed a free zone before looking 490 * through the free zone list. The free zone counter and the number 491 * of zones marked as free must be out of sync. 492 */ 493 WARN_ON_ONCE(1); 494 495 atomic_inc(&zi->zi_nr_free_zones); 496 return NULL; 497 498 found: 499 xas_clear_mark(&xas, XFS_RTG_FREE); 500 xas_unlock(&xas); 501 502 set_current_state(TASK_RUNNING); 503 return xfs_init_open_zone(to_rtg(xg), 0, write_hint, is_gc); 504 } 505 506 static struct xfs_open_zone * 507 xfs_try_open_zone( 508 struct xfs_mount *mp, 509 enum rw_hint write_hint) 510 __releases(&mp->m_zone_info->zi_open_zones_lock) 511 __acquires(&mp->m_zone_info->zi_open_zones_lock) 512 { 513 struct xfs_zone_info *zi = mp->m_zone_info; 514 struct xfs_open_zone *oz; 515 516 if (zi->zi_nr_open_zones >= mp->m_max_open_zones - XFS_OPEN_GC_ZONES) 517 return NULL; 518 519 /* 520 * Increment the open zone count to reserve our slot before dropping 521 * zi_open_zones_lock. 522 */ 523 zi->zi_nr_open_zones++; 524 spin_unlock(&zi->zi_open_zones_lock); 525 oz = xfs_open_zone(mp, write_hint, false); 526 spin_lock(&zi->zi_open_zones_lock); 527 if (!oz) { 528 zi->zi_nr_open_zones--; 529 return NULL; 530 } 531 532 atomic_inc(&oz->oz_ref); 533 list_add_tail(&oz->oz_entry, &zi->zi_open_zones); 534 535 /* 536 * If this was the last free zone, other waiters might be waiting 537 * on us to write to it as well. 538 */ 539 wake_up_all(&zi->zi_zone_wait); 540 541 if (xfs_zoned_need_gc(mp)) 542 wake_up_process(zi->zi_gc_thread); 543 544 trace_xfs_zone_opened(oz->oz_rtg); 545 return oz; 546 } 547 548 enum xfs_zone_alloc_score { 549 /* Any open zone will do it, we're desperate */ 550 XFS_ZONE_ALLOC_ANY = 0, 551 552 /* It better fit somehow */ 553 XFS_ZONE_ALLOC_OK = 1, 554 555 /* Only reuse a zone if it fits really well. */ 556 XFS_ZONE_ALLOC_GOOD = 2, 557 }; 558 559 /* 560 * Life time hint co-location matrix. Fields not set default to 0 561 * aka XFS_ZONE_ALLOC_ANY. 562 */ 563 static const unsigned int 564 xfs_zoned_hint_score[WRITE_LIFE_HINT_NR][WRITE_LIFE_HINT_NR] = { 565 [WRITE_LIFE_NOT_SET] = { 566 [WRITE_LIFE_NOT_SET] = XFS_ZONE_ALLOC_OK, 567 }, 568 [WRITE_LIFE_NONE] = { 569 [WRITE_LIFE_NONE] = XFS_ZONE_ALLOC_OK, 570 }, 571 [WRITE_LIFE_SHORT] = { 572 [WRITE_LIFE_SHORT] = XFS_ZONE_ALLOC_GOOD, 573 }, 574 [WRITE_LIFE_MEDIUM] = { 575 [WRITE_LIFE_MEDIUM] = XFS_ZONE_ALLOC_GOOD, 576 }, 577 [WRITE_LIFE_LONG] = { 578 [WRITE_LIFE_LONG] = XFS_ZONE_ALLOC_OK, 579 [WRITE_LIFE_EXTREME] = XFS_ZONE_ALLOC_OK, 580 }, 581 [WRITE_LIFE_EXTREME] = { 582 [WRITE_LIFE_LONG] = XFS_ZONE_ALLOC_OK, 583 [WRITE_LIFE_EXTREME] = XFS_ZONE_ALLOC_OK, 584 }, 585 }; 586 587 static bool 588 xfs_try_use_zone( 589 struct xfs_zone_info *zi, 590 enum rw_hint file_hint, 591 struct xfs_open_zone *oz, 592 unsigned int goodness) 593 { 594 if (oz->oz_is_gc) 595 return false; 596 597 if (oz->oz_allocated == rtg_blocks(oz->oz_rtg)) 598 return false; 599 600 if (xfs_zoned_hint_score[oz->oz_write_hint][file_hint] < goodness) 601 return false; 602 603 if (!atomic_inc_not_zero(&oz->oz_ref)) 604 return false; 605 606 /* 607 * If we have a hint set for the data, use that for the zone even if 608 * some data was written already without any hint set, but don't change 609 * the temperature after that as that would make little sense without 610 * tracking per-temperature class written block counts, which is 611 * probably overkill anyway. 612 */ 613 if (file_hint != WRITE_LIFE_NOT_SET && 614 oz->oz_write_hint == WRITE_LIFE_NOT_SET) 615 oz->oz_write_hint = file_hint; 616 617 /* 618 * If we couldn't match by inode or life time we just pick the first 619 * zone with enough space above. For that we want the least busy zone 620 * for some definition of "least" busy. For now this simple LRU 621 * algorithm that rotates every zone to the end of the list will do it, 622 * even if it isn't exactly cache friendly. 623 */ 624 if (!list_is_last(&oz->oz_entry, &zi->zi_open_zones)) 625 list_move_tail(&oz->oz_entry, &zi->zi_open_zones); 626 return true; 627 } 628 629 static struct xfs_open_zone * 630 xfs_select_open_zone_lru( 631 struct xfs_zone_info *zi, 632 enum rw_hint file_hint, 633 unsigned int goodness) 634 { 635 struct xfs_open_zone *oz; 636 637 lockdep_assert_held(&zi->zi_open_zones_lock); 638 639 list_for_each_entry(oz, &zi->zi_open_zones, oz_entry) 640 if (xfs_try_use_zone(zi, file_hint, oz, goodness)) 641 return oz; 642 643 cond_resched_lock(&zi->zi_open_zones_lock); 644 return NULL; 645 } 646 647 static struct xfs_open_zone * 648 xfs_select_open_zone_mru( 649 struct xfs_zone_info *zi, 650 enum rw_hint file_hint) 651 { 652 struct xfs_open_zone *oz; 653 654 lockdep_assert_held(&zi->zi_open_zones_lock); 655 656 list_for_each_entry_reverse(oz, &zi->zi_open_zones, oz_entry) 657 if (xfs_try_use_zone(zi, file_hint, oz, XFS_ZONE_ALLOC_OK)) 658 return oz; 659 660 cond_resched_lock(&zi->zi_open_zones_lock); 661 return NULL; 662 } 663 664 static inline enum rw_hint xfs_inode_write_hint(struct xfs_inode *ip) 665 { 666 if (xfs_has_nolifetime(ip->i_mount)) 667 return WRITE_LIFE_NOT_SET; 668 return VFS_I(ip)->i_write_hint; 669 } 670 671 /* 672 * Try to tightly pack small files that are written back after they were closed 673 * instead of trying to open new zones for them or spread them to the least 674 * recently used zone. This optimizes the data layout for workloads that untar 675 * or copy a lot of small files. Right now this does not separate multiple such 676 * streams. 677 */ 678 static inline bool xfs_zoned_pack_tight(struct xfs_inode *ip) 679 { 680 struct xfs_mount *mp = ip->i_mount; 681 size_t zone_capacity = 682 XFS_FSB_TO_B(mp, mp->m_groups[XG_TYPE_RTG].blocks); 683 684 /* 685 * Do not pack write files that are already using a full zone to avoid 686 * fragmentation. 687 */ 688 if (i_size_read(VFS_I(ip)) >= zone_capacity) 689 return false; 690 691 return !inode_is_open_for_write(VFS_I(ip)) && 692 !(ip->i_diflags & XFS_DIFLAG_APPEND); 693 } 694 695 static struct xfs_open_zone * 696 xfs_select_zone_nowait( 697 struct xfs_mount *mp, 698 enum rw_hint write_hint, 699 bool pack_tight) 700 { 701 struct xfs_zone_info *zi = mp->m_zone_info; 702 struct xfs_open_zone *oz = NULL; 703 704 if (xfs_is_shutdown(mp)) 705 return NULL; 706 707 /* 708 * Try to fill up open zones with matching temperature if available. It 709 * is better to try to co-locate data when this is favorable, so we can 710 * activate empty zones when it is statistically better to separate 711 * data. 712 */ 713 spin_lock(&zi->zi_open_zones_lock); 714 oz = xfs_select_open_zone_lru(zi, write_hint, XFS_ZONE_ALLOC_GOOD); 715 if (oz) 716 goto out_unlock; 717 718 if (pack_tight) { 719 oz = xfs_select_open_zone_mru(zi, write_hint); 720 if (oz) 721 goto out_unlock; 722 } 723 724 /* 725 * See if we can open a new zone and use that so that data for different 726 * files is mixed as little as possible. 727 */ 728 oz = xfs_try_open_zone(mp, write_hint); 729 if (oz) 730 goto out_unlock; 731 732 /* 733 * Try to find a zone that is an ok match to colocate data with. 734 */ 735 oz = xfs_select_open_zone_lru(zi, write_hint, XFS_ZONE_ALLOC_OK); 736 if (oz) 737 goto out_unlock; 738 739 /* 740 * Pick the least recently used zone, regardless of hint match 741 */ 742 oz = xfs_select_open_zone_lru(zi, write_hint, XFS_ZONE_ALLOC_ANY); 743 out_unlock: 744 spin_unlock(&zi->zi_open_zones_lock); 745 return oz; 746 } 747 748 static struct xfs_open_zone * 749 xfs_select_zone( 750 struct xfs_mount *mp, 751 enum rw_hint write_hint, 752 bool pack_tight) 753 { 754 struct xfs_zone_info *zi = mp->m_zone_info; 755 DEFINE_WAIT (wait); 756 struct xfs_open_zone *oz; 757 758 oz = xfs_select_zone_nowait(mp, write_hint, pack_tight); 759 if (oz) 760 return oz; 761 762 for (;;) { 763 prepare_to_wait(&zi->zi_zone_wait, &wait, TASK_UNINTERRUPTIBLE); 764 oz = xfs_select_zone_nowait(mp, write_hint, pack_tight); 765 if (oz || xfs_is_shutdown(mp)) 766 break; 767 schedule(); 768 } 769 finish_wait(&zi->zi_zone_wait, &wait); 770 return oz; 771 } 772 773 static unsigned int 774 xfs_zone_alloc_blocks( 775 struct xfs_open_zone *oz, 776 xfs_filblks_t count_fsb, 777 sector_t *sector, 778 bool *is_seq) 779 { 780 struct xfs_rtgroup *rtg = oz->oz_rtg; 781 struct xfs_mount *mp = rtg_mount(rtg); 782 xfs_rgblock_t allocated; 783 784 spin_lock(&oz->oz_alloc_lock); 785 count_fsb = min3(count_fsb, XFS_MAX_BMBT_EXTLEN, 786 (xfs_filblks_t)rtg_blocks(rtg) - oz->oz_allocated); 787 if (!count_fsb) { 788 spin_unlock(&oz->oz_alloc_lock); 789 return 0; 790 } 791 allocated = oz->oz_allocated; 792 oz->oz_allocated += count_fsb; 793 spin_unlock(&oz->oz_alloc_lock); 794 795 trace_xfs_zone_alloc_blocks(oz, allocated, count_fsb); 796 797 *sector = xfs_gbno_to_daddr(rtg_group(rtg), 0); 798 *is_seq = bdev_zone_is_seq(mp->m_rtdev_targp->bt_bdev, *sector); 799 if (!*is_seq) 800 *sector += XFS_FSB_TO_BB(mp, allocated); 801 return XFS_FSB_TO_B(mp, count_fsb); 802 } 803 804 void 805 xfs_mark_rtg_boundary( 806 struct iomap_ioend *ioend) 807 { 808 struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount; 809 sector_t sector = ioend->io_bio.bi_iter.bi_sector; 810 811 if (xfs_rtb_to_rgbno(mp, xfs_daddr_to_rtb(mp, sector)) == 0) 812 ioend->io_flags |= IOMAP_IOEND_BOUNDARY; 813 } 814 815 /* 816 * Check if we have a cached last open zone available for the inode and 817 * if yes return a reference to it. 818 */ 819 static struct xfs_open_zone * 820 xfs_get_cached_zone( 821 struct xfs_inode *ip) 822 { 823 struct xfs_open_zone *oz; 824 825 rcu_read_lock(); 826 oz = VFS_I(ip)->i_private; 827 if (!oz) 828 goto out_unlock; 829 830 /* 831 * GC only steals open zones at mount time, so no GC zones should end up 832 * in the cache. 833 */ 834 ASSERT(!oz->oz_is_gc); 835 836 /* 837 * Drop the old cached open zone if it is full. 838 */ 839 if (oz->oz_allocated == rtg_blocks(oz->oz_rtg)) { 840 spin_lock(&ip->i_flags_lock); 841 oz = VFS_I(ip)->i_private; 842 if (oz && oz->oz_allocated == rtg_blocks(oz->oz_rtg)) { 843 VFS_I(ip)->i_private = NULL; 844 spin_unlock(&ip->i_flags_lock); 845 xfs_open_zone_put(oz); 846 oz = NULL; 847 goto out_unlock; 848 } 849 spin_unlock(&ip->i_flags_lock); 850 } 851 852 if (oz && !atomic_inc_not_zero(&oz->oz_ref)) 853 oz = NULL; 854 out_unlock: 855 rcu_read_unlock(); 856 return oz; 857 } 858 859 /* 860 * Stash our zone in the inode so that it is reused for future allocations. 861 * 862 * The open_zone structure will be pinned until either the inode is freed or 863 * until the cached open zone is replaced with a different one because the 864 * current one was full when we tried to use it. This means we keep any 865 * open zone around forever as long as any inode that used it for the last 866 * write is cached, which slightly increases the memory use of cached inodes 867 * that were every written to, but significantly simplifies the cached zone 868 * lookup. Because the open_zone is clearly marked as full when all data 869 * in the underlying RTG was written, the caching is always safe. 870 * 871 * Called with a reference on @oz held. And returns two references on the 872 * returned zone: one for the caller and one for pinning the zone in 873 * inode->i_private. 874 */ 875 static struct xfs_open_zone * 876 xfs_set_cached_zone( 877 struct xfs_inode *ip, 878 struct xfs_open_zone *oz) 879 { 880 struct xfs_open_zone *old_oz; 881 882 /* 883 * If the open zone cached in the inode still has free space, use that 884 * instead of the new open zone just selected. This can happen when 885 * multiple threads race to perform zone selection for an inode. 886 * io_uring worker threads seem to be good way to trigger this. 887 * 888 * We need to grab an extra reference to this open zone as the caller 889 * owns a reference in addition to the i_private pointer. 890 */ 891 spin_lock(&ip->i_flags_lock); 892 old_oz = VFS_I(ip)->i_private; 893 if (old_oz && old_oz->oz_allocated < rtg_blocks(old_oz->oz_rtg) && 894 atomic_inc_not_zero(&old_oz->oz_ref)) { 895 spin_unlock(&ip->i_flags_lock); 896 xfs_open_zone_put(oz); 897 return old_oz; 898 } 899 VFS_I(ip)->i_private = oz; 900 atomic_inc(&oz->oz_ref); 901 spin_unlock(&ip->i_flags_lock); 902 if (old_oz) 903 xfs_open_zone_put(old_oz); 904 return oz; 905 } 906 907 static void 908 xfs_submit_zoned_bio( 909 struct iomap_ioend *ioend, 910 struct xfs_open_zone *oz, 911 bool is_seq) 912 { 913 ioend->io_bio.bi_iter.bi_sector = ioend->io_sector; 914 ioend->io_private = oz; 915 atomic_inc(&oz->oz_ref); /* for xfs_zoned_end_io */ 916 917 if (is_seq) { 918 ioend->io_bio.bi_opf &= ~REQ_OP_WRITE; 919 ioend->io_bio.bi_opf |= REQ_OP_ZONE_APPEND; 920 } else { 921 xfs_mark_rtg_boundary(ioend); 922 } 923 924 submit_bio(&ioend->io_bio); 925 } 926 927 void 928 xfs_zone_alloc_and_submit( 929 struct iomap_ioend *ioend, 930 struct xfs_open_zone **oz) 931 { 932 struct xfs_inode *ip = XFS_I(ioend->io_inode); 933 struct xfs_mount *mp = ip->i_mount; 934 enum rw_hint write_hint = xfs_inode_write_hint(ip); 935 bool pack_tight = xfs_zoned_pack_tight(ip); 936 unsigned int alloc_len; 937 struct iomap_ioend *split; 938 bool is_seq; 939 940 if (xfs_is_shutdown(mp)) 941 goto out_error; 942 943 if (ioend->io_flags & IOMAP_IOEND_INTEGRITY) 944 fs_bio_integrity_generate(&ioend->io_bio); 945 946 /* 947 * If we don't have a locally cached zone in this write context, see if 948 * the inode is still associated with a zone and use that if so. 949 */ 950 if (!*oz) 951 select_zone: 952 *oz = xfs_get_cached_zone(ip); 953 if (!*oz) { 954 *oz = xfs_select_zone(mp, write_hint, pack_tight); 955 if (!*oz) 956 goto out_error; 957 *oz = xfs_set_cached_zone(ip, *oz); 958 } 959 960 alloc_len = xfs_zone_alloc_blocks(*oz, XFS_B_TO_FSB(mp, ioend->io_size), 961 &ioend->io_sector, &is_seq); 962 if (!alloc_len) { 963 xfs_open_zone_put(*oz); 964 goto select_zone; 965 } 966 967 while ((split = iomap_split_ioend(ioend, alloc_len, is_seq))) { 968 if (IS_ERR(split)) 969 goto out_split_error; 970 alloc_len -= split->io_bio.bi_iter.bi_size; 971 xfs_submit_zoned_bio(split, *oz, is_seq); 972 if (!alloc_len) { 973 xfs_open_zone_put(*oz); 974 goto select_zone; 975 } 976 } 977 978 xfs_submit_zoned_bio(ioend, *oz, is_seq); 979 return; 980 981 out_split_error: 982 ioend->io_bio.bi_status = errno_to_blk_status(PTR_ERR(split)); 983 out_error: 984 bio_io_error(&ioend->io_bio); 985 } 986 987 /* 988 * Wake up all threads waiting for a zoned space allocation when the file system 989 * is shut down. 990 */ 991 void 992 xfs_zoned_wake_all( 993 struct xfs_mount *mp) 994 { 995 /* 996 * Don't wake up if there is no m_zone_info. This is complicated by the 997 * fact that unmount can't atomically clear m_zone_info and thus we need 998 * to check SB_ACTIVE for that, but mount temporarily enables SB_ACTIVE 999 * during log recovery so we can't entirely rely on that either. 1000 */ 1001 if ((mp->m_super->s_flags & SB_ACTIVE) && mp->m_zone_info) 1002 wake_up_all(&mp->m_zone_info->zi_zone_wait); 1003 } 1004 1005 /* 1006 * Check if @rgbno in @rgb is a potentially valid block. It might still be 1007 * unused, but that information is only found in the rmap. 1008 */ 1009 bool 1010 xfs_zone_rgbno_is_valid( 1011 struct xfs_rtgroup *rtg, 1012 xfs_rgnumber_t rgbno) 1013 { 1014 lockdep_assert_held(&rtg_rmap(rtg)->i_lock); 1015 1016 if (rtg->rtg_open_zone) 1017 return rgbno < rtg->rtg_open_zone->oz_allocated; 1018 return !xa_get_mark(&rtg_mount(rtg)->m_groups[XG_TYPE_RTG].xa, 1019 rtg_rgno(rtg), XFS_RTG_FREE); 1020 } 1021 1022 void 1023 xfs_zone_mark_free( 1024 struct xfs_rtgroup *rtg) 1025 { 1026 xfs_group_set_mark(rtg_group(rtg), XFS_RTG_FREE); 1027 atomic_inc(&rtg_mount(rtg)->m_zone_info->zi_nr_free_zones); 1028 } 1029 1030 static void 1031 xfs_free_open_zones( 1032 struct xfs_zone_info *zi) 1033 { 1034 struct xfs_open_zone *oz; 1035 1036 spin_lock(&zi->zi_open_zones_lock); 1037 while ((oz = list_first_entry_or_null(&zi->zi_open_zones, 1038 struct xfs_open_zone, oz_entry))) { 1039 list_del(&oz->oz_entry); 1040 xfs_open_zone_put(oz); 1041 } 1042 spin_unlock(&zi->zi_open_zones_lock); 1043 1044 /* 1045 * Wait for all open zones to be freed so that they drop the group 1046 * references: 1047 */ 1048 rcu_barrier(); 1049 } 1050 1051 struct xfs_init_zones { 1052 uint32_t zone_size; 1053 uint32_t zone_capacity; 1054 uint64_t available; 1055 uint64_t reclaimable; 1056 }; 1057 1058 /* 1059 * For sequential write required zones, we restart writing at the hardware write 1060 * pointer returned by xfs_validate_blk_zone(). 1061 * 1062 * For conventional zones or conventional devices we have to query the rmap to 1063 * find the highest recorded block and set the write pointer to the block after 1064 * that. In case of a power loss this misses blocks where the data I/O has 1065 * completed but not recorded in the rmap yet, and it also rewrites blocks if 1066 * the most recently written ones got deleted again before unmount, but this is 1067 * the best we can do without hardware support. 1068 */ 1069 static int 1070 xfs_query_write_pointer( 1071 struct xfs_init_zones *iz, 1072 struct xfs_rtgroup *rtg, 1073 xfs_rgblock_t *write_pointer) 1074 { 1075 struct xfs_mount *mp = rtg_mount(rtg); 1076 struct block_device *bdev = mp->m_rtdev_targp->bt_bdev; 1077 sector_t start = xfs_gbno_to_daddr(&rtg->rtg_group, 0); 1078 xfs_rgblock_t highest_rgbno; 1079 struct blk_zone zone = {}; 1080 int error; 1081 1082 if (bdev_is_zoned(bdev)) { 1083 error = blkdev_get_zone_info(bdev, start, &zone); 1084 if (error) 1085 return error; 1086 if (zone.start != start) { 1087 xfs_warn(mp, "mismatched zone start: 0x%llx/0x%llx.", 1088 zone.start, start); 1089 return -EFSCORRUPTED; 1090 } 1091 1092 if (!xfs_validate_blk_zone(mp, &zone, rtg_rgno(rtg), 1093 iz->zone_size, iz->zone_capacity, 1094 write_pointer)) 1095 return -EFSCORRUPTED; 1096 1097 /* 1098 * Use the hardware write pointer returned by 1099 * xfs_validate_blk_zone for sequential write required zones, 1100 * else fall through to the rmap-based estimation below. 1101 */ 1102 if (zone.cond != BLK_ZONE_COND_NOT_WP) 1103 return 0; 1104 } 1105 1106 xfs_rtgroup_lock(rtg, XFS_RTGLOCK_RMAP); 1107 highest_rgbno = xfs_rtrmap_highest_rgbno(rtg); 1108 xfs_rtgroup_unlock(rtg, XFS_RTGLOCK_RMAP); 1109 1110 if (highest_rgbno == NULLRGBLOCK) 1111 *write_pointer = 0; 1112 else 1113 *write_pointer = highest_rgbno + 1; 1114 return 0; 1115 } 1116 1117 static int 1118 xfs_init_zone( 1119 struct xfs_init_zones *iz, 1120 struct xfs_rtgroup *rtg, 1121 xfs_rgblock_t write_pointer) 1122 { 1123 struct xfs_mount *mp = rtg_mount(rtg); 1124 struct xfs_zone_info *zi = mp->m_zone_info; 1125 uint32_t used = rtg_rmap(rtg)->i_used_blocks; 1126 int error; 1127 1128 if (write_pointer > rtg->rtg_extents) { 1129 xfs_warn(mp, "zone %u has invalid write pointer (0x%x).", 1130 rtg_rgno(rtg), write_pointer); 1131 return -EFSCORRUPTED; 1132 } 1133 1134 if (used > rtg->rtg_extents) { 1135 xfs_warn(mp, 1136 "zone %u has used counter (0x%x) larger than zone capacity (0x%llx).", 1137 rtg_rgno(rtg), used, rtg->rtg_extents); 1138 return -EFSCORRUPTED; 1139 } 1140 1141 if (used > write_pointer) { 1142 xfs_warn(mp, 1143 "zone %u has used counter (0x%x) larger than write pointer (0x%x).", 1144 rtg_rgno(rtg), used, write_pointer); 1145 return -EFSCORRUPTED; 1146 } 1147 1148 if (write_pointer == 0 && used != 0) { 1149 xfs_warn(mp, "empty zone %u has non-zero used counter (0x%x).", 1150 rtg_rgno(rtg), used); 1151 return -EFSCORRUPTED; 1152 } 1153 1154 /* 1155 * If there are no used blocks, but the zone is not in empty state yet 1156 * we lost power before the zoned reset. In that case finish the work 1157 * here. 1158 */ 1159 if (write_pointer == rtg_blocks(rtg) && used == 0) { 1160 error = xfs_zone_gc_reset_sync(rtg); 1161 if (error) 1162 return error; 1163 write_pointer = 0; 1164 } 1165 1166 if (write_pointer == 0) { 1167 /* zone is empty */ 1168 xfs_zone_mark_free(rtg); 1169 iz->available += rtg_blocks(rtg); 1170 } else if (write_pointer < rtg_blocks(rtg)) { 1171 /* zone is open */ 1172 struct xfs_open_zone *oz; 1173 1174 atomic_inc(&rtg_group(rtg)->xg_active_ref); 1175 oz = xfs_init_open_zone(rtg, write_pointer, WRITE_LIFE_NOT_SET, 1176 false); 1177 list_add_tail(&oz->oz_entry, &zi->zi_open_zones); 1178 zi->zi_nr_open_zones++; 1179 1180 iz->available += (rtg_blocks(rtg) - write_pointer); 1181 iz->reclaimable += write_pointer - used; 1182 } else if (used < rtg_blocks(rtg)) { 1183 /* zone fully written, but has freed blocks */ 1184 xfs_zone_account_reclaimable(rtg, rtg_blocks(rtg) - used); 1185 iz->reclaimable += (rtg_blocks(rtg) - used); 1186 } 1187 1188 return 0; 1189 } 1190 1191 /* 1192 * Calculate the max open zone limit based on the of number of backing zones 1193 * available. 1194 */ 1195 static inline uint32_t 1196 xfs_max_open_zones( 1197 struct xfs_mount *mp) 1198 { 1199 unsigned int max_open, max_open_data_zones; 1200 1201 /* 1202 * We need two zones for every open data zone, one in reserve as we 1203 * don't reclaim open zones. One data zone and its spare is included 1204 * in XFS_MIN_ZONES to support at least one user data writer. 1205 */ 1206 max_open_data_zones = (mp->m_sb.sb_rgcount - XFS_MIN_ZONES) / 2 + 1; 1207 max_open = max_open_data_zones + XFS_OPEN_GC_ZONES; 1208 1209 /* 1210 * Cap the max open limit to 1/4 of available space. Without this we'd 1211 * run out of easy reclaim targets too quickly and storage devices don't 1212 * handle huge numbers of concurrent write streams overly well. 1213 */ 1214 max_open = min(max_open, mp->m_sb.sb_rgcount / 4); 1215 1216 return max(XFS_MIN_OPEN_ZONES, max_open); 1217 } 1218 1219 /* 1220 * Normally we use the open zone limit that the device reports. If there is 1221 * none let the user pick one from the command line. 1222 * 1223 * If the device doesn't report an open zone limit and there is no override, 1224 * allow to hold about a quarter of the zones open. In theory we could allow 1225 * all to be open, but at that point we run into GC deadlocks because we can't 1226 * reclaim open zones. 1227 * 1228 * When used on conventional SSDs a lower open limit is advisable as we'll 1229 * otherwise overwhelm the FTL just as much as a conventional block allocator. 1230 * 1231 * Note: To debug the open zone management code, force max_open to 1 here. 1232 */ 1233 static int 1234 xfs_calc_open_zones( 1235 struct xfs_mount *mp) 1236 { 1237 struct block_device *bdev = mp->m_rtdev_targp->bt_bdev; 1238 unsigned int bdev_open_zones = bdev_max_open_zones(bdev); 1239 1240 if (!mp->m_max_open_zones) { 1241 if (bdev_open_zones) 1242 mp->m_max_open_zones = bdev_open_zones; 1243 else 1244 mp->m_max_open_zones = XFS_DEFAULT_MAX_OPEN_ZONES; 1245 } 1246 1247 if (mp->m_max_open_zones < XFS_MIN_OPEN_ZONES) { 1248 xfs_notice(mp, "need at least %u open zones.", 1249 XFS_MIN_OPEN_ZONES); 1250 return -EIO; 1251 } 1252 1253 if (bdev_open_zones && bdev_open_zones < mp->m_max_open_zones) { 1254 mp->m_max_open_zones = bdev_open_zones; 1255 xfs_info(mp, "limiting open zones to %u due to hardware limit.", 1256 bdev_open_zones); 1257 } 1258 1259 if (mp->m_max_open_zones > xfs_max_open_zones(mp)) { 1260 mp->m_max_open_zones = xfs_max_open_zones(mp); 1261 xfs_info(mp, 1262 "limiting open zones to %u due to total zone count (%u)", 1263 mp->m_max_open_zones, mp->m_sb.sb_rgcount); 1264 } 1265 1266 return 0; 1267 } 1268 1269 static unsigned long * 1270 xfs_alloc_bucket_bitmap( 1271 struct xfs_mount *mp) 1272 { 1273 return kvmalloc_array(BITS_TO_LONGS(mp->m_sb.sb_rgcount), 1274 sizeof(unsigned long), GFP_KERNEL | __GFP_ZERO); 1275 } 1276 1277 static struct xfs_zone_info * 1278 xfs_alloc_zone_info( 1279 struct xfs_mount *mp) 1280 { 1281 struct xfs_zone_info *zi; 1282 int i; 1283 1284 zi = kzalloc_obj(*zi); 1285 if (!zi) 1286 return NULL; 1287 INIT_LIST_HEAD(&zi->zi_open_zones); 1288 INIT_LIST_HEAD(&zi->zi_reclaim_reservations); 1289 spin_lock_init(&zi->zi_reset_list_lock); 1290 spin_lock_init(&zi->zi_open_zones_lock); 1291 spin_lock_init(&zi->zi_reservation_lock); 1292 init_waitqueue_head(&zi->zi_zone_wait); 1293 spin_lock_init(&zi->zi_used_buckets_lock); 1294 for (i = 0; i < XFS_ZONE_USED_BUCKETS; i++) { 1295 zi->zi_used_bucket_bitmap[i] = xfs_alloc_bucket_bitmap(mp); 1296 if (!zi->zi_used_bucket_bitmap[i]) 1297 goto out_free_bitmaps; 1298 } 1299 return zi; 1300 1301 out_free_bitmaps: 1302 while (--i >= 0) 1303 kvfree(zi->zi_used_bucket_bitmap[i]); 1304 kfree(zi); 1305 return NULL; 1306 } 1307 1308 static void 1309 xfs_free_zone_info( 1310 struct xfs_zone_info *zi) 1311 { 1312 int i; 1313 1314 xfs_free_open_zones(zi); 1315 for (i = 0; i < XFS_ZONE_USED_BUCKETS; i++) 1316 kvfree(zi->zi_used_bucket_bitmap[i]); 1317 kfree(zi); 1318 } 1319 1320 static int 1321 xfs_report_zones( 1322 struct xfs_mount *mp, 1323 struct xfs_init_zones *iz) 1324 { 1325 struct xfs_rtgroup *rtg = NULL; 1326 1327 while ((rtg = xfs_rtgroup_next(mp, rtg))) { 1328 xfs_rgblock_t write_pointer; 1329 int error; 1330 1331 error = xfs_query_write_pointer(iz, rtg, &write_pointer); 1332 if (!error) 1333 error = xfs_init_zone(iz, rtg, write_pointer); 1334 if (error) { 1335 xfs_rtgroup_rele(rtg); 1336 return error; 1337 } 1338 } 1339 1340 return 0; 1341 } 1342 1343 static inline bool 1344 xfs_zone_is_conv( 1345 struct xfs_rtgroup *rtg) 1346 { 1347 return !bdev_zone_is_seq(rtg_mount(rtg)->m_rtdev_targp->bt_bdev, 1348 xfs_gbno_to_daddr(rtg_group(rtg), 0)); 1349 } 1350 1351 static struct xfs_open_zone * 1352 xfs_find_fullest_conventional_open_zone( 1353 struct xfs_mount *mp) 1354 { 1355 struct xfs_zone_info *zi = mp->m_zone_info; 1356 struct xfs_open_zone *found = NULL, *oz; 1357 1358 spin_lock(&zi->zi_open_zones_lock); 1359 list_for_each_entry(oz, &zi->zi_open_zones, oz_entry) { 1360 if (!xfs_zone_is_conv(oz->oz_rtg)) 1361 continue; 1362 if (!found || oz->oz_allocated > found->oz_allocated) 1363 found = oz; 1364 } 1365 spin_unlock(&zi->zi_open_zones_lock); 1366 1367 return found; 1368 } 1369 1370 /* 1371 * Find the fullest conventional zones and remove them from the open zone pool 1372 * until we are at the open zone limit. 1373 * 1374 * We can end up with spurious "open" zones when the last blocks in a fully 1375 * written zone were invalidate as there is no write pointer for conventional 1376 * zones. 1377 * 1378 * If we are still over the limit when there is no conventional open zone left, 1379 * the user overrode the max open zones limit using the max_open_zones mount 1380 * option we should fail. 1381 */ 1382 static int 1383 xfs_finish_spurious_open_zones( 1384 struct xfs_mount *mp, 1385 struct xfs_init_zones *iz) 1386 { 1387 struct xfs_zone_info *zi = mp->m_zone_info; 1388 1389 while (zi->zi_nr_open_zones > mp->m_max_open_zones) { 1390 struct xfs_open_zone *oz; 1391 xfs_filblks_t adjust; 1392 1393 oz = xfs_find_fullest_conventional_open_zone(mp); 1394 if (!oz) { 1395 xfs_err(mp, 1396 "too many open zones for max_open_zones limit (%u/%u)", 1397 zi->zi_nr_open_zones, mp->m_max_open_zones); 1398 return -EINVAL; 1399 } 1400 1401 xfs_rtgroup_lock(oz->oz_rtg, XFS_RTGLOCK_RMAP); 1402 adjust = rtg_blocks(oz->oz_rtg) - oz->oz_written; 1403 trace_xfs_zone_spurious_open(oz, oz->oz_written, adjust); 1404 oz->oz_written = rtg_blocks(oz->oz_rtg); 1405 xfs_open_zone_mark_full(oz); 1406 xfs_rtgroup_unlock(oz->oz_rtg, XFS_RTGLOCK_RMAP); 1407 iz->available -= adjust; 1408 iz->reclaimable += adjust; 1409 } 1410 1411 return 0; 1412 } 1413 1414 int 1415 xfs_mount_zones( 1416 struct xfs_mount *mp) 1417 { 1418 struct xfs_init_zones iz = { 1419 .zone_capacity = mp->m_groups[XG_TYPE_RTG].blocks, 1420 .zone_size = xfs_rtgroup_raw_size(mp), 1421 }; 1422 int error; 1423 1424 if (!mp->m_rtdev_targp) { 1425 xfs_notice(mp, "RT device missing."); 1426 return -EINVAL; 1427 } 1428 1429 if (!xfs_has_rtgroups(mp) || !xfs_has_rmapbt(mp)) { 1430 xfs_notice(mp, "invalid flag combination."); 1431 return -EFSCORRUPTED; 1432 } 1433 if (mp->m_sb.sb_rextsize != 1) { 1434 xfs_notice(mp, "zoned file systems do not support rextsize."); 1435 return -EFSCORRUPTED; 1436 } 1437 if (mp->m_sb.sb_rgcount < XFS_MIN_ZONES) { 1438 xfs_notice(mp, 1439 "zoned file systems need to have at least %u zones.", XFS_MIN_ZONES); 1440 return -EFSCORRUPTED; 1441 } 1442 1443 error = xfs_calc_open_zones(mp); 1444 if (error) 1445 return error; 1446 1447 mp->m_zone_info = xfs_alloc_zone_info(mp); 1448 if (!mp->m_zone_info) 1449 return -ENOMEM; 1450 1451 error = xfs_report_zones(mp, &iz); 1452 if (error) 1453 goto out_free_zone_info; 1454 1455 error = xfs_finish_spurious_open_zones(mp, &iz); 1456 if (error) 1457 goto out_free_zone_info; 1458 1459 xfs_set_freecounter(mp, XC_FREE_RTAVAILABLE, iz.available); 1460 xfs_set_freecounter(mp, XC_FREE_RTEXTENTS, 1461 iz.available + iz.reclaimable); 1462 1463 /* 1464 * The writeback code switches between inodes regularly to provide 1465 * fairness. The default lower bound is 4MiB, but for zoned file 1466 * systems we want to increase that both to reduce seeks, but also more 1467 * importantly so that workloads that writes files in a multiple of the 1468 * zone size do not get fragmented and require garbage collection when 1469 * they shouldn't. Increase is to the zone size capped by the max 1470 * extent len. 1471 * 1472 * Note that because s_min_writeback_pages is a superblock field, this 1473 * value also get applied to non-zoned files on the data device if 1474 * there are any. On typical zoned setup all data is on the RT device 1475 * because using the more efficient sequential write required zones 1476 * is the reason for using the zone allocator, and either the RT device 1477 * and the (meta)data device are on the same block device, or the 1478 * (meta)data device is on a fast SSD while the data on the RT device 1479 * is on a SMR HDD. In any combination of the above cases enforcing 1480 * the higher min_writeback_pages for non-RT inodes is either a noop 1481 * or beneficial. 1482 */ 1483 mp->m_super->s_min_writeback_pages = 1484 XFS_FSB_TO_B(mp, min(iz.zone_capacity, XFS_MAX_BMBT_EXTLEN)) >> 1485 PAGE_SHIFT; 1486 1487 /* 1488 * The user may configure GC to free up a percentage of unused blocks. 1489 * By default this is 0. GC will always trigger at the minimum level 1490 * for keeping max_open_zones available for data placement. 1491 */ 1492 mp->m_zonegc_low_space = 0; 1493 1494 error = xfs_zone_gc_mount(mp); 1495 if (error) 1496 goto out_free_zone_info; 1497 1498 error = xfs_zoned_sysfs_init(mp); 1499 if (error) 1500 goto out_zone_gc_unmount; 1501 1502 xfs_info(mp, "%u zones of %u blocks (%u max open zones)", 1503 mp->m_sb.sb_rgcount, iz.zone_capacity, mp->m_max_open_zones); 1504 trace_xfs_zones_mount(mp); 1505 return 0; 1506 1507 out_zone_gc_unmount: 1508 xfs_zone_gc_unmount(mp); 1509 out_free_zone_info: 1510 xfs_free_zone_info(mp->m_zone_info); 1511 return error; 1512 } 1513 1514 void 1515 xfs_unmount_zones( 1516 struct xfs_mount *mp) 1517 { 1518 xfs_zoned_sysfs_del(mp); 1519 xfs_zone_gc_unmount(mp); 1520 xfs_free_zone_info(mp->m_zone_info); 1521 } 1522