arc.c revision 229663
1168404Spjd/* 2168404Spjd * CDDL HEADER START 3168404Spjd * 4168404Spjd * The contents of this file are subject to the terms of the 5168404Spjd * Common Development and Distribution License (the "License"). 6168404Spjd * You may not use this file except in compliance with the License. 7168404Spjd * 8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9168404Spjd * or http://www.opensolaris.org/os/licensing. 10168404Spjd * See the License for the specific language governing permissions 11168404Spjd * and limitations under the License. 12168404Spjd * 13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each 14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15168404Spjd * If applicable, add the following below this CDDL HEADER, with the 16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying 17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner] 18168404Spjd * 19168404Spjd * CDDL HEADER END 20168404Spjd */ 21168404Spjd/* 22219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23228103Smm * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 24228103Smm * Copyright (c) 2011 by Delphix. All rights reserved. 25168404Spjd */ 26168404Spjd 27168404Spjd/* 28168404Spjd * DVA-based Adjustable Replacement Cache 29168404Spjd * 30168404Spjd * While much of the theory of operation used here is 31168404Spjd * based on the self-tuning, low overhead replacement cache 32168404Spjd * presented by Megiddo and Modha at FAST 2003, there are some 33168404Spjd * significant differences: 34168404Spjd * 35168404Spjd * 1. The Megiddo and Modha model assumes any page is evictable. 36168404Spjd * Pages in its cache cannot be "locked" into memory. This makes 37168404Spjd * the eviction algorithm simple: evict the last page in the list. 38168404Spjd * This also make the performance characteristics easy to reason 39168404Spjd * about. Our cache is not so simple. At any given moment, some 40168404Spjd * subset of the blocks in the cache are un-evictable because we 41168404Spjd * have handed out a reference to them. Blocks are only evictable 42168404Spjd * when there are no external references active. This makes 43168404Spjd * eviction far more problematic: we choose to evict the evictable 44168404Spjd * blocks that are the "lowest" in the list. 45168404Spjd * 46168404Spjd * There are times when it is not possible to evict the requested 47168404Spjd * space. In these circumstances we are unable to adjust the cache 48168404Spjd * size. To prevent the cache growing unbounded at these times we 49185029Spjd * implement a "cache throttle" that slows the flow of new data 50185029Spjd * into the cache until we can make space available. 51168404Spjd * 52168404Spjd * 2. The Megiddo and Modha model assumes a fixed cache size. 53168404Spjd * Pages are evicted when the cache is full and there is a cache 54168404Spjd * miss. Our model has a variable sized cache. It grows with 55185029Spjd * high use, but also tries to react to memory pressure from the 56168404Spjd * operating system: decreasing its size when system memory is 57168404Spjd * tight. 58168404Spjd * 59168404Spjd * 3. The Megiddo and Modha model assumes a fixed page size. All 60168404Spjd * elements of the cache are therefor exactly the same size. So 61168404Spjd * when adjusting the cache size following a cache miss, its simply 62168404Spjd * a matter of choosing a single page to evict. In our model, we 63168404Spjd * have variable sized cache blocks (rangeing from 512 bytes to 64168404Spjd * 128K bytes). We therefor choose a set of blocks to evict to make 65168404Spjd * space for a cache miss that approximates as closely as possible 66168404Spjd * the space used by the new block. 67168404Spjd * 68168404Spjd * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache" 69168404Spjd * by N. Megiddo & D. Modha, FAST 2003 70168404Spjd */ 71168404Spjd 72168404Spjd/* 73168404Spjd * The locking model: 74168404Spjd * 75168404Spjd * A new reference to a cache buffer can be obtained in two 76168404Spjd * ways: 1) via a hash table lookup using the DVA as a key, 77185029Spjd * or 2) via one of the ARC lists. The arc_read() interface 78168404Spjd * uses method 1, while the internal arc algorithms for 79168404Spjd * adjusting the cache use method 2. We therefor provide two 80168404Spjd * types of locks: 1) the hash table lock array, and 2) the 81168404Spjd * arc list locks. 82168404Spjd * 83168404Spjd * Buffers do not have their own mutexs, rather they rely on the 84168404Spjd * hash table mutexs for the bulk of their protection (i.e. most 85168404Spjd * fields in the arc_buf_hdr_t are protected by these mutexs). 86168404Spjd * 87168404Spjd * buf_hash_find() returns the appropriate mutex (held) when it 88168404Spjd * locates the requested buffer in the hash table. It returns 89168404Spjd * NULL for the mutex if the buffer was not in the table. 90168404Spjd * 91168404Spjd * buf_hash_remove() expects the appropriate hash mutex to be 92168404Spjd * already held before it is invoked. 93168404Spjd * 94168404Spjd * Each arc state also has a mutex which is used to protect the 95168404Spjd * buffer list associated with the state. When attempting to 96168404Spjd * obtain a hash table lock while holding an arc list lock you 97168404Spjd * must use: mutex_tryenter() to avoid deadlock. Also note that 98168404Spjd * the active state mutex must be held before the ghost state mutex. 99168404Spjd * 100168404Spjd * Arc buffers may have an associated eviction callback function. 101168404Spjd * This function will be invoked prior to removing the buffer (e.g. 102168404Spjd * in arc_do_user_evicts()). Note however that the data associated 103168404Spjd * with the buffer may be evicted prior to the callback. The callback 104168404Spjd * must be made with *no locks held* (to prevent deadlock). Additionally, 105168404Spjd * the users of callbacks must ensure that their private data is 106168404Spjd * protected from simultaneous callbacks from arc_buf_evict() 107168404Spjd * and arc_do_user_evicts(). 108168404Spjd * 109168404Spjd * Note that the majority of the performance stats are manipulated 110168404Spjd * with atomic operations. 111185029Spjd * 112185029Spjd * The L2ARC uses the l2arc_buflist_mtx global mutex for the following: 113185029Spjd * 114185029Spjd * - L2ARC buflist creation 115185029Spjd * - L2ARC buflist eviction 116185029Spjd * - L2ARC write completion, which walks L2ARC buflists 117185029Spjd * - ARC header destruction, as it removes from L2ARC buflists 118185029Spjd * - ARC header release, as it removes from L2ARC buflists 119168404Spjd */ 120168404Spjd 121168404Spjd#include <sys/spa.h> 122168404Spjd#include <sys/zio.h> 123168404Spjd#include <sys/zfs_context.h> 124168404Spjd#include <sys/arc.h> 125168404Spjd#include <sys/refcount.h> 126185029Spjd#include <sys/vdev.h> 127219089Spjd#include <sys/vdev_impl.h> 128168404Spjd#ifdef _KERNEL 129168404Spjd#include <sys/dnlc.h> 130168404Spjd#endif 131168404Spjd#include <sys/callb.h> 132168404Spjd#include <sys/kstat.h> 133219089Spjd#include <zfs_fletcher.h> 134168404Spjd#include <sys/sdt.h> 135168404Spjd 136191902Skmacy#include <vm/vm_pageout.h> 137191902Skmacy 138168404Spjdstatic kmutex_t arc_reclaim_thr_lock; 139168404Spjdstatic kcondvar_t arc_reclaim_thr_cv; /* used to signal reclaim thr */ 140168404Spjdstatic uint8_t arc_thread_exit; 141168404Spjd 142185029Spjdextern int zfs_write_limit_shift; 143185029Spjdextern uint64_t zfs_write_limit_max; 144185029Spjdextern kmutex_t zfs_write_limit_lock; 145185029Spjd 146168404Spjd#define ARC_REDUCE_DNLC_PERCENT 3 147168404Spjduint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT; 148168404Spjd 149168404Spjdtypedef enum arc_reclaim_strategy { 150168404Spjd ARC_RECLAIM_AGGR, /* Aggressive reclaim strategy */ 151168404Spjd ARC_RECLAIM_CONS /* Conservative reclaim strategy */ 152168404Spjd} arc_reclaim_strategy_t; 153168404Spjd 154168404Spjd/* number of seconds before growing cache again */ 155168404Spjdstatic int arc_grow_retry = 60; 156168404Spjd 157208373Smm/* shift of arc_c for calculating both min and max arc_p */ 158208373Smmstatic int arc_p_min_shift = 4; 159208373Smm 160208373Smm/* log2(fraction of arc to reclaim) */ 161208373Smmstatic int arc_shrink_shift = 5; 162208373Smm 163168404Spjd/* 164168404Spjd * minimum lifespan of a prefetch block in clock ticks 165168404Spjd * (initialized in arc_init()) 166168404Spjd */ 167168404Spjdstatic int arc_min_prefetch_lifespan; 168168404Spjd 169208373Smmstatic int arc_dead; 170194043Skmacyextern int zfs_prefetch_disable; 171168404Spjd 172168404Spjd/* 173185029Spjd * The arc has filled available memory and has now warmed up. 174185029Spjd */ 175185029Spjdstatic boolean_t arc_warm; 176185029Spjd 177185029Spjd/* 178168404Spjd * These tunables are for performance analysis. 179168404Spjd */ 180185029Spjduint64_t zfs_arc_max; 181185029Spjduint64_t zfs_arc_min; 182185029Spjduint64_t zfs_arc_meta_limit = 0; 183208373Smmint zfs_arc_grow_retry = 0; 184208373Smmint zfs_arc_shrink_shift = 0; 185208373Smmint zfs_arc_p_min_shift = 0; 186185029Spjd 187185029SpjdTUNABLE_QUAD("vfs.zfs.arc_max", &zfs_arc_max); 188185029SpjdTUNABLE_QUAD("vfs.zfs.arc_min", &zfs_arc_min); 189185029SpjdTUNABLE_QUAD("vfs.zfs.arc_meta_limit", &zfs_arc_meta_limit); 190168473SpjdSYSCTL_DECL(_vfs_zfs); 191217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_max, CTLFLAG_RDTUN, &zfs_arc_max, 0, 192168473Spjd "Maximum ARC size"); 193217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_min, CTLFLAG_RDTUN, &zfs_arc_min, 0, 194168473Spjd "Minimum ARC size"); 195168404Spjd 196168404Spjd/* 197185029Spjd * Note that buffers can be in one of 6 states: 198168404Spjd * ARC_anon - anonymous (discussed below) 199168404Spjd * ARC_mru - recently used, currently cached 200168404Spjd * ARC_mru_ghost - recentely used, no longer in cache 201168404Spjd * ARC_mfu - frequently used, currently cached 202168404Spjd * ARC_mfu_ghost - frequently used, no longer in cache 203185029Spjd * ARC_l2c_only - exists in L2ARC but not other states 204185029Spjd * When there are no active references to the buffer, they are 205185029Spjd * are linked onto a list in one of these arc states. These are 206185029Spjd * the only buffers that can be evicted or deleted. Within each 207185029Spjd * state there are multiple lists, one for meta-data and one for 208185029Spjd * non-meta-data. Meta-data (indirect blocks, blocks of dnodes, 209185029Spjd * etc.) is tracked separately so that it can be managed more 210185029Spjd * explicitly: favored over data, limited explicitly. 211168404Spjd * 212168404Spjd * Anonymous buffers are buffers that are not associated with 213168404Spjd * a DVA. These are buffers that hold dirty block copies 214168404Spjd * before they are written to stable storage. By definition, 215168404Spjd * they are "ref'd" and are considered part of arc_mru 216168404Spjd * that cannot be freed. Generally, they will aquire a DVA 217168404Spjd * as they are written and migrate onto the arc_mru list. 218185029Spjd * 219185029Spjd * The ARC_l2c_only state is for buffers that are in the second 220185029Spjd * level ARC but no longer in any of the ARC_m* lists. The second 221185029Spjd * level ARC itself may also contain buffers that are in any of 222185029Spjd * the ARC_m* states - meaning that a buffer can exist in two 223185029Spjd * places. The reason for the ARC_l2c_only state is to keep the 224185029Spjd * buffer header in the hash table, so that reads that hit the 225185029Spjd * second level ARC benefit from these fast lookups. 226168404Spjd */ 227168404Spjd 228205264Skmacy#define ARCS_LOCK_PAD CACHE_LINE_SIZE 229205231Skmacystruct arcs_lock { 230205231Skmacy kmutex_t arcs_lock; 231205231Skmacy#ifdef _KERNEL 232205231Skmacy unsigned char pad[(ARCS_LOCK_PAD - sizeof (kmutex_t))]; 233205231Skmacy#endif 234205231Skmacy}; 235205231Skmacy 236205231Skmacy/* 237205231Skmacy * must be power of two for mask use to work 238205231Skmacy * 239205231Skmacy */ 240205231Skmacy#define ARC_BUFC_NUMDATALISTS 16 241205231Skmacy#define ARC_BUFC_NUMMETADATALISTS 16 242206796Spjd#define ARC_BUFC_NUMLISTS (ARC_BUFC_NUMMETADATALISTS + ARC_BUFC_NUMDATALISTS) 243205231Skmacy 244168404Spjdtypedef struct arc_state { 245185029Spjd uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */ 246185029Spjd uint64_t arcs_size; /* total amount of data in this state */ 247205231Skmacy list_t arcs_lists[ARC_BUFC_NUMLISTS]; /* list of evictable buffers */ 248205264Skmacy struct arcs_lock arcs_locks[ARC_BUFC_NUMLISTS] __aligned(CACHE_LINE_SIZE); 249168404Spjd} arc_state_t; 250168404Spjd 251206796Spjd#define ARCS_LOCK(s, i) (&((s)->arcs_locks[(i)].arcs_lock)) 252205231Skmacy 253185029Spjd/* The 6 states: */ 254168404Spjdstatic arc_state_t ARC_anon; 255168404Spjdstatic arc_state_t ARC_mru; 256168404Spjdstatic arc_state_t ARC_mru_ghost; 257168404Spjdstatic arc_state_t ARC_mfu; 258168404Spjdstatic arc_state_t ARC_mfu_ghost; 259185029Spjdstatic arc_state_t ARC_l2c_only; 260168404Spjd 261168404Spjdtypedef struct arc_stats { 262168404Spjd kstat_named_t arcstat_hits; 263168404Spjd kstat_named_t arcstat_misses; 264168404Spjd kstat_named_t arcstat_demand_data_hits; 265168404Spjd kstat_named_t arcstat_demand_data_misses; 266168404Spjd kstat_named_t arcstat_demand_metadata_hits; 267168404Spjd kstat_named_t arcstat_demand_metadata_misses; 268168404Spjd kstat_named_t arcstat_prefetch_data_hits; 269168404Spjd kstat_named_t arcstat_prefetch_data_misses; 270168404Spjd kstat_named_t arcstat_prefetch_metadata_hits; 271168404Spjd kstat_named_t arcstat_prefetch_metadata_misses; 272168404Spjd kstat_named_t arcstat_mru_hits; 273168404Spjd kstat_named_t arcstat_mru_ghost_hits; 274168404Spjd kstat_named_t arcstat_mfu_hits; 275168404Spjd kstat_named_t arcstat_mfu_ghost_hits; 276205231Skmacy kstat_named_t arcstat_allocated; 277168404Spjd kstat_named_t arcstat_deleted; 278205231Skmacy kstat_named_t arcstat_stolen; 279168404Spjd kstat_named_t arcstat_recycle_miss; 280168404Spjd kstat_named_t arcstat_mutex_miss; 281168404Spjd kstat_named_t arcstat_evict_skip; 282208373Smm kstat_named_t arcstat_evict_l2_cached; 283208373Smm kstat_named_t arcstat_evict_l2_eligible; 284208373Smm kstat_named_t arcstat_evict_l2_ineligible; 285168404Spjd kstat_named_t arcstat_hash_elements; 286168404Spjd kstat_named_t arcstat_hash_elements_max; 287168404Spjd kstat_named_t arcstat_hash_collisions; 288168404Spjd kstat_named_t arcstat_hash_chains; 289168404Spjd kstat_named_t arcstat_hash_chain_max; 290168404Spjd kstat_named_t arcstat_p; 291168404Spjd kstat_named_t arcstat_c; 292168404Spjd kstat_named_t arcstat_c_min; 293168404Spjd kstat_named_t arcstat_c_max; 294168404Spjd kstat_named_t arcstat_size; 295185029Spjd kstat_named_t arcstat_hdr_size; 296208373Smm kstat_named_t arcstat_data_size; 297208373Smm kstat_named_t arcstat_other_size; 298185029Spjd kstat_named_t arcstat_l2_hits; 299185029Spjd kstat_named_t arcstat_l2_misses; 300185029Spjd kstat_named_t arcstat_l2_feeds; 301185029Spjd kstat_named_t arcstat_l2_rw_clash; 302208373Smm kstat_named_t arcstat_l2_read_bytes; 303208373Smm kstat_named_t arcstat_l2_write_bytes; 304185029Spjd kstat_named_t arcstat_l2_writes_sent; 305185029Spjd kstat_named_t arcstat_l2_writes_done; 306185029Spjd kstat_named_t arcstat_l2_writes_error; 307185029Spjd kstat_named_t arcstat_l2_writes_hdr_miss; 308185029Spjd kstat_named_t arcstat_l2_evict_lock_retry; 309185029Spjd kstat_named_t arcstat_l2_evict_reading; 310185029Spjd kstat_named_t arcstat_l2_free_on_write; 311185029Spjd kstat_named_t arcstat_l2_abort_lowmem; 312185029Spjd kstat_named_t arcstat_l2_cksum_bad; 313185029Spjd kstat_named_t arcstat_l2_io_error; 314185029Spjd kstat_named_t arcstat_l2_size; 315185029Spjd kstat_named_t arcstat_l2_hdr_size; 316185029Spjd kstat_named_t arcstat_memory_throttle_count; 317205231Skmacy kstat_named_t arcstat_l2_write_trylock_fail; 318205231Skmacy kstat_named_t arcstat_l2_write_passed_headroom; 319205231Skmacy kstat_named_t arcstat_l2_write_spa_mismatch; 320206796Spjd kstat_named_t arcstat_l2_write_in_l2; 321205231Skmacy kstat_named_t arcstat_l2_write_hdr_io_in_progress; 322205231Skmacy kstat_named_t arcstat_l2_write_not_cacheable; 323205231Skmacy kstat_named_t arcstat_l2_write_full; 324205231Skmacy kstat_named_t arcstat_l2_write_buffer_iter; 325205231Skmacy kstat_named_t arcstat_l2_write_pios; 326205231Skmacy kstat_named_t arcstat_l2_write_buffer_bytes_scanned; 327205231Skmacy kstat_named_t arcstat_l2_write_buffer_list_iter; 328205231Skmacy kstat_named_t arcstat_l2_write_buffer_list_null_iter; 329168404Spjd} arc_stats_t; 330168404Spjd 331168404Spjdstatic arc_stats_t arc_stats = { 332168404Spjd { "hits", KSTAT_DATA_UINT64 }, 333168404Spjd { "misses", KSTAT_DATA_UINT64 }, 334168404Spjd { "demand_data_hits", KSTAT_DATA_UINT64 }, 335168404Spjd { "demand_data_misses", KSTAT_DATA_UINT64 }, 336168404Spjd { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 337168404Spjd { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 338168404Spjd { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 339168404Spjd { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 340168404Spjd { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 341168404Spjd { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 342168404Spjd { "mru_hits", KSTAT_DATA_UINT64 }, 343168404Spjd { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 344168404Spjd { "mfu_hits", KSTAT_DATA_UINT64 }, 345168404Spjd { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 346205231Skmacy { "allocated", KSTAT_DATA_UINT64 }, 347168404Spjd { "deleted", KSTAT_DATA_UINT64 }, 348205231Skmacy { "stolen", KSTAT_DATA_UINT64 }, 349168404Spjd { "recycle_miss", KSTAT_DATA_UINT64 }, 350168404Spjd { "mutex_miss", KSTAT_DATA_UINT64 }, 351168404Spjd { "evict_skip", KSTAT_DATA_UINT64 }, 352208373Smm { "evict_l2_cached", KSTAT_DATA_UINT64 }, 353208373Smm { "evict_l2_eligible", KSTAT_DATA_UINT64 }, 354208373Smm { "evict_l2_ineligible", KSTAT_DATA_UINT64 }, 355168404Spjd { "hash_elements", KSTAT_DATA_UINT64 }, 356168404Spjd { "hash_elements_max", KSTAT_DATA_UINT64 }, 357168404Spjd { "hash_collisions", KSTAT_DATA_UINT64 }, 358168404Spjd { "hash_chains", KSTAT_DATA_UINT64 }, 359168404Spjd { "hash_chain_max", KSTAT_DATA_UINT64 }, 360168404Spjd { "p", KSTAT_DATA_UINT64 }, 361168404Spjd { "c", KSTAT_DATA_UINT64 }, 362168404Spjd { "c_min", KSTAT_DATA_UINT64 }, 363168404Spjd { "c_max", KSTAT_DATA_UINT64 }, 364185029Spjd { "size", KSTAT_DATA_UINT64 }, 365185029Spjd { "hdr_size", KSTAT_DATA_UINT64 }, 366208373Smm { "data_size", KSTAT_DATA_UINT64 }, 367208373Smm { "other_size", KSTAT_DATA_UINT64 }, 368185029Spjd { "l2_hits", KSTAT_DATA_UINT64 }, 369185029Spjd { "l2_misses", KSTAT_DATA_UINT64 }, 370185029Spjd { "l2_feeds", KSTAT_DATA_UINT64 }, 371185029Spjd { "l2_rw_clash", KSTAT_DATA_UINT64 }, 372208373Smm { "l2_read_bytes", KSTAT_DATA_UINT64 }, 373208373Smm { "l2_write_bytes", KSTAT_DATA_UINT64 }, 374185029Spjd { "l2_writes_sent", KSTAT_DATA_UINT64 }, 375185029Spjd { "l2_writes_done", KSTAT_DATA_UINT64 }, 376185029Spjd { "l2_writes_error", KSTAT_DATA_UINT64 }, 377185029Spjd { "l2_writes_hdr_miss", KSTAT_DATA_UINT64 }, 378185029Spjd { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 379185029Spjd { "l2_evict_reading", KSTAT_DATA_UINT64 }, 380185029Spjd { "l2_free_on_write", KSTAT_DATA_UINT64 }, 381185029Spjd { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 382185029Spjd { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 383185029Spjd { "l2_io_error", KSTAT_DATA_UINT64 }, 384185029Spjd { "l2_size", KSTAT_DATA_UINT64 }, 385185029Spjd { "l2_hdr_size", KSTAT_DATA_UINT64 }, 386205231Skmacy { "memory_throttle_count", KSTAT_DATA_UINT64 }, 387206796Spjd { "l2_write_trylock_fail", KSTAT_DATA_UINT64 }, 388206796Spjd { "l2_write_passed_headroom", KSTAT_DATA_UINT64 }, 389206796Spjd { "l2_write_spa_mismatch", KSTAT_DATA_UINT64 }, 390206796Spjd { "l2_write_in_l2", KSTAT_DATA_UINT64 }, 391206796Spjd { "l2_write_io_in_progress", KSTAT_DATA_UINT64 }, 392206796Spjd { "l2_write_not_cacheable", KSTAT_DATA_UINT64 }, 393206796Spjd { "l2_write_full", KSTAT_DATA_UINT64 }, 394206796Spjd { "l2_write_buffer_iter", KSTAT_DATA_UINT64 }, 395206796Spjd { "l2_write_pios", KSTAT_DATA_UINT64 }, 396206796Spjd { "l2_write_buffer_bytes_scanned", KSTAT_DATA_UINT64 }, 397206796Spjd { "l2_write_buffer_list_iter", KSTAT_DATA_UINT64 }, 398206796Spjd { "l2_write_buffer_list_null_iter", KSTAT_DATA_UINT64 } 399168404Spjd}; 400168404Spjd 401168404Spjd#define ARCSTAT(stat) (arc_stats.stat.value.ui64) 402168404Spjd 403168404Spjd#define ARCSTAT_INCR(stat, val) \ 404168404Spjd atomic_add_64(&arc_stats.stat.value.ui64, (val)); 405168404Spjd 406206796Spjd#define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 407168404Spjd#define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 408168404Spjd 409168404Spjd#define ARCSTAT_MAX(stat, val) { \ 410168404Spjd uint64_t m; \ 411168404Spjd while ((val) > (m = arc_stats.stat.value.ui64) && \ 412168404Spjd (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 413168404Spjd continue; \ 414168404Spjd} 415168404Spjd 416168404Spjd#define ARCSTAT_MAXSTAT(stat) \ 417168404Spjd ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 418168404Spjd 419168404Spjd/* 420168404Spjd * We define a macro to allow ARC hits/misses to be easily broken down by 421168404Spjd * two separate conditions, giving a total of four different subtypes for 422168404Spjd * each of hits and misses (so eight statistics total). 423168404Spjd */ 424168404Spjd#define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 425168404Spjd if (cond1) { \ 426168404Spjd if (cond2) { \ 427168404Spjd ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 428168404Spjd } else { \ 429168404Spjd ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 430168404Spjd } \ 431168404Spjd } else { \ 432168404Spjd if (cond2) { \ 433168404Spjd ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 434168404Spjd } else { \ 435168404Spjd ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 436168404Spjd } \ 437168404Spjd } 438168404Spjd 439168404Spjdkstat_t *arc_ksp; 440206796Spjdstatic arc_state_t *arc_anon; 441168404Spjdstatic arc_state_t *arc_mru; 442168404Spjdstatic arc_state_t *arc_mru_ghost; 443168404Spjdstatic arc_state_t *arc_mfu; 444168404Spjdstatic arc_state_t *arc_mfu_ghost; 445185029Spjdstatic arc_state_t *arc_l2c_only; 446168404Spjd 447168404Spjd/* 448168404Spjd * There are several ARC variables that are critical to export as kstats -- 449168404Spjd * but we don't want to have to grovel around in the kstat whenever we wish to 450168404Spjd * manipulate them. For these variables, we therefore define them to be in 451168404Spjd * terms of the statistic variable. This assures that we are not introducing 452168404Spjd * the possibility of inconsistency by having shadow copies of the variables, 453168404Spjd * while still allowing the code to be readable. 454168404Spjd */ 455168404Spjd#define arc_size ARCSTAT(arcstat_size) /* actual total arc size */ 456168404Spjd#define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 457168404Spjd#define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 458168404Spjd#define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 459168404Spjd#define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 460168404Spjd 461168404Spjdstatic int arc_no_grow; /* Don't try to grow cache size */ 462168404Spjdstatic uint64_t arc_tempreserve; 463209962Smmstatic uint64_t arc_loaned_bytes; 464185029Spjdstatic uint64_t arc_meta_used; 465185029Spjdstatic uint64_t arc_meta_limit; 466185029Spjdstatic uint64_t arc_meta_max = 0; 467229663SpjdSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_meta_used, CTLFLAG_RD, &arc_meta_used, 0, 468229663Spjd "ARC metadata used"); 469229663SpjdSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_meta_limit, CTLFLAG_RW, &arc_meta_limit, 0, 470229663Spjd "ARC metadata limit"); 471168404Spjd 472185029Spjdtypedef struct l2arc_buf_hdr l2arc_buf_hdr_t; 473185029Spjd 474168404Spjdtypedef struct arc_callback arc_callback_t; 475168404Spjd 476168404Spjdstruct arc_callback { 477168404Spjd void *acb_private; 478168404Spjd arc_done_func_t *acb_done; 479168404Spjd arc_buf_t *acb_buf; 480168404Spjd zio_t *acb_zio_dummy; 481168404Spjd arc_callback_t *acb_next; 482168404Spjd}; 483168404Spjd 484168404Spjdtypedef struct arc_write_callback arc_write_callback_t; 485168404Spjd 486168404Spjdstruct arc_write_callback { 487168404Spjd void *awcb_private; 488168404Spjd arc_done_func_t *awcb_ready; 489168404Spjd arc_done_func_t *awcb_done; 490168404Spjd arc_buf_t *awcb_buf; 491168404Spjd}; 492168404Spjd 493168404Spjdstruct arc_buf_hdr { 494168404Spjd /* protected by hash lock */ 495168404Spjd dva_t b_dva; 496168404Spjd uint64_t b_birth; 497168404Spjd uint64_t b_cksum0; 498168404Spjd 499168404Spjd kmutex_t b_freeze_lock; 500168404Spjd zio_cksum_t *b_freeze_cksum; 501219089Spjd void *b_thawed; 502168404Spjd 503168404Spjd arc_buf_hdr_t *b_hash_next; 504168404Spjd arc_buf_t *b_buf; 505168404Spjd uint32_t b_flags; 506168404Spjd uint32_t b_datacnt; 507168404Spjd 508168404Spjd arc_callback_t *b_acb; 509168404Spjd kcondvar_t b_cv; 510168404Spjd 511168404Spjd /* immutable */ 512168404Spjd arc_buf_contents_t b_type; 513168404Spjd uint64_t b_size; 514209962Smm uint64_t b_spa; 515168404Spjd 516168404Spjd /* protected by arc state mutex */ 517168404Spjd arc_state_t *b_state; 518168404Spjd list_node_t b_arc_node; 519168404Spjd 520168404Spjd /* updated atomically */ 521168404Spjd clock_t b_arc_access; 522168404Spjd 523168404Spjd /* self protecting */ 524168404Spjd refcount_t b_refcnt; 525185029Spjd 526185029Spjd l2arc_buf_hdr_t *b_l2hdr; 527185029Spjd list_node_t b_l2node; 528168404Spjd}; 529168404Spjd 530168404Spjdstatic arc_buf_t *arc_eviction_list; 531168404Spjdstatic kmutex_t arc_eviction_mtx; 532168404Spjdstatic arc_buf_hdr_t arc_eviction_hdr; 533168404Spjdstatic void arc_get_data_buf(arc_buf_t *buf); 534168404Spjdstatic void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock); 535185029Spjdstatic int arc_evict_needed(arc_buf_contents_t type); 536209962Smmstatic void arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes); 537168404Spjd 538209962Smmstatic boolean_t l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab); 539208373Smm 540168404Spjd#define GHOST_STATE(state) \ 541185029Spjd ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 542185029Spjd (state) == arc_l2c_only) 543168404Spjd 544168404Spjd/* 545168404Spjd * Private ARC flags. These flags are private ARC only flags that will show up 546168404Spjd * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can 547168404Spjd * be passed in as arc_flags in things like arc_read. However, these flags 548168404Spjd * should never be passed and should only be set by ARC code. When adding new 549168404Spjd * public flags, make sure not to smash the private ones. 550168404Spjd */ 551168404Spjd 552168404Spjd#define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */ 553168404Spjd#define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */ 554168404Spjd#define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */ 555168404Spjd#define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */ 556168404Spjd#define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */ 557168404Spjd#define ARC_INDIRECT (1 << 14) /* this is an indirect block */ 558185029Spjd#define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */ 559185029Spjd#define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */ 560185029Spjd#define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */ 561185029Spjd#define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */ 562168404Spjd 563168404Spjd#define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE) 564168404Spjd#define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS) 565168404Spjd#define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR) 566208373Smm#define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_PREFETCH) 567168404Spjd#define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ) 568168404Spjd#define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE) 569185029Spjd#define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS) 570185029Spjd#define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE) 571185029Spjd#define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \ 572185029Spjd (hdr)->b_l2hdr != NULL) 573185029Spjd#define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING) 574185029Spjd#define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED) 575185029Spjd#define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD) 576168404Spjd 577168404Spjd/* 578185029Spjd * Other sizes 579185029Spjd */ 580185029Spjd 581185029Spjd#define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 582185029Spjd#define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t)) 583185029Spjd 584185029Spjd/* 585168404Spjd * Hash table routines 586168404Spjd */ 587168404Spjd 588205253Skmacy#define HT_LOCK_PAD CACHE_LINE_SIZE 589168404Spjd 590168404Spjdstruct ht_lock { 591168404Spjd kmutex_t ht_lock; 592168404Spjd#ifdef _KERNEL 593168404Spjd unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 594168404Spjd#endif 595168404Spjd}; 596168404Spjd 597168404Spjd#define BUF_LOCKS 256 598168404Spjdtypedef struct buf_hash_table { 599168404Spjd uint64_t ht_mask; 600168404Spjd arc_buf_hdr_t **ht_table; 601205264Skmacy struct ht_lock ht_locks[BUF_LOCKS] __aligned(CACHE_LINE_SIZE); 602168404Spjd} buf_hash_table_t; 603168404Spjd 604168404Spjdstatic buf_hash_table_t buf_hash_table; 605168404Spjd 606168404Spjd#define BUF_HASH_INDEX(spa, dva, birth) \ 607168404Spjd (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 608168404Spjd#define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 609168404Spjd#define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 610219089Spjd#define HDR_LOCK(hdr) \ 611219089Spjd (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth))) 612168404Spjd 613168404Spjduint64_t zfs_crc64_table[256]; 614168404Spjd 615185029Spjd/* 616185029Spjd * Level 2 ARC 617185029Spjd */ 618185029Spjd 619208373Smm#define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 620208373Smm#define L2ARC_HEADROOM 2 /* num of writes */ 621208373Smm#define L2ARC_FEED_SECS 1 /* caching interval secs */ 622208373Smm#define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ 623185029Spjd 624185029Spjd#define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 625185029Spjd#define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 626185029Spjd 627185029Spjd/* 628185029Spjd * L2ARC Performance Tunables 629185029Spjd */ 630185029Spjduint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 631185029Spjduint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */ 632185029Spjduint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 633185029Spjduint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 634208373Smmuint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval milliseconds */ 635219089Spjdboolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 636208373Smmboolean_t l2arc_feed_again = B_TRUE; /* turbo warmup */ 637208373Smmboolean_t l2arc_norw = B_TRUE; /* no reads during writes */ 638185029Spjd 639217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_max, CTLFLAG_RW, 640205231Skmacy &l2arc_write_max, 0, "max write size"); 641217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_boost, CTLFLAG_RW, 642205231Skmacy &l2arc_write_boost, 0, "extra write during warmup"); 643217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_headroom, CTLFLAG_RW, 644205231Skmacy &l2arc_headroom, 0, "number of dev writes"); 645217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_secs, CTLFLAG_RW, 646205231Skmacy &l2arc_feed_secs, 0, "interval seconds"); 647217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_min_ms, CTLFLAG_RW, 648208373Smm &l2arc_feed_min_ms, 0, "min interval milliseconds"); 649205231Skmacy 650205231SkmacySYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_noprefetch, CTLFLAG_RW, 651205231Skmacy &l2arc_noprefetch, 0, "don't cache prefetch bufs"); 652208373SmmSYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_feed_again, CTLFLAG_RW, 653208373Smm &l2arc_feed_again, 0, "turbo warmup"); 654208373SmmSYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_norw, CTLFLAG_RW, 655208373Smm &l2arc_norw, 0, "no reads during writes"); 656205231Skmacy 657217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_size, CTLFLAG_RD, 658205231Skmacy &ARC_anon.arcs_size, 0, "size of anonymous state"); 659217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_metadata_lsize, CTLFLAG_RD, 660205231Skmacy &ARC_anon.arcs_lsize[ARC_BUFC_METADATA], 0, "size of anonymous state"); 661217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_data_lsize, CTLFLAG_RD, 662205231Skmacy &ARC_anon.arcs_lsize[ARC_BUFC_DATA], 0, "size of anonymous state"); 663205231Skmacy 664217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_size, CTLFLAG_RD, 665205231Skmacy &ARC_mru.arcs_size, 0, "size of mru state"); 666217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_metadata_lsize, CTLFLAG_RD, 667205231Skmacy &ARC_mru.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mru state"); 668217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_data_lsize, CTLFLAG_RD, 669205231Skmacy &ARC_mru.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mru state"); 670205231Skmacy 671217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_size, CTLFLAG_RD, 672205231Skmacy &ARC_mru_ghost.arcs_size, 0, "size of mru ghost state"); 673217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_metadata_lsize, CTLFLAG_RD, 674205231Skmacy &ARC_mru_ghost.arcs_lsize[ARC_BUFC_METADATA], 0, 675205231Skmacy "size of metadata in mru ghost state"); 676217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_data_lsize, CTLFLAG_RD, 677205231Skmacy &ARC_mru_ghost.arcs_lsize[ARC_BUFC_DATA], 0, 678205231Skmacy "size of data in mru ghost state"); 679205231Skmacy 680217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_size, CTLFLAG_RD, 681205231Skmacy &ARC_mfu.arcs_size, 0, "size of mfu state"); 682217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_metadata_lsize, CTLFLAG_RD, 683205231Skmacy &ARC_mfu.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mfu state"); 684217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_data_lsize, CTLFLAG_RD, 685205231Skmacy &ARC_mfu.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mfu state"); 686205231Skmacy 687217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_size, CTLFLAG_RD, 688205231Skmacy &ARC_mfu_ghost.arcs_size, 0, "size of mfu ghost state"); 689217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_metadata_lsize, CTLFLAG_RD, 690205231Skmacy &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_METADATA], 0, 691205231Skmacy "size of metadata in mfu ghost state"); 692217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_data_lsize, CTLFLAG_RD, 693205231Skmacy &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_DATA], 0, 694205231Skmacy "size of data in mfu ghost state"); 695205231Skmacy 696217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2c_only_size, CTLFLAG_RD, 697205231Skmacy &ARC_l2c_only.arcs_size, 0, "size of mru state"); 698205231Skmacy 699185029Spjd/* 700185029Spjd * L2ARC Internals 701185029Spjd */ 702185029Spjdtypedef struct l2arc_dev { 703185029Spjd vdev_t *l2ad_vdev; /* vdev */ 704185029Spjd spa_t *l2ad_spa; /* spa */ 705185029Spjd uint64_t l2ad_hand; /* next write location */ 706185029Spjd uint64_t l2ad_write; /* desired write size, bytes */ 707185029Spjd uint64_t l2ad_boost; /* warmup write boost, bytes */ 708185029Spjd uint64_t l2ad_start; /* first addr on device */ 709185029Spjd uint64_t l2ad_end; /* last addr on device */ 710185029Spjd uint64_t l2ad_evict; /* last addr eviction reached */ 711185029Spjd boolean_t l2ad_first; /* first sweep through */ 712208373Smm boolean_t l2ad_writing; /* currently writing */ 713185029Spjd list_t *l2ad_buflist; /* buffer list */ 714185029Spjd list_node_t l2ad_node; /* device list node */ 715185029Spjd} l2arc_dev_t; 716185029Spjd 717185029Spjdstatic list_t L2ARC_dev_list; /* device list */ 718185029Spjdstatic list_t *l2arc_dev_list; /* device list pointer */ 719185029Spjdstatic kmutex_t l2arc_dev_mtx; /* device list mutex */ 720185029Spjdstatic l2arc_dev_t *l2arc_dev_last; /* last device used */ 721185029Spjdstatic kmutex_t l2arc_buflist_mtx; /* mutex for all buflists */ 722185029Spjdstatic list_t L2ARC_free_on_write; /* free after write buf list */ 723185029Spjdstatic list_t *l2arc_free_on_write; /* free after write list ptr */ 724185029Spjdstatic kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 725185029Spjdstatic uint64_t l2arc_ndev; /* number of devices */ 726185029Spjd 727185029Spjdtypedef struct l2arc_read_callback { 728185029Spjd arc_buf_t *l2rcb_buf; /* read buffer */ 729185029Spjd spa_t *l2rcb_spa; /* spa */ 730185029Spjd blkptr_t l2rcb_bp; /* original blkptr */ 731185029Spjd zbookmark_t l2rcb_zb; /* original bookmark */ 732185029Spjd int l2rcb_flags; /* original flags */ 733185029Spjd} l2arc_read_callback_t; 734185029Spjd 735185029Spjdtypedef struct l2arc_write_callback { 736185029Spjd l2arc_dev_t *l2wcb_dev; /* device info */ 737185029Spjd arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 738185029Spjd} l2arc_write_callback_t; 739185029Spjd 740185029Spjdstruct l2arc_buf_hdr { 741185029Spjd /* protected by arc_buf_hdr mutex */ 742185029Spjd l2arc_dev_t *b_dev; /* L2ARC device */ 743208373Smm uint64_t b_daddr; /* disk address, offset byte */ 744185029Spjd}; 745185029Spjd 746185029Spjdtypedef struct l2arc_data_free { 747185029Spjd /* protected by l2arc_free_on_write_mtx */ 748185029Spjd void *l2df_data; 749185029Spjd size_t l2df_size; 750185029Spjd void (*l2df_func)(void *, size_t); 751185029Spjd list_node_t l2df_list_node; 752185029Spjd} l2arc_data_free_t; 753185029Spjd 754185029Spjdstatic kmutex_t l2arc_feed_thr_lock; 755185029Spjdstatic kcondvar_t l2arc_feed_thr_cv; 756185029Spjdstatic uint8_t l2arc_thread_exit; 757185029Spjd 758185029Spjdstatic void l2arc_read_done(zio_t *zio); 759185029Spjdstatic void l2arc_hdr_stat_add(void); 760185029Spjdstatic void l2arc_hdr_stat_remove(void); 761185029Spjd 762168404Spjdstatic uint64_t 763209962Smmbuf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) 764168404Spjd{ 765168404Spjd uint8_t *vdva = (uint8_t *)dva; 766168404Spjd uint64_t crc = -1ULL; 767168404Spjd int i; 768168404Spjd 769168404Spjd ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 770168404Spjd 771168404Spjd for (i = 0; i < sizeof (dva_t); i++) 772168404Spjd crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 773168404Spjd 774209962Smm crc ^= (spa>>8) ^ birth; 775168404Spjd 776168404Spjd return (crc); 777168404Spjd} 778168404Spjd 779168404Spjd#define BUF_EMPTY(buf) \ 780168404Spjd ((buf)->b_dva.dva_word[0] == 0 && \ 781168404Spjd (buf)->b_dva.dva_word[1] == 0 && \ 782168404Spjd (buf)->b_birth == 0) 783168404Spjd 784168404Spjd#define BUF_EQUAL(spa, dva, birth, buf) \ 785168404Spjd ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 786168404Spjd ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 787168404Spjd ((buf)->b_birth == birth) && ((buf)->b_spa == spa) 788168404Spjd 789219089Spjdstatic void 790219089Spjdbuf_discard_identity(arc_buf_hdr_t *hdr) 791219089Spjd{ 792219089Spjd hdr->b_dva.dva_word[0] = 0; 793219089Spjd hdr->b_dva.dva_word[1] = 0; 794219089Spjd hdr->b_birth = 0; 795219089Spjd hdr->b_cksum0 = 0; 796219089Spjd} 797219089Spjd 798168404Spjdstatic arc_buf_hdr_t * 799209962Smmbuf_hash_find(uint64_t spa, const dva_t *dva, uint64_t birth, kmutex_t **lockp) 800168404Spjd{ 801168404Spjd uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 802168404Spjd kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 803168404Spjd arc_buf_hdr_t *buf; 804168404Spjd 805168404Spjd mutex_enter(hash_lock); 806168404Spjd for (buf = buf_hash_table.ht_table[idx]; buf != NULL; 807168404Spjd buf = buf->b_hash_next) { 808168404Spjd if (BUF_EQUAL(spa, dva, birth, buf)) { 809168404Spjd *lockp = hash_lock; 810168404Spjd return (buf); 811168404Spjd } 812168404Spjd } 813168404Spjd mutex_exit(hash_lock); 814168404Spjd *lockp = NULL; 815168404Spjd return (NULL); 816168404Spjd} 817168404Spjd 818168404Spjd/* 819168404Spjd * Insert an entry into the hash table. If there is already an element 820168404Spjd * equal to elem in the hash table, then the already existing element 821168404Spjd * will be returned and the new element will not be inserted. 822168404Spjd * Otherwise returns NULL. 823168404Spjd */ 824168404Spjdstatic arc_buf_hdr_t * 825168404Spjdbuf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp) 826168404Spjd{ 827168404Spjd uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 828168404Spjd kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 829168404Spjd arc_buf_hdr_t *fbuf; 830168404Spjd uint32_t i; 831168404Spjd 832168404Spjd ASSERT(!HDR_IN_HASH_TABLE(buf)); 833168404Spjd *lockp = hash_lock; 834168404Spjd mutex_enter(hash_lock); 835168404Spjd for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL; 836168404Spjd fbuf = fbuf->b_hash_next, i++) { 837168404Spjd if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf)) 838168404Spjd return (fbuf); 839168404Spjd } 840168404Spjd 841168404Spjd buf->b_hash_next = buf_hash_table.ht_table[idx]; 842168404Spjd buf_hash_table.ht_table[idx] = buf; 843168404Spjd buf->b_flags |= ARC_IN_HASH_TABLE; 844168404Spjd 845168404Spjd /* collect some hash table performance data */ 846168404Spjd if (i > 0) { 847168404Spjd ARCSTAT_BUMP(arcstat_hash_collisions); 848168404Spjd if (i == 1) 849168404Spjd ARCSTAT_BUMP(arcstat_hash_chains); 850168404Spjd 851168404Spjd ARCSTAT_MAX(arcstat_hash_chain_max, i); 852168404Spjd } 853168404Spjd 854168404Spjd ARCSTAT_BUMP(arcstat_hash_elements); 855168404Spjd ARCSTAT_MAXSTAT(arcstat_hash_elements); 856168404Spjd 857168404Spjd return (NULL); 858168404Spjd} 859168404Spjd 860168404Spjdstatic void 861168404Spjdbuf_hash_remove(arc_buf_hdr_t *buf) 862168404Spjd{ 863168404Spjd arc_buf_hdr_t *fbuf, **bufp; 864168404Spjd uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 865168404Spjd 866168404Spjd ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 867168404Spjd ASSERT(HDR_IN_HASH_TABLE(buf)); 868168404Spjd 869168404Spjd bufp = &buf_hash_table.ht_table[idx]; 870168404Spjd while ((fbuf = *bufp) != buf) { 871168404Spjd ASSERT(fbuf != NULL); 872168404Spjd bufp = &fbuf->b_hash_next; 873168404Spjd } 874168404Spjd *bufp = buf->b_hash_next; 875168404Spjd buf->b_hash_next = NULL; 876168404Spjd buf->b_flags &= ~ARC_IN_HASH_TABLE; 877168404Spjd 878168404Spjd /* collect some hash table performance data */ 879168404Spjd ARCSTAT_BUMPDOWN(arcstat_hash_elements); 880168404Spjd 881168404Spjd if (buf_hash_table.ht_table[idx] && 882168404Spjd buf_hash_table.ht_table[idx]->b_hash_next == NULL) 883168404Spjd ARCSTAT_BUMPDOWN(arcstat_hash_chains); 884168404Spjd} 885168404Spjd 886168404Spjd/* 887168404Spjd * Global data structures and functions for the buf kmem cache. 888168404Spjd */ 889168404Spjdstatic kmem_cache_t *hdr_cache; 890168404Spjdstatic kmem_cache_t *buf_cache; 891168404Spjd 892168404Spjdstatic void 893168404Spjdbuf_fini(void) 894168404Spjd{ 895168404Spjd int i; 896168404Spjd 897168404Spjd kmem_free(buf_hash_table.ht_table, 898168404Spjd (buf_hash_table.ht_mask + 1) * sizeof (void *)); 899168404Spjd for (i = 0; i < BUF_LOCKS; i++) 900168404Spjd mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 901168404Spjd kmem_cache_destroy(hdr_cache); 902168404Spjd kmem_cache_destroy(buf_cache); 903168404Spjd} 904168404Spjd 905168404Spjd/* 906168404Spjd * Constructor callback - called when the cache is empty 907168404Spjd * and a new buf is requested. 908168404Spjd */ 909168404Spjd/* ARGSUSED */ 910168404Spjdstatic int 911168404Spjdhdr_cons(void *vbuf, void *unused, int kmflag) 912168404Spjd{ 913168404Spjd arc_buf_hdr_t *buf = vbuf; 914168404Spjd 915168404Spjd bzero(buf, sizeof (arc_buf_hdr_t)); 916168404Spjd refcount_create(&buf->b_refcnt); 917168404Spjd cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL); 918185029Spjd mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 919208373Smm arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 920185029Spjd 921168404Spjd return (0); 922168404Spjd} 923168404Spjd 924185029Spjd/* ARGSUSED */ 925185029Spjdstatic int 926185029Spjdbuf_cons(void *vbuf, void *unused, int kmflag) 927185029Spjd{ 928185029Spjd arc_buf_t *buf = vbuf; 929185029Spjd 930185029Spjd bzero(buf, sizeof (arc_buf_t)); 931219089Spjd mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL); 932219089Spjd rw_init(&buf->b_data_lock, NULL, RW_DEFAULT, NULL); 933208373Smm arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); 934208373Smm 935185029Spjd return (0); 936185029Spjd} 937185029Spjd 938168404Spjd/* 939168404Spjd * Destructor callback - called when a cached buf is 940168404Spjd * no longer required. 941168404Spjd */ 942168404Spjd/* ARGSUSED */ 943168404Spjdstatic void 944168404Spjdhdr_dest(void *vbuf, void *unused) 945168404Spjd{ 946168404Spjd arc_buf_hdr_t *buf = vbuf; 947168404Spjd 948219089Spjd ASSERT(BUF_EMPTY(buf)); 949168404Spjd refcount_destroy(&buf->b_refcnt); 950168404Spjd cv_destroy(&buf->b_cv); 951185029Spjd mutex_destroy(&buf->b_freeze_lock); 952208373Smm arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 953168404Spjd} 954168404Spjd 955185029Spjd/* ARGSUSED */ 956185029Spjdstatic void 957185029Spjdbuf_dest(void *vbuf, void *unused) 958185029Spjd{ 959185029Spjd arc_buf_t *buf = vbuf; 960185029Spjd 961219089Spjd mutex_destroy(&buf->b_evict_lock); 962219089Spjd rw_destroy(&buf->b_data_lock); 963208373Smm arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); 964185029Spjd} 965185029Spjd 966168404Spjd/* 967168404Spjd * Reclaim callback -- invoked when memory is low. 968168404Spjd */ 969168404Spjd/* ARGSUSED */ 970168404Spjdstatic void 971168404Spjdhdr_recl(void *unused) 972168404Spjd{ 973168404Spjd dprintf("hdr_recl called\n"); 974168404Spjd /* 975168404Spjd * umem calls the reclaim func when we destroy the buf cache, 976168404Spjd * which is after we do arc_fini(). 977168404Spjd */ 978168404Spjd if (!arc_dead) 979168404Spjd cv_signal(&arc_reclaim_thr_cv); 980168404Spjd} 981168404Spjd 982168404Spjdstatic void 983168404Spjdbuf_init(void) 984168404Spjd{ 985168404Spjd uint64_t *ct; 986168404Spjd uint64_t hsize = 1ULL << 12; 987168404Spjd int i, j; 988168404Spjd 989168404Spjd /* 990168404Spjd * The hash table is big enough to fill all of physical memory 991168404Spjd * with an average 64K block size. The table will take up 992168404Spjd * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers). 993168404Spjd */ 994168696Spjd while (hsize * 65536 < (uint64_t)physmem * PAGESIZE) 995168404Spjd hsize <<= 1; 996168404Spjdretry: 997168404Spjd buf_hash_table.ht_mask = hsize - 1; 998168404Spjd buf_hash_table.ht_table = 999168404Spjd kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 1000168404Spjd if (buf_hash_table.ht_table == NULL) { 1001168404Spjd ASSERT(hsize > (1ULL << 8)); 1002168404Spjd hsize >>= 1; 1003168404Spjd goto retry; 1004168404Spjd } 1005168404Spjd 1006168404Spjd hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t), 1007168404Spjd 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0); 1008168404Spjd buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 1009185029Spjd 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 1010168404Spjd 1011168404Spjd for (i = 0; i < 256; i++) 1012168404Spjd for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 1013168404Spjd *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 1014168404Spjd 1015168404Spjd for (i = 0; i < BUF_LOCKS; i++) { 1016168404Spjd mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 1017168404Spjd NULL, MUTEX_DEFAULT, NULL); 1018168404Spjd } 1019168404Spjd} 1020168404Spjd 1021168404Spjd#define ARC_MINTIME (hz>>4) /* 62 ms */ 1022168404Spjd 1023168404Spjdstatic void 1024168404Spjdarc_cksum_verify(arc_buf_t *buf) 1025168404Spjd{ 1026168404Spjd zio_cksum_t zc; 1027168404Spjd 1028168404Spjd if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1029168404Spjd return; 1030168404Spjd 1031168404Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1032168404Spjd if (buf->b_hdr->b_freeze_cksum == NULL || 1033168404Spjd (buf->b_hdr->b_flags & ARC_IO_ERROR)) { 1034168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1035168404Spjd return; 1036168404Spjd } 1037168404Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 1038168404Spjd if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) 1039168404Spjd panic("buffer modified while frozen!"); 1040168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1041168404Spjd} 1042168404Spjd 1043185029Spjdstatic int 1044185029Spjdarc_cksum_equal(arc_buf_t *buf) 1045185029Spjd{ 1046185029Spjd zio_cksum_t zc; 1047185029Spjd int equal; 1048185029Spjd 1049185029Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1050185029Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 1051185029Spjd equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc); 1052185029Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1053185029Spjd 1054185029Spjd return (equal); 1055185029Spjd} 1056185029Spjd 1057168404Spjdstatic void 1058185029Spjdarc_cksum_compute(arc_buf_t *buf, boolean_t force) 1059168404Spjd{ 1060185029Spjd if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY)) 1061168404Spjd return; 1062168404Spjd 1063168404Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1064168404Spjd if (buf->b_hdr->b_freeze_cksum != NULL) { 1065168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1066168404Spjd return; 1067168404Spjd } 1068168404Spjd buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 1069168404Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, 1070168404Spjd buf->b_hdr->b_freeze_cksum); 1071168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1072168404Spjd} 1073168404Spjd 1074168404Spjdvoid 1075168404Spjdarc_buf_thaw(arc_buf_t *buf) 1076168404Spjd{ 1077185029Spjd if (zfs_flags & ZFS_DEBUG_MODIFY) { 1078185029Spjd if (buf->b_hdr->b_state != arc_anon) 1079185029Spjd panic("modifying non-anon buffer!"); 1080185029Spjd if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS) 1081185029Spjd panic("modifying buffer while i/o in progress!"); 1082185029Spjd arc_cksum_verify(buf); 1083185029Spjd } 1084168404Spjd 1085168404Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1086168404Spjd if (buf->b_hdr->b_freeze_cksum != NULL) { 1087168404Spjd kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 1088168404Spjd buf->b_hdr->b_freeze_cksum = NULL; 1089168404Spjd } 1090219089Spjd 1091219089Spjd if (zfs_flags & ZFS_DEBUG_MODIFY) { 1092219089Spjd if (buf->b_hdr->b_thawed) 1093219089Spjd kmem_free(buf->b_hdr->b_thawed, 1); 1094219089Spjd buf->b_hdr->b_thawed = kmem_alloc(1, KM_SLEEP); 1095219089Spjd } 1096219089Spjd 1097168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1098168404Spjd} 1099168404Spjd 1100168404Spjdvoid 1101168404Spjdarc_buf_freeze(arc_buf_t *buf) 1102168404Spjd{ 1103219089Spjd kmutex_t *hash_lock; 1104219089Spjd 1105168404Spjd if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1106168404Spjd return; 1107168404Spjd 1108219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 1109219089Spjd mutex_enter(hash_lock); 1110219089Spjd 1111168404Spjd ASSERT(buf->b_hdr->b_freeze_cksum != NULL || 1112168404Spjd buf->b_hdr->b_state == arc_anon); 1113185029Spjd arc_cksum_compute(buf, B_FALSE); 1114219089Spjd mutex_exit(hash_lock); 1115168404Spjd} 1116168404Spjd 1117168404Spjdstatic void 1118205231Skmacyget_buf_info(arc_buf_hdr_t *ab, arc_state_t *state, list_t **list, kmutex_t **lock) 1119205231Skmacy{ 1120205231Skmacy uint64_t buf_hashid = buf_hash(ab->b_spa, &ab->b_dva, ab->b_birth); 1121205231Skmacy 1122206796Spjd if (ab->b_type == ARC_BUFC_METADATA) 1123206796Spjd buf_hashid &= (ARC_BUFC_NUMMETADATALISTS - 1); 1124205231Skmacy else { 1125206796Spjd buf_hashid &= (ARC_BUFC_NUMDATALISTS - 1); 1126205231Skmacy buf_hashid += ARC_BUFC_NUMMETADATALISTS; 1127205231Skmacy } 1128205231Skmacy 1129205231Skmacy *list = &state->arcs_lists[buf_hashid]; 1130205231Skmacy *lock = ARCS_LOCK(state, buf_hashid); 1131205231Skmacy} 1132205231Skmacy 1133205231Skmacy 1134205231Skmacystatic void 1135168404Spjdadd_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 1136168404Spjd{ 1137168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 1138168404Spjd 1139168404Spjd if ((refcount_add(&ab->b_refcnt, tag) == 1) && 1140168404Spjd (ab->b_state != arc_anon)) { 1141206796Spjd uint64_t delta = ab->b_size * ab->b_datacnt; 1142206796Spjd uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type]; 1143205231Skmacy list_t *list; 1144205231Skmacy kmutex_t *lock; 1145168404Spjd 1146205231Skmacy get_buf_info(ab, ab->b_state, &list, &lock); 1147205231Skmacy ASSERT(!MUTEX_HELD(lock)); 1148205231Skmacy mutex_enter(lock); 1149168404Spjd ASSERT(list_link_active(&ab->b_arc_node)); 1150185029Spjd list_remove(list, ab); 1151168404Spjd if (GHOST_STATE(ab->b_state)) { 1152168404Spjd ASSERT3U(ab->b_datacnt, ==, 0); 1153168404Spjd ASSERT3P(ab->b_buf, ==, NULL); 1154168404Spjd delta = ab->b_size; 1155168404Spjd } 1156168404Spjd ASSERT(delta > 0); 1157185029Spjd ASSERT3U(*size, >=, delta); 1158185029Spjd atomic_add_64(size, -delta); 1159206794Spjd mutex_exit(lock); 1160185029Spjd /* remove the prefetch flag if we get a reference */ 1161168404Spjd if (ab->b_flags & ARC_PREFETCH) 1162168404Spjd ab->b_flags &= ~ARC_PREFETCH; 1163168404Spjd } 1164168404Spjd} 1165168404Spjd 1166168404Spjdstatic int 1167168404Spjdremove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 1168168404Spjd{ 1169168404Spjd int cnt; 1170168404Spjd arc_state_t *state = ab->b_state; 1171168404Spjd 1172168404Spjd ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 1173168404Spjd ASSERT(!GHOST_STATE(state)); 1174168404Spjd 1175168404Spjd if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) && 1176168404Spjd (state != arc_anon)) { 1177185029Spjd uint64_t *size = &state->arcs_lsize[ab->b_type]; 1178205231Skmacy list_t *list; 1179205231Skmacy kmutex_t *lock; 1180185029Spjd 1181205231Skmacy get_buf_info(ab, state, &list, &lock); 1182205231Skmacy ASSERT(!MUTEX_HELD(lock)); 1183205231Skmacy mutex_enter(lock); 1184168404Spjd ASSERT(!list_link_active(&ab->b_arc_node)); 1185205231Skmacy list_insert_head(list, ab); 1186168404Spjd ASSERT(ab->b_datacnt > 0); 1187185029Spjd atomic_add_64(size, ab->b_size * ab->b_datacnt); 1188206794Spjd mutex_exit(lock); 1189168404Spjd } 1190168404Spjd return (cnt); 1191168404Spjd} 1192168404Spjd 1193168404Spjd/* 1194168404Spjd * Move the supplied buffer to the indicated state. The mutex 1195168404Spjd * for the buffer must be held by the caller. 1196168404Spjd */ 1197168404Spjdstatic void 1198168404Spjdarc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock) 1199168404Spjd{ 1200168404Spjd arc_state_t *old_state = ab->b_state; 1201168404Spjd int64_t refcnt = refcount_count(&ab->b_refcnt); 1202168404Spjd uint64_t from_delta, to_delta; 1203205231Skmacy list_t *list; 1204205231Skmacy kmutex_t *lock; 1205168404Spjd 1206168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 1207168404Spjd ASSERT(new_state != old_state); 1208168404Spjd ASSERT(refcnt == 0 || ab->b_datacnt > 0); 1209168404Spjd ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state)); 1210219089Spjd ASSERT(ab->b_datacnt <= 1 || old_state != arc_anon); 1211168404Spjd 1212168404Spjd from_delta = to_delta = ab->b_datacnt * ab->b_size; 1213168404Spjd 1214168404Spjd /* 1215168404Spjd * If this buffer is evictable, transfer it from the 1216168404Spjd * old state list to the new state list. 1217168404Spjd */ 1218168404Spjd if (refcnt == 0) { 1219168404Spjd if (old_state != arc_anon) { 1220205231Skmacy int use_mutex; 1221185029Spjd uint64_t *size = &old_state->arcs_lsize[ab->b_type]; 1222168404Spjd 1223205231Skmacy get_buf_info(ab, old_state, &list, &lock); 1224205231Skmacy use_mutex = !MUTEX_HELD(lock); 1225168404Spjd if (use_mutex) 1226205231Skmacy mutex_enter(lock); 1227168404Spjd 1228168404Spjd ASSERT(list_link_active(&ab->b_arc_node)); 1229205231Skmacy list_remove(list, ab); 1230168404Spjd 1231168404Spjd /* 1232168404Spjd * If prefetching out of the ghost cache, 1233219089Spjd * we will have a non-zero datacnt. 1234168404Spjd */ 1235168404Spjd if (GHOST_STATE(old_state) && ab->b_datacnt == 0) { 1236168404Spjd /* ghost elements have a ghost size */ 1237168404Spjd ASSERT(ab->b_buf == NULL); 1238168404Spjd from_delta = ab->b_size; 1239168404Spjd } 1240185029Spjd ASSERT3U(*size, >=, from_delta); 1241185029Spjd atomic_add_64(size, -from_delta); 1242168404Spjd 1243168404Spjd if (use_mutex) 1244205231Skmacy mutex_exit(lock); 1245168404Spjd } 1246168404Spjd if (new_state != arc_anon) { 1247206796Spjd int use_mutex; 1248185029Spjd uint64_t *size = &new_state->arcs_lsize[ab->b_type]; 1249168404Spjd 1250205231Skmacy get_buf_info(ab, new_state, &list, &lock); 1251205231Skmacy use_mutex = !MUTEX_HELD(lock); 1252168404Spjd if (use_mutex) 1253205231Skmacy mutex_enter(lock); 1254168404Spjd 1255205231Skmacy list_insert_head(list, ab); 1256168404Spjd 1257168404Spjd /* ghost elements have a ghost size */ 1258168404Spjd if (GHOST_STATE(new_state)) { 1259168404Spjd ASSERT(ab->b_datacnt == 0); 1260168404Spjd ASSERT(ab->b_buf == NULL); 1261168404Spjd to_delta = ab->b_size; 1262168404Spjd } 1263185029Spjd atomic_add_64(size, to_delta); 1264168404Spjd 1265168404Spjd if (use_mutex) 1266205231Skmacy mutex_exit(lock); 1267168404Spjd } 1268168404Spjd } 1269168404Spjd 1270168404Spjd ASSERT(!BUF_EMPTY(ab)); 1271219089Spjd if (new_state == arc_anon && HDR_IN_HASH_TABLE(ab)) 1272168404Spjd buf_hash_remove(ab); 1273168404Spjd 1274168404Spjd /* adjust state sizes */ 1275168404Spjd if (to_delta) 1276168404Spjd atomic_add_64(&new_state->arcs_size, to_delta); 1277168404Spjd if (from_delta) { 1278168404Spjd ASSERT3U(old_state->arcs_size, >=, from_delta); 1279168404Spjd atomic_add_64(&old_state->arcs_size, -from_delta); 1280168404Spjd } 1281168404Spjd ab->b_state = new_state; 1282185029Spjd 1283185029Spjd /* adjust l2arc hdr stats */ 1284185029Spjd if (new_state == arc_l2c_only) 1285185029Spjd l2arc_hdr_stat_add(); 1286185029Spjd else if (old_state == arc_l2c_only) 1287185029Spjd l2arc_hdr_stat_remove(); 1288168404Spjd} 1289168404Spjd 1290185029Spjdvoid 1291208373Smmarc_space_consume(uint64_t space, arc_space_type_t type) 1292185029Spjd{ 1293208373Smm ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 1294208373Smm 1295208373Smm switch (type) { 1296208373Smm case ARC_SPACE_DATA: 1297208373Smm ARCSTAT_INCR(arcstat_data_size, space); 1298208373Smm break; 1299208373Smm case ARC_SPACE_OTHER: 1300208373Smm ARCSTAT_INCR(arcstat_other_size, space); 1301208373Smm break; 1302208373Smm case ARC_SPACE_HDRS: 1303208373Smm ARCSTAT_INCR(arcstat_hdr_size, space); 1304208373Smm break; 1305208373Smm case ARC_SPACE_L2HDRS: 1306208373Smm ARCSTAT_INCR(arcstat_l2_hdr_size, space); 1307208373Smm break; 1308208373Smm } 1309208373Smm 1310185029Spjd atomic_add_64(&arc_meta_used, space); 1311185029Spjd atomic_add_64(&arc_size, space); 1312185029Spjd} 1313185029Spjd 1314185029Spjdvoid 1315208373Smmarc_space_return(uint64_t space, arc_space_type_t type) 1316185029Spjd{ 1317208373Smm ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 1318208373Smm 1319208373Smm switch (type) { 1320208373Smm case ARC_SPACE_DATA: 1321208373Smm ARCSTAT_INCR(arcstat_data_size, -space); 1322208373Smm break; 1323208373Smm case ARC_SPACE_OTHER: 1324208373Smm ARCSTAT_INCR(arcstat_other_size, -space); 1325208373Smm break; 1326208373Smm case ARC_SPACE_HDRS: 1327208373Smm ARCSTAT_INCR(arcstat_hdr_size, -space); 1328208373Smm break; 1329208373Smm case ARC_SPACE_L2HDRS: 1330208373Smm ARCSTAT_INCR(arcstat_l2_hdr_size, -space); 1331208373Smm break; 1332208373Smm } 1333208373Smm 1334185029Spjd ASSERT(arc_meta_used >= space); 1335185029Spjd if (arc_meta_max < arc_meta_used) 1336185029Spjd arc_meta_max = arc_meta_used; 1337185029Spjd atomic_add_64(&arc_meta_used, -space); 1338185029Spjd ASSERT(arc_size >= space); 1339185029Spjd atomic_add_64(&arc_size, -space); 1340185029Spjd} 1341185029Spjd 1342185029Spjdvoid * 1343185029Spjdarc_data_buf_alloc(uint64_t size) 1344185029Spjd{ 1345185029Spjd if (arc_evict_needed(ARC_BUFC_DATA)) 1346185029Spjd cv_signal(&arc_reclaim_thr_cv); 1347185029Spjd atomic_add_64(&arc_size, size); 1348185029Spjd return (zio_data_buf_alloc(size)); 1349185029Spjd} 1350185029Spjd 1351185029Spjdvoid 1352185029Spjdarc_data_buf_free(void *buf, uint64_t size) 1353185029Spjd{ 1354185029Spjd zio_data_buf_free(buf, size); 1355185029Spjd ASSERT(arc_size >= size); 1356185029Spjd atomic_add_64(&arc_size, -size); 1357185029Spjd} 1358185029Spjd 1359168404Spjdarc_buf_t * 1360168404Spjdarc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type) 1361168404Spjd{ 1362168404Spjd arc_buf_hdr_t *hdr; 1363168404Spjd arc_buf_t *buf; 1364168404Spjd 1365168404Spjd ASSERT3U(size, >, 0); 1366185029Spjd hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 1367168404Spjd ASSERT(BUF_EMPTY(hdr)); 1368168404Spjd hdr->b_size = size; 1369168404Spjd hdr->b_type = type; 1370228103Smm hdr->b_spa = spa_load_guid(spa); 1371168404Spjd hdr->b_state = arc_anon; 1372168404Spjd hdr->b_arc_access = 0; 1373185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1374168404Spjd buf->b_hdr = hdr; 1375168404Spjd buf->b_data = NULL; 1376168404Spjd buf->b_efunc = NULL; 1377168404Spjd buf->b_private = NULL; 1378168404Spjd buf->b_next = NULL; 1379168404Spjd hdr->b_buf = buf; 1380168404Spjd arc_get_data_buf(buf); 1381168404Spjd hdr->b_datacnt = 1; 1382168404Spjd hdr->b_flags = 0; 1383168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1384168404Spjd (void) refcount_add(&hdr->b_refcnt, tag); 1385168404Spjd 1386168404Spjd return (buf); 1387168404Spjd} 1388168404Spjd 1389209962Smmstatic char *arc_onloan_tag = "onloan"; 1390209962Smm 1391209962Smm/* 1392209962Smm * Loan out an anonymous arc buffer. Loaned buffers are not counted as in 1393209962Smm * flight data by arc_tempreserve_space() until they are "returned". Loaned 1394209962Smm * buffers must be returned to the arc before they can be used by the DMU or 1395209962Smm * freed. 1396209962Smm */ 1397209962Smmarc_buf_t * 1398209962Smmarc_loan_buf(spa_t *spa, int size) 1399209962Smm{ 1400209962Smm arc_buf_t *buf; 1401209962Smm 1402209962Smm buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA); 1403209962Smm 1404209962Smm atomic_add_64(&arc_loaned_bytes, size); 1405209962Smm return (buf); 1406209962Smm} 1407209962Smm 1408209962Smm/* 1409209962Smm * Return a loaned arc buffer to the arc. 1410209962Smm */ 1411209962Smmvoid 1412209962Smmarc_return_buf(arc_buf_t *buf, void *tag) 1413209962Smm{ 1414209962Smm arc_buf_hdr_t *hdr = buf->b_hdr; 1415209962Smm 1416209962Smm ASSERT(buf->b_data != NULL); 1417219089Spjd (void) refcount_add(&hdr->b_refcnt, tag); 1418219089Spjd (void) refcount_remove(&hdr->b_refcnt, arc_onloan_tag); 1419209962Smm 1420209962Smm atomic_add_64(&arc_loaned_bytes, -hdr->b_size); 1421209962Smm} 1422209962Smm 1423219089Spjd/* Detach an arc_buf from a dbuf (tag) */ 1424219089Spjdvoid 1425219089Spjdarc_loan_inuse_buf(arc_buf_t *buf, void *tag) 1426219089Spjd{ 1427219089Spjd arc_buf_hdr_t *hdr; 1428219089Spjd 1429219089Spjd ASSERT(buf->b_data != NULL); 1430219089Spjd hdr = buf->b_hdr; 1431219089Spjd (void) refcount_add(&hdr->b_refcnt, arc_onloan_tag); 1432219089Spjd (void) refcount_remove(&hdr->b_refcnt, tag); 1433219089Spjd buf->b_efunc = NULL; 1434219089Spjd buf->b_private = NULL; 1435219089Spjd 1436219089Spjd atomic_add_64(&arc_loaned_bytes, hdr->b_size); 1437219089Spjd} 1438219089Spjd 1439168404Spjdstatic arc_buf_t * 1440168404Spjdarc_buf_clone(arc_buf_t *from) 1441168404Spjd{ 1442168404Spjd arc_buf_t *buf; 1443168404Spjd arc_buf_hdr_t *hdr = from->b_hdr; 1444168404Spjd uint64_t size = hdr->b_size; 1445168404Spjd 1446219089Spjd ASSERT(hdr->b_state != arc_anon); 1447219089Spjd 1448185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1449168404Spjd buf->b_hdr = hdr; 1450168404Spjd buf->b_data = NULL; 1451168404Spjd buf->b_efunc = NULL; 1452168404Spjd buf->b_private = NULL; 1453168404Spjd buf->b_next = hdr->b_buf; 1454168404Spjd hdr->b_buf = buf; 1455168404Spjd arc_get_data_buf(buf); 1456168404Spjd bcopy(from->b_data, buf->b_data, size); 1457168404Spjd hdr->b_datacnt += 1; 1458168404Spjd return (buf); 1459168404Spjd} 1460168404Spjd 1461168404Spjdvoid 1462168404Spjdarc_buf_add_ref(arc_buf_t *buf, void* tag) 1463168404Spjd{ 1464168404Spjd arc_buf_hdr_t *hdr; 1465168404Spjd kmutex_t *hash_lock; 1466168404Spjd 1467168404Spjd /* 1468185029Spjd * Check to see if this buffer is evicted. Callers 1469185029Spjd * must verify b_data != NULL to know if the add_ref 1470185029Spjd * was successful. 1471168404Spjd */ 1472219089Spjd mutex_enter(&buf->b_evict_lock); 1473185029Spjd if (buf->b_data == NULL) { 1474219089Spjd mutex_exit(&buf->b_evict_lock); 1475168404Spjd return; 1476168404Spjd } 1477219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 1478219089Spjd mutex_enter(hash_lock); 1479185029Spjd hdr = buf->b_hdr; 1480219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 1481219089Spjd mutex_exit(&buf->b_evict_lock); 1482168404Spjd 1483168404Spjd ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 1484168404Spjd add_reference(hdr, hash_lock, tag); 1485208373Smm DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 1486168404Spjd arc_access(hdr, hash_lock); 1487168404Spjd mutex_exit(hash_lock); 1488168404Spjd ARCSTAT_BUMP(arcstat_hits); 1489168404Spjd ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 1490168404Spjd demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 1491168404Spjd data, metadata, hits); 1492168404Spjd} 1493168404Spjd 1494185029Spjd/* 1495185029Spjd * Free the arc data buffer. If it is an l2arc write in progress, 1496185029Spjd * the buffer is placed on l2arc_free_on_write to be freed later. 1497185029Spjd */ 1498168404Spjdstatic void 1499185029Spjdarc_buf_data_free(arc_buf_hdr_t *hdr, void (*free_func)(void *, size_t), 1500185029Spjd void *data, size_t size) 1501185029Spjd{ 1502185029Spjd if (HDR_L2_WRITING(hdr)) { 1503185029Spjd l2arc_data_free_t *df; 1504185029Spjd df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP); 1505185029Spjd df->l2df_data = data; 1506185029Spjd df->l2df_size = size; 1507185029Spjd df->l2df_func = free_func; 1508185029Spjd mutex_enter(&l2arc_free_on_write_mtx); 1509185029Spjd list_insert_head(l2arc_free_on_write, df); 1510185029Spjd mutex_exit(&l2arc_free_on_write_mtx); 1511185029Spjd ARCSTAT_BUMP(arcstat_l2_free_on_write); 1512185029Spjd } else { 1513185029Spjd free_func(data, size); 1514185029Spjd } 1515185029Spjd} 1516185029Spjd 1517185029Spjdstatic void 1518168404Spjdarc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all) 1519168404Spjd{ 1520168404Spjd arc_buf_t **bufp; 1521168404Spjd 1522168404Spjd /* free up data associated with the buf */ 1523168404Spjd if (buf->b_data) { 1524168404Spjd arc_state_t *state = buf->b_hdr->b_state; 1525168404Spjd uint64_t size = buf->b_hdr->b_size; 1526168404Spjd arc_buf_contents_t type = buf->b_hdr->b_type; 1527168404Spjd 1528168404Spjd arc_cksum_verify(buf); 1529219089Spjd 1530168404Spjd if (!recycle) { 1531168404Spjd if (type == ARC_BUFC_METADATA) { 1532185029Spjd arc_buf_data_free(buf->b_hdr, zio_buf_free, 1533185029Spjd buf->b_data, size); 1534208373Smm arc_space_return(size, ARC_SPACE_DATA); 1535168404Spjd } else { 1536168404Spjd ASSERT(type == ARC_BUFC_DATA); 1537185029Spjd arc_buf_data_free(buf->b_hdr, 1538185029Spjd zio_data_buf_free, buf->b_data, size); 1539208373Smm ARCSTAT_INCR(arcstat_data_size, -size); 1540185029Spjd atomic_add_64(&arc_size, -size); 1541168404Spjd } 1542168404Spjd } 1543168404Spjd if (list_link_active(&buf->b_hdr->b_arc_node)) { 1544185029Spjd uint64_t *cnt = &state->arcs_lsize[type]; 1545185029Spjd 1546168404Spjd ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt)); 1547168404Spjd ASSERT(state != arc_anon); 1548185029Spjd 1549185029Spjd ASSERT3U(*cnt, >=, size); 1550185029Spjd atomic_add_64(cnt, -size); 1551168404Spjd } 1552168404Spjd ASSERT3U(state->arcs_size, >=, size); 1553168404Spjd atomic_add_64(&state->arcs_size, -size); 1554168404Spjd buf->b_data = NULL; 1555168404Spjd ASSERT(buf->b_hdr->b_datacnt > 0); 1556168404Spjd buf->b_hdr->b_datacnt -= 1; 1557168404Spjd } 1558168404Spjd 1559168404Spjd /* only remove the buf if requested */ 1560168404Spjd if (!all) 1561168404Spjd return; 1562168404Spjd 1563168404Spjd /* remove the buf from the hdr list */ 1564168404Spjd for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next) 1565168404Spjd continue; 1566168404Spjd *bufp = buf->b_next; 1567219089Spjd buf->b_next = NULL; 1568168404Spjd 1569168404Spjd ASSERT(buf->b_efunc == NULL); 1570168404Spjd 1571168404Spjd /* clean up the buf */ 1572168404Spjd buf->b_hdr = NULL; 1573168404Spjd kmem_cache_free(buf_cache, buf); 1574168404Spjd} 1575168404Spjd 1576168404Spjdstatic void 1577168404Spjdarc_hdr_destroy(arc_buf_hdr_t *hdr) 1578168404Spjd{ 1579168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1580168404Spjd ASSERT3P(hdr->b_state, ==, arc_anon); 1581168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 1582219089Spjd l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr; 1583168404Spjd 1584219089Spjd if (l2hdr != NULL) { 1585219089Spjd boolean_t buflist_held = MUTEX_HELD(&l2arc_buflist_mtx); 1586219089Spjd /* 1587219089Spjd * To prevent arc_free() and l2arc_evict() from 1588219089Spjd * attempting to free the same buffer at the same time, 1589219089Spjd * a FREE_IN_PROGRESS flag is given to arc_free() to 1590219089Spjd * give it priority. l2arc_evict() can't destroy this 1591219089Spjd * header while we are waiting on l2arc_buflist_mtx. 1592219089Spjd * 1593219089Spjd * The hdr may be removed from l2ad_buflist before we 1594219089Spjd * grab l2arc_buflist_mtx, so b_l2hdr is rechecked. 1595219089Spjd */ 1596219089Spjd if (!buflist_held) { 1597185029Spjd mutex_enter(&l2arc_buflist_mtx); 1598219089Spjd l2hdr = hdr->b_l2hdr; 1599219089Spjd } 1600219089Spjd 1601219089Spjd if (l2hdr != NULL) { 1602219089Spjd list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 1603219089Spjd ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 1604219089Spjd kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 1605219089Spjd if (hdr->b_state == arc_l2c_only) 1606219089Spjd l2arc_hdr_stat_remove(); 1607219089Spjd hdr->b_l2hdr = NULL; 1608219089Spjd } 1609219089Spjd 1610219089Spjd if (!buflist_held) 1611185029Spjd mutex_exit(&l2arc_buflist_mtx); 1612185029Spjd } 1613185029Spjd 1614168404Spjd if (!BUF_EMPTY(hdr)) { 1615168404Spjd ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1616219089Spjd buf_discard_identity(hdr); 1617168404Spjd } 1618168404Spjd while (hdr->b_buf) { 1619168404Spjd arc_buf_t *buf = hdr->b_buf; 1620168404Spjd 1621168404Spjd if (buf->b_efunc) { 1622168404Spjd mutex_enter(&arc_eviction_mtx); 1623219089Spjd mutex_enter(&buf->b_evict_lock); 1624168404Spjd ASSERT(buf->b_hdr != NULL); 1625168404Spjd arc_buf_destroy(hdr->b_buf, FALSE, FALSE); 1626168404Spjd hdr->b_buf = buf->b_next; 1627168404Spjd buf->b_hdr = &arc_eviction_hdr; 1628168404Spjd buf->b_next = arc_eviction_list; 1629168404Spjd arc_eviction_list = buf; 1630219089Spjd mutex_exit(&buf->b_evict_lock); 1631168404Spjd mutex_exit(&arc_eviction_mtx); 1632168404Spjd } else { 1633168404Spjd arc_buf_destroy(hdr->b_buf, FALSE, TRUE); 1634168404Spjd } 1635168404Spjd } 1636168404Spjd if (hdr->b_freeze_cksum != NULL) { 1637168404Spjd kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 1638168404Spjd hdr->b_freeze_cksum = NULL; 1639168404Spjd } 1640219089Spjd if (hdr->b_thawed) { 1641219089Spjd kmem_free(hdr->b_thawed, 1); 1642219089Spjd hdr->b_thawed = NULL; 1643219089Spjd } 1644168404Spjd 1645168404Spjd ASSERT(!list_link_active(&hdr->b_arc_node)); 1646168404Spjd ASSERT3P(hdr->b_hash_next, ==, NULL); 1647168404Spjd ASSERT3P(hdr->b_acb, ==, NULL); 1648168404Spjd kmem_cache_free(hdr_cache, hdr); 1649168404Spjd} 1650168404Spjd 1651168404Spjdvoid 1652168404Spjdarc_buf_free(arc_buf_t *buf, void *tag) 1653168404Spjd{ 1654168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 1655168404Spjd int hashed = hdr->b_state != arc_anon; 1656168404Spjd 1657168404Spjd ASSERT(buf->b_efunc == NULL); 1658168404Spjd ASSERT(buf->b_data != NULL); 1659168404Spjd 1660168404Spjd if (hashed) { 1661168404Spjd kmutex_t *hash_lock = HDR_LOCK(hdr); 1662168404Spjd 1663168404Spjd mutex_enter(hash_lock); 1664219089Spjd hdr = buf->b_hdr; 1665219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 1666219089Spjd 1667168404Spjd (void) remove_reference(hdr, hash_lock, tag); 1668219089Spjd if (hdr->b_datacnt > 1) { 1669168404Spjd arc_buf_destroy(buf, FALSE, TRUE); 1670219089Spjd } else { 1671219089Spjd ASSERT(buf == hdr->b_buf); 1672219089Spjd ASSERT(buf->b_efunc == NULL); 1673168404Spjd hdr->b_flags |= ARC_BUF_AVAILABLE; 1674219089Spjd } 1675168404Spjd mutex_exit(hash_lock); 1676168404Spjd } else if (HDR_IO_IN_PROGRESS(hdr)) { 1677168404Spjd int destroy_hdr; 1678168404Spjd /* 1679168404Spjd * We are in the middle of an async write. Don't destroy 1680168404Spjd * this buffer unless the write completes before we finish 1681168404Spjd * decrementing the reference count. 1682168404Spjd */ 1683168404Spjd mutex_enter(&arc_eviction_mtx); 1684168404Spjd (void) remove_reference(hdr, NULL, tag); 1685168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1686168404Spjd destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 1687168404Spjd mutex_exit(&arc_eviction_mtx); 1688168404Spjd if (destroy_hdr) 1689168404Spjd arc_hdr_destroy(hdr); 1690168404Spjd } else { 1691219089Spjd if (remove_reference(hdr, NULL, tag) > 0) 1692168404Spjd arc_buf_destroy(buf, FALSE, TRUE); 1693219089Spjd else 1694168404Spjd arc_hdr_destroy(hdr); 1695168404Spjd } 1696168404Spjd} 1697168404Spjd 1698168404Spjdint 1699168404Spjdarc_buf_remove_ref(arc_buf_t *buf, void* tag) 1700168404Spjd{ 1701168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 1702168404Spjd kmutex_t *hash_lock = HDR_LOCK(hdr); 1703168404Spjd int no_callback = (buf->b_efunc == NULL); 1704168404Spjd 1705168404Spjd if (hdr->b_state == arc_anon) { 1706219089Spjd ASSERT(hdr->b_datacnt == 1); 1707168404Spjd arc_buf_free(buf, tag); 1708168404Spjd return (no_callback); 1709168404Spjd } 1710168404Spjd 1711168404Spjd mutex_enter(hash_lock); 1712219089Spjd hdr = buf->b_hdr; 1713219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 1714168404Spjd ASSERT(hdr->b_state != arc_anon); 1715168404Spjd ASSERT(buf->b_data != NULL); 1716168404Spjd 1717168404Spjd (void) remove_reference(hdr, hash_lock, tag); 1718168404Spjd if (hdr->b_datacnt > 1) { 1719168404Spjd if (no_callback) 1720168404Spjd arc_buf_destroy(buf, FALSE, TRUE); 1721168404Spjd } else if (no_callback) { 1722168404Spjd ASSERT(hdr->b_buf == buf && buf->b_next == NULL); 1723219089Spjd ASSERT(buf->b_efunc == NULL); 1724168404Spjd hdr->b_flags |= ARC_BUF_AVAILABLE; 1725168404Spjd } 1726168404Spjd ASSERT(no_callback || hdr->b_datacnt > 1 || 1727168404Spjd refcount_is_zero(&hdr->b_refcnt)); 1728168404Spjd mutex_exit(hash_lock); 1729168404Spjd return (no_callback); 1730168404Spjd} 1731168404Spjd 1732168404Spjdint 1733168404Spjdarc_buf_size(arc_buf_t *buf) 1734168404Spjd{ 1735168404Spjd return (buf->b_hdr->b_size); 1736168404Spjd} 1737168404Spjd 1738168404Spjd/* 1739168404Spjd * Evict buffers from list until we've removed the specified number of 1740168404Spjd * bytes. Move the removed buffers to the appropriate evict state. 1741168404Spjd * If the recycle flag is set, then attempt to "recycle" a buffer: 1742168404Spjd * - look for a buffer to evict that is `bytes' long. 1743168404Spjd * - return the data block from this buffer rather than freeing it. 1744168404Spjd * This flag is used by callers that are trying to make space for a 1745168404Spjd * new buffer in a full arc cache. 1746185029Spjd * 1747185029Spjd * This function makes a "best effort". It skips over any buffers 1748185029Spjd * it can't get a hash_lock on, and so may not catch all candidates. 1749185029Spjd * It may also return without evicting as much space as requested. 1750168404Spjd */ 1751168404Spjdstatic void * 1752209962Smmarc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle, 1753168404Spjd arc_buf_contents_t type) 1754168404Spjd{ 1755168404Spjd arc_state_t *evicted_state; 1756168404Spjd uint64_t bytes_evicted = 0, skipped = 0, missed = 0; 1757205231Skmacy int64_t bytes_remaining; 1758168404Spjd arc_buf_hdr_t *ab, *ab_prev = NULL; 1759205231Skmacy list_t *evicted_list, *list, *evicted_list_start, *list_start; 1760205231Skmacy kmutex_t *lock, *evicted_lock; 1761168404Spjd kmutex_t *hash_lock; 1762168404Spjd boolean_t have_lock; 1763168404Spjd void *stolen = NULL; 1764205231Skmacy static int evict_metadata_offset, evict_data_offset; 1765205231Skmacy int i, idx, offset, list_count, count; 1766168404Spjd 1767168404Spjd ASSERT(state == arc_mru || state == arc_mfu); 1768168404Spjd 1769168404Spjd evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 1770206796Spjd 1771205231Skmacy if (type == ARC_BUFC_METADATA) { 1772205231Skmacy offset = 0; 1773205231Skmacy list_count = ARC_BUFC_NUMMETADATALISTS; 1774205231Skmacy list_start = &state->arcs_lists[0]; 1775205231Skmacy evicted_list_start = &evicted_state->arcs_lists[0]; 1776205231Skmacy idx = evict_metadata_offset; 1777205231Skmacy } else { 1778205231Skmacy offset = ARC_BUFC_NUMMETADATALISTS; 1779205231Skmacy list_start = &state->arcs_lists[offset]; 1780205231Skmacy evicted_list_start = &evicted_state->arcs_lists[offset]; 1781205231Skmacy list_count = ARC_BUFC_NUMDATALISTS; 1782205231Skmacy idx = evict_data_offset; 1783205231Skmacy } 1784205231Skmacy bytes_remaining = evicted_state->arcs_lsize[type]; 1785205231Skmacy count = 0; 1786206796Spjd 1787205231Skmacyevict_start: 1788205231Skmacy list = &list_start[idx]; 1789205231Skmacy evicted_list = &evicted_list_start[idx]; 1790205231Skmacy lock = ARCS_LOCK(state, (offset + idx)); 1791206796Spjd evicted_lock = ARCS_LOCK(evicted_state, (offset + idx)); 1792168404Spjd 1793205231Skmacy mutex_enter(lock); 1794205231Skmacy mutex_enter(evicted_lock); 1795205231Skmacy 1796185029Spjd for (ab = list_tail(list); ab; ab = ab_prev) { 1797185029Spjd ab_prev = list_prev(list, ab); 1798205231Skmacy bytes_remaining -= (ab->b_size * ab->b_datacnt); 1799168404Spjd /* prefetch buffers have a minimum lifespan */ 1800168404Spjd if (HDR_IO_IN_PROGRESS(ab) || 1801185029Spjd (spa && ab->b_spa != spa) || 1802168404Spjd (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) && 1803219089Spjd ddi_get_lbolt() - ab->b_arc_access < 1804219089Spjd arc_min_prefetch_lifespan)) { 1805168404Spjd skipped++; 1806168404Spjd continue; 1807168404Spjd } 1808168404Spjd /* "lookahead" for better eviction candidate */ 1809168404Spjd if (recycle && ab->b_size != bytes && 1810168404Spjd ab_prev && ab_prev->b_size == bytes) 1811168404Spjd continue; 1812168404Spjd hash_lock = HDR_LOCK(ab); 1813168404Spjd have_lock = MUTEX_HELD(hash_lock); 1814168404Spjd if (have_lock || mutex_tryenter(hash_lock)) { 1815168404Spjd ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0); 1816168404Spjd ASSERT(ab->b_datacnt > 0); 1817168404Spjd while (ab->b_buf) { 1818168404Spjd arc_buf_t *buf = ab->b_buf; 1819219089Spjd if (!mutex_tryenter(&buf->b_evict_lock)) { 1820185029Spjd missed += 1; 1821185029Spjd break; 1822185029Spjd } 1823168404Spjd if (buf->b_data) { 1824168404Spjd bytes_evicted += ab->b_size; 1825168404Spjd if (recycle && ab->b_type == type && 1826185029Spjd ab->b_size == bytes && 1827185029Spjd !HDR_L2_WRITING(ab)) { 1828168404Spjd stolen = buf->b_data; 1829168404Spjd recycle = FALSE; 1830168404Spjd } 1831168404Spjd } 1832168404Spjd if (buf->b_efunc) { 1833168404Spjd mutex_enter(&arc_eviction_mtx); 1834168404Spjd arc_buf_destroy(buf, 1835168404Spjd buf->b_data == stolen, FALSE); 1836168404Spjd ab->b_buf = buf->b_next; 1837168404Spjd buf->b_hdr = &arc_eviction_hdr; 1838168404Spjd buf->b_next = arc_eviction_list; 1839168404Spjd arc_eviction_list = buf; 1840168404Spjd mutex_exit(&arc_eviction_mtx); 1841219089Spjd mutex_exit(&buf->b_evict_lock); 1842168404Spjd } else { 1843219089Spjd mutex_exit(&buf->b_evict_lock); 1844168404Spjd arc_buf_destroy(buf, 1845168404Spjd buf->b_data == stolen, TRUE); 1846168404Spjd } 1847168404Spjd } 1848208373Smm 1849208373Smm if (ab->b_l2hdr) { 1850208373Smm ARCSTAT_INCR(arcstat_evict_l2_cached, 1851208373Smm ab->b_size); 1852208373Smm } else { 1853208373Smm if (l2arc_write_eligible(ab->b_spa, ab)) { 1854208373Smm ARCSTAT_INCR(arcstat_evict_l2_eligible, 1855208373Smm ab->b_size); 1856208373Smm } else { 1857208373Smm ARCSTAT_INCR( 1858208373Smm arcstat_evict_l2_ineligible, 1859208373Smm ab->b_size); 1860208373Smm } 1861208373Smm } 1862208373Smm 1863185029Spjd if (ab->b_datacnt == 0) { 1864185029Spjd arc_change_state(evicted_state, ab, hash_lock); 1865185029Spjd ASSERT(HDR_IN_HASH_TABLE(ab)); 1866185029Spjd ab->b_flags |= ARC_IN_HASH_TABLE; 1867185029Spjd ab->b_flags &= ~ARC_BUF_AVAILABLE; 1868185029Spjd DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab); 1869185029Spjd } 1870168404Spjd if (!have_lock) 1871168404Spjd mutex_exit(hash_lock); 1872168404Spjd if (bytes >= 0 && bytes_evicted >= bytes) 1873168404Spjd break; 1874205231Skmacy if (bytes_remaining > 0) { 1875205231Skmacy mutex_exit(evicted_lock); 1876205231Skmacy mutex_exit(lock); 1877206796Spjd idx = ((idx + 1) & (list_count - 1)); 1878205231Skmacy count++; 1879205231Skmacy goto evict_start; 1880205231Skmacy } 1881168404Spjd } else { 1882168404Spjd missed += 1; 1883168404Spjd } 1884168404Spjd } 1885168404Spjd 1886205231Skmacy mutex_exit(evicted_lock); 1887205231Skmacy mutex_exit(lock); 1888206796Spjd 1889206796Spjd idx = ((idx + 1) & (list_count - 1)); 1890205231Skmacy count++; 1891168404Spjd 1892205231Skmacy if (bytes_evicted < bytes) { 1893205231Skmacy if (count < list_count) 1894205231Skmacy goto evict_start; 1895205231Skmacy else 1896205231Skmacy dprintf("only evicted %lld bytes from %x", 1897205231Skmacy (longlong_t)bytes_evicted, state); 1898205231Skmacy } 1899206796Spjd if (type == ARC_BUFC_METADATA) 1900205231Skmacy evict_metadata_offset = idx; 1901205231Skmacy else 1902205231Skmacy evict_data_offset = idx; 1903206796Spjd 1904168404Spjd if (skipped) 1905168404Spjd ARCSTAT_INCR(arcstat_evict_skip, skipped); 1906168404Spjd 1907168404Spjd if (missed) 1908168404Spjd ARCSTAT_INCR(arcstat_mutex_miss, missed); 1909168404Spjd 1910185029Spjd /* 1911185029Spjd * We have just evicted some date into the ghost state, make 1912185029Spjd * sure we also adjust the ghost state size if necessary. 1913185029Spjd */ 1914185029Spjd if (arc_no_grow && 1915185029Spjd arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) { 1916185029Spjd int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size + 1917185029Spjd arc_mru_ghost->arcs_size - arc_c; 1918185029Spjd 1919185029Spjd if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) { 1920185029Spjd int64_t todelete = 1921185029Spjd MIN(arc_mru_ghost->arcs_lsize[type], mru_over); 1922209962Smm arc_evict_ghost(arc_mru_ghost, 0, todelete); 1923185029Spjd } else if (arc_mfu_ghost->arcs_lsize[type] > 0) { 1924185029Spjd int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type], 1925185029Spjd arc_mru_ghost->arcs_size + 1926185029Spjd arc_mfu_ghost->arcs_size - arc_c); 1927209962Smm arc_evict_ghost(arc_mfu_ghost, 0, todelete); 1928185029Spjd } 1929185029Spjd } 1930205231Skmacy if (stolen) 1931205231Skmacy ARCSTAT_BUMP(arcstat_stolen); 1932185029Spjd 1933168404Spjd return (stolen); 1934168404Spjd} 1935168404Spjd 1936168404Spjd/* 1937168404Spjd * Remove buffers from list until we've removed the specified number of 1938168404Spjd * bytes. Destroy the buffers that are removed. 1939168404Spjd */ 1940168404Spjdstatic void 1941209962Smmarc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes) 1942168404Spjd{ 1943168404Spjd arc_buf_hdr_t *ab, *ab_prev; 1944219089Spjd arc_buf_hdr_t marker = { 0 }; 1945205231Skmacy list_t *list, *list_start; 1946205231Skmacy kmutex_t *hash_lock, *lock; 1947168404Spjd uint64_t bytes_deleted = 0; 1948168404Spjd uint64_t bufs_skipped = 0; 1949205231Skmacy static int evict_offset; 1950205231Skmacy int list_count, idx = evict_offset; 1951205231Skmacy int offset, count = 0; 1952168404Spjd 1953168404Spjd ASSERT(GHOST_STATE(state)); 1954205231Skmacy 1955205231Skmacy /* 1956205231Skmacy * data lists come after metadata lists 1957205231Skmacy */ 1958205231Skmacy list_start = &state->arcs_lists[ARC_BUFC_NUMMETADATALISTS]; 1959205231Skmacy list_count = ARC_BUFC_NUMDATALISTS; 1960205231Skmacy offset = ARC_BUFC_NUMMETADATALISTS; 1961206796Spjd 1962205231Skmacyevict_start: 1963205231Skmacy list = &list_start[idx]; 1964205231Skmacy lock = ARCS_LOCK(state, idx + offset); 1965205231Skmacy 1966205231Skmacy mutex_enter(lock); 1967185029Spjd for (ab = list_tail(list); ab; ab = ab_prev) { 1968185029Spjd ab_prev = list_prev(list, ab); 1969185029Spjd if (spa && ab->b_spa != spa) 1970185029Spjd continue; 1971219089Spjd 1972219089Spjd /* ignore markers */ 1973219089Spjd if (ab->b_spa == 0) 1974219089Spjd continue; 1975219089Spjd 1976168404Spjd hash_lock = HDR_LOCK(ab); 1977219089Spjd /* caller may be trying to modify this buffer, skip it */ 1978219089Spjd if (MUTEX_HELD(hash_lock)) 1979219089Spjd continue; 1980168404Spjd if (mutex_tryenter(hash_lock)) { 1981168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(ab)); 1982168404Spjd ASSERT(ab->b_buf == NULL); 1983168404Spjd ARCSTAT_BUMP(arcstat_deleted); 1984168404Spjd bytes_deleted += ab->b_size; 1985185029Spjd 1986185029Spjd if (ab->b_l2hdr != NULL) { 1987185029Spjd /* 1988185029Spjd * This buffer is cached on the 2nd Level ARC; 1989185029Spjd * don't destroy the header. 1990185029Spjd */ 1991185029Spjd arc_change_state(arc_l2c_only, ab, hash_lock); 1992185029Spjd mutex_exit(hash_lock); 1993185029Spjd } else { 1994185029Spjd arc_change_state(arc_anon, ab, hash_lock); 1995185029Spjd mutex_exit(hash_lock); 1996185029Spjd arc_hdr_destroy(ab); 1997185029Spjd } 1998185029Spjd 1999168404Spjd DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab); 2000168404Spjd if (bytes >= 0 && bytes_deleted >= bytes) 2001168404Spjd break; 2002219089Spjd } else if (bytes < 0) { 2003219089Spjd /* 2004219089Spjd * Insert a list marker and then wait for the 2005219089Spjd * hash lock to become available. Once its 2006219089Spjd * available, restart from where we left off. 2007219089Spjd */ 2008219089Spjd list_insert_after(list, ab, &marker); 2009219089Spjd mutex_exit(lock); 2010219089Spjd mutex_enter(hash_lock); 2011219089Spjd mutex_exit(hash_lock); 2012219089Spjd mutex_enter(lock); 2013219089Spjd ab_prev = list_prev(list, &marker); 2014219089Spjd list_remove(list, &marker); 2015219089Spjd } else 2016168404Spjd bufs_skipped += 1; 2017168404Spjd } 2018205231Skmacy mutex_exit(lock); 2019206796Spjd idx = ((idx + 1) & (ARC_BUFC_NUMDATALISTS - 1)); 2020205231Skmacy count++; 2021206796Spjd 2022205231Skmacy if (count < list_count) 2023205231Skmacy goto evict_start; 2024206796Spjd 2025205231Skmacy evict_offset = idx; 2026205231Skmacy if ((uintptr_t)list > (uintptr_t)&state->arcs_lists[ARC_BUFC_NUMMETADATALISTS] && 2027185029Spjd (bytes < 0 || bytes_deleted < bytes)) { 2028205231Skmacy list_start = &state->arcs_lists[0]; 2029205231Skmacy list_count = ARC_BUFC_NUMMETADATALISTS; 2030205231Skmacy offset = count = 0; 2031205231Skmacy goto evict_start; 2032185029Spjd } 2033185029Spjd 2034168404Spjd if (bufs_skipped) { 2035168404Spjd ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped); 2036168404Spjd ASSERT(bytes >= 0); 2037168404Spjd } 2038168404Spjd 2039168404Spjd if (bytes_deleted < bytes) 2040168404Spjd dprintf("only deleted %lld bytes from %p", 2041168404Spjd (longlong_t)bytes_deleted, state); 2042168404Spjd} 2043168404Spjd 2044168404Spjdstatic void 2045168404Spjdarc_adjust(void) 2046168404Spjd{ 2047208373Smm int64_t adjustment, delta; 2048168404Spjd 2049208373Smm /* 2050208373Smm * Adjust MRU size 2051208373Smm */ 2052168404Spjd 2053209275Smm adjustment = MIN((int64_t)(arc_size - arc_c), 2054209275Smm (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used - 2055209275Smm arc_p)); 2056208373Smm 2057208373Smm if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) { 2058208373Smm delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment); 2059209962Smm (void) arc_evict(arc_mru, 0, delta, FALSE, ARC_BUFC_DATA); 2060208373Smm adjustment -= delta; 2061168404Spjd } 2062168404Spjd 2063208373Smm if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) { 2064208373Smm delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment); 2065209962Smm (void) arc_evict(arc_mru, 0, delta, FALSE, 2066185029Spjd ARC_BUFC_METADATA); 2067185029Spjd } 2068185029Spjd 2069208373Smm /* 2070208373Smm * Adjust MFU size 2071208373Smm */ 2072168404Spjd 2073208373Smm adjustment = arc_size - arc_c; 2074208373Smm 2075208373Smm if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) { 2076208373Smm delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]); 2077209962Smm (void) arc_evict(arc_mfu, 0, delta, FALSE, ARC_BUFC_DATA); 2078208373Smm adjustment -= delta; 2079168404Spjd } 2080168404Spjd 2081208373Smm if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) { 2082208373Smm int64_t delta = MIN(adjustment, 2083208373Smm arc_mfu->arcs_lsize[ARC_BUFC_METADATA]); 2084209962Smm (void) arc_evict(arc_mfu, 0, delta, FALSE, 2085208373Smm ARC_BUFC_METADATA); 2086208373Smm } 2087168404Spjd 2088208373Smm /* 2089208373Smm * Adjust ghost lists 2090208373Smm */ 2091168404Spjd 2092208373Smm adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c; 2093168404Spjd 2094208373Smm if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) { 2095208373Smm delta = MIN(arc_mru_ghost->arcs_size, adjustment); 2096209962Smm arc_evict_ghost(arc_mru_ghost, 0, delta); 2097208373Smm } 2098185029Spjd 2099208373Smm adjustment = 2100208373Smm arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c; 2101208373Smm 2102208373Smm if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) { 2103208373Smm delta = MIN(arc_mfu_ghost->arcs_size, adjustment); 2104209962Smm arc_evict_ghost(arc_mfu_ghost, 0, delta); 2105168404Spjd } 2106168404Spjd} 2107168404Spjd 2108168404Spjdstatic void 2109168404Spjdarc_do_user_evicts(void) 2110168404Spjd{ 2111191903Skmacy static arc_buf_t *tmp_arc_eviction_list; 2112191903Skmacy 2113191903Skmacy /* 2114191903Skmacy * Move list over to avoid LOR 2115191903Skmacy */ 2116206796Spjdrestart: 2117168404Spjd mutex_enter(&arc_eviction_mtx); 2118191903Skmacy tmp_arc_eviction_list = arc_eviction_list; 2119191903Skmacy arc_eviction_list = NULL; 2120191903Skmacy mutex_exit(&arc_eviction_mtx); 2121191903Skmacy 2122191903Skmacy while (tmp_arc_eviction_list != NULL) { 2123191903Skmacy arc_buf_t *buf = tmp_arc_eviction_list; 2124191903Skmacy tmp_arc_eviction_list = buf->b_next; 2125219089Spjd mutex_enter(&buf->b_evict_lock); 2126168404Spjd buf->b_hdr = NULL; 2127219089Spjd mutex_exit(&buf->b_evict_lock); 2128168404Spjd 2129168404Spjd if (buf->b_efunc != NULL) 2130168404Spjd VERIFY(buf->b_efunc(buf) == 0); 2131168404Spjd 2132168404Spjd buf->b_efunc = NULL; 2133168404Spjd buf->b_private = NULL; 2134168404Spjd kmem_cache_free(buf_cache, buf); 2135168404Spjd } 2136191903Skmacy 2137191903Skmacy if (arc_eviction_list != NULL) 2138191903Skmacy goto restart; 2139168404Spjd} 2140168404Spjd 2141168404Spjd/* 2142185029Spjd * Flush all *evictable* data from the cache for the given spa. 2143168404Spjd * NOTE: this will not touch "active" (i.e. referenced) data. 2144168404Spjd */ 2145168404Spjdvoid 2146185029Spjdarc_flush(spa_t *spa) 2147168404Spjd{ 2148209962Smm uint64_t guid = 0; 2149209962Smm 2150209962Smm if (spa) 2151228103Smm guid = spa_load_guid(spa); 2152209962Smm 2153205231Skmacy while (arc_mru->arcs_lsize[ARC_BUFC_DATA]) { 2154209962Smm (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA); 2155185029Spjd if (spa) 2156185029Spjd break; 2157185029Spjd } 2158205231Skmacy while (arc_mru->arcs_lsize[ARC_BUFC_METADATA]) { 2159209962Smm (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA); 2160185029Spjd if (spa) 2161185029Spjd break; 2162185029Spjd } 2163205231Skmacy while (arc_mfu->arcs_lsize[ARC_BUFC_DATA]) { 2164209962Smm (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA); 2165185029Spjd if (spa) 2166185029Spjd break; 2167185029Spjd } 2168205231Skmacy while (arc_mfu->arcs_lsize[ARC_BUFC_METADATA]) { 2169209962Smm (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA); 2170185029Spjd if (spa) 2171185029Spjd break; 2172185029Spjd } 2173168404Spjd 2174209962Smm arc_evict_ghost(arc_mru_ghost, guid, -1); 2175209962Smm arc_evict_ghost(arc_mfu_ghost, guid, -1); 2176168404Spjd 2177168404Spjd mutex_enter(&arc_reclaim_thr_lock); 2178168404Spjd arc_do_user_evicts(); 2179168404Spjd mutex_exit(&arc_reclaim_thr_lock); 2180185029Spjd ASSERT(spa || arc_eviction_list == NULL); 2181168404Spjd} 2182168404Spjd 2183168404Spjdvoid 2184168404Spjdarc_shrink(void) 2185168404Spjd{ 2186168404Spjd if (arc_c > arc_c_min) { 2187168404Spjd uint64_t to_free; 2188168404Spjd 2189168404Spjd#ifdef _KERNEL 2190168404Spjd to_free = arc_c >> arc_shrink_shift; 2191168404Spjd#else 2192168404Spjd to_free = arc_c >> arc_shrink_shift; 2193168404Spjd#endif 2194168404Spjd if (arc_c > arc_c_min + to_free) 2195168404Spjd atomic_add_64(&arc_c, -to_free); 2196168404Spjd else 2197168404Spjd arc_c = arc_c_min; 2198168404Spjd 2199168404Spjd atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 2200168404Spjd if (arc_c > arc_size) 2201168404Spjd arc_c = MAX(arc_size, arc_c_min); 2202168404Spjd if (arc_p > arc_c) 2203168404Spjd arc_p = (arc_c >> 1); 2204168404Spjd ASSERT(arc_c >= arc_c_min); 2205168404Spjd ASSERT((int64_t)arc_p >= 0); 2206168404Spjd } 2207168404Spjd 2208168404Spjd if (arc_size > arc_c) 2209168404Spjd arc_adjust(); 2210168404Spjd} 2211168404Spjd 2212185029Spjdstatic int needfree = 0; 2213168404Spjd 2214168404Spjdstatic int 2215168404Spjdarc_reclaim_needed(void) 2216168404Spjd{ 2217168404Spjd 2218168404Spjd#ifdef _KERNEL 2219219089Spjd 2220197816Skmacy if (needfree) 2221197816Skmacy return (1); 2222168404Spjd 2223191902Skmacy /* 2224212780Savg * Cooperate with pagedaemon when it's time for it to scan 2225212780Savg * and reclaim some pages. 2226191902Skmacy */ 2227212783Savg if (vm_paging_needed()) 2228191902Skmacy return (1); 2229191902Skmacy 2230219089Spjd#ifdef sun 2231168404Spjd /* 2232185029Spjd * take 'desfree' extra pages, so we reclaim sooner, rather than later 2233185029Spjd */ 2234185029Spjd extra = desfree; 2235185029Spjd 2236185029Spjd /* 2237185029Spjd * check that we're out of range of the pageout scanner. It starts to 2238185029Spjd * schedule paging if freemem is less than lotsfree and needfree. 2239185029Spjd * lotsfree is the high-water mark for pageout, and needfree is the 2240185029Spjd * number of needed free pages. We add extra pages here to make sure 2241185029Spjd * the scanner doesn't start up while we're freeing memory. 2242185029Spjd */ 2243185029Spjd if (freemem < lotsfree + needfree + extra) 2244185029Spjd return (1); 2245185029Spjd 2246185029Spjd /* 2247168404Spjd * check to make sure that swapfs has enough space so that anon 2248185029Spjd * reservations can still succeed. anon_resvmem() checks that the 2249168404Spjd * availrmem is greater than swapfs_minfree, and the number of reserved 2250168404Spjd * swap pages. We also add a bit of extra here just to prevent 2251168404Spjd * circumstances from getting really dire. 2252168404Spjd */ 2253168404Spjd if (availrmem < swapfs_minfree + swapfs_reserve + extra) 2254168404Spjd return (1); 2255168404Spjd 2256168404Spjd#if defined(__i386) 2257168404Spjd /* 2258168404Spjd * If we're on an i386 platform, it's possible that we'll exhaust the 2259168404Spjd * kernel heap space before we ever run out of available physical 2260168404Spjd * memory. Most checks of the size of the heap_area compare against 2261168404Spjd * tune.t_minarmem, which is the minimum available real memory that we 2262168404Spjd * can have in the system. However, this is generally fixed at 25 pages 2263168404Spjd * which is so low that it's useless. In this comparison, we seek to 2264168404Spjd * calculate the total heap-size, and reclaim if more than 3/4ths of the 2265185029Spjd * heap is allocated. (Or, in the calculation, if less than 1/4th is 2266168404Spjd * free) 2267168404Spjd */ 2268168404Spjd if (btop(vmem_size(heap_arena, VMEM_FREE)) < 2269168404Spjd (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2)) 2270168404Spjd return (1); 2271168404Spjd#endif 2272219089Spjd#else /* !sun */ 2273175633Spjd if (kmem_used() > (kmem_size() * 3) / 4) 2274168404Spjd return (1); 2275219089Spjd#endif /* sun */ 2276168404Spjd 2277168404Spjd#else 2278168404Spjd if (spa_get_random(100) == 0) 2279168404Spjd return (1); 2280168404Spjd#endif 2281168404Spjd return (0); 2282168404Spjd} 2283168404Spjd 2284208454Spjdextern kmem_cache_t *zio_buf_cache[]; 2285208454Spjdextern kmem_cache_t *zio_data_buf_cache[]; 2286208454Spjd 2287168404Spjdstatic void 2288168404Spjdarc_kmem_reap_now(arc_reclaim_strategy_t strat) 2289168404Spjd{ 2290168404Spjd size_t i; 2291168404Spjd kmem_cache_t *prev_cache = NULL; 2292168404Spjd kmem_cache_t *prev_data_cache = NULL; 2293168404Spjd 2294168404Spjd#ifdef _KERNEL 2295185029Spjd if (arc_meta_used >= arc_meta_limit) { 2296185029Spjd /* 2297185029Spjd * We are exceeding our meta-data cache limit. 2298185029Spjd * Purge some DNLC entries to release holds on meta-data. 2299185029Spjd */ 2300185029Spjd dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 2301185029Spjd } 2302168404Spjd#if defined(__i386) 2303168404Spjd /* 2304168404Spjd * Reclaim unused memory from all kmem caches. 2305168404Spjd */ 2306168404Spjd kmem_reap(); 2307168404Spjd#endif 2308168404Spjd#endif 2309168404Spjd 2310168404Spjd /* 2311185029Spjd * An aggressive reclamation will shrink the cache size as well as 2312168404Spjd * reap free buffers from the arc kmem caches. 2313168404Spjd */ 2314168404Spjd if (strat == ARC_RECLAIM_AGGR) 2315168404Spjd arc_shrink(); 2316168404Spjd 2317168404Spjd for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 2318168404Spjd if (zio_buf_cache[i] != prev_cache) { 2319168404Spjd prev_cache = zio_buf_cache[i]; 2320168404Spjd kmem_cache_reap_now(zio_buf_cache[i]); 2321168404Spjd } 2322168404Spjd if (zio_data_buf_cache[i] != prev_data_cache) { 2323168404Spjd prev_data_cache = zio_data_buf_cache[i]; 2324168404Spjd kmem_cache_reap_now(zio_data_buf_cache[i]); 2325168404Spjd } 2326168404Spjd } 2327168404Spjd kmem_cache_reap_now(buf_cache); 2328168404Spjd kmem_cache_reap_now(hdr_cache); 2329168404Spjd} 2330168404Spjd 2331168404Spjdstatic void 2332168404Spjdarc_reclaim_thread(void *dummy __unused) 2333168404Spjd{ 2334168404Spjd clock_t growtime = 0; 2335168404Spjd arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 2336168404Spjd callb_cpr_t cpr; 2337168404Spjd 2338168404Spjd CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 2339168404Spjd 2340168404Spjd mutex_enter(&arc_reclaim_thr_lock); 2341168404Spjd while (arc_thread_exit == 0) { 2342168404Spjd if (arc_reclaim_needed()) { 2343168404Spjd 2344168404Spjd if (arc_no_grow) { 2345168404Spjd if (last_reclaim == ARC_RECLAIM_CONS) { 2346168404Spjd last_reclaim = ARC_RECLAIM_AGGR; 2347168404Spjd } else { 2348168404Spjd last_reclaim = ARC_RECLAIM_CONS; 2349168404Spjd } 2350168404Spjd } else { 2351168404Spjd arc_no_grow = TRUE; 2352168404Spjd last_reclaim = ARC_RECLAIM_AGGR; 2353168404Spjd membar_producer(); 2354168404Spjd } 2355168404Spjd 2356168404Spjd /* reset the growth delay for every reclaim */ 2357219089Spjd growtime = ddi_get_lbolt() + (arc_grow_retry * hz); 2358168404Spjd 2359185029Spjd if (needfree && last_reclaim == ARC_RECLAIM_CONS) { 2360168404Spjd /* 2361185029Spjd * If needfree is TRUE our vm_lowmem hook 2362168404Spjd * was called and in that case we must free some 2363168404Spjd * memory, so switch to aggressive mode. 2364168404Spjd */ 2365168404Spjd arc_no_grow = TRUE; 2366168404Spjd last_reclaim = ARC_RECLAIM_AGGR; 2367168404Spjd } 2368168404Spjd arc_kmem_reap_now(last_reclaim); 2369185029Spjd arc_warm = B_TRUE; 2370185029Spjd 2371219089Spjd } else if (arc_no_grow && ddi_get_lbolt() >= growtime) { 2372168404Spjd arc_no_grow = FALSE; 2373168404Spjd } 2374168404Spjd 2375209275Smm arc_adjust(); 2376168404Spjd 2377168404Spjd if (arc_eviction_list != NULL) 2378168404Spjd arc_do_user_evicts(); 2379168404Spjd 2380211762Savg#ifdef _KERNEL 2381211762Savg if (needfree) { 2382185029Spjd needfree = 0; 2383185029Spjd wakeup(&needfree); 2384211762Savg } 2385168404Spjd#endif 2386168404Spjd 2387168404Spjd /* block until needed, or one second, whichever is shorter */ 2388168404Spjd CALLB_CPR_SAFE_BEGIN(&cpr); 2389168404Spjd (void) cv_timedwait(&arc_reclaim_thr_cv, 2390168404Spjd &arc_reclaim_thr_lock, hz); 2391168404Spjd CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 2392168404Spjd } 2393168404Spjd 2394168404Spjd arc_thread_exit = 0; 2395168404Spjd cv_broadcast(&arc_reclaim_thr_cv); 2396168404Spjd CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 2397168404Spjd thread_exit(); 2398168404Spjd} 2399168404Spjd 2400168404Spjd/* 2401168404Spjd * Adapt arc info given the number of bytes we are trying to add and 2402168404Spjd * the state that we are comming from. This function is only called 2403168404Spjd * when we are adding new content to the cache. 2404168404Spjd */ 2405168404Spjdstatic void 2406168404Spjdarc_adapt(int bytes, arc_state_t *state) 2407168404Spjd{ 2408168404Spjd int mult; 2409208373Smm uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 2410168404Spjd 2411185029Spjd if (state == arc_l2c_only) 2412185029Spjd return; 2413185029Spjd 2414168404Spjd ASSERT(bytes > 0); 2415168404Spjd /* 2416168404Spjd * Adapt the target size of the MRU list: 2417168404Spjd * - if we just hit in the MRU ghost list, then increase 2418168404Spjd * the target size of the MRU list. 2419168404Spjd * - if we just hit in the MFU ghost list, then increase 2420168404Spjd * the target size of the MFU list by decreasing the 2421168404Spjd * target size of the MRU list. 2422168404Spjd */ 2423168404Spjd if (state == arc_mru_ghost) { 2424168404Spjd mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? 2425168404Spjd 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); 2426209275Smm mult = MIN(mult, 10); /* avoid wild arc_p adjustment */ 2427168404Spjd 2428208373Smm arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 2429168404Spjd } else if (state == arc_mfu_ghost) { 2430208373Smm uint64_t delta; 2431208373Smm 2432168404Spjd mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? 2433168404Spjd 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); 2434209275Smm mult = MIN(mult, 10); 2435168404Spjd 2436208373Smm delta = MIN(bytes * mult, arc_p); 2437208373Smm arc_p = MAX(arc_p_min, arc_p - delta); 2438168404Spjd } 2439168404Spjd ASSERT((int64_t)arc_p >= 0); 2440168404Spjd 2441168404Spjd if (arc_reclaim_needed()) { 2442168404Spjd cv_signal(&arc_reclaim_thr_cv); 2443168404Spjd return; 2444168404Spjd } 2445168404Spjd 2446168404Spjd if (arc_no_grow) 2447168404Spjd return; 2448168404Spjd 2449168404Spjd if (arc_c >= arc_c_max) 2450168404Spjd return; 2451168404Spjd 2452168404Spjd /* 2453168404Spjd * If we're within (2 * maxblocksize) bytes of the target 2454168404Spjd * cache size, increment the target cache size 2455168404Spjd */ 2456168404Spjd if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 2457168404Spjd atomic_add_64(&arc_c, (int64_t)bytes); 2458168404Spjd if (arc_c > arc_c_max) 2459168404Spjd arc_c = arc_c_max; 2460168404Spjd else if (state == arc_anon) 2461168404Spjd atomic_add_64(&arc_p, (int64_t)bytes); 2462168404Spjd if (arc_p > arc_c) 2463168404Spjd arc_p = arc_c; 2464168404Spjd } 2465168404Spjd ASSERT((int64_t)arc_p >= 0); 2466168404Spjd} 2467168404Spjd 2468168404Spjd/* 2469168404Spjd * Check if the cache has reached its limits and eviction is required 2470168404Spjd * prior to insert. 2471168404Spjd */ 2472168404Spjdstatic int 2473185029Spjdarc_evict_needed(arc_buf_contents_t type) 2474168404Spjd{ 2475185029Spjd if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit) 2476185029Spjd return (1); 2477185029Spjd 2478219089Spjd#ifdef sun 2479185029Spjd#ifdef _KERNEL 2480185029Spjd /* 2481185029Spjd * If zio data pages are being allocated out of a separate heap segment, 2482185029Spjd * then enforce that the size of available vmem for this area remains 2483185029Spjd * above about 1/32nd free. 2484185029Spjd */ 2485185029Spjd if (type == ARC_BUFC_DATA && zio_arena != NULL && 2486185029Spjd vmem_size(zio_arena, VMEM_FREE) < 2487185029Spjd (vmem_size(zio_arena, VMEM_ALLOC) >> 5)) 2488185029Spjd return (1); 2489185029Spjd#endif 2490219089Spjd#endif /* sun */ 2491185029Spjd 2492168404Spjd if (arc_reclaim_needed()) 2493168404Spjd return (1); 2494168404Spjd 2495168404Spjd return (arc_size > arc_c); 2496168404Spjd} 2497168404Spjd 2498168404Spjd/* 2499168404Spjd * The buffer, supplied as the first argument, needs a data block. 2500168404Spjd * So, if we are at cache max, determine which cache should be victimized. 2501168404Spjd * We have the following cases: 2502168404Spjd * 2503168404Spjd * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) -> 2504168404Spjd * In this situation if we're out of space, but the resident size of the MFU is 2505168404Spjd * under the limit, victimize the MFU cache to satisfy this insertion request. 2506168404Spjd * 2507168404Spjd * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) -> 2508168404Spjd * Here, we've used up all of the available space for the MRU, so we need to 2509168404Spjd * evict from our own cache instead. Evict from the set of resident MRU 2510168404Spjd * entries. 2511168404Spjd * 2512168404Spjd * 3. Insert for MFU (c - p) > sizeof(arc_mfu) -> 2513168404Spjd * c minus p represents the MFU space in the cache, since p is the size of the 2514168404Spjd * cache that is dedicated to the MRU. In this situation there's still space on 2515168404Spjd * the MFU side, so the MRU side needs to be victimized. 2516168404Spjd * 2517168404Spjd * 4. Insert for MFU (c - p) < sizeof(arc_mfu) -> 2518168404Spjd * MFU's resident set is consuming more space than it has been allotted. In 2519168404Spjd * this situation, we must victimize our own cache, the MFU, for this insertion. 2520168404Spjd */ 2521168404Spjdstatic void 2522168404Spjdarc_get_data_buf(arc_buf_t *buf) 2523168404Spjd{ 2524168404Spjd arc_state_t *state = buf->b_hdr->b_state; 2525168404Spjd uint64_t size = buf->b_hdr->b_size; 2526168404Spjd arc_buf_contents_t type = buf->b_hdr->b_type; 2527168404Spjd 2528168404Spjd arc_adapt(size, state); 2529168404Spjd 2530168404Spjd /* 2531168404Spjd * We have not yet reached cache maximum size, 2532168404Spjd * just allocate a new buffer. 2533168404Spjd */ 2534185029Spjd if (!arc_evict_needed(type)) { 2535168404Spjd if (type == ARC_BUFC_METADATA) { 2536168404Spjd buf->b_data = zio_buf_alloc(size); 2537208373Smm arc_space_consume(size, ARC_SPACE_DATA); 2538168404Spjd } else { 2539168404Spjd ASSERT(type == ARC_BUFC_DATA); 2540168404Spjd buf->b_data = zio_data_buf_alloc(size); 2541208373Smm ARCSTAT_INCR(arcstat_data_size, size); 2542185029Spjd atomic_add_64(&arc_size, size); 2543168404Spjd } 2544168404Spjd goto out; 2545168404Spjd } 2546168404Spjd 2547168404Spjd /* 2548168404Spjd * If we are prefetching from the mfu ghost list, this buffer 2549168404Spjd * will end up on the mru list; so steal space from there. 2550168404Spjd */ 2551168404Spjd if (state == arc_mfu_ghost) 2552168404Spjd state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu; 2553168404Spjd else if (state == arc_mru_ghost) 2554168404Spjd state = arc_mru; 2555168404Spjd 2556168404Spjd if (state == arc_mru || state == arc_anon) { 2557168404Spjd uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size; 2558208373Smm state = (arc_mfu->arcs_lsize[type] >= size && 2559185029Spjd arc_p > mru_used) ? arc_mfu : arc_mru; 2560168404Spjd } else { 2561168404Spjd /* MFU cases */ 2562168404Spjd uint64_t mfu_space = arc_c - arc_p; 2563208373Smm state = (arc_mru->arcs_lsize[type] >= size && 2564185029Spjd mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu; 2565168404Spjd } 2566209962Smm if ((buf->b_data = arc_evict(state, 0, size, TRUE, type)) == NULL) { 2567168404Spjd if (type == ARC_BUFC_METADATA) { 2568168404Spjd buf->b_data = zio_buf_alloc(size); 2569208373Smm arc_space_consume(size, ARC_SPACE_DATA); 2570168404Spjd } else { 2571168404Spjd ASSERT(type == ARC_BUFC_DATA); 2572168404Spjd buf->b_data = zio_data_buf_alloc(size); 2573208373Smm ARCSTAT_INCR(arcstat_data_size, size); 2574185029Spjd atomic_add_64(&arc_size, size); 2575168404Spjd } 2576168404Spjd ARCSTAT_BUMP(arcstat_recycle_miss); 2577168404Spjd } 2578168404Spjd ASSERT(buf->b_data != NULL); 2579168404Spjdout: 2580168404Spjd /* 2581168404Spjd * Update the state size. Note that ghost states have a 2582168404Spjd * "ghost size" and so don't need to be updated. 2583168404Spjd */ 2584168404Spjd if (!GHOST_STATE(buf->b_hdr->b_state)) { 2585168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 2586168404Spjd 2587168404Spjd atomic_add_64(&hdr->b_state->arcs_size, size); 2588168404Spjd if (list_link_active(&hdr->b_arc_node)) { 2589168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 2590185029Spjd atomic_add_64(&hdr->b_state->arcs_lsize[type], size); 2591168404Spjd } 2592168404Spjd /* 2593168404Spjd * If we are growing the cache, and we are adding anonymous 2594168404Spjd * data, and we have outgrown arc_p, update arc_p 2595168404Spjd */ 2596168404Spjd if (arc_size < arc_c && hdr->b_state == arc_anon && 2597168404Spjd arc_anon->arcs_size + arc_mru->arcs_size > arc_p) 2598168404Spjd arc_p = MIN(arc_c, arc_p + size); 2599168404Spjd } 2600205231Skmacy ARCSTAT_BUMP(arcstat_allocated); 2601168404Spjd} 2602168404Spjd 2603168404Spjd/* 2604168404Spjd * This routine is called whenever a buffer is accessed. 2605168404Spjd * NOTE: the hash lock is dropped in this function. 2606168404Spjd */ 2607168404Spjdstatic void 2608168404Spjdarc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock) 2609168404Spjd{ 2610219089Spjd clock_t now; 2611219089Spjd 2612168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 2613168404Spjd 2614168404Spjd if (buf->b_state == arc_anon) { 2615168404Spjd /* 2616168404Spjd * This buffer is not in the cache, and does not 2617168404Spjd * appear in our "ghost" list. Add the new buffer 2618168404Spjd * to the MRU state. 2619168404Spjd */ 2620168404Spjd 2621168404Spjd ASSERT(buf->b_arc_access == 0); 2622219089Spjd buf->b_arc_access = ddi_get_lbolt(); 2623168404Spjd DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2624168404Spjd arc_change_state(arc_mru, buf, hash_lock); 2625168404Spjd 2626168404Spjd } else if (buf->b_state == arc_mru) { 2627219089Spjd now = ddi_get_lbolt(); 2628219089Spjd 2629168404Spjd /* 2630168404Spjd * If this buffer is here because of a prefetch, then either: 2631168404Spjd * - clear the flag if this is a "referencing" read 2632168404Spjd * (any subsequent access will bump this into the MFU state). 2633168404Spjd * or 2634168404Spjd * - move the buffer to the head of the list if this is 2635168404Spjd * another prefetch (to make it less likely to be evicted). 2636168404Spjd */ 2637168404Spjd if ((buf->b_flags & ARC_PREFETCH) != 0) { 2638168404Spjd if (refcount_count(&buf->b_refcnt) == 0) { 2639168404Spjd ASSERT(list_link_active(&buf->b_arc_node)); 2640168404Spjd } else { 2641168404Spjd buf->b_flags &= ~ARC_PREFETCH; 2642168404Spjd ARCSTAT_BUMP(arcstat_mru_hits); 2643168404Spjd } 2644219089Spjd buf->b_arc_access = now; 2645168404Spjd return; 2646168404Spjd } 2647168404Spjd 2648168404Spjd /* 2649168404Spjd * This buffer has been "accessed" only once so far, 2650168404Spjd * but it is still in the cache. Move it to the MFU 2651168404Spjd * state. 2652168404Spjd */ 2653219089Spjd if (now > buf->b_arc_access + ARC_MINTIME) { 2654168404Spjd /* 2655168404Spjd * More than 125ms have passed since we 2656168404Spjd * instantiated this buffer. Move it to the 2657168404Spjd * most frequently used state. 2658168404Spjd */ 2659219089Spjd buf->b_arc_access = now; 2660168404Spjd DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2661168404Spjd arc_change_state(arc_mfu, buf, hash_lock); 2662168404Spjd } 2663168404Spjd ARCSTAT_BUMP(arcstat_mru_hits); 2664168404Spjd } else if (buf->b_state == arc_mru_ghost) { 2665168404Spjd arc_state_t *new_state; 2666168404Spjd /* 2667168404Spjd * This buffer has been "accessed" recently, but 2668168404Spjd * was evicted from the cache. Move it to the 2669168404Spjd * MFU state. 2670168404Spjd */ 2671168404Spjd 2672168404Spjd if (buf->b_flags & ARC_PREFETCH) { 2673168404Spjd new_state = arc_mru; 2674168404Spjd if (refcount_count(&buf->b_refcnt) > 0) 2675168404Spjd buf->b_flags &= ~ARC_PREFETCH; 2676168404Spjd DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2677168404Spjd } else { 2678168404Spjd new_state = arc_mfu; 2679168404Spjd DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2680168404Spjd } 2681168404Spjd 2682219089Spjd buf->b_arc_access = ddi_get_lbolt(); 2683168404Spjd arc_change_state(new_state, buf, hash_lock); 2684168404Spjd 2685168404Spjd ARCSTAT_BUMP(arcstat_mru_ghost_hits); 2686168404Spjd } else if (buf->b_state == arc_mfu) { 2687168404Spjd /* 2688168404Spjd * This buffer has been accessed more than once and is 2689168404Spjd * still in the cache. Keep it in the MFU state. 2690168404Spjd * 2691168404Spjd * NOTE: an add_reference() that occurred when we did 2692168404Spjd * the arc_read() will have kicked this off the list. 2693168404Spjd * If it was a prefetch, we will explicitly move it to 2694168404Spjd * the head of the list now. 2695168404Spjd */ 2696168404Spjd if ((buf->b_flags & ARC_PREFETCH) != 0) { 2697168404Spjd ASSERT(refcount_count(&buf->b_refcnt) == 0); 2698168404Spjd ASSERT(list_link_active(&buf->b_arc_node)); 2699168404Spjd } 2700168404Spjd ARCSTAT_BUMP(arcstat_mfu_hits); 2701219089Spjd buf->b_arc_access = ddi_get_lbolt(); 2702168404Spjd } else if (buf->b_state == arc_mfu_ghost) { 2703168404Spjd arc_state_t *new_state = arc_mfu; 2704168404Spjd /* 2705168404Spjd * This buffer has been accessed more than once but has 2706168404Spjd * been evicted from the cache. Move it back to the 2707168404Spjd * MFU state. 2708168404Spjd */ 2709168404Spjd 2710168404Spjd if (buf->b_flags & ARC_PREFETCH) { 2711168404Spjd /* 2712168404Spjd * This is a prefetch access... 2713168404Spjd * move this block back to the MRU state. 2714168404Spjd */ 2715168404Spjd ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0); 2716168404Spjd new_state = arc_mru; 2717168404Spjd } 2718168404Spjd 2719219089Spjd buf->b_arc_access = ddi_get_lbolt(); 2720168404Spjd DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2721168404Spjd arc_change_state(new_state, buf, hash_lock); 2722168404Spjd 2723168404Spjd ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 2724185029Spjd } else if (buf->b_state == arc_l2c_only) { 2725185029Spjd /* 2726185029Spjd * This buffer is on the 2nd Level ARC. 2727185029Spjd */ 2728185029Spjd 2729219089Spjd buf->b_arc_access = ddi_get_lbolt(); 2730185029Spjd DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2731185029Spjd arc_change_state(arc_mfu, buf, hash_lock); 2732168404Spjd } else { 2733168404Spjd ASSERT(!"invalid arc state"); 2734168404Spjd } 2735168404Spjd} 2736168404Spjd 2737168404Spjd/* a generic arc_done_func_t which you can use */ 2738168404Spjd/* ARGSUSED */ 2739168404Spjdvoid 2740168404Spjdarc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 2741168404Spjd{ 2742219089Spjd if (zio == NULL || zio->io_error == 0) 2743219089Spjd bcopy(buf->b_data, arg, buf->b_hdr->b_size); 2744168404Spjd VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2745168404Spjd} 2746168404Spjd 2747185029Spjd/* a generic arc_done_func_t */ 2748168404Spjdvoid 2749168404Spjdarc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 2750168404Spjd{ 2751168404Spjd arc_buf_t **bufp = arg; 2752168404Spjd if (zio && zio->io_error) { 2753168404Spjd VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2754168404Spjd *bufp = NULL; 2755168404Spjd } else { 2756168404Spjd *bufp = buf; 2757219089Spjd ASSERT(buf->b_data); 2758168404Spjd } 2759168404Spjd} 2760168404Spjd 2761168404Spjdstatic void 2762168404Spjdarc_read_done(zio_t *zio) 2763168404Spjd{ 2764168404Spjd arc_buf_hdr_t *hdr, *found; 2765168404Spjd arc_buf_t *buf; 2766168404Spjd arc_buf_t *abuf; /* buffer we're assigning to callback */ 2767168404Spjd kmutex_t *hash_lock; 2768168404Spjd arc_callback_t *callback_list, *acb; 2769168404Spjd int freeable = FALSE; 2770168404Spjd 2771168404Spjd buf = zio->io_private; 2772168404Spjd hdr = buf->b_hdr; 2773168404Spjd 2774168404Spjd /* 2775168404Spjd * The hdr was inserted into hash-table and removed from lists 2776168404Spjd * prior to starting I/O. We should find this header, since 2777168404Spjd * it's in the hash table, and it should be legit since it's 2778168404Spjd * not possible to evict it during the I/O. The only possible 2779168404Spjd * reason for it not to be found is if we were freed during the 2780168404Spjd * read. 2781168404Spjd */ 2782209962Smm found = buf_hash_find(hdr->b_spa, &hdr->b_dva, hdr->b_birth, 2783168404Spjd &hash_lock); 2784168404Spjd 2785168404Spjd ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) || 2786185029Spjd (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 2787185029Spjd (found == hdr && HDR_L2_READING(hdr))); 2788168404Spjd 2789185029Spjd hdr->b_flags &= ~ARC_L2_EVICTED; 2790185029Spjd if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH)) 2791185029Spjd hdr->b_flags &= ~ARC_L2CACHE; 2792206796Spjd 2793168404Spjd /* byteswap if necessary */ 2794168404Spjd callback_list = hdr->b_acb; 2795168404Spjd ASSERT(callback_list != NULL); 2796209101Smm if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) { 2797185029Spjd arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 2798185029Spjd byteswap_uint64_array : 2799185029Spjd dmu_ot[BP_GET_TYPE(zio->io_bp)].ot_byteswap; 2800185029Spjd func(buf->b_data, hdr->b_size); 2801185029Spjd } 2802168404Spjd 2803185029Spjd arc_cksum_compute(buf, B_FALSE); 2804168404Spjd 2805219089Spjd if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) { 2806219089Spjd /* 2807219089Spjd * Only call arc_access on anonymous buffers. This is because 2808219089Spjd * if we've issued an I/O for an evicted buffer, we've already 2809219089Spjd * called arc_access (to prevent any simultaneous readers from 2810219089Spjd * getting confused). 2811219089Spjd */ 2812219089Spjd arc_access(hdr, hash_lock); 2813219089Spjd } 2814219089Spjd 2815168404Spjd /* create copies of the data buffer for the callers */ 2816168404Spjd abuf = buf; 2817168404Spjd for (acb = callback_list; acb; acb = acb->acb_next) { 2818168404Spjd if (acb->acb_done) { 2819168404Spjd if (abuf == NULL) 2820168404Spjd abuf = arc_buf_clone(buf); 2821168404Spjd acb->acb_buf = abuf; 2822168404Spjd abuf = NULL; 2823168404Spjd } 2824168404Spjd } 2825168404Spjd hdr->b_acb = NULL; 2826168404Spjd hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 2827168404Spjd ASSERT(!HDR_BUF_AVAILABLE(hdr)); 2828219089Spjd if (abuf == buf) { 2829219089Spjd ASSERT(buf->b_efunc == NULL); 2830219089Spjd ASSERT(hdr->b_datacnt == 1); 2831168404Spjd hdr->b_flags |= ARC_BUF_AVAILABLE; 2832219089Spjd } 2833168404Spjd 2834168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL); 2835168404Spjd 2836168404Spjd if (zio->io_error != 0) { 2837168404Spjd hdr->b_flags |= ARC_IO_ERROR; 2838168404Spjd if (hdr->b_state != arc_anon) 2839168404Spjd arc_change_state(arc_anon, hdr, hash_lock); 2840168404Spjd if (HDR_IN_HASH_TABLE(hdr)) 2841168404Spjd buf_hash_remove(hdr); 2842168404Spjd freeable = refcount_is_zero(&hdr->b_refcnt); 2843168404Spjd } 2844168404Spjd 2845168404Spjd /* 2846168404Spjd * Broadcast before we drop the hash_lock to avoid the possibility 2847168404Spjd * that the hdr (and hence the cv) might be freed before we get to 2848168404Spjd * the cv_broadcast(). 2849168404Spjd */ 2850168404Spjd cv_broadcast(&hdr->b_cv); 2851168404Spjd 2852168404Spjd if (hash_lock) { 2853168404Spjd mutex_exit(hash_lock); 2854168404Spjd } else { 2855168404Spjd /* 2856168404Spjd * This block was freed while we waited for the read to 2857168404Spjd * complete. It has been removed from the hash table and 2858168404Spjd * moved to the anonymous state (so that it won't show up 2859168404Spjd * in the cache). 2860168404Spjd */ 2861168404Spjd ASSERT3P(hdr->b_state, ==, arc_anon); 2862168404Spjd freeable = refcount_is_zero(&hdr->b_refcnt); 2863168404Spjd } 2864168404Spjd 2865168404Spjd /* execute each callback and free its structure */ 2866168404Spjd while ((acb = callback_list) != NULL) { 2867168404Spjd if (acb->acb_done) 2868168404Spjd acb->acb_done(zio, acb->acb_buf, acb->acb_private); 2869168404Spjd 2870168404Spjd if (acb->acb_zio_dummy != NULL) { 2871168404Spjd acb->acb_zio_dummy->io_error = zio->io_error; 2872168404Spjd zio_nowait(acb->acb_zio_dummy); 2873168404Spjd } 2874168404Spjd 2875168404Spjd callback_list = acb->acb_next; 2876168404Spjd kmem_free(acb, sizeof (arc_callback_t)); 2877168404Spjd } 2878168404Spjd 2879168404Spjd if (freeable) 2880168404Spjd arc_hdr_destroy(hdr); 2881168404Spjd} 2882168404Spjd 2883168404Spjd/* 2884168404Spjd * "Read" the block block at the specified DVA (in bp) via the 2885168404Spjd * cache. If the block is found in the cache, invoke the provided 2886168404Spjd * callback immediately and return. Note that the `zio' parameter 2887168404Spjd * in the callback will be NULL in this case, since no IO was 2888168404Spjd * required. If the block is not in the cache pass the read request 2889168404Spjd * on to the spa with a substitute callback function, so that the 2890168404Spjd * requested block will be added to the cache. 2891168404Spjd * 2892168404Spjd * If a read request arrives for a block that has a read in-progress, 2893168404Spjd * either wait for the in-progress read to complete (and return the 2894168404Spjd * results); or, if this is a read with a "done" func, add a record 2895168404Spjd * to the read to invoke the "done" func when the read completes, 2896168404Spjd * and return; or just return. 2897168404Spjd * 2898168404Spjd * arc_read_done() will invoke all the requested "done" functions 2899168404Spjd * for readers of this block. 2900185029Spjd * 2901185029Spjd * Normal callers should use arc_read and pass the arc buffer and offset 2902185029Spjd * for the bp. But if you know you don't need locking, you can use 2903219089Spjd * arc_read_nolock. 2904168404Spjd */ 2905168404Spjdint 2906219089Spjdarc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_buf_t *pbuf, 2907185029Spjd arc_done_func_t *done, void *private, int priority, int zio_flags, 2908185029Spjd uint32_t *arc_flags, const zbookmark_t *zb) 2909168404Spjd{ 2910185029Spjd int err; 2911185029Spjd 2912219089Spjd if (pbuf == NULL) { 2913219089Spjd /* 2914219089Spjd * XXX This happens from traverse callback funcs, for 2915219089Spjd * the objset_phys_t block. 2916219089Spjd */ 2917219089Spjd return (arc_read_nolock(pio, spa, bp, done, private, priority, 2918219089Spjd zio_flags, arc_flags, zb)); 2919219089Spjd } 2920219089Spjd 2921185029Spjd ASSERT(!refcount_is_zero(&pbuf->b_hdr->b_refcnt)); 2922185029Spjd ASSERT3U((char *)bp - (char *)pbuf->b_data, <, pbuf->b_hdr->b_size); 2923219089Spjd rw_enter(&pbuf->b_data_lock, RW_READER); 2924185029Spjd 2925185029Spjd err = arc_read_nolock(pio, spa, bp, done, private, priority, 2926185029Spjd zio_flags, arc_flags, zb); 2927219089Spjd rw_exit(&pbuf->b_data_lock); 2928219089Spjd 2929185029Spjd return (err); 2930185029Spjd} 2931185029Spjd 2932185029Spjdint 2933219089Spjdarc_read_nolock(zio_t *pio, spa_t *spa, const blkptr_t *bp, 2934185029Spjd arc_done_func_t *done, void *private, int priority, int zio_flags, 2935185029Spjd uint32_t *arc_flags, const zbookmark_t *zb) 2936185029Spjd{ 2937168404Spjd arc_buf_hdr_t *hdr; 2938168404Spjd arc_buf_t *buf; 2939168404Spjd kmutex_t *hash_lock; 2940185029Spjd zio_t *rzio; 2941228103Smm uint64_t guid = spa_load_guid(spa); 2942168404Spjd 2943168404Spjdtop: 2944219089Spjd hdr = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp), 2945219089Spjd &hash_lock); 2946168404Spjd if (hdr && hdr->b_datacnt > 0) { 2947168404Spjd 2948168404Spjd *arc_flags |= ARC_CACHED; 2949168404Spjd 2950168404Spjd if (HDR_IO_IN_PROGRESS(hdr)) { 2951168404Spjd 2952168404Spjd if (*arc_flags & ARC_WAIT) { 2953168404Spjd cv_wait(&hdr->b_cv, hash_lock); 2954168404Spjd mutex_exit(hash_lock); 2955168404Spjd goto top; 2956168404Spjd } 2957168404Spjd ASSERT(*arc_flags & ARC_NOWAIT); 2958168404Spjd 2959168404Spjd if (done) { 2960168404Spjd arc_callback_t *acb = NULL; 2961168404Spjd 2962168404Spjd acb = kmem_zalloc(sizeof (arc_callback_t), 2963168404Spjd KM_SLEEP); 2964168404Spjd acb->acb_done = done; 2965168404Spjd acb->acb_private = private; 2966168404Spjd if (pio != NULL) 2967168404Spjd acb->acb_zio_dummy = zio_null(pio, 2968209962Smm spa, NULL, NULL, NULL, zio_flags); 2969168404Spjd 2970168404Spjd ASSERT(acb->acb_done != NULL); 2971168404Spjd acb->acb_next = hdr->b_acb; 2972168404Spjd hdr->b_acb = acb; 2973168404Spjd add_reference(hdr, hash_lock, private); 2974168404Spjd mutex_exit(hash_lock); 2975168404Spjd return (0); 2976168404Spjd } 2977168404Spjd mutex_exit(hash_lock); 2978168404Spjd return (0); 2979168404Spjd } 2980168404Spjd 2981168404Spjd ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 2982168404Spjd 2983168404Spjd if (done) { 2984168404Spjd add_reference(hdr, hash_lock, private); 2985168404Spjd /* 2986168404Spjd * If this block is already in use, create a new 2987168404Spjd * copy of the data so that we will be guaranteed 2988168404Spjd * that arc_release() will always succeed. 2989168404Spjd */ 2990168404Spjd buf = hdr->b_buf; 2991168404Spjd ASSERT(buf); 2992168404Spjd ASSERT(buf->b_data); 2993168404Spjd if (HDR_BUF_AVAILABLE(hdr)) { 2994168404Spjd ASSERT(buf->b_efunc == NULL); 2995168404Spjd hdr->b_flags &= ~ARC_BUF_AVAILABLE; 2996168404Spjd } else { 2997168404Spjd buf = arc_buf_clone(buf); 2998168404Spjd } 2999219089Spjd 3000168404Spjd } else if (*arc_flags & ARC_PREFETCH && 3001168404Spjd refcount_count(&hdr->b_refcnt) == 0) { 3002168404Spjd hdr->b_flags |= ARC_PREFETCH; 3003168404Spjd } 3004168404Spjd DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 3005168404Spjd arc_access(hdr, hash_lock); 3006185029Spjd if (*arc_flags & ARC_L2CACHE) 3007185029Spjd hdr->b_flags |= ARC_L2CACHE; 3008168404Spjd mutex_exit(hash_lock); 3009168404Spjd ARCSTAT_BUMP(arcstat_hits); 3010168404Spjd ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 3011168404Spjd demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 3012168404Spjd data, metadata, hits); 3013168404Spjd 3014168404Spjd if (done) 3015168404Spjd done(NULL, buf, private); 3016168404Spjd } else { 3017168404Spjd uint64_t size = BP_GET_LSIZE(bp); 3018168404Spjd arc_callback_t *acb; 3019185029Spjd vdev_t *vd = NULL; 3020208373Smm uint64_t addr; 3021208373Smm boolean_t devw = B_FALSE; 3022168404Spjd 3023168404Spjd if (hdr == NULL) { 3024168404Spjd /* this block is not in the cache */ 3025168404Spjd arc_buf_hdr_t *exists; 3026168404Spjd arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 3027168404Spjd buf = arc_buf_alloc(spa, size, private, type); 3028168404Spjd hdr = buf->b_hdr; 3029168404Spjd hdr->b_dva = *BP_IDENTITY(bp); 3030219089Spjd hdr->b_birth = BP_PHYSICAL_BIRTH(bp); 3031168404Spjd hdr->b_cksum0 = bp->blk_cksum.zc_word[0]; 3032168404Spjd exists = buf_hash_insert(hdr, &hash_lock); 3033168404Spjd if (exists) { 3034168404Spjd /* somebody beat us to the hash insert */ 3035168404Spjd mutex_exit(hash_lock); 3036219089Spjd buf_discard_identity(hdr); 3037168404Spjd (void) arc_buf_remove_ref(buf, private); 3038168404Spjd goto top; /* restart the IO request */ 3039168404Spjd } 3040168404Spjd /* if this is a prefetch, we don't have a reference */ 3041168404Spjd if (*arc_flags & ARC_PREFETCH) { 3042168404Spjd (void) remove_reference(hdr, hash_lock, 3043168404Spjd private); 3044168404Spjd hdr->b_flags |= ARC_PREFETCH; 3045168404Spjd } 3046185029Spjd if (*arc_flags & ARC_L2CACHE) 3047185029Spjd hdr->b_flags |= ARC_L2CACHE; 3048168404Spjd if (BP_GET_LEVEL(bp) > 0) 3049168404Spjd hdr->b_flags |= ARC_INDIRECT; 3050168404Spjd } else { 3051168404Spjd /* this block is in the ghost cache */ 3052168404Spjd ASSERT(GHOST_STATE(hdr->b_state)); 3053168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3054168404Spjd ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0); 3055168404Spjd ASSERT(hdr->b_buf == NULL); 3056168404Spjd 3057168404Spjd /* if this is a prefetch, we don't have a reference */ 3058168404Spjd if (*arc_flags & ARC_PREFETCH) 3059168404Spjd hdr->b_flags |= ARC_PREFETCH; 3060168404Spjd else 3061168404Spjd add_reference(hdr, hash_lock, private); 3062185029Spjd if (*arc_flags & ARC_L2CACHE) 3063185029Spjd hdr->b_flags |= ARC_L2CACHE; 3064185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 3065168404Spjd buf->b_hdr = hdr; 3066168404Spjd buf->b_data = NULL; 3067168404Spjd buf->b_efunc = NULL; 3068168404Spjd buf->b_private = NULL; 3069168404Spjd buf->b_next = NULL; 3070168404Spjd hdr->b_buf = buf; 3071168404Spjd ASSERT(hdr->b_datacnt == 0); 3072168404Spjd hdr->b_datacnt = 1; 3073219089Spjd arc_get_data_buf(buf); 3074219089Spjd arc_access(hdr, hash_lock); 3075168404Spjd } 3076168404Spjd 3077219089Spjd ASSERT(!GHOST_STATE(hdr->b_state)); 3078219089Spjd 3079168404Spjd acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 3080168404Spjd acb->acb_done = done; 3081168404Spjd acb->acb_private = private; 3082168404Spjd 3083168404Spjd ASSERT(hdr->b_acb == NULL); 3084168404Spjd hdr->b_acb = acb; 3085168404Spjd hdr->b_flags |= ARC_IO_IN_PROGRESS; 3086168404Spjd 3087185029Spjd if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL && 3088185029Spjd (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) { 3089208373Smm devw = hdr->b_l2hdr->b_dev->l2ad_writing; 3090185029Spjd addr = hdr->b_l2hdr->b_daddr; 3091185029Spjd /* 3092185029Spjd * Lock out device removal. 3093185029Spjd */ 3094185029Spjd if (vdev_is_dead(vd) || 3095185029Spjd !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 3096185029Spjd vd = NULL; 3097185029Spjd } 3098185029Spjd 3099168404Spjd mutex_exit(hash_lock); 3100168404Spjd 3101168404Spjd ASSERT3U(hdr->b_size, ==, size); 3102219089Spjd DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp, 3103219089Spjd uint64_t, size, zbookmark_t *, zb); 3104168404Spjd ARCSTAT_BUMP(arcstat_misses); 3105168404Spjd ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 3106168404Spjd demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 3107168404Spjd data, metadata, misses); 3108228392Spjd#ifdef _KERNEL 3109228392Spjd curthread->td_ru.ru_inblock++; 3110228392Spjd#endif 3111168404Spjd 3112208373Smm if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 3113185029Spjd /* 3114185029Spjd * Read from the L2ARC if the following are true: 3115185029Spjd * 1. The L2ARC vdev was previously cached. 3116185029Spjd * 2. This buffer still has L2ARC metadata. 3117185029Spjd * 3. This buffer isn't currently writing to the L2ARC. 3118185029Spjd * 4. The L2ARC entry wasn't evicted, which may 3119185029Spjd * also have invalidated the vdev. 3120208373Smm * 5. This isn't prefetch and l2arc_noprefetch is set. 3121185029Spjd */ 3122185029Spjd if (hdr->b_l2hdr != NULL && 3123208373Smm !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 3124208373Smm !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 3125185029Spjd l2arc_read_callback_t *cb; 3126185029Spjd 3127185029Spjd DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 3128185029Spjd ARCSTAT_BUMP(arcstat_l2_hits); 3129185029Spjd 3130185029Spjd cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 3131185029Spjd KM_SLEEP); 3132185029Spjd cb->l2rcb_buf = buf; 3133185029Spjd cb->l2rcb_spa = spa; 3134185029Spjd cb->l2rcb_bp = *bp; 3135185029Spjd cb->l2rcb_zb = *zb; 3136185029Spjd cb->l2rcb_flags = zio_flags; 3137185029Spjd 3138185029Spjd /* 3139185029Spjd * l2arc read. The SCL_L2ARC lock will be 3140185029Spjd * released by l2arc_read_done(). 3141185029Spjd */ 3142185029Spjd rzio = zio_read_phys(pio, vd, addr, size, 3143206796Spjd buf->b_data, ZIO_CHECKSUM_OFF, 3144185029Spjd l2arc_read_done, cb, priority, zio_flags | 3145185029Spjd ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL | 3146185029Spjd ZIO_FLAG_DONT_PROPAGATE | 3147185029Spjd ZIO_FLAG_DONT_RETRY, B_FALSE); 3148185029Spjd DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 3149185029Spjd zio_t *, rzio); 3150208373Smm ARCSTAT_INCR(arcstat_l2_read_bytes, size); 3151185029Spjd 3152185029Spjd if (*arc_flags & ARC_NOWAIT) { 3153185029Spjd zio_nowait(rzio); 3154185029Spjd return (0); 3155185029Spjd } 3156185029Spjd 3157185029Spjd ASSERT(*arc_flags & ARC_WAIT); 3158185029Spjd if (zio_wait(rzio) == 0) 3159185029Spjd return (0); 3160185029Spjd 3161185029Spjd /* l2arc read error; goto zio_read() */ 3162185029Spjd } else { 3163185029Spjd DTRACE_PROBE1(l2arc__miss, 3164185029Spjd arc_buf_hdr_t *, hdr); 3165185029Spjd ARCSTAT_BUMP(arcstat_l2_misses); 3166185029Spjd if (HDR_L2_WRITING(hdr)) 3167185029Spjd ARCSTAT_BUMP(arcstat_l2_rw_clash); 3168185029Spjd spa_config_exit(spa, SCL_L2ARC, vd); 3169185029Spjd } 3170208373Smm } else { 3171208373Smm if (vd != NULL) 3172208373Smm spa_config_exit(spa, SCL_L2ARC, vd); 3173208373Smm if (l2arc_ndev != 0) { 3174208373Smm DTRACE_PROBE1(l2arc__miss, 3175208373Smm arc_buf_hdr_t *, hdr); 3176208373Smm ARCSTAT_BUMP(arcstat_l2_misses); 3177208373Smm } 3178185029Spjd } 3179185029Spjd 3180168404Spjd rzio = zio_read(pio, spa, bp, buf->b_data, size, 3181185029Spjd arc_read_done, buf, priority, zio_flags, zb); 3182168404Spjd 3183168404Spjd if (*arc_flags & ARC_WAIT) 3184168404Spjd return (zio_wait(rzio)); 3185168404Spjd 3186168404Spjd ASSERT(*arc_flags & ARC_NOWAIT); 3187168404Spjd zio_nowait(rzio); 3188168404Spjd } 3189168404Spjd return (0); 3190168404Spjd} 3191168404Spjd 3192168404Spjdvoid 3193168404Spjdarc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 3194168404Spjd{ 3195168404Spjd ASSERT(buf->b_hdr != NULL); 3196168404Spjd ASSERT(buf->b_hdr->b_state != arc_anon); 3197168404Spjd ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL); 3198219089Spjd ASSERT(buf->b_efunc == NULL); 3199219089Spjd ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr)); 3200219089Spjd 3201168404Spjd buf->b_efunc = func; 3202168404Spjd buf->b_private = private; 3203168404Spjd} 3204168404Spjd 3205168404Spjd/* 3206168404Spjd * This is used by the DMU to let the ARC know that a buffer is 3207168404Spjd * being evicted, so the ARC should clean up. If this arc buf 3208168404Spjd * is not yet in the evicted state, it will be put there. 3209168404Spjd */ 3210168404Spjdint 3211168404Spjdarc_buf_evict(arc_buf_t *buf) 3212168404Spjd{ 3213168404Spjd arc_buf_hdr_t *hdr; 3214168404Spjd kmutex_t *hash_lock; 3215168404Spjd arc_buf_t **bufp; 3216205231Skmacy list_t *list, *evicted_list; 3217205231Skmacy kmutex_t *lock, *evicted_lock; 3218206796Spjd 3219219089Spjd mutex_enter(&buf->b_evict_lock); 3220168404Spjd hdr = buf->b_hdr; 3221168404Spjd if (hdr == NULL) { 3222168404Spjd /* 3223168404Spjd * We are in arc_do_user_evicts(). 3224168404Spjd */ 3225168404Spjd ASSERT(buf->b_data == NULL); 3226219089Spjd mutex_exit(&buf->b_evict_lock); 3227168404Spjd return (0); 3228185029Spjd } else if (buf->b_data == NULL) { 3229185029Spjd arc_buf_t copy = *buf; /* structure assignment */ 3230185029Spjd /* 3231185029Spjd * We are on the eviction list; process this buffer now 3232185029Spjd * but let arc_do_user_evicts() do the reaping. 3233185029Spjd */ 3234185029Spjd buf->b_efunc = NULL; 3235219089Spjd mutex_exit(&buf->b_evict_lock); 3236185029Spjd VERIFY(copy.b_efunc(©) == 0); 3237185029Spjd return (1); 3238168404Spjd } 3239168404Spjd hash_lock = HDR_LOCK(hdr); 3240168404Spjd mutex_enter(hash_lock); 3241219089Spjd hdr = buf->b_hdr; 3242219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 3243168404Spjd 3244168404Spjd ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt); 3245168404Spjd ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 3246168404Spjd 3247168404Spjd /* 3248168404Spjd * Pull this buffer off of the hdr 3249168404Spjd */ 3250168404Spjd bufp = &hdr->b_buf; 3251168404Spjd while (*bufp != buf) 3252168404Spjd bufp = &(*bufp)->b_next; 3253168404Spjd *bufp = buf->b_next; 3254168404Spjd 3255168404Spjd ASSERT(buf->b_data != NULL); 3256168404Spjd arc_buf_destroy(buf, FALSE, FALSE); 3257168404Spjd 3258168404Spjd if (hdr->b_datacnt == 0) { 3259168404Spjd arc_state_t *old_state = hdr->b_state; 3260168404Spjd arc_state_t *evicted_state; 3261168404Spjd 3262219089Spjd ASSERT(hdr->b_buf == NULL); 3263168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 3264168404Spjd 3265168404Spjd evicted_state = 3266168404Spjd (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 3267168404Spjd 3268205231Skmacy get_buf_info(hdr, old_state, &list, &lock); 3269205231Skmacy get_buf_info(hdr, evicted_state, &evicted_list, &evicted_lock); 3270205231Skmacy mutex_enter(lock); 3271205231Skmacy mutex_enter(evicted_lock); 3272168404Spjd 3273168404Spjd arc_change_state(evicted_state, hdr, hash_lock); 3274168404Spjd ASSERT(HDR_IN_HASH_TABLE(hdr)); 3275185029Spjd hdr->b_flags |= ARC_IN_HASH_TABLE; 3276185029Spjd hdr->b_flags &= ~ARC_BUF_AVAILABLE; 3277168404Spjd 3278205231Skmacy mutex_exit(evicted_lock); 3279205231Skmacy mutex_exit(lock); 3280168404Spjd } 3281168404Spjd mutex_exit(hash_lock); 3282219089Spjd mutex_exit(&buf->b_evict_lock); 3283168404Spjd 3284168404Spjd VERIFY(buf->b_efunc(buf) == 0); 3285168404Spjd buf->b_efunc = NULL; 3286168404Spjd buf->b_private = NULL; 3287168404Spjd buf->b_hdr = NULL; 3288219089Spjd buf->b_next = NULL; 3289168404Spjd kmem_cache_free(buf_cache, buf); 3290168404Spjd return (1); 3291168404Spjd} 3292168404Spjd 3293168404Spjd/* 3294168404Spjd * Release this buffer from the cache. This must be done 3295168404Spjd * after a read and prior to modifying the buffer contents. 3296168404Spjd * If the buffer has more than one reference, we must make 3297185029Spjd * a new hdr for the buffer. 3298168404Spjd */ 3299168404Spjdvoid 3300168404Spjdarc_release(arc_buf_t *buf, void *tag) 3301168404Spjd{ 3302185029Spjd arc_buf_hdr_t *hdr; 3303219089Spjd kmutex_t *hash_lock = NULL; 3304185029Spjd l2arc_buf_hdr_t *l2hdr; 3305185029Spjd uint64_t buf_size; 3306168404Spjd 3307219089Spjd /* 3308219089Spjd * It would be nice to assert that if it's DMU metadata (level > 3309219089Spjd * 0 || it's the dnode file), then it must be syncing context. 3310219089Spjd * But we don't know that information at this level. 3311219089Spjd */ 3312219089Spjd 3313219089Spjd mutex_enter(&buf->b_evict_lock); 3314185029Spjd hdr = buf->b_hdr; 3315185029Spjd 3316168404Spjd /* this buffer is not on any list */ 3317168404Spjd ASSERT(refcount_count(&hdr->b_refcnt) > 0); 3318168404Spjd 3319168404Spjd if (hdr->b_state == arc_anon) { 3320168404Spjd /* this buffer is already released */ 3321168404Spjd ASSERT(buf->b_efunc == NULL); 3322208373Smm } else { 3323208373Smm hash_lock = HDR_LOCK(hdr); 3324208373Smm mutex_enter(hash_lock); 3325219089Spjd hdr = buf->b_hdr; 3326219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 3327168404Spjd } 3328168404Spjd 3329185029Spjd l2hdr = hdr->b_l2hdr; 3330185029Spjd if (l2hdr) { 3331185029Spjd mutex_enter(&l2arc_buflist_mtx); 3332185029Spjd hdr->b_l2hdr = NULL; 3333185029Spjd buf_size = hdr->b_size; 3334185029Spjd } 3335185029Spjd 3336168404Spjd /* 3337168404Spjd * Do we have more than one buf? 3338168404Spjd */ 3339185029Spjd if (hdr->b_datacnt > 1) { 3340168404Spjd arc_buf_hdr_t *nhdr; 3341168404Spjd arc_buf_t **bufp; 3342168404Spjd uint64_t blksz = hdr->b_size; 3343209962Smm uint64_t spa = hdr->b_spa; 3344168404Spjd arc_buf_contents_t type = hdr->b_type; 3345185029Spjd uint32_t flags = hdr->b_flags; 3346168404Spjd 3347185029Spjd ASSERT(hdr->b_buf != buf || buf->b_next != NULL); 3348168404Spjd /* 3349219089Spjd * Pull the data off of this hdr and attach it to 3350219089Spjd * a new anonymous hdr. 3351168404Spjd */ 3352168404Spjd (void) remove_reference(hdr, hash_lock, tag); 3353168404Spjd bufp = &hdr->b_buf; 3354168404Spjd while (*bufp != buf) 3355168404Spjd bufp = &(*bufp)->b_next; 3356219089Spjd *bufp = buf->b_next; 3357168404Spjd buf->b_next = NULL; 3358168404Spjd 3359168404Spjd ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size); 3360168404Spjd atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size); 3361168404Spjd if (refcount_is_zero(&hdr->b_refcnt)) { 3362185029Spjd uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type]; 3363185029Spjd ASSERT3U(*size, >=, hdr->b_size); 3364185029Spjd atomic_add_64(size, -hdr->b_size); 3365168404Spjd } 3366168404Spjd hdr->b_datacnt -= 1; 3367168404Spjd arc_cksum_verify(buf); 3368168404Spjd 3369168404Spjd mutex_exit(hash_lock); 3370168404Spjd 3371185029Spjd nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 3372168404Spjd nhdr->b_size = blksz; 3373168404Spjd nhdr->b_spa = spa; 3374168404Spjd nhdr->b_type = type; 3375168404Spjd nhdr->b_buf = buf; 3376168404Spjd nhdr->b_state = arc_anon; 3377168404Spjd nhdr->b_arc_access = 0; 3378185029Spjd nhdr->b_flags = flags & ARC_L2_WRITING; 3379185029Spjd nhdr->b_l2hdr = NULL; 3380168404Spjd nhdr->b_datacnt = 1; 3381168404Spjd nhdr->b_freeze_cksum = NULL; 3382168404Spjd (void) refcount_add(&nhdr->b_refcnt, tag); 3383168404Spjd buf->b_hdr = nhdr; 3384219089Spjd mutex_exit(&buf->b_evict_lock); 3385168404Spjd atomic_add_64(&arc_anon->arcs_size, blksz); 3386168404Spjd } else { 3387219089Spjd mutex_exit(&buf->b_evict_lock); 3388168404Spjd ASSERT(refcount_count(&hdr->b_refcnt) == 1); 3389168404Spjd ASSERT(!list_link_active(&hdr->b_arc_node)); 3390168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3391219089Spjd if (hdr->b_state != arc_anon) 3392219089Spjd arc_change_state(arc_anon, hdr, hash_lock); 3393168404Spjd hdr->b_arc_access = 0; 3394219089Spjd if (hash_lock) 3395219089Spjd mutex_exit(hash_lock); 3396185029Spjd 3397219089Spjd buf_discard_identity(hdr); 3398168404Spjd arc_buf_thaw(buf); 3399168404Spjd } 3400168404Spjd buf->b_efunc = NULL; 3401168404Spjd buf->b_private = NULL; 3402185029Spjd 3403185029Spjd if (l2hdr) { 3404185029Spjd list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 3405185029Spjd kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 3406185029Spjd ARCSTAT_INCR(arcstat_l2_size, -buf_size); 3407185029Spjd mutex_exit(&l2arc_buflist_mtx); 3408185029Spjd } 3409168404Spjd} 3410168404Spjd 3411219089Spjd/* 3412219089Spjd * Release this buffer. If it does not match the provided BP, fill it 3413219089Spjd * with that block's contents. 3414219089Spjd */ 3415219089Spjd/* ARGSUSED */ 3416168404Spjdint 3417219089Spjdarc_release_bp(arc_buf_t *buf, void *tag, blkptr_t *bp, spa_t *spa, 3418219089Spjd zbookmark_t *zb) 3419219089Spjd{ 3420219089Spjd arc_release(buf, tag); 3421219089Spjd return (0); 3422219089Spjd} 3423219089Spjd 3424219089Spjdint 3425168404Spjdarc_released(arc_buf_t *buf) 3426168404Spjd{ 3427185029Spjd int released; 3428185029Spjd 3429219089Spjd mutex_enter(&buf->b_evict_lock); 3430185029Spjd released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon); 3431219089Spjd mutex_exit(&buf->b_evict_lock); 3432185029Spjd return (released); 3433168404Spjd} 3434168404Spjd 3435168404Spjdint 3436168404Spjdarc_has_callback(arc_buf_t *buf) 3437168404Spjd{ 3438185029Spjd int callback; 3439185029Spjd 3440219089Spjd mutex_enter(&buf->b_evict_lock); 3441185029Spjd callback = (buf->b_efunc != NULL); 3442219089Spjd mutex_exit(&buf->b_evict_lock); 3443185029Spjd return (callback); 3444168404Spjd} 3445168404Spjd 3446168404Spjd#ifdef ZFS_DEBUG 3447168404Spjdint 3448168404Spjdarc_referenced(arc_buf_t *buf) 3449168404Spjd{ 3450185029Spjd int referenced; 3451185029Spjd 3452219089Spjd mutex_enter(&buf->b_evict_lock); 3453185029Spjd referenced = (refcount_count(&buf->b_hdr->b_refcnt)); 3454219089Spjd mutex_exit(&buf->b_evict_lock); 3455185029Spjd return (referenced); 3456168404Spjd} 3457168404Spjd#endif 3458168404Spjd 3459168404Spjdstatic void 3460168404Spjdarc_write_ready(zio_t *zio) 3461168404Spjd{ 3462168404Spjd arc_write_callback_t *callback = zio->io_private; 3463168404Spjd arc_buf_t *buf = callback->awcb_buf; 3464185029Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3465168404Spjd 3466185029Spjd ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt)); 3467185029Spjd callback->awcb_ready(zio, buf, callback->awcb_private); 3468185029Spjd 3469185029Spjd /* 3470185029Spjd * If the IO is already in progress, then this is a re-write 3471185029Spjd * attempt, so we need to thaw and re-compute the cksum. 3472185029Spjd * It is the responsibility of the callback to handle the 3473185029Spjd * accounting for any re-write attempt. 3474185029Spjd */ 3475185029Spjd if (HDR_IO_IN_PROGRESS(hdr)) { 3476185029Spjd mutex_enter(&hdr->b_freeze_lock); 3477185029Spjd if (hdr->b_freeze_cksum != NULL) { 3478185029Spjd kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 3479185029Spjd hdr->b_freeze_cksum = NULL; 3480185029Spjd } 3481185029Spjd mutex_exit(&hdr->b_freeze_lock); 3482168404Spjd } 3483185029Spjd arc_cksum_compute(buf, B_FALSE); 3484185029Spjd hdr->b_flags |= ARC_IO_IN_PROGRESS; 3485168404Spjd} 3486168404Spjd 3487168404Spjdstatic void 3488168404Spjdarc_write_done(zio_t *zio) 3489168404Spjd{ 3490168404Spjd arc_write_callback_t *callback = zio->io_private; 3491168404Spjd arc_buf_t *buf = callback->awcb_buf; 3492168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3493168404Spjd 3494219089Spjd ASSERT(hdr->b_acb == NULL); 3495168404Spjd 3496219089Spjd if (zio->io_error == 0) { 3497219089Spjd hdr->b_dva = *BP_IDENTITY(zio->io_bp); 3498219089Spjd hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); 3499219089Spjd hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0]; 3500219089Spjd } else { 3501219089Spjd ASSERT(BUF_EMPTY(hdr)); 3502219089Spjd } 3503219089Spjd 3504168404Spjd /* 3505168404Spjd * If the block to be written was all-zero, we may have 3506168404Spjd * compressed it away. In this case no write was performed 3507219089Spjd * so there will be no dva/birth/checksum. The buffer must 3508219089Spjd * therefore remain anonymous (and uncached). 3509168404Spjd */ 3510168404Spjd if (!BUF_EMPTY(hdr)) { 3511168404Spjd arc_buf_hdr_t *exists; 3512168404Spjd kmutex_t *hash_lock; 3513168404Spjd 3514219089Spjd ASSERT(zio->io_error == 0); 3515219089Spjd 3516168404Spjd arc_cksum_verify(buf); 3517168404Spjd 3518168404Spjd exists = buf_hash_insert(hdr, &hash_lock); 3519168404Spjd if (exists) { 3520168404Spjd /* 3521168404Spjd * This can only happen if we overwrite for 3522168404Spjd * sync-to-convergence, because we remove 3523168404Spjd * buffers from the hash table when we arc_free(). 3524168404Spjd */ 3525219089Spjd if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 3526219089Spjd if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 3527219089Spjd panic("bad overwrite, hdr=%p exists=%p", 3528219089Spjd (void *)hdr, (void *)exists); 3529219089Spjd ASSERT(refcount_is_zero(&exists->b_refcnt)); 3530219089Spjd arc_change_state(arc_anon, exists, hash_lock); 3531219089Spjd mutex_exit(hash_lock); 3532219089Spjd arc_hdr_destroy(exists); 3533219089Spjd exists = buf_hash_insert(hdr, &hash_lock); 3534219089Spjd ASSERT3P(exists, ==, NULL); 3535219089Spjd } else { 3536219089Spjd /* Dedup */ 3537219089Spjd ASSERT(hdr->b_datacnt == 1); 3538219089Spjd ASSERT(hdr->b_state == arc_anon); 3539219089Spjd ASSERT(BP_GET_DEDUP(zio->io_bp)); 3540219089Spjd ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 3541219089Spjd } 3542168404Spjd } 3543168404Spjd hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3544185029Spjd /* if it's not anon, we are doing a scrub */ 3545219089Spjd if (!exists && hdr->b_state == arc_anon) 3546185029Spjd arc_access(hdr, hash_lock); 3547168404Spjd mutex_exit(hash_lock); 3548168404Spjd } else { 3549168404Spjd hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3550168404Spjd } 3551168404Spjd 3552219089Spjd ASSERT(!refcount_is_zero(&hdr->b_refcnt)); 3553219089Spjd callback->awcb_done(zio, buf, callback->awcb_private); 3554168404Spjd 3555168404Spjd kmem_free(callback, sizeof (arc_write_callback_t)); 3556168404Spjd} 3557168404Spjd 3558168404Spjdzio_t * 3559219089Spjdarc_write(zio_t *pio, spa_t *spa, uint64_t txg, 3560219089Spjd blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, const zio_prop_t *zp, 3561219089Spjd arc_done_func_t *ready, arc_done_func_t *done, void *private, 3562219089Spjd int priority, int zio_flags, const zbookmark_t *zb) 3563168404Spjd{ 3564168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3565168404Spjd arc_write_callback_t *callback; 3566185029Spjd zio_t *zio; 3567168404Spjd 3568185029Spjd ASSERT(ready != NULL); 3569219089Spjd ASSERT(done != NULL); 3570168404Spjd ASSERT(!HDR_IO_ERROR(hdr)); 3571168404Spjd ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0); 3572219089Spjd ASSERT(hdr->b_acb == NULL); 3573185029Spjd if (l2arc) 3574185029Spjd hdr->b_flags |= ARC_L2CACHE; 3575168404Spjd callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 3576168404Spjd callback->awcb_ready = ready; 3577168404Spjd callback->awcb_done = done; 3578168404Spjd callback->awcb_private = private; 3579168404Spjd callback->awcb_buf = buf; 3580168404Spjd 3581219089Spjd zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp, 3582185029Spjd arc_write_ready, arc_write_done, callback, priority, zio_flags, zb); 3583185029Spjd 3584168404Spjd return (zio); 3585168404Spjd} 3586168404Spjd 3587185029Spjdstatic int 3588209962Smmarc_memory_throttle(uint64_t reserve, uint64_t inflight_data, uint64_t txg) 3589185029Spjd{ 3590185029Spjd#ifdef _KERNEL 3591219089Spjd uint64_t available_memory = 3592219089Spjd ptoa((uintmax_t)cnt.v_free_count + cnt.v_cache_count); 3593185029Spjd static uint64_t page_load = 0; 3594185029Spjd static uint64_t last_txg = 0; 3595185029Spjd 3596219089Spjd#ifdef sun 3597185029Spjd#if defined(__i386) 3598185029Spjd available_memory = 3599185029Spjd MIN(available_memory, vmem_size(heap_arena, VMEM_FREE)); 3600185029Spjd#endif 3601219089Spjd#endif /* sun */ 3602185029Spjd if (available_memory >= zfs_write_limit_max) 3603185029Spjd return (0); 3604185029Spjd 3605185029Spjd if (txg > last_txg) { 3606185029Spjd last_txg = txg; 3607185029Spjd page_load = 0; 3608185029Spjd } 3609185029Spjd /* 3610185029Spjd * If we are in pageout, we know that memory is already tight, 3611185029Spjd * the arc is already going to be evicting, so we just want to 3612185029Spjd * continue to let page writes occur as quickly as possible. 3613185029Spjd */ 3614185029Spjd if (curproc == pageproc) { 3615185029Spjd if (page_load > available_memory / 4) 3616185029Spjd return (ERESTART); 3617185029Spjd /* Note: reserve is inflated, so we deflate */ 3618185029Spjd page_load += reserve / 8; 3619185029Spjd return (0); 3620185029Spjd } else if (page_load > 0 && arc_reclaim_needed()) { 3621185029Spjd /* memory is low, delay before restarting */ 3622185029Spjd ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 3623185029Spjd return (EAGAIN); 3624185029Spjd } 3625185029Spjd page_load = 0; 3626185029Spjd 3627185029Spjd if (arc_size > arc_c_min) { 3628185029Spjd uint64_t evictable_memory = 3629185029Spjd arc_mru->arcs_lsize[ARC_BUFC_DATA] + 3630185029Spjd arc_mru->arcs_lsize[ARC_BUFC_METADATA] + 3631185029Spjd arc_mfu->arcs_lsize[ARC_BUFC_DATA] + 3632185029Spjd arc_mfu->arcs_lsize[ARC_BUFC_METADATA]; 3633185029Spjd available_memory += MIN(evictable_memory, arc_size - arc_c_min); 3634185029Spjd } 3635185029Spjd 3636185029Spjd if (inflight_data > available_memory / 4) { 3637185029Spjd ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 3638185029Spjd return (ERESTART); 3639185029Spjd } 3640185029Spjd#endif 3641185029Spjd return (0); 3642185029Spjd} 3643185029Spjd 3644168404Spjdvoid 3645185029Spjdarc_tempreserve_clear(uint64_t reserve) 3646168404Spjd{ 3647185029Spjd atomic_add_64(&arc_tempreserve, -reserve); 3648168404Spjd ASSERT((int64_t)arc_tempreserve >= 0); 3649168404Spjd} 3650168404Spjd 3651168404Spjdint 3652185029Spjdarc_tempreserve_space(uint64_t reserve, uint64_t txg) 3653168404Spjd{ 3654185029Spjd int error; 3655209962Smm uint64_t anon_size; 3656185029Spjd 3657168404Spjd#ifdef ZFS_DEBUG 3658168404Spjd /* 3659168404Spjd * Once in a while, fail for no reason. Everything should cope. 3660168404Spjd */ 3661168404Spjd if (spa_get_random(10000) == 0) { 3662168404Spjd dprintf("forcing random failure\n"); 3663168404Spjd return (ERESTART); 3664168404Spjd } 3665168404Spjd#endif 3666185029Spjd if (reserve > arc_c/4 && !arc_no_grow) 3667185029Spjd arc_c = MIN(arc_c_max, reserve * 4); 3668185029Spjd if (reserve > arc_c) 3669168404Spjd return (ENOMEM); 3670168404Spjd 3671168404Spjd /* 3672209962Smm * Don't count loaned bufs as in flight dirty data to prevent long 3673209962Smm * network delays from blocking transactions that are ready to be 3674209962Smm * assigned to a txg. 3675209962Smm */ 3676209962Smm anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0); 3677209962Smm 3678209962Smm /* 3679185029Spjd * Writes will, almost always, require additional memory allocations 3680185029Spjd * in order to compress/encrypt/etc the data. We therefor need to 3681185029Spjd * make sure that there is sufficient available memory for this. 3682185029Spjd */ 3683209962Smm if (error = arc_memory_throttle(reserve, anon_size, txg)) 3684185029Spjd return (error); 3685185029Spjd 3686185029Spjd /* 3687168404Spjd * Throttle writes when the amount of dirty data in the cache 3688168404Spjd * gets too large. We try to keep the cache less than half full 3689168404Spjd * of dirty blocks so that our sync times don't grow too large. 3690168404Spjd * Note: if two requests come in concurrently, we might let them 3691168404Spjd * both succeed, when one of them should fail. Not a huge deal. 3692168404Spjd */ 3693209962Smm 3694209962Smm if (reserve + arc_tempreserve + anon_size > arc_c / 2 && 3695209962Smm anon_size > arc_c / 4) { 3696185029Spjd dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 3697185029Spjd "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 3698185029Spjd arc_tempreserve>>10, 3699185029Spjd arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 3700185029Spjd arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 3701185029Spjd reserve>>10, arc_c>>10); 3702168404Spjd return (ERESTART); 3703168404Spjd } 3704185029Spjd atomic_add_64(&arc_tempreserve, reserve); 3705168404Spjd return (0); 3706168404Spjd} 3707168404Spjd 3708168582Spjdstatic kmutex_t arc_lowmem_lock; 3709168404Spjd#ifdef _KERNEL 3710168566Spjdstatic eventhandler_tag arc_event_lowmem = NULL; 3711168404Spjd 3712168404Spjdstatic void 3713168566Spjdarc_lowmem(void *arg __unused, int howto __unused) 3714168404Spjd{ 3715168404Spjd 3716168566Spjd /* Serialize access via arc_lowmem_lock. */ 3717168566Spjd mutex_enter(&arc_lowmem_lock); 3718219089Spjd mutex_enter(&arc_reclaim_thr_lock); 3719185029Spjd needfree = 1; 3720168404Spjd cv_signal(&arc_reclaim_thr_cv); 3721185029Spjd while (needfree) 3722219089Spjd msleep(&needfree, &arc_reclaim_thr_lock, 0, "zfs:lowmem", 0); 3723219089Spjd mutex_exit(&arc_reclaim_thr_lock); 3724168566Spjd mutex_exit(&arc_lowmem_lock); 3725168404Spjd} 3726168404Spjd#endif 3727168404Spjd 3728168404Spjdvoid 3729168404Spjdarc_init(void) 3730168404Spjd{ 3731219089Spjd int i, prefetch_tunable_set = 0; 3732205231Skmacy 3733168404Spjd mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 3734168404Spjd cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 3735168566Spjd mutex_init(&arc_lowmem_lock, NULL, MUTEX_DEFAULT, NULL); 3736168404Spjd 3737168404Spjd /* Convert seconds to clock ticks */ 3738168404Spjd arc_min_prefetch_lifespan = 1 * hz; 3739168404Spjd 3740168404Spjd /* Start out with 1/8 of all memory */ 3741168566Spjd arc_c = kmem_size() / 8; 3742219089Spjd 3743219089Spjd#ifdef sun 3744192360Skmacy#ifdef _KERNEL 3745192360Skmacy /* 3746192360Skmacy * On architectures where the physical memory can be larger 3747192360Skmacy * than the addressable space (intel in 32-bit mode), we may 3748192360Skmacy * need to limit the cache to 1/8 of VM size. 3749192360Skmacy */ 3750192360Skmacy arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 3751192360Skmacy#endif 3752219089Spjd#endif /* sun */ 3753168566Spjd /* set min cache to 1/32 of all memory, or 16MB, whichever is more */ 3754168566Spjd arc_c_min = MAX(arc_c / 4, 64<<18); 3755168566Spjd /* set max to 1/2 of all memory, or all but 1GB, whichever is more */ 3756168404Spjd if (arc_c * 8 >= 1<<30) 3757168404Spjd arc_c_max = (arc_c * 8) - (1<<30); 3758168404Spjd else 3759168404Spjd arc_c_max = arc_c_min; 3760175633Spjd arc_c_max = MAX(arc_c * 5, arc_c_max); 3761219089Spjd 3762168481Spjd#ifdef _KERNEL 3763168404Spjd /* 3764168404Spjd * Allow the tunables to override our calculations if they are 3765168566Spjd * reasonable (ie. over 16MB) 3766168404Spjd */ 3767219089Spjd if (zfs_arc_max > 64<<18 && zfs_arc_max < kmem_size()) 3768168404Spjd arc_c_max = zfs_arc_max; 3769219089Spjd if (zfs_arc_min > 64<<18 && zfs_arc_min <= arc_c_max) 3770168404Spjd arc_c_min = zfs_arc_min; 3771168481Spjd#endif 3772219089Spjd 3773168404Spjd arc_c = arc_c_max; 3774168404Spjd arc_p = (arc_c >> 1); 3775168404Spjd 3776185029Spjd /* limit meta-data to 1/4 of the arc capacity */ 3777185029Spjd arc_meta_limit = arc_c_max / 4; 3778185029Spjd 3779185029Spjd /* Allow the tunable to override if it is reasonable */ 3780185029Spjd if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 3781185029Spjd arc_meta_limit = zfs_arc_meta_limit; 3782185029Spjd 3783185029Spjd if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 3784185029Spjd arc_c_min = arc_meta_limit / 2; 3785185029Spjd 3786208373Smm if (zfs_arc_grow_retry > 0) 3787208373Smm arc_grow_retry = zfs_arc_grow_retry; 3788208373Smm 3789208373Smm if (zfs_arc_shrink_shift > 0) 3790208373Smm arc_shrink_shift = zfs_arc_shrink_shift; 3791208373Smm 3792208373Smm if (zfs_arc_p_min_shift > 0) 3793208373Smm arc_p_min_shift = zfs_arc_p_min_shift; 3794208373Smm 3795168404Spjd /* if kmem_flags are set, lets try to use less memory */ 3796168404Spjd if (kmem_debugging()) 3797168404Spjd arc_c = arc_c / 2; 3798168404Spjd if (arc_c < arc_c_min) 3799168404Spjd arc_c = arc_c_min; 3800168404Spjd 3801168473Spjd zfs_arc_min = arc_c_min; 3802168473Spjd zfs_arc_max = arc_c_max; 3803168473Spjd 3804168404Spjd arc_anon = &ARC_anon; 3805168404Spjd arc_mru = &ARC_mru; 3806168404Spjd arc_mru_ghost = &ARC_mru_ghost; 3807168404Spjd arc_mfu = &ARC_mfu; 3808168404Spjd arc_mfu_ghost = &ARC_mfu_ghost; 3809185029Spjd arc_l2c_only = &ARC_l2c_only; 3810168404Spjd arc_size = 0; 3811168404Spjd 3812205231Skmacy for (i = 0; i < ARC_BUFC_NUMLISTS; i++) { 3813205231Skmacy mutex_init(&arc_anon->arcs_locks[i].arcs_lock, 3814205231Skmacy NULL, MUTEX_DEFAULT, NULL); 3815205231Skmacy mutex_init(&arc_mru->arcs_locks[i].arcs_lock, 3816205231Skmacy NULL, MUTEX_DEFAULT, NULL); 3817205231Skmacy mutex_init(&arc_mru_ghost->arcs_locks[i].arcs_lock, 3818205231Skmacy NULL, MUTEX_DEFAULT, NULL); 3819205231Skmacy mutex_init(&arc_mfu->arcs_locks[i].arcs_lock, 3820205231Skmacy NULL, MUTEX_DEFAULT, NULL); 3821205231Skmacy mutex_init(&arc_mfu_ghost->arcs_locks[i].arcs_lock, 3822205231Skmacy NULL, MUTEX_DEFAULT, NULL); 3823205231Skmacy mutex_init(&arc_l2c_only->arcs_locks[i].arcs_lock, 3824205231Skmacy NULL, MUTEX_DEFAULT, NULL); 3825206796Spjd 3826205231Skmacy list_create(&arc_mru->arcs_lists[i], 3827205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3828205231Skmacy list_create(&arc_mru_ghost->arcs_lists[i], 3829205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3830205231Skmacy list_create(&arc_mfu->arcs_lists[i], 3831205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3832205231Skmacy list_create(&arc_mfu_ghost->arcs_lists[i], 3833205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3834205231Skmacy list_create(&arc_mfu_ghost->arcs_lists[i], 3835205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3836205231Skmacy list_create(&arc_l2c_only->arcs_lists[i], 3837205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3838205231Skmacy } 3839168404Spjd 3840168404Spjd buf_init(); 3841168404Spjd 3842168404Spjd arc_thread_exit = 0; 3843168404Spjd arc_eviction_list = NULL; 3844168404Spjd mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 3845168404Spjd bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 3846168404Spjd 3847168404Spjd arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 3848168404Spjd sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 3849168404Spjd 3850168404Spjd if (arc_ksp != NULL) { 3851168404Spjd arc_ksp->ks_data = &arc_stats; 3852168404Spjd kstat_install(arc_ksp); 3853168404Spjd } 3854168404Spjd 3855168404Spjd (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 3856168404Spjd TS_RUN, minclsyspri); 3857168404Spjd 3858168404Spjd#ifdef _KERNEL 3859168566Spjd arc_event_lowmem = EVENTHANDLER_REGISTER(vm_lowmem, arc_lowmem, NULL, 3860168404Spjd EVENTHANDLER_PRI_FIRST); 3861168404Spjd#endif 3862168404Spjd 3863168404Spjd arc_dead = FALSE; 3864185029Spjd arc_warm = B_FALSE; 3865168566Spjd 3866185029Spjd if (zfs_write_limit_max == 0) 3867185029Spjd zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift; 3868185029Spjd else 3869185029Spjd zfs_write_limit_shift = 0; 3870185029Spjd mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL); 3871185029Spjd 3872168566Spjd#ifdef _KERNEL 3873194043Skmacy if (TUNABLE_INT_FETCH("vfs.zfs.prefetch_disable", &zfs_prefetch_disable)) 3874193953Skmacy prefetch_tunable_set = 1; 3875206796Spjd 3876193878Skmacy#ifdef __i386__ 3877193953Skmacy if (prefetch_tunable_set == 0) { 3878196863Strasz printf("ZFS NOTICE: Prefetch is disabled by default on i386 " 3879196863Strasz "-- to enable,\n"); 3880196863Strasz printf(" add \"vfs.zfs.prefetch_disable=0\" " 3881196863Strasz "to /boot/loader.conf.\n"); 3882219089Spjd zfs_prefetch_disable = 1; 3883193878Skmacy } 3884206796Spjd#else 3885193878Skmacy if ((((uint64_t)physmem * PAGESIZE) < (1ULL << 32)) && 3886193953Skmacy prefetch_tunable_set == 0) { 3887196863Strasz printf("ZFS NOTICE: Prefetch is disabled by default if less " 3888196941Strasz "than 4GB of RAM is present;\n" 3889196863Strasz " to enable, add \"vfs.zfs.prefetch_disable=0\" " 3890196863Strasz "to /boot/loader.conf.\n"); 3891219089Spjd zfs_prefetch_disable = 1; 3892193878Skmacy } 3893206796Spjd#endif 3894175633Spjd /* Warn about ZFS memory and address space requirements. */ 3895168696Spjd if (((uint64_t)physmem * PAGESIZE) < (256 + 128 + 64) * (1 << 20)) { 3896168987Sbmah printf("ZFS WARNING: Recommended minimum RAM size is 512MB; " 3897168987Sbmah "expect unstable behavior.\n"); 3898175633Spjd } 3899175633Spjd if (kmem_size() < 512 * (1 << 20)) { 3900173419Spjd printf("ZFS WARNING: Recommended minimum kmem_size is 512MB; " 3901168987Sbmah "expect unstable behavior.\n"); 3902185029Spjd printf(" Consider tuning vm.kmem_size and " 3903173419Spjd "vm.kmem_size_max\n"); 3904185029Spjd printf(" in /boot/loader.conf.\n"); 3905168566Spjd } 3906168566Spjd#endif 3907168404Spjd} 3908168404Spjd 3909168404Spjdvoid 3910168404Spjdarc_fini(void) 3911168404Spjd{ 3912205231Skmacy int i; 3913206796Spjd 3914168404Spjd mutex_enter(&arc_reclaim_thr_lock); 3915168404Spjd arc_thread_exit = 1; 3916168404Spjd cv_signal(&arc_reclaim_thr_cv); 3917168404Spjd while (arc_thread_exit != 0) 3918168404Spjd cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 3919168404Spjd mutex_exit(&arc_reclaim_thr_lock); 3920168404Spjd 3921185029Spjd arc_flush(NULL); 3922168404Spjd 3923168404Spjd arc_dead = TRUE; 3924168404Spjd 3925168404Spjd if (arc_ksp != NULL) { 3926168404Spjd kstat_delete(arc_ksp); 3927168404Spjd arc_ksp = NULL; 3928168404Spjd } 3929168404Spjd 3930168404Spjd mutex_destroy(&arc_eviction_mtx); 3931168404Spjd mutex_destroy(&arc_reclaim_thr_lock); 3932168404Spjd cv_destroy(&arc_reclaim_thr_cv); 3933168404Spjd 3934205231Skmacy for (i = 0; i < ARC_BUFC_NUMLISTS; i++) { 3935205231Skmacy list_destroy(&arc_mru->arcs_lists[i]); 3936205231Skmacy list_destroy(&arc_mru_ghost->arcs_lists[i]); 3937205231Skmacy list_destroy(&arc_mfu->arcs_lists[i]); 3938205231Skmacy list_destroy(&arc_mfu_ghost->arcs_lists[i]); 3939206795Spjd list_destroy(&arc_l2c_only->arcs_lists[i]); 3940168404Spjd 3941205231Skmacy mutex_destroy(&arc_anon->arcs_locks[i].arcs_lock); 3942205231Skmacy mutex_destroy(&arc_mru->arcs_locks[i].arcs_lock); 3943205231Skmacy mutex_destroy(&arc_mru_ghost->arcs_locks[i].arcs_lock); 3944205231Skmacy mutex_destroy(&arc_mfu->arcs_locks[i].arcs_lock); 3945205231Skmacy mutex_destroy(&arc_mfu_ghost->arcs_locks[i].arcs_lock); 3946206795Spjd mutex_destroy(&arc_l2c_only->arcs_locks[i].arcs_lock); 3947205231Skmacy } 3948206796Spjd 3949185029Spjd mutex_destroy(&zfs_write_limit_lock); 3950185029Spjd 3951168404Spjd buf_fini(); 3952168404Spjd 3953209962Smm ASSERT(arc_loaned_bytes == 0); 3954209962Smm 3955168582Spjd mutex_destroy(&arc_lowmem_lock); 3956168404Spjd#ifdef _KERNEL 3957168566Spjd if (arc_event_lowmem != NULL) 3958168566Spjd EVENTHANDLER_DEREGISTER(vm_lowmem, arc_event_lowmem); 3959168404Spjd#endif 3960168404Spjd} 3961185029Spjd 3962185029Spjd/* 3963185029Spjd * Level 2 ARC 3964185029Spjd * 3965185029Spjd * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 3966185029Spjd * It uses dedicated storage devices to hold cached data, which are populated 3967185029Spjd * using large infrequent writes. The main role of this cache is to boost 3968185029Spjd * the performance of random read workloads. The intended L2ARC devices 3969185029Spjd * include short-stroked disks, solid state disks, and other media with 3970185029Spjd * substantially faster read latency than disk. 3971185029Spjd * 3972185029Spjd * +-----------------------+ 3973185029Spjd * | ARC | 3974185029Spjd * +-----------------------+ 3975185029Spjd * | ^ ^ 3976185029Spjd * | | | 3977185029Spjd * l2arc_feed_thread() arc_read() 3978185029Spjd * | | | 3979185029Spjd * | l2arc read | 3980185029Spjd * V | | 3981185029Spjd * +---------------+ | 3982185029Spjd * | L2ARC | | 3983185029Spjd * +---------------+ | 3984185029Spjd * | ^ | 3985185029Spjd * l2arc_write() | | 3986185029Spjd * | | | 3987185029Spjd * V | | 3988185029Spjd * +-------+ +-------+ 3989185029Spjd * | vdev | | vdev | 3990185029Spjd * | cache | | cache | 3991185029Spjd * +-------+ +-------+ 3992185029Spjd * +=========+ .-----. 3993185029Spjd * : L2ARC : |-_____-| 3994185029Spjd * : devices : | Disks | 3995185029Spjd * +=========+ `-_____-' 3996185029Spjd * 3997185029Spjd * Read requests are satisfied from the following sources, in order: 3998185029Spjd * 3999185029Spjd * 1) ARC 4000185029Spjd * 2) vdev cache of L2ARC devices 4001185029Spjd * 3) L2ARC devices 4002185029Spjd * 4) vdev cache of disks 4003185029Spjd * 5) disks 4004185029Spjd * 4005185029Spjd * Some L2ARC device types exhibit extremely slow write performance. 4006185029Spjd * To accommodate for this there are some significant differences between 4007185029Spjd * the L2ARC and traditional cache design: 4008185029Spjd * 4009185029Spjd * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 4010185029Spjd * the ARC behave as usual, freeing buffers and placing headers on ghost 4011185029Spjd * lists. The ARC does not send buffers to the L2ARC during eviction as 4012185029Spjd * this would add inflated write latencies for all ARC memory pressure. 4013185029Spjd * 4014185029Spjd * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 4015185029Spjd * It does this by periodically scanning buffers from the eviction-end of 4016185029Spjd * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 4017185029Spjd * not already there. It scans until a headroom of buffers is satisfied, 4018185029Spjd * which itself is a buffer for ARC eviction. The thread that does this is 4019185029Spjd * l2arc_feed_thread(), illustrated below; example sizes are included to 4020185029Spjd * provide a better sense of ratio than this diagram: 4021185029Spjd * 4022185029Spjd * head --> tail 4023185029Spjd * +---------------------+----------+ 4024185029Spjd * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 4025185029Spjd * +---------------------+----------+ | o L2ARC eligible 4026185029Spjd * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 4027185029Spjd * +---------------------+----------+ | 4028185029Spjd * 15.9 Gbytes ^ 32 Mbytes | 4029185029Spjd * headroom | 4030185029Spjd * l2arc_feed_thread() 4031185029Spjd * | 4032185029Spjd * l2arc write hand <--[oooo]--' 4033185029Spjd * | 8 Mbyte 4034185029Spjd * | write max 4035185029Spjd * V 4036185029Spjd * +==============================+ 4037185029Spjd * L2ARC dev |####|#|###|###| |####| ... | 4038185029Spjd * +==============================+ 4039185029Spjd * 32 Gbytes 4040185029Spjd * 4041185029Spjd * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 4042185029Spjd * evicted, then the L2ARC has cached a buffer much sooner than it probably 4043185029Spjd * needed to, potentially wasting L2ARC device bandwidth and storage. It is 4044185029Spjd * safe to say that this is an uncommon case, since buffers at the end of 4045185029Spjd * the ARC lists have moved there due to inactivity. 4046185029Spjd * 4047185029Spjd * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 4048185029Spjd * then the L2ARC simply misses copying some buffers. This serves as a 4049185029Spjd * pressure valve to prevent heavy read workloads from both stalling the ARC 4050185029Spjd * with waits and clogging the L2ARC with writes. This also helps prevent 4051185029Spjd * the potential for the L2ARC to churn if it attempts to cache content too 4052185029Spjd * quickly, such as during backups of the entire pool. 4053185029Spjd * 4054185029Spjd * 5. After system boot and before the ARC has filled main memory, there are 4055185029Spjd * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 4056185029Spjd * lists can remain mostly static. Instead of searching from tail of these 4057185029Spjd * lists as pictured, the l2arc_feed_thread() will search from the list heads 4058185029Spjd * for eligible buffers, greatly increasing its chance of finding them. 4059185029Spjd * 4060185029Spjd * The L2ARC device write speed is also boosted during this time so that 4061185029Spjd * the L2ARC warms up faster. Since there have been no ARC evictions yet, 4062185029Spjd * there are no L2ARC reads, and no fear of degrading read performance 4063185029Spjd * through increased writes. 4064185029Spjd * 4065185029Spjd * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 4066185029Spjd * the vdev queue can aggregate them into larger and fewer writes. Each 4067185029Spjd * device is written to in a rotor fashion, sweeping writes through 4068185029Spjd * available space then repeating. 4069185029Spjd * 4070185029Spjd * 7. The L2ARC does not store dirty content. It never needs to flush 4071185029Spjd * write buffers back to disk based storage. 4072185029Spjd * 4073185029Spjd * 8. If an ARC buffer is written (and dirtied) which also exists in the 4074185029Spjd * L2ARC, the now stale L2ARC buffer is immediately dropped. 4075185029Spjd * 4076185029Spjd * The performance of the L2ARC can be tweaked by a number of tunables, which 4077185029Spjd * may be necessary for different workloads: 4078185029Spjd * 4079185029Spjd * l2arc_write_max max write bytes per interval 4080185029Spjd * l2arc_write_boost extra write bytes during device warmup 4081185029Spjd * l2arc_noprefetch skip caching prefetched buffers 4082185029Spjd * l2arc_headroom number of max device writes to precache 4083185029Spjd * l2arc_feed_secs seconds between L2ARC writing 4084185029Spjd * 4085185029Spjd * Tunables may be removed or added as future performance improvements are 4086185029Spjd * integrated, and also may become zpool properties. 4087208373Smm * 4088208373Smm * There are three key functions that control how the L2ARC warms up: 4089208373Smm * 4090208373Smm * l2arc_write_eligible() check if a buffer is eligible to cache 4091208373Smm * l2arc_write_size() calculate how much to write 4092208373Smm * l2arc_write_interval() calculate sleep delay between writes 4093208373Smm * 4094208373Smm * These three functions determine what to write, how much, and how quickly 4095208373Smm * to send writes. 4096185029Spjd */ 4097185029Spjd 4098208373Smmstatic boolean_t 4099209962Smml2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab) 4100208373Smm{ 4101208373Smm /* 4102208373Smm * A buffer is *not* eligible for the L2ARC if it: 4103208373Smm * 1. belongs to a different spa. 4104208373Smm * 2. is already cached on the L2ARC. 4105208373Smm * 3. has an I/O in progress (it may be an incomplete read). 4106208373Smm * 4. is flagged not eligible (zfs property). 4107208373Smm */ 4108209962Smm if (ab->b_spa != spa_guid) { 4109208373Smm ARCSTAT_BUMP(arcstat_l2_write_spa_mismatch); 4110208373Smm return (B_FALSE); 4111208373Smm } 4112208373Smm if (ab->b_l2hdr != NULL) { 4113208373Smm ARCSTAT_BUMP(arcstat_l2_write_in_l2); 4114208373Smm return (B_FALSE); 4115208373Smm } 4116208373Smm if (HDR_IO_IN_PROGRESS(ab)) { 4117208373Smm ARCSTAT_BUMP(arcstat_l2_write_hdr_io_in_progress); 4118208373Smm return (B_FALSE); 4119208373Smm } 4120208373Smm if (!HDR_L2CACHE(ab)) { 4121208373Smm ARCSTAT_BUMP(arcstat_l2_write_not_cacheable); 4122208373Smm return (B_FALSE); 4123208373Smm } 4124208373Smm 4125208373Smm return (B_TRUE); 4126208373Smm} 4127208373Smm 4128208373Smmstatic uint64_t 4129208373Smml2arc_write_size(l2arc_dev_t *dev) 4130208373Smm{ 4131208373Smm uint64_t size; 4132208373Smm 4133208373Smm size = dev->l2ad_write; 4134208373Smm 4135208373Smm if (arc_warm == B_FALSE) 4136208373Smm size += dev->l2ad_boost; 4137208373Smm 4138208373Smm return (size); 4139208373Smm 4140208373Smm} 4141208373Smm 4142208373Smmstatic clock_t 4143208373Smml2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 4144208373Smm{ 4145219089Spjd clock_t interval, next, now; 4146208373Smm 4147208373Smm /* 4148208373Smm * If the ARC lists are busy, increase our write rate; if the 4149208373Smm * lists are stale, idle back. This is achieved by checking 4150208373Smm * how much we previously wrote - if it was more than half of 4151208373Smm * what we wanted, schedule the next write much sooner. 4152208373Smm */ 4153208373Smm if (l2arc_feed_again && wrote > (wanted / 2)) 4154208373Smm interval = (hz * l2arc_feed_min_ms) / 1000; 4155208373Smm else 4156208373Smm interval = hz * l2arc_feed_secs; 4157208373Smm 4158219089Spjd now = ddi_get_lbolt(); 4159219089Spjd next = MAX(now, MIN(now + interval, began + interval)); 4160208373Smm 4161208373Smm return (next); 4162208373Smm} 4163208373Smm 4164185029Spjdstatic void 4165185029Spjdl2arc_hdr_stat_add(void) 4166185029Spjd{ 4167185029Spjd ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE); 4168185029Spjd ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE); 4169185029Spjd} 4170185029Spjd 4171185029Spjdstatic void 4172185029Spjdl2arc_hdr_stat_remove(void) 4173185029Spjd{ 4174185029Spjd ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE)); 4175185029Spjd ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE); 4176185029Spjd} 4177185029Spjd 4178185029Spjd/* 4179185029Spjd * Cycle through L2ARC devices. This is how L2ARC load balances. 4180185029Spjd * If a device is returned, this also returns holding the spa config lock. 4181185029Spjd */ 4182185029Spjdstatic l2arc_dev_t * 4183185029Spjdl2arc_dev_get_next(void) 4184185029Spjd{ 4185185029Spjd l2arc_dev_t *first, *next = NULL; 4186185029Spjd 4187185029Spjd /* 4188185029Spjd * Lock out the removal of spas (spa_namespace_lock), then removal 4189185029Spjd * of cache devices (l2arc_dev_mtx). Once a device has been selected, 4190185029Spjd * both locks will be dropped and a spa config lock held instead. 4191185029Spjd */ 4192185029Spjd mutex_enter(&spa_namespace_lock); 4193185029Spjd mutex_enter(&l2arc_dev_mtx); 4194185029Spjd 4195185029Spjd /* if there are no vdevs, there is nothing to do */ 4196185029Spjd if (l2arc_ndev == 0) 4197185029Spjd goto out; 4198185029Spjd 4199185029Spjd first = NULL; 4200185029Spjd next = l2arc_dev_last; 4201185029Spjd do { 4202185029Spjd /* loop around the list looking for a non-faulted vdev */ 4203185029Spjd if (next == NULL) { 4204185029Spjd next = list_head(l2arc_dev_list); 4205185029Spjd } else { 4206185029Spjd next = list_next(l2arc_dev_list, next); 4207185029Spjd if (next == NULL) 4208185029Spjd next = list_head(l2arc_dev_list); 4209185029Spjd } 4210185029Spjd 4211185029Spjd /* if we have come back to the start, bail out */ 4212185029Spjd if (first == NULL) 4213185029Spjd first = next; 4214185029Spjd else if (next == first) 4215185029Spjd break; 4216185029Spjd 4217185029Spjd } while (vdev_is_dead(next->l2ad_vdev)); 4218185029Spjd 4219185029Spjd /* if we were unable to find any usable vdevs, return NULL */ 4220185029Spjd if (vdev_is_dead(next->l2ad_vdev)) 4221185029Spjd next = NULL; 4222185029Spjd 4223185029Spjd l2arc_dev_last = next; 4224185029Spjd 4225185029Spjdout: 4226185029Spjd mutex_exit(&l2arc_dev_mtx); 4227185029Spjd 4228185029Spjd /* 4229185029Spjd * Grab the config lock to prevent the 'next' device from being 4230185029Spjd * removed while we are writing to it. 4231185029Spjd */ 4232185029Spjd if (next != NULL) 4233185029Spjd spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 4234185029Spjd mutex_exit(&spa_namespace_lock); 4235185029Spjd 4236185029Spjd return (next); 4237185029Spjd} 4238185029Spjd 4239185029Spjd/* 4240185029Spjd * Free buffers that were tagged for destruction. 4241185029Spjd */ 4242185029Spjdstatic void 4243185029Spjdl2arc_do_free_on_write() 4244185029Spjd{ 4245185029Spjd list_t *buflist; 4246185029Spjd l2arc_data_free_t *df, *df_prev; 4247185029Spjd 4248185029Spjd mutex_enter(&l2arc_free_on_write_mtx); 4249185029Spjd buflist = l2arc_free_on_write; 4250185029Spjd 4251185029Spjd for (df = list_tail(buflist); df; df = df_prev) { 4252185029Spjd df_prev = list_prev(buflist, df); 4253185029Spjd ASSERT(df->l2df_data != NULL); 4254185029Spjd ASSERT(df->l2df_func != NULL); 4255185029Spjd df->l2df_func(df->l2df_data, df->l2df_size); 4256185029Spjd list_remove(buflist, df); 4257185029Spjd kmem_free(df, sizeof (l2arc_data_free_t)); 4258185029Spjd } 4259185029Spjd 4260185029Spjd mutex_exit(&l2arc_free_on_write_mtx); 4261185029Spjd} 4262185029Spjd 4263185029Spjd/* 4264185029Spjd * A write to a cache device has completed. Update all headers to allow 4265185029Spjd * reads from these buffers to begin. 4266185029Spjd */ 4267185029Spjdstatic void 4268185029Spjdl2arc_write_done(zio_t *zio) 4269185029Spjd{ 4270185029Spjd l2arc_write_callback_t *cb; 4271185029Spjd l2arc_dev_t *dev; 4272185029Spjd list_t *buflist; 4273185029Spjd arc_buf_hdr_t *head, *ab, *ab_prev; 4274185029Spjd l2arc_buf_hdr_t *abl2; 4275185029Spjd kmutex_t *hash_lock; 4276185029Spjd 4277185029Spjd cb = zio->io_private; 4278185029Spjd ASSERT(cb != NULL); 4279185029Spjd dev = cb->l2wcb_dev; 4280185029Spjd ASSERT(dev != NULL); 4281185029Spjd head = cb->l2wcb_head; 4282185029Spjd ASSERT(head != NULL); 4283185029Spjd buflist = dev->l2ad_buflist; 4284185029Spjd ASSERT(buflist != NULL); 4285185029Spjd DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 4286185029Spjd l2arc_write_callback_t *, cb); 4287185029Spjd 4288185029Spjd if (zio->io_error != 0) 4289185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_error); 4290185029Spjd 4291185029Spjd mutex_enter(&l2arc_buflist_mtx); 4292185029Spjd 4293185029Spjd /* 4294185029Spjd * All writes completed, or an error was hit. 4295185029Spjd */ 4296185029Spjd for (ab = list_prev(buflist, head); ab; ab = ab_prev) { 4297185029Spjd ab_prev = list_prev(buflist, ab); 4298185029Spjd 4299185029Spjd hash_lock = HDR_LOCK(ab); 4300185029Spjd if (!mutex_tryenter(hash_lock)) { 4301185029Spjd /* 4302185029Spjd * This buffer misses out. It may be in a stage 4303185029Spjd * of eviction. Its ARC_L2_WRITING flag will be 4304185029Spjd * left set, denying reads to this buffer. 4305185029Spjd */ 4306185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss); 4307185029Spjd continue; 4308185029Spjd } 4309185029Spjd 4310185029Spjd if (zio->io_error != 0) { 4311185029Spjd /* 4312185029Spjd * Error - drop L2ARC entry. 4313185029Spjd */ 4314185029Spjd list_remove(buflist, ab); 4315185029Spjd abl2 = ab->b_l2hdr; 4316185029Spjd ab->b_l2hdr = NULL; 4317185029Spjd kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 4318185029Spjd ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 4319185029Spjd } 4320185029Spjd 4321185029Spjd /* 4322185029Spjd * Allow ARC to begin reads to this L2ARC entry. 4323185029Spjd */ 4324185029Spjd ab->b_flags &= ~ARC_L2_WRITING; 4325185029Spjd 4326185029Spjd mutex_exit(hash_lock); 4327185029Spjd } 4328185029Spjd 4329185029Spjd atomic_inc_64(&l2arc_writes_done); 4330185029Spjd list_remove(buflist, head); 4331185029Spjd kmem_cache_free(hdr_cache, head); 4332185029Spjd mutex_exit(&l2arc_buflist_mtx); 4333185029Spjd 4334185029Spjd l2arc_do_free_on_write(); 4335185029Spjd 4336185029Spjd kmem_free(cb, sizeof (l2arc_write_callback_t)); 4337185029Spjd} 4338185029Spjd 4339185029Spjd/* 4340185029Spjd * A read to a cache device completed. Validate buffer contents before 4341185029Spjd * handing over to the regular ARC routines. 4342185029Spjd */ 4343185029Spjdstatic void 4344185029Spjdl2arc_read_done(zio_t *zio) 4345185029Spjd{ 4346185029Spjd l2arc_read_callback_t *cb; 4347185029Spjd arc_buf_hdr_t *hdr; 4348185029Spjd arc_buf_t *buf; 4349185029Spjd kmutex_t *hash_lock; 4350185029Spjd int equal; 4351185029Spjd 4352185029Spjd ASSERT(zio->io_vd != NULL); 4353185029Spjd ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 4354185029Spjd 4355185029Spjd spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 4356185029Spjd 4357185029Spjd cb = zio->io_private; 4358185029Spjd ASSERT(cb != NULL); 4359185029Spjd buf = cb->l2rcb_buf; 4360185029Spjd ASSERT(buf != NULL); 4361185029Spjd 4362219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 4363185029Spjd mutex_enter(hash_lock); 4364219089Spjd hdr = buf->b_hdr; 4365219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 4366185029Spjd 4367185029Spjd /* 4368185029Spjd * Check this survived the L2ARC journey. 4369185029Spjd */ 4370185029Spjd equal = arc_cksum_equal(buf); 4371185029Spjd if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 4372185029Spjd mutex_exit(hash_lock); 4373185029Spjd zio->io_private = buf; 4374185029Spjd zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 4375185029Spjd zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 4376185029Spjd arc_read_done(zio); 4377185029Spjd } else { 4378185029Spjd mutex_exit(hash_lock); 4379185029Spjd /* 4380185029Spjd * Buffer didn't survive caching. Increment stats and 4381185029Spjd * reissue to the original storage device. 4382185029Spjd */ 4383185029Spjd if (zio->io_error != 0) { 4384185029Spjd ARCSTAT_BUMP(arcstat_l2_io_error); 4385185029Spjd } else { 4386185029Spjd zio->io_error = EIO; 4387185029Spjd } 4388185029Spjd if (!equal) 4389185029Spjd ARCSTAT_BUMP(arcstat_l2_cksum_bad); 4390185029Spjd 4391185029Spjd /* 4392185029Spjd * If there's no waiter, issue an async i/o to the primary 4393185029Spjd * storage now. If there *is* a waiter, the caller must 4394185029Spjd * issue the i/o in a context where it's OK to block. 4395185029Spjd */ 4396209962Smm if (zio->io_waiter == NULL) { 4397209962Smm zio_t *pio = zio_unique_parent(zio); 4398209962Smm 4399209962Smm ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 4400209962Smm 4401209962Smm zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp, 4402185029Spjd buf->b_data, zio->io_size, arc_read_done, buf, 4403185029Spjd zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb)); 4404209962Smm } 4405185029Spjd } 4406185029Spjd 4407185029Spjd kmem_free(cb, sizeof (l2arc_read_callback_t)); 4408185029Spjd} 4409185029Spjd 4410185029Spjd/* 4411185029Spjd * This is the list priority from which the L2ARC will search for pages to 4412185029Spjd * cache. This is used within loops (0..3) to cycle through lists in the 4413185029Spjd * desired order. This order can have a significant effect on cache 4414185029Spjd * performance. 4415185029Spjd * 4416185029Spjd * Currently the metadata lists are hit first, MFU then MRU, followed by 4417185029Spjd * the data lists. This function returns a locked list, and also returns 4418185029Spjd * the lock pointer. 4419185029Spjd */ 4420185029Spjdstatic list_t * 4421185029Spjdl2arc_list_locked(int list_num, kmutex_t **lock) 4422185029Spjd{ 4423185029Spjd list_t *list; 4424205231Skmacy int idx; 4425185029Spjd 4426206796Spjd ASSERT(list_num >= 0 && list_num < 2 * ARC_BUFC_NUMLISTS); 4427206796Spjd 4428205231Skmacy if (list_num < ARC_BUFC_NUMMETADATALISTS) { 4429205231Skmacy idx = list_num; 4430205231Skmacy list = &arc_mfu->arcs_lists[idx]; 4431205231Skmacy *lock = ARCS_LOCK(arc_mfu, idx); 4432206796Spjd } else if (list_num < ARC_BUFC_NUMMETADATALISTS * 2) { 4433205231Skmacy idx = list_num - ARC_BUFC_NUMMETADATALISTS; 4434205231Skmacy list = &arc_mru->arcs_lists[idx]; 4435205231Skmacy *lock = ARCS_LOCK(arc_mru, idx); 4436206796Spjd } else if (list_num < (ARC_BUFC_NUMMETADATALISTS * 2 + 4437205231Skmacy ARC_BUFC_NUMDATALISTS)) { 4438205231Skmacy idx = list_num - ARC_BUFC_NUMMETADATALISTS; 4439205231Skmacy list = &arc_mfu->arcs_lists[idx]; 4440205231Skmacy *lock = ARCS_LOCK(arc_mfu, idx); 4441205231Skmacy } else { 4442205231Skmacy idx = list_num - ARC_BUFC_NUMLISTS; 4443205231Skmacy list = &arc_mru->arcs_lists[idx]; 4444205231Skmacy *lock = ARCS_LOCK(arc_mru, idx); 4445185029Spjd } 4446185029Spjd 4447185029Spjd ASSERT(!(MUTEX_HELD(*lock))); 4448185029Spjd mutex_enter(*lock); 4449185029Spjd return (list); 4450185029Spjd} 4451185029Spjd 4452185029Spjd/* 4453185029Spjd * Evict buffers from the device write hand to the distance specified in 4454185029Spjd * bytes. This distance may span populated buffers, it may span nothing. 4455185029Spjd * This is clearing a region on the L2ARC device ready for writing. 4456185029Spjd * If the 'all' boolean is set, every buffer is evicted. 4457185029Spjd */ 4458185029Spjdstatic void 4459185029Spjdl2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 4460185029Spjd{ 4461185029Spjd list_t *buflist; 4462185029Spjd l2arc_buf_hdr_t *abl2; 4463185029Spjd arc_buf_hdr_t *ab, *ab_prev; 4464185029Spjd kmutex_t *hash_lock; 4465185029Spjd uint64_t taddr; 4466185029Spjd 4467185029Spjd buflist = dev->l2ad_buflist; 4468185029Spjd 4469185029Spjd if (buflist == NULL) 4470185029Spjd return; 4471185029Spjd 4472185029Spjd if (!all && dev->l2ad_first) { 4473185029Spjd /* 4474185029Spjd * This is the first sweep through the device. There is 4475185029Spjd * nothing to evict. 4476185029Spjd */ 4477185029Spjd return; 4478185029Spjd } 4479185029Spjd 4480185029Spjd if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 4481185029Spjd /* 4482185029Spjd * When nearing the end of the device, evict to the end 4483185029Spjd * before the device write hand jumps to the start. 4484185029Spjd */ 4485185029Spjd taddr = dev->l2ad_end; 4486185029Spjd } else { 4487185029Spjd taddr = dev->l2ad_hand + distance; 4488185029Spjd } 4489185029Spjd DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 4490185029Spjd uint64_t, taddr, boolean_t, all); 4491185029Spjd 4492185029Spjdtop: 4493185029Spjd mutex_enter(&l2arc_buflist_mtx); 4494185029Spjd for (ab = list_tail(buflist); ab; ab = ab_prev) { 4495185029Spjd ab_prev = list_prev(buflist, ab); 4496185029Spjd 4497185029Spjd hash_lock = HDR_LOCK(ab); 4498185029Spjd if (!mutex_tryenter(hash_lock)) { 4499185029Spjd /* 4500185029Spjd * Missed the hash lock. Retry. 4501185029Spjd */ 4502185029Spjd ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 4503185029Spjd mutex_exit(&l2arc_buflist_mtx); 4504185029Spjd mutex_enter(hash_lock); 4505185029Spjd mutex_exit(hash_lock); 4506185029Spjd goto top; 4507185029Spjd } 4508185029Spjd 4509185029Spjd if (HDR_L2_WRITE_HEAD(ab)) { 4510185029Spjd /* 4511185029Spjd * We hit a write head node. Leave it for 4512185029Spjd * l2arc_write_done(). 4513185029Spjd */ 4514185029Spjd list_remove(buflist, ab); 4515185029Spjd mutex_exit(hash_lock); 4516185029Spjd continue; 4517185029Spjd } 4518185029Spjd 4519185029Spjd if (!all && ab->b_l2hdr != NULL && 4520185029Spjd (ab->b_l2hdr->b_daddr > taddr || 4521185029Spjd ab->b_l2hdr->b_daddr < dev->l2ad_hand)) { 4522185029Spjd /* 4523185029Spjd * We've evicted to the target address, 4524185029Spjd * or the end of the device. 4525185029Spjd */ 4526185029Spjd mutex_exit(hash_lock); 4527185029Spjd break; 4528185029Spjd } 4529185029Spjd 4530185029Spjd if (HDR_FREE_IN_PROGRESS(ab)) { 4531185029Spjd /* 4532185029Spjd * Already on the path to destruction. 4533185029Spjd */ 4534185029Spjd mutex_exit(hash_lock); 4535185029Spjd continue; 4536185029Spjd } 4537185029Spjd 4538185029Spjd if (ab->b_state == arc_l2c_only) { 4539185029Spjd ASSERT(!HDR_L2_READING(ab)); 4540185029Spjd /* 4541185029Spjd * This doesn't exist in the ARC. Destroy. 4542185029Spjd * arc_hdr_destroy() will call list_remove() 4543185029Spjd * and decrement arcstat_l2_size. 4544185029Spjd */ 4545185029Spjd arc_change_state(arc_anon, ab, hash_lock); 4546185029Spjd arc_hdr_destroy(ab); 4547185029Spjd } else { 4548185029Spjd /* 4549185029Spjd * Invalidate issued or about to be issued 4550185029Spjd * reads, since we may be about to write 4551185029Spjd * over this location. 4552185029Spjd */ 4553185029Spjd if (HDR_L2_READING(ab)) { 4554185029Spjd ARCSTAT_BUMP(arcstat_l2_evict_reading); 4555185029Spjd ab->b_flags |= ARC_L2_EVICTED; 4556185029Spjd } 4557185029Spjd 4558185029Spjd /* 4559185029Spjd * Tell ARC this no longer exists in L2ARC. 4560185029Spjd */ 4561185029Spjd if (ab->b_l2hdr != NULL) { 4562185029Spjd abl2 = ab->b_l2hdr; 4563185029Spjd ab->b_l2hdr = NULL; 4564185029Spjd kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 4565185029Spjd ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 4566185029Spjd } 4567185029Spjd list_remove(buflist, ab); 4568185029Spjd 4569185029Spjd /* 4570185029Spjd * This may have been leftover after a 4571185029Spjd * failed write. 4572185029Spjd */ 4573185029Spjd ab->b_flags &= ~ARC_L2_WRITING; 4574185029Spjd } 4575185029Spjd mutex_exit(hash_lock); 4576185029Spjd } 4577185029Spjd mutex_exit(&l2arc_buflist_mtx); 4578185029Spjd 4579219089Spjd vdev_space_update(dev->l2ad_vdev, -(taddr - dev->l2ad_evict), 0, 0); 4580185029Spjd dev->l2ad_evict = taddr; 4581185029Spjd} 4582185029Spjd 4583185029Spjd/* 4584185029Spjd * Find and write ARC buffers to the L2ARC device. 4585185029Spjd * 4586185029Spjd * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid 4587185029Spjd * for reading until they have completed writing. 4588185029Spjd */ 4589208373Smmstatic uint64_t 4590185029Spjdl2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) 4591185029Spjd{ 4592185029Spjd arc_buf_hdr_t *ab, *ab_prev, *head; 4593185029Spjd l2arc_buf_hdr_t *hdrl2; 4594185029Spjd list_t *list; 4595185029Spjd uint64_t passed_sz, write_sz, buf_sz, headroom; 4596185029Spjd void *buf_data; 4597185029Spjd kmutex_t *hash_lock, *list_lock; 4598185029Spjd boolean_t have_lock, full; 4599185029Spjd l2arc_write_callback_t *cb; 4600185029Spjd zio_t *pio, *wzio; 4601228103Smm uint64_t guid = spa_load_guid(spa); 4602185029Spjd int try; 4603185029Spjd 4604185029Spjd ASSERT(dev->l2ad_vdev != NULL); 4605185029Spjd 4606185029Spjd pio = NULL; 4607185029Spjd write_sz = 0; 4608185029Spjd full = B_FALSE; 4609185029Spjd head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 4610185029Spjd head->b_flags |= ARC_L2_WRITE_HEAD; 4611185029Spjd 4612205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_iter); 4613185029Spjd /* 4614185029Spjd * Copy buffers for L2ARC writing. 4615185029Spjd */ 4616185029Spjd mutex_enter(&l2arc_buflist_mtx); 4617206796Spjd for (try = 0; try < 2 * ARC_BUFC_NUMLISTS; try++) { 4618185029Spjd list = l2arc_list_locked(try, &list_lock); 4619185029Spjd passed_sz = 0; 4620205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_list_iter); 4621185029Spjd 4622185029Spjd /* 4623185029Spjd * L2ARC fast warmup. 4624185029Spjd * 4625185029Spjd * Until the ARC is warm and starts to evict, read from the 4626185029Spjd * head of the ARC lists rather than the tail. 4627185029Spjd */ 4628185029Spjd headroom = target_sz * l2arc_headroom; 4629185029Spjd if (arc_warm == B_FALSE) 4630185029Spjd ab = list_head(list); 4631185029Spjd else 4632185029Spjd ab = list_tail(list); 4633206796Spjd if (ab == NULL) 4634205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_list_null_iter); 4635185029Spjd 4636185029Spjd for (; ab; ab = ab_prev) { 4637185029Spjd if (arc_warm == B_FALSE) 4638185029Spjd ab_prev = list_next(list, ab); 4639185029Spjd else 4640185029Spjd ab_prev = list_prev(list, ab); 4641205231Skmacy ARCSTAT_INCR(arcstat_l2_write_buffer_bytes_scanned, ab->b_size); 4642206796Spjd 4643185029Spjd hash_lock = HDR_LOCK(ab); 4644185029Spjd have_lock = MUTEX_HELD(hash_lock); 4645185029Spjd if (!have_lock && !mutex_tryenter(hash_lock)) { 4646205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_trylock_fail); 4647185029Spjd /* 4648185029Spjd * Skip this buffer rather than waiting. 4649185029Spjd */ 4650185029Spjd continue; 4651185029Spjd } 4652185029Spjd 4653185029Spjd passed_sz += ab->b_size; 4654185029Spjd if (passed_sz > headroom) { 4655185029Spjd /* 4656185029Spjd * Searched too far. 4657185029Spjd */ 4658185029Spjd mutex_exit(hash_lock); 4659205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_passed_headroom); 4660185029Spjd break; 4661185029Spjd } 4662185029Spjd 4663209962Smm if (!l2arc_write_eligible(guid, ab)) { 4664185029Spjd mutex_exit(hash_lock); 4665185029Spjd continue; 4666185029Spjd } 4667185029Spjd 4668185029Spjd if ((write_sz + ab->b_size) > target_sz) { 4669185029Spjd full = B_TRUE; 4670185029Spjd mutex_exit(hash_lock); 4671205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_full); 4672185029Spjd break; 4673185029Spjd } 4674185029Spjd 4675185029Spjd if (pio == NULL) { 4676185029Spjd /* 4677185029Spjd * Insert a dummy header on the buflist so 4678185029Spjd * l2arc_write_done() can find where the 4679185029Spjd * write buffers begin without searching. 4680185029Spjd */ 4681185029Spjd list_insert_head(dev->l2ad_buflist, head); 4682185029Spjd 4683185029Spjd cb = kmem_alloc( 4684185029Spjd sizeof (l2arc_write_callback_t), KM_SLEEP); 4685185029Spjd cb->l2wcb_dev = dev; 4686185029Spjd cb->l2wcb_head = head; 4687185029Spjd pio = zio_root(spa, l2arc_write_done, cb, 4688185029Spjd ZIO_FLAG_CANFAIL); 4689205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_pios); 4690185029Spjd } 4691185029Spjd 4692185029Spjd /* 4693185029Spjd * Create and add a new L2ARC header. 4694185029Spjd */ 4695185029Spjd hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP); 4696185029Spjd hdrl2->b_dev = dev; 4697185029Spjd hdrl2->b_daddr = dev->l2ad_hand; 4698185029Spjd 4699206792Spjd ab->b_flags |= ARC_L2_WRITING; 4700185029Spjd ab->b_l2hdr = hdrl2; 4701185029Spjd list_insert_head(dev->l2ad_buflist, ab); 4702185029Spjd buf_data = ab->b_buf->b_data; 4703185029Spjd buf_sz = ab->b_size; 4704185029Spjd 4705185029Spjd /* 4706185029Spjd * Compute and store the buffer cksum before 4707185029Spjd * writing. On debug the cksum is verified first. 4708185029Spjd */ 4709185029Spjd arc_cksum_verify(ab->b_buf); 4710185029Spjd arc_cksum_compute(ab->b_buf, B_TRUE); 4711185029Spjd 4712185029Spjd mutex_exit(hash_lock); 4713185029Spjd 4714185029Spjd wzio = zio_write_phys(pio, dev->l2ad_vdev, 4715185029Spjd dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 4716185029Spjd NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 4717185029Spjd ZIO_FLAG_CANFAIL, B_FALSE); 4718185029Spjd 4719185029Spjd DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 4720185029Spjd zio_t *, wzio); 4721185029Spjd (void) zio_nowait(wzio); 4722185029Spjd 4723185029Spjd /* 4724185029Spjd * Keep the clock hand suitably device-aligned. 4725185029Spjd */ 4726185029Spjd buf_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 4727185029Spjd 4728185029Spjd write_sz += buf_sz; 4729185029Spjd dev->l2ad_hand += buf_sz; 4730185029Spjd } 4731185029Spjd 4732185029Spjd mutex_exit(list_lock); 4733185029Spjd 4734185029Spjd if (full == B_TRUE) 4735185029Spjd break; 4736185029Spjd } 4737185029Spjd mutex_exit(&l2arc_buflist_mtx); 4738185029Spjd 4739185029Spjd if (pio == NULL) { 4740185029Spjd ASSERT3U(write_sz, ==, 0); 4741185029Spjd kmem_cache_free(hdr_cache, head); 4742208373Smm return (0); 4743185029Spjd } 4744185029Spjd 4745185029Spjd ASSERT3U(write_sz, <=, target_sz); 4746185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_sent); 4747208373Smm ARCSTAT_INCR(arcstat_l2_write_bytes, write_sz); 4748185029Spjd ARCSTAT_INCR(arcstat_l2_size, write_sz); 4749219089Spjd vdev_space_update(dev->l2ad_vdev, write_sz, 0, 0); 4750185029Spjd 4751185029Spjd /* 4752185029Spjd * Bump device hand to the device start if it is approaching the end. 4753185029Spjd * l2arc_evict() will already have evicted ahead for this case. 4754185029Spjd */ 4755185029Spjd if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 4756219089Spjd vdev_space_update(dev->l2ad_vdev, 4757219089Spjd dev->l2ad_end - dev->l2ad_hand, 0, 0); 4758185029Spjd dev->l2ad_hand = dev->l2ad_start; 4759185029Spjd dev->l2ad_evict = dev->l2ad_start; 4760185029Spjd dev->l2ad_first = B_FALSE; 4761185029Spjd } 4762185029Spjd 4763208373Smm dev->l2ad_writing = B_TRUE; 4764185029Spjd (void) zio_wait(pio); 4765208373Smm dev->l2ad_writing = B_FALSE; 4766208373Smm 4767208373Smm return (write_sz); 4768185029Spjd} 4769185029Spjd 4770185029Spjd/* 4771185029Spjd * This thread feeds the L2ARC at regular intervals. This is the beating 4772185029Spjd * heart of the L2ARC. 4773185029Spjd */ 4774185029Spjdstatic void 4775185029Spjdl2arc_feed_thread(void *dummy __unused) 4776185029Spjd{ 4777185029Spjd callb_cpr_t cpr; 4778185029Spjd l2arc_dev_t *dev; 4779185029Spjd spa_t *spa; 4780208373Smm uint64_t size, wrote; 4781219089Spjd clock_t begin, next = ddi_get_lbolt(); 4782185029Spjd 4783185029Spjd CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 4784185029Spjd 4785185029Spjd mutex_enter(&l2arc_feed_thr_lock); 4786185029Spjd 4787185029Spjd while (l2arc_thread_exit == 0) { 4788185029Spjd CALLB_CPR_SAFE_BEGIN(&cpr); 4789185029Spjd (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 4790219089Spjd next - ddi_get_lbolt()); 4791185029Spjd CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 4792219089Spjd next = ddi_get_lbolt() + hz; 4793185029Spjd 4794185029Spjd /* 4795185029Spjd * Quick check for L2ARC devices. 4796185029Spjd */ 4797185029Spjd mutex_enter(&l2arc_dev_mtx); 4798185029Spjd if (l2arc_ndev == 0) { 4799185029Spjd mutex_exit(&l2arc_dev_mtx); 4800185029Spjd continue; 4801185029Spjd } 4802185029Spjd mutex_exit(&l2arc_dev_mtx); 4803219089Spjd begin = ddi_get_lbolt(); 4804185029Spjd 4805185029Spjd /* 4806185029Spjd * This selects the next l2arc device to write to, and in 4807185029Spjd * doing so the next spa to feed from: dev->l2ad_spa. This 4808185029Spjd * will return NULL if there are now no l2arc devices or if 4809185029Spjd * they are all faulted. 4810185029Spjd * 4811185029Spjd * If a device is returned, its spa's config lock is also 4812185029Spjd * held to prevent device removal. l2arc_dev_get_next() 4813185029Spjd * will grab and release l2arc_dev_mtx. 4814185029Spjd */ 4815185029Spjd if ((dev = l2arc_dev_get_next()) == NULL) 4816185029Spjd continue; 4817185029Spjd 4818185029Spjd spa = dev->l2ad_spa; 4819185029Spjd ASSERT(spa != NULL); 4820185029Spjd 4821185029Spjd /* 4822219089Spjd * If the pool is read-only then force the feed thread to 4823219089Spjd * sleep a little longer. 4824219089Spjd */ 4825219089Spjd if (!spa_writeable(spa)) { 4826219089Spjd next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz; 4827219089Spjd spa_config_exit(spa, SCL_L2ARC, dev); 4828219089Spjd continue; 4829219089Spjd } 4830219089Spjd 4831219089Spjd /* 4832185029Spjd * Avoid contributing to memory pressure. 4833185029Spjd */ 4834185029Spjd if (arc_reclaim_needed()) { 4835185029Spjd ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 4836185029Spjd spa_config_exit(spa, SCL_L2ARC, dev); 4837185029Spjd continue; 4838185029Spjd } 4839185029Spjd 4840185029Spjd ARCSTAT_BUMP(arcstat_l2_feeds); 4841185029Spjd 4842208373Smm size = l2arc_write_size(dev); 4843185029Spjd 4844185029Spjd /* 4845185029Spjd * Evict L2ARC buffers that will be overwritten. 4846185029Spjd */ 4847185029Spjd l2arc_evict(dev, size, B_FALSE); 4848185029Spjd 4849185029Spjd /* 4850185029Spjd * Write ARC buffers. 4851185029Spjd */ 4852208373Smm wrote = l2arc_write_buffers(spa, dev, size); 4853208373Smm 4854208373Smm /* 4855208373Smm * Calculate interval between writes. 4856208373Smm */ 4857208373Smm next = l2arc_write_interval(begin, size, wrote); 4858185029Spjd spa_config_exit(spa, SCL_L2ARC, dev); 4859185029Spjd } 4860185029Spjd 4861185029Spjd l2arc_thread_exit = 0; 4862185029Spjd cv_broadcast(&l2arc_feed_thr_cv); 4863185029Spjd CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 4864185029Spjd thread_exit(); 4865185029Spjd} 4866185029Spjd 4867185029Spjdboolean_t 4868185029Spjdl2arc_vdev_present(vdev_t *vd) 4869185029Spjd{ 4870185029Spjd l2arc_dev_t *dev; 4871185029Spjd 4872185029Spjd mutex_enter(&l2arc_dev_mtx); 4873185029Spjd for (dev = list_head(l2arc_dev_list); dev != NULL; 4874185029Spjd dev = list_next(l2arc_dev_list, dev)) { 4875185029Spjd if (dev->l2ad_vdev == vd) 4876185029Spjd break; 4877185029Spjd } 4878185029Spjd mutex_exit(&l2arc_dev_mtx); 4879185029Spjd 4880185029Spjd return (dev != NULL); 4881185029Spjd} 4882185029Spjd 4883185029Spjd/* 4884185029Spjd * Add a vdev for use by the L2ARC. By this point the spa has already 4885185029Spjd * validated the vdev and opened it. 4886185029Spjd */ 4887185029Spjdvoid 4888219089Spjdl2arc_add_vdev(spa_t *spa, vdev_t *vd) 4889185029Spjd{ 4890185029Spjd l2arc_dev_t *adddev; 4891185029Spjd 4892185029Spjd ASSERT(!l2arc_vdev_present(vd)); 4893185029Spjd 4894185029Spjd /* 4895185029Spjd * Create a new l2arc device entry. 4896185029Spjd */ 4897185029Spjd adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 4898185029Spjd adddev->l2ad_spa = spa; 4899185029Spjd adddev->l2ad_vdev = vd; 4900185029Spjd adddev->l2ad_write = l2arc_write_max; 4901185029Spjd adddev->l2ad_boost = l2arc_write_boost; 4902219089Spjd adddev->l2ad_start = VDEV_LABEL_START_SIZE; 4903219089Spjd adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 4904185029Spjd adddev->l2ad_hand = adddev->l2ad_start; 4905185029Spjd adddev->l2ad_evict = adddev->l2ad_start; 4906185029Spjd adddev->l2ad_first = B_TRUE; 4907208373Smm adddev->l2ad_writing = B_FALSE; 4908185029Spjd ASSERT3U(adddev->l2ad_write, >, 0); 4909185029Spjd 4910185029Spjd /* 4911185029Spjd * This is a list of all ARC buffers that are still valid on the 4912185029Spjd * device. 4913185029Spjd */ 4914185029Spjd adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP); 4915185029Spjd list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 4916185029Spjd offsetof(arc_buf_hdr_t, b_l2node)); 4917185029Spjd 4918219089Spjd vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); 4919185029Spjd 4920185029Spjd /* 4921185029Spjd * Add device to global list 4922185029Spjd */ 4923185029Spjd mutex_enter(&l2arc_dev_mtx); 4924185029Spjd list_insert_head(l2arc_dev_list, adddev); 4925185029Spjd atomic_inc_64(&l2arc_ndev); 4926185029Spjd mutex_exit(&l2arc_dev_mtx); 4927185029Spjd} 4928185029Spjd 4929185029Spjd/* 4930185029Spjd * Remove a vdev from the L2ARC. 4931185029Spjd */ 4932185029Spjdvoid 4933185029Spjdl2arc_remove_vdev(vdev_t *vd) 4934185029Spjd{ 4935185029Spjd l2arc_dev_t *dev, *nextdev, *remdev = NULL; 4936185029Spjd 4937185029Spjd /* 4938185029Spjd * Find the device by vdev 4939185029Spjd */ 4940185029Spjd mutex_enter(&l2arc_dev_mtx); 4941185029Spjd for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 4942185029Spjd nextdev = list_next(l2arc_dev_list, dev); 4943185029Spjd if (vd == dev->l2ad_vdev) { 4944185029Spjd remdev = dev; 4945185029Spjd break; 4946185029Spjd } 4947185029Spjd } 4948185029Spjd ASSERT(remdev != NULL); 4949185029Spjd 4950185029Spjd /* 4951185029Spjd * Remove device from global list 4952185029Spjd */ 4953185029Spjd list_remove(l2arc_dev_list, remdev); 4954185029Spjd l2arc_dev_last = NULL; /* may have been invalidated */ 4955185029Spjd atomic_dec_64(&l2arc_ndev); 4956185029Spjd mutex_exit(&l2arc_dev_mtx); 4957185029Spjd 4958185029Spjd /* 4959185029Spjd * Clear all buflists and ARC references. L2ARC device flush. 4960185029Spjd */ 4961185029Spjd l2arc_evict(remdev, 0, B_TRUE); 4962185029Spjd list_destroy(remdev->l2ad_buflist); 4963185029Spjd kmem_free(remdev->l2ad_buflist, sizeof (list_t)); 4964185029Spjd kmem_free(remdev, sizeof (l2arc_dev_t)); 4965185029Spjd} 4966185029Spjd 4967185029Spjdvoid 4968185029Spjdl2arc_init(void) 4969185029Spjd{ 4970185029Spjd l2arc_thread_exit = 0; 4971185029Spjd l2arc_ndev = 0; 4972185029Spjd l2arc_writes_sent = 0; 4973185029Spjd l2arc_writes_done = 0; 4974185029Spjd 4975185029Spjd mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 4976185029Spjd cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 4977185029Spjd mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 4978185029Spjd mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL); 4979185029Spjd mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 4980185029Spjd 4981185029Spjd l2arc_dev_list = &L2ARC_dev_list; 4982185029Spjd l2arc_free_on_write = &L2ARC_free_on_write; 4983185029Spjd list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 4984185029Spjd offsetof(l2arc_dev_t, l2ad_node)); 4985185029Spjd list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 4986185029Spjd offsetof(l2arc_data_free_t, l2df_list_node)); 4987185029Spjd} 4988185029Spjd 4989185029Spjdvoid 4990185029Spjdl2arc_fini(void) 4991185029Spjd{ 4992185029Spjd /* 4993185029Spjd * This is called from dmu_fini(), which is called from spa_fini(); 4994185029Spjd * Because of this, we can assume that all l2arc devices have 4995185029Spjd * already been removed when the pools themselves were removed. 4996185029Spjd */ 4997185029Spjd 4998185029Spjd l2arc_do_free_on_write(); 4999185029Spjd 5000185029Spjd mutex_destroy(&l2arc_feed_thr_lock); 5001185029Spjd cv_destroy(&l2arc_feed_thr_cv); 5002185029Spjd mutex_destroy(&l2arc_dev_mtx); 5003185029Spjd mutex_destroy(&l2arc_buflist_mtx); 5004185029Spjd mutex_destroy(&l2arc_free_on_write_mtx); 5005185029Spjd 5006185029Spjd list_destroy(l2arc_dev_list); 5007185029Spjd list_destroy(l2arc_free_on_write); 5008185029Spjd} 5009185029Spjd 5010185029Spjdvoid 5011185029Spjdl2arc_start(void) 5012185029Spjd{ 5013209962Smm if (!(spa_mode_global & FWRITE)) 5014185029Spjd return; 5015185029Spjd 5016185029Spjd (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 5017185029Spjd TS_RUN, minclsyspri); 5018185029Spjd} 5019185029Spjd 5020185029Spjdvoid 5021185029Spjdl2arc_stop(void) 5022185029Spjd{ 5023209962Smm if (!(spa_mode_global & FWRITE)) 5024185029Spjd return; 5025185029Spjd 5026185029Spjd mutex_enter(&l2arc_feed_thr_lock); 5027185029Spjd cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 5028185029Spjd l2arc_thread_exit = 1; 5029185029Spjd while (l2arc_thread_exit != 0) 5030185029Spjd cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 5031185029Spjd mutex_exit(&l2arc_feed_thr_lock); 5032185029Spjd} 5033