1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51484Sek110237 * Common Development and Distribution License (the "License"). 61484Sek110237 * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 221484Sek110237 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens /* 29789Sahrens * DVA-based Adjustable Relpacement Cache 30789Sahrens * 311544Seschrock * While much of the theory of operation used here is 321544Seschrock * based on the self-tuning, low overhead replacement cache 33789Sahrens * presented by Megiddo and Modha at FAST 2003, there are some 34789Sahrens * significant differences: 35789Sahrens * 36789Sahrens * 1. The Megiddo and Modha model assumes any page is evictable. 37789Sahrens * Pages in its cache cannot be "locked" into memory. This makes 38789Sahrens * the eviction algorithm simple: evict the last page in the list. 39789Sahrens * This also make the performance characteristics easy to reason 40789Sahrens * about. Our cache is not so simple. At any given moment, some 41789Sahrens * subset of the blocks in the cache are un-evictable because we 42789Sahrens * have handed out a reference to them. Blocks are only evictable 43789Sahrens * when there are no external references active. This makes 44789Sahrens * eviction far more problematic: we choose to evict the evictable 45789Sahrens * blocks that are the "lowest" in the list. 46789Sahrens * 47789Sahrens * There are times when it is not possible to evict the requested 48789Sahrens * space. In these circumstances we are unable to adjust the cache 49789Sahrens * size. To prevent the cache growing unbounded at these times we 50789Sahrens * implement a "cache throttle" that slowes the flow of new data 51789Sahrens * into the cache until we can make space avaiable. 52789Sahrens * 53789Sahrens * 2. The Megiddo and Modha model assumes a fixed cache size. 54789Sahrens * Pages are evicted when the cache is full and there is a cache 55789Sahrens * miss. Our model has a variable sized cache. It grows with 56789Sahrens * high use, but also tries to react to memory preasure from the 57789Sahrens * operating system: decreasing its size when system memory is 58789Sahrens * tight. 59789Sahrens * 60789Sahrens * 3. The Megiddo and Modha model assumes a fixed page size. All 61789Sahrens * elements of the cache are therefor exactly the same size. So 62789Sahrens * when adjusting the cache size following a cache miss, its simply 63789Sahrens * a matter of choosing a single page to evict. In our model, we 64789Sahrens * have variable sized cache blocks (rangeing from 512 bytes to 65789Sahrens * 128K bytes). We therefor choose a set of blocks to evict to make 66789Sahrens * space for a cache miss that approximates as closely as possible 67789Sahrens * the space used by the new block. 68789Sahrens * 69789Sahrens * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache" 70789Sahrens * by N. Megiddo & D. Modha, FAST 2003 71789Sahrens */ 72789Sahrens 73789Sahrens /* 74789Sahrens * The locking model: 75789Sahrens * 76789Sahrens * A new reference to a cache buffer can be obtained in two 77789Sahrens * ways: 1) via a hash table lookup using the DVA as a key, 78789Sahrens * or 2) via one of the ARC lists. The arc_read() inerface 79789Sahrens * uses method 1, while the internal arc algorithms for 80789Sahrens * adjusting the cache use method 2. We therefor provide two 81789Sahrens * types of locks: 1) the hash table lock array, and 2) the 82789Sahrens * arc list locks. 83789Sahrens * 84789Sahrens * Buffers do not have their own mutexs, rather they rely on the 85789Sahrens * hash table mutexs for the bulk of their protection (i.e. most 86789Sahrens * fields in the arc_buf_hdr_t are protected by these mutexs). 87789Sahrens * 88789Sahrens * buf_hash_find() returns the appropriate mutex (held) when it 89789Sahrens * locates the requested buffer in the hash table. It returns 90789Sahrens * NULL for the mutex if the buffer was not in the table. 91789Sahrens * 92789Sahrens * buf_hash_remove() expects the appropriate hash mutex to be 93789Sahrens * already held before it is invoked. 94789Sahrens * 95789Sahrens * Each arc state also has a mutex which is used to protect the 96789Sahrens * buffer list associated with the state. When attempting to 97789Sahrens * obtain a hash table lock while holding an arc list lock you 98789Sahrens * must use: mutex_tryenter() to avoid deadlock. Also note that 992688Smaybee * the active state mutex must be held before the ghost state mutex. 100789Sahrens * 1011544Seschrock * Arc buffers may have an associated eviction callback function. 1021544Seschrock * This function will be invoked prior to removing the buffer (e.g. 1031544Seschrock * in arc_do_user_evicts()). Note however that the data associated 1041544Seschrock * with the buffer may be evicted prior to the callback. The callback 1051544Seschrock * must be made with *no locks held* (to prevent deadlock). Additionally, 1061544Seschrock * the users of callbacks must ensure that their private data is 1071544Seschrock * protected from simultaneous callbacks from arc_buf_evict() 1081544Seschrock * and arc_do_user_evicts(). 1091544Seschrock * 110789Sahrens * Note that the majority of the performance stats are manipulated 111789Sahrens * with atomic operations. 112789Sahrens */ 113789Sahrens 114789Sahrens #include <sys/spa.h> 115789Sahrens #include <sys/zio.h> 116*3093Sahrens #include <sys/zio_checksum.h> 117789Sahrens #include <sys/zfs_context.h> 118789Sahrens #include <sys/arc.h> 119789Sahrens #include <sys/refcount.h> 120789Sahrens #ifdef _KERNEL 121789Sahrens #include <sys/vmsystm.h> 122789Sahrens #include <vm/anon.h> 123789Sahrens #include <sys/fs/swapnode.h> 1241484Sek110237 #include <sys/dnlc.h> 125789Sahrens #endif 126789Sahrens #include <sys/callb.h> 127789Sahrens 128789Sahrens static kmutex_t arc_reclaim_thr_lock; 129789Sahrens static kcondvar_t arc_reclaim_thr_cv; /* used to signal reclaim thr */ 130789Sahrens static uint8_t arc_thread_exit; 131789Sahrens 1321484Sek110237 #define ARC_REDUCE_DNLC_PERCENT 3 1331484Sek110237 uint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT; 1341484Sek110237 135789Sahrens typedef enum arc_reclaim_strategy { 136789Sahrens ARC_RECLAIM_AGGR, /* Aggressive reclaim strategy */ 137789Sahrens ARC_RECLAIM_CONS /* Conservative reclaim strategy */ 138789Sahrens } arc_reclaim_strategy_t; 139789Sahrens 140789Sahrens /* number of seconds before growing cache again */ 141789Sahrens static int arc_grow_retry = 60; 142789Sahrens 1432391Smaybee /* 1442638Sperrin * minimum lifespan of a prefetch block in clock ticks 1452638Sperrin * (initialized in arc_init()) 1462391Smaybee */ 1472638Sperrin static int arc_min_prefetch_lifespan; 1482391Smaybee 149789Sahrens static kmutex_t arc_reclaim_lock; 150789Sahrens static int arc_dead; 151789Sahrens 152789Sahrens /* 1532885Sahrens * These tunables are for performance analysis. 1542885Sahrens */ 1552885Sahrens uint64_t zfs_arc_max; 1562885Sahrens uint64_t zfs_arc_min; 1572885Sahrens 1582885Sahrens /* 159789Sahrens * Note that buffers can be on one of 5 states: 160789Sahrens * ARC_anon - anonymous (discussed below) 1611544Seschrock * ARC_mru - recently used, currently cached 1621544Seschrock * ARC_mru_ghost - recentely used, no longer in cache 1631544Seschrock * ARC_mfu - frequently used, currently cached 1641544Seschrock * ARC_mfu_ghost - frequently used, no longer in cache 165789Sahrens * When there are no active references to the buffer, they 166789Sahrens * are linked onto one of the lists in arc. These are the 167789Sahrens * only buffers that can be evicted or deleted. 168789Sahrens * 169789Sahrens * Anonymous buffers are buffers that are not associated with 170789Sahrens * a DVA. These are buffers that hold dirty block copies 171789Sahrens * before they are written to stable storage. By definition, 1721544Seschrock * they are "ref'd" and are considered part of arc_mru 173789Sahrens * that cannot be freed. Generally, they will aquire a DVA 1741544Seschrock * as they are written and migrate onto the arc_mru list. 175789Sahrens */ 176789Sahrens 177789Sahrens typedef struct arc_state { 178789Sahrens list_t list; /* linked list of evictable buffer in state */ 179789Sahrens uint64_t lsize; /* total size of buffers in the linked list */ 180789Sahrens uint64_t size; /* total size of all buffers in this state */ 181789Sahrens uint64_t hits; 182789Sahrens kmutex_t mtx; 183789Sahrens } arc_state_t; 184789Sahrens 185789Sahrens /* The 5 states: */ 186789Sahrens static arc_state_t ARC_anon; 1871544Seschrock static arc_state_t ARC_mru; 1881544Seschrock static arc_state_t ARC_mru_ghost; 1891544Seschrock static arc_state_t ARC_mfu; 1901544Seschrock static arc_state_t ARC_mfu_ghost; 191789Sahrens 192789Sahrens static struct arc { 193789Sahrens arc_state_t *anon; 1941544Seschrock arc_state_t *mru; 1951544Seschrock arc_state_t *mru_ghost; 1961544Seschrock arc_state_t *mfu; 1971544Seschrock arc_state_t *mfu_ghost; 198789Sahrens uint64_t size; /* Actual total arc size */ 1991544Seschrock uint64_t p; /* Target size (in bytes) of mru */ 200789Sahrens uint64_t c; /* Target size of cache (in bytes) */ 201789Sahrens uint64_t c_min; /* Minimum target cache size */ 202789Sahrens uint64_t c_max; /* Maximum target cache size */ 203789Sahrens 204789Sahrens /* performance stats */ 205789Sahrens uint64_t hits; 206789Sahrens uint64_t misses; 207789Sahrens uint64_t deleted; 2082688Smaybee uint64_t recycle_miss; 2092688Smaybee uint64_t mutex_miss; 2102688Smaybee uint64_t evict_skip; 211789Sahrens uint64_t hash_elements; 212789Sahrens uint64_t hash_elements_max; 213789Sahrens uint64_t hash_collisions; 214789Sahrens uint64_t hash_chains; 215789Sahrens uint32_t hash_chain_max; 216789Sahrens 217789Sahrens int no_grow; /* Don't try to grow cache size */ 218789Sahrens } arc; 219789Sahrens 220789Sahrens static uint64_t arc_tempreserve; 221789Sahrens 222789Sahrens typedef struct arc_callback arc_callback_t; 223789Sahrens 224789Sahrens struct arc_callback { 225789Sahrens arc_done_func_t *acb_done; 226789Sahrens void *acb_private; 227789Sahrens arc_byteswap_func_t *acb_byteswap; 228789Sahrens arc_buf_t *acb_buf; 229789Sahrens zio_t *acb_zio_dummy; 230789Sahrens arc_callback_t *acb_next; 231789Sahrens }; 232789Sahrens 233789Sahrens struct arc_buf_hdr { 234789Sahrens /* immutable */ 235789Sahrens uint64_t b_size; 236789Sahrens spa_t *b_spa; 237789Sahrens 238789Sahrens /* protected by hash lock */ 239789Sahrens dva_t b_dva; 240789Sahrens uint64_t b_birth; 241789Sahrens uint64_t b_cksum0; 242789Sahrens 243*3093Sahrens kmutex_t b_freeze_lock; 244*3093Sahrens zio_cksum_t *b_freeze_cksum; 245*3093Sahrens 246789Sahrens arc_buf_hdr_t *b_hash_next; 247789Sahrens arc_buf_t *b_buf; 248789Sahrens uint32_t b_flags; 2491544Seschrock uint32_t b_datacnt; 250789Sahrens 251789Sahrens kcondvar_t b_cv; 252789Sahrens arc_callback_t *b_acb; 253789Sahrens 254789Sahrens /* protected by arc state mutex */ 255789Sahrens arc_state_t *b_state; 256789Sahrens list_node_t b_arc_node; 257789Sahrens 258789Sahrens /* updated atomically */ 259789Sahrens clock_t b_arc_access; 260789Sahrens 261789Sahrens /* self protecting */ 262789Sahrens refcount_t b_refcnt; 263789Sahrens }; 264789Sahrens 2651544Seschrock static arc_buf_t *arc_eviction_list; 2661544Seschrock static kmutex_t arc_eviction_mtx; 2672887Smaybee static arc_buf_hdr_t arc_eviction_hdr; 2682688Smaybee static void arc_get_data_buf(arc_buf_t *buf); 2692688Smaybee static void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock); 2701544Seschrock 2711544Seschrock #define GHOST_STATE(state) \ 2721544Seschrock ((state) == arc.mru_ghost || (state) == arc.mfu_ghost) 2731544Seschrock 274789Sahrens /* 275789Sahrens * Private ARC flags. These flags are private ARC only flags that will show up 276789Sahrens * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can 277789Sahrens * be passed in as arc_flags in things like arc_read. However, these flags 278789Sahrens * should never be passed and should only be set by ARC code. When adding new 279789Sahrens * public flags, make sure not to smash the private ones. 280789Sahrens */ 281789Sahrens 2821544Seschrock #define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */ 283789Sahrens #define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */ 284789Sahrens #define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */ 285789Sahrens #define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */ 2861544Seschrock #define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */ 2872391Smaybee #define ARC_INDIRECT (1 << 14) /* this is an indirect block */ 288789Sahrens 2891544Seschrock #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE) 290789Sahrens #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS) 291789Sahrens #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR) 292789Sahrens #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ) 2931544Seschrock #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE) 294789Sahrens 295789Sahrens /* 296789Sahrens * Hash table routines 297789Sahrens */ 298789Sahrens 299789Sahrens #define HT_LOCK_PAD 64 300789Sahrens 301789Sahrens struct ht_lock { 302789Sahrens kmutex_t ht_lock; 303789Sahrens #ifdef _KERNEL 304789Sahrens unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 305789Sahrens #endif 306789Sahrens }; 307789Sahrens 308789Sahrens #define BUF_LOCKS 256 309789Sahrens typedef struct buf_hash_table { 310789Sahrens uint64_t ht_mask; 311789Sahrens arc_buf_hdr_t **ht_table; 312789Sahrens struct ht_lock ht_locks[BUF_LOCKS]; 313789Sahrens } buf_hash_table_t; 314789Sahrens 315789Sahrens static buf_hash_table_t buf_hash_table; 316789Sahrens 317789Sahrens #define BUF_HASH_INDEX(spa, dva, birth) \ 318789Sahrens (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 319789Sahrens #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 320789Sahrens #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 321789Sahrens #define HDR_LOCK(buf) \ 322789Sahrens (BUF_HASH_LOCK(BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth))) 323789Sahrens 324789Sahrens uint64_t zfs_crc64_table[256]; 325789Sahrens 326789Sahrens static uint64_t 327789Sahrens buf_hash(spa_t *spa, dva_t *dva, uint64_t birth) 328789Sahrens { 329789Sahrens uintptr_t spav = (uintptr_t)spa; 330789Sahrens uint8_t *vdva = (uint8_t *)dva; 331789Sahrens uint64_t crc = -1ULL; 332789Sahrens int i; 333789Sahrens 334789Sahrens ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 335789Sahrens 336789Sahrens for (i = 0; i < sizeof (dva_t); i++) 337789Sahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 338789Sahrens 339789Sahrens crc ^= (spav>>8) ^ birth; 340789Sahrens 341789Sahrens return (crc); 342789Sahrens } 343789Sahrens 344789Sahrens #define BUF_EMPTY(buf) \ 345789Sahrens ((buf)->b_dva.dva_word[0] == 0 && \ 346789Sahrens (buf)->b_dva.dva_word[1] == 0 && \ 347789Sahrens (buf)->b_birth == 0) 348789Sahrens 349789Sahrens #define BUF_EQUAL(spa, dva, birth, buf) \ 350789Sahrens ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 351789Sahrens ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 352789Sahrens ((buf)->b_birth == birth) && ((buf)->b_spa == spa) 353789Sahrens 354789Sahrens static arc_buf_hdr_t * 355789Sahrens buf_hash_find(spa_t *spa, dva_t *dva, uint64_t birth, kmutex_t **lockp) 356789Sahrens { 357789Sahrens uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 358789Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 359789Sahrens arc_buf_hdr_t *buf; 360789Sahrens 361789Sahrens mutex_enter(hash_lock); 362789Sahrens for (buf = buf_hash_table.ht_table[idx]; buf != NULL; 363789Sahrens buf = buf->b_hash_next) { 364789Sahrens if (BUF_EQUAL(spa, dva, birth, buf)) { 365789Sahrens *lockp = hash_lock; 366789Sahrens return (buf); 367789Sahrens } 368789Sahrens } 369789Sahrens mutex_exit(hash_lock); 370789Sahrens *lockp = NULL; 371789Sahrens return (NULL); 372789Sahrens } 373789Sahrens 374789Sahrens /* 375789Sahrens * Insert an entry into the hash table. If there is already an element 376789Sahrens * equal to elem in the hash table, then the already existing element 377789Sahrens * will be returned and the new element will not be inserted. 378789Sahrens * Otherwise returns NULL. 379789Sahrens */ 380789Sahrens static arc_buf_hdr_t * 381789Sahrens buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp) 382789Sahrens { 383789Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 384789Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 385789Sahrens arc_buf_hdr_t *fbuf; 386789Sahrens uint32_t max, i; 387789Sahrens 3881544Seschrock ASSERT(!HDR_IN_HASH_TABLE(buf)); 389789Sahrens *lockp = hash_lock; 390789Sahrens mutex_enter(hash_lock); 391789Sahrens for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL; 392789Sahrens fbuf = fbuf->b_hash_next, i++) { 393789Sahrens if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf)) 394789Sahrens return (fbuf); 395789Sahrens } 396789Sahrens 397789Sahrens buf->b_hash_next = buf_hash_table.ht_table[idx]; 398789Sahrens buf_hash_table.ht_table[idx] = buf; 3991544Seschrock buf->b_flags |= ARC_IN_HASH_TABLE; 400789Sahrens 401789Sahrens /* collect some hash table performance data */ 402789Sahrens if (i > 0) { 403789Sahrens atomic_add_64(&arc.hash_collisions, 1); 404789Sahrens if (i == 1) 405789Sahrens atomic_add_64(&arc.hash_chains, 1); 406789Sahrens } 407789Sahrens while (i > (max = arc.hash_chain_max) && 408789Sahrens max != atomic_cas_32(&arc.hash_chain_max, max, i)) { 409789Sahrens continue; 410789Sahrens } 411789Sahrens atomic_add_64(&arc.hash_elements, 1); 412789Sahrens if (arc.hash_elements > arc.hash_elements_max) 413789Sahrens atomic_add_64(&arc.hash_elements_max, 1); 414789Sahrens 415789Sahrens return (NULL); 416789Sahrens } 417789Sahrens 418789Sahrens static void 419789Sahrens buf_hash_remove(arc_buf_hdr_t *buf) 420789Sahrens { 421789Sahrens arc_buf_hdr_t *fbuf, **bufp; 422789Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 423789Sahrens 424789Sahrens ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 4251544Seschrock ASSERT(HDR_IN_HASH_TABLE(buf)); 426789Sahrens 427789Sahrens bufp = &buf_hash_table.ht_table[idx]; 428789Sahrens while ((fbuf = *bufp) != buf) { 429789Sahrens ASSERT(fbuf != NULL); 430789Sahrens bufp = &fbuf->b_hash_next; 431789Sahrens } 432789Sahrens *bufp = buf->b_hash_next; 433789Sahrens buf->b_hash_next = NULL; 4341544Seschrock buf->b_flags &= ~ARC_IN_HASH_TABLE; 435789Sahrens 436789Sahrens /* collect some hash table performance data */ 437789Sahrens atomic_add_64(&arc.hash_elements, -1); 438789Sahrens if (buf_hash_table.ht_table[idx] && 439789Sahrens buf_hash_table.ht_table[idx]->b_hash_next == NULL) 440789Sahrens atomic_add_64(&arc.hash_chains, -1); 441789Sahrens } 442789Sahrens 443789Sahrens /* 444789Sahrens * Global data structures and functions for the buf kmem cache. 445789Sahrens */ 446789Sahrens static kmem_cache_t *hdr_cache; 447789Sahrens static kmem_cache_t *buf_cache; 448789Sahrens 449789Sahrens static void 450789Sahrens buf_fini(void) 451789Sahrens { 452789Sahrens int i; 453789Sahrens 454789Sahrens kmem_free(buf_hash_table.ht_table, 455789Sahrens (buf_hash_table.ht_mask + 1) * sizeof (void *)); 456789Sahrens for (i = 0; i < BUF_LOCKS; i++) 457789Sahrens mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 458789Sahrens kmem_cache_destroy(hdr_cache); 459789Sahrens kmem_cache_destroy(buf_cache); 460789Sahrens } 461789Sahrens 462789Sahrens /* 463789Sahrens * Constructor callback - called when the cache is empty 464789Sahrens * and a new buf is requested. 465789Sahrens */ 466789Sahrens /* ARGSUSED */ 467789Sahrens static int 468789Sahrens hdr_cons(void *vbuf, void *unused, int kmflag) 469789Sahrens { 470789Sahrens arc_buf_hdr_t *buf = vbuf; 471789Sahrens 472789Sahrens bzero(buf, sizeof (arc_buf_hdr_t)); 473789Sahrens refcount_create(&buf->b_refcnt); 474789Sahrens cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL); 475789Sahrens return (0); 476789Sahrens } 477789Sahrens 478789Sahrens /* 479789Sahrens * Destructor callback - called when a cached buf is 480789Sahrens * no longer required. 481789Sahrens */ 482789Sahrens /* ARGSUSED */ 483789Sahrens static void 484789Sahrens hdr_dest(void *vbuf, void *unused) 485789Sahrens { 486789Sahrens arc_buf_hdr_t *buf = vbuf; 487789Sahrens 488789Sahrens refcount_destroy(&buf->b_refcnt); 489789Sahrens cv_destroy(&buf->b_cv); 490789Sahrens } 491789Sahrens 4921544Seschrock static int arc_reclaim_needed(void); 493789Sahrens void arc_kmem_reclaim(void); 494789Sahrens 495789Sahrens /* 496789Sahrens * Reclaim callback -- invoked when memory is low. 497789Sahrens */ 498789Sahrens /* ARGSUSED */ 499789Sahrens static void 500789Sahrens hdr_recl(void *unused) 501789Sahrens { 502789Sahrens dprintf("hdr_recl called\n"); 5031544Seschrock if (arc_reclaim_needed()) 5041544Seschrock arc_kmem_reclaim(); 505789Sahrens } 506789Sahrens 507789Sahrens static void 508789Sahrens buf_init(void) 509789Sahrens { 510789Sahrens uint64_t *ct; 5111544Seschrock uint64_t hsize = 1ULL << 12; 512789Sahrens int i, j; 513789Sahrens 514789Sahrens /* 515789Sahrens * The hash table is big enough to fill all of physical memory 5161544Seschrock * with an average 64K block size. The table will take up 5171544Seschrock * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers). 518789Sahrens */ 5191544Seschrock while (hsize * 65536 < physmem * PAGESIZE) 520789Sahrens hsize <<= 1; 5211544Seschrock retry: 522789Sahrens buf_hash_table.ht_mask = hsize - 1; 5231544Seschrock buf_hash_table.ht_table = 5241544Seschrock kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 5251544Seschrock if (buf_hash_table.ht_table == NULL) { 5261544Seschrock ASSERT(hsize > (1ULL << 8)); 5271544Seschrock hsize >>= 1; 5281544Seschrock goto retry; 5291544Seschrock } 530789Sahrens 531789Sahrens hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t), 532789Sahrens 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0); 533789Sahrens buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 534789Sahrens 0, NULL, NULL, NULL, NULL, NULL, 0); 535789Sahrens 536789Sahrens for (i = 0; i < 256; i++) 537789Sahrens for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 538789Sahrens *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 539789Sahrens 540789Sahrens for (i = 0; i < BUF_LOCKS; i++) { 541789Sahrens mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 542789Sahrens NULL, MUTEX_DEFAULT, NULL); 543789Sahrens } 544789Sahrens } 545789Sahrens 546789Sahrens #define ARC_MINTIME (hz>>4) /* 62 ms */ 547789Sahrens 548789Sahrens static void 549*3093Sahrens arc_cksum_verify(arc_buf_t *buf) 550*3093Sahrens { 551*3093Sahrens zio_cksum_t zc; 552*3093Sahrens 553*3093Sahrens if (!zfs_flags & ZFS_DEBUG_MODIFY) 554*3093Sahrens return; 555*3093Sahrens 556*3093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 557*3093Sahrens if (buf->b_hdr->b_freeze_cksum == NULL) { 558*3093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 559*3093Sahrens return; 560*3093Sahrens } 561*3093Sahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 562*3093Sahrens if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) 563*3093Sahrens panic("buffer modified while frozen!"); 564*3093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 565*3093Sahrens } 566*3093Sahrens 567*3093Sahrens static void 568*3093Sahrens arc_cksum_compute(arc_buf_t *buf) 569*3093Sahrens { 570*3093Sahrens if (!zfs_flags & ZFS_DEBUG_MODIFY) 571*3093Sahrens return; 572*3093Sahrens 573*3093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 574*3093Sahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 575*3093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 576*3093Sahrens return; 577*3093Sahrens } 578*3093Sahrens buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 579*3093Sahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, 580*3093Sahrens buf->b_hdr->b_freeze_cksum); 581*3093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 582*3093Sahrens } 583*3093Sahrens 584*3093Sahrens void 585*3093Sahrens arc_buf_thaw(arc_buf_t *buf) 586*3093Sahrens { 587*3093Sahrens if (!zfs_flags & ZFS_DEBUG_MODIFY) 588*3093Sahrens return; 589*3093Sahrens 590*3093Sahrens if (buf->b_hdr->b_state != arc.anon) 591*3093Sahrens panic("modifying non-anon buffer!"); 592*3093Sahrens if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS) 593*3093Sahrens panic("modifying buffer while i/o in progress!"); 594*3093Sahrens arc_cksum_verify(buf); 595*3093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 596*3093Sahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 597*3093Sahrens kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 598*3093Sahrens buf->b_hdr->b_freeze_cksum = NULL; 599*3093Sahrens } 600*3093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 601*3093Sahrens } 602*3093Sahrens 603*3093Sahrens void 604*3093Sahrens arc_buf_freeze(arc_buf_t *buf) 605*3093Sahrens { 606*3093Sahrens ASSERT(buf->b_hdr->b_freeze_cksum != NULL || 607*3093Sahrens buf->b_hdr->b_state == arc.anon); 608*3093Sahrens arc_cksum_compute(buf); 609*3093Sahrens } 610*3093Sahrens 611*3093Sahrens static void 612789Sahrens add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 613789Sahrens { 614789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 615789Sahrens 616789Sahrens if ((refcount_add(&ab->b_refcnt, tag) == 1) && 617789Sahrens (ab->b_state != arc.anon)) { 6181544Seschrock int delta = ab->b_size * ab->b_datacnt; 619789Sahrens 620789Sahrens ASSERT(!MUTEX_HELD(&ab->b_state->mtx)); 621789Sahrens mutex_enter(&ab->b_state->mtx); 622789Sahrens ASSERT(list_link_active(&ab->b_arc_node)); 623789Sahrens list_remove(&ab->b_state->list, ab); 6241544Seschrock if (GHOST_STATE(ab->b_state)) { 6251544Seschrock ASSERT3U(ab->b_datacnt, ==, 0); 6261544Seschrock ASSERT3P(ab->b_buf, ==, NULL); 6271544Seschrock delta = ab->b_size; 6281544Seschrock } 6291544Seschrock ASSERT(delta > 0); 6301544Seschrock ASSERT3U(ab->b_state->lsize, >=, delta); 6311544Seschrock atomic_add_64(&ab->b_state->lsize, -delta); 632789Sahrens mutex_exit(&ab->b_state->mtx); 6332391Smaybee /* remove the prefetch flag is we get a reference */ 6342391Smaybee if (ab->b_flags & ARC_PREFETCH) 6352391Smaybee ab->b_flags &= ~ARC_PREFETCH; 636789Sahrens } 637789Sahrens } 638789Sahrens 639789Sahrens static int 640789Sahrens remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 641789Sahrens { 642789Sahrens int cnt; 643789Sahrens 6441544Seschrock ASSERT(ab->b_state == arc.anon || MUTEX_HELD(hash_lock)); 6451544Seschrock ASSERT(!GHOST_STATE(ab->b_state)); 646789Sahrens 647789Sahrens if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) && 648789Sahrens (ab->b_state != arc.anon)) { 649789Sahrens 650789Sahrens ASSERT(!MUTEX_HELD(&ab->b_state->mtx)); 651789Sahrens mutex_enter(&ab->b_state->mtx); 652789Sahrens ASSERT(!list_link_active(&ab->b_arc_node)); 653789Sahrens list_insert_head(&ab->b_state->list, ab); 6541544Seschrock ASSERT(ab->b_datacnt > 0); 6551544Seschrock atomic_add_64(&ab->b_state->lsize, ab->b_size * ab->b_datacnt); 6561544Seschrock ASSERT3U(ab->b_state->size, >=, ab->b_state->lsize); 657789Sahrens mutex_exit(&ab->b_state->mtx); 658789Sahrens } 659789Sahrens return (cnt); 660789Sahrens } 661789Sahrens 662789Sahrens /* 663789Sahrens * Move the supplied buffer to the indicated state. The mutex 664789Sahrens * for the buffer must be held by the caller. 665789Sahrens */ 666789Sahrens static void 6671544Seschrock arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock) 668789Sahrens { 6691544Seschrock arc_state_t *old_state = ab->b_state; 6701544Seschrock int refcnt = refcount_count(&ab->b_refcnt); 6711544Seschrock int from_delta, to_delta; 672789Sahrens 673789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 6741544Seschrock ASSERT(new_state != old_state); 6751544Seschrock ASSERT(refcnt == 0 || ab->b_datacnt > 0); 6761544Seschrock ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state)); 6771544Seschrock 6781544Seschrock from_delta = to_delta = ab->b_datacnt * ab->b_size; 679789Sahrens 680789Sahrens /* 681789Sahrens * If this buffer is evictable, transfer it from the 682789Sahrens * old state list to the new state list. 683789Sahrens */ 6841544Seschrock if (refcnt == 0) { 6851544Seschrock if (old_state != arc.anon) { 6861544Seschrock int use_mutex = !MUTEX_HELD(&old_state->mtx); 6871544Seschrock 6881544Seschrock if (use_mutex) 6891544Seschrock mutex_enter(&old_state->mtx); 6901544Seschrock 6911544Seschrock ASSERT(list_link_active(&ab->b_arc_node)); 6921544Seschrock list_remove(&old_state->list, ab); 693789Sahrens 6942391Smaybee /* 6952391Smaybee * If prefetching out of the ghost cache, 6962391Smaybee * we will have a non-null datacnt. 6972391Smaybee */ 6982391Smaybee if (GHOST_STATE(old_state) && ab->b_datacnt == 0) { 6992391Smaybee /* ghost elements have a ghost size */ 7001544Seschrock ASSERT(ab->b_buf == NULL); 7011544Seschrock from_delta = ab->b_size; 702789Sahrens } 7031544Seschrock ASSERT3U(old_state->lsize, >=, from_delta); 7041544Seschrock atomic_add_64(&old_state->lsize, -from_delta); 7051544Seschrock 7061544Seschrock if (use_mutex) 7071544Seschrock mutex_exit(&old_state->mtx); 708789Sahrens } 709789Sahrens if (new_state != arc.anon) { 7101544Seschrock int use_mutex = !MUTEX_HELD(&new_state->mtx); 711789Sahrens 7121544Seschrock if (use_mutex) 713789Sahrens mutex_enter(&new_state->mtx); 7141544Seschrock 715789Sahrens list_insert_head(&new_state->list, ab); 7161544Seschrock 7171544Seschrock /* ghost elements have a ghost size */ 7181544Seschrock if (GHOST_STATE(new_state)) { 7191544Seschrock ASSERT(ab->b_datacnt == 0); 7201544Seschrock ASSERT(ab->b_buf == NULL); 7211544Seschrock to_delta = ab->b_size; 7221544Seschrock } 7231544Seschrock atomic_add_64(&new_state->lsize, to_delta); 7241544Seschrock ASSERT3U(new_state->size + to_delta, >=, 7251544Seschrock new_state->lsize); 7261544Seschrock 7271544Seschrock if (use_mutex) 728789Sahrens mutex_exit(&new_state->mtx); 729789Sahrens } 730789Sahrens } 731789Sahrens 732789Sahrens ASSERT(!BUF_EMPTY(ab)); 7331544Seschrock if (new_state == arc.anon && old_state != arc.anon) { 734789Sahrens buf_hash_remove(ab); 735789Sahrens } 736789Sahrens 7371544Seschrock /* adjust state sizes */ 7381544Seschrock if (to_delta) 7391544Seschrock atomic_add_64(&new_state->size, to_delta); 7401544Seschrock if (from_delta) { 7411544Seschrock ASSERT3U(old_state->size, >=, from_delta); 7421544Seschrock atomic_add_64(&old_state->size, -from_delta); 743789Sahrens } 744789Sahrens ab->b_state = new_state; 745789Sahrens } 746789Sahrens 747789Sahrens arc_buf_t * 748789Sahrens arc_buf_alloc(spa_t *spa, int size, void *tag) 749789Sahrens { 750789Sahrens arc_buf_hdr_t *hdr; 751789Sahrens arc_buf_t *buf; 752789Sahrens 753789Sahrens ASSERT3U(size, >, 0); 754789Sahrens hdr = kmem_cache_alloc(hdr_cache, KM_SLEEP); 755789Sahrens ASSERT(BUF_EMPTY(hdr)); 756789Sahrens hdr->b_size = size; 757789Sahrens hdr->b_spa = spa; 758789Sahrens hdr->b_state = arc.anon; 759789Sahrens hdr->b_arc_access = 0; 760789Sahrens buf = kmem_cache_alloc(buf_cache, KM_SLEEP); 761789Sahrens buf->b_hdr = hdr; 7622688Smaybee buf->b_data = NULL; 7631544Seschrock buf->b_efunc = NULL; 7641544Seschrock buf->b_private = NULL; 765789Sahrens buf->b_next = NULL; 766789Sahrens hdr->b_buf = buf; 7672688Smaybee arc_get_data_buf(buf); 7681544Seschrock hdr->b_datacnt = 1; 769789Sahrens hdr->b_flags = 0; 770789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 771789Sahrens (void) refcount_add(&hdr->b_refcnt, tag); 772789Sahrens 773789Sahrens return (buf); 774789Sahrens } 775789Sahrens 7762688Smaybee static arc_buf_t * 7772688Smaybee arc_buf_clone(arc_buf_t *from) 7781544Seschrock { 7792688Smaybee arc_buf_t *buf; 7802688Smaybee arc_buf_hdr_t *hdr = from->b_hdr; 7812688Smaybee uint64_t size = hdr->b_size; 7821544Seschrock 7832688Smaybee buf = kmem_cache_alloc(buf_cache, KM_SLEEP); 7842688Smaybee buf->b_hdr = hdr; 7852688Smaybee buf->b_data = NULL; 7862688Smaybee buf->b_efunc = NULL; 7872688Smaybee buf->b_private = NULL; 7882688Smaybee buf->b_next = hdr->b_buf; 7892688Smaybee hdr->b_buf = buf; 7902688Smaybee arc_get_data_buf(buf); 7912688Smaybee bcopy(from->b_data, buf->b_data, size); 7922688Smaybee hdr->b_datacnt += 1; 7932688Smaybee return (buf); 7941544Seschrock } 7951544Seschrock 7961544Seschrock void 7971544Seschrock arc_buf_add_ref(arc_buf_t *buf, void* tag) 7981544Seschrock { 7992887Smaybee arc_buf_hdr_t *hdr; 8001544Seschrock kmutex_t *hash_lock; 8011544Seschrock 8022724Smaybee /* 8032724Smaybee * Check to see if this buffer is currently being evicted via 8042887Smaybee * arc_do_user_evicts(). 8052724Smaybee */ 8062887Smaybee mutex_enter(&arc_eviction_mtx); 8072887Smaybee hdr = buf->b_hdr; 8082887Smaybee if (hdr == NULL) { 8092887Smaybee mutex_exit(&arc_eviction_mtx); 8102724Smaybee return; 8112887Smaybee } 8122887Smaybee hash_lock = HDR_LOCK(hdr); 8132887Smaybee mutex_exit(&arc_eviction_mtx); 8142724Smaybee 8152724Smaybee mutex_enter(hash_lock); 8161544Seschrock if (buf->b_data == NULL) { 8171544Seschrock /* 8181544Seschrock * This buffer is evicted. 8191544Seschrock */ 8202724Smaybee mutex_exit(hash_lock); 8211544Seschrock return; 8221544Seschrock } 8231544Seschrock 8242724Smaybee ASSERT(buf->b_hdr == hdr); 8252724Smaybee ASSERT(hdr->b_state == arc.mru || hdr->b_state == arc.mfu); 8261544Seschrock add_reference(hdr, hash_lock, tag); 8272688Smaybee arc_access(hdr, hash_lock); 8282688Smaybee mutex_exit(hash_lock); 8291544Seschrock atomic_add_64(&arc.hits, 1); 8301544Seschrock } 8311544Seschrock 832789Sahrens static void 8332688Smaybee arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all) 8341544Seschrock { 8351544Seschrock arc_buf_t **bufp; 8361544Seschrock 8371544Seschrock /* free up data associated with the buf */ 8381544Seschrock if (buf->b_data) { 8391544Seschrock arc_state_t *state = buf->b_hdr->b_state; 8401544Seschrock uint64_t size = buf->b_hdr->b_size; 8411544Seschrock 842*3093Sahrens arc_cksum_verify(buf); 8432688Smaybee if (!recycle) { 8442688Smaybee zio_buf_free(buf->b_data, size); 8452688Smaybee atomic_add_64(&arc.size, -size); 8462688Smaybee } 8471544Seschrock if (list_link_active(&buf->b_hdr->b_arc_node)) { 8481544Seschrock ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt)); 8491544Seschrock ASSERT(state != arc.anon); 8501544Seschrock ASSERT3U(state->lsize, >=, size); 8511544Seschrock atomic_add_64(&state->lsize, -size); 8521544Seschrock } 8531544Seschrock ASSERT3U(state->size, >=, size); 8541544Seschrock atomic_add_64(&state->size, -size); 8551544Seschrock buf->b_data = NULL; 8561544Seschrock ASSERT(buf->b_hdr->b_datacnt > 0); 8571544Seschrock buf->b_hdr->b_datacnt -= 1; 8581544Seschrock } 8591544Seschrock 8601544Seschrock /* only remove the buf if requested */ 8611544Seschrock if (!all) 8621544Seschrock return; 8631544Seschrock 8641544Seschrock /* remove the buf from the hdr list */ 8651544Seschrock for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next) 8661544Seschrock continue; 8671544Seschrock *bufp = buf->b_next; 8681544Seschrock 8691544Seschrock ASSERT(buf->b_efunc == NULL); 8701544Seschrock 8711544Seschrock /* clean up the buf */ 8721544Seschrock buf->b_hdr = NULL; 8731544Seschrock kmem_cache_free(buf_cache, buf); 8741544Seschrock } 8751544Seschrock 8761544Seschrock static void 8771544Seschrock arc_hdr_destroy(arc_buf_hdr_t *hdr) 878789Sahrens { 879789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 880789Sahrens ASSERT3P(hdr->b_state, ==, arc.anon); 8811544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 882789Sahrens 883789Sahrens if (!BUF_EMPTY(hdr)) { 8841544Seschrock ASSERT(!HDR_IN_HASH_TABLE(hdr)); 885789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 886789Sahrens hdr->b_birth = 0; 887789Sahrens hdr->b_cksum0 = 0; 888789Sahrens } 8891544Seschrock while (hdr->b_buf) { 890789Sahrens arc_buf_t *buf = hdr->b_buf; 891789Sahrens 8921544Seschrock if (buf->b_efunc) { 8931544Seschrock mutex_enter(&arc_eviction_mtx); 8941544Seschrock ASSERT(buf->b_hdr != NULL); 8952688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, FALSE); 8961544Seschrock hdr->b_buf = buf->b_next; 8972887Smaybee buf->b_hdr = &arc_eviction_hdr; 8981544Seschrock buf->b_next = arc_eviction_list; 8991544Seschrock arc_eviction_list = buf; 9001544Seschrock mutex_exit(&arc_eviction_mtx); 9011544Seschrock } else { 9022688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, TRUE); 9031544Seschrock } 904789Sahrens } 905*3093Sahrens if (hdr->b_freeze_cksum != NULL) { 906*3093Sahrens kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 907*3093Sahrens hdr->b_freeze_cksum = NULL; 908*3093Sahrens } 9091544Seschrock 910789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 911789Sahrens ASSERT3P(hdr->b_hash_next, ==, NULL); 912789Sahrens ASSERT3P(hdr->b_acb, ==, NULL); 913789Sahrens kmem_cache_free(hdr_cache, hdr); 914789Sahrens } 915789Sahrens 916789Sahrens void 917789Sahrens arc_buf_free(arc_buf_t *buf, void *tag) 918789Sahrens { 919789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 9201544Seschrock int hashed = hdr->b_state != arc.anon; 9211544Seschrock 9221544Seschrock ASSERT(buf->b_efunc == NULL); 9231544Seschrock ASSERT(buf->b_data != NULL); 9241544Seschrock 9251544Seschrock if (hashed) { 9261544Seschrock kmutex_t *hash_lock = HDR_LOCK(hdr); 9271544Seschrock 9281544Seschrock mutex_enter(hash_lock); 9291544Seschrock (void) remove_reference(hdr, hash_lock, tag); 9301544Seschrock if (hdr->b_datacnt > 1) 9312688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 9321544Seschrock else 9331544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 9341544Seschrock mutex_exit(hash_lock); 9351544Seschrock } else if (HDR_IO_IN_PROGRESS(hdr)) { 9361544Seschrock int destroy_hdr; 9371544Seschrock /* 9381544Seschrock * We are in the middle of an async write. Don't destroy 9391544Seschrock * this buffer unless the write completes before we finish 9401544Seschrock * decrementing the reference count. 9411544Seschrock */ 9421544Seschrock mutex_enter(&arc_eviction_mtx); 9431544Seschrock (void) remove_reference(hdr, NULL, tag); 9441544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 9451544Seschrock destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 9461544Seschrock mutex_exit(&arc_eviction_mtx); 9471544Seschrock if (destroy_hdr) 9481544Seschrock arc_hdr_destroy(hdr); 9491544Seschrock } else { 9501544Seschrock if (remove_reference(hdr, NULL, tag) > 0) { 9511544Seschrock ASSERT(HDR_IO_ERROR(hdr)); 9522688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 9531544Seschrock } else { 9541544Seschrock arc_hdr_destroy(hdr); 9551544Seschrock } 9561544Seschrock } 9571544Seschrock } 9581544Seschrock 9591544Seschrock int 9601544Seschrock arc_buf_remove_ref(arc_buf_t *buf, void* tag) 9611544Seschrock { 9621544Seschrock arc_buf_hdr_t *hdr = buf->b_hdr; 963789Sahrens kmutex_t *hash_lock = HDR_LOCK(hdr); 9641544Seschrock int no_callback = (buf->b_efunc == NULL); 9651544Seschrock 9661544Seschrock if (hdr->b_state == arc.anon) { 9671544Seschrock arc_buf_free(buf, tag); 9681544Seschrock return (no_callback); 9691544Seschrock } 970789Sahrens 971789Sahrens mutex_enter(hash_lock); 9721544Seschrock ASSERT(hdr->b_state != arc.anon); 9731544Seschrock ASSERT(buf->b_data != NULL); 974789Sahrens 9751544Seschrock (void) remove_reference(hdr, hash_lock, tag); 9761544Seschrock if (hdr->b_datacnt > 1) { 9771544Seschrock if (no_callback) 9782688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 9791544Seschrock } else if (no_callback) { 9801544Seschrock ASSERT(hdr->b_buf == buf && buf->b_next == NULL); 9811544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 982789Sahrens } 9831544Seschrock ASSERT(no_callback || hdr->b_datacnt > 1 || 9841544Seschrock refcount_is_zero(&hdr->b_refcnt)); 985789Sahrens mutex_exit(hash_lock); 9861544Seschrock return (no_callback); 987789Sahrens } 988789Sahrens 989789Sahrens int 990789Sahrens arc_buf_size(arc_buf_t *buf) 991789Sahrens { 992789Sahrens return (buf->b_hdr->b_size); 993789Sahrens } 994789Sahrens 995789Sahrens /* 996789Sahrens * Evict buffers from list until we've removed the specified number of 997789Sahrens * bytes. Move the removed buffers to the appropriate evict state. 9982688Smaybee * If the recycle flag is set, then attempt to "recycle" a buffer: 9992688Smaybee * - look for a buffer to evict that is `bytes' long. 10002688Smaybee * - return the data block from this buffer rather than freeing it. 10012688Smaybee * This flag is used by callers that are trying to make space for a 10022688Smaybee * new buffer in a full arc cache. 1003789Sahrens */ 10042688Smaybee static void * 10052688Smaybee arc_evict(arc_state_t *state, int64_t bytes, boolean_t recycle) 1006789Sahrens { 1007789Sahrens arc_state_t *evicted_state; 10082688Smaybee uint64_t bytes_evicted = 0, skipped = 0, missed = 0; 10092918Smaybee arc_buf_hdr_t *ab, *ab_prev = NULL; 1010789Sahrens kmutex_t *hash_lock; 10112688Smaybee boolean_t have_lock; 10122918Smaybee void *stolen = NULL; 1013789Sahrens 10141544Seschrock ASSERT(state == arc.mru || state == arc.mfu); 1015789Sahrens 10161544Seschrock evicted_state = (state == arc.mru) ? arc.mru_ghost : arc.mfu_ghost; 1017789Sahrens 1018789Sahrens mutex_enter(&state->mtx); 1019789Sahrens mutex_enter(&evicted_state->mtx); 1020789Sahrens 1021789Sahrens for (ab = list_tail(&state->list); ab; ab = ab_prev) { 1022789Sahrens ab_prev = list_prev(&state->list, ab); 10232391Smaybee /* prefetch buffers have a minimum lifespan */ 10242688Smaybee if (HDR_IO_IN_PROGRESS(ab) || 10252688Smaybee (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) && 10262688Smaybee lbolt - ab->b_arc_access < arc_min_prefetch_lifespan)) { 10272391Smaybee skipped++; 10282391Smaybee continue; 10292391Smaybee } 10302918Smaybee /* "lookahead" for better eviction candidate */ 10312918Smaybee if (recycle && ab->b_size != bytes && 10322918Smaybee ab_prev && ab_prev->b_size == bytes) 10332688Smaybee continue; 1034789Sahrens hash_lock = HDR_LOCK(ab); 10352688Smaybee have_lock = MUTEX_HELD(hash_lock); 10362688Smaybee if (have_lock || mutex_tryenter(hash_lock)) { 1037789Sahrens ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0); 10381544Seschrock ASSERT(ab->b_datacnt > 0); 10391544Seschrock while (ab->b_buf) { 10401544Seschrock arc_buf_t *buf = ab->b_buf; 10412688Smaybee if (buf->b_data) { 10421544Seschrock bytes_evicted += ab->b_size; 10432918Smaybee if (recycle && ab->b_size == bytes) { 10442918Smaybee stolen = buf->b_data; 10452918Smaybee recycle = FALSE; 10462918Smaybee } 10472688Smaybee } 10481544Seschrock if (buf->b_efunc) { 10491544Seschrock mutex_enter(&arc_eviction_mtx); 10502918Smaybee arc_buf_destroy(buf, 10512918Smaybee buf->b_data == stolen, FALSE); 10521544Seschrock ab->b_buf = buf->b_next; 10532887Smaybee buf->b_hdr = &arc_eviction_hdr; 10541544Seschrock buf->b_next = arc_eviction_list; 10551544Seschrock arc_eviction_list = buf; 10561544Seschrock mutex_exit(&arc_eviction_mtx); 10571544Seschrock } else { 10582918Smaybee arc_buf_destroy(buf, 10592918Smaybee buf->b_data == stolen, TRUE); 10601544Seschrock } 10611544Seschrock } 10621544Seschrock ASSERT(ab->b_datacnt == 0); 1063789Sahrens arc_change_state(evicted_state, ab, hash_lock); 10641544Seschrock ASSERT(HDR_IN_HASH_TABLE(ab)); 10651544Seschrock ab->b_flags = ARC_IN_HASH_TABLE; 1066789Sahrens DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab); 10672688Smaybee if (!have_lock) 10682688Smaybee mutex_exit(hash_lock); 10691544Seschrock if (bytes >= 0 && bytes_evicted >= bytes) 1070789Sahrens break; 1071789Sahrens } else { 10722688Smaybee missed += 1; 1073789Sahrens } 1074789Sahrens } 1075789Sahrens mutex_exit(&evicted_state->mtx); 1076789Sahrens mutex_exit(&state->mtx); 1077789Sahrens 1078789Sahrens if (bytes_evicted < bytes) 1079789Sahrens dprintf("only evicted %lld bytes from %x", 1080789Sahrens (longlong_t)bytes_evicted, state); 1081789Sahrens 10822688Smaybee if (skipped) 10832688Smaybee atomic_add_64(&arc.evict_skip, skipped); 10842688Smaybee if (missed) 10852688Smaybee atomic_add_64(&arc.mutex_miss, missed); 10862918Smaybee return (stolen); 1087789Sahrens } 1088789Sahrens 1089789Sahrens /* 1090789Sahrens * Remove buffers from list until we've removed the specified number of 1091789Sahrens * bytes. Destroy the buffers that are removed. 1092789Sahrens */ 1093789Sahrens static void 10941544Seschrock arc_evict_ghost(arc_state_t *state, int64_t bytes) 1095789Sahrens { 1096789Sahrens arc_buf_hdr_t *ab, *ab_prev; 1097789Sahrens kmutex_t *hash_lock; 10981544Seschrock uint64_t bytes_deleted = 0; 10991544Seschrock uint_t bufs_skipped = 0; 1100789Sahrens 11011544Seschrock ASSERT(GHOST_STATE(state)); 1102789Sahrens top: 1103789Sahrens mutex_enter(&state->mtx); 1104789Sahrens for (ab = list_tail(&state->list); ab; ab = ab_prev) { 1105789Sahrens ab_prev = list_prev(&state->list, ab); 1106789Sahrens hash_lock = HDR_LOCK(ab); 1107789Sahrens if (mutex_tryenter(hash_lock)) { 11082391Smaybee ASSERT(!HDR_IO_IN_PROGRESS(ab)); 11091544Seschrock ASSERT(ab->b_buf == NULL); 1110789Sahrens arc_change_state(arc.anon, ab, hash_lock); 1111789Sahrens mutex_exit(hash_lock); 1112789Sahrens atomic_add_64(&arc.deleted, 1); 11131544Seschrock bytes_deleted += ab->b_size; 11141544Seschrock arc_hdr_destroy(ab); 1115789Sahrens DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab); 1116789Sahrens if (bytes >= 0 && bytes_deleted >= bytes) 1117789Sahrens break; 1118789Sahrens } else { 1119789Sahrens if (bytes < 0) { 1120789Sahrens mutex_exit(&state->mtx); 1121789Sahrens mutex_enter(hash_lock); 1122789Sahrens mutex_exit(hash_lock); 1123789Sahrens goto top; 1124789Sahrens } 1125789Sahrens bufs_skipped += 1; 1126789Sahrens } 1127789Sahrens } 1128789Sahrens mutex_exit(&state->mtx); 1129789Sahrens 1130789Sahrens if (bufs_skipped) { 11312688Smaybee atomic_add_64(&arc.mutex_miss, bufs_skipped); 1132789Sahrens ASSERT(bytes >= 0); 1133789Sahrens } 1134789Sahrens 1135789Sahrens if (bytes_deleted < bytes) 1136789Sahrens dprintf("only deleted %lld bytes from %p", 1137789Sahrens (longlong_t)bytes_deleted, state); 1138789Sahrens } 1139789Sahrens 1140789Sahrens static void 1141789Sahrens arc_adjust(void) 1142789Sahrens { 1143789Sahrens int64_t top_sz, mru_over, arc_over; 1144789Sahrens 11451544Seschrock top_sz = arc.anon->size + arc.mru->size; 1146789Sahrens 11471544Seschrock if (top_sz > arc.p && arc.mru->lsize > 0) { 11481544Seschrock int64_t toevict = MIN(arc.mru->lsize, top_sz-arc.p); 11492688Smaybee (void) arc_evict(arc.mru, toevict, FALSE); 11501544Seschrock top_sz = arc.anon->size + arc.mru->size; 1151789Sahrens } 1152789Sahrens 11531544Seschrock mru_over = top_sz + arc.mru_ghost->size - arc.c; 1154789Sahrens 1155789Sahrens if (mru_over > 0) { 11561544Seschrock if (arc.mru_ghost->lsize > 0) { 11571544Seschrock int64_t todelete = MIN(arc.mru_ghost->lsize, mru_over); 11581544Seschrock arc_evict_ghost(arc.mru_ghost, todelete); 1159789Sahrens } 1160789Sahrens } 1161789Sahrens 1162789Sahrens if ((arc_over = arc.size - arc.c) > 0) { 11631544Seschrock int64_t tbl_over; 1164789Sahrens 11651544Seschrock if (arc.mfu->lsize > 0) { 11661544Seschrock int64_t toevict = MIN(arc.mfu->lsize, arc_over); 11672688Smaybee (void) arc_evict(arc.mfu, toevict, FALSE); 1168789Sahrens } 1169789Sahrens 11701544Seschrock tbl_over = arc.size + arc.mru_ghost->lsize + 11711544Seschrock arc.mfu_ghost->lsize - arc.c*2; 1172789Sahrens 11731544Seschrock if (tbl_over > 0 && arc.mfu_ghost->lsize > 0) { 11741544Seschrock int64_t todelete = MIN(arc.mfu_ghost->lsize, tbl_over); 11751544Seschrock arc_evict_ghost(arc.mfu_ghost, todelete); 1176789Sahrens } 1177789Sahrens } 1178789Sahrens } 1179789Sahrens 11801544Seschrock static void 11811544Seschrock arc_do_user_evicts(void) 11821544Seschrock { 11831544Seschrock mutex_enter(&arc_eviction_mtx); 11841544Seschrock while (arc_eviction_list != NULL) { 11851544Seschrock arc_buf_t *buf = arc_eviction_list; 11861544Seschrock arc_eviction_list = buf->b_next; 11871544Seschrock buf->b_hdr = NULL; 11881544Seschrock mutex_exit(&arc_eviction_mtx); 11891544Seschrock 11901819Smaybee if (buf->b_efunc != NULL) 11911819Smaybee VERIFY(buf->b_efunc(buf) == 0); 11921544Seschrock 11931544Seschrock buf->b_efunc = NULL; 11941544Seschrock buf->b_private = NULL; 11951544Seschrock kmem_cache_free(buf_cache, buf); 11961544Seschrock mutex_enter(&arc_eviction_mtx); 11971544Seschrock } 11981544Seschrock mutex_exit(&arc_eviction_mtx); 11991544Seschrock } 12001544Seschrock 1201789Sahrens /* 1202789Sahrens * Flush all *evictable* data from the cache. 1203789Sahrens * NOTE: this will not touch "active" (i.e. referenced) data. 1204789Sahrens */ 1205789Sahrens void 1206789Sahrens arc_flush(void) 1207789Sahrens { 12082688Smaybee while (list_head(&arc.mru->list)) 12092688Smaybee (void) arc_evict(arc.mru, -1, FALSE); 12102688Smaybee while (list_head(&arc.mfu->list)) 12112688Smaybee (void) arc_evict(arc.mfu, -1, FALSE); 1212789Sahrens 12131544Seschrock arc_evict_ghost(arc.mru_ghost, -1); 12141544Seschrock arc_evict_ghost(arc.mfu_ghost, -1); 12151544Seschrock 12161544Seschrock mutex_enter(&arc_reclaim_thr_lock); 12171544Seschrock arc_do_user_evicts(); 12181544Seschrock mutex_exit(&arc_reclaim_thr_lock); 12191544Seschrock ASSERT(arc_eviction_list == NULL); 1220789Sahrens } 1221789Sahrens 12222391Smaybee int arc_kmem_reclaim_shift = 5; /* log2(fraction of arc to reclaim) */ 12232391Smaybee 1224789Sahrens void 1225789Sahrens arc_kmem_reclaim(void) 1226789Sahrens { 12272048Sstans uint64_t to_free; 12282048Sstans 1229789Sahrens /* 1230789Sahrens * We need arc_reclaim_lock because we don't want multiple 1231789Sahrens * threads trying to reclaim concurrently. 1232789Sahrens */ 1233789Sahrens 1234789Sahrens /* 1235789Sahrens * umem calls the reclaim func when we destroy the buf cache, 1236789Sahrens * which is after we do arc_fini(). So we set a flag to prevent 1237789Sahrens * accessing the destroyed mutexes and lists. 1238789Sahrens */ 1239789Sahrens if (arc_dead) 1240789Sahrens return; 1241789Sahrens 12421544Seschrock if (arc.c <= arc.c_min) 12431544Seschrock return; 12441544Seschrock 1245789Sahrens mutex_enter(&arc_reclaim_lock); 1246789Sahrens 12472048Sstans #ifdef _KERNEL 12482391Smaybee to_free = MAX(arc.c >> arc_kmem_reclaim_shift, ptob(needfree)); 12492048Sstans #else 12502391Smaybee to_free = arc.c >> arc_kmem_reclaim_shift; 12512048Sstans #endif 12522048Sstans if (arc.c > to_free) 12532048Sstans atomic_add_64(&arc.c, -to_free); 12542048Sstans else 12552048Sstans arc.c = arc.c_min; 12562048Sstans 12572391Smaybee atomic_add_64(&arc.p, -(arc.p >> arc_kmem_reclaim_shift)); 12581544Seschrock if (arc.c > arc.size) 12591544Seschrock arc.c = arc.size; 1260789Sahrens if (arc.c < arc.c_min) 1261789Sahrens arc.c = arc.c_min; 12621544Seschrock if (arc.p > arc.c) 12631544Seschrock arc.p = (arc.c >> 1); 12641544Seschrock ASSERT((int64_t)arc.p >= 0); 1265789Sahrens 1266789Sahrens arc_adjust(); 1267789Sahrens 1268789Sahrens mutex_exit(&arc_reclaim_lock); 1269789Sahrens } 1270789Sahrens 1271789Sahrens static int 1272789Sahrens arc_reclaim_needed(void) 1273789Sahrens { 1274789Sahrens uint64_t extra; 1275789Sahrens 1276789Sahrens #ifdef _KERNEL 12772048Sstans 12782048Sstans if (needfree) 12792048Sstans return (1); 12802048Sstans 1281789Sahrens /* 1282789Sahrens * take 'desfree' extra pages, so we reclaim sooner, rather than later 1283789Sahrens */ 1284789Sahrens extra = desfree; 1285789Sahrens 1286789Sahrens /* 1287789Sahrens * check that we're out of range of the pageout scanner. It starts to 1288789Sahrens * schedule paging if freemem is less than lotsfree and needfree. 1289789Sahrens * lotsfree is the high-water mark for pageout, and needfree is the 1290789Sahrens * number of needed free pages. We add extra pages here to make sure 1291789Sahrens * the scanner doesn't start up while we're freeing memory. 1292789Sahrens */ 1293789Sahrens if (freemem < lotsfree + needfree + extra) 1294789Sahrens return (1); 1295789Sahrens 1296789Sahrens /* 1297789Sahrens * check to make sure that swapfs has enough space so that anon 1298789Sahrens * reservations can still succeeed. anon_resvmem() checks that the 1299789Sahrens * availrmem is greater than swapfs_minfree, and the number of reserved 1300789Sahrens * swap pages. We also add a bit of extra here just to prevent 1301789Sahrens * circumstances from getting really dire. 1302789Sahrens */ 1303789Sahrens if (availrmem < swapfs_minfree + swapfs_reserve + extra) 1304789Sahrens return (1); 1305789Sahrens 13061936Smaybee #if defined(__i386) 1307789Sahrens /* 1308789Sahrens * If we're on an i386 platform, it's possible that we'll exhaust the 1309789Sahrens * kernel heap space before we ever run out of available physical 1310789Sahrens * memory. Most checks of the size of the heap_area compare against 1311789Sahrens * tune.t_minarmem, which is the minimum available real memory that we 1312789Sahrens * can have in the system. However, this is generally fixed at 25 pages 1313789Sahrens * which is so low that it's useless. In this comparison, we seek to 1314789Sahrens * calculate the total heap-size, and reclaim if more than 3/4ths of the 1315789Sahrens * heap is allocated. (Or, in the caclulation, if less than 1/4th is 1316789Sahrens * free) 1317789Sahrens */ 1318789Sahrens if (btop(vmem_size(heap_arena, VMEM_FREE)) < 1319789Sahrens (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2)) 1320789Sahrens return (1); 1321789Sahrens #endif 1322789Sahrens 1323789Sahrens #else 1324789Sahrens if (spa_get_random(100) == 0) 1325789Sahrens return (1); 1326789Sahrens #endif 1327789Sahrens return (0); 1328789Sahrens } 1329789Sahrens 1330789Sahrens static void 1331789Sahrens arc_kmem_reap_now(arc_reclaim_strategy_t strat) 1332789Sahrens { 1333789Sahrens size_t i; 1334789Sahrens kmem_cache_t *prev_cache = NULL; 1335789Sahrens extern kmem_cache_t *zio_buf_cache[]; 1336789Sahrens 13371484Sek110237 #ifdef _KERNEL 13381484Sek110237 /* 13391484Sek110237 * First purge some DNLC entries, in case the DNLC is using 13401484Sek110237 * up too much memory. 13411484Sek110237 */ 13421505Sek110237 dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 13431936Smaybee 13441936Smaybee #if defined(__i386) 13451936Smaybee /* 13461936Smaybee * Reclaim unused memory from all kmem caches. 13471936Smaybee */ 13481936Smaybee kmem_reap(); 13491936Smaybee #endif 13501484Sek110237 #endif 13511484Sek110237 1352789Sahrens /* 13531544Seschrock * An agressive reclamation will shrink the cache size as well as 13541544Seschrock * reap free buffers from the arc kmem caches. 1355789Sahrens */ 1356789Sahrens if (strat == ARC_RECLAIM_AGGR) 13571544Seschrock arc_kmem_reclaim(); 1358789Sahrens 1359789Sahrens for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 1360789Sahrens if (zio_buf_cache[i] != prev_cache) { 1361789Sahrens prev_cache = zio_buf_cache[i]; 1362789Sahrens kmem_cache_reap_now(zio_buf_cache[i]); 1363789Sahrens } 1364789Sahrens } 13651544Seschrock kmem_cache_reap_now(buf_cache); 13661544Seschrock kmem_cache_reap_now(hdr_cache); 1367789Sahrens } 1368789Sahrens 1369789Sahrens static void 1370789Sahrens arc_reclaim_thread(void) 1371789Sahrens { 1372789Sahrens clock_t growtime = 0; 1373789Sahrens arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 1374789Sahrens callb_cpr_t cpr; 1375789Sahrens 1376789Sahrens CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 1377789Sahrens 1378789Sahrens mutex_enter(&arc_reclaim_thr_lock); 1379789Sahrens while (arc_thread_exit == 0) { 1380789Sahrens if (arc_reclaim_needed()) { 1381789Sahrens 1382789Sahrens if (arc.no_grow) { 1383789Sahrens if (last_reclaim == ARC_RECLAIM_CONS) { 1384789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 1385789Sahrens } else { 1386789Sahrens last_reclaim = ARC_RECLAIM_CONS; 1387789Sahrens } 1388789Sahrens } else { 1389789Sahrens arc.no_grow = TRUE; 1390789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 1391789Sahrens membar_producer(); 1392789Sahrens } 1393789Sahrens 1394789Sahrens /* reset the growth delay for every reclaim */ 1395789Sahrens growtime = lbolt + (arc_grow_retry * hz); 13962856Snd150628 ASSERT(growtime > 0); 1397789Sahrens 1398789Sahrens arc_kmem_reap_now(last_reclaim); 1399789Sahrens 1400789Sahrens } else if ((growtime > 0) && ((growtime - lbolt) <= 0)) { 1401789Sahrens arc.no_grow = FALSE; 1402789Sahrens } 1403789Sahrens 14041544Seschrock if (arc_eviction_list != NULL) 14051544Seschrock arc_do_user_evicts(); 14061544Seschrock 1407789Sahrens /* block until needed, or one second, whichever is shorter */ 1408789Sahrens CALLB_CPR_SAFE_BEGIN(&cpr); 1409789Sahrens (void) cv_timedwait(&arc_reclaim_thr_cv, 1410789Sahrens &arc_reclaim_thr_lock, (lbolt + hz)); 1411789Sahrens CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 1412789Sahrens } 1413789Sahrens 1414789Sahrens arc_thread_exit = 0; 1415789Sahrens cv_broadcast(&arc_reclaim_thr_cv); 1416789Sahrens CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 1417789Sahrens thread_exit(); 1418789Sahrens } 1419789Sahrens 14201544Seschrock /* 14211544Seschrock * Adapt arc info given the number of bytes we are trying to add and 14221544Seschrock * the state that we are comming from. This function is only called 14231544Seschrock * when we are adding new content to the cache. 14241544Seschrock */ 1425789Sahrens static void 14261544Seschrock arc_adapt(int bytes, arc_state_t *state) 1427789Sahrens { 14281544Seschrock int mult; 14291544Seschrock 14301544Seschrock ASSERT(bytes > 0); 1431789Sahrens /* 14321544Seschrock * Adapt the target size of the MRU list: 14331544Seschrock * - if we just hit in the MRU ghost list, then increase 14341544Seschrock * the target size of the MRU list. 14351544Seschrock * - if we just hit in the MFU ghost list, then increase 14361544Seschrock * the target size of the MFU list by decreasing the 14371544Seschrock * target size of the MRU list. 1438789Sahrens */ 14391544Seschrock if (state == arc.mru_ghost) { 14401544Seschrock mult = ((arc.mru_ghost->size >= arc.mfu_ghost->size) ? 14411544Seschrock 1 : (arc.mfu_ghost->size/arc.mru_ghost->size)); 14421544Seschrock 14431544Seschrock arc.p = MIN(arc.c, arc.p + bytes * mult); 14441544Seschrock } else if (state == arc.mfu_ghost) { 14451544Seschrock mult = ((arc.mfu_ghost->size >= arc.mru_ghost->size) ? 14461544Seschrock 1 : (arc.mru_ghost->size/arc.mfu_ghost->size)); 14471544Seschrock 14481544Seschrock arc.p = MAX(0, (int64_t)arc.p - bytes * mult); 14491544Seschrock } 14501544Seschrock ASSERT((int64_t)arc.p >= 0); 1451789Sahrens 1452789Sahrens if (arc_reclaim_needed()) { 1453789Sahrens cv_signal(&arc_reclaim_thr_cv); 1454789Sahrens return; 1455789Sahrens } 1456789Sahrens 1457789Sahrens if (arc.no_grow) 1458789Sahrens return; 1459789Sahrens 14601544Seschrock if (arc.c >= arc.c_max) 14611544Seschrock return; 14621544Seschrock 1463789Sahrens /* 14641544Seschrock * If we're within (2 * maxblocksize) bytes of the target 14651544Seschrock * cache size, increment the target cache size 1466789Sahrens */ 14671544Seschrock if (arc.size > arc.c - (2ULL << SPA_MAXBLOCKSHIFT)) { 14681544Seschrock atomic_add_64(&arc.c, (int64_t)bytes); 1469789Sahrens if (arc.c > arc.c_max) 1470789Sahrens arc.c = arc.c_max; 14711544Seschrock else if (state == arc.anon) 14721544Seschrock atomic_add_64(&arc.p, (int64_t)bytes); 14731544Seschrock if (arc.p > arc.c) 14741544Seschrock arc.p = arc.c; 1475789Sahrens } 14761544Seschrock ASSERT((int64_t)arc.p >= 0); 1477789Sahrens } 1478789Sahrens 1479789Sahrens /* 14801544Seschrock * Check if the cache has reached its limits and eviction is required 14811544Seschrock * prior to insert. 1482789Sahrens */ 1483789Sahrens static int 1484789Sahrens arc_evict_needed() 1485789Sahrens { 1486789Sahrens if (arc_reclaim_needed()) 1487789Sahrens return (1); 1488789Sahrens 14891544Seschrock return (arc.size > arc.c); 1490789Sahrens } 1491789Sahrens 1492789Sahrens /* 14932688Smaybee * The buffer, supplied as the first argument, needs a data block. 14942688Smaybee * So, if we are at cache max, determine which cache should be victimized. 14952688Smaybee * We have the following cases: 1496789Sahrens * 14971544Seschrock * 1. Insert for MRU, p > sizeof(arc.anon + arc.mru) -> 1498789Sahrens * In this situation if we're out of space, but the resident size of the MFU is 1499789Sahrens * under the limit, victimize the MFU cache to satisfy this insertion request. 1500789Sahrens * 15011544Seschrock * 2. Insert for MRU, p <= sizeof(arc.anon + arc.mru) -> 1502789Sahrens * Here, we've used up all of the available space for the MRU, so we need to 1503789Sahrens * evict from our own cache instead. Evict from the set of resident MRU 1504789Sahrens * entries. 1505789Sahrens * 15061544Seschrock * 3. Insert for MFU (c - p) > sizeof(arc.mfu) -> 1507789Sahrens * c minus p represents the MFU space in the cache, since p is the size of the 1508789Sahrens * cache that is dedicated to the MRU. In this situation there's still space on 1509789Sahrens * the MFU side, so the MRU side needs to be victimized. 1510789Sahrens * 15111544Seschrock * 4. Insert for MFU (c - p) < sizeof(arc.mfu) -> 1512789Sahrens * MFU's resident set is consuming more space than it has been allotted. In 1513789Sahrens * this situation, we must victimize our own cache, the MFU, for this insertion. 1514789Sahrens */ 1515789Sahrens static void 15162688Smaybee arc_get_data_buf(arc_buf_t *buf) 1517789Sahrens { 15182688Smaybee arc_state_t *state = buf->b_hdr->b_state; 15192688Smaybee uint64_t size = buf->b_hdr->b_size; 15202688Smaybee 15212688Smaybee arc_adapt(size, state); 1522789Sahrens 15232688Smaybee /* 15242688Smaybee * We have not yet reached cache maximum size, 15252688Smaybee * just allocate a new buffer. 15262688Smaybee */ 15272688Smaybee if (!arc_evict_needed()) { 15282688Smaybee buf->b_data = zio_buf_alloc(size); 15292688Smaybee atomic_add_64(&arc.size, size); 15302688Smaybee goto out; 15312688Smaybee } 15322688Smaybee 15332688Smaybee /* 15342688Smaybee * If we are prefetching from the mfu ghost list, this buffer 15352688Smaybee * will end up on the mru list; so steal space from there. 15362688Smaybee */ 15372688Smaybee if (state == arc.mfu_ghost) 15382688Smaybee state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc.mru : arc.mfu; 15392688Smaybee else if (state == arc.mru_ghost) 15402688Smaybee state = arc.mru; 1541789Sahrens 15422688Smaybee if (state == arc.mru || state == arc.anon) { 15432688Smaybee uint64_t mru_used = arc.anon->size + arc.mru->size; 15442688Smaybee state = (arc.p > mru_used) ? arc.mfu : arc.mru; 1545789Sahrens } else { 15462688Smaybee /* MFU cases */ 15472688Smaybee uint64_t mfu_space = arc.c - arc.p; 15482688Smaybee state = (mfu_space > arc.mfu->size) ? arc.mru : arc.mfu; 15492688Smaybee } 15502688Smaybee if ((buf->b_data = arc_evict(state, size, TRUE)) == NULL) { 15512688Smaybee buf->b_data = zio_buf_alloc(size); 15522688Smaybee atomic_add_64(&arc.size, size); 15532688Smaybee atomic_add_64(&arc.recycle_miss, 1); 15542688Smaybee } 15552688Smaybee ASSERT(buf->b_data != NULL); 15562688Smaybee out: 15572688Smaybee /* 15582688Smaybee * Update the state size. Note that ghost states have a 15592688Smaybee * "ghost size" and so don't need to be updated. 15602688Smaybee */ 15612688Smaybee if (!GHOST_STATE(buf->b_hdr->b_state)) { 15622688Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 15632688Smaybee 15642688Smaybee atomic_add_64(&hdr->b_state->size, size); 15652688Smaybee if (list_link_active(&hdr->b_arc_node)) { 15662688Smaybee ASSERT(refcount_is_zero(&hdr->b_refcnt)); 15672688Smaybee atomic_add_64(&hdr->b_state->lsize, size); 1568789Sahrens } 1569789Sahrens } 1570789Sahrens } 1571789Sahrens 1572789Sahrens /* 1573789Sahrens * This routine is called whenever a buffer is accessed. 15741544Seschrock * NOTE: the hash lock is dropped in this function. 1575789Sahrens */ 1576789Sahrens static void 15772688Smaybee arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock) 1578789Sahrens { 1579789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 1580789Sahrens 1581789Sahrens if (buf->b_state == arc.anon) { 1582789Sahrens /* 1583789Sahrens * This buffer is not in the cache, and does not 1584789Sahrens * appear in our "ghost" list. Add the new buffer 1585789Sahrens * to the MRU state. 1586789Sahrens */ 1587789Sahrens 1588789Sahrens ASSERT(buf->b_arc_access == 0); 1589789Sahrens buf->b_arc_access = lbolt; 15901544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 15911544Seschrock arc_change_state(arc.mru, buf, hash_lock); 1592789Sahrens 15931544Seschrock } else if (buf->b_state == arc.mru) { 1594789Sahrens /* 15952391Smaybee * If this buffer is here because of a prefetch, then either: 15962391Smaybee * - clear the flag if this is a "referencing" read 15972391Smaybee * (any subsequent access will bump this into the MFU state). 15982391Smaybee * or 15992391Smaybee * - move the buffer to the head of the list if this is 16002391Smaybee * another prefetch (to make it less likely to be evicted). 1601789Sahrens */ 1602789Sahrens if ((buf->b_flags & ARC_PREFETCH) != 0) { 16032391Smaybee if (refcount_count(&buf->b_refcnt) == 0) { 16042391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 16052391Smaybee mutex_enter(&arc.mru->mtx); 16062391Smaybee list_remove(&arc.mru->list, buf); 16072391Smaybee list_insert_head(&arc.mru->list, buf); 16082391Smaybee mutex_exit(&arc.mru->mtx); 16092391Smaybee } else { 16102391Smaybee buf->b_flags &= ~ARC_PREFETCH; 16112391Smaybee atomic_add_64(&arc.mru->hits, 1); 16122391Smaybee } 16132391Smaybee buf->b_arc_access = lbolt; 1614789Sahrens return; 1615789Sahrens } 1616789Sahrens 1617789Sahrens /* 1618789Sahrens * This buffer has been "accessed" only once so far, 1619789Sahrens * but it is still in the cache. Move it to the MFU 1620789Sahrens * state. 1621789Sahrens */ 1622789Sahrens if (lbolt > buf->b_arc_access + ARC_MINTIME) { 1623789Sahrens /* 1624789Sahrens * More than 125ms have passed since we 1625789Sahrens * instantiated this buffer. Move it to the 1626789Sahrens * most frequently used state. 1627789Sahrens */ 1628789Sahrens buf->b_arc_access = lbolt; 16291544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 16301544Seschrock arc_change_state(arc.mfu, buf, hash_lock); 1631789Sahrens } 16321544Seschrock atomic_add_64(&arc.mru->hits, 1); 16331544Seschrock } else if (buf->b_state == arc.mru_ghost) { 1634789Sahrens arc_state_t *new_state; 1635789Sahrens /* 1636789Sahrens * This buffer has been "accessed" recently, but 1637789Sahrens * was evicted from the cache. Move it to the 1638789Sahrens * MFU state. 1639789Sahrens */ 1640789Sahrens 1641789Sahrens if (buf->b_flags & ARC_PREFETCH) { 16421544Seschrock new_state = arc.mru; 16432391Smaybee if (refcount_count(&buf->b_refcnt) > 0) 16442391Smaybee buf->b_flags &= ~ARC_PREFETCH; 16451544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 1646789Sahrens } else { 16471544Seschrock new_state = arc.mfu; 16481544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 1649789Sahrens } 1650789Sahrens 1651789Sahrens buf->b_arc_access = lbolt; 1652789Sahrens arc_change_state(new_state, buf, hash_lock); 1653789Sahrens 16541544Seschrock atomic_add_64(&arc.mru_ghost->hits, 1); 16551544Seschrock } else if (buf->b_state == arc.mfu) { 1656789Sahrens /* 1657789Sahrens * This buffer has been accessed more than once and is 1658789Sahrens * still in the cache. Keep it in the MFU state. 1659789Sahrens * 16602391Smaybee * NOTE: an add_reference() that occurred when we did 16612391Smaybee * the arc_read() will have kicked this off the list. 16622391Smaybee * If it was a prefetch, we will explicitly move it to 16632391Smaybee * the head of the list now. 1664789Sahrens */ 16652391Smaybee if ((buf->b_flags & ARC_PREFETCH) != 0) { 16662391Smaybee ASSERT(refcount_count(&buf->b_refcnt) == 0); 16672391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 16682391Smaybee mutex_enter(&arc.mfu->mtx); 16692391Smaybee list_remove(&arc.mfu->list, buf); 16702391Smaybee list_insert_head(&arc.mfu->list, buf); 16712391Smaybee mutex_exit(&arc.mfu->mtx); 16722391Smaybee } 16731544Seschrock atomic_add_64(&arc.mfu->hits, 1); 16742391Smaybee buf->b_arc_access = lbolt; 16751544Seschrock } else if (buf->b_state == arc.mfu_ghost) { 16762391Smaybee arc_state_t *new_state = arc.mfu; 1677789Sahrens /* 1678789Sahrens * This buffer has been accessed more than once but has 1679789Sahrens * been evicted from the cache. Move it back to the 1680789Sahrens * MFU state. 1681789Sahrens */ 1682789Sahrens 16832391Smaybee if (buf->b_flags & ARC_PREFETCH) { 16842391Smaybee /* 16852391Smaybee * This is a prefetch access... 16862391Smaybee * move this block back to the MRU state. 16872391Smaybee */ 16882391Smaybee ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0); 16892391Smaybee new_state = arc.mru; 16902391Smaybee } 16912391Smaybee 1692789Sahrens buf->b_arc_access = lbolt; 16931544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 16942391Smaybee arc_change_state(new_state, buf, hash_lock); 1695789Sahrens 16961544Seschrock atomic_add_64(&arc.mfu_ghost->hits, 1); 1697789Sahrens } else { 1698789Sahrens ASSERT(!"invalid arc state"); 1699789Sahrens } 1700789Sahrens } 1701789Sahrens 1702789Sahrens /* a generic arc_done_func_t which you can use */ 1703789Sahrens /* ARGSUSED */ 1704789Sahrens void 1705789Sahrens arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 1706789Sahrens { 1707789Sahrens bcopy(buf->b_data, arg, buf->b_hdr->b_size); 17081544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 1709789Sahrens } 1710789Sahrens 1711789Sahrens /* a generic arc_done_func_t which you can use */ 1712789Sahrens void 1713789Sahrens arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 1714789Sahrens { 1715789Sahrens arc_buf_t **bufp = arg; 1716789Sahrens if (zio && zio->io_error) { 17171544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 1718789Sahrens *bufp = NULL; 1719789Sahrens } else { 1720789Sahrens *bufp = buf; 1721789Sahrens } 1722789Sahrens } 1723789Sahrens 1724789Sahrens static void 1725789Sahrens arc_read_done(zio_t *zio) 1726789Sahrens { 17271589Smaybee arc_buf_hdr_t *hdr, *found; 1728789Sahrens arc_buf_t *buf; 1729789Sahrens arc_buf_t *abuf; /* buffer we're assigning to callback */ 1730789Sahrens kmutex_t *hash_lock; 1731789Sahrens arc_callback_t *callback_list, *acb; 1732789Sahrens int freeable = FALSE; 1733789Sahrens 1734789Sahrens buf = zio->io_private; 1735789Sahrens hdr = buf->b_hdr; 1736789Sahrens 17371589Smaybee /* 17381589Smaybee * The hdr was inserted into hash-table and removed from lists 17391589Smaybee * prior to starting I/O. We should find this header, since 17401589Smaybee * it's in the hash table, and it should be legit since it's 17411589Smaybee * not possible to evict it during the I/O. The only possible 17421589Smaybee * reason for it not to be found is if we were freed during the 17431589Smaybee * read. 17441589Smaybee */ 17451589Smaybee found = buf_hash_find(zio->io_spa, &hdr->b_dva, hdr->b_birth, 1746*3093Sahrens &hash_lock); 1747789Sahrens 17481589Smaybee ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) || 17491589Smaybee (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp)))); 1750789Sahrens 1751789Sahrens /* byteswap if necessary */ 1752789Sahrens callback_list = hdr->b_acb; 1753789Sahrens ASSERT(callback_list != NULL); 1754789Sahrens if (BP_SHOULD_BYTESWAP(zio->io_bp) && callback_list->acb_byteswap) 1755789Sahrens callback_list->acb_byteswap(buf->b_data, hdr->b_size); 1756789Sahrens 1757*3093Sahrens arc_cksum_compute(buf); 1758*3093Sahrens 1759789Sahrens /* create copies of the data buffer for the callers */ 1760789Sahrens abuf = buf; 1761789Sahrens for (acb = callback_list; acb; acb = acb->acb_next) { 1762789Sahrens if (acb->acb_done) { 17632688Smaybee if (abuf == NULL) 17642688Smaybee abuf = arc_buf_clone(buf); 1765789Sahrens acb->acb_buf = abuf; 1766789Sahrens abuf = NULL; 1767789Sahrens } 1768789Sahrens } 1769789Sahrens hdr->b_acb = NULL; 1770789Sahrens hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 17711544Seschrock ASSERT(!HDR_BUF_AVAILABLE(hdr)); 17721544Seschrock if (abuf == buf) 17731544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 1774789Sahrens 1775789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL); 1776789Sahrens 1777789Sahrens if (zio->io_error != 0) { 1778789Sahrens hdr->b_flags |= ARC_IO_ERROR; 1779789Sahrens if (hdr->b_state != arc.anon) 1780789Sahrens arc_change_state(arc.anon, hdr, hash_lock); 17811544Seschrock if (HDR_IN_HASH_TABLE(hdr)) 17821544Seschrock buf_hash_remove(hdr); 1783789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 17842391Smaybee /* convert checksum errors into IO errors */ 17851544Seschrock if (zio->io_error == ECKSUM) 17861544Seschrock zio->io_error = EIO; 1787789Sahrens } 1788789Sahrens 17891544Seschrock /* 17902391Smaybee * Broadcast before we drop the hash_lock to avoid the possibility 17912391Smaybee * that the hdr (and hence the cv) might be freed before we get to 17922391Smaybee * the cv_broadcast(). 17931544Seschrock */ 17941544Seschrock cv_broadcast(&hdr->b_cv); 17951544Seschrock 17961589Smaybee if (hash_lock) { 1797789Sahrens /* 1798789Sahrens * Only call arc_access on anonymous buffers. This is because 1799789Sahrens * if we've issued an I/O for an evicted buffer, we've already 1800789Sahrens * called arc_access (to prevent any simultaneous readers from 1801789Sahrens * getting confused). 1802789Sahrens */ 1803789Sahrens if (zio->io_error == 0 && hdr->b_state == arc.anon) 18042688Smaybee arc_access(hdr, hash_lock); 18052688Smaybee mutex_exit(hash_lock); 1806789Sahrens } else { 1807789Sahrens /* 1808789Sahrens * This block was freed while we waited for the read to 1809789Sahrens * complete. It has been removed from the hash table and 1810789Sahrens * moved to the anonymous state (so that it won't show up 1811789Sahrens * in the cache). 1812789Sahrens */ 1813789Sahrens ASSERT3P(hdr->b_state, ==, arc.anon); 1814789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 1815789Sahrens } 1816789Sahrens 1817789Sahrens /* execute each callback and free its structure */ 1818789Sahrens while ((acb = callback_list) != NULL) { 1819789Sahrens if (acb->acb_done) 1820789Sahrens acb->acb_done(zio, acb->acb_buf, acb->acb_private); 1821789Sahrens 1822789Sahrens if (acb->acb_zio_dummy != NULL) { 1823789Sahrens acb->acb_zio_dummy->io_error = zio->io_error; 1824789Sahrens zio_nowait(acb->acb_zio_dummy); 1825789Sahrens } 1826789Sahrens 1827789Sahrens callback_list = acb->acb_next; 1828789Sahrens kmem_free(acb, sizeof (arc_callback_t)); 1829789Sahrens } 1830789Sahrens 1831789Sahrens if (freeable) 18321544Seschrock arc_hdr_destroy(hdr); 1833789Sahrens } 1834789Sahrens 1835789Sahrens /* 1836789Sahrens * "Read" the block block at the specified DVA (in bp) via the 1837789Sahrens * cache. If the block is found in the cache, invoke the provided 1838789Sahrens * callback immediately and return. Note that the `zio' parameter 1839789Sahrens * in the callback will be NULL in this case, since no IO was 1840789Sahrens * required. If the block is not in the cache pass the read request 1841789Sahrens * on to the spa with a substitute callback function, so that the 1842789Sahrens * requested block will be added to the cache. 1843789Sahrens * 1844789Sahrens * If a read request arrives for a block that has a read in-progress, 1845789Sahrens * either wait for the in-progress read to complete (and return the 1846789Sahrens * results); or, if this is a read with a "done" func, add a record 1847789Sahrens * to the read to invoke the "done" func when the read completes, 1848789Sahrens * and return; or just return. 1849789Sahrens * 1850789Sahrens * arc_read_done() will invoke all the requested "done" functions 1851789Sahrens * for readers of this block. 1852789Sahrens */ 1853789Sahrens int 1854789Sahrens arc_read(zio_t *pio, spa_t *spa, blkptr_t *bp, arc_byteswap_func_t *swap, 1855789Sahrens arc_done_func_t *done, void *private, int priority, int flags, 18562391Smaybee uint32_t *arc_flags, zbookmark_t *zb) 1857789Sahrens { 1858789Sahrens arc_buf_hdr_t *hdr; 1859789Sahrens arc_buf_t *buf; 1860789Sahrens kmutex_t *hash_lock; 1861789Sahrens zio_t *rzio; 1862789Sahrens 1863789Sahrens top: 1864789Sahrens hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock); 18651544Seschrock if (hdr && hdr->b_datacnt > 0) { 1866789Sahrens 18672391Smaybee *arc_flags |= ARC_CACHED; 18682391Smaybee 1869789Sahrens if (HDR_IO_IN_PROGRESS(hdr)) { 18702391Smaybee 18712391Smaybee if (*arc_flags & ARC_WAIT) { 18722391Smaybee cv_wait(&hdr->b_cv, hash_lock); 18732391Smaybee mutex_exit(hash_lock); 18742391Smaybee goto top; 18752391Smaybee } 18762391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 18772391Smaybee 18782391Smaybee if (done) { 1879789Sahrens arc_callback_t *acb = NULL; 1880789Sahrens 1881789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), 1882789Sahrens KM_SLEEP); 1883789Sahrens acb->acb_done = done; 1884789Sahrens acb->acb_private = private; 1885789Sahrens acb->acb_byteswap = swap; 1886789Sahrens if (pio != NULL) 1887789Sahrens acb->acb_zio_dummy = zio_null(pio, 1888789Sahrens spa, NULL, NULL, flags); 1889789Sahrens 1890789Sahrens ASSERT(acb->acb_done != NULL); 1891789Sahrens acb->acb_next = hdr->b_acb; 1892789Sahrens hdr->b_acb = acb; 1893789Sahrens add_reference(hdr, hash_lock, private); 1894789Sahrens mutex_exit(hash_lock); 1895789Sahrens return (0); 1896789Sahrens } 1897789Sahrens mutex_exit(hash_lock); 1898789Sahrens return (0); 1899789Sahrens } 1900789Sahrens 19011544Seschrock ASSERT(hdr->b_state == arc.mru || hdr->b_state == arc.mfu); 1902789Sahrens 19031544Seschrock if (done) { 19042688Smaybee add_reference(hdr, hash_lock, private); 19051544Seschrock /* 19061544Seschrock * If this block is already in use, create a new 19071544Seschrock * copy of the data so that we will be guaranteed 19081544Seschrock * that arc_release() will always succeed. 19091544Seschrock */ 19101544Seschrock buf = hdr->b_buf; 19111544Seschrock ASSERT(buf); 19121544Seschrock ASSERT(buf->b_data); 19132688Smaybee if (HDR_BUF_AVAILABLE(hdr)) { 19141544Seschrock ASSERT(buf->b_efunc == NULL); 19151544Seschrock hdr->b_flags &= ~ARC_BUF_AVAILABLE; 19162688Smaybee } else { 19172688Smaybee buf = arc_buf_clone(buf); 19181544Seschrock } 19192391Smaybee } else if (*arc_flags & ARC_PREFETCH && 19202391Smaybee refcount_count(&hdr->b_refcnt) == 0) { 19212391Smaybee hdr->b_flags |= ARC_PREFETCH; 1922789Sahrens } 1923789Sahrens DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 19242688Smaybee arc_access(hdr, hash_lock); 19252688Smaybee mutex_exit(hash_lock); 1926789Sahrens atomic_add_64(&arc.hits, 1); 1927789Sahrens if (done) 1928789Sahrens done(NULL, buf, private); 1929789Sahrens } else { 1930789Sahrens uint64_t size = BP_GET_LSIZE(bp); 1931789Sahrens arc_callback_t *acb; 1932789Sahrens 1933789Sahrens if (hdr == NULL) { 1934789Sahrens /* this block is not in the cache */ 1935789Sahrens arc_buf_hdr_t *exists; 1936789Sahrens 1937789Sahrens buf = arc_buf_alloc(spa, size, private); 1938789Sahrens hdr = buf->b_hdr; 1939789Sahrens hdr->b_dva = *BP_IDENTITY(bp); 1940789Sahrens hdr->b_birth = bp->blk_birth; 1941789Sahrens hdr->b_cksum0 = bp->blk_cksum.zc_word[0]; 1942789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 1943789Sahrens if (exists) { 1944789Sahrens /* somebody beat us to the hash insert */ 1945789Sahrens mutex_exit(hash_lock); 1946789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 1947789Sahrens hdr->b_birth = 0; 1948789Sahrens hdr->b_cksum0 = 0; 19491544Seschrock (void) arc_buf_remove_ref(buf, private); 1950789Sahrens goto top; /* restart the IO request */ 1951789Sahrens } 19522391Smaybee /* if this is a prefetch, we don't have a reference */ 19532391Smaybee if (*arc_flags & ARC_PREFETCH) { 19542391Smaybee (void) remove_reference(hdr, hash_lock, 19552391Smaybee private); 19562391Smaybee hdr->b_flags |= ARC_PREFETCH; 19572391Smaybee } 19582391Smaybee if (BP_GET_LEVEL(bp) > 0) 19592391Smaybee hdr->b_flags |= ARC_INDIRECT; 1960789Sahrens } else { 1961789Sahrens /* this block is in the ghost cache */ 19621544Seschrock ASSERT(GHOST_STATE(hdr->b_state)); 19631544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 19642391Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0); 19652391Smaybee ASSERT(hdr->b_buf == NULL); 1966789Sahrens 19672391Smaybee /* if this is a prefetch, we don't have a reference */ 19682391Smaybee if (*arc_flags & ARC_PREFETCH) 19692391Smaybee hdr->b_flags |= ARC_PREFETCH; 19702391Smaybee else 19712391Smaybee add_reference(hdr, hash_lock, private); 1972789Sahrens buf = kmem_cache_alloc(buf_cache, KM_SLEEP); 19731544Seschrock buf->b_hdr = hdr; 19742688Smaybee buf->b_data = NULL; 19751544Seschrock buf->b_efunc = NULL; 19761544Seschrock buf->b_private = NULL; 19771544Seschrock buf->b_next = NULL; 19781544Seschrock hdr->b_buf = buf; 19792688Smaybee arc_get_data_buf(buf); 19801544Seschrock ASSERT(hdr->b_datacnt == 0); 19811544Seschrock hdr->b_datacnt = 1; 19822391Smaybee 1983789Sahrens } 1984789Sahrens 1985789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 1986789Sahrens acb->acb_done = done; 1987789Sahrens acb->acb_private = private; 1988789Sahrens acb->acb_byteswap = swap; 1989789Sahrens 1990789Sahrens ASSERT(hdr->b_acb == NULL); 1991789Sahrens hdr->b_acb = acb; 1992789Sahrens hdr->b_flags |= ARC_IO_IN_PROGRESS; 1993789Sahrens 1994789Sahrens /* 1995789Sahrens * If the buffer has been evicted, migrate it to a present state 1996789Sahrens * before issuing the I/O. Once we drop the hash-table lock, 1997789Sahrens * the header will be marked as I/O in progress and have an 1998789Sahrens * attached buffer. At this point, anybody who finds this 1999789Sahrens * buffer ought to notice that it's legit but has a pending I/O. 2000789Sahrens */ 2001789Sahrens 20021544Seschrock if (GHOST_STATE(hdr->b_state)) 20032688Smaybee arc_access(hdr, hash_lock); 20042688Smaybee mutex_exit(hash_lock); 2005789Sahrens 2006789Sahrens ASSERT3U(hdr->b_size, ==, size); 20071596Sahrens DTRACE_PROBE3(arc__miss, blkptr_t *, bp, uint64_t, size, 20081596Sahrens zbookmark_t *, zb); 2009789Sahrens atomic_add_64(&arc.misses, 1); 20101544Seschrock 2011789Sahrens rzio = zio_read(pio, spa, bp, buf->b_data, size, 20121544Seschrock arc_read_done, buf, priority, flags, zb); 2013789Sahrens 20142391Smaybee if (*arc_flags & ARC_WAIT) 2015789Sahrens return (zio_wait(rzio)); 2016789Sahrens 20172391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 2018789Sahrens zio_nowait(rzio); 2019789Sahrens } 2020789Sahrens return (0); 2021789Sahrens } 2022789Sahrens 2023789Sahrens /* 2024789Sahrens * arc_read() variant to support pool traversal. If the block is already 2025789Sahrens * in the ARC, make a copy of it; otherwise, the caller will do the I/O. 2026789Sahrens * The idea is that we don't want pool traversal filling up memory, but 2027789Sahrens * if the ARC already has the data anyway, we shouldn't pay for the I/O. 2028789Sahrens */ 2029789Sahrens int 2030789Sahrens arc_tryread(spa_t *spa, blkptr_t *bp, void *data) 2031789Sahrens { 2032789Sahrens arc_buf_hdr_t *hdr; 2033789Sahrens kmutex_t *hash_mtx; 2034789Sahrens int rc = 0; 2035789Sahrens 2036789Sahrens hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_mtx); 2037789Sahrens 20381544Seschrock if (hdr && hdr->b_datacnt > 0 && !HDR_IO_IN_PROGRESS(hdr)) { 20391544Seschrock arc_buf_t *buf = hdr->b_buf; 20401544Seschrock 20411544Seschrock ASSERT(buf); 20421544Seschrock while (buf->b_data == NULL) { 20431544Seschrock buf = buf->b_next; 20441544Seschrock ASSERT(buf); 20451544Seschrock } 20461544Seschrock bcopy(buf->b_data, data, hdr->b_size); 20471544Seschrock } else { 2048789Sahrens rc = ENOENT; 20491544Seschrock } 2050789Sahrens 2051789Sahrens if (hash_mtx) 2052789Sahrens mutex_exit(hash_mtx); 2053789Sahrens 2054789Sahrens return (rc); 2055789Sahrens } 2056789Sahrens 20571544Seschrock void 20581544Seschrock arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 20591544Seschrock { 20601544Seschrock ASSERT(buf->b_hdr != NULL); 20611544Seschrock ASSERT(buf->b_hdr->b_state != arc.anon); 20621544Seschrock ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL); 20631544Seschrock buf->b_efunc = func; 20641544Seschrock buf->b_private = private; 20651544Seschrock } 20661544Seschrock 20671544Seschrock /* 20681544Seschrock * This is used by the DMU to let the ARC know that a buffer is 20691544Seschrock * being evicted, so the ARC should clean up. If this arc buf 20701544Seschrock * is not yet in the evicted state, it will be put there. 20711544Seschrock */ 20721544Seschrock int 20731544Seschrock arc_buf_evict(arc_buf_t *buf) 20741544Seschrock { 20752887Smaybee arc_buf_hdr_t *hdr; 20761544Seschrock kmutex_t *hash_lock; 20771544Seschrock arc_buf_t **bufp; 20781544Seschrock 20792887Smaybee mutex_enter(&arc_eviction_mtx); 20802887Smaybee hdr = buf->b_hdr; 20811544Seschrock if (hdr == NULL) { 20821544Seschrock /* 20831544Seschrock * We are in arc_do_user_evicts(). 20841544Seschrock */ 20851544Seschrock ASSERT(buf->b_data == NULL); 20862887Smaybee mutex_exit(&arc_eviction_mtx); 20871544Seschrock return (0); 20881544Seschrock } 20892887Smaybee hash_lock = HDR_LOCK(hdr); 20902887Smaybee mutex_exit(&arc_eviction_mtx); 20911544Seschrock 20921544Seschrock mutex_enter(hash_lock); 20931544Seschrock 20942724Smaybee if (buf->b_data == NULL) { 20952724Smaybee /* 20962724Smaybee * We are on the eviction list. 20972724Smaybee */ 20982724Smaybee mutex_exit(hash_lock); 20992724Smaybee mutex_enter(&arc_eviction_mtx); 21002724Smaybee if (buf->b_hdr == NULL) { 21012724Smaybee /* 21022724Smaybee * We are already in arc_do_user_evicts(). 21032724Smaybee */ 21042724Smaybee mutex_exit(&arc_eviction_mtx); 21052724Smaybee return (0); 21062724Smaybee } else { 21072724Smaybee arc_buf_t copy = *buf; /* structure assignment */ 21082724Smaybee /* 21092724Smaybee * Process this buffer now 21102724Smaybee * but let arc_do_user_evicts() do the reaping. 21112724Smaybee */ 21122724Smaybee buf->b_efunc = NULL; 21132724Smaybee mutex_exit(&arc_eviction_mtx); 21142724Smaybee VERIFY(copy.b_efunc(©) == 0); 21152724Smaybee return (1); 21162724Smaybee } 21172724Smaybee } 21182724Smaybee 21192724Smaybee ASSERT(buf->b_hdr == hdr); 21202724Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt); 21211544Seschrock ASSERT(hdr->b_state == arc.mru || hdr->b_state == arc.mfu); 21221544Seschrock 21231544Seschrock /* 21241544Seschrock * Pull this buffer off of the hdr 21251544Seschrock */ 21261544Seschrock bufp = &hdr->b_buf; 21271544Seschrock while (*bufp != buf) 21281544Seschrock bufp = &(*bufp)->b_next; 21291544Seschrock *bufp = buf->b_next; 21301544Seschrock 21311544Seschrock ASSERT(buf->b_data != NULL); 21322688Smaybee arc_buf_destroy(buf, FALSE, FALSE); 21331544Seschrock 21341544Seschrock if (hdr->b_datacnt == 0) { 21351544Seschrock arc_state_t *old_state = hdr->b_state; 21361544Seschrock arc_state_t *evicted_state; 21371544Seschrock 21381544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 21391544Seschrock 21401544Seschrock evicted_state = 21411544Seschrock (old_state == arc.mru) ? arc.mru_ghost : arc.mfu_ghost; 21421544Seschrock 21431544Seschrock mutex_enter(&old_state->mtx); 21441544Seschrock mutex_enter(&evicted_state->mtx); 21451544Seschrock 21461544Seschrock arc_change_state(evicted_state, hdr, hash_lock); 21471544Seschrock ASSERT(HDR_IN_HASH_TABLE(hdr)); 21481544Seschrock hdr->b_flags = ARC_IN_HASH_TABLE; 21491544Seschrock 21501544Seschrock mutex_exit(&evicted_state->mtx); 21511544Seschrock mutex_exit(&old_state->mtx); 21521544Seschrock } 21531544Seschrock mutex_exit(hash_lock); 21541819Smaybee 21551544Seschrock VERIFY(buf->b_efunc(buf) == 0); 21561544Seschrock buf->b_efunc = NULL; 21571544Seschrock buf->b_private = NULL; 21581544Seschrock buf->b_hdr = NULL; 21591544Seschrock kmem_cache_free(buf_cache, buf); 21601544Seschrock return (1); 21611544Seschrock } 21621544Seschrock 2163789Sahrens /* 2164789Sahrens * Release this buffer from the cache. This must be done 2165789Sahrens * after a read and prior to modifying the buffer contents. 2166789Sahrens * If the buffer has more than one reference, we must make 2167789Sahrens * make a new hdr for the buffer. 2168789Sahrens */ 2169789Sahrens void 2170789Sahrens arc_release(arc_buf_t *buf, void *tag) 2171789Sahrens { 2172789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 2173789Sahrens kmutex_t *hash_lock = HDR_LOCK(hdr); 2174789Sahrens 2175789Sahrens /* this buffer is not on any list */ 2176789Sahrens ASSERT(refcount_count(&hdr->b_refcnt) > 0); 2177789Sahrens 2178789Sahrens if (hdr->b_state == arc.anon) { 2179789Sahrens /* this buffer is already released */ 2180789Sahrens ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 1); 2181789Sahrens ASSERT(BUF_EMPTY(hdr)); 21821544Seschrock ASSERT(buf->b_efunc == NULL); 2183*3093Sahrens arc_buf_thaw(buf); 2184789Sahrens return; 2185789Sahrens } 2186789Sahrens 2187789Sahrens mutex_enter(hash_lock); 2188789Sahrens 21891544Seschrock /* 21901544Seschrock * Do we have more than one buf? 21911544Seschrock */ 21921544Seschrock if (hdr->b_buf != buf || buf->b_next != NULL) { 2193789Sahrens arc_buf_hdr_t *nhdr; 2194789Sahrens arc_buf_t **bufp; 2195789Sahrens uint64_t blksz = hdr->b_size; 2196789Sahrens spa_t *spa = hdr->b_spa; 2197789Sahrens 21981544Seschrock ASSERT(hdr->b_datacnt > 1); 2199789Sahrens /* 2200789Sahrens * Pull the data off of this buf and attach it to 2201789Sahrens * a new anonymous buf. 2202789Sahrens */ 22031544Seschrock (void) remove_reference(hdr, hash_lock, tag); 2204789Sahrens bufp = &hdr->b_buf; 22051544Seschrock while (*bufp != buf) 2206789Sahrens bufp = &(*bufp)->b_next; 2207789Sahrens *bufp = (*bufp)->b_next; 22081544Seschrock 2209789Sahrens ASSERT3U(hdr->b_state->size, >=, hdr->b_size); 2210789Sahrens atomic_add_64(&hdr->b_state->size, -hdr->b_size); 22111544Seschrock if (refcount_is_zero(&hdr->b_refcnt)) { 22121544Seschrock ASSERT3U(hdr->b_state->lsize, >=, hdr->b_size); 22131544Seschrock atomic_add_64(&hdr->b_state->lsize, -hdr->b_size); 22141544Seschrock } 22151544Seschrock hdr->b_datacnt -= 1; 22161544Seschrock 2217789Sahrens mutex_exit(hash_lock); 2218789Sahrens 2219789Sahrens nhdr = kmem_cache_alloc(hdr_cache, KM_SLEEP); 2220789Sahrens nhdr->b_size = blksz; 2221789Sahrens nhdr->b_spa = spa; 2222789Sahrens nhdr->b_buf = buf; 2223789Sahrens nhdr->b_state = arc.anon; 2224789Sahrens nhdr->b_arc_access = 0; 2225789Sahrens nhdr->b_flags = 0; 22261544Seschrock nhdr->b_datacnt = 1; 2227*3093Sahrens nhdr->b_freeze_cksum = 2228*3093Sahrens kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 2229*3093Sahrens *nhdr->b_freeze_cksum = *hdr->b_freeze_cksum; /* struct copy */ 2230789Sahrens buf->b_hdr = nhdr; 2231789Sahrens buf->b_next = NULL; 2232789Sahrens (void) refcount_add(&nhdr->b_refcnt, tag); 2233789Sahrens atomic_add_64(&arc.anon->size, blksz); 2234789Sahrens 2235789Sahrens hdr = nhdr; 2236789Sahrens } else { 22371544Seschrock ASSERT(refcount_count(&hdr->b_refcnt) == 1); 2238789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 2239789Sahrens ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 2240789Sahrens arc_change_state(arc.anon, hdr, hash_lock); 2241789Sahrens hdr->b_arc_access = 0; 2242789Sahrens mutex_exit(hash_lock); 2243789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 2244789Sahrens hdr->b_birth = 0; 2245789Sahrens hdr->b_cksum0 = 0; 2246789Sahrens } 22471544Seschrock buf->b_efunc = NULL; 22481544Seschrock buf->b_private = NULL; 2249*3093Sahrens arc_buf_thaw(buf); 2250789Sahrens } 2251789Sahrens 2252789Sahrens int 2253789Sahrens arc_released(arc_buf_t *buf) 2254789Sahrens { 22551544Seschrock return (buf->b_data != NULL && buf->b_hdr->b_state == arc.anon); 22561544Seschrock } 22571544Seschrock 22581544Seschrock int 22591544Seschrock arc_has_callback(arc_buf_t *buf) 22601544Seschrock { 22611544Seschrock return (buf->b_efunc != NULL); 2262789Sahrens } 2263789Sahrens 22641544Seschrock #ifdef ZFS_DEBUG 22651544Seschrock int 22661544Seschrock arc_referenced(arc_buf_t *buf) 22671544Seschrock { 22681544Seschrock return (refcount_count(&buf->b_hdr->b_refcnt)); 22691544Seschrock } 22701544Seschrock #endif 22711544Seschrock 2272789Sahrens static void 2273789Sahrens arc_write_done(zio_t *zio) 2274789Sahrens { 2275789Sahrens arc_buf_t *buf; 2276789Sahrens arc_buf_hdr_t *hdr; 2277789Sahrens arc_callback_t *acb; 2278789Sahrens 2279789Sahrens buf = zio->io_private; 2280789Sahrens hdr = buf->b_hdr; 2281789Sahrens acb = hdr->b_acb; 2282789Sahrens hdr->b_acb = NULL; 22831544Seschrock ASSERT(acb != NULL); 2284789Sahrens 2285789Sahrens /* this buffer is on no lists and is not in the hash table */ 2286789Sahrens ASSERT3P(hdr->b_state, ==, arc.anon); 2287789Sahrens 2288789Sahrens hdr->b_dva = *BP_IDENTITY(zio->io_bp); 2289789Sahrens hdr->b_birth = zio->io_bp->blk_birth; 2290789Sahrens hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0]; 22911544Seschrock /* 22921544Seschrock * If the block to be written was all-zero, we may have 22931544Seschrock * compressed it away. In this case no write was performed 22941544Seschrock * so there will be no dva/birth-date/checksum. The buffer 22951544Seschrock * must therefor remain anonymous (and uncached). 22961544Seschrock */ 2297789Sahrens if (!BUF_EMPTY(hdr)) { 2298789Sahrens arc_buf_hdr_t *exists; 2299789Sahrens kmutex_t *hash_lock; 2300789Sahrens 2301*3093Sahrens arc_cksum_verify(buf); 2302*3093Sahrens 2303789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 2304789Sahrens if (exists) { 2305789Sahrens /* 2306789Sahrens * This can only happen if we overwrite for 2307789Sahrens * sync-to-convergence, because we remove 2308789Sahrens * buffers from the hash table when we arc_free(). 2309789Sahrens */ 2310789Sahrens ASSERT(DVA_EQUAL(BP_IDENTITY(&zio->io_bp_orig), 2311789Sahrens BP_IDENTITY(zio->io_bp))); 2312789Sahrens ASSERT3U(zio->io_bp_orig.blk_birth, ==, 2313789Sahrens zio->io_bp->blk_birth); 2314789Sahrens 2315789Sahrens ASSERT(refcount_is_zero(&exists->b_refcnt)); 2316789Sahrens arc_change_state(arc.anon, exists, hash_lock); 2317789Sahrens mutex_exit(hash_lock); 23181544Seschrock arc_hdr_destroy(exists); 2319789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 2320789Sahrens ASSERT3P(exists, ==, NULL); 2321789Sahrens } 23221544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 23232688Smaybee arc_access(hdr, hash_lock); 23242688Smaybee mutex_exit(hash_lock); 23251544Seschrock } else if (acb->acb_done == NULL) { 23261544Seschrock int destroy_hdr; 23271544Seschrock /* 23281544Seschrock * This is an anonymous buffer with no user callback, 23291544Seschrock * destroy it if there are no active references. 23301544Seschrock */ 23311544Seschrock mutex_enter(&arc_eviction_mtx); 23321544Seschrock destroy_hdr = refcount_is_zero(&hdr->b_refcnt); 23331544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 23341544Seschrock mutex_exit(&arc_eviction_mtx); 23351544Seschrock if (destroy_hdr) 23361544Seschrock arc_hdr_destroy(hdr); 23371544Seschrock } else { 23381544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 2339789Sahrens } 23401544Seschrock 23411544Seschrock if (acb->acb_done) { 2342789Sahrens ASSERT(!refcount_is_zero(&hdr->b_refcnt)); 2343789Sahrens acb->acb_done(zio, buf, acb->acb_private); 2344789Sahrens } 2345789Sahrens 23461544Seschrock kmem_free(acb, sizeof (arc_callback_t)); 2347789Sahrens } 2348789Sahrens 2349789Sahrens int 23501775Sbillm arc_write(zio_t *pio, spa_t *spa, int checksum, int compress, int ncopies, 2351789Sahrens uint64_t txg, blkptr_t *bp, arc_buf_t *buf, 2352789Sahrens arc_done_func_t *done, void *private, int priority, int flags, 23531544Seschrock uint32_t arc_flags, zbookmark_t *zb) 2354789Sahrens { 2355789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 2356789Sahrens arc_callback_t *acb; 2357789Sahrens zio_t *rzio; 2358789Sahrens 2359789Sahrens /* this is a private buffer - no locking required */ 2360789Sahrens ASSERT3P(hdr->b_state, ==, arc.anon); 2361789Sahrens ASSERT(BUF_EMPTY(hdr)); 2362789Sahrens ASSERT(!HDR_IO_ERROR(hdr)); 23632237Smaybee ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0); 23642237Smaybee ASSERT(hdr->b_acb == 0); 2365789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 2366789Sahrens acb->acb_done = done; 2367789Sahrens acb->acb_private = private; 2368789Sahrens acb->acb_byteswap = (arc_byteswap_func_t *)-1; 2369789Sahrens hdr->b_acb = acb; 23701544Seschrock hdr->b_flags |= ARC_IO_IN_PROGRESS; 2371*3093Sahrens arc_cksum_compute(buf); 23721775Sbillm rzio = zio_write(pio, spa, checksum, compress, ncopies, txg, bp, 23731544Seschrock buf->b_data, hdr->b_size, arc_write_done, buf, priority, flags, zb); 2374789Sahrens 2375789Sahrens if (arc_flags & ARC_WAIT) 2376789Sahrens return (zio_wait(rzio)); 2377789Sahrens 2378789Sahrens ASSERT(arc_flags & ARC_NOWAIT); 2379789Sahrens zio_nowait(rzio); 2380789Sahrens 2381789Sahrens return (0); 2382789Sahrens } 2383789Sahrens 2384789Sahrens int 2385789Sahrens arc_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 2386789Sahrens zio_done_func_t *done, void *private, uint32_t arc_flags) 2387789Sahrens { 2388789Sahrens arc_buf_hdr_t *ab; 2389789Sahrens kmutex_t *hash_lock; 2390789Sahrens zio_t *zio; 2391789Sahrens 2392789Sahrens /* 2393789Sahrens * If this buffer is in the cache, release it, so it 2394789Sahrens * can be re-used. 2395789Sahrens */ 2396789Sahrens ab = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock); 2397789Sahrens if (ab != NULL) { 2398789Sahrens /* 2399789Sahrens * The checksum of blocks to free is not always 2400789Sahrens * preserved (eg. on the deadlist). However, if it is 2401789Sahrens * nonzero, it should match what we have in the cache. 2402789Sahrens */ 2403789Sahrens ASSERT(bp->blk_cksum.zc_word[0] == 0 || 2404789Sahrens ab->b_cksum0 == bp->blk_cksum.zc_word[0]); 24051990Smaybee if (ab->b_state != arc.anon) 24061990Smaybee arc_change_state(arc.anon, ab, hash_lock); 24072391Smaybee if (HDR_IO_IN_PROGRESS(ab)) { 24082391Smaybee /* 24092391Smaybee * This should only happen when we prefetch. 24102391Smaybee */ 24112391Smaybee ASSERT(ab->b_flags & ARC_PREFETCH); 24122391Smaybee ASSERT3U(ab->b_datacnt, ==, 1); 24132391Smaybee ab->b_flags |= ARC_FREED_IN_READ; 24142391Smaybee if (HDR_IN_HASH_TABLE(ab)) 24152391Smaybee buf_hash_remove(ab); 24162391Smaybee ab->b_arc_access = 0; 24172391Smaybee bzero(&ab->b_dva, sizeof (dva_t)); 24182391Smaybee ab->b_birth = 0; 24192391Smaybee ab->b_cksum0 = 0; 24202391Smaybee ab->b_buf->b_efunc = NULL; 24212391Smaybee ab->b_buf->b_private = NULL; 24222391Smaybee mutex_exit(hash_lock); 24232391Smaybee } else if (refcount_is_zero(&ab->b_refcnt)) { 2424789Sahrens mutex_exit(hash_lock); 24251544Seschrock arc_hdr_destroy(ab); 2426789Sahrens atomic_add_64(&arc.deleted, 1); 2427789Sahrens } else { 24281589Smaybee /* 24292391Smaybee * We still have an active reference on this 24302391Smaybee * buffer. This can happen, e.g., from 24312391Smaybee * dbuf_unoverride(). 24321589Smaybee */ 24332391Smaybee ASSERT(!HDR_IN_HASH_TABLE(ab)); 2434789Sahrens ab->b_arc_access = 0; 2435789Sahrens bzero(&ab->b_dva, sizeof (dva_t)); 2436789Sahrens ab->b_birth = 0; 2437789Sahrens ab->b_cksum0 = 0; 24381544Seschrock ab->b_buf->b_efunc = NULL; 24391544Seschrock ab->b_buf->b_private = NULL; 2440789Sahrens mutex_exit(hash_lock); 2441789Sahrens } 2442789Sahrens } 2443789Sahrens 2444789Sahrens zio = zio_free(pio, spa, txg, bp, done, private); 2445789Sahrens 2446789Sahrens if (arc_flags & ARC_WAIT) 2447789Sahrens return (zio_wait(zio)); 2448789Sahrens 2449789Sahrens ASSERT(arc_flags & ARC_NOWAIT); 2450789Sahrens zio_nowait(zio); 2451789Sahrens 2452789Sahrens return (0); 2453789Sahrens } 2454789Sahrens 2455789Sahrens void 2456789Sahrens arc_tempreserve_clear(uint64_t tempreserve) 2457789Sahrens { 2458789Sahrens atomic_add_64(&arc_tempreserve, -tempreserve); 2459789Sahrens ASSERT((int64_t)arc_tempreserve >= 0); 2460789Sahrens } 2461789Sahrens 2462789Sahrens int 2463789Sahrens arc_tempreserve_space(uint64_t tempreserve) 2464789Sahrens { 2465789Sahrens #ifdef ZFS_DEBUG 2466789Sahrens /* 2467789Sahrens * Once in a while, fail for no reason. Everything should cope. 2468789Sahrens */ 2469789Sahrens if (spa_get_random(10000) == 0) { 2470789Sahrens dprintf("forcing random failure\n"); 2471789Sahrens return (ERESTART); 2472789Sahrens } 2473789Sahrens #endif 2474982Smaybee if (tempreserve > arc.c/4 && !arc.no_grow) 2475982Smaybee arc.c = MIN(arc.c_max, tempreserve * 4); 2476982Smaybee if (tempreserve > arc.c) 2477982Smaybee return (ENOMEM); 2478982Smaybee 2479789Sahrens /* 2480982Smaybee * Throttle writes when the amount of dirty data in the cache 2481982Smaybee * gets too large. We try to keep the cache less than half full 2482982Smaybee * of dirty blocks so that our sync times don't grow too large. 2483982Smaybee * Note: if two requests come in concurrently, we might let them 2484982Smaybee * both succeed, when one of them should fail. Not a huge deal. 2485982Smaybee * 2486982Smaybee * XXX The limit should be adjusted dynamically to keep the time 2487982Smaybee * to sync a dataset fixed (around 1-5 seconds?). 2488789Sahrens */ 2489789Sahrens 2490982Smaybee if (tempreserve + arc_tempreserve + arc.anon->size > arc.c / 2 && 2491982Smaybee arc_tempreserve + arc.anon->size > arc.c / 4) { 2492789Sahrens dprintf("failing, arc_tempreserve=%lluK anon=%lluK " 2493789Sahrens "tempreserve=%lluK arc.c=%lluK\n", 2494789Sahrens arc_tempreserve>>10, arc.anon->lsize>>10, 2495789Sahrens tempreserve>>10, arc.c>>10); 2496789Sahrens return (ERESTART); 2497789Sahrens } 2498789Sahrens atomic_add_64(&arc_tempreserve, tempreserve); 2499789Sahrens return (0); 2500789Sahrens } 2501789Sahrens 2502789Sahrens void 2503789Sahrens arc_init(void) 2504789Sahrens { 2505789Sahrens mutex_init(&arc_reclaim_lock, NULL, MUTEX_DEFAULT, NULL); 2506789Sahrens mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 2507789Sahrens cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 2508789Sahrens 25092391Smaybee /* Convert seconds to clock ticks */ 25102638Sperrin arc_min_prefetch_lifespan = 1 * hz; 25112391Smaybee 2512789Sahrens /* Start out with 1/8 of all memory */ 2513789Sahrens arc.c = physmem * PAGESIZE / 8; 2514789Sahrens 2515789Sahrens #ifdef _KERNEL 2516789Sahrens /* 2517789Sahrens * On architectures where the physical memory can be larger 2518789Sahrens * than the addressable space (intel in 32-bit mode), we may 2519789Sahrens * need to limit the cache to 1/8 of VM size. 2520789Sahrens */ 2521789Sahrens arc.c = MIN(arc.c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 2522789Sahrens #endif 2523789Sahrens 2524982Smaybee /* set min cache to 1/32 of all memory, or 64MB, whichever is more */ 2525789Sahrens arc.c_min = MAX(arc.c / 4, 64<<20); 2526982Smaybee /* set max to 3/4 of all memory, or all but 1GB, whichever is more */ 2527789Sahrens if (arc.c * 8 >= 1<<30) 2528789Sahrens arc.c_max = (arc.c * 8) - (1<<30); 2529789Sahrens else 2530789Sahrens arc.c_max = arc.c_min; 2531789Sahrens arc.c_max = MAX(arc.c * 6, arc.c_max); 25322885Sahrens 25332885Sahrens /* 25342885Sahrens * Allow the tunables to override our calculations if they are 25352885Sahrens * reasonable (ie. over 64MB) 25362885Sahrens */ 25372885Sahrens if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE) 25382885Sahrens arc.c_max = zfs_arc_max; 25392885Sahrens if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc.c_max) 25402885Sahrens arc.c_min = zfs_arc_min; 25412885Sahrens 2542789Sahrens arc.c = arc.c_max; 2543789Sahrens arc.p = (arc.c >> 1); 2544789Sahrens 2545789Sahrens /* if kmem_flags are set, lets try to use less memory */ 2546789Sahrens if (kmem_debugging()) 2547789Sahrens arc.c = arc.c / 2; 2548789Sahrens if (arc.c < arc.c_min) 2549789Sahrens arc.c = arc.c_min; 2550789Sahrens 2551789Sahrens arc.anon = &ARC_anon; 25521544Seschrock arc.mru = &ARC_mru; 25531544Seschrock arc.mru_ghost = &ARC_mru_ghost; 25541544Seschrock arc.mfu = &ARC_mfu; 25551544Seschrock arc.mfu_ghost = &ARC_mfu_ghost; 25561544Seschrock arc.size = 0; 2557789Sahrens 25582688Smaybee arc.hits = 0; 25592688Smaybee arc.recycle_miss = 0; 25602688Smaybee arc.evict_skip = 0; 25612688Smaybee arc.mutex_miss = 0; 25622688Smaybee 25632856Snd150628 mutex_init(&arc.anon->mtx, NULL, MUTEX_DEFAULT, NULL); 25642856Snd150628 mutex_init(&arc.mru->mtx, NULL, MUTEX_DEFAULT, NULL); 25652856Snd150628 mutex_init(&arc.mru_ghost->mtx, NULL, MUTEX_DEFAULT, NULL); 25662856Snd150628 mutex_init(&arc.mfu->mtx, NULL, MUTEX_DEFAULT, NULL); 25672856Snd150628 mutex_init(&arc.mfu_ghost->mtx, NULL, MUTEX_DEFAULT, NULL); 25682856Snd150628 25691544Seschrock list_create(&arc.mru->list, sizeof (arc_buf_hdr_t), 2570789Sahrens offsetof(arc_buf_hdr_t, b_arc_node)); 25711544Seschrock list_create(&arc.mru_ghost->list, sizeof (arc_buf_hdr_t), 2572789Sahrens offsetof(arc_buf_hdr_t, b_arc_node)); 25731544Seschrock list_create(&arc.mfu->list, sizeof (arc_buf_hdr_t), 2574789Sahrens offsetof(arc_buf_hdr_t, b_arc_node)); 25751544Seschrock list_create(&arc.mfu_ghost->list, sizeof (arc_buf_hdr_t), 2576789Sahrens offsetof(arc_buf_hdr_t, b_arc_node)); 2577789Sahrens 2578789Sahrens buf_init(); 2579789Sahrens 2580789Sahrens arc_thread_exit = 0; 25811544Seschrock arc_eviction_list = NULL; 25821544Seschrock mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 25832887Smaybee bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 2584789Sahrens 2585789Sahrens (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 2586789Sahrens TS_RUN, minclsyspri); 2587789Sahrens } 2588789Sahrens 2589789Sahrens void 2590789Sahrens arc_fini(void) 2591789Sahrens { 2592789Sahrens mutex_enter(&arc_reclaim_thr_lock); 2593789Sahrens arc_thread_exit = 1; 2594789Sahrens while (arc_thread_exit != 0) 2595789Sahrens cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 2596789Sahrens mutex_exit(&arc_reclaim_thr_lock); 2597789Sahrens 2598789Sahrens arc_flush(); 2599789Sahrens 2600789Sahrens arc_dead = TRUE; 2601789Sahrens 26021544Seschrock mutex_destroy(&arc_eviction_mtx); 2603789Sahrens mutex_destroy(&arc_reclaim_lock); 2604789Sahrens mutex_destroy(&arc_reclaim_thr_lock); 2605789Sahrens cv_destroy(&arc_reclaim_thr_cv); 2606789Sahrens 26071544Seschrock list_destroy(&arc.mru->list); 26081544Seschrock list_destroy(&arc.mru_ghost->list); 26091544Seschrock list_destroy(&arc.mfu->list); 26101544Seschrock list_destroy(&arc.mfu_ghost->list); 2611789Sahrens 26122856Snd150628 mutex_destroy(&arc.anon->mtx); 26132856Snd150628 mutex_destroy(&arc.mru->mtx); 26142856Snd150628 mutex_destroy(&arc.mru_ghost->mtx); 26152856Snd150628 mutex_destroy(&arc.mfu->mtx); 26162856Snd150628 mutex_destroy(&arc.mfu_ghost->mtx); 26172856Snd150628 2618789Sahrens buf_fini(); 2619789Sahrens } 2620