arc.c revision 286764
1/* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21/* 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2012, Joyent, Inc. All rights reserved. 24 * Copyright (c) 2011, 2014 by Delphix. All rights reserved. 25 * Copyright (c) 2014 by Saso Kiselkov. All rights reserved. 26 * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 27 */ 28 29/* 30 * DVA-based Adjustable Replacement Cache 31 * 32 * While much of the theory of operation used here is 33 * based on the self-tuning, low overhead replacement cache 34 * presented by Megiddo and Modha at FAST 2003, there are some 35 * significant differences: 36 * 37 * 1. The Megiddo and Modha model assumes any page is evictable. 38 * Pages in its cache cannot be "locked" into memory. This makes 39 * the eviction algorithm simple: evict the last page in the list. 40 * This also make the performance characteristics easy to reason 41 * about. Our cache is not so simple. At any given moment, some 42 * subset of the blocks in the cache are un-evictable because we 43 * have handed out a reference to them. Blocks are only evictable 44 * when there are no external references active. This makes 45 * eviction far more problematic: we choose to evict the evictable 46 * blocks that are the "lowest" in the list. 47 * 48 * There are times when it is not possible to evict the requested 49 * space. In these circumstances we are unable to adjust the cache 50 * size. To prevent the cache growing unbounded at these times we 51 * implement a "cache throttle" that slows the flow of new data 52 * into the cache until we can make space available. 53 * 54 * 2. The Megiddo and Modha model assumes a fixed cache size. 55 * Pages are evicted when the cache is full and there is a cache 56 * miss. Our model has a variable sized cache. It grows with 57 * high use, but also tries to react to memory pressure from the 58 * operating system: decreasing its size when system memory is 59 * tight. 60 * 61 * 3. The Megiddo and Modha model assumes a fixed page size. All 62 * elements of the cache are therefore exactly the same size. So 63 * when adjusting the cache size following a cache miss, its simply 64 * a matter of choosing a single page to evict. In our model, we 65 * have variable sized cache blocks (rangeing from 512 bytes to 66 * 128K bytes). We therefore choose a set of blocks to evict to make 67 * space for a cache miss that approximates as closely as possible 68 * the space used by the new block. 69 * 70 * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache" 71 * by N. Megiddo & D. Modha, FAST 2003 72 */ 73 74/* 75 * The locking model: 76 * 77 * A new reference to a cache buffer can be obtained in two 78 * ways: 1) via a hash table lookup using the DVA as a key, 79 * or 2) via one of the ARC lists. The arc_read() interface 80 * uses method 1, while the internal arc algorithms for 81 * adjusting the cache use method 2. We therefore provide two 82 * types of locks: 1) the hash table lock array, and 2) the 83 * arc list locks. 84 * 85 * Buffers do not have their own mutexs, rather they rely on the 86 * hash table mutexs for the bulk of their protection (i.e. most 87 * fields in the arc_buf_hdr_t are protected by these mutexs). 88 * 89 * buf_hash_find() returns the appropriate mutex (held) when it 90 * locates the requested buffer in the hash table. It returns 91 * NULL for the mutex if the buffer was not in the table. 92 * 93 * buf_hash_remove() expects the appropriate hash mutex to be 94 * already held before it is invoked. 95 * 96 * Each arc state also has a mutex which is used to protect the 97 * buffer list associated with the state. When attempting to 98 * obtain a hash table lock while holding an arc list lock you 99 * must use: mutex_tryenter() to avoid deadlock. Also note that 100 * the active state mutex must be held before the ghost state mutex. 101 * 102 * Arc buffers may have an associated eviction callback function. 103 * This function will be invoked prior to removing the buffer (e.g. 104 * in arc_do_user_evicts()). Note however that the data associated 105 * with the buffer may be evicted prior to the callback. The callback 106 * must be made with *no locks held* (to prevent deadlock). Additionally, 107 * the users of callbacks must ensure that their private data is 108 * protected from simultaneous callbacks from arc_clear_callback() 109 * and arc_do_user_evicts(). 110 * 111 * Note that the majority of the performance stats are manipulated 112 * with atomic operations. 113 * 114 * The L2ARC uses the l2ad_mtx on each vdev for the following: 115 * 116 * - L2ARC buflist creation 117 * - L2ARC buflist eviction 118 * - L2ARC write completion, which walks L2ARC buflists 119 * - ARC header destruction, as it removes from L2ARC buflists 120 * - ARC header release, as it removes from L2ARC buflists 121 */ 122 123#include <sys/spa.h> 124#include <sys/zio.h> 125#include <sys/zio_compress.h> 126#include <sys/zfs_context.h> 127#include <sys/arc.h> 128#include <sys/refcount.h> 129#include <sys/vdev.h> 130#include <sys/vdev_impl.h> 131#include <sys/dsl_pool.h> 132#include <sys/multilist.h> 133#ifdef _KERNEL 134#include <sys/dnlc.h> 135#endif 136#include <sys/callb.h> 137#include <sys/kstat.h> 138#include <sys/trim_map.h> 139#include <zfs_fletcher.h> 140#include <sys/sdt.h> 141 142#include <vm/vm_pageout.h> 143#include <machine/vmparam.h> 144 145#ifdef illumos 146#ifndef _KERNEL 147/* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */ 148boolean_t arc_watch = B_FALSE; 149int arc_procfd; 150#endif 151#endif /* illumos */ 152 153static kmutex_t arc_reclaim_lock; 154static kcondvar_t arc_reclaim_thread_cv; 155static boolean_t arc_reclaim_thread_exit; 156static kcondvar_t arc_reclaim_waiters_cv; 157 158static kmutex_t arc_user_evicts_lock; 159static kcondvar_t arc_user_evicts_cv; 160static boolean_t arc_user_evicts_thread_exit; 161 162uint_t arc_reduce_dnlc_percent = 3; 163 164/* 165 * The number of headers to evict in arc_evict_state_impl() before 166 * dropping the sublist lock and evicting from another sublist. A lower 167 * value means we're more likely to evict the "correct" header (i.e. the 168 * oldest header in the arc state), but comes with higher overhead 169 * (i.e. more invocations of arc_evict_state_impl()). 170 */ 171int zfs_arc_evict_batch_limit = 10; 172 173/* 174 * The number of sublists used for each of the arc state lists. If this 175 * is not set to a suitable value by the user, it will be configured to 176 * the number of CPUs on the system in arc_init(). 177 */ 178int zfs_arc_num_sublists_per_state = 0; 179 180/* number of seconds before growing cache again */ 181static int arc_grow_retry = 60; 182 183/* shift of arc_c for calculating overflow limit in arc_get_data_buf */ 184int zfs_arc_overflow_shift = 8; 185 186/* shift of arc_c for calculating both min and max arc_p */ 187static int arc_p_min_shift = 4; 188 189/* log2(fraction of arc to reclaim) */ 190static int arc_shrink_shift = 7; 191 192/* 193 * log2(fraction of ARC which must be free to allow growing). 194 * I.e. If there is less than arc_c >> arc_no_grow_shift free memory, 195 * when reading a new block into the ARC, we will evict an equal-sized block 196 * from the ARC. 197 * 198 * This must be less than arc_shrink_shift, so that when we shrink the ARC, 199 * we will still not allow it to grow. 200 */ 201int arc_no_grow_shift = 5; 202 203 204/* 205 * minimum lifespan of a prefetch block in clock ticks 206 * (initialized in arc_init()) 207 */ 208static int arc_min_prefetch_lifespan; 209 210/* 211 * If this percent of memory is free, don't throttle. 212 */ 213int arc_lotsfree_percent = 10; 214 215static int arc_dead; 216extern int zfs_prefetch_disable; 217 218/* 219 * The arc has filled available memory and has now warmed up. 220 */ 221static boolean_t arc_warm; 222 223/* 224 * These tunables are for performance analysis. 225 */ 226uint64_t zfs_arc_max; 227uint64_t zfs_arc_min; 228uint64_t zfs_arc_meta_limit = 0; 229uint64_t zfs_arc_meta_min = 0; 230int zfs_arc_grow_retry = 0; 231int zfs_arc_shrink_shift = 0; 232int zfs_arc_p_min_shift = 0; 233int zfs_disable_dup_eviction = 0; 234uint64_t zfs_arc_average_blocksize = 8 * 1024; /* 8KB */ 235u_int zfs_arc_free_target = 0; 236 237static int sysctl_vfs_zfs_arc_free_target(SYSCTL_HANDLER_ARGS); 238static int sysctl_vfs_zfs_arc_meta_limit(SYSCTL_HANDLER_ARGS); 239 240#ifdef _KERNEL 241static void 242arc_free_target_init(void *unused __unused) 243{ 244 245 zfs_arc_free_target = vm_pageout_wakeup_thresh; 246} 247SYSINIT(arc_free_target_init, SI_SUB_KTHREAD_PAGE, SI_ORDER_ANY, 248 arc_free_target_init, NULL); 249 250TUNABLE_QUAD("vfs.zfs.arc_meta_limit", &zfs_arc_meta_limit); 251TUNABLE_QUAD("vfs.zfs.arc_meta_min", &zfs_arc_meta_min); 252TUNABLE_INT("vfs.zfs.arc_shrink_shift", &zfs_arc_shrink_shift); 253SYSCTL_DECL(_vfs_zfs); 254SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_max, CTLFLAG_RDTUN, &zfs_arc_max, 0, 255 "Maximum ARC size"); 256SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_min, CTLFLAG_RDTUN, &zfs_arc_min, 0, 257 "Minimum ARC size"); 258SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_average_blocksize, CTLFLAG_RDTUN, 259 &zfs_arc_average_blocksize, 0, 260 "ARC average blocksize"); 261SYSCTL_INT(_vfs_zfs, OID_AUTO, arc_shrink_shift, CTLFLAG_RW, 262 &arc_shrink_shift, 0, 263 "log2(fraction of arc to reclaim)"); 264 265/* 266 * We don't have a tunable for arc_free_target due to the dependency on 267 * pagedaemon initialisation. 268 */ 269SYSCTL_PROC(_vfs_zfs, OID_AUTO, arc_free_target, 270 CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(u_int), 271 sysctl_vfs_zfs_arc_free_target, "IU", 272 "Desired number of free pages below which ARC triggers reclaim"); 273 274static int 275sysctl_vfs_zfs_arc_free_target(SYSCTL_HANDLER_ARGS) 276{ 277 u_int val; 278 int err; 279 280 val = zfs_arc_free_target; 281 err = sysctl_handle_int(oidp, &val, 0, req); 282 if (err != 0 || req->newptr == NULL) 283 return (err); 284 285 if (val < minfree) 286 return (EINVAL); 287 if (val > vm_cnt.v_page_count) 288 return (EINVAL); 289 290 zfs_arc_free_target = val; 291 292 return (0); 293} 294 295/* 296 * Must be declared here, before the definition of corresponding kstat 297 * macro which uses the same names will confuse the compiler. 298 */ 299SYSCTL_PROC(_vfs_zfs, OID_AUTO, arc_meta_limit, 300 CTLTYPE_U64 | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(uint64_t), 301 sysctl_vfs_zfs_arc_meta_limit, "QU", 302 "ARC metadata limit"); 303#endif 304 305/* 306 * Note that buffers can be in one of 6 states: 307 * ARC_anon - anonymous (discussed below) 308 * ARC_mru - recently used, currently cached 309 * ARC_mru_ghost - recentely used, no longer in cache 310 * ARC_mfu - frequently used, currently cached 311 * ARC_mfu_ghost - frequently used, no longer in cache 312 * ARC_l2c_only - exists in L2ARC but not other states 313 * When there are no active references to the buffer, they are 314 * are linked onto a list in one of these arc states. These are 315 * the only buffers that can be evicted or deleted. Within each 316 * state there are multiple lists, one for meta-data and one for 317 * non-meta-data. Meta-data (indirect blocks, blocks of dnodes, 318 * etc.) is tracked separately so that it can be managed more 319 * explicitly: favored over data, limited explicitly. 320 * 321 * Anonymous buffers are buffers that are not associated with 322 * a DVA. These are buffers that hold dirty block copies 323 * before they are written to stable storage. By definition, 324 * they are "ref'd" and are considered part of arc_mru 325 * that cannot be freed. Generally, they will aquire a DVA 326 * as they are written and migrate onto the arc_mru list. 327 * 328 * The ARC_l2c_only state is for buffers that are in the second 329 * level ARC but no longer in any of the ARC_m* lists. The second 330 * level ARC itself may also contain buffers that are in any of 331 * the ARC_m* states - meaning that a buffer can exist in two 332 * places. The reason for the ARC_l2c_only state is to keep the 333 * buffer header in the hash table, so that reads that hit the 334 * second level ARC benefit from these fast lookups. 335 */ 336 337typedef struct arc_state { 338 /* 339 * list of evictable buffers 340 */ 341 multilist_t arcs_list[ARC_BUFC_NUMTYPES]; 342 /* 343 * total amount of evictable data in this state 344 */ 345 uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; 346 /* 347 * total amount of data in this state; this includes: evictable, 348 * non-evictable, ARC_BUFC_DATA, and ARC_BUFC_METADATA. 349 */ 350 uint64_t arcs_size; 351} arc_state_t; 352 353/* The 6 states: */ 354static arc_state_t ARC_anon; 355static arc_state_t ARC_mru; 356static arc_state_t ARC_mru_ghost; 357static arc_state_t ARC_mfu; 358static arc_state_t ARC_mfu_ghost; 359static arc_state_t ARC_l2c_only; 360 361typedef struct arc_stats { 362 kstat_named_t arcstat_hits; 363 kstat_named_t arcstat_misses; 364 kstat_named_t arcstat_demand_data_hits; 365 kstat_named_t arcstat_demand_data_misses; 366 kstat_named_t arcstat_demand_metadata_hits; 367 kstat_named_t arcstat_demand_metadata_misses; 368 kstat_named_t arcstat_prefetch_data_hits; 369 kstat_named_t arcstat_prefetch_data_misses; 370 kstat_named_t arcstat_prefetch_metadata_hits; 371 kstat_named_t arcstat_prefetch_metadata_misses; 372 kstat_named_t arcstat_mru_hits; 373 kstat_named_t arcstat_mru_ghost_hits; 374 kstat_named_t arcstat_mfu_hits; 375 kstat_named_t arcstat_mfu_ghost_hits; 376 kstat_named_t arcstat_allocated; 377 kstat_named_t arcstat_deleted; 378 /* 379 * Number of buffers that could not be evicted because the hash lock 380 * was held by another thread. The lock may not necessarily be held 381 * by something using the same buffer, since hash locks are shared 382 * by multiple buffers. 383 */ 384 kstat_named_t arcstat_mutex_miss; 385 /* 386 * Number of buffers skipped because they have I/O in progress, are 387 * indrect prefetch buffers that have not lived long enough, or are 388 * not from the spa we're trying to evict from. 389 */ 390 kstat_named_t arcstat_evict_skip; 391 /* 392 * Number of times arc_evict_state() was unable to evict enough 393 * buffers to reach it's target amount. 394 */ 395 kstat_named_t arcstat_evict_not_enough; 396 kstat_named_t arcstat_evict_l2_cached; 397 kstat_named_t arcstat_evict_l2_eligible; 398 kstat_named_t arcstat_evict_l2_ineligible; 399 kstat_named_t arcstat_evict_l2_skip; 400 kstat_named_t arcstat_hash_elements; 401 kstat_named_t arcstat_hash_elements_max; 402 kstat_named_t arcstat_hash_collisions; 403 kstat_named_t arcstat_hash_chains; 404 kstat_named_t arcstat_hash_chain_max; 405 kstat_named_t arcstat_p; 406 kstat_named_t arcstat_c; 407 kstat_named_t arcstat_c_min; 408 kstat_named_t arcstat_c_max; 409 kstat_named_t arcstat_size; 410 /* 411 * Number of bytes consumed by internal ARC structures necessary 412 * for tracking purposes; these structures are not actually 413 * backed by ARC buffers. This includes arc_buf_hdr_t structures 414 * (allocated via arc_buf_hdr_t_full and arc_buf_hdr_t_l2only 415 * caches), and arc_buf_t structures (allocated via arc_buf_t 416 * cache). 417 */ 418 kstat_named_t arcstat_hdr_size; 419 /* 420 * Number of bytes consumed by ARC buffers of type equal to 421 * ARC_BUFC_DATA. This is generally consumed by buffers backing 422 * on disk user data (e.g. plain file contents). 423 */ 424 kstat_named_t arcstat_data_size; 425 /* 426 * Number of bytes consumed by ARC buffers of type equal to 427 * ARC_BUFC_METADATA. This is generally consumed by buffers 428 * backing on disk data that is used for internal ZFS 429 * structures (e.g. ZAP, dnode, indirect blocks, etc). 430 */ 431 kstat_named_t arcstat_metadata_size; 432 /* 433 * Number of bytes consumed by various buffers and structures 434 * not actually backed with ARC buffers. This includes bonus 435 * buffers (allocated directly via zio_buf_* functions), 436 * dmu_buf_impl_t structures (allocated via dmu_buf_impl_t 437 * cache), and dnode_t structures (allocated via dnode_t cache). 438 */ 439 kstat_named_t arcstat_other_size; 440 /* 441 * Total number of bytes consumed by ARC buffers residing in the 442 * arc_anon state. This includes *all* buffers in the arc_anon 443 * state; e.g. data, metadata, evictable, and unevictable buffers 444 * are all included in this value. 445 */ 446 kstat_named_t arcstat_anon_size; 447 /* 448 * Number of bytes consumed by ARC buffers that meet the 449 * following criteria: backing buffers of type ARC_BUFC_DATA, 450 * residing in the arc_anon state, and are eligible for eviction 451 * (e.g. have no outstanding holds on the buffer). 452 */ 453 kstat_named_t arcstat_anon_evictable_data; 454 /* 455 * Number of bytes consumed by ARC buffers that meet the 456 * following criteria: backing buffers of type ARC_BUFC_METADATA, 457 * residing in the arc_anon state, and are eligible for eviction 458 * (e.g. have no outstanding holds on the buffer). 459 */ 460 kstat_named_t arcstat_anon_evictable_metadata; 461 /* 462 * Total number of bytes consumed by ARC buffers residing in the 463 * arc_mru state. This includes *all* buffers in the arc_mru 464 * state; e.g. data, metadata, evictable, and unevictable buffers 465 * are all included in this value. 466 */ 467 kstat_named_t arcstat_mru_size; 468 /* 469 * Number of bytes consumed by ARC buffers that meet the 470 * following criteria: backing buffers of type ARC_BUFC_DATA, 471 * residing in the arc_mru state, and are eligible for eviction 472 * (e.g. have no outstanding holds on the buffer). 473 */ 474 kstat_named_t arcstat_mru_evictable_data; 475 /* 476 * Number of bytes consumed by ARC buffers that meet the 477 * following criteria: backing buffers of type ARC_BUFC_METADATA, 478 * residing in the arc_mru state, and are eligible for eviction 479 * (e.g. have no outstanding holds on the buffer). 480 */ 481 kstat_named_t arcstat_mru_evictable_metadata; 482 /* 483 * Total number of bytes that *would have been* consumed by ARC 484 * buffers in the arc_mru_ghost state. The key thing to note 485 * here, is the fact that this size doesn't actually indicate 486 * RAM consumption. The ghost lists only consist of headers and 487 * don't actually have ARC buffers linked off of these headers. 488 * Thus, *if* the headers had associated ARC buffers, these 489 * buffers *would have* consumed this number of bytes. 490 */ 491 kstat_named_t arcstat_mru_ghost_size; 492 /* 493 * Number of bytes that *would have been* consumed by ARC 494 * buffers that are eligible for eviction, of type 495 * ARC_BUFC_DATA, and linked off the arc_mru_ghost state. 496 */ 497 kstat_named_t arcstat_mru_ghost_evictable_data; 498 /* 499 * Number of bytes that *would have been* consumed by ARC 500 * buffers that are eligible for eviction, of type 501 * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state. 502 */ 503 kstat_named_t arcstat_mru_ghost_evictable_metadata; 504 /* 505 * Total number of bytes consumed by ARC buffers residing in the 506 * arc_mfu state. This includes *all* buffers in the arc_mfu 507 * state; e.g. data, metadata, evictable, and unevictable buffers 508 * are all included in this value. 509 */ 510 kstat_named_t arcstat_mfu_size; 511 /* 512 * Number of bytes consumed by ARC buffers that are eligible for 513 * eviction, of type ARC_BUFC_DATA, and reside in the arc_mfu 514 * state. 515 */ 516 kstat_named_t arcstat_mfu_evictable_data; 517 /* 518 * Number of bytes consumed by ARC buffers that are eligible for 519 * eviction, of type ARC_BUFC_METADATA, and reside in the 520 * arc_mfu state. 521 */ 522 kstat_named_t arcstat_mfu_evictable_metadata; 523 /* 524 * Total number of bytes that *would have been* consumed by ARC 525 * buffers in the arc_mfu_ghost state. See the comment above 526 * arcstat_mru_ghost_size for more details. 527 */ 528 kstat_named_t arcstat_mfu_ghost_size; 529 /* 530 * Number of bytes that *would have been* consumed by ARC 531 * buffers that are eligible for eviction, of type 532 * ARC_BUFC_DATA, and linked off the arc_mfu_ghost state. 533 */ 534 kstat_named_t arcstat_mfu_ghost_evictable_data; 535 /* 536 * Number of bytes that *would have been* consumed by ARC 537 * buffers that are eligible for eviction, of type 538 * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state. 539 */ 540 kstat_named_t arcstat_mfu_ghost_evictable_metadata; 541 kstat_named_t arcstat_l2_hits; 542 kstat_named_t arcstat_l2_misses; 543 kstat_named_t arcstat_l2_feeds; 544 kstat_named_t arcstat_l2_rw_clash; 545 kstat_named_t arcstat_l2_read_bytes; 546 kstat_named_t arcstat_l2_write_bytes; 547 kstat_named_t arcstat_l2_writes_sent; 548 kstat_named_t arcstat_l2_writes_done; 549 kstat_named_t arcstat_l2_writes_error; 550 kstat_named_t arcstat_l2_writes_lock_retry; 551 kstat_named_t arcstat_l2_evict_lock_retry; 552 kstat_named_t arcstat_l2_evict_reading; 553 kstat_named_t arcstat_l2_evict_l1cached; 554 kstat_named_t arcstat_l2_free_on_write; 555 kstat_named_t arcstat_l2_cdata_free_on_write; 556 kstat_named_t arcstat_l2_abort_lowmem; 557 kstat_named_t arcstat_l2_cksum_bad; 558 kstat_named_t arcstat_l2_io_error; 559 kstat_named_t arcstat_l2_size; 560 kstat_named_t arcstat_l2_asize; 561 kstat_named_t arcstat_l2_hdr_size; 562 kstat_named_t arcstat_l2_compress_successes; 563 kstat_named_t arcstat_l2_compress_zeros; 564 kstat_named_t arcstat_l2_compress_failures; 565 kstat_named_t arcstat_l2_write_trylock_fail; 566 kstat_named_t arcstat_l2_write_passed_headroom; 567 kstat_named_t arcstat_l2_write_spa_mismatch; 568 kstat_named_t arcstat_l2_write_in_l2; 569 kstat_named_t arcstat_l2_write_hdr_io_in_progress; 570 kstat_named_t arcstat_l2_write_not_cacheable; 571 kstat_named_t arcstat_l2_write_full; 572 kstat_named_t arcstat_l2_write_buffer_iter; 573 kstat_named_t arcstat_l2_write_pios; 574 kstat_named_t arcstat_l2_write_buffer_bytes_scanned; 575 kstat_named_t arcstat_l2_write_buffer_list_iter; 576 kstat_named_t arcstat_l2_write_buffer_list_null_iter; 577 kstat_named_t arcstat_memory_throttle_count; 578 kstat_named_t arcstat_duplicate_buffers; 579 kstat_named_t arcstat_duplicate_buffers_size; 580 kstat_named_t arcstat_duplicate_reads; 581 kstat_named_t arcstat_meta_used; 582 kstat_named_t arcstat_meta_limit; 583 kstat_named_t arcstat_meta_max; 584 kstat_named_t arcstat_meta_min; 585} arc_stats_t; 586 587static arc_stats_t arc_stats = { 588 { "hits", KSTAT_DATA_UINT64 }, 589 { "misses", KSTAT_DATA_UINT64 }, 590 { "demand_data_hits", KSTAT_DATA_UINT64 }, 591 { "demand_data_misses", KSTAT_DATA_UINT64 }, 592 { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 593 { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 594 { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 595 { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 596 { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 597 { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 598 { "mru_hits", KSTAT_DATA_UINT64 }, 599 { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 600 { "mfu_hits", KSTAT_DATA_UINT64 }, 601 { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 602 { "allocated", KSTAT_DATA_UINT64 }, 603 { "deleted", KSTAT_DATA_UINT64 }, 604 { "mutex_miss", KSTAT_DATA_UINT64 }, 605 { "evict_skip", KSTAT_DATA_UINT64 }, 606 { "evict_not_enough", KSTAT_DATA_UINT64 }, 607 { "evict_l2_cached", KSTAT_DATA_UINT64 }, 608 { "evict_l2_eligible", KSTAT_DATA_UINT64 }, 609 { "evict_l2_ineligible", KSTAT_DATA_UINT64 }, 610 { "evict_l2_skip", KSTAT_DATA_UINT64 }, 611 { "hash_elements", KSTAT_DATA_UINT64 }, 612 { "hash_elements_max", KSTAT_DATA_UINT64 }, 613 { "hash_collisions", KSTAT_DATA_UINT64 }, 614 { "hash_chains", KSTAT_DATA_UINT64 }, 615 { "hash_chain_max", KSTAT_DATA_UINT64 }, 616 { "p", KSTAT_DATA_UINT64 }, 617 { "c", KSTAT_DATA_UINT64 }, 618 { "c_min", KSTAT_DATA_UINT64 }, 619 { "c_max", KSTAT_DATA_UINT64 }, 620 { "size", KSTAT_DATA_UINT64 }, 621 { "hdr_size", KSTAT_DATA_UINT64 }, 622 { "data_size", KSTAT_DATA_UINT64 }, 623 { "metadata_size", KSTAT_DATA_UINT64 }, 624 { "other_size", KSTAT_DATA_UINT64 }, 625 { "anon_size", KSTAT_DATA_UINT64 }, 626 { "anon_evictable_data", KSTAT_DATA_UINT64 }, 627 { "anon_evictable_metadata", KSTAT_DATA_UINT64 }, 628 { "mru_size", KSTAT_DATA_UINT64 }, 629 { "mru_evictable_data", KSTAT_DATA_UINT64 }, 630 { "mru_evictable_metadata", KSTAT_DATA_UINT64 }, 631 { "mru_ghost_size", KSTAT_DATA_UINT64 }, 632 { "mru_ghost_evictable_data", KSTAT_DATA_UINT64 }, 633 { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, 634 { "mfu_size", KSTAT_DATA_UINT64 }, 635 { "mfu_evictable_data", KSTAT_DATA_UINT64 }, 636 { "mfu_evictable_metadata", KSTAT_DATA_UINT64 }, 637 { "mfu_ghost_size", KSTAT_DATA_UINT64 }, 638 { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64 }, 639 { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, 640 { "l2_hits", KSTAT_DATA_UINT64 }, 641 { "l2_misses", KSTAT_DATA_UINT64 }, 642 { "l2_feeds", KSTAT_DATA_UINT64 }, 643 { "l2_rw_clash", KSTAT_DATA_UINT64 }, 644 { "l2_read_bytes", KSTAT_DATA_UINT64 }, 645 { "l2_write_bytes", KSTAT_DATA_UINT64 }, 646 { "l2_writes_sent", KSTAT_DATA_UINT64 }, 647 { "l2_writes_done", KSTAT_DATA_UINT64 }, 648 { "l2_writes_error", KSTAT_DATA_UINT64 }, 649 { "l2_writes_lock_retry", KSTAT_DATA_UINT64 }, 650 { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 651 { "l2_evict_reading", KSTAT_DATA_UINT64 }, 652 { "l2_evict_l1cached", KSTAT_DATA_UINT64 }, 653 { "l2_free_on_write", KSTAT_DATA_UINT64 }, 654 { "l2_cdata_free_on_write", KSTAT_DATA_UINT64 }, 655 { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 656 { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 657 { "l2_io_error", KSTAT_DATA_UINT64 }, 658 { "l2_size", KSTAT_DATA_UINT64 }, 659 { "l2_asize", KSTAT_DATA_UINT64 }, 660 { "l2_hdr_size", KSTAT_DATA_UINT64 }, 661 { "l2_compress_successes", KSTAT_DATA_UINT64 }, 662 { "l2_compress_zeros", KSTAT_DATA_UINT64 }, 663 { "l2_compress_failures", KSTAT_DATA_UINT64 }, 664 { "l2_write_trylock_fail", KSTAT_DATA_UINT64 }, 665 { "l2_write_passed_headroom", KSTAT_DATA_UINT64 }, 666 { "l2_write_spa_mismatch", KSTAT_DATA_UINT64 }, 667 { "l2_write_in_l2", KSTAT_DATA_UINT64 }, 668 { "l2_write_io_in_progress", KSTAT_DATA_UINT64 }, 669 { "l2_write_not_cacheable", KSTAT_DATA_UINT64 }, 670 { "l2_write_full", KSTAT_DATA_UINT64 }, 671 { "l2_write_buffer_iter", KSTAT_DATA_UINT64 }, 672 { "l2_write_pios", KSTAT_DATA_UINT64 }, 673 { "l2_write_buffer_bytes_scanned", KSTAT_DATA_UINT64 }, 674 { "l2_write_buffer_list_iter", KSTAT_DATA_UINT64 }, 675 { "l2_write_buffer_list_null_iter", KSTAT_DATA_UINT64 }, 676 { "memory_throttle_count", KSTAT_DATA_UINT64 }, 677 { "duplicate_buffers", KSTAT_DATA_UINT64 }, 678 { "duplicate_buffers_size", KSTAT_DATA_UINT64 }, 679 { "duplicate_reads", KSTAT_DATA_UINT64 }, 680 { "arc_meta_used", KSTAT_DATA_UINT64 }, 681 { "arc_meta_limit", KSTAT_DATA_UINT64 }, 682 { "arc_meta_max", KSTAT_DATA_UINT64 }, 683 { "arc_meta_min", KSTAT_DATA_UINT64 } 684}; 685 686#define ARCSTAT(stat) (arc_stats.stat.value.ui64) 687 688#define ARCSTAT_INCR(stat, val) \ 689 atomic_add_64(&arc_stats.stat.value.ui64, (val)) 690 691#define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 692#define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 693 694#define ARCSTAT_MAX(stat, val) { \ 695 uint64_t m; \ 696 while ((val) > (m = arc_stats.stat.value.ui64) && \ 697 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 698 continue; \ 699} 700 701#define ARCSTAT_MAXSTAT(stat) \ 702 ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 703 704/* 705 * We define a macro to allow ARC hits/misses to be easily broken down by 706 * two separate conditions, giving a total of four different subtypes for 707 * each of hits and misses (so eight statistics total). 708 */ 709#define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 710 if (cond1) { \ 711 if (cond2) { \ 712 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 713 } else { \ 714 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 715 } \ 716 } else { \ 717 if (cond2) { \ 718 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 719 } else { \ 720 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 721 } \ 722 } 723 724kstat_t *arc_ksp; 725static arc_state_t *arc_anon; 726static arc_state_t *arc_mru; 727static arc_state_t *arc_mru_ghost; 728static arc_state_t *arc_mfu; 729static arc_state_t *arc_mfu_ghost; 730static arc_state_t *arc_l2c_only; 731 732/* 733 * There are several ARC variables that are critical to export as kstats -- 734 * but we don't want to have to grovel around in the kstat whenever we wish to 735 * manipulate them. For these variables, we therefore define them to be in 736 * terms of the statistic variable. This assures that we are not introducing 737 * the possibility of inconsistency by having shadow copies of the variables, 738 * while still allowing the code to be readable. 739 */ 740#define arc_size ARCSTAT(arcstat_size) /* actual total arc size */ 741#define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 742#define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 743#define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 744#define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 745#define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */ 746#define arc_meta_min ARCSTAT(arcstat_meta_min) /* min size for metadata */ 747#define arc_meta_used ARCSTAT(arcstat_meta_used) /* size of metadata */ 748#define arc_meta_max ARCSTAT(arcstat_meta_max) /* max size of metadata */ 749 750#define L2ARC_IS_VALID_COMPRESS(_c_) \ 751 ((_c_) == ZIO_COMPRESS_LZ4 || (_c_) == ZIO_COMPRESS_EMPTY) 752 753static int arc_no_grow; /* Don't try to grow cache size */ 754static uint64_t arc_tempreserve; 755static uint64_t arc_loaned_bytes; 756 757typedef struct arc_callback arc_callback_t; 758 759struct arc_callback { 760 void *acb_private; 761 arc_done_func_t *acb_done; 762 arc_buf_t *acb_buf; 763 zio_t *acb_zio_dummy; 764 arc_callback_t *acb_next; 765}; 766 767typedef struct arc_write_callback arc_write_callback_t; 768 769struct arc_write_callback { 770 void *awcb_private; 771 arc_done_func_t *awcb_ready; 772 arc_done_func_t *awcb_physdone; 773 arc_done_func_t *awcb_done; 774 arc_buf_t *awcb_buf; 775}; 776 777/* 778 * ARC buffers are separated into multiple structs as a memory saving measure: 779 * - Common fields struct, always defined, and embedded within it: 780 * - L2-only fields, always allocated but undefined when not in L2ARC 781 * - L1-only fields, only allocated when in L1ARC 782 * 783 * Buffer in L1 Buffer only in L2 784 * +------------------------+ +------------------------+ 785 * | arc_buf_hdr_t | | arc_buf_hdr_t | 786 * | | | | 787 * | | | | 788 * | | | | 789 * +------------------------+ +------------------------+ 790 * | l2arc_buf_hdr_t | | l2arc_buf_hdr_t | 791 * | (undefined if L1-only) | | | 792 * +------------------------+ +------------------------+ 793 * | l1arc_buf_hdr_t | 794 * | | 795 * | | 796 * | | 797 * | | 798 * +------------------------+ 799 * 800 * Because it's possible for the L2ARC to become extremely large, we can wind 801 * up eating a lot of memory in L2ARC buffer headers, so the size of a header 802 * is minimized by only allocating the fields necessary for an L1-cached buffer 803 * when a header is actually in the L1 cache. The sub-headers (l1arc_buf_hdr and 804 * l2arc_buf_hdr) are embedded rather than allocated separately to save a couple 805 * words in pointers. arc_hdr_realloc() is used to switch a header between 806 * these two allocation states. 807 */ 808typedef struct l1arc_buf_hdr { 809 kmutex_t b_freeze_lock; 810#ifdef ZFS_DEBUG 811 /* 812 * used for debugging wtih kmem_flags - by allocating and freeing 813 * b_thawed when the buffer is thawed, we get a record of the stack 814 * trace that thawed it. 815 */ 816 void *b_thawed; 817#endif 818 819 arc_buf_t *b_buf; 820 uint32_t b_datacnt; 821 /* for waiting on writes to complete */ 822 kcondvar_t b_cv; 823 824 /* protected by arc state mutex */ 825 arc_state_t *b_state; 826 multilist_node_t b_arc_node; 827 828 /* updated atomically */ 829 clock_t b_arc_access; 830 831 /* self protecting */ 832 refcount_t b_refcnt; 833 834 arc_callback_t *b_acb; 835 /* temporary buffer holder for in-flight compressed data */ 836 void *b_tmp_cdata; 837} l1arc_buf_hdr_t; 838 839typedef struct l2arc_dev l2arc_dev_t; 840 841typedef struct l2arc_buf_hdr { 842 /* protected by arc_buf_hdr mutex */ 843 l2arc_dev_t *b_dev; /* L2ARC device */ 844 uint64_t b_daddr; /* disk address, offset byte */ 845 /* real alloc'd buffer size depending on b_compress applied */ 846 int32_t b_asize; 847 848 list_node_t b_l2node; 849} l2arc_buf_hdr_t; 850 851struct arc_buf_hdr { 852 /* protected by hash lock */ 853 dva_t b_dva; 854 uint64_t b_birth; 855 /* 856 * Even though this checksum is only set/verified when a buffer is in 857 * the L1 cache, it needs to be in the set of common fields because it 858 * must be preserved from the time before a buffer is written out to 859 * L2ARC until after it is read back in. 860 */ 861 zio_cksum_t *b_freeze_cksum; 862 863 arc_buf_hdr_t *b_hash_next; 864 arc_flags_t b_flags; 865 866 /* immutable */ 867 int32_t b_size; 868 uint64_t b_spa; 869 870 /* L2ARC fields. Undefined when not in L2ARC. */ 871 l2arc_buf_hdr_t b_l2hdr; 872 /* L1ARC fields. Undefined when in l2arc_only state */ 873 l1arc_buf_hdr_t b_l1hdr; 874}; 875 876#ifdef _KERNEL 877static int 878sysctl_vfs_zfs_arc_meta_limit(SYSCTL_HANDLER_ARGS) 879{ 880 uint64_t val; 881 int err; 882 883 val = arc_meta_limit; 884 err = sysctl_handle_64(oidp, &val, 0, req); 885 if (err != 0 || req->newptr == NULL) 886 return (err); 887 888 if (val <= 0 || val > arc_c_max) 889 return (EINVAL); 890 891 arc_meta_limit = val; 892 return (0); 893} 894#endif 895 896static arc_buf_t *arc_eviction_list; 897static arc_buf_hdr_t arc_eviction_hdr; 898 899#define GHOST_STATE(state) \ 900 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 901 (state) == arc_l2c_only) 902 903#define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE) 904#define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) 905#define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR) 906#define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH) 907#define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FLAG_FREED_IN_READ) 908#define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_FLAG_BUF_AVAILABLE) 909 910#define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE) 911#define HDR_L2COMPRESS(hdr) ((hdr)->b_flags & ARC_FLAG_L2COMPRESS) 912#define HDR_L2_READING(hdr) \ 913 (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \ 914 ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)) 915#define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING) 916#define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED) 917#define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD) 918 919#define HDR_ISTYPE_METADATA(hdr) \ 920 ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA) 921#define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr)) 922 923#define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR) 924#define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR) 925 926/* For storing compression mode in b_flags */ 927#define HDR_COMPRESS_OFFSET 24 928#define HDR_COMPRESS_NBITS 7 929 930#define HDR_GET_COMPRESS(hdr) ((enum zio_compress)BF32_GET(hdr->b_flags, \ 931 HDR_COMPRESS_OFFSET, HDR_COMPRESS_NBITS)) 932#define HDR_SET_COMPRESS(hdr, cmp) BF32_SET(hdr->b_flags, \ 933 HDR_COMPRESS_OFFSET, HDR_COMPRESS_NBITS, (cmp)) 934 935/* 936 * Other sizes 937 */ 938 939#define HDR_FULL_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 940#define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr)) 941 942/* 943 * Hash table routines 944 */ 945 946#define HT_LOCK_PAD CACHE_LINE_SIZE 947 948struct ht_lock { 949 kmutex_t ht_lock; 950#ifdef _KERNEL 951 unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 952#endif 953}; 954 955#define BUF_LOCKS 256 956typedef struct buf_hash_table { 957 uint64_t ht_mask; 958 arc_buf_hdr_t **ht_table; 959 struct ht_lock ht_locks[BUF_LOCKS] __aligned(CACHE_LINE_SIZE); 960} buf_hash_table_t; 961 962static buf_hash_table_t buf_hash_table; 963 964#define BUF_HASH_INDEX(spa, dva, birth) \ 965 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 966#define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 967#define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 968#define HDR_LOCK(hdr) \ 969 (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth))) 970 971uint64_t zfs_crc64_table[256]; 972 973/* 974 * Level 2 ARC 975 */ 976 977#define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 978#define L2ARC_HEADROOM 2 /* num of writes */ 979/* 980 * If we discover during ARC scan any buffers to be compressed, we boost 981 * our headroom for the next scanning cycle by this percentage multiple. 982 */ 983#define L2ARC_HEADROOM_BOOST 200 984#define L2ARC_FEED_SECS 1 /* caching interval secs */ 985#define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ 986 987/* 988 * Used to distinguish headers that are being process by 989 * l2arc_write_buffers(), but have yet to be assigned to a l2arc disk 990 * address. This can happen when the header is added to the l2arc's list 991 * of buffers to write in the first stage of l2arc_write_buffers(), but 992 * has not yet been written out which happens in the second stage of 993 * l2arc_write_buffers(). 994 */ 995#define L2ARC_ADDR_UNSET ((uint64_t)(-1)) 996 997#define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 998#define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 999 1000/* L2ARC Performance Tunables */ 1001uint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 1002uint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */ 1003uint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 1004uint64_t l2arc_headroom_boost = L2ARC_HEADROOM_BOOST; 1005uint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 1006uint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval milliseconds */ 1007boolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 1008boolean_t l2arc_feed_again = B_TRUE; /* turbo warmup */ 1009boolean_t l2arc_norw = B_TRUE; /* no reads during writes */ 1010 1011SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_max, CTLFLAG_RW, 1012 &l2arc_write_max, 0, "max write size"); 1013SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_boost, CTLFLAG_RW, 1014 &l2arc_write_boost, 0, "extra write during warmup"); 1015SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_headroom, CTLFLAG_RW, 1016 &l2arc_headroom, 0, "number of dev writes"); 1017SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_secs, CTLFLAG_RW, 1018 &l2arc_feed_secs, 0, "interval seconds"); 1019SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_min_ms, CTLFLAG_RW, 1020 &l2arc_feed_min_ms, 0, "min interval milliseconds"); 1021 1022SYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_noprefetch, CTLFLAG_RW, 1023 &l2arc_noprefetch, 0, "don't cache prefetch bufs"); 1024SYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_feed_again, CTLFLAG_RW, 1025 &l2arc_feed_again, 0, "turbo warmup"); 1026SYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_norw, CTLFLAG_RW, 1027 &l2arc_norw, 0, "no reads during writes"); 1028 1029SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_size, CTLFLAG_RD, 1030 &ARC_anon.arcs_size, 0, "size of anonymous state"); 1031SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_metadata_lsize, CTLFLAG_RD, 1032 &ARC_anon.arcs_lsize[ARC_BUFC_METADATA], 0, "size of anonymous state"); 1033SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_data_lsize, CTLFLAG_RD, 1034 &ARC_anon.arcs_lsize[ARC_BUFC_DATA], 0, "size of anonymous state"); 1035 1036SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_size, CTLFLAG_RD, 1037 &ARC_mru.arcs_size, 0, "size of mru state"); 1038SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_metadata_lsize, CTLFLAG_RD, 1039 &ARC_mru.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mru state"); 1040SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_data_lsize, CTLFLAG_RD, 1041 &ARC_mru.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mru state"); 1042 1043SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_size, CTLFLAG_RD, 1044 &ARC_mru_ghost.arcs_size, 0, "size of mru ghost state"); 1045SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_metadata_lsize, CTLFLAG_RD, 1046 &ARC_mru_ghost.arcs_lsize[ARC_BUFC_METADATA], 0, 1047 "size of metadata in mru ghost state"); 1048SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_data_lsize, CTLFLAG_RD, 1049 &ARC_mru_ghost.arcs_lsize[ARC_BUFC_DATA], 0, 1050 "size of data in mru ghost state"); 1051 1052SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_size, CTLFLAG_RD, 1053 &ARC_mfu.arcs_size, 0, "size of mfu state"); 1054SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_metadata_lsize, CTLFLAG_RD, 1055 &ARC_mfu.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mfu state"); 1056SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_data_lsize, CTLFLAG_RD, 1057 &ARC_mfu.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mfu state"); 1058 1059SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_size, CTLFLAG_RD, 1060 &ARC_mfu_ghost.arcs_size, 0, "size of mfu ghost state"); 1061SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_metadata_lsize, CTLFLAG_RD, 1062 &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_METADATA], 0, 1063 "size of metadata in mfu ghost state"); 1064SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_data_lsize, CTLFLAG_RD, 1065 &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_DATA], 0, 1066 "size of data in mfu ghost state"); 1067 1068SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2c_only_size, CTLFLAG_RD, 1069 &ARC_l2c_only.arcs_size, 0, "size of mru state"); 1070 1071/* 1072 * L2ARC Internals 1073 */ 1074struct l2arc_dev { 1075 vdev_t *l2ad_vdev; /* vdev */ 1076 spa_t *l2ad_spa; /* spa */ 1077 uint64_t l2ad_hand; /* next write location */ 1078 uint64_t l2ad_start; /* first addr on device */ 1079 uint64_t l2ad_end; /* last addr on device */ 1080 boolean_t l2ad_first; /* first sweep through */ 1081 boolean_t l2ad_writing; /* currently writing */ 1082 kmutex_t l2ad_mtx; /* lock for buffer list */ 1083 list_t l2ad_buflist; /* buffer list */ 1084 list_node_t l2ad_node; /* device list node */ 1085 refcount_t l2ad_alloc; /* allocated bytes */ 1086}; 1087 1088static list_t L2ARC_dev_list; /* device list */ 1089static list_t *l2arc_dev_list; /* device list pointer */ 1090static kmutex_t l2arc_dev_mtx; /* device list mutex */ 1091static l2arc_dev_t *l2arc_dev_last; /* last device used */ 1092static list_t L2ARC_free_on_write; /* free after write buf list */ 1093static list_t *l2arc_free_on_write; /* free after write list ptr */ 1094static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 1095static uint64_t l2arc_ndev; /* number of devices */ 1096 1097typedef struct l2arc_read_callback { 1098 arc_buf_t *l2rcb_buf; /* read buffer */ 1099 spa_t *l2rcb_spa; /* spa */ 1100 blkptr_t l2rcb_bp; /* original blkptr */ 1101 zbookmark_phys_t l2rcb_zb; /* original bookmark */ 1102 int l2rcb_flags; /* original flags */ 1103 enum zio_compress l2rcb_compress; /* applied compress */ 1104} l2arc_read_callback_t; 1105 1106typedef struct l2arc_write_callback { 1107 l2arc_dev_t *l2wcb_dev; /* device info */ 1108 arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 1109} l2arc_write_callback_t; 1110 1111typedef struct l2arc_data_free { 1112 /* protected by l2arc_free_on_write_mtx */ 1113 void *l2df_data; 1114 size_t l2df_size; 1115 void (*l2df_func)(void *, size_t); 1116 list_node_t l2df_list_node; 1117} l2arc_data_free_t; 1118 1119static kmutex_t l2arc_feed_thr_lock; 1120static kcondvar_t l2arc_feed_thr_cv; 1121static uint8_t l2arc_thread_exit; 1122 1123static void arc_get_data_buf(arc_buf_t *); 1124static void arc_access(arc_buf_hdr_t *, kmutex_t *); 1125static boolean_t arc_is_overflowing(); 1126static void arc_buf_watch(arc_buf_t *); 1127 1128static arc_buf_contents_t arc_buf_type(arc_buf_hdr_t *); 1129static uint32_t arc_bufc_to_flags(arc_buf_contents_t); 1130 1131static boolean_t l2arc_write_eligible(uint64_t, arc_buf_hdr_t *); 1132static void l2arc_read_done(zio_t *); 1133 1134static boolean_t l2arc_compress_buf(arc_buf_hdr_t *); 1135static void l2arc_decompress_zio(zio_t *, arc_buf_hdr_t *, enum zio_compress); 1136static void l2arc_release_cdata_buf(arc_buf_hdr_t *); 1137 1138static uint64_t 1139buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) 1140{ 1141 uint8_t *vdva = (uint8_t *)dva; 1142 uint64_t crc = -1ULL; 1143 int i; 1144 1145 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 1146 1147 for (i = 0; i < sizeof (dva_t); i++) 1148 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 1149 1150 crc ^= (spa>>8) ^ birth; 1151 1152 return (crc); 1153} 1154 1155#define BUF_EMPTY(buf) \ 1156 ((buf)->b_dva.dva_word[0] == 0 && \ 1157 (buf)->b_dva.dva_word[1] == 0) 1158 1159#define BUF_EQUAL(spa, dva, birth, buf) \ 1160 ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 1161 ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 1162 ((buf)->b_birth == birth) && ((buf)->b_spa == spa) 1163 1164static void 1165buf_discard_identity(arc_buf_hdr_t *hdr) 1166{ 1167 hdr->b_dva.dva_word[0] = 0; 1168 hdr->b_dva.dva_word[1] = 0; 1169 hdr->b_birth = 0; 1170} 1171 1172static arc_buf_hdr_t * 1173buf_hash_find(uint64_t spa, const blkptr_t *bp, kmutex_t **lockp) 1174{ 1175 const dva_t *dva = BP_IDENTITY(bp); 1176 uint64_t birth = BP_PHYSICAL_BIRTH(bp); 1177 uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 1178 kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 1179 arc_buf_hdr_t *hdr; 1180 1181 mutex_enter(hash_lock); 1182 for (hdr = buf_hash_table.ht_table[idx]; hdr != NULL; 1183 hdr = hdr->b_hash_next) { 1184 if (BUF_EQUAL(spa, dva, birth, hdr)) { 1185 *lockp = hash_lock; 1186 return (hdr); 1187 } 1188 } 1189 mutex_exit(hash_lock); 1190 *lockp = NULL; 1191 return (NULL); 1192} 1193 1194/* 1195 * Insert an entry into the hash table. If there is already an element 1196 * equal to elem in the hash table, then the already existing element 1197 * will be returned and the new element will not be inserted. 1198 * Otherwise returns NULL. 1199 * If lockp == NULL, the caller is assumed to already hold the hash lock. 1200 */ 1201static arc_buf_hdr_t * 1202buf_hash_insert(arc_buf_hdr_t *hdr, kmutex_t **lockp) 1203{ 1204 uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 1205 kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 1206 arc_buf_hdr_t *fhdr; 1207 uint32_t i; 1208 1209 ASSERT(!DVA_IS_EMPTY(&hdr->b_dva)); 1210 ASSERT(hdr->b_birth != 0); 1211 ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1212 1213 if (lockp != NULL) { 1214 *lockp = hash_lock; 1215 mutex_enter(hash_lock); 1216 } else { 1217 ASSERT(MUTEX_HELD(hash_lock)); 1218 } 1219 1220 for (fhdr = buf_hash_table.ht_table[idx], i = 0; fhdr != NULL; 1221 fhdr = fhdr->b_hash_next, i++) { 1222 if (BUF_EQUAL(hdr->b_spa, &hdr->b_dva, hdr->b_birth, fhdr)) 1223 return (fhdr); 1224 } 1225 1226 hdr->b_hash_next = buf_hash_table.ht_table[idx]; 1227 buf_hash_table.ht_table[idx] = hdr; 1228 hdr->b_flags |= ARC_FLAG_IN_HASH_TABLE; 1229 1230 /* collect some hash table performance data */ 1231 if (i > 0) { 1232 ARCSTAT_BUMP(arcstat_hash_collisions); 1233 if (i == 1) 1234 ARCSTAT_BUMP(arcstat_hash_chains); 1235 1236 ARCSTAT_MAX(arcstat_hash_chain_max, i); 1237 } 1238 1239 ARCSTAT_BUMP(arcstat_hash_elements); 1240 ARCSTAT_MAXSTAT(arcstat_hash_elements); 1241 1242 return (NULL); 1243} 1244 1245static void 1246buf_hash_remove(arc_buf_hdr_t *hdr) 1247{ 1248 arc_buf_hdr_t *fhdr, **hdrp; 1249 uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 1250 1251 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 1252 ASSERT(HDR_IN_HASH_TABLE(hdr)); 1253 1254 hdrp = &buf_hash_table.ht_table[idx]; 1255 while ((fhdr = *hdrp) != hdr) { 1256 ASSERT(fhdr != NULL); 1257 hdrp = &fhdr->b_hash_next; 1258 } 1259 *hdrp = hdr->b_hash_next; 1260 hdr->b_hash_next = NULL; 1261 hdr->b_flags &= ~ARC_FLAG_IN_HASH_TABLE; 1262 1263 /* collect some hash table performance data */ 1264 ARCSTAT_BUMPDOWN(arcstat_hash_elements); 1265 1266 if (buf_hash_table.ht_table[idx] && 1267 buf_hash_table.ht_table[idx]->b_hash_next == NULL) 1268 ARCSTAT_BUMPDOWN(arcstat_hash_chains); 1269} 1270 1271/* 1272 * Global data structures and functions for the buf kmem cache. 1273 */ 1274static kmem_cache_t *hdr_full_cache; 1275static kmem_cache_t *hdr_l2only_cache; 1276static kmem_cache_t *buf_cache; 1277 1278static void 1279buf_fini(void) 1280{ 1281 int i; 1282 1283 kmem_free(buf_hash_table.ht_table, 1284 (buf_hash_table.ht_mask + 1) * sizeof (void *)); 1285 for (i = 0; i < BUF_LOCKS; i++) 1286 mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 1287 kmem_cache_destroy(hdr_full_cache); 1288 kmem_cache_destroy(hdr_l2only_cache); 1289 kmem_cache_destroy(buf_cache); 1290} 1291 1292/* 1293 * Constructor callback - called when the cache is empty 1294 * and a new buf is requested. 1295 */ 1296/* ARGSUSED */ 1297static int 1298hdr_full_cons(void *vbuf, void *unused, int kmflag) 1299{ 1300 arc_buf_hdr_t *hdr = vbuf; 1301 1302 bzero(hdr, HDR_FULL_SIZE); 1303 cv_init(&hdr->b_l1hdr.b_cv, NULL, CV_DEFAULT, NULL); 1304 refcount_create(&hdr->b_l1hdr.b_refcnt); 1305 mutex_init(&hdr->b_l1hdr.b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 1306 multilist_link_init(&hdr->b_l1hdr.b_arc_node); 1307 arc_space_consume(HDR_FULL_SIZE, ARC_SPACE_HDRS); 1308 1309 return (0); 1310} 1311 1312/* ARGSUSED */ 1313static int 1314hdr_l2only_cons(void *vbuf, void *unused, int kmflag) 1315{ 1316 arc_buf_hdr_t *hdr = vbuf; 1317 1318 bzero(hdr, HDR_L2ONLY_SIZE); 1319 arc_space_consume(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); 1320 1321 return (0); 1322} 1323 1324/* ARGSUSED */ 1325static int 1326buf_cons(void *vbuf, void *unused, int kmflag) 1327{ 1328 arc_buf_t *buf = vbuf; 1329 1330 bzero(buf, sizeof (arc_buf_t)); 1331 mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL); 1332 arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); 1333 1334 return (0); 1335} 1336 1337/* 1338 * Destructor callback - called when a cached buf is 1339 * no longer required. 1340 */ 1341/* ARGSUSED */ 1342static void 1343hdr_full_dest(void *vbuf, void *unused) 1344{ 1345 arc_buf_hdr_t *hdr = vbuf; 1346 1347 ASSERT(BUF_EMPTY(hdr)); 1348 cv_destroy(&hdr->b_l1hdr.b_cv); 1349 refcount_destroy(&hdr->b_l1hdr.b_refcnt); 1350 mutex_destroy(&hdr->b_l1hdr.b_freeze_lock); 1351 ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 1352 arc_space_return(HDR_FULL_SIZE, ARC_SPACE_HDRS); 1353} 1354 1355/* ARGSUSED */ 1356static void 1357hdr_l2only_dest(void *vbuf, void *unused) 1358{ 1359 arc_buf_hdr_t *hdr = vbuf; 1360 1361 ASSERT(BUF_EMPTY(hdr)); 1362 arc_space_return(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); 1363} 1364 1365/* ARGSUSED */ 1366static void 1367buf_dest(void *vbuf, void *unused) 1368{ 1369 arc_buf_t *buf = vbuf; 1370 1371 mutex_destroy(&buf->b_evict_lock); 1372 arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); 1373} 1374 1375/* 1376 * Reclaim callback -- invoked when memory is low. 1377 */ 1378/* ARGSUSED */ 1379static void 1380hdr_recl(void *unused) 1381{ 1382 dprintf("hdr_recl called\n"); 1383 /* 1384 * umem calls the reclaim func when we destroy the buf cache, 1385 * which is after we do arc_fini(). 1386 */ 1387 if (!arc_dead) 1388 cv_signal(&arc_reclaim_thread_cv); 1389} 1390 1391static void 1392buf_init(void) 1393{ 1394 uint64_t *ct; 1395 uint64_t hsize = 1ULL << 12; 1396 int i, j; 1397 1398 /* 1399 * The hash table is big enough to fill all of physical memory 1400 * with an average block size of zfs_arc_average_blocksize (default 8K). 1401 * By default, the table will take up 1402 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers). 1403 */ 1404 while (hsize * zfs_arc_average_blocksize < (uint64_t)physmem * PAGESIZE) 1405 hsize <<= 1; 1406retry: 1407 buf_hash_table.ht_mask = hsize - 1; 1408 buf_hash_table.ht_table = 1409 kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 1410 if (buf_hash_table.ht_table == NULL) { 1411 ASSERT(hsize > (1ULL << 8)); 1412 hsize >>= 1; 1413 goto retry; 1414 } 1415 1416 hdr_full_cache = kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE, 1417 0, hdr_full_cons, hdr_full_dest, hdr_recl, NULL, NULL, 0); 1418 hdr_l2only_cache = kmem_cache_create("arc_buf_hdr_t_l2only", 1419 HDR_L2ONLY_SIZE, 0, hdr_l2only_cons, hdr_l2only_dest, hdr_recl, 1420 NULL, NULL, 0); 1421 buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 1422 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 1423 1424 for (i = 0; i < 256; i++) 1425 for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 1426 *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 1427 1428 for (i = 0; i < BUF_LOCKS; i++) { 1429 mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 1430 NULL, MUTEX_DEFAULT, NULL); 1431 } 1432} 1433 1434/* 1435 * Transition between the two allocation states for the arc_buf_hdr struct. 1436 * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without 1437 * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller 1438 * version is used when a cache buffer is only in the L2ARC in order to reduce 1439 * memory usage. 1440 */ 1441static arc_buf_hdr_t * 1442arc_hdr_realloc(arc_buf_hdr_t *hdr, kmem_cache_t *old, kmem_cache_t *new) 1443{ 1444 ASSERT(HDR_HAS_L2HDR(hdr)); 1445 1446 arc_buf_hdr_t *nhdr; 1447 l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; 1448 1449 ASSERT((old == hdr_full_cache && new == hdr_l2only_cache) || 1450 (old == hdr_l2only_cache && new == hdr_full_cache)); 1451 1452 nhdr = kmem_cache_alloc(new, KM_PUSHPAGE); 1453 1454 ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); 1455 buf_hash_remove(hdr); 1456 1457 bcopy(hdr, nhdr, HDR_L2ONLY_SIZE); 1458 1459 if (new == hdr_full_cache) { 1460 nhdr->b_flags |= ARC_FLAG_HAS_L1HDR; 1461 /* 1462 * arc_access and arc_change_state need to be aware that a 1463 * header has just come out of L2ARC, so we set its state to 1464 * l2c_only even though it's about to change. 1465 */ 1466 nhdr->b_l1hdr.b_state = arc_l2c_only; 1467 1468 /* Verify previous threads set to NULL before freeing */ 1469 ASSERT3P(nhdr->b_l1hdr.b_tmp_cdata, ==, NULL); 1470 } else { 1471 ASSERT(hdr->b_l1hdr.b_buf == NULL); 1472 ASSERT0(hdr->b_l1hdr.b_datacnt); 1473 1474 /* 1475 * If we've reached here, We must have been called from 1476 * arc_evict_hdr(), as such we should have already been 1477 * removed from any ghost list we were previously on 1478 * (which protects us from racing with arc_evict_state), 1479 * thus no locking is needed during this check. 1480 */ 1481 ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 1482 1483 /* 1484 * A buffer must not be moved into the arc_l2c_only 1485 * state if it's not finished being written out to the 1486 * l2arc device. Otherwise, the b_l1hdr.b_tmp_cdata field 1487 * might try to be accessed, even though it was removed. 1488 */ 1489 VERIFY(!HDR_L2_WRITING(hdr)); 1490 VERIFY3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL); 1491 1492 nhdr->b_flags &= ~ARC_FLAG_HAS_L1HDR; 1493 } 1494 /* 1495 * The header has been reallocated so we need to re-insert it into any 1496 * lists it was on. 1497 */ 1498 (void) buf_hash_insert(nhdr, NULL); 1499 1500 ASSERT(list_link_active(&hdr->b_l2hdr.b_l2node)); 1501 1502 mutex_enter(&dev->l2ad_mtx); 1503 1504 /* 1505 * We must place the realloc'ed header back into the list at 1506 * the same spot. Otherwise, if it's placed earlier in the list, 1507 * l2arc_write_buffers() could find it during the function's 1508 * write phase, and try to write it out to the l2arc. 1509 */ 1510 list_insert_after(&dev->l2ad_buflist, hdr, nhdr); 1511 list_remove(&dev->l2ad_buflist, hdr); 1512 1513 mutex_exit(&dev->l2ad_mtx); 1514 1515 /* 1516 * Since we're using the pointer address as the tag when 1517 * incrementing and decrementing the l2ad_alloc refcount, we 1518 * must remove the old pointer (that we're about to destroy) and 1519 * add the new pointer to the refcount. Otherwise we'd remove 1520 * the wrong pointer address when calling arc_hdr_destroy() later. 1521 */ 1522 1523 (void) refcount_remove_many(&dev->l2ad_alloc, 1524 hdr->b_l2hdr.b_asize, hdr); 1525 1526 (void) refcount_add_many(&dev->l2ad_alloc, 1527 nhdr->b_l2hdr.b_asize, nhdr); 1528 1529 buf_discard_identity(hdr); 1530 hdr->b_freeze_cksum = NULL; 1531 kmem_cache_free(old, hdr); 1532 1533 return (nhdr); 1534} 1535 1536 1537#define ARC_MINTIME (hz>>4) /* 62 ms */ 1538 1539static void 1540arc_cksum_verify(arc_buf_t *buf) 1541{ 1542 zio_cksum_t zc; 1543 1544 if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1545 return; 1546 1547 mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1548 if (buf->b_hdr->b_freeze_cksum == NULL || HDR_IO_ERROR(buf->b_hdr)) { 1549 mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1550 return; 1551 } 1552 fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 1553 if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) 1554 panic("buffer modified while frozen!"); 1555 mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1556} 1557 1558static int 1559arc_cksum_equal(arc_buf_t *buf) 1560{ 1561 zio_cksum_t zc; 1562 int equal; 1563 1564 mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1565 fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 1566 equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc); 1567 mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1568 1569 return (equal); 1570} 1571 1572static void 1573arc_cksum_compute(arc_buf_t *buf, boolean_t force) 1574{ 1575 if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY)) 1576 return; 1577 1578 mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1579 if (buf->b_hdr->b_freeze_cksum != NULL) { 1580 mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1581 return; 1582 } 1583 buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 1584 fletcher_2_native(buf->b_data, buf->b_hdr->b_size, 1585 buf->b_hdr->b_freeze_cksum); 1586 mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1587#ifdef illumos 1588 arc_buf_watch(buf); 1589#endif 1590} 1591 1592#ifdef illumos 1593#ifndef _KERNEL 1594typedef struct procctl { 1595 long cmd; 1596 prwatch_t prwatch; 1597} procctl_t; 1598#endif 1599 1600/* ARGSUSED */ 1601static void 1602arc_buf_unwatch(arc_buf_t *buf) 1603{ 1604#ifndef _KERNEL 1605 if (arc_watch) { 1606 int result; 1607 procctl_t ctl; 1608 ctl.cmd = PCWATCH; 1609 ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1610 ctl.prwatch.pr_size = 0; 1611 ctl.prwatch.pr_wflags = 0; 1612 result = write(arc_procfd, &ctl, sizeof (ctl)); 1613 ASSERT3U(result, ==, sizeof (ctl)); 1614 } 1615#endif 1616} 1617 1618/* ARGSUSED */ 1619static void 1620arc_buf_watch(arc_buf_t *buf) 1621{ 1622#ifndef _KERNEL 1623 if (arc_watch) { 1624 int result; 1625 procctl_t ctl; 1626 ctl.cmd = PCWATCH; 1627 ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1628 ctl.prwatch.pr_size = buf->b_hdr->b_size; 1629 ctl.prwatch.pr_wflags = WA_WRITE; 1630 result = write(arc_procfd, &ctl, sizeof (ctl)); 1631 ASSERT3U(result, ==, sizeof (ctl)); 1632 } 1633#endif 1634} 1635#endif /* illumos */ 1636 1637static arc_buf_contents_t 1638arc_buf_type(arc_buf_hdr_t *hdr) 1639{ 1640 if (HDR_ISTYPE_METADATA(hdr)) { 1641 return (ARC_BUFC_METADATA); 1642 } else { 1643 return (ARC_BUFC_DATA); 1644 } 1645} 1646 1647static uint32_t 1648arc_bufc_to_flags(arc_buf_contents_t type) 1649{ 1650 switch (type) { 1651 case ARC_BUFC_DATA: 1652 /* metadata field is 0 if buffer contains normal data */ 1653 return (0); 1654 case ARC_BUFC_METADATA: 1655 return (ARC_FLAG_BUFC_METADATA); 1656 default: 1657 break; 1658 } 1659 panic("undefined ARC buffer type!"); 1660 return ((uint32_t)-1); 1661} 1662 1663void 1664arc_buf_thaw(arc_buf_t *buf) 1665{ 1666 if (zfs_flags & ZFS_DEBUG_MODIFY) { 1667 if (buf->b_hdr->b_l1hdr.b_state != arc_anon) 1668 panic("modifying non-anon buffer!"); 1669 if (HDR_IO_IN_PROGRESS(buf->b_hdr)) 1670 panic("modifying buffer while i/o in progress!"); 1671 arc_cksum_verify(buf); 1672 } 1673 1674 mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1675 if (buf->b_hdr->b_freeze_cksum != NULL) { 1676 kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 1677 buf->b_hdr->b_freeze_cksum = NULL; 1678 } 1679 1680#ifdef ZFS_DEBUG 1681 if (zfs_flags & ZFS_DEBUG_MODIFY) { 1682 if (buf->b_hdr->b_l1hdr.b_thawed != NULL) 1683 kmem_free(buf->b_hdr->b_l1hdr.b_thawed, 1); 1684 buf->b_hdr->b_l1hdr.b_thawed = kmem_alloc(1, KM_SLEEP); 1685 } 1686#endif 1687 1688 mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1689 1690#ifdef illumos 1691 arc_buf_unwatch(buf); 1692#endif 1693} 1694 1695void 1696arc_buf_freeze(arc_buf_t *buf) 1697{ 1698 kmutex_t *hash_lock; 1699 1700 if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1701 return; 1702 1703 hash_lock = HDR_LOCK(buf->b_hdr); 1704 mutex_enter(hash_lock); 1705 1706 ASSERT(buf->b_hdr->b_freeze_cksum != NULL || 1707 buf->b_hdr->b_l1hdr.b_state == arc_anon); 1708 arc_cksum_compute(buf, B_FALSE); 1709 mutex_exit(hash_lock); 1710 1711} 1712 1713static void 1714add_reference(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, void *tag) 1715{ 1716 ASSERT(HDR_HAS_L1HDR(hdr)); 1717 ASSERT(MUTEX_HELD(hash_lock)); 1718 arc_state_t *state = hdr->b_l1hdr.b_state; 1719 1720 if ((refcount_add(&hdr->b_l1hdr.b_refcnt, tag) == 1) && 1721 (state != arc_anon)) { 1722 /* We don't use the L2-only state list. */ 1723 if (state != arc_l2c_only) { 1724 arc_buf_contents_t type = arc_buf_type(hdr); 1725 uint64_t delta = hdr->b_size * hdr->b_l1hdr.b_datacnt; 1726 multilist_t *list = &state->arcs_list[type]; 1727 uint64_t *size = &state->arcs_lsize[type]; 1728 1729 multilist_remove(list, hdr); 1730 1731 if (GHOST_STATE(state)) { 1732 ASSERT0(hdr->b_l1hdr.b_datacnt); 1733 ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 1734 delta = hdr->b_size; 1735 } 1736 ASSERT(delta > 0); 1737 ASSERT3U(*size, >=, delta); 1738 atomic_add_64(size, -delta); 1739 } 1740 /* remove the prefetch flag if we get a reference */ 1741 hdr->b_flags &= ~ARC_FLAG_PREFETCH; 1742 } 1743} 1744 1745static int 1746remove_reference(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, void *tag) 1747{ 1748 int cnt; 1749 arc_state_t *state = hdr->b_l1hdr.b_state; 1750 1751 ASSERT(HDR_HAS_L1HDR(hdr)); 1752 ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 1753 ASSERT(!GHOST_STATE(state)); 1754 1755 /* 1756 * arc_l2c_only counts as a ghost state so we don't need to explicitly 1757 * check to prevent usage of the arc_l2c_only list. 1758 */ 1759 if (((cnt = refcount_remove(&hdr->b_l1hdr.b_refcnt, tag)) == 0) && 1760 (state != arc_anon)) { 1761 arc_buf_contents_t type = arc_buf_type(hdr); 1762 multilist_t *list = &state->arcs_list[type]; 1763 uint64_t *size = &state->arcs_lsize[type]; 1764 1765 multilist_insert(list, hdr); 1766 1767 ASSERT(hdr->b_l1hdr.b_datacnt > 0); 1768 atomic_add_64(size, hdr->b_size * 1769 hdr->b_l1hdr.b_datacnt); 1770 } 1771 return (cnt); 1772} 1773 1774/* 1775 * Move the supplied buffer to the indicated state. The hash lock 1776 * for the buffer must be held by the caller. 1777 */ 1778static void 1779arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *hdr, 1780 kmutex_t *hash_lock) 1781{ 1782 arc_state_t *old_state; 1783 int64_t refcnt; 1784 uint32_t datacnt; 1785 uint64_t from_delta, to_delta; 1786 arc_buf_contents_t buftype = arc_buf_type(hdr); 1787 1788 /* 1789 * We almost always have an L1 hdr here, since we call arc_hdr_realloc() 1790 * in arc_read() when bringing a buffer out of the L2ARC. However, the 1791 * L1 hdr doesn't always exist when we change state to arc_anon before 1792 * destroying a header, in which case reallocating to add the L1 hdr is 1793 * pointless. 1794 */ 1795 if (HDR_HAS_L1HDR(hdr)) { 1796 old_state = hdr->b_l1hdr.b_state; 1797 refcnt = refcount_count(&hdr->b_l1hdr.b_refcnt); 1798 datacnt = hdr->b_l1hdr.b_datacnt; 1799 } else { 1800 old_state = arc_l2c_only; 1801 refcnt = 0; 1802 datacnt = 0; 1803 } 1804 1805 ASSERT(MUTEX_HELD(hash_lock)); 1806 ASSERT3P(new_state, !=, old_state); 1807 ASSERT(refcnt == 0 || datacnt > 0); 1808 ASSERT(!GHOST_STATE(new_state) || datacnt == 0); 1809 ASSERT(old_state != arc_anon || datacnt <= 1); 1810 1811 from_delta = to_delta = datacnt * hdr->b_size; 1812 1813 /* 1814 * If this buffer is evictable, transfer it from the 1815 * old state list to the new state list. 1816 */ 1817 if (refcnt == 0) { 1818 if (old_state != arc_anon && old_state != arc_l2c_only) { 1819 uint64_t *size = &old_state->arcs_lsize[buftype]; 1820 1821 ASSERT(HDR_HAS_L1HDR(hdr)); 1822 multilist_remove(&old_state->arcs_list[buftype], hdr); 1823 1824 /* 1825 * If prefetching out of the ghost cache, 1826 * we will have a non-zero datacnt. 1827 */ 1828 if (GHOST_STATE(old_state) && datacnt == 0) { 1829 /* ghost elements have a ghost size */ 1830 ASSERT(hdr->b_l1hdr.b_buf == NULL); 1831 from_delta = hdr->b_size; 1832 } 1833 ASSERT3U(*size, >=, from_delta); 1834 atomic_add_64(size, -from_delta); 1835 } 1836 if (new_state != arc_anon && new_state != arc_l2c_only) { 1837 uint64_t *size = &new_state->arcs_lsize[buftype]; 1838 1839 /* 1840 * An L1 header always exists here, since if we're 1841 * moving to some L1-cached state (i.e. not l2c_only or 1842 * anonymous), we realloc the header to add an L1hdr 1843 * beforehand. 1844 */ 1845 ASSERT(HDR_HAS_L1HDR(hdr)); 1846 multilist_insert(&new_state->arcs_list[buftype], hdr); 1847 1848 /* ghost elements have a ghost size */ 1849 if (GHOST_STATE(new_state)) { 1850 ASSERT0(datacnt); 1851 ASSERT(hdr->b_l1hdr.b_buf == NULL); 1852 to_delta = hdr->b_size; 1853 } 1854 atomic_add_64(size, to_delta); 1855 } 1856 } 1857 1858 ASSERT(!BUF_EMPTY(hdr)); 1859 if (new_state == arc_anon && HDR_IN_HASH_TABLE(hdr)) 1860 buf_hash_remove(hdr); 1861 1862 /* adjust state sizes (ignore arc_l2c_only) */ 1863 if (to_delta && new_state != arc_l2c_only) 1864 atomic_add_64(&new_state->arcs_size, to_delta); 1865 if (from_delta && old_state != arc_l2c_only) { 1866 ASSERT3U(old_state->arcs_size, >=, from_delta); 1867 atomic_add_64(&old_state->arcs_size, -from_delta); 1868 } 1869 if (HDR_HAS_L1HDR(hdr)) 1870 hdr->b_l1hdr.b_state = new_state; 1871 1872 /* 1873 * L2 headers should never be on the L2 state list since they don't 1874 * have L1 headers allocated. 1875 */ 1876 ASSERT(multilist_is_empty(&arc_l2c_only->arcs_list[ARC_BUFC_DATA]) && 1877 multilist_is_empty(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA])); 1878} 1879 1880void 1881arc_space_consume(uint64_t space, arc_space_type_t type) 1882{ 1883 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 1884 1885 switch (type) { 1886 case ARC_SPACE_DATA: 1887 ARCSTAT_INCR(arcstat_data_size, space); 1888 break; 1889 case ARC_SPACE_META: 1890 ARCSTAT_INCR(arcstat_metadata_size, space); 1891 break; 1892 case ARC_SPACE_OTHER: 1893 ARCSTAT_INCR(arcstat_other_size, space); 1894 break; 1895 case ARC_SPACE_HDRS: 1896 ARCSTAT_INCR(arcstat_hdr_size, space); 1897 break; 1898 case ARC_SPACE_L2HDRS: 1899 ARCSTAT_INCR(arcstat_l2_hdr_size, space); 1900 break; 1901 } 1902 1903 if (type != ARC_SPACE_DATA) 1904 ARCSTAT_INCR(arcstat_meta_used, space); 1905 1906 atomic_add_64(&arc_size, space); 1907} 1908 1909void 1910arc_space_return(uint64_t space, arc_space_type_t type) 1911{ 1912 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 1913 1914 switch (type) { 1915 case ARC_SPACE_DATA: 1916 ARCSTAT_INCR(arcstat_data_size, -space); 1917 break; 1918 case ARC_SPACE_META: 1919 ARCSTAT_INCR(arcstat_metadata_size, -space); 1920 break; 1921 case ARC_SPACE_OTHER: 1922 ARCSTAT_INCR(arcstat_other_size, -space); 1923 break; 1924 case ARC_SPACE_HDRS: 1925 ARCSTAT_INCR(arcstat_hdr_size, -space); 1926 break; 1927 case ARC_SPACE_L2HDRS: 1928 ARCSTAT_INCR(arcstat_l2_hdr_size, -space); 1929 break; 1930 } 1931 1932 if (type != ARC_SPACE_DATA) { 1933 ASSERT(arc_meta_used >= space); 1934 if (arc_meta_max < arc_meta_used) 1935 arc_meta_max = arc_meta_used; 1936 ARCSTAT_INCR(arcstat_meta_used, -space); 1937 } 1938 1939 ASSERT(arc_size >= space); 1940 atomic_add_64(&arc_size, -space); 1941} 1942 1943arc_buf_t * 1944arc_buf_alloc(spa_t *spa, int32_t size, void *tag, arc_buf_contents_t type) 1945{ 1946 arc_buf_hdr_t *hdr; 1947 arc_buf_t *buf; 1948 1949 ASSERT3U(size, >, 0); 1950 hdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE); 1951 ASSERT(BUF_EMPTY(hdr)); 1952 ASSERT3P(hdr->b_freeze_cksum, ==, NULL); 1953 hdr->b_size = size; 1954 hdr->b_spa = spa_load_guid(spa); 1955 1956 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1957 buf->b_hdr = hdr; 1958 buf->b_data = NULL; 1959 buf->b_efunc = NULL; 1960 buf->b_private = NULL; 1961 buf->b_next = NULL; 1962 1963 hdr->b_flags = arc_bufc_to_flags(type); 1964 hdr->b_flags |= ARC_FLAG_HAS_L1HDR; 1965 1966 hdr->b_l1hdr.b_buf = buf; 1967 hdr->b_l1hdr.b_state = arc_anon; 1968 hdr->b_l1hdr.b_arc_access = 0; 1969 hdr->b_l1hdr.b_datacnt = 1; 1970 hdr->b_l1hdr.b_tmp_cdata = NULL; 1971 1972 arc_get_data_buf(buf); 1973 ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 1974 (void) refcount_add(&hdr->b_l1hdr.b_refcnt, tag); 1975 1976 return (buf); 1977} 1978 1979static char *arc_onloan_tag = "onloan"; 1980 1981/* 1982 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in 1983 * flight data by arc_tempreserve_space() until they are "returned". Loaned 1984 * buffers must be returned to the arc before they can be used by the DMU or 1985 * freed. 1986 */ 1987arc_buf_t * 1988arc_loan_buf(spa_t *spa, int size) 1989{ 1990 arc_buf_t *buf; 1991 1992 buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA); 1993 1994 atomic_add_64(&arc_loaned_bytes, size); 1995 return (buf); 1996} 1997 1998/* 1999 * Return a loaned arc buffer to the arc. 2000 */ 2001void 2002arc_return_buf(arc_buf_t *buf, void *tag) 2003{ 2004 arc_buf_hdr_t *hdr = buf->b_hdr; 2005 2006 ASSERT(buf->b_data != NULL); 2007 ASSERT(HDR_HAS_L1HDR(hdr)); 2008 (void) refcount_add(&hdr->b_l1hdr.b_refcnt, tag); 2009 (void) refcount_remove(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); 2010 2011 atomic_add_64(&arc_loaned_bytes, -hdr->b_size); 2012} 2013 2014/* Detach an arc_buf from a dbuf (tag) */ 2015void 2016arc_loan_inuse_buf(arc_buf_t *buf, void *tag) 2017{ 2018 arc_buf_hdr_t *hdr = buf->b_hdr; 2019 2020 ASSERT(buf->b_data != NULL); 2021 ASSERT(HDR_HAS_L1HDR(hdr)); 2022 (void) refcount_add(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); 2023 (void) refcount_remove(&hdr->b_l1hdr.b_refcnt, tag); 2024 buf->b_efunc = NULL; 2025 buf->b_private = NULL; 2026 2027 atomic_add_64(&arc_loaned_bytes, hdr->b_size); 2028} 2029 2030static arc_buf_t * 2031arc_buf_clone(arc_buf_t *from) 2032{ 2033 arc_buf_t *buf; 2034 arc_buf_hdr_t *hdr = from->b_hdr; 2035 uint64_t size = hdr->b_size; 2036 2037 ASSERT(HDR_HAS_L1HDR(hdr)); 2038 ASSERT(hdr->b_l1hdr.b_state != arc_anon); 2039 2040 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 2041 buf->b_hdr = hdr; 2042 buf->b_data = NULL; 2043 buf->b_efunc = NULL; 2044 buf->b_private = NULL; 2045 buf->b_next = hdr->b_l1hdr.b_buf; 2046 hdr->b_l1hdr.b_buf = buf; 2047 arc_get_data_buf(buf); 2048 bcopy(from->b_data, buf->b_data, size); 2049 2050 /* 2051 * This buffer already exists in the arc so create a duplicate 2052 * copy for the caller. If the buffer is associated with user data 2053 * then track the size and number of duplicates. These stats will be 2054 * updated as duplicate buffers are created and destroyed. 2055 */ 2056 if (HDR_ISTYPE_DATA(hdr)) { 2057 ARCSTAT_BUMP(arcstat_duplicate_buffers); 2058 ARCSTAT_INCR(arcstat_duplicate_buffers_size, size); 2059 } 2060 hdr->b_l1hdr.b_datacnt += 1; 2061 return (buf); 2062} 2063 2064void 2065arc_buf_add_ref(arc_buf_t *buf, void* tag) 2066{ 2067 arc_buf_hdr_t *hdr; 2068 kmutex_t *hash_lock; 2069 2070 /* 2071 * Check to see if this buffer is evicted. Callers 2072 * must verify b_data != NULL to know if the add_ref 2073 * was successful. 2074 */ 2075 mutex_enter(&buf->b_evict_lock); 2076 if (buf->b_data == NULL) { 2077 mutex_exit(&buf->b_evict_lock); 2078 return; 2079 } 2080 hash_lock = HDR_LOCK(buf->b_hdr); 2081 mutex_enter(hash_lock); 2082 hdr = buf->b_hdr; 2083 ASSERT(HDR_HAS_L1HDR(hdr)); 2084 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 2085 mutex_exit(&buf->b_evict_lock); 2086 2087 ASSERT(hdr->b_l1hdr.b_state == arc_mru || 2088 hdr->b_l1hdr.b_state == arc_mfu); 2089 2090 add_reference(hdr, hash_lock, tag); 2091 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 2092 arc_access(hdr, hash_lock); 2093 mutex_exit(hash_lock); 2094 ARCSTAT_BUMP(arcstat_hits); 2095 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), 2096 demand, prefetch, !HDR_ISTYPE_METADATA(hdr), 2097 data, metadata, hits); 2098} 2099 2100static void 2101arc_buf_free_on_write(void *data, size_t size, 2102 void (*free_func)(void *, size_t)) 2103{ 2104 l2arc_data_free_t *df; 2105 2106 df = kmem_alloc(sizeof (*df), KM_SLEEP); 2107 df->l2df_data = data; 2108 df->l2df_size = size; 2109 df->l2df_func = free_func; 2110 mutex_enter(&l2arc_free_on_write_mtx); 2111 list_insert_head(l2arc_free_on_write, df); 2112 mutex_exit(&l2arc_free_on_write_mtx); 2113} 2114 2115/* 2116 * Free the arc data buffer. If it is an l2arc write in progress, 2117 * the buffer is placed on l2arc_free_on_write to be freed later. 2118 */ 2119static void 2120arc_buf_data_free(arc_buf_t *buf, void (*free_func)(void *, size_t)) 2121{ 2122 arc_buf_hdr_t *hdr = buf->b_hdr; 2123 2124 if (HDR_L2_WRITING(hdr)) { 2125 arc_buf_free_on_write(buf->b_data, hdr->b_size, free_func); 2126 ARCSTAT_BUMP(arcstat_l2_free_on_write); 2127 } else { 2128 free_func(buf->b_data, hdr->b_size); 2129 } 2130} 2131 2132/* 2133 * Free up buf->b_data and if 'remove' is set, then pull the 2134 * arc_buf_t off of the the arc_buf_hdr_t's list and free it. 2135 */ 2136static void 2137arc_buf_l2_cdata_free(arc_buf_hdr_t *hdr) 2138{ 2139 ASSERT(HDR_HAS_L2HDR(hdr)); 2140 ASSERT(MUTEX_HELD(&hdr->b_l2hdr.b_dev->l2ad_mtx)); 2141 2142 /* 2143 * The b_tmp_cdata field is linked off of the b_l1hdr, so if 2144 * that doesn't exist, the header is in the arc_l2c_only state, 2145 * and there isn't anything to free (it's already been freed). 2146 */ 2147 if (!HDR_HAS_L1HDR(hdr)) 2148 return; 2149 2150 /* 2151 * The header isn't being written to the l2arc device, thus it 2152 * shouldn't have a b_tmp_cdata to free. 2153 */ 2154 if (!HDR_L2_WRITING(hdr)) { 2155 ASSERT3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL); 2156 return; 2157 } 2158 2159 /* 2160 * The header does not have compression enabled. This can be due 2161 * to the buffer not being compressible, or because we're 2162 * freeing the buffer before the second phase of 2163 * l2arc_write_buffer() has started (which does the compression 2164 * step). In either case, b_tmp_cdata does not point to a 2165 * separately compressed buffer, so there's nothing to free (it 2166 * points to the same buffer as the arc_buf_t's b_data field). 2167 */ 2168 if (HDR_GET_COMPRESS(hdr) == ZIO_COMPRESS_OFF) { 2169 hdr->b_l1hdr.b_tmp_cdata = NULL; 2170 return; 2171 } 2172 2173 /* 2174 * There's nothing to free since the buffer was all zero's and 2175 * compressed to a zero length buffer. 2176 */ 2177 if (HDR_GET_COMPRESS(hdr) == ZIO_COMPRESS_EMPTY) { 2178 ASSERT3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL); 2179 return; 2180 } 2181 2182 ASSERT(L2ARC_IS_VALID_COMPRESS(HDR_GET_COMPRESS(hdr))); 2183 2184 arc_buf_free_on_write(hdr->b_l1hdr.b_tmp_cdata, 2185 hdr->b_size, zio_data_buf_free); 2186 2187 ARCSTAT_BUMP(arcstat_l2_cdata_free_on_write); 2188 hdr->b_l1hdr.b_tmp_cdata = NULL; 2189} 2190 2191static void 2192arc_buf_destroy(arc_buf_t *buf, boolean_t remove) 2193{ 2194 arc_buf_t **bufp; 2195 2196 /* free up data associated with the buf */ 2197 if (buf->b_data != NULL) { 2198 arc_state_t *state = buf->b_hdr->b_l1hdr.b_state; 2199 uint64_t size = buf->b_hdr->b_size; 2200 arc_buf_contents_t type = arc_buf_type(buf->b_hdr); 2201 2202 arc_cksum_verify(buf); 2203#ifdef illumos 2204 arc_buf_unwatch(buf); 2205#endif 2206 2207 if (type == ARC_BUFC_METADATA) { 2208 arc_buf_data_free(buf, zio_buf_free); 2209 arc_space_return(size, ARC_SPACE_META); 2210 } else { 2211 ASSERT(type == ARC_BUFC_DATA); 2212 arc_buf_data_free(buf, zio_data_buf_free); 2213 arc_space_return(size, ARC_SPACE_DATA); 2214 } 2215 2216 /* protected by hash lock, if in the hash table */ 2217 if (multilist_link_active(&buf->b_hdr->b_l1hdr.b_arc_node)) { 2218 uint64_t *cnt = &state->arcs_lsize[type]; 2219 2220 ASSERT(refcount_is_zero( 2221 &buf->b_hdr->b_l1hdr.b_refcnt)); 2222 ASSERT(state != arc_anon && state != arc_l2c_only); 2223 2224 ASSERT3U(*cnt, >=, size); 2225 atomic_add_64(cnt, -size); 2226 } 2227 ASSERT3U(state->arcs_size, >=, size); 2228 atomic_add_64(&state->arcs_size, -size); 2229 buf->b_data = NULL; 2230 2231 /* 2232 * If we're destroying a duplicate buffer make sure 2233 * that the appropriate statistics are updated. 2234 */ 2235 if (buf->b_hdr->b_l1hdr.b_datacnt > 1 && 2236 HDR_ISTYPE_DATA(buf->b_hdr)) { 2237 ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers); 2238 ARCSTAT_INCR(arcstat_duplicate_buffers_size, -size); 2239 } 2240 ASSERT(buf->b_hdr->b_l1hdr.b_datacnt > 0); 2241 buf->b_hdr->b_l1hdr.b_datacnt -= 1; 2242 } 2243 2244 /* only remove the buf if requested */ 2245 if (!remove) 2246 return; 2247 2248 /* remove the buf from the hdr list */ 2249 for (bufp = &buf->b_hdr->b_l1hdr.b_buf; *bufp != buf; 2250 bufp = &(*bufp)->b_next) 2251 continue; 2252 *bufp = buf->b_next; 2253 buf->b_next = NULL; 2254 2255 ASSERT(buf->b_efunc == NULL); 2256 2257 /* clean up the buf */ 2258 buf->b_hdr = NULL; 2259 kmem_cache_free(buf_cache, buf); 2260} 2261 2262static void 2263arc_hdr_l2hdr_destroy(arc_buf_hdr_t *hdr) 2264{ 2265 l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr; 2266 l2arc_dev_t *dev = l2hdr->b_dev; 2267 2268 ASSERT(MUTEX_HELD(&dev->l2ad_mtx)); 2269 ASSERT(HDR_HAS_L2HDR(hdr)); 2270 2271 list_remove(&dev->l2ad_buflist, hdr); 2272 2273 /* 2274 * We don't want to leak the b_tmp_cdata buffer that was 2275 * allocated in l2arc_write_buffers() 2276 */ 2277 arc_buf_l2_cdata_free(hdr); 2278 2279 /* 2280 * If the l2hdr's b_daddr is equal to L2ARC_ADDR_UNSET, then 2281 * this header is being processed by l2arc_write_buffers() (i.e. 2282 * it's in the first stage of l2arc_write_buffers()). 2283 * Re-affirming that truth here, just to serve as a reminder. If 2284 * b_daddr does not equal L2ARC_ADDR_UNSET, then the header may or 2285 * may not have its HDR_L2_WRITING flag set. (the write may have 2286 * completed, in which case HDR_L2_WRITING will be false and the 2287 * b_daddr field will point to the address of the buffer on disk). 2288 */ 2289 IMPLY(l2hdr->b_daddr == L2ARC_ADDR_UNSET, HDR_L2_WRITING(hdr)); 2290 2291 /* 2292 * If b_daddr is equal to L2ARC_ADDR_UNSET, we're racing with 2293 * l2arc_write_buffers(). Since we've just removed this header 2294 * from the l2arc buffer list, this header will never reach the 2295 * second stage of l2arc_write_buffers(), which increments the 2296 * accounting stats for this header. Thus, we must be careful 2297 * not to decrement them for this header either. 2298 */ 2299 if (l2hdr->b_daddr != L2ARC_ADDR_UNSET) { 2300 ARCSTAT_INCR(arcstat_l2_asize, -l2hdr->b_asize); 2301 ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 2302 2303 vdev_space_update(dev->l2ad_vdev, 2304 -l2hdr->b_asize, 0, 0); 2305 2306 (void) refcount_remove_many(&dev->l2ad_alloc, 2307 l2hdr->b_asize, hdr); 2308 } 2309 2310 hdr->b_flags &= ~ARC_FLAG_HAS_L2HDR; 2311} 2312 2313static void 2314arc_hdr_destroy(arc_buf_hdr_t *hdr) 2315{ 2316 if (HDR_HAS_L1HDR(hdr)) { 2317 ASSERT(hdr->b_l1hdr.b_buf == NULL || 2318 hdr->b_l1hdr.b_datacnt > 0); 2319 ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2320 ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 2321 } 2322 ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 2323 ASSERT(!HDR_IN_HASH_TABLE(hdr)); 2324 2325 if (HDR_HAS_L2HDR(hdr)) { 2326 l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; 2327 boolean_t buflist_held = MUTEX_HELD(&dev->l2ad_mtx); 2328 2329 if (!buflist_held) 2330 mutex_enter(&dev->l2ad_mtx); 2331 2332 /* 2333 * Even though we checked this conditional above, we 2334 * need to check this again now that we have the 2335 * l2ad_mtx. This is because we could be racing with 2336 * another thread calling l2arc_evict() which might have 2337 * destroyed this header's L2 portion as we were waiting 2338 * to acquire the l2ad_mtx. If that happens, we don't 2339 * want to re-destroy the header's L2 portion. 2340 */ 2341 if (HDR_HAS_L2HDR(hdr)) { 2342 if (hdr->b_l2hdr.b_daddr != L2ARC_ADDR_UNSET) 2343 trim_map_free(dev->l2ad_vdev, 2344 hdr->b_l2hdr.b_daddr, 2345 hdr->b_l2hdr.b_asize, 0); 2346 arc_hdr_l2hdr_destroy(hdr); 2347 } 2348 2349 if (!buflist_held) 2350 mutex_exit(&dev->l2ad_mtx); 2351 } 2352 2353 if (!BUF_EMPTY(hdr)) 2354 buf_discard_identity(hdr); 2355 if (hdr->b_freeze_cksum != NULL) { 2356 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 2357 hdr->b_freeze_cksum = NULL; 2358 } 2359 2360 if (HDR_HAS_L1HDR(hdr)) { 2361 while (hdr->b_l1hdr.b_buf) { 2362 arc_buf_t *buf = hdr->b_l1hdr.b_buf; 2363 2364 if (buf->b_efunc != NULL) { 2365 mutex_enter(&arc_user_evicts_lock); 2366 mutex_enter(&buf->b_evict_lock); 2367 ASSERT(buf->b_hdr != NULL); 2368 arc_buf_destroy(hdr->b_l1hdr.b_buf, FALSE); 2369 hdr->b_l1hdr.b_buf = buf->b_next; 2370 buf->b_hdr = &arc_eviction_hdr; 2371 buf->b_next = arc_eviction_list; 2372 arc_eviction_list = buf; 2373 mutex_exit(&buf->b_evict_lock); 2374 cv_signal(&arc_user_evicts_cv); 2375 mutex_exit(&arc_user_evicts_lock); 2376 } else { 2377 arc_buf_destroy(hdr->b_l1hdr.b_buf, TRUE); 2378 } 2379 } 2380#ifdef ZFS_DEBUG 2381 if (hdr->b_l1hdr.b_thawed != NULL) { 2382 kmem_free(hdr->b_l1hdr.b_thawed, 1); 2383 hdr->b_l1hdr.b_thawed = NULL; 2384 } 2385#endif 2386 } 2387 2388 ASSERT3P(hdr->b_hash_next, ==, NULL); 2389 if (HDR_HAS_L1HDR(hdr)) { 2390 ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 2391 ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 2392 kmem_cache_free(hdr_full_cache, hdr); 2393 } else { 2394 kmem_cache_free(hdr_l2only_cache, hdr); 2395 } 2396} 2397 2398void 2399arc_buf_free(arc_buf_t *buf, void *tag) 2400{ 2401 arc_buf_hdr_t *hdr = buf->b_hdr; 2402 int hashed = hdr->b_l1hdr.b_state != arc_anon; 2403 2404 ASSERT(buf->b_efunc == NULL); 2405 ASSERT(buf->b_data != NULL); 2406 2407 if (hashed) { 2408 kmutex_t *hash_lock = HDR_LOCK(hdr); 2409 2410 mutex_enter(hash_lock); 2411 hdr = buf->b_hdr; 2412 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 2413 2414 (void) remove_reference(hdr, hash_lock, tag); 2415 if (hdr->b_l1hdr.b_datacnt > 1) { 2416 arc_buf_destroy(buf, TRUE); 2417 } else { 2418 ASSERT(buf == hdr->b_l1hdr.b_buf); 2419 ASSERT(buf->b_efunc == NULL); 2420 hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; 2421 } 2422 mutex_exit(hash_lock); 2423 } else if (HDR_IO_IN_PROGRESS(hdr)) { 2424 int destroy_hdr; 2425 /* 2426 * We are in the middle of an async write. Don't destroy 2427 * this buffer unless the write completes before we finish 2428 * decrementing the reference count. 2429 */ 2430 mutex_enter(&arc_user_evicts_lock); 2431 (void) remove_reference(hdr, NULL, tag); 2432 ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2433 destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 2434 mutex_exit(&arc_user_evicts_lock); 2435 if (destroy_hdr) 2436 arc_hdr_destroy(hdr); 2437 } else { 2438 if (remove_reference(hdr, NULL, tag) > 0) 2439 arc_buf_destroy(buf, TRUE); 2440 else 2441 arc_hdr_destroy(hdr); 2442 } 2443} 2444 2445boolean_t 2446arc_buf_remove_ref(arc_buf_t *buf, void* tag) 2447{ 2448 arc_buf_hdr_t *hdr = buf->b_hdr; 2449 kmutex_t *hash_lock = HDR_LOCK(hdr); 2450 boolean_t no_callback = (buf->b_efunc == NULL); 2451 2452 if (hdr->b_l1hdr.b_state == arc_anon) { 2453 ASSERT(hdr->b_l1hdr.b_datacnt == 1); 2454 arc_buf_free(buf, tag); 2455 return (no_callback); 2456 } 2457 2458 mutex_enter(hash_lock); 2459 hdr = buf->b_hdr; 2460 ASSERT(hdr->b_l1hdr.b_datacnt > 0); 2461 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 2462 ASSERT(hdr->b_l1hdr.b_state != arc_anon); 2463 ASSERT(buf->b_data != NULL); 2464 2465 (void) remove_reference(hdr, hash_lock, tag); 2466 if (hdr->b_l1hdr.b_datacnt > 1) { 2467 if (no_callback) 2468 arc_buf_destroy(buf, TRUE); 2469 } else if (no_callback) { 2470 ASSERT(hdr->b_l1hdr.b_buf == buf && buf->b_next == NULL); 2471 ASSERT(buf->b_efunc == NULL); 2472 hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; 2473 } 2474 ASSERT(no_callback || hdr->b_l1hdr.b_datacnt > 1 || 2475 refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2476 mutex_exit(hash_lock); 2477 return (no_callback); 2478} 2479 2480int32_t 2481arc_buf_size(arc_buf_t *buf) 2482{ 2483 return (buf->b_hdr->b_size); 2484} 2485 2486/* 2487 * Called from the DMU to determine if the current buffer should be 2488 * evicted. In order to ensure proper locking, the eviction must be initiated 2489 * from the DMU. Return true if the buffer is associated with user data and 2490 * duplicate buffers still exist. 2491 */ 2492boolean_t 2493arc_buf_eviction_needed(arc_buf_t *buf) 2494{ 2495 arc_buf_hdr_t *hdr; 2496 boolean_t evict_needed = B_FALSE; 2497 2498 if (zfs_disable_dup_eviction) 2499 return (B_FALSE); 2500 2501 mutex_enter(&buf->b_evict_lock); 2502 hdr = buf->b_hdr; 2503 if (hdr == NULL) { 2504 /* 2505 * We are in arc_do_user_evicts(); let that function 2506 * perform the eviction. 2507 */ 2508 ASSERT(buf->b_data == NULL); 2509 mutex_exit(&buf->b_evict_lock); 2510 return (B_FALSE); 2511 } else if (buf->b_data == NULL) { 2512 /* 2513 * We have already been added to the arc eviction list; 2514 * recommend eviction. 2515 */ 2516 ASSERT3P(hdr, ==, &arc_eviction_hdr); 2517 mutex_exit(&buf->b_evict_lock); 2518 return (B_TRUE); 2519 } 2520 2521 if (hdr->b_l1hdr.b_datacnt > 1 && HDR_ISTYPE_DATA(hdr)) 2522 evict_needed = B_TRUE; 2523 2524 mutex_exit(&buf->b_evict_lock); 2525 return (evict_needed); 2526} 2527 2528/* 2529 * Evict the arc_buf_hdr that is provided as a parameter. The resultant 2530 * state of the header is dependent on it's state prior to entering this 2531 * function. The following transitions are possible: 2532 * 2533 * - arc_mru -> arc_mru_ghost 2534 * - arc_mfu -> arc_mfu_ghost 2535 * - arc_mru_ghost -> arc_l2c_only 2536 * - arc_mru_ghost -> deleted 2537 * - arc_mfu_ghost -> arc_l2c_only 2538 * - arc_mfu_ghost -> deleted 2539 */ 2540static int64_t 2541arc_evict_hdr(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) 2542{ 2543 arc_state_t *evicted_state, *state; 2544 int64_t bytes_evicted = 0; 2545 2546 ASSERT(MUTEX_HELD(hash_lock)); 2547 ASSERT(HDR_HAS_L1HDR(hdr)); 2548 2549 state = hdr->b_l1hdr.b_state; 2550 if (GHOST_STATE(state)) { 2551 ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 2552 ASSERT(hdr->b_l1hdr.b_buf == NULL); 2553 2554 /* 2555 * l2arc_write_buffers() relies on a header's L1 portion 2556 * (i.e. it's b_tmp_cdata field) during it's write phase. 2557 * Thus, we cannot push a header onto the arc_l2c_only 2558 * state (removing it's L1 piece) until the header is 2559 * done being written to the l2arc. 2560 */ 2561 if (HDR_HAS_L2HDR(hdr) && HDR_L2_WRITING(hdr)) { 2562 ARCSTAT_BUMP(arcstat_evict_l2_skip); 2563 return (bytes_evicted); 2564 } 2565 2566 ARCSTAT_BUMP(arcstat_deleted); 2567 bytes_evicted += hdr->b_size; 2568 2569 DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, hdr); 2570 2571 if (HDR_HAS_L2HDR(hdr)) { 2572 /* 2573 * This buffer is cached on the 2nd Level ARC; 2574 * don't destroy the header. 2575 */ 2576 arc_change_state(arc_l2c_only, hdr, hash_lock); 2577 /* 2578 * dropping from L1+L2 cached to L2-only, 2579 * realloc to remove the L1 header. 2580 */ 2581 hdr = arc_hdr_realloc(hdr, hdr_full_cache, 2582 hdr_l2only_cache); 2583 } else { 2584 arc_change_state(arc_anon, hdr, hash_lock); 2585 arc_hdr_destroy(hdr); 2586 } 2587 return (bytes_evicted); 2588 } 2589 2590 ASSERT(state == arc_mru || state == arc_mfu); 2591 evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 2592 2593 /* prefetch buffers have a minimum lifespan */ 2594 if (HDR_IO_IN_PROGRESS(hdr) || 2595 ((hdr->b_flags & (ARC_FLAG_PREFETCH | ARC_FLAG_INDIRECT)) && 2596 ddi_get_lbolt() - hdr->b_l1hdr.b_arc_access < 2597 arc_min_prefetch_lifespan)) { 2598 ARCSTAT_BUMP(arcstat_evict_skip); 2599 return (bytes_evicted); 2600 } 2601 2602 ASSERT0(refcount_count(&hdr->b_l1hdr.b_refcnt)); 2603 ASSERT3U(hdr->b_l1hdr.b_datacnt, >, 0); 2604 while (hdr->b_l1hdr.b_buf) { 2605 arc_buf_t *buf = hdr->b_l1hdr.b_buf; 2606 if (!mutex_tryenter(&buf->b_evict_lock)) { 2607 ARCSTAT_BUMP(arcstat_mutex_miss); 2608 break; 2609 } 2610 if (buf->b_data != NULL) 2611 bytes_evicted += hdr->b_size; 2612 if (buf->b_efunc != NULL) { 2613 mutex_enter(&arc_user_evicts_lock); 2614 arc_buf_destroy(buf, FALSE); 2615 hdr->b_l1hdr.b_buf = buf->b_next; 2616 buf->b_hdr = &arc_eviction_hdr; 2617 buf->b_next = arc_eviction_list; 2618 arc_eviction_list = buf; 2619 cv_signal(&arc_user_evicts_cv); 2620 mutex_exit(&arc_user_evicts_lock); 2621 mutex_exit(&buf->b_evict_lock); 2622 } else { 2623 mutex_exit(&buf->b_evict_lock); 2624 arc_buf_destroy(buf, TRUE); 2625 } 2626 } 2627 2628 if (HDR_HAS_L2HDR(hdr)) { 2629 ARCSTAT_INCR(arcstat_evict_l2_cached, hdr->b_size); 2630 } else { 2631 if (l2arc_write_eligible(hdr->b_spa, hdr)) 2632 ARCSTAT_INCR(arcstat_evict_l2_eligible, hdr->b_size); 2633 else 2634 ARCSTAT_INCR(arcstat_evict_l2_ineligible, hdr->b_size); 2635 } 2636 2637 if (hdr->b_l1hdr.b_datacnt == 0) { 2638 arc_change_state(evicted_state, hdr, hash_lock); 2639 ASSERT(HDR_IN_HASH_TABLE(hdr)); 2640 hdr->b_flags |= ARC_FLAG_IN_HASH_TABLE; 2641 hdr->b_flags &= ~ARC_FLAG_BUF_AVAILABLE; 2642 DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, hdr); 2643 } 2644 2645 return (bytes_evicted); 2646} 2647 2648static uint64_t 2649arc_evict_state_impl(multilist_t *ml, int idx, arc_buf_hdr_t *marker, 2650 uint64_t spa, int64_t bytes) 2651{ 2652 multilist_sublist_t *mls; 2653 uint64_t bytes_evicted = 0; 2654 arc_buf_hdr_t *hdr; 2655 kmutex_t *hash_lock; 2656 int evict_count = 0; 2657 2658 ASSERT3P(marker, !=, NULL); 2659 IMPLY(bytes < 0, bytes == ARC_EVICT_ALL); 2660 2661 mls = multilist_sublist_lock(ml, idx); 2662 2663 for (hdr = multilist_sublist_prev(mls, marker); hdr != NULL; 2664 hdr = multilist_sublist_prev(mls, marker)) { 2665 if ((bytes != ARC_EVICT_ALL && bytes_evicted >= bytes) || 2666 (evict_count >= zfs_arc_evict_batch_limit)) 2667 break; 2668 2669 /* 2670 * To keep our iteration location, move the marker 2671 * forward. Since we're not holding hdr's hash lock, we 2672 * must be very careful and not remove 'hdr' from the 2673 * sublist. Otherwise, other consumers might mistake the 2674 * 'hdr' as not being on a sublist when they call the 2675 * multilist_link_active() function (they all rely on 2676 * the hash lock protecting concurrent insertions and 2677 * removals). multilist_sublist_move_forward() was 2678 * specifically implemented to ensure this is the case 2679 * (only 'marker' will be removed and re-inserted). 2680 */ 2681 multilist_sublist_move_forward(mls, marker); 2682 2683 /* 2684 * The only case where the b_spa field should ever be 2685 * zero, is the marker headers inserted by 2686 * arc_evict_state(). It's possible for multiple threads 2687 * to be calling arc_evict_state() concurrently (e.g. 2688 * dsl_pool_close() and zio_inject_fault()), so we must 2689 * skip any markers we see from these other threads. 2690 */ 2691 if (hdr->b_spa == 0) 2692 continue; 2693 2694 /* we're only interested in evicting buffers of a certain spa */ 2695 if (spa != 0 && hdr->b_spa != spa) { 2696 ARCSTAT_BUMP(arcstat_evict_skip); 2697 continue; 2698 } 2699 2700 hash_lock = HDR_LOCK(hdr); 2701 2702 /* 2703 * We aren't calling this function from any code path 2704 * that would already be holding a hash lock, so we're 2705 * asserting on this assumption to be defensive in case 2706 * this ever changes. Without this check, it would be 2707 * possible to incorrectly increment arcstat_mutex_miss 2708 * below (e.g. if the code changed such that we called 2709 * this function with a hash lock held). 2710 */ 2711 ASSERT(!MUTEX_HELD(hash_lock)); 2712 2713 if (mutex_tryenter(hash_lock)) { 2714 uint64_t evicted = arc_evict_hdr(hdr, hash_lock); 2715 mutex_exit(hash_lock); 2716 2717 bytes_evicted += evicted; 2718 2719 /* 2720 * If evicted is zero, arc_evict_hdr() must have 2721 * decided to skip this header, don't increment 2722 * evict_count in this case. 2723 */ 2724 if (evicted != 0) 2725 evict_count++; 2726 2727 /* 2728 * If arc_size isn't overflowing, signal any 2729 * threads that might happen to be waiting. 2730 * 2731 * For each header evicted, we wake up a single 2732 * thread. If we used cv_broadcast, we could 2733 * wake up "too many" threads causing arc_size 2734 * to significantly overflow arc_c; since 2735 * arc_get_data_buf() doesn't check for overflow 2736 * when it's woken up (it doesn't because it's 2737 * possible for the ARC to be overflowing while 2738 * full of un-evictable buffers, and the 2739 * function should proceed in this case). 2740 * 2741 * If threads are left sleeping, due to not 2742 * using cv_broadcast, they will be woken up 2743 * just before arc_reclaim_thread() sleeps. 2744 */ 2745 mutex_enter(&arc_reclaim_lock); 2746 if (!arc_is_overflowing()) 2747 cv_signal(&arc_reclaim_waiters_cv); 2748 mutex_exit(&arc_reclaim_lock); 2749 } else { 2750 ARCSTAT_BUMP(arcstat_mutex_miss); 2751 } 2752 } 2753 2754 multilist_sublist_unlock(mls); 2755 2756 return (bytes_evicted); 2757} 2758 2759/* 2760 * Evict buffers from the given arc state, until we've removed the 2761 * specified number of bytes. Move the removed buffers to the 2762 * appropriate evict state. 2763 * 2764 * This function makes a "best effort". It skips over any buffers 2765 * it can't get a hash_lock on, and so, may not catch all candidates. 2766 * It may also return without evicting as much space as requested. 2767 * 2768 * If bytes is specified using the special value ARC_EVICT_ALL, this 2769 * will evict all available (i.e. unlocked and evictable) buffers from 2770 * the given arc state; which is used by arc_flush(). 2771 */ 2772static uint64_t 2773arc_evict_state(arc_state_t *state, uint64_t spa, int64_t bytes, 2774 arc_buf_contents_t type) 2775{ 2776 uint64_t total_evicted = 0; 2777 multilist_t *ml = &state->arcs_list[type]; 2778 int num_sublists; 2779 arc_buf_hdr_t **markers; 2780 2781 IMPLY(bytes < 0, bytes == ARC_EVICT_ALL); 2782 2783 num_sublists = multilist_get_num_sublists(ml); 2784 2785 /* 2786 * If we've tried to evict from each sublist, made some 2787 * progress, but still have not hit the target number of bytes 2788 * to evict, we want to keep trying. The markers allow us to 2789 * pick up where we left off for each individual sublist, rather 2790 * than starting from the tail each time. 2791 */ 2792 markers = kmem_zalloc(sizeof (*markers) * num_sublists, KM_SLEEP); 2793 for (int i = 0; i < num_sublists; i++) { 2794 markers[i] = kmem_cache_alloc(hdr_full_cache, KM_SLEEP); 2795 2796 /* 2797 * A b_spa of 0 is used to indicate that this header is 2798 * a marker. This fact is used in arc_adjust_type() and 2799 * arc_evict_state_impl(). 2800 */ 2801 markers[i]->b_spa = 0; 2802 2803 multilist_sublist_t *mls = multilist_sublist_lock(ml, i); 2804 multilist_sublist_insert_tail(mls, markers[i]); 2805 multilist_sublist_unlock(mls); 2806 } 2807 2808 /* 2809 * While we haven't hit our target number of bytes to evict, or 2810 * we're evicting all available buffers. 2811 */ 2812 while (total_evicted < bytes || bytes == ARC_EVICT_ALL) { 2813 /* 2814 * Start eviction using a randomly selected sublist, 2815 * this is to try and evenly balance eviction across all 2816 * sublists. Always starting at the same sublist 2817 * (e.g. index 0) would cause evictions to favor certain 2818 * sublists over others. 2819 */ 2820 int sublist_idx = multilist_get_random_index(ml); 2821 uint64_t scan_evicted = 0; 2822 2823 for (int i = 0; i < num_sublists; i++) { 2824 uint64_t bytes_remaining; 2825 uint64_t bytes_evicted; 2826 2827 if (bytes == ARC_EVICT_ALL) 2828 bytes_remaining = ARC_EVICT_ALL; 2829 else if (total_evicted < bytes) 2830 bytes_remaining = bytes - total_evicted; 2831 else 2832 break; 2833 2834 bytes_evicted = arc_evict_state_impl(ml, sublist_idx, 2835 markers[sublist_idx], spa, bytes_remaining); 2836 2837 scan_evicted += bytes_evicted; 2838 total_evicted += bytes_evicted; 2839 2840 /* we've reached the end, wrap to the beginning */ 2841 if (++sublist_idx >= num_sublists) 2842 sublist_idx = 0; 2843 } 2844 2845 /* 2846 * If we didn't evict anything during this scan, we have 2847 * no reason to believe we'll evict more during another 2848 * scan, so break the loop. 2849 */ 2850 if (scan_evicted == 0) { 2851 /* This isn't possible, let's make that obvious */ 2852 ASSERT3S(bytes, !=, 0); 2853 2854 /* 2855 * When bytes is ARC_EVICT_ALL, the only way to 2856 * break the loop is when scan_evicted is zero. 2857 * In that case, we actually have evicted enough, 2858 * so we don't want to increment the kstat. 2859 */ 2860 if (bytes != ARC_EVICT_ALL) { 2861 ASSERT3S(total_evicted, <, bytes); 2862 ARCSTAT_BUMP(arcstat_evict_not_enough); 2863 } 2864 2865 break; 2866 } 2867 } 2868 2869 for (int i = 0; i < num_sublists; i++) { 2870 multilist_sublist_t *mls = multilist_sublist_lock(ml, i); 2871 multilist_sublist_remove(mls, markers[i]); 2872 multilist_sublist_unlock(mls); 2873 2874 kmem_cache_free(hdr_full_cache, markers[i]); 2875 } 2876 kmem_free(markers, sizeof (*markers) * num_sublists); 2877 2878 return (total_evicted); 2879} 2880 2881/* 2882 * Flush all "evictable" data of the given type from the arc state 2883 * specified. This will not evict any "active" buffers (i.e. referenced). 2884 * 2885 * When 'retry' is set to FALSE, the function will make a single pass 2886 * over the state and evict any buffers that it can. Since it doesn't 2887 * continually retry the eviction, it might end up leaving some buffers 2888 * in the ARC due to lock misses. 2889 * 2890 * When 'retry' is set to TRUE, the function will continually retry the 2891 * eviction until *all* evictable buffers have been removed from the 2892 * state. As a result, if concurrent insertions into the state are 2893 * allowed (e.g. if the ARC isn't shutting down), this function might 2894 * wind up in an infinite loop, continually trying to evict buffers. 2895 */ 2896static uint64_t 2897arc_flush_state(arc_state_t *state, uint64_t spa, arc_buf_contents_t type, 2898 boolean_t retry) 2899{ 2900 uint64_t evicted = 0; 2901 2902 while (state->arcs_lsize[type] != 0) { 2903 evicted += arc_evict_state(state, spa, ARC_EVICT_ALL, type); 2904 2905 if (!retry) 2906 break; 2907 } 2908 2909 return (evicted); 2910} 2911 2912/* 2913 * Evict the specified number of bytes from the state specified, 2914 * restricting eviction to the spa and type given. This function 2915 * prevents us from trying to evict more from a state's list than 2916 * is "evictable", and to skip evicting altogether when passed a 2917 * negative value for "bytes". In contrast, arc_evict_state() will 2918 * evict everything it can, when passed a negative value for "bytes". 2919 */ 2920static uint64_t 2921arc_adjust_impl(arc_state_t *state, uint64_t spa, int64_t bytes, 2922 arc_buf_contents_t type) 2923{ 2924 int64_t delta; 2925 2926 if (bytes > 0 && state->arcs_lsize[type] > 0) { 2927 delta = MIN(state->arcs_lsize[type], bytes); 2928 return (arc_evict_state(state, spa, delta, type)); 2929 } 2930 2931 return (0); 2932} 2933 2934/* 2935 * Evict metadata buffers from the cache, such that arc_meta_used is 2936 * capped by the arc_meta_limit tunable. 2937 */ 2938static uint64_t 2939arc_adjust_meta(void) 2940{ 2941 uint64_t total_evicted = 0; 2942 int64_t target; 2943 2944 /* 2945 * If we're over the meta limit, we want to evict enough 2946 * metadata to get back under the meta limit. We don't want to 2947 * evict so much that we drop the MRU below arc_p, though. If 2948 * we're over the meta limit more than we're over arc_p, we 2949 * evict some from the MRU here, and some from the MFU below. 2950 */ 2951 target = MIN((int64_t)(arc_meta_used - arc_meta_limit), 2952 (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size - arc_p)); 2953 2954 total_evicted += arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); 2955 2956 /* 2957 * Similar to the above, we want to evict enough bytes to get us 2958 * below the meta limit, but not so much as to drop us below the 2959 * space alloted to the MFU (which is defined as arc_c - arc_p). 2960 */ 2961 target = MIN((int64_t)(arc_meta_used - arc_meta_limit), 2962 (int64_t)(arc_mfu->arcs_size - (arc_c - arc_p))); 2963 2964 total_evicted += arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); 2965 2966 return (total_evicted); 2967} 2968 2969/* 2970 * Return the type of the oldest buffer in the given arc state 2971 * 2972 * This function will select a random sublist of type ARC_BUFC_DATA and 2973 * a random sublist of type ARC_BUFC_METADATA. The tail of each sublist 2974 * is compared, and the type which contains the "older" buffer will be 2975 * returned. 2976 */ 2977static arc_buf_contents_t 2978arc_adjust_type(arc_state_t *state) 2979{ 2980 multilist_t *data_ml = &state->arcs_list[ARC_BUFC_DATA]; 2981 multilist_t *meta_ml = &state->arcs_list[ARC_BUFC_METADATA]; 2982 int data_idx = multilist_get_random_index(data_ml); 2983 int meta_idx = multilist_get_random_index(meta_ml); 2984 multilist_sublist_t *data_mls; 2985 multilist_sublist_t *meta_mls; 2986 arc_buf_contents_t type; 2987 arc_buf_hdr_t *data_hdr; 2988 arc_buf_hdr_t *meta_hdr; 2989 2990 /* 2991 * We keep the sublist lock until we're finished, to prevent 2992 * the headers from being destroyed via arc_evict_state(). 2993 */ 2994 data_mls = multilist_sublist_lock(data_ml, data_idx); 2995 meta_mls = multilist_sublist_lock(meta_ml, meta_idx); 2996 2997 /* 2998 * These two loops are to ensure we skip any markers that 2999 * might be at the tail of the lists due to arc_evict_state(). 3000 */ 3001 3002 for (data_hdr = multilist_sublist_tail(data_mls); data_hdr != NULL; 3003 data_hdr = multilist_sublist_prev(data_mls, data_hdr)) { 3004 if (data_hdr->b_spa != 0) 3005 break; 3006 } 3007 3008 for (meta_hdr = multilist_sublist_tail(meta_mls); meta_hdr != NULL; 3009 meta_hdr = multilist_sublist_prev(meta_mls, meta_hdr)) { 3010 if (meta_hdr->b_spa != 0) 3011 break; 3012 } 3013 3014 if (data_hdr == NULL && meta_hdr == NULL) { 3015 type = ARC_BUFC_DATA; 3016 } else if (data_hdr == NULL) { 3017 ASSERT3P(meta_hdr, !=, NULL); 3018 type = ARC_BUFC_METADATA; 3019 } else if (meta_hdr == NULL) { 3020 ASSERT3P(data_hdr, !=, NULL); 3021 type = ARC_BUFC_DATA; 3022 } else { 3023 ASSERT3P(data_hdr, !=, NULL); 3024 ASSERT3P(meta_hdr, !=, NULL); 3025 3026 /* The headers can't be on the sublist without an L1 header */ 3027 ASSERT(HDR_HAS_L1HDR(data_hdr)); 3028 ASSERT(HDR_HAS_L1HDR(meta_hdr)); 3029 3030 if (data_hdr->b_l1hdr.b_arc_access < 3031 meta_hdr->b_l1hdr.b_arc_access) { 3032 type = ARC_BUFC_DATA; 3033 } else { 3034 type = ARC_BUFC_METADATA; 3035 } 3036 } 3037 3038 multilist_sublist_unlock(meta_mls); 3039 multilist_sublist_unlock(data_mls); 3040 3041 return (type); 3042} 3043 3044/* 3045 * Evict buffers from the cache, such that arc_size is capped by arc_c. 3046 */ 3047static uint64_t 3048arc_adjust(void) 3049{ 3050 uint64_t total_evicted = 0; 3051 uint64_t bytes; 3052 int64_t target; 3053 3054 /* 3055 * If we're over arc_meta_limit, we want to correct that before 3056 * potentially evicting data buffers below. 3057 */ 3058 total_evicted += arc_adjust_meta(); 3059 3060 /* 3061 * Adjust MRU size 3062 * 3063 * If we're over the target cache size, we want to evict enough 3064 * from the list to get back to our target size. We don't want 3065 * to evict too much from the MRU, such that it drops below 3066 * arc_p. So, if we're over our target cache size more than 3067 * the MRU is over arc_p, we'll evict enough to get back to 3068 * arc_p here, and then evict more from the MFU below. 3069 */ 3070 target = MIN((int64_t)(arc_size - arc_c), 3071 (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used - 3072 arc_p)); 3073 3074 /* 3075 * If we're below arc_meta_min, always prefer to evict data. 3076 * Otherwise, try to satisfy the requested number of bytes to 3077 * evict from the type which contains older buffers; in an 3078 * effort to keep newer buffers in the cache regardless of their 3079 * type. If we cannot satisfy the number of bytes from this 3080 * type, spill over into the next type. 3081 */ 3082 if (arc_adjust_type(arc_mru) == ARC_BUFC_METADATA && 3083 arc_meta_used > arc_meta_min) { 3084 bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); 3085 total_evicted += bytes; 3086 3087 /* 3088 * If we couldn't evict our target number of bytes from 3089 * metadata, we try to get the rest from data. 3090 */ 3091 target -= bytes; 3092 3093 total_evicted += 3094 arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA); 3095 } else { 3096 bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA); 3097 total_evicted += bytes; 3098 3099 /* 3100 * If we couldn't evict our target number of bytes from 3101 * data, we try to get the rest from metadata. 3102 */ 3103 target -= bytes; 3104 3105 total_evicted += 3106 arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); 3107 } 3108 3109 /* 3110 * Adjust MFU size 3111 * 3112 * Now that we've tried to evict enough from the MRU to get its 3113 * size back to arc_p, if we're still above the target cache 3114 * size, we evict the rest from the MFU. 3115 */ 3116 target = arc_size - arc_c; 3117 3118 if (arc_adjust_type(arc_mfu) == ARC_BUFC_METADATA && 3119 arc_meta_used > arc_meta_min) { 3120 bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); 3121 total_evicted += bytes; 3122 3123 /* 3124 * If we couldn't evict our target number of bytes from 3125 * metadata, we try to get the rest from data. 3126 */ 3127 target -= bytes; 3128 3129 total_evicted += 3130 arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA); 3131 } else { 3132 bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA); 3133 total_evicted += bytes; 3134 3135 /* 3136 * If we couldn't evict our target number of bytes from 3137 * data, we try to get the rest from data. 3138 */ 3139 target -= bytes; 3140 3141 total_evicted += 3142 arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); 3143 } 3144 3145 /* 3146 * Adjust ghost lists 3147 * 3148 * In addition to the above, the ARC also defines target values 3149 * for the ghost lists. The sum of the mru list and mru ghost 3150 * list should never exceed the target size of the cache, and 3151 * the sum of the mru list, mfu list, mru ghost list, and mfu 3152 * ghost list should never exceed twice the target size of the 3153 * cache. The following logic enforces these limits on the ghost 3154 * caches, and evicts from them as needed. 3155 */ 3156 target = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c; 3157 3158 bytes = arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_DATA); 3159 total_evicted += bytes; 3160 3161 target -= bytes; 3162 3163 total_evicted += 3164 arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_METADATA); 3165 3166 /* 3167 * We assume the sum of the mru list and mfu list is less than 3168 * or equal to arc_c (we enforced this above), which means we 3169 * can use the simpler of the two equations below: 3170 * 3171 * mru + mfu + mru ghost + mfu ghost <= 2 * arc_c 3172 * mru ghost + mfu ghost <= arc_c 3173 */ 3174 target = arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c; 3175 3176 bytes = arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_DATA); 3177 total_evicted += bytes; 3178 3179 target -= bytes; 3180 3181 total_evicted += 3182 arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_METADATA); 3183 3184 return (total_evicted); 3185} 3186 3187static void 3188arc_do_user_evicts(void) 3189{ 3190 mutex_enter(&arc_user_evicts_lock); 3191 while (arc_eviction_list != NULL) { 3192 arc_buf_t *buf = arc_eviction_list; 3193 arc_eviction_list = buf->b_next; 3194 mutex_enter(&buf->b_evict_lock); 3195 buf->b_hdr = NULL; 3196 mutex_exit(&buf->b_evict_lock); 3197 mutex_exit(&arc_user_evicts_lock); 3198 3199 if (buf->b_efunc != NULL) 3200 VERIFY0(buf->b_efunc(buf->b_private)); 3201 3202 buf->b_efunc = NULL; 3203 buf->b_private = NULL; 3204 kmem_cache_free(buf_cache, buf); 3205 mutex_enter(&arc_user_evicts_lock); 3206 } 3207 mutex_exit(&arc_user_evicts_lock); 3208} 3209 3210void 3211arc_flush(spa_t *spa, boolean_t retry) 3212{ 3213 uint64_t guid = 0; 3214 3215 /* 3216 * If retry is TRUE, a spa must not be specified since we have 3217 * no good way to determine if all of a spa's buffers have been 3218 * evicted from an arc state. 3219 */ 3220 ASSERT(!retry || spa == 0); 3221 3222 if (spa != NULL) 3223 guid = spa_load_guid(spa); 3224 3225 (void) arc_flush_state(arc_mru, guid, ARC_BUFC_DATA, retry); 3226 (void) arc_flush_state(arc_mru, guid, ARC_BUFC_METADATA, retry); 3227 3228 (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_DATA, retry); 3229 (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_METADATA, retry); 3230 3231 (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_DATA, retry); 3232 (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_METADATA, retry); 3233 3234 (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_DATA, retry); 3235 (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_METADATA, retry); 3236 3237 arc_do_user_evicts(); 3238 ASSERT(spa || arc_eviction_list == NULL); 3239} 3240 3241void 3242arc_shrink(int64_t to_free) 3243{ 3244 if (arc_c > arc_c_min) { 3245 DTRACE_PROBE4(arc__shrink, uint64_t, arc_c, uint64_t, 3246 arc_c_min, uint64_t, arc_p, uint64_t, to_free); 3247 if (arc_c > arc_c_min + to_free) 3248 atomic_add_64(&arc_c, -to_free); 3249 else 3250 arc_c = arc_c_min; 3251 3252 atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 3253 if (arc_c > arc_size) 3254 arc_c = MAX(arc_size, arc_c_min); 3255 if (arc_p > arc_c) 3256 arc_p = (arc_c >> 1); 3257 3258 DTRACE_PROBE2(arc__shrunk, uint64_t, arc_c, uint64_t, 3259 arc_p); 3260 3261 ASSERT(arc_c >= arc_c_min); 3262 ASSERT((int64_t)arc_p >= 0); 3263 } 3264 3265 if (arc_size > arc_c) { 3266 DTRACE_PROBE2(arc__shrink_adjust, uint64_t, arc_size, 3267 uint64_t, arc_c); 3268 (void) arc_adjust(); 3269 } 3270} 3271 3272static long needfree = 0; 3273 3274typedef enum free_memory_reason_t { 3275 FMR_UNKNOWN, 3276 FMR_NEEDFREE, 3277 FMR_LOTSFREE, 3278 FMR_SWAPFS_MINFREE, 3279 FMR_PAGES_PP_MAXIMUM, 3280 FMR_HEAP_ARENA, 3281 FMR_ZIO_ARENA, 3282 FMR_ZIO_FRAG, 3283} free_memory_reason_t; 3284 3285int64_t last_free_memory; 3286free_memory_reason_t last_free_reason; 3287 3288/* 3289 * Additional reserve of pages for pp_reserve. 3290 */ 3291int64_t arc_pages_pp_reserve = 64; 3292 3293/* 3294 * Additional reserve of pages for swapfs. 3295 */ 3296int64_t arc_swapfs_reserve = 64; 3297 3298/* 3299 * Return the amount of memory that can be consumed before reclaim will be 3300 * needed. Positive if there is sufficient free memory, negative indicates 3301 * the amount of memory that needs to be freed up. 3302 */ 3303static int64_t 3304arc_available_memory(void) 3305{ 3306 int64_t lowest = INT64_MAX; 3307 int64_t n; 3308 free_memory_reason_t r = FMR_UNKNOWN; 3309 3310#ifdef _KERNEL 3311 if (needfree > 0) { 3312 n = PAGESIZE * (-needfree); 3313 if (n < lowest) { 3314 lowest = n; 3315 r = FMR_NEEDFREE; 3316 } 3317 } 3318 3319 /* 3320 * Cooperate with pagedaemon when it's time for it to scan 3321 * and reclaim some pages. 3322 */ 3323 n = PAGESIZE * ((int64_t)freemem - zfs_arc_free_target); 3324 if (n < lowest) { 3325 lowest = n; 3326 r = FMR_LOTSFREE; 3327 } 3328 3329#ifdef illumos 3330 /* 3331 * check that we're out of range of the pageout scanner. It starts to 3332 * schedule paging if freemem is less than lotsfree and needfree. 3333 * lotsfree is the high-water mark for pageout, and needfree is the 3334 * number of needed free pages. We add extra pages here to make sure 3335 * the scanner doesn't start up while we're freeing memory. 3336 */ 3337 n = PAGESIZE * (freemem - lotsfree - needfree - desfree); 3338 if (n < lowest) { 3339 lowest = n; 3340 r = FMR_LOTSFREE; 3341 } 3342 3343 /* 3344 * check to make sure that swapfs has enough space so that anon 3345 * reservations can still succeed. anon_resvmem() checks that the 3346 * availrmem is greater than swapfs_minfree, and the number of reserved 3347 * swap pages. We also add a bit of extra here just to prevent 3348 * circumstances from getting really dire. 3349 */ 3350 n = PAGESIZE * (availrmem - swapfs_minfree - swapfs_reserve - 3351 desfree - arc_swapfs_reserve); 3352 if (n < lowest) { 3353 lowest = n; 3354 r = FMR_SWAPFS_MINFREE; 3355 } 3356 3357 3358 /* 3359 * Check that we have enough availrmem that memory locking (e.g., via 3360 * mlock(3C) or memcntl(2)) can still succeed. (pages_pp_maximum 3361 * stores the number of pages that cannot be locked; when availrmem 3362 * drops below pages_pp_maximum, page locking mechanisms such as 3363 * page_pp_lock() will fail.) 3364 */ 3365 n = PAGESIZE * (availrmem - pages_pp_maximum - 3366 arc_pages_pp_reserve); 3367 if (n < lowest) { 3368 lowest = n; 3369 r = FMR_PAGES_PP_MAXIMUM; 3370 } 3371 3372#endif /* illumos */ 3373#if defined(__i386) || !defined(UMA_MD_SMALL_ALLOC) 3374 /* 3375 * If we're on an i386 platform, it's possible that we'll exhaust the 3376 * kernel heap space before we ever run out of available physical 3377 * memory. Most checks of the size of the heap_area compare against 3378 * tune.t_minarmem, which is the minimum available real memory that we 3379 * can have in the system. However, this is generally fixed at 25 pages 3380 * which is so low that it's useless. In this comparison, we seek to 3381 * calculate the total heap-size, and reclaim if more than 3/4ths of the 3382 * heap is allocated. (Or, in the calculation, if less than 1/4th is 3383 * free) 3384 */ 3385 n = (int64_t)vmem_size(heap_arena, VMEM_FREE) - 3386 (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC) >> 2); 3387 if (n < lowest) { 3388 lowest = n; 3389 r = FMR_HEAP_ARENA; 3390 } 3391#define zio_arena NULL 3392#else 3393#define zio_arena heap_arena 3394#endif 3395 3396 /* 3397 * If zio data pages are being allocated out of a separate heap segment, 3398 * then enforce that the size of available vmem for this arena remains 3399 * above about 1/16th free. 3400 * 3401 * Note: The 1/16th arena free requirement was put in place 3402 * to aggressively evict memory from the arc in order to avoid 3403 * memory fragmentation issues. 3404 */ 3405 if (zio_arena != NULL) { 3406 n = (int64_t)vmem_size(zio_arena, VMEM_FREE) - 3407 (vmem_size(zio_arena, VMEM_ALLOC) >> 4); 3408 if (n < lowest) { 3409 lowest = n; 3410 r = FMR_ZIO_ARENA; 3411 } 3412 } 3413 3414 /* 3415 * Above limits know nothing about real level of KVA fragmentation. 3416 * Start aggressive reclamation if too little sequential KVA left. 3417 */ 3418 if (lowest > 0) { 3419 n = (vmem_size(heap_arena, VMEM_MAXFREE) < zfs_max_recordsize) ? 3420 -((int64_t)vmem_size(heap_arena, VMEM_ALLOC) >> 4) : 3421 INT64_MAX; 3422 if (n < lowest) { 3423 lowest = n; 3424 r = FMR_ZIO_FRAG; 3425 } 3426 } 3427 3428#else /* _KERNEL */ 3429 /* Every 100 calls, free a small amount */ 3430 if (spa_get_random(100) == 0) 3431 lowest = -1024; 3432#endif /* _KERNEL */ 3433 3434 last_free_memory = lowest; 3435 last_free_reason = r; 3436 DTRACE_PROBE2(arc__available_memory, int64_t, lowest, int, r); 3437 return (lowest); 3438} 3439 3440 3441/* 3442 * Determine if the system is under memory pressure and is asking 3443 * to reclaim memory. A return value of TRUE indicates that the system 3444 * is under memory pressure and that the arc should adjust accordingly. 3445 */ 3446static boolean_t 3447arc_reclaim_needed(void) 3448{ 3449 return (arc_available_memory() < 0); 3450} 3451 3452extern kmem_cache_t *zio_buf_cache[]; 3453extern kmem_cache_t *zio_data_buf_cache[]; 3454extern kmem_cache_t *range_seg_cache; 3455 3456static __noinline void 3457arc_kmem_reap_now(void) 3458{ 3459 size_t i; 3460 kmem_cache_t *prev_cache = NULL; 3461 kmem_cache_t *prev_data_cache = NULL; 3462 3463 DTRACE_PROBE(arc__kmem_reap_start); 3464#ifdef _KERNEL 3465 if (arc_meta_used >= arc_meta_limit) { 3466 /* 3467 * We are exceeding our meta-data cache limit. 3468 * Purge some DNLC entries to release holds on meta-data. 3469 */ 3470 dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 3471 } 3472#if defined(__i386) 3473 /* 3474 * Reclaim unused memory from all kmem caches. 3475 */ 3476 kmem_reap(); 3477#endif 3478#endif 3479 3480 for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 3481 if (zio_buf_cache[i] != prev_cache) { 3482 prev_cache = zio_buf_cache[i]; 3483 kmem_cache_reap_now(zio_buf_cache[i]); 3484 } 3485 if (zio_data_buf_cache[i] != prev_data_cache) { 3486 prev_data_cache = zio_data_buf_cache[i]; 3487 kmem_cache_reap_now(zio_data_buf_cache[i]); 3488 } 3489 } 3490 kmem_cache_reap_now(buf_cache); 3491 kmem_cache_reap_now(hdr_full_cache); 3492 kmem_cache_reap_now(hdr_l2only_cache); 3493 kmem_cache_reap_now(range_seg_cache); 3494 3495#ifdef illumos 3496 if (zio_arena != NULL) { 3497 /* 3498 * Ask the vmem arena to reclaim unused memory from its 3499 * quantum caches. 3500 */ 3501 vmem_qcache_reap(zio_arena); 3502 } 3503#endif 3504 DTRACE_PROBE(arc__kmem_reap_end); 3505} 3506 3507/* 3508 * Threads can block in arc_get_data_buf() waiting for this thread to evict 3509 * enough data and signal them to proceed. When this happens, the threads in 3510 * arc_get_data_buf() are sleeping while holding the hash lock for their 3511 * particular arc header. Thus, we must be careful to never sleep on a 3512 * hash lock in this thread. This is to prevent the following deadlock: 3513 * 3514 * - Thread A sleeps on CV in arc_get_data_buf() holding hash lock "L", 3515 * waiting for the reclaim thread to signal it. 3516 * 3517 * - arc_reclaim_thread() tries to acquire hash lock "L" using mutex_enter, 3518 * fails, and goes to sleep forever. 3519 * 3520 * This possible deadlock is avoided by always acquiring a hash lock 3521 * using mutex_tryenter() from arc_reclaim_thread(). 3522 */ 3523static void 3524arc_reclaim_thread(void *dummy __unused) 3525{ 3526 clock_t growtime = 0; 3527 callb_cpr_t cpr; 3528 3529 CALLB_CPR_INIT(&cpr, &arc_reclaim_lock, callb_generic_cpr, FTAG); 3530 3531 mutex_enter(&arc_reclaim_lock); 3532 while (!arc_reclaim_thread_exit) { 3533 int64_t free_memory = arc_available_memory(); 3534 uint64_t evicted = 0; 3535 3536 mutex_exit(&arc_reclaim_lock); 3537 3538 if (free_memory < 0) { 3539 3540 arc_no_grow = B_TRUE; 3541 arc_warm = B_TRUE; 3542 3543 /* 3544 * Wait at least zfs_grow_retry (default 60) seconds 3545 * before considering growing. 3546 */ 3547 growtime = ddi_get_lbolt() + (arc_grow_retry * hz); 3548 3549 arc_kmem_reap_now(); 3550 3551 /* 3552 * If we are still low on memory, shrink the ARC 3553 * so that we have arc_shrink_min free space. 3554 */ 3555 free_memory = arc_available_memory(); 3556 3557 int64_t to_free = 3558 (arc_c >> arc_shrink_shift) - free_memory; 3559 if (to_free > 0) { 3560#ifdef _KERNEL 3561 to_free = MAX(to_free, ptob(needfree)); 3562#endif 3563 arc_shrink(to_free); 3564 } 3565 } else if (free_memory < arc_c >> arc_no_grow_shift) { 3566 arc_no_grow = B_TRUE; 3567 } else if (ddi_get_lbolt() >= growtime) { 3568 arc_no_grow = B_FALSE; 3569 } 3570 3571 evicted = arc_adjust(); 3572 3573 mutex_enter(&arc_reclaim_lock); 3574 3575 /* 3576 * If evicted is zero, we couldn't evict anything via 3577 * arc_adjust(). This could be due to hash lock 3578 * collisions, but more likely due to the majority of 3579 * arc buffers being unevictable. Therefore, even if 3580 * arc_size is above arc_c, another pass is unlikely to 3581 * be helpful and could potentially cause us to enter an 3582 * infinite loop. 3583 */ 3584 if (arc_size <= arc_c || evicted == 0) { 3585#ifdef _KERNEL 3586 needfree = 0; 3587#endif 3588 /* 3589 * We're either no longer overflowing, or we 3590 * can't evict anything more, so we should wake 3591 * up any threads before we go to sleep. 3592 */ 3593 cv_broadcast(&arc_reclaim_waiters_cv); 3594 3595 /* 3596 * Block until signaled, or after one second (we 3597 * might need to perform arc_kmem_reap_now() 3598 * even if we aren't being signalled) 3599 */ 3600 CALLB_CPR_SAFE_BEGIN(&cpr); 3601 (void) cv_timedwait(&arc_reclaim_thread_cv, 3602 &arc_reclaim_lock, hz); 3603 CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_lock); 3604 } 3605 } 3606 3607 arc_reclaim_thread_exit = FALSE; 3608 cv_broadcast(&arc_reclaim_thread_cv); 3609 CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_lock */ 3610 thread_exit(); 3611} 3612 3613static void 3614arc_user_evicts_thread(void *dummy __unused) 3615{ 3616 callb_cpr_t cpr; 3617 3618 CALLB_CPR_INIT(&cpr, &arc_user_evicts_lock, callb_generic_cpr, FTAG); 3619 3620 mutex_enter(&arc_user_evicts_lock); 3621 while (!arc_user_evicts_thread_exit) { 3622 mutex_exit(&arc_user_evicts_lock); 3623 3624 arc_do_user_evicts(); 3625 3626 /* 3627 * This is necessary in order for the mdb ::arc dcmd to 3628 * show up to date information. Since the ::arc command 3629 * does not call the kstat's update function, without 3630 * this call, the command may show stale stats for the 3631 * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even 3632 * with this change, the data might be up to 1 second 3633 * out of date; but that should suffice. The arc_state_t 3634 * structures can be queried directly if more accurate 3635 * information is needed. 3636 */ 3637 if (arc_ksp != NULL) 3638 arc_ksp->ks_update(arc_ksp, KSTAT_READ); 3639 3640 mutex_enter(&arc_user_evicts_lock); 3641 3642 /* 3643 * Block until signaled, or after one second (we need to 3644 * call the arc's kstat update function regularly). 3645 */ 3646 CALLB_CPR_SAFE_BEGIN(&cpr); 3647 (void) cv_timedwait(&arc_user_evicts_cv, 3648 &arc_user_evicts_lock, hz); 3649 CALLB_CPR_SAFE_END(&cpr, &arc_user_evicts_lock); 3650 } 3651 3652 arc_user_evicts_thread_exit = FALSE; 3653 cv_broadcast(&arc_user_evicts_cv); 3654 CALLB_CPR_EXIT(&cpr); /* drops arc_user_evicts_lock */ 3655 thread_exit(); 3656} 3657 3658/* 3659 * Adapt arc info given the number of bytes we are trying to add and 3660 * the state that we are comming from. This function is only called 3661 * when we are adding new content to the cache. 3662 */ 3663static void 3664arc_adapt(int bytes, arc_state_t *state) 3665{ 3666 int mult; 3667 uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 3668 3669 if (state == arc_l2c_only) 3670 return; 3671 3672 ASSERT(bytes > 0); 3673 /* 3674 * Adapt the target size of the MRU list: 3675 * - if we just hit in the MRU ghost list, then increase 3676 * the target size of the MRU list. 3677 * - if we just hit in the MFU ghost list, then increase 3678 * the target size of the MFU list by decreasing the 3679 * target size of the MRU list. 3680 */ 3681 if (state == arc_mru_ghost) { 3682 mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? 3683 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); 3684 mult = MIN(mult, 10); /* avoid wild arc_p adjustment */ 3685 3686 arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 3687 } else if (state == arc_mfu_ghost) { 3688 uint64_t delta; 3689 3690 mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? 3691 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); 3692 mult = MIN(mult, 10); 3693 3694 delta = MIN(bytes * mult, arc_p); 3695 arc_p = MAX(arc_p_min, arc_p - delta); 3696 } 3697 ASSERT((int64_t)arc_p >= 0); 3698 3699 if (arc_reclaim_needed()) { 3700 cv_signal(&arc_reclaim_thread_cv); 3701 return; 3702 } 3703 3704 if (arc_no_grow) 3705 return; 3706 3707 if (arc_c >= arc_c_max) 3708 return; 3709 3710 /* 3711 * If we're within (2 * maxblocksize) bytes of the target 3712 * cache size, increment the target cache size 3713 */ 3714 if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 3715 DTRACE_PROBE1(arc__inc_adapt, int, bytes); 3716 atomic_add_64(&arc_c, (int64_t)bytes); 3717 if (arc_c > arc_c_max) 3718 arc_c = arc_c_max; 3719 else if (state == arc_anon) 3720 atomic_add_64(&arc_p, (int64_t)bytes); 3721 if (arc_p > arc_c) 3722 arc_p = arc_c; 3723 } 3724 ASSERT((int64_t)arc_p >= 0); 3725} 3726 3727/* 3728 * Check if arc_size has grown past our upper threshold, determined by 3729 * zfs_arc_overflow_shift. 3730 */ 3731static boolean_t 3732arc_is_overflowing(void) 3733{ 3734 /* Always allow at least one block of overflow */ 3735 uint64_t overflow = MAX(SPA_MAXBLOCKSIZE, 3736 arc_c >> zfs_arc_overflow_shift); 3737 3738 return (arc_size >= arc_c + overflow); 3739} 3740 3741/* 3742 * The buffer, supplied as the first argument, needs a data block. If we 3743 * are hitting the hard limit for the cache size, we must sleep, waiting 3744 * for the eviction thread to catch up. If we're past the target size 3745 * but below the hard limit, we'll only signal the reclaim thread and 3746 * continue on. 3747 */ 3748static void 3749arc_get_data_buf(arc_buf_t *buf) 3750{ 3751 arc_state_t *state = buf->b_hdr->b_l1hdr.b_state; 3752 uint64_t size = buf->b_hdr->b_size; 3753 arc_buf_contents_t type = arc_buf_type(buf->b_hdr); 3754 3755 arc_adapt(size, state); 3756 3757 /* 3758 * If arc_size is currently overflowing, and has grown past our 3759 * upper limit, we must be adding data faster than the evict 3760 * thread can evict. Thus, to ensure we don't compound the 3761 * problem by adding more data and forcing arc_size to grow even 3762 * further past it's target size, we halt and wait for the 3763 * eviction thread to catch up. 3764 * 3765 * It's also possible that the reclaim thread is unable to evict 3766 * enough buffers to get arc_size below the overflow limit (e.g. 3767 * due to buffers being un-evictable, or hash lock collisions). 3768 * In this case, we want to proceed regardless if we're 3769 * overflowing; thus we don't use a while loop here. 3770 */ 3771 if (arc_is_overflowing()) { 3772 mutex_enter(&arc_reclaim_lock); 3773 3774 /* 3775 * Now that we've acquired the lock, we may no longer be 3776 * over the overflow limit, lets check. 3777 * 3778 * We're ignoring the case of spurious wake ups. If that 3779 * were to happen, it'd let this thread consume an ARC 3780 * buffer before it should have (i.e. before we're under 3781 * the overflow limit and were signalled by the reclaim 3782 * thread). As long as that is a rare occurrence, it 3783 * shouldn't cause any harm. 3784 */ 3785 if (arc_is_overflowing()) { 3786 cv_signal(&arc_reclaim_thread_cv); 3787 cv_wait(&arc_reclaim_waiters_cv, &arc_reclaim_lock); 3788 } 3789 3790 mutex_exit(&arc_reclaim_lock); 3791 } 3792 3793 if (type == ARC_BUFC_METADATA) { 3794 buf->b_data = zio_buf_alloc(size); 3795 arc_space_consume(size, ARC_SPACE_META); 3796 } else { 3797 ASSERT(type == ARC_BUFC_DATA); 3798 buf->b_data = zio_data_buf_alloc(size); 3799 arc_space_consume(size, ARC_SPACE_DATA); 3800 } 3801 3802 /* 3803 * Update the state size. Note that ghost states have a 3804 * "ghost size" and so don't need to be updated. 3805 */ 3806 if (!GHOST_STATE(buf->b_hdr->b_l1hdr.b_state)) { 3807 arc_buf_hdr_t *hdr = buf->b_hdr; 3808 3809 atomic_add_64(&hdr->b_l1hdr.b_state->arcs_size, size); 3810 3811 /* 3812 * If this is reached via arc_read, the link is 3813 * protected by the hash lock. If reached via 3814 * arc_buf_alloc, the header should not be accessed by 3815 * any other thread. And, if reached via arc_read_done, 3816 * the hash lock will protect it if it's found in the 3817 * hash table; otherwise no other thread should be 3818 * trying to [add|remove]_reference it. 3819 */ 3820 if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { 3821 ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 3822 atomic_add_64(&hdr->b_l1hdr.b_state->arcs_lsize[type], 3823 size); 3824 } 3825 /* 3826 * If we are growing the cache, and we are adding anonymous 3827 * data, and we have outgrown arc_p, update arc_p 3828 */ 3829 if (arc_size < arc_c && hdr->b_l1hdr.b_state == arc_anon && 3830 arc_anon->arcs_size + arc_mru->arcs_size > arc_p) 3831 arc_p = MIN(arc_c, arc_p + size); 3832 } 3833 ARCSTAT_BUMP(arcstat_allocated); 3834} 3835 3836/* 3837 * This routine is called whenever a buffer is accessed. 3838 * NOTE: the hash lock is dropped in this function. 3839 */ 3840static void 3841arc_access(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) 3842{ 3843 clock_t now; 3844 3845 ASSERT(MUTEX_HELD(hash_lock)); 3846 ASSERT(HDR_HAS_L1HDR(hdr)); 3847 3848 if (hdr->b_l1hdr.b_state == arc_anon) { 3849 /* 3850 * This buffer is not in the cache, and does not 3851 * appear in our "ghost" list. Add the new buffer 3852 * to the MRU state. 3853 */ 3854 3855 ASSERT0(hdr->b_l1hdr.b_arc_access); 3856 hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 3857 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); 3858 arc_change_state(arc_mru, hdr, hash_lock); 3859 3860 } else if (hdr->b_l1hdr.b_state == arc_mru) { 3861 now = ddi_get_lbolt(); 3862 3863 /* 3864 * If this buffer is here because of a prefetch, then either: 3865 * - clear the flag if this is a "referencing" read 3866 * (any subsequent access will bump this into the MFU state). 3867 * or 3868 * - move the buffer to the head of the list if this is 3869 * another prefetch (to make it less likely to be evicted). 3870 */ 3871 if (HDR_PREFETCH(hdr)) { 3872 if (refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { 3873 /* link protected by hash lock */ 3874 ASSERT(multilist_link_active( 3875 &hdr->b_l1hdr.b_arc_node)); 3876 } else { 3877 hdr->b_flags &= ~ARC_FLAG_PREFETCH; 3878 ARCSTAT_BUMP(arcstat_mru_hits); 3879 } 3880 hdr->b_l1hdr.b_arc_access = now; 3881 return; 3882 } 3883 3884 /* 3885 * This buffer has been "accessed" only once so far, 3886 * but it is still in the cache. Move it to the MFU 3887 * state. 3888 */ 3889 if (now > hdr->b_l1hdr.b_arc_access + ARC_MINTIME) { 3890 /* 3891 * More than 125ms have passed since we 3892 * instantiated this buffer. Move it to the 3893 * most frequently used state. 3894 */ 3895 hdr->b_l1hdr.b_arc_access = now; 3896 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 3897 arc_change_state(arc_mfu, hdr, hash_lock); 3898 } 3899 ARCSTAT_BUMP(arcstat_mru_hits); 3900 } else if (hdr->b_l1hdr.b_state == arc_mru_ghost) { 3901 arc_state_t *new_state; 3902 /* 3903 * This buffer has been "accessed" recently, but 3904 * was evicted from the cache. Move it to the 3905 * MFU state. 3906 */ 3907 3908 if (HDR_PREFETCH(hdr)) { 3909 new_state = arc_mru; 3910 if (refcount_count(&hdr->b_l1hdr.b_refcnt) > 0) 3911 hdr->b_flags &= ~ARC_FLAG_PREFETCH; 3912 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); 3913 } else { 3914 new_state = arc_mfu; 3915 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 3916 } 3917 3918 hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 3919 arc_change_state(new_state, hdr, hash_lock); 3920 3921 ARCSTAT_BUMP(arcstat_mru_ghost_hits); 3922 } else if (hdr->b_l1hdr.b_state == arc_mfu) { 3923 /* 3924 * This buffer has been accessed more than once and is 3925 * still in the cache. Keep it in the MFU state. 3926 * 3927 * NOTE: an add_reference() that occurred when we did 3928 * the arc_read() will have kicked this off the list. 3929 * If it was a prefetch, we will explicitly move it to 3930 * the head of the list now. 3931 */ 3932 if ((HDR_PREFETCH(hdr)) != 0) { 3933 ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 3934 /* link protected by hash_lock */ 3935 ASSERT(multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 3936 } 3937 ARCSTAT_BUMP(arcstat_mfu_hits); 3938 hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 3939 } else if (hdr->b_l1hdr.b_state == arc_mfu_ghost) { 3940 arc_state_t *new_state = arc_mfu; 3941 /* 3942 * This buffer has been accessed more than once but has 3943 * been evicted from the cache. Move it back to the 3944 * MFU state. 3945 */ 3946 3947 if (HDR_PREFETCH(hdr)) { 3948 /* 3949 * This is a prefetch access... 3950 * move this block back to the MRU state. 3951 */ 3952 ASSERT0(refcount_count(&hdr->b_l1hdr.b_refcnt)); 3953 new_state = arc_mru; 3954 } 3955 3956 hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 3957 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 3958 arc_change_state(new_state, hdr, hash_lock); 3959 3960 ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 3961 } else if (hdr->b_l1hdr.b_state == arc_l2c_only) { 3962 /* 3963 * This buffer is on the 2nd Level ARC. 3964 */ 3965 3966 hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 3967 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 3968 arc_change_state(arc_mfu, hdr, hash_lock); 3969 } else { 3970 ASSERT(!"invalid arc state"); 3971 } 3972} 3973 3974/* a generic arc_done_func_t which you can use */ 3975/* ARGSUSED */ 3976void 3977arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 3978{ 3979 if (zio == NULL || zio->io_error == 0) 3980 bcopy(buf->b_data, arg, buf->b_hdr->b_size); 3981 VERIFY(arc_buf_remove_ref(buf, arg)); 3982} 3983 3984/* a generic arc_done_func_t */ 3985void 3986arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 3987{ 3988 arc_buf_t **bufp = arg; 3989 if (zio && zio->io_error) { 3990 VERIFY(arc_buf_remove_ref(buf, arg)); 3991 *bufp = NULL; 3992 } else { 3993 *bufp = buf; 3994 ASSERT(buf->b_data); 3995 } 3996} 3997 3998static void 3999arc_read_done(zio_t *zio) 4000{ 4001 arc_buf_hdr_t *hdr; 4002 arc_buf_t *buf; 4003 arc_buf_t *abuf; /* buffer we're assigning to callback */ 4004 kmutex_t *hash_lock = NULL; 4005 arc_callback_t *callback_list, *acb; 4006 int freeable = FALSE; 4007 4008 buf = zio->io_private; 4009 hdr = buf->b_hdr; 4010 4011 /* 4012 * The hdr was inserted into hash-table and removed from lists 4013 * prior to starting I/O. We should find this header, since 4014 * it's in the hash table, and it should be legit since it's 4015 * not possible to evict it during the I/O. The only possible 4016 * reason for it not to be found is if we were freed during the 4017 * read. 4018 */ 4019 if (HDR_IN_HASH_TABLE(hdr)) { 4020 ASSERT3U(hdr->b_birth, ==, BP_PHYSICAL_BIRTH(zio->io_bp)); 4021 ASSERT3U(hdr->b_dva.dva_word[0], ==, 4022 BP_IDENTITY(zio->io_bp)->dva_word[0]); 4023 ASSERT3U(hdr->b_dva.dva_word[1], ==, 4024 BP_IDENTITY(zio->io_bp)->dva_word[1]); 4025 4026 arc_buf_hdr_t *found = buf_hash_find(hdr->b_spa, zio->io_bp, 4027 &hash_lock); 4028 4029 ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && 4030 hash_lock == NULL) || 4031 (found == hdr && 4032 DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 4033 (found == hdr && HDR_L2_READING(hdr))); 4034 } 4035 4036 hdr->b_flags &= ~ARC_FLAG_L2_EVICTED; 4037 if (l2arc_noprefetch && HDR_PREFETCH(hdr)) 4038 hdr->b_flags &= ~ARC_FLAG_L2CACHE; 4039 4040 /* byteswap if necessary */ 4041 callback_list = hdr->b_l1hdr.b_acb; 4042 ASSERT(callback_list != NULL); 4043 if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) { 4044 dmu_object_byteswap_t bswap = 4045 DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp)); 4046 arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 4047 byteswap_uint64_array : 4048 dmu_ot_byteswap[bswap].ob_func; 4049 func(buf->b_data, hdr->b_size); 4050 } 4051 4052 arc_cksum_compute(buf, B_FALSE); 4053#ifdef illumos 4054 arc_buf_watch(buf); 4055#endif 4056 4057 if (hash_lock && zio->io_error == 0 && 4058 hdr->b_l1hdr.b_state == arc_anon) { 4059 /* 4060 * Only call arc_access on anonymous buffers. This is because 4061 * if we've issued an I/O for an evicted buffer, we've already 4062 * called arc_access (to prevent any simultaneous readers from 4063 * getting confused). 4064 */ 4065 arc_access(hdr, hash_lock); 4066 } 4067 4068 /* create copies of the data buffer for the callers */ 4069 abuf = buf; 4070 for (acb = callback_list; acb; acb = acb->acb_next) { 4071 if (acb->acb_done) { 4072 if (abuf == NULL) { 4073 ARCSTAT_BUMP(arcstat_duplicate_reads); 4074 abuf = arc_buf_clone(buf); 4075 } 4076 acb->acb_buf = abuf; 4077 abuf = NULL; 4078 } 4079 } 4080 hdr->b_l1hdr.b_acb = NULL; 4081 hdr->b_flags &= ~ARC_FLAG_IO_IN_PROGRESS; 4082 ASSERT(!HDR_BUF_AVAILABLE(hdr)); 4083 if (abuf == buf) { 4084 ASSERT(buf->b_efunc == NULL); 4085 ASSERT(hdr->b_l1hdr.b_datacnt == 1); 4086 hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; 4087 } 4088 4089 ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt) || 4090 callback_list != NULL); 4091 4092 if (zio->io_error != 0) { 4093 hdr->b_flags |= ARC_FLAG_IO_ERROR; 4094 if (hdr->b_l1hdr.b_state != arc_anon) 4095 arc_change_state(arc_anon, hdr, hash_lock); 4096 if (HDR_IN_HASH_TABLE(hdr)) 4097 buf_hash_remove(hdr); 4098 freeable = refcount_is_zero(&hdr->b_l1hdr.b_refcnt); 4099 } 4100 4101 /* 4102 * Broadcast before we drop the hash_lock to avoid the possibility 4103 * that the hdr (and hence the cv) might be freed before we get to 4104 * the cv_broadcast(). 4105 */ 4106 cv_broadcast(&hdr->b_l1hdr.b_cv); 4107 4108 if (hash_lock != NULL) { 4109 mutex_exit(hash_lock); 4110 } else { 4111 /* 4112 * This block was freed while we waited for the read to 4113 * complete. It has been removed from the hash table and 4114 * moved to the anonymous state (so that it won't show up 4115 * in the cache). 4116 */ 4117 ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 4118 freeable = refcount_is_zero(&hdr->b_l1hdr.b_refcnt); 4119 } 4120 4121 /* execute each callback and free its structure */ 4122 while ((acb = callback_list) != NULL) { 4123 if (acb->acb_done) 4124 acb->acb_done(zio, acb->acb_buf, acb->acb_private); 4125 4126 if (acb->acb_zio_dummy != NULL) { 4127 acb->acb_zio_dummy->io_error = zio->io_error; 4128 zio_nowait(acb->acb_zio_dummy); 4129 } 4130 4131 callback_list = acb->acb_next; 4132 kmem_free(acb, sizeof (arc_callback_t)); 4133 } 4134 4135 if (freeable) 4136 arc_hdr_destroy(hdr); 4137} 4138 4139/* 4140 * "Read" the block at the specified DVA (in bp) via the 4141 * cache. If the block is found in the cache, invoke the provided 4142 * callback immediately and return. Note that the `zio' parameter 4143 * in the callback will be NULL in this case, since no IO was 4144 * required. If the block is not in the cache pass the read request 4145 * on to the spa with a substitute callback function, so that the 4146 * requested block will be added to the cache. 4147 * 4148 * If a read request arrives for a block that has a read in-progress, 4149 * either wait for the in-progress read to complete (and return the 4150 * results); or, if this is a read with a "done" func, add a record 4151 * to the read to invoke the "done" func when the read completes, 4152 * and return; or just return. 4153 * 4154 * arc_read_done() will invoke all the requested "done" functions 4155 * for readers of this block. 4156 */ 4157int 4158arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_done_func_t *done, 4159 void *private, zio_priority_t priority, int zio_flags, 4160 arc_flags_t *arc_flags, const zbookmark_phys_t *zb) 4161{ 4162 arc_buf_hdr_t *hdr = NULL; 4163 arc_buf_t *buf = NULL; 4164 kmutex_t *hash_lock = NULL; 4165 zio_t *rzio; 4166 uint64_t guid = spa_load_guid(spa); 4167 4168 ASSERT(!BP_IS_EMBEDDED(bp) || 4169 BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA); 4170 4171top: 4172 if (!BP_IS_EMBEDDED(bp)) { 4173 /* 4174 * Embedded BP's have no DVA and require no I/O to "read". 4175 * Create an anonymous arc buf to back it. 4176 */ 4177 hdr = buf_hash_find(guid, bp, &hash_lock); 4178 } 4179 4180 if (hdr != NULL && HDR_HAS_L1HDR(hdr) && hdr->b_l1hdr.b_datacnt > 0) { 4181 4182 *arc_flags |= ARC_FLAG_CACHED; 4183 4184 if (HDR_IO_IN_PROGRESS(hdr)) { 4185 4186 if (*arc_flags & ARC_FLAG_WAIT) { 4187 cv_wait(&hdr->b_l1hdr.b_cv, hash_lock); 4188 mutex_exit(hash_lock); 4189 goto top; 4190 } 4191 ASSERT(*arc_flags & ARC_FLAG_NOWAIT); 4192 4193 if (done) { 4194 arc_callback_t *acb = NULL; 4195 4196 acb = kmem_zalloc(sizeof (arc_callback_t), 4197 KM_SLEEP); 4198 acb->acb_done = done; 4199 acb->acb_private = private; 4200 if (pio != NULL) 4201 acb->acb_zio_dummy = zio_null(pio, 4202 spa, NULL, NULL, NULL, zio_flags); 4203 4204 ASSERT(acb->acb_done != NULL); 4205 acb->acb_next = hdr->b_l1hdr.b_acb; 4206 hdr->b_l1hdr.b_acb = acb; 4207 add_reference(hdr, hash_lock, private); 4208 mutex_exit(hash_lock); 4209 return (0); 4210 } 4211 mutex_exit(hash_lock); 4212 return (0); 4213 } 4214 4215 ASSERT(hdr->b_l1hdr.b_state == arc_mru || 4216 hdr->b_l1hdr.b_state == arc_mfu); 4217 4218 if (done) { 4219 add_reference(hdr, hash_lock, private); 4220 /* 4221 * If this block is already in use, create a new 4222 * copy of the data so that we will be guaranteed 4223 * that arc_release() will always succeed. 4224 */ 4225 buf = hdr->b_l1hdr.b_buf; 4226 ASSERT(buf); 4227 ASSERT(buf->b_data); 4228 if (HDR_BUF_AVAILABLE(hdr)) { 4229 ASSERT(buf->b_efunc == NULL); 4230 hdr->b_flags &= ~ARC_FLAG_BUF_AVAILABLE; 4231 } else { 4232 buf = arc_buf_clone(buf); 4233 } 4234 4235 } else if (*arc_flags & ARC_FLAG_PREFETCH && 4236 refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { 4237 hdr->b_flags |= ARC_FLAG_PREFETCH; 4238 } 4239 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 4240 arc_access(hdr, hash_lock); 4241 if (*arc_flags & ARC_FLAG_L2CACHE) 4242 hdr->b_flags |= ARC_FLAG_L2CACHE; 4243 if (*arc_flags & ARC_FLAG_L2COMPRESS) 4244 hdr->b_flags |= ARC_FLAG_L2COMPRESS; 4245 mutex_exit(hash_lock); 4246 ARCSTAT_BUMP(arcstat_hits); 4247 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), 4248 demand, prefetch, !HDR_ISTYPE_METADATA(hdr), 4249 data, metadata, hits); 4250 4251 if (done) 4252 done(NULL, buf, private); 4253 } else { 4254 uint64_t size = BP_GET_LSIZE(bp); 4255 arc_callback_t *acb; 4256 vdev_t *vd = NULL; 4257 uint64_t addr = 0; 4258 boolean_t devw = B_FALSE; 4259 enum zio_compress b_compress = ZIO_COMPRESS_OFF; 4260 int32_t b_asize = 0; 4261 4262 if (hdr == NULL) { 4263 /* this block is not in the cache */ 4264 arc_buf_hdr_t *exists = NULL; 4265 arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 4266 buf = arc_buf_alloc(spa, size, private, type); 4267 hdr = buf->b_hdr; 4268 if (!BP_IS_EMBEDDED(bp)) { 4269 hdr->b_dva = *BP_IDENTITY(bp); 4270 hdr->b_birth = BP_PHYSICAL_BIRTH(bp); 4271 exists = buf_hash_insert(hdr, &hash_lock); 4272 } 4273 if (exists != NULL) { 4274 /* somebody beat us to the hash insert */ 4275 mutex_exit(hash_lock); 4276 buf_discard_identity(hdr); 4277 (void) arc_buf_remove_ref(buf, private); 4278 goto top; /* restart the IO request */ 4279 } 4280 4281 /* if this is a prefetch, we don't have a reference */ 4282 if (*arc_flags & ARC_FLAG_PREFETCH) { 4283 (void) remove_reference(hdr, hash_lock, 4284 private); 4285 hdr->b_flags |= ARC_FLAG_PREFETCH; 4286 } 4287 if (*arc_flags & ARC_FLAG_L2CACHE) 4288 hdr->b_flags |= ARC_FLAG_L2CACHE; 4289 if (*arc_flags & ARC_FLAG_L2COMPRESS) 4290 hdr->b_flags |= ARC_FLAG_L2COMPRESS; 4291 if (BP_GET_LEVEL(bp) > 0) 4292 hdr->b_flags |= ARC_FLAG_INDIRECT; 4293 } else { 4294 /* 4295 * This block is in the ghost cache. If it was L2-only 4296 * (and thus didn't have an L1 hdr), we realloc the 4297 * header to add an L1 hdr. 4298 */ 4299 if (!HDR_HAS_L1HDR(hdr)) { 4300 hdr = arc_hdr_realloc(hdr, hdr_l2only_cache, 4301 hdr_full_cache); 4302 } 4303 4304 ASSERT(GHOST_STATE(hdr->b_l1hdr.b_state)); 4305 ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 4306 ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 4307 ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 4308 4309 /* if this is a prefetch, we don't have a reference */ 4310 if (*arc_flags & ARC_FLAG_PREFETCH) 4311 hdr->b_flags |= ARC_FLAG_PREFETCH; 4312 else 4313 add_reference(hdr, hash_lock, private); 4314 if (*arc_flags & ARC_FLAG_L2CACHE) 4315 hdr->b_flags |= ARC_FLAG_L2CACHE; 4316 if (*arc_flags & ARC_FLAG_L2COMPRESS) 4317 hdr->b_flags |= ARC_FLAG_L2COMPRESS; 4318 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 4319 buf->b_hdr = hdr; 4320 buf->b_data = NULL; 4321 buf->b_efunc = NULL; 4322 buf->b_private = NULL; 4323 buf->b_next = NULL; 4324 hdr->b_l1hdr.b_buf = buf; 4325 ASSERT0(hdr->b_l1hdr.b_datacnt); 4326 hdr->b_l1hdr.b_datacnt = 1; 4327 arc_get_data_buf(buf); 4328 arc_access(hdr, hash_lock); 4329 } 4330 4331 ASSERT(!GHOST_STATE(hdr->b_l1hdr.b_state)); 4332 4333 acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 4334 acb->acb_done = done; 4335 acb->acb_private = private; 4336 4337 ASSERT(hdr->b_l1hdr.b_acb == NULL); 4338 hdr->b_l1hdr.b_acb = acb; 4339 hdr->b_flags |= ARC_FLAG_IO_IN_PROGRESS; 4340 4341 if (HDR_HAS_L2HDR(hdr) && 4342 (vd = hdr->b_l2hdr.b_dev->l2ad_vdev) != NULL) { 4343 devw = hdr->b_l2hdr.b_dev->l2ad_writing; 4344 addr = hdr->b_l2hdr.b_daddr; 4345 b_compress = HDR_GET_COMPRESS(hdr); 4346 b_asize = hdr->b_l2hdr.b_asize; 4347 /* 4348 * Lock out device removal. 4349 */ 4350 if (vdev_is_dead(vd) || 4351 !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 4352 vd = NULL; 4353 } 4354 4355 if (hash_lock != NULL) 4356 mutex_exit(hash_lock); 4357 4358 /* 4359 * At this point, we have a level 1 cache miss. Try again in 4360 * L2ARC if possible. 4361 */ 4362 ASSERT3U(hdr->b_size, ==, size); 4363 DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp, 4364 uint64_t, size, zbookmark_phys_t *, zb); 4365 ARCSTAT_BUMP(arcstat_misses); 4366 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), 4367 demand, prefetch, !HDR_ISTYPE_METADATA(hdr), 4368 data, metadata, misses); 4369#ifdef _KERNEL 4370 curthread->td_ru.ru_inblock++; 4371#endif 4372 4373 if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 4374 /* 4375 * Read from the L2ARC if the following are true: 4376 * 1. The L2ARC vdev was previously cached. 4377 * 2. This buffer still has L2ARC metadata. 4378 * 3. This buffer isn't currently writing to the L2ARC. 4379 * 4. The L2ARC entry wasn't evicted, which may 4380 * also have invalidated the vdev. 4381 * 5. This isn't prefetch and l2arc_noprefetch is set. 4382 */ 4383 if (HDR_HAS_L2HDR(hdr) && 4384 !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 4385 !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 4386 l2arc_read_callback_t *cb; 4387 4388 DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 4389 ARCSTAT_BUMP(arcstat_l2_hits); 4390 4391 cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 4392 KM_SLEEP); 4393 cb->l2rcb_buf = buf; 4394 cb->l2rcb_spa = spa; 4395 cb->l2rcb_bp = *bp; 4396 cb->l2rcb_zb = *zb; 4397 cb->l2rcb_flags = zio_flags; 4398 cb->l2rcb_compress = b_compress; 4399 4400 ASSERT(addr >= VDEV_LABEL_START_SIZE && 4401 addr + size < vd->vdev_psize - 4402 VDEV_LABEL_END_SIZE); 4403 4404 /* 4405 * l2arc read. The SCL_L2ARC lock will be 4406 * released by l2arc_read_done(). 4407 * Issue a null zio if the underlying buffer 4408 * was squashed to zero size by compression. 4409 */ 4410 if (b_compress == ZIO_COMPRESS_EMPTY) { 4411 rzio = zio_null(pio, spa, vd, 4412 l2arc_read_done, cb, 4413 zio_flags | ZIO_FLAG_DONT_CACHE | 4414 ZIO_FLAG_CANFAIL | 4415 ZIO_FLAG_DONT_PROPAGATE | 4416 ZIO_FLAG_DONT_RETRY); 4417 } else { 4418 rzio = zio_read_phys(pio, vd, addr, 4419 b_asize, buf->b_data, 4420 ZIO_CHECKSUM_OFF, 4421 l2arc_read_done, cb, priority, 4422 zio_flags | ZIO_FLAG_DONT_CACHE | 4423 ZIO_FLAG_CANFAIL | 4424 ZIO_FLAG_DONT_PROPAGATE | 4425 ZIO_FLAG_DONT_RETRY, B_FALSE); 4426 } 4427 DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 4428 zio_t *, rzio); 4429 ARCSTAT_INCR(arcstat_l2_read_bytes, b_asize); 4430 4431 if (*arc_flags & ARC_FLAG_NOWAIT) { 4432 zio_nowait(rzio); 4433 return (0); 4434 } 4435 4436 ASSERT(*arc_flags & ARC_FLAG_WAIT); 4437 if (zio_wait(rzio) == 0) 4438 return (0); 4439 4440 /* l2arc read error; goto zio_read() */ 4441 } else { 4442 DTRACE_PROBE1(l2arc__miss, 4443 arc_buf_hdr_t *, hdr); 4444 ARCSTAT_BUMP(arcstat_l2_misses); 4445 if (HDR_L2_WRITING(hdr)) 4446 ARCSTAT_BUMP(arcstat_l2_rw_clash); 4447 spa_config_exit(spa, SCL_L2ARC, vd); 4448 } 4449 } else { 4450 if (vd != NULL) 4451 spa_config_exit(spa, SCL_L2ARC, vd); 4452 if (l2arc_ndev != 0) { 4453 DTRACE_PROBE1(l2arc__miss, 4454 arc_buf_hdr_t *, hdr); 4455 ARCSTAT_BUMP(arcstat_l2_misses); 4456 } 4457 } 4458 4459 rzio = zio_read(pio, spa, bp, buf->b_data, size, 4460 arc_read_done, buf, priority, zio_flags, zb); 4461 4462 if (*arc_flags & ARC_FLAG_WAIT) 4463 return (zio_wait(rzio)); 4464 4465 ASSERT(*arc_flags & ARC_FLAG_NOWAIT); 4466 zio_nowait(rzio); 4467 } 4468 return (0); 4469} 4470 4471void 4472arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 4473{ 4474 ASSERT(buf->b_hdr != NULL); 4475 ASSERT(buf->b_hdr->b_l1hdr.b_state != arc_anon); 4476 ASSERT(!refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt) || 4477 func == NULL); 4478 ASSERT(buf->b_efunc == NULL); 4479 ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr)); 4480 4481 buf->b_efunc = func; 4482 buf->b_private = private; 4483} 4484 4485/* 4486 * Notify the arc that a block was freed, and thus will never be used again. 4487 */ 4488void 4489arc_freed(spa_t *spa, const blkptr_t *bp) 4490{ 4491 arc_buf_hdr_t *hdr; 4492 kmutex_t *hash_lock; 4493 uint64_t guid = spa_load_guid(spa); 4494 4495 ASSERT(!BP_IS_EMBEDDED(bp)); 4496 4497 hdr = buf_hash_find(guid, bp, &hash_lock); 4498 if (hdr == NULL) 4499 return; 4500 if (HDR_BUF_AVAILABLE(hdr)) { 4501 arc_buf_t *buf = hdr->b_l1hdr.b_buf; 4502 add_reference(hdr, hash_lock, FTAG); 4503 hdr->b_flags &= ~ARC_FLAG_BUF_AVAILABLE; 4504 mutex_exit(hash_lock); 4505 4506 arc_release(buf, FTAG); 4507 (void) arc_buf_remove_ref(buf, FTAG); 4508 } else { 4509 mutex_exit(hash_lock); 4510 } 4511 4512} 4513 4514/* 4515 * Clear the user eviction callback set by arc_set_callback(), first calling 4516 * it if it exists. Because the presence of a callback keeps an arc_buf cached 4517 * clearing the callback may result in the arc_buf being destroyed. However, 4518 * it will not result in the *last* arc_buf being destroyed, hence the data 4519 * will remain cached in the ARC. We make a copy of the arc buffer here so 4520 * that we can process the callback without holding any locks. 4521 * 4522 * It's possible that the callback is already in the process of being cleared 4523 * by another thread. In this case we can not clear the callback. 4524 * 4525 * Returns B_TRUE if the callback was successfully called and cleared. 4526 */ 4527boolean_t 4528arc_clear_callback(arc_buf_t *buf) 4529{ 4530 arc_buf_hdr_t *hdr; 4531 kmutex_t *hash_lock; 4532 arc_evict_func_t *efunc = buf->b_efunc; 4533 void *private = buf->b_private; 4534 4535 mutex_enter(&buf->b_evict_lock); 4536 hdr = buf->b_hdr; 4537 if (hdr == NULL) { 4538 /* 4539 * We are in arc_do_user_evicts(). 4540 */ 4541 ASSERT(buf->b_data == NULL); 4542 mutex_exit(&buf->b_evict_lock); 4543 return (B_FALSE); 4544 } else if (buf->b_data == NULL) { 4545 /* 4546 * We are on the eviction list; process this buffer now 4547 * but let arc_do_user_evicts() do the reaping. 4548 */ 4549 buf->b_efunc = NULL; 4550 mutex_exit(&buf->b_evict_lock); 4551 VERIFY0(efunc(private)); 4552 return (B_TRUE); 4553 } 4554 hash_lock = HDR_LOCK(hdr); 4555 mutex_enter(hash_lock); 4556 hdr = buf->b_hdr; 4557 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 4558 4559 ASSERT3U(refcount_count(&hdr->b_l1hdr.b_refcnt), <, 4560 hdr->b_l1hdr.b_datacnt); 4561 ASSERT(hdr->b_l1hdr.b_state == arc_mru || 4562 hdr->b_l1hdr.b_state == arc_mfu); 4563 4564 buf->b_efunc = NULL; 4565 buf->b_private = NULL; 4566 4567 if (hdr->b_l1hdr.b_datacnt > 1) { 4568 mutex_exit(&buf->b_evict_lock); 4569 arc_buf_destroy(buf, TRUE); 4570 } else { 4571 ASSERT(buf == hdr->b_l1hdr.b_buf); 4572 hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; 4573 mutex_exit(&buf->b_evict_lock); 4574 } 4575 4576 mutex_exit(hash_lock); 4577 VERIFY0(efunc(private)); 4578 return (B_TRUE); 4579} 4580 4581/* 4582 * Release this buffer from the cache, making it an anonymous buffer. This 4583 * must be done after a read and prior to modifying the buffer contents. 4584 * If the buffer has more than one reference, we must make 4585 * a new hdr for the buffer. 4586 */ 4587void 4588arc_release(arc_buf_t *buf, void *tag) 4589{ 4590 arc_buf_hdr_t *hdr = buf->b_hdr; 4591 4592 ASSERT(HDR_HAS_L1HDR(hdr)); 4593 4594 /* 4595 * It would be nice to assert that if it's DMU metadata (level > 4596 * 0 || it's the dnode file), then it must be syncing context. 4597 * But we don't know that information at this level. 4598 */ 4599 4600 mutex_enter(&buf->b_evict_lock); 4601 /* 4602 * We don't grab the hash lock prior to this check, because if 4603 * the buffer's header is in the arc_anon state, it won't be 4604 * linked into the hash table. 4605 */ 4606 if (hdr->b_l1hdr.b_state == arc_anon) { 4607 mutex_exit(&buf->b_evict_lock); 4608 ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 4609 ASSERT(!HDR_IN_HASH_TABLE(hdr)); 4610 ASSERT(!HDR_HAS_L2HDR(hdr)); 4611 ASSERT(BUF_EMPTY(hdr)); 4612 ASSERT3U(hdr->b_l1hdr.b_datacnt, ==, 1); 4613 ASSERT3S(refcount_count(&hdr->b_l1hdr.b_refcnt), ==, 1); 4614 ASSERT(!list_link_active(&hdr->b_l1hdr.b_arc_node)); 4615 4616 ASSERT3P(buf->b_efunc, ==, NULL); 4617 ASSERT3P(buf->b_private, ==, NULL); 4618 4619 hdr->b_l1hdr.b_arc_access = 0; 4620 arc_buf_thaw(buf); 4621 4622 return; 4623 } 4624 4625 kmutex_t *hash_lock = HDR_LOCK(hdr); 4626 mutex_enter(hash_lock); 4627 4628 /* 4629 * This assignment is only valid as long as the hash_lock is 4630 * held, we must be careful not to reference state or the 4631 * b_state field after dropping the lock. 4632 */ 4633 arc_state_t *state = hdr->b_l1hdr.b_state; 4634 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 4635 ASSERT3P(state, !=, arc_anon); 4636 4637 /* this buffer is not on any list */ 4638 ASSERT(refcount_count(&hdr->b_l1hdr.b_refcnt) > 0); 4639 4640 if (HDR_HAS_L2HDR(hdr)) { 4641 mutex_enter(&hdr->b_l2hdr.b_dev->l2ad_mtx); 4642 4643 /* 4644 * We have to recheck this conditional again now that 4645 * we're holding the l2ad_mtx to prevent a race with 4646 * another thread which might be concurrently calling 4647 * l2arc_evict(). In that case, l2arc_evict() might have 4648 * destroyed the header's L2 portion as we were waiting 4649 * to acquire the l2ad_mtx. 4650 */ 4651 if (HDR_HAS_L2HDR(hdr)) { 4652 if (hdr->b_l2hdr.b_daddr != L2ARC_ADDR_UNSET) 4653 trim_map_free(hdr->b_l2hdr.b_dev->l2ad_vdev, 4654 hdr->b_l2hdr.b_daddr, 4655 hdr->b_l2hdr.b_asize, 0); 4656 arc_hdr_l2hdr_destroy(hdr); 4657 } 4658 4659 mutex_exit(&hdr->b_l2hdr.b_dev->l2ad_mtx); 4660 } 4661 4662 /* 4663 * Do we have more than one buf? 4664 */ 4665 if (hdr->b_l1hdr.b_datacnt > 1) { 4666 arc_buf_hdr_t *nhdr; 4667 arc_buf_t **bufp; 4668 uint64_t blksz = hdr->b_size; 4669 uint64_t spa = hdr->b_spa; 4670 arc_buf_contents_t type = arc_buf_type(hdr); 4671 uint32_t flags = hdr->b_flags; 4672 4673 ASSERT(hdr->b_l1hdr.b_buf != buf || buf->b_next != NULL); 4674 /* 4675 * Pull the data off of this hdr and attach it to 4676 * a new anonymous hdr. 4677 */ 4678 (void) remove_reference(hdr, hash_lock, tag); 4679 bufp = &hdr->b_l1hdr.b_buf; 4680 while (*bufp != buf) 4681 bufp = &(*bufp)->b_next; 4682 *bufp = buf->b_next; 4683 buf->b_next = NULL; 4684 4685 ASSERT3P(state, !=, arc_l2c_only); 4686 ASSERT3U(state->arcs_size, >=, hdr->b_size); 4687 atomic_add_64(&state->arcs_size, -hdr->b_size); 4688 if (refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) { 4689 ASSERT3P(state, !=, arc_l2c_only); 4690 uint64_t *size = &state->arcs_lsize[type]; 4691 ASSERT3U(*size, >=, hdr->b_size); 4692 atomic_add_64(size, -hdr->b_size); 4693 } 4694 4695 /* 4696 * We're releasing a duplicate user data buffer, update 4697 * our statistics accordingly. 4698 */ 4699 if (HDR_ISTYPE_DATA(hdr)) { 4700 ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers); 4701 ARCSTAT_INCR(arcstat_duplicate_buffers_size, 4702 -hdr->b_size); 4703 } 4704 hdr->b_l1hdr.b_datacnt -= 1; 4705 arc_cksum_verify(buf); 4706#ifdef illumos 4707 arc_buf_unwatch(buf); 4708#endif 4709 4710 mutex_exit(hash_lock); 4711 4712 nhdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE); 4713 nhdr->b_size = blksz; 4714 nhdr->b_spa = spa; 4715 4716 nhdr->b_flags = flags & ARC_FLAG_L2_WRITING; 4717 nhdr->b_flags |= arc_bufc_to_flags(type); 4718 nhdr->b_flags |= ARC_FLAG_HAS_L1HDR; 4719 4720 nhdr->b_l1hdr.b_buf = buf; 4721 nhdr->b_l1hdr.b_datacnt = 1; 4722 nhdr->b_l1hdr.b_state = arc_anon; 4723 nhdr->b_l1hdr.b_arc_access = 0; 4724 nhdr->b_l1hdr.b_tmp_cdata = NULL; 4725 nhdr->b_freeze_cksum = NULL; 4726 4727 (void) refcount_add(&nhdr->b_l1hdr.b_refcnt, tag); 4728 buf->b_hdr = nhdr; 4729 mutex_exit(&buf->b_evict_lock); 4730 atomic_add_64(&arc_anon->arcs_size, blksz); 4731 } else { 4732 mutex_exit(&buf->b_evict_lock); 4733 ASSERT(refcount_count(&hdr->b_l1hdr.b_refcnt) == 1); 4734 /* protected by hash lock, or hdr is on arc_anon */ 4735 ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 4736 ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 4737 arc_change_state(arc_anon, hdr, hash_lock); 4738 hdr->b_l1hdr.b_arc_access = 0; 4739 mutex_exit(hash_lock); 4740 4741 buf_discard_identity(hdr); 4742 arc_buf_thaw(buf); 4743 } 4744 buf->b_efunc = NULL; 4745 buf->b_private = NULL; 4746} 4747 4748int 4749arc_released(arc_buf_t *buf) 4750{ 4751 int released; 4752 4753 mutex_enter(&buf->b_evict_lock); 4754 released = (buf->b_data != NULL && 4755 buf->b_hdr->b_l1hdr.b_state == arc_anon); 4756 mutex_exit(&buf->b_evict_lock); 4757 return (released); 4758} 4759 4760#ifdef ZFS_DEBUG 4761int 4762arc_referenced(arc_buf_t *buf) 4763{ 4764 int referenced; 4765 4766 mutex_enter(&buf->b_evict_lock); 4767 referenced = (refcount_count(&buf->b_hdr->b_l1hdr.b_refcnt)); 4768 mutex_exit(&buf->b_evict_lock); 4769 return (referenced); 4770} 4771#endif 4772 4773static void 4774arc_write_ready(zio_t *zio) 4775{ 4776 arc_write_callback_t *callback = zio->io_private; 4777 arc_buf_t *buf = callback->awcb_buf; 4778 arc_buf_hdr_t *hdr = buf->b_hdr; 4779 4780 ASSERT(HDR_HAS_L1HDR(hdr)); 4781 ASSERT(!refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt)); 4782 ASSERT(hdr->b_l1hdr.b_datacnt > 0); 4783 callback->awcb_ready(zio, buf, callback->awcb_private); 4784 4785 /* 4786 * If the IO is already in progress, then this is a re-write 4787 * attempt, so we need to thaw and re-compute the cksum. 4788 * It is the responsibility of the callback to handle the 4789 * accounting for any re-write attempt. 4790 */ 4791 if (HDR_IO_IN_PROGRESS(hdr)) { 4792 mutex_enter(&hdr->b_l1hdr.b_freeze_lock); 4793 if (hdr->b_freeze_cksum != NULL) { 4794 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 4795 hdr->b_freeze_cksum = NULL; 4796 } 4797 mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 4798 } 4799 arc_cksum_compute(buf, B_FALSE); 4800 hdr->b_flags |= ARC_FLAG_IO_IN_PROGRESS; 4801} 4802 4803/* 4804 * The SPA calls this callback for each physical write that happens on behalf 4805 * of a logical write. See the comment in dbuf_write_physdone() for details. 4806 */ 4807static void 4808arc_write_physdone(zio_t *zio) 4809{ 4810 arc_write_callback_t *cb = zio->io_private; 4811 if (cb->awcb_physdone != NULL) 4812 cb->awcb_physdone(zio, cb->awcb_buf, cb->awcb_private); 4813} 4814 4815static void 4816arc_write_done(zio_t *zio) 4817{ 4818 arc_write_callback_t *callback = zio->io_private; 4819 arc_buf_t *buf = callback->awcb_buf; 4820 arc_buf_hdr_t *hdr = buf->b_hdr; 4821 4822 ASSERT(hdr->b_l1hdr.b_acb == NULL); 4823 4824 if (zio->io_error == 0) { 4825 if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) { 4826 buf_discard_identity(hdr); 4827 } else { 4828 hdr->b_dva = *BP_IDENTITY(zio->io_bp); 4829 hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); 4830 } 4831 } else { 4832 ASSERT(BUF_EMPTY(hdr)); 4833 } 4834 4835 /* 4836 * If the block to be written was all-zero or compressed enough to be 4837 * embedded in the BP, no write was performed so there will be no 4838 * dva/birth/checksum. The buffer must therefore remain anonymous 4839 * (and uncached). 4840 */ 4841 if (!BUF_EMPTY(hdr)) { 4842 arc_buf_hdr_t *exists; 4843 kmutex_t *hash_lock; 4844 4845 ASSERT(zio->io_error == 0); 4846 4847 arc_cksum_verify(buf); 4848 4849 exists = buf_hash_insert(hdr, &hash_lock); 4850 if (exists != NULL) { 4851 /* 4852 * This can only happen if we overwrite for 4853 * sync-to-convergence, because we remove 4854 * buffers from the hash table when we arc_free(). 4855 */ 4856 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 4857 if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 4858 panic("bad overwrite, hdr=%p exists=%p", 4859 (void *)hdr, (void *)exists); 4860 ASSERT(refcount_is_zero( 4861 &exists->b_l1hdr.b_refcnt)); 4862 arc_change_state(arc_anon, exists, hash_lock); 4863 mutex_exit(hash_lock); 4864 arc_hdr_destroy(exists); 4865 exists = buf_hash_insert(hdr, &hash_lock); 4866 ASSERT3P(exists, ==, NULL); 4867 } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) { 4868 /* nopwrite */ 4869 ASSERT(zio->io_prop.zp_nopwrite); 4870 if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 4871 panic("bad nopwrite, hdr=%p exists=%p", 4872 (void *)hdr, (void *)exists); 4873 } else { 4874 /* Dedup */ 4875 ASSERT(hdr->b_l1hdr.b_datacnt == 1); 4876 ASSERT(hdr->b_l1hdr.b_state == arc_anon); 4877 ASSERT(BP_GET_DEDUP(zio->io_bp)); 4878 ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 4879 } 4880 } 4881 hdr->b_flags &= ~ARC_FLAG_IO_IN_PROGRESS; 4882 /* if it's not anon, we are doing a scrub */ 4883 if (exists == NULL && hdr->b_l1hdr.b_state == arc_anon) 4884 arc_access(hdr, hash_lock); 4885 mutex_exit(hash_lock); 4886 } else { 4887 hdr->b_flags &= ~ARC_FLAG_IO_IN_PROGRESS; 4888 } 4889 4890 ASSERT(!refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 4891 callback->awcb_done(zio, buf, callback->awcb_private); 4892 4893 kmem_free(callback, sizeof (arc_write_callback_t)); 4894} 4895 4896zio_t * 4897arc_write(zio_t *pio, spa_t *spa, uint64_t txg, 4898 blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, boolean_t l2arc_compress, 4899 const zio_prop_t *zp, arc_done_func_t *ready, arc_done_func_t *physdone, 4900 arc_done_func_t *done, void *private, zio_priority_t priority, 4901 int zio_flags, const zbookmark_phys_t *zb) 4902{ 4903 arc_buf_hdr_t *hdr = buf->b_hdr; 4904 arc_write_callback_t *callback; 4905 zio_t *zio; 4906 4907 ASSERT(ready != NULL); 4908 ASSERT(done != NULL); 4909 ASSERT(!HDR_IO_ERROR(hdr)); 4910 ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 4911 ASSERT(hdr->b_l1hdr.b_acb == NULL); 4912 ASSERT(hdr->b_l1hdr.b_datacnt > 0); 4913 if (l2arc) 4914 hdr->b_flags |= ARC_FLAG_L2CACHE; 4915 if (l2arc_compress) 4916 hdr->b_flags |= ARC_FLAG_L2COMPRESS; 4917 callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 4918 callback->awcb_ready = ready; 4919 callback->awcb_physdone = physdone; 4920 callback->awcb_done = done; 4921 callback->awcb_private = private; 4922 callback->awcb_buf = buf; 4923 4924 zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp, 4925 arc_write_ready, arc_write_physdone, arc_write_done, callback, 4926 priority, zio_flags, zb); 4927 4928 return (zio); 4929} 4930 4931static int 4932arc_memory_throttle(uint64_t reserve, uint64_t txg) 4933{ 4934#ifdef _KERNEL 4935 uint64_t available_memory = ptob(freemem); 4936 static uint64_t page_load = 0; 4937 static uint64_t last_txg = 0; 4938 4939#if defined(__i386) || !defined(UMA_MD_SMALL_ALLOC) 4940 available_memory = 4941 MIN(available_memory, ptob(vmem_size(heap_arena, VMEM_FREE))); 4942#endif 4943 4944 if (freemem > (uint64_t)physmem * arc_lotsfree_percent / 100) 4945 return (0); 4946 4947 if (txg > last_txg) { 4948 last_txg = txg; 4949 page_load = 0; 4950 } 4951 /* 4952 * If we are in pageout, we know that memory is already tight, 4953 * the arc is already going to be evicting, so we just want to 4954 * continue to let page writes occur as quickly as possible. 4955 */ 4956 if (curproc == pageproc) { 4957 if (page_load > MAX(ptob(minfree), available_memory) / 4) 4958 return (SET_ERROR(ERESTART)); 4959 /* Note: reserve is inflated, so we deflate */ 4960 page_load += reserve / 8; 4961 return (0); 4962 } else if (page_load > 0 && arc_reclaim_needed()) { 4963 /* memory is low, delay before restarting */ 4964 ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 4965 return (SET_ERROR(EAGAIN)); 4966 } 4967 page_load = 0; 4968#endif 4969 return (0); 4970} 4971 4972void 4973arc_tempreserve_clear(uint64_t reserve) 4974{ 4975 atomic_add_64(&arc_tempreserve, -reserve); 4976 ASSERT((int64_t)arc_tempreserve >= 0); 4977} 4978 4979int 4980arc_tempreserve_space(uint64_t reserve, uint64_t txg) 4981{ 4982 int error; 4983 uint64_t anon_size; 4984 4985 if (reserve > arc_c/4 && !arc_no_grow) { 4986 arc_c = MIN(arc_c_max, reserve * 4); 4987 DTRACE_PROBE1(arc__set_reserve, uint64_t, arc_c); 4988 } 4989 if (reserve > arc_c) 4990 return (SET_ERROR(ENOMEM)); 4991 4992 /* 4993 * Don't count loaned bufs as in flight dirty data to prevent long 4994 * network delays from blocking transactions that are ready to be 4995 * assigned to a txg. 4996 */ 4997 anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0); 4998 4999 /* 5000 * Writes will, almost always, require additional memory allocations 5001 * in order to compress/encrypt/etc the data. We therefore need to 5002 * make sure that there is sufficient available memory for this. 5003 */ 5004 error = arc_memory_throttle(reserve, txg); 5005 if (error != 0) 5006 return (error); 5007 5008 /* 5009 * Throttle writes when the amount of dirty data in the cache 5010 * gets too large. We try to keep the cache less than half full 5011 * of dirty blocks so that our sync times don't grow too large. 5012 * Note: if two requests come in concurrently, we might let them 5013 * both succeed, when one of them should fail. Not a huge deal. 5014 */ 5015 5016 if (reserve + arc_tempreserve + anon_size > arc_c / 2 && 5017 anon_size > arc_c / 4) { 5018 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 5019 "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 5020 arc_tempreserve>>10, 5021 arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 5022 arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 5023 reserve>>10, arc_c>>10); 5024 return (SET_ERROR(ERESTART)); 5025 } 5026 atomic_add_64(&arc_tempreserve, reserve); 5027 return (0); 5028} 5029 5030static void 5031arc_kstat_update_state(arc_state_t *state, kstat_named_t *size, 5032 kstat_named_t *evict_data, kstat_named_t *evict_metadata) 5033{ 5034 size->value.ui64 = state->arcs_size; 5035 evict_data->value.ui64 = state->arcs_lsize[ARC_BUFC_DATA]; 5036 evict_metadata->value.ui64 = state->arcs_lsize[ARC_BUFC_METADATA]; 5037} 5038 5039static int 5040arc_kstat_update(kstat_t *ksp, int rw) 5041{ 5042 arc_stats_t *as = ksp->ks_data; 5043 5044 if (rw == KSTAT_WRITE) { 5045 return (EACCES); 5046 } else { 5047 arc_kstat_update_state(arc_anon, 5048 &as->arcstat_anon_size, 5049 &as->arcstat_anon_evictable_data, 5050 &as->arcstat_anon_evictable_metadata); 5051 arc_kstat_update_state(arc_mru, 5052 &as->arcstat_mru_size, 5053 &as->arcstat_mru_evictable_data, 5054 &as->arcstat_mru_evictable_metadata); 5055 arc_kstat_update_state(arc_mru_ghost, 5056 &as->arcstat_mru_ghost_size, 5057 &as->arcstat_mru_ghost_evictable_data, 5058 &as->arcstat_mru_ghost_evictable_metadata); 5059 arc_kstat_update_state(arc_mfu, 5060 &as->arcstat_mfu_size, 5061 &as->arcstat_mfu_evictable_data, 5062 &as->arcstat_mfu_evictable_metadata); 5063 arc_kstat_update_state(arc_mfu_ghost, 5064 &as->arcstat_mfu_ghost_size, 5065 &as->arcstat_mfu_ghost_evictable_data, 5066 &as->arcstat_mfu_ghost_evictable_metadata); 5067 } 5068 5069 return (0); 5070} 5071 5072/* 5073 * This function *must* return indices evenly distributed between all 5074 * sublists of the multilist. This is needed due to how the ARC eviction 5075 * code is laid out; arc_evict_state() assumes ARC buffers are evenly 5076 * distributed between all sublists and uses this assumption when 5077 * deciding which sublist to evict from and how much to evict from it. 5078 */ 5079unsigned int 5080arc_state_multilist_index_func(multilist_t *ml, void *obj) 5081{ 5082 arc_buf_hdr_t *hdr = obj; 5083 5084 /* 5085 * We rely on b_dva to generate evenly distributed index 5086 * numbers using buf_hash below. So, as an added precaution, 5087 * let's make sure we never add empty buffers to the arc lists. 5088 */ 5089 ASSERT(!BUF_EMPTY(hdr)); 5090 5091 /* 5092 * The assumption here, is the hash value for a given 5093 * arc_buf_hdr_t will remain constant throughout it's lifetime 5094 * (i.e. it's b_spa, b_dva, and b_birth fields don't change). 5095 * Thus, we don't need to store the header's sublist index 5096 * on insertion, as this index can be recalculated on removal. 5097 * 5098 * Also, the low order bits of the hash value are thought to be 5099 * distributed evenly. Otherwise, in the case that the multilist 5100 * has a power of two number of sublists, each sublists' usage 5101 * would not be evenly distributed. 5102 */ 5103 return (buf_hash(hdr->b_spa, &hdr->b_dva, hdr->b_birth) % 5104 multilist_get_num_sublists(ml)); 5105} 5106 5107#ifdef _KERNEL 5108static eventhandler_tag arc_event_lowmem = NULL; 5109 5110static void 5111arc_lowmem(void *arg __unused, int howto __unused) 5112{ 5113 5114 mutex_enter(&arc_reclaim_lock); 5115 /* XXX: Memory deficit should be passed as argument. */ 5116 needfree = btoc(arc_c >> arc_shrink_shift); 5117 DTRACE_PROBE(arc__needfree); 5118 cv_signal(&arc_reclaim_thread_cv); 5119 5120 /* 5121 * It is unsafe to block here in arbitrary threads, because we can come 5122 * here from ARC itself and may hold ARC locks and thus risk a deadlock 5123 * with ARC reclaim thread. 5124 */ 5125 if (curproc == pageproc) 5126 (void) cv_wait(&arc_reclaim_waiters_cv, &arc_reclaim_lock); 5127 mutex_exit(&arc_reclaim_lock); 5128} 5129#endif 5130 5131void 5132arc_init(void) 5133{ 5134 int i, prefetch_tunable_set = 0; 5135 5136 mutex_init(&arc_reclaim_lock, NULL, MUTEX_DEFAULT, NULL); 5137 cv_init(&arc_reclaim_thread_cv, NULL, CV_DEFAULT, NULL); 5138 cv_init(&arc_reclaim_waiters_cv, NULL, CV_DEFAULT, NULL); 5139 5140 mutex_init(&arc_user_evicts_lock, NULL, MUTEX_DEFAULT, NULL); 5141 cv_init(&arc_user_evicts_cv, NULL, CV_DEFAULT, NULL); 5142 5143 /* Convert seconds to clock ticks */ 5144 arc_min_prefetch_lifespan = 1 * hz; 5145 5146 /* Start out with 1/8 of all memory */ 5147 arc_c = kmem_size() / 8; 5148 5149#ifdef illumos 5150#ifdef _KERNEL 5151 /* 5152 * On architectures where the physical memory can be larger 5153 * than the addressable space (intel in 32-bit mode), we may 5154 * need to limit the cache to 1/8 of VM size. 5155 */ 5156 arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 5157#endif 5158#endif /* illumos */ 5159 /* set min cache to 1/32 of all memory, or 16MB, whichever is more */ 5160 arc_c_min = MAX(arc_c / 4, 16 << 20); 5161 /* set max to 1/2 of all memory, or all but 1GB, whichever is more */ 5162 if (arc_c * 8 >= 1 << 30) 5163 arc_c_max = (arc_c * 8) - (1 << 30); 5164 else 5165 arc_c_max = arc_c_min; 5166 arc_c_max = MAX(arc_c * 5, arc_c_max); 5167 5168#ifdef _KERNEL 5169 /* 5170 * Allow the tunables to override our calculations if they are 5171 * reasonable (ie. over 16MB) 5172 */ 5173 if (zfs_arc_max > 16 << 20 && zfs_arc_max < kmem_size()) 5174 arc_c_max = zfs_arc_max; 5175 if (zfs_arc_min > 16 << 20 && zfs_arc_min <= arc_c_max) 5176 arc_c_min = zfs_arc_min; 5177#endif 5178 5179 arc_c = arc_c_max; 5180 arc_p = (arc_c >> 1); 5181 5182 /* limit meta-data to 1/4 of the arc capacity */ 5183 arc_meta_limit = arc_c_max / 4; 5184 5185 /* Allow the tunable to override if it is reasonable */ 5186 if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 5187 arc_meta_limit = zfs_arc_meta_limit; 5188 5189 if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 5190 arc_c_min = arc_meta_limit / 2; 5191 5192 if (zfs_arc_meta_min > 0) { 5193 arc_meta_min = zfs_arc_meta_min; 5194 } else { 5195 arc_meta_min = arc_c_min / 2; 5196 } 5197 5198 if (zfs_arc_grow_retry > 0) 5199 arc_grow_retry = zfs_arc_grow_retry; 5200 5201 if (zfs_arc_shrink_shift > 0) 5202 arc_shrink_shift = zfs_arc_shrink_shift; 5203 5204 /* 5205 * Ensure that arc_no_grow_shift is less than arc_shrink_shift. 5206 */ 5207 if (arc_no_grow_shift >= arc_shrink_shift) 5208 arc_no_grow_shift = arc_shrink_shift - 1; 5209 5210 if (zfs_arc_p_min_shift > 0) 5211 arc_p_min_shift = zfs_arc_p_min_shift; 5212 5213 if (zfs_arc_num_sublists_per_state < 1) 5214 zfs_arc_num_sublists_per_state = MAX(max_ncpus, 1); 5215 5216 /* if kmem_flags are set, lets try to use less memory */ 5217 if (kmem_debugging()) 5218 arc_c = arc_c / 2; 5219 if (arc_c < arc_c_min) 5220 arc_c = arc_c_min; 5221 5222 zfs_arc_min = arc_c_min; 5223 zfs_arc_max = arc_c_max; 5224 5225 arc_anon = &ARC_anon; 5226 arc_mru = &ARC_mru; 5227 arc_mru_ghost = &ARC_mru_ghost; 5228 arc_mfu = &ARC_mfu; 5229 arc_mfu_ghost = &ARC_mfu_ghost; 5230 arc_l2c_only = &ARC_l2c_only; 5231 arc_size = 0; 5232 5233 multilist_create(&arc_mru->arcs_list[ARC_BUFC_METADATA], 5234 sizeof (arc_buf_hdr_t), 5235 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5236 zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5237 multilist_create(&arc_mru->arcs_list[ARC_BUFC_DATA], 5238 sizeof (arc_buf_hdr_t), 5239 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5240 zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5241 multilist_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA], 5242 sizeof (arc_buf_hdr_t), 5243 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5244 zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5245 multilist_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA], 5246 sizeof (arc_buf_hdr_t), 5247 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5248 zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5249 multilist_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA], 5250 sizeof (arc_buf_hdr_t), 5251 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5252 zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5253 multilist_create(&arc_mfu->arcs_list[ARC_BUFC_DATA], 5254 sizeof (arc_buf_hdr_t), 5255 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5256 zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5257 multilist_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA], 5258 sizeof (arc_buf_hdr_t), 5259 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5260 zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5261 multilist_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA], 5262 sizeof (arc_buf_hdr_t), 5263 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5264 zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5265 multilist_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA], 5266 sizeof (arc_buf_hdr_t), 5267 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5268 zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5269 multilist_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA], 5270 sizeof (arc_buf_hdr_t), 5271 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5272 zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5273 5274 buf_init(); 5275 5276 arc_reclaim_thread_exit = FALSE; 5277 arc_user_evicts_thread_exit = FALSE; 5278 arc_eviction_list = NULL; 5279 bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 5280 5281 arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 5282 sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 5283 5284 if (arc_ksp != NULL) { 5285 arc_ksp->ks_data = &arc_stats; 5286 arc_ksp->ks_update = arc_kstat_update; 5287 kstat_install(arc_ksp); 5288 } 5289 5290 (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 5291 TS_RUN, minclsyspri); 5292 5293#ifdef _KERNEL 5294 arc_event_lowmem = EVENTHANDLER_REGISTER(vm_lowmem, arc_lowmem, NULL, 5295 EVENTHANDLER_PRI_FIRST); 5296#endif 5297 5298 (void) thread_create(NULL, 0, arc_user_evicts_thread, NULL, 0, &p0, 5299 TS_RUN, minclsyspri); 5300 5301 arc_dead = FALSE; 5302 arc_warm = B_FALSE; 5303 5304 /* 5305 * Calculate maximum amount of dirty data per pool. 5306 * 5307 * If it has been set by /etc/system, take that. 5308 * Otherwise, use a percentage of physical memory defined by 5309 * zfs_dirty_data_max_percent (default 10%) with a cap at 5310 * zfs_dirty_data_max_max (default 4GB). 5311 */ 5312 if (zfs_dirty_data_max == 0) { 5313 zfs_dirty_data_max = ptob(physmem) * 5314 zfs_dirty_data_max_percent / 100; 5315 zfs_dirty_data_max = MIN(zfs_dirty_data_max, 5316 zfs_dirty_data_max_max); 5317 } 5318 5319#ifdef _KERNEL 5320 if (TUNABLE_INT_FETCH("vfs.zfs.prefetch_disable", &zfs_prefetch_disable)) 5321 prefetch_tunable_set = 1; 5322 5323#ifdef __i386__ 5324 if (prefetch_tunable_set == 0) { 5325 printf("ZFS NOTICE: Prefetch is disabled by default on i386 " 5326 "-- to enable,\n"); 5327 printf(" add \"vfs.zfs.prefetch_disable=0\" " 5328 "to /boot/loader.conf.\n"); 5329 zfs_prefetch_disable = 1; 5330 } 5331#else 5332 if ((((uint64_t)physmem * PAGESIZE) < (1ULL << 32)) && 5333 prefetch_tunable_set == 0) { 5334 printf("ZFS NOTICE: Prefetch is disabled by default if less " 5335 "than 4GB of RAM is present;\n" 5336 " to enable, add \"vfs.zfs.prefetch_disable=0\" " 5337 "to /boot/loader.conf.\n"); 5338 zfs_prefetch_disable = 1; 5339 } 5340#endif 5341 /* Warn about ZFS memory and address space requirements. */ 5342 if (((uint64_t)physmem * PAGESIZE) < (256 + 128 + 64) * (1 << 20)) { 5343 printf("ZFS WARNING: Recommended minimum RAM size is 512MB; " 5344 "expect unstable behavior.\n"); 5345 } 5346 if (kmem_size() < 512 * (1 << 20)) { 5347 printf("ZFS WARNING: Recommended minimum kmem_size is 512MB; " 5348 "expect unstable behavior.\n"); 5349 printf(" Consider tuning vm.kmem_size and " 5350 "vm.kmem_size_max\n"); 5351 printf(" in /boot/loader.conf.\n"); 5352 } 5353#endif 5354} 5355 5356void 5357arc_fini(void) 5358{ 5359 mutex_enter(&arc_reclaim_lock); 5360 arc_reclaim_thread_exit = TRUE; 5361 /* 5362 * The reclaim thread will set arc_reclaim_thread_exit back to 5363 * FALSE when it is finished exiting; we're waiting for that. 5364 */ 5365 while (arc_reclaim_thread_exit) { 5366 cv_signal(&arc_reclaim_thread_cv); 5367 cv_wait(&arc_reclaim_thread_cv, &arc_reclaim_lock); 5368 } 5369 mutex_exit(&arc_reclaim_lock); 5370 5371 mutex_enter(&arc_user_evicts_lock); 5372 arc_user_evicts_thread_exit = TRUE; 5373 /* 5374 * The user evicts thread will set arc_user_evicts_thread_exit 5375 * to FALSE when it is finished exiting; we're waiting for that. 5376 */ 5377 while (arc_user_evicts_thread_exit) { 5378 cv_signal(&arc_user_evicts_cv); 5379 cv_wait(&arc_user_evicts_cv, &arc_user_evicts_lock); 5380 } 5381 mutex_exit(&arc_user_evicts_lock); 5382 5383 /* Use TRUE to ensure *all* buffers are evicted */ 5384 arc_flush(NULL, TRUE); 5385 5386 arc_dead = TRUE; 5387 5388 if (arc_ksp != NULL) { 5389 kstat_delete(arc_ksp); 5390 arc_ksp = NULL; 5391 } 5392 5393 mutex_destroy(&arc_reclaim_lock); 5394 cv_destroy(&arc_reclaim_thread_cv); 5395 cv_destroy(&arc_reclaim_waiters_cv); 5396 5397 mutex_destroy(&arc_user_evicts_lock); 5398 cv_destroy(&arc_user_evicts_cv); 5399 5400 multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]); 5401 multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); 5402 multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]); 5403 multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); 5404 multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]); 5405 multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); 5406 multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]); 5407 multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); 5408 5409 buf_fini(); 5410 5411 ASSERT0(arc_loaned_bytes); 5412 5413#ifdef _KERNEL 5414 if (arc_event_lowmem != NULL) 5415 EVENTHANDLER_DEREGISTER(vm_lowmem, arc_event_lowmem); 5416#endif 5417} 5418 5419/* 5420 * Level 2 ARC 5421 * 5422 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 5423 * It uses dedicated storage devices to hold cached data, which are populated 5424 * using large infrequent writes. The main role of this cache is to boost 5425 * the performance of random read workloads. The intended L2ARC devices 5426 * include short-stroked disks, solid state disks, and other media with 5427 * substantially faster read latency than disk. 5428 * 5429 * +-----------------------+ 5430 * | ARC | 5431 * +-----------------------+ 5432 * | ^ ^ 5433 * | | | 5434 * l2arc_feed_thread() arc_read() 5435 * | | | 5436 * | l2arc read | 5437 * V | | 5438 * +---------------+ | 5439 * | L2ARC | | 5440 * +---------------+ | 5441 * | ^ | 5442 * l2arc_write() | | 5443 * | | | 5444 * V | | 5445 * +-------+ +-------+ 5446 * | vdev | | vdev | 5447 * | cache | | cache | 5448 * +-------+ +-------+ 5449 * +=========+ .-----. 5450 * : L2ARC : |-_____-| 5451 * : devices : | Disks | 5452 * +=========+ `-_____-' 5453 * 5454 * Read requests are satisfied from the following sources, in order: 5455 * 5456 * 1) ARC 5457 * 2) vdev cache of L2ARC devices 5458 * 3) L2ARC devices 5459 * 4) vdev cache of disks 5460 * 5) disks 5461 * 5462 * Some L2ARC device types exhibit extremely slow write performance. 5463 * To accommodate for this there are some significant differences between 5464 * the L2ARC and traditional cache design: 5465 * 5466 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 5467 * the ARC behave as usual, freeing buffers and placing headers on ghost 5468 * lists. The ARC does not send buffers to the L2ARC during eviction as 5469 * this would add inflated write latencies for all ARC memory pressure. 5470 * 5471 * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 5472 * It does this by periodically scanning buffers from the eviction-end of 5473 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 5474 * not already there. It scans until a headroom of buffers is satisfied, 5475 * which itself is a buffer for ARC eviction. If a compressible buffer is 5476 * found during scanning and selected for writing to an L2ARC device, we 5477 * temporarily boost scanning headroom during the next scan cycle to make 5478 * sure we adapt to compression effects (which might significantly reduce 5479 * the data volume we write to L2ARC). The thread that does this is 5480 * l2arc_feed_thread(), illustrated below; example sizes are included to 5481 * provide a better sense of ratio than this diagram: 5482 * 5483 * head --> tail 5484 * +---------------------+----------+ 5485 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 5486 * +---------------------+----------+ | o L2ARC eligible 5487 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 5488 * +---------------------+----------+ | 5489 * 15.9 Gbytes ^ 32 Mbytes | 5490 * headroom | 5491 * l2arc_feed_thread() 5492 * | 5493 * l2arc write hand <--[oooo]--' 5494 * | 8 Mbyte 5495 * | write max 5496 * V 5497 * +==============================+ 5498 * L2ARC dev |####|#|###|###| |####| ... | 5499 * +==============================+ 5500 * 32 Gbytes 5501 * 5502 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 5503 * evicted, then the L2ARC has cached a buffer much sooner than it probably 5504 * needed to, potentially wasting L2ARC device bandwidth and storage. It is 5505 * safe to say that this is an uncommon case, since buffers at the end of 5506 * the ARC lists have moved there due to inactivity. 5507 * 5508 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 5509 * then the L2ARC simply misses copying some buffers. This serves as a 5510 * pressure valve to prevent heavy read workloads from both stalling the ARC 5511 * with waits and clogging the L2ARC with writes. This also helps prevent 5512 * the potential for the L2ARC to churn if it attempts to cache content too 5513 * quickly, such as during backups of the entire pool. 5514 * 5515 * 5. After system boot and before the ARC has filled main memory, there are 5516 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 5517 * lists can remain mostly static. Instead of searching from tail of these 5518 * lists as pictured, the l2arc_feed_thread() will search from the list heads 5519 * for eligible buffers, greatly increasing its chance of finding them. 5520 * 5521 * The L2ARC device write speed is also boosted during this time so that 5522 * the L2ARC warms up faster. Since there have been no ARC evictions yet, 5523 * there are no L2ARC reads, and no fear of degrading read performance 5524 * through increased writes. 5525 * 5526 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 5527 * the vdev queue can aggregate them into larger and fewer writes. Each 5528 * device is written to in a rotor fashion, sweeping writes through 5529 * available space then repeating. 5530 * 5531 * 7. The L2ARC does not store dirty content. It never needs to flush 5532 * write buffers back to disk based storage. 5533 * 5534 * 8. If an ARC buffer is written (and dirtied) which also exists in the 5535 * L2ARC, the now stale L2ARC buffer is immediately dropped. 5536 * 5537 * The performance of the L2ARC can be tweaked by a number of tunables, which 5538 * may be necessary for different workloads: 5539 * 5540 * l2arc_write_max max write bytes per interval 5541 * l2arc_write_boost extra write bytes during device warmup 5542 * l2arc_noprefetch skip caching prefetched buffers 5543 * l2arc_headroom number of max device writes to precache 5544 * l2arc_headroom_boost when we find compressed buffers during ARC 5545 * scanning, we multiply headroom by this 5546 * percentage factor for the next scan cycle, 5547 * since more compressed buffers are likely to 5548 * be present 5549 * l2arc_feed_secs seconds between L2ARC writing 5550 * 5551 * Tunables may be removed or added as future performance improvements are 5552 * integrated, and also may become zpool properties. 5553 * 5554 * There are three key functions that control how the L2ARC warms up: 5555 * 5556 * l2arc_write_eligible() check if a buffer is eligible to cache 5557 * l2arc_write_size() calculate how much to write 5558 * l2arc_write_interval() calculate sleep delay between writes 5559 * 5560 * These three functions determine what to write, how much, and how quickly 5561 * to send writes. 5562 */ 5563 5564static boolean_t 5565l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *hdr) 5566{ 5567 /* 5568 * A buffer is *not* eligible for the L2ARC if it: 5569 * 1. belongs to a different spa. 5570 * 2. is already cached on the L2ARC. 5571 * 3. has an I/O in progress (it may be an incomplete read). 5572 * 4. is flagged not eligible (zfs property). 5573 */ 5574 if (hdr->b_spa != spa_guid) { 5575 ARCSTAT_BUMP(arcstat_l2_write_spa_mismatch); 5576 return (B_FALSE); 5577 } 5578 if (HDR_HAS_L2HDR(hdr)) { 5579 ARCSTAT_BUMP(arcstat_l2_write_in_l2); 5580 return (B_FALSE); 5581 } 5582 if (HDR_IO_IN_PROGRESS(hdr)) { 5583 ARCSTAT_BUMP(arcstat_l2_write_hdr_io_in_progress); 5584 return (B_FALSE); 5585 } 5586 if (!HDR_L2CACHE(hdr)) { 5587 ARCSTAT_BUMP(arcstat_l2_write_not_cacheable); 5588 return (B_FALSE); 5589 } 5590 5591 return (B_TRUE); 5592} 5593 5594static uint64_t 5595l2arc_write_size(void) 5596{ 5597 uint64_t size; 5598 5599 /* 5600 * Make sure our globals have meaningful values in case the user 5601 * altered them. 5602 */ 5603 size = l2arc_write_max; 5604 if (size == 0) { 5605 cmn_err(CE_NOTE, "Bad value for l2arc_write_max, value must " 5606 "be greater than zero, resetting it to the default (%d)", 5607 L2ARC_WRITE_SIZE); 5608 size = l2arc_write_max = L2ARC_WRITE_SIZE; 5609 } 5610 5611 if (arc_warm == B_FALSE) 5612 size += l2arc_write_boost; 5613 5614 return (size); 5615 5616} 5617 5618static clock_t 5619l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 5620{ 5621 clock_t interval, next, now; 5622 5623 /* 5624 * If the ARC lists are busy, increase our write rate; if the 5625 * lists are stale, idle back. This is achieved by checking 5626 * how much we previously wrote - if it was more than half of 5627 * what we wanted, schedule the next write much sooner. 5628 */ 5629 if (l2arc_feed_again && wrote > (wanted / 2)) 5630 interval = (hz * l2arc_feed_min_ms) / 1000; 5631 else 5632 interval = hz * l2arc_feed_secs; 5633 5634 now = ddi_get_lbolt(); 5635 next = MAX(now, MIN(now + interval, began + interval)); 5636 5637 return (next); 5638} 5639 5640/* 5641 * Cycle through L2ARC devices. This is how L2ARC load balances. 5642 * If a device is returned, this also returns holding the spa config lock. 5643 */ 5644static l2arc_dev_t * 5645l2arc_dev_get_next(void) 5646{ 5647 l2arc_dev_t *first, *next = NULL; 5648 5649 /* 5650 * Lock out the removal of spas (spa_namespace_lock), then removal 5651 * of cache devices (l2arc_dev_mtx). Once a device has been selected, 5652 * both locks will be dropped and a spa config lock held instead. 5653 */ 5654 mutex_enter(&spa_namespace_lock); 5655 mutex_enter(&l2arc_dev_mtx); 5656 5657 /* if there are no vdevs, there is nothing to do */ 5658 if (l2arc_ndev == 0) 5659 goto out; 5660 5661 first = NULL; 5662 next = l2arc_dev_last; 5663 do { 5664 /* loop around the list looking for a non-faulted vdev */ 5665 if (next == NULL) { 5666 next = list_head(l2arc_dev_list); 5667 } else { 5668 next = list_next(l2arc_dev_list, next); 5669 if (next == NULL) 5670 next = list_head(l2arc_dev_list); 5671 } 5672 5673 /* if we have come back to the start, bail out */ 5674 if (first == NULL) 5675 first = next; 5676 else if (next == first) 5677 break; 5678 5679 } while (vdev_is_dead(next->l2ad_vdev)); 5680 5681 /* if we were unable to find any usable vdevs, return NULL */ 5682 if (vdev_is_dead(next->l2ad_vdev)) 5683 next = NULL; 5684 5685 l2arc_dev_last = next; 5686 5687out: 5688 mutex_exit(&l2arc_dev_mtx); 5689 5690 /* 5691 * Grab the config lock to prevent the 'next' device from being 5692 * removed while we are writing to it. 5693 */ 5694 if (next != NULL) 5695 spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 5696 mutex_exit(&spa_namespace_lock); 5697 5698 return (next); 5699} 5700 5701/* 5702 * Free buffers that were tagged for destruction. 5703 */ 5704static void 5705l2arc_do_free_on_write() 5706{ 5707 list_t *buflist; 5708 l2arc_data_free_t *df, *df_prev; 5709 5710 mutex_enter(&l2arc_free_on_write_mtx); 5711 buflist = l2arc_free_on_write; 5712 5713 for (df = list_tail(buflist); df; df = df_prev) { 5714 df_prev = list_prev(buflist, df); 5715 ASSERT(df->l2df_data != NULL); 5716 ASSERT(df->l2df_func != NULL); 5717 df->l2df_func(df->l2df_data, df->l2df_size); 5718 list_remove(buflist, df); 5719 kmem_free(df, sizeof (l2arc_data_free_t)); 5720 } 5721 5722 mutex_exit(&l2arc_free_on_write_mtx); 5723} 5724 5725/* 5726 * A write to a cache device has completed. Update all headers to allow 5727 * reads from these buffers to begin. 5728 */ 5729static void 5730l2arc_write_done(zio_t *zio) 5731{ 5732 l2arc_write_callback_t *cb; 5733 l2arc_dev_t *dev; 5734 list_t *buflist; 5735 arc_buf_hdr_t *head, *hdr, *hdr_prev; 5736 kmutex_t *hash_lock; 5737 int64_t bytes_dropped = 0; 5738 5739 cb = zio->io_private; 5740 ASSERT(cb != NULL); 5741 dev = cb->l2wcb_dev; 5742 ASSERT(dev != NULL); 5743 head = cb->l2wcb_head; 5744 ASSERT(head != NULL); 5745 buflist = &dev->l2ad_buflist; 5746 ASSERT(buflist != NULL); 5747 DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 5748 l2arc_write_callback_t *, cb); 5749 5750 if (zio->io_error != 0) 5751 ARCSTAT_BUMP(arcstat_l2_writes_error); 5752 5753 /* 5754 * All writes completed, or an error was hit. 5755 */ 5756top: 5757 mutex_enter(&dev->l2ad_mtx); 5758 for (hdr = list_prev(buflist, head); hdr; hdr = hdr_prev) { 5759 hdr_prev = list_prev(buflist, hdr); 5760 5761 hash_lock = HDR_LOCK(hdr); 5762 5763 /* 5764 * We cannot use mutex_enter or else we can deadlock 5765 * with l2arc_write_buffers (due to swapping the order 5766 * the hash lock and l2ad_mtx are taken). 5767 */ 5768 if (!mutex_tryenter(hash_lock)) { 5769 /* 5770 * Missed the hash lock. We must retry so we 5771 * don't leave the ARC_FLAG_L2_WRITING bit set. 5772 */ 5773 ARCSTAT_BUMP(arcstat_l2_writes_lock_retry); 5774 5775 /* 5776 * We don't want to rescan the headers we've 5777 * already marked as having been written out, so 5778 * we reinsert the head node so we can pick up 5779 * where we left off. 5780 */ 5781 list_remove(buflist, head); 5782 list_insert_after(buflist, hdr, head); 5783 5784 mutex_exit(&dev->l2ad_mtx); 5785 5786 /* 5787 * We wait for the hash lock to become available 5788 * to try and prevent busy waiting, and increase 5789 * the chance we'll be able to acquire the lock 5790 * the next time around. 5791 */ 5792 mutex_enter(hash_lock); 5793 mutex_exit(hash_lock); 5794 goto top; 5795 } 5796 5797 /* 5798 * We could not have been moved into the arc_l2c_only 5799 * state while in-flight due to our ARC_FLAG_L2_WRITING 5800 * bit being set. Let's just ensure that's being enforced. 5801 */ 5802 ASSERT(HDR_HAS_L1HDR(hdr)); 5803 5804 /* 5805 * We may have allocated a buffer for L2ARC compression, 5806 * we must release it to avoid leaking this data. 5807 */ 5808 l2arc_release_cdata_buf(hdr); 5809 5810 if (zio->io_error != 0) { 5811 /* 5812 * Error - drop L2ARC entry. 5813 */ 5814 trim_map_free(hdr->b_l2hdr.b_dev->l2ad_vdev, 5815 hdr->b_l2hdr.b_daddr, hdr->b_l2hdr.b_asize, 0); 5816 hdr->b_flags &= ~ARC_FLAG_HAS_L2HDR; 5817 5818 ARCSTAT_INCR(arcstat_l2_asize, -hdr->b_l2hdr.b_asize); 5819 ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 5820 5821 bytes_dropped += hdr->b_l2hdr.b_asize; 5822 (void) refcount_remove_many(&dev->l2ad_alloc, 5823 hdr->b_l2hdr.b_asize, hdr); 5824 } 5825 5826 /* 5827 * Allow ARC to begin reads and ghost list evictions to 5828 * this L2ARC entry. 5829 */ 5830 hdr->b_flags &= ~ARC_FLAG_L2_WRITING; 5831 5832 mutex_exit(hash_lock); 5833 } 5834 5835 atomic_inc_64(&l2arc_writes_done); 5836 list_remove(buflist, head); 5837 ASSERT(!HDR_HAS_L1HDR(head)); 5838 kmem_cache_free(hdr_l2only_cache, head); 5839 mutex_exit(&dev->l2ad_mtx); 5840 5841 vdev_space_update(dev->l2ad_vdev, -bytes_dropped, 0, 0); 5842 5843 l2arc_do_free_on_write(); 5844 5845 kmem_free(cb, sizeof (l2arc_write_callback_t)); 5846} 5847 5848/* 5849 * A read to a cache device completed. Validate buffer contents before 5850 * handing over to the regular ARC routines. 5851 */ 5852static void 5853l2arc_read_done(zio_t *zio) 5854{ 5855 l2arc_read_callback_t *cb; 5856 arc_buf_hdr_t *hdr; 5857 arc_buf_t *buf; 5858 kmutex_t *hash_lock; 5859 int equal; 5860 5861 ASSERT(zio->io_vd != NULL); 5862 ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 5863 5864 spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 5865 5866 cb = zio->io_private; 5867 ASSERT(cb != NULL); 5868 buf = cb->l2rcb_buf; 5869 ASSERT(buf != NULL); 5870 5871 hash_lock = HDR_LOCK(buf->b_hdr); 5872 mutex_enter(hash_lock); 5873 hdr = buf->b_hdr; 5874 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 5875 5876 /* 5877 * If the buffer was compressed, decompress it first. 5878 */ 5879 if (cb->l2rcb_compress != ZIO_COMPRESS_OFF) 5880 l2arc_decompress_zio(zio, hdr, cb->l2rcb_compress); 5881 ASSERT(zio->io_data != NULL); 5882 5883 /* 5884 * Check this survived the L2ARC journey. 5885 */ 5886 equal = arc_cksum_equal(buf); 5887 if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 5888 mutex_exit(hash_lock); 5889 zio->io_private = buf; 5890 zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 5891 zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 5892 arc_read_done(zio); 5893 } else { 5894 mutex_exit(hash_lock); 5895 /* 5896 * Buffer didn't survive caching. Increment stats and 5897 * reissue to the original storage device. 5898 */ 5899 if (zio->io_error != 0) { 5900 ARCSTAT_BUMP(arcstat_l2_io_error); 5901 } else { 5902 zio->io_error = SET_ERROR(EIO); 5903 } 5904 if (!equal) 5905 ARCSTAT_BUMP(arcstat_l2_cksum_bad); 5906 5907 /* 5908 * If there's no waiter, issue an async i/o to the primary 5909 * storage now. If there *is* a waiter, the caller must 5910 * issue the i/o in a context where it's OK to block. 5911 */ 5912 if (zio->io_waiter == NULL) { 5913 zio_t *pio = zio_unique_parent(zio); 5914 5915 ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 5916 5917 zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp, 5918 buf->b_data, zio->io_size, arc_read_done, buf, 5919 zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb)); 5920 } 5921 } 5922 5923 kmem_free(cb, sizeof (l2arc_read_callback_t)); 5924} 5925 5926/* 5927 * This is the list priority from which the L2ARC will search for pages to 5928 * cache. This is used within loops (0..3) to cycle through lists in the 5929 * desired order. This order can have a significant effect on cache 5930 * performance. 5931 * 5932 * Currently the metadata lists are hit first, MFU then MRU, followed by 5933 * the data lists. This function returns a locked list, and also returns 5934 * the lock pointer. 5935 */ 5936static multilist_sublist_t * 5937l2arc_sublist_lock(int list_num) 5938{ 5939 multilist_t *ml = NULL; 5940 unsigned int idx; 5941 5942 ASSERT(list_num >= 0 && list_num <= 3); 5943 5944 switch (list_num) { 5945 case 0: 5946 ml = &arc_mfu->arcs_list[ARC_BUFC_METADATA]; 5947 break; 5948 case 1: 5949 ml = &arc_mru->arcs_list[ARC_BUFC_METADATA]; 5950 break; 5951 case 2: 5952 ml = &arc_mfu->arcs_list[ARC_BUFC_DATA]; 5953 break; 5954 case 3: 5955 ml = &arc_mru->arcs_list[ARC_BUFC_DATA]; 5956 break; 5957 } 5958 5959 /* 5960 * Return a randomly-selected sublist. This is acceptable 5961 * because the caller feeds only a little bit of data for each 5962 * call (8MB). Subsequent calls will result in different 5963 * sublists being selected. 5964 */ 5965 idx = multilist_get_random_index(ml); 5966 return (multilist_sublist_lock(ml, idx)); 5967} 5968 5969/* 5970 * Evict buffers from the device write hand to the distance specified in 5971 * bytes. This distance may span populated buffers, it may span nothing. 5972 * This is clearing a region on the L2ARC device ready for writing. 5973 * If the 'all' boolean is set, every buffer is evicted. 5974 */ 5975static void 5976l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 5977{ 5978 list_t *buflist; 5979 arc_buf_hdr_t *hdr, *hdr_prev; 5980 kmutex_t *hash_lock; 5981 uint64_t taddr; 5982 5983 buflist = &dev->l2ad_buflist; 5984 5985 if (!all && dev->l2ad_first) { 5986 /* 5987 * This is the first sweep through the device. There is 5988 * nothing to evict. 5989 */ 5990 return; 5991 } 5992 5993 if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 5994 /* 5995 * When nearing the end of the device, evict to the end 5996 * before the device write hand jumps to the start. 5997 */ 5998 taddr = dev->l2ad_end; 5999 } else { 6000 taddr = dev->l2ad_hand + distance; 6001 } 6002 DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 6003 uint64_t, taddr, boolean_t, all); 6004 6005top: 6006 mutex_enter(&dev->l2ad_mtx); 6007 for (hdr = list_tail(buflist); hdr; hdr = hdr_prev) { 6008 hdr_prev = list_prev(buflist, hdr); 6009 6010 hash_lock = HDR_LOCK(hdr); 6011 6012 /* 6013 * We cannot use mutex_enter or else we can deadlock 6014 * with l2arc_write_buffers (due to swapping the order 6015 * the hash lock and l2ad_mtx are taken). 6016 */ 6017 if (!mutex_tryenter(hash_lock)) { 6018 /* 6019 * Missed the hash lock. Retry. 6020 */ 6021 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 6022 mutex_exit(&dev->l2ad_mtx); 6023 mutex_enter(hash_lock); 6024 mutex_exit(hash_lock); 6025 goto top; 6026 } 6027 6028 if (HDR_L2_WRITE_HEAD(hdr)) { 6029 /* 6030 * We hit a write head node. Leave it for 6031 * l2arc_write_done(). 6032 */ 6033 list_remove(buflist, hdr); 6034 mutex_exit(hash_lock); 6035 continue; 6036 } 6037 6038 if (!all && HDR_HAS_L2HDR(hdr) && 6039 (hdr->b_l2hdr.b_daddr > taddr || 6040 hdr->b_l2hdr.b_daddr < dev->l2ad_hand)) { 6041 /* 6042 * We've evicted to the target address, 6043 * or the end of the device. 6044 */ 6045 mutex_exit(hash_lock); 6046 break; 6047 } 6048 6049 ASSERT(HDR_HAS_L2HDR(hdr)); 6050 if (!HDR_HAS_L1HDR(hdr)) { 6051 ASSERT(!HDR_L2_READING(hdr)); 6052 /* 6053 * This doesn't exist in the ARC. Destroy. 6054 * arc_hdr_destroy() will call list_remove() 6055 * and decrement arcstat_l2_size. 6056 */ 6057 arc_change_state(arc_anon, hdr, hash_lock); 6058 arc_hdr_destroy(hdr); 6059 } else { 6060 ASSERT(hdr->b_l1hdr.b_state != arc_l2c_only); 6061 ARCSTAT_BUMP(arcstat_l2_evict_l1cached); 6062 /* 6063 * Invalidate issued or about to be issued 6064 * reads, since we may be about to write 6065 * over this location. 6066 */ 6067 if (HDR_L2_READING(hdr)) { 6068 ARCSTAT_BUMP(arcstat_l2_evict_reading); 6069 hdr->b_flags |= ARC_FLAG_L2_EVICTED; 6070 } 6071 6072 /* Ensure this header has finished being written */ 6073 ASSERT(!HDR_L2_WRITING(hdr)); 6074 ASSERT3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL); 6075 6076 arc_hdr_l2hdr_destroy(hdr); 6077 } 6078 mutex_exit(hash_lock); 6079 } 6080 mutex_exit(&dev->l2ad_mtx); 6081} 6082 6083/* 6084 * Find and write ARC buffers to the L2ARC device. 6085 * 6086 * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid 6087 * for reading until they have completed writing. 6088 * The headroom_boost is an in-out parameter used to maintain headroom boost 6089 * state between calls to this function. 6090 * 6091 * Returns the number of bytes actually written (which may be smaller than 6092 * the delta by which the device hand has changed due to alignment). 6093 */ 6094static uint64_t 6095l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz, 6096 boolean_t *headroom_boost) 6097{ 6098 arc_buf_hdr_t *hdr, *hdr_prev, *head; 6099 uint64_t write_asize, write_psize, write_sz, headroom, 6100 buf_compress_minsz; 6101 void *buf_data; 6102 boolean_t full; 6103 l2arc_write_callback_t *cb; 6104 zio_t *pio, *wzio; 6105 uint64_t guid = spa_load_guid(spa); 6106 const boolean_t do_headroom_boost = *headroom_boost; 6107 int try; 6108 6109 ASSERT(dev->l2ad_vdev != NULL); 6110 6111 /* Lower the flag now, we might want to raise it again later. */ 6112 *headroom_boost = B_FALSE; 6113 6114 pio = NULL; 6115 write_sz = write_asize = write_psize = 0; 6116 full = B_FALSE; 6117 head = kmem_cache_alloc(hdr_l2only_cache, KM_PUSHPAGE); 6118 head->b_flags |= ARC_FLAG_L2_WRITE_HEAD; 6119 head->b_flags |= ARC_FLAG_HAS_L2HDR; 6120 6121 ARCSTAT_BUMP(arcstat_l2_write_buffer_iter); 6122 /* 6123 * We will want to try to compress buffers that are at least 2x the 6124 * device sector size. 6125 */ 6126 buf_compress_minsz = 2 << dev->l2ad_vdev->vdev_ashift; 6127 6128 /* 6129 * Copy buffers for L2ARC writing. 6130 */ 6131 for (try = 0; try <= 3; try++) { 6132 multilist_sublist_t *mls = l2arc_sublist_lock(try); 6133 uint64_t passed_sz = 0; 6134 6135 ARCSTAT_BUMP(arcstat_l2_write_buffer_list_iter); 6136 6137 /* 6138 * L2ARC fast warmup. 6139 * 6140 * Until the ARC is warm and starts to evict, read from the 6141 * head of the ARC lists rather than the tail. 6142 */ 6143 if (arc_warm == B_FALSE) 6144 hdr = multilist_sublist_head(mls); 6145 else 6146 hdr = multilist_sublist_tail(mls); 6147 if (hdr == NULL) 6148 ARCSTAT_BUMP(arcstat_l2_write_buffer_list_null_iter); 6149 6150 headroom = target_sz * l2arc_headroom; 6151 if (do_headroom_boost) 6152 headroom = (headroom * l2arc_headroom_boost) / 100; 6153 6154 for (; hdr; hdr = hdr_prev) { 6155 kmutex_t *hash_lock; 6156 uint64_t buf_sz; 6157 6158 if (arc_warm == B_FALSE) 6159 hdr_prev = multilist_sublist_next(mls, hdr); 6160 else 6161 hdr_prev = multilist_sublist_prev(mls, hdr); 6162 ARCSTAT_INCR(arcstat_l2_write_buffer_bytes_scanned, hdr->b_size); 6163 6164 hash_lock = HDR_LOCK(hdr); 6165 if (!mutex_tryenter(hash_lock)) { 6166 ARCSTAT_BUMP(arcstat_l2_write_trylock_fail); 6167 /* 6168 * Skip this buffer rather than waiting. 6169 */ 6170 continue; 6171 } 6172 6173 passed_sz += hdr->b_size; 6174 if (passed_sz > headroom) { 6175 /* 6176 * Searched too far. 6177 */ 6178 mutex_exit(hash_lock); 6179 ARCSTAT_BUMP(arcstat_l2_write_passed_headroom); 6180 break; 6181 } 6182 6183 if (!l2arc_write_eligible(guid, hdr)) { 6184 mutex_exit(hash_lock); 6185 continue; 6186 } 6187 6188 if ((write_sz + hdr->b_size) > target_sz) { 6189 full = B_TRUE; 6190 mutex_exit(hash_lock); 6191 ARCSTAT_BUMP(arcstat_l2_write_full); 6192 break; 6193 } 6194 6195 if (pio == NULL) { 6196 /* 6197 * Insert a dummy header on the buflist so 6198 * l2arc_write_done() can find where the 6199 * write buffers begin without searching. 6200 */ 6201 mutex_enter(&dev->l2ad_mtx); 6202 list_insert_head(&dev->l2ad_buflist, head); 6203 mutex_exit(&dev->l2ad_mtx); 6204 6205 cb = kmem_alloc( 6206 sizeof (l2arc_write_callback_t), KM_SLEEP); 6207 cb->l2wcb_dev = dev; 6208 cb->l2wcb_head = head; 6209 pio = zio_root(spa, l2arc_write_done, cb, 6210 ZIO_FLAG_CANFAIL); 6211 ARCSTAT_BUMP(arcstat_l2_write_pios); 6212 } 6213 6214 /* 6215 * Create and add a new L2ARC header. 6216 */ 6217 hdr->b_l2hdr.b_dev = dev; 6218 hdr->b_flags |= ARC_FLAG_L2_WRITING; 6219 /* 6220 * Temporarily stash the data buffer in b_tmp_cdata. 6221 * The subsequent write step will pick it up from 6222 * there. This is because can't access b_l1hdr.b_buf 6223 * without holding the hash_lock, which we in turn 6224 * can't access without holding the ARC list locks 6225 * (which we want to avoid during compression/writing). 6226 */ 6227 HDR_SET_COMPRESS(hdr, ZIO_COMPRESS_OFF); 6228 hdr->b_l2hdr.b_asize = hdr->b_size; 6229 hdr->b_l1hdr.b_tmp_cdata = hdr->b_l1hdr.b_buf->b_data; 6230 6231 /* 6232 * Explicitly set the b_daddr field to a known 6233 * value which means "invalid address". This 6234 * enables us to differentiate which stage of 6235 * l2arc_write_buffers() the particular header 6236 * is in (e.g. this loop, or the one below). 6237 * ARC_FLAG_L2_WRITING is not enough to make 6238 * this distinction, and we need to know in 6239 * order to do proper l2arc vdev accounting in 6240 * arc_release() and arc_hdr_destroy(). 6241 * 6242 * Note, we can't use a new flag to distinguish 6243 * the two stages because we don't hold the 6244 * header's hash_lock below, in the second stage 6245 * of this function. Thus, we can't simply 6246 * change the b_flags field to denote that the 6247 * IO has been sent. We can change the b_daddr 6248 * field of the L2 portion, though, since we'll 6249 * be holding the l2ad_mtx; which is why we're 6250 * using it to denote the header's state change. 6251 */ 6252 hdr->b_l2hdr.b_daddr = L2ARC_ADDR_UNSET; 6253 6254 buf_sz = hdr->b_size; 6255 hdr->b_flags |= ARC_FLAG_HAS_L2HDR; 6256 6257 mutex_enter(&dev->l2ad_mtx); 6258 list_insert_head(&dev->l2ad_buflist, hdr); 6259 mutex_exit(&dev->l2ad_mtx); 6260 6261 /* 6262 * Compute and store the buffer cksum before 6263 * writing. On debug the cksum is verified first. 6264 */ 6265 arc_cksum_verify(hdr->b_l1hdr.b_buf); 6266 arc_cksum_compute(hdr->b_l1hdr.b_buf, B_TRUE); 6267 6268 mutex_exit(hash_lock); 6269 6270 write_sz += buf_sz; 6271 } 6272 6273 multilist_sublist_unlock(mls); 6274 6275 if (full == B_TRUE) 6276 break; 6277 } 6278 6279 /* No buffers selected for writing? */ 6280 if (pio == NULL) { 6281 ASSERT0(write_sz); 6282 ASSERT(!HDR_HAS_L1HDR(head)); 6283 kmem_cache_free(hdr_l2only_cache, head); 6284 return (0); 6285 } 6286 6287 mutex_enter(&dev->l2ad_mtx); 6288 6289 /* 6290 * Now start writing the buffers. We're starting at the write head 6291 * and work backwards, retracing the course of the buffer selector 6292 * loop above. 6293 */ 6294 for (hdr = list_prev(&dev->l2ad_buflist, head); hdr; 6295 hdr = list_prev(&dev->l2ad_buflist, hdr)) { 6296 uint64_t buf_sz; 6297 6298 /* 6299 * We rely on the L1 portion of the header below, so 6300 * it's invalid for this header to have been evicted out 6301 * of the ghost cache, prior to being written out. The 6302 * ARC_FLAG_L2_WRITING bit ensures this won't happen. 6303 */ 6304 ASSERT(HDR_HAS_L1HDR(hdr)); 6305 6306 /* 6307 * We shouldn't need to lock the buffer here, since we flagged 6308 * it as ARC_FLAG_L2_WRITING in the previous step, but we must 6309 * take care to only access its L2 cache parameters. In 6310 * particular, hdr->l1hdr.b_buf may be invalid by now due to 6311 * ARC eviction. 6312 */ 6313 hdr->b_l2hdr.b_daddr = dev->l2ad_hand; 6314 6315 if ((HDR_L2COMPRESS(hdr)) && 6316 hdr->b_l2hdr.b_asize >= buf_compress_minsz) { 6317 if (l2arc_compress_buf(hdr)) { 6318 /* 6319 * If compression succeeded, enable headroom 6320 * boost on the next scan cycle. 6321 */ 6322 *headroom_boost = B_TRUE; 6323 } 6324 } 6325 6326 /* 6327 * Pick up the buffer data we had previously stashed away 6328 * (and now potentially also compressed). 6329 */ 6330 buf_data = hdr->b_l1hdr.b_tmp_cdata; 6331 buf_sz = hdr->b_l2hdr.b_asize; 6332 6333 /* 6334 * If the data has not been compressed, then clear b_tmp_cdata 6335 * to make sure that it points only to a temporary compression 6336 * buffer. 6337 */ 6338 if (!L2ARC_IS_VALID_COMPRESS(HDR_GET_COMPRESS(hdr))) 6339 hdr->b_l1hdr.b_tmp_cdata = NULL; 6340 6341 /* 6342 * We need to do this regardless if buf_sz is zero or 6343 * not, otherwise, when this l2hdr is evicted we'll 6344 * remove a reference that was never added. 6345 */ 6346 (void) refcount_add_many(&dev->l2ad_alloc, buf_sz, hdr); 6347 6348 /* Compression may have squashed the buffer to zero length. */ 6349 if (buf_sz != 0) { 6350 uint64_t buf_p_sz; 6351 6352 wzio = zio_write_phys(pio, dev->l2ad_vdev, 6353 dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 6354 NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 6355 ZIO_FLAG_CANFAIL, B_FALSE); 6356 6357 DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 6358 zio_t *, wzio); 6359 (void) zio_nowait(wzio); 6360 6361 write_asize += buf_sz; 6362 6363 /* 6364 * Keep the clock hand suitably device-aligned. 6365 */ 6366 buf_p_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 6367 write_psize += buf_p_sz; 6368 dev->l2ad_hand += buf_p_sz; 6369 } 6370 } 6371 6372 mutex_exit(&dev->l2ad_mtx); 6373 6374 ASSERT3U(write_asize, <=, target_sz); 6375 ARCSTAT_BUMP(arcstat_l2_writes_sent); 6376 ARCSTAT_INCR(arcstat_l2_write_bytes, write_asize); 6377 ARCSTAT_INCR(arcstat_l2_size, write_sz); 6378 ARCSTAT_INCR(arcstat_l2_asize, write_asize); 6379 vdev_space_update(dev->l2ad_vdev, write_asize, 0, 0); 6380 6381 /* 6382 * Bump device hand to the device start if it is approaching the end. 6383 * l2arc_evict() will already have evicted ahead for this case. 6384 */ 6385 if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 6386 dev->l2ad_hand = dev->l2ad_start; 6387 dev->l2ad_first = B_FALSE; 6388 } 6389 6390 dev->l2ad_writing = B_TRUE; 6391 (void) zio_wait(pio); 6392 dev->l2ad_writing = B_FALSE; 6393 6394 return (write_asize); 6395} 6396 6397/* 6398 * Compresses an L2ARC buffer. 6399 * The data to be compressed must be prefilled in l1hdr.b_tmp_cdata and its 6400 * size in l2hdr->b_asize. This routine tries to compress the data and 6401 * depending on the compression result there are three possible outcomes: 6402 * *) The buffer was incompressible. The original l2hdr contents were left 6403 * untouched and are ready for writing to an L2 device. 6404 * *) The buffer was all-zeros, so there is no need to write it to an L2 6405 * device. To indicate this situation b_tmp_cdata is NULL'ed, b_asize is 6406 * set to zero and b_compress is set to ZIO_COMPRESS_EMPTY. 6407 * *) Compression succeeded and b_tmp_cdata was replaced with a temporary 6408 * data buffer which holds the compressed data to be written, and b_asize 6409 * tells us how much data there is. b_compress is set to the appropriate 6410 * compression algorithm. Once writing is done, invoke 6411 * l2arc_release_cdata_buf on this l2hdr to free this temporary buffer. 6412 * 6413 * Returns B_TRUE if compression succeeded, or B_FALSE if it didn't (the 6414 * buffer was incompressible). 6415 */ 6416static boolean_t 6417l2arc_compress_buf(arc_buf_hdr_t *hdr) 6418{ 6419 void *cdata; 6420 size_t csize, len, rounded; 6421 ASSERT(HDR_HAS_L2HDR(hdr)); 6422 l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr; 6423 6424 ASSERT(HDR_HAS_L1HDR(hdr)); 6425 ASSERT(HDR_GET_COMPRESS(hdr) == ZIO_COMPRESS_OFF); 6426 ASSERT(hdr->b_l1hdr.b_tmp_cdata != NULL); 6427 6428 len = l2hdr->b_asize; 6429 cdata = zio_data_buf_alloc(len); 6430 ASSERT3P(cdata, !=, NULL); 6431 csize = zio_compress_data(ZIO_COMPRESS_LZ4, hdr->b_l1hdr.b_tmp_cdata, 6432 cdata, l2hdr->b_asize); 6433 6434 if (csize == 0) { 6435 /* zero block, indicate that there's nothing to write */ 6436 zio_data_buf_free(cdata, len); 6437 HDR_SET_COMPRESS(hdr, ZIO_COMPRESS_EMPTY); 6438 l2hdr->b_asize = 0; 6439 hdr->b_l1hdr.b_tmp_cdata = NULL; 6440 ARCSTAT_BUMP(arcstat_l2_compress_zeros); 6441 return (B_TRUE); 6442 } 6443 6444 rounded = P2ROUNDUP(csize, 6445 (size_t)1 << l2hdr->b_dev->l2ad_vdev->vdev_ashift); 6446 if (rounded < len) { 6447 /* 6448 * Compression succeeded, we'll keep the cdata around for 6449 * writing and release it afterwards. 6450 */ 6451 if (rounded > csize) { 6452 bzero((char *)cdata + csize, rounded - csize); 6453 csize = rounded; 6454 } 6455 HDR_SET_COMPRESS(hdr, ZIO_COMPRESS_LZ4); 6456 l2hdr->b_asize = csize; 6457 hdr->b_l1hdr.b_tmp_cdata = cdata; 6458 ARCSTAT_BUMP(arcstat_l2_compress_successes); 6459 return (B_TRUE); 6460 } else { 6461 /* 6462 * Compression failed, release the compressed buffer. 6463 * l2hdr will be left unmodified. 6464 */ 6465 zio_data_buf_free(cdata, len); 6466 ARCSTAT_BUMP(arcstat_l2_compress_failures); 6467 return (B_FALSE); 6468 } 6469} 6470 6471/* 6472 * Decompresses a zio read back from an l2arc device. On success, the 6473 * underlying zio's io_data buffer is overwritten by the uncompressed 6474 * version. On decompression error (corrupt compressed stream), the 6475 * zio->io_error value is set to signal an I/O error. 6476 * 6477 * Please note that the compressed data stream is not checksummed, so 6478 * if the underlying device is experiencing data corruption, we may feed 6479 * corrupt data to the decompressor, so the decompressor needs to be 6480 * able to handle this situation (LZ4 does). 6481 */ 6482static void 6483l2arc_decompress_zio(zio_t *zio, arc_buf_hdr_t *hdr, enum zio_compress c) 6484{ 6485 ASSERT(L2ARC_IS_VALID_COMPRESS(c)); 6486 6487 if (zio->io_error != 0) { 6488 /* 6489 * An io error has occured, just restore the original io 6490 * size in preparation for a main pool read. 6491 */ 6492 zio->io_orig_size = zio->io_size = hdr->b_size; 6493 return; 6494 } 6495 6496 if (c == ZIO_COMPRESS_EMPTY) { 6497 /* 6498 * An empty buffer results in a null zio, which means we 6499 * need to fill its io_data after we're done restoring the 6500 * buffer's contents. 6501 */ 6502 ASSERT(hdr->b_l1hdr.b_buf != NULL); 6503 bzero(hdr->b_l1hdr.b_buf->b_data, hdr->b_size); 6504 zio->io_data = zio->io_orig_data = hdr->b_l1hdr.b_buf->b_data; 6505 } else { 6506 ASSERT(zio->io_data != NULL); 6507 /* 6508 * We copy the compressed data from the start of the arc buffer 6509 * (the zio_read will have pulled in only what we need, the 6510 * rest is garbage which we will overwrite at decompression) 6511 * and then decompress back to the ARC data buffer. This way we 6512 * can minimize copying by simply decompressing back over the 6513 * original compressed data (rather than decompressing to an 6514 * aux buffer and then copying back the uncompressed buffer, 6515 * which is likely to be much larger). 6516 */ 6517 uint64_t csize; 6518 void *cdata; 6519 6520 csize = zio->io_size; 6521 cdata = zio_data_buf_alloc(csize); 6522 bcopy(zio->io_data, cdata, csize); 6523 if (zio_decompress_data(c, cdata, zio->io_data, csize, 6524 hdr->b_size) != 0) 6525 zio->io_error = EIO; 6526 zio_data_buf_free(cdata, csize); 6527 } 6528 6529 /* Restore the expected uncompressed IO size. */ 6530 zio->io_orig_size = zio->io_size = hdr->b_size; 6531} 6532 6533/* 6534 * Releases the temporary b_tmp_cdata buffer in an l2arc header structure. 6535 * This buffer serves as a temporary holder of compressed data while 6536 * the buffer entry is being written to an l2arc device. Once that is 6537 * done, we can dispose of it. 6538 */ 6539static void 6540l2arc_release_cdata_buf(arc_buf_hdr_t *hdr) 6541{ 6542 enum zio_compress comp = HDR_GET_COMPRESS(hdr); 6543 6544 ASSERT(HDR_HAS_L1HDR(hdr)); 6545 ASSERT(comp == ZIO_COMPRESS_OFF || L2ARC_IS_VALID_COMPRESS(comp)); 6546 6547 if (comp == ZIO_COMPRESS_OFF) { 6548 /* 6549 * In this case, b_tmp_cdata points to the same buffer 6550 * as the arc_buf_t's b_data field. We don't want to 6551 * free it, since the arc_buf_t will handle that. 6552 */ 6553 hdr->b_l1hdr.b_tmp_cdata = NULL; 6554 } else if (comp == ZIO_COMPRESS_EMPTY) { 6555 /* 6556 * In this case, b_tmp_cdata was compressed to an empty 6557 * buffer, thus there's nothing to free and b_tmp_cdata 6558 * should have been set to NULL in l2arc_write_buffers(). 6559 */ 6560 ASSERT3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL); 6561 } else { 6562 /* 6563 * If the data was compressed, then we've allocated a 6564 * temporary buffer for it, so now we need to release it. 6565 */ 6566 ASSERT(hdr->b_l1hdr.b_tmp_cdata != NULL); 6567 zio_data_buf_free(hdr->b_l1hdr.b_tmp_cdata, 6568 hdr->b_size); 6569 hdr->b_l1hdr.b_tmp_cdata = NULL; 6570 } 6571} 6572 6573/* 6574 * This thread feeds the L2ARC at regular intervals. This is the beating 6575 * heart of the L2ARC. 6576 */ 6577static void 6578l2arc_feed_thread(void *dummy __unused) 6579{ 6580 callb_cpr_t cpr; 6581 l2arc_dev_t *dev; 6582 spa_t *spa; 6583 uint64_t size, wrote; 6584 clock_t begin, next = ddi_get_lbolt(); 6585 boolean_t headroom_boost = B_FALSE; 6586 6587 CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 6588 6589 mutex_enter(&l2arc_feed_thr_lock); 6590 6591 while (l2arc_thread_exit == 0) { 6592 CALLB_CPR_SAFE_BEGIN(&cpr); 6593 (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 6594 next - ddi_get_lbolt()); 6595 CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 6596 next = ddi_get_lbolt() + hz; 6597 6598 /* 6599 * Quick check for L2ARC devices. 6600 */ 6601 mutex_enter(&l2arc_dev_mtx); 6602 if (l2arc_ndev == 0) { 6603 mutex_exit(&l2arc_dev_mtx); 6604 continue; 6605 } 6606 mutex_exit(&l2arc_dev_mtx); 6607 begin = ddi_get_lbolt(); 6608 6609 /* 6610 * This selects the next l2arc device to write to, and in 6611 * doing so the next spa to feed from: dev->l2ad_spa. This 6612 * will return NULL if there are now no l2arc devices or if 6613 * they are all faulted. 6614 * 6615 * If a device is returned, its spa's config lock is also 6616 * held to prevent device removal. l2arc_dev_get_next() 6617 * will grab and release l2arc_dev_mtx. 6618 */ 6619 if ((dev = l2arc_dev_get_next()) == NULL) 6620 continue; 6621 6622 spa = dev->l2ad_spa; 6623 ASSERT(spa != NULL); 6624 6625 /* 6626 * If the pool is read-only then force the feed thread to 6627 * sleep a little longer. 6628 */ 6629 if (!spa_writeable(spa)) { 6630 next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz; 6631 spa_config_exit(spa, SCL_L2ARC, dev); 6632 continue; 6633 } 6634 6635 /* 6636 * Avoid contributing to memory pressure. 6637 */ 6638 if (arc_reclaim_needed()) { 6639 ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 6640 spa_config_exit(spa, SCL_L2ARC, dev); 6641 continue; 6642 } 6643 6644 ARCSTAT_BUMP(arcstat_l2_feeds); 6645 6646 size = l2arc_write_size(); 6647 6648 /* 6649 * Evict L2ARC buffers that will be overwritten. 6650 */ 6651 l2arc_evict(dev, size, B_FALSE); 6652 6653 /* 6654 * Write ARC buffers. 6655 */ 6656 wrote = l2arc_write_buffers(spa, dev, size, &headroom_boost); 6657 6658 /* 6659 * Calculate interval between writes. 6660 */ 6661 next = l2arc_write_interval(begin, size, wrote); 6662 spa_config_exit(spa, SCL_L2ARC, dev); 6663 } 6664 6665 l2arc_thread_exit = 0; 6666 cv_broadcast(&l2arc_feed_thr_cv); 6667 CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 6668 thread_exit(); 6669} 6670 6671boolean_t 6672l2arc_vdev_present(vdev_t *vd) 6673{ 6674 l2arc_dev_t *dev; 6675 6676 mutex_enter(&l2arc_dev_mtx); 6677 for (dev = list_head(l2arc_dev_list); dev != NULL; 6678 dev = list_next(l2arc_dev_list, dev)) { 6679 if (dev->l2ad_vdev == vd) 6680 break; 6681 } 6682 mutex_exit(&l2arc_dev_mtx); 6683 6684 return (dev != NULL); 6685} 6686 6687/* 6688 * Add a vdev for use by the L2ARC. By this point the spa has already 6689 * validated the vdev and opened it. 6690 */ 6691void 6692l2arc_add_vdev(spa_t *spa, vdev_t *vd) 6693{ 6694 l2arc_dev_t *adddev; 6695 6696 ASSERT(!l2arc_vdev_present(vd)); 6697 6698 vdev_ashift_optimize(vd); 6699 6700 /* 6701 * Create a new l2arc device entry. 6702 */ 6703 adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 6704 adddev->l2ad_spa = spa; 6705 adddev->l2ad_vdev = vd; 6706 adddev->l2ad_start = VDEV_LABEL_START_SIZE; 6707 adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 6708 adddev->l2ad_hand = adddev->l2ad_start; 6709 adddev->l2ad_first = B_TRUE; 6710 adddev->l2ad_writing = B_FALSE; 6711 6712 mutex_init(&adddev->l2ad_mtx, NULL, MUTEX_DEFAULT, NULL); 6713 /* 6714 * This is a list of all ARC buffers that are still valid on the 6715 * device. 6716 */ 6717 list_create(&adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 6718 offsetof(arc_buf_hdr_t, b_l2hdr.b_l2node)); 6719 6720 vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); 6721 refcount_create(&adddev->l2ad_alloc); 6722 6723 /* 6724 * Add device to global list 6725 */ 6726 mutex_enter(&l2arc_dev_mtx); 6727 list_insert_head(l2arc_dev_list, adddev); 6728 atomic_inc_64(&l2arc_ndev); 6729 mutex_exit(&l2arc_dev_mtx); 6730} 6731 6732/* 6733 * Remove a vdev from the L2ARC. 6734 */ 6735void 6736l2arc_remove_vdev(vdev_t *vd) 6737{ 6738 l2arc_dev_t *dev, *nextdev, *remdev = NULL; 6739 6740 /* 6741 * Find the device by vdev 6742 */ 6743 mutex_enter(&l2arc_dev_mtx); 6744 for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 6745 nextdev = list_next(l2arc_dev_list, dev); 6746 if (vd == dev->l2ad_vdev) { 6747 remdev = dev; 6748 break; 6749 } 6750 } 6751 ASSERT(remdev != NULL); 6752 6753 /* 6754 * Remove device from global list 6755 */ 6756 list_remove(l2arc_dev_list, remdev); 6757 l2arc_dev_last = NULL; /* may have been invalidated */ 6758 atomic_dec_64(&l2arc_ndev); 6759 mutex_exit(&l2arc_dev_mtx); 6760 6761 /* 6762 * Clear all buflists and ARC references. L2ARC device flush. 6763 */ 6764 l2arc_evict(remdev, 0, B_TRUE); 6765 list_destroy(&remdev->l2ad_buflist); 6766 mutex_destroy(&remdev->l2ad_mtx); 6767 refcount_destroy(&remdev->l2ad_alloc); 6768 kmem_free(remdev, sizeof (l2arc_dev_t)); 6769} 6770 6771void 6772l2arc_init(void) 6773{ 6774 l2arc_thread_exit = 0; 6775 l2arc_ndev = 0; 6776 l2arc_writes_sent = 0; 6777 l2arc_writes_done = 0; 6778 6779 mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 6780 cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 6781 mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 6782 mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 6783 6784 l2arc_dev_list = &L2ARC_dev_list; 6785 l2arc_free_on_write = &L2ARC_free_on_write; 6786 list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 6787 offsetof(l2arc_dev_t, l2ad_node)); 6788 list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 6789 offsetof(l2arc_data_free_t, l2df_list_node)); 6790} 6791 6792void 6793l2arc_fini(void) 6794{ 6795 /* 6796 * This is called from dmu_fini(), which is called from spa_fini(); 6797 * Because of this, we can assume that all l2arc devices have 6798 * already been removed when the pools themselves were removed. 6799 */ 6800 6801 l2arc_do_free_on_write(); 6802 6803 mutex_destroy(&l2arc_feed_thr_lock); 6804 cv_destroy(&l2arc_feed_thr_cv); 6805 mutex_destroy(&l2arc_dev_mtx); 6806 mutex_destroy(&l2arc_free_on_write_mtx); 6807 6808 list_destroy(l2arc_dev_list); 6809 list_destroy(l2arc_free_on_write); 6810} 6811 6812void 6813l2arc_start(void) 6814{ 6815 if (!(spa_mode_global & FWRITE)) 6816 return; 6817 6818 (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 6819 TS_RUN, minclsyspri); 6820} 6821 6822void 6823l2arc_stop(void) 6824{ 6825 if (!(spa_mode_global & FWRITE)) 6826 return; 6827 6828 mutex_enter(&l2arc_feed_thr_lock); 6829 cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 6830 l2arc_thread_exit = 1; 6831 while (l2arc_thread_exit != 0) 6832 cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 6833 mutex_exit(&l2arc_feed_thr_lock); 6834} 6835