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 /* 226018Sbrendan * Copyright 2008 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 /* 293403Sbmc * DVA-based Adjustable Replacement 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 505450Sbrendan * implement a "cache throttle" that slows the flow of new data 515450Sbrendan * into the cache until we can make space available. 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 565450Sbrendan * high use, but also tries to react to memory pressure 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, 785450Sbrendan * or 2) via one of the ARC lists. The arc_read() interface 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. 1125450Sbrendan * 1135450Sbrendan * The L2ARC uses the l2arc_buflist_mtx global mutex for the following: 1145450Sbrendan * 1155450Sbrendan * - L2ARC buflist creation 1165450Sbrendan * - L2ARC buflist eviction 1175450Sbrendan * - L2ARC write completion, which walks L2ARC buflists 1185450Sbrendan * - ARC header destruction, as it removes from L2ARC buflists 1195450Sbrendan * - ARC header release, as it removes from L2ARC buflists 120789Sahrens */ 121789Sahrens 122789Sahrens #include <sys/spa.h> 123789Sahrens #include <sys/zio.h> 1243093Sahrens #include <sys/zio_checksum.h> 125789Sahrens #include <sys/zfs_context.h> 126789Sahrens #include <sys/arc.h> 127789Sahrens #include <sys/refcount.h> 1286643Seschrock #include <sys/vdev.h> 129789Sahrens #ifdef _KERNEL 130789Sahrens #include <sys/vmsystm.h> 131789Sahrens #include <vm/anon.h> 132789Sahrens #include <sys/fs/swapnode.h> 1331484Sek110237 #include <sys/dnlc.h> 134789Sahrens #endif 135789Sahrens #include <sys/callb.h> 1363403Sbmc #include <sys/kstat.h> 137789Sahrens 138789Sahrens static kmutex_t arc_reclaim_thr_lock; 139789Sahrens static kcondvar_t arc_reclaim_thr_cv; /* used to signal reclaim thr */ 140789Sahrens static uint8_t arc_thread_exit; 141789Sahrens 1426245Smaybee extern int zfs_write_limit_shift; 1436245Smaybee extern uint64_t zfs_write_limit_max; 1446245Smaybee extern uint64_t zfs_write_limit_inflated; 1456245Smaybee 1461484Sek110237 #define ARC_REDUCE_DNLC_PERCENT 3 1471484Sek110237 uint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT; 1481484Sek110237 149789Sahrens typedef enum arc_reclaim_strategy { 150789Sahrens ARC_RECLAIM_AGGR, /* Aggressive reclaim strategy */ 151789Sahrens ARC_RECLAIM_CONS /* Conservative reclaim strategy */ 152789Sahrens } arc_reclaim_strategy_t; 153789Sahrens 154789Sahrens /* number of seconds before growing cache again */ 155789Sahrens static int arc_grow_retry = 60; 156789Sahrens 1572391Smaybee /* 1582638Sperrin * minimum lifespan of a prefetch block in clock ticks 1592638Sperrin * (initialized in arc_init()) 1602391Smaybee */ 1612638Sperrin static int arc_min_prefetch_lifespan; 1622391Smaybee 163789Sahrens static int arc_dead; 164789Sahrens 165789Sahrens /* 1666987Sbrendan * The arc has filled available memory and has now warmed up. 1676987Sbrendan */ 1686987Sbrendan static boolean_t arc_warm; 1696987Sbrendan 1706987Sbrendan /* 1712885Sahrens * These tunables are for performance analysis. 1722885Sahrens */ 1732885Sahrens uint64_t zfs_arc_max; 1742885Sahrens uint64_t zfs_arc_min; 1754645Sek110237 uint64_t zfs_arc_meta_limit = 0; 1767046Sahrens int zfs_mdcomp_disable = 0; 1772885Sahrens 1782885Sahrens /* 1795450Sbrendan * Note that buffers can be in one of 6 states: 180789Sahrens * ARC_anon - anonymous (discussed below) 1811544Seschrock * ARC_mru - recently used, currently cached 1821544Seschrock * ARC_mru_ghost - recentely used, no longer in cache 1831544Seschrock * ARC_mfu - frequently used, currently cached 1841544Seschrock * ARC_mfu_ghost - frequently used, no longer in cache 1855450Sbrendan * ARC_l2c_only - exists in L2ARC but not other states 1864309Smaybee * When there are no active references to the buffer, they are 1874309Smaybee * are linked onto a list in one of these arc states. These are 1884309Smaybee * the only buffers that can be evicted or deleted. Within each 1894309Smaybee * state there are multiple lists, one for meta-data and one for 1904309Smaybee * non-meta-data. Meta-data (indirect blocks, blocks of dnodes, 1914309Smaybee * etc.) is tracked separately so that it can be managed more 1925450Sbrendan * explicitly: favored over data, limited explicitly. 193789Sahrens * 194789Sahrens * Anonymous buffers are buffers that are not associated with 195789Sahrens * a DVA. These are buffers that hold dirty block copies 196789Sahrens * before they are written to stable storage. By definition, 1971544Seschrock * they are "ref'd" and are considered part of arc_mru 198789Sahrens * that cannot be freed. Generally, they will aquire a DVA 1991544Seschrock * as they are written and migrate onto the arc_mru list. 2005450Sbrendan * 2015450Sbrendan * The ARC_l2c_only state is for buffers that are in the second 2025450Sbrendan * level ARC but no longer in any of the ARC_m* lists. The second 2035450Sbrendan * level ARC itself may also contain buffers that are in any of 2045450Sbrendan * the ARC_m* states - meaning that a buffer can exist in two 2055450Sbrendan * places. The reason for the ARC_l2c_only state is to keep the 2065450Sbrendan * buffer header in the hash table, so that reads that hit the 2075450Sbrendan * second level ARC benefit from these fast lookups. 208789Sahrens */ 209789Sahrens 210789Sahrens typedef struct arc_state { 2114309Smaybee list_t arcs_list[ARC_BUFC_NUMTYPES]; /* list of evictable buffers */ 2124309Smaybee uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */ 2134309Smaybee uint64_t arcs_size; /* total amount of data in this state */ 2143403Sbmc kmutex_t arcs_mtx; 215789Sahrens } arc_state_t; 216789Sahrens 2175450Sbrendan /* The 6 states: */ 218789Sahrens static arc_state_t ARC_anon; 2191544Seschrock static arc_state_t ARC_mru; 2201544Seschrock static arc_state_t ARC_mru_ghost; 2211544Seschrock static arc_state_t ARC_mfu; 2221544Seschrock static arc_state_t ARC_mfu_ghost; 2235450Sbrendan static arc_state_t ARC_l2c_only; 224789Sahrens 2253403Sbmc typedef struct arc_stats { 2263403Sbmc kstat_named_t arcstat_hits; 2273403Sbmc kstat_named_t arcstat_misses; 2283403Sbmc kstat_named_t arcstat_demand_data_hits; 2293403Sbmc kstat_named_t arcstat_demand_data_misses; 2303403Sbmc kstat_named_t arcstat_demand_metadata_hits; 2313403Sbmc kstat_named_t arcstat_demand_metadata_misses; 2323403Sbmc kstat_named_t arcstat_prefetch_data_hits; 2333403Sbmc kstat_named_t arcstat_prefetch_data_misses; 2343403Sbmc kstat_named_t arcstat_prefetch_metadata_hits; 2353403Sbmc kstat_named_t arcstat_prefetch_metadata_misses; 2363403Sbmc kstat_named_t arcstat_mru_hits; 2373403Sbmc kstat_named_t arcstat_mru_ghost_hits; 2383403Sbmc kstat_named_t arcstat_mfu_hits; 2393403Sbmc kstat_named_t arcstat_mfu_ghost_hits; 2403403Sbmc kstat_named_t arcstat_deleted; 2413403Sbmc kstat_named_t arcstat_recycle_miss; 2423403Sbmc kstat_named_t arcstat_mutex_miss; 2433403Sbmc kstat_named_t arcstat_evict_skip; 2443403Sbmc kstat_named_t arcstat_hash_elements; 2453403Sbmc kstat_named_t arcstat_hash_elements_max; 2463403Sbmc kstat_named_t arcstat_hash_collisions; 2473403Sbmc kstat_named_t arcstat_hash_chains; 2483403Sbmc kstat_named_t arcstat_hash_chain_max; 2493403Sbmc kstat_named_t arcstat_p; 2503403Sbmc kstat_named_t arcstat_c; 2513403Sbmc kstat_named_t arcstat_c_min; 2523403Sbmc kstat_named_t arcstat_c_max; 2533403Sbmc kstat_named_t arcstat_size; 2545450Sbrendan kstat_named_t arcstat_hdr_size; 2555450Sbrendan kstat_named_t arcstat_l2_hits; 2565450Sbrendan kstat_named_t arcstat_l2_misses; 2575450Sbrendan kstat_named_t arcstat_l2_feeds; 2585450Sbrendan kstat_named_t arcstat_l2_rw_clash; 2595450Sbrendan kstat_named_t arcstat_l2_writes_sent; 2605450Sbrendan kstat_named_t arcstat_l2_writes_done; 2615450Sbrendan kstat_named_t arcstat_l2_writes_error; 2625450Sbrendan kstat_named_t arcstat_l2_writes_hdr_miss; 2635450Sbrendan kstat_named_t arcstat_l2_evict_lock_retry; 2645450Sbrendan kstat_named_t arcstat_l2_evict_reading; 2655450Sbrendan kstat_named_t arcstat_l2_free_on_write; 2665450Sbrendan kstat_named_t arcstat_l2_abort_lowmem; 2675450Sbrendan kstat_named_t arcstat_l2_cksum_bad; 2685450Sbrendan kstat_named_t arcstat_l2_io_error; 2695450Sbrendan kstat_named_t arcstat_l2_size; 2705450Sbrendan kstat_named_t arcstat_l2_hdr_size; 2716245Smaybee kstat_named_t arcstat_memory_throttle_count; 2723403Sbmc } arc_stats_t; 2733403Sbmc 2743403Sbmc static arc_stats_t arc_stats = { 2753403Sbmc { "hits", KSTAT_DATA_UINT64 }, 2763403Sbmc { "misses", KSTAT_DATA_UINT64 }, 2773403Sbmc { "demand_data_hits", KSTAT_DATA_UINT64 }, 2783403Sbmc { "demand_data_misses", KSTAT_DATA_UINT64 }, 2793403Sbmc { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 2803403Sbmc { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 2813403Sbmc { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 2823403Sbmc { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 2833403Sbmc { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 2843403Sbmc { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 2853403Sbmc { "mru_hits", KSTAT_DATA_UINT64 }, 2863403Sbmc { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 2873403Sbmc { "mfu_hits", KSTAT_DATA_UINT64 }, 2883403Sbmc { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 2893403Sbmc { "deleted", KSTAT_DATA_UINT64 }, 2903403Sbmc { "recycle_miss", KSTAT_DATA_UINT64 }, 2913403Sbmc { "mutex_miss", KSTAT_DATA_UINT64 }, 2923403Sbmc { "evict_skip", KSTAT_DATA_UINT64 }, 2933403Sbmc { "hash_elements", KSTAT_DATA_UINT64 }, 2943403Sbmc { "hash_elements_max", KSTAT_DATA_UINT64 }, 2953403Sbmc { "hash_collisions", KSTAT_DATA_UINT64 }, 2963403Sbmc { "hash_chains", KSTAT_DATA_UINT64 }, 2973403Sbmc { "hash_chain_max", KSTAT_DATA_UINT64 }, 2983403Sbmc { "p", KSTAT_DATA_UINT64 }, 2993403Sbmc { "c", KSTAT_DATA_UINT64 }, 3003403Sbmc { "c_min", KSTAT_DATA_UINT64 }, 3013403Sbmc { "c_max", KSTAT_DATA_UINT64 }, 3025450Sbrendan { "size", KSTAT_DATA_UINT64 }, 3035450Sbrendan { "hdr_size", KSTAT_DATA_UINT64 }, 3045450Sbrendan { "l2_hits", KSTAT_DATA_UINT64 }, 3055450Sbrendan { "l2_misses", KSTAT_DATA_UINT64 }, 3065450Sbrendan { "l2_feeds", KSTAT_DATA_UINT64 }, 3075450Sbrendan { "l2_rw_clash", KSTAT_DATA_UINT64 }, 3085450Sbrendan { "l2_writes_sent", KSTAT_DATA_UINT64 }, 3095450Sbrendan { "l2_writes_done", KSTAT_DATA_UINT64 }, 3105450Sbrendan { "l2_writes_error", KSTAT_DATA_UINT64 }, 3115450Sbrendan { "l2_writes_hdr_miss", KSTAT_DATA_UINT64 }, 3125450Sbrendan { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 3135450Sbrendan { "l2_evict_reading", KSTAT_DATA_UINT64 }, 3145450Sbrendan { "l2_free_on_write", KSTAT_DATA_UINT64 }, 3155450Sbrendan { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 3165450Sbrendan { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 3175450Sbrendan { "l2_io_error", KSTAT_DATA_UINT64 }, 3185450Sbrendan { "l2_size", KSTAT_DATA_UINT64 }, 3196245Smaybee { "l2_hdr_size", KSTAT_DATA_UINT64 }, 3206245Smaybee { "memory_throttle_count", KSTAT_DATA_UINT64 } 3213403Sbmc }; 322789Sahrens 3233403Sbmc #define ARCSTAT(stat) (arc_stats.stat.value.ui64) 3243403Sbmc 3253403Sbmc #define ARCSTAT_INCR(stat, val) \ 3263403Sbmc atomic_add_64(&arc_stats.stat.value.ui64, (val)); 3273403Sbmc 3283403Sbmc #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 3293403Sbmc #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 3303403Sbmc 3313403Sbmc #define ARCSTAT_MAX(stat, val) { \ 3323403Sbmc uint64_t m; \ 3333403Sbmc while ((val) > (m = arc_stats.stat.value.ui64) && \ 3343403Sbmc (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 3353403Sbmc continue; \ 3363403Sbmc } 3373403Sbmc 3383403Sbmc #define ARCSTAT_MAXSTAT(stat) \ 3393403Sbmc ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 340789Sahrens 3413403Sbmc /* 3423403Sbmc * We define a macro to allow ARC hits/misses to be easily broken down by 3433403Sbmc * two separate conditions, giving a total of four different subtypes for 3443403Sbmc * each of hits and misses (so eight statistics total). 3453403Sbmc */ 3463403Sbmc #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 3473403Sbmc if (cond1) { \ 3483403Sbmc if (cond2) { \ 3493403Sbmc ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 3503403Sbmc } else { \ 3513403Sbmc ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 3523403Sbmc } \ 3533403Sbmc } else { \ 3543403Sbmc if (cond2) { \ 3553403Sbmc ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 3563403Sbmc } else { \ 3573403Sbmc ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 3583403Sbmc } \ 3593403Sbmc } 360789Sahrens 3613403Sbmc kstat_t *arc_ksp; 3623403Sbmc static arc_state_t *arc_anon; 3633403Sbmc static arc_state_t *arc_mru; 3643403Sbmc static arc_state_t *arc_mru_ghost; 3653403Sbmc static arc_state_t *arc_mfu; 3663403Sbmc static arc_state_t *arc_mfu_ghost; 3675450Sbrendan static arc_state_t *arc_l2c_only; 3683403Sbmc 3693403Sbmc /* 3703403Sbmc * There are several ARC variables that are critical to export as kstats -- 3713403Sbmc * but we don't want to have to grovel around in the kstat whenever we wish to 3723403Sbmc * manipulate them. For these variables, we therefore define them to be in 3733403Sbmc * terms of the statistic variable. This assures that we are not introducing 3743403Sbmc * the possibility of inconsistency by having shadow copies of the variables, 3753403Sbmc * while still allowing the code to be readable. 3763403Sbmc */ 3773403Sbmc #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */ 3783403Sbmc #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 3793403Sbmc #define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 3803403Sbmc #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 3813403Sbmc #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 3823403Sbmc 3833403Sbmc static int arc_no_grow; /* Don't try to grow cache size */ 3843403Sbmc static uint64_t arc_tempreserve; 3854309Smaybee static uint64_t arc_meta_used; 3864309Smaybee static uint64_t arc_meta_limit; 3874309Smaybee static uint64_t arc_meta_max = 0; 388789Sahrens 3895450Sbrendan typedef struct l2arc_buf_hdr l2arc_buf_hdr_t; 3905450Sbrendan 391789Sahrens typedef struct arc_callback arc_callback_t; 392789Sahrens 393789Sahrens struct arc_callback { 3943547Smaybee void *acb_private; 395789Sahrens arc_done_func_t *acb_done; 396789Sahrens arc_buf_t *acb_buf; 397789Sahrens zio_t *acb_zio_dummy; 398789Sahrens arc_callback_t *acb_next; 399789Sahrens }; 400789Sahrens 4013547Smaybee typedef struct arc_write_callback arc_write_callback_t; 4023547Smaybee 4033547Smaybee struct arc_write_callback { 4043547Smaybee void *awcb_private; 4053547Smaybee arc_done_func_t *awcb_ready; 4063547Smaybee arc_done_func_t *awcb_done; 4073547Smaybee arc_buf_t *awcb_buf; 4083547Smaybee }; 4093547Smaybee 410789Sahrens struct arc_buf_hdr { 411789Sahrens /* protected by hash lock */ 412789Sahrens dva_t b_dva; 413789Sahrens uint64_t b_birth; 414789Sahrens uint64_t b_cksum0; 415789Sahrens 4163093Sahrens kmutex_t b_freeze_lock; 4173093Sahrens zio_cksum_t *b_freeze_cksum; 4183093Sahrens 419789Sahrens arc_buf_hdr_t *b_hash_next; 420789Sahrens arc_buf_t *b_buf; 421789Sahrens uint32_t b_flags; 4221544Seschrock uint32_t b_datacnt; 423789Sahrens 4243290Sjohansen arc_callback_t *b_acb; 425789Sahrens kcondvar_t b_cv; 4263290Sjohansen 4273290Sjohansen /* immutable */ 4283290Sjohansen arc_buf_contents_t b_type; 4293290Sjohansen uint64_t b_size; 4303290Sjohansen spa_t *b_spa; 431789Sahrens 432789Sahrens /* protected by arc state mutex */ 433789Sahrens arc_state_t *b_state; 434789Sahrens list_node_t b_arc_node; 435789Sahrens 436789Sahrens /* updated atomically */ 437789Sahrens clock_t b_arc_access; 438789Sahrens 439789Sahrens /* self protecting */ 440789Sahrens refcount_t b_refcnt; 4415450Sbrendan 4425450Sbrendan l2arc_buf_hdr_t *b_l2hdr; 4435450Sbrendan list_node_t b_l2node; 4447046Sahrens /* 4457046Sahrens * scrub code can lockout access to the buf while it changes 4467046Sahrens * bp's contained within it. 4477046Sahrens */ 4487046Sahrens krwlock_t b_datalock; 449789Sahrens }; 450789Sahrens 4511544Seschrock static arc_buf_t *arc_eviction_list; 4521544Seschrock static kmutex_t arc_eviction_mtx; 4532887Smaybee static arc_buf_hdr_t arc_eviction_hdr; 4542688Smaybee static void arc_get_data_buf(arc_buf_t *buf); 4552688Smaybee static void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock); 4564309Smaybee static int arc_evict_needed(arc_buf_contents_t type); 4575642Smaybee static void arc_evict_ghost(arc_state_t *state, spa_t *spa, int64_t bytes); 4581544Seschrock 4591544Seschrock #define GHOST_STATE(state) \ 4605450Sbrendan ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 4615450Sbrendan (state) == arc_l2c_only) 4621544Seschrock 463789Sahrens /* 464789Sahrens * Private ARC flags. These flags are private ARC only flags that will show up 465789Sahrens * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can 466789Sahrens * be passed in as arc_flags in things like arc_read. However, these flags 467789Sahrens * should never be passed and should only be set by ARC code. When adding new 468789Sahrens * public flags, make sure not to smash the private ones. 469789Sahrens */ 470789Sahrens 4711544Seschrock #define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */ 472789Sahrens #define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */ 473789Sahrens #define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */ 474789Sahrens #define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */ 4751544Seschrock #define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */ 4762391Smaybee #define ARC_INDIRECT (1 << 14) /* this is an indirect block */ 4775450Sbrendan #define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */ 478*7237Sek110237 #define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */ 479*7237Sek110237 #define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */ 480*7237Sek110237 #define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */ 481*7237Sek110237 #define ARC_STORED (1 << 19) /* has been store()d to */ 482789Sahrens 4831544Seschrock #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE) 484789Sahrens #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS) 485789Sahrens #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR) 486789Sahrens #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ) 4871544Seschrock #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE) 4885450Sbrendan #define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS) 489*7237Sek110237 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE) 4906987Sbrendan #define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \ 4916987Sbrendan (hdr)->b_l2hdr != NULL) 4925450Sbrendan #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING) 4935450Sbrendan #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED) 4945450Sbrendan #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD) 495789Sahrens 496789Sahrens /* 4976018Sbrendan * Other sizes 4986018Sbrendan */ 4996018Sbrendan 5006018Sbrendan #define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 5016018Sbrendan #define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t)) 5026018Sbrendan 5036018Sbrendan /* 504789Sahrens * Hash table routines 505789Sahrens */ 506789Sahrens 507789Sahrens #define HT_LOCK_PAD 64 508789Sahrens 509789Sahrens struct ht_lock { 510789Sahrens kmutex_t ht_lock; 511789Sahrens #ifdef _KERNEL 512789Sahrens unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 513789Sahrens #endif 514789Sahrens }; 515789Sahrens 516789Sahrens #define BUF_LOCKS 256 517789Sahrens typedef struct buf_hash_table { 518789Sahrens uint64_t ht_mask; 519789Sahrens arc_buf_hdr_t **ht_table; 520789Sahrens struct ht_lock ht_locks[BUF_LOCKS]; 521789Sahrens } buf_hash_table_t; 522789Sahrens 523789Sahrens static buf_hash_table_t buf_hash_table; 524789Sahrens 525789Sahrens #define BUF_HASH_INDEX(spa, dva, birth) \ 526789Sahrens (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 527789Sahrens #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 528789Sahrens #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 529789Sahrens #define HDR_LOCK(buf) \ 530789Sahrens (BUF_HASH_LOCK(BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth))) 531789Sahrens 532789Sahrens uint64_t zfs_crc64_table[256]; 533789Sahrens 5345450Sbrendan /* 5355450Sbrendan * Level 2 ARC 5365450Sbrendan */ 5375450Sbrendan 5385450Sbrendan #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 5395450Sbrendan #define L2ARC_HEADROOM 4 /* num of writes */ 5405450Sbrendan #define L2ARC_FEED_SECS 1 /* caching interval */ 5415450Sbrendan 5425450Sbrendan #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 5435450Sbrendan #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 5445450Sbrendan 5455450Sbrendan /* 5465450Sbrendan * L2ARC Performance Tunables 5475450Sbrendan */ 5485450Sbrendan uint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 5496987Sbrendan uint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */ 5505450Sbrendan uint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 5515450Sbrendan uint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 5525450Sbrendan boolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 5535450Sbrendan 5545450Sbrendan /* 5555450Sbrendan * L2ARC Internals 5565450Sbrendan */ 5575450Sbrendan typedef struct l2arc_dev { 5585450Sbrendan vdev_t *l2ad_vdev; /* vdev */ 5595450Sbrendan spa_t *l2ad_spa; /* spa */ 5605450Sbrendan uint64_t l2ad_hand; /* next write location */ 5615450Sbrendan uint64_t l2ad_write; /* desired write size, bytes */ 5626987Sbrendan uint64_t l2ad_boost; /* warmup write boost, bytes */ 5635450Sbrendan uint64_t l2ad_start; /* first addr on device */ 5645450Sbrendan uint64_t l2ad_end; /* last addr on device */ 5655450Sbrendan uint64_t l2ad_evict; /* last addr eviction reached */ 5665450Sbrendan boolean_t l2ad_first; /* first sweep through */ 5675450Sbrendan list_t *l2ad_buflist; /* buffer list */ 5685450Sbrendan list_node_t l2ad_node; /* device list node */ 5695450Sbrendan } l2arc_dev_t; 5705450Sbrendan 5715450Sbrendan static list_t L2ARC_dev_list; /* device list */ 5725450Sbrendan static list_t *l2arc_dev_list; /* device list pointer */ 5735450Sbrendan static kmutex_t l2arc_dev_mtx; /* device list mutex */ 5745450Sbrendan static l2arc_dev_t *l2arc_dev_last; /* last device used */ 5755450Sbrendan static kmutex_t l2arc_buflist_mtx; /* mutex for all buflists */ 5765450Sbrendan static list_t L2ARC_free_on_write; /* free after write buf list */ 5775450Sbrendan static list_t *l2arc_free_on_write; /* free after write list ptr */ 5785450Sbrendan static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 5795450Sbrendan static uint64_t l2arc_ndev; /* number of devices */ 5805450Sbrendan 5815450Sbrendan typedef struct l2arc_read_callback { 5825450Sbrendan arc_buf_t *l2rcb_buf; /* read buffer */ 5835450Sbrendan spa_t *l2rcb_spa; /* spa */ 5845450Sbrendan blkptr_t l2rcb_bp; /* original blkptr */ 5855450Sbrendan zbookmark_t l2rcb_zb; /* original bookmark */ 5865450Sbrendan int l2rcb_flags; /* original flags */ 5875450Sbrendan } l2arc_read_callback_t; 5885450Sbrendan 5895450Sbrendan typedef struct l2arc_write_callback { 5905450Sbrendan l2arc_dev_t *l2wcb_dev; /* device info */ 5915450Sbrendan arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 5925450Sbrendan } l2arc_write_callback_t; 5935450Sbrendan 5945450Sbrendan struct l2arc_buf_hdr { 5955450Sbrendan /* protected by arc_buf_hdr mutex */ 5965450Sbrendan l2arc_dev_t *b_dev; /* L2ARC device */ 5975450Sbrendan daddr_t b_daddr; /* disk address, offset byte */ 5985450Sbrendan }; 5995450Sbrendan 6005450Sbrendan typedef struct l2arc_data_free { 6015450Sbrendan /* protected by l2arc_free_on_write_mtx */ 6025450Sbrendan void *l2df_data; 6035450Sbrendan size_t l2df_size; 6045450Sbrendan void (*l2df_func)(void *, size_t); 6055450Sbrendan list_node_t l2df_list_node; 6065450Sbrendan } l2arc_data_free_t; 6075450Sbrendan 6085450Sbrendan static kmutex_t l2arc_feed_thr_lock; 6095450Sbrendan static kcondvar_t l2arc_feed_thr_cv; 6105450Sbrendan static uint8_t l2arc_thread_exit; 6115450Sbrendan 6125450Sbrendan static void l2arc_read_done(zio_t *zio); 6135450Sbrendan static void l2arc_hdr_stat_add(void); 6145450Sbrendan static void l2arc_hdr_stat_remove(void); 6155450Sbrendan 616789Sahrens static uint64_t 6177046Sahrens buf_hash(spa_t *spa, const dva_t *dva, uint64_t birth) 618789Sahrens { 619789Sahrens uintptr_t spav = (uintptr_t)spa; 620789Sahrens uint8_t *vdva = (uint8_t *)dva; 621789Sahrens uint64_t crc = -1ULL; 622789Sahrens int i; 623789Sahrens 624789Sahrens ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 625789Sahrens 626789Sahrens for (i = 0; i < sizeof (dva_t); i++) 627789Sahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 628789Sahrens 629789Sahrens crc ^= (spav>>8) ^ birth; 630789Sahrens 631789Sahrens return (crc); 632789Sahrens } 633789Sahrens 634789Sahrens #define BUF_EMPTY(buf) \ 635789Sahrens ((buf)->b_dva.dva_word[0] == 0 && \ 636789Sahrens (buf)->b_dva.dva_word[1] == 0 && \ 637789Sahrens (buf)->b_birth == 0) 638789Sahrens 639789Sahrens #define BUF_EQUAL(spa, dva, birth, buf) \ 640789Sahrens ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 641789Sahrens ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 642789Sahrens ((buf)->b_birth == birth) && ((buf)->b_spa == spa) 643789Sahrens 644789Sahrens static arc_buf_hdr_t * 6457046Sahrens buf_hash_find(spa_t *spa, const dva_t *dva, uint64_t birth, kmutex_t **lockp) 646789Sahrens { 647789Sahrens uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 648789Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 649789Sahrens arc_buf_hdr_t *buf; 650789Sahrens 651789Sahrens mutex_enter(hash_lock); 652789Sahrens for (buf = buf_hash_table.ht_table[idx]; buf != NULL; 653789Sahrens buf = buf->b_hash_next) { 654789Sahrens if (BUF_EQUAL(spa, dva, birth, buf)) { 655789Sahrens *lockp = hash_lock; 656789Sahrens return (buf); 657789Sahrens } 658789Sahrens } 659789Sahrens mutex_exit(hash_lock); 660789Sahrens *lockp = NULL; 661789Sahrens return (NULL); 662789Sahrens } 663789Sahrens 664789Sahrens /* 665789Sahrens * Insert an entry into the hash table. If there is already an element 666789Sahrens * equal to elem in the hash table, then the already existing element 667789Sahrens * will be returned and the new element will not be inserted. 668789Sahrens * Otherwise returns NULL. 669789Sahrens */ 670789Sahrens static arc_buf_hdr_t * 671789Sahrens buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp) 672789Sahrens { 673789Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 674789Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 675789Sahrens arc_buf_hdr_t *fbuf; 6763403Sbmc uint32_t i; 677789Sahrens 6781544Seschrock ASSERT(!HDR_IN_HASH_TABLE(buf)); 679789Sahrens *lockp = hash_lock; 680789Sahrens mutex_enter(hash_lock); 681789Sahrens for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL; 682789Sahrens fbuf = fbuf->b_hash_next, i++) { 683789Sahrens if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf)) 684789Sahrens return (fbuf); 685789Sahrens } 686789Sahrens 687789Sahrens buf->b_hash_next = buf_hash_table.ht_table[idx]; 688789Sahrens buf_hash_table.ht_table[idx] = buf; 6891544Seschrock buf->b_flags |= ARC_IN_HASH_TABLE; 690789Sahrens 691789Sahrens /* collect some hash table performance data */ 692789Sahrens if (i > 0) { 6933403Sbmc ARCSTAT_BUMP(arcstat_hash_collisions); 694789Sahrens if (i == 1) 6953403Sbmc ARCSTAT_BUMP(arcstat_hash_chains); 6963403Sbmc 6973403Sbmc ARCSTAT_MAX(arcstat_hash_chain_max, i); 698789Sahrens } 6993403Sbmc 7003403Sbmc ARCSTAT_BUMP(arcstat_hash_elements); 7013403Sbmc ARCSTAT_MAXSTAT(arcstat_hash_elements); 702789Sahrens 703789Sahrens return (NULL); 704789Sahrens } 705789Sahrens 706789Sahrens static void 707789Sahrens buf_hash_remove(arc_buf_hdr_t *buf) 708789Sahrens { 709789Sahrens arc_buf_hdr_t *fbuf, **bufp; 710789Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 711789Sahrens 712789Sahrens ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 7131544Seschrock ASSERT(HDR_IN_HASH_TABLE(buf)); 714789Sahrens 715789Sahrens bufp = &buf_hash_table.ht_table[idx]; 716789Sahrens while ((fbuf = *bufp) != buf) { 717789Sahrens ASSERT(fbuf != NULL); 718789Sahrens bufp = &fbuf->b_hash_next; 719789Sahrens } 720789Sahrens *bufp = buf->b_hash_next; 721789Sahrens buf->b_hash_next = NULL; 7221544Seschrock buf->b_flags &= ~ARC_IN_HASH_TABLE; 723789Sahrens 724789Sahrens /* collect some hash table performance data */ 7253403Sbmc ARCSTAT_BUMPDOWN(arcstat_hash_elements); 7263403Sbmc 727789Sahrens if (buf_hash_table.ht_table[idx] && 728789Sahrens buf_hash_table.ht_table[idx]->b_hash_next == NULL) 7293403Sbmc ARCSTAT_BUMPDOWN(arcstat_hash_chains); 730789Sahrens } 731789Sahrens 732789Sahrens /* 733789Sahrens * Global data structures and functions for the buf kmem cache. 734789Sahrens */ 735789Sahrens static kmem_cache_t *hdr_cache; 736789Sahrens static kmem_cache_t *buf_cache; 737789Sahrens 738789Sahrens static void 739789Sahrens buf_fini(void) 740789Sahrens { 741789Sahrens int i; 742789Sahrens 743789Sahrens kmem_free(buf_hash_table.ht_table, 744789Sahrens (buf_hash_table.ht_mask + 1) * sizeof (void *)); 745789Sahrens for (i = 0; i < BUF_LOCKS; i++) 746789Sahrens mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 747789Sahrens kmem_cache_destroy(hdr_cache); 748789Sahrens kmem_cache_destroy(buf_cache); 749789Sahrens } 750789Sahrens 751789Sahrens /* 752789Sahrens * Constructor callback - called when the cache is empty 753789Sahrens * and a new buf is requested. 754789Sahrens */ 755789Sahrens /* ARGSUSED */ 756789Sahrens static int 757789Sahrens hdr_cons(void *vbuf, void *unused, int kmflag) 758789Sahrens { 759789Sahrens arc_buf_hdr_t *buf = vbuf; 760789Sahrens 761789Sahrens bzero(buf, sizeof (arc_buf_hdr_t)); 762789Sahrens refcount_create(&buf->b_refcnt); 763789Sahrens cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL); 7644831Sgw25295 mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 7657046Sahrens rw_init(&buf->b_datalock, NULL, RW_DEFAULT, NULL); 7665450Sbrendan 7676018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE); 768789Sahrens return (0); 769789Sahrens } 770789Sahrens 771789Sahrens /* 772789Sahrens * Destructor callback - called when a cached buf is 773789Sahrens * no longer required. 774789Sahrens */ 775789Sahrens /* ARGSUSED */ 776789Sahrens static void 777789Sahrens hdr_dest(void *vbuf, void *unused) 778789Sahrens { 779789Sahrens arc_buf_hdr_t *buf = vbuf; 780789Sahrens 781789Sahrens refcount_destroy(&buf->b_refcnt); 782789Sahrens cv_destroy(&buf->b_cv); 7834831Sgw25295 mutex_destroy(&buf->b_freeze_lock); 7847046Sahrens rw_destroy(&buf->b_datalock); 7855450Sbrendan 7866018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE); 787789Sahrens } 788789Sahrens 789789Sahrens /* 790789Sahrens * Reclaim callback -- invoked when memory is low. 791789Sahrens */ 792789Sahrens /* ARGSUSED */ 793789Sahrens static void 794789Sahrens hdr_recl(void *unused) 795789Sahrens { 796789Sahrens dprintf("hdr_recl called\n"); 7973158Smaybee /* 7983158Smaybee * umem calls the reclaim func when we destroy the buf cache, 7993158Smaybee * which is after we do arc_fini(). 8003158Smaybee */ 8013158Smaybee if (!arc_dead) 8023158Smaybee cv_signal(&arc_reclaim_thr_cv); 803789Sahrens } 804789Sahrens 805789Sahrens static void 806789Sahrens buf_init(void) 807789Sahrens { 808789Sahrens uint64_t *ct; 8091544Seschrock uint64_t hsize = 1ULL << 12; 810789Sahrens int i, j; 811789Sahrens 812789Sahrens /* 813789Sahrens * The hash table is big enough to fill all of physical memory 8141544Seschrock * with an average 64K block size. The table will take up 8151544Seschrock * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers). 816789Sahrens */ 8171544Seschrock while (hsize * 65536 < physmem * PAGESIZE) 818789Sahrens hsize <<= 1; 8191544Seschrock retry: 820789Sahrens buf_hash_table.ht_mask = hsize - 1; 8211544Seschrock buf_hash_table.ht_table = 8221544Seschrock kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 8231544Seschrock if (buf_hash_table.ht_table == NULL) { 8241544Seschrock ASSERT(hsize > (1ULL << 8)); 8251544Seschrock hsize >>= 1; 8261544Seschrock goto retry; 8271544Seschrock } 828789Sahrens 829789Sahrens hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t), 830789Sahrens 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0); 831789Sahrens buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 832789Sahrens 0, NULL, NULL, NULL, NULL, NULL, 0); 833789Sahrens 834789Sahrens for (i = 0; i < 256; i++) 835789Sahrens for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 836789Sahrens *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 837789Sahrens 838789Sahrens for (i = 0; i < BUF_LOCKS; i++) { 839789Sahrens mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 840789Sahrens NULL, MUTEX_DEFAULT, NULL); 841789Sahrens } 842789Sahrens } 843789Sahrens 844789Sahrens #define ARC_MINTIME (hz>>4) /* 62 ms */ 845789Sahrens 846789Sahrens static void 8473093Sahrens arc_cksum_verify(arc_buf_t *buf) 8483093Sahrens { 8493093Sahrens zio_cksum_t zc; 8503093Sahrens 8513312Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 8523093Sahrens return; 8533093Sahrens 8543093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 8553265Sahrens if (buf->b_hdr->b_freeze_cksum == NULL || 8563265Sahrens (buf->b_hdr->b_flags & ARC_IO_ERROR)) { 8573093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 8583093Sahrens return; 8593093Sahrens } 8603093Sahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 8613093Sahrens if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) 8623093Sahrens panic("buffer modified while frozen!"); 8633093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 8643093Sahrens } 8653093Sahrens 8665450Sbrendan static int 8675450Sbrendan arc_cksum_equal(arc_buf_t *buf) 8685450Sbrendan { 8695450Sbrendan zio_cksum_t zc; 8705450Sbrendan int equal; 8715450Sbrendan 8725450Sbrendan mutex_enter(&buf->b_hdr->b_freeze_lock); 8735450Sbrendan fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 8745450Sbrendan equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc); 8755450Sbrendan mutex_exit(&buf->b_hdr->b_freeze_lock); 8765450Sbrendan 8775450Sbrendan return (equal); 8785450Sbrendan } 8795450Sbrendan 8803093Sahrens static void 8815450Sbrendan arc_cksum_compute(arc_buf_t *buf, boolean_t force) 8823093Sahrens { 8835450Sbrendan if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY)) 8843093Sahrens return; 8853093Sahrens 8863093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 8873093Sahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 8883093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 8893093Sahrens return; 8903093Sahrens } 8913093Sahrens buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 8923093Sahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, 8933093Sahrens buf->b_hdr->b_freeze_cksum); 8943093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 8953093Sahrens } 8963093Sahrens 8973093Sahrens void 8983093Sahrens arc_buf_thaw(arc_buf_t *buf) 8993093Sahrens { 9005450Sbrendan if (zfs_flags & ZFS_DEBUG_MODIFY) { 9015450Sbrendan if (buf->b_hdr->b_state != arc_anon) 9025450Sbrendan panic("modifying non-anon buffer!"); 9035450Sbrendan if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS) 9045450Sbrendan panic("modifying buffer while i/o in progress!"); 9055450Sbrendan arc_cksum_verify(buf); 9065450Sbrendan } 9075450Sbrendan 9083093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 9093093Sahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 9103093Sahrens kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 9113093Sahrens buf->b_hdr->b_freeze_cksum = NULL; 9123093Sahrens } 9133093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9143093Sahrens } 9153093Sahrens 9163093Sahrens void 9173093Sahrens arc_buf_freeze(arc_buf_t *buf) 9183093Sahrens { 9193312Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 9203312Sahrens return; 9213312Sahrens 9223093Sahrens ASSERT(buf->b_hdr->b_freeze_cksum != NULL || 9233403Sbmc buf->b_hdr->b_state == arc_anon); 9245450Sbrendan arc_cksum_compute(buf, B_FALSE); 9253093Sahrens } 9263093Sahrens 9273093Sahrens static void 928789Sahrens add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 929789Sahrens { 930789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 931789Sahrens 932789Sahrens if ((refcount_add(&ab->b_refcnt, tag) == 1) && 9333403Sbmc (ab->b_state != arc_anon)) { 9343700Sek110237 uint64_t delta = ab->b_size * ab->b_datacnt; 9354309Smaybee list_t *list = &ab->b_state->arcs_list[ab->b_type]; 9364309Smaybee uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type]; 937789Sahrens 9383403Sbmc ASSERT(!MUTEX_HELD(&ab->b_state->arcs_mtx)); 9393403Sbmc mutex_enter(&ab->b_state->arcs_mtx); 940789Sahrens ASSERT(list_link_active(&ab->b_arc_node)); 9414309Smaybee list_remove(list, ab); 9421544Seschrock if (GHOST_STATE(ab->b_state)) { 9431544Seschrock ASSERT3U(ab->b_datacnt, ==, 0); 9441544Seschrock ASSERT3P(ab->b_buf, ==, NULL); 9451544Seschrock delta = ab->b_size; 9461544Seschrock } 9471544Seschrock ASSERT(delta > 0); 9484309Smaybee ASSERT3U(*size, >=, delta); 9494309Smaybee atomic_add_64(size, -delta); 9503403Sbmc mutex_exit(&ab->b_state->arcs_mtx); 9517046Sahrens /* remove the prefetch flag if we get a reference */ 9522391Smaybee if (ab->b_flags & ARC_PREFETCH) 9532391Smaybee ab->b_flags &= ~ARC_PREFETCH; 954789Sahrens } 955789Sahrens } 956789Sahrens 957789Sahrens static int 958789Sahrens remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 959789Sahrens { 960789Sahrens int cnt; 9613403Sbmc arc_state_t *state = ab->b_state; 962789Sahrens 9633403Sbmc ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 9643403Sbmc ASSERT(!GHOST_STATE(state)); 965789Sahrens 966789Sahrens if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) && 9673403Sbmc (state != arc_anon)) { 9684309Smaybee uint64_t *size = &state->arcs_lsize[ab->b_type]; 9694309Smaybee 9703403Sbmc ASSERT(!MUTEX_HELD(&state->arcs_mtx)); 9713403Sbmc mutex_enter(&state->arcs_mtx); 972789Sahrens ASSERT(!list_link_active(&ab->b_arc_node)); 9734309Smaybee list_insert_head(&state->arcs_list[ab->b_type], ab); 9741544Seschrock ASSERT(ab->b_datacnt > 0); 9754309Smaybee atomic_add_64(size, ab->b_size * ab->b_datacnt); 9763403Sbmc mutex_exit(&state->arcs_mtx); 977789Sahrens } 978789Sahrens return (cnt); 979789Sahrens } 980789Sahrens 981789Sahrens /* 982789Sahrens * Move the supplied buffer to the indicated state. The mutex 983789Sahrens * for the buffer must be held by the caller. 984789Sahrens */ 985789Sahrens static void 9861544Seschrock arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock) 987789Sahrens { 9881544Seschrock arc_state_t *old_state = ab->b_state; 9893700Sek110237 int64_t refcnt = refcount_count(&ab->b_refcnt); 9903700Sek110237 uint64_t from_delta, to_delta; 991789Sahrens 992789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 9931544Seschrock ASSERT(new_state != old_state); 9941544Seschrock ASSERT(refcnt == 0 || ab->b_datacnt > 0); 9951544Seschrock ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state)); 9961544Seschrock 9971544Seschrock from_delta = to_delta = ab->b_datacnt * ab->b_size; 998789Sahrens 999789Sahrens /* 1000789Sahrens * If this buffer is evictable, transfer it from the 1001789Sahrens * old state list to the new state list. 1002789Sahrens */ 10031544Seschrock if (refcnt == 0) { 10043403Sbmc if (old_state != arc_anon) { 10053403Sbmc int use_mutex = !MUTEX_HELD(&old_state->arcs_mtx); 10064309Smaybee uint64_t *size = &old_state->arcs_lsize[ab->b_type]; 10071544Seschrock 10081544Seschrock if (use_mutex) 10093403Sbmc mutex_enter(&old_state->arcs_mtx); 10101544Seschrock 10111544Seschrock ASSERT(list_link_active(&ab->b_arc_node)); 10124309Smaybee list_remove(&old_state->arcs_list[ab->b_type], ab); 1013789Sahrens 10142391Smaybee /* 10152391Smaybee * If prefetching out of the ghost cache, 10162391Smaybee * we will have a non-null datacnt. 10172391Smaybee */ 10182391Smaybee if (GHOST_STATE(old_state) && ab->b_datacnt == 0) { 10192391Smaybee /* ghost elements have a ghost size */ 10201544Seschrock ASSERT(ab->b_buf == NULL); 10211544Seschrock from_delta = ab->b_size; 1022789Sahrens } 10234309Smaybee ASSERT3U(*size, >=, from_delta); 10244309Smaybee atomic_add_64(size, -from_delta); 10251544Seschrock 10261544Seschrock if (use_mutex) 10273403Sbmc mutex_exit(&old_state->arcs_mtx); 1028789Sahrens } 10293403Sbmc if (new_state != arc_anon) { 10303403Sbmc int use_mutex = !MUTEX_HELD(&new_state->arcs_mtx); 10314309Smaybee uint64_t *size = &new_state->arcs_lsize[ab->b_type]; 1032789Sahrens 10331544Seschrock if (use_mutex) 10343403Sbmc mutex_enter(&new_state->arcs_mtx); 10351544Seschrock 10364309Smaybee list_insert_head(&new_state->arcs_list[ab->b_type], ab); 10371544Seschrock 10381544Seschrock /* ghost elements have a ghost size */ 10391544Seschrock if (GHOST_STATE(new_state)) { 10401544Seschrock ASSERT(ab->b_datacnt == 0); 10411544Seschrock ASSERT(ab->b_buf == NULL); 10421544Seschrock to_delta = ab->b_size; 10431544Seschrock } 10444309Smaybee atomic_add_64(size, to_delta); 10451544Seschrock 10461544Seschrock if (use_mutex) 10473403Sbmc mutex_exit(&new_state->arcs_mtx); 1048789Sahrens } 1049789Sahrens } 1050789Sahrens 1051789Sahrens ASSERT(!BUF_EMPTY(ab)); 10525450Sbrendan if (new_state == arc_anon) { 1053789Sahrens buf_hash_remove(ab); 1054789Sahrens } 1055789Sahrens 10561544Seschrock /* adjust state sizes */ 10571544Seschrock if (to_delta) 10583403Sbmc atomic_add_64(&new_state->arcs_size, to_delta); 10591544Seschrock if (from_delta) { 10603403Sbmc ASSERT3U(old_state->arcs_size, >=, from_delta); 10613403Sbmc atomic_add_64(&old_state->arcs_size, -from_delta); 1062789Sahrens } 1063789Sahrens ab->b_state = new_state; 10645450Sbrendan 10655450Sbrendan /* adjust l2arc hdr stats */ 10665450Sbrendan if (new_state == arc_l2c_only) 10675450Sbrendan l2arc_hdr_stat_add(); 10685450Sbrendan else if (old_state == arc_l2c_only) 10695450Sbrendan l2arc_hdr_stat_remove(); 1070789Sahrens } 1071789Sahrens 10724309Smaybee void 10734309Smaybee arc_space_consume(uint64_t space) 10744309Smaybee { 10754309Smaybee atomic_add_64(&arc_meta_used, space); 10764309Smaybee atomic_add_64(&arc_size, space); 10774309Smaybee } 10784309Smaybee 10794309Smaybee void 10804309Smaybee arc_space_return(uint64_t space) 10814309Smaybee { 10824309Smaybee ASSERT(arc_meta_used >= space); 10834309Smaybee if (arc_meta_max < arc_meta_used) 10844309Smaybee arc_meta_max = arc_meta_used; 10854309Smaybee atomic_add_64(&arc_meta_used, -space); 10864309Smaybee ASSERT(arc_size >= space); 10874309Smaybee atomic_add_64(&arc_size, -space); 10884309Smaybee } 10894309Smaybee 10904309Smaybee void * 10914309Smaybee arc_data_buf_alloc(uint64_t size) 10924309Smaybee { 10934309Smaybee if (arc_evict_needed(ARC_BUFC_DATA)) 10944309Smaybee cv_signal(&arc_reclaim_thr_cv); 10954309Smaybee atomic_add_64(&arc_size, size); 10964309Smaybee return (zio_data_buf_alloc(size)); 10974309Smaybee } 10984309Smaybee 10994309Smaybee void 11004309Smaybee arc_data_buf_free(void *buf, uint64_t size) 11014309Smaybee { 11024309Smaybee zio_data_buf_free(buf, size); 11034309Smaybee ASSERT(arc_size >= size); 11044309Smaybee atomic_add_64(&arc_size, -size); 11054309Smaybee } 11064309Smaybee 1107789Sahrens arc_buf_t * 11083290Sjohansen arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type) 1109789Sahrens { 1110789Sahrens arc_buf_hdr_t *hdr; 1111789Sahrens arc_buf_t *buf; 1112789Sahrens 1113789Sahrens ASSERT3U(size, >, 0); 11146245Smaybee hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 1115789Sahrens ASSERT(BUF_EMPTY(hdr)); 1116789Sahrens hdr->b_size = size; 11173290Sjohansen hdr->b_type = type; 1118789Sahrens hdr->b_spa = spa; 11193403Sbmc hdr->b_state = arc_anon; 1120789Sahrens hdr->b_arc_access = 0; 11216245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1122789Sahrens buf->b_hdr = hdr; 11232688Smaybee buf->b_data = NULL; 11241544Seschrock buf->b_efunc = NULL; 11251544Seschrock buf->b_private = NULL; 1126789Sahrens buf->b_next = NULL; 1127789Sahrens hdr->b_buf = buf; 11282688Smaybee arc_get_data_buf(buf); 11291544Seschrock hdr->b_datacnt = 1; 1130789Sahrens hdr->b_flags = 0; 1131789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1132789Sahrens (void) refcount_add(&hdr->b_refcnt, tag); 1133789Sahrens 1134789Sahrens return (buf); 1135789Sahrens } 1136789Sahrens 11372688Smaybee static arc_buf_t * 11382688Smaybee arc_buf_clone(arc_buf_t *from) 11391544Seschrock { 11402688Smaybee arc_buf_t *buf; 11412688Smaybee arc_buf_hdr_t *hdr = from->b_hdr; 11422688Smaybee uint64_t size = hdr->b_size; 11431544Seschrock 11446245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 11452688Smaybee buf->b_hdr = hdr; 11462688Smaybee buf->b_data = NULL; 11472688Smaybee buf->b_efunc = NULL; 11482688Smaybee buf->b_private = NULL; 11492688Smaybee buf->b_next = hdr->b_buf; 11502688Smaybee hdr->b_buf = buf; 11512688Smaybee arc_get_data_buf(buf); 11522688Smaybee bcopy(from->b_data, buf->b_data, size); 11532688Smaybee hdr->b_datacnt += 1; 11542688Smaybee return (buf); 11551544Seschrock } 11561544Seschrock 11571544Seschrock void 11581544Seschrock arc_buf_add_ref(arc_buf_t *buf, void* tag) 11591544Seschrock { 11602887Smaybee arc_buf_hdr_t *hdr; 11611544Seschrock kmutex_t *hash_lock; 11621544Seschrock 11632724Smaybee /* 11642724Smaybee * Check to see if this buffer is currently being evicted via 11652887Smaybee * arc_do_user_evicts(). 11662724Smaybee */ 11672887Smaybee mutex_enter(&arc_eviction_mtx); 11682887Smaybee hdr = buf->b_hdr; 11692887Smaybee if (hdr == NULL) { 11702887Smaybee mutex_exit(&arc_eviction_mtx); 11712724Smaybee return; 11722887Smaybee } 11732887Smaybee hash_lock = HDR_LOCK(hdr); 11742887Smaybee mutex_exit(&arc_eviction_mtx); 11752724Smaybee 11762724Smaybee mutex_enter(hash_lock); 11771544Seschrock if (buf->b_data == NULL) { 11781544Seschrock /* 11791544Seschrock * This buffer is evicted. 11801544Seschrock */ 11812724Smaybee mutex_exit(hash_lock); 11821544Seschrock return; 11831544Seschrock } 11841544Seschrock 11852724Smaybee ASSERT(buf->b_hdr == hdr); 11863403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 11871544Seschrock add_reference(hdr, hash_lock, tag); 11882688Smaybee arc_access(hdr, hash_lock); 11892688Smaybee mutex_exit(hash_lock); 11903403Sbmc ARCSTAT_BUMP(arcstat_hits); 11913403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 11923403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 11933403Sbmc data, metadata, hits); 11941544Seschrock } 11951544Seschrock 11965450Sbrendan /* 11975450Sbrendan * Free the arc data buffer. If it is an l2arc write in progress, 11985450Sbrendan * the buffer is placed on l2arc_free_on_write to be freed later. 11995450Sbrendan */ 12005450Sbrendan static void 12015450Sbrendan arc_buf_data_free(arc_buf_hdr_t *hdr, void (*free_func)(void *, size_t), 12025450Sbrendan void *data, size_t size) 12035450Sbrendan { 12045450Sbrendan if (HDR_L2_WRITING(hdr)) { 12055450Sbrendan l2arc_data_free_t *df; 12065450Sbrendan df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP); 12075450Sbrendan df->l2df_data = data; 12085450Sbrendan df->l2df_size = size; 12095450Sbrendan df->l2df_func = free_func; 12105450Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 12115450Sbrendan list_insert_head(l2arc_free_on_write, df); 12125450Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 12135450Sbrendan ARCSTAT_BUMP(arcstat_l2_free_on_write); 12145450Sbrendan } else { 12155450Sbrendan free_func(data, size); 12165450Sbrendan } 12175450Sbrendan } 12185450Sbrendan 1219789Sahrens static void 12202688Smaybee arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all) 12211544Seschrock { 12221544Seschrock arc_buf_t **bufp; 12231544Seschrock 12241544Seschrock /* free up data associated with the buf */ 12251544Seschrock if (buf->b_data) { 12261544Seschrock arc_state_t *state = buf->b_hdr->b_state; 12271544Seschrock uint64_t size = buf->b_hdr->b_size; 12283290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 12291544Seschrock 12303093Sahrens arc_cksum_verify(buf); 12312688Smaybee if (!recycle) { 12323290Sjohansen if (type == ARC_BUFC_METADATA) { 12335450Sbrendan arc_buf_data_free(buf->b_hdr, zio_buf_free, 12345450Sbrendan buf->b_data, size); 12354309Smaybee arc_space_return(size); 12363290Sjohansen } else { 12373290Sjohansen ASSERT(type == ARC_BUFC_DATA); 12385450Sbrendan arc_buf_data_free(buf->b_hdr, 12395450Sbrendan zio_data_buf_free, buf->b_data, size); 12404309Smaybee atomic_add_64(&arc_size, -size); 12413290Sjohansen } 12422688Smaybee } 12431544Seschrock if (list_link_active(&buf->b_hdr->b_arc_node)) { 12444309Smaybee uint64_t *cnt = &state->arcs_lsize[type]; 12454309Smaybee 12461544Seschrock ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt)); 12473403Sbmc ASSERT(state != arc_anon); 12484309Smaybee 12494309Smaybee ASSERT3U(*cnt, >=, size); 12504309Smaybee atomic_add_64(cnt, -size); 12511544Seschrock } 12523403Sbmc ASSERT3U(state->arcs_size, >=, size); 12533403Sbmc atomic_add_64(&state->arcs_size, -size); 12541544Seschrock buf->b_data = NULL; 12551544Seschrock ASSERT(buf->b_hdr->b_datacnt > 0); 12561544Seschrock buf->b_hdr->b_datacnt -= 1; 12571544Seschrock } 12581544Seschrock 12591544Seschrock /* only remove the buf if requested */ 12601544Seschrock if (!all) 12611544Seschrock return; 12621544Seschrock 12631544Seschrock /* remove the buf from the hdr list */ 12641544Seschrock for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next) 12651544Seschrock continue; 12661544Seschrock *bufp = buf->b_next; 12671544Seschrock 12681544Seschrock ASSERT(buf->b_efunc == NULL); 12691544Seschrock 12701544Seschrock /* clean up the buf */ 12711544Seschrock buf->b_hdr = NULL; 12721544Seschrock kmem_cache_free(buf_cache, buf); 12731544Seschrock } 12741544Seschrock 12751544Seschrock static void 12761544Seschrock arc_hdr_destroy(arc_buf_hdr_t *hdr) 1277789Sahrens { 1278789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 12793403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 12801544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 12817046Sahrens ASSERT(!(hdr->b_flags & ARC_STORED)); 1282789Sahrens 12835450Sbrendan if (hdr->b_l2hdr != NULL) { 12845450Sbrendan if (!MUTEX_HELD(&l2arc_buflist_mtx)) { 12855450Sbrendan /* 12865450Sbrendan * To prevent arc_free() and l2arc_evict() from 12875450Sbrendan * attempting to free the same buffer at the same time, 12885450Sbrendan * a FREE_IN_PROGRESS flag is given to arc_free() to 12895450Sbrendan * give it priority. l2arc_evict() can't destroy this 12905450Sbrendan * header while we are waiting on l2arc_buflist_mtx. 12915450Sbrendan */ 12925450Sbrendan mutex_enter(&l2arc_buflist_mtx); 12935450Sbrendan ASSERT(hdr->b_l2hdr != NULL); 12945450Sbrendan 12955450Sbrendan list_remove(hdr->b_l2hdr->b_dev->l2ad_buflist, hdr); 12965450Sbrendan mutex_exit(&l2arc_buflist_mtx); 12975450Sbrendan } else { 12985450Sbrendan list_remove(hdr->b_l2hdr->b_dev->l2ad_buflist, hdr); 12995450Sbrendan } 13005450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 13015450Sbrendan kmem_free(hdr->b_l2hdr, sizeof (l2arc_buf_hdr_t)); 13025450Sbrendan if (hdr->b_state == arc_l2c_only) 13035450Sbrendan l2arc_hdr_stat_remove(); 13045450Sbrendan hdr->b_l2hdr = NULL; 13055450Sbrendan } 13065450Sbrendan 1307789Sahrens if (!BUF_EMPTY(hdr)) { 13081544Seschrock ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1309789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 1310789Sahrens hdr->b_birth = 0; 1311789Sahrens hdr->b_cksum0 = 0; 1312789Sahrens } 13131544Seschrock while (hdr->b_buf) { 1314789Sahrens arc_buf_t *buf = hdr->b_buf; 1315789Sahrens 13161544Seschrock if (buf->b_efunc) { 13171544Seschrock mutex_enter(&arc_eviction_mtx); 13181544Seschrock ASSERT(buf->b_hdr != NULL); 13192688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, FALSE); 13201544Seschrock hdr->b_buf = buf->b_next; 13212887Smaybee buf->b_hdr = &arc_eviction_hdr; 13221544Seschrock buf->b_next = arc_eviction_list; 13231544Seschrock arc_eviction_list = buf; 13241544Seschrock mutex_exit(&arc_eviction_mtx); 13251544Seschrock } else { 13262688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, TRUE); 13271544Seschrock } 1328789Sahrens } 13293093Sahrens if (hdr->b_freeze_cksum != NULL) { 13303093Sahrens kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 13313093Sahrens hdr->b_freeze_cksum = NULL; 13323093Sahrens } 13331544Seschrock 1334789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 1335789Sahrens ASSERT3P(hdr->b_hash_next, ==, NULL); 1336789Sahrens ASSERT3P(hdr->b_acb, ==, NULL); 1337789Sahrens kmem_cache_free(hdr_cache, hdr); 1338789Sahrens } 1339789Sahrens 1340789Sahrens void 1341789Sahrens arc_buf_free(arc_buf_t *buf, void *tag) 1342789Sahrens { 1343789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 13443403Sbmc int hashed = hdr->b_state != arc_anon; 13451544Seschrock 13461544Seschrock ASSERT(buf->b_efunc == NULL); 13471544Seschrock ASSERT(buf->b_data != NULL); 13481544Seschrock 13491544Seschrock if (hashed) { 13501544Seschrock kmutex_t *hash_lock = HDR_LOCK(hdr); 13511544Seschrock 13521544Seschrock mutex_enter(hash_lock); 13531544Seschrock (void) remove_reference(hdr, hash_lock, tag); 13541544Seschrock if (hdr->b_datacnt > 1) 13552688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 13561544Seschrock else 13571544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 13581544Seschrock mutex_exit(hash_lock); 13591544Seschrock } else if (HDR_IO_IN_PROGRESS(hdr)) { 13601544Seschrock int destroy_hdr; 13611544Seschrock /* 13621544Seschrock * We are in the middle of an async write. Don't destroy 13631544Seschrock * this buffer unless the write completes before we finish 13641544Seschrock * decrementing the reference count. 13651544Seschrock */ 13661544Seschrock mutex_enter(&arc_eviction_mtx); 13671544Seschrock (void) remove_reference(hdr, NULL, tag); 13681544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 13691544Seschrock destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 13701544Seschrock mutex_exit(&arc_eviction_mtx); 13711544Seschrock if (destroy_hdr) 13721544Seschrock arc_hdr_destroy(hdr); 13731544Seschrock } else { 13741544Seschrock if (remove_reference(hdr, NULL, tag) > 0) { 13751544Seschrock ASSERT(HDR_IO_ERROR(hdr)); 13762688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 13771544Seschrock } else { 13781544Seschrock arc_hdr_destroy(hdr); 13791544Seschrock } 13801544Seschrock } 13811544Seschrock } 13821544Seschrock 13831544Seschrock int 13841544Seschrock arc_buf_remove_ref(arc_buf_t *buf, void* tag) 13851544Seschrock { 13861544Seschrock arc_buf_hdr_t *hdr = buf->b_hdr; 1387789Sahrens kmutex_t *hash_lock = HDR_LOCK(hdr); 13881544Seschrock int no_callback = (buf->b_efunc == NULL); 13891544Seschrock 13903403Sbmc if (hdr->b_state == arc_anon) { 13911544Seschrock arc_buf_free(buf, tag); 13921544Seschrock return (no_callback); 13931544Seschrock } 1394789Sahrens 1395789Sahrens mutex_enter(hash_lock); 13963403Sbmc ASSERT(hdr->b_state != arc_anon); 13971544Seschrock ASSERT(buf->b_data != NULL); 1398789Sahrens 13991544Seschrock (void) remove_reference(hdr, hash_lock, tag); 14001544Seschrock if (hdr->b_datacnt > 1) { 14011544Seschrock if (no_callback) 14022688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 14031544Seschrock } else if (no_callback) { 14041544Seschrock ASSERT(hdr->b_buf == buf && buf->b_next == NULL); 14051544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 1406789Sahrens } 14071544Seschrock ASSERT(no_callback || hdr->b_datacnt > 1 || 14081544Seschrock refcount_is_zero(&hdr->b_refcnt)); 1409789Sahrens mutex_exit(hash_lock); 14101544Seschrock return (no_callback); 1411789Sahrens } 1412789Sahrens 1413789Sahrens int 1414789Sahrens arc_buf_size(arc_buf_t *buf) 1415789Sahrens { 1416789Sahrens return (buf->b_hdr->b_size); 1417789Sahrens } 1418789Sahrens 1419789Sahrens /* 1420789Sahrens * Evict buffers from list until we've removed the specified number of 1421789Sahrens * bytes. Move the removed buffers to the appropriate evict state. 14222688Smaybee * If the recycle flag is set, then attempt to "recycle" a buffer: 14232688Smaybee * - look for a buffer to evict that is `bytes' long. 14242688Smaybee * - return the data block from this buffer rather than freeing it. 14252688Smaybee * This flag is used by callers that are trying to make space for a 14262688Smaybee * new buffer in a full arc cache. 14275642Smaybee * 14285642Smaybee * This function makes a "best effort". It skips over any buffers 14295642Smaybee * it can't get a hash_lock on, and so may not catch all candidates. 14305642Smaybee * It may also return without evicting as much space as requested. 1431789Sahrens */ 14322688Smaybee static void * 14335642Smaybee arc_evict(arc_state_t *state, spa_t *spa, int64_t bytes, boolean_t recycle, 14343290Sjohansen arc_buf_contents_t type) 1435789Sahrens { 1436789Sahrens arc_state_t *evicted_state; 14372688Smaybee uint64_t bytes_evicted = 0, skipped = 0, missed = 0; 14382918Smaybee arc_buf_hdr_t *ab, *ab_prev = NULL; 14394309Smaybee list_t *list = &state->arcs_list[type]; 1440789Sahrens kmutex_t *hash_lock; 14412688Smaybee boolean_t have_lock; 14422918Smaybee void *stolen = NULL; 1443789Sahrens 14443403Sbmc ASSERT(state == arc_mru || state == arc_mfu); 1445789Sahrens 14463403Sbmc evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 1447789Sahrens 14483403Sbmc mutex_enter(&state->arcs_mtx); 14493403Sbmc mutex_enter(&evicted_state->arcs_mtx); 1450789Sahrens 14514309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 14524309Smaybee ab_prev = list_prev(list, ab); 14532391Smaybee /* prefetch buffers have a minimum lifespan */ 14542688Smaybee if (HDR_IO_IN_PROGRESS(ab) || 14555642Smaybee (spa && ab->b_spa != spa) || 14562688Smaybee (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) && 14572688Smaybee lbolt - ab->b_arc_access < arc_min_prefetch_lifespan)) { 14582391Smaybee skipped++; 14592391Smaybee continue; 14602391Smaybee } 14612918Smaybee /* "lookahead" for better eviction candidate */ 14622918Smaybee if (recycle && ab->b_size != bytes && 14632918Smaybee ab_prev && ab_prev->b_size == bytes) 14642688Smaybee continue; 1465789Sahrens hash_lock = HDR_LOCK(ab); 14662688Smaybee have_lock = MUTEX_HELD(hash_lock); 14672688Smaybee if (have_lock || mutex_tryenter(hash_lock)) { 1468789Sahrens ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0); 14691544Seschrock ASSERT(ab->b_datacnt > 0); 14701544Seschrock while (ab->b_buf) { 14711544Seschrock arc_buf_t *buf = ab->b_buf; 14722688Smaybee if (buf->b_data) { 14731544Seschrock bytes_evicted += ab->b_size; 14743290Sjohansen if (recycle && ab->b_type == type && 14755450Sbrendan ab->b_size == bytes && 14765450Sbrendan !HDR_L2_WRITING(ab)) { 14772918Smaybee stolen = buf->b_data; 14782918Smaybee recycle = FALSE; 14792918Smaybee } 14802688Smaybee } 14811544Seschrock if (buf->b_efunc) { 14821544Seschrock mutex_enter(&arc_eviction_mtx); 14832918Smaybee arc_buf_destroy(buf, 14842918Smaybee buf->b_data == stolen, FALSE); 14851544Seschrock ab->b_buf = buf->b_next; 14862887Smaybee buf->b_hdr = &arc_eviction_hdr; 14871544Seschrock buf->b_next = arc_eviction_list; 14881544Seschrock arc_eviction_list = buf; 14891544Seschrock mutex_exit(&arc_eviction_mtx); 14901544Seschrock } else { 14912918Smaybee arc_buf_destroy(buf, 14922918Smaybee buf->b_data == stolen, TRUE); 14931544Seschrock } 14941544Seschrock } 14951544Seschrock ASSERT(ab->b_datacnt == 0); 1496789Sahrens arc_change_state(evicted_state, ab, hash_lock); 14971544Seschrock ASSERT(HDR_IN_HASH_TABLE(ab)); 14985450Sbrendan ab->b_flags |= ARC_IN_HASH_TABLE; 14995450Sbrendan ab->b_flags &= ~ARC_BUF_AVAILABLE; 1500789Sahrens DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab); 15012688Smaybee if (!have_lock) 15022688Smaybee mutex_exit(hash_lock); 15031544Seschrock if (bytes >= 0 && bytes_evicted >= bytes) 1504789Sahrens break; 1505789Sahrens } else { 15062688Smaybee missed += 1; 1507789Sahrens } 1508789Sahrens } 15093403Sbmc 15103403Sbmc mutex_exit(&evicted_state->arcs_mtx); 15113403Sbmc mutex_exit(&state->arcs_mtx); 1512789Sahrens 1513789Sahrens if (bytes_evicted < bytes) 1514789Sahrens dprintf("only evicted %lld bytes from %x", 1515789Sahrens (longlong_t)bytes_evicted, state); 1516789Sahrens 15172688Smaybee if (skipped) 15183403Sbmc ARCSTAT_INCR(arcstat_evict_skip, skipped); 15193403Sbmc 15202688Smaybee if (missed) 15213403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, missed); 15223403Sbmc 15234709Smaybee /* 15244709Smaybee * We have just evicted some date into the ghost state, make 15254709Smaybee * sure we also adjust the ghost state size if necessary. 15264709Smaybee */ 15274709Smaybee if (arc_no_grow && 15284709Smaybee arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) { 15294709Smaybee int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size + 15304709Smaybee arc_mru_ghost->arcs_size - arc_c; 15314709Smaybee 15324709Smaybee if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) { 15334709Smaybee int64_t todelete = 15344709Smaybee MIN(arc_mru_ghost->arcs_lsize[type], mru_over); 15355642Smaybee arc_evict_ghost(arc_mru_ghost, NULL, todelete); 15364709Smaybee } else if (arc_mfu_ghost->arcs_lsize[type] > 0) { 15374709Smaybee int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type], 15384709Smaybee arc_mru_ghost->arcs_size + 15394709Smaybee arc_mfu_ghost->arcs_size - arc_c); 15405642Smaybee arc_evict_ghost(arc_mfu_ghost, NULL, todelete); 15414709Smaybee } 15424709Smaybee } 15434709Smaybee 15442918Smaybee return (stolen); 1545789Sahrens } 1546789Sahrens 1547789Sahrens /* 1548789Sahrens * Remove buffers from list until we've removed the specified number of 1549789Sahrens * bytes. Destroy the buffers that are removed. 1550789Sahrens */ 1551789Sahrens static void 15525642Smaybee arc_evict_ghost(arc_state_t *state, spa_t *spa, int64_t bytes) 1553789Sahrens { 1554789Sahrens arc_buf_hdr_t *ab, *ab_prev; 15554309Smaybee list_t *list = &state->arcs_list[ARC_BUFC_DATA]; 1556789Sahrens kmutex_t *hash_lock; 15571544Seschrock uint64_t bytes_deleted = 0; 15583700Sek110237 uint64_t bufs_skipped = 0; 1559789Sahrens 15601544Seschrock ASSERT(GHOST_STATE(state)); 1561789Sahrens top: 15623403Sbmc mutex_enter(&state->arcs_mtx); 15634309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 15644309Smaybee ab_prev = list_prev(list, ab); 15655642Smaybee if (spa && ab->b_spa != spa) 15665642Smaybee continue; 1567789Sahrens hash_lock = HDR_LOCK(ab); 1568789Sahrens if (mutex_tryenter(hash_lock)) { 15692391Smaybee ASSERT(!HDR_IO_IN_PROGRESS(ab)); 15701544Seschrock ASSERT(ab->b_buf == NULL); 15713403Sbmc ARCSTAT_BUMP(arcstat_deleted); 15721544Seschrock bytes_deleted += ab->b_size; 15735450Sbrendan 15745450Sbrendan if (ab->b_l2hdr != NULL) { 15755450Sbrendan /* 15765450Sbrendan * This buffer is cached on the 2nd Level ARC; 15775450Sbrendan * don't destroy the header. 15785450Sbrendan */ 15795450Sbrendan arc_change_state(arc_l2c_only, ab, hash_lock); 15805450Sbrendan mutex_exit(hash_lock); 15815450Sbrendan } else { 15825450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 15835450Sbrendan mutex_exit(hash_lock); 15845450Sbrendan arc_hdr_destroy(ab); 15855450Sbrendan } 15865450Sbrendan 1587789Sahrens DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab); 1588789Sahrens if (bytes >= 0 && bytes_deleted >= bytes) 1589789Sahrens break; 1590789Sahrens } else { 1591789Sahrens if (bytes < 0) { 15923403Sbmc mutex_exit(&state->arcs_mtx); 1593789Sahrens mutex_enter(hash_lock); 1594789Sahrens mutex_exit(hash_lock); 1595789Sahrens goto top; 1596789Sahrens } 1597789Sahrens bufs_skipped += 1; 1598789Sahrens } 1599789Sahrens } 16003403Sbmc mutex_exit(&state->arcs_mtx); 1601789Sahrens 16024309Smaybee if (list == &state->arcs_list[ARC_BUFC_DATA] && 16034309Smaybee (bytes < 0 || bytes_deleted < bytes)) { 16044309Smaybee list = &state->arcs_list[ARC_BUFC_METADATA]; 16054309Smaybee goto top; 16064309Smaybee } 16074309Smaybee 1608789Sahrens if (bufs_skipped) { 16093403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped); 1610789Sahrens ASSERT(bytes >= 0); 1611789Sahrens } 1612789Sahrens 1613789Sahrens if (bytes_deleted < bytes) 1614789Sahrens dprintf("only deleted %lld bytes from %p", 1615789Sahrens (longlong_t)bytes_deleted, state); 1616789Sahrens } 1617789Sahrens 1618789Sahrens static void 1619789Sahrens arc_adjust(void) 1620789Sahrens { 16213403Sbmc int64_t top_sz, mru_over, arc_over, todelete; 1622789Sahrens 16235642Smaybee top_sz = arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used; 1624789Sahrens 16254309Smaybee if (top_sz > arc_p && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) { 16264309Smaybee int64_t toevict = 16274309Smaybee MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], top_sz - arc_p); 16285642Smaybee (void) arc_evict(arc_mru, NULL, toevict, FALSE, ARC_BUFC_DATA); 16294309Smaybee top_sz = arc_anon->arcs_size + arc_mru->arcs_size; 16304309Smaybee } 16314309Smaybee 16324309Smaybee if (top_sz > arc_p && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) { 16334309Smaybee int64_t toevict = 16344309Smaybee MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], top_sz - arc_p); 16355642Smaybee (void) arc_evict(arc_mru, NULL, toevict, FALSE, 16365642Smaybee ARC_BUFC_METADATA); 16373403Sbmc top_sz = arc_anon->arcs_size + arc_mru->arcs_size; 1638789Sahrens } 1639789Sahrens 16403403Sbmc mru_over = top_sz + arc_mru_ghost->arcs_size - arc_c; 1641789Sahrens 1642789Sahrens if (mru_over > 0) { 16434309Smaybee if (arc_mru_ghost->arcs_size > 0) { 16444309Smaybee todelete = MIN(arc_mru_ghost->arcs_size, mru_over); 16455642Smaybee arc_evict_ghost(arc_mru_ghost, NULL, todelete); 1646789Sahrens } 1647789Sahrens } 1648789Sahrens 16493403Sbmc if ((arc_over = arc_size - arc_c) > 0) { 16501544Seschrock int64_t tbl_over; 1651789Sahrens 16524309Smaybee if (arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) { 16534309Smaybee int64_t toevict = 16544309Smaybee MIN(arc_mfu->arcs_lsize[ARC_BUFC_DATA], arc_over); 16555642Smaybee (void) arc_evict(arc_mfu, NULL, toevict, FALSE, 16564309Smaybee ARC_BUFC_DATA); 16574309Smaybee arc_over = arc_size - arc_c; 1658789Sahrens } 1659789Sahrens 16604309Smaybee if (arc_over > 0 && 16614309Smaybee arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) { 16624309Smaybee int64_t toevict = 16634309Smaybee MIN(arc_mfu->arcs_lsize[ARC_BUFC_METADATA], 16644309Smaybee arc_over); 16655642Smaybee (void) arc_evict(arc_mfu, NULL, toevict, FALSE, 16664309Smaybee ARC_BUFC_METADATA); 16674309Smaybee } 16684309Smaybee 16694309Smaybee tbl_over = arc_size + arc_mru_ghost->arcs_size + 16704309Smaybee arc_mfu_ghost->arcs_size - arc_c * 2; 16714309Smaybee 16724309Smaybee if (tbl_over > 0 && arc_mfu_ghost->arcs_size > 0) { 16734309Smaybee todelete = MIN(arc_mfu_ghost->arcs_size, tbl_over); 16745642Smaybee arc_evict_ghost(arc_mfu_ghost, NULL, todelete); 1675789Sahrens } 1676789Sahrens } 1677789Sahrens } 1678789Sahrens 16791544Seschrock static void 16801544Seschrock arc_do_user_evicts(void) 16811544Seschrock { 16821544Seschrock mutex_enter(&arc_eviction_mtx); 16831544Seschrock while (arc_eviction_list != NULL) { 16841544Seschrock arc_buf_t *buf = arc_eviction_list; 16851544Seschrock arc_eviction_list = buf->b_next; 16861544Seschrock buf->b_hdr = NULL; 16871544Seschrock mutex_exit(&arc_eviction_mtx); 16881544Seschrock 16891819Smaybee if (buf->b_efunc != NULL) 16901819Smaybee VERIFY(buf->b_efunc(buf) == 0); 16911544Seschrock 16921544Seschrock buf->b_efunc = NULL; 16931544Seschrock buf->b_private = NULL; 16941544Seschrock kmem_cache_free(buf_cache, buf); 16951544Seschrock mutex_enter(&arc_eviction_mtx); 16961544Seschrock } 16971544Seschrock mutex_exit(&arc_eviction_mtx); 16981544Seschrock } 16991544Seschrock 1700789Sahrens /* 17015642Smaybee * Flush all *evictable* data from the cache for the given spa. 1702789Sahrens * NOTE: this will not touch "active" (i.e. referenced) data. 1703789Sahrens */ 1704789Sahrens void 17055642Smaybee arc_flush(spa_t *spa) 1706789Sahrens { 17075642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) { 17085642Smaybee (void) arc_evict(arc_mru, spa, -1, FALSE, ARC_BUFC_DATA); 17095642Smaybee if (spa) 17105642Smaybee break; 17115642Smaybee } 17125642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) { 17135642Smaybee (void) arc_evict(arc_mru, spa, -1, FALSE, ARC_BUFC_METADATA); 17145642Smaybee if (spa) 17155642Smaybee break; 17165642Smaybee } 17175642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) { 17185642Smaybee (void) arc_evict(arc_mfu, spa, -1, FALSE, ARC_BUFC_DATA); 17195642Smaybee if (spa) 17205642Smaybee break; 17215642Smaybee } 17225642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) { 17235642Smaybee (void) arc_evict(arc_mfu, spa, -1, FALSE, ARC_BUFC_METADATA); 17245642Smaybee if (spa) 17255642Smaybee break; 17265642Smaybee } 17275642Smaybee 17285642Smaybee arc_evict_ghost(arc_mru_ghost, spa, -1); 17295642Smaybee arc_evict_ghost(arc_mfu_ghost, spa, -1); 17301544Seschrock 17311544Seschrock mutex_enter(&arc_reclaim_thr_lock); 17321544Seschrock arc_do_user_evicts(); 17331544Seschrock mutex_exit(&arc_reclaim_thr_lock); 17345642Smaybee ASSERT(spa || arc_eviction_list == NULL); 1735789Sahrens } 1736789Sahrens 17373158Smaybee int arc_shrink_shift = 5; /* log2(fraction of arc to reclaim) */ 17382391Smaybee 1739789Sahrens void 17403158Smaybee arc_shrink(void) 1741789Sahrens { 17423403Sbmc if (arc_c > arc_c_min) { 17433158Smaybee uint64_t to_free; 1744789Sahrens 17452048Sstans #ifdef _KERNEL 17463403Sbmc to_free = MAX(arc_c >> arc_shrink_shift, ptob(needfree)); 17472048Sstans #else 17483403Sbmc to_free = arc_c >> arc_shrink_shift; 17492048Sstans #endif 17503403Sbmc if (arc_c > arc_c_min + to_free) 17513403Sbmc atomic_add_64(&arc_c, -to_free); 17523158Smaybee else 17533403Sbmc arc_c = arc_c_min; 17542048Sstans 17553403Sbmc atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 17563403Sbmc if (arc_c > arc_size) 17573403Sbmc arc_c = MAX(arc_size, arc_c_min); 17583403Sbmc if (arc_p > arc_c) 17593403Sbmc arc_p = (arc_c >> 1); 17603403Sbmc ASSERT(arc_c >= arc_c_min); 17613403Sbmc ASSERT((int64_t)arc_p >= 0); 17623158Smaybee } 1763789Sahrens 17643403Sbmc if (arc_size > arc_c) 17653158Smaybee arc_adjust(); 1766789Sahrens } 1767789Sahrens 1768789Sahrens static int 1769789Sahrens arc_reclaim_needed(void) 1770789Sahrens { 1771789Sahrens uint64_t extra; 1772789Sahrens 1773789Sahrens #ifdef _KERNEL 17742048Sstans 17752048Sstans if (needfree) 17762048Sstans return (1); 17772048Sstans 1778789Sahrens /* 1779789Sahrens * take 'desfree' extra pages, so we reclaim sooner, rather than later 1780789Sahrens */ 1781789Sahrens extra = desfree; 1782789Sahrens 1783789Sahrens /* 1784789Sahrens * check that we're out of range of the pageout scanner. It starts to 1785789Sahrens * schedule paging if freemem is less than lotsfree and needfree. 1786789Sahrens * lotsfree is the high-water mark for pageout, and needfree is the 1787789Sahrens * number of needed free pages. We add extra pages here to make sure 1788789Sahrens * the scanner doesn't start up while we're freeing memory. 1789789Sahrens */ 1790789Sahrens if (freemem < lotsfree + needfree + extra) 1791789Sahrens return (1); 1792789Sahrens 1793789Sahrens /* 1794789Sahrens * check to make sure that swapfs has enough space so that anon 17955450Sbrendan * reservations can still succeed. anon_resvmem() checks that the 1796789Sahrens * availrmem is greater than swapfs_minfree, and the number of reserved 1797789Sahrens * swap pages. We also add a bit of extra here just to prevent 1798789Sahrens * circumstances from getting really dire. 1799789Sahrens */ 1800789Sahrens if (availrmem < swapfs_minfree + swapfs_reserve + extra) 1801789Sahrens return (1); 1802789Sahrens 18031936Smaybee #if defined(__i386) 1804789Sahrens /* 1805789Sahrens * If we're on an i386 platform, it's possible that we'll exhaust the 1806789Sahrens * kernel heap space before we ever run out of available physical 1807789Sahrens * memory. Most checks of the size of the heap_area compare against 1808789Sahrens * tune.t_minarmem, which is the minimum available real memory that we 1809789Sahrens * can have in the system. However, this is generally fixed at 25 pages 1810789Sahrens * which is so low that it's useless. In this comparison, we seek to 1811789Sahrens * calculate the total heap-size, and reclaim if more than 3/4ths of the 18125450Sbrendan * heap is allocated. (Or, in the calculation, if less than 1/4th is 1813789Sahrens * free) 1814789Sahrens */ 1815789Sahrens if (btop(vmem_size(heap_arena, VMEM_FREE)) < 1816789Sahrens (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2)) 1817789Sahrens return (1); 1818789Sahrens #endif 1819789Sahrens 1820789Sahrens #else 1821789Sahrens if (spa_get_random(100) == 0) 1822789Sahrens return (1); 1823789Sahrens #endif 1824789Sahrens return (0); 1825789Sahrens } 1826789Sahrens 1827789Sahrens static void 1828789Sahrens arc_kmem_reap_now(arc_reclaim_strategy_t strat) 1829789Sahrens { 1830789Sahrens size_t i; 1831789Sahrens kmem_cache_t *prev_cache = NULL; 18323290Sjohansen kmem_cache_t *prev_data_cache = NULL; 1833789Sahrens extern kmem_cache_t *zio_buf_cache[]; 18343290Sjohansen extern kmem_cache_t *zio_data_buf_cache[]; 1835789Sahrens 18361484Sek110237 #ifdef _KERNEL 18374309Smaybee if (arc_meta_used >= arc_meta_limit) { 18384309Smaybee /* 18394309Smaybee * We are exceeding our meta-data cache limit. 18404309Smaybee * Purge some DNLC entries to release holds on meta-data. 18414309Smaybee */ 18424309Smaybee dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 18434309Smaybee } 18441936Smaybee #if defined(__i386) 18451936Smaybee /* 18461936Smaybee * Reclaim unused memory from all kmem caches. 18471936Smaybee */ 18481936Smaybee kmem_reap(); 18491936Smaybee #endif 18501484Sek110237 #endif 18511484Sek110237 1852789Sahrens /* 18535450Sbrendan * An aggressive reclamation will shrink the cache size as well as 18541544Seschrock * reap free buffers from the arc kmem caches. 1855789Sahrens */ 1856789Sahrens if (strat == ARC_RECLAIM_AGGR) 18573158Smaybee arc_shrink(); 1858789Sahrens 1859789Sahrens for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 1860789Sahrens if (zio_buf_cache[i] != prev_cache) { 1861789Sahrens prev_cache = zio_buf_cache[i]; 1862789Sahrens kmem_cache_reap_now(zio_buf_cache[i]); 1863789Sahrens } 18643290Sjohansen if (zio_data_buf_cache[i] != prev_data_cache) { 18653290Sjohansen prev_data_cache = zio_data_buf_cache[i]; 18663290Sjohansen kmem_cache_reap_now(zio_data_buf_cache[i]); 18673290Sjohansen } 1868789Sahrens } 18691544Seschrock kmem_cache_reap_now(buf_cache); 18701544Seschrock kmem_cache_reap_now(hdr_cache); 1871789Sahrens } 1872789Sahrens 1873789Sahrens static void 1874789Sahrens arc_reclaim_thread(void) 1875789Sahrens { 1876789Sahrens clock_t growtime = 0; 1877789Sahrens arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 1878789Sahrens callb_cpr_t cpr; 1879789Sahrens 1880789Sahrens CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 1881789Sahrens 1882789Sahrens mutex_enter(&arc_reclaim_thr_lock); 1883789Sahrens while (arc_thread_exit == 0) { 1884789Sahrens if (arc_reclaim_needed()) { 1885789Sahrens 18863403Sbmc if (arc_no_grow) { 1887789Sahrens if (last_reclaim == ARC_RECLAIM_CONS) { 1888789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 1889789Sahrens } else { 1890789Sahrens last_reclaim = ARC_RECLAIM_CONS; 1891789Sahrens } 1892789Sahrens } else { 18933403Sbmc arc_no_grow = TRUE; 1894789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 1895789Sahrens membar_producer(); 1896789Sahrens } 1897789Sahrens 1898789Sahrens /* reset the growth delay for every reclaim */ 1899789Sahrens growtime = lbolt + (arc_grow_retry * hz); 1900789Sahrens 1901789Sahrens arc_kmem_reap_now(last_reclaim); 19026987Sbrendan arc_warm = B_TRUE; 1903789Sahrens 19044309Smaybee } else if (arc_no_grow && lbolt >= growtime) { 19053403Sbmc arc_no_grow = FALSE; 1906789Sahrens } 1907789Sahrens 19083403Sbmc if (2 * arc_c < arc_size + 19093403Sbmc arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size) 19103298Smaybee arc_adjust(); 19113298Smaybee 19121544Seschrock if (arc_eviction_list != NULL) 19131544Seschrock arc_do_user_evicts(); 19141544Seschrock 1915789Sahrens /* block until needed, or one second, whichever is shorter */ 1916789Sahrens CALLB_CPR_SAFE_BEGIN(&cpr); 1917789Sahrens (void) cv_timedwait(&arc_reclaim_thr_cv, 1918789Sahrens &arc_reclaim_thr_lock, (lbolt + hz)); 1919789Sahrens CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 1920789Sahrens } 1921789Sahrens 1922789Sahrens arc_thread_exit = 0; 1923789Sahrens cv_broadcast(&arc_reclaim_thr_cv); 1924789Sahrens CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 1925789Sahrens thread_exit(); 1926789Sahrens } 1927789Sahrens 19281544Seschrock /* 19291544Seschrock * Adapt arc info given the number of bytes we are trying to add and 19301544Seschrock * the state that we are comming from. This function is only called 19311544Seschrock * when we are adding new content to the cache. 19321544Seschrock */ 1933789Sahrens static void 19341544Seschrock arc_adapt(int bytes, arc_state_t *state) 1935789Sahrens { 19361544Seschrock int mult; 19371544Seschrock 19385450Sbrendan if (state == arc_l2c_only) 19395450Sbrendan return; 19405450Sbrendan 19411544Seschrock ASSERT(bytes > 0); 1942789Sahrens /* 19431544Seschrock * Adapt the target size of the MRU list: 19441544Seschrock * - if we just hit in the MRU ghost list, then increase 19451544Seschrock * the target size of the MRU list. 19461544Seschrock * - if we just hit in the MFU ghost list, then increase 19471544Seschrock * the target size of the MFU list by decreasing the 19481544Seschrock * target size of the MRU list. 1949789Sahrens */ 19503403Sbmc if (state == arc_mru_ghost) { 19513403Sbmc mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? 19523403Sbmc 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); 19531544Seschrock 19543403Sbmc arc_p = MIN(arc_c, arc_p + bytes * mult); 19553403Sbmc } else if (state == arc_mfu_ghost) { 19563403Sbmc mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? 19573403Sbmc 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); 19581544Seschrock 19593403Sbmc arc_p = MAX(0, (int64_t)arc_p - bytes * mult); 19601544Seschrock } 19613403Sbmc ASSERT((int64_t)arc_p >= 0); 1962789Sahrens 1963789Sahrens if (arc_reclaim_needed()) { 1964789Sahrens cv_signal(&arc_reclaim_thr_cv); 1965789Sahrens return; 1966789Sahrens } 1967789Sahrens 19683403Sbmc if (arc_no_grow) 1969789Sahrens return; 1970789Sahrens 19713403Sbmc if (arc_c >= arc_c_max) 19721544Seschrock return; 19731544Seschrock 1974789Sahrens /* 19751544Seschrock * If we're within (2 * maxblocksize) bytes of the target 19761544Seschrock * cache size, increment the target cache size 1977789Sahrens */ 19783403Sbmc if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 19793403Sbmc atomic_add_64(&arc_c, (int64_t)bytes); 19803403Sbmc if (arc_c > arc_c_max) 19813403Sbmc arc_c = arc_c_max; 19823403Sbmc else if (state == arc_anon) 19833403Sbmc atomic_add_64(&arc_p, (int64_t)bytes); 19843403Sbmc if (arc_p > arc_c) 19853403Sbmc arc_p = arc_c; 1986789Sahrens } 19873403Sbmc ASSERT((int64_t)arc_p >= 0); 1988789Sahrens } 1989789Sahrens 1990789Sahrens /* 19911544Seschrock * Check if the cache has reached its limits and eviction is required 19921544Seschrock * prior to insert. 1993789Sahrens */ 1994789Sahrens static int 19954309Smaybee arc_evict_needed(arc_buf_contents_t type) 1996789Sahrens { 19974309Smaybee if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit) 19984309Smaybee return (1); 19994309Smaybee 20004309Smaybee #ifdef _KERNEL 20014309Smaybee /* 20024309Smaybee * If zio data pages are being allocated out of a separate heap segment, 20034309Smaybee * then enforce that the size of available vmem for this area remains 20044309Smaybee * above about 1/32nd free. 20054309Smaybee */ 20064309Smaybee if (type == ARC_BUFC_DATA && zio_arena != NULL && 20074309Smaybee vmem_size(zio_arena, VMEM_FREE) < 20084309Smaybee (vmem_size(zio_arena, VMEM_ALLOC) >> 5)) 20094309Smaybee return (1); 20104309Smaybee #endif 20114309Smaybee 2012789Sahrens if (arc_reclaim_needed()) 2013789Sahrens return (1); 2014789Sahrens 20153403Sbmc return (arc_size > arc_c); 2016789Sahrens } 2017789Sahrens 2018789Sahrens /* 20192688Smaybee * The buffer, supplied as the first argument, needs a data block. 20202688Smaybee * So, if we are at cache max, determine which cache should be victimized. 20212688Smaybee * We have the following cases: 2022789Sahrens * 20233403Sbmc * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) -> 2024789Sahrens * In this situation if we're out of space, but the resident size of the MFU is 2025789Sahrens * under the limit, victimize the MFU cache to satisfy this insertion request. 2026789Sahrens * 20273403Sbmc * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) -> 2028789Sahrens * Here, we've used up all of the available space for the MRU, so we need to 2029789Sahrens * evict from our own cache instead. Evict from the set of resident MRU 2030789Sahrens * entries. 2031789Sahrens * 20323403Sbmc * 3. Insert for MFU (c - p) > sizeof(arc_mfu) -> 2033789Sahrens * c minus p represents the MFU space in the cache, since p is the size of the 2034789Sahrens * cache that is dedicated to the MRU. In this situation there's still space on 2035789Sahrens * the MFU side, so the MRU side needs to be victimized. 2036789Sahrens * 20373403Sbmc * 4. Insert for MFU (c - p) < sizeof(arc_mfu) -> 2038789Sahrens * MFU's resident set is consuming more space than it has been allotted. In 2039789Sahrens * this situation, we must victimize our own cache, the MFU, for this insertion. 2040789Sahrens */ 2041789Sahrens static void 20422688Smaybee arc_get_data_buf(arc_buf_t *buf) 2043789Sahrens { 20443290Sjohansen arc_state_t *state = buf->b_hdr->b_state; 20453290Sjohansen uint64_t size = buf->b_hdr->b_size; 20463290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 20472688Smaybee 20482688Smaybee arc_adapt(size, state); 2049789Sahrens 20502688Smaybee /* 20512688Smaybee * We have not yet reached cache maximum size, 20522688Smaybee * just allocate a new buffer. 20532688Smaybee */ 20544309Smaybee if (!arc_evict_needed(type)) { 20553290Sjohansen if (type == ARC_BUFC_METADATA) { 20563290Sjohansen buf->b_data = zio_buf_alloc(size); 20574309Smaybee arc_space_consume(size); 20583290Sjohansen } else { 20593290Sjohansen ASSERT(type == ARC_BUFC_DATA); 20603290Sjohansen buf->b_data = zio_data_buf_alloc(size); 20614309Smaybee atomic_add_64(&arc_size, size); 20623290Sjohansen } 20632688Smaybee goto out; 20642688Smaybee } 20652688Smaybee 20662688Smaybee /* 20672688Smaybee * If we are prefetching from the mfu ghost list, this buffer 20682688Smaybee * will end up on the mru list; so steal space from there. 20692688Smaybee */ 20703403Sbmc if (state == arc_mfu_ghost) 20713403Sbmc state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu; 20723403Sbmc else if (state == arc_mru_ghost) 20733403Sbmc state = arc_mru; 2074789Sahrens 20753403Sbmc if (state == arc_mru || state == arc_anon) { 20763403Sbmc uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size; 20774309Smaybee state = (arc_mfu->arcs_lsize[type] > 0 && 20784309Smaybee arc_p > mru_used) ? arc_mfu : arc_mru; 2079789Sahrens } else { 20802688Smaybee /* MFU cases */ 20813403Sbmc uint64_t mfu_space = arc_c - arc_p; 20824309Smaybee state = (arc_mru->arcs_lsize[type] > 0 && 20834309Smaybee mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu; 20842688Smaybee } 20855642Smaybee if ((buf->b_data = arc_evict(state, NULL, size, TRUE, type)) == NULL) { 20863290Sjohansen if (type == ARC_BUFC_METADATA) { 20873290Sjohansen buf->b_data = zio_buf_alloc(size); 20884309Smaybee arc_space_consume(size); 20893290Sjohansen } else { 20903290Sjohansen ASSERT(type == ARC_BUFC_DATA); 20913290Sjohansen buf->b_data = zio_data_buf_alloc(size); 20924309Smaybee atomic_add_64(&arc_size, size); 20933290Sjohansen } 20943403Sbmc ARCSTAT_BUMP(arcstat_recycle_miss); 20952688Smaybee } 20962688Smaybee ASSERT(buf->b_data != NULL); 20972688Smaybee out: 20982688Smaybee /* 20992688Smaybee * Update the state size. Note that ghost states have a 21002688Smaybee * "ghost size" and so don't need to be updated. 21012688Smaybee */ 21022688Smaybee if (!GHOST_STATE(buf->b_hdr->b_state)) { 21032688Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 21042688Smaybee 21053403Sbmc atomic_add_64(&hdr->b_state->arcs_size, size); 21062688Smaybee if (list_link_active(&hdr->b_arc_node)) { 21072688Smaybee ASSERT(refcount_is_zero(&hdr->b_refcnt)); 21084309Smaybee atomic_add_64(&hdr->b_state->arcs_lsize[type], size); 2109789Sahrens } 21103298Smaybee /* 21113298Smaybee * If we are growing the cache, and we are adding anonymous 21123403Sbmc * data, and we have outgrown arc_p, update arc_p 21133298Smaybee */ 21143403Sbmc if (arc_size < arc_c && hdr->b_state == arc_anon && 21153403Sbmc arc_anon->arcs_size + arc_mru->arcs_size > arc_p) 21163403Sbmc arc_p = MIN(arc_c, arc_p + size); 2117789Sahrens } 2118789Sahrens } 2119789Sahrens 2120789Sahrens /* 2121789Sahrens * This routine is called whenever a buffer is accessed. 21221544Seschrock * NOTE: the hash lock is dropped in this function. 2123789Sahrens */ 2124789Sahrens static void 21252688Smaybee arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock) 2126789Sahrens { 2127789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 2128789Sahrens 21293403Sbmc if (buf->b_state == arc_anon) { 2130789Sahrens /* 2131789Sahrens * This buffer is not in the cache, and does not 2132789Sahrens * appear in our "ghost" list. Add the new buffer 2133789Sahrens * to the MRU state. 2134789Sahrens */ 2135789Sahrens 2136789Sahrens ASSERT(buf->b_arc_access == 0); 2137789Sahrens buf->b_arc_access = lbolt; 21381544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 21393403Sbmc arc_change_state(arc_mru, buf, hash_lock); 2140789Sahrens 21413403Sbmc } else if (buf->b_state == arc_mru) { 2142789Sahrens /* 21432391Smaybee * If this buffer is here because of a prefetch, then either: 21442391Smaybee * - clear the flag if this is a "referencing" read 21452391Smaybee * (any subsequent access will bump this into the MFU state). 21462391Smaybee * or 21472391Smaybee * - move the buffer to the head of the list if this is 21482391Smaybee * another prefetch (to make it less likely to be evicted). 2149789Sahrens */ 2150789Sahrens if ((buf->b_flags & ARC_PREFETCH) != 0) { 21512391Smaybee if (refcount_count(&buf->b_refcnt) == 0) { 21522391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 21532391Smaybee } else { 21542391Smaybee buf->b_flags &= ~ARC_PREFETCH; 21553403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 21562391Smaybee } 21572391Smaybee buf->b_arc_access = lbolt; 2158789Sahrens return; 2159789Sahrens } 2160789Sahrens 2161789Sahrens /* 2162789Sahrens * This buffer has been "accessed" only once so far, 2163789Sahrens * but it is still in the cache. Move it to the MFU 2164789Sahrens * state. 2165789Sahrens */ 2166789Sahrens if (lbolt > buf->b_arc_access + ARC_MINTIME) { 2167789Sahrens /* 2168789Sahrens * More than 125ms have passed since we 2169789Sahrens * instantiated this buffer. Move it to the 2170789Sahrens * most frequently used state. 2171789Sahrens */ 2172789Sahrens buf->b_arc_access = lbolt; 21731544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 21743403Sbmc arc_change_state(arc_mfu, buf, hash_lock); 2175789Sahrens } 21763403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 21773403Sbmc } else if (buf->b_state == arc_mru_ghost) { 2178789Sahrens arc_state_t *new_state; 2179789Sahrens /* 2180789Sahrens * This buffer has been "accessed" recently, but 2181789Sahrens * was evicted from the cache. Move it to the 2182789Sahrens * MFU state. 2183789Sahrens */ 2184789Sahrens 2185789Sahrens if (buf->b_flags & ARC_PREFETCH) { 21863403Sbmc new_state = arc_mru; 21872391Smaybee if (refcount_count(&buf->b_refcnt) > 0) 21882391Smaybee buf->b_flags &= ~ARC_PREFETCH; 21891544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2190789Sahrens } else { 21913403Sbmc new_state = arc_mfu; 21921544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2193789Sahrens } 2194789Sahrens 2195789Sahrens buf->b_arc_access = lbolt; 2196789Sahrens arc_change_state(new_state, buf, hash_lock); 2197789Sahrens 21983403Sbmc ARCSTAT_BUMP(arcstat_mru_ghost_hits); 21993403Sbmc } else if (buf->b_state == arc_mfu) { 2200789Sahrens /* 2201789Sahrens * This buffer has been accessed more than once and is 2202789Sahrens * still in the cache. Keep it in the MFU state. 2203789Sahrens * 22042391Smaybee * NOTE: an add_reference() that occurred when we did 22052391Smaybee * the arc_read() will have kicked this off the list. 22062391Smaybee * If it was a prefetch, we will explicitly move it to 22072391Smaybee * the head of the list now. 2208789Sahrens */ 22092391Smaybee if ((buf->b_flags & ARC_PREFETCH) != 0) { 22102391Smaybee ASSERT(refcount_count(&buf->b_refcnt) == 0); 22112391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 22122391Smaybee } 22133403Sbmc ARCSTAT_BUMP(arcstat_mfu_hits); 22142391Smaybee buf->b_arc_access = lbolt; 22153403Sbmc } else if (buf->b_state == arc_mfu_ghost) { 22163403Sbmc arc_state_t *new_state = arc_mfu; 2217789Sahrens /* 2218789Sahrens * This buffer has been accessed more than once but has 2219789Sahrens * been evicted from the cache. Move it back to the 2220789Sahrens * MFU state. 2221789Sahrens */ 2222789Sahrens 22232391Smaybee if (buf->b_flags & ARC_PREFETCH) { 22242391Smaybee /* 22252391Smaybee * This is a prefetch access... 22262391Smaybee * move this block back to the MRU state. 22272391Smaybee */ 22282391Smaybee ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0); 22293403Sbmc new_state = arc_mru; 22302391Smaybee } 22312391Smaybee 2232789Sahrens buf->b_arc_access = lbolt; 22331544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 22342391Smaybee arc_change_state(new_state, buf, hash_lock); 2235789Sahrens 22363403Sbmc ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 22375450Sbrendan } else if (buf->b_state == arc_l2c_only) { 22385450Sbrendan /* 22395450Sbrendan * This buffer is on the 2nd Level ARC. 22405450Sbrendan */ 22415450Sbrendan 22425450Sbrendan buf->b_arc_access = lbolt; 22435450Sbrendan DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 22445450Sbrendan arc_change_state(arc_mfu, buf, hash_lock); 2245789Sahrens } else { 2246789Sahrens ASSERT(!"invalid arc state"); 2247789Sahrens } 2248789Sahrens } 2249789Sahrens 2250789Sahrens /* a generic arc_done_func_t which you can use */ 2251789Sahrens /* ARGSUSED */ 2252789Sahrens void 2253789Sahrens arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 2254789Sahrens { 2255789Sahrens bcopy(buf->b_data, arg, buf->b_hdr->b_size); 22561544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2257789Sahrens } 2258789Sahrens 22594309Smaybee /* a generic arc_done_func_t */ 2260789Sahrens void 2261789Sahrens arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 2262789Sahrens { 2263789Sahrens arc_buf_t **bufp = arg; 2264789Sahrens if (zio && zio->io_error) { 22651544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2266789Sahrens *bufp = NULL; 2267789Sahrens } else { 2268789Sahrens *bufp = buf; 2269789Sahrens } 2270789Sahrens } 2271789Sahrens 2272789Sahrens static void 2273789Sahrens arc_read_done(zio_t *zio) 2274789Sahrens { 22751589Smaybee arc_buf_hdr_t *hdr, *found; 2276789Sahrens arc_buf_t *buf; 2277789Sahrens arc_buf_t *abuf; /* buffer we're assigning to callback */ 2278789Sahrens kmutex_t *hash_lock; 2279789Sahrens arc_callback_t *callback_list, *acb; 2280789Sahrens int freeable = FALSE; 2281789Sahrens 2282789Sahrens buf = zio->io_private; 2283789Sahrens hdr = buf->b_hdr; 2284789Sahrens 22851589Smaybee /* 22861589Smaybee * The hdr was inserted into hash-table and removed from lists 22871589Smaybee * prior to starting I/O. We should find this header, since 22881589Smaybee * it's in the hash table, and it should be legit since it's 22891589Smaybee * not possible to evict it during the I/O. The only possible 22901589Smaybee * reason for it not to be found is if we were freed during the 22911589Smaybee * read. 22921589Smaybee */ 22931589Smaybee found = buf_hash_find(zio->io_spa, &hdr->b_dva, hdr->b_birth, 22943093Sahrens &hash_lock); 2295789Sahrens 22961589Smaybee ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) || 22975450Sbrendan (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 22985450Sbrendan (found == hdr && HDR_L2_READING(hdr))); 22995450Sbrendan 23006987Sbrendan hdr->b_flags &= ~ARC_L2_EVICTED; 23015450Sbrendan if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH)) 2302*7237Sek110237 hdr->b_flags &= ~ARC_L2CACHE; 2303789Sahrens 2304789Sahrens /* byteswap if necessary */ 2305789Sahrens callback_list = hdr->b_acb; 2306789Sahrens ASSERT(callback_list != NULL); 23077046Sahrens if (BP_SHOULD_BYTESWAP(zio->io_bp)) { 23087046Sahrens arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 23097046Sahrens byteswap_uint64_array : 23107046Sahrens dmu_ot[BP_GET_TYPE(zio->io_bp)].ot_byteswap; 23117046Sahrens func(buf->b_data, hdr->b_size); 23127046Sahrens } 2313789Sahrens 23145450Sbrendan arc_cksum_compute(buf, B_FALSE); 23153093Sahrens 2316789Sahrens /* create copies of the data buffer for the callers */ 2317789Sahrens abuf = buf; 2318789Sahrens for (acb = callback_list; acb; acb = acb->acb_next) { 2319789Sahrens if (acb->acb_done) { 23202688Smaybee if (abuf == NULL) 23212688Smaybee abuf = arc_buf_clone(buf); 2322789Sahrens acb->acb_buf = abuf; 2323789Sahrens abuf = NULL; 2324789Sahrens } 2325789Sahrens } 2326789Sahrens hdr->b_acb = NULL; 2327789Sahrens hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 23281544Seschrock ASSERT(!HDR_BUF_AVAILABLE(hdr)); 23291544Seschrock if (abuf == buf) 23301544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 2331789Sahrens 2332789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL); 2333789Sahrens 2334789Sahrens if (zio->io_error != 0) { 2335789Sahrens hdr->b_flags |= ARC_IO_ERROR; 23363403Sbmc if (hdr->b_state != arc_anon) 23373403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 23381544Seschrock if (HDR_IN_HASH_TABLE(hdr)) 23391544Seschrock buf_hash_remove(hdr); 2340789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 23412391Smaybee /* convert checksum errors into IO errors */ 23421544Seschrock if (zio->io_error == ECKSUM) 23431544Seschrock zio->io_error = EIO; 2344789Sahrens } 2345789Sahrens 23461544Seschrock /* 23472391Smaybee * Broadcast before we drop the hash_lock to avoid the possibility 23482391Smaybee * that the hdr (and hence the cv) might be freed before we get to 23492391Smaybee * the cv_broadcast(). 23501544Seschrock */ 23511544Seschrock cv_broadcast(&hdr->b_cv); 23521544Seschrock 23531589Smaybee if (hash_lock) { 2354789Sahrens /* 2355789Sahrens * Only call arc_access on anonymous buffers. This is because 2356789Sahrens * if we've issued an I/O for an evicted buffer, we've already 2357789Sahrens * called arc_access (to prevent any simultaneous readers from 2358789Sahrens * getting confused). 2359789Sahrens */ 23603403Sbmc if (zio->io_error == 0 && hdr->b_state == arc_anon) 23612688Smaybee arc_access(hdr, hash_lock); 23622688Smaybee mutex_exit(hash_lock); 2363789Sahrens } else { 2364789Sahrens /* 2365789Sahrens * This block was freed while we waited for the read to 2366789Sahrens * complete. It has been removed from the hash table and 2367789Sahrens * moved to the anonymous state (so that it won't show up 2368789Sahrens * in the cache). 2369789Sahrens */ 23703403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 2371789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2372789Sahrens } 2373789Sahrens 2374789Sahrens /* execute each callback and free its structure */ 2375789Sahrens while ((acb = callback_list) != NULL) { 2376789Sahrens if (acb->acb_done) 2377789Sahrens acb->acb_done(zio, acb->acb_buf, acb->acb_private); 2378789Sahrens 2379789Sahrens if (acb->acb_zio_dummy != NULL) { 2380789Sahrens acb->acb_zio_dummy->io_error = zio->io_error; 2381789Sahrens zio_nowait(acb->acb_zio_dummy); 2382789Sahrens } 2383789Sahrens 2384789Sahrens callback_list = acb->acb_next; 2385789Sahrens kmem_free(acb, sizeof (arc_callback_t)); 2386789Sahrens } 2387789Sahrens 2388789Sahrens if (freeable) 23891544Seschrock arc_hdr_destroy(hdr); 2390789Sahrens } 2391789Sahrens 2392789Sahrens /* 2393789Sahrens * "Read" the block block at the specified DVA (in bp) via the 2394789Sahrens * cache. If the block is found in the cache, invoke the provided 2395789Sahrens * callback immediately and return. Note that the `zio' parameter 2396789Sahrens * in the callback will be NULL in this case, since no IO was 2397789Sahrens * required. If the block is not in the cache pass the read request 2398789Sahrens * on to the spa with a substitute callback function, so that the 2399789Sahrens * requested block will be added to the cache. 2400789Sahrens * 2401789Sahrens * If a read request arrives for a block that has a read in-progress, 2402789Sahrens * either wait for the in-progress read to complete (and return the 2403789Sahrens * results); or, if this is a read with a "done" func, add a record 2404789Sahrens * to the read to invoke the "done" func when the read completes, 2405789Sahrens * and return; or just return. 2406789Sahrens * 2407789Sahrens * arc_read_done() will invoke all the requested "done" functions 2408789Sahrens * for readers of this block. 24097046Sahrens * 24107046Sahrens * Normal callers should use arc_read and pass the arc buffer and offset 24117046Sahrens * for the bp. But if you know you don't need locking, you can use 24127046Sahrens * arc_read_bp. 2413789Sahrens */ 2414789Sahrens int 24157046Sahrens arc_read(zio_t *pio, spa_t *spa, blkptr_t *bp, arc_buf_t *pbuf, 2416*7237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 24177046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 24187046Sahrens { 24197046Sahrens int err; 24207046Sahrens 24217046Sahrens ASSERT(!refcount_is_zero(&pbuf->b_hdr->b_refcnt)); 24227046Sahrens ASSERT3U((char *)bp - (char *)pbuf->b_data, <, pbuf->b_hdr->b_size); 24237046Sahrens rw_enter(&pbuf->b_hdr->b_datalock, RW_READER); 24247046Sahrens 24257046Sahrens err = arc_read_nolock(pio, spa, bp, done, private, priority, 2426*7237Sek110237 zio_flags, arc_flags, zb); 24277046Sahrens 24287046Sahrens rw_exit(&pbuf->b_hdr->b_datalock); 24297046Sahrens return (err); 24307046Sahrens } 24317046Sahrens 24327046Sahrens int 24337046Sahrens arc_read_nolock(zio_t *pio, spa_t *spa, blkptr_t *bp, 2434*7237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 24357046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 2436789Sahrens { 2437789Sahrens arc_buf_hdr_t *hdr; 2438789Sahrens arc_buf_t *buf; 2439789Sahrens kmutex_t *hash_lock; 24405450Sbrendan zio_t *rzio; 2441789Sahrens 2442789Sahrens top: 2443789Sahrens hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock); 24441544Seschrock if (hdr && hdr->b_datacnt > 0) { 2445789Sahrens 24462391Smaybee *arc_flags |= ARC_CACHED; 24472391Smaybee 2448789Sahrens if (HDR_IO_IN_PROGRESS(hdr)) { 24492391Smaybee 24502391Smaybee if (*arc_flags & ARC_WAIT) { 24512391Smaybee cv_wait(&hdr->b_cv, hash_lock); 24522391Smaybee mutex_exit(hash_lock); 24532391Smaybee goto top; 24542391Smaybee } 24552391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 24562391Smaybee 24572391Smaybee if (done) { 2458789Sahrens arc_callback_t *acb = NULL; 2459789Sahrens 2460789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), 2461789Sahrens KM_SLEEP); 2462789Sahrens acb->acb_done = done; 2463789Sahrens acb->acb_private = private; 2464789Sahrens if (pio != NULL) 2465789Sahrens acb->acb_zio_dummy = zio_null(pio, 2466*7237Sek110237 spa, NULL, NULL, zio_flags); 2467789Sahrens 2468789Sahrens ASSERT(acb->acb_done != NULL); 2469789Sahrens acb->acb_next = hdr->b_acb; 2470789Sahrens hdr->b_acb = acb; 2471789Sahrens add_reference(hdr, hash_lock, private); 2472789Sahrens mutex_exit(hash_lock); 2473789Sahrens return (0); 2474789Sahrens } 2475789Sahrens mutex_exit(hash_lock); 2476789Sahrens return (0); 2477789Sahrens } 2478789Sahrens 24793403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 2480789Sahrens 24811544Seschrock if (done) { 24822688Smaybee add_reference(hdr, hash_lock, private); 24831544Seschrock /* 24841544Seschrock * If this block is already in use, create a new 24851544Seschrock * copy of the data so that we will be guaranteed 24861544Seschrock * that arc_release() will always succeed. 24871544Seschrock */ 24881544Seschrock buf = hdr->b_buf; 24891544Seschrock ASSERT(buf); 24901544Seschrock ASSERT(buf->b_data); 24912688Smaybee if (HDR_BUF_AVAILABLE(hdr)) { 24921544Seschrock ASSERT(buf->b_efunc == NULL); 24931544Seschrock hdr->b_flags &= ~ARC_BUF_AVAILABLE; 24942688Smaybee } else { 24952688Smaybee buf = arc_buf_clone(buf); 24961544Seschrock } 24972391Smaybee } else if (*arc_flags & ARC_PREFETCH && 24982391Smaybee refcount_count(&hdr->b_refcnt) == 0) { 24992391Smaybee hdr->b_flags |= ARC_PREFETCH; 2500789Sahrens } 2501789Sahrens DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 25022688Smaybee arc_access(hdr, hash_lock); 2503*7237Sek110237 if (*arc_flags & ARC_L2CACHE) 2504*7237Sek110237 hdr->b_flags |= ARC_L2CACHE; 25052688Smaybee mutex_exit(hash_lock); 25063403Sbmc ARCSTAT_BUMP(arcstat_hits); 25073403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 25083403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 25093403Sbmc data, metadata, hits); 25103403Sbmc 2511789Sahrens if (done) 2512789Sahrens done(NULL, buf, private); 2513789Sahrens } else { 2514789Sahrens uint64_t size = BP_GET_LSIZE(bp); 2515789Sahrens arc_callback_t *acb; 25166987Sbrendan vdev_t *vd = NULL; 25176987Sbrendan daddr_t addr; 2518789Sahrens 2519789Sahrens if (hdr == NULL) { 2520789Sahrens /* this block is not in the cache */ 2521789Sahrens arc_buf_hdr_t *exists; 25223290Sjohansen arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 25233290Sjohansen buf = arc_buf_alloc(spa, size, private, type); 2524789Sahrens hdr = buf->b_hdr; 2525789Sahrens hdr->b_dva = *BP_IDENTITY(bp); 2526789Sahrens hdr->b_birth = bp->blk_birth; 2527789Sahrens hdr->b_cksum0 = bp->blk_cksum.zc_word[0]; 2528789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 2529789Sahrens if (exists) { 2530789Sahrens /* somebody beat us to the hash insert */ 2531789Sahrens mutex_exit(hash_lock); 2532789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 2533789Sahrens hdr->b_birth = 0; 2534789Sahrens hdr->b_cksum0 = 0; 25351544Seschrock (void) arc_buf_remove_ref(buf, private); 2536789Sahrens goto top; /* restart the IO request */ 2537789Sahrens } 25382391Smaybee /* if this is a prefetch, we don't have a reference */ 25392391Smaybee if (*arc_flags & ARC_PREFETCH) { 25402391Smaybee (void) remove_reference(hdr, hash_lock, 25412391Smaybee private); 25422391Smaybee hdr->b_flags |= ARC_PREFETCH; 25432391Smaybee } 2544*7237Sek110237 if (*arc_flags & ARC_L2CACHE) 2545*7237Sek110237 hdr->b_flags |= ARC_L2CACHE; 25462391Smaybee if (BP_GET_LEVEL(bp) > 0) 25472391Smaybee hdr->b_flags |= ARC_INDIRECT; 2548789Sahrens } else { 2549789Sahrens /* this block is in the ghost cache */ 25501544Seschrock ASSERT(GHOST_STATE(hdr->b_state)); 25511544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 25522391Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0); 25532391Smaybee ASSERT(hdr->b_buf == NULL); 2554789Sahrens 25552391Smaybee /* if this is a prefetch, we don't have a reference */ 25562391Smaybee if (*arc_flags & ARC_PREFETCH) 25572391Smaybee hdr->b_flags |= ARC_PREFETCH; 25582391Smaybee else 25592391Smaybee add_reference(hdr, hash_lock, private); 2560*7237Sek110237 if (*arc_flags & ARC_L2CACHE) 2561*7237Sek110237 hdr->b_flags |= ARC_L2CACHE; 25626245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 25631544Seschrock buf->b_hdr = hdr; 25642688Smaybee buf->b_data = NULL; 25651544Seschrock buf->b_efunc = NULL; 25661544Seschrock buf->b_private = NULL; 25671544Seschrock buf->b_next = NULL; 25681544Seschrock hdr->b_buf = buf; 25692688Smaybee arc_get_data_buf(buf); 25701544Seschrock ASSERT(hdr->b_datacnt == 0); 25711544Seschrock hdr->b_datacnt = 1; 25722391Smaybee 2573789Sahrens } 2574789Sahrens 2575789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 2576789Sahrens acb->acb_done = done; 2577789Sahrens acb->acb_private = private; 2578789Sahrens 2579789Sahrens ASSERT(hdr->b_acb == NULL); 2580789Sahrens hdr->b_acb = acb; 2581789Sahrens hdr->b_flags |= ARC_IO_IN_PROGRESS; 2582789Sahrens 2583789Sahrens /* 2584789Sahrens * If the buffer has been evicted, migrate it to a present state 2585789Sahrens * before issuing the I/O. Once we drop the hash-table lock, 2586789Sahrens * the header will be marked as I/O in progress and have an 2587789Sahrens * attached buffer. At this point, anybody who finds this 2588789Sahrens * buffer ought to notice that it's legit but has a pending I/O. 2589789Sahrens */ 2590789Sahrens 25911544Seschrock if (GHOST_STATE(hdr->b_state)) 25922688Smaybee arc_access(hdr, hash_lock); 2593789Sahrens 25946987Sbrendan if (hdr->b_l2hdr != NULL) { 25956987Sbrendan vd = hdr->b_l2hdr->b_dev->l2ad_vdev; 25966987Sbrendan addr = hdr->b_l2hdr->b_daddr; 25976987Sbrendan } 25986987Sbrendan 25996987Sbrendan mutex_exit(hash_lock); 26006987Sbrendan 2601789Sahrens ASSERT3U(hdr->b_size, ==, size); 26021596Sahrens DTRACE_PROBE3(arc__miss, blkptr_t *, bp, uint64_t, size, 26031596Sahrens zbookmark_t *, zb); 26043403Sbmc ARCSTAT_BUMP(arcstat_misses); 26053403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 26063403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 26073403Sbmc data, metadata, misses); 26081544Seschrock 2609*7237Sek110237 if (l2arc_ndev != 0 && HDR_L2CACHE(hdr)) { 26105450Sbrendan /* 26116987Sbrendan * Lock out device removal. 26125450Sbrendan */ 26136987Sbrendan spa_config_enter(spa, RW_READER, FTAG); 26146987Sbrendan 26156987Sbrendan /* 26166987Sbrendan * Read from the L2ARC if the following are true: 26176987Sbrendan * 1. The L2ARC vdev was previously cached. 26186987Sbrendan * 2. This buffer still has L2ARC metadata. 26196987Sbrendan * 3. This buffer isn't currently writing to the L2ARC. 26206987Sbrendan * 4. The L2ARC entry wasn't evicted, which may 26216987Sbrendan * also have invalidated the vdev. 26226987Sbrendan */ 26236987Sbrendan if (vd != NULL && hdr->b_l2hdr != NULL && 26246987Sbrendan !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr)) { 26255450Sbrendan l2arc_read_callback_t *cb; 26265450Sbrendan 26276643Seschrock if (vdev_is_dead(vd)) 26286987Sbrendan goto l2skip; 26295450Sbrendan 26306643Seschrock DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 26316643Seschrock ARCSTAT_BUMP(arcstat_l2_hits); 26326643Seschrock 26335450Sbrendan cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 26345450Sbrendan KM_SLEEP); 26355450Sbrendan cb->l2rcb_buf = buf; 26365450Sbrendan cb->l2rcb_spa = spa; 26375450Sbrendan cb->l2rcb_bp = *bp; 26385450Sbrendan cb->l2rcb_zb = *zb; 2639*7237Sek110237 cb->l2rcb_flags = zio_flags; 26405450Sbrendan 26415450Sbrendan /* 26425450Sbrendan * l2arc read. 26435450Sbrendan */ 26445450Sbrendan rzio = zio_read_phys(pio, vd, addr, size, 26455450Sbrendan buf->b_data, ZIO_CHECKSUM_OFF, 2646*7237Sek110237 l2arc_read_done, cb, priority, zio_flags | 26476987Sbrendan ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL, 26486987Sbrendan B_FALSE); 26495450Sbrendan DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 26505450Sbrendan zio_t *, rzio); 26516987Sbrendan spa_config_exit(spa, FTAG); 26526987Sbrendan 26536987Sbrendan if (*arc_flags & ARC_NOWAIT) { 26546987Sbrendan zio_nowait(rzio); 26556987Sbrendan return (0); 26566987Sbrendan } 26576987Sbrendan 26586987Sbrendan ASSERT(*arc_flags & ARC_WAIT); 26596987Sbrendan if (zio_wait(rzio) == 0) 26606987Sbrendan return (0); 26616987Sbrendan 26626987Sbrendan /* l2arc read error; goto zio_read() */ 26635450Sbrendan } else { 26645450Sbrendan DTRACE_PROBE1(l2arc__miss, 26655450Sbrendan arc_buf_hdr_t *, hdr); 26665450Sbrendan ARCSTAT_BUMP(arcstat_l2_misses); 26675450Sbrendan if (HDR_L2_WRITING(hdr)) 26685450Sbrendan ARCSTAT_BUMP(arcstat_l2_rw_clash); 26696987Sbrendan l2skip: 26706987Sbrendan spa_config_exit(spa, FTAG); 26715450Sbrendan } 26725450Sbrendan } 26736643Seschrock 2674789Sahrens rzio = zio_read(pio, spa, bp, buf->b_data, size, 2675*7237Sek110237 arc_read_done, buf, priority, zio_flags, zb); 2676789Sahrens 26772391Smaybee if (*arc_flags & ARC_WAIT) 2678789Sahrens return (zio_wait(rzio)); 2679789Sahrens 26802391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 2681789Sahrens zio_nowait(rzio); 2682789Sahrens } 2683789Sahrens return (0); 2684789Sahrens } 2685789Sahrens 2686789Sahrens /* 2687789Sahrens * arc_read() variant to support pool traversal. If the block is already 2688789Sahrens * in the ARC, make a copy of it; otherwise, the caller will do the I/O. 2689789Sahrens * The idea is that we don't want pool traversal filling up memory, but 2690789Sahrens * if the ARC already has the data anyway, we shouldn't pay for the I/O. 2691789Sahrens */ 2692789Sahrens int 2693789Sahrens arc_tryread(spa_t *spa, blkptr_t *bp, void *data) 2694789Sahrens { 2695789Sahrens arc_buf_hdr_t *hdr; 2696789Sahrens kmutex_t *hash_mtx; 2697789Sahrens int rc = 0; 2698789Sahrens 2699789Sahrens hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_mtx); 2700789Sahrens 27011544Seschrock if (hdr && hdr->b_datacnt > 0 && !HDR_IO_IN_PROGRESS(hdr)) { 27021544Seschrock arc_buf_t *buf = hdr->b_buf; 27031544Seschrock 27041544Seschrock ASSERT(buf); 27051544Seschrock while (buf->b_data == NULL) { 27061544Seschrock buf = buf->b_next; 27071544Seschrock ASSERT(buf); 27081544Seschrock } 27091544Seschrock bcopy(buf->b_data, data, hdr->b_size); 27101544Seschrock } else { 2711789Sahrens rc = ENOENT; 27121544Seschrock } 2713789Sahrens 2714789Sahrens if (hash_mtx) 2715789Sahrens mutex_exit(hash_mtx); 2716789Sahrens 2717789Sahrens return (rc); 2718789Sahrens } 2719789Sahrens 27201544Seschrock void 27211544Seschrock arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 27221544Seschrock { 27231544Seschrock ASSERT(buf->b_hdr != NULL); 27243403Sbmc ASSERT(buf->b_hdr->b_state != arc_anon); 27251544Seschrock ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL); 27261544Seschrock buf->b_efunc = func; 27271544Seschrock buf->b_private = private; 27281544Seschrock } 27291544Seschrock 27301544Seschrock /* 27311544Seschrock * This is used by the DMU to let the ARC know that a buffer is 27321544Seschrock * being evicted, so the ARC should clean up. If this arc buf 27331544Seschrock * is not yet in the evicted state, it will be put there. 27341544Seschrock */ 27351544Seschrock int 27361544Seschrock arc_buf_evict(arc_buf_t *buf) 27371544Seschrock { 27382887Smaybee arc_buf_hdr_t *hdr; 27391544Seschrock kmutex_t *hash_lock; 27401544Seschrock arc_buf_t **bufp; 27411544Seschrock 27422887Smaybee mutex_enter(&arc_eviction_mtx); 27432887Smaybee hdr = buf->b_hdr; 27441544Seschrock if (hdr == NULL) { 27451544Seschrock /* 27461544Seschrock * We are in arc_do_user_evicts(). 27471544Seschrock */ 27481544Seschrock ASSERT(buf->b_data == NULL); 27492887Smaybee mutex_exit(&arc_eviction_mtx); 27501544Seschrock return (0); 27511544Seschrock } 27522887Smaybee hash_lock = HDR_LOCK(hdr); 27532887Smaybee mutex_exit(&arc_eviction_mtx); 27541544Seschrock 27551544Seschrock mutex_enter(hash_lock); 27561544Seschrock 27572724Smaybee if (buf->b_data == NULL) { 27582724Smaybee /* 27592724Smaybee * We are on the eviction list. 27602724Smaybee */ 27612724Smaybee mutex_exit(hash_lock); 27622724Smaybee mutex_enter(&arc_eviction_mtx); 27632724Smaybee if (buf->b_hdr == NULL) { 27642724Smaybee /* 27652724Smaybee * We are already in arc_do_user_evicts(). 27662724Smaybee */ 27672724Smaybee mutex_exit(&arc_eviction_mtx); 27682724Smaybee return (0); 27692724Smaybee } else { 27702724Smaybee arc_buf_t copy = *buf; /* structure assignment */ 27712724Smaybee /* 27722724Smaybee * Process this buffer now 27732724Smaybee * but let arc_do_user_evicts() do the reaping. 27742724Smaybee */ 27752724Smaybee buf->b_efunc = NULL; 27762724Smaybee mutex_exit(&arc_eviction_mtx); 27772724Smaybee VERIFY(copy.b_efunc(©) == 0); 27782724Smaybee return (1); 27792724Smaybee } 27802724Smaybee } 27812724Smaybee 27822724Smaybee ASSERT(buf->b_hdr == hdr); 27832724Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt); 27843403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 27851544Seschrock 27861544Seschrock /* 27871544Seschrock * Pull this buffer off of the hdr 27881544Seschrock */ 27891544Seschrock bufp = &hdr->b_buf; 27901544Seschrock while (*bufp != buf) 27911544Seschrock bufp = &(*bufp)->b_next; 27921544Seschrock *bufp = buf->b_next; 27931544Seschrock 27941544Seschrock ASSERT(buf->b_data != NULL); 27952688Smaybee arc_buf_destroy(buf, FALSE, FALSE); 27961544Seschrock 27971544Seschrock if (hdr->b_datacnt == 0) { 27981544Seschrock arc_state_t *old_state = hdr->b_state; 27991544Seschrock arc_state_t *evicted_state; 28001544Seschrock 28011544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 28021544Seschrock 28031544Seschrock evicted_state = 28043403Sbmc (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 28051544Seschrock 28063403Sbmc mutex_enter(&old_state->arcs_mtx); 28073403Sbmc mutex_enter(&evicted_state->arcs_mtx); 28081544Seschrock 28091544Seschrock arc_change_state(evicted_state, hdr, hash_lock); 28101544Seschrock ASSERT(HDR_IN_HASH_TABLE(hdr)); 28115450Sbrendan hdr->b_flags |= ARC_IN_HASH_TABLE; 28125450Sbrendan hdr->b_flags &= ~ARC_BUF_AVAILABLE; 28131544Seschrock 28143403Sbmc mutex_exit(&evicted_state->arcs_mtx); 28153403Sbmc mutex_exit(&old_state->arcs_mtx); 28161544Seschrock } 28171544Seschrock mutex_exit(hash_lock); 28181819Smaybee 28191544Seschrock VERIFY(buf->b_efunc(buf) == 0); 28201544Seschrock buf->b_efunc = NULL; 28211544Seschrock buf->b_private = NULL; 28221544Seschrock buf->b_hdr = NULL; 28231544Seschrock kmem_cache_free(buf_cache, buf); 28241544Seschrock return (1); 28251544Seschrock } 28261544Seschrock 2827789Sahrens /* 2828789Sahrens * Release this buffer from the cache. This must be done 2829789Sahrens * after a read and prior to modifying the buffer contents. 2830789Sahrens * If the buffer has more than one reference, we must make 28317046Sahrens * a new hdr for the buffer. 2832789Sahrens */ 2833789Sahrens void 2834789Sahrens arc_release(arc_buf_t *buf, void *tag) 2835789Sahrens { 2836789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 2837789Sahrens kmutex_t *hash_lock = HDR_LOCK(hdr); 28385450Sbrendan l2arc_buf_hdr_t *l2hdr = NULL; 28395450Sbrendan uint64_t buf_size; 2840789Sahrens 2841789Sahrens /* this buffer is not on any list */ 2842789Sahrens ASSERT(refcount_count(&hdr->b_refcnt) > 0); 28437046Sahrens ASSERT(!(hdr->b_flags & ARC_STORED)); 2844789Sahrens 28453403Sbmc if (hdr->b_state == arc_anon) { 2846789Sahrens /* this buffer is already released */ 2847789Sahrens ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 1); 2848789Sahrens ASSERT(BUF_EMPTY(hdr)); 28491544Seschrock ASSERT(buf->b_efunc == NULL); 28503093Sahrens arc_buf_thaw(buf); 2851789Sahrens return; 2852789Sahrens } 2853789Sahrens 2854789Sahrens mutex_enter(hash_lock); 2855789Sahrens 28561544Seschrock /* 28571544Seschrock * Do we have more than one buf? 28581544Seschrock */ 28591544Seschrock if (hdr->b_buf != buf || buf->b_next != NULL) { 2860789Sahrens arc_buf_hdr_t *nhdr; 2861789Sahrens arc_buf_t **bufp; 2862789Sahrens uint64_t blksz = hdr->b_size; 2863789Sahrens spa_t *spa = hdr->b_spa; 28643290Sjohansen arc_buf_contents_t type = hdr->b_type; 28655450Sbrendan uint32_t flags = hdr->b_flags; 2866789Sahrens 28671544Seschrock ASSERT(hdr->b_datacnt > 1); 2868789Sahrens /* 2869789Sahrens * Pull the data off of this buf and attach it to 2870789Sahrens * a new anonymous buf. 2871789Sahrens */ 28721544Seschrock (void) remove_reference(hdr, hash_lock, tag); 2873789Sahrens bufp = &hdr->b_buf; 28741544Seschrock while (*bufp != buf) 2875789Sahrens bufp = &(*bufp)->b_next; 2876789Sahrens *bufp = (*bufp)->b_next; 28773897Smaybee buf->b_next = NULL; 28781544Seschrock 28793403Sbmc ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size); 28803403Sbmc atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size); 28811544Seschrock if (refcount_is_zero(&hdr->b_refcnt)) { 28824309Smaybee uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type]; 28834309Smaybee ASSERT3U(*size, >=, hdr->b_size); 28844309Smaybee atomic_add_64(size, -hdr->b_size); 28851544Seschrock } 28861544Seschrock hdr->b_datacnt -= 1; 28875450Sbrendan if (hdr->b_l2hdr != NULL) { 28885450Sbrendan mutex_enter(&l2arc_buflist_mtx); 28895450Sbrendan l2hdr = hdr->b_l2hdr; 28905450Sbrendan hdr->b_l2hdr = NULL; 28915450Sbrendan buf_size = hdr->b_size; 28925450Sbrendan } 28933547Smaybee arc_cksum_verify(buf); 28941544Seschrock 2895789Sahrens mutex_exit(hash_lock); 2896789Sahrens 28976245Smaybee nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 2898789Sahrens nhdr->b_size = blksz; 2899789Sahrens nhdr->b_spa = spa; 29003290Sjohansen nhdr->b_type = type; 2901789Sahrens nhdr->b_buf = buf; 29023403Sbmc nhdr->b_state = arc_anon; 2903789Sahrens nhdr->b_arc_access = 0; 29045450Sbrendan nhdr->b_flags = flags & ARC_L2_WRITING; 29055450Sbrendan nhdr->b_l2hdr = NULL; 29061544Seschrock nhdr->b_datacnt = 1; 29073547Smaybee nhdr->b_freeze_cksum = NULL; 29083897Smaybee (void) refcount_add(&nhdr->b_refcnt, tag); 2909789Sahrens buf->b_hdr = nhdr; 29103403Sbmc atomic_add_64(&arc_anon->arcs_size, blksz); 2911789Sahrens } else { 29121544Seschrock ASSERT(refcount_count(&hdr->b_refcnt) == 1); 2913789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 2914789Sahrens ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 29153403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 2916789Sahrens hdr->b_arc_access = 0; 29175450Sbrendan if (hdr->b_l2hdr != NULL) { 29185450Sbrendan mutex_enter(&l2arc_buflist_mtx); 29195450Sbrendan l2hdr = hdr->b_l2hdr; 29205450Sbrendan hdr->b_l2hdr = NULL; 29215450Sbrendan buf_size = hdr->b_size; 29225450Sbrendan } 2923789Sahrens mutex_exit(hash_lock); 29245450Sbrendan 2925789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 2926789Sahrens hdr->b_birth = 0; 2927789Sahrens hdr->b_cksum0 = 0; 29283547Smaybee arc_buf_thaw(buf); 2929789Sahrens } 29301544Seschrock buf->b_efunc = NULL; 29311544Seschrock buf->b_private = NULL; 29325450Sbrendan 29335450Sbrendan if (l2hdr) { 29345450Sbrendan list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 29355450Sbrendan kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 29365450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -buf_size); 29375450Sbrendan } 29385450Sbrendan if (MUTEX_HELD(&l2arc_buflist_mtx)) 29395450Sbrendan mutex_exit(&l2arc_buflist_mtx); 2940789Sahrens } 2941789Sahrens 2942789Sahrens int 2943789Sahrens arc_released(arc_buf_t *buf) 2944789Sahrens { 29453403Sbmc return (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon); 29461544Seschrock } 29471544Seschrock 29481544Seschrock int 29491544Seschrock arc_has_callback(arc_buf_t *buf) 29501544Seschrock { 29511544Seschrock return (buf->b_efunc != NULL); 2952789Sahrens } 2953789Sahrens 29541544Seschrock #ifdef ZFS_DEBUG 29551544Seschrock int 29561544Seschrock arc_referenced(arc_buf_t *buf) 29571544Seschrock { 29581544Seschrock return (refcount_count(&buf->b_hdr->b_refcnt)); 29591544Seschrock } 29601544Seschrock #endif 29611544Seschrock 2962789Sahrens static void 29633547Smaybee arc_write_ready(zio_t *zio) 29643547Smaybee { 29653547Smaybee arc_write_callback_t *callback = zio->io_private; 29663547Smaybee arc_buf_t *buf = callback->awcb_buf; 29675329Sgw25295 arc_buf_hdr_t *hdr = buf->b_hdr; 29685329Sgw25295 29695329Sgw25295 if (zio->io_error == 0 && callback->awcb_ready) { 29703547Smaybee ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt)); 29713547Smaybee callback->awcb_ready(zio, buf, callback->awcb_private); 29723547Smaybee } 29735329Sgw25295 /* 29745329Sgw25295 * If the IO is already in progress, then this is a re-write 29755329Sgw25295 * attempt, so we need to thaw and re-compute the cksum. It is 29765329Sgw25295 * the responsibility of the callback to handle the freeing 29775329Sgw25295 * and accounting for any re-write attempt. If we don't have a 29785329Sgw25295 * callback registered then simply free the block here. 29795329Sgw25295 */ 29805329Sgw25295 if (HDR_IO_IN_PROGRESS(hdr)) { 29815329Sgw25295 if (!BP_IS_HOLE(&zio->io_bp_orig) && 29825329Sgw25295 callback->awcb_ready == NULL) { 29835329Sgw25295 zio_nowait(zio_free(zio, zio->io_spa, zio->io_txg, 29845329Sgw25295 &zio->io_bp_orig, NULL, NULL)); 29855329Sgw25295 } 29865329Sgw25295 mutex_enter(&hdr->b_freeze_lock); 29875329Sgw25295 if (hdr->b_freeze_cksum != NULL) { 29885329Sgw25295 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 29895329Sgw25295 hdr->b_freeze_cksum = NULL; 29905329Sgw25295 } 29915329Sgw25295 mutex_exit(&hdr->b_freeze_lock); 29925329Sgw25295 } 29935450Sbrendan arc_cksum_compute(buf, B_FALSE); 29945329Sgw25295 hdr->b_flags |= ARC_IO_IN_PROGRESS; 29953547Smaybee } 29963547Smaybee 29973547Smaybee static void 2998789Sahrens arc_write_done(zio_t *zio) 2999789Sahrens { 30003547Smaybee arc_write_callback_t *callback = zio->io_private; 30013547Smaybee arc_buf_t *buf = callback->awcb_buf; 30023547Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 3003789Sahrens 3004789Sahrens hdr->b_acb = NULL; 3005789Sahrens 3006789Sahrens hdr->b_dva = *BP_IDENTITY(zio->io_bp); 3007789Sahrens hdr->b_birth = zio->io_bp->blk_birth; 3008789Sahrens hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0]; 30091544Seschrock /* 30101544Seschrock * If the block to be written was all-zero, we may have 30111544Seschrock * compressed it away. In this case no write was performed 30121544Seschrock * so there will be no dva/birth-date/checksum. The buffer 30131544Seschrock * must therefor remain anonymous (and uncached). 30141544Seschrock */ 3015789Sahrens if (!BUF_EMPTY(hdr)) { 3016789Sahrens arc_buf_hdr_t *exists; 3017789Sahrens kmutex_t *hash_lock; 3018789Sahrens 30193093Sahrens arc_cksum_verify(buf); 30203093Sahrens 3021789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 3022789Sahrens if (exists) { 3023789Sahrens /* 3024789Sahrens * This can only happen if we overwrite for 3025789Sahrens * sync-to-convergence, because we remove 3026789Sahrens * buffers from the hash table when we arc_free(). 3027789Sahrens */ 3028789Sahrens ASSERT(DVA_EQUAL(BP_IDENTITY(&zio->io_bp_orig), 3029789Sahrens BP_IDENTITY(zio->io_bp))); 3030789Sahrens ASSERT3U(zio->io_bp_orig.blk_birth, ==, 3031789Sahrens zio->io_bp->blk_birth); 3032789Sahrens 3033789Sahrens ASSERT(refcount_is_zero(&exists->b_refcnt)); 30343403Sbmc arc_change_state(arc_anon, exists, hash_lock); 3035789Sahrens mutex_exit(hash_lock); 30361544Seschrock arc_hdr_destroy(exists); 3037789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 3038789Sahrens ASSERT3P(exists, ==, NULL); 3039789Sahrens } 30401544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 30417046Sahrens /* if it's not anon, we are doing a scrub */ 30427046Sahrens if (hdr->b_state == arc_anon) 30437046Sahrens arc_access(hdr, hash_lock); 30442688Smaybee mutex_exit(hash_lock); 30453547Smaybee } else if (callback->awcb_done == NULL) { 30461544Seschrock int destroy_hdr; 30471544Seschrock /* 30481544Seschrock * This is an anonymous buffer with no user callback, 30491544Seschrock * destroy it if there are no active references. 30501544Seschrock */ 30511544Seschrock mutex_enter(&arc_eviction_mtx); 30521544Seschrock destroy_hdr = refcount_is_zero(&hdr->b_refcnt); 30531544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 30541544Seschrock mutex_exit(&arc_eviction_mtx); 30551544Seschrock if (destroy_hdr) 30561544Seschrock arc_hdr_destroy(hdr); 30571544Seschrock } else { 30581544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3059789Sahrens } 30607046Sahrens hdr->b_flags &= ~ARC_STORED; 30611544Seschrock 30623547Smaybee if (callback->awcb_done) { 3063789Sahrens ASSERT(!refcount_is_zero(&hdr->b_refcnt)); 30643547Smaybee callback->awcb_done(zio, buf, callback->awcb_private); 3065789Sahrens } 3066789Sahrens 30673547Smaybee kmem_free(callback, sizeof (arc_write_callback_t)); 3068789Sahrens } 3069789Sahrens 30707046Sahrens static void 30717046Sahrens write_policy(spa_t *spa, const writeprops_t *wp, 30727046Sahrens int *cksump, int *compp, int *copiesp) 30737046Sahrens { 30747046Sahrens int copies = wp->wp_copies; 30757046Sahrens boolean_t ismd = (wp->wp_level > 0 || dmu_ot[wp->wp_type].ot_metadata); 30767046Sahrens 30777046Sahrens /* Determine copies setting */ 30787046Sahrens if (ismd) 30797046Sahrens copies++; 30807046Sahrens *copiesp = MIN(copies, spa_max_replication(spa)); 30817046Sahrens 30827046Sahrens /* Determine checksum setting */ 30837046Sahrens if (ismd) { 30847046Sahrens /* 30857046Sahrens * Metadata always gets checksummed. If the data 30867046Sahrens * checksum is multi-bit correctable, and it's not a 30877046Sahrens * ZBT-style checksum, then it's suitable for metadata 30887046Sahrens * as well. Otherwise, the metadata checksum defaults 30897046Sahrens * to fletcher4. 30907046Sahrens */ 30917046Sahrens if (zio_checksum_table[wp->wp_oschecksum].ci_correctable && 30927046Sahrens !zio_checksum_table[wp->wp_oschecksum].ci_zbt) 30937046Sahrens *cksump = wp->wp_oschecksum; 30947046Sahrens else 30957046Sahrens *cksump = ZIO_CHECKSUM_FLETCHER_4; 30967046Sahrens } else { 30977046Sahrens *cksump = zio_checksum_select(wp->wp_dnchecksum, 30987046Sahrens wp->wp_oschecksum); 30997046Sahrens } 31007046Sahrens 31017046Sahrens /* Determine compression setting */ 31027046Sahrens if (ismd) { 31037046Sahrens /* 31047046Sahrens * XXX -- we should design a compression algorithm 31057046Sahrens * that specializes in arrays of bps. 31067046Sahrens */ 31077046Sahrens *compp = zfs_mdcomp_disable ? ZIO_COMPRESS_EMPTY : 31087046Sahrens ZIO_COMPRESS_LZJB; 31097046Sahrens } else { 31107046Sahrens *compp = zio_compress_select(wp->wp_dncompress, 31117046Sahrens wp->wp_oscompress); 31127046Sahrens } 31137046Sahrens } 31147046Sahrens 31153547Smaybee zio_t * 31167046Sahrens arc_write(zio_t *pio, spa_t *spa, const writeprops_t *wp, 3117*7237Sek110237 boolean_t l2arc, uint64_t txg, blkptr_t *bp, arc_buf_t *buf, 31183547Smaybee arc_done_func_t *ready, arc_done_func_t *done, void *private, int priority, 3119*7237Sek110237 int zio_flags, const zbookmark_t *zb) 3120789Sahrens { 3121789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 31223547Smaybee arc_write_callback_t *callback; 31233547Smaybee zio_t *zio; 31247046Sahrens int cksum, comp, copies; 31257046Sahrens 3126789Sahrens ASSERT(!HDR_IO_ERROR(hdr)); 31272237Smaybee ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0); 31282237Smaybee ASSERT(hdr->b_acb == 0); 3129*7237Sek110237 if (l2arc) 3130*7237Sek110237 hdr->b_flags |= ARC_L2CACHE; 31313547Smaybee callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 31323547Smaybee callback->awcb_ready = ready; 31333547Smaybee callback->awcb_done = done; 31343547Smaybee callback->awcb_private = private; 31353547Smaybee callback->awcb_buf = buf; 31367046Sahrens 31377046Sahrens write_policy(spa, wp, &cksum, &comp, &copies); 31387046Sahrens zio = zio_write(pio, spa, cksum, comp, copies, txg, bp, 31397046Sahrens buf->b_data, hdr->b_size, arc_write_ready, arc_write_done, 3140*7237Sek110237 callback, priority, zio_flags, zb); 3141789Sahrens 31423547Smaybee return (zio); 3143789Sahrens } 3144789Sahrens 3145789Sahrens int 3146789Sahrens arc_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 3147789Sahrens zio_done_func_t *done, void *private, uint32_t arc_flags) 3148789Sahrens { 3149789Sahrens arc_buf_hdr_t *ab; 3150789Sahrens kmutex_t *hash_lock; 3151789Sahrens zio_t *zio; 3152789Sahrens 3153789Sahrens /* 3154789Sahrens * If this buffer is in the cache, release it, so it 3155789Sahrens * can be re-used. 3156789Sahrens */ 3157789Sahrens ab = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock); 3158789Sahrens if (ab != NULL) { 3159789Sahrens /* 3160789Sahrens * The checksum of blocks to free is not always 3161789Sahrens * preserved (eg. on the deadlist). However, if it is 3162789Sahrens * nonzero, it should match what we have in the cache. 3163789Sahrens */ 3164789Sahrens ASSERT(bp->blk_cksum.zc_word[0] == 0 || 3165789Sahrens ab->b_cksum0 == bp->blk_cksum.zc_word[0]); 31663403Sbmc if (ab->b_state != arc_anon) 31673403Sbmc arc_change_state(arc_anon, ab, hash_lock); 31682391Smaybee if (HDR_IO_IN_PROGRESS(ab)) { 31692391Smaybee /* 31702391Smaybee * This should only happen when we prefetch. 31712391Smaybee */ 31722391Smaybee ASSERT(ab->b_flags & ARC_PREFETCH); 31732391Smaybee ASSERT3U(ab->b_datacnt, ==, 1); 31742391Smaybee ab->b_flags |= ARC_FREED_IN_READ; 31752391Smaybee if (HDR_IN_HASH_TABLE(ab)) 31762391Smaybee buf_hash_remove(ab); 31772391Smaybee ab->b_arc_access = 0; 31782391Smaybee bzero(&ab->b_dva, sizeof (dva_t)); 31792391Smaybee ab->b_birth = 0; 31802391Smaybee ab->b_cksum0 = 0; 31812391Smaybee ab->b_buf->b_efunc = NULL; 31822391Smaybee ab->b_buf->b_private = NULL; 31832391Smaybee mutex_exit(hash_lock); 31842391Smaybee } else if (refcount_is_zero(&ab->b_refcnt)) { 31855450Sbrendan ab->b_flags |= ARC_FREE_IN_PROGRESS; 3186789Sahrens mutex_exit(hash_lock); 31871544Seschrock arc_hdr_destroy(ab); 31883403Sbmc ARCSTAT_BUMP(arcstat_deleted); 3189789Sahrens } else { 31901589Smaybee /* 31912391Smaybee * We still have an active reference on this 31922391Smaybee * buffer. This can happen, e.g., from 31932391Smaybee * dbuf_unoverride(). 31941589Smaybee */ 31952391Smaybee ASSERT(!HDR_IN_HASH_TABLE(ab)); 3196789Sahrens ab->b_arc_access = 0; 3197789Sahrens bzero(&ab->b_dva, sizeof (dva_t)); 3198789Sahrens ab->b_birth = 0; 3199789Sahrens ab->b_cksum0 = 0; 32001544Seschrock ab->b_buf->b_efunc = NULL; 32011544Seschrock ab->b_buf->b_private = NULL; 3202789Sahrens mutex_exit(hash_lock); 3203789Sahrens } 3204789Sahrens } 3205789Sahrens 3206789Sahrens zio = zio_free(pio, spa, txg, bp, done, private); 3207789Sahrens 3208789Sahrens if (arc_flags & ARC_WAIT) 3209789Sahrens return (zio_wait(zio)); 3210789Sahrens 3211789Sahrens ASSERT(arc_flags & ARC_NOWAIT); 3212789Sahrens zio_nowait(zio); 3213789Sahrens 3214789Sahrens return (0); 3215789Sahrens } 3216789Sahrens 32176245Smaybee static int 32186245Smaybee arc_memory_throttle(uint64_t reserve, uint64_t txg) 32196245Smaybee { 32206245Smaybee #ifdef _KERNEL 32216245Smaybee uint64_t inflight_data = arc_anon->arcs_size; 32226245Smaybee uint64_t available_memory = ptob(freemem); 32236245Smaybee static uint64_t page_load = 0; 32246245Smaybee static uint64_t last_txg = 0; 32256245Smaybee 32266245Smaybee #if defined(__i386) 32276245Smaybee available_memory = 32286245Smaybee MIN(available_memory, vmem_size(heap_arena, VMEM_FREE)); 32296245Smaybee #endif 32306245Smaybee if (available_memory >= zfs_write_limit_max) 32316245Smaybee return (0); 32326245Smaybee 32336245Smaybee if (txg > last_txg) { 32346245Smaybee last_txg = txg; 32356245Smaybee page_load = 0; 32366245Smaybee } 32376245Smaybee /* 32386245Smaybee * If we are in pageout, we know that memory is already tight, 32396245Smaybee * the arc is already going to be evicting, so we just want to 32406245Smaybee * continue to let page writes occur as quickly as possible. 32416245Smaybee */ 32426245Smaybee if (curproc == proc_pageout) { 32436245Smaybee if (page_load > MAX(ptob(minfree), available_memory) / 4) 32446245Smaybee return (ERESTART); 32456245Smaybee /* Note: reserve is inflated, so we deflate */ 32466245Smaybee page_load += reserve / 8; 32476245Smaybee return (0); 32486245Smaybee } else if (page_load > 0 && arc_reclaim_needed()) { 32496245Smaybee /* memory is low, delay before restarting */ 32506245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 32516245Smaybee return (EAGAIN); 32526245Smaybee } 32536245Smaybee page_load = 0; 32546245Smaybee 32556245Smaybee if (arc_size > arc_c_min) { 32566245Smaybee uint64_t evictable_memory = 32576245Smaybee arc_mru->arcs_lsize[ARC_BUFC_DATA] + 32586245Smaybee arc_mru->arcs_lsize[ARC_BUFC_METADATA] + 32596245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_DATA] + 32606245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_METADATA]; 32616245Smaybee available_memory += MIN(evictable_memory, arc_size - arc_c_min); 32626245Smaybee } 32636245Smaybee 32646245Smaybee if (inflight_data > available_memory / 4) { 32656245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 32666245Smaybee return (ERESTART); 32676245Smaybee } 32686245Smaybee #endif 32696245Smaybee return (0); 32706245Smaybee } 32716245Smaybee 3272789Sahrens void 32736245Smaybee arc_tempreserve_clear(uint64_t reserve) 3274789Sahrens { 32756245Smaybee atomic_add_64(&arc_tempreserve, -reserve); 3276789Sahrens ASSERT((int64_t)arc_tempreserve >= 0); 3277789Sahrens } 3278789Sahrens 3279789Sahrens int 32806245Smaybee arc_tempreserve_space(uint64_t reserve, uint64_t txg) 3281789Sahrens { 32826245Smaybee int error; 32836245Smaybee 3284789Sahrens #ifdef ZFS_DEBUG 3285789Sahrens /* 3286789Sahrens * Once in a while, fail for no reason. Everything should cope. 3287789Sahrens */ 3288789Sahrens if (spa_get_random(10000) == 0) { 3289789Sahrens dprintf("forcing random failure\n"); 3290789Sahrens return (ERESTART); 3291789Sahrens } 3292789Sahrens #endif 32936245Smaybee if (reserve > arc_c/4 && !arc_no_grow) 32946245Smaybee arc_c = MIN(arc_c_max, reserve * 4); 32956245Smaybee if (reserve > arc_c) 3296982Smaybee return (ENOMEM); 3297982Smaybee 3298789Sahrens /* 32996245Smaybee * Writes will, almost always, require additional memory allocations 33006245Smaybee * in order to compress/encrypt/etc the data. We therefor need to 33016245Smaybee * make sure that there is sufficient available memory for this. 33026245Smaybee */ 33036245Smaybee if (error = arc_memory_throttle(reserve, txg)) 33046245Smaybee return (error); 33056245Smaybee 33066245Smaybee /* 3307982Smaybee * Throttle writes when the amount of dirty data in the cache 3308982Smaybee * gets too large. We try to keep the cache less than half full 3309982Smaybee * of dirty blocks so that our sync times don't grow too large. 3310982Smaybee * Note: if two requests come in concurrently, we might let them 3311982Smaybee * both succeed, when one of them should fail. Not a huge deal. 3312789Sahrens */ 33136245Smaybee if (reserve + arc_tempreserve + arc_anon->arcs_size > arc_c / 2 && 33146245Smaybee arc_anon->arcs_size > arc_c / 4) { 33154309Smaybee dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 33164309Smaybee "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 33174309Smaybee arc_tempreserve>>10, 33184309Smaybee arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 33194309Smaybee arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 33206245Smaybee reserve>>10, arc_c>>10); 3321789Sahrens return (ERESTART); 3322789Sahrens } 33236245Smaybee atomic_add_64(&arc_tempreserve, reserve); 3324789Sahrens return (0); 3325789Sahrens } 3326789Sahrens 3327789Sahrens void 3328789Sahrens arc_init(void) 3329789Sahrens { 3330789Sahrens mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 3331789Sahrens cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 3332789Sahrens 33332391Smaybee /* Convert seconds to clock ticks */ 33342638Sperrin arc_min_prefetch_lifespan = 1 * hz; 33352391Smaybee 3336789Sahrens /* Start out with 1/8 of all memory */ 33373403Sbmc arc_c = physmem * PAGESIZE / 8; 3338789Sahrens 3339789Sahrens #ifdef _KERNEL 3340789Sahrens /* 3341789Sahrens * On architectures where the physical memory can be larger 3342789Sahrens * than the addressable space (intel in 32-bit mode), we may 3343789Sahrens * need to limit the cache to 1/8 of VM size. 3344789Sahrens */ 33453403Sbmc arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 3346789Sahrens #endif 3347789Sahrens 3348982Smaybee /* set min cache to 1/32 of all memory, or 64MB, whichever is more */ 33493403Sbmc arc_c_min = MAX(arc_c / 4, 64<<20); 3350982Smaybee /* set max to 3/4 of all memory, or all but 1GB, whichever is more */ 33513403Sbmc if (arc_c * 8 >= 1<<30) 33523403Sbmc arc_c_max = (arc_c * 8) - (1<<30); 3353789Sahrens else 33543403Sbmc arc_c_max = arc_c_min; 33553403Sbmc arc_c_max = MAX(arc_c * 6, arc_c_max); 33562885Sahrens 33572885Sahrens /* 33582885Sahrens * Allow the tunables to override our calculations if they are 33592885Sahrens * reasonable (ie. over 64MB) 33602885Sahrens */ 33612885Sahrens if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE) 33623403Sbmc arc_c_max = zfs_arc_max; 33633403Sbmc if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max) 33643403Sbmc arc_c_min = zfs_arc_min; 33652885Sahrens 33663403Sbmc arc_c = arc_c_max; 33673403Sbmc arc_p = (arc_c >> 1); 3368789Sahrens 33694309Smaybee /* limit meta-data to 1/4 of the arc capacity */ 33704309Smaybee arc_meta_limit = arc_c_max / 4; 33714645Sek110237 33724645Sek110237 /* Allow the tunable to override if it is reasonable */ 33734645Sek110237 if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 33744645Sek110237 arc_meta_limit = zfs_arc_meta_limit; 33754645Sek110237 33764309Smaybee if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 33774309Smaybee arc_c_min = arc_meta_limit / 2; 33784309Smaybee 3379789Sahrens /* if kmem_flags are set, lets try to use less memory */ 3380789Sahrens if (kmem_debugging()) 33813403Sbmc arc_c = arc_c / 2; 33823403Sbmc if (arc_c < arc_c_min) 33833403Sbmc arc_c = arc_c_min; 3384789Sahrens 33853403Sbmc arc_anon = &ARC_anon; 33863403Sbmc arc_mru = &ARC_mru; 33873403Sbmc arc_mru_ghost = &ARC_mru_ghost; 33883403Sbmc arc_mfu = &ARC_mfu; 33893403Sbmc arc_mfu_ghost = &ARC_mfu_ghost; 33905450Sbrendan arc_l2c_only = &ARC_l2c_only; 33913403Sbmc arc_size = 0; 3392789Sahrens 33933403Sbmc mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 33943403Sbmc mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 33953403Sbmc mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 33963403Sbmc mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 33973403Sbmc mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 33985450Sbrendan mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 33992688Smaybee 34004309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA], 34014309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34024309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_DATA], 34034309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34044309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA], 34054309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34064309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA], 34074309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34084309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA], 34094309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34104309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA], 34114309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34124309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA], 34134309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34144309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA], 34154309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34165450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA], 34175450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34185450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA], 34195450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3420789Sahrens 3421789Sahrens buf_init(); 3422789Sahrens 3423789Sahrens arc_thread_exit = 0; 34241544Seschrock arc_eviction_list = NULL; 34251544Seschrock mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 34262887Smaybee bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 3427789Sahrens 34283403Sbmc arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 34293403Sbmc sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 34303403Sbmc 34313403Sbmc if (arc_ksp != NULL) { 34323403Sbmc arc_ksp->ks_data = &arc_stats; 34333403Sbmc kstat_install(arc_ksp); 34343403Sbmc } 34353403Sbmc 3436789Sahrens (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 3437789Sahrens TS_RUN, minclsyspri); 34383158Smaybee 34393158Smaybee arc_dead = FALSE; 34406987Sbrendan arc_warm = B_FALSE; 34416245Smaybee 34426245Smaybee if (zfs_write_limit_max == 0) 34436245Smaybee zfs_write_limit_max = physmem * PAGESIZE >> 34446245Smaybee zfs_write_limit_shift; 34456245Smaybee else 34466245Smaybee zfs_write_limit_shift = 0; 3447789Sahrens } 3448789Sahrens 3449789Sahrens void 3450789Sahrens arc_fini(void) 3451789Sahrens { 3452789Sahrens mutex_enter(&arc_reclaim_thr_lock); 3453789Sahrens arc_thread_exit = 1; 3454789Sahrens while (arc_thread_exit != 0) 3455789Sahrens cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 3456789Sahrens mutex_exit(&arc_reclaim_thr_lock); 3457789Sahrens 34585642Smaybee arc_flush(NULL); 3459789Sahrens 3460789Sahrens arc_dead = TRUE; 3461789Sahrens 34623403Sbmc if (arc_ksp != NULL) { 34633403Sbmc kstat_delete(arc_ksp); 34643403Sbmc arc_ksp = NULL; 34653403Sbmc } 34663403Sbmc 34671544Seschrock mutex_destroy(&arc_eviction_mtx); 3468789Sahrens mutex_destroy(&arc_reclaim_thr_lock); 3469789Sahrens cv_destroy(&arc_reclaim_thr_cv); 3470789Sahrens 34714309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]); 34724309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); 34734309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]); 34744309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); 34754309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]); 34764309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); 34774309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]); 34784309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); 3479789Sahrens 34803403Sbmc mutex_destroy(&arc_anon->arcs_mtx); 34813403Sbmc mutex_destroy(&arc_mru->arcs_mtx); 34823403Sbmc mutex_destroy(&arc_mru_ghost->arcs_mtx); 34833403Sbmc mutex_destroy(&arc_mfu->arcs_mtx); 34843403Sbmc mutex_destroy(&arc_mfu_ghost->arcs_mtx); 34852856Snd150628 3486789Sahrens buf_fini(); 3487789Sahrens } 34885450Sbrendan 34895450Sbrendan /* 34905450Sbrendan * Level 2 ARC 34915450Sbrendan * 34925450Sbrendan * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 34935450Sbrendan * It uses dedicated storage devices to hold cached data, which are populated 34945450Sbrendan * using large infrequent writes. The main role of this cache is to boost 34955450Sbrendan * the performance of random read workloads. The intended L2ARC devices 34965450Sbrendan * include short-stroked disks, solid state disks, and other media with 34975450Sbrendan * substantially faster read latency than disk. 34985450Sbrendan * 34995450Sbrendan * +-----------------------+ 35005450Sbrendan * | ARC | 35015450Sbrendan * +-----------------------+ 35025450Sbrendan * | ^ ^ 35035450Sbrendan * | | | 35045450Sbrendan * l2arc_feed_thread() arc_read() 35055450Sbrendan * | | | 35065450Sbrendan * | l2arc read | 35075450Sbrendan * V | | 35085450Sbrendan * +---------------+ | 35095450Sbrendan * | L2ARC | | 35105450Sbrendan * +---------------+ | 35115450Sbrendan * | ^ | 35125450Sbrendan * l2arc_write() | | 35135450Sbrendan * | | | 35145450Sbrendan * V | | 35155450Sbrendan * +-------+ +-------+ 35165450Sbrendan * | vdev | | vdev | 35175450Sbrendan * | cache | | cache | 35185450Sbrendan * +-------+ +-------+ 35195450Sbrendan * +=========+ .-----. 35205450Sbrendan * : L2ARC : |-_____-| 35215450Sbrendan * : devices : | Disks | 35225450Sbrendan * +=========+ `-_____-' 35235450Sbrendan * 35245450Sbrendan * Read requests are satisfied from the following sources, in order: 35255450Sbrendan * 35265450Sbrendan * 1) ARC 35275450Sbrendan * 2) vdev cache of L2ARC devices 35285450Sbrendan * 3) L2ARC devices 35295450Sbrendan * 4) vdev cache of disks 35305450Sbrendan * 5) disks 35315450Sbrendan * 35325450Sbrendan * Some L2ARC device types exhibit extremely slow write performance. 35335450Sbrendan * To accommodate for this there are some significant differences between 35345450Sbrendan * the L2ARC and traditional cache design: 35355450Sbrendan * 35365450Sbrendan * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 35375450Sbrendan * the ARC behave as usual, freeing buffers and placing headers on ghost 35385450Sbrendan * lists. The ARC does not send buffers to the L2ARC during eviction as 35395450Sbrendan * this would add inflated write latencies for all ARC memory pressure. 35405450Sbrendan * 35415450Sbrendan * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 35425450Sbrendan * It does this by periodically scanning buffers from the eviction-end of 35435450Sbrendan * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 35445450Sbrendan * not already there. It scans until a headroom of buffers is satisfied, 35455450Sbrendan * which itself is a buffer for ARC eviction. The thread that does this is 35465450Sbrendan * l2arc_feed_thread(), illustrated below; example sizes are included to 35475450Sbrendan * provide a better sense of ratio than this diagram: 35485450Sbrendan * 35495450Sbrendan * head --> tail 35505450Sbrendan * +---------------------+----------+ 35515450Sbrendan * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 35525450Sbrendan * +---------------------+----------+ | o L2ARC eligible 35535450Sbrendan * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 35545450Sbrendan * +---------------------+----------+ | 35555450Sbrendan * 15.9 Gbytes ^ 32 Mbytes | 35565450Sbrendan * headroom | 35575450Sbrendan * l2arc_feed_thread() 35585450Sbrendan * | 35595450Sbrendan * l2arc write hand <--[oooo]--' 35605450Sbrendan * | 8 Mbyte 35615450Sbrendan * | write max 35625450Sbrendan * V 35635450Sbrendan * +==============================+ 35645450Sbrendan * L2ARC dev |####|#|###|###| |####| ... | 35655450Sbrendan * +==============================+ 35665450Sbrendan * 32 Gbytes 35675450Sbrendan * 35685450Sbrendan * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 35695450Sbrendan * evicted, then the L2ARC has cached a buffer much sooner than it probably 35705450Sbrendan * needed to, potentially wasting L2ARC device bandwidth and storage. It is 35715450Sbrendan * safe to say that this is an uncommon case, since buffers at the end of 35725450Sbrendan * the ARC lists have moved there due to inactivity. 35735450Sbrendan * 35745450Sbrendan * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 35755450Sbrendan * then the L2ARC simply misses copying some buffers. This serves as a 35765450Sbrendan * pressure valve to prevent heavy read workloads from both stalling the ARC 35775450Sbrendan * with waits and clogging the L2ARC with writes. This also helps prevent 35785450Sbrendan * the potential for the L2ARC to churn if it attempts to cache content too 35795450Sbrendan * quickly, such as during backups of the entire pool. 35805450Sbrendan * 35816987Sbrendan * 5. After system boot and before the ARC has filled main memory, there are 35826987Sbrendan * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 35836987Sbrendan * lists can remain mostly static. Instead of searching from tail of these 35846987Sbrendan * lists as pictured, the l2arc_feed_thread() will search from the list heads 35856987Sbrendan * for eligible buffers, greatly increasing its chance of finding them. 35866987Sbrendan * 35876987Sbrendan * The L2ARC device write speed is also boosted during this time so that 35886987Sbrendan * the L2ARC warms up faster. Since there have been no ARC evictions yet, 35896987Sbrendan * there are no L2ARC reads, and no fear of degrading read performance 35906987Sbrendan * through increased writes. 35916987Sbrendan * 35926987Sbrendan * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 35935450Sbrendan * the vdev queue can aggregate them into larger and fewer writes. Each 35945450Sbrendan * device is written to in a rotor fashion, sweeping writes through 35955450Sbrendan * available space then repeating. 35965450Sbrendan * 35976987Sbrendan * 7. The L2ARC does not store dirty content. It never needs to flush 35985450Sbrendan * write buffers back to disk based storage. 35995450Sbrendan * 36006987Sbrendan * 8. If an ARC buffer is written (and dirtied) which also exists in the 36015450Sbrendan * L2ARC, the now stale L2ARC buffer is immediately dropped. 36025450Sbrendan * 36035450Sbrendan * The performance of the L2ARC can be tweaked by a number of tunables, which 36045450Sbrendan * may be necessary for different workloads: 36055450Sbrendan * 36065450Sbrendan * l2arc_write_max max write bytes per interval 36076987Sbrendan * l2arc_write_boost extra write bytes during device warmup 36085450Sbrendan * l2arc_noprefetch skip caching prefetched buffers 36095450Sbrendan * l2arc_headroom number of max device writes to precache 36105450Sbrendan * l2arc_feed_secs seconds between L2ARC writing 36115450Sbrendan * 36125450Sbrendan * Tunables may be removed or added as future performance improvements are 36135450Sbrendan * integrated, and also may become zpool properties. 36145450Sbrendan */ 36155450Sbrendan 36165450Sbrendan static void 36175450Sbrendan l2arc_hdr_stat_add(void) 36185450Sbrendan { 36196018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE); 36206018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE); 36215450Sbrendan } 36225450Sbrendan 36235450Sbrendan static void 36245450Sbrendan l2arc_hdr_stat_remove(void) 36255450Sbrendan { 36266018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE)); 36276018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE); 36285450Sbrendan } 36295450Sbrendan 36305450Sbrendan /* 36315450Sbrendan * Cycle through L2ARC devices. This is how L2ARC load balances. 36326987Sbrendan * If a device is returned, this also returns holding the spa config lock. 36335450Sbrendan */ 36345450Sbrendan static l2arc_dev_t * 36355450Sbrendan l2arc_dev_get_next(void) 36365450Sbrendan { 36376987Sbrendan l2arc_dev_t *first, *next = NULL; 36386987Sbrendan 36396987Sbrendan /* 36406987Sbrendan * Lock out the removal of spas (spa_namespace_lock), then removal 36416987Sbrendan * of cache devices (l2arc_dev_mtx). Once a device has been selected, 36426987Sbrendan * both locks will be dropped and a spa config lock held instead. 36436987Sbrendan */ 36446987Sbrendan mutex_enter(&spa_namespace_lock); 36456987Sbrendan mutex_enter(&l2arc_dev_mtx); 36466643Seschrock 36476643Seschrock /* if there are no vdevs, there is nothing to do */ 36486643Seschrock if (l2arc_ndev == 0) 36496987Sbrendan goto out; 36506643Seschrock 36516643Seschrock first = NULL; 36526643Seschrock next = l2arc_dev_last; 36536643Seschrock do { 36546643Seschrock /* loop around the list looking for a non-faulted vdev */ 36556643Seschrock if (next == NULL) { 36565450Sbrendan next = list_head(l2arc_dev_list); 36576643Seschrock } else { 36586643Seschrock next = list_next(l2arc_dev_list, next); 36596643Seschrock if (next == NULL) 36606643Seschrock next = list_head(l2arc_dev_list); 36616643Seschrock } 36626643Seschrock 36636643Seschrock /* if we have come back to the start, bail out */ 36646643Seschrock if (first == NULL) 36656643Seschrock first = next; 36666643Seschrock else if (next == first) 36676643Seschrock break; 36686643Seschrock 36696643Seschrock } while (vdev_is_dead(next->l2ad_vdev)); 36706643Seschrock 36716643Seschrock /* if we were unable to find any usable vdevs, return NULL */ 36726643Seschrock if (vdev_is_dead(next->l2ad_vdev)) 36736987Sbrendan next = NULL; 36745450Sbrendan 36755450Sbrendan l2arc_dev_last = next; 36765450Sbrendan 36776987Sbrendan out: 36786987Sbrendan mutex_exit(&l2arc_dev_mtx); 36796987Sbrendan 36806987Sbrendan /* 36816987Sbrendan * Grab the config lock to prevent the 'next' device from being 36826987Sbrendan * removed while we are writing to it. 36836987Sbrendan */ 36846987Sbrendan if (next != NULL) 36856987Sbrendan spa_config_enter(next->l2ad_spa, RW_READER, next); 36866987Sbrendan mutex_exit(&spa_namespace_lock); 36876987Sbrendan 36885450Sbrendan return (next); 36895450Sbrendan } 36905450Sbrendan 36915450Sbrendan /* 36926987Sbrendan * Free buffers that were tagged for destruction. 36936987Sbrendan */ 36946987Sbrendan static void 36956987Sbrendan l2arc_do_free_on_write() 36966987Sbrendan { 36976987Sbrendan list_t *buflist; 36986987Sbrendan l2arc_data_free_t *df, *df_prev; 36996987Sbrendan 37006987Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 37016987Sbrendan buflist = l2arc_free_on_write; 37026987Sbrendan 37036987Sbrendan for (df = list_tail(buflist); df; df = df_prev) { 37046987Sbrendan df_prev = list_prev(buflist, df); 37056987Sbrendan ASSERT(df->l2df_data != NULL); 37066987Sbrendan ASSERT(df->l2df_func != NULL); 37076987Sbrendan df->l2df_func(df->l2df_data, df->l2df_size); 37086987Sbrendan list_remove(buflist, df); 37096987Sbrendan kmem_free(df, sizeof (l2arc_data_free_t)); 37106987Sbrendan } 37116987Sbrendan 37126987Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 37136987Sbrendan } 37146987Sbrendan 37156987Sbrendan /* 37165450Sbrendan * A write to a cache device has completed. Update all headers to allow 37175450Sbrendan * reads from these buffers to begin. 37185450Sbrendan */ 37195450Sbrendan static void 37205450Sbrendan l2arc_write_done(zio_t *zio) 37215450Sbrendan { 37225450Sbrendan l2arc_write_callback_t *cb; 37235450Sbrendan l2arc_dev_t *dev; 37245450Sbrendan list_t *buflist; 37255450Sbrendan arc_buf_hdr_t *head, *ab, *ab_prev; 37266987Sbrendan l2arc_buf_hdr_t *abl2; 37275450Sbrendan kmutex_t *hash_lock; 37285450Sbrendan 37295450Sbrendan cb = zio->io_private; 37305450Sbrendan ASSERT(cb != NULL); 37315450Sbrendan dev = cb->l2wcb_dev; 37325450Sbrendan ASSERT(dev != NULL); 37335450Sbrendan head = cb->l2wcb_head; 37345450Sbrendan ASSERT(head != NULL); 37355450Sbrendan buflist = dev->l2ad_buflist; 37365450Sbrendan ASSERT(buflist != NULL); 37375450Sbrendan DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 37385450Sbrendan l2arc_write_callback_t *, cb); 37395450Sbrendan 37405450Sbrendan if (zio->io_error != 0) 37415450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_error); 37425450Sbrendan 37435450Sbrendan mutex_enter(&l2arc_buflist_mtx); 37445450Sbrendan 37455450Sbrendan /* 37465450Sbrendan * All writes completed, or an error was hit. 37475450Sbrendan */ 37485450Sbrendan for (ab = list_prev(buflist, head); ab; ab = ab_prev) { 37495450Sbrendan ab_prev = list_prev(buflist, ab); 37505450Sbrendan 37515450Sbrendan hash_lock = HDR_LOCK(ab); 37525450Sbrendan if (!mutex_tryenter(hash_lock)) { 37535450Sbrendan /* 37545450Sbrendan * This buffer misses out. It may be in a stage 37555450Sbrendan * of eviction. Its ARC_L2_WRITING flag will be 37565450Sbrendan * left set, denying reads to this buffer. 37575450Sbrendan */ 37585450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss); 37595450Sbrendan continue; 37605450Sbrendan } 37615450Sbrendan 37625450Sbrendan if (zio->io_error != 0) { 37635450Sbrendan /* 37646987Sbrendan * Error - drop L2ARC entry. 37655450Sbrendan */ 37666987Sbrendan list_remove(buflist, ab); 37676987Sbrendan abl2 = ab->b_l2hdr; 37685450Sbrendan ab->b_l2hdr = NULL; 37696987Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 37706987Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 37715450Sbrendan } 37725450Sbrendan 37735450Sbrendan /* 37745450Sbrendan * Allow ARC to begin reads to this L2ARC entry. 37755450Sbrendan */ 37765450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 37775450Sbrendan 37785450Sbrendan mutex_exit(hash_lock); 37795450Sbrendan } 37805450Sbrendan 37815450Sbrendan atomic_inc_64(&l2arc_writes_done); 37825450Sbrendan list_remove(buflist, head); 37835450Sbrendan kmem_cache_free(hdr_cache, head); 37845450Sbrendan mutex_exit(&l2arc_buflist_mtx); 37855450Sbrendan 37866987Sbrendan l2arc_do_free_on_write(); 37875450Sbrendan 37885450Sbrendan kmem_free(cb, sizeof (l2arc_write_callback_t)); 37895450Sbrendan } 37905450Sbrendan 37915450Sbrendan /* 37925450Sbrendan * A read to a cache device completed. Validate buffer contents before 37935450Sbrendan * handing over to the regular ARC routines. 37945450Sbrendan */ 37955450Sbrendan static void 37965450Sbrendan l2arc_read_done(zio_t *zio) 37975450Sbrendan { 37985450Sbrendan l2arc_read_callback_t *cb; 37995450Sbrendan arc_buf_hdr_t *hdr; 38005450Sbrendan arc_buf_t *buf; 38015450Sbrendan zio_t *rzio; 38025450Sbrendan kmutex_t *hash_lock; 38036987Sbrendan int equal; 38045450Sbrendan 38055450Sbrendan cb = zio->io_private; 38065450Sbrendan ASSERT(cb != NULL); 38075450Sbrendan buf = cb->l2rcb_buf; 38085450Sbrendan ASSERT(buf != NULL); 38095450Sbrendan hdr = buf->b_hdr; 38105450Sbrendan ASSERT(hdr != NULL); 38115450Sbrendan 38125450Sbrendan hash_lock = HDR_LOCK(hdr); 38135450Sbrendan mutex_enter(hash_lock); 38145450Sbrendan 38155450Sbrendan /* 38165450Sbrendan * Check this survived the L2ARC journey. 38175450Sbrendan */ 38185450Sbrendan equal = arc_cksum_equal(buf); 38195450Sbrendan if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 38205450Sbrendan mutex_exit(hash_lock); 38215450Sbrendan zio->io_private = buf; 38225450Sbrendan arc_read_done(zio); 38235450Sbrendan } else { 38245450Sbrendan mutex_exit(hash_lock); 38255450Sbrendan /* 38265450Sbrendan * Buffer didn't survive caching. Increment stats and 38275450Sbrendan * reissue to the original storage device. 38285450Sbrendan */ 38296987Sbrendan if (zio->io_error != 0) { 38305450Sbrendan ARCSTAT_BUMP(arcstat_l2_io_error); 38316987Sbrendan } else { 38326987Sbrendan zio->io_error = EIO; 38336987Sbrendan } 38345450Sbrendan if (!equal) 38355450Sbrendan ARCSTAT_BUMP(arcstat_l2_cksum_bad); 38365450Sbrendan 38376987Sbrendan if (zio->io_waiter == NULL) { 38386987Sbrendan /* 38396987Sbrendan * Let the resent I/O call arc_read_done() instead. 38406987Sbrendan */ 38416987Sbrendan zio->io_done = NULL; 38426987Sbrendan zio->io_flags &= ~ZIO_FLAG_DONT_CACHE; 38436987Sbrendan 38446987Sbrendan rzio = zio_read(NULL, cb->l2rcb_spa, &cb->l2rcb_bp, 38456987Sbrendan buf->b_data, zio->io_size, arc_read_done, buf, 38466987Sbrendan zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb); 38476987Sbrendan 38486987Sbrendan (void) zio_nowait(rzio); 38496987Sbrendan } 38505450Sbrendan } 38515450Sbrendan 38525450Sbrendan kmem_free(cb, sizeof (l2arc_read_callback_t)); 38535450Sbrendan } 38545450Sbrendan 38555450Sbrendan /* 38565450Sbrendan * This is the list priority from which the L2ARC will search for pages to 38575450Sbrendan * cache. This is used within loops (0..3) to cycle through lists in the 38585450Sbrendan * desired order. This order can have a significant effect on cache 38595450Sbrendan * performance. 38605450Sbrendan * 38615450Sbrendan * Currently the metadata lists are hit first, MFU then MRU, followed by 38625450Sbrendan * the data lists. This function returns a locked list, and also returns 38635450Sbrendan * the lock pointer. 38645450Sbrendan */ 38655450Sbrendan static list_t * 38665450Sbrendan l2arc_list_locked(int list_num, kmutex_t **lock) 38675450Sbrendan { 38685450Sbrendan list_t *list; 38695450Sbrendan 38705450Sbrendan ASSERT(list_num >= 0 && list_num <= 3); 38715450Sbrendan 38725450Sbrendan switch (list_num) { 38735450Sbrendan case 0: 38745450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_METADATA]; 38755450Sbrendan *lock = &arc_mfu->arcs_mtx; 38765450Sbrendan break; 38775450Sbrendan case 1: 38785450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_METADATA]; 38795450Sbrendan *lock = &arc_mru->arcs_mtx; 38805450Sbrendan break; 38815450Sbrendan case 2: 38825450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_DATA]; 38835450Sbrendan *lock = &arc_mfu->arcs_mtx; 38845450Sbrendan break; 38855450Sbrendan case 3: 38865450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_DATA]; 38875450Sbrendan *lock = &arc_mru->arcs_mtx; 38885450Sbrendan break; 38895450Sbrendan } 38905450Sbrendan 38915450Sbrendan ASSERT(!(MUTEX_HELD(*lock))); 38925450Sbrendan mutex_enter(*lock); 38935450Sbrendan return (list); 38945450Sbrendan } 38955450Sbrendan 38965450Sbrendan /* 38975450Sbrendan * Evict buffers from the device write hand to the distance specified in 38985450Sbrendan * bytes. This distance may span populated buffers, it may span nothing. 38995450Sbrendan * This is clearing a region on the L2ARC device ready for writing. 39005450Sbrendan * If the 'all' boolean is set, every buffer is evicted. 39015450Sbrendan */ 39025450Sbrendan static void 39035450Sbrendan l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 39045450Sbrendan { 39055450Sbrendan list_t *buflist; 39065450Sbrendan l2arc_buf_hdr_t *abl2; 39075450Sbrendan arc_buf_hdr_t *ab, *ab_prev; 39085450Sbrendan kmutex_t *hash_lock; 39095450Sbrendan uint64_t taddr; 39105450Sbrendan 39115450Sbrendan buflist = dev->l2ad_buflist; 39125450Sbrendan 39135450Sbrendan if (buflist == NULL) 39145450Sbrendan return; 39155450Sbrendan 39165450Sbrendan if (!all && dev->l2ad_first) { 39175450Sbrendan /* 39185450Sbrendan * This is the first sweep through the device. There is 39195450Sbrendan * nothing to evict. 39205450Sbrendan */ 39215450Sbrendan return; 39225450Sbrendan } 39235450Sbrendan 39246987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 39255450Sbrendan /* 39265450Sbrendan * When nearing the end of the device, evict to the end 39275450Sbrendan * before the device write hand jumps to the start. 39285450Sbrendan */ 39295450Sbrendan taddr = dev->l2ad_end; 39305450Sbrendan } else { 39315450Sbrendan taddr = dev->l2ad_hand + distance; 39325450Sbrendan } 39335450Sbrendan DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 39345450Sbrendan uint64_t, taddr, boolean_t, all); 39355450Sbrendan 39365450Sbrendan top: 39375450Sbrendan mutex_enter(&l2arc_buflist_mtx); 39385450Sbrendan for (ab = list_tail(buflist); ab; ab = ab_prev) { 39395450Sbrendan ab_prev = list_prev(buflist, ab); 39405450Sbrendan 39415450Sbrendan hash_lock = HDR_LOCK(ab); 39425450Sbrendan if (!mutex_tryenter(hash_lock)) { 39435450Sbrendan /* 39445450Sbrendan * Missed the hash lock. Retry. 39455450Sbrendan */ 39465450Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 39475450Sbrendan mutex_exit(&l2arc_buflist_mtx); 39485450Sbrendan mutex_enter(hash_lock); 39495450Sbrendan mutex_exit(hash_lock); 39505450Sbrendan goto top; 39515450Sbrendan } 39525450Sbrendan 39535450Sbrendan if (HDR_L2_WRITE_HEAD(ab)) { 39545450Sbrendan /* 39555450Sbrendan * We hit a write head node. Leave it for 39565450Sbrendan * l2arc_write_done(). 39575450Sbrendan */ 39585450Sbrendan list_remove(buflist, ab); 39595450Sbrendan mutex_exit(hash_lock); 39605450Sbrendan continue; 39615450Sbrendan } 39625450Sbrendan 39635450Sbrendan if (!all && ab->b_l2hdr != NULL && 39645450Sbrendan (ab->b_l2hdr->b_daddr > taddr || 39655450Sbrendan ab->b_l2hdr->b_daddr < dev->l2ad_hand)) { 39665450Sbrendan /* 39675450Sbrendan * We've evicted to the target address, 39685450Sbrendan * or the end of the device. 39695450Sbrendan */ 39705450Sbrendan mutex_exit(hash_lock); 39715450Sbrendan break; 39725450Sbrendan } 39735450Sbrendan 39745450Sbrendan if (HDR_FREE_IN_PROGRESS(ab)) { 39755450Sbrendan /* 39765450Sbrendan * Already on the path to destruction. 39775450Sbrendan */ 39785450Sbrendan mutex_exit(hash_lock); 39795450Sbrendan continue; 39805450Sbrendan } 39815450Sbrendan 39825450Sbrendan if (ab->b_state == arc_l2c_only) { 39835450Sbrendan ASSERT(!HDR_L2_READING(ab)); 39845450Sbrendan /* 39855450Sbrendan * This doesn't exist in the ARC. Destroy. 39865450Sbrendan * arc_hdr_destroy() will call list_remove() 39875450Sbrendan * and decrement arcstat_l2_size. 39885450Sbrendan */ 39895450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 39905450Sbrendan arc_hdr_destroy(ab); 39915450Sbrendan } else { 39925450Sbrendan /* 39936987Sbrendan * Invalidate issued or about to be issued 39946987Sbrendan * reads, since we may be about to write 39956987Sbrendan * over this location. 39966987Sbrendan */ 39976987Sbrendan if (HDR_L2_READING(ab)) { 39986987Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_reading); 39996987Sbrendan ab->b_flags |= ARC_L2_EVICTED; 40006987Sbrendan } 40016987Sbrendan 40026987Sbrendan /* 40035450Sbrendan * Tell ARC this no longer exists in L2ARC. 40045450Sbrendan */ 40055450Sbrendan if (ab->b_l2hdr != NULL) { 40065450Sbrendan abl2 = ab->b_l2hdr; 40075450Sbrendan ab->b_l2hdr = NULL; 40085450Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 40095450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 40105450Sbrendan } 40115450Sbrendan list_remove(buflist, ab); 40125450Sbrendan 40135450Sbrendan /* 40145450Sbrendan * This may have been leftover after a 40155450Sbrendan * failed write. 40165450Sbrendan */ 40175450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 40185450Sbrendan } 40195450Sbrendan mutex_exit(hash_lock); 40205450Sbrendan } 40215450Sbrendan mutex_exit(&l2arc_buflist_mtx); 40225450Sbrendan 40235450Sbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, -(taddr - dev->l2ad_evict)); 40245450Sbrendan dev->l2ad_evict = taddr; 40255450Sbrendan } 40265450Sbrendan 40275450Sbrendan /* 40285450Sbrendan * Find and write ARC buffers to the L2ARC device. 40295450Sbrendan * 40305450Sbrendan * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid 40315450Sbrendan * for reading until they have completed writing. 40325450Sbrendan */ 40335450Sbrendan static void 40346987Sbrendan l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) 40355450Sbrendan { 40365450Sbrendan arc_buf_hdr_t *ab, *ab_prev, *head; 40375450Sbrendan l2arc_buf_hdr_t *hdrl2; 40385450Sbrendan list_t *list; 40396987Sbrendan uint64_t passed_sz, write_sz, buf_sz, headroom; 40405450Sbrendan void *buf_data; 40415450Sbrendan kmutex_t *hash_lock, *list_lock; 40425450Sbrendan boolean_t have_lock, full; 40435450Sbrendan l2arc_write_callback_t *cb; 40445450Sbrendan zio_t *pio, *wzio; 40455450Sbrendan 40465450Sbrendan ASSERT(dev->l2ad_vdev != NULL); 40475450Sbrendan 40485450Sbrendan pio = NULL; 40495450Sbrendan write_sz = 0; 40505450Sbrendan full = B_FALSE; 40516245Smaybee head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 40525450Sbrendan head->b_flags |= ARC_L2_WRITE_HEAD; 40535450Sbrendan 40545450Sbrendan /* 40555450Sbrendan * Copy buffers for L2ARC writing. 40565450Sbrendan */ 40575450Sbrendan mutex_enter(&l2arc_buflist_mtx); 40585450Sbrendan for (int try = 0; try <= 3; try++) { 40595450Sbrendan list = l2arc_list_locked(try, &list_lock); 40605450Sbrendan passed_sz = 0; 40615450Sbrendan 40626987Sbrendan /* 40636987Sbrendan * L2ARC fast warmup. 40646987Sbrendan * 40656987Sbrendan * Until the ARC is warm and starts to evict, read from the 40666987Sbrendan * head of the ARC lists rather than the tail. 40676987Sbrendan */ 40686987Sbrendan headroom = target_sz * l2arc_headroom; 40696987Sbrendan if (arc_warm == B_FALSE) 40706987Sbrendan ab = list_head(list); 40716987Sbrendan else 40726987Sbrendan ab = list_tail(list); 40736987Sbrendan 40746987Sbrendan for (; ab; ab = ab_prev) { 40756987Sbrendan if (arc_warm == B_FALSE) 40766987Sbrendan ab_prev = list_next(list, ab); 40776987Sbrendan else 40786987Sbrendan ab_prev = list_prev(list, ab); 40795450Sbrendan 40805450Sbrendan hash_lock = HDR_LOCK(ab); 40815450Sbrendan have_lock = MUTEX_HELD(hash_lock); 40825450Sbrendan if (!have_lock && !mutex_tryenter(hash_lock)) { 40835450Sbrendan /* 40845450Sbrendan * Skip this buffer rather than waiting. 40855450Sbrendan */ 40865450Sbrendan continue; 40875450Sbrendan } 40885450Sbrendan 40895450Sbrendan passed_sz += ab->b_size; 40905450Sbrendan if (passed_sz > headroom) { 40915450Sbrendan /* 40925450Sbrendan * Searched too far. 40935450Sbrendan */ 40945450Sbrendan mutex_exit(hash_lock); 40955450Sbrendan break; 40965450Sbrendan } 40975450Sbrendan 40985450Sbrendan if (ab->b_spa != spa) { 40995450Sbrendan mutex_exit(hash_lock); 41005450Sbrendan continue; 41015450Sbrendan } 41025450Sbrendan 41035450Sbrendan if (ab->b_l2hdr != NULL) { 41045450Sbrendan /* 41055450Sbrendan * Already in L2ARC. 41065450Sbrendan */ 41075450Sbrendan mutex_exit(hash_lock); 41085450Sbrendan continue; 41095450Sbrendan } 41105450Sbrendan 4111*7237Sek110237 if (HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab)) { 41125450Sbrendan mutex_exit(hash_lock); 41135450Sbrendan continue; 41145450Sbrendan } 41155450Sbrendan 41165450Sbrendan if ((write_sz + ab->b_size) > target_sz) { 41175450Sbrendan full = B_TRUE; 41185450Sbrendan mutex_exit(hash_lock); 41195450Sbrendan break; 41205450Sbrendan } 41215450Sbrendan 41225450Sbrendan if (ab->b_buf == NULL) { 41235450Sbrendan DTRACE_PROBE1(l2arc__buf__null, void *, ab); 41245450Sbrendan mutex_exit(hash_lock); 41255450Sbrendan continue; 41265450Sbrendan } 41275450Sbrendan 41285450Sbrendan if (pio == NULL) { 41295450Sbrendan /* 41305450Sbrendan * Insert a dummy header on the buflist so 41315450Sbrendan * l2arc_write_done() can find where the 41325450Sbrendan * write buffers begin without searching. 41335450Sbrendan */ 41345450Sbrendan list_insert_head(dev->l2ad_buflist, head); 41355450Sbrendan 41365450Sbrendan cb = kmem_alloc( 41375450Sbrendan sizeof (l2arc_write_callback_t), KM_SLEEP); 41385450Sbrendan cb->l2wcb_dev = dev; 41395450Sbrendan cb->l2wcb_head = head; 41405450Sbrendan pio = zio_root(spa, l2arc_write_done, cb, 41415450Sbrendan ZIO_FLAG_CANFAIL); 41425450Sbrendan } 41435450Sbrendan 41445450Sbrendan /* 41455450Sbrendan * Create and add a new L2ARC header. 41465450Sbrendan */ 41475450Sbrendan hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP); 41485450Sbrendan hdrl2->b_dev = dev; 41495450Sbrendan hdrl2->b_daddr = dev->l2ad_hand; 41505450Sbrendan 41515450Sbrendan ab->b_flags |= ARC_L2_WRITING; 41525450Sbrendan ab->b_l2hdr = hdrl2; 41535450Sbrendan list_insert_head(dev->l2ad_buflist, ab); 41545450Sbrendan buf_data = ab->b_buf->b_data; 41555450Sbrendan buf_sz = ab->b_size; 41565450Sbrendan 41575450Sbrendan /* 41585450Sbrendan * Compute and store the buffer cksum before 41595450Sbrendan * writing. On debug the cksum is verified first. 41605450Sbrendan */ 41615450Sbrendan arc_cksum_verify(ab->b_buf); 41625450Sbrendan arc_cksum_compute(ab->b_buf, B_TRUE); 41635450Sbrendan 41645450Sbrendan mutex_exit(hash_lock); 41655450Sbrendan 41665450Sbrendan wzio = zio_write_phys(pio, dev->l2ad_vdev, 41675450Sbrendan dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 41685450Sbrendan NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 41695450Sbrendan ZIO_FLAG_CANFAIL, B_FALSE); 41705450Sbrendan 41715450Sbrendan DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 41725450Sbrendan zio_t *, wzio); 41735450Sbrendan (void) zio_nowait(wzio); 41745450Sbrendan 41755450Sbrendan write_sz += buf_sz; 41765450Sbrendan dev->l2ad_hand += buf_sz; 41775450Sbrendan } 41785450Sbrendan 41795450Sbrendan mutex_exit(list_lock); 41805450Sbrendan 41815450Sbrendan if (full == B_TRUE) 41825450Sbrendan break; 41835450Sbrendan } 41845450Sbrendan mutex_exit(&l2arc_buflist_mtx); 41855450Sbrendan 41865450Sbrendan if (pio == NULL) { 41875450Sbrendan ASSERT3U(write_sz, ==, 0); 41885450Sbrendan kmem_cache_free(hdr_cache, head); 41895450Sbrendan return; 41905450Sbrendan } 41915450Sbrendan 41925450Sbrendan ASSERT3U(write_sz, <=, target_sz); 41935450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_sent); 41945450Sbrendan ARCSTAT_INCR(arcstat_l2_size, write_sz); 41955450Sbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, write_sz); 41965450Sbrendan 41975450Sbrendan /* 41985450Sbrendan * Bump device hand to the device start if it is approaching the end. 41995450Sbrendan * l2arc_evict() will already have evicted ahead for this case. 42005450Sbrendan */ 42016987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 42025450Sbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, 42035450Sbrendan dev->l2ad_end - dev->l2ad_hand); 42045450Sbrendan dev->l2ad_hand = dev->l2ad_start; 42055450Sbrendan dev->l2ad_evict = dev->l2ad_start; 42065450Sbrendan dev->l2ad_first = B_FALSE; 42075450Sbrendan } 42085450Sbrendan 42095450Sbrendan (void) zio_wait(pio); 42105450Sbrendan } 42115450Sbrendan 42125450Sbrendan /* 42135450Sbrendan * This thread feeds the L2ARC at regular intervals. This is the beating 42145450Sbrendan * heart of the L2ARC. 42155450Sbrendan */ 42165450Sbrendan static void 42175450Sbrendan l2arc_feed_thread(void) 42185450Sbrendan { 42195450Sbrendan callb_cpr_t cpr; 42205450Sbrendan l2arc_dev_t *dev; 42215450Sbrendan spa_t *spa; 42226987Sbrendan uint64_t size; 42235450Sbrendan 42245450Sbrendan CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 42255450Sbrendan 42265450Sbrendan mutex_enter(&l2arc_feed_thr_lock); 42275450Sbrendan 42285450Sbrendan while (l2arc_thread_exit == 0) { 42295450Sbrendan /* 42306987Sbrendan * Pause for l2arc_feed_secs seconds between writes. 42315450Sbrendan */ 42325450Sbrendan CALLB_CPR_SAFE_BEGIN(&cpr); 42336987Sbrendan (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 42346987Sbrendan lbolt + (hz * l2arc_feed_secs)); 42356987Sbrendan CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 42366987Sbrendan 42376987Sbrendan /* 42386987Sbrendan * Quick check for L2ARC devices. 42396987Sbrendan */ 42406987Sbrendan mutex_enter(&l2arc_dev_mtx); 42416987Sbrendan if (l2arc_ndev == 0) { 42426987Sbrendan mutex_exit(&l2arc_dev_mtx); 42436987Sbrendan continue; 42445450Sbrendan } 42456987Sbrendan mutex_exit(&l2arc_dev_mtx); 42466643Seschrock 42475450Sbrendan /* 42486643Seschrock * This selects the next l2arc device to write to, and in 42496643Seschrock * doing so the next spa to feed from: dev->l2ad_spa. This 42506987Sbrendan * will return NULL if there are now no l2arc devices or if 42516987Sbrendan * they are all faulted. 42526987Sbrendan * 42536987Sbrendan * If a device is returned, its spa's config lock is also 42546987Sbrendan * held to prevent device removal. l2arc_dev_get_next() 42556987Sbrendan * will grab and release l2arc_dev_mtx. 42565450Sbrendan */ 42576987Sbrendan if ((dev = l2arc_dev_get_next()) == NULL) 42585450Sbrendan continue; 42596987Sbrendan 42606987Sbrendan spa = dev->l2ad_spa; 42616987Sbrendan ASSERT(spa != NULL); 42625450Sbrendan 42635450Sbrendan /* 42645450Sbrendan * Avoid contributing to memory pressure. 42655450Sbrendan */ 42665450Sbrendan if (arc_reclaim_needed()) { 42675450Sbrendan ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 42686987Sbrendan spa_config_exit(spa, dev); 42695450Sbrendan continue; 42705450Sbrendan } 42715450Sbrendan 42725450Sbrendan ARCSTAT_BUMP(arcstat_l2_feeds); 42735450Sbrendan 42746987Sbrendan size = dev->l2ad_write; 42756987Sbrendan if (arc_warm == B_FALSE) 42766987Sbrendan size += dev->l2ad_boost; 42776987Sbrendan 42785450Sbrendan /* 42795450Sbrendan * Evict L2ARC buffers that will be overwritten. 42805450Sbrendan */ 42816987Sbrendan l2arc_evict(dev, size, B_FALSE); 42825450Sbrendan 42835450Sbrendan /* 42845450Sbrendan * Write ARC buffers. 42855450Sbrendan */ 42866987Sbrendan l2arc_write_buffers(spa, dev, size); 42876987Sbrendan spa_config_exit(spa, dev); 42885450Sbrendan } 42895450Sbrendan 42905450Sbrendan l2arc_thread_exit = 0; 42915450Sbrendan cv_broadcast(&l2arc_feed_thr_cv); 42925450Sbrendan CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 42935450Sbrendan thread_exit(); 42945450Sbrendan } 42955450Sbrendan 42966643Seschrock boolean_t 42976643Seschrock l2arc_vdev_present(vdev_t *vd) 42986643Seschrock { 42996643Seschrock l2arc_dev_t *dev; 43006643Seschrock 43016643Seschrock mutex_enter(&l2arc_dev_mtx); 43026643Seschrock for (dev = list_head(l2arc_dev_list); dev != NULL; 43036643Seschrock dev = list_next(l2arc_dev_list, dev)) { 43046643Seschrock if (dev->l2ad_vdev == vd) 43056643Seschrock break; 43066643Seschrock } 43076643Seschrock mutex_exit(&l2arc_dev_mtx); 43086643Seschrock 43096643Seschrock return (dev != NULL); 43106643Seschrock } 43116643Seschrock 43125450Sbrendan /* 43135450Sbrendan * Add a vdev for use by the L2ARC. By this point the spa has already 43145450Sbrendan * validated the vdev and opened it. 43155450Sbrendan */ 43165450Sbrendan void 43175450Sbrendan l2arc_add_vdev(spa_t *spa, vdev_t *vd, uint64_t start, uint64_t end) 43185450Sbrendan { 43195450Sbrendan l2arc_dev_t *adddev; 43205450Sbrendan 43216643Seschrock ASSERT(!l2arc_vdev_present(vd)); 43226643Seschrock 43235450Sbrendan /* 43245450Sbrendan * Create a new l2arc device entry. 43255450Sbrendan */ 43265450Sbrendan adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 43275450Sbrendan adddev->l2ad_spa = spa; 43285450Sbrendan adddev->l2ad_vdev = vd; 43295450Sbrendan adddev->l2ad_write = l2arc_write_max; 43306987Sbrendan adddev->l2ad_boost = l2arc_write_boost; 43315450Sbrendan adddev->l2ad_start = start; 43325450Sbrendan adddev->l2ad_end = end; 43335450Sbrendan adddev->l2ad_hand = adddev->l2ad_start; 43345450Sbrendan adddev->l2ad_evict = adddev->l2ad_start; 43355450Sbrendan adddev->l2ad_first = B_TRUE; 43365450Sbrendan ASSERT3U(adddev->l2ad_write, >, 0); 43375450Sbrendan 43385450Sbrendan /* 43395450Sbrendan * This is a list of all ARC buffers that are still valid on the 43405450Sbrendan * device. 43415450Sbrendan */ 43425450Sbrendan adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP); 43435450Sbrendan list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 43445450Sbrendan offsetof(arc_buf_hdr_t, b_l2node)); 43455450Sbrendan 43465450Sbrendan spa_l2cache_space_update(vd, adddev->l2ad_end - adddev->l2ad_hand, 0); 43475450Sbrendan 43485450Sbrendan /* 43495450Sbrendan * Add device to global list 43505450Sbrendan */ 43515450Sbrendan mutex_enter(&l2arc_dev_mtx); 43525450Sbrendan list_insert_head(l2arc_dev_list, adddev); 43535450Sbrendan atomic_inc_64(&l2arc_ndev); 43545450Sbrendan mutex_exit(&l2arc_dev_mtx); 43555450Sbrendan } 43565450Sbrendan 43575450Sbrendan /* 43585450Sbrendan * Remove a vdev from the L2ARC. 43595450Sbrendan */ 43605450Sbrendan void 43615450Sbrendan l2arc_remove_vdev(vdev_t *vd) 43625450Sbrendan { 43635450Sbrendan l2arc_dev_t *dev, *nextdev, *remdev = NULL; 43645450Sbrendan 43655450Sbrendan /* 43665450Sbrendan * Find the device by vdev 43675450Sbrendan */ 43685450Sbrendan mutex_enter(&l2arc_dev_mtx); 43695450Sbrendan for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 43705450Sbrendan nextdev = list_next(l2arc_dev_list, dev); 43715450Sbrendan if (vd == dev->l2ad_vdev) { 43725450Sbrendan remdev = dev; 43735450Sbrendan break; 43745450Sbrendan } 43755450Sbrendan } 43765450Sbrendan ASSERT(remdev != NULL); 43775450Sbrendan 43785450Sbrendan /* 43795450Sbrendan * Remove device from global list 43805450Sbrendan */ 43815450Sbrendan list_remove(l2arc_dev_list, remdev); 43825450Sbrendan l2arc_dev_last = NULL; /* may have been invalidated */ 43836987Sbrendan atomic_dec_64(&l2arc_ndev); 43846987Sbrendan mutex_exit(&l2arc_dev_mtx); 43855450Sbrendan 43865450Sbrendan /* 43875450Sbrendan * Clear all buflists and ARC references. L2ARC device flush. 43885450Sbrendan */ 43895450Sbrendan l2arc_evict(remdev, 0, B_TRUE); 43905450Sbrendan list_destroy(remdev->l2ad_buflist); 43915450Sbrendan kmem_free(remdev->l2ad_buflist, sizeof (list_t)); 43925450Sbrendan kmem_free(remdev, sizeof (l2arc_dev_t)); 43935450Sbrendan } 43945450Sbrendan 43955450Sbrendan void 43965450Sbrendan l2arc_init() 43975450Sbrendan { 43985450Sbrendan l2arc_thread_exit = 0; 43995450Sbrendan l2arc_ndev = 0; 44005450Sbrendan l2arc_writes_sent = 0; 44015450Sbrendan l2arc_writes_done = 0; 44025450Sbrendan 44035450Sbrendan mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 44045450Sbrendan cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 44055450Sbrendan mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 44065450Sbrendan mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL); 44075450Sbrendan mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 44085450Sbrendan 44095450Sbrendan l2arc_dev_list = &L2ARC_dev_list; 44105450Sbrendan l2arc_free_on_write = &L2ARC_free_on_write; 44115450Sbrendan list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 44125450Sbrendan offsetof(l2arc_dev_t, l2ad_node)); 44135450Sbrendan list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 44145450Sbrendan offsetof(l2arc_data_free_t, l2df_list_node)); 44155450Sbrendan 44165450Sbrendan (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 44175450Sbrendan TS_RUN, minclsyspri); 44185450Sbrendan } 44195450Sbrendan 44205450Sbrendan void 44215450Sbrendan l2arc_fini() 44225450Sbrendan { 44236987Sbrendan /* 44246987Sbrendan * This is called from dmu_fini(), which is called from spa_fini(); 44256987Sbrendan * Because of this, we can assume that all l2arc devices have 44266987Sbrendan * already been removed when the pools themselves were removed. 44276987Sbrendan */ 44286987Sbrendan 44295450Sbrendan mutex_enter(&l2arc_feed_thr_lock); 44305450Sbrendan cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 44315450Sbrendan l2arc_thread_exit = 1; 44325450Sbrendan while (l2arc_thread_exit != 0) 44335450Sbrendan cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 44345450Sbrendan mutex_exit(&l2arc_feed_thr_lock); 44355450Sbrendan 44366987Sbrendan l2arc_do_free_on_write(); 44376987Sbrendan 44385450Sbrendan mutex_destroy(&l2arc_feed_thr_lock); 44395450Sbrendan cv_destroy(&l2arc_feed_thr_cv); 44405450Sbrendan mutex_destroy(&l2arc_dev_mtx); 44415450Sbrendan mutex_destroy(&l2arc_buflist_mtx); 44425450Sbrendan mutex_destroy(&l2arc_free_on_write_mtx); 44435450Sbrendan 44445450Sbrendan list_destroy(l2arc_dev_list); 44455450Sbrendan list_destroy(l2arc_free_on_write); 44465450Sbrendan } 4447